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