8 a->type = a->id >> 28 & 15;
9 a->keysize = a->id >> 13 & 0x7ff8;
15 a->recsize = 8 + a->keysize + 8;
18 a->recsize = 8 + a->keysize + 16;
21 a->recsize = 8 + a->keysize + 32;
29 hash(uchar *s, int n, int m)
34 h = 0xcbf29ce484222325ULL;
35 for(i = 0; i < n; i++){
37 h *= 0x100000001b3ULL;
40 h *= 0x100000001b3ULL;
45 keyeq(uchar *a, uchar *b, int n, int m)
49 for(i = 0; i < n; i++)
58 /* calculate v*v with 128 bits precision and add it to the 128-bit word at q */
60 addsquare(u64int *q, s64int v)
69 s0 = (s64int)v0 * (s64int)v0;
70 s1 = (s64int)v0 * (s64int)v1;
71 s2 = (s64int)v1 * (s64int)v1;
73 if(r < (u64int)s0) q[1]++;
76 q[1] += s2 + (s1>>31);
80 updaterecord(int type, u64int *q, s64int val)
83 case AGGCNT: q[0] += 1; break;
84 case AGGSUM: q[0] += val; break;
85 case AGGAVG: q[0] += val; q[1]++; break;
86 case AGGMIN: if(val < q[0]) q[0] = val; break;
87 case AGGMAX: if(val > q[0]) q[0] = val; break;
88 case AGGSTD: q[0] += val; q[1]++; addsquare(&q[2], val); break;
93 createrecord(int type, u64int *q, s64int val)
96 case AGGCNT: q[0] = 1; break;
97 case AGGSUM: case AGGMIN: case AGGMAX: q[0] = val; break;
98 case AGGAVG: q[0] = val; q[1] = 1; break;
99 case AGGSTD: q[0] = val; q[1] = 1; q[2] = 0; q[3] = 0; addsquare(&q[2], val); break;
103 /* runs in probe context */
105 dtarecord(DTChan *ch, int mach, DTAgg *a, uchar *key, int nkey, s64int val)
111 c = ch->aggwrbufs[mach];
112 h = hash(key, nkey, a->keysize);
113 p = (u32int*)(c->data + DTABUCKETS + (h % DTANUMBUCKETS) * 4);
115 assert((uint)*p < DTABUCKETS);
116 q = (u32int*)(c->data + *p);
117 if(q[1] == a->id && keyeq((uchar*)(q + 2), key, nkey, a->keysize) == 0){
118 updaterecord(a->type, (u64int*)(q + 2 + a->keysize / 4), val);
123 if(c->wr + a->recsize > DTABUCKETS)
126 q = (u32int*)(c->data + c->wr);
129 if(nkey == a->keysize)
130 memmove(&q[2], key, nkey);
131 else if(nkey > a->keysize){
132 memmove(&q[2], key, nkey);
133 memset((uchar*)q + 8 + nkey, 0, a->keysize - nkey);
135 memmove(&q[2], key, a->keysize);
136 createrecord(a->type, (u64int*)(q + 2 + a->keysize / 4), val);