forked from privacore/open-source-search-engine
/
Statsdb.cpp
1623 lines (1354 loc) · 44.4 KB
/
Statsdb.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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <errno.h>
#include <math.h>
#include "Conf.h"
#include "PingServer.h"
#include "gb-include.h"
#include "Statsdb.h"
#include "hash.h"
#include "Parms.h"
#include "Process.h"
#include "max_niceness.h"
Statsdb g_statsdb;
static void flushStatsWrapper ( int fd , void *state ) ;
class Label {
public:
int32_t m_graphType;
// . SCALED unit max!
// . -1 indicates to auto determine the maximum y value for time window
float m_absYMax;
// for hashing or printing in the map key
const char *m_label;
// . min resolution per pixel in the graph
// . pixely1 - pixely2 >= m_minRes
// . compared AFTER applying m_yscalar
float m_minRes;
// for printing the y tick mark labels using sprintf:
const char *m_format;
// for scaling the y values when printing tick marks
float m_yscalar;
// use this color
int32_t m_color;
const char *m_keyDesc;
// graph hash
uint32_t m_labelHash;
uint32_t m_graphHash;
};
#define GRAPH_OPS 0
#define GRAPH_QUANTITY 1
#define GRAPH_LATENCY 2
#define GRAPH_RATE 3
#define GRAPH_QUANTITY_PER_OP 4
static Label s_labels[] = {
// . 30 is the max qps regardless to stop graph shrinkage
// . use .03 qps as the min resolution per pixel
// . changed 30 to 150 and .005 to .020
// . back down to 30 since scale was too high before i think for most
// ppl. really it should be autoscaled.
{ GRAPH_OPS,30,"query",.025,"%.1f qps",1.0,0x753d30,"queries per sec"},
// . 10000 ms is the max latency to stop graph shrinkage
// . use 1 ms as the min resolution per pixel
// . use scalar of 1000 since stored in seconds, and we want ms
{ GRAPH_LATENCY,5000,"query" , 1,"%.0f ms", 1000.0, 0x00b58869,"query latency"},
// . quantity of query stats is the number of query terms
// . 10000 ms is the max latency to stop graph shrinkage
// . use 1 ms as the min resolution per pixel
// . use scalar of 1000 since stored in seconds, and we want ms
{ GRAPH_QUANTITY_PER_OP,10,"query" , .001,"%.0f terms", 1.0, 0x00ffffff,"terms per query"},
// . 300MB/s is max read rate regardless to stop graph shrinkage
// . use 1KB as the min resolution per pixel
// . stored in Bps so use 1/1000 as scalar to get into KBps
{ GRAPH_QUANTITY,200,"disk_read",1,"%.0f MBps",1.0/(1000.0*1000.0),0x000000,"disk read"},
// . 300MB/s is max write rate regardless to stop graph shrinkage
// . use 1KB as the min resolution per pixel
// . stored in Bps so use 1/1000 as scalar to get into KBps
{GRAPH_QUANTITY,200,"disk_write",1,"%.0f Mbps",1.0/(1000.0*1000.0), 0xff0000, "disk write"},
// . 20 is the max dps regardless to stop graph shrinkage
// . use .03 qps as the min resolution per pixel
{GRAPH_OPS,20,"parse_doc", .005,"%.1f dps" , 1.0 , 0x00fea915,"parsed doc" },
{GRAPH_QUANTITY_PER_OP,-1,"docs_per_second", .1,"%.1f docs per second" , -1 , 0x1F2F5C,"*successfully* indexed docs per second" },
// . use .1 * 1000 docs as the min resolution per pixel
// . max = -1, means dynamic size the ymax!
// . use 1B for now again...
// . color=pink
// . make it 2M now not 50M. seems like it is per pixel and theres
// like 1000 pixels vertically. but we need to autoscale it
// eventually
{GRAPH_QUANTITY,-1,"docs_indexed", .1,"%.0f docs" , -1, 0x00cc0099,"docs indexed" }
};
void drawLine3 ( SafeBuf &sb ,
int32_t x1 ,
int32_t x2 ,
int32_t fy1 ,
int32_t color ,
int32_t width ) ;
Label *Statsdb::getLabel ( int32_t labelHash ) {
Label **label = (Label **)m_labelTable.getValue ( &labelHash );
if ( ! label ) return NULL;
return *label;
}
Statsdb::Statsdb ( ) {
m_init = false;
m_disabled = true;
}
bool Statsdb::init ( ) {
// 20 pixel borders
m_bx = 10;
m_by = 40;
// keep it at least at 20MB otherwise it is filling up the tree
// constantly and dumping
int32_t maxTreeMem = g_conf.m_statsdbMaxTreeMem;
if ( maxTreeMem < 10000000 ) maxTreeMem = 10000000;
int32_t nodeSize = sizeof(StatData)+8+12+4 + sizeof(collnum_t);
// for debug
//nodeSize = 50000;
// . We take a snapshot of g_stats every minute.
// . Each sample struct taken from g_stats ranges from 1k - 2k
// after compression depending on the state of the
// all errors arrays.
uint32_t maxTreeNodes = maxTreeMem / nodeSize;
uint32_t maxCacheNodes = g_conf.m_statsdbMaxCacheMem / nodeSize;
// assume low niceness
m_niceness = 0;
// init the label table
static char s_buf[832];
if ( ! m_labelTable.set(4,sizeof(Label *),64,
s_buf,832,false,0,"statcolors") )
return false;
// stock the table
int32_t n = (int32_t)sizeof(s_labels)/ sizeof(Label);
for ( int32_t i = 0 ; i < n ; i++ ) {
Label *bb = &s_labels[i];
// hash the label
bb->m_labelHash = hash32n ( bb->m_label );
// then incorporate the bool parm
bb->m_graphHash = hash32h( bb->m_labelHash , bb->m_graphType );
// add it to labeltable... why???
if ( ! m_labelTable.addKey (&bb->m_graphHash,&bb ) ) {
g_process.shutdownAbort(true); }
}
// sanity test
//Stat ts;
//ts.setKey ( 0x123456789LL , 0x7654321 );
//if ( ts.getTime1() != 0x123456789LL ) { g_process.shutdownAbort(true); }
//if ( ts.getLabelHash() != 0x7654321 ) { g_process.shutdownAbort(true); }
//ts.setKey ( 1268261684329LL , -246356284 );
//if ( ts.getTime1() != 1268261684329LL ) { g_process.shutdownAbort(true); }
//if ( ts.getLabelHash() != -246356284 ) { g_process.shutdownAbort(true); }
// call this twice per second
if ( ! g_loop.registerSleepCallback(500,NULL,flushStatsWrapper))
return log("statsdb: Failed to initialize timer callback2.");
m_init = true;
// make the rec cache 0 bytes, cuz we are just using page cache now
if ( ! m_rdb.init ( g_hostdb.m_dir , // working directory
"statsdb" , // dbname
true , // dedup keys
sizeof(StatData) , // fixed record size
200,//g_conf.m_statsdbMinFilesToMerge ,
maxTreeMem ,
maxTreeNodes ,
true , // balance tree?
0 , // m_statsdbMaxCchMem
maxCacheNodes ,
false , // use half keys?
false , // cache from disk?
NULL , // page cache pointer
false , // is titledb?
false ,
sizeof(key96_t) , // key size
false, // bias disk page cache?
true ) ) // is collectionless?
return false;
m_disabled = false;
// normally Collectiondb.addColl() will call Rdb::addColl() which
// will init the CollectionRec::m_rdbBase, which is what
// Rdb::getBase(collnum_t) will return. however, for collectionless
// rdb databases we set Rdb::m_collectionlessBase special here.
return m_rdb.addRdbBase1 ( NULL );
}
// Make sure we need this function.
// main.cpp currently uses the addColl from m_rdb
//bool Statsdb::addColl ( char *coll, bool doVerify ) {
// if ( ! m_rdb.addColl ( coll ) ) return false;
// return true;
//}
void flushStatsWrapper ( int fd , void *state ) {
g_statsdb.addDocsIndexed();
// force a statsdb tree dump if running out of room
Rdb *rdb = &g_statsdb.m_rdb;
RdbTree *tree = &rdb->m_tree;
// if we got 20% room left and 50k available mem, do not dump
if ( (float)tree->getNumUsedNodes() * 1.2 <
(float)tree->getNumAvailNodes () &&
//tree->getNumAvailNodes () > 1000 &&
rdb-> m_mem.getAvailMem() > 50000 )
return;
if ( ! isClockInSync() ) return;
// force a dump
rdb->dumpTree ( 1 );
}
void Statsdb::addDocsIndexed ( ) {
if ( ! isClockInSync() ) return;
if ( g_hostdb.hasDeadHost() ) return;
// only host #0 needs this
if ( g_hostdb.m_hostId != 0 ) return;
// only once per five seconds
int32_t now = getTimeLocal();
static int32_t s_lastTime = 0;
if ( now - s_lastTime < 5 ) return;
int32_t interval = now - s_lastTime;
s_lastTime = now;
int64_t total = 0LL;
static int64_t s_lastTotal = 0LL;
// every 5 seconds update docs indexed count
for ( int32_t i = 0 ; i < g_hostdb.m_numHosts ; i++ ) {
Host *h = &g_hostdb.m_hosts[i];
// must have something
if ( h->m_pingInfo.m_totalDocsIndexed <= 0 ) continue;
// add it up
total += h->m_pingInfo.m_totalDocsIndexed;
}
// divide by # of groups
total /= g_hostdb.getNumHostsPerShard();
// skip if no change
if ( total == s_lastTotal ) return;
int32_t docsIndexedInInterval = total - s_lastTotal;
float docsPerSecond = docsIndexedInInterval / (float)interval;
log("build: total docs indexed: %f. docs per second %f %i %i", (float)total, docsPerSecond, docsIndexedInInterval, interval);
// add it if changed though
int64_t nowms = gettimeofdayInMillisecondsGlobal();
addStat ( MAX_NICENESS,"docs_indexed", nowms, nowms, (float)total );
// Prevent a datapoint which adds all of the docs indexed to date.
if( s_lastTotal != 0 ) {
addStat ( MAX_NICENESS,"docs_per_second", nowms, nowms, docsPerSecond );
}
s_lastTotal = total;
}
// . m_key bitmap in statsdb:
// tttttttt tttttttt tttttttt tttttttt t = time in milliseconds, t1
// tttttttt tttttttt tttttttt tttttttt
// hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh h = hash32 of m_title
// . returns false if could not add stat, true otherwise
// . do not set g_errno if we return false just to keep things simple
// . we only add the stat to our local statsdb rdb, but because
// we might be dumping statsdb to disk or something it is possible
// we get an ETRYAGAIN error, so we try to accumulate stats in a
// local buffer in that case
// . "label" is something like "queryLatency" or whatever
// . [t1,t2] are the time endpoints for the operation being measured
// . "value" is usually "numBytes", or a quantity indicator of whatever
// was processed.
// . oldVal, newVal are reflect a state change, like maybe changing the
// value of a parm. typically for such things t1 equals t2
bool Statsdb::addStat ( int32_t niceness ,
const char *label ,
int64_t t1Arg ,
int64_t t2Arg ,
float value , // y-value really, "numBytes"
int32_t parmHash ,
float oldVal ,
float newVal ,
int32_t userId32 ) {
if ( ! g_conf.m_useStatsdb ) return true;
// so Process.cpp can turn it off when dumping core
if ( m_disabled ) return true;
// not thread safe!
//if ( g_threads.amThread() ) {
// log("statsdb: called from thread");
// g_process.shutdownAbort(true);
//}
// . for now we can only add stats if we are synced with host #0 clock
// . this is kinda a hack and it would be nice to not miss stats!
if ( ! isClockInSync() ) return true;
RdbTree *tree = &m_rdb.m_tree;
// do not add stats to our tree if it is loading
if ( tree->m_isLoading ) return true;
// convert into host #0 synced time
t1Arg = localToGlobalTimeMilliseconds ( t1Arg );
t2Arg = localToGlobalTimeMilliseconds ( t2Arg );
// sanity check
if ( ! label ) { g_process.shutdownAbort(true); }
int32_t labelHash;
if ( parmHash ) labelHash = parmHash;
else labelHash = hash32n ( label );
// fix it for parm changes, and docs_indexed stat, etc.
if ( t1Arg == t2Arg ) t2Arg++;
// how many SECONDS did the op take? (convert from ms to secs)
float dtms = (t2Arg - t1Arg);
float dtSecs = dtms / 1000.0;
// we have already flushed stats 30+ seconds old, so if this op took
// 30 seconds, discard it!
if ( dtSecs >= 30 ) {
//log("statsdb: stat is %" PRId32" secs > 30 secs old, discarding.",
// (int32_t)dtSecs);
return true;
}
int64_t nextup;
// loop over all "second" buckets
for ( int64_t tx = t1Arg ; tx < t2Arg ; tx = nextup ) {
// get next second-aligned point in milliseconds
nextup = ((tx +1000)/ 1000) * 1000;
// truncate if we need to
if ( nextup > t2Arg ) nextup = t2Arg;
// . how much of the stat is in this time interval?
// . like if operation took 3 seconds, we might cover
// 50% of the first 1-second interval. so we use this
// as a weight for the stats we keep for that particular
// second. then we can plot a point for each second
// in time which is an average of all the queries that
// were in progress at that second.
float fractionTime = ((float)(nextup - tx)) / dtms;
// . get the time point bucket in which this stat belongs
// . every "second" in time has a bucket
uint32_t t1 = tx / 1000;
StatKey sk;
memset(&sk,0,sizeof(sk));
sk.m_zero = 0x01; // make it a positive key
sk.m_time1 = t1;
sk.m_labelHash = labelHash;
// so we can show just the stats for a particular user...
if ( userId32 ) {
sk.m_zero = userId32;
// make it positive
sk.m_zero |= 0x01;
}
// if we already have added a bucket for this "second" then
// get it from the tree so we can add to its accumulated stats.
int32_t node1 = tree->getNode ( 0 , (char *)&sk );
int32_t node2;
StatData *sd;
// get that stat, see if we are accumulating it already
if ( node1 >= 0 )
sd = (StatData *)tree->getData ( node1 );
// make a new one if not there
else {
StatData tmp;
// init it
memset(&tmp,0,sizeof(tmp));
tmp.m_totalOps = 0.0;
tmp.m_totalQuantity = 0.0;
tmp.m_totalTime = 0.0;
// save this
int32_t saved = g_errno;
// need to add using rdb so it can gbmemcpy the data
if ( ! m_rdb.addRecord ( (collnum_t)0 ,
(char *)&sk,
(char *)&tmp,
sizeof(StatData),
niceness ) ) {
if ( g_errno != ETRYAGAIN )
log("statsdb: add rec failed: %s",
mstrerror(g_errno));
// caller does not care about g_errno
g_errno = saved;
return false;
}
// caller does not care about g_errno
g_errno = saved;
// get the node in the tree
//sd = (StatData *)tree->getData ( node1 );
// must be there!
node2 = tree->getNode ( 0 , (char *)&sk );
// must be there!
if ( node2 < 0 ) { g_process.shutdownAbort(true); }
// point to it
sd = (StatData *)tree->getData ( node2 );
}
// use the milliseconds elapsed as the value if none given
//if ( value == 0 && ! parmHash )
// value = t2Arg - t1Arg;
// if we got it for this time, accumulate it
// convert x into pixel position
sd->m_totalOps += 1 * fractionTime;
sd->m_totalQuantity += value * fractionTime;
sd->m_totalTime += dtSecs * fractionTime;
if ( ! parmHash ) continue;
sd->m_totalOps = 0;
sd->m_totalQuantity = oldVal;
sd->m_newVal = newVal;
// no fractions for this!
break;
}
//logf(LOG_DEBUG,"statsdb: sp=0x%" PRIx32,(int32_t)sp);
return true;
}
#define MAXSAMPLES 1000
// . returns false if blocked, true otherwise
// . returns true and sets g_errno on error
bool Statsdb::makeGIF ( int32_t t1Arg ,
int32_t t2Arg ,
int32_t samples ,
SafeBuf *sb2 ,
void *state ,
void (* callback) (void *state) ,
int32_t userId32 ) {
if ( t1Arg >= t2Arg ) {
g_errno = EBADENGINEER;
return true;
}
if ( t1Arg < 0 ) { g_process.shutdownAbort(true); }
if ( t2Arg < 0 ) { g_process.shutdownAbort(true); }
// # of samples in moving average
m_samples = samples;
if ( m_samples > MAXSAMPLES ) m_samples = MAXSAMPLES;
if ( m_samples < 1 ) m_samples = 1;
m_state = state;
m_callback = callback;
m_t1 = t1Arg;//(int64_t)t1Arg * 1000LL;
m_t2 = t2Arg;//(int64_t)t2Arg * 1000LL;
if ( m_t1 >= m_t2 ) { g_process.shutdownAbort(true); }
m_ht0.reset();
m_sb0.reset();
m_sb1.reset();
m_sb2 = sb2;
m_sb2->reset();
m_sb3.reset();
m_ht3.reset();
// print graph in here as a bunch of divs now:
m_gw.purge();
m_dupTable.reset();
m_dupTable.set(4,0,20000,NULL,0,false,0,"statstbl");
// . start at t1 and get stats lists, up to 1MB of stats at a time
// . subtract 60 seconds so we can have a better shot at having
// a moving average for the last SAMPLE points
m_startKey.n1 = m_t1 - 60;
if ( m_startKey.n1 < 0 ) m_startKey.n1 = 0;
m_startKey.n0 = 0x0000000000000000LL;
m_endKey.n1 = m_t2;
m_endKey.n0 = 0xffffffffffffffffLL;
m_done = false;
if ( ! m_ht0.set ( 4 , 4,256,NULL,0,true,m_niceness,"statht0") )
return true;
if ( ! m_ht3.set ( 4 , 4,256,NULL,0,true,m_niceness,"statht3") )
return true;
// open the file for the gif
/*
char fname [ 1024 ];
sprintf ( fname , "%s/stats%" PRId32".gif" ,
g_hostdb.m_httpRootDir , g_hostdb.m_hostId );
m_fd = fopen ( fname,"w" );
if ( ! m_fd ) {
log("admin: Statsdb::makeGIF: fopen ( %s , \"w\" ) : %s",
fname , mstrerror(errno) );
return true;
}
*/
return gifLoop ();
}
#define POINTWIDTH 8
#define DY2 400 // pixels vertical
#define DX2 1000 // pixels across
#define MAX_LINES2 (DY2 / (MAX_WIDTH+1)) // leave free pixel above each line
int32_t Statsdb::getImgHeight() {
return (int32_t)DY2 + m_by * 2;
}
int32_t Statsdb::getImgWidth() {
return (int32_t)DX2 + m_bx * 2;
}
// these are used for storing the "events"
class EventPoint {
public:
// m_a and m_b are in pixel space
int32_t m_a;
int32_t m_b;
int32_t m_parmHash;
float m_oldVal;
float m_newVal;
//int32_t m_colorRGB;
int32_t m_thickness;
};
static void gotListWrapper ( void *state , RdbList *list, Msg5 *msg5 ) ;
// returns false if blocked, true otherwise
bool Statsdb::gifLoop ( ) {
// shortcut
Msg5 *m = &m_msg5;
//#ifndef _USEPLOTTER_
//return true;
//#endif
// loop over all the lists in the time range, [m_t1,m_t2]
for ( ; ! m_done ; ) {
if ( ! m->getList ( (char)RDB_STATSDB ,
(collnum_t)0,//"statsdb" , // coll
&m_list ,
(char *)&m_startKey ,
(char *)&m_endKey ,
32000 , // requested scan size
true , // include tree?
false , // add to cache?
0 , // max cache age
0 , // start file number
-1 , // number of files
NULL , // state
gotListWrapper, // callback
m_niceness , // niceness
false , // do error correction?
NULL , // cache key pointer
0 , // # retries
-1 , // max # retries
true , // compensate for merge?
-1 ) ) // sync point
return false;
// . process list
// . returns false with g_errno set on error
if ( ! processList() ) return true;
}
// define time delta - commented out because it's currently not used.
int32_t dt = m_t2 - m_t1;
//#ifdef _USEPLOTTER_
// gif size
//char tmp[64];
// dimensions of the gif
//sprintf ( tmp , "%" PRId32"x%" PRId32, (int32_t)DX2+m_bx*2 , (int32_t)DY2+m_by*2 );
//GIFPlotter::parampl ( "BITMAPSIZE" , (void *)tmp );
// create one
//GIFPlotter plotter ( NULL , m_fd , NULL );
// open it
//plotter.openpl ( );
// define the space with boundaries 100 unit wide boundaries
//plotter.space ( 0 , 0 , DX2 + m_bx * 2 , DY2 + m_by * 2 );
// line thickness in user coordinates (pixels for us)
//plotter.linewidth ( 1 );
// set bg color to gray (r/g/b)
//plotter.bgcolor ( 0xd600 , 0xce00 , 0xd600 );
// erase Plotter's graphics display
//plotter.erase ();
// draw axises in black
//plotter.pencolorname ("black");
//
// main graphing window
//
m_gw.safePrintf("<div style=\"position:relative;"
"background-color:#c0c0c0;"
//"overflow-y:hidden;"
"overflow-x:hidden;"
"z-index:0;"
// the tick marks we print below are based on it
// being a window of the last 20 seconds... and using
// DX2 pixels
"min-width:%" PRId32"px;"
"min-height:%" PRId32"px;"
//"width:100%%;"
//"min-height:600px;"
"margin-top:10px;"
"margin-bottom:10px;"
"margin-right:10px;"
"margin-left:10px;\">"
,(int32_t)DX2 + 2 *m_bx
,(int32_t)DY2 + 2*m_by);
// draw the x-axis
//plotter.line ( m_bx , m_by , DX2 + m_bx , m_by );
// 10 x-axis tick marks
for ( int x = DX2/20 ; x <= DX2 ; x += DX2/20 ) {
// tick mark
//plotter.line ( x , -20 , x , 20 );
m_gw.safePrintf("<div style=\"position:absolute;"
"left:%" PRId32";"
"bottom:0;"
"background-color:black;"
"z-index:10;"
"min-height:20px;"
"min-width:3px;\"></div>\n"
, m_bx + (int32_t)x-1
);
int32_t xv = (int32_t)(dt * (int64_t)x/(int64_t)DX2)-(int32_t)dt;
// LABEL
m_gw.safePrintf("<div style=\"position:absolute;"
"left:%" PRId32";"
"bottom:22;"
//"background-color:#000000;"
"z-index:10;"
"height:20px;"
"width:3px;"
"color:black;"
"font-size:10px;"
"min-height:20px;"
"min-width:3px;\">%" PRId32"s</div>\n"
, (int32_t)x-15 + m_bx
// the label:
, xv
);
}
HashTableX tmpht;
tmpht.set(4,0,0,NULL,0,false,m_niceness,"statsparms");
int32_t col = 0;
m_sb2->safePrintf("<table border=1 width=100%%>\n");
// label offset to prevent collisions of superimposing multiple
// graph calbrations
int32_t zoff = 0;
//
// point to the triplets in m_sb1's buffer (x,y,c)
//
char *p = m_sb1.getBufStart();
char *pend = p + m_sb1.length();
for ( ; p < pend ; p += 12 ) {
// breathe
QUICKPOLL ( m_niceness );
// get graph hash of this point
int32_t gh = *(int32_t *)(p +8);
// if we already did this graph, skip it
if ( tmpht.isInTable ( &gh ) ) continue;
// . graph this single graph of this color
// . returns ptr to first point of different color!
plotGraph ( p , pend , gh , m_gw , zoff );
// prevent collisions
zoff += 20;
// get the label based on graphHash
Label *bb = getLabel ( gh );
// add to key
if ( col == 0 )
m_sb2->safePrintf("<tr>");
m_sb2->safePrintf("<td bgcolor=#%06" PRIx32"><label>"
"<input class=\"graph-toggles\" "
"type=\"checkbox\" "
"value=\"%06" PRIx32"\"/></label></td>"
"<td>%s</td>\n",
bb->m_color ,
bb->m_color ,
bb->m_keyDesc );
if ( col == 1 )
m_sb2->safePrintf("</tr>\n");
// inc column and wrap
if ( ++col >= 2 ) col = 0;
// . do not re-display
// . TODO: deal with error
tmpht.addKey ( &gh );
}
// clear that up
m_sb1.reset();
// now plot the events, horizontal line segments like the performance
// graph uses
for ( int32_t i = 0 ; i < m_ht3.m_numSlots ; i++ ) {
// breathe
QUICKPOLL ( m_niceness );
// skip if slot empty
if ( ! m_ht3.m_flags[i] ) continue;
// get the offset into m_sb3
int32_t offset = *(int32_t *)m_ht3.getValueFromSlot(i);
// get buf start
char *bufStart = m_sb3.getBufStart();
// get the ptr
EventPoint *pp = (EventPoint *)(bufStart + offset);
// get name of parm
Parm *m = g_parms.getParmFromParmHash ( pp->m_parmHash );
// make sure we got it
if ( ! m ) {
log("statsdb: unrecognized parm hash = %" PRId32,
pp->m_parmHash);
continue;
//g_process.shutdownAbort(true); }
}
// set the line width
//plotter.linewidth ( pp->m_thickness );
// get parm hash
int32_t colorHash = pp->m_parmHash;
// add in old/new values to make it different
colorHash = hash32h ( (int32_t)pp->m_oldVal , colorHash );
colorHash = hash32h ( (int32_t)pp->m_newVal , colorHash );
// . get color
// . is really the parm hash in disguise
int32_t c1 = colorHash & 0x00ffffff;
// use the color specified from addStat_r() for this line/pt
//plotter.pencolor ( ((c1 >> 16) & 0xff) << 8 ,
// ((c1 >> 8) & 0xff) << 8 ,
// ((c1 >> 0) & 0xff) << 8 );
int32_t x1 = pp->m_a;
int32_t x2 = pp->m_b;
int32_t y1 = *(int32_t *)m_ht3.getKey(i); // i value
// ensure at least 3 units wide for visibility
if ( x2 < x1 + 10 ) x2 = x1 + 10;
// . flip the y so we don't have to scroll the browser down
// . DY2 does not include the axis and tick marks
//int32_t fy1 = DY2 - y1 + m_by ;
// plot it
//plotter.line ( x1 , fy1 , x2 , fy1 );
drawLine3 ( m_gw , x1 , x2 , y1 , c1 , pp->m_thickness );
// add to map key? only if we haven't already
if ( tmpht.isInTable ( &colorHash ) ) continue;
// add it
if ( col == 0 )
m_sb2->safePrintf("<tr>");
const char *title = "unknown parm";
if ( m ) title = m->m_title;
m_sb2->safePrintf("<td bgcolor=#%06" PRIx32"> </td>",c1);
// print the parm name and old/new values
m_sb2->safePrintf("<td><b>%s</b>",title);
if ( pp->m_oldVal != pp->m_newVal )
m_sb2->safePrintf(" (%.02f -> %.02f)",
pp->m_oldVal,pp->m_newVal);
m_sb2->safePrintf("</td>");
if ( col == 1 )
m_sb2->safePrintf("</tr>\n");
// inc column and wrap
if ( ++col >= 2 ) col = 0;
// . do not re-display
// . TODO: deal with error
tmpht.addKey ( &colorHash ) ;
}
m_sb2->safePrintf("</table>\n");
// clear that up
m_ht3.reset();
m_sb3.reset();
// and stat states
m_ht0.reset();
m_sb0.reset();
// all done free some mem
m_sb1.reset();
//m_sb2.reset();
//
// but not m_sb2 cuz that has the html in it!!
//
// all done
//if ( plotter.closepl () < 0 )
// log("admin: Could not close performance graph object.");
// close the file
//fclose ( m_fd );
//#endif
// close main graphing window
m_gw.safePrintf("</div>\n");
return true;
}
char *Statsdb::plotGraph ( char *pstart ,
char *pend ,
int32_t graphHash ,
//GIFPlotter *plotter ,
SafeBuf &gw ,
int32_t zoff ) {
// . use "graphHash" to map to unit display
// . this is a disk read volume
Label *label = getLabel ( graphHash );
if ( ! label ) { g_process.shutdownAbort(true); }
//log("stats: plotting %s",label->m_keyDesc) ;
// let's first scan m_sb1 to normalize the y values
bool needMin = true;
bool needMax = true;
float ymin = 0.0;
float ymax = 0.0;
float yscalar = label->m_yscalar;
char *p = pstart;
for ( ; p < pend ; p += 12 ) {
// breathe
QUICKPOLL ( m_niceness );
if ( m_gw.getLength() > 10000000 ) break;
// get the y
float y2 = *(float *)(p+4);
// get color of this point
int32_t gh = *(int32_t *)(p +8);
// stop if not us
if ( gh != graphHash ) continue;
// put into scaled space right away
if (label->m_yscalar >= 0)
y2 = y2 * label->m_yscalar;
// . limit y to absolute max
// . these units should be scaled as well!
if ( y2 > label->m_absYMax && label->m_absYMax > 0.0 )
y2 = label->m_absYMax;
// get min and max
if ( y2 < ymin || needMin ) ymin = y2;
if ( y2 > ymax || needMax ) ymax = y2;
needMin = false;
needMax = false;
}
// force to zero for now
//ymin = 0.0;
// . and force to ymax for now as well
// . -1 indicates dynamic though!
if ( label->m_absYMax > 0.0 ) ymax = label->m_absYMax;
// add a 20% ceiling
// else ymax *= 1.20;
if( label->m_yscalar <= 0 ) {
if(ymax == ymin) {
yscalar = 0;
} else {
yscalar = (float)DY2 / (ymax - ymin);
}
}
// return that!
char *retp = p;
// set the line width
//plotter->linewidth ( 1 );
int32_t color = label->m_color;
// use the color specified from addStat_r() for this line/pt
//plotter->pencolor ( ((color >> 16) & 0xff) << 8 ,
// ((color >> 8) & 0xff) << 8 ,
// ((color >> 0) & 0xff) << 8 );
// . the minimum difference between ymax and ymin is minDiff.
// . this prevents us from zooming in too close!
float minDiff = (float)DY2 * label->m_minRes ;
// we are already scaled!
float ourDiff = (ymax - ymin) ;
// . pad y range if total range is small
// . only do this for certain types of stats, like qps and disk i/o
if ( label->m_yscalar >=0 && ourDiff < minDiff ) {
float pad = (minDiff - ourDiff) / 2;
// pad it out
ymin -= pad ;
ymax += pad ;
// fix it some
if ( ymin < 0 ) {
ymax += -1*ymin;
ymin = 0;
}
// limit again just in case
if ( ymax > label->m_absYMax && label->m_absYMax > 0.0 )
ymax = label->m_absYMax;
}
// set the line width
//plotter->linewidth ( 2 );
// reset for 2nd scan
p = pstart;
int32_t lastx = -1;
float lasty ;
bool firstPoint = true;
// now the m_sb1 buffer consists of points to make lines with
for ( ; p < pend ; ) {
// breathe
QUICKPOLL ( m_niceness );
if ( m_gw.getLength() > 10000000 ) break;
// first is x pixel pos
int32_t x2 = *(int32_t *)p; p += 4;
// then y pos
float y2 = *(float *)p; p += 4;
// scale it right away
if(label->m_yscalar < 0) {
y2 = (y2 - ymin) * yscalar;
}
else {
y2 *= yscalar;
}
// adjust
if ( y2 > ymax ) y2 = ymax;
if ( y2 < 0 ) y2 = 0;
// then graphHash
int32_t gh = *(int32_t *)p; p += 4;