forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebpf_process.c
1071 lines (923 loc) · 36.2 KB
/
ebpf_process.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-3.0-or-later
#include <sys/resource.h>
#include "ebpf.h"
#include "ebpf_process.h"
/*****************************************************************
*
* GLOBAL VARIABLES
*
*****************************************************************/
static char *process_dimension_names[NETDATA_MAX_MONITOR_VECTOR] = { "open", "close", "delete", "read", "write",
"process", "task", "process", "thread" };
static char *process_id_names[NETDATA_MAX_MONITOR_VECTOR] = { "do_sys_open", "__close_fd", "vfs_unlink",
"vfs_read", "vfs_write", "do_exit",
"release_task", "_do_fork", "sys_clone" };
static char *status[] = { "process", "zombie" };
static netdata_idx_t *process_hash_values = NULL;
static netdata_syscall_stat_t *process_aggregated_data = NULL;
static netdata_publish_syscall_t *process_publish_aggregated = NULL;
static ebpf_data_t process_data;
ebpf_process_stat_t **global_process_stats = NULL;
ebpf_process_publish_apps_t **current_apps_data = NULL;
ebpf_process_publish_apps_t **prev_apps_data = NULL;
int process_enabled = 0;
static int *map_fd = NULL;
static struct bpf_object *objects = NULL;
static struct bpf_link **probe_links = NULL;
/*****************************************************************
*
* PROCESS DATA AND SEND TO NETDATA
*
*****************************************************************/
/**
* Update publish structure before to send data to Netdata.
*
* @param publish the first output structure with independent dimensions
* @param pvc the second output structure with correlated dimensions
* @param input the structure with the input data.
*/
static void ebpf_update_global_publish(
netdata_publish_syscall_t *publish, netdata_publish_vfs_common_t *pvc, netdata_syscall_stat_t *input)
{
netdata_publish_syscall_t *move = publish;
while (move) {
if (input->call != move->pcall) {
//This condition happens to avoid initial values with dimensions higher than normal values.
if (move->pcall) {
move->ncall = (input->call > move->pcall) ? input->call - move->pcall : move->pcall - input->call;
move->nbyte = (input->bytes > move->pbyte) ? input->bytes - move->pbyte : move->pbyte - input->bytes;
move->nerr = (input->ecall > move->nerr) ? input->ecall - move->perr : move->perr - input->ecall;
} else {
move->ncall = 0;
move->nbyte = 0;
move->nerr = 0;
}
move->pcall = input->call;
move->pbyte = input->bytes;
move->perr = input->ecall;
} else {
move->ncall = 0;
move->nbyte = 0;
move->nerr = 0;
}
input = input->next;
move = move->next;
}
pvc->write = -((long)publish[2].nbyte);
pvc->read = (long)publish[3].nbyte;
pvc->running = (long)publish[7].ncall - (long)publish[8].ncall;
publish[6].ncall = -publish[6].ncall; // release
pvc->zombie = (long)publish[5].ncall + (long)publish[6].ncall;
}
/**
* Update apps dimension to publish.
*
* @param curr Last values read from memory.
* @param prev Previous values read from memory.
* @param first was it allocated now?
*/
static void
ebpf_process_update_apps_publish(ebpf_process_publish_apps_t *curr, ebpf_process_publish_apps_t *prev, int first)
{
if (first)
return;
curr->publish_open = curr->call_sys_open - prev->call_sys_open;
curr->publish_closed = curr->call_close_fd - prev->call_close_fd;
curr->publish_deleted = curr->call_vfs_unlink - prev->call_vfs_unlink;
curr->publish_write_call = curr->call_write - prev->call_write;
curr->publish_write_bytes = curr->bytes_written - prev->bytes_written;
curr->publish_read_call = curr->call_read - prev->call_read;
curr->publish_read_bytes = curr->bytes_read - prev->bytes_read;
curr->publish_process = curr->call_do_fork - prev->call_do_fork;
curr->publish_thread = curr->call_sys_clone - prev->call_sys_clone;
curr->publish_task = curr->call_release_task - prev->call_release_task;
curr->publish_open_error = curr->ecall_sys_open - prev->ecall_sys_open;
curr->publish_close_error = curr->ecall_close_fd - prev->ecall_close_fd;
curr->publish_write_error = curr->ecall_write - prev->ecall_write;
curr->publish_read_error = curr->ecall_read - prev->ecall_read;
}
/**
* Call the necessary functions to create a chart.
*
* @param family the chart family
* @param move the pointer with the values that will be published
*/
static void write_status_chart(char *family, netdata_publish_vfs_common_t *pvc)
{
write_begin_chart(family, NETDATA_PROCESS_STATUS_NAME);
write_chart_dimension(status[0], (long long)pvc->running);
write_chart_dimension(status[1], (long long)pvc->zombie);
write_end_chart();
}
/**
* Send data to Netdata calling auxiliar functions.
*
* @param em the structure with thread information
*/
static void ebpf_process_send_data(ebpf_module_t *em)
{
netdata_publish_vfs_common_t pvc;
ebpf_update_global_publish(process_publish_aggregated, &pvc, process_aggregated_data);
write_count_chart(
NETDATA_FILE_OPEN_CLOSE_COUNT, NETDATA_EBPF_FAMILY, process_publish_aggregated, 2);
write_count_chart(
NETDATA_VFS_FILE_CLEAN_COUNT, NETDATA_EBPF_FAMILY, &process_publish_aggregated[NETDATA_DEL_START], 1);
write_count_chart(
NETDATA_VFS_FILE_IO_COUNT, NETDATA_EBPF_FAMILY, &process_publish_aggregated[NETDATA_IN_START_BYTE], 2);
write_count_chart(
NETDATA_EXIT_SYSCALL, NETDATA_EBPF_FAMILY, &process_publish_aggregated[NETDATA_EXIT_START], 2);
write_count_chart(
NETDATA_PROCESS_SYSCALL, NETDATA_EBPF_FAMILY, &process_publish_aggregated[NETDATA_PROCESS_START], 2);
write_status_chart(NETDATA_EBPF_FAMILY, &pvc);
if (em->mode < MODE_ENTRY) {
write_err_chart(
NETDATA_FILE_OPEN_ERR_COUNT, NETDATA_EBPF_FAMILY, process_publish_aggregated, 2);
write_err_chart(
NETDATA_VFS_FILE_ERR_COUNT, NETDATA_EBPF_FAMILY, &process_publish_aggregated[2], NETDATA_VFS_ERRORS);
write_err_chart(
NETDATA_PROCESS_ERROR_NAME, NETDATA_EBPF_FAMILY, &process_publish_aggregated[NETDATA_PROCESS_START], 2);
}
write_io_chart(NETDATA_VFS_IO_FILE_BYTES, NETDATA_EBPF_FAMILY, process_id_names[3], process_id_names[4], &pvc);
}
/**
* Sum values for pid
*
* @param root the structure with all available PIDs
*
* @param offset the address that we are reading
*
* @return it returns the sum of all PIDs
*/
long long ebpf_process_sum_values_for_pids(struct pid_on_target *root, size_t offset)
{
long long ret = 0;
while (root) {
int32_t pid = root->pid;
ebpf_process_publish_apps_t *w = current_apps_data[pid];
if (w) {
ret += get_value_from_structure((char *)w, offset);
}
root = root->next;
}
return ret;
}
/**
* Remove process pid
*
* Remove from PID task table when task_release was called.
*/
void ebpf_process_remove_pids()
{
struct pid_stat *pids = root_of_pids;
int pid_fd = map_fd[0];
while (pids) {
uint32_t pid = pids->pid;
ebpf_process_stat_t *w = global_process_stats[pid];
if (w) {
if (w->removeme) {
freez(w);
global_process_stats[pid] = NULL;
bpf_map_delete_elem(pid_fd, &pid);
}
}
pids = pids->next;
}
}
/**
* Send data to Netdata calling auxiliar functions.
*
* @param em the structure with thread information
* @param root the target list.
*/
void ebpf_process_send_apps_data(ebpf_module_t *em, struct target *root)
{
struct target *w;
collected_number value;
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_OPEN);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_open));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
if (em->mode < MODE_ENTRY) {
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_OPEN_ERROR);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_process_sum_values_for_pids(
w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_open_error));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
}
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_CLOSED);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value =
ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_closed));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
if (em->mode < MODE_ENTRY) {
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_CLOSE_ERROR);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_process_sum_values_for_pids(
w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_close_error));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
}
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_DELETED);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value =
ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_deleted));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_WRITE_CALLS);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_process_sum_values_for_pids(
w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_write_call));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
if (em->mode < MODE_ENTRY) {
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_WRITE_CALLS_ERROR);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_process_sum_values_for_pids(
w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_write_error));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
}
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_READ_CALLS);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value =
ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_read_call));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
if (em->mode < MODE_ENTRY) {
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_READ_CALLS_ERROR);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_process_sum_values_for_pids(
w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_read_error));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
}
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_WRITE_BYTES);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_process_sum_values_for_pids(
w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_write_bytes));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_VFS_READ_BYTES);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_process_sum_values_for_pids(
w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_read_bytes));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_PROCESS);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value =
ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_process));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_THREAD);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value =
ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_thread));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_CLOSE);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, publish_task));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
ebpf_process_remove_pids();
}
/*****************************************************************
*
* READ INFORMATION FROM KERNEL RING
*
*****************************************************************/
/**
* Read the hash table and store data to allocated vectors.
*/
static void read_hash_global_tables()
{
uint64_t idx;
netdata_idx_t res[NETDATA_GLOBAL_VECTOR];
netdata_idx_t *val = process_hash_values;
for (idx = 0; idx < NETDATA_GLOBAL_VECTOR; idx++) {
if (!bpf_map_lookup_elem(map_fd[1], &idx, val)) {
uint64_t total = 0;
int i;
int end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
for (i = 0; i < end; i++)
total += val[i];
res[idx] = total;
} else {
res[idx] = 0;
}
}
process_aggregated_data[0].call = res[NETDATA_KEY_CALLS_DO_SYS_OPEN];
process_aggregated_data[1].call = res[NETDATA_KEY_CALLS_CLOSE_FD];
process_aggregated_data[2].call = res[NETDATA_KEY_CALLS_VFS_UNLINK];
process_aggregated_data[3].call = res[NETDATA_KEY_CALLS_VFS_READ] + res[NETDATA_KEY_CALLS_VFS_READV];
process_aggregated_data[4].call = res[NETDATA_KEY_CALLS_VFS_WRITE] + res[NETDATA_KEY_CALLS_VFS_WRITEV];
process_aggregated_data[5].call = res[NETDATA_KEY_CALLS_DO_EXIT];
process_aggregated_data[6].call = res[NETDATA_KEY_CALLS_RELEASE_TASK];
process_aggregated_data[7].call = res[NETDATA_KEY_CALLS_DO_FORK];
process_aggregated_data[8].call = res[NETDATA_KEY_CALLS_SYS_CLONE];
process_aggregated_data[0].ecall = res[NETDATA_KEY_ERROR_DO_SYS_OPEN];
process_aggregated_data[1].ecall = res[NETDATA_KEY_ERROR_CLOSE_FD];
process_aggregated_data[2].ecall = res[NETDATA_KEY_ERROR_VFS_UNLINK];
process_aggregated_data[3].ecall = res[NETDATA_KEY_ERROR_VFS_READ] + res[NETDATA_KEY_ERROR_VFS_READV];
process_aggregated_data[4].ecall = res[NETDATA_KEY_ERROR_VFS_WRITE] + res[NETDATA_KEY_ERROR_VFS_WRITEV];
process_aggregated_data[7].ecall = res[NETDATA_KEY_ERROR_DO_FORK];
process_aggregated_data[8].ecall = res[NETDATA_KEY_ERROR_SYS_CLONE];
process_aggregated_data[2].bytes = (uint64_t)res[NETDATA_KEY_BYTES_VFS_WRITE] +
(uint64_t)res[NETDATA_KEY_BYTES_VFS_WRITEV];
process_aggregated_data[3].bytes = (uint64_t)res[NETDATA_KEY_BYTES_VFS_READ] +
(uint64_t)res[NETDATA_KEY_BYTES_VFS_READV];
}
/**
* Read the hash table and store data to allocated vectors.
*/
static void ebpf_process_update_apps_data()
{
struct pid_stat *pids = root_of_pids;
while (pids) {
uint32_t current_pid = pids->pid;
ebpf_process_stat_t *ps = global_process_stats[current_pid];
if (!ps) {
pids = pids->next;
continue;
}
ebpf_process_publish_apps_t *cad = current_apps_data[current_pid];
ebpf_process_publish_apps_t *pad = prev_apps_data[current_pid];
int lstatus;
if (!cad) {
ebpf_process_publish_apps_t *ptr = callocz(2, sizeof(ebpf_process_publish_apps_t));
cad = &ptr[0];
current_apps_data[current_pid] = cad;
pad = &ptr[1];
prev_apps_data[current_pid] = pad;
lstatus = 1;
} else {
memcpy(pad, cad, sizeof(ebpf_process_publish_apps_t));
lstatus = 0;
}
//Read data
cad->call_sys_open = ps->open_call;
cad->call_close_fd = ps->close_call;
cad->call_vfs_unlink = ps->unlink_call;
cad->call_read = ps->read_call + ps->readv_call;
cad->call_write = ps->write_call + ps->writev_call;
cad->call_do_exit = ps->exit_call;
cad->call_release_task = ps->release_call;
cad->call_do_fork = ps->fork_call;
cad->call_sys_clone = ps->clone_call;
cad->ecall_sys_open = ps->open_err;
cad->ecall_close_fd = ps->close_err;
cad->ecall_vfs_unlink = ps->unlink_err;
cad->ecall_read = ps->read_err + ps->readv_err;
cad->ecall_write = ps->write_err + ps->writev_err;
cad->ecall_do_fork = ps->fork_err;
cad->ecall_sys_clone = ps->clone_err;
cad->bytes_written = (uint64_t)ps->write_bytes + (uint64_t)ps->write_bytes;
cad->bytes_read = (uint64_t)ps->read_bytes + (uint64_t)ps->readv_bytes;
ebpf_process_update_apps_publish(cad, pad, lstatus);
pids = pids->next;
}
}
/*****************************************************************
*
* FUNCTIONS TO CREATE CHARTS
*
*****************************************************************/
/**
* Create IO chart
*
* @param family the chart family
* @param name the chart name
* @param axis the axis label
* @param web the group name used to attach the chart on dashaboard
* @param order the order number of the specified chart
*/
static void ebpf_create_io_chart(char *family, char *name, char *axis, char *web, int order)
{
printf("CHART %s.%s '' 'Bytes written and read' '%s' '%s' '' line %d %d\n",
family,
name,
axis,
web,
order,
update_every);
printf("DIMENSION %s %s absolute 1 1\n", process_id_names[3], NETDATA_VFS_DIM_OUT_FILE_BYTES);
printf("DIMENSION %s %s absolute 1 1\n", process_id_names[4], NETDATA_VFS_DIM_IN_FILE_BYTES);
}
/**
* Create process status chart
*
* @param family the chart family
* @param name the chart name
* @param axis the axis label
* @param web the group name used to attach the chart on dashaboard
* @param order the order number of the specified chart
*/
static void ebpf_process_status_chart(char *family, char *name, char *axis, char *web, int order)
{
printf("CHART %s.%s '' 'Process not closed' '%s' '%s' '' line %d %d ''\n",
family,
name,
axis,
web,
order,
update_every);
printf("DIMENSION %s '' absolute 1 1\n", status[0]);
printf("DIMENSION %s '' absolute 1 1\n", status[1]);
}
/**
* Create global charts
*
* Call ebpf_create_chart to create the charts for the collector.
*
* @param em a pointer to the structure with the default values.
*/
static void ebpf_create_global_charts(ebpf_module_t *em)
{
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_FILE_OPEN_CLOSE_COUNT,
"Open and close calls",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_FILE_GROUP,
21000,
ebpf_create_global_dimension,
process_publish_aggregated,
2);
if (em->mode < MODE_ENTRY) {
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_FILE_OPEN_ERR_COUNT,
"Open fails",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_FILE_GROUP,
21001,
ebpf_create_global_dimension,
process_publish_aggregated,
2);
}
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_VFS_FILE_CLEAN_COUNT,
"Remove files",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_VFS_GROUP,
21002,
ebpf_create_global_dimension,
&process_publish_aggregated[NETDATA_DEL_START],
1);
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_VFS_FILE_IO_COUNT,
"Calls to IO",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_VFS_GROUP,
21003,
ebpf_create_global_dimension,
&process_publish_aggregated[NETDATA_IN_START_BYTE],
2);
ebpf_create_io_chart(NETDATA_EBPF_FAMILY,
NETDATA_VFS_IO_FILE_BYTES,
EBPF_COMMON_DIMENSION_BYTESS,
NETDATA_VFS_GROUP,
21004);
if (em->mode < MODE_ENTRY) {
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_VFS_FILE_ERR_COUNT,
"Fails to write or read",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_VFS_GROUP,
21005,
ebpf_create_global_dimension,
&process_publish_aggregated[2],
NETDATA_VFS_ERRORS);
}
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_PROCESS_SYSCALL,
"Start process",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_PROCESS_GROUP,
21006,
ebpf_create_global_dimension,
&process_publish_aggregated[NETDATA_PROCESS_START],
2);
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_EXIT_SYSCALL,
"Exit process",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_PROCESS_GROUP,
21007,
ebpf_create_global_dimension,
&process_publish_aggregated[NETDATA_EXIT_START],
2);
ebpf_process_status_chart(NETDATA_EBPF_FAMILY,
NETDATA_PROCESS_STATUS_NAME,
EBPF_COMMON_DIMENSION_DIFFERENCE,
NETDATA_PROCESS_GROUP,
21008);
if (em->mode < MODE_ENTRY) {
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_PROCESS_ERROR_NAME,
"Fails to create process",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_PROCESS_GROUP,
21009,
ebpf_create_global_dimension,
&process_publish_aggregated[NETDATA_PROCESS_START],
2);
}
}
/**
* Create process apps charts
*
* Call ebpf_create_chart to create the charts on apps submenu.
*
* @param em a pointer to the structure with the default values.
* @param root a pointer for the targets.
*/
static void ebpf_process_create_apps_charts(ebpf_module_t *em, struct target *root)
{
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_OPEN,
"Number of open files",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_FILE_GROUP,
20061,
root);
if (em->mode < MODE_ENTRY) {
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_OPEN_ERROR,
"Fails to open files",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_FILE_GROUP,
20062,
root);
}
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_CLOSED,
"Files closed",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_FILE_GROUP,
20063,
root);
if (em->mode < MODE_ENTRY) {
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_CLOSE_ERROR,
"Fails to close files",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_FILE_GROUP,
20064,
root);
}
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_DELETED,
"Files deleted",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_VFS_GROUP,
20065,
root);
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_WRITE_CALLS,
"Write to disk",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_VFS_GROUP,
20066,
apps_groups_root_target);
if (em->mode < MODE_ENTRY) {
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_WRITE_CALLS_ERROR,
"Fails to write",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_VFS_GROUP,
20067,
root);
}
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_READ_CALLS,
"Read from disk",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_VFS_GROUP,
20068,
root);
if (em->mode < MODE_ENTRY) {
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_READ_CALLS_ERROR,
"Fails to read",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_VFS_GROUP,
20069,
root);
}
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_WRITE_BYTES,
"Bytes written on disk",
EBPF_COMMON_DIMENSION_BYTESS,
NETDATA_APPS_VFS_GROUP,
20070,
root);
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_VFS_READ_BYTES,
"Bytes read from disk",
EBPF_COMMON_DIMENSION_BYTESS,
NETDATA_APPS_VFS_GROUP,
20071,
root);
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_PROCESS,
"Process started",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_PROCESS_GROUP,
20072,
root);
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_THREAD,
"Threads started",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_PROCESS_GROUP,
20073,
root);
ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_CLOSE,
"Tasks closed",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_PROCESS_GROUP,
20074,
root);
}
/**
* Create apps charts
*
* Call ebpf_create_chart to create the charts on apps submenu.
*
* @param em a pointer to the structure with the default values.
* @param root a pointer for the targets.
*/
static void ebpf_create_apps_charts(ebpf_module_t *em, struct target *root)
{
struct target *w;
int newly_added = 0;
for (w = root; w; w = w->next) {
if (w->target)
continue;
if (unlikely(w->processes && (debug_enabled || w->debug_enabled))) {
struct pid_on_target *pid_on_target;
fprintf(
stderr, "ebpf.plugin: target '%s' has aggregated %u process%s:", w->name, w->processes,
(w->processes == 1) ? "" : "es");
for (pid_on_target = w->root_pid; pid_on_target; pid_on_target = pid_on_target->next) {
fprintf(stderr, " %d", pid_on_target->pid);
}
fputc('\n', stderr);
}
if (!w->exposed && w->processes) {
newly_added++;
w->exposed = 1;
if (debug_enabled || w->debug_enabled)
debug_log_int("%s just added - regenerating charts.", w->name);
}
}
if (!newly_added)
return;
if (ebpf_modules[EBPF_MODULE_PROCESS_IDX].apps_charts)
ebpf_process_create_apps_charts(em, root);
if (ebpf_modules[EBPF_MODULE_SOCKET_IDX].apps_charts)
ebpf_socket_create_apps_charts(NULL, root);
}
/*****************************************************************
*
* FUNCTIONS WITH THE MAIN LOOP
*
*****************************************************************/
/**
* Main loop for this collector.
*
* @param step the number of microseconds used with heart beat
* @param em the structure with thread information
*/
static void process_collector(usec_t step, ebpf_module_t *em)
{
heartbeat_t hb;
heartbeat_init(&hb);
int publish_global = em->global_charts;
int apps_enabled = em->apps_charts;
int pid_fd = map_fd[0];
while (!close_ebpf_plugin) {
usec_t dt = heartbeat_next(&hb, step);
(void)dt;
read_hash_global_tables();
pthread_mutex_lock(&collect_data_mutex);
cleanup_exited_pids();
collect_data_for_all_processes(pid_fd);
ebpf_create_apps_charts(em, apps_groups_root_target);
pthread_cond_broadcast(&collect_data_cond_var);
pthread_mutex_unlock(&collect_data_mutex);
int publish_apps = 0;
if (apps_enabled && all_pids_count > 0) {
publish_apps = 1;
ebpf_process_update_apps_data();
}
pthread_mutex_lock(&lock);
if (publish_global) {
ebpf_process_send_data(em);
}
if (publish_apps) {
ebpf_process_send_apps_data(em, apps_groups_root_target);
}
pthread_mutex_unlock(&lock);
fflush(stdout);
}
}
/*****************************************************************
*
* FUNCTIONS TO CLOSE THE THREAD
*
*****************************************************************/
void clean_global_memory() {
int pid_fd = map_fd[0];
struct pid_stat *pids = root_of_pids;
while (pids) {
uint32_t pid = pids->pid;
freez(global_process_stats[pid]);
bpf_map_delete_elem(pid_fd, &pid);
freez(current_apps_data[pid]);
pids = pids->next;
}
}
void clean_pid_on_target(struct pid_on_target *ptr) {
while (ptr) {
struct pid_on_target *next = ptr->next;
freez(ptr);
ptr = next;
}
}
void clean_apps_structures(struct target *ptr) {
struct target *agdt = ptr;
while (agdt) {
struct target *next = agdt->next;
clean_pid_on_target(agdt->root_pid);
freez(agdt);
agdt = next;
}
}
/**
* Clean up the main thread.
*
* @param ptr thread data.
*/
static void ebpf_process_cleanup(void *ptr)
{
UNUSED(ptr);
heartbeat_t hb;
heartbeat_init(&hb);
uint32_t tick = 200*USEC_PER_MS;
while (!finalized_threads) {
usec_t dt = heartbeat_next(&hb, tick);
UNUSED(dt);
}
freez(process_aggregated_data);
freez(process_publish_aggregated);
freez(process_hash_values);
clean_global_memory();
freez(global_process_stats);
freez(current_apps_data);
freez(prev_apps_data);
clean_apps_structures(apps_groups_root_target);
freez(process_data.map_fd);
struct bpf_program *prog;
size_t i = 0 ;
bpf_object__for_each_program(prog, objects) {
bpf_link__destroy(probe_links[i]);
i++;
}
bpf_object__close(objects);
}
/*****************************************************************
*
* FUNCTIONS TO START THREAD
*
*****************************************************************/
/**
* Allocate vectors used with this thread.
* We are not testing the return, because callocz does this and shutdown the software
* case it was not possible to allocate.
*
* @param length is the length for the vectors used inside the collector.
*/
static void ebpf_process_allocate_global_vectors(size_t length)
{
process_aggregated_data = callocz(length, sizeof(netdata_syscall_stat_t));
process_publish_aggregated = callocz(length, sizeof(netdata_publish_syscall_t));
process_hash_values = callocz(ebpf_nprocs, sizeof(netdata_idx_t));
global_process_stats = callocz((size_t)pid_max, sizeof(ebpf_process_stat_t *));
current_apps_data = callocz((size_t)pid_max, sizeof(ebpf_process_publish_apps_t *));
prev_apps_data = callocz((size_t)pid_max, sizeof(ebpf_process_publish_apps_t *));
}
static void change_syscalls()
{
static char *lfork = { "do_fork" };
process_id_names[7] = lfork;
}
/**
* Set local variables
*
*/
static void set_local_pointers()
{
map_fd = process_data.map_fd;
if (process_data.isrh >= NETDATA_MINIMUM_RH_VERSION && process_data.isrh < NETDATA_RH_8)
change_syscalls();
}
/*****************************************************************
*
* EBPF PROCESS THREAD
*
*****************************************************************/
/**
*
*/
static void wait_for_all_threads_die()
{
ebpf_modules[EBPF_MODULE_PROCESS_IDX].enabled = 0;