Browse Source

Fix chart update ebpf.plugin (#13351)

thiagoftsm 2 years ago
parent
commit
7f7225dfaa

+ 17 - 19
collectors/ebpf.plugin/ebpf_cachestat.c

@@ -879,7 +879,7 @@ static int ebpf_send_systemd_cachestat_charts()
     for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
         if (unlikely(ect->systemd) && unlikely(ect->updated)) {
             write_chart_dimension(ect->name, (long long)ect->publish_cachestat.ratio);
-        } else
+        } else if (unlikely(ect->systemd))
             ret = 0;
     }
     write_end_chart();
@@ -1079,32 +1079,30 @@ static void cachestat_collector(ebpf_module_t *em)
     int apps = em->apps_charts;
     int cgroups = em->cgroup_charts;
     int update_every = em->update_every;
-    int counter = update_every - 1;
+    heartbeat_t hb;
+    heartbeat_init(&hb);
+    usec_t step = update_every * USEC_PER_SEC;
     while (!close_ebpf_plugin) {
-        pthread_mutex_lock(&collect_data_mutex);
-        pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
+        (void)heartbeat_next(&hb, step);
 
-        if (++counter == update_every) {
-            counter = 0;
-            if (apps)
-                read_apps_table();
-
-            if (cgroups)
-                ebpf_update_cachestat_cgroup();
+        pthread_mutex_lock(&collect_data_mutex);
+        if (apps)
+            read_apps_table();
 
-            pthread_mutex_lock(&lock);
+        if (cgroups)
+            ebpf_update_cachestat_cgroup();
 
-            cachestat_send_global(&publish);
+        pthread_mutex_lock(&lock);
 
-            if (apps)
-                ebpf_cache_send_apps_data(apps_groups_root_target);
+        cachestat_send_global(&publish);
 
-            if (cgroups)
-                ebpf_cachestat_send_cgroup_data(update_every);
+        if (apps)
+            ebpf_cache_send_apps_data(apps_groups_root_target);
 
-            pthread_mutex_unlock(&lock);
-        }
+        if (cgroups)
+            ebpf_cachestat_send_cgroup_data(update_every);
 
+        pthread_mutex_unlock(&lock);
         pthread_mutex_unlock(&collect_data_mutex);
     }
 }

+ 17 - 19
collectors/ebpf.plugin/ebpf_dcstat.c

@@ -868,7 +868,7 @@ static int ebpf_send_systemd_dc_charts()
     for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
         if (unlikely(ect->systemd) && unlikely(ect->updated)) {
             write_chart_dimension(ect->name, (long long) ect->publish_dc.ratio);
-        } else
+        } else if (unlikely(ect->systemd))
             ret = 0;
     }
     write_end_chart();
@@ -1008,32 +1008,30 @@ static void dcstat_collector(ebpf_module_t *em)
     int apps = em->apps_charts;
     int cgroups = em->cgroup_charts;
     int update_every = em->update_every;
-    int counter = update_every - 1;
+    heartbeat_t hb;
+    heartbeat_init(&hb);
+    usec_t step = update_every * USEC_PER_SEC;
     while (!close_ebpf_plugin) {
-        pthread_mutex_lock(&collect_data_mutex);
-        pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
+        (void)heartbeat_next(&hb, step);
 
-        if (++counter == update_every) {
-            counter = 0;
-            if (apps)
-                read_apps_table();
-
-            if (cgroups)
-                ebpf_update_dc_cgroup();
+        pthread_mutex_lock(&collect_data_mutex);
+        if (apps)
+            read_apps_table();
 
-            pthread_mutex_lock(&lock);
+        if (cgroups)
+            ebpf_update_dc_cgroup();
 
-            dcstat_send_global(&publish);
+        pthread_mutex_lock(&lock);
 
-            if (apps)
-                ebpf_dcache_send_apps_data(apps_groups_root_target);
+        dcstat_send_global(&publish);
 
-            if (cgroups)
-                ebpf_dc_send_cgroup_data(update_every);
+        if (apps)
+            ebpf_dcache_send_apps_data(apps_groups_root_target);
 
-            pthread_mutex_unlock(&lock);
-        }
+        if (cgroups)
+            ebpf_dc_send_cgroup_data(update_every);
 
+        pthread_mutex_unlock(&lock);
         pthread_mutex_unlock(&collect_data_mutex);
     }
 }

+ 8 - 12
collectors/ebpf.plugin/ebpf_disk.c

