Deal with kernel affinity bug for tests
authorMathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Mon, 22 Jun 2009 01:32:05 +0000 (21:32 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Mon, 22 Jun 2009 01:32:05 +0000 (21:32 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
test_mutex.c
test_perthreadlock.c
test_qsbr.c
test_qsbr_gc.c
test_rwlock.c
test_urcu.c
test_urcu_gc.c

index 8b480403fcabdd4adccfe0774c51a9d8a45cb42d..4cf1d32c29151e54f1aa3dd67461bc4f3e1b8098 100644 (file)
@@ -38,6 +38,9 @@
 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
 #define CACHE_LINE_SIZE 4096
 
+/* hardcoded number of CPUs */
+#define NR_CPUS 16384
+
 #if defined(_syscall0)
 _syscall0(pid_t, gettid)
 #elif defined(__NR_gettid)
@@ -91,6 +94,26 @@ static int verbose_mode;
                        printf(fmt, args);      \
        } while (0)
 
+static unsigned int cpu_affinities[NR_CPUS];
+static unsigned int next_aff = 0;
+static int use_affinity = 0;
+
+static void set_affinity(void)
+{
+       cpu_set_t mask;
+       int cpu;
+
+       if (!use_affinity)
+               return;
+
+       cpu = cpu_affinities[next_aff++];
+       CPU_ZERO(&mask);
+       CPU_SET(cpu, &mask);
+       sched_setaffinity(0, sizeof(mask), &mask);
+}
+
+
+
 /*
  * returns 0 if test should end.
  */
@@ -145,6 +168,8 @@ void *thr_reader(void *data)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "reader", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        while (!test_go)
        {
        }
@@ -174,6 +199,8 @@ void *thr_writer(void *data)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "writer", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        while (!test_go)
        {
        }
@@ -210,8 +237,6 @@ void show_usage(int argc, char **argv)
        printf("\n");
 }
 
-cpu_set_t affinity;
-
 int main(int argc, char **argv)
 {
        int err;
@@ -220,7 +245,6 @@ int main(int argc, char **argv)
        unsigned long long *count_reader, *count_writer;
        unsigned long long tot_reads = 0, tot_writes = 0;
        int i, a;
-       int use_affinity = 0;
 
        if (argc < 4) {
                show_usage(argc, argv);
@@ -246,8 +270,6 @@ int main(int argc, char **argv)
                return -1;
        }
 
-       CPU_ZERO(&affinity);
-
        for (i = 4; i < argc; i++) {
                if (argv[i][0] != '-')
                        continue;
@@ -266,7 +288,7 @@ int main(int argc, char **argv)
                                return -1;
                        }
                        a = atoi(argv[++i]);
-                       CPU_SET(a, &affinity);
+                       cpu_affinities[next_aff++] = a;
                        use_affinity = 1;
                        printf_verbose("Adding CPU %d affinity\n", a);
                        break;
@@ -297,12 +319,6 @@ int main(int argc, char **argv)
        printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
                        "main", pthread_self(), (unsigned long)gettid());
 
-       if (use_affinity
-           && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
-               perror("sched_setaffinity");
-               exit(-1);
-       }
-
        tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
        tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
        count_reader = malloc(sizeof(*count_reader) * nr_readers);
@@ -310,6 +326,8 @@ int main(int argc, char **argv)
        tot_nr_reads = malloc(sizeof(*tot_nr_reads) * nr_readers);
        tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
 
+       next_aff = 0;
+
        for (i = 0; i < nr_readers; i++) {
                err = pthread_create(&tid_reader[i], NULL, thr_reader,
                                     (void *)(long)i);
index 3fc4a537f89c98f9556fe706ddb9eb65b85da675..5bbe72b1637af9adbaa099cb4c4631a74a3fc50e 100644 (file)
@@ -38,6 +38,9 @@
 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
 #define CACHE_LINE_SIZE 4096
 
+/* hardcoded number of CPUs */
+#define NR_CPUS 16384
+
 #if defined(_syscall0)
 _syscall0(pid_t, gettid)
 #elif defined(__NR_gettid)
@@ -95,6 +98,26 @@ static int verbose_mode;
                        printf(fmt, args);      \
        } while (0)
 
