int main(int ac, char **av) { int lc,num,i; /* loop counter */ char *msg; /* message returned from parse_opts */ cpu_set_t mask; unsigned int len = sizeof(cpu_set_t); /* parse standard options */ if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); tst_exit(); } setup(); TEST(num=sysconf(_SC_NPROCESSORS_CONF)); //the number of processor(s) tst_resm(TINFO,"system has %d processor(s).", num); /* Check looping state if -i option given */ for (lc = 0; TEST_LOOPING(lc); ++lc) { Tst_count = 0; for (testno = 0; testno < TST_TOTAL; ++testno) { QUICK_TEST(sched_getaffinity(0, len, (cpu_set_t *)-1)); QUICK_TEST(sched_getaffinity(0, 0, &mask)); QUICK_TEST(sched_getaffinity(getpid() + 1, len, &mask)); CPU_ZERO(&mask); //clear TEST(sched_getaffinity(0,len,&mask)); //call sched_getaffinity() if(TEST_RETURN == -1) { tst_resm(TFAIL|TTERRNO, "could not get cpu affinity"); cleanup(); tst_exit(); } else { tst_resm(TINFO,"cpusetsize is %d", len); tst_resm(TINFO,"mask.__bits[0] = %lu ",mask.__bits[0]); for(i=0;i<num;i++){ // check the processor TEST(CPU_ISSET(i,&mask)); if (TEST_RETURN != -1 ){ tst_resm(TPASS,"sched_getaffinity() succeed ,this process %d is running processor: %d",getpid(), i); } } } } } cleanup(); tst_exit(); }
static void do_test(void) { int i; cpu_set_t *mask; int nrcpus = 1024; pid_t pid; unsigned len; #if __GLIBC_PREREQ(2, 7) realloc: mask = CPU_ALLOC(nrcpus); #else mask = malloc(sizeof(cpu_set_t)); #endif if (mask == NULL) tst_brkm(TFAIL | TTERRNO, cleanup, "fail to get enough memory"); #if __GLIBC_PREREQ(2, 7) len = CPU_ALLOC_SIZE(nrcpus); CPU_ZERO_S(len, mask); #else len = sizeof(cpu_set_t); CPU_ZERO(mask); #endif /* positive test */ TEST(sched_getaffinity(0, len, mask)); if (TEST_RETURN == -1) { CPU_FREE(mask); #if __GLIBC_PREREQ(2, 7) if (errno == EINVAL && nrcpus < (1024 << 8)) { nrcpus = nrcpus << 2; goto realloc; } #else if (errno == EINVAL) tst_resm(TFAIL, "NR_CPUS > 1024, we'd better use a " "newer glibc(>= 2.7)"); else #endif tst_resm(TFAIL | TTERRNO, "fail to get cpu affinity"); cleanup(); } else { tst_resm(TINFO, "cpusetsize is %d", len); tst_resm(TINFO, "mask.__bits[0] = %lu ", mask->__bits[0]); for (i = 0; i < num; i++) { #if __GLIBC_PREREQ(2, 7) TEST(CPU_ISSET_S(i, len, mask)); #else TEST(CPU_ISSET(i, mask)); #endif if (TEST_RETURN != -1) tst_resm(TPASS, "sched_getaffinity() succeed, " "this process %d is running " "processor: %d", getpid(), i); } } #if __GLIBC_PREREQ(2, 7) CPU_ZERO_S(len, mask); #else CPU_ZERO(mask); #endif /* negative tests */ QUICK_TEST(sched_getaffinity(0, len, (cpu_set_t *) - 1)); QUICK_TEST(sched_getaffinity(0, 0, mask)); /* * pid_t -> int -- the actual kernel limit is lower * though, but this is a negative test, not a positive * one. * * Push comes to shove, if the user doesn't have the * ability to kill(3) processes (errno = EPERM), then * set the pid to the highest possible represented * value and cross your fingers in the hope that * a) Linux somehow hasn't started allocating PIDs * this high and b) the PID = INT_MAX isn't in fact * running. */ for (pid = 2; pid < INT_MAX; pid++) { if (kill(pid, 0) == -1) { if (errno == ESRCH) break; else if (errno == EPERM) pid = INT_MAX - 1; } } QUICK_TEST(sched_getaffinity(pid, len, mask)); CPU_FREE(mask); }