| rev |
line source |
|
Me@0
|
1 /*
|
|
seanhalle@31
|
2 * Copyright 2010 OpenSourceResearchInstitute.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>
|
|
seanhalle@22
|
9 #include <string.h>
|
|
Me@0
|
10 #include "Histogram.h"
|
|
Me@0
|
11
|
|
Me@0
|
12
|
|
Me@0
|
13 /*This Histogram Abstract Data Type has a number of bins plus a range of
|
|
Me@0
|
14 * values that the bins span, both chosen at creation.
|
|
Me@0
|
15 *
|
|
Me@0
|
16 *One creates a Histogram instance using the makeHistogram function, then
|
|
Me@0
|
17 * updates it with the addToHist function, and prints it out with the
|
|
Me@0
|
18 * printHist function.
|
|
Me@0
|
19 *
|
|
Me@0
|
20 *Note, the bin width is an integer, so the end of the range is adjusted
|
|
Me@0
|
21 * accordingly. Use the bin-width to calculate the bin boundaries.
|
|
Me@0
|
22 */
|
|
Me@0
|
23
|
|
Me@0
|
24
|
|
Me@0
|
25 Histogram *
|
|
SeanHalle@5
|
26 makeHistogram( int32 numBins, int32 startOfRange, int32 endOfRange )
|
|
Me@0
|
27 {
|
|
Me@0
|
28 Histogram *hist;
|
|
SeanHalle@5
|
29 int32 i;
|
|
Me@0
|
30
|
|
Me@0
|
31
|
|
seanhalle@29
|
32 hist = PR_int__malloc( sizeof(Histogram) );
|
|
seanhalle@29
|
33 hist->bins = PR_int__malloc( numBins * sizeof(int32) );
|
|
Me@0
|
34
|
|
Me@0
|
35 hist->numBins = numBins;
|
|
Me@0
|
36 hist->binWidth = (endOfRange - startOfRange) / numBins;
|
|
Me@0
|
37 hist->endOfRange = startOfRange + hist->binWidth * numBins;
|
|
Me@0
|
38 hist->startOfRange = startOfRange;
|
|
Me@0
|
39
|
|
Me@0
|
40 for( i = 0; i < hist->numBins; i++ )
|
|
Me@0
|
41 {
|
|
Me@0
|
42 hist->bins[ i ] = 0;
|
|
Me@0
|
43 }
|
|
Me@0
|
44
|
|
Me@7
|
45 hist->name = NULL;
|
|
Me@7
|
46 return hist;
|
|
Me@7
|
47 }
|
|
Me@7
|
48
|
|
Me@7
|
49 inline void
|
|
Me@7
|
50 makeHist_helper( Histogram *hist, int32 numBins,
|
|
Me@7
|
51 int32 startOfRange, int32 binWidth, char *nameCopy )
|
|
Me@7
|
52 {
|
|
Me@7
|
53 hist->numBins = numBins;
|
|
Me@7
|
54 hist->binWidth = binWidth;
|
|
Me@7
|
55 hist->endOfRange = startOfRange + hist->binWidth * numBins;
|
|
Me@7
|
56 hist->startOfRange = startOfRange;
|
|
Me@7
|
57 hist->name = nameCopy;
|
|
Me@7
|
58 memset( hist->bins, 0, numBins * sizeof(int32) );
|
|
Me@7
|
59 }
|
|
Me@7
|
60
|
|
Me@7
|
61
|
|
Me@7
|
62 Histogram *
|
|
Me@7
|
63 makeFixedBinHist( int32 numBins, int32 startOfRange, int32 binWidth,
|
|
Me@7
|
64 char *name )
|
|
Me@7
|
65
|
|
Me@7
|
66 {
|
|
Me@7
|
67 Histogram *hist;
|
|
Me@7
|
68
|
|
seanhalle@29
|
69 hist = PR_int__malloc( sizeof(Histogram) );
|
|
seanhalle@29
|
70 hist->bins = PR_int__malloc( numBins * sizeof(int32) );
|
|
Me@7
|
71
|
|
seanhalle@29
|
72 char *nameCopy = (char *)PR_int__strDup(name);
|
|
seanhalle@22
|
73 makeHist_helper( hist, numBins, startOfRange, binWidth, nameCopy);
|
|
Me@7
|
74
|
|
Me@7
|
75 return hist;
|
|
Me@7
|
76 }
|
|
Me@7
|
77
|
|
Me@7
|
78 Histogram *
|
|
Me@15
|
79 makeFixedBinHistExt( int32 numBins, int32 startOfRange, int32 binWidth,
|
|
Me@7
|
80 char *name )
|
|
Me@7
|
81
|
|
Me@7
|
82 {
|
|
Me@7
|
83 Histogram *hist;
|
|
Me@7
|
84
|
|
Me@7
|
85 hist = malloc( sizeof(Histogram) );
|
|
Me@7
|
86 hist->bins = malloc( numBins * sizeof(int32) );
|
|
Me@7
|
87
|
|
Me@7
|
88 makeHist_helper( hist, numBins, startOfRange, binWidth, strdup(name));
|
|
Me@7
|
89
|
|
Me@0
|
90 return hist;
|
|
Me@0
|
91 }
|
|
Me@0
|
92
|
|
SeanHalle@5
|
93 void inline
|
|
SeanHalle@5
|
94 addToHist( int32 value, Histogram *hist )
|
|
Me@0
|
95 {
|
|
SeanHalle@5
|
96 int32 binIdx;
|
|
Me@0
|
97
|
|
Me@0
|
98 if( value < hist->startOfRange )
|
|
Me@0
|
99 { binIdx = 0;
|
|
Me@0
|
100 }
|
|
Me@0
|
101 else if( value > hist->endOfRange )
|
|
Me@0
|
102 { binIdx = hist->numBins - 1;
|
|
Me@0
|
103 }
|
|
Me@0
|
104 else
|
|
Me@0
|
105 {
|
|
Me@0
|
106 binIdx = (value - hist->startOfRange) / hist->binWidth;
|
|
Me@0
|
107 }
|
|
Me@0
|
108
|
|
Me@0
|
109 hist->bins[ binIdx ] += 1;
|
|
Me@0
|
110 }
|
|
Me@0
|
111
|
|
Me@8
|
112 void inline
|
|
Me@8
|
113 subFromHist( int32 value, Histogram *hist )
|
|
Me@8
|
114 {
|
|
Me@8
|
115 int32 binIdx;
|
|
Me@8
|
116
|
|
Me@8
|
117 if( value < hist->startOfRange )
|
|
Me@8
|
118 { binIdx = 0;
|
|
Me@8
|
119 }
|
|
Me@8
|
120 else if( value > hist->endOfRange )
|
|
Me@8
|
121 { binIdx = hist->numBins - 1;
|
|
Me@8
|
122 }
|
|
Me@8
|
123 else
|
|
Me@8
|
124 {
|
|
Me@8
|
125 binIdx = (value - hist->startOfRange) / hist->binWidth;
|
|
Me@8
|
126 }
|
|
Me@8
|
127
|
|
Me@8
|
128 hist->bins[ binIdx ] -= 1;
|
|
Me@8
|
129 }
|
|
Me@8
|
130
|
|
SeanHalle@5
|
131
|
|
Me@7
|
132 /*Inline because use with RDTSC in innermost code so need ultra-fast
|
|
Me@7
|
133 */
|
|
SeanHalle@5
|
134 void inline
|
|
msach@10
|
135 addIntervalToHist( uint32 startIntvl, uint32 endIntvl, Histogram *hist )
|
|
SeanHalle@5
|
136 {
|
|
SeanHalle@5
|
137 int32 value;
|
|
SeanHalle@5
|
138
|
|
SeanHalle@5
|
139 value = endIntvl - startIntvl;
|
|
SeanHalle@5
|
140 if( value < 0 || value > 10000000 ) return; //sanity check
|
|
SeanHalle@5
|
141 addToHist( value, hist );
|
|
SeanHalle@5
|
142 }
|
|
SeanHalle@5
|
143
|
|
Me@8
|
144 void inline
|
|
Me@8
|
145 subIntervalFromHist( int32 startIntvl, int32 endIntvl, Histogram *hist )
|
|
Me@8
|
146 {
|
|
Me@8
|
147 int32 value;
|
|
Me@8
|
148
|
|
Me@8
|
149 value = endIntvl - startIntvl;
|
|
Me@8
|
150 if( value < 0 || value > 10000000 ) return; //sanity check
|
|
Me@8
|
151 subFromHist( value, hist );
|
|
Me@8
|
152 }
|
|
Me@8
|
153
|
|
Me@0
|
154 void
|
|
msach@10
|
155 saveHistToFile(Histogram *hist)
|
|
msach@10
|
156 {
|
|
msach@10
|
157 FILE *output;
|
|
msach@10
|
158 int32 binIdx, binStart, binEnd, centerValue, width;
|
|
msach@10
|
159 int32 maxHeight, i,n;
|
|
msach@11
|
160 float32 total, total2, expectedValue1, expectedValue2;
|
|
msach@10
|
161
|
|
msach@10
|
162 if(hist == NULL || hist->name == NULL)
|
|
msach@10
|
163 return;
|
|
msach@10
|
164
|
|
msach@10
|
165 //Calculate the average
|
|
msach@10
|
166 //do all except the top bin
|
|
msach@10
|
167 maxHeight = 0; total = 0.0; expectedValue1 = 0.0;
|
|
msach@10
|
168 for( i = 0; i < hist->numBins -1; i++ )
|
|
msach@10
|
169 {
|
|
msach@10
|
170 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
|
|
msach@10
|
171 total += hist->bins[ i ];
|
|
msach@10
|
172 binStart = hist->startOfRange + hist->binWidth * i;
|
|
msach@10
|
173 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
|
|
msach@10
|
174 }
|
|
msach@10
|
175 //copy and calc expected value minus the top bin
|
|
msach@10
|
176 expectedValue2 = expectedValue1;
|
|
msach@10
|
177 expectedValue2 /= total;
|
|
msach@10
|
178 total2 = total;
|
|
msach@10
|
179 //now do last iteration, to add the top bin
|
|
msach@10
|
180 if(maxHeight < hist->bins[ i ])
|
|
msach@10
|
181 maxHeight = hist->bins[ i ];
|
|
msach@10
|
182 total += hist->bins[ i ];
|
|
msach@10
|
183 binStart = hist->startOfRange + hist->binWidth * i;
|
|
msach@10
|
184 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
|
|
msach@10
|
185
|
|
msach@10
|
186 expectedValue1 /= total;
|
|
msach@10
|
187
|
|
msach@10
|
188
|
|
msach@10
|
189
|
|
msach@10
|
190
|
|
msach@10
|
191 //If a histogram directory does not exist, do not save to file.
|
|
msach@10
|
192 //TODO change to argument
|
|
msach@10
|
193 char filename[255];
|
|
msach@10
|
194 for(n=0;n<255;n++)
|
|
msach@10
|
195 {
|
|
msach@10
|
196 sprintf(filename, "./histograms/%s.%d.dat", hist->name,n);
|
|
msach@10
|
197 output = fopen(filename,"r");
|
|
msach@10
|
198 if(output)
|
|
msach@10
|
199 {
|
|
msach@10
|
200 fclose(output);
|
|
msach@10
|
201 }else{
|
|
msach@10
|
202 break;
|
|
msach@10
|
203 }
|
|
msach@10
|
204 }
|
|
msach@10
|
205 printf("Saving Hist to File: %s ...\n", filename);
|
|
msach@10
|
206 output = fopen(filename,"w+");
|
|
msach@10
|
207 if(output == NULL){
|
|
msach@10
|
208 printf("[!]No histogram was saved. To save histograms create folder 'histograms'.\n");
|
|
msach@10
|
209 return;
|
|
msach@10
|
210 }
|
|
msach@10
|
211
|
|
msach@10
|
212 fprintf(output, "#\n# Histogram Name: %s\n", hist->name);
|
|
msach@10
|
213 fprintf(output, "# Expected Values\n");
|
|
msach@10
|
214 fprintf(output, "#\tnum samples: %d | expected value: %3.2f \n",
|
|
msach@10
|
215 (int)total, expectedValue1 );
|
|
msach@10
|
216 fprintf(output, "#\tminus top bin, num samples: %d | expected value: %3.2f \n",
|
|
msach@10
|
217 (int)total2, expectedValue2 );
|
|
msach@10
|
218 fprintf(output, "#\n# [Interval] [Center Value] [Count] [relative Count] [Width]\n");
|
|
msach@10
|
219
|
|
msach@10
|
220 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
|
|
msach@10
|
221 {
|
|
msach@10
|
222 binStart = hist->startOfRange + hist->binWidth * binIdx;
|
|
msach@10
|
223 binEnd = binStart + hist->binWidth - 1;
|
|
msach@10
|
224 centerValue = (binStart+binEnd)/2;
|
|
msach@10
|
225 width = (binEnd-binStart)+1;
|
|
msach@10
|
226 fprintf(output, "%d-%d\t%d\t%d\t%.4f\t%d\n", binStart, binEnd, centerValue,
|
|
msach@10
|
227 hist->bins[ binIdx ],
|
|
msach@10
|
228 hist->bins[ binIdx ]/total, width);
|
|
msach@10
|
229 }
|
|
msach@10
|
230
|
|
msach@10
|
231 fclose(output);
|
|
msach@10
|
232 fflush(stdout);
|
|
msach@10
|
233 }
|
|
msach@10
|
234
|
|
msach@10
|
235 void
|
|
Me@0
|
236 printHist( Histogram *hist )
|
|
Me@0
|
237 {
|
|
Me@7
|
238 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
|
|
Me@8
|
239 float32 total, total2, binPercent, expectedValue1, expectedValue2;
|
|
Me@0
|
240
|
|
Me@8
|
241 if( hist == NULL ) return;
|
|
Me@8
|
242
|
|
Me@7
|
243 //do all except the top bin
|
|
Me@7
|
244 maxHeight = 0; total = 0.0; expectedValue1 = 0.0;
|
|
Me@7
|
245 for( i = 0; i < hist->numBins -1; i++ )
|
|
Me@0
|
246 {
|
|
Me@0
|
247 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
|
|
Me@7
|
248 total += hist->bins[ i ];
|
|
Me@7
|
249 binStart = hist->startOfRange + hist->binWidth * i;
|
|
Me@7
|
250 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
|
|
Me@0
|
251 }
|
|
Me@7
|
252 //copy and calc expected value minus the top bin
|
|
Me@8
|
253 expectedValue2 = expectedValue1;
|
|
Me@7
|
254 expectedValue2 /= total;
|
|
Me@8
|
255 total2 = total;
|
|
Me@7
|
256 //now do last iteration, to add the top bin
|
|
Me@7
|
257 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
|
|
Me@7
|
258 total += hist->bins[ i ];
|
|
Me@7
|
259 binStart = hist->startOfRange + hist->binWidth * i;
|
|
Me@7
|
260 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
|
|
Me@7
|
261
|
|
Me@7
|
262 expectedValue1 /= total;
|
|
Me@7
|
263
|
|
Me@7
|
264 printf( "histogram: " );
|
|
Me@7
|
265 if( hist->name != NULL ) printf( "%s\n", hist->name );
|
|
Me@7
|
266 else printf( "\n" );
|
|
Me@8
|
267 printf( " num samples: %d | expected value: %3.2f \n",
|
|
Me@8
|
268 (int)total, expectedValue1 );
|
|
Me@8
|
269 printf( "minus top bin, num samples: %d | expected value: %3.2f \n",
|
|
Me@8
|
270 (int)total2, expectedValue2 );
|
|
Me@7
|
271
|
|
msach@10
|
272 if(maxHeight < 60){
|
|
msach@10
|
273 barValue = 1;
|
|
msach@10
|
274 printf("Single Bar Value: %i\n", barValue);
|
|
msach@10
|
275 }else{
|
|
msach@10
|
276 barValue = maxHeight / 60; //60 spaces across page for tallest bin
|
|
msach@10
|
277 printf("Single Bar Value: %0.3f\n", (float)maxHeight /60);
|
|
msach@10
|
278 }
|
|
msach@10
|
279
|
|
Me@8
|
280 if( barValue == 0 ) { printf("error: bar val zero\n"); return; }
|
|
Me@0
|
281 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
|
|
Me@0
|
282 {
|
|
Me@0
|
283 binStart = hist->startOfRange + hist->binWidth * binIdx;
|
|
Me@0
|
284 binEnd = binStart + hist->binWidth - 1;
|
|
Me@7
|
285 binPercent = 100 * hist->bins[ binIdx ] / total;
|
|
Me@7
|
286 printf("bin range: %d - %d | %3.2f", binStart, binEnd, binPercent );
|
|
Me@0
|
287
|
|
Me@0
|
288 numBars = hist->bins[ binIdx ] / barValue;
|
|
Me@0
|
289 //print one bin, height of bar is num dashes across page
|
|
Me@0
|
290 for( i = 0; i < numBars; i++ )
|
|
Me@0
|
291 {
|
|
Me@0
|
292 printf("-");
|
|
Me@0
|
293 }
|
|
Me@0
|
294 printf("\n");
|
|
Me@0
|
295 }
|
|
Me@0
|
296 }
|
|
Me@0
|
297
|
|
Me@3
|
298 void
|
|
Me@3
|
299 freeHist( Histogram *hist )
|
|
Me@3
|
300 {
|
|
seanhalle@29
|
301 PR_int__free( hist->bins );
|
|
seanhalle@29
|
302 PR_int__free( hist->name );
|
|
seanhalle@29
|
303 PR_int__free( hist );
|
|
Me@15
|
304 }
|
|
Me@15
|
305 void
|
|
Me@15
|
306 freeHistExt( Histogram *hist )
|
|
Me@15
|
307 {
|
|
Me@7
|
308 free( hist->bins );
|
|
Me@7
|
309 free( hist->name );
|
|
Me@7
|
310 free( hist );
|
|
Me@7
|
311 }
|