27
27
#include <ruby/st.h>
28
28
#include <ruby/io.h>
29
29
#include <ruby/re.h>
30
#include <../../node.h>
32
35
size_t rb_str_memsize(VALUE);
33
36
size_t rb_ary_memsize(VALUE);
34
size_t rb_io_memsize(rb_io_t *);
35
size_t onig_memsize(regex_t *);
37
size_t rb_io_memsize(const rb_io_t *);
36
38
size_t rb_generic_ivar_memsize(VALUE);
37
39
size_t rb_objspace_data_type_memsize(VALUE obj);
39
void rb_objspace_each_objects(
40
int (*callback)(void *start, void *end, size_t stride, void *data),
44
42
memsize_of(VALUE obj)
72
70
if (RCLASS(obj)->ptr->iv_tbl) {
73
71
size += st_memsize(RCLASS(obj)->ptr->iv_tbl);
73
if (RCLASS(obj)->ptr->const_tbl) {
74
size += st_memsize(RCLASS(obj)->ptr->const_tbl);
75
76
size += sizeof(rb_classext_t);
159
160
* Return consuming memory size of obj.
161
* Note that this information is incomplete. You need to deal with
162
* this information as only a *HINT*. Especaially, the size of
163
* T_DATA may not right size.
162
* Note that the return size is incomplete. You need to deal with
163
* this information as only a *HINT*. Especially, the size of
164
* T_DATA may not be correct.
165
166
* This method is not expected to work except C Ruby.
171
172
return SIZET2NUM(memsize_of(obj));
181
total_i(void *vstart, void *vend, size_t stride, void *ptr)
184
struct total_data *data = (struct total_data *)ptr;
186
for (v = (VALUE)vstart; v != (VALUE)vend; v += stride) {
187
if (RBASIC(v)->flags) {
188
switch (BUILTIN_TYPE(v)) {
195
if (FL_TEST(v, FL_SINGLETON))
198
if (data->klass == 0 || rb_obj_is_kind_of(v, data->klass)) {
199
data->total += memsize_of(v);
210
* ObjectSpace.memsize_of_all([klass]) -> Integer
212
* Return consuming memory size of all living objects.
213
* If klass (should be Class object) is given, return the total
214
* memory size of instances of the given class.
216
* Note that the returned size is incomplete. You need to deal with
217
* this information as only a *HINT*. Especially, the size of
218
* T_DATA may not be correct.
220
* Note that this method does *NOT* return total malloc'ed memory size.
222
* This method can be defined by the following Ruby code:
224
* def memsize_of_all klass = false
226
* ObjectSpace.each_objects{|e|
227
* total += ObjectSpace.memsize_of(e) if klass == false || e.kind_of?(klass)
232
* This method is not expected to work except C Ruby.
236
memsize_of_all_m(int argc, VALUE *argv, VALUE self)
238
struct total_data data = {0, 0};
241
rb_scan_args(argc, argv, "01", &data.klass);
244
rb_objspace_each_objects(total_i, &data);
245
return SIZET2NUM(data.total);
175
249
set_zero_i(st_data_t key, st_data_t val, st_data_t arg)
201
275
* Counts objects size (in bytes) for each type.
203
277
* Note that this information is incomplete. You need to deal with
204
* this information as only a *HINT*. Especaially, total size of
278
* this information as only a *HINT*. Especially, total size of
205
279
* T_DATA may not right size.
207
281
* It returns a hash as:
474
548
for (; v != (VALUE)vend; v += stride) {
475
549
if (RBASIC(v)->flags && BUILTIN_TYPE(v) == T_DATA) {
476
VALUE counter = rb_hash_aref(hash, RBASIC(v)->klass);
551
VALUE key = RBASIC(v)->klass;
554
const char *name = rb_objspace_data_type_name(v);
555
if (name == 0) name = "unknown";
556
key = ID2SYM(rb_intern(name));
559
counter = rb_hash_aref(hash, key);
477
560
if (NIL_P(counter)) {
478
561
counter = INT2FIX(1);
481
564
counter = INT2FIX(FIX2INT(counter) + 1);
483
rb_hash_aset(hash, RBASIC(v)->klass, counter);
567
rb_hash_aset(hash, key, counter);
492
576
* ObjectSpace.count_tdata_objects([result_hash]) -> hash
494
* Counts nodes for each node type.
578
* Counts objects for each T_DATA type.
496
580
* This method is not for ordinary Ruby programmers, but for MRI developers
497
581
* who interest on MRI performance.
499
583
* It returns a hash as:
500
* {:NODE_METHOD=>2027, :NODE_FBODY=>1927, :NODE_CFUNC=>1798, ...}
584
* {RubyVM::InstructionSequence=>504, :parser=>5, :barrier=>6,
585
* :mutex=>6, Proc=>60, RubyVM::Env=>57, Mutex=>1, Encoding=>99,
586
* ThreadGroup=>1, Binding=>1, Thread=>1, RubyVM=>1, :iseq=>1,
587
* Random=>1, ARGF.class=>1, Data=>1, :autoload=>3, Time=>2}
588
* # T_DATA objects existing at startup on r32276.
502
590
* If the optional argument, result_hash, is given,
503
591
* it is overwritten and returned.
506
594
* The contents of the returned hash is implementation defined.
507
595
* It may be changed in future.
597
* In this version, keys are Class object or Symbol object.
598
* If object is kind of normal (accessible) object, the key is Class object.
599
* If object is not a kind of normal (internal) object, the key is symbol
600
* name, registered by rb_data_type_struct.
509
602
* This method is not expected to work except C Ruby.
548
641
VALUE rb_mObjSpace = rb_const_get(rb_cObject, rb_intern("ObjectSpace"));
643
rb_define_module_function(rb_mObjSpace, "memsize_of", memsize_of_m, 1);
644
rb_define_module_function(rb_mObjSpace, "memsize_of_all",
645
memsize_of_all_m, -1);
550
647
rb_define_module_function(rb_mObjSpace, "count_objects_size", count_objects_size, -1);
551
rb_define_module_function(rb_mObjSpace, "memsize_of", memsize_of_m, 1);
552
648
rb_define_module_function(rb_mObjSpace, "count_nodes", count_nodes, -1);
553
649
rb_define_module_function(rb_mObjSpace, "count_tdata_objects", count_tdata_objects, -1);