LGPLv2.1 relicensing
[urcu.git] / test_rwlock_timing.c
CommitLineData
20bf310a
MD
1/*
2 * test_urcu.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
8 * Distributed under GPLv2
9 */
10
11#include <stdio.h>
12#include <pthread.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/types.h>
16#include <sys/wait.h>
17#include <unistd.h>
18#include <stdio.h>
19#include <assert.h>
20#include <sys/syscall.h>
21#include <pthread.h>
121a5d44 22#include <arch.h>
20bf310a
MD
23
24#if defined(_syscall0)
25_syscall0(pid_t, gettid)
26#elif defined(__NR_gettid)
27static inline pid_t gettid(void)
28{
29 return syscall(__NR_gettid);
30}
31#else
32#warning "use pid as tid"
33static inline pid_t gettid(void)
34{
35 return getpid();
36}
37#endif
38
20bf310a
MD
39#include "urcu.h"
40
41struct test_array {
42 int a;
43};
44
45pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
46
47static struct test_array test_array = { 8 };
48
49#define OUTER_READ_LOOP 200U
50#define INNER_READ_LOOP 100000U
51#define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
52
53#define WRITE_LOOP 2000U
54
55#define NR_READ 10
56#define NR_WRITE 9
57
58static cycles_t reader_time[NR_READ] __attribute__((aligned(128)));
59
60void *thr_reader(void *arg)
61{
62 int i, j;
63 cycles_t time1, time2;
64
65 printf("thread_begin %s, thread id : %lx, tid %lu\n",
66 "reader", pthread_self(), (unsigned long)gettid());
67 sleep(2);
68
69 time1 = get_cycles();
70 for (i = 0; i < OUTER_READ_LOOP; i++) {
71 for (j = 0; j < INNER_READ_LOOP; j++) {
72 pthread_rwlock_rdlock(&lock);
73 assert(test_array.a == 8);
74 pthread_rwlock_unlock(&lock);
75 }
76 }
77 time2 = get_cycles();
78
79 reader_time[(unsigned long)arg] = time2 - time1;
80
81 sleep(2);
82 printf("thread_end %s, thread id : %lx, tid %lu\n",
83 "reader", pthread_self(), (unsigned long)gettid());
84 return ((void*)1);
85
86}
87
88void *thr_writer(void *arg)
89{
90 int i;
91
92 printf("thread_begin %s, thread id : %lx, tid %lu\n",
93 "writer", pthread_self(), (unsigned long)gettid());
94 sleep(2);
95
96 for (i = 0; i < WRITE_LOOP; i++) {
97 pthread_rwlock_wrlock(&lock);
98 test_array.a = 8;
99 pthread_rwlock_unlock(&lock);
100 usleep(1);
101 }
102
103 printf("thread_end %s, thread id : %lx, tid %lu\n",
104 "writer", pthread_self(), (unsigned long)gettid());
105 return ((void*)2);
106}
107
108int main()
109{
110 int err;
111 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
112 void *tret;
113 int i;
114 cycles_t tot_time = 0;
115
116 printf("thread %-6s, thread id : %lx, tid %lu\n",
117 "main", pthread_self(), (unsigned long)gettid());
118
119 for (i = 0; i < NR_READ; i++) {
120 err = pthread_create(&tid_reader[i], NULL, thr_reader,
121 (void *)(long)i);
122 if (err != 0)
123 exit(1);
124 }
125 for (i = 0; i < NR_WRITE; i++) {
126 err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL);
127 if (err != 0)
128 exit(1);
129 }
130
131 sleep(10);
132
133 for (i = 0; i < NR_READ; i++) {
134 err = pthread_join(tid_reader[i], &tret);
135 if (err != 0)
136 exit(1);
137 tot_time += reader_time[i];
138 }
139 for (i = 0; i < NR_WRITE; i++) {
140 err = pthread_join(tid_writer[i], &tret);
141 if (err != 0)
142 exit(1);
143 }
144 printf("Time per read : %g cycles\n",
145 (double)tot_time / ((double)NR_READ * (double)READ_LOOP));
146
147 return 0;
148}
This page took 0.027003 seconds and 4 git commands to generate.