-
Notifications
You must be signed in to change notification settings - Fork 3
/
wavesketch.cpp
399 lines (369 loc) · 10.8 KB
/
wavesketch.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
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string.h>
#include <stdint.h>
#include <random>
#include <string>
#include <memory>
#include <iostream>
#include <cmath>
#include <math.h>
#include<time.h>
#include<unordered_map>
#include<algorithm>
#include<fstream>
#include <bitset>
using namespace std;
#define START_FILE_NO 1
#define END_FILE_NO 10
#define landa_d 16
#define b 1.08
#define test_cycles 1
#define k 1000
#define hh 0.0002
#define hc 0.0005
#define epoch 10
#define BUCKET_NUM (HEAVY_MEM / 134)
#define TOT_MEM_IN_BYTES (600 * 1024)
#define INT_MAX ((int)(~0U>>1))
struct FIVE_TUPLE { char key[13]; };
typedef vector<FIVE_TUPLE> TRACE;
TRACE traces[END_FILE_NO - START_FILE_NO + 1];
static const int COUNT[2] = {1, -1};
static const int factor = 1;
static int HEAVY_MEM = 5 * 1024;
inline unsigned int BKDRHash(char* str)
{
unsigned int seed = 131;
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
inline unsigned int finger_print(unsigned int hash)
{
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
return hash & 0x7FFFFFFF;
}
class hg_node
{
public:
vector<unsigned int>items; //32*16
vector<int>counters; //32*16
bitset<landa_d>real; //16
int incast; //32
hg_node()
{
items = vector<unsigned int>(landa_d, 0);
counters = vector<int>(landa_d, 0);
incast = 0;
real.reset();
}
void insert(unsigned int item, int hash)
{
unsigned int choice = item & 1;
int min_num = INT_MAX;
unsigned int min_pos = -1;
for(unsigned int i = 0; i < landa_d; ++i){
if(counters[i] == 0){
items[i] = item;
counters[i] = 1;
real[i] = 1;
return;
}
else if(items[i] == item){
if(real[i])
counters[i]++;
else{
counters[i]++;
incast += COUNT[choice];
}
return;
}
if(counters[i] < min_num){
min_num = counters[i];
min_pos = i;
}
}
/*
if(incast * COUNT[choice] >= min_num){
//count_type pre_incast = incast;
if(real[min_pos]){
uint32_t min_choice = hash_(items[min_pos], 17) & 1;
incast += COUNT[min_choice] * counters[min_pos];
}
items[min_pos] = item;
counters[min_pos] += 1;
real[min_pos] = 0;
}
incast += COUNT[choice];
*/
if(incast * COUNT[choice] >= int(min_num * factor)){
//count_type pre_incast = incast;
if(real[min_pos]){
unsigned int min_choice = items[min_pos] & 1;
incast += COUNT[min_choice] * counters[min_pos];
}
items[min_pos] = item;
counters[min_pos] += 1;
real[min_pos] = 0;
}
incast += COUNT[choice];
}
int query(unsigned int item, int hash)
{
unsigned int choice = item & 1;
for(unsigned int i = 0; i < landa_d; ++i){
if(items[i] == item){
return counters[i];
}
}
return 0;//incast * COUNT[choice];
}
};
void ReadInTraces(const char* trace_prefix)
{
for (int datafileCnt = START_FILE_NO; datafileCnt <= END_FILE_NO; ++datafileCnt)
{
char datafileName[100];
sprintf(datafileName, "%s%d.dat", trace_prefix, datafileCnt - 1);
FILE* fin = fopen(datafileName, "rb");
FIVE_TUPLE tmp_five_tuple;
traces[datafileCnt - 1].clear();
while (fread(&tmp_five_tuple, 1, 13, fin) == 13)
{
traces[datafileCnt - 1].push_back(tmp_five_tuple);
}
fclose(fin);
printf("Successfully read in %s, %ld packets\n", datafileName, traces[datafileCnt - 1].size());
}
printf("\n");
}
bool cmp1(pair<int,int>p1, pair<int,int>p2)
{
return p1.first > p2.first;
}
bool cmp2(pair<int,int>p1, pair<int,int>p2)
{
return p1.second > p2.second;
}
int main()
{
ReadInTraces("data/");
ofstream out("./ws.txt");
for(int i = 0; i<10; i++)
{
out<<"memory: "<<HEAVY_MEM<<endl;
vector<double> gb_heavy_changer(10, 0);
vector<int>hc_cnt(10,0);
vector<double> gb_heavy_hitter(10, 0);
vector<int>hh_cnt(10,0);
double gb_ARE = 0;
double gb_WMRE = 0;
double gb_entropy = 0;
double gb_fscore = 0;
double gb_throughput = 0;
srand((unsigned)time(NULL));
for (int datafileCnt = START_FILE_NO; datafileCnt <= END_FILE_NO; ++datafileCnt)
{
unordered_map<int,int>gb_cnt;
unordered_map<int,int>lc_cnt;
unordered_map<int,int>pre_cnt;
unordered_map<int,int>hit_cnt;
unordered_map<int,int>ph_cnt;
unordered_map<int,int>freq;
unordered_map<int,int>freq_e;
timespec time1, time2;
long long resns;
int packet_cnt = (int)traces[datafileCnt - 1].size();
int window = packet_cnt / epoch;
//printf("packet count:%d\n", packet_cnt);
char** keys = new char * [(int)traces[datafileCnt - 1].size()];
for (int i = 0; i < (int)traces[datafileCnt - 1].size(); ++i)
{
keys[i] = new char[13];
memcpy(keys[i], traces[datafileCnt - 1][i].key, 13);
}
clock_gettime(CLOCK_MONOTONIC, &time1);
double th;
for (int t = 0; t < test_cycles; ++t)
{
vector<hg_node>hg(BUCKET_NUM);
for (int i = 0; i < packet_cnt; ++i)
{
int hash = BKDRHash(keys[i]);
gb_cnt[hash] += 1;
lc_cnt[hash] += 1;
if(hit_cnt.count(hash) == 0)hit_cnt[hash] = hg[hash % BUCKET_NUM].query(finger_print(hash), hash);
hg[hash % BUCKET_NUM].insert(finger_print(hash), hash);
//if (i % 100000 == 0)printf("flow frequency:%d\n", hg[hash % BUCKET_NUM].query(finger_print(hash), hash));
//hg[hash % BUCKET_NUM].query(finger_print(hash), hash);
if(i && i % window == 0)
{
//heavy hitter
if(1)
{
double th = window * hh;
int tp = 0, fp =0, tn = 0, fn = 0;
for(auto it:gb_cnt)
{
bool f1 = 0, f2 = 0;
if(it.second >= th)f1 = 1;
int hash = it.first;
int efreq = hg[hash % BUCKET_NUM].query(finger_print(hash), hash);
if(efreq >= th)f2 = 1;
if(f1 && f2)tp++;
else if(f1 && !f2)fn++;
else if(!f1 && f2)fp++;
else if(!f1 && !f2)tn++;
}
double recall = (double)tp/(tp+fn);
double precision = (double)tp/(tp+fp);
double fscore = 2*(recall*precision)/(recall + precision);
//cout<<recall<<" "<<precision<<endl;
//out<<i/window <<"th heavy hitter FSOCRE:"<<fscore<<endl;
gb_heavy_hitter[i/window-1] += fscore;
hh_cnt[i/window-1] += 1;
}
//heavy changer
if(1)
{
double th = window * hc;
int tp = 0, fp =0, tn = 0, fn = 0;
for(auto it:lc_cnt)
{
bool f1 = 0, f2 = 0;
if(it.second >= th)f1 = 1;
int hash = it.first;
int efreq = hg[hash % BUCKET_NUM].query(finger_print(hash), hash) - hit_cnt[hash];
ph_cnt[hash] = efreq;
if(efreq >= th)f2 = 1;
if(f1 && f2)tp++;
else if(f1 && !f2)fn++;
else if(!f1 && f2)fp++;
else if(!f1 && !f2)tn++;
}
double recall = (double)tp/(tp+fn);
double precision = (double)tp/(tp+fp);
double fscore = 2*(recall*precision)/(recall + precision);
//cout<<recall<<" "<<precision<<endl;
//out<<i/window <<"th heavy hitter FSOCRE:"<<fscore<<endl;
gb_heavy_changer[i/window-1] += fscore;
hc_cnt[i/window-1] += 1;
}
lc_cnt = unordered_map<int,int>();
hit_cnt = unordered_map<int,int>();
}
}
clock_gettime(CLOCK_MONOTONIC, &time2);
resns = (long long)(time2.tv_sec - time1.tv_sec) * 1000000000LL + (time2.tv_nsec - time1.tv_nsec);
th = (double)1000.0 * test_cycles * packet_cnt / resns;
//ARE
vector<pair<int,int>>topk;
for(auto it:gb_cnt)
{
topk.push_back(make_pair(it.first, it.second));
}
sort(topk.begin(), topk.end(), cmp2);
double ARE = 0;
for(int i =0; i<k; i++)
{
int hash = topk[i].first;
int efq = hg[hash % BUCKET_NUM].query(finger_print(hash), hash);
ARE += (double)abs(topk[i].second - efq)/topk[i].second;
}
ARE /= k;
//out<<"ARE:"<<ARE<<endl;
cout<<"ARE:"<<ARE<<endl;
gb_ARE += ARE;
//WMRE
int max_freq = 0;
for(auto it:gb_cnt)
{
freq[it.second] += 1;
int hash = it.first;
int efreq = hg[hash % BUCKET_NUM].query(finger_print(hash), hash);
freq_e[efreq] += 1;
max_freq = max(max_freq, it.second);
max_freq = max(max_freq, efreq);
}
double wmre = 0, wmd = 0;
for(int i = 1; i<=max_freq; i++)
{
wmre += (double)abs(freq[i] - freq_e[i]);
wmd += ((double)freq[i] + freq_e[i])/2;
}
//out<<"WMRE:"<<wmre/wmd<<endl;
cout<<"WMRE:"<<wmre/wmd<<endl;
gb_WMRE += wmre/wmd;
//entropy
int flow_num = gb_cnt.size();
cout<<"flow_num:"<<flow_num<<endl;
double e = 0, ee = 0;
for(int i = 1; i<=max_freq; i++)
{
e += freq[i]?-1*((double)i*freq[i]/flow_num)*log2((double)freq[i]/flow_num):0;
ee += freq_e[i]?-1*((double)i*freq_e[i]/flow_num)*log2((double)freq_e[i]/flow_num):0;
}
//out<<"entropy ARE:"<<fabs(e-ee)/e<<endl;
cout<<"entropy ARE:"<<fabs(e-ee)/e<<endl;
//out<<"real entropy:"<<e<<endl;
gb_entropy += fabs(e-ee)/e;
//FSCORE
unordered_map<int,bool>ef;
double th = packet_cnt * hh;
int tp = 0, fp =0, tn = 0, fn = 0;
for(auto it:gb_cnt)
{
bool f1 = 0, f2 = 0;
if(it.second >= th)f1 = 1;
int hash = it.first;
int efreq = hg[hash % BUCKET_NUM].query(finger_print(hash), hash);
if(efreq >= th)f2 = 1;
if(f1 && f2)tp++;
else if(f1 && !f2)fn++;
else if(!f1 && f2)fp++;
else if(!f1 && !f2)tn++;
}
double recall = (double)tp/(tp+fn);
double precision = (double)tp/(tp+fp);
double fscore = 2*(recall*precision)/(recall + precision);
//cout<<recall<<" "<<precision<<endl;
//out<<"Total FSOCRE:"<<fscore<<endl;
cout<<"Total FSOCRE:"<<fscore<<endl;
gb_fscore += fscore;
}
/* free memory */
for (int i = 0; i < (int)traces[datafileCnt - 1].size(); ++i)
delete[] keys[i];
delete[] keys;
printf("throughput is %lf mbps\n", th);
//out<<"throughput:"<<th<<"\n"<<endl;
gb_throughput += th;
}
out<<"heavy changer FSOCRE:"<<endl;
for(int i = 0; i<10; i++)
{
if(hc_cnt[i] > 0)out<<gb_heavy_changer[i]/hc_cnt[i]<<endl;
}
out<<"heavy hitter FSOCRE:"<<endl;
for(int i = 0; i<10; i++)
{
if(hh_cnt[i] > 0)out<<gb_heavy_hitter[i]/hh_cnt[i]<<endl;
}
out<<"ARE:"<<gb_ARE/10<<endl;
out<<"WMRE:"<<gb_WMRE/10<<endl;
out<<"entropy ARE:"<<gb_entropy/10<<endl;
out<<"Total FSOCRE:"<<gb_fscore/10<<endl;
out<<"throughput:"<<gb_throughput/10<<endl;
out<<endl;
HEAVY_MEM += 5*1024;
}
}