xref: /aosp_15_r20/external/armnn/profiling/common/include/ProfilingGuidGenerator.hpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "IProfilingGuidGenerator.hpp"
9 #include "ProfilingGuid.hpp"
10 
11 #include <functional>
12 #include <mutex>
13 
14 namespace arm
15 {
16 
17 namespace pipe
18 {
19 
20 class ProfilingGuidGenerator : public IProfilingGuidGenerator
21 {
22 public:
23     /// Construct a generator with the default address space static/dynamic partitioning
ProfilingGuidGenerator()24     ProfilingGuidGenerator() : m_Sequence(0) {}
25 
26     /// Return the next random Guid in the sequence
NextGuid()27     inline ProfilingDynamicGuid NextGuid() override
28     {
29 #if !defined(ARMNN_DISABLE_THREADS)
30         std::lock_guard<std::mutex> sequencelock(m_SequenceMutex);
31 #endif
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         }
39         return guid;
40     }
41 
42     /// Create a ProfilingStaticGuid based on a hash of the string
GenerateStaticId(const std::string & str)43     inline ProfilingStaticGuid GenerateStaticId(const std::string& str) override
44     {
45         uint64_t staticHash = m_Hash(str) | MIN_STATIC_GUID;
46         return ProfilingStaticGuid(staticHash);
47     }
48 
49     /// Reset the generator back to zero. Used mainly for test.
Reset()50     inline void Reset()
51     {
52 #if !defined(ARMNN_DISABLE_THREADS)
53         std::lock_guard<std::mutex> sequencelock(m_SequenceMutex);
54 #endif
55         m_Sequence = 0;
56     }
57 
58 private:
59     std::hash<std::string> m_Hash;
60     uint64_t m_Sequence;
61 #if !defined(ARMNN_DISABLE_THREADS)
62     std::mutex m_SequenceMutex;
63 #endif
64 };
65 
66 } // namespace pipe
67 
68 } // namespace arm
69