-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.c
More file actions
393 lines (343 loc) · 8.1 KB
/
app.c
File metadata and controls
393 lines (343 loc) · 8.1 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h> /* For SYS_xxx definitions */
#include "test.h"
static int in_kvm;
struct sched_stats
{
unsigned long logged;
unsigned long oflowed;
unsigned long long of_max;
unsigned long long max;
unsigned long *logs;
unsigned long logs_len;
};
struct thread_param
{
int id;
int pcpu;
struct request *req;
struct sched_stats *yield;
struct sched_stats *preempted;
};
static struct thread_param **params = NULL;
static pthread_t *threads = NULL;
unsigned long long prempt_crazy_noise, max_crazy_noise;
/* We only log the scheduler situation from MAX_NOISE to MAX_NOISE_LOGS */
#define MAX_NOISE 0x30000
#define MAX_NOISE_LOG 0x1000000
unsigned long prempt_log[MAX_NOISE_LOG >> 12], total_yielded;
int yield_exec(void)
{
if (!in_kvm)
{
sched_yield();
} else
{
int ret;
int hc=1,p1=1,p2=1;
asm volatile(".byte 0x0f,0x01,0xc1\n\t"
".byte 0x0f,0x01,0xd9\n\t"
: "=a"(ret)
:
"a"(hc),
"b"(p1),
"c"(p2)
:
"memory");
}
return 0;
}
/* XXX hardcode here, will change to cpuid when begin the KVM task */
int check_inkvm(void)
{
in_kvm = 0;
return 0;
}
ttime_t YIELD_THRESH = 0x1000;
ttime_t YIELD_LOG_MAX = 0x2000;
ttime_t YIELD_LOG_SHIFT = 12;
ttime_t PREEMPTION_THRESH = 0x1000;
ttime_t PREEMPTION_LOG_MAX = 0x2000;
ttime_t PREEMPTION_LOG_SHIFT = 12;
static void dumpResult(struct thread_param *result)
{
int i;
tprintf("yield delay status \n");
tprintf("maxium yield is %lld max overflow %lld\n",
result->yield->max, result->yield->of_max);
for (i=0; i< (YIELD_LOG_MAX>> 12); i++)
if (result->yield->logs[i])
tprintf("yield %x count %ld\n", i << 12, result->yield->logs[i]);
tprintf("Total preempted %lx \n", result->preempted->logged);
tprintf("maxium preempted is %lld max overflow %lld\n",
result->preempted->max, result->preempted->of_max);
for (i=0; i< (PREEMPTION_LOG_MAX >> 12); i++)
if (result->preempted->logs[i])
tprintf("preempted %x count %ld\n", i << 12, result->preempted->logs[i]);
}
static int num_apps_initiated = 0;
void dumpAppResults(void)
{
int i;
for (i = 0; i < num_apps_initiated; i++)
{
tprintf("\ndump result %x\n", i);
dumpResult(params[i]);
}
}
void log_preempted(struct sched_stats *preempt, ttime_t delta)
{
if (delta > preempt->max)
preempt->max = delta;
if (delta > PREEMPTION_THRESH)
{
/* Align to about 1us, since we don't care for the varia less than 1us */
preempt->logged ++;
if (delta > PREEMPTION_LOG_MAX)
{
preempt->oflowed++;
if (delta> preempt->of_max)
preempt->of_max = delta;
}
else
preempt->logs[delta >> PREEMPTION_LOG_SHIFT] ++;
}
}
/* XXX possibly we can merge with the log_yielded if we are sure they are
* totally similar
*/
void log_yield(struct sched_stats* yield, ttime_t delta)
{
if (delta > yield->max)
yield->max = delta;
if (delta > YIELD_THRESH)
{
/* Align to about 1us, since we don't care for the varia less than 1us */
yield->logged ++;
if (delta> YIELD_LOG_MAX)
{
yield->oflowed++;
if (delta> yield->of_max)
yield->of_max = delta;
}
else
yield->logs[delta >> YIELD_LOG_SHIFT] ++;
}
}
int waitReqReady(struct request *req, int sync)
{
if (!req)
return -EFAULT;
if (req->status != reqs_sent)
return -EBUSY;
return 0;
}
ttime_t getDeadline(ttime_t now, struct request *req)
{
return now + (req->deadline - req->rtime);
}
static int execTask(ttime_t duration)
{
while (duration)
duration--;
return 0;
}
int test(struct request *req, struct sched_stats *prempt,
struct sched_stats *yield)
{
ttime_t stsc, etsc, delta, deadline;
int oldstat;
while(app_loop && waitReqReady(req, 0));
if (!app_loop)
return 0;
oldstat = __sync_val_compare_and_swap(&req->status, reqs_sent,
reqs_wip);
if (oldstat != reqs_sent)
{
tprintf("receiving status changed on the fly to %d, anything wrong?? \n", oldstat);
return -EFAULT;
}
req->stime = getNow();
deadline = getDeadline(req->stime, req);
while ((getNow() < deadline) && (req->done < req->req.size) && !req->eabort)
{
stsc=getNow();
execTask(req->req.duration);
etsc = getNow();
if (etsc < stsc)
{
tprintf("Hit time wrap, exit\n");
return -EFAULT;
}
else {
delta = etsc - stsc;
if ((delta - req->req.duration) > PREEMPTION_THRESH)
log_preempted(prempt, delta);
}
req->done++;
}
req->etime = getNow();
// printf("req abort %x done %x size %x duration %x now %llx deadline %llx req deadline %llx rtime %llx\n", req->eabort, req->done, req->req.size, req->req.duration, getNow(), deadline, req->deadline, req->rtime);
oldstat = __sync_val_compare_and_swap(&req->status, reqs_wip,
reqs_done);
if (oldstat != reqs_wip)
{
tprintf("status changed on the guest fly, anything wrong?? \n");
return -EFAULT;
}
stsc = getNow();
yield_exec();
etsc = getNow();
if (etsc < stsc)
{
tprintf("Hit tscl wrap after yield\n");
}
else {
delta = etsc - stsc;
log_yield(yield, delta);
}
return 0;
}
volatile int app_loop;
void *app_thread(void *param)
{
struct thread_param *par = param;
int pcpu;
cpu_set_t mask;
pthread_t thread;
pcpu = par->pcpu;
if (pcpu)
{
CPU_ZERO(&mask);
CPU_SET(pcpu, &mask);
thread = pthread_self();
if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask))
{
tprintf("set affinity failed \n");
printf("set affinity failed from app.c %u \n", pcpu);
return NULL;
}
}
while (app_loop) {
if (test(par->req, par->preempted, par->yield))
{
tprintf("Something wrong on the test, exit now!\n");
return NULL;
}
}
return NULL;
}
/* XXX Hardcode now, can be configuration in future */
static int getPCpu(int id)
{
return id + 1;
//return 22;
}
int init_dpdk_apps(int num_apps)
{
int i,result = 0;
params = calloc(1, sizeof(struct thread_param *) * num_apps);
if (!params)
return -ENOMEM;
threads = calloc(1, sizeof(pthread_t) * num_apps);
if (!threads)
{
result = -ENOMEM;
goto error;
}
for (i = 0; i < num_apps; i++)
{
pthread_attr_t attr;
int status;
struct thread_param *par = NULL;
status = pthread_attr_init(&attr);
if (status != 0){
tprintf("error from pthread_attr_init for thread %d\n", i);
result = -ENOMEM;
break;
}
par = params[i] = calloc(1,
sizeof(struct thread_param)+
2*sizeof(struct sched_stats));
if (!par )
{
tprintf("Failed to alloc thread param\n");
result = -ENOMEM;
break;
}
par->yield = (struct sched_stats *)(
(void *)par + sizeof(struct thread_param));
par->preempted= (struct sched_stats *)(
(void *)(par->yield) + sizeof(struct thread_param));
par->id = i;
par->pcpu = getPCpu(i);
par->req = getRequest(i);
par->yield->logs_len = YIELD_LOG_MAX >> YIELD_LOG_SHIFT;
par->yield->logs = calloc(par->yield->logs_len,
sizeof(ttime_t));
par->preempted->logs_len = PREEMPTION_LOG_MAX >> PREEMPTION_LOG_SHIFT;
par->preempted->logs = calloc(par->preempted->logs_len,
sizeof(ttime_t));
if (!par->yield->logs || !par->preempted->logs)
{
tprintf("Failed to alloc logs buffer\n");
result = -ENOMEM;
break;
}
if (pthread_create(&threads[i], &attr, app_thread, par))
{
tprintf("Failed to craete the thread\n");
result = - ENOSYS;
break;
}
}
error:
if (result)
free_dpdk_apps();
else
num_apps_initiated = num_apps;
return result;
}
void wait_dpdk_done(void)
{
int i, num_apps = num_apps_initiated;
if (!threads)
return;
for (i = 0; i < num_apps; i++)
{
if (threads[i])
{
int jret;
tprintf("Join the app thread %llx\n",
(unsigned long long)threads[i]);
jret = pthread_join(threads[i], NULL);
if (jret)
tprintf("jret failed %x\n", jret);
}
}
free(threads);
threads = NULL;
//num_apps_initiated = 0;
}
void free_dpdk_apps(void)
{
int i, num_apps = num_apps_initiated;
/* In case thread is not terminated yet */
wait_dpdk_done();
if (params)
for (i = 0; i < num_apps; i++)
if (params[i]){
if (params[i]->preempted->logs)
free(params[i]->preempted->logs);
if (params[i]->yield->logs)
free(params[i]->yield->logs);
free(params[i]);
}
}