-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathatc.bpf.c
148 lines (116 loc) · 3.24 KB
/
atc.bpf.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
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
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
char LICENSE[] SEC("license") = "Dual BSD/GPL";
unsigned long tgidpid = 0;
unsigned long cgid = 0;
unsigned long allret = 0;
unsigned long max_exec_slice = 0;
#define INVALID_RET ((unsigned long) -1L)
//#define debug(args...) bpf_printk(args)
#define debug(args...)
SEC("sched/cfs_check_preempt_wakeup")
int BPF_PROG(wakeup, struct task_struct *curr, struct task_struct *p)
{
unsigned long tgidpid1, tgidpid2;
int ret = 0;
if (allret)
return allret;
if (tgidpid) {
tgidpid1 = (unsigned long)curr->tgid << 32 | curr->pid;
tgidpid2 = (unsigned long)p->tgid << 32 | p->pid;
if ((tgidpid1 & tgidpid) == tgidpid)
ret = -1;
else if ((tgidpid2 & tgidpid) == tgidpid)
ret = 1;
if (ret) {
debug("wakeup1 tgid %d pid %d", tgidpid1 >> 32,
tgidpid1 & 0xFFFFFFFF);
debug("wakeup2 tgid %d pid %d", tgidpid2 >> 32,
tgidpid2 & 0xFFFFFFFF);
debug("wakeup ret %d", ret);
}
} else if (cgid) {
if (bpf_sched_entity_belongs_to_cgrp(&curr->se, cgid))
ret = -1;
else if (bpf_sched_entity_belongs_to_cgrp(&p->se, cgid))
ret = 1;
if (ret) {
tgidpid1 = (unsigned long)curr->tgid << 32 | curr->pid;
tgidpid2 = (unsigned long)p->tgid << 32 | p->pid;
debug("wakeup1 tgid %d pid %d", tgidpid1 >> 32,
tgidpid1 & 0xFFFFFFFF);
debug("wakeup2 tgid %d pid %d", tgidpid2 >> 32,
tgidpid2 & 0xFFFFFFFF);
debug("wakeup ret %d", ret);
}
}
return ret;
}
SEC("sched/cfs_wakeup_preempt_entity")
int BPF_PROG(preempt_entity, struct sched_entity *curr, struct sched_entity *se)
{
int ret = 0;
if (allret)
return allret;
if (curr == NULL || se == NULL)
return 0;
/* pid/tgid mode */
if (tgidpid) {
unsigned long tgidpid1, tgidpid2;
tgidpid1 = bpf_sched_entity_to_tgidpid(curr);
tgidpid2 = bpf_sched_entity_to_tgidpid(se);
if ((tgidpid1 & tgidpid) == tgidpid)
ret = -1;
else if ((tgidpid2 & tgidpid) == tgidpid)
ret = 1;
if (ret) {
debug("entity1 tgid %d pid %d", tgidpid1 >> 32,
tgidpid1 & 0xFFFFFFFF);
debug("entity2 tgid %d pid %d", tgidpid2 >> 32,
tgidpid2 & 0xFFFFFFFF);
debug("entity ret %d", ret);
}
/* cgroup id mode */
} else if (cgid) {
if (bpf_sched_entity_belongs_to_cgrp(curr, cgid))
ret = -1;
else if (bpf_sched_entity_belongs_to_cgrp(se, cgid))
ret = 1;
if (ret) {
debug("entity cg %lu", bpf_sched_entity_to_cgrpid(curr));
debug("entity cg %lu", bpf_sched_entity_to_cgrpid(se));
debug("entity cg %d", ret);
}
}
return ret;
}
SEC("sched/cfs_check_preempt_tick")
int BPF_PROG(tick, struct sched_entity *curr, unsigned long delta_exec)
{
unsigned long tgidpid1;
int ret = 0;
if (delta_exec > max_exec_slice)
return 0;
if (allret)
return allret;
if (curr == NULL)
return 0;
/* pid/tgid mode */
if (tgidpid) {
tgidpid1 = bpf_sched_entity_to_tgidpid(curr);
if ((tgidpid1 & tgidpid) == tgidpid)
ret = -1;
if (ret)
debug("tick tgid %d pid %d ret %d", tgidpid1 >> 32,
tgidpid1 & 0xFFFFFFFF, ret);
/* cgroup id mode */
} else if (cgid) {
if (bpf_sched_entity_belongs_to_cgrp(curr, cgid)) {
ret = -1;
debug("tick cg %lu %d", bpf_sched_entity_to_cgrpid(curr), ret);
}
}
return ret;
}