Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unify memory printing functions #311

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions pkg/c3/util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "util.h"

c3_w
_c3_printcap_mem_w(FILE *fil_u, c3_w wor_w, const c3_c *cap_c)
{
return _c3_printcap_mem_z(fil_u, (c3_z)wor_w << 2, cap_c) >> 2;
}

c3_z
_c3_printcap_mem_z(FILE *fil_u, c3_z byt_z, const c3_c *cap_c)
{
c3_assert( 0 != fil_u ); /* ;;: I assume this is important from commit
f975ca908b143fb76c104ecc32cb59317ea5b198:
threads output file pointer through memory
marking and printing.

If not necessary, we can get rid of
c3_maid_w */
Comment on lines +9 to +18
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface is even simpler if we can get rid of the null file assert in _c3_printcap_mem_z. I think we might be able to, but not sure. See inline comment.

If we can get rid of this, all calls to c3_maid_w can just call c3_print_mem_w.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the question is... when do we want a call to c3_print_mem_w to crash the process when the output file is null (rather than ignore the print). I would guess never


c3_z gib_z = (byt_z / (1 << 30));
c3_z mib_z = (byt_z % (1 << 30)) / (1 << 20);
c3_z kib_z = (byt_z % (1 << 20)) / (1 << 10);
c3_z bib_z = (byt_z % (1 << 10));

if ( gib_z ) {
fprintf(fil_u, "%s" "GiB/%" PRIc3_z ".%03" PRIc3_z ".%03" PRIc3_z ".%03" PRIc3_z "\r\n",
cap_c, gib_z, mib_z, kib_z, bib_z);
}
else if ( mib_z ) {
fprintf(fil_u, "%s" "MiB/%" PRIc3_z ".%03" PRIc3_z ".%03" PRIc3_z "\r\n",
cap_c, mib_z, kib_z, bib_z);
}
else if ( kib_z ) {
fprintf(fil_u, "%s" "KiB/%" PRIc3_z ".%03" PRIc3_z "\r\n",
cap_c, kib_z, bib_z);
}
else {
fprintf(fil_u, "%s" "B/%" PRIc3_z "\r\n",
cap_c, bib_z);
}
return byt_z;
}
82 changes: 82 additions & 0 deletions pkg/c3/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#ifndef C3_UTIL_H
#define C3_UTIL_H

#include <stdio.h>

#include "types.h"
#include "defs.h"

c3_w _c3_printcap_mem_w (FILE *fil_u, c3_w wor_w, const c3_c *cap_c);
c3_z _c3_printcap_mem_z(FILE *fil_u, c3_z byt_z, const c3_c *cap_c);


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some Form Feed (U+000C) characters interspersed here and in other places. We should get rid of them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Form feed characters are good and make code easier to navigate

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, I just haven't seen them elsewhere in our codebase. Feel free to disregard.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They certainly might not be anywhere else, but I don't think they hurt.

Copy link
Member Author

@barter-simsum barter-simsum Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and fyi, I mainly put them here to bracket the pairs of macros which function more as single logical units. I also don't mind their usage to page code into logical sections with header comments

/*
c3_print_mem_w(
FILE * FILE - file. must not be NULL,
c3_w WOR - size in words,
? c3_c * q_CAP - caption,
)

Print word-sized quantity, WOR, in typical format:
[Caption: ][GiB|MiB|KiB|B]/QUANTITY
*/
#define c3_print_mem_w( ... ) \
c3_print_mem_w_0( __VA_ARGS__, "" )
#define c3_print_mem_w_0( FILE, WOR, q_CAP, ... ) \
((q_CAP[0]) \
? _c3_printcap_mem_w((FILE), (WOR), q_CAP ": ") \
: _c3_printcap_mem_w((FILE), (WOR), ""))


/*
c3_print_mem_z(
FILE * FILE - file. must not be NULL,
c3_w BYT - size in bytes,
? c3_c * q_CAP - caption,
)

Print byte-sized quantity, BYT like c3_print_mem_w
*/
#define c3_print_mem_z( ... ) \
c3_print_mem_z_0( __VA_ARGS__, "" )
#define c3_print_mem_z_0( FILE, BYT, q_CAP, ... ) \
((q_CAP[0]) \
? _c3_printcap_mem_z((FILE), (BYT), q_CAP ": ") \
: _c3_printcap_mem_z((FILE), (BYT), ""))


/*
c3_print_mem_w(
FILE * FILE - file may be NULL,
c3_w WOR - size in words,
? c3_c * q_CAP - caption,
)

Just like c3_print_mem_w with the exception that FILE may be null. In which
case, this is a NOP.
*/
#define c3_maid_w( ... ) \
c3_maid_w_0( __VA_ARGS__, "" )
#define c3_maid_w_0( FILE, WOR, ... ) \
((0 == (FILE)) \
? (WOR) \
: c3_print_mem_w( FILE, WOR, __VA_ARGS__ ))


