[atom_cpu_profiler_gem_promotion] migrated CPU frame data capture API and implementation from Atom over to Profiler gem

Signed-off-by: AMZN-ScottR <24445312+AMZN-ScottR@users.noreply.github.com>
This commit is contained in:
AMZN-ScottR
2021-10-15 17:40:50 -07:00
parent 66f905870d
commit b89e437a6b
6 changed files with 234 additions and 76 deletions
@@ -8,14 +8,113 @@
#include <ProfilerSystemComponent.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/EditContextConstants.inl>
#include <AzCore/Serialization/Json/JsonSerializationSettings.h>
#include <AzCore/Serialization/Json/JsonUtils.h>
#include <AzCore/Serialization/SerializeContext.h>
namespace Profiler
{
static constexpr AZ::Crc32 profilerServiceCrc = AZ_CRC_CE("ProfilerService");
struct DeplayedFunction
{
using func_type = AZStd::function<void()>;
DeplayedFunction(int framesToDelay, func_type&& function)
: m_function(AZStd::move(function))
, m_framesLeft(framesToDelay)
{
}
void Run()
{
if (--m_framesLeft <= 0)
{
m_function();
}
else
{
AZ::SystemTickBus::QueueFunction(
[](DeplayedFunction&& delayedFunc)
{
delayedFunc.Run();
},
AZStd::move(*this)
);
}
}
func_type m_function;
int m_framesLeft{ 0 };
};
class ProfilerNotificationBusHandler final
: public ProfilerNotificationBus::Handler
, public AZ::BehaviorEBusHandler
{
public:
AZ_EBUS_BEHAVIOR_BINDER(ProfilerNotificationBusHandler, "{44161459-B816-4876-95A4-BA16DEC767D6}", AZ::SystemAllocator,
OnCaptureCpuProfilingStatisticsFinished
);
void OnCaptureCpuProfilingStatisticsFinished(bool result, const AZStd::string& info) override
{
Call(FN_OnCaptureCpuProfilingStatisticsFinished, result, info);
}
static void Reflect(AZ::ReflectContext* context)
{
if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
{
behaviorContext->EBus<ProfilerNotificationBus>("ProfilerNotificationBus")
->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
->Attribute(AZ::Script::Attributes::Module, "profiler")
->Handler<ProfilerNotificationBusHandler>();
}
}
};
bool SerializeCpuProfilingData(const AZStd::ring_buffer<CpuProfiler::TimeRegionMap>& data, AZStd::string outputFilePath, bool wasEnabled)
{
AZ_TracePrintf("ProfilerSystemComponent", "Beginning serialization of %zu frames of profiling data\n", data.size());
AZ::JsonSerializerSettings serializationSettings;
serializationSettings.m_keepDefaults = true;
CpuProfilingStatisticsSerializer serializer(data);
const auto saveResult = AZ::JsonSerializationUtils::SaveObjectToFile(&serializer,
outputFilePath, (CpuProfilingStatisticsSerializer*)nullptr, &serializationSettings);
AZStd::string captureInfo = outputFilePath;
if (!saveResult.IsSuccess())
{
captureInfo = AZStd::string::format("Failed to save Cpu Profiling Statistics data to file '%s'. Error: %s",
outputFilePath.c_str(),
saveResult.GetError().c_str());
AZ_Warning("ProfilerSystemComponent", false, captureInfo.c_str());
}
else
{
AZ_Printf("ProfilerSystemComponent", "Cpu profiling statistics was saved to file [%s]\n", outputFilePath.c_str());
}
// Disable the profiler again
if (!wasEnabled)
{
CpuProfiler::Get()->SetProfilerEnabled(false);
}
// Notify listeners that the pass' PipelineStatistics queries capture has finished.
ProfilerNotificationBus::Broadcast(&ProfilerNotificationBus::Events::OnCaptureCpuProfilingStatisticsFinished,
saveResult.IsSuccess(),
captureInfo);
return saveResult.IsSuccess();
}
void ProfilerSystemComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
@@ -29,9 +128,21 @@ namespace Profiler
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
->Attribute(AZ::Edit::Attributes::AutoExpand, true);
ProfilerNotificationBusHandler::Reflect(context);
}
}
if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
{
behaviorContext->EBus<ProfilerRequestBus>("ProfilerRequestBus")
->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
->Attribute(AZ::Script::Attributes::Module, "profiler")
->Event("CaptureCpuProfilingStatistics", &ProfilerRequestBus::Events::CaptureCpuProfilingStatistics);
ProfilerNotificationBusHandler::Reflect(context);
}
CpuProfilingStatisticsSerializer::Reflect(context);
}
@@ -69,10 +180,6 @@ namespace Profiler
}
}
void ProfilerSystemComponent::Init()
{
}
void ProfilerSystemComponent::Activate()
{
ProfilerRequestBus::Handler::BusConnect();
@@ -85,5 +192,89 @@ namespace Profiler
m_cpuProfiler.Shutdown();
ProfilerRequestBus::Handler::BusDisconnect();
// Block deactivation until the IO thread has finished serializing the CPU data
if (m_cpuDataSerializationThread.joinable())
{
m_cpuDataSerializationThread.join();
}
}
bool ProfilerSystemComponent::CaptureCpuProfilingStatistics(const AZStd::string& outputFilePath)
{
bool expected = false;
if (!m_cpuCaptureInProgress.compare_exchange_strong(expected, true))
{
return false;
}
// Start the cpu profiling
bool wasEnabled = m_cpuProfiler.IsProfilerEnabled();
if (!wasEnabled)
{
m_cpuProfiler.SetProfilerEnabled(true);
}
const int frameDelay = 5; // arbitrary number
DeplayedFunction delayedFunc(frameDelay,
[this, outputFilePath, wasEnabled]()
{
// Blocking call for a single frame of data, avoid thread overhead
AZStd::ring_buffer<CpuProfiler::TimeRegionMap> singleFrameData(1);
singleFrameData.push_back(m_cpuProfiler.GetTimeRegionMap());
SerializeCpuProfilingData(singleFrameData, outputFilePath, wasEnabled);
m_cpuCaptureInProgress.store(false);
}
);
delayedFunc.Run();
return true;
}
bool ProfilerSystemComponent::BeginContinuousCpuProfilingCapture()
{
return m_cpuProfiler.BeginContinuousCapture();
}
bool ProfilerSystemComponent::EndContinuousCpuProfilingCapture(const AZStd::string& outputFilePath)
{
bool expected = false;
if (!m_cpuDataSerializationInProgress.compare_exchange_strong(expected, true))
{
AZ_TracePrintf(
"ProfilerSystemComponent",
"Cannot end a continuous capture - another serialization is currently in progress\n");
return false;
}
AZStd::ring_buffer<CpuProfiler::TimeRegionMap> captureResult;
const bool captureEnded = m_cpuProfiler.EndContinuousCapture(captureResult);
if (!captureEnded)
{
AZ_TracePrintf("ProfilerSystemComponent", "Could not end the continuous capture, is one in progress?\n");
m_cpuDataSerializationInProgress.store(false);
return false;
}
// cpuProfilingData could be 1GB+ once saved, so use an IO thread to write it to disk.
auto threadIoFunction =
[data = AZStd::move(captureResult), filePath = AZStd::string(outputFilePath), &flag = m_cpuDataSerializationInProgress]()
{
SerializeCpuProfilingData(data, filePath, true);
flag.store(false);
};
// If the thread object already exists (ex. we have already serialized data), join. This will not block since
// m_cpuDataSerializationInProgress was false, meaning the IO thread has already completed execution.
// TODO Use a reusable thread implementation over repeated creation + destruction of threads [ATOM-16214]
if (m_cpuDataSerializationThread.joinable())
{
m_cpuDataSerializationThread.join();
}
auto thread = AZStd::thread(threadIoFunction);
m_cpuDataSerializationThread = AZStd::move(thread);
return true;
}
} // namespace Profiler