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
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <chrono>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <string_view>
|
2020-04-01 23:10:28 -04:00
|
|
|
|
|
|
|
|
#include <hal/cpp/fpga_clock.h>
|
|
|
|
|
#include <wpi/StringMap.h>
|
|
|
|
|
|
2020-04-05 23:09:21 -07:00
|
|
|
namespace wpi {
|
|
|
|
|
class raw_ostream;
|
|
|
|
|
} // namespace wpi
|
|
|
|
|
|
2020-04-01 23:10:28 -04:00
|
|
|
namespace frc {
|
|
|
|
|
/**
|
|
|
|
|
* A class for keeping track of how much time it takes for different parts of
|
|
|
|
|
* code to execute. This is done with epochs, that are added to calls to
|
|
|
|
|
* AddEpoch() and can be printed with a call to PrintEpochs().
|
|
|
|
|
*
|
|
|
|
|
* Epochs are a way to partition the time elapsed so that when overruns occur,
|
|
|
|
|
* one can determine which parts of an operation consumed the most time.
|
|
|
|
|
*/
|
|
|
|
|
class Tracer {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a Tracer instance.
|
|
|
|
|
*/
|
|
|
|
|
Tracer();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Restarts the epoch timer.
|
|
|
|
|
*/
|
|
|
|
|
void ResetTimer();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears all epochs.
|
|
|
|
|
*/
|
|
|
|
|
void ClearEpochs();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds time since last epoch to the list printed by PrintEpochs().
|
|
|
|
|
*
|
|
|
|
|
* Epochs are a way to partition the time elapsed so that when overruns occur,
|
|
|
|
|
* one can determine which parts of an operation consumed the most time.
|
|
|
|
|
*
|
|
|
|
|
* @param epochName The name to associate with the epoch.
|
|
|
|
|
*/
|
2021-06-06 16:13:58 -07:00
|
|
|
void AddEpoch(std::string_view epochName);
|
2020-04-01 23:10:28 -04:00
|
|
|
|
|
|
|
|
/**
|
2020-04-05 23:09:21 -07:00
|
|
|
* Prints list of epochs added so far and their times to the DriverStation.
|
2020-04-01 23:10:28 -04:00
|
|
|
*/
|
|
|
|
|
void PrintEpochs();
|
|
|
|
|
|
2020-04-05 23:09:21 -07:00
|
|
|
/**
|
|
|
|
|
* Prints list of epochs added so far and their times to a stream.
|
|
|
|
|
*
|
|
|
|
|
* @param os output stream
|
|
|
|
|
*/
|
|
|
|
|
void PrintEpochs(wpi::raw_ostream& os);
|
|
|
|
|
|
2020-04-01 23:10:28 -04:00
|
|
|
private:
|
|
|
|
|
static constexpr std::chrono::milliseconds kMinPrintPeriod{1000};
|
|
|
|
|
|
|
|
|
|
hal::fpga_clock::time_point m_startTime;
|
|
|
|
|
hal::fpga_clock::time_point m_lastEpochsPrintTime = hal::fpga_clock::epoch();
|
|
|
|
|
|
|
|
|
|
wpi::StringMap<std::chrono::nanoseconds> m_epochs;
|
|
|
|
|
};
|
|
|
|
|
} // namespace frc
|