-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace_sched.c
92 lines (73 loc) · 1.75 KB
/
trace_sched.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
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
int main()
{
int pipe1[2], pipe2[2];
char tracebuf[256];
cpu_set_t cpus;
int buf = 65;
int tracefd;
int fd, len;
if (pipe(pipe1) || pipe(pipe2)) {
perror("pipe");
return -1;
}
tracefd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY);
if (tracefd < 0) {
perror("open trace_marker");
return -1;
}
fd = fork();
if (fd == -1) {
perror("fork");
return -1;
}
CPU_ZERO(&cpus);
if (fd == 0) {
len = snprintf(tracebuf, 256, "in child");
write(tracefd, &tracebuf, len);
CPU_SET(6, &cpus);
if (sched_setaffinity(0, sizeof(&cpus), &cpus)) {
perror("sched_setaffinity");
return -1;
}
close(pipe1[1]);
close(pipe2[0]);
len = snprintf(tracebuf, 256, "child read pipe1");
write(tracefd, &tracebuf, len);
read(pipe1[0], &buf, 1);
close(pipe1[0]);
len = snprintf(tracebuf, 256, "child write pipe2");
write(tracefd, &tracebuf, len);
write(pipe2[1], &buf, 1);
close(pipe2[1]);
len = snprintf(tracebuf, 256, "child done");
write(tracefd, &tracebuf, len);
} else {
len = snprintf(tracebuf, 256, "in parent");
write(tracefd, &tracebuf, len);
CPU_SET(4, &cpus);
if (sched_setaffinity(0, sizeof(&cpus), &cpus)) {
perror("sched_setaffinity");
return -1;
}
close(pipe1[0]);
close(pipe2[1]);
len = snprintf(tracebuf, 256, "parent write pipe1");
write(tracefd, &tracebuf, len);
write(pipe1[1], &buf, 1);
close(pipe1[1]);
len = snprintf(tracebuf, 256, "parent read pipe2");
write(tracefd, &tracebuf, len);
read(pipe2[0], &buf, 1);
close(pipe2[0]);
len = snprintf(tracebuf, 256, "parent done");
write(tracefd, &tracebuf, len);
}
return 0;
}