/*
c3_print_memdiff_w(
FILE * FILE - file,
c3_w WOR0 - size in words,
c3_w WOR1 - size in words,
? c3_c * q_CAP - caption,
)

Prints absolute value of WOR0 - WOR1 using c3_print_mem_w
*/
#define c3_print_memdiff_w( ... ) \
c3_print_memdiff_w_0( __VA_ARGS__, "" )

#define c3_print_memdiff_w_0( FILE, WOR0, WOR1, ... ) \
c3_print_mem_w(FILE, ((0+WOR0) > (0+WOR1) ? WOR0 - WOR1 : WOR1 - WOR0), __VA_ARGS__ )

#endif /* ifndef C3_UTIL_H */
83 changes: 20 additions & 63 deletions pkg/noun/allocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "options.h"
#include "retrieve.h"
#include "trace.h"
#include "util.h"
#include "vortex.h"

u3_road* u3a_Road;
Expand Down Expand Up @@ -1980,64 +1981,20 @@ u3a_print_time(c3_c* str_c, c3_c* cap_c, c3_d mic_d)
}
}

/* u3a_print_memory: print memory amount.
*/
void
u3a_print_memory(FILE* fil_u, c3_c* cap_c, c3_w wor_w)
{
c3_assert( 0 != fil_u );

c3_z byt_z = ((c3_z)wor_w * 4);
c3_z gib_z = (byt_z / 1000000000);
c3_z mib_z = (byt_z % 1000000000) / 1000000;
c3_z kib_z = (byt_z % 1000000) / 1000;
c3_z bib_z = (byt_z % 1000);

if ( byt_z ) {
if ( gib_z ) {
fprintf(fil_u, "%s: GB/%" PRIc3_z ".%03" PRIc3_z ".%03" PRIc3_z ".%03" PRIc3_z "\r\n",
cap_c, gib_z, mib_z, kib_z, bib_z);
}
else if ( mib_z ) {
fprintf(fil_u, "%s: MB/%" PRIc3_z ".%03" PRIc3_z ".%03" PRIc3_z "\r\n",
cap_c, mib_z, kib_z, bib_z);
}
else if ( kib_z ) {
fprintf(fil_u, "%s: KB/%" PRIc3_z ".%03" PRIc3_z "\r\n",
cap_c, kib_z, bib_z);
}
else if ( bib_z ) {
fprintf(fil_u, "%s: B/%" PRIc3_z "\r\n",
cap_c, bib_z);
}
}
}

/* u3a_maid(): maybe print memory.
*/
c3_w
u3a_maid(FILE* fil_u, c3_c* cap_c, c3_w wor_w)
{
if ( 0 != fil_u ) {
u3a_print_memory(fil_u, cap_c, wor_w);
}
return wor_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, " memoization cache", u3h_mark(u3R->cax.har_p));
return u3a_maid(fil_u, "total road stuff", tot_w);
tot_w += c3_maid_w(fil_u, u3a_mark_noun(u3R->ski.gul), " namespace");
tot_w += c3_maid_w(fil_u, u3a_mark_noun(u3R->bug.tax), " trace stack");
tot_w += c3_maid_w(fil_u, u3a_mark_noun(u3R->bug.mer), " trace buffer");
tot_w += c3_maid_w(fil_u, u3a_mark_noun(u3R->pro.don), " profile batteries");
tot_w += c3_maid_w(fil_u, u3a_mark_noun(u3R->pro.day), " profile doss");
tot_w += c3_maid_w(fil_u, u3a_mark_noun(u3R->pro.trace), " new profile trace");
tot_w += c3_maid_w(fil_u, u3h_mark(u3R->cax.har_p), " memoization cache");
return c3_maid_w(fil_u, tot_w, "total road stuff");
}

