Skip to content

Commit

Permalink
[wpiutil] Add DataLog Stop()
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Oct 31, 2023
1 parent a4a8ad9 commit 4f028a3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
1 change: 1 addition & 0 deletions ntcoreffi/src/main/native/symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ WPI_DataLog_Resume
WPI_DataLog_SetFilename
WPI_DataLog_SetMetadata
WPI_DataLog_Start
WPI_DataLog_Stop
WPI_DestroyEvent
WPI_DestroySemaphore
WPI_DestroySignalObject
Expand Down
33 changes: 25 additions & 8 deletions wpiutil/src/main/native/cpp/DataLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,16 @@ void DataLog::AddSchema(std::string_view name, std::string_view type,
if (entryInfo.id != 0) {
return; // don't add duplicates
}
entryInfo.schemaData.assign(schema.begin(), schema.end());
int entry = StartImpl(fullName, type, {}, timestamp);

// inline AppendRaw() without releasing lock
if (entry <= 0) {
[[unlikely]] return; // should never happen, but check anyway
}
if (m_stopped) {
[[unlikely]] return;
}
StartRecord(entry, timestamp, schema.size(), 0);
AppendImpl(schema);
}
Expand Down Expand Up @@ -521,9 +525,9 @@ int DataLog::StartImpl(std::string_view name, std::string_view type,
if (entryInfo.id == 0) {
entryInfo.id = ++m_lastId;
}
auto& savedCount = m_entryCounts[entryInfo.id];
++savedCount;
if (savedCount > 1) {
auto& entryInfo2 = m_entryIds[entryInfo.id];
++entryInfo2.count;
if (entryInfo2.count > 1) {
if (entryInfo.type != type) {
WPI_ERROR(m_msglog,
"type mismatch for '{}': was '{}', requested '{}'; ignoring",
Expand All @@ -533,6 +537,12 @@ int DataLog::StartImpl(std::string_view name, std::string_view type,
return entryInfo.id;
}
entryInfo.type = type;
entryInfo2.metadata = metadata;

if (m_stopped) {
[[unlikely]] return entryInfo.id;
}

size_t strsize = name.size() + type.size() + metadata.size();
uint8_t* buf = StartRecord(0, timestamp, 5 + 12 + strsize, 5);
*buf++ = impl::kControlStart;
Expand All @@ -549,15 +559,18 @@ void DataLog::Finish(int entry, int64_t timestamp) {
return;
}
std::scoped_lock lock{m_mutex};
auto& savedCount = m_entryCounts[entry];
if (savedCount == 0) {
auto& entryInfo2 = m_entryIds[entry];
if (entryInfo2.count == 0) {
return;
}
--savedCount;
if (savedCount != 0) {
--entryInfo2.count;
if (entryInfo2.count != 0) {
return;
}
m_entryCounts.erase(entry);
m_entryIds.erase(entry);
if (m_stopped) {
[[unlikely]] return;
}
uint8_t* buf = StartRecord(0, timestamp, 5, 5);
*buf++ = impl::kControlFinish;
wpi::support::endian::write32le(buf, entry);
Expand All @@ -569,6 +582,10 @@ void DataLog::SetMetadata(int entry, std::string_view metadata,
return;
}
std::scoped_lock lock{m_mutex};
m_entryIds[entry].metadata = metadata;
if (m_stopped) {
[[unlikely]] return;
}
uint8_t* buf = StartRecord(0, timestamp, 5 + 4 + metadata.size(), 5);
*buf++ = impl::kControlSetMetadata;
wpi::support::endian::write32le(buf, entry);
Expand Down
30 changes: 27 additions & 3 deletions wpiutil/src/main/native/include/wpi/DataLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,18 @@ class DataLog final {
void Pause();

/**
* Resumes appending of data records to the log.
* Resumes appending of data records to the log. If called after Stop(),
* opens a new file (with random name if SetFilename was not called after
* Stop()) and appends Start records and schema data values for all previously
* started entries and schemas.
*/
void Resume();

/**
* Stops appending all records to the log, and closes the log file.
*/
void Stop();

/**
* Returns whether there is a data schema already registered with the given
* name.
Expand Down Expand Up @@ -475,6 +483,7 @@ class DataLog final {
bool m_active{true};
bool m_doFlush{false};
bool m_paused{false};
bool m_stopped{false};
double m_period;
std::string m_extraHeader;
std::string m_newFilename;
Expand All @@ -483,10 +492,15 @@ class DataLog final {
std::vector<Buffer> m_outgoing;
struct EntryInfo {
std::string type;
std::vector<uint8_t> schemaData; // only set for schema entries
int id{0};
};
wpi::StringMap<EntryInfo> m_entries;
wpi::DenseMap<int, unsigned int> m_entryCounts;
struct EntryInfo2 {
std::string metadata;
unsigned int count;
};
wpi::DenseMap<int, EntryInfo2> m_entryIds;
int m_lastId = 0;
std::thread m_thread;
};
Expand Down Expand Up @@ -1113,12 +1127,22 @@ void WPI_DataLog_Flush(struct WPI_DataLog* datalog);
void WPI_DataLog_Pause(struct WPI_DataLog* datalog);

/**
* Resumes appending of data records to the log.
* Resumes appending of data records to the log. If called after Stop(),
* opens a new file (with random name if SetFilename was not called after
* Stop()) and appends Start records and schema data values for all previously
* started entries and schemas.
*
* @param datalog data log
*/
void WPI_DataLog_Resume(struct WPI_DataLog* datalog);

/**
* Stops appending all records to the log, and closes the log file.
*
* @param datalog data log
*/
void WPI_DataLog_Stop(struct WPI_DataLog* datalog);

/**
* Start an entry. Duplicate names are allowed (with the same type), and
* result in the same index being returned (Start/Finish are reference
Expand Down

0 comments on commit 4f028a3

Please sign in to comment.