SuperluminalProfilerEventForwarder.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <SuperluminalProfilerEventForwarder.h>
  9. #include <AzCore/Interface/Interface.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <Superluminal/PerformanceAPI.h>
  12. namespace SuperluminalProfiler
  13. {
  14. void SuperluminalProfilerEventForwarder::Init()
  15. {
  16. AZ::Interface<AZ::Debug::Profiler>::Register(this);
  17. m_initialized = true;
  18. }
  19. void SuperluminalProfilerEventForwarder::Shutdown()
  20. {
  21. if (!m_initialized)
  22. {
  23. return;
  24. }
  25. // When this call is made, no more thread profiling calls can be performed anymore
  26. AZ::Interface<AZ::Debug::Profiler>::Unregister(this);
  27. // Wait for the remaining threads that might still be processing its profiling calls
  28. AZStd::unique_lock<AZStd::shared_mutex> shutdownLock(m_shutdownMutex);
  29. }
  30. void SuperluminalProfilerEventForwarder::BeginRegion(const AZ::Debug::Budget* budget, const char* eventName, ...)
  31. {
  32. // Try to lock here, the shutdownMutex will only be contested when the CpuProfiler is shutting down.
  33. if (m_shutdownMutex.try_lock_shared())
  34. {
  35. PerformanceAPI_BeginEvent(eventName, budget->Name(), budget->Crc());
  36. m_shutdownMutex.unlock_shared();
  37. }
  38. }
  39. void SuperluminalProfilerEventForwarder::EndRegion([[maybe_unused]] const AZ::Debug::Budget* budget)
  40. {
  41. // Try to lock here, the shutdownMutex will only be contested when the CpuProfiler is shutting down.
  42. if (m_shutdownMutex.try_lock_shared())
  43. {
  44. PerformanceAPI_EndEvent();
  45. m_shutdownMutex.unlock_shared();
  46. }
  47. }
  48. } // namespace SuperluminalProfiler