Home | History | Annotate | Download | only in src
      1 /************************************************************************
      2 Copyright (c) 2015, 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_single_line_poll.c
     32  * @brief Adds ability for data collection from single line files
     33  *
     34  * File contains methods for searching and polling data from
     35  * single line files, meaning a file with multiple lines, but each
     36  * line contains the name of the dp, followed by the value.
     37  */
     38 
     39 #include <stdio.h>
     40 #include <string.h>
     41 #include <stdlib.h>
     42 #include "datatop_interface.h"
     43 #include "datatop_fileops.h"
     44 #include "datatop_str.h"
     45 
     46 #define DTOP_SINGLE_SIZE 8192
     47 #define DTOP_SINGLE_LINE (DTOP_SINGLE_SIZE>>2)
     48 
     49 /**
     50 * @struct dtop_single_line_vars
     51 * @brief Struct used to hold necessary variables for dual_line_file dpgs.
     52 *
     53 * @var dtop_single_line_vars::line
     54 * Array of strings where necessary dp names and values are held.
     55 * @var dtop_single_line_vars::line_count
     56 * Number of lines the file is that the dpg represents.
     57 */
     58 struct dtop_single_line_vars {
     59 	char **line;
     60 	int line_count;
     61 };
     62 
     63 /**
     64  * @brief Stores the data collected from a single_line files.
     65  *
     66  * @param dpg Struct that polled data is added to.
     67  * @return DTOP_POLL_IO_ERR - Poll of dpg unsuccessful.
     68  * @return DTOP_POLL_OK - Poll of dpg successful.
     69  */
     70 int dtop_single_line_poll(struct dtop_data_point_gatherer *dpg)
     71 {
     72 	char *data;
     73 	int *line_len = malloc(sizeof(int) *
     74 			((struct dtop_single_line_vars *)
     75 			(dpg->priv))->line_count);
     76 	int read;
     77 	struct dt_procdict dict;
     78 	int i, j, n, sum;
     79 
     80 	read = dt_read_file(dpg->file, &data, DTOP_SINGLE_SIZE);
     81 	if (read == 0 || data == 0)
     82 		return DTOP_POLL_IO_ERR;
     83 
     84 	sum = 0;
     85 	/* Assigns each line read from the file, a length */
     86 	for (n = 0; n < ((struct dtop_single_line_vars *)
     87 				(dpg->priv))->line_count; n++) {
     88 		line_len[n] = dt_read_line(((struct dtop_single_line_vars *)
     89 					(dpg->priv))->line[n],
     90 					   DTOP_SINGLE_LINE, data,
     91 					   DTOP_SINGLE_SIZE, sum);
     92 		if (n <= (((struct dtop_single_line_vars *)
     93 			(dpg->priv))->line_count - 1)) {
     94 			sum += (line_len[n] + 1);
     95 		}
     96 
     97 	}
     98 
     99 	/* Stores dp names and values in dictionary */
    100 	for (i = 0; i < dpg->data_points_len; i++)
    101 		dt_parse_proc_same_line_key_and_val((
    102 			(struct dtop_single_line_vars *)
    103 			(dpg->priv))->line[i], line_len[i], i, &dict);
    104 
    105 	/* Assigns the dp value to the dp struct */
    106 	for (j = 0; j < dpg->data_points_len; j++) {
    107 		i = dt_find_dict_idx(dpg->data_points[j].name, &dict);
    108 		if (i >= 0 && i < dict.max)
    109 			dtop_store_dp(&(dpg->data_points[j]),
    110 				      dict.val[i]);
    111 	}
    112 
    113 	dt_free(&data);
    114 	free(line_len);
    115 	return DTOP_POLL_OK;
    116 }
    117 
    118 /**
    119  * @brief Frees dynamically allocated single_line_file dpgs.
    120  *
    121  * Frees the memory of the dpg along with it's data_points
    122  * and other malloc'd memory no longer needed.
    123  *
    124  * @param dpg Dpg to deconstruct and deallocate memory for.
    125  */
    126 static void dtop_single_line_dpg_deconstructor
    127 			(struct dtop_data_point_gatherer *dpset)
    128 {
    129 	int i;
    130 	free(dpset->data_points);
    131 	for (i = 0; i < ((struct dtop_single_line_vars *)
    132 				(dpset->priv))->line_count; i++)
    133 		free(((struct dtop_single_line_vars *)(dpset->priv))->line[i]);
    134 	free(((struct dtop_single_line_vars *)(dpset->priv))->line);
    135 	free(((struct dtop_single_line_vars *)(dpset->priv)));
    136 	free(dpset);
    137 }
    138 
    139 /**
    140  * @brief Creates a dpg for a single_line file.
    141  *
    142  * Dynamically allocates memory for dpg which is then added to a linked list
    143  * via the dtop_register(dpg) function call.
    144  *
    145  * @param name Name of file dpg represents.
    146  * @param data_points dtop_data_point struct that dpg points to.
    147  * @param storage dtop_single_line_vars struct that holds relevant dpg variables.
    148  */
    149 static void construct_single_line_file_dpg(char *name, struct dtop_data_point
    150 		*data_points, struct dtop_single_line_vars *storage)
    151 {
    152 	struct dtop_data_point_gatherer *dpg = malloc
    153 		(sizeof(struct dtop_data_point_gatherer));
    154 	dpg->prefix = name;
    155 	dpg->file = name;
    156 	dpg->poll = dtop_single_line_poll;
    157 	dpg->data_points = data_points;
    158 	dpg->priv = (struct dtop_single_line_vars *)storage;
    159 	dpg->data_points_len = storage->line_count;
    160 	dpg->deconstruct = dtop_single_line_dpg_deconstructor;
    161 
    162 	dtop_register(dpg);
    163 }
    164 
    165 /**
    166  * @brief Scans a single_line file for all datapoints and creats dps.
    167  *
    168  * Searches through a single_line file (Key followed by value on the
    169  * same line) for all available data points to create as dp structs.
    170  *
    171  * @param name Name of file.
    172  * @param storage dtop_single_line_vars struct where relevant variables are stored.
    173  */
    174 int dtop_single_line_search(char *name, struct dtop_single_line_vars *storage)
    175 {
    176 	int i, k, n, sum;
    177 	char *data;
    178 	int *line_len = malloc(sizeof(int) * storage->line_count);
    179 	int read;
    180 	struct dt_procdict dict;
    181 	struct dtop_data_point *data_points;
    182 
    183 	storage->line = malloc(storage->line_count * sizeof(*storage->line));
    184 
    185 	for (i = 0; i < storage->line_count; i++)
    186 		storage->line[i] = malloc(sizeof(char) * DTOP_SINGLE_LINE);
    187 
    188 	read = dt_read_file(name, &data, DTOP_SINGLE_SIZE);
    189 	if (read == 0 || data == 0)
    190 		return DTOP_POLL_IO_ERR;
    191 
    192 	sum = 0;
    193 	/* Assigns each line read from the file, a length */
    194 	for (n = 0; n < storage->line_count; n++) {
    195 		line_len[n] = dt_read_line(storage->line[n],
    196 					   DTOP_SINGLE_LINE, data,
    197 					   DTOP_SINGLE_SIZE, sum);
    198 		if (n < (storage->line_count - 1))
    199 			sum += (line_len[n] + 1);
    200 	}
    201 
    202 	/* Stores dp names and values in dictionary */
    203 	for (i = 0; i < (storage->line_count); i++)
    204 		dt_parse_proc_same_line_key_and_val(storage->line[i],
    205 						line_len[i], i, &dict);
    206 
    207 	data_points = malloc
    208 		       (storage->line_count * sizeof(struct dtop_data_point));
    209 
    210 	k = 0;
    211 	/* Creates a dtop_data_point struct for each dp found in the file */
    212 	for (i = 0; i < dict.max; i++) {
    213 		if (dict.val[i][0] == '-')
    214 			data_points[k].type = DTOP_LONG;
    215 		else
    216 			data_points[k].type = DTOP_ULONG;
    217 		data_points[i].name = dict.key[i];
    218 		data_points[i].prefix = NULL;
    219 		k++;
    220 	}
    221 
    222 	/* Calls dpg constructor, dpg will point to the dp struct */
    223 	construct_single_line_file_dpg(name, data_points, storage);
    224 
    225 	free(line_len);
    226 	dt_free(&data);
    227 
    228 	return DTOP_POLL_OK;
    229 }
    230 
    231 /**
    232  * @brief Calls dtop_search for a files with single line format.
    233  *
    234  * Single line format refers to a file, where each line contains
    235  * the name of a dp, followed by the value of a dp.
    236  */
    237 void dtop_single_line_init(char *name)
    238 {
    239 	struct dtop_single_line_vars *storage = malloc
    240 			(sizeof(struct dtop_single_line_vars));
    241 	storage->line_count = dtop_get_file_line_amount(name);
    242 	dtop_single_line_search(name, storage);
    243 }
    244