0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #undef NDEBUG
0017 #include <assert.h>
0018 #include <errno.h>
0019 #include <string.h>
0020 #include <stdlib.h>
0021 #include <unistd.h>
0022
0023 #include "proc.h"
0024
0025 static void proc_uptime(int fd, uint64_t *uptime, uint64_t *idle)
0026 {
0027 uint64_t val1, val2;
0028 char buf[64], *p;
0029 ssize_t rv;
0030
0031
0032 memset(buf, 0, sizeof(buf));
0033 rv = pread(fd, buf, sizeof(buf), 0);
0034 assert(0 <= rv && rv <= sizeof(buf));
0035 buf[sizeof(buf) - 1] = '\0';
0036
0037 p = buf;
0038
0039 val1 = xstrtoull(p, &p);
0040 assert(p[0] == '.');
0041 assert('0' <= p[1] && p[1] <= '9');
0042 assert('0' <= p[2] && p[2] <= '9');
0043 assert(p[3] == ' ');
0044
0045 val2 = (p[1] - '0') * 10 + p[2] - '0';
0046 *uptime = val1 * 100 + val2;
0047
0048 p += 4;
0049
0050 val1 = xstrtoull(p, &p);
0051 assert(p[0] == '.');
0052 assert('0' <= p[1] && p[1] <= '9');
0053 assert('0' <= p[2] && p[2] <= '9');
0054 assert(p[3] == '\n');
0055
0056 val2 = (p[1] - '0') * 10 + p[2] - '0';
0057 *idle = val1 * 100 + val2;
0058
0059 assert(p + 4 == buf + rv);
0060 }