/* u3a_reclaim(): clear ad-hoc persistent caches to reclaim memory.
Expand Down Expand Up @@ -2149,7 +2106,7 @@ _ca_print_leak(c3_c* cap_c, u3a_box* box_u, c3_w eus_w, c3_w use_w)
c3_free(cod_c);
}

u3a_print_memory(stderr, " size", box_u->siz_w);
c3_print_mem_w(stderr, box_u->siz_w, " size");

{
c3_c* dat_c = _ca_print_box(box_u);
Expand All @@ -2169,7 +2126,7 @@ _ca_print_leak(c3_c* cap_c, u3a_box* box_u, c3_ws use_ws)
((u3a_noun *)(u3a_boxto(box_u)))->mug_w,
use_ws);

u3a_print_memory(stderr, " size", box_u->siz_w);
c3_print_mem_w(stderr, box_u->siz_w, " size");

{
c3_c* dat_c = _ca_print_box(box_u);
Expand Down Expand Up @@ -2299,24 +2256,24 @@ u3a_sweep(void)

#ifdef U3_CPU_DEBUG
if ( (0 != u3R->par_p) && (u3R->all.max_w > 1000000) ) {
u3a_print_memory(stderr, "available", (tot_w - pos_w));
u3a_print_memory(stderr, "allocated", pos_w);
u3a_print_memory(stderr, "volatile", caf_w);
c3_print_mem_w(stderr, (tot_w - pos_w), "available");
c3_print_mem_w(stderr, pos_w, "allocated");
c3_print_mem_w(stderr, caf_w, "volatile");

u3a_print_memory(stderr, "maximum", u3R->all.max_w);
c3_print_mem_w(stderr, u3R->all.max_w, "maximum");
}
#endif

#if 0
u3a_print_memory(stderr, "available", (tot_w - pos_w));
u3a_print_memory(stderr, "allocated", pos_w);
u3a_print_memory(stderr, "volatile", caf_w);
c3_print_mem_w(stderr, (tot_w - pos_w), "available");
c3_print_mem_w(stderr, pos_w, "allocated");
c3_print_mem_w(stderr, caf_w, "volatile");
#endif
}
#endif

u3a_print_memory(stderr, "leaked", leq_w);
u3a_print_memory(stderr, "weaked", weq_w);
c3_print_mem_w(stderr, leq_w, "leaked");
c3_print_mem_w(stderr, weq_w, "weaked");

c3_assert( (pos_w + leq_w + weq_w) == neg_w );
c3_assert( (0 == leq_w) && (0 == weq_w) );
Expand Down
10 changes: 0 additions & 10 deletions pkg/noun/allocate.h
Original file line number Diff line number Diff line change
Expand Up @@ -717,16 +717,6 @@
void
u3a_print_time(c3_c* str_c, c3_c* cap_c, c3_d mic_d);

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

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

/* u3a_deadbeef(): write 0xdeadbeef from hat to cap.
*/
void
Expand Down
6 changes: 3 additions & 3 deletions pkg/noun/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#include "options.h"
#include "retrieve.h"
#include "types.h"
#include "util.h"

/// Snapshotting system.
u3e_pool u3e_Pool;
Expand Down Expand Up @@ -1081,7 +1082,7 @@ u3e_save(void)
/* attempt to avoid propagating anything insane to disk */
u3a_loom_sane();

// u3a_print_memory(stderr, "sync: save", 4096 * pat_u->con_u->pgs_w);
// c3_print_mem_w(stderr, 4096 * pat_u->con_u->pgs_w, "sync: save");

_ce_patch_sync(pat_u);

Expand Down Expand Up @@ -1197,8 +1198,7 @@ u3e_live(c3_o nuu_o, c3_c* dir_c)
nuu_o = c3y;
}
else {
u3a_print_memory(stderr, "live: loaded",
(u3P.nor_u.pgs_w + u3P.sou_u.pgs_w) << u3a_page);
c3_print_mem_w(stderr, (u3P.nor_u.pgs_w + u3P.sou_u.pgs_w) << u3a_page, "sync: save");
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/noun/jets.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "retrieve.h"
#include "serial.h"
#include "trace.h"
#include "util.h"
#include "urcrypt/urcrypt.h"
#include "vortex.h"
#include "xtract.h"
Expand Down Expand Up @@ -2319,22 +2320,22 @@ u3j_mark(FILE* fil_u)
{
c3_w tot_w = 0;

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));
tot_w += c3_maid_w(fil_u, u3h_mark(u3R->jed.war_p), " warm jet state");
tot_w += c3_maid_w(fil_u, u3h_mark(u3R->jed.cod_p), " cold jet state");
tot_w += c3_maid_w(fil_u, u3h_mark(u3R->jed.han_p), " hank cache");
tot_w += c3_maid_w(fil_u, u3h_mark(u3R->jed.bas_p), " battery hash cache");

{
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);
tot_w += c3_maid_w(fil_u, han_w, " call site cache");
}

if ( u3R == &(u3H->rod_u) ) {
tot_w += u3a_maid(fil_u, " hot jet state", u3h_mark(u3R->jed.hot_p));
tot_w += c3_maid_w(fil_u, u3h_mark(u3R->jed.hot_p), " hot jet state");
}

return u3a_maid(fil_u, "total jet stuff", tot_w);
return c3_maid_w(fil_u, tot_w, "total jet stuff");
}

/* _cj_free_hank(): free an entry from the hank cache.
Expand Down
8 changes: 4 additions & 4 deletions pkg/noun/jets/e/jam.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ u3qe_jam(u3_atom a)
u3l_log("item: B/0");
}
else {
u3a_print_memory(stderr, "item", siz_w);
c3_print_mem_w(stderr, siz_w, "item");
}
}
if ( u3_blip != som ) {
Expand All @@ -31,11 +31,11 @@ u3qe_jam(u3_atom a)
for ( som = u3t(a); c3y == u3du(som); som = u3t(som) ) u3a_discount_noun(u3h(som));
u3h_discount(u3R->cax.har_p);

u3a_print_memory(stderr, "total", tot_w);
u3a_print_memory(stderr, "memoization cache", mem_w);
c3_print_mem_w(stderr, tot_w, "total");
c3_print_mem_w(stderr, mem_w, "memoization cache");
u3h_root* har_u = u3to(u3h_root, u3R->cax.har_p);
u3l_log("memoization entries: %d", har_u->use_w);
u3a_print_memory(stderr, "unused free", u3a_open(u3R));
c3_print_mem_w(stderr, u3a_open(u3R), "unused free");
return tot_w;
}
#endif
Expand Down
Loading