diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e53cd5..65cd42b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,8 +27,6 @@ set(SENSOR_SOURCES src/config.c src/util.c src/target.c - src/target_docker.c - src/target_kubernetes.c src/pmu.c src/events.c src/hwinfo.c diff --git a/src/sensor.c b/src/sensor.c index 9aab42e..7487b30 100644 --- a/src/sensor.c +++ b/src/sensor.c @@ -266,7 +266,7 @@ main(int argc, char **argv) /* start system monitoring actor only when needed */ if (zhashx_size(config->events.system)) { - system_target = target_create(TARGET_TYPE_ALL, NULL, NULL); + system_target = target_create(TARGET_TYPE_GLOBAL, NULL, NULL); system_monitor_config = perf_config_create(hwinfo, config->events.system, system_target); system_perf_monitor = zactor_new(perf_monitoring_actor, system_monitor_config); } diff --git a/src/target.c b/src/target.c index 3f28e55..119c147 100644 --- a/src/target.c +++ b/src/target.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, INRIA + * Copyright (c) 2018, Inria * Copyright (c) 2018, University of Lille * All rights reserved. * @@ -38,60 +38,8 @@ #include #include "target.h" -#include "target_docker.h" -#include "target_kubernetes.h" -enum target_type -target_detect_type(const char *cgroup_path) -{ - /* All running processes/threads (not a cgroup) */ - if (!cgroup_path) - return TARGET_TYPE_ALL; - - /* System (running processes/threads in system cgroup) */ - if (strstr(cgroup_path, "perf_event/system")) - return TARGET_TYPE_SYSTEM; - - /* Kernel (running processes/threads in kernel cgroup) */ - if (strstr(cgroup_path, "perf_event/kernel")) - return TARGET_TYPE_KERNEL; - - /* Docker (running containers) */ - if (strstr(cgroup_path, "perf_event/docker")) - return TARGET_TYPE_DOCKER; - - /* Kubernetes (running containers) */ - if (strstr(cgroup_path, "perf_event/kubepods")) - return TARGET_TYPE_KUBERNETES; - - /* LibVirt (running virtual machine) */ - if (strstr(cgroup_path, "perf_event/machine.slice")) - return TARGET_TYPE_LIBVIRT; - - /* LXC (running containers) */ - if (strstr(cgroup_path, "perf_event/lxc")) - return TARGET_TYPE_LXC; - - return TARGET_TYPE_UNKNOWN; -} - -int -target_validate_type(enum target_type type, const char *cgroup_path) -{ - switch (type) { - case TARGET_TYPE_DOCKER: - return target_docker_validate(cgroup_path); - - case TARGET_TYPE_KUBERNETES: - return target_kubernetes_validate(cgroup_path); - - default: - /* other types does not need a validation */ - return true; - } -} - struct target * target_create(enum target_type type, const char *cgroup_basedir, const char *cgroup_path) { @@ -113,26 +61,10 @@ target_resolve_real_name(struct target *target) char *target_real_name = NULL; switch (target->type) { - case TARGET_TYPE_DOCKER: - target_real_name = target_docker_resolve_name(target); - break; - - case TARGET_TYPE_KUBERNETES: - target_real_name = target_kubernetes_resolve_name(target); - break; - - case TARGET_TYPE_ALL: + case TARGET_TYPE_GLOBAL: target_real_name = strdup("all"); break; - case TARGET_TYPE_KERNEL: - target_real_name = strdup("kernel"); - break; - - case TARGET_TYPE_SYSTEM: - target_real_name = strdup("system"); - break; - default: break; } @@ -161,7 +93,6 @@ target_discover_running(const char *base_path, zhashx_t *targets) const char *path[] = { base_path, NULL }; FTS *file_system = NULL; FTSENT *node = NULL; - enum target_type type = TARGET_TYPE_UNKNOWN; struct target *target = NULL; file_system = fts_open((char * const *) path, FTS_PHYSICAL | FTS_NOCHDIR | FTS_NOSTAT, NULL); @@ -192,7 +123,7 @@ target_discover_running(const char *base_path, zhashx_t *targets) if (node->fts_number != 0) continue; - target = target_create(type, base_path, node->fts_path); + target = target_create(TARGET_TYPE_CGROUP, base_path, node->fts_path); zhashx_insert(targets, node->fts_path, target); } diff --git a/src/target.h b/src/target.h index 95cc49d..1c28b7c 100644 --- a/src/target.h +++ b/src/target.h @@ -34,20 +34,14 @@ #include + /* * target_type stores the supported target types. */ enum target_type { - TARGET_TYPE_UNKNOWN = 1, - TARGET_TYPE_ALL = 2, - TARGET_TYPE_SYSTEM = 4, - TARGET_TYPE_KERNEL = 8, - TARGET_TYPE_DOCKER = 16, - TARGET_TYPE_KUBERNETES = 32, - TARGET_TYPE_LIBVIRT = 64, - TARGET_TYPE_LXC = 128, - TARGET_TYPE_EVERYTHING = 255 + TARGET_TYPE_GLOBAL = 1, + TARGET_TYPE_CGROUP = 2, }; /* @@ -60,16 +54,6 @@ struct target char *cgroup_path; }; -/* - * target_detect_type returns the target type of the given cgroup path. - */ -enum target_type target_detect_type(const char *cgroup_path); - -/* - * target_validate_type validate the target type of the given cgroup path. - */ -int target_validate_type(enum target_type type, const char *cgroup_path); - /* * target_create allocate the resources and configure the target. */ @@ -91,4 +75,3 @@ void target_destroy(struct target *target); int target_discover_running(const char *base_path, zhashx_t *targets); #endif /* TARGET_H */ - diff --git a/src/target_docker.c b/src/target_docker.c deleted file mode 100644 index 5a503e6..0000000 --- a/src/target_docker.c +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (c) 2018, INRIA - * Copyright (c) 2018, University of Lille - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * * Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -#include "target.h" -#include "target_docker.h" - -/* - * CONTAINER_ID_REGEX is the regex used to extract the Docker container id from the cgroup path. - * CONTAINER_ID_REGEX_EXPECTED_MATCHES is the number of expected matches from the regex. (num groups + 1) - */ -#define CONTAINER_ID_REGEX "perf_event/docker/([a-f0-9]{64})$" -#define CONTAINER_ID_REGEX_EXPECTED_MATCHES 2 - -/* - * CONTAINER_NAME_REGEX is the regex used to extract the Docker container name from its json configuration file. - * CONTAINER_NAME_REGEX_EXPECTED_MATCHES is the number of expected matches from the regex. (num groups + 1) - */ -#define CONTAINER_NAME_REGEX "\"Name\":\"/([a-zA-Z0-9][a-zA-Z0-9_.-]+)\"" -#define CONTAINER_NAME_REGEX_EXPECTED_MATCHES 2 - - -int -target_docker_validate(const char *cgroup_path) -{ - regex_t re; - int is_docker_target = false; - - if (!cgroup_path) - return false; - - if (!regcomp(&re, CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) { - if (!regexec(&re, cgroup_path, 0, NULL, 0)) - is_docker_target = true; - - regfree(&re); - } - - return is_docker_target; -} - -static char * -build_container_config_path(const char *cgroup_path) -{ - regex_t re; - regmatch_t matches[CONTAINER_ID_REGEX_EXPECTED_MATCHES]; - regoff_t length; - const char *id = NULL; - char buffer[PATH_MAX] = {}; - char *config_path = NULL; - - if (!regcomp(&re, CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE)) { - if (!regexec(&re, cgroup_path, CONTAINER_ID_REGEX_EXPECTED_MATCHES, matches, 0)) { - id = cgroup_path + matches[1].rm_so; - length = matches[1].rm_eo - matches[1].rm_so; - snprintf(buffer, PATH_MAX, "/var/lib/docker/containers/%.*s/config.v2.json", length, id); - config_path = strdup(buffer); - } - regfree(&re); - } - - return config_path; -} - -char * -target_docker_resolve_name(struct target *target) -{ - char *config_path = NULL; - FILE *json_file = NULL; - char *json = NULL; - size_t json_len; - regex_t re; - regmatch_t matches[CONTAINER_NAME_REGEX_EXPECTED_MATCHES]; - char *target_name = NULL; - - config_path = build_container_config_path(target->cgroup_path); - if (!config_path) - return NULL; - - json_file = fopen(config_path, "r"); - if (json_file) { - if (getline(&json, &json_len, json_file) != -1) { - if (!regcomp(&re, CONTAINER_NAME_REGEX, REG_EXTENDED | REG_NEWLINE)) { - if (!regexec(&re, json, CONTAINER_NAME_REGEX_EXPECTED_MATCHES, matches, 0)) - target_name = strndup(json + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so); - - regfree(&re); - } - free(json); - } - fclose(json_file); - } - - free(config_path); - return target_name; -} - diff --git a/src/target_docker.h b/src/target_docker.h deleted file mode 100644 index 841d7ec..0000000 --- a/src/target_docker.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2018, INRIA - * Copyright (c) 2018, University of Lille - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * * Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef TARGET_DOCKER_H -#define TARGET_DOCKER_H - -#include "target.h" - -/* - * target_docker_validate check if the cgroup path lead to a valid Docker target. - */ -int target_docker_validate(const char *cgroup_path); - -/* - * target_docker_resolve_name resolve and return the real name of the given target. - */ -char *target_docker_resolve_name(struct target *target); - -#endif /* TARGET_DOCKER_H */ - diff --git a/src/target_kubernetes.c b/src/target_kubernetes.c deleted file mode 100644 index 2a6bc62..0000000 --- a/src/target_kubernetes.c +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (c) 2018, INRIA - * Copyright (c) 2018, University of Lille - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * * Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include - -#include "target.h" -#include "target_kubernetes.h" - -/* - * CONTAINER_ID_REGEX is the regex used to extract the Docker container id from a cgroup path. - * CONTAINER_ID_REGEX_EXPECTED_MATCHES is the number of matches expected from the regex. (num groups + 1) - */ -#define CONTAINER_ID_REGEX \ - "perf_event/kubepods/" \ - "(besteffort/|burstable/|)" \ - "(pod[a-zA-Z0-9][a-zA-Z0-9.-]+)/" /* Pod ID */ \ - "([a-f0-9]{64})" /* Container ID */ \ - "(/[a-zA-Z0-9][a-zA-Z0-9.-]+|)" /* Resource group */ -#define CONTAINER_ID_REGEX_EXPECTED_MATCHES 5 - -/* - * CONTAINER_NAME_REGEX is the regex used to extract the name of the Docker container from its json configuration file. - * CONTAINER_NAME_REGEX_EXPECTED_MATCHES is the number of matches expected from the regex. (num groups + 1) - */ -#define CONTAINER_NAME_REGEX "\"Name\":\"/([a-zA-Z0-9][a-zA-Z0-9_.-]+)\"" -#define CONTAINER_NAME_REGEX_EXPECTED_MATCHES 2 - - -int -target_kubernetes_validate(const char *cgroup_path) -{ - regex_t re; - bool is_kubernetes_target = false; - - if (!cgroup_path) - return false; - - if (!regcomp(&re, CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) { - if (!regexec(&re, cgroup_path, 0, NULL, 0)) - is_kubernetes_target = true; - - regfree(&re); - } - - return is_kubernetes_target; -} - -static char * -build_container_config_path(const char *cgroup_path) -{ - regex_t re; - regmatch_t matches[CONTAINER_ID_REGEX_EXPECTED_MATCHES]; - regoff_t length; - const char *id = NULL; - char buffer[PATH_MAX] = {}; - char *config_path = NULL; - - if (!regcomp(&re, CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE)) { - if (!regexec(&re, cgroup_path, CONTAINER_ID_REGEX_EXPECTED_MATCHES, matches, 0)) { - id = cgroup_path + matches[3].rm_so; - length = matches[3].rm_eo - matches[3].rm_so; - snprintf(buffer, PATH_MAX, "/var/lib/docker/containers/%.*s/config.v2.json", length, id); - config_path = strdup(buffer); - } - regfree(&re); - } - - return config_path; -} - -char * -target_kubernetes_resolve_name(struct target *target) -{ - char *config_path = NULL; - FILE *json_file = NULL; - char *json = NULL; - size_t json_len; - regex_t re; - regmatch_t matches[CONTAINER_NAME_REGEX_EXPECTED_MATCHES]; - char *target_name = NULL; - - config_path = build_container_config_path(target->cgroup_path); - if (!config_path) - return NULL; - - json_file = fopen(config_path, "r"); - if (json_file) { - if (getline(&json, &json_len, json_file) != -1) { - if (!regcomp(&re, CONTAINER_NAME_REGEX, REG_EXTENDED | REG_NEWLINE)) { - if (!regexec(&re, json, CONTAINER_NAME_REGEX_EXPECTED_MATCHES, matches, 0)) - target_name = strndup(json + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so); - - regfree(&re); - } - free(json); - } - fclose(json_file); - } - - free(config_path); - return target_name; -} - diff --git a/src/target_kubernetes.h b/src/target_kubernetes.h deleted file mode 100644 index fe2a3d8..0000000 --- a/src/target_kubernetes.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2018, INRIA - * Copyright (c) 2018, University of Lille - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * * Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef TARGET_KUBERNETES_H -#define TARGET_KUBERNETES_H - -#include "target.h" - -/* - * target_kubernetes_validate check if the cgroup path lead to a valid Kubernetes target. - */ -int target_kubernetes_validate(const char *cgroup_path); - -/* - * target_kubernetes_resolve_name resolve and return the real name of the given target. - */ -char *target_kubernetes_resolve_name(struct target *target); - -#endif /* TARGET_KUBERNETES_H */ -