From bcb9e8252f192560195b4faa5c79a84ea31df4f6 Mon Sep 17 00:00:00 2001 From: "Aleksinski, MichalX" Date: Wed, 20 Feb 2019 12:32:50 +0000 Subject: [PATCH] utils_proc_pids: moved to utils directory Change-Id: Ia7c9bf5dca2b871b05c9ed3876059e6d34bd943e --- Makefile.am | 8 +-- src/intel_rdt.c | 12 ++-- .../proc_pids/proc_pids.c} | 58 ++++++++++++---- .../proc_pids/proc_pids.h} | 79 ++++++---------------- .../proc_pids/proc_pids_test.c} | 30 ++++---- 5 files changed, 91 insertions(+), 96 deletions(-) rename src/{utils_proc_pids.c => utils/proc_pids/proc_pids.c} (84%) rename src/{utils_proc_pids.h => utils/proc_pids/proc_pids.h} (79%) rename src/{utils_proc_pids_test.c => utils/proc_pids/proc_pids_test.c} (94%) diff --git a/Makefile.am b/Makefile.am index b4195c13..7a64fbfd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -378,7 +378,7 @@ test_utils_config_cores_SOURCES = \ test_utils_config_cores_LDADD = libplugin_mock.la test_utils_proc_pids_SOURCES = \ - src/utils_proc_pids_test.c \ + src/utils/proc_pids/proc_pids_test.c \ src/testing.h test_utils_proc_pids_LDADD = libplugin_mock.la @@ -1071,8 +1071,8 @@ if BUILD_PLUGIN_INTEL_RDT pkglib_LTLIBRARIES += intel_rdt.la intel_rdt_la_SOURCES = \ src/intel_rdt.c \ - src/utils_proc_pids.c \ - src/utils_proc_pids.h \ + 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) @@ -1082,7 +1082,7 @@ 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.c \ + src/utils/proc_pids/proc_pids.c \ src/daemon/configfile.c \ src/daemon/types_list.c test_plugin_intel_rdt_CPPFLAGS = $(AM_CPPFLAGS) diff --git a/src/intel_rdt.c b/src/intel_rdt.c index 73648208..003f71ab 100644 --- a/src/intel_rdt.c +++ b/src/intel_rdt.c @@ -31,7 +31,7 @@ #include "collectd.h" #include "utils/common/common.h" #include "utils/config_cores/config_cores.h" -#include "utils_proc_pids.h" +#include "utils/proc_pids/proc_pids.h" #include #define RDT_PLUGIN "intel_rdt" @@ -526,7 +526,7 @@ static int rdt_config_ngroups(rdt_ctx_t *rdt, const oconfig_item_t *item) { name_idx++) { DEBUG(RDT_PLUGIN ": checking process name [%zu]: %s", name_idx, rdt->ngroups[group_idx].names[name_idx]); - if (!is_proc_name_valid(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]); @@ -798,7 +798,7 @@ static int read_pids_data() { #endif /* COLLECT_DEBUG */ groups_refresh: - ret = update_proc_pids(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids); + 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; @@ -839,8 +839,8 @@ static void rdt_init_pids_monitoring() { * PIDs statistics detection. */ rdt_name_group_t *ng = &g_rdt->ngroups[group_idx]; - int init_result = initialize_proc_pids((const char **)ng->names, - ng->num_names, &ng->proc_pids); + 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", @@ -865,7 +865,7 @@ static void rdt_init_pids_monitoring() { } int update_result = - update_proc_pids(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids); + 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"); diff --git a/src/utils_proc_pids.c b/src/utils/proc_pids/proc_pids.c similarity index 84% rename from src/utils_proc_pids.c rename to src/utils/proc_pids/proc_pids.c index 0fbea530..336a9964 100644 --- a/src/utils_proc_pids.c +++ b/src/utils/proc_pids/proc_pids.c @@ -1,5 +1,5 @@ /** - * collectd - src/utils_config_pids.c + * collectd - src/utils/proc_pids/proc_pids.c * * Copyright(c) 2018-2019 Intel Corporation. All rights reserved. * @@ -29,7 +29,7 @@ #include "collectd.h" #include "utils/common/common.h" -#include "utils_proc_pids.h" +#include "utils/proc_pids/proc_pids.h" #define UTIL_NAME "utils_proc_pids" @@ -40,7 +40,7 @@ void pids_list_free(pids_list_t *list) { sfree(list); } -int is_proc_name_valid(const char *name) { +int proc_pids_is_name_valid(const char *name) { if (name != NULL) { unsigned len = strlen(name); @@ -123,8 +123,27 @@ int pids_list_contains_pid(pids_list_t *list, const pid_t pid) { return 0; } -int read_proc_name(const char *procfs_path, const struct dirent *pid_entry, - char *name, const size_t out_size) { +/* + * 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); @@ -155,7 +174,22 @@ int read_proc_name(const char *procfs_path, const struct dirent *pid_entry, return read_length; } -int get_pid_number(struct dirent *entry, pid_t *pid) { +/* + * 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) @@ -173,9 +207,9 @@ int get_pid_number(struct dirent *entry, pid_t *pid) { return 0; } -int initialize_proc_pids(const char **procs_names_array, - const size_t procs_names_array_size, - proc_pids_t **proc_pids[]) { +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); @@ -190,7 +224,7 @@ int initialize_proc_pids(const char **procs_names_array, 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 initialize_proc_pids_error; + 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)); @@ -201,7 +235,7 @@ int initialize_proc_pids(const char **procs_names_array, *proc_pids = proc_pids_array; return 0; -initialize_proc_pids_error: +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]); @@ -219,7 +253,7 @@ static void swap_proc_pids(proc_pids_t **proc_pids, size_t proc_pids_num) { } } -int update_proc_pids(const char *procfs_path, proc_pids_t **proc_pids, +int proc_pids_update(const char *procfs_path, proc_pids_t **proc_pids, size_t proc_pids_num) { assert(procfs_path); assert(proc_pids); diff --git a/src/utils_proc_pids.h b/src/utils/proc_pids/proc_pids.h similarity index 79% rename from src/utils_proc_pids.h rename to src/utils/proc_pids/proc_pids.h index a2a668dd..8b19497c 100644 --- a/src/utils_proc_pids.h +++ b/src/utils/proc_pids/proc_pids.h @@ -1,5 +1,5 @@ /** - * collectd - src/utils_config_pids.h + * collectd - src/utils/proc_pids/proc_pids.h * * Copyright(c) 2018-2019 Intel Corporation. All rights reserved. * @@ -69,22 +69,6 @@ void pids_list_free(pids_list_t *list); /* * NAME - * is_proc_name_valid - * - * DESCRIPTION - * Checks if given string as 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 is_proc_name_valid(const char *name); - -/* - * NAME * pids_list_add_pid * * DESCRIPTION @@ -132,7 +116,6 @@ int pids_list_clear(pids_list_t *list); * On success, returns 0. * -1 on memory allocation error. */ -# int pids_list_add_list(pids_list_t *dst, pids_list_t *src); /* @@ -154,45 +137,39 @@ int pids_list_contains_pid(pids_list_t *list, const pid_t pid); /* * NAME - * read_proc_name + * pids_list_diff * * DESCRIPTION - * Reads process name from given pid directory. - * Strips new-line character (\n). + * Searches for differences in two given lists * * 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) - * + * `proc' List of pids + * `added' New pids which appeared + * `removed' Result array storing pids which disappeared * RETURN VALUE - * On success, the number of read bytes (includes stripped \n). - * -1 on file open error + * 0 on success. Negative number on error. */ -int read_proc_name(const char *procfs_path, const struct dirent *pid_entry, - char *name, const size_t out_size); +int pids_list_diff(proc_pids_t *proc, pids_list_t *added, pids_list_t *removed); /* * NAME - * get_pid_number + * proc_pids_is_name_valid * * DESCRIPTION - * Gets pid number for given /proc/pid directory entry or - * returns error if input directory does not hold PID information. + * Checks if given string is valid process name. * * PARAMETERS - * `entry' Dirent for PID directory - * `pid' PID number to be filled + * `name' null-terminated char array * * RETURN VALUE - * 0 on success. -1 on error. + * If given name is a valid process name, returns 1, + * Otherwise returns 0. */ -int get_pid_number(struct dirent *entry, pid_t *pid); +int proc_pids_is_name_valid(const char *name); /* * NAME - * initialize_proc_pids + * proc_pids_init * * DESCRIPTION * Helper function to properly initialize array of proc_pids. @@ -209,13 +186,13 @@ int get_pid_number(struct dirent *entry, pid_t *pid); * 0 on success. Negative number on error: * -1: allocation error */ -int initialize_proc_pids(const char **procs_names_array, - const size_t procs_names_array_size, - proc_pids_t **proc_pids[]); +int proc_pids_init(const char **procs_names_array, + const size_t procs_names_array_size, + proc_pids_t **proc_pids[]); /* * NAME - * update_proc_pids + * proc_pids_update * * DESCRIPTION * Updates PIDs matching processes's names. @@ -229,27 +206,11 @@ int initialize_proc_pids(const char **procs_names_array, * RETURN VALUE * 0 on success. -1 on error. */ -int update_proc_pids(const char *procfs_path, proc_pids_t *proc_pids[], +int proc_pids_update(const char *procfs_path, proc_pids_t *proc_pids[], size_t proc_pids_num); /* * 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_free * * DESCRIPTION diff --git a/src/utils_proc_pids_test.c b/src/utils/proc_pids/proc_pids_test.c similarity index 94% rename from src/utils_proc_pids_test.c rename to src/utils/proc_pids/proc_pids_test.c index 21c8b349..06b8d39b 100644 --- a/src/utils_proc_pids_test.c +++ b/src/utils/proc_pids/proc_pids_test.c @@ -1,5 +1,5 @@ #include "testing.h" -#include "utils_proc_pids.c" /* sic */ +#include "utils/proc_pids/proc_pids.c" /* sic */ #include /*************************************************************************** @@ -88,15 +88,15 @@ int stub_procfs_teardown() { /*************************************************************************** * tests */ -DEF_TEST(initialize_proc_pids__on_nullptr) { +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 = initialize_proc_pids(procs_names_array, procs_names_array_size, - &proc_pids_array); + 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); @@ -259,7 +259,7 @@ DEF_TEST(read_proc_name__invalid_name) { return 0; } -DEF_TEST(update_proc_pids__one_proc_many_pid) { +DEF_TEST(proc_pids_update__one_proc_many_pid) { /* setup */ const char *proc_names[] = {"proc1"}; stub_proc_pid_t pp_stubs[] = {{"proc1", 1007}, @@ -271,12 +271,12 @@ DEF_TEST(update_proc_pids__one_proc_many_pid) { int result; stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs)); - result = initialize_proc_pids(proc_names, STATIC_ARRAY_SIZE(proc_names), - &proc_pids); + result = + proc_pids_init(proc_names, STATIC_ARRAY_SIZE(proc_names), &proc_pids); EXPECT_EQ_INT(0, result); /* check */ - result = update_proc_pids(proc_fs, proc_pids, STATIC_ARRAY_SIZE(proc_names)); + result = proc_pids_update(proc_fs, proc_pids, STATIC_ARRAY_SIZE(proc_names)); EXPECT_EQ_INT(0, result); /* proc name check */ @@ -299,7 +299,7 @@ DEF_TEST(update_proc_pids__one_proc_many_pid) { return 0; } -DEF_TEST(update_proc_pids__many_proc_many_pid) { +DEF_TEST(proc_pids_update__many_proc_many_pid) { /* setup */ const char *proc_names[] = {"proc1", "proc2", "proc3"}; stub_proc_pid_t pp_stubs[] = { @@ -311,12 +311,12 @@ DEF_TEST(update_proc_pids__many_proc_many_pid) { int result; stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs)); - result = initialize_proc_pids(proc_names, STATIC_ARRAY_SIZE(proc_names), - &proc_pids); + result = + proc_pids_init(proc_names, STATIC_ARRAY_SIZE(proc_names), &proc_pids); EXPECT_EQ_INT(0, result); /* check */ - result = update_proc_pids(proc_fs, proc_pids, STATIC_ARRAY_SIZE(proc_names)); + 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) { @@ -485,7 +485,7 @@ DEF_TEST(pids_list_diff__one_removed) { int main(void) { stub_procfs_teardown(); - RUN_TEST(initialize_proc_pids__on_nullptr); + 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); @@ -494,8 +494,8 @@ int main(void) { RUN_TEST(get_pid_number__invalid_dir_name); RUN_TEST(read_proc_name__valid_name); RUN_TEST(read_proc_name__invalid_name); - RUN_TEST(update_proc_pids__one_proc_many_pid); - RUN_TEST(update_proc_pids__many_proc_many_pid); + 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); -- 2.11.0