Module: Memprof
- Defined in:
- lib/memprof/tracer.rb,
lib/memprof/middleware.rb,
ext/memprof.c
Defined Under Namespace
Modules: Filter Classes: Middleware, Tracer, Unsupported
Class Method Summary collapse
- .dump(*args) ⇒ Object
- .dump_all(*args) ⇒ Object
- .start ⇒ Object
- .stats(*args) ⇒ Object
- .stats!(*args) ⇒ Object
- .stop ⇒ Object
- .trace(*args) ⇒ Object
- .trace_filename ⇒ Object
- .trace_filename=(*args) ⇒ Object
- .trace_request(env) ⇒ Object
- .track(*args) ⇒ Object
Class Method Details
.dump(*args) ⇒ Object
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 |
# File 'ext/memprof.c', line 1602
static VALUE
memprof_dump(int argc, VALUE *argv, VALUE self)
{
VALUE ret = Qnil;
int old = track_objs;
if (rb_block_given_p()) {
memprof_start(self);
ret = rb_yield(Qnil);
} else if (!track_objs)
rb_raise(rb_eRuntimeError, "object tracking disabled, call Memprof.start first");
track_objs = 0;
json_gen gen = json_for_args(argc, argv);
st_foreach(objs, objs_each_dump, (st_data_t)gen);
json_free(gen);
if (rb_block_given_p())
memprof_stop(self);
track_objs = old;
return ret;
}
|
.dump_all(*args) ⇒ Object
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 |
# File 'ext/memprof.c', line 1627
static VALUE
memprof_dump_all(int argc, VALUE *argv, VALUE self)
{
if (memprof_config.heaps == NULL ||
memprof_config.heaps_used == NULL ||
memprof_config.sizeof_RVALUE == 0 ||
memprof_config.sizeof_heaps_slot == 0 ||
memprof_config.offset_heaps_slot_slot == SIZE_MAX ||
memprof_config.offset_heaps_slot_limit == SIZE_MAX)
rb_raise(eUnsupported, "not enough config data to dump heap");
char *heaps = *(char**)memprof_config.heaps;
int heaps_used = *(int*)memprof_config.heaps_used;
char *p, *pend;
int i, limit;
VALUE str;
char *filename = NULL;
char *in_progress_filename = NULL;
FILE *out = NULL;
rb_scan_args(argc, argv, "01", &str);
if (RTEST(str)) {
filename = StringValueCStr(str);
size_t filename_len = strlen(filename);
in_progress_filename = alloca(filename_len + 13);
memcpy(in_progress_filename, filename, filename_len);
memcpy(in_progress_filename + filename_len, ".IN_PROGRESS\0", 13);
out = fopen(in_progress_filename, "w");
if (!out)
rb_raise(rb_eArgError, "unable to open output file");
}
json_gen_config conf = { .beautify = 0, .indentString = " " };
json_gen gen = json_gen_alloc2((json_print_t)&json_print, &conf, NULL, (void*)out);
track_objs = 0;
memprof_dump_finalizers(gen);
memprof_dump_globals(gen);
memprof_dump_stack(gen);
for (i=0; i < heaps_used; i++) {
p = *(char**)(heaps + (i * memprof_config.sizeof_heaps_slot) + memprof_config.offset_heaps_slot_slot);
limit = *(int*)(heaps + (i * memprof_config.sizeof_heaps_slot) + memprof_config.offset_heaps_slot_limit);
pend = p + (memprof_config.sizeof_RVALUE * limit);
while (p < pend) {
if (RBASIC(p)->flags) {
obj_dump((VALUE)p, gen);
json_gen_reset(gen);
}
p += memprof_config.sizeof_RVALUE;
}
}
memprof_dump_lsof(gen);
memprof_dump_ps(gen);
json_gen_clear(gen);
json_gen_free(gen);
if (out) {
fclose(out);
rename(in_progress_filename, filename);
}
track_objs = 1;
return Qnil;
}
|
.start ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'ext/memprof.c', line 233
static VALUE
memprof_start(VALUE self)
{
if (!memprof_started) {
insert_tramp("rb_newobj", newobj_tramp);
insert_tramp("add_freelist", freelist_tramp);
memprof_started = 1;
}
if (track_objs == 1)
return Qfalse;
track_objs = 1;
return Qtrue;
}
|
.stats(*args) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'ext/memprof.c', line 270
static VALUE
memprof_stats(int argc, VALUE *argv, VALUE self)
{
st_table *tmp_table;
struct results res;
size_t i;
VALUE str;
FILE *out = NULL;
if (!track_objs)
rb_raise(rb_eRuntimeError, "object tracking disabled, call Memprof.start first");
rb_scan_args(argc, argv, "01", &str);
if (RTEST(str)) {
out = fopen(StringValueCStr(str), "w");
if (!out)
rb_raise(rb_eArgError, "unable to open output file");
}
track_objs = 0;
tmp_table = st_init_strtable();
st_foreach(objs, objs_tabulate, (st_data_t)tmp_table);
res.num_entries = 0;
res.entries = malloc(sizeof(char*) * tmp_table->num_entries);
st_foreach(tmp_table, objs_to_array, (st_data_t)&res);
st_free_table(tmp_table);
qsort(res.entries, res.num_entries, sizeof(char*), &memprof_strcmp);
for (i=0; i < res.num_entries; i++) {
fprintf(out ? out : stderr, "%s\n", res.entries[i]);
free(res.entries[i]);
}
free(res.entries);
if (out)
fclose(out);
track_objs = 1;
return Qnil;
}
|
.stats!(*args) ⇒ Object
316 317 318 319 320 321 322 |
# File 'ext/memprof.c', line 316
static VALUE
memprof_stats_bang(int argc, VALUE *argv, VALUE self)
{
memprof_stats(argc, argv, self);
st_foreach(objs, objs_free, (st_data_t)0);
return Qnil;
}
|
.stop ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'ext/memprof.c', line 249
static VALUE
memprof_stop(VALUE self)
{
/* TODO: remove trampolines and set memprof_started = 0 */
if (track_objs == 0)
return Qfalse;
track_objs = 0;
st_foreach(objs, objs_free, (st_data_t)0);
return Qtrue;
}
|
.trace(*args) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'ext/memprof.c', line 412
static VALUE
memprof_trace(int argc, VALUE *argv, VALUE self)
{
if (!rb_block_given_p())
rb_raise(rb_eArgError, "block required");
json_gen gen = json_for_args(argc, argv);
trace_set_output(gen);
json_gen_map_open(gen);
trace_invoke_all(TRACE_RESET);
trace_invoke_all(TRACE_START);
VALUE ret = rb_yield(Qnil);
trace_invoke_all(TRACE_DUMP);
trace_invoke_all(TRACE_STOP);
json_gen_map_close(gen);
json_gen_reset(gen);
json_free(gen);
trace_set_output(NULL);
return ret;
}
|
.trace_filename ⇒ Object
477 478 479 480 481 |
# File 'ext/memprof.c', line 477
static VALUE
memprof_trace_filename_get(VALUE self)
{
return tracing_json_filename;
}
|
.trace_filename=(*args) ⇒ Object
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'ext/memprof.c', line 459
static VALUE
memprof_trace_filename_set(int argc, VALUE *argv, VALUE self)
{
if (tracing_json_gen) {
json_free(tracing_json_gen);
tracing_json_gen = NULL;
}
if (!RTEST(*argv)) {
tracing_json_filename = Qnil;
} else {
tracing_json_gen = json_for_args(argc, argv);
tracing_json_filename = *argv;
}
return tracing_json_filename;
}
|
.trace_request(env) ⇒ Object
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
# File 'ext/memprof.c', line 483
static VALUE
memprof_trace_request(VALUE self, VALUE env)
{
if (!rb_block_given_p())
rb_raise(rb_eArgError, "block required");
uint64_t start_time;
uint64_t end_time;
char str_time[32];
json_gen gen;
if (tracing_json_gen)
gen = tracing_json_gen;
else
gen = json_for_args(0, NULL);
json_gen_map_open(gen);
json_gen_cstr(gen, "start");
start_time = timeofday_ms();
sprintf(str_time, "%" PRIu64, start_time);
json_gen_number(gen, str_time, strlen(str_time));
json_gen_cstr(gen, "tracers");
json_gen_map_open(gen);
trace_set_output(gen);
trace_invoke_all(TRACE_RESET);
trace_invoke_all(TRACE_START);
start_time = timeofday_ms();
VALUE ret = rb_yield(Qnil);
end_time = timeofday_ms();
trace_invoke_all(TRACE_DUMP);
trace_invoke_all(TRACE_STOP);
json_gen_map_close(gen);
if (RTEST(env) && TYPE(env) == T_HASH) {
VALUE val, str;
val = rb_hash_aref(env, rb_str_new2("action_controller.request.path_parameters"));
if (!RTEST(val))
val = rb_hash_aref(env, rb_str_new2("action_dispatch.request.parameters"));
if (RTEST(val) && TYPE(val) == T_HASH) {
json_gen_cstr(gen, "rails");
json_gen_map_open(gen);
str = rb_hash_aref(val, rb_str_new2("controller"));
if (RTEST(str) && TYPE(str) == T_STRING) {
json_gen_cstr(gen, "controller");
json_gen_cstr(gen, RSTRING_PTR(str));
}
str = rb_hash_aref(val, rb_str_new2("action"));
if (RTEST(str) && TYPE(str) == T_STRING) {
json_gen_cstr(gen, "action");
json_gen_cstr(gen, RSTRING_PTR(str));
}
json_gen_map_close(gen);
}
json_gen_cstr(gen, "request");
json_gen_map_open(gen);
// struct RHash *hash = RHASH(env);
// st_foreach(hash->tbl, each_request_entry, (st_data_t)gen);
#define DUMP_HASH_ENTRY(key) do { \
str = rb_hash_aref(env, rb_str_new2(key)); \
if (RTEST(str) && \
TYPE(str) == T_STRING && \
RSTRING_PTR(str)) { \
json_gen_cstr(gen, key); \
json_gen_cstr(gen, RSTRING_PTR(str)); \
} \
} while(0)
// DUMP_HASH_ENTRY("HTTP_USER_AGENT");
DUMP_HASH_ENTRY("REQUEST_PATH");
DUMP_HASH_ENTRY("PATH_INFO");
DUMP_HASH_ENTRY("REMOTE_ADDR");
DUMP_HASH_ENTRY("REQUEST_URI");
DUMP_HASH_ENTRY("REQUEST_METHOD");
DUMP_HASH_ENTRY("QUERY_STRING");
json_gen_map_close(gen);
}
if (RTEST(ret) && TYPE(ret) == T_ARRAY) {
json_gen_cstr(gen, "response");
json_gen_map_open(gen);
json_gen_cstr(gen, "code");
json_gen_value(gen, RARRAY_PTR(ret)[0]);
json_gen_map_close(gen);
}
json_gen_cstr(gen, "time");
json_gen_integer(gen, end_time-start_time);
json_gen_map_close(gen);
json_gen_reset(gen);
if (gen != tracing_json_gen)
json_free(gen);
return ret;
}
|
.track(*args) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'ext/memprof.c', line 337
static VALUE
memprof_track(int argc, VALUE *argv, VALUE self)
{
if (!rb_block_given_p())
rb_raise(rb_eArgError, "block required");
memprof_start(self);
rb_yield(Qnil);
memprof_stats(argc, argv, self);
memprof_stop(self);
return Qnil;
}
|