From 9f564cf8df6ef5fcba37082ba8013d6175955125 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 23 Jul 2024 02:02:51 -0700 Subject: libvmon: assume short positive reads found EOF This avoids a bunch of pread() returning 0 EOF-finding calls. These are proc files, and actually shouldn't be getting read in a loop like this at all because it's racy to do so. With proc files you need to atomically read everything you wish to parse as one sample as an atomic unit. So this needs to be properly reworked to enlarge the buffer when a read exhausts it, throwing away what was read, then repeating the sample with the enlarged buffer. But this is tenable for now until I get around to the proper rework... just looking to reduce some sampling overheads on lower end embedded devices. --- src/libvmon/vmon.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src') diff --git a/src/libvmon/vmon.c b/src/libvmon/vmon.c index 83d78c4..cace462 100644 --- a/src/libvmon/vmon.c +++ b/src/libvmon/vmon.c @@ -138,6 +138,12 @@ static char * load_contents_fd(vmon_t *vmon, vmon_char_array_t *array, int fd, v memcmpcpy(&alloc[total], vmon->buf, len, changed, changed_pos); total += len; + + /* assume short read found EOF */ + if (len < sizeof(vmon->buf)) { + len = 0; + break; + } } array->array = alloc; @@ -347,6 +353,10 @@ static sample_ret_t proc_sample_stat(vmon_t *vmon, vmon_proc_t *proc, vmon_proc_ goto _out; /* this saves us the EOF read syscall */ } } + + /* assume short read found EOF */ + if (len < sizeof(vmon->buf)) + break; } _out: @@ -430,6 +440,10 @@ static int proc_follow_children(vmon_t *vmon, vmon_proc_t *proc, vmon_proc_follo break; } } + + /* assume short read found EOF */ + if (len < sizeof(vmon->buf)) + break; } /* look for children which seem to no longer exist (found by stale generation numbers) and queue them for unmonitoring, flag this as a children change too */ @@ -791,6 +805,10 @@ static sample_ret_t proc_sample_vm(vmon_t *vmon, vmon_proc_t *proc, vmon_proc_vm goto _out; /* this saves us the EOF read syscall */ } } + + /* assume short read found EOF */ + if (len < sizeof(vmon->buf)) + break; } _out: @@ -848,6 +866,10 @@ static sample_ret_t proc_sample_io(vmon_t *vmon, vmon_proc_t *proc, vmon_proc_io goto _out; /* this saves us the EOF read syscall */ } } + + /* assume short read found EOF */ + if (len < sizeof(vmon->buf)) + break; } _out: @@ -911,6 +933,10 @@ static sample_ret_t sys_sample_stat(vmon_t *vmon, vmon_sys_stat_t **store) goto _out; /* this saves us the EOF read syscall */ } } + + /* assume short read found EOF */ + if (len < sizeof(vmon->buf)) + break; } _out: @@ -961,6 +987,10 @@ static sample_ret_t sys_sample_vm(vmon_t *vmon, vmon_sys_vm_t **store) goto _out; /* this saves us the EOF read syscall */ } } + + /* assume short read found EOF */ + if (len < sizeof(vmon->buf)) + break; } _out: -- cgit v1.2.3