summaryrefslogtreecommitdiffstats
path: root/src/collectors/cgroups.plugin/cgroup-discovery.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/collectors/cgroups.plugin/cgroup-discovery.c (renamed from collectors/cgroups.plugin/cgroup-discovery.c)94
1 files changed, 91 insertions, 3 deletions
diff --git a/collectors/cgroups.plugin/cgroup-discovery.c b/src/collectors/cgroups.plugin/cgroup-discovery.c
index ede35ed8a..e5d029cfb 100644
--- a/collectors/cgroups.plugin/cgroup-discovery.c
+++ b/src/collectors/cgroups.plugin/cgroup-discovery.c
@@ -25,6 +25,10 @@ char cgroup_chart_id_prefix[] = "cgroup_";
char services_chart_id_prefix[] = "systemd_";
char *cgroups_rename_script = NULL;
+// Shared memory with information from detected cgroups
+netdata_ebpf_cgroup_shm_t shm_cgroup_ebpf = {NULL, NULL};
+int shm_fd_cgroup_ebpf = -1;
+sem_t *shm_mutex_cgroup_ebpf = SEM_FAILED;
// ----------------------------------------------------------------------------
@@ -42,7 +46,7 @@ static inline void cgroup_free_network_interfaces(struct cgroup *cg) {
cg->interfaces = i->next;
// delete the registration of proc_net_dev rename
- netdev_rename_device_del(i->host_device);
+ cgroup_rename_task_device_del(i->host_device);
freez((void *)i->host_device);
freez((void *)i->container_device);
@@ -1027,6 +1031,82 @@ static int discovery_is_cgroup_duplicate(struct cgroup *cg) {
}
// ----------------------------------------------------------------------------
+// ebpf shared memory
+
+static void netdata_cgroup_ebpf_set_values(size_t length)
+{
+ sem_wait(shm_mutex_cgroup_ebpf);
+
+ shm_cgroup_ebpf.header->cgroup_max = cgroup_root_max;
+ shm_cgroup_ebpf.header->systemd_enabled = cgroup_enable_systemd_services |
+ cgroup_enable_systemd_services_detailed_memory |
+ cgroup_used_memory;
+ shm_cgroup_ebpf.header->body_length = length;
+
+ sem_post(shm_mutex_cgroup_ebpf);
+}
+
+static void netdata_cgroup_ebpf_initialize_shm()
+{
+ shm_fd_cgroup_ebpf = shm_open(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME, O_CREAT | O_RDWR, 0660);
+ if (shm_fd_cgroup_ebpf < 0) {
+ collector_error("Cannot initialize shared memory used by cgroup and eBPF, integration won't happen.");
+ return;
+ }
+
+ size_t length = sizeof(netdata_ebpf_cgroup_shm_header_t) + cgroup_root_max * sizeof(netdata_ebpf_cgroup_shm_body_t);
+ if (ftruncate(shm_fd_cgroup_ebpf, length)) {
+ collector_error("Cannot set size for shared memory.");
+ goto end_init_shm;
+ }
+
+ shm_cgroup_ebpf.header = (netdata_ebpf_cgroup_shm_header_t *) mmap(NULL, length,
+ PROT_READ | PROT_WRITE, MAP_SHARED,
+ shm_fd_cgroup_ebpf, 0);
+
+ if (unlikely(MAP_FAILED == shm_cgroup_ebpf.header)) {
+ shm_cgroup_ebpf.header = NULL;
+ collector_error("Cannot map shared memory used between cgroup and eBPF, integration won't happen");
+ goto end_init_shm;
+ }
+ shm_cgroup_ebpf.body = (netdata_ebpf_cgroup_shm_body_t *) ((char *)shm_cgroup_ebpf.header +
+ sizeof(netdata_ebpf_cgroup_shm_header_t));
+
+ shm_mutex_cgroup_ebpf = sem_open(NETDATA_NAMED_SEMAPHORE_EBPF_CGROUP_NAME, O_CREAT,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, 1);
+
+ if (shm_mutex_cgroup_ebpf != SEM_FAILED) {
+ netdata_cgroup_ebpf_set_values(length);
+ return;
+ }
+
+ collector_error("Cannot create semaphore, integration between eBPF and cgroup won't happen");
+ munmap(shm_cgroup_ebpf.header, length);
+ shm_cgroup_ebpf.header = NULL;
+
+ end_init_shm:
+ close(shm_fd_cgroup_ebpf);
+ shm_fd_cgroup_ebpf = -1;
+ shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
+}
+
+static void cgroup_cleanup_ebpf_integration()
+{
+ if (shm_mutex_cgroup_ebpf != SEM_FAILED) {
+ sem_close(shm_mutex_cgroup_ebpf);
+ }
+
+ if (shm_cgroup_ebpf.header) {
+ shm_cgroup_ebpf.header->cgroup_root_count = 0;
+ munmap(shm_cgroup_ebpf.header, shm_cgroup_ebpf.header->body_length);
+ }
+
+ if (shm_fd_cgroup_ebpf > 0) {
+ close(shm_fd_cgroup_ebpf);
+ }
+}
+
+// ----------------------------------------------------------------------------
// cgroup network interfaces
#define CGROUP_NETWORK_INTERFACE_MAX_LINE 2048
@@ -1084,8 +1164,13 @@ static inline void read_cgroup_network_interfaces(struct cgroup *cg) {
collector_info("CGROUP: cgroup '%s' has network interface '%s' as '%s'", cg->id, i->host_device, i->container_device);
// register a device rename to proc_net_dev.c
- netdev_rename_device_add(i->host_device, i->container_device, cg->chart_id, cg->chart_labels,
- k8s_is_kubepod(cg) ? "k8s." : "", cgroup_netdev_get(cg));
+ cgroup_rename_task_add(
+ i->host_device,
+ i->container_device,
+ cg->chart_id,
+ cg->chart_labels,
+ k8s_is_kubepod(cg) ? "k8s." : "",
+ cgroup_netdev_get(cg));
}
}
@@ -1226,6 +1311,8 @@ void cgroup_discovery_worker(void *ptr)
service_register(SERVICE_THREAD_TYPE_LIBUV, NULL, NULL, NULL, false);
+ netdata_cgroup_ebpf_initialize_shm();
+
while (service_running(SERVICE_COLLECTORS)) {
worker_is_idle();
@@ -1239,6 +1326,7 @@ void cgroup_discovery_worker(void *ptr)
discovery_find_all_cgroups();
}
collector_info("discovery thread stopped");
+ cgroup_cleanup_ebpf_integration();
worker_unregister();
service_exits();
__atomic_store_n(&discovery_thread.exited,1,__ATOMIC_RELAXED);