Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add superluminal, Tracy and Optick profilers integration as Gems #834

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Gems/ExternalProfilers/SuperluminalProfiler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Copyright (c) Contributors to the Open 3D Engine Project.
# For complete copyright and license terms please see the LICENSE at the root of this distribution.
#
# SPDX-License-Identifier: Apache-2.0 OR MIT
#
#

o3de_gem_setup("SuperluminalProfiler")

add_subdirectory(Code)
60 changes: 60 additions & 0 deletions Gems/ExternalProfilers/SuperluminalProfiler/Code/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#
# Copyright (c) Contributors to the Open 3D Engine Project.
# For complete copyright and license terms please see the LICENSE at the root of this distribution.
#
# SPDX-License-Identifier: Apache-2.0 OR MIT
#
#

set(SUPERLUMINAL_API_PATH "C:/Program Files/Superluminal/Performance/API" CACHE PATH "Path to the Superluminal 3rd party profiler API folder.")
list(APPEND CMAKE_MODULE_PATH ${SUPERLUMINAL_API_PATH})

find_package(SuperluminalAPI REQUIRED)

ly_add_target(
NAME ${gem_name}.Static STATIC
NAMESPACE Gem
FILES_CMAKE
superluminalprofiler_files.cmake
INCLUDE_DIRECTORIES
PUBLIC
Include
PRIVATE
Source
${SuperluminalAPI_INCLUDE_DIRS}
BUILD_DEPENDENCIES
PUBLIC
AZ::AzCore
AZ::AzFramework
PRIVATE
${SuperluminalAPI_LIBS_RELEASE}
)

ly_add_target(
NAME ${gem_name} ${PAL_TRAIT_MONOLITHIC_DRIVEN_MODULE_TYPE}
NAMESPACE Gem
FILES_CMAKE
superluminalprofiler_shared_files.cmake
INCLUDE_DIRECTORIES
PUBLIC
Include
PRIVATE
Source
BUILD_DEPENDENCIES
PRIVATE
Gem::${gem_name}.Static
)

ly_add_source_properties(
SOURCES
Source/ProfilerModule.cpp
PROPERTY COMPILE_DEFINITIONS
VALUES
O3DE_GEM_NAME=${gem_name}
O3DE_GEM_VERSION=${gem_version})

ly_create_alias(NAME ${gem_name}.Servers NAMESPACE Gem TARGETS Gem::${gem_name})
ly_create_alias(NAME ${gem_name}.Builders NAMESPACE Gem TARGETS Gem::${gem_name})
ly_create_alias(NAME ${gem_name}.Clients NAMESPACE Gem TARGETS Gem::${gem_name})
ly_create_alias(NAME ${gem_name}.Unified NAMESPACE Gem TARGETS Gem::${gem_name})
ly_create_alias(NAME ${gem_name}.Tools NAMESPACE Gem TARGETS Gem::${gem_name})
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/

#include <CpuProfiler.h>

#include <AzCore/Interface/Interface.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <Superluminal/PerformanceAPI.h>

