diff options
author | Harout Hedeshian <harouth@codeaurora.org> | 2015-04-13 19:02:25 -0600 |
---|---|---|
committer | Subash Abhinov Kasiviswanathan <subashab@codeaurora.org> | 2015-05-15 12:58:48 -0600 |
commit | 6202ba7e9a6f84e524f2437135d4eddd1c354840 (patch) | |
tree | 90f0d13d4eff2094a7cda03daed1e2542c27b6f4 | |
parent | c30edead617466240377d93037ab5cabc59bf22d (diff) | |
download | android_vendor_qcom_opensource_dataservices-6202ba7e9a6f84e524f2437135d4eddd1c354840.tar.gz android_vendor_qcom_opensource_dataservices-6202ba7e9a6f84e524f2437135d4eddd1c354840.tar.bz2 android_vendor_qcom_opensource_dataservices-6202ba7e9a6f84e524f2437135d4eddd1c354840.zip |
datatop: Snapshot of version 1.0.4
datatop is a utility that captures various linux system parameters
required for analyzing throughput and data path issues.
Fix some compilation and code style issues.
Acked-by: Brennan McConnell <brennanm@qti.qualcomm.com>
Change-Id: I2138f657a565fe3d7bcf4bce2ef7142f5557a868
28 files changed, 4482 insertions, 0 deletions
diff --git a/datatop/Android.mk b/datatop/Android.mk new file mode 100644 index 0000000..8338432 --- /dev/null +++ b/datatop/Android.mk @@ -0,0 +1,2 @@ +include $(call all-subdir-makefiles) + diff --git a/datatop/Makefile.am b/datatop/Makefile.am new file mode 100644 index 0000000..15ea2b3 --- /dev/null +++ b/datatop/Makefile.am @@ -0,0 +1,3 @@ +AUTOMAKE_OPTIONS = foreign + +SUBDIRS = src diff --git a/datatop/autogen.sh b/datatop/autogen.sh new file mode 100644 index 0000000..7141954 --- /dev/null +++ b/datatop/autogen.sh @@ -0,0 +1,51 @@ +#!/bin/sh + +# Copyright (c) 2015, The Linux Foundation. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of The Linux Foundation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +ACLOCAL=`which aclocal` +AUTOCONF=`which autoconf` +AUTOMAKE=`which automake` + +if [ ! -x "$ACLOCAL" ]; then + echo "Missing 'aclocal'; not in path. Make sure it is installed!" + exit -1 +fi + +if [ ! -x "$AUTOCONF" ]; then + echo "Missing 'autoconf'; not in path. Make sure it is installed!" + exit -1 +fi + +if [ ! -x "$AUTOMAKE" ]; then + echo "Missing 'automake'; not in path. Make sure it is installed!" + exit -1 +fi + +$ACLOCAL +$AUTOCONF +$AUTOMAKE -a diff --git a/datatop/configure.ac b/datatop/configure.ac new file mode 100644 index 0000000..b9f9abf --- /dev/null +++ b/datatop/configure.ac @@ -0,0 +1,56 @@ +# -*- Autoconf -*- + +# Copyright (c) 2015, The Linux Foundation. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of The Linux Foundation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# Process this file with autoconf to produce a configure script. + +AC_PREREQ([2.68]) +AC_INIT([Data Top], [1.0.4], [harouth@codeaurora.org]) +AC_CONFIG_SRCDIR([src/datatop.c]) +#AC_CONFIG_HEADERS([src/config.h]) +AC_CONFIG_FILES([Makefile + src/Makefile]) + +# Checks for programs. +AC_PROG_CC + +# Checks for libraries. + +# Checks for header files. +AC_CHECK_HEADERS([stdlib.h string.h]) + +# Checks for typedefs, structures, and compiler characteristics. + +# Checks for library functions. +AC_FUNC_MALLOC + + +#### Generate Makefile Data +AM_INIT_AUTOMAKE([datatop], [1.0.4]) + +AC_OUTPUT diff --git a/datatop/src/Android.mk b/datatop/src/Android.mk new file mode 100644 index 0000000..835e969 --- /dev/null +++ b/datatop/src/Android.mk @@ -0,0 +1,29 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := datatop.c +LOCAL_SRC_FILES += datatop_cpu_stats_poll.c +LOCAL_SRC_FILES += datatop_dev_poll.c +LOCAL_SRC_FILES += datatop_dual_line_poll.c +LOCAL_SRC_FILES += datatop_fileops.c +LOCAL_SRC_FILES += datatop_gen_poll.c +LOCAL_SRC_FILES += datatop_helpers.c +LOCAL_SRC_FILES += datatop_linked_list.c +LOCAL_SRC_FILES += datatop_meminfo_file_poll.c +LOCAL_SRC_FILES += datatop_opt.c +LOCAL_SRC_FILES += datatop_single_line_poll.c +LOCAL_SRC_FILES += datatop_stat_poll.c +LOCAL_SRC_FILES += datatop_str.c +LOCAL_SRC_FILES += datatop_sys_snap.c +LOCAL_SRC_FILES += datatop_value_only_poll.c + +LOCAL_CFLAGS := -Wall -Wextra -Werror -pedantic +LOCAL_CFLAGS += -DVERSION="\"1.0.4"\" + +LOCAL_C_INCLUDES := $(LOCAL_PATH) + +LOCAL_MODULE := datatop +LOCAL_MODULE_TAGS := optional + +include $(BUILD_EXECUTABLE) diff --git a/datatop/src/Makefile.am b/datatop/src/Makefile.am new file mode 100644 index 0000000..554b40c --- /dev/null +++ b/datatop/src/Makefile.am @@ -0,0 +1,22 @@ +## Makefile.am for main application + +CFLAGS := -std=c99 # Target c99 for portability +CFLAGS += -Wall -Wextra -Werror -pedantic # Strict code quality enforcement +CFLAGS += -g # Enable debugging + +bin_PROGRAMS = datatop +datatop_SOURCES := datatop.c +datatop_SOURCES += datatop_fileops.c +datatop_SOURCES += datatop_dual_line_poll.c +datatop_SOURCES += datatop_single_line_poll.c +datatop_SOURCES += datatop_meminfo_file_poll.c +datatop_SOURCES += datatop_dev_poll.c +datatop_SOURCES += datatop_stat_poll.c +datatop_SOURCES += datatop_value_only_poll.c +datatop_SOURCES += datatop_str.c +datatop_SOURCES += datatop_cpu_stats_poll.c +datatop_SOURCES += datatop_helpers.c +datatop_SOURCES += datatop_linked_list.c +datatop_SOURCES += datatop_opt.c +datatop_SOURCES += datatop_gen_poll.c +datatop_SOURCES += datatop_sys_snap.c diff --git a/datatop/src/datatop.c b/datatop/src/datatop.c new file mode 100644 index 0000000..984de05 --- /dev/null +++ b/datatop/src/datatop.c @@ -0,0 +1,275 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** +* @file datatop.c +* @brief Executes commands for application. +* +* Contains the main() function where program executes. Calls appropriate +* methods based on user's CLI commands. Executes parsing function to +* determine necessary output and handles errors which may arise during the +* parse. Initiliazes files for data collection. Will call functions designed +* to poll and print the data in understandable format. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <sys/time.h> +#include <sys/resource.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include "datatop_interface.h" +#include "datatop_linked_list.h" +#include "datatop_opt.h" +#include "datatop_fileops.h" +#include "datatop_polling.h" +#include "datatop_gen_poll.h" + +struct dtop_linked_list *first_dpg_list; +struct cli_opts usr_cl_opts; + +/** + * @brief Prints the datapoint names and values to the terminal. + * + * @param dpg_list A pointer to the first node of a linked list which + * contains all data_point_gatherer structs to print. + */ +void dtop_print_terminal(struct dtop_linked_list *dpg_list) +{ + struct dtop_linked_list *curr_ptr = dpg_list; + struct dtop_data_point_gatherer *dpset; + + while (curr_ptr) { + dpset = (struct dtop_data_point_gatherer *) curr_ptr->data; + dtop_print_dpg(dpset); + curr_ptr = curr_ptr->next_ptr; + } +} + +/** + * @brief Polls the data periodically and prints to file specified by the user. + * + * Polls the data as often as specified by the user in their CLI arguments + * and outputs the data to a file also specified in CLI arguments. Then prints + * a snapshot of delta(dp_value) to the terminal. + * + * @param dpg_list A pointer to the first node of a linked list which contains + * all data_point_gatherer structs to poll and print. + * @param fw A pointer to the file which will be printed to. + * @return FILE_ERROR - Writing to file was unsuccessful. + * @return FILE_SUCCESS - Writing to file was successful. + */ +int dtop_poll_periodically(struct dtop_linked_list *dpg_list, FILE *fw) +{ + struct timeval tv, timeout; + fd_set rfds; + time_t curtime, endtime; + int inp, quit; + struct dtop_linked_list *curr_ptr = dpg_list; + struct dtop_data_point_gatherer *dpset; + + gettimeofday(&tv, NULL); + curtime = tv.tv_sec; + endtime = tv.tv_sec + usr_cl_opts.poll_time; + + /* print all of our datapoint names as column headers in csv format */ + if (fprintf(fw, "\"Time\",") < 0) + return FILE_ERROR; + + while (curr_ptr) { + dpset = (struct dtop_data_point_gatherer *) curr_ptr->data; + if (dtop_print_dpg_names_csv(dpset, fw) == FILE_ERROR) + return FILE_ERROR; + curr_ptr = curr_ptr->next_ptr; + } + if (fprintf(fw, "\n") < 0) + return FILE_ERROR; + + dtop_print_interactive_opts(); + /* periodically poll the datapoints and print in csv format */ + while (curtime < endtime + || usr_cl_opts.poll_time == POLL_NOT_SPECIFIED) { + FD_ZERO(&rfds); + FD_SET(0, &rfds); + timeout.tv_sec = usr_cl_opts.poll_per; + timeout.tv_usec = 0; + inp = select(1, &rfds, NULL, NULL, &timeout); + if (inp) { + char s[4]; + scanf("%s", s); + if (strcmp(s, "quit") == 0 + || strcmp(s, "q") == 0) { + quit = QUIT; + break; + } + if (strcmp(s, "i") == 0) { + dtop_print_snapshot_diff(first_dpg_list); + dtop_reset_dp_initial_values(first_dpg_list); + } + if (strcmp(s, "l") == 0) + dtop_print_snapshot_diff(first_dpg_list); + } + gettimeofday(&tv, NULL); + curtime = tv.tv_sec; + dtop_poll(dpg_list); + printf("Polled at %ld.%06ld\n", tv.tv_sec, tv.tv_usec); + if (dtop_print_time_at_poll(fw) == FILE_ERROR) + return FILE_ERROR; + if (dtop_write_pollingdata_csv(dpg_list, fw) == FILE_ERROR) + return FILE_ERROR; + } + + if (quit != QUIT) + dtop_print_snapshot_diff(dpg_list); + return FILE_SUCCESS; +} + +static void dtop_set_niceness(int niceness) +{ + int pid, rc; + pid = getpid(); + printf("Requesting nice %d\n", niceness); + rc = setpriority(PRIO_PROCESS, pid, niceness); + if (rc != 0) + fprintf(stderr, "Error setting priority [%d]\n", errno); + + rc = getpriority(PRIO_PROCESS, pid); + printf("Running with nice %d.\n", rc); +} + +int main(int argc, char **argv) +{ + int parse_status; + printf("DataTop - Version %s\n", VERSION); + printf("(c)2014-2015 Linux Foundation\n"); + + dtop_load_default_options(&usr_cl_opts); + + parse_status = dtop_parse_cli_opts(&usr_cl_opts, argc, argv); + switch (parse_status) { + case PARSE_SUCCESS: + dtop_set_niceness(usr_cl_opts.priority); + break; + + case PARSE_FORCE_EXIT: + exit(EXIT_SUCCESS); + break; + + case PARSE_FAILURE: + default: + printf("Failed to parse command line arguments.\n"); + exit(EXIT_FAILURE); + break; + } + + dtop_dual_line_init("/proc/net/netstat"); + dtop_dual_line_init("/proc/net/snmp"); + dtop_single_line_init("/proc/net/snmp6"); + dtop_gen_init("/proc/sys/net/"); + dtop_gen_init("/sys/module/rmnet_data/parameters/"); + dtop_gen_init("/sys/class/net/rmnet_mhi0/statistics/"); + dtop_gen_init("/sys/class/net/usb_rmnet0/statistics/"); + dtop_gen_init("/sys/class/net/rmnet_ipa0/statistics/"); + dtop_meminfo_init(); + dtop_dev_init(); + dtop_stat_init(); + dtop_cpu_stats_init(); + dtop_gen_init("/sys/kernel/debug/clk/bimc_clk/"); + dtop_gen_init("/sys/kernel/debug/clk/snoc_clk/"); + dtop_gen_init("/sys/kernel/debug/clk/pnoc_clk/"); + + if (usr_cl_opts.print_cl == OPT_CHOSE) { + dtop_poll(first_dpg_list); + dtop_print_terminal(first_dpg_list); + } + + if (usr_cl_opts.print_csv == OPT_CHOSE) { + FILE *to_file = NULL; + if ((dtop_open_writing_file(usr_cl_opts.file_name, + &to_file)) == VALID) { + printf("\nData being polled for %ld seconds.\n", + usr_cl_opts.poll_time); + if (dtop_poll_periodically(first_dpg_list, to_file) + == FILE_ERROR) { + fprintf(stderr, "err=%d: %s\n", errno, + strerror(errno)); + dtop_close_file(to_file); + deconstruct_dpgs(first_dpg_list); + dtop_rem_linked_list(first_dpg_list); + exit(EXIT_FAILURE); + } + dtop_close_file(to_file); + } else { + printf("File Can Not Be Opened\n"); + exit(EXIT_FAILURE); + } + } + + if (usr_cl_opts.snapshot_file) { + if (dtop_print_system_snapshot(usr_cl_opts.snapshot_file) + == FILE_ERROR) { + fprintf(stderr, "err=%d: %s\n", errno, + strerror(errno)); + deconstruct_dpgs(first_dpg_list); + dtop_rem_linked_list(first_dpg_list); + exit(EXIT_FAILURE); + } + } + + if (usr_cl_opts.print_cl == OPT_NOT_CHOSE && + usr_cl_opts.print_csv == OPT_NOT_CHOSE) { + if ((!usr_cl_opts.snapshot_file) + || usr_cl_opts.poll_time_selected == POLL_TIME_SELECTED) { + printf("\nData will now be polled for %ld seconds.\n", + usr_cl_opts.poll_time); + dtop_poll(first_dpg_list); + sleep(usr_cl_opts.poll_time); + dtop_poll(first_dpg_list); + dtop_print_snapshot_diff(first_dpg_list); + } + } + + deconstruct_dpgs(first_dpg_list); + dtop_rem_linked_list(first_dpg_list); + return 0; +} + +/** + * @brief Adds each dpg as a node to a linked list. + * + * Called when a dpg is initialized. + * + * @param dpg A pointer to a data_point_gatherer struct which is to be added to the linked list. + */ +void dtop_register(struct dtop_data_point_gatherer *dpg) +{ + if (dpg) + first_dpg_list = dtop_add_linked_list(dpg, first_dpg_list); +} diff --git a/datatop/src/datatop_cpu_stats_poll.c b/datatop/src/datatop_cpu_stats_poll.c new file mode 100644 index 0000000..275ddd8 --- /dev/null +++ b/datatop/src/datatop_cpu_stats_poll.c @@ -0,0 +1,182 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_cpu_stats_poll.c + * @brief Calls dtop_value_only_init for necessary cpu datapoints. + * + * File contains methods for determing number of cpu's online and calling + * correct initialization function to gather scaling_cur_freq data point + * for each cpu along with each cpu's online status. + */ + +#include <unistd.h> +#include <stdio.h> +#include <dirent.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <ctype.h> +#include "datatop_interface.h" +#include "datatop_fileops.h" +#include "datatop_str.h" +#include "datatop_polling.h" + +#define DTOP_GEN_SIZE 8192 +#define DTOP_GEN_LINE (DTOP_GEN_SIZE>>2) +#define NO_CPUS_ONLINE -1 + +/** + * @brief Searches /sys/devices/system/cpu/ directory to get find number of CPUs. + * + * @return Number of CPUs found in directory. + */ +static int dtop_cpu_search(void) +{ + DIR *dp; + struct dirent *entry; + struct stat s; + int cpu_amt; + char cwd[1024]; + + if (!getcwd(cwd, sizeof(cwd))) { + fprintf(stderr, "Failed to get current working dir\n"); + return -1; + } + + dp = opendir("/sys/devices/system/cpu/"); + if (dp == NULL) { + fprintf(stderr, "err=%d: %s\n", errno, strerror(errno)); + fprintf(stderr, "Cannot open directory: %s\n", + "/sys/devices/system/cpu/"); + return NO_CPUS_ONLINE; + } + + chdir("/sys/devices/system/cpu/"); + cpu_amt = 0; + while ((entry = readdir(dp))) { + if (stat(entry->d_name, &s)) { + printf("stat err=%d: %s\n", errno, strerror(errno)); + return NO_CPUS_ONLINE; + } + + if (entry->d_name[0] == 'c' && + entry->d_name[1] == 'p' && + entry->d_name[2] == 'u' && + (isdigit(entry->d_name[3]))) { + + cpu_amt++; + } + } + + closedir(dp); + chdir(cwd); + return cpu_amt; +} + +/** + * @brief Creates a dpg designed for CPU online and CPU scaling_cur_freq stats. + * + * @param name Name of file dpg represents. + */ +static void construct_cpu_stat_dpg(char *name) +{ + char *file = malloc(strlen(name) + 1); + struct dtop_data_point *dp = + malloc(sizeof(struct dtop_data_point)); + struct dtop_data_point_gatherer *dpg = malloc + (sizeof(struct dtop_data_point_gatherer)); + + strlcpy(file, name, strlen(name) + 1); + + dp[0].type = DTOP_ULONG; + dp[0].name = malloc(5); + strlcpy(dp[0].name, "", 5); + dp[0].prefix = NULL; + dp[0].data.d_ulong = 0; + dp[0].initial_data.d_ulong = 0; + dp[0].skip = DO_NOT_SKIP; + dp[0].initial_data_populated = NOT_POPULATED; + + dpg->prefix = file; + dpg->file = file; + dpg->poll = dtop_value_only_poll; + dpg->data_points = dp; + dpg->data_points_len = 1; + dpg->deconstruct = dtop_value_only_dpg_deconstructor; + + dtop_register(dpg); +} + +/** + * @brief Calls dpg constructor for necessary CPU stat files. + * + * Creates file names based on number of CPUs found and calls the + * dpg constructor for them. + * + * @param file Directory where the CPUs are found. + * @param add String which is concatenated onto file and represents + * the path after a CPU directory is entered. + * @param cpu_amt Amount of CPUs found on device. + */ +static void cpu_poll_helper(char *file, char *add, int cpu_amt) +{ + int i; + for (i = 0; i < cpu_amt; i++) { + char *cpu_num = malloc(5); + char *newfile; + int nf_len; + snprintf(cpu_num, 5, "%d", i); + nf_len = strlen(file) + strlen(add) + strlen(cpu_num) + 2; + newfile = malloc(nf_len); + strlcpy(newfile, file, nf_len); + strlcat(newfile, cpu_num, nf_len); + strlcat(newfile, add, nf_len); + free(cpu_num); + construct_cpu_stat_dpg(newfile); + free(newfile); + } +} + +/** + * @brief Calls necessary functions for CPU stat dpgs. + */ +void dtop_cpu_stats_init(void) +{ + int cpu_amt; + char *file = "/sys/devices/system/cpu/cpu"; + char *add = "/cpufreq/scaling_cur_freq"; + + cpu_amt = dtop_cpu_search(); + cpu_poll_helper(file, add, cpu_amt); + add = "/online"; + cpu_poll_helper(file, add, cpu_amt); +} diff --git a/datatop/src/datatop_dev_poll.c b/datatop/src/datatop_dev_poll.c new file mode 100644 index 0000000..0dcab1e --- /dev/null +++ b/datatop/src/datatop_dev_poll.c @@ -0,0 +1,320 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_dev_poll.c + * @brief Adds ability for data collection from /proc/net/dev + * + * File contains methods for searching and polling data from + * "/proc/net/dev" + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "datatop_interface.h" +#include "datatop_fileops.h" +#include "datatop_str.h" + +#define DTOP_DEV_SIZE 8192 +#define DTOP_DEV_LINE (DTOP_DEV_SIZE>>2) + +/** +* @struct dtop_dev_vars +* @brief Struct used to hold necessary variables for /proc/net/dev dpg +* +* @var dtop_dev_vars::line +* Array of strings where necessary dp names and values are held. +* @var dtop_dev_vars::line_count +* Number of lines the file is that the dpg represents. +*/ +struct dtop_dev_vars { + char **line; + int line_count; +}; + +/** + * @brief Parses lines with data in "/proc/net/dev" + * + * @param line1 Line to parse to find datapoint names and values. + * @param len1 Length of line1. + * @param index Index in the dictionary the key (name) is added to. + * @param dict Dictionary the keys and values are added to. + */ +static void dt_dev_parse(char *line1, int len1, + int index, struct dt_procdict *dict) +{ + int i, start = 0; + int j, k, n; + i = 0; + while (line1[i] == ' ' || line1[i] == ' ') + i++; + dict->key[index] = &line1[i]; + for (i = 0; i < len1; i++) { + if (line1[i] == ':') { + line1[i+1] = 0; + start = i+2; + break; + } + } + + k = 0; + for (j = start; j < len1; j++) { + if (line1[j] != ' ' && line1[j] != ' ') { + dict->val[k] = &line1[j]; + n = j; + while (line1[n] != ' ' && line1[n] != ' ') + n++; + if (n < len1) + line1[n] = 0; + j = n; + k++; + } + } +} + +/** + * @brief Stores the data collected from "/proc/net/dev" + * + * @param dpg Struct that polled data is added to. + * @return DTOP_POLL_IO_ERR - Poll of dpg unsuccessful. + * @return DTOP_POLL_OK - Poll of dpg successful. + */ +int dtop_dev_poll(struct dtop_data_point_gatherer *dpg) +{ + char *data; + int *line_len = malloc(sizeof(int) * + ((struct dtop_dev_vars *) + (dpg->priv))->line_count); + int read; + struct dt_procdict *dict = malloc(sizeof(struct dt_procdict) + *((struct dtop_dev_vars *) + (dpg->priv))->line_count-2); + int j, n, sum; + int index = 0; + int dp = 0; + + read = dt_read_file(dpg->file, &data, DTOP_DEV_SIZE); + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + sum = 0; + /* Assigns each line read from the file, a length */ + for (n = 0; n < ((struct dtop_dev_vars *) + (dpg->priv))->line_count; n++) { + line_len[n] = dt_read_line(((struct dtop_dev_vars *) + (dpg->priv))->line[n], + DTOP_DEV_LINE, data, + DTOP_DEV_SIZE, sum); + if (n <= (((struct dtop_dev_vars *) + (dpg->priv))->line_count - 1)) { + sum += (line_len[n] + 1); + } + + } + + for (n = 2; n < ((struct dtop_dev_vars *) + (dpg->priv))->line_count; n++) { + dt_dev_parse(((struct dtop_dev_vars *) + (dpg->priv))->line[n], line_len[n], + index, &dict[index]); + index++; + } + + + /* Assigns the dp value to the dp struct */ + for (n = 2; n < ((struct dtop_dev_vars *) + (dpg->priv))->line_count; n++) { + for (j = 0; j < 16; j++) { + dtop_store_dp(&(dpg->data_points[dp]), + dict[n-2].val[j]); + dp++; + } + } + + dt_free(&data); + free(line_len); + free(dict); + return DTOP_POLL_OK; +} + +/** + * @brief Frees dynamically allocated "/proc/net/dev" dpg. + * + * Frees the memory of the dpg along with it's data_points + * and other malloc'd memory no longer needed. + * + * @param dpg Dpg to deconstruct and deallocate memory for. + */ +static void dtop_dev_dpg_deconstructor + (struct dtop_data_point_gatherer *dpset) +{ + int i, j, dp; + dp = 0; + for (j = 0; j < ((((struct dtop_dev_vars *) + (dpset->priv))->line_count)-2); j++) { + for (i = 0; i < 16; i++) { + free(dpset->data_points[dp].prefix); + dp++; + } + } + free(dpset->data_points); + for (i = 0; i < ((struct dtop_dev_vars *) + (dpset->priv))->line_count; i++) + free(((struct dtop_dev_vars *)(dpset->priv))->line[i]); + free(((struct dtop_dev_vars *)(dpset->priv))->line); + free(((struct dtop_dev_vars *)(dpset->priv))); + free(dpset); +} + +/** + * @brief Creates a dpg for "/proc/net/dev" file + * + * Dynamically allocates memory for dpg which is then added to a linked list + * via the dtop_register(dpg) function call. + * + * @param data_points dtop_data_point struct that dpg points to. + * @param storage dtop_dev_vars struct that holds relevant dpg variables. + */ +static void construct_dev_file_dpg(struct dtop_dev_vars *storage, + int dp_count, struct dtop_data_point *data_points) +{ + struct dtop_data_point_gatherer *dpg = malloc + (sizeof(struct dtop_data_point_gatherer)); + + dpg->prefix = "/proc/net/dev"; + dpg->file = "/proc/net/dev"; + dpg->poll = dtop_dev_poll; + dpg->data_points = data_points; + dpg->priv = (struct dtop_dev_vars *)storage; + dpg->data_points_len = dp_count; + dpg->deconstruct = dtop_dev_dpg_deconstructor; + + dtop_register(dpg); +} + +/** + * @brief Scans "/proc/net/dev in order to autodetect dps. + * + * Searches through "/proc/net/dev" file for all available data + * points to create as dp structs. + * + * @param name This is the file name "/proc/net/dev" passed in by dtop_dev_init + * @param storage dtop_dev_vars struct where relevant variables are stored. + */ +int dtop_dev_search(char *name, struct dtop_dev_vars *storage) +{ + int i, n, sum; + char *data; + int *line_len = malloc(sizeof(int) * storage->line_count); + int read; + struct dt_procdict dict; + struct dt_procdict dev_dict; + struct dtop_data_point *data_points = malloc + (sizeof(struct dtop_data_point) * 16 * (storage->line_count-2)); + int dp_count = (16 * (storage->line_count - 2)); + int index = 0; + int dp = 0; + + storage->line = malloc(storage->line_count * sizeof(*storage->line)); + + for (i = 0; i < storage->line_count; i++) + storage->line[i] = malloc(sizeof(char) * DTOP_DEV_LINE); + + dev_dict.val[0] = "bytes"; + dev_dict.val[1] = "packets"; + dev_dict.val[2] = "errs"; + dev_dict.val[3] = "drop"; + dev_dict.val[4] = "fifo"; + dev_dict.val[5] = "frame"; + dev_dict.val[6] = "compressed"; + dev_dict.val[7] = "multicast"; + dev_dict.val[8] = "bytes"; + dev_dict.val[9] = "packets"; + dev_dict.val[10] = "errs"; + dev_dict.val[11] = "drop"; + dev_dict.val[12] = "fifo"; + dev_dict.val[13] = "colls"; + dev_dict.val[14] = "carrier"; + dev_dict.val[15] = "compressed"; + + read = dt_read_file(name, &data, DTOP_DEV_SIZE); + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + sum = 0; + /* Assigns each line read from the file, a length */ + for (n = 0; n < storage->line_count; n++) { + line_len[n] = dt_read_line(storage->line[n], + DTOP_DEV_LINE, data, + DTOP_DEV_SIZE, sum); + if (n < (storage->line_count - 1)) + sum += (line_len[n] + 1); + } + + construct_dev_file_dpg(storage, dp_count, data_points); + + for (n = 2; n < storage->line_count; n++) { + dt_dev_parse(storage->line[n], line_len[n], index, &dict); + index++; + } + + for (n = 2; n < storage->line_count; n++) { + for (i = 0; i < 16; i++) { + char *pref = malloc(30 * sizeof(char)); + data_points[dp].skip = 0; + data_points[dp].initial_data_populated = NOT_POPULATED; + if (i < 8) + strlcpy(pref, "Receive:", 30 * sizeof(char)); + else if (i >= 8) + strlcpy(pref, "Transmit:", 30 * sizeof(char)); + strlcat(pref, dev_dict.val[i], 30 * sizeof(char)); + data_points[dp].prefix = pref; + data_points[dp].name = dict.key[n-2]; + data_points[dp].type = DTOP_ULONG; + dp++; + } + index++; + } + + free(line_len); + dt_free(&data); + return DTOP_POLL_OK; +} + +/** + * @brief Calls dtop_search for "/proc/net/dev" file. + */ +void dtop_dev_init(void) +{ + struct dtop_dev_vars *storage = malloc + (sizeof(struct dtop_dev_vars)); + storage->line_count = dtop_get_file_line_amount("/proc/net/dev"); + dtop_dev_search("/proc/net/dev", storage); +} diff --git a/datatop/src/datatop_dual_line_poll.c b/datatop/src/datatop_dual_line_poll.c new file mode 100644 index 0000000..8ada0c1 --- /dev/null +++ b/datatop/src/datatop_dual_line_poll.c @@ -0,0 +1,319 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_dual_line_poll.c + * @brief Adds ability for data collection from dual line files. + * + * File contains methods for searching and polling data from + * dual line files, meaning the first line contains the dp names + * while the second line contains the corresponding values. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "datatop_interface.h" +#include "datatop_fileops.h" +#include "datatop_str.h" + +#define DTOP_DUAL_SIZE 8192 +#define DTOP_DUAL_LINE (DTOP_DUAL_SIZE>>2) + +/** +* @struct dtop_dual_line_vars +* @brief Struct used to hold necessary variables for dual_line_file dpgs. +* +* @var dtop_dual_line_vars::line +* Array of strings where necessary dp names and values are held. +* @var dtop_dual_line_vars::line2 +* Array of strings where necessary dp names and values are held. +* @var dtop_dual_line_vars::line_count +* Number of lines the file is that the dpg represents. +*/ +struct dtop_dual_line_vars { + char **line; + char **line2; + int line_count; +}; + +/** + * @brief Stores the data collected from a dual_line file. + * + * @param dpg Struct that polled data is added to. + * @return DTOP_POLL_IO_ERR - Poll of dpg unsuccessful. + * @return DTOP_POLL_OK - Poll of dpg successful. + */ +int dtop_dual_line_poll(struct dtop_data_point_gatherer *dpg) +{ + char *data; + int *line_len = malloc(sizeof(int) * + ((struct dtop_dual_line_vars *) + (dpg->priv))->line_count); + int *line_len2 = malloc(sizeof(int) * + ((struct dtop_dual_line_vars *) + (dpg->priv))->line_count); + int read; + + struct dt_procdict *dict = malloc(sizeof(struct dt_procdict) + * (((struct dtop_dual_line_vars *) + (dpg->priv))->line_count/2)); + struct dt_procdict *prefix_dict = malloc(sizeof(struct dt_procdict) + * (((struct dtop_dual_line_vars *) + (dpg->priv))->line_count/2)); + int i, j, k, n, sum, sum2; + + read = dt_read_file(dpg->file, &data, DTOP_DUAL_SIZE); + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + sum = 0; + sum2 = 0; + /* Assigns each line read from the file, a length */ + for (n = 0; n < ((struct dtop_dual_line_vars *) + (dpg->priv))->line_count; n++) { + line_len[n] = dt_read_line(((struct dtop_dual_line_vars *) + (dpg->priv))->line[n], + DTOP_DUAL_LINE, data, + DTOP_DUAL_SIZE, sum); + line_len2[n] = dt_read_line(((struct dtop_dual_line_vars *) + (dpg->priv))->line2[n], + DTOP_DUAL_LINE, data, + DTOP_DUAL_SIZE, sum2); + if (n <= (((struct dtop_dual_line_vars *) + (dpg->priv))->line_count-2)) { + sum += (line_len[n] + 1); + sum2 += (line_len2[n] + 1); + } + + } + + /* Stores dp names and values in dictionary */ + for (i = 0; i < (((struct dtop_dual_line_vars *) + (dpg->priv))->line_count/2); i++) + dt_parse_proc_dictionary(((struct dtop_dual_line_vars *) + (dpg->priv))->line[2*i], + line_len[2*i], + ((struct dtop_dual_line_vars *) + (dpg->priv))->line[(2*i)+1], + line_len[(2*i)+1], + &dict[i]); + + /* Stores dp prefices in dictionary */ + for (i = 0; i < (((struct dtop_dual_line_vars *) + (dpg->priv))->line_count/2); i++) + dt_parse_for_prefix(((struct dtop_dual_line_vars *) + (dpg->priv))->line2[2*i], line_len2[2*i], + &prefix_dict[i]); + + /* Assigns a dp value to each dp struct */ + for (k = 0; k < (((struct dtop_dual_line_vars *) + (dpg->priv))->line_count/2); k++) { + for (j = 0; j < dpg->data_points_len; j++) { + i = dt_find_dict_idx(dpg->data_points[j].name, + &dict[k]); + if (i >= 0 && i < dict[k].max && + (strcmp(dpg->data_points[j].prefix, + prefix_dict[k].val[i]) == 0)) + + dtop_store_dp(&(dpg->data_points[j]), + dict[k].val[i]); + } + } + + dt_free(&data); + free(line_len); + free(line_len2); + free(dict); + free(prefix_dict); + return DTOP_POLL_OK; +} + +/** + * @brief Frees dynamically allocated dual_line_file dpgs. + * + * Frees the memory of the dpg along with it's data_points + * and other malloc'd memory no longer needed. + * + * @param dpg Dpg to deconstruct and deallocate memory for. + */ +static void dtop_dual_line_dpg_deconstructor + (struct dtop_data_point_gatherer *dpset) +{ + int i; + free(dpset->data_points); + for (i = 0; i < ((struct dtop_dual_line_vars *) + (dpset->priv))->line_count; i++) { + free(((struct dtop_dual_line_vars *)(dpset->priv))->line[i]); + free(((struct dtop_dual_line_vars *)(dpset->priv))->line2[i]); + } + free(((struct dtop_dual_line_vars *)(dpset->priv))->line); + free(((struct dtop_dual_line_vars *)(dpset->priv))->line2); + free(((struct dtop_dual_line_vars *)(dpset->priv))); + free(dpset); +} + +/** + * @brief Creates a dpg for a dual_line file. + * + * Dynamically allocates memory for dpg which is then added to a linked list + * via the dtop_register(dpg) function call. + * + * @param name Name of file dpg represents. + * @param data_points dtop_data_point struct that dpg points to. + * @param storage dtop_dual_line_vars struct that hold relevant dpg variables. + * @param dp_count Number of datapoints in dtop_data_point struct array. + */ +static void construct_dual_line_file_dpg(char *name, struct dtop_data_point + *data_points, struct dtop_dual_line_vars *storage, int dp_count) +{ + struct dtop_data_point_gatherer *dpg = malloc + (sizeof(struct dtop_data_point_gatherer)); + dpg->prefix = name; + dpg->file = name; + dpg->poll = dtop_dual_line_poll; + dpg->data_points = data_points; + dpg->priv = (struct dtop_dual_line_vars *)storage; + dpg->data_points_len = dp_count; + dpg->deconstruct = dtop_dual_line_dpg_deconstructor; + + dtop_register(dpg); +} + +/** + * @brief Scans a dual_line file for all datapoints and creats dps. + * + * Searches through a dual_line file (Key on one line with value on next line) + * for all available data points to create as dp structs. + * + * @param name Name of file. + * @param storage dtop_dual_line_vars struct where relevant variables are stored. + */ +int dtop_dual_line_search(char *name, struct dtop_dual_line_vars *storage) +{ + int i, j, k, n, sum, sum2; + char *data; + int *line_len = malloc(sizeof(int) * storage->line_count); + int *line_len2 = malloc(sizeof(int) * storage->line_count); + int read; + struct dt_procdict *dict, *prefix_dict; + struct dtop_data_point *data_points; + int dp_count = 0; + + storage->line = malloc(storage->line_count * sizeof(*storage->line)); + storage->line2 = malloc(storage->line_count * sizeof(*storage->line2)); + for (i = 0; i < storage->line_count; i++) { + storage->line[i] = malloc(sizeof(char) * DTOP_DUAL_LINE); + storage->line2[i] = malloc(sizeof(char) * DTOP_DUAL_LINE); + } + dict = malloc(sizeof(struct dt_procdict) * (storage->line_count/2)); + prefix_dict = malloc(sizeof(struct dt_procdict) + * (storage->line_count/2)); + + read = dt_read_file(name, &data, DTOP_DUAL_SIZE); + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + sum = 0; + sum2 = 0; + /* Assigns each line read from the file, a length */ + for (n = 0; n < storage->line_count; n++) { + line_len[n] = dt_read_line(storage->line[n], + DTOP_DUAL_LINE, data, + DTOP_DUAL_SIZE, sum); + line_len2[n] = dt_read_line(storage->line2[n], + DTOP_DUAL_LINE, data, + DTOP_DUAL_SIZE, sum2); + if (n <= (storage->line_count-2)) { + sum += (line_len[n] + 1); + sum2 += (line_len2[n] + 1); + } + + } + + /* Stores dp names and prefixes in dictionaries */ + for (i = 0; i < (storage->line_count/2); i++) + dt_parse_proc_dictionary(storage->line[2*i], line_len[2*i], + storage->line[(2*i)+1], line_len[(2*i)+1], &dict[i]); + + for (i = 0; i < (storage->line_count/2); i++) + dt_parse_for_prefix(storage->line2[2*i], line_len2[2*i], + &prefix_dict[i]); + + /* Finds how many data points were gathered from the file */ + for (j = 0; j < (storage->line_count/2); j++) { + for (i = 0; i < dict[j].max; i++) + dp_count++; + } + + data_points = malloc(dp_count * sizeof(struct dtop_data_point)); + + k = 0; + /* Creates a dtop_data_point struct for each dp found in the file */ + for (j = 0; j < (storage->line_count/2); j++) + for (i = 0; i < dict[j].max; i++) { + if (dict[j].val[i][0] == '-') + data_points[k].type = DTOP_LONG; + else + data_points[k].type = DTOP_ULONG; + data_points[k].name = dict[j].key[i]; + data_points[k].prefix = prefix_dict[j].val[i]; + data_points[k].skip = DO_NOT_SKIP; + data_points[k].initial_data_populated = NOT_POPULATED; + k++; + } + + /* Calls dpg constructor, dpg will point to the dp struct */ + construct_dual_line_file_dpg(name, data_points, storage, dp_count); + + free(line_len); + free(line_len2); + free(dict); + free(prefix_dict); + dt_free(&data); + + return DTOP_POLL_OK; +} + +/** + * @brief Calls dtop_search for a file with dual line pairs. + */ +void dtop_dual_line_init(char *name) +{ + struct dtop_dual_line_vars *storage = malloc + (sizeof(struct dtop_dual_line_vars)); + storage->line_count = dtop_get_file_line_amount(name); + + if (storage->line_count%2 != 0) { + printf("Dual line file, %s, contains error.\n", name); + printf("Data will not be collected from %s\n", name); + return; + } + dtop_dual_line_search(name, storage); +} diff --git a/datatop/src/datatop_fileops.c b/datatop/src/datatop_fileops.c new file mode 100644 index 0000000..52068f5 --- /dev/null +++ b/datatop/src/datatop_fileops.c @@ -0,0 +1,167 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_fileops.c + * @brief Declares functions for reading and writing to files. + * + * Declares functions called when reading from files which data is collected. + * Also contains methods to handle files which will be written to. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include "datatop_interface.h" +#include "datatop_linked_list.h" +#include "datatop_opt.h" +#include "datatop_fileops.h" + +/** + * @brief Reads the lines from files which we are collecting data from. + * + * @param file File which is read from + * @param buffer Pointer to buffer where data will be read. The buffer is allocated + * in dt_read_file() and passed back to the caller. Caller should + * free this when done. + * @param len Maximum amount of data which should be read from the file. + * @return Number of bytes of data placed in *buffer. + */ +int dt_read_file(const char *file, char **buffer, int len) +{ + int read; + FILE *fp; + + *buffer = (char *)malloc(len); + if (!(*buffer)) { + fprintf(stderr, "%s(): malloc(%d) failed\n", __func__, len); + return 0; + } + + fp = fopen(file, "r"); + if (!fp) { + fprintf(stderr, "%s(): Failed to open %s: ", __func__, file); + fprintf(stderr, "Error: %s\n", strerror(errno)); + free(*buffer); + *buffer = 0; + return 0; + } + read = fread(*buffer, sizeof(char), len, fp); + fclose(fp); + + return read; +} + +/** + * @brief Deallocates memory no longer being used. + * + * @param buffer Buffer to be deallocated. + */ +void dt_free(char **buffer) +{ + free(*buffer); + *buffer = 0; +} + +/** + * @brief Checks for access to a file for writing. + * + * @param fw File to check access of. + * @return INVALID - File already exists or write access denied. + * @return VALID - File does not exist and can be written to. + */ +int dtop_check_writefile_access(char *fw) +{ + if (!access(fw, F_OK)) { + printf("File specified already exists\n"); + return INVALID; + } + + if (!access(fw, W_OK)) { + printf("Permission to write to specified file denied\n"); + return INVALID; + } + + return VALID; +} + +/** + * @brief Opens file and handles possible errors. + * + * @param fw File path to be opened. + * @param to_file Pointer to the *file that is opened. + * @return VALID - File opened successfully. + * @return INVALID - File could not be opened. + */ +int dtop_open_writing_file(char *fw, FILE **to_file) +{ + *to_file = fopen(fw, "w"); + if (*to_file) { + return VALID; + } else { + fprintf(stderr, "Value of errno: %d\n", errno); + fprintf(stderr, "Error opening file: %s\n", strerror(errno)); + fprintf(stderr, "Please try writing to a non-existent file\n"); + printf("See datatop -h for help\n"); + return INVALID; + } +} + +/** + * @brief Closes a file if not a standard stream. + * + * @param fw File to be closed. + */ +void dtop_close_file(FILE *fw) +{ + if (fw != stdout && fw != stderr && fw != stdin) + fclose(fw); +} + +/** + * @brief Helper function to find number of lines in dual_line file. + * + * @return Number of lines in a dual_line file. + */ +int dtop_get_file_line_amount(char *name) +{ + signed char rc = -1; + int line_count = 0; + FILE *file = fopen(name, "r"); + while (rc != EOF) { + if (rc == '\n') + line_count++; + rc = fgetc(file); + } + + fclose(file); + return line_count; +} diff --git a/datatop/src/datatop_fileops.h b/datatop/src/datatop_fileops.h new file mode 100644 index 0000000..0078e06 --- /dev/null +++ b/datatop/src/datatop_fileops.h @@ -0,0 +1,48 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_fileops.h + * @brief Declares functions held within datatop_fileops.h + */ + +#ifndef DATATOP_FILEOPS_H +#define DATATOP_FILEOPS_H + +#include "datatop_interface.h" +#include "datatop_linked_list.h" +#include "datatop_opt.h" + +int dt_read_file(const char *file, char **buffer, int len); +void dt_free(char **buffer); +int dtop_check_writefile_access(char *fw); +int dtop_open_writing_file(char *fw, FILE **to_file); +void dtop_close_file(FILE *fw); +int dtop_get_file_line_amount(char *file); +#endif /* DATATOP_FILEOPS_H */ diff --git a/datatop/src/datatop_gen_poll.c b/datatop/src/datatop_gen_poll.c new file mode 100644 index 0000000..457cc35 --- /dev/null +++ b/datatop/src/datatop_gen_poll.c @@ -0,0 +1,276 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_gen_poll.c + * @brief Contains functions which add ability to scan directories for data points. + * + * Contains functions that search through a directory and create dpg's for any + * important data values found which can then be polled for data collection. + */ + +#include <unistd.h> +#include <stdio.h> +#include <dirent.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/stat.h> +#include <sys/types.h> + +#include "datatop_interface.h" +#include "datatop_fileops.h" +#include "datatop_str.h" +#include "datatop_gen_poll.h" + +#define DTOP_GEN_SIZE 8192 +#define DTOP_GEN_LINE (DTOP_GEN_SIZE>>2) + +/** + * @brief Searches a file to find the number of data values it contains. + * + * @param dpg The struct which contains the file to search. + * @return Number of datapoints found in the file. + */ +static int get_number_of_values(struct dtop_data_point_gatherer *dpg) +{ + char *data; + int read; + char line[DTOP_GEN_LINE]; + int line_len; + int i, num; + + read = dt_read_file(dpg->file, &data, DTOP_GEN_SIZE); + line_len = dt_read_line(line, DTOP_GEN_LINE, data, DTOP_GEN_SIZE, 0); + + if (line_len < 1) { + dt_free(&data); + return 0; + } + + num = 1; + for (i = 0; i < line_len; i++) { + if ((line[i] == ' ' || line[i] == ',' + || line[i] == ' ') &&line[i+1] != 0) + num++; + } + + dt_free(&data); + return num; +} + +/** + * @brief Stores the data collected from a dpg that was constructed during dtop_search. + * + * @param dpg Struct that polled data is added to. + * @return DTOP_POLL_IO_ERR - Poll of dpg unsuccessful. + * @return DTOP_POLL_OK - Poll of dpg successful. + */ +int dtop_gen_poll(struct dtop_data_point_gatherer *dpg) +{ + char *data; + int read; + char line[DTOP_GEN_LINE]; + int line_len; + struct dt_procdict dict; + int i; + + read = dt_read_file(dpg->file, &data, DTOP_GEN_SIZE); + line_len = dt_read_line(line, DTOP_GEN_LINE, data, DTOP_GEN_SIZE, 0); + + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + dt_single_line_parse(line, line_len, &dict); + + for (i = 0; i < dpg->data_points_len; i++) { + if (dict.val[i][0] == '-') + dpg->data_points[i].type = DTOP_LONG; + dtop_store_dp(&(dpg->data_points[i]), dict.val[i]); + } + + dt_free(&data); + return DTOP_POLL_OK; +} + +/** + * @brief Frees dynamically allocated dpg's. + * + * Frees the memory of dpg variables and the dpg for all dynamically allocated + * dpgs. + * + * @param dpg Dpg to deconstruct and deallocate memory for. + */ +static void dtop_gen_dpg_deconstructor(struct dtop_data_point_gatherer *dpset) +{ + int i; + for (i = 0; i < dpset->data_points_len; i++) + free(dpset->data_points[i].name); + free(dpset->data_points); + free(dpset->file); + free(dpset->prefix); + free(dpset); +} + +/** + * @brief Creates a dpg and all necessary dp's corresponding to it. + * + * Dynamically allocates memory for dpg and dp structs which are then + * created and added to a linked_list of dpgs through the dtop_register + * function. + * + * @param dir Directory which file is located in, assigned to the dpg prefix. + * @param name Name of file that dpg represents, assigned to a dp name. + */ +static void dpg_construction(char *dir, char *name) +{ + int num, i; + int both_len = strlen(dir) + strlen(name) + 1; + char *both = malloc(both_len); + char *maindir; + struct dtop_data_point_gatherer *dpg = malloc + (sizeof(struct dtop_data_point_gatherer)); + strlcpy(both, dir, both_len); + strlcat(both, name, both_len); + maindir = malloc(strlen(dir) + 1); + strlcpy(maindir, dir, strlen(dir) + 1); + + dpg->prefix = maindir; + dpg->file = both; + dpg->poll = dtop_gen_poll; + dpg->deconstruct = dtop_gen_dpg_deconstructor; + num = get_number_of_values(dpg); + + if (num != 0) { + struct dtop_data_point *dp = malloc + (num * sizeof(struct dtop_data_point)); + for (i = 0; i < num; i++) { + if (num == 1) { + dp[i].name = malloc(strlen(name) + 1); + strlcpy(dp[i].name, name, strlen(name) + 1); + } else { + char *add = malloc(7 * sizeof(char)); + char *newname; + int nn_len, dpn_len; + snprintf(add, 7 * sizeof(char), "[%d]:", i); + nn_len = strlen(name) + strlen(add) + 1; + newname = malloc(nn_len); + strlcpy(newname, name, nn_len); + strlcat(newname, add, nn_len); + dpn_len = strlen(newname) + 1; + dp[i].name = malloc(dpn_len); + strlcpy(dp[i].name, newname, dpn_len); + free(add); + free(newname); + } + dp[i].prefix = NULL; + dp[i].type = DTOP_ULONG; + dp[i].skip = DO_NOT_SKIP; + dp[i].initial_data_populated = NOT_POPULATED; + } + + dpg->data_points = dp; + dpg->data_points_len = num; + + dtop_register(dpg); + } else { + free(dpg->prefix); + free(dpg->file); + free(dpg); + } +} + +/** + * @brief Scans a directory for all important datapoints to be collected. + * + * Recursively scans a directory and locates all files which data will be + * collected from. + * + * @param dir Directory to search. + */ +static int dtop_search(char *dir) +{ + DIR *dp; + struct dirent *entry; + struct stat s; + char cwd[1024]; + + if (!getcwd(cwd, sizeof(cwd))) { + fprintf(stderr, "Failed to get current working dir\n"); + return -1; + } + + dp = opendir(dir); + if (dp == NULL) { + fprintf(stderr, "err=%d: %s\n", errno, strerror(errno)); + fprintf(stderr, "Cannot open directory: %s\n", dir); + return DIR_FAILURE; + } + + chdir(dir); + + while ((entry = readdir(dp))) { + if (stat(entry->d_name, &s)) { + printf("stat err=%d: %s\n", errno, strerror(errno)); + return DIR_FAILURE; + } + + if (strcmp(".", entry->d_name) != 0 && + strcmp("..", entry->d_name) != 0 && + S_ISREG(s.st_mode)) { + + dpg_construction(dir, entry->d_name); + + } else if (strcmp(".", entry->d_name) != 0 && + strcmp("..", entry->d_name) != 0 && + S_ISDIR(s.st_mode)) { + int nd_len = strlen(dir) + strlen(entry->d_name) + 2; + char *newdir = malloc(nd_len); + strlcpy(newdir, dir, nd_len); + strlcat(newdir, entry->d_name, nd_len); + strlcat(newdir, "/", nd_len); + dtop_search(newdir); + free(newdir); + } + } + + closedir(dp); + chdir(cwd); + return DIR_SUCCESS; +} + +/** + * @brief Calls dtop_search for any specified directory. + * + * @param dir Directory to search. + */ +void dtop_gen_init(char *dir) +{ + dtop_search(dir); +} diff --git a/datatop/src/datatop_gen_poll.h b/datatop/src/datatop_gen_poll.h new file mode 100644 index 0000000..6baffc9 --- /dev/null +++ b/datatop/src/datatop_gen_poll.h @@ -0,0 +1,43 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_gen_poll.h + * @brief Declares functions held within datatop_gen_poll.c + */ + +#ifndef DATATOP_GEN_POLL_H +#define DATATOP_GEN_POLL_H + +#define DIR_SUCCESS 0 +#define DIR_FAILURE 1 + +void dtop_gen_init(char *dir); +#endif /* DATATOP_GEN_POLL_H */ + diff --git a/datatop/src/datatop_helpers.c b/datatop/src/datatop_helpers.c new file mode 100644 index 0000000..55fbb09 --- /dev/null +++ b/datatop/src/datatop_helpers.c @@ -0,0 +1,493 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_helpers.c + * @brief Contains functions which output data. + * + * Contains functions which are used for printing data to output streams. + * Handles all formatting for data output. Also contains functions which + * are responsible for data gathering and collection. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <sys/time.h> +#include <string.h> +#include <errno.h> +#include "datatop_interface.h" +#include "datatop_linked_list.h" +#include "datatop_fileops.h" + +/** + * @brief Prints the name and prefix of a datapoint. + * + * @param dp Dp whose name and prefix is printed. + * @param prefix Directory where dp is contained. + * @param fw File to print the information to. + * @return FILE_ERROR - Writing to file was unsuccessful. + * @return FILE_SUCCESS - Writing to file was successful. + */ +static int dtop_format_dp_names(struct dtop_data_point *dp, const char + *prefix, FILE *fw) +{ + if (dp->prefix) { + if (fprintf(fw, "\"%s:%s:%s\",", prefix, dp->prefix, + dp->name) < 0) + return FILE_ERROR; + } else { + if (fprintf(fw, "\"%s::%s\",", prefix, dp->name) < 0) + return FILE_ERROR; + } + return FILE_SUCCESS; +} + +/** + * @brief Prints the value of a datapoint. + * + * Checks the type of the value and will print it accordingly. + * + * @param dp Pointer to the data_point struct which holds the value that will + * be printed. + * @param fw File to print the information to. + * @return FILE_ERROR - Writing to file was unsuccessful. + * @return FILE_SUCCESS - Writing to file was successful. + */ +static int dtop_format_dp_values(struct dtop_data_point *dp, FILE *fw) +{ + switch (dp->type) { + case DTOP_ULONG: + if (fprintf(fw, "%lu", dp->data.d_ulong) < 0) + return FILE_ERROR; + break; + case DTOP_LONG: + if (fprintf(fw, "%ld", dp->data.d_long) < 0) + return FILE_ERROR; + break; + case DTOP_UINT: + if (fprintf(fw, "%d", dp->data.d_uint) < 0) + return FILE_ERROR; + break; + case DTOP_INT: + if (fprintf(fw, "%u", dp->data.d_uint) < 0) + return FILE_ERROR; + break; + case DTOP_UCHAR: + if (fprintf(fw, "%c,", dp->data.d_uchar) < 0) + return FILE_ERROR; + if (fprintf(fw, "(0x%02X)", dp->data.d_uchar) < 0) + return FILE_ERROR; + break; + case DTOP_CHAR: + if (fprintf(fw, "%c,", dp->data.d_char) < 0) + return FILE_ERROR; + if (fprintf(fw, "(%d)", dp->data.d_char) < 0) + return FILE_ERROR; + break; + case DTOP_STR: + if (fprintf(fw, "\"%s\"", dp->data.d_str) < 0) + return FILE_ERROR; + break; + default: + if (fprintf(fw, "UNKNOWN_TYPE") < 0) + return FILE_ERROR; + break; + } + return FILE_SUCCESS; +} + +/** + * @brief Prints the name and prefix of a dp, formatted appropriately. + * + * @param dpset data_point_gatherer used to access dp directory. + * @param dp data_point used to get datapoint prefix if available. + */ +static void dtop_format_text_for_snapshot + (struct dtop_data_point_gatherer *dpset, struct dtop_data_point dp) +{ + printf("%s:", dpset->prefix); + if (dp.prefix) + printf("%s:", dp.prefix); + + printf("%s::", dp.name); +} + +/** + * @brief Prints a datapoint value to a specified csv file. + * + * @param dp Datapoint that holds the value to be printed. + * @param fw File to print to. + * @return FILE_ERROR - Writing to file was unsuccessful. + * @return FILE_SUCCESS - Writing to file was successful. + */ +static int dtop_print_dp_csv(struct dtop_data_point *dp, FILE *fw) +{ + if (dtop_format_dp_values(dp, fw) == FILE_ERROR) + return FILE_ERROR; + if (fprintf(fw, ",") < 0) + return FILE_ERROR; + return FILE_SUCCESS; +} + +/** + * @brief Prints a datapoint value to the terminal. + * + * @param dp Holds the value to be printed print. + * @param prefix Used to print prefix of the data_point. + */ +static void dtop_print_dp(struct dtop_data_point *dp, const char *prefix) +{ + dtop_format_dp_names(dp, prefix, stdout); + printf(" "); + dtop_format_dp_values(dp, stdout); + printf("\n"); +} + +/** + * @brief Finds delta(value) of a datapoint. + * + * Function accounts for different types that values may be. + * + * @param dpset Pointer to a data_point used as another parameter for printing. + * @param dp Datapoint which contains the value to find the difference of. + */ +static void dtop_handle_dp_type_for_snapshot( + struct dtop_data_point_gatherer *dpset, struct dtop_data_point dp) +{ + int64_t int64; + + switch (dp.type) { + case DTOP_ULONG: + default: + /* This is less than ideal. Replace with 128-bit ops later */ + int64 = (int64_t)dp.data.d_ulong + - (int64_t)dp.initial_data.d_ulong; + if (int64 != 0) { + dtop_format_text_for_snapshot(dpset, dp); + #if (__SIZEOF_LONG__ == 4) + printf("%lld\n", int64); + #elif (__SIZEOF_LONG__ == 8) + printf("%ld\n", int64); + #endif + } + break; + + case DTOP_LONG: + /* This is less than ideal. Replace with 128-bit ops later */ + int64 = (int64_t)dp.data.d_long + - (int64_t)dp.initial_data.d_long; + if (int64 != 0) { + dtop_format_text_for_snapshot(dpset, dp); + #if (__SIZEOF_LONG__ == 4) + printf("%lld\n", int64); + #elif (__SIZEOF_LONG__ == 8) + printf("%ld\n", int64); + #endif + } + break; + + case DTOP_UINT: + int64 = (int64_t)dp.data.d_uint + - (int64_t)dp.initial_data.d_uint; + if (int64 != 0) { + dtop_format_text_for_snapshot(dpset, dp); + #if (__SIZEOF_LONG__ == 4) + printf("%lld\n", int64); + #elif (__SIZEOF_LONG__ == 8) + printf("%ld\n", int64); + #endif + } + break; + + case DTOP_INT: + int64 = (int64_t)dp.data.d_int + - (int64_t)dp.initial_data.d_int; + if (int64 != 0) { + dtop_format_text_for_snapshot(dpset, dp); + #if (__SIZEOF_LONG__ == 4) + printf("%lld\n", int64); + #elif (__SIZEOF_LONG__ == 8) + printf("%ld\n", int64); + #endif + } + break; + } +} + +/** + * @brief Calls the dtop_print_dp_csv function for each data_point a dpg has access to. + * + * @param dpg A data_point_gatherer struct that is iterated through for each datapoint. + * @param fw File to print datapoint values to. + * @return FILE_ERROR - Writing to file was unsuccessful. + * @return FILE_SUCCESS - Writing to file was successful. + */ +static int dtop_print_dpg_csv(struct dtop_data_point_gatherer *dpg, FILE *fw) +{ + int i; + + for (i = 0; i < dpg->data_points_len; i++) + if (dtop_print_dp_csv(&(dpg->data_points[i]), fw) == FILE_ERROR) + return FILE_ERROR; + return FILE_SUCCESS; +} + +/** + * @brief Calls the dtop_format_dp_names function for each data_point a dpg has access to. + * + * @param dpg A data_point_gatherer struct that is iterated through for each datapoint. + * @param fw File to printg datapoint names and prefixes to. + * @return FILE_ERROR - Writing to file was unsuccessful. + * @return FILE_SUCCESS - Writing to file was successful. + */ +int dtop_print_dpg_names_csv(struct dtop_data_point_gatherer *dpg, FILE *fw) +{ + int i; + + for (i = 0; i < dpg->data_points_len; i++) + if (dtop_format_dp_names(&(dpg->data_points[i]), + dpg->prefix, fw) == FILE_ERROR) + return FILE_ERROR; + + return FILE_SUCCESS; +} + +/** + * @brief Prints all dp values to a specified file. + * + * This function is responsible for the printing of all data_point values + * to a specified file. It will iterate through the linked list which contains + * all of the dpgs and will print each dp value, being sure to flush the buffer. + * + * @param dpg_list Pointer to first node of linked list which contains all dpgs. + * @param fw File that data prints to. + * @return FILE_ERROR - Writing to file was unsuccessful. + * @return FILE_SUCCESS - Writing to file was successful. + */ +int dtop_write_pollingdata_csv(struct dtop_linked_list *dpg_list, FILE *fw) +{ + struct dtop_linked_list *curr_ptr = dpg_list; + struct dtop_data_point_gatherer *dpset; + + while (curr_ptr) { + dpset = (struct dtop_data_point_gatherer *) curr_ptr->data; + if (dtop_print_dpg_csv(dpset, fw) == FILE_ERROR) + return FILE_ERROR; + curr_ptr = curr_ptr->next_ptr; + fflush(fw); + } + + if (fprintf(fw, "\n") < 0) + return FILE_ERROR; + + return FILE_SUCCESS; +} + +/** + * @brief Calls the dtop_print_dp function for each data_point a dpg has access to. + * + * @param dpg A data_point_gatherer struct that is iterated through for each datapoint. + */ +void dtop_print_dpg(struct dtop_data_point_gatherer *dpg) +{ + int i; + for (i = 0; i < dpg->data_points_len; i++) + dtop_print_dp(&(dpg->data_points[i]), dpg->prefix); +} + +/** + * @brief Stores the values for the datapoints and populates the initial value. + * + * @param dp A datapoint whose value will be stored. + * @param str Str used for sscanf function call to find value of dp. + */ +void dtop_store_dp(struct dtop_data_point *dp, const char *str) +{ + switch (dp->type) { + case DTOP_ULONG: + sscanf(str, "%lu", &(dp->data.d_ulong)); + if (dp->initial_data_populated == NOT_POPULATED) { + dp->initial_data.d_ulong = dp->data.d_ulong; + dp->initial_data_populated = POPULATED; + } + break; + case DTOP_LONG: + sscanf(str, "%ld", &(dp->data.d_long)); + if (dp->initial_data_populated == NOT_POPULATED) { + dp->initial_data.d_long = dp->data.d_long; + dp->initial_data_populated = POPULATED; + } + break; + case DTOP_UINT: + sscanf(str, "%u", &(dp->data.d_uint)); + if (dp->initial_data_populated == NOT_POPULATED) { + dp->initial_data.d_uint = dp->data.d_uint; + dp->initial_data_populated = POPULATED; + } + break; + case DTOP_INT: + sscanf(str, "%d", &(dp->data.d_int)); + if (dp->initial_data_populated == NOT_POPULATED) { + dp->initial_data.d_int = dp->data.d_int; + dp->initial_data_populated = POPULATED; + } + break; + case DTOP_UCHAR: + sscanf(str, "%c", &(dp->data.d_uchar)); + if (dp->initial_data_populated == NOT_POPULATED) { + dp->initial_data.d_uchar = dp->data.d_uchar; + dp->initial_data_populated = POPULATED; + } + break; + case DTOP_CHAR: + sscanf(str, "%c", &(dp->data.d_char)); + if (dp->initial_data_populated == NOT_POPULATED) { + dp->initial_data.d_char = dp->data.d_char; + dp->initial_data_populated = POPULATED; + } + break; + case DTOP_STR: + sscanf(str, "%s", dp->data.d_str); + if (dp->initial_data_populated == NOT_POPULATED) { + memcpy(dp->initial_data.d_str, dp->data.d_str, + DTOP_DP_MAX_STR_LEN); + dp->initial_data_populated = POPULATED; + } + break; + default: + break; + } +} + +/** + * @brief Responsible for calculating and printing current time to file. + * + * Prints the time since 1970, in Seconds and Milliseconds. + * + * @param fw File that time is printed to. + * @return FILE_ERROR - Writing to file was unsuccessful. + * @return FILE_SUCCESS - Writing to file was successful. + */ +int dtop_print_time_at_poll(FILE *fw) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + + if (fprintf(fw, "%10ld", tv.tv_sec) < 0) + return FILE_ERROR; + + if (fprintf(fw, ".%06ld,", tv.tv_usec) < 0) + return FILE_ERROR; + + return FILE_SUCCESS; +} + +/** + * @brief Polls all dp values and updates each value. + * + * @param dpg_list Pointer to first node of linked list which contains all dpgs. + */ +void dtop_poll(struct dtop_linked_list *dpg_list) +{ + struct dtop_linked_list *curr_ptr = dpg_list; + struct dtop_data_point_gatherer *dpset; + + while (curr_ptr) { + dpset = (struct dtop_data_point_gatherer *) curr_ptr->data; + dpset->poll(dpset); + curr_ptr = curr_ptr->next_ptr; + } +} + +/** + * @brief Prints the delta(value) of all data_points to terminal. + * + * @param dpg_list Pointer to first node of linked list which contains all dpgs. + */ +void dtop_print_snapshot_diff(struct dtop_linked_list *dpg_list) +{ + int i; + + struct dtop_linked_list *curr_ptr = dpg_list; + struct dtop_data_point_gatherer *dpset; + printf("\n"); + printf("Change In Datapoint Values\n"); + printf("---------------------------\n"); + while (curr_ptr) { + dpset = (struct dtop_data_point_gatherer *) curr_ptr->data; + for (i = 0; i < dpset->data_points_len; i++) + dtop_handle_dp_type_for_snapshot(dpset, + dpset->data_points[i]); + curr_ptr = curr_ptr->next_ptr; + } + printf("\n"); +} + +/** + * @brief Resets the initial values of all data_points. + * + * @param dpg_list Pointer to first node of linked list which contains all dpgs. + */ +void dtop_reset_dp_initial_values(struct dtop_linked_list *dpg_list) +{ + int i; + + struct dtop_linked_list *curr_ptr = dpg_list; + struct dtop_data_point_gatherer *dpset; + + while (curr_ptr) { + dpset = (struct dtop_data_point_gatherer *) curr_ptr->data; + for (i = 0; i < dpset->data_points_len; i++) + dpset->data_points[i].initial_data_populated + = NOT_POPULATED; + curr_ptr = curr_ptr->next_ptr; + } +} + +/** + * @brief Calls deconstructor method for all dpgs dynamically created. + * + * Checks to see if each dpg created has a deconstructor method. If not null, + * function calls the appropiate deconstructor method to deallocate memory. + * + * @param dpg_list Pointer to first node of linked list which contains all dpgs. + */ +void deconstruct_dpgs(struct dtop_linked_list *dpg_list) +{ + struct dtop_linked_list *curr_ptr = dpg_list; + struct dtop_data_point_gatherer *dpset; + + while (curr_ptr) { + dpset = (struct dtop_data_point_gatherer *) curr_ptr->data; + if (dpset->deconstruct) + dpset->deconstruct(dpset); + curr_ptr = curr_ptr->next_ptr; + } +} diff --git a/datatop/src/datatop_interface.h b/datatop/src/datatop_interface.h new file mode 100644 index 0000000..b3a3966 --- /dev/null +++ b/datatop/src/datatop_interface.h @@ -0,0 +1,159 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_interface.h + * @brief Declares functions held within datatop.c and datatop_helpers.c + * + * Declares functions which are held within datatop.c and datatop_helpers.c. + * Also defines data structures used for storing data gathered during polling + * such as datapoint names, values, and prefixes along with other valuable + * information. + */ + +#ifndef DATATOP_INTERFACE_H +#define DATATOP_INTERFACE_H + +#include <inttypes.h> +#include "datatop_linked_list.h" + +#define DTOP_ULONG 0 +#define DTOP_LONG 1 +#define DTOP_UINT 2 +#define DTOP_INT 3 +#define DTOP_UCHAR 4 +#define DTOP_CHAR 5 +#define DTOP_STR 6 + +#define FILE_ERROR -1 +#define FILE_SUCCESS 0 +#define SKIP 1 +#define DO_NOT_SKIP 0 +#define POPULATED 1 +#define NOT_POPULATED 0 + +#define DTOP_POLL_OK 0 +#define DTOP_POLL_IO_ERR 1 +#define NOT_CHECKED 0 + +#define QUIT 1 + +#define DTOP_DP_MAX_STR_LEN 32 + +#define DTOP_DP_HFILL .initial_data_populated = NOT_POPULATED, \ + .skip = 0 + +/** + * @struct dtop_data_union + * @brief Provides the type for dp value. + */ +union dtop_data_union { + uint64_t d_ulong; + int64_t d_long; + uint32_t d_uint; + int32_t d_int; + uint8_t d_uchar; + int8_t d_char; + char d_str[DTOP_DP_MAX_STR_LEN]; +}; + +/** + * @struct dtop_data_point + * @brief Individual datapoint in a file. + * + * @var dtop_data_point::name + * Stores the datapoints name. + * @var dtop_data_point::prefix + * Stores the individual prefix for the dp. + * @var dtop_data_point::type + * Type dp value is, see definitions. + * @var dtop_data_point::initial_data + * Holds the initial value of the dp the first time it was polled. + * @var dtop_data_point::initial_data_populated + * Variable that is changed when initial_data is populated. + * @var dtop_data_point::data + * Value of the dp at the most recent poll. + */ +struct dtop_data_point { + char *name; + char *prefix; + + /* Results of polling */ + char type; + union dtop_data_union initial_data; + char initial_data_populated; + union dtop_data_union data; + + /* Skip on subsequent polls */ + char skip; +}; + +/** + * @struct dtop_data_point_gatherer + * @brief Struct used to hold data about a set of collected data. + * + * @var dtop_data_point_gatherer::prefix + * Name of directory which data is collected from. + * @var dtop_data_point_gatherer::file + * File path that data is collected from. + * @var dtop_data_point_gatherer::poll + * Poll function takes a dtop_data_point_gatherer as parameter. + * int equals, DTOP_POLL_IO_ERR - Poll of dpg unsuccessful, or + * DTOP_POLL_OK - Poll of dpg successful. + * @var dtop_data_point_gatherer::data_points + * Pointer to a dtop_data_point struct (dp). + * @var dtop_data_point_gatherer::data_points_len + * Number of elements in the array of dp's the dpg accesses. + */ +struct dtop_data_point_gatherer { + char *prefix; + char *file; + int (*poll)(struct dtop_data_point_gatherer *dpg); + void (*deconstruct)(struct dtop_data_point_gatherer *dpg); + + struct dtop_data_point *data_points; + int data_points_len; + + /* Private data */ + void *priv; +}; + +void dtop_register(struct dtop_data_point_gatherer *dpg); +void dtop_store_dp(struct dtop_data_point *dp, const char *str); +void dtop_print_dpg(struct dtop_data_point_gatherer *dpg); +void get_snapshot_diff(struct dtop_linked_list *dpg_list); +void dtop_print_snapshot_diff(struct dtop_linked_list *dpg_list); +void dtop_poll(struct dtop_linked_list *dpg_list); +int dtop_print_time_at_poll(FILE *fw); +int dtop_print_dpg_names_csv(struct dtop_data_point_gatherer *dpg, FILE *fw); +int dtop_write_pollingdata_csv(struct dtop_linked_list *dpg_list, FILE *fw); +void dtop_reset_dp_initial_values(struct dtop_linked_list *dpg_list); +void deconstruct_dpgs(struct dtop_linked_list *dpg_list); +int dtop_print_system_snapshot(char *file); +#endif /* DATATOP_INTERFACE_H */ diff --git a/datatop/src/datatop_linked_list.c b/datatop/src/datatop_linked_list.c new file mode 100644 index 0000000..7e5b632 --- /dev/null +++ b/datatop/src/datatop_linked_list.c @@ -0,0 +1,84 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_linked_list.c + * @brief Necessary linked_list functions created. + * + * Holds function which adds to or creates a linked list + * used for storing dtop_data_point_gatherer's (dpg's). + * Datapoints are stored in linked list for ability to + * iteratively poll and print efficiently. Handles creation + * and deletion of memory for linked list nodes. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#include "datatop_linked_list.h" + +/** + * @brief Adds a node to the beginning of a linked list. + * + * @param data A void pointer which can hold any data in the node. + * @param list The list that is added to. + * @return Updated linked list struct. + */ +struct dtop_linked_list *dtop_add_linked_list(void *data, + struct dtop_linked_list *list) +{ + struct dtop_linked_list *list_node; + list_node = malloc(sizeof(struct dtop_linked_list)); + + if (!list_node) { + fprintf(stderr, "failed to allocate memory.\n"); + exit(EXIT_FAILURE); + } + + list_node->data = data; + list_node->next_ptr = list; + list = list_node; + return list; +} + +/** + * @brief Deletes a linked list. + * + * @param head_ptr Pointer to the first node in the linked list that is to be deleted. + */ +void dtop_rem_linked_list(struct dtop_linked_list *head_ptr) +{ + struct dtop_linked_list *tmp_ptr = NULL; + while (head_ptr) { + tmp_ptr = head_ptr; + head_ptr = head_ptr->next_ptr; + free(tmp_ptr); + } +} diff --git a/datatop/src/datatop_linked_list.h b/datatop/src/datatop_linked_list.h new file mode 100644 index 0000000..4386d02 --- /dev/null +++ b/datatop/src/datatop_linked_list.h @@ -0,0 +1,59 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_linked_list.h + * @brief Defines linked list struct and declares linked list methods. + * + * Defines linked list struct which can be used for any data + * storage. Declares methods held within datatop_linked_list.c. + */ + +#ifndef DATATOP_LINKED_LIST_H +#define DATATOP_LINKED_LIST_H + +/** + * @struct dtop_linked_list + * @brief Struct used to represent linked list node that stores a pointer with any type. + * + * @var dtop_linked_list::next_ptr + * Pointer to next node in the list. + * @var dtop_linked_list::data + * Pointer to data the node stores. + */ +struct dtop_linked_list { + struct dtop_linked_list *next_ptr; + void *data; +}; + +struct dtop_linked_list *dtop_add_linked_list(void *data, + struct dtop_linked_list *list); +void dtop_rem_linked_list(struct dtop_linked_list *head_ptr); +#endif /* DATATOP_LINKED_LIST_H */ + diff --git a/datatop/src/datatop_meminfo_file_poll.c b/datatop/src/datatop_meminfo_file_poll.c new file mode 100644 index 0000000..bef1c5d --- /dev/null +++ b/datatop/src/datatop_meminfo_file_poll.c @@ -0,0 +1,280 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_meminfo_file_poll.c + * @brief Adds ability for data collection from /proc/meminfo + * + * File contains methods for searching and polling data from + * "/proc/meminfo" + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "datatop_interface.h" +#include "datatop_fileops.h" +#include "datatop_str.h" + +#define DTOP_MEM_SIZE 8192 +#define DTOP_MEM_LINE (DTOP_MEM_SIZE>>2) + +/** +* @struct dtop_meminfo_vars +* @brief Struct used to hold necessary variables for /proc/meminfo dpg +* +* @var dtop_meminfo_vars::line +* Array of strings where necessary dp names and values are held. +* @var dtop_meminfo_vars::line_count +* Number of lines the file is that the dpg represents. +*/ +struct dtop_meminfo_vars { + char **line; + int line_count; +}; + +/** + * @brief Parses lines with data in "/proc/meminfo" + * + * @param line1 Line to parse to find datapoint names and values. + * @param len1 Length of line1. + * @param l Index in the dictionary the key/value pair is added to. + * @param dict Dictionary the keys and values are added to. + */ +int dt_meminfo_parse(char *line1, int len1, + int l, struct dt_procdict *dict) +{ + int i, k, n; + if (len1 < 1) + return 0; + + if (line1 == 0 || dict == 0) + return 0; + + k = l; + dict->key[k] = &line1[0]; + for (i = 0; i < len1 && k < DTOP_DICT_SIZE; i++) { + if (line1[i] == ' ' || line1[i] == ' ') { + line1[i] = 0; + n = i+1; + while (line1[n] == ' ' || line1[n] == ' ') + n++; + dict->val[k] = &line1[n]; + while (line1[n] != ' ') + n++; + line1[n] = 0; + break; + } + } + k++; + dict->max = k; + return k; +} + +/** + * @brief Stores the data collected from a "/proc/meminfo" + * + * @param dpg Struct that polled data is added to. + * @return DTOP_POLL_IO_ERR - Poll of dpg unsuccessful. + * @return DTOP_POLL_OK - Poll of dpg successful. + */ +int dtop_meminfo_poll(struct dtop_data_point_gatherer *dpg) +{ + char *data; + int *line_len = malloc(sizeof(int) * + ((struct dtop_meminfo_vars *) + (dpg->priv))->line_count); + int read; + struct dt_procdict dict; + int i, j, n, sum; + + read = dt_read_file(dpg->file, &data, DTOP_MEM_SIZE); + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + sum = 0; + /* Assigns each line read from the file, a length */ + for (n = 0; n < ((struct dtop_meminfo_vars *) + (dpg->priv))->line_count; n++) { + line_len[n] = dt_read_line(((struct dtop_meminfo_vars *) + (dpg->priv))->line[n], + DTOP_MEM_LINE, data, + DTOP_MEM_SIZE, sum); + if (n <= (((struct dtop_meminfo_vars *) + (dpg->priv))->line_count - 1)) { + sum += (line_len[n] + 1); + } + + } + + /* Stores dp names and values in dictionary */ + for (i = 0; i < dpg->data_points_len; i++) + dt_meminfo_parse(((struct dtop_meminfo_vars *) + (dpg->priv))->line[i], line_len[i], i, &dict); + + /* Assigns the dp value to the dp struct */ + for (j = 0; j < dpg->data_points_len; j++) { + i = dt_find_dict_idx(dpg->data_points[j].name, &dict); + if (i >= 0 && i < dict.max) { + sscanf(dict.val[i], "%lu", + &(dpg->data_points[i].data.d_ulong)); + dpg->data_points[i].data.d_ulong *= 1024; + if (dpg->data_points[i]. + initial_data_populated == NOT_POPULATED) { + dpg->data_points[i].initial_data.d_ulong + = dpg->data_points[i].data.d_ulong; + dpg->data_points[i].initial_data_populated + = POPULATED; + } + } + } + + dt_free(&data); + free(line_len); + return DTOP_POLL_OK; +} + +/** + * @brief Frees dynamically allocated "/proc/meminfo" dpg. + * + * Frees the memory of the dpg along with it's data_points + * and other malloc'd memory no longer needed. + * + * @param dpg Dpg to deconstruct and deallocate memory for. + */ +static void dtop_meminfo_dpg_deconstructor + (struct dtop_data_point_gatherer *dpset) +{ + int i; + free(dpset->data_points); + for (i = 0; i < ((struct dtop_meminfo_vars *) + (dpset->priv))->line_count; i++) + free(((struct dtop_meminfo_vars *)(dpset->priv))->line[i]); + free(((struct dtop_meminfo_vars *)(dpset->priv))->line); + free(((struct dtop_meminfo_vars *)(dpset->priv))); + free(dpset); +} + +/** + * @brief Creates a dpg for "/proc/meminfo" file + * + * Dynamically allocates memory for dpg which is then added to a linked list + * via the dtop_register(dpg) function call. + * + * @param data_points dtop_data_point struct that dpg points to. + * @param storage dtop_meminfo_vars struct that holds relevant dpg variables. + */ +static void construct_meminfo_file_dpg(struct dtop_data_point + *data_points, struct dtop_meminfo_vars *storage) +{ + struct dtop_data_point_gatherer *dpg = malloc + (sizeof(struct dtop_data_point_gatherer)); + dpg->prefix = "/proc/meminfo"; + dpg->file = "/proc/meminfo"; + dpg->poll = dtop_meminfo_poll; + dpg->data_points = data_points; + dpg->priv = (struct dtop_meminfo_vars *)storage; + dpg->data_points_len = storage->line_count; + dpg->deconstruct = dtop_meminfo_dpg_deconstructor; + + dtop_register(dpg); +} + +/** + * @brief Scans "/proc/meminfo in order to autodetect dps. + * + * Searches through "/proc/meminfo" file for all available data + * points to create as dp structs. + * + * @param storage dtop_meminfo_vars struct where relevant variables are stored. + */ +int dtop_meminfo_search(struct dtop_meminfo_vars *storage) +{ + int i, k, n, sum; + char *data; + int *line_len = malloc(sizeof(int) * storage->line_count); + int read; + struct dt_procdict dict; + struct dtop_data_point *data_points; + + storage->line = malloc(storage->line_count * sizeof(*storage->line)); + + for (i = 0; i < storage->line_count; i++) + storage->line[i] = malloc(sizeof(char) * DTOP_MEM_LINE); + + read = dt_read_file("/proc/meminfo", &data, DTOP_MEM_SIZE); + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + sum = 0; + /* Assigns each line read from the file, a length */ + for (n = 0; n < storage->line_count; n++) { + line_len[n] = dt_read_line(storage->line[n], + DTOP_MEM_LINE, data, + DTOP_MEM_SIZE, sum); + if (n < (storage->line_count - 1)) + sum += (line_len[n] + 1); + } + + /* Stores dp names in dictionary */ + for (i = 0; i < (storage->line_count); i++) + dt_parse_proc_same_line_key_and_val(storage->line[i], + line_len[i], i, &dict); + + data_points = malloc + (storage->line_count * sizeof(struct dtop_data_point)); + + k = 0; + /* Creates a dtop_data_point struct for each dp found in the file */ + for (i = 0; i < dict.max; i++) { + data_points[i].name = dict.key[i]; + data_points[i].prefix = NULL; + data_points[i].type = DTOP_ULONG; + k++; + } + + /* Calls dpg constructor, dpg will point to the dp struct */ + construct_meminfo_file_dpg(data_points, storage); + + free(line_len); + dt_free(&data); + + return DTOP_POLL_OK; +} + +/** + * @brief Calls dtop_search for "/proc/meminfo" file. + */ +void dtop_meminfo_init(void) +{ + struct dtop_meminfo_vars *storage = malloc + (sizeof(struct dtop_meminfo_vars)); + storage->line_count = dtop_get_file_line_amount("/proc/meminfo"); + dtop_meminfo_search(storage); +} diff --git a/datatop/src/datatop_opt.c b/datatop/src/datatop_opt.c new file mode 100644 index 0000000..c2bb366 --- /dev/null +++ b/datatop/src/datatop_opt.c @@ -0,0 +1,184 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_opt.c + * @brief Adds getopt functionality for CLI commands. + * + * Contains method for getopt functionality used for parsing + * the CLI arguments into executable commands. Handles + * errors which arise when parsing. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <ctype.h> +#include <getopt.h> +#include "datatop_opt.h" +#include "datatop_interface.h" +#include "datatop_linked_list.h" +#include "datatop_fileops.h" + +/** + * @brief Populate the comand line options with sane defaults + * + * @param clopts Struct used to hold data regarding CLI arguments. + */ +void dtop_load_default_options(struct cli_opts *clopts) +{ + memset(clopts, 0, sizeof(struct cli_opts)); + + clopts->priority = DEFAULT_NICE; +} + +/** + * @brief Parses all CLI commands for main() to execute. + * + * @param clopts Struct used to hold data regarding CLI arguments. + * @param argc Parameter used to read CLI commands from. + * @param argv Parameter used to read CLI arguments from. + * @return PARSE_SUCCESS - CLI arguments read successfully, + * @return PARSE_FAILURE - CLI arguments and/or input not valid. + * @return PARSE_FORCE_EXIT - Exit immediately, print help options. + */ +int dtop_parse_cli_opts(struct cli_opts *clopts, int argc, char **argv) +{ + int option; + + if (!clopts || !*argv) { + printf("Internal Error: Null Pointer\n"); + goto error; + } + + while ((option = getopt(argc, argv, "phi:t:w:s:n:")) != -1) { + switch (option) { + case 'p': + clopts->print_cl = OPT_CHOSE; + break; + + case 'h': + dtop_print_help_opts(); + return PARSE_FORCE_EXIT; + break; + + case 'n': + clopts->priority = strtol(optarg, 0, 10); + if (clopts->priority > 19 || clopts->priority < -20) { + printf("Argument for -n is not valid. "); + printf("Must be between -20 and 19.\n"); + goto error; + } + break; + + case 'i': + clopts->poll_per = strtol(optarg, 0, 10); + if (clopts->poll_per <= 0) { + printf("Argument for -i is not valid. "); + printf("Must be positive integer.\n"); + goto error; + } + break; + + case 't': + clopts->poll_time = strtol(optarg, 0, 10); + clopts->poll_time_selected = POLL_TIME_SELECTED; + if (clopts->poll_time <= 0) { + printf("Argument for -t is not valid. "); + printf("Must be positive integer.\n"); + goto error; + } + break; + + case 'w': + if (dtop_check_writefile_access(optarg) == VALID) { + clopts->file_name = optarg; + clopts->print_csv = OPT_CHOSE; + } else { + goto error; + } + break; + + case 's': + if (dtop_check_writefile_access(optarg) == VALID) + clopts->snapshot_file = optarg; + else + goto error; + break; + + case '?': + default: + goto error; + } + } + + if (clopts->poll_time == 0) { + if (clopts->print_csv == 1) + clopts->poll_time = POLL_NOT_SPECIFIED; + else + clopts->poll_time = POLL_TIME_DEFAULT; + } + if (clopts->poll_per == 0) + clopts->poll_per = DEFAULT_POLL_INTERVAL; + + return PARSE_SUCCESS; + +error: + printf("See datatop -h for help\n"); + return PARSE_FAILURE; +} + +/** + * @brief Prints the options the user has for the program to terminal. + */ +void dtop_print_help_opts(void) +{ + printf("The following datatop commands are:\n"); + printf("\t-p\t\t\tPrint output to terminal\n"); + printf("\t-i , seconds\t\tSpecify polling period\n"); + printf("\t-t , seconds\t\tSpecify polling duration\n"); + printf("\t-w , file name (.csv)\tWrite output to a file\n"); + printf("\t-s , file name\t\tPrint system snapshot to a file\n"); + printf("\t-n , nice value\t\tSet niceness (default 19)\n"); + printf("\t-h\t\t\tGet help\n"); +} + + +/** +* @brief Prints the interactive options the user can enter during runtime. +*/ +void dtop_print_interactive_opts(void) +{ + printf("The following interactive commands are:\n"); + printf("\tq | quit\tTerminate program at any time\n"); + printf("\ti\t\tPrint dp differences, reset initial dp values\n"); + printf("\tl\t\tPrint dp differences since last reset\n"); + printf("\n"); +} diff --git a/datatop/src/datatop_opt.h b/datatop/src/datatop_opt.h new file mode 100644 index 0000000..8f2d855 --- /dev/null +++ b/datatop/src/datatop_opt.h @@ -0,0 +1,88 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_opt.h + * @brief Declares methods and defines struct used within datatop_opt.c + * + * Struct defined is used when parsing the CLI arguments. + */ + +#ifndef DATATOP_OPT_H +#define DATATOP_OPT_H + +#define OPT_CHOSE 1 +#define OPT_NOT_CHOSE 0 +#define DEFAULT_POLL_INTERVAL 1 +#define POLL_NOT_SPECIFIED -1 +#define POLL_TIME_DEFAULT 30 +#define POLL_TIME_SELECTED 1 +#define PARSE_SUCCESS 0 +#define PARSE_FAILURE -1 +#define PARSE_FORCE_EXIT -2 +#define VALID 0 +#define INVALID -1 +#define DEFAULT_NICE 19 /* Lowest priority */ + +/** + * @struct cli_opts + * @brief Struct used to store arguments from CLI input. + * + * @var cli_opts::print_cl + * Represents -p argument. + * @var cli_opts::poll_per + * Polling frequency argument. + * @var cli_opts::poll_time + * Polling duration argument. + * @var cli_opts::cli_help + * Represents -h argument. + * @var cli_opts::file_name + * File name argument. + * @var cli_opts::print_csv + * Represents -w argument. + */ +struct cli_opts { + int print_cl; /* -p option */ + long int poll_per; /* -i option */ + long int poll_time; /* -t option */ + int cli_help; /* -h option */ + char *file_name; /* -w option */ + char *snapshot_file; /* -s option */ + int print_csv; + int poll_time_selected; + int priority; /* -n option (niceness) */ +}; + +int dtop_parse_cli_opts(struct cli_opts *clopts, int argc, char **argv); +void dtop_print_help_opts(void); +void dtop_print_interactive_opts(void); +void dtop_load_default_options(struct cli_opts *clopts); + +#endif /* DATATOP_OPT_H */ + diff --git a/datatop/src/datatop_polling.h b/datatop/src/datatop_polling.h new file mode 100644 index 0000000..d917474 --- /dev/null +++ b/datatop/src/datatop_polling.h @@ -0,0 +1,49 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_dual_line_poll.h + * @brief Declares methods held in datatop_dual_line_poll.c + */ + +#ifndef DATATOP_DUAL_LINE_POLL_H +#define DATATOP_DUAL_LINE_POLL_H + +void dtop_dual_line_init(char *name); +void dtop_single_line_init(char *name); +void dtop_value_only_init(char *name); +void dtop_meminfo_init(void); +void dtop_dev_init(void); +void dtop_stat_init(void); +void dtop_cpu_stats_init(void); +int dtop_value_only_poll(struct dtop_data_point_gatherer *dpg); +void dtop_value_only_dpg_deconstructor + (struct dtop_data_point_gatherer *dpset); + +#endif /* DATATOP_DUAL_LINE_POLL_H */ diff --git a/datatop/src/datatop_single_line_poll.c b/datatop/src/datatop_single_line_poll.c new file mode 100644 index 0000000..a43036f --- /dev/null +++ b/datatop/src/datatop_single_line_poll.c @@ -0,0 +1,243 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_single_line_poll.c + * @brief Adds ability for data collection from single line files + * + * File contains methods for searching and polling data from + * single line files, meaning a file with multiple lines, but each + * line contains the name of the dp, followed by the value. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "datatop_interface.h" +#include "datatop_fileops.h" +#include "datatop_str.h" + +#define DTOP_SINGLE_SIZE 8192 +#define DTOP_SINGLE_LINE (DTOP_SINGLE_SIZE>>2) + +/** +* @struct dtop_single_line_vars +* @brief Struct used to hold necessary variables for dual_line_file dpgs. +* +* @var dtop_single_line_vars::line +* Array of strings where necessary dp names and values are held. +* @var dtop_single_line_vars::line_count +* Number of lines the file is that the dpg represents. +*/ +struct dtop_single_line_vars { + char **line; + int line_count; +}; + +/** + * @brief Stores the data collected from a single_line files. + * + * @param dpg Struct that polled data is added to. + * @return DTOP_POLL_IO_ERR - Poll of dpg unsuccessful. + * @return DTOP_POLL_OK - Poll of dpg successful. + */ +int dtop_single_line_poll(struct dtop_data_point_gatherer *dpg) +{ + char *data; + int *line_len = malloc(sizeof(int) * + ((struct dtop_single_line_vars *) + (dpg->priv))->line_count); + int read; + struct dt_procdict dict; + int i, j, n, sum; + + read = dt_read_file(dpg->file, &data, DTOP_SINGLE_SIZE); + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + sum = 0; + /* Assigns each line read from the file, a length */ + for (n = 0; n < ((struct dtop_single_line_vars *) + (dpg->priv))->line_count; n++) { + line_len[n] = dt_read_line(((struct dtop_single_line_vars *) + (dpg->priv))->line[n], + DTOP_SINGLE_LINE, data, + DTOP_SINGLE_SIZE, sum); + if (n <= (((struct dtop_single_line_vars *) + (dpg->priv))->line_count - 1)) { + sum += (line_len[n] + 1); + } + + } + + /* Stores dp names and values in dictionary */ + for (i = 0; i < dpg->data_points_len; i++) + dt_parse_proc_same_line_key_and_val(( + (struct dtop_single_line_vars *) + (dpg->priv))->line[i], line_len[i], i, &dict); + + /* Assigns the dp value to the dp struct */ + for (j = 0; j < dpg->data_points_len; j++) { + i = dt_find_dict_idx(dpg->data_points[j].name, &dict); + if (i >= 0 && i < dict.max) + dtop_store_dp(&(dpg->data_points[j]), + dict.val[i]); + } + + dt_free(&data); + free(line_len); + return DTOP_POLL_OK; +} + +/** + * @brief Frees dynamically allocated single_line_file dpgs. + * + * Frees the memory of the dpg along with it's data_points + * and other malloc'd memory no longer needed. + * + * @param dpg Dpg to deconstruct and deallocate memory for. + */ +static void dtop_single_line_dpg_deconstructor + (struct dtop_data_point_gatherer *dpset) +{ + int i; + free(dpset->data_points); + for (i = 0; i < ((struct dtop_single_line_vars *) + (dpset->priv))->line_count; i++) + free(((struct dtop_single_line_vars *)(dpset->priv))->line[i]); + free(((struct dtop_single_line_vars *)(dpset->priv))->line); + free(((struct dtop_single_line_vars *)(dpset->priv))); + free(dpset); +} + +/** + * @brief Creates a dpg for a single_line file. + * + * Dynamically allocates memory for dpg which is then added to a linked list + * via the dtop_register(dpg) function call. + * + * @param name Name of file dpg represents. + * @param data_points dtop_data_point struct that dpg points to. + * @param storage dtop_single_line_vars struct that holds relevant dpg variables. + */ +static void construct_single_line_file_dpg(char *name, struct dtop_data_point + *data_points, struct dtop_single_line_vars *storage) +{ + struct dtop_data_point_gatherer *dpg = malloc + (sizeof(struct dtop_data_point_gatherer)); + dpg->prefix = name; + dpg->file = name; + dpg->poll = dtop_single_line_poll; + dpg->data_points = data_points; + dpg->priv = (struct dtop_single_line_vars *)storage; + dpg->data_points_len = storage->line_count; + dpg->deconstruct = dtop_single_line_dpg_deconstructor; + + dtop_register(dpg); +} + +/** + * @brief Scans a single_line file for all datapoints and creats dps. + * + * Searches through a single_line file (Key followed by value on the + * same line) for all available data points to create as dp structs. + * + * @param name Name of file. + * @param storage dtop_single_line_vars struct where relevant variables are stored. + */ +int dtop_single_line_search(char *name, struct dtop_single_line_vars *storage) +{ + int i, k, n, sum; + char *data; + int *line_len = malloc(sizeof(int) * storage->line_count); + int read; + struct dt_procdict dict; + struct dtop_data_point *data_points; + + storage->line = malloc(storage->line_count * sizeof(*storage->line)); + + for (i = 0; i < storage->line_count; i++) + storage->line[i] = malloc(sizeof(char) * DTOP_SINGLE_LINE); + + read = dt_read_file(name, &data, DTOP_SINGLE_SIZE); + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + sum = 0; + /* Assigns each line read from the file, a length */ + for (n = 0; n < storage->line_count; n++) { + line_len[n] = dt_read_line(storage->line[n], + DTOP_SINGLE_LINE, data, + DTOP_SINGLE_SIZE, sum); + if (n < (storage->line_count - 1)) + sum += (line_len[n] + 1); + } + + /* Stores dp names and values in dictionary */ + for (i = 0; i < (storage->line_count); i++) + dt_parse_proc_same_line_key_and_val(storage->line[i], + line_len[i], i, &dict); + + data_points = malloc + (storage->line_count * sizeof(struct dtop_data_point)); + + k = 0; + /* Creates a dtop_data_point struct for each dp found in the file */ + for (i = 0; i < dict.max; i++) { + if (dict.val[i][0] == '-') + data_points[k].type = DTOP_LONG; + else + data_points[k].type = DTOP_ULONG; + data_points[i].name = dict.key[i]; + data_points[i].prefix = NULL; + k++; + } + + /* Calls dpg constructor, dpg will point to the dp struct */ + construct_single_line_file_dpg(name, data_points, storage); + + free(line_len); + dt_free(&data); + + return DTOP_POLL_OK; +} + +/** + * @brief Calls dtop_search for a files with single line format. + * + * Single line format refers to a file, where each line contains + * the name of a dp, followed by the value of a dp. + */ +void dtop_single_line_init(char *name) +{ + struct dtop_single_line_vars *storage = malloc + (sizeof(struct dtop_single_line_vars)); + storage->line_count = dtop_get_file_line_amount(name); + dtop_single_line_search(name, storage); +} diff --git a/datatop/src/datatop_stat_poll.c b/datatop/src/datatop_stat_poll.c new file mode 100644 index 0000000..3eadaeb --- /dev/null +++ b/datatop/src/datatop_stat_poll.c @@ -0,0 +1,320 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_stat_poll.c + * @brief Adds ability for data collection from /proc/stat + * + * File contains methods for searching and polling data from + * "/proc/stat" + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "datatop_interface.h" +#include "datatop_fileops.h" +#include "datatop_str.h" + +#define DTOP_STAT_SIZE 16384 +#define DTOP_STAT_LINE (DTOP_STAT_SIZE>>2) + +/** +* @struct dtop_stat_vars +* @brief Struct used to hold necessary variables for /proc/stat dpg +* +* @var dtop_stat_vars::line +* Array of strings where necessary dp names and values are held. +* @var dtop_stat_vars::line_count +* Number of lines the file is that the dpg represents. +*/ +struct dtop_stat_vars { + char **line; + int line_count; +}; + +/** + * @brief Parses lines with data in "/proc/stat" + * + * @param line1 Line to parse to find datapoint names and values. + * @param len1 Length of line1. + * @param n_index Index in the dictionary the key (name) is added to. + * @param v_index Index in the dictionary the value is added to. + * @param dict Dictionary the keys and values are added to. + */ +static int dt_stat_parse(char *line1, int len1, + int n_index, int v_index, struct dt_procdict *dict) +{ + int i, k, j, start = 0; + if (len1 < 1) + return 0; + + if (line1 == 0 || dict == 0) + return 0; + + dict->key[n_index] = &line1[0]; + for (i = 0; i < len1; i++) { + if (line1[i] == ' ') { + line1[i] = 0; + start = (i+1); + break; + } + } + + k = v_index; + for (i = start; i < len1 && k < DTOP_DICT_SIZE; i++) { + if (line1[i] != ' ') { + dict->val[k] = &line1[i]; + for (j = i; j < len1; j++) { + if (line1[j] == ' ') { + line1[j] = 0; + break; + } + } + i = j; + k++; + } + } + + dict->max = k; + return k; +} + +/** + * @brief Stores the data collected from "/proc/stat" + * + * @param dpg Struct that polled data is added to. + * @return DTOP_POLL_IO_ERR - Poll of dpg unsuccessful. + * @return DTOP_POLL_OK - Poll of dpg successful. + */ +int dtop_stat_poll(struct dtop_data_point_gatherer *dpg) +{ + char *data; + int *line_len = malloc(sizeof(int) * + ((struct dtop_stat_vars *) + (dpg->priv))->line_count); + int read; + struct dt_procdict dict; + int i, n, sum; + int dp_count = 0; + + read = dt_read_file(dpg->file, &data, DTOP_STAT_SIZE); + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + sum = 0; + /* Assigns each line read from the file, a length */ + for (n = 0; n < ((struct dtop_stat_vars *) + (dpg->priv))->line_count; n++) { + line_len[n] = dt_read_line(((struct dtop_stat_vars *) + (dpg->priv))->line[n], + DTOP_STAT_LINE, data, + DTOP_STAT_SIZE, sum); + if (n <= (((struct dtop_stat_vars *) + (dpg->priv))->line_count - 1)) { + sum += (line_len[n] + 1); + } + + } + + /* Stores dp names and values in dictionary */ + for (i = 0; i < ((struct dtop_stat_vars *)(dpg->priv))->line_count; i++) + dp_count = dt_stat_parse(((struct dtop_stat_vars *) + (dpg->priv))->line[i], line_len[i], i, dp_count, &dict); + + /* Assigns the dp value to the dp struct */ + for (n = 0; n < dp_count; n++) { + dtop_store_dp(&(dpg->data_points[n]), + dict.val[n]); + } + + dt_free(&data); + free(line_len); + return DTOP_POLL_OK; +} + +/** + * @brief Frees dynamically allocated "/proc/stat" dpg. + * + * Frees the memory of the dpg along with it's data_points + * and other malloc'd memory no longer needed. + * + * @param dpg Dpg to deconstruct and deallocate memory for. + */ +static void dtop_stat_dpg_deconstructor + (struct dtop_data_point_gatherer *dpset) +{ + int i; + for (i = 0; i < dpset->data_points_len; i++) + free(dpset->data_points[i].name); + free(dpset->data_points); + for (i = 0; i < ((struct dtop_stat_vars *) + (dpset->priv))->line_count; i++) + free(((struct dtop_stat_vars *)(dpset->priv))->line[i]); + free(((struct dtop_stat_vars *)(dpset->priv))->line); + free(((struct dtop_stat_vars *)(dpset->priv))); + + free(dpset); +} + +/** + * @brief Creates a dpg for "/proc/stat" file + * + * Dynamically allocates memory for dpg which is then added to a linked list + * via the dtop_register(dpg) function call. + * + * @param data_points dtop_data_point struct that dpg points to. + * @param storage dtop_stat_vars struct that holds relevant dpg variables. + * @param dp_count Number of data_points in data_points array + */ +static void construct_stat_file_dpg(struct dtop_data_point + *data_points, struct dtop_stat_vars *storage, int dp_count) +{ + struct dtop_data_point_gatherer *dpg = malloc + (sizeof(struct dtop_data_point_gatherer)); + dpg->prefix = "/proc/stat"; + dpg->file = "/proc/stat"; + dpg->poll = dtop_stat_poll; + dpg->data_points = data_points; + dpg->priv = (struct dtop_stat_vars *)storage; + dpg->data_points_len = dp_count; + dpg->deconstruct = dtop_stat_dpg_deconstructor; + + dtop_register(dpg); +} + +/** + * @brief Scans "/proc/stat" in order to autodetect dps. + * + * Searches through "/proc/stat" file for all available data + * points to create as dp structs. + * + * @param storage dtop_stat_vars struct where relevant variables are stored. + */ +int dtop_stat_search(struct dtop_stat_vars *storage) +{ + int i, k, n, sum; + char *data; + int *line_len = malloc(sizeof(int) * storage->line_count); + int read; + struct dt_procdict dict; + int dp_count = 0; + int end; + int *dp_per_line; + struct dtop_data_point *data_points; + int count = 0; + + storage->line = malloc(storage->line_count * sizeof(*storage->line)); + + for (i = 0; i < storage->line_count; i++) + storage->line[i] = malloc(sizeof(char) * DTOP_STAT_LINE); + + read = dt_read_file("/proc/stat", &data, DTOP_STAT_SIZE); + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + sum = 0; + /* Assigns each line read from the file, a length */ + for (n = 0; n < storage->line_count; n++) { + line_len[n] = dt_read_line(storage->line[n], + DTOP_STAT_LINE, data, + DTOP_STAT_SIZE, sum); + if (n < (storage->line_count - 1)) + sum += (line_len[n] + 1); + } + + dp_per_line = malloc(sizeof(int) * (storage->line_count)); + /* Stores dp names in dictionary */ + + for (i = 0; i < (storage->line_count); i++) { + end = dp_count; + dp_count = dt_stat_parse(storage->line[i], + line_len[i], i, dp_count, &dict); + dp_per_line[i] = (dp_count - end); + } + + data_points = malloc(dp_count * sizeof(struct dtop_data_point)); + + for (i = 0; i < (storage->line_count); i++) { + for (n = 0; n < dp_per_line[i]; n++) { + if (dp_per_line[i] == 1) { + int dk_len = strlen(dict.key[i]) + 1; + int dp_len; + char *newname = malloc(dk_len); + strlcpy(newname, dict.key[i], dk_len); + dp_len = strlen(newname) + 1; + data_points[count].name = malloc(dp_len); + strlcpy(data_points[count].name, newname, + dp_len); + free(newname); + } else { + char *add = malloc(15 * sizeof(char)); + char *newname; + int nn_len, dpn_len; + snprintf(add, 15 * sizeof(char), "[%d]:", n); + nn_len = strlen(dict.key[i]) + strlen(add) + 1; + newname = malloc(nn_len); + strlcpy(newname, dict.key[i], nn_len); + strlcat(newname, add, nn_len); + dpn_len = strlen(newname) + 1; + data_points[count].name = malloc(dpn_len); + strlcpy(data_points[count].name, newname, + dpn_len); + free(newname); + free(add); + } + data_points[count].prefix = NULL; + data_points[count].type = DTOP_ULONG; + data_points[count].initial_data_populated + = NOT_POPULATED; + data_points[count].skip = DO_NOT_SKIP; + count++; + } + } + k = 0; + + /* Calls dpg constructor, dpg will point to the dp struct */ + construct_stat_file_dpg(data_points, storage, dp_count); + free(dp_per_line); + free(line_len); + dt_free(&data); + + return DTOP_POLL_OK; +} + +/** + * @brief Calls dtop_search for "/proc/stat" file. + */ +void dtop_stat_init(void) +{ + struct dtop_stat_vars *storage = malloc + (sizeof(struct dtop_stat_vars)); + storage->line_count = dtop_get_file_line_amount("/proc/stat"); + dtop_stat_search(storage); +} diff --git a/datatop/src/datatop_str.c b/datatop/src/datatop_str.c new file mode 100644 index 0000000..3d25751 --- /dev/null +++ b/datatop/src/datatop_str.c @@ -0,0 +1,260 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_str.c + * @brief Algorithms used for storing and polling data created. + * + * Methods created which store collected data from files in + * dictionaries for many different file formats. + */ + +#include <stdio.h> +#include <string.h> +#include "datatop_str.h" + +/** @brief Reads an individual line from a file. + * + * Will read from buf2 until either a '\n' is reached, or the end of buf1 + * or buf2 is reached. The result is guaranteed to be null terminated. + * + * @param buf1 Destination buffer to store the read line. + * @param len1 Size of destination buffer. + * @param buf2 Source buffer to read lines from. Const, will not be + * modified by this function. + * @param len2 Size of the source buffer. + * @param start Offset (in bytes) to start reading from source buffer. + * @return Length of line (of chars). + */ +int dt_read_line(char *buf1, int len1, const char *buf2, int len2, int start) +{ + int i, j; + + if (len1 < 1 || len2 < 1 || start < 0 || start > len2) + return 0; + + if (buf1 == 0 || buf2 == 0) + return 0; + + i = 0; + j = start; + + while ((i < (len1-1)) && (j < len2)) { + buf1[i] = buf2[j]; + if (buf1[i] == '\n') + break; + i++; + j++; + } + buf1[i] = 0; + return i; +} + +/** + * @brief Parses files that have Names and Values on separate lines. + * + * Use this method to parse files that have names on one line, followed by + * the corresponding values on the next line. Such as "/proc/net/netstat" + * + * @param line1 First line that is parsed to store the datapoint names as keys. + * @param len1 Length of line1. + * @param line2 Second line that is parsed to store the datapoint values as dictionary values. + * @param len2 Length of line2. + * @param dict Dictionary that keys and values are added to. + * @return Number of key/val pairs in the dictionary. + */ +int dt_parse_proc_dictionary(char *line1, int len1, char *line2, + int len2, struct dt_procdict *dict) +{ + int i, j, k; + + if (len1 < 1 || len2 < 1) + return 0; + + if (line1 == 0 || line2 == 0 || dict == 0) + return 0; + + k = 0; + for (i = 0; i < len1 && k < DTOP_DICT_SIZE; i++) { + if (line1[i] == ' ') { + dict->key[k] = &line1[i+1]; + line1[i] = 0; + k++; + } + } + j = k; + + k = 0; + for (i = 0; i < len2 && k < DTOP_DICT_SIZE; i++) { + if (line2[i] == ' ') { + dict->val[k] = &line2[i+1]; + line2[i] = 0; + k++; + } + } + if (j != k) { + if (k < j) + j = k; + fprintf(stderr, "Warning, list index length mismatch\n"); + } + dict->max = j; + return j; +} + +/** + * @brief Parses line for prefixes for files that have individual data_point prefixes. + * + * Use this method for lines that have a prefix before data begins. Such as + * "/proc/net/snmp" + * + * @param line1 Line to parse to find datapoint prefix. + * @param len1 Length of line1. + * @param dict Dictionary prefix is being added to. + */ +void dt_parse_for_prefix(char *line1, int len1, struct dt_procdict *dict) +{ + int i, j, k; + + if (len1 < 1) + return; + + if (line1 == 0 || dict == 0) + return; + + k = 0; + for (i = 0; i < len1 && k < DTOP_DICT_SIZE; i++) { + if (line1[i] == ' ') { + dict->key[k] = &line1[i+1]; + line1[i] = 0; + k++; + } + } + + for (j = 0; j < k; j++) + dict->val[j] = &line1[0]; + + for (j = 0; j < len1; j++) { + if (line1[j] == ':') + line1[j] = 0; + } +} + +/** + * @brief Finds the dictionary index of a data_point name. + * + * @param str Name of data_point that is to be located in dict. + * @param dict Dictionary to look through for dp name. + * @return Dictionary index of name if found. + * @return -1 if name not found in dictionary keys. + */ +int dt_find_dict_idx(const char *str, struct dt_procdict *dict) +{ + int i; + if (str == 0 || dict == 0) + return -1; + + for (i = 0; i < dict->max; i++) { + if (dict->key[i] && !strcmp(str, dict->key[i])) + return i; + } + return -1; +} + +/** + * @brief Parses files that have Names and Values on same line. + * + * Use this method to parse lines that have a dp name followed + * by a dp value. Such as "/proc/net/snmp6" + * + * @param line1 Line to parse to find datapoint names and values. + * @param len1 Length of line1. + * @param l Index in the dictionary the key/val pair is added to. + * @param dict Dictionary the keys and values are added to. + * @return Number of key/val pairs in the dictionary. + */ +int dt_parse_proc_same_line_key_and_val(char *line1, int len1, + int l, struct dt_procdict *dict) +{ + int i, k, n; + if (len1 < 1) + return 0; + + if (line1 == 0 || dict == 0) + return 0; + + k = l; + for (i = 0; i < len1 && k < DTOP_DICT_SIZE; i++) { + if (line1[i] == ' ') { + dict->key[k] = &line1[0]; + line1[i] = 0; + for (n = i+1; n < len1; n++) { + if (line1[n] != ' ') { + dict->val[k] = &line1[n+1]; + break; + } + } + break; + } + } + k++; + dict->max = k; + return k; +} + +/** + * @brief Parses files that have a single line. + * + * Parses a single line file for csv, tab-separated, space-separated, and single + * value formats and adds values to a dictionary. Such as + * "/proc/sys/net/ipv4/ping_group_range" + * + * Use this method to parse lines that contain only values. + * + * @param line1 Line to parse. + * @param len1 Length of line1. + * @param dict Dictionary datapoints are added to. + * @return Number of values dictionary holds. + */ +int dt_single_line_parse(char *line1, int len1, struct dt_procdict *dict) +{ + int i, k; + k = 0; + dict->val[k] = &line1[0]; + k++; + + for (i = 0; i < len1; i++) { + if (line1[i] == ' ' || line1[i] == ',' || line1[i] == ' ') { + line1[i] = 0; + dict->val[k] = &line1[i+1]; + k++; + } + } + dict->max = k; + return k; +} diff --git a/datatop/src/datatop_str.h b/datatop/src/datatop_str.h new file mode 100644 index 0000000..1f30370 --- /dev/null +++ b/datatop/src/datatop_str.h @@ -0,0 +1,70 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_str.h + * @brief Declares methods held in datatop_str.c and defines dictionary struct. + */ + +#ifndef DATATOP_STR_H +#define DATATOP_STR_H + +#define DTOP_DICT_SIZE 2048 + +/** + * @struct dt_procdict + * @brief Struct used to create dictionary for parsing purposes. + * + * @var dt_procdict::max + * Number of key/val pairs in dictionary. + * @var dt_procdict::key + * Holds the key that is used to access the value. + * @var dt_procdict::val + * Value that the key accesses. + */ +struct dt_procdict { + int max; + char *key[DTOP_DICT_SIZE]; + char *val[DTOP_DICT_SIZE]; +}; + +int dt_read_line(char *buf1, int len1, const char *buf2, int len2, int start); + +int dt_parse_proc_dictionary(char *line1, int len1, char *line2, int len2, + struct dt_procdict *dict); + +int dt_find_dict_idx(const char *str, struct dt_procdict *dict); + +int dt_parse_proc_same_line_key_and_val(char *line1, int len1, int l, + struct dt_procdict *dict); + +void dt_parse_for_prefix(char *line1, int len1, struct dt_procdict *dict); + +int dt_single_line_parse(char *line1, int len1, struct dt_procdict *dict); +#endif /* DATATOP_STR_H */ diff --git a/datatop/src/datatop_sys_snap.c b/datatop/src/datatop_sys_snap.c new file mode 100644 index 0000000..ee9ea95 --- /dev/null +++ b/datatop/src/datatop_sys_snap.c @@ -0,0 +1,199 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +#include <unistd.h> +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <fcntl.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <sys/stat.h> + +#include "datatop_interface.h" +#include "datatop_fileops.h" +#include "datatop_str.h" +#include "datatop_opt.h" + +#define DTOP_SNAP_SIZE 8192 +#define DTOP_SNAP_LINE (DTOP_SNAP_SIZE>>2) + +static int dtop_system_snapshot_helper_print(char *file, const char *str) +{ + FILE *snap_file = fopen(file, "a"); + + if (snap_file) { + if (fprintf(snap_file, "%s", str) < 0) { + fclose(snap_file); + return FILE_ERROR; + } + } else { + return FILE_ERROR; + } + fflush(snap_file); + fclose(snap_file); + return FILE_SUCCESS; +} + +/** + * @brief A helper function to dtop_print_system_snapshot. + * + * @param fw File that desired system data is printed to. + * @return FILE_ERROR - Writing to file was unsuccessful. + * @return FILE_SUCCESS - Writing to file was successful. + */ +static int dtop_run_and_log(char *file, const char *c1, const char **args) +{ + int i; + pid_t child_pid; + + i = 0; + dtop_system_snapshot_helper_print(file, "\n" + "--------------------------------------------------------------\n" + "Command: "); + while(args[i] != 0) { + dtop_system_snapshot_helper_print(file, args[i++]); + dtop_system_snapshot_helper_print(file, " "); + } + dtop_system_snapshot_helper_print(file, "\n"); + + + child_pid = fork(); + if (child_pid == 0) { + int fd = open(file, O_WRONLY | O_APPEND | O_CREAT, + S_IRUSR | S_IWUSR); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + close(fd); + execvp(c1, (char * const *)args); + printf("Failed to execute %s\n", c1); + printf("errno=%d error=%s\n", errno, strerror(errno)); + close(STDOUT_FILENO); + close(STDERR_FILENO); + exit(0); + } else if (child_pid < 0) { + return FILE_ERROR; + } else { + int return_status; + waitpid(child_pid, &return_status, 0); + + if (return_status != 0) + return FILE_ERROR; + } + + return FILE_SUCCESS; +} + + + +/* IPv4 */ +const char *ip_addr_cmd[] = {"ip", "addr", 0}; +const char *ip_route_cmd[] = {"ip", "route", 0}; +const char *ip_route_all_tables_cmd[] = {"ip", "route", "show", "table", "all", 0}; +const char *ip_rule_cmd[] = {"ip", "rule", "show", 0}; +const char *ip_tables_cmd[] = {"iptables", "-L", "-n", "-v", 0}; +const char *ip_tables_nat_cmd[] = {"iptables", "-t", "nat", "-L", "-n", "-v", 0}; +const char *ip_tables_mangle_cmd[] = {"iptables", "-t", "mangle", "-L", "-n", "-v", 0}; +const char *ip_tables_raw_cmd[] = {"iptables", "-t", "raw", "-L", "-n", "-v", 0}; + +/* IPv6 */ +const char *ip6_addr_cmd[] = {"ip", "-6", "addr", 0}; +const char *ip6_route_cmd[] = {"ip", "-6", "route", 0}; +const char *ip6_route_all_tables_cmd[] = {"ip", "-6", "route", "show", "table", "all", 0}; +const char *ip6_rule_cmd[] = {"ip", "-6", "rule", "show", 0}; +const char *ip6_tables_cmd[] = {"ip6tables", "-L", "-n", "-v", 0}; +const char *ip6_tables_nat_cmd[] = {"ip6tables", "-t", "nat", "-L", "-n", "-v", 0}; +const char *ip6_tables_mangle_cmd[] = {"ip6tables", "-t", "mangle", "-L", "-n", "-v", 0}; +const char *ip6_tables_raw_cmd[] = {"ip6tables", "-t", "raw", "-L", "-n", "-v", 0}; + +/* Misc */ +const char *rps_config[] = {"cat", "/sys/class/net/rmnet_mhi0/queues/rx-0/rps_cpus", 0}; +const char *if_config[] = {"/data/busybox/busybox", "ifconfig", 0}; +const char *netcfg[] = {"netcfg", 0}; +const char *softnet_stat[] = {"cat", "/proc/net/softnet_stat", 0}; + +/* XFRM logging */ +const char *xfrm_state[] = {"ip", "xfrm", "state", "show", 0}; +const char *xfrm_policy[] = {"ip", "xfrm", "policy", "show", 0}; +const char *xfrm_netstat[] = {"cat", "/proc/net/xfrm_stat", 0}; + +#define DO_DTOP_RUN_AND_LOG(X) \ + dtop_run_and_log(file, X[0], X); +/** + * @brief Prints a System snapshot to a file specified by the user. + * + * @param fw File that system snapshot is printed to. + * @return FILE_ERROR - Writing to file was unsuccessful. + * @return FILE_SUCCESS - Writing to file was successful. + */ +int dtop_print_system_snapshot(char *file) +{ + dtop_system_snapshot_helper_print(file, + "==============================================================\n" + " System Data Snapshot - Captured with Data Top \n" + " Version "); + dtop_system_snapshot_helper_print(file, VERSION); + dtop_system_snapshot_helper_print(file, "\n" + "==============================================================\n" + "\n"); + + /* IPv4 */ + DO_DTOP_RUN_AND_LOG(ip_addr_cmd); + DO_DTOP_RUN_AND_LOG(ip_route_cmd); + DO_DTOP_RUN_AND_LOG(ip_route_all_tables_cmd); + DO_DTOP_RUN_AND_LOG(ip_rule_cmd); + DO_DTOP_RUN_AND_LOG(ip_tables_cmd); + DO_DTOP_RUN_AND_LOG(ip_tables_nat_cmd); + DO_DTOP_RUN_AND_LOG(ip_tables_mangle_cmd); + DO_DTOP_RUN_AND_LOG(ip_tables_raw_cmd); + + /* IPv6 */ + DO_DTOP_RUN_AND_LOG(ip6_addr_cmd); + DO_DTOP_RUN_AND_LOG(ip6_route_cmd); + DO_DTOP_RUN_AND_LOG(ip6_route_all_tables_cmd); + DO_DTOP_RUN_AND_LOG(ip6_rule_cmd); + DO_DTOP_RUN_AND_LOG(ip6_tables_cmd); + DO_DTOP_RUN_AND_LOG(ip6_tables_nat_cmd); + DO_DTOP_RUN_AND_LOG(ip6_tables_mangle_cmd); + DO_DTOP_RUN_AND_LOG(ip6_tables_raw_cmd); + + /* Misc */ + DO_DTOP_RUN_AND_LOG(rps_config); + DO_DTOP_RUN_AND_LOG(if_config); + DO_DTOP_RUN_AND_LOG(netcfg); + DO_DTOP_RUN_AND_LOG(softnet_stat); + + /* XFRM logging */ + DO_DTOP_RUN_AND_LOG(xfrm_state); + DO_DTOP_RUN_AND_LOG(xfrm_policy); + DO_DTOP_RUN_AND_LOG(xfrm_netstat); + + return FILE_SUCCESS; +} diff --git a/datatop/src/datatop_value_only_poll.c b/datatop/src/datatop_value_only_poll.c new file mode 100644 index 0000000..bf6fc54 --- /dev/null +++ b/datatop/src/datatop_value_only_poll.c @@ -0,0 +1,201 @@ +/************************************************************************ +Copyright (c) 2015, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************/ + +/** + * @file datatop_value_only_poll.c + * @brief Adds ability for data collection from files with only values + * + * File contains methods for searching and polling data from + * value_only files, meaning a file with a single line, containing + * only values. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "datatop_interface.h" +#include "datatop_fileops.h" +#include "datatop_str.h" + +#define DTOP_SINGLE_SIZE 8192 +#define DTOP_SINGLE_LINE (DTOP_SINGLE_SIZE>>2) + +/** + * @brief Stores the data collected from a value_only files. + * + * @param dpg Struct that polled data is added to. + * @return DTOP_POLL_IO_ERR - Poll of dpg unsuccessful. + * @return DTOP_POLL_OK - Poll of dpg successful. + */ +int dtop_value_only_poll(struct dtop_data_point_gatherer *dpg) +{ + char *data; + int line_len; + char line[DTOP_SINGLE_LINE]; + int read; + struct dt_procdict dict; + int j; + FILE *check = fopen(dpg->file, "r"); + if (check) { + fclose(check); + read = dt_read_file(dpg->file, &data, DTOP_SINGLE_SIZE); + } else { + return DTOP_POLL_IO_ERR; + } + + if (read == 0 || data == 0) + return DTOP_POLL_IO_ERR; + + line_len = dt_read_line(line, DTOP_SINGLE_LINE, data, + DTOP_SINGLE_SIZE, 0); + + /* Stores dp values in dictionary */ + dt_single_line_parse(line, line_len, &dict); + + /* Assigns the dp value to the dp struct */ + for (j = 0; j < dpg->data_points_len; j++) + dtop_store_dp(&(dpg->data_points[j]), dict.val[j]); + + dt_free(&data); + return DTOP_POLL_OK; +} + +/** + * @brief Frees dynamically allocated single line dpg. + * + * Frees the memory of the dpg along with it's data_points + * and other malloc'd memory no longer needed. + * + * @param dpg Dpg to deconstruct and deallocate memory for. + */ +void dtop_value_only_dpg_deconstructor + (struct dtop_data_point_gatherer *dpset) +{ + int i; + for (i = 0; i < dpset->data_points_len; i++) + free(dpset->data_points[i].name); + free(dpset->data_points); + free(dpset->file); + free(dpset); +} + +/** + * @brief Creates a dpg for a single line file. + * + * Dynamically allocates memory for dpg which is then added to a linked list + * via the dtop_register(dpg) function call. + * + * @param name Name of file dpg represents. + * @param data_points dtop_data_point struct that dpg points to. + * @param dp_count Number of data_points in data_points array + */ +static void construct_value_only_dpg(char *name, struct dtop_data_point + *data_points, int dp_count) +{ + struct dtop_data_point_gatherer *dpg = malloc + (sizeof(struct dtop_data_point_gatherer)); + dpg->prefix = name; + dpg->file = name; + dpg->poll = dtop_value_only_poll; + dpg->data_points = data_points; + dpg->data_points_len = dp_count; + dpg->deconstruct = dtop_value_only_dpg_deconstructor; + + dtop_register(dpg); +} + +/** + * @brief Scans a single line file in order to autodetect dps. + * + * Searches through a file that contains a single line and only + * values in order to detect and create dp's/ + * + * @param name Name of file to scan. + */ +int dtop_value_only_search(char *name) +{ + int i; + char *data; + char line[DTOP_SINGLE_LINE]; + int line_len; + int read; + struct dt_procdict dict; + struct dtop_data_point *data_points; + + + read = dt_read_file(name, &data, DTOP_SINGLE_SIZE); + if (read == 0 || data == 0) { + free(name); + return DTOP_POLL_IO_ERR; + } + + line_len = dt_read_line(line, + DTOP_SINGLE_LINE, data, + DTOP_SINGLE_SIZE, 0); + + /* Stores dp values in dictionary */ + dt_single_line_parse(line, line_len, &dict); + + data_points = malloc(dict.max * sizeof(struct dtop_data_point)); + + /* Creates a dtop_data_point struct for each dp found in the file */ + for (i = 0; i < dict.max; i++) { + char *newname = malloc(sizeof(10)); + if (dict.val[i][0] == '-') + data_points[i].type = DTOP_LONG; + else + data_points[i].type = DTOP_ULONG; + data_points[i].name = malloc(sizeof(10)); + if (dict.max > 1) + snprintf(newname, sizeof(10), "[%d]:", i); + else + strlcpy(newname, "", sizeof(10)); + strlcpy(data_points[i].name, newname, sizeof(10)); + free(newname); + data_points[i].prefix = NULL; + data_points[i].skip = DO_NOT_SKIP; + data_points[i].initial_data_populated = NOT_POPULATED; + } + + /* Calls dpg constructor, dpg will point to the dp struct */ + construct_value_only_dpg(name, data_points, dict.max); + + dt_free(&data); + return DTOP_POLL_OK; +} + +/** + * @brief Calls dtop_search for files with a single line and only values. + */ +void dtop_value_only_init(char *name) +{ + char *file = malloc(strlen(name) + 1); + strlcpy(file, name, strlen(name) + 1); + dtop_value_only_search(file); +} |