blob: 1375411ff108a95125f2abef10d3eec1cf1b24ff [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Schuh1e69f942020-11-14 15:06:14 -08004
5#include "frc/Tracer.h"
6
Austin Schuh812d0d12021-11-04 20:16:48 -07007#include <fmt/format.h>
Austin Schuh1e69f942020-11-14 15:06:14 -08008#include <wpi/SmallString.h>
9#include <wpi/raw_ostream.h>
10
Austin Schuh812d0d12021-11-04 20:16:48 -070011#include "frc/Errors.h"
Austin Schuh1e69f942020-11-14 15:06:14 -080012
13using namespace frc;
14
Austin Schuh812d0d12021-11-04 20:16:48 -070015Tracer::Tracer() {
16 ResetTimer();
17}
Austin Schuh1e69f942020-11-14 15:06:14 -080018
Austin Schuh812d0d12021-11-04 20:16:48 -070019void Tracer::ResetTimer() {
20 m_startTime = hal::fpga_clock::now();
21}
Austin Schuh1e69f942020-11-14 15:06:14 -080022
23void Tracer::ClearEpochs() {
24 ResetTimer();
25 m_epochs.clear();
26}
27
Austin Schuh812d0d12021-11-04 20:16:48 -070028void Tracer::AddEpoch(std::string_view epochName) {
Austin Schuh1e69f942020-11-14 15:06:14 -080029 auto currentTime = hal::fpga_clock::now();
30 m_epochs[epochName] = currentTime - m_startTime;
31 m_startTime = currentTime;
32}
33
34void Tracer::PrintEpochs() {
35 wpi::SmallString<128> buf;
36 wpi::raw_svector_ostream os(buf);
37 PrintEpochs(os);
Austin Schuh812d0d12021-11-04 20:16:48 -070038 if (!buf.empty()) {
39 FRC_ReportError(warn::Warning, "{}", buf.c_str());
40 }
Austin Schuh1e69f942020-11-14 15:06:14 -080041}
42
43void 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 Schuh812d0d12021-11-04 20:16:48 -070051 os << fmt::format(
52 "\t{}: {:.6f}s\n", epoch.getKey(),
53 duration_cast<microseconds>(epoch.getValue()).count() / 1.0e6);
Austin Schuh1e69f942020-11-14 15:06:14 -080054 }
55 }
56}