blob: 22a488df9333700a716af96da9bf18099950a172 [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
Nikhil Raj5b1bcc92021-06-08 12:31:50 +01008#include "IProfilingGuidGenerator.hpp"
9#include "ProfilingGuid.hpp"
Nikhil Raj7dcc6972021-04-30 15:44:24 +010010
Jim Flynnab845752019-10-25 13:17:30 +010011#include <functional>
Finn Williams38939ff2020-04-16 16:57:59 +010012#include <mutex>
Jim Flynnab845752019-10-25 13:17:30 +010013
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000014namespace arm
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010015{
16
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000017namespace pipe
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010018{
19
Jim Flynn00f3aaf2019-10-24 11:58:06 +010020class ProfilingGuidGenerator : public IProfilingGuidGenerator
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010021{
22public:
23 /// Construct a generator with the default address space static/dynamic partitioning
Jim Flynnab845752019-10-25 13:17:30 +010024 ProfilingGuidGenerator() : m_Sequence(0) {}
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010025
26 /// Return the next random Guid in the sequence
Jim Flynnab845752019-10-25 13:17:30 +010027 inline ProfilingDynamicGuid NextGuid() override
28 {
Jim Flynn870b96c2022-03-25 21:24:56 +000029#if !defined(ARMNN_DISABLE_THREADS)
Finn Williams38939ff2020-04-16 16:57:59 +010030 std::lock_guard<std::mutex> sequencelock(m_SequenceMutex);
Jim Flynn870b96c2022-03-25 21:24:56 +000031#endif
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000032 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 Flynnab845752019-10-25 13:17:30 +010039 return guid;
40 }
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010041
Jim Flynn00f3aaf2019-10-24 11:58:06 +010042 /// Create a ProfilingStaticGuid based on a hash of the string
Jim Flynnab845752019-10-25 13:17:30 +010043 inline ProfilingStaticGuid GenerateStaticId(const std::string& str) override
44 {
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000045 uint64_t staticHash = m_Hash(str) | MIN_STATIC_GUID;
46 return ProfilingStaticGuid(staticHash);
Jim Flynnab845752019-10-25 13:17:30 +010047 }
48
Jim Flynn6398a982020-05-27 17:05:21 +010049 /// Reset the generator back to zero. Used mainly for test.
50 inline void Reset()
51 {
Jim Flynn870b96c2022-03-25 21:24:56 +000052#if !defined(ARMNN_DISABLE_THREADS)
Jim Flynn6398a982020-05-27 17:05:21 +010053 std::lock_guard<std::mutex> sequencelock(m_SequenceMutex);
Jim Flynn870b96c2022-03-25 21:24:56 +000054#endif
Jim Flynn6398a982020-05-27 17:05:21 +010055 m_Sequence = 0;
56 }
57
Jim Flynnab845752019-10-25 13:17:30 +010058private:
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000059 std::hash<std::string> m_Hash;
Finn Williams38939ff2020-04-16 16:57:59 +010060 uint64_t m_Sequence;
Jim Flynn870b96c2022-03-25 21:24:56 +000061#if !defined(ARMNN_DISABLE_THREADS)
Finn Williams38939ff2020-04-16 16:57:59 +010062 std::mutex m_SequenceMutex;
Jim Flynn870b96c2022-03-25 21:24:56 +000063#endif
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010064};
65
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000066} // namespace pipe
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010067
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000068} // namespace arm