Skip to content

Commit

Permalink
mass: return memory report to arvo (#675)
Browse files Browse the repository at this point in the history
Dill can give Vere a `[%quac ~]` gift and vere returns a memory report
in a `[%quac p=(list quac)]` where a quac is a `[name=@t size=@ud
quacs=(list quac)]` nested memory report structure. Vere itself does not
print the memory report in this case, but it still does when requested
with the old `|mass`/`%heft` and `grab` from the command-line. Arvo will
implement its own memory report printer for quacs.

Unlike the previous version that used a fake driver to inject the
report, this does it properly through Dill's driver, using a `%quiz`
plea/writ to query the serf for this info.
  • Loading branch information
pkova authored Aug 15, 2024
2 parents 2c33ef6 + cccfcd2 commit db15130
Show file tree
Hide file tree
Showing 17 changed files with 541 additions and 152 deletions.
187 changes: 142 additions & 45 deletions pkg/noun/allocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2005,9 +2005,8 @@ u3a_maid(FILE* fil_u, c3_c* cap_c, c3_w wor_w)
/* _ca_print_memory(): un-captioned u3a_print_memory().
*/
static void
_ca_print_memory(FILE* fil_u, c3_w wor_w)
_ca_print_memory(FILE* fil_u, c3_w byt_w)
{
c3_w byt_w = (wor_w * 4);
c3_w gib_w = (byt_w / 1000000000);
c3_w mib_w = (byt_w % 1000000000) / 1000000;
c3_w kib_w = (byt_w % 1000000) / 1000;
Expand All @@ -2028,43 +2027,55 @@ _ca_print_memory(FILE* fil_u, c3_w wor_w)
}
}

/* u3a_quac_free: free quac memory.
*/
void
u3a_quac_free(u3m_quac* qua_u)
{
c3_w i_w = 0;
while ( qua_u->qua_u[i_w] != NULL ) {
u3a_quac_free(qua_u->qua_u[i_w]);
i_w++;
}
c3_free(qua_u->nam_c);
c3_free(qua_u->qua_u);
c3_free(qua_u);
}

