Skip to content

Commit

Permalink
Add option to put absolute timestamps in dumpfile
Browse files Browse the repository at this point in the history
  • Loading branch information
loostrum committed Dec 9, 2024
1 parent ddeeff5 commit 9493976
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion host/include/PowerSensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PowerSensor {

State read() const;

void dump(const std::string dumpFileName); // dumpFileName == 0 --> stop dumping
void dump(const std::string dumpFileName, bool useAbsoluteTimestamp=false); // dumpFileName == 0 --> stop dumping
void mark(char name);
void reset(bool dfuMode);

Expand Down Expand Up @@ -92,6 +92,7 @@ class PowerSensor {
bool readLevelFromDevice(unsigned int* sensorNumber, uint16_t* level, unsigned int* marker);

std::unique_ptr<std::ofstream> dumpFile;
bool useAbsoluteTimestamp;
void dumpCurrentWattToFile(const char markerChar);

Semaphore threadStarted;
Expand Down
12 changes: 10 additions & 2 deletions host/src/PowerSensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <chrono>
#include <cstring>
#include <iostream>
#include <iomanip>

#include "PowerSensor.hpp"

Expand Down Expand Up @@ -440,8 +441,9 @@ void PowerSensor::stopIOThread() {
* @brief Enable dumping of sensor values to the given output file.
*
* @param dumpFileName
* @param useAbsoluteTimestamp
*/
void PowerSensor::dump(std::string dumpFileName) {
void PowerSensor::dump(std::string dumpFileName, bool useAbsoluteTimestamp) {
// if dumping to a new file or dumping should be stopped, first wait until all markers are written
if (dumpFile != nullptr || dumpFileName.empty()) {
waitForMarkers();
Expand All @@ -450,6 +452,7 @@ void PowerSensor::dump(std::string dumpFileName) {
std::unique_lock<std::mutex> lock(dumpFileMutex);
dumpFile = std::unique_ptr<std::ofstream>(dumpFileName.empty() ? nullptr: new std::ofstream(dumpFileName));
if (!dumpFileName.empty()) {
this->useAbsoluteTimestamp = useAbsoluteTimestamp;
*dumpFile << "marker time dt_micro device_timestamp";
for (unsigned int pairID=0; pairID < MAX_PAIRS; pairID++) {
if (sensorPairs[pairID].inUse)
Expand All @@ -472,7 +475,12 @@ void PowerSensor::dumpCurrentWattToFile(const char markerChar) {
auto time = std::chrono::high_resolution_clock::now();
static auto previousTime = startTime;

*dumpFile << markerChar << ' ' << elapsedSeconds(startTime, time);
*dumpFile << markerChar << ' ';
if (useAbsoluteTimestamp) {
*dumpFile << std::fixed << std::setprecision(6) << std::chrono::duration_cast<std::chrono::microseconds>(time.time_since_epoch()).count() / 1.0e6;
} else {
*dumpFile << elapsedSeconds(startTime, time);
}
*dumpFile << ' ' << static_cast<int>(1e6 * elapsedSeconds(previousTime, time));
*dumpFile << ' ' << timestamp;
previousTime = time;
Expand Down

0 comments on commit 9493976

Please sign in to comment.