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 // Test that values in /proc/uptime increment monotonically
0017 // while shifting across CPUs.
0018 #undef NDEBUG
0019 #include <assert.h>
0020 #include <unistd.h>
0021 #include <sys/syscall.h>
0022 #include <stdlib.h>
0023 #include <string.h>
0024 
0025 #include <stdint.h>
0026 #include <sys/types.h>
0027 #include <sys/stat.h>
0028 #include <fcntl.h>
0029 
0030 #include "proc-uptime.h"
0031 
0032 static inline int sys_sched_getaffinity(pid_t pid, unsigned int len, unsigned long *m)
0033 {
0034     return syscall(SYS_sched_getaffinity, pid, len, m);
0035 }
0036 
0037 static inline int sys_sched_setaffinity(pid_t pid, unsigned int len, unsigned long *m)
0038 {
0039     return syscall(SYS_sched_setaffinity, pid, len, m);
0040 }
0041 
0042 int main(void)
0043 {
0044     unsigned int len;
0045     unsigned long *m;
0046     unsigned int cpu;
0047     uint64_t u0, u1, i0, i1;
0048     int fd;
0049 
0050     /* find out "nr_cpu_ids" */
0051     m = NULL;
0052     len = 0;
0053     do {
0054         len += sizeof(unsigned long);
0055         free(m);
0056         m = malloc(len);
0057     } while (sys_sched_getaffinity(0, len, m) == -EINVAL);
0058 
0059     fd = open("/proc/uptime", O_RDONLY);
0060     assert(fd >= 0);
0061 
0062     proc_uptime(fd, &u0, &i0);
0063     for (cpu = 0; cpu < len * 8; cpu++) {
0064         memset(m, 0, len);
0065         m[cpu / (8 * sizeof(unsigned long))] |= 1UL << (cpu % (8 * sizeof(unsigned long)));
0066 
0067         /* CPU might not exist, ignore error */
0068         sys_sched_setaffinity(0, len, m);
0069 
0070         proc_uptime(fd, &u1, &i1);
0071         assert(u1 >= u0);
0072         assert(i1 >= i0);
0073         u0 = u1;
0074         i0 = i1;
0075     }
0076 
0077     return 0;
0078 }