Home | History | Annotate | Download | only in src
      1 /************************************************************************
      2 Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
      3 
      4 Redistribution and use in source and binary forms, with or without
      5 modification, are permitted provided that the following conditions are
      6 met:
      7     * Redistributions of source code must retain the above copyright
      8       notice, this list of conditions and the following disclaimer.
      9     * Redistributions in binary form must reproduce the above
     10       copyright notice, this list of conditions and the following
     11       disclaimer in the documentation and/or other materials provided
     12       with the distribution.
     13     * Neither the name of The Linux Foundation nor the names of its
     14       contributors may be used to endorse or promote products derived
     15       from this software without specific prior written permission.
     16 
     17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 ************************************************************************/
     29 
     30 /**
     31 * @file datatop.c
     32 * @brief Executes commands for application.
     33 *
     34 * Contains the main() function where program executes. Calls appropriate
     35 * methods based on user's CLI commands. Executes parsing function to
     36 * determine necessary output and handles errors which may arise during the
     37 * parse. Initiliazes files for data collection. Will call functions designed
     38 * to poll and print the data in understandable format.
     39 */
     40 
     41 #include <stdio.h>
     42 #include <stdlib.h>
     43 #include <sys/time.h>
     44 #include <sys/resource.h>
     45 #include <unistd.h>
     46 #include <string.h>
     47 #include <errno.h>
     48 #include <time.h>
     49 #include "datatop_interface.h"
     50 #include "datatop_linked_list.h"
     51 #include "datatop_opt.h"
     52 #include "datatop_fileops.h"
     53 #include "datatop_polling.h"
     54 #include "datatop_gen_poll.h"
     55 
     56 struct dtop_linked_list *first_dpg_list;
     57 struct cli_opts usr_cl_opts;
     58 
     59 /**
     60  * @brief Prints the datapoint names and values to the terminal.
     61  *
     62  * @param dpg_list A pointer to the first node of a linked list which
     63  *                 contains all data_point_gatherer structs to print.
     64  */
     65 void dtop_print_terminal(struct dtop_linked_list *dpg_list)
     66 {
     67 	struct dtop_linked_list *curr_ptr = dpg_list;
     68 	struct dtop_data_point_gatherer *dpset;
     69 
     70 	while (curr_ptr) {
     71 		dpset = (struct dtop_data_point_gatherer *) curr_ptr->data;
     72 		dtop_print_dpg(dpset);
     73 		curr_ptr = curr_ptr->next_ptr;
     74 	}
     75 }
     76 
     77 /**
     78  * @brief Polls the data periodically and prints to file specified by the user.
     79  *
     80  * Polls the data as often as specified by the user in their CLI arguments
     81  * and outputs the data to a file also specified in CLI arguments. Then prints
     82  * a snapshot of delta(dp_value) to the terminal.
     83  *
     84  * @param dpg_list A pointer to the first node of a linked list which contains
     85  *                 all data_point_gatherer structs to poll and print.
     86  * @param fw A pointer to the file which will be printed to.
     87  * @return FILE_ERROR - Writing to file was unsuccessful.
     88  * @return FILE_SUCCESS - Writing to file was successful.
     89  */
     90 int dtop_poll_periodically(struct dtop_linked_list *dpg_list, FILE *fw)
     91 {
     92 	struct timeval tv, timeout;
     93 	fd_set rfds;
     94 	time_t curtime, endtime;
     95 	int inp, quit = 0;
     96 	struct dtop_linked_list *curr_ptr = dpg_list;
     97 	struct dtop_data_point_gatherer *dpset;
     98 	struct timeval ftime, itime, polltime;
     99 
    100 	gettimeofday(&tv, NULL);
    101 	curtime = tv.tv_sec;
    102 	endtime = tv.tv_sec + usr_cl_opts.poll_time;
    103 
    104 	/* print all of our datapoint names as column headers in csv format */
    105 	if (fprintf(fw, "\"Time\",") < 0)
    106 		return FILE_ERROR;
    107 
    108 	while (curr_ptr) {
    109 		dpset = (struct dtop_data_point_gatherer *) curr_ptr->data;
    110 		if (dtop_print_dpg_names_csv(dpset, fw) == FILE_ERROR)
    111 			return FILE_ERROR;
    112 		curr_ptr = curr_ptr->next_ptr;
    113 	}
    114 	if (fprintf(fw, "\n") < 0)
    115 		return FILE_ERROR;
    116 
    117 	dtop_print_interactive_opts();
    118 	gettimeofday(&itime, NULL);
    119 	/* periodically poll the datapoints and print in csv format */
    120 	while (curtime < endtime
    121 		|| usr_cl_opts.poll_time == POLL_NOT_SPECIFIED) {
    122 		FD_ZERO(&rfds);
    123 		FD_SET(0, &rfds);
    124 		timeout.tv_sec = usr_cl_opts.poll_per;
    125 		timeout.tv_usec = 0;
    126 		//ftime is right before timeout calculations for most acurate calculations
    127 		gettimeofday(&ftime, NULL);
    128 		timersub(&ftime, &itime, &polltime);
    129 		timersub(&timeout,&polltime, &timeout);
    130 		inp = select(1, &rfds, NULL, NULL, &timeout);
    131 		gettimeofday(&itime, NULL);
    132 		if (inp) {
    133 			char s[4];
    134 			scanf("%s", s);
    135 			if (strcmp(s, "quit") == 0
    136 			    || strcmp(s, "q") == 0) {
    137 				quit = QUIT;
    138 				break;
    139 			}
    140 			if (strcmp(s, "i") == 0) {
    141 				dtop_print_snapshot_diff(first_dpg_list);
    142 				dtop_reset_dp_initial_values(first_dpg_list);
    143 			}
    144 			if (strcmp(s, "l") == 0)
    145 				dtop_print_snapshot_diff(first_dpg_list);
    146 		}
    147 		gettimeofday(&tv, NULL);
    148 		curtime = tv.tv_sec;
    149 		dtop_poll(dpg_list);
    150 		printf("Polled at %ld.%06ld\n", tv.tv_sec, tv.tv_usec);
    151 		if (dtop_print_time_at_poll(fw) == FILE_ERROR)
    152 			return FILE_ERROR;
    153 		if (dtop_write_pollingdata_csv(dpg_list, fw) == FILE_ERROR)
    154 			return FILE_ERROR;
    155 	}
    156 
    157 	if (quit != QUIT)
    158 		dtop_print_snapshot_diff(dpg_list);
    159 	return FILE_SUCCESS;
    160 }
    161 
    162 static void dtop_set_niceness(int niceness)
    163 {
    164 	int pid, rc;
    165 	pid = getpid();
    166 	printf("Requesting nice %d\n", niceness);
    167 	rc = setpriority(PRIO_PROCESS, pid, niceness);
    168 	if (rc != 0)
    169 		fprintf(stderr, "Error setting priority [%d]\n", errno);
    170 
    171 	rc = getpriority(PRIO_PROCESS, pid);
    172 	printf("Running with nice %d.\n", rc);
    173 }
    174 
    175 int main(int argc, char **argv)
    176 {
    177 	int parse_status;
    178 	printf("DataTop - Version %s\n", VERSION);
    179 	printf("(c)2014-2015 Linux Foundation\n");
    180 
    181 	dtop_load_default_options(&usr_cl_opts);
    182 
    183 	parse_status = dtop_parse_cli_opts(&usr_cl_opts, argc, argv);
    184 	switch (parse_status) {
    185 	case PARSE_SUCCESS:
    186 		dtop_set_niceness(usr_cl_opts.priority);
    187 	break;
    188 
    189 	case PARSE_FORCE_EXIT:
    190 		exit(EXIT_SUCCESS);
    191 	break;
    192 
    193 	case PARSE_FAILURE:
    194 	default:
    195 		printf("Failed to parse command line arguments.\n");
    196 		exit(EXIT_FAILURE);
    197 	break;
    198 	}
    199 
    200 	dtop_dual_line_init("/proc/net/netstat");
    201 	dtop_dual_line_init("/proc/net/snmp");
    202 	dtop_single_line_init("/proc/net/snmp6");
    203 	dtop_gen_init("/proc/sys/net/");
    204 	dtop_gen_init("/sys/module/rmnet_data/parameters/");
    205 	dtop_gen_init("/sys/class/net/rmnet_mhi0/statistics/");
    206 	dtop_gen_init("/sys/class/net/usb_rmnet0/statistics/");
    207 	dtop_gen_init("/sys/class/net/rmnet_ipa0/statistics/");
    208 	dtop_meminfo_init();
    209 	dtop_dev_init();
    210 	dtop_stat_init();
    211 	dtop_cpu_stats_init();
    212 	dtop_gen_init("/sys/kernel/debug/clk/bimc_clk/");
    213 	dtop_gen_init("/sys/kernel/debug/clk/snoc_clk/");
    214 	dtop_gen_init("/sys/kernel/debug/clk/pnoc_clk/");
    215 
    216 	if (usr_cl_opts.print_cl == OPT_CHOSE) {
    217 		dtop_poll(first_dpg_list);
    218 		dtop_print_terminal(first_dpg_list);
    219 	}
    220 
    221 	if (usr_cl_opts.print_csv == OPT_CHOSE) {
    222 		FILE *to_file = NULL;
    223 		if ((dtop_open_writing_file(usr_cl_opts.file_name,
    224 						&to_file)) == VALID) {
    225 			printf("\nData being polled for %ld seconds.\n",
    226 						usr_cl_opts.poll_time);
    227 			if (dtop_poll_periodically(first_dpg_list, to_file)
    228 			    == FILE_ERROR) {
    229 				fprintf(stderr, "err=%d: %s\n", errno,
    230 							strerror(errno));
    231 				dtop_close_file(to_file);
    232 				deconstruct_dpgs(first_dpg_list);
    233 				dtop_rem_linked_list(first_dpg_list);
    234 				exit(EXIT_FAILURE);
    235 			}
    236 			dtop_close_file(to_file);
    237 		} else {
    238 			printf("File Can Not Be Opened\n");
    239 			exit(EXIT_FAILURE);
    240 		}
    241 	}
    242 
    243 	if (usr_cl_opts.snapshot_file) {
    244 		if (dtop_print_system_snapshot(usr_cl_opts.snapshot_file)
    245 			    == FILE_ERROR) {
    246 			fprintf(stderr, "err=%d: %s\n", errno,
    247 						strerror(errno));
    248 			deconstruct_dpgs(first_dpg_list);
    249 			dtop_rem_linked_list(first_dpg_list);
    250 			exit(EXIT_FAILURE);
    251 		}
    252 	}
    253 
    254 	if (usr_cl_opts.print_cl == OPT_NOT_CHOSE &&
    255 	    usr_cl_opts.print_csv == OPT_NOT_CHOSE) {
    256 		if ((!usr_cl_opts.snapshot_file)
    257 		   || usr_cl_opts.poll_time_selected == POLL_TIME_SELECTED) {
    258 			printf("\nData will now be polled for %ld seconds.\n",
    259 							usr_cl_opts.poll_time);
    260 			dtop_poll(first_dpg_list);
    261 			sleep(usr_cl_opts.poll_time);
    262 			dtop_poll(first_dpg_list);
    263 			dtop_print_snapshot_diff(first_dpg_list);
    264 		}
    265 	}
    266 
    267 	deconstruct_dpgs(first_dpg_list);
    268 	dtop_rem_linked_list(first_dpg_list);
    269 	return 0;
    270 }
    271 
    272 /**
    273  * @brief Adds each dpg as a node to a linked list.
    274  *
    275  * Called when a dpg is initialized.
    276  *
    277  * @param dpg A pointer to a data_point_gatherer struct which is to be added to the linked list.
    278  */
    279 void dtop_register(struct dtop_data_point_gatherer *dpg)
    280 {
    281 	if (dpg)
    282 		first_dpg_list = dtop_add_linked_list(dpg, first_dpg_list);
    283 }
    284