aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2021-09-22 19:52:03 -0700
committerRob Landley <rob@landley.net>2021-09-23 07:49:02 -0500
commit84b76f4be4deff568ee79b0ef5ed63318018a504 (patch)
treef653e1af34549ceabf69e864798ba386e5eba6e6
parentbb0dda496d84b3c98b32d6c3905d467de6e298b8 (diff)
downloadtoybox-84b76f4be4deff568ee79b0ef5ed63318018a504.tar.gz
bootchartd: stop using get_line().
We don't even need to read from /proc at all here: we can get the time since boot from clock_gettime(2) directly. Also, since TT.buf is only used for the timestamp, give it that name.
-rw-r--r--toys/pending/bootchartd.c28
1 files changed, 7 insertions, 21 deletions
diff --git a/toys/pending/bootchartd.c b/toys/pending/bootchartd.c
index 3590c81e..78084289 100644
--- a/toys/pending/bootchartd.c
+++ b/toys/pending/bootchartd.c
@@ -29,7 +29,7 @@ config BOOTCHARTD
#include "toys.h"
GLOBALS(
- char buf[32];
+ char timestamp[32];
long msec;
int proc_accounting;
@@ -41,7 +41,7 @@ static void dump_data_in_file(char *fname, int wfd)
int rfd = open(fname, O_RDONLY);
if (rfd != -1) {
- xwrite(wfd, TT.buf, strlen(TT.buf));
+ xwrite(wfd, TT.timestamp, strlen(TT.timestamp));
xsendfile(rfd, wfd);
close(rfd);
xwrite(wfd, "\n", 1);
@@ -55,7 +55,7 @@ static int dump_proc_data(FILE *fp)
pid_t pid;
DIR *proc_dir = opendir("/proc");
- fputs(TT.buf, fp);
+ fputs(TT.timestamp, fp);
while ((pid_dir = readdir(proc_dir))) {
char filename[64];
int fd;
@@ -134,6 +134,7 @@ static char *create_tmp_dir()
static void start_logging()
{
+ struct timespec ts;
int proc_stat_fd = xcreate("proc_stat.log",
O_WRONLY | O_CREAT | O_TRUNC, 0644);
int proc_diskstats_fd = xcreate("proc_diskstats.log",
@@ -148,31 +149,16 @@ static void start_logging()
xclose(kp_fd);
acct("kernel_procs_acct");
}
- memset(TT.buf, 0, sizeof(TT.buf));
while (--tcnt && !toys.signal) {
- int i = 0, j = 0, fd = open("/proc/uptime", O_RDONLY);
- if (fd < 0) goto wait;
- char *line = get_line(fd);
-
- if (!line) goto wait;
- while (line[i] != ' ') {
- if (line[i] == '.') {
- i++;
- continue;
- }
- TT.buf[j++] = line[i++];
- }
- TT.buf[j++] = '\n';
- TT.buf[j] = 0;
- free(line);
- close(fd);
+ clock_gettime(CLOCK_BOOTTIME, &ts);
+ sprintf(TT.timestamp, "%ld.%02d\n", (long) ts.tv_sec,
+ (int) (ts.tv_nsec/10000000));
dump_data_in_file("/proc/stat", proc_stat_fd);
dump_data_in_file("/proc/diskstats", proc_diskstats_fd);
// stop proc dumping in 2 secs if getty or gdm, kdm, xdm found
if (dump_proc_data(proc_ps_fp))
if (tcnt > 2 * 1000 / TT.msec) tcnt = 2 * 1000 / TT.msec;
fflush(0);
-wait:
msleep(TT.msec);
}
xclose(proc_stat_fd);