From ff4c0d7e4d64f9aba85364ac0cf83a5901005fbd Mon Sep 17 00:00:00 2001 From: Elisa Ferrara Date: Mon, 8 Jun 2026 12:13:35 +0200 Subject: [PATCH] test(microbench): add gravity() benchmark Add an on-target microbenchmark for LatLonAlt::Wgs84::gravity(), the Somigliana latitude-dependent gravity used by EKF2 (APX4-4273), to bound its CPU cost on the flight controller. Measured ~0.45 us/call on fmu-v6s (STM32H7). The value is cached and only recomputed when latitude moves >1 deg, so the steady-state EKF cost is unchanged. Enabled on the fmu-v6s performance-test board. Run with: nsh> microbench microbench_gravity APX4-4273 --- .../fmu-v6s/performance-test.px4board | 1 + src/systemcmds/microbench/CMakeLists.txt | 2 + src/systemcmds/microbench/microbench_main.cpp | 2 + .../microbench/test_microbench_gravity.cpp | 141 ++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 src/systemcmds/microbench/test_microbench_gravity.cpp diff --git a/boards/auterion/fmu-v6s/performance-test.px4board b/boards/auterion/fmu-v6s/performance-test.px4board index 9184c6fb4ad..a0eaace15ba 100644 --- a/boards/auterion/fmu-v6s/performance-test.px4board +++ b/boards/auterion/fmu-v6s/performance-test.px4board @@ -15,6 +15,7 @@ CONFIG_SYSTEMCMDS_LED_CONTROL=y CONFIG_SYSTEMCMDS_MFT=y CONFIG_SYSTEMCMDS_MTD=y CONFIG_SYSTEMCMDS_MFT_CFG=y +CONFIG_SYSTEMCMDS_MICROBENCH=y CONFIG_SYSTEMCMDS_PARAM=y CONFIG_SYSTEMCMDS_PERF=y CONFIG_SYSTEMCMDS_REBOOT=y diff --git a/src/systemcmds/microbench/CMakeLists.txt b/src/systemcmds/microbench/CMakeLists.txt index 2b123ca3895..912a6663b10 100644 --- a/src/systemcmds/microbench/CMakeLists.txt +++ b/src/systemcmds/microbench/CMakeLists.txt @@ -44,10 +44,12 @@ px4_add_module( microbench_main.cpp test_microbench_atomic.cpp + test_microbench_gravity.cpp test_microbench_hrt.cpp test_microbench_math.cpp test_microbench_matrix.cpp test_microbench_uorb.cpp DEPENDS + lat_lon_alt ) diff --git a/src/systemcmds/microbench/microbench_main.cpp b/src/systemcmds/microbench/microbench_main.cpp index 56e441cc093..5489407532f 100644 --- a/src/systemcmds/microbench/microbench_main.cpp +++ b/src/systemcmds/microbench/microbench_main.cpp @@ -42,6 +42,7 @@ __BEGIN_DECLS extern int test_microbench_atomic(int argc, char *argv[]); +extern int test_microbench_gravity(int argc, char *argv[]); extern int test_microbench_hrt(int argc, char *argv[]); extern int test_microbench_math(int argc, char *argv[]); extern int test_microbench_matrix(int argc, char *argv[]); @@ -64,6 +65,7 @@ const struct { {"all", microbench_all, OPT_NOALLTEST}, {"microbench_atomic", test_microbench_atomic, 0}, + {"microbench_gravity", test_microbench_gravity, 0}, {"microbench_hrt", test_microbench_hrt, 0}, {"microbench_math", test_microbench_math, 0}, {"microbench_matrix", test_microbench_matrix, 0}, diff --git a/src/systemcmds/microbench/test_microbench_gravity.cpp b/src/systemcmds/microbench/test_microbench_gravity.cpp new file mode 100644 index 00000000000..23f066e67f8 --- /dev/null +++ b/src/systemcmds/microbench/test_microbench_gravity.cpp @@ -0,0 +1,141 @@ +/**************************************************************************** + * + * Copyright (C) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file test_microbench_gravity.cpp + * Benchmark for LatLonAlt::Wgs84::gravity() — the Somigliana latitude-dependent + * gravity helper used by EKF2 and SIH. Measures the one-time cost of the + * sin + sqrt computation on the target MCU. + */ + +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include + +namespace MicroBenchGravity +{ + +#ifdef __PX4_NUTTX +#include +static irqstate_t flags; +#endif + +void lock() +{ +#ifdef __PX4_NUTTX + flags = px4_enter_critical_section(); +#endif +} + +void unlock() +{ +#ifdef __PX4_NUTTX + px4_leave_critical_section(flags); +#endif +} + +#define PERF(name, op, count) do { \ + reset(); \ + perf_counter_t p = perf_alloc(PC_ELAPSED, name); \ + for (int rep = 0; rep < 10; rep++) { \ + px4_usleep(1000); \ + lock(); \ + perf_begin(p); \ + for (int i = 0; i < (count)/10; i++) { \ + op; \ + op; \ + op; \ + op; \ + op; \ + op; \ + op; \ + op; \ + op; \ + op; \ + } \ + perf_end(p); \ + unlock(); \ + reset(); \ + } \ + perf_print_counter(p); \ + perf_free(p); \ + } while (0) + +class MicroBenchGravity : public UnitTest +{ +public: + virtual bool run_tests(); + +private: + bool time_gravity(); + void reset(); + + // volatile so the compiler cannot eliminate the call as dead code + volatile double lat_rad; + volatile float gravity_out; +}; + +void MicroBenchGravity::reset() +{ + srand(time(nullptr)); + // random latitude in [-pi/2, pi/2] — representative of real flight locations + lat_rad = ((double)rand() / RAND_MAX - 0.5) * M_PI; + gravity_out = 0.f; +} + +bool MicroBenchGravity::time_gravity() +{ + // 100 calls: the function runs at most once per ~111 km of latitude travel, + // so even 1 call cost is the relevant metric; 100 gives a stable average. + PERF("gravity() 100 ops", gravity_out = LatLonAlt::Wgs84::gravity(lat_rad), 100); + return true; +} + +bool MicroBenchGravity::run_tests() +{ + ut_run_test(time_gravity); + return (_tests_failed == 0); +} + +ut_declare_test_c(test_microbench_gravity, MicroBenchGravity) + +} // namespace MicroBenchGravity