about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2022-12-15 20:52:37 +0000
committerEric Wong <mwrap-perl@80x24.org>2022-12-16 09:27:37 +0000
commit956b57fbea889b9838f80f6f62df942bb5a8a76f (patch)
treea9c456aef126aaa8eb5475187aa77070446b4421
parent73249f748a7d11560850b87dc764824e31f149a3 (diff)
downloadmwrap-956b57fbea889b9838f80f6f62df942bb5a8a76f.tar.gz
We actually want to read total_bytes_dec before total_bytes_inc
to ensure a greater likelyhood of the resulting total being
positive (or not underflowed into SIZE_MAX territory)
-rw-r--r--mwrap_httpd.h15
1 files changed, 9 insertions, 6 deletions
diff --git a/mwrap_httpd.h b/mwrap_httpd.h
index bd37fb2..aa4574a 100644
--- a/mwrap_httpd.h
+++ b/mwrap_httpd.h
@@ -487,6 +487,13 @@ static struct h1_src_loc *accumulate(unsigned long min, size_t *hslc, FILE *lp)
         return hslv;
 }
 
+static void show_age(FILE *fp)
+{
+        size_t dec = uatomic_read(&total_bytes_dec);
+        size_t inc = uatomic_read(&total_bytes_inc);
+        fprintf(fp, "<p>Current age: %zu (live: %zu)", inc , inc - dec);
+}
+
 /* /$PID/at/$LOCATION endpoint */
 static enum mw_qev each_at(struct mw_h1 *h1, struct mw_h1req *h1r)
 {
@@ -519,9 +526,7 @@ static enum mw_qev each_at(struct mw_h1 *h1, struct mw_h1req *h1r)
         write_html(fp, lname.ptr, lname.len);
         free(lname.ptr);
 
-        size_t age = uatomic_read(&total_bytes_inc);
-        fprintf(fp, "<p>Current age: %zu (live: %zu)",
-                age, age - uatomic_read(&total_bytes_dec));
+        show_age(fp);
         FPUTS("<table><tr><th>size</th><th>generation</th>"
                 "<th>address</th></tr>", fp);
 
@@ -580,10 +585,8 @@ static enum mw_qev each_gt(struct mw_h1 *h1, struct mw_h1req *h1r,
         fprintf(fp, "<html><head><title>mwrap each &gt;%lu"
                 "</title></head><body><p>mwrap each &gt;%lu "
                 "(change `%lu' in URL to adjust filtering)", min, min, min);
-        size_t age = uatomic_read(&total_bytes_inc);
-        fprintf(fp, "<p>Current age: %zu (live: %zu)",
-                age, age - uatomic_read(&total_bytes_dec));
 
+        show_age(fp);
         if (bt_req_depth) /* need borders to distinguish multi-level traces */
                 FPUTS("<table\nborder=1><tr>", fp);
         else /* save screen space if only tracing one line */