Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2020 FIRST. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "frc/Tracer.h" |
| 9 | |
| 10 | #include <wpi/Format.h> |
| 11 | #include <wpi/SmallString.h> |
| 12 | #include <wpi/raw_ostream.h> |
| 13 | |
| 14 | #include "frc/DriverStation.h" |
| 15 | |
| 16 | using namespace frc; |
| 17 | |
| 18 | Tracer::Tracer() { ResetTimer(); } |
| 19 | |
| 20 | void Tracer::ResetTimer() { m_startTime = hal::fpga_clock::now(); } |
| 21 | |
| 22 | void Tracer::ClearEpochs() { |
| 23 | ResetTimer(); |
| 24 | m_epochs.clear(); |
| 25 | } |
| 26 | |
| 27 | void Tracer::AddEpoch(wpi::StringRef epochName) { |
| 28 | auto currentTime = hal::fpga_clock::now(); |
| 29 | m_epochs[epochName] = currentTime - m_startTime; |
| 30 | m_startTime = currentTime; |
| 31 | } |
| 32 | |
| 33 | void Tracer::PrintEpochs() { |
| 34 | wpi::SmallString<128> buf; |
| 35 | wpi::raw_svector_ostream os(buf); |
| 36 | PrintEpochs(os); |
| 37 | if (!buf.empty()) DriverStation::ReportWarning(buf); |
| 38 | } |
| 39 | |
| 40 | void Tracer::PrintEpochs(wpi::raw_ostream& os) { |
| 41 | using std::chrono::duration_cast; |
| 42 | using std::chrono::microseconds; |
| 43 | |
| 44 | auto now = hal::fpga_clock::now(); |
| 45 | if (now - m_lastEpochsPrintTime > kMinPrintPeriod) { |
| 46 | m_lastEpochsPrintTime = now; |
| 47 | for (const auto& epoch : m_epochs) { |
| 48 | os << '\t' << epoch.getKey() << ": " |
| 49 | << wpi::format( |
| 50 | "%.6f", |
| 51 | duration_cast<microseconds>(epoch.getValue()).count() / 1.0e6) |
| 52 | << "s\n"; |
| 53 | } |
| 54 | } |
| 55 | } |