[wpilibc] Refactor Tracer functionality out of Watchdog class (#2456)

This commit is contained in:
Prateek Machiraju
2020-04-01 23:10:28 -04:00
committed by GitHub
parent c14b87b228
commit b9ee3ae030
4 changed files with 119 additions and 25 deletions

View File

@@ -0,0 +1,66 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <chrono>
#include <hal/cpp/fpga_clock.h>
#include <units/units.h>
#include <wpi/StringMap.h>
#include <wpi/StringRef.h>
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.
*/
void AddEpoch(wpi::StringRef epochName);
/**
* Prints list of epochs added so far and their times.
*/
void PrintEpochs();
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

View File

@@ -14,10 +14,11 @@
#include <hal/cpp/fpga_clock.h>
#include <units/units.h>
#include <wpi/SafeThread.h>
#include <wpi/StringMap.h>
#include <wpi/StringRef.h>
#include <wpi/deprecated.h>
#include "frc/Tracer.h"
namespace frc {
/**
@@ -155,9 +156,8 @@ class Watchdog {
hal::fpga_clock::time_point m_expirationTime;
std::function<void()> m_callback;
hal::fpga_clock::time_point m_lastTimeoutPrintTime = hal::fpga_clock::epoch();
hal::fpga_clock::time_point m_lastEpochsPrintTime = hal::fpga_clock::epoch();
wpi::StringMap<std::chrono::nanoseconds> m_epochs;
Tracer m_tracer;
bool m_isExpired = false;
bool m_suppressTimeoutMessage = false;