-
Notifications
You must be signed in to change notification settings - Fork 4
/
hcat.cpp
executable file
·755 lines (703 loc) · 21.1 KB
/
hcat.cpp
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
// hcat: Histogram concatenation program
/* allow files >2GB */
#define _LARGEFILE_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h> // for "isprint()"
const unsigned int MAX_LINE = 1024;
#ifndef MIN
#define MIN(X,Y) (X<Y?X:Y)
#define MAX(X,Y) (X>Y?X:Y)
#endif // MIN/MAX
#ifndef WIN32
#include <unistd.h>
#include <errno.h>
#include <sys/time.h> // for gettimeofday()
#include <sys/types.h>
#endif // !WIN32
void usage()
{
fprintf(stderr, "Usage: hcat [normalize][prange [<rangeMin>:]<rangeMax>][pc <percentile>]\n"
" [bins <numBins>][percent][count][range [<rangeMin>:]<rangeMax>]\n"
" <file1> [<file2> <file3> ...]\n");
}
class FastReader
{
public:
enum Result {OK, ERROR_, DONE, TIMEOUT};
FastReader();
FastReader::Result Read(FILE* filePtr, char* buffer, unsigned int* len,
double timeout = -1.0);
FastReader::Result Readline(FILE* filePtr, char* buffer, unsigned int* len,
double timeout = -1.0);
private:
enum {BUFSIZE = 2048};
char savebuf[BUFSIZE];
char* saveptr;
unsigned int savecount;
}; // end class FastReader
// Simple self-scaling linear/non-linear histogram (one-sided)
class Histogram
{
public:
Histogram();
bool IsEmpty() {return (NULL == bin);}
void Init(unsigned long numBins, double linearity)
{
num_bins = numBins;
q = linearity;
if (bin) delete[] bin;
bin = NULL;
}
bool InitBins(double rangeMin, double rangeMax);
bool Tally(double value, unsigned long count = 1);
void Print(FILE* file, bool showAll = false);
unsigned long Count();
double PercentageInRange(double rangeMin, double rangeMax);
unsigned long CountInRange(double rangeMin, double rangeMax);
double Min() {return min_val;}
double Max() {return ((max_val < 0.0) ? 2.0*max_val : 0.5*max_val);}
double Percentile(double p);
private:
double GetBinValue(unsigned int i)
{
if (bin && bin[i].count)
{
return (bin[i].total / ((double)bin[i].count));
}
else
{
double x = pow(((double)i) / ((double)num_bins-1), 1.0/q);
x *= (max_val - min_val);
x += min_val;
return x;
}
}
typedef struct
{
double total;
unsigned long count;
} Bin;
double q;
unsigned long num_bins;
double min_val;
double max_val;
Bin* bin;
}; // end class Histogram
Histogram::Histogram()
: q(1.0), num_bins(1000), min_val(0.0), max_val(0.0), bin(NULL)
{
}
/** This method creates an empty histogram with a preset
* value range. This is useful for outputting
* equivalent histgrams for multiplots
*/
bool Histogram::InitBins(double rangeMin, double rangeMax)
{
if (bin) delete[] bin;
if (!(bin = new Bin[num_bins]))
{
perror("hcat: Histogram::InitBins() Error allocating bins");
return false;
}
memset(bin, 0, num_bins*sizeof(Bin));
min_val = rangeMin;
max_val = rangeMax;
return true;
} // end Histogram::InitBins()
bool Histogram::Tally(double value, unsigned long count)
{
if (!bin)
{
if (!(bin = new Bin[num_bins]))
{
perror("trpr: Histogram::Tally() Error allocating histogram");
return false;
}
memset(bin, 0, num_bins*sizeof(Bin));
min_val = max_val = value;
bin[0].count = count;
bin[0].total = (value * (double)count);
}
else if ((value > max_val) || (value < min_val))
{
Bin* newBin = new Bin[num_bins];
if (!newBin)
{
perror("trpr: Histogram::Tally() Error reallocating histogram");
return false;
}
memset(newBin, 0, num_bins*sizeof(Bin));
double newScale, minVal;
if (value < min_val)
{
newScale = ((double)(num_bins-1)) / pow(max_val - value, q);
minVal = value;
}
else
{
double s = (value < 0.0) ? 0.5 : 2.0;
newScale = ((double)(num_bins-1)) / pow(s*value - min_val, q);
minVal = min_val;
}
// Copy old histogram bins into new bins
for (unsigned int i = 0; i < num_bins; i++)
{
if (bin[i].count)
{
double x = bin[i].total / ((double)bin[i].count);
unsigned long index = (unsigned long)ceil(newScale * pow(x - minVal, q));
if (index > (num_bins-1)) index = num_bins - 1;
newBin[index].count += bin[i].count;
newBin[index].total += bin[i].total;
}
}
if (value < min_val)
{
newBin[0].count += count;
newBin[0].total += (value * (double)count);
min_val = value;
}
else
{
double s = (value < 0.0) ? 0.5 : 2.0;
max_val = s*value;
unsigned long index =
(unsigned long)ceil(((double)(num_bins-1)) * pow((value-min_val)/(max_val-min_val), q));
if (index > (num_bins-1)) index = num_bins - 1;
newBin[index].count += count;
newBin[index].total += (value * (double)count);
}
delete[] bin;
bin = newBin;
}
else
{
unsigned long index =
(unsigned long)ceil(((double)(num_bins-1)) * pow((value-min_val)/(max_val-min_val), q));
if (index > (num_bins-1)) index = num_bins - 1;
bin[index].count += count;
bin[index].total += (value * (double)count);
}
return true;
} // end Histogram::Tally()
void Histogram::Print(FILE* file, bool showAll)
{
if (bin)
{
for (unsigned int i = 0; i < num_bins; i++)
{
if ((0 != bin[i].count) || showAll)
{
fprintf(file, "%f, %lu\n", GetBinValue(i), bin[i].count);
}
}
}
} // end Histogram::Print()
unsigned long Histogram::Count()
{
if (bin)
{
unsigned long total = 0 ;
for (unsigned int i = 0; i < num_bins; i++)
{
total += bin[i].count;
}
return total;
}
else
{
return 0;
}
} // end Histogram::Count()
double Histogram::PercentageInRange(double rangeMin, double rangeMax)
{
if (bin)
{
unsigned long countTotal = 0;
unsigned long rangeTotal = 0;
for (unsigned long i = 0; i < num_bins; i++)
{
if (bin[i].count)
{
double value = bin[i].total / ((double)bin[i].count);
countTotal += bin[i].count;
if (value < rangeMin)
continue;
else if (value > rangeMax)
continue;
else
rangeTotal += bin[i].count;
}
}
return (100.0 * ((double)rangeTotal) / ((double)countTotal));
}
else
{
return 0.0;
}
} // end Histogram::PercentageInRange()
unsigned long Histogram::CountInRange(double rangeMin, double rangeMax)
{
if (bin)
{
unsigned long rangeTotal = 0;
for (unsigned long i = 0; i < num_bins; i++)
{
if (bin[i].count)
{
double value = bin[i].total / ((double)bin[i].count);
if (value < rangeMin)
continue;
else if (value > rangeMax)
break;
else
rangeTotal += bin[i].count;
}
}
return rangeTotal;
}
else
{
return 0;
}
} // end Histogram::CountInRange()
double Histogram::Percentile(double p)
{
unsigned long goal = Count();
goal = (unsigned long)(((double)goal) * p + 0.5);
unsigned long count = 0;
if (bin)
{
for (unsigned long i = 0; i < num_bins; i++)
{
count += bin[i].count;
if (count >= goal)
{
double x = pow(((double)i) / ((double)num_bins-1), 1.0/q);
x *= (max_val - min_val);
x += min_val;
return x;
}
}
}
return max_val;
} // end Histogram::Percentile()
int main(int argc, char* argv[])
{
unsigned int nbins = 1000;
bool doNormalize = false;
bool getPercentage = false;
bool getCount = false;
double rangeMin = 0.0;
double rangeMax = 0.0;
bool getPercentile = false;
double pc = 0.0;
double q = 0.5; // default linearity
bool presetRange = false;
double presetRangeMin = 0.0;
double presetRangeMax = 0.0;
// Process command line options
int i = 1;
while(i < argc)
{
int len = strlen(argv[i]);
if (!strncmp(argv[i], "normalize", len))
{
doNormalize = true;
i++;
}
else if (!strncmp(argv[i], "percent", len))
{
getPercentage = true;
i++;
}
else if (!strncmp(argv[i], "bins", len))
{
if (++i >= argc)
{
fprintf(stderr, "hcat: missing \"bins\" args!\n");
usage();
exit(-1);
}
if (1 != sscanf(argv[i], "%u", &nbins))
{
fprintf(stderr, "hcat: invalid bins <numBins>!\n");
usage();
exit(-1);
}
i++;
}
else if (!strncmp(argv[i], "linear", len))
{
if (++i >= argc)
{
fprintf(stderr, "hcat: missing \"linear\" args!\n");
usage();
exit(-1);
}
if (1 != sscanf(argv[i], "%lf", &q))
{
fprintf(stderr, "hcat: invalid linear <q>!\n");
usage();
exit(-1);
}
i++;
}
else if (!strncmp(argv[i], "count", len))
{
getCount = true;
i++;
}
else if (!strncmp(argv[i], "pc", len))
{
getPercentile = true;
if (++i >= argc)
{
fprintf(stderr, "hcat: missing \"pc\" args!\n");
usage();
exit(-1);
}
if (1 != sscanf(argv[i], "%lf", &pc))
{
fprintf(stderr, "hcat: invalid <percentile>!\n");
usage();
exit(-1);
}
pc /= 100.0;
i++;
}
else if (!strncmp(argv[i], "range", len))
{
if (++i >= argc)
{
fprintf(stderr, "hcat: missing \"range\" args!\n");
usage();
exit(-1);
}
char* ptr = strchr(argv[i], ':');
if (ptr)
{
if (2 != sscanf(argv[i], "%lf:%lf", &rangeMin, &rangeMax))
{
fprintf(stderr, "hcat: invalid <range>!\n");
usage();
exit(-1);
}
}
else
{
rangeMin = 0.0;
if (1 != sscanf(argv[i], "%lf", &rangeMax))
{
fprintf(stderr, "hcat: invalid <range>!\n");
usage();
exit(-1);
}
}
i++;
}
else if (!strncmp(argv[i], "prange", len))
{
presetRange = true;
if (++i >= argc)
{
fprintf(stderr, "hcat: missing \"prange\" args!\n");
usage();
exit(-1);
}
char* ptr = strchr(argv[i], ':');
if (ptr)
{
if (2 != sscanf(argv[i], "%lf:%lf", &presetRangeMin, &presetRangeMax))
{
fprintf(stderr, "hcat: invalid <presetRange>!\n");
usage();
exit(-1);
}
}
else
{
presetRangeMin = 0.0;
if (1 != sscanf(argv[i], "%lf", &presetRangeMax))
{
fprintf(stderr, "hcat: invalid <presetRange>!\n");
usage();
exit(-1);
}
}
i++;
}
else
{
// Must be first of input file names
break;
}
}
if (i >= argc)
{
fprintf(stderr, "hcat: no <files> given!\n");
usage();
exit(-1);
}
Histogram h;
h.Init(nbins, q); // 1000 point, q-linear histogram
if (presetRange)
{
if (!h.InitBins(presetRangeMin, presetRangeMax))
{
fprintf(stderr, "hcat: error presetting range!\n");
usage();
exit(-1);
}
}
bool firstBin = true;
double minimum = 0.0;
double mean = 0.0;
int meanCount = 0;
for (; i < argc; i++)
{
FILE* file = fopen(argv[i], "r");
if (!file)
{
perror("hcat: Error opening input file");
usage();
exit(-1);
}
FastReader reader;
char buffer[MAX_LINE];
unsigned int len = MAX_LINE;
while (FastReader::OK == reader.Readline(file, buffer, &len))
{
// Skip blank and commented (leading `#` lines)
if ((0 == len) || ('#' == buffer[0]))
{
len = MAX_LINE;
continue;
}
len = MAX_LINE;
double value;
unsigned long count;
int result = sscanf(buffer, "%lf, %lu", &value, &count);
if (1 == result)
{
count = 1; // assume single values
}
else if (2 != sscanf(buffer, "%lf, %lu", &value, &count))
{
fprintf(stderr, "hcat: Warning! Bad histogram line in file: %s\n", argv[i]);
continue;
}
if (doNormalize)
{
if (firstBin)
{
minimum = value;
firstBin = false;
value = 0.0;
}
else
{
value -= minimum;
}
}
if (!h.Tally(value, count))
{
fprintf(stderr, "hcat: Error adding tallying data point!\n");
exit(-1);
}
mean += count * value;
meanCount += count;
} // end while(reader.Readline())
fclose(file);
firstBin = true;
} // end for(i=1..argc)
mean /= meanCount;
if (h.IsEmpty())
{
fprintf(stderr, "hcat: Warning! Empty histogram.\n");
exit(0); // nothing to output
}
if (getPercentage)
{
double percent = h.PercentageInRange(rangeMin, rangeMax);
fprintf(stdout, "%lf\n", percent);
}
else if (getCount)
{
unsigned long count = h.CountInRange(rangeMin, rangeMax);
fprintf(stdout, "%lu\n", count);
}
else if (getPercentile)
{
double percentile = h.Percentile(pc);
fprintf(stdout, "%lf\n", percentile);
}
else
{
// Default output
// Output new combined histogram w/ percentile info
const double p[6] = {0.99, 0.95, 0.9, 0.8, 0.75, 0.5};
fprintf(stdout, "#histogram: ");
fprintf(stdout, "min:%f max:%f mean:%lf percentiles: ", h.Min(), h.Max(), mean);
for (int j = 0; j < 6; j++)
{
double percentile = h.Percentile(p[j]);
fprintf(stdout, "%2d>%f ", (int)(p[j]*100.0+0.5), percentile);
}
fprintf(stdout, "\n");
h.Print(stdout, presetRange);
}
return 0;
} // end main()
FastReader::FastReader()
: savecount(0)
{
}
FastReader::Result FastReader::Read(FILE* filePtr,
char* buffer,
unsigned int* len,
double timeout)
{
unsigned int want = *len;
if (savecount)
{
unsigned int ncopy = MIN(want, savecount);
memcpy(buffer, saveptr, ncopy);
savecount -= ncopy;
saveptr += ncopy;
buffer += ncopy;
want -= ncopy;
}
while (want)
{
unsigned int result;
#ifndef WIN32 // no real-time TRPR for WIN32 yet
if (timeout >= 0.0)
{
int fd = fileno(filePtr);
fd_set input;
FD_ZERO(&input);
struct timeval t;
t.tv_sec = (unsigned long)timeout;
t.tv_usec = (unsigned long)((1.0e+06 * (timeout - (double)t.tv_sec)) + 0.5);
FD_SET(fd, &input);
int status = select(fd+1, &input, NULL, NULL, &t);
switch(status)
{
case -1:
if (EINTR != errno)
{
perror("trpr: FastReader::Read() select() error");
return ERROR_;
}
else
{
continue;
}
break;
case 0:
return TIMEOUT;
default:
result = fread(savebuf, sizeof(char), 1, filePtr);
break;
}
}
else
#endif // !WIN32
{
// Perform buffered read when there is no "timeout"
result = fread(savebuf, sizeof(char), BUFSIZE, filePtr);
}
if (result)
{
// This check skips NULLs that have been read on some
// use of trpr via tail from an NFS mounted file
if (!isprint(*savebuf) &&
('\t' != *savebuf) &&
('\n' != *savebuf) &&
('\r' != *savebuf))
continue;
unsigned int ncopy= MIN(want, result);
memcpy(buffer, savebuf, ncopy);
savecount = result - ncopy;
saveptr = savebuf + ncopy;
buffer += ncopy;
want -= ncopy;
}
else // end-of-file
{
#ifndef WIN32
if (ferror(filePtr))
{
if (EINTR == errno) continue;
}
#endif // !WIN32
*len -= want;
if (*len)
return OK; // we read at least something
else
return DONE; // we read nothing
}
} // end while(want)
return OK;
} // end FastReader::Read()
// An OK text readline() routine (reads what will fit into buffer incl. NULL termination)
// if *len is unchanged on return, it means the line is bigger than the buffer and
// requires multiple reads
FastReader::Result FastReader::Readline(FILE* filePtr,
char* buffer,
unsigned int* len,
double timeout)
{
unsigned int count = 0;
unsigned int length = *len;
char* ptr = buffer;
while (count < length)
{
unsigned int one = 1;
switch (Read(filePtr, ptr, &one, timeout))
{
case OK:
if (('\n' == *ptr) || ('\r' == *ptr))
{
*ptr = '\0';
*len = count;
return OK;
}
count++;
ptr++;
break;
case TIMEOUT:
// On timeout, save any partial line collected
if (count)
{
savecount = MIN(count, BUFSIZE);
if (count < BUFSIZE)
{
memcpy(savebuf, buffer, count);
savecount = count;
saveptr = savebuf;
*len = 0;
}
else
{
memcpy(savebuf, buffer+count-BUFSIZE, BUFSIZE);
savecount = BUFSIZE;
saveptr = savebuf;
*len = count - BUFSIZE;
}
}
return TIMEOUT;
case ERROR_:
return ERROR_;
case DONE:
return DONE;
}
}
// We've filled up the buffer provided with no end-of-line
return ERROR_;
} // end FastReader::Readline()