]>
git.ipfire.org Git - thirdparty/rng-tools.git/blob - stats.c
2 * stats.c -- Statistics helpers
4 * Copyright (C) 2004 Henrique de Moraes Holschuh <hmh@debian.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #error Invalid or missing autoconf build environment
27 #include "rng-tools-config.h"
40 static char stat_prefix
[20] = "";
42 void set_stat_prefix(const char* prefix
)
44 stat_prefix
[sizeof(stat_prefix
)-1] = 0;
45 strncpy(stat_prefix
, prefix
, sizeof(stat_prefix
)-1);
48 static void scale_mult_unit(char *unit
, int unitsize
,
55 char multchar
[] = "KMGTPE";
57 while ((*value_min
>= 1024.0) && (*value_avg
>= 1024.0) &&
58 (*value_max
>= 1024.0) && (mult
< sizeof(multchar
))) {
60 *value_min
= *value_min
/ 1024.0;
61 *value_max
= *value_max
/ 1024.0;
62 *value_avg
= *value_avg
/ 1024.0;
66 snprintf(unit
, unitsize
, "%ci%s", multchar
[mult
-1], baseunit
);
68 strncpy(unit
, baseunit
, unitsize
);
71 /* Computes elapsed time in microseconds */
72 uint64_t elapsed_time(struct timeval
*start
,
77 if (stop
->tv_sec
< start
->tv_sec
) return 0;
79 diff
= (stop
->tv_sec
- start
->tv_sec
) * 1000000ULL;
80 if (stop
->tv_usec
> start
->tv_usec
) {
81 diff
+= stop
->tv_usec
- start
->tv_usec
;
83 diff
-= start
->tv_usec
- stop
->tv_usec
;
89 /* Updates min-max stat */
90 void update_stat(struct rng_stat
*stat
, uint64_t value
)
92 uint64_t overflow
= stat
->num_samples
;
94 if ((stat
->min
== 0 ) || (value
< stat
->min
)) stat
->min
= value
;
95 if (value
> stat
->max
) stat
->max
= value
;
96 if (++stat
->num_samples
> overflow
) {
100 stat
->num_samples
= 1;
104 char *dump_stat_counter(char *buf
, int size
,
105 const char *msg
, uint64_t value
)
108 snprintf(buf
, size
-1, "%s%s: %llu", stat_prefix
, msg
,
109 (unsigned long long) value
);
114 char *dump_stat_stat(char *buf
, int size
,
115 const char *msg
, const char *unit
, struct rng_stat
*stat
)
119 if (stat
->num_samples
> 0)
120 avg
= (double)stat
->sum
/ stat
->num_samples
;
123 snprintf(buf
, size
-1, "%s%s: (min=%llu; avg=%.3f; max=%llu)%s",
124 stat_prefix
, msg
, (unsigned long long) stat
->min
, avg
,
125 (unsigned long long) stat
->max
, unit
);
130 char *dump_stat_bw(char *buf
, int size
,
131 const char *msg
, const char *unit
,
132 struct rng_stat
*stat
,
136 double bw_avg
= 0.0, bw_min
= 0.0, bw_max
= 0.0;
139 bw_min
= (1000000.0 * blocksize
) / stat
->max
;
141 bw_max
= (1000000.0 * blocksize
) / stat
->min
;
142 if (stat
->num_samples
> 0)
143 bw_avg
= (1000000.0 * blocksize
* stat
->num_samples
) / stat
->sum
;
145 scale_mult_unit(unitscaled
, sizeof(unitscaled
), unit
,
146 &bw_min
, &bw_avg
, &bw_max
);
149 snprintf(buf
, size
-1, "%s%s: (min=%.3f; avg=%.3f; max=%.3f)%s/s",
150 stat_prefix
, msg
, bw_min
, bw_avg
, bw_max
, unitscaled
);