allocation_tracer 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e21df296d813762ba6a5b3393ae95cbc09fa2560
4
- data.tar.gz: 089e3fcaa7df385f58c91a3762d1c1f714cbb50a
3
+ metadata.gz: 5591d5370e3ce789b1f83038fb1a569d5d95df2f
4
+ data.tar.gz: 02e0ce857929fb00a7a05b2a4624a1ade3a9c634
5
5
  SHA512:
6
- metadata.gz: a9f98fcbbd4a1e7cac48aaab69e10e9857b63050ffb8ec7e9d8ef9800d9668cfb2715ec2414ed277276edaf3ca53527f1606ec589656b71ea972d33d4a3d8765
7
- data.tar.gz: 7d34cd7d37a60411664a82b94edb9b832cbe00a351c5b9b4da33adce9ee354b98f3f79d99ab594b6253675c1ffe4c6e3a3487052f4e7f7ba9ad586e823ff2f7d
6
+ metadata.gz: ea426ef389e777313fef152336f8c0fdee45710d192ec063ba9996edd2d3cff48a473593a19ee41b98312d70b78160a60edba741891f5ea1461ed9481ac03cd8
7
+ data.tar.gz: 8134ddefac4fb01ca99e0d7442cf1626e4e4a18802f80e5e1ed0f5d0abd3791976c72e88b32d5a52ea8fe411d44bbf4676b623966738f6d79d3c2f58a2897091
data/README.md CHANGED
@@ -179,11 +179,11 @@ will show
179
179
 
180
180
  This output means that the age of 3434 T_OBJECT objects are 0, 96563
181
181
  objects are 1 and 2 objects are 7. Also the age of 3435 T_STRING
182
- objects are 0, 96556 are 1 and so on.
182
+ objects are 0, 96556 objects are 1 and so on.
183
183
 
184
- Note that these number includes living objects and dead objects. For
185
- dead object, age means lifetime. For living objects, age means current
186
- age.
184
+ Note that these numbers includes living objects and dead objects. For
185
+ dead objects, age means lifetime. For living objects, age means
186
+ current age.
187
187
 
188
188
  ## Contributing
189
189
 
@@ -176,6 +176,19 @@ free_key_values_i(st_data_t key, st_data_t value, void *data)
176
176
  return ST_CONTINUE;
177
177
  }
178
178
 
179
+ static void
180
+ delete_lifetime_table(struct traceobj_arg *arg)
181
+ {
182
+ int i;
183
+ if (arg->lifetime_table) {
184
+ for (i=0; i<T_MASK; i++) {
185
+ free(arg->lifetime_table[i]);
186
+ }
187
+ free(arg->lifetime_table);
188
+ arg->lifetime_table = NULL;
189
+ }
190
+ }
191
+
179
192
  static void
180
193
  clear_traceobj_arg(void)
181
194
  {
@@ -187,17 +200,8 @@ clear_traceobj_arg(void)
187
200
  st_clear(arg->object_table);
188
201
  st_foreach(arg->str_table, free_keys_i, 0);
189
202
  st_clear(arg->str_table);
190
-
191
- if (arg->lifetime_table) {
192
- int i;
193
-
194
- for (i=0; i<T_MASK; i++) {
195
- if (arg->lifetime_table[i] != NULL) {
196
- free(arg->lifetime_table[i]);
197
- arg->lifetime_table[i] = NULL;
198
- }
199
- }
200
- }
203
+ arg->freed_allocation_info = 0;
204
+ delete_lifetime_table(arg);
201
205
  }
202
206
 
203
207
  static struct allocation_info *
@@ -316,11 +320,13 @@ aggregate_freed_info(void *data)
316
320
 
317
321
  arg->freed_allocation_info = NULL;
318
322
 
