summaryrefslogtreecommitdiffstats
path: root/collectors/ebpf.plugin/ebpf_cgroup.c
blob: 6d7c555bd982962f9aeda14fecd1b0def01b8108 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

#include <sys/resource.h>

#include "ebpf.h"
#include "ebpf_cgroup.h"

ebpf_cgroup_target_t *ebpf_cgroup_pids = NULL;
static void *ebpf_mapped_memory = NULL;
int send_cgroup_chart = 0;

// --------------------------------------------------------------------------------------------------------------------
// Map shared memory

/**
 * Map Shared Memory locally
 *
 * Map the shared memory for current process
 *
 * @param fd       file descriptor returned after shm_open was called.
 * @param length   length of the shared memory
 *
 * @return It returns a pointer to the region mapped on success and MAP_FAILED otherwise.
 */
static inline void *ebpf_cgroup_map_shm_locally(int fd, size_t length)
{
    void *value;

    value =  mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (!value) {
        error("Cannot map shared memory used between eBPF and cgroup, integration between processes won't happen");
        close(shm_fd_ebpf_cgroup);
        shm_fd_ebpf_cgroup = -1;
        shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
    }

    return value;
}

/**
 * Unmap Shared Memory
 *
 * Unmap shared memory used to integrate eBPF and cgroup plugin
 */
void ebpf_unmap_cgroup_shared_memory()
{
    munmap(ebpf_mapped_memory, shm_ebpf_cgroup.header->body_length);
}

/**
 * Map cgroup shared memory
 *
 * Map cgroup shared memory from cgroup to plugin
 */
void ebpf_map_cgroup_shared_memory()
{
    static int limit_try = 0;
    static time_t next_try = 0;

    if (shm_ebpf_cgroup.header || limit_try > NETDATA_EBPF_CGROUP_MAX_TRIES)
        return;

    time_t curr_time = time(NULL);
    if (curr_time < next_try)
        return;

    limit_try++;
    next_try = curr_time + NETDATA_EBPF_CGROUP_NEXT_TRY_SEC;

    if (shm_fd_ebpf_cgroup < 0) {
        shm_fd_ebpf_cgroup = shm_open(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME, O_RDWR, 0660);
        if (shm_fd_ebpf_cgroup < 0) {
            if (limit_try == NETDATA_EBPF_CGROUP_MAX_TRIES)
                error("Shared memory was not initialized, integration between processes won't happen.");

            return;
        }
    }

    // Map only header
    void *mapped = (netdata_ebpf_cgroup_shm_header_t *) ebpf_cgroup_map_shm_locally(shm_fd_ebpf_cgroup,
                                                                                   sizeof(netdata_ebpf_cgroup_shm_header_t));
    if (unlikely(mapped == SEM_FAILED)) {
        return;
    }
    netdata_ebpf_cgroup_shm_header_t *header = mapped;

    size_t length =  header->body_length;

    munmap(header, sizeof(netdata_ebpf_cgroup_shm_header_t));

    if (length <= ((sizeof(netdata_ebpf_cgroup_shm_header_t) + sizeof(netdata_ebpf_cgroup_shm_body_t)))) {
        return;
    }

    ebpf_mapped_memory = (void *)ebpf_cgroup_map_shm_locally(shm_fd_ebpf_cgroup, length);
    if (unlikely(ebpf_mapped_memory == MAP_FAILED)) {
        return;
    }
    shm_ebpf_cgroup.header = ebpf_mapped_memory;
    shm_ebpf_cgroup.body = ebpf_mapped_memory + sizeof(netdata_ebpf_cgroup_shm_header_t);

    shm_sem_ebpf_cgroup = sem_open(NETDATA_NAMED_SEMAPHORE_EBPF_CGROUP_NAME, O_CREAT, 0660, 1);

    if (shm_sem_ebpf_cgroup == SEM_FAILED) {
        error("Cannot create semaphore, integration between eBPF and cgroup won't happen");
        limit_try = NETDATA_EBPF_CGROUP_MAX_TRIES + 1;
        munmap(ebpf_mapped_memory, length);
        shm_ebpf_cgroup.header = NULL;
        shm_ebpf_cgroup.body = NULL;
        close(shm_fd_ebpf_cgroup);
        shm_fd_ebpf_cgroup = -1;
        shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
    }
}

