blob: 86f9ce09de8d993338e31da45260519928d72d95 [file] [log] [blame]
Narumol Prangnawarat15effd82019-10-22 14:17:11 +01001//
Jim Flynn6398a982020-05-27 17:05:21 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Narumol Prangnawarat15effd82019-10-22 14:17:11 +01003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Colm Donelan5ccb33d2020-01-24 16:27:02 +00008#include "armnn/profiling/IProfilingGuidGenerator.hpp"
Narumol Prangnawarat15effd82019-10-22 14:17:11 +01009
Jim Flynnab845752019-10-25 13:17:30 +010010#include <functional>
Finn Williams38939ff2020-04-16 16:57:59 +010011#include <mutex>
Jim Flynnab845752019-10-25 13:17:30 +010012
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010013namespace armnn
14{
15
16namespace profiling
17{
18
Jim Flynn00f3aaf2019-10-24 11:58:06 +010019class ProfilingGuidGenerator : public IProfilingGuidGenerator
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010020{
21public:
22 /// Construct a generator with the default address space static/dynamic partitioning
Jim Flynnab845752019-10-25 13:17:30 +010023 ProfilingGuidGenerator() : m_Sequence(0) {}
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010024
25 /// Return the next random Guid in the sequence
Jim Flynnab845752019-10-25 13:17:30 +010026 inline ProfilingDynamicGuid NextGuid() override
27 {
Finn Williams38939ff2020-04-16 16:57:59 +010028 std::lock_guard<std::mutex> sequencelock(m_SequenceMutex);
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000029 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 Flynnab845752019-10-25 13:17:30 +010036 return guid;
37 }
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010038
Jim Flynn00f3aaf2019-10-24 11:58:06 +010039 /// Create a ProfilingStaticGuid based on a hash of the string
Jim Flynnab845752019-10-25 13:17:30 +010040 inline ProfilingStaticGuid GenerateStaticId(const std::string& str) override
41 {
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000042 uint64_t staticHash = m_Hash(str) | MIN_STATIC_GUID;
43 return ProfilingStaticGuid(staticHash);
Jim Flynnab845752019-10-25 13:17:30 +010044 }
45
Jim Flynn6398a982020-05-27 17:05:21 +010046 /// 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 Flynnab845752019-10-25 13:17:30 +010053private:
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000054 std::hash<std::string> m_Hash;
Finn Williams38939ff2020-04-16 16:57:59 +010055 uint64_t m_Sequence;
56 std::mutex m_SequenceMutex;
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010057};
58
59} // namespace profiling
60
61} // namespace armnn