comparison Histogram.c @ 10:7a39408f9ea3

Version 0
author Merten Sach <msach@mailbox.tu-berlin.de>
date Mon, 11 Jul 2011 18:10:52 +0200
parents 060d63cb5d34
children 31a248920429
comparison
equal deleted inserted replaced
7:d68258caa53d 8:e15db7e192f0
5 * Author: seanhalle@yahoo.com 5 * Author: seanhalle@yahoo.com
6 * 6 *
7 */ 7 */
8 #include <stdio.h> 8 #include <stdio.h>
9 #include "Histogram.h" 9 #include "Histogram.h"
10 #include "../VMS.h"
10 #include "../vutilities.h" 11 #include "../vutilities.h"
11 #include "../vmalloc.h" 12 #include "../vmalloc.h"
12 13
14 //External variables for saving of the histogram
15 //These have to be defined by to plugins in order to enable VMS to print this
16 //information to the histogram file
17 extern char __ProgrammName[]; //Defined in main.c
18 extern char __Scheduler[]; //Defined in VPThread_PluginFns.c
19 extern char __DataSet[255];
13 20
14 /*This Histogram Abstract Data Type has a number of bins plus a range of 21 /*This Histogram Abstract Data Type has a number of bins plus a range of
15 * values that the bins span, both chosen at creation. 22 * values that the bins span, both chosen at creation.
16 * 23 *
17 *One creates a Histogram instance using the makeHistogram function, then 24 *One creates a Histogram instance using the makeHistogram function, then
131 138
132 139
133 /*Inline because use with RDTSC in innermost code so need ultra-fast 140 /*Inline because use with RDTSC in innermost code so need ultra-fast
134 */ 141 */
135 void inline 142 void inline
136 addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist ) 143 addIntervalToHist( uint32 startIntvl, uint32 endIntvl, Histogram *hist )
137 { 144 {
138 int32 value; 145 int32 value;
139 146
140 value = endIntvl - startIntvl; 147 value = endIntvl - startIntvl;
141 if( value < 0 || value > 10000000 ) return; //sanity check 148 if( value < 0 || value > 10000000 ) return; //sanity check
149 156
150 value = endIntvl - startIntvl; 157 value = endIntvl - startIntvl;
151 if( value < 0 || value > 10000000 ) return; //sanity check 158 if( value < 0 || value > 10000000 ) return; //sanity check
152 subFromHist( value, hist ); 159 subFromHist( value, hist );
153 } 160 }
161
162 void
163 saveHistToFile(Histogram *hist)
164 {
165 FILE *output;
166 int32 binIdx, binStart, binEnd, centerValue, width;
167 int32 maxHeight, i,n;
168 float32 total, total2, binPercent, expectedValue1, expectedValue2;
169
170 if(hist == NULL || hist->name == NULL)
171 return;
172
173 //Calculate the average
174 //do all except the top bin
175 maxHeight = 0; total = 0.0; expectedValue1 = 0.0;
176 for( i = 0; i < hist->numBins -1; i++ )
177 {
178 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
179 total += hist->bins[ i ];
180 binStart = hist->startOfRange + hist->binWidth * i;
181 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
182 }
183 //copy and calc expected value minus the top bin
184 expectedValue2 = expectedValue1;
185 expectedValue2 /= total;
186 total2 = total;
187 //now do last iteration, to add the top bin
188 if(maxHeight < hist->bins[ i ])
189 maxHeight = hist->bins[ i ];
190 total += hist->bins[ i ];
191 binStart = hist->startOfRange + hist->binWidth * i;
192 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
193
194 expectedValue1 /= total;
195
196
197
198
199 //If a histogram directory does not exist, do not save to file.
200 //TODO change to argument
201 char filename[255];
202 for(n=0;n<255;n++)
203 {
204 sprintf(filename, "./histograms/%s.%d.dat", hist->name,n);
205 output = fopen(filename,"r");
206 if(output)
207 {
208 fclose(output);
209 }else{
210 break;
211 }
212 }
213 printf("Saving Hist to File: %s ...\n", filename);
214 output = fopen(filename,"w+");
215 if(output == NULL){
216 printf("[!]No histogram was saved. To save histograms create folder 'histograms'.\n");
217 return;
218 }
219
220 /*
221 * Write the header of the measurement file.
222 */
223 //--------------------------
224 //Build Environment
225 fprintf(output, "# >> Build Environment <<\n");
226 fprintf(output, "# Hardware Architecture: ");
227 #ifdef __x86_64
228 fprintf(output, "x86_64");
229 #endif __
230 #ifdef __i386
231 fprintf(output, "x86");
232 #endif
233 fprintf(output, "\n");
234 fprintf(output, "# GCC VERSION: %d.%d.%d\n",__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__);
235 fprintf(output, "# Build Date: %s %s\n", __DATE__, __TIME__);
236 fprintf(output, "# Number of Cores: %d\n", NUM_CORES);
237 //--------------------------
238
239 //--------------------------
240 //VMS Plugins
241 fprintf(output, "#\n# >> VMS Plugins <<\n");
242 fprintf(output, "# Language : ");
243 #ifdef VPTHREAD
244 fprintf(output, "VPThread");
245 #endif
246 #ifdef VCILK
247 fprintf(output, "VCilk");
248 #endif
249 #ifdef SSR
250 fprintf(output, "SSR");
251 #endif
252 fprintf(output, "\n");
253 fprintf(output, "# Scheduler: %s\n", __Scheduler);
254
255 //--------------------------
256 //Application
257 fprintf(output, "#\n# >> Application <<\n");
258 fprintf(output, "# Name: %s\n", __ProgrammName);
259 fprintf(output, "# Data Set:\n%s\n",__DataSet);
260
261 //--------------------------
262 //Histogram
263 fprintf(output, "#\n# Histogram Name: %s\n", hist->name);
264 fprintf(output, "# Expected Values\n");
265 fprintf(output, "#\tnum samples: %d | expected value: %3.2f \n",
266 (int)total, expectedValue1 );
267 fprintf(output, "#\tminus top bin, num samples: %d | expected value: %3.2f \n",
268 (int)total2, expectedValue2 );
269 fprintf(output, "#\n# [Interval] [Center Value] [Count] [relative Count] [Width]\n");
270
271 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
272 {
273 binStart = hist->startOfRange + hist->binWidth * binIdx;
274 binEnd = binStart + hist->binWidth - 1;
275 centerValue = (binStart+binEnd)/2;
276 width = (binEnd-binStart)+1;
277 fprintf(output, "%d-%d\t%d\t%d\t%.4f\t%d\n", binStart, binEnd, centerValue,
278 hist->bins[ binIdx ],
279 hist->bins[ binIdx ]/total, width);
280 }
281
282 fclose(output);
283 fflush(stdout);
284 }
154 285
155 void 286 void
156 printHist( Histogram *hist ) 287 printHist( Histogram *hist )
157 { 288 {
158 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd; 289 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
179 binStart = hist->startOfRange + hist->binWidth * i; 310 binStart = hist->startOfRange + hist->binWidth * i;
180 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0); 311 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
181 312
182 expectedValue1 /= total; 313 expectedValue1 /= total;
183 314
184 barValue = maxHeight / 60; //60 spaces across page for tallest bin
185
186 printf( "histogram: " ); 315 printf( "histogram: " );
187 if( hist->name != NULL ) printf( "%s\n", hist->name ); 316 if( hist->name != NULL ) printf( "%s\n", hist->name );
188 else printf( "\n" ); 317 else printf( "\n" );
189 printf( " num samples: %d | expected value: %3.2f \n", 318 printf( " num samples: %d | expected value: %3.2f \n",
190 (int)total, expectedValue1 ); 319 (int)total, expectedValue1 );
191 printf( "minus top bin, num samples: %d | expected value: %3.2f \n", 320 printf( "minus top bin, num samples: %d | expected value: %3.2f \n",
192 (int)total2, expectedValue2 ); 321 (int)total2, expectedValue2 );
193 322
323 if(maxHeight < 60){
324 barValue = 1;
325 printf("Single Bar Value: %i\n", barValue);
326 }else{
327 barValue = maxHeight / 60; //60 spaces across page for tallest bin
328 printf("Single Bar Value: %0.3f\n", (float)maxHeight /60);
329 }
330
194 if( barValue == 0 ) { printf("error: bar val zero\n"); return; } 331 if( barValue == 0 ) { printf("error: bar val zero\n"); return; }
195 for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) 332 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
196 { 333 {
197 binStart = hist->startOfRange + hist->binWidth * binIdx; 334 binStart = hist->startOfRange + hist->binWidth * binIdx;
198 binEnd = binStart + hist->binWidth - 1; 335 binEnd = binStart + hist->binWidth - 1;