2 * Copyright (c) 2019 Arm Limited.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
20 #include <vppinfra/mem.h>
21 #include <vppinfra/cache.h>
22 #include <vppinfra/lock.h>
24 #include <vppinfra/format.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/time.h>
28 #include <vppinfra/atomics.h>
30 static u32 all_threads_online = 0;
34 uword threads_per_core;
39 uword increment_per_thread;
46 write_shared_counter (void *arg)
48 f64 *time = vec_new (f64, 1);
50 rwlock_test_main_t *rtm = arg;
52 /* Wait for all threads to be created */
53 while (!clib_atomic_load_acq_n (&all_threads_online));
55 f64 start = clib_cpu_time_now ();
56 for (uword i = 0; i < rtm->increment_per_thread; i++)
58 clib_rwlock_writer_lock (&rtm->rwlock);
60 clib_rwlock_writer_unlock (&rtm->rwlock);
62 *time = clib_cpu_time_now () - start;
67 read_shared_counter (void *arg)
69 f64 *time = vec_new (f64, 1);
71 rwlock_test_main_t *rtm = arg;
72 uword cnt_cpy = 0, exp = rtm->increment_per_thread * rtm->write_cores *
73 rtm->threads_per_core;
75 /* Wait for all threads to be created */
76 while (!clib_atomic_load_acq_n (&all_threads_online));
78 f64 start = clib_cpu_time_now ();
81 clib_rwlock_reader_lock (&rtm->rwlock);
82 cnt_cpy = rtm->shared_count;
83 clib_rwlock_reader_unlock (&rtm->rwlock);
85 *time = clib_cpu_time_now () - start;
90 test_rwlock (rwlock_test_main_t * rtm, f64 * elapse_time)
92 int error = 0, total_threads = (rtm->read_cores + rtm->write_cores)
93 * rtm->threads_per_core;
94 pthread_t pthread[total_threads];
97 unsigned cores_set = 0, cpu_id = 0;
99 /* Spawn reader (consumer) threads */
100 for (unsigned cpu_mask = rtm->cpu_mask_read; cpu_mask; cpu_mask >>= 1)
109 CPU_SET (cpu_id, &cpuset);
110 for (uword t_num = 0; t_num < rtm->threads_per_core; t_num++)
112 uword t_index = cores_set * rtm->threads_per_core + t_num;
113 if ((error = pthread_create (&pthread[t_index], NULL,
114 &read_shared_counter, rtm)))
115 clib_unix_warning ("pthread_create failed with %d", error);
117 if ((error = pthread_setaffinity_np (pthread[t_index],
118 sizeof (cpu_set_t), &cpuset)))
119 clib_unix_warning ("pthread_set_affinity_np failed with %d",
126 /* Spawn writer (producer) threads */
128 for (unsigned cpu_mask = rtm->cpu_mask_write; cpu_mask; cpu_mask >>= 1)
137 CPU_SET (cpu_id, &cpuset);
138 for (uword t_num = 0; t_num < rtm->threads_per_core; t_num++)
140 uword t_index = cores_set * rtm->threads_per_core + t_num;
141 if ((error = pthread_create (&pthread[t_index], NULL,
142 &write_shared_counter, rtm)))
143 clib_unix_warning ("pthread_create failed with %d", error);
145 if ((error = pthread_setaffinity_np (pthread[t_index],
146 sizeof (cpu_set_t), &cpuset)))
147 clib_unix_warning ("pthread_set_affinity_np failed with %d",
154 /* Launch all threads */
155 clib_atomic_store_rel_n (&all_threads_online, 1);
157 for (uword thread_num = 0; thread_num < total_threads; thread_num++)
160 if ((error = pthread_join (pthread[thread_num], (void *) &time)))
161 clib_unix_warning ("pthread_join failed with %d", error);
162 *elapse_time += *time;
166 fformat (stdout, "Time elapsed: %.4e\n", *elapse_time);
167 return rtm->shared_count;
171 num_cores_in_cpu_mask (uword mask)
174 for (uword cpu_mask = mask; cpu_mask; cpu_mask >>= 1)
175 num_cores += (cpu_mask & 1);
180 test_rwlock_main (unformat_input_t * i)
182 rwlock_test_main_t _rtm, *rtm = &_rtm;
183 clib_memset (rtm, 0, sizeof (rwlock_test_main_t));
185 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
187 if (0 == unformat (i, "threads/core %d", &rtm->threads_per_core)
188 && 0 == unformat (i, "cpu_mask_read %x", &rtm->cpu_mask_read)
189 && 0 == unformat (i, "cpu_mask_write %x", &rtm->cpu_mask_write)
190 && 0 == unformat (i, "increment %d", &rtm->increment_per_thread)
191 && 0 == unformat (i, "iterations %d", &rtm->iterations))
193 clib_unix_warning ("unknown input '%U'", format_unformat_error, i);
198 rtm->read_cores = num_cores_in_cpu_mask (rtm->cpu_mask_read);
199 rtm->write_cores = num_cores_in_cpu_mask (rtm->cpu_mask_write);
201 uword total_increment = rtm->threads_per_core * rtm->write_cores *
202 rtm->increment_per_thread;
204 clib_rwlock_init (&rtm->rwlock);
206 f64 average_time = 0;
207 for (uword trial = 0; trial < rtm->iterations; trial++)
209 rtm->shared_count = 0;
211 if (test_rwlock (rtm, &elapse_time) != total_increment)
213 clib_rwlock_free (&rtm->rwlock);
214 fformat (stdout, "FAILED: expected count: %d, actual count: %d\n",
215 total_increment, rtm->shared_count);
218 fformat (stdout, "Trial %d SUCCESS: %d = %d\n",
219 trial, rtm->shared_count, total_increment);
220 average_time = (average_time * trial + elapse_time) / (trial + 1);
221 fformat (stdout, "Average lock/unlock cycles: %.4e\n", average_time);
223 clib_rwlock_free (&rtm->rwlock);
228 /** Launches a number of writer threads to simultaneously increment a global
229 counter and a number of reader threads to continuously poll the counter,
230 and records timestamps for rwlock performance benchmarking
232 @param "threads/core [# threads/core]" - number of threads per core
233 @param "cpu_mask_read [cpu_mask]" - reader thread cpu string e.g. input
235 @param "cpu_mask_write [cpu_mask]" - writer thread cpu string
236 @param "increment [# increments]" - number of increments per writer thread
237 @param "iterations [# iterations]" - number of iterations
241 main (int argc, char *argv[])
247 clib_mem_init (0, 3ULL << 30);
248 clib_time_init (&time);
250 unformat_init_command_line (&i, argv);
251 ret = test_rwlock_main (&i);
256 #endif /* CLIB_UNIX */
259 * fd.io coding-style-patch-verification: ON
262 * eval: (c-set-style "gnu")