From: Ruben Kerkhof Date: Tue, 7 May 2019 08:04:33 +0000 (+0200) Subject: Merge pull request #3019 from sradco/master X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=dfb17826ceb8223cca9009b7b9f4194252ff994d;hp=cc0fe32830bc2cfd1353c151aa1028107f5c05de Merge pull request #3019 from sradco/master Add write_syslog output plugin --- diff --git a/.cirrus.yml b/.cirrus.yml index c6a38dac..ab88daf6 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -1,5 +1,6 @@ env: LANG: C + CIRRUS_CLONE_DEPTH: 1 DEFAULT_CONFIG_OPTS: --enable-debug --without-libstatgrab --disable-dependency-tracking @@ -16,7 +17,7 @@ release_ready_task: - ./build.sh - ./configure $DEFAULT_CONFIG_OPTS checks_script: - - make -s distcheck DISTCHECK_CONFIGURE_FLAGS="${DEFAULT_CONFIG_OPTS}" + - make -j2 -s distcheck DISTCHECK_CONFIGURE_FLAGS="${DEFAULT_CONFIG_OPTS}" ### # Default toolchain and build flags used in deb packages, on a range of Debian @@ -25,10 +26,6 @@ release_ready_task: # debian_default_toolchain_task: matrix: - - allow_failures: true - skip_notifications: true - container: - image: collectd/ci:wheezy_amd64 # TODO: fix this platform - allow_failures: false container: image: collectd/ci:jessie_amd64 @@ -59,9 +56,9 @@ debian_default_toolchain_task: CPPLAGS="$(dpkg-buildflags --get CPPFLAGS)" LDFLAGS="$(dpkg-buildflags --get LDFLAGS)" build_script: - - make -sk + - make -j2 -sk tests_script: - - make -sk check + - make -j2 -sk check - /checks/check-built-plugins.sh ### @@ -82,10 +79,6 @@ redhat_default_toolchain_task: - allow_failures: true skip_notifications: true container: - image: collectd/ci:fedora26_x86_64 - - allow_failures: true - skip_notifications: true - container: image: collectd/ci:fedora28_x86_64 # fedora/rawhide is expected to fail - allow_failures: true @@ -98,9 +91,9 @@ redhat_default_toolchain_task: - gcc --version - ./configure CC=gcc $DEFAULT_CONFIG_OPTS CFLAGS="$(rpm --eval '%optflags')" build_script: - - make -sk + - make -j2 -sk tests_script: - - make -sk check + - make -j2 -sk check - /checks/check-built-plugins.sh @@ -124,15 +117,15 @@ non_standard_toolchains_task: - ./build.sh - clang --version - > - ./configure CC=clang + ./configure CC=clang CXX=clang++ $DEFAULT_CONFIG_OPTS CFLAGS="$(dpkg-buildflags --get CFLAGS)" CPPLAGS="$(dpkg-buildflags --get CPPFLAGS)" LDFLAGS="$(dpkg-buildflags --get LDFLAGS)" build_script: - - make -sk + - make -j2 -sk tests_script: - - make -sk check + - make -j2 -sk check # build against libstatgrab, should always pass - env: @@ -148,7 +141,7 @@ non_standard_toolchains_task: CPPLAGS="$(dpkg-buildflags --get CPPFLAGS)" LDFLAGS="$(dpkg-buildflags --get LDFLAGS)" build_script: - - make -sk + - make -j2 -sk tests_script: - > for i in cpu disk interface load memory swap users; do @@ -158,25 +151,6 @@ non_standard_toolchains_task: fi done - # build against musl-libc using gcc wrapper, expected to fail - - env: - LABEL: musl libc - allow_failures: true - skip_notifications: true - configure_script: - - ./build.sh - - musl-gcc --version - - > - ./configure CC=musl-gcc - $DEFAULT_CONFIG_OPTS - CFLAGS="$(dpkg-buildflags --get CFLAGS)" - CPPLAGS="$(dpkg-buildflags --get CPPFLAGS)" - LDFLAGS="$(dpkg-buildflags --get LDFLAGS)" - build_script: - - make -sk - tests_script: - - make -sk check - # build using clang with a collection of strict build flags, will most # probably always fail - env: @@ -187,7 +161,7 @@ non_standard_toolchains_task: - ./build.sh - clang --version - > - ./configure CC=clang + ./configure CC=clang CXX=clang++ $DEFAULT_CONFIG_OPTS CFLAGS='-Wall -Wno-error @@ -214,9 +188,9 @@ non_standard_toolchains_task: -Wno-typedef-redefinition -Wno-gnu-variable-sized-type-not-at-end' build_script: - - make -sk + - make -j2 -sk tests_script: - - make -sk check + - make -j2 -sk check ### # Build using a range of compilers, available in debian/unstable. NB: might @@ -246,6 +220,6 @@ bleeding_edge_compilers_task: CPPLAGS="$(dpkg-buildflags --get CPPFLAGS)" LDFLAGS="$(dpkg-buildflags --get LDFLAGS)" build_script: - - make -sk + - make -j2 -sk tests_script: - - make -sk check + - make -j2 -sk check diff --git a/Makefile.am b/Makefile.am index 5957e59f..85f8da8a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -159,7 +159,8 @@ check_PROGRAMS = \ test_utils_time \ test_utils_vl_lookup \ test_libcollectd_network_parse \ - test_utils_config_cores + test_utils_config_cores \ + test_utils_proc_pids TESTS = $(check_PROGRAMS) @@ -376,6 +377,11 @@ test_utils_config_cores_SOURCES = \ src/testing.h test_utils_config_cores_LDADD = libplugin_mock.la +test_utils_proc_pids_SOURCES = \ + src/utils/proc_pids/proc_pids_test.c \ + src/testing.h +test_utils_proc_pids_LDADD = libplugin_mock.la + libavltree_la_SOURCES = \ src/utils/avltree/avltree.c \ src/utils/avltree/avltree.h @@ -1065,11 +1071,25 @@ if BUILD_PLUGIN_INTEL_RDT pkglib_LTLIBRARIES += intel_rdt.la intel_rdt_la_SOURCES = \ src/intel_rdt.c \ + src/utils/proc_pids/proc_pids.c \ + src/utils/proc_pids/proc_pids.h \ src/utils/config_cores/config_cores.h \ src/utils/config_cores/config_cores.c intel_rdt_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBPQOS_CPPFLAGS) intel_rdt_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBPQOS_LDFLAGS) intel_rdt_la_LIBADD = $(BUILD_WITH_LIBPQOS_LIBS) + +test_plugin_intel_rdt_SOURCES = \ + src/intel_rdt_test.c \ + src/utils/config_cores/config_cores.c \ + src/utils/proc_pids/proc_pids.c \ + src/daemon/configfile.c \ + src/daemon/types_list.c +test_plugin_intel_rdt_CPPFLAGS = $(AM_CPPFLAGS) +test_plugin_intel_rdt_LDFLAGS = $(PLUGIN_LDFLAGS) +test_plugin_intel_rdt_LDADD = liboconfig.la libplugin_mock.la +check_PROGRAMS += test_plugin_intel_rdt +TESTS += test_plugin_intel_rdt endif if BUILD_PLUGIN_INTERFACE @@ -1939,12 +1959,13 @@ virt_la_CFLAGS = $(AM_CFLAGS) \ virt_la_LDFLAGS = $(PLUGIN_LDFLAGS) virt_la_LIBADD = libignorelist.la $(BUILD_WITH_LIBVIRT_LIBS) $(BUILD_WITH_LIBXML2_LIBS) -test_plugin_virt_SOURCES = src/virt_test.c +test_plugin_virt_SOURCES = src/virt_test.c src/daemon/configfile.c \ + src/daemon/types_list.c test_plugin_virt_CPPFLAGS = $(AM_CPPFLAGS) \ $(BUILD_WITH_LIBVIRT_CPPFLAGS) $(BUILD_WITH_LIBXML2_CFLAGS) test_plugin_virt_LDFLAGS = $(PLUGIN_LDFLAGS) \ $(BUILD_WITH_LIBVIRT_LDFLAGS) $(BUILD_WITH_LIBXML2_LDFLAGS) -test_plugin_virt_LDADD = libplugin_mock.la \ +test_plugin_virt_LDADD = liboconfig.la libplugin_mock.la \ $(BUILD_WITH_LIBVIRT_LIBS) $(BUILD_WITH_LIBXML2_LIBS) check_PROGRAMS += test_plugin_virt TESTS += test_plugin_virt diff --git a/configure.ac b/configure.ac index b272364b..c95422f4 100644 --- a/configure.ac +++ b/configure.ac @@ -6518,6 +6518,7 @@ fi # FreeBSD if test "x$ac_system" = "xFreeBSD"; then + plugin_cpufreq="yes" plugin_disk="yes" plugin_zfs_arc="yes" fi diff --git a/contrib/systemd.collectd.service b/contrib/systemd.collectd.service index c5b1142b..fe535bfe 100644 --- a/contrib/systemd.collectd.service +++ b/contrib/systemd.collectd.service @@ -8,8 +8,6 @@ Requires=local-fs.target network-online.target ExecStart=/usr/sbin/collectd EnvironmentFile=-/etc/sysconfig/collectd EnvironmentFile=-/etc/default/collectd -ProtectSystem=full -ProtectHome=true # A few plugins won't work without some privileges, which you'll have to # specify using the CapabilityBoundingSet directive below. diff --git a/src/collectd-lua.pod b/src/collectd-lua.pod index f5e43aaa..eccd71fb 100644 --- a/src/collectd-lua.pod +++ b/src/collectd-lua.pod @@ -72,8 +72,8 @@ These are used to collect the actual data. It is called once per interval (see the B configuration option of collectd). Usually it will call B to dispatch the values to collectd which will pass them on to all registered B. If this function -does not return 0 the plugin will be skipped for an increasing -amount of time until it returns normally again. +does not return 0, interval between its calls will grow until function returns +0 again. See the B configuration option of collectd. =item write functions @@ -90,12 +90,14 @@ The following functions are provided to Lua modules: =item register_read(callback) +Function to register read callbacks. The callback will be called without arguments. If this callback function does not return 0 the next call will be delayed by an increasing interval. -=item register_write +=item register_write(callback) +Function to register write callbacks. The callback function will be called with one argument passed, which will be a table of values. If this callback function does not return 0 next call will be delayed by @@ -136,8 +138,8 @@ A very simple write function might look like: To register those functions with collectd: - collectd.register_read(read) - collectd.register_write(write) + collectd.register_read(read) -- pass function as variable + collectd.register_write("write") -- pass by global-scope function name =back diff --git a/src/collectd.conf.in b/src/collectd.conf.in index e37c5e86..f8e52881 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -704,6 +704,7 @@ # # Cores "0-2" +# Processes "sshd" # # @@ -1678,7 +1679,7 @@ # InterfaceFormat name # PluginInstanceFormat name # Instances 1 -# ExtraStats "cpu_util disk disk_err domain_state fs_info job_stats_background pcpu perf vcpupin" +# ExtraStats "cpu_util disk disk_err domain_state fs_info job_stats_background pcpu perf vcpupin disk_physical disk_allocation disk_capacity" # PersistentNotification false # @@ -1701,6 +1702,7 @@ # SeparateInstances false # PreserveSeparator false # DropDuplicateFields false +# ReverseHost false # # diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index d13932bb..fb0751a7 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -1656,15 +1656,24 @@ Defaults to B. =head2 Plugin C -This plugin doesn't have any options. It reads +This plugin is available on Linux and FreeBSD only. It doesn't have any +options. On Linux it reads F (for the first CPU installed) to get the current CPU frequency. If this file does not exist make sure B (L) or a similar tool is installed and an "cpu governor" (that's a kernel module) is loaded. -If the system has the I kernel module loaded, this plugin reports -the rate of p-state (cpu frequency) transitions and the percentage of time spent -in each p-state. +On Linux, if the system has the I kernel module loaded, this +plugin reports the rate of p-state (cpu frequency) transitions and the +percentage of time spent in each p-state. + +On FreeBSD it does a sysctl dev.cpu.0.freq and submits this as instance 0. +At this time FreeBSD only has one frequency setting for all cores. +See the BUGS section in the FreeBSD man page for cpufreq(4) for more details. + +On FreeBSD the plugin checks the success of sysctl dev.cpu.0.freq and +unregisters the plugin when this fails. A message will be logged to indicate +this. =head2 Plugin C @@ -3498,6 +3507,7 @@ B Cores "0-2" "3,4,6" "8-10,15" + Processes "sshd,qemu-system-x86" "bash" B @@ -3513,11 +3523,10 @@ recommended to set interval higher than 1 sec. =item B I -All events are reported on a per core basis. Monitoring of the events can be -configured for group of cores (aggregated statistics). This field defines groups -of cores on which to monitor supported events. The field is represented as list -of strings with core group values. Each string represents a list of cores in a -group. Allowed formats are: +Monitoring of the events can be configured for group of cores +(aggregated statistics). This field defines groups of cores on which to monitor +supported events. The field is represented as list of strings with core group +values. Each string represents a list of cores in a group. Allowed formats are: 0,1,2,3 0-10,20-18 1,3,5-8,10,0x10-12 @@ -3525,6 +3534,15 @@ group. Allowed formats are: If an empty string is provided as value for this field default cores configuration is applied - a separate group is created for each core. +=item B I + +Monitoring of the events can be configured for group of processes +(aggregated statistics). This field defines groups of processes on which to +monitor supported events. The field is represented as list of strings with +process names group values. Each string represents a list of processes in a +group. Allowed format is: + sshd,bash,qemu + =back B By default global interval is used to retrieve statistics on monitored @@ -4444,6 +4462,12 @@ For Modbus/RTU, specifies the path to the serial device being used. For Modbus/RTU, specifies the baud rate of the serial device. Note, connections currently support only 8/N/1. +=item B I + +For Modbus/RTU, specifies the type of the serial device. +RS232, RS422 and RS485 are supported. Defaults to RS232. +Available only on Linux systems with libmodbus>=2.9.4. + =item B I Sets the interval (in seconds) in which the values will be collected from this @@ -9252,13 +9276,51 @@ surrounded by I and collectd was compiled with support for regexps. The default is to collect statistics for all domains and all their devices. -Example: +B B and B options are related to +corresponding B<*Format> options. Specifically, B filtering depends +on B setting - if user wants to filter block devices by +'target' name then B option has to be set to 'target' and +B option must be set to a valid block device target +name("/:hdb/"). Mixing formats and filter values from different worlds (i.e., +using 'target' name as B value with B set to +'source') may lead to unexpected results (all devices filtered out or all +visible, depending on the value of B option). +Similarly, option B is related to B setting +(i.e., when user wants to use MAC address as a filter then B +has to be set to 'address' - using wrong type here may filter out all of the +interfaces). + +B + +Ignore all I devices on any domain, but other block devices (eg. I) +will be collected: BlockDevice "/:hdb/" IgnoreSelected "true" + BlockDeviceFormat "target" -Ignore all I devices on any domain, but other block devices (eg. I) -will be collected. +B + +Collect metrics only for block device on 'baremetal0' domain when its +'source' matches given path: + + BlockDevice "baremetal0:/var/lib/libvirt/images/baremetal0.qcow2" + BlockDeviceFormat source + +As you can see it is possible to filter devices/interfaces using +various formats - for block devices 'target' or 'source' name can be +used. Interfaces can be filtered using 'name', 'address' or 'number'. + +B + +Collect metrics only for domains 'baremetal0' and 'baremetal1' and +ignore any other domain: + + Domain "baremetal0" + Domain "baremetal1" + +It is possible to filter multiple block devices/domains/interfaces by +adding multiple filtering entries in separate lines. =item B B|B @@ -9289,6 +9351,11 @@ to C. Setting C will cause the I to be set to C. +B this option determines also what field will be used for +filtering over block devices (filter value in B +will be applied to target or source). More info about filtering +block devices can be found in the description of B. + =item B B|B The B controls whether the full path or the @@ -9339,6 +9406,11 @@ interface path might change between reboots of a guest or across migrations. B means use the interface's number in guest. +B this option determines also what field will be used for +filtering over interface device (filter value in B +will be applied to name, address or number). More info about filtering +interfaces can be found in the description of B. + =item B B When the virt plugin logs data, it sets the plugin_instance of the collected @@ -9421,6 +9493,18 @@ B: I metrics can't be collected if I plugin is enabled. =item B: report pinning of domain VCPUs to host physical CPUs. +=item B: report 'disk_physical' statistic for disk device. +B: This statistic is only reported for disk devices with 'source' +property available. + +=item B: report 'disk_allocation' statistic for disk device. +B: This statistic is only reported for disk devices with 'source' +property available. + +=item B: report 'disk_capacity' statistic for disk device. +B: This statistic is only reported for disk devices with 'source' +property available. + =back =item B B|B @@ -9510,6 +9594,7 @@ Synopsis: LogSendErrors true Prefix "collectd" UseTags false + ReverseHost false @@ -9621,6 +9706,30 @@ are not used. Default value: B. +=item B B|B + +If set to B, the (dot separated) parts of the B field of the +I will be rewritten in reverse order. The rewrite happens I +special characters are replaced with the B. + +This option might be convenient if the metrics are presented with Graphite in a +DNS like tree structure (probably without replacing dots in hostnames). + +Example: + Hostname "node3.cluster1.example.com" + LoadPlugin "cpu" + LoadPlugin "write_graphite" + + + EscapeCharacter "." + ReverseHost true + + + + result on the wire: com.example.cluster1.node3.cpu-0.cpu-idle 99.900993 1543010932 + +Default value: B. + =back =head2 Plugin C diff --git a/src/cpufreq.c b/src/cpufreq.c index 35ec07fb..f95b2828 100644 --- a/src/cpufreq.c +++ b/src/cpufreq.c @@ -25,6 +25,12 @@ #include "plugin.h" #include "utils/common/common.h" +#if KERNEL_FREEBSD +#include +#include +#endif + +#if KERNEL_LINUX #define MAX_AVAIL_FREQS 20 static int num_cpu; @@ -71,8 +77,10 @@ static void cpufreq_stats_init(void) { } return; } +#endif /* KERNEL_LINUX */ static int cpufreq_init(void) { +#if KERNEL_LINUX char filename[PATH_MAX]; num_cpu = 0; @@ -96,6 +104,16 @@ static int cpufreq_init(void) { if (num_cpu == 0) plugin_unregister_read("cpufreq"); +#elif KERNEL_FREEBSD + char mib[] = "dev.cpu.0.freq"; + int cpufreq; + size_t cf_len = sizeof(cpufreq); + + if (sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) { + WARNING("cpufreq plugin: sysctl \"%s\" failed.", mib); + plugin_unregister_read("cpufreq"); + } +#endif return 0; } /* int cpufreq_init */ @@ -116,6 +134,7 @@ static void cpufreq_submit(int cpu_num, const char *type, plugin_dispatch_values(&vl); } +#if KERNEL_LINUX static void cpufreq_read_stats(int cpu) { char filename[PATH_MAX]; /* Read total transitions for cpu frequency */ @@ -184,8 +203,10 @@ static void cpufreq_read_stats(int cpu) { } fclose(fh); } +#endif /* KERNEL_LINUX */ static int cpufreq_read(void) { +#if KERNEL_LINUX for (int cpu = 0; cpu < num_cpu; cpu++) { char filename[PATH_MAX]; /* Read cpu frequency */ @@ -206,6 +227,23 @@ static int cpufreq_read(void) { if (report_p_stats) cpufreq_read_stats(cpu); } +#elif KERNEL_FREEBSD + /* FreeBSD currently only has 1 freq setting. See BUGS in cpufreq(4) */ + char mib[] = "dev.cpu.0.freq"; + int cpufreq; + size_t cf_len = sizeof(cpufreq); + + if (sysctlbyname(mib, &cpufreq, &cf_len, NULL, 0) != 0) { + WARNING("cpufreq plugin: sysctl \"%s\" failed.", mib); + return 0; + } + + value_t v; + /* convert Mhz to Hz */ + v.gauge = cpufreq * 1000000.0; + + cpufreq_submit(0, "cpufreq", NULL, &v); +#endif return 0; } /* int cpufreq_read */ diff --git a/src/intel_rdt.c b/src/intel_rdt.c index 193aa77b..a68620ef 100644 --- a/src/intel_rdt.c +++ b/src/intel_rdt.c @@ -1,7 +1,7 @@ /** * collectd - src/intel_rdt.c * - * Copyright(c) 2016-2018 Intel Corporation. All rights reserved. + * Copyright(c) 2016-2019 Intel Corporation. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,30 +23,68 @@ * * Authors: * Serhiy Pshyk + * Starzyk, Mateusz + * Wojciech Andralojc + * Michał Aleksiński **/ #include "collectd.h" #include "utils/common/common.h" #include "utils/config_cores/config_cores.h" - +#include "utils/proc_pids/proc_pids.h" #include #define RDT_PLUGIN "intel_rdt" +/* libpqos v2.0 or newer is required for process monitoring*/ +#undef LIBPQOS2 +#if defined(PQOS_VERSION) && PQOS_VERSION >= 20000 +#define LIBPQOS2 +#endif + +#define RDT_PLUGIN "intel_rdt" + #define RDT_MAX_SOCKETS 8 #define RDT_MAX_SOCKET_CORES 64 #define RDT_MAX_CORES (RDT_MAX_SOCKET_CORES * RDT_MAX_SOCKETS) +#ifdef LIBPQOS2 +/* + * Process name inside comm file is limited to 16 chars. + * More info here: http://man7.org/linux/man-pages/man5/proc.5.html + */ +#define RDT_MAX_NAMES_GROUPS 64 +#define RDT_PROC_PATH "/proc" +#endif /* LIBPQOS2 */ + typedef enum { UNKNOWN = 0, CONFIGURATION_ERROR, } rdt_config_status; +#ifdef LIBPQOS2 +struct rdt_name_group_s { + char *desc; + size_t num_names; + char **names; + proc_pids_t **proc_pids; + size_t monitored_pids_count; + enum pqos_mon_event events; +}; +typedef struct rdt_name_group_s rdt_name_group_t; +#endif /* LIBPQOS2 */ + struct rdt_ctx_s { core_groups_list_t cores; enum pqos_mon_event events[RDT_MAX_CORES]; - struct pqos_mon_data *pgroups[RDT_MAX_CORES]; - size_t num_groups; + struct pqos_mon_data *pcgroups[RDT_MAX_CORES]; +#ifdef LIBPQOS2 + rdt_name_group_t ngroups[RDT_MAX_NAMES_GROUPS]; + struct pqos_mon_data *pngroups[RDT_MAX_NAMES_GROUPS]; + size_t num_ngroups; + proc_pids_t **proc_pids; + size_t num_proc_pids; +#endif /* LIBPQOS2 */ const struct pqos_cpuinfo *pqos_cpu; const struct pqos_cap *pqos_cap; const struct pqos_capability *cap_mon; @@ -57,6 +95,40 @@ static rdt_ctx_t *g_rdt; static rdt_config_status g_state = UNKNOWN; +static int g_interface = -1; + +static void rdt_submit_derive(const char *cgroup, const char *type, + const char *type_instance, derive_t value) { + value_list_t vl = VALUE_LIST_INIT; + + vl.values = &(value_t){.derive = value}; + vl.values_len = 1; + + sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin)); + snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup); + sstrncpy(vl.type, type, sizeof(vl.type)); + if (type_instance) + sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); + + plugin_dispatch_values(&vl); +} + +static void rdt_submit_gauge(const char *cgroup, const char *type, + const char *type_instance, gauge_t value) { + value_list_t vl = VALUE_LIST_INIT; + + vl.values = &(value_t){.gauge = value}; + vl.values_len = 1; + + sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin)); + snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup); + sstrncpy(vl.type, type, sizeof(vl.type)); + if (type_instance) + sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); + + plugin_dispatch_values(&vl); +} + #if COLLECT_DEBUG static void rdt_dump_cgroups(void) { char cores[RDT_MAX_CORES * 4]; @@ -65,9 +137,9 @@ static void rdt_dump_cgroups(void) { return; DEBUG(RDT_PLUGIN ": Core Groups Dump"); - DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->num_groups); + DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->cores.num_cgroups); - for (size_t i = 0; i < g_rdt->num_groups; i++) { + for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) { core_group_t *cgroup = g_rdt->cores.cgroups + i; memset(cores, 0, sizeof(cores)); @@ -85,45 +157,750 @@ static void rdt_dump_cgroups(void) { return; } +#ifdef LIBPQOS2 +static void rdt_dump_ngroups(void) { + + char names[DATA_MAX_NAME_LEN]; + + if (g_rdt == NULL) + return; + + DEBUG(RDT_PLUGIN ": Process Names Groups Dump"); + DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->num_ngroups); + + for (size_t i = 0; i < g_rdt->num_ngroups; i++) { + memset(names, 0, sizeof(names)); + for (size_t j = 0; j < g_rdt->ngroups[i].num_names; j++) + snprintf(names + strlen(names), sizeof(names) - strlen(names) - 1, " %s", + g_rdt->ngroups[i].names[j]); + + DEBUG(RDT_PLUGIN ": group[%d]:", (int)i); + DEBUG(RDT_PLUGIN ": description: %s", g_rdt->ngroups[i].desc); + DEBUG(RDT_PLUGIN ": process names:%s", names); + DEBUG(RDT_PLUGIN ": events: 0x%X", g_rdt->ngroups[i].events); + } + + return; +} +#endif /* LIBPQOS2 */ + static inline double bytes_to_kb(const double bytes) { return bytes / 1024.0; } static inline double bytes_to_mb(const double bytes) { return bytes / (1024.0 * 1024.0); } -static void rdt_dump_data(void) { +static void rdt_dump_cores_data(void) { +/* + * CORE - monitored group of cores + * RMID - Resource Monitoring ID associated with the monitored group + * This is not available for monitoring with resource control + * LLC - last level cache occupancy + * MBL - local memory bandwidth + * MBR - remote memory bandwidth + */ +#ifdef LIBPQOS2 + if (g_interface == PQOS_INTER_OS_RESCTRL_MON) { + DEBUG(RDT_PLUGIN ": CORE LLC[KB] MBL[MB] MBR[MB]"); + } else { + DEBUG(RDT_PLUGIN ": CORE RMID LLC[KB] MBL[MB] MBR[MB]"); + } +#else + DEBUG(RDT_PLUGIN ": CORE RMID LLC[KB] MBL[MB] MBR[MB]"); +#endif /* LIBPQOS2 */ + + for (int i = 0; i < g_rdt->cores.num_cgroups; i++) { + const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values; + + double llc = bytes_to_kb(pv->llc); + double mbr = bytes_to_mb(pv->mbm_remote_delta); + double mbl = bytes_to_mb(pv->mbm_local_delta); +#ifdef LIBPQOS2 + if (g_interface == PQOS_INTER_OS_RESCTRL_MON) { + DEBUG(RDT_PLUGIN ": [%s] %10.1f %10.1f %10.1f", + g_rdt->cores.cgroups[i].desc, llc, mbl, mbr); + } else { + DEBUG(RDT_PLUGIN ": [%s] %8u %10.1f %10.1f %10.1f", + g_rdt->cores.cgroups[i].desc, g_rdt->pcgroups[i]->poll_ctx[0].rmid, + llc, mbl, mbr); + } +#else + DEBUG(RDT_PLUGIN ": [%s] %8u %10.1f %10.1f %10.1f", + g_rdt->cores.cgroups[i].desc, g_rdt->pcgroups[i]->poll_ctx[0].rmid, + llc, mbl, mbr); +#endif /* LIBPQOS2 */ + } +} + +#ifdef LIBPQOS2 +static void rdt_dump_pids_data(void) { /* - * CORE - monitored group of cores - * RMID - Resource Monitoring ID associated with the monitored group + * NAME - monitored group of processes + * PIDs - list of PID numbers in the NAME group * LLC - last level cache occupancy * MBL - local memory bandwidth * MBR - remote memory bandwidth */ - DEBUG(" CORE RMID LLC[KB] MBL[MB] MBR[MB]"); - for (size_t i = 0; i < g_rdt->num_groups; i++) { - const struct pqos_event_values *pv = &g_rdt->pgroups[i]->values; + + DEBUG(RDT_PLUGIN ": NAME PIDs"); + char pids[DATA_MAX_NAME_LEN]; + for (size_t i = 0; i < g_rdt->num_ngroups; ++i) { + memset(pids, 0, sizeof(pids)); + for (size_t j = 0; j < g_rdt->ngroups[i].num_names; ++j) { + pids_list_t *list = g_rdt->ngroups[i].proc_pids[j]->curr; + for (size_t k = 0; k < list->size; k++) + snprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u", + list->pids[k]); + } + DEBUG(RDT_PLUGIN ": [%s] %s", g_rdt->ngroups[i].desc, pids); + } + + DEBUG(RDT_PLUGIN ": NAME LLC[KB] MBL[MB] MBR[MB]"); + for (size_t i = 0; i < g_rdt->num_ngroups; i++) { + + const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values; double llc = bytes_to_kb(pv->llc); double mbr = bytes_to_mb(pv->mbm_remote_delta); double mbl = bytes_to_mb(pv->mbm_local_delta); - DEBUG(" [%s] %8u %10.1f %10.1f %10.1f", g_rdt->cores.cgroups[i].desc, - g_rdt->pgroups[i]->poll_ctx[0].rmid, llc, mbl, mbr); + DEBUG(RDT_PLUGIN ": [%s] %10.1f %10.1f %10.1f", g_rdt->ngroups[i].desc, + llc, mbl, mbr); + } +} +#endif /* LIBPQOS2 */ +#endif /* COLLECT_DEBUG */ + +#ifdef LIBPQOS2 +static int isdupstr(const char *names[], const size_t size, const char *name) { + for (size_t i = 0; i < size; i++) + if (strncmp(names[i], name, (size_t)MAX_PROC_NAME_LEN) == 0) + return 1; + + return 0; +} + +/* + * NAME + * strlisttoarray + * + * DESCRIPTION + * Converts string representing list of strings into array of strings. + * Allowed format is: + * name,name1,name2,name3 + * + * PARAMETERS + * `str_list' String representing list of strings. + * `names' Array to put extracted strings into. + * `names_num' Variable to put number of extracted strings. + * + * RETURN VALUE + * Number of elements placed into names. + */ +static int strlisttoarray(char *str_list, char ***names, size_t *names_num) { + char *saveptr = NULL; + + if (str_list == NULL || names == NULL) + return -EINVAL; + + if (strstr(str_list, ",,")) { + /* strtok ignores empty words between separators. + * This condition handles that by rejecting strings + * with consecutive seprators */ + ERROR(RDT_PLUGIN ": Empty process name"); + return -EINVAL; + } + + for (;;) { + char *token = strtok_r(str_list, ",", &saveptr); + if (token == NULL) + break; + + str_list = NULL; + + while (isspace(*token)) + token++; + + if (*token == '\0') + continue; + + if ((isdupstr((const char **)*names, *names_num, token))) { + ERROR(RDT_PLUGIN ": Duplicated process name \'%s\' in group \'%s\'", + token, str_list); + return -EINVAL; + } else { + if (0 != strarray_add(names, names_num, token)) { + ERROR(RDT_PLUGIN ": Error allocating process name string"); + return -ENOMEM; + } + } + } + + return 0; +} + +/* + * NAME + * ngroup_cmp + * + * DESCRIPTION + * Function to compare names in two name groups. + * + * PARAMETERS + * `ng_a' Pointer to name group a. + * `ng_b' Pointer to name group b. + * + * RETURN VALUE + * 1 if both groups contain the same names + * 0 if none of their names match + * -1 if some but not all names match + */ +static int ngroup_cmp(const rdt_name_group_t *ng_a, + const rdt_name_group_t *ng_b) { + unsigned found = 0; + + assert(ng_a != NULL); + assert(ng_b != NULL); + + const size_t sz_a = (unsigned)ng_a->num_names; + const size_t sz_b = (unsigned)ng_b->num_names; + const char **tab_a = (const char **)ng_a->names; + const char **tab_b = (const char **)ng_b->names; + + for (size_t i = 0; i < sz_a; i++) { + for (size_t j = 0; j < sz_b; j++) + if (strncmp(tab_a[i], tab_b[j], (size_t)MAX_PROC_NAME_LEN) == 0) + found++; + } + /* if no names are the same */ + if (!found) + return 0; + /* if group contains same names */ + if (sz_a == sz_b && sz_b == (size_t)found) + return 1; + /* if not all names are the same */ + return -1; +} + +/* + * NAME + * oconfig_to_ngroups + * + * DESCRIPTION + * Function to set the descriptions and names for each process names group. + * Takes a config option containing list of strings that are used to set + * process group values. + * + * PARAMETERS + * `item' Config option containing process names groups. + * `groups' Table of process name groups to set values in. + * `max_groups' Maximum number of process name groups allowed. + * + * RETURN VALUE + * On success, the number of name groups set up. On error, appropriate + * negative error value. + */ +static int oconfig_to_ngroups(const oconfig_item_t *item, + rdt_name_group_t *groups, + const size_t max_groups) { + int index = 0; + + assert(groups != NULL); + assert(max_groups > 0); + assert(item != NULL); + + for (int j = 0; j < item->values_num; j++) { + int ret; + char value[DATA_MAX_NAME_LEN]; + + if ((item->values[j].value.string == NULL) || + (strlen(item->values[j].value.string) == 0)) { + ERROR(RDT_PLUGIN ": Error - empty group"); + return -EINVAL; + } + + sstrncpy(value, item->values[j].value.string, sizeof(value)); + + ret = strlisttoarray(value, &groups[index].names, &groups[index].num_names); + if (ret != 0 || groups[index].num_names == 0) { + ERROR(RDT_PLUGIN ": Error parsing process names group (%s)", + item->values[j].value.string); + return -EINVAL; + } + + /* set group description info */ + groups[index].desc = sstrdup(item->values[j].value.string); + if (groups[index].desc == NULL) { + ERROR(RDT_PLUGIN ": Error allocating name group description"); + return -ENOMEM; + } + + groups[index].proc_pids = NULL; + groups[index].monitored_pids_count = 0; + + index++; + + if (index >= (const int)max_groups) { + WARNING(RDT_PLUGIN ": Too many process names groups configured"); + return index; + } + } + + return index; +} + +/* + * NAME + * rdt_free_ngroups + * + * DESCRIPTION + * Function to deallocate memory allocated for name groups. + * + * PARAMETERS + * `rdt' Pointer to rdt context + */ +static void rdt_free_ngroups(rdt_ctx_t *rdt) { + for (int i = 0; i < RDT_MAX_NAMES_GROUPS; i++) { + if (rdt->ngroups[i].desc) + DEBUG(RDT_PLUGIN ": Freeing pids \'%s\' group\'s data...", + rdt->ngroups[i].desc); + sfree(rdt->ngroups[i].desc); + strarray_free(rdt->ngroups[i].names, rdt->ngroups[i].num_names); + + if (rdt->ngroups[i].proc_pids) + proc_pids_free(rdt->ngroups[i].proc_pids, rdt->ngroups[i].num_names); + + rdt->ngroups[i].num_names = 0; + sfree(rdt->pngroups[i]); + } + if (rdt->proc_pids) + sfree(rdt->proc_pids); + + rdt->num_ngroups = 0; +} + +/* + * NAME + * rdt_config_ngroups + * + * DESCRIPTION + * Reads name groups configuration. + * + * PARAMETERS + * `rdt` Pointer to rdt context + * `item' Config option containing process names groups. + * + * RETURN VALUE + * 0 on success. Negative number on error. + */ +static int rdt_config_ngroups(rdt_ctx_t *rdt, const oconfig_item_t *item) { + int n = 0; + enum pqos_mon_event events = 0; + + if (item == NULL) { + DEBUG(RDT_PLUGIN ": ngroups_config: Invalid argument."); + return -EINVAL; + } + + DEBUG(RDT_PLUGIN ": Process names groups [%d]:", item->values_num); + for (int j = 0; j < item->values_num; j++) { + if (item->values[j].type != OCONFIG_TYPE_STRING) { + ERROR(RDT_PLUGIN + ": given process names group value is not a string [idx=%d]", + j); + return -EINVAL; + } + DEBUG(RDT_PLUGIN ": [%d]: %s", j, item->values[j].value.string); + } + + n = oconfig_to_ngroups(item, rdt->ngroups, RDT_MAX_NAMES_GROUPS); + if (n < 0) { + rdt_free_ngroups(rdt); + ERROR(RDT_PLUGIN ": Error parsing process name groups configuration."); + return -EINVAL; + } + + /* validate configured process name values */ + for (int group_idx = 0; group_idx < n; group_idx++) { + DEBUG(RDT_PLUGIN ": checking group [%d]: %s", group_idx, + rdt->ngroups[group_idx].desc); + for (size_t name_idx = 0; name_idx < rdt->ngroups[group_idx].num_names; + name_idx++) { + DEBUG(RDT_PLUGIN ": checking process name [%zu]: %s", name_idx, + rdt->ngroups[group_idx].names[name_idx]); + if (!proc_pids_is_name_valid(rdt->ngroups[group_idx].names[name_idx])) { + ERROR(RDT_PLUGIN ": Process name group '%s' contains invalid name '%s'", + rdt->ngroups[group_idx].desc, + rdt->ngroups[group_idx].names[name_idx]); + rdt_free_ngroups(rdt); + return -EINVAL; + } + } + } + + if (n == 0) { + ERROR(RDT_PLUGIN ": Empty process name groups configured."); + return -EINVAL; + } + + /* Get all available events on this platform */ + for (unsigned i = 0; i < rdt->cap_mon->u.mon->num_events; i++) + events |= rdt->cap_mon->u.mon->events[i].type; + + events &= ~(PQOS_PERF_EVENT_LLC_MISS); + + DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events); + + rdt->num_ngroups = n; + for (int i = 0; i < n; i++) { + for (int j = 0; j < i; j++) { + int found = ngroup_cmp(&rdt->ngroups[j], &rdt->ngroups[i]); + if (found != 0) { + rdt_free_ngroups(rdt); + ERROR(RDT_PLUGIN + ": Cannot monitor same process name in different groups."); + return -EINVAL; + } + } + + rdt->ngroups[i].events = events; + rdt->pngroups[i] = calloc(1, sizeof(*rdt->pngroups[i])); + if (rdt->pngroups[i] == NULL) { + rdt_free_ngroups(rdt); + ERROR(RDT_PLUGIN + ": Failed to allocate memory for process name monitoring data."); + return -ENOMEM; + } + } + + return 0; +} + +/* + * NAME + * rdt_refresh_ngroup + * + * DESCRIPTION + * Refresh pids monitored by name group. + * + * PARAMETERS + * `ngroup` Pointer to name group. + * `group_mon_data' PQoS monitoring context. + * + * RETURN VALUE + * 0 on success. Negative number on error. + */ +static int rdt_refresh_ngroup(rdt_name_group_t *ngroup, + struct pqos_mon_data *group_mon_data) { + + int result = 0; + + if (NULL == ngroup) + return -1; + + if (NULL == ngroup->proc_pids) { + ERROR(RDT_PLUGIN + ": rdt_refresh_ngroup: \'%s\' uninitialized process pids array.", + ngroup->desc); + + return -1; + } + + DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group.", + ngroup->desc); + + proc_pids_t **proc_pids = ngroup->proc_pids; + pids_list_t added_pids; + pids_list_t removed_pids; + + memset(&added_pids, 0, sizeof(added_pids)); + memset(&removed_pids, 0, sizeof(removed_pids)); + + for (size_t i = 0; i < ngroup->num_names; ++i) { + int diff_result = pids_list_diff(proc_pids[i], &added_pids, &removed_pids); + if (0 != diff_result) { + ERROR(RDT_PLUGIN + ": rdt_refresh_ngroup: \'%s\'. Error [%d] during PID diff.", + ngroup->desc, diff_result); + result = -1; + goto cleanup; + } + } + + DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group, added: " + "%u, removed: %u.", + ngroup->desc, (unsigned)added_pids.size, (unsigned)removed_pids.size); + + if (added_pids.size > 0) { + + /* no pids are monitored for this group yet: start monitoring */ + if (0 == ngroup->monitored_pids_count) { + + int start_result = + pqos_mon_start_pids(added_pids.size, added_pids.pids, ngroup->events, + (void *)ngroup->desc, group_mon_data); + if (PQOS_RETVAL_OK == start_result) { + ngroup->monitored_pids_count = added_pids.size; + } else { + ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while " + "STARTING pids monitoring", + ngroup->desc, start_result); + result = -1; + goto pqos_error_recovery; + } + + } else { + + int add_result = + pqos_mon_add_pids(added_pids.size, added_pids.pids, group_mon_data); + if (PQOS_RETVAL_OK == add_result) + ngroup->monitored_pids_count += added_pids.size; + else { + ERROR(RDT_PLUGIN + ": rdt_refresh_ngroup: \'%s\'. Error [%d] while ADDING pids.", + ngroup->desc, add_result); + result = -1; + goto pqos_error_recovery; + } + } + } + + if (removed_pids.size > 0) { + + /* all pids are removed: stop monitoring */ + if (removed_pids.size == ngroup->monitored_pids_count) { + /* all pids for this group are lost: stop monitoring */ + int stop_result = pqos_mon_stop(group_mon_data); + if (PQOS_RETVAL_OK != stop_result) { + ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while " + "STOPPING monitoring", + ngroup->desc, stop_result); + result = -1; + goto pqos_error_recovery; + } + ngroup->monitored_pids_count = 0; + } else { + int remove_result = pqos_mon_remove_pids( + removed_pids.size, removed_pids.pids, group_mon_data); + if (PQOS_RETVAL_OK == remove_result) { + ngroup->monitored_pids_count -= removed_pids.size; + } else { + ERROR(RDT_PLUGIN + ": rdt_refresh_ngroup: \'%s\'. Error [%d] while REMOVING pids.", + ngroup->desc, remove_result); + result = -1; + goto pqos_error_recovery; + } + } } + + goto cleanup; + +pqos_error_recovery: + /* Why? + * Resources might be temporary unavailable. + * + * How? + * Collectd will halt the reading thread for this + * plugin if it returns an error. + * Consecutive errors will be increasing the read period + * up to 1 day interval. + * On pqos error stop monitoring current group + * and reset the proc_pids array + * monitoring will be restarted on next collectd read cycle + */ + DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' group RESET after error.", + ngroup->desc); + pqos_mon_stop(group_mon_data); + for (size_t i = 0; i < ngroup->num_names; ++i) + if (ngroup->proc_pids[i]->curr) + ngroup->proc_pids[i]->curr->size = 0; + + ngroup->monitored_pids_count = 0; + +cleanup: + pids_list_clear(&added_pids); + pids_list_clear(&removed_pids); + + return result; } + +/* + * NAME + * read_pids_data + * + * DESCRIPTION + * Poll monitoring statistics for name groups + * + * RETURN VALUE + * 0 on success. Negative number on error. + */ +static int read_pids_data() { + + if (0 == g_rdt->num_ngroups) { + DEBUG(RDT_PLUGIN ": read_pids_data: not configured - PIDs read skipped"); + return 0; + } + + DEBUG(RDT_PLUGIN ": read_pids_data: Scanning active groups"); + struct pqos_mon_data *active_groups[RDT_MAX_NAMES_GROUPS] = {0}; + size_t active_group_idx = 0; + for (size_t pngroups_idx = 0; + pngroups_idx < STATIC_ARRAY_SIZE(g_rdt->pngroups); ++pngroups_idx) + if (0 != g_rdt->ngroups[pngroups_idx].monitored_pids_count) + active_groups[active_group_idx++] = g_rdt->pngroups[pngroups_idx]; + + int ret = 0; + + if (0 == active_group_idx) { + DEBUG(RDT_PLUGIN ": read_pids_data: no active groups - PIDs read skipped"); + goto groups_refresh; + } + + DEBUG(RDT_PLUGIN ": read_pids_data: PIDs data polling"); + + int poll_result = pqos_mon_poll(active_groups, active_group_idx); + if (poll_result != PQOS_RETVAL_OK) { + ERROR(RDT_PLUGIN ": read_pids_data: Failed to poll monitoring data for " + "pids. Error [%d].", + poll_result); + ret = -poll_result; + goto groups_refresh; + } + + for (size_t i = 0; i < g_rdt->num_ngroups; i++) { + enum pqos_mon_event mbm_events = + (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW | + PQOS_MON_EVENT_RMEM_BW); + + if (g_rdt->pngroups[i] == NULL || + g_rdt->ngroups[i].monitored_pids_count == 0) + continue; + + const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values; + + /* Submit only monitored events data */ + + if (g_rdt->ngroups[i].events & PQOS_MON_EVENT_L3_OCCUP) + rdt_submit_gauge(g_rdt->ngroups[i].desc, "bytes", "llc", pv->llc); + + if (g_rdt->ngroups[i].events & PQOS_PERF_EVENT_IPC) + rdt_submit_gauge(g_rdt->ngroups[i].desc, "ipc", NULL, pv->ipc); + + if (g_rdt->ngroups[i].events & mbm_events) { + rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "local", + pv->mbm_local_delta); + rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "remote", + pv->mbm_remote_delta); + } + } + +#if COLLECT_DEBUG + rdt_dump_pids_data(); #endif /* COLLECT_DEBUG */ +groups_refresh: + ret = proc_pids_update(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids); + if (0 != ret) { + ERROR(RDT_PLUGIN ": Initial update of proc pids failed"); + return ret; + } + + for (size_t i = 0; i < g_rdt->num_ngroups; i++) { + int refresh_result = + rdt_refresh_ngroup(&(g_rdt->ngroups[i]), g_rdt->pngroups[i]); + + if (0 != refresh_result) { + ERROR(RDT_PLUGIN ": read_pids_data: NGroup %zu refresh failed. Error: %d", + i, refresh_result); + if (0 == ret) { + /* refresh error will be escalated only if there were no + * errors before. + */ + ret = refresh_result; + } + } + } + + assert(ret <= 0); + return ret; +} + +/* + * NAME + * rdt_init_pids_monitoring + * + * DESCRIPTION + * Initialize pids monitoring for all name groups + */ +static void rdt_init_pids_monitoring() { + for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) { + /* + * Each group must have not-null proc_pids array. + * Initial refresh is not mandatory for proper + * PIDs statistics detection. + */ + rdt_name_group_t *ng = &g_rdt->ngroups[group_idx]; + int init_result = + proc_pids_init((const char **)ng->names, ng->num_names, &ng->proc_pids); + if (0 != init_result) { + ERROR(RDT_PLUGIN + ": Initialization of proc_pids for group %zu failed. Error: %d", + group_idx, init_result); + continue; + } + + /* update global proc_pids table */ + proc_pids_t **proc_pids = realloc(g_rdt->proc_pids, + (g_rdt->num_proc_pids + ng->num_names) * + sizeof(*g_rdt->proc_pids)); + if (NULL == proc_pids) { + ERROR(RDT_PLUGIN ": Alloc error\n"); + continue; + } + + for (size_t i = 0; i < ng->num_names; i++) + proc_pids[g_rdt->num_proc_pids + i] = ng->proc_pids[i]; + + g_rdt->proc_pids = proc_pids; + g_rdt->num_proc_pids += ng->num_names; + } + + if (g_rdt->num_ngroups > 0) { + int update_result = + proc_pids_update(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids); + if (0 != update_result) + ERROR(RDT_PLUGIN ": Initial update of proc pids failed"); + } + + for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) { + int refresh_result = rdt_refresh_ngroup(&(g_rdt->ngroups[group_idx]), + g_rdt->pngroups[group_idx]); + if (0 != refresh_result) + ERROR(RDT_PLUGIN ": Initial refresh of group %zu failed. Error: %d", + group_idx, refresh_result); + } +} +#endif /* LIBPQOS2 */ +/* + * NAME + * rdt_free_cgroups + * + * DESCRIPTION + * Function to deallocate memory allocated for core groups. + */ static void rdt_free_cgroups(void) { config_cores_cleanup(&g_rdt->cores); for (int i = 0; i < RDT_MAX_CORES; i++) { - sfree(g_rdt->pgroups[i]); + sfree(g_rdt->pcgroups[i]); } + g_rdt->cores.num_cgroups = 0; } static int rdt_default_cgroups(void) { unsigned num_cores = g_rdt->pqos_cpu->num_cores; - g_rdt->cores.cgroups = calloc(num_cores, sizeof(*g_rdt->cores.cgroups)); + g_rdt->cores.cgroups = calloc(num_cores, sizeof(*(g_rdt->cores.cgroups))); if (g_rdt->cores.cgroups == NULL) { ERROR(RDT_PLUGIN ": Error allocating core groups array"); return -ENOMEM; @@ -213,9 +990,9 @@ static int rdt_config_cgroups(oconfig_item_t *item) { g_rdt->pqos_cpu->num_cores); DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events); - g_rdt->num_groups = n; - for (size_t i = 0; i < n; i++) { - for (size_t j = 0; j < i; j++) { + g_rdt->cores.num_cgroups = n; + for (int i = 0; i < n; i++) { + for (int j = 0; j < i; j++) { int found = 0; found = config_cores_cmp_cgroups(&g_rdt->cores.cgroups[j], &g_rdt->cores.cgroups[i]); @@ -227,8 +1004,8 @@ static int rdt_config_cgroups(oconfig_item_t *item) { } g_rdt->events[i] = events; - g_rdt->pgroups[i] = calloc(1, sizeof(*g_rdt->pgroups[i])); - if (g_rdt->pgroups[i] == NULL) { + g_rdt->pcgroups[i] = calloc(1, sizeof(*g_rdt->pcgroups[i])); + if (g_rdt->pcgroups[i] == NULL) { rdt_free_cgroups(); ERROR(RDT_PLUGIN ": Failed to allocate memory for monitoring data."); return -ENOMEM; @@ -259,14 +1036,34 @@ static int rdt_preinit(void) { struct pqos_config pqos = {.fd_log = -1, .callback_log = rdt_pqos_log, .context_log = NULL, - .verbose = 0}; + .verbose = 0, +#ifdef LIBPQOS2 + .interface = PQOS_INTER_OS_RESCTRL_MON}; + DEBUG(RDT_PLUGIN ": Initializing PQoS with RESCTRL interface"); +#else + .interface = PQOS_INTER_MSR}; + DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface"); +#endif ret = pqos_init(&pqos); + DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret); + +#ifdef LIBPQOS2 + if (ret == PQOS_RETVAL_INTER) { + pqos.interface = PQOS_INTER_MSR; + DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface"); + ret = pqos_init(&pqos); + DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret); + } +#endif + if (ret != PQOS_RETVAL_OK) { ERROR(RDT_PLUGIN ": Error initializing PQoS library!"); goto rdt_preinit_error1; } + g_interface = pqos.interface; + ret = pqos_cap_get(&g_rdt->pqos_cap, &g_rdt->pqos_cpu); if (ret != PQOS_RETVAL_OK) { ERROR(RDT_PLUGIN ": Error retrieving PQoS capabilities."); @@ -295,7 +1092,6 @@ rdt_preinit_error2: pqos_fini(); rdt_preinit_error1: - sfree(g_rdt); return -1; @@ -308,25 +1104,66 @@ static int rdt_config(oconfig_item_t *ci) { reports a failure in configuration and aborts */ - return (0); + return 0; } for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; - if (strcasecmp("Cores", child->key) == 0) { - if (rdt_config_cgroups(child) != 0) { + if (strncasecmp("Cores", child->key, (size_t)strlen("Cores")) == 0) { + if (g_rdt->cores.num_cgroups > 0) { + ERROR(RDT_PLUGIN + ": Configuration parameter \"%s\" can be used only once.", + child->key); g_state = CONFIGURATION_ERROR; + } else if (rdt_config_cgroups(child) != 0) + g_state = CONFIGURATION_ERROR; + + if (g_state == CONFIGURATION_ERROR) /* if we return -1 at this point collectd reports a failure in configuration and aborts */ - return (0); - } + return 0; #if COLLECT_DEBUG rdt_dump_cgroups(); #endif /* COLLECT_DEBUG */ + } else if (strncasecmp("Processes", child->key, + (size_t)strlen("Processes")) == 0) { +#ifdef LIBPQOS2 + if (g_interface != PQOS_INTER_OS_RESCTRL_MON) { + ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported. " + "Resctrl monitoring is needed for PIDs monitoring.", + child->key); + g_state = CONFIGURATION_ERROR; + } + + else if (g_rdt->num_ngroups > 0) { + ERROR(RDT_PLUGIN + ": Configuration parameter \"%s\" can be used only once.", + child->key); + g_state = CONFIGURATION_ERROR; + } + + else if (rdt_config_ngroups(g_rdt, child) != 0) + g_state = CONFIGURATION_ERROR; + + if (g_state == CONFIGURATION_ERROR) + /* if we return -1 at this point collectd + reports a failure in configuration and + aborts + */ + return 0; + +#if COLLECT_DEBUG + rdt_dump_ngroups(); +#endif /* COLLECT_DEBUG */ +#else /* !LIBPQOS2 */ + ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported, please " + "recompile collectd with libpqos version 2.0 or newer.", + child->key); +#endif /* LIBPQOS2 */ } else { ERROR(RDT_PLUGIN ": Unknown configuration parameter \"%s\".", child->key); } @@ -335,64 +1172,30 @@ static int rdt_config(oconfig_item_t *ci) { return 0; } -static void rdt_submit_derive(const char *cgroup, const char *type, - const char *type_instance, derive_t value) { - value_list_t vl = VALUE_LIST_INIT; - - vl.values = &(value_t){.derive = value}; - vl.values_len = 1; - - sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin)); - snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup); - sstrncpy(vl.type, type, sizeof(vl.type)); - if (type_instance) - sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); - - plugin_dispatch_values(&vl); -} - -static void rdt_submit_gauge(const char *cgroup, const char *type, - const char *type_instance, gauge_t value) { - value_list_t vl = VALUE_LIST_INIT; - - vl.values = &(value_t){.gauge = value}; - vl.values_len = 1; - - sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin)); - snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup); - sstrncpy(vl.type, type, sizeof(vl.type)); - if (type_instance) - sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); - - plugin_dispatch_values(&vl); -} - -static int rdt_read(__attribute__((unused)) user_data_t *ud) { - int ret; +static int read_cores_data() { - if (g_rdt == NULL) { - ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized."); - return -EINVAL; + if (0 == g_rdt->cores.num_cgroups) { + DEBUG(RDT_PLUGIN ": read_cores_data: not configured - Cores read skipped"); + return 0; } + DEBUG(RDT_PLUGIN ": read_cores_data: Cores data poll"); - ret = pqos_mon_poll(&g_rdt->pgroups[0], (unsigned)g_rdt->num_groups); + int ret = + pqos_mon_poll(&g_rdt->pcgroups[0], (unsigned)g_rdt->cores.num_cgroups); if (ret != PQOS_RETVAL_OK) { - ERROR(RDT_PLUGIN ": Failed to poll monitoring data."); + ERROR(RDT_PLUGIN ": read_cores_data: Failed to poll monitoring data for " + "cores. Error [%d].", + ret); return -1; } -#if COLLECT_DEBUG - rdt_dump_data(); -#endif /* COLLECT_DEBUG */ - - for (size_t i = 0; i < g_rdt->num_groups; i++) { + for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) { core_group_t *cgroup = g_rdt->cores.cgroups + i; - enum pqos_mon_event mbm_events = (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW | PQOS_MON_EVENT_RMEM_BW); - const struct pqos_event_values *pv = &g_rdt->pgroups[i]->values; + const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values; /* Submit only monitored events data */ @@ -410,31 +1213,75 @@ static int rdt_read(__attribute__((unused)) user_data_t *ud) { } } +#if COLLECT_DEBUG + rdt_dump_cores_data(); +#endif /* COLLECT_DEBUG */ + return 0; } -static int rdt_init(void) { - int ret; +static int rdt_read(__attribute__((unused)) user_data_t *ud) { - if (g_state == CONFIGURATION_ERROR) - return -1; + if (g_rdt == NULL) { + ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized."); + return -EINVAL; + } - ret = rdt_preinit(); - if (ret != 0) - return ret; + int cores_read_result = read_cores_data(); + +#ifdef LIBPQOS2 + int pids_read_result = read_pids_data(); +#endif /* LIBPQOS2 */ + + if (0 != cores_read_result) + return cores_read_result; - /* Start monitoring */ - for (size_t i = 0; i < g_rdt->num_groups; i++) { +#ifdef LIBPQOS2 + if (0 != pids_read_result) + return pids_read_result; +#endif /* LIBPQOS2 */ + + return 0; +} + +static void rdt_init_cores_monitoring() { + for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) { core_group_t *cg = g_rdt->cores.cgroups + i; - ret = pqos_mon_start(cg->num_cores, cg->cores, g_rdt->events[i], - (void *)cg->desc, g_rdt->pgroups[i]); + int mon_start_result = + pqos_mon_start(cg->num_cores, cg->cores, g_rdt->events[i], + (void *)cg->desc, g_rdt->pcgroups[i]); + + if (mon_start_result != PQOS_RETVAL_OK) + ERROR(RDT_PLUGIN + ": Error starting cores monitoring group %s (pqos status=%d)", + cg->desc, mon_start_result); + } +} + +static int rdt_init(void) { - if (ret != PQOS_RETVAL_OK) - ERROR(RDT_PLUGIN ": Error starting monitoring group %s (pqos status=%d)", - cg->desc, ret); + if (g_state == CONFIGURATION_ERROR) { + if (g_rdt != NULL) { + if (g_rdt->cores.num_cgroups > 0) + rdt_free_cgroups(); +#ifdef LIBPQOS2 + if (g_rdt->num_ngroups > 0) + rdt_free_ngroups(g_rdt); +#endif + } + return -1; } + int rdt_preinint_result = rdt_preinit(); + if (rdt_preinint_result != 0) + return rdt_preinint_result; + + rdt_init_cores_monitoring(); +#ifdef LIBPQOS2 + rdt_init_pids_monitoring(); +#endif /* LIBPQOS2 */ + return 0; } @@ -446,16 +1293,24 @@ static int rdt_shutdown(void) { if (g_rdt == NULL) return 0; - /* Stop monitoring */ - for (size_t i = 0; i < g_rdt->num_groups; i++) { - pqos_mon_stop(g_rdt->pgroups[i]); + /* Stop monitoring cores */ + for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) { + pqos_mon_stop(g_rdt->pcgroups[i]); } +/* Stop pids monitoring */ +#ifdef LIBPQOS2 + for (size_t i = 0; i < g_rdt->num_ngroups; i++) + pqos_mon_stop(g_rdt->pngroups[i]); +#endif + ret = pqos_fini(); if (ret != PQOS_RETVAL_OK) ERROR(RDT_PLUGIN ": Error shutting down PQoS library."); - rdt_free_cgroups(); +#ifdef LIBPQOS2 + rdt_free_ngroups(g_rdt); +#endif /* LIBPQOS2 */ sfree(g_rdt); return 0; diff --git a/src/intel_rdt_test.c b/src/intel_rdt_test.c new file mode 100644 index 00000000..af5672b9 --- /dev/null +++ b/src/intel_rdt_test.c @@ -0,0 +1,302 @@ +#include "intel_rdt.c" /* sic */ +#include "testing.h" + +/*************************************************************************** + * PQOS mocks + */ +int pqos_mon_reset(void) { return 0; } +int pqos_mon_assoc_get(const unsigned lcore, pqos_rmid_t *rmid) { return 0; } +int pqos_mon_start(const unsigned num_cores, const unsigned *cores, + const enum pqos_mon_event event, void *context, + struct pqos_mon_data *group) { + return 0; +} +#if PQOS_VERSION >= 30000 +int pqos_mon_start_pids(const unsigned num_pids, const pid_t *pids, + const enum pqos_mon_event event, void *context, + struct pqos_mon_data *group) { + return 0; +} +int pqos_mon_add_pids(const unsigned num_pids, const pid_t *pids, + struct pqos_mon_data *group) { + return 0; +} +int pqos_mon_remove_pids(const unsigned num_pids, const pid_t *pids, + struct pqos_mon_data *group) { + return 0; +} + +#else +int pqos_mon_start_pid(const pid_t pids, const enum pqos_mon_event event, + void *context, struct pqos_mon_data *group) { + return 0; +} +#endif +int pqos_mon_stop(struct pqos_mon_data *group) { return 0; } +int pqos_mon_poll(struct pqos_mon_data **groups, const unsigned num_groups) { + return 0; +} + +#if PQOS_VERSION >= 30000 +int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg, + const enum pqos_cdp_config l2_cdp_cfg, + const enum pqos_mba_config mba_cfg) { + return 0; +} +#elif PQOS_VERSION >= 20000 +int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg, + const enum pqos_cdp_config l2_cdp_cfg) { + return 0; +} +#else +int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg) { return 0; } +#endif +int pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id) { + return 0; +} +int pqos_alloc_assoc_get(const unsigned lcore, unsigned *class_id) { return 0; } +int pqos_alloc_assoc_set_pid(const pid_t task, const unsigned class_id) { + return 0; +} +int pqos_alloc_assoc_get_pid(const pid_t task, unsigned *class_id) { return 0; } +int pqos_alloc_assign(const unsigned technology, const unsigned *core_array, + const unsigned core_num, unsigned *class_id) { + return 0; +} +int pqos_alloc_release(const unsigned *core_array, const unsigned core_num) { + return 0; +} +int pqos_alloc_assign_pid(const unsigned technology, const pid_t *task_array, + const unsigned task_num, unsigned *class_id) { + return 0; +} +int pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num) { + return 0; +} +int pqos_init(const struct pqos_config *config) { return 0; } +int pqos_fini(void) { return 0; } +int pqos_cap_get_type(const struct pqos_cap *cap, const enum pqos_cap_type type, + const struct pqos_capability **cap_item) { + return 0; +} +int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) { + return 0; +} + +#ifdef LIBPQOS2 +/*************************************************************************** + * helper functions + */ +rdt_ctx_t *stub_rdt_setup() { + + rdt_ctx_t *rdt = calloc(1, sizeof(*rdt)); + struct pqos_cpuinfo *pqos_cpu = calloc(1, sizeof(*pqos_cpu)); + struct pqos_cap *pqos_cap = calloc(1, sizeof(*pqos_cap)); + struct pqos_cap_mon *mon = calloc(1, sizeof(*mon)); + struct pqos_capability *cap_mon = calloc(1, sizeof(*cap_mon)); + + cap_mon->u.mon = mon; + rdt->pqos_cap = pqos_cap; + rdt->pqos_cpu = pqos_cpu; + rdt->cap_mon = cap_mon; + + return rdt; +} + +void stub_rdt_teardown(rdt_ctx_t *rdt) { + free(rdt->cap_mon->u.mon); + free((void *)rdt->cap_mon); + free((void *)rdt->pqos_cpu); + free((void *)rdt->pqos_cap); + free(rdt); +} + +/*************************************************************************** + * tests + */ +DEF_TEST(rdt_config_ngroups__one_process) { + /* setup */ + rdt_ctx_t *rdt = stub_rdt_setup(); + + oconfig_value_t values[] = { + {.value.string = "proc1", .type = OCONFIG_TYPE_STRING}, + }; + oconfig_item_t config_item = { + .values = values, .values_num = STATIC_ARRAY_SIZE(values), + }; + + /* check */ + int result = rdt_config_ngroups(rdt, &config_item); + EXPECT_EQ_INT(0, result); + EXPECT_EQ_STR(values[0].value.string, rdt->ngroups[0].desc); + EXPECT_EQ_INT(1, rdt->num_ngroups); + + /* cleanup */ + rdt_free_ngroups(rdt); + stub_rdt_teardown(rdt); + + return 0; +} + +DEF_TEST(rdt_config_ngroups__two_groups) { + /* setup */ + rdt_ctx_t *rdt = stub_rdt_setup(); + + oconfig_value_t values[] = { + {.value.string = "proc11,proc12,proc13", .type = OCONFIG_TYPE_STRING}, + {.value.string = "proc21,proc22,proc23", .type = OCONFIG_TYPE_STRING}, + }; + oconfig_item_t config_item = { + .values = values, .values_num = STATIC_ARRAY_SIZE(values), + }; + + /* check */ + int result = rdt_config_ngroups(rdt, &config_item); + EXPECT_EQ_INT(0, result); + EXPECT_EQ_INT(2, rdt->num_ngroups); + EXPECT_EQ_STR("proc11,proc12,proc13", rdt->ngroups[0].desc); + EXPECT_EQ_STR("proc21,proc22,proc23", rdt->ngroups[1].desc); + EXPECT_EQ_STR("proc11", rdt->ngroups[0].names[0]); + EXPECT_EQ_STR("proc12", rdt->ngroups[0].names[1]); + EXPECT_EQ_STR("proc13", rdt->ngroups[0].names[2]); + EXPECT_EQ_STR("proc21", rdt->ngroups[1].names[0]); + EXPECT_EQ_STR("proc22", rdt->ngroups[1].names[1]); + EXPECT_EQ_STR("proc23", rdt->ngroups[1].names[2]); + + /* cleanup */ + rdt_free_ngroups(rdt); + stub_rdt_teardown(rdt); + + return 0; +} + +DEF_TEST(rdt_config_ngroups__too_long_proc_name) { + /* setup */ + rdt_ctx_t *rdt = stub_rdt_setup(); + + oconfig_value_t values[] = { + {.value.string = "_seventeen_chars_", .type = OCONFIG_TYPE_STRING}, + }; + oconfig_item_t config_item = { + .values = values, .values_num = STATIC_ARRAY_SIZE(values), + }; + + /* check */ + int result = rdt_config_ngroups(rdt, &config_item); + EXPECT_EQ_INT(-EINVAL, result); + + /* cleanup */ + stub_rdt_teardown(rdt); + + return 0; +} + +DEF_TEST(rdt_config_ngroups__duplicate_proc_name_between_groups) { + /* setup */ + rdt_ctx_t *rdt = stub_rdt_setup(); + + oconfig_value_t values[] = { + {.value.string = "proc11,proc12,proc", .type = OCONFIG_TYPE_STRING}, + {.value.string = "proc21,proc,proc23", .type = OCONFIG_TYPE_STRING}, + }; + oconfig_item_t config_item = { + .values = values, .values_num = STATIC_ARRAY_SIZE(values), + }; + + /* check */ + int result = rdt_config_ngroups(rdt, &config_item); + EXPECT_EQ_INT(-EINVAL, result); + + /* cleanup */ + stub_rdt_teardown(rdt); + + return 0; +} + +DEF_TEST(rdt_config_ngroups__duplicate_proc_name_in_group) { + /* setup */ + rdt_ctx_t *rdt = stub_rdt_setup(); + + oconfig_value_t values[] = { + {.value.string = "proc11,proc,proc,proc14", .type = OCONFIG_TYPE_STRING}, + }; + oconfig_item_t config_item = { + .values = values, .values_num = STATIC_ARRAY_SIZE(values), + }; + + /* check */ + int result = rdt_config_ngroups(rdt, &config_item); + EXPECT_EQ_INT(-EINVAL, result); + + /* cleanup */ + stub_rdt_teardown(rdt); + + return 0; +} + +DEF_TEST(rdt_config_ngroups__empty_group) { + /* setup */ + rdt_ctx_t *rdt = stub_rdt_setup(); + + oconfig_value_t values[] = { + {.value.string = "proc11,proc12,proc13", .type = OCONFIG_TYPE_STRING}, + {.value.string = "", .type = OCONFIG_TYPE_STRING}, + + }; + oconfig_item_t config_item = { + .values = values, .values_num = STATIC_ARRAY_SIZE(values), + }; + + /* check */ + int result = rdt_config_ngroups(rdt, &config_item); + EXPECT_EQ_INT(-EINVAL, result); + + /* cleanup */ + stub_rdt_teardown(rdt); + + return 0; +} + +DEF_TEST(rdt_config_ngroups__empty_proc_name) { + /* setup */ + rdt_ctx_t *rdt = stub_rdt_setup(); + + oconfig_value_t values[] = { + {.value.string = "proc11,,proc13", .type = OCONFIG_TYPE_STRING}, + }; + oconfig_item_t config_item = { + .values = values, .values_num = STATIC_ARRAY_SIZE(values), + }; + + /* check */ + int result = rdt_config_ngroups(rdt, &config_item); + EXPECT_EQ_INT(-EINVAL, result); + + /* cleanup */ + stub_rdt_teardown(rdt); + + return 0; +} + +int main(void) { + RUN_TEST(rdt_config_ngroups__one_process); + RUN_TEST(rdt_config_ngroups__two_groups); + RUN_TEST(rdt_config_ngroups__too_long_proc_name); + RUN_TEST(rdt_config_ngroups__duplicate_proc_name_between_groups); + RUN_TEST(rdt_config_ngroups__duplicate_proc_name_in_group); + RUN_TEST(rdt_config_ngroups__empty_group); + RUN_TEST(rdt_config_ngroups__empty_proc_name); + END_TEST; +} + +#else +DEF_TEST(pqos12_test_stub) { + EXPECT_EQ_INT(0, 0); + return 0; +} + +int main(void) { + RUN_TEST(pqos12_test_stub); + END_TEST; +} +#endif diff --git a/src/lua.c b/src/lua.c index 45b5b189..9a1ceed7 100644 --- a/src/lua.c +++ b/src/lua.c @@ -40,8 +40,10 @@ #include +#define PLUGIN_READ 1 +#define PLUGIN_WRITE 2 + typedef struct lua_script_s { - char *script_path; lua_State *lua_state; struct lua_script_s *next; } lua_script_t; @@ -80,18 +82,14 @@ static int clua_load_callback(lua_State *L, int callback_ref) /* {{{ */ * garbage collector. */ static int clua_store_thread(lua_State *L, int idx) /* {{{ */ { - if (idx < 0) - idx += lua_gettop(L) + 1; - - /* Copy the thread pointer */ - lua_pushvalue(L, idx); /* +1 = 3 */ - if (!lua_isthread(L, -1)) { - lua_pop(L, 3); /* -3 = 0 */ + if (!lua_isthread(L, idx)) { return -1; } + /* Copy the thread pointer */ + lua_pushvalue(L, idx); + luaL_ref(L, LUA_REGISTRYINDEX); - lua_pop(L, 1); /* -1 = 0 */ return 0; } /* }}} int clua_store_thread */ @@ -266,65 +264,43 @@ static int lua_cb_dispatch_values(lua_State *L) /* {{{ */ static void lua_cb_free(void *data) { clua_callback_data_t *cb = data; free(cb->lua_function_name); + pthread_mutex_destroy(&cb->lock); free(cb); } -static int lua_cb_register_read(lua_State *L) /* {{{ */ +static int lua_cb_register_generic(lua_State *L, int type) /* {{{ */ { int nargs = lua_gettop(L); if (nargs != 1) return luaL_error(L, "Invalid number of arguments (%d != 1)", nargs); - luaL_checktype(L, 1, LUA_TFUNCTION); - - char function_name[DATA_MAX_NAME_LEN]; - snprintf(function_name, sizeof(function_name), "lua/%s", lua_tostring(L, 1)); - - int callback_id = clua_store_callback(L, 1); - if (callback_id < 0) - return luaL_error(L, "%s", "Storing callback function failed"); - - lua_State *thread = lua_newthread(L); - if (thread == NULL) - return luaL_error(L, "%s", "lua_newthread failed"); - clua_store_thread(L, -1); - lua_pop(L, 1); - - clua_callback_data_t *cb = calloc(1, sizeof(*cb)); - if (cb == NULL) - return luaL_error(L, "%s", "calloc failed"); - - cb->lua_state = thread; - cb->callback_id = callback_id; - cb->lua_function_name = strdup(function_name); - pthread_mutex_init(&cb->lock, NULL); - - int status = - plugin_register_complex_read(/* group = */ "lua", - /* name = */ function_name, - /* callback = */ clua_read, - /* interval = */ 0, - &(user_data_t){ - .data = cb, .free_func = lua_cb_free, - }); - - if (status != 0) - return luaL_error(L, "%s", "plugin_register_complex_read failed"); - return 0; -} /* }}} int lua_cb_register_read */ - -static int lua_cb_register_write(lua_State *L) /* {{{ */ -{ - int nargs = lua_gettop(L); + char subname[DATA_MAX_NAME_LEN]; + if (!lua_isfunction(L, 1) && lua_isstring(L, 1)) { + const char *fname = lua_tostring(L, 1); + snprintf(subname, sizeof(subname), "%s()", fname); - if (nargs != 1) - return luaL_error(L, "Invalid number of arguments (%d != 1)", nargs); + lua_getglobal(L, fname); // Push function into stack + lua_remove(L, 1); // Remove string from stack + if (!lua_isfunction(L, -1)) { + return luaL_error(L, "Unable to find function '%s'", fname); + } + } else { + lua_getfield(L, LUA_REGISTRYINDEX, "collectd:callback_num"); + int tmp = lua_tointeger(L, -1); + snprintf(subname, sizeof(subname), "callback_%d", tmp); + lua_pop(L, 1); // Remove old value from stack + lua_pushinteger(L, tmp + 1); + lua_setfield(L, LUA_REGISTRYINDEX, "collectd:callback_num"); // pops value + } luaL_checktype(L, 1, LUA_TFUNCTION); - char function_name[DATA_MAX_NAME_LEN] = ""; - snprintf(function_name, sizeof(function_name), "lua/%s", lua_tostring(L, 1)); + lua_getfield(L, LUA_REGISTRYINDEX, "collectd:script_path"); + char function_name[DATA_MAX_NAME_LEN]; + snprintf(function_name, sizeof(function_name), "lua/%s/%s", + lua_tostring(L, -1), subname); + lua_pop(L, 1); int callback_id = clua_store_callback(L, 1); if (callback_id < 0) @@ -345,16 +321,41 @@ static int lua_cb_register_write(lua_State *L) /* {{{ */ cb->lua_function_name = strdup(function_name); pthread_mutex_init(&cb->lock, NULL); - int status = plugin_register_write(/* name = */ function_name, - /* callback = */ clua_write, + if (PLUGIN_READ == type) { + int status = + plugin_register_complex_read(/* group = */ "lua", + /* name = */ function_name, + /* callback = */ clua_read, + /* interval = */ 0, &(user_data_t){ .data = cb, .free_func = lua_cb_free, }); - if (status != 0) - return luaL_error(L, "%s", "plugin_register_write failed"); - return 0; -} /* }}} int lua_cb_register_write */ + if (status != 0) + return luaL_error(L, "%s", "plugin_register_complex_read failed"); + return 0; + } else if (PLUGIN_WRITE == type) { + int status = plugin_register_write(/* name = */ function_name, + /* callback = */ clua_write, + &(user_data_t){ + .data = cb, .free_func = lua_cb_free, + }); + + if (status != 0) + return luaL_error(L, "%s", "plugin_register_write failed"); + return 0; + } else { + return luaL_error(L, "%s", "lua_cb_register_generic unsupported type"); + } +} /* }}} int lua_cb_register_generic */ + +static int lua_cb_register_read(lua_State *L) { + return lua_cb_register_generic(L, PLUGIN_READ); +} + +static int lua_cb_register_write(lua_State *L) { + return lua_cb_register_generic(L, PLUGIN_WRITE); +} static const luaL_Reg collectdlib[] = { {"log_debug", lua_cb_log_debug}, @@ -389,7 +390,6 @@ static void lua_script_free(lua_script_t *script) /* {{{ */ script->lua_state = NULL; } - sfree(script->script_path); sfree(script); lua_script_free(next); @@ -453,14 +453,7 @@ static int lua_script_load(const char *script_path) /* {{{ */ return status; } - script->script_path = strdup(script_path); - if (script->script_path == NULL) { - ERROR("Lua plugin: strdup failed."); - lua_script_free(script); - return -1; - } - - status = luaL_loadfile(script->lua_state, script->script_path); + status = luaL_loadfile(script->lua_state, script_path); if (status != 0) { ERROR("Lua plugin: luaL_loadfile failed: %s", lua_tostring(script->lua_state, -1)); @@ -469,6 +462,11 @@ static int lua_script_load(const char *script_path) /* {{{ */ return -1; } + lua_pushstring(script->lua_state, script_path); + lua_setfield(script->lua_state, LUA_REGISTRYINDEX, "collectd:script_path"); + lua_pushinteger(script->lua_state, 0); + lua_setfield(script->lua_state, LUA_REGISTRYINDEX, "collectd:callback_num"); + status = lua_pcall(script->lua_state, /* nargs = */ 0, /* nresults = */ LUA_MULTRET, @@ -481,11 +479,8 @@ static int lua_script_load(const char *script_path) /* {{{ */ "In addition, no error message could be retrieved from the stack.", status); else - ERROR("Lua plugin: Executing script \"%s\" failed:\n%s", - script->script_path, errmsg); - - lua_script_free(script); - return -1; + ERROR("Lua plugin: Executing script \"%s\" failed: %s", script_path, + errmsg); } /* Append this script to the global list of scripts. */ @@ -499,6 +494,9 @@ static int lua_script_load(const char *script_path) /* {{{ */ scripts = script; } + if (status != 0) + return -1; + return 0; } /* }}} int lua_script_load */ diff --git a/src/modbus.c b/src/modbus.c index ed53319f..04232c35 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -97,6 +97,12 @@ enum mb_conntype_e /* {{{ */ MBCONN_RTU }; /* }}} */ typedef enum mb_conntype_e mb_conntype_t; +enum mb_uarttype_e /* {{{ */ +{ UARTTYPE_RS232, + UARTTYPE_RS422, + UARTTYPE_RS485 }; /* }}} */ +typedef enum mb_uarttype_e mb_uarttype_t; + struct mb_data_s; typedef struct mb_data_s mb_data_t; struct mb_data_s /* {{{ */ @@ -126,8 +132,9 @@ struct mb_host_s /* {{{ */ char host[DATA_MAX_NAME_LEN]; char node[NI_MAXHOST]; /* TCP hostname or RTU serial device */ /* char service[NI_MAXSERV]; */ - int port; /* for Modbus/TCP */ - int baudrate; /* for Modbus/RTU */ + int port; /* for Modbus/TCP */ + int baudrate; /* for Modbus/RTU */ + mb_uarttype_t uarttype; /* UART type for Modbus/RTU */ mb_conntype_t conntype; mb_slave_t *slaves; @@ -387,6 +394,22 @@ static int mb_init_connection(mb_host_t *host) /* {{{ */ return status; } +#if defined(linux) && LIBMODBUS_VERSION_CHECK(2, 9, 4) + switch (host->uarttype) { + case UARTTYPE_RS485: + if (modbus_rtu_set_serial_mode(host->connection, MODBUS_RTU_RS485)) + DEBUG("Modbus plugin: Setting RS485 mode failed."); + break; + case UARTTYPE_RS422: + /* libmodbus doesn't say anything about full-duplex symmetric RS422 UART */ + break; + case UARTTYPE_RS232: + break; + default: + DEBUG("Modbus plugin: Invalid UART type!."); + } +#endif /* defined(linux) && LIBMODBUS_VERSION_CHECK(2, 9, 4) */ + return 0; } /* }}} int mb_init_connection */ #endif /* !LEGACY_LIBMODBUS */ @@ -983,11 +1006,35 @@ static int mb_config_add_host(oconfig_item_t *ci) /* {{{ */ status = -1; } else if (strcasecmp("Device", child->key) == 0) { status = cf_util_get_string_buffer(child, host->node, sizeof(host->node)); - if (status == 0) + if (status == 0) { host->conntype = MBCONN_RTU; + host->uarttype = UARTTYPE_RS232; + } } else if (strcasecmp("Baudrate", child->key) == 0) status = cf_util_get_int(child, &host->baudrate); - else if (strcasecmp("Interval", child->key) == 0) + else if (strcasecmp("UARTType", child->key) == 0) { +#if defined(linux) && !LEGACY_LIBMODBUS && LIBMODBUS_VERSION_CHECK(2, 9, 4) + char buffer[NI_MAXHOST]; + status = cf_util_get_string_buffer(child, buffer, sizeof(buffer)); + if (status != 0) + break; + if (strncmp(buffer, "RS485", 6) == 0) + host->uarttype = UARTTYPE_RS485; + else if (strncmp(buffer, "RS422", 6) == 0) + host->uarttype = UARTTYPE_RS422; + else if (strncmp(buffer, "RS232", 6) == 0) + host->uarttype = UARTTYPE_RS232; + else { + ERROR("Modbus plugin: The UARTType \"%s\" is unknown.", buffer); + status = -1; + break; + } +#else + ERROR("Modbus plugin: Option `UARTType' not supported. Please " + "upgrade libmodbus to at least 2.9.4"); + return -1; +#endif + } else if (strcasecmp("Interval", child->key) == 0) status = cf_util_get_cdtime(child, &interval); else if (strcasecmp("Slave", child->key) == 0) /* Don't set status: Gracefully continue if a slave fails. */ diff --git a/src/types.db b/src/types.db index e9de64fc..69f59b06 100644 --- a/src/types.db +++ b/src/types.db @@ -51,6 +51,8 @@ df used:GAUGE:0:1125899906842623, free:GAUGE:0:112589990684 df_complex value:GAUGE:0:U df_inodes value:GAUGE:0:U dilution_of_precision value:GAUGE:0:U +disk_allocation value:GAUGE:0:U +disk_capacity value:GAUGE:0:U disk_error value:GAUGE:0:U disk_io_time io_time:DERIVE:0:U, weighted_io_time:DERIVE:0:U disk_latency read:GAUGE:0:U, write:GAUGE:0:U @@ -58,6 +60,7 @@ disk_merged read:DERIVE:0:U, write:DERIVE:0:U disk_octets read:DERIVE:0:U, write:DERIVE:0:U disk_ops read:DERIVE:0:U, write:DERIVE:0:U disk_ops_complex value:DERIVE:0:U +disk_physical value:GAUGE:0:U disk_time read:DERIVE:0:U, write:DERIVE:0:U dns_answer value:DERIVE:0:U dns_notify value:DERIVE:0:U diff --git a/src/utils/format_graphite/format_graphite.c b/src/utils/format_graphite/format_graphite.c index d0e047ff..ffef3e2f 100644 --- a/src/utils/format_graphite/format_graphite.c +++ b/src/utils/format_graphite/format_graphite.c @@ -34,6 +34,35 @@ /* Utils functions to format data sets in graphite format. * Largely taken from write_graphite.c as it remains the same formatting */ +/* helper function for reverse_hostname */ +void reverse_string(char *r_host, int len) { + for (int i = 0, j = len - 1; i < j; i++, j--) { + char t = r_host[i]; + r_host[i] = r_host[j]; + r_host[j] = t; + } +} + +void reverse_hostname(char *r_host, char const *orig_host) { + int len_host = strlen(orig_host); + + /* put reversed hostname into working copy */ + for (int i = 0; i < len_host; i++) + r_host[i] = orig_host[len_host - 1 - i]; + r_host[len_host] = '\0'; + + /* reverse labels (except last) */ + int p = 0; + for (int i = 0; i < len_host; i++) + if (r_host[i] == '.') { + reverse_string(&r_host[p], i - p); + p = i + 1; + } + + /* reverse last label */ + reverse_string(&r_host[p], len_host - p); +} + static int gr_format_values(char *ret, size_t ret_len, int ds_num, const data_set_t *ds, const value_list_t *vl, gauge_t const *rates) { @@ -120,7 +149,13 @@ static int gr_format_name_tagged(char *ret, int ret_len, value_list_t const *vl, if (postfix == NULL) postfix = ""; - gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char, 1); + if (flags & GRAPHITE_REVERSE_HOST) { + char r_host[DATA_MAX_NAME_LEN]; + reverse_hostname(r_host, vl->host); + gr_copy_escape_part(n_host, r_host, sizeof(n_host), escape_char, 1); + } else { + gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char, 1); + } gr_copy_escape_part(n_plugin, vl->plugin, sizeof(n_plugin), escape_char, 1); gr_copy_escape_part(n_plugin_instance, vl->plugin_instance, sizeof(n_plugin_instance), escape_char, 1); @@ -198,8 +233,15 @@ static int gr_format_name(char *ret, int ret_len, value_list_t const *vl, bool preserve_separator = (flags & GRAPHITE_PRESERVE_SEPARATOR); - gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char, - preserve_separator); + if (flags & GRAPHITE_REVERSE_HOST) { + char r_host[DATA_MAX_NAME_LEN]; + reverse_hostname(r_host, vl->host); + gr_copy_escape_part(n_host, r_host, sizeof(n_host), escape_char, + preserve_separator); + } else { + gr_copy_escape_part(n_host, vl->host, sizeof(n_host), escape_char, + preserve_separator); + } gr_copy_escape_part(n_plugin, vl->plugin, sizeof(n_plugin), escape_char, preserve_separator); gr_copy_escape_part(n_plugin_instance, vl->plugin_instance, diff --git a/src/utils/format_graphite/format_graphite.h b/src/utils/format_graphite/format_graphite.h index 60b89ae7..4df7db3d 100644 --- a/src/utils/format_graphite/format_graphite.h +++ b/src/utils/format_graphite/format_graphite.h @@ -32,6 +32,7 @@ #define GRAPHITE_DROP_DUPE_FIELDS 0x08 #define GRAPHITE_PRESERVE_SEPARATOR 0x10 #define GRAPHITE_USE_TAGS 0x20 +#define GRAPHITE_REVERSE_HOST 0x40 int format_graphite(char *buffer, size_t buffer_size, const data_set_t *ds, const value_list_t *vl, const char *prefix, diff --git a/src/utils/proc_pids/proc_pids.c b/src/utils/proc_pids/proc_pids.c new file mode 100644 index 00000000..336a9964 --- /dev/null +++ b/src/utils/proc_pids/proc_pids.c @@ -0,0 +1,361 @@ +/** + * collectd - src/utils/proc_pids/proc_pids.c + * + * Copyright(c) 2018-2019 Intel Corporation. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Authors: + * Starzyk, Mateusz + * Wojciech Andralojc + * Michał Aleksiński + **/ + +#include "collectd.h" +#include "utils/common/common.h" +#include "utils/proc_pids/proc_pids.h" + +#define UTIL_NAME "utils_proc_pids" + +void pids_list_free(pids_list_t *list) { + assert(list); + + sfree(list->pids); + sfree(list); +} + +int proc_pids_is_name_valid(const char *name) { + + if (name != NULL) { + unsigned len = strlen(name); + if (len > 0 && len <= MAX_PROC_NAME_LEN) + return 1; + else { + DEBUG(UTIL_NAME + ": Process name \'%s\' is too long. Max supported len is %d chars.", + name, MAX_PROC_NAME_LEN); + } + } + + return 0; +} + +int pids_list_add_pid(pids_list_t *list, const pid_t pid) { + assert(list); + + if (list->allocated == list->size) { + size_t new_allocated = list->allocated + 1 + list->allocated / 10; + pid_t *new_pids = realloc(list->pids, sizeof(pid_t) * new_allocated); + + if (NULL == new_pids) { + ERROR(UTIL_NAME ": Alloc error\n"); + return -1; + } + + list->pids = new_pids; + list->allocated = new_allocated; + } + + list->pids[list->size] = pid; + list->size++; + + return 0; +} + +int pids_list_add_list(pids_list_t *dst, pids_list_t *src) { + assert(dst); + assert(src); + + if (dst->allocated < dst->size + src->size) { + pid_t *new_pids = + realloc(dst->pids, sizeof(pid_t) * (dst->size + src->size)); + + if (NULL == new_pids) { + ERROR(UTIL_NAME ": Alloc error\n"); + return -1; + } + + dst->allocated = dst->size + src->size; + dst->pids = new_pids; + } + + memcpy(dst->pids + dst->size, src->pids, src->size * sizeof(*(src->pids))); + dst->size += src->size; + + return 0; +} + +int pids_list_clear(pids_list_t *list) { + assert(list); + + if (list->pids != NULL) + sfree(list->pids); + + list->size = 0; + list->allocated = 0; + + return 0; +} + +int pids_list_contains_pid(pids_list_t *list, const pid_t pid) { + assert(list); + + for (int i = 0; i < list->size; i++) + if (list->pids[i] == pid) + return 1; + + return 0; +} + +/* + * NAME + * read_proc_name + * + * DESCRIPTION + * Reads process name from given pid directory. + * Strips new-line character (\n). + * + * PARAMETERS + * `procfs_path' Path to systems proc directory (e.g. /proc) + * `pid_entry' Dirent for PID directory + * `name' Output buffer for process name, recommended proc_comm. + * `out_size' Output buffer size, recommended sizeof(proc_comm) + * + * RETURN VALUE + * On success, the number of read bytes (includes stripped \n). + * -1 on file open error +*/ +static int read_proc_name(const char *procfs_path, + const struct dirent *pid_entry, char *name, + const size_t out_size) { + assert(pid_entry); + assert(name); + assert(out_size); + memset(name, 0, out_size); + + const char *comm_file_name = "comm"; + + char *path = ssnprintf_alloc("%s/%s/%s", procfs_path, pid_entry->d_name, + comm_file_name); + if (path == NULL) + return -1; + FILE *f = fopen(path, "r"); + if (f == NULL) { + ERROR(UTIL_NAME ": Failed to open comm file, error: %d\n", errno); + sfree(path); + return -1; + } + size_t read_length = fread(name, sizeof(char), out_size, f); + name[out_size - 1] = '\0'; + fclose(f); + sfree(path); + /* strip new line ending */ + char *newline = strchr(name, '\n'); + if (newline) { + *newline = '\0'; + } + + return read_length; +} + +/* + * NAME + * get_pid_number + * + * DESCRIPTION + * Gets pid number for given /proc/pid directory entry or + * returns error if input directory does not hold PID information. + * + * PARAMETERS + * `entry' Dirent for PID directory + * `pid' PID number to be filled + * + * RETURN VALUE + * 0 on success. -1 on error. + */ +static int get_pid_number(struct dirent *entry, pid_t *pid) { + char *tmp_end; /* used for strtoul error check*/ + + if (pid == NULL || entry == NULL) + return -1; + + if (entry->d_type != DT_DIR) + return -1; + + /* trying to get pid number from directory name*/ + *pid = strtoul(entry->d_name, &tmp_end, 10); + if (*tmp_end != '\0') { + return -1; /* conversion failed, not proc-pid */ + } + /* all checks passed, marking as success */ + return 0; +} + +int proc_pids_init(const char **procs_names_array, + const size_t procs_names_array_size, + proc_pids_t **proc_pids[]) { + + proc_pids_t **proc_pids_array; + assert(proc_pids); + assert(NULL == *proc_pids); + + /* Copy procs names to output array. Initialize pids list with NULL value. */ + proc_pids_array = calloc(procs_names_array_size, sizeof(*proc_pids_array)); + + if (NULL == proc_pids_array) + return -1; + + for (size_t i = 0; i < procs_names_array_size; ++i) { + proc_pids_array[i] = calloc(1, sizeof(**proc_pids_array)); + if (NULL == proc_pids_array[i]) + goto proc_pids_init_error; + + sstrncpy(proc_pids_array[i]->process_name, procs_names_array[i], + STATIC_ARRAY_SIZE(proc_pids_array[i]->process_name)); + proc_pids_array[i]->prev = NULL; + proc_pids_array[i]->curr = NULL; + } + + *proc_pids = proc_pids_array; + + return 0; +proc_pids_init_error: + if (NULL != proc_pids_array) { + for (size_t i = 0; i < procs_names_array_size; ++i) { + free(proc_pids_array[i]); + } + free(proc_pids_array); + } + return -1; +} + +static void swap_proc_pids(proc_pids_t **proc_pids, size_t proc_pids_num) { + for (size_t i = 0; i < proc_pids_num; i++) { + pids_list_t *swap = proc_pids[i]->prev; + proc_pids[i]->prev = proc_pids[i]->curr; + proc_pids[i]->curr = swap; + } +} + +int proc_pids_update(const char *procfs_path, proc_pids_t **proc_pids, + size_t proc_pids_num) { + assert(procfs_path); + assert(proc_pids); + + DIR *proc_dir = opendir(procfs_path); + if (proc_dir == NULL) { + ERROR(UTIL_NAME ": Could not open %s directory, error: %d", procfs_path, + errno); + return -1; + } + + swap_proc_pids(proc_pids, proc_pids_num); + + for (size_t i = 0; i < proc_pids_num; i++) { + if (NULL == proc_pids[i]->curr) + proc_pids[i]->curr = calloc(1, sizeof(*(proc_pids[i]->curr))); + + if (NULL == proc_pids[i]->curr) { + ERROR(UTIL_NAME ": Alloc error\n"); + goto update_error; + } + + proc_pids[i]->curr->size = 0; + } + + /* Go through procfs and find PIDS and their comms */ + struct dirent *entry; + while ((entry = readdir(proc_dir)) != NULL) { + pid_t pid; + int pid_conversion = get_pid_number(entry, &pid); + if (pid_conversion < 0) + continue; + + proc_comm_t comm; + int read_result = + read_proc_name(procfs_path, entry, comm, sizeof(proc_comm_t)); + if (read_result <= 0) + continue; + + /* Try to find comm in input procs array */ + for (size_t i = 0; i < proc_pids_num; ++i) { + if (0 == + strncmp(comm, proc_pids[i]->process_name, STATIC_ARRAY_SIZE(comm))) + pids_list_add_pid(proc_pids[i]->curr, pid); + } + } + + int close_result = closedir(proc_dir); + if (0 != close_result) { + ERROR(UTIL_NAME ": failed to close /proc directory, error: %d", errno); + goto update_error; + } + return 0; + +update_error: + swap_proc_pids(proc_pids, proc_pids_num); + return -1; +} + +int pids_list_diff(proc_pids_t *proc, pids_list_t *added, + pids_list_t *removed) { + assert(proc); + assert(added); + assert(removed); + + added->size = 0; + removed->size = 0; + + if (NULL == proc->prev || 0 == proc->prev->size) { + /* append all PIDs from curr to added*/ + return pids_list_add_list(added, proc->curr); + } else if (NULL == proc->curr || 0 == proc->curr->size) { + /* append all PIDs from prev to removed*/ + return pids_list_add_list(removed, proc->prev); + } + + for (int i = 0; i < proc->prev->size; i++) + if (0 == pids_list_contains_pid(proc->curr, proc->prev->pids[i])) { + int add_result = pids_list_add_pid(removed, proc->prev->pids[i]); + if (add_result < 0) + return add_result; + } + + for (int i = 0; i < proc->curr->size; i++) + if (0 == pids_list_contains_pid(proc->prev, proc->curr->pids[i])) { + int add_result = pids_list_add_pid(added, proc->curr->pids[i]); + if (add_result < 0) + return add_result; + } + + return 0; +} + +int proc_pids_free(proc_pids_t *proc_pids[], size_t proc_pids_num) { + for (size_t i = 0; i < proc_pids_num; i++) { + if (NULL != proc_pids[i]->curr) + pids_list_free(proc_pids[i]->curr); + if (NULL != proc_pids[i]->prev) + pids_list_free(proc_pids[i]->prev); + sfree(proc_pids[i]); + } + sfree(proc_pids); + + return 0; +} diff --git a/src/utils/proc_pids/proc_pids.h b/src/utils/proc_pids/proc_pids.h new file mode 100644 index 00000000..8b19497c --- /dev/null +++ b/src/utils/proc_pids/proc_pids.h @@ -0,0 +1,226 @@ +/** + * collectd - src/utils/proc_pids/proc_pids.h + * + * Copyright(c) 2018-2019 Intel Corporation. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Authors: + * Starzyk, Mateusz + * Wojciech Andralojc + * Michał Aleksiński + **/ + +#include +#include + +/* + * Process name inside comm file is limited to 16 chars. + * More info here: http://man7.org/linux/man-pages/man5/proc.5.html + */ +#define MAX_PROC_NAME_LEN 16 + +/* Helper typedef for process name array + * Extra 1 char is added for string null termination. + */ +typedef char proc_comm_t[MAX_PROC_NAME_LEN + 1]; + +/* List of pids. */ +typedef struct pids_list_s { + pid_t *pids; + size_t size; + size_t allocated; +} pids_list_t; + +/* Holds process name and list of pids assigned to that name */ +typedef struct proc_pids_s { + proc_comm_t process_name; + pids_list_t *prev; + pids_list_t *curr; +} proc_pids_t; + +/* + * NAME + * pids_list_free + * + * DESCRIPTION + * Free all elements of given pids list + * + * PARAMETERS + * `list' Head of target pids_list. + */ +void pids_list_free(pids_list_t *list); + +/* + * NAME + * pids_list_add_pid + * + * DESCRIPTION + * Adds pid at the end of the pids array. + * Reallocates memory for new pid element, it is up to user to free it. + * + * PARAMETERS + * `list' Target pids_list. + * `pid' Pid to be added. + * + * RETURN VALUE + * On success, returns 0. + * -1 on memory allocation error. + */ +int pids_list_add_pid(pids_list_t *list, const pid_t pid); + +/* + * NAME + * pids_list_clear + * + * DESCRIPTION + * Remove all pids from the list + * + * PARAMETERS + * `list' Target pids_list. + * + * RETURN VALUE + * On success, return 0 + */ +int pids_list_clear(pids_list_t *list); + +/* + * NAME + * pids_list_add_list + * + * DESCRIPTION + * Adds pids list at the end of the pids list. + * Allocates memory for new pid elements, it is up to user to free it. + * + * PARAMETERS + * `dst' Target PIDs list. + * `src' Source PIDs list. + * + * RETURN VALUE + * On success, returns 0. + * -1 on memory allocation error. + */ +int pids_list_add_list(pids_list_t *dst, pids_list_t *src); + +/* + * NAME + * pids_list_contains_pid + * + * DESCRIPTION + * Tests if pids list contains specific pid. + * + * PARAMETERS + * `list' pids_list to check. + * `pid' Pid to be searched for. + * + * RETURN VALUE + * If PID found in list, returns 1, + * Otherwise returns 0. + */ +int pids_list_contains_pid(pids_list_t *list, const pid_t pid); + +/* + * NAME + * pids_list_diff + * + * DESCRIPTION + * Searches for differences in two given lists + * + * PARAMETERS + * `proc' List of pids + * `added' New pids which appeared + * `removed' Result array storing pids which disappeared + * RETURN VALUE + * 0 on success. Negative number on error. + */ +int pids_list_diff(proc_pids_t *proc, pids_list_t *added, pids_list_t *removed); + +/* + * NAME + * proc_pids_is_name_valid + * + * DESCRIPTION + * Checks if given string is valid process name. + * + * PARAMETERS + * `name' null-terminated char array + * + * RETURN VALUE + * If given name is a valid process name, returns 1, + * Otherwise returns 0. + */ +int proc_pids_is_name_valid(const char *name); + +/* + * NAME + * proc_pids_init + * + * DESCRIPTION + * Helper function to properly initialize array of proc_pids. + * Allocates memory for proc_pids structs. + * + * PARAMETERS + * `procs_names_array' Array of null-terminated strings with + * process' names to be copied to new array + * `procs_names_array_size' procs_names_array element count + * `proc_pids' Address of pointer, under which new + * array of proc_pids will be allocated. + * Must be NULL. + * RETURN VALUE + * 0 on success. Negative number on error: + * -1: allocation error + */ +int proc_pids_init(const char **procs_names_array, + const size_t procs_names_array_size, + proc_pids_t **proc_pids[]); + +/* + * NAME + * proc_pids_update + * + * DESCRIPTION + * Updates PIDs matching processes's names. + * Searches all PID directories in /proc fs and updates current pids_list. + * + * PARAMETERS + * `procfs_path' Path to systems proc directory (e.g. /proc) + * `proc_pids' Array of proc_pids pointers to be updated. + * `proc_pids_num' proc_pids element count + * + * RETURN VALUE + * 0 on success. -1 on error. + */ +int proc_pids_update(const char *procfs_path, proc_pids_t *proc_pids[], + size_t proc_pids_num); + +/* + * NAME + * proc_pids_free + * + * DESCRIPTION + * Releses memory allocatd for proc_pids + * + * PARAMETERS + * `proc_pids' Array of proc_pids + * `proc_pids_num' proc_pids element count + * + * RETURN VALUE + * 0 on success. -1 on error. + */ +int proc_pids_free(proc_pids_t *proc_pids[], size_t proc_pids_num); diff --git a/src/utils/proc_pids/proc_pids_test.c b/src/utils/proc_pids/proc_pids_test.c new file mode 100644 index 00000000..06b8d39b --- /dev/null +++ b/src/utils/proc_pids/proc_pids_test.c @@ -0,0 +1,505 @@ +#include "testing.h" +#include "utils/proc_pids/proc_pids.c" /* sic */ +#include + +/*************************************************************************** + * helper functions + */ + +typedef struct stub_proc_pid { + proc_comm_t comm; + pid_t pid; +} stub_proc_pid_t; + +static const char *proc_fs = "/tmp/procfs_stub"; + +/* + * NAME + * stub_procfs_setup + * + * DESCRIPTION + * Prepares testing environment by creating temporary + * PID/comm file structure. + * + * PARAMETERS + * `proc_pids_array' Array of stub_proc_pid_t structs. Represents + * which PIDs should hold given process name. + * `proc_pids_array_length' Element count of input array. + * + * RETURN VALUE + * 0 on success. + * -1 on base dir creation error. + * -2 on comm file creation error. + * -3 on comm file write error. + */ +int stub_procfs_setup(const stub_proc_pid_t *proc_pids_array, + const size_t proc_pids_array_length) { + if (mkdir(proc_fs, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) + return -1; + char path[256]; + + for (size_t i = 0; i < proc_pids_array_length; ++i) { + memset(path, 0, sizeof(path)); + snprintf(path, STATIC_ARRAY_SIZE(path), "%s/%d", proc_fs, + proc_pids_array[i].pid); + mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + strncat(path, "/comm", STATIC_ARRAY_SIZE(path) - strlen(path) - 1); + + FILE *fp = fopen(path, "w"); + if (!fp) + return -2; + + size_t slen = strlen(proc_pids_array[i].comm); + size_t wlen = fwrite(proc_pids_array[i].comm, sizeof(char), slen, fp); + fclose(fp); + + if (slen != wlen) + return -3; + } + return 0; +} + +/* + * NAME + * stub_procfs_teardown + * + * DESCRIPTION + * Clears testing environment: removes stub proc files. + * NOTE - This function could be implemented by usage of nftw, but this + * would require #define _XOPEN_SOURCE 500, which + * messes up intel_rdt includes. + * + * RETURN VALUE + * system command result + */ +int stub_procfs_teardown() { + char cmd[256]; + sstrncpy(cmd, "rm -rf ", STATIC_ARRAY_SIZE(cmd)); + strncat(cmd, proc_fs, STATIC_ARRAY_SIZE(cmd) - strlen(cmd) - 1); + return system(cmd); +} + +/* Max PID value. More info: + * http://web.archive.org/web/20111209081734/http://research.cs.wisc.edu/condor/condorg/linux_scalability.html + */ +#define MAX_PID 4194304 +#define MAX_PID_STR "4194304" + +/*************************************************************************** + * tests + */ +DEF_TEST(proc_pids_init__on_nullptr) { + /* setup */ + const char *procs_names_array[] = {"proc1", "proc2", "proc3"}; + const size_t procs_names_array_size = STATIC_ARRAY_SIZE(procs_names_array); + proc_pids_t **proc_pids_array = NULL; + + /* check */ + int result = proc_pids_init(procs_names_array, procs_names_array_size, + &proc_pids_array); + EXPECT_EQ_INT(0, result); + for (size_t i = 0; i < procs_names_array_size; ++i) + EXPECT_EQ_STR(procs_names_array[i], proc_pids_array[i]->process_name); + + /* cleanup */ + proc_pids_free(proc_pids_array, procs_names_array_size); + return 0; +} + +DEF_TEST(pid_list_add_pid__empty_list) { + /* setup */ + pids_list_t *proc_pids_instance = calloc(1, sizeof(*proc_pids_instance)); + pid_t pid = 1234; + + /* check */ + pids_list_add_pid(proc_pids_instance, pid); + EXPECT_EQ_INT(pid, proc_pids_instance->pids[0]); + + /* cleanup */ + pids_list_free(proc_pids_instance); + return 0; +} + +DEF_TEST(pid_list_add_pid__non_empty_list) { + /* setup */ + pids_list_t *proc_pids_instance = calloc(1, sizeof(*proc_pids_instance)); + pid_t pids[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007}; + + /* check */ + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i) + pids_list_add_pid(proc_pids_instance, pids[i]); + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i) { + EXPECT_EQ_INT(pids[i], proc_pids_instance->pids[i]); + } + + /* cleanup */ + pids_list_free(proc_pids_instance); + return 0; +} + +DEF_TEST(pids_list_add_pids_list__non_empty_lists) { + /* setup */ + pid_t pids_array_1[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007}; + pid_t pids_array_2[] = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007}; + pids_list_t *pids_list_1 = calloc(1, sizeof(*pids_list_1)); + pids_list_t *pids_list_2 = calloc(1, sizeof(*pids_list_2)); + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_1); ++i) { + pids_list_add_pid(pids_list_1, pids_array_1[i]); + pids_list_add_pid(pids_list_2, pids_array_2[i]); + } + + /* check */ + int result = pids_list_add_list(pids_list_1, pids_list_2); + EXPECT_EQ_INT(0, result); + EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_2) + + STATIC_ARRAY_SIZE(pids_array_1), + pids_list_1->size); + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_1); ++i) { + EXPECT_EQ_INT(1, pids_list_contains_pid(pids_list_1, pids_array_1[i])); + EXPECT_EQ_INT(1, pids_list_contains_pid(pids_list_1, pids_array_2[i])); + } + + /* setup */ + pids_list_free(pids_list_1); + pids_list_free(pids_list_2); + return 0; +} + +DEF_TEST(pids_list_add_pids_list__add_to_empty) { + /* setup */ + pid_t pids_array[] = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007}; + pids_list_t *pids_list_1 = calloc(1, sizeof(*pids_list_1)); + pids_list_t *pids_list_2 = calloc(1, sizeof(*pids_list_2)); + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array); ++i) + pids_list_add_pid(pids_list_2, pids_array[i]); + + /* check */ + int result = pids_list_add_list(pids_list_1, pids_list_2); + EXPECT_EQ_INT(0, result); + EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array), pids_list_1->size); + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array); ++i) + EXPECT_EQ_INT(1, pids_list_contains_pid(pids_list_1, pids_array[i])); + + /* setup */ + pids_list_free(pids_list_1); + pids_list_free(pids_list_2); + return 0; +} + +DEF_TEST(get_pid_number__valid_dir) { + /* setup */ + struct dirent d; + sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name)); + d.d_type = DT_DIR; + pid_t pid = 0; + + /* check */ + int pid_conversion = get_pid_number(&d, &pid); + + EXPECT_EQ_INT(0, pid_conversion); + EXPECT_EQ_INT(MAX_PID, pid); + + /* cleanup */ + return 0; +} + +DEF_TEST(get_pid_number__invalid_dir_name) { + /* setup */ + struct dirent d; + sstrncpy(d.d_name, "invalid", STATIC_ARRAY_SIZE(d.d_name)); + d.d_type = DT_DIR; + pid_t pid = 0; + + /* check */ + int pid_conversion = get_pid_number(&d, &pid); + + EXPECT_EQ_INT(-1, pid_conversion); + EXPECT_EQ_INT(0, pid); + + /* cleanup */ + return 0; +} + +DEF_TEST(read_proc_name__valid_name) { + /* setup */ + stub_proc_pid_t pp_stubs[] = {{"proc1", MAX_PID}}; + stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs)); + struct dirent d; + sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name)); + d.d_type = DT_DIR; + + /* check */ + proc_comm_t comm; + int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm)); + + EXPECT_EQ_INT(strlen(pp_stubs[0].comm), read_result); + EXPECT_EQ_STR(pp_stubs[0].comm, comm); + + /* cleanup */ + stub_procfs_teardown(); + return 0; +} + +DEF_TEST(read_proc_name__invalid_name) { + /* setup */ + struct dirent d; + sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name)); + d.d_type = DT_DIR; + + /* check */ + proc_comm_t comm; + int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm)); + + EXPECT_EQ_INT(-1, read_result); + + /* cleanup */ + return 0; +} + +DEF_TEST(proc_pids_update__one_proc_many_pid) { + /* setup */ + const char *proc_names[] = {"proc1"}; + stub_proc_pid_t pp_stubs[] = {{"proc1", 1007}, + {"proc1", 1008}, + {"proc1", 1009}, + {"proc2", 1010}, + {"proc3", 1011}}; + proc_pids_t **proc_pids = NULL; + int result; + stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs)); + + result = + proc_pids_init(proc_names, STATIC_ARRAY_SIZE(proc_names), &proc_pids); + EXPECT_EQ_INT(0, result); + + /* check */ + result = proc_pids_update(proc_fs, proc_pids, STATIC_ARRAY_SIZE(proc_names)); + EXPECT_EQ_INT(0, result); + + /* proc name check */ + EXPECT_EQ_STR(proc_names[0], proc_pids[0]->process_name); + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pp_stubs); ++i) { + if (0 == strcmp(pp_stubs[i].comm, proc_names[0])) + /* check if proc struct has correct pids */ + EXPECT_EQ_INT(pids_list_contains_pid(proc_pids[0]->curr, pp_stubs[i].pid), + 1); + else + /* check if proc struct has no incorrect pids */ + EXPECT_EQ_INT(pids_list_contains_pid(proc_pids[0]->curr, pp_stubs[i].pid), + 0); + } + + /* cleanup */ + proc_pids_free(proc_pids, STATIC_ARRAY_SIZE(proc_names)); + stub_procfs_teardown(); + return 0; +} + +DEF_TEST(proc_pids_update__many_proc_many_pid) { + /* setup */ + const char *proc_names[] = {"proc1", "proc2", "proc3"}; + stub_proc_pid_t pp_stubs[] = { + {"proc1", 1007}, {"proc1", 1008}, {"proc1", 1009}, {"proc2", 2007}, + {"proc2", 2008}, {"proc2", 2009}, {"proc3", 3007}, {"proc3", 3008}, + {"proc3", 3009}, {"proc4", 4007}, {"proc4", 4008}, {"proc4", 4009}, + {"proc5", 5007}, {"proc5", 5008}, {"proc5", 5009}}; + proc_pids_t **proc_pids = NULL; + int result; + stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs)); + + result = + proc_pids_init(proc_names, STATIC_ARRAY_SIZE(proc_names), &proc_pids); + EXPECT_EQ_INT(0, result); + + /* check */ + result = proc_pids_update(proc_fs, proc_pids, STATIC_ARRAY_SIZE(proc_names)); + EXPECT_EQ_INT(0, result); + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) { + + /* proc name check */ + EXPECT_EQ_STR(proc_names[i], proc_pids[i]->process_name); + + for (size_t j = 0; j < STATIC_ARRAY_SIZE(pp_stubs); ++j) { + if (0 == strcmp(pp_stubs[j].comm, proc_names[i])) + /* check if proc struct has correct pids */ + EXPECT_EQ_INT( + pids_list_contains_pid(proc_pids[i]->curr, pp_stubs[j].pid), 1); + else + /* check if proc struct has no incorrect pids */ + EXPECT_EQ_INT( + pids_list_contains_pid(proc_pids[i]->curr, pp_stubs[j].pid), 0); + } + } + + /* cleanup */ + proc_pids_free(proc_pids, STATIC_ARRAY_SIZE(proc_names)); + stub_procfs_teardown(); + return 0; +} + +DEF_TEST(pids_list_diff__all_changed) { + /* setup */ + pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007}; + pid_t pids_array_after[] = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007}; + proc_pids_t proc_pids; + pids_list_t curr; + pids_list_t prev; + + prev.pids = pids_array_before; + prev.size = STATIC_ARRAY_SIZE(pids_array_before); + prev.allocated = prev.size; + curr.pids = pids_array_after; + curr.size = STATIC_ARRAY_SIZE(pids_array_after); + curr.allocated = curr.size; + proc_pids.curr = &curr; + proc_pids.prev = &prev; + + pids_list_t *new_pids = calloc(1, sizeof(*new_pids)); + pids_list_t *lost_pids = calloc(1, sizeof(*lost_pids)); + + /* check */ + int result = pids_list_diff(&proc_pids, new_pids, lost_pids); + EXPECT_EQ_INT(0, result); + EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_before), lost_pids->size); + EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_after), new_pids->size); + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_before); ++i) { + EXPECT_EQ_INT(1, pids_list_contains_pid(new_pids, pids_array_after[i])); + EXPECT_EQ_INT(1, pids_list_contains_pid(lost_pids, pids_array_before[i])); + } + + /* cleanup */ + pids_list_free(new_pids); + pids_list_free(lost_pids); + + return 0; +} + +DEF_TEST(pids_list_diff__nothing_changed) { + /* setup */ + pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007}; + proc_pids_t proc_pids; + pids_list_t curr; + pids_list_t prev; + + prev.pids = pids_array_before; + prev.size = STATIC_ARRAY_SIZE(pids_array_before); + prev.allocated = prev.size; + curr.pids = pids_array_before; + curr.size = STATIC_ARRAY_SIZE(pids_array_before); + curr.allocated = curr.size; + proc_pids.curr = &curr; + proc_pids.prev = &prev; + + pids_list_t *new_pids = calloc(1, sizeof(*new_pids)); + pids_list_t *lost_pids = calloc(1, sizeof(*lost_pids)); + + /* check */ + int result = pids_list_diff(&proc_pids, new_pids, lost_pids); + EXPECT_EQ_INT(0, result); + EXPECT_EQ_INT(0, lost_pids->size); + EXPECT_EQ_INT(0, new_pids->size); + + /* cleanup */ + pids_list_free(lost_pids); + pids_list_free(new_pids); + + return 0; +} + +DEF_TEST(pids_list_diff__one_added) { + /* setup */ + pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007}; + pid_t pids_array_after[] = {1000, 1001, 1002, 1003, 1004, + 1005, 1006, 1007, 1008}; + proc_pids_t proc_pids; + pids_list_t curr; + pids_list_t prev; + + prev.pids = pids_array_before; + prev.size = STATIC_ARRAY_SIZE(pids_array_before); + prev.allocated = prev.size; + curr.pids = pids_array_after; + curr.size = STATIC_ARRAY_SIZE(pids_array_after); + curr.allocated = curr.size; + proc_pids.curr = &curr; + proc_pids.prev = &prev; + + pids_list_t *new_pids = calloc(1, sizeof(*new_pids)); + pids_list_t *lost_pids = calloc(1, sizeof(*lost_pids)); + + /* check */ + int result = pids_list_diff(&proc_pids, new_pids, lost_pids); + EXPECT_EQ_INT(0, result); + EXPECT_EQ_INT(0, lost_pids->size); + EXPECT_EQ_INT(1, new_pids->size); + EXPECT_EQ_INT(1008, new_pids->pids[0]); + + /* cleanup */ + pids_list_free(lost_pids); + pids_list_free(new_pids); + + return 0; +} + +DEF_TEST(pids_list_diff__one_removed) { + /* setup */ + pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004, + 1005, 1006, 1007, 1008}; + pid_t pids_array_after[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007}; + + proc_pids_t proc_pids; + pids_list_t curr; + pids_list_t prev; + + prev.pids = pids_array_before; + prev.size = STATIC_ARRAY_SIZE(pids_array_before); + prev.allocated = prev.size; + curr.pids = pids_array_after; + curr.size = STATIC_ARRAY_SIZE(pids_array_after); + curr.allocated = curr.size; + proc_pids.curr = &curr; + proc_pids.prev = &prev; + + pids_list_t *new_pids = calloc(1, sizeof(*new_pids)); + pids_list_t *lost_pids = calloc(1, sizeof(*lost_pids)); + + /* check */ + int result = pids_list_diff(&proc_pids, new_pids, lost_pids); + EXPECT_EQ_INT(0, result); + EXPECT_EQ_INT(0, new_pids->size); + EXPECT_EQ_INT(1, lost_pids->size); + EXPECT_EQ_INT(1008, lost_pids->pids[0]); + + /* cleanup */ + pids_list_free(lost_pids); + pids_list_free(new_pids); + + return 0; +} + +int main(void) { + stub_procfs_teardown(); + RUN_TEST(proc_pids_init__on_nullptr); + RUN_TEST(pid_list_add_pid__empty_list); + RUN_TEST(pid_list_add_pid__non_empty_list); + RUN_TEST(pids_list_add_pids_list__non_empty_lists); + RUN_TEST(pids_list_add_pids_list__add_to_empty); + RUN_TEST(get_pid_number__valid_dir); + RUN_TEST(get_pid_number__invalid_dir_name); + RUN_TEST(read_proc_name__valid_name); + RUN_TEST(read_proc_name__invalid_name); + RUN_TEST(proc_pids_update__one_proc_many_pid); + RUN_TEST(proc_pids_update__many_proc_many_pid); + RUN_TEST(pids_list_diff__all_changed); + RUN_TEST(pids_list_diff__nothing_changed); + RUN_TEST(pids_list_diff__one_added); + RUN_TEST(pids_list_diff__one_removed); + stub_procfs_teardown(); + END_TEST; +} diff --git a/src/virt.c b/src/virt.c index d44aeb76..9d368af6 100644 --- a/src/virt.c +++ b/src/virt.c @@ -117,32 +117,6 @@ typedef struct virt_notif_thread_s { bool is_active; } virt_notif_thread_t; -static const char *config_keys[] = {"Connection", - - "RefreshInterval", - - "Domain", - "BlockDevice", - "BlockDeviceFormat", - "BlockDeviceFormatBasename", - "InterfaceDevice", - "IgnoreSelected", - - "HostnameFormat", - "HostnameMetadataNS", - "HostnameMetadataXPath", - "InterfaceFormat", - - "PluginInstanceFormat", - - "Instances", - "ExtraStats", - "PersistentNotification", - - "ReportBlockDevices", - "ReportNetworkInterfaces", - NULL}; - /* PersistentNotification is false by default */ static bool persistent_notification = false; @@ -465,7 +439,6 @@ const char *domain_reasons[][DOMAIN_STATE_REASON_MAX_SIZE] = { }; #endif /* HAVE_DOM_REASON */ -#define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1) #define NANOSEC_IN_SEC 1e9 #define GET_STATS(_f, _name, ...) \ @@ -500,6 +473,7 @@ static int ignore_device_match(ignorelist_t *, const char *domname, struct block_device { virDomainPtr dom; /* domain */ char *path; /* name of block device */ + bool has_source; /* information whether source is defined or not */ }; /* Actual list of network interfaces found on last refresh. */ @@ -534,7 +508,7 @@ static int add_domain(struct lv_read_state *state, virDomainPtr dom, static void free_block_devices(struct lv_read_state *state); static int add_block_device(struct lv_read_state *state, virDomainPtr dom, - const char *path); + const char *path, bool has_source); static void free_interface_devices(struct lv_read_state *state); static int add_interface_device(struct lv_read_state *state, virDomainPtr dom, @@ -616,6 +590,9 @@ enum ex_stats { ex_stats_job_stats_completed = 1 << 8, ex_stats_job_stats_background = 1 << 9, #endif + ex_stats_disk_allocation = 1 << 10, + ex_stats_disk_capacity = 1 << 11, + ex_stats_disk_physical = 1 << 12 }; static unsigned int extra_stats = ex_stats_none; @@ -643,6 +620,9 @@ static const struct ex_stats_item ex_stats_table[] = { {"job_stats_completed", ex_stats_job_stats_completed}, {"job_stats_background", ex_stats_job_stats_background}, #endif + {"disk_allocation", ex_stats_disk_allocation}, + {"disk_capacity", ex_stats_disk_capacity}, + {"disk_physical", ex_stats_disk_physical}, {NULL, ex_stats_none}, }; @@ -656,7 +636,7 @@ static time_t last_refresh = (time_t)0; static int refresh_lists(struct lv_read_instance *inst); -struct lv_block_info { +struct lv_block_stats { virDomainBlockStatsStruct bi; long long rd_total_times; @@ -666,50 +646,56 @@ struct lv_block_info { long long fl_total_times; }; -static void init_block_info(struct lv_block_info *binfo) { - if (binfo == NULL) +static void init_block_stats(struct lv_block_stats *bstats) { + if (bstats == NULL) return; - binfo->bi.rd_req = -1; - binfo->bi.wr_req = -1; - binfo->bi.rd_bytes = -1; - binfo->bi.wr_bytes = -1; + bstats->bi.rd_req = -1; + bstats->bi.wr_req = -1; + bstats->bi.rd_bytes = -1; + bstats->bi.wr_bytes = -1; - binfo->rd_total_times = -1; - binfo->wr_total_times = -1; - binfo->fl_req = -1; - binfo->fl_total_times = -1; + bstats->rd_total_times = -1; + bstats->wr_total_times = -1; + bstats->fl_req = -1; + bstats->fl_total_times = -1; +} + +static void init_block_info(virDomainBlockInfoPtr binfo) { + binfo->allocation = -1; + binfo->capacity = -1; + binfo->physical = -1; } #ifdef HAVE_BLOCK_STATS_FLAGS -#define GET_BLOCK_INFO_VALUE(NAME, FIELD) \ +#define GET_BLOCK_STATS_VALUE(NAME, FIELD) \ if (!strcmp(param[i].field, NAME)) { \ - binfo->FIELD = param[i].value.l; \ + bstats->FIELD = param[i].value.l; \ continue; \ } -static int get_block_info(struct lv_block_info *binfo, - virTypedParameterPtr param, int nparams) { - if (binfo == NULL || param == NULL) +static int get_block_stats(struct lv_block_stats *bstats, + virTypedParameterPtr param, int nparams) { + if (bstats == NULL || param == NULL) return -1; for (int i = 0; i < nparams; ++i) { /* ignore type. Everything must be LLONG anyway. */ - GET_BLOCK_INFO_VALUE("rd_operations", bi.rd_req); - GET_BLOCK_INFO_VALUE("wr_operations", bi.wr_req); - GET_BLOCK_INFO_VALUE("rd_bytes", bi.rd_bytes); - GET_BLOCK_INFO_VALUE("wr_bytes", bi.wr_bytes); - GET_BLOCK_INFO_VALUE("rd_total_times", rd_total_times); - GET_BLOCK_INFO_VALUE("wr_total_times", wr_total_times); - GET_BLOCK_INFO_VALUE("flush_operations", fl_req); - GET_BLOCK_INFO_VALUE("flush_total_times", fl_total_times); + GET_BLOCK_STATS_VALUE("rd_operations", bi.rd_req); + GET_BLOCK_STATS_VALUE("wr_operations", bi.wr_req); + GET_BLOCK_STATS_VALUE("rd_bytes", bi.rd_bytes); + GET_BLOCK_STATS_VALUE("wr_bytes", bi.wr_bytes); + GET_BLOCK_STATS_VALUE("rd_total_times", rd_total_times); + GET_BLOCK_STATS_VALUE("wr_total_times", wr_total_times); + GET_BLOCK_STATS_VALUE("flush_operations", fl_req); + GET_BLOCK_STATS_VALUE("flush_total_times", fl_total_times); } return 0; } -#undef GET_BLOCK_INFO_VALUE +#undef GET_BLOCK_STATS_VALUE #endif /* HAVE_BLOCK_STATS_FLAGS */ @@ -1002,8 +988,9 @@ static void vcpu_submit(derive_t value, virDomainPtr dom, int vcpu_nr, submit(dom, type, type_instance, &(value_t){.derive = value}, 1); } -static void disk_submit(struct lv_block_info *binfo, virDomainPtr dom, - const char *dev) { +static void disk_block_stats_submit(struct lv_block_stats *bstats, + virDomainPtr dom, const char *dev, + virDomainBlockInfoPtr binfo) { char *dev_copy = strdup(dev); const char *type_instance = dev_copy; @@ -1022,34 +1009,58 @@ static void disk_submit(struct lv_block_info *binfo, virDomainPtr dom, snprintf(flush_type_instance, sizeof(flush_type_instance), "flush-%s", type_instance); - if ((binfo->bi.rd_req != -1) && (binfo->bi.wr_req != -1)) - submit_derive2("disk_ops", (derive_t)binfo->bi.rd_req, - (derive_t)binfo->bi.wr_req, dom, type_instance); + if ((bstats->bi.rd_req != -1) && (bstats->bi.wr_req != -1)) + submit_derive2("disk_ops", (derive_t)bstats->bi.rd_req, + (derive_t)bstats->bi.wr_req, dom, type_instance); - if ((binfo->bi.rd_bytes != -1) && (binfo->bi.wr_bytes != -1)) - submit_derive2("disk_octets", (derive_t)binfo->bi.rd_bytes, - (derive_t)binfo->bi.wr_bytes, dom, type_instance); + if ((bstats->bi.rd_bytes != -1) && (bstats->bi.wr_bytes != -1)) + submit_derive2("disk_octets", (derive_t)bstats->bi.rd_bytes, + (derive_t)bstats->bi.wr_bytes, dom, type_instance); if (extra_stats & ex_stats_disk) { - if ((binfo->rd_total_times != -1) && (binfo->wr_total_times != -1)) - submit_derive2("disk_time", (derive_t)binfo->rd_total_times, - (derive_t)binfo->wr_total_times, dom, type_instance); + if ((bstats->rd_total_times != -1) && (bstats->wr_total_times != -1)) + submit_derive2("disk_time", (derive_t)bstats->rd_total_times, + (derive_t)bstats->wr_total_times, dom, type_instance); - if (binfo->fl_req != -1) + if (bstats->fl_req != -1) submit(dom, "total_requests", flush_type_instance, - &(value_t){.derive = (derive_t)binfo->fl_req}, 1); - if (binfo->fl_total_times != -1) { - derive_t value = binfo->fl_total_times / 1000; // ns -> ms + &(value_t){.derive = (derive_t)bstats->fl_req}, 1); + if (bstats->fl_total_times != -1) { + derive_t value = bstats->fl_total_times / 1000; // ns -> ms submit(dom, "total_time_in_ms", flush_type_instance, &(value_t){.derive = value}, 1); } } + /* disk_allocation, disk_capacity and disk_physical are stored only + * if corresponding extrastats are set in collectd configuration file */ + if ((extra_stats & ex_stats_disk_allocation) && binfo->allocation != -1) + submit(dom, "disk_allocation", type_instance, + &(value_t){.gauge = (gauge_t)binfo->allocation}, 1); + + if ((extra_stats & ex_stats_disk_capacity) && binfo->capacity != -1) + submit(dom, "disk_capacity", type_instance, + &(value_t){.gauge = (gauge_t)binfo->capacity}, 1); + + if ((extra_stats & ex_stats_disk_physical) && binfo->physical != -1) + submit(dom, "disk_physical", type_instance, + &(value_t){.gauge = (gauge_t)binfo->physical}, 1); + sfree(dev_copy); } -static unsigned int parse_ex_stats_flags(char **exstats, int numexstats) { +/** + * Function for parsing ExtraStats configuration options. + * Result of parsing is stored under 'out_parsed_flags' pointer. + * + * Returns 0 in case of success and 1 in case of parsing error + */ +static int parse_ex_stats_flags(unsigned int *out_parsed_flags, char **exstats, + int numexstats) { unsigned int ex_stats_flags = ex_stats_none; + + assert(out_parsed_flags != NULL); + for (int i = 0; i < numexstats; i++) { for (int j = 0; ex_stats_table[j].name != NULL; j++) { if (strcasecmp(exstats[i], ex_stats_table[j].name) == 0) { @@ -1062,10 +1073,13 @@ static unsigned int parse_ex_stats_flags(char **exstats, int numexstats) { if (ex_stats_table[j + 1].name == NULL) { ERROR(PLUGIN_NAME " plugin: Unmatched ExtraStats option: %s", exstats[i]); + return 1; } } } - return ex_stats_flags; + + *out_parsed_flags = ex_stats_flags; + return 0; } static void domain_state_submit_notif(virDomainPtr dom, int state, int reason) { @@ -1138,224 +1152,255 @@ static int lv_init_ignorelists() { return 0; } -static int lv_config(const char *key, const char *value) { - if (virInitialize() != 0) - return 1; - - if (lv_init_ignorelists() != 0) - return 1; - - if (strcasecmp(key, "Connection") == 0) { - char *tmp = strdup(value); - if (tmp == NULL) { - ERROR(PLUGIN_NAME " plugin: Connection strdup failed."); - return 1; - } - sfree(conn_string); - conn_string = tmp; - return 0; - } - - if (strcasecmp(key, "RefreshInterval") == 0) { - char *eptr = NULL; - interval = strtol(value, &eptr, 10); - if (eptr == NULL || *eptr != '\0') - return 1; - return 0; +/* Validates config option that may take multiple strings arguments. + * Returns 0 on success, -1 otherwise */ +static int check_config_multiple_string_entry(const oconfig_item_t *ci) { + if (ci == NULL) { + ERROR(PLUGIN_NAME " plugin: ci oconfig_item can't be NULL"); + return -1; } - if (strcasecmp(key, "Domain") == 0) { - if (ignorelist_add(il_domains, value)) - return 1; - return 0; - } - if (strcasecmp(key, "BlockDevice") == 0) { - if (ignorelist_add(il_block_devices, value)) - return 1; - return 0; + if (ci->values_num < 1) { + ERROR(PLUGIN_NAME + " plugin: the '%s' option requires at least one string argument", + ci->key); + return -1; } - if (strcasecmp(key, "BlockDeviceFormat") == 0) { - if (strcasecmp(value, "target") == 0) - blockdevice_format = target; - else if (strcasecmp(value, "source") == 0) - blockdevice_format = source; - else { - ERROR(PLUGIN_NAME " plugin: unknown BlockDeviceFormat: %s", value); + for (int i = 0; i < ci->values_num; ++i) { + if (ci->values[i].type != OCONFIG_TYPE_STRING) { + ERROR(PLUGIN_NAME + " plugin: one of the '%s' options is not a valid string", + ci->key); return -1; } - return 0; - } - if (strcasecmp(key, "BlockDeviceFormatBasename") == 0) { - blockdevice_format_basename = IS_TRUE(value) ? true : false; - return 0; - } - if (strcasecmp(key, "InterfaceDevice") == 0) { - if (ignorelist_add(il_interface_devices, value)) - return 1; - return 0; } - if (strcasecmp(key, "IgnoreSelected") == 0) { - if (IS_TRUE(value)) { - ignorelist_set_invert(il_domains, 0); - ignorelist_set_invert(il_block_devices, 0); - ignorelist_set_invert(il_interface_devices, 0); - } else { - ignorelist_set_invert(il_domains, 1); - ignorelist_set_invert(il_block_devices, 1); - ignorelist_set_invert(il_interface_devices, 1); - } - return 0; - } + return 0; +} - if (strcasecmp(key, "HostnameMetadataNS") == 0) { - char *tmp = strdup(value); - if (tmp == NULL) { - ERROR(PLUGIN_NAME " plugin: HostnameMetadataNS strdup failed."); - return 1; - } - sfree(hm_ns); - hm_ns = tmp; - return 0; +static int lv_config(oconfig_item_t *ci) { + if (lv_init_ignorelists() != 0) { + ERROR(PLUGIN_NAME " plugin: lv_init_ignorelist failed."); + return -1; } - if (strcasecmp(key, "HostnameMetadataXPath") == 0) { - char *tmp = strdup(value); - if (tmp == NULL) { - ERROR(PLUGIN_NAME " plugin: HostnameMetadataXPath strdup failed."); - return 1; - } - sfree(hm_xpath); - hm_xpath = tmp; - return 0; - } + for (int i = 0; i < ci->children_num; ++i) { + oconfig_item_t *c = ci->children + i; - if (strcasecmp(key, "HostnameFormat") == 0) { - char *value_copy = strdup(value); - if (value_copy == NULL) { - ERROR(PLUGIN_NAME " plugin: strdup failed."); - return -1; - } + if (strcasecmp(c->key, "Connection") == 0) { + if (cf_util_get_string(c, &conn_string) != 0 || conn_string == NULL) + return -1; - char *fields[HF_MAX_FIELDS]; - int n = strsplit(value_copy, fields, HF_MAX_FIELDS); - if (n < 1) { - sfree(value_copy); - ERROR(PLUGIN_NAME " plugin: HostnameFormat: no fields"); - return -1; - } + continue; + } else if (strcasecmp(c->key, "RefreshInterval") == 0) { + if (cf_util_get_int(c, &interval) != 0) + return -1; - for (int i = 0; i < n; ++i) { - if (strcasecmp(fields[i], "hostname") == 0) - hostname_format[i] = hf_hostname; - else if (strcasecmp(fields[i], "name") == 0) - hostname_format[i] = hf_name; - else if (strcasecmp(fields[i], "uuid") == 0) - hostname_format[i] = hf_uuid; - else if (strcasecmp(fields[i], "metadata") == 0) - hostname_format[i] = hf_metadata; + continue; + } else if (strcasecmp(c->key, "Domain") == 0) { + char *domain_name = NULL; + if (cf_util_get_string(c, &domain_name) != 0) + return -1; + + if (ignorelist_add(il_domains, domain_name)) { + ERROR(PLUGIN_NAME " plugin: Adding '%s' to domain-ignorelist failed", + domain_name); + sfree(domain_name); + return -1; + } + + sfree(domain_name); + continue; + } else if (strcasecmp(c->key, "BlockDevice") == 0) { + char *device_name = NULL; + if (cf_util_get_string(c, &device_name) != 0) + return -1; + + if (ignorelist_add(il_block_devices, device_name) != 0) { + ERROR(PLUGIN_NAME + " plugin: Adding '%s' to block-device-ignorelist failed", + device_name); + sfree(device_name); + return -1; + } + + sfree(device_name); + continue; + } else if (strcasecmp(c->key, "BlockDeviceFormat") == 0) { + char *device_format = NULL; + if (cf_util_get_string(c, &device_format) != 0) + return -1; + + if (strcasecmp(device_format, "target") == 0) + blockdevice_format = target; + else if (strcasecmp(device_format, "source") == 0) + blockdevice_format = source; else { - ERROR(PLUGIN_NAME " plugin: unknown HostnameFormat field: %s", - fields[i]); - sfree(value_copy); + ERROR(PLUGIN_NAME " plugin: unknown BlockDeviceFormat: %s", + device_format); + sfree(device_format); return -1; } - } - sfree(value_copy); - for (int i = n; i < HF_MAX_FIELDS; ++i) - hostname_format[i] = hf_none; + sfree(device_format); + continue; + } else if (strcasecmp(c->key, "BlockDeviceFormatBasename") == 0) { + if (cf_util_get_boolean(c, &blockdevice_format_basename) != 0) + return -1; + + continue; + } else if (strcasecmp(c->key, "InterfaceDevice") == 0) { + char *interface_name = NULL; + if (cf_util_get_string(c, &interface_name) != 0) + return -1; - return 0; - } + if (ignorelist_add(il_interface_devices, interface_name)) { + ERROR(PLUGIN_NAME " plugin: Adding '%s' to interface-ignorelist failed", + interface_name); + sfree(interface_name); + return -1; + } - if (strcasecmp(key, "PluginInstanceFormat") == 0) { - char *value_copy = strdup(value); - if (value_copy == NULL) { - ERROR(PLUGIN_NAME " plugin: strdup failed."); - return -1; - } + sfree(interface_name); + continue; + } else if (strcasecmp(c->key, "IgnoreSelected") == 0) { + bool ignore_selected = false; + if (cf_util_get_boolean(c, &ignore_selected) != 0) + return -1; - char *fields[PLGINST_MAX_FIELDS]; - int n = strsplit(value_copy, fields, PLGINST_MAX_FIELDS); - if (n < 1) { - sfree(value_copy); - ERROR(PLUGIN_NAME " plugin: PluginInstanceFormat: no fields"); - return -1; - } + if (ignore_selected) { + ignorelist_set_invert(il_domains, 0); + ignorelist_set_invert(il_block_devices, 0); + ignorelist_set_invert(il_interface_devices, 0); + } else { + ignorelist_set_invert(il_domains, 1); + ignorelist_set_invert(il_block_devices, 1); + ignorelist_set_invert(il_interface_devices, 1); + } - for (int i = 0; i < n; ++i) { - if (strcasecmp(fields[i], "none") == 0) { - plugin_instance_format[i] = plginst_none; - break; - } else if (strcasecmp(fields[i], "name") == 0) - plugin_instance_format[i] = plginst_name; - else if (strcasecmp(fields[i], "uuid") == 0) - plugin_instance_format[i] = plginst_uuid; - else if (strcasecmp(fields[i], "metadata") == 0) - plugin_instance_format[i] = plginst_metadata; - else { - ERROR(PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s", - fields[i]); - sfree(value_copy); + continue; + } else if (strcasecmp(c->key, "HostnameMetadataNS") == 0) { + if (cf_util_get_string(c, &hm_ns) != 0) + return -1; + + continue; + } else if (strcasecmp(c->key, "HostnameMetadataXPath") == 0) { + if (cf_util_get_string(c, &hm_xpath) != 0) + return -1; + + continue; + } else if (strcasecmp(c->key, "HostnameFormat") == 0) { + /* this option can take multiple strings arguments in one config line*/ + if (check_config_multiple_string_entry(c) != 0) { + ERROR(PLUGIN_NAME " plugin: Could not get 'HostnameFormat' parameter"); return -1; } - } - sfree(value_copy); - for (int i = n; i < PLGINST_MAX_FIELDS; ++i) - plugin_instance_format[i] = plginst_none; + const int params_num = c->values_num; + for (int i = 0; i < params_num; ++i) { + const char *param_name = c->values[i].value.string; + if (strcasecmp(param_name, "hostname") == 0) + hostname_format[i] = hf_hostname; + else if (strcasecmp(param_name, "name") == 0) + hostname_format[i] = hf_name; + else if (strcasecmp(param_name, "uuid") == 0) + hostname_format[i] = hf_uuid; + else if (strcasecmp(param_name, "metadata") == 0) + hostname_format[i] = hf_metadata; + else { + ERROR(PLUGIN_NAME " plugin: unknown HostnameFormat field: %s", + param_name); + return -1; + } + } - return 0; - } + for (int i = params_num; i < HF_MAX_FIELDS; ++i) + hostname_format[i] = hf_none; - if (strcasecmp(key, "InterfaceFormat") == 0) { - if (strcasecmp(value, "name") == 0) - interface_format = if_name; - else if (strcasecmp(value, "address") == 0) - interface_format = if_address; - else if (strcasecmp(value, "number") == 0) - interface_format = if_number; - else { - ERROR(PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", value); - return -1; - } - return 0; - } + continue; + } else if (strcasecmp(c->key, "PluginInstanceFormat") == 0) { + /* this option can handle list of string parameters in one line*/ + if (check_config_multiple_string_entry(c) != 0) { + ERROR(PLUGIN_NAME + " plugin: Could not get 'PluginInstanceFormat' parameter"); + return -1; + } - if (strcasecmp(key, "Instances") == 0) { - char *eptr = NULL; - double val = strtod(value, &eptr); + const int params_num = c->values_num; + for (int i = 0; i < params_num; ++i) { + const char *param_name = c->values[i].value.string; + if (strcasecmp(param_name, "none") == 0) { + plugin_instance_format[i] = plginst_none; + break; + } else if (strcasecmp(param_name, "name") == 0) + plugin_instance_format[i] = plginst_name; + else if (strcasecmp(param_name, "uuid") == 0) + plugin_instance_format[i] = plginst_uuid; + else if (strcasecmp(param_name, "metadata") == 0) + plugin_instance_format[i] = plginst_metadata; + else { + ERROR(PLUGIN_NAME " plugin: unknown PluginInstanceFormat field: %s", + param_name); + + return -1; + } + } - if (*eptr != '\0') { - ERROR(PLUGIN_NAME " plugin: Invalid value for Instances = '%s'", value); - return 1; - } - if (val <= 0) { - ERROR(PLUGIN_NAME " plugin: Instances <= 0 makes no sense."); - return 1; - } - if (val > NR_INSTANCES_MAX) { - ERROR(PLUGIN_NAME " plugin: Instances=%f > NR_INSTANCES_MAX=%i" - " use a lower setting or recompile the plugin.", - val, NR_INSTANCES_MAX); - return 1; - } + for (int i = params_num; i < PLGINST_MAX_FIELDS; ++i) + plugin_instance_format[i] = plginst_none; - nr_instances = (int)val; - DEBUG(PLUGIN_NAME " plugin: configured %i instances", nr_instances); - return 0; - } + continue; + } else if (strcasecmp(c->key, "InterfaceFormat") == 0) { + char *format = NULL; + if (cf_util_get_string(c, &format) != 0) + return -1; + + if (strcasecmp(format, "name") == 0) + interface_format = if_name; + else if (strcasecmp(format, "address") == 0) + interface_format = if_address; + else if (strcasecmp(format, "number") == 0) + interface_format = if_number; + else { + ERROR(PLUGIN_NAME " plugin: unknown InterfaceFormat: %s", format); + sfree(format); + return -1; + } + + sfree(format); + continue; + } else if (strcasecmp(c->key, "Instances") == 0) { + if (cf_util_get_int(c, &nr_instances) != 0) + return -1; + + if (nr_instances <= 0) { + ERROR(PLUGIN_NAME " plugin: Instances <= 0 makes no sense."); + return -1; + } + if (nr_instances > NR_INSTANCES_MAX) { + ERROR(PLUGIN_NAME " plugin: Instances=%i > NR_INSTANCES_MAX=%i" + " use a lower setting or recompile the plugin.", + nr_instances, NR_INSTANCES_MAX); + return -1; + } + + DEBUG(PLUGIN_NAME " plugin: configured %i instances", nr_instances); + continue; + } else if (strcasecmp(c->key, "ExtraStats") == 0) { + char *ex_str = NULL; + + if (cf_util_get_string(c, &ex_str) != 0) + return -1; - if (strcasecmp(key, "ExtraStats") == 0) { - char *localvalue = strdup(value); - if (localvalue != NULL) { char *exstats[EX_STATS_MAX_FIELDS]; - int numexstats = - strsplit(localvalue, exstats, STATIC_ARRAY_SIZE(exstats)); - extra_stats = parse_ex_stats_flags(exstats, numexstats); - sfree(localvalue); + int numexstats = strsplit(ex_str, exstats, STATIC_ARRAY_SIZE(exstats)); + int status = parse_ex_stats_flags(&extra_stats, exstats, numexstats); + sfree(ex_str); + if (status != 0) { + ERROR(PLUGIN_NAME " plugin: parsing 'ExtraStats' option failed"); + return status; + } #ifdef HAVE_JOB_STATS if ((extra_stats & ex_stats_job_stats_completed) && @@ -1363,29 +1408,35 @@ static int lv_config(const char *key, const char *value) { ERROR(PLUGIN_NAME " plugin: Invalid job stats configuration. Only one " "type of job statistics can be collected at the same " "time"); - return 1; + return -1; } #endif - } - } - if (strcasecmp(key, "PersistentNotification") == 0) { - persistent_notification = IS_TRUE(value); - return 0; - } + /* ExtraStats parsed successfully */ + continue; + } else if (strcasecmp(c->key, "PersistentNotification") == 0) { + if (cf_util_get_boolean(c, &persistent_notification) != 0) + return -1; - if (strcasecmp(key, "ReportBlockDevices") == 0) { - report_block_devices = IS_TRUE(value); - return 0; - } + continue; + } else if (strcasecmp(c->key, "ReportBlockDevices") == 0) { + if (cf_util_get_boolean(c, &report_block_devices) != 0) + return -1; - if (strcasecmp(key, "ReportNetworkInterfaces") == 0) { - report_network_interfaces = IS_TRUE(value); - return 0; + continue; + } else if (strcasecmp(c->key, "ReportNetworkInterfaces") == 0) { + if (cf_util_get_boolean(c, &report_network_interfaces) != 0) + return -1; + + continue; + } else { + /* Unrecognised option. */ + ERROR(PLUGIN_NAME " plugin: Unrecognized option: '%s'", c->key); + return -1; + } } - /* Unrecognised option. */ - return -1; + return 0; } static int lv_connect(void) { @@ -1422,8 +1473,8 @@ static void lv_disconnect(void) { WARNING(PLUGIN_NAME " plugin: closed connection to libvirt"); } -static int lv_domain_block_info(virDomainPtr dom, const char *path, - struct lv_block_info *binfo) { +static int lv_domain_block_stats(virDomainPtr dom, const char *path, + struct lv_block_stats *bstats) { #ifdef HAVE_BLOCK_STATS_FLAGS int nparams = 0; if (virDomainBlockStatsFlags(dom, path, NULL, &nparams, 0) < 0 || @@ -1443,14 +1494,14 @@ static int lv_domain_block_info(virDomainPtr dom, const char *path, if (virDomainBlockStatsFlags(dom, path, params, &nparams, 0) < 0) { VIRT_ERROR(conn, "getting the disk params values"); } else { - rc = get_block_info(binfo, params, nparams); + rc = get_block_stats(bstats, params, nparams); } virTypedParamsClear(params, nparams); sfree(params); return rc; #else - return virDomainBlockStats(dom, path, &(binfo->bi), sizeof(binfo->bi)); + return virDomainBlockStats(dom, path, &(bstats->bi), sizeof(bstats->bi)); #endif /* HAVE_BLOCK_STATS_FLAGS */ } @@ -1689,22 +1740,39 @@ static int get_disk_err(virDomainPtr domain) { } #endif /* HAVE_DISK_ERR */ -static int get_block_stats(struct block_device *block_dev) { - +static int get_block_device_stats(struct block_device *block_dev) { if (!block_dev) { ERROR(PLUGIN_NAME " plugin: get_block_stats NULL pointer"); return -1; } - struct lv_block_info binfo; + virDomainBlockInfo binfo; init_block_info(&binfo); - if (lv_domain_block_info(block_dev->dom, block_dev->path, &binfo) < 0) { - ERROR(PLUGIN_NAME " plugin: lv_domain_block_info failed"); + /* Fetching block info stats only if needed*/ + if (extra_stats & (ex_stats_disk_allocation | ex_stats_disk_capacity | + ex_stats_disk_physical)) { + /* Block info statistics can be only fetched from devices with 'source' + * defined */ + if (block_dev->has_source) { + if (virDomainGetBlockInfo(block_dev->dom, block_dev->path, &binfo, 0) < + 0) { + ERROR(PLUGIN_NAME " plugin: virDomainGetBlockInfo failed for path: %s", + block_dev->path); + return -1; + } + } + } + + struct lv_block_stats bstats; + init_block_stats(&bstats); + + if (lv_domain_block_stats(block_dev->dom, block_dev->path, &bstats) < 0) { + ERROR(PLUGIN_NAME " plugin: lv_domain_block_stats failed"); return -1; } - disk_submit(&binfo, block_dev->dom, block_dev->path); + disk_block_stats_submit(&bstats, block_dev->dom, block_dev->path, &binfo); return 0; } @@ -2055,11 +2123,15 @@ static int start_event_loop(virt_notif_thread_t *thread_data) { return -1; } + DEBUG(PLUGIN_NAME " plugin: starting event loop"); + virt_notif_thread_set_active(thread_data, 1); if (pthread_create(&thread_data->event_loop_tid, NULL, event_loop_worker, thread_data)) { ERROR(PLUGIN_NAME " plugin: failed event loop thread creation"); + virt_notif_thread_set_active(thread_data, 0); virConnectDomainEventDeregisterAny(conn, thread_data->domain_event_cb_id); + thread_data->domain_event_cb_id = -1; return -1; } @@ -2068,13 +2140,21 @@ static int start_event_loop(virt_notif_thread_t *thread_data) { /* stop event loop thread and deregister callback */ static void stop_event_loop(virt_notif_thread_t *thread_data) { - /* stopping loop and de-registering event handler*/ - virt_notif_thread_set_active(thread_data, 0); - if (conn != NULL && thread_data->domain_event_cb_id != -1) - virConnectDomainEventDeregisterAny(conn, thread_data->domain_event_cb_id); - if (pthread_join(notif_thread.event_loop_tid, NULL) != 0) - ERROR(PLUGIN_NAME " plugin: stopping notification thread failed"); + DEBUG(PLUGIN_NAME " plugin: stopping event loop"); + + /* Stopping loop */ + if (virt_notif_thread_is_active(thread_data)) { + virt_notif_thread_set_active(thread_data, 0); + if (pthread_join(notif_thread.event_loop_tid, NULL) != 0) + ERROR(PLUGIN_NAME " plugin: stopping notification thread failed"); + } + + /* ... and de-registering event handler */ + if (conn != NULL && thread_data->domain_event_cb_id != -1) { + virConnectDomainEventDeregisterAny(conn, thread_data->domain_event_cb_id); + thread_data->domain_event_cb_id = -1; + } } static int persistent_domains_state_notification(void) { @@ -2234,7 +2314,7 @@ static int lv_read(user_data_t *ud) { /* Get block device stats for each domain. */ for (int i = 0; i < state->nr_block_devices; ++i) { - int status = get_block_stats(&state->block_devices[i]); + int status = get_block_device_stats(&state->block_devices[i]); if (status != 0) ERROR(PLUGIN_NAME " plugin: failed to get stats for block device (%s) in domain %s", @@ -2305,8 +2385,6 @@ static int lv_init(void) { if (lv_connect() != 0) return -1; - DEBUG(PLUGIN_NAME " plugin: starting event loop"); - if (!persistent_notification) { virt_notif_thread_init(¬if_thread); if (start_event_loop(¬if_thread) != 0) @@ -2419,35 +2497,76 @@ static int lv_instance_include_domain(struct lv_read_instance *inst, static void lv_add_block_devices(struct lv_read_state *state, virDomainPtr dom, const char *domname, xmlXPathContextPtr xpath_ctx) { - const char *bd_xmlpath = "/domain/devices/disk/target[@dev]"; - if (blockdevice_format == source) - bd_xmlpath = "/domain/devices/disk/source[@dev]"; - xmlXPathObjectPtr xpath_obj = - xmlXPathEval((const xmlChar *)bd_xmlpath, xpath_ctx); + xmlXPathEval((const xmlChar *)"/domain/devices/disk", xpath_ctx); - if (xpath_obj == NULL) + if (xpath_obj == NULL) { + DEBUG(PLUGIN_NAME " plugin: no disk xpath-object found for domain %s", + domname); return; + } if (xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) { - xmlXPathFreeObject(xpath_obj); - return; + DEBUG(PLUGIN_NAME " plugin: no disk node found for domain %s", domname); + goto cleanup; } - for (int j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) { - xmlNodePtr node = xpath_obj->nodesetval->nodeTab[j]; - if (!node) - continue; + xmlNodeSetPtr xml_block_devices = xpath_obj->nodesetval; + for (int i = 0; i < xml_block_devices->nodeNr; ++i) { + xmlNodePtr xml_device = xpath_obj->nodesetval->nodeTab[i]; + char *path_str = NULL; + char *source_str = NULL; - char *path = (char *)xmlGetProp(node, (xmlChar *)"dev"); - if (!path) + if (!xml_device) continue; - if (ignore_device_match(il_block_devices, domname, path) == 0) - add_block_device(state, dom, path); + /* Fetching path and source for block device */ + for (xmlNodePtr child = xml_device->children; child; child = child->next) { + if (child->type != XML_ELEMENT_NODE) + continue; + + /* we are interested only in either "target" or "source" elements */ + if (xmlStrEqual(child->name, (const xmlChar *)"target")) + path_str = (char *)xmlGetProp(child, (const xmlChar *)"dev"); + else if (xmlStrEqual(child->name, (const xmlChar *)"source")) { + /* name of the source is located in "dev" or "file" element (it depends + * on type of source). Trying "dev" at first*/ + source_str = (char *)xmlGetProp(child, (const xmlChar *)"dev"); + if (!source_str) + source_str = (char *)xmlGetProp(child, (const xmlChar *)"file"); + } + /* ignoring any other element*/ + } + + /* source_str will be interpreted as a device path if blockdevice_format + * param is set to 'source'. */ + const char *device_path = + (blockdevice_format == source) ? source_str : path_str; + + if (!device_path) { + /* no path found and we can't add block_device without it */ + WARNING(PLUGIN_NAME " plugin: could not generate device path for disk in " + "domain %s - disk device will be ignored in reports", + domname); + goto cont; + } + + if (ignore_device_match(il_block_devices, domname, device_path) == 0) { + /* we only have to store information whether 'source' exists or not */ + bool has_source = (source_str != NULL) ? true : false; + + add_block_device(state, dom, device_path, has_source); + } - xmlFree(path); + cont: + if (path_str) + xmlFree(path_str); + + if (source_str) + xmlFree(source_str); } + +cleanup: xmlXPathFreeObject(xpath_obj); } @@ -2470,6 +2589,7 @@ static void lv_add_network_interfaces(struct lv_read_state *state, for (int j = 0; j < xml_interfaces->nodeNr; ++j) { char *path = NULL; char *address = NULL; + const int itf_number = j + 1; xmlNodePtr xml_interface = xml_interfaces->nodeTab[j]; if (!xml_interface) @@ -2491,11 +2611,31 @@ static void lv_add_network_interfaces(struct lv_read_state *state, } } - if ((ignore_device_match(il_interface_devices, domname, path) == 0 && - ignore_device_match(il_interface_devices, domname, address) == 0)) { - add_interface_device(state, dom, path, address, j + 1); + bool device_ignored = false; + switch (interface_format) { + case if_name: + if (ignore_device_match(il_interface_devices, domname, path) != 0) + device_ignored = true; + break; + case if_address: + if (ignore_device_match(il_interface_devices, domname, address) != 0) + device_ignored = true; + break; + case if_number: { + char number_string[4]; + snprintf(number_string, sizeof(number_string), "%d", itf_number); + if (ignore_device_match(il_interface_devices, domname, number_string) != + 0) + device_ignored = true; + } break; + default: + ERROR(PLUGIN_NAME " plugin: Unknown interface_format option: %d", + interface_format); } + if (!device_ignored) + add_interface_device(state, dom, path, address, itf_number); + if (path) xmlFree(path); if (address) @@ -2504,6 +2644,24 @@ static void lv_add_network_interfaces(struct lv_read_state *state, xmlXPathFreeObject(xpath_obj); } +static bool is_domain_ignored(virDomainPtr dom) { + const char *domname = virDomainGetName(dom); + + if (domname == NULL) { + VIRT_ERROR(conn, "virDomainGetName failed, ignoring domain"); + return true; + } + + if (ignorelist_match(il_domains, domname) != 0) { + DEBUG(PLUGIN_NAME + " plugin: ignoring domain '%s' because of ignorelist option", + domname); + return true; + } + + return false; +} + static int refresh_lists(struct lv_read_instance *inst) { struct lv_read_state *state = &inst->read_state; int n; @@ -2553,8 +2711,9 @@ static int refresh_lists(struct lv_read_instance *inst) { #ifdef HAVE_LIST_ALL_DOMAINS for (int i = 0; i < m; ++i) - if (add_domain(state, domains_inactive[i], 0) < 0) { - ERROR(PLUGIN_NAME " plugin: malloc failed."); + if (is_domain_ignored(domains_inactive[i]) || + add_domain(state, domains_inactive[i], 0) < 0) { + /* domain ignored or failed during adding to domains list*/ virDomainFree(domains_inactive[i]); domains_inactive[i] = NULL; continue; @@ -2575,8 +2734,10 @@ static int refresh_lists(struct lv_read_instance *inst) { } #endif - if (add_domain(state, dom, 1) < 0) { + if (is_domain_ignored(dom) || add_domain(state, dom, 1) < 0) { /* + * domain ignored or failed during adding to domains list + * * When domain is already tracked, then there is * no problem with memory handling (will be freed * with the rest of domains cached data) @@ -2584,7 +2745,6 @@ static int refresh_lists(struct lv_read_instance *inst) { * before adding domain to track) we have to take * care it ourselves and call virDomainFree */ - ERROR(PLUGIN_NAME " plugin: malloc failed."); virDomainFree(dom); continue; } @@ -2608,9 +2768,6 @@ static int refresh_lists(struct lv_read_instance *inst) { continue; } - if (ignorelist_match(il_domains, domname) != 0) - continue; - /* Get a list of devices for this domain. */ xmlDocPtr xml_doc = NULL; xmlXPathContextPtr xpath_ctx = NULL; @@ -2687,12 +2844,13 @@ static void free_domains(struct lv_read_state *state) { static int add_domain(struct lv_read_state *state, virDomainPtr dom, bool active) { - int new_size = sizeof(state->domains[0]) * (state->nr_domains + 1); domain_t *new_ptr = realloc(state->domains, new_size); - if (new_ptr == NULL) + if (new_ptr == NULL) { + ERROR(PLUGIN_NAME " plugin: realloc failed in add_domain()"); return -1; + } state->domains = new_ptr; state->domains[state->nr_domains].ptr = dom; @@ -2714,7 +2872,7 @@ static void free_block_devices(struct lv_read_state *state) { } static int add_block_device(struct lv_read_state *state, virDomainPtr dom, - const char *path) { + const char *path, bool has_source) { char *path_copy = strdup(path); if (!path_copy) @@ -2731,6 +2889,7 @@ static int add_block_device(struct lv_read_state *state, virDomainPtr dom, state->block_devices = new_ptr; state->block_devices[state->nr_block_devices].dom = dom; state->block_devices[state->nr_block_devices].path = path_copy; + state->block_devices[state->nr_block_devices].has_source = has_source; return state->nr_block_devices++; } @@ -2815,8 +2974,6 @@ static int lv_shutdown(void) { lv_fini_instance(i); } - DEBUG(PLUGIN_NAME " plugin: stopping event loop"); - if (!persistent_notification) stop_event_loop(¬if_thread); @@ -2833,7 +2990,7 @@ static int lv_shutdown(void) { } void module_register(void) { - plugin_register_config(PLUGIN_NAME, lv_config, config_keys, NR_CONFIG_KEYS); + plugin_register_complex_config("virt", lv_config); plugin_register_init(PLUGIN_NAME, lv_init); plugin_register_shutdown(PLUGIN_NAME, lv_shutdown); } diff --git a/src/write_graphite.c b/src/write_graphite.c index 000b62ed..cb6793dd 100644 --- a/src/write_graphite.c +++ b/src/write_graphite.c @@ -29,7 +29,7 @@ * Based on the write_http plugin. **/ -/* write_graphite plugin configuation example +/* write_graphite plugin configuration example * * * @@ -39,6 +39,7 @@ * LogSendErrors true * Prefix "collectd" * UseTags true + * ReverseHost false * * */ @@ -521,6 +522,8 @@ static int wg_config_node(oconfig_item_t *ci) { cf_util_get_flag(child, &cb->format_flags, GRAPHITE_DROP_DUPE_FIELDS); else if (strcasecmp("UseTags", child->key) == 0) cf_util_get_flag(child, &cb->format_flags, GRAPHITE_USE_TAGS); + else if (strcasecmp("ReverseHost", child->key) == 0) + cf_util_get_flag(child, &cb->format_flags, GRAPHITE_REVERSE_HOST); else if (strcasecmp("EscapeCharacter", child->key) == 0) config_set_char(&cb->escape_char, child); else {