* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <sys/syscall.h>
+#include <sched.h>
#include "arch.h"
+/* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
+#define CACHE_LINE_SIZE 4096
+
#if defined(_syscall0)
_syscall0(pid_t, gettid)
#elif defined(__NR_gettid)
struct per_thread_lock {
pthread_mutex_t lock;
-} __attribute__((aligned(128))); /* cache-line aligned */
+} __attribute__((aligned(CACHE_LINE_SIZE))); /* cache-line aligned */
static struct per_thread_lock *per_thread_lock;
-static volatile int test_go;
+static volatile int test_go, test_stop;
-static int wdelay;
+static unsigned long wdelay;
-static struct test_array test_array = { 8 };
+static volatile struct test_array test_array = { 8 };
static unsigned long duration;
-static time_t start_time;
-static unsigned long __thread duration_interval;
-#define DURATION_TEST_DELAY_WRITE 4
-#define DURATION_TEST_DELAY_READ 100
+
+/* read-side C.S. duration, in loops */
+static unsigned long rduration;
+
+static inline void loop_sleep(unsigned long l)
+{
+ while(l-- != 0)
+ cpu_relax();
+}
+
+static int verbose_mode;
+
+#define printf_verbose(fmt, args...) \
+ do { \
+ if (verbose_mode) \
+ printf(fmt, args); \
+ } while (0)
/*
* returns 0 if test should end.
*/
static int test_duration_write(void)
{
- if (duration_interval++ >= DURATION_TEST_DELAY_WRITE) {
- duration_interval = 0;
- if (time(NULL) - start_time >= duration)
- return 0;
- }
- return 1;
+ return !test_stop;
}
static int test_duration_read(void)
{
- if (duration_interval++ >= DURATION_TEST_DELAY_READ) {
- duration_interval = 0;
- if (time(NULL) - start_time >= duration)
- return 0;
- }
- return 1;
+ return !test_stop;
}
static unsigned long long __thread nr_writes;
static unsigned long long __thread nr_reads;
-static unsigned long long __attribute__((aligned(128))) *tot_nr_writes;
-static unsigned long long __attribute__((aligned(128))) *tot_nr_reads;
+static
+unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_writes;
+static
+unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_reads;
static unsigned int nr_readers;
static unsigned int nr_writers;
{
unsigned long tidx = (unsigned long)data;
- printf("thread_begin %s, thread id : %lx, tid %lu\n",
+ printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
"reader", pthread_self(), (unsigned long)gettid());
while (!test_go)
for (;;) {
pthread_mutex_lock(&per_thread_lock[tidx].lock);
assert(test_array.a == 8);
+ if (unlikely(rduration))
+ loop_sleep(rduration);
pthread_mutex_unlock(&per_thread_lock[tidx].lock);
nr_reads++;
- if (!test_duration_read())
+ if (unlikely(!test_duration_read()))
break;
}
tot_nr_reads[tidx] = nr_reads;
- printf("thread_end %s, thread id : %lx, tid %lu\n",
+ printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
"reader", pthread_self(), (unsigned long)gettid());
return ((void*)1);
unsigned long wtidx = (unsigned long)data;
long tidx;
- printf("thread_begin %s, thread id : %lx, tid %lu\n",
+ printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
"writer", pthread_self(), (unsigned long)gettid());
while (!test_go)
for (tidx = 0; tidx < nr_readers; tidx++) {
pthread_mutex_lock(&per_thread_lock[tidx].lock);
}
+ test_array.a = 0;
test_array.a = 8;
- for (tidx = nr_readers - 1; tidx >= 0; tidx--) {
+ for (tidx = (long)nr_readers - 1; tidx >= 0; tidx--) {
pthread_mutex_unlock(&per_thread_lock[tidx].lock);
}
nr_writes++;
- if (!test_duration_write())
+ if (unlikely(!test_duration_write()))
break;
- if (wdelay)
- usleep(wdelay);
+ if (unlikely(wdelay))
+ loop_sleep(wdelay);
}
- printf("thread_end %s, thread id : %lx, tid %lu\n",
+ printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
"writer", pthread_self(), (unsigned long)gettid());
tot_nr_writes[wtidx] = nr_writes;
return ((void*)2);
printf(" [-r] [-w] (yield reader and/or writer)");
#endif
printf(" [-d delay] (writer period (us))");
+ printf(" [-c duration] (reader C.S. duration (in loops))");
+ printf(" [-v] (verbose output)");
+ printf(" [-a cpu#] [-a cpu#]... (affinity)");
printf("\n");
}
+cpu_set_t affinity;
+
int main(int argc, char **argv)
{
int err;
void *tret;
unsigned long long *count_reader, *count_writer;
unsigned long long tot_reads = 0, tot_writes = 0;
- int i;
+ int i, a;
+ int use_affinity = 0;
if (argc < 4) {
show_usage(argc, argv);
return -1;
}
+ CPU_ZERO(&affinity);
+
for (i = 4; i < argc; i++) {
if (argv[i][0] != '-')
continue;
yield_active |= YIELD_WRITE;
break;
#endif
+ case 'a':
+ if (argc < i + 2) {
+ show_usage(argc, argv);
+ return -1;
+ }
+ a = atoi(argv[++i]);
+ CPU_SET(a, &affinity);
+ use_affinity = 1;
+ printf_verbose("Adding CPU %d affinity\n", a);
+ break;
+ case 'c':
+ if (argc < i + 2) {
+ show_usage(argc, argv);
+ return -1;
+ }
+ rduration = atol(argv[++i]);
+ break;
case 'd':
if (argc < i + 2) {
show_usage(argc, argv);
return -1;
}
- wdelay = atoi(argv[++i]);
+ wdelay = atol(argv[++i]);
+ break;
+ case 'v':
+ verbose_mode = 1;
break;
}
}
- printf("running test for %lu seconds, %u readers, %u writers.\n",
+ printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
duration, nr_readers, nr_writers);
- printf("Writer delay : %u us.\n", wdelay);
- printf("thread %-6s, thread id : %lx, tid %lu\n",
+ printf_verbose("Writer delay : %lu loops.\n", wdelay);
+ 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);
exit(1);
}
- start_time = time(NULL);
- test_go = 1;
smp_mb();
+ test_go = 1;
+
+ sleep(duration);
+
+ test_stop = 1;
+
for (i = 0; i < nr_readers; i++) {
err = pthread_join(tid_reader[i], &tret);
if (err != 0)
exit(1);
tot_writes += tot_nr_writes[i];
}
-
- printf("total number of reads : %llu, writes %llu\n", tot_reads,
+
+ printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
tot_writes);
+ printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
+ "nr_writers %3u "
+ "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
+ argv[0], duration, nr_readers, rduration,
+ nr_writers, wdelay, tot_reads, tot_writes,
+ tot_reads + tot_writes);
+
free(tid_reader);
free(tid_writer);
free(count_reader);