subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright © International Business Machines Corp., 2007, 2008 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation,Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * getcpu1.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * getcpu01 - call getcpu() and make sure it succeeds |
| 26 | * |
| 27 | * ALGORITHM |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * set cpu affinity of the process |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 29 | * If setaffinity() fails exit from the test suite |
| 30 | * Store the node ID of the cpu which has been set in previous step |
| 31 | * Make a call to getcpu() system call |
| 32 | * Verify the returned valued with the set value |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 33 | * if they match |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 34 | * test is considered PASS |
| 35 | * else |
| 36 | * test is considered FAIL |
| 37 | * |
| 38 | * USAGE: <for command-line> |
| 39 | * getcpu [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 40 | * where, -c n : Run n copies concurrently. |
| 41 | * -f : Turn off functionality Testing. |
| 42 | * -i n : Execute test n times. |
| 43 | * -I x : Execute test for x seconds. |
| 44 | * -P x : Pause for x seconds between iterations. |
| 45 | * -t : Turn on syscall timing. |
| 46 | * |
| 47 | * HISTORY |
| 48 | * 06/2008 written by Sharyathi Nagesh <sharyathi@in.ibm.com> |
| 49 | * |
subrata_modak | 5c8eb0c | 2009-05-30 18:30:15 +0000 | [diff] [blame] | 50 | * 05/2009 Suzuki K P <suzuki@in.ibm.com> |
| 51 | * Use TCONF instead of TWARN for non-NUMA machines |
| 52 | * |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 53 | * RESTRICTIONS |
| 54 | * none |
| 55 | */ |
| 56 | |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 57 | #define _GNU_SOURCE |
| 58 | #include <sched.h> |
| 59 | #include <errno.h> |
| 60 | #include "test.h" |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 61 | #include <sys/types.h> |
| 62 | #include <dirent.h> |
| 63 | |
| 64 | #if defined(__i386__) || defined(__x86_64__) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 65 | #if __GLIBC_PREREQ(2,6) |
| 66 | #if defined(__x86_64__) |
| 67 | #include <utmpx.h> |
| 68 | #endif |
| 69 | int sys_support = 1; |
| 70 | #elif defined(__i386__) |
| 71 | int sys_support = 1; |
| 72 | #else |
| 73 | int sys_support = 0; |
| 74 | #endif |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 75 | #else |
| 76 | int sys_support = 0; |
| 77 | #endif |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 78 | |
subrata_modak | 08216ea | 2010-01-07 11:03:10 +0000 | [diff] [blame] | 79 | #if !(__GLIBC_PREREQ(2, 7)) |
| 80 | #define CPU_FREE(ptr) free(ptr) |
| 81 | #endif |
| 82 | |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 83 | void cleanup(void); |
| 84 | void setup(void); |
| 85 | static inline int getcpu(unsigned int *, unsigned int *, void *); |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 86 | unsigned int set_cpu_affinity(void); |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 87 | unsigned int get_nodeid(unsigned int); |
subrata_modak | 08216ea | 2010-01-07 11:03:10 +0000 | [diff] [blame] | 88 | unsigned int max_cpuid(size_t, cpu_set_t *); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 89 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | char *TCID = "getcpu01"; |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 91 | int TST_TOTAL = 1; |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 92 | |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 93 | int main(int ac, char **av) |
| 94 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 95 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 96 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | unsigned int cpu_id, node_id = 0; |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 98 | unsigned int cpu_set; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | #ifdef __i386__ |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 100 | unsigned int node_set; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | #endif |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 102 | |
| 103 | /* Check For Kernel Version */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | if (((tst_kvercmp(2, 6, 20)) < 0) || !(sys_support)) { |
| 105 | tst_resm(TCONF, "This test can only run on kernels that are "); |
| 106 | tst_resm(TCONF, |
| 107 | "2.6.20 and higher and glibc version 2.6 and above"); |
| 108 | tst_resm(TCONF, "Currently the test case has been"); |
| 109 | tst_resm(TCONF, "developed only for i386 and x86_64"); |
| 110 | exit(0); |
| 111 | } |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 112 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 113 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 114 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 115 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | setup(); /* global setup */ |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 117 | |
| 118 | /* The following loop checks looping state if -i option given */ |
| 119 | |
| 120 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 121 | /* reset tst_count in case we are looping */ |
| 122 | tst_count = 0; |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 123 | |
| 124 | /* call the system call with the TEST() macro */ |
| 125 | cpu_set = set_cpu_affinity(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | #ifdef __i386__ |
| 127 | node_set = get_nodeid(cpu_set); |
| 128 | #endif |
| 129 | TEST(getcpu(&cpu_id, &node_id, NULL)); |
| 130 | if (TEST_RETURN == 0) { |
| 131 | if (cpu_id != cpu_set) { |
| 132 | tst_resm(TFAIL, "getcpu() returned wrong value" |
| 133 | " expected cpuid:%d, returned value cpuid: %d", |
| 134 | cpu_set, cpu_id); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 135 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 136 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | #ifdef __i386__ |
| 138 | else if (node_id != node_set) { |
| 139 | tst_resm(TFAIL, "getcpu() returned wrong value" |
| 140 | " expected node id:%d returned node id:%d", |
| 141 | node_set, node_id); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 142 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | } |
| 144 | #endif |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 145 | else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | tst_resm(TPASS, "getcpu() returned proper" |
| 147 | " cpuid:%d, node id:%d", cpu_id, |
| 148 | node_id); |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 149 | } else { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 150 | tst_resm(TFAIL, "getcpu() Failed, errno=%d:%s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | TEST_ERRNO, strerror(TEST_ERRNO)); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 152 | |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | cleanup(); |
| 157 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 158 | tst_exit(); |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 159 | } |
| 160 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 161 | /* |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 162 | * getcpu() - calls the system call |
| 163 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | static inline int getcpu(unsigned *cpu_id, unsigned *node_id, |
| 165 | void *cache_struct) |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 166 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | #if defined(__i386__) |
| 168 | return syscall(318, cpu_id, node_id, cache_struct); |
| 169 | #elif __GLIBC_PREREQ(2,6) |
| 170 | *cpu_id = sched_getcpu(); |
| 171 | #endif |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 175 | /* |
| 176 | * setup() - performs all the ONE TIME setup for this test. |
| 177 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 178 | void setup(void) |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 179 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 180 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 181 | /* ?? */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 182 | |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 183 | TEST_PAUSE; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * This will set the affinity to max cpu on which process can run |
| 188 | * and return that cpu id to the calling process |
| 189 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 190 | unsigned int set_cpu_affinity(void) |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 191 | { |
| 192 | unsigned cpu_max; |
subrata_modak | 08216ea | 2010-01-07 11:03:10 +0000 | [diff] [blame] | 193 | cpu_set_t *set; |
| 194 | size_t size; |
| 195 | int nrcpus = 1024; |
| 196 | #if __GLIBC_PREREQ(2, 7) |
| 197 | realloc: |
| 198 | set = CPU_ALLOC(nrcpus); |
| 199 | #else |
| 200 | set = malloc(sizeof(cpu_set_t)); |
| 201 | #endif |
| 202 | if (set == NULL) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 203 | tst_brkm(TFAIL, NULL, "CPU_ALLOC:errno:%d", errno); |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 204 | } |
subrata_modak | 08216ea | 2010-01-07 11:03:10 +0000 | [diff] [blame] | 205 | #if __GLIBC_PREREQ(2, 7) |
| 206 | size = CPU_ALLOC_SIZE(nrcpus); |
| 207 | CPU_ZERO_S(size, set); |
| 208 | #else |
| 209 | size = sizeof(cpu_set_t); |
| 210 | CPU_ZERO(set); |
| 211 | #endif |
| 212 | if (sched_getaffinity(0, size, set) < 0) { |
| 213 | CPU_FREE(set); |
| 214 | #if __GLIBC_PREREQ(2, 7) |
| 215 | if (errno == EINVAL && nrcpus < (1024 << 8)) { |
| 216 | nrcpus = nrcpus << 2; |
| 217 | goto realloc; |
| 218 | } |
| 219 | #else |
| 220 | if (errno == EINVAL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 221 | tst_resm(TFAIL, |
| 222 | "NR_CPUS of the kernel is more than 1024, so we'd better use a newer glibc(>= 2.7)"); |
subrata_modak | 08216ea | 2010-01-07 11:03:10 +0000 | [diff] [blame] | 223 | else |
| 224 | #endif |
| 225 | tst_resm(TFAIL, "sched_getaffinity:errno:%d", errno); |
| 226 | tst_exit(); |
| 227 | } |
| 228 | cpu_max = max_cpuid(size, set); |
| 229 | #if __GLIBC_PREREQ(2, 7) |
| 230 | CPU_ZERO_S(size, set); |
| 231 | CPU_SET_S(cpu_max, size, set); |
| 232 | #else |
| 233 | CPU_ZERO(set); |
| 234 | CPU_SET(cpu_max, set); |
| 235 | #endif |
| 236 | if (sched_setaffinity(0, size, set) < 0) { |
| 237 | CPU_FREE(set); |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 238 | tst_brkm(TFAIL, NULL, "sched_setaffinity:errno:%d", errno); |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 239 | } |
subrata_modak | 08216ea | 2010-01-07 11:03:10 +0000 | [diff] [blame] | 240 | CPU_FREE(set); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 241 | return cpu_max; |
| 242 | } |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * Return the maximum cpu id |
| 246 | */ |
| 247 | #define BITS_PER_BYTE 8 |
subrata_modak | 08216ea | 2010-01-07 11:03:10 +0000 | [diff] [blame] | 248 | unsigned int max_cpuid(size_t size, cpu_set_t * set) |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 249 | { |
| 250 | unsigned int index, max = 0; |
subrata_modak | 08216ea | 2010-01-07 11:03:10 +0000 | [diff] [blame] | 251 | for (index = 0; index < size * BITS_PER_BYTE; index++) |
| 252 | #if __GLIBC_PREREQ(2, 7) |
| 253 | if (CPU_ISSET_S(index, size, set)) |
| 254 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 255 | if (CPU_ISSET(index, set)) |
subrata_modak | 08216ea | 2010-01-07 11:03:10 +0000 | [diff] [blame] | 256 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 257 | max = index; |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 258 | return max; |
| 259 | } |
| 260 | |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 261 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 262 | * get_nodeid(cpuid) - This will return the node to which selected cpu belongs |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 263 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 264 | unsigned int get_nodeid(unsigned int cpu_id) |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 265 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 266 | DIR *directory_parent, *directory_node; |
| 267 | struct dirent *de, *dn; |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 268 | char directory_path[255]; |
| 269 | unsigned int cpu; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 270 | int node_id = 0; |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 271 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 272 | directory_parent = opendir("/sys/devices/system/node"); |
| 273 | if (!directory_parent) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 274 | tst_resm(TCONF, |
| 275 | "/sys not mounted or not a numa system. Assuming one node"); |
| 276 | tst_resm(TCONF, |
| 277 | "Error opening: /sys/devices/system/node :%s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 278 | strerror(errno)); |
| 279 | return 0; //By Default assume it to belong to node Zero |
| 280 | } else { |
| 281 | while ((de = readdir(directory_parent)) != NULL) { |
| 282 | if (strncmp(de->d_name, "node", 4)) |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 283 | continue; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 284 | sprintf(directory_path, "/sys/devices/system/node/%s", |
| 285 | de->d_name); |
| 286 | directory_node = opendir(directory_path); |
| 287 | while ((dn = readdir(directory_node)) != NULL) { |
| 288 | if (strncmp(dn->d_name, "cpu", 3)) |
| 289 | continue; |
| 290 | cpu = strtoul(dn->d_name + 3, NULL, 0); |
| 291 | if (cpu == cpu_id) { |
| 292 | node_id = |
| 293 | strtoul(de->d_name + 4, NULL, 0); |
| 294 | break; |
| 295 | } |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 296 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 297 | closedir(directory_node); |
| 298 | } |
| 299 | closedir(directory_parent); |
| 300 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 301 | return node_id; |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
| 305 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 306 | * or premature exit. |
| 307 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 308 | void cleanup(void) |
subrata_modak | 5a4cf27 | 2008-08-22 21:09:58 +0000 | [diff] [blame] | 309 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 310 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 311 | } |