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
|
/*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*/
#define _GNU_SOURCE
#ifndef HAVE_CONFIG_H
#error Invalid or missing autoconf build environment
#endif
#include "rng-tools-config.h"
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include "fips.h"
#include "stats.h"
static char stat_prefix[20] = "";
void set_stat_prefix(const char* prefix)
{
stat_prefix[sizeof(stat_prefix)-1] = 0;
strncpy(stat_prefix, prefix, sizeof(stat_prefix)-1);
}
static void scale_mult_unit(char *unit, int unitsize,
const char *baseunit,
double *value_min,
double *value_avg,
double *value_max)
{
int mult = 0;
char multchar[] = "KMGTPE";
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;
}
unit[unitsize-1] = 0;
if (mult)
snprintf(unit, unitsize, "%ci%s", multchar[mult-1], baseunit);
else
strncpy(unit, baseunit, unitsize);
}
/* Computes elapsed time in microseconds */
uint64_t elapsed_time(struct timeval *start,
struct timeval *stop)
{
uint64_t diff;
if (stop->tv_sec < start->tv_sec) return 0;
diff = (stop->tv_sec - start->tv_sec) * 1000000ULL;
if (stop->tv_usec > start->tv_usec) {
diff += stop->tv_usec - start->tv_usec;
} else {
diff -= start->tv_usec - stop->tv_usec;
}
return diff;
}
/* Updates min-max stat */
void update_stat(struct rng_stat *stat, uint64_t value)
{
uint64_t overflow = stat->num_samples;
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, int size,
const char *msg, uint64_t value)
{
buf[size-1] = 0;
snprintf(buf, size-1, "%s%s: %llu", stat_prefix, msg,
(unsigned long long) value);
return buf;
}
char *dump_stat_stat(char *buf, int size,
const char *msg, const char *unit, struct rng_stat *stat)
{
double avg = 0.0;
if (stat->num_samples > 0)
avg = (double)stat->sum / stat->num_samples;
buf[size-1] = 0;
snprintf(buf, size-1, "%s%s: (min=%llu; avg=%.3f; max=%llu)%s",
stat_prefix, msg, (unsigned long long) stat->min, avg,
(unsigned long long) stat->max, unit);
return buf;
}
char *dump_stat_bw(char *buf, int 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;
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;
}
|