aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/proc/proc-uptime-002.c
diff options
context:
space:
mode:
authorAlexey Dobriyan <adobriyan@gmail.com>2018-04-10 19:43:28 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2018-04-11 13:28:34 -0400
commit1f5bd0547654ada423b184e22f320d76c0fac49e (patch)
tree0d305b2d1f713af5bf4b9b534cb253183a58e716 /tools/testing/selftests/proc/proc-uptime-002.c
parent4f1134370a29a5f2d0f4b4be4c5e2fddd38f0f9d (diff)
proc: selftests: test /proc/uptime
The only tests I could come up with for /proc/uptime are: - test that values increase monotonically for 1 second, - bounce around CPUs and test the same thing. Avoid glibc like plague for affinity given patches like this: https://marc.info/?l=linux-kernel&m=152130031912594&w=4 Link: http://lkml.kernel.org/r/20180317165235.GB3445@avx2 Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Cc: Shuah Khan <shuah@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'tools/testing/selftests/proc/proc-uptime-002.c')
-rw-r--r--tools/testing/selftests/proc/proc-uptime-002.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/tools/testing/selftests/proc/proc-uptime-002.c b/tools/testing/selftests/proc/proc-uptime-002.c
new file mode 100644
index 000000000000..0cb79e1f1674
--- /dev/null
+++ b/tools/testing/selftests/proc/proc-uptime-002.c
@@ -0,0 +1,79 @@
1/*
2 * Copyright _ 2018 Alexey Dobriyan <adobriyan@gmail.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16// Test that values in /proc/uptime increment monotonically
17// while shifting across CPUs.
18#define _GNU_SOURCE
19#undef NDEBUG
20#include <assert.h>
21#include <unistd.h>
22#include <sys/syscall.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <stdint.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30
31#include "proc-uptime.h"
32
33static inline int sys_sched_getaffinity(pid_t pid, unsigned int len, unsigned long *m)
34{
35 return syscall(SYS_sched_getaffinity, pid, len, m);
36}
37
38static inline int sys_sched_setaffinity(pid_t pid, unsigned int len, unsigned long *m)
39{
40 return syscall(SYS_sched_setaffinity, pid, len, m);
41}
42
43int main(void)
44{
45 unsigned int len;
46 unsigned long *m;
47 unsigned int cpu;
48 uint64_t u0, u1, i0, i1;
49 int fd;
50
51 /* find out "nr_cpu_ids" */
52 m = NULL;
53 len = 0;
54 do {
55 len += sizeof(unsigned long);
56 free(m);
57 m = malloc(len);
58 } while (sys_sched_getaffinity(0, len, m) == -EINVAL);
59
60 fd = open("/proc/uptime", O_RDONLY);
61 assert(fd >= 0);
62
63 proc_uptime(fd, &u0, &i0);
64 for (cpu = 0; cpu < len * 8; cpu++) {
65 memset(m, 0, len);
66 m[cpu / (8 * sizeof(unsigned long))] |= 1UL << (cpu % (8 * sizeof(unsigned long)));
67
68 /* CPU might not exist, ignore error */
69 sys_sched_setaffinity(0, len, m);
70
71 proc_uptime(fd, &u1, &i1);
72 assert(u1 >= u0);
73 assert(i1 >= i0);
74 u0 = u1;
75 i0 = i1;
76 }
77
78 return 0;
79}