Loading...
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 | /* * Copyright (c) 2012-2015 Wind River Systems, Inc. * Copyright (c) 2023 Intel Corporation. * * SPDX-License-Identifier: Apache-2.0 */ /* * @file measure time for sema lock and release * * This file contains the test that measures semaphore give and take time * in the kernel. There is no contention on the semaphore being tested. */ #include <zephyr/kernel.h> #include <zephyr/timing/timing.h> #include "utils.h" #include "timing_sc.h" static struct k_sem sem; static void alt_thread_entry(void *p1, void *p2, void *p3) { uint32_t num_iterations = (uint32_t)(uintptr_t)p1; timing_t mid; ARG_UNUSED(p2); ARG_UNUSED(p3); for (uint32_t i = 0; i < num_iterations; i++) { /* * 2. Give the semaphore, thereby forcing a context switch back * to <start_thread>. */ mid = timing_timestamp_get(); k_sem_give(&sem); /* 5. Share the <mid> timestamp. */ timestamp.sample = mid; /* 6. Give <sem> so <start_thread> resumes execution */ k_sem_give(&sem); } } static void start_thread_entry(void *p1, void *p2, void *p3) { uint32_t num_iterations = (uint32_t)(uintptr_t)p1; timing_t start; timing_t mid; timing_t finish; uint32_t i; uint64_t take_sum = 0ull; uint64_t give_sum = 0ull; ARG_UNUSED(p2); ARG_UNUSED(p3); k_thread_start(&alt_thread); for (i = 0; i < num_iterations; i++) { /* * 1. Block on taking the semaphore and force a context switch * to <alt_thread>. */ start = timing_timestamp_get(); k_sem_take(&sem, K_FOREVER); /* 3. Get the <finish> timestamp. */ finish = timing_timestamp_get(); /* * 4. Let <alt_thread> run so it can share its <mid> * timestamp. */ k_sem_take(&sem, K_FOREVER); /* 7. Retrieve the <mid> timestamp */ mid = timestamp.sample; take_sum += timing_cycles_get(&start, &mid); give_sum += timing_cycles_get(&mid, &finish); } k_thread_join(&alt_thread, K_FOREVER); /* Share the totals with the main thread */ timestamp.cycles = take_sum; k_sem_take(&sem, K_FOREVER); timestamp.cycles = give_sum; } void sema_context_switch(uint32_t num_iterations, uint32_t start_options, uint32_t alt_options) { uint64_t cycles; char tag[50]; char description[120]; int priority; timing_start(); priority = k_thread_priority_get(k_current_get()); k_thread_create(&start_thread, start_stack, K_THREAD_STACK_SIZEOF(start_stack), start_thread_entry, (void *)(uintptr_t)num_iterations, NULL, NULL, priority - 2, start_options, K_FOREVER); k_thread_create(&alt_thread, alt_stack, K_THREAD_STACK_SIZEOF(alt_stack), alt_thread_entry, (void *)(uintptr_t)num_iterations, NULL, NULL, priority - 1, alt_options, K_FOREVER); k_thread_access_grant(&start_thread, &sem, &alt_thread); k_thread_access_grant(&alt_thread, &sem); /* Start the test threads */ k_thread_start(&start_thread); /* Retrieve the number of cycles spent taking the semaphore */ cycles = timestamp.cycles; cycles -= timestamp_overhead_adjustment(start_options, alt_options); snprintf(tag, sizeof(tag), "semaphore.take.blocking.%c_to_%c", ((start_options & K_USER) == K_USER) ? 'u' : 'k', ((alt_options & K_USER) == K_USER) ? 'u' : 'k'); snprintf(description, sizeof(description), "%-40s - Take a semaphore (context switch)", tag); PRINT_STATS_AVG(description, (uint32_t)cycles, num_iterations, false, ""); /* Unblock <start_thread> */ k_sem_give(&sem); /* Retrieve the number of cycles spent taking the semaphore */ cycles = timestamp.cycles; cycles -= timestamp_overhead_adjustment(start_options, alt_options); snprintf(tag, sizeof(tag), "semaphore.give.wake+ctx.%c_to_%c", ((alt_options & K_USER) == K_USER) ? 'u' : 'k', ((start_options & K_USER) == K_USER) ? 'u' : 'k'); snprintf(description, sizeof(description), "%-40s - Give a semaphore (context switch)", tag); PRINT_STATS_AVG(description, (uint32_t)cycles, num_iterations, false, ""); k_thread_join(&start_thread, K_FOREVER); timing_stop(); return; } /** * This is the entry point for the test that performs uncontested operations * on the semaphore. It gives the semaphore many times, takes the semaphore * many times and then sends the results back to the main thread. */ static void immediate_give_take(void *p1, void *p2, void *p3) { uint32_t num_iterations = (uint32_t)(uintptr_t)p1; timing_t start; timing_t finish; uint64_t give_cycles; uint64_t take_cycles; ARG_UNUSED(p2); ARG_UNUSED(p3); /* 1. Give a semaphore. No threads are waiting on it */ start = timing_timestamp_get(); for (uint32_t i = 0; i < num_iterations; i++) { k_sem_give(&sem); } finish = timing_timestamp_get(); give_cycles = timing_cycles_get(&start, &finish); /* 2. Take a semaphore--no contention */ start = timing_timestamp_get(); for (uint32_t i = 0; i < num_iterations; i++) { k_sem_take(&sem, K_NO_WAIT); } finish = timing_timestamp_get(); take_cycles = timing_cycles_get(&start, &finish); /* 3. Post the number of cycles spent giving the semaphore */ timestamp.cycles = give_cycles; /* 4. Wait for the main thread to retrieve the data */ k_sem_take(&sem, K_FOREVER); /* 7. Post the number of cycles spent taking the semaphore */ timestamp.cycles = take_cycles; } /** * * @brief The function tests semaphore test/signal time * * The routine performs unlock the quite amount of semaphores and then * acquires them in order to measure the necessary time. * * @return 0 on success */ int sema_test_signal(uint32_t num_iterations, uint32_t options) { uint64_t cycles; int priority; char tag[50]; char description[120]; timing_start(); k_sem_init(&sem, 0, num_iterations); priority = k_thread_priority_get(k_current_get()); k_thread_create(&start_thread, start_stack, K_THREAD_STACK_SIZEOF(start_stack), immediate_give_take, (void *)(uintptr_t)num_iterations, NULL, NULL, priority - 1, options, K_FOREVER); k_thread_access_grant(&start_thread, &sem); k_thread_start(&start_thread); /* 5. Retrieve the number of cycles spent giving the semaphore */ cycles = timestamp.cycles; snprintf(tag, sizeof(tag), "semaphore.give.immediate.%s", (options & K_USER) == K_USER ? "user" : "kernel"); snprintf(description, sizeof(description), "%-40s - Give a semaphore (no waiters)", tag); PRINT_STATS_AVG(description, (uint32_t)cycles, num_iterations, false, ""); /* 6. Unblock <start_thread> */ k_sem_give(&sem); /* 8. Wait for <start_thread> to finish */ k_thread_join(&start_thread, K_FOREVER); /* 9. Retrieve the number of cycles spent taking the semaphore */ cycles = timestamp.cycles; snprintf(tag, sizeof(tag), "semaphore.take.immediate.%s", (options & K_USER) == K_USER ? "user" : "kernel"); snprintf(description, sizeof(description), "%-40s - Take a semaphore (no blocking)", tag); PRINT_STATS_AVG(description, (uint32_t)cycles, num_iterations, false, ""); timing_stop(); return 0; } |