ipsec: IPSec protection for multi-point tunnel interfaces
[vpp.git] / src / vppinfra / test_rwlock.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #ifndef _GNU_SOURCE
17 #define _GNU_SOURCE
18 #endif
19
20 #include <vppinfra/mem.h>
21 #include <vppinfra/cache.h>
22 #include <vppinfra/lock.h>
23 #include <pthread.h>
24 #include <vppinfra/format.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/time.h>
27 #include <sched.h>
28 #include <vppinfra/atomics.h>
29
30 static u32 all_threads_online = 0;
31
32 typedef struct
33 {
34   uword threads_per_core;
35   uword cpu_mask_read;
36   uword read_cores;
37   uword cpu_mask_write;
38   uword write_cores;
39   uword increment_per_thread;
40   clib_rwlock_t rwlock;
41   uword shared_count;
42   uword iterations;
43 } rwlock_test_main_t;
44
45 void *
46 write_shared_counter (void *arg)
47 {
48   f64 *time = vec_new (f64, 1);
49   *time = 0;
50   rwlock_test_main_t *rtm = arg;
51
52   /* Wait for all threads to be created */
53   while (!clib_atomic_load_acq_n (&all_threads_online));
54
55   f64 start = clib_cpu_time_now ();
56   for (uword i = 0; i < rtm->increment_per_thread; i++)
57     {
58       clib_rwlock_writer_lock (&rtm->rwlock);
59       rtm->shared_count++;
60       clib_rwlock_writer_unlock (&rtm->rwlock);
61     }
62   *time = clib_cpu_time_now () - start;
63   return time;
64 }
65
66 void *
67 read_shared_counter (void *arg)
68 {
69   f64 *time = vec_new (f64, 1);
70   *time = 0;
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;
74
75   /* Wait for all threads to be created */
76   while (!clib_atomic_load_acq_n (&all_threads_online));
77
78   f64 start = clib_cpu_time_now ();
79   while (cnt_cpy < exp)
80     {
81       clib_rwlock_reader_lock (&rtm->rwlock);
82       cnt_cpy = rtm->shared_count;
83       clib_rwlock_reader_unlock (&rtm->rwlock);
84     }
85   *time = clib_cpu_time_now () - start;
86   return time;
87 }
88
89 unsigned
90 test_rwlock (rwlock_test_main_t * rtm, f64 * elapse_time)
91 {
92   int error = 0, total_threads = (rtm->read_cores + rtm->write_cores)
93     * rtm->threads_per_core;
94   pthread_t pthread[total_threads];
95
96   cpu_set_t cpuset;
97   unsigned cores_set = 0, cpu_id = 0;
98
99   /* Spawn reader (consumer) threads */
100   for (unsigned cpu_mask = rtm->cpu_mask_read; cpu_mask; cpu_mask >>= 1)
101     {
102       if (!(cpu_mask & 1))
103         {
104           cpu_id++;
105           continue;
106         }
107
108       CPU_ZERO (&cpuset);
109       CPU_SET (cpu_id, &cpuset);
110       for (uword t_num = 0; t_num < rtm->threads_per_core; t_num++)
111         {
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);
116
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",
120                                error);
121         }
122       cores_set++;
123       cpu_id++;
124     }
125
126   /* Spawn writer (producer) threads */
127   cpu_id = 0;
128   for (unsigned cpu_mask = rtm->cpu_mask_write; cpu_mask; cpu_mask >>= 1)
129     {
130       if (!(cpu_mask & 1))
131         {
132           cpu_id++;
133           continue;
134         }
135
136       CPU_ZERO (&cpuset);
137       CPU_SET (cpu_id, &cpuset);
138       for (uword t_num = 0; t_num < rtm->threads_per_core; t_num++)
139         {
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);
144
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",
148                                error);
149         }
150       cores_set++;
151       cpu_id++;
152     }
153
154   /* Launch all threads */
155   clib_atomic_store_rel_n (&all_threads_online, 1);
156
157   for (uword thread_num = 0; thread_num < total_threads; thread_num++)
158     {
159       f64 *time;
160       if ((error = pthread_join (pthread[thread_num], (void *) &time)))
161         clib_unix_warning ("pthread_join failed with %d", error);
162       *elapse_time += *time;
163       vec_free (time);
164     }
165
166   fformat (stdout, "Time elapsed: %.4e\n", *elapse_time);
167   return rtm->shared_count;
168 }
169
170 uword
171 num_cores_in_cpu_mask (uword mask)
172 {
173   uword num_cores = 0;
174   for (uword cpu_mask = mask; cpu_mask; cpu_mask >>= 1)
175     num_cores += (cpu_mask & 1);
176   return num_cores;
177 }
178
179 int
180 test_rwlock_main (unformat_input_t * i)
181 {
182   rwlock_test_main_t _rtm, *rtm = &_rtm;
183   clib_memset (rtm, 0, sizeof (rwlock_test_main_t));
184
185   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
186     {
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))
192         {
193           clib_unix_warning ("unknown input '%U'", format_unformat_error, i);
194           return 1;
195         }
196     }
197
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);
200
201   uword total_increment = rtm->threads_per_core * rtm->write_cores *
202     rtm->increment_per_thread;
203
204   clib_rwlock_init (&rtm->rwlock);
205
206   f64 average_time = 0;
207   for (uword trial = 0; trial < rtm->iterations; trial++)
208     {
209       rtm->shared_count = 0;
210       f64 elapse_time = 0;
211       if (test_rwlock (rtm, &elapse_time) != total_increment)
212         {
213           clib_rwlock_free (&rtm->rwlock);
214           fformat (stdout, "FAILED: expected count: %d, actual count: %d\n",
215                    total_increment, rtm->shared_count);
216           return 1;
217         }
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);
222     }
223   clib_rwlock_free (&rtm->rwlock);
224   return 0;
225 }
226
227 #ifdef CLIB_UNIX
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
231
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
234             ff sets cpus 0 - 7
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
238     @returns exit code
239 */
240 int
241 main (int argc, char *argv[])
242 {
243   unformat_input_t i;
244   i32 ret;
245   clib_time_t time;
246
247   clib_mem_init (0, 3ULL << 30);
248   clib_time_init (&time);
249
250   unformat_init_command_line (&i, argv);
251   ret = test_rwlock_main (&i);
252   unformat_free (&i);
253
254   return ret;
255 }
256 #endif /* CLIB_UNIX */
257
258 /*
259  * fd.io coding-style-patch-verification: ON
260  *
261  * Local Variables:
262  * eval: (c-set-style "gnu")
263  * End:
264  */