diff --git a/lustre/utils/obd.c b/lustre/utils/obd.c index 99cb91d..3d4b62f 100644 --- a/lustre/utils/obd.c +++ b/lustre/utils/obd.c @@ -67,6 +67,7 @@ #include <string.h> #include <ctype.h> #include <lustre/liblustreapi.h> +#include <sched.h> #ifdef HAVE_ASM_PAGE_H #include <asm/page.h> /* needed for PAGE_SIZE - rread */ @@ -1489,6 +1490,97 @@ int jt_obd_test_getattr(int argc, char **argv) return rc; } +static int build_cpu_array(char *cpus, int cpu_array[]) +{ + char *tmpptr, *endptr; + int i = 0; + + tmpptr = cpus; + while (*tmpptr != '\0') { + cpu_array[i++] = strtol(tmpptr, &endptr, 10); + tmpptr = endptr; + if (*tmpptr != '\0') + tmpptr++; /* skip the , */ + } + return i; +} + +static void set_cpu_affinity(__u32 devno) +{ + cpu_set_t cpuset; + int cpu = -1, cpu_cnt = 0, mapped_cnt = 0, i; + FILE *fp; + char filename[20] = "/tmp/affinity_map"; + char buff[200], mode[10], format[20], cpus[150]; + int cpu_array[64], mapped_array[8]; + + fp = fopen(filename, "r"); + if (fp == NULL) { + fprintf(stderr, "error: can't open file %s\n", filename); + return; + } + + sprintf(format, "%u=%s", devno, "%s"); + cpu_array[0] = -1; + + while (fgets(buff, sizeof(buff), fp) != NULL) { + if (strcmp(mode, "thread") && strcmp(mode, "devno")) { + if (sscanf(buff, "mode=%s", mode) == 0) + continue; + } + + if (cpu_array[0] == -1) { + if (sscanf(buff, "cpus=%s", cpus) == 0) + continue; + cpu_cnt = build_cpu_array(cpus, cpu_array); + memset(cpus, 0, sizeof(cpus)); + } + + if (strcmp(mode, "thread") == 0) { + break; + } + + if (sscanf(buff, format, cpus) != 0) { + mapped_cnt = build_cpu_array(cpus, mapped_array); + break; + } + } + fclose(fp); + + if (!cpu_cnt) { + fprintf(stderr, "error: no cpus provided\n"); + return; + } else { + printf("set_affinity: %d cpus:", cpu_cnt); + for (i = 0; i < cpu_cnt; i++) { + printf("%d ", cpu_array[i]); + } + printf("\n"); + } + + if (strcmp(mode, "thread") == 0) { + cpu = cpu_array[getpid() % cpu_cnt]; + printf("set_affinity: mode:%s, thread:%d bind to cpu:%d\n", + mode, getpid(), cpu); + } else { + if (!mapped_cnt) { + fprintf(stderr, "error, no mapped cpus provided\n"); + return; + } + cpu = mapped_array[getpid() % mapped_cnt]; + printf("set_affinity: mode:%s, devno:%u, bind cpus:%s, " + "thread:%d, cpu:%d\n", + mode, devno, cpus, getpid(), cpu); + } + + CPU_ZERO(&cpuset); + CPU_SET(cpu, &cpuset); + + if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset) != 0) + fprintf(stderr, "error: sched_setaffinity return %d\n", + errno); +} + /* test_brw <cnt> count <r|w[r(repeat)x(noverify)]> mode <q|v|#(print interval)> verbosity @@ -1646,6 +1738,8 @@ int jt_obd_test_brw(int argc, char **argv) thr_offset += (thread - 1) * len; } + set_cpu_affinity(cur_device); + shared_data->barrier--; if (shared_data->barrier == 0) l_cond_broadcast(&shared_data->cond);