Module: HeapDump

Defined in:
lib/heap_dump.rb,
lib/heap_dump/version.rb,
ext/heap_dump/heap_dump.c

Constant Summary collapse

VERSION =
"0.0.6"

Class Method Summary collapse

Class Method Details

.dump(filename = 'dump.json', gc_before_dump = true) ⇒ Object

Dumps ruby object space to file



12
13
14
15
# File 'lib/heap_dump.rb', line 12

def self.dump filename='dump.json', gc_before_dump=true
  GC.start if gc_before_dump
  return dump_ext(filename)
end

.dump_ext(filename) ⇒ Object



1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
# File 'ext/heap_dump/heap_dump.c', line 1244

static VALUE
rb_heapdump_dump(VALUE self, VALUE filename)
{
  struct walk_ctx ctx_o, *ctx = &ctx_o;
  memset(ctx, 0, sizeof(*ctx));

  Check_Type(filename, T_STRING);

  printf("Dump should go to %s\n", RSTRING_PTR(filename));
  ctx->file = fopen(RSTRING_PTR(filename), "wt");
  ctx->yajl = yajl_gen_alloc(NULL,NULL);
  yajl_gen_array_open(ctx->yajl);

  //dump origins:
  yajl_gen_map_open(ctx->yajl);
  ygh_cstring("id", "_ROOTS_");

  printf("machine context\n");

  dump_machine_context(ctx);
  flush_yajl(ctx);

  struct gc_list *list;
  /* mark protected global variables */
  printf("global_List\n");
  for (list = GET_THREAD()->vm->global_List; list; list = list->next) {
    VALUE v = *list->varptr;
    //printf("global %p\n", v);
  }

  yg_cstring("classes");
  yajl_gen_array_open(ctx->yajl);
  printf("classes\n");
  if (rb_class_tbl && rb_class_tbl->num_entries > 0)
    st_foreach(rb_class_tbl, dump_iv_entry1, (st_data_t)ctx);
  else printf("no classes\n");
  yajl_gen_array_close(ctx->yajl);
  flush_yajl(ctx);

  //TODO: other gc entry points - symbols, encodings, etc.

  yajl_gen_map_close(ctx->yajl); //id:roots
  flush_yajl(ctx);

  //now dump all live objects
  printf("starting objspace walk\n");
  rb_objspace_each_objects(objspace_walker, ctx);

  yajl_gen_array_close(ctx->yajl);
  flush_yajl(ctx);
  yajl_gen_free(ctx->yajl);
  fclose(ctx->file);

  printf("Walker called %d times, seen %d live objects.\n", ctx->walker_called, ctx->live_objects);

  return Qnil;
}