Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Nikhil Raj | 5b1bcc9 | 2021-06-08 12:31:50 +0100 | [diff] [blame] | 8 | #include "IProfilingGuidGenerator.hpp" |
| 9 | #include "ProfilingGuid.hpp" |
Nikhil Raj | 7dcc697 | 2021-04-30 15:44:24 +0100 | [diff] [blame] | 10 | |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 11 | #include <functional> |
Finn Williams | 38939ff | 2020-04-16 16:57:59 +0100 | [diff] [blame] | 12 | #include <mutex> |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 13 | |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 14 | namespace armnn |
| 15 | { |
| 16 | |
| 17 | namespace profiling |
| 18 | { |
| 19 | |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 20 | class ProfilingGuidGenerator : public IProfilingGuidGenerator |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 21 | { |
| 22 | public: |
| 23 | /// Construct a generator with the default address space static/dynamic partitioning |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 24 | ProfilingGuidGenerator() : m_Sequence(0) {} |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 25 | |
| 26 | /// Return the next random Guid in the sequence |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 27 | inline ProfilingDynamicGuid NextGuid() override |
| 28 | { |
Finn Williams | 38939ff | 2020-04-16 16:57:59 +0100 | [diff] [blame] | 29 | std::lock_guard<std::mutex> sequencelock(m_SequenceMutex); |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 30 | ProfilingDynamicGuid guid(m_Sequence); |
| 31 | m_Sequence++; |
| 32 | if (m_Sequence >= MIN_STATIC_GUID) |
| 33 | { |
| 34 | // Reset the sequence to 0 when it reaches the upper bound of dynamic guid |
| 35 | m_Sequence = 0; |
| 36 | } |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 37 | return guid; |
| 38 | } |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 39 | |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 40 | /// Create a ProfilingStaticGuid based on a hash of the string |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 41 | inline ProfilingStaticGuid GenerateStaticId(const std::string& str) override |
| 42 | { |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 43 | uint64_t staticHash = m_Hash(str) | MIN_STATIC_GUID; |
| 44 | return ProfilingStaticGuid(staticHash); |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 47 | /// Reset the generator back to zero. Used mainly for test. |
| 48 | inline void Reset() |
| 49 | { |
| 50 | std::lock_guard<std::mutex> sequencelock(m_SequenceMutex); |
| 51 | m_Sequence = 0; |
| 52 | } |
| 53 | |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 54 | private: |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 55 | std::hash<std::string> m_Hash; |
Finn Williams | 38939ff | 2020-04-16 16:57:59 +0100 | [diff] [blame] | 56 | uint64_t m_Sequence; |
| 57 | std::mutex m_SequenceMutex; |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | } // namespace profiling |
| 61 | |
| 62 | } // namespace armnn |