-
Notifications
You must be signed in to change notification settings - Fork 0
/
AtomicTester.cpp
287 lines (254 loc) · 7.75 KB
/
AtomicTester.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#define LOG_TAG "AtomicTester"
#include <utils/Log.h>
#include <sys/atomics.h>
#include <utils/Atomic.h>
#define USE_ATOMIC_TYPE 1 //1-cutils; 2-bionic; 3-others(mutex)
#define THREAD_COUNT 10
#define ITERATION_COUNT 500000
#define TEST_BIONIC
#ifdef TEST_BIONIC
extern int __atomic_cmpxchg(int old, int _new, volatile int *ptr);
extern int __atomic_swap(int _new, volatile int *ptr);
extern int __atomic_dec(volatile int *ptr);
extern int __atomic_inc(volatile int *ptr);
#endif
static pthread_mutex_t waitLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t waitCond = PTHREAD_COND_INITIALIZER;
#define TEST_MUTEX // not define it will cause wrong result when multi-threads ops the same var.
#ifdef TEST_MUTEX
static pthread_mutex_t incLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t decLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t addLock = PTHREAD_MUTEX_INITIALIZER;
#endif
static volatile int threadsStarted = 0;
/* results */
static int incTest = 0;
static int decTest = 0;
static int addTest = 0;
static int andTest = 0;
static int orTest = 0;
static int casTest = 0;
static int failingCasTest = 0;
/*
* Get a relative time value.
*/
static int64_t getRelativeTimeNsec()
{
#define HAVE_POSIX_CLOCKS
#ifdef HAVE_POSIX_CLOCKS
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (int64_t) now.tv_sec*1000000000LL + now.tv_nsec;
#else
struct timeval now;
gettimeofday(&now, NULL);
return (int64_t) now.tv_sec*1000000000LL + now.tv_usec * 1000LL;
#endif
}
/*
* Non-atomic implementations, for comparison.
*
* If these get inlined the compiler may figure out what we're up to and
* completely elide the operations.
*/
#define USE_NOINLINE 0
#if USE_NOINLINE == 1
static void incr() __attribute__((noinline));
static void decr() __attribute__((noinline));
static void add(int addVal) __attribute__((noinline));
static int compareAndSwap(int oldVal, int newVal, int* addr) __attribute__((noinline));
#else
static void incr() __attribute__((always_inline));
static void decr() __attribute__((always_inline));
static void add(int addVal) __attribute__((always_inline));
static int compareAndSwap(int oldVal, int newVal, int* addr) __attribute__((always_inline));
#endif
static void incr()
{
#ifdef TEST_MUTEX
pthread_mutex_lock(&incLock);
#endif
incTest++;
#ifdef TEST_MUTEX
pthread_mutex_unlock(&incLock);
#endif
}
static void decr()
{
#ifdef TEST_MUTEX
pthread_mutex_lock(&decLock);
#endif
decTest--;
#ifdef TEST_MUTEX
pthread_mutex_unlock(&decLock);
#endif
}
static void add(int32_t addVal)
{
#ifdef TEST_MUTEX
pthread_mutex_lock(&addLock);
#endif
addTest += addVal;
#ifdef TEST_MUTEX
pthread_mutex_unlock(&addLock);
#endif
}
static int compareAndSwap(int32_t oldVal, int32_t newVal, int32_t* addr)
{
#ifdef TEST_MUTEX
pthread_mutex_lock(&addLock);
#endif
if (*addr == oldVal) {
*addr = newVal;
#ifdef TEST_MUTEX
pthread_mutex_unlock(&addLock);
#endif
return 0;
}
#ifdef TEST_MUTEX
pthread_mutex_unlock(&addLock);
#endif
return 1;
}
/*
* Exercise several of the atomic ops.
*/
static void doAtomicTest(int num)
{
int addVal = (num & 0x01) + 1;
int i;
for (i = 0; i < ITERATION_COUNT; i++) {
if (USE_ATOMIC_TYPE == 1) { //cutils, duration=865ms on ARM_CPU_PART_CORTEX_A53
android_atomic_inc(&incTest); // wrapper for android_atomic_add(1, addr)
android_atomic_dec(&decTest);
android_atomic_add(addVal, &addTest);
int val;
do {
val = casTest;
} while (android_atomic_release_cas(val, val+3, &casTest) != 0);
//} while (android_atomic_cmpxchg(val, val+3, &casTest) != 0);
do {
val = casTest;
} while (android_atomic_acquire_cas(val, val-1, &casTest) != 0); // same with release_cas
} else if (USE_ATOMIC_TYPE == 2) { //bionic, duration=650ms
__atomic_inc(&incTest); // wrapper for __sync_fetch_and_add(addr, 1), which is gcc built-in API
__atomic_dec(&decTest);
__sync_fetch_and_add(&addTest, addVal);
int val;
do {
val = casTest;
} while (android_atomic_release_cas(val, val+3, &casTest) != 0);
do {
val = casTest;
} while (android_atomic_acquire_cas(val, val-1, &casTest) != 0);
} else { //mutex, noinline_duration=3500ms, inline_duration=28000ms
//usleep(1); // if no sleep, result may be right; when have sleep, result must be wrong!
incr();
decr();
add(addVal);
int val;
do {
val = casTest;
} while (compareAndSwap(val, val+3, &casTest) != 0);
do {
val = casTest;
} while (compareAndSwap(val, val-1, &casTest) != 0);
}
}
}
/*
* Entry point for multi-thread test.
*/
static void* atomicTest(void* arg)
{
pthread_mutex_lock(&waitLock);
threadsStarted++;
ALOGD("thread[%d] wait to run...", (int)arg);
pthread_cond_wait(&waitCond, &waitLock);
pthread_mutex_unlock(&waitLock);
doAtomicTest((int) arg);
return NULL;
}
static void doBionicTest()
{
#ifdef TEST_BIONIC
/*
* Quick function test on the bionic ops.
*/
int prev;
int initVal = 7;
prev = __atomic_inc(&initVal);
__atomic_inc(&initVal);
__atomic_inc(&initVal);
ALOGD("bionic __atomic_inc: %d -> %d\n", prev, initVal);
prev = __atomic_dec(&initVal);
__atomic_dec(&initVal);
__atomic_dec(&initVal);
ALOGD("bionic __atomic_dec: %d -> %d\n", prev, initVal);
prev = __atomic_swap(27, &initVal);
ALOGD("bionic __atomic_swap: %d -> %d\n", prev, initVal);
int swap_ret = __atomic_cmpxchg(27, 72, &initVal);
ALOGD("bionic __atomic_cmpxchg: %d (%d)\n", initVal, swap_ret);
#endif
}
int main(void)
{
ALOGW("-------------AtomicTester begin--------------");
pthread_t threads[THREAD_COUNT];
void *(*startRoutine)(void*) = atomicTest;
int64_t startWhen, endWhen;
#if defined(__ARM_ARCH__)
ALOGD("__ARM_ARCH__ is %d", __ARM_ARCH__);
#endif
#if defined(ANDROID_SMP)
ALOGD("ANDROID_SMP is %d", ANDROID_SMP);
#endif
ALOGD("Creating threads...");
int i;
for (i = 0; i < THREAD_COUNT; i++) {
void* arg = (void*) i;
if (pthread_create(&threads[i], NULL, startRoutine, arg) != 0) {
ALOGD("thread create failed");
}
}
/* wait for all the threads to reach the starting line */
while (1) {
pthread_mutex_lock(&waitLock);
if (threadsStarted == THREAD_COUNT) {
ALOGD("Starting test!");
startWhen = getRelativeTimeNsec();
pthread_cond_broadcast(&waitCond);
pthread_mutex_unlock(&waitLock);
break;
}
pthread_mutex_unlock(&waitLock);
usleep(100000);
}
for (i = 0; i < THREAD_COUNT; i++) {
void* retval;
if (pthread_join(threads[i], &retval) != 0) {
ALOGE("thread join (%d) failed\n", i);
}
}
endWhen = getRelativeTimeNsec();
if (USE_ATOMIC_TYPE == 3) {
ALOGD("test pthread_mutex_t with %s", USE_NOINLINE==1 ? "noinline":"always_inline");
}
ALOGD("TYPE=%d, All threads stopped, duration time is %.6fms\n",
USE_ATOMIC_TYPE, (endWhen - startWhen) / 1000000.0);
/*
* Show results; expecting:
*
* incTest = 5000000
* decTest = -5000000
* addTest = 7500000
* casTest = 10000000
*/
ALOGD("incTest = %d\n", incTest);
ALOGD("decTest = %d\n", decTest);
ALOGD("addTest = %d\n", addTest);
ALOGD("casTest = %d\n", casTest);
doBionicTest();
ALOGW("-------------AtomicTester end--------------");
return 0;
}