Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
0003  *
0004  * Permission to use, copy, modify, and distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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     /* save "p < end" checks */
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 }