blob: bfee7642e9f27f750c6f465ea4675cb32f929aa0 [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 {
Finn Williams38939ff2020-04-16 16:57:59 +010029 std::lock_guard<std::mutex> sequencelock(m_SequenceMutex);
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000030 ProfilingDynamicGuid guid(m_Sequence);
31 m_Sequence++;
32 if (m_Sequence >= MIN_STATIC_GUID)
33 {
34 // Reset the sequence to 0 when it reaches the upper bound of dynamic guid
35 m_Sequence = 0;
36 }
Jim Flynnab845752019-10-25 13:17:30 +010037 return guid;
38 }
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010039
Jim Flynn00f3aaf2019-10-24 11:58:06 +010040 /// Create a ProfilingStaticGuid based on a hash of the string
Jim Flynnab845752019-10-25 13:17:30 +010041 inline ProfilingStaticGuid GenerateStaticId(const std::string& str) override
42 {
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000043 uint64_t staticHash = m_Hash(str) | MIN_STATIC_GUID;
44 return ProfilingStaticGuid(staticHash);
Jim Flynnab845752019-10-25 13:17:30 +010045 }
46
Jim Flynn6398a982020-05-27 17:05:21 +010047 /// Reset the generator back to zero. Used mainly for test.
48 inline void Reset()
49 {
50 std::lock_guard<std::mutex> sequencelock(m_SequenceMutex);
51 m_Sequence = 0;
52 }
53
Jim Flynnab845752019-10-25 13:17:30 +010054private:
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000055 std::hash<std::string> m_Hash;
Finn Williams38939ff2020-04-16 16:57:59 +010056 uint64_t m_Sequence;
57 std::mutex m_SequenceMutex;
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010058};
59
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000060} // namespace pipe
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010061
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000062} // namespace arm