-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.cpp
1378 lines (1150 loc) · 44.1 KB
/
cache.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 "cache.h"
#include "plotrenderer.h"
#include "requester.h"
#include "utils.h"
#include <cstdint>
#include <functional>
#include <QHash>
#include <QList>
#include <QSharedPointer>
#include <QTimer>
StreamKey::StreamKey(const QUuid& stream_uuid, DataSource* stream_source)
: uuid(stream_uuid), source(stream_source) {}
StreamKey::StreamKey(const StreamKey& other)
: StreamKey(other.uuid, other.source) {}
StreamKey::StreamKey()
: uuid(), source(nullptr) {}
bool StreamKey::operator==(const StreamKey& other) const
{
return this->uuid == other.uuid && this->source == other.source;
}
uint qHash(const StreamKey& sk, uint seed)
{
return qHash(sk.uuid) ^ qHash(reinterpret_cast<uintptr_t>(sk.source)) ^ seed;
}
CostEntry::CostEntry(QSharedPointer<CacheEntry>& cache_ent)
: cache_entry(cache_ent), type(CostType::CACHE_ENTRY) {}
CostEntry::CostEntry(StreamKey& stream_ent)
: stream_entry(stream_ent), type(CostType::STREAM_ENTRY) {}
/* Size is 40 bytes.
*/
struct cachedpt
{
float reltime;
float min;
float prevcount;
float mean;
float flags;
float reltime2;
float max;
float count;
float truecount;
float flags2;
};
/* The overhead cost, in cached points, of the data stored in a
* Cache Entry.
*
* For correctness, this MUST be greater than zero! If it is zero, then we may
* mistakenly believe that there is no data stored for a UUID, when in reality
* there is a placeholder cache entry which is preventing us from freeing that
* entry in the hash table.
*/
#define CACHE_ENTRY_OVERHEAD (sizeof(CacheEntry))
/* The overhead cost, in cached points, of the metadata for a stream. */
#define STREAM_OVERHEAD (sizeof(struct streamcache) + PWE_MAX * sizeof(QMap<int64_t, QSharedPointer<CacheEntry>>))
#define CACHED_POINT_SIZE (sizeof(struct cachedpt))
CacheEntry::CacheEntry(Cache* c, const StreamKey& sk, int64_t startRange, int64_t endRange, uint8_t pwexp) :
start(startRange), end(endRange), streamKey(sk), maincache(c), pwe(pwexp)
{
Q_ASSERT(pwexp < PWE_MAX);
Q_ASSERT(endRange >= startRange);
this->cached = nullptr;
this->cachedlen = 0;
this->vbo = 0;
this->firstpt = nullptr;
this->lastpt = nullptr;
this->joinsPrev = false;
this->joinsNext = false;
this->prepared = false;
this->connectsToBefore = false;
this->connectsToAfter = false;
this->evicted = false;
}
CacheEntry::~CacheEntry()
{
/* If this is a placeholder, there is an outstanding request
* for this data, and we'll need a place to put that data when
* the response comes back. So we can't free this memory just
* yet.
*/
Q_ASSERT(this->cached != nullptr || this->evicted);
delete[] this->cached;
if (this->firstpt != nullptr)
{
delete this->firstpt;
}
if (this->lastpt != nullptr)
{
delete this->lastpt;
}
if (this->vbo != 0)
{
/* Mark the VBO for deletion. */
this->maincache->todelete.push_back(this->vbo);
}
}
#define FLAGS_NONE 0.0f
#define FLAGS_GAP 1.0f
#define FLAGS_ALWAYS_HIDE 0.75f
#define FLAGS_LONEPT -1.0f
/* Pulls the data density graph to zero, and creates a gap in the main plot. */
void pullToZero(struct cachedpt* pt, int64_t time, int64_t epoch, float prevcnt, struct statpt* prev, struct statpt* next)
{
float reltime = (float) (time - epoch);
/* Get the interpolated values. */
uint64_t deltaT = (uint64_t) (next->time - prev->time);
uint64_t dT = (uint64_t) (time - prev->time);
double imin = prev->min + ((next->min - prev->min) / deltaT) * dT;
double imean = prev->mean + ((next->mean - prev->mean) / deltaT) * dT;
double imax = prev->max + ((next->max - prev->max) / deltaT) * dT;
pt->reltime = reltime;
pt->min = (float) imin;
pt->prevcount = prevcnt;
pt->mean = (float) imean;
pt->flags = FLAGS_GAP;
pt->reltime2 = reltime;
pt->max = (float) imax;
pt->count = 0.0f;
pt->truecount = 0.0f;
pt->flags2 = FLAGS_GAP;
}
void pullToZeroNoInterp(struct cachedpt* pt, int64_t time, int64_t epoch, float prevcnt)
{
float reltime = (float) (time - epoch);
pt->reltime = reltime;
pt->min = 0.0f;
pt->prevcount = prevcnt;
pt->mean = 0.0f;
pt->flags = FLAGS_ALWAYS_HIDE;
pt->reltime2 = reltime;
pt->max = 0.0f;
pt->count = 0.0f;
pt->truecount = 0.0f;
pt->flags2 = FLAGS_ALWAYS_HIDE;
}
void fillpt(struct cachedpt* output, struct statpt* input, int64_t epoch, float prevcount, float count, float flags)
{
output->reltime = (float) (input->time - epoch);
output->min = (float) input->min;
output->prevcount = prevcount;
output->mean = (float) input->mean;
output->flags = flags;
output->reltime2 = output->reltime;
output->max = (float) input->max;
output->count = count;
output->truecount = output->count;
output->flags2 = flags;
}
/* SPOINTS should contain all statistical points where the MIDPOINT is
* in the (closed) interval [start, end] of this cache entry.
* If there is a point immediately to the left of and adjacent to the
* first point, or a point immediately to the right of and adjacent to
* the last point in the range, those points should also be included.
*/
void CacheEntry::cacheData(struct statpt* spoints, int len,
QSharedPointer<CacheEntry> prev, QSharedPointer<CacheEntry> next)
{
Q_ASSERT(this->cached == nullptr);
this->cost = ((uint64_t) len) * CACHED_POINT_SIZE;
int64_t pw = Q_INT64_C(1) << this->pwe;
int64_t pwmask = ~(pw - 1);
int64_t halfpw = pw >> 1;
/* True iff first point in spoints belongs to the cache entry previous to this one. */
bool prevfirst = (len > 0 && spoints[0].time == ((this->start - halfpw - 1) & pwmask));
/* True iff the last point in spoints belongs to the cache entry after this one. */
bool nextlast = (len > 0 && spoints[len - 1].time == (((this->end - halfpw + pw) & pwmask)));
/*
* These "connect" variables refer to whether this cache entry
* should take responsibility for connecting to the previous
* cache entry, when drawn with the ALWAYS CONNECT setting.
*/
/* If this is true, then ddstartatzero is true. */
this->connectsToBefore = !prevfirst && prev.data() != nullptr && prev->lastpt != nullptr && prev->end + 1 == this->start;
/* If this is true, then ddendatzero is true. */
this->connectsToAfter = !nextlast && next.data() != nullptr && next->firstpt != nullptr && this->end + 1 == next->start;
if (len == 0)
{
/* Edge case: no data. Just draw 0 data density plot. */
this->epoch = (this->start >> 1) + (this->end >> 1);
if (this->connectsToBefore && this->connectsToAfter)
{
/* Bridge the gap. */
this->cachedlen = 4;
this->cached = new struct cachedpt[this->cachedlen];
fillpt(&this->cached[0], prev->lastpt, this->epoch, 0.0f, 0.0f, FLAGS_GAP);
pullToZero(&this->cached[1], this->start, this->epoch, 0.0f, prev->lastpt, next->firstpt);
pullToZero(&this->cached[2], this->end + 1, this->epoch, 0.0f, prev->lastpt, next->firstpt);
fillpt(&this->cached[3], next->firstpt, this->epoch, 0.0f, 0.0f, FLAGS_GAP);
}
else
{
this->cachedlen = 2;
this->cached = new struct cachedpt[this->cachedlen];
pullToZeroNoInterp(&this->cached[0], this->start, this->epoch, 0.0f);
pullToZeroNoInterp(&this->cached[1], this->end + 1, this->epoch, 0.0f);
if (!this->connectsToBefore && this->connectsToAfter)
{
this->firstpt = new struct statpt;
*this->firstpt = *next->firstpt;
}
if (this->connectsToBefore && !this->connectsToAfter)
{
this->lastpt = new struct statpt;
*this->lastpt = *prev->lastpt;
}
this->connectsToBefore = false;
this->connectsToAfter = false;
}
return;
}
this->joinsPrev = (prev != nullptr && !prev->joinsNext && prev->cachedlen != 0);
this->joinsNext = (next != nullptr && !next->joinsPrev && next->cachedlen != 0);
this->epoch = (spoints[len - 1].time >> 1) + (spoints[0].time >> 1);
/* NUMINPUTS is the number of inputs that we look at in the main iteration over
* the array.
*/
int numinputs = len;
statpt* inputs = spoints;
bool ddstartatzero = false;
bool ddendatzero = false;
if (prevfirst && !this->joinsPrev)
{
/* We have an element that's one past the left of the range we're interested
* in, but we don't have to connect with it because the previous entry takes
* care of it.
*/
numinputs--;
inputs++;
}
else if (!prevfirst)
{
/* There's no point immediately before the first point, so we need to make
* sure that the data density plot starts at the beginning of the cache entry.
*/
ddstartatzero = true;
}
if (nextlast && !this->joinsNext)
{
/* We have an element that's one past the right of the range we're interested
* in, but we don't have to connect with it because the next entry takes care
* of it.
*/
numinputs--;
}
else if (!nextlast)
{
/* There's no point immediately after the last point, so we need to pull the
* data density plot to 0 and then extend it to the end of this cache entry.
*/
ddendatzero = true;
}
if (!this->connectsToBefore)
{
this->firstpt = new struct statpt;
*this->firstpt = spoints[qMin(len - 1, (int) prevfirst)];
}
if (!this->connectsToAfter)
{
this->lastpt = new struct statpt;
*this->lastpt = spoints[qMax(0, len - 1 - nextlast)];
}
/* We can get two distinct bounds on the number of cached points.
* In the worst case, we will create a single "gap point" for every point we consider
* in the spoints array, plus one before and two after. We can also say that, in the
* worst case, we will have one point for every possible statistical point,
* (i.e. ((end - start) >> pwe) + 1), plus one additional point to the left and
* two additional points to the right. We take the smaller of the two to use the
* tighter upper bound. If we make this too high it's OK; we just allocate more
* memory than we really need. If we make it too low, then we'll write past the end
* of the buffer, which is bad.
*/
this->cachedlen = (int) qMin((((uint64_t) len) << 1) + 2, (((uint64_t) (end - start)) >> this->pwe) + 4);
this->cached = new struct cachedpt[cachedlen + this->connectsToBefore + ddstartatzero + this->connectsToAfter + (2 * ddendatzero)];
struct cachedpt* outputs = this->cached + ddstartatzero + this->connectsToBefore;
int i, j;
int64_t exptime;
j = 0;
if (ddstartatzero)
{
if (this->connectsToBefore)
{
struct cachedpt* output = &this->cached[0];
struct statpt* input = prev->lastpt;
fillpt(output, input, this->epoch, 0.0f, 0.0f, FLAGS_GAP);
pullToZero(&this->cached[1], this->start, this->epoch, 0.0f, prev->lastpt, &spoints[0]);
}
else
{
pullToZeroNoInterp(&this->cached[0], this->start, this->epoch, 0.0f);
}
}
float prevcount = prevfirst ? spoints[0].count : 0.0f;
int64_t prevtime; // Don't need to initialize this.
/* Mutually exclusive with ddstartatzero. */
if (prevfirst)
{
/* Edge case: What if there's a gap before any points? */
exptime = spoints[0].time + pw;
if (len > 1 && spoints[1].time > exptime)
{
pullToZero(&outputs[0], exptime, this->epoch, 0.0f, &spoints[0], &spoints[1]);
prevcount = 0.0f;
j = 1;
}
}
else
{
/* We still need to initialize exptime.
* Note this only matters if prevfirst is false (ddstartatzero is true)
* and numinputs is 0.
* We know that len is nonzero (since we handle that case specially), so
* the only way numinputs can be 0 is if we have nextlast && !this->joinsNext.
*
* Below, we set exptime to the time where we would expect the first point
* after the start to be.
*/
exptime = ((this->start - halfpw - 1) & pwmask) + pw;
}
for (i = 0; i < numinputs; i++, j++)
{
struct statpt* input = &inputs[i];
struct cachedpt* output;
Q_ASSERT(j < this->cachedlen);
output = &outputs[j];
fillpt(output, input, this->epoch, prevcount, (float) input->count, FLAGS_NONE);
prevtime = input->time;
prevcount = output->count;
/* Check if we need to insert a gap after this point.
* Gaps between two cache entries are handled by the first cache entry, so we don't
* have to worry about inserting a gap before the first point.
*/
exptime = prevtime + pw;
if ((i == numinputs - 1 && !this->joinsNext && (!nextlast || inputs[i + 1].time > exptime)) || (i != numinputs - 1 && inputs[i + 1].time > exptime))
{
j++;
Q_ASSERT(j < this->cachedlen);
if (i != numinputs - 1)
{
pullToZero(&outputs[j], exptime, this->epoch, prevcount, &inputs[i], &inputs[i + 1]);
}
else
{
if (nextlast)
{
pullToZero(&outputs[j], exptime, this->epoch, prevcount, input, &spoints[len - 1]);
}
else if (this->connectsToAfter)
{
pullToZero(&outputs[j], exptime, this->epoch, prevcount, input, next->firstpt);
}
else
{
pullToZeroNoInterp(&outputs[j], exptime, this->epoch, prevcount);
}
}
/* If the previous point (at index j - 1) has a gap on either
* side, it needs to be rendered as vertical line.
*/
if ((j > 1 && outputs[j - 2].flags == FLAGS_GAP) || (j == 1 && !prevfirst))
{
/* This tells the vertex shader the appropriate info. */
Q_ASSERT(outputs[j - 1].prevcount == 0.0f);
Q_ASSERT(outputs[j - 1].count != 0.0f);
outputs[j - 1].flags = FLAGS_LONEPT;
outputs[j - 1].flags2 = FLAGS_LONEPT;
}
prevtime = exptime;
prevcount = 0.0f;
exptime += pw;
}
}
if (nextlast && !this->joinsNext)
{
/* This is mutually exclusive with ddendatzero. */
if (spoints[len - 1].time > exptime)
{
/* Don't interpolate unless there is actually a point to interpolate from! */
if (i > 0)
{
pullToZero(&outputs[j], exptime, this->epoch, prevcount, &inputs[i - 1], &spoints[len - 1]);
j += 1;
}
pullToZeroNoInterp(&outputs[j], spoints[len - 1].time, this->epoch, 0.0f);
j += 1;
}
}
if (ddendatzero)
{
if (this->connectsToAfter)
{
pullToZero(&outputs[j], exptime, this->epoch, prevcount, &spoints[len - 1], next->firstpt);
/* Is this really necessary? */
pullToZero(&outputs[j + 1], this->end + 1, this->epoch, 0.0f, &spoints[len - 1], next->firstpt);
struct cachedpt* output = &outputs[j + 2];
struct statpt* input = next->firstpt;
fillpt(output, input, this->epoch, 0.0f, 0.0f, FLAGS_GAP);
j += 3;
}
else
{
pullToZeroNoInterp(&outputs[j], exptime, this->epoch, prevcount);
pullToZeroNoInterp(&outputs[j + 1], this->end + 1, this->epoch, 0.0f);
j += 2;
}
}
Q_ASSERT(j + ddstartatzero + this->connectsToBefore <= this->cachedlen + this->connectsToBefore + ddstartatzero + this->connectsToAfter + (2 * ddendatzero));
this->cachedlen = j + ddstartatzero + this->connectsToBefore; // The remaining were extra...
}
bool CacheEntry::isPlaceholder()
{
return this->cached == nullptr;
}
void CacheEntry::prepare(QOpenGLFunctions* funcs)
{
Q_ASSERT(!this->prepared);
if (this->cachedlen != 0)
{
funcs->glGenBuffers(1, &this->vbo);
funcs->glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
funcs->glBufferData(GL_ARRAY_BUFFER, this->cachedlen * sizeof(struct cachedpt), this->cached, GL_STATIC_DRAW);
funcs->glBindBuffer(GL_ARRAY_BUFFER, 0);
}
this->prepared = true;
}
bool CacheEntry::isPrepared() const
{
return this->prepared;
}
void CacheEntry::renderPlot(QOpenGLFunctions* funcs, float yStart,
float yEnd, int64_t tStart, int64_t tEnd,
int64_t timeOffset,
GLint axisMatUniform, GLint axisVecUniform,
GLint tstripUniform, GLint opacityUniform)
{
Q_ASSERT(this->prepared);
if (this->vbo != 0)
{
float matrix[9];
float vector[2];
/* Fill in the matrix in column-major order. */
matrix[0] = 2.0f / (tEnd - tStart);
matrix[1] = 0.0f;
matrix[2] = 0.0f;
matrix[3] = 0.0f;
matrix[4] = -2.0f / (yEnd - yStart);
matrix[5] = 0.0f;
matrix[6] = -1.0f;
matrix[7] = 1.0f;
matrix[8] = 1.0f;
/* Fill in the offset vector. */
vector[0] = (float) (tStart - epoch - timeOffset - ((Q_INT64_C(1) << pwe) >> 1));
vector[1] = yStart;
/* Now, given a vector <time, value>, where time is relative to
* epoch, first subtract the offset vector, then pad the result
* with a 1 and multiply by the transform matrix. The result is
* the screen coordinates for that point.
*/
funcs->glUniformMatrix3fv(axisMatUniform, 1, GL_FALSE, matrix);
funcs->glUniform2fv(axisVecUniform, 1, vector);
/* First, draw the min-max background. */
funcs->glUniform1f(opacityUniform, 0.5);
funcs->glUniform1i(tstripUniform, 1);
funcs->glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
funcs->glVertexAttribPointer(TIME_ATTR_LOC, 1, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*) 0);
funcs->glVertexAttribPointer(VALUE_ATTR_LOC, 1, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*) sizeof(float));
funcs->glVertexAttribPointer(FLAGS_ATTR_LOC, 1, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*) (4 * sizeof(float)));
funcs->glEnableVertexAttribArray(TIME_ATTR_LOC);
funcs->glEnableVertexAttribArray(VALUE_ATTR_LOC);
funcs->glEnableVertexAttribArray(FLAGS_ATTR_LOC);
funcs->glBindBuffer(GL_ARRAY_BUFFER, 0);
funcs->glDrawArrays(GL_TRIANGLE_STRIP, 0, this->cachedlen << 1);
/* Second, draw vertical lines for disconnected points. */
funcs->glUniform1i(tstripUniform, 0);
funcs->glDrawArrays(GL_LINES, 0, this->cachedlen << 1);
/* Third, draw the mean line. */
funcs->glUniform1f(opacityUniform, 1.0);
funcs->glUniform1i(tstripUniform, 1);
funcs->glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
funcs->glVertexAttribPointer(TIME_ATTR_LOC, 1, GL_FLOAT, GL_FALSE, sizeof(struct cachedpt), (const void*) 0);
funcs->glVertexAttribPointer(VALUE_ATTR_LOC, 1, GL_FLOAT, GL_FALSE, sizeof(struct cachedpt), (const void*) (3 * sizeof(float)));
funcs->glVertexAttribPointer(FLAGS_ATTR_LOC, 1, GL_FLOAT, GL_FALSE, sizeof(struct cachedpt), (const void*) (4 * sizeof(float)));
funcs->glEnableVertexAttribArray(TIME_ATTR_LOC);
funcs->glEnableVertexAttribArray(VALUE_ATTR_LOC);
funcs->glEnableVertexAttribArray(FLAGS_ATTR_LOC);
funcs->glBindBuffer(GL_ARRAY_BUFFER, 0);
funcs->glDrawArrays(GL_LINE_STRIP, 0, this->cachedlen);
/* Fourth, draw the points. */
funcs->glUniform1i(tstripUniform, 0);
funcs->glDrawArrays(GL_POINTS, 0, this->cachedlen);
}
}
void CacheEntry::renderDDPlot(QOpenGLFunctions* funcs, float yStart,
float yEnd, int64_t tStart, int64_t tEnd,
int64_t timeOffset,
GLint axisMatUniform, GLint axisVecUniform)
{
Q_ASSERT(this->prepared);
if (this->vbo != 0)
{
float matrix[9];
float vector[2];
/* Fill in the matrix in column-major order. */
matrix[0] = 2.0f / (tEnd - tStart);
matrix[1] = 0.0f;
matrix[2] = 0.0f;
matrix[3] = 0.0f;
matrix[4] = -2.0f / (yEnd - yStart);
matrix[5] = 0.0f;
matrix[6] = -1.0f;
matrix[7] = 1.0f;
matrix[8] = 1.0f;
/* Fill in the offset vector. */
vector[0] = (float) (tStart - epoch - timeOffset) + (Q_INT64_C(1) << pwe) / 2.0f;
vector[1] = yStart;
/* Now, given a vector <time, value>, where time is relative to
* epoch, first subtract the offset vector, then pad the result
* with a 1 and multiply by the transform matrix. The result is
* the screen coordinates for that point.
*/
funcs->glUniformMatrix3fv(axisMatUniform, 1, GL_FALSE, matrix);
funcs->glUniform2fv(axisVecUniform, 1, vector);
/* Draw the data density plot. */
funcs->glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
funcs->glVertexAttribPointer(TIME_ATTR_LOC, 1, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*) (0 + this->connectsToBefore * sizeof(struct cachedpt)));
funcs->glVertexAttribPointer(COUNT_ATTR_LOC, 1, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*) (2 * sizeof(float) + this->connectsToBefore * sizeof(struct cachedpt)));
funcs->glEnableVertexAttribArray(TIME_ATTR_LOC);
funcs->glEnableVertexAttribArray(COUNT_ATTR_LOC);
funcs->glBindBuffer(GL_ARRAY_BUFFER, 0);
funcs->glDrawArrays(GL_LINE_STRIP, 0, (this->cachedlen - this->connectsToBefore - this->connectsToAfter) << 1);
}
}
void CacheEntry::getRange(int64_t starttime, int64_t endtime, bool count, float& minimum, float& maximum)
{
float relstart = (float) (starttime - this->epoch);
float relend = (float) (endtime - this->epoch);
for (int i = 0; i < cachedlen; i++)
{
struct cachedpt* pt = &this->cached[i];
if (pt->flags != FLAGS_GAP && pt->flags != FLAGS_ALWAYS_HIDE && pt->reltime >= relstart && pt->reltime <= relend)
{
if (count)
{
maximum = qMax(maximum, pt->truecount);
}
else
{
minimum = qMin(minimum, pt->min);
maximum = qMax(maximum , pt->max);
}
}
}
}
uint qHash(const CacheEntry& key, uint seed)
{
return qHash(key.start) ^ qHash(key.end) ^ seed;
}
uint qHash(const QSharedPointer<CacheEntry>& key, uint seed)
{
return qHash(key.data(), seed);
}
Cache::Cache() : cache(), outstanding(), loading(), lru()
{
Q_ASSERT(sizeof(struct cachedpt) == 40);
this->curr_queryid = 0;
this->cost = 0;
this->requester = new Requester;
this->begunChangedRangesUpdateLoop = false;
}
Cache::~Cache()
{
for (auto i = this->cache.begin(); i != this->cache.end(); i++)
{
delete[] i->entries;
}
delete this->requester;
}
/*
* There are two ways we could do this.
* 1) requestData returns a list of entries that were cache hits,
* and entries that missed in the cache are returned
* asynchronously via a signal.
* 2) requestData accepts a callbackthat is fired with a list of
* all cache entries in the range.
*
* The first way allows the plot to update as data becomes
* available; the second way allows the plot to wait until
* all data is available before updating the view.
*
* I think we'll go with the second way, for now. It requires us
* to do more bookkeeping since we have to remember the request
* associated to each chunk of data we get back, but it provides
* for a cleaner API overall.
*/
void Cache::requestData(DataSource* source, const QUuid& uuid, int64_t start, int64_t end,
uint8_t pwe, std::function<void(QList<QSharedPointer<CacheEntry>>, bool)> callback,
uint64_t request_hint, bool includemargins)
{
Q_ASSERT(pwe < PWE_MAX);
QList<QSharedPointer<CacheEntry>>* result = new QList<QSharedPointer<CacheEntry>>;
StreamKey sk(uuid, source);
bool initscache = !this->cache.contains(sk);
struct streamcache& scache = this->cache[sk];
if (initscache)
{
scache.cachedbytes = 0;
CLEAR_CACHED_BOUNDS(scache);
scache.oldestgen = GENERATION_MAX;
scache.lrupos = this->lru.end();
scache.entries = nullptr;
}
if (scache.entries == nullptr)
{
scache.entries = new QMap<int64_t, QSharedPointer<CacheEntry>>[PWE_MAX];
}
QMap<int64_t, QSharedPointer<CacheEntry>>* pwemap = scache.entries;
QMap<int64_t, QSharedPointer<CacheEntry>>* entries = &pwemap[pwe];
QMap<int64_t, QSharedPointer<CacheEntry>>::iterator i;
uint64_t queryid = this->curr_queryid++;
this->outstanding[queryid] = QPair<uint64_t, std::function<void()>>(0, [callback, result]()
{
QList<QSharedPointer<CacheEntry>> res = *result;
delete result;
callback(res, false);
});
unsigned int numnewentries = 0;
/* I'm assuming that the makeDataRequest callbacks ALWAYS happen
* asynchronously.
*/
int64_t nextexp = start; // expected start of the next entry
int64_t filluntil;
QSharedPointer<CacheEntry> nullpointer;
QSharedPointer<CacheEntry> prev = nullpointer;
i = entries->lowerBound(start);
if (includemargins && i != entries->begin())
{
auto ptr = *(i - 1);
if (!ptr->isPlaceholder())
{
result->append(ptr);
}
}
for (; nextexp <= end; i++)
{
QSharedPointer<CacheEntry> entry = (i == entries->end() ? nullpointer : *i);
if (entry == nullpointer)
{
filluntil = end;
}
else
{
/* Check that adjacent cache entries do not overlap. */
Q_ASSERT(nextexp == start || entry->start >= nextexp);
if (entry->start <= nextexp)
{
goto nogap;
}
filluntil = qMin(entry->start == INT64_MIN ? entry->start : entry->start - 1, end);
}
{
/* There's a gap that needs to be filled. First, check if we
* should "expand" the query, according to the REQUEST HINT.
*/
if (((uint64_t) (filluntil - nextexp)) < request_hint)
{
/* If this hole is in the "middle" of the query, we
* can't expand it because it is bounded by a cache entry
* on either side.
*
* But we might be able to expand it if it is at the start
* or end of the query.
*/
int64_t newval;
if (filluntil == end)
{
filluntil = nextexp + request_hint;
if (filluntil < nextexp)
{
filluntil = INT64_MAX;
}
if (entry != nullpointer)
{
newval = entry->start;
if (newval != INT64_MIN)
{
newval--;
}
filluntil = qMin(filluntil, newval);
}
}
else if (nextexp == start)
{
nextexp = filluntil - request_hint;
if (nextexp > filluntil)
{
nextexp = INT64_MIN;
}
if (i != entries->begin())
{
newval = (*(i - 1))->end;
if (newval != INT64_MAX)
{
newval++;
}
nextexp = qMax(nextexp, newval);
}
}
}
/* We're about to insert an entry, so check that it doesn't
* overlap with the previous one.
*/
if (i != entries->begin())
{
Q_ASSERT ((*(i - 1))->end < nextexp);
}
QSharedPointer<CacheEntry> gapfill(new CacheEntry(this, sk, nextexp, filluntil, pwe));
/* I could call this->addCost here, but it actually drops cache entries immediately,
* altering the structure of the tree. If I get unlucky, it may remove the entry
* that the iterator is pointing to (the variable ENTRY), invalidating the iterator.
* So I'm just going to count the number of gaps filled and add the cost at the end,
* when I'm not iterating over the map.
*/
numnewentries++;
result->append(gapfill);
this->outstanding[queryid].first++;
this->loading.insertMulti(gapfill, queryid);
i = entries->insert(i, gapfill->end, gapfill);
/* Make the request. */
this->requester->makeDataRequest(uuid, gapfill->start, gapfill->end, pwe, source,
[this, i, gapfill, prev, entry, callback, result](struct statpt* points, int len, uint64_t gen)
{
/* ALWAYS fill it with data, because this entry may be needed to draw one last frame. */
gapfill->cacheData(points, len, prev, entry);
/* If the entry was evicted meanwhile, skip its initialization. Don't touch i,
* since the entry has been removed from the tree and therefore the iterator
* pointing to it is invalid.
*/
if (!gapfill->evicted)
{
/* Add it to the LRU linked list before removing entries
* to meet the cache threshold, so that we release this
* same cache entry should we need to.
*/
gapfill->cachepos = i;
this->use(gapfill, true);
this->addCost(gapfill->streamKey, ((uint64_t) len) * CACHED_POINT_SIZE);
if (len != 0)
{
/* If we got back zero points, BTrDB gave us no frames,
* and therefore no version number. Don't trust the
* version number in the callback.
*/
this->updateGeneration(gapfill->streamKey, gen);
}
}
QHash<QSharedPointer<CacheEntry>, uint64_t>::const_iterator j;
for (j = this->loading.find(gapfill); j != this->loading.end() && j.key() == gapfill; ++j) {
if (--this->outstanding[j.value()].first == 0)
{
auto tocall = this->outstanding[j.value()].second;
this->outstanding.remove(j.value());
tocall();
}
}
/* The reason that we aren't using "erase()" while
* iterating is that doing so prevents the hashtable
* from rehashing, since the iterator needs to remain
* valid. We don't want to prevent that.
*/
this->loading.remove(gapfill);
});
i++;
}
nogap:
if (entry == nullpointer || entry->start > end)
{
break;
}
if (entry->isPlaceholder())
{
this->outstanding[queryid].first++;
this->loading.insertMulti(entry, queryid);
}
else
{
this->use(entry, false);
}
result->append(entry);
/* Edge case: if entry->end is INT64_MAX, then adding
* one to it to get the next nextexp will overflow,
* messing up the cache on the next iteration.
*
* Instead, just break the loop if we reach this case.
* There's no work left to do.
*/
if (entry->end == INT64_MAX)
{
i++;
break;
}
nextexp = entry->end + 1;
prev = entry;
}
if (includemargins && i != entries->end()) {
auto ptr = *i;
Q_ASSERT(result->isEmpty() || result->last() != ptr);
if (!ptr->isPlaceholder())
{
result->append(ptr);
}
}
uint64_t numqueriesmade = this->outstanding[queryid].first;
if (numqueriesmade == 0)
{
/* Cache hit! */
callback(*result, true);
delete result;
this->outstanding.remove(queryid);
}
this->addCost(sk, numnewentries * CACHE_ENTRY_OVERHEAD);
if (initscache)
{
this->addCost(sk, STREAM_OVERHEAD);
}
this->beginChangedRangesUpdateLoopIfNotBegun();
}
void Cache::requestBrackets(DataSource* source, const QList<QUuid> uuids,
std::function<void (int64_t, int64_t)> callback)
{
/* First, check if the brackets are in the cache. */
QList<QUuid> torequest;
int64_t initlowerbound = INT64_MAX;
int64_t initupperbound = INT64_MIN;
for (auto j = uuids.begin(); j != uuids.end(); j++)