1 #include "cado.h" // IWYU pragma: keep
2 #include <math.h>       // INFINITY
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <inttypes.h>
6 #include "stats.h"
7 #include "timing.h"     // wct_seconds
8 
9 void
stats_init(stats_data_t r,FILE * f,uint64_t * followed_var,uint8_t max_log_report,const char * verb,const char * name,const char * outofname,const char * abbrv)10 stats_init (stats_data_t r, FILE *f, uint64_t *followed_var,
11             uint8_t max_log_report, const char *verb, const char *name,
12             const char *outofname, const char *abbrv)
13 {
14   r->followed_var = followed_var;
15   r->last_report = 0;
16   r->t0 = wct_seconds();
17   r->out = f;
18   r->log_report = MIN_LOG_REPORT;
19   if (max_log_report >= MIN_LOG_REPORT)
20     r->max_log_report = max_log_report;
21   else
22     r->max_log_report = MIN_LOG_REPORT;
23   r->name = name;
24   r->abbrv = abbrv;
25   r->verb = verb;
26   r->outofname = outofname;
27 }
28 
29 /* Return 1 if more than 2^r->log_report relations were read since last
30    progress report.
31    Otherwise return 0 */
32 int
stats_test_progress(stats_data_t r)33 stats_test_progress (stats_data_t r)
34 {
35   if ((*(r->followed_var) >> r->log_report) != r->last_report)
36     return 1;
37   else
38     return 0;
39 }
40 
41 /* Print a line of the form:
42  *    Read 42 relations in 1.4s -- 30.0 rels/s
43  * Prepend a "Done: " if end is non-zero.
44  * Add "-- xy.z MB/s " in the middle, if nByte > 0, with xy.z being
45  * nByte/time spent.
46  * Add "(out of <outof> ssss) " in the middle, if outof > 0, with ssss being
47  * r->outofname.
48  */
49 void
stats_print_progress(stats_data_t r,uint64_t i,uint64_t outof,size_t nByte,int end)50 stats_print_progress (stats_data_t r, uint64_t i, uint64_t outof, size_t nByte,
51                       int end)
52 {
53   char MBpart[32] = "";
54   char outofpart[64] = "";
55   double t, dt, speed;
56   t = wct_seconds();
57   dt = t - r->t0;
58   speed = dt > 0.01 ? i/dt : INFINITY;
59   if (nByte > 0)
60   {
61     double mb_s = dt > 0.01 ? (nByte/dt * 1.0e-6) : INFINITY;
62     snprintf (MBpart, 32, "-- %.1f MB/s ", mb_s);
63   }
64   if (outof > 0)
65     snprintf (outofpart, 64, "(out of %" PRIu64 " %s) ", outof, r->outofname);
66   const char * prefix = (end) ? "# Done: " : "# ";
67   fprintf(r->out, "%s%s %" PRIu64 " %s %sin %.1fs %s-- %.1f %s/s\n",
68           prefix, r->verb, i, r->name, outofpart, dt, MBpart, speed, r->abbrv);
69   fflush(r->out);
70   if (r->log_report < r->max_log_report)
71     r->log_report++;
72   r->last_report = (*(r->followed_var) >> r->log_report);
73 }
74