|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Andrey Kunitsyn |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#include <modm/board.hpp> |
| 13 | +#include <modm/debug/logger.hpp> |
| 14 | +#include <modm/processing.hpp> |
| 15 | +#include <mutex> |
| 16 | + |
| 17 | +using namespace Board; |
| 18 | +using namespace std::chrono_literals; |
| 19 | + |
| 20 | +// Create an IODeviceWrapper around the Uart Peripheral we want to use |
| 21 | +modm::IODeviceWrapper<Uart0, modm::IOBuffer::BlockIfFull> loggerDevice; |
| 22 | + |
| 23 | +// Set all four logger streams to use the UART |
| 24 | +modm::log::Logger modm::log::debug(loggerDevice); |
| 25 | +modm::log::Logger modm::log::info(loggerDevice); |
| 26 | +modm::log::Logger modm::log::warning(loggerDevice); |
| 27 | +modm::log::Logger modm::log::error(loggerDevice); |
| 28 | + |
| 29 | +constexpr uint32_t cycles = 1'000'000; |
| 30 | + |
| 31 | +static multicore::Mutex log_mutex; |
| 32 | + |
| 33 | +struct CoreData |
| 34 | +{ |
| 35 | + uint32_t total_counter = 0; |
| 36 | + uint32_t f1counter = 0; |
| 37 | + uint32_t f2counter = 0; |
| 38 | +}; |
| 39 | + |
| 40 | +void |
| 41 | +fiber_function1(CoreData& d) |
| 42 | +{ |
| 43 | + while (++d.f1counter < cycles) |
| 44 | + { |
| 45 | + modm::fiber::yield(); |
| 46 | + d.total_counter++; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void |
| 51 | +fiber_function2(CoreData& d) |
| 52 | +{ |
| 53 | + while (++d.f2counter < cycles) |
| 54 | + { |
| 55 | + modm::fiber::yield(); |
| 56 | + d.total_counter++; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// put cores to mostly equalent environment |
| 61 | +modm_core0_data CoreData d0; |
| 62 | +modm_core1_data CoreData d1; |
| 63 | + |
| 64 | +modm_core0_noinit modm::fiber::Stack<384> stack01; |
| 65 | +modm_core0_noinit modm::fiber::Stack<384> stack02; |
| 66 | +modm_core1_noinit modm::fiber::Stack<384> stack11; |
| 67 | +modm_core1_noinit modm::fiber::Stack<384> stack12; |
| 68 | + |
| 69 | +modm_core0_data |
| 70 | +modm::Fiber fiber01(stack01, []() { fiber_function1(d0); }, 0); |
| 71 | +modm_core0_data |
| 72 | +modm::Fiber fiber02(stack02, []() { fiber_function2(d0); }, 0); |
| 73 | +modm_core1_data |
| 74 | +modm::Fiber fiber11(stack11, []() { fiber_function1(d1); }, 1); |
| 75 | +modm_core1_data |
| 76 | +modm::Fiber fiber12(stack12, []() { fiber_function2(d1); }, 1); |
| 77 | + |
| 78 | +template<typename TimeDiff> |
| 79 | +static void |
| 80 | +print_result(const CoreData& d, TimeDiff diff) |
| 81 | +{ |
| 82 | + std::lock_guard<multicore::Mutex> g(log_mutex); |
| 83 | + MODM_LOG_INFO << "Benchmark for core" << multicore::Core::cpuId() << " done!" << modm::endl; |
| 84 | + MODM_LOG_INFO << "Executed " << d.total_counter << " yields in " << diff << modm::endl; |
| 85 | + MODM_LOG_INFO << ((d.total_counter * 1'000'000ull) / std::chrono::microseconds(diff).count()); |
| 86 | + MODM_LOG_INFO << " yields per second, "; |
| 87 | + MODM_LOG_INFO << (std::chrono::nanoseconds(diff).count() / d.total_counter); |
| 88 | + MODM_LOG_INFO << "ns per yield" << modm::endl; |
| 89 | + MODM_LOG_INFO.flush(); |
| 90 | +} |
| 91 | + |
| 92 | +void |
| 93 | +core1_main() |
| 94 | +{ |
| 95 | + const modm::PreciseTimestamp start = modm::PreciseClock::now(); |
| 96 | + modm::fiber::Scheduler::run(); |
| 97 | + const auto diff = (modm::PreciseClock::now() - start); |
| 98 | + |
| 99 | + print_result(d1, diff); |
| 100 | + while (true) __NOP(); |
| 101 | +} |
| 102 | + |
| 103 | +int |
| 104 | +main() |
| 105 | +{ |
| 106 | + Board::initialize(); |
| 107 | + |
| 108 | + // initialize Uart0 for MODM_LOG_* |
| 109 | + Uart0::connect<GpioOutput0::Tx>(); |
| 110 | + Uart0::initialize<Board::SystemClock, 115200_Bd>(); |
| 111 | + |
| 112 | + MODM_LOG_INFO << "Starting fiber modm::yield benchmark..." << modm::endl; |
| 113 | + MODM_LOG_INFO.flush(); |
| 114 | + |
| 115 | + multicore::Core1::run(core1_main); |
| 116 | + |
| 117 | + const modm::PreciseTimestamp start = modm::PreciseClock::now(); |
| 118 | + modm::fiber::Scheduler::run(); |
| 119 | + const auto diff = (modm::PreciseClock::now() - start); |
| 120 | + |
| 121 | + print_result(d0, diff); |
| 122 | + while (true) __NOP(); |
| 123 | + |
| 124 | + return 0; |
| 125 | +} |
0 commit comments