PX4 Firmware
PX4 Autopilot Software http://px4.io
test_microbench_hrt.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2018-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file test_microbench_hrt.cpp
36  * Tests for the microbench high resolution timer.
37  */
38 
39 #include <unit_test.h>
40 
41 #include <time.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 
45 #include <drivers/drv_hrt.h>
46 #include <perf/perf_counter.h>
47 #include <px4_platform_common/px4_config.h>
48 #include <px4_platform_common/micro_hal.h>
49 
50 namespace MicroBenchHRT
51 {
52 
53 #ifdef __PX4_NUTTX
54 #include <nuttx/irq.h>
55 static irqstate_t flags;
56 #endif
57 
58 void lock()
59 {
60 #ifdef __PX4_NUTTX
61  flags = px4_enter_critical_section();
62 #endif
63 }
64 
65 void unlock()
66 {
67 #ifdef __PX4_NUTTX
68  px4_leave_critical_section(flags);
69 #endif
70 }
71 
72 #define PERF(name, op, count) do { \
73  px4_usleep(1000); \
74  reset(); \
75  perf_counter_t p = perf_alloc(PC_ELAPSED, name); \
76  for (int i = 0; i < count; i++) { \
77  lock(); \
78  perf_begin(p); \
79  op; \
80  perf_end(p); \
81  unlock(); \
82  reset(); \
83  } \
84  perf_print_counter(p); \
85  perf_free(p); \
86  } while (0)
87 
88 class MicroBenchHRT : public UnitTest
89 {
90 public:
91  virtual bool run_tests();
92 
93 private:
94 
95  bool time_px4_hrt();
96 
97  void reset();
98 
99  void lock()
100  {
101 #ifdef __PX4_NUTTX
102  flags = px4_enter_critical_section();
103 #endif
104  }
105 
106  void unlock()
107  {
108 #ifdef __PX4_NUTTX
109  px4_leave_critical_section(flags);
110 #endif
111  }
112 
113  uint64_t u_64;
114  uint64_t u_64_out;
115 };
116 
117 bool MicroBenchHRT::run_tests()
118 {
119  ut_run_test(time_px4_hrt);
120 
121  return (_tests_failed == 0);
122 }
123 
124 template<typename T>
125 T random(T min, T max)
126 {
127  const T scale = rand() / (T) RAND_MAX; /* [0, 1.0] */
128  return min + scale * (max - min); /* [min, max] */
129 }
130 
132 {
133  srand(time(nullptr));
134 
135  // initialize with random data
136  u_64 = rand();
137  u_64_out = rand();
138 }
139 
141 
142 bool MicroBenchHRT::time_px4_hrt()
143 {
144  PERF("hrt_absolute_time()", u_64_out = hrt_absolute_time(), 1000);
145  PERF("hrt_elapsed_time()", u_64_out = hrt_elapsed_time_atomic(&u_64), 1000);
146 
147  return true;
148 }
149 
150 } // namespace MicroBenchHRT
int test_microbench_hrt(int argc, char *argv[])
ut_declare_test_c(test_microbench_hrt, MicroBenchHRT) bool MicroBenchHRT
int reset(enum LPS22HB_BUS busid)
Reset the driver.
#define PERF(name, op, count)
Base class to be used for unit tests.
Definition: unit_test.h:54
High-resolution timer with callouts and timekeeping.
constexpr _Tp min(_Tp a, _Tp b)
Definition: Limits.hpp:54
constexpr _Tp max(_Tp a, _Tp b)
Definition: Limits.hpp:60
T random(T min, T max)
#define ut_run_test(test)
Runs a single unit test.
Definition: unit_test.h:96
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
Performance measuring tools.
__EXPORT hrt_abstime hrt_elapsed_time_atomic(const volatile hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.