| 1 | // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | |
| 5 | /* |
| 6 | * Per thread locks - test program |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <pthread.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/wait.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdio.h> |
| 17 | #include <pthread.h> |
| 18 | #include <errno.h> |
| 19 | |
| 20 | #include <urcu/arch.h> |
| 21 | #include <urcu/assert.h> |
| 22 | |
| 23 | #include "thread-id.h" |
| 24 | |
| 25 | #include <urcu.h> |
| 26 | |
| 27 | struct test_array { |
| 28 | int a; |
| 29 | }; |
| 30 | |
| 31 | static struct test_array test_array = { 8 }; |
| 32 | |
| 33 | struct per_thread_lock { |
| 34 | pthread_mutex_t lock; |
| 35 | } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); /* cache-line aligned */ |
| 36 | |
| 37 | static struct per_thread_lock *per_thread_lock; |
| 38 | |
| 39 | #define OUTER_READ_LOOP 200U |
| 40 | #define INNER_READ_LOOP 100000U |
| 41 | #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP) |
| 42 | |
| 43 | #define OUTER_WRITE_LOOP 10U |
| 44 | #define INNER_WRITE_LOOP 200U |
| 45 | #define WRITE_LOOP ((unsigned long long)OUTER_WRITE_LOOP * INNER_WRITE_LOOP) |
| 46 | |
| 47 | static int num_read; |
| 48 | static int num_write; |
| 49 | |
| 50 | #define NR_READ num_read |
| 51 | #define NR_WRITE num_write |
| 52 | |
| 53 | static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time; |
| 54 | static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time; |
| 55 | |
| 56 | static |
| 57 | void *thr_reader(void *arg) |
| 58 | { |
| 59 | caa_cycles_t time1, time2; |
| 60 | long tidx = (long)arg; |
| 61 | unsigned int i, j; |
| 62 | int ret; |
| 63 | |
| 64 | printf("thread_begin %s, tid %lu\n", |
| 65 | "reader", urcu_get_thread_id()); |
| 66 | sleep(2); |
| 67 | |
| 68 | time1 = caa_get_cycles(); |
| 69 | for (i = 0; i < OUTER_READ_LOOP; i++) { |
| 70 | for (j = 0; j < INNER_READ_LOOP; j++) { |
| 71 | ret = pthread_mutex_lock(&per_thread_lock[tidx].lock); |
| 72 | if (ret) { |
| 73 | perror("Error in pthread mutex lock"); |
| 74 | exit(-1); |
| 75 | } |
| 76 | urcu_posix_assert(test_array.a == 8); |
| 77 | ret = pthread_mutex_unlock(&per_thread_lock[tidx].lock); |
| 78 | if (ret) { |
| 79 | perror("Error in pthread mutex unlock"); |
| 80 | exit(-1); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | time2 = caa_get_cycles(); |
| 85 | |
| 86 | reader_time[tidx] = time2 - time1; |
| 87 | |
| 88 | sleep(2); |
| 89 | printf("thread_end %s, tid %lu\n", |
| 90 | "reader", urcu_get_thread_id()); |
| 91 | return ((void*)1); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | static |
| 96 | void *thr_writer(void *arg) |
| 97 | { |
| 98 | caa_cycles_t time1, time2; |
| 99 | unsigned int i, j; |
| 100 | long tidx; |
| 101 | int ret; |
| 102 | |
| 103 | printf("thread_begin %s, tid %lu\n", |
| 104 | "writer", urcu_get_thread_id()); |
| 105 | sleep(2); |
| 106 | |
| 107 | for (i = 0; i < OUTER_WRITE_LOOP; i++) { |
| 108 | for (j = 0; j < INNER_WRITE_LOOP; j++) { |
| 109 | time1 = caa_get_cycles(); |
| 110 | for (tidx = 0; tidx < NR_READ; tidx++) { |
| 111 | ret = pthread_mutex_lock(&per_thread_lock[tidx].lock); |
| 112 | if (ret) { |
| 113 | perror("Error in pthread mutex lock"); |
| 114 | exit(-1); |
| 115 | } |
| 116 | } |
| 117 | test_array.a = 8; |
| 118 | for (tidx = NR_READ - 1; tidx >= 0; tidx--) { |
| 119 | ret = pthread_mutex_unlock(&per_thread_lock[tidx].lock); |
| 120 | if (ret) { |
| 121 | perror("Error in pthread mutex unlock"); |
| 122 | exit(-1); |
| 123 | } |
| 124 | } |
| 125 | time2 = caa_get_cycles(); |
| 126 | writer_time[(unsigned long)arg] += time2 - time1; |
| 127 | usleep(1); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | printf("thread_end %s, tid %lu\n", |
| 132 | "writer", urcu_get_thread_id()); |
| 133 | return ((void*)2); |
| 134 | } |
| 135 | |
| 136 | int main(int argc, char **argv) |
| 137 | { |
| 138 | int err; |
| 139 | pthread_t *tid_reader, *tid_writer; |
| 140 | void *tret; |
| 141 | int i; |
| 142 | caa_cycles_t tot_rtime = 0; |
| 143 | caa_cycles_t tot_wtime = 0; |
| 144 | |
| 145 | if (argc < 2) { |
| 146 | printf("Usage : %s nr_readers nr_writers\n", argv[0]); |
| 147 | exit(-1); |
| 148 | } |
| 149 | num_read = atoi(argv[1]); |
| 150 | num_write = atoi(argv[2]); |
| 151 | |
| 152 | reader_time = calloc(num_read, sizeof(*reader_time)); |
| 153 | writer_time = calloc(num_write, sizeof(*writer_time)); |
| 154 | tid_reader = calloc(num_read, sizeof(*tid_reader)); |
| 155 | tid_writer = calloc(num_write, sizeof(*tid_writer)); |
| 156 | |
| 157 | printf("thread %-6s, tid %lu\n", |
| 158 | "main", urcu_get_thread_id()); |
| 159 | |
| 160 | per_thread_lock = malloc(sizeof(struct per_thread_lock) * NR_READ); |
| 161 | |
| 162 | for (i = 0; i < NR_READ; i++) { |
| 163 | pthread_mutex_init(&per_thread_lock[i].lock, NULL); |
| 164 | } |
| 165 | for (i = 0; i < NR_READ; i++) { |
| 166 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
| 167 | (void *)(long)i); |
| 168 | if (err != 0) |
| 169 | exit(1); |
| 170 | } |
| 171 | for (i = 0; i < NR_WRITE; i++) { |
| 172 | err = pthread_create(&tid_writer[i], NULL, thr_writer, |
| 173 | (void *)(long)i); |
| 174 | if (err != 0) |
| 175 | exit(1); |
| 176 | } |
| 177 | |
| 178 | sleep(10); |
| 179 | |
| 180 | for (i = 0; i < NR_READ; i++) { |
| 181 | err = pthread_join(tid_reader[i], &tret); |
| 182 | if (err != 0) |
| 183 | exit(1); |
| 184 | tot_rtime += reader_time[i]; |
| 185 | } |
| 186 | for (i = 0; i < NR_WRITE; i++) { |
| 187 | err = pthread_join(tid_writer[i], &tret); |
| 188 | if (err != 0) |
| 189 | exit(1); |
| 190 | tot_wtime += writer_time[i]; |
| 191 | } |
| 192 | printf("Time per read : %g cycles\n", |
| 193 | (double)tot_rtime / ((double)NR_READ * (double)READ_LOOP)); |
| 194 | printf("Time per write : %g cycles\n", |
| 195 | (double)tot_wtime / ((double)NR_WRITE * (double)WRITE_LOOP)); |
| 196 | free(per_thread_lock); |
| 197 | |
| 198 | free(reader_time); |
| 199 | free(writer_time); |
| 200 | free(tid_reader); |
| 201 | free(tid_writer); |
| 202 | |
| 203 | return 0; |
| 204 | } |