-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtlock.c
496 lines (434 loc) · 11.1 KB
/
htlock.c
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* this file is taken from libslock (https://github.com/tudordavid/libslock) */
/*
* File: htlock.c
* Author: Vasileios Trigonakis <[email protected]>
*
* Description: an numa-aware hierarchical ticket lock
* The htlock contains N local ticket locks (N = number of memory
* nodes) and 1 global ticket lock. A thread always tries to acquire
* the local ticket lock first. If there isn't any (local) available,
* it enqueues for acquiring the global ticket lock and at the same
* time it "gives" NB_TICKETS_LOCAL tickets to the local ticket lock,
* so that if more threads from the same socket try to acquire the lock,
* they will enqueue on the local lock, without even accessing the
* global one.
*
* The MIT License (MIT)
*
* Copyright (c) 2013 Vasileios Trigonakis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "htlock.h"
__thread uint32_t my_node, my_id;
htlock_t*
create_htlock()
{
htlock_t* htl = NULL;
#ifdef __sparc__
htl = memalign(CACHE_LINE_SIZE, sizeof(htlock_t));
if (htl==NULL)
{
fprintf(stderr,"Error @ memalign : create htlock\n");
}
#else
if (posix_memalign((void**) &htl, CACHE_LINE_SIZE, sizeof(htlock_t)) < 0)
{
fprintf(stderr, "Error @ posix_memalign : create_htlock\n");
}
#endif
assert(htl != NULL);
#ifdef __sparc__
htl->global = memalign(CACHE_LINE_SIZE, sizeof(htlock_global_t));
if (htl==NULL)
{
fprintf(stderr,"Error @ memalign : create htlock\n");
}
#else
if (posix_memalign((void**) &htl->global, CACHE_LINE_SIZE, sizeof(htlock_global_t)) < 0)
{
fprintf(stderr, "Error @ posix_memalign : create_htlock\n");
}
#endif
assert(htl->global != NULL);
uint32_t s;
for (s = 0; s < NUMBER_OF_SOCKETS; s++)
{
#if defined(__x86_64__) && defined(PLATFORM_NUMA)
numa_set_preferred(s);
htl->local[s] = (htlock_local_t*) numa_alloc_onnode(sizeof(htlock_local_t), s);
#else
htl->local[s] = (htlock_local_t*) malloc(sizeof(htlock_local_t));
#endif
assert(htl->local != NULL);
}
#if defined(__x86_64__) && defined(PLATFORM_NUMA)
numa_set_preferred(my_node);
#endif
htl->global->cur = 0;
htl->global->nxt = 0;
uint32_t n;
for (n = 0; n < NUMBER_OF_SOCKETS; n++)
{
htl->local[n]->cur = NB_TICKETS_LOCAL;
htl->local[n]->nxt = 0;
}
return htl;
}
void
init_htlock(htlock_t* htl)
{
assert(htl != NULL);
htl->global->cur = 0;
htl->global->nxt = 0;
uint32_t n;
for (n = 0; n < NUMBER_OF_SOCKETS; n++)
{
htl->local[n]->cur = NB_TICKETS_LOCAL;
htl->local[n]->nxt = 0;
}
}
void
init_alloc_htlock(htlock_t* htl)
{
assert(htl != NULL);
#ifdef __sparc__
htl->global = memalign(CACHE_LINE_SIZE, sizeof(htlock_global_t));
if (htl==NULL)
{
fprintf(stderr,"Error @ memalign : create htlock\n");
}
#else
if (posix_memalign((void**) &htl->global, CACHE_LINE_SIZE, sizeof(htlock_global_t)) < 0)
{
fprintf(stderr, "Error @ posix_memalign : create_htlock\n");
}
#endif
assert(htl->global != NULL);
uint32_t s;
for (s = 0; s < NUMBER_OF_SOCKETS; s++)
{
#if defined(__x86_64__) && defined(PLATFORM_NUMA)
numa_set_preferred(s);
htl->local[s] = (htlock_local_t*) numa_alloc_onnode(sizeof(htlock_local_t), s);
#else
htl->local[s] = (htlock_local_t*) malloc(sizeof(htlock_local_t));
#endif
assert(htl->local != NULL);
}
#if defined(__x86_64__) && defined(PLATFORM_NUMA)
numa_set_preferred(my_node);
#endif
htl->global->cur = 0;
htl->global->nxt = 0;
uint32_t n;
for (n = 0; n < NUMBER_OF_SOCKETS; n++)
{
htl->local[n]->cur = NB_TICKETS_LOCAL;
htl->local[n]->nxt = 0;
}
}
void
init_thread_htlocks(uint32_t phys_core)
{
set_cpu(phys_core);
#ifdef XEON
__sync_synchronize();
uint32_t real_core_num = 0;
uint32_t i;
for (i = 0; i < (NUMBER_OF_SOCKETS * CORES_PER_SOCKET); i++)
{
if (the_cores[i]==phys_core)
{
real_core_num = i;
break;
}
}
MEM_BARRIER;
my_id = real_core_num;
my_node = get_cluster(phys_core);
#else
my_id = phys_core;
my_node = get_cluster(phys_core);
#endif
/* printf("id %3d / phys_core %3d / node %d \n", my_id, phys_core, my_node); */
}
uint32_t
is_free_hticket(htlock_t* htl)
{
htlock_global_t* glb = htl->global;
#if defined(OPTERON_OPTIMIZE)
PREFETCHW(glb);
#endif
if (glb->cur == glb->nxt)
{
return 1;
}
return 0;
}
static htlock_t*
create_htlock_no_alloc(htlock_t* htl, htlock_local_t* locals[NUMBER_OF_SOCKETS], size_t offset)
{
#ifdef __sparc__
htl->global = memalign(CACHE_LINE_SIZE, sizeof(htlock_global_t));
if (htl==NULL)
{
fprintf(stderr,"Error @ memalign : create htlock\n");
}
#else
if (posix_memalign((void**) &htl->global, CACHE_LINE_SIZE, sizeof(htlock_global_t)) < 0)
{
fprintf(stderr, "Error @ posix_memalign : create_htlock\n");
}
#endif
assert(htl->global != NULL);
uint32_t s;
for (s = 0; s < NUMBER_OF_SOCKETS; s++)
{
htl->local[s] = locals[s] + offset;
}
htl->global->cur = 0;
htl->global->nxt = 0;
uint32_t n;
for (n = 0; n < NUMBER_OF_SOCKETS; n++)
{
htl->local[n]->cur = NB_TICKETS_LOCAL;
htl->local[n]->nxt = 0;
}
return htl;
}
htlock_t*
init_htlocks(uint32_t num_locks)
{
htlock_t* htls = NULL;
#ifdef __sparc__
htls = memalign(CACHE_LINE_SIZE, num_locks * sizeof(htlock_t));
if (htls==NULL) {
fprintf(stderr, "Error @ memalign : init_htlocks\n");
}
#else
if (posix_memalign((void**) &htls, 64, num_locks * sizeof(htlock_t)) < 0)
{
fprintf(stderr, "Error @ posix_memalign : init_htlocks\n");
}
#endif
assert(htls != NULL);
size_t alloc_locks = (num_locks < 64) ? 64 : num_locks;
htlock_local_t* locals[NUMBER_OF_SOCKETS];
uint32_t n;
for (n = 0; n < NUMBER_OF_SOCKETS; n++)
{
#if defined(__x86_64__) && defined(PLATFORM_NUMA)
numa_set_preferred(n);
_mm_mfence();
#endif
locals[n] = (htlock_local_t*) calloc(alloc_locks, sizeof(htlock_local_t));
//numa_alloc_onnode(num_locks * sizeof(htlock_local_t), n);
assert(locals[n] != NULL);
}
#if defined(__x86_64__) && defined(PLATFORM_NUMA)
numa_set_preferred(my_node);
#endif
uint32_t i;
for (i = 0; i < num_locks; i++)
{
create_htlock_no_alloc(htls + i, locals, i);
}
return htls;
}
void
free_htlocks(htlock_t* locks)
{
free(locks);
}
static inline uint32_t
sub_abs(const uint32_t a, const uint32_t b)
{
if (a > b)
{
return a - b;
}
else
{
return b - a;
}
}
#define TICKET_BASE_WAIT 512
#define TICKET_MAX_WAIT 4095
#define TICKET_WAIT_NEXT 64
static inline void
htlock_wait_ticket(htlock_local_t* lock, const uint32_t ticket)
{
#if defined(OPTERON_OPTIMIZE)
uint32_t wait = TICKET_BASE_WAIT;
uint32_t distance_prev = 1;
while (1)
{
PREFETCHW(lock);
int32_t lock_cur = lock->cur;
if (lock_cur == ticket)
{
break;
}
uint32_t distance = sub_abs(lock->cur, ticket);
if (distance > 1)
{
if (distance != distance_prev)
{
distance_prev = distance;
wait = TICKET_BASE_WAIT;
}
nop_rep(distance * wait);
wait = (wait + TICKET_BASE_WAIT) & TICKET_MAX_WAIT;
}
else
{
nop_rep(TICKET_WAIT_NEXT);
}
}
#else
while (lock->cur != ticket)
{
uint32_t distance = sub_abs(lock->cur, ticket);
if (distance > 1)
{
nop_rep(distance * TICKET_BASE_WAIT);
}
else
{
PAUSE;
}
}
#endif /* OPTERON_OPTIMIZE */
}
static inline void
htlock_wait_global(htlock_local_t* lock, const uint32_t ticket)
{
while (lock->cur != ticket)
{
uint32_t distance = sub_abs(lock->cur, ticket);
if (distance > 1)
{
wait_cycles(distance * 256);
}
else
{
PAUSE;
}
}
}
void
htlock_lock(htlock_t* l)
{
htlock_local_t* localp = l->local[my_node];
int32_t local_ticket;
/* _mm_clflush(localp); */
again_local:
/* if (my_id == 32) */
/* { */
/* s = getticks(); */
/* } */
local_ticket = DAF_U32((volatile uint32_t*) &localp->nxt);
/* if (my_id == 32) */
/* { */
/* e = getticks(); */
/* printf("%5llu - ", e - s - 74); */
/* } */
/* only the guy which gets local_ticket == -1 is allowed to share tickets */
if (local_ticket < -1)
{
PAUSE;
wait_cycles(-local_ticket * 120);
PAUSE;
goto again_local;
}
if (local_ticket >= 0) /* local grabing successful */
{
htlock_wait_ticket((htlock_local_t*) localp, local_ticket);
}
else /* no local ticket available */
{
do
{
#if defined(OPTERON_OPTIMIZE)
PREFETCHW(localp);
#endif
} while (localp->cur != NB_TICKETS_LOCAL);
localp->nxt = NB_TICKETS_LOCAL; /* give tickets to the local neighbors */
htlock_global_t* globalp = l->global;
uint32_t global_ticket = FAI_U32(&globalp->nxt);
/* htlock_wait_ticket((htlock_local_t*) globalp, global_ticket); */
htlock_wait_global((htlock_local_t*) globalp, global_ticket);
}
}
void
htlock_release(htlock_t* l)
{
htlock_local_t* localp = l->local[my_node];
#if defined(OPTERON_OPTIMIZE)
PREFETCHW(localp);
#endif
int32_t local_cur = localp->cur;
int32_t local_nxt = CAS_U32((volatile uint32_t*) &localp->nxt, local_cur, 0);
if (local_cur == 0 || local_cur == local_nxt) /* global */
{
#if defined(OPTERON_OPTIMIZE)
PREFETCHW((l->global));
PREFETCHW(localp);
#endif
localp->cur = NB_TICKETS_LOCAL;
l->global->cur++;
}
else /* local */
{
#if defined(OPTERON_OPTIMIZE)
PREFETCHW(localp);
#endif
localp->cur = local_cur - 1;
}
}
uint32_t
htlock_trylock(htlock_t* l)
{
htlock_global_t* globalp = l->global;
PREFETCHW(globalp);
uint32_t global_nxt = globalp->nxt;
htlock_global_t tmp =
{
.nxt = global_nxt,
.cur = global_nxt
};
htlock_global_t tmp_new =
{
.nxt = global_nxt + 1,
.cur = global_nxt
};
uint64_t tmp64 = *(uint64_t*) &tmp;
uint64_t tmp_new64 = *(uint64_t*) &tmp_new;
if (CAS_U64((uint64_t*) globalp, tmp64, tmp_new64) == tmp64)
{
return 1;
}
return 0;
}
inline void
htlock_release_try(htlock_t* l) /* trylock rls */
{
PREFETCHW((l->global));
l->global->cur++;
}