#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include int set_affinity(unsigned int cpu) { int rc = 0; cpu_set_t set; CPU_ZERO(&set); CPU_SET(cpu, &set); #ifdef MAKE_RHEL3 if (sched_setaffinity(0, &set) != 0) #else if (sched_setaffinity(0, sizeof(set), &set) != 0) #endif { rc = 1; fprintf(stderr, "failed to set CPU mask to %x: %s\n", cpu, strerror(errno)); } return rc; } int main(int argc, char *argv[]) { unsigned int tsleep = 0; unsigned int i; int max_cpu; struct timeval tv; if (argc < 2) { printf("usage: %s ncpus [sleeptime]\n", basename(argv[0])); return 1; } max_cpu = atoi(argv[1]); if (max_cpu == 0) return 2; if (argc > 2) tsleep = atoi(argv[2]); while(1) { printf("\n"); for (i = 0; i < max_cpu; ++i) { if (set_affinity(i) != 0) break; if (gettimeofday(&tv, NULL) != 0) printf("gettimeofday failed\n"); else printf("cpu %d: %ld.%06ld\n", i, tv.tv_sec, tv.tv_usec); } if (!tsleep) break; sleep(tsleep); } return 0; }