Move parser from children to main thread (#11152)
Centralize eBPF plugin parser to avoid possible contradictions between user configuration and visualized charts.
thiagoftsm committed
May 25, 2021 at 11:54 UTC
1b47b0411da56f6d143a1ab572541c56743b1475
15 files changed
+66
-35
collectors/ebpf.plugin/ebpf.c
+27
-6
@@ -78,29 +78,36 @@ ebpf_module_t ebpf_modules[] = {
78
{ .thread_name = "process", .config_name = "process", .enabled = 0, .start_routine = ebpf_process_thread,
79
.update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
80
.optional = 0, .apps_routine = ebpf_process_create_apps_charts, .maps = NULL,
81
- .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL},
81
+ .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &process_config,
82
+ .config_file = NETDATA_PROCESS_CONFIG_FILE},
83
{ .thread_name = "socket", .config_name = "socket", .enabled = 0, .start_routine = ebpf_socket_thread,
84
.update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
85
.optional = 0, .apps_routine = ebpf_socket_create_apps_charts, .maps = NULL,
85
- .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL},
86
+ .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &socket_config,
87
+ .config_file = NETDATA_NETWORK_CONFIG_FILE},
88
{ .thread_name = "cachestat", .config_name = "cachestat", .enabled = 0, .start_routine = ebpf_cachestat_thread,
89
.update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
90
.optional = 0, .apps_routine = ebpf_cachestat_create_apps_charts, .maps = NULL,
89
- .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL},
91
+ .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &cachestat_config,
92
+ .config_file = NETDATA_CACHESTAT_CONFIG_FILE},
93
{ .thread_name = "sync", .config_name = "sync", .enabled = 0, .start_routine = ebpf_sync_thread,
94
.update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
92
- .optional = 0, .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL },
95
+ .optional = 0, .apps_routine = NULL, .maps = NULL,
96
+ .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &sync_config,
97
+ .config_file = NETDATA_SYNC_CONFIG_FILE},
98
{ .thread_name = "dc", .config_name = "dc", .enabled = 0, .start_routine = ebpf_dcstat_thread,
99
.update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
100
.optional = 0, .apps_routine = ebpf_dcstat_create_apps_charts, .maps = NULL,
96
- .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL },
101
+ .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &dcstat_config,
102
+ .config_file = NETDATA_DIRECTORY_DCSTAT_CONFIG_FILE},
103
{ .thread_name = "swap", .config_name = "swap", .enabled = 0, .start_routine = ebpf_swap_thread,
104
.update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
105
.optional = 0, .apps_routine = ebpf_swap_create_apps_charts, .maps = NULL,
106
.pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL },
107
{ .thread_name = NULL, .enabled = 0, .start_routine = NULL, .update_time = 1,
108
.global_charts = 0, .apps_charts = 1, .mode = MODE_ENTRY,
103
- .optional = 0, .apps_routine = NULL, .maps = NULL, .pid_map_size = 0, .names = NULL },
109
+ .optional = 0, .apps_routine = NULL, .maps = NULL, .pid_map_size = 0, .names = NULL,
110
+ .cfg = NULL, .config_name = NULL},
111
};
112
113
// Link with apps.plugin
@@ -1160,6 +1167,19 @@ static void parse_args(int argc, char **argv)
1167
*
1168
*****************************************************************/
1169
1170
+/**
1171
+ * Load collector config
1172
+ *
1173
+ * @param lmode the mode that will be used for them.
1174
+ */
1175
+static inline void ebpf_load_thread_config()
1176
+{
1177
+ int i;
1178
+ for (i = 0; ebpf_modules[i].thread_name; i++) {
1179
+ ebpf_update_module(&ebpf_modules[i]);
1180
+ }
1181
+}
1182
+
1183
/**
1184
* Entry point
1185
*
@@ -1172,6 +1192,7 @@ int main(int argc, char **argv)
1192
{
1193
set_global_variables();
1194
parse_args(argc, argv);
1195
+ ebpf_load_thread_config();
1196
1197
running_on_kernel = get_kernel_version(kernel_string, 63);
1198
if (!has_condition_to_run(running_on_kernel)) {
collectors/ebpf.plugin/ebpf_cachestat.c
-1
@@ -615,7 +615,6 @@ void *ebpf_cachestat_thread(void *ptr)
615
em->maps = cachestat_maps;
616
fill_ebpf_data(&cachestat_data);
617
618
- ebpf_update_module(em, &cachestat_config, NETDATA_CACHESTAT_CONFIG_FILE);
618
ebpf_update_pid_table(&cachestat_maps[0], em);
619
620
if (!em->enabled)
collectors/ebpf.plugin/ebpf_cachestat.h
+2
@@ -62,4 +62,6 @@ typedef struct netdata_publish_cachestat {
62
extern void *ebpf_cachestat_thread(void *ptr);
63
extern void clean_cachestat_pid_structures();
64
65
+extern struct config cachestat_config;
66
+
67
#endif // NETDATA_EBPF_CACHESTAT_H
collectors/ebpf.plugin/ebpf_dcstat.c
-1
@@ -565,7 +565,6 @@ void *ebpf_dcstat_thread(void *ptr)
565
em->maps = dcstat_maps;
566
fill_ebpf_data(&dcstat_data);
567
568
- ebpf_update_module(em, &dcstat_config, NETDATA_DIRECTORY_DCSTAT_CONFIG_FILE);
568
ebpf_update_pid_table(&dcstat_maps[0], em);
569
570
ebpf_update_names(dc_optional_name, em);
collectors/ebpf.plugin/ebpf_dcstat.h
+1
@@ -60,5 +60,6 @@ typedef struct netdata_publish_dcstat {
60
extern void *ebpf_dcstat_thread(void *ptr);
61
extern void ebpf_dcstat_create_apps_charts(struct ebpf_module *em, void *ptr);
62
extern void clean_dcstat_pid_structures();
63
+extern struct config dcstat_config;
64
65
#endif // NETDATA_EBPF_DCSTAT_H
collectors/ebpf.plugin/ebpf_process.c
+3
-5
@@ -790,10 +790,9 @@ void ebpf_process_create_apps_charts(struct ebpf_module *em, void *ptr)
790
*
791
* Call ebpf_create_chart to create the charts on apps submenu.
792
*
793
- * @param em a pointer to the structure with the default values.
793
* @param root a pointer for the targets.
794
*/
796
-static void ebpf_create_apps_charts(ebpf_module_t *em, struct target *root)
795
+static void ebpf_create_apps_charts(struct target *root)
796
{
797
struct target *w;
798
int newly_added = 0;
@@ -831,7 +830,7 @@ static void ebpf_create_apps_charts(ebpf_module_t *em, struct target *root)
830
for (counter = 0; ebpf_modules[counter].thread_name; counter++) {
831
ebpf_module_t *current = &ebpf_modules[counter];
832
if (current->enabled && current->apps_charts && current->apps_routine)
834
- current->apps_routine(em, root);
833
+ current->apps_routine(current, root);
834
}
835
}
836
@@ -864,7 +863,7 @@ static void process_collector(usec_t step, ebpf_module_t *em)
863
cleanup_exited_pids();
864
collect_data_for_all_processes(pid_fd);
865
867
- ebpf_create_apps_charts(em, apps_groups_root_target);
866
+ ebpf_create_apps_charts(apps_groups_root_target);
867
868
pthread_cond_broadcast(&collect_data_cond_var);
869
pthread_mutex_unlock(&collect_data_mutex);
@@ -1045,7 +1044,6 @@ void *ebpf_process_thread(void *ptr)
1044
goto endprocess;
1045
}
1046
1048
- ebpf_update_module(em, &process_config, NETDATA_PROCESS_CONFIG_FILE);
1047
ebpf_update_pid_table(&process_maps[0], em);
1048
1049
set_local_pointers();
collectors/ebpf.plugin/ebpf_process.h
+2
@@ -138,4 +138,6 @@ typedef struct ebpf_process_publish_apps {
138
uint64_t bytes_read;
139
} ebpf_process_publish_apps_t;
140
141
+extern struct config process_config;
142
+
143
#endif /* NETDATA_EBPF_PROCESS_H */
collectors/ebpf.plugin/ebpf_socket.c
+3
-1
@@ -2862,7 +2862,6 @@ void *ebpf_socket_thread(void *ptr)
2862
em->maps = socket_maps;
2863
fill_ebpf_data(&socket_data);
2864
2865
- ebpf_update_module(em, &socket_config, NETDATA_NETWORK_CONFIG_FILE);
2865
parse_network_viewer_section(&socket_config);
2866
parse_service_name_section(&socket_config);
2867
parse_table_size_options(&socket_config);
@@ -2885,6 +2884,9 @@ void *ebpf_socket_thread(void *ptr)
2884
}
2885
2886
set_local_pointers();
2887
+ if (running_on_kernel < NETDATA_EBPF_KERNEL_5_0)
2888
+ em->mode = MODE_ENTRY;
2889
+
2890
probe_links = ebpf_load_program(ebpf_plugin_dir, em, kernel_string, &objects, socket_data.map_fd);
2891
if (!probe_links) {
2892
pthread_mutex_unlock(&lock);
collectors/ebpf.plugin/ebpf_socket.h
+1
@@ -312,5 +312,6 @@ extern void parse_service_name_section(struct config *cfg);
312
extern void clean_socket_apps_structures();
313
314
extern ebpf_socket_publish_apps_t **socket_bandwidth_curr;
315
+extern struct config socket_config;
316
317
#endif
collectors/ebpf.plugin/ebpf_swap.c
-1
@@ -404,7 +404,6 @@ void *ebpf_swap_thread(void *ptr)
404
em->maps = swap_maps;
405
fill_ebpf_data(&swap_data);
406
407
- ebpf_update_module(em, &swap_config, NETDATA_DIRECTORY_SWAP_CONFIG_FILE);
407
ebpf_update_pid_table(&swap_maps[0], em);
408
409
if (!em->enabled)
collectors/ebpf.plugin/ebpf_sync.c
-1
@@ -359,7 +359,6 @@ void *ebpf_sync_thread(void *ptr)
359
ebpf_module_t *em = (ebpf_module_t *)ptr;
360
fill_ebpf_data(&sync_data);
361
362
- ebpf_update_module(em, &sync_config, NETDATA_SYNC_CONFIG_FILE);
362
ebpf_sync_parse_syscalls();
363
364
if (!em->enabled)
collectors/ebpf.plugin/ebpf_sync.h
+1
@@ -50,5 +50,6 @@ enum netdata_sync_table {
50
};
51
52
extern void *ebpf_sync_thread(void *ptr);
53
+extern struct config sync_config;
54
55
#endif /* NETDATA_EBPF_SYNC_H */
libnetdata/ebpf/ebpf.c
+13
-15
@@ -462,7 +462,7 @@ void ebpf_update_names(ebpf_specify_name_t *opt, ebpf_module_t *em)
462
463
//----------------------------------------------------------------------------------------------------------------------
464
465
-void ebpf_mount_config_name(char *filename, size_t length, char *path, char *config)
465
+void ebpf_mount_config_name(char *filename, size_t length, char *path, const char *config)
466
{
467
snprintf(filename, length, "%s/ebpf.d/%s", path, config);
468
}
@@ -483,17 +483,17 @@ static netdata_run_mode_t ebpf_select_mode(char *mode)
483
return MODE_ENTRY;
484
}
485
486
-void ebpf_update_module_using_config(ebpf_module_t *modules, struct config *cfg)
486
+void ebpf_update_module_using_config(ebpf_module_t *modules)
487
{
488
- char *mode = appconfig_get(cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_LOAD_MODE, EBPF_CFG_LOAD_MODE_DEFAULT);
488
+ char *mode = appconfig_get(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_LOAD_MODE, EBPF_CFG_LOAD_MODE_DEFAULT);
489
modules->mode = ebpf_select_mode(mode);
490
491
- modules->update_time = (int)appconfig_get_number(cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_UPDATE_EVERY, 1);
491
+ modules->update_time = (int)appconfig_get_number(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_UPDATE_EVERY, 1);
492
493
- modules->apps_charts = appconfig_get_boolean(cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_APPLICATION,
493
+ modules->apps_charts = appconfig_get_boolean(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_APPLICATION,
494
CONFIG_BOOLEAN_YES);
495
496
- modules->pid_map_size = (uint32_t)appconfig_get_number(cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_PID_SIZE,
496
+ modules->pid_map_size = (uint32_t)appconfig_get_number(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_PID_SIZE,
497
modules->pid_map_size);
498
}
499
@@ -507,20 +507,18 @@ void ebpf_update_module_using_config(ebpf_module_t *modules, struct config *cfg)
507
* update the variables.
508
*
509
* @param em the module structure
510
- * @param cfg the configuration structure
511
- * @param cfg_file the filename to load
510
*/
513
-void ebpf_update_module(ebpf_module_t *em, struct config *cfg, char *cfg_file)
511
+void ebpf_update_module(ebpf_module_t *em)
512
{
513
char filename[FILENAME_MAX+1];
516
- ebpf_mount_config_name(filename, FILENAME_MAX, ebpf_user_config_dir, cfg_file);
517
- if (!ebpf_load_config(cfg, filename)) {
518
- ebpf_mount_config_name(filename, FILENAME_MAX, ebpf_stock_config_dir, cfg_file);
519
- if (!ebpf_load_config(cfg, filename)) {
520
- error("Cannot load the ebpf configuration file %s", cfg_file);
514
+ ebpf_mount_config_name(filename, FILENAME_MAX, ebpf_user_config_dir, em->config_file);
515
+ if (!ebpf_load_config(em->cfg, filename)) {
516
+ ebpf_mount_config_name(filename, FILENAME_MAX, ebpf_stock_config_dir, em->config_file);
517
+ if (!ebpf_load_config(em->cfg, filename)) {
518
+ error("Cannot load the ebpf configuration file %s", em->config_file);
519
return;
520
}
521
}
522
525
- ebpf_update_module_using_config(em, cfg);
523
+ ebpf_update_module_using_config(em);
524
}
libnetdata/ebpf/ebpf.h
+12
-3
@@ -56,6 +56,13 @@
56
*/
57
#define NETDATA_EBPF_KERNEL_5_10 330240
58
59
+/**
60
+ * Kernel 5.0
61
+ *
62
+ * 327680 = 5*65536 +256*0
63
+ */
64
+#define NETDATA_EBPF_KERNEL_5_0 327680
65
+
66
/**
67
* Kernel 4.17
68
*
@@ -127,6 +134,8 @@ typedef struct ebpf_module {
134
ebpf_local_maps_t *maps;
135
ebpf_specify_name_t *names;
136
uint32_t pid_map_size;
137
+ struct config *cfg;
138
+ const char *config_file;
139
} ebpf_module_t;
140
141
extern int get_kernel_version(char *out, int size);
@@ -140,10 +149,10 @@ extern struct bpf_link **ebpf_load_program(char *plugins_dir,
149
struct bpf_object **obj,
150
int *map_fd);
151
143
-extern void ebpf_mount_config_name(char *filename, size_t length, char *path, char *config);
152
+extern void ebpf_mount_config_name(char *filename, size_t length, char *path, const char *config);
153
extern int ebpf_load_config(struct config *config, char *filename);
145
-extern void ebpf_update_module_using_config(ebpf_module_t *modules, struct config *cfg);
146
-extern void ebpf_update_module(ebpf_module_t *em, struct config *cfg, char *cfg_file);
154
+extern void ebpf_update_module_using_config(ebpf_module_t *modules);
155
+extern void ebpf_update_module(ebpf_module_t *em);
156
extern void ebpf_update_names(ebpf_specify_name_t *opt, ebpf_module_t *em);
157
158
#endif /* NETDATA_EBPF_H */
mqtt_websockets
+1
-1
@@ -1 +1 @@
1
-Subproject commit 6a4fba11856e39179d26bf8b0c56f4241c7b1df7
1
+Subproject commit 6b2ec8310f391bea0879ad308eecef8a479d37e2