2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-04-01 23:10:28 -04:00
|
|
|
|
|
|
|
|
#include "frc/Tracer.h"
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2020-04-05 23:09:21 -07:00
|
|
|
#include <wpi/SmallString.h>
|
2020-04-01 23:10:28 -04:00
|
|
|
#include <wpi/raw_ostream.h>
|
|
|
|
|
|
2021-05-24 23:36:26 -07:00
|
|
|
#include "frc/Errors.h"
|
2020-04-05 23:09:21 -07:00
|
|
|
|
2020-04-01 23:10:28 -04:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
Tracer::Tracer() {
|
|
|
|
|
ResetTimer();
|
|
|
|
|
}
|
2020-04-01 23:10:28 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Tracer::ResetTimer() {
|
|
|
|
|
m_startTime = hal::fpga_clock::now();
|
|
|
|
|
}
|
2020-04-01 23:10:28 -04:00
|
|
|
|
|
|
|
|
void Tracer::ClearEpochs() {
|
|
|
|
|
ResetTimer();
|
|
|
|
|
m_epochs.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void Tracer::AddEpoch(std::string_view epochName) {
|
2020-04-01 23:10:28 -04:00
|
|
|
auto currentTime = hal::fpga_clock::now();
|
|
|
|
|
m_epochs[epochName] = currentTime - m_startTime;
|
|
|
|
|
m_startTime = currentTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Tracer::PrintEpochs() {
|
2020-04-05 23:09:21 -07:00
|
|
|
wpi::SmallString<128> buf;
|
|
|
|
|
wpi::raw_svector_ostream os(buf);
|
|
|
|
|
PrintEpochs(os);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!buf.empty()) {
|
2021-05-24 23:36:26 -07:00
|
|
|
FRC_ReportError(warn::Warning, "{}", buf.c_str());
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-04-05 23:09:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Tracer::PrintEpochs(wpi::raw_ostream& os) {
|
2020-04-01 23:10:28 -04:00
|
|
|
using std::chrono::duration_cast;
|
|
|
|
|
using std::chrono::microseconds;
|
|
|
|
|
|
|
|
|
|
auto now = hal::fpga_clock::now();
|
|
|
|
|
if (now - m_lastEpochsPrintTime > kMinPrintPeriod) {
|
|
|
|
|
m_lastEpochsPrintTime = now;
|
|
|
|
|
for (const auto& epoch : m_epochs) {
|
2021-06-06 16:13:58 -07:00
|
|
|
os << fmt::format(
|
|
|
|
|
"\t{}: {:.6f}s\n", epoch.getKey(),
|
|
|
|
|
duration_cast<microseconds>(epoch.getValue()).count() / 1.0e6);
|
2020-04-01 23:10:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|