forked from tsuna/contextswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timectxswws.c
105 lines (97 loc) · 2.83 KB
/
timectxswws.c
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
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <linux/futex.h>
static inline long long unsigned time_ns(struct timespec* const ts) {
if (clock_gettime(CLOCK_REALTIME, ts)) {
exit(1);
}
return ((long long unsigned) ts->tv_sec) * 1000000000LLU
+ (long long unsigned) ts->tv_nsec;
}
static inline int get_iterations(int ws_pages) {
int iterations = 1000;
while (iterations * ws_pages * 4096L < 4294967296L) { // 4GB
iterations += 1000;
}
return iterations;
}
int main(int argc, char** argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s <size of working set in 4K pages>\n", *argv);
return 1;
}
const long ws_pages = strtol(argv[1], NULL, 10);
if (ws_pages < 0) {
fprintf(stderr, "Invalid usage: working set size must be positive\n");
return 1;
}
const int iterations = get_iterations(ws_pages);
struct timespec ts;
long long unsigned memset_time = 0;
if (ws_pages) {
void* buf = malloc(ws_pages * 4096);
memset_time = time_ns(&ts);
for (int i = 0; i < iterations; i++) {
memset(buf, i, ws_pages * 4096);
}
memset_time = time_ns(&ts) - memset_time;
printf("%i memset on %4liK in %10lluns (%.1fns/page)\n",
iterations, ws_pages * 4, memset_time,
(memset_time / ((float) ws_pages * iterations)));
free(buf);
}
const int shm_id = shmget(IPC_PRIVATE, (ws_pages + 1) * 4096,
IPC_CREAT | 0666);
const pid_t other = fork();
int* futex = shmat(shm_id, NULL, 0);
void* ws = ((char *) futex) + 4096;
*futex = 0xA;
if (other == 0) {
for (int i = 0; i < iterations; i++) {
sched_yield();
while (syscall(SYS_futex, futex, FUTEX_WAIT, 0xA, NULL, NULL, 42)) {
// retry
sched_yield();
}
*futex = 0xB;
if (ws_pages) {
memset(ws, i, ws_pages * 4096);
}
while (!syscall(SYS_futex, futex, FUTEX_WAKE, 1, NULL, NULL, 42)) {
// retry
sched_yield();
}
}
return 0;
}
const long long unsigned start_ns = time_ns(&ts);
for (int i = 0; i < iterations; i++) {
*futex = 0xA;
if (ws_pages) {
memset(ws, i, ws_pages * 4096);
}
while (!syscall(SYS_futex, futex, FUTEX_WAKE, 1, NULL, NULL, 42)) {
// retry
sched_yield();
}
sched_yield();
while (syscall(SYS_futex, futex, FUTEX_WAIT, 0xB, NULL, NULL, 42)) {
// retry
sched_yield();
}
}
const long long unsigned delta = time_ns(&ts) - start_ns - memset_time * 2;
const int nswitches = iterations * 4;
printf("%i process context switches (wss:%4liK) in %12lluns (%.1fns/ctxsw)\n",
nswitches, ws_pages * 4, delta, (delta / (float) nswitches));
wait(futex);
return 0;
}