summaryrefslogtreecommitdiff
path: root/sysdeps/linux/glibtop_private.c
blob: d3a49aa697eacef4961abcbcbcbe5540c005e9d4 (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
#include <config.h>
#include <glibtop.h>
#include <glibtop/error.h>

#include "glibtop_private.h"

#include <glib.h>

#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

#include <fcntl.h>
#include <unistd.h>


unsigned long long
get_scaled(const char *buffer, const char *key)
{
	const char    *ptr = buffer;
	char	      *next;
	unsigned long long value;

	if (key) {
		if (G_LIKELY((ptr = strstr(buffer, key))))
			ptr += strlen(key);
		else {
			g_warning("Could not read key '%s' in buffer '%s'",
				  key, buffer);
			return 0;
		}
	}

	value = strtoull(ptr, &next, 0);

	for ( ; *next; ++next) {
		if (*next == 'k') {
			value *= 1024;
			break;
		} else if (*next == 'M') {
			value *= 1024 * 1024;
			break;
		}
	}

	return value;
}


char *
skip_token (const char *p)
{
	p = next_token(p);
	while (*p && !g_ascii_isspace(*p)) p++;
	p = next_token(p);
	return (char *)p;
}


/*
 * Read functions
 */
enum TRY_FILE_TO_BUFFER
{
	TRY_FILE_TO_BUFFER_OK = 0,
	TRY_FILE_TO_BUFFER_OPEN = -1,
	TRY_FILE_TO_BUFFER_READ = -2
};

/*
 * Doesn't handle bufsiz == 0
 */
int try_file_to_buffer(char *buffer, size_t bufsiz, const char *format, ...)
{
	char path[4096];
	int fd;
	size_t len = 0;
	ssize_t nread = 0;
	va_list pa;

	if (G_UNLIKELY(bufsiz <= sizeof(char*)))
	  g_warning("Huhu, bufsiz of %lu looks bad", (gulong)bufsiz);

	va_start(pa, format);

	/* C99 also provides vsnprintf */
	g_vsnprintf(path, sizeof path, format, pa);

	va_end(pa);

	bufsiz--; /* reserve 1 for trailing NUL */
	buffer [0] = '\0';

	if((fd = open (path, O_RDONLY)) < 0)
		return TRY_FILE_TO_BUFFER_OPEN;

	while (len < bufsiz) {
		nread = read (fd, buffer + len, bufsiz - len);

		if (G_UNLIKELY(nread < 0)) {
			if (errno == EINTR)
				continue;
			else
				break;
		}

		len += nread;

		if (nread == 0)
			break;
	}

	close (fd);

	if (nread < 0)
		return TRY_FILE_TO_BUFFER_READ;

	buffer [len] = '\0';

	return TRY_FILE_TO_BUFFER_OK;
}


void
file_to_buffer(glibtop *server, char *buffer, size_t bufsiz, const char *filename)
{
	switch(try_file_to_buffer(buffer, bufsiz, "%s", filename))
	{
	case TRY_FILE_TO_BUFFER_OPEN:
		glibtop_error_io_r (server, "open (%s)", filename);
	case TRY_FILE_TO_BUFFER_READ:
		glibtop_error_io_r (server, "read (%s)", filename);
	}
}




static unsigned long
read_boot_time(glibtop *server)
{
	char* line = NULL;
	size_t size = 0;
	FILE* stat;
	unsigned long btime = 0;

	if (!(stat = fopen("/proc/stat", "r"))) {
		glibtop_error_io_r(server, "fopen(\"/proc/stat\")");
		goto out;
	}

	while (getline(&line, &size, stat) != -1) {
		if (!strncmp(line, "btime", 5)) {
			btime = strtoul(skip_token(line), NULL, 10);
			break;
		}
	}

	free(line);
	fclose(stat);
out:
	return btime;
}



unsigned long
get_boot_time(glibtop *server)
{
	static unsigned long boot_time = 0UL;

	if(G_UNLIKELY(!boot_time))
	{
		boot_time = read_boot_time(server);
	}

	return boot_time;
}



gboolean
check_cpu_line(glibtop *server, const char *line, unsigned i)
{
	char start[10];

	g_snprintf(start, sizeof start, "cpu%u", i);

	return g_str_has_prefix(line, start);
}



gboolean
has_sysfs(void)
{
	static gboolean init;
	static gboolean sysfs;

	if (G_UNLIKELY(!init)) {
		sysfs = g_file_test("/sys", G_FILE_TEST_IS_DIR);
		init = TRUE;
	}

	return sysfs;
}



gboolean safe_readlink(const char *path, char *buf, size_t bufsiz)
{
	ssize_t ret;

	ret = readlink(path, buf, bufsiz - 1);

	if (ret == -1) {
		g_warning("Could not read link %s : %s", path, strerror(errno));
		return FALSE;
	}

	buf[ret] = '\0';
	return TRUE;
}