319
- while (info) {
320
- struct allocation_info *next_info = info->next;
321
- aggregate_each_info(arg, info, gc_count);
322
- free_allocation_info(arg, info);
323
- info = next_info;
323
+ if (arg->running) {
324
+ while (info) {
325
+ struct allocation_info *next_info = info->next;
326
+ aggregate_each_info(arg, info, gc_count);
327
+ free_allocation_info(arg, info);
328
+ info = next_info;
329
+ }
324
330
  }
325
331
  }
326
332
 
@@ -340,9 +346,9 @@ add_lifetime_table(size_t **lines, int type, struct allocation_info *info)
340
346
 
341
347
  if (line == NULL) {
342
348
  len = age + 1;
343
- line = lines[type] = malloc(sizeof(size_t) * (1 + len));
349
+ line = lines[type] = calloc(1 + len, sizeof(size_t));
350
+ assert(line != NULL);
344
351
  line[0] = len;
345
- for (i=0; i<len; i++) line[i+1] = 0;
346
352
  }
347
353
  else {
348
354
  len = line[0];
@@ -351,12 +357,18 @@ add_lifetime_table(size_t **lines, int type, struct allocation_info *info)
351
357
  size_t old_len = len;
352
358
  len = age + 1;
353
359
  line = lines[type] = realloc(line, sizeof(size_t) * (1 + len));
354
- for (i=old_len; i<len; i++) line[i+1] = 0;
360
+
361
+ assert(line != NULL);
362
+
363
+ for (i=old_len; i<len; i++) {
364
+ line[i+1] = 0;
365
+ }
366
+
355
367
  line[0] = len;
356
368
  }
357
369
  }
358
370
 
359
- line[age + 1]++;
371
+ line[1 + age]++;
360
372
  }
361
373
 
362
374
  static void
@@ -467,7 +479,7 @@ stop_alloc_hooks(VALUE self)
467
479
  static VALUE
468
480
  type_sym(int type)
469
481
  {
470
- static VALUE syms[T_MASK];
482
+ static VALUE syms[T_MASK] = {0};
471
483
 
472
484
  if (syms[0] == 0) {
473
485
  int i;
@@ -501,6 +513,7 @@ type_sym(int type)
501
513
  TYPE_NAME(T_DATA);
502
514
  default:
503
515
  syms[i] = ID2SYM(rb_intern("unknown"));
516
+ break;
504
517
  #undef TYPE_NAME
505
518
  }
506
519
  }
@@ -546,7 +559,6 @@ aggregate_result_i(st_data_t key, st_data_t val, void *data)
546
559
  }
547
560
  if (arg->keys & KEY_TYPE) {
548
561
  int sym_index = key_buff->data[i++];
549
- assert(T_MASK > sym_index);
550
562
  rb_ary_push(k, type_sym(sym_index));
551
563
  }
552
564
  if (arg->keys & KEY_CLASS) {
@@ -646,6 +658,8 @@ aggregate_result(struct traceobj_arg *arg)
646
658
  VALUE h = rb_hash_new();
647
659
  int i;
648
660
 
661
+ rb_ivar_set(rb_mAllocationTracer, rb_intern("lifetime_table"), h);
662
+
649
663
  for (i=0; i<T_MASK; i++) {
650
664
  size_t *line = arg->lifetime_table[i];
651
665
 
@@ -654,16 +668,15 @@ aggregate_result(struct traceobj_arg *arg)
654
668
  VALUE ary = rb_ary_new();
655
669
  VALUE sym = type_sym(i);
656
670
 
671
+ rb_hash_aset(h, sym, ary);
672
+
657
673
  for (j=0; j<len; j++) {
658
674
  rb_ary_push(ary, SIZET2NUM(line[j+1]));
659
675
  }
660
-
661
- rb_hash_aset(h, sym, ary);
662
676
  }
663
677
  }
664
678
 
665
679
  st_foreach(arg->object_table, lifetime_table_for_live_objects_i, (st_data_t)h);