+static unsigned int cpu_affinities[NR_CPUS];
+static unsigned int next_aff = 0;
+static int use_affinity = 0;
+
+static void set_affinity(void)
+{
+       cpu_set_t mask;
+       int cpu;
+
+       if (!use_affinity)
+               return;
+
+       cpu = cpu_affinities[next_aff++];
+       CPU_ZERO(&mask);
+       CPU_SET(cpu, &mask);
+       sched_setaffinity(0, sizeof(mask), &mask);
+}
+
+
+
 /*
  * returns 0 if test should end.
  */
@@ -149,6 +172,8 @@ void *thr_reader(void *data)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "reader", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        while (!test_go)
        {
        }
@@ -179,6 +204,8 @@ void *thr_writer(void *data)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "writer", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        while (!test_go)
        {
        }
@@ -219,8 +246,6 @@ void show_usage(int argc, char **argv)
        printf("\n");
 }
 
-cpu_set_t affinity;
-
 int main(int argc, char **argv)
 {
        int err;
@@ -229,7 +254,6 @@ int main(int argc, char **argv)
        unsigned long long *count_reader, *count_writer;
        unsigned long long tot_reads = 0, tot_writes = 0;
        int i, a;
-       int use_affinity = 0;
 
        if (argc < 4) {
                show_usage(argc, argv);
@@ -255,8 +279,6 @@ int main(int argc, char **argv)
                return -1;
        }
 
-       CPU_ZERO(&affinity);
-
        for (i = 4; i < argc; i++) {
                if (argv[i][0] != '-')
                        continue;
@@ -275,7 +297,7 @@ int main(int argc, char **argv)
                                return -1;
                        }
                        a = atoi(argv[++i]);
-                       CPU_SET(a, &affinity);
+                       cpu_affinities[next_aff++] = a;
                        use_affinity = 1;
                        printf_verbose("Adding CPU %d affinity\n", a);
                        break;
@@ -306,12 +328,6 @@ int main(int argc, char **argv)
        printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
                        "main", pthread_self(), (unsigned long)gettid());
 
-       if (use_affinity
-           && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
-               perror("sched_setaffinity");
-               exit(-1);
-       }
-
        tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
        tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
        count_reader = malloc(sizeof(*count_reader) * nr_readers);
@@ -320,6 +336,8 @@ int main(int argc, char **argv)
        tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
        per_thread_lock = malloc(sizeof(*per_thread_lock) * nr_readers);
 
+       next_aff = 0;
+
        for (i = 0; i < nr_readers; i++) {
                err = pthread_create(&tid_reader[i], NULL, thr_reader,
                                     (void *)(long)i);
index 6830fe1d2c1c77f144f1f28760bf2a2123cff2c4..af5ccfd58317d06d5c1e2d6c37ac0f268b0c5e16 100644 (file)
@@ -38,6 +38,9 @@
 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
 #define CACHE_LINE_SIZE 4096
 
+/* hardcoded number of CPUs */
+#define NR_CPUS 16384
+
 #if defined(_syscall0)
 _syscall0(pid_t, gettid)
 #elif defined(__NR_gettid)
@@ -85,6 +88,26 @@ static int verbose_mode;
                        printf(fmt, args);      \
        } while (0)
 
+static unsigned int cpu_affinities[NR_CPUS];
+static unsigned int next_aff = 0;
+static int use_affinity = 0;
+
+static void set_affinity(void)
+{
+       cpu_set_t mask;
+       int cpu;
+
+       if (!use_affinity)
+               return;
+
+       cpu = cpu_affinities[next_aff++];
+       CPU_ZERO(&mask);
+       CPU_SET(cpu, &mask);
+       sched_setaffinity(0, sizeof(mask), &mask);
+}
+
+
+
 /*
  * returns 0 if test should end.
  */
