Skip to content

Commit

Permalink
trace: when profiling, write sampling trace to put
Browse files Browse the repository at this point in the history
Whenever we run under the profiling flag, we now collect sampling traces
into .urb/put/trace/dump.txt.

We take care to format the output in a way that's compatible with
flamegraph tooling. Specifically, we avoid writing into the first couple
lines of the file, and suffix every trace with a counter (always 1).

(See also brendangregg/FlameGraph.)

We have not yet cleaned up old code in u3t_samp.

Co-authored-by: joemfb <[email protected]>
  • Loading branch information
Fang- and joemfb committed Jul 9, 2024
1 parent 48b73dc commit 7d13f5d
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pkg/noun/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ static c3_o _ct_lop_o;
/// Nock PID.
static pid_t _nock_pid_i = 0;

/// dump trace file.
static FILE* _filt_u = NULL;

/// JSON trace file.
static FILE* _file_u = NULL;

Expand Down Expand Up @@ -209,6 +212,28 @@ u3t_samp(void)
return;
}

u3_noun don = u3R->pro.don;
while ( u3_nul != don ) {
u3_noun dot = u3h(don);

while ( u3_nul != dot ) {
u3_noun dit = u3h(dot);
c3_w len_w = u3r_met(3, dit);
c3_c* dit_c = ( c3y == u3a_is_cat(dit) )
? &dit
: ((u3a_atom*)u3a_to_ptr(dit))->buf_w;
fprintf(_filt_u, "/%.*s", len_w, dit_c);

dot = u3t(dot);
}
fprintf(_filt_u, "\n");

don = u3t(don);
}
fprintf(_filt_u, "1\n\n");

return;

c3_w old_wag = u3C.wag_w;
u3C.wag_w &= ~u3o_debug_cpu;
u3C.wag_w &= ~u3o_trace;
Expand Down Expand Up @@ -547,6 +572,24 @@ u3t_init(void)
u3T.far_o = c3n;
u3T.coy_o = c3n;
u3T.euq_o = c3n;

if ( ( u3C.wag_w & u3o_debug_cpu ) && !_filt_u ) {
c3_c fil_c[2048];
snprintf(fil_c, 2048, "%s/.urb/put/trace", u3C.dir_c);

struct stat st;
if ( (-1 == stat(fil_c, &st))
&& (-1 == c3_mkdir(fil_c, 0700)) )
{
fprintf(stderr, "mkdir: %s failed: %s\r\n", fil_c, strerror(errno));
return;
}

snprintf(fil_c, 2048, "%s/.urb/put/trace/dump.txt", u3C.dir_c);

_filt_u = c3_fopen(fil_c, "w");
fprintf(_filt_u, "\n\n\n");
}
}

c3_w
Expand Down

0 comments on commit 7d13f5d

Please sign in to comment.