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 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 14 | namespace arm |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 15 | { |
| 16 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 17 | namespace pipe |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 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 | { |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame^] | 29 | #if !defined(ARMNN_DISABLE_THREADS) |
Finn Williams | 38939ff | 2020-04-16 16:57:59 +0100 | [diff] [blame] | 30 | std::lock_guard<std::mutex> sequencelock(m_SequenceMutex); |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame^] | 31 | #endif |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 32 | ProfilingDynamicGuid guid(m_Sequence); |
| 33 | m_Sequence++; |
| 34 | if (m_Sequence >= MIN_STATIC_GUID) |
| 35 | { |
| 36 | // Reset the sequence to 0 when it reaches the upper bound of dynamic guid |
| 37 | m_Sequence = 0; |
| 38 | } |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 39 | return guid; |
| 40 | } |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 41 | |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 42 | /// Create a ProfilingStaticGuid based on a hash of the string |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 43 | inline ProfilingStaticGuid GenerateStaticId(const std::string& str) override |
| 44 | { |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 45 | uint64_t staticHash = m_Hash(str) | MIN_STATIC_GUID; |
| 46 | return ProfilingStaticGuid(staticHash); |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 49 | /// Reset the generator back to zero. Used mainly for test. |
| 50 | inline void Reset() |
| 51 | { |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame^] | 52 | #if !defined(ARMNN_DISABLE_THREADS) |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 53 | std::lock_guard<std::mutex> sequencelock(m_SequenceMutex); |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame^] | 54 | #endif |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 55 | m_Sequence = 0; |
| 56 | } |
| 57 | |
Jim Flynn | ab84575 | 2019-10-25 13:17:30 +0100 | [diff] [blame] | 58 | private: |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 59 | std::hash<std::string> m_Hash; |
Finn Williams | 38939ff | 2020-04-16 16:57:59 +0100 | [diff] [blame] | 60 | uint64_t m_Sequence; |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame^] | 61 | #if !defined(ARMNN_DISABLE_THREADS) |
Finn Williams | 38939ff | 2020-04-16 16:57:59 +0100 | [diff] [blame] | 62 | std::mutex m_SequenceMutex; |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame^] | 63 | #endif |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 66 | } // namespace pipe |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 67 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 68 | } // namespace arm |