@@ -171,6 +194,8 @@ void *thr_reader(void *_count)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "reader", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        rcu_register_thread();
 
        while (!test_go)
@@ -212,6 +237,8 @@ void *thr_writer(void *_count)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "writer", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        while (!test_go)
        {
        }
@@ -256,8 +283,6 @@ void show_usage(int argc, char **argv)
        printf("\n");
 }
 
-cpu_set_t affinity;
-
 int main(int argc, char **argv)
 {
        int err;
@@ -266,7 +291,6 @@ int main(int argc, char **argv)
        unsigned long long *count_reader, *count_writer;
        unsigned long long tot_reads = 0, tot_writes = 0;
        int i, a;
-       int use_affinity = 0;
 
        if (argc < 4) {
                show_usage(argc, argv);
@@ -291,8 +315,6 @@ int main(int argc, char **argv)
                return -1;
        }
 
-       CPU_ZERO(&affinity);
-
        for (i = 4; i < argc; i++) {
                if (argv[i][0] != '-')
                        continue;
@@ -311,7 +333,7 @@ int main(int argc, char **argv)
                                return -1;
                        }
                        a = atoi(argv[++i]);
-                       CPU_SET(a, &affinity);
+                       cpu_affinities[next_aff++] = a;
                        use_affinity = 1;
                        printf_verbose("Adding CPU %d affinity\n", a);
                        break;
@@ -342,34 +364,14 @@ int main(int argc, char **argv)
        printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
                        "main", pthread_self(), (unsigned long)gettid());
 
-       if (use_affinity) {
-               for (i = 0; i < CPU_SETSIZE; i++)
-                       if (CPU_ISSET(i, &affinity))
-                               printf_verbose("About to set affinity "
-                                              "to CPU : %d\n", i);
-
-               if (sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
-                       perror("sched_setaffinity");
-                       exit(-1);
-               }
-
-               if (sched_getaffinity(0, sizeof(affinity), &affinity) < 0) {
-                       perror("sched_getaffinity");
-                       exit(-1);
-               }
-
-               for (i = 0; i < CPU_SETSIZE; i++)
-                       if (CPU_ISSET(i, &affinity))
-                               printf_verbose("Effectively set affinity "
-                                              "to CPU : %d\n", i);
-       }
-
        test_array = malloc(sizeof(*test_array) * ARRAY_SIZE);
        tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
        tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
        count_reader = malloc(sizeof(*count_reader) * nr_readers);
        count_writer = malloc(sizeof(*count_writer) * nr_writers);
 
+       next_aff = 0;
+
        for (i = 0; i < nr_readers; i++) {
                err = pthread_create(&tid_reader[i], NULL, thr_reader,
                                     &count_reader[i]);
index c5cb9b168a905206d9bd837c9e9edc471d456dbd..0b2d5df8ddd8dd13cc36c1b40998f78e2a646322 100644 (file)
@@ -38,6 +38,9 @@
 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
 #define CACHE_LINE_SIZE 4096
 
+/* hardcoded number of CPUs */
+#define NR_CPUS 16384
+
 #if defined(_syscall0)
 _syscall0(pid_t, gettid)
 #elif defined(__NR_gettid)
@@ -94,6 +97,26 @@ static int verbose_mode;
                        printf(fmt, args);      \
        } while (0)
 
+static unsigned int cpu_affinities[NR_CPUS];
+static unsigned int next_aff = 0;
+static int use_affinity = 0;
+
+static void set_affinity(void)
+{
+       cpu_set_t mask;
+       int cpu;
+
+       if (!use_affinity)
+               return;
+
+       cpu = cpu_affinities[next_aff++];
+       CPU_ZERO(&mask);
+       CPU_SET(cpu, &mask);
+       sched_setaffinity(0, sizeof(mask), &mask);
+}
+
+
+
 /*
  * returns 0 if test should end.
  */
@@ -147,6 +170,8 @@ void *thr_reader(void *_count)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "reader", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        rcu_register_thread();
 
        while (!test_go)
@@ -219,6 +244,8 @@ void *thr_writer(void *data)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "writer", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        while (!test_go)
        {
        }
@@ -260,8 +287,6 @@ void show_usage(int argc, char **argv)
        printf("\n");
 }
 
-cpu_set_t affinity;
-
 int main(int argc, char **argv)
 {
        int err;
@@ -270,7 +295,6 @@ int main(int argc, char **argv)
        unsigned long long *count_reader;
        unsigned long long tot_reads = 0, tot_writes = 0;
        int i, a;
-       int use_affinity = 0;
 
        if (argc < 4) {
                show_usage(argc, argv);
@@ -295,8 +319,6 @@ int main(int argc, char **argv)
                return -1;
        }
 
-       CPU_ZERO(&affinity);
-
        for (i = 4; i < argc; i++) {
                if (argv[i][0] != '-')
                        continue;
@@ -315,7 +337,7 @@ int main(int argc, char **argv)
                                return -1;
                        }
                        a = atoi(argv[++i]);
-                       CPU_SET(a, &affinity);
+                       cpu_affinities[next_aff++] = a;
                        use_affinity = 1;
                        printf_verbose("Adding CPU %d affinity\n", a);
                        break;
@@ -353,12 +375,6 @@ int main(int argc, char **argv)
        printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
                        "main", pthread_self(), (unsigned long)gettid());
 
-       if (use_affinity
-           && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
-               perror("sched_setaffinity");
-               exit(-1);
-       }
-
        tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
        tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
        count_reader = malloc(sizeof(*count_reader) * nr_readers);
@@ -375,6 +391,8 @@ int main(int argc, char **argv)
        for (i = 0; i < nr_writers; i++)
                pending_reclaims[i].head = pending_reclaims[i].queue;
 
+       next_aff = 0;
+
        for (i = 0; i < nr_readers; i++) {
                err = pthread_create(&tid_reader[i], NULL, thr_reader,
                                     &count_reader[i]);
index 733263be628be26b443678eb7d5ec5d687082952..b7909019f8b1df843e25512bad4367d427a1bbfe 100644 (file)
@@ -38,6 +38,9 @@
 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
 #define CACHE_LINE_SIZE 4096
 
+/* hardcoded number of CPUs */
+#define NR_CPUS 16384
+
 #if defined(_syscall0)
 _syscall0(pid_t, gettid)
 #elif defined(__NR_gettid)
@@ -91,6 +94,26 @@ static int verbose_mode;
                        printf(fmt, args);      \
        } while (0)
 
+static unsigned int cpu_affinities[NR_CPUS];
+static unsigned int next_aff = 0;
+static int use_affinity = 0;
+
+static void set_affinity(void)
+{
+       cpu_set_t mask;
+       int cpu;
+
+       if (!use_affinity)
+               return;
+
+       cpu = cpu_affinities[next_aff++];
+       CPU_ZERO(&mask);
+       CPU_SET(cpu, &mask);
+       sched_setaffinity(0, sizeof(mask), &mask);
+}
+
+
+
 /*
  * returns 0 if test should end.
  */
@@ -140,6 +163,8 @@ void *thr_reader(void *_count)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "reader", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        while (!test_go)
        {
        }
@@ -169,6 +194,8 @@ void *thr_writer(void *_count)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "writer", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        while (!test_go)
        {
        }
@@ -205,8 +232,6 @@ void show_usage(int argc, char **argv)
        printf("\n");
 }
 
-cpu_set_t affinity;
-
 int main(int argc, char **argv)
 {
        int err;
@@ -215,7 +240,6 @@ int main(int argc, char **argv)
        unsigned long long *count_reader, *count_writer;
        unsigned long long tot_reads = 0, tot_writes = 0;
        int i, a;
-       int use_affinity = 0;
 
        if (argc < 4) {
                show_usage(argc, argv);
@@ -241,8 +265,6 @@ int main(int argc, char **argv)
                return -1;
        }
 
-       CPU_ZERO(&affinity);
-
        for (i = 4; i < argc; i++) {
                if (argv[i][0] != '-')
                        continue;
@@ -261,7 +283,7 @@ int main(int argc, char **argv)
                                return -1;
                        }
                        a = atoi(argv[++i]);
-                       CPU_SET(a, &affinity);
+                       cpu_affinities[next_aff++] = a;
                        use_affinity = 1;
                        printf_verbose("Adding CPU %d affinity\n", a);
                        break;
@@ -291,17 +313,14 @@ int main(int argc, char **argv)
        printf_verbose("Reader duration : %lu loops.\n", rduration);
        printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
                        "main", pthread_self(), (unsigned long)gettid());
-       if (use_affinity
-           && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
-               perror("sched_setaffinity");
-               exit(-1);
-       }
 
        tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
        tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
        count_reader = malloc(sizeof(*count_reader) * nr_readers);
        count_writer = malloc(sizeof(*count_writer) * nr_writers);
 
+       next_aff = 0;
+
        for (i = 0; i < nr_readers; i++) {
                err = pthread_create(&tid_reader[i], NULL, thr_reader,
                                     &count_reader[i]);
index fc9e8b539a9b3a9f21eb05702230602dd760b6d0..96dac983336bfa87a57fc861a6b780c843378fba 100644 (file)
@@ -38,6 +38,9 @@
 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
 #define CACHE_LINE_SIZE 4096
 
+/* hardcoded number of CPUs */
+#define NR_CPUS 16384
+
 #if defined(_syscall0)
 _syscall0(pid_t, gettid)
 #elif defined(__NR_gettid)
@@ -89,6 +92,37 @@ static int verbose_mode;
                        printf(fmt, args);      \
        } while (0)
 
+static unsigned int cpu_affinities[NR_CPUS];
+static unsigned int next_aff = 0;
+static int use_affinity = 0;
+
+pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void set_affinity(void)
+{
+       cpu_set_t mask;
+       int cpu;
+       int ret;
+
+       if (!use_affinity)
+               return;
+
+       ret = pthread_mutex_lock(&affinity_mutex);
+       if (ret) {
+               perror("Error in pthread mutex lock");
+               exit(-1);
+       }
+       cpu = cpu_affinities[next_aff++];
+       ret = pthread_mutex_unlock(&affinity_mutex);
+       if (ret) {
+               perror("Error in pthread mutex unlock");
+               exit(-1);
+       }
+       CPU_ZERO(&mask);
+       CPU_SET(cpu, &mask);
+       sched_setaffinity(0, sizeof(mask), &mask);
+}
+
 /*
  * returns 0 if test should end.
  */
@@ -175,6 +209,8 @@ void *thr_reader(void *_count)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "reader", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        rcu_register_thread();
 
        while (!test_go)
@@ -213,6 +249,8 @@ void *thr_writer(void *_count)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "writer", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        while (!test_go)
        {
        }
@@ -257,8 +295,6 @@ void show_usage(int argc, char **argv)
        printf("\n");
 }
 
-cpu_set_t affinity;
-
 int main(int argc, char **argv)
 {
        int err;
@@ -267,7 +303,6 @@ int main(int argc, char **argv)
        unsigned long long *count_reader, *count_writer;
        unsigned long long tot_reads = 0, tot_writes = 0;
        int i, a;
-       int use_affinity = 0;
 
        if (argc < 4) {
                show_usage(argc, argv);
@@ -292,8 +327,6 @@ int main(int argc, char **argv)
                return -1;
        }
 
-       CPU_ZERO(&affinity);
-
        for (i = 4; i < argc; i++) {
                if (argv[i][0] != '-')
                        continue;
@@ -312,7 +345,7 @@ int main(int argc, char **argv)
                                return -1;
                        }
                        a = atoi(argv[++i]);
-                       CPU_SET(a, &affinity);
+                       cpu_affinities[next_aff++] = a;
                        use_affinity = 1;
                        printf_verbose("Adding CPU %d affinity\n", a);
                        break;
@@ -343,18 +376,14 @@ int main(int argc, char **argv)
        printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
                        "main", pthread_self(), (unsigned long)gettid());
 
