blob: b9cba6bee5d118955ad8f3eaded559c81e5e2fda [file] [log] [blame]
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001/*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005 */
6
Anas Nashife90a4bb2020-08-18 08:41:02 -04007#ifndef _LATENCY_MEASURE_UNIT_H
8#define _LATENCY_MEASURE_UNIT_H
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07009/*
Peter Mitsis8c80bbb2023-09-01 14:27:33 -040010 * @brief This file contains function declarations, macros and inline functions
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070011 * used in latency measurement.
12 */
13
Gerard Marull-Paretasade7ccb2022-05-06 10:44:33 +020014#include <zephyr/timing/timing.h>
15#include <zephyr/sys/printk.h>
Anas Nashife90a4bb2020-08-18 08:41:02 -040016#include <stdio.h>
Fabio Baltieridef23012022-07-19 14:16:24 +000017#include <zephyr/timestamp.h>
Peter Mitsis6291a572024-01-02 13:54:15 -050018#include <zephyr/app_memory/app_memdomain.h>
Daniel Leung21797052020-04-07 12:21:46 -070019
Peter Mitsisa8914c52024-01-04 11:25:03 -050020#define START_STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
21#define ALT_STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
Peter Mitsis8c80bbb2023-09-01 14:27:33 -040022
Peter Mitsis1a7e5c62023-10-05 10:30:43 -040023#ifdef CONFIG_USERSPACE
24#define BENCH_BMEM K_APP_BMEM(bench_mem_partition)
25#else
26#define BENCH_BMEM
27#endif
28
Peter Mitsis8c80bbb2023-09-01 14:27:33 -040029struct timestamp_data {
30 uint64_t cycles;
31 timing_t sample;
32};
33
34K_THREAD_STACK_DECLARE(start_stack, START_STACK_SIZE);
35K_THREAD_STACK_DECLARE(alt_stack, ALT_STACK_SIZE);
36
37extern struct k_thread start_thread;
38extern struct k_thread alt_thread;
39
40extern struct k_sem pause_sem;
41
42extern struct timestamp_data timestamp;
Peter Mitsis1a7e5c62023-10-05 10:30:43 -040043#ifdef CONFIG_USERSPACE
44extern uint64_t user_timestamp_overhead;
45#endif
46extern uint64_t timestamp_overhead;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070047
Anas Nashif0505c9c2017-03-24 10:23:40 -040048extern int error_count;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070049
Peter Mitsisaa4f2462023-06-07 18:12:09 -040050#define TICK_OCCURRENCE_ERROR "Error: Tick Occurred"
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070051
Anas Nashife90a4bb2020-08-18 08:41:02 -040052#ifdef CSV_FORMAT_OUTPUT
Peter Mitsisd3a3d632024-01-17 13:17:07 -050053#define FORMAT_STR "%-94s,%s,%s,%s\n"
Peter Mitsisfe922d32023-06-07 17:33:56 -040054#define CYCLE_FORMAT "%8u"
55#define NSEC_FORMAT "%8u"
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070056#else
Peter Mitsisd3a3d632024-01-17 13:17:07 -050057#define FORMAT_STR "%-94s:%s , %s : %s\n"
Peter Mitsisfe922d32023-06-07 17:33:56 -040058#define CYCLE_FORMAT "%8u cycles"
59#define NSEC_FORMAT "%8u ns"
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070060#endif
61
Peter Mitsisaa4f2462023-06-07 18:12:09 -040062/**
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 Mitsisfe922d32023-06-07 17:33:56 -040074#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-Gonzalez8ddf82c2015-04-10 16:44:37 -070088
Peter Mitsisaa4f2462023-06-07 18:12:09 -040089#define PRINT_STATS(summary, value, error, notes) \
90 PRINT_F(summary, value, \
91 (uint32_t)timing_cycles_to_ns(value), \
92 error, notes)
Anas Nashife90a4bb2020-08-18 08:41:02 -040093
Peter Mitsisaa4f2462023-06-07 18:12:09 -040094#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 Nashife90a4bb2020-08-18 08:41:02 -040098
99
100#endif