Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012-2014 Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Anas Nashif | e90a4bb | 2020-08-18 08:41:02 -0400 | [diff] [blame] | 7 | #ifndef _LATENCY_MEASURE_UNIT_H |
| 8 | #define _LATENCY_MEASURE_UNIT_H |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 9 | /* |
Peter Mitsis | 8c80bbb | 2023-09-01 14:27:33 -0400 | [diff] [blame] | 10 | * @brief This file contains function declarations, macros and inline functions |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 11 | * used in latency measurement. |
| 12 | */ |
| 13 | |
Gerard Marull-Paretas | ade7ccb | 2022-05-06 10:44:33 +0200 | [diff] [blame] | 14 | #include <zephyr/timing/timing.h> |
| 15 | #include <zephyr/sys/printk.h> |
Anas Nashif | e90a4bb | 2020-08-18 08:41:02 -0400 | [diff] [blame] | 16 | #include <stdio.h> |
Fabio Baltieri | def2301 | 2022-07-19 14:16:24 +0000 | [diff] [blame] | 17 | #include <zephyr/timestamp.h> |
Peter Mitsis | 6291a57 | 2024-01-02 13:54:15 -0500 | [diff] [blame] | 18 | #include <zephyr/app_memory/app_memdomain.h> |
Daniel Leung | 2179705 | 2020-04-07 12:21:46 -0700 | [diff] [blame] | 19 | |
Peter Mitsis | a8914c5 | 2024-01-04 11:25:03 -0500 | [diff] [blame] | 20 | #define START_STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACK_SIZE) |
| 21 | #define ALT_STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACK_SIZE) |
Peter Mitsis | 8c80bbb | 2023-09-01 14:27:33 -0400 | [diff] [blame] | 22 | |
Peter Mitsis | 1a7e5c6 | 2023-10-05 10:30:43 -0400 | [diff] [blame] | 23 | #ifdef CONFIG_USERSPACE |
| 24 | #define BENCH_BMEM K_APP_BMEM(bench_mem_partition) |
| 25 | #else |
| 26 | #define BENCH_BMEM |
| 27 | #endif |
| 28 | |
Peter Mitsis | 8c80bbb | 2023-09-01 14:27:33 -0400 | [diff] [blame] | 29 | struct timestamp_data { |
| 30 | uint64_t cycles; |
| 31 | timing_t sample; |
| 32 | }; |
| 33 | |
| 34 | K_THREAD_STACK_DECLARE(start_stack, START_STACK_SIZE); |
| 35 | K_THREAD_STACK_DECLARE(alt_stack, ALT_STACK_SIZE); |
| 36 | |
| 37 | extern struct k_thread start_thread; |
| 38 | extern struct k_thread alt_thread; |
| 39 | |
| 40 | extern struct k_sem pause_sem; |
| 41 | |
| 42 | extern struct timestamp_data timestamp; |
Peter Mitsis | 1a7e5c6 | 2023-10-05 10:30:43 -0400 | [diff] [blame] | 43 | #ifdef CONFIG_USERSPACE |
| 44 | extern uint64_t user_timestamp_overhead; |
| 45 | #endif |
| 46 | extern uint64_t timestamp_overhead; |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 47 | |
Anas Nashif | 0505c9c | 2017-03-24 10:23:40 -0400 | [diff] [blame] | 48 | extern int error_count; |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 49 | |
Peter Mitsis | aa4f246 | 2023-06-07 18:12:09 -0400 | [diff] [blame] | 50 | #define TICK_OCCURRENCE_ERROR "Error: Tick Occurred" |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 51 | |
Anas Nashif | e90a4bb | 2020-08-18 08:41:02 -0400 | [diff] [blame] | 52 | #ifdef CSV_FORMAT_OUTPUT |
Peter Mitsis | d3a3d63 | 2024-01-17 13:17:07 -0500 | [diff] [blame] | 53 | #define FORMAT_STR "%-94s,%s,%s,%s\n" |
Peter Mitsis | fe922d3 | 2023-06-07 17:33:56 -0400 | [diff] [blame] | 54 | #define CYCLE_FORMAT "%8u" |
| 55 | #define NSEC_FORMAT "%8u" |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 56 | #else |
Peter Mitsis | d3a3d63 | 2024-01-17 13:17:07 -0500 | [diff] [blame] | 57 | #define FORMAT_STR "%-94s:%s , %s : %s\n" |
Peter Mitsis | fe922d3 | 2023-06-07 17:33:56 -0400 | [diff] [blame] | 58 | #define CYCLE_FORMAT "%8u cycles" |
| 59 | #define NSEC_FORMAT "%8u ns" |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
Peter Mitsis | aa4f246 | 2023-06-07 18:12:09 -0400 | [diff] [blame] | 62 | /** |
| 63 | * @brief Display a line of statistics |
| 64 | * |
| 65 | * This macro displays the following: |
| 66 | * 1. Test description summary |
| 67 | * 2. Number of cycles - See Note |
| 68 | * 3. Number of nanoseconds - See Note |
| 69 | * 4. Additional notes describing the nature of any errors |
| 70 | * |
| 71 | * Note - If the @a error parameter is not false, then the test has no |
| 72 | * numerical information to print and it will instead print "FAILED". |
| 73 | */ |
Peter Mitsis | fe922d3 | 2023-06-07 17:33:56 -0400 | [diff] [blame] | 74 | #define PRINT_F(summary, cycles, nsec, error, notes) \ |
| 75 | do { \ |
| 76 | char cycle_str[32]; \ |
| 77 | char nsec_str[32]; \ |
| 78 | \ |
| 79 | if (!error) { \ |
| 80 | snprintk(cycle_str, 30, CYCLE_FORMAT, cycles); \ |
| 81 | snprintk(nsec_str, 30, NSEC_FORMAT, nsec); \ |
| 82 | } else { \ |
| 83 | snprintk(cycle_str, 30, "%15s", "FAILED"); \ |
| 84 | snprintk(nsec_str, 30, "%15s", "FAILED"); \ |
| 85 | } \ |
| 86 | printk(FORMAT_STR, summary, cycle_str, nsec_str, notes); \ |
| 87 | } while (0) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 88 | |
Peter Mitsis | aa4f246 | 2023-06-07 18:12:09 -0400 | [diff] [blame] | 89 | #define PRINT_STATS(summary, value, error, notes) \ |
| 90 | PRINT_F(summary, value, \ |
| 91 | (uint32_t)timing_cycles_to_ns(value), \ |
| 92 | error, notes) |
Anas Nashif | e90a4bb | 2020-08-18 08:41:02 -0400 | [diff] [blame] | 93 | |
Peter Mitsis | aa4f246 | 2023-06-07 18:12:09 -0400 | [diff] [blame] | 94 | #define PRINT_STATS_AVG(summary, value, counter, error, notes) \ |
| 95 | PRINT_F(summary, value / counter, \ |
| 96 | (uint32_t)timing_cycles_to_ns_avg(value, counter), \ |
| 97 | error, notes); |
Anas Nashif | e90a4bb | 2020-08-18 08:41:02 -0400 | [diff] [blame] | 98 | |
| 99 | |
| 100 | #endif |