-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess.cpp
340 lines (283 loc) · 8.28 KB
/
process.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
#include <iostream>
#include <math.h>
#include <vector>
#include <queue>
#include <random>
#include <sstream>
#include <string>
#include <fstream>
#include <algorithm>
#include <time.h>
#define TIME 77
#define SLOTS 6000
#define SESSIONS 4885
#define PEOPLE 2235
#define CACHESIZE 100
#define MAXTHREADS 25
#define UPTIME 5
#define DOWNTIME 5
#define LOCALTIME 100
/*
* 1: 0.0167276
5: 0.0230444
9: 0.0198048
13: 0.0227342
17: 0.0227221
21: 0.028723
25: 0.0351694
50: 0.0516618
75: 0.0729074
100: 0.197228
125: 0.227336
150: 0.259223
175: 0.36999
200: 0.614497
225: 0.683961*/
/*1: 0.016754
5: 0.0161688
9: 0.0166452
13: 0.0161343
17: 0.0185968
21: 0.0191036
25: 0.0200433
29: 0.0236736
33: 0.0217142
37: 0.0254793
41: 0.0229777
66: 0.03218
91: 0.0374223
116: 0.0477759
141: 0.0498934
166: 0.0613052
191: 0.067237
216: 0.0696899
241: 0.103933*/
using namespace std;
struct session{
int time;
int id;
int count;
int *people;
};
struct quest{
int person;
int image;
int repeats;
};
vector<struct session> sessions[TIME];
vector<struct quest> quests[TIME*SLOTS];
vector<int> cache[PEOPLE];
queue<int> queueNormal;
queue<int> queueHigh;
queue<int> queueTime;
int threads[MAXTHREADS] = { 0 };
int interval = 500;
int randomNum(int max) {
return rand() % max;
}
void loadTraces() {
ifstream file("dataset/mobicom06-trace.txt");
string line, word;
getline(file, line);
int total = 0;
for(int i = 0; i < SESSIONS; i++) {
getline(file, line);
istringstream is1(line);
session curSession;
is1>>word;
curSession.time = stoi(word);
is1 >> word;
curSession.id = stoi(word);
is1 >> word;
curSession.count = stoi(word);
total += curSession.count;
curSession.people = (int*)malloc(sizeof(int)*curSession.count);
getline(file, line);
istringstream is2(line);
for (int j = 0; j < curSession.count; j++) {
is2 >> word;
curSession.people[j] = stoi(word);
}
sessions[curSession.time].push_back(curSession);
}
cout << "total view count: " << total << endl;
file.close();
}
void distributeQuests() {
for (int t = 0; t < TIME; t++) {
for (int i = 0; i < sessions[t].size(); i++) {
session curSession = sessions[t][i];
for (int j = 0; j < curSession.count; j++) {
struct quest curQuest;
curQuest.person = curSession.people[j] / 10;
curQuest.image = curSession.id % 1000;
int startSlot = randomNum(interval);
for (int k = 0; k < SLOTS; k += interval) {
curQuest.repeats = k/interval;
quests[curSession.time * SLOTS + k + startSlot].push_back(curQuest);
}
}
}
}
#if 0
for (int i = 0; i < TIME; i++) {
for (int j = 0; j < SLOTS; j++) {
cout << quests[i * SLOTS + j].size() << " ";
}
cout << endl;
}
#endif
}
int curRunningTime(int runningThreads) {
int time = 10 + runningThreads * 2;
//cout<<"cur time "<<time<<endl;
return time;
}
bool cacheHit(quest q) {
for (int i = 0; i < cache[q.person].size(); i++) {
if (cache[q.person][i] == q.image)
return true;
}
return false;
}
void insertCache(int person, int image) {
if(image == 0) return;
//already exist
for (int i = 0; i < cache[person].size(); i++) {
if (cache[person][i] == image)
return;
}
//cache size full, random replace
if (cache[person].size() == CACHESIZE) {
cache[person][randomNum(CACHESIZE)] = image;
return;
}
cache[person].push_back(image);
}
int getCache(int person) {
if (cache[person].size() == 0) return 0;
int index = randomNum(cache[person].size());
return cache[person][index];
}
void cacheSharing(int time) {
for (int i = 0; i < sessions[time].size(); i++) {
session curSession = sessions[time][i];
random_shuffle(curSession.people, curSession.people + curSession.count);
for (int j = 0; j < curSession.count - 1; j += 2) {
insertCache(curSession.people[j]/10, getCache(curSession.people[j + 1]/10));
insertCache(curSession.people[j + 1]/10, getCache(curSession.people[j]/10));
}
}
}
int main(int argc, char *argv[]) {
int serverOnly = 0;
double totalTime = 0;
int totalQuests = 0;
int latestTime = 0;
int averageTime = 0;
int totalCache = 0;
int hittedCache = 0;
int totalUp = 0, realUp = 0;
if(argc < 3) {
cout << "Usage: " << argv[0] << " interval[100/200/...] serverOnly[0/1]" << endl;
return 1;
} else {
interval = stoi(argv[1]);
serverOnly = stoi(argv[2]);
}
loadTraces();
distributeQuests();
for (int i = 0; i < TIME * SLOTS * 10; i++) {
//schedule server execution
int runningThreads = 0;
for (int j = 0; j < MAXTHREADS; j++) {
if(threads[j] > 0) threads[j]--;
if (threads[j] == 0) {
if (queueHigh.size() > 0) {
//queuing time
totalTime += i - queueHigh.front();
latestTime = i - queueHigh.front() + 20;
queueTime.push(latestTime);
queueHigh.pop();
threads[j] = -1;
}
else if (queueNormal.size() > 0) {
//queuing time
totalTime += i - queueNormal.front();
latestTime = i - queueNormal.front() + 20;
queueTime.push(latestTime);
queueNormal.pop();
threads[j] = -1;
}
}
if (threads[j] != 0) runningThreads++;
}
for (int j = 0; j < MAXTHREADS; j++) {
if (threads[j] == -1) {
threads[j] = curRunningTime(runningThreads);
//execution time
totalTime += threads[j];
totalQuests++;
}
}
//latestTime += curRunningTime(runningThreads);
//client cache sharing
if(i % (100 * 10) == 0) cacheSharing(i / (SLOTS * 10));
//client request generation
if (i % 10 == 0) {
vector<struct quest> curQuests = quests[i / 10];
for (int j = 0; j < curQuests.size()/5; j++) {
quest curQuest = curQuests[j];
totalUp++;
//edge process quest
int server_est = 0;
server_est = latestTime;
#if 0
while(queueTime.size() > 5) queueTime.pop();
for(int i = 0; i < queueTime.size(); i++) {
server_est += queueTime.front();
queueTime.push(queueTime.front());
queueTime.pop();
}
server_est = server_est * 1.0f / queueTime.size();
server_est = (queueHigh.size() + queueNormal.size()) * 1.5f / MAXTHREADS * 50 + 20;
#endif
if (serverOnly || server_est < LOCALTIME) {
//send quest to edge
totalTime += UPTIME;
//server process, time is determined later
queueNormal.push(i);
//server send result back
totalTime += DOWNTIME;
realUp++;
}
else {
//client local cache
totalTime += LOCALTIME;
//if(curQuest.repeats < 2) totalCache++;
totalCache++;
//local cache miss
if (!cacheHit(curQuest)) {
//send quest to edge
totalTime += UPTIME;
//server process, time is determined later
queueHigh.push(i);
//server send result back
totalTime += DOWNTIME;
realUp++;
}
else {
totalQuests++;
//if(curQuest.repeats < 2) hittedCache++;
hittedCache++;
}
}
insertCache(curQuest.person, curQuest.image);
}
}
//if (i != 0 && i % 100000 == 0) cout << totalTime * 1.0f / totalQuests << endl;
if (i != 0 && i % 100000 == 0) cout << hittedCache * 1.0f / totalCache << endl;
//if (i != 0 && i % 100000 == 0) cout << totalUp << endl;
}
return 0;
}