Skip to content

Commit 79e6453

Browse files
committed
support the new heapsnapshot API
No longer returns an object we have to dump ourselves. Instead we pass a filename at the beginning where snapshots will be stored immediately, with shared data (strings, types, static frames) appended at the end.
1 parent d016ffd commit 79e6453

File tree

1 file changed

+11
-90
lines changed

1 file changed

+11
-90
lines changed

src/vm/moar/HLL/Backend.nqp

Lines changed: 11 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ class HLL::Backend::MoarVM {
5656
if nqp::defined(@END);
5757

5858
self.ensure_prof_routines();
59-
$prof_start_sub(nqp::hash('kind', $kind));
59+
60+
if $kind eq "heap" {
61+
unless nqp::defined($filename) {
62+
$filename := 'heap-snapshot-' ~ nqp::time_n();
63+
}
64+
$prof_start_sub(nqp::hash('kind', $kind, 'path', $filename));
65+
} else {
66+
$prof_start_sub(nqp::hash('kind', $kind));
67+
}
68+
6069
my $res := $what();
6170
unless nqp::defined(@END) {
6271
my $data := $prof_end_sub();
@@ -402,95 +411,7 @@ class HLL::Backend::MoarVM {
402411
}
403412

404413
method dump_heap_profile_data($data, $filename) {
405-
unless nqp::defined($filename) {
406-
$filename := 'heap-snapshot-' ~ nqp::time_n();
407-
}
408-
note("Writing heap snapshot to $filename");
409-
my $hs_fh := open($filename, :w);
410-
411-
sub write_json($obj) {
412-
my $escaped_backslash := q{\\\\};
413-
my $escaped_dquote := q{\\"};
414-
my @pieces := nqp::list_s();
415-
sub to_json($obj) {
416-
if nqp::islist($obj) {
417-
nqp::push_s(@pieces, '[');
418-
my $first := 1;
419-
for $obj {
420-
if $first {
421-
$first := 0;
422-
}
423-
else {
424-
nqp::push_s(@pieces, ',');
425-
}
426-
to_json($_);
427-
}
428-
nqp::push_s(@pieces, ']');
429-
}
430-
elsif nqp::ishash($obj) {
431-
nqp::push_s(@pieces, '{');
432-
my $first := 1;
433-
for sorted_keys($obj) {
434-
if $first {
435-
$first := 0;
436-
}
437-
else {
438-
nqp::push_s(@pieces, ',');
439-
}
440-
nqp::push_s(@pieces, '"');
441-
nqp::push_s(@pieces, $_);
442-
nqp::push_s(@pieces, '":');
443-
to_json($obj{$_});
444-
}
445-
nqp::push_s(@pieces, '}');
446-
}
447-
elsif nqp::isstr($obj) {
448-
if nqp::index($obj, '\\') {
449-
$obj := literal_subst($obj, '\\', $escaped_backslash);
450-
}
451-
if nqp::index($obj, '"') {
452-
$obj := literal_subst($obj, '"', $escaped_dquote);
453-
}
454-
nqp::push_s(@pieces, '"');
455-
nqp::push_s(@pieces, $obj);
456-
nqp::push_s(@pieces, '"');
457-
}
458-
elsif nqp::isint($obj) || nqp::isnum($obj) {
459-
nqp::push_s(@pieces, ~$obj);
460-
}
461-
elsif nqp::can($obj, 'Str') {
462-
to_json(nqp::unbox_s($obj.Str));
463-
}
464-
else {
465-
nqp::die("Don't know how to dump a " ~ $obj.HOW.name($obj));
466-
}
467-
if nqp::elems(@pieces) > 4096 {
468-
$hs_fh.print(nqp::join('', @pieces));
469-
nqp::setelems(@pieces, 0);
470-
}
471-
}
472-
to_json($obj);
473-
$hs_fh.print(nqp::join('', @pieces));
474-
}
475-
476-
$hs_fh.print('strings: ');
477-
write_json($data<strings>);
478-
$hs_fh.print("\ntypes: ");
479-
$hs_fh.print($data<types>);
480-
$hs_fh.print("\nstatic_frames: ");
481-
$hs_fh.print($data<static_frames>);
482-
$hs_fh.print("\n\n");
483-
484-
my int $i := 0;
485-
for $data<snapshots> {
486-
$hs_fh.print("snapshot $i\n");
487-
$hs_fh.print("collectables: " ~ $_<collectables> ~ "\n");
488-
$hs_fh.print("references: " ~ $_<references> ~ "\n");
489-
$hs_fh.print("\n");
490-
$i++;
491-
}
492-
493-
$hs_fh.close;
414+
note("Heap snapshot written to $filename");
494415
}
495416

496417
method run_traced($level, $what) {

0 commit comments

Comments
 (0)