Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2002-2004 Roland McGrath <roland@redhat.com> |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 3 | * Copyright (c) 2009-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 29 | #include "defs.h" |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 30 | #include <sched.h> |
| 31 | |
| 32 | static unsigned int |
| 33 | get_cpuset_size(void) |
| 34 | { |
| 35 | static unsigned int cpuset_size; |
| 36 | |
| 37 | if (!cpuset_size) { |
| 38 | pid_t pid = getpid(); |
| 39 | cpuset_size = 128; |
| 40 | while (cpuset_size && |
| 41 | sched_getaffinity(pid, cpuset_size, NULL) == -1 && |
| 42 | EINVAL == errno) { |
| 43 | cpuset_size <<= 1; |
| 44 | } |
| 45 | if (!cpuset_size) |
| 46 | cpuset_size = 128; |
| 47 | } |
| 48 | |
| 49 | return cpuset_size; |
| 50 | } |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 51 | |
| 52 | static void |
Dmitry V. Levin | a52375f | 2015-07-20 19:22:10 +0000 | [diff] [blame] | 53 | print_affinitylist(struct tcb *tcp, const unsigned long addr, const unsigned int len) |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 54 | { |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 55 | const unsigned int max_size = get_cpuset_size(); |
| 56 | const unsigned int umove_size = len < max_size ? len : max_size; |
| 57 | const unsigned int size = |
| 58 | (umove_size + current_wordsize - 1) & -current_wordsize; |
| 59 | const unsigned int ncpu = size * 8; |
| 60 | void *cpu; |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 61 | |
Dmitry V. Levin | a52375f | 2015-07-20 19:22:10 +0000 | [diff] [blame] | 62 | if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) || |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 63 | !addr || !len || !(cpu = calloc(size, 1))) { |
Dmitry V. Levin | a52375f | 2015-07-20 19:22:10 +0000 | [diff] [blame] | 64 | printaddr(addr); |
| 65 | return; |
| 66 | } |
| 67 | |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 68 | if (!umoven_or_printaddr(tcp, addr, umove_size, cpu)) { |
| 69 | int i = 0; |
| 70 | const char *sep = ""; |
| 71 | |
| 72 | tprints("["); |
| 73 | for (;; i++) { |
| 74 | i = next_set_bit(cpu, i, ncpu); |
| 75 | if (i < 0) |
| 76 | break; |
| 77 | tprintf("%s%d", sep, i); |
| 78 | sep = " "; |
| 79 | } |
| 80 | if (size < len) |
| 81 | tprintf("%s...", sep); |
| 82 | tprints("]"); |
Dmitry V. Levin | a52375f | 2015-07-20 19:22:10 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 85 | free(cpu); |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Dmitry V. Levin | a0bd374 | 2015-04-07 01:36:50 +0000 | [diff] [blame] | 88 | SYS_FUNC(sched_setaffinity) |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 89 | { |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 90 | const int pid = tcp->u_arg[0]; |
| 91 | const unsigned int len = tcp->u_arg[1]; |
| 92 | |
| 93 | tprintf("%d, %u, ", pid, len); |
| 94 | print_affinitylist(tcp, tcp->u_arg[2], len); |
Dmitry V. Levin | 8aa2a81 | 2015-07-20 19:24:43 +0000 | [diff] [blame] | 95 | |
| 96 | return RVAL_DECODED; |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Dmitry V. Levin | a0bd374 | 2015-04-07 01:36:50 +0000 | [diff] [blame] | 99 | SYS_FUNC(sched_getaffinity) |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 100 | { |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 101 | const int pid = tcp->u_arg[0]; |
| 102 | const unsigned int len = tcp->u_arg[1]; |
| 103 | |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 104 | if (entering(tcp)) { |
Dmitry V. Levin | 6bf08e3 | 2016-01-07 00:31:33 +0000 | [diff] [blame] | 105 | tprintf("%d, %u, ", pid, len); |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 106 | } else { |
Dmitry V. Levin | a52375f | 2015-07-20 19:22:10 +0000 | [diff] [blame] | 107 | print_affinitylist(tcp, tcp->u_arg[2], tcp->u_rval); |
Dmitry V. Levin | b94b983 | 2014-12-11 19:25:02 +0000 | [diff] [blame] | 108 | } |
| 109 | return 0; |
| 110 | } |