Skip to content

Commit 694de9f

Browse files
Rtoaxekyooo
authored andcommitted
libbpf-tools: trace_helpers: add str_loadavg() and str_timestamp()
There are many tools read /proc/loadavg and timestamp, this commit add two common function str_loadavg() and str_timestamp(). And fix bashreadline timestamp's wrong format, %m is month, should be %M (min). Signed-off-by: Rong Tao <[email protected]>
1 parent 8d066f0 commit 694de9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+133
-278
lines changed

libbpf-tools/bashreadline.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,9 @@ static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va
7474
static void handle_event(void *ctx, int cpu, void *data, __u32 data_size)
7575
{
7676
struct str_t *e = data;
77-
struct tm *tm;
7877
char ts[16];
79-
time_t t;
8078

81-
time(&t);
82-
tm = localtime(&t);
83-
strftime(ts, sizeof(ts), "%H:%m:%S", tm);
79+
str_timestamp("%H:%M:%S", ts, sizeof(ts));
8480

8581
printf("%-9s %-7d %s\n", ts, e->pid, e->str);
8682
}

libbpf-tools/bindsnoop.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,13 @@ static void sig_int(int signo)
142142
static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
143143
{
144144
struct bind_event *e = data;
145-
time_t t;
146-
struct tm *tm;
147145
char ts[32], addr[48];
148146
char opts[] = {'F', 'T', 'N', 'R', 'r', '\0'};
149147
const char *proto;
150148
int i = 0;
151149

152150
if (emit_timestamp) {
153-
time(&t);
154-
tm = localtime(&t);
155-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
151+
str_timestamp("%H:%M:%S", ts, sizeof(ts));
156152
printf("%8s ", ts);
157153
}
158154
if (e->proto == IPPROTO_TCP)

libbpf-tools/biolatency.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ int main(int argc, char **argv)
277277
.doc = argp_program_doc,
278278
};
279279
struct biolatency_bpf *obj;
280-
struct tm *tm;
281280
char ts[32];
282-
time_t t;
283281
int err;
284282
int idx, cg_map_fd;
285283
int cgfd = -1;
@@ -370,9 +368,7 @@ int main(int argc, char **argv)
370368
printf("\n");
371369

372370
if (env.timestamp) {
373-
time(&t);
374-
tm = localtime(&t);
375-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
371+
str_timestamp("%H:%M:%S", ts, sizeof(ts));
376372
printf("%-8s\n", ts);
377373
}
378374

libbpf-tools/biopattern.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ static int print_map(struct bpf_map *counters, struct partitions *partitions)
116116
int err, fd = bpf_map__fd(counters);
117117
const struct partition *partition;
118118
struct counter counter;
119-
struct tm *tm;
120119
char ts[32];
121-
time_t t;
122120

123121
while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
124122
err = bpf_map_lookup_elem(fd, &next_key, &counter);
@@ -131,9 +129,7 @@ static int print_map(struct bpf_map *counters, struct partitions *partitions)
131129
if (!total)
132130
continue;
133131
if (env.timestamp) {
134-
time(&t);
135-
tm = localtime(&t);
136-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
132+
str_timestamp("%H:%M:%S", ts, sizeof(ts));
137133
printf("%-9s ", ts);
138134
}
139135
partition = partitions__get_by_dev(partitions, next_key);

libbpf-tools/biosnoop.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
180180
struct event e;
181181
char rwbs[RWBS_LEN];
182182
struct timespec ct;
183-
struct tm *tm;
184183
char ts[32];
185184

186185
if (data_sz < sizeof(e)) {
@@ -194,8 +193,7 @@ void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
194193
/* Since `bpf_ktime_get_boot_ns` requires at least 5.8 kernel,
195194
* so get time from usespace instead */
196195
clock_gettime(CLOCK_REALTIME, &ct);
197-
tm = localtime(&ct.tv_sec);
198-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
196+
str_timestamp("%H:%M:%S", ts, sizeof(ts));
199197
printf("%-8s.%03ld ", ts, ct.tv_nsec / 1000000);
200198
} else {
201199
if (!start_ts) {

libbpf-tools/biotop.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -301,24 +301,15 @@ 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 loadavg[256], ts[64];
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(loadavg, sizeof(loadavg)) <= 0;
309+
err = err ?: (str_timestamp("%H:%M:%S", ts, sizeof(ts)) <= 0);
310+
if (!err)
311+
printf("%8s %s\n", ts, loadavg);
312+
322313
printf("%-7s %-16s %1s %-3s %-3s %-8s %5s %7s %6s\n",
323314
"PID", "COMM", "D", "MAJ", "MIN", "DISK", "I/O", "Kbytes", "AVGms");
324315

libbpf-tools/bitesize.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,8 @@ int main(int argc, char **argv)
158158
.doc = argp_program_doc,
159159
};
160160
struct bitesize_bpf *obj;
161-
struct tm *tm;
162161
char ts[32];
163162
int fd, err;
164-
time_t t;
165163

166164
err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
167165
if (err)
@@ -218,9 +216,7 @@ int main(int argc, char **argv)
218216
printf("\n");
219217

220218
if (env.timestamp) {
221-
time(&t);
222-
tm = localtime(&t);
223-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
219+
str_timestamp("%H:%M:%S", ts, sizeof(ts));
224220
printf("%-8s\n", ts);
225221
}
226222

libbpf-tools/cachestat.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,8 @@ int main(int argc, char **argv)
132132
__u64 buffers, cached, mbd;
133133
struct cachestat_bpf *obj;
134134
__s64 total, misses, hits;
135-
struct tm *tm;
136135
float ratio;
137136
char ts[32];
138-
time_t t;
139137
int err;
140138

141139
err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
@@ -262,9 +260,7 @@ int main(int argc, char **argv)
262260
goto cleanup;
263261
}
264262
if (env.timestamp) {
265-
time(&t);
266-
tm = localtime(&t);
267-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
263+
str_timestamp("%H:%M:%S", ts, sizeof(ts));
268264
printf("%-8s ", ts);
269265
}
270266
printf("%8lld %8lld %8llu %7.2f%% %12llu %10llu\n",

libbpf-tools/capable.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,9 @@ static void print_map(struct ksyms *ksyms, struct syms_cache *syms_cache)
278278
static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
279279
{
280280
const struct cap_event *e = data;
281-
struct tm *tm;
282281
char ts[32];
283-
time_t t;
284282

285-
time(&t);
286-
tm = localtime(&t);
287-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
283+
str_timestamp("%H:%M:%S", ts, sizeof(ts));
288284

289285
char *verdict = "deny";
290286
if (!e->ret)

libbpf-tools/cpudist.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ int main(int argc, char **argv)
198198
};
199199
struct cpudist_bpf *obj;
200200
int pid_max, fd, err;
201-
struct tm *tm;
202201
char ts[32];
203-
time_t t;
204202
int idx, cg_map_fd;
205203
int cgfd = -1;
206204

@@ -280,9 +278,7 @@ int main(int argc, char **argv)
280278
printf("\n");
281279

282280
if (env.timestamp) {
283-
time(&t);
284-
tm = localtime(&t);
285-
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
281+
str_timestamp("%H:%M:%S", ts, sizeof(ts));
286282
printf("%-8s\n", ts);
287283
}
288284

0 commit comments

Comments
 (0)