annotate Histogram.c @ 14:1fbaedaac2c7

made defualt brch pure C (no VMS__malloc..)
author Me@portablequad
date Sat, 11 Feb 2012 19:19:06 -0800
parents 31a248920429
children 32489b8b763c
rev   line source
Me@0 1 /*
Me@0 2 * Copyright 2010 OpenSourceStewardshipFoundation.org
Me@0 3 * Licensed under GNU General Public License version 2
Me@0 4 *
Me@0 5 * Author: seanhalle@yahoo.com
Me@0 6 *
Me@0 7 */
Me@4 8 #include <stdio.h>
Me@0 9 #include "Histogram.h"
Me@0 10
msach@10 11 //External variables for saving of the histogram
msach@10 12 //These have to be defined by to plugins in order to enable VMS to print this
msach@10 13 //information to the histogram file
msach@10 14 extern char __ProgrammName[]; //Defined in main.c
msach@10 15 extern char __Scheduler[]; //Defined in VPThread_PluginFns.c
msach@10 16 extern char __DataSet[255];
Me@0 17
Me@0 18 /*This Histogram Abstract Data Type has a number of bins plus a range of
Me@0 19 * values that the bins span, both chosen at creation.
Me@0 20 *
Me@0 21 *One creates a Histogram instance using the makeHistogram function, then
Me@0 22 * updates it with the addToHist function, and prints it out with the
Me@0 23 * printHist function.
Me@0 24 *
Me@0 25 *Note, the bin width is an integer, so the end of the range is adjusted
Me@0 26 * accordingly. Use the bin-width to calculate the bin boundaries.
Me@0 27 */
Me@0 28
Me@0 29
Me@0 30 Histogram *
SeanHalle@5 31 makeHistogram( int32 numBins, int32 startOfRange, int32 endOfRange )
SeanHalle@6 32
Me@0 33 {
Me@0 34 Histogram *hist;
SeanHalle@5 35 int32 i;
Me@0 36
Me@0 37
Me@14 38 hist = malloc( sizeof(Histogram) );
Me@14 39 hist->bins = malloc( numBins * sizeof(int32) );
Me@0 40
Me@0 41 hist->numBins = numBins;
Me@0 42 hist->binWidth = (endOfRange - startOfRange) / numBins;
Me@0 43 hist->endOfRange = startOfRange + hist->binWidth * numBins;
Me@0 44 hist->startOfRange = startOfRange;
Me@0 45
Me@0 46 for( i = 0; i < hist->numBins; i++ )
Me@0 47 {
Me@0 48 hist->bins[ i ] = 0;
Me@0 49 }
Me@0 50
Me@7 51 hist->name = NULL;
Me@7 52 return hist;
Me@7 53 }
Me@7 54
Me@7 55 inline void
Me@7 56 makeHist_helper( Histogram *hist, int32 numBins,
Me@7 57 int32 startOfRange, int32 binWidth, char *nameCopy )
Me@7 58 {
Me@7 59 hist->numBins = numBins;
Me@7 60 hist->binWidth = binWidth;
Me@7 61 hist->endOfRange = startOfRange + hist->binWidth * numBins;
Me@7 62 hist->startOfRange = startOfRange;
Me@7 63 hist->name = nameCopy;
Me@7 64 memset( hist->bins, 0, numBins * sizeof(int32) );
Me@7 65 }
Me@7 66
Me@7 67
Me@7 68 Histogram *
Me@7 69 makeFixedBinHist( int32 numBins, int32 startOfRange, int32 binWidth,
Me@7 70 char *name )
Me@7 71
Me@7 72 {
Me@7 73 Histogram *hist;
Me@7 74
Me@14 75 hist = malloc( sizeof(Histogram) );
Me@14 76 hist->bins = malloc( numBins * sizeof(int32) );
Me@7 77
Me@14 78 makeHist_helper( hist, numBins, startOfRange, binWidth,strDup(name));
Me@7 79
Me@7 80 return hist;
Me@7 81 }
Me@7 82
Me@7 83 Histogram *
Me@14 84 makeFixedBinHist( int32 numBins, int32 startOfRange, int32 binWidth,
Me@7 85 char *name )
Me@7 86
Me@7 87 {
Me@7 88 Histogram *hist;
Me@7 89
Me@7 90 hist = malloc( sizeof(Histogram) );
Me@7 91 hist->bins = malloc( numBins * sizeof(int32) );
Me@7 92
Me@7 93 makeHist_helper( hist, numBins, startOfRange, binWidth, strdup(name));
Me@7 94
Me@0 95 return hist;
Me@0 96 }
Me@0 97
SeanHalle@5 98 void inline
SeanHalle@5 99 addToHist( int32 value, Histogram *hist )
Me@0 100 {
SeanHalle@5 101 int32 binIdx;
Me@0 102
Me@0 103 if( value < hist->startOfRange )
Me@0 104 { binIdx = 0;
Me@0 105 }
Me@0 106 else if( value > hist->endOfRange )
Me@0 107 { binIdx = hist->numBins - 1;
Me@0 108 }
Me@0 109 else
Me@0 110 {
Me@0 111 binIdx = (value - hist->startOfRange) / hist->binWidth;
Me@0 112 }
Me@0 113
Me@0 114 hist->bins[ binIdx ] += 1;
Me@0 115 }
Me@0 116
Me@8 117 void inline
Me@8 118 subFromHist( int32 value, Histogram *hist )
Me@8 119 {
Me@8 120 int32 binIdx;
Me@8 121
Me@8 122 if( value < hist->startOfRange )
Me@8 123 { binIdx = 0;
Me@8 124 }
Me@8 125 else if( value > hist->endOfRange )
Me@8 126 { binIdx = hist->numBins - 1;
Me@8 127 }
Me@8 128 else
Me@8 129 {
Me@8 130 binIdx = (value - hist->startOfRange) / hist->binWidth;
Me@8 131 }
Me@8 132
Me@8 133 hist->bins[ binIdx ] -= 1;
Me@8 134 }
Me@8 135
SeanHalle@5 136
Me@7 137 /*Inline because use with RDTSC in innermost code so need ultra-fast
Me@7 138 */
SeanHalle@5 139 void inline
msach@10 140 addIntervalToHist( uint32 startIntvl, uint32 endIntvl, Histogram *hist )
SeanHalle@5 141 {
SeanHalle@5 142 int32 value;
SeanHalle@5 143
SeanHalle@5 144 value = endIntvl - startIntvl;
SeanHalle@5 145 if( value < 0 || value > 10000000 ) return; //sanity check
SeanHalle@5 146 addToHist( value, hist );
SeanHalle@5 147 }
SeanHalle@5 148
Me@8 149 void inline
Me@8 150 subIntervalFromHist( int32 startIntvl, int32 endIntvl, Histogram *hist )
Me@8 151 {
Me@8 152 int32 value;
Me@8 153
Me@8 154 value = endIntvl - startIntvl;
Me@8 155 if( value < 0 || value > 10000000 ) return; //sanity check
Me@8 156 subFromHist( value, hist );
Me@8 157 }
Me@8 158
Me@0 159 void
msach@10 160 saveHistToFile(Histogram *hist)
msach@10 161 {
msach@10 162 FILE *output;
msach@10 163 int32 binIdx, binStart, binEnd, centerValue, width;
msach@10 164 int32 maxHeight, i,n;
msach@11 165 float32 total, total2, expectedValue1, expectedValue2;
msach@10 166
msach@10 167 if(hist == NULL || hist->name == NULL)
msach@10 168 return;
msach@10 169
msach@10 170 //Calculate the average
msach@10 171 //do all except the top bin
msach@10 172 maxHeight = 0; total = 0.0; expectedValue1 = 0.0;
msach@10 173 for( i = 0; i < hist->numBins -1; i++ )
msach@10 174 {
msach@10 175 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
msach@10 176 total += hist->bins[ i ];
msach@10 177 binStart = hist->startOfRange + hist->binWidth * i;
msach@10 178 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
msach@10 179 }
msach@10 180 //copy and calc expected value minus the top bin
msach@10 181 expectedValue2 = expectedValue1;
msach@10 182 expectedValue2 /= total;
msach@10 183 total2 = total;
msach@10 184 //now do last iteration, to add the top bin
msach@10 185 if(maxHeight < hist->bins[ i ])
msach@10 186 maxHeight = hist->bins[ i ];
msach@10 187 total += hist->bins[ i ];
msach@10 188 binStart = hist->startOfRange + hist->binWidth * i;
msach@10 189 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
msach@10 190
msach@10 191 expectedValue1 /= total;
msach@10 192
msach@10 193
msach@10 194
msach@10 195
msach@10 196 //If a histogram directory does not exist, do not save to file.
msach@10 197 //TODO change to argument
msach@10 198 char filename[255];
msach@10 199 for(n=0;n<255;n++)
msach@10 200 {
msach@10 201 sprintf(filename, "./histograms/%s.%d.dat", hist->name,n);
msach@10 202 output = fopen(filename,"r");
msach@10 203 if(output)
msach@10 204 {
msach@10 205 fclose(output);
msach@10 206 }else{
msach@10 207 break;
msach@10 208 }
msach@10 209 }
msach@10 210 printf("Saving Hist to File: %s ...\n", filename);
msach@10 211 output = fopen(filename,"w+");
msach@10 212 if(output == NULL){
msach@10 213 printf("[!]No histogram was saved. To save histograms create folder 'histograms'.\n");
msach@10 214 return;
msach@10 215 }
msach@10 216
msach@10 217 /*
msach@10 218 * Write the header of the measurement file.
msach@10 219 */
msach@10 220 //--------------------------
msach@10 221 //Build Environment
msach@10 222 fprintf(output, "# >> Build Environment <<\n");
msach@10 223 fprintf(output, "# Hardware Architecture: ");
msach@10 224 #ifdef __x86_64
msach@10 225 fprintf(output, "x86_64");
msach@11 226 #endif //__x86_64
msach@10 227 #ifdef __i386
msach@10 228 fprintf(output, "x86");
msach@11 229 #endif //__i386
msach@10 230 fprintf(output, "\n");
msach@10 231 fprintf(output, "# GCC VERSION: %d.%d.%d\n",__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__);
msach@10 232 fprintf(output, "# Build Date: %s %s\n", __DATE__, __TIME__);
msach@10 233 fprintf(output, "# Number of Cores: %d\n", NUM_CORES);
msach@10 234 //--------------------------
msach@10 235
msach@10 236 //--------------------------
msach@10 237 //VMS Plugins
msach@10 238 fprintf(output, "#\n# >> VMS Plugins <<\n");
msach@10 239 fprintf(output, "# Language : ");
msach@10 240 #ifdef VPTHREAD
msach@10 241 fprintf(output, "VPThread");
msach@10 242 #endif
msach@10 243 #ifdef VCILK
msach@10 244 fprintf(output, "VCilk");
msach@10 245 #endif
msach@10 246 #ifdef SSR
msach@10 247 fprintf(output, "SSR");
msach@10 248 #endif
msach@10 249 fprintf(output, "\n");
msach@10 250 fprintf(output, "# Scheduler: %s\n", __Scheduler);
msach@10 251
msach@10 252 //--------------------------
msach@10 253 //Application
msach@10 254 fprintf(output, "#\n# >> Application <<\n");
msach@10 255 fprintf(output, "# Name: %s\n", __ProgrammName);
msach@10 256 fprintf(output, "# Data Set:\n%s\n",__DataSet);
msach@10 257
msach@10 258 //--------------------------
msach@10 259 //Histogram
msach@10 260 fprintf(output, "#\n# Histogram Name: %s\n", hist->name);
msach@10 261 fprintf(output, "# Expected Values\n");
msach@10 262 fprintf(output, "#\tnum samples: %d | expected value: %3.2f \n",
msach@10 263 (int)total, expectedValue1 );
msach@10 264 fprintf(output, "#\tminus top bin, num samples: %d | expected value: %3.2f \n",
msach@10 265 (int)total2, expectedValue2 );
msach@10 266 fprintf(output, "#\n# [Interval] [Center Value] [Count] [relative Count] [Width]\n");
msach@10 267
msach@10 268 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
msach@10 269 {
msach@10 270 binStart = hist->startOfRange + hist->binWidth * binIdx;
msach@10 271 binEnd = binStart + hist->binWidth - 1;
msach@10 272 centerValue = (binStart+binEnd)/2;
msach@10 273 width = (binEnd-binStart)+1;
msach@10 274 fprintf(output, "%d-%d\t%d\t%d\t%.4f\t%d\n", binStart, binEnd, centerValue,
msach@10 275 hist->bins[ binIdx ],
msach@10 276 hist->bins[ binIdx ]/total, width);
msach@10 277 }
msach@10 278
msach@10 279 fclose(output);
msach@10 280 fflush(stdout);
msach@10 281 }
msach@10 282
msach@10 283 void
Me@0 284 printHist( Histogram *hist )
Me@0 285 {
Me@7 286 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
Me@8 287 float32 total, total2, binPercent, expectedValue1, expectedValue2;
Me@0 288
Me@8 289 if( hist == NULL ) return;
Me@8 290
Me@7 291 //do all except the top bin
Me@7 292 maxHeight = 0; total = 0.0; expectedValue1 = 0.0;
Me@7 293 for( i = 0; i < hist->numBins -1; i++ )
Me@0 294 {
Me@0 295 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
Me@7 296 total += hist->bins[ i ];
Me@7 297 binStart = hist->startOfRange + hist->binWidth * i;
Me@7 298 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
Me@0 299 }
Me@7 300 //copy and calc expected value minus the top bin
Me@8 301 expectedValue2 = expectedValue1;
Me@7 302 expectedValue2 /= total;
Me@8 303 total2 = total;
Me@7 304 //now do last iteration, to add the top bin
Me@7 305 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
Me@7 306 total += hist->bins[ i ];
Me@7 307 binStart = hist->startOfRange + hist->binWidth * i;
Me@7 308 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
Me@7 309
Me@7 310 expectedValue1 /= total;
Me@7 311
Me@7 312 printf( "histogram: " );
Me@7 313 if( hist->name != NULL ) printf( "%s\n", hist->name );
Me@7 314 else printf( "\n" );
Me@8 315 printf( " num samples: %d | expected value: %3.2f \n",
Me@8 316 (int)total, expectedValue1 );
Me@8 317 printf( "minus top bin, num samples: %d | expected value: %3.2f \n",
Me@8 318 (int)total2, expectedValue2 );
Me@7 319
msach@10 320 if(maxHeight < 60){
msach@10 321 barValue = 1;
msach@10 322 printf("Single Bar Value: %i\n", barValue);
msach@10 323 }else{
msach@10 324 barValue = maxHeight / 60; //60 spaces across page for tallest bin
msach@10 325 printf("Single Bar Value: %0.3f\n", (float)maxHeight /60);
msach@10 326 }
msach@10 327
Me@8 328 if( barValue == 0 ) { printf("error: bar val zero\n"); return; }
Me@0 329 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
Me@0 330 {
Me@0 331 binStart = hist->startOfRange + hist->binWidth * binIdx;
Me@0 332 binEnd = binStart + hist->binWidth - 1;
Me@7 333 binPercent = 100 * hist->bins[ binIdx ] / total;
Me@7 334 printf("bin range: %d - %d | %3.2f", binStart, binEnd, binPercent );
Me@0 335
Me@0 336 numBars = hist->bins[ binIdx ] / barValue;
Me@0 337 //print one bin, height of bar is num dashes across page
Me@0 338 for( i = 0; i < numBars; i++ )
Me@0 339 {
Me@0 340 printf("-");
Me@0 341 }
Me@0 342 printf("\n");
Me@0 343 }
Me@0 344 }
Me@0 345
Me@3 346 void
Me@3 347 freeHist( Histogram *hist )
Me@3 348 {
Me@7 349 free( hist->bins );
Me@7 350 free( hist->name );
Me@7 351 free( hist );
Me@7 352 }