// --------------------------------------------------------------------------------------------------------------------
// Close and Cleanup

/**
 * Clean Specific cgroup pid
 *
 * Clean all PIDs associated with cgroup.
 *
 * @param pt structure pid on target that will have your PRs removed
 */
static inline void ebpf_clean_specific_cgroup_pids(struct pid_on_target2 *pt)
{
    while (pt) {
        struct pid_on_target2 *next_pid = pt->next;

        freez(pt);
        pt = next_pid;
    }
}

/**
 * Remove Cgroup Update Target Update List
 *
 * Remove from cgroup target and update the link list
 */
static void ebpf_remove_cgroup_target_update_list()
{
    ebpf_cgroup_target_t *next, *ect = ebpf_cgroup_pids;
    ebpf_cgroup_target_t *prev = ebpf_cgroup_pids;
    while (ect) {
        next = ect->next;
        if (!ect->updated) {
            if (ect == ebpf_cgroup_pids) {
                ebpf_cgroup_pids = next;
                prev = next;
            } else {
                prev->next = next;
            }

            ebpf_clean_specific_cgroup_pids(ect->pids);
            freez(ect);
        } else {
            prev = ect;
        }

        ect = next;
    }
}

// --------------------------------------------------------------------------------------------------------------------
// Fill variables

/**
 * Set Target Data
 *
 * Set local variable values according shared memory information.
 *
 * @param out local output variable.
 * @param ptr input from shared memory.
 */
static inline void ebpf_cgroup_set_target_data(ebpf_cgroup_target_t *out, netdata_ebpf_cgroup_shm_body_t *ptr)
{
    out->hash = ptr->hash;
    snprintfz(out->name, 255, "%s", ptr->name);
    out->systemd = ptr->options & CGROUP_OPTIONS_SYSTEM_SLICE_SERVICE;
    out->updated = 1;
}

/**
 * Find or create
 *
 * Find the structure inside the link list or allocate and link when it is not present.
 *
 * @param ptr Input from shared memory.
 *
 * @return It returns a pointer for the structure associated with the input.
 */
static ebpf_cgroup_target_t * ebpf_cgroup_find_or_create(netdata_ebpf_cgroup_shm_body_t *ptr)
{
    ebpf_cgroup_target_t *ect, *prev;
    for (ect = ebpf_cgroup_pids, prev = ebpf_cgroup_pids; ect; prev = ect, ect = ect->next) {
        if (ect->hash == ptr->hash && !strcmp(ect->name, ptr->name)) {
            ect->updated = 1;
            return ect;
        }
    }

    ebpf_cgroup_target_t *new_ect = callocz(1, sizeof(ebpf_cgroup_target_t));

    ebpf_cgroup_set_target_data(new_ect, ptr);
    if (!ebpf_cgroup_pids) {
        ebpf_cgroup_pids = new_ect;
    } else {
        prev->next = new_ect;
    }

    return new_ect;
}

/**
 * Update pid link list
 *
 * Update PIDs list associated with specific cgroup.
 *
 * @param ect  cgroup structure where pids will be stored
 * @param path file with PIDs associated to cgroup.
 */
static void ebpf_update_pid_link_list(ebpf_cgroup_target_t *ect, char *path)
{
    procfile *ff = procfile_open_no_log(path, " \t:", PROCFILE_FLAG_DEFAULT);
    if (!ff)
        return;

    ff = procfile_readall(ff);
    if (!ff)
        return;

    size_t lines = procfile_lines(ff), l;
    for (l = 0; l < lines ;l++) {
        int pid = (int)str2l(procfile_lineword(ff, l, 0));
        if (pid) {
            struct pid_on_target2 *pt, *prev;
            for (pt = ect->pids, prev = ect->pids; pt; prev = pt, pt = pt->next) {
                if (pt->pid == pid)
                    break;
            }

            if (!pt) {
                struct pid_on_target2 *w = callocz(1, sizeof(struct pid_on_target2));
                w->pid = pid;
                if (!ect->pids)
                    ect->pids = w;
                else
                    prev->next = w;
            }
        }
    }

    procfile_close(ff);
}

/**
 * Set remove var
 *
 * Set variable remove. If this variable is not reset, the structure will be removed from link list.
 */
void ebpf_reset_updated_var()
 {
     ebpf_cgroup_target_t *ect;
     for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
         ect->updated = 0;
     }
 }