/* u3a_prof(): mark/measure/print memory profile. RETAIN.
*/
c3_w
u3a_prof(FILE* fil_u, c3_w den_w, u3_noun mas)
u3m_quac*
u3a_prof(FILE* fil_u, u3_noun mas)
{
c3_w tot_w = 0;
u3m_quac* pro_u = c3_calloc(sizeof(*pro_u));
u3_noun h_mas, t_mas;

if ( c3n == u3r_cell(mas, &h_mas, &t_mas) ) {
fprintf(fil_u, "%.*smistyped mass\r\n", den_w, "");
return tot_w;
fprintf(fil_u, "mistyped mass\r\n");
c3_free(pro_u);
return NULL;
}
else if ( _(u3du(h_mas)) ) {
fprintf(fil_u, "%.*smistyped mass head\r\n", den_w, "");
else if ( c3y == u3du(h_mas) ) {
fprintf(fil_u, "mistyped mass head\r\n");
{
c3_c* lab_c = u3m_pretty(h_mas);
fprintf(fil_u, "h_mas: %s", lab_c);
c3_free(lab_c);
}
return tot_w;
c3_free(pro_u);
return NULL;
}
else {
{
c3_c* lab_c = u3m_pretty(h_mas);
fprintf(fil_u, "%*s%s: ", den_w, "", lab_c);
c3_free(lab_c);
}

u3_noun it_mas, tt_mas;

if ( c3n == u3r_cell(t_mas, &it_mas, &tt_mas) ) {
fprintf(fil_u, "%*smistyped mass tail\r\n", den_w, "");
return tot_w;
fprintf(fil_u, "mistyped mass tail\r\n");
c3_free(pro_u);
return NULL;
}
else if ( c3y == it_mas ) {
tot_w += u3a_mark_noun(tt_mas);
_ca_print_memory(fil_u, tot_w);
c3_w siz_w = u3a_mark_noun(tt_mas);

#if 1
/* The basic issue here is that tt_mas is included in .sac
Expand All @@ -2075,7 +2086,7 @@ u3a_prof(FILE* fil_u, c3_w den_w, u3_noun mas)
*
* see u3a_mark_ptr().
*/
if ( _(u3a_is_dog(tt_mas)) ) {
if ( c3y == u3a_is_dog(tt_mas) ) {
u3a_box* box_u = u3a_botox(u3a_to_ptr(tt_mas));
#ifdef U3_MEMORY_DEBUG
if ( 1 == box_u->eus_w ) {
Expand All @@ -2094,45 +2105,131 @@ u3a_prof(FILE* fil_u, c3_w den_w, u3_noun mas)
#endif
}
#endif
pro_u->nam_c = u3r_string(h_mas);
pro_u->siz_w = siz_w*4;
pro_u->qua_u = NULL;
return pro_u;

return tot_w;
}
else if ( c3n == it_mas ) {
fprintf(fil_u, "\r\n");

while ( _(u3du(tt_mas)) ) {
tot_w += u3a_prof(fil_u, den_w+2, u3h(tt_mas));
pro_u->qua_u = c3_malloc(sizeof(pro_u->qua_u));
c3_w i_w = 0;
c3_t bad_t = 0;
while ( c3y == u3du(tt_mas) ) {
u3m_quac* new_u = u3a_prof(fil_u, u3h(tt_mas));
if ( NULL == new_u ) {
bad_t = 1;
} else {
pro_u->qua_u = c3_realloc(pro_u->qua_u, (i_w + 2) * sizeof(pro_u->qua_u));
pro_u->siz_w += new_u->siz_w;
pro_u->qua_u[i_w] = new_u;
}
tt_mas = u3t(tt_mas);
i_w++;
}
pro_u->qua_u[i_w] = NULL;

fprintf(fil_u, "%*s--", den_w, "");
_ca_print_memory(fil_u, tot_w);

return tot_w;

if ( bad_t ) {
i_w = 0;
while ( pro_u->qua_u[i_w] != NULL ) {
u3a_quac_free(pro_u->qua_u[i_w]);
i_w++;
}
c3_free(pro_u->qua_u);
c3_free(pro_u);
return NULL;
} else {
pro_u->nam_c = u3r_string(h_mas);
return pro_u;
}
}
else {
fprintf(fil_u, "%*smistyped (strange) mass tail\r\n", den_w, "");
return tot_w;
fprintf(fil_u, "mistyped (strange) mass tail\r\n");
c3_free(pro_u);
return NULL;
}
}
}


/* u3a_print_quac: print a memory report.
*/

void
u3a_print_quac(FILE* fil_u, c3_w den_w, u3m_quac* mas_u)
{
u3_assert( 0 != fil_u );

if ( mas_u->siz_w ) {
fprintf(fil_u, "%*s%s: ", den_w, "", mas_u->nam_c);

if ( mas_u->qua_u == NULL ) {
_ca_print_memory(fil_u, mas_u->siz_w);
} else {
fprintf(fil_u, "\r\n");
c3_w i_w = 0;
while ( mas_u->qua_u[i_w] != NULL ) {
u3a_print_quac(fil_u, den_w+2, mas_u->qua_u[i_w]);
i_w++;
}
fprintf(fil_u, "%*s--", den_w, "");
_ca_print_memory(fil_u, mas_u->siz_w);
}
}
}

/* u3a_mark_road(): mark ad-hoc persistent road structures.
*/
c3_w
u3a_mark_road(FILE* fil_u)
{
c3_w tot_w = 0;
tot_w += u3a_maid(fil_u, " namespace", u3a_mark_noun(u3R->ski.gul));
tot_w += u3a_maid(fil_u, " trace stack", u3a_mark_noun(u3R->bug.tax));
tot_w += u3a_maid(fil_u, " trace buffer", u3a_mark_noun(u3R->bug.mer));
tot_w += u3a_maid(fil_u, " profile batteries", u3a_mark_noun(u3R->pro.don));
tot_w += u3a_maid(fil_u, " profile doss", u3a_mark_noun(u3R->pro.day));
tot_w += u3a_maid(fil_u, " new profile trace", u3a_mark_noun(u3R->pro.trace));
tot_w += u3a_maid(fil_u, " transient memoization cache", u3h_mark(u3R->cax.har_p));
tot_w += u3a_maid(fil_u, " persistent memoization cache", u3h_mark(u3R->cax.per_p));
return u3a_maid(fil_u, "total road stuff", tot_w);
u3m_quac*
u3a_mark_road()
{
u3m_quac** qua_u = c3_malloc(sizeof(*qua_u) * 9);

qua_u[0] = c3_calloc(sizeof(*qua_u[0]));
qua_u[0]->nam_c = strdup("namespace");
qua_u[0]->siz_w = u3a_mark_noun(u3R->ski.gul) * 4;

qua_u[1] = c3_calloc(sizeof(*qua_u[1]));
qua_u[1]->nam_c = strdup("trace stack");
qua_u[1]->siz_w = u3a_mark_noun(u3R->bug.tax) * 4;

qua_u[2] = c3_calloc(sizeof(*qua_u[2]));
qua_u[2]->nam_c = strdup("trace buffer");
qua_u[2]->siz_w = u3a_mark_noun(u3R->bug.mer) * 4;

qua_u[3] = c3_calloc(sizeof(*qua_u[3]));
qua_u[3]->nam_c = strdup("profile batteries");
qua_u[3]->siz_w = u3a_mark_noun(u3R->pro.don) * 4;

qua_u[4] = c3_calloc(sizeof(*qua_u[4]));
qua_u[4]->nam_c = strdup("profile doss");
qua_u[4]->siz_w = u3a_mark_noun(u3R->pro.day) * 4;

qua_u[5] = c3_calloc(sizeof(*qua_u[5]));
qua_u[5]->nam_c = strdup("new profile trace");
qua_u[5]->siz_w = u3a_mark_noun(u3R->pro.trace) * 4;

qua_u[6] = c3_calloc(sizeof(*qua_u[6]));
qua_u[6]->nam_c = strdup("transient memoization cache");
qua_u[6]->siz_w = u3h_mark(u3R->cax.har_p) * 4;

qua_u[7] = c3_calloc(sizeof(*qua_u[7]));
qua_u[7]->nam_c = strdup("persistent memoization cache");
qua_u[7]->siz_w = u3h_mark(u3R->cax.per_p) * 4;

qua_u[8] = NULL;

c3_w sum_w = 0;
for (c3_w i_w = 0; i_w < 8; i_w++) {
sum_w += qua_u[i_w]->siz_w;
}

u3m_quac* tot_u = c3_malloc(sizeof(*tot_u));
tot_u->nam_c = strdup("total road stuff");
tot_u->siz_w = sum_w;
tot_u->qua_u = qua_u;

return tot_u;
}

/* u3a_reclaim(): clear ad-hoc persistent caches to reclaim memory.
Expand Down
24 changes: 19 additions & 5 deletions pkg/noun/allocate.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@

/* u3a_mark_road(): mark ad-hoc persistent road structures.
*/
c3_w
u3a_mark_road(FILE* fil_u);
u3m_quac*
u3a_mark_road();

/* u3a_reclaim(): clear ad-hoc persistent caches to reclaim memory.
*/
Expand Down Expand Up @@ -702,21 +702,35 @@
void
u3a_print_time(c3_c* str_c, c3_c* cap_c, c3_d mic_d);

/* u3a_print_quac: print a quac memory report.
*/
void
u3a_print_quac(FILE* fil_u, c3_w den_w, u3m_quac* mas_u);

/* u3a_print_memory(): print memory amount.
*/
void
u3a_print_memory(FILE* fil_u, c3_c* cap_c, c3_w wor_w);

/* u3a_prof(): mark/measure/print memory profile. RETAIN.
*/
c3_w
u3a_prof(FILE* fil_u, c3_w den_w, u3_noun mas);
u3m_quac*
u3a_prof(FILE* fil_u, u3_noun mas);

/* u3a_maid(): maybe print memory.
*/
c3_w
u3a_maid(FILE* fil_u, c3_c* cap_c, c3_w wor_w);

/* u3a_quac_free(): free quac memory.
*/
void
u3a_quac_free(u3m_quac* qua_u);

/* u3a_uncap_print_memory(): un-captioned print memory amount.
*/
void
u3a_uncap_print_memory(FILE* fil_u, c3_w byt_w);

/* u3a_deadbeef(): write 0xdeadbeef from hat to cap.
*/
void
Expand Down
62 changes: 48 additions & 14 deletions pkg/noun/jets.c
Original file line number Diff line number Diff line change
Expand Up @@ -2305,27 +2305,61 @@ _cj_mark_hank(u3_noun kev, void* dat)

/* u3j_mark(): mark jet state for gc.
*/
c3_w
u3j_mark(FILE* fil_u)
u3m_quac*
u3j_mark()
{
c3_w tot_w = 0;
u3m_quac** qua_u = c3_malloc(sizeof(*qua_u) * 7);

tot_w += u3a_maid(fil_u, " warm jet state", u3h_mark(u3R->jed.war_p));
tot_w += u3a_maid(fil_u, " cold jet state", u3h_mark(u3R->jed.cod_p));
tot_w += u3a_maid(fil_u, " hank cache", u3h_mark(u3R->jed.han_p));
tot_w += u3a_maid(fil_u, " battery hash cache", u3h_mark(u3R->jed.bas_p));
qua_u[0] = c3_calloc(sizeof(*qua_u[0]));
qua_u[0]->nam_c = strdup("warm jet state");
qua_u[0]->siz_w = u3h_mark(u3R->jed.war_p) * 4;

{
c3_w han_w = 0;
u3h_walk_with(u3R->jed.han_p, _cj_mark_hank, &han_w);
tot_w += u3a_maid(fil_u, " call site cache", han_w);
qua_u[1] = c3_calloc(sizeof(*qua_u[1]));
qua_u[1]->nam_c = strdup("cold jet state");
qua_u[1]->siz_w = u3h_mark(u3R->jed.cod_p) * 4;

qua_u[2] = c3_calloc(sizeof(*qua_u[2]));
qua_u[2]->nam_c = strdup("hank cache");
qua_u[2]->siz_w = u3h_mark(u3R->jed.han_p) * 4;

qua_u[3] = c3_calloc(sizeof(*qua_u[3]));
qua_u[3]->nam_c = strdup("battery hash cache");
qua_u[3]->siz_w = u3h_mark(u3R->jed.bas_p) * 4;

qua_u[4] = c3_calloc(sizeof(*qua_u[4]));
qua_u[4]->nam_c = strdup("call site cache");
u3h_walk_with(u3R->jed.han_p, _cj_mark_hank, &qua_u[4]->siz_w);
qua_u[4]->siz_w *= 4;

c3_w sum_w = 0;
for ( c3_w i_w = 0; i_w < 5; i_w++ ) {
sum_w += qua_u[i_w]->siz_w;
}

u3m_quac* tot_u = c3_calloc(sizeof(*tot_u));
tot_u->nam_c = strdup("total jet stuff");

if ( u3R == &(u3H->rod_u) ) {
tot_w += u3a_maid(fil_u, " hot jet state", u3h_mark(u3R->jed.hot_p));
}
qua_u[5] = c3_calloc(sizeof(*qua_u[5]));
qua_u[5]->nam_c = strdup("hot jet state");
qua_u[5]->siz_w = u3h_mark(u3R->jed.hot_p) * 4;

sum_w += qua_u[5]->siz_w;

qua_u[6] = NULL;

tot_u->siz_w = sum_w;
tot_u->qua_u = qua_u;

return u3a_maid(fil_u, "total jet stuff", tot_w);
return tot_u;
} else {
qua_u[5] = NULL;

tot_u->siz_w = sum_w;
tot_u->qua_u = qua_u;

return tot_u;
}
}

/* u3j_free_hank(): free an entry from the hank cache.
Expand Down
4 changes: 2 additions & 2 deletions pkg/noun/jets.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@

/* u3j_mark(): mark jet state for gc.
*/
c3_w
u3j_mark(FILE* fil_u);
u3m_quac*
u3j_mark();

/* u3j_free(): free jet state.
*/
Expand Down
Loading

0 comments on commit db15130

Please sign in to comment.