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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
// $Id: hist.cpp 91813 2010-09-17 07:52:52Z johnnyw $
/* Replacement for CSIM histogram created by Alex Carobus 7/20/98
#include <std/disclaimer.h>
*/
/*
//
// Update history:
//
// [99.10.18] jcohen
// version 1.3
// added optional fields to header
// to use: add_field("key","value",histogram);
// or: add_field_n("key",45,histogram);
//
// [99.01.08] jcohen
// version 1.2
// histogram includes skew and kurtosis
// call enable_skew(h) to enable skew and kurtosis on histogram h
//
// [98.07.29] jcohen
// added add_histogram function
// added version 1.1 display, added num_bins display
// added read_hist
//
*/
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_stdlib.h"
#include <float.h>
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_math.h"
#include "hist.h"
namespace ACE_SCTP
{
hist_t *head_hist, *tail_hist;
hist_t *histogram(char *name, unsigned int num_bins, double first,
double last) {
ACE_SCTP::hist_t *hist;
if ((hist = (hist_t *)ACE_OS::malloc(sizeof(hist_t))) == 0) {
ACE_OS::fprintf(stderr, "unable to allocate memory for histogram : %s", name);
ACE_OS::exit(-1);
}
if ((hist->hs = (unsigned int *)ACE_OS::malloc(sizeof(unsigned int) * (num_bins+2))) == 0){
ACE_OS::fprintf(stderr, "unable to allocate memory for histogram : %s", name);
ACE_OS::exit(-1);
}
ACE_OS::memset(hist->hs, 0, sizeof(unsigned int) * (num_bins+2));
hist->name = name;
hist->num_bins = num_bins;
hist->first = first;
hist->last = last;
hist->num_points = 0;
hist->max_num_outer = hist->num_outer = 0;
hist->sum = 0.0;
hist->sum2 = 0.0;
hist->sum3 = 0.0;
hist->sum4 = 0.0;
hist->max = DBL_MIN;
hist->min = DBL_MAX;
hist->outer = 0;
hist->next = 0;
hist->skew = 0;
hist->firstoptheader = 0;
if(head_hist == 0)
head_hist = tail_hist = hist;
else {
tail_hist->next = hist;
tail_hist = tail_hist->next;
}
return hist;
}
void set_outer(unsigned int max_num_outer, hist_t *hist) {
hist->outer = (double *)ACE_OS::realloc(hist->outer,sizeof(double) * max_num_outer);
hist->max_num_outer = max_num_outer;
}
void enable_skew(hist_t *hist) {
hist->skew = 1;
}
void add_field(char *key, char *value, hist_t *hist) {
struct optheader *nextoptheader, *trace;
/* create and prepare nextoptheader */
nextoptheader = (struct optheader *) ACE_OS::malloc(sizeof(struct optheader));
nextoptheader->key = (char *) ACE_OS::malloc(ACE_OS::strlen(key)+1);
nextoptheader->value = (char *) ACE_OS::malloc(ACE_OS::strlen(value)+1);
nextoptheader->next = 0;
ACE_OS::strcpy(nextoptheader->key,key);
ACE_OS::strcpy(nextoptheader->value,value);
/* tack nextoptheader onto end of optheader list */
if (hist->firstoptheader == 0) {
hist->firstoptheader = nextoptheader;
} else {
trace = hist->firstoptheader;
while (trace->next != 0) {
trace = trace->next;
}
trace->next = nextoptheader;
}
}
void add_field_n(char *key, int value, hist_t *hist) {
char textvalue[40];
ACE_OS::sprintf(textvalue,"%d",value);
add_field(key,textvalue,hist);
}
void record(double point, hist_t *hist) {
unsigned int bin;
double incsum;
if (point < hist->min) hist->min = point;
if (point > hist->max) hist->max = point;
if (point < hist->first) {
bin = 0;
if (hist->max_num_outer > hist->num_outer)
hist->outer[hist->num_outer++] = point;
}
else if (point >= hist->last) {
bin = hist->num_bins+1;
if (hist->max_num_outer > hist->num_outer)
hist->outer[hist->num_outer++] = point;
}
else
bin = (unsigned int)(((point - hist->first)*(hist->num_bins))/(hist->last - hist->first)+1);
hist->hs[bin]++;
hist->num_points++;
hist->sum+=point;
incsum = point*point;
hist->sum2 += incsum;
if (hist->skew) {
incsum *= point;
hist->sum3 += incsum;
incsum *= point;
hist->sum4 += incsum;
}
/* printf("point : %g, bin %u, points in bin %u\n", point, bin, hist->hs[bin]); */
}
void report_to(FILE *strm, hist_t *hist) {
unsigned int sofar, i;
double p1, p2, d, mean;
double variance;
struct optheader *trace;
if (hist->num_points == 0) {
ACE_OS::fprintf(strm, "\n\n\t\t\t\tHistogram %s is empty\n\n", hist->name);
return;
}
ACE_OS::fprintf(strm, "\n\n\t\t\t\tHistogram %s\n", hist->name);
if (hist->skew) ACE_OS::fprintf(strm, "version: %s\n", HIST_VERSION);
else ACE_OS::fprintf(strm, "version: 1.1\n");
ACE_OS::fprintf(strm, "minimum: %g\n", hist->min);
ACE_OS::fprintf(strm, "maximum: %g\n", hist->max);
ACE_OS::fprintf(strm, "mean: %g\n", mean = hist->sum/hist->num_points);
variance = (hist->sum2 -
2*hist->sum*mean +
hist->num_points*mean*mean) / (hist->num_points-1);
ACE_OS::fprintf(strm, "variance: %g\n", variance);
if (hist->skew)
{
ACE_OS::fprintf(strm, "skew: %g\n", ((hist->sum3 -
3*hist->sum2*mean +
3*hist->sum*mean*mean -
hist->num_points*mean*mean*mean) /
pow(sqrt(variance),3) / hist->num_points));
ACE_OS::fprintf(strm, "kurtosis: %g\n", ((hist->sum4 -
4*hist->sum3*mean +
6*hist->sum2*mean*mean -
4*hist->sum*mean*mean*mean +
hist->num_points*mean*mean*mean*mean) /
pow(sqrt(variance),4)) / hist->num_points - 3);
}
ACE_OS::fprintf(strm, "num_points: %u\n", hist->num_points);
ACE_OS::fprintf(strm, "num_bins: %d %g %g\n", hist->num_bins,hist->first,hist->last);
if (hist->firstoptheader) {
trace = hist->firstoptheader;
while(trace->next != 0)
{
ACE_OS::fprintf(strm, "%s: %s\n", trace->key, trace->value);
trace = trace->next;
}
ACE_OS::fprintf(strm, "%s: %s\n", trace->key, trace->value);
}
ACE_OS::fprintf(strm, "\n");
sofar = hist->hs[0];
ACE_OS::fprintf(strm, "\t Low - High Count Fraction Cumulative\n");
ACE_OS::fprintf(strm, "\t below - %12.3f : %5u %0.3f %0.3f\n", hist->first,
hist->hs[0], (double)hist->hs[0]/hist->num_points,
(double)histfloor(1000.0*sofar/hist->num_points)/1000.0);
p2 = hist->first;
d = (hist->last - hist->first)/hist->num_bins;
for(i = 1; i <= hist->num_bins; i++) {
p1 = p2;
p2 = i*d+hist->first;
sofar += hist->hs[i];
ACE_OS::fprintf(strm, "\t%12.3f - %12.3f : %5u %0.3f %0.3f\n", p1, p2, hist->hs[i],
(double)hist->hs[i]/hist->num_points,
(double)histfloor(1000.0*sofar/hist->num_points)/1000.0);
}
sofar += hist->hs[hist->num_bins+1];
ACE_OS::fprintf(strm, "\t%12.3f - above : %5u %0.3f %0.3f\n\n",
hist->last, hist->hs[hist->num_bins+1],
(double)hist->hs[hist->num_bins+1]/hist->num_points,
(double)histfloor(1000.0*sofar/hist->num_points)/1000.0);
if (hist->num_outer)
{
ACE_OS::fprintf(strm, "outliers:\n");
for(i = 0; i < hist->num_outer; i++) ACE_OS::fprintf(strm, "\t%12.3f\n",
hist->outer[i]);
}
ACE_OS::fprintf(strm, "\n\n");
}
void report() {
hist_t *temp = head_hist;
for(; temp; temp = temp->next)
report_to(stdout, temp);
}
void stats_init() {
/* this is not necessary */
}
double get_mean(HIST hist) {
return(hist->sum/hist->num_points);
}
double get_min(HIST hist) {
return(hist->min);
}
double get_max(HIST hist) {
return(hist->max);
}
double get_variance(HIST hist) {
double mean = hist->sum/hist->num_points;
return((hist->sum2 - 2*hist->sum*mean + hist->num_points*mean*mean)/
(hist->num_points-1));
}
double get_num(HIST hist) {
return(hist->num_points);
}
void set_mean(HIST hist, double mean) {
hist->sum = mean*hist->num_points;
}
void set_min(HIST hist, double min) {
hist->min = min;
}
void set_max(HIST hist, double max) {
hist->max = max;
}
void set_variance(HIST hist, double variance) {
/* do this after seting mean */
double mean = hist->sum/hist->num_points;
hist->sum2 = variance *(hist->num_points-1) + 2*hist->sum*mean - hist->num_points*mean*mean;
}
void add_histogram(HIST dest, HIST source) {
unsigned int i,j;
double d,midpoint;
d = (source->last - source->first)/source->num_bins;
for(i=1;i<=source->num_bins;i++) {
midpoint = source->first + ((i - 0.5) * d);
for(j=0;j<source->hs[i];j++) {
record(midpoint,dest);
}
}
for(i=0;i<source->num_outer;i++)
record(source->outer[i],dest);
set_min(dest, get_min(source));
set_max(dest, get_max(source));
set_mean(dest, get_mean(source));
set_variance(dest, get_variance(source));
}
double histfloor (double x) {
#ifdef WIN32
return ACE_OS::floor(x);
#else
return static_cast<double> (static_cast<long long> (x));
#endif
}
}
|