From 954d6c12f58724773596b1bd669683ce59cae2c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Mon, 28 Sep 2020 12:01:56 +0200 Subject: [PATCH] Simplify statm parsing and document unused fields --- linux/LinuxProcess.c | 4 ++-- linux/LinuxProcessList.c | 31 +++++++++++++------------------ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 8fb26e01..373c9ad8 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -67,8 +67,8 @@ ProcessFieldData Process_fields[] = { [M_SHARE] = { .name = "M_SHARE", .title = " SHR ", .description = "Size of the process's shared pages", .flags = 0, }, [M_TRS] = { .name = "M_TRS", .title = " CODE ", .description = "Size of the text segment of the process", .flags = 0, }, [M_DRS] = { .name = "M_DRS", .title = " DATA ", .description = "Size of the data segment plus stack usage of the process", .flags = 0, }, - [M_LRS] = { .name = "M_LRS", .title = " LIB ", .description = "The library size of the process", .flags = 0, }, - [M_DT] = { .name = "M_DT", .title = " DIRTY ", .description = "Size of the dirty pages of the process", .flags = 0, }, + [M_LRS] = { .name = "M_LRS", .title = " LIB ", .description = "The library size of the process (unused since Linux 2.6; always 0)", .flags = 0, }, + [M_DT] = { .name = "M_DT", .title = " DIRTY ", .description = "Size of the dirty pages of the process (unused since Linux 2.6; always 0)", .flags = 0, }, [ST_UID] = { .name = "ST_UID", .title = " UID ", .description = "User ID of the process owner", .flags = 0, }, [PERCENT_CPU] = { .name = "PERCENT_CPU", .title = "CPU% ", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, }, [PERCENT_MEM] = { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, }, diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index c02bca88..2317e1c8 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -406,25 +406,20 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, const char* dirna static bool LinuxProcessList_readStatmFile(LinuxProcess* process, const char* dirname, const char* name) { char filename[MAX_NAME+1]; - xSnprintf(filename, MAX_NAME, "%s/%s/statm", dirname, name); - int fd = open(filename, O_RDONLY); - if (fd == -1) + xSnprintf(filename, sizeof(filename), "%s/%s/statm", dirname, name); + FILE* statmfile = fopen(filename, "r"); + if (!statmfile) return false; - char buf[PROC_LINE_LENGTH + 1]; - ssize_t rres = xread(fd, buf, PROC_LINE_LENGTH); - close(fd); - if (rres < 1) return false; - - char *p = buf; - errno = 0; - process->super.m_size = strtol(p, &p, 10); if (*p == ' ') p++; - process->super.m_resident = strtol(p, &p, 10); if (*p == ' ') p++; - process->m_share = strtol(p, &p, 10); if (*p == ' ') p++; - process->m_trs = strtol(p, &p, 10); if (*p == ' ') p++; - process->m_lrs = strtol(p, &p, 10); if (*p == ' ') p++; - process->m_drs = strtol(p, &p, 10); if (*p == ' ') p++; - process->m_dt = strtol(p, &p, 10); - return (errno == 0); + int r = fscanf(statmfile, "%ld %ld %ld %ld %ld %ld %ld", + &process->super.m_size, + &process->super.m_resident, + &process->m_share, + &process->m_trs, + &process->m_lrs, + &process->m_drs, + &process->m_dt); + fclose(statmfile); + return r == 7; } static bool LinuxProcessList_readSmapsFile(LinuxProcess* process, const char* dirname, const char* name, bool haveSmapsRollup) {