Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_UUID_H_ |
| 2 | #define AOS_EVENTS_LOGGING_UUID_H_ |
| 3 | |
| 4 | #include <array> |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 5 | #include <ostream> |
James Kuszmaul | a791b76 | 2023-07-13 14:56:21 -0700 | [diff] [blame] | 6 | #include <random> |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include "absl/types/span.h" |
| 10 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 11 | |
| 12 | namespace aos { |
| 13 | |
| 14 | // Class to generate and hold a UUID. |
| 15 | class UUID { |
| 16 | public: |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 17 | // Size of a UUID both as a string and the raw data. |
| 18 | static constexpr size_t kStringSize = 36; |
| 19 | static constexpr size_t kDataSize = 16; |
| 20 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 21 | // Returns a randomly generated UUID. This is known as a UUID4. |
James Kuszmaul | a791b76 | 2023-07-13 14:56:21 -0700 | [diff] [blame] | 22 | // The first Random() call in a thread will tend to be slightly slower than |
| 23 | // the rest so that it can seed the pseudo-random number generator used |
| 24 | // internally. |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 25 | static UUID Random(); |
| 26 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 27 | // Returns a uuid with all '0's. |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame] | 28 | static constexpr UUID Zero() { |
| 29 | UUID result; |
| 30 | std::memset(result.data_.data(), 0, result.data_.size()); |
| 31 | return result; |
| 32 | } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 33 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 34 | // Converts a string UUID of the form 00000000-0000-0000-0000-000000000000 to |
| 35 | // a UUID. |
| 36 | static UUID FromString(std::string_view string); |
| 37 | static UUID FromString(const flatbuffers::String *string); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 38 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 39 | // Converts a 16 byte vector (128 bits) to a UUID. This requires no |
| 40 | // transformation. |
| 41 | static UUID FromVector(const flatbuffers::Vector<uint8_t> *data); |
| 42 | |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame] | 43 | // Initializes new UUID from data. |
| 44 | static UUID FromSpan(absl::Span<const uint8_t> data); |
| 45 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 46 | // Returns the boot UUID for the current linux computer. |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 47 | static UUID BootUUID(); |
| 48 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 49 | // Default constructor which builds an uninitialized UUID. Use one of the |
| 50 | // static methods if you want something more useful. |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame] | 51 | constexpr UUID() : data_() {} |
Brian Silverman | 2596976 | 2022-08-24 19:54:00 -0700 | [diff] [blame] | 52 | constexpr UUID(const UUID &uuid) = default; |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame] | 53 | |
Brian Silverman | 2596976 | 2022-08-24 19:54:00 -0700 | [diff] [blame] | 54 | constexpr UUID &operator=(const UUID &other) = default; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 55 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 56 | // Packs this UUID into a flatbuffer as a string. |
| 57 | flatbuffers::Offset<flatbuffers::String> PackString( |
| 58 | flatbuffers::FlatBufferBuilder *fbb) const; |
| 59 | // Copies this UUID as a string into the memory pointed by result. Result |
| 60 | // must be at least kStringSize long. |
| 61 | void CopyTo(char *result) const; |
| 62 | // Returns this UUID as a string. |
| 63 | std::string ToString() const; |
| 64 | |
| 65 | // Packs the UUID bytes directly into a vector. |
| 66 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> PackVector( |
| 67 | flatbuffers::FlatBufferBuilder *fbb) const; |
| 68 | |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame] | 69 | // Returns a human-readable string representing this UUID. |
| 70 | // |
| 71 | // This is done without any memory allocation, which means it's returned in a |
| 72 | // thread-local buffer. |
| 73 | // |
| 74 | // Be careful using this. It's mostly useful for low-level tracing of UUIDs |
| 75 | // through the system. |
| 76 | const char *thread_local_string() const { |
Austin Schuh | f7bfb65 | 2023-08-25 14:22:50 -0700 | [diff] [blame^] | 77 | thread_local char buffer[kStringSize + 1]; |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame] | 78 | CopyTo(buffer); |
| 79 | return buffer; |
| 80 | } |
| 81 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 82 | // Returns the underlying UUID data. |
| 83 | absl::Span<const uint8_t> span() const { |
| 84 | return absl::Span<const uint8_t>(data_.data(), data_.size()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 87 | bool operator==(const UUID &other) const { return other.span() == span(); } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 88 | bool operator<(const UUID &other) const { return other.span() < span(); } |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 89 | bool operator!=(const UUID &other) const { return other.span() != span(); } |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 90 | |
| 91 | private: |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 92 | friend std::ostream &operator<<(std::ostream &os, const UUID &uuid); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 93 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 94 | // Encoded storage for the data. |
| 95 | std::array<uint8_t, kDataSize> data_; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 98 | std::ostream &operator<<(std::ostream &os, const UUID &uuid); |
| 99 | |
James Kuszmaul | a791b76 | 2023-07-13 14:56:21 -0700 | [diff] [blame] | 100 | namespace internal { |
| 101 | // Initializes a mt19937 with as much entropy as it can take (rather than just a |
| 102 | // 32-bit value from std::random_device). |
| 103 | // Exposed for testing purposes. |
| 104 | std::mt19937 FullySeededRandomGenerator(); |
| 105 | } // namespace internal |
| 106 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 107 | } // namespace aos |
| 108 | |
| 109 | #endif // AOS_EVENTS_LOGGING_UUID_H_ |