This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Make StatsDuration thread-safe
ClosedPublic

Authored by labath on Jan 17 2022, 6:17 AM.

Details

Summary

std::chrono::duration types are not thread-safe, and they cannot be
concurrently updated from multiple threads. Currently, we were doing
such a thing (only) in the DWARF indexing code
(DWARFUnit::ExtractDIEsRWLocked), but I think it can easily happen that
someone else tries to update another statistic like this without
bothering to check for thread safety.

This patch changes the StatsDuration type from a simple typedef into a
class in its own right. The class stores the duration internally as
std::atomic<uint64_t> (so it can be updated atomically), but presents it
to its users as the usual chrono type (duration<float>).

Diff Detail

Event Timeline

labath created this revision.Jan 17 2022, 6:17 AM
labath requested review of this revision.Jan 17 2022, 6:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 17 2022, 6:17 AM
clayborg accepted this revision.Jan 18 2022, 12:16 PM

Thanks for finding a solution. I tried originally doing a std::atomic<double> but those are not supported. As long as you have verified that storing at a uint64_t reports the same kinds of durations and that they are accurate, I am good with this.

This revision is now accepted and ready to land.Jan 18 2022, 12:16 PM

The precision is now limited to microseconds (arbitrary choice that I though should be sufficient) whereas previously it was... floating. There are no changes besides that.

This revision was automatically updated to reflect the committed changes.