Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | 48d97b6 | 2016-02-06 01:12:22 +0000 | [diff] [blame] | 2 | * This file is part of sched_xetaffinity strace test. |
| 3 | * |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 4 | * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include "tests.h" |
Dmitry V. Levin | 6a2f43c | 2016-08-09 14:38:29 +0000 | [diff] [blame^] | 31 | #include <asm/unistd.h> |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 32 | #include <sched.h> |
| 33 | |
| 34 | #if defined __NR_sched_getaffinity && defined __NR_sched_setaffinity \ |
| 35 | && defined CPU_ISSET_S && defined CPU_ZERO_S && defined CPU_SET_S |
| 36 | |
| 37 | # include <assert.h> |
| 38 | # include <errno.h> |
| 39 | # include <stdio.h> |
| 40 | # include <unistd.h> |
| 41 | |
| 42 | static int |
| 43 | getaffinity(unsigned long pid, unsigned long size, void *set) |
| 44 | { |
| 45 | return syscall(__NR_sched_getaffinity, pid, size, set); |
| 46 | } |
| 47 | |
| 48 | static int |
| 49 | setaffinity(unsigned long pid, unsigned long size, void *set) |
| 50 | { |
| 51 | return syscall(__NR_sched_setaffinity, pid, size, set); |
| 52 | } |
| 53 | |
| 54 | int |
| 55 | main(void) |
| 56 | { |
| 57 | unsigned int cpuset_size = 1; |
Dmitry V. Levin | 48d97b6 | 2016-02-06 01:12:22 +0000 | [diff] [blame] | 58 | const pid_t pid = getpid(); |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 59 | |
| 60 | while (cpuset_size) { |
| 61 | assert(getaffinity(pid, cpuset_size, NULL) == -1); |
| 62 | if (EFAULT == errno) |
| 63 | break; |
| 64 | if (EINVAL != errno) |
| 65 | perror_msg_and_skip("sched_getaffinity"); |
| 66 | printf("sched_getaffinity(%d, %u, NULL) = -1 EINVAL (%m)\n", |
| 67 | pid, cpuset_size); |
| 68 | cpuset_size <<= 1; |
| 69 | } |
| 70 | assert(cpuset_size); |
| 71 | printf("sched_getaffinity(%d, %u, NULL) = -1 EFAULT (%m)\n", |
| 72 | pid, cpuset_size); |
| 73 | |
| 74 | cpu_set_t *cpuset = tail_alloc(cpuset_size); |
Dmitry V. Levin | 48d97b6 | 2016-02-06 01:12:22 +0000 | [diff] [blame] | 75 | assert(getaffinity(pid, cpuset_size, cpuset + 1) == -1); |
| 76 | printf("sched_getaffinity(%d, %u, %p) = -1 EFAULT (%m)\n", |
| 77 | pid, cpuset_size, cpuset + 1); |
| 78 | |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 79 | assert(getaffinity(pid, cpuset_size, cpuset) == (int) cpuset_size); |
| 80 | printf("sched_getaffinity(%d, %u, [", pid, cpuset_size); |
| 81 | const char *sep; |
| 82 | unsigned int i, cpu; |
| 83 | for (i = 0, cpu = 0, sep = ""; i < cpuset_size * 8; ++i) { |
| 84 | if (CPU_ISSET_S(i, cpuset_size, cpuset)) { |
| 85 | printf("%s%u", sep, i); |
| 86 | sep = " "; |
| 87 | cpu = i; |
| 88 | } |
| 89 | } |
| 90 | printf("]) = %u\n", cpuset_size); |
| 91 | |
| 92 | CPU_ZERO_S(cpuset_size, cpuset); |
| 93 | CPU_SET_S(cpu, cpuset_size, cpuset); |
| 94 | if (setaffinity(pid, cpuset_size, cpuset)) |
| 95 | perror_msg_and_skip("sched_setaffinity"); |
| 96 | printf("sched_setaffinity(%d, %u, [%u]) = 0\n", |
| 97 | pid, cpuset_size, cpu); |
| 98 | |
| 99 | const unsigned int big_size = cpuset_size < 128 ? 128 : cpuset_size * 2; |
| 100 | cpuset = tail_alloc(big_size); |
| 101 | const int ret_size = getaffinity(pid, big_size, cpuset); |
| 102 | assert(ret_size >= (int) cpuset_size && ret_size <= (int) big_size); |
| 103 | printf("sched_getaffinity(%d, %u, [", pid, big_size); |
| 104 | for (i = 0, sep = ""; i < (unsigned) ret_size * 8; ++i) { |
| 105 | if (CPU_ISSET_S(i, (unsigned) ret_size, cpuset)) { |
| 106 | printf("%s%u", sep, i); |
| 107 | sep = " "; |
| 108 | } |
| 109 | } |
| 110 | printf("]) = %d\n", ret_size); |
| 111 | |
| 112 | puts("+++ exited with 0 +++"); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | #else |
| 117 | |
| 118 | SKIP_MAIN_UNDEFINED("__NR_sched_getaffinity && __NR_sched_setaffinity" |
| 119 | " && CPU_ISSET_S && CPU_ZERO_S && CPU_SET_S") |
| 120 | |
| 121 | #endif |