1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
* stats.c -- Statistics helpers
*
* Copyright (C) 2004 Henrique de Moraes Holschuh <hmh@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _GNU_SOURCE
#ifndef HAVE_CONFIG_H
#error Invalid or missing autoconf build environment
#endif
#include "rng-tools-config.h"
/* For printf types macros (PRIu64) */
#define __STDC_FORMAT_MACROS
#include <unistd.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#include "fips.h"
#include "stats.h"
static char stat_prefix[20] = "";
void set_stat_prefix(const char* prefix)
{
if (prefix) {
strncpy(stat_prefix, prefix, sizeof(stat_prefix)-1);
stat_prefix[sizeof(stat_prefix)-1] = 0;
} else stat_prefix[0] = 0;
}
static void scale_mult_unit(char *unit, size_t unitsize,
const char *baseunit,
double *value_min,
double *value_avg,
double *value_max)
{
unsigned int mult = 0;
char multchar[] = "KMGTPE";
/* assert(unit != NULL && baseunit != NULL &&
value_min != NULL && value_avg != NULL && value_max != NULL); */
while ((*value_min >= 1024.0) && (*value_avg >= 1024.0) &&
(*value_max >= 1024.0) && (mult < sizeof(multchar))) {
mult++;
*value_min = *value_min / 1024.0;
*value_max = *value_max / 1024.0;
*value_avg = *value_avg / 1024.0;
}
if (mult)
snprintf(unit, unitsize, "%ci%s", multchar[mult-1], baseunit);
else
strncpy(unit, baseunit, unitsize);
unit[unitsize-1] = 0;
}
/* Updates min-max stat */
void update_stat(struct rng_stat *stat, uint64_t value)
{
uint64_t overflow = stat->num_samples;
assert(stat != NULL);
if ((stat->min == 0 ) || (value < stat->min)) stat->min = value;
if (value > stat->max) stat->max = value;
if (++stat->num_samples > overflow) {
stat->sum += value;
} else {
stat->sum = value;
stat->num_samples = 1;
}
}
char *dump_stat_counter(char *buf, size_t size,
const char *msg, uint64_t value)
{
assert(buf != NULL && msg != NULL);
snprintf(buf, size-1, "%s%s: %" PRIu64 , stat_prefix, msg, value);
buf[size-1] = 0;
return buf;
}
char *dump_stat_stat(char *buf, size_t size,
const char *msg, const char *unit, struct rng_stat *stat)
{
double avg = 0.0;
assert(stat != NULL && msg != NULL && unit != NULL && buf != NULL);
if (stat->num_samples > 0)
avg = (double)stat->sum / stat->num_samples;
buf[size-1] = 0;
snprintf(buf, size-1,
"%s%s: (min=%" PRIu64 "; avg=%.3f; max=%" PRIu64 ")%s",
stat_prefix, msg, stat->min, avg, stat->max, unit);
return buf;
}
char *dump_stat_bw(char *buf, size_t size,
const char *msg, const char *unit,
struct rng_stat *stat,
uint64_t blocksize)
{
char unitscaled[20];
double bw_avg = 0.0, bw_min = 0.0, bw_max = 0.0;
assert(stat != NULL && msg != NULL && unit != NULL);
if (stat->max > 0)
bw_min = (1000000.0 * blocksize) / stat->max;
if (stat->min > 0)
bw_max = (1000000.0 * blocksize) / stat->min;
if (stat->num_samples > 0)
bw_avg = (1000000.0 * blocksize * stat->num_samples) / stat->sum;
scale_mult_unit(unitscaled, sizeof(unitscaled), unit,
&bw_min, &bw_avg, &bw_max);
buf[size-1] = 0;
snprintf(buf, size-1, "%s%s: (min=%.3f; avg=%.3f; max=%.3f)%s/s",
stat_prefix, msg, bw_min, bw_avg, bw_max, unitscaled);
return buf;
}
|