Skip to content

Commit 2585756

Browse files
committed
libbpf-tools: trace_helpers: add str_loadavg()
There are many tools read /proc/loadavg, this commit add a comment function str_loadavg(). Signed-off-by: Rong Tao <[email protected]>
1 parent 137bd5f commit 2585756

File tree

7 files changed

+60
-74
lines changed

7 files changed

+60
-74
lines changed

libbpf-tools/biotop.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -301,24 +301,14 @@ static int read_stat(struct biotop_bpf *obj, struct data_t *datas, __u32 *count)
301301

302302
static int print_stat(struct biotop_bpf *obj)
303303
{
304-
FILE *f;
305-
time_t t;
306-
struct tm *tm;
307-
char ts[16], buf[256];
304+
char buf[256];
308305
static struct data_t datas[OUTPUT_ROWS_LIMIT];
309-
int n, i, err = 0, rows = OUTPUT_ROWS_LIMIT;
310-
311-
f = fopen("/proc/loadavg", "r");
312-
if (f) {
313-
time(&t);
314-
tm = localtime(&t);
315-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
316-
memset(buf, 0, sizeof(buf));
317-
n = fread(buf, 1, sizeof(buf), f);
318-
if (n)
319-
printf("%8s loadavg: %s\n", ts, buf);
320-
fclose(f);
321-
}
306+
int i, err = 0, rows = OUTPUT_ROWS_LIMIT;
307+
308+
err = str_loadavg(buf, sizeof(buf), true);
309+
if (!err)
310+
printf("%s\n", buf);
311+
322312
printf("%-7s %-16s %1s %-3s %-3s %-8s %5s %7s %6s\n",
323313
"PID", "COMM", "D", "MAJ", "MIN", "DISK", "I/O", "Kbytes", "AVGms");
324314

libbpf-tools/filetop.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,13 @@ static int sort_column(const void *obj1, const void *obj2)
182182

183183
static int print_stat(struct filetop_bpf *obj)
184184
{
185-
FILE *f;
186-
time_t t;
187-
struct tm *tm;
188-
char ts[16], buf[256];
185+
char buf[256];
189186
struct file_id key, *prev_key = NULL;
190187
static struct file_stat values[OUTPUT_ROWS_LIMIT];
191-
int n, i, err = 0, rows = 0;
188+
int i, err = 0, rows = 0;
192189
int fd = bpf_map__fd(obj->maps.entries);
193190

194-
f = fopen("/proc/loadavg", "r");
195-
if (f) {
196-
time(&t);
197-
tm = localtime(&t);
198-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
199-
memset(buf, 0, sizeof(buf));
200-
n = fread(buf, 1, sizeof(buf), f);
201-
if (n)
202-
printf("%8s loadavg: %s\n", ts, buf);
203-
fclose(f);
204-
}
191+
printf("%s\n", str_loadavg(buf, sizeof(buf), true) ? "N/A" : buf);
205192

206193
printf("%-7s %-16s %-6s %-6s %-7s %-7s %1s %s\n",
207194
"TID", "COMM", "READS", "WRITES", "R_Kb", "W_Kb", "T", "FILE");

libbpf-tools/oomkill.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,17 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
6060

6161
static int handle_event(void *ctx, void *data, size_t len)
6262
{
63-
FILE *f;
6463
char buf[256];
65-
int n = 0;
6664
struct tm *tm;
6765
char ts[32];
6866
time_t t;
6967
struct data_t *e = data;
7068

71-
f = fopen("/proc/loadavg", "r");
72-
if (f) {
73-
memset(buf, 0, sizeof(buf));
74-
n = fread(buf, 1, sizeof(buf), f);
75-
fclose(f);
76-
}
7769
time(&t);
7870
tm = localtime(&t);
7971
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
8072

81-
if (n)
73+
if (!str_loadavg(buf, sizeof(buf), false))
8274
printf("%s Triggered by PID %d (\"%s\"), OOM kill of PID %d (\"%s\"), %lld pages, loadavg: %s",
8375
ts, e->fpid, e->fcomm, e->tpid, e->tcomm, e->pages, buf);
8476
else

libbpf-tools/slabratetop.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -169,26 +169,13 @@ static int sort_column(const void *obj1, const void *obj2)
169169

170170
static int print_stat(struct slabratetop_bpf *obj)
171171
{
172-
FILE *f;
173-
time_t t;
174-
struct tm *tm;
175-
char ts[16], buf[256];
172+
char buf[256];
176173
char *key, **prev_key = NULL;
177174
static struct slabrate_info values[OUTPUT_ROWS_LIMIT];
178-
int n, i, err = 0, rows = 0;
175+
int i, err = 0, rows = 0;
179176
int fd = bpf_map__fd(obj->maps.slab_entries);
180177

181-
f = fopen("/proc/loadavg", "r");
182-
if (f) {
183-
time(&t);
184-
tm = localtime(&t);
185-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
186-
memset(buf, 0 , sizeof(buf));
187-
n = fread(buf, 1, sizeof(buf), f);
188-
if (n)
189-
printf("%8s loadavg: %s\n", ts, buf);
190-
fclose(f);
191-
}
178+
printf("%s\n", str_loadavg(buf, sizeof(buf), true) ? "N/A" : buf);
192179

193180
printf("%-32s %6s %10s\n", "CACHE", "ALLOCS", "BYTES");
194181

libbpf-tools/tcptop.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,10 @@ static int sort_column(const void *obj1, const void *obj2)
214214

215215
static int print_stat(struct tcptop_bpf *obj)
216216
{
217-
FILE *f;
218-
time_t t;
219-
struct tm *tm;
220-
char ts[16], buf[256];
217+
char buf[256];
221218
struct ip_key_t key, *prev_key = NULL;
222219
static struct info_t infos[OUTPUT_ROWS_LIMIT];
223-
int n, i, err = 0;
220+
int i, err = 0;
224221
int fd = bpf_map__fd(obj->maps.ip_map);
225222
int rows = 0;
226223
bool ipv6_header_printed = false;
@@ -232,17 +229,7 @@ static int print_stat(struct tcptop_bpf *obj)
232229
close(pid_max_fd);
233230

234231
if (!no_summary) {
235-
f = fopen("/proc/loadavg", "r");
236-
if (f) {
237-
time(&t);
238-
tm = localtime(&t);
239-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
240-
memset(buf, 0, sizeof(buf));
241-
n = fread(buf, 1, sizeof(buf), f);
242-
if (n)
243-
printf("%8s loadavg: %s\n", ts, buf);
244-
fclose(f);
245-
}
232+
printf("%s\n", str_loadavg(buf, sizeof(buf), true) ? "N/A" : buf);
246233
}
247234

248235
while (1) {

libbpf-tools/trace_helpers.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,3 +1311,43 @@ int str_to_long(const char *src, void *dest)
13111311

13121312
return errno;
13131313
}
1314+
1315+
/**
1316+
* If @ts_pfx is true, the timestamp prefix will added.
1317+
*/
1318+
int str_loadavg(char *buf, size_t buf_len, bool ts_pfx)
1319+
{
1320+
int n, err = 0;
1321+
time_t t;
1322+
struct tm *tm;
1323+
char ts[16], avg[64];
1324+
FILE *f;
1325+
1326+
if (!buf || buf_len == 0)
1327+
return -EINVAL;
1328+
1329+
f = fopen("/proc/loadavg", "r");
1330+
if (!f) {
1331+
return -errno;
1332+
}
1333+
1334+
n = fread(avg, 1, sizeof(avg), f);
1335+
if (!n) {
1336+
err = -errno;
1337+
goto cleanup;
1338+
}
1339+
1340+
memset(buf, 0, buf_len);
1341+
if (ts_pfx) {
1342+
time(&t);
1343+
tm = localtime(&t);
1344+
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
1345+
snprintf(buf, buf_len, "%8s loadavg: %s", ts, avg);
1346+
} else {
1347+
snprintf(buf, buf_len, "loadavg: %s", avg);
1348+
}
1349+
1350+
cleanup:
1351+
fclose(f);
1352+
return err;
1353+
}

libbpf-tools/trace_helpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,7 @@ int str_to_int(const char *src, void *dest);
121121
/* converts a string to a long integer */
122122
int str_to_long(const char *src, void *dest);
123123

124+
/* get loadavg string */
125+
int str_loadavg(char *buf, size_t buf_len, bool ts_pfx);
126+
124127
#endif /* __TRACE_HELPERS_H */

0 commit comments

Comments
 (0)