666
- rb_ivar_set(rb_mAllocationTracer, rb_intern("lifetime_table"), h);
667
680
  }
668
681
 
669
682
  return aar.result;
@@ -805,15 +818,11 @@ allocation_tracer_lifetime_table_setup(VALUE self, VALUE set)
805
818
 
806
819
  if (RTEST(set)) {
807
820
  if (arg->lifetime_table == NULL) {
808
- arg->lifetime_table = (size_t **)calloc(sizeof(size_t **), T_MASK);
809
-
821
+ arg->lifetime_table = (size_t **)calloc(T_MASK, sizeof(size_t **));
810
822
  }
811
823
  }
812
824
  else {
813
- if (arg->lifetime_table != NULL) {
814
- free(arg->lifetime_table);
815
- arg->lifetime_table = NULL;
816
- }
825
+ delete_lifetime_table(arg);
817
826
  }
818
827
 
819
828
  return Qnil;
@@ -1,3 +1,3 @@
1
1
  module ObjectSpace::AllocationTracer
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -2,5 +2,26 @@ require "allocation_tracer/version"
2
2
  require "allocation_tracer/allocation_tracer"
3
3
 
4
4
  module ObjectSpace::AllocationTracer
5
- # Your code goes here...
5
+ def self.collect_lifetime_talbe
6
+ ObjectSpace::AllocationTracer.lifetime_table_setup true
7
+
8
+ if block_given?
9
+ begin
10
+ ObjectSpace::AllocationTracer.trace do
11
+ yield
12
+ end
13
+ ObjectSpace::AllocationTracer.lifetime_table
14
+ ensure
15
+ ObjectSpace::AllocationTracer.lifetime_table_setup false
16
+ end
17
+ else
18
+ ObjectSpace::AllocationTracer.trace
19
+ end
20
+ end
21
+
22
+ def self.collect_lifetime_talbe_stop
23
+ ObjectSpace::AllocationTracer.stop
24
+ result = ObjectSpace::AllocationTracer.lifetime_table
25
+ ObjectSpace::AllocationTracer.lifetime_table_setup false
26
+ end
6
27
  end
@@ -188,7 +188,7 @@ describe ObjectSpace::AllocationTracer do
188
188
  expect(table[:T_NONE]).to be nil
189
189
  end
190
190
 
191
- it 'should nil when ObjectSpace::AllocationTracer.lifetime_table_setup is nil' do
191
+ it 'should return nil when ObjectSpace::AllocationTracer.lifetime_table_setup is false' do
192
192
  ObjectSpace::AllocationTracer.lifetime_table_setup false
193
193
 
194
194
  ObjectSpace::AllocationTracer.trace do
@@ -202,5 +202,34 @@ describe ObjectSpace::AllocationTracer do
202
202
 
203
203
  expect(table).to be nil
204
204
  end
205
+
206
+ it 'should return nil getting it twice' do
207
+ ObjectSpace::AllocationTracer.trace do
208
+ 100000.times{
209
+ Object.new
210
+ ''
211
+ }
212
+ end
213
+
214
+ table = ObjectSpace::AllocationTracer.lifetime_table
215
+ table = ObjectSpace::AllocationTracer.lifetime_table
216
+
217
+ expect(table).to be nil
218
+ end
219
+ end
220
+
221
+ describe 'ObjectSpace::AllocationTracer.collect_lifetime_talbe' do
222
+ it 'should collect lifetime table' do
223
+ table = ObjectSpace::AllocationTracer.collect_lifetime_talbe do
224
+ 100000.times{
225
+ Object.new
226
+ ''
227
+ }
228
+ end
229
+
230
+ expect(table[:T_OBJECT].inject(&:+)).to be >= 10_000
231
+ expect(table[:T_STRING].inject(&:+)).to be >= 10_000
232
+ expect(table[:T_NONE]).to be nil
233
+ end
205
234
  end
206
235
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: allocation_tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koichi Sasada