@@ -729,22 +729,18 @@ static void disk_collector(ebpf_module_t *em)
                           ebpf_disk_read_hash, em);
 
     int update_every = em->update_every;
-    int counter = update_every - 1;
     read_thread_closed = 0;
+    heartbeat_t hb;
+    heartbeat_init(&hb);
+    usec_t step = update_every * USEC_PER_SEC;
     while (!close_ebpf_plugin) {
-        pthread_mutex_lock(&collect_data_mutex);
-        pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
+        (void)heartbeat_next(&hb, step);
 
-        if (++counter == update_every) {
-            counter = 0;
-            pthread_mutex_lock(&lock);
-            ebpf_remove_pointer_from_plot_disk(em);
-            ebpf_latency_send_hd_data(update_every);
-
-            pthread_mutex_unlock(&lock);
-        }
+        pthread_mutex_lock(&lock);
+        ebpf_remove_pointer_from_plot_disk(em);
+        ebpf_latency_send_hd_data(update_every);
 
-        pthread_mutex_unlock(&collect_data_mutex);
+        pthread_mutex_unlock(&lock);
 
         ebpf_update_disks(em);
     }

+ 17 - 20
collectors/ebpf.plugin/ebpf_fd.c

@@ -559,7 +559,7 @@ static int ebpf_send_systemd_fd_charts(ebpf_module_t *em)
     for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
         if (unlikely(ect->systemd) && unlikely(ect->updated)) {
             write_chart_dimension(ect->name, ect->publish_systemd_fd.open_call);
-        } else
+        } else if (unlikely(ect->systemd))
             ret = 0;
     }
     write_end_chart();
@@ -657,33 +657,30 @@ static void fd_collector(ebpf_module_t *em)
 
     int apps = em->apps_charts;
     int cgroups = em->cgroup_charts;
-    int update_every = em->update_every;
-    int counter = update_every - 1;
+    heartbeat_t hb;
+    heartbeat_init(&hb);
+    usec_t step = em->update_every * USEC_PER_SEC;
     while (!close_ebpf_plugin) {
-        pthread_mutex_lock(&collect_data_mutex);
-        pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
+        (void)heartbeat_next(&hb, step);
 
-        if (++counter == update_every) {
-            counter = 0;
-            if (apps)
-                read_apps_table();
-
-            if (cgroups)
-                ebpf_update_fd_cgroup();
+        pthread_mutex_lock(&collect_data_mutex);
+        if (apps)
+            read_apps_table();
 
-            pthread_mutex_lock(&lock);
+        if (cgroups)
+            ebpf_update_fd_cgroup();
 
-            ebpf_fd_send_data(em);
+        pthread_mutex_lock(&lock);
 
-            if (apps)
-                ebpf_fd_send_apps_data(em, apps_groups_root_target);
+        ebpf_fd_send_data(em);
 
-            if (cgroups)
-                ebpf_fd_send_cgroup_data(em);
+        if (apps)
+            ebpf_fd_send_apps_data(em, apps_groups_root_target);
 
-            pthread_mutex_unlock(&lock);
-        }
+        if (cgroups)
+            ebpf_fd_send_cgroup_data(em);
 
+        pthread_mutex_unlock(&lock);
         pthread_mutex_unlock(&collect_data_mutex);
     }
 }

+ 8 - 12
collectors/ebpf.plugin/ebpf_filesystem.c

@@ -582,22 +582,18 @@ static void filesystem_collector(ebpf_module_t *em)
                           NETDATA_THREAD_OPTION_JOINABLE, ebpf_filesystem_read_hash, em);
 
     int update_every = em->update_every;
-    int counter = update_every - 1;
+    heartbeat_t hb;
+    heartbeat_init(&hb);
+    usec_t step = update_every * USEC_PER_SEC;
     while (!close_ebpf_plugin || em->optional) {
-        pthread_mutex_lock(&collect_data_mutex);
-        pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
+        (void)heartbeat_next(&hb, step);
 
-        if (++counter == update_every) {
-            counter = 0;
-            pthread_mutex_lock(&lock);
+        pthread_mutex_lock(&lock);
 
-            ebpf_create_fs_charts(update_every);
-            ebpf_histogram_send_data();
-
-            pthread_mutex_unlock(&lock);
-        }
+        ebpf_create_fs_charts(update_every);
+        ebpf_histogram_send_data();
 
-        pthread_mutex_unlock(&collect_data_mutex);
+        pthread_mutex_unlock(&lock);
     }
 }
 

