Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/Tracer.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | #include <fmt/format.h> |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 8 | #include <wpi/SmallString.h> |
| 9 | #include <wpi/raw_ostream.h> |
| 10 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 11 | #include "frc/Errors.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 12 | |
| 13 | using namespace frc; |
| 14 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 15 | Tracer::Tracer() { |
| 16 | ResetTimer(); |
| 17 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 18 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 19 | void Tracer::ResetTimer() { |
| 20 | m_startTime = hal::fpga_clock::now(); |
| 21 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 22 | |
| 23 | void Tracer::ClearEpochs() { |
| 24 | ResetTimer(); |
| 25 | m_epochs.clear(); |
| 26 | } |
| 27 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 28 | void Tracer::AddEpoch(std::string_view epochName) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 29 | auto currentTime = hal::fpga_clock::now(); |
| 30 | m_epochs[epochName] = currentTime - m_startTime; |
| 31 | m_startTime = currentTime; |
| 32 | } |
| 33 | |
| 34 | void Tracer::PrintEpochs() { |
| 35 | wpi::SmallString<128> buf; |
| 36 | wpi::raw_svector_ostream os(buf); |
| 37 | PrintEpochs(os); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 38 | if (!buf.empty()) { |
| 39 | FRC_ReportError(warn::Warning, "{}", buf.c_str()); |
| 40 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void Tracer::PrintEpochs(wpi::raw_ostream& os) { |
| 44 | using std::chrono::duration_cast; |
| 45 | using std::chrono::microseconds; |
| 46 | |
| 47 | auto now = hal::fpga_clock::now(); |
| 48 | if (now - m_lastEpochsPrintTime > kMinPrintPeriod) { |
| 49 | m_lastEpochsPrintTime = now; |
| 50 | for (const auto& epoch : m_epochs) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 51 | os << fmt::format( |
| 52 | "\t{}: {:.6f}s\n", epoch.getKey(), |
| 53 | duration_cast<microseconds>(epoch.getValue()).count() / 1.0e6); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | } |