void myth_get_available_cpus(void) { int i; int n_specified_cpus = myth_parse_cpu_list("MYTH_CPU_LIST", myth_cpu_list, N_MAX_CPUS); n_available_cpus = 0; if (n_specified_cpus == -1) { fprintf(stderr, "myth: malformed MYTH_CPU_LIST ignored\n"); n_specified_cpus = 0; /* as if it wasn't specified */ } if (n_specified_cpus == 0) { /* MYTH_CPU_LIST not specified. treat as if it was 0,1,2,3,4,5,... */ n_specified_cpus = myth_get_n_available_cpus(); if (n_specified_cpus == -1) { fprintf(stderr, "myth: the platform does not tell the number of CPUs. to bind workers to cores, consider using MYTH_CPU_LIST environment variable\n"); /* we still go ahead, but does not do anything except for issuing a warning and return */ } for (i = 0; i < n_specified_cpus; i++) { myth_cpu_list[i] = i; } } /* got user-specified list of cpus. now, intersect with actually available cpus */ #if defined(HAVE_SCHED_GETAFFINITY) { cpu_set_t cset; /* get cpu set we can use */ sched_getaffinity(getpid(), sizeof(cpu_set_t), &cset); for (i = 0; i < n_specified_cpus; i++){ /* is myth_cpu_list[i] available? */ if (CPU_ISSET(myth_cpu_list[i], &cset)) { /* yes, use it */ worker_cpu[n_available_cpus] = myth_cpu_list[i]; n_available_cpus++; } } } #else /* no HAVE_SCHED_GETAFFINITY */ for (i = 0; i < n_specified_cpus; i++){ worker_cpu[n_available_cpus] = myth_cpu_list[i]; n_available_cpus++; } #endif /* HAVE_SCHED_GETAFFINITY */ if (n_available_cpus == 0) { fprintf(stderr, "myth: could not get any available CPUs. won't bind worker to cores\n"); } #if 0 /* debug */ printf("%d available CPUs:", n_available_cpus); for (i = 0; i < n_available_cpus; i++) { printf(" "); printf("%d", worker_cpu[i]); } printf("\n"); #endif /* debug */ }
void myth_init_process_affinity_info(void) { int n = myth_parse_cpu_list("MYTH_CPU_LIST", myth_cpu_list, CPU_SETSIZE); if (n == -1) { fprintf(stderr, "MYTH_CPU_LIST ignored\n"); } cpu_set_t cset; sched_getaffinity(getpid(),sizeof(cpu_set_t),&cset); int i; for (i=0;i<CPU_SETSIZE;i++){ CPU_ZERO(&worker_cpusets[i]); } available_cores=0; int sz = (n ? n : CPU_SETSIZE); for (i=0;i<sz;i++){ if (CPU_ISSET((n>0)?myth_cpu_list[i]:i,&cset)){ CPU_SET((n>0)?myth_cpu_list[i]:i,&worker_cpusets[available_cores]); available_cores++; } } #if 0 printf("%d available cores:", available_cores); for (i = 0; i < available_cores; i++) { int x; int nx = 0; printf(" "); printf("{"); for (x = 0; x < CPU_SETSIZE; x++) { if (CPU_ISSET(x, &worker_cpusets[i])) { if (nx > 0) printf(","); printf("%d", x); } } printf("}"); } printf("\n"); #endif }