+ 11 - 16
collectors/ebpf.plugin/ebpf_hardirq.c

@@ -433,26 +433,21 @@ static void hardirq_collector(ebpf_module_t *em)
     pthread_mutex_unlock(&lock);
 
     // loop and read from published data until ebpf plugin is closed.
-    int update_every = em->update_every;
-    int counter = update_every - 1;
+    heartbeat_t hb;
+    heartbeat_init(&hb);
+    usec_t step = em->update_every * USEC_PER_SEC;
     while (!close_ebpf_plugin) {
-        pthread_mutex_lock(&collect_data_mutex);
-        pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
+        (void)heartbeat_next(&hb, step);
 
-        if (++counter == update_every) {
-            counter = 0;
-            pthread_mutex_lock(&lock);
+        pthread_mutex_lock(&lock);
 
-            // write dims now for all hitherto discovered IRQs.
-            write_begin_chart(NETDATA_EBPF_SYSTEM_GROUP, "hardirq_latency");
-            avl_traverse_lock(&hardirq_pub, hardirq_write_dims, NULL);
-            hardirq_write_static_dims();
-            write_end_chart();
-
-            pthread_mutex_unlock(&lock);
-        }
+        // write dims now for all hitherto discovered IRQs.
+        write_begin_chart(NETDATA_EBPF_SYSTEM_GROUP, "hardirq_latency");
+        avl_traverse_lock(&hardirq_pub, hardirq_write_dims, NULL);
+        hardirq_write_static_dims();
+        write_end_chart();
 
-        pthread_mutex_unlock(&collect_data_mutex);
+        pthread_mutex_unlock(&lock);
     }
 }
 

+ 9 - 16
collectors/ebpf.plugin/ebpf_mdflush.c

@@ -261,25 +261,18 @@ static void mdflush_collector(ebpf_module_t *em)
     pthread_mutex_unlock(&lock);
 
     // loop and read from published data until ebpf plugin is closed.
-    int update_every = em->update_every;
-    int counter = update_every - 1;
+    heartbeat_t hb;
+    heartbeat_init(&hb);
+    usec_t step = em->update_every * USEC_PER_SEC;
     while (!close_ebpf_plugin) {
-        pthread_mutex_lock(&collect_data_mutex);
-        pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
-
-        if (++counter == update_every) {
-            counter = 0;
-            pthread_mutex_lock(&lock);
+        (void)heartbeat_next(&hb, step);
 
-            // write dims now for all hitherto discovered devices.
-            write_begin_chart("mdstat", "mdstat_flush");
-            avl_traverse_lock(&mdflush_pub, mdflush_write_dims, NULL);
-            write_end_chart();
-
-            pthread_mutex_unlock(&lock);
-        }
+        // write dims now for all hitherto discovered devices.
+        write_begin_chart("mdstat", "mdstat_flush");
+        avl_traverse_lock(&mdflush_pub, mdflush_write_dims, NULL);
+        write_end_chart();
 
-        pthread_mutex_unlock(&collect_data_mutex);
+        pthread_mutex_unlock(&lock);
     }
 }
 

+ 7 - 12
collectors/ebpf.plugin/ebpf_mount.c

@@ -352,22 +352,17 @@ static void mount_collector(ebpf_module_t *em)
     netdata_thread_create(mount_thread.thread, mount_thread.name, NETDATA_THREAD_OPTION_JOINABLE,
                           ebpf_mount_read_hash, em);
 
-    int update_every = em->update_every;
-    int counter = update_every - 1;
+    heartbeat_t hb;
+    heartbeat_init(&hb);
+    usec_t step = em->update_every * USEC_PER_SEC;
     while (!close_ebpf_plugin) {
-        pthread_mutex_lock(&collect_data_mutex);
-        pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
+        (void)heartbeat_next(&hb, step);
 
-        if (++counter == update_every) {
-            counter = 0;
-            pthread_mutex_lock(&lock);
+        pthread_mutex_lock(&lock);
 
-            ebpf_mount_send_data();
-
-            pthread_mutex_unlock(&lock);
-        }
+        ebpf_mount_send_data();
 
-        pthread_mutex_unlock(&collect_data_mutex);
+        pthread_mutex_unlock(&lock);
     }
 }
 

+ 17 - 19
collectors/ebpf.plugin/ebpf_oomkill.c

