summaryrefslogtreecommitdiffstats
path: root/datatop/src/datatop_gen_poll.c
blob: 457cc350fc9adae0db3e6ad26fcab729cbe76213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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);
}