-       if (use_affinity
-           && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
-               perror("sched_setaffinity");
-               exit(-1);
-       }
-
        test_array = malloc(sizeof(*test_array) * ARRAY_SIZE);
        tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
        tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
        count_reader = malloc(sizeof(*count_reader) * nr_readers);
        count_writer = malloc(sizeof(*count_writer) * nr_writers);
 
+       next_aff = 0;
+
        for (i = 0; i < nr_readers; i++) {
                err = pthread_create(&tid_reader[i], NULL, thr_reader,
                                     &count_reader[i]);
index ee936f04d7633dd39d6316d7f9d21c1012f4490d..f1cce248a8b604709ffcb9e3a265c1f983bc03f7 100644 (file)
@@ -38,6 +38,9 @@
 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
 #define CACHE_LINE_SIZE 4096
 
+/* hardcoded number of CPUs */
+#define NR_CPUS 16384
+
 #if defined(_syscall0)
 _syscall0(pid_t, gettid)
 #elif defined(__NR_gettid)
@@ -98,6 +101,26 @@ static int verbose_mode;
                        printf(fmt, args);      \
        } while (0)
 
+static unsigned int cpu_affinities[NR_CPUS];
+static unsigned int next_aff = 0;
+static int use_affinity = 0;
+
+static void set_affinity(void)
+{
+       cpu_set_t mask;
+       int cpu;
+
+       if (!use_affinity)
+               return;
+
+       cpu = cpu_affinities[next_aff++];
+       CPU_ZERO(&mask);
+       CPU_SET(cpu, &mask);
+       sched_setaffinity(0, sizeof(mask), &mask);
+}
+
+
+
 /*
  * returns 0 if test should end.
  */
@@ -151,6 +174,8 @@ void *thr_reader(void *_count)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "reader", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        rcu_register_thread();
 
        while (!test_go)
@@ -220,6 +245,8 @@ void *thr_writer(void *data)
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "writer", pthread_self(), (unsigned long)gettid());
 
+       set_affinity();
+
        while (!test_go)
        {
        }
@@ -261,8 +288,6 @@ void show_usage(int argc, char **argv)
        printf("\n");
 }
 
-cpu_set_t affinity;
-
 int main(int argc, char **argv)
 {
        int err;
@@ -271,7 +296,6 @@ int main(int argc, char **argv)
        unsigned long long *count_reader;
        unsigned long long tot_reads = 0, tot_writes = 0;
        int i, a;
-       int use_affinity = 0;
 
        if (argc < 4) {
                show_usage(argc, argv);
@@ -296,8 +320,6 @@ int main(int argc, char **argv)
                return -1;
        }
 
-       CPU_ZERO(&affinity);
-
        for (i = 4; i < argc; i++) {
                if (argv[i][0] != '-')
                        continue;
@@ -316,7 +338,7 @@ int main(int argc, char **argv)
                                return -1;
                        }
                        a = atoi(argv[++i]);
-                       CPU_SET(a, &affinity);
+                       cpu_affinities[next_aff++] = a;
                        use_affinity = 1;
                        printf_verbose("Adding CPU %d affinity\n", a);
                        break;
@@ -354,12 +376,6 @@ int main(int argc, char **argv)
        printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
                        "main", pthread_self(), (unsigned long)gettid());
 
-       if (use_affinity
-           && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
-               perror("sched_setaffinity");
-               exit(-1);
-       }
-
        tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
        tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
        count_reader = malloc(sizeof(*count_reader) * nr_readers);
@@ -376,6 +392,8 @@ int main(int argc, char **argv)
        for (i = 0; i < nr_writers; i++)
                pending_reclaims[i].head = pending_reclaims[i].queue;
 
+       next_aff = 0;
+
        for (i = 0; i < nr_readers; i++) {
                err = pthread_create(&tid_reader[i], NULL, thr_reader,
                                     &count_reader[i]);
This page took 0.03675 seconds and 4 git commands to generate.