Skip to content

Commit

Permalink
mx_proc: Add rss_anon to struct mx_proc_pid_stat
Browse files Browse the repository at this point in the history
  • Loading branch information
donald committed Apr 2, 2022
1 parent 4a4ed5b commit b70ace5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions mx_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,38 @@
#include <assert.h>
#include <dirent.h>
#include <ctype.h>
#include <limits.h>

#include "mx_util.h"
#include "mx_proc.h"

static long long int get_rss_anon(pid_t pid) {
_mx_cleanup_free_ char *fname;
mx_asprintf_forever(&fname, "/proc/%d/status", pid);
_mx_cleanup_fclose_ FILE *file = fopen(fname, "r");
if (file == NULL)
return -errno;
_mx_cleanup_free_ char *buf = NULL;
size_t n = 0;
while(1) {
size_t len = getline(&buf, &n, file);
if (len == -1)
break;
if (strncmp(buf, "RssAnon:", 8) == 0) {
unsigned long long int anon_rss_kb = strtoull(buf+8, NULL, 10);
if (anon_rss_kb == ULLONG_MAX)
return -errno;
if (anon_rss_kb > LLONG_MAX/1024) { // anon_rss > 8 EiB
return -ERANGE;
}
return anon_rss_kb*1024;
}
}
if (feof(file))
return 0; /* kernel thread */
return -errno;
}

static int _mx_proc_pid_stat_strscan(char *str, struct mx_proc_pid_stat *pps)
{
size_t res = 0;
Expand Down Expand Up @@ -94,6 +122,10 @@ int mx_proc_pid_stat(struct mx_proc_pid_stat **pps, pid_t pid)
res = mx_proc_pid_stat_read(pstat, "/proc/%d/stat", pid);
if (res < 0)
return res;
long long int rss_anon = get_rss_anon(pid);
if (rss_anon < 0)
return rss_anon;
pstat->rss_anon = rss_anon;
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions mx_proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ struct mx_proc_pid_stat {
unsigned long long int delayacct_blkio_ticks; /* 42 */
unsigned long long int guest_time; /* 43 */
long long int cguest_time; /* 44 */

unsigned long long int rss_anon; /* from /proc/PID/status. may be null (kernel thread). unit: bytes */
};

int mx_proc_pid_stat_read(struct mx_proc_pid_stat *pps, char *fmt, ...);
Expand Down

0 comments on commit b70ace5

Please sign in to comment.