Skip to content

Commit

Permalink
Add waitForMakers function and avoid segfaults when writing to file.
Browse files Browse the repository at this point in the history
Use this to wait for any writing of markers to finish. Used when the dumping to file is stopped,
and when the I/O thread is stopped. Sometimes a marker was written after
closing the dump file. Check if the dumpfile is not nullptr both when
writing a marker and when writing power data
  • Loading branch information
loostrum committed Nov 15, 2024
1 parent 517ab8b commit 038addb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions host/include/PowerSensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class PowerSensor {
int openDevice(std::string device);
std::queue<char> markers;
void writeMarker();
void waitForMarkers(int timeout=500);

void initializeSensorPairs();
void updateSensorPairs();
Expand Down
36 changes: 33 additions & 3 deletions host/src/PowerSensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,11 @@ void PowerSensor::mark(char name) {
*
*/
void PowerSensor::writeMarker() {
std::unique_lock<std::mutex> lock(dumpFileMutex);
*dumpFile << "M " << markers.front() << std::endl;
markers.pop();
if (dumpFile != nullptr) {
std::unique_lock<std::mutex> lock(dumpFileMutex);
*dumpFile << "M " << markers.front() << std::endl;
markers.pop();
}
}

/**
Expand All @@ -357,6 +359,24 @@ void PowerSensor::mark(const State &startState, const State &stopState, std::str
}
}

/**
* @brief Wait for all markers to be written to the dump file
*
* @param timeout maximum waiting time in milliseconds
*/
void PowerSensor::waitForMarkers(int timeout) {
std::chrono::duration<double, std::milli> elapsed;
auto tstart = std::chrono::high_resolution_clock::now();
while (markers.size() != 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
elapsed = std::chrono::high_resolution_clock::now() - tstart;
if (elapsed.count() > timeout) {
break;
}
}
std::cout << "Marker waiting time: " << elapsed.count() << std::endl;
}

/**
* @brief thread to continuously read sensor values from device
*
Expand Down Expand Up @@ -418,6 +438,8 @@ void PowerSensor::startIOThread() {
*
*/
void PowerSensor::stopIOThread() {
// first ensure there are no markers still to be sent from the device
waitForMarkers();
if (thread != nullptr) {
writeCharToDevice('X');
thread->join();
Expand All @@ -432,6 +454,11 @@ void PowerSensor::stopIOThread() {
* @param dumpFileName
*/
void PowerSensor::dump(std::string dumpFileName) {
// if dumping should be stopped, first wait until all markers are written
if (dumpFileName.empty()) {
waitForMarkers();
}

std::unique_lock<std::mutex> lock(dumpFileMutex);
dumpFile = std::unique_ptr<std::ofstream>(dumpFileName.empty() ? nullptr: new std::ofstream(dumpFileName));
if (!dumpFileName.empty()) {
Expand All @@ -449,6 +476,9 @@ void PowerSensor::dump(std::string dumpFileName) {
*
*/
void PowerSensor::dumpCurrentWattToFile() {
if (dumpFile == nullptr) {
return;
}
std::unique_lock<std::mutex> lock(dumpFileMutex);
double totalWatt = 0;
double time = omp_get_wtime();
Expand Down

0 comments on commit 038addb

Please sign in to comment.