|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Erik Henriksson |
| 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 | + |
| 16 | +using namespace Board; |
| 17 | +using namespace std::chrono_literals; |
| 18 | + |
| 19 | +constexpr uint32_t cycles = 100'000; |
| 20 | +volatile uint32_t f1counter = 0, f2counter = 0; |
| 21 | +uint32_t total_counter=0; |
| 22 | + |
| 23 | +void |
| 24 | +fiber_function1() |
| 25 | +{ |
| 26 | + MODM_LOG_INFO << MODM_FILE_INFO << modm::endl; |
| 27 | + while (++f1counter < cycles) { modm::fiber::yield(); total_counter++; } |
| 28 | +} |
| 29 | + |
| 30 | +void |
| 31 | +fiber_function2(uint32_t cyc) |
| 32 | +{ |
| 33 | + MODM_LOG_INFO << MODM_FILE_INFO << modm::endl; |
| 34 | + while (++f2counter < cyc) { modm::fiber::yield(); total_counter++; } |
| 35 | +} |
| 36 | + |
| 37 | +struct Test |
| 38 | +{ |
| 39 | + void |
| 40 | + fiber_function3() |
| 41 | + { |
| 42 | + MODM_LOG_INFO << MODM_FILE_INFO << modm::endl; |
| 43 | + while (++f3counter < cycles) { modm::fiber::yield(); total_counter++; } |
| 44 | + } |
| 45 | + |
| 46 | + void |
| 47 | + fiber_function4(uint32_t cyc) |
| 48 | + { |
| 49 | + MODM_LOG_INFO << MODM_FILE_INFO << modm::endl; |
| 50 | + while (++f4counter < cyc) { modm::fiber::yield(); total_counter++; } |
| 51 | + } |
| 52 | + |
| 53 | + volatile uint32_t f3counter{0}; |
| 54 | + volatile uint32_t f4counter{0}; |
| 55 | +} test; |
| 56 | + |
| 57 | +modm::fiber::Stack<128> stack1; |
| 58 | +modm::fiber::Stack<128> stack2; |
| 59 | +modm::fiber::Stack<128> stack3; |
| 60 | +modm::fiber::Stack<128> stack4; |
| 61 | +modm::Fiber fiber1(stack1, fiber_function1); |
| 62 | +modm::Fiber fiber2(stack2, [](){ fiber_function2(cycles); }); |
| 63 | +modm::Fiber fiber3(stack3, [](){ test.fiber_function3(); }); |
| 64 | +modm::Fiber fiber4(stack4, [cyc=uint32_t(cycles)]() mutable { cyc++; test.fiber_function4(cyc); }); |
| 65 | + |
| 66 | +// ATmega2560@16MHz: 239996 yields in 2492668us, 96280 yields per second, 10386ns per yield |
| 67 | +int |
| 68 | +main() |
| 69 | +{ |
| 70 | + Board::initialize(); |
| 71 | + Board::LedD13::setOutput(); |
| 72 | + MODM_LOG_INFO << "Starting fiber modm::yield benchmark..." << modm::endl; |
| 73 | + MODM_LOG_INFO.flush(); |
| 74 | + |
| 75 | + const modm::PreciseTimestamp start = modm::PreciseClock::now(); |
| 76 | + modm::fiber::Scheduler::run(); |
| 77 | + const auto diff = (modm::PreciseClock::now() - start); |
| 78 | + |
| 79 | + MODM_LOG_INFO << "Benchmark done!" << modm::endl; |
| 80 | + MODM_LOG_INFO << "Executed " << total_counter << " yields in " << diff << modm::endl; |
| 81 | + MODM_LOG_INFO << uint32_t((total_counter * 1'000'000ull) / std::chrono::microseconds(diff).count()); |
| 82 | + MODM_LOG_INFO << " yields per second, "; |
| 83 | + MODM_LOG_INFO << uint32_t(std::chrono::nanoseconds(diff).count() / total_counter); |
| 84 | + MODM_LOG_INFO << "ns per yield" << modm::endl; |
| 85 | + MODM_LOG_INFO.flush(); |
| 86 | + |
| 87 | + while(1) ; |
| 88 | + return 0; |
| 89 | +} |
0 commit comments