1 /*
2  * Copyright (c) 2012-2014 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _LATENCY_MEASURE_UNIT_H
8 #define _LATENCY_MEASURE_UNIT_H
9 /*
10  * @brief This file contains function declarations, macros and inline functions
11  * used in latency measurement.
12  */
13 
14 #include <zephyr/timing/timing.h>
15 #include <zephyr/sys/printk.h>
16 #include <stdio.h>
17 #include <zephyr/timestamp.h>
18 #include <zephyr/app_memory/app_memdomain.h>
19 
20 #define START_STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
21 #define ALT_STACK_SIZE   (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
22 
23 #ifdef CONFIG_USERSPACE
24 #define  BENCH_BMEM  K_APP_BMEM(bench_mem_partition)
25 #else
26 #define  BENCH_BMEM
27 #endif
28 
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;
43 #ifdef CONFIG_USERSPACE
44 extern uint64_t user_timestamp_overhead;
45 #endif
46 extern uint64_t timestamp_overhead;
47 
48 extern int error_count;
49 
50 #define TICK_OCCURRENCE_ERROR  "Error: Tick Occurred"
51 
52 #ifdef CSV_FORMAT_OUTPUT
53 #define FORMAT_STR   "%-94s,%s,%s,%s\n"
54 #define CYCLE_FORMAT "%8u"
55 #define NSEC_FORMAT  "%8u"
56 #else
57 #define FORMAT_STR   "%-94s:%s , %s : %s\n"
58 #define CYCLE_FORMAT "%8u cycles"
59 #define NSEC_FORMAT  "%8u ns"
60 #endif
61 
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  */
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)
88 
89 #define PRINT_STATS(summary, value, error, notes)     \
90 	PRINT_F(summary, value,                       \
91 		(uint32_t)timing_cycles_to_ns(value), \
92 		error, notes)
93 
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);
98 
99 
100 #endif
101