-
Notifications
You must be signed in to change notification settings - Fork 0
/
duriand.c
199 lines (181 loc) · 6.12 KB
/
duriand.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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <bpf/bpf.h>
#include <trace_helpers.h>
#include <bpf_load.h>
#include <hiredis/hiredis.h>
#include "durian_maps.h"
#include "durian_common_user_bpf.h"
#include "durian_common_user_debug.h"
#include "durian_repository.h"
static int handle_rb_event(void *ctx, void *data, size_t data_size)
{
struct durian_ctx *ctx_data = (struct durian_ctx *)ctx;
const struct sched_event_data_t *e = data;
if (e->prev_task_state == __TASK_STOPPED && e->next_task_state == TASK_RUNNING_RQ)
{
/* Task started */
repository_track_task(ctx_data, e->pid, e->comm, e->prio, e->ktime_ns);
}
else if (e->prev_task_state == TASK_RUNNING_CPU && e->next_task_state == __TASK_STOPPED)
{
/* Task terminated */
repository_untrack_task(ctx_data, e->pid, e->ktime_ns);
}
else if (e->prev_task_state == TASK_RUNNING_CPU && e->next_task_state == TASK_RUNNING_RQ)
{
/* Task switched out */
repository_update_stats_task_exits_cpu(ctx_data, e->pid, e->ktime_ns, e->trace_sched_switch_state);
}
else if (e->prev_task_state == TASK_RUNNING_RQ && e->next_task_state == TASK_RUNNING_CPU)
{
/* Task switched in */
repository_update_stats_task_enters_cpu(ctx_data, e->pid, e->comm, e->prio, e->ktime_ns);
}
else if (e->prev_task_state == TASK_WAITING && e->next_task_state == TASK_RUNNING_RQ)
{
/* Task exited a wait queue and enqueued to run queue */
repository_update_stats_task_wait_ends(ctx_data, e->pid, e->ktime_ns);
}
else
{
printf("[%d:%s] task state change monitoring for prev: %s next: %s is not supported\n", e->pid, e->comm, get_task_state_name(e->prev_task_state), get_task_state_name(e->next_task_state));
}
return 0;
}
int main(int argc, char **argv)
{
struct bpf_object *bpf_obj;
int err, fd, pinned;
/**
* Connect to redis for storing persistent task statistics
*/
redisContext *redis_context = redisConnect("localhost", 6379);
if (redis_context == NULL || redis_context->err)
{
printf("Error: %s\n", redis_context == NULL ? "connection error" : redis_context->errstr);
return -1;
}
durian_ctx *ctx = malloc(sizeof(durian_ctx));
ctx->redis_context = redis_context;
ctx->success = 1;
ctx->redis_cmd_cnt = 0;
/* Preload lua scripts */
err = load_transaction_script(ctx, lua_script_track_task, lua_script_track_task_sha1_hash);
if (err != 0)
{
return -1;
}
err = load_transaction_script(ctx, lua_script_untrack_task, lua_script_untrack_task_sha1_hash);
if (err != 0)
{
return -1;
}
err = load_transaction_script(ctx, lua_script_update_stats_task_exits_cpu, lua_script_update_stats_task_exits_cpu_sha1_hash);
if (err != 0)
{
return -1;
}
err = load_transaction_script(ctx, lua_script_update_stats_task_enters_cpu, lua_script_update_stats_task_enters_cpu_sha1_hash);
if (err != 0)
{
return -1;
}
err = load_transaction_script(ctx, lua_script_update_stats_task_wait_ends, lua_script_update_stats_task_wait_ends_sha1_hash);
if (err != 0)
{
return -1;
}
pinned = bpf_obj_get(sched_event_map_file_path);
if (pinned < 0)
{
printf("Failed to find bpf object at %s: %s\n", sched_event_map_file_path, strerror(errno));
fd = bpf_create_map_name(BPF_MAP_TYPE_RINGBUF, "sched_events", 0, 0, SCHED_EVENT_RINGBUF_SIZE, 0);
if (fd < 0)
{
printf("Failed to create map: %d (%s)\n", fd, strerror(errno));
return -1;
}
pinned = bpf_obj_pin(fd, sched_event_map_file_path);
if (pinned < 0)
{
printf("Failed to pin map to the file system: %d (%s)\n", pinned, strerror(errno));
return -1;
}
}
bpf_obj = load_bpf_and_tracepoint_attach("durian_probes/sched/sched_wakeup_new.o", pin_basedir);
if (!bpf_obj)
{
printf("The kernel didn't load the BPF program: %s\n", strerror(errno));
return -1;
}
bpf_obj = load_bpf_and_tracepoint_attach("durian_probes/sched/sched_wakeup.o", pin_basedir);
if (!bpf_obj)
{
printf("The kernel didn't load the BPF program: %s\n", strerror(errno));
return -1;
}
bpf_obj = load_bpf_and_tracepoint_attach("durian_probes/sched/sched_switch.o", pin_basedir);
if (!bpf_obj)
{
printf("The kernel didn't load the BPF program: %s\n", strerror(errno));
return -1;
}
bpf_obj = load_bpf_and_tracepoint_attach("durian_probes/sched/sched_process_exit.o", pin_basedir);
if (!bpf_obj)
{
printf("The kernel didn't load the BPF program: %s\n", strerror(errno));
return -1;
}
/**
* Start of RB , will migrate to either Go or Rust in the future
*/
struct ring_buffer *rb;
fd = bpf_obj_get(sched_event_map_file_path);
rb = ring_buffer__new(fd, handle_rb_event, ctx, NULL);
if (!rb)
{
printf("Failed to create ring buffer %s\n", strerror(errno));
return -1;
}
while (true)
{
err = ring_buffer__consume(rb);
if (err < 0)
{
printf("Error polling ring buffer: %d\n", err);
break;
}
/**
* Submit single batch statistics to redis and read the replies
*/
redisReply *reply;
for (int i = 0; i < ctx->redis_cmd_cnt; i++)
{
int status = redisGetReply(redis_context, (void **)&reply);
if (status != REDIS_OK)
{
// Handle error
fprintf(stderr, "Error while reading redis reply: %d %s\n", status, ctx->redis_context->errstr);
}
if (reply != NULL)
{
// Process reply
// ...
if (reply->type == REDIS_REPLY_ERROR)
{
printf("%s\n", reply->str);
}
}
freeReplyObject(reply);
}
ctx->redis_cmd_cnt = 0;
}
/**
* End of RB Testing
*/
// read_trace_pipe();
return 0;
}