@@ -160,7 +160,7 @@ static int ebpf_send_systemd_oomkill_charts()
         if (unlikely(ect->systemd) && unlikely(ect->updated)) {
             write_chart_dimension(ect->name, (long long) ect->oomkill);
             ect->oomkill = 0;
-        } else
+        } else if (unlikely(ect->systemd))
             ret = 0;
     }
     write_end_chart();
@@ -313,34 +313,32 @@ static void oomkill_collector(ebpf_module_t *em)
 {
     int cgroups = em->cgroup_charts;
     int update_every = em->update_every;
-    int counter = update_every - 1;
     int32_t keys[NETDATA_OOMKILL_MAX_ENTRIES];
     memset(keys, 0, sizeof(keys));
 
     // loop and read until ebpf plugin is closed.
+    heartbeat_t hb;
+    heartbeat_init(&hb);
+    usec_t step = update_every * USEC_PER_SEC;
     while (!close_ebpf_plugin) {
-        pthread_mutex_lock(&collect_data_mutex);
-        pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
-
-        if (++counter == update_every) {
-            counter = 0;
-            pthread_mutex_lock(&lock);
+        (void)heartbeat_next(&hb, step);
 
-            uint32_t count = oomkill_read_data(keys);
-            if (cgroups && count)
-                ebpf_update_oomkill_cgroup(keys, count);
+        pthread_mutex_lock(&collect_data_mutex);
+        pthread_mutex_lock(&lock);
 
-            // write everything from the ebpf map.
-            if (cgroups)
-                ebpf_oomkill_send_cgroup_data(update_every);
+        uint32_t count = oomkill_read_data(keys);
+        if (cgroups && count)
+            ebpf_update_oomkill_cgroup(keys, count);
 
-            write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_OOMKILL_CHART);
-            oomkill_write_data(keys, count);
-            write_end_chart();
+        // write everything from the ebpf map.
+        if (cgroups)
+            ebpf_oomkill_send_cgroup_data(update_every);
 
-            pthread_mutex_unlock(&lock);
-        }
+        write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_OOMKILL_CHART);
+        oomkill_write_data(keys, count);
+        write_end_chart();
 
+        pthread_mutex_unlock(&lock);
         pthread_mutex_unlock(&collect_data_mutex);
     }
 }

+ 11 - 6
collectors/ebpf.plugin/ebpf_process.c

@@ -884,7 +884,7 @@ static int ebpf_send_systemd_process_charts(ebpf_module_t *em)
     for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
         if (unlikely(ect->systemd) && unlikely(ect->updated)) {
             write_chart_dimension(ect->name, ect->publish_systemd_ps.create_process);
-        } else
+        } else if (unlikely(ect->systemd))
             ret = 0;
     }
     write_end_chart();
@@ -1031,20 +1031,23 @@ static void process_collector(ebpf_module_t *em)
     if (cgroups)
         ebpf_process_update_cgroup_algorithm();
 
+    int update_apps_every = (int) EBPF_CFG_UPDATE_APPS_EVERY_DEFAULT;
     int pid_fd = process_maps[NETDATA_PROCESS_PID_TABLE].map_fd;
     int update_every = em->update_every;
     int counter = update_every - 1;
+    int update_apps_list = update_apps_every - 1;
     while (!close_ebpf_plugin) {
         usec_t dt = heartbeat_next(&hb, USEC_PER_SEC);
         (void)dt;
 
         pthread_mutex_lock(&collect_data_mutex);
-        cleanup_exited_pids();
-        collect_data_for_all_processes(pid_fd);
+        if (++update_apps_list == update_apps_every) {
+            update_apps_list = 0;
+            cleanup_exited_pids();
+            collect_data_for_all_processes(pid_fd);
 
-        ebpf_create_apps_charts(apps_groups_root_target);
-
-        pthread_cond_broadcast(&collect_data_cond_var);
+            ebpf_create_apps_charts(apps_groups_root_target);
+        }
         pthread_mutex_unlock(&collect_data_mutex);
 
         if (++counter == update_every) {
@@ -1053,6 +1056,7 @@ static void process_collector(ebpf_module_t *em)
             read_hash_global_tables();
 
             int publish_apps = 0;
+            pthread_mutex_lock(&collect_data_mutex);
             if (all_pids_count > 0) {
                 if (apps_enabled) {
                     publish_apps = 1;
@@ -1081,6 +1085,7 @@ static void process_collector(ebpf_module_t *em)
                 }
             }
             pthread_mutex_unlock(&lock);
+            pthread_mutex_unlock(&collect_data_mutex);
         }
 
         fflush(stdout);

Some files were not shown because too many files changed in this diff