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>
29 #include <vppinfra/atomics.h>
31 static u32 all_threads_online = 0;
35 uword threads_per_core;
38 uword increment_per_thread;
39 clib_spinlock_t slock;
42 } spinlock_test_main_t;
45 inc_shared_counter (void *arg)
47 f64 *time = vec_new (f64, 1);
49 spinlock_test_main_t *stm = arg;
51 /* Wait for all threads to be created */
52 while (!clib_atomic_load_acq_n (&all_threads_online));
54 f64 start = clib_cpu_time_now ();
55 for (uword i = 0; i < stm->increment_per_thread; i++)
57 clib_spinlock_lock (&stm->slock);
59 clib_spinlock_unlock (&stm->slock);
61 *time = clib_cpu_time_now () - start;
66 test_spinlock (spinlock_test_main_t * stm, f64 * elapse_time)
69 uword num_threads = stm->num_cores * stm->threads_per_core;
70 pthread_t pthread[num_threads];
73 unsigned cores_set = 0, cpu_id = 0;
74 for (unsigned cpu_mask = stm->cpu_mask; cpu_mask; cpu_mask >>= 1)
83 CPU_SET (cpu_id, &cpuset);
84 for (uword t_num = 0; t_num < stm->threads_per_core; t_num++)
86 uword t_index = cores_set * stm->threads_per_core + t_num;
87 if ((error = pthread_create (&pthread[t_index], NULL,
88 &inc_shared_counter, stm)))
89 clib_unix_warning ("pthread_create failed with %d", error);
91 if ((error = pthread_setaffinity_np (pthread[t_index],
92 sizeof (cpu_set_t), &cpuset)))
93 clib_unix_warning ("pthread_setaffinity_np failed with %d",
100 /* Launch all threads */
101 clib_atomic_store_rel_n (&all_threads_online, 1);
103 for (uword thread_num = 0; thread_num < num_threads; thread_num++)
106 if ((error = pthread_join (pthread[thread_num], (void *) &time)))
107 clib_unix_warning ("pthread_join failed with %d", error);
108 *elapse_time += *time;
112 fformat (stdout, "Time elapsed: %.4e cycles\n", *elapse_time);
113 return stm->shared_count;
117 num_cores_in_cpu_mask (uword mask)
120 for (uword cpu_mask = mask; cpu_mask; cpu_mask >>= 1)
121 num_cores += (cpu_mask & 1);
126 test_spinlock_main (unformat_input_t * i)
128 spinlock_test_main_t _stm, *stm = &_stm;
129 clib_memset (stm, 0, sizeof (spinlock_test_main_t));
131 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
133 if (0 == unformat (i, "threads/core %d", &stm->threads_per_core)
134 && 0 == unformat (i, "cpu_mask %x", &stm->cpu_mask)
135 && 0 == unformat (i, "increment %d", &stm->increment_per_thread)
136 && 0 == unformat (i, "iterations %d", &stm->iterations))
138 clib_unix_warning ("unknown input '%U'", format_unformat_error, i);
143 stm->num_cores = num_cores_in_cpu_mask (stm->cpu_mask);
145 uword total_increment = stm->threads_per_core * stm->num_cores *
146 stm->increment_per_thread;
148 clib_spinlock_init (&stm->slock);
150 f64 average_time = 0;
151 for (uword trial = 0; trial < stm->iterations; trial++)
153 stm->shared_count = 0;
155 if (test_spinlock (stm, &elapse_time) != total_increment)
157 clib_spinlock_free (&stm->slock);
158 fformat (stdout, "FAILED: expected count: %d, actual count: %d\n",
159 total_increment, stm->shared_count);
163 fformat (stdout, "Trial %d SUCCESS: %d = %d\n",
164 trial, stm->shared_count, total_increment);
165 average_time = (average_time * trial + elapse_time) / (trial + 1);
166 fformat (stdout, "Average lock/unlock cycles %.4e\n", average_time);
168 clib_spinlock_free (&stm->slock);
173 /** Launches a number of threads to simultaneously increment a global
174 counter, and records timestamps for spinlock performance benchmarking
176 @param "threads/core [# threads/core]" - number of threads per core
177 @param "cpu_mask [cpu_mask]" - cpu hex string e.g. input ff sets cpus 0 - 7
178 @param "increment [# increments]" - number of increments per threads
179 @param "iterations [# iterations]" - number of iterations
183 main (int argc, char *argv[])
189 clib_mem_init (0, 3ULL << 30);
190 clib_time_init (&time);
192 unformat_init_command_line (&i, argv);
193 ret = test_spinlock_main (&i);
198 #endif /* CLIB_UNIX */
201 * fd.io coding-style-patch-verification: ON
204 * eval: (c-set-style "gnu")