2021-07-13 20:22:54 +02:00
|
|
|
#include "fsfw/timemanager/Stopwatch.h"
|
2022-02-02 10:29:30 +01:00
|
|
|
|
2021-07-13 20:22:54 +02:00
|
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
2021-01-27 18:03:56 +01:00
|
|
|
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-08-13 20:53:35 +02:00
|
|
|
#include <iomanip>
|
2021-01-27 18:03:56 +01:00
|
|
|
#endif
|
2020-08-13 20:53:35 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
Stopwatch::Stopwatch(bool displayOnDestruction, StopwatchDisplayMode displayMode)
|
|
|
|
: displayOnDestruction(displayOnDestruction), displayMode(displayMode) {
|
|
|
|
// Measures start time on initialization.
|
2023-02-24 16:39:49 +01:00
|
|
|
Clock::getClock_timeval(&startTime);
|
2020-08-13 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void Stopwatch::start() { Clock::getUptime(&startTime); }
|
2020-08-13 20:53:35 +02:00
|
|
|
|
2020-09-18 12:27:40 +02:00
|
|
|
dur_millis_t Stopwatch::stop(bool display) {
|
2022-02-02 10:29:30 +01:00
|
|
|
stopInternal();
|
|
|
|
if (display) {
|
|
|
|
this->display();
|
|
|
|
}
|
|
|
|
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
|
2020-08-13 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 12:27:40 +02:00
|
|
|
double Stopwatch::stopSeconds() {
|
2022-02-02 10:29:30 +01:00
|
|
|
stopInternal();
|
|
|
|
return timevalOperations::toDouble(elapsedTime);
|
2020-08-13 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stopwatch::display() {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (displayMode == StopwatchDisplayMode::MILLIS) {
|
|
|
|
dur_millis_t timeMillis =
|
|
|
|
static_cast<dur_millis_t>(elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000);
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::info << "Stopwatch: Operation took " << timeMillis << " milliseconds" << std::endl;
|
2021-01-22 12:21:45 +01:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printInfo("Stopwatch: Operation took %lu milliseconds\n\r",
|
|
|
|
static_cast<unsigned int>(timeMillis));
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
} else if (displayMode == StopwatchDisplayMode::SECONDS) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::info << "Stopwatch: Operation took " << std::setprecision(3) << std::fixed
|
|
|
|
<< timevalOperations::toDouble(elapsedTime) << " seconds" << std::endl;
|
2021-01-22 12:21:45 +01:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printInfo("Stopwatch: Operation took %.3f seconds\n\r",
|
|
|
|
static_cast<float>(timevalOperations::toDouble(elapsedTime)));
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2020-08-13 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Stopwatch::~Stopwatch() {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (displayOnDestruction) {
|
|
|
|
stopInternal();
|
|
|
|
display();
|
|
|
|
}
|
2020-08-13 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stopwatch::setDisplayMode(StopwatchDisplayMode displayMode) {
|
2022-02-02 10:29:30 +01:00
|
|
|
this->displayMode = displayMode;
|
2020-08-13 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
StopwatchDisplayMode Stopwatch::getDisplayMode() const { return displayMode; }
|
2020-08-13 20:53:35 +02:00
|
|
|
|
|
|
|
void Stopwatch::stopInternal() {
|
2022-02-02 10:29:30 +01:00
|
|
|
timeval endTime;
|
2023-02-24 16:39:49 +01:00
|
|
|
Clock::getClock_timeval(&endTime);
|
2022-02-02 10:29:30 +01:00
|
|
|
elapsedTime = endTime - startTime;
|
2020-08-13 20:53:35 +02:00
|
|
|
}
|