namespace SuperluminalProfiler
{
void CpuProfiler::Init()
{
AZ::Interface<AZ::Debug::Profiler>::Register(this);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that only one profiler can be active at a time, I might want to assert there, but ideally I should use a mechanism to say that when this gem is enabled none of the other profiling gem can be enabled, I don't know if we have something like that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is, kinda-of. There is a field in gems.json called provided_unique_service that you can set to any string, and the scripts to enable gems should check against this. We did this for PhysX4 vs PhysX5

https://github.com/o3de/o3de/blob/f29613a5c80d8dbc53016bdaaf3849cd6d77f60f/Gems/PhysX/Core/PhysX4/gem.json#L26

https://github.com/o3de/o3de/blob/f29613a5c80d8dbc53016bdaaf3849cd6d77f60f/Gems/PhysX/Core/PhysX5/gem.json#L26

m_initialized = true;
AZ::SystemTickBus::Handler::BusConnect();
}

void CpuProfiler::Shutdown()
{
if (!m_initialized)
{
return;
}

// When this call is made, no more thread profiling calls can be performed anymore
AZ::Interface<AZ::Debug::Profiler>::Unregister(this);

// Wait for the remaining threads that might still be processing its profiling calls
AZStd::unique_lock<AZStd::shared_mutex> shutdownLock(m_shutdownMutex);
AZ::SystemTickBus::Handler::BusDisconnect();
}

void CpuProfiler::BeginRegion(const AZ::Debug::Budget* budget, const char* eventName, ...)
{
// Try to lock here, the shutdownMutex will only be contested when the CpuProfiler is shutting down.
if (m_shutdownMutex.try_lock_shared())
{
PerformanceAPI_BeginEvent(eventName, budget->Name(), budget->Crc());
m_shutdownMutex.unlock_shared();
}
}

void CpuProfiler::EndRegion([[maybe_unused]] const AZ::Debug::Budget* budget)
{
// Try to lock here, the shutdownMutex will only be contested when the CpuProfiler is shutting down.
if (m_shutdownMutex.try_lock_shared())
{
PerformanceAPI_EndEvent();
m_shutdownMutex.unlock_shared();
}
}

void CpuProfiler::OnSystemTick()
{
PerformanceAPI::InstrumentationScope("SystemTick", nullptr, PERFORMANCEAPI_MAKE_COLOR(0, 255, 0));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/

#pragma once

#include <AzCore/Component/TickBus.h>
#include <AzCore/Debug/Profiler.h>
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/Name/Name.h>
#include <AzCore/RTTI/RTTI.h>
#include <AzCore/std/parallel/mutex.h>
#include <AzCore/std/parallel/shared_mutex.h>

namespace SuperluminalProfiler
{
class CpuProfiler final
: public AZ::Debug::Profiler
, public AZ::SystemTickBus::Handler
{
public:
AZ_RTTI(CpuProfiler, "{A05E7DC4-AB00-41BC-A739-8E58908CB84F}", AZ::Debug::Profiler);
AZ_CLASS_ALLOCATOR(CpuProfiler, AZ::SystemAllocator);

CpuProfiler() = default;
~CpuProfiler() = default;

//! Registers/un-registers the AZ::Debug::Profiler instance to the interface
void Init();
void Shutdown();

//! AZ::Debug::Profiler overrides...
void BeginRegion(const AZ::Debug::Budget* budget, const char* eventName, ...) final override;
void EndRegion(const AZ::Debug::Budget* budget) final override;

//! Check to see if a programmatic capture is currently in progress, implies
//! that the profiler is active if returns True.
bool IsContinuousCaptureInProgress() const;

//! AZ::SystemTickBus::Handler overrides
//! When fired, the profiler collects all profiling data from registered threads and updates
//! m_timeRegionMap so that the next frame has up-to-date profiling data.
void OnSystemTick() final override;

private:
// This lock will only be contested when the CpuProfiler's Shutdown() method has been called
AZStd::shared_mutex m_shutdownMutex;

bool m_initialized = false;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/

#include <ProfilerSystemComponent.h>

#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/Module/Module.h>

namespace SuperluminalProfiler
{
class ProfilerModule
: public AZ::Module
{
public:
AZ_RTTI(ProfilerModule, "{7C666AFB-E699-42FB-8F32-DAEE5A62CD01}", AZ::Module);
AZ_CLASS_ALLOCATOR(ProfilerModule, AZ::SystemAllocator);

ProfilerModule()
{
// Push results of [MyComponent]::CreateDescriptor() into m_descriptors here.
// Add ALL components descriptors associated with this gem to m_descriptors.
// This will associate the AzTypeInfo information for the components with the the SerializeContext, BehaviorContext and EditContext.
// This happens through the [MyComponent]::Reflect() function.
m_descriptors.insert(m_descriptors.end(), {
ProfilerSystemComponent::CreateDescriptor(),
});
}

/**
* Add required SystemComponents to the SystemEntity.
*/
AZ::ComponentTypeList GetRequiredSystemComponents() const override
{
return AZ::ComponentTypeList{
azrtti_typeid<ProfilerSystemComponent>(),
};
}
};
}

#if defined(O3DE_GEM_NAME)
AZ_DECLARE_MODULE_CLASS(AZ_JOIN(Gem_, O3DE_GEM_NAME), SuperluminalProfiler::ProfilerModule)
#else
AZ_DECLARE_MODULE_CLASS(Gem_Profiler, SuperluminalProfiler::ProfilerModule)
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/

#include "ProfilerSystemComponent.h"

#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/EditContextConstants.inl>
#include <AzCore/Serialization/SerializeContext.h>

namespace SuperluminalProfiler
{
static constexpr AZ::Crc32 profilerServiceCrc = AZ_CRC_CE("ProfilerService");

void ProfilerSystemComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
{
serialize->Class<ProfilerSystemComponent, AZ::Component>()
->Version(0);
}
}

void ProfilerSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(profilerServiceCrc);
}

void ProfilerSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
{
incompatible.push_back(profilerServiceCrc);
}

void ProfilerSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
{
}

void ProfilerSystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
{
}

ProfilerSystemComponent::ProfilerSystemComponent()
{
if (AZ::Debug::ProfilerSystemInterface::Get() == nullptr)
{
AZ::Debug::ProfilerSystemInterface::Register(this);
}
}

ProfilerSystemComponent::~ProfilerSystemComponent()
{
if (AZ::Debug::ProfilerSystemInterface::Get() == this)
{
AZ::Debug::ProfilerSystemInterface::Unregister(this);
}
}

void ProfilerSystemComponent::Activate()
{
m_cpuProfiler.Init();
}

void ProfilerSystemComponent::Deactivate()
{
m_cpuProfiler.Shutdown();
}

bool ProfilerSystemComponent::IsActive() const
{
return false;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While superluminal API does not provide anything to trigger a record from the editor, we don't want to leave this unimplemeted either as we can only have one active profiler at a time (with the exception of Pix that is doing its hook directly in profiler.h)

}

void ProfilerSystemComponent::SetActive([[maybe_unused]] bool enabled)
{

}

bool ProfilerSystemComponent::CaptureFrame([[maybe_unused]] const AZStd::string& outputFilePath)
{
return true;
}

bool ProfilerSystemComponent::StartCapture([[maybe_unused]] AZStd::string outputFilePath)
{
return true;
}

bool ProfilerSystemComponent::EndCapture()
{
return true;
}

bool ProfilerSystemComponent::IsCaptureInProgress() const
{
return false;
}

}
Loading