blob: 3798e0cc2d72aac08a816935e9a0fa29e1052c02 [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
Nikhil Raj7dcc6972021-04-30 15:44:24 +010010#include <common/include/ProfilingGuid.hpp>
11
Jim Flynnab845752019-10-25 13:17:30 +010012#include <functional>
Finn Williams38939ff2020-04-16 16:57:59 +010013#include <mutex>
Jim Flynnab845752019-10-25 13:17:30 +010014
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010015namespace armnn
16{
17
18namespace profiling
19{
20
Jim Flynn00f3aaf2019-10-24 11:58:06 +010021class ProfilingGuidGenerator : public IProfilingGuidGenerator
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010022{
23public:
24 /// Construct a generator with the default address space static/dynamic partitioning
Jim Flynnab845752019-10-25 13:17:30 +010025 ProfilingGuidGenerator() : m_Sequence(0) {}
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010026
27 /// Return the next random Guid in the sequence
Jim Flynnab845752019-10-25 13:17:30 +010028 inline ProfilingDynamicGuid NextGuid() override
29 {
Finn Williams38939ff2020-04-16 16:57:59 +010030 std::lock_guard<std::mutex> sequencelock(m_SequenceMutex);
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000031 ProfilingDynamicGuid guid(m_Sequence);
32 m_Sequence++;
33 if (m_Sequence >= MIN_STATIC_GUID)
34 {
35 // Reset the sequence to 0 when it reaches the upper bound of dynamic guid
36 m_Sequence = 0;
37 }
Jim Flynnab845752019-10-25 13:17:30 +010038 return guid;
39 }
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010040
Jim Flynn00f3aaf2019-10-24 11:58:06 +010041 /// Create a ProfilingStaticGuid based on a hash of the string
Jim Flynnab845752019-10-25 13:17:30 +010042 inline ProfilingStaticGuid GenerateStaticId(const std::string& str) override
43 {
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000044 uint64_t staticHash = m_Hash(str) | MIN_STATIC_GUID;
45 return ProfilingStaticGuid(staticHash);
Jim Flynnab845752019-10-25 13:17:30 +010046 }
47
Jim Flynn6398a982020-05-27 17:05:21 +010048 /// Reset the generator back to zero. Used mainly for test.
49 inline void Reset()
50 {
51 std::lock_guard<std::mutex> sequencelock(m_SequenceMutex);
52 m_Sequence = 0;
53 }
54
Jim Flynnab845752019-10-25 13:17:30 +010055private:
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000056 std::hash<std::string> m_Hash;
Finn Williams38939ff2020-04-16 16:57:59 +010057 uint64_t m_Sequence;
58 std::mutex m_SequenceMutex;
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010059};
60
61} // namespace profiling
62
63} // namespace armnn