/**
 * Parse cgroup shared memory
 *
 * This function is responsible to copy necessary data from shared memory to local memory.
 */
void ebpf_parse_cgroup_shm_data()
{
    static int previous = 0;
    if (!shm_ebpf_cgroup.header || shm_sem_ebpf_cgroup == SEM_FAILED)
        return;

    sem_wait(shm_sem_ebpf_cgroup);
    int i, end = shm_ebpf_cgroup.header->cgroup_root_count;
    if (end <= 0) {
        sem_post(shm_sem_ebpf_cgroup);
        return;
    }

    pthread_mutex_lock(&mutex_cgroup_shm);
    ebpf_remove_cgroup_target_update_list();

    ebpf_reset_updated_var();

    for (i = 0; i < end; i++) {
        netdata_ebpf_cgroup_shm_body_t *ptr = &shm_ebpf_cgroup.body[i];
        if (ptr->enabled) {
            ebpf_cgroup_target_t *ect =  ebpf_cgroup_find_or_create(ptr);
            ebpf_update_pid_link_list(ect, ptr->path);
        }
    }
    send_cgroup_chart = previous != shm_ebpf_cgroup.header->cgroup_root_count;
    previous = shm_ebpf_cgroup.header->cgroup_root_count;
    sem_post(shm_sem_ebpf_cgroup);
    pthread_mutex_unlock(&mutex_cgroup_shm);
#ifdef NETDATA_DEV_MODE
    info("Updating cgroup %d (Previous: %d, Current: %d)",
         send_cgroup_chart, previous, shm_ebpf_cgroup.header->cgroup_root_count);
#endif

    sem_post(shm_sem_ebpf_cgroup);
}

// --------------------------------------------------------------------------------------------------------------------
// Create charts

/**
 * Create charts on systemd submenu
 *
 * @param id   the chart id
 * @param title  the value displayed on vertical axis.
 * @param units  the value displayed on vertical axis.
 * @param family Submenu that the chart will be attached on dashboard.
 * @param charttype chart type
 * @param order  the chart order
 * @param algorithm the algorithm used by dimension
 * @param context   add context for chart
 * @param module    chart module name, this is the eBPF thread.
 * @param update_every value to overwrite the update frequency set by the server.
 */
void ebpf_create_charts_on_systemd(char *id, char *title, char *units, char *family, char *charttype, int order,
                                   char *algorithm, char *context, char *module, int update_every)
{
    ebpf_cgroup_target_t *w;
    ebpf_write_chart_cmd(NETDATA_SERVICE_FAMILY, id, title, units, family, charttype, context,
                         order, update_every, module);

    for (w = ebpf_cgroup_pids; w; w = w->next) {
        if (unlikely(w->systemd) && unlikely(w->updated))
            fprintf(stdout, "DIMENSION %s '' %s 1 1\n", w->name, algorithm);
    }
}

// --------------------------------------------------------------------------------------------------------------------
// Cgroup main thread

/**
 * CGROUP exit
 *
 * Clean up the main thread.
 *
 * @param ptr thread data.
 */
static void ebpf_cgroup_exit(void *ptr)
{
    UNUSED(ptr);
}

/**
 * Cgroup integratin
 *
 * Thread responsible to call functions responsible to sync data between plugins.
 *
 * @param ptr It is a NULL value for this thread.
 *
 * @return It always returns NULL.
 */
void *ebpf_cgroup_integration(void *ptr)
{
    netdata_thread_cleanup_push(ebpf_cgroup_exit, ptr);

    usec_t step = USEC_PER_SEC;
    int counter = NETDATA_EBPF_CGROUP_UPDATE - 1;
    heartbeat_t hb;
    heartbeat_init(&hb);
    //Plugin will be killed when it receives a signal
    while (!ebpf_exit_plugin) {
        (void)heartbeat_next(&hb, step);

        // We are using a small heartbeat time to wake up thread,
        // but we should not update so frequently the shared memory data
        if (++counter >=  NETDATA_EBPF_CGROUP_UPDATE) {
            counter = 0;
            if (!shm_ebpf_cgroup.header)
                ebpf_map_cgroup_shared_memory();
            else
                ebpf_parse_cgroup_shm_data();
        }
    }

    netdata_thread_cleanup_pop(1);
    return NULL;
}