diff --git a/Windows_Libs/Dev/Render/Profiler.h b/Windows_Libs/Dev/Render/Profiler.h new file mode 100644 index 0000000..e78cd52 --- /dev/null +++ b/Windows_Libs/Dev/Render/Profiler.h @@ -0,0 +1,14 @@ +#pragma once + +// enable only if you know what you're doing +// requires linking Minecraft.Client against Ws2_32.lib +// stats are available at http://127.0.0.1:1338/ + +//#define ENABLE_PROFILING + +#ifdef ENABLE_PROFILING +#include "microprofile/microprofile.h" +#define PROFILER_SCOPE(group, name, color) MICROPROFILE_SCOPEI(group, name, color); +#else +#define PROFILER_SCOPE(group, name, color) ((void)0); +#endif \ No newline at end of file diff --git a/Windows_Libs/Dev/Render/Render.vcxproj b/Windows_Libs/Dev/Render/Render.vcxproj index d6a3346..e7af720 100644 --- a/Windows_Libs/Dev/Render/Render.vcxproj +++ b/Windows_Libs/Dev/Render/Render.vcxproj @@ -123,7 +123,7 @@ true stdafx.h MultiThreadedDebug - libpng\;zlib\;%(AdditionalIncludeDirectories) + libpng\;zlib\;microprofile\;%(AdditionalIncludeDirectories) Default true @@ -148,7 +148,7 @@ true stdafx.h MultiThreaded - libpng\;zlib\;%(AdditionalIncludeDirectories) + libpng\;zlib\;microprofile\;%(AdditionalIncludeDirectories) @@ -167,6 +167,10 @@ + + + + @@ -329,6 +333,7 @@ + diff --git a/Windows_Libs/Dev/Render/Render.vcxproj.filters b/Windows_Libs/Dev/Render/Render.vcxproj.filters index 8107131..a54788a 100644 --- a/Windows_Libs/Dev/Render/Render.vcxproj.filters +++ b/Windows_Libs/Dev/Render/Render.vcxproj.filters @@ -18,6 +18,12 @@ {0629c87a-576d-4163-8c60-dad190ee97d0} + + {8be558c7-c6e6-4ef7-bfcb-83d29f079240} + + + {6cbd8b95-ff51-42d2-a66f-282d3a11c042} + @@ -92,6 +98,18 @@ Header Files\libpng + + Header Files\microprofile + + + Header Files\microprofile + + + Header Files\microprofile + + + Header Files + @@ -166,5 +184,8 @@ Source Files\libpng + + Source Files\microprofile + \ No newline at end of file diff --git a/Windows_Libs/Dev/Render/Renderer.h b/Windows_Libs/Dev/Render/Renderer.h index ba1859e..24a4fc2 100644 --- a/Windows_Libs/Dev/Render/Renderer.h +++ b/Windows_Libs/Dev/Render/Renderer.h @@ -1,5 +1,6 @@ #pragma once #include "4J_Render.h" +#include "Profiler.h" #include #include #include diff --git a/Windows_Libs/Dev/Render/RendererCBuff.cpp b/Windows_Libs/Dev/Render/RendererCBuff.cpp index 120338b..b1fc591 100644 --- a/Windows_Libs/Dev/Render/RendererCBuff.cpp +++ b/Windows_Libs/Dev/Render/RendererCBuff.cpp @@ -71,6 +71,8 @@ void Renderer::CommandBuffer::AddMatrix(const float *matrix) void Renderer::CommandBuffer::AddVertices(unsigned int stride, unsigned int count, void *dataIn, Renderer::Context &c) { + PROFILER_SCOPE("Renderer::CommandBuffer::AddVertices", "AddVertices", MP_ORANGE) + if (c.matrixDirty[MATRIX_MODE_MODELVIEW_CBUFF]) { AddMatrix(InternalRenderManager.MatrixGet(MATRIX_MODE_MODELVIEW_CBUFF)); @@ -243,6 +245,8 @@ void Renderer::CommandBuffer::SetFaceCull(bool enable) void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Context &c, int primitiveType) { + PROFILER_SCOPE("Renderer::CommandBuffer::Render", "Render", MP_ORANGE) + if (!m_vertexBuffer) return; @@ -252,6 +256,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con for (const Command &command : m_commands) { + PROFILER_SCOPE("Renderer::CommandBuffer::Render", "ProcessCommand", MP_ORANGE) switch (command.m_command_type) { diff --git a/Windows_Libs/Dev/Render/RendererCore.cpp b/Windows_Libs/Dev/Render/RendererCore.cpp index 426ab7f..1cd8139 100644 --- a/Windows_Libs/Dev/Render/RendererCore.cpp +++ b/Windows_Libs/Dev/Render/RendererCore.cpp @@ -226,12 +226,15 @@ void Renderer::CaptureScreen(ImageFileBuffer *, XSOCIAL_PREVIEWIMAGE *) {} void Renderer::Clear(int flags, D3D11_RECT *) { + PROFILER_SCOPE("Renderer::Clear", "Clear", MP_MAGENTA) + Renderer::Context &c = getContext(); ID3D11BlendState *blendState = NULL; ID3D11DepthStencilState *depthState = NULL; ID3D11RasterizerState *rasterizerState = NULL; + PROFILER_SCOPE("Renderer::Clear", "Blend", MP_MAGENTA) D3D11_BLEND_DESC blendDesc = {}; blendDesc.AlphaToCoverageEnable = false; blendDesc.IndependentBlendEnable = false; @@ -245,6 +248,7 @@ void Renderer::Clear(int flags, D3D11_RECT *) blendDesc.RenderTarget[0].RenderTargetWriteMask = (flags & CLEAR_COLOUR_FLAG) ? D3D11_COLOR_WRITE_ENABLE_ALL : 0; m_pDevice->CreateBlendState(&blendDesc, &blendState); + PROFILER_SCOPE("Renderer::Clear", "Depth", MP_MAGENTA) D3D11_DEPTH_STENCIL_DESC depthDesc = {}; depthDesc.DepthEnable = (flags & CLEAR_DEPTH_FLAG) ? true : false; depthDesc.DepthWriteMask = depthDesc.DepthEnable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; @@ -262,6 +266,7 @@ void Renderer::Clear(int flags, D3D11_RECT *) depthDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; m_pDevice->CreateDepthStencilState(&depthDesc, &depthState); + PROFILER_SCOPE("Renderer::Clear", "Rasterizer", MP_MAGENTA) D3D11_RASTERIZER_DESC rasterDesc = {}; rasterDesc.FillMode = D3D11_FILL_SOLID; rasterDesc.CullMode = D3D11_CULL_NONE; @@ -269,6 +274,7 @@ void Renderer::Clear(int flags, D3D11_RECT *) rasterDesc.MultisampleEnable = true; m_pDevice->CreateRasterizerState(&rasterDesc, &rasterizerState); + PROFILER_SCOPE("Renderer::Clear", "DrawClearQuad", MP_MAGENTA) c.m_pDeviceContext->VSSetShader(screenClearVertexShader, NULL, 0); c.m_pDeviceContext->IASetInputLayout(NULL); c.m_pDeviceContext->PSSetShader(screenClearPixelShader, NULL, 0); @@ -362,6 +368,11 @@ void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain) m_pDeviceContext = InitialiseContext(true); m_pSwapChain = pSwapChain; + #ifdef ENABLE_PROFILING + MicroProfileOnThreadCreate("MainRenderThread"); + MicroProfileSetEnableAllGroups(true); + #endif + m_commandHandleToIndex = new int16_t[NUM_COMMAND_HANDLES]; m_commandBuffers = new CommandBuffer *[MAX_COMMAND_BUFFERS]; m_commandMatrices = new DirectX::XMMATRIX[MAX_COMMAND_BUFFERS]; @@ -554,8 +565,12 @@ bool Renderer::IsWidescreen() void Renderer::Present() { + PROFILER_SCOPE("Renderer::Present", "Present", MP_MAGENTA) + if (m_bShouldScreenGrabNextFrame) { + PROFILER_SCOPE("Renderer::Present", "ScreenGrab", MP_MAGENTA) + unsigned char *linearData = new unsigned char[kScreenGrabWidth * kScreenGrabHeight * 4]; ID3D11Texture2D *backBuffer = NULL; @@ -575,6 +590,7 @@ void Renderer::Present() if (stagingTexture && backBuffer) { + PROFILER_SCOPE("Renderer::Present", "CopyResource", MP_MAGENTA) m_pDeviceContext->CopyResource(stagingTexture, backBuffer); D3D11_MAPPED_SUBRESOURCE mapped = {}; @@ -692,6 +708,8 @@ void Renderer::SetupShaders() void Renderer::StartFrame() { + PROFILER_SCOPE("Renderer::StartFrame", "StartFrame", MP_MAGENTA) + Renderer::Context &c = getContext(); activeVertexType = -1; @@ -700,6 +718,8 @@ void Renderer::StartFrame() TextureBindVertex(-1); TextureBind(-1); + PROFILER_SCOPE("Renderer::StartFrame", "State", MP_MAGENTA) + StateSetColour(1.0f, 1.0f, 1.0f, 1.0f); StateSetDepthMask(true); StateSetBlendEnable(true); @@ -725,6 +745,10 @@ void Renderer::StartFrame() viewport.MaxDepth = 1.0f; c.m_pDeviceContext->RSSetViewports(1, &viewport); c.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView); + + #ifdef ENABLE_PROFILING + MicroProfileFlip(nullptr); + #endif } void Renderer::Suspend() diff --git a/Windows_Libs/Dev/Render/RendererState.cpp b/Windows_Libs/Dev/Render/RendererState.cpp index 0953cc0..3cef1a0 100644 --- a/Windows_Libs/Dev/Render/RendererState.cpp +++ b/Windows_Libs/Dev/Render/RendererState.cpp @@ -6,6 +6,7 @@ ID3D11BlendState *Renderer::GetManagedBlendState() { + PROFILER_SCOPE("Renderer::GetManagedBlendState", "GetManagedBlendState", MP_ORCHID1) Context &c = getContext(); const D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = c.blendDesc.RenderTarget[0]; @@ -24,6 +25,7 @@ ID3D11BlendState *Renderer::GetManagedBlendState() ID3D11DepthStencilState *Renderer::GetManagedDepthStencilState() { + PROFILER_SCOPE("Renderer::GetManagedBlendState", "GetManagedDepthStencilState", MP_ORCHID1) Context &c = getContext(); const int key = (c.depthStencilDesc.DepthEnable ? 2 : 0) | ((static_cast(c.depthStencilDesc.DepthFunc) & 0x0F) << 2) | @@ -41,6 +43,7 @@ ID3D11DepthStencilState *Renderer::GetManagedDepthStencilState() ID3D11RasterizerState *Renderer::GetManagedRasterizerState() { + PROFILER_SCOPE("Renderer::GetManagedRasterizerState", "GetManagedRasterizerState", MP_ORCHID1) Context &c = getContext(); const int key = (static_cast(c.rasterizerDesc.DepthBias)) | @@ -59,6 +62,7 @@ ID3D11RasterizerState *Renderer::GetManagedRasterizerState() ID3D11SamplerState *Renderer::GetManagedSamplerState() { + PROFILER_SCOPE("Renderer::GetManagedSamplerState", "GetManagedSamplerState", MP_ORCHID1) Context &c = getContext(); const int key = m_textures[c.textureIdx].samplerParams; @@ -376,6 +380,7 @@ void Renderer::StateSetDepthSlopeAndBias(float slope, float bias) void Renderer::UpdateFogState() { + PROFILER_SCOPE("Renderer::UpdateFogState", "UpdateFogState", MP_ORCHID1) Context &c = getContext(); ID3D11DeviceContext *d3d11 = c.m_pDeviceContext; @@ -420,6 +425,7 @@ void Renderer::StateSetVertexTextureUV(float u, float v) void Renderer::UpdateTexGenState() { + PROFILER_SCOPE("Renderer::UpdateTexGenState", "UpdateTexGenState", MP_ORCHID1) Context &c = getContext(); D3D11_MAPPED_SUBRESOURCE mapped = {}; @@ -430,6 +436,7 @@ void Renderer::UpdateTexGenState() void Renderer::UpdateLightingState() { + PROFILER_SCOPE("Renderer::UpdateLightingState", "UpdateLightingState", MP_ORCHID1) Context &c = getContext(); if (!c.lightingDirty || !c.lightingEnabled) { @@ -608,6 +615,7 @@ void Renderer::StateSetForceLOD(int LOD) void Renderer::StateUpdate() { + PROFILER_SCOPE("Renderer::StateUpdate", "StateUpdate", MP_ORCHID1) Context &c = getContext(); StateSetFaceCull(c.faceCullEnabled); StateSetDepthMask(c.depthWriteEnabled); diff --git a/Windows_Libs/Dev/Render/RendererTexture.cpp b/Windows_Libs/Dev/Render/RendererTexture.cpp index 25aa978..f9243d9 100644 --- a/Windows_Libs/Dev/Render/RendererTexture.cpp +++ b/Windows_Libs/Dev/Render/RendererTexture.cpp @@ -37,6 +37,7 @@ void user_flush_data(png_struct_def* png_ptr) int Renderer::TextureCreate() { + PROFILER_SCOPE("Renderer::TextureCreate", "TextureCreate", MP_PURPLE4) for (int i = 0; i < MAX_TEXTURES; i++) { if (!m_textures[i].allocated) @@ -53,6 +54,7 @@ int Renderer::TextureCreate() void Renderer::TextureFree(int idx) { + PROFILER_SCOPE("Renderer::TextureFree", "TextureFree", MP_PURPLE4) if (m_textures[idx].texture) { m_textures[idx].texture->Release(); @@ -68,6 +70,7 @@ void Renderer::TextureFree(int idx) void Renderer::TextureBind(int idx) { + PROFILER_SCOPE("Renderer::TextureBind", "TextureBind", MP_PURPLE4) if (idx == -1) idx = defaultTextureIndex; @@ -84,6 +87,7 @@ void Renderer::TextureBind(int idx) void Renderer::TextureBindVertex(int idx) { + PROFILER_SCOPE("Renderer::TextureBindVertex", "TextureBindVertex", MP_PURPLE4) if (idx == -1) idx = defaultTextureIndex; @@ -109,6 +113,7 @@ int Renderer::TextureGetTextureLevels() void Renderer::TextureData(int width, int height, void* data, int level, C4JRender::eTextureFormat format) { + PROFILER_SCOPE("Renderer::TextureData", "TextureData", MP_PURPLE4) Context& c = getContext(); int idx = c.textureIdx; @@ -145,6 +150,7 @@ void Renderer::TextureData(int width, int height, void* data, int level, C4JRend void Renderer::TextureDataUpdate(int xoffset, int yoffset, int width, int height, void* data, int level) { + PROFILER_SCOPE("Renderer::TextureDataUpdate", "TextureDataUpdate", MP_PURPLE4) Context& c = getContext(); int idx = c.textureIdx; @@ -222,6 +228,7 @@ void Renderer::UpdateTextureState(bool bVertex) HRESULT Renderer::LoadTextureData(const char* szFilename, D3DXIMAGE_INFO* pSrcInfo, int** ppDataOut) { + PROFILER_SCOPE("Renderer::LoadTextureData_File", "LoadTextureData_File", MP_PURPLE4) png_image image; memset(&image, 0, sizeof(image)); image.version = PNG_IMAGE_VERSION; @@ -246,6 +253,7 @@ HRESULT Renderer::LoadTextureData(const char* szFilename, D3DXIMAGE_INFO* pSrcIn HRESULT Renderer::LoadTextureData(BYTE* pbData, DWORD dwBytes, D3DXIMAGE_INFO* pSrcInfo, int** ppDataOut) { + PROFILER_SCOPE("Renderer::LoadTextureData_Memory", "LoadTextureData_Memory", MP_PURPLE4) png_image image; memset(&image, 0, sizeof(image)); image.version = PNG_IMAGE_VERSION; @@ -270,6 +278,7 @@ HRESULT Renderer::LoadTextureData(BYTE* pbData, DWORD dwBytes, D3DXIMAGE_INFO* p HRESULT Renderer::SaveTextureData(const char* szFilename, D3DXIMAGE_INFO* pSrcInfo, int* ppDataOut) { + PROFILER_SCOPE("Renderer::SaveTextureData", "SaveTextureData", MP_PURPLE4) png_image image; memset(&image, 0, sizeof(image)); image.width = pSrcInfo->Width; @@ -283,6 +292,7 @@ HRESULT Renderer::SaveTextureData(const char* szFilename, D3DXIMAGE_INFO* pSrcIn HRESULT Renderer::SaveTextureDataToMemory(void* pOutput, int outputCapacity, int* outputLength, int width, int height, int* ppDataIn) { + PROFILER_SCOPE("Renderer::SaveTextureDataToMemory", "SaveTextureDataToMemory", MP_PURPLE4) png_image image; memset(&image, 0, sizeof(image)); image.width = width; diff --git a/Windows_Libs/Dev/Render/RendererVertex.cpp b/Windows_Libs/Dev/Render/RendererVertex.cpp index 06966a0..ff806ec 100644 --- a/Windows_Libs/Dev/Render/RendererVertex.cpp +++ b/Windows_Libs/Dev/Render/RendererVertex.cpp @@ -4,11 +4,14 @@ void Renderer::DrawVertexBuffer(C4JRender::ePrimitiveType PrimitiveType, int count, ID3D11Buffer *buffer, C4JRender::eVertexType vType, C4JRender::ePixelShaderType psType) { + PROFILER_SCOPE("Renderer::DrawVertexBuffer", "DrawVertexBuffer", MP_RED2) Renderer::Context &c = getContext(); ID3D11DeviceContext *d3d11 = c.m_pDeviceContext; int drawCount = count; bool indexed = false; + + PROFILER_SCOPE("Renderer::DrawVertexBuffer", "DrawVertexSetup", MP_RED2) DrawVertexSetup(vType, psType, PrimitiveType, &drawCount, &indexed); StateUpdate(); @@ -25,6 +28,7 @@ void Renderer::DrawVertexBuffer(C4JRender::ePrimitiveType PrimitiveType, int cou void Renderer::DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelShaderType psType, C4JRender::ePrimitiveType PrimitiveType, int *count, bool *indexed) { + PROFILER_SCOPE("Renderer::DrawVertexSetup", "DrawVertexSetup", MP_RED2) Renderer::Context &c = getContext(); ID3D11DeviceContext *d3d11 = c.m_pDeviceContext; @@ -101,6 +105,7 @@ void Renderer::DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelSh void Renderer::DrawVertices(C4JRender::ePrimitiveType PrimitiveType, int count, void *vertices, C4JRender::eVertexType vType, C4JRender::ePixelShaderType psType) { + PROFILER_SCOPE("Renderer::DrawVertices", "DrawVertices", MP_RED2) Renderer::Context &c = getContext(); ID3D11DeviceContext *d3d11 = c.m_pDeviceContext; Renderer::CommandBuffer *commandBuffer = c.commandBuffer; @@ -120,6 +125,8 @@ void Renderer::DrawVertices(C4JRender::ePrimitiveType PrimitiveType, int count, int drawCount = count; bool indexed = false; + + PROFILER_SCOPE("Renderer::DrawVertices", "DrawVertexSetup", MP_RED2) DrawVertexSetup(vType, psType, PrimitiveType, &drawCount, &indexed); const UINT stride = vertexStrideTable[vType]; diff --git a/Windows_Libs/Dev/Render/microprofile/microprofile.config.h b/Windows_Libs/Dev/Render/microprofile/microprofile.config.h new file mode 100644 index 0000000..7c66cfd --- /dev/null +++ b/Windows_Libs/Dev/Render/microprofile/microprofile.config.h @@ -0,0 +1,4 @@ + +#define MICROPROFILE_ENABLED 1 +#define MICROPROFILE_GPU_TIMERS_D3D11 1 +#define MICROPROFILE_GPU_TIMERS_D3D12 1 diff --git a/Windows_Libs/Dev/Render/microprofile/microprofile.cpp b/Windows_Libs/Dev/Render/microprofile/microprofile.cpp new file mode 100644 index 0000000..c4af767 --- /dev/null +++ b/Windows_Libs/Dev/Render/microprofile/microprofile.cpp @@ -0,0 +1,16191 @@ +#include "../Profiler.h" + +#ifdef ENABLE_PROFILING + +#define MICROPROFILE_IMPL +#include "microprofile.h" +#if MICROPROFILE_ENABLED + +#define BREAK_SKIP() __builtin_trap() + +#ifdef _WIN32 +#if !defined(WIN32_LEAN_AND_MEAN) +#define WIN32_LEAN_AND_MEAN +#endif +#include +#endif + +#ifdef _WIN32 +#define MICROPROFILE_MAX_PATH MAX_PATH +#else +#define MICROPROFILE_MAX_PATH 1024 +#endif + +#include +#include +#include +#include +#include +#include +#include + +#if defined(MICROPROFILE_SYSTEM_STB) +#include +#else +#define STB_SPRINTF_IMPLEMENTATION +#include "stb/stb_sprintf.h" +#endif + +#if defined(_WIN32) && _MSC_VER == 1700 +#define PRIx64 "llx" +#define PRIu64 "llu" +#define PRId64 "lld" +#else +#include +#endif + +#define MICROPROFILE_MAX_COUNTERS 512 +#define MICROPROFILE_MAX_COUNTER_NAME_CHARS (MICROPROFILE_MAX_COUNTERS * 16) +#define MICROPROFILE_MAX_GROUP_INTS (MICROPROFILE_MAX_GROUPS / 32) +#define MICROPROFILE_MAX_CATEGORIES 16 +#define MICROPROFILE_MAX_GRAPHS 5 +#define MICROPROFILE_GRAPH_HISTORY 128 +#define MICROPROFILE_BUFFER_SIZE ((MICROPROFILE_PER_THREAD_BUFFER_SIZE) / sizeof(MicroProfileLogEntry)) +#define MICROPROFILE_GPU_BUFFER_SIZE ((MICROPROFILE_PER_THREAD_GPU_BUFFER_SIZE) / sizeof(MicroProfileLogEntry)) +#define MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS 256 +#define MICROPROFILE_WEBSOCKET_BUFFER_SIZE (64 << 10) +#define MICROPROFILE_INVALID_TICK ((uint64_t) - 1) +#define MICROPROFILE_DROPPED_TICK ((uint64_t) - 2) +#define MICROPROFILE_INVALID_FRAME ((uint32_t) - 1) +#define MICROPROFILE_GROUP_MASK_ALL 0xffffffff +#define MICROPROFILE_MAX_PATCH_ERRORS 32 +#define MICROPROFILE_MAX_MODULE_EXEC_REGIONS 16 + +#define MP_LOG_TICK_MASK 0x0000ffffffffffff +#define MP_LOG_INDEX_MASK 0x3fff000000000000 +#define MP_LOG_BEGIN_MASK 0xc000000000000000 +#define MP_LOG_CSTR_MASK 0xe000000000000000 +#define MP_LOG_CSTR_BIT 0x2000000000000000 +#define MP_LOG_PAYLOAD_PTR_MASK (~(MP_LOG_BEGIN_MASK | MP_LOG_CSTR_BIT)) + +#define MP_LOG_ENTER_LEAVE_MASK 0x8000000000000000 + +#define MP_LOG_LEAVE 0x0 +#define MP_LOG_ENTER 0x1 +#define MP_LOG_EXTENDED 0x2 +#define MP_LOG_EXTENDED_NO_DATA 0x3 + +#ifndef MICROPROFILE_SETTINGS_FILE +#define MICROPROFILE_SETTINGS_FILE "mppresets.cfg" +#endif +#ifndef MICROPROFILE_SETTINGS_FILE_BUILTIN +#define MICROPROFILE_SETTINGS_FILE_BUILTIN "mppresets.builtin.cfg" +#endif +#ifndef MICROPROFILE_SETTINGS_FILE_TEMP +#define MICROPROFILE_SETTINGS_FILE_TEMP ".tmp" +#endif + +// #define MP_LOG_EXTRA_DATA 0x3 + +static_assert(0 == (MICROPROFILE_MAX_GROUPS % 32), "MICROPROFILE_MAX_GROUPS must be divisible by 32"); + +enum EMicroProfileTokenExtended +{ + ETOKEN_GPU_CPU_TIMESTAMP = 0x3fff, + ETOKEN_GPU_CPU_SOURCE_THREAD = 0x3ffe, + ETOKEN_META_MARKER = 0x3ffd, + ETOKEN_CUSTOM_NAME = 0x3ffc, + ETOKEN_CUSTOM_COLOR = 0x3ffb, + ETOKEN_CUSTOM_ID = 0x3ffa, + ETOKEN_CSTR_PTR = 0x2000, // note, matches MP_LOG_CSTR_BIT + ETOKEN_MAX = 0x2000, +}; + +enum +{ + MICROPROFILE_WEBSOCKET_DIRTY_MENU, + MICROPROFILE_WEBSOCKET_DIRTY_ENABLED, +}; + +#ifndef MICROPROFILE_ALLOC // redefine all if overriding +#define MICROPROFILE_ALLOC(nSize, nAlign) MicroProfileAllocAligned(nSize, nAlign); +#define MICROPROFILE_REALLOC(p, s) realloc(p, s) +#define MICROPROFILE_FREE(p) MicroProfileFreeAligned(p) +#define MICROPROFILE_FREE_NON_ALIGNED(p) free(p) +#endif + +#define MP_ALLOC(nSize, nAlign) MicroProfileAllocInternal(nSize, nAlign) +#define MP_REALLOC(p, s) MicroProfileReallocInternal(p, s) +#define MP_FREE(p) MicroProfileFreeInternal(p) +#define MP_ALLOC_OBJECT(T) (T*)MP_ALLOC(sizeof(T), alignof(T)) +#define MP_ALLOC_OBJECT_ARRAY(T, Count) (T*)MP_ALLOC(sizeof(T) * Count, alignof(T)) + +#ifndef MICROPROFILE_DEBUG +#define MICROPROFILE_DEBUG 0 +#endif + +typedef uint64_t MicroProfileLogEntry; + +void MicroProfileSleep(uint32_t nMs); +template +T MicroProfileMin(T a, T b); +template +T MicroProfileMax(T a, T b); +template +T MicroProfileClamp(T a, T min_, T max_); +int64_t MicroProfileMsToTick(float fMs, int64_t nTicksPerSecond); +float MicroProfileTickToMsMultiplier(int64_t nTicksPerSecond); +uint32_t MicroProfileLogGetType(MicroProfileLogEntry Index); +uint64_t MicroProfileLogGetTimerIndex(MicroProfileLogEntry Index); +MicroProfileLogEntry MicroProfileMakeLogIndex(uint64_t nBegin, MicroProfileToken nToken, int64_t nTick); +int64_t MicroProfileLogTickDifference(MicroProfileLogEntry Start, MicroProfileLogEntry End); +int64_t MicroProfileLogSetTick(MicroProfileLogEntry e, int64_t nTick); +uint16_t MicroProfileGetTimerIndex(MicroProfileToken t); +uint32_t MicroProfileGetGroupMask(MicroProfileToken t); +MicroProfileToken MicroProfileMakeToken(uint64_t nGroupMask, uint32_t nGroupIndex, uint16_t nTimer); +bool MicroProfileAnyGroupActive(); +void MicroProfileWriteFile(void* Handle, size_t nSize, const char* pData); + +// defer implementation +#define CONCAT_INTERNAL(x, y) x##y +#define CONCAT(x, y) CONCAT_INTERNAL(x, y) +void IntentionallyNotDefinedFunction__(); // DO NOT DEFINE THIS +template +struct MicroProfileExitScope +{ + T lambda; + MicroProfileExitScope(T lambda) + : lambda(lambda) + { + } + ~MicroProfileExitScope() + { + lambda(); + } + + MicroProfileExitScope(const MicroProfileExitScope& rhs) + : lambda(rhs.lambda) + { + IntentionallyNotDefinedFunction__(); // this is here to ensure the compiler does not create duplicate copies + } + + private: + MicroProfileExitScope& operator=(const MicroProfileExitScope&); +}; + +class MicroProfileExitScopeHelp +{ + public: + template + MicroProfileExitScope operator+(T t) + { + return t; + } +}; +#define defer const auto& CONCAT(defer__, __LINE__) = MicroProfileExitScopeHelp() + [&]() + +////////////////////////////////////////////////////////////////////////// +// platform IMPL +void* MicroProfileAllocInternal(size_t nSize, size_t nAlign); +void MicroProfileFreeInternal(void* pPtr); +void* MicroProfileReallocInternal(void* pPtr, size_t nSize); + +void* MicroProfileAllocAligned(size_t nSize, size_t nAlign); +void MicroProfileFreeAligned(void* pMem); + +#if defined(__APPLE__) +#include +#include +#include +#include +#include +#include + +#if TARGET_OS_IPHONE +#define MICROPROFILE_IOS +#endif + +#define MP_TICK() mach_absolute_time() +inline int64_t MicroProfileTicksPerSecondCpu_() +{ + static int64_t nTicksPerSecond = 0; + if(nTicksPerSecond == 0) + { + mach_timebase_info_data_t sTimebaseInfo; + mach_timebase_info(&sTimebaseInfo); + nTicksPerSecond = 1000000000ll * sTimebaseInfo.denom / sTimebaseInfo.numer; + } + return nTicksPerSecond; +} + +int64_t MicroProfileTicksPerSecondCpu() +{ + return MicroProfileTicksPerSecondCpu_(); +} +#define MicroProfileTicksPerSecondCpu MicroProfileTicksPerSecondCpu_ + +inline uint64_t MicroProfileGetCurrentThreadId() +{ + uint64_t tid; + pthread_threadid_np(pthread_self(), &tid); + return tid; +} + +#include + +#define MP_BREAK() __builtin_trap() +#define MP_THREAD_LOCAL __thread +#define MP_STRCASECMP strcasecmp +#define MP_GETCURRENTTHREADID() MicroProfileGetCurrentThreadId() +#define MP_STRCASESTR strcasestr +#define MP_THREAD_LOCAL __thread +#define MP_NOINLINE __attribute__((noinline)) + +void* MicroProfileAllocAligned(size_t nSize, size_t nAlign) +{ + void* p; + int result = posix_memalign(&p, nAlign, nSize); + if(result != 0) + { + return nullptr; + } + return p; +} + +void MicroProfileFreeAligned(void* pMem) +{ + free(pMem); +} + +#elif defined(_WIN32) +#include +#include +#include +int64_t MicroProfileGetTick(); +#define MP_TICK() MicroProfileGetTick() +#define MP_BREAK() __debugbreak() +#define MP_THREAD_LOCAL __declspec(thread) +#define MP_STRCASECMP _stricmp +#define MP_GETCURRENTTHREADID() GetCurrentThreadId() +#define MP_STRCASESTR StrStrI +#define MP_THREAD_LOCAL __declspec(thread) +#define MP_NOINLINE __declspec(noinline) + +#ifndef MICROPROFILE_WIN32_TRAP_ALLOCATOR +#define MICROPROFILE_WIN32_TRAP_ALLOCATOR 0 +#endif + +#if MICROPROFILE_WIN32_TRAP_ALLOCATOR +// minimal trap allocator +#define PAGE_SIZE (4096) +void* MicroProfileAllocAligned(size_t nSize, size_t nAlign) +{ + (void)nAlign; + size_t nAlignedSize = (nSize + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)); + size_t nDelta = nAlignedSize - nSize; + size_t nFullSize = nAlignedSize + 2 * PAGE_SIZE; + + void* ptr = VirtualAlloc(0, nFullSize, MEM_RESERVE, PAGE_READWRITE); + intptr_t intptr = (intptr_t)ptr; + + void* pResult = VirtualAlloc((void*)(intptr + PAGE_SIZE), nAlignedSize, MEM_COMMIT, PAGE_READWRITE); + memset(pResult, 0xf0, nAlignedSize); + + intptr_t page = (intptr_t)pResult; + //((char*)page)[-1] = 0x70; //trap test + page += nDelta; + pResult = (void*)page; + memset(pResult, 0xfe, nSize); + //((char*)page)[nSize] = 0x70; //trap test + return (void*)page; +} + +void MicroProfileFreeAligned(void* pMem) +{ + intptr_t intptr = (intptr_t)pMem; + intptr = (intptr & (~(PAGE_SIZE - 1))) - PAGE_SIZE; + VirtualFree(pMem, 0, MEM_RELEASE); +} +#else +void* MicroProfileAllocAligned(size_t nSize, size_t nAlign) +{ + return _aligned_malloc(nSize, nAlign); +} + +void MicroProfileFreeAligned(void* pMem) +{ + _aligned_free(pMem); +} +#endif + +#else + +#ifndef MICROPROFILE_CUSTOM_PLATFORM +#include +#include +#include +#include +#include + +inline int64_t MicroProfileTicksPerSecondCpu_() +{ + return 1000000000ll; +} + +int64_t MicroProfileTicksPerSecondCpu() +{ + return MicroProfileTicksPerSecondCpu_(); +} +#define MicroProfileTicksPerSecondCpu MicroProfileTicksPerSecondCpu_ + +inline int64_t MicroProfileGetTick() +{ + timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + return 1000000000ll * ts.tv_sec + ts.tv_nsec; +} +#define MP_TICK() MicroProfileGetTick() +#define MP_BREAK() __builtin_trap() +#define MP_THREAD_LOCAL __thread +#define MP_STRCASECMP strcasecmp +#define MP_GETCURRENTTHREADID() (uint64_t) pthread_self() +#define MP_STRCASESTR strcasestr +#define MP_THREAD_LOCAL __thread +#define MP_NOINLINE __attribute__((noinline)) + +void* MicroProfileAllocAligned(size_t nSize, size_t nAlign) +{ +#if defined(__linux__) + void* p; + int result = posix_memalign(&p, nAlign, nSize); + if(result != 0) + { + return nullptr; + } + return p; +#else + return memalign(nAlign, nSize); +#endif +} +void MicroProfileFreeAligned(void* pMem) +{ + free(pMem); +} +#endif + +#endif + +#ifdef MICROPROFILE_PS4 +#define MICROPROFILE_PS4_DECL +#include "microprofile_ps4.h" +#endif + +#ifdef MICROPROFILE_XBOXONE +#define MICROPROFILE_XBOXONE_DECL +#include "microprofile_xboxone.h" +#else +#ifdef _WIN32 +#include +#endif +#endif + +#ifdef _WIN32 +typedef uint32_t MicroProfileThreadIdType; +#else +#ifdef MICROPROFILE_THREADID_SIZE_4BYTE +typedef uint32_t MicroProfileThreadIdType; +#elif MICROPROFILE_THREADID_SIZE_8BYTE +typedef uint64_t MicroProfileThreadIdType; +#else +typedef uint64_t MicroProfileThreadIdType; +#endif +#endif + +#define MP_ASSERT(a) \ + do \ + { \ + if(!(a)) \ + { \ + MP_BREAK(); \ + } \ + } while(0) + +#ifdef _WIN32 +#include +typedef UINT_PTR MpSocket; +#else +typedef int MpSocket; +#endif + +#ifndef _WIN32 +typedef pthread_t MicroProfileThread; +#elif defined(_WIN32) +#if _MSC_VER == 1900 +typedef void* HANDLE; +#endif + +typedef HANDLE MicroProfileThread; +#else +typedef std::thread* MicroProfileThread; +#endif + +#if MICROPROFILE_DYNAMIC_INSTRUMENT +struct MicroProfileSymbolDesc; + +#define MICROPROFILE_SUSPEND_MAX (4 << 10) +struct MicroProfileSuspendState +{ + uint32_t SuspendCounter = 0; + uint32_t NumSuspended = 0; +#ifdef _WIN32 + HANDLE Suspended[MICROPROFILE_SUSPEND_MAX]; + intptr_t SuspendedIP[MICROPROFILE_SUSPEND_MAX]; +#endif +}; + +void MicroProfileSymbolQueryFunctions(MpSocket Connection, const char* pFilter); +bool MicroProfileInstrumentFunction(void* pFunction, const char* pModuleName, const char* pFunctionName, uint32_t nColor); +bool MicroProfileSymbolInitialize(bool bStartLoad, const char* pModuleName = 0); +MicroProfileSymbolDesc* MicroProfileSymbolFindFuction(void* pAddress); +void MicroProfileInstrumentFunctionsCalled(void* pFunction, const char* pModuleName, const char* pFunctionName, int nMinBytes, int nMaxCalls); +void MicroProfileSymbolQuerySendResult(MpSocket Connection); +void MicroProfileSymbolSendFunctionNames(MpSocket Connection); +void MicroProfileSymbolSendErrors(MpSocket Connection); +const char* MicroProfileSymbolModuleGetString(uint32_t nIndex); +void MicroProfileInstrumentWithoutSymbols(const char** pModules, const char** pSymbols, uint32_t nNumSymbols); +void MicroProfileSymbolUpdateModuleList(); +bool MicroProfileSymInit(); +void MicroProfileSymCleanup(); +#endif + +struct MicroProfileFunctionQuery; + +// hash table functions & declarations +struct MicroProfileHashTable; +struct MicroProfileHashTableIterator; +typedef bool (*MicroProfileHashCompareFunction)(uint64_t l, uint64_t r); +typedef uint64_t (*MicroProfileHashFunction)(uint64_t p); +uint64_t MicroProfileHashTableHashString(uint64_t pString); +bool MicroProfileHashTableCompareString(uint64_t L, uint64_t R); +uint64_t MicroProfileHashTableHashPtr(uint64_t pString); +bool MicroProfileHashTableComparePtr(uint64_t L, uint64_t R); +void MicroProfileHashTableInit(MicroProfileHashTable* pTable, uint32_t nInitialSize, uint32_t nSearchLimit, MicroProfileHashCompareFunction CompareFunc, MicroProfileHashFunction HashFunc); +void MicroProfileHashTableDestroy(MicroProfileHashTable* pTable); +uint64_t MicroProfileHashTableHash(MicroProfileHashTable* pTable, uint64_t K); +bool MicroProfileHashTableSet(MicroProfileHashTable* pTable, uint64_t Key, uintptr_t Value); +MicroProfileHashTableIterator MicroProfileGetHashTableIteratorBegin(MicroProfileHashTable* HashTable); +MicroProfileHashTableIterator MicroProfileGetHashTableIteratorEnd(MicroProfileHashTable* HashTable); + +template +struct MicroProfileArray +{ + T* Data = nullptr; + uint32_t Size = 0; + uint32_t Capacity = 0; + T& operator[](const uint32_t Index); + const T& operator[](const uint32_t Index) const; + T* begin(); + T* end(); +}; + +template +void MicroProfileArrayInit(MicroProfileArray& Array, uint32_t InitialCapacity); +template +void MicroProfileArrayDestroy(MicroProfileArray& Array, uint32_t InitialCapacity); +template +void MicroProfileArrayClear(MicroProfileArray& Array); +template +void MicroProfileArrayPushBack(MicroProfileArray& Array, const T& v); + +struct MicroProfileTimer +{ + uint64_t nTicks; + uint32_t nCount; +}; + +struct MicroProfileCategory +{ + char pName[MICROPROFILE_NAME_MAX_LEN]; + uint32_t nGroupMask[MICROPROFILE_MAX_GROUP_INTS]; +}; + +struct MicroProfileGroupInfo +{ + char pName[MICROPROFILE_NAME_MAX_LEN]; + uint32_t nNameLen; + uint32_t nGroupIndex; + uint32_t nNumTimers; + uint32_t nMaxTimerNameLen; + uint32_t nColor; + uint32_t nCategory; + MicroProfileTokenType Type; + int nWSNext; +}; + +struct MicroProfileTimerInfo +{ + MicroProfileToken nToken; + uint32_t nTimerIndex; + uint32_t nGroupIndex; + char pName[MICROPROFILE_NAME_MAX_LEN]; + char pNameExt[MICROPROFILE_NAME_MAX_LEN]; + uint32_t nNameLen; + uint32_t nColor; + int nWSNext; + bool bGraph; + MicroProfileTokenType Type; + uint32_t Flags; +}; + +struct MicroProfileCounterInfo +{ + int nParent; + int nSibling; + int nFirstChild; + uint16_t nNameLen; + uint8_t nLevel; + const char* pName; + uint32_t nFlags; + int64_t nLimit; + double dLimit; + int nWSNext; + MicroProfileCounterFormat eFormat; + std::atomic ExternalAtomic; +}; + +struct MicroProfileCounterHistory +{ + uint32_t nPut; + uint64_t nHistory[MICROPROFILE_GRAPH_HISTORY]; +}; + +struct MicroProfileCounterSource +{ + void* pSource; + uint32_t nSourceSize; +}; + +struct MicroProfileGraphState +{ + int64_t nHistory[MICROPROFILE_GRAPH_HISTORY]; + MicroProfileToken nToken; + int32_t nKey; +}; + +struct MicroProfileContextSwitch +{ + MicroProfileThreadIdType nThreadOut; + MicroProfileThreadIdType nThreadIn; + int64_t nCpu : 8; + int64_t nTicks : 56; +}; + +struct MicroProfileFrameState +{ + uint64_t nFrameStartCpu; + uint64_t nFrameStartGpu; + uint64_t nFrameId; + uint32_t nGpuPending; + uint32_t nLogStart[MICROPROFILE_MAX_THREADS]; + uint32_t nLogStartTimeline; + uint32_t nTimelineFrameMax; + int32_t nHistoryTimeline; +}; + +// All frame counter data stored. Used to store the time for all counters/groups for every frame. +// Must be enabled with MicroProfileEnableFrameCounterExtraData() +// Will allocate sizeof(MicroProfileFrameExtraCounterData) * MICROPROFILE_MAX_FRAME_HISTORY bytes +struct MicroProfileFrameExtraCounterData +{ + uint16_t NumTimers; + uint16_t NumGroups; + uint64_t Timers[MICROPROFILE_MAX_TIMERS]; + uint64_t Groups[MICROPROFILE_MAX_GROUPS]; +}; + +struct MicroProfileCsvConfig +{ + enum CsvConfigState + { + INACTIVE = 0, + CONFIG, + ACTIVE, + }; + CsvConfigState State; + uint32_t NumTimers; + uint32_t NumGroups; + uint32_t NumCounters; + uint32_t MaxTimers; + uint32_t MaxGroups; + uint32_t MaxCounters; + uint32_t TotalElements; + uint16_t* TimerIndices; + uint16_t* GroupIndices; + uint16_t* CounterIndices; + uint64_t* FrameData; + const char** pTimerNames; + const char** pGroupNames; + const char** pCounterNames; + uint32_t Flags; +}; + +#ifdef _WIN32 +#pragma warning(push) +#pragma warning(disable : 4200) // zero-sized struct +#pragma warning(disable : 4201) // nameless struct/union +#pragma warning(disable : 4244) // possible loss of data +#pragma warning(disable : 4100) // unreferenced formal parameter +#pragma warning(disable : 4091) +#pragma warning(disable : 4189) // local variable is initialized but not referenced. (for defer local variables) +#pragma warning(disable : 4456) +#pragma warning(disable : 4702) +#endif + +struct MicroProfileStringBlock +{ + enum + { + DEFAULT_SIZE = 8192, + }; + MicroProfileStringBlock* pNext; + uint32_t nUsed; + uint32_t nSize; + char Memory[]; +}; + +struct MicroProfileHashTableEntry +{ + uint64_t Key; + uint64_t Hash; + uintptr_t Value; +}; + +struct MicroProfileHashTable +{ + MicroProfileHashTableEntry* pEntries; + uint32_t nUsed; + uint32_t nAllocated; + uint32_t nSearchLimit; + uint32_t nLim; + MicroProfileHashCompareFunction CompareFunc; + MicroProfileHashFunction HashFunc; +}; + +struct MicroProfileHashTableIterator +{ + MicroProfileHashTableIterator(uint32_t nIndex, MicroProfileHashTable* pTable) + : nIndex(nIndex) + , pTable(pTable) + { + } + MicroProfileHashTableIterator(const MicroProfileHashTableIterator& other) + : nIndex(other.nIndex) + , pTable(other.pTable) + { + } + + uint32_t nIndex; + MicroProfileHashTable* pTable; + + void AssertValid() + { + MP_ASSERT(nIndex < pTable->nAllocated); + } + + MicroProfileHashTableEntry& operator*() + { + AssertValid(); + return pTable->pEntries[nIndex]; + } + MicroProfileHashTableEntry* operator->() + { + AssertValid(); + return &pTable->pEntries[nIndex]; + } + bool operator==(const MicroProfileHashTableIterator& rhs) + { + return nIndex == rhs.nIndex && pTable == rhs.pTable; + } + bool operator!=(const MicroProfileHashTableIterator& rhs) + { + return nIndex != rhs.nIndex || pTable != rhs.pTable; + } + + void SkipInvalid() + { + while(nIndex < pTable->nAllocated && pTable->pEntries[nIndex].Hash == 0) + nIndex++; + } + MicroProfileHashTableIterator operator++() + { + AssertValid(); + nIndex++; + SkipInvalid(); + return *this; + } + MicroProfileHashTableIterator operator++(int) + { + MicroProfileHashTableIterator tmp = *this; + ++(*this); + return tmp; + } +}; + +struct MicroProfileStrings +{ + MicroProfileHashTable HashTable; + MicroProfileStringBlock* pFirst; + MicroProfileStringBlock* pLast; +}; + +struct MicroProfileThreadLog +{ + + std::atomic nPut; + std::atomic nGet; + + MicroProfileLogEntry Log[MICROPROFILE_BUFFER_SIZE]; + + uint32_t nStackPut; + uint32_t nStackScope; +#ifdef MICROPROFILE_VERIFY_BALANCED + uint64_t VerifyStack[MICROPROFILE_STACK_MAX]; +#endif + MicroProfileScopeStateC ScopeState[MICROPROFILE_STACK_MAX]; + + uint32_t nActive; + uint32_t nGpu; + MicroProfileThreadIdType nThreadId; + uint32_t nLogIndex; + uint32_t nCustomId; + uint32_t nIdleFrames; + + MicroProfileLogEntry nStackLogEntry[MICROPROFILE_STACK_MAX]; + uint64_t nChildTickStack[MICROPROFILE_STACK_MAX + 1]; + int32_t nStackPos; + + uint8_t nGroupStackPos[MICROPROFILE_MAX_GROUPS]; + uint64_t nGroupTicks[MICROPROFILE_MAX_GROUPS]; + uint64_t nAggregateGroupTicks[MICROPROFILE_MAX_GROUPS]; + enum + { + THREAD_MAX_LEN = 64, + }; + char ThreadName[64]; + int nFreeListNext; +}; + +struct MicroProfileWebSocketBuffer +{ + char* pBufferAllocation; + char* pBuffer; + uint32_t nBufferSize; + uint32_t nPut; + MpSocket Socket; + + char SendBuffer[MICROPROFILE_WEBSOCKET_BUFFER_SIZE]; + std::atomic nSendPut; + std::atomic nSendGet; +}; + +typedef void (*MicroProfileHookFunc)(int x); + +struct MicroProfilePatchError +{ + unsigned char Code[32]; + char Message[256]; + int AlreadyInstrumented; + int nCodeSize; +}; + +// linear, per-frame per-thread gpu log +struct MicroProfileThreadLogGpu +{ + MicroProfileLogEntry Log[MICROPROFILE_GPU_BUFFER_SIZE]; + uint32_t nPut; + uint32_t nStart; + uint32_t nId; + void* pContext; + uint32_t nAllocated; + + uint32_t nStackScope; + MicroProfileScopeStateC ScopeState[MICROPROFILE_STACK_MAX]; +}; + +#if MICROPROFILE_GPU_TIMERS +static MicroProfileGpuInsertTimeStamp_CB MicroProfileGpuInsertTimeStamp_Callback = 0; +static MicroProfileGpuGetTimeStamp_CB MicroProfileGpuGetTimeStamp_Callback = 0; +static MicroProfileTicksPerSecondGpu_CB MicroProfileTicksPerSecondGpu_Callback = 0; +static MicroProfileGetGpuTickReference_CB MicroProfileGetGpuTickReference_Callback = 0; +static MicroProfileGpuFlip_CB MicroProfileGpuFlip_Callback = 0; +static MicroProfileGpuShutdown_CB MicroProfileGpuShutdown_Callback = 0; + +uint32_t MicroProfileGpuInsertTimeStamp(void* pContext) +{ + return MicroProfileGpuInsertTimeStamp_Callback ? MicroProfileGpuInsertTimeStamp_Callback(pContext) : 0; +} +uint64_t MicroProfileGpuGetTimeStamp(uint32_t nKey) +{ + return MicroProfileGpuGetTimeStamp_Callback ? MicroProfileGpuGetTimeStamp_Callback(nKey) : 1; +} +uint64_t MicroProfileTicksPerSecondGpu() +{ + return MicroProfileTicksPerSecondGpu_Callback ? MicroProfileTicksPerSecondGpu_Callback() : 1; +} +int MicroProfileGetGpuTickReference(int64_t* pOutCPU, int64_t* pOutGpu) +{ + return MicroProfileGetGpuTickReference_Callback ? MicroProfileGetGpuTickReference_Callback(pOutCPU, pOutGpu) : 0; +} +uint32_t MicroProfileGpuFlip(void* p) +{ + return MicroProfileGpuFlip_Callback ? MicroProfileGpuFlip_Callback(p) : 0; +} +void MicroProfileGpuShutdown() +{ + if(MicroProfileGpuShutdown_Callback) + MicroProfileGpuShutdown_Callback(); +} + +#endif + +#if MICROPROFILE_GPU_TIMERS_D3D11 +//:'######:::'########::'##::::'##::::'########:::'#######::'########:::::'##::::::'##::: +//'##... ##:: ##.... ##: ##:::: ##:::: ##.... ##:'##.... ##: ##.... ##::'####::::'####::: +// ##:::..::: ##:::: ##: ##:::: ##:::: ##:::: ##:..::::: ##: ##:::: ##::.. ##::::.. ##::: +// ##::'####: ########:: ##:::: ##:::: ##:::: ##::'#######:: ##:::: ##:::: ##:::::: ##::: +// ##::: ##:: ##.....::: ##:::: ##:::: ##:::: ##::...... ##: ##:::: ##:::: ##:::::: ##::: +// ##::: ##:: ##:::::::: ##:::: ##:::: ##:::: ##:'##:::: ##: ##:::: ##:::: ##:::::: ##::: +//. ######::: ##::::::::. #######::::: ########::. #######:: ########:::'######::'######: +//:......::::..::::::::::.......::::::........::::.......:::........::::......:::......:: + +struct MicroProfileD3D11Frame +{ + uint32_t m_nQueryStart; + uint32_t m_nQueryCountMax; + std::atomic m_nQueryCount; + uint32_t m_nRateQueryStarted; + void* m_pRateQuery; +}; + +struct MicroProfileGpuTimerStateD3D11 : public MicroProfileGpuTimerState +{ + uint32_t bInitialized; + void* m_pDevice; + void* m_pImmediateContext; + void* m_pQueries[MICROPROFILE_D3D11_MAX_QUERIES]; + int64_t m_nQueryResults[MICROPROFILE_D3D11_MAX_QUERIES]; + + uint32_t m_nQueryPut; + uint32_t m_nQueryGet; + uint32_t m_nQueryFrame; + int64_t m_nQueryFrequency; + void* pSyncQuery; + + MicroProfileD3D11Frame m_QueryFrames[MICROPROFILE_GPU_FRAME_DELAY]; +}; + +uint32_t MicroProfileGpuInsertTimeStampD3D11(void* pContext_); +uint64_t MicroProfileGpuGetTimeStampD3D11(uint32_t nIndex); +bool MicroProfileGpuGetDataD3D11(void* pQuery, void* pData, uint32_t nDataSize); +uint64_t MicroProfileTicksPerSecondGpuD3D11(); +uint32_t MicroProfileGpuFlipD3D11(void* pDeviceContext_); +void MicroProfileGpuInitD3D11(void* pDevice_, void* pImmediateContext); +void MicroProfileGpuShutdownD3D11(); +int MicroProfileGetGpuTickReferenceD3D11(int64_t* pOutCPU, int64_t* pOutGpu); +MicroProfileGpuTimerStateD3D11* MicroProfileGetGpuTimerStateD3D11(); +#endif + +#if MICROPROFILE_GPU_TIMERS_D3D12 +//:'######:::'########::'##::::'##::::'########:::'#######::'########:::::'##::::'#######:: +//'##... ##:: ##.... ##: ##:::: ##:::: ##.... ##:'##.... ##: ##.... ##::'####:::'##.... ##: +// ##:::..::: ##:::: ##: ##:::: ##:::: ##:::: ##:..::::: ##: ##:::: ##::.. ##:::..::::: ##: +// ##::'####: ########:: ##:::: ##:::: ##:::: ##::'#######:: ##:::: ##:::: ##::::'#######:: +// ##::: ##:: ##.....::: ##:::: ##:::: ##:::: ##::...... ##: ##:::: ##:::: ##:::'##:::::::: +// ##::: ##:: ##:::::::: ##:::: ##:::: ##:::: ##:'##:::: ##: ##:::: ##:::: ##::: ##:::::::: +//. ######::: ##::::::::. #######::::: ########::. #######:: ########:::'######: #########: +//:......::::..::::::::::.......::::::........::::.......:::........::::......::.........:: + +#include + +#ifndef MICROPROFILE_D3D12_MAX_QUERIES +#define MICROPROFILE_D3D12_MAX_QUERIES (32 << 10) +#endif + +#define MICROPROFILE_D3D_MAX_NODE_COUNT 4 +#define MICROPROFILE_D3D_INTERNAL_DELAY 8 + +#define MP_NODE_MASK_ALL(n) ((1u << (n)) - 1u) +#define MP_NODE_MASK_ONE(n) (1u << (n)) + +struct MicroProfileGpuTimerStateD3D12; + +int MicroProfileGetGpuTickReferenceD3D12(int64_t* pOutCPU, int64_t* pOutGpu); +uint32_t MicroProfileGpuInsertTimeStampD3D12(void* pContext); +uint64_t MicroProfileGpuGetTimeStampD3D12(uint32_t nIndex); +uint64_t MicroProfileTicksPerSecondGpuD3D12(); +uint32_t MicroProfileGpuFlipD3D12(void* pContext); +void MicroProfileGpuInitD3D12(void* pDevice_, uint32_t nNodeCount, void** pCommandQueues_, void** pCommandQueuesCopy_); +void MicroProfileGpuShutdownD3D12(); +void MicroProfileSetCurrentNodeD3D12(uint32_t nNode); +int MicroProfileGetGpuTickReferenceD3D12(int64_t* pOutCPU, int64_t* pOutGpu); +MicroProfileGpuTimerStateD3D12* MicroProfileGetGpuTimerStateD3D12(); + +struct MicroProfileFrameD3D12 +{ + uint32_t nTimeStampBegin; + uint32_t nTimeStampCount; + uint32_t nTimeStampBeginCopyQueue; + uint32_t nTimeStampCountCopyQueue; + uint32_t nNode; + ID3D12GraphicsCommandList* pCommandList[MICROPROFILE_D3D_MAX_NODE_COUNT]; + ID3D12GraphicsCommandList* pCommandListCopy[MICROPROFILE_D3D_MAX_NODE_COUNT]; + ID3D12CommandAllocator* pCommandAllocator; + ID3D12CommandAllocator* pCommandAllocatorCopy; +}; + +struct MicroProfileGpuTimerStateD3D12 : public MicroProfileGpuTimerState +{ + ID3D12Device* pDevice; + uint32_t nNodeCount; + uint32_t nCurrentNode; + + uint64_t nFrame; + uint64_t nPendingFrame; + + uint32_t nFrameStartTimeStamps; + uint32_t nFrameStartCopyQueueTimeStamps; + std::atomic nFrameCountTimeStamps; + std::atomic nFrameCountCopyQueueTimeStamps; + + int64_t nFrequency; + ID3D12Resource* pBuffer; + ID3D12Resource* pBufferCopy; + + struct + { + ID3D12CommandQueue* pCommandQueue; + ID3D12CommandQueue* pCommandQueueCopy; + ID3D12QueryHeap* pHeap; + ID3D12QueryHeap* pCopyQueueHeap; + ID3D12Fence* pFence; + ID3D12Fence* pFenceCopy; + } NodeState[MICROPROFILE_D3D_MAX_NODE_COUNT]; + + uint16_t nQueryFrames[MICROPROFILE_D3D12_MAX_QUERIES]; + int64_t nResults[MICROPROFILE_D3D12_MAX_QUERIES]; + uint16_t nQueryFramesCopy[MICROPROFILE_D3D12_MAX_QUERIES]; + int64_t nResultsCopy[MICROPROFILE_D3D12_MAX_QUERIES]; + + MicroProfileFrameD3D12 Frames[MICROPROFILE_D3D_INTERNAL_DELAY]; +}; +#endif + +#if MICROPROFILE_GPU_TIMERS_GL +//:'######:::'########::'##::::'##:::::'######:::'##::::::: +//'##... ##:: ##.... ##: ##:::: ##::::'##... ##:: ##::::::: +// ##:::..::: ##:::: ##: ##:::: ##:::: ##:::..::: ##::::::: +// ##::'####: ########:: ##:::: ##:::: ##::'####: ##::::::: +// ##::: ##:: ##.....::: ##:::: ##:::: ##::: ##:: ##::::::: +// ##::: ##:: ##:::::::: ##:::: ##:::: ##::: ##:: ##::::::: +//. ######::: ##::::::::. #######:::::. ######::: ########: +//:......::::..::::::::::.......:::::::......::::........:: +struct MicroProfileGpuTimerStateGL : public MicroProfileGpuTimerState +{ + uint32_t GLTimers[MICROPROFILE_GL_MAX_QUERIES]; + uint32_t GLTimerPos; +}; + +MicroProfileGpuTimerStateGL* MicroProfileGetGpuTimerStateGL(); +uint32_t MicroProfileGpuInsertTimeStampGL(void* pContext); +uint64_t MicroProfileGpuGetTimeStampGL(uint32_t nKey); +uint64_t MicroProfileTicksPerSecondGpuGL(); +int MicroProfileGetGpuTickReferenceGL(int64_t* pOutCpu, int64_t* pOutGpu); +uint32_t MicroProfileGpuFlipGL(void* pContext); +void MicroProfileGpuShutdownGL(); +#endif + +#if MICROPROFILE_GPU_TIMERS_VULKAN + +//:'######:::'########::'##::::'##::::'##::::'##:'##::::'##:'##:::::::'##:::'##::::'###::::'##::: ##: +//'##... ##:: ##.... ##: ##:::: ##:::: ##:::: ##: ##:::: ##: ##::::::: ##::'##::::'## ##::: ###:: ##: +// ##:::..::: ##:::: ##: ##:::: ##:::: ##:::: ##: ##:::: ##: ##::::::: ##:'##::::'##:. ##:: ####: ##: +// ##::'####: ########:: ##:::: ##:::: ##:::: ##: ##:::: ##: ##::::::: #####::::'##:::. ##: ## ## ##: +// ##::: ##:: ##.....::: ##:::: ##::::. ##:: ##:: ##:::: ##: ##::::::: ##. ##::: #########: ##. ####: +// ##::: ##:: ##:::::::: ##:::: ##:::::. ## ##::: ##:::: ##: ##::::::: ##:. ##:: ##.... ##: ##:. ###: +//. ######::: ##::::::::. #######:::::::. ###::::. #######:: ########: ##::. ##: ##:::: ##: ##::. ##: +//:......::::..::::::::::.......:::::::::...::::::.......:::........::..::::..::..:::::..::..::::..:: + +struct MicroProfileGpuTimerStateVulkan; +MicroProfileGpuTimerStateVulkan* MicroProfileGetGpuTimerStateVulkan(); +uint32_t MicroProfileGpuInsertTimeStampVulkan(void* pContext); +uint64_t MicroProfileGpuGetTimeStampVulkan(uint32_t nKey); +uint64_t MicroProfileTicksPerSecondGpuVulkan(); +int MicroProfileGetGpuTickReferenceVulkan(int64_t* pOutCpu, int64_t* pOutGpu); +uint32_t MicroProfileGpuFlipVulkan(void* pContext); +void MicroProfileGpuShutdownVulkan(); +#endif + +struct MicroProfileSymbolState +{ + std::atomic nModuleLoadsFinished; + std::atomic nModuleLoadsRequested; + std::atomic nSymbolsLoaded; +}; + +struct MicroProfileSymbolModuleRegion +{ + intptr_t nBegin; + intptr_t nEnd; +}; +struct MicroProfileSymbolModule +{ + uint64_t nModuleBase; + uint32_t nMatchOffset; + uint32_t nStringOffset; + const char* pBaseString; + const char* pTrimmedString; + MicroProfileSymbolModuleRegion Regions[MICROPROFILE_MAX_MODULE_EXEC_REGIONS]; + int nNumExecutableRegions; + + bool bDownloading; + intptr_t nProgress; + intptr_t nProgressTarget; + struct MicroProfileSymbolBlock* pSymbolBlock; + MicroProfileHashTable AddressToSymbol; + + int64_t nSymbols; + std::atomic nSymbolsLoaded; + std::atomic nModuleLoadRequested; + std::atomic nModuleLoadFinished; +}; + +struct MicroProfileInstrumentMemoryRegion +{ + intptr_t Start; + intptr_t Size; + uint32_t Protect; +}; + +struct MicroProfile +{ + uint32_t nTotalTimers; + uint32_t nGroupCount; + uint32_t nCategoryCount; + uint32_t nAggregateClear; + uint32_t nAggregateFlip; + uint32_t nAggregateFlipCount; + uint32_t nAggregateFrames; + + uint64_t nFlipStartTick; + uint64_t nAggregateFlipTick; + + uint32_t nDisplay; + uint32_t nBars; + uint32_t nActiveGroups[MICROPROFILE_MAX_GROUP_INTS]; + bool AnyActive; + uint32_t nFrozen; + uint32_t nWasFrozen; + uint32_t nPlatformMarkersEnabled; + + uint32_t nForceEnable; + + uint32_t nForceGroups[MICROPROFILE_MAX_GROUP_INTS]; + uint32_t nActiveGroupsWanted[MICROPROFILE_MAX_GROUP_INTS]; + uint32_t nGroupMask[MICROPROFILE_MAX_GROUP_INTS]; + + uint32_t nStartEnabled; + uint32_t nAllThreadsWanted; + + uint32_t nOverflow; + + uint32_t nMaxGroupSize; + uint32_t nDumpFileNextFrame; + uint32_t nDumpFileCountDown; + uint32_t nDumpSpikeMask; + uint32_t nAutoClearFrames; + + float fDumpCpuSpike; + float fDumpGpuSpike; + char HtmlDumpPath[512]; + char CsvDumpPath[512]; + uint32_t DumpFrameCount; + + int64_t nPauseTicks; + std::atomic nContextSwitchStalledTick; + int64_t nContextSwitchLastPushed; + int64_t nContextSwitchLastIndexPushed; + + float fReferenceTime; + float fRcpReferenceTime; + + MicroProfileCategory CategoryInfo[MICROPROFILE_MAX_CATEGORIES]; + MicroProfileGroupInfo GroupInfo[MICROPROFILE_MAX_GROUPS]; + MicroProfileTimerInfo TimerInfo[MICROPROFILE_MAX_TIMERS]; + uint32_t TimerToGroup[MICROPROFILE_MAX_TIMERS]; + + MicroProfileTimer AccumTimers[MICROPROFILE_MAX_TIMERS]; + uint64_t AccumMaxTimers[MICROPROFILE_MAX_TIMERS]; + uint64_t AccumMinTimers[MICROPROFILE_MAX_TIMERS]; + uint64_t AccumTimersExclusive[MICROPROFILE_MAX_TIMERS]; + uint64_t AccumMaxTimersExclusive[MICROPROFILE_MAX_TIMERS]; + + MicroProfileTimer Frame[MICROPROFILE_MAX_TIMERS]; + uint64_t FrameExclusive[MICROPROFILE_MAX_TIMERS]; + + MicroProfileTimer Aggregate[MICROPROFILE_MAX_TIMERS]; + uint64_t AggregateMax[MICROPROFILE_MAX_TIMERS]; + uint64_t AggregateMin[MICROPROFILE_MAX_TIMERS]; + uint64_t AggregateExclusive[MICROPROFILE_MAX_TIMERS]; + uint64_t AggregateMaxExclusive[MICROPROFILE_MAX_TIMERS]; + + uint32_t FrameGroupThreadValid[MICROPROFILE_MAX_THREADS / 32 + 1]; + struct GroupTime + { + uint64_t nTicks; + uint64_t nTicksExclusive; + uint32_t nCount; + }; + + GroupTime FrameGroupThread[MICROPROFILE_MAX_THREADS][MICROPROFILE_MAX_GROUPS]; + GroupTime FrameGroup[MICROPROFILE_MAX_GROUPS]; + uint64_t AccumGroup[MICROPROFILE_MAX_GROUPS]; + uint64_t AccumGroupMax[MICROPROFILE_MAX_GROUPS]; + + uint64_t AggregateGroup[MICROPROFILE_MAX_GROUPS]; + uint64_t AggregateGroupMax[MICROPROFILE_MAX_GROUPS]; + + MicroProfileGraphState Graph[MICROPROFILE_MAX_GRAPHS]; + uint32_t nGraphPut; + + uint32_t nThreadActive[MICROPROFILE_MAX_THREADS]; + MicroProfileThreadLog* Pool[MICROPROFILE_MAX_THREADS]; + MicroProfileThreadLogGpu* PoolGpu[MICROPROFILE_MAX_THREADS]; + + MicroProfileThreadLog TimelineLog; + uint32_t TimelineTokenFrameEnter[MICROPROFILE_TIMELINE_MAX_TOKENS]; + uint32_t TimelineTokenFrameLeave[MICROPROFILE_TIMELINE_MAX_TOKENS]; + uint32_t TimelineToken[MICROPROFILE_TIMELINE_MAX_TOKENS]; + const char* TimelineTokenStaticString[MICROPROFILE_TIMELINE_MAX_TOKENS]; + + uint32_t nTimelineFrameMax; + MicroProfileFrameExtraCounterData* FrameExtraCounterData; + MicroProfileCsvConfig CsvConfig; + const char* pSettings; + const char* pSettingsReadOnly; + const char* pSettingsTemp; + + uint32_t nNumLogs; + uint32_t nNumLogsGpu; + uint32_t nMemUsage; + int nFreeListHead; + + uint32_t nFrameCurrent; + uint32_t nFrameCurrentIndex; + uint32_t nFramePut; + uint32_t nFrameNext; + uint64_t nFramePutIndex; + + MicroProfileFrameState Frames[MICROPROFILE_MAX_FRAME_HISTORY]; + + uint64_t nFlipTicks; + uint64_t nFlipAggregate; + uint64_t nFlipMax; + uint64_t nFlipAggregateDisplay; + uint64_t nFlipMaxDisplay; + + MicroProfileThread ContextSwitchThread; + bool bContextSwitchRunning; + bool bContextSwitchStop; + bool bContextSwitchAllThreads; + bool bContextSwitchNoBars; + uint32_t nContextSwitchUsage; + uint32_t nContextSwitchLastPut; + + int64_t nContextSwitchHoverTickIn; + int64_t nContextSwitchHoverTickOut; + uint32_t nContextSwitchHoverThread; + uint32_t nContextSwitchHoverThreadBefore; + uint32_t nContextSwitchHoverThreadAfter; + uint8_t nContextSwitchHoverCpu; + uint8_t nContextSwitchHoverCpuNext; + + uint32_t CoreCount; + uint8_t CoreEfficiencyClass[MICROPROFILE_MAX_CPU_CORES]; + + uint32_t nContextSwitchPut; + MicroProfileContextSwitch ContextSwitch[MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE]; + + MpSocket ListenerSocket; + uint32_t nWebServerPort; + + char WebServerBuffer[MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE]; + uint32_t WebServerPut; + + uint64_t nWebServerDataSent; + + int WebSocketTimers; + int WebSocketCounters; + int WebSocketGroups; + uint32_t nWebSocketDirty; + MpSocket WebSockets[1]; + int64_t WebSocketFrameLast[1]; + uint32_t nNumWebSockets; + uint32_t nSocketFail; // for error propagation. + + MicroProfileThread WebSocketSendThread; + bool WebSocketThreadRunning; + bool WebSocketThreadJoined; + + uint32_t WSCategoriesSent; + uint32_t WSGroupsSent; + uint32_t WSTimersSent; + uint32_t WSCountersSent; + MicroProfileWebSocketBuffer WSBuf; + char* pJsonSettings; + const char* pJsonSettingsName; + bool bJsonSettingsReadOnly; + uint32_t nJsonSettingsPending; + uint32_t nJsonSettingsBufferSize; + uint32_t nWSWasConnected; + uint32_t nMicroProfileShutdown; + uint32_t nWSViewMode; + + char CounterNames[MICROPROFILE_MAX_COUNTER_NAME_CHARS]; + MicroProfileCounterInfo CounterInfo[MICROPROFILE_MAX_COUNTERS]; + MicroProfileCounterSource CounterSource[MICROPROFILE_MAX_COUNTERS]; + uint32_t nNumCounters; + uint32_t nCounterNamePos; + std::atomic Counters[MICROPROFILE_MAX_COUNTERS]; + std::atomic* CountersDouble; +#if MICROPROFILE_COUNTER_HISTORY // uses 1kb per allocated counter. 512kb for default counter count + uint32_t nCounterHistoryPut; + int64_t nCounterHistory[MICROPROFILE_GRAPH_HISTORY][MICROPROFILE_MAX_COUNTERS]; // flipped to make swapping cheap, drawing more expensive. + int64_t nCounterMax[MICROPROFILE_MAX_COUNTERS]; + int64_t nCounterMin[MICROPROFILE_MAX_COUNTERS]; + double* dCounterHistory; + double* dCounterMax; + double* dCounterMin; +#endif + + MicroProfileThread AutoFlipThread; + std::atomic nAutoFlipDelay; + std::atomic nAutoFlipStop; + + MicroProfileStrings Strings; + MicroProfileToken CounterToken_MicroProfile; + MicroProfileToken CounterToken_StringBlock; + MicroProfileToken CounterToken_StringBlock_Count; + MicroProfileToken CounterToken_StringBlock_Waste; + MicroProfileToken CounterToken_StringBlock_Strings; + MicroProfileToken CounterToken_StringBlock_Memory; + + MicroProfileToken CounterToken_Alloc; + MicroProfileToken CounterToken_Alloc_Memory; + MicroProfileToken CounterToken_Alloc_Count; + +#if MICROPROFILE_DYNAMIC_INSTRUMENT + uint32_t DynamicTokenIndex; + MicroProfileToken DynamicTokens[MICROPROFILE_MAX_DYNAMIC_TOKENS]; + void* FunctionsInstrumented[MICROPROFILE_MAX_DYNAMIC_TOKENS]; + const char* FunctionsInstrumentedName[MICROPROFILE_MAX_DYNAMIC_TOKENS]; + const char* FunctionsInstrumentedModuleNames[MICROPROFILE_MAX_DYNAMIC_TOKENS]; + // const char* FunctionsInstrumentedUnmangled[MICROPROFILE_MAX_DYNAMIC_TOKENS]; + uint32_t WSFunctionsInstrumentedSent; + MicroProfileSymbolState SymbolState; + + MicroProfileSymbolModule SymbolModules[MICROPROFILE_INSTRUMENT_MAX_MODULES]; + char SymbolModuleNameBuffer[MICROPROFILE_INSTRUMENT_MAX_MODULE_CHARS]; + int SymbolModuleNameOffset; + int SymbolNumModules; + int WSSymbolModulesSent; + std::atomic nSymbolsDirty; + + MicroProfileFunctionQuery* pPendingQuery; + MicroProfileFunctionQuery* pFinishedQuery; + MicroProfileFunctionQuery* pQueryFreeList; + uint32_t nQueryProcessed; + uint32_t nNumQueryFree; + uint32_t nNumQueryAllocated; + + int SymbolThreadRunning; + int SymbolThreadFinished; + MicroProfileThread SymbolThread; + int nNumPatchErrors; + MicroProfilePatchError PatchErrors[MICROPROFILE_MAX_PATCH_ERRORS]; + int nNumPatchErrorFunctions; + const char* PatchErrorFunctionNames[MICROPROFILE_MAX_PATCH_ERRORS]; + MicroProfileSuspendState SuspendState; + MicroProfileArray MemoryRegions; +#endif + + int GpuQueue; + MicroProfileThreadLogGpu* pGpuGlobal; + MicroProfileGpuTimerState* pGPU; +}; + +inline uint32_t MicroProfileLogGetType(MicroProfileLogEntry Index) +{ + return ((MP_LOG_BEGIN_MASK & Index) >> 62) & 0x3; +} + +inline uint64_t MicroProfileLogGetTimerIndex(MicroProfileLogEntry Index) +{ + return (0x3fff & (Index >> 48)); +} +uint32_t MicroProfileLogGetDataSize(MicroProfileLogEntry Index) +{ + if(MicroProfileLogGetType(Index) == MP_LOG_EXTENDED) + return 0xffff & (Index >> 32); + else + return 0; +} + +inline EMicroProfileTokenExtended MicroProfileLogGetExtendedToken(MicroProfileLogEntry Index) +{ + return (EMicroProfileTokenExtended)(0x3fff & (Index >> 48)); +} + +inline uint32_t MicroProfileLogGetExtendedDataSize(MicroProfileLogEntry Index) +{ + return (uint32_t)(0xffff & (Index >> 32)); +} + +inline uint32_t MicroProfileLogGetExtendedPayload(MicroProfileLogEntry Index) +{ + return (uint32_t)(0xffffffff & Index); +} + +inline uint64_t MicroProfileLogGetExtendedPayloadNoData(MicroProfileLogEntry Index) +{ + return (uint64_t)(MP_LOG_TICK_MASK & Index); +} + +inline void* MicroProfileLogGetExtendedPayloadNoDataPtr(MicroProfileLogEntry Index) +{ + return (void*)(MP_LOG_PAYLOAD_PTR_MASK & Index); +} + +MicroProfileLogEntry MicroProfileMakeLogIndex(uint64_t nBegin, MicroProfileToken nToken, int64_t nTick); +MicroProfileLogEntry MicroProfileMakeLogExtended(EMicroProfileTokenExtended eTokenExt, uint32_t nDataSizeQWords, uint32_t nPayload); +MicroProfileLogEntry MicroProfileMakeLogExtendedNoData(EMicroProfileTokenExtended eTokenExt, uint64_t nTick); + +inline MicroProfileLogEntry MicroProfileMakeLogIndex(uint64_t nBegin, MicroProfileToken nToken, int64_t nTick) +{ + MicroProfileLogEntry Entry = (nBegin << 62) | ((0x3fff & nToken) << 48) | (MP_LOG_TICK_MASK & nTick); + uint32_t t = MicroProfileLogGetType(Entry); + uint64_t nTimerIndex = MicroProfileLogGetTimerIndex(Entry); + MP_ASSERT(t == nBegin); + MP_ASSERT(nTimerIndex == (nToken & 0x3fff)); + return Entry; +} + +// extended data, with the option to store 0xfffe * 8 bytes after +inline MicroProfileLogEntry MicroProfileMakeLogExtended(EMicroProfileTokenExtended eTokenExt, uint32_t nDataSizeQWords, uint32_t nPayload) +{ + MP_ASSERT(nDataSizeQWords < 0xffff); + MicroProfileLogEntry Entry = (((uint64_t)MP_LOG_EXTENDED) << 62) | ((0x3fff & (uint64_t)eTokenExt) << 48) | ((0xffff & (uint64_t)nDataSizeQWords) << 32) | nPayload; + + MP_ASSERT(MicroProfileLogGetExtendedToken(Entry) == eTokenExt); + MP_ASSERT(MicroProfileLogGetExtendedDataSize(Entry) == nDataSizeQWords); + MP_ASSERT(MicroProfileLogGetExtendedPayload(Entry) == nPayload); + + return Entry; +} +// extended with no data, but instead 48 bits payload +inline MicroProfileLogEntry MicroProfileMakeLogExtendedNoData(EMicroProfileTokenExtended eTokenExt, uint64_t nPayload) +{ + MicroProfileLogEntry Entry = (((uint64_t)MP_LOG_EXTENDED_NO_DATA) << 62) | ((0x3fff & (uint64_t)eTokenExt) << 48) | (MP_LOG_TICK_MASK & nPayload); + + MP_ASSERT(MicroProfileLogGetExtendedToken(Entry) == eTokenExt); + MP_ASSERT(MicroProfileLogGetExtendedPayloadNoData(Entry) == nPayload); + + return Entry; +} + +// extended with no data, but instead 61 bits payload. used to store a pointer. +inline MicroProfileLogEntry MicroProfileMakeLogExtendedNoDataPtr(uint64_t nPayload) +{ + uint64_t hest = ETOKEN_CSTR_PTR; + MicroProfileLogEntry Entry = (((uint64_t)MP_LOG_EXTENDED_NO_DATA) << 62) | (hest << 48) | (MP_LOG_PAYLOAD_PTR_MASK & nPayload); + uint64_t v0 = (MP_LOG_PAYLOAD_PTR_MASK & nPayload); + uint64_t v1 = (uint64_t)MicroProfileLogGetExtendedPayloadNoDataPtr(Entry); + + MP_ASSERT(v0 == v1); + return Entry; +} + +inline uint32_t MicroProfileGetQWordSize(uint32_t nDataSize) +{ + uint32_t nSize = (nDataSize + 7) / 8; + MP_ASSERT(nSize < 0xffff); // won't pack... + return nSize; +} + +namespace +{ +struct MicroProfilePayloadPack +{ + union + { + struct + { +#if MICROPROFILE_BIG_ENDIAN /// NOT implemented. + char h; + char message[7]; +#else + char message[7]; + char h; +#endif + }; + uint64_t LogEntry; + }; +}; +}; // namespace + +inline int64_t MicroProfileLogTickDifference(MicroProfileLogEntry Start, MicroProfileLogEntry End) +{ + int64_t nStart = Start; + int64_t nEnd = End; + int64_t nDifference = ((nEnd << 16) - (nStart << 16)); + return nDifference >> 16; +} +inline int64_t MicroProfileLogTickMax(MicroProfileLogEntry A, MicroProfileLogEntry B) +{ + int64_t Diff = MicroProfileLogTickDifference(A, B); + if(Diff < 0) + { + return A; + } + else + { + return B; + } +} + +inline int64_t MicroProfileLogTickMin(MicroProfileLogEntry A, MicroProfileLogEntry B) +{ + int64_t Diff = MicroProfileLogTickDifference(A, B); + if(Diff < 0) + { + return B; + } + else + { + return A; + } +} +inline int64_t MicroProfileLogTickClamp(uint64_t T, uint64_t min, uint64_t max) +{ + return MicroProfileLogTickMin(MicroProfileLogTickMax(T, min), max); +} + +inline int64_t MicroProfileLogGetTick(MicroProfileLogEntry e) +{ + return MP_LOG_TICK_MASK & e; +} + +inline int64_t MicroProfileLogSetTick(MicroProfileLogEntry e, int64_t nTick) +{ + return (MP_LOG_TICK_MASK & nTick) | (e & ~MP_LOG_TICK_MASK); +} + +inline uint16_t MicroProfileGetTimerIndex(MicroProfileToken t) +{ + return (t & 0xffff); +} +inline uint32_t MicroProfileGetGroupMask(MicroProfileToken t) +{ + return (uint32_t)((t >> 16) & MICROPROFILE_GROUP_MASK_ALL); +} +inline uint32_t MicroProfileGetGroupMaskIndex(MicroProfileToken t) +{ + return (uint32_t)(t >> 48); +} + +inline MicroProfileToken MicroProfileMakeToken(uint32_t nGroupMask, uint16_t nGroupIndex, uint16_t nTimer) +{ + uint64_t token = ((uint64_t)nGroupIndex << 48llu) | ((uint64_t)nGroupMask << 16llu) | nTimer; + if(0 != (token & MP_LOG_CSTR_MASK)) + { + MP_BREAK(); // should never happen + } + return token; +} + +template +T MicroProfileMin(T a, T b) +{ + return a < b ? a : b; +} + +template +T MicroProfileMax(T a, T b) +{ + return a > b ? a : b; +} +template +T MicroProfileClamp(T a, T min_, T max_) +{ + return MicroProfileMin(max_, MicroProfileMax(min_, a)); +} + +inline int64_t MicroProfileMsToTick(float fMs, int64_t nTicksPerSecond) +{ + return (int64_t)(fMs * 0.001f * nTicksPerSecond); +} + +inline float MicroProfileTickToMsMultiplier(int64_t nTicksPerSecond) +{ + return 1000.f / (nTicksPerSecond ? nTicksPerSecond : 1); +} +float MicroProfileTickToMsMultiplierCpu() +{ + return MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); +} + +float MicroProfileTickToMsMultiplierGpu() +{ + return MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); +} +uint16_t MicroProfileGetGroupIndex(MicroProfileToken t) +{ + return (uint16_t)MicroProfileGet()->TimerToGroup[MicroProfileGetTimerIndex(t)]; +} + +uint64_t MicroProfileTick() +{ + return MP_TICK(); +} + +#ifdef _WIN32 +#include +#define fopen microprofile_fopen_helper + +FILE* microprofile_fopen_helper(const char* filename, const char* mode) +{ + FILE* F = 0; + if(0 == fopen_s(&F, filename, mode)) + { + return F; + } + return 0; +} + +int64_t MicroProfileTicksPerSecondCpu() +{ + static int64_t nTicksPerSecond = 0; + if(nTicksPerSecond == 0) + { + QueryPerformanceFrequency((LARGE_INTEGER*)&nTicksPerSecond); + } + return nTicksPerSecond; +} +int64_t MicroProfileGetTick() +{ + int64_t ticks; + QueryPerformanceCounter((LARGE_INTEGER*)&ticks); + return ticks; +} + +#endif + +#if 1 + +typedef void* (*MicroProfileThreadFunc)(void*); + +#ifndef _WIN32 +typedef pthread_t MicroProfileThread; +void MicroProfileThreadStart(MicroProfileThread* pThread, MicroProfileThreadFunc Func) +{ + pthread_attr_t Attr; + int r = pthread_attr_init(&Attr); + MP_ASSERT(r == 0); + pthread_create(pThread, &Attr, Func, 0); +} +void MicroProfileThreadJoin(MicroProfileThread* pThread) +{ + int r = pthread_join(*pThread, 0); + MP_ASSERT(r == 0); +} +#elif defined(_WIN32) +typedef HANDLE MicroProfileThread; +DWORD __stdcall ThreadTrampoline(void* pFunc) +{ + MicroProfileThreadFunc F = (MicroProfileThreadFunc)pFunc; + return (uint32_t)(uintptr_t)F(0); +} + +void MicroProfileThreadStart(MicroProfileThread* pThread, MicroProfileThreadFunc Func) +{ + *pThread = CreateThread(0, 0, ThreadTrampoline, Func, 0, 0); +} +void MicroProfileThreadJoin(MicroProfileThread* pThread) +{ + WaitForSingleObject(*pThread, INFINITE); + CloseHandle(*pThread); +} +#else +#include +typedef std::thread* MicroProfileThread; +inline void MicroProfileThreadStart(MicroProfileThread* pThread, MicroProfileThreadFunc Func) +{ + *pThread = MP_ALLOC_OBJECT(std::thread); + new(*pThread) std::thread(Func, nullptr); +} +inline void MicroProfileThreadJoin(MicroProfileThread* pThread) +{ + (*pThread)->join(); + (*pThread)->~thread(); + MP_FREE(*pThread); + *pThread = 0; +} +#endif +#endif + +#if MICROPROFILE_WEBSERVER + +#ifdef _WIN32 +#define MP_INVALID_SOCKET(f) (f == INVALID_SOCKET) +#else +#include +#include +#include +#define MP_INVALID_SOCKET(f) (f < 0) +#endif + +void MicroProfileWebServerStart(); +void MicroProfileWebServerStop(); +void MicroProfileWebServerJoin(); +bool MicroProfileWebServerUpdate(); +void MicroProfileDumpToFile(); + +#else + +#define MicroProfileWebServerStart() \ + do \ + { \ + } while(0) +#define MicroProfileWebServerStop() \ + do \ + { \ + } while(0) +#define MicroProfileWebServerJoin() \ + do \ + { \ + } while(0) +#define MicroProfileWebServerUpdate() false +#define MicroProfileDumpToFile() \ + do \ + { \ + } while(0) +#endif + +#include +#include +#include +#include + +#if MICROPROFILE_DEBUG +#ifdef _WIN32 +void uprintf(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + char buffer[1024]; + stbsp_vsnprintf(buffer, sizeof(buffer) - 1, fmt, args); + OutputDebugStringA(buffer); + va_end(args); +} +#else +#define uprintf(...) printf(__VA_ARGS__) +#endif +#else +#define uprintf(...) \ + do \ + { \ + sizeof(__VA_ARGS__); \ + } while(0) +#endif + +#define S g_MicroProfile + +MicroProfile g_MicroProfile; +#ifdef MICROPROFILE_IOS +// iOS doesn't support __thread +static pthread_key_t g_MicroProfileThreadLogKey; +static pthread_once_t g_MicroProfileThreadLogKeyOnce = PTHREAD_ONCE_INIT; + +static void MicroProfileCreateThreadLogKey() +{ + pthread_key_create(&g_MicroProfileThreadLogKey, NULL); +} +#else +MP_THREAD_LOCAL MicroProfileThreadLog* g_MicroProfileThreadLogThreadLocal = 0; +#endif +static bool g_bUseLock = false; /// This is used because windows does not support using mutexes under dll init(which is where global initialization is handled) + +MICROPROFILE_DEFINE(g_MicroProfileFlip, "MicroProfile", "MicroProfileFlip", MP_GREEN4); +MICROPROFILE_DEFINE(g_MicroProfileThreadLoop, "MicroProfile", "ThreadLoop", MP_GREEN4); +MICROPROFILE_DEFINE(g_MicroProfileClear, "MicroProfile", "Clear", MP_GREEN4); +MICROPROFILE_DEFINE(g_MicroProfileAccumulate, "MicroProfile", "Accumulate", MP_GREEN4); +MICROPROFILE_DEFINE(g_MicroProfileContextSwitchSearch, "MicroProfile", "ContextSwitchSearch", MP_GREEN4); +MICROPROFILE_DEFINE(g_MicroProfileGpuSubmit, "MicroProfile", "MicroProfileGpuSubmit", MP_HOTPINK2); +MICROPROFILE_DEFINE(g_MicroProfileSendLoop, "MicroProfile", "MicroProfileSocketSendLoop", MP_GREEN4); +MICROPROFILE_DEFINE_LOCAL_ATOMIC_COUNTER(g_MicroProfileBytesPerFlip, "microprofile/bytesperflip"); + +// void MicroProfileHashTableInit(MicroProfileHashTable* pTable, uint32_t nInitialSize, MicroProfileHashCompareFunction CompareFunc, MicroProfileHashFunction HashFunc); +void MicroProfileHashTableDestroy(MicroProfileHashTable* pTable); +uint64_t MicroProfileHashTableHash(MicroProfileHashTable* pTable, uint64_t K); +void MicroProfileHashTableGrow(MicroProfileHashTable* pTable); + +bool MicroProfileHashTableSet(MicroProfileHashTable* pTable, uint64_t Key, uintptr_t Value, uint64_t H, bool bAllowGrow); +bool MicroProfileHashTableGet(MicroProfileHashTable* pTable, uint64_t Key, uintptr_t* pValue); +bool MicroProfileHashTableRemove(MicroProfileHashTable* pTable, uint64_t Key); + +bool MicroProfileHashTableSetString(MicroProfileHashTable* pTable, const char* pKey, const char* pValue); +bool MicroProfileHashTableGetString(MicroProfileHashTable* pTable, const char* pKey, const char** pValue); +bool MicroProfileHashTableRemoveString(MicroProfileHashTable* pTable, const char* pKey); + +bool MicroProfileHashTableSetPtr(MicroProfileHashTable* pTable, const void* pKey, void* pValue); +template +bool MicroProfileHashTableGetPtr(MicroProfileHashTable* pTable, const void* pKey, T** pValue = nullptr); +bool MicroProfileHashTableRemovePtr(MicroProfileHashTable* pTable, const void* pKey); + +enum +{ + ESTRINGINTERN_LOWERCASE = 1, + ESTRINGINTERN_FORCEFORWARDSLASH = 0x2, +}; +const char* MicroProfileStringIntern(const char* pStr); +const char* MicroProfileStringInternLower(const char* pStr); +const char* MicroProfileStringInternSlash(const char* pStr); +const char* MicroProfileStringIntern(const char* pStr, uint32_t nLen, uint32_t nInternalFlags = 0); + +void MicroProfileStringsInit(MicroProfileStrings* pStrings); +void MicroProfileStringsDestroy(MicroProfileStrings* pStrings); + +MicroProfileToken MicroProfileCounterTokenInit(int nParent, uint32_t nFlags); +void MicroProfileCounterTokenInitName(MicroProfileToken nToken, const char* pName); +void MicroProfileCounterConfigToken(MicroProfileToken, uint32_t eFormat, int64_t nLimit, uint32_t nFlags); +uint16_t MicroProfileFindGroup(const char* pGroup); + +inline std::recursive_mutex& MicroProfileMutex() +{ + static std::recursive_mutex Mutex; + return Mutex; +} +std::recursive_mutex& MicroProfileGetMutex() +{ + return MicroProfileMutex(); +} + +inline std::recursive_mutex& MicroProfileTimelineMutex() +{ + static std::recursive_mutex Mutex; + return Mutex; +} +MICROPROFILE_API MicroProfile* MicroProfileGet() +{ + return &g_MicroProfile; +} + +MicroProfileThreadLog* MicroProfileCreateThreadLog(const char* pName); +MicroProfileThreadLogGpu* MicroProfileThreadLogGpuAllocInternal(); +void* MicroProfileSocketSenderThread(void*); + +void MicroProfileInit() +{ + static bool bOnce = true; + if(!bOnce) + { + return; + } + + std::recursive_mutex& mutex = MicroProfileMutex(); + bool bUseLock = g_bUseLock; + if(bUseLock) + mutex.lock(); + if(bOnce) + { + bOnce = false; + memset(&S, 0, sizeof(S)); + + MicroProfileStringsInit(&S.Strings); + + // these strings are used for counter names inside the string + S.CounterToken_MicroProfile = MicroProfileCounterTokenInit(-1, 0); + S.CounterToken_StringBlock = MicroProfileCounterTokenInit(S.CounterToken_MicroProfile, 0); + S.CounterToken_StringBlock_Count = MicroProfileCounterTokenInit(S.CounterToken_StringBlock, 0); + S.CounterToken_StringBlock_Waste = MicroProfileCounterTokenInit(S.CounterToken_StringBlock, 0); + S.CounterToken_StringBlock_Strings = MicroProfileCounterTokenInit(S.CounterToken_StringBlock, 0); + S.CounterToken_StringBlock_Memory = MicroProfileCounterTokenInit(S.CounterToken_StringBlock, 0); + + S.CounterToken_Alloc = MicroProfileCounterTokenInit(S.CounterToken_MicroProfile, 0); + S.CounterToken_Alloc_Memory = MicroProfileCounterTokenInit(S.CounterToken_Alloc, 0); + S.CounterToken_Alloc_Count = MicroProfileCounterTokenInit(S.CounterToken_Alloc, 0); + + MicroProfileCounterTokenInitName(S.CounterToken_MicroProfile, "microprofile"); + MicroProfileCounterTokenInitName(S.CounterToken_StringBlock, "stringblock"); + MicroProfileCounterTokenInitName(S.CounterToken_StringBlock_Count, "count"); + MicroProfileCounterTokenInitName(S.CounterToken_StringBlock_Waste, "waste"); + MicroProfileCounterTokenInitName(S.CounterToken_StringBlock_Strings, "strings"); + MicroProfileCounterTokenInitName(S.CounterToken_StringBlock_Memory, "memory"); + + MicroProfileCounterTokenInitName(S.CounterToken_Alloc, "alloc"); + MicroProfileCounterTokenInitName(S.CounterToken_Alloc_Memory, "memory"); + MicroProfileCounterTokenInitName(S.CounterToken_Alloc_Count, "count"); + + S.nMemUsage += sizeof(S); + for(int i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + { + S.GroupInfo[i].pName[0] = '\0'; + } + for(int i = 0; i < MICROPROFILE_MAX_CATEGORIES; ++i) + { + S.CategoryInfo[i].pName[0] = '\0'; + memset(S.CategoryInfo[i].nGroupMask, 0, sizeof(S.CategoryInfo[i].nGroupMask)); + } + memcpy(&S.CategoryInfo[0].pName[0], "default", sizeof("default")); + S.nCategoryCount = 1; + for(int i = 0; i < MICROPROFILE_MAX_TIMERS; ++i) + { + S.TimerInfo[i].pName[0] = '\0'; + } + S.nGroupCount = 0; + S.nFlipStartTick = MP_TICK(); + S.nContextSwitchStalledTick = MP_TICK(); + S.nAggregateFlipTick = MP_TICK(); + memset(S.nActiveGroups, 0, sizeof(S.nActiveGroups)); + S.nFrozen = 0; + S.nWasFrozen = 0; + memset(S.nForceGroups, 0, sizeof(S.nForceGroups)); + memset(S.nActiveGroupsWanted, 0, sizeof(S.nActiveGroupsWanted)); + S.nStartEnabled = 0; + S.nAllThreadsWanted = 1; + S.nAggregateFlip = 0; + S.nTotalTimers = 0; + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + { + S.Graph[i].nToken = MICROPROFILE_INVALID_TOKEN; + } + S.fReferenceTime = 33.33f; + S.fRcpReferenceTime = 1.f / S.fReferenceTime; + S.nFreeListHead = -1; + int64_t nTick = MP_TICK(); + for(int i = 0; i < MICROPROFILE_MAX_FRAME_HISTORY; ++i) + { + S.Frames[i].nFrameStartCpu = nTick; + S.Frames[i].nFrameStartGpu = MICROPROFILE_INVALID_TICK; + } + S.nWebServerPort = MICROPROFILE_WEBSERVER_PORT; // Use defined value as default port + S.nWebServerDataSent = (uint64_t)-1; + S.WebSocketTimers = -1; + S.WebSocketCounters = -1; + S.WebSocketGroups = -1; + S.nSocketFail = 0; + + S.DumpFrameCount = MICROPROFILE_WEBSERVER_DEFAULT_FRAMES; + +#if MICROPROFILE_COUNTER_HISTORY + S.nCounterHistoryPut = 0; + for(uint32_t i = 0; i < MICROPROFILE_MAX_COUNTERS; ++i) + { + S.nCounterMin[i] = 0x7fffffffffffffff; + S.nCounterMax[i] = 0x8000000000000000; + } +#endif + S.GpuQueue = MICROPROFILE_GPU_INIT_QUEUE("GPU"); + S.pGpuGlobal = MicroProfileThreadLogGpuAllocInternal(); + MicroProfileGpuBegin(0, S.pGpuGlobal); + + S.pJsonSettings = 0; + S.pJsonSettingsName = nullptr; + S.nJsonSettingsPending = 0; + S.nJsonSettingsBufferSize = 0; + S.nWSWasConnected = 0; + + for(uint32_t i = 0; i < MICROPROFILE_TIMELINE_MAX_TOKENS; ++i) + { + S.TimelineTokenFrameEnter[i] = MICROPROFILE_INVALID_FRAME; + S.TimelineTokenFrameLeave[i] = MICROPROFILE_INVALID_FRAME; + S.TimelineTokenStaticString[i] = nullptr; + S.TimelineToken[i] = 0; + } + memset(&S.AccumMinTimers[0], 0xFF, sizeof(S.AccumMinTimers)); + S.CountersDouble = (std::atomic*)&S.Counters; +#if MICROPROFILE_COUNTER_HISTORY + S.dCounterHistory = (double*)S.nCounterHistory; + S.dCounterMax = (double*)S.nCounterMax; + S.dCounterMin = (double*)S.nCounterMin; +#endif + } + MicroProfileUpdateSettingsPath(); + +#if MICROPROFILE_FRAME_EXTRA_DATA + S.FrameExtraCounterData = (MicroProfileFrameExtraCounterData*)1; +#endif + MicroProfileCounterConfigToken(S.CounterToken_Alloc_Memory, MICROPROFILE_COUNTER_FORMAT_BYTES, 0, MICROPROFILE_COUNTER_FLAG_DETAILED); + MICROPROFILE_COUNTER_CONFIG("MicroProfile/ThreadLog/Memory", MICROPROFILE_COUNTER_FORMAT_BYTES, 0, MICROPROFILE_COUNTER_FLAG_DETAILED); + + if(bUseLock) + { + mutex.unlock(); + } +} +void MicroProfileUpdateSettingsPath() +{ + if(S.pSettings) + { + MicroProfileFreeInternal((void*)S.pSettings); + S.pSettings = nullptr; + } + if(S.pSettingsReadOnly) + { + MicroProfileFreeInternal((void*)S.pSettingsReadOnly); + S.pSettingsReadOnly = nullptr; + } + if(S.pSettingsTemp) + { + MicroProfileFreeInternal((void*)S.pSettingsTemp); + S.pSettingsTemp = nullptr; + } + auto DupeString = [](const char* BasePath, const char* File) -> const char* + { + size_t BaseLen = strlen(BasePath); + bool TrailingSlash = BaseLen > 1 && (BasePath[BaseLen - 1] == '\\' || BasePath[BaseLen - 1] == '/'); + size_t Len = BaseLen + strlen(File) + 2; + char* Data = (char*)MicroProfileAllocInternal(Len + 1, 1); +#ifdef _WIN32 + char Slash = '\\'; +#else + char Slash = '/'; +#endif + if(TrailingSlash) + snprintf(Data, Len, "%s%s", BasePath, File); + else + snprintf(Data, Len, "%s%c%s", BasePath, Slash, File); + + return Data; + }; + const char* pBaseSettingsPath = MICROPROFILE_GET_SETTINGS_FILE_PATH; + S.pSettings = DupeString(pBaseSettingsPath, MICROPROFILE_SETTINGS_FILE); + S.pSettingsReadOnly = DupeString(pBaseSettingsPath, MICROPROFILE_SETTINGS_FILE_BUILTIN); + S.pSettingsTemp = DupeString(pBaseSettingsPath, MICROPROFILE_SETTINGS_FILE MICROPROFILE_SETTINGS_FILE_TEMP); +} + +void MicroProfileJoinContextSwitchTrace(); + +void MicroProfileShutdown() +{ + { + std::lock_guard Lock(MicroProfileMutex()); + S.nMicroProfileShutdown = 1; + MicroProfileStopContextSwitchTrace(); + } + MicroProfileWebServerJoin(); + MicroProfileJoinContextSwitchTrace(); + { + + std::lock_guard Lock(MicroProfileMutex()); + if(S.pJsonSettings) + { + MP_FREE(S.pJsonSettings); + S.pJsonSettings = 0; + S.pJsonSettingsName = 0; + S.nJsonSettingsBufferSize = 0; + } + if(S.pGPU) + { + MicroProfileGpuShutdownPlatform(); + } + MicroProfileHashTableDestroy(&S.Strings.HashTable); + MicroProfileStringsDestroy(&S.Strings); + MICROPROFILE_FREE_NON_ALIGNED(S.WSBuf.pBufferAllocation); + + MicroProfileFreeGpuQueue(S.GpuQueue); + MicroProfileThreadLogGpuFree(S.pGpuGlobal); + + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { +#if MICROPROFILE_ASSERT_LOG_FREED + MP_ASSERT(S.Pool[i]->nActive != 1); +#endif + MP_FREE(S.Pool[i]); + } + + for(uint32_t i = 0; i < S.nNumLogsGpu; ++i) + { +#if MICROPROFILE_ASSERT_LOG_FREED + MP_ASSERT(!S.PoolGpu[i]->nAllocated); +#endif + MP_FREE(S.PoolGpu[i]); + } + MicroProfileFreeInternal((void*)S.pSettings); + S.pSettings = nullptr; + MicroProfileFreeInternal((void*)S.pSettingsReadOnly); + S.pSettingsReadOnly = nullptr; + MicroProfileFreeInternal((void*)S.pSettingsTemp); + S.pSettingsTemp = nullptr; + } +} + +static void* MicroProfileAutoFlipThread(void*) +{ + MicroProfileOnThreadCreate("AutoFlipThread"); + while(0 == S.nAutoFlipStop.load()) + { + MICROPROFILE_SCOPEI("MICROPROFILE", "AutoFlipThread", 0); + MicroProfileSleep(S.nAutoFlipDelay); + MicroProfileFlip(0); + } + MicroProfileOnThreadExit(); + return 0; +} + +void MicroProfileStartAutoFlip(uint32_t nMsDelay) +{ + S.nAutoFlipDelay = nMsDelay; + S.nAutoFlipStop.store(0); + MicroProfileThreadStart(&S.AutoFlipThread, MicroProfileAutoFlipThread); +} +void MicroProfileStopAutoFlip() +{ + S.nAutoFlipStop.store(1); + MicroProfileThreadJoin(&S.AutoFlipThread); +} + +void MicroProfileEnableFrameExtraCounterData() +{ + // should not be called at the same time as MicroProfileFlip. + if(!S.FrameExtraCounterData) + { + S.FrameExtraCounterData = (MicroProfileFrameExtraCounterData*)1; + } +} + +void MicroProfileCsvConfigEnd() +{ + MP_ASSERT(S.CsvConfig.State == MicroProfileCsvConfig::CONFIG); + S.CsvConfig.State = MicroProfileCsvConfig::ACTIVE; +} +void MicroProfileCsvConfigBegin(uint32_t MaxTimers, uint32_t MaxGroups, uint32_t MaxCounters, uint32_t Flags) +{ + MP_ASSERT(S.CsvConfig.State == MicroProfileCsvConfig::INACTIVE); // right now, only support being configured once. + uint32_t TotalElements = MaxTimers + MaxGroups + MaxCounters; + uint32_t BaseSize = (sizeof(MicroProfileCsvConfig) + 7) & 7; + uint32_t TimerIndexSize = sizeof(uint16_t) * MaxTimers; + uint32_t GroupIndexSize = sizeof(uint16_t) * MaxGroups; + uint32_t CounterIndexSize = sizeof(uint16_t) * MaxCounters; + uint32_t FrameBlockSize = TotalElements * sizeof(uint64_t); + uint32_t FrameDataSize = FrameBlockSize * MICROPROFILE_MAX_FRAME_HISTORY; + S.CsvConfig.NumTimers = 0; + S.CsvConfig.NumGroups = 0; + S.CsvConfig.NumCounters = 0; + S.CsvConfig.MaxTimers = MaxTimers; + S.CsvConfig.MaxGroups = MaxGroups; + S.CsvConfig.MaxCounters = MaxCounters; + S.CsvConfig.TotalElements = TotalElements; + S.CsvConfig.TimerIndices = (uint16_t*)MicroProfileAllocInternal(TimerIndexSize, alignof(uint16_t)); + S.CsvConfig.pTimerNames = (const char**)MicroProfileAllocInternal(MaxTimers * sizeof(const char*), alignof(const char*)); + memset(S.CsvConfig.pTimerNames, 0, MaxTimers * sizeof(const char*)); + for(uint32_t i = 0; i < MaxTimers; ++i) + S.CsvConfig.TimerIndices[i] = UINT16_MAX; + S.CsvConfig.pGroupNames = (const char**)MicroProfileAllocInternal(MaxGroups * sizeof(const char*), alignof(const char*)); + memset(S.CsvConfig.pGroupNames, 0, MaxGroups * sizeof(const char*)); + S.CsvConfig.GroupIndices = (uint16_t*)MicroProfileAllocInternal(GroupIndexSize, alignof(uint16_t)); + for(uint32_t i = 0; i < MaxGroups; ++i) + S.CsvConfig.GroupIndices[i] = UINT16_MAX; + S.CsvConfig.pCounterNames = (const char**)MicroProfileAllocInternal(MaxCounters * sizeof(const char*), alignof(const char*)); + memset(S.CsvConfig.pCounterNames, 0, MaxCounters * sizeof(const char*)); + S.CsvConfig.CounterIndices = (uint16_t*)MicroProfileAllocInternal(CounterIndexSize, alignof(uint16_t)); + for(uint32_t i = 0; i < MaxCounters; ++i) + S.CsvConfig.CounterIndices[i] = UINT16_MAX; + S.CsvConfig.FrameData = (uint64_t*)MicroProfileAllocInternal(FrameDataSize, alignof(uint64_t)); + memset(S.CsvConfig.FrameData, 0, FrameDataSize); + S.CsvConfig.State = MicroProfileCsvConfig::CONFIG; + S.CsvConfig.Flags = Flags; +} +void MicroProfileCsvConfigAddTimer(const char* Group, const char* Timer, const char* Name, MicroProfileTokenType Type) +{ + MP_ASSERT(S.CsvConfig.State == MicroProfileCsvConfig::CONFIG); + if(S.CsvConfig.State == MicroProfileCsvConfig::CONFIG && S.CsvConfig.NumTimers < S.CsvConfig.MaxTimers) + { + MicroProfileToken ret = MicroProfileGetToken(Group, Timer, MP_AUTO, Type, MICROPROFILE_TIMER_FLAG_PLACEHOLDER); + if(ret != MICROPROFILE_INVALID_TOKEN) + { + MP_ASSERT(S.CsvConfig.NumTimers < S.CsvConfig.MaxTimers); + uint16_t TimerIndex = MicroProfileGetTimerIndex(ret); + for(uint32_t i = 0; i < S.CsvConfig.NumTimers; ++i) + { + if(S.CsvConfig.TimerIndices[i] == TimerIndex) + return; + } + S.CsvConfig.pTimerNames[S.CsvConfig.NumTimers] = Name; + S.CsvConfig.TimerIndices[S.CsvConfig.NumTimers++] = TimerIndex; + } + } +} +void MicroProfileCsvConfigAddGroup(const char* Group, const char* Name) +{ + MP_ASSERT(S.CsvConfig.State == MicroProfileCsvConfig::CONFIG); + if(S.CsvConfig.State == MicroProfileCsvConfig::CONFIG && S.CsvConfig.NumGroups < S.CsvConfig.MaxGroups) + { + uint16_t Index = MicroProfileFindGroup(Group); + MP_ASSERT(UINT16_MAX != Index); + if(UINT16_MAX != Index) + { + MP_ASSERT(S.CsvConfig.NumGroups < S.CsvConfig.MaxGroups); + for(uint32_t i = 0; i < S.CsvConfig.NumGroups; ++i) + { + if(S.CsvConfig.GroupIndices[i] == Index) + return; + } + S.CsvConfig.pGroupNames[S.CsvConfig.NumGroups] = Name; + S.CsvConfig.GroupIndices[S.CsvConfig.NumGroups++] = Index; + } + } +} +void MicroProfileCsvConfigAddCounter(const char* CounterName, const char* Name) +{ + MP_ASSERT(S.CsvConfig.State == MicroProfileCsvConfig::CONFIG); + if(S.CsvConfig.State == MicroProfileCsvConfig::CONFIG && S.CsvConfig.NumCounters < S.CsvConfig.MaxCounters) + { + MicroProfileToken Token = MicroProfileGetCounterToken(CounterName, 0); + if(MICROPROFILE_INVALID_TOKEN != Token) + { + MP_ASSERT(Token < UINT16_MAX); + MP_ASSERT(S.CsvConfig.NumCounters < S.CsvConfig.MaxCounters); + for(uint32_t i = 0; i < S.CsvConfig.NumCounters; ++i) + { + if(S.CsvConfig.CounterIndices[i] == (uint16_t)Token) + return; + } + S.CsvConfig.pCounterNames[S.CsvConfig.NumCounters] = Name; + S.CsvConfig.CounterIndices[S.CsvConfig.NumCounters++] = (uint16_t)Token; + } + } +} + +#ifdef MICROPROFILE_IOS +inline MicroProfileThreadLog* MicroProfileGetThreadLog() +{ + pthread_once(&g_MicroProfileThreadLogKeyOnce, MicroProfileCreateThreadLogKey); + return (MicroProfileThreadLog*)pthread_getspecific(g_MicroProfileThreadLogKey); +} + +inline void MicroProfileSetThreadLog(MicroProfileThreadLog* pLog) +{ + pthread_once(&g_MicroProfileThreadLogKeyOnce, MicroProfileCreateThreadLogKey); + pthread_setspecific(g_MicroProfileThreadLogKey, pLog); +} +#else +MicroProfileThreadLog* MicroProfileGetThreadLog() +{ + return g_MicroProfileThreadLogThreadLocal; +} +void MicroProfileSetThreadLog(MicroProfileThreadLog* pLog) +{ + g_MicroProfileThreadLogThreadLocal = pLog; +} +#endif + +MicroProfileThreadLog* MicroProfileGetThreadLog2() +{ + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog(); + if(!pLog) + { + MicroProfileInitThreadLog(); + pLog = MicroProfileGetThreadLog(); + } + return pLog; +} + +struct MicroProfileScopeLock +{ + bool bUseLock; + int nUnlock; + std::recursive_mutex& m; + MicroProfileScopeLock(std::recursive_mutex& m) + : bUseLock(g_bUseLock) + , nUnlock(0) + , m(m) + { + if(bUseLock) + m.lock(); + } + ~MicroProfileScopeLock() + { + MP_ASSERT(nUnlock == 0); + if(bUseLock) + m.unlock(); + } + void Unlock() + { + MP_ASSERT(bUseLock); + m.unlock(); + nUnlock++; + } + void Lock() + { + m.lock(); + nUnlock--; + } +}; + +void MicroProfileLogReset(MicroProfileThreadLog* pLog); +void MicroProfileLogClearInternal(MicroProfileThreadLog* pLog); + +MicroProfileThreadLog* MicroProfileCreateThreadLog(const char* pName) +{ + MicroProfileScopeLock L(MicroProfileMutex()); + + if(S.nNumLogs == MICROPROFILE_MAX_THREADS && S.nFreeListHead == -1) + { + uprintf("recycling thread logs\n"); + // reuse the oldest. + MicroProfileThreadLog* pOldest = 0; + uint32_t nIdleFrames = 0; + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + MicroProfileThreadLog* pLog = S.Pool[i]; + uprintf("tlactive %p, %d. idle:%d\n", pLog, pLog->nActive, pLog->nIdleFrames); + if(pLog->nActive == 2) + { + if(pLog->nIdleFrames >= nIdleFrames) + { + nIdleFrames = pLog->nIdleFrames; + pOldest = pLog; + } + } + } + MP_ASSERT(pOldest); + MicroProfileLogReset(pOldest); + } + + MicroProfileThreadLog* pLog = 0; + if(S.nFreeListHead != -1) + { + pLog = S.Pool[S.nFreeListHead]; + MP_ASSERT(pLog->nPut.load() == 0); + MP_ASSERT(pLog->nGet.load() == 0); + S.nFreeListHead = S.Pool[S.nFreeListHead]->nFreeListNext; + } + else + { + MICROPROFILE_COUNTER_ADD("MicroProfile/ThreadLog/Allocated", 1); + MICROPROFILE_COUNTER_ADD("MicroProfile/ThreadLog/Memory", sizeof(MicroProfileThreadLog)); + pLog = MP_ALLOC_OBJECT(MicroProfileThreadLog); + MicroProfileLogClearInternal(pLog); + S.nMemUsage += sizeof(MicroProfileThreadLog); + pLog->nLogIndex = S.nNumLogs; + MP_ASSERT(S.nNumLogs < MICROPROFILE_MAX_THREADS); + S.Pool[S.nNumLogs++] = pLog; + } + int len = 0; + if(pName) + { + len = (int)strlen(pName); + int maxlen = sizeof(pLog->ThreadName) - 1; + len = len < maxlen ? len : maxlen; + memcpy(&pLog->ThreadName[0], pName, len); + } + else + { + len = snprintf(&pLog->ThreadName[0], sizeof(pLog->ThreadName) - 1, "TID:[%" PRId64 "]", (int64_t)MP_GETCURRENTTHREADID()); + } + pLog->ThreadName[len] = '\0'; + pLog->nThreadId = MP_GETCURRENTTHREADID(); + pLog->nFreeListNext = -1; + pLog->nActive = 1; + return pLog; +} + +void MicroProfileOnThreadCreate(const char* pThreadName) +{ + char Buffer[64]; + g_bUseLock = true; + MicroProfileInit(); + MP_ASSERT(MicroProfileGetThreadLog() == 0); + MicroProfileThreadLog* pLog = MicroProfileCreateThreadLog(pThreadName ? pThreadName : MicroProfileGetThreadName(Buffer)); + (void)Buffer; + MP_ASSERT(pLog); + MicroProfileSetThreadLog(pLog); +} + +void MicroProfileThreadLogGpuReset(MicroProfileThreadLogGpu* pLog) +{ + MP_ASSERT(pLog->nAllocated); + pLog->pContext = (void*)-1; + pLog->nStart = (uint32_t)-1; + pLog->nPut = 0; + pLog->nStackScope = 0; +} + +MicroProfileThreadLogGpu* MicroProfileThreadLogGpuAllocInternal() +{ + MicroProfileThreadLogGpu* pLog = 0; + for(uint32_t i = 0; i < S.nNumLogsGpu; ++i) + { + MicroProfileThreadLogGpu* pNextLog = S.PoolGpu[i]; + if(pNextLog && !pNextLog->nAllocated) + { + pLog = pNextLog; + break; + } + } + if(!pLog) + { + pLog = MP_ALLOC_OBJECT(MicroProfileThreadLogGpu); + int nLogIndex = S.nNumLogsGpu++; + MP_ASSERT(nLogIndex < MICROPROFILE_MAX_THREADS); + pLog->nId = nLogIndex; + S.PoolGpu[nLogIndex] = pLog; + } + pLog->nAllocated = 1; + MicroProfileThreadLogGpuReset(pLog); + return pLog; +} + +MicroProfileThreadLogGpu* MicroProfileThreadLogGpuAlloc() +{ + std::lock_guard Lock(MicroProfileMutex()); + return MicroProfileThreadLogGpuAllocInternal(); +} + +void MicroProfileThreadLogGpuFree(MicroProfileThreadLogGpu* pLog) +{ + MP_ASSERT(pLog->nAllocated); + pLog->nAllocated = 0; +} + +int MicroProfileGetGpuQueue(const char* pQueueName) +{ + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; i++) + { + MicroProfileThreadLog* pLog = S.Pool[i]; + if(pLog && pLog->nGpu && pLog->nActive && 0 == MP_STRCASECMP(pQueueName, pLog->ThreadName)) + { + return i; + } + } + MP_ASSERT(0); // call MicroProfileInitGpuQueue + return 0; +} + +MicroProfileThreadLog* MicroProfileGetGpuQueueLog(const char* pQueueName) +{ + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; i++) + { + MicroProfileThreadLog* pLog = S.Pool[i]; + if(pLog && pLog->nGpu && pLog->nActive && 0 == MP_STRCASECMP(pQueueName, pLog->ThreadName)) + { + return pLog; + } + } + MP_ASSERT(0); // call MicroProfileInitGpuQueue + return 0; +} + +int MicroProfileInitGpuQueue(const char* pQueueName) +{ + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + MicroProfileThreadLog* pLog = S.Pool[i]; + if(pLog && 0 == MP_STRCASECMP(pQueueName, pLog->ThreadName)) + { + + MP_ASSERT(0); // call MicroProfileInitGpuQueue only once per CommandQueue. name must not clash with threadname + } + } + MicroProfileThreadLog* pLog = MicroProfileCreateThreadLog(pQueueName); + pLog->nGpu = 1; + pLog->nThreadId = 0; + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + if(S.Pool[i] == pLog) + { + return i; + } + } + MP_BREAK(); + return 0; +} + +void MicroProfileFreeGpuQueue(int nQueue) +{ + MicroProfileThreadLog* pLog = S.Pool[nQueue]; + if(pLog) + { + MP_ASSERT(pLog->nActive == 1); + pLog->nActive = 2; + } +} + +MicroProfileThreadLogGpu* MicroProfileGetGlobalGpuThreadLog() +{ + return S.pGpuGlobal; +} + +MICROPROFILE_API int MicroProfileGetGlobalGpuQueue() +{ + return S.GpuQueue; +} +void MicroProfileLogClearInternal(MicroProfileThreadLog* pLog) +{ + // can't clear atomics.. + void* pStart = (void*)&pLog->Log[0]; + void* pEnd = (void*)(pLog + 1); + memset(pStart, 0, (uintptr_t)pEnd - (uintptr_t)pStart); + pLog->nPut.store(0); + pLog->nGet.store(0); +} +void MicroProfileLogReset(MicroProfileThreadLog* pLog) +{ + std::lock_guard Lock(MicroProfileMutex()); + + int32_t nLogIndex = -1; + for(int i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + if(pLog == S.Pool[i]) + { + nLogIndex = i; + break; + } + } + MP_ASSERT(nLogIndex < MICROPROFILE_MAX_THREADS && nLogIndex > 0); + MicroProfileLogClearInternal(pLog); + pLog->nFreeListNext = S.nFreeListHead; + S.nFreeListHead = nLogIndex; + for(int i = 0; i < MICROPROFILE_MAX_FRAME_HISTORY; ++i) + { + S.Frames[i].nLogStart[nLogIndex] = 0; + } +} + +void MicroProfileOnThreadExit() +{ + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog(); + if(pLog) + { + MP_ASSERT(pLog->nActive == 1); + pLog->nActive = 2; + } +} + +void MicroProfileInitThreadLog() +{ + MicroProfileOnThreadCreate(nullptr); +} + +MicroProfileToken MicroProfileFindTokenInternal(const char* pGroup, const char* pName) +{ + MicroProfileInit(); + MicroProfileScopeLock L(MicroProfileMutex()); + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + if(!MP_STRCASECMP(pName, S.TimerInfo[i].pName) && !MP_STRCASECMP(pGroup, S.GroupInfo[S.TimerToGroup[i]].pName)) + { + return S.TimerInfo[i].nToken; + } + } + return MICROPROFILE_INVALID_TOKEN; +} +MicroProfileToken MicroProfileFindToken(const char* pGroup, const char* pName) +{ + return MicroProfileGetToken(pGroup, pName, MP_AUTO, MicroProfileTokenTypeCpu, MICROPROFILE_TIMER_FLAG_PLACEHOLDER); +} + +uint16_t MicroProfileFindGroup(const char* pGroup) +{ + for(uint32_t i = 0; i < S.nGroupCount; ++i) + { + if(!MP_STRCASECMP(pGroup, S.GroupInfo[i].pName)) + { + return i; + } + } + return UINT16_MAX; +} + +uint16_t MicroProfileGetGroup(const char* pGroup, MicroProfileTokenType Type) +{ + for(uint32_t i = 0; i < S.nGroupCount; ++i) + { + if(!MP_STRCASECMP(pGroup, S.GroupInfo[i].pName)) + { + return i; + } + } + uint16_t nGroupIndex = 0xffff; + uint32_t nLen = (uint32_t)strlen(pGroup); + if(nLen > MICROPROFILE_NAME_MAX_LEN - 1) + nLen = MICROPROFILE_NAME_MAX_LEN - 1; + memcpy(&S.GroupInfo[S.nGroupCount].pName[0], pGroup, nLen); + S.GroupInfo[S.nGroupCount].pName[nLen] = '\0'; + S.GroupInfo[S.nGroupCount].nNameLen = nLen; + S.GroupInfo[S.nGroupCount].nNumTimers = 0; + S.GroupInfo[S.nGroupCount].nGroupIndex = S.nGroupCount; + S.GroupInfo[S.nGroupCount].Type = Type; + S.GroupInfo[S.nGroupCount].nMaxTimerNameLen = 0; + S.GroupInfo[S.nGroupCount].nColor = 0x42; + S.GroupInfo[S.nGroupCount].nCategory = 0; + S.GroupInfo[S.nGroupCount].nWSNext = -2; + + uint32_t nIndex = S.nGroupCount / 32; + uint32_t nBit = S.nGroupCount % 32; + { + S.CategoryInfo[0].nGroupMask[nIndex] |= (1 << nBit); + } + if(S.nStartEnabled) + { + S.nActiveGroupsWanted[nIndex] |= (1ll << nBit); + S.nActiveGroups[nIndex] |= (1ll << nBit); + S.AnyActive = true; + } + nGroupIndex = S.nGroupCount++; + S.nGroupMask[nIndex] |= (1 << nBit); + MP_ASSERT(S.nGroupCount < MICROPROFILE_MAX_GROUPS); + return nGroupIndex; +} + +void MicroProfileRegisterGroup(const char* pGroup, const char* pCategory, uint32_t nColor) +{ + MicroProfileScopeLock L(MicroProfileMutex()); + + int nCategoryIndex = -1; + for(uint32_t i = 0; i < S.nCategoryCount; ++i) + { + if(!MP_STRCASECMP(pCategory, S.CategoryInfo[i].pName)) + { + nCategoryIndex = (int)i; + break; + } + } + if(-1 == nCategoryIndex && S.nCategoryCount < MICROPROFILE_MAX_CATEGORIES) + { + MP_ASSERT(S.CategoryInfo[S.nCategoryCount].pName[0] == '\0'); + nCategoryIndex = (int)S.nCategoryCount++; + uint32_t nLen = (uint32_t)strlen(pCategory); + if(nLen > MICROPROFILE_NAME_MAX_LEN - 1) + nLen = MICROPROFILE_NAME_MAX_LEN - 1; + memcpy(&S.CategoryInfo[nCategoryIndex].pName[0], pCategory, nLen); + S.CategoryInfo[nCategoryIndex].pName[nLen] = '\0'; + } + uint16_t nGroup = MicroProfileGetGroup(pGroup, 0 != MP_STRCASECMP(pGroup, "gpu") ? MicroProfileTokenTypeCpu : MicroProfileTokenTypeGpu); + S.GroupInfo[nGroup].nColor = nColor; + if(nCategoryIndex >= 0) + { + uint32_t nIndex = nGroup / 32; + uint32_t nBit = nGroup % 32; + nBit = (1 << nBit); + uint32_t nOldCategory = S.GroupInfo[nGroup].nCategory; + S.CategoryInfo[nOldCategory].nGroupMask[nIndex] &= ~nBit; + S.CategoryInfo[nCategoryIndex].nGroupMask[nIndex] |= nBit; + S.GroupInfo[nGroup].nCategory = nCategoryIndex; + } +} + +MicroProfileToken MicroProfileGetToken(const char* pGroup, const char* pName, uint32_t nColor, MicroProfileTokenType Type, uint32_t Flags) +{ + MicroProfileInit(); + MicroProfileScopeLock L(MicroProfileMutex()); + MicroProfileToken ret = MicroProfileFindTokenInternal(pGroup, pName); + if(ret != MICROPROFILE_INVALID_TOKEN) + { + int idx = MicroProfileGetTimerIndex(ret); + if(S.TimerInfo[idx].Flags & MICROPROFILE_TIMER_FLAG_PLACEHOLDER) + { + S.TimerInfo[idx].nColor = nColor & 0xffffff; + S.TimerInfo[idx].Flags = Flags; + S.TimerInfo[idx].Type = Type; + } + MP_ASSERT(S.TimerInfo[idx].Flags == Flags || (Flags & MICROPROFILE_TIMER_FLAG_PLACEHOLDER)); + return ret; + } + uint16_t nGroupIndex = MicroProfileGetGroup(pGroup, Type); + uint16_t nTimerIndex = (uint16_t)(S.nTotalTimers++); + MP_ASSERT(nTimerIndex < MICROPROFILE_MAX_TIMERS); + + uint32_t nBitIndex = nGroupIndex / 32; + uint32_t nBit = nGroupIndex % 32; + uint32_t nGroupMask = 1ll << nBit; + MicroProfileToken nToken = MicroProfileMakeToken(nGroupMask, (uint16_t)nBitIndex, nTimerIndex); + S.GroupInfo[nGroupIndex].nNumTimers++; + S.GroupInfo[nGroupIndex].nMaxTimerNameLen = MicroProfileMax(S.GroupInfo[nGroupIndex].nMaxTimerNameLen, (uint32_t)strlen(pName)); + MP_ASSERT(S.GroupInfo[nGroupIndex].Type == Type); // dont mix cpu & gpu timers in the same group + S.nMaxGroupSize = MicroProfileMax(S.nMaxGroupSize, S.GroupInfo[nGroupIndex].nNumTimers); + S.TimerInfo[nTimerIndex].nToken = nToken; + uint32_t nLen = (uint32_t)strlen(pName); + if(nLen > MICROPROFILE_NAME_MAX_LEN - 1) + nLen = MICROPROFILE_NAME_MAX_LEN - 1; + memcpy(&S.TimerInfo[nTimerIndex].pName, pName, nLen); + snprintf(&S.TimerInfo[nTimerIndex].pNameExt[0], sizeof(S.TimerInfo[nTimerIndex].pNameExt) - 1, "%s %s", S.GroupInfo[nGroupIndex].pName, pName); + S.TimerInfo[nTimerIndex].pName[nLen] = '\0'; + S.TimerInfo[nTimerIndex].nNameLen = nLen; + S.TimerInfo[nTimerIndex].nColor = nColor & 0xffffff; + S.TimerInfo[nTimerIndex].nGroupIndex = nGroupIndex; + S.TimerInfo[nTimerIndex].nTimerIndex = nTimerIndex; + S.TimerInfo[nTimerIndex].nWSNext = -2; + S.TimerInfo[nTimerIndex].Type = Type; + S.TimerInfo[nTimerIndex].Flags = Flags; + // printf("*** TOKEN %08d %s\\%s .. flags %08x\n", nTimerIndex, pGroup, pName, Flags); + S.TimerToGroup[nTimerIndex] = nGroupIndex; + return nToken; +} + +void MicroProfileGetTokenC(MicroProfileToken* pToken, const char* pGroup, const char* pName, uint32_t nColor, MicroProfileTokenType Type, uint32_t flags) +{ + if(*pToken == MICROPROFILE_INVALID_TOKEN) + { + MicroProfileInit(); + MicroProfileScopeLock L(MicroProfileMutex()); + if(*pToken == MICROPROFILE_INVALID_TOKEN) + { + *pToken = MicroProfileGetToken(pGroup, pName, nColor, Type, flags); + } + } +} + +const char* MicroProfileNextName(const char* pName, char* pNameOut, uint32_t* nSubNameLen) +{ + int nMaxLen = MICROPROFILE_NAME_MAX_LEN - 1; + const char* pRet = 0; + bool bDone = false; + uint32_t nChars = 0; + for(int i = 0; i < nMaxLen && !bDone; ++i) + { + char c = *pName++; + switch(c) + { + case 0: + bDone = true; + break; + case '\\': + case '/': + if(nChars) + { + bDone = true; + pRet = pName; + } + break; + default: + nChars++; + *pNameOut++ = c; + } + } + *nSubNameLen = nChars; + *pNameOut = '\0'; + return pRet; +} + +const char* MicroProfileCounterFullName(int nCounter) +{ + static char Buffer[1024]; + int nNodes[32]; + int nIndex = 0; + do + { + nNodes[nIndex++] = nCounter; + nCounter = S.CounterInfo[nCounter].nParent; + } while(nCounter >= 0); + int nOffset = 0; + while(nIndex >= 0 && nOffset < (int)sizeof(Buffer) - 2) + { + uint32_t nLen = S.CounterInfo[nNodes[nIndex]].nNameLen + nOffset; // < sizeof(Buffer)-1 + nLen = MicroProfileMin((uint32_t)(sizeof(Buffer) - 2 - nOffset), nLen); + memcpy(&Buffer[nOffset], S.CounterInfo[nNodes[nIndex]].pName, nLen); + + nOffset += S.CounterInfo[nNodes[nIndex]].nNameLen + 1; + if(nIndex) + { + Buffer[nOffset++] = '/'; + } + nIndex--; + } + return &Buffer[0]; +} + +MicroProfileToken MicroProfileCounterTokenInit(int nParent, uint32_t nFlags) +{ + MP_ASSERT(0 == (nFlags & (~MICROPROFILE_COUNTER_FLAG_TYPE_MASK))); + MicroProfileToken nResult = S.nNumCounters++; + S.CounterInfo[nResult].nParent = nParent; + S.CounterInfo[nResult].nSibling = -1; + S.CounterInfo[nResult].nFirstChild = -1; + S.CounterInfo[nResult].nFlags = nFlags; + S.CounterInfo[nResult].eFormat = MICROPROFILE_COUNTER_FORMAT_DEFAULT; + S.CounterInfo[nResult].nLimit = 0; + S.CounterInfo[nResult].ExternalAtomic = 0; + S.CounterSource[nResult].pSource = 0; + S.CounterSource[nResult].nSourceSize = 0; + S.CounterInfo[nResult].nNameLen = 0; + S.CounterInfo[nResult].pName = nullptr; + S.CounterInfo[nResult].nWSNext = -2; + if(nParent >= 0) + { + MP_ASSERT(nParent < (int)S.nNumCounters); + S.CounterInfo[nResult].nSibling = S.CounterInfo[nParent].nFirstChild; + S.CounterInfo[nResult].nLevel = S.CounterInfo[nParent].nLevel + 1; + S.CounterInfo[nParent].nFirstChild = nResult; + } + else + { + S.CounterInfo[nResult].nLevel = 0; + } + return nResult; +} +void MicroProfileCounterTokenInitName(MicroProfileToken nToken, const char* pName) +{ + MP_ASSERT(0 == S.CounterInfo[nToken].pName); + S.CounterInfo[nToken].nNameLen = (uint16_t)strlen(pName); + S.CounterInfo[nToken].pName = MicroProfileStringInternLower(pName); +} + +MicroProfileToken MicroProfileGetCounterTokenByParent(int nParent, const char* pName, uint32_t nFlags) +{ + for(uint32_t i = 0; i < S.nNumCounters; ++i) + { + if(nParent == S.CounterInfo[i].nParent && S.CounterInfo[i].pName == pName) + { + return i; + } + } + if(0 != (MICROPROFILE_COUNTER_FLAG_TOKEN_DONT_CREATE & nFlags)) + return MICROPROFILE_INVALID_TOKEN; + MicroProfileToken nResult = MicroProfileCounterTokenInit(nParent, nFlags); + MicroProfileCounterTokenInitName(nResult, pName); + return nResult; +} + +// by passing in last token/parent, and a non-changing static string, +// we can quickly return in case the parent is the same as before. +// Note that this doesn't support paths, but instead must be called once per level in the tree +// String must be preinterned. +MicroProfileToken MicroProfileCounterTokenTree(MicroProfileToken* LastToken, MicroProfileToken CurrentParent, const char* pString) +{ + MicroProfileToken Token = *LastToken; + if(Token != MICROPROFILE_INVALID_TOKEN) + { + if(S.CounterInfo[Token].pName == pString && S.CounterInfo[Token].nParent == CurrentParent) + { + return Token; + } + } + MicroProfileInit(); + MicroProfileScopeLock L(MicroProfileMutex()); + Token = MicroProfileGetCounterTokenByParent(CurrentParent, pString, 0); + *LastToken = Token; + return Token; +} + +const char* MicroProfileCounterString(const char* pString) +{ + MicroProfileInit(); + MicroProfileScopeLock L(MicroProfileMutex()); + return MicroProfileStringInternLower(pString); +} + +// Same as above, but works with non-static strings. always takes a lock, and does a search, so expect this to be not cheap +MicroProfileToken MicroProfileCounterTokenTreeDynamic(MicroProfileToken* LastToken, MicroProfileToken Parent, const char* pString) +{ + (void)LastToken; + MicroProfileInit(); + MicroProfileScopeLock L(MicroProfileMutex()); + const char* pSubNameLower = MicroProfileStringInternLower(pString); + return MicroProfileGetCounterTokenByParent(Parent, pSubNameLower, 0); +} + +MicroProfileToken MicroProfileGetCounterToken(const char* pName, uint32_t CounterFlag) +{ + MicroProfileInit(); + MicroProfileScopeLock L(MicroProfileMutex()); + char SubName[MICROPROFILE_NAME_MAX_LEN]; + MicroProfileToken nResult = MICROPROFILE_INVALID_TOKEN; + do + { + uint32_t nLen = 0; + pName = MicroProfileNextName(pName, &SubName[0], &nLen); + if(0 == nLen) + { + break; + } + const char* pSubNameLower = MicroProfileStringInternLower(SubName); + nResult = MicroProfileGetCounterTokenByParent(nResult, pSubNameLower, 0); + if(MICROPROFILE_INVALID_TOKEN == nResult) + return nResult; + + } while(pName != 0); + S.CounterInfo[nResult].nFlags |= MICROPROFILE_COUNTER_FLAG_LEAF; + +#if MICROPROFILE_COUNTER_HISTORY + if(CounterFlag & MICROPROFILE_COUNTER_FLAG_DOUBLE) + { + S.CounterInfo[nResult].nFlags |= MICROPROFILE_COUNTER_FLAG_DOUBLE; + S.dCounterMax[nResult] = -DBL_MAX; + S.dCounterMin[nResult] = DBL_MAX; + } +#endif + + MP_ASSERT((int)nResult >= 0); + return nResult; +} + +MicroProfileToken MicroProfileGetChildCounterToken(MicroProfileToken Parent, const char* pName) +{ + MP_ASSERT(NULL == strpbrk(pName, "\\/")); // delimiters not supported when manually building the tree. + return MicroProfileCounterTokenTreeDynamic(nullptr, Parent, pName); +} + +inline void MicroProfileLogPut(MicroProfileLogEntry LE, MicroProfileThreadLog* pLog) +{ + MP_ASSERT(pLog != 0); // this assert is hit if MicroProfileOnCreateThread is not called + MP_ASSERT(pLog->nActive == 1); // Dont put after calling thread exit + uint32_t nPut = pLog->nPut.load(std::memory_order_relaxed); + uint32_t nNextPos = (nPut + 1) % MICROPROFILE_BUFFER_SIZE; + uint32_t nGet = pLog->nGet.load(std::memory_order_relaxed); + uint32_t nDistance = (nGet - nNextPos) % MICROPROFILE_BUFFER_SIZE; + MP_ASSERT(nDistance < MICROPROFILE_BUFFER_SIZE); + uint32_t nStackPut = pLog->nStackPut; + if(nDistance < nStackPut + 2) + { + S.nOverflow = 100; + } + else + { + pLog->Log[nPut] = LE; + pLog->nPut.store(nNextPos, std::memory_order_release); + } +} + +inline uint64_t MicroProfileLogPutEnter(MicroProfileToken nToken_, uint64_t nTick, MicroProfileThreadLog* pLog) +{ + MP_ASSERT(pLog != 0); // this assert is hit if MicroProfileOnCreateThread is not called + MP_ASSERT(pLog->nActive == 1); // Dont put after calling thread exit + uint32_t nStackPut = pLog->nStackPut; + if(nStackPut < MICROPROFILE_STACK_MAX) + { + uint64_t LE = MicroProfileMakeLogIndex(MP_LOG_ENTER, nToken_, nTick); + uint32_t nPut = pLog->nPut.load(std::memory_order_relaxed); + uint32_t nNextPos = (nPut + 1) % MICROPROFILE_BUFFER_SIZE; + uint32_t nGet = pLog->nGet.load(std::memory_order_acquire); + uint32_t nDistance = (nGet - nNextPos) % MICROPROFILE_BUFFER_SIZE; + MP_ASSERT(nDistance < MICROPROFILE_BUFFER_SIZE); + if(nDistance < nStackPut + 4) // 2 for ring buffer, 2 for the actual entries + { + S.nOverflow = 100; + return MICROPROFILE_INVALID_TICK; + } + else + { +#ifdef MICROPROFILE_VERIFY_BALANCED + pLog->VerifyStack[nStackPut] = LE; +#endif + pLog->nStackPut = nStackPut + 1; + pLog->Log[nPut] = LE; + pLog->nPut.store(nNextPos, std::memory_order_release); + return nTick; + } + } + else + { + S.nOverflow = 100; + pLog->nStackPut = nStackPut + 1; + return MICROPROFILE_DROPPED_TICK; + } +} + +inline uint64_t MicroProfileLogPutEnterCStr(const char* pStr, uint64_t nTick, MicroProfileThreadLog* pLog) +{ + MP_ASSERT(pLog != 0); // this assert is hit if MicroProfileOnCreateThread is not called + MP_ASSERT(pLog->nActive == 1); // Dont put after calling thread exit + uint32_t nStackPut = pLog->nStackPut; + if(nStackPut < MICROPROFILE_STACK_MAX) + { + uint64_t LE = MicroProfileMakeLogIndex(MP_LOG_ENTER, ETOKEN_CSTR_PTR, nTick); + uint64_t LEStr = MicroProfileMakeLogExtendedNoDataPtr((uint64_t)pStr); + + MP_ASSERT(ETOKEN_CSTR_PTR == MicroProfileLogGetTimerIndex(LE)); + + uint32_t nPut = pLog->nPut.load(std::memory_order_relaxed); + uint32_t nNextPos = (nPut + 2) % MICROPROFILE_BUFFER_SIZE; + uint32_t nGet = pLog->nGet.load(std::memory_order_acquire); + uint32_t nDistance = (nGet - nNextPos) % MICROPROFILE_BUFFER_SIZE; + MP_ASSERT(nDistance < MICROPROFILE_BUFFER_SIZE); + if(nDistance < nStackPut + 6) // 2 for ring buffer, 4 for the actual entries + { + S.nOverflow = 100; + return MICROPROFILE_INVALID_TICK; + } + else + { + pLog->nStackPut = nStackPut + 1; + pLog->Log[nPut + 0] = LE; + pLog->Log[(nPut + 1) % MICROPROFILE_BUFFER_SIZE] = LEStr; + pLog->nPut.store(nNextPos, std::memory_order_release); + return nTick; + } + } + else + { + S.nOverflow = 100; + pLog->nStackPut = nStackPut + 1; + return MICROPROFILE_DROPPED_TICK; + } +} +inline void MicroProfileLogPutLeaveCStr(const char* pStr, uint64_t nTick, MicroProfileThreadLog* pLog) +{ + MP_ASSERT(pLog != 0); // this assert is hit if MicroProfileOnCreateThread is not called + MP_ASSERT(pLog->nActive); + MP_ASSERT(pLog->nStackPut != 0); + uint32_t nStackPut = --(pLog->nStackPut); + MP_ASSERT(nStackPut < 0xf0000000); + if(nStackPut < MICROPROFILE_STACK_MAX) + { + uint64_t LE = MicroProfileMakeLogIndex(MP_LOG_LEAVE, ETOKEN_CSTR_PTR, nTick); + uint64_t LEStr = MicroProfileMakeLogExtendedNoDataPtr((uint64_t)pStr); + MP_ASSERT(ETOKEN_CSTR_PTR == MicroProfileLogGetTimerIndex(LE)); + + uint32_t nPos = pLog->nPut.load(std::memory_order_relaxed); + uint32_t nNextPos = (nPos + 2) % MICROPROFILE_BUFFER_SIZE; + + uint32_t nGet = pLog->nGet.load(std::memory_order_acquire); + MP_ASSERT(nStackPut < MICROPROFILE_STACK_MAX); + MP_ASSERT(nNextPos != nGet); // should never happen + pLog->Log[nPos + 0] = LE; + pLog->Log[(nPos + 1) % MICROPROFILE_BUFFER_SIZE] = LEStr; + + pLog->nPut.store(nNextPos, std::memory_order_release); + } +} + +inline void MicroProfileLogPutLeave(MicroProfileToken nToken_, uint64_t nTick, MicroProfileThreadLog* pLog) +{ + MP_ASSERT(pLog != 0); // this assert is hit if MicroProfileOnCreateThread is not called + MP_ASSERT(pLog->nActive); + MP_ASSERT(pLog->nStackPut != 0); + uint32_t nStackPut = --(pLog->nStackPut); + if(nStackPut < MICROPROFILE_STACK_MAX) + { + uint64_t LE = MicroProfileMakeLogIndex(MP_LOG_LEAVE, nToken_, nTick); + uint32_t nPos = pLog->nPut.load(std::memory_order_relaxed); + uint32_t nNextPos = (nPos + 1) % MICROPROFILE_BUFFER_SIZE; + + uint32_t nGet = pLog->nGet.load(std::memory_order_acquire); + MP_ASSERT(nStackPut < MICROPROFILE_STACK_MAX); + MP_ASSERT(nNextPos != nGet); // should never happen + +#ifdef MICROPROFILE_VERIFY_BALANCED + // verify what we pop is what we push. + uint64_t Pushed = pLog->VerifyStack[nStackPut]; + uint64_t TimerPopped = MicroProfileLogGetTimerIndex(LE); + uint64_t TimerOnStack = MicroProfileLogGetTimerIndex(Pushed); + if(TimerPopped != TimerOnStack) + { + uprintf("Push/Pop Mismatch %s vs %s\n", S.TimerInfo[TimerPopped].pName, S.TimerInfo[TimerOnStack].pName); + MP_ASSERT(0); + } +#endif + + pLog->Log[nPos] = LE; + pLog->nPut.store(nNextPos, std::memory_order_release); + } +} + +inline void MicroProfileLogPut(MicroProfileToken nToken_, uint64_t nTick, uint64_t nBegin, MicroProfileThreadLog* pLog) +{ + MicroProfileLogPut(MicroProfileMakeLogIndex(nBegin, nToken_, nTick), pLog); +} + +inline void MicroProfileLogPutGpu(MicroProfileLogEntry LE, MicroProfileThreadLogGpu* pLog) +{ + uint32_t nPos = pLog->nPut; + if(nPos < MICROPROFILE_GPU_BUFFER_SIZE) + { + pLog->Log[nPos] = LE; + pLog->nPut = nPos + 1; + } +} + +inline void MicroProfileLogPutGpuTimer(MicroProfileToken nToken_, uint64_t nTick, uint64_t nBegin, MicroProfileThreadLogGpu* pLog) +{ + MicroProfileLogPutGpu(MicroProfileMakeLogIndex(nBegin, nToken_, nTick), pLog); +} + +inline void MicroProfileLogPutGpuExtended(EMicroProfileTokenExtended eTokenExt, uint32_t nDataSizeQWords, uint32_t nPayload, MicroProfileThreadLogGpu* pLog) +{ + MicroProfileLogEntry LE = MicroProfileMakeLogExtended(eTokenExt, nDataSizeQWords, nPayload); + MicroProfileLogPutGpu(LE, pLog); +} + +inline void MicroProfileLogPutGpuExtendedNoData(EMicroProfileTokenExtended eTokenExt, uint64_t nPayload, MicroProfileThreadLogGpu* pLog) +{ + MicroProfileLogEntry LE = MicroProfileMakeLogExtendedNoData(eTokenExt, nPayload); + MicroProfileLogPutGpu(LE, pLog); +} + +uint32_t MicroProfileGroupTokenActive(MicroProfileToken nToken_) +{ + uint32_t nMask = MicroProfileGetGroupMask(nToken_); + uint32_t nIndex = MicroProfileGetGroupMaskIndex(nToken_); + return 0 != (S.nActiveGroups[nIndex] & nMask); +} + +uint64_t MicroProfileEnterInternal(MicroProfileToken nToken_) +{ + if(MicroProfileGroupTokenActive(nToken_)) + { + uint64_t nTick = MP_TICK(); + if(MICROPROFILE_PLATFORM_MARKERS_ENABLED) + { + uint32_t idx = MicroProfileGetTimerIndex(nToken_); + MicroProfileTimerInfo& TI = S.TimerInfo[idx]; + MICROPROFILE_PLATFORM_MARKER_BEGIN(TI.nColor, TI.pNameExt); + return nTick; + } + else + { + return MicroProfileLogPutEnter(nToken_, nTick, MicroProfileGetThreadLog2()); + } + } + return MICROPROFILE_INVALID_TICK; +} + +uint64_t MicroProfileEnterInternalCStr(const char* pStr) +{ + if(S.AnyActive) + { + uint64_t nTick = MP_TICK(); + if(MICROPROFILE_PLATFORM_MARKERS_ENABLED) + { + MICROPROFILE_PLATFORM_MARKER_BEGIN(0, pStr); + return nTick; + } + else + { + return MicroProfileLogPutEnterCStr(pStr, nTick, MicroProfileGetThreadLog2()); + } + } + return MICROPROFILE_INVALID_TICK; +} + +void MicroProfileTimelineLeave(uint32_t id) +{ + if(!id) + return; + std::lock_guard Lock(MicroProfileTimelineMutex()); + MicroProfileThreadLog* pLog = &S.TimelineLog; + uint32_t nPut = pLog->nPut.load(std::memory_order_relaxed); + uint32_t nNextPos = (nPut + 1) % MICROPROFILE_BUFFER_SIZE; + uint32_t nGet = pLog->nGet.load(std::memory_order_acquire); + uint32_t nDistance = (nGet - nNextPos) % MICROPROFILE_BUFFER_SIZE; + + { + uint32_t nFrameStart = S.TimelineTokenFrameEnter[id % MICROPROFILE_TIMELINE_MAX_TOKENS]; + uint32_t nFrameCurrent = S.nFrameCurrent; + if(nFrameCurrent < nFrameStart) + nFrameCurrent += MICROPROFILE_MAX_FRAME_HISTORY; + uint32_t nFrameDistance = (nFrameCurrent - nFrameStart) % MICROPROFILE_MAX_FRAME_HISTORY; + + S.TimelineTokenFrameEnter[id % MICROPROFILE_TIMELINE_MAX_TOKENS] = MICROPROFILE_INVALID_FRAME; + S.TimelineTokenFrameLeave[id % MICROPROFILE_TIMELINE_MAX_TOKENS] = nFrameCurrent; + + S.TimelineToken[id % MICROPROFILE_TIMELINE_MAX_TOKENS] = 0; + S.nTimelineFrameMax = MicroProfileMax(S.nTimelineFrameMax, nFrameDistance); + } + + if(nDistance < 2 + 4) + { + S.nOverflow = 100; + } + else + { + uint64_t LEEnter = MicroProfileMakeLogIndex(MP_LOG_LEAVE, ETOKEN_CUSTOM_NAME, MP_TICK()); + uint64_t LEId = MicroProfileMakeLogExtended(ETOKEN_CUSTOM_ID, 0, id); + + pLog->Log[nPut++] = LEEnter; + nPut %= MICROPROFILE_BUFFER_SIZE; + pLog->Log[nPut++] = LEId; + nPut %= MICROPROFILE_BUFFER_SIZE; + pLog->nPut.store(nPut); + } +} + +void MicroProfileTimelineEnterStatic(uint32_t nColor, const char* pStr) +{ + if(!S.AnyActive) + return; + uint32_t nToken = MicroProfileTimelineEnterInternal(nColor, pStr, (uint32_t)strlen(pStr), true); + (void)nToken; +} +void MicroProfileTimelineLeaveStatic(const char* pStr) +{ + if(!S.AnyActive) + return; + + for(uint32_t i = 0; i < MICROPROFILE_TIMELINE_MAX_TOKENS; ++i) + { + if(S.TimelineTokenStaticString[i] && 0 == MP_STRCASECMP(pStr, S.TimelineTokenStaticString[i])) + { + MicroProfileTimelineLeave(S.TimelineToken[i]); + } + } +} + +uint32_t MicroProfileTimelineEnterInternal(uint32_t nColor, const char* pStr, uint32_t nStrLen, int bIsStaticString) +{ + if(!S.AnyActive) + return 0; + std::lock_guard Lock(MicroProfileTimelineMutex()); + MicroProfileThreadLog* pLog = &S.TimelineLog; + MP_ASSERT(pStr[nStrLen] == '\0'); + nStrLen += 1; + uint32_t nStringQwords = MicroProfileGetQWordSize(nStrLen); + uint32_t nNumMessages = nStringQwords; + + uint32_t nPut = pLog->nPut.load(std::memory_order_relaxed); + uint32_t nNextPos = (nPut + 1) % MICROPROFILE_BUFFER_SIZE; + uint32_t nGet = pLog->nGet.load(std::memory_order_acquire); + uint32_t nDistance = (nGet - nNextPos) % MICROPROFILE_BUFFER_SIZE; + + if(nDistance < nNumMessages + 7) + { + S.nOverflow = 100; + return 0; + } + else + { + + uint32_t token = pLog->nCustomId; + uint32_t nFrameLeave = S.TimelineTokenFrameLeave[token % MICROPROFILE_TIMELINE_MAX_TOKENS]; + uint32_t nFrameEnter = S.TimelineTokenFrameEnter[token % MICROPROFILE_TIMELINE_MAX_TOKENS]; + uint32_t nCounter = 0; + uint32_t nFrameCurrent = S.nFrameCurrent; + { + + /// dont reuse tokens until their leave command has been dead for the maximum amount of frames we can generate a capture for. + while(token == 0 || nFrameEnter != MICROPROFILE_INVALID_FRAME || (nFrameCurrent - nFrameLeave < MICROPROFILE_MAX_FRAME_HISTORY + 3 && nFrameLeave != MICROPROFILE_INVALID_FRAME)) + { + token = (uint32_t)pLog->nCustomId++; + nFrameLeave = S.TimelineTokenFrameLeave[token % MICROPROFILE_TIMELINE_MAX_TOKENS]; + nFrameEnter = S.TimelineTokenFrameEnter[token % MICROPROFILE_TIMELINE_MAX_TOKENS]; + if(++nCounter == MICROPROFILE_TIMELINE_MAX_TOKENS) + { + // MP_BREAK(); + return 0; + } + } + S.TimelineTokenFrameEnter[token % MICROPROFILE_TIMELINE_MAX_TOKENS] = S.nFrameCurrent; + } + if(bIsStaticString) + { + S.TimelineTokenStaticString[token % MICROPROFILE_TIMELINE_MAX_TOKENS] = pStr; + } + else + { + S.TimelineTokenStaticString[token % MICROPROFILE_TIMELINE_MAX_TOKENS] = nullptr; + } + S.TimelineToken[token % MICROPROFILE_TIMELINE_MAX_TOKENS] = token; + + uint64_t LEEnter = MicroProfileMakeLogIndex(MP_LOG_ENTER, ETOKEN_CUSTOM_NAME, MP_TICK()); + uint64_t LEColor = MicroProfileMakeLogExtended(ETOKEN_CUSTOM_COLOR, 0, nColor); + uint64_t LEId = MicroProfileMakeLogExtended(ETOKEN_CUSTOM_ID, nStringQwords, token); + + pLog->Log[nPut++] = LEEnter; + nPut %= MICROPROFILE_BUFFER_SIZE; + pLog->Log[nPut++] = LEColor; + nPut %= MICROPROFILE_BUFFER_SIZE; + pLog->Log[nPut++] = LEId; + nPut %= MICROPROFILE_BUFFER_SIZE; + + // copy if we dont wrap + if(nPut + nStringQwords <= MICROPROFILE_BUFFER_SIZE) + { + memcpy(&pLog->Log[nPut], pStr, nStrLen + 1); + nPut += nStringQwords; + } + else + { + int nCharsLeft = (int)nStrLen; + while(nCharsLeft > 0) + { + int nCount = MicroProfileMin(nCharsLeft, 8); + memcpy(&pLog->Log[nPut++], pStr, nCount); + // uint64_t LEPayload = MicroProfileMakeLogPayload(pStr, nCount); + // pLog->Log[nPut++] = LEPayload; nPut %= MICROPROFILE_BUFFER_SIZE; + pStr += nCount; + nCharsLeft -= nCount; + } + } + pLog->nPut.store(nPut); + return token; + } +} + +uint32_t MicroProfileTimelineEnter(uint32_t nColor, const char* pStr) +{ + return MicroProfileTimelineEnterInternal(nColor, pStr, (uint32_t)strlen(pStr), false); +} + +uint32_t MicroProfileTimelineEnterf(uint32_t nColor, const char* pStr, ...) +{ + if(!S.AnyActive) + return 0; + char buffer[MICROPROFILE_MAX_STRING + 1]; + va_list args; + va_start(args, pStr); +#ifdef _WIN32 + size_t size = vsprintf_s(buffer, pStr, args); +#else + size_t size = vsnprintf(buffer, sizeof(buffer) - 1, pStr, args); +#endif + va_end(args); + MP_ASSERT(size < sizeof(buffer)); + buffer[size] = '\0'; + return MicroProfileTimelineEnterInternal(nColor, buffer, (uint32_t)size, false); +} + +void MicroProfileLocalCounterAdd(int64_t* pCounter, int64_t nCount) +{ + *pCounter += nCount; +} +int64_t MicroProfileLocalCounterSet(int64_t* pCounter, int64_t nCount) +{ + int64_t r = *pCounter; + *pCounter = nCount; + return r; +} + +void MicroProfileLocalCounterAddAtomic(MicroProfileToken nToken, int64_t nCount) +{ + std::atomic* pCounter = &S.CounterInfo[nToken].ExternalAtomic; + pCounter->fetch_add(nCount); +} +int64_t MicroProfileLocalCounterSetAtomic(MicroProfileToken nToken, int64_t nCount) +{ + + std::atomic* pCounter = &S.CounterInfo[nToken].ExternalAtomic; + return pCounter->exchange(nCount); +} + +void MicroProfileCounterAdd(MicroProfileToken nToken, int64_t nCount) +{ + MP_ASSERT(nToken < S.nNumCounters); + S.Counters[nToken].fetch_add(nCount); +} +void MicroProfileCounterSet(MicroProfileToken nToken, int64_t nCount) +{ + MP_ASSERT(nToken < S.nNumCounters); + S.Counters[nToken].store(nCount); +} +int64_t MicroProfileCounterGet(MicroProfileToken nToken) +{ + MP_ASSERT(nToken < S.nNumCounters); + return S.Counters[nToken].load(); +} + +void MicroProfileCounterSetDouble(MicroProfileToken nToken, double nCount) +{ + MP_ASSERT(nToken < S.nNumCounters); + MP_ASSERT((S.CounterInfo[nToken].nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) == MICROPROFILE_COUNTER_FLAG_DOUBLE); + S.CountersDouble[nToken].store(nCount); +} +double MicroProfileCounterGetDouble(MicroProfileToken nToken) +{ + MP_ASSERT(nToken < S.nNumCounters); + MP_ASSERT((S.CounterInfo[nToken].nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) == MICROPROFILE_COUNTER_FLAG_DOUBLE); + return S.CountersDouble[nToken].load(); +} +void MicroProfileCounterSetLimit(MicroProfileToken nToken, int64_t nCount) +{ + MP_ASSERT(nToken < S.nNumCounters); + S.CounterInfo[nToken].nLimit = nCount; +} + +void MicroProfileCounterSetLimitDouble(MicroProfileToken nToken, double dCount) +{ + MP_ASSERT(nToken < S.nNumCounters); + MP_ASSERT((S.CounterInfo[nToken].nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) == MICROPROFILE_COUNTER_FLAG_DOUBLE); + S.CounterInfo[nToken].dLimit = dCount; +} + +void MicroProfileCounterConfigToken(MicroProfileToken nToken, uint32_t eFormat, int64_t nLimit, uint32_t nFlags) +{ + S.CounterInfo[nToken].eFormat = (MicroProfileCounterFormat)eFormat; + S.CounterInfo[nToken].nLimit = nLimit; + S.CounterInfo[nToken].nFlags |= (nFlags & ~MICROPROFILE_COUNTER_FLAG_INTERNAL_MASK); +} + +void MicroProfileCounterConfig(const char* pName, uint32_t eFormat, int64_t nLimit, uint32_t nFlags) +{ + MicroProfileToken nToken = MicroProfileGetCounterToken(pName, 0); + MicroProfileCounterConfigToken(nToken, eFormat, nLimit, nFlags); +} + +void MicroProfileCounterSetPtr(const char* pCounterName, void* pSource, uint32_t nSize) +{ + MicroProfileToken nToken = MicroProfileGetCounterToken(pCounterName, 0); + S.CounterSource[nToken].pSource = pSource; + S.CounterSource[nToken].nSourceSize = nSize; +} + +inline void MicroProfileFetchCounter(uint32_t i) +{ + MP_ASSERT(0 == S.CounterSource[i].nSourceSize || (S.CounterInfo[i].nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) == MICROPROFILE_COUNTER_FLAG_DOUBLE); + switch(S.CounterSource[i].nSourceSize) + { + case sizeof(int32_t): + S.Counters[i] = *(int32_t*)S.CounterSource[i].pSource; + break; + case sizeof(int64_t): + S.Counters[i] = *(int64_t*)S.CounterSource[i].pSource; + break; + default: + break; + } +} +void MicroProfileCounterFetchCounters() +{ + for(uint32_t i = 0; i < S.nNumCounters; ++i) + { + MicroProfileFetchCounter(i); + } +} + +void MicroProfileLeaveInternal(MicroProfileToken nToken_, uint64_t nTickStart) +{ + if(MICROPROFILE_INVALID_TICK != nTickStart) + { + if(MICROPROFILE_PLATFORM_MARKERS_ENABLED) + { + MICROPROFILE_PLATFORM_MARKER_END(); + } + else + { + uint64_t nTick = MP_TICK(); + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog2(); + MicroProfileLogPutLeave(nToken_, nTick, pLog); + } + } +} + +void MicroProfileLeaveInternalCStr(const char* pStr, uint64_t nTickStart) +{ + if(MICROPROFILE_INVALID_TICK != nTickStart) + { + if(MICROPROFILE_PLATFORM_MARKERS_ENABLED) + { + MICROPROFILE_PLATFORM_MARKER_END(); + } + else + { + uint64_t nTick = MP_TICK(); + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog2(); + MicroProfileLogPutLeaveCStr(pStr, nTick, pLog); + } + } +} + +void MicroProfileEnter(MicroProfileToken nToken) +{ + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog2(); + MP_ASSERT(pLog->nStackScope < MICROPROFILE_STACK_MAX); // if youre hitting this assert you probably have mismatched _ENTER/_LEAVE markers + uint32_t nStackPos = pLog->nStackScope++; + if(nStackPos < MICROPROFILE_STACK_MAX) + { + MicroProfileScopeStateC* pScopeState = &pLog->ScopeState[nStackPos]; + pScopeState->Token = nToken; + pScopeState->nTick = MicroProfileEnterInternal(nToken); + } + else + { + S.nOverflow = 100; + } +} +void MicroProfileLeave() +{ + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog2(); + MP_ASSERT(pLog->nStackScope > 0); // if youre hitting this assert you probably have mismatched _ENTER/_LEAVE markers + uint32_t nStackPos = --pLog->nStackScope; + if(nStackPos < MICROPROFILE_STACK_MAX) + { + MicroProfileScopeStateC* pScopeState = &pLog->ScopeState[nStackPos]; + MicroProfileLeaveInternal(pScopeState->Token, pScopeState->nTick); + } + else + { + S.nOverflow = 100; + } +} + +void MicroProfileEnterGpu(MicroProfileToken nToken, MicroProfileThreadLogGpu* pLog) +{ + // MP_ASSERT(pLog->nStackScope < MICROPROFILE_STACK_MAX); // if youre hitting this assert you probably have mismatched _ENTER/_LEAVE markers + uint32_t nStackPos = pLog->nStackScope++; + if(nStackPos < MICROPROFILE_STACK_MAX) + { + MicroProfileScopeStateC* pScopeState = &pLog->ScopeState[nStackPos]; + pScopeState->Token = nToken; + pScopeState->nTick = MicroProfileGpuEnterInternal(pLog, nToken); + } + else + { + S.nOverflow = 100; + } +} +void MicroProfileLeaveGpu(MicroProfileThreadLogGpu* pLog) +{ + uint32_t nStackPos = --pLog->nStackScope; + if(nStackPos < MICROPROFILE_STACK_MAX) + { + MicroProfileScopeStateC* pScopeState = &pLog->ScopeState[nStackPos]; + MicroProfileGpuLeaveInternal(pLog, pScopeState->Token, pScopeState->nTick); + } +} + +void MicroProfileGpuBegin(void* pContext, MicroProfileThreadLogGpu* pLog) +{ + MP_ASSERT(pLog->pContext == (void*)-1); // dont call begin without calling end + MP_ASSERT(pLog->nStart == (uint32_t)-1); + MP_ASSERT(pContext != (void*)-1); + + pLog->pContext = pContext; + pLog->nStart = pLog->nPut; + MicroProfileLogPutGpu(0, pLog); +} + +void MicroProfileGpuSetContext(void* pContext, MicroProfileThreadLogGpu* pLog) +{ + MP_ASSERT(pLog->pContext != (void*)-1); // dont call begin without calling end + MP_ASSERT(pLog->nStart != (uint32_t)-1); + pLog->pContext = pContext; +} + +uint64_t MicroProfileGpuEnd(MicroProfileThreadLogGpu* pLog) +{ + uint64_t nStart = pLog->nStart; + uint32_t nEnd = pLog->nPut; + uint64_t nId = pLog->nId; + if(nStart < MICROPROFILE_GPU_BUFFER_SIZE) + { + pLog->Log[nStart] = nEnd - nStart - 1; + } + pLog->pContext = (void*)-1; + pLog->nStart = (uint32_t)-1; + return nStart | (nId << 32); +} + +void MicroProfileGpuSubmit(int nQueue, uint64_t nWork) +{ + MP_ASSERT(nQueue >= 0 && nQueue < MICROPROFILE_MAX_THREADS); + MICROPROFILE_SCOPE(g_MicroProfileGpuSubmit); + uint32_t nStart = (uint32_t)nWork; + uint32_t nThreadLog = uint32_t(nWork >> 32); + + MicroProfileThreadLog* pQueueLog = S.Pool[nQueue]; + MP_ASSERT(nQueue < MICROPROFILE_MAX_THREADS); + MicroProfileThreadLogGpu* pGpuLog = S.PoolGpu[nThreadLog]; + MP_ASSERT(pGpuLog); + + int64_t nCount = 0; + if(nStart < MICROPROFILE_GPU_BUFFER_SIZE) + { + nCount = pGpuLog->Log[nStart]; + } + MP_ASSERT(nCount < (int64_t)MICROPROFILE_GPU_BUFFER_SIZE); + nStart++; + for(int32_t i = 0; i < nCount; ++i) + { + MP_ASSERT(nStart < MICROPROFILE_GPU_BUFFER_SIZE); + MicroProfileLogEntry LE = pGpuLog->Log[nStart++]; + MicroProfileLogPut(LE, pQueueLog); + } +} + +uint64_t MicroProfileGpuEnterInternal(MicroProfileThreadLogGpu* pGpuLog, MicroProfileToken nToken_) +{ + if(MicroProfileGroupTokenActive(nToken_)) + { + if(!MicroProfileGetThreadLog()) + { + MicroProfileInitThreadLog(); + } + + MP_ASSERT(pGpuLog->pContext != (void*)-1); // must be called between GpuBegin/GpuEnd + uint64_t nTimer = MicroProfileGpuInsertTimeStamp(pGpuLog->pContext); + MicroProfileLogPutGpuTimer(nToken_, nTimer, MP_LOG_ENTER, pGpuLog); + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog(); + + MicroProfileLogPutGpuExtendedNoData(ETOKEN_GPU_CPU_TIMESTAMP, MP_TICK(), pGpuLog); + MicroProfileLogPutGpuExtendedNoData(ETOKEN_GPU_CPU_SOURCE_THREAD, pLog->nLogIndex, pGpuLog); + // MP_ASSERT(pGpuLog->pContext != (void*)-1); // must be called between GpuBegin/GpuEnd + // uint64_t nTimer = MicroProfileGpuInsertTimeStamp(pGpuLog->pContext); + // MicroProfileLogPutGpu(nToken_, nTimer, MP_LOG_ENTER, pGpuLog); + // MicroProfileThreadLog* pLog = MicroProfileGetThreadLog(); + // MicroProfileLogPutGpu(ETOKEN_GPU_CPU_TIMESTAMP, MP_TICK(), MP_LOG_EXTRA_DATA, pGpuLog); + // MicroProfileLogPutGpu(ETOKEN_GPU_CPU_SOURCE_THREAD, pLog->nLogIndex, MP_LOG_EXTRA_DATA, pGpuLog); + + return 1; + } + return 0; +} + +uint64_t MicroProfileGpuEnterInternalCStr(MicroProfileThreadLogGpu* pGpuLog, const char* pStr) +{ + MP_BREAK(); // not implemented + return 0; + // if(S.AnyGpuActive) + // { + // if(!MicroProfileGetThreadLog()) + // { + // MicroProfileInitThreadLog(); + // } + + // MP_ASSERT(pGpuLog->pContext != (void*)-1); // must be called between GpuBegin/GpuEnd + // uint64_t nTimer = MicroProfileGpuInsertTimeStamp(pGpuLog->pContext); + // MicroProfileLogPutGpuTimer(nToken_, nTimer, MP_LOG_ENTER, pGpuLog); + // MicroProfileThreadLog* pLog = MicroProfileGetThreadLog(); + + // MicroProfileLogPutGpuExtendedNoData(ETOKEN_GPU_CPU_TIMESTAMP, MP_TICK(), pGpuLog); + // MicroProfileLogPutGpuExtendedNoData(ETOKEN_GPU_CPU_SOURCE_THREAD, pLog->nLogIndex, pGpuLog); + // // MP_ASSERT(pGpuLog->pContext != (void*)-1); // must be called between GpuBegin/GpuEnd + // // uint64_t nTimer = MicroProfileGpuInsertTimeStamp(pGpuLog->pContext); + // // MicroProfileLogPutGpu(nToken_, nTimer, MP_LOG_ENTER, pGpuLog); + // // MicroProfileThreadLog* pLog = MicroProfileGetThreadLog(); + // // MicroProfileLogPutGpu(ETOKEN_GPU_CPU_TIMESTAMP, MP_TICK(), MP_LOG_EXTRA_DATA, pGpuLog); + // // MicroProfileLogPutGpu(ETOKEN_GPU_CPU_SOURCE_THREAD, pLog->nLogIndex, MP_LOG_EXTRA_DATA, pGpuLog); + + // return 1; + // } + // return 0; +} + +void MicroProfileGpuLeaveInternal(MicroProfileThreadLogGpu* pGpuLog, MicroProfileToken nToken_, uint64_t nTickStart) +{ + if(nTickStart) + { + if(!MicroProfileGetThreadLog()) + { + MicroProfileInitThreadLog(); + } + + MP_ASSERT(pGpuLog->pContext != (void*)-1); // must be called between GpuBegin/GpuEnd + uint64_t nTimer = MicroProfileGpuInsertTimeStamp(pGpuLog->pContext); + MicroProfileLogPutGpuTimer(nToken_, nTimer, MP_LOG_LEAVE, pGpuLog); + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog(); + MicroProfileLogPutGpuExtendedNoData(ETOKEN_GPU_CPU_TIMESTAMP, MP_TICK(), pGpuLog); + MicroProfileLogPutGpuExtendedNoData(ETOKEN_GPU_CPU_SOURCE_THREAD, pLog->nLogIndex, pGpuLog); + } +} + +void MicroProfileGpuLeaveInternalCStr(MicroProfileThreadLogGpu* pGpuLog, uint64_t nTickStart) +{ + MP_BREAK(); // not implemented + return; + // if(nTickStart) + // { + // if(!MicroProfileGetThreadLog()) + // { + // MicroProfileInitThreadLog(); + // } + + // MP_ASSERT(pGpuLog->pContext != (void*)-1); // must be called between GpuBegin/GpuEnd + // uint64_t nTimer = MicroProfileGpuInsertTimeStamp(pGpuLog->pContext); + // MicroProfileLogPutGpuTimer(nToken_, nTimer, MP_LOG_LEAVE, pGpuLog); + // MicroProfileThreadLog* pLog = MicroProfileGetThreadLog(); + // MicroProfileLogPutGpuExtendedNoData(ETOKEN_GPU_CPU_TIMESTAMP, MP_TICK(), pGpuLog); + // MicroProfileLogPutGpuExtendedNoData(ETOKEN_GPU_CPU_SOURCE_THREAD, pLog->nLogIndex, pGpuLog); + // } +} + +void MicroProfileContextSwitchPut(MicroProfileContextSwitch* pContextSwitch) +{ + if(0 == S.nPauseTicks || (S.nPauseTicks - pContextSwitch->nTicks) > 0) + { + uint32_t nPut = S.nContextSwitchPut; + S.ContextSwitch[nPut] = *pContextSwitch; + S.nContextSwitchPut = (S.nContextSwitchPut + 1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; + // if(S.nContextSwitchPut < nPut) + //{ + // float fMsDelay = MicroProfileTickToMsMultiplierCpu() * ((int64_t)S.nFlipStartTick - pContextSwitch->nTicks); + // uprintf("context switch wrap .. %7.3fms\n", fMsDelay); + // } + // if(S.nContextSwitchPut % 1024 == 0) + //{ + // float fMsDelay = MicroProfileTickToMsMultiplierCpu() * ((int64_t)S.nFlipStartTick - pContextSwitch->nTicks); + // uprintf("cswitch tick %x ... %7.3fms\n", S.nContextSwitchPut, fMsDelay); + // } + S.nContextSwitchLastPushed = pContextSwitch->nTicks; + } + else + { + S.nContextSwitchStalledTick = MP_TICK(); + } +} + +void MicroProfileGetRange(uint32_t nPut, uint32_t nGet, uint32_t nRange[2][2]) +{ + if(nPut > nGet) + { + nRange[0][0] = nGet; + nRange[0][1] = nPut; + nRange[1][0] = nRange[1][1] = 0; + } + else if(nPut != nGet) + { + MP_ASSERT(nGet != MICROPROFILE_BUFFER_SIZE); + uint32_t nCountEnd = MICROPROFILE_BUFFER_SIZE - nGet; + nRange[0][0] = nGet; + nRange[0][1] = nGet + nCountEnd; + nRange[1][0] = 0; + nRange[1][1] = nPut; + } +} + +void MicroProfileToggleFrozen() +{ + S.nFrozen = !S.nFrozen; +} + +int MicroProfileIsFrozen() +{ + return S.nFrozen != 0 ? 1 : 0; +} +int MicroProfileEnabled() +{ + return MicroProfileAnyGroupActive(); +} +void* MicroProfileAllocInternal(size_t nSize, size_t nAlign) +{ + nAlign = MicroProfileMax(4 * sizeof(uint32_t), nAlign); + nSize += nAlign; + intptr_t nPtr = (intptr_t)MICROPROFILE_ALLOC(nSize, nAlign); + nPtr += nAlign; + uint32_t* pVal = (uint32_t*)nPtr; + MP_ASSERT(nSize < 0xffffffff); + MP_ASSERT(nAlign < 0xffffffff); + pVal[-1] = (uint32_t)nSize; + pVal[-2] = (uint32_t)nAlign; + pVal[-3] = (uint32_t)0x28586813; + MicroProfileCounterAdd(S.CounterToken_Alloc_Memory, nSize); + MicroProfileCounterAdd(S.CounterToken_Alloc_Count, 1); + return (void*)nPtr; +} +void MicroProfileFreeInternal(void* pPtr) +{ + intptr_t p = (intptr_t)pPtr; + uint32_t* p4 = (uint32_t*)pPtr; + uint32_t nSize = p4[-1]; + uint32_t nAlign = p4[-2]; + uint32_t nMagic = p4[-3]; + MP_ASSERT(nMagic == 0x28586813); + MICROPROFILE_FREE((void*)(p - nAlign)); + MicroProfileCounterAdd(S.CounterToken_Alloc_Memory, -(int)nSize); + MicroProfileCounterAdd(S.CounterToken_Alloc_Count, -1); +} +void* MicroProfileReallocInternal(void* pPtr, size_t nSize) +{ + intptr_t p = (intptr_t)pPtr; + uint32_t nAlignBase; + + if(p) + { + uint32_t* p4 = (uint32_t*)pPtr; + uint32_t nSizeBase = p4[-1]; + nAlignBase = p4[-2]; + uint32_t nMagicBase = p4[-3]; + MP_ASSERT(nMagicBase == 0x28586813); + + MicroProfileCounterAdd(S.CounterToken_Alloc_Memory, nSize - nSizeBase); + } + else + { + nAlignBase = 4 * sizeof(uint32_t); + MicroProfileCounterAdd(S.CounterToken_Alloc_Memory, nSize + nAlignBase); + MicroProfileCounterAdd(S.CounterToken_Alloc_Count, 1); + } + + nSize += nAlignBase; + MP_ASSERT(nAlignBase >= 4 * sizeof(uint32_t)); + if(p) + { + p = (intptr_t)MICROPROFILE_REALLOC((void*)(p - nAlignBase), nSize); + } + else + { + p = (intptr_t)MICROPROFILE_REALLOC((void*)(p), nSize); + } + p += nAlignBase; + uint32_t* pVal = (uint32_t*)p; + MP_ASSERT(nSize < 0xffffffff); + MP_ASSERT(nAlignBase < 0xffffffff); + pVal[-1] = (uint32_t)nSize; + pVal[-2] = (uint32_t)nAlignBase; + pVal[-3] = (uint32_t)0x28586813; + return (void*)p; +} + +static void MicroProfileFlipEnabled() +{ + if(S.nFrozen) + { + memset(S.nActiveGroups, 0, sizeof(S.nActiveGroups)); + S.AnyActive = false; + } + else + { + bool AnyActive = false; + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUP_INTS; ++i) + { + uint32_t nNew = S.nActiveGroupsWanted[i]; + nNew |= S.nForceGroups[i]; + if(nNew) + AnyActive = true; + if(S.nActiveGroups[i] != nNew) + { + S.nActiveGroups[i] = nNew; + } + } + S.AnyActive = AnyActive; + } +} + +void MicroProfileFlip(void* pContext, uint32_t FlipFlag) +{ + MicroProfileFlip_CB(pContext, nullptr, FlipFlag); +} + +#define MICROPROFILE_TICK_VALIDATE_FRAME_TIME 0 + +void MicroProfileFlip_CB(void* pContext, MicroProfileOnFreeze FreezeCB, uint32_t FlipFlag) +{ + MICROPROFILE_COUNTER_LOCAL_UPDATE_SET_ATOMIC(g_MicroProfileBytesPerFlip); +#if 0 + //verify LogEntry wraps correctly + MicroProfileLogEntry c = MP_LOG_TICK_MASK-5000; + for(int i = 0; i < 10000; ++i, c += 1) + { + MicroProfileLogEntry l2 = (c+2500) & MP_LOG_TICK_MASK; + MP_ASSERT(2500 == MicroProfileLogTickDifference(c, l2)); + } +#endif + MICROPROFILE_SCOPE(g_MicroProfileFlip); + std::lock_guard Lock(MicroProfileMutex()); + + if(S.nDumpFileNextFrame) + { + if(0 == S.nDumpFileCountDown) + { + MicroProfileDumpToFile(); + S.nDumpFileNextFrame = 0; + S.nAutoClearFrames = MICROPROFILE_GPU_FRAME_DELAY + 3; // hide spike from dumping webpage + } + else + { + S.nDumpFileCountDown--; + } + } +#if MICROPROFILE_WEBSERVER + if(MICROPROFILE_FLIP_FLAG_START_WEBSERVER == (MICROPROFILE_FLIP_FLAG_START_WEBSERVER & FlipFlag) && S.nWebServerDataSent == (uint64_t)-1) + { + MicroProfileWebServerStart(); + S.nWebServerDataSent = 0; + if(!S.WebSocketThreadRunning) + { + S.WebSocketThreadRunning = 1; + MicroProfileThreadStart(&S.WebSocketSendThread, MicroProfileSocketSenderThread); + } + } +#endif + + int nLoop = 0; + do + { +#if MICROPROFILE_WEBSERVER + if(MicroProfileWebServerUpdate()) + { + S.nAutoClearFrames = MICROPROFILE_GPU_FRAME_DELAY + 3; // hide spike from dumping webpage + } +#endif + if(nLoop++) + { + MicroProfileSleep(100); + if((nLoop % 10) == 0) + { + uprintf("microprofile frozen %d\n", nLoop); + } + } + } while(S.nFrozen); + + uint32_t nAggregateClear = S.nAggregateClear || S.nAutoClearFrames, nAggregateFlip = 0; + + if(S.nAutoClearFrames) + { + nAggregateClear = 1; + nAggregateFlip = 1; + S.nAutoClearFrames -= 1; + } + + bool nRunning = MicroProfileAnyGroupActive(); + if(nRunning) + { + S.nFlipStartTick = MP_TICK(); + int64_t nGpuWork = MicroProfileGpuEnd(S.pGpuGlobal); + MicroProfileGpuSubmit(S.GpuQueue, nGpuWork); + MicroProfileThreadLogGpuReset(S.pGpuGlobal); + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + if(S.PoolGpu[i]) + { + S.PoolGpu[i]->nPut = 0; + } + } + + MicroProfileGpuBegin(pContext, S.pGpuGlobal); + + uint32_t nGpuTimeStamp = MicroProfileGpuFlip(pContext); + + uint64_t nFrameIdx = S.nFramePutIndex++; + S.nFramePut = (S.nFramePut + 1) % MICROPROFILE_MAX_FRAME_HISTORY; + S.Frames[S.nFramePut].nFrameId = nFrameIdx; + MP_ASSERT((S.nFramePutIndex % MICROPROFILE_MAX_FRAME_HISTORY) == S.nFramePut); + S.nFrameCurrent = (S.nFramePut + MICROPROFILE_MAX_FRAME_HISTORY - MICROPROFILE_GPU_FRAME_DELAY - 1) % MICROPROFILE_MAX_FRAME_HISTORY; + S.nFrameCurrentIndex++; + uint32_t nFrameNext = (S.nFrameCurrent + 1) % MICROPROFILE_MAX_FRAME_HISTORY; + S.nFrameNext = nFrameNext; + + uint32_t nContextSwitchPut = S.nContextSwitchPut; + if(S.nContextSwitchLastPut < nContextSwitchPut) + { + S.nContextSwitchUsage = (nContextSwitchPut - S.nContextSwitchLastPut); + } + else + { + S.nContextSwitchUsage = MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE - S.nContextSwitchLastPut + nContextSwitchPut; + } + S.nContextSwitchLastPut = nContextSwitchPut; + + MicroProfileFrameState* pFramePut = &S.Frames[S.nFramePut]; + MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent]; + MicroProfileFrameState* pFrameNext = &S.Frames[nFrameNext]; + const int64_t nTickStartFrame = pFrameCurrent->nFrameStartCpu; + const int64_t nTickEndFrame = pFrameNext->nFrameStartCpu; + + pFrameCurrent->nGpuPending = 0; + pFramePut->nGpuPending = 1; + + pFramePut->nFrameStartCpu = MP_TICK(); + + pFramePut->nFrameStartGpu = nGpuTimeStamp; + { + const float fDumpTimeThreshold = 1000.f * 60 * 60 * 24 * 365.f; // if time above this, then we're handling uninitialized counters + int nDumpNextFrame = 0; + float fTimeGpu = 0.f; + if(pFrameNext->nFrameStartGpu != MICROPROFILE_INVALID_TICK) + { + + uint64_t nTickCurrent = pFrameCurrent->nFrameStartGpu; + uint64_t nTickNext = pFrameNext->nFrameStartGpu = MicroProfileGpuGetTimeStamp((uint32_t)pFrameNext->nFrameStartGpu); + nTickCurrent = MicroProfileLogTickMin(nTickCurrent, nTickNext); + float fTime = 1000.f * (nTickNext - nTickCurrent) / (MicroProfileTicksPerSecondGpu()); + fTime = fTimeGpu; + if(S.fDumpGpuSpike > 0.f && fTime > S.fDumpGpuSpike && fTime < fDumpTimeThreshold) + { + nDumpNextFrame = 1; + } + } + float fTimeCpu = 1000.f * (pFrameNext->nFrameStartCpu - pFrameCurrent->nFrameStartCpu) / MicroProfileTicksPerSecondCpu(); + if(S.fDumpCpuSpike > 0.f && fTimeCpu > S.fDumpCpuSpike && fTimeCpu < fDumpTimeThreshold) + { + nDumpNextFrame = 1; + } + if(nDumpNextFrame) + { + S.nDumpFileNextFrame = S.nDumpSpikeMask; + S.nDumpSpikeMask = 0; + S.nDumpFileCountDown = 5; + } + } + + const uint64_t nTickEndFrameGpu_ = pFrameNext->nFrameStartGpu; + const uint64_t nTickStartFrameGpu_ = pFrameCurrent->nFrameStartGpu; + const bool bGpuFrameInvalid = nTickEndFrameGpu_ == MICROPROFILE_INVALID_TICK || nTickStartFrameGpu_ == MICROPROFILE_INVALID_TICK; + const uint64_t nTickEndFrameGpu = bGpuFrameInvalid ? 1 : nTickEndFrameGpu_; + const uint64_t nTickStartFrameGpu = bGpuFrameInvalid ? 2 : nTickStartFrameGpu_; + + MicroProfileFrameExtraCounterData* ExtraData = S.FrameExtraCounterData; + bool UsingExtraData = false; + if(ExtraData) + { + if((intptr_t)ExtraData == 1) + { + size_t Bytes = sizeof(MicroProfileFrameExtraCounterData) * MICROPROFILE_MAX_FRAME_HISTORY; + printf(" allocating %d bytes %f\n", (int)Bytes, Bytes / (1024.0 * 1024.0)); + ExtraData = S.FrameExtraCounterData = (MicroProfileFrameExtraCounterData*)MicroProfileAllocInternal(Bytes, alignof(uint64_t)); + memset(ExtraData, 0, Bytes); + } + ExtraData = ExtraData + S.nFrameCurrent; + UsingExtraData = true; + } +#define MP_ASSERT_LE_WRAP(l, g) MP_ASSERT(uint64_t(g - l) < 0x8000000000000000) + + { + MP_ASSERT_LE_WRAP(nTickStartFrame, nTickEndFrame); + uint64_t nTick = nTickEndFrame - nTickStartFrame; + S.nFlipTicks = nTick; + S.nFlipAggregate += nTick; + S.nFlipMax = MicroProfileMax(S.nFlipMax, nTick); + } + + uint32_t* pTimerToGroup = &S.TimerToGroup[0]; + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + MicroProfileThreadLog* pLog = S.Pool[i]; + if(!pLog) + { + pFramePut->nLogStart[i] = 0; + } + else + { + uint32_t nPut = pLog->nPut.load(std::memory_order_acquire); + pFramePut->nLogStart[i] = nPut; + if(!pLog->nGpu) + { + uint32_t nStart = pFrameCurrent->nLogStart[i]; + while(nStart != nPut) + { + int64_t LE = pLog->Log[nStart]; + int64_t nDifference = MicroProfileLogTickDifference(LE, nTickEndFrame); + uint32_t Ext = MicroProfileLogGetType(LE); + if(nDifference > 0 || 0 != (0x2 & Ext)) + { + nStart = (nStart + 1) % MICROPROFILE_BUFFER_SIZE; + } + else + { + break; + } + } + pFrameNext->nLogStart[i] = nStart; + } + } + } + { + pFramePut->nLogStartTimeline = S.TimelineLog.nPut.load(std::memory_order_acquire); + + uint32_t nFrameCurrent = S.nFrameCurrent; + uint32_t nTimelineFrameDeltaMax = S.nTimelineFrameMax; + for(uint32_t i = 0; i != MICROPROFILE_TIMELINE_MAX_TOKENS; ++i) + { + uint32_t nFrameStart = S.TimelineTokenFrameEnter[i]; + if(nFrameStart != MICROPROFILE_INVALID_FRAME) + { + uint32_t nCur = nFrameCurrent; + if(nCur < nFrameStart) + nCur += MICROPROFILE_MAX_FRAME_HISTORY; + if(nCur >= nFrameStart) + { + uint32_t D = nCur - nFrameStart; + nTimelineFrameDeltaMax = MicroProfileMax(nTimelineFrameDeltaMax, D); + } + } + } + pFramePut->nTimelineFrameMax = nTimelineFrameDeltaMax; + S.nTimelineFrameMax = 0; + } + { + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + MicroProfileThreadLog* pLog = S.Pool[i]; + if(!pLog) + continue; + if(pLog->nGpu) + { + uint32_t nPut = pFrameNext->nLogStart[i]; + uint32_t nGet = pFrameCurrent->nLogStart[i]; + uint32_t nRange[2][2] = { + { 0, 0 }, + { 0, 0 }, + }; + MicroProfileGetRange(nPut, nGet, nRange); + for(uint32_t j = 0; j < 2; ++j) + { + uint32_t nStart = nRange[j][0]; + uint32_t nEnd = nRange[j][1]; + for(uint32_t k = nStart; k < nEnd; ++k) + { + MicroProfileLogEntry L = pLog->Log[k]; + if(MicroProfileLogGetType(L) < MP_LOG_EXTENDED) + { + pLog->Log[k] = MicroProfileLogSetTick(L, MicroProfileGpuGetTimeStamp((uint32_t)MicroProfileLogGetTick(L))); + } + k += MicroProfileLogGetDataSize(L); + } + } + } + } + } + + { + MicroProfile::GroupTime* pFrameGroup = &S.FrameGroup[0]; + { + MICROPROFILE_SCOPE(g_MicroProfileClear); + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + S.Frame[i].nTicks = 0; + S.Frame[i].nCount = 0; + S.FrameExclusive[i] = 0; + } + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + { + pFrameGroup[i].nTicks = 0; + pFrameGroup[i].nTicksExclusive = 0; + pFrameGroup[i].nCount = 0; + } + } + { + MICROPROFILE_SCOPE(g_MicroProfileThreadLoop); + memset(S.FrameGroupThreadValid, 0, sizeof(S.FrameGroupThreadValid)); + + for(uint32_t idx_thread = 0; idx_thread < MICROPROFILE_MAX_THREADS; ++idx_thread) + { + MicroProfileThreadLog* pLog = S.Pool[idx_thread]; + if(!pLog) + continue; + bool bGpu = pLog->nGpu != 0; + int64_t nTickStartLog = bGpu ? nTickStartFrameGpu : nTickStartFrame; + int64_t nTickEndLog = bGpu ? nTickEndFrameGpu : nTickEndFrame; + + float fToMs = bGpu ? MicroProfileTickToMsMultiplierGpu() : MicroProfileTickToMsMultiplierCpu(); + float fFrameTime = fToMs * (nTickEndLog - nTickStartLog); + + MicroProfile::GroupTime* pFrameGroupThread = &S.FrameGroupThread[idx_thread][0]; + + uint32_t nPut = pFrameNext->nLogStart[idx_thread]; + uint32_t nGet = pFrameCurrent->nLogStart[idx_thread]; + uint32_t nRange[2][2] = { + { 0, 0 }, + { 0, 0 }, + }; + MicroProfileGetRange(nPut, nGet, nRange); + if(nPut != nGet) + { + S.FrameGroupThreadValid[idx_thread / 32] |= 1 << (idx_thread % 32); + memset(pFrameGroupThread, 0, sizeof(S.FrameGroupThread[idx_thread])); + } + + uint64_t* pStackLog = &pLog->nStackLogEntry[0]; + uint64_t* pChildTickStack = &pLog->nChildTickStack[1]; + int32_t nStackPos = pLog->nStackPos; + uint8_t TimerStackPos[MICROPROFILE_MAX_TIMERS]; + uint8_t GroupStackPos[MICROPROFILE_MAX_GROUPS]; + memset(TimerStackPos, 0, sizeof(TimerStackPos)); + memset(GroupStackPos, 0, sizeof(GroupStackPos)); + + // restore group and timer stack pos. + for(int32_t i = 0; i < nStackPos; ++i) + { + uint64_t nTimer = MicroProfileLogGetTimerIndex(pStackLog[i]); + uint32_t nGroup = pTimerToGroup[nTimer]; + MP_ASSERT(nTimer < MICROPROFILE_MAX_TIMERS); + MP_ASSERT(nGroup < MICROPROFILE_MAX_GROUPS); + TimerStackPos[nTimer]++; + GroupStackPos[nGroup]++; + } + + for(uint32_t j = 0; j < 2; ++j) + { + uint32_t nStart = nRange[j][0]; + uint32_t nEnd = nRange[j][1]; + for(uint32_t k = nStart; k < nEnd; ++k) + { + MicroProfileLogEntry LE = pLog->Log[k]; + uint32_t nType = MicroProfileLogGetType(LE); + + switch(nType) + { + case MP_LOG_ENTER: + { + uint64_t nTimer = MicroProfileLogGetTimerIndex(LE); + if(nTimer != ETOKEN_CSTR_PTR) + { + MP_ASSERT(nTimer < S.nTotalTimers); + uint32_t nGroup = pTimerToGroup[nTimer]; + MP_ASSERT(nStackPos < MICROPROFILE_STACK_MAX); + MP_ASSERT(nGroup < MICROPROFILE_MAX_GROUPS); + + // When we aggretate the total time, we have to count if the timers & groups are layered, to avoid summing them twice when calculating the total time. + // Averages become nonsense regardless. + TimerStackPos[nTimer]++; + GroupStackPos[nGroup]++; + + pStackLog[nStackPos] = LE; + + pChildTickStack[nStackPos] = 0; + nStackPos++; + } + break; + } + case MP_LOG_LEAVE: + { + uint64_t nTimer = MicroProfileLogGetTimerIndex(LE); + if(nTimer != ETOKEN_CSTR_PTR) + { + MP_ASSERT(nTimer < S.nTotalTimers); + uint32_t nGroup = pTimerToGroup[nTimer]; + MP_ASSERT(nGroup < MICROPROFILE_MAX_GROUPS); + MP_ASSERT(nStackPos); + uint64_t nTicks; + bool bGroupRoot = 0 == GroupStackPos[nGroup] || 0 == --GroupStackPos[nGroup]; + bool bTimerRoot = 0 == TimerStackPos[nTimer] || 0 == --TimerStackPos[nTimer]; + { + nStackPos--; + MicroProfileLogEntry LEStack = pStackLog[nStackPos]; + MP_ASSERT(MicroProfileLogGetTimerIndex(LEStack) == nTimer); // unbalanced timers are not supported + uint64_t nTickStart = MicroProfileLogTickClamp(LEStack, nTickStartLog, nTickEndLog); + uint64_t nClamped = MicroProfileLogTickClamp(LE, nTickStartLog, nTickEndLog); + nTicks = MicroProfileLogTickDifference(nTickStart, nClamped); + MP_ASSERT(nTicks < 0x8000000000000000); + + uint64_t nChildTicks = pChildTickStack[nStackPos]; + + MP_ASSERT(nStackPos < MICROPROFILE_STACK_MAX); + if(nStackPos) + { + pChildTickStack[nStackPos - 1] += nTicks; + } + MP_ASSERT(nTicks >= nChildTicks); + uint64_t nTicksExclusive = (nTicks - nChildTicks); + S.FrameExclusive[nTimer] += nTicksExclusive; + pFrameGroupThread[nGroup].nTicksExclusive += nTicksExclusive; + if(bTimerRoot) // dont count this if its below another instance of the same timer. + { + S.Frame[nTimer].nTicks += nTicks; + S.Frame[nTimer].nCount += 1; + MP_ASSERT(nGroup < MICROPROFILE_MAX_GROUPS); + if(bGroupRoot) + { + pFrameGroupThread[nGroup].nTicks += nTicks; + pFrameGroupThread[nGroup].nCount += 1; + } + } + } + } + break; + } + case MP_LOG_EXTENDED: + { + k += MicroProfileLogGetDataSize(LE); + break; + } + case MP_LOG_EXTENDED_NO_DATA: + break; + } + } + } + + for(int32_t i = nStackPos - 1; i >= 0; --i) + { + + MicroProfileLogEntry LE = pStackLog[i]; + uint64_t nTickStart = MicroProfileLogTickClamp(LE, nTickStartLog, nTickEndLog); + uint64_t nTicks = MicroProfileLogTickDifference(nTickStart, nTickEndLog); + int64_t nChildTicks = pChildTickStack[i]; + pChildTickStack[i] = 0; // consume.. + + MP_ASSERT(i < MICROPROFILE_STACK_MAX && i >= 0); + if(i) + { + pChildTickStack[i - 1] += nTicks; + } + MP_ASSERT(nTicks >= (uint64_t)nChildTicks); + + uint32_t nTimer = (uint32_t)MicroProfileLogGetTimerIndex(LE); + uint32_t nGroup = pTimerToGroup[nTimer]; + + bool bGroupRoot = 0 == GroupStackPos[nGroup] || 0 == --GroupStackPos[nGroup]; + bool bTimerRoot = 0 == TimerStackPos[nTimer] || 0 == --TimerStackPos[nTimer]; + + uint64_t nTicksExclusive = (nTicks - nChildTicks); + S.FrameExclusive[nTimer] += nTicksExclusive; + pFrameGroupThread[nGroup].nTicksExclusive += nTicksExclusive; + if(bTimerRoot) + { + S.Frame[nTimer].nTicks += nTicks; + S.Frame[nTimer].nCount += 1; + + MP_ASSERT(nGroup < MICROPROFILE_MAX_GROUPS); + if(bGroupRoot) + { + pFrameGroupThread[nGroup].nTicks += nTicks; + pFrameGroupThread[nGroup].nCount += 1; + } + } + } +#ifdef MP_ASSERT + for(uint8_t& g : GroupStackPos) + { + MP_ASSERT(g == 0); + } + for(uint8_t& g : TimerStackPos) + { + MP_ASSERT(g == 0); + } +#endif + + pLog->nStackPos = nStackPos; + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + pLog->nGroupTicks[j] += pFrameGroupThread[j].nTicks; + + if((S.FrameGroupThreadValid[idx_thread / 32] & (1 << (idx_thread % 32))) != 0) + { + pFrameGroup[j].nTicks += pFrameGroupThread[j].nTicks; + pFrameGroup[j].nTicksExclusive += pFrameGroupThread[j].nTicksExclusive; + pFrameGroup[j].nCount += pFrameGroupThread[j].nCount; + } + } + + if(pLog->nPut == pLog->nGet && pLog->nActive == 2) + { + pLog->nIdleFrames++; + } + else + { + pLog->nIdleFrames = 0; + } + if(pLog->nActive == 2 && pLog->nIdleFrames > MICROPROFILE_THREAD_LOG_FRAMES_REUSE) + { + MicroProfileLogReset(pLog); + } + } + } + { + MICROPROFILE_SCOPE(g_MicroProfileAccumulate); + uint64_t* ExtraPut = nullptr; + if(UsingExtraData) + { + ExtraPut = &ExtraData->Timers[0]; + ExtraData->NumTimers = S.nTotalTimers; + } + + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + S.AccumTimers[i].nTicks += S.Frame[i].nTicks; + S.AccumTimers[i].nCount += S.Frame[i].nCount; + S.AccumMaxTimers[i] = MicroProfileMax(S.AccumMaxTimers[i], S.Frame[i].nTicks); + S.AccumMinTimers[i] = MicroProfileMin(S.AccumMinTimers[i], S.Frame[i].nTicks); + S.AccumTimersExclusive[i] += S.FrameExclusive[i]; + S.AccumMaxTimersExclusive[i] = MicroProfileMax(S.AccumMaxTimersExclusive[i], S.FrameExclusive[i]); + if(ExtraPut) + *ExtraPut++ = S.Frame[i].nTicks; + } + ExtraPut = nullptr; + if(UsingExtraData) + { + ExtraPut = &ExtraData->Groups[0]; + ExtraData->NumGroups = S.nGroupCount; + } + + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + { + S.AccumGroup[i] += pFrameGroup[i].nTicks; + S.AccumGroupMax[i] = MicroProfileMax(S.AccumGroupMax[i], pFrameGroup[i].nTicks); + if(ExtraPut) + *ExtraPut++ = pFrameGroup[i].nTicks; + } +#if MICROPROFILE_IMGUI + void MicroProfileImguiGather(); + MicroProfileImguiGather(); +#endif + if(S.CsvConfig.State == MicroProfileCsvConfig::ACTIVE) + { + uint32_t FrameIndex = S.nFrameCurrent % MICROPROFILE_MAX_FRAME_HISTORY; + uint64_t* FrameData = S.CsvConfig.FrameData + S.CsvConfig.TotalElements * FrameIndex; + { + uint16_t* TimerIndices = S.CsvConfig.TimerIndices; + for(uint32_t i = 0; i < S.CsvConfig.NumTimers; ++i) + { + uint16_t Index = TimerIndices[i]; + if(Index != UINT16_MAX) + { + *FrameData = S.Frame[Index].nTicks; + } + else + { + *FrameData = 0; + } + FrameData++; + } + } + { + uint16_t* GroupIndices = S.CsvConfig.GroupIndices; + for(uint32_t i = 0; i < S.CsvConfig.NumGroups; ++i) + { + uint16_t Index = GroupIndices[i]; + if(Index != UINT16_MAX) + { + *FrameData = pFrameGroup[Index].nTicks; + } + else + { + *FrameData = 0; + } + FrameData++; + } + } + { + uint16_t* CounterIndices = S.CsvConfig.CounterIndices; + for(uint32_t i = 0; i < S.CsvConfig.NumCounters; ++i) + { + uint16_t Index = CounterIndices[i]; + if(Index != UINT16_MAX) + { + if(S.CounterInfo[Index].nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) + { + double d = S.CountersDouble[Index].load(); + memcpy(FrameData, &d, sizeof(d)); + } + else + { + *FrameData = S.Counters[Index].load(); + } + } + else + { + *FrameData = 0; + } + FrameData++; + } + } + } + } + for(uint32_t i = 0; i < MICROPROFILE_MAX_GRAPHS; ++i) + { + if(S.Graph[i].nToken != MICROPROFILE_INVALID_TOKEN) + { + MicroProfileToken nToken = S.Graph[i].nToken; + S.Graph[i].nHistory[S.nGraphPut] = S.Frame[MicroProfileGetTimerIndex(nToken)].nTicks; + } + } + S.nGraphPut = (S.nGraphPut + 1) % MICROPROFILE_GRAPH_HISTORY; + } + + if(S.nAggregateFlip <= ++S.nAggregateFlipCount) + { + nAggregateFlip = 1; + if(S.nAggregateFlip) // if 0 accumulate indefinitely + { + nAggregateClear = 1; + } + } + + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + MicroProfileThreadLog* pLog = S.Pool[i]; + uint32_t nNewGet = pFrameNext->nLogStart[i]; + + if(pLog && nNewGet != (uint32_t)-1) + { + pLog->nGet.store(nNewGet); + } + } + if(pFrameNext->nLogStartTimeline != (uint32_t)-1) + { + S.TimelineLog.nGet.store(pFrameNext->nLogStartTimeline); + } + } + if(nAggregateFlip) + { + memcpy(&S.Aggregate[0], &S.AccumTimers[0], sizeof(S.Aggregate[0]) * S.nTotalTimers); + memcpy(&S.AggregateMax[0], &S.AccumMaxTimers[0], sizeof(S.AggregateMax[0]) * S.nTotalTimers); + memcpy(&S.AggregateMin[0], &S.AccumMinTimers[0], sizeof(S.AggregateMin[0]) * S.nTotalTimers); + memcpy(&S.AggregateExclusive[0], &S.AccumTimersExclusive[0], sizeof(S.AggregateExclusive[0]) * S.nTotalTimers); + memcpy(&S.AggregateMaxExclusive[0], &S.AccumMaxTimersExclusive[0], sizeof(S.AggregateMaxExclusive[0]) * S.nTotalTimers); + + memcpy(&S.AggregateGroup[0], &S.AccumGroup[0], sizeof(S.AggregateGroup)); + memcpy(&S.AggregateGroupMax[0], &S.AccumGroupMax[0], sizeof(S.AggregateGroup)); + + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + MicroProfileThreadLog* pLog = S.Pool[i]; + if(!pLog) + continue; + + memcpy(&pLog->nAggregateGroupTicks[0], &pLog->nGroupTicks[0], sizeof(pLog->nAggregateGroupTicks)); + + if(nAggregateClear) + { + memset(&pLog->nGroupTicks[0], 0, sizeof(pLog->nGroupTicks)); + } + } + + S.nAggregateFrames = S.nAggregateFlipCount; + S.nFlipAggregateDisplay = S.nFlipAggregate; + S.nFlipMaxDisplay = S.nFlipMax; + if(nAggregateClear) + { + memset(&S.AccumTimers[0], 0, sizeof(S.Aggregate[0]) * S.nTotalTimers); + memset(&S.AccumMaxTimers[0], 0, sizeof(S.AccumMaxTimers[0]) * S.nTotalTimers); + memset(&S.AccumMinTimers[0], 0xFF, sizeof(S.AccumMinTimers[0]) * S.nTotalTimers); + memset(&S.AccumTimersExclusive[0], 0, sizeof(S.AggregateExclusive[0]) * S.nTotalTimers); + memset(&S.AccumMaxTimersExclusive[0], 0, sizeof(S.AccumMaxTimersExclusive[0]) * S.nTotalTimers); + memset(&S.AccumGroup[0], 0, sizeof(S.AggregateGroup)); + memset(&S.AccumGroupMax[0], 0, sizeof(S.AggregateGroup)); + + S.nAggregateFlipCount = 0; + S.nFlipAggregate = 0; + S.nFlipMax = 0; + + S.nAggregateFlipTick = MP_TICK(); + } + +#if MICROPROFILE_COUNTER_HISTORY + int64_t* pDest = &S.nCounterHistory[S.nCounterHistoryPut][0]; + S.nCounterHistoryPut = (S.nCounterHistoryPut + 1) % MICROPROFILE_GRAPH_HISTORY; + for(uint32_t i = 0; i < S.nNumCounters; ++i) + { + if(0 != (S.CounterInfo[i].nFlags & MICROPROFILE_COUNTER_FLAG_DETAILED)) + { + MicroProfileFetchCounter(i); + bool IsDouble = (S.CounterInfo[i].nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) != 0; + if(IsDouble) + { + double dValue = S.CountersDouble[i].load(std::memory_order_relaxed); + memcpy(&pDest[i], &dValue, sizeof(dValue)); + S.dCounterMin[i] = MicroProfileMin(S.dCounterMin[i], dValue); + S.dCounterMax[i] = MicroProfileMax(S.dCounterMax[i], dValue); + } + else + { + uint64_t nValue = S.Counters[i].load(std::memory_order_relaxed); + pDest[i] = nValue; + S.nCounterMin[i] = MicroProfileMin(S.nCounterMin[i], (int64_t)nValue); + S.nCounterMax[i] = MicroProfileMax(S.nCounterMax[i], (int64_t)nValue); + } + } + } +#endif + } + S.nAggregateClear = 0; + + MicroProfileFlipEnabled(); +} + +void MicroProfileSetEnableAllGroups(int bEnable) +{ + if(bEnable) + { + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUP_INTS; ++i) + { + S.nActiveGroupsWanted[i] = S.nGroupMask[i]; + } + S.nStartEnabled = 1; + MicroProfileFlipEnabled(); + } + else + { + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUP_INTS; ++i) + { + S.nActiveGroupsWanted[i] = 0; + } + S.nStartEnabled = 0; + MicroProfileFlipEnabled(); + } +} +void MicroProfileEnableCategory(const char* pCategory, int bEnabled) +{ + int nCategoryIndex = -1; + for(uint32_t i = 0; i < S.nCategoryCount; ++i) + { + if(!MP_STRCASECMP(pCategory, S.CategoryInfo[i].pName)) + { + nCategoryIndex = (int)i; + break; + } + } + if(nCategoryIndex >= 0) + { + if(bEnabled) + { + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUP_INTS; ++i) + { + S.nActiveGroupsWanted[i] |= S.CategoryInfo[nCategoryIndex].nGroupMask[i]; + } + } + else + { + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUP_INTS; ++i) + { + S.nActiveGroupsWanted[i] &= ~S.CategoryInfo[nCategoryIndex].nGroupMask[i]; + } + } + } +} + +void MicroProfileEnableCategory(const char* pCategory) +{ + MicroProfileEnableCategory(pCategory, true); +} +void MicroProfileDisableCategory(const char* pCategory) +{ + MicroProfileEnableCategory(pCategory, false); +} + +int MicroProfileGetEnableAllGroups() +{ + return 0 == memcmp(S.nGroupMask, S.nActiveGroupsWanted, sizeof(S.nGroupMask)); +} + +void MicroProfileSetForceMetaCounters(int bForce) +{ +} + +int MicroProfileGetForceMetaCounters() +{ + + return 0; +} + +void MicroProfileEnableMetaCounter(const char* pMeta) +{ +} + +void MicroProfileDisableMetaCounter(const char* pMeta) +{ +} + +void MicroProfileSetAggregateFrames(int nFrames) +{ + S.nAggregateFlip = (uint32_t)nFrames; + if(0 == nFrames) + { + S.nAggregateClear = 1; + } +} + +int MicroProfileGetAggregateFrames() +{ + return S.nAggregateFlip; +} + +int MicroProfileGetCurrentAggregateFrames() +{ + return int(S.nAggregateFlip ? S.nAggregateFlip : S.nAggregateFlipCount); +} + +void MicroProfileForceEnableGroup(const char* pGroup, MicroProfileTokenType Type) +{ + MicroProfileInit(); + std::lock_guard Lock(MicroProfileMutex()); + uint16_t nGroup = MicroProfileGetGroup(pGroup, Type); + uint32_t nIndex = nGroup / 32; + uint32_t nBit = nGroup % 32; + S.nForceGroups[nIndex] |= (1ll << nBit); +} + +void MicroProfileForceDisableGroup(const char* pGroup, MicroProfileTokenType Type) +{ + MicroProfileInit(); + std::lock_guard Lock(MicroProfileMutex()); + uint16_t nGroup = MicroProfileGetGroup(pGroup, Type); + uint32_t nIndex = nGroup / 32; + uint32_t nBit = nGroup % 32; + + S.nForceGroups[nIndex] &= ~(1ll << nBit); +} + +struct MicroProfileTimerValues +{ + float TimeMs; + float AverageMs; + float MaxMs; + float MinMs; + float CallAverageMs; + float ExclusiveMs; + float AverageExclusiveMs; + float MaxExclusiveMs; + float TotalMs; + uint32_t nCount; +}; + +void MicroProfileCalcTimers(int nTimer, MicroProfileTimerValues& Out) +{ + const uint32_t nGroupId = S.TimerInfo[nTimer].nGroupIndex; + const float fToMs = MicroProfileTickToMsMultiplier(S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); + uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; + uint32_t nAggregateCount = S.Aggregate[nTimer].nCount ? S.Aggregate[nTimer].nCount : 1; + Out.nCount = S.Aggregate[nTimer].nCount; + + float fToPrc = S.fRcpReferenceTime; + float fMs = fToMs * (S.Frame[nTimer].nTicks); + + float fAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateFrames); + float fMaxMs = fToMs * (S.AggregateMax[nTimer]); + float fMinMs = fToMs * (S.AggregateMin[nTimer] != uint64_t(-1) ? S.AggregateMin[nTimer] : 0); + float fCallAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateCount); + float fMsExclusive = fToMs * (S.FrameExclusive[nTimer]); + float fAverageMsExclusive = fToMs * (S.AggregateExclusive[nTimer] / nAggregateFrames); + float fMaxMsExclusive = fToMs * (S.AggregateMaxExclusive[nTimer]); + float fTotalMs = fToMs * S.Aggregate[nTimer].nTicks; + + Out.TimeMs = fMs; + Out.AverageMs = fAverageMs; + Out.MaxMs = fMaxMs; + Out.MinMs = fMinMs; + Out.CallAverageMs = fCallAverageMs; + Out.ExclusiveMs = fMsExclusive; + Out.AverageExclusiveMs = fAverageMsExclusive; + Out.MaxExclusiveMs = fMaxMsExclusive; + Out.TotalMs = fTotalMs; +} + +void MicroProfileCalcAllTimers( + float* pTimers, float* pAverage, float* pMax, float* pMin, float* pCallAverage, float* pExclusive, float* pAverageExclusive, float* pMaxExclusive, float* pTotal, uint32_t nSize) +{ + for(uint32_t i = 0; i < S.nTotalTimers && i < nSize; ++i) + { + const uint32_t nGroupId = S.TimerInfo[i].nGroupIndex; + const float fToMs = MicroProfileTickToMsMultiplier(S.GroupInfo[nGroupId].Type == MicroProfileTokenTypeGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); + uint32_t nTimer = i; + uint32_t nIdx = i * 2; + uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; + uint32_t nAggregateCount = S.Aggregate[nTimer].nCount ? S.Aggregate[nTimer].nCount : 1; + float fToPrc = S.fRcpReferenceTime; + float fMs = fToMs * (S.Frame[nTimer].nTicks); + float fPrc = MicroProfileMin(fMs * fToPrc, 1.f); + float fAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateFrames); + float fAveragePrc = MicroProfileMin(fAverageMs * fToPrc, 1.f); + float fMaxMs = fToMs * (S.AggregateMax[nTimer]); + float fMaxPrc = MicroProfileMin(fMaxMs * fToPrc, 1.f); + float fMinMs = fToMs * (S.AggregateMin[nTimer] != uint64_t(-1) ? S.AggregateMin[nTimer] : 0); + float fMinPrc = MicroProfileMin(fMinMs * fToPrc, 1.f); + float fCallAverageMs = fToMs * (S.Aggregate[nTimer].nTicks / nAggregateCount); + float fCallAveragePrc = MicroProfileMin(fCallAverageMs * fToPrc, 1.f); + float fMsExclusive = fToMs * (S.FrameExclusive[nTimer]); + float fPrcExclusive = MicroProfileMin(fMsExclusive * fToPrc, 1.f); + float fAverageMsExclusive = fToMs * (S.AggregateExclusive[nTimer] / nAggregateFrames); + float fAveragePrcExclusive = MicroProfileMin(fAverageMsExclusive * fToPrc, 1.f); + float fMaxMsExclusive = fToMs * (S.AggregateMaxExclusive[nTimer]); + float fMaxPrcExclusive = MicroProfileMin(fMaxMsExclusive * fToPrc, 1.f); + float fTotalMs = fToMs * S.Aggregate[nTimer].nTicks; + pTimers[nIdx] = fMs; + pTimers[nIdx + 1] = fPrc; + pAverage[nIdx] = fAverageMs; + pAverage[nIdx + 1] = fAveragePrc; + pMax[nIdx] = fMaxMs; + pMax[nIdx + 1] = fMaxPrc; + pMin[nIdx] = fMinMs; + pMin[nIdx + 1] = fMinPrc; + pCallAverage[nIdx] = fCallAverageMs; + pCallAverage[nIdx + 1] = fCallAveragePrc; + pExclusive[nIdx] = fMsExclusive; + pExclusive[nIdx + 1] = fPrcExclusive; + pAverageExclusive[nIdx] = fAverageMsExclusive; + pAverageExclusive[nIdx + 1] = fAveragePrcExclusive; + pMaxExclusive[nIdx] = fMaxMsExclusive; + pMaxExclusive[nIdx + 1] = fMaxPrcExclusive; + pTotal[nIdx] = fTotalMs; + pTotal[nIdx + 1] = 0.f; + } +} + +float MicroProfileGetTime(const char* pGroup, const char* pName) +{ + MicroProfileToken nToken = MicroProfileFindTokenInternal(pGroup, pName); + if(nToken == MICROPROFILE_INVALID_TOKEN) + { + return 0.f; + } + uint32_t nTimerIndex = MicroProfileGetTimerIndex(nToken); + uint32_t nGroupIndex = MicroProfileGetGroupIndex(nToken); + float fToMs = MicroProfileTickToMsMultiplier(S.GroupInfo[nGroupIndex].Type == MicroProfileTokenTypeGpu ? MicroProfileTicksPerSecondGpu() : MicroProfileTicksPerSecondCpu()); + return S.Frame[nTimerIndex].nTicks * fToMs; +} + +int MicroProfilePlatformMarkersGetEnabled() +{ + return S.nPlatformMarkersEnabled != 0 ? 1 : 0; +} +void MicroProfilePlatformMarkersSetEnabled(int bEnabled) +{ + S.nPlatformMarkersEnabled = bEnabled ? 1 : 0; +} + +#define MICROPROFILE_CONTEXT_SWITCH_SEARCH_DEBUG MICROPROFILE_DEBUG + +void MicroProfileContextSwitchSearch(uint32_t* pContextSwitchStart, uint32_t* pContextSwitchEnd, uint64_t nBaseTicksCpu, uint64_t nBaseTicksEndCpu) +{ + MICROPROFILE_SCOPE(g_MicroProfileContextSwitchSearch); + uint32_t nContextSwitchPut = S.nContextSwitchPut; + uint64_t nContextSwitchStart, nContextSwitchEnd; + nContextSwitchStart = nContextSwitchEnd = (nContextSwitchPut + MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE - 1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; + int64_t nSearchEnd = nBaseTicksEndCpu + MicroProfileMsToTick(30.f, MicroProfileTicksPerSecondCpu()); + int64_t nSearchBegin = nBaseTicksCpu - MicroProfileMsToTick(30.f, MicroProfileTicksPerSecondCpu()); + +#if MICROPROFILE_CONTEXT_SWITCH_SEARCH_DEBUG + int64_t lp = S.nContextSwitchLastPushed; + uprintf("cswitch-search\n"); + uprintf("Begin %" PRId64 " End %" PRId64 " Last %" PRId64 "\n", nSearchBegin, nSearchEnd, lp); + + float fToMs = MicroProfileTickToMsMultiplierCpu(); + uprintf("E %6.2fms\n", fToMs * (nSearchEnd - nSearchBegin)); + uprintf("LAST %6.2fms\n", fToMs * (lp - nSearchBegin)); +#endif + + int64_t nMax = INT64_MIN; + int64_t nMin = INT64_MAX; + for(uint32_t i = 0; i < MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; ++i) + { + uint32_t nIndex = (nContextSwitchPut + MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE - (i + 1)) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; + MicroProfileContextSwitch& CS = S.ContextSwitch[nIndex]; + if(nMax < CS.nTicks) + nMax = CS.nTicks; + if(nMin > CS.nTicks && CS.nTicks != 0) + nMin = CS.nTicks; + if(CS.nTicks > nSearchEnd) + { + nContextSwitchEnd = nIndex; + } + if(CS.nTicks > nSearchBegin) + { + nContextSwitchStart = nIndex; + } + } + *pContextSwitchStart = nContextSwitchStart; + *pContextSwitchEnd = nContextSwitchEnd; + +#if MICROPROFILE_CONTEXT_SWITCH_SEARCH_DEBUG + { + uprintf("contextswitch start %" PRId64 " %" PRId64 "\n", nContextSwitchStart, nContextSwitchEnd); + + MicroProfileContextSwitch& CS0 = S.ContextSwitch[0]; + int64_t nMax = CS0.nTicks; + int64_t nMin = CS0.nTicks; + int64_t nBegin = 0; + int64_t nEnd = 0; + int nRanges = 0; + for(uint32_t i = 0; i < MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; i += 1024) + { + int64_t MinTick = INT64_MAX; + int64_t MaxTick = INT64_MIN; + for(int j = 0; j < 1024; ++j) + { + MicroProfileContextSwitch& CS = S.ContextSwitch[i + j]; + int64_t nTicks = CS.nTicks; + MinTick = MicroProfileMin(nTicks, MinTick); + MaxTick = MicroProfileMax(nTicks, MaxTick); + } + + uprintf("XX range [%5" PRIx64 ":%5" PRIx64 "] :: [%6.2f:%6.2f] [%p :: %p] .. ref %p\n", + i, + i + 1024, + fToMs * (MinTick - nSearchBegin), + fToMs * (MaxTick - nSearchBegin), + (void*)MinTick, + (void*)MaxTick, + (void*)nSearchBegin + + ); + } + uprintf("\n\n"); + + for(uint32_t i = 0; i < MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE; ++i) + { + MicroProfileContextSwitch& CS = S.ContextSwitch[i]; + int64_t nTicks = CS.nTicks; + float fMs = (nTicks - nMax) * fToMs; + if(fMs < 0 || fMs > 50) + { + // dump range here + uprintf("range [%5" PRId64 ":%5" PRId64 "] :: [%6.2f:%6.2f] [%p :: %p] .. ref %p\n", + nBegin, + nEnd, + fToMs * (nMin - nSearchBegin), + fToMs * (nMax - nSearchBegin), + (void*)nMin, + (void*)nMax, + (void*)nSearchBegin + + ); + + nEnd = nBegin = i; + nMax = nMin = CS.nTicks; + nRanges++; + } + else + { + nEnd = i; + nMax = MicroProfileMax(nTicks, nMax); + } + } + } + + lp = S.nContextSwitchLastPushed; + uprintf("E %6.2fms\n", fToMs * (nSearchEnd - nSearchBegin)); + uprintf("LP2 %6.2fms\n", fToMs * (lp - nSearchBegin)); +#endif +} + +int MicroProfileFormatCounter(int eFormat, int64_t nCounter, char* pOut, uint32_t nBufferSize) +{ + if(!nCounter) + { + pOut[0] = '0'; + pOut[1] = '\0'; + return 1; + } + int nLen = 0; + char* pBase = pOut; + char* pTmp = pOut; + char* pEnd = pOut + nBufferSize; + int nNegative = 0; + if(nCounter < 0) + { + nCounter = -nCounter; + nNegative = 1; + if(nCounter < 0) // handle INT_MIN + { + nCounter = -(nCounter + 1); + } + } + + switch(eFormat) + { + case MICROPROFILE_COUNTER_FORMAT_DEFAULT: + { + int nSeperate = 0; + while(nCounter) + { + if(nSeperate) + { + *pTmp++ = '.'; + } + nSeperate = 1; + for(uint32_t i = 0; nCounter && i < 3; ++i) + { + int nDigit = nCounter % 10; + nCounter /= 10; + *pTmp++ = '0' + nDigit; + } + } + if(nNegative) + { + *pTmp++ = '-'; + } + nLen = pTmp - pOut; + --pTmp; + MP_ASSERT(pTmp <= pEnd); + while(pTmp > pOut) // reverse string + { + char c = *pTmp; + *pTmp = *pOut; + *pOut = c; + pTmp--; + pOut++; + } + } + break; + case MICROPROFILE_COUNTER_FORMAT_BYTES: + { + const char* pExt[] = { "b", "kb", "mb", "gb", "tb", "pb", "eb", "zb", "yb" }; + size_t nNumExt = sizeof(pExt) / sizeof(pExt[0]); + int64_t nShift = 0; + int64_t nDivisor = 1; + int64_t nCountShifted = nCounter >> 10; + while(nCountShifted) + { + nDivisor <<= 10; + nCountShifted >>= 10; + nShift++; + } + MP_ASSERT(nShift < (int64_t)nNumExt); + if(nShift) + { + nLen = snprintf(pOut, nBufferSize - 1, "%c%3.2f%s", nNegative ? '-' : ' ', (double)nCounter / nDivisor, pExt[nShift]); + } + else + { + nLen = snprintf(pOut, nBufferSize - 1, "%c%" PRId64 "%s", nNegative ? '-' : ' ', nCounter, pExt[nShift]); + } + } + break; + } + pBase[nLen] = '\0'; + + return nLen; +} + +int MicroProfileFormatCounterDouble(int eFormat, double dCounter, char* pOut, uint32_t nBufferSize) +{ + int nLen = 0; + switch(eFormat) + { + case MICROPROFILE_COUNTER_FORMAT_DEFAULT: + { + nLen = stbsp_snprintf(pOut, nBufferSize - 1, "%f", dCounter); + } + break; + case MICROPROFILE_COUNTER_FORMAT_BYTES: + { + const char* pExt[] = { "b", "kb", "mb", "gb", "tb", "pb", "eb", "zb", "yb" }; + double scale = 1.f; + int offset = 0; + int end = sizeof(pExt) / sizeof(pExt[0]); + double d = dCounter; + while(d / scale > 1024.f && offset + 1 < end) + { + scale *= 1024.f; + offset += 1; + } + nLen = stbsp_snprintf(pOut, nBufferSize - 1, "%.3f%s", d / scale, pExt[offset]); + } + break; + } + pOut[nLen] = '\0'; + + return nLen; +} + +bool MicroProfileAnyGroupActive() +{ + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUP_INTS; ++i) + { + if(S.nActiveGroups[i] != 0) + return true; + } + return false; +} +bool MicroProfileGroupActive(uint32_t nGroupIndex) +{ + MP_ASSERT(nGroupIndex < MICROPROFILE_MAX_GROUPS); + uint32_t nIndex = nGroupIndex / 32; + uint32_t nBit = nGroupIndex % 32; + return ((S.nActiveGroups[nIndex] >> nBit) & 1) == 1; +} + +void MicroProfileToggleGroup(uint32_t nGroup) +{ + if(nGroup < S.nGroupCount) + { + uint32_t nIndex = nGroup / 32; + uint32_t nBit = nGroup % 32; + S.nActiveGroupsWanted[nIndex] ^= (1ll << nBit); + S.nWebSocketDirty |= MICROPROFILE_WEBSOCKET_DIRTY_ENABLED; + } +} +void MicroProfileGroupSetEnabled(uint32_t nGroup) +{ + if(nGroup < S.nGroupCount) + { + uint32_t nIndex = nGroup / 32; + uint32_t nBit = nGroup % 32; + S.nActiveGroupsWanted[nIndex] |= (1ll << nBit); + S.nWebSocketDirty |= MICROPROFILE_WEBSOCKET_DIRTY_ENABLED; + } +} +bool MicroProfileGroupEnabled(uint32_t nGroup) +{ + if(nGroup < S.nGroupCount) + { + uint32_t nIndex = nGroup / 32; + uint32_t nBit = nGroup % 32; + return 0 != (S.nActiveGroupsWanted[nIndex] & (1ll << nBit)); + } + return false; +} +bool MicroProfileCategoryEnabled(uint32_t nCategory) +{ + if(nCategory < S.nCategoryCount) + { + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUP_INTS; ++i) + { + if(S.CategoryInfo[nCategory].nGroupMask[i] != (S.CategoryInfo[nCategory].nGroupMask[i] & S.nActiveGroupsWanted[i])) + { + return false; + } + } + return true; + } + return false; +} + +bool MicroProfileCategoryDisabled(uint32_t nCategory) +{ + if(nCategory < S.nCategoryCount) + { + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUP_INTS; ++i) + { + uint32_t ActiveMask = S.nActiveGroupsWanted[i]; + uint32_t CategoryMask = S.CategoryInfo[nCategory].nGroupMask[i]; + + if(0 != (ActiveMask & CategoryMask)) + { + return false; + } + } + return true; + } + return false; +} + +void MicroProfileToggleCategory(uint32_t nCategory) +{ + if(nCategory < S.nCategoryCount) + { + bool bAllSet = true; + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUP_INTS; ++i) + { + bAllSet = bAllSet && S.CategoryInfo[nCategory].nGroupMask[i] == (S.CategoryInfo[nCategory].nGroupMask[i] & S.nActiveGroupsWanted[i]); + } + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUP_INTS; ++i) + { + if(bAllSet) + { + S.nActiveGroupsWanted[i] &= ~S.CategoryInfo[nCategory].nGroupMask[i]; + } + else + { + S.nActiveGroupsWanted[i] |= S.CategoryInfo[nCategory].nGroupMask[i]; + } + } + S.nWebSocketDirty |= MICROPROFILE_WEBSOCKET_DIRTY_ENABLED; + } +} + +void MicroProfileSleep(uint32_t nMs) +{ +#ifdef _WIN32 + Sleep(nMs); +#else + usleep(nMs * 1000); +#endif +} + +#if MICROPROFILE_WEBSERVER + +#define MICROPROFILE_EMBED_HTML + +extern const char* g_MicroProfileHtml_begin[]; +extern size_t g_MicroProfileHtml_begin_sizes[]; +extern size_t g_MicroProfileHtml_begin_count; +extern const char* g_MicroProfileHtml_end[]; +extern size_t g_MicroProfileHtml_end_sizes[]; +extern size_t g_MicroProfileHtml_end_count; + +extern const char* g_MicroProfileHtmlLive_begin[]; +extern size_t g_MicroProfileHtmlLive_begin_sizes[]; +extern size_t g_MicroProfileHtmlLive_begin_count; +extern const char* g_MicroProfileHtmlLive_end[]; +extern size_t g_MicroProfileHtmlLive_end_sizes[]; +extern size_t g_MicroProfileHtmlLive_end_count; + +extern const uint32_t uprof_16[]; +extern const uint32_t uprof_16_len; +extern const uint32_t uprof_32[]; +extern const uint32_t uprof_32_len; +extern const uint32_t uprof_192[]; +extern const uint32_t uprof_192_len; +extern const uint32_t uprof_512[]; +extern const uint32_t uprof_512_len; + +typedef void (*MicroProfileWriteCallback)(void* Handle, size_t size, const char* pData); + +uint32_t MicroProfileWebServerPort() +{ + return S.nWebServerPort; +} + +void MicroProfileSetWebServerPort(uint32_t nPort) +{ + if(S.nWebServerPort != nPort) + { + MicroProfileWebServerJoin(); + MicroProfileWebServerStop(); + S.nWebServerPort = nPort; + S.nWebServerDataSent = (uint64_t)-1; // Will cause the web server and its thread to be restarted next time MicroProfileFlip() is called. + } +} + +void MicroProfileDumpFileImmediately(const char* pHtml, const char* pCsv, void* pGpuContext, uint32_t FrameCount) +{ + for(uint32_t i = 0; i < 2; ++i) + { + MicroProfileFlip(pGpuContext); + } + for(uint32_t i = 0; i < MICROPROFILE_GPU_FRAME_DELAY + 1; ++i) + { + MicroProfileFlip(pGpuContext); + } + + uint32_t nDumpMask = 0; + if(pHtml) + { + + size_t nLen = strlen(pHtml); + if(nLen > sizeof(S.HtmlDumpPath) - 1) + { + return; + } + const size_t ExtSize = sizeof(".html") - 1; + if(nLen > ExtSize && 0 == memcmp(".html", pHtml + nLen - ExtSize, ExtSize)) + nLen -= ExtSize; + memcpy(S.HtmlDumpPath, pHtml, nLen); + S.HtmlDumpPath[nLen] = '\0'; + + nDumpMask |= 1; + } + if(pCsv) + { + size_t nLen = strlen(pCsv); + if(nLen > sizeof(S.CsvDumpPath) - 1) + { + return; + } + const size_t ExtSize = sizeof(".csv") - 1; + if(nLen > ExtSize && 0 == memcmp(".csv", pCsv + nLen - ExtSize, ExtSize)) + nLen -= ExtSize; + memcpy(S.CsvDumpPath, pCsv, nLen + 1); + S.CsvDumpPath[nLen] = '\0'; + + nDumpMask |= 2; + } + std::lock_guard Lock(MicroProfileMutex()); + S.nDumpFileNextFrame = nDumpMask; + S.nDumpSpikeMask = 0; + S.nDumpFileCountDown = 0; + S.DumpFrameCount = FrameCount; + + MicroProfileDumpToFile(); +} +void MicroProfileDumpFile(const char* pHtml, const char* pCsv, float fCpuSpike, float fGpuSpike, uint32_t FrameCount) +{ + S.fDumpCpuSpike = fCpuSpike; + S.fDumpGpuSpike = fGpuSpike; + S.DumpFrameCount = FrameCount; + uint32_t nDumpMask = 0; + if(pHtml) + { + size_t nLen = strlen(pHtml); + if(nLen > sizeof(S.HtmlDumpPath) - 1) + { + return; + } + const size_t ExtSize = sizeof(".html") - 1; + if(nLen > ExtSize && 0 == memcmp(".html", pHtml + nLen - ExtSize, ExtSize)) + nLen -= ExtSize; + memcpy(S.HtmlDumpPath, pHtml, nLen); + S.HtmlDumpPath[nLen] = '\0'; + + nDumpMask |= 1; + } + if(pCsv) + { + size_t nLen = strlen(pCsv); + if(nLen > sizeof(S.CsvDumpPath) - 1) + { + return; + } + const size_t ExtSize = sizeof(".csv") - 1; + if(nLen > ExtSize && 0 == memcmp(".csv", pCsv + nLen - ExtSize, ExtSize)) + nLen -= ExtSize; + memcpy(S.CsvDumpPath, pCsv, nLen); + S.CsvDumpPath[nLen] = '\0'; + + nDumpMask |= 2; + } + if(fCpuSpike > 0.f || fGpuSpike > 0.f) + { + S.nDumpFileNextFrame = 0; + S.nDumpSpikeMask = nDumpMask; + } + else + { + std::lock_guard Lock(MicroProfileMutex()); + S.nDumpFileNextFrame = nDumpMask; + S.nDumpSpikeMask = 0; + S.nDumpFileCountDown = 0; + + MicroProfileDumpToFile(); + } +} + +struct MicroProfilePrintfArgs +{ + MicroProfileWriteCallback CB; + void* Handle; +}; + +char* MicroProfilePrintfCallback(const char* buf, void* user, int len) +{ + MicroProfilePrintfArgs* A = (MicroProfilePrintfArgs*)user; + (A->CB)(A->Handle, len, buf); + return const_cast(buf); +}; + +void MicroProfilePrintf(MicroProfileWriteCallback CB, void* Handle, const char* pFmt, ...) +{ + va_list args; + va_start(args, pFmt); + MicroProfilePrintfArgs A; + A.CB = CB; + A.Handle = Handle; + char Buffer[STB_SPRINTF_MIN]; + int size = stbsp_vsprintfcb(MicroProfilePrintfCallback, (void*)&A, Buffer, pFmt, args); + (void)size; + va_end(args); +} + +void MicroProfileGetFramesToDump(uint64_t nStartFrameId, uint32_t nMaxFrames, uint32_t& nFirstFrame, uint32_t& nLastFrame, uint32_t& nNumFrames) +{ + nFirstFrame = (uint32_t)-1; + nNumFrames = 0; + + if(nStartFrameId != (uint64_t)-1) + { + // search for the frane + for(uint32_t i = 0; i < MICROPROFILE_MAX_FRAME_HISTORY; ++i) + { + if(S.Frames[i].nFrameId == nStartFrameId) + { + nFirstFrame = i; + break; + } + } + if(nFirstFrame != (uint32_t)-1) + { + nLastFrame = S.nFrameCurrent; + uint32_t nDistance = (MICROPROFILE_MAX_FRAME_HISTORY + nFirstFrame - nLastFrame) % MICROPROFILE_MAX_FRAME_HISTORY; + nNumFrames = MicroProfileMin(nDistance, (uint32_t)nMaxFrames); + } + } + + if(nNumFrames == 0) + { + nNumFrames = (MICROPROFILE_MAX_FRAME_HISTORY - MICROPROFILE_GPU_FRAME_DELAY - 3); // leave a few to not overwrite + nNumFrames = MicroProfileMin(nNumFrames, (uint32_t)nMaxFrames); + nFirstFrame = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - nNumFrames) % MICROPROFILE_MAX_FRAME_HISTORY; + } + + nLastFrame = (nFirstFrame + nNumFrames) % MICROPROFILE_MAX_FRAME_HISTORY; +} + +#define printf(...) MicroProfilePrintf(CB, Handle, __VA_ARGS__) + +void MicroProfileDumpCsvWithConfig(MicroProfileWriteCallback CB, void* Handle, uint32_t nFirstFrame, uint32_t nLastFrame, uint32_t nNumFrames) +{ + uint32_t NumTimers = S.CsvConfig.NumTimers; + uint32_t NumGroups = S.CsvConfig.NumGroups; + uint32_t NumCounters = S.CsvConfig.NumCounters; + uint16_t* TimerIndices = S.CsvConfig.TimerIndices; + uint16_t* GroupIndices = S.CsvConfig.GroupIndices; + uint64_t* FrameData = S.CsvConfig.FrameData; + uint16_t* CounterIndices = S.CsvConfig.CounterIndices; + uint32_t TotalElements = S.CsvConfig.TotalElements; + uint32_t Offset = 0; + bool UseFrameTime = 0 != (MICROPROFILE_CSV_FLAG_FRAME_TIME & S.CsvConfig.Flags); + const char** pTimerNames = S.CsvConfig.pTimerNames; + const char** pGroupNames = S.CsvConfig.pGroupNames; + const char** pCounterNames = S.CsvConfig.pCounterNames; + if(UseFrameTime) + printf("Time"); + else + printf("FrameNumber"); + for(uint32_t i = 0; i < NumTimers; ++i, ++Offset) + printf(", %s", pTimerNames[i] ? pTimerNames[i] : S.TimerInfo[TimerIndices[i]].pName); + + for(uint32_t i = 0; i < NumGroups; ++i, ++Offset) + printf(", %s", pGroupNames[i] ? pGroupNames[i] : S.GroupInfo[GroupIndices[i]].pName); + for(uint32_t i = 0; i < NumCounters; ++i, ++Offset) + printf(", %s", pCounterNames[i] ? pCounterNames[i] : S.CounterInfo[CounterIndices[i]].pName); + printf("\n"); + + float* fToMsTimer = (float*)alloca(sizeof(float) * NumTimers); + float* fToMsGroup = (float*)alloca(sizeof(float) * NumGroups); + float fToMsCPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fToMsGPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); + + for(uint32_t i = 0; i < NumTimers; ++i) + fToMsTimer[i] = S.TimerInfo[TimerIndices[i]].Type == MicroProfileTokenTypeGpu ? fToMsGPU : fToMsCPU; + for(uint32_t i = 0; i < NumGroups; ++i) + fToMsGroup[i] = S.GroupInfo[GroupIndices[i]].Type == MicroProfileTokenTypeGpu ? fToMsGPU : fToMsCPU; + + uint64_t TickStart = S.Frames[nFirstFrame % MICROPROFILE_MAX_FRAME_HISTORY].nFrameStartCpu; + for(uint32_t i = 0; i < nNumFrames; ++i) + { + uint32_t FrameIndex = ((nFirstFrame + i) % MICROPROFILE_MAX_FRAME_HISTORY); + uint64_t TickFrame = S.Frames[FrameIndex].nFrameStartCpu; + uint64_t* Data = FrameData + TotalElements * FrameIndex; + if(UseFrameTime) + printf("%f", (TickFrame - TickStart) * fToMsCPU); + else + printf("%d", i); + Offset = 0; + for(uint32_t j = 0; j < NumTimers; ++j) + printf(", %f", Data[Offset++] * fToMsTimer[j]); + for(uint32_t j = 0; j < NumGroups; ++j) + printf(", %f", Data[Offset++] * fToMsGroup[j]); + for(uint32_t j = 0; j < NumCounters; ++j) + { + if(S.CounterInfo[CounterIndices[j]].nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) + { + printf(", %f", ((double*)Data)[Offset++]); + } + else + { + printf(", %lld", Data[Offset++]); + } + } + printf("\n"); + } +} +void MicroProfileDumpCsvTimerFrames(MicroProfileWriteCallback CB, void* Handle, uint32_t nFirstFrame, uint32_t nLastFrame, uint32_t nNumFrames) +{ + MP_ASSERT(S.FrameExtraCounterData); + uint32_t TotalTimers = S.nTotalTimers; + float* fToMs = (float*)alloca(sizeof(float) * TotalTimers); + float fToMsCPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fToMsGPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); + + for(uint32_t i = 0; i < TotalTimers; ++i) + fToMs[i] = S.TimerInfo[i].Type == MicroProfileTokenTypeGpu ? fToMsGPU : fToMsCPU; + + for(uint32_t i = 0; i < TotalTimers; ++i) + { + printf(i == 0 ? "FrameNumber, \"%s\"" : ",\"%s\"", S.TimerInfo[i].pName); + } + printf("\n"); + + for(uint32_t i = 0; i < nNumFrames; ++i) + { + // printf("%d", i) MicroProfileFrame& F = S.Frames[(i + nFirstFrame) % MICROPROFILE_MAX_FRAME_HISTORY]; + MicroProfileFrameExtraCounterData* Data = S.FrameExtraCounterData; + uint32_t NumTimers = 0; + uint32_t j; + printf("%d", i); + Data += ((i + nFirstFrame) % MICROPROFILE_MAX_FRAME_HISTORY); + NumTimers = MicroProfileMin(TotalTimers, (uint32_t)Data->NumTimers); + for(j = 0; j < NumTimers; ++j) + { + printf(",%f", Data->Timers[j] * fToMs[j]); + } + for(; j < TotalTimers; ++j) + printf(",0"); + printf("\n"); + } +} + +void MicroProfileDumpCsvGroupFrames(MicroProfileWriteCallback CB, void* Handle, uint32_t nFirstFrame, uint32_t nLastFrame, uint32_t nNumFrames) +{ + MP_ASSERT(S.FrameExtraCounterData); + float fToMsCPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fToMsGPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); + + uint32_t nGroupCount = S.nGroupCount; + + float* fToMs = (float*)alloca(sizeof(float) * nGroupCount); + for(uint32_t i = 0; i < nGroupCount; ++i) + fToMs[i] = S.GroupInfo[i].Type == MicroProfileTokenTypeGpu ? fToMsGPU : fToMsCPU; + + for(uint32_t i = 0; i < nGroupCount; ++i) + { + printf(i == 0 ? "FrameNumber, \"%s\"" : ",\"%s\"", S.GroupInfo[i].pName); + } + + printf("\n"); + for(uint32_t i = 0; i < nNumFrames; ++i) + { + MicroProfileFrameExtraCounterData* Data = S.FrameExtraCounterData; + uint32_t NumGroups = 0; + uint32_t j; + printf("%d", i); + Data += ((i + nFirstFrame) % MICROPROFILE_MAX_FRAME_HISTORY); + NumGroups = MicroProfileMin(nGroupCount, (uint32_t)Data->NumGroups); + for(j = 0; j < NumGroups; ++j) + { + printf(",%f", Data->Groups[j] * fToMs[j]); + } + for(; j < nGroupCount; ++j) + printf(",0"); + printf("\n"); + } +} + +void MicroProfileDumpCsv(uint32_t nDumpFrameCount) +{ + uint32_t nNumFrames, nFirstFrame, nLastFrame; + MicroProfileGetFramesToDump((uint64_t)-1, nDumpFrameCount, nFirstFrame, nLastFrame, nNumFrames); + + char Path[MICROPROFILE_MAX_PATH]; + int Length; + if(S.FrameExtraCounterData) + { + Length = snprintf(Path, sizeof(S.CsvDumpPath), "%s_timer_frames.csv", S.CsvDumpPath); + if(Length > 0 && Length < MICROPROFILE_MAX_PATH) + { + FILE* F = fopen(Path, "w"); + if(F) + { + MicroProfileDumpCsvTimerFrames(MicroProfileWriteFile, F, nFirstFrame, nLastFrame, nNumFrames); + fclose(F); + } + } + Length = snprintf(Path, sizeof(S.CsvDumpPath), "%s_group_frames.csv", S.CsvDumpPath); + if(Length > 0 && Length < MICROPROFILE_MAX_PATH) + { + FILE* F = fopen(Path, "w"); + if(F) + { + MicroProfileDumpCsvGroupFrames(MicroProfileWriteFile, F, nFirstFrame, nLastFrame, nNumFrames); + fclose(F); + } + } + } + if(S.CsvConfig.State == MicroProfileCsvConfig::ACTIVE) + { + Length = snprintf(Path, sizeof(S.CsvDumpPath), "%s_custom.csv", S.CsvDumpPath); + if(Length > 0 && Length < MICROPROFILE_MAX_PATH) + { + FILE* F = fopen(Path, "w"); + if(F) + { + MicroProfileDumpCsvWithConfig(MicroProfileWriteFile, F, nFirstFrame, nLastFrame, nNumFrames); + fclose(F); + } + } + } +} + +void MicroProfileDumpCsvLegacy(MicroProfileWriteCallback CB, void* Handle) +{ + uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; + float fToMsCPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fToMsGPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); + + printf("frames,%d\n", nAggregateFrames); + printf("group,name,average,max,callaverage\n"); + + uint32_t nNumTimers = S.nTotalTimers; + uint32_t nBlockSize = 2 * nNumTimers; + float* pTimers = (float*)alloca(nBlockSize * 9 * sizeof(float)); + float* pAverage = pTimers + nBlockSize; + float* pMax = pTimers + 2 * nBlockSize; + float* pMin = pTimers + 3 * nBlockSize; + float* pCallAverage = pTimers + 4 * nBlockSize; + float* pTimersExclusive = pTimers + 5 * nBlockSize; + float* pAverageExclusive = pTimers + 6 * nBlockSize; + float* pMaxExclusive = pTimers + 7 * nBlockSize; + float* pTotal = pTimers + 8 * nBlockSize; + + MicroProfileCalcAllTimers(pTimers, pAverage, pMax, pMin, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, pTotal, nNumTimers); + + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + uint32_t nIdx = i * 2; + printf("\"%s\",\"%s\",%f,%f,%f\n", S.TimerInfo[i].pName, S.GroupInfo[S.TimerInfo[i].nGroupIndex].pName, pAverage[nIdx], pMax[nIdx], pCallAverage[nIdx]); + } + + printf("\n\n"); + + printf("group,average,max,total\n"); + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + const char* pGroupName = S.GroupInfo[j].pName; + float fToMs = S.GroupInfo[j].Type == MicroProfileTokenTypeGpu ? fToMsGPU : fToMsCPU; + if(pGroupName[0] != '\0') + { + printf("\"%s\",%.3f,%.3f,%.3f\n", pGroupName, fToMs * S.AggregateGroup[j] / nAggregateFrames, fToMs * S.AggregateGroup[j] / nAggregateFrames, fToMs * S.AggregateGroup[j]); + } + } + + printf("\n\n"); + printf("group,thread,average,total\n"); + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + if(S.Pool[i]) + { + const char* pThreadName = &S.Pool[i]->ThreadName[0]; + // MicroProfilePrintf(CB, Handle, "var ThreadGroupTime%d = [", i); + float fToMs = S.Pool[i]->nGpu ? fToMsGPU : fToMsCPU; + { + uint64_t nTicks = S.Pool[i]->nAggregateGroupTicks[j]; + float fTime = nTicks / nAggregateFrames * fToMs; + float fTimeTotal = nTicks * fToMs; + if(fTimeTotal > 0.01f) + { + const char* pGroupName = S.GroupInfo[j].pName; + printf("\"%s\",\"%s\",%.3f,%.3f\n", pGroupName, pThreadName, fTime, fTimeTotal); + } + } + } + } + } + + printf("\n\n"); + printf("frametimecpu\n"); + + const uint32_t nCount = MICROPROFILE_MAX_FRAME_HISTORY - MICROPROFILE_GPU_FRAME_DELAY - 3; + const uint32_t nStart = S.nFrameCurrent; + for(uint32_t i = nCount; i > 0; i--) + { + uint32_t nFrame = (nStart + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY; + uint32_t nFrameNext = (nStart + MICROPROFILE_MAX_FRAME_HISTORY - i + 1) % MICROPROFILE_MAX_FRAME_HISTORY; + uint64_t nTicks = S.Frames[nFrameNext].nFrameStartCpu - S.Frames[nFrame].nFrameStartCpu; + printf("%f,", nTicks * fToMsCPU); + } + printf("\n"); + + printf("\n\n"); + printf("frametimegpu\n"); + + for(uint32_t i = nCount; i > 0; i--) + { + uint32_t nFrame = (nStart + MICROPROFILE_MAX_FRAME_HISTORY - i) % MICROPROFILE_MAX_FRAME_HISTORY; + uint32_t nFrameNext = (nStart + MICROPROFILE_MAX_FRAME_HISTORY - i + 1) % MICROPROFILE_MAX_FRAME_HISTORY; + uint64_t nTicks = S.Frames[nFrameNext].nFrameStartGpu - S.Frames[nFrame].nFrameStartGpu; + printf("%f,", nTicks * fToMsGPU); + } + printf("\n\n"); +} +#undef printf + +void MicroProfileDumpCsvLegacy() +{ + char Path[MICROPROFILE_MAX_PATH]; + int Length = snprintf(Path, sizeof(S.CsvDumpPath), "%s.csv", S.CsvDumpPath); + if(Length > 0 && Length < MICROPROFILE_MAX_PATH) + { + FILE* F = fopen(Path, "w"); + if(F) + { + MicroProfileDumpCsvLegacy(MicroProfileWriteFile, F); + fclose(F); + } + } +} + +void MicroProfileDumpHtmlLive(MicroProfileWriteCallback CB, void* Handle) +{ + for(size_t i = 0; i < g_MicroProfileHtmlLive_begin_count; ++i) + { + CB(Handle, g_MicroProfileHtmlLive_begin_sizes[i] - 1, g_MicroProfileHtmlLive_begin[i]); + } + for(size_t i = 0; i < g_MicroProfileHtmlLive_end_count; ++i) + { + CB(Handle, g_MicroProfileHtmlLive_end_sizes[i] - 1, g_MicroProfileHtmlLive_end[i]); + } +} +void MicroProfileGetCoreInformation() +{ +#ifdef _WIN32 + unsigned long BufferSize; + HANDLE Process = GetCurrentProcess(); + GetSystemCpuSetInformation(nullptr, 0, &BufferSize, Process, 0); + char* Buffer = (char*)alloca(BufferSize); + if(!GetSystemCpuSetInformation((PSYSTEM_CPU_SET_INFORMATION)Buffer, BufferSize, &BufferSize, Process, 0)) + { + return; + } + for(ULONG Size = 0; Size < BufferSize;) + { + PSYSTEM_CPU_SET_INFORMATION CpuSet = reinterpret_cast(Buffer); + if(CpuSet->Type == CPU_SET_INFORMATION_TYPE::CpuSetInformation) + { + if(CpuSet->CpuSet.CoreIndex < MICROPROFILE_MAX_CPU_CORES) + { + S.CoreEfficiencyClass[CpuSet->CpuSet.LogicalProcessorIndex] = CpuSet->CpuSet.EfficiencyClass; + } + } + Buffer += CpuSet->Size; + Size += CpuSet->Size; + } +#endif +} + +void MicroProfileDumpHtml(MicroProfileWriteCallback CB, void* Handle, uint64_t nMaxFrames, const char* pHost, uint64_t nStartFrameId = (uint64_t)-1) +{ + // Stall pushing of timers + uint64_t nActiveGroup[MICROPROFILE_MAX_GROUP_INTS]; + memcpy(nActiveGroup, S.nActiveGroups, sizeof(S.nActiveGroups)); + memset(S.nActiveGroups, 0, sizeof(S.nActiveGroups)); + bool AnyActive = S.AnyActive; + S.AnyActive = false; + + S.nPauseTicks = MP_TICK(); + + MicroProfileGetCoreInformation(); + + if(S.bContextSwitchRunning) + { + auto StallForContextSwitchThread = []() + { + int64_t nPauseTicks = S.nPauseTicks; + int64_t nContextSwitchStalledTick = S.nContextSwitchStalledTick; + return (nPauseTicks - nContextSwitchStalledTick) > 0; + }; + int SleepMs = 1; + while(S.bContextSwitchRunning && !S.bContextSwitchStop && StallForContextSwitchThread()) + { + MicroProfileSleep(SleepMs); + SleepMs = SleepMs * 2 / 3; + SleepMs = MicroProfileMin(128, SleepMs); + } + int64_t TicksAfterStall = MP_TICK(); + uprintf("Stalled %7.2fms for context switch data\n", MicroProfileTickToMsMultiplierCpu() * (TicksAfterStall - S.nPauseTicks)); + } + + MicroProfileHashTable StringsHashTable; + MicroProfileHashTableInit(&StringsHashTable, 50, 25, MicroProfileHashTableCompareString, MicroProfileHashTableHashString); + + defer + { + MicroProfileHashTableDestroy(&StringsHashTable); + }; + + MicroProfileCounterFetchCounters(); + for(size_t i = 0; i < g_MicroProfileHtml_begin_count; ++i) + { + CB(Handle, g_MicroProfileHtml_begin_sizes[i] - 1, g_MicroProfileHtml_begin[i]); + } + // dump info + uint64_t nTicks = MP_TICK(); + + float fToMsCPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fToMsGPU = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); + float fAggregateMs = fToMsCPU * (nTicks - S.nAggregateFlipTick); + + uint32_t nNumFrames = 0; + uint32_t nFirstFrame = (uint32_t)-1; + if(nStartFrameId != (uint64_t)-1) + { + // search for the frane + for(uint32_t i = 0; i < MICROPROFILE_MAX_FRAME_HISTORY; ++i) + { + if(S.Frames[i].nFrameId == nStartFrameId) + { + nFirstFrame = i; + break; + } + } + if(nFirstFrame != (uint32_t)-1) + { + uint32_t nLastFrame = S.nFrameCurrent; + uint32_t nDistance = (MICROPROFILE_MAX_FRAME_HISTORY + nFirstFrame - nLastFrame) % MICROPROFILE_MAX_FRAME_HISTORY; + nNumFrames = MicroProfileMin(nDistance, (uint32_t)nMaxFrames); + } + } + + if(nNumFrames == 0) + { + nNumFrames = (MICROPROFILE_MAX_FRAME_HISTORY - MICROPROFILE_GPU_FRAME_DELAY - 3); // leave a few to not overwrite + nNumFrames = MicroProfileMin(nNumFrames, (uint32_t)nMaxFrames); + nFirstFrame = (S.nFrameCurrent + MICROPROFILE_MAX_FRAME_HISTORY - nNumFrames) % MICROPROFILE_MAX_FRAME_HISTORY; + } + + uint32_t nLastFrame = (nFirstFrame + nNumFrames) % MICROPROFILE_MAX_FRAME_HISTORY; + MP_ASSERT(nFirstFrame < MICROPROFILE_MAX_FRAME_HISTORY); + MP_ASSERT(nLastFrame < MICROPROFILE_MAX_FRAME_HISTORY); + + MicroProfilePrintf(CB, Handle, "S.DumpHost = '%s';\n", pHost ? pHost : ""); + time_t CaptureTime; + time(&CaptureTime); + MicroProfilePrintf(CB, Handle, "S.DumpUtcCaptureTime = %ld;\n", CaptureTime); + MicroProfilePrintf(CB, Handle, "S.AggregateInfo = {'Frames':%d, 'Time':%f};\n", S.nAggregateFrames, fAggregateMs); + + // categories + MicroProfilePrintf(CB, Handle, "S.CategoryInfo = Array(%d);\n", S.nCategoryCount); + for(uint32_t i = 0; i < S.nCategoryCount; ++i) + { + MicroProfilePrintf(CB, Handle, "S.CategoryInfo[%d] = \"%s\";\n", i, S.CategoryInfo[i].pName); + } + + // groups + MicroProfilePrintf(CB, Handle, "S.GroupInfo = Array(%d);\n\n", S.nGroupCount + 1); + uint32_t nAggregateFrames = S.nAggregateFrames ? S.nAggregateFrames : 1; + float fRcpAggregateFrames = 1.f / nAggregateFrames; + (void)fRcpAggregateFrames; + char ColorString[32]; + for(uint32_t i = 0; i < S.nGroupCount; ++i) + { + MP_ASSERT(i == S.GroupInfo[i].nGroupIndex); + float fToMs = S.GroupInfo[i].Type == MicroProfileTokenTypeCpu ? fToMsCPU : fToMsGPU; + const char* pColorStr = ""; + if(S.GroupInfo[i].nColor != 0x42) + { + stbsp_snprintf(ColorString, + sizeof(ColorString) - 1, + "#%02x%02x%02x", + MICROPROFILE_UNPACK_RED(S.GroupInfo[i].nColor) & 0xff, + MICROPROFILE_UNPACK_GREEN(S.GroupInfo[i].nColor) & 0xff, + MICROPROFILE_UNPACK_BLUE(S.GroupInfo[i].nColor) & 0xff); + pColorStr = &ColorString[0]; + } + MicroProfilePrintf(CB, + Handle, + "S.GroupInfo[%d] = MakeGroup(%d, \"%s\", %d, %d, %d, %f, %f, %f, '%s');\n", + S.GroupInfo[i].nGroupIndex, + S.GroupInfo[i].nGroupIndex, + S.GroupInfo[i].pName, + S.GroupInfo[i].nCategory, + S.GroupInfo[i].nNumTimers, + S.GroupInfo[i].Type == MicroProfileTokenTypeGpu ? 1 : 0, + fToMs * S.AggregateGroup[i], + fToMs * S.AggregateGroup[i] / nAggregateFrames, + fToMs * S.AggregateGroupMax[i], + pColorStr); + } + uint32_t nUncategorized = S.nGroupCount; + + MicroProfilePrintf(CB, + Handle, + "S.GroupInfo[%d] = MakeGroup(%d, \"%s\", %d, %d, %d, %f, %f, %f, 'grey');\n", + nUncategorized, + nUncategorized, + "Uncategorized", + -1, + 1, + // S.GroupInfo[i].Type == MicroProfileTokenTypeGpu ? 1 : + 0, + 0, + 0, + 0); + + // timers + + uint32_t nNumTimers = S.nTotalTimers; + uint32_t nBlockSize = 2 * nNumTimers; + float* pTimers = (float*)alloca(nBlockSize * 9 * sizeof(float)); + float* pAverage = pTimers + nBlockSize; + float* pMax = pTimers + 2 * nBlockSize; + float* pMin = pTimers + 3 * nBlockSize; + float* pCallAverage = pTimers + 4 * nBlockSize; + float* pTimersExclusive = pTimers + 5 * nBlockSize; + float* pAverageExclusive = pTimers + 6 * nBlockSize; + float* pMaxExclusive = pTimers + 7 * nBlockSize; + float* pTotal = pTimers + 8 * nBlockSize; + + MicroProfileCalcAllTimers(pTimers, pAverage, pMax, pMin, pCallAverage, pTimersExclusive, pAverageExclusive, pMaxExclusive, pTotal, nNumTimers); + + MicroProfilePrintf(CB, Handle, "\nS.TimerInfo = Array(%d);\n\n", S.nTotalTimers); + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + uint32_t nIdx = i * 2; + MP_ASSERT(i == S.TimerInfo[i].nTimerIndex); + MicroProfilePrintf(CB, Handle, "S.Meta%d = [];\n", i); + MicroProfilePrintf(CB, Handle, "S.MetaAvg%d = [];\n", i); + MicroProfilePrintf(CB, Handle, "S.MetaMax%d = [];\n", i); + + uint32_t nColor = S.TimerInfo[i].nColor; + uint32_t nColorDark = (nColor >> 1) & ~0x80808080; + MicroProfilePrintf(CB, + Handle, + "S.TimerInfo[%d] = MakeTimer(%d, \"%s\", %d, '#%02x%02x%02x','#%02x%02x%02x', %f, %f, %f, %f, %f, %f, %d, %f, S.Meta%d, S.MetaAvg%d, S.MetaMax%d, %d);\n", + S.TimerInfo[i].nTimerIndex, + S.TimerInfo[i].nTimerIndex, + S.TimerInfo[i].pName, + S.TimerInfo[i].nGroupIndex, + MICROPROFILE_UNPACK_RED(nColor) & 0xff, + MICROPROFILE_UNPACK_GREEN(nColor) & 0xff, + MICROPROFILE_UNPACK_BLUE(nColor) & 0xff, + MICROPROFILE_UNPACK_RED(nColorDark) & 0xff, + MICROPROFILE_UNPACK_GREEN(nColorDark) & 0xff, + MICROPROFILE_UNPACK_BLUE(nColorDark) & 0xff, + pAverage[nIdx], + pMax[nIdx], + pMin[nIdx], + pAverageExclusive[nIdx], + pMaxExclusive[nIdx], + pCallAverage[nIdx], + S.Aggregate[i].nCount, + pTotal[nIdx], + i, + i, + i, + S.TimerInfo[i].Flags); + } + + uint32_t nTotalTimersExt = S.nTotalTimers; + { + for(uint32_t j = 0; j < S.nNumLogs; ++j) + { + MicroProfileThreadLog* pLog = S.Pool[j]; + uint32_t nLogStart = S.Frames[nFirstFrame].nLogStart[j]; + uint32_t nLogEnd = S.Frames[nLastFrame].nLogStart[j]; + uint64_t nLogType; + if(nLogStart != nLogEnd) + { + for(uint32_t k = nLogStart; k != nLogEnd; k = (k + 1) % MICROPROFILE_BUFFER_SIZE) + { + uint64_t v = pLog->Log[k]; + nLogType = MicroProfileLogGetType(v); + uint32_t tidx = MicroProfileLogGetTimerIndex(v); + if((nLogType == MP_LOG_ENTER || nLogType == MP_LOG_LEAVE) && tidx == ETOKEN_CSTR_PTR) + { + MP_ASSERT(k + 1 != nLogEnd); + uint64_t v1 = pLog->Log[(k + 1) % MICROPROFILE_BUFFER_SIZE]; + const char* pString = (const char*)MicroProfileLogGetExtendedPayloadNoDataPtr(v1); + uintptr_t value; + if(!MicroProfileHashTableGet(&StringsHashTable, (uint64_t)pString, &value)) + { + uintptr_t nTimerIndex = nTotalTimersExt++; + MicroProfileHashTableSet(&StringsHashTable, (uint64_t)pString, nTimerIndex); + MicroProfilePrintf( + CB, Handle, "S.TimerInfo.push(MakeTimer(%d, \"%s\", %d, '#000000','#000000', 0, 0, 0, 0, 0, 0, 0, 0, null, null, null, 0));\n", nTimerIndex, pString, nUncategorized); + } + } + } + } + } + } + + MicroProfilePrintf(CB, Handle, "\nS.ThreadNames = ["); + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + if(S.Pool[i]) + { + MicroProfilePrintf(CB, Handle, "'%s',", S.Pool[i]->ThreadName); + } + else + { + MicroProfilePrintf(CB, Handle, "'Thread %d',", i); + } + } + MicroProfilePrintf(CB, Handle, "];\n\n"); + MicroProfilePrintf(CB, Handle, "\nS.ISGPU = ["); + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + MicroProfilePrintf(CB, Handle, "%d,", (S.Pool[i] && S.Pool[i]->nGpu) ? 1 : 0); + } + MicroProfilePrintf(CB, Handle, "];\n\n"); + + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + if(S.Pool[i]) + { + MicroProfilePrintf(CB, Handle, "S.ThreadGroupTime%d = [", i); + float fToMs = S.Pool[i]->nGpu ? fToMsGPU : fToMsCPU; + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + MicroProfilePrintf(CB, Handle, "%f,", S.Pool[i]->nAggregateGroupTicks[j] / nAggregateFrames * fToMs); + } + MicroProfilePrintf(CB, Handle, "];\n"); + } + } + MicroProfilePrintf(CB, Handle, "\nS.ThreadGroupTimeArray = ["); + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + if(S.Pool[i]) + { + MicroProfilePrintf(CB, Handle, "S.ThreadGroupTime%d,", i); + } + } + MicroProfilePrintf(CB, Handle, "];\n"); + + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + if(S.Pool[i]) + { + MicroProfilePrintf(CB, Handle, "S.ThreadGroupTimeTotal%d = [", i); + float fToMs = S.Pool[i]->nGpu ? fToMsGPU : fToMsCPU; + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) + { + MicroProfilePrintf(CB, Handle, "%f,", S.Pool[i]->nAggregateGroupTicks[j] * fToMs); + } + MicroProfilePrintf(CB, Handle, "];\n"); + } + } + MicroProfilePrintf(CB, Handle, "\nS.ThreadGroupTimeTotalArray = ["); + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + if(S.Pool[i]) + { + MicroProfilePrintf(CB, Handle, "S.ThreadGroupTimeTotal%d,", i); + } + } + MicroProfilePrintf(CB, Handle, "];"); + + MicroProfilePrintf(CB, Handle, "\nS.ThreadIds = ["); + for(uint32_t i = 0; i < S.nNumLogs; ++i) + { + if(S.Pool[i]) + { + MicroProfileThreadIdType ThreadId = S.Pool[i]->nThreadId; + if(!ThreadId) + { + ThreadId = (MicroProfileThreadIdType)-1; + } + MicroProfilePrintf(CB, Handle, "%" PRIu64 ",", (uint64_t)ThreadId); + } + else + { + MicroProfilePrintf(CB, Handle, "-1,"); + } + } + MicroProfilePrintf(CB, Handle, "];\n\n"); + + for(int i = 0; i < (int)S.nNumCounters; ++i) + { + bool IsDouble = (S.CounterInfo[i].nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) != 0; + if(0 != (S.CounterInfo[i].nFlags & MICROPROFILE_COUNTER_FLAG_DETAILED) && !IsDouble) + { + int64_t nCounterMax = S.nCounterMax[i]; + int64_t nCounterMin = S.nCounterMin[i]; + uint32_t nBaseIndex = S.nCounterHistoryPut; + MicroProfilePrintf(CB, Handle, "\nS.CounterHistoryArray%d =[", i); + for(uint32_t j = 0; j < MICROPROFILE_GRAPH_HISTORY; ++j) + { + uint32_t nHistoryIndex = (nBaseIndex + j) % MICROPROFILE_GRAPH_HISTORY; + int64_t nValue = MicroProfileClamp(S.nCounterHistory[nHistoryIndex][i], nCounterMin, nCounterMax); + MicroProfilePrintf(CB, Handle, "%lld,", nValue); + } + MicroProfilePrintf(CB, Handle, "];\n"); + + int64_t nCounterHeightBase = nCounterMax; + int64_t nCounterOffset = 0; + if(nCounterMin < 0) + { + nCounterHeightBase = nCounterMax - nCounterMin; + nCounterOffset = -nCounterMin; + } + double fRcp = nCounterHeightBase ? (1.0 / nCounterHeightBase) : 0; + + MicroProfilePrintf(CB, Handle, "\nS.CounterHistoryArrayPrc%d =[", i); + for(uint32_t j = 0; j < MICROPROFILE_GRAPH_HISTORY; ++j) + { + uint32_t nHistoryIndex = (nBaseIndex + j) % MICROPROFILE_GRAPH_HISTORY; + int64_t nValue = MicroProfileClamp(S.nCounterHistory[nHistoryIndex][i], nCounterMin, nCounterMax); + float fPrc = (nValue + nCounterOffset) * fRcp; + MicroProfilePrintf(CB, Handle, "%f,", fPrc); + } + MicroProfilePrintf(CB, Handle, "];\n"); + MicroProfilePrintf(CB, Handle, "S.CounterHistory%d = MakeCounterHistory(%d, S.CounterHistoryArray%d, S.CounterHistoryArrayPrc%d)\n", i, i, i, i); + } + else + { + MicroProfilePrintf(CB, Handle, "S.CounterHistory%d;\n", i); + } + } + + MicroProfilePrintf(CB, Handle, "\nS.CounterInfo = ["); + + for(int i = 0; i < (int)S.nNumCounters; ++i) + { + bool IsDouble = (S.CounterInfo[i].nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) != 0; + float fCounterPrc = 0.f; + float fBoxPrc = 1.f; + double dCounter, dLimit, dMax, dMin; + char Formatted[64]; + char FormattedLimit[64]; + + if(!IsDouble) + { + uint64_t nCounter = S.Counters[i].load(); + uint64_t nLimit = S.CounterInfo[i].nLimit; + fCounterPrc = 0.f; + if(nLimit) + { + fCounterPrc = (float)nCounter / nLimit; + if(fCounterPrc > 1.f) + { + fBoxPrc = 1.f / fCounterPrc; + fCounterPrc = 1.f; + } + } + MicroProfileFormatCounter(S.CounterInfo[i].eFormat, nCounter, Formatted, sizeof(Formatted) - 1); + MicroProfileFormatCounter(S.CounterInfo[i].eFormat, S.CounterInfo[i].nLimit, FormattedLimit, sizeof(FormattedLimit) - 1); + + dCounter = (double)nCounter; + dMin = (double)S.nCounterMin[i]; + dMax = (double)S.nCounterMax[i]; + dLimit = (double)nLimit; + } + else + { + dCounter = S.CountersDouble[i].load(); + dLimit = S.CounterInfo[i].dLimit; + fCounterPrc = 0.f; + if(dLimit > 0.f) + { + fCounterPrc = (float)(dCounter / dLimit); + if(fCounterPrc > 1.f) + { + fBoxPrc = 1.f / fCounterPrc; + fCounterPrc = 1.f; + } + } + MicroProfileFormatCounterDouble(S.CounterInfo[i].eFormat, dCounter, Formatted, sizeof(Formatted) - 1); + MicroProfileFormatCounterDouble(S.CounterInfo[i].eFormat, S.CounterInfo[i].dLimit, FormattedLimit, sizeof(FormattedLimit) - 1); + dMin = (double)S.dCounterMin[i]; + dMax = (double)S.dCounterMax[i]; + } + MicroProfilePrintf(CB, + Handle, + "MakeCounter(%d, %d, %d, %d, %d, '%s', %f, %f, %f, '%s', %f, '%s', %f, %f, %d, S.CounterHistory%d),", + i, + S.CounterInfo[i].nParent, + S.CounterInfo[i].nSibling, + S.CounterInfo[i].nFirstChild, + S.CounterInfo[i].nLevel, + S.CounterInfo[i].pName, + dCounter, + dMin, + dMax, + Formatted, + dLimit, + FormattedLimit, + fCounterPrc, + fBoxPrc, + S.CounterInfo[i].eFormat == MICROPROFILE_COUNTER_FORMAT_BYTES ? 1 : 0, + i); + } + MicroProfilePrintf(CB, Handle, "];\n\n"); + + const int64_t nTickStart = S.Frames[nFirstFrame].nFrameStartCpu; + const int64_t nTickEnd = S.Frames[nLastFrame].nFrameStartCpu; + int64_t nTickStartGpu = S.Frames[nFirstFrame].nFrameStartGpu; + + int64_t nTickReferenceCpu, nTickReferenceGpu; + int64_t nTicksPerSecondCpu = MicroProfileTicksPerSecondCpu(); + int64_t nTicksPerSecondGpu = MicroProfileTicksPerSecondGpu(); + int nTickReference = 0; + if(MicroProfileGetGpuTickReference(&nTickReferenceCpu, &nTickReferenceGpu)) + { + nTickStartGpu = (nTickStart - nTickReferenceCpu) * nTicksPerSecondGpu / nTicksPerSecondCpu + nTickReferenceGpu; + nTickReference = 1; + } + + uprintf("dumping %d frames\n", nNumFrames); + uprintf("dumping frame %d to %d\n", nFirstFrame, nLastFrame); + + uint32_t* nTimerCounter = (uint32_t*)alloca(sizeof(uint32_t) * S.nTotalTimers); + memset(nTimerCounter, 0, sizeof(uint32_t) * S.nTotalTimers); + + { + MicroProfilePrintf(CB, Handle, " //Timeline begin\n"); + MicroProfileThreadLog* pLog = &S.TimelineLog; + uint32_t nFrameIndexFirst = (nFirstFrame) % MICROPROFILE_MAX_FRAME_HISTORY; + uint32_t nFrameIndexLast = (nFirstFrame + nNumFrames) % MICROPROFILE_MAX_FRAME_HISTORY; + { + // find the frame that has an active marker the furtest distance from the selected range + int nDelta = 0; + int nOffset = 0; + for(uint32_t i = nFrameIndexFirst; i != nFrameIndexLast; i = (i + 1) % MICROPROFILE_MAX_FRAME_HISTORY) + { + int D = (int)S.Frames[i].nTimelineFrameMax - nOffset; + nDelta = MicroProfileMax(D, nDelta); + nOffset++; + } + nFrameIndexFirst = (nFirstFrame - nDelta) % MICROPROFILE_MAX_FRAME_HISTORY; + } + + uint32_t nLogStart = S.Frames[nFrameIndexFirst].nLogStartTimeline; + uint32_t nLogEnd = S.Frames[nFrameIndexLast].nLogStartTimeline; + float fToMs = MicroProfileTickToMsMultiplier(nTicksPerSecondCpu); + +#define pp(...) MicroProfilePrintf(CB, Handle, __VA_ARGS__) + + if(nLogStart != nLogEnd) + { + uint32_t nLogType; + float fTime; + int f = 0; + + pp("S.TimelineColorArray=["); + for(uint32_t k = nLogStart; k != nLogEnd; k = (k + 1) % MICROPROFILE_BUFFER_SIZE) + { + uint64_t v = pLog->Log[k]; + uint64_t nIndex = MicroProfileLogGetTimerIndex(v); + uint64_t nTick = MicroProfileLogGetTick(v); + (void)nTick; + nLogType = MicroProfileLogGetType(v); + switch(nLogType) + { + case MP_LOG_ENTER: + break; + case MP_LOG_LEAVE: + pp("%c'%s'", f++ ? ',' : ' ', "#ff8080"); + break; + + case MP_LOG_EXTENDED: + case MP_LOG_EXTENDED_NO_DATA: + uint32_t payload = MicroProfileLogGetExtendedPayload(v); + if(nIndex == ETOKEN_CUSTOM_COLOR) + { + uint32_t nColor = payload; + pp("%c'#%02x%02x%02x'", f++ ? ',' : ' ', MICROPROFILE_UNPACK_RED(nColor) & 0xff, MICROPROFILE_UNPACK_GREEN(nColor) & 0xff, MICROPROFILE_UNPACK_BLUE(nColor) & 0xff); + } + k += MicroProfileLogGetDataSize(v); + break; + } + } + pp("];\n"); + + f = 0; + pp("S.TimelineIdArray=["); + for(uint32_t k = nLogStart; k != nLogEnd; k = (k + 1) % MICROPROFILE_BUFFER_SIZE) + { + uint64_t v = pLog->Log[k]; + uint64_t nIndex = MicroProfileLogGetTimerIndex(v); + uint64_t nTick = MicroProfileLogGetTick(v); + (void)nTick; + nLogType = MicroProfileLogGetType(v); + switch(nLogType) + { + case MP_LOG_ENTER: + case MP_LOG_LEAVE: + case MP_LOG_EXTENDED_NO_DATA: + + break; + case MP_LOG_EXTENDED: + if(nIndex == ETOKEN_CUSTOM_ID) + { + pp("%c%d", f++ ? ',' : ' ', (uint32_t)nTick); + } + k += MicroProfileLogGetDataSize(v); + break; + } + } + pp("];\n"); + + f = 0; + + pp("S.TimelineArray=["); + for(uint32_t k = nLogStart; k != nLogEnd; k = (k + 1) % MICROPROFILE_BUFFER_SIZE) + { + uint64_t v = pLog->Log[k]; + nLogType = MicroProfileLogGetType(v); + switch(nLogType) + { + case MP_LOG_ENTER: + case MP_LOG_LEAVE: + fTime = MicroProfileLogTickDifference(nTickStart, v) * fToMs; + pp("%c%f", f++ ? ',' : ' ', fTime); + break; + case MP_LOG_EXTENDED: + k += MicroProfileLogGetDataSize(v); + break; + case MP_LOG_EXTENDED_NO_DATA: + break; + } + } + pp("];\n"); + pp("S.TimelineNames=["); + f = 0; + char String[MICROPROFILE_MAX_STRING + 1]; + for(uint32_t k = nLogStart; k != nLogEnd;) + { + uint64_t v = pLog->Log[k]; + nLogType = MicroProfileLogGetType(v); + uint64_t nIndex = MicroProfileLogGetTimerIndex(v); + uint64_t nTick = MicroProfileLogGetTick(v); + (void)nTick; + switch(nLogType) + { + case MP_LOG_ENTER: + case MP_LOG_LEAVE: + if(nIndex == ETOKEN_CUSTOM_NAME && nLogType == MP_LOG_LEAVE) + { + // pp(f++ ? ",''" : "''"); + } + k = (k + 1) % MICROPROFILE_BUFFER_SIZE; + break; + case MP_LOG_EXTENDED_NO_DATA: + k = (k + 1) % MICROPROFILE_BUFFER_SIZE; + break; + case MP_LOG_EXTENDED: + uint32_t nSize = MicroProfileLogGetDataSize(v); + + if(nIndex == ETOKEN_CUSTOM_ID) + { + char* pSource = (char*)&pLog->Log[(k + 1) % MICROPROFILE_BUFFER_SIZE]; + const char* pOut = nullptr; + if(nSize == 0) + { + pOut = ""; + } + else if(k + nSize <= MICROPROFILE_BUFFER_SIZE) + { + pOut = pSource; + } + else + { + pOut = &String[0]; + char* pDest = &String[0]; + MP_ASSERT(nSize * 8 < sizeof(MICROPROFILE_MAX_STRING) + 1); + uint32_t Index = (k + 1) % MICROPROFILE_BUFFER_SIZE; + for(uint32_t l = 0; l < nSize; ++l) + { + memcpy(pDest, (char*)pLog->Log[Index], sizeof(uint64_t)); + Index = (Index + 1) % MICROPROFILE_BUFFER_SIZE; + } + } + if(f++) + { + pp(",'%s'", pOut); + } + else + { + pp("'%s'", pOut); + } + } + k = (k + 1 + nSize) % MICROPROFILE_BUFFER_SIZE; + break; + } + } + pp("];\n"); + } + MicroProfilePrintf(CB, Handle, " //Timeline end\n"); + } + + MicroProfilePrintf(CB, Handle, "S.Frames = Array(%d);\n", nNumFrames); + for(uint32_t i = 0; i < nNumFrames; ++i) + { + uint32_t nFrameIndex = (nFirstFrame + i) % MICROPROFILE_MAX_FRAME_HISTORY; + uint32_t nFrameIndexNext = (nFrameIndex + 1) % MICROPROFILE_MAX_FRAME_HISTORY; + + for(uint32_t j = 0; j < S.nNumLogs; ++j) + { + MicroProfileThreadLog* pLog = S.Pool[j]; + int64_t nStartTickBase = pLog->nGpu ? nTickStartGpu : nTickStart; + uint32_t nLogStart = S.Frames[nFrameIndex].nLogStart[j]; + uint32_t nLogEnd = S.Frames[nFrameIndexNext].nLogStart[j]; + uint32_t nLogType; + float fToMs; + uint64_t nStartTick; + float fToMsCpu = MicroProfileTickToMsMultiplier(nTicksPerSecondCpu); + float fToMsBase = MicroProfileTickToMsMultiplier(pLog->nGpu ? nTicksPerSecondGpu : nTicksPerSecondCpu); + MicroProfilePrintf(CB, Handle, "S.ts_%d_%d = [", i, j); + if(nLogStart != nLogEnd) + { + int f = 0; + for(uint32_t k = nLogStart; k != nLogEnd; k = (k + 1) % MICROPROFILE_BUFFER_SIZE) + { + float fTime; + MicroProfileLogEntry v = pLog->Log[k]; + nLogType = MicroProfileLogGetType(v); + fToMs = fToMsBase; + nStartTick = nStartTickBase; + switch(nLogType) + { + case MP_LOG_EXTENDED: + { + fTime = 0.f; + k += MicroProfileLogGetDataSize(v); + break; + } + case MP_LOG_EXTENDED_NO_DATA: + { + uint32_t nTimerIndex = (uint32_t)MicroProfileLogGetTimerIndex(v); + if(nTimerIndex == ETOKEN_GPU_CPU_TIMESTAMP) + { + fToMs = fToMsCpu; + nStartTick = nTickStart; + fTime = MicroProfileLogTickDifference(nStartTick, v) * fToMs; + } + else + { + fTime = 0.f; + } + break; + } + default: + fTime = MicroProfileLogTickDifference(nStartTick, pLog->Log[k]) * fToMs; + } + MicroProfilePrintf(CB, Handle, f++ ? ",%f" : "%f", fTime); + } + } + MicroProfilePrintf(CB, Handle, "];\n"); + + MicroProfilePrintf(CB, Handle, "S.tt_%d_%d = [", i, j); + if(nLogStart != nLogEnd) + { + uint32_t k = nLogStart; + MicroProfilePrintf(CB, Handle, "%d", MicroProfileLogGetType(pLog->Log[k])); + for(k = (k + 1) % MICROPROFILE_BUFFER_SIZE; k != nLogEnd; k = (k + 1) % MICROPROFILE_BUFFER_SIZE) + { + uint64_t v = pLog->Log[k]; + uint32_t nLogType2 = MicroProfileLogGetType(v); + + if(nLogType2 > MP_LOG_ENTER) + nLogType2 |= (MicroProfileLogGetExtendedToken(v)) + << 2; // pack extended token here.. this way all code can check agains ENTER/LEAVE, and only the ext code needs to care about the top bits. + MicroProfilePrintf(CB, Handle, ",%d", nLogType2); + if(nLogType2 == MP_LOG_EXTENDED) + k += MicroProfileLogGetDataSize(v); + } + } + MicroProfilePrintf(CB, Handle, "];\n"); + + MicroProfilePrintf(CB, Handle, "S.ti_%d_%d = [", i, j); + if(nLogStart != nLogEnd) + { + for(uint32_t k = nLogStart; k != nLogEnd; k = (k + 1) % MICROPROFILE_BUFFER_SIZE) + { + uint64_t v = pLog->Log[k]; + nLogType = MicroProfileLogGetType(v); + const char* pFormat = k == nLogStart ? "%d" : ",%d"; + if(nLogType == MP_LOG_ENTER || nLogType == MP_LOG_LEAVE) + { + uint32_t nTimerIndex = (uint32_t)MicroProfileLogGetTimerIndex(pLog->Log[k]); + if(ETOKEN_CSTR_PTR == nTimerIndex) + { + MP_ASSERT(k + 1 != nLogEnd); + uint64_t v1 = pLog->Log[(k + 1) % MICROPROFILE_BUFFER_SIZE]; + + const char* pString = (const char*)MicroProfileLogGetExtendedPayloadNoDataPtr(v1); + uintptr_t value; + if(!MicroProfileHashTableGet(&StringsHashTable, (uint64_t)pString, &value)) + { + MP_BREAK(); // should be covered earlier. + } + MicroProfilePrintf(CB, Handle, pFormat, value); + } + else + { + if(nTimerIndex < S.nTotalTimers) + { + nTimerCounter[nTimerIndex]++; + } + MicroProfilePrintf(CB, Handle, pFormat, nTimerIndex); + } + } + else + { + uint64_t ExtendedToken = MicroProfileLogGetExtendedToken(v); + uint64_t PayloadNoData = MicroProfileLogGetExtendedPayloadNoData(v); + switch(ExtendedToken) + { + case ETOKEN_GPU_CPU_SOURCE_THREAD: + MicroProfilePrintf(CB, Handle, pFormat, PayloadNoData); + break; + default: + MicroProfilePrintf(CB, Handle, pFormat, -1); + } + + if(nLogType == MP_LOG_EXTENDED) + k += MicroProfileLogGetDataSize(v); + } + } + } + MicroProfilePrintf(CB, Handle, "];\n"); + } + + MicroProfilePrintf(CB, Handle, "S.ts%d = [", i); + for(uint32_t j = 0; j < S.nNumLogs; ++j) + { + MicroProfilePrintf(CB, Handle, "S.ts_%d_%d,", i, j); + } + MicroProfilePrintf(CB, Handle, "];\n"); + MicroProfilePrintf(CB, Handle, "S.tt%d = [", i); + for(uint32_t j = 0; j < S.nNumLogs; ++j) + { + MicroProfilePrintf(CB, Handle, "S.tt_%d_%d,", i, j); + } + MicroProfilePrintf(CB, Handle, "];\n"); + + MicroProfilePrintf(CB, Handle, "S.ti%d = [", i); + for(uint32_t j = 0; j < S.nNumLogs; ++j) + { + MicroProfilePrintf(CB, Handle, "S.ti_%d_%d,", i, j); + } + MicroProfilePrintf(CB, Handle, "];\n"); + + int64_t nFrameStart = S.Frames[nFrameIndex].nFrameStartCpu; + int64_t nFrameEnd = S.Frames[nFrameIndexNext].nFrameStartCpu; + + float fToMs = MicroProfileTickToMsMultiplier(nTicksPerSecondCpu); + float fFrameMs = MicroProfileLogTickDifference(nTickStart, nFrameStart) * fToMs; + float fFrameEndMs = MicroProfileLogTickDifference(nTickStart, nFrameEnd) * fToMs; + float fFrameGpuMs = 0; + float fFrameGpuEndMs = 0; + if(nTickReference) + { + fFrameGpuMs = MicroProfileLogTickDifference(nTickStartGpu, S.Frames[nFrameIndex].nFrameStartGpu) * fToMsGPU; + fFrameGpuEndMs = MicroProfileLogTickDifference(nTickStartGpu, S.Frames[nFrameIndexNext].nFrameStartGpu) * fToMsGPU; + } + MicroProfilePrintf(CB, Handle, "S.Frames[%d] = MakeFrame(%d, %f, %f, %f, %f, S.ts%d, S.tt%d, S.ti%d);\n", i, 0, fFrameMs, fFrameEndMs, fFrameGpuMs, fFrameGpuEndMs, i, i, i); + } + + uint32_t nContextSwitchStart = 0; + uint32_t nContextSwitchEnd = 0; + MicroProfileContextSwitchSearch(&nContextSwitchStart, &nContextSwitchEnd, nTickStart, nTickEnd); + + uprintf("CONTEXT SWITCH SEARCH .... %d %d %d .... %lld, %lld\n", nContextSwitchStart, nContextSwitchEnd, nContextSwitchEnd - nContextSwitchStart, nTickStart, nTickEnd); + + uint32_t nWrittenBefore = S.nWebServerDataSent; + MicroProfilePrintf(CB, Handle, "S.CSwitchThreadInOutCpu = ["); + for(uint32_t j = nContextSwitchStart; j != nContextSwitchEnd; j = (j + 1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE) + { + MicroProfileContextSwitch CS = S.ContextSwitch[j]; + int nCpu = CS.nCpu; + MicroProfilePrintf(CB, Handle, "%d,%d,%d,", CS.nThreadIn, CS.nThreadOut, nCpu); + } + MicroProfilePrintf(CB, Handle, "];\n"); + MicroProfilePrintf(CB, Handle, "S.CSwitchTime = ["); + float fToMsCpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + for(uint32_t j = nContextSwitchStart; j != nContextSwitchEnd; j = (j + 1) % MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE) + { + MicroProfileContextSwitch CS = S.ContextSwitch[j]; + float fTime = MicroProfileLogTickDifference(nTickStart, CS.nTicks) * fToMsCpu; + MicroProfilePrintf(CB, Handle, "%f,", fTime); + } + MicroProfilePrintf(CB, Handle, "];\n"); + + MicroProfilePrintf(CB, Handle, "S.CSwitchThreads = {"); + + MicroProfileThreadInfo* pThreadInfo = nullptr; + uint32_t nNumThreads = MicroProfileGetThreadInfoArray(&pThreadInfo); + for(uint32_t i = 0; i < nNumThreads; ++i) + { + const char* p1 = pThreadInfo[i].pThreadModule ? pThreadInfo[i].pThreadModule : "?"; + const char* p2 = pThreadInfo[i].pProcessModule ? pThreadInfo[i].pProcessModule : "?"; + + MicroProfilePrintf(CB, + Handle, + "%" PRId64 ":{\'tid\':%" PRId64 ",\'pid\':%" PRId64 ",\'t\':\'%s\',\'p\':\'%s\'},", + (uint64_t)pThreadInfo[i].tid, + (uint64_t)pThreadInfo[i].tid, + (uint64_t)pThreadInfo[i].pid, + p1, + p2); + } + + MicroProfilePrintf(CB, Handle, "};\n"); + MicroProfilePrintf(CB, Handle, "S.CoreEfficiencyClass = ["); + for(uint32_t i = 0; i < MICROPROFILE_MAX_CPU_CORES; ++i) + { + MicroProfilePrintf(CB, Handle, "%d,", S.CoreEfficiencyClass[i]); + } + MicroProfilePrintf(CB, Handle, "];\n"); + + { + MicroProfilePrintf(CB, Handle, "//String Table\n"); + MicroProfilePrintf(CB, Handle, "S.StringTable = {}\n"); + // dump string table + MicroProfileHashTableIterator beg = MicroProfileGetHashTableIteratorBegin(&StringsHashTable); + MicroProfileHashTableIterator end = MicroProfileGetHashTableIteratorEnd(&StringsHashTable); + while(beg != end) + { + uint64_t Key = beg->Key; + uint64_t Value = beg->Value; + MicroProfilePrintf(CB, Handle, "S.StringTable[%d] = '%s';\n", Value, (const char*)Key); + beg++; + } + } + + uint32_t nWrittenAfter = S.nWebServerDataSent; + + MicroProfilePrintf(CB, Handle, "//CSwitch Size %d\n", nWrittenAfter - nWrittenBefore); + + for(size_t i = 0; i < g_MicroProfileHtml_end_count; ++i) + { + CB(Handle, g_MicroProfileHtml_end_sizes[i] - 1, g_MicroProfileHtml_end[i]); + } + + uint32_t* nGroupCounter = (uint32_t*)alloca(sizeof(uint32_t) * S.nGroupCount); + + memset(nGroupCounter, 0, sizeof(uint32_t) * S.nGroupCount); + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + uint32_t nGroupIndex = S.TimerInfo[i].nGroupIndex; + nGroupCounter[nGroupIndex] += nTimerCounter[i]; + } + + uint32_t* nGroupCounterSort = (uint32_t*)alloca(sizeof(uint32_t) * S.nGroupCount); + uint32_t* nTimerCounterSort = (uint32_t*)alloca(sizeof(uint32_t) * S.nTotalTimers); + for(uint32_t i = 0; i < S.nGroupCount; ++i) + { + nGroupCounterSort[i] = i; + } + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + nTimerCounterSort[i] = i; + } + std::sort(nGroupCounterSort, nGroupCounterSort + S.nGroupCount, [nGroupCounter](const uint32_t l, const uint32_t r) { return nGroupCounter[l] > nGroupCounter[r]; }); + + std::sort(nTimerCounterSort, nTimerCounterSort + S.nTotalTimers, [nTimerCounter](const uint32_t l, const uint32_t r) { return nTimerCounter[l] > nTimerCounter[r]; }); + + MicroProfilePrintf(CB, Handle, "\n\n"); + + memcpy(S.nActiveGroups, nActiveGroup, sizeof(S.nActiveGroups)); + S.AnyActive = AnyActive; +#if MICROPROFILE_DEBUG + int64_t nTicksEnd = MP_TICK(); + float fMs = fToMsCpu * (nTicksEnd - S.nPauseTicks); + uprintf("html dump took %6.2fms\n", fMs); +#endif + +#undef pp + + S.nPauseTicks = 0; +} + +void MicroProfileWriteFile(void* Handle, size_t nSize, const char* pData) +{ + fwrite(pData, nSize, 1, (FILE*)Handle); +} + +void MicroProfileDumpToFile() +{ + std::lock_guard Lock(MicroProfileMutex()); + if(S.nDumpFileNextFrame & 1) + { + char Path[MICROPROFILE_MAX_PATH]; + int Length = snprintf(Path, sizeof(S.HtmlDumpPath), "%s.html", S.HtmlDumpPath); + if(Length > 0 && Length < MICROPROFILE_MAX_PATH) + { + FILE* F = fopen(Path, "w"); + if(F) + { + MicroProfileDumpHtml(MicroProfileWriteFile, F, S.DumpFrameCount, S.HtmlDumpPath); + fclose(F); + } + } + } + if(S.nDumpFileNextFrame & 2) + { +#if MICROPROFILE_LEGACY_CSV + MicroProfileDumpCsvLegacy(); +#else + MicroProfileDumpCsv(S.DumpFrameCount); +#endif + } +} + +void MicroProfileFlushSocket(MpSocket Socket) +{ + send(Socket, &S.WebServerBuffer[0], S.WebServerPut, 0); + S.WebServerPut = 0; +} + +void MicroProfileWriteSocket(void* Handle, size_t nSize, const char* pData) +{ + S.nWebServerDataSent += nSize; + MpSocket Socket = *(MpSocket*)Handle; + if(nSize > MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE / 2) + { + MicroProfileFlushSocket(Socket); + send(Socket, pData, (int)nSize, 0); + } + else + { + memcpy(&S.WebServerBuffer[S.WebServerPut], pData, nSize); + S.WebServerPut += (uint32_t)nSize; + if(S.WebServerPut > MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE / 2) + { + MicroProfileFlushSocket(Socket); + } + } +} + +#if MICROPROFILE_MINIZ +#ifndef MICROPROFILE_COMPRESS_BUFFER_SIZE +#define MICROPROFILE_COMPRESS_BUFFER_SIZE (256 << 10) +#endif + +#define MICROPROFILE_COMPRESS_CHUNK (MICROPROFILE_COMPRESS_BUFFER_SIZE / 2) +struct MicroProfileCompressedSocketState +{ + unsigned char DeflateOut[MICROPROFILE_COMPRESS_CHUNK]; + unsigned char DeflateIn[MICROPROFILE_COMPRESS_CHUNK]; + mz_stream Stream; + MpSocket Socket; + uint32_t nSize; + uint32_t nCompressedSize; + uint32_t nFlushes; + uint32_t nMemmoveBytes; +}; + +void MicroProfileCompressedSocketFlush(MicroProfileCompressedSocketState* pState) +{ + mz_stream& Stream = pState->Stream; + unsigned char* pSendStart = &pState->DeflateOut[0]; + unsigned char* pSendEnd = &pState->DeflateOut[MICROPROFILE_COMPRESS_CHUNK - Stream.avail_out]; + if(pSendStart != pSendEnd) + { + send(pState->Socket, (const char*)pSendStart, pSendEnd - pSendStart, 0); + pState->nCompressedSize += pSendEnd - pSendStart; + } + Stream.next_out = &pState->DeflateOut[0]; + Stream.avail_out = MICROPROFILE_COMPRESS_CHUNK; +} +void MicroProfileCompressedSocketStart(MicroProfileCompressedSocketState* pState, MpSocket Socket) +{ + mz_stream& Stream = pState->Stream; + memset(&Stream, 0, sizeof(Stream)); + Stream.next_out = &pState->DeflateOut[0]; + Stream.avail_out = MICROPROFILE_COMPRESS_CHUNK; + Stream.next_in = &pState->DeflateIn[0]; + Stream.avail_in = 0; + mz_deflateInit(&Stream, Z_DEFAULT_COMPRESSION); + pState->Socket = Socket; + pState->nSize = 0; + pState->nCompressedSize = 0; + pState->nFlushes = 0; + pState->nMemmoveBytes = 0; +} +void MicroProfileCompressedSocketFinish(MicroProfileCompressedSocketState* pState) +{ + mz_stream& Stream = pState->Stream; + MicroProfileCompressedSocketFlush(pState); + int r = mz_deflate(&Stream, MZ_FINISH); + MP_ASSERT(r == MZ_STREAM_END); + MicroProfileCompressedSocketFlush(pState); + r = mz_deflateEnd(&Stream); + MP_ASSERT(r == MZ_OK); +} + +void MicroProfileCompressedWriteSocket(void* Handle, size_t nSize, const char* pData) +{ + MicroProfileCompressedSocketState* pState = (MicroProfileCompressedSocketState*)Handle; + mz_stream& Stream = pState->Stream; + const unsigned char* pDeflateInEnd = Stream.next_in + Stream.avail_in; + const unsigned char* pDeflateInStart = &pState->DeflateIn[0]; + const unsigned char* pDeflateInRealEnd = &pState->DeflateIn[MICROPROFILE_COMPRESS_CHUNK]; + pState->nSize += (uint32_t)nSize; + if((ptrdiff_t)nSize <= pDeflateInRealEnd - pDeflateInEnd) + { + memcpy((void*)pDeflateInEnd, pData, nSize); + Stream.avail_in += (uint32_t)nSize; + MP_ASSERT(Stream.next_in + Stream.avail_in <= pDeflateInRealEnd); + return; + } + int Flush = 0; + while(nSize) + { + pDeflateInEnd = Stream.next_in + Stream.avail_in; + if(Flush) + { + pState->nFlushes++; + MicroProfileCompressedSocketFlush(pState); + pDeflateInRealEnd = &pState->DeflateIn[MICROPROFILE_COMPRESS_CHUNK]; + if(pDeflateInEnd == pDeflateInRealEnd) + { + if(Stream.avail_in) + { + MP_ASSERT(pDeflateInStart != Stream.next_in); + memmove((void*)pDeflateInStart, Stream.next_in, Stream.avail_in); + pState->nMemmoveBytes += Stream.avail_in; + } + Stream.next_in = pDeflateInStart; + pDeflateInEnd = Stream.next_in + Stream.avail_in; + } + } + size_t nSpace = pDeflateInRealEnd - pDeflateInEnd; + size_t nBytes = MicroProfileMin(nSpace, nSize); + MP_ASSERT(nBytes + pDeflateInEnd <= pDeflateInRealEnd); + memcpy((void*)pDeflateInEnd, pData, nBytes); + Stream.avail_in += (uint32_t)nBytes; + nSize -= nBytes; + pData += nBytes; + int r = mz_deflate(&Stream, MZ_NO_FLUSH); + Flush = r == MZ_BUF_ERROR || nBytes == 0 || Stream.avail_out == 0 ? 1 : 0; + MP_ASSERT(r == MZ_BUF_ERROR || r == MZ_OK); + if(r == MZ_BUF_ERROR) + { + r = mz_deflate(&Stream, MZ_SYNC_FLUSH); + } + } +} +#endif + +#ifndef MicroProfileSetNonBlocking // fcntl doesnt work on a some unix like platforms.. +void MicroProfileSetNonBlocking(MpSocket Socket, int NonBlocking) +{ +#ifdef _WIN32 + u_long nonBlocking = NonBlocking ? 1 : 0; + ioctlsocket(Socket, FIONBIO, &nonBlocking); +#else + int Options = fcntl(Socket, F_GETFL); + if(NonBlocking) + { + fcntl(Socket, F_SETFL, Options | O_NONBLOCK); + } + else + { + fcntl(Socket, F_SETFL, Options & (~O_NONBLOCK)); + } +#endif +} +#endif + +void MicroProfileWebServerStart() +{ +#ifdef _WIN32 + WSADATA wsa; + if(WSAStartup(MAKEWORD(2, 2), &wsa)) + { + S.ListenerSocket = (MpSocket)-1; + return; + } +#endif + + S.ListenerSocket = socket(PF_INET, SOCK_STREAM, 6); + MP_ASSERT(!MP_INVALID_SOCKET(S.ListenerSocket)); + MicroProfileSetNonBlocking(S.ListenerSocket, 1); + + { + int r = 0; + int on = 1; +#if defined(_WIN32) + r = setsockopt(S.ListenerSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)); +#else + r = setsockopt(S.ListenerSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)); +#endif + (void)r; + } + + int nStartPort = S.nWebServerPort; + struct sockaddr_in Addr; + Addr.sin_family = AF_INET; + Addr.sin_addr.s_addr = INADDR_ANY; + for(int i = 0; i < 20; ++i) + { + Addr.sin_port = htons(nStartPort + i); + if(0 == bind(S.ListenerSocket, (sockaddr*)&Addr, sizeof(Addr))) + { + S.nWebServerPort = (uint32_t)(nStartPort + i); + break; + } + } + listen(S.ListenerSocket, 8); +} + +void MicroProfileWebServerJoin() +{ + if(S.WebSocketThreadRunning) + { + MicroProfileThreadJoin(&S.WebSocketSendThread); + } + S.WebSocketThreadJoined = 1; +} + +void MicroProfileWebServerStop() +{ + MP_ASSERT(S.WebSocketThreadJoined); +#ifdef _WIN32 + closesocket(S.ListenerSocket); + WSACleanup(); +#else + close(S.ListenerSocket); +#endif +} +enum MicroProfileGetCommand +{ + EMICROPROFILE_GET_COMMAND_DUMP, + EMICROPROFILE_GET_COMMAND_DUMP_RANGE, + EMICROPROFILE_GET_COMMAND_LIVE, + EMICROPROFILE_GET_COMMAND_FAVICON, + EMICROPROFILE_GET_COMMAND_SERVICE_WORKER, + EMICROPROFILE_GET_COMMAND_UNKNOWN, +}; +struct MicroProfileParseGetResult +{ + uint64_t nFrames; + uint64_t nFrameStart; +}; +MicroProfileGetCommand MicroProfileParseGet(const char* pGet, MicroProfileParseGetResult* pResult) +{ + if(0 == strlen(pGet)) + { + return EMICROPROFILE_GET_COMMAND_LIVE; + } + if(0 == strcmp(pGet, "favicon.ico")) + { + return EMICROPROFILE_GET_COMMAND_FAVICON; + } + if(0 == strcmp(pGet, "favicon.png")) + { + return EMICROPROFILE_GET_COMMAND_FAVICON; + } + if(0 == strcmp(pGet, "service-worker.js")) + { + return EMICROPROFILE_GET_COMMAND_SERVICE_WORKER; + } + const char* pStart = pGet; + if(*pStart == 'b' || *pStart == 'p') + { + S.nWSWasConnected = 1; // do not load default when url has one specified. + return EMICROPROFILE_GET_COMMAND_LIVE; + } + if(*pStart == 'r') // range + { + // very very manual parsing + if('/' != *++pStart) + return EMICROPROFILE_GET_COMMAND_UNKNOWN; + ++pStart; + + char* pEnd = nullptr; + uint64_t nFrameStart = strtoll(pStart, &pEnd, 10); + if(pEnd == pStart || *pEnd != '/' || *pEnd == '\0') + { + return EMICROPROFILE_GET_COMMAND_UNKNOWN; + } + pStart = pEnd + 1; + + uint64_t nFrameEnd = strtoll(pStart, &pEnd, 10); + + if(pEnd == pStart || nFrameEnd <= nFrameStart) + { + return EMICROPROFILE_GET_COMMAND_UNKNOWN; + } + pResult->nFrames = nFrameEnd - nFrameStart; + pResult->nFrameStart = nFrameStart; + return EMICROPROFILE_GET_COMMAND_DUMP_RANGE; + } + while(*pGet != '\0') + { + if(*pGet < '0' || *pGet > '9') + return EMICROPROFILE_GET_COMMAND_UNKNOWN; + pGet++; + } + int nFrames = atoi(pStart); + pResult->nFrameStart = (uint64_t)-1; + if(nFrames) + { + pResult->nFrames = nFrames; + } + else + { + pResult->nFrames = MICROPROFILE_WEBSERVER_DEFAULT_FRAMES; + } + return EMICROPROFILE_GET_COMMAND_DUMP; +} + +void MicroProfileBase64Encode(char* pOut, const uint8_t* pIn, uint32_t nLen) +{ + static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + //..straight from wikipedia. + int b; + char* o = pOut; + for(uint32_t i = 0; i < nLen; i += 3) + { + b = (pIn[i] & 0xfc) >> 2; + *o++ = CODES[b]; + b = (pIn[i] & 0x3) << 4; + if(i + 1 < nLen) + { + b |= (pIn[i + 1] & 0xF0) >> 4; + *o++ = CODES[b]; + b = (pIn[i + 1] & 0x0F) << 2; + if(i + 2 < nLen) + { + b |= (pIn[i + 2] & 0xC0) >> 6; + *o++ = CODES[b]; + b = pIn[i + 2] & 0x3F; + *o++ = CODES[b]; + } + else + { + *o++ = CODES[b]; + *o++ = '='; + } + } + else + { + *o++ = CODES[b]; + *o++ = '='; + *o++ = '='; + } + } +} + +// begin: SHA-1 in C +// ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c +// SHA-1 in C +// By Steve Reid +// 100% Public Domain + +typedef struct +{ + uint32_t state[5]; + uint32_t count[2]; + unsigned char buffer[64]; +} MicroProfile_SHA1_CTX; +#include +#ifndef _WIN32 +#include +#endif + +static void MicroProfile_SHA1_Transform(uint32_t[5], const unsigned char[64]); + +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) + +#define blk0(i) (block->l[i] = htonl(block->l[i])) +#define blk(i) (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1)) + +#define R0(v, w, x, y, z, i) \ + z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \ + w = rol(w, 30); +#define R1(v, w, x, y, z, i) \ + z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \ + w = rol(w, 30); +#define R2(v, w, x, y, z, i) \ + z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \ + w = rol(w, 30); +#define R3(v, w, x, y, z, i) \ + z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \ + w = rol(w, 30); +#define R4(v, w, x, y, z, i) \ + z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \ + w = rol(w, 30); + +// Hash a single 512-bit block. This is the core of the algorithm. + +static void MicroProfile_SHA1_Transform(uint32_t state[5], const unsigned char buffer[64]) +{ + uint32_t a, b, c, d, e; + typedef union + { + unsigned char c[64]; + uint32_t l[16]; + } CHAR64LONG16; + CHAR64LONG16* block; + + block = (CHAR64LONG16*)buffer; + // Copy context->state[] to working vars + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + // 4 rounds of 20 operations each. Loop unrolled. + R0(a, b, c, d, e, 0); + R0(e, a, b, c, d, 1); + R0(d, e, a, b, c, 2); + R0(c, d, e, a, b, 3); + R0(b, c, d, e, a, 4); + R0(a, b, c, d, e, 5); + R0(e, a, b, c, d, 6); + R0(d, e, a, b, c, 7); + R0(c, d, e, a, b, 8); + R0(b, c, d, e, a, 9); + R0(a, b, c, d, e, 10); + R0(e, a, b, c, d, 11); + R0(d, e, a, b, c, 12); + R0(c, d, e, a, b, 13); + R0(b, c, d, e, a, 14); + R0(a, b, c, d, e, 15); + R1(e, a, b, c, d, 16); + R1(d, e, a, b, c, 17); + R1(c, d, e, a, b, 18); + R1(b, c, d, e, a, 19); + R2(a, b, c, d, e, 20); + R2(e, a, b, c, d, 21); + R2(d, e, a, b, c, 22); + R2(c, d, e, a, b, 23); + R2(b, c, d, e, a, 24); + R2(a, b, c, d, e, 25); + R2(e, a, b, c, d, 26); + R2(d, e, a, b, c, 27); + R2(c, d, e, a, b, 28); + R2(b, c, d, e, a, 29); + R2(a, b, c, d, e, 30); + R2(e, a, b, c, d, 31); + R2(d, e, a, b, c, 32); + R2(c, d, e, a, b, 33); + R2(b, c, d, e, a, 34); + R2(a, b, c, d, e, 35); + R2(e, a, b, c, d, 36); + R2(d, e, a, b, c, 37); + R2(c, d, e, a, b, 38); + R2(b, c, d, e, a, 39); + R3(a, b, c, d, e, 40); + R3(e, a, b, c, d, 41); + R3(d, e, a, b, c, 42); + R3(c, d, e, a, b, 43); + R3(b, c, d, e, a, 44); + R3(a, b, c, d, e, 45); + R3(e, a, b, c, d, 46); + R3(d, e, a, b, c, 47); + R3(c, d, e, a, b, 48); + R3(b, c, d, e, a, 49); + R3(a, b, c, d, e, 50); + R3(e, a, b, c, d, 51); + R3(d, e, a, b, c, 52); + R3(c, d, e, a, b, 53); + R3(b, c, d, e, a, 54); + R3(a, b, c, d, e, 55); + R3(e, a, b, c, d, 56); + R3(d, e, a, b, c, 57); + R3(c, d, e, a, b, 58); + R3(b, c, d, e, a, 59); + R4(a, b, c, d, e, 60); + R4(e, a, b, c, d, 61); + R4(d, e, a, b, c, 62); + R4(c, d, e, a, b, 63); + R4(b, c, d, e, a, 64); + R4(a, b, c, d, e, 65); + R4(e, a, b, c, d, 66); + R4(d, e, a, b, c, 67); + R4(c, d, e, a, b, 68); + R4(b, c, d, e, a, 69); + R4(a, b, c, d, e, 70); + R4(e, a, b, c, d, 71); + R4(d, e, a, b, c, 72); + R4(c, d, e, a, b, 73); + R4(b, c, d, e, a, 74); + R4(a, b, c, d, e, 75); + R4(e, a, b, c, d, 76); + R4(d, e, a, b, c, 77); + R4(c, d, e, a, b, 78); + R4(b, c, d, e, a, 79); + // Add the working vars back into context.state[] + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + // Wipe variables + a = b = c = d = e = 0; +} + +void MicroProfile_SHA1_Init(MicroProfile_SHA1_CTX* context) +{ + // SHA1 initialization constants + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; +} + +// Run your data through this. + +void MicroProfile_SHA1_Update(MicroProfile_SHA1_CTX* context, const unsigned char* data, unsigned int len) +{ + unsigned int i, j; + + j = (context->count[0] >> 3) & 63; + if((context->count[0] += len << 3) < (len << 3)) + context->count[1]++; + context->count[1] += (len >> 29); + i = 64 - j; + while(len >= i) + { + memcpy(&context->buffer[j], data, i); + MicroProfile_SHA1_Transform(context->state, context->buffer); + data += i; + len -= i; + i = 64; + j = 0; + } + + memcpy(&context->buffer[j], data, len); +} + +// Add padding and return the message digest. + +void MicroProfile_SHA1_Final(unsigned char digest[20], MicroProfile_SHA1_CTX* context) +{ + uint32_t i, j; + unsigned char finalcount[8]; + + for(i = 0; i < 8; i++) + { + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); // Endian independent + } + MicroProfile_SHA1_Update(context, (unsigned char*)"\200", 1); + while((context->count[0] & 504) != 448) + { + MicroProfile_SHA1_Update(context, (unsigned char*)"\0", 1); + } + MicroProfile_SHA1_Update(context, finalcount, 8); // Should cause a SHA1Transform() + for(i = 0; i < 20; i++) + { + digest[i] = (unsigned char)((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); + } + // Wipe variables + i = j = 0; + memset(context->buffer, 0, 64); + memset(context->state, 0, 20); + memset(context->count, 0, 8); + memset(&finalcount, 0, 8); +} + +#undef rol +#undef blk0 +#undef blk +#undef R0 +#undef R1 +#undef R2 +#undef R3 +#undef R4 + +// end: SHA-1 in C + +void MicroProfileWebSocketSendState(MpSocket C); +void MicroProfileWebSocketSendEnabled(MpSocket C); +void MicroProfileWSPrintStart(MpSocket C); +void MicroProfileWSPrintf(const char* pFmt, ...); +void MicroProfileWSPrintEnd(); +void MicroProfileWSFlush(); +bool MicroProfileWebSocketReceive(MpSocket C); + +enum +{ + TYPE_NONE = 0, + TYPE_TIMER = 1, + TYPE_GROUP = 2, + TYPE_CATEGORY = 3, + TYPE_SETTING = 4, + TYPE_COUNTER = 5, +}; + +enum +{ + SETTING_FORCE_ENABLE = 0, + SETTING_CONTEXT_SWITCH_TRACE = 1, + SETTING_PLATFORM_MARKERS = 2, +}; + +enum +{ + MSG_TIMER_TREE = 1, + MSG_ENABLED = 2, + MSG_FRAME = 3, + MSG_LOADSETTINGS = 4, + MSG_PRESETS = 5, + MSG_CURRENTSETTINGS = 6, + MSG_COUNTERS = 7, + MSG_FUNCTION_RESULTS = 8, + MSG_INACTIVE_FRAME = 9, + MSG_FUNCTION_NAMES = 10, + MSG_INSTRUMENT_ERROR = 11, + MSG_QUERY_INDEX = 12, + // MSG_MODULE_NAME = 12, +}; + +enum +{ + VIEW_GRAPH_SPLIT = 0, + VIEW_GRAPH_PERCENTILE = 1, + VIEW_GRAPH_THREAD_GROUP = 2, + VIEW_BAR = 3, + VIEW_BAR_ALL = 4, + VIEW_BAR_SINGLE = 5, + VIEW_COUNTERS = 6, + VIEW_SIZE = 7, +}; + +void MicroProfileSocketDumpState() +{ + fd_set Read, Write, Error; + FD_ZERO(&Read); + FD_ZERO(&Write); + FD_ZERO(&Error); + MpSocket LastSocket = 1; + for(uint32_t i = 0; i < S.nNumWebSockets; ++i) + { + LastSocket = MicroProfileMax(LastSocket, S.WebSockets[i] + 1); + FD_SET(S.WebSockets[i], &Read); + FD_SET(S.WebSockets[i], &Write); + FD_SET(S.WebSockets[i], &Error); + } + timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 0; + + if(-1 == select(LastSocket, &Read, &Write, &Error, &tv)) + { + MP_ASSERT(0); + } + for(uint32_t i = 0; i < S.nNumWebSockets; i++) + { + MpSocket s = S.WebSockets[i]; + uprintf("%" PRId64 " ", (uint64_t)s); + + if(FD_ISSET(s, &Error)) + { + uprintf("e"); + } + else + { + uprintf("_"); + } + if(FD_ISSET(s, &Read)) + { + uprintf("r"); + } + else + { + uprintf(" "); + } + if(FD_ISSET(s, &Write)) + { + uprintf("w"); + } + else + { + uprintf(" "); + } + } + uprintf("\n"); + for(uint32_t i = 1; i < S.nNumWebSockets; i++) + { + MpSocket s = S.WebSockets[i]; + int error_code; + socklen_t error_code_size = sizeof(error_code); + int r = getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&error_code, &error_code_size); + MP_ASSERT(r >= 0); + if(error_code != 0) + { +#ifdef _WIN32 + char buffer[1024]; + strerror_s(buffer, sizeof(buffer) - 1, error_code); + fprintf(stderr, "socket error: %d %s\n", (int)s, buffer); +#else + fprintf(stderr, "socket error: %d %s\n", (int)s, strerror(error_code)); +#endif + MP_ASSERT(0); + } + } +} + +bool MicroProfileSocketSend2(MpSocket Connection, const void* pMessage, int nLen); +void* MicroProfileSocketSenderThread(void*) +{ + MicroProfileOnThreadCreate("MicroProfileSocketSenderThread"); + while(!S.nMicroProfileShutdown) + { + if(S.nSocketFail) + { + MicroProfileSleep(100); + continue; + } + + uint32_t nEnd = MICROPROFILE_WEBSOCKET_BUFFER_SIZE; + uint32_t nGet = S.WSBuf.nSendGet.load(); + uint32_t nPut = S.WSBuf.nSendPut.load(); + uint32_t nSendStart = 0; + uint32_t nSendAmount = 0; + if(nGet > nPut) + { + nSendStart = nGet; + nSendAmount = nEnd - nGet; + } + else if(nGet < nPut) + { + nSendStart = nGet; + nSendAmount = nPut - nGet; + } + + if(nSendAmount) + { + MICROPROFILE_SCOPE(g_MicroProfileSendLoop); + MICROPROFILE_COUNTER_LOCAL_ADD_ATOMIC(g_MicroProfileBytesPerFlip, nSendAmount); + if(!MicroProfileSocketSend2(S.WebSockets[0], &S.WSBuf.SendBuffer[nSendStart], nSendAmount)) + { + S.nSocketFail = 1; + } + else + { + S.WSBuf.nSendGet.store((nGet + nSendAmount) % MICROPROFILE_WEBSOCKET_BUFFER_SIZE); + } + } + else + { + MicroProfileSleep(20); + } + } + MicroProfileOnThreadExit(); + return 0; +} + +void MicroProfileSocketSend(MpSocket Connection, const void* pMessage, int nLen) +{ + if(S.nSocketFail || nLen <= 0) + { + return; + } + MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileSocketSend", MP_GREEN4); + while(nLen != 0) + { + MP_ASSERT(nLen > 0); + uint32_t nEnd = MICROPROFILE_WEBSOCKET_BUFFER_SIZE; + uint32_t nGet = S.WSBuf.nSendGet.load(); + uint32_t nPut = S.WSBuf.nSendPut.load(); + uint32_t nAmount = 0; + if(nPut < nGet) + { + nAmount = nGet - nPut - 1; + } + else + { + if(nGet == 0) + { + nAmount = nEnd - nPut - 1; + } + else + { + nAmount = nEnd - nPut; + } + } + MP_ASSERT((int)nAmount >= 0); + nAmount = MicroProfileMin(nLen, (int)nAmount); + if(nAmount) + { + memcpy(&S.WSBuf.SendBuffer[nPut], pMessage, nAmount); + pMessage = (void*)((char*)pMessage + nAmount); + nLen -= nAmount; + S.WSBuf.nSendPut.store((nPut + nAmount) % MICROPROFILE_WEBSOCKET_BUFFER_SIZE); + } + else + { + if(S.nSocketFail) + { + return; + } + MicroProfileSleep(20); + } + } +} + +bool MicroProfileSocketSend2(MpSocket Connection, const void* pMessage, int nLen) +{ + if(S.nSocketFail || nLen <= 0) + { + return false; + } + // MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileSocketSend2", 0); +#ifndef _WIN32 + int error_code; + socklen_t error_code_size = sizeof(error_code); + getsockopt(Connection, SOL_SOCKET, SO_ERROR, &error_code, &error_code_size); + if(error_code != 0) + { + return false; + } +#endif + + int s = 0; + while(nLen) + { + s = send(Connection, (const char*)pMessage, nLen, 0); + if(s < 0) + { + const int error = errno; + if(error == EAGAIN || error == EWOULDBLOCK) + { + MicroProfileSleep(20); + continue; + } + break; + } + + nLen -= s; + pMessage = (const char*)pMessage + s; + } +#ifdef _WIN32 + if(s == SOCKET_ERROR) + { + return false; + } +#endif + if(s < 0) + { + return false; + } + return true; +} + +uint32_t MicroProfileWebSocketIdPack(uint32_t type, uint32_t element) +{ + MP_ASSERT(type < 255); + MP_ASSERT(element < 0xffffff); + return type << 24 | element; +} +void MicroProfileWebSocketIdUnpack(uint32_t nPacked, uint32_t& type, uint32_t& element) +{ + type = (nPacked >> 24) & 0xff; + element = nPacked & 0xffffff; +} + +struct MicroProfileWebSocketHeader0 +{ + union + { + struct + { + uint8_t opcode : 4; + uint8_t RSV3 : 1; + uint8_t RSV2 : 1; + uint8_t RSV1 : 1; + uint8_t FIN : 1; + }; + uint8_t v; + }; +}; + +struct MicroProfileWebSocketHeader1 +{ + union + { + struct + { + uint8_t payload : 7; + uint8_t MASK : 1; + }; + uint8_t v; + }; +}; + +bool MicroProfileWebSocketSend(MpSocket Connection, const char* pMessage, uint64_t nLen) +{ + MicroProfileWebSocketHeader0 h0; + MicroProfileWebSocketHeader1 h1; + h0.v = 0; + h1.v = 0; + h0.opcode = 1; + h0.FIN = 1; + uint32_t nExtraSizeBytes = 0; + uint8_t nExtraSize[8]; + if(nLen > 125) + { + if(nLen > 0xffff) + { + nExtraSizeBytes = 8; + h1.payload = 127; + } + else + { + h1.payload = 126; + nExtraSizeBytes = 2; + } + uint64_t nCount = nLen; + for(uint32_t i = 0; i < nExtraSizeBytes; ++i) + { + nExtraSize[nExtraSizeBytes - i - 1] = nCount & 0xff; + nCount >>= 8; + } + + uint32_t nSize = 0; + for(uint32_t i = 0; i < nExtraSizeBytes; i++) + { + nSize <<= 8; + nSize += nExtraSize[i]; + } + MP_ASSERT(nSize == nLen); // verify + } + else + { + h1.payload = nLen; + } + MP_ASSERT(pMessage == S.WSBuf.pBuffer); // space for header is preallocated here + MP_ASSERT(pMessage == S.WSBuf.pBufferAllocation + 20); // space for header is preallocated here + MP_ASSERT(nExtraSizeBytes < 18); + char* pTmp = (char*)(pMessage - nExtraSizeBytes - 2); + memcpy(pTmp + 2, &nExtraSize[0], nExtraSizeBytes); + pTmp[1] = *(char*)&h1; + pTmp[0] = *(char*)&h0; +// MicroProfileSocketSend(Connection, pTmp, nExtraSizeBytes + 2 + nLen); +#if 1 + MicroProfileSocketSend(Connection, &h0, 1); + MicroProfileSocketSend(Connection, &h1, 1); + if(nExtraSizeBytes) + { + MicroProfileSocketSend(Connection, &nExtraSize[0], nExtraSizeBytes); + } + MicroProfileSocketSend(Connection, pMessage, nLen); +#endif + return true; +} + +void MicroProfileWebSocketClearTimers() +{ + while(S.WebSocketTimers > -1) + { + int nNext = S.TimerInfo[S.WebSocketTimers].nWSNext; + S.TimerInfo[S.WebSocketTimers].nWSNext = -2; + S.WebSocketTimers = nNext; + } + MP_ASSERT(S.WebSocketTimers == -1); + while(S.WebSocketCounters > -1) + { + int nNext = S.CounterInfo[S.WebSocketCounters].nWSNext; + S.CounterInfo[S.WebSocketCounters].nWSNext = -2; + S.WebSocketCounters = nNext; + } + MP_ASSERT(S.WebSocketCounters == -1); + + while(S.WebSocketGroups > -1) + { + int nNext = S.GroupInfo[S.WebSocketGroups].nWSNext; + S.GroupInfo[S.WebSocketGroups].nWSNext = -2; + S.WebSocketGroups = nNext; + } + MP_ASSERT(S.WebSocketGroups == -1); + S.nWebSocketDirty |= MICROPROFILE_WEBSOCKET_DIRTY_ENABLED; +} +void MicroProfileWebSocketToggleTimer(uint32_t nTimer) +{ + if(nTimer < S.nTotalTimers) + { + auto& TI = S.TimerInfo[nTimer]; + int* pPrev = &S.WebSocketTimers; + while(*pPrev > -1 && *pPrev != (int)nTimer) + { + MP_ASSERT(*pPrev < (int)S.nTotalTimers && *pPrev >= 0); + pPrev = &S.TimerInfo[*pPrev].nWSNext; + } + if(TI.nWSNext >= -1) + { + MP_ASSERT(*pPrev == (int)nTimer); + *pPrev = TI.nWSNext; + TI.nWSNext = -2; + } + else + { + MP_ASSERT(*pPrev == -1); + TI.nWSNext = -1; + *pPrev = (int)nTimer; + } + S.nWebSocketDirty |= MICROPROFILE_WEBSOCKET_DIRTY_ENABLED; + } +} + +void MicroProfileWebSocketToggleCounter(uint32_t nCounter) +{ + if(nCounter < S.nNumCounters) + { + auto& TI = S.CounterInfo[nCounter]; + int* pPrev = &S.WebSocketCounters; + while(*pPrev > -1 && *pPrev != (int)nCounter) + { + MP_ASSERT(*pPrev < (int)S.nNumCounters && *pPrev >= 0); + pPrev = &S.CounterInfo[*pPrev].nWSNext; + } + if(TI.nWSNext >= -1) + { + MP_ASSERT(*pPrev == (int)nCounter); + *pPrev = TI.nWSNext; + TI.nWSNext = -2; + } + else + { + MP_ASSERT(*pPrev == -1); + TI.nWSNext = -1; + *pPrev = (int)nCounter; + } + S.nWebSocketDirty |= MICROPROFILE_WEBSOCKET_DIRTY_ENABLED; + } +} + +void MicroProfileWebSocketToggleGroup(uint32_t nGroup) +{ + if(nGroup < S.nGroupCount) + { + auto& TI = S.GroupInfo[nGroup]; + int* pPrev = &S.WebSocketGroups; + while(*pPrev > -1 && *pPrev != (int)nGroup) + { + MP_ASSERT(*pPrev < (int)S.nGroupCount && *pPrev >= 0); + pPrev = &S.GroupInfo[*pPrev].nWSNext; + } + if(TI.nWSNext >= -1) + { + MP_ASSERT(*pPrev == (int)nGroup); + *pPrev = TI.nWSNext; + TI.nWSNext = -2; + } + else + { + MP_ASSERT(*pPrev == -1); + TI.nWSNext = -1; + *pPrev = (int)nGroup; + } + S.nWebSocketDirty |= MICROPROFILE_WEBSOCKET_DIRTY_ENABLED; + } +} + +bool MicroProfileWebSocketTimerEnabled(uint32_t nTimer) +{ + if(nTimer < S.nTotalTimers) + { + return S.TimerInfo[nTimer].nWSNext > -2; + } + return false; +} + +bool MicroProfileWebSocketCounterEnabled(uint32_t nCounter) +{ + if(nCounter < S.nNumCounters) + { + return S.CounterInfo[nCounter].nWSNext > -2; + } + return false; +} +void MicroProfileWebSocketCommand(uint32_t nCommand) +{ + uint32_t nType, nElement; + MicroProfileWebSocketIdUnpack(nCommand, nType, nElement); + switch(nType) + { + case TYPE_NONE: + break; + case TYPE_SETTING: + switch(nElement) + { + case SETTING_FORCE_ENABLE: + MicroProfileSetEnableAllGroups(!MicroProfileGetEnableAllGroups()); + break; + case SETTING_CONTEXT_SWITCH_TRACE: + if(!S.bContextSwitchRunning) + { + MicroProfileStartContextSwitchTrace(); + } + else + { + MicroProfileStopContextSwitchTrace(); + } + break; + case SETTING_PLATFORM_MARKERS: + MicroProfilePlatformMarkersSetEnabled(!MicroProfilePlatformMarkersGetEnabled()); + break; + } + S.nWebSocketDirty |= MICROPROFILE_WEBSOCKET_DIRTY_ENABLED; + break; + case TYPE_TIMER: + MicroProfileWebSocketToggleTimer(nElement); + break; + case TYPE_GROUP: + MicroProfileToggleGroup(nElement); + break; + case TYPE_CATEGORY: + MicroProfileToggleCategory(nElement); + break; + case TYPE_COUNTER: + MicroProfileWebSocketToggleCounter(nElement); + break; + default: + uprintf("unknown type %d\n", nType); + } +} +#define MICROPROFILE_PRESET_HEADER_MAGIC2 0x28586813 +#define MICROPROFILE_PRESET_HEADER_VERSION2 0x00000200 + +struct MicroProfileSettingsFileHeader +{ + uint32_t nMagic; + uint32_t nVersion; + uint32_t nNumHeaders; + uint32_t nHeadersOffset; + uint32_t nMaxJsonSize; + uint32_t nMaxNameSize; +}; +struct MicroProfileSettingsHeader +{ + uint32_t nJsonOffset; + uint32_t nJsonSize; + uint32_t nNameOffset; + uint32_t nNameSize; +}; + +template +void MicroProfileParseSettings(const char* pFileName, T CB) +{ + std::lock_guard Lock(MicroProfileGetMutex()); + + FILE* F = fopen(pFileName, "rb"); + if(!F) + { + return; + } + long nFileSize = 0; + fseek(F, 0, SEEK_END); + nFileSize = ftell(F); + char* pFile = 0; + char* pAlloc = 0; + if(nFileSize > (32 << 10)) + { + pFile = pAlloc = (char*)MP_ALLOC(nFileSize + 1, 1); + } + else + { + pFile = (char*)alloca(nFileSize + 1); + } + fseek(F, 0, SEEK_SET); + if(1 != fread(pFile, nFileSize, 1, F)) + { + uprintf("failed to read settings file\n"); + fclose(F); + return; + } + fclose(F); + pFile[nFileSize] = '\0'; + + char* pPos = pFile; + char* pEnd = pFile + nFileSize; + + while(pPos != pEnd) + { + const char* pName = 0; + int nNameLen = 0; + const char* pJson = 0; + int nJsonLen = 0; + int Failed = 0; + + auto SkipWhite = [&](char* pPos, const char* pEnd) + { + while(pPos != pEnd) + { + if(isspace(*pPos)) + { + pPos++; + } + else if('#' == *pPos) + { + while(pPos != pEnd && *pPos != '\n') + { + ++pPos; + } + } + else + { + break; + } + } + return pPos; + }; + + auto ParseName = [&](char* pPos, char* pEnd, const char** ppName, int* pLen) + { + pPos = SkipWhite(pPos, pEnd); + int nLen = 0; + *ppName = pPos; + + while(pPos != pEnd && (isalpha(*pPos) || isdigit(*pPos) || *pPos == '_')) + { + nLen++; + pPos++; + } + *pLen = nLen; + if(pPos == pEnd || !isspace(*pPos)) + { + Failed = 1; + return pEnd; + } + *pPos++ = '\0'; + return pPos; + }; + + auto ParseJson = [&](char* pPos, char* pEnd, const char** pJson, int* pLen) -> char* + { + pPos = SkipWhite(pPos, pEnd); + if(*pPos != '{' || pPos == pEnd) + { + Failed = 1; + return pPos; + } + *pJson = pPos++; + int nLen = 1; + int nDepth = 1; + while(pPos != pEnd && nDepth != 0) + { + nLen++; + char nChar = *pPos++; + if(nChar == '{') + { + nDepth++; + } + else if(nChar == '}') + { + nDepth--; + } + } + if(pPos == pEnd || !isspace(*pPos)) + { + Failed = 1; + return pEnd; + } + *pLen = nLen; + *pPos++ = '\0'; + return pPos; + }; + + pPos = ParseName(pPos, pEnd, &pName, &nNameLen); + pPos = ParseJson(pPos, pEnd, &pJson, &nJsonLen); + if(Failed) + { + break; + } + if(!CB(pName, nNameLen, pJson, nJsonLen)) + { + break; + } + } + if(pAlloc) + MP_FREE(pAlloc); +} + +bool MicroProfileSavePresets(const char* pSettingsName, const char* pJsonSettings) +{ + std::lock_guard Lock(MicroProfileGetMutex()); + + FILE* F = fopen(S.pSettingsTemp, "w"); + if(!F) + { + return false; + } + + bool bWritten = false; + + MicroProfileParseSettings(S.pSettings, + [&](const char* pName, uint32_t nNameSize, const char* pJson, uint32_t nJsonSize) -> bool + { + fwrite(pName, nNameSize, 1, F); + fputc(' ', F); + if(0 != MP_STRCASECMP(pSettingsName, pName)) + { + fwrite(pJson, nJsonSize, 1, F); + } + else + { + bWritten = true; + fwrite(pJsonSettings, strlen(pJsonSettings), 1, F); + } + fputc('\n', F); + return true; + }); + if(!bWritten) + { + fwrite(pSettingsName, strlen(pSettingsName), 1, F); + fputc(' ', F); + fwrite(pJsonSettings, strlen(pJsonSettings), 1, F); + fputc('\n', F); + } + fflush(F); + fclose(F); +#ifdef MICROPROFILE_MOVE_FILE + MICROPROFILE_MOVE_FILE(S.pSettingsTemp, S.pSettings); +#elif defined(_WIN32) + MoveFileExA(S.pSettingsTemp, S.pSettings, MOVEFILE_REPLACE_EXISTING); +#else + rename(S.pSettingsTemp, S.pSettings); +#endif + return false; +} + +void MicroProfileWriteJsonString(const char* pJson, uint32_t nJsonLen) +{ + char* pCur = (char*)pJson; + char* pEnd = pCur + nJsonLen; + MicroProfileWSPrintf("\"", pCur); + while(pCur != pEnd) + { + char* pTag = strchr(pCur, '\"'); + if(pTag) + { + *pTag = '\0'; + MicroProfileWSPrintf("%s\\\"", pCur); + *pTag = '\"'; + pCur = pTag + 1; + } + else + { + MicroProfileWSPrintf("%s\"", pCur); + pCur = pEnd; + } + } +}; + +void MicroProfileWebSocketSendPresets(MpSocket Connection) +{ + std::lock_guard Lock(MicroProfileGetMutex()); + uprintf("sending presets ... \n"); + MicroProfileWSPrintStart(Connection); + MicroProfileWSPrintf("{\"k\":\"%d\",\"v\":{", MSG_PRESETS); + MicroProfileWSPrintf("\"p\":{\"Default\":\"{}\""); + + MicroProfileParseSettings(S.pSettings, + [](const char* pName, uint32_t nNameLen, const char* pJson, uint32_t nJsonLen) + { + MicroProfileWSPrintf(",\"%s\":", pName); + MicroProfileWriteJsonString(pJson, nJsonLen); + + return true; + }); + MicroProfileWSPrintf("},\"r\":{"); + bool bFirst = true; + MicroProfileParseSettings(S.pSettingsReadOnly, + [&bFirst](const char* pName, uint32_t nNameLen, const char* pJson, uint32_t nJsonLen) + { + MicroProfileWSPrintf("%c\"%s\":", bFirst ? ' ' : ',', pName); + MicroProfileWriteJsonString(pJson, nJsonLen); + + bFirst = false; + return true; + }); + MicroProfileWSPrintf("}}}"); + MicroProfileWSFlush(); + MicroProfileWSPrintEnd(); +} + +#define LOAD_PRESET_DEFAULT 0x1 +#define LOAD_PRESET_READONLY 0x2 + +void MicroProfileLoadPresets(const char* pSettingsName, uint32_t nLoadPresetType) +{ + std::lock_guard Lock(MicroProfileGetMutex()); + const char* pPresetFiles[] = { S.pSettings, S.pSettingsReadOnly }; + for(uint32_t i = 0; i < 2; ++i) + { + if(nLoadPresetType & (1u << i)) + { + const char* pPresetFile = pPresetFiles[i]; + bool bReadOnly = (1u << i) == LOAD_PRESET_READONLY; + bool bSuccess = false; + MicroProfileParseSettings(pPresetFile, + [&bSuccess, bReadOnly, pSettingsName](const char* pName, uint32_t l0, const char* pJson, uint32_t l1) + { + if(0 == MP_STRCASECMP(pName, pSettingsName)) + { + uint32_t nLen = (uint32_t)strlen(pJson) + 1; + if(nLen > S.nJsonSettingsBufferSize) + { + if(S.pJsonSettings) + S.pJsonSettings = nullptr; + S.pJsonSettings = (char*)MP_ALLOC(nLen, 1); + S.nJsonSettingsBufferSize = nLen; + } + S.pJsonSettingsName = pSettingsName; + memcpy(S.pJsonSettings, pJson, nLen); + S.nJsonSettingsPending = 1; + S.bJsonSettingsReadOnly = bReadOnly ? 1 : 0; + bSuccess = true; + return false; + } + return true; + }); + if(bSuccess) + return; + } + } +} + +bool MicroProfileWebSocketReceive(MpSocket Connection) +{ + + // 0 1 2 3 + // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + // +-+-+-+-+-------+-+-------------+-------------------------------+ + // |F|R|R|R| opcode|M| Payload len | Extended payload length | + // |I|S|S|S| (4) |A| (7) | (16/64) | + // |N|V|V|V| |S| | (if payload len==126/127) | + // | |1|2|3| |K| | | + // +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + + int r; + uint64_t nSize; + uint64_t nSizeBytes = 0; + uint8_t Mask[4]; + static unsigned char* Bytes = 0; + static uint64_t BytesAllocated = 0; + MicroProfileWebSocketHeader0 h0; + MicroProfileWebSocketHeader1 h1; + static_assert(sizeof(h0) == 1, ""); + static_assert(sizeof(h1) == 1, ""); + r = recv(Connection, (char*)&h0, 1, 0); + if(1 != r) + goto fail; + r = recv(Connection, (char*)&h1, 1, 0); + if(1 != r) + goto fail; + + if(h0.v == 0x88) + { + goto fail; + } + + if(h0.RSV1 != 0 || h0.RSV2 != 0 || h0.RSV3 != 0) + goto fail; + + nSize = h1.payload; + nSizeBytes = 0; + switch(nSize) + { + case 126: + nSizeBytes = 2; + break; + case 127: + nSizeBytes = 8; + break; + default: + break; + } + if(nSizeBytes) + { + nSize = 0; + uint64_t MessageLength = 0; + + uint8_t BytesMessage[8]; + r = recv(Connection, (char*)&BytesMessage[0], nSizeBytes, 0); + if((int)nSizeBytes != r) + goto fail; + for(uint32_t i = 0; i < nSizeBytes; i++) + { + nSize <<= 8; + nSize += BytesMessage[i]; + } + + for(uint32_t i = 0; i < nSizeBytes; i++) + MessageLength |= BytesMessage[i] << ((nSizeBytes - 1 - i) * 8); + MP_ASSERT(MessageLength == nSize); + } + + if(h1.MASK) + { + recv(Connection, (char*)&Mask[0], 4, 0); + } + + MICROPROFILE_COUNTER_LOCAL_ADD_ATOMIC(g_MicroProfileBytesPerFlip, nSize); + if(nSize + 1 > BytesAllocated) + { + Bytes = (unsigned char*)MP_REALLOC(Bytes, nSize + 1); + BytesAllocated = nSize + 1; + } + recv(Connection, (char*)Bytes, nSize, 0); + for(uint32_t i = 0; i < nSize; ++i) + Bytes[i] ^= Mask[i & 3]; + + Bytes[nSize] = '\0'; + switch(Bytes[0]) + { + case 'a': + { + S.nAggregateFlip = strtoll((const char*)&Bytes[1], nullptr, 10); + } + break; + case 's': + { + char* pJson = strchr((char*)Bytes, ','); + if(pJson && *pJson != '\0') + { + *pJson = '\0'; + MicroProfileSavePresets((const char*)Bytes + 1, (const char*)pJson + 1); + } + break; + } + + case 'l': + { + MicroProfileLoadPresets((const char*)Bytes + 1, LOAD_PRESET_DEFAULT); + break; + } + case 'm': + { + MicroProfileLoadPresets((const char*)Bytes + 1, LOAD_PRESET_READONLY); + break; + } + case 'd': + { + MicroProfileWebSocketClearTimers(); + memset(&S.nActiveGroupsWanted, 0, sizeof(S.nActiveGroupsWanted)); + S.nWebSocketDirty |= MICROPROFILE_WEBSOCKET_DIRTY_ENABLED; + break; + } + case 'c': + { + char* pStr = (char*)Bytes + 1; + char* pEnd = pStr + nSize - 1; + uint32_t Message = strtol(pStr, &pEnd, 10); + MicroProfileWebSocketCommand(Message); + } + break; + case 'f': + MicroProfileToggleFrozen(); + break; + case 'v': + S.nWSViewMode = (int)Bytes[1] - '0'; + break; + case 'r': + uprintf("got clear message\n"); + S.nAggregateClear = 1; + break; + case 'x': + MicroProfileWebSocketClearTimers(); + break; +#if MICROPROFILE_DYNAMIC_INSTRUMENT + case 'D': // instrumentation without loading queryable symbols. + { + uprintf("got INSTRUMENT Message: %s\n", (const char*)&Bytes[0]); + char* pGet = (char*)&Bytes[1]; + uint32_t nNumArguments = 0; +#ifdef _WIN32 + r = sscanf_s(pGet, "%d", &nNumArguments); +#else + r = sscanf(pGet, "%d", &nNumArguments); +#endif + if(r != 1) + { + uprintf("failed to parse..\n"); + break; + } + while(' ' == *pGet || (*pGet >= '0' && *pGet <= '9')) + { + pGet++; + } + if(nNumArguments > 200) + nNumArguments = 200; + uint32_t nParsedArguments = 0; + const char* pModule = 0; + const char* pSymbol = 0; + const char** pModules = (const char**)(alloca(sizeof(const char*) * nNumArguments)); + const char** pSymbols = (const char**)(alloca(sizeof(const char*) * nNumArguments)); + auto Next = [&pGet]() -> const char* + { + if(!pGet) + return 0; + const char* pRet = pGet; + pGet = (char*)strchr(pRet, '!'); + if(!pGet) + { + return 0; + } + *pGet++ = '\0'; + return (const char*)pRet; + }; + do + { + pModule = Next(); + pSymbol = Next(); + if(pModule && pSymbol) + { + pModules[nParsedArguments] = pModule; + pSymbols[nParsedArguments] = pSymbol; + uprintf("found symbol %s ::: %s \n", pModule, pSymbol); + nParsedArguments++; + if(nParsedArguments == nNumArguments) + { + break; + } + } + } while(pGet); + + MicroProfileInstrumentWithoutSymbols(pModules, pSymbols, nParsedArguments); + + break; + } + case 'I': + case 'i': + { + uprintf("got Message: %s\n", (const char*)&Bytes[0]); + void* p = 0; + uint32_t nColor = 0x0; + int nMinBytes = 0; + int nMaxCalls = 0; + int nCharsRead = 0; +#ifdef _WIN32 + r = sscanf_s((const char*)&Bytes[1], "%p %x %d %d%n", &p, &nColor, &nMinBytes, &nMaxCalls, &nCharsRead); +#else + r = sscanf((const char*)&Bytes[1], "%p %x %d %d%n", &p, &nColor, &nMinBytes, &nMaxCalls, &nCharsRead); +#endif + if(r == 4) + { + const char* pModule = (const char*)&Bytes[1]; + // int nNumChars = stbsp_snprintf(0, 0, "%p %x", p, nColor); + pModule += nCharsRead; + while(*pModule != ' ' && *pModule != '\0') + ++pModule; + + if(*pModule == '\0') + break; + + pModule++; + const char* pName = pModule; + while(*pName != '!' && *pName != '\0') + { + pName++; + } + if(*pName == '!') + { + // name and module seperately + *(char*)pName = '\0'; + pName++; + } + else + { + // name only + pName = pModule; + pModule = ""; + } + + uprintf("scanning for ptr %p %x mod:'%s' name'%s'\n", p, nColor, pModule, pName); + if(Bytes[0] == 'I') + { + MicroProfileInstrumentFunctionsCalled(p, pModule, pName, nMinBytes, nMaxCalls); + } + else + { + MicroProfileInstrumentFunction(p, pModule, pName, nColor); + } + } + } + break; + case 'S': + uprintf("loading symbols...\n"); + MicroProfileSymbolInitialize(true); + break; + case 'q': + MicroProfileSymbolQueryFunctions(Connection, 1 + (const char*)Bytes); + break; + case 'L': + uprintf("LOAD MODULE: '%s'\n", 1 + (const char*)Bytes); + MicroProfileSymbolInitialize(true, 1 + (const char*)Bytes); + break; +#else + case 'D': + case 'I': + case 'i': + case 'S': + case 'q': + case 'L': + break; +#endif + default: + uprintf("got unknown message size %lld: '%s'\n", (long long)nSize, Bytes); + } + return true; + +fail: + return false; +} +void MicroProfileWebSocketSendPresets(MpSocket Connection); + +void MicroProfileWebSocketHandshake(MpSocket Connection, char* pWebSocketKey) +{ + // reset web socket buffer + S.WSBuf.nSendPut.store(0); + S.WSBuf.nSendGet.store(0); + S.nSocketFail = 0; + + const char* pGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + const char* pHandShake = "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: "; + + char EncodeBuffer[512]; + int nLen = stbsp_snprintf(EncodeBuffer, sizeof(EncodeBuffer) - 1, "%s%s", pWebSocketKey, pGUID); + // uprintf("encode buffer is '%s' %d, %d\n", EncodeBuffer, nLen, (int)strlen(EncodeBuffer)); + + uint8_t sha[20]; + MicroProfile_SHA1_CTX ctx; + MicroProfile_SHA1_Init(&ctx); + MicroProfile_SHA1_Update(&ctx, (unsigned char*)EncodeBuffer, nLen); + MicroProfile_SHA1_Final((unsigned char*)&sha[0], &ctx); + char HashOut[(2 + sizeof(sha) / 3) * 4]; + memset(&HashOut[0], 0, sizeof(HashOut)); + MicroProfileBase64Encode(&HashOut[0], &sha[0], sizeof(sha)); + + char Reply[11024]; + nLen = stbsp_snprintf(Reply, sizeof(Reply) - 1, "%s%s\r\n\r\n", pHandShake, HashOut); + ; + MP_ASSERT(nLen >= 0); + MicroProfileSocketSend(Connection, Reply, nLen); + S.WebSockets[S.nNumWebSockets++] = Connection; + + S.WSCategoriesSent = 0; + S.WSGroupsSent = 0; + S.WSTimersSent = 0; + S.WSCountersSent = 0; + S.nJsonSettingsPending = 0; +#if MICROPROFILE_DYNAMIC_INSTRUMENT + S.WSFunctionsInstrumentedSent = 0; + S.WSSymbolModulesSent = 0; + { + uint64_t t0 = MP_TICK(); + MicroProfileSymbolUpdateModuleList(); + uint64_t t1 = MP_TICK(); + float fTime = float(MicroProfileTickToMsMultiplierCpu()) * (t1 - t0); + (void)fTime; + uprintf("update module list time %6.2fms\n", fTime); + } +#endif + + MicroProfileWebSocketSendState(Connection); + MicroProfileWebSocketSendPresets(Connection); + if(!S.nWSWasConnected) + { + S.nWSWasConnected = 1; + MicroProfileLoadPresets("Default", LOAD_PRESET_DEFAULT | LOAD_PRESET_READONLY); + } + else + { +#if MICROPROFILE_DYNAMIC_INSTRUMENT + MicroProfileWSPrintStart(Connection); + MicroProfileWSPrintf("{\"k\":\"%d\",\"qp\":%d}", MSG_QUERY_INDEX, S.nQueryProcessed); + MicroProfileWSFlush(); + MicroProfileWSPrintEnd(); +#endif + if(S.pJsonSettings) + { + MicroProfileWSPrintStart(Connection); + MicroProfileWSPrintf( + "{\"k\":\"%d\",\"ro\":%d,\"name\":\"%s\",\"v\":%s}", MSG_CURRENTSETTINGS, S.bJsonSettingsReadOnly ? 1 : 0, S.pJsonSettingsName ? S.pJsonSettingsName : "", S.pJsonSettings); + MicroProfileWSFlush(); + MicroProfileWSPrintEnd(); + } + } +} + +void MicroProfileWebSocketSendCounters() +{ + MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileWebSocketSendCounters", MP_GREEN4); + if(S.nWSViewMode == VIEW_COUNTERS) + { + MicroProfileWSPrintf("{\"k\":\"%d\",\"v\":[", MSG_COUNTERS); + for(uint32_t i = 0; i < S.nNumCounters; ++i) + { + bool IsDouble = (S.CounterInfo[i].nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) != 0; + if(IsDouble) + { + double dCounter = S.CountersDouble[i].load(); + MicroProfileWSPrintf("%c%f", i == 0 ? ' ' : ',', dCounter); + } + else + { + uint64_t nCounter = S.Counters[i].load(); + MicroProfileWSPrintf("%c%lld", i == 0 ? ' ' : ',', nCounter); + } + } + MicroProfileWSPrintf("]}"); + MicroProfileWSFlush(); + } +} + +#if MICROPROFILE_DYNAMIC_INSTRUMENT +void MicroProfileSymbolSendModuleState() +{ + if(S.WSSymbolModulesSent != S.SymbolNumModules || S.nSymbolsDirty.load()) // todo: tag when modulestate is updated. + { + S.nSymbolsDirty.exchange(0); + MicroProfileWSPrintf(",\"M\":["); + bool bFirst = true; + for(int i = 0; i < S.SymbolNumModules; ++i) + { + MicroProfileSymbolModule& M = S.SymbolModules[i]; + const char* pModuleName = (const char*)M.pBaseString; + uint64_t nAddrBegin = M.Regions[0].nBegin; + // intptr_t nProgress = M.nProgress; + intptr_t nProgressTarget = M.nProgressTarget; + nProgressTarget = MicroProfileMax(intptr_t(1), M.nProgressTarget); + // nProgress = MicroProfileMin(nProgressTarget, M.nProgress); + float fLoadPrc = M.nProgress / float(nProgressTarget); + uint64_t nNumSymbols = M.nSymbolsLoaded; +#define FMT "{\"n\":\"%s\",\"a\":\"%llx\",\"s\":\"%lld\", \"p\":%f, \"d\":%d}" + MicroProfileWSPrintf(bFirst ? FMT : ("," FMT), pModuleName, nAddrBegin, nNumSymbols, fLoadPrc, M.bDownloading ? 1 : 0); +#undef FMT + bFirst = false; + } + MicroProfileWSPrintf("]"); + S.WSSymbolModulesSent = S.SymbolNumModules; + } +} +#endif + +void MicroProfileWebSocketSendFrame(MpSocket Connection) +{ + if(S.nFrameCurrent != S.WebSocketFrameLast[0] || S.nFrozen) + { + MicroProfileWebSocketSendState(Connection); + MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileWebSocketSendFrame", MP_GREEN4); + MicroProfileWSPrintStart(Connection); + float fTickToMsCpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fTickToMsGpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondGpu()); + MicroProfileFrameState* pFrameCurrent = &S.Frames[S.nFrameCurrent]; + MicroProfileFrameState* pFrameNext = &S.Frames[S.nFrameNext]; + + uint64_t nFrameTicks = pFrameNext->nFrameStartCpu - pFrameCurrent->nFrameStartCpu; + uint64_t nFrame = pFrameCurrent->nFrameId; + double fTime = nFrameTicks * fTickToMsCpu; + MicroProfileWSPrintf("{\"k\":\"%d\",\"v\":{\"t\":%f,\"f\":%lld,\"a\":%d,\"fr\":%d,\"m\":%d", MSG_FRAME, fTime, nFrame, MicroProfileGetCurrentAggregateFrames(), S.nFrozen, S.nWSViewMode); +#if MICROPROFILE_DYNAMIC_INSTRUMENT + MicroProfileWSPrintf(",\"s\":{\"n\":%d,\"f\":%d,\"r\":%d,\"l\":%d,\"q\":%d}", + S.SymbolNumModules, + S.SymbolState.nModuleLoadsFinished.load(), + S.SymbolState.nModuleLoadsRequested.load(), + S.SymbolState.nSymbolsLoaded.load(), + S.pPendingQuery ? 1 : 0); + MicroProfileSymbolSendModuleState(); +#endif + + auto WriteTickArray = [fTickToMsCpu, fTickToMsGpu](MicroProfile::GroupTime* pFrameGroup) + { + MicroProfileWSPrintf("["); + int f = 0; + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + { + uint64_t nTicksExcl = pFrameGroup[i].nTicksExclusive; + if(nTicksExcl) + { + uint64_t nTicks = pFrameGroup[i].nTicks; + float fCount = (float)pFrameGroup[i].nCount; + float fToMs = S.GroupInfo[i].Type == MicroProfileTokenTypeCpu ? fTickToMsCpu : fTickToMsGpu; + + MicroProfileWSPrintf("%c[%f,%f,%f]", f ? ',' : ' ', nTicks * fToMs, nTicksExcl * fToMs, fCount); + f = 1; + } + } + MicroProfileWSPrintf("]"); + }; + auto WriteIndexArray = [](MicroProfile::GroupTime* pFrameGroup) + { + MicroProfileWSPrintf("["); + int f = 0; + for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + { + uint64_t nTicksExcl = pFrameGroup[i].nTicksExclusive; + if(nTicksExcl) + { + uint32_t id = MicroProfileWebSocketIdPack(TYPE_GROUP, i); + MicroProfileWSPrintf("%c%d", f ? ',' : ' ', id); + f = 1; + } + } + MicroProfileWSPrintf("]"); + }; + + MicroProfileWSPrintf(",\"g\":"); + WriteTickArray(S.FrameGroup); + MicroProfileWSPrintf(",\"gi\":"); + WriteIndexArray(S.FrameGroup); + if(S.nWSViewMode == VIEW_GRAPH_THREAD_GROUP) + { + MicroProfileWSPrintf(",\"gt\":["); + int f = 0; + for(uint32_t i = 0; i < MICROPROFILE_MAX_THREADS; ++i) + { + if(0 != (S.FrameGroupThreadValid[i / 32] & (1 << (i % 32)))) + { + if(!f) + MicroProfileWSPrintf("{"); + else + MicroProfileWSPrintf(",{"); + MicroProfileThreadLog* pLog = S.Pool[i]; + MicroProfileWSPrintf("\"i\":%d,\"n\":\"%s\",\"g\":", i, pLog->ThreadName); + WriteTickArray(&S.FrameGroupThread[i][0]); + MicroProfileWSPrintf(",\"gi\":"); + WriteIndexArray(&S.FrameGroupThread[i][0]); + MicroProfileWSPrintf("}"); + f = 1; + } + } + MicroProfileWSPrintf("]"); + } + + if(S.nFrameCurrent != S.WebSocketFrameLast[0]) + { + MicroProfileWSPrintf(",\"x\":{\"t\":{"); + int nTimer = S.WebSocketTimers; + // uprintf("T : "); + while(nTimer >= 0) + { + MicroProfileTimerInfo& TI = S.TimerInfo[nTimer]; + float fTickToMs = TI.Type == MicroProfileTokenTypeGpu ? fTickToMsGpu : fTickToMsCpu; + uint32_t id = MicroProfileWebSocketIdPack(TYPE_TIMER, nTimer); + fTime = fTickToMs * S.Frame[nTimer].nTicks; + float fCount = (float)S.Frame[nTimer].nCount; + float fTimeExcl = fTickToMs * S.FrameExclusive[nTimer]; + // uprintf("%4.2f, ", fTimeExcl); + if(!MicroProfileGroupActive(TI.nGroupIndex)) + { + fTime = fCount = fTimeExcl = 0.f; + } + nTimer = TI.nWSNext; + MicroProfileWSPrintf("\"%d\":[%f,%f,%f]%c", id, fTime, fTimeExcl, fCount, nTimer == -1 ? ' ' : ','); + } + MicroProfileWSPrintf("}, \"c\":{"); + int nCounter = S.WebSocketCounters; + while(nCounter >= 0) + { + MicroProfileCounterInfo& CI = S.CounterInfo[nCounter]; + bool IsDouble = (CI.nFlags & MICROPROFILE_COUNTER_FLAG_DOUBLE) != 0; + uint32_t id = MicroProfileWebSocketIdPack(TYPE_COUNTER, nCounter); + int nCounterNext = CI.nWSNext; + if(IsDouble) + { + double value = S.CountersDouble[nCounter].load(); + MicroProfileWSPrintf("\"%d\":%f%c", id, value, nCounterNext < 0 ? ' ' : ','); + } + else + { + uint64_t value = S.Counters[nCounter].load(); + MicroProfileWSPrintf("\"%d\":%lld%c", id, value, nCounterNext < 0 ? ' ' : ','); + } + nCounter = nCounterNext; + } + MicroProfileWSPrintf("}, \"g\":{"); + // uprintf("\n"); + MicroProfileWSPrintf("}}"); + } + MicroProfileWSPrintf("}}"); + MicroProfileWSFlush(); + MicroProfileWebSocketSendCounters(); + MicroProfileWSPrintEnd(); + S.WebSocketFrameLast[0] = S.nFrameCurrent; + } + else + { + MicroProfileWSPrintStart(Connection); + MicroProfileWSPrintf("{\"k\":\"%d\",\"v\":{\"fr\":%d,\"m\":%d", MSG_INACTIVE_FRAME, S.nFrozen, S.nWSViewMode); +#if MICROPROFILE_DYNAMIC_INSTRUMENT + MicroProfileWSPrintf(",\"s\":{\"n\":%d,\"f\":%d,\"r\":%d,\"l\":%d,\"q\":%d}", + S.SymbolNumModules, + S.SymbolState.nModuleLoadsFinished.load(), + S.SymbolState.nModuleLoadsRequested.load(), + S.SymbolState.nSymbolsLoaded.load(), + S.pPendingQuery ? 1 : 0); +#endif + MicroProfileWSPrintf("}}"); + MicroProfileWSFlush(); + MicroProfileWebSocketSendCounters(); + MicroProfileWSPrintEnd(); + } +#if MICROPROFILE_DYNAMIC_INSTRUMENT + MicroProfileSymbolQuerySendResult(Connection); + MicroProfileSymbolSendFunctionNames(Connection); + MicroProfileSymbolSendErrors(Connection); +#endif +} + +void MicroProfileWebSocketFrame() +{ + if(!S.nNumWebSockets) + { + return; + } + MICROPROFILE_SCOPEI("MicroProfile", "Websocket-update", MP_GREEN4); + fd_set Read, Write, Error; + FD_ZERO(&Read); + FD_ZERO(&Write); + FD_ZERO(&Error); + MpSocket LastSocket = 1; + for(uint32_t i = 0; i < S.nNumWebSockets; ++i) + { + LastSocket = MicroProfileMax(LastSocket, S.WebSockets[i] + 1); + FD_SET(S.WebSockets[i], &Read); + FD_SET(S.WebSockets[i], &Write); + FD_SET(S.WebSockets[i], &Error); + } + timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 0; + + if(-1 == select(LastSocket, &Read, &Write, &Error, &tv)) + { + MP_ASSERT(0); + } + for(uint32_t i = 0; i < S.nNumWebSockets;) + { + MpSocket s = S.WebSockets[i]; + bool bConnected = true; + if(FD_ISSET(s, &Error)) + { + MP_ASSERT(0); // todo, remove & fix. + } + if(FD_ISSET(s, &Read)) + { + bConnected = MicroProfileWebSocketReceive(s); + } + if(FD_ISSET(s, &Write)) + { + if(S.nJsonSettingsPending) + { + MicroProfileWSPrintStart(s); + MicroProfileWSPrintf( + "{\"k\":\"%d\",\"ro\":%d,\"name\":\"%s\",\"v\":%s}", MSG_LOADSETTINGS, S.bJsonSettingsReadOnly ? 1 : 0, S.pJsonSettingsName ? S.pJsonSettingsName : "", S.pJsonSettings); + MicroProfileWSFlush(); + MicroProfileWSPrintEnd(); + S.nJsonSettingsPending = 0; + } + if(S.nWebSocketDirty) + { + MicroProfileFlipEnabled(); + MicroProfileWebSocketSendEnabled(s); + S.nWebSocketDirty = 0; + } + MicroProfileWebSocketSendFrame(s); + } + if(S.nSocketFail) + { + bConnected = false; + } + S.nSocketFail = 0; + + if(!bConnected) + { + uprintf("removing socket %" PRId64 "\n", (uint64_t)s); + +#ifndef _WIN32 + shutdown(S.WebSockets[i], SHUT_WR); +#else + shutdown(S.WebSockets[i], 1); +#endif + char tmp[128]; + int r = 1; + while(r > 0) + { + r = recv(S.WebSockets[i], tmp, sizeof(tmp), 0); + } +#ifdef _WIN32 + closesocket(S.WebSockets[i]); +#else + close(S.WebSockets[i]); +#endif + + --S.nNumWebSockets; + S.WebSockets[i] = S.WebSockets[S.nNumWebSockets]; + uprintf("done removing\n"); + } + else + { + ++i; + } + } + if(S.nWasFrozen) + { + S.nWasFrozen--; + } +} + +void MicroProfileWSPrintStart(MpSocket C) +{ + MP_ASSERT(S.WSBuf.Socket == 0); + MP_ASSERT(S.WSBuf.nPut == 0); + S.WSBuf.Socket = C; +} + +void MicroProfileResizeWSBuf(uint32_t nMinSize = 0) +{ + uint32_t nNewSize = MicroProfileMax(S.WSBuf.nPut + 2 * (nMinSize + 2 + 20), MicroProfileMax(S.WSBuf.nBufferSize * 3 / 2, (uint32_t)MICROPROFILE_WEBSOCKET_BUFFER_SIZE)); + S.WSBuf.pBufferAllocation = (char*)MICROPROFILE_REALLOC(S.WSBuf.pBufferAllocation, nNewSize); + S.WSBuf.pBuffer = S.WSBuf.pBufferAllocation + 20; + S.WSBuf.nBufferSize = nNewSize - 20; +} + +char* MicroProfileWSPrintfCallback(const char* buf, void* user, int len) +{ + MP_ASSERT(S.WSBuf.nPut == buf - S.WSBuf.pBuffer); + S.WSBuf.nPut += len; + if(S.WSBuf.nPut + STB_SPRINTF_MIN + 2 >= S.WSBuf.nBufferSize) // + { + MicroProfileResizeWSBuf(S.WSBuf.nPut + STB_SPRINTF_MIN); + } + return S.WSBuf.pBuffer + S.WSBuf.nPut; +} + +void MicroProfileWSPrintf(const char* pFmt, ...) +{ + if(!S.WSBuf.nBufferSize) + { + MicroProfileResizeWSBuf(STB_SPRINTF_MIN * 2); + } + va_list args; + va_start(args, pFmt); + MP_ASSERT(S.WSBuf.nPut + STB_SPRINTF_MIN < S.WSBuf.nBufferSize); + stbsp_vsprintfcb(MicroProfileWSPrintfCallback, 0, S.WSBuf.pBuffer + S.WSBuf.nPut, pFmt, args); + va_end(args); +} + +void MicroProfileWSPrintEnd() +{ + MP_ASSERT(S.WSBuf.nPut == 0); + S.WSBuf.Socket = 0; +} + +void MicroProfileWSFlush() +{ + MP_ASSERT(S.WSBuf.Socket != 0); + MP_ASSERT(S.WSBuf.nPut != 0); + MicroProfileWebSocketSend(S.WSBuf.Socket, &S.WSBuf.pBuffer[0], S.WSBuf.nPut); + S.WSBuf.nPut = 0; +} +void MicroProfileWebSocketSendEnabledMessage(uint32_t id, int bEnabled) +{ + MicroProfileWSPrintf("{\"k\":\"%d\",\"v\":{\"id\":%d,\"e\":%d}}", MSG_ENABLED, id, bEnabled ? 1 : 0); + MicroProfileWSFlush(); +} +void MicroProfileWebSocketSendEnabled(MpSocket C) +{ + MICROPROFILE_SCOPEI("MicroProfile", "Websocket-SendEnabled", MP_GREEN4); + MicroProfileWSPrintStart(C); + for(uint32_t i = 0; i < S.nCategoryCount; ++i) + { + MicroProfileWebSocketSendEnabledMessage(MicroProfileWebSocketIdPack(TYPE_CATEGORY, i), MicroProfileCategoryEnabled(i)); + } + + for(uint32_t i = 0; i < S.nGroupCount; ++i) + { + MicroProfileWebSocketSendEnabledMessage(MicroProfileWebSocketIdPack(TYPE_GROUP, i), MicroProfileGroupEnabled(i)); + } + for(uint32_t i = 0; i < S.nTotalTimers; ++i) + { + MicroProfileWebSocketSendEnabledMessage(MicroProfileWebSocketIdPack(TYPE_TIMER, i), MicroProfileWebSocketTimerEnabled(i)); + } + for(uint32_t i = 0; i < S.nNumCounters; ++i) + { + MicroProfileWebSocketSendEnabledMessage(MicroProfileWebSocketIdPack(TYPE_COUNTER, i), MicroProfileWebSocketCounterEnabled(i)); + } + MicroProfileWebSocketSendEnabledMessage(MicroProfileWebSocketIdPack(TYPE_SETTING, SETTING_FORCE_ENABLE), MicroProfileGetEnableAllGroups()); + MicroProfileWebSocketSendEnabledMessage(MicroProfileWebSocketIdPack(TYPE_SETTING, SETTING_CONTEXT_SWITCH_TRACE), S.bContextSwitchRunning); + MicroProfileWebSocketSendEnabledMessage(MicroProfileWebSocketIdPack(TYPE_SETTING, SETTING_PLATFORM_MARKERS), MicroProfilePlatformMarkersGetEnabled()); + + MicroProfileWSPrintEnd(); +} +void MicroProfileWebSocketSendEntry(uint32_t id, uint32_t parent, const char* pName, int nEnabled, uint32_t nColor, uint32_t nType) +{ + MicroProfileWSPrintf("{\"k\":\"%d\",\"v\":{\"id\":%d,\"pid\":%d,", MSG_TIMER_TREE, id, parent); + MicroProfileWSPrintf("\"name\":\"%s\",", pName); + MicroProfileWSPrintf("\"e\":%d,", nEnabled); + MicroProfileWSPrintf("\"type\":%d,", nType); + if(nColor == 0x42) + { + MicroProfileWSPrintf("\"color\":\"\""); + } + else + { + MicroProfileWSPrintf("\"color\":\"#%02x%02x%02x\"", MICROPROFILE_UNPACK_RED(nColor) & 0xff, MICROPROFILE_UNPACK_GREEN(nColor) & 0xff, MICROPROFILE_UNPACK_BLUE(nColor) & 0xff); + } + + MicroProfileWSPrintf("}}"); + MicroProfileWSFlush(); +} + +void MicroProfileWebSocketSendCounterEntry(uint32_t id, uint32_t parent, const char* pName, int nEnabled, int64_t nLimit, int nFormat) +{ + MicroProfileWSPrintf("{\"k\":\"%d\",\"v\":{\"id\":%d,\"pid\":%d,", MSG_TIMER_TREE, id, parent); + MicroProfileWSPrintf("\"name\":\"%s\",", pName); + MicroProfileWSPrintf("\"e\":%d,", nEnabled); + MicroProfileWSPrintf("\"limit\":%lld,", nLimit); + MicroProfileWSPrintf("\"format\":%d", nFormat); + MicroProfileWSPrintf("}}"); + MicroProfileWSFlush(); +} + +void MicroProfileWebSocketSendState(MpSocket C) +{ + if(S.WSCategoriesSent != S.nCategoryCount || S.WSGroupsSent != S.nGroupCount || S.WSTimersSent != S.nTotalTimers || S.WSCountersSent != S.nNumCounters) + { + MicroProfileWSPrintStart(C); + uint32_t root = MicroProfileWebSocketIdPack(TYPE_SETTING, SETTING_FORCE_ENABLE); + MicroProfileWebSocketSendEntry(root, 0, "All", MicroProfileGetEnableAllGroups(), (uint32_t)-1, 0); + for(uint32_t i = S.WSCategoriesSent; i < S.nCategoryCount; ++i) + { + + MicroProfileCategory& CI = S.CategoryInfo[i]; + uint32_t id = MicroProfileWebSocketIdPack(TYPE_CATEGORY, i); + uint32_t parent = root; + MicroProfileWebSocketSendEntry(id, parent, CI.pName, MicroProfileCategoryEnabled(i), 0xffffffff, 0); + } + + for(uint32_t i = S.WSGroupsSent; i < S.nGroupCount; ++i) + { + MicroProfileGroupInfo& GI = S.GroupInfo[i]; + uint32_t id = MicroProfileWebSocketIdPack(TYPE_GROUP, i); + uint32_t parent = MicroProfileWebSocketIdPack(TYPE_CATEGORY, GI.nCategory); + MicroProfileWebSocketSendEntry(id, parent, GI.pName, MicroProfileGroupEnabled(i), GI.nColor, GI.Type); + } + + for(uint32_t i = S.WSTimersSent; i < S.nTotalTimers; ++i) + { + MicroProfileTimerInfo& TI = S.TimerInfo[i]; + uint32_t id = MicroProfileWebSocketIdPack(TYPE_TIMER, i); + uint32_t parent = MicroProfileWebSocketIdPack(TYPE_GROUP, TI.nGroupIndex); + MicroProfileWebSocketSendEntry(id, parent, TI.pName, MicroProfileWebSocketTimerEnabled(i), TI.nColor, TI.Type); + } + + for(uint32_t i = S.WSCountersSent; i < S.nNumCounters; ++i) + { + MicroProfileCounterInfo& CI = S.CounterInfo[i]; + uint32_t id = MicroProfileWebSocketIdPack(TYPE_COUNTER, i); + uint32_t parent = CI.nParent == -1 ? 0u : MicroProfileWebSocketIdPack(TYPE_COUNTER, CI.nParent); + MicroProfileWebSocketSendCounterEntry(id, parent, CI.pName, MicroProfileWebSocketCounterEnabled(i), CI.nLimit, CI.eFormat); + } +#if MICROPROFILE_CONTEXT_SWITCH_TRACE + MicroProfileWebSocketSendEntry(MicroProfileWebSocketIdPack(TYPE_SETTING, SETTING_CONTEXT_SWITCH_TRACE), 0, "Context Switch Trace", S.bContextSwitchRunning, (uint32_t)-1, 0); +#endif +#if MICROPROFILE_PLATFORM_MARKERS + MicroProfileWebSocketSendEntry(MicroProfileWebSocketIdPack(TYPE_SETTING, SETTING_PLATFORM_MARKERS), 0, "Platform Markers", S.bContextSwitchRunning, (uint32_t)-1); +#endif + MicroProfileWSPrintEnd(); + + S.WSCategoriesSent = S.nCategoryCount; + S.WSGroupsSent = S.nGroupCount; + S.WSTimersSent = S.nTotalTimers; + S.WSCountersSent = S.nNumCounters; + } +} + +bool MicroProfileWebServerUpdate() +{ + MICROPROFILE_SCOPEI("MicroProfile", "Webserver-update", MP_GREEN4); + MpSocket Connection = accept(S.ListenerSocket, 0, 0); + bool bServed = false; + MicroProfileWebSocketFrame(); + if(!MP_INVALID_SOCKET(Connection)) + { + std::lock_guard Lock(MicroProfileMutex()); + char Req[8192]; + int nReceived = recv(Connection, Req, sizeof(Req) - 1, 0); + if(nReceived > 0) + { + Req[nReceived] = '\0'; + uprintf("req received\n%s", Req); + +#define MICROPROFILE_HTML_PNG_HEADER "HTTP/1.0 200 OK\r\nContent-Type: image/png\r\n\r\n" +#define MICROPROFILE_HTML_JS_HEADER "HTTP/1.0 200 OK\r\nContent-Type: text/javascript\r\n\r\n" +#if MICROPROFILE_MINIZ + // Expires: Tue, 01 Jan 2199 16:00:00 GMT\r\n +#define MICROPROFILE_HTML_HEADER "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Encoding: deflate\r\n\r\n" +#else +#define MICROPROFILE_HTML_HEADER "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" +#endif + + char* pHttp = strstr(Req, "HTTP/"); + + char* pGet = strstr(Req, "GET /"); + char* pHost = strstr(Req, "Host: "); + char* pWebSocketKey = strstr(Req, "Sec-WebSocket-Key: "); + auto Terminate = [](char* pString) + { + char* pEnd = pString; + while(*pEnd != '\0') + { + if(*pEnd == '\r' || *pEnd == '\n' || *pEnd == ' ') + { + *pEnd = '\0'; + return; + } + pEnd++; + } + }; + + if(pWebSocketKey) + { + if(S.nNumWebSockets) // only allow 1 + { + return false; + } + pWebSocketKey += sizeof("Sec-WebSocket-Key: ") - 1; + Terminate(pWebSocketKey); + MicroProfileWebSocketHandshake(Connection, pWebSocketKey); + return false; + } + + if(pHost) + { + pHost += sizeof("Host: ") - 1; + Terminate(pHost); + } + + if(pHttp && pGet) + { + *pHttp = '\0'; + pGet += sizeof("GET /") - 1; + Terminate(pGet); + MicroProfileParseGetResult R; + auto P = MicroProfileParseGet(pGet, &R); + switch(P) + { + case EMICROPROFILE_GET_COMMAND_SERVICE_WORKER: + { + MicroProfileSetNonBlocking(Connection, 1); + uint64_t nTickStart = MP_TICK(); + send(Connection, MICROPROFILE_HTML_JS_HEADER, sizeof(MICROPROFILE_HTML_JS_HEADER) - 1, 0); + const char* JsCode = "self.addEventListener(\"fetch\", () => {}); \r\n\r\n"; + send(Connection, JsCode, (int)strlen(JsCode), 0); + break; + } + case EMICROPROFILE_GET_COMMAND_FAVICON: + { + MicroProfileSetNonBlocking(Connection, 1); + uint64_t nTickStart = MP_TICK(); + send(Connection, MICROPROFILE_HTML_PNG_HEADER, sizeof(MICROPROFILE_HTML_PNG_HEADER) - 1, 0); + extern const uint32_t uprof_512[]; + extern const uint32_t uprof_512_len; + const char* pFile = (const char*)&uprof_512[0]; + uint32_t nFileSize = uprof_512_len; + send(Connection, pFile, nFileSize, 0); + } + break; + case EMICROPROFILE_GET_COMMAND_LIVE: + { + MicroProfileSetNonBlocking(Connection, 0); + uint64_t nTickStart = MP_TICK(); + send(Connection, MICROPROFILE_HTML_HEADER, sizeof(MICROPROFILE_HTML_HEADER) - 1, 0); + uint64_t nDataStart = S.nWebServerDataSent; + S.WebServerPut = 0; +#if 0 == MICROPROFILE_MINIZ + MicroProfileDumpHtmlLive(MicroProfileWriteSocket, &Connection); + uint64_t nDataEnd = S.nWebServerDataSent; + uint64_t nTickEnd = MP_TICK(); + uint64_t nDiff = (nTickEnd - nTickStart); + float fMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * nDiff; + int nKb = ((nDataEnd-nDataStart)>>10) + 1; + int nCompressedKb = nKb; + MicroProfilePrintf(MicroProfileWriteSocket, &Connection, "\n\n\n",nKb, fMs); + MicroProfileFlushSocket(Connection); +#else + MicroProfileCompressedSocketState CompressState; + MicroProfileCompressedSocketStart(&CompressState, Connection); + MicroProfileDumpHtmlLive(MicroProfileCompressedWriteSocket, &CompressState); + S.nWebServerDataSent += CompressState.nSize; + uint64_t nDataEnd = S.nWebServerDataSent; + uint64_t nTickEnd = MP_TICK(); + uint64_t nDiff = (nTickEnd - nTickStart); + float fMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * nDiff; + int nKb = ((nDataEnd - nDataStart) >> 10) + 1; + int nCompressedKb = ((CompressState.nCompressedSize) >> 10) + 1; + MicroProfilePrintf(MicroProfileCompressedWriteSocket, &CompressState, "\n\n\n", nKb, nCompressedKb, fMs); + MicroProfileCompressedSocketFinish(&CompressState); + MicroProfileFlushSocket(Connection); +#endif + + uprintf("\n\n\n", nKb, nCompressedKb, fMs); + (void)nCompressedKb; + } + break; + case EMICROPROFILE_GET_COMMAND_DUMP_RANGE: + case EMICROPROFILE_GET_COMMAND_DUMP: + { + { + MicroProfileSetNonBlocking(Connection, 0); + uint64_t nTickStart = MP_TICK(); + send(Connection, MICROPROFILE_HTML_HEADER, sizeof(MICROPROFILE_HTML_HEADER) - 1, 0); + uint64_t nDataStart = S.nWebServerDataSent; + S.WebServerPut = 0; +#if 0 == MICROPROFILE_MINIZ + MicroProfileDumpHtml(MicroProfileWriteSocket, &Connection, R.nFrames, pHost, R.nFrameStart); + uint64_t nDataEnd = S.nWebServerDataSent; + uint64_t nTickEnd = MP_TICK(); + uint64_t nDiff = (nTickEnd - nTickStart); + float fMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * nDiff; + int nKb = ((nDataEnd-nDataStart)>>10) + 1; + int nCompressedKb = nKb; + MicroProfilePrintf(MicroProfileWriteSocket, &Connection, "\n\n\n",nKb, fMs); + MicroProfileFlushSocket(Connection); +#else + MicroProfileCompressedSocketState CompressState; + MicroProfileCompressedSocketStart(&CompressState, Connection); + + MicroProfileDumpHtml(MicroProfileCompressedWriteSocket, &CompressState, R.nFrames, pHost, R.nFrameStart); + + S.nWebServerDataSent += CompressState.nSize; + uint64_t nDataEnd = S.nWebServerDataSent; + uint64_t nTickEnd = MP_TICK(); + uint64_t nDiff = (nTickEnd - nTickStart); + float fMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()) * nDiff; + int nKb = ((nDataEnd - nDataStart) >> 10) + 1; + int nCompressedKb = ((CompressState.nCompressedSize) >> 10) + 1; + MicroProfilePrintf(MicroProfileCompressedWriteSocket, &CompressState, "\n\n\n", nKb, nCompressedKb, fMs); + MicroProfileCompressedSocketFinish(&CompressState); + MicroProfileFlushSocket(Connection); +#endif + + uprintf("\n\n\n", nKb, nCompressedKb, fMs); + (void)nCompressedKb; + } + } + break; + case EMICROPROFILE_GET_COMMAND_UNKNOWN: + { + uprintf("unknown get command %s\n", pGet); + } + break; + } + } + } +#ifdef _WIN32 + closesocket(Connection); +#else + close(Connection); +#endif + } + return bServed; +} +#endif + +#if MICROPROFILE_CONTEXT_SWITCH_TRACE +// functions that need to be implemented per platform. +void* MicroProfileTraceThread(void* unused); +int MicroProfileIsLocalThread(uint32_t nThreadId); + +void MicroProfileStartContextSwitchTrace() +{ + if(!S.bContextSwitchRunning && !S.nMicroProfileShutdown) + { + S.bContextSwitchRunning = true; + S.bContextSwitchStop = false; + MicroProfileThreadStart(&S.ContextSwitchThread, MicroProfileTraceThread); + } +} + +void MicroProfileJoinContextSwitchTrace() +{ + if(S.bContextSwitchStop) + { + MicroProfileThreadJoin(&S.ContextSwitchThread); + } +} + +void MicroProfileStopContextSwitchTrace() +{ + if(S.bContextSwitchRunning) + { + S.bContextSwitchStop = true; + } +} + +#ifdef _WIN32 +#define INITGUID +#include +#include +#include + +static GUID g_MicroProfileThreadClassGuid = { 0x3d6fa8d1, 0xfe05, 0x11d0, 0x9d, 0xda, 0x00, 0xc0, 0x4f, 0xd7, 0xba, 0x7c }; + +struct MicroProfileSCSwitch +{ + uint32_t NewThreadId; + uint32_t OldThreadId; + int8_t NewThreadPriority; + int8_t OldThreadPriority; + uint8_t PreviousCState; + int8_t SpareByte; + int8_t OldThreadWaitReason; + int8_t OldThreadWaitMode; + int8_t OldThreadState; + int8_t OldThreadWaitIdealProcessor; + uint32_t NewThreadWaitTime; + uint32_t Reserved; +}; + +VOID WINAPI MicroProfileContextSwitchCallback(PEVENT_TRACE pEvent) +{ + if(pEvent->Header.Guid == g_MicroProfileThreadClassGuid) + { + if(pEvent->Header.Class.Type == 36) + { + MicroProfileSCSwitch* pCSwitch = (MicroProfileSCSwitch*)pEvent->MofData; + if((pCSwitch->NewThreadId != 0) || (pCSwitch->OldThreadId != 0)) + { + MicroProfileContextSwitch Switch; + Switch.nThreadOut = pCSwitch->OldThreadId; + Switch.nThreadIn = pCSwitch->NewThreadId; + Switch.nCpu = pEvent->BufferContext.ProcessorNumber; + Switch.nTicks = pEvent->Header.TimeStamp.QuadPart; + MicroProfileContextSwitchPut(&Switch); + } + } + } +} + +ULONG WINAPI MicroProfileBufferCallback(PEVENT_TRACE_LOGFILEA Buffer) +{ + return (S.bContextSwitchStop || !S.bContextSwitchRunning) ? FALSE : TRUE; +} + +struct MicroProfileKernelTraceProperties : public EVENT_TRACE_PROPERTIES +{ + char dummy[sizeof(KERNEL_LOGGER_NAME)]; +}; + +void MicroProfileContextSwitchShutdownTrace() +{ + TRACEHANDLE SessionHandle = 0; + MicroProfileKernelTraceProperties sessionProperties; + + ZeroMemory(&sessionProperties, sizeof(sessionProperties)); + sessionProperties.Wnode.BufferSize = sizeof(sessionProperties); + sessionProperties.Wnode.Flags = WNODE_FLAG_TRACED_GUID; + sessionProperties.Wnode.ClientContext = 1; // QPC clock resolution + sessionProperties.Wnode.Guid = SystemTraceControlGuid; + sessionProperties.BufferSize = 1; + sessionProperties.NumberOfBuffers = 128; + sessionProperties.EnableFlags = EVENT_TRACE_FLAG_CSWITCH; + sessionProperties.LogFileMode = EVENT_TRACE_REAL_TIME_MODE; + sessionProperties.MaximumFileSize = 0; + sessionProperties.LoggerNameOffset = sizeof(EVENT_TRACE_PROPERTIES); + sessionProperties.LogFileNameOffset = 0; + + EVENT_TRACE_LOGFILEA log; + ZeroMemory(&log, sizeof(log)); + log.LoggerName = (LPSTR)KERNEL_LOGGER_NAMEA; + log.ProcessTraceMode = 0; + TRACEHANDLE hLog = OpenTraceA(&log); + if(hLog) + { + ControlTrace(SessionHandle, KERNEL_LOGGER_NAME, &sessionProperties, EVENT_TRACE_CONTROL_STOP); + } + CloseTrace(hLog); +} + +typedef VOID(WINAPI* EventCallback)(PEVENT_TRACE); +typedef ULONG(WINAPI* BufferCallback)(PEVENT_TRACE_LOGFILEA); +bool MicroProfileStartWin32Trace(EventCallback EvtCb, BufferCallback BufferCB) +{ + MicroProfileContextSwitchShutdownTrace(); + ULONG status = ERROR_SUCCESS; + TRACEHANDLE SessionHandle = 0; + MicroProfileKernelTraceProperties sessionProperties; + + ZeroMemory(&sessionProperties, sizeof(sessionProperties)); + sessionProperties.Wnode.BufferSize = sizeof(sessionProperties); + sessionProperties.Wnode.Flags = WNODE_FLAG_TRACED_GUID; + sessionProperties.Wnode.ClientContext = 1; // QPC clock resolution + sessionProperties.Wnode.Guid = SystemTraceControlGuid; + sessionProperties.BufferSize = 1; + sessionProperties.NumberOfBuffers = 128; + sessionProperties.EnableFlags = EVENT_TRACE_FLAG_CSWITCH | EVENT_TRACE_FLAG_PROCESS; + sessionProperties.LogFileMode = EVENT_TRACE_REAL_TIME_MODE; + sessionProperties.MaximumFileSize = 0; + sessionProperties.LoggerNameOffset = sizeof(EVENT_TRACE_PROPERTIES); + sessionProperties.LogFileNameOffset = 0; + + StopTrace(NULL, KERNEL_LOGGER_NAME, &sessionProperties); + status = StartTrace((PTRACEHANDLE)&SessionHandle, KERNEL_LOGGER_NAME, &sessionProperties); + + if(ERROR_SUCCESS != status) + { + return false; + } + + EVENT_TRACE_LOGFILEA log; + ZeroMemory(&log, sizeof(log)); + + log.LoggerName = (LPSTR)KERNEL_LOGGER_NAME; + log.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME | PROCESS_TRACE_MODE_RAW_TIMESTAMP; + log.EventCallback = EvtCb; + log.BufferCallback = BufferCB; + + TRACEHANDLE hLog = OpenTraceA(&log); + ProcessTrace(&hLog, 1, 0, 0); + CloseTrace(hLog); + MicroProfileContextSwitchShutdownTrace(); + return true; +} + +#include +#include +#include +#define ThreadQuerySetWin32StartAddress 9 +typedef LONG NTSTATUS; +typedef NTSTATUS(WINAPI* pNtQIT)(HANDLE, LONG, PVOID, ULONG, PULONG); +#define STATUS_SUCCESS ((NTSTATUS)0x000 00000L) +#define ThreadQuerySetWin32StartAddress 9 +#undef Process32First +#undef Process32Next +#undef PROCESSENTRY32 +#undef Module32First +#undef Module32Next +#undef MODULEENTRY32 + +struct MicroProfileWin32ContextSwitchShared +{ + std::atomic nPut; + std::atomic nGet; + std::atomic nQuit; + std::atomic nTickTrace; + std::atomic nTickProgram; + enum + { + BUFFER_SIZE = (2 << 20) / sizeof(MicroProfileContextSwitch), + }; + MicroProfileContextSwitch Buffer[BUFFER_SIZE]; +}; + +struct MicroProfileWin32ThreadInfo +{ + struct Process + { + uint32_t pid; + uint32_t nNumModules; + uint32_t nModuleStart; + const char* pProcessModule; + }; + struct Module + { + int64_t nBase; + int64_t nEnd; + const char* pName; + }; + enum + { + MAX_PROCESSES = 5 * 1024, + MAX_THREADS = 20 * 1024, + MAX_MODULES = 20 * 1024, + MAX_STRINGS = 16 * 1024, + MAX_CHARS = 128 * 1024, + }; + uint32_t nNumProcesses; + uint32_t nNumThreads; + uint32_t nStringOffset; + uint32_t nNumStrings; + uint32_t nNumModules; + Process P[MAX_PROCESSES]; + Module M[MAX_MODULES]; + MicroProfileThreadInfo T[MAX_THREADS]; + const char* pStrings[MAX_STRINGS]; + char StringData[MAX_CHARS]; +}; + +static MicroProfileWin32ThreadInfo g_ThreadInfo; + +const char* MicroProfileWin32ThreadInfoAddString(const char* pString) +{ + size_t nLen = strlen(pString); + uint32_t nHash = *(uint32_t*)pString; + nHash ^= (nHash >> 16); + enum + { + MAX_SEARCH = 256, + }; + for(uint32_t i = 0; i < MAX_SEARCH; ++i) + { + uint32_t idx = (i + nHash) % MicroProfileWin32ThreadInfo::MAX_STRINGS; + if(0 == g_ThreadInfo.pStrings[idx]) + { + g_ThreadInfo.pStrings[idx] = &g_ThreadInfo.StringData[g_ThreadInfo.nStringOffset]; + memcpy(&g_ThreadInfo.StringData[g_ThreadInfo.nStringOffset], pString, nLen + 1); + g_ThreadInfo.nStringOffset += (uint32_t)(nLen + 1); + return g_ThreadInfo.pStrings[idx]; + } + if(0 == strcmp(g_ThreadInfo.pStrings[idx], pString)) + { + return g_ThreadInfo.pStrings[idx]; + } + } + return "internal hash table fail: should never happen"; +} +void MicroProfileWin32ExtractModules(MicroProfileWin32ThreadInfo::Process& P) +{ + HANDLE hModuleSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, P.pid); + MODULEENTRY32 me; + if(Module32First(hModuleSnapshot, &me)) + { + do + { + if(g_ThreadInfo.nNumModules < MicroProfileWin32ThreadInfo::MAX_MODULES) + { + auto& M = g_ThreadInfo.M[g_ThreadInfo.nNumModules++]; + P.nNumModules++; + intptr_t nBase = (intptr_t)me.modBaseAddr; + intptr_t nEnd = nBase + me.modBaseSize; + M.nBase = nBase; + M.nEnd = nEnd; + M.pName = MicroProfileWin32ThreadInfoAddString(&me.szModule[0]); + } + } while(Module32Next(hModuleSnapshot, &me)); + } + if(hModuleSnapshot) + CloseHandle(hModuleSnapshot); +} +void MicroProfileWin32InitThreadInfo2() +{ + memset(&g_ThreadInfo, 0, sizeof(g_ThreadInfo)); +#if MICROPROFILE_DEBUG + float fToMsCpu = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); +#endif + + HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); + PROCESSENTRY32 pe32; + THREADENTRY32 te32; + te32.dwSize = sizeof(THREADENTRY32); + pe32.dwSize = sizeof(PROCESSENTRY32); + { +#if MICROPROFILE_DEBUG + int64_t nTickStart = MP_TICK(); +#endif + if(Process32First(hSnap, &pe32)) + { + do + { + + MicroProfileWin32ThreadInfo::Process P; + P.pid = pe32.th32ProcessID; + P.pProcessModule = MicroProfileWin32ThreadInfoAddString(pe32.szExeFile); + g_ThreadInfo.P[g_ThreadInfo.nNumProcesses++] = P; + } while(Process32Next(hSnap, &pe32) && g_ThreadInfo.nNumProcesses < MicroProfileWin32ThreadInfo::MAX_PROCESSES); + } +#if MICROPROFILE_DEBUG + int64_t nTicksEnd = MP_TICK(); + float fMs = fToMsCpu * (nTicksEnd - nTickStart); + uprintf("Process iteration %6.2fms processes %d\n", fMs, g_ThreadInfo.nNumProcesses); +#endif + } + { +#if MICROPROFILE_DEBUG + int64_t nTickStart = MP_TICK(); +#endif + for(uint32_t i = 0; i < g_ThreadInfo.nNumProcesses; ++i) + { + g_ThreadInfo.P[i].nModuleStart = g_ThreadInfo.nNumModules; + g_ThreadInfo.P[i].nNumModules = 0; + MicroProfileWin32ExtractModules(g_ThreadInfo.P[i]); + } +#if MICROPROFILE_DEBUG + int64_t nTicksEnd = MP_TICK(); + float fMs = fToMsCpu * (nTicksEnd - nTickStart); + uprintf("Module iteration %6.2fms NumModules %d\n", fMs, g_ThreadInfo.nNumModules); +#endif + } + + pNtQIT NtQueryInformationThread = (pNtQIT)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationThread"); + intptr_t dwStartAddress; + ULONG olen; + uint32_t nThreadsTested = 0; + uint32_t nThreadsSucceeded = 0; + + if(Thread32First(hSnap, &te32)) + { +#if MICROPROFILE_DEBUG + int64_t nTickStart = MP_TICK(); +#endif + do + { + nThreadsTested++; + const char* pModule = "?"; + HANDLE hThread = OpenThread(THREAD_QUERY_INFORMATION, FALSE, te32.th32ThreadID); + if(hThread) + { + + NTSTATUS ntStatus = NtQueryInformationThread(hThread, (THREADINFOCLASS)ThreadQuerySetWin32StartAddress, &dwStartAddress, sizeof(dwStartAddress), &olen); + if(0 == ntStatus) + { + uint32_t nProcessIndex = (uint32_t)-1; + for(uint32_t i = 0; i < g_ThreadInfo.nNumProcesses; ++i) + { + if(g_ThreadInfo.P[i].pid == te32.th32OwnerProcessID) + { + nProcessIndex = i; + break; + } + } + if(nProcessIndex != (uint32_t)-1) + { + uint32_t nModuleStart = g_ThreadInfo.P[nProcessIndex].nModuleStart; + uint32_t nNumModules = g_ThreadInfo.P[nProcessIndex].nNumModules; + for(uint32_t i = 0; i < nNumModules; ++i) + { + auto& M = g_ThreadInfo.M[nModuleStart + i]; + if(M.nBase <= dwStartAddress && M.nEnd >= dwStartAddress) + { + pModule = M.pName; + } + } + } + } + } + if(hThread) + CloseHandle(hThread); + { + MicroProfileThreadInfo T; + T.pid = te32.th32OwnerProcessID; + T.tid = te32.th32ThreadID; + const char* pProcess = "unknown"; + for(uint32_t i = 0; i < g_ThreadInfo.nNumProcesses; ++i) + { + if(g_ThreadInfo.P[i].pid == T.pid) + { + pProcess = g_ThreadInfo.P[i].pProcessModule; + break; + } + } + T.pProcessModule = pProcess; + T.pThreadModule = MicroProfileWin32ThreadInfoAddString(pModule); + T.nIsLocal = GetCurrentProcessId() == T.pid ? 1 : 0; + nThreadsSucceeded++; + g_ThreadInfo.T[g_ThreadInfo.nNumThreads++] = T; + } + + } while(Thread32Next(hSnap, &te32) && g_ThreadInfo.nNumThreads < MicroProfileWin32ThreadInfo::MAX_THREADS); + +#if MICROPROFILE_DEBUG + int64_t nTickEnd = MP_TICK(); + float fMs = fToMsCpu * (nTickEnd - nTickStart); + uprintf("Thread iteration %6.2fms Threads %d\n", fMs, g_ThreadInfo.nNumThreads); +#endif + } +} + +void MicroProfileWin32UpdateThreadInfo() +{ + static int nWasRunning = 1; + static int nOnce = 0; + int nRunning = MicroProfileAnyGroupActive() ? 1 : 0; + + if((0 == nRunning && 1 == nWasRunning) || nOnce == 0) + { + nOnce = 1; + MicroProfileWin32InitThreadInfo2(); + } + nWasRunning = nRunning; +} + +const char* MicroProfileThreadNameFromId(MicroProfileThreadIdType nThreadId) +{ + MicroProfileWin32UpdateThreadInfo(); + static char result[1024]; + for(uint32_t i = 0; i < g_ThreadInfo.nNumThreads; ++i) + { + if(g_ThreadInfo.T[i].tid == nThreadId) + { + sprintf_s(result, "p:%s t:%s", g_ThreadInfo.T[i].pProcessModule, g_ThreadInfo.T[i].pThreadModule); + return result; + } + } + sprintf_s(result, "?"); + return result; +} + +#define MICROPROFILE_FILEMAPPING "microprofile-shared" +#ifdef MICROPROFILE_WIN32_COLLECTOR +#define MICROPROFILE_WIN32_CSWITCH_TIMEOUT 15 // seconds to wait before collector exits +static MicroProfileWin32ContextSwitchShared* g_pShared = 0; +VOID WINAPI MicroProfileContextSwitchCallbackCollector(PEVENT_TRACE pEvent) +{ + static int64_t nPackets = 0; + static int64_t nSkips = 0; + if(pEvent->Header.Guid == g_MicroProfileThreadClassGuid) + { + if(pEvent->Header.Class.Type == 36) + { + MicroProfileSCSwitch* pCSwitch = (MicroProfileSCSwitch*)pEvent->MofData; + if((pCSwitch->NewThreadId != 0) || (pCSwitch->OldThreadId != 0)) + { + MicroProfileContextSwitch Switch; + Switch.nThreadOut = pCSwitch->OldThreadId; + Switch.nThreadIn = pCSwitch->NewThreadId; + Switch.nCpu = pEvent->BufferContext.ProcessorNumber; + Switch.nTicks = pEvent->Header.TimeStamp.QuadPart; + int64_t nPut = g_pShared->nPut.load(std::memory_order_relaxed); + int64_t nGet = g_pShared->nGet.load(std::memory_order_relaxed); + nPackets++; + if(nPut - nGet < MicroProfileWin32ContextSwitchShared::BUFFER_SIZE) + { + g_pShared->Buffer[nPut % MicroProfileWin32ContextSwitchShared::BUFFER_SIZE] = Switch; + g_pShared->nPut.store(nPut + 1, std::memory_order_release); + nSkips = 0; + } + else + { + nSkips++; + } + } + } + } + if(0 == (nPackets % (4 << 10))) + { + int64_t nTickTrace = MP_TICK(); + g_pShared->nTickTrace.store(nTickTrace); + int64_t nTickProgram = g_pShared->nTickProgram.load(); + float fTickToMs = MicroProfileTickToMsMultiplier(MicroProfileTicksPerSecondCpu()); + float fTime = fabs(fTickToMs * (nTickTrace - nTickProgram)); + printf("\rRead %" PRId64 " CSwitch Packets, Skips %" PRId64 " Time difference %6.3fms ", nPackets, nSkips, fTime); + fflush(stdout); + if(fTime > MICROPROFILE_WIN32_CSWITCH_TIMEOUT * 1000) + { + g_pShared->nQuit.store(1); + } + } +} + +ULONG WINAPI MicroProfileBufferCallbackCollector(PEVENT_TRACE_LOGFILEA Buffer) +{ + return (g_pShared->nQuit.load()) ? FALSE : TRUE; +} + +int main(int argc, char* argv[]) +{ + if(argc != 2) + { + return 1; + } + printf("using file '%s'\n", argv[1]); + HANDLE hMemory = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, argv[1]); + if(hMemory == NULL) + { + return 1; + } + g_pShared = (MicroProfileWin32ContextSwitchShared*)MapViewOfFile(hMemory, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(MicroProfileWin32ContextSwitchShared)); + + if(g_pShared != NULL) + { + MicroProfileStartWin32Trace(MicroProfileContextSwitchCallbackCollector, MicroProfileBufferCallbackCollector); + UnmapViewOfFile(g_pShared); + } + + CloseHandle(hMemory); + return 0; +} +#endif +#include +void* MicroProfileTraceThread(void* unused) +{ + MicroProfileOnThreadCreate("ContextSwitchThread"); + MicroProfileContextSwitchShutdownTrace(); + if(!MicroProfileStartWin32Trace(MicroProfileContextSwitchCallback, MicroProfileBufferCallback)) + { + MicroProfileContextSwitchShutdownTrace(); + // not running as admin. try and start other process. + MicroProfileWin32ContextSwitchShared* pShared = 0; + char Filename[512]; + time_t t = time(NULL); + _snprintf_s(Filename, sizeof(Filename), "%s_%d", MICROPROFILE_FILEMAPPING, (int)t); + + HANDLE hMemory = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(MicroProfileWin32ContextSwitchShared), Filename); + if(hMemory != NULL) + { + pShared = (MicroProfileWin32ContextSwitchShared*)MapViewOfFile(hMemory, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(MicroProfileWin32ContextSwitchShared)); + if(pShared != NULL) + { +#ifdef _M_IX86 +#define CSWITCH_EXE "microprofile-win32-cswitch_x86.exe" +#else +#define CSWITCH_EXE "microprofile-win32-cswitch_x64.exe" +#endif + pShared->nTickProgram.store(MP_TICK()); + pShared->nTickTrace.store(MP_TICK()); + HINSTANCE Instance = ShellExecuteA(NULL, "runas", CSWITCH_EXE, Filename, "", SW_SHOWMINNOACTIVE); + int64_t nInstance = (int64_t)Instance; + if(nInstance >= 32) + { + int64_t nPut, nGet; + while(!S.bContextSwitchStop) + { + nPut = pShared->nPut.load(std::memory_order_acquire); + nGet = pShared->nGet.load(std::memory_order_relaxed); + if(nPut == nGet) + { + Sleep(20); + } + else + { + for(int64_t i = nGet; i != nPut; i++) + { + MicroProfileContextSwitchPut(&pShared->Buffer[i % MicroProfileWin32ContextSwitchShared::BUFFER_SIZE]); + } + pShared->nGet.store(nPut, std::memory_order_release); + pShared->nTickProgram.store(MP_TICK()); + } + } + pShared->nQuit.store(1); + } + } + UnmapViewOfFile(pShared); + } + CloseHandle(hMemory); + } + S.bContextSwitchRunning = false; + MicroProfileOnThreadExit(); + return 0; +} + +MicroProfileThreadInfo MicroProfileGetThreadInfo(MicroProfileThreadIdType nThreadId) +{ + MicroProfileWin32UpdateThreadInfo(); + + for(uint32_t i = 0; i < g_ThreadInfo.nNumThreads; ++i) + { + if(g_ThreadInfo.T[i].tid == nThreadId) + { + return g_ThreadInfo.T[i]; + } + } + MicroProfileThreadInfo TI((uint32_t)nThreadId, 0, 0); + return TI; +} +uint32_t MicroProfileGetThreadInfoArray(MicroProfileThreadInfo** pThreadArray) +{ + MicroProfileWin32InitThreadInfo2(); + *pThreadArray = &g_ThreadInfo.T[0]; + return g_ThreadInfo.nNumThreads; +} + +#elif defined(__APPLE__) +#include +void* MicroProfileTraceThread(void* unused) +{ + FILE* pFile = fopen("mypipe", "r"); + if(!pFile) + { + uprintf("CONTEXT SWITCH FAILED TO OPEN FILE: make sure to run dtrace script\n"); + S.bContextSwitchRunning = false; + return 0; + } + uprintf("STARTING TRACE THREAD\n"); + char* pLine = 0; + size_t cap = 0; + size_t len = 0; + struct timeval tv; + + gettimeofday(&tv, NULL); + + uint64_t nsSinceEpoch = ((uint64_t)(tv.tv_sec) * 1000000 + (uint64_t)(tv.tv_usec)) * 1000; + uint64_t nTickEpoch = MP_TICK(); + uint32_t nLastThread[MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS] = { 0 }; + mach_timebase_info_data_t sTimebaseInfo; + mach_timebase_info(&sTimebaseInfo); + S.bContextSwitchRunning = true; + + uint64_t nProcessed = 0; + uint64_t nProcessedLast = 0; + while((len = getline(&pLine, &cap, pFile)) > 0 && !S.bContextSwitchStop) + { + nProcessed += len; + if(nProcessed - nProcessedLast > 10 << 10) + { + nProcessedLast = nProcessed; + uprintf("processed %llukb %llukb\n", (nProcessed - nProcessedLast) >> 10, nProcessed >> 10); + } + + char* pX = strchr(pLine, 'X'); + if(pX) + { + int cpu = atoi(pX + 1); + char* pX2 = strchr(pX + 1, 'X'); + char* pX3 = strchr(pX2 + 1, 'X'); + int thread = atoi(pX2 + 1); + char* lala; + int64_t timestamp = strtoll(pX3 + 1, &lala, 10); + MicroProfileContextSwitch Switch; + + // convert to ticks. + uint64_t nDeltaNsSinceEpoch = timestamp - nsSinceEpoch; + uint64_t nDeltaTickSinceEpoch = sTimebaseInfo.numer * nDeltaNsSinceEpoch / sTimebaseInfo.denom; + uint64_t nTicks = nDeltaTickSinceEpoch + nTickEpoch; + if(cpu < MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS) + { + Switch.nThreadOut = nLastThread[cpu]; + Switch.nThreadIn = thread; + nLastThread[cpu] = thread; + Switch.nCpu = cpu; + Switch.nTicks = nTicks; + MicroProfileContextSwitchPut(&Switch); + } + } + } + uprintf("EXITING TRACE THREAD\n"); + S.bContextSwitchRunning = false; + return 0; +} + +MicroProfileThreadInfo MicroProfileGetThreadInfo(MicroProfileThreadIdType nThreadId) +{ + MicroProfileThreadInfo TI((uint32_t)nThreadId, 0, 0); + return TI; +} +uint32_t MicroProfileGetThreadInfoArray(MicroProfileThreadInfo** pThreadArray) +{ + *pThreadArray = 0; + return 0; +} + +#endif +#else + +MicroProfileThreadInfo MicroProfileGetThreadInfo(MicroProfileThreadIdType nThreadId) +{ + MicroProfileThreadInfo TI((uint32_t)nThreadId, 0, 0); + return TI; +} +uint32_t MicroProfileGetThreadInfoArray(MicroProfileThreadInfo** pThreadArray) +{ + *pThreadArray = 0; + return 0; +} +void MicroProfileStopContextSwitchTrace() +{ +} +void MicroProfileJoinContextSwitchTrace() +{ +} +void MicroProfileStartContextSwitchTrace() +{ +} + +#endif + +#if MICROPROFILE_GPU_TIMERS +void MicroProfileGpuShutdownPlatform() +{ + if(S.pGPU) + { + MicroProfileGpuShutdown(); + MP_FREE(S.pGPU); + S.pGPU = nullptr; + MicroProfileGpuInsertTimeStamp_Callback = nullptr; + MicroProfileGpuGetTimeStamp_Callback = nullptr; + MicroProfileTicksPerSecondGpu_Callback = nullptr; + MicroProfileGetGpuTickReference_Callback = nullptr; + MicroProfileGpuFlip_Callback = nullptr; + MicroProfileGpuShutdown_Callback = nullptr; + } +} + +void MicroProfileGpuInitPlatform(MicroProfileGpuTimerStateType eType, + MicroProfileGpuTimerState* pGPU, + MicroProfileGpuInsertTimeStamp_CB InsertTimeStamp, + MicroProfileGpuGetTimeStamp_CB GetTimeStamp, + MicroProfileTicksPerSecondGpu_CB TicksPerSecond, + MicroProfileGetGpuTickReference_CB GetTickReference, + MicroProfileGpuFlip_CB Flip, + MicroProfileGpuShutdown_CB Shutdown) +{ + + MP_ASSERT(S.pGPU == nullptr); + pGPU->Type = eType; + S.pGPU = pGPU; + + MicroProfileGpuInsertTimeStamp_Callback = InsertTimeStamp; + MicroProfileGpuGetTimeStamp_Callback = GetTimeStamp; + MicroProfileTicksPerSecondGpu_Callback = TicksPerSecond; + MicroProfileGetGpuTickReference_Callback = GetTickReference; + MicroProfileGpuFlip_Callback = Flip; + MicroProfileGpuShutdown_Callback = Shutdown; +} +#endif + +#if MICROPROFILE_GPU_TIMERS_D3D11 +//:'######:::'########::'##::::'##::::'########:::'#######::'########:::::'##::::::'##::: +//'##... ##:: ##.... ##: ##:::: ##:::: ##.... ##:'##.... ##: ##.... ##::'####::::'####::: +// ##:::..::: ##:::: ##: ##:::: ##:::: ##:::: ##:..::::: ##: ##:::: ##::.. ##::::.. ##::: +// ##::'####: ########:: ##:::: ##:::: ##:::: ##::'#######:: ##:::: ##:::: ##:::::: ##::: +// ##::: ##:: ##.....::: ##:::: ##:::: ##:::: ##::...... ##: ##:::: ##:::: ##:::::: ##::: +// ##::: ##:: ##:::::::: ##:::: ##:::: ##:::: ##:'##:::: ##: ##:::: ##:::: ##:::::: ##::: +//. ######::: ##::::::::. #######::::: ########::. #######:: ########:::'######::'######: +//:......::::..::::::::::.......::::::........::::.......:::........::::......:::......:: +uint32_t MicroProfileGpuInsertTimeStampD3D11(void* pContext_) +{ + MicroProfileGpuTimerStateD3D11* pGPU = MicroProfileGetGpuTimerStateD3D11(); + if(!pGPU) + return 0; + MicroProfileD3D11Frame& Frame = pGPU->m_QueryFrames[pGPU->m_nQueryFrame]; + uint32_t nStart = Frame.m_nQueryStart; + if(Frame.m_nRateQueryStarted) + { + uint32_t nIndex = (uint32_t)-1; + do + { + nIndex = Frame.m_nQueryCount.load(); + if(nIndex + 1 >= Frame.m_nQueryCountMax) + { + return (uint32_t)-1; + } + } while(!Frame.m_nQueryCount.compare_exchange_weak(nIndex, nIndex + 1)); + nIndex += nStart; + uint32_t nQueryIndex = nIndex % MICROPROFILE_D3D11_MAX_QUERIES; + + ID3D11Query* pQuery = (ID3D11Query*)pGPU->m_pQueries[nQueryIndex]; + ID3D11DeviceContext* pContext = (ID3D11DeviceContext*)pContext_; + pContext->End(pQuery); + return nQueryIndex; + } + return (uint32_t)-1; +} + +uint64_t MicroProfileGpuGetTimeStampD3D11(uint32_t nIndex) +{ + if(nIndex == (uint32_t)-1) + { + return (uint64_t)-1; + } + MicroProfileGpuTimerStateD3D11* pGPU = MicroProfileGetGpuTimerStateD3D11(); + if(!pGPU) + return 0; + + int64_t nResult = pGPU->m_nQueryResults[nIndex]; + MP_ASSERT(nResult != -1); + return nResult; +} + +bool MicroProfileGpuGetDataD3D11(void* pQuery, void* pData, uint32_t nDataSize) +{ + MicroProfileGpuTimerStateD3D11* pGPU = MicroProfileGetGpuTimerStateD3D11(); + if(!pGPU) + return false; + + HRESULT hr; + do + { + hr = ((ID3D11DeviceContext*)pGPU->m_pImmediateContext)->GetData((ID3D11Query*)pQuery, pData, nDataSize, 0); + } while(hr == S_FALSE); + switch(hr) + { + case DXGI_ERROR_DEVICE_REMOVED: + case DXGI_ERROR_INVALID_CALL: + case E_INVALIDARG: + MP_BREAK(); + return false; + } + return true; +} + +uint64_t MicroProfileTicksPerSecondGpuD3D11() +{ + MicroProfileGpuTimerStateD3D11* pGPU = MicroProfileGetGpuTimerStateD3D11(); + if(!pGPU) + return 1; + + return pGPU->m_nQueryFrequency; +} + +uint32_t MicroProfileGpuFlipD3D11(void* pDeviceContext_) +{ + if(!pDeviceContext_) + { + return (uint32_t)-1; + } + MicroProfileGpuTimerStateD3D11* pGPU = MicroProfileGetGpuTimerStateD3D11(); + if(!pGPU) + return 0; + + ID3D11DeviceContext* pDeviceContext = (ID3D11DeviceContext*)pDeviceContext_; + uint32_t nFrameTimeStamp = MicroProfileGpuInsertTimeStamp(pDeviceContext); + MicroProfileD3D11Frame& CurrentFrame = pGPU->m_QueryFrames[pGPU->m_nQueryFrame]; + ID3D11DeviceContext* pImmediateContext = (ID3D11DeviceContext*)pGPU->m_pImmediateContext; + if(CurrentFrame.m_nRateQueryStarted) + { + pImmediateContext->End((ID3D11Query*)CurrentFrame.m_pRateQuery); + } + uint32_t nNextFrame = (pGPU->m_nQueryFrame + 1) % MICROPROFILE_GPU_FRAME_DELAY; + pGPU->m_nQueryPut = (CurrentFrame.m_nQueryStart + CurrentFrame.m_nQueryCount) % MICROPROFILE_D3D11_MAX_QUERIES; + MicroProfileD3D11Frame& OldFrame = pGPU->m_QueryFrames[nNextFrame]; + if(OldFrame.m_nRateQueryStarted) + { + struct RateQueryResult + { + uint64_t nFrequency; + BOOL bDisjoint; + }; + RateQueryResult Result; + if(MicroProfileGpuGetDataD3D11(OldFrame.m_pRateQuery, &Result, sizeof(Result))) + { + if(pGPU->m_nQueryFrequency != (int64_t)Result.nFrequency) + { + if(pGPU->m_nQueryFrequency) + { + OutputDebugStringA("Query freq changing"); + } + pGPU->m_nQueryFrequency = Result.nFrequency; + } + uint32_t nStart = OldFrame.m_nQueryStart; + uint32_t nCount = OldFrame.m_nQueryCount; + for(uint32_t i = 0; i < nCount; ++i) + { + uint32_t nIndex = (i + nStart) % MICROPROFILE_D3D11_MAX_QUERIES; + + if(!MicroProfileGpuGetDataD3D11(pGPU->m_pQueries[nIndex], &pGPU->m_nQueryResults[nIndex], sizeof(uint64_t))) + { + pGPU->m_nQueryResults[nIndex] = -1; + } + } + } + else + { + uint32_t nStart = OldFrame.m_nQueryStart; + uint32_t nCount = OldFrame.m_nQueryCount; + + for(uint32_t i = 0; i < nCount; ++i) + { + uint32_t nIndex = (i + nStart) % MICROPROFILE_D3D11_MAX_QUERIES; + pGPU->m_nQueryResults[nIndex] = -1; + } + } + pGPU->m_nQueryGet = (OldFrame.m_nQueryStart + OldFrame.m_nQueryCount) % MICROPROFILE_D3D11_MAX_QUERIES; + } + + pGPU->m_nQueryFrame = nNextFrame; + MicroProfileD3D11Frame& NextFrame = pGPU->m_QueryFrames[nNextFrame]; + pImmediateContext->Begin((ID3D11Query*)NextFrame.m_pRateQuery); + NextFrame.m_nQueryStart = pGPU->m_nQueryPut; + NextFrame.m_nQueryCount = 0; + if(pGPU->m_nQueryPut >= pGPU->m_nQueryGet) + { + NextFrame.m_nQueryCountMax = (MICROPROFILE_D3D11_MAX_QUERIES - pGPU->m_nQueryPut) + pGPU->m_nQueryGet; + } + else + { + NextFrame.m_nQueryCountMax = pGPU->m_nQueryGet - pGPU->m_nQueryPut - 1; + } + if(NextFrame.m_nQueryCountMax) + NextFrame.m_nQueryCountMax -= 1; + NextFrame.m_nRateQueryStarted = 1; + return nFrameTimeStamp; +} + +void MicroProfileGpuInitD3D11(void* pDevice_, void* pImmediateContext) +{ + ID3D11Device* pDevice = (ID3D11Device*)pDevice_; + + MicroProfileGpuTimerStateD3D11* pGPU = MP_ALLOC_OBJECT(MicroProfileGpuTimerStateD3D11); + + MicroProfileGpuInitPlatform(MicroProfileGpuTimerStateType_D3D11, + pGPU, + MicroProfileGpuInsertTimeStampD3D11, + MicroProfileGpuGetTimeStampD3D11, + MicroProfileTicksPerSecondGpuD3D11, + MicroProfileGetGpuTickReferenceD3D11, + MicroProfileGpuFlipD3D11, + MicroProfileGpuShutdownD3D11); + + pGPU->m_pImmediateContext = pImmediateContext; + + D3D11_QUERY_DESC Desc; + Desc.MiscFlags = 0; + Desc.Query = D3D11_QUERY_TIMESTAMP; + for(uint32_t i = 0; i < MICROPROFILE_D3D11_MAX_QUERIES; ++i) + { + HRESULT hr = pDevice->CreateQuery(&Desc, (ID3D11Query**)&pGPU->m_pQueries[i]); + MP_ASSERT(hr == S_OK); + pGPU->m_nQueryResults[i] = -1; + } + HRESULT hr = pDevice->CreateQuery(&Desc, (ID3D11Query**)&pGPU->pSyncQuery); + MP_ASSERT(hr == S_OK); + + pGPU->m_nQueryPut = 0; + pGPU->m_nQueryGet = 0; + pGPU->m_nQueryFrame = 0; + pGPU->m_nQueryFrequency = 0; + Desc.Query = D3D11_QUERY_TIMESTAMP_DISJOINT; + for(uint32_t i = 0; i < MICROPROFILE_GPU_FRAME_DELAY; ++i) + { + pGPU->m_QueryFrames[i].m_nQueryStart = 0; + pGPU->m_QueryFrames[i].m_nQueryCount = 0; + pGPU->m_QueryFrames[i].m_nRateQueryStarted = 0; + hr = pDevice->CreateQuery(&Desc, (ID3D11Query**)&pGPU->m_QueryFrames[i].m_pRateQuery); + MP_ASSERT(hr == S_OK); + } +} + +void MicroProfileGpuShutdownD3D11() +{ + MicroProfileGpuTimerStateD3D11* pGPU = MicroProfileGetGpuTimerStateD3D11(); + if(!pGPU) + return; + + for(uint32_t i = 0; i < MICROPROFILE_D3D11_MAX_QUERIES; ++i) + { + if(pGPU->m_pQueries[i]) + { + ID3D11Query* pQuery = (ID3D11Query*)pGPU->m_pQueries[i]; + pQuery->Release(); + pGPU->m_pQueries[i] = 0; + } + } + for(uint32_t i = 0; i < MICROPROFILE_GPU_FRAME_DELAY; ++i) + { + if(pGPU->m_QueryFrames[i].m_pRateQuery) + { + ID3D11Query* pQuery = (ID3D11Query*)pGPU->m_QueryFrames[i].m_pRateQuery; + pQuery->Release(); + pGPU->m_QueryFrames[i].m_pRateQuery = 0; + } + } + if(pGPU->pSyncQuery) + { + ID3D11Query* pSyncQuery = (ID3D11Query*)pGPU->pSyncQuery; + pSyncQuery->Release(); + pGPU->pSyncQuery = 0; + } +} + +int MicroProfileGetGpuTickReferenceD3D11(int64_t* pOutCPU, int64_t* pOutGpu) +{ + MicroProfileGpuTimerStateD3D11* pGPU = MicroProfileGetGpuTimerStateD3D11(); + if(!pGPU) + return 0; + { + MicroProfileD3D11Frame& Frame = pGPU->m_QueryFrames[pGPU->m_nQueryFrame]; + if(Frame.m_nRateQueryStarted) + { + ID3D11Query* pSyncQuery = (ID3D11Query*)pGPU->pSyncQuery; + ID3D11DeviceContext* pImmediateContext = (ID3D11DeviceContext*)pGPU->m_pImmediateContext; + pImmediateContext->End(pSyncQuery); + + HRESULT hr; + do + { + hr = pImmediateContext->GetData(pSyncQuery, pOutGpu, sizeof(*pOutGpu), 0); + } while(hr == S_FALSE); + *pOutCPU = MP_TICK(); + switch(hr) + { + case DXGI_ERROR_DEVICE_REMOVED: + case DXGI_ERROR_INVALID_CALL: + case E_INVALIDARG: + MP_BREAK(); + return false; + } + MP_ASSERT(hr == S_OK); + return 1; + } + } + return 0; +} +MicroProfileGpuTimerStateD3D11* MicroProfileGetGpuTimerStateD3D11() +{ + if(S.pGPU && S.pGPU->Type == MicroProfileGpuTimerStateType_D3D11) + return (MicroProfileGpuTimerStateD3D11*)S.pGPU; + return nullptr; +} + +#endif + +#if MICROPROFILE_GPU_TIMERS_D3D12 +//:'######:::'########::'##::::'##::::'########:::'#######::'########:::::'##::::'#######:: +//'##... ##:: ##.... ##: ##:::: ##:::: ##.... ##:'##.... ##: ##.... ##::'####:::'##.... ##: +// ##:::..::: ##:::: ##: ##:::: ##:::: ##:::: ##:..::::: ##: ##:::: ##::.. ##:::..::::: ##: +// ##::'####: ########:: ##:::: ##:::: ##:::: ##::'#######:: ##:::: ##:::: ##::::'#######:: +// ##::: ##:: ##.....::: ##:::: ##:::: ##:::: ##::...... ##: ##:::: ##:::: ##:::'##:::::::: +// ##::: ##:: ##:::::::: ##:::: ##:::: ##:::: ##:'##:::: ##: ##:::: ##:::: ##::: ##:::::::: +//. ######::: ##::::::::. #######::::: ########::. #######:: ########:::'######: #########: +//:......::::..::::::::::.......::::::........::::.......:::........::::......::.........:: +#include +uint32_t MicroProfileGpuInsertTimeStampD3D12(void* pContext) +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + if(!pGPU || !pContext) + return 0; + + ID3D12GraphicsCommandList* pCommandList = (ID3D12GraphicsCommandList*)pContext; + + bool IsCopy = D3D12_COMMAND_LIST_TYPE_COPY == pCommandList->GetType(); + uint32_t nNode = pGPU->nCurrentNode; + uint32_t nFrame = pGPU->nFrame; + + ID3D12QueryHeap* pHeap = IsCopy ? pGPU->NodeState[nNode].pCopyQueueHeap : pGPU->NodeState[nNode].pHeap; + + uint32_t nQueryIndex = IsCopy ? ((pGPU->nFrameCountCopyQueueTimeStamps.fetch_add(1) + pGPU->nFrameStartCopyQueueTimeStamps) % MICROPROFILE_D3D12_MAX_QUERIES) + : ((pGPU->nFrameCountTimeStamps.fetch_add(1) + pGPU->nFrameStartTimeStamps) % MICROPROFILE_D3D12_MAX_QUERIES); + + pCommandList->EndQuery(pHeap, D3D12_QUERY_TYPE_TIMESTAMP, nQueryIndex); + MP_ASSERT(nQueryIndex <= 0xffff); + uint32_t res = (IsCopy ? 0x80000000 : 0) | ((nFrame << 16) & 0x7fff0000) | (nQueryIndex); + return res; +} + +void MicroProfileGpuFetchRange(uint32_t nBegin, int32_t nCount, uint64_t nFrame, int64_t nTimestampOffset) +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + if(!pGPU || nCount <= 0) + return; + void* pData = 0; + // uprintf("fetch [%d-%d]\n", nBegin, nBegin + nCount); + D3D12_RANGE Range = { sizeof(uint64_t) * nBegin, sizeof(uint64_t) * (nBegin + nCount) }; + pGPU->pBuffer->Map(0, &Range, &pData); + memcpy(&pGPU->nResults[nBegin], nBegin + (uint64_t*)pData, nCount * sizeof(uint64_t)); + for(int i = 0; i < nCount; ++i) + { + pGPU->nQueryFrames[i + nBegin] = nFrame; + pGPU->nResults[i + nBegin] -= nTimestampOffset; + } + pGPU->pBuffer->Unmap(0, 0); +} +void MicroProfileGpuFetchRangeCopy(uint32_t nBegin, int32_t nCount, uint64_t nFrame, int64_t nTimestampOffset) +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + if(!pGPU || nCount <= 0) + return; + void* pData = 0; + D3D12_RANGE Range = { sizeof(uint64_t) * nBegin, sizeof(uint64_t) * (nBegin + nCount) }; + pGPU->pBufferCopy->Map(0, &Range, &pData); + memcpy(&pGPU->nResultsCopy[nBegin], nBegin + (uint64_t*)pData, nCount * sizeof(uint64_t)); + for(int i = 0; i < nCount; ++i) + { + pGPU->nQueryFramesCopy[i + nBegin] = nFrame; + pGPU->nResultsCopy[i + nBegin] -= nTimestampOffset; + } + pGPU->pBufferCopy->Unmap(0, 0); +} +void MicroProfileGpuWaitFenceD3D12(uint32_t nNode, uint64_t nFence) +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + if(!pGPU) + return; + + auto GetFence = [&]() -> uint64_t + { + uint64_t f0 = pGPU->NodeState[nNode].pFence->GetCompletedValue(); + uint64_t f1 = pGPU->NodeState[nNode].pFenceCopy->GetCompletedValue(); + return MicroProfileMin(f0, f1); + }; + uint64_t nCompletedFrame = GetFence(); + // while(nCompletedFrame < nPending) + // while(0 < nPending - nCompletedFrame) + while(0 < (int64_t)(nFence - nCompletedFrame)) + { + MICROPROFILE_SCOPEI("Microprofile", "gpu-wait", MP_GREEN4); + Sleep(20); // todo: use event. + nCompletedFrame = GetFence(); + if((uint64_t)-1 == nCompletedFrame) // likely device removed. + return; + } +} + +void MicroProfileGpuFetchResultsD3D12(uint64_t nFrame) +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + if(!pGPU) + return; + uint64_t nPending = pGPU->nPendingFrame; + + // while(nPending <= nFrame) + // while(0 <= nFrame - nPending) + while(0 <= (int64_t)(nFrame - nPending)) + { + uint32_t nInternal = nPending % MICROPROFILE_D3D_INTERNAL_DELAY; + uint32_t nNode = pGPU->Frames[nInternal].nNode; + MicroProfileGpuWaitFenceD3D12(nNode, nPending); + int64_t nTimestampOffset = 0; + if(nNode != 0) + { + // Adjust timestamp queries from GPU x to be in GPU 0's frame of reference + HRESULT hr; + int64_t nCPU0, nGPU0; + hr = pGPU->NodeState[0].pCommandQueue->GetClockCalibration((uint64_t*)&nGPU0, (uint64_t*)&nCPU0); + MP_ASSERT(hr == S_OK); + int64_t nCPUx, nGPUx; + hr = pGPU->NodeState[nNode].pCommandQueue->GetClockCalibration((uint64_t*)&nGPUx, (uint64_t*)&nCPUx); + MP_ASSERT(hr == S_OK); + int64_t nFreqCPU = MicroProfileTicksPerSecondCpu(); + int64_t nElapsedCPU = nCPUx - nCPU0; + int64_t nElapsedGPU = pGPU->nFrequency * nElapsedCPU / nFreqCPU; + nTimestampOffset = nGPUx - nGPU0 - nElapsedGPU; + } + + { + uint32_t nTimeStampBegin = pGPU->Frames[nInternal].nTimeStampBegin; + uint32_t nTimeStampCount = pGPU->Frames[nInternal].nTimeStampCount; + MicroProfileGpuFetchRange( + nTimeStampBegin, (nTimeStampBegin + nTimeStampCount) > MICROPROFILE_D3D12_MAX_QUERIES ? MICROPROFILE_D3D12_MAX_QUERIES - nTimeStampBegin : nTimeStampCount, nPending, nTimestampOffset); + MicroProfileGpuFetchRange(0, (nTimeStampBegin + nTimeStampCount) - MICROPROFILE_D3D12_MAX_QUERIES, nPending, nTimestampOffset); + } + { + uint32_t nTimeStampBegin = pGPU->Frames[nInternal].nTimeStampBeginCopyQueue; + uint32_t nTimeStampCount = pGPU->Frames[nInternal].nTimeStampCountCopyQueue; + MicroProfileGpuFetchRangeCopy( + nTimeStampBegin, (nTimeStampBegin + nTimeStampCount) > MICROPROFILE_D3D12_MAX_QUERIES ? MICROPROFILE_D3D12_MAX_QUERIES - nTimeStampBegin : nTimeStampCount, nPending, nTimestampOffset); + MicroProfileGpuFetchRangeCopy(0, (nTimeStampBegin + nTimeStampCount) - MICROPROFILE_D3D12_MAX_QUERIES, nPending, nTimestampOffset); + } + nPending = ++pGPU->nPendingFrame; + MP_ASSERT(pGPU->nFrame > nPending); + } +} + +uint64_t MicroProfileGpuGetTimeStampD3D12(uint32_t nIndex) +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + if(!pGPU) + return 0; + + uint32_t nFrame = nIndex >> 16; + bool IsCopy = (nFrame & 0x8000) != 0; + nFrame &= 0x7fff; + uint32_t nQueryIndex = nIndex & 0xffff; + uint32_t lala = IsCopy ? pGPU->nQueryFramesCopy[nQueryIndex] : pGPU->nQueryFrames[nQueryIndex]; + // uprintf("read TS [%d <- %lld]\n", nQueryIndex, pGPU->nResults[nQueryIndex]); + MP_ASSERT(nIndex == 0 || (0x7fff & lala) == nFrame); + uint64_t r = IsCopy ? pGPU->nResultsCopy[nQueryIndex] : pGPU->nResults[nQueryIndex]; + if(r == 0x7fffffffffffffff) + { + MP_BREAK(); + } + return r; +} + +uint64_t MicroProfileTicksPerSecondGpuD3D12() +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + if(!pGPU) + return 1; + return pGPU->nFrequency; +} + +uint32_t MicroProfileGpuFlipD3D12(void* pContext) +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + if(!pGPU) + return 0; + uint32_t nNode = pGPU->nCurrentNode; + uint32_t nFrameIndex = pGPU->nFrame % MICROPROFILE_D3D_INTERNAL_DELAY; + uint32_t nCount = 0, nStart = 0; + uint32_t nCountCopyQueue = 0, nStartCopyQueue = 0; + + ID3D12CommandAllocator* pCommandAllocator = pGPU->Frames[nFrameIndex].pCommandAllocator; + ID3D12CommandAllocator* pCommandAllocatorCopy = pGPU->Frames[nFrameIndex].pCommandAllocatorCopy; + pCommandAllocator->Reset(); + pCommandAllocatorCopy->Reset(); + ID3D12GraphicsCommandList* pCommandList = pGPU->Frames[nFrameIndex].pCommandList[nNode]; + + pCommandList->Reset(pCommandAllocator, nullptr); + + ID3D12GraphicsCommandList* pCommandListCopy = nullptr; + + uint32_t nFrameTimeStamp = MicroProfileGpuInsertTimeStamp(pCommandList); + + { + nCount = pGPU->nFrameCountTimeStamps.exchange(0); + nStart = pGPU->nFrameStartTimeStamps; + pGPU->nFrameStartTimeStamps = (pGPU->nFrameStartTimeStamps + nCount) % MICROPROFILE_D3D12_MAX_QUERIES; + uint32_t nEnd = MicroProfileMin(nStart + nCount, (uint32_t)MICROPROFILE_D3D12_MAX_QUERIES); + MP_ASSERT(nStart != nEnd); + uint32_t nSize = nEnd - nStart; + pCommandList->ResolveQueryData(pGPU->NodeState[nNode].pHeap, D3D12_QUERY_TYPE_TIMESTAMP, nStart, nEnd - nStart, pGPU->pBuffer, nStart * sizeof(int64_t)); + if(nStart + nCount > MICROPROFILE_D3D12_MAX_QUERIES) + { + pCommandList->ResolveQueryData(pGPU->NodeState[nNode].pHeap, D3D12_QUERY_TYPE_TIMESTAMP, 0, nEnd + nStart - MICROPROFILE_D3D12_MAX_QUERIES, pGPU->pBuffer, 0); + } + pCommandList->Close(); + } + + { + pCommandListCopy = pGPU->Frames[nFrameIndex].pCommandListCopy[nNode]; + pCommandListCopy->Reset(pCommandAllocatorCopy, nullptr); + + nCountCopyQueue = pGPU->nFrameCountCopyQueueTimeStamps.exchange(0); + nStartCopyQueue = pGPU->nFrameStartCopyQueueTimeStamps; + pGPU->nFrameStartCopyQueueTimeStamps = (nStartCopyQueue + nCountCopyQueue) % MICROPROFILE_D3D12_MAX_QUERIES; + uint32_t nEnd = MicroProfileMin(nStartCopyQueue + nCountCopyQueue, (uint32_t)MICROPROFILE_D3D12_MAX_QUERIES); + if(nStartCopyQueue != nEnd) + { + uint32_t nSize = nEnd - nStartCopyQueue; + pCommandListCopy->ResolveQueryData( + pGPU->NodeState[nNode].pCopyQueueHeap, D3D12_QUERY_TYPE_TIMESTAMP, nStartCopyQueue, nEnd - nStartCopyQueue, pGPU->pBufferCopy, nStartCopyQueue * sizeof(int64_t)); + if(nStartCopyQueue + nCountCopyQueue > MICROPROFILE_D3D12_MAX_QUERIES) + { + pCommandListCopy->ResolveQueryData(pGPU->NodeState[nNode].pCopyQueueHeap, D3D12_QUERY_TYPE_TIMESTAMP, 0, nEnd + nStartCopyQueue - MICROPROFILE_D3D12_MAX_QUERIES, pGPU->pBufferCopy, 0); + } + } + pCommandListCopy->Close(); + } + + if(pCommandList) + { + ID3D12CommandList* pList = pCommandList; + pGPU->NodeState[nNode].pCommandQueue->ExecuteCommandLists(1, &pList); + } + if(pCommandListCopy) + { + ID3D12CommandList* pList = pCommandListCopy; + pGPU->NodeState[nNode].pCommandQueueCopy->ExecuteCommandLists(1, &pList); + } + pGPU->NodeState[nNode].pCommandQueue->Signal(pGPU->NodeState[nNode].pFence, pGPU->nFrame); + pGPU->NodeState[nNode].pCommandQueueCopy->Signal(pGPU->NodeState[nNode].pFenceCopy, pGPU->nFrame); + pGPU->Frames[nFrameIndex].nTimeStampBegin = nStart; + pGPU->Frames[nFrameIndex].nTimeStampCount = nCount; + pGPU->Frames[nFrameIndex].nTimeStampBeginCopyQueue = nStartCopyQueue; + pGPU->Frames[nFrameIndex].nTimeStampCountCopyQueue = nCountCopyQueue; + + pGPU->Frames[nFrameIndex].nNode = nNode; + + pGPU->nFrame++; + // fetch from earlier frames + + MicroProfileGpuFetchResultsD3D12(pGPU->nFrame - MICROPROFILE_GPU_FRAME_DELAY); + return nFrameTimeStamp; +} + +void MicroProfileGpuInitD3D12(void* pDevice_, uint32_t nNodeCount, void** pCommandQueues_, void** pCommandQueuesCopy_) +{ + MicroProfileGpuTimerStateD3D12* pGPU = MP_ALLOC_OBJECT(MicroProfileGpuTimerStateD3D12); + memset(pGPU, 0, sizeof(MicroProfileGpuTimerStateD3D12)); + + MicroProfileGpuInitPlatform(MicroProfileGpuTimerStateType_D3D12, + pGPU, + MicroProfileGpuInsertTimeStampD3D12, + MicroProfileGpuGetTimeStampD3D12, + MicroProfileTicksPerSecondGpuD3D12, + MicroProfileGetGpuTickReferenceD3D12, + MicroProfileGpuFlipD3D12, + MicroProfileGpuShutdownD3D12); + + ID3D12Device* pDevice = (ID3D12Device*)pDevice_; + + pGPU->pDevice = pDevice; + pGPU->nNodeCount = nNodeCount; + MP_ASSERT(pGPU->nNodeCount <= MICROPROFILE_D3D_MAX_NODE_COUNT); + + for(uint32_t nNode = 0; nNode < pGPU->nNodeCount; ++nNode) + { + pGPU->NodeState[nNode].pCommandQueue = (ID3D12CommandQueue*)pCommandQueues_[nNode]; + pGPU->NodeState[nNode].pCommandQueueCopy = (ID3D12CommandQueue*)pCommandQueuesCopy_[nNode]; + if(nNode == 0) + { + pGPU->NodeState[nNode].pCommandQueue->GetTimestampFrequency((uint64_t*)&(pGPU->nFrequency)); + MP_ASSERT(pGPU->nFrequency); + } + else + { + // Don't support GPUs with different timer frequencies for now + int64_t nFrequency; + pGPU->NodeState[nNode].pCommandQueue->GetTimestampFrequency((uint64_t*)&nFrequency); + MP_ASSERT(nFrequency == pGPU->nFrequency); + } + + D3D12_QUERY_HEAP_DESC QHDesc; + QHDesc.Count = MICROPROFILE_D3D12_MAX_QUERIES; + QHDesc.Type = D3D12_QUERY_HEAP_TYPE_TIMESTAMP; + QHDesc.NodeMask = MP_NODE_MASK_ONE(nNode); + HRESULT hr = pDevice->CreateQueryHeap(&QHDesc, IID_PPV_ARGS(&pGPU->NodeState[nNode].pHeap)); + MP_ASSERT(hr == S_OK); + QHDesc.Type = D3D12_QUERY_HEAP_TYPE_COPY_QUEUE_TIMESTAMP; + hr = pDevice->CreateQueryHeap(&QHDesc, IID_PPV_ARGS(&pGPU->NodeState[nNode].pCopyQueueHeap)); + MP_ASSERT(hr == S_OK); + + pDevice->CreateFence(pGPU->nPendingFrame, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&pGPU->NodeState[nNode].pFence)); + pDevice->CreateFence(pGPU->nPendingFrame, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&pGPU->NodeState[nNode].pFenceCopy)); + } + + HRESULT hr; + D3D12_HEAP_PROPERTIES HeapProperties; + HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + HeapProperties.CreationNodeMask = 0; + HeapProperties.VisibleNodeMask = MP_NODE_MASK_ALL(pGPU->nNodeCount); + HeapProperties.Type = D3D12_HEAP_TYPE_READBACK; + + const size_t nResourceSize = MICROPROFILE_D3D12_MAX_QUERIES * 8; + + D3D12_RESOURCE_DESC ResourceDesc; + ResourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + ResourceDesc.Alignment = 0; + ResourceDesc.Width = nResourceSize; + ResourceDesc.Height = 1; + ResourceDesc.DepthOrArraySize = 1; + ResourceDesc.MipLevels = 1; + ResourceDesc.Format = DXGI_FORMAT_UNKNOWN; + ResourceDesc.SampleDesc.Count = 1; + ResourceDesc.SampleDesc.Quality = 0; + ResourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + ResourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE; + + hr = pDevice->CreateCommittedResource(&HeapProperties, D3D12_HEAP_FLAG_NONE, &ResourceDesc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pGPU->pBuffer)); + MP_ASSERT(hr == S_OK); + hr = pDevice->CreateCommittedResource(&HeapProperties, D3D12_HEAP_FLAG_NONE, &ResourceDesc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pGPU->pBufferCopy)); + MP_ASSERT(hr == S_OK); + + pGPU->nFrame = 0; + pGPU->nPendingFrame = 0; + + for(MicroProfileFrameD3D12& Frame : pGPU->Frames) + { + hr = pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&Frame.pCommandAllocator)); + MP_ASSERT(hr == S_OK); + hr = pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COPY, IID_PPV_ARGS(&Frame.pCommandAllocatorCopy)); + MP_ASSERT(hr == S_OK); + for(uint32_t nNode = 0; nNode < pGPU->nNodeCount; ++nNode) + { + hr = pDevice->CreateCommandList(MP_NODE_MASK_ONE(nNode), D3D12_COMMAND_LIST_TYPE_DIRECT, Frame.pCommandAllocator, nullptr, IID_PPV_ARGS(&Frame.pCommandList[nNode])); + MP_ASSERT(hr == S_OK); + hr = Frame.pCommandList[nNode]->Close(); + MP_ASSERT(hr == S_OK); + hr = pDevice->CreateCommandList(MP_NODE_MASK_ONE(nNode), D3D12_COMMAND_LIST_TYPE_COPY, Frame.pCommandAllocatorCopy, nullptr, IID_PPV_ARGS(&Frame.pCommandListCopy[nNode])); + MP_ASSERT(hr == S_OK); + hr = Frame.pCommandListCopy[nNode]->Close(); + MP_ASSERT(hr == S_OK); + } + } +} + +void MicroProfileGpuShutdownD3D12() +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + if(!pGPU) + return; + for(uint32_t nNode = 0; nNode < pGPU->nNodeCount; ++nNode) + { + MicroProfileGpuWaitFenceD3D12(nNode, pGPU->nFrame - 1); + } + for(uint32_t nNode = 0; nNode < pGPU->nNodeCount; ++nNode) + { + pGPU->NodeState[nNode].pHeap->Release(); + pGPU->NodeState[nNode].pCopyQueueHeap->Release(); + pGPU->NodeState[nNode].pFence->Release(); + pGPU->NodeState[nNode].pFenceCopy->Release(); + } + pGPU->pBuffer->Release(); + pGPU->pBufferCopy->Release(); + for(MicroProfileFrameD3D12& Frame : pGPU->Frames) + { + Frame.pCommandAllocator->Release(); + Frame.pCommandAllocatorCopy->Release(); + for(uint32_t nNode = 0; nNode < pGPU->nNodeCount; ++nNode) + { + Frame.pCommandList[nNode]->Release(); + Frame.pCommandListCopy[nNode]->Release(); + } + } +} +void MicroProfileSetCurrentNodeD3D12(uint32_t nNode) +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + pGPU->nCurrentNode = nNode; +} + +int MicroProfileGetGpuTickReferenceD3D12(int64_t* pOutCPU, int64_t* pOutGpu) +{ + MicroProfileGpuTimerStateD3D12* pGPU = MicroProfileGetGpuTimerStateD3D12(); + if(!pGPU) + { + *pOutCPU = 1; + *pOutGpu = 1; + return 1; + } + + HRESULT hr = pGPU->NodeState[0].pCommandQueue->GetClockCalibration((uint64_t*)pOutGpu, (uint64_t*)pOutCPU); + MP_ASSERT(hr == S_OK); + return 1; +} + +MicroProfileGpuTimerStateD3D12* MicroProfileGetGpuTimerStateD3D12() +{ + if(S.pGPU && S.pGPU->Type == MicroProfileGpuTimerStateType_D3D12) + return (MicroProfileGpuTimerStateD3D12*)S.pGPU; + return nullptr; +} + +#endif + +#if MICROPROFILE_GPU_TIMERS_VULKAN + +//:'######:::'########::'##::::'##::::'##::::'##:'##::::'##:'##:::::::'##:::'##::::'###::::'##::: ##: +//'##... ##:: ##.... ##: ##:::: ##:::: ##:::: ##: ##:::: ##: ##::::::: ##::'##::::'## ##::: ###:: ##: +// ##:::..::: ##:::: ##: ##:::: ##:::: ##:::: ##: ##:::: ##: ##::::::: ##:'##::::'##:. ##:: ####: ##: +// ##::'####: ########:: ##:::: ##:::: ##:::: ##: ##:::: ##: ##::::::: #####::::'##:::. ##: ## ## ##: +// ##::: ##:: ##.....::: ##:::: ##::::. ##:: ##:: ##:::: ##: ##::::::: ##. ##::: #########: ##. ####: +// ##::: ##:: ##:::::::: ##:::: ##:::::. ## ##::: ##:::: ##: ##::::::: ##:. ##:: ##.... ##: ##:. ###: +//. ######::: ##::::::::. #######:::::::. ###::::. #######:: ########: ##::. ##: ##:::: ##: ##::. ##: +//:......::::..::::::::::.......:::::::::...::::::.......:::........::..::::..::..:::::..::..::::..:: + +#ifndef MICROPROFILE_VULKAN_MAX_QUERIES +#define MICROPROFILE_VULKAN_MAX_QUERIES (32 << 10) +#endif + +#define MICROPROFILE_VULKAN_MAX_NODE_COUNT 4 +#define MICROPROFILE_VULKAN_INTERNAL_DELAY 8 + +#include +struct MicroProfileGpuFrameVulkan +{ + uint32_t nBegin; + uint32_t nCount; + uint32_t nNode; + VkCommandBuffer CommandBuffer[MICROPROFILE_VULKAN_MAX_NODE_COUNT]; + VkFence Fences[MICROPROFILE_VULKAN_MAX_NODE_COUNT]; +}; +struct MicroProfileGpuTimerStateVulkan : public MicroProfileGpuTimerState +{ + VkDevice Devices[MICROPROFILE_VULKAN_MAX_NODE_COUNT]; + VkPhysicalDevice PhysicalDevices[MICROPROFILE_VULKAN_MAX_NODE_COUNT]; + VkQueue Queues[MICROPROFILE_VULKAN_MAX_NODE_COUNT]; + VkQueryPool QueryPool[MICROPROFILE_VULKAN_MAX_NODE_COUNT]; + VkCommandPool CommandPool[MICROPROFILE_VULKAN_MAX_NODE_COUNT]; + + uint32_t nNodeCount; + uint32_t nCurrentNode; + uint64_t nFrame; + uint64_t nPendingFrame; + uint32_t nFrameStart; + std::atomic nFrameCount; + int64_t nFrequency; + + uint16_t nQueryFrames[MICROPROFILE_VULKAN_MAX_QUERIES]; + int64_t nResults[MICROPROFILE_VULKAN_MAX_QUERIES]; + + MicroProfileGpuFrameVulkan Frames[MICROPROFILE_VULKAN_INTERNAL_DELAY]; +}; + +MicroProfileGpuTimerStateVulkan* MicroProfileGetGpuTimerStateVulkan() +{ + if(S.pGPU && S.pGPU->Type == MicroProfileGpuTimerStateType_Vulkan) + return (MicroProfileGpuTimerStateVulkan*)S.pGPU; + return nullptr; +} + +uint32_t MicroProfileGpuInsertTimeStampVulkan(void* pContext) +{ + MicroProfileGpuTimerStateVulkan* pGPU = MicroProfileGetGpuTimerStateVulkan(); + if(!pGPU) + return 0; + VkCommandBuffer CB = (VkCommandBuffer)pContext; + uint32_t nNode = pGPU->nCurrentNode; + uint32_t nFrame = pGPU->nFrame; + uint32_t nQueryIndex = (pGPU->nFrameCount.fetch_add(1) + pGPU->nFrameStart) % MICROPROFILE_VULKAN_MAX_QUERIES; + vkCmdWriteTimestamp(CB, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, pGPU->QueryPool[nNode], nQueryIndex); + MP_ASSERT(nQueryIndex <= 0xffff); + // uprintf("insert timestamp %d :: %d ... ctx %p\n", nQueryIndex, nFrame, pContext); + return ((nFrame << 16) & 0xffff0000) | (nQueryIndex); +} + +void MicroProfileGpuFetchRangeVulkan(VkCommandBuffer CommandBuffer, uint32_t nNode, uint32_t nBegin, int32_t nCount, uint64_t nFrame, int64_t nTimestampOffset) +{ + if(nCount <= 0) + return; + MicroProfileGpuTimerStateVulkan* pGPU = MicroProfileGetGpuTimerStateVulkan(); + if(!pGPU) + return; + + vkGetQueryPoolResults(pGPU->Devices[nNode], pGPU->QueryPool[nNode], nBegin, nCount, 8 * nCount, &pGPU->nResults[nBegin], 8, VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_PARTIAL_BIT); + vkCmdResetQueryPool(CommandBuffer, pGPU->QueryPool[nNode], nBegin, nCount); + for(int i = 0; i < nCount; ++i) + { + pGPU->nQueryFrames[i + nBegin] = nFrame; + } +} +void MicroProfileGpuWaitFenceVulkan(uint32_t nNode, uint64_t nFrame) +{ + MicroProfileGpuTimerStateVulkan* pGPU = MicroProfileGetGpuTimerStateVulkan(); + if(!pGPU) + return; + + int r; + int c = 0; + do + { + MICROPROFILE_SCOPEI("Microprofile", "gpu-wait", MP_GREEN4); + r = vkWaitForFences(pGPU->Devices[nNode], 1, &pGPU->Frames[nFrame].Fences[nNode], 1, 1000 * 30); +#if 0 + if(c++ > 1000 && (c%100) == 0) + { + uprintf("waiting really long time for fence\n"); + OutputDebugString("waiting really long time for fence\n"); + } +#endif + } while(r != VK_SUCCESS); +} + +void MicroProfileGpuFetchResultsVulkan(VkCommandBuffer Buffer, uint64_t nFrame) +{ + MicroProfileGpuTimerStateVulkan* pGPU = MicroProfileGetGpuTimerStateVulkan(); + if(!pGPU) + return; + + uint64_t nPending = pGPU->nPendingFrame; + // while(nPending <= nFrame) + // while(0 <= nFrame - nPending) + while(0 <= (int64_t)(nFrame - nPending)) + { + uint32_t nInternal = nPending % MICROPROFILE_VULKAN_INTERNAL_DELAY; + uint32_t nNode = pGPU->Frames[nInternal].nNode; + MicroProfileGpuWaitFenceVulkan(nNode, nInternal); + int64_t nTimestampOffset = 0; + + if(nNode != 0) + { + MP_ASSERT(0 && "NOT IMPLEMENTED"); + // note: timestamp adjustment not implemented. + } + + uint32_t nBegin = pGPU->Frames[nInternal].nBegin; + uint32_t nCount = pGPU->Frames[nInternal].nCount; + MicroProfileGpuFetchRangeVulkan(Buffer, nNode, nBegin, (nBegin + nCount) > MICROPROFILE_VULKAN_MAX_QUERIES ? MICROPROFILE_VULKAN_MAX_QUERIES - nBegin : nCount, nPending, nTimestampOffset); + MicroProfileGpuFetchRangeVulkan(Buffer, nNode, 0, (nBegin + nCount) - MICROPROFILE_VULKAN_MAX_QUERIES, nPending, nTimestampOffset); + + nPending = ++pGPU->nPendingFrame; + MP_ASSERT(pGPU->nFrame > nPending); + } +} + +uint64_t MicroProfileGpuGetTimeStampVulkan(uint32_t nIndex) +{ + if(nIndex == (uint32_t)-1) + { + return 0; + } + + MicroProfileGpuTimerStateVulkan* pGPU = MicroProfileGetGpuTimerStateVulkan(); + if(!pGPU) + return 0; + + uint32_t nFrame = nIndex >> 16; + uint32_t nQueryIndex = nIndex & 0xffff; + uint32_t lala = pGPU->nQueryFrames[nQueryIndex]; + MP_ASSERT((0xffff & lala) == nFrame); + // uprintf("read TS [%d <- %lld]\n", nQueryIndex, pGPU->nResults[nQueryIndex]); + return pGPU->nResults[nQueryIndex]; +} + +uint64_t MicroProfileTicksPerSecondGpuVulkan() +{ + MicroProfileGpuTimerStateVulkan* pGPU = MicroProfileGetGpuTimerStateVulkan(); + if(!pGPU) + return 1; + return pGPU->nFrequency; +} + +uint32_t MicroProfileGpuFlipVulkan(void* pContext) +{ + MicroProfileGpuTimerStateVulkan* pGPU = MicroProfileGetGpuTimerStateVulkan(); + if(!pGPU) + return 0; + + uint32_t nNode = pGPU->nCurrentNode; + uint32_t nFrameIndex = pGPU->nFrame % MICROPROFILE_VULKAN_INTERNAL_DELAY; + uint32_t nCount = 0, nStart = 0; + + VkCommandBuffer CommandBuffer = pGPU->Frames[nFrameIndex].CommandBuffer[nNode]; + auto& F = pGPU->Frames[nFrameIndex]; + VkFence Fence = F.Fences[nNode]; + VkDevice Device = pGPU->Devices[nNode]; + VkQueue Queue = pGPU->Queues[nNode]; + + vkWaitForFences(Device, 1, &Fence, 1, (uint64_t)-1); + uint32_t nFrameTimeStamp = MicroProfileGpuInsertTimeStamp(pContext); + vkResetCommandBuffer(F.CommandBuffer[nNode], VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT); + + VkCommandBufferBeginInfo CBI; + CBI.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + CBI.pNext = 0; + CBI.pInheritanceInfo = 0; + CBI.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + vkBeginCommandBuffer(F.CommandBuffer[nNode], &CBI); + vkResetFences(Device, 1, &Fence); + + nCount = pGPU->nFrameCount.exchange(0); + nStart = pGPU->nFrameStart; + pGPU->nFrameStart = (pGPU->nFrameStart + nCount) % MICROPROFILE_VULKAN_MAX_QUERIES; + uint32_t nEnd = MicroProfileMin(nStart + nCount, (uint32_t)MICROPROFILE_VULKAN_MAX_QUERIES); + MP_ASSERT(nStart != nEnd); + uint32_t nSize = nEnd - nStart; + + pGPU->Frames[nFrameIndex].nBegin = nStart; + pGPU->Frames[nFrameIndex].nCount = nCount; + pGPU->Frames[nFrameIndex].nNode = nNode; + pGPU->nFrame++; + ////fetch from earlier frames + MicroProfileGpuFetchResultsVulkan(CommandBuffer, pGPU->nFrame - MICROPROFILE_GPU_FRAME_DELAY); + + vkEndCommandBuffer(F.CommandBuffer[nNode]); + VkSubmitInfo SubmitInfo = {}; + SubmitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + SubmitInfo.pNext = nullptr; + SubmitInfo.waitSemaphoreCount = 0; + SubmitInfo.pWaitSemaphores = nullptr; + SubmitInfo.commandBufferCount = 1; + SubmitInfo.pCommandBuffers = &CommandBuffer; + SubmitInfo.signalSemaphoreCount = 0; + SubmitInfo.pSignalSemaphores = nullptr; + vkQueueSubmit(Queue, 1, &SubmitInfo, Fence); + return nFrameTimeStamp; +} + +void MicroProfileGpuInitVulkan(VkDevice* pDevices, VkPhysicalDevice* pPhysicalDevices, VkQueue* pQueues, uint32_t* QueueFamily, uint32_t nNodeCount) +{ + MicroProfileGpuTimerStateVulkan* pGPU = MP_ALLOC_OBJECT(MicroProfileGpuTimerStateVulkan); + memset(pGPU, 0, sizeof(MicroProfileGpuTimerStateVulkan)); + MicroProfileGpuInitPlatform(MicroProfileGpuTimerStateType_Vulkan, + pGPU, + MicroProfileGpuInsertTimeStampVulkan, + MicroProfileGpuGetTimeStampVulkan, + MicroProfileTicksPerSecondGpuVulkan, + MicroProfileGetGpuTickReferenceVulkan, + MicroProfileGpuFlipVulkan, + MicroProfileGpuShutdownVulkan); + + pGPU->nNodeCount = nNodeCount; + + VkQueryPoolCreateInfo Q; + Q.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; + Q.pNext = 0; + Q.flags = 0; + Q.queryType = VK_QUERY_TYPE_TIMESTAMP; + Q.queryCount = MICROPROFILE_VULKAN_MAX_QUERIES + 1; + + VkCommandPoolCreateInfo CreateInfo; + CreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; + CreateInfo.pNext = 0; + CreateInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; + + VkResult r; + for(uint32_t i = 0; i < nNodeCount; ++i) + { + pGPU->Devices[i] = pDevices[i]; + pGPU->PhysicalDevices[i] = pPhysicalDevices[i]; + pGPU->Queues[i] = pQueues[i]; + r = vkCreateQueryPool(pGPU->Devices[i], &Q, 0, &pGPU->QueryPool[i]); + MP_ASSERT(r == VK_SUCCESS); + + CreateInfo.queueFamilyIndex = QueueFamily[i]; + r = vkCreateCommandPool(pGPU->Devices[i], &CreateInfo, 0, &pGPU->CommandPool[i]); + MP_ASSERT(r == VK_SUCCESS); + + for(uint32_t j = 0; j < MICROPROFILE_VULKAN_INTERNAL_DELAY; ++j) + { + auto& F = pGPU->Frames[j]; + VkCommandBufferAllocateInfo AllocInfo; + AllocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; + AllocInfo.pNext = 0; + AllocInfo.commandBufferCount = 1; + AllocInfo.commandPool = pGPU->CommandPool[i]; + AllocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; + r = vkAllocateCommandBuffers(pGPU->Devices[i], &AllocInfo, &F.CommandBuffer[i]); + MP_ASSERT(r == VK_SUCCESS); + + VkFenceCreateInfo FCI; + FCI.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; + FCI.pNext = 0; + FCI.flags = j == 0 ? 0 : VK_FENCE_CREATE_SIGNALED_BIT; + r = vkCreateFence(pGPU->Devices[i], &FCI, 0, &F.Fences[i]); + MP_ASSERT(r == VK_SUCCESS); + if(j == 0) + { + VkCommandBufferBeginInfo CBI; + CBI.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + CBI.pNext = 0; + CBI.pInheritanceInfo = 0; + CBI.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + vkBeginCommandBuffer(F.CommandBuffer[i], &CBI); + vkCmdResetQueryPool(F.CommandBuffer[i], pGPU->QueryPool[i], 0, MICROPROFILE_VULKAN_MAX_QUERIES + 1); + + vkEndCommandBuffer(F.CommandBuffer[i]); + VkSubmitInfo SubmitInfo = {}; + SubmitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + SubmitInfo.pNext = nullptr; + SubmitInfo.waitSemaphoreCount = 0; + SubmitInfo.pWaitSemaphores = nullptr; + SubmitInfo.commandBufferCount = 1; + SubmitInfo.pCommandBuffers = &F.CommandBuffer[i]; + SubmitInfo.signalSemaphoreCount = 0; + SubmitInfo.pSignalSemaphores = nullptr; + vkQueueSubmit(pQueues[i], 1, &SubmitInfo, F.Fences[i]); + vkWaitForFences(pGPU->Devices[i], 1, &F.Fences[i], 1, (uint64_t)-1); + vkResetCommandBuffer(F.CommandBuffer[i], VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT); + } + } + } + + VkPhysicalDeviceProperties Properties; + vkGetPhysicalDeviceProperties(pPhysicalDevices[0], &Properties); + pGPU->nFrequency = 1000000000ll / Properties.limits.timestampPeriod; +} + +void MicroProfileGpuShutdownVulkan() +{ + // this is clearly leaking .. +} +void MicroProfileSetCurrentNodeVulkan(uint32_t nNode) +{ + + MicroProfileGpuTimerStateVulkan* pGPU = MicroProfileGetGpuTimerStateVulkan(); + if(!pGPU) + return; + pGPU->nCurrentNode = nNode; +} + +int MicroProfileGetGpuTickReferenceVulkan(int64_t* pOutCPU, int64_t* pOutGpu) +{ + MicroProfileGpuTimerStateVulkan* pGPU = MicroProfileGetGpuTimerStateVulkan(); + if(!pGPU) + return 0; + + auto& F = pGPU->Frames[pGPU->nFrame % MICROPROFILE_VULKAN_INTERNAL_DELAY]; + uint32_t nGpu = pGPU->nCurrentNode; + + VkCommandBufferBeginInfo CBI; + CBI.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + CBI.pNext = 0; + CBI.pInheritanceInfo = 0; + CBI.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + VkCommandBuffer CB = F.CommandBuffer[nGpu]; + VkDevice Device = pGPU->Devices[nGpu]; + VkFence Fence = F.Fences[nGpu]; + + vkWaitForFences(Device, 1, &Fence, 1, (uint64_t)-1); + vkResetFences(Device, 1, &Fence); + vkResetCommandBuffer(CB, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT); + vkBeginCommandBuffer(CB, &CBI); + vkCmdResetQueryPool(CB, pGPU->QueryPool[nGpu], MICROPROFILE_VULKAN_MAX_QUERIES, 1); + vkCmdWriteTimestamp(CB, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, pGPU->QueryPool[nGpu], MICROPROFILE_VULKAN_MAX_QUERIES); + vkEndCommandBuffer(CB); + VkSubmitInfo SubmitInfo = {}; + SubmitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + SubmitInfo.pNext = nullptr; + SubmitInfo.waitSemaphoreCount = 0; + SubmitInfo.pWaitSemaphores = nullptr; + SubmitInfo.commandBufferCount = 1; + SubmitInfo.pCommandBuffers = &CB; + SubmitInfo.signalSemaphoreCount = 0; + SubmitInfo.pSignalSemaphores = nullptr; + vkQueueSubmit(pGPU->Queues[nGpu], 1, &SubmitInfo, Fence); + vkWaitForFences(Device, 1, &Fence, 1, (uint64_t)-1); + *pOutGpu = 0; + vkGetQueryPoolResults(Device, pGPU->QueryPool[nGpu], MICROPROFILE_VULKAN_MAX_QUERIES, 1, 8, pOutGpu, 8, VK_QUERY_RESULT_64_BIT); + *pOutCPU = MP_TICK(); + return 1; +} +#endif + +#if MICROPROFILE_GPU_TIMERS_GL +//:'######:::'########::'##::::'##:::::'######:::'##::::::: +//'##... ##:: ##.... ##: ##:::: ##::::'##... ##:: ##::::::: +// ##:::..::: ##:::: ##: ##:::: ##:::: ##:::..::: ##::::::: +// ##::'####: ########:: ##:::: ##:::: ##::'####: ##::::::: +// ##::: ##:: ##.....::: ##:::: ##:::: ##::: ##:: ##::::::: +// ##::: ##:: ##:::::::: ##:::: ##:::: ##::: ##:: ##::::::: +//. ######::: ##::::::::. #######:::::. ######::: ########: +//:......::::..::::::::::.......:::::::......::::........:: + +void MicroProfileGpuInitGL() +{ + MicroProfileGpuTimerStateGL* pGPU = MP_ALLOC_OBJECT(MicroProfileGpuTimerStateGL); + memset(pGPU, 0, sizeof(MicroProfileGpuTimerStateGL)); + MicroProfileGpuInitPlatform(MicroProfileGpuTimerStateType_GL, + pGPU, + MicroProfileGpuInsertTimeStampGL, + MicroProfileGpuGetTimeStampGL, + MicroProfileTicksPerSecondGpuGL, + MicroProfileGetGpuTickReferenceGL, + MicroProfileGpuFlipGL, + MicroProfileGpuShutdownGL); + + pGPU->GLTimerPos = 0; + glGenQueries(MICROPROFILE_GL_MAX_QUERIES, &pGPU->GLTimers[0]); +} + +uint32_t MicroProfileGpuInsertTimeStampGL(void* pContext) +{ + MicroProfileGpuTimerStateGL* pGPU = MicroProfileGetGpuTimerStateGL(); + if(!pGPU) + return 0; + + uint32_t nIndex = (pGPU->GLTimerPos + 1) % MICROPROFILE_GL_MAX_QUERIES; + glQueryCounter(pGPU->GLTimers[nIndex], GL_TIMESTAMP); + pGPU->GLTimerPos = nIndex; + return nIndex; +} +uint64_t MicroProfileGpuGetTimeStampGL(uint32_t nKey) +{ + MicroProfileGpuTimerStateGL* pGPU = MicroProfileGetGpuTimerStateGL(); + if(!pGPU) + return 0; + + uint64_t result; + glGetQueryObjectui64v(pGPU->GLTimers[nKey], GL_QUERY_RESULT, &result); + return result; +} + +uint64_t MicroProfileTicksPerSecondGpuGL() +{ + return 1000000000ll; +} + +int MicroProfileGetGpuTickReferenceGL(int64_t* pOutCpu, int64_t* pOutGpu) +{ + MicroProfileGpuTimerStateGL* pGPU = MicroProfileGetGpuTimerStateGL(); + if(!pGPU) + return 0; + + int64_t nGpuTimeStamp; + glGetInteger64v(GL_TIMESTAMP, &nGpuTimeStamp); + if(nGpuTimeStamp) + { + *pOutCpu = MP_TICK(); + *pOutGpu = nGpuTimeStamp; +#if 0 // debug test if timestamp diverges + static int64_t nTicksPerSecondCpu = MicroProfileTicksPerSecondCpu(); + static int64_t nTicksPerSecondGpu = MicroProfileTicksPerSecondGpu(); + static int64_t nGpuStart = 0; + static int64_t nCpuStart = 0; + if(!nCpuStart) + { + nCpuStart = *pOutCpu; + nGpuStart = *pOutGpu; + } + static int nCountDown = 100; + if(0 == nCountDown--) + { + int64_t nCurCpu = *pOutCpu; + int64_t nCurGpu = *pOutGpu; + double fDistanceCpu = (nCurCpu - nCpuStart) / (double)nTicksPerSecondCpu; + double fDistanceGpu = (nCurGpu - nGpuStart) / (double)nTicksPerSecondGpu; + + char buf[254]; + snprintf(buf, sizeof(buf)-1,"Distance %f %f diff %f\n", fDistanceCpu, fDistanceGpu, fDistanceCpu-fDistanceGpu); + OutputDebugString(buf); + nCountDown = 100; + } +#endif + return 1; + } + return 0; +} +uint32_t MicroProfileGpuFlipGL(void* pContext) +{ + return MicroProfileGpuInsertTimeStampGL(pContext); +} + +void MicroProfileGpuShutdownGL() +{ + MicroProfileGpuTimerStateGL* pGPU = MicroProfileGetGpuTimerStateGL(); + if(!pGPU) + return; + + glDeleteQueries(MICROPROFILE_GL_MAX_QUERIES, &pGPU->GLTimers[0]); +} + +MicroProfileGpuTimerStateGL* MicroProfileGetGpuTimerStateGL() +{ + if(S.pGPU && S.pGPU->Type == MicroProfileGpuTimerStateType_GL) + return (MicroProfileGpuTimerStateGL*)S.pGPU; + return nullptr; +} +#endif + +uint32_t MicroProfileStringHash(const char* pString) // note matching: code in javascript: microprofilelive.html: function StringHash(s) +{ + uint32_t h = 0xfeedba3e; + char c; + while(0 != (c = *pString++)) + { + h = c + ((h << 5) - h); + } + return h; +} + +const char* MicroProfileStrDup(const char* pStr) +{ + size_t len = strlen(pStr) + 1; + char* pOut = (char*)MP_ALLOC(len, 8); + memcpy(pOut, pStr, len); + return pOut; +} + +uint32_t MicroProfileColorFromString(const char* pString) // note matching code/constants in javascript: microprofilelive.html: function StringToColor(s) +{ + // var h = StringHash(s); + // var cidx = h % 360; + // return "hsl(" + cidx + ",50%, 70%)"; //note: matching code constants in microprofile.cpp: MicroProfileColorFromString + + float h = MicroProfileStringHash(pString) % 360; + float s = 0.5f; + float l = 0.7f; + // from https://www.rapidtables.com/convert/color/hsl-to-rgb.html + float c = (1 - fabsf(2 * l - 1)) * s; + float x = c * (1 - fabsf(fmodf(h / 60, 2.f) - 1)); + float m = l - c / 2.f; + float r = 0.f, g = 0.f, b = 0.f; + if(h < 60) + { + r = c; + g = x; + } + else if(h < 120.f) + { + r = x; + g = c; + } + else if(h < 180.f) + { + g = c; + b = x; + } + else if(h < 240.f) + { + g = x; + b = c; + } + else if(h < 300.f) + { + r = x; + b = c; + } + else + { + r = c; + b = x; + } + r += m; + g += m; + b += m; + + r *= 255.f; + g *= 255.f; + b *= 255.f; + + uint32_t R = MicroProfileMin(0xffu, (uint32_t)r); + uint32_t G = MicroProfileMin(0xffu, (uint32_t)g); + uint32_t B = MicroProfileMin(0xffu, (uint32_t)b); + + return (R << 16) | (G << 8) | B; +} + +#if MICROPROFILE_DYNAMIC_INSTRUMENT +// '##::::'##::'#######:::'#######::'##:::'##:::::'######::'##::::'##::::'###::::'########::'########:'########:: +// ##:::: ##:'##.... ##:'##.... ##: ##::'##:::::'##... ##: ##:::: ##:::'## ##::: ##.... ##: ##.....:: ##.... ##: +// ##:::: ##: ##:::: ##: ##:::: ##: ##:'##:::::: ##:::..:: ##:::: ##::'##:. ##:: ##:::: ##: ##::::::: ##:::: ##: +// #########: ##:::: ##: ##:::: ##: #####:::::::. ######:: #########:'##:::. ##: ########:: ######::: ##:::: ##: +// ##.... ##: ##:::: ##: ##:::: ##: ##. ##:::::::..... ##: ##.... ##: #########: ##.. ##::: ##...:::: ##:::: ##: +// ##:::: ##: ##:::: ##: ##:::: ##: ##:. ##:::::'##::: ##: ##:::: ##: ##.... ##: ##::. ##:: ##::::::: ##:::: ##: +// ##:::: ##:. #######::. #######:: ##::. ##::::. ######:: ##:::: ##: ##:::: ##: ##:::. ##: ########: ########:: +// ..:::::..:::.......::::.......:::..::::..::::::......:::..:::::..::..:::::..::..:::::..::........::........::: + +#include +#include + +#if MICROPROFILE_BREAK_ON_PATCH_FAIL +#define BREAK_ON_PATCH_FAIL() MP_BREAK() +#else +#define BREAK_ON_PATCH_FAIL() \ + do \ + { \ + } while(0) +#endif + +void* MicroProfileX64FollowJump(void* pSrc); +bool MicroProfileCopyInstructionBytes(char* pDest, + void* pSrc, + const int nLimit, + const int nMaxSize, + char* pTrunk, + intptr_t nTrunkSize, + uint32_t nUsableJumpRegs, + int* nBytesDest, + int* nBytesSrc, + uint32_t* pRegsWritten, + uint32_t* nRetSafe); +bool MicroProfilePatchFunction(void* f, int Argument, MicroProfileHookFunc enter, MicroProfileHookFunc leave, MicroProfilePatchError* pError); +template +void MicroProfileIterateSymbols(Callback CB, uint32_t* nModules, uint32_t nNumModules); + +bool MicroProfileDemangleName(const char* pName, char* OutName, uint32_t Size); +bool MicroProfilePatchBeginSuspend(); +void MicroProfilePatchEndSuspend(); +bool MicroProfilePatchHasSuspendedThread(intptr_t Begin, intptr_t End); + +#if 1 +#define STRING_MATCH_SIZE 64 +typedef uint64_t uint_string_match; +#else +#define STRING_MATCH_SIZE 32 +typedef uint32_t uint_string_match; +#endif + +struct MicroProfileStringMatchMask +{ + uint_string_match nMask; + uint_string_match M[64]; +}; + +struct MicroProfileSymbolDesc +{ + const char* pName; + const char* pShortName; + intptr_t nAddress; + intptr_t nAddressEnd; + uint_string_match nMask; + int nIgnoreSymbol; + uint32_t nModule; +}; + +struct MicroProfileSymbolBlock +{ + MicroProfileSymbolBlock* pNext; + uint32_t nNumSymbols; + uint32_t nNumChars; + uint_string_match nMask; + MicroProfileStringMatchMask MatchMask; + enum + { + ESIZE = 4 << 10, + }; + union + { + MicroProfileSymbolDesc Symbols[ESIZE / sizeof(MicroProfileSymbolDesc)]; + char Chars[ESIZE]; + }; +}; + +typedef void (*MicroProfileOnSymbolCallback)(const char* pSymbolName, intptr_t nAddress); + +MP_THREAD_LOCAL uintptr_t g_MicroProfile_TLS[17] = { 16 }; + +extern "C" MP_NOINLINE uintptr_t MicroProfile_Patch_TLS_PUSH(uintptr_t t) +{ + uintptr_t* pTLS = &g_MicroProfile_TLS[0]; + + uintptr_t Limit = (uint32_t)pTLS[0]; + uintptr_t Pos = (uint32_t)(pTLS[0] >> 32); + if(Pos == Limit) + { + return 0; + } + else + { + pTLS[0] = (Limit) | ((Pos + 1) << 32); + } + pTLS[Pos + 1] = t; + return 1; +} +extern "C" MP_NOINLINE uintptr_t MicroProfile_Patch_TLS_POP() +{ + uintptr_t* pTLS = &g_MicroProfile_TLS[0]; + uintptr_t Limit = (uint32_t)pTLS[0]; + uintptr_t Pos = (uint32_t)(pTLS[0] >> 32); + if(Pos == 0) + { + MP_BREAK(); // this should never happen + return 0; + } + else + { + pTLS[0] = (Limit) | ((Pos - 1) << 32); + } + uintptr_t t = pTLS[Pos]; + return t; +} + +char* MicroProfileInsertRegisterJump(char* pCode, intptr_t pDest, int reg) +{ + MP_ASSERT(reg >= R_RAX && reg <= R_R15); + int large = reg >= R_R8 ? 1 : 0; + int offset = large ? (reg - R_R8) : (reg - R_RAX); + unsigned char* uc = (unsigned char*)pCode; + *uc++ = large ? 0x49 : 0x48; + *uc++ = 0xb8 + offset; + memcpy(uc, &pDest, 8); + uc += 8; + if(large) + *uc++ = 0x41; + *uc++ = 0xff; + *uc++ = 0xe0 + offset; + return (char*)uc; + // 164: 48 b8 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %rax + // 16e: 48 b9 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %rcx + // 178: 48 ba 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %rdx + // 182: 48 bb 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %rbx + // 18c: 48 bc 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %rsp + // 196: 48 bd 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %rbp + // 1a0: 48 be 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %rsi + // 1aa: 48 bf 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %rdi + // 1b4: 49 b8 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %r8 + // 1be: 49 b9 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %r9 + // 1c8: 49 ba 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %r10 + // 1d2: 49 bb 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %r11 + // 1dc: 49 bc 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %r12 + // 1e6: 49 bd 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %r13 + // 1f0: 49 be 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %r14 + // 1fa: 49 bf 08 07 06 05 04 03 02 01 movabsq $72623859790382856, %r15 + // 204: ff e0 jmpq *%rax + // 206: ff e1 jmpq *%rcx + // 208: ff e2 jmpq *%rdx + // 20a: ff e3 jmpq *%rbx + // 20c: ff e4 jmpq *%rsp + // 20e: ff e5 jmpq *%rbp + // 210: ff e6 jmpq *%rsi + // 212: ff e7 jmpq *%rdi + // 214: 41 ff e0 jmpq *%r8 + // 217: 41 ff e1 jmpq *%r9 + // 21a: 41 ff e2 jmpq *%r10 + // 21d: 41 ff e3 jmpq *%r11 + // 220: 41 ff e4 jmpq *%r12 + // 223: 41 ff e5 jmpq *%r13 + // 226: 41 ff e6 jmpq *%r14 + // 229: 41 ff e7 jmpq *%r15 +} + +char* MicroProfileInsertRelativeJump(char* pCode, intptr_t pDest) +{ + intptr_t src = intptr_t(pCode) + 5; + intptr_t off = pDest - src; + MP_ASSERT(off > intptr_t(0xffffffff80000000) && off <= 0x7fffffff); + int32_t i32off = (int32_t)off; + unsigned char* uc = (unsigned char*)pCode; + unsigned char* c = (unsigned char*)&i32off; + *uc++ = 0xe9; + memcpy(uc, c, 4); + uc += 4; + return (char*)uc; +} + +char* MicroProfileInsertRetJump(char* pCode, intptr_t pDest) +{ + uint32_t lower = (uint32_t)pDest; + uint32_t upper = (uint32_t)(pDest >> 32); + unsigned char* uc = (unsigned char*)pCode; + *uc++ = 0x68; + memcpy(uc, &lower, 4); + uc += 4; + *uc++ = 0xc7; + *uc++ = 0x44; + *uc++ = 0x24; + *uc++ = 0x04; + memcpy(uc, &upper, 4); + uc += 4; + *uc++ = 0xc3; + return (char*)uc; +} + +uint8_t* MicroProfileInsertMov(uint8_t* p, uint8_t* pend, int r, intptr_t value) +{ + int Large = r >= R_R8 ? 1 : 0; + int RegIndex = Large ? (r - R_R8) : (r - R_RAX); + *p++ = Large ? 0x49 : 0x48; + *p++ = 0xb8 + RegIndex; // + (reg - (large?(R_R8-R_RAX):0)); + intptr_t* pAddress = (intptr_t*)p; + pAddress[0] = value; + p = (uint8_t*)(pAddress + 1); + MP_ASSERT(p < pend); + return p; +} + +uint8_t* MicroProfileInsertCall(uint8_t* p, uint8_t* pend, int r) +{ + int Large = r >= R_R8 ? 1 : 0; + int RegIndex = Large ? (r - R_R8) : (r - R_RAX); + if(Large) + { + *p++ = 0x41; + } + *p++ = 0xff; + *p++ = 0xd0 + RegIndex; + MP_ASSERT(p < pend); + return p; +} + +bool MicroProfileStringMatch(const char* pSymbol, uint32_t nStartOffset, const char** pPatterns, uint32_t* nPatternLength, uint32_t nNumPatterns) +{ + MP_ASSERT(nStartOffset <= nNumPatterns); + const char* p = pSymbol; + for(uint32_t i = nStartOffset; i < nNumPatterns; ++i) + { + p = MP_STRCASESTR(p, pPatterns[i]); + if(p) + { + p += nPatternLength[i]; + } + else + { + return false; + } + } + return true; +} + +int MicroProfileStringMatchOffset(const char* pSymbol, const char** pPatterns, uint32_t* nPatternLength, uint32_t nNumPatterns) +{ + int nOffset = 0; + const char* p = pSymbol; + for(uint32_t i = 0; i < nNumPatterns; ++i) + { + p = MP_STRCASESTR(p, pPatterns[i]); + if(p) + { + p += nPatternLength[i]; + nOffset++; + } + else + { + break; + } + } + return nOffset; +} + +void* MicroProfileX64FollowJump(void* pSrc) +{ + for(uint32_t i = 0; i < S.DynamicTokenIndex; ++i) + if(S.FunctionsInstrumented[i] == pSrc) + return pSrc; // if already instrumented, do not follow the jump inserted by itself. + + // uprintf("deref possible trampoline for %p\n", pSrc); + _DecodeType dt = Decode64Bits; + _DInst Instructions[1]; + unsigned int nCount = 0; + + _CodeInfo ci; + ci.code = (uint8_t*)pSrc; + ci.codeLen = 15; + ci.codeOffset = 0; + ci.dt = dt; + ci.features = DF_NONE; + int r = distorm_decompose(&ci, Instructions, 1, &nCount); + if(!r || nCount != 1) + { + return pSrc; // fail, just return + } + + auto& I = Instructions[0]; + if(I.opcode == I_JMP) + { + if(I.ops[0].type == O_PC) + { + if(I.ops[0].size == 0x20) + { + intptr_t p = (intptr_t)pSrc; + p += I.size; + p += I.imm.sdword; + return (void*)p; + } + } + else if(I.ops[0].type == O_SMEM) + { + if(I.ops[0].index == R_RIP) + { + intptr_t p = (intptr_t)pSrc; + p += I.size; + p += I.disp; + void* pHest = *(void**)p; + return pHest; + } + } + uprintf("failed to interpret I_JMP %p %d %d\n", pSrc, I.ops[0].size, I.ops[0].type); + return pSrc; + MP_BREAK(); + } + return pSrc; +} + +bool MicroProfileCopyInstructionBytes(char* pDest, + void* pSrc, + const int nLimit, + const int nMaxSize, + char* pTrunk, + intptr_t nTrunkSize, + const uint32_t nUsableJumpRegs, + int* pBytesDest, + int* pBytesSrc, + uint32_t* pRegsWritten, + uint32_t* pRetSafe) +{ + + _DecodeType dt = Decode64Bits; + _DInst Instructions[128]; + int rip[128] = { 0 }; + uint32_t nRegsWrittenInstr[128] = { 0 }; + int offsets[129] = { 0 }; + unsigned int nCount = 0; + + _CodeInfo ci; + ci.code = (uint8_t*)pSrc; + ci.codeLen = nLimit + 15; + ci.codeOffset = 0; + ci.dt = dt; + ci.features = DF_NONE; + int r = distorm_decompose(&ci, Instructions, 128, &nCount); + if(r != DECRES_SUCCESS) + { + BREAK_ON_PATCH_FAIL(); + return false; + } + int offset = 0; + unsigned int i = 0; + unsigned nInstructions = 0; + int64_t nTrunkUsage = 0; + offsets[0] = 0; + uint32_t nRegsWritten = 0; + + auto Align16 = [](intptr_t p) { return (p + 15) & (~15); }; + + { + + intptr_t iTrunk = (intptr_t)pTrunk; + intptr_t iTrunkEnd = iTrunk + nTrunkSize; + intptr_t iTrunkAligned = (iTrunk + 15) & ~15; + nTrunkSize = iTrunkEnd - iTrunkAligned; + pTrunk = (char*)iTrunkAligned; + } + const uint8_t* pTrunkEnd = (uint8_t*)(pTrunk + nTrunkSize); + + auto RegToBit = [](int r) -> uint32_t + { + if(r >= R_RAX && r <= R_R15) + { + return (1u << (r - R_RAX)); + } + else if(r >= R_EAX && r <= R_R15D) + { + return (1u << (r - R_EAX)); + } + else if(r >= R_AX && r <= R_R15W) + { + return (1u << (r - R_AX)); + } + else if(r >= R_AL && r <= R_R15B) + { + return (1u << (r - R_AL)); + } + return 0; // might hit on registers like RIP + MP_BREAK(); + }; +#ifdef _WIN32 + const uint32_t nUsableRegisters = RegToBit(R_RAX) | RegToBit(R_R10) | RegToBit(R_R11); +#else + const uint32_t nUsableRegisters = RegToBit(R_RAX) | RegToBit(R_R10) | RegToBit(R_R11); +#endif + + int nBytesToMove = 0; + for(i = 0; i < nCount; ++i) + { + nBytesToMove += Instructions[i].size; + if(nBytesToMove >= nLimit) + break; + } + *pBytesSrc = nBytesToMove; + + uint32_t nRspMask = RegToBit(R_RSP); + *pRetSafe = 1; + + for(i = 0; i < nCount; ++i) + { + rip[i] = 0; + auto& I = Instructions[i]; + // bool bHasRipReference = false; + if(I.opcode == I_LEA) + { + } + if(I.opcode == I_CALL) + { + auto& O = I.ops[0]; + if(O.type != O_PC || O.size != 0x20) + { + uprintf("unknown call encountered. cannot move\n"); + BREAK_ON_PATCH_FAIL(); + return false; + } + if((nRegsWritten & nUsableRegisters) == nUsableRegisters) + { + uprintf("call encountered, but register all regs was written to. TODO: push regs?\n"); + BREAK_ON_PATCH_FAIL(); + return false; + } + // return value might be used past return so preserve registers. +#ifdef _WIN32 + nRegsWritten |= RegToBit(R_RAX); +#else + nRegsWritten |= RegToBit(R_RAX) | RegToBit(R_RDX); +#endif + } + + switch(I.ops[0].type) + { + case O_REG: + { + uint32_t reg = I.ops[0].index; + nRegsWritten |= RegToBit(reg); + auto& O2 = I.ops[1]; + switch(O2.type) + { + case O_REG: + case O_MEM: + case O_SMEM: + { + // if register is RSP 'contaminated', it prevents us from using that to do retjmps + uint32_t nMask = RegToBit(O2.index); + if(nRspMask & nMask) + { + nRspMask |= RegToBit(reg); + } + } + default: + break; + } + break; + } + case O_MEM: + case O_SMEM: + { + uint32_t reg = I.ops[0].index; + if(nRspMask & RegToBit(reg)) + { + uprintf("found contaminated reg at +%lld\n", (long long)I.addr); + *pRetSafe = 0; + } + break; + } + } + nRegsWrittenInstr[i] = nRegsWritten; + for(int j = 0; j < 4; ++j) + { + auto& O = I.ops[j]; + + switch(O.type) + { + case O_REG: + case O_SMEM: + case O_MEM: + { + if(O.index == R_RIP) + { + if(j != 1) + { + uprintf("found non base reference of rip. fail\n"); + BREAK_ON_PATCH_FAIL(); + return false; + } + if(I.dispSize != 0x20 && I.dispSize != 0x10) + { + uprintf("found offset size != 32 && != 16 bit. not implemented\n"); + BREAK_ON_PATCH_FAIL(); + return false; + } + rip[i] = 1; + nTrunkUsage += Align16(O.size / 8); + if(nTrunkUsage > nTrunkSize) + { + uprintf("overuse of trunk %lld\n", (long long)nTrunkUsage); + BREAK_ON_PATCH_FAIL(); + return false; + } + } + break; + } + } + } + if(rip[i]) + { + if(I.ops[0].type != O_REG) + { + uprintf("arg 0 should be O_REG, fail\n"); + BREAK_ON_PATCH_FAIL(); + return false; + } + if(I.ops[1].type != O_SMEM) + { + uprintf("arg 1 should be O_SMEM, fail was %d\n", O_SMEM); + BREAK_ON_PATCH_FAIL(); + return false; + } + } + int fc = META_GET_FC(Instructions[i].meta); + switch(fc) + { + case FC_CALL: + { + break; + } + case FC_RET: + case FC_SYS: + case FC_UNC_BRANCH: + case FC_CND_BRANCH: + uprintf("found branch inst %d :: %d\n", fc, offset); + BREAK_ON_PATCH_FAIL(); + return false; + } + offset += Instructions[i].size; + offsets[i + 1] = offset; + if(offset >= nLimit) + { + nInstructions = i + 1; + break; + } + } + if(nTrunkUsage > nTrunkSize) + { + uprintf("function using too much trunk space\n"); + BREAK_ON_PATCH_FAIL(); + return false; + } + if(offset < nLimit) + { + uprintf("function only had %d bytes of %d\n", offset, nLimit); + BREAK_ON_PATCH_FAIL(); + return false; + } + + if(0 == *pRetSafe && 0 == (nUsableJumpRegs & ~nRegsWritten)) + { + // if ret jump is unsafe all of the usable jump regs are taken, fail. + uprintf("cannot patch function without breaking code]\n"); + BREAK_ON_PATCH_FAIL(); + MP_BREAK(); + return false; + } + + // MP_BREAK(); + *pRegsWritten = nRegsWritten; + uint8_t* d = (uint8_t*)pDest; + uint8_t* dend = d + nMaxSize; + const uint8_t* s = (const uint8_t*)pSrc; + + nTrunkUsage = 0; + + for(i = 0; i < nInstructions; ++i) + { + auto& I = Instructions[i]; + unsigned size = Instructions[i].size; + if(I.opcode == I_CALL) + { + // find reg + uint32_t nRegsWritten = nRegsWrittenInstr[i]; + uint32_t nUsable = nUsableRegisters & ~nRegsWritten; + MP_ASSERT(nUsable); + int r = R_RAX; + while(0 == (1 & nUsable)) + { + nUsable >>= 1; + r++; + } + + intptr_t p = offsets[i + 1]; + p += (intptr_t)pSrc; + p += I.imm.sdword; + d = MicroProfileInsertMov(d, dend, r, p); + d = MicroProfileInsertCall(d, dend, r); + s += size; + } + else if(rip[i]) + { + if(I.opcode == I_LEA) + { + if(I.ops[0].type != O_REG) + { + MP_BREAK(); + } + if(I.ops[1].index != R_RIP) + { + MP_BREAK(); + } + int reg = I.ops[0].index - R_RAX; + int large = I.ops[0].index >= R_R8 ? 1 : 0; + *d++ = large ? 0x49 : 0x48; + *d++ = 0xb8 + (reg - (large ? (R_R8 - R_RAX) : 0)); + // calculate the offset + int64_t offset = offsets[i + 1] + I.disp; + intptr_t base = (intptr_t)pSrc; + + intptr_t sum = base + offset; + intptr_t* pAddress = (intptr_t*)d; + pAddress[0] = sum; + s += size; + d += 10; + d = (uint8_t*)(pAddress + 1); + } + else + { + if(15 & (intptr_t)pTrunk) + { + MP_BREAK(); + } + intptr_t t = (intptr_t)pTrunk; + t = (t + 15) & ~15; + pTrunk = (char*)t; + auto& O = I.ops[1]; + uint32_t Op1Size = O.size / 8; + + memcpy(d, s, size); + int32_t DispOriginal = (int32_t)I.disp; + const uint8_t* pOriginal = (s + size) + DispOriginal; + + intptr_t DispNew = ((uint8_t*)pTrunk - (d + size)); + if(!((intptr_t)pTrunk + Op1Size <= (intptr_t)pTrunkEnd)) + { + MP_BREAK(); + } + memcpy(pTrunk, pOriginal, Op1Size); + pTrunk += Align16(Op1Size); + if(I.dispSize == 32) + { + int32_t off = (int32_t)DispNew; + if(DispNew > 0x7fffffff || DispNew < 0) + { + MP_BREAK(); + } + memcpy(d + size - 4, &off, 4); + } + else if(I.dispSize == 16) + { + int16_t off = (int16_t)DispNew; + if(DispNew > 0x7fff || DispNew < 0) + { + MP_BREAK(); + } + memcpy(d + size - 2, &off, 2); + } + + d += size; + s += size; + } + } + else + { + memcpy(d, s, size); + d += size; + s += size; + } + } + + *pBytesDest = (int)(d - (uint8_t*)pDest); + + return true; +} + +extern "C" void MicroProfileInterceptEnter(int a) +{ + MicroProfileToken T = S.DynamicTokens[a]; + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog2(); + MP_ASSERT(pLog->nStackScope < MICROPROFILE_STACK_MAX); // if youre hitting this assert your instrumenting a deeply nested function + MicroProfileScopeStateC* pScopeState = &pLog->ScopeState[pLog->nStackScope++]; + pScopeState->Token = T; + if(T) + { + pScopeState->nTick = MicroProfileEnterInternal(T); + } + else + { + pScopeState->nTick = MICROPROFILE_INVALID_TICK; + } +} +extern "C" void MicroProfileInterceptLeave(int a) +{ + MicroProfileThreadLog* pLog = MicroProfileGetThreadLog2(); + MP_ASSERT(pLog->nStackScope > 0); // if youre hitting this assert you probably have mismatched _ENTER/_LEAVE markers + MicroProfileScopeStateC* pScopeState = &pLog->ScopeState[--pLog->nStackScope]; + MicroProfileLeaveInternal(pScopeState->Token, pScopeState->nTick); +} + +bool MicroProfileInstrumentFromAddressOnly(void* pFunction) +{ + MicroProfileSymbolDesc* pDesc = MicroProfileSymbolFindFuction(pFunction); + if(pDesc) + { + uprintf("Found function %p :: %s %s\n", (void*)pDesc->nAddress, pDesc->pName, pDesc->pShortName); + uint32_t nColor = MicroProfileColorFromString(pDesc->pName); + + return MicroProfileInstrumentFunction(pFunction, MicroProfileSymbolModuleGetString(pDesc->nModule), pDesc->pName, nColor); + } + else + { + uprintf("No Function Found %p\n", pFunction); + return false; + } +} + +template +void MicroProfileInstrumentScanForFunctionCalls(CB Callback, void* pFunction, size_t nFunctionSize) +{ + pFunction = MicroProfileX64FollowJump(pFunction); + const intptr_t nCodeLen = nFunctionSize; + const uint32_t nMaxInstructions = 15; + intptr_t nOffset = 0; + _DecodeType dt = Decode64Bits; + _DInst Instructions[15]; + _CodeInfo ci; + do + { + ci.code = nOffset + (uint8_t*)pFunction; + ci.codeLen = nCodeLen - nOffset; + ci.codeOffset = 0; + ci.dt = dt; + ci.features = DF_RETURN_FC_ONLY; + uint32_t nCount = 0; + uint32_t nOffsetNext = 0; + + int r = distorm_decompose(&ci, Instructions, nMaxInstructions, &nCount); + // uprintf("decomposed %d\n", nCount); + if(r != DECRES_SUCCESS && r != DECRES_MEMORYERR) + { + BREAK_ON_PATCH_FAIL(); + return; + } + if(nCount == 0) + { + // no instructions left + break; + } + // uprintf("instructions decoded %d %p ::\n", nCount, pFunction); + for(int i = 0; i < (int)nCount; ++i) + { + // rip[i] = 0; + auto& I = Instructions[i]; + // bool bHasRipReference = false; + if(I.addr < nOffsetNext) + { + MP_BREAK(); + } + nOffsetNext = I.addr + I.size; + if(I.opcode == I_CALL) + { + auto& O = I.ops[0]; + if(O.type != O_PC || O.size != 0x20) + { + uprintf("non immediate call encountered. cannot follow\n"); + BREAK_ON_PATCH_FAIL(); + continue; + } + intptr_t pDst = nOffset + (intptr_t)pFunction; + pDst += I.addr; + pDst += I.size; + pDst += I.imm.sdword; + + void* fFun1 = MicroProfileX64FollowJump((void*)pDst); + Callback(fFun1); + } + } + nOffset += nOffsetNext; + } while(nOffset < nCodeLen); +} + +void MicroProfileInstrumentFunctionsCalled(void* pFunction, const char* pModuleName, const char* pFunctionName, int nMinBytes, int nMaxCalls) +{ + pFunction = MicroProfileX64FollowJump(pFunction); + + MicroProfileSymbolDesc* pDesc = MicroProfileSymbolFindFuction(pFunction); + if(pDesc) + { + uprintf("instrumenting child functions %p %p :: %s :: %s\n", (void*)pDesc->nAddress, (void*)pDesc->nAddressEnd, pDesc->pName, pDesc->pShortName); + int a = 0; + (void)a; + } + else + { + uprintf("could not find symbol info\n"); + return; + } + + const intptr_t nCodeLen = (intptr_t)pDesc->nAddressEnd - (intptr_t)pDesc->nAddress; + + MicroProfilePatchBeginSuspend(); + int NumFunctionsInstrumented = 0; + auto Callback = [&NumFunctionsInstrumented, nMinBytes, nMaxCalls](void* pFunc) + { + MicroProfileSymbolDesc* pDesc = MicroProfileSymbolFindFuction(pFunc); + if(!pDesc) + return; + const char* pName = pDesc ? pDesc->pName : "??"; + intptr_t Size = pDesc->nAddressEnd - pDesc->nAddress; + if(nMinBytes == 0 || Size >= nMinBytes) + { + if(0 == nMaxCalls || NumFunctionsInstrumented < nMaxCalls) + { + uprintf("** func Instrumented, count %d, size %d %s\n", NumFunctionsInstrumented, Size, pName); + if(MicroProfileInstrumentFromAddressOnly(pFunc)) + { + ++NumFunctionsInstrumented; + } + } + else + { + uprintf("** func Skipped, count %d>=%d :: %s\n", NumFunctionsInstrumented, nMaxCalls, pName); + } + } + else + { + uprintf("** func Skipped, Size %d<%d :: %s\n", Size, nMinBytes, pName); + } + }; + MicroProfileInstrumentScanForFunctionCalls(Callback, pFunction, nCodeLen); + + MicroProfilePatchEndSuspend(); +} + +bool MicroProfileInstrumentFunction(void* pFunction, const char* pModuleName, const char* pFunctionName, uint32_t nColor) +{ + MicroProfilePatchBeginSuspend(); + struct ScopeExit + { + ~ScopeExit() + { + MicroProfilePatchEndSuspend(); + } + } dummy; + + MicroProfilePatchError Err; + if(S.DynamicTokenIndex == MICROPROFILE_MAX_DYNAMIC_TOKENS) + { + uprintf("instrument failing, out of dynamic tokens %d\n", S.DynamicTokenIndex); + return false; + } + for(uint32_t i = 0; i < S.DynamicTokenIndex; ++i) + { + if(S.FunctionsInstrumented[i] == pFunction) + { + uprintf("function %p already instrumented\n", pFunction); + return false; + } + } + if(MicroProfilePatchFunction(pFunction, S.DynamicTokenIndex, MicroProfileInterceptEnter, MicroProfileInterceptLeave, &Err)) + { + MicroProfileToken Tok = S.DynamicTokens[S.DynamicTokenIndex] = MicroProfileGetToken("PATCHED", pFunctionName, nColor, MicroProfileTokenTypeCpu, 0); + S.FunctionsInstrumented[S.DynamicTokenIndex] = pFunction; + S.FunctionsInstrumentedName[S.DynamicTokenIndex] = MicroProfileStringIntern(pFunctionName); + S.FunctionsInstrumentedModuleNames[S.DynamicTokenIndex] = MicroProfileStringIntern(pModuleName); + S.DynamicTokenIndex++; + + uint16_t nGroup = MicroProfileGetGroupIndex(Tok); + if(!MicroProfileGroupActive(nGroup)) + { + MicroProfileGroupSetEnabled(nGroup); + } +#if MICROPROFILE_WEBSERVER + MicroProfileWebSocketToggleTimer(MicroProfileGetTimerIndex(Tok)); +#endif + + return false; + } + else + { + bool bFound = false; + for(int i = 0; i < S.nNumPatchErrors; ++i) + { + if(Err.nCodeSize == S.PatchErrors[i].nCodeSize && 0 == memcmp(Err.Code, S.PatchErrors[i].Code, Err.nCodeSize)) + { + bFound = true; + break; + } + } + if(!bFound && S.nNumPatchErrors < MICROPROFILE_MAX_PATCH_ERRORS) + { + memcpy(&S.PatchErrors[S.nNumPatchErrors++], &Err, sizeof(Err)); + } + bFound = false; + for(int i = 0; i < S.nNumPatchErrorFunctions; ++i) + { + if(0 == strcmp(pFunctionName, S.PatchErrorFunctionNames[i])) + { + bFound = true; + } + } + if(!bFound && S.nNumPatchErrorFunctions < MICROPROFILE_MAX_PATCH_ERRORS) + { + S.PatchErrorFunctionNames[S.nNumPatchErrorFunctions++] = pFunctionName; + } + uprintf("interception fail!!\n"); + return false; + } +} + +void MicroProfileInstrumentPreInit(); +void MicroProfileSymbolInitializeInternal(); +void MicroProfileSymbolFreeDataInternal(); +void MicroProfileSymbolKickThread(); +void MicroProfileQueryJoinThread(); + +bool MicroProfileSymbolInitialize(bool bStartLoad, const char* pModuleName) +{ + if(!bStartLoad) + return S.SymbolState.nModuleLoadsFinished.load() != 0; + // int nRequests = 0; + { + MicroProfileScopeLock L(MicroProfileMutex()); + for(int i = 0; i < S.SymbolNumModules; ++i) + { + if(0 == pModuleName || 0 == strcmp(pModuleName, (const char*)S.SymbolModules[i].pBaseString)) + { + if(0 == S.SymbolModules[i].nModuleLoadRequested.exchange(1)) + { + S.SymbolState.nModuleLoadsRequested.fetch_add(1); + } + } + } + } + + // todo: unload modules + MicroProfileSymbolKickThread(); + return S.SymbolState.nModuleLoadsRequested.load() == S.SymbolState.nModuleLoadsFinished.load(); + + // if(S.SymbolState.nState == MICROPROFILE_SYMBOLSTATE_DEFAULT) + // { + // if(!bStartLoad) + // return false; + // { + // MicroProfileScopeLock L(MicroProfileMutex()); + // S.SymbolState.nState.store(MICROPROFILE_SYMBOLSTATE_LOADING); + // S.SymbolState.nSymbolsLoaded.store(0); + // } + // MicroProfileSymbolKickThread(); + // return false; + // } + // if(nRequests) + // { + // } + // if(S.SymbolState.nState.load() == MICROPROFILE_SYMBOLSTATE_DONE) + // { + // MicroProfileQueryJoinThread(); + // } + // if(S.SymbolState.nState == MICROPROFILE_SYMBOLSTATE_DONE && bStartLoad) + // { + // MicroProfileSymbolFreeDataInternal(); + // { + // MicroProfileScopeLock L(MicroProfileMutex()); + // S.SymbolState.nState.store(MICROPROFILE_SYMBOLSTATE_LOADING); + // S.SymbolState.nSymbolsLoaded.store(0); + // } + // MicroProfileSymbolKickThread(); + // return false; + + // } + // else + // { + // return S.SymbolState.nState == MICROPROFILE_SYMBOLSTATE_DONE; + // } +} + +void MicroProfileSymbolFreeDataInternal() +{ + { + uprintf("todod;....\n"); + MP_BREAK(); + // MP_ASSERT(S.SymbolState.nState == MICROPROFILE_SYMBOLSTATE_DONE); + + S.nNumPatchErrorFunctions = 0; + memset(S.PatchErrorFunctionNames, 0, sizeof(S.PatchErrorFunctionNames)); + + for(int i = 0; i < S.SymbolNumModules; ++i) + { + + while(S.SymbolModules[i].pSymbolBlock) + { + MicroProfileSymbolBlock* pBlock = S.SymbolModules[i].pSymbolBlock; + S.SymbolModules[i].pSymbolBlock = pBlock->pNext; + MP_FREE(pBlock); + MICROPROFILE_COUNTER_SUB("/MicroProfile/Symbols/Allocs", 1); + MICROPROFILE_COUNTER_SUB("/MicroProfile/Symbols/Memory", sizeof(MicroProfileSymbolBlock)); + } + } + memset(&S.SymbolModules[0], 0, sizeof(S.SymbolModules)); + memset(&S.SymbolModuleNameBuffer[0], 0, sizeof(S.SymbolModuleNameBuffer)); + S.SymbolModuleNameOffset = 0; + S.SymbolNumModules = 0; + } +} +#if STRING_MATCH_SIZE == 64 +int MicroProfileCharacterMaskCharIndex(char c) +{ + if(c >= 'A' && c <= 'Z') + c = 'a' + (c - 'A'); + // abcdefghijklmnopqrstuvwxyz + if(c >= 'a' && c <= 'z') + { + int b = c - 'a'; + return b; + } + if(c >= '0' && c <= '9') + { + int b = c - '0'; + return b + 26; + } + switch(c) + { + case ':': + return 37; + case ';': + return 38; + case '\\': + return 39; + case '\'': + return 40; + case '\"': + return 41; + case '/': + return 42; + case '{': + return 43; + case '}': + return 44; + case '(': + return 45; + case ')': + return 46; + case '[': + return 47; + case ']': + return 48; + case '<': + return 49; + case '>': + return 50; + case '.': + return 51; + case ',': + return 52; // special characters + case ' ': + return -1; // special characters + } + return 63; +} + +uint64_t MicroProfileCharacterMaskChar(char c) +{ + uint64_t nMask = 1; + int nIndex = MicroProfileCharacterMaskCharIndex(c); + if(nIndex == -1) + return 0; + return nMask << nIndex; +} + +#else +uint32_t MicroProfileCharacterMaskChar(char c) +{ + if(c >= 'A' && c <= 'Z') + c = 'a' + (c - 'A'); + // abcdefghijklmnopqrstuvwxyz + if(c >= 'a' && c <= 'z') + { + int b = c - 'a'; + b = MicroProfileMin(20, b); // squish the last together + // static int once = 0; + // if(0 == once) + //{ + // for(int i = 20; i < 28; ++i) + // { + // uprintf("char %d is %c\n", i, (char)('a' + i)); + // } + // once = 1; + //} + uint32_t v = 1; + return v << b; + } + if(c >= '0' && c <= '9') + { + int b = c - '0'; + b += 21; + if(b < 21 || b > 30) + MP_BREAK(); + return 1 << b; + } + switch(c) + { + case ':': + case ';': + case '\\': + case '\'': + case '\"': + case '/': + case '{': + case '}': + case '(': + case ')': + case '[': + case ']': + return 1u << 31; // special characters + case ' ': + return 0; + } + return 0; +} +int MicroProfileCharacterMaskCharIndex(char c) +{ + if(c >= 'A' && c <= 'Z') + c = 'a' + (c - 'A'); + // abcdefghijklmnopqrstuvwxyz + if(c >= 'a' && c <= 'z') + { + int b = c - 'a'; + b = MicroProfileMin(20, b); // squish the last together + static int once = 0; + if(0 == once) + { + for(int i = 20; i < 28; ++i) + { + uprintf("char %d is %c\n", i, (char)('a' + i)); + } + once = 1; + } + return b; + } + if(c >= '0' && c <= '9') + { + int b = c - '0'; + b += 21; + if(b < 21 || b > 30) + MP_BREAK(); + return b; + } + switch(c) + { + case ':': + case ';': + case '\\': + case '\'': + case '\"': + case '/': + case '{': + case '}': + case '(': + case ')': + case '[': + case ']': + return 31; // special characters + case ' ': + return -1; + } + return 1; +} +#endif + +uint_string_match MicroProfileCharacterMaskString(const char* pStr) +{ + uint_string_match nMask = 0; + char c = 0; + while(0 != (c = *pStr++)) + { + nMask |= MicroProfileCharacterMaskChar(c); + } + return nMask; +} + +void MicroProfileCharacterMaskString2(const char* pStr, MicroProfileStringMatchMask& M) +{ + uint_string_match nMask = 0; + char c = 0; + int nLast = -1; + while(0 != (c = *pStr++)) + { + nMask |= MicroProfileCharacterMaskChar(c); + int nIndex = MicroProfileCharacterMaskCharIndex(c); + if(nIndex >= 0 && nLast >= 0) + { + MP_ASSERT(nIndex < STRING_MATCH_SIZE); + M.M[nLast] |= 1llu << nIndex; + } + nLast = nIndex; + } + M.nMask |= nMask; +} + +bool MicroProfileCharacterMatch(const MicroProfileStringMatchMask& Block, const MicroProfileStringMatchMask& String) +{ + if(String.nMask != (Block.nMask & String.nMask)) + return false; + for(uint32_t i = 0; i < STRING_MATCH_SIZE; ++i) + { + if(String.M[i] != (Block.M[i] & String.M[i])) + return false; + } + return true; +} + +uint32_t MicroProfileSymbolGetModule(const char* pString, intptr_t nBaseAddr) +{ + + for(int i = 0; i < S.SymbolNumModules; ++i) + { + auto& M = S.SymbolModules[i]; + for(int j = 0; j < M.nNumExecutableRegions; ++j) + { + if(M.Regions[j].nBegin <= nBaseAddr && nBaseAddr < M.Regions[j].nEnd) + return i; + } + } + MP_BREAK(); // should never happen. + return 0; +} + +void MicroProfileSymbolMergeExecutableRegions() +{ + for(int i = 0; i < S.SymbolNumModules; ++i) + { + auto& M = S.SymbolModules[i]; + if(M.nNumExecutableRegions > 1) + { + std::sort(&M.Regions[0], &M.Regions[M.nNumExecutableRegions], [](const MicroProfileSymbolModuleRegion& l, const MicroProfileSymbolModuleRegion& r) { return l.nBegin < r.nBegin; }); + + int p = 0; + int g = 1; + while(g < M.nNumExecutableRegions) + { + if(M.Regions[p].nEnd == M.Regions[g].nBegin) + { + M.Regions[p].nEnd = M.Regions[g].nEnd; + g++; + } + else + { + ++p; + if(p != g) + M.Regions[p] = M.Regions[g]; + g++; + } + } + M.nNumExecutableRegions = p + 1; + } + } + for(int i = 0; i < S.SymbolNumModules; ++i) + { + auto& M = S.SymbolModules[i]; + uprintf("region %s %s\n", M.pTrimmedString, M.pBaseString); + for(int j = 0; j < M.nNumExecutableRegions; ++j) + uprintf("\t[%p-%p]\n", (void*)M.Regions[j].nBegin, (void*)M.Regions[j].nEnd); + } +} + +uint32_t MicroProfileSymbolInitModule(const char* pString_, intptr_t nAddrBegin, intptr_t nAddrEnd) +{ + const char* pString = MicroProfileStringInternSlash(pString_); + for(int i = 0; i < S.SymbolNumModules; ++i) + { + auto& M = S.SymbolModules[i]; + for(int j = 0; j < M.nNumExecutableRegions; ++j) + { + if(M.Regions[j].nBegin <= nAddrBegin && nAddrEnd < M.Regions[j].nEnd) + { + MP_ASSERT(pString == M.pBaseString); + return i; + } + } + } + + for(int i = 0; i < S.SymbolNumModules; ++i) + { + auto& M = S.SymbolModules[i]; + if(M.pBaseString == pString) + { + MP_ASSERT((intptr_t)pString != -2); + for(int j = 0; j < M.nNumExecutableRegions; ++j) + if(nAddrBegin == M.Regions[j].nBegin) + return i; + + if(M.nNumExecutableRegions == MICROPROFILE_MAX_MODULE_EXEC_REGIONS) + { + return (uint32_t)-1; + } + M.Regions[M.nNumExecutableRegions].nBegin = nAddrBegin; + M.Regions[M.nNumExecutableRegions].nEnd = nAddrEnd; + // uprintf("added module region %d %p %p %s \n", M.nNumExecutableRegions, (void*)nAddrBegin, (void*)nAddrEnd, pString); + M.nNumExecutableRegions++; + return i; + } + } + + MP_ASSERT((intptr_t)pString != -2); + // trim untill last path char + const char* pTrimmedString = pString; + + const char* pWork = pTrimmedString; + bool bLastSeperator = false; + while(*pWork != '\0') + { + if(bLastSeperator) + pTrimmedString = pWork; + bLastSeperator = *pWork == '\\' || *pWork == '/'; + + pWork++; + } + int nLen = (int)strlen(pTrimmedString) + 1; + // uprintf("STRING '%s' :: trimmedstring %s . len %d\n", pString, pTrimmedString, nLen); + + const char* pTrimmedIntern = MicroProfileStringIntern(pTrimmedString); + if(S.SymbolModuleNameOffset + nLen > MICROPROFILE_INSTRUMENT_MAX_MODULE_CHARS) + return 0; + memcpy(S.SymbolModuleNameOffset + &S.SymbolModuleNameBuffer[0], pTrimmedString, nLen); + + MP_ASSERT(S.SymbolNumModules < MICROPROFILE_INSTRUMENT_MAX_MODULES); + S.SymbolModules[S.SymbolNumModules].nModuleBase = nAddrBegin; + S.SymbolModules[S.SymbolNumModules].nMatchOffset = 0; + S.SymbolModules[S.SymbolNumModules].nStringOffset = S.SymbolModuleNameOffset; + S.SymbolModules[S.SymbolNumModules].pBaseString = (const char*)pString; + S.SymbolModules[S.SymbolNumModules].pTrimmedString = pTrimmedIntern; + S.SymbolModules[S.SymbolNumModules].Regions[0].nBegin = nAddrBegin; + S.SymbolModules[S.SymbolNumModules].Regions[0].nEnd = nAddrEnd; + S.SymbolModules[S.SymbolNumModules].nNumExecutableRegions = 1; + S.SymbolModules[S.SymbolNumModules].bDownloading = false; + S.SymbolModules[S.SymbolNumModules].nProgress = 0; + S.SymbolModules[S.SymbolNumModules].nProgressTarget = 0; + + S.SymbolModuleNameOffset += nLen; + return S.SymbolNumModules++; +} + +const char* MicroProfileSymbolModuleGetString(uint32_t nIndex) +{ + MP_ASSERT(S.SymbolNumModules > (int)nIndex); + return S.SymbolModules[nIndex].nStringOffset + &S.SymbolModuleNameBuffer[0]; +} + +bool MicroProfileSymbolIgnoreSymbol(const char* pName) +{ + if(strstr(pName, "MicroProfile")) + { +#if MICROPROFILE_INSTRUMENT_MICROPROFILE == 0 + return true; +#else + if(strstr(pName, "Log") || strstr(pName, "Scope") || strstr(pName, "Tick") || strstr(pName, "Enter") || strstr(pName, "Leave") || strstr(pName, "Thread") || strstr(pName, "Thread") || + strstr(pName, "Mutex")) // just for debugging: skip these so we can play around with the sample projects + { + return true; + } +#endif + } +#ifdef _WIN32 + if(pName[0] == '_' && pName[1] == '_') + return true; + if(strstr(pName, "__security_check_cookie") || strstr(pName, "_RTC_CheckStackVars") || strstr(pName, "__chkstk") || strstr(pName, "std::_Atomic") || strstr(pName, "_Init_thread_header") || + strstr(pName, "_Init_thread_footer")) + { + return true; + } +#endif + return false; +} + +void MicroProfileSymbolInitializeInternal() +{ + uprintf("Starting load...\n"); + MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileSymbolInitialize", MP_CYAN); + + auto AllocBlock = []() -> MicroProfileSymbolBlock* + { + MicroProfileSymbolBlock* pBlock = MP_ALLOC_OBJECT(MicroProfileSymbolBlock); + MICROPROFILE_COUNTER_ADD("/MicroProfile/Symbols/Allocs", 1); + MICROPROFILE_COUNTER_ADD("/MicroProfile/Symbols/Memory", sizeof(MicroProfileSymbolBlock)); + MICROPROFILE_COUNTER_CONFIG_ONCE("/MicroProfile/Symbols/Memory", MICROPROFILE_COUNTER_FORMAT_BYTES, 0, 0); + memset(pBlock, 0, sizeof(MicroProfileSymbolBlock)); + return pBlock; + }; + + auto SymbolCallback = [&](const char* pName, const char* pShortName, intptr_t nAddress, intptr_t nAddressEnd, uint32_t nModuleId) + { + MICROPROFILE_SCOPEI("microprofile", "SymbolCallback", MP_AUTO); + uint32_t nModule = nModuleId; + if(MicroProfileHashTableGetPtr(&S.SymbolModules[nModule].AddressToSymbol, (void*)nAddress)) + { + return; + } + char Demangled[1024]; + if(MicroProfileDemangleName(pName, Demangled, sizeof(Demangled))) + { + pName = &Demangled[0]; + pShortName = &Demangled[0]; + } + intptr_t delta = nAddressEnd - nAddress; + S.SymbolModules[nModule].nProgress = MicroProfileMax(delta, S.SymbolModules[nModule].nProgress); + S.nSymbolsDirty++; + + int nIgnoreSymbol = MicroProfileSymbolIgnoreSymbol(pName) ? 1 : 0; + + MicroProfileSymbolBlock* pActiveBlock = S.SymbolModules[nModule].pSymbolBlock; + if(!pActiveBlock) + { + pActiveBlock = AllocBlock(); + pActiveBlock->pNext = S.SymbolModules[nModule].pSymbolBlock; + S.SymbolModules[nModule].pSymbolBlock = pActiveBlock; + } + + if(pName == pShortName) + { + pShortName = 0; + } + uint32_t nLen = (uint32_t)strlen(pName) + 1; + + if(nLen > MICROPROFILE_INSTRUMENT_SYMBOLNAME_MAXLEN) + nLen = MICROPROFILE_INSTRUMENT_SYMBOLNAME_MAXLEN; + uint32_t nLenShort = (uint32_t)(pShortName ? 1 + strlen(pShortName) : 0); + if(nLenShort && nLenShort > MICROPROFILE_INSTRUMENT_SYMBOLNAME_MAXLEN) + nLenShort = MICROPROFILE_INSTRUMENT_SYMBOLNAME_MAXLEN; + uint32_t S0 = sizeof(MicroProfileSymbolDesc) * pActiveBlock->nNumSymbols; + uint32_t S1 = pActiveBlock->nNumChars; + uint32_t S3 = nLenShort + nLen + sizeof(MicroProfileSymbolDesc) + 64; + if(S0 + S1 + S3 >= MicroProfileSymbolBlock::ESIZE) + { + MicroProfileSymbolBlock* pNewBlock = AllocBlock(); + MP_ASSERT(pActiveBlock == S.SymbolModules[nModule].pSymbolBlock); + pNewBlock->pNext = pActiveBlock; + S.SymbolModules[nModule].pSymbolBlock = pNewBlock; + pActiveBlock = pNewBlock; + } + S0 = sizeof(MicroProfileSymbolDesc) * pActiveBlock->nNumSymbols; + S1 = pActiveBlock->nNumChars; + S3 = nLenShort + nLen + sizeof(MicroProfileSymbolDesc); + MP_ASSERT(S0 + S1 + S3 < MicroProfileSymbolBlock::ESIZE); + pActiveBlock->nNumChars += nLen; + char* pStr = &pActiveBlock->Chars[MicroProfileSymbolBlock::ESIZE - pActiveBlock->nNumChars - 1]; + memcpy(pStr, pName, nLen); + pStr[nLen - 1] = '\0'; + MicroProfileSymbolDesc& E = pActiveBlock->Symbols[pActiveBlock->nNumSymbols++]; + MicroProfileHashTableSetPtr(&S.SymbolModules[nModule].AddressToSymbol, (void*)nAddress, &E); + + E.pName = pStr; + E.nAddress = nAddress; + E.nAddressEnd = nAddressEnd; + E.nIgnoreSymbol = nIgnoreSymbol; + E.nModule = nModule; + if(pShortName && strlen(pShortName)) + { + pActiveBlock->nNumChars += nLenShort; + char* pStrShort = &pActiveBlock->Chars[MicroProfileSymbolBlock::ESIZE - pActiveBlock->nNumChars - 1]; + memcpy(pStrShort, pShortName, nLenShort); + pStrShort[nLenShort - 1] = '\0'; + E.pShortName = pStrShort; + } + else + { + E.pShortName = E.pName; + } +#define SYMDBG 0 +#if SYMDBG + uprintf("Got symbol %lld %lld %f .. %llx %llx %llx %s\n", + S.SymbolModules[nModule].nProgress, + S.SymbolModules[nModule].nProgressTarget, + S.SymbolModules[nModule].nProgressTarget ? float(S.SymbolModules[nModule].nProgress) / float(S.SymbolModules[nModule].nProgressTarget) : 0.f, + (int64_t)E.nAddress, + (int64_t)S.SymbolModules[nModule].nAddrBegin, + (int64_t)S.SymbolModules[nModule].nAddrEnd, + E.pName); + if(E.nAddress < (int64_t)S.SymbolModules[nModule].nAddrBegin || E.nAddress > (int64_t)S.SymbolModules[nModule].nAddrEnd) + { + MP_BREAK(); + } +#endif + E.nMask = MicroProfileCharacterMaskString(E.pShortName); + MicroProfileCharacterMaskString2(E.pShortName, pActiveBlock->MatchMask); + + pActiveBlock->nMask |= E.nMask; + MICROPROFILE_COUNTER_ADD("/MicroProfile/Symbols/Count", 1); + if(nIgnoreSymbol) + { + MICROPROFILE_COUNTER_ADD("/MicroProfile/Symbols/Ignored", 1); + } +#if SYMDBG + MicroProfileSleep(10); +#endif +#undef SYMDBG + + S.SymbolModules[nModule].nSymbolsLoaded.fetch_add(1); + S.nSymbolsDirty.exchange(1); + S.SymbolState.nSymbolsLoaded.fetch_add(1); + MP_ASSERT((intptr_t)E.pShortName >= (intptr_t)&E); // assert pointer arithmetic is correct. + }; + do + { + uint32_t nModuleLoad[MICROPROFILE_INSTRUMENT_MAX_MODULES]; + uint32_t nNumModulesRequested = 0; + for(int i = 0; i < S.SymbolNumModules; ++i) + { + if(S.SymbolModules[i].nModuleLoadRequested.load() != 0 && S.SymbolModules[i].nModuleLoadFinished.load() == 0) + { + nModuleLoad[nNumModulesRequested] = i; + S.SymbolModules[i].nProgress = 0; + MicroProfileHashTableInit(&S.SymbolModules[i].AddressToSymbol, 256, 64, MicroProfileHashTableComparePtr, MicroProfileHashTableHashPtr); + nNumModulesRequested++; + } + } + if(0 == nNumModulesRequested) + { + break; + } + MicroProfileIterateSymbols(SymbolCallback, nModuleLoad, nNumModulesRequested); + S.SymbolState.nModuleLoadsFinished.fetch_add(nNumModulesRequested); + for(uint32_t i = 0; i < nNumModulesRequested; ++i) + { + if(S.SymbolModules[nModuleLoad[i]].nModuleLoadRequested.load() == S.SymbolModules[nModuleLoad[i]].nModuleLoadFinished.load()) + { + S.SymbolModules[nModuleLoad[i]].nProgress = S.SymbolModules[nModuleLoad[i]].nProgressTarget; + S.nSymbolsDirty.exchange(1); + } + } + } while(1); +} + +MicroProfileSymbolDesc* MicroProfileSymbolFindFuction(void* pAddress) +{ + for(int i = 0; i < S.SymbolNumModules; ++i) + { + MicroProfileSymbolDesc* pDesc = nullptr; + if(MicroProfileHashTableGetPtr(&S.SymbolModules[i].AddressToSymbol, pAddress, &pDesc)) + { + if(0 == pDesc->nIgnoreSymbol) + return pDesc; + else + return nullptr; + } + } + return nullptr; +} + +#define MICROPROFILE_MAX_FILTER 32 +#define MICROPROFILE_MAX_QUERY_RESULTS 32 +#define MICROPROFILE_MAX_FILTER_STRING 1024 + +struct MicroProfileFunctionQuery +{ + MicroProfileFunctionQuery* pNext; + uint32_t nState; + const char* pFilterStrings[MICROPROFILE_MAX_FILTER]; + uint32_t nPatternLength[MICROPROFILE_MAX_FILTER]; + int nMaxFilter; + + uint32_t nModuleFilterMatch[MICROPROFILE_INSTRUMENT_MAX_MODULES]; // prematch the modules, so it can be skipped during search + uint32_t nMask[MICROPROFILE_MAX_FILTER]; // masks for subpatterns skipped + MicroProfileStringMatchMask MatchMask[MICROPROFILE_MAX_FILTER]; // masks for subpatterns skipped + + // results + MicroProfileSymbolDesc* Results[MICROPROFILE_MAX_QUERY_RESULTS]; + uint32_t nNumResults; + char FilterString[MICROPROFILE_MAX_FILTER_STRING]; + + uint32_t QueryId; +}; + +MicroProfileFunctionQuery* MicroProfileAllocFunctionQuery() +{ + MicroProfileScopeLock L(MicroProfileMutex()); + MicroProfileFunctionQuery* pQ = nullptr; + S.nNumQueryAllocated++; + if(S.pQueryFreeList != 0) + { + pQ = S.pQueryFreeList; + S.pQueryFreeList = pQ->pNext; + S.nNumQueryFree--; + } + else + { + pQ = MP_ALLOC_OBJECT(MicroProfileFunctionQuery); + MICROPROFILE_COUNTER_ADD("MicroProfile/Symbols/FunctionQuery", 1); + MICROPROFILE_COUNTER_ADD("MicroProfile/Symbols/FunctionQueryMem", sizeof(MicroProfileFunctionQuery)); + S.nNumQueryAllocated++; + } + memset(pQ, 0, sizeof(MicroProfileFunctionQuery)); + return pQ; +} +void MicroProfileFreeFunctionQuery(MicroProfileFunctionQuery* pQ) +{ + pQ->pNext = S.pQueryFreeList; + S.pQueryFreeList = pQ; +} + +void MicroProfileProcessQuery(MicroProfileFunctionQuery* pQuery) +{ + MicroProfileFunctionQuery& Q = *pQuery; + + int nBlocksTested = 0, nSymbolsTested = 0, nStringsTested = 0, nStringsTested0 = 0; + int nBlocks = 0; + // (void)nBlocksTested; + // (void)nSymbolsTested; + // (void)nStringsTested; + // (void)nStringsTested0; + // (void)nBlocks; + + int64_t t = MP_TICK(); + int64_t tt = 0; + + for(int i = 0; i < S.SymbolNumModules; ++i) + { + int nModule = i; + uint32_t nModuleMatchOffset = Q.nModuleFilterMatch[nModule]; + MicroProfileSymbolBlock* pSymbols = S.SymbolModules[nModule].pSymbolBlock; + + uint32_t nMaskQ = Q.nMask[nModuleMatchOffset]; + MicroProfileStringMatchMask& MatchMaskQ = Q.MatchMask[nModuleMatchOffset]; + { + while(pSymbols && 0 == S.pPendingQuery && Q.nNumResults < MICROPROFILE_MAX_QUERY_RESULTS) + { + + MICROPROFILE_SCOPEI("MicroProfile", "SymbolQueryLoop", MP_YELLOW); + nBlocks++; + if(MicroProfileCharacterMatch(pSymbols->MatchMask, MatchMaskQ)) + { + nBlocksTested++; + for(uint32_t i = 0; i < pSymbols->nNumSymbols && 0 == S.pPendingQuery && Q.nNumResults < MICROPROFILE_MAX_QUERY_RESULTS; ++i) + { + MicroProfileSymbolDesc& E = pSymbols->Symbols[i]; + if(0 == E.nIgnoreSymbol) + { + nSymbolsTested++; + if(nMaskQ == (nMaskQ & E.nMask)) + { + nStringsTested++; + MP_ASSERT((int)E.nModule < S.SymbolNumModules); + if(MicroProfileStringMatch(E.pShortName, nModuleMatchOffset, &Q.pFilterStrings[0], Q.nPatternLength, Q.nMaxFilter)) + { + if(Q.nNumResults < MICROPROFILE_MAX_QUERY_RESULTS) + { + + Q.Results[Q.nNumResults++] = &E; + if(Q.nNumResults == MICROPROFILE_MAX_QUERY_RESULTS) + tt = MP_TICK(); + } + } + if(Q.nNumResults < MICROPROFILE_MAX_QUERY_RESULTS) + nStringsTested0++; + } + } + } + } + pSymbols = pSymbols->pNext; + } + } + } + int64_t tend = MP_TICK(); + float ToMS = MicroProfileTickToMsMultiplierCpu(); + float TIME = (tend - t) * ToMS; + float TIME0 = (tt - t) * ToMS; + uprintf(" %6.3fms [%6.3f]: %5d/%5d blocks tested. %5d symbols %5d/%5d string compares\n", TIME, TIME0, nBlocksTested, nBlocks, nSymbolsTested, nStringsTested, nStringsTested0); +} + +void* MicroProfileQueryThread(void* p) +{ + MicroProfileOnThreadCreate("MicroProfileSymbolThread"); + { + while(1) + { + MicroProfileSleep(100); // todo:: use an event instead + MicroProfileScopeLock L(MicroProfileMutex()); + if(S.pPendingQuery != nullptr) + { + MICROPROFILE_SCOPEI("MicroProfile", "SymbolQuery", MP_WHEAT); + MicroProfileFunctionQuery* pQuery = S.pPendingQuery; + + MP_ASSERT(pQuery->QueryId > S.nQueryProcessed); + S.pPendingQuery = 0; + L.Unlock(); + + // uprintf("processing query %d\n", pQuery->QueryId); + MicroProfileProcessQuery(pQuery); + + L.Lock(); + S.nQueryProcessed = MicroProfileMax(pQuery->QueryId, S.nQueryProcessed); + + pQuery->pNext = S.pFinishedQuery; + S.pFinishedQuery = pQuery; + } + if(S.SymbolState.nModuleLoadsRequested.load() != S.SymbolState.nModuleLoadsFinished.load()) + { + L.Unlock(); + MicroProfileSymbolInitializeInternal(); + L.Lock(); + } + } + + S.SymbolThreadFinished = 1; + } + MicroProfileOnThreadExit(); + return 0; +} + +void MicroProfileQueryJoinThread() +{ + if(S.SymbolThreadFinished) + { + MicroProfileThreadJoin(&S.SymbolThread); + S.SymbolThreadFinished = 0; + S.SymbolThreadRunning = 0; + } +} +void MicroProfileSymbolKickThread() +{ + // MicroProfileQueryJoinThread(); + if(S.SymbolThreadRunning == 0) + { + S.SymbolThreadRunning = 1; + MicroProfileThreadStart(&S.SymbolThread, MicroProfileQueryThread); + } +} +#if MICROPROFILE_WEBSERVER +void MicroProfileSymbolSendFunctionNames(MpSocket Connection) +{ + if(S.WSFunctionsInstrumentedSent < S.DynamicTokenIndex) + { + MicroProfileWSPrintStart(Connection); + MicroProfileWSPrintf("{\"k\":\"%d\",\"v\":[", MSG_FUNCTION_NAMES); + bool bFirst = true; + for(uint32_t i = S.WSFunctionsInstrumentedSent; i < S.DynamicTokenIndex; ++i) + { + const char* pString = S.FunctionsInstrumentedName[i]; + const char* pModuleString = S.FunctionsInstrumentedModuleNames[i]; + MicroProfileWSPrintf(bFirst ? "[\"%s\",\"%s\",\"%s\"]" : ",[\"%s\",\"%s\",\"%s\"]", pString, pModuleString, "unused"); + bFirst = false; + } + MicroProfileWSPrintf("]}"); + MicroProfileWSFlush(); + MicroProfileWSPrintEnd(); + + S.WSFunctionsInstrumentedSent = S.DynamicTokenIndex; + } +} + +void MicroProfileSymbolSendErrors(MpSocket Connection) +{ + if(S.nNumPatchErrors) + { + MicroProfileWSPrintStart(Connection); + MicroProfileWSPrintf("{\"k\":\"%d\",\"v\":{\"version\":\"%d.%d\",\"data\":[", MSG_INSTRUMENT_ERROR, MICROPROFILE_MAJOR_VERSION, MICROPROFILE_MINOR_VERSION); + bool bFirst = true; + for(int i = 0; i < S.nNumPatchErrors; ++i) + { + MicroProfilePatchError& E = S.PatchErrors[i]; + (void)E; + if(!bFirst) + MicroProfileWSPrintf(","); + MicroProfileWSPrintf("{\"code\":\""); + for(int i = 0; i < E.nCodeSize; ++i) + MicroProfileWSPrintf("%02x", E.Code[i] & 0xff); + MicroProfileWSPrintf("\",\"message\":\"%s\",\"already\":%d}", &E.Message[0], E.AlreadyInstrumented); + bFirst = false; + } + + MicroProfileWSPrintf("],\"functions\":["); + bFirst = true; + for(int i = 0; i < S.nNumPatchErrorFunctions; ++i) + { + if(!bFirst) + MicroProfileWSPrintf(","); + + MicroProfileWSPrintf("\"%s\"", S.PatchErrorFunctionNames[i]); + + bFirst = false; + } + + MicroProfileWSPrintf("]}}"); + + MicroProfileWSFlush(); + MicroProfileWSPrintEnd(); + + S.nNumPatchErrors = 0; + S.nNumPatchErrorFunctions = 0; + } +} + +void MicroProfileSymbolQuerySendResult(MpSocket Connection) +{ + MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileSymbolQuerySendResult", MP_PINK2); + MicroProfileFunctionQuery* pQuery = 0; + { + MicroProfileScopeLock L(MicroProfileMutex()); + + uint32_t nBest = 0; + + while(S.pFinishedQuery != nullptr) + { + if(!pQuery) + { + pQuery = S.pFinishedQuery; + nBest = pQuery->QueryId; + S.pFinishedQuery = pQuery->pNext; + } + else + { + MicroProfileFunctionQuery* pQ = S.pFinishedQuery; + S.pFinishedQuery = pQ->pNext; + if(pQ->QueryId > nBest) + { + MicroProfileFreeFunctionQuery(pQuery); + nBest = pQ->QueryId; + pQuery = pQ; + } + else + { + MicroProfileFreeFunctionQuery(pQ); + } + } + } + } + + if(pQuery) + { + uprintf("Sending result for query %d\n", pQuery->QueryId); + MicroProfileWSPrintStart(Connection); + MicroProfileWSPrintf("{\"k\":\"%d\",\"q\":%d,\"v\":[", MSG_FUNCTION_RESULTS, pQuery->QueryId); + bool bFirst = true; + for(uint32_t i = 0; i < pQuery->nNumResults; ++i) + { + MicroProfileSymbolDesc& E = *pQuery->Results[i]; + if(bFirst) + { + MicroProfileWSPrintf("{\"a\":\"%p\",\"n\":\"%s\",\"sn\":\"%s\",\"m\":\"%s\"}", E.nAddress, E.pName, E.pShortName, MicroProfileSymbolModuleGetString(E.nModule)); + bFirst = false; + } + else + { + MicroProfileWSPrintf(",{\"a\":\"%p\",\"n\":\"%s\",\"sn\":\"%s\",\"m\":\"%s\"}", E.nAddress, E.pName, E.pShortName, MicroProfileSymbolModuleGetString(E.nModule)); + } + } + MicroProfileWSPrintf("]}"); + MicroProfileWSFlush(); + MicroProfileWSPrintEnd(); + + MicroProfileScopeLock L(MicroProfileMutex()); + MicroProfileFreeFunctionQuery(pQuery); + } +} +#endif + +void MicroProfileSymbolQueryFunctions(MpSocket Connection, const char* pFilter) +{ + MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileSymbolQueryFunctions", MP_WHEAT); + + if(!MicroProfileSymbolInitialize(false)) + { + return; + } + { + int QueryId = atoi(pFilter); + pFilter = strchr(pFilter, 'x'); + pFilter++; + MicroProfileScopeLock L(MicroProfileMutex()); + if(0 == S.pPendingQuery || S.pPendingQuery->QueryId < (uint32_t)QueryId) + { + MicroProfileFunctionQuery* pQuery = S.pPendingQuery; + if(!pQuery) + { + S.pPendingQuery = pQuery = MicroProfileAllocFunctionQuery(); + } + MP_ASSERT(pQuery->pNext == 0); + memset(pQuery, 0, sizeof(*pQuery)); + + MicroProfileFunctionQuery& Q = *pQuery; + Q.QueryId = QueryId; + + uint32_t nLen = (uint32_t)strlen(pFilter) + 1; + if(nLen >= MICROPROFILE_MAX_FILTER_STRING) + nLen = MICROPROFILE_MAX_FILTER_STRING - 1; + + memcpy(Q.FilterString, pFilter, nLen); + Q.FilterString[nLen] = '\0'; + + char* pBuffer = Q.FilterString; + bool bStartString = true; + for(uint32_t i = 0; i < nLen; ++i) + { + char c = pBuffer[i]; + if(c == '\0') + { + break; + } + if(isspace(c) || c == '*') + { + pBuffer[i] = '\0'; + bStartString = true; + } + else + { + if(bStartString) + { + if(Q.nMaxFilter < MICROPROFILE_MAX_FILTER) + { + const char* pstr = &pBuffer[i]; + Q.nMask[Q.nMaxFilter] = MicroProfileCharacterMaskString(pstr); + MicroProfileCharacterMaskString2(pstr, Q.MatchMask[Q.nMaxFilter]); + Q.pFilterStrings[Q.nMaxFilter++] = &pBuffer[i]; + } + } + bStartString = false; + } + } + memset(Q.nModuleFilterMatch, 0xff, sizeof(Q.nModuleFilterMatch)); + for(int i = 0; i < S.SymbolNumModules; ++i) + { + Q.nModuleFilterMatch[i] = MicroProfileStringMatchOffset(MicroProfileSymbolModuleGetString(i), Q.pFilterStrings, Q.nPatternLength, Q.nMaxFilter); + } + +#if 0 + uprintf("query %d::",QueryId); + for(int i = 0; i < Q.nMaxFilter; ++i) + { + Q.nPatternLength[i] = (uint32_t)strlen(Q.pFilterStrings[i]); + uprintf("'%s' ", Q.pFilterStrings[i]); + } + uprintf("\n"); +#endif + } + } + MicroProfileSymbolKickThread(); +} + +#if defined(_WIN32) +// '##::::'##::'#######:::'#######::'##:::'##::::'##:::::'##:'####:'##::: ##::'#######:::'#######:: +// ##:::: ##:'##.... ##:'##.... ##: ##::'##::::: ##:'##: ##:. ##:: ###:: ##:'##.... ##:'##.... ##: +// ##:::: ##: ##:::: ##: ##:::: ##: ##:'##:::::: ##: ##: ##:: ##:: ####: ##:..::::: ##:..::::: ##: +// #########: ##:::: ##: ##:::: ##: #####::::::: ##: ##: ##:: ##:: ## ## ##::'#######:::'#######:: +// ##.... ##: ##:::: ##: ##:::: ##: ##. ##:::::: ##: ##: ##:: ##:: ##. ####::...... ##:'##:::::::: +// ##:::: ##: ##:::: ##: ##:::: ##: ##:. ##::::: ##: ##: ##:: ##:: ##:. ###:'##:::: ##: ##:::::::: +// ##:::: ##:. #######::. #######:: ##::. ##::::. ###. ###::'####: ##::. ##:. #######:: #########: +// ..:::::..:::.......::::.......:::..::::..::::::...::...:::....::..::::..:::.......:::.........:: + +#ifdef _WIN32 +static void* MicroProfileAllocExecutableMemory(void* pBase, size_t s); +static void* MicroProfileAllocExecutableMemoryFar(size_t s); +static void MicroProfileMakeMemoryExecutable(void* p, size_t s); +static void MicroProfileMakeWriteable(void* p_, size_t size, DWORD* oldFlags); +static void MicroProfileRestore(void* p_, size_t size, DWORD* oldFlags); + +extern "C" void microprofile_tramp_enter_patch(); +extern "C" void microprofile_tramp_enter(); +extern "C" void microprofile_tramp_code_begin(); +extern "C" void microprofile_tramp_code_end(); +extern "C" void microprofile_tramp_intercept0(); +extern "C" void microprofile_tramp_end(); +extern "C" void microprofile_tramp_exit(); +extern "C" void microprofile_tramp_leave(); +extern "C" void microprofile_tramp_trunk(); +extern "C" void microprofile_tramp_call_patch_pop(); +extern "C" void microprofile_tramp_call_patch_push(); + +bool MicroProfilePatchFunction(void* f, int Argument, MicroProfileHookFunc enter, MicroProfileHookFunc leave, MicroProfilePatchError* pError) +{ + char* pOriginal = (char*)f; + + f = MicroProfileX64FollowJump(f); + if(MicroProfilePatchHasSuspendedThread((intptr_t)f, (intptr_t)f + 32)) + { + uprintf("failed to patch, thread running in patch position"); + return false; + } + intptr_t t_enter = (intptr_t)microprofile_tramp_enter; + intptr_t t_enter_patch_offset = (intptr_t)microprofile_tramp_enter_patch - t_enter; + intptr_t t_code_begin_offset = (intptr_t)microprofile_tramp_code_begin - t_enter; + intptr_t t_code_end_offset = (intptr_t)microprofile_tramp_code_end - t_enter; + intptr_t t_code_intercept0_offset = (intptr_t)microprofile_tramp_intercept0 - t_enter; + intptr_t t_code_exit_offset = (intptr_t)microprofile_tramp_exit - t_enter; + intptr_t t_code_leave_offset = (intptr_t)microprofile_tramp_leave - t_enter; + + intptr_t t_code_call_patch_push_offset = (intptr_t)microprofile_tramp_call_patch_push - t_enter; + intptr_t t_code_call_patch_pop_offset = (intptr_t)microprofile_tramp_call_patch_pop - t_enter; + intptr_t codemaxsize = t_code_end_offset - t_code_begin_offset; + intptr_t t_end_offset = (intptr_t)microprofile_tramp_end - t_enter; + intptr_t t_trunk_offset = (intptr_t)microprofile_tramp_trunk - t_enter; + int t_trunk_size = (int)((intptr_t)microprofile_tramp_end - (intptr_t)microprofile_tramp_trunk); + + char* ptramp = (char*)MicroProfileAllocExecutableMemory(f, t_end_offset); + if(!ptramp) + ptramp = (char*)MicroProfileAllocExecutableMemoryFar(t_end_offset); + + intptr_t offset = ((intptr_t)f + 6 - (intptr_t)ptramp); + + uint32_t nBytesToCopy = 14; + if(offset < 0x80000000 && offset > -0x7fffffff) + { + /// offset is small enough to insert a relative jump + nBytesToCopy = 5; + } + + memcpy(ptramp, (void*)t_enter, t_end_offset); + + int nInstructionBytesDest = 0; + char* pInstructionMoveDest = ptramp + t_code_begin_offset; + char* pTrunk = ptramp + t_trunk_offset; + + int nInstructionBytesSrc = 0; + uint32_t nRegsWritten = 0; + uint32_t nRetSafe = 1; + uint32_t nUsableJumpRegs = (1 << R_RAX) | (1 << R_R10) | (1 << R_R11); + static_assert(R_RAX == 0, "R_RAX must be 0"); + if(!MicroProfileCopyInstructionBytes( + pInstructionMoveDest, f, nBytesToCopy, (int)codemaxsize, pTrunk, t_trunk_size, nUsableJumpRegs, &nInstructionBytesDest, &nInstructionBytesSrc, &nRegsWritten, &nRetSafe)) + { + if(pError) + { + const char* pCode = (const char*)f; + memset(pError->Code, 0, sizeof(pError->Code)); + memcpy(pError->Code, pCode, nInstructionBytesSrc); + int off = stbsp_snprintf(pError->Message, sizeof(pError->Message), "Failed to move %d code bytes ", nInstructionBytesSrc); + pError->nCodeSize = nInstructionBytesSrc; + for(int i = 0; i < nInstructionBytesSrc; ++i) + { + off += stbsp_snprintf(off + pError->Message, sizeof(pError->Message) - off, "%02x ", 0xff & pCode[i]); + } + uprintf("%s\n", pError->Message); + } + return false; + } + + intptr_t phome = nInstructionBytesSrc + (intptr_t)f; + uint32_t reg = nUsableJumpRegs & ~nRegsWritten; + if(0 == reg) + { + if(0 == nRetSafe) + MP_BREAK(); // should be caught earlier + MicroProfileInsertRetJump(pInstructionMoveDest + nInstructionBytesDest, phome); + } + else + { + int r = R_RAX; + while((reg & 1) == 0) + { + reg >>= 1; + r++; + } + MicroProfileInsertRegisterJump(pInstructionMoveDest + nInstructionBytesDest, phome, r); + } + + // PATCH 1 TRAMP EXIT + intptr_t microprofile_tramp_exit = (intptr_t)ptramp + t_code_exit_offset; + memcpy(ptramp + t_enter_patch_offset + 2, (void*)µprofile_tramp_exit, 8); + + char* pintercept = t_code_intercept0_offset + ptramp; + + // PATCH 1.5 Argument + memcpy(pintercept - 4, (void*)&Argument, 4); + + // PATCH 2 INTERCEPT0 + intptr_t addr = (intptr_t)enter; //&intercept0; + memcpy(pintercept + 2, (void*)&addr, 8); + + // PATHC 2.5 argument + memcpy(ptramp + t_code_exit_offset + 3, (void*)&Argument, 4); + + intptr_t microprofile_tramp_leave = (intptr_t)ptramp + t_code_leave_offset; + // PATCH 3 INTERCEPT1 + intptr_t addr1 = (intptr_t)leave; //&intercept1; + memcpy((char*)microprofile_tramp_leave + 2, (void*)&addr1, 8); + + intptr_t patch_push_addr = (intptr_t)(&MicroProfile_Patch_TLS_PUSH); + intptr_t patch_pop_addr = (intptr_t)(&MicroProfile_Patch_TLS_POP); + memcpy((char*)ptramp + t_code_call_patch_push_offset + 2, &patch_push_addr, 8); + memcpy((char*)ptramp + t_code_call_patch_pop_offset + 2, &patch_pop_addr, 8); + MicroProfileMakeMemoryExecutable(ptramp, t_end_offset); + + { + // PATCH 4 DEST FUNC + + DWORD OldFlags[2] = { 0 }; + MicroProfileMakeWriteable(f, nInstructionBytesSrc, OldFlags); + char* pp = (char*)f; + char* ppend = pp + nInstructionBytesSrc; + + if(nInstructionBytesSrc < 14) + { + pp = MicroProfileInsertRelativeJump((char*)pp, (intptr_t)ptramp); + } + else + { + pp = MicroProfileInsertRegisterJump((char*)pp, (intptr_t)ptramp, R_RAX); + } + + while(pp != ppend) + { + char c = (unsigned char)0x90; + MP_ASSERT((unsigned char)c == (unsigned char)0x90); + *pp++ = (unsigned char)0x90; + } + MicroProfileRestore(f, nInstructionBytesSrc, OldFlags); + } + return true; +} + +static void MicroProfileMakeWriteable(void* p_, size_t s, DWORD* oldFlags) +{ + static uint64_t nPageSize = 4 << 10; + + intptr_t aligned = (intptr_t)p_; + aligned = (aligned & (~(nPageSize - 1))); + intptr_t aligned_end = (intptr_t)p_; + aligned_end += s; + aligned_end = (aligned_end + nPageSize - 1) & (~(nPageSize - 1)); + uint32_t nNumPages = (uint32_t)((aligned_end - aligned) / nPageSize); + MP_ASSERT(nNumPages >= 1 && nNumPages <= 2); + for(uint32_t i = 0; i < nNumPages; ++i) + { + if(!VirtualProtect((void*)(aligned + nPageSize * i), nPageSize, PAGE_EXECUTE_READWRITE, oldFlags + i)) + { + MP_BREAK(); + } + } + //*(unsigned char*)p_ = 0x90; +} + +static void MicroProfileRestore(void* p_, size_t s, DWORD* oldFlags) +{ + static uint64_t nPageSize = 4 << 10; + + intptr_t aligned = (intptr_t)p_; + aligned = (aligned & (~(nPageSize - 1))); + intptr_t aligned_end = (intptr_t)p_; + aligned_end += s; + aligned_end = (aligned_end + nPageSize - 1) & (~(nPageSize - 1)); + uint32_t nNumPages = (uint32_t)((aligned_end - aligned) / nPageSize); + DWORD Dummy; + for(uint32_t i = 0; i < nNumPages; ++i) + { + if(!VirtualProtect((void*)(aligned + nPageSize * i), nPageSize, oldFlags[i], &Dummy)) + { + MP_BREAK(); + } + } +} + +void* MicroProfileAllocExecutableMemoryUp(intptr_t nBase, size_t s, uint32_t RegionIndex) +{ + SYSTEM_INFO si; + GetSystemInfo(&si); + size_t Granularity = si.dwAllocationGranularity << 1; + nBase = (nBase / Granularity) * Granularity; + intptr_t nEnd = nBase + 0x80000000; + + for(uint32_t i = RegionIndex; i < S.MemoryRegions.Size; i++) + { + // try and allocate 2x before + nBase = S.MemoryRegions[i].Start + S.MemoryRegions[i].Size + Granularity; + nBase = (nBase / Granularity) * Granularity; + + if(nBase >= nEnd) + break; + void* pMemory = VirtualAlloc((void*)nBase, s, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if(pMemory) + { + return pMemory; + } + } + return nullptr; +} + +static void MicroProfileUpdateMemoryRegions() +{ + MicroProfileArrayClear(S.MemoryRegions); + SYSTEM_INFO si; + GetSystemInfo(&si); + + BYTE* Addr = (BYTE*)si.lpMinimumApplicationAddress; + BYTE* MaxAddr = (BYTE*)si.lpMaximumApplicationAddress; + // uprintf("updating memory regions\n"); + uint32_t idx = 0; + (void)idx; + while(Addr < MaxAddr) + { + MEMORY_BASIC_INFORMATION mbi; + SIZE_T Result = VirtualQuery(Addr, &mbi, sizeof(mbi)); + if(Result == 0) + break; + MicroProfileInstrumentMemoryRegion region; + region.Start = (intptr_t)mbi.BaseAddress; + region.Size = (intptr_t)mbi.RegionSize; + MicroProfileArrayPushBack(S.MemoryRegions, region); + // uprintf("Memory Region %d: %p(%p) %p .. State=%08x Protect=%08x Type=%08x\n", idx++, mbi.BaseAddress, mbi.AllocationBase, (intptr_t)mbi.BaseAddress + mbi.RegionSize, mbi.State, mbi.Protect, + // mbi.Type); + Addr = (BYTE*)mbi.BaseAddress + mbi.RegionSize; + } + uprintf("Iterated %d regions\n", S.MemoryRegions.Size); +} + +static void* MicroProfileAllocExecutableMemoryDown(intptr_t nBase, size_t s, uint32_t RegionIndex) +{ + SYSTEM_INFO si; + GetSystemInfo(&si); + size_t Granularity = si.dwAllocationGranularity << 1; + intptr_t nEnd = nBase - 0x80000000; + + for(int32_t i = RegionIndex; i >= 0; i--) + { + // try and allocate 2x before + nBase = S.MemoryRegions[i].Start - Granularity; + nBase = (nBase / Granularity) * Granularity; + if(nBase < nEnd) + break; + void* pMemory = VirtualAlloc((void*)nBase, s, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if(pMemory) + { + return pMemory; + } + } + return nullptr; +} + +static void* MicroProfileAllocExecutableMemory(void* pBase, size_t s) +{ + uint32_t RegionIndex = 0; + for(uint32_t i = 0; i < S.MemoryRegions.Size; ++i) + { + auto& R = S.MemoryRegions[i]; + if(R.Start <= (intptr_t)pBase && (intptr_t)pBase < R.Start + R.Size) + { + RegionIndex = i; + break; + } + } + + s = (s + 4095) & ~(4095); + intptr_t nBase = (intptr_t)pBase; + void* pResult = 0; + if(0 == pResult && nBase > 0x40000000) + { + pResult = MicroProfileAllocExecutableMemoryDown(nBase - 0x40000000, s, RegionIndex); + if(0 == pResult) + { + pResult = MicroProfileAllocExecutableMemoryUp(nBase - 0x40000000, s, RegionIndex); + } + } + if(0 == pResult && nBase < 0xffffffff40000000) + { + pResult = MicroProfileAllocExecutableMemoryUp(nBase + 0x40000000, s, RegionIndex); + if(0 == pResult) + { + pResult = MicroProfileAllocExecutableMemoryUp(nBase + 0x40000000, s, RegionIndex); + } + } + return pResult; +} +static void* MicroProfileAllocExecutableMemoryFar(size_t s) +{ + static uint64_t nPageSize = 4 << 10; + s = (s + (nPageSize - 1)) & (~(nPageSize - 1)); + + void* pMem = VirtualAlloc(0, s, MEM_COMMIT, PAGE_READWRITE); + MP_ASSERT(pMem); + + // uprintf("Allocating %zu %p\n", s, pMem); + return pMem; +} +static void MicroProfileMakeMemoryExecutable(void* p, size_t s) +{ + static uint64_t nPageSize = 4 << 10; + s = (s + (nPageSize - 1)) & (~(nPageSize - 1)); + DWORD Unused; + if(!VirtualProtect(p, s, PAGE_EXECUTE_READ, &Unused)) + { + MP_BREAK(); + } +} +#endif + +int MicroProfileTrimFunctionName(const char* pStr, char* pOutBegin, char* pOutEnd) +{ + const char* pStart = pOutBegin; + int l = (int)strlen(pStr) - 1; + int sz = 0; + pOutEnd--; + if(l < 1024 && pOutBegin != pOutEnd) + { + const char* p = pStr; + const char* pEnd = pStr + l + 1; + int in = 0; + while(p != pEnd && pOutBegin != pOutEnd) + { + char c = *p++; + if(c == '(' || c == '<') + { + in++; + } + else if(c == ')' || c == '>') + { + in--; + continue; + } + + if(in == 0) + { + *pOutBegin++ = c; + sz++; + } + } + + *pOutBegin++ = '\0'; + } + return sz; +} + +int MicroProfileFindFunctionName(const char* pStr, const char** ppStart) +{ + int l = (int)strlen(pStr) - 1; + if(l < 1024) + { + char b[1024] = { 0 }; + char* put = &b[0]; + + const char* p = pStr; + const char* pEnd = pStr + l + 1; + int in = 0; + while(p != pEnd) + { + char c = *p++; + if(c == '(' || c == '<') + { + in++; + } + else if(c == ')' || c == '>') + { + in--; + continue; + } + + if(in == 0) + { + *put++ = c; + } + } + + *put++ = '\0'; + uprintf("trimmed %s\n", b); + } + + // int nFirstParen = l; + int nNumParen = 0; + int c = 0; + + while(l >= 0 && pStr[l] != ')' && c++ < sizeof(" const") - 1) + { + l--; + } + if(pStr[l] == ')') + { + do + { + if(pStr[l] == ')') + { + nNumParen++; + } + else if(pStr[l] == '(') + { + nNumParen--; + } + l--; + } while(nNumParen > 0 && l >= 0); + } + else + { + *ppStart = pStr; + return 0; + } + while(l >= 0 && isspace(pStr[l])) + { + --l; + } + int nLast = l; + while(l >= 0 && !isspace(pStr[l])) + { + l--; + } + int nFirst = l; + if(nFirst == nLast) + return 0; + int nCount = nLast - nFirst + 1; + *ppStart = pStr + nFirst; + return nCount; +} + +#include +#include +#include +#include +struct MicroProfileQueryContext +{ + + const char* pFilterStrings[MICROPROFILE_MAX_FILTER]; + uint32_t nPatternLength[MICROPROFILE_MAX_FILTER]; + int nMaxFilter = 0; + char TempBuffer[128]; + uint32_t size = 0; + bool bFirst = false; +}; + +BOOL CALLBACK MicroProfileEnumModules(_In_ PCTSTR ModuleName, _In_ DWORD64 BaseOfDll, _In_opt_ PVOID UserContext) +{ + + MODULEINFO MI; + GetModuleInformation(GetCurrentProcess(), (HMODULE)BaseOfDll, &MI, sizeof(MI)); + MEMORY_BASIC_INFORMATION B; + int r = VirtualQuery((LPCVOID)BaseOfDll, (MEMORY_BASIC_INFORMATION*)&B, sizeof(B)); + char buffer[1024]; + int r1 = GetLastError(); + if(r == 0) + { + stbsp_snprintf(buffer, sizeof(buffer) - 1, "Error %d\n", r1); + OutputDebugString(buffer); + MP_BREAK(); + } + MicroProfileSymbolInitModule(ModuleName, BaseOfDll, BaseOfDll + MI.SizeOfImage); + return true; +} + +namespace +{ +struct QueryCallbackBase // fucking c++, this is a pain in the ass +{ + virtual void CB(const char* pName, const char* pShortName, intptr_t addr, intptr_t addrend, uint32_t nModuleId) = 0; +}; +template +struct QueryCallbackImpl : public QueryCallbackBase +{ + T t; + QueryCallbackImpl(T t) + : t(t) + { + } + virtual void CB(const char* pName, const char* pShortName, intptr_t addr, intptr_t addrend, uint32_t nModuleId) + { + t(pName, pShortName, addr, addrend, nModuleId); + } +}; +} // namespace + +static uint32_t nLastModuleIdWin32 = (uint32_t)-1; +static intptr_t nLastModuleBaseWin32 = (intptr_t)-1; + +BOOL MicroProfileQueryContextEnumSymbols(_In_ PSYMBOL_INFO pSymInfo, _In_ ULONG SymbolSize, _In_opt_ PVOID UserContext) +{ + uint32_t nModuleId = nLastModuleIdWin32; + if(nLastModuleBaseWin32 != (intptr_t)pSymInfo->ModBase) + { + nLastModuleIdWin32 = nModuleId = MicroProfileSymbolGetModule((const char*)(intptr_t)-2, pSymInfo->ModBase); + nLastModuleBaseWin32 = (intptr_t)pSymInfo->ModBase; + } + + if(pSymInfo->Tag == 5 || pSymInfo->Tag == 10) + { + + char FunctionName[1024]; + int ret = 0; + int l = MicroProfileTrimFunctionName(pSymInfo->Name, &FunctionName[0], &FunctionName[1024]); + QueryCallbackBase* pCB = (QueryCallbackBase*)UserContext; + + pCB->CB(pSymInfo->Name, l ? &FunctionName[0] : 0, (intptr_t)pSymInfo->Address, pSymInfo->Size + (intptr_t)pSymInfo->Address, nModuleId); + } + return TRUE; +}; + +bool MicroProfileDemangleName(const char* pName, char* OutName, uint32_t Size) +{ + MICROPROFILE_SCOPEI("microprofile", "SymbolDemangle", MP_AUTO); + if(UnDecorateSymbolName(pName, OutName, Size, UNDNAME_NAME_ONLY)) + { + return true; + } + return false; +} + +bool MicroProfileExtractPdbInfo(HMODULE hMod, GUID& guid, DWORD& age, char pdbName[MAX_PATH]) +{ + struct CV_INFO_PDB70 + { + DWORD CvSignature; // "RSDS" + GUID Signature; // GUID + DWORD Age; // Age + char PdbFileName[1]; // Null-terminated string + }; + + BYTE* base = (BYTE*)hMod; + IMAGE_DOS_HEADER* dos = (IMAGE_DOS_HEADER*)base; + if(dos->e_magic != IMAGE_DOS_SIGNATURE) + return false; + IMAGE_NT_HEADERS* nt = (IMAGE_NT_HEADERS*)(base + dos->e_lfanew); + if(nt->Signature != IMAGE_NT_SIGNATURE) + return false; + auto& dd = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG]; + if(!dd.VirtualAddress || !dd.Size) + return false; + IMAGE_DEBUG_DIRECTORY* debugDir = (IMAGE_DEBUG_DIRECTORY*)(base + dd.VirtualAddress); + int count = dd.Size / sizeof(IMAGE_DEBUG_DIRECTORY); + for(int i = 0; i < count; i++) + { + if(debugDir[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW) + { + auto cv = (CV_INFO_PDB70*)(base + debugDir[i].AddressOfRawData); + if(cv->CvSignature != 'SDSR') + continue; // "RSDS" + guid = cv->Signature; + age = cv->Age; + strcpy_s(pdbName, MAX_PATH, cv->PdbFileName); + return true; + } + } + return false; +} + +bool MicroProfileDownloadPDB(HMODULE Module, HANDLE Process, char outPath[MAX_PATH]) +{ + GUID guid; + DWORD age; + char pdbName[MAX_PATH]; + if(!MicroProfileExtractPdbInfo(Module, guid, age, pdbName)) + { + uprintf("Failed to download pdb\n"); + MP_BREAK(); + return false; + } + uprintf("pdb name %s age %d\n", pdbName, age); + + FILE* f = fopen(pdbName, "r"); + if(f) + { + fclose(f); + strcpy_s(outPath, MAX_PATH, pdbName); + return true; + } + char localPath[MAX_PATH] = {}; + BOOL ok = SymFindFileInPath(Process, + NULL, + pdbName, + (PVOID)&guid, // GUID + age, // Age + 0, // FileSize (not used for PDBs) + SSRVOPT_GUIDPTR, // we're passing GUID pointer + outPath, + NULL, + NULL); + return ok != 0; +} + +#include "PDB.h" +#include "PDB_DBIStream.h" +#include "PDB_IPIStream.h" +#include "PDB_InfoStream.h" +#include "PDB_NamesStream.h" +#include "PDB_RawFile.h" +#include "PDB_TPIStream.h" + +template +void MicroProfileLoadRawPDB(Callback CB, const char* Filename, uint64_t Base, uint32_t nModuleId) +{ + auto OnSymbol = [CB, Base, nModuleId](const char* Sym, uint32_t Offset, uint32_t Size) + { + char FunctionName[1024]; + int ret = 0; + int l = MicroProfileTrimFunctionName(Sym, &FunctionName[0], &FunctionName[1024]); + const char* fname = l ? &FunctionName[0] : nullptr; + CB(Sym, fname, (intptr_t)Offset + Base, (intptr_t)Offset + Base + Size, nModuleId); + }; + + void* File = CreateFileA(Filename, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, nullptr); + + if(File == INVALID_HANDLE_VALUE) + { + MP_BREAK(); + } + + void* FileMapping = CreateFileMappingA(File, nullptr, PAGE_READONLY, 0, 0, nullptr); + + if(FileMapping == nullptr) + { + CloseHandle(File); + MP_BREAK(); + } + + void* BaseAddress = MapViewOfFile(FileMapping, FILE_MAP_READ, 0, 0, 0); + + if(BaseAddress == nullptr) + { + CloseHandle(FileMapping); + CloseHandle(File); + } + + BY_HANDLE_FILE_INFORMATION FileInformation; + const bool GetInformationResult = GetFileInformationByHandle(File, &FileInformation); + if(!GetInformationResult) + { + UnmapViewOfFile(BaseAddress); + CloseHandle(FileMapping); + CloseHandle(File); + + MP_BREAK(); + } + + const size_t FileSizeHighBytes = static_cast(FileInformation.nFileSizeHigh) << 32; + const size_t FileSizeLowBytes = FileInformation.nFileSizeLow; + const size_t FileSize = FileSizeHighBytes | FileSizeLowBytes; + + const PDB::RawFile RawPdbFile = PDB::CreateRawFile(BaseAddress); + if(PDB::HasValidDBIStream(RawPdbFile) != PDB::ErrorCode::Success) + { + MP_BREAK(); + } + const PDB::InfoStream InfoStream(RawPdbFile); + if(InfoStream.UsesDebugFastLink()) + { + MP_BREAK(); + } + + // const PDB::Header* h = InfoStream.GetHeader(); + // uprintf("Version %u, signature %u, age %u, GUID %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n", + // static_cast(h->version), h->signature, h->age, + // h->guid.Data1, h->guid.Data2, h->guid.Data3, + // h->guid.Data4[0], h->guid.Data4[1], h->guid.Data4[2], h->guid.Data4[3], h->guid.Data4[4], h->guid.Data4[5], h->guid.Data4[6], h->guid.Data4[7]); + + const PDB::DBIStream DbiStream = PDB::CreateDBIStream(RawPdbFile); + if(PDB::ErrorCode::Success != DbiStream.HasValidSymbolRecordStream(RawPdbFile)) + { + MP_BREAK(); + } + + if(PDB::ErrorCode::Success != DbiStream.HasValidPublicSymbolStream(RawPdbFile)) + { + MP_BREAK(); + } + + if(PDB::ErrorCode::Success != DbiStream.HasValidGlobalSymbolStream(RawPdbFile)) + { + MP_BREAK(); + } + + if(PDB::ErrorCode::Success != DbiStream.HasValidSectionContributionStream(RawPdbFile)) + { + MP_BREAK(); + } + + if(PDB::ErrorCode::Success != DbiStream.HasValidImageSectionStream(RawPdbFile)) + { + MP_BREAK(); + } + + const PDB::ImageSectionStream ImageSectionStream = DbiStream.CreateImageSectionStream(RawPdbFile); + const PDB::ModuleInfoStream ModuleInfoStream = DbiStream.CreateModuleInfoStream(RawPdbFile); + const PDB::CoalescedMSFStream SymbolRecordStream = DbiStream.CreateSymbolRecordStream(RawPdbFile); + + const PDB::ArrayView modules = ModuleInfoStream.GetModules(); + + for(const PDB::ModuleInfoStream::Module& module : modules) + { + if(!module.HasSymbolStream()) + { + continue; + } + + const PDB::ModuleSymbolStream moduleSymbolStream = module.CreateSymbolStream(RawPdbFile); + moduleSymbolStream.ForEachSymbol( + [&ImageSectionStream, &OnSymbol](const PDB::CodeView::DBI::Record* record) + { + // only grab function symbols from the module streams + const char* name = nullptr; + uint32_t rva = 0u; + uint32_t size = 0u; + if(record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_FRAMEPROC) + { + // functionSymbols[functionSymbols.size() - 1].frameProc = record; + return; + } + else if(record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_THUNK32) + { + if(record->data.S_THUNK32.thunk == PDB::CodeView::DBI::ThunkOrdinal::TrampolineIncremental) + { + // we have never seen incremental linking thunks stored inside a S_THUNK32 symbol, but better safe than sorry + name = "ILT"; + rva = ImageSectionStream.ConvertSectionOffsetToRVA(record->data.S_THUNK32.section, record->data.S_THUNK32.offset); + size = 5u; + } + } + else if(record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_TRAMPOLINE) + { + // incremental linking thunks are stored in the linker module + name = "ILT"; + rva = ImageSectionStream.ConvertSectionOffsetToRVA(record->data.S_TRAMPOLINE.thunkSection, record->data.S_TRAMPOLINE.thunkOffset); + size = 5u; + } + else if(record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LPROC32) + { + name = record->data.S_LPROC32.name; + rva = ImageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LPROC32.section, record->data.S_LPROC32.offset); + size = record->data.S_LPROC32.codeSize; + } + else if(record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GPROC32) + { + name = record->data.S_GPROC32.name; + rva = ImageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GPROC32.section, record->data.S_GPROC32.offset); + size = record->data.S_GPROC32.codeSize; + } + else if(record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LPROC32_ID) + { + name = record->data.S_LPROC32_ID.name; + rva = ImageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LPROC32_ID.section, record->data.S_LPROC32_ID.offset); + size = record->data.S_LPROC32_ID.codeSize; + } + else if(record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GPROC32_ID) + { + name = record->data.S_GPROC32_ID.name; + rva = ImageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GPROC32_ID.section, record->data.S_GPROC32_ID.offset); + size = record->data.S_GPROC32_ID.codeSize; + } + + if(rva == 0u) + { + return; + } + // uprintf("func %p / %d .. %s \n", rva, size, name); + OnSymbol(name, rva, size); + }); + } + const PDB::PublicSymbolStream PublicSymbolStream = DbiStream.CreatePublicSymbolStream(RawPdbFile); + { + const PDB::ArrayView HashRecords = PublicSymbolStream.GetRecords(); + const size_t Count = HashRecords.GetLength(); + + for(const PDB::HashRecord& HashRecord : HashRecords) + { + const PDB::CodeView::DBI::Record* Record = PublicSymbolStream.GetRecord(SymbolRecordStream, HashRecord); + if(Record->header.kind != PDB::CodeView::DBI::SymbolRecordKind::S_PUB32) + { + continue; + } + + if((PDB_AS_UNDERLYING(Record->data.S_PUB32.flags) & PDB_AS_UNDERLYING(PDB::CodeView::DBI::PublicSymbolFlags::Function)) == 0u) + { + continue; + } + + const uint32_t rva = ImageSectionStream.ConvertSectionOffsetToRVA(Record->data.S_PUB32.section, Record->data.S_PUB32.offset); + if(rva == 0u) + { + continue; + } + OnSymbol(Record->data.S_PUB32.name, rva, 0); + } + } + UnmapViewOfFile(BaseAddress); + CloseHandle(FileMapping); + CloseHandle(File); +} + +bool MicroProfilePatchHasSuspendedThread(intptr_t Begin, intptr_t End) +{ + MicroProfileSuspendState& State = S.SuspendState; + for(uint32_t i = 0; i < State.NumSuspended; ++i) + { + intptr_t ip = State.SuspendedIP[i]; + if(Begin <= ip && ip <= End) + return true; + } + return false; +} + +bool MicroProfilePatchBeginSuspend() +{ + MicroProfileSuspendState& State = S.SuspendState; + + if(State.SuspendCounter++ > 0) + return true; + MicroProfileUpdateMemoryRegions(); + + MicroProfileMutex().lock(); + MP_ASSERT(State.NumSuspended == 0); + + DWORD ProcessId = GetCurrentProcessId(); + DWORD ThreadId = GetCurrentThreadId(); + + HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); + if(hSnap == INVALID_HANDLE_VALUE) + { + return false; + } + THREADENTRY32 te{}; + te.dwSize = sizeof(te); + State.NumSuspended = 0; + + if(Thread32First(hSnap, &te)) + { + do + { + if(te.th32OwnerProcessID != ProcessId) + continue; + if(te.th32ThreadID == ThreadId) + continue; + HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, te.th32ThreadID); + if(!hThread) + { + continue; + } + DWORD PrevCount = SuspendThread(hThread); + if(PrevCount == (DWORD)-1) + { + CloseHandle(hThread); + continue; + } + + CONTEXT ctx{}; + ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER; // Rip + registers + if(GetThreadContext(hThread, &ctx)) + { + State.SuspendedIP[State.NumSuspended] = (intptr_t)ctx.Rip; + } + if(State.NumSuspended < MICROPROFILE_SUSPEND_MAX) + { + State.Suspended[State.NumSuspended++] = hThread; + } + } while(Thread32Next(hSnap, &te)); + } + else + { + uprintf("Thread32First failed %08x\n", GetLastError()); + CloseHandle(hSnap); + return false; + } + CloseHandle(hSnap); + return State.NumSuspended > 0; +} + +void MicroProfilePatchEndSuspend() +{ + MicroProfileSuspendState& State = S.SuspendState; + if(0 == --State.SuspendCounter) + { + + for(uint32_t i = 0; i < State.NumSuspended; ++i) + { + ResumeThread(State.Suspended[i]); + CloseHandle(State.Suspended[i]); + } + State.NumSuspended = 0; + MicroProfileMutex().unlock(); + } +} + +template +void MicroProfileIterateSymbols(Callback CB, uint32_t* nModules, uint32_t nNumModules) +{ + MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileIterateSymbols", MP_PINK3); + QueryCallbackImpl Context(CB); + if(MicroProfileSymInit()) + { + // uprintf("symbols loaded!\n"); + // API_VERSION* pv = ImagehlpApiVersion(); + // uprintf("VERSION %d.%d.%d\n", pv->MajorVersion, pv->MinorVersion, pv->Revision); + + nLastModuleBaseWin32 = -1; + if(SymEnumerateModules64(GetCurrentProcess(), (PSYM_ENUMMODULES_CALLBACK64)MicroProfileEnumModules, NULL)) + { + } + QueryCallbackBase* pBase = &Context; + if(nNumModules) + { + HANDLE hProcess = GetCurrentProcess(); + char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)]; + PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer; + uint64_t t0 = MP_TICK(); + for(uint32_t i = 0; i < nNumModules; ++i) + { + uint32_t nModule = nModules[i]; + int64_t nBytes = 0; + MEMORY_BASIC_INFORMATION B; + + for(int j = 0; j < S.SymbolModules[nModule].nNumExecutableRegions; ++j) + { + intptr_t b = S.SymbolModules[nModule].Regions[j].nBegin; + intptr_t e = S.SymbolModules[nModule].Regions[j].nEnd; + while(b < e) + { + int r = VirtualQuery((LPCVOID)b, &B, sizeof(B)); + if(!r) + break; + switch(B.Protect) + { + case PAGE_EXECUTE: + case PAGE_EXECUTE_READ: + case PAGE_EXECUTE_READWRITE: + case PAGE_EXECUTE_WRITECOPY: + nBytes += B.RegionSize; + // uprintf("RANGE %p, %p .. %5.2fkb %08x, %08x\n", B.BaseAddress, (void*)(intptr_t(B.BaseAddress) + B.RegionSize), B.RegionSize / 1024.f, B.State, B.Protect); + } + b = intptr_t(B.BaseAddress) + B.RegionSize; + } + } + S.SymbolModules[nModule].nProgressTarget = nBytes; + + char pdbPath[MAX_PATH]; + HMODULE Module = (HMODULE)S.SymbolModules[nModule].nModuleBase; + S.nSymbolsDirty++; + S.SymbolModules[nModule].bDownloading = true; + if(MicroProfileDownloadPDB(Module, hProcess, pdbPath)) + { + S.SymbolModules[nModule].bDownloading = false; + S.nSymbolsDirty++; + MicroProfileLoadRawPDB(CB, pdbPath, S.SymbolModules[nModule].nModuleBase, nModule); + } + S.SymbolModules[nModule].bDownloading = false; + S.nSymbolsDirty++; + S.SymbolModules[nModule].nProgress = S.SymbolModules[nModule].nProgressTarget; + S.SymbolModules[nModule].nModuleLoadFinished.exchange(1); + } + + uint64_t t1 = MP_TICK(); + float fTime = float(MicroProfileTickToMsMultiplierCpu()) * (t1 - t0); + uprintf("load symbol time %6.2fms\n", fTime); + } + MicroProfileSymCleanup(); + } +} + +static int MicroProfileWin32SymInitCount = 0; +static int MicroProfileWin32SymInitSuccess = 0; + +bool MicroProfileSymInit() +{ + if(0 == MicroProfileWin32SymInitCount++) + { + auto h = GetCurrentProcess(); + SymCleanup(h); + SymSetOptions(SYMOPT_DEFERRED_LOADS); + if(SymInitialize(h, 0, FALSE)) + { + + MicroProfileWin32SymInitSuccess = 1; + char Path[MAX_PATH]; + bool PathValid = SymGetSearchPath(h, Path, MAX_PATH) > 0; + if(PathValid) + { + PathValid = strlen(Path) > 3; + } + if(!PathValid) + { + SymSetSearchPath(h, "srv*C:\\symbols*https://msdl.microsoft.com/download/symbols"); + } + } + else + { + MicroProfileWin32SymInitSuccess = 0; + } + } + return MicroProfileWin32SymInitSuccess != 0; +} +void MicroProfileSymCleanup() +{ + if(0 == --MicroProfileWin32SymInitCount) + { + MicroProfileWin32SymInitSuccess = 0; + SymCleanup(GetCurrentProcess()); + } +} + +static void* g_pFunctionFoundHack = 0; +static const char* g_pFunctionpNameFound = 0; +static char g_Demangled[512]; + +BOOL MicroProfileQueryContextEnumSymbols1(_In_ PSYMBOL_INFO pSymInfo, _In_ ULONG SymbolSize, _In_opt_ PVOID UserContext) +{ + if(pSymInfo->Tag == 5 || pSymInfo->Tag == 10) + { + char str[200]; + stbsp_snprintf(str, sizeof(str) - 1, "%s : %p\n", pSymInfo->Name, (void*)pSymInfo->Address); + OutputDebugStringA(str); + g_pFunctionpNameFound = pSymInfo->Name; + g_pFunctionFoundHack = (void*)pSymInfo->Address; + return FALSE; + } + return TRUE; +}; + +const char* MicroProfileDemangleSymbol(const char* pSymbol) +{ + return pSymbol; // todo: for some reasons all symbols im seaing right now are already undecorated? +} + +void MicroProfileInstrumentWithoutSymbols(const char** pModules, const char** pSymbols, uint32_t nNumSymbols) +{ + char SymString[512]; + const char* pStr = 0; + if(MicroProfileSymInit()) + { + HANDLE h = GetCurrentProcess(); + for(uint32_t i = 0; i < nNumSymbols; ++i) + { + int nCount = stbsp_snprintf(SymString, sizeof(SymString) - 1, "%s!%s", pModules[i], pSymbols[i]); + if(nCount <= sizeof(SymString) - 1) + { + g_pFunctionFoundHack = 0; + if(SymEnumSymbols(h, 0, SymString, MicroProfileQueryContextEnumSymbols1, 0)) + { + if(g_pFunctionFoundHack) + { + uint32_t nColor = MicroProfileColorFromString(pSymbols[i]); + const char* pDemangled = pSymbols[i]; // MicroProfileDemangleSymbol(pSymbols[i]); + MicroProfileInstrumentFunction(g_pFunctionFoundHack, pModules[i], pDemangled, nColor); + } + } + } + } + MicroProfileSymCleanup(); + } +} + +void MicroProfileSymbolEnumModules() +{ + HMODULE modules[1024]; + DWORD needed; + HANDLE h = GetCurrentProcess(); + + if(EnumProcessModules(h, modules, sizeof(modules), &needed)) + { + int count = needed / sizeof(HMODULE); + for(int i = 0; i < count; i++) + { + char moduleName[MAX_PATH]; + if(GetModuleFileNameEx(h, modules[i], moduleName, MAX_PATH)) + { + MODULEINFO mi = {}; + if(GetModuleInformation(h, modules[i], &mi, sizeof(mi))) + { + MicroProfileEnumModules(moduleName, (DWORD64)mi.lpBaseOfDll, 0); + } + } + } + } +} + +void MicroProfileSymbolUpdateModuleList() +{ + MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileSymbolUpdateModuleList", MP_PINK3); + // QueryCallbackImpl Context(CB); + if(MicroProfileSymInit()) + { + uprintf("symbols loaded!\n"); + API_VERSION* pv = ImagehlpApiVersion(); + uprintf("VERSION %d.%d.%d\n", pv->MajorVersion, pv->MinorVersion, pv->Revision); + + nLastModuleBaseWin32 = -1; + MicroProfileSymbolEnumModules(); + MicroProfileSymCleanup(); + } +} + +#endif + +#if defined(__APPLE__) && defined(__MACH__) +// '##::::'##::'#######:::'#######::'##:::'##:::::'#######:::'######::'##::::'##: +// ##:::: ##:'##.... ##:'##.... ##: ##::'##:::::'##.... ##:'##... ##:. ##::'##:: +// ##:::: ##: ##:::: ##: ##:::: ##: ##:'##:::::: ##:::: ##: ##:::..:::. ##'##::: +// #########: ##:::: ##: ##:::: ##: #####::::::: ##:::: ##:. ######::::. ###:::: +// ##.... ##: ##:::: ##: ##:::: ##: ##. ##:::::: ##:::: ##::..... ##::: ## ##::: +// ##:::: ##: ##:::: ##: ##:::: ##: ##:. ##::::: ##:::: ##:'##::: ##:: ##:. ##:: +// ##:::: ##:. #######::. #######:: ##::. ##::::. #######::. ######:: ##:::. ##: +// ..:::::..:::.......::::.......:::..::::..::::::.......::::......:::..:::::..:: + +#include +#include +#include +#include +#include +#include +#include +#include + +static void* MicroProfileAllocExecutableMemory(void* f, size_t s); +static void MicroProfileMakeWriteable(void* p_); + +extern "C" void microprofile_tramp_enter_patch(); +extern "C" void microprofile_tramp_enter(); +extern "C" void microprofile_tramp_code_begin(); +extern "C" void microprofile_tramp_code_end(); +extern "C" void microprofile_tramp_intercept0(); +extern "C" void microprofile_tramp_end(); +extern "C" void microprofile_tramp_exit(); +extern "C" void microprofile_tramp_leave(); +extern "C" void microprofile_tramp_trunk(); +extern "C" void microprofile_tramp_call_patch_pop(); +extern "C" void microprofile_tramp_call_patch_push(); + +bool MicroProfilePatchFunction(void* f, int Argument, MicroProfileHookFunc enter, MicroProfileHookFunc leave, MicroProfilePatchError* pError) __attribute__((optnone)) +{ + if(pError) + { + memcpy(&pError->Code[0], f, 12); + } + + intptr_t t_enter = (intptr_t)microprofile_tramp_enter; + intptr_t t_enter_patch_offset = (intptr_t)microprofile_tramp_enter_patch - t_enter; + intptr_t t_code_begin_offset = (intptr_t)microprofile_tramp_code_begin - t_enter; + intptr_t t_code_end_offset = (intptr_t)microprofile_tramp_code_end - t_enter; + intptr_t t_code_intercept0_offset = (intptr_t)microprofile_tramp_intercept0 - t_enter; + intptr_t t_code_exit_offset = (intptr_t)microprofile_tramp_exit - t_enter; + intptr_t t_code_leave_offset = (intptr_t)microprofile_tramp_leave - t_enter; + + intptr_t t_code_call_patch_push_offset = (intptr_t)microprofile_tramp_call_patch_push - t_enter; + intptr_t t_code_call_patch_pop_offset = (intptr_t)microprofile_tramp_call_patch_pop - t_enter; + intptr_t codemaxsize = t_code_end_offset - t_code_begin_offset; + intptr_t t_end_offset = (intptr_t)microprofile_tramp_end - t_enter; + intptr_t t_trunk_offset = (intptr_t)microprofile_tramp_trunk - t_enter; + intptr_t t_trunk_size = (intptr_t)microprofile_tramp_end - (intptr_t)microprofile_tramp_trunk; + + char* ptramp = (char*)MicroProfileAllocExecutableMemory(f, t_end_offset); + + intptr_t offset = ((intptr_t)f + 6 - (intptr_t)ptramp); + + uint32_t nBytesToCopy = 14; + if(offset < 0x80000000 && offset > -0x7fffffff) + { + /// offset is small enough to insert a relative jump + nBytesToCopy = 5; + } + + memcpy(ptramp, (void*)t_enter, t_end_offset); + + int nInstructionBytesDest = 0; + char* pInstructionMoveDest = ptramp + t_code_begin_offset; + char* pTrunk = ptramp + t_trunk_offset; + + int nInstructionBytesSrc = 0; + + uint32_t nRegsWritten = 0; + uint32_t nRetSafe = 0; + uint32_t nUsableJumpRegs = (1 << R_RAX) | (1 << R_R10) | (1 << R_R11); // scratch && !parameter register + if(!MicroProfileCopyInstructionBytes( + pInstructionMoveDest, f, nBytesToCopy, codemaxsize, pTrunk, t_trunk_size, nUsableJumpRegs, &nInstructionBytesDest, &nInstructionBytesSrc, &nRegsWritten, &nRetSafe)) + { + if(pError) + { + const char* pCode = (const char*)f; + memset(pError->Code, 0, sizeof(pError->Code)); + memcpy(pError->Code, pCode, nInstructionBytesSrc); + int off = stbsp_snprintf(pError->Message, sizeof(pError->Message), "Failed to move %d code bytes ", nInstructionBytesSrc); + pError->nCodeSize = nInstructionBytesSrc; + for(int i = 0; i < nInstructionBytesSrc; ++i) + { + off += stbsp_snprintf(off + pError->Message, sizeof(pError->Message) - off, "%02x ", 0xff & pCode[i]); + } + uprintf("%s\n", pError->Message); + } + return false; + } + intptr_t phome = nInstructionBytesSrc + (intptr_t)f; + uint32_t reg = nUsableJumpRegs & ~nRegsWritten; + static_assert(R_RAX == 0, "R_RAX must be 0"); + if(0 == reg) + { + if(nRetSafe == 0) + { + MP_BREAK(); // shout fail earlier + } + MicroProfileInsertRetJump(pInstructionMoveDest + nInstructionBytesDest, phome); + } + else + { + int r = R_RAX; + while((reg & 1) == 0) + { + reg >>= 1; + r++; + } + MicroProfileInsertRegisterJump(pInstructionMoveDest + nInstructionBytesDest, phome, r); + } + + // PATCH 1 TRAMP EXIT + intptr_t microprofile_tramp_exit = (intptr_t)ptramp + t_code_exit_offset; + memcpy(ptramp + t_enter_patch_offset + 2, (void*)µprofile_tramp_exit, 8); + + char* pintercept = t_code_intercept0_offset + ptramp; + + // PATCH 1.5 Argument + memcpy(pintercept - 4, (void*)&Argument, 4); + + // PATCH 2 INTERCEPT0 + intptr_t addr = (intptr_t)enter; //&intercept0; + memcpy(pintercept + 2, (void*)&addr, 8); + + // PATHC 2.5 argument + memcpy(ptramp + t_code_exit_offset + 3, (void*)&Argument, 4); + + intptr_t microprofile_tramp_leave = (intptr_t)ptramp + t_code_leave_offset; + // PATCH 3 INTERCEPT1 + intptr_t addr1 = (intptr_t)leave; //&intercept1; + memcpy((char*)microprofile_tramp_leave + 2, (void*)&addr1, 8); + + intptr_t patch_push_addr = (intptr_t)(&MicroProfile_Patch_TLS_PUSH); + intptr_t patch_pop_addr = (intptr_t)(&MicroProfile_Patch_TLS_POP); + memcpy((char*)ptramp + t_code_call_patch_push_offset + 2, &patch_push_addr, 8); + memcpy((char*)ptramp + t_code_call_patch_pop_offset + 2, &patch_pop_addr, 8); + + { + // PATCH 4 DEST FUNC + + MicroProfileMakeWriteable(f); + char* pp = (char*)f; + char* ppend = pp + nInstructionBytesSrc; + if(nInstructionBytesSrc < 14) + { + uprintf("inserting 5b jump\n"); + pp = MicroProfileInsertRelativeJump((char*)pp, (intptr_t)ptramp); + } + else + { + uprintf("inserting 14b jump\n"); + pp = MicroProfileInsertRegisterJump(pp, (intptr_t)ptramp, R_RAX); + } + while(pp != ppend) + { + *pp++ = 0x90; + } + } + return true; +} + +static void MicroProfileMakeWriteable(void* p_) +{ +#ifdef _PATCH_TEST + // for testing.. + static const uint32_t WritableSize = 16; + static uint32_t WritableCount = 0; + static intptr_t WritableStart[WritableSize] = { 0 }; + static intptr_t WritableEnd[WritableSize] = { 0 }; + for(uint32_t i = 0; i < WritableCount; ++i) + { + intptr_t x = (intptr_t)p_; + if(x >= WritableStart[i] && x < WritableEnd[i]) + { + return; + } + } + +#endif + + intptr_t p = (intptr_t)p_; + // uprintf("MicroProfilemakewriteable %lx\n", p); + mach_port_name_t task = mach_task_self(); + vm_map_offset_t vmoffset = 0; + mach_vm_size_t vmsize = 0; + uint32_t nd; + kern_return_t kr; + vm_region_submap_info_64 vbr; + mach_msg_type_number_t vbrcount = sizeof(vbr) / 4; + + while(KERN_SUCCESS == (kr = mach_vm_region_recurse(task, &vmoffset, &vmsize, &nd, (vm_region_recurse_info_t)&vbr, &vbrcount))) + { + if(p >= (intptr_t)vmoffset && p <= intptr_t(vmoffset + vmsize)) + { + if(0 == (vbr.protection & VM_PROT_WRITE)) + { + // uprintf("region match .. enabling write\n"); + int x = mprotect((void*)vmoffset, vmsize, PROT_WRITE | PROT_READ | PROT_EXEC); + if(x) + { + // uprintf("mprotect failed ... err %d:: %d %s\n", errno, x, strerror(errno)); + } + else + { + uprintf("region is [%llx,%llx] .. %08llx %d", vmoffset, vmoffset + vmsize, vmsize, vbr.is_submap); + uprintf("prot: %c%c%c %c%c%c\n", + vbr.protection & VM_PROT_READ ? 'r' : '-', + vbr.protection & VM_PROT_WRITE ? 'w' : '-', + vbr.protection & VM_PROT_EXECUTE ? 'x' : '-', + + vbr.max_protection & VM_PROT_READ ? 'r' : '-', + vbr.max_protection & VM_PROT_WRITE ? 'w' : '-', + vbr.max_protection & VM_PROT_EXECUTE ? 'x' : '-'); + continue; + } + } + else + { +#ifdef _PATCH_TEST + if(WritableCount < WritableSize) + { + WritableStart[WritableCount] = vmoffset; + WritableEnd[WritableCount] = vmoffset + vmsize; + WritableCount++; + } + +#endif + } + } + + vmoffset += vmsize; + vbrcount = sizeof(vbr) / 4; + } +} + +int MicroProfileTrimFunctionName(const char* pStr, char* pOutBegin, char* pOutEnd) +{ + int l = strlen(pStr) - 1; + int sz = 0; + pOutEnd--; + if(l < pOutEnd - pOutBegin && pOutBegin != pOutEnd) + { + const char* p = pStr; + const char* pEnd = pStr + l + 1; + int in = 0; + while(p != pEnd && pOutBegin != pOutEnd) + { + char c = *p++; + if(c == '(' || c == '<') + { + in++; + } + else if(c == ')' || c == '>') + { + in--; + continue; + } + + if(in == 0) + { + *pOutBegin++ = c; + sz++; + } + } + + *pOutBegin++ = '\0'; + } + return sz; +} + +int MicroProfileFindFunctionName(const char* pStr, const char** ppStart) +{ + int l = strlen(pStr) - 1; + if(l < 1024) + { + char b[1024] = { 0 }; + char* put = &b[0]; + + const char* p = pStr; + const char* pEnd = pStr + l + 1; + int in = 0; + while(p != pEnd) + { + char c = *p++; + if(c == '(' || c == '<') + { + in++; + } + else if(c == ')' || c == '>') + { + in--; + continue; + } + + if(in == 0) + { + *put++ = c; + } + } + + *put++ = '\0'; + uprintf("trimmed %s\n", b); + } + + // int nFirstParen = l; + int nNumParen = 0; + int c = 0; + + while(l >= 0 && pStr[l] != ')' && c++ < (int)(sizeof(" const") - 1)) + { + l--; + } + if(pStr[l] == ')') + { + do + { + if(pStr[l] == ')') + { + nNumParen++; + } + else if(pStr[l] == '(') + { + nNumParen--; + } + l--; + } while(nNumParen > 0 && l >= 0); + } + else + { + *ppStart = pStr; + return 0; + } + while(l >= 0 && isspace(pStr[l])) + { + --l; + } + int nLast = l; + while(l >= 0 && !isspace(pStr[l])) + { + l--; + } + int nFirst = l; + if(nFirst == nLast) + return 0; + int nCount = nLast - nFirst + 1; + *ppStart = pStr + nFirst; + return nCount; +} + +const char* MicroProfileDemangleSymbol(const char* pSymbol) +{ + static unsigned long size = 128; + static char* pTempBuffer = (char*)malloc(size); // needs to be malloc because demangle function might realloc it. + unsigned long len = size; + int ret = 0; + char* pBuffer = pTempBuffer; + pBuffer = abi::__cxa_demangle(pSymbol, pTempBuffer, &len, &ret); + if(ret == 0) + { + if(pBuffer != pTempBuffer) + { + pTempBuffer = pBuffer; + if(len < size) + __builtin_trap(); + size = len; + } + return pTempBuffer; + } + else + { + return pSymbol; + } +} + +template +void MicroProfileIterateSymbols(Callback CB, uint32_t* nModules, uint32_t nNumModules) +{ + MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileIterateSymbols", MP_PINK3); + char FunctionName[1024]; + (void)FunctionName; + mach_port_name_t task = mach_task_self(); + vm_map_offset_t vmoffset = 0; + mach_vm_size_t vmsize = 0; + uint32_t nd; + kern_return_t kr; + vm_region_submap_info_64 vbr; + mach_msg_type_number_t vbrcount = sizeof(vbr) / 4; + + intptr_t nCurrentModule = -1; + uint32_t nCurrentModuleId = -1; + + auto OnFunction = [&](void* addr, void* addrend, const char* pSymbol, const char* pModuleName, void* pModuleAddr) -> bool + { + const char* pStr = MicroProfileDemangleSymbol(pSymbol); + ; + int l = MicroProfileTrimFunctionName(pStr, &FunctionName[0], &FunctionName[1024]); + if(nCurrentModule != (intptr_t)pModuleAddr) + { + nCurrentModule = (intptr_t)pModuleAddr; + nCurrentModuleId = MicroProfileSymbolGetModule(pModuleName, nCurrentModule); + } + + CB(l ? &FunctionName[0] : pStr, l ? &FunctionName[0] : 0, (intptr_t)addr, (intptr_t)addrend, nCurrentModuleId); + return true; + }; + vm_offset_t addr_prev = 0; + + while(KERN_SUCCESS == (kr = mach_vm_region_recurse(task, &vmoffset, &vmsize, &nd, (vm_region_recurse_info_t)&vbr, &vbrcount))) + { + { + addr_prev = vmoffset + vmsize; + if(0 != (vbr.protection & VM_PROT_EXECUTE)) + { + bool bProcessModule = true; + int nModule = -1; + if(nNumModules) + { + bProcessModule = false; + for(uint32_t i = 0; i < nNumModules; ++i) + { + intptr_t nBase = S.SymbolModules[nModules[i]].Regions[0].nBegin; + if((intptr_t)vmoffset == nBase) + { + bProcessModule = true; + nModule = nModules[i]; + break; + } + } + } + if(bProcessModule) + { + S.SymbolModules[nModule].nProgressTarget = S.SymbolModules[nModule].Regions[0].nEnd - S.SymbolModules[nModule].Regions[0].nBegin; + dl_info di; + int r = 0; + r = dladdr((void*)vmoffset, &di); + if(r) + { + OnFunction(di.dli_saddr, (void*)addr_prev, di.dli_sname, di.dli_fname, di.dli_fbase); + } + intptr_t addr = vmoffset + vmsize - 1; + while(1) + { + r = dladdr((void*)(addr), &di); + if(r) + { + if(!di.dli_sname) + { + break; + } + OnFunction(di.dli_saddr, (void*)addr_prev, di.dli_sname, di.dli_fname, di.dli_fbase); + } + else + { + break; + } + addr_prev = (vm_offset_t)di.dli_saddr; + addr = (intptr_t)di.dli_saddr - 1; + if(di.dli_saddr < (void*)vmoffset) + { + break; + } + } + for(int i = 0; i < S.SymbolNumModules; ++i) + { + if(S.SymbolModules[i].Regions[0].nBegin == (intptr_t)vmoffset) + { + S.SymbolModules[i].nModuleLoadFinished.store(1); + } + } + } + } + } + vmoffset += vmsize; + vbrcount = sizeof(vbr) / 4; + } +} + +void MicroProfileSymbolUpdateModuleList() +{ + char FunctionName[1024]; + (void)FunctionName; + mach_port_name_t task = mach_task_self(); + vm_map_offset_t vmoffset = 0; + mach_vm_size_t vmsize = 0; + uint32_t nd; + kern_return_t kr; + vm_region_submap_info_64 vbr; + mach_msg_type_number_t vbrcount = sizeof(vbr) / 4; + + while(KERN_SUCCESS == (kr = mach_vm_region_recurse(task, &vmoffset, &vmsize, &nd, (vm_region_recurse_info_t)&vbr, &vbrcount))) + { + { + if(0 != (vbr.protection & VM_PROT_EXECUTE)) + { + dl_info di; + int r = 0; + r = dladdr((void*)vmoffset, &di); + if(r) + { + uprintf("[0x%p-0x%p] (0x%p) %s %s\n", (void*)vmoffset, (void*)addr_prev, di.dli_fbase, di.dli_fname, di.dli_sname); + MicroProfileSymbolInitModule(di.dli_fname, (intptr_t)vmoffset, (intptr_t)vmoffset + vmsize); + } + } + } + vmoffset += vmsize; + vbrcount = sizeof(vbr) / 4; + } +} + +static void* MicroProfileAllocExecutableMemory(void* f, size_t s) +{ + static uint64_t nPageSize = 0; + if(!nPageSize) + { + nPageSize = getpagesize(); + } + s = (s + (nPageSize - 1)) & (~(nPageSize - 1)); + + void* pMem = mmap((void*)f, s, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, 0, 0); + + // uprintf("Allocating %zu %p\n", s, pMem); + return pMem; +} + +bool MicroProfileDemangleName(const char* pName, char* OutName, uint32_t Size) +{ + // demangle not implemented + strcpy(OutName, pName); + return true; +} + +bool MicroProfilePatchBeginSuspend() +{ + // Not implemented + return true; +} + +void MicroProfilePatchEndSuspend() +{ + // Not implemented +} + +void MicroProfileInstrumentWithoutSymbols(const char** pModules, const char** pSymbols, uint32_t nNumSymbols) +{ + void* M = dlopen(0, 0); + for(uint32_t i = 0; i < nNumSymbols; ++i) + { + // uprintf("trying to find symbol %s\n", pSym); + void* s = dlsym(M, pSymbols[i]); + uprintf("sym returned %p\n", s); + if(s) + { + uint32_t nColor = MicroProfileColorFromString(pSymbols[i]); + const char* pDemangled = MicroProfileDemangleSymbol(pSymbols[i]); + MicroProfileInstrumentFunction(s, pModules[i], pDemangled, nColor); + } + } + dlclose(M); +} +#endif + +#if defined(__unix__) && defined(__x86_64__) +// '##::::'##::'#######:::'#######::'##:::'##::::'##:::::::'####:'##::: ##:'##::::'##:'##::::'##: +// ##:::: ##:'##.... ##:'##.... ##: ##::'##::::: ##:::::::. ##:: ###:: ##: ##:::: ##:. ##::'##:: +// ##:::: ##: ##:::: ##: ##:::: ##: ##:'##:::::: ##:::::::: ##:: ####: ##: ##:::: ##::. ##'##::: +// #########: ##:::: ##: ##:::: ##: #####::::::: ##:::::::: ##:: ## ## ##: ##:::: ##:::. ###:::: +// ##.... ##: ##:::: ##: ##:::: ##: ##. ##:::::: ##:::::::: ##:: ##. ####: ##:::: ##::: ## ##::: +// ##:::: ##: ##:::: ##: ##:::: ##: ##:. ##::::: ##:::::::: ##:: ##:. ###: ##:::: ##:: ##:. ##:: +// ##:::: ##:. #######::. #######:: ##::. ##:::: ########:'####: ##::. ##:. #######:: ##:::. ##: +// ..:::::..:::.......::::.......:::..::::..:::::........::....::..::::..:::.......:::..:::::..:: + +#include +#include +#include +#include +#include +#include + +static void* MicroProfileAllocExecutableMemory(void* f, size_t s); +static void MicroProfileMakeWriteable(void* p_); + +extern "C" void microprofile_tramp_enter_patch() asm("_microprofile_tramp_enter_patch"); +extern "C" void microprofile_tramp_enter() asm("_microprofile_tramp_enter"); +extern "C" void microprofile_tramp_code_begin() asm("_microprofile_tramp_code_begin"); +extern "C" void microprofile_tramp_code_end() asm("_microprofile_tramp_code_end"); +extern "C" void microprofile_tramp_intercept0() asm("_microprofile_tramp_intercept0"); +extern "C" void microprofile_tramp_end() asm("_microprofile_tramp_end"); +extern "C" void microprofile_tramp_exit() asm("_microprofile_tramp_exit"); +extern "C" void microprofile_tramp_leave() asm("_microprofile_tramp_leave"); +extern "C" void microprofile_tramp_trunk() asm("_microprofile_tramp_trunk"); +extern "C" void microprofile_tramp_call_patch_pop() asm("_microprofile_tramp_call_patch_pop"); +extern "C" void microprofile_tramp_call_patch_push() asm("_microprofile_tramp_call_patch_push"); + +bool MicroProfilePatchFunction(void* f, int Argument, MicroProfileHookFunc enter, MicroProfileHookFunc leave, MicroProfilePatchError* pError) +{ + if(pError) + { + memcpy(&pError->Code[0], f, 12); + } + + intptr_t t_enter = (intptr_t)microprofile_tramp_enter; + intptr_t t_enter_patch_offset = (intptr_t)microprofile_tramp_enter_patch - t_enter; + intptr_t t_code_begin_offset = (intptr_t)microprofile_tramp_code_begin - t_enter; + intptr_t t_code_end_offset = (intptr_t)microprofile_tramp_code_end - t_enter; + intptr_t t_code_intercept0_offset = (intptr_t)microprofile_tramp_intercept0 - t_enter; + intptr_t t_code_exit_offset = (intptr_t)microprofile_tramp_exit - t_enter; + intptr_t t_code_leave_offset = (intptr_t)microprofile_tramp_leave - t_enter; + + intptr_t t_code_call_patch_push_offset = (intptr_t)microprofile_tramp_call_patch_push - t_enter; + intptr_t t_code_call_patch_pop_offset = (intptr_t)microprofile_tramp_call_patch_pop - t_enter; + intptr_t codemaxsize = t_code_end_offset - t_code_begin_offset; + intptr_t t_end_offset = (intptr_t)microprofile_tramp_end - t_enter; + intptr_t t_trunk_offset = (intptr_t)microprofile_tramp_trunk - t_enter; + intptr_t t_trunk_size = (intptr_t)microprofile_tramp_end - (intptr_t)microprofile_tramp_trunk; + + char* ptramp = (char*)MicroProfileAllocExecutableMemory(f, t_end_offset); + + intptr_t offset = ((intptr_t)f + 6 - (intptr_t)ptramp); + + uint32_t nBytesToCopy = 14; + if(offset < 0x80000000 && offset > -0x7fffffff) + { + /// offset is small enough to insert a relative jump + nBytesToCopy = 5; + } + + memcpy(ptramp, (void*)t_enter, t_end_offset); + + int nInstructionBytesDest = 0; + char* pInstructionMoveDest = ptramp + t_code_begin_offset; + char* pTrunk = ptramp + t_trunk_offset; + + int nInstructionBytesSrc = 0; + + uint32_t nRegsWritten = 0; + uint32_t nRetSafe = 0; + uint32_t nUsableJumpRegs = (1 << R_RAX) | (1 << R_R10) | (1 << R_R11); // scratch && !parameter register + if(!MicroProfileCopyInstructionBytes( + pInstructionMoveDest, f, nBytesToCopy, codemaxsize, pTrunk, t_trunk_size, nUsableJumpRegs, &nInstructionBytesDest, &nInstructionBytesSrc, &nRegsWritten, &nRetSafe)) + { + if(pError) + { + const char* pCode = (const char*)f; + memset(pError->Code, 0, sizeof(pError->Code)); + memcpy(pError->Code, pCode, nInstructionBytesSrc); + int off = stbsp_snprintf(pError->Message, sizeof(pError->Message), "Failed to move %d code bytes ", nInstructionBytesSrc); + pError->nCodeSize = nInstructionBytesSrc; + for(int i = 0; i < nInstructionBytesSrc; ++i) + { + off += stbsp_snprintf(off + pError->Message, sizeof(pError->Message) - off, "%02x ", 0xff & pCode[i]); + } + uprintf("%s\n", pError->Message); + } + return false; + } + intptr_t phome = nInstructionBytesSrc + (intptr_t)f; + uint32_t reg = nUsableJumpRegs & ~nRegsWritten; + static_assert(R_RAX == 0, "R_RAX must be 0"); + if(0 == reg) + { + if(nRetSafe == 0) + { + MP_BREAK(); // shout fail earlier + } + MicroProfileInsertRetJump(pInstructionMoveDest + nInstructionBytesDest, phome); + } + else + { + int r = R_RAX; + while((reg & 1) == 0) + { + reg >>= 1; + r++; + } + MicroProfileInsertRegisterJump(pInstructionMoveDest + nInstructionBytesDest, phome, r); + } + + // PATCH 1 TRAMP EXIT + intptr_t microprofile_tramp_exit = (intptr_t)ptramp + t_code_exit_offset; + memcpy(ptramp + t_enter_patch_offset + 2, (void*)µprofile_tramp_exit, 8); + + char* pintercept = t_code_intercept0_offset + ptramp; + + // PATCH 1.5 Argument + memcpy(pintercept - 4, (void*)&Argument, 4); + + // PATCH 2 INTERCEPT0 + intptr_t addr = (intptr_t)enter; //&intercept0; + memcpy(pintercept + 2, (void*)&addr, 8); + + // PATHC 2.5 argument + memcpy(ptramp + t_code_exit_offset + 3, (void*)&Argument, 4); + + intptr_t microprofile_tramp_leave = (intptr_t)ptramp + t_code_leave_offset; + // PATCH 3 INTERCEPT1 + intptr_t addr1 = (intptr_t)leave; //&intercept1; + memcpy((char*)microprofile_tramp_leave + 2, (void*)&addr1, 8); + + intptr_t patch_push_addr = (intptr_t)(&MicroProfile_Patch_TLS_PUSH); + intptr_t patch_pop_addr = (intptr_t)(&MicroProfile_Patch_TLS_POP); + memcpy((char*)ptramp + t_code_call_patch_push_offset + 2, &patch_push_addr, 8); + memcpy((char*)ptramp + t_code_call_patch_pop_offset + 2, &patch_pop_addr, 8); + + { + // PATCH 4 DEST FUNC + + MicroProfileMakeWriteable(f); + char* pp = (char*)f; + char* ppend = pp + nInstructionBytesSrc; + + if(nInstructionBytesSrc < 14) + { + uprintf("inserting 5b jump\n"); + pp = MicroProfileInsertRelativeJump((char*)pp, (intptr_t)ptramp); + } + else + { + uprintf("inserting 14b jump\n"); + pp = MicroProfileInsertRegisterJump(pp, (intptr_t)ptramp, R_RAX); + } + while(pp != ppend) + { + *pp++ = 0x90; + } + } + return true; +} + +static void MicroProfileMakeWriteable(void* p_) +{ + intptr_t nPageSize = (intptr_t)getpagesize(); + intptr_t p = ((intptr_t)p_) & ~(nPageSize - 1); + intptr_t e = nPageSize + ((14 + (intptr_t)p_) & ~(nPageSize - 1)); + size_t s = e - p; + mprotect((void*)p, s, PROT_READ | PROT_WRITE | PROT_EXEC); +} + +int MicroProfileTrimFunctionName(const char* pStr, char* pOutBegin, char* pOutEnd) +{ + int l = strlen(pStr) - 1; + int sz = 0; + pOutEnd--; + if(l < pOutEnd - pOutBegin && pOutBegin != pOutEnd) + { + const char* p = pStr; + const char* pEnd = pStr + l + 1; + int in = 0; + while(p != pEnd && pOutBegin != pOutEnd) + { + char c = *p++; + if(c == '(' || c == '<') + { + in++; + } + else if(c == ')' || c == '>') + { + in--; + continue; + } + + if(in == 0) + { + *pOutBegin++ = c; + sz++; + } + } + + *pOutBegin++ = '\0'; + } + return sz; +} + +int MicroProfileFindFunctionName(const char* pStr, const char** ppStart) +{ + int l = strlen(pStr) - 1; + if(l < 1024) + { + char b[1024] = { 0 }; + char* put = &b[0]; + + const char* p = pStr; + const char* pEnd = pStr + l + 1; + int in = 0; + while(p != pEnd) + { + char c = *p++; + if(c == '(' || c == '<') + { + in++; + } + else if(c == ')' || c == '>') + { + in--; + continue; + } + + if(in == 0) + { + *put++ = c; + } + } + + *put++ = '\0'; + uprintf("trimmed %s\n", b); + } + + // int nFirstParen = l; + int nNumParen = 0; + int c = 0; + + while(l >= 0 && pStr[l] != ')' && c++ < (int)(sizeof(" const") - 1)) + { + l--; + } + if(pStr[l] == ')') + { + do + { + if(pStr[l] == ')') + { + nNumParen++; + } + else if(pStr[l] == '(') + { + nNumParen--; + } + l--; + } while(nNumParen > 0 && l >= 0); + } + else + { + *ppStart = pStr; + return 0; + } + while(l >= 0 && isspace(pStr[l])) + { + --l; + } + int nLast = l; + while(l >= 0 && !isspace(pStr[l])) + { + l--; + } + int nFirst = l; + if(nFirst == nLast) + return 0; + int nCount = nLast - nFirst + 1; + *ppStart = pStr + nFirst; + return nCount; +} + +const char* MicroProfileDemangleSymbol(const char* pSymbol) +{ + static unsigned long size = 128; + static char* pTempBuffer = (char*)malloc(size); // needs to be malloc because demangle function might realloc it. + unsigned long len = size; + int ret = 0; + char* pBuffer = pTempBuffer; + pBuffer = abi::__cxa_demangle(pSymbol, pTempBuffer, &len, &ret); + if(ret == 0) + { + if(pBuffer != pTempBuffer) + { + pTempBuffer = pBuffer; + if(len < size) + __builtin_trap(); + size = len; + } + return pTempBuffer; + } + else + { + return pSymbol; + } +} + +template +void MicroProfileIterateSymbols(Callback CB, uint32_t* nModules, uint32_t nNumModules) +{ + MICROPROFILE_SCOPEI("MicroProfile", "MicroProfileIterateSymbols", MP_PINK3); + char FunctionName[1024]; + + intptr_t nCurrentModule = -1; + uint32_t nCurrentModuleId = -1; + + auto OnFunction = [&](void* addr, void* addrend, const char* pSymbol, const char* pModuleName, void* pModuleAddr) -> bool + { + const char* pStr = MicroProfileDemangleSymbol(pSymbol); + ; + int l = MicroProfileTrimFunctionName(pStr, &FunctionName[0], &FunctionName[1024]); + MP_ASSERT(nCurrentModule == (intptr_t)pModuleAddr); + CB(l ? &FunctionName[0] : pStr, l ? &FunctionName[0] : 0, (intptr_t)addr, (intptr_t)addrend, nCurrentModuleId); + return true; + }; + + for(int i = 0; i < S.SymbolNumModules; ++i) + { + auto& M = S.SymbolModules[i]; + if(0 != nNumModules) + { + bool bProcess = false; + for(uint32_t j = 0; j < nNumModules; ++j) + { + if(nModules[j] == (uint32_t)i) + { + bProcess = true; + break; + } + } + if(!bProcess) + continue; + } + nCurrentModuleId = i; + Dl_info di; + int r = 0; + r = dladdr((void*)(M.Regions[0].nBegin), &di); + if(r) + { + nCurrentModule = (intptr_t)di.dli_fbase; + M.nProgressTarget = 0; + for(int j = 0; j < M.nNumExecutableRegions; ++j) + { + M.nProgressTarget += M.Regions[j].nEnd - M.Regions[j].nBegin; + } + for(int j = 0; j < M.nNumExecutableRegions; ++j) + { + const intptr_t nBegin = M.Regions[j].nBegin; + const intptr_t nEnd = M.Regions[j].nEnd; + int r = 0; + intptr_t nAddr = (nEnd - 8) & ~7; + intptr_t nAddrPrev = nEnd; + while(1) + { + r = dladdr((void*)(nAddr), &di); + if(r && di.dli_sname) + { + OnFunction(di.dli_saddr, (void*)nAddrPrev, di.dli_sname, di.dli_fname, di.dli_fbase); + nAddrPrev = (intptr_t)di.dli_saddr; + nAddr = (intptr_t)di.dli_saddr - 1; + } + else + { + nAddr = (nAddr - 7) & ~7; // pretty ineffecient, but it seems linux just returns 0 when there is no symbols, making this the only option I can come up with? + } + if(nAddr < nBegin) + { + break; + } + } + } + M.nProgress = M.nProgressTarget; + M.nModuleLoadFinished.store(1); + } + } +} + +void MicroProfileSymbolUpdateModuleList() +{ + // So, this was the only way I could find to do this.. + // Is this seriously how they want this to be done? + FILE* F = fopen("/proc/self/maps", "r"); + char* line = 0; + size_t len; + ssize_t read; + Dl_info di; + while((read = getline(&line, &len, F)) != -1) + { + void* pBase = 0; + void* pEnd = 0; + char c, r, w, x, p; + + if(8 == sscanf(line, "%p%c%p%c%c%c%c%c", &pBase, &c, &pEnd, &c, &r, &w, &x, &p)) + { + if('x' == x) + { + int r = 0; + r = dladdr(pBase, &di); + if(r) + { + if('[' != di.dli_fname[0]) + { + MicroProfileSymbolInitModule(di.dli_fname, (intptr_t)pBase, (intptr_t)pEnd); + } + } + } + } + } + fclose(F); + MicroProfileSymbolMergeExecutableRegions(); +} + +static void* MicroProfileAllocExecutableMemory(void* f, size_t s) +{ + static uint64_t nPageSize = 0; + if(!nPageSize) + { + nPageSize = getpagesize(); + } + s = (s + (nPageSize - 1)) & (~(nPageSize - 1)); + + void* pMem = mmap(f, s, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, 0, 0); + return pMem; +} + +bool MicroProfileDemangleName(const char* pName, char* OutName, uint32_t Size) +{ + // demangle not implemented + strcpy(OutName, pName); + return true; +} + +bool MicroProfilePatchBeginSuspend() +{ + // Not implemented + return true; +} + +void MicroProfilePatchEndSuspend() +{ + // Not implemented +} + +// not yet tested. +void MicroProfileInstrumentWithoutSymbols(const char** pModules, const char** pSymbols, uint32_t nNumSymbols) +{ + void* M = dlopen(0, 0); + for(uint32_t i = 0; i < nNumSymbols; ++i) + { + // uprintf("trying to find symbol %s\n", pSym); + void* s = dlsym(M, pSymbols[i]); + uprintf("sym returned %p\n", s); + if(s) + { + uint32_t nColor = MicroProfileColorFromString(pSymbols[i]); + const char* pDemangled = MicroProfileDemangleSymbol(pSymbols[i]); + MicroProfileInstrumentFunction(s, pModules[i], pDemangled, nColor); + } + } + dlclose(M); +} + +#endif + +#endif + +void MicroProfileHashTableInit(MicroProfileHashTable* pTable, uint32_t nInitialSize, uint32_t nSearchLimit, MicroProfileHashCompareFunction CompareFunc, MicroProfileHashFunction HashFunc) +{ + pTable->nAllocated = nInitialSize; + pTable->nUsed = 0; + uint32_t nSize = nInitialSize * sizeof(MicroProfileHashTableEntry); + pTable->pEntries = (MicroProfileHashTableEntry*)MICROPROFILE_ALLOC(nSize, 8); + pTable->CompareFunc = CompareFunc; + pTable->HashFunc = HashFunc; + pTable->nSearchLimit = nSearchLimit; + pTable->nLim = pTable->nAllocated / 5; + if(pTable->nLim > pTable->nSearchLimit) + pTable->nLim = pTable->nSearchLimit; + memset(pTable->pEntries, 0, nSize); +} +void MicroProfileHashTableDestroy(MicroProfileHashTable* pTable) +{ + MICROPROFILE_FREE(pTable->pEntries); +} + +uint64_t MicroProfileHashTableHash(MicroProfileHashTable* pTable, uint64_t K) +{ + uint64_t H = pTable->HashFunc ? (*pTable->HashFunc)(K) : K; + return H == 0 ? 1 : H; +} + +void MicroProfileHashTableGrow(MicroProfileHashTable* pTable) +{ + uint32_t nAllocated = pTable->nAllocated; + uint32_t nNewSize = nAllocated * 2; + uprintf("GROW %d -> %d\n", nAllocated, nNewSize); + + MicroProfileHashTable New; + MicroProfileHashTableInit(&New, nNewSize, pTable->nSearchLimit, pTable->CompareFunc, pTable->HashFunc); + for(uint32_t i = 0; i < nAllocated; ++i) + { + MicroProfileHashTableEntry& E = pTable->pEntries[i]; + if(E.Hash != 0) + { + MicroProfileHashTableSet(&New, E.Key, E.Value, E.Hash, false); + } + } + MicroProfileHashTableDestroy(pTable); + *pTable = New; +} + +bool MicroProfileHashTableSet(MicroProfileHashTable* pTable, uint64_t Key, uintptr_t Value) +{ + uint64_t H = MicroProfileHashTableHash(pTable, Key); + return MicroProfileHashTableSet(pTable, Key, Value, H, true); +} + +MicroProfileHashTableIterator MicroProfileGetHashTableIteratorBegin(MicroProfileHashTable* HashTable) +{ + return MicroProfileHashTableIterator(0, HashTable); +} + +MicroProfileHashTableIterator MicroProfileGetHashTableIteratorEnd(MicroProfileHashTable* HashTable) +{ + return MicroProfileHashTableIterator(HashTable->nAllocated, HashTable); +} + +bool MicroProfileHashTableSet(MicroProfileHashTable* pTable, uint64_t Key, uintptr_t Value, uint64_t H, bool bAllowGrow) +{ + if(H == 0) + MP_BREAK(); // not supported. + MicroProfileHashCompareFunction Cmp = pTable->CompareFunc; + while(1) + { + const uint32_t nLim = pTable->nLim; + uint32_t B = H % pTable->nAllocated; + MicroProfileHashTableEntry* pEntries = pTable->pEntries; + + for(uint32_t i = 0; i < pTable->nAllocated; ++i) + { + uint32_t Idx = (B + i) % pTable->nAllocated; + if(pEntries[Idx].Hash == 0) + { + pEntries[Idx].Hash = H; + pEntries[Idx].Key = Key; + pEntries[Idx].Value = Value; + return true; + } + else if(pEntries[Idx].Hash == H && (Cmp ? (Cmp)(Key, pEntries[Idx].Key) : Key == pEntries[Idx].Key)) + { + pEntries[Idx].Value = Value; + return true; + } + else if(i > nLim) + { + break; + } + } + if(bAllowGrow) + { + MicroProfileHashTableGrow(pTable); + } + else + { + MP_BREAK(); + } + } + MP_BREAK(); +} + +bool MicroProfileHashTableGet(MicroProfileHashTable* pTable, uint64_t Key, uintptr_t* pValue) +{ + uint64_t H = MicroProfileHashTableHash(pTable, Key); + uint32_t B = H % pTable->nAllocated; + MicroProfileHashTableEntry* pEntries = pTable->pEntries; + MicroProfileHashCompareFunction Cmp = pTable->CompareFunc; + for(uint32_t i = 0; i < pTable->nAllocated; ++i) + { + uint32_t Idx = (B + i) % pTable->nAllocated; + if(pEntries[Idx].Hash == 0) + { + return false; + } + else if(pEntries[Idx].Hash == H && (Cmp ? (Cmp)(Key, pEntries[Idx].Key) : Key == pEntries[Idx].Key)) + { + *pValue = pEntries[Idx].Value; + return true; + } + } + return false; +} + +bool MicroProfileHashTableRemove(MicroProfileHashTable* pTable, uint64_t Key) +{ + + uint64_t H = MicroProfileHashTableHash(pTable, Key); + uint32_t B = H % pTable->nAllocated; + MicroProfileHashTableEntry* pEntries = pTable->pEntries; + MicroProfileHashCompareFunction Cmp = pTable->CompareFunc; + uint32_t nBase = (uint32_t)-1; + uint32_t nAllocated = pTable->nAllocated; + for(uint32_t i = 0; i < nAllocated; ++i) + { + uint32_t Idx = (B + i) % nAllocated; + if(pEntries[Idx].Hash == 0) + { + return false; + } + else if(pEntries[Idx].Hash == H && (Cmp ? (Cmp)(Key, pEntries[Idx].Key) : Key == pEntries[Idx].Key)) + { + nBase = Idx; + break; + } + } + pEntries[nBase].Hash = 0; + pEntries[nBase].Key = 0; + pEntries[nBase].Value = 0; + nBase++; + for(uint32_t i = 0; i < nAllocated; ++i) + { + uint32_t Idx = (nBase + i) % nAllocated; + if(pEntries[Idx].Hash == 0) + { + break; + } + else + { + MicroProfileHashTableEntry E = pEntries[Idx]; + pEntries[Idx] = {}; + MicroProfileHashTableSet(pTable, E.Key, E.Value, E.Hash, false); + } + } + return true; +} +uint64_t MicroProfileHashTableHashString(uint64_t pString) +{ + return MicroProfileStringHash((const char*)pString); +} + +bool MicroProfileHashTableCompareString(uint64_t L, uint64_t R) +{ + return 0 == strcmp((const char*)L, (const char*)R); +} +uint64_t MicroProfileHashTableHashPtr(uint64_t x) +{ + x ^= x >> 33; + x *= 0xff51afd7ed558ccdULL; + x ^= x >> 33; + x *= 0xc4ceb9fe1a85ec53ULL; + x ^= x >> 33; + return x; +} +bool MicroProfileHashTableComparePtr(uint64_t L, uint64_t R) +{ + return L == R; +} + +bool MicroProfileHashTableSetString(MicroProfileHashTable* pTable, const char* pKey, const char* pValue) +{ + return MicroProfileHashTableSet(pTable, (uint64_t)pKey, (uintptr_t)pValue); +} + +bool MicroProfileHashTableGetString(MicroProfileHashTable* pTable, const char* pKey, const char** pValue) +{ + return MicroProfileHashTableGet(pTable, (uint64_t)pKey, (uintptr_t*)pValue); +} + +bool MicroProfileHashTableRemoveString(MicroProfileHashTable* pTable, const char* pKey) +{ + return MicroProfileHashTableRemove(pTable, (uint64_t)pKey); +} + +bool MicroProfileHashTableSetPtr(MicroProfileHashTable* pTable, const void* pKey, void* pValue) +{ + return MicroProfileHashTableSet(pTable, (uint64_t)pKey, (uintptr_t)pValue); +} + +template +bool MicroProfileHashTableGetPtr(MicroProfileHashTable* pTable, const void* pKey, T** pValue) +{ + uintptr_t Dummy; + uintptr_t* Arg = pValue ? (uintptr_t*)pValue : &Dummy; + return MicroProfileHashTableGet(pTable, (uint64_t)pKey, Arg); +} + +bool MicroProfileHashTableRemovePtr(MicroProfileHashTable* pTable, const char* pKey) +{ + return MicroProfileHashTableRemove(pTable, (uint64_t)pKey); +} + +template +T& MicroProfileArray::operator[](const uint32_t Index) +{ + return Data[Index]; +} + +template +const T& MicroProfileArray::operator[](const uint32_t Index) const +{ + MP_ASSERT(Index < Size); + return Data[Index]; +} +template +T* MicroProfileArray::begin() +{ + return Data; +} +template +T* MicroProfileArray::end() +{ + return Data + Size; +} + +template +void MicroProfileArrayInit(MicroProfileArray& Array, uint32_t InitialCapacity) +{ + MP_ASSERT(Array.Data == nullptr); + MP_ASSERT(Array.Size == 0); + MP_ASSERT(Array.Capacity == 0); + Array.Capacity = InitialCapacity; + Array.Data = MP_ALLOC_OBJECT_ARRAY(T, InitialCapacity); + Array.Size = 0; +} +template +void MicroProfileArrayDestroy(MicroProfileArray& Array, uint32_t InitialCapacity) +{ + if(Array.Data) + MP_FREE(Array.Data); + memset(Array, 0, sizeof(*Array)); +} +template +void MicroProfileArrayClear(MicroProfileArray& Array) +{ + Array.Size = 0; +} + +template +void MicroProfileArrayPushBack(MicroProfileArray& Array, const T& v) +{ + uint32_t& Size = Array.Size; + uint32_t& Capacity = Array.Capacity; + if(Size >= Capacity) + { + uint32_t NewCapacity = (MicroProfileMax(1u, Capacity) + 1) * 3 / 2; + T* NewData = MP_ALLOC_OBJECT_ARRAY(T, NewCapacity); + memcpy(NewData, Array.Data, Size * sizeof(T)); + if(Array.Data) + { + MP_FREE(Array.Data); + } + Array.Data = NewData; + Capacity = NewCapacity; + } + Array.Data[Size++] = v; +} + +void MicroProfileStringBlockFree(MicroProfileStringBlock* pBlock) +{ + MicroProfileCounterAdd(S.CounterToken_StringBlock_Count, -1); + MicroProfileCounterAdd(S.CounterToken_StringBlock_Memory, -(int64_t)(pBlock->nSize + sizeof(MicroProfileStringBlock))); + + MP_FREE(pBlock); +} +MicroProfileStringBlock* MicroProfileStringBlockAlloc(uint32_t nSize) +{ + nSize = MicroProfileMax(nSize, (uint32_t)(MicroProfileStringBlock::DEFAULT_SIZE - sizeof(MicroProfileStringBlock))); + nSize += sizeof(MicroProfileStringBlock); + MicroProfileCounterAdd(S.CounterToken_StringBlock_Count, 1); + MicroProfileCounterAdd(S.CounterToken_StringBlock_Memory, nSize); + // uprintf("alloc string block %d sizeof strings is %d\n", nSize, (int)sizeof(MicroProfileStringBlock)); + MicroProfileStringBlock* pBlock = (MicroProfileStringBlock*)MP_ALLOC(nSize, 8); + pBlock->pNext = 0; + pBlock->nSize = nSize - sizeof(MicroProfileStringBlock); + pBlock->nUsed = 0; + return pBlock; +} + +void MicroProfileStringsInit(MicroProfileStrings* pStrings) +{ + MicroProfileHashTableInit(&pStrings->HashTable, 1, 25, MicroProfileHashTableCompareString, MicroProfileHashTableHashString); + pStrings->pFirst = 0; + pStrings->pLast = 0; +} +void MicroProfileStringsDestroy(MicroProfileStrings* pStrings) +{ + MicroProfileStringBlock* pBlock = pStrings->pFirst; + while(pBlock) + { + MicroProfileStringBlock* pNext = pBlock->pNext; + MicroProfileStringBlockFree(pBlock); + pBlock = pNext; + } + MicroProfileCounterSet(S.CounterToken_StringBlock_Waste, 0); + MicroProfileCounterSet(S.CounterToken_StringBlock_Strings, 0); + + memset(pStrings, 0, sizeof(*pStrings)); +} + +const char* MicroProfileStringIntern(const char* pStr) +{ + return MicroProfileStringIntern(pStr, (uint32_t)strlen(pStr), 0); +} + +const char* MicroProfileStringInternLower(const char* pStr) +{ + return MicroProfileStringIntern(pStr, (uint32_t)strlen(pStr), ESTRINGINTERN_LOWERCASE); +} +const char* MicroProfileStringInternSlash(const char* pStr) +{ + return MicroProfileStringIntern(pStr, (uint32_t)strlen(pStr), ESTRINGINTERN_FORCEFORWARDSLASH); +} + +const char* MicroProfileStringIntern(const char* pStr_, uint32_t nLen, uint32_t nFlags) +{ + MicroProfileStrings* pStrings = &S.Strings; + const char* pStr = pStr_; + char* pLowerCaseStr = (char*)alloca(nLen + 1); + if(0 != (nFlags & (ESTRINGINTERN_FORCEFORWARDSLASH | ESTRINGINTERN_LOWERCASE))) + { + for(uint32_t i = 0; i < nLen; ++i) + { + char c = pStr[i]; + if(nFlags & ESTRINGINTERN_LOWERCASE) + { + c = tolower(c); + } + if(nFlags & ESTRINGINTERN_FORCEFORWARDSLASH) + { + if(c == '\\') + c = '/'; + } + pLowerCaseStr[i] = c; + } + pLowerCaseStr[nLen] = '\0'; + pStr = pLowerCaseStr; + } + const char* pRet; + if(MicroProfileHashTableGetString(&pStrings->HashTable, pStr, &pRet)) + { + if(0 != strcmp(pStr, pRet)) + { + MP_BREAK(); + } + return pRet; + } + else + { + if(pStr[nLen] != '\0') + MP_BREAK(); // string should be 0 terminated. + nLen += 1; + MicroProfileStringBlock* pBlock = pStrings->pLast; + if(0 == pBlock || pBlock->nUsed + nLen > pBlock->nSize) + { + MicroProfileStringBlock* pNewBlock = MicroProfileStringBlockAlloc(nLen); + if(pBlock) + { + pBlock->pNext = pNewBlock; + pStrings->pLast = pNewBlock; + MicroProfileCounterAdd(S.CounterToken_StringBlock_Waste, pBlock->nSize - pBlock->nUsed); + } + else + { + pStrings->pLast = pStrings->pFirst = pNewBlock; + } + pBlock = pNewBlock; + } + MicroProfileCounterAdd(S.CounterToken_StringBlock_Strings, 1); + char* pDest = &pBlock->Memory[pBlock->nUsed]; + pBlock->nUsed += nLen; + MP_ASSERT(pBlock->nUsed <= pBlock->nSize); + memcpy(pDest, pStr, nLen); + MicroProfileHashTableSetString(&pStrings->HashTable, pDest, pDest); + +#if 0 + void DumpTableStr(MicroProfileHashTable* pTable); + DumpTableStr(&pStrings->HashTable); +#endif + + return pDest; + } +} + +void DumpTable(MicroProfileHashTable* pTable) +{ + for(uint32_t i = 0; i < pTable->nAllocated; ++i) + { + if(pTable->pEntries[i].Hash != 0) + { + uprintf("[%05d,%05" PRIu64 "] ::::%" PRIx64 ", %p .. hash %" PRIx64 "\n", + i, + pTable->pEntries[i].Hash % pTable->nAllocated, + pTable->pEntries[i].Key, + (void*)pTable->pEntries[i].Value, + pTable->pEntries[i].Hash); + } + } +}; +void DumpTableStr(MicroProfileHashTable* pTable) +{ + int c = 0; + (void)c; + for(uint32_t i = 0; i < pTable->nAllocated; ++i) + { + if(pTable->pEntries[i].Hash != 0) + { + uprintf("%03d [%05d,%05" PRIu64 "] ::::%s, %s .. hash %" PRIx64 "\n", + c++, + i, + pTable->pEntries[i].Hash % pTable->nAllocated, + (const char*)pTable->pEntries[i].Key, + (const char*)pTable->pEntries[i].Value, + pTable->pEntries[i].Hash); + } + } + uprintf("FillPrc %f\n", 100.f * c / (float)pTable->nAllocated); +}; + +static const char* txt[] = { "gaudy", "chilly", "obtain", "suspend", "jelly", "peel", "nauseating", "complain", "cave", "practise", "sail", "close", + "drawer", "mature", "impossible", "exist", "sister", "poke", "ancient", "paddle", "ask", "shallow", "outrageous", "healthy", + "reading", "obey", "water", "elbow", "abnormal", "trap", "wholesale", "lovely", "stupid", "comparison", "swim", "brash", + "towering", "accept", "invention", "plantation", "spooky", "tiger", "knot", "literate", "awake", "itch", "medical", "ticket", + "tawdry", "correct", "mine", "accidental", "dinner", "produce", "protective", "red", "dreary", "toe", "drain", "zesty", + "inform", "boundless", "ghost", "attend", "rely", "fill", "liquid", "pump", "continue", "spark", "church", "fortunate", + "truthful", "conscious", "possible", "motion", "evanescent", "branch", "skirt", "number", "meek", "hour", "form", "work", + "car", "post", "talk", "fear", "tightfisted", "dress", "perform", "fry", "courageous", "dysfunctional", "page", "one", + "annoy", "abrasive", "dependent", "payment" }; + +void MicroProfileStringInternTest() +{ + MicroProfileStringsInit(&S.Strings); + uint32_t nCount = sizeof(txt) / sizeof(txt[0]); + const char* pStrings[100]; + const char* pStrings2[100]; + + DumpTableStr(&S.Strings.HashTable); + for(uint32_t i = 0; i < nCount; ++i) + { + pStrings[i] = MicroProfileStringIntern(txt[i]); + pStrings2[i] = MicroProfileStrDup(txt[i]); + } + + for(uint32_t i = 0; i < nCount; ++i) + { + const char* pStr = MicroProfileStringIntern(pStrings2[i]); + if(pStr != pStrings[i]) + { + MP_BREAK(); + } + } + DumpTableStr(&S.Strings.HashTable); + + MicroProfileStringsDestroy(&S.Strings); +} + +void MicroProfileHashTableTest() +{ + MicroProfileStringInternTest(); + + MicroProfileHashTable T; + MicroProfileHashTable* pTable = &T; + MicroProfileHashTableInit(pTable, 1, 100, 0, 0); + +#define NUM_ITEMS 100 + + uint64_t Keys[NUM_ITEMS]; + uint64_t Values[NUM_ITEMS]; + memset(Keys, 0xff, sizeof(Keys)); + memset(Values, 0xff, sizeof(Values)); + + static int l = 0; + auto RR = [&]() -> uint64_t + { + if(l++ % 4 < 2) + { + return l; + } + uint64_t l2 = rand(); + uint64_t u = rand(); + return l2 | (u << 32); + }; + auto RRUnique = [&]() + { + bool bFound = false; + uint64_t V = 0; + do + { + V = RR(); + for(uint32_t i = 0; i != NUM_ITEMS; ++i) + { + if(V == Keys[i]) + { + bFound = true; + } + } + if(!bFound) + { + return V; + } + } while(bFound); + MP_BREAK(); + return (uint64_t)0; + }; + + Keys[0] = 0; + Values[0] = 42; + for(uint32_t i = 1; i < NUM_ITEMS; ++i) + { + Keys[i] = RRUnique(); + Values[i] = RR(); + } + + for(uint32_t i = 0; i < NUM_ITEMS; ++i) + { + MicroProfileHashTableSet(pTable, Keys[i], Values[i]); + } + + for(uint32_t i = 0; i < NUM_ITEMS; ++i) + { + uintptr_t V; + if(MicroProfileHashTableGet(pTable, Keys[i], &V)) + { + if(V != Values[i]) + { + MP_BREAK(); + } + } + else + { + MP_BREAK(); + } + uint64_t nonkey = RRUnique(); + if(MicroProfileHashTableGet(pTable, nonkey, &V)) + { + MP_BREAK(); + } + } + + DumpTable(pTable); + if(!MicroProfileHashTableRemove(pTable, 0)) + { + MP_BREAK(); + } + uprintf("removed\n"); + DumpTable(pTable); + uintptr_t v; + if(MicroProfileHashTableGet(pTable, 0, &v)) + { + MP_BREAK(); + } + if(MicroProfileHashTableGet(pTable, 1, &v)) + { + if(v != 2) + MP_BREAK(); + } + + MicroProfileHashTableDestroy(pTable); + + MicroProfileHashTable Strings; + MicroProfileHashTableInit(&Strings, 1, 25, MicroProfileHashTableCompareString, MicroProfileHashTableHashString); + uint32_t nCount = sizeof(txt) / sizeof(txt[0]); + for(uint32_t i = 0; i < nCount; i += 2) + { + MicroProfileHashTableSetString(&Strings, txt[i], txt[i + 1]); + } + DumpTableStr(&Strings); + + for(uint32_t i = 0; i < nCount; i += 2) + { + const char* pKey = txt[i]; + const char* pValue = txt[i + 1]; + const char* pRes = 0; + if(MicroProfileHashTableGetString(&Strings, pKey, &pRes)) + { + if(pRes != pValue) + { + MP_BREAK(); + } + } + else + { + MP_BREAK(); + } + } + uint32_t nRem = nCount / 2; + for(uint32_t i = 0; i < nRem; i += 2) + { + const char* pKey = txt[i]; + const char* pValue = txt[i + 1]; + + if(!MicroProfileHashTableRemoveString(&Strings, pKey)) + { + MP_BREAK(); + } + if(MicroProfileHashTableRemoveString(&Strings, pValue)) + { + MP_BREAK(); + } + } + for(uint32_t i = 0; i < nRem; i += 2) + { + const char* pKey = txt[i]; + if(MicroProfileHashTableRemoveString(&Strings, pKey)) + { + MP_BREAK(); + } + } + + for(uint32_t i = 0; i < nCount; i += 2) + { + const char* pKey = txt[i]; + const char* pValue = txt[i + 1]; + const char* V; + if(MicroProfileHashTableGetString(&Strings, pKey, &V)) + { + if(i < nRem) + { + MP_BREAK(); + } + else + { + if(V != pValue) + MP_BREAK(); + } + } + else + { + if(i >= nRem) + MP_BREAK(); + } + } + + DumpTableStr(&Strings); + MicroProfileHashTableDestroy(&Strings); +} + +uint32_t MicroProfileGetColor(uint32_t TimerIndex) +{ + MicroProfileTimerInfo& TI = S.TimerInfo[TimerIndex]; + if(TI.nColor == MP_AUTO) + { + return MicroProfileColorFromString(TI.pName); + } + else + { + return TI.nColor; + } +} + +#if MICROPROFILE_IMGUI +#include "imgui.h" +#ifndef MICROPROFILE_IMGUI_MAX_GRAPHS +#define MICROPROFILE_IMGUI_MAX_GRAPHS 64 +#endif + +#define MICROPROFILE_IMGUI_GRAPH_SIZE 256 + +struct MicroProfileImguiTimerState +{ + int TimerIndex = -1; + uint64_t FrameFetched = (uint64_t)-1; + uint32_t nColor = 0; + float fValues[MICROPROFILE_IMGUI_GRAPH_SIZE]; +}; + +struct MicroProfileImguiState +{ + MicroProfileImguiTimerState Timers[MICROPROFILE_IMGUI_MAX_GRAPHS]; + uint32_t NumTimers = 0; + uint32_t GraphPut; +}; + +static MicroProfileImguiState ImguiState; + +void MicroProfileImguiGather() +{ + MICROPROFILE_SCOPEI("MicroProfile", "ImguiGather", MP_AUTO); + uint32_t Put = ImguiState.GraphPut; + for(uint32_t i = 0; i < ImguiState.NumTimers; ++i) + { + MicroProfileImguiTimerState* pGraphInfo = &ImguiState.Timers[i]; + uint64_t Ticks = S.Frame[pGraphInfo->TimerIndex].nTicks; + float fToMs = S.TimerInfo[pGraphInfo->TimerIndex].Type == MicroProfileTokenTypeGpu ? MicroProfileTickToMsMultiplierGpu() : MicroProfileTickToMsMultiplierCpu(); + pGraphInfo->fValues[Put] = fToMs * Ticks; + } + ImguiState.GraphPut = (ImguiState.GraphPut + 1) % MICROPROFILE_IMGUI_GRAPH_SIZE; +} + +uint32_t MicroProfileImGuiColor(uint32_t Color) +{ + uint32_t A = 0xff; + uint32_t R = 0xff & (Color >> 16); + uint32_t G = 0xff & (Color >> 8); + uint32_t B = 0xff & (Color); + + return (A << IM_COL32_A_SHIFT) | (R << IM_COL32_R_SHIFT) | (G << IM_COL32_G_SHIFT) | (B << IM_COL32_B_SHIFT); +} + +void MicroProfileImguiControls() +{ + using namespace ImGui; + uint32_t IdCounter = 42; + { + PushID(IdCounter++); + int Aggr = MicroProfileGetAggregateFrames(); + Text("Aggregate Frames %7d", MicroProfileGetCurrentAggregateFrames()); + SameLine(); + if(RadioButton("Inf", Aggr == 0)) + MicroProfileSetAggregateFrames(0); + int AggrFrameOptions[] = { + 30, + 60, + 100, + 1000, + }; + for(int i = 0; i < sizeof(AggrFrameOptions) / sizeof(AggrFrameOptions[0]); ++i) + { + int v = AggrFrameOptions[i]; + char Buffer[32]; + stbsp_snprintf(Buffer, sizeof(Buffer) - 1, "%d", v); + SameLine(); + if(RadioButton(Buffer, Aggr == v)) + MicroProfileSetAggregateFrames(v); + } + + if(Aggr == 0) + { + if(Button("Clear Inf Aggregate")) + S.nAggregateClear = 1; + } + + PopID(); + } + Separator(); + { + PushID(IdCounter++); + Text("Categories"); + if(BeginTable("CategoryTable", 3, 0)) + { + TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch); + TableSetupColumn("On", ImGuiTableColumnFlags_WidthFixed, 70); + TableSetupColumn("Off", ImGuiTableColumnFlags_WidthFixed, 70); + for(uint32_t i = 0; i < S.nCategoryCount; ++i) + { + PushID(i); + TableNextRow(); + TableSetColumnIndex(0); + Text(S.CategoryInfo[i].pName); + bool bEnabled = MicroProfileCategoryEnabled(i); + bool bDisabled = MicroProfileCategoryDisabled(i); + TableSetColumnIndex(1); + if(RadioButton("On", bEnabled)) + MicroProfileEnableCategory(S.CategoryInfo[i].pName); + TableSetColumnIndex(2); + if(RadioButton("Off", bDisabled)) + MicroProfileDisableCategory(S.CategoryInfo[i].pName); + + PopID(); + } + EndTable(); + } + PopID(); + } + Separator(); + { + PushID(IdCounter++); + Text("Groups"); + if(BeginTable("GroupTable", 3, 0)) + { + TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch); + TableSetupColumn("On", ImGuiTableColumnFlags_WidthFixed, 70); + TableSetupColumn("Off", ImGuiTableColumnFlags_WidthFixed, 70); + + for(uint32_t i = 0; i < S.nGroupCount; ++i) + { + TableNextRow(); + PushID(i); + const char* pName = S.GroupInfo[i].pName; + bool bEnabled = MicroProfileGroupEnabled(i); + TableSetColumnIndex(0); + Text(pName); + TableSetColumnIndex(1); + if(RadioButton("On", bEnabled)) + MicroProfileToggleGroup(i); + TableSetColumnIndex(2); + if(RadioButton("Off", !bEnabled)) + MicroProfileToggleGroup(i); + PopID(); + } + EndTable(); + } + PopID(); + } +} + +MicroProfileImguiTimerState* MicroProfileImguiGetTimerState(int TimerIndex) +{ + MicroProfileImguiTimerState* ptr = nullptr; + for(uint32_t i = 0; i < ImguiState.NumTimers; ++i) + if(ImguiState.Timers[i].TimerIndex == TimerIndex) + return &ImguiState.Timers[i]; + + if(ImguiState.NumTimers < MICROPROFILE_IMGUI_MAX_GRAPHS) + { + MicroProfileImguiTimerState* pState = &ImguiState.Timers[ImguiState.NumTimers++]; + pState->TimerIndex = TimerIndex; + pState->nColor = MicroProfileGetColor(TimerIndex); + memset(&pState->fValues[0], 0, sizeof(pState->fValues)); + return pState; + } + + return nullptr; +} + +void MicroProfileImguiTable(const MicroProfileImguiWindowDesc& Window, const MicroProfileImguiEntryDesc* Entries, uint32_t NumEntries) +{ + using namespace ImGui; + const uint32_t NumColumns = 6; + ImGuiIO& io = ImGui::GetIO(); + + float Padding = GetStyle().CellPadding.x * 2; + float GroupWidth = CalcTextSize("Group").x; + float NameWidth = CalcTextSize("Name").x; + float BaseWidth = CalcTextSize("100000.00").x; + float Height = CalcTextSize("G").y + Padding; + + for(uint32_t i = 0; i < NumEntries; ++i) + { + uint32_t TimerIndex = MicroProfileGetTimerIndex(Entries[i].GraphTimer); + const MicroProfileTimerInfo& TI = S.TimerInfo[TimerIndex]; + const MicroProfileGroupInfo& GI = S.GroupInfo[TI.nGroupIndex]; + GroupWidth = MicroProfileMax(GroupWidth, CalcTextSize(GI.pName).x); + NameWidth = MicroProfileMax(NameWidth, CalcTextSize(TI.pName).x); + } + + float TableWidth = GroupWidth + NameWidth + BaseWidth * 4 + NumColumns * Padding + (NumColumns - 1) * GetStyle().ItemSpacing.x; + float TableHeight = Height * (NumEntries + 1); + + ImVec2 TablePos = ImVec2(0.f, 0.f); + if(Window.Align == MICROPROFILE_IMGUI_ALIGN_TOP_RIGHT || Window.Align == MICROPROFILE_IMGUI_ALIGN_BOTTOM_RIGHT) + TablePos.x = io.DisplaySize.x - TableWidth; + + if(Window.Align == MICROPROFILE_IMGUI_ALIGN_BOTTOM_LEFT || Window.Align == MICROPROFILE_IMGUI_ALIGN_BOTTOM_RIGHT) + TablePos.y = io.DisplaySize.y - TableHeight; + TablePos.x += Window.OffsetX; + TablePos.y += Window.OffsetY; + + SetCursorScreenPos(TablePos); + + if(BeginTable("MicroProfileImguiTable", NumColumns, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX)) + { + TableSetupColumn("Group", ImGuiTableColumnFlags_WidthFixed, GroupWidth); + TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, NameWidth); + + TableSetupColumn("Max", ImGuiTableColumnFlags_WidthFixed, BaseWidth); + TableSetupColumn("Min", ImGuiTableColumnFlags_WidthFixed, BaseWidth); + TableSetupColumn("Avg", ImGuiTableColumnFlags_WidthFixed, BaseWidth); + TableSetupColumn("Time", ImGuiTableColumnFlags_WidthFixed, BaseWidth); + + TableHeadersRow(); + + for(uint32_t i = 0; i < NumEntries; ++i) + { + uint32_t TimerIndex = MicroProfileGetTimerIndex(Entries[i].GraphTimer); + + MicroProfileTimerValues Values; + MicroProfileCalcTimers(TimerIndex, Values); + const MicroProfileTimerInfo& TI = S.TimerInfo[TimerIndex]; + const MicroProfileGroupInfo& GI = S.GroupInfo[TI.nGroupIndex]; + TableNextRow(); + ImU32 RowBGColor = GetColorU32((i % 2) ? ImVec4(0.1f, 0.1f, 0.1f, 0.85f) : ImVec4(0.2f, 0.2f, 0.2f, 0.85f)); + TableSetBgColor(ImGuiTableBgTarget_RowBg1, RowBGColor); + PushID(i); + float fMax = 0.f, fMin = 0.f, fAvg = 0.f, fTime = 0.f; + + auto RightAlignedFloat = [](float f) + { + float CellWidth = GetContentRegionAvail().x; + char Buffer[32]; + stbsp_snprintf(Buffer, sizeof(Buffer) - 1, "%.2f", f); + ImVec2 TextSize = CalcTextSize(Buffer); + SetCursorPosX(GetCursorPosX() + (CellWidth - TextSize.x)); + TextUnformatted(Buffer); + }; + + TableSetColumnIndex(0); + Text(GI.pName); + TableSetColumnIndex(1); + Text(TI.pName); + TableSetColumnIndex(2); + RightAlignedFloat(Values.MaxMs); + TableSetColumnIndex(3); + RightAlignedFloat(Values.MinMs); + TableSetColumnIndex(4); + RightAlignedFloat(Values.AverageMs); + TableSetColumnIndex(5); + RightAlignedFloat(Values.TimeMs); + PopID(); + } + EndTable(); + } +} + +void MicroProfileImguiGraphs(const MicroProfileImguiWindowDesc& Window, const MicroProfileImguiEntryDesc* Entries, uint32_t NumEntries) +{ + using namespace ImGui; + ImGuiIO& io = ImGui::GetIO(); + uint32_t Width = Window.GraphWidth; + uint32_t Height = (Window.GraphHeight + GetStyle().ItemSpacing.y) * NumEntries; + + ImVec2 Pos = ImVec2(0.f, 0.f); + if(Window.Align == MICROPROFILE_IMGUI_ALIGN_TOP_RIGHT || Window.Align == MICROPROFILE_IMGUI_ALIGN_BOTTOM_RIGHT) + Pos.x = io.DisplaySize.x - Width; + + if(Window.Align == MICROPROFILE_IMGUI_ALIGN_BOTTOM_LEFT || Window.Align == MICROPROFILE_IMGUI_ALIGN_BOTTOM_RIGHT) + Pos.y = io.DisplaySize.y - Height; + + Pos.x += Window.OffsetX; + Pos.y += Window.OffsetY; + for(uint32_t i = 0; i < NumEntries; ++i) + { + SetCursorScreenPos(Pos); + uint32_t TimerIndex = MicroProfileGetTimerIndex(Entries[i].GraphTimer); + float GraphMax = Entries[i].GraphMax; + const MicroProfileTimerInfo& TI = S.TimerInfo[TimerIndex]; + + MicroProfileImguiTimerState* TimerState = MicroProfileImguiGetTimerState(TimerIndex); + + PushID(i << 16 | TimerIndex); + if(TimerState->nColor == 0) + TimerState->nColor = MicroProfileGetColor(TimerIndex); + ImVec4 FrameBg = GetStyleColorVec4(ImGuiCol_FrameBg); + FrameBg.x = 0.15f; + FrameBg.y = 0.15f; + FrameBg.z = 0.15f; + FrameBg.w = 0.8f; + + PushStyleColor(ImGuiCol_PlotLines, MicroProfileImGuiColor(TimerState->nColor)); + PushStyleColor(ImGuiCol_FrameBg, FrameBg); + uint32_t Start = (ImguiState.GraphPut) % MICROPROFILE_IMGUI_GRAPH_SIZE; + uint32_t Last = (ImguiState.GraphPut + MICROPROFILE_IMGUI_GRAPH_SIZE - 1) % MICROPROFILE_IMGUI_GRAPH_SIZE; + PlotLines("", &TimerState->fValues[0], MICROPROFILE_IMGUI_GRAPH_SIZE, Start, nullptr, 0.f, GraphMax, ImVec2(Window.GraphWidth, Window.GraphHeight)); + + char TimeStr[32]; + stbsp_snprintf(TimeStr, sizeof(TimeStr) - 1, "%.3fms", TimerState->fValues[Last]); + ImVec2 PlotMin = GetItemRectMin(); + ImVec2 PlotMax = GetItemRectMax(); + ImVec2 NameSize = CalcTextSize(TI.pName); + ImVec2 NamePos = ImVec2(PlotMin.x + 1, PlotMax.y - NameSize.y - 1); + ImVec2 TimeSize = CalcTextSize(TimeStr); + ImVec2 TimePos = ImVec2(PlotMax.x - TimeSize.x - 1, PlotMax.y - TimeSize.y - 1); + GetWindowDrawList()->AddText(NamePos, GetColorU32(ImGuiCol_Text), TI.pName); + GetWindowDrawList()->AddText(TimePos, GetColorU32(ImGuiCol_Text), TimeStr); + + PopStyleColor(); + PopStyleColor(); + PopID(); + Pos.y += Window.GraphHeight + GetStyle().ItemSpacing.y; + } +} + +#endif + +#undef uprintf + +#undef S +#ifdef _WIN32 +#pragma warning(pop) +#undef microprofile_fopen_helper +#endif + +#ifdef MICROPROFILE_PS4 +#define MICROPROFILE_PS4_IMPL +#include "microprofile_ps4.h" +#endif +#ifdef MICROPROFILE_XBOXONE +#define MICROPROFILE_XBOXONE_IMPL +#include "microprofile_xboxone.h" +#endif + +#endif // #if MICROPROFILE_ENABLED + +#include "microprofile_html.h" +#include "microprofile_icons.h" + +#endif \ No newline at end of file diff --git a/Windows_Libs/Dev/Render/microprofile/microprofile.h b/Windows_Libs/Dev/Render/microprofile/microprofile.h new file mode 100644 index 0000000..1f1dfa0 --- /dev/null +++ b/Windows_Libs/Dev/Render/microprofile/microprofile.h @@ -0,0 +1,2058 @@ +#pragma once +// MIT License +// +// Copyright (c) 2023 Jonas Meyer +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// *********************************************************************** + +#ifndef MICROPROFILE_USE_CONFIG +#define MICROPROFILE_USE_CONFIG 1 +#endif + +#if MICROPROFILE_USE_CONFIG +#include "microprofile.config.h" +#endif + +#ifndef MICROPROFILE_ENABLED +#define MICROPROFILE_ENABLED 1 +#endif + +#ifndef MICROPROFILE_DYNAMIC_INSTRUMENT +#if(defined(__APPLE__) && defined(__MACH__)) || (defined(_WIN32) && defined(_M_X64)) || (defined(__unix__) && defined(__x86_64__)) +#ifdef MICROPROFILE_DYNAMIC_INSTRUMENT_ENABLE +#define MICROPROFILE_DYNAMIC_INSTRUMENT 1 +#endif +#endif +#endif + +#ifndef MICROPROFILE_DYNAMIC_INSTRUMENT +#define MICROPROFILE_DYNAMIC_INSTRUMENT 0 +#endif + +#ifndef MICROPROFILE_GPU_TIMERS_D3D11 +#define MICROPROFILE_GPU_TIMERS_D3D11 0 +#endif + +#ifndef MICROPROFILE_GPU_TIMERS_GL +#define MICROPROFILE_GPU_TIMERS_GL 0 +#endif + +#ifndef MICROPROFILE_GPU_TIMERS_D3D12 +#define MICROPROFILE_GPU_TIMERS_D3D12 0 +#endif + +#ifndef MICROPROFILE_BIG_ENDIAN +#define MICROPROFILE_BIG_ENDIAN 0 +#endif + +#ifndef MICROPROFILE_GPU_TIMERS_VULKAN +#define MICROPROFILE_GPU_TIMERS_VULKAN 0 +#endif + +#ifndef MICROPROFILE_LEGACY_CSV // set this if you rely on the legacy csv dump format. (single file with multiple csv sections) +#define MICROPROFILE_LEGACY_CSV 0 +#endif + +#ifndef MICROPROFILE_FRAME_EXTRA_DATA // Set this to always allocate frame extra data. This is useful if you're doing profiling on platform where there is plenty of memory. +#define MICROPROFILE_FRAME_EXTRA_DATA 0 +#endif + +#ifndef MICROPROFILE_IMGUI +#define MICROPROFILE_IMGUI 0 +#endif + +#ifndef MICROPROFILE_VERIFY_BALANCED +#define MICROPROFILE_VERIFY_BALANCED 0 +#endif + +#ifndef MICROPROFILE_ONCE +#define MICROPROFILE_ONCE + +#include + +typedef uint64_t MicroProfileToken; +typedef uint16_t MicroProfileGroupId; +typedef uint32_t MicroProfileTimelineToken; + +#if 0 == MICROPROFILE_ENABLED + +#define MICROPROFILE_DECLARE(var) +#define MICROPROFILE_DEFINE(var, group, name, color) +#define MICROPROFILE_REGISTER_GROUP(group, color, category) +#define MICROPROFILE_DECLARE_GPU(var) +#define MICROPROFILE_DEFINE_GPU(var, name, color) +#define MICROPROFILE_SCOPE(var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SCOPEI(group, name, color) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SCOPE_CSTR(cstr) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SCOPE_TOKEN(token) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SCOPEGPU_TOKEN(token) +#define MICROPROFILE_SCOPEGPU(var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SCOPEGPUI(name, color) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SCOPEGPU_CSTR(cstr) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SCOPEGPU_TOKEN_L(Log, token) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SCOPEGPU_L(Log, var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SCOPEGPUI_L(Log, name, color) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SCOPEGPU_CSTR_L(Log, cstr) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SECTIONGPU_TOKEN(token) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SECTIONGPU(var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SECTIONGPUI(name, color) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SECTIONGPU_TOKEN_L(Log, token) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SECTIONGPU_L(Log, var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_SECTIONGPUI_L(Log, name, color) \ + do \ + { \ + } while(0) + +#define MICROPROFILE_CONDITIONAL_SCOPEI(condition, group, name, color) \ + do \ + { \ + } while(0) +#define MICROPROFILE_ENTER(var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_ENTER_TOKEN(var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_ENTERI(group, name, color) \ + do \ + { \ + } while(0) +#define MICROPROFILE_LEAVE() \ + do \ + { \ + } while(0) + +#define MICROPROFILE_GPU_ENTER(var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_ENTER_TOKEN(token) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_ENTERI(group, name, color) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_LEAVE() \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_ENTER_L(Log, var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_ENTER_TOKEN_L(Log, token) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_ENTERI_L(Log, name, color) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_LEAVE_L(Log) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_INIT_QUEUE(QueueName) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_GET_QUEUE(QueueName) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_BEGIN(pContext, pLog) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_SET_CONTEXT(pContext, pLog) \ + do \ + { \ + } while(0) +#define MICROPROFILE_GPU_END(pLog) 0 +#define MICROPROFILE_GPU_SUBMIT(Queue, Work) \ + do \ + { \ + } while(0) + +#define MICROPROFILE_TIMELINE_TOKEN(token) \ + do \ + { \ + } while(0) +#define MICROPROFILE_TIMELINE_ENTER(token, color, name) \ + do \ + { \ + } while(0) +#define MICROPROFILE_TIMELINE_ENTERF(token, color, fmt, ...) \ + do \ + { \ + } while(0) +#define MICROPROFILE_TIMELINE_LEAVE(token) \ + do \ + { \ + } while(0) +#define MICROPROFILE_TIMELINE_ENTER_STATIC(color, name) \ + do \ + { \ + } while(0) +#define MICROPROFILE_TIMELINE_LEAVE_STATIC(name) \ + do \ + { \ + } while(0) +#define MICROPROFILE_TIMELINE_SCOPE(...) \ + do \ + { \ + } while(0) +#define MICROPROFILE_THREADLOGGPURESET(a) \ + do \ + { \ + } while(0) +#define MICROPROFILE_META_CPU(name, count) +#define MICROPROFILE_META_GPU(name, count) +#define MICROPROFILE_FORCEENABLECPUGROUP(s) \ + do \ + { \ + } while(0) +#define MICROPROFILE_FORCEDISABLECPUGROUP(s) \ + do \ + { \ + } while(0) +#define MICROPROFILE_FORCEENABLEGPUGROUP(s) \ + do \ + { \ + } while(0) +#define MICROPROFILE_FORCEDISABLEGPUGROUP(s) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_ADD(name, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_SUB(name, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_SET(name, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_SET_DOUBLE(name, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_SET_INT32_PTR(name, ptr) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_SET_INT64_PTR(name, ptr) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_CLEAR_PTR(name) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_SET_LIMIT(name, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_CONDITIONAL(expr) +#define MICROPROFILE_COUNTER_CONFIG(name, type, limit, flags) +#define MICROPROFILE_COUNTER_CONFIG_ONCE(name, type, limit, flags) +#define MICROPROFILE_DECLARE_LOCAL_COUNTER(var) +#define MICROPROFILE_DEFINE_LOCAL_COUNTER(var, name) +#define MICROPROFILE_DECLARE_LOCAL_ATOMIC_COUNTER(var) +#define MICROPROFILE_DEFINE_LOCAL_ATOMIC_COUNTER(var, name) +#define MICROPROFILE_COUNTER_LOCAL_ADD(var, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_LOCAL_SUB(var, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_LOCAL_SET(var, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_LOCAL_UPDATE_ADD(var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_LOCAL_UPDATE_SET(var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_LOCAL_ADD_ATOMIC(var, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_LOCAL_SUB_ATOMIC(var, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_LOCAL_SET_ATOMIC(var, count) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_LOCAL_UPDATE_ADD_ATOMIC(var) \ + do \ + { \ + } while(0) +#define MICROPROFILE_COUNTER_LOCAL_UPDATE_SET_ATOMIC(var) \ + do \ + { \ + } while(0) + +#define MICROPROFILE_COUNTER_GET_TOKEN(name, f) 0 +#define MICROPROFILE_COUNTER_TOKEN_ADD(v, count) do{}while(0) +#define MICROPROFILE_COUNTER_TOKEN_SUB(v, count) do{}while(0) +#define MICROPROFILE_COUNTER_TOKEN_SET(v, count) do{}while(0) + + +#define MicroProfileStartAutoFlip(nHz) \ + do \ + { \ + } while(0) +#define MicroProfileStopAutoFlip() \ + do \ + { \ + } while(0) +#define MicroProfileEnableFrameExtraCounterData() \ + do \ + { \ + } while(0) +#define MicroProfileGetTime(group, name) 0.f +#define MicroProfileOnThreadCreate(foo) \ + do \ + { \ + } while(0) +#define MicroProfileOnThreadExit() \ + do \ + { \ + } while(0) +#define MicroProfileFlip(...) \ + do \ + { \ + } while(0) +#define MicroProfileFlip_CB(pContext, FreezeCB, Flag) \ + do \ + { \ + } while(0) +#define MicroProfileSetAggregateFrames(a) \ + do \ + { \ + } while(0) +#define MicroProfileGetAggregateFrames() 0 +#define MicroProfileGetCurrentAggregateFrames() 0 +#define MicroProfileTogglePause() \ + do \ + { \ + } while(0) +#define MicroProfileToggleAllGroups() \ + do \ + { \ + } while(0) +#define MicroProfileDumpTimers() \ + do \ + { \ + } while(0) +#define MicroProfileShutdown() \ + do \ + { \ + } while(0) +#define MicroProfileSetForceEnable(a) \ + do \ + { \ + } while(0) +#define MicroProfileGetForceEnable() false +#define MicroProfileSetEnableAllGroups(b) \ + do \ + { \ + } while(0) +#define MicroProfileEnableCategory(a) \ + do \ + { \ + } while(0) +#define MicroProfileDisableCategory(a) \ + do \ + { \ + } while(0) +#define MicroProfileGetEnableAllGroups() false +#define MicroProfileSetForceMetaCounters(a) +#define MicroProfileGetForceMetaCounters() 0 +#define MicroProfileEnableMetaCounter(c) \ + do \ + { \ + } while(0) +#define MicroProfileDisableMetaCounter(c) \ + do \ + { \ + } while(0) +#define MicroProfileDumpFile(...) \ + do \ + { \ + } while(0) +#define MicroProfileDumpFileImmediately(...) \ + do \ + { \ + } while(0) +#define MicroProfileStartContextSwitchTrace() \ + do \ + { \ + } while(0) +#define MicroProfileGpuInsertTimeStamp(a) 1 +#define MicroProfileGpuGetTimeStamp(a) 0 +#define MicroProfileTicksPerSecondGpu() 1 +#define MicroProfileGetGpuTickReference(a, b) 0 +#define MicroProfileGpuInitD3D12(pDevice, nNodeCount, pCommandQueue, pCopyQueue) \ + do \ + { \ + } while(0) +#define MicroProfileGpuInitD3D11(pDevice, pDeviceContext) \ + do \ + { \ + } while(0) +#define MicroProfileGpuShutdown() \ + do \ + { \ + } while(0) +#define MicroProfileGpuShutdownPlatform() \ + do \ + { \ + } while(0) +#define MicroProfileGpuInitGL() \ + do \ + { \ + } while(0) +#define MicroProfileSetCurrentNodeD3D12(nNode) \ + do \ + { \ + } while(0) +#define MicroProfilePlatformMarkersGetEnabled() 0 +#define MicroProfilePlatformMarkersSetEnabled(bEnabled) \ + do \ + { \ + } while(0) +#define MicroProfileTickToMsMultiplierCpu() 1.f +#define MicroProfileTickToMsMultiplierGpu() 0.f +#define MicroProfileTicksPerSecondCpu() 1 +#define MicroProfileTick() 0 +#define MicroProfileEnabled() 0 + +#define MicroProfileCounterString nullptr +#define MicroProfileCounterTokenTree 0 +#define MicroProfileCounterTokenTreeDynamic 0 + +#define MicroProfileCsvConfigEnd() +#define MicroProfileCsvConfigBegin(...) +#define MicroProfileCsvConfigAddTimer(...) +#define MicroProfileCsvConfigAddGroup(...) +#define MicroProfileCsvConfigAddCounter(...) +#define MicroProfileUpdateSettingsPath(...) + +#define MicroProfileImguiGraphs(...) \ + do \ + { \ + } while(0) +#define MicroProfileImguiTable(...) \ + do \ + { \ + } while(0) +#define MicroProfileImguiControls(...) \ + do \ + { \ + } while(0) + +#else + +#include + +#ifdef MICROPROFILE_EXPORT +#include "microprofile.export.h" +#else +#ifndef MICROPROFILE_API +#define MICROPROFILE_API +#endif +#endif + +#ifdef MICROPROFILE_PS4 +#include "microprofile_ps4.h" +#endif +#ifdef MICROPROFILE_XBOXONE +#include "microprofile_xboxone.h" +#endif + +#ifdef _WIN32 +typedef uint32_t MicroProfileThreadIdType; +#else +#ifdef MICROPROFILE_THREADID_SIZE_4BYTE +typedef uint32_t MicroProfileThreadIdType; +#elif MICROPROFILE_THREADID_SIZE_8BYTE +typedef uint64_t MicroProfileThreadIdType; +#else +typedef uint64_t MicroProfileThreadIdType; +#endif +#endif + +typedef void (*MicroProfileOnFreeze)(int nFrozen); + +#define MICROPROFILE_TOKEN_PASTE0(a, b) a##b +#define MICROPROFILE_TOKEN_PASTE(a, b) MICROPROFILE_TOKEN_PASTE0(a, b) + +#define MICROPROFILE_DECLARE(var) extern MicroProfileToken g_mp_##var +#define MICROPROFILE_DECLARE_GPU(var) extern MicroProfileToken g_mpGPU_##var + +#define MICROPROFILE_DECLARE_SECTION(var) extern MicroProfileToken g_mp_##var +#define MICROPROFILE_DECLARE_SECTION_GPU(var) extern MicroProfileToken g_mpGPU_##var + +#define MICROPROFILE_DEFINE(var, group, name, color) MicroProfileToken g_mp_##var = MicroProfileGetToken(group, name, color, MicroProfileTokenTypeCpu, 0) +#define MICROPROFILE_DEFINE_GPU(var, name, color) MicroProfileToken g_mpGPU_##var = MicroProfileGetToken("GPU", name, color, MicroProfileTokenTypeGpu, 0) + +#define MICROPROFILE_DEFINE_SECTION(var, name, color) MicroProfileToken g_mp_##var = MicroProfileGetToken("__SECTION", name, color, MicroProfileTokenTypeCpu, MICROPROFILE_TIMER_FLAG_SECTION) +#define MICROPROFILE_DEFINE_SECTION_GPU(var, name, color) MicroProfileToken g_mpGPU_##var = MicroProfileGetToken("GPU", name, color, MicroProfileTokenTypeGpu, MICROPROFILE_TIMER_FLAG_SECTION) + +#define MICROPROFILE_REGISTER_GROUP(group, category, color) MicroProfileRegisterGroup(group, category, color) + +#define MICROPROFILE_SCOPE(var) MicroProfileScopeHandler MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(g_mp_##var) +#define MICROPROFILE_SCOPE_TOKEN(token) MicroProfileScopeHandler MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(token) +#define MICROPROFILE_SCOPEI(group, name, color) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp, __LINE__) = MicroProfileGetToken(group, name, color, MicroProfileTokenTypeCpu, 0); \ + MicroProfileScopeHandler MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(MICROPROFILE_TOKEN_PASTE(g_mp, __LINE__)) +#define MICROPROFILE_SCOPE_CSTR(CStr) MicroProfileScopeHandlerCStr MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(CStr) + +#define MICROPROFILE_SECTION(var) MICROPROFILE_SCOPE(var) +#define MICROPROFILE_SECTIONI(name, color) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp, __LINE__) = MicroProfileGetToken("__SECTION", name, color, MicroProfileTokenTypeCpu, MICROPROFILE_TIMER_FLAG_SECTION); \ + MicroProfileScopeHandler MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(MICROPROFILE_TOKEN_PASTE(g_mp, __LINE__)) + +#define MICROPROFILE_SCOPEGPU_TOKEN(token) MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(fooGPU, __LINE__)(token, MicroProfileGetGlobalGpuThreadLog()) +#define MICROPROFILE_SCOPEGPU(var) MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(fooGPU, __LINE__)(g_mpGPU_##var, MicroProfileGetGlobalGpuThreadLog()) +#define MICROPROFILE_SCOPEGPUI(name, color) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__) = MicroProfileGetToken("GPU", name, color, MicroProfileTokenTypeGpu, 0); \ + MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(fooGPU, __LINE__)(MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__), MicroProfileGetGlobalGpuThreadLog()) +#define MICROPROFILE_SCOPEGPU_CSTR(CStr) MicroProfileScopeGpuHandlerCStr MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(CStr, MicroProfileGetGlobalGpuThreadLog()) + +#define MICROPROFILE_SCOPEGPU_TOKEN_L(Log, token) MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(fooGPU, __LINE__)(token, Log) +#define MICROPROFILE_SCOPEGPU_L(Log, var) MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(fooGPU, __LINE__)(g_mpGPU_##var, Log) +#define MICROPROFILE_SCOPEGPUI_L(Log, name, color) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__) = MicroProfileGetToken("GPU", name, color, MicroProfileTokenTypeGpu, 0); \ + MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(fooGPU, __LINE__)(MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__), Log) +#define MICROPROFILE_SCOPEGPU_CSTR_L(Log, CStr) MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(CStr, Log) + +#define MICROPROFILE_SECTIONGPU_TOKEN(token) MICROPROFILE_SCOPEGPU_TOKEN(token) +#define MICROPROFILE_SECTIONGPU(var) MICROPROFILE_SCOPEGPU(var) +#define MICROPROFILE_SECTIONGPUI(name, color) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__) = MicroProfileGetToken("__SECTIONGPU", name, color, MicroProfileTokenTypeGpu, MICROPROFILE_TIMER_FLAG_SECTION); \ + MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(fooGPU, __LINE__)(MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__)) + +#define MICROPROFILE_SECTIONGPU_TOKEN_L(Log, token) MICROPROFILE_SCOPEGPU_TOKEN_L(token, Log) +#define MICROPROFILE_SECTIONGPU_L(Log, var) MICROPROFILE_SCOPEGPU_L(var, Log) +#define MICROPROFILE_SECTIONGPUI_L(Log, name, color) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__) = MicroProfileGetToken("__SECTIONGPU", name, color, MicroProfileTokenTypeGpu, MICROPROFILE_TIMER_FLAG_SECTION); \ + MicroProfileScopeGpuHandler MICROPROFILE_TOKEN_PASTE(fooGPU, __LINE__)(MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__), Log) + +#define MICROPROFILE_CONDITIONAL_SCOPEI(condition, group, name, color) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp, __LINE__) = MicroProfileGetToken(group, name, color, MicroProfileTokenTypeCpu, 0); \ + MicroProfileConditionalScopeHandler MICROPROFILE_TOKEN_PASTE(foo, __LINE__)(MICROPROFILE_TOKEN_PASTE(g_mp, __LINE__), condition) +#define MICROPROFILE_ENTER(var) MicroProfileEnter(g_mp_##var) +#define MICROPROFILE_ENTER_TOKEN(token) MicroProfileEnter(token) +#define MICROPROFILE_ENTERI(group, name, color) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp, __LINE__) = MICROPROFILE_INVALID_TOKEN; \ + if(MICROPROFILE_INVALID_TOKEN == MICROPROFILE_TOKEN_PASTE(g_mp, __LINE__)) \ + { \ + MicroProfileGetTokenC(&MICROPROFILE_TOKEN_PASTE(g_mp, __LINE__), group, name, color, MicroProfileTokenTypeCpu, 0); \ + } \ + MicroProfileEnter(MICROPROFILE_TOKEN_PASTE(g_mp, __LINE__)) +#define MICROPROFILE_LEAVE() MicroProfileLeave() + +#define MICROPROFILE_GPU_ENTER(var) MicroProfileEnterGpu(g_mpGPU_##var, MicroProfileGetGlobalGpuThreadLog()) +#define MICROPROFILE_GPU_ENTER_TOKEN(token) MicroProfileEnterGpu(token, MicroProfileGetGlobalGpuThreadLog()) +#define MICROPROFILE_GPU_ENTERI(group, name, color) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__) = MICROPROFILE_INVALID_TOKEN; \ + if(MICROPROFILE_INVALID_TOKEN == MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__)) \ + { \ + MicroProfileGetTokenC(&MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__), group, name, color, MicroProfileTokenTypeGpu, 0); \ + } \ + MicroProfileEnterGpu(MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__), MicroProfileGetGlobalGpuThreadLog()) +#define MICROPROFILE_GPU_LEAVE() MicroProfileLeaveGpu(MicroProfileGetGlobalGpuThreadLog()) +#define MICROPROFILE_GPU_ENTER_L(Log, var) MicroProfileEnterGpu(g_mpGPU_##var, Log) +#define MICROPROFILE_GPU_ENTER_TOKEN_L(Log, token) MicroProfileEnterGpu(token, Log) +#define MICROPROFILE_GPU_ENTERI_L(Log, name, color) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__) = MICROPROFILE_INVALID_TOKEN; \ + if(MICROPROFILE_INVALID_TOKEN == MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__)) \ + { \ + MicroProfileGetTokenC(&MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__), group, name, color, MicroProfileTokenTypeGpu, 0); \ + } \ + MicroProfileEnterGpu(MICROPROFILE_TOKEN_PASTE(g_mpGPU, __LINE__), Log) +#define MICROPROFILE_GPU_LEAVE_L(Log) MicroProfileLeaveGpu(Log) +#define MICROPROFILE_GPU_INIT_QUEUE(QueueName) MicroProfileInitGpuQueue(QueueName) +#define MICROPROFILE_GPU_FREE_QUEUE(QueueName) MicroProfileFreeGpuQueue(QueueName) +#define MICROPROFILE_GPU_GET_QUEUE(QueueName) MicroProfileGetGpuQueue(QueueName) +#define MICROPROFILE_GPU_BEGIN(pContext, pLog) MicroProfileGpuBegin(pContext, pLog) +#define MICROPROFILE_GPU_SET_CONTEXT(pContext, pLog) MicroProfileGpuSetContext(pContext, pLog) +#define MICROPROFILE_GPU_END(pLog) MicroProfileGpuEnd(pLog) +#define MICROPROFILE_GPU_SUBMIT(Queue, Work) MicroProfileGpuSubmit(Queue, Work) +#define MICROPROFILE_TIMELINE_TOKEN(token) MicroProfileTimelineToken token = 0 +#define MICROPROFILE_TIMELINE_ENTER(token, color, name) token = MicroProfileTimelineEnter(color, name) +#define MICROPROFILE_TIMELINE_ENTERF(token, color, fmt, ...) token = MicroProfileTimelineEnterf(color, fmt, ##__VA_ARGS__) +#define MICROPROFILE_TIMELINE_LEAVE(token) \ + do \ + { \ + if(token) \ + { \ + MicroProfileTimelineLeave(token); \ + } \ + } while(0) +// use only with static string literals +#define MICROPROFILE_TIMELINE_ENTER_STATIC(color, name) MicroProfileTimelineEnterStatic(color, name) +// use only with static string literals +#define MICROPROFILE_TIMELINE_LEAVE_STATIC(name) MicroProfileTimelineLeaveStatic(name) +#define MICROPROFILE_TIMELINE_SCOPE(color, fmt, ...) \ + MicroProfileTimelineToken MICROPROFILE_TOKEN_PASTE(__timeline__, __LINE__) = MicroProfileTimelineEnterf(color, fmt, ##__VA_ARGS__); \ + MicroProfileScopeTimelineExitHandler MICROPROFILE_TOKEN_PASTE(__timeline__0, __LINE__)(MICROPROFILE_TOKEN_PASTE(__timeline__, __LINE__)) + +#define MICROPROFILE_THREADLOGGPURESET(a) MicroProfileThreadLogGpuReset(a) +#define MICROPROFILE_META_CPU(name, count) \ + do \ + { \ + } while(0) // static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_meta,__LINE__) = MicroProfileGetMetaToken(name); MicroProfileMetaUpdate(MICROPROFILE_TOKEN_PASTE(g_mp_meta,__LINE__), count, + // MicroProfileTokenTypeCpu) +#define MICROPROFILE_COUNTER_ADD(name, count) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_counter, __LINE__) = MicroProfileGetCounterToken(name, 0); \ + MicroProfileCounterAdd(MICROPROFILE_TOKEN_PASTE(g_mp_counter, __LINE__), count) +#define MICROPROFILE_COUNTER_SUB(name, count) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_counter, __LINE__) = MicroProfileGetCounterToken(name, 0); \ + MicroProfileCounterAdd(MICROPROFILE_TOKEN_PASTE(g_mp_counter, __LINE__), -(int64_t)count) +#define MICROPROFILE_COUNTER_SET(name, count) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_counter_set, __LINE__) = MicroProfileGetCounterToken(name, 0); \ + MicroProfileCounterSet(MICROPROFILE_TOKEN_PASTE(g_mp_counter_set, __LINE__), count) +#define MICROPROFILE_COUNTER_SET_DOUBLE(name, count) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_counter_set, __LINE__) = MicroProfileGetCounterToken(name, MICROPROFILE_COUNTER_FLAG_DOUBLE); \ + MicroProfileCounterSetDouble(MICROPROFILE_TOKEN_PASTE(g_mp_counter_set, __LINE__), count) +#define MICROPROFILE_COUNTER_SET_INT32_PTR(name, ptr) MicroProfileCounterSetPtr(name, ptr, sizeof(int32_t)) +#define MICROPROFILE_COUNTER_SET_INT64_PTR(name, ptr) MicroProfileCounterSetPtr(name, ptr, sizeof(int64_t)) +#define MICROPROFILE_COUNTER_CLEAR_PTR(name) MicroProfileCounterSetPtr(name, 0, 0) +#define MICROPROFILE_COUNTER_SET_LIMIT(name, count) \ + static MicroProfileToken MICROPROFILE_TOKEN_PASTE(g_mp_counter, __LINE__) = MicroProfileGetCounterToken(name, 0); \ + MicroProfileCounterSetLimit(MICROPROFILE_TOKEN_PASTE(g_mp_counter, __LINE__), count) +#define MICROPROFILE_COUNTER_CONFIG(name, type, limit, flags) MicroProfileCounterConfig(name, type, limit, flags) +#define MICROPROFILE_COUNTER_CONFIG_ONCE(name, type, limit, flags) \ + do \ + { \ + static bool MICROPROFILE_TOKEN_PASTE(g_mponce, __LINE__) = false; \ + if(!MICROPROFILE_TOKEN_PASTE(g_mponce, __LINE__)) \ + { \ + MICROPROFILE_TOKEN_PASTE(g_mponce, __LINE__) = true; \ + MicroProfileCounterConfig(name, type, limit, flags); \ + } \ + } while(0) +#define MICROPROFILE_DECLARE_LOCAL_COUNTER(var) \ + extern int64_t g_mp_local_counter##var; \ + extern MicroProfileToken g_mp_counter_token##var; +#define MICROPROFILE_DEFINE_LOCAL_COUNTER(var, name) \ + int64_t g_mp_local_counter##var = 0; \ + MicroProfileToken g_mp_counter_token##var = MicroProfileGetCounterToken(name, 0) +#define MICROPROFILE_DECLARE_LOCAL_ATOMIC_COUNTER(var) extern MicroProfileToken g_mp_counter_token##var; +#define MICROPROFILE_DEFINE_LOCAL_ATOMIC_COUNTER(var, name) MicroProfileToken g_mp_counter_token##var = MicroProfileGetCounterToken(name, 0) +#define MICROPROFILE_COUNTER_LOCAL_ADD(var, count) MicroProfileLocalCounterAdd(&g_mp_local_counter##var, (count)) +#define MICROPROFILE_COUNTER_LOCAL_SUB(var, count) MicroProfileLocalCounterAdd(&g_mp_local_counter##var, -(int64_t)(count)) +#define MICROPROFILE_COUNTER_LOCAL_SET(var, count) MicroProfileLocalCounterSet(&g_mp_local_counter##var, count) +#define MICROPROFILE_COUNTER_LOCAL_UPDATE_ADD(var) MicroProfileCounterAdd(g_mp_counter_token##var, MicroProfileLocalCounterSet(&g_mp_local_counter##var, 0)) +#define MICROPROFILE_COUNTER_LOCAL_UPDATE_SET(var) MicroProfileCounterSet(g_mp_counter_token##var, MicroProfileLocalCounterSet(&g_mp_local_counter##var, 0)) +#define MICROPROFILE_COUNTER_LOCAL_ADD_ATOMIC(var, count) MicroProfileLocalCounterAddAtomic(g_mp_counter_token##var, (count)) +#define MICROPROFILE_COUNTER_LOCAL_SUB_ATOMIC(var, count) MicroProfileLocalCounterAddAtomic(g_mp_counter_token##var, -(int64_t)(count)) +#define MICROPROFILE_COUNTER_LOCAL_SET_ATOMIC(var, count) MicroProfileLocalCounterSetAtomic(g_mp_counter_token##var, count) +#define MICROPROFILE_COUNTER_LOCAL_UPDATE_ADD_ATOMIC(var) MicroProfileCounterAdd(g_mp_counter_token##var, MicroProfileLocalCounterSetAtomic(g_mp_counter_token##var, 0)) +#define MICROPROFILE_COUNTER_LOCAL_UPDATE_SET_ATOMIC(var) MicroProfileCounterSet(g_mp_counter_token##var, MicroProfileLocalCounterSetAtomic(g_mp_counter_token##var, 0)) +#define MICROPROFILE_COUNTER_GET_TOKEN(name, f) MicroProfileGetCounterToken(name, f) +#define MICROPROFILE_COUNTER_TOKEN_ADD(v, count) MicroProfileCounterAdd(v, count) +#define MICROPROFILE_COUNTER_TOKEN_SUB(v, count) MicroProfileCounterAdd(v, -(int64_t)count) +#define MICROPROFILE_COUNTER_TOKEN_SET(v, count) MicroProfileCounterSet(v, count) +#define MICROPROFILE_FORCEENABLECPUGROUP(s) MicroProfileForceEnableGroup(s, MicroProfileTokenTypeCpu) +#define MICROPROFILE_FORCEDISABLECPUGROUP(s) MicroProfileForceDisableGroup(s, MicroProfileTokenTypeCpu) +#define MICROPROFILE_FORCEENABLEGPUGROUP(s) MicroProfileForceEnableGroup(s, MicroProfileTokenTypeGpu) +#define MICROPROFILE_FORCEDISABLEGPUGROUP(s) MicroProfileForceDisableGroup(s, MicroProfileTokenTypeGpu) +#define MICROPROFILE_CONDITIONAL(expr) expr + +#ifndef MICROPROFILE_PLATFORM_MARKERS +#define MICROPROFILE_PLATFORM_MARKERS 0 +#endif + +#if MICROPROFILE_PLATFORM_MARKERS +#define MICROPROFILE_PLATFORM_MARKERS_ENABLED S.nPlatformMarkersEnabled +#define MICROPROFILE_PLATFORM_MARKER_BEGIN(color, marker) MicroProfilePlatformMarkerBegin(color, marker) +#define MICROPROFILE_PLATFORM_MARKER_END() MicroProfilePlatformMarkerEnd() +#else +#define MICROPROFILE_PLATFORM_MARKER_BEGIN(color, marker) \ + do \ + { \ + (void)color; \ + (void)marker; \ + } while(0) +#define MICROPROFILE_PLATFORM_MARKER_END() \ + do \ + { \ + } while(0) +#define MICROPROFILE_PLATFORM_MARKERS_ENABLED 0 +#endif + +#define MICROPROFILE_MAJOR_VERSION 4 +#define MICROPROFILE_MINOR_VERSION 0 + +#ifndef MICROPROFILE_USE_THREAD_NAME_CALLBACK +#define MICROPROFILE_USE_THREAD_NAME_CALLBACK 0 +#endif + +#ifndef MICROPROFILE_PER_THREAD_BUFFER_SIZE +#define MICROPROFILE_PER_THREAD_BUFFER_SIZE (2048 << 10) +#endif + +#ifndef MICROPROFILE_PER_THREAD_GPU_BUFFER_SIZE +#define MICROPROFILE_PER_THREAD_GPU_BUFFER_SIZE (128 << 10) +#endif + +#ifndef MICROPROFILE_MAX_FRAME_HISTORY +#define MICROPROFILE_MAX_FRAME_HISTORY 512 +#endif + +#ifndef MICROPROFILE_PRINTF +#define MICROPROFILE_PRINTF printf +#endif + +// #ifndef MICROPROFILE_META_MAX +// #define MICROPROFILE_META_MAX 8 +// #endif + +#ifndef MICROPROFILE_WEBSERVER_PORT +#define MICROPROFILE_WEBSERVER_PORT 1338 +#endif + +#ifndef MICROPROFILE_WEBSERVER +#define MICROPROFILE_WEBSERVER 1 +#endif + +#ifndef MICROPROFILE_WEBSERVER_AUTO_START +#define MICROPROFILE_WEBSERVER_AUTO_START 1 // when set to 1, the webserver will start on first call to MicroProfileFlip +#endif + +#ifndef MICROPROFILE_WEBSERVER_DEFAULT_FRAMES +#define MICROPROFILE_WEBSERVER_DEFAULT_FRAMES 30 +#endif + +#ifndef MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE +#define MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE (16 << 10) +#endif + +#ifndef MICROPROFILE_GPU_TIMERS +#if defined(MICROPROFILE_GPU_TIMERS_VULKAN) || defined(MICROPROFILE_GPU_TIMERS_D3D12) || defined(MICROPROFILE_GPU_TIMERS_D3D11) || defined(MICROPROFILE_GPU_TIMERS_GL) +#define MICROPROFILE_GPU_TIMERS 1 +#else +#define MICROPROFILE_GPU_TIMERS 0 +#endif +#endif + +#ifndef MICROPROFILE_GPU_FRAME_DELAY +#define MICROPROFILE_GPU_FRAME_DELAY 5 // must be > 0 +#endif + +#ifndef MICROPROFILE_NAME_MAX_LEN +#define MICROPROFILE_NAME_MAX_LEN 64 +#endif + +#ifndef MICROPROFILE_MAX_TIMERS +#define MICROPROFILE_MAX_TIMERS 1024 +#endif + +#ifndef MICROPROFILE_MAX_THREADS +#define MICROPROFILE_MAX_THREADS 128 +#endif + +#ifndef MICROPROFILE_UNPACK_RED +#define MICROPROFILE_UNPACK_RED(c) (0xff & ((c) >> 16)) +#endif + +#ifndef MICROPROFILE_UNPACK_GREEN +#define MICROPROFILE_UNPACK_GREEN(c) (0xff & ((c) >> 8)) +#endif + +#ifndef MICROPROFILE_UNPACK_BLUE +#define MICROPROFILE_UNPACK_BLUE(c) (0xff & ((c))) +#endif + +#ifndef MICROPROFILE_DEFAULT_PRESET +#define MICROPROFILE_DEFAULT_PRESET "Default" +#endif + +#ifndef MICROPROFILE_TIMELINE_MAX_ENTRIES +#define MICROPROFILE_TIMELINE_MAX_ENTRIES (4 << 10) +#endif + +#ifndef MICROPROFILE_MAX_STRING +#define MICROPROFILE_MAX_STRING 128 +#endif + +#ifndef MICROPROFILE_TIMELINE_MAX_TOKENS +#define MICROPROFILE_TIMELINE_MAX_TOKENS 64 +#endif + +#ifndef MICROPROFILE_THREAD_LOG_FRAMES_REUSE +#define MICROPROFILE_THREAD_LOG_FRAMES_REUSE 200 +#endif + +#ifndef MICROPROFILE_CONTEXT_SWITCH_TRACE +#if defined(_WIN32) +#define MICROPROFILE_CONTEXT_SWITCH_TRACE 1 +#elif defined(__APPLE__) +#define MICROPROFILE_CONTEXT_SWITCH_TRACE 0 // disabled until dtrace script is working. +#else +#define MICROPROFILE_CONTEXT_SWITCH_TRACE 0 +#endif +#endif + +#if MICROPROFILE_CONTEXT_SWITCH_TRACE +#define MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE (128 * 1024) // 2mb with 16 byte entry size +#else +#define MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE (1) +#endif + +#ifndef MICROPROFILE_MINIZ +#define MICROPROFILE_MINIZ 0 +#endif + +#ifndef MICROPROFILE_COUNTER_HISTORY +#define MICROPROFILE_COUNTER_HISTORY 1 +#endif + +#ifndef MICROPROFILE_MAX_GROUPS +#define MICROPROFILE_MAX_GROUPS 128 // must be multiple of 32 +#endif + +#ifndef MICROPROFILE_STACK_MAX +#define MICROPROFILE_STACK_MAX 64 +#endif + +#ifndef MICROPROFILE_BREAK_ON_PATCH_FAIL +#define MICROPROFILE_BREAK_ON_PATCH_FAIL 0 +#endif + +#ifndef MICROPROFILE_INSTRUMENT_MICROPROFILE +#define MICROPROFILE_INSTRUMENT_MICROPROFILE 0 +#endif + +#ifndef MICROPROFILE_MAX_DYNAMIC_TOKENS +#define MICROPROFILE_MAX_DYNAMIC_TOKENS 2048 +#endif + +#ifndef MICROPROFILE_INSTRUMENT_SYMBOLNAME_MAXLEN +#define MICROPROFILE_INSTRUMENT_SYMBOLNAME_MAXLEN 128 +#endif + +#ifndef MICROPROFILE_INSTRUMENT_MAX_MODULES +#define MICROPROFILE_INSTRUMENT_MAX_MODULES 256 +#endif + +#ifndef MICROPROFILE_INSTRUMENT_MAX_MODULE_CHARS +#define MICROPROFILE_INSTRUMENT_MAX_MODULE_CHARS (8 << 10) +#endif + +#ifndef MICROPROFILE_ASSERT_LOG_FREED +#define MICROPROFILE_ASSERT_LOG_FREED 0 +#endif + +#define MICROPROFILE_FLIP_FLAG_START_WEBSERVER 0x1 + +#if MICROPROFILE_WEBSERVER_AUTO_START +#define MICROPROFILE_FLIP_FLAG_DEFAULT (MICROPROFILE_FLIP_FLAG_START_WEBSERVER) +#else +#define MICROPROFILE_FLIP_FLAG_DEFAULT (0) +#endif + +#ifndef MICROPROFILE_GET_SETTINGS_FILE_PATH +#define MICROPROFILE_GET_SETTINGS_FILE_PATH "" +#endif + +#ifndef MICROPROFILE_MAX_CPU_CORES +#define MICROPROFILE_MAX_CPU_CORES 256 +#endif + +typedef enum MicroProfileTokenType_t +{ + MicroProfileTokenTypeCpu, + MicroProfileTokenTypeGpu, +} MicroProfileTokenType; + +struct MicroProfile; +struct MicroProfileThreadLogGpu; +struct MicroProfileScopeStateC; + +#ifdef __cplusplus +#define IF_CPP(exp) exp +#else +#define IF_CPP(exp) +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define MICROPROFILE_INVALID_TOKEN ((uint64_t) - 1) +#define MICROPROFILE_TIMER_FLAG_SECTION 0x1 +#define MICROPROFILE_TIMER_FLAG_PLACEHOLDER 0x1000 +#define MICROPROFILE_CSV_FLAG_FRAME_NUMBERS 0x0 +#define MICROPROFILE_CSV_FLAG_FRAME_TIME 0x1 + + MICROPROFILE_API void MicroProfileInit(); + MICROPROFILE_API void MicroProfileShutdown(); + MICROPROFILE_API void MicroProfileStartAutoFlip(uint32_t nHz); + MICROPROFILE_API void MicroProfileStopAutoFlip(); + MICROPROFILE_API void MicroProfileEnableFrameExtraCounterData(); // enable per frame counter storage. uses more memory + MICROPROFILE_API MicroProfileToken MicroProfileFindToken(const char* sGroup, const char* sName); + MICROPROFILE_API MicroProfileToken MicroProfileGetToken(const char* sGroup, const char* sName, uint32_t nColor, MicroProfileTokenType Token, uint32_t flags); + MICROPROFILE_API void MicroProfileGetTokenC(MicroProfileToken* pToken, const char* sGroup, const char* sName, uint32_t nColor, MicroProfileTokenType Token, uint32_t flags); + MICROPROFILE_API MicroProfileToken MicroProfileGetCounterToken(const char* pName, uint32_t CounterTokenFlag); + MICROPROFILE_API MicroProfileToken MicroProfileGetChildCounterToken(MicroProfileToken Parent, const char* pName); // manually create the child/parent tree + MICROPROFILE_API void MicroProfileCounterAdd(MicroProfileToken nToken, int64_t nCount); + MICROPROFILE_API void MicroProfileCounterSet(MicroProfileToken nToken, int64_t nCount); + MICROPROFILE_API int64_t MicroProfileCounterGet(MicroProfileToken nToken); + MICROPROFILE_API void MicroProfileCounterSetDouble(MicroProfileToken nToken, double dCount); + MICROPROFILE_API double MicroProfileCounterGetDouble(MicroProfileToken nToken); + MICROPROFILE_API void MicroProfileCounterSetLimit(MicroProfileToken nToken, int64_t nCount); + MICROPROFILE_API void MicroProfileCounterSetLimitDouble(MicroProfileToken nToken, double nCount); + MICROPROFILE_API void MicroProfileCounterConfig(const char* pCounterName, uint32_t nFormat, int64_t nLimit, uint32_t nFlags); + MICROPROFILE_API void MicroProfileCounterConfigToken(MicroProfileToken nToken, uint32_t nFormat, int64_t nLimit, uint32_t nFlags); + MICROPROFILE_API void MicroProfileCounterSetPtr(const char* pCounterName, void* pValue, uint32_t nSize); + MICROPROFILE_API void MicroProfileCounterFetchCounters(); + MICROPROFILE_API void MicroProfileLocalCounterAdd(int64_t* pCounter, int64_t nCount); + MICROPROFILE_API int64_t MicroProfileLocalCounterSet(int64_t* pCounter, int64_t nCount); + MICROPROFILE_API uint64_t MicroProfileEnterInternal(MicroProfileToken nToken); + MICROPROFILE_API void MicroProfileLeaveInternal(MicroProfileToken nToken, uint64_t nTick); + MICROPROFILE_API uint64_t MicroProfileEnterInternalCStr(const char* pStr); + + MICROPROFILE_API void MicroProfileLeaveInternalCStr(const char* pStr, uint64_t nTickStart); + + MICROPROFILE_API void MicroProfileEnter(MicroProfileToken nToken); + MICROPROFILE_API void MicroProfileLeave(); + + MICROPROFILE_API void MicroProfileEnterSection(const char* pName, uint32_t nColor); + MICROPROFILE_API void MicroProfileLeaveSection(); + + MICROPROFILE_API void MicroProfileEnterGpu(MicroProfileToken nToken, struct MicroProfileThreadLogGpu* pLog); + MICROPROFILE_API void MicroProfileLeaveGpu(struct MicroProfileThreadLogGpu* pLog); + MICROPROFILE_API MicroProfileTimelineToken MicroProfileTimelineEnterInternal(uint32_t nColor, const char* pStr, uint32_t nStrLen, int bIsStaticString); + MICROPROFILE_API MicroProfileTimelineToken MicroProfileTimelineEnter(uint32_t nColor, const char* pStr); + MICROPROFILE_API MicroProfileTimelineToken MicroProfileTimelineEnterf(uint32_t nColor, const char* pStr, ...); + MICROPROFILE_API void MicroProfileTimelineLeave(MicroProfileTimelineToken id); + MICROPROFILE_API void MicroProfileTimelineEnterStatic(uint32_t nColor, const char* pStr); + MICROPROFILE_API void MicroProfileTimelineLeaveStatic(const char* pStr); + + MICROPROFILE_API uint64_t MicroProfileGpuEnterInternal(struct MicroProfileThreadLogGpu* pLog, MicroProfileToken nToken); + MICROPROFILE_API void MicroProfileGpuLeaveInternal(struct MicroProfileThreadLogGpu* pLog, MicroProfileToken nToken, uint64_t nTick); + MICROPROFILE_API uint64_t MicroProfileGpuEnterInternalCStr(struct MicroProfileThreadLogGpu* pGpuLog, const char* pStr); + MICROPROFILE_API void MicroProfileGpuLeaveInternalCStr(struct MicroProfileThreadLogGpu* pGpuLog, uint64_t nTickStart); + + MICROPROFILE_API void MicroProfileGpuBegin(void* pContext, struct MicroProfileThreadLogGpu* pLog); + MICROPROFILE_API void MicroProfileGpuSetContext(void* pContext, struct MicroProfileThreadLogGpu* pLog); + MICROPROFILE_API uint64_t MicroProfileGpuEnd(struct MicroProfileThreadLogGpu* pLog); + MICROPROFILE_API struct MicroProfileThreadLogGpu* MicroProfileThreadLogGpuAlloc(); + MICROPROFILE_API void MicroProfileThreadLogGpuFree(struct MicroProfileThreadLogGpu* pLog); + MICROPROFILE_API void MicroProfileThreadLogGpuReset(struct MicroProfileThreadLogGpu* pLog); + MICROPROFILE_API void MicroProfileGpuSubmit(int nQueue, uint64_t nWork); + MICROPROFILE_API int MicroProfileInitGpuQueue(const char* pQueueName); + MICROPROFILE_API void MicroProfileFreeGpuQueue(int nQueue); + MICROPROFILE_API int MicroProfileGetGpuQueue(const char* pQueueName); + + MICROPROFILE_API void MicroProfileFlip(void* pGpuContext, uint32_t FlipFlag IF_CPP(= MICROPROFILE_FLIP_FLAG_DEFAULT)); //! call once per frame. + MICROPROFILE_API void MicroProfileFlip_CB(void* pGpuContext, MicroProfileOnFreeze FreezeCB, uint32_t FlipFlag IF_CPP(= MICROPROFILE_FLIP_FLAG_DEFAULT)); //! call once per frame. + MICROPROFILE_API void MicroProfileToggleFrozen(); + MICROPROFILE_API int MicroProfileIsFrozen(); + MICROPROFILE_API int MicroProfileEnabled(); + MICROPROFILE_API void MicroProfileForceEnableGroup(const char* pGroup, MicroProfileTokenType Type); + MICROPROFILE_API void MicroProfileForceDisableGroup(const char* pGroup, MicroProfileTokenType Type); + MICROPROFILE_API float MicroProfileGetTime(const char* pGroup, const char* pName); + MICROPROFILE_API int MicroProfilePlatformMarkersGetEnabled(); // enable platform markers. disables microprofile markers + MICROPROFILE_API void MicroProfilePlatformMarkersSetEnabled(int bEnabled); // enable platform markers. disables microprofile markers + MICROPROFILE_API void MicroProfileContextSwitchSearch(uint32_t* pContextSwitchStart, uint32_t* pContextSwitchEnd, uint64_t nBaseTicksCpu, uint64_t nBaseTicksEndCpu); + MICROPROFILE_API void MicroProfileOnThreadCreate(const char* pThreadName); // should be called from newly created threads + MICROPROFILE_API void MicroProfileOnThreadExit(); // call on exit to reuse log + MICROPROFILE_API void MicroProfileInitThreadLog(); + MICROPROFILE_API void MicroProfileSetEnableAllGroups(int bEnable); + MICROPROFILE_API void MicroProfileEnableCategory(const char* pCategory); + MICROPROFILE_API void MicroProfileDisableCategory(const char* pCategory); + MICROPROFILE_API int MicroProfileGetEnableAllGroups(); + MICROPROFILE_API void MicroProfileSetForceMetaCounters(int bEnable); + MICROPROFILE_API int MicroProfileGetForceMetaCounters(); + MICROPROFILE_API void MicroProfileEnableMetaCounter(const char* pMet); + MICROPROFILE_API void MicroProfileDisableMetaCounter(const char* pMet); + MICROPROFILE_API void MicroProfileSetAggregateFrames(int frames); + MICROPROFILE_API int MicroProfileGetAggregateFrames(); + MICROPROFILE_API int MicroProfileGetCurrentAggregateFrames(); + MICROPROFILE_API struct MicroProfile* MicroProfileGet(); + MICROPROFILE_API void MicroProfileGetRange(uint32_t nPut, uint32_t nGet, uint32_t nRange[2][2]); + MICROPROFILE_API void MicroProfileStartContextSwitchTrace(); + MICROPROFILE_API void MicroProfileStopContextSwitchTrace(); + MICROPROFILE_API int MicroProfileIsLocalThread(uint32_t nThreadId); + MICROPROFILE_API int MicroProfileFormatCounter(int eFormat, int64_t nCounter, char* pOut, uint32_t nBufferSize); + MICROPROFILE_API struct MicroProfileThreadLogGpu* MicroProfileGetGlobalGpuThreadLog(); + MICROPROFILE_API int MicroProfileGetGlobalGpuQueue(); + MICROPROFILE_API void MicroProfileRegisterGroup(const char* pGroup, const char* pCategory, uint32_t nColor); +#if MICROPROFILE_PLATFORM_MARKERS + MICROPROFILE_API void MicroProfilePlatformMarkerBegin(uint32_t nColor, const char* pMarker); // not implemented by microprofile. + MICROPROFILE_API void MicroProfilePlatformMarkerEnd(); // not implemented by microprofile. +#endif + + MICROPROFILE_API float MicroProfileTickToMsMultiplierCpu(); + MICROPROFILE_API float MicroProfileTickToMsMultiplierGpu(); + MICROPROFILE_API int64_t MicroProfileTicksPerSecondCpu(); + MICROPROFILE_API uint64_t MicroProfileTick(); + + MICROPROFILE_API void MicroProfileLocalCounterAddAtomic(MicroProfileToken Token, int64_t nCount); + MICROPROFILE_API int64_t MicroProfileLocalCounterSetAtomic(MicroProfileToken, int64_t nCount); + + MICROPROFILE_API const char* MicroProfileCounterString(const char* String); + MICROPROFILE_API MicroProfileToken MicroProfileCounterTokenTree(MicroProfileToken* LastToken, MicroProfileToken CurrentParent, const char* pString); + MICROPROFILE_API MicroProfileToken MicroProfileCounterTokenTreeDynamic(MicroProfileToken* LastToken, MicroProfileToken Parent, const char* pString); + + MICROPROFILE_API void MicroProfileCsvConfigEnd(); + MICROPROFILE_API void MicroProfileCsvConfigBegin(uint32_t MaxTimers, uint32_t MaxGroups, uint32_t MaxCounters, uint32_t Flags); + MICROPROFILE_API void MicroProfileCsvConfigAddTimer(const char* Group, const char* Timer, const char* DisplayName IF_CPP(= nullptr), MicroProfileTokenType Type IF_CPP(= MicroProfileTokenTypeCpu)); + MICROPROFILE_API void MicroProfileCsvConfigAddGroup(const char* Group, const char* DisplayName IF_CPP(= nullptr)); + MICROPROFILE_API void MicroProfileCsvConfigAddCounter(const char* CounterName, const char* DisplayName IF_CPP(= nullptr)); + + MICROPROFILE_API void MicroProfileUpdateSettingsPath(); + +#if MICROPROFILE_IMGUI + // Basic support for controlling MicroProfile from ImGui, and for rendering graphs & tables + enum MicroProfileImguiAlign; + struct MicroProfileImguiWindowDesc; + struct MicroProfileImguiEntryDesc; + MICROPROFILE_API void MicroProfileImguiGraphs(const MicroProfileImguiWindowDesc& Window, const MicroProfileImguiEntryDesc* Entries, uint32_t NumEntries); + MICROPROFILE_API void MicroProfileImguiTable(const MicroProfileImguiWindowDesc& Window, const MicroProfileImguiEntryDesc* Entries, uint32_t NumEntries); + MICROPROFILE_API void MicroProfileImguiControls(); +#endif + +#ifdef __cplusplus +} +#endif + +#ifdef __cplusplus +struct MicroProfileThreadInfo +{ + // 3 first members are used to sort. dont reorder + uint32_t nIsLocal; + MicroProfileThreadIdType pid; + MicroProfileThreadIdType tid; + // 3 first members are used to sort. dont reorder + + const char* pThreadModule; + const char* pProcessModule; + MicroProfileThreadInfo() + : nIsLocal(0) + , pid(0) + , tid(0) + , pThreadModule("?") + , pProcessModule("?") + { + } + MicroProfileThreadInfo(uint32_t ThreadId, uint32_t ProcessId, uint32_t nIsLocal) + : nIsLocal(nIsLocal) + , pid(ProcessId) + , tid(ThreadId) + , pThreadModule("?") + , pProcessModule("?") + { + } + ~MicroProfileThreadInfo() + { + } +}; +MICROPROFILE_API MicroProfileThreadInfo MicroProfileGetThreadInfo(MicroProfileThreadIdType nThreadId); +MICROPROFILE_API uint32_t MicroProfileGetThreadInfoArray(MicroProfileThreadInfo** pThreadArray); +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +#if MICROPROFILE_GPU_TIMERS_D3D12 + MICROPROFILE_API void MicroProfileGpuInitD3D12(void* pDevice, uint32_t nNodeCount, void** pCommandQueues, void** pCopyQueues); + MICROPROFILE_API void MicroProfileGpuShutdownD3D12(); + MICROPROFILE_API void MicroProfileSetCurrentNodeD3D12(uint32_t nNode); +#endif + +#if MICROPROFILE_GPU_TIMERS_VULKAN +#include + void MicroProfileGpuInitVulkan(VkDevice* pDevices, VkPhysicalDevice* pPhysicalDevices, VkQueue* pQueues, uint32_t* QueueFamily, uint32_t nNodeCount); + MICROPROFILE_API void MicroProfileGpuShutdownVulkan(); + MICROPROFILE_API void MicroProfileSetCurrentNodeVulkan(uint32_t nNode); +#endif + + MICROPROFILE_API void MicroProfileDumpFile(const char* pHtml, const char* pCsv, float fCpuSpike, float fGpuSpike, uint32_t FrameCount IF_CPP(= MICROPROFILE_WEBSERVER_DEFAULT_FRAMES)); + MICROPROFILE_API void MicroProfileDumpFileImmediately(const char* pHtml, const char* pCsv, void* pGpuContext, uint32_t FrameCount IF_CPP(= MICROPROFILE_WEBSERVER_DEFAULT_FRAMES)); + +#if MICROPROFILE_ENABLED && MICROPROFILE_WEBSERVER + MICROPROFILE_API uint32_t MicroProfileWebServerPort(); + MICROPROFILE_API void MicroProfileSetWebServerPort(uint32_t nPort); +#else +#define MicroProfileWebServerPort() ((uint32_t) - 1) +#define MicroProfileSetWebServerPort(a) \ + do \ + { \ + } while(0) +#endif + + typedef enum MicroProfileGpuTimerStateType_ + { + MicroProfileGpuTimerStateType_Invalid = 0, + MicroProfileGpuTimerStateType_D3D11, + MicroProfileGpuTimerStateType_D3D12, + MicroProfileGpuTimerStateType_Vulkan, + MicroProfileGpuTimerStateType_GL, + MicroProfileGpuTimerStateType_Custom + } MicroProfileGpuTimerStateType; + + typedef struct MicroProfileGpuTimerState_ + { + MicroProfileGpuTimerStateType Type; + } MicroProfileGpuTimerState; + +#if MICROPROFILE_GPU_TIMERS + typedef uint32_t(*MicroProfileGpuInsertTimeStamp_CB)(void* pContext); + typedef uint64_t(*MicroProfileGpuGetTimeStamp_CB)(uint32_t nKey); + typedef uint64_t(*MicroProfileTicksPerSecondGpu_CB)(); + typedef int (*MicroProfileGetGpuTickReference_CB)(int64_t* pOutCPU, int64_t* pOutGpu); + typedef uint32_t(*MicroProfileGpuFlip_CB)(void*); + typedef void (*MicroProfileGpuShutdown_CB)(); + + MICROPROFILE_API void MicroProfileGpuShutdownPlatform(); + + MICROPROFILE_API void MicroProfileGpuInitPlatform(MicroProfileGpuTimerStateType eType, + MicroProfileGpuTimerState* pGpuState, + MicroProfileGpuInsertTimeStamp_CB InsertTimeStamp, + MicroProfileGpuGetTimeStamp_CB GetTimeStamp, + MicroProfileTicksPerSecondGpu_CB TicksPerSecond, + MicroProfileGetGpuTickReference_CB GetTickReference, + MicroProfileGpuFlip_CB Flip, + MicroProfileGpuShutdown_CB Shutdown); +#else +#define MicroProfileGpuShutdownPlatform() \ + do \ + { \ + } while(0) +#define MicroProfileGpuInitPlatform(...) \ + do \ + { \ + } while(0) +#define MicroProfileGpuInsertTimeStamp(a) 1 +#define MicroProfileGpuGetTimeStamp(a) 0 +#define MicroProfileTicksPerSecondGpu() 1 +#define MicroProfileGetGpuTickReference(a, b) 0 +#define MicroProfileGpuFlip(a) 0 +#define MicroProfileGpuShutdown() \ + do \ + { \ + } while(0) + +#endif + +#if MICROPROFILE_GPU_TIMERS_D3D11 +#define MICROPROFILE_D3D11_MAX_QUERIES (32 << 10) + MICROPROFILE_API void MicroProfileGpuInitD3D11(void* pDevice, void* pDeviceContext); + MICROPROFILE_API void MicroProfileGpuShutdownD3D11(); +#endif + +#if MICROPROFILE_GPU_TIMERS_GL +#define MICROPROFILE_GL_MAX_QUERIES (8 << 10) + MICROPROFILE_API void MicroProfileGpuInitGL(); +#endif + +#if MICROPROFILE_USE_THREAD_NAME_CALLBACK + MICROPROFILE_API const char* MicroProfileGetThreadName(char* pzName); +#else +#define MicroProfileGetThreadName(a) 0 +#endif + +#ifdef __cplusplus +} +#endif + +struct MicroProfileScopeStateC +{ + MicroProfileToken Token; + int64_t nTick; +}; + +#ifdef __cplusplus +struct MicroProfileScopeHandler +{ + MicroProfileToken nToken; + uint64_t nTick; + MicroProfileScopeHandler(MicroProfileToken Token) + : nToken(Token) + { + nTick = MicroProfileEnterInternal(nToken); + } + ~MicroProfileScopeHandler() + { + MicroProfileLeaveInternal(nToken, nTick); + } +}; + +struct MicroProfileScopeHandlerCStr +{ + uint64_t nTick; + const char* pStr; + MicroProfileScopeHandlerCStr(const char* pStr) + : pStr(pStr) + { + nTick = MicroProfileEnterInternalCStr(pStr); + } + ~MicroProfileScopeHandlerCStr() + { + MicroProfileLeaveInternalCStr(pStr, nTick); + } +}; + +struct MicroProfileScopeGpuHandler +{ + MicroProfileToken nToken; + MicroProfileThreadLogGpu* pLog; + uint64_t nTick; + MicroProfileScopeGpuHandler(MicroProfileToken Token, MicroProfileThreadLogGpu* pLog) + : nToken(Token) + , pLog(pLog) + { + nTick = MicroProfileGpuEnterInternal(pLog, nToken); + } + ~MicroProfileScopeGpuHandler() + { + MicroProfileGpuLeaveInternal(pLog, nToken, nTick); + } +}; + +struct MicroProfileScopeGpuHandlerCStr +{ + MicroProfileThreadLogGpu* pLog; + uint64_t nTick; + MicroProfileScopeGpuHandlerCStr(const char* pStr, MicroProfileThreadLogGpu* pLog) + : pLog(pLog) + { + nTick = MicroProfileGpuEnterInternalCStr(pLog, pStr); + } + ~MicroProfileScopeGpuHandlerCStr() + { + MicroProfileGpuLeaveInternalCStr(pLog, nTick); + } +}; + +struct MicroProfileConditionalScopeHandler +{ + MicroProfileToken nToken; + uint64_t nTick; + MicroProfileConditionalScopeHandler(MicroProfileToken token, bool condition) + : nToken(token) + { + nTick = condition ? MicroProfileEnterInternal(token) : MICROPROFILE_INVALID_TOKEN; + } + ~MicroProfileConditionalScopeHandler() + { + if (nTick != MICROPROFILE_INVALID_TOKEN) + { + MicroProfileLeaveInternal(nToken, nTick); + } + } +}; +struct MicroProfileScopeTimelineExitHandler +{ + MicroProfileTimelineToken nToken; + MicroProfileScopeTimelineExitHandler(MicroProfileTimelineToken Token) + : nToken(Token) + { + } + ~MicroProfileScopeTimelineExitHandler() + { + MicroProfileTimelineLeave(nToken); + } +}; + +#if MICROPROFILE_IMGUI +enum MicroProfileImguiAlign +{ + MICROPROFILE_IMGUI_ALIGN_TOP_LEFT = 0, + MICROPROFILE_IMGUI_ALIGN_TOP_RIGHT = 1, + MICROPROFILE_IMGUI_ALIGN_BOTTOM_LEFT = 2, + MICROPROFILE_IMGUI_ALIGN_BOTTOM_RIGHT = 3, +}; + +struct MicroProfileImguiWindowDesc +{ + uint32_t Align = MICROPROFILE_IMGUI_ALIGN_TOP_LEFT; + int GraphWidth = 300; + int GraphHeight = 80; + int OffsetX = 0; + int OffsetY = 0; +}; +struct MicroProfileImguiEntryDesc +{ + MicroProfileToken GraphTimer; + float GraphMax = -1; +}; +#endif + +#endif //__cplusplus +#endif // enabled + +enum MicroProfileCounterFormat +{ + MICROPROFILE_COUNTER_FORMAT_DEFAULT = 0, + MICROPROFILE_COUNTER_FORMAT_BYTES = 1, +}; + +enum MicroProfileCounterFlags +{ + MICROPROFILE_COUNTER_FLAG_NONE = 0, + MICROPROFILE_COUNTER_FLAG_DETAILED = 0x1, + MICROPROFILE_COUNTER_FLAG_DETAILED_GRAPH = 0x2, + // internal: + MICROPROFILE_COUNTER_FLAG_INTERNAL_MASK = ~0x3, + MICROPROFILE_COUNTER_FLAG_HAS_LIMIT = 0x4, + MICROPROFILE_COUNTER_FLAG_CLOSED = 0x8, + MICROPROFILE_COUNTER_FLAG_MANUAL_SWAP = 0x10, + MICROPROFILE_COUNTER_FLAG_LEAF = 0x20, + MICROPROFILE_COUNTER_FLAG_DOUBLE = 0x40, + MICROPROFILE_COUNTER_FLAG_TYPE_MASK = MICROPROFILE_COUNTER_FLAG_DOUBLE, + MICROPROFILE_COUNTER_FLAG_TOKEN_DONT_CREATE = 0x80000000, +}; + +#endif // once + +#define MP_AUTO 0 +// from http://fugal.net/vim/rgbtxt.html +#define MP_SNOW 0xfffafa +#define MP_GHOSTWHITE 0xf8f8ff +#define MP_WHITESMOKE 0xf5f5f5 +#define MP_GAINSBORO 0xdcdcdc +#define MP_FLORALWHITE 0xfffaf0 +#define MP_OLDLACE 0xfdf5e6 +#define MP_LINEN 0xfaf0e6 +#define MP_ANTIQUEWHITE 0xfaebd7 +#define MP_PAPAYAWHIP 0xffefd5 +#define MP_BLANCHEDALMOND 0xffebcd +#define MP_BISQUE 0xffe4c4 +#define MP_PEACHPUFF 0xffdab9 +#define MP_NAVAJOWHITE 0xffdead +#define MP_MOCCASIN 0xffe4b5 +#define MP_CORNSILK 0xfff8dc +#define MP_IVORY 0xfffff0 +#define MP_LEMONCHIFFON 0xfffacd +#define MP_SEASHELL 0xfff5ee +#define MP_HONEYDEW 0xf0fff0 +#define MP_MINTCREAM 0xf5fffa +#define MP_AZURE 0xf0ffff +#define MP_ALICEBLUE 0xf0f8ff +#define MP_LAVENDER 0xe6e6fa +#define MP_LAVENDERBLUSH 0xfff0f5 +#define MP_MISTYROSE 0xffe4e1 +#define MP_WHITE 0xffffff +#define MP_BLACK 0x010101 +#define MP_DARKSLATEGRAY 0x2f4f4f +#define MP_DARKSLATEGREY 0x2f4f4f +#define MP_DIMGRAY 0x696969 +#define MP_DIMGREY 0x696969 +#define MP_SLATEGRAY 0x708090 +#define MP_SLATEGREY 0x708090 +#define MP_LIGHTSLATEGRAY 0x778899 +#define MP_LIGHTSLATEGREY 0x778899 +#define MP_GRAY 0xbebebe +#define MP_GREY 0xbebebe +#define MP_LIGHTGREY 0xd3d3d3 +#define MP_LIGHTGRAY 0xd3d3d3 +#define MP_MIDNIGHTBLUE 0x191970 +#define MP_NAVY 0x000080 +#define MP_NAVYBLUE 0x000080 +#define MP_CORNFLOWERBLUE 0x6495ed +#define MP_DARKSLATEBLUE 0x483d8b +#define MP_SLATEBLUE 0x6a5acd +#define MP_MEDIUMSLATEBLUE 0x7b68ee +#define MP_LIGHTSLATEBLUE 0x8470ff +#define MP_MEDIUMBLUE 0x0000cd +#define MP_ROYALBLUE 0x4169e1 +#define MP_BLUE 0x0000ff +#define MP_DODGERBLUE 0x1e90ff +#define MP_DEEPSKYBLUE 0x00bfff +#define MP_SKYBLUE 0x87ceeb +#define MP_LIGHTSKYBLUE 0x87cefa +#define MP_STEELBLUE 0x4682b4 +#define MP_LIGHTSTEELBLUE 0xb0c4de +#define MP_LIGHTBLUE 0xadd8e6 +#define MP_POWDERBLUE 0xb0e0e6 +#define MP_PALETURQUOISE 0xafeeee +#define MP_DARKTURQUOISE 0x00ced1 +#define MP_MEDIUMTURQUOISE 0x48d1cc +#define MP_TURQUOISE 0x40e0d0 +#define MP_CYAN 0x00ffff +#define MP_LIGHTCYAN 0xe0ffff +#define MP_CADETBLUE 0x5f9ea0 +#define MP_MEDIUMAQUAMARINE 0x66cdaa +#define MP_AQUAMARINE 0x7fffd4 +#define MP_DARKGREEN 0x006400 +#define MP_DARKOLIVEGREEN 0x556b2f +#define MP_DARKSEAGREEN 0x8fbc8f +#define MP_SEAGREEN 0x2e8b57 +#define MP_MEDIUMSEAGREEN 0x3cb371 +#define MP_LIGHTSEAGREEN 0x20b2aa +#define MP_PALEGREEN 0x98fb98 +#define MP_SPRINGGREEN 0x00ff7f +#define MP_LAWNGREEN 0x7cfc00 +#define MP_GREEN 0x00ff00 +#define MP_CHARTREUSE 0x7fff00 +#define MP_MEDIUMSPRINGGREEN 0x00fa9a +#define MP_GREENYELLOW 0xadff2f +#define MP_LIMEGREEN 0x32cd32 +#define MP_YELLOWGREEN 0x9acd32 +#define MP_FORESTGREEN 0x228b22 +#define MP_OLIVEDRAB 0x6b8e23 +#define MP_DARKKHAKI 0xbdb76b +#define MP_KHAKI 0xf0e68c +#define MP_PALEGOLDENROD 0xeee8aa +#define MP_LIGHTGOLDENRODYELLOW 0xfafad2 +#define MP_LIGHTYELLOW 0xffffe0 +#define MP_YELLOW 0xffff00 +#define MP_GOLD 0xffd700 +#define MP_LIGHTGOLDENROD 0xeedd82 +#define MP_GOLDENROD 0xdaa520 +#define MP_DARKGOLDENROD 0xb8860b +#define MP_ROSYBROWN 0xbc8f8f +#define MP_INDIANRED 0xcd5c5c +#define MP_SADDLEBROWN 0x8b4513 +#define MP_SIENNA 0xa0522d +#define MP_PERU 0xcd853f +#define MP_BURLYWOOD 0xdeb887 +#define MP_BEIGE 0xf5f5dc +#define MP_WHEAT 0xf5deb3 +#define MP_SANDYBROWN 0xf4a460 +#define MP_TAN 0xd2b48c +#define MP_CHOCOLATE 0xd2691e +#define MP_FIREBRICK 0xb22222 +#define MP_BROWN 0xa52a2a +#define MP_DARKSALMON 0xe9967a +#define MP_SALMON 0xfa8072 +#define MP_LIGHTSALMON 0xffa07a +#define MP_ORANGE 0xffa500 +#define MP_DARKORANGE 0xff8c00 +#define MP_CORAL 0xff7f50 +#define MP_LIGHTCORAL 0xf08080 +#define MP_TOMATO 0xff6347 +#define MP_ORANGERED 0xff4500 +#define MP_RED 0xff0000 +#define MP_HOTPINK 0xff69b4 +#define MP_DEEPPINK 0xff1493 +#define MP_PINK 0xffc0cb +#define MP_LIGHTPINK 0xffb6c1 +#define MP_PALEVIOLETRED 0xdb7093 +#define MP_MAROON 0xb03060 +#define MP_MEDIUMVIOLETRED 0xc71585 +#define MP_VIOLETRED 0xd02090 +#define MP_MAGENTA 0xff00ff +#define MP_VIOLET 0xee82ee +#define MP_PLUM 0xdda0dd +#define MP_ORCHID 0xda70d6 +#define MP_MEDIUMORCHID 0xba55d3 +#define MP_DARKORCHID 0x9932cc +#define MP_DARKVIOLET 0x9400d3 +#define MP_BLUEVIOLET 0x8a2be2 +#define MP_PURPLE 0xa020f0 +#define MP_MEDIUMPURPLE 0x9370db +#define MP_THISTLE 0xd8bfd8 +#define MP_SNOW1 0xfffafa +#define MP_SNOW2 0xeee9e9 +#define MP_SNOW3 0xcdc9c9 +#define MP_SNOW4 0x8b8989 +#define MP_SEASHELL1 0xfff5ee +#define MP_SEASHELL2 0xeee5de +#define MP_SEASHELL3 0xcdc5bf +#define MP_SEASHELL4 0x8b8682 +#define MP_ANTIQUEWHITE1 0xffefdb +#define MP_ANTIQUEWHITE2 0xeedfcc +#define MP_ANTIQUEWHITE3 0xcdc0b0 +#define MP_ANTIQUEWHITE4 0x8b8378 +#define MP_BISQUE1 0xffe4c4 +#define MP_BISQUE2 0xeed5b7 +#define MP_BISQUE3 0xcdb79e +#define MP_BISQUE4 0x8b7d6b +#define MP_PEACHPUFF1 0xffdab9 +#define MP_PEACHPUFF2 0xeecbad +#define MP_PEACHPUFF3 0xcdaf95 +#define MP_PEACHPUFF4 0x8b7765 +#define MP_NAVAJOWHITE1 0xffdead +#define MP_NAVAJOWHITE2 0xeecfa1 +#define MP_NAVAJOWHITE3 0xcdb38b +#define MP_NAVAJOWHITE4 0x8b795e +#define MP_LEMONCHIFFON1 0xfffacd +#define MP_LEMONCHIFFON2 0xeee9bf +#define MP_LEMONCHIFFON3 0xcdc9a5 +#define MP_LEMONCHIFFON4 0x8b8970 +#define MP_CORNSILK1 0xfff8dc +#define MP_CORNSILK2 0xeee8cd +#define MP_CORNSILK3 0xcdc8b1 +#define MP_CORNSILK4 0x8b8878 +#define MP_IVORY1 0xfffff0 +#define MP_IVORY2 0xeeeee0 +#define MP_IVORY3 0xcdcdc1 +#define MP_IVORY4 0x8b8b83 +#define MP_HONEYDEW1 0xf0fff0 +#define MP_HONEYDEW2 0xe0eee0 +#define MP_HONEYDEW3 0xc1cdc1 +#define MP_HONEYDEW4 0x838b83 +#define MP_LAVENDERBLUSH1 0xfff0f5 +#define MP_LAVENDERBLUSH2 0xeee0e5 +#define MP_LAVENDERBLUSH3 0xcdc1c5 +#define MP_LAVENDERBLUSH4 0x8b8386 +#define MP_MISTYROSE1 0xffe4e1 +#define MP_MISTYROSE2 0xeed5d2 +#define MP_MISTYROSE3 0xcdb7b5 +#define MP_MISTYROSE4 0x8b7d7b +#define MP_AZURE1 0xf0ffff +#define MP_AZURE2 0xe0eeee +#define MP_AZURE3 0xc1cdcd +#define MP_AZURE4 0x838b8b +#define MP_SLATEBLUE1 0x836fff +#define MP_SLATEBLUE2 0x7a67ee +#define MP_SLATEBLUE3 0x6959cd +#define MP_SLATEBLUE4 0x473c8b +#define MP_ROYALBLUE1 0x4876ff +#define MP_ROYALBLUE2 0x436eee +#define MP_ROYALBLUE3 0x3a5fcd +#define MP_ROYALBLUE4 0x27408b +#define MP_BLUE1 0x0000ff +#define MP_BLUE2 0x0000ee +#define MP_BLUE3 0x0000cd +#define MP_BLUE4 0x00008b +#define MP_DODGERBLUE1 0x1e90ff +#define MP_DODGERBLUE2 0x1c86ee +#define MP_DODGERBLUE3 0x1874cd +#define MP_DODGERBLUE4 0x104e8b +#define MP_STEELBLUE1 0x63b8ff +#define MP_STEELBLUE2 0x5cacee +#define MP_STEELBLUE3 0x4f94cd +#define MP_STEELBLUE4 0x36648b +#define MP_DEEPSKYBLUE1 0x00bfff +#define MP_DEEPSKYBLUE2 0x00b2ee +#define MP_DEEPSKYBLUE3 0x009acd +#define MP_DEEPSKYBLUE4 0x00688b +#define MP_SKYBLUE1 0x87ceff +#define MP_SKYBLUE2 0x7ec0ee +#define MP_SKYBLUE3 0x6ca6cd +#define MP_SKYBLUE4 0x4a708b +#define MP_LIGHTSKYBLUE1 0xb0e2ff +#define MP_LIGHTSKYBLUE2 0xa4d3ee +#define MP_LIGHTSKYBLUE3 0x8db6cd +#define MP_LIGHTSKYBLUE4 0x607b8b +#define MP_SLATEGRAY1 0xc6e2ff +#define MP_SLATEGRAY2 0xb9d3ee +#define MP_SLATEGRAY3 0x9fb6cd +#define MP_SLATEGRAY4 0x6c7b8b +#define MP_LIGHTSTEELBLUE1 0xcae1ff +#define MP_LIGHTSTEELBLUE2 0xbcd2ee +#define MP_LIGHTSTEELBLUE3 0xa2b5cd +#define MP_LIGHTSTEELBLUE4 0x6e7b8b +#define MP_LIGHTBLUE1 0xbfefff +#define MP_LIGHTBLUE2 0xb2dfee +#define MP_LIGHTBLUE3 0x9ac0cd +#define MP_LIGHTBLUE4 0x68838b +#define MP_LIGHTCYAN1 0xe0ffff +#define MP_LIGHTCYAN2 0xd1eeee +#define MP_LIGHTCYAN3 0xb4cdcd +#define MP_LIGHTCYAN4 0x7a8b8b +#define MP_PALETURQUOISE1 0xbbffff +#define MP_PALETURQUOISE2 0xaeeeee +#define MP_PALETURQUOISE3 0x96cdcd +#define MP_PALETURQUOISE4 0x668b8b +#define MP_CADETBLUE1 0x98f5ff +#define MP_CADETBLUE2 0x8ee5ee +#define MP_CADETBLUE3 0x7ac5cd +#define MP_CADETBLUE4 0x53868b +#define MP_TURQUOISE1 0x00f5ff +#define MP_TURQUOISE2 0x00e5ee +#define MP_TURQUOISE3 0x00c5cd +#define MP_TURQUOISE4 0x00868b +#define MP_CYAN1 0x00ffff +#define MP_CYAN2 0x00eeee +#define MP_CYAN3 0x00cdcd +#define MP_CYAN4 0x008b8b +#define MP_DARKSLATEGRAY1 0x97ffff +#define MP_DARKSLATEGRAY2 0x8deeee +#define MP_DARKSLATEGRAY3 0x79cdcd +#define MP_DARKSLATEGRAY4 0x528b8b +#define MP_AQUAMARINE1 0x7fffd4 +#define MP_AQUAMARINE2 0x76eec6 +#define MP_AQUAMARINE3 0x66cdaa +#define MP_AQUAMARINE4 0x458b74 +#define MP_DARKSEAGREEN1 0xc1ffc1 +#define MP_DARKSEAGREEN2 0xb4eeb4 +#define MP_DARKSEAGREEN3 0x9bcd9b +#define MP_DARKSEAGREEN4 0x698b69 +#define MP_SEAGREEN1 0x54ff9f +#define MP_SEAGREEN2 0x4eee94 +#define MP_SEAGREEN3 0x43cd80 +#define MP_SEAGREEN4 0x2e8b57 +#define MP_PALEGREEN1 0x9aff9a +#define MP_PALEGREEN2 0x90ee90 +#define MP_PALEGREEN3 0x7ccd7c +#define MP_PALEGREEN4 0x548b54 +#define MP_SPRINGGREEN1 0x00ff7f +#define MP_SPRINGGREEN2 0x00ee76 +#define MP_SPRINGGREEN3 0x00cd66 +#define MP_SPRINGGREEN4 0x008b45 +#define MP_GREEN1 0x00ff00 +#define MP_GREEN2 0x00ee00 +#define MP_GREEN3 0x00cd00 +#define MP_GREEN4 0x008b00 +#define MP_CHARTREUSE1 0x7fff00 +#define MP_CHARTREUSE2 0x76ee00 +#define MP_CHARTREUSE3 0x66cd00 +#define MP_CHARTREUSE4 0x458b00 +#define MP_OLIVEDRAB1 0xc0ff3e +#define MP_OLIVEDRAB2 0xb3ee3a +#define MP_OLIVEDRAB3 0x9acd32 +#define MP_OLIVEDRAB4 0x698b22 +#define MP_DARKOLIVEGREEN1 0xcaff70 +#define MP_DARKOLIVEGREEN2 0xbcee68 +#define MP_DARKOLIVEGREEN3 0xa2cd5a +#define MP_DARKOLIVEGREEN4 0x6e8b3d +#define MP_KHAKI1 0xfff68f +#define MP_KHAKI2 0xeee685 +#define MP_KHAKI3 0xcdc673 +#define MP_KHAKI4 0x8b864e +#define MP_LIGHTGOLDENROD1 0xffec8b +#define MP_LIGHTGOLDENROD2 0xeedc82 +#define MP_LIGHTGOLDENROD3 0xcdbe70 +#define MP_LIGHTGOLDENROD4 0x8b814c +#define MP_LIGHTYELLOW1 0xffffe0 +#define MP_LIGHTYELLOW2 0xeeeed1 +#define MP_LIGHTYELLOW3 0xcdcdb4 +#define MP_LIGHTYELLOW4 0x8b8b7a +#define MP_YELLOW1 0xffff00 +#define MP_YELLOW2 0xeeee00 +#define MP_YELLOW3 0xcdcd00 +#define MP_YELLOW4 0x8b8b00 +#define MP_GOLD1 0xffd700 +#define MP_GOLD2 0xeec900 +#define MP_GOLD3 0xcdad00 +#define MP_GOLD4 0x8b7500 +#define MP_GOLDENROD1 0xffc125 +#define MP_GOLDENROD2 0xeeb422 +#define MP_GOLDENROD3 0xcd9b1d +#define MP_GOLDENROD4 0x8b6914 +#define MP_DARKGOLDENROD1 0xffb90f +#define MP_DARKGOLDENROD2 0xeead0e +#define MP_DARKGOLDENROD3 0xcd950c +#define MP_DARKGOLDENROD4 0x8b6508 +#define MP_ROSYBROWN1 0xffc1c1 +#define MP_ROSYBROWN2 0xeeb4b4 +#define MP_ROSYBROWN3 0xcd9b9b +#define MP_ROSYBROWN4 0x8b6969 +#define MP_INDIANRED1 0xff6a6a +#define MP_INDIANRED2 0xee6363 +#define MP_INDIANRED3 0xcd5555 +#define MP_INDIANRED4 0x8b3a3a +#define MP_SIENNA1 0xff8247 +#define MP_SIENNA2 0xee7942 +#define MP_SIENNA3 0xcd6839 +#define MP_SIENNA4 0x8b4726 +#define MP_BURLYWOOD1 0xffd39b +#define MP_BURLYWOOD2 0xeec591 +#define MP_BURLYWOOD3 0xcdaa7d +#define MP_BURLYWOOD4 0x8b7355 +#define MP_WHEAT1 0xffe7ba +#define MP_WHEAT2 0xeed8ae +#define MP_WHEAT3 0xcdba96 +#define MP_WHEAT4 0x8b7e66 +#define MP_TAN1 0xffa54f +#define MP_TAN2 0xee9a49 +#define MP_TAN3 0xcd853f +#define MP_TAN4 0x8b5a2b +#define MP_CHOCOLATE1 0xff7f24 +#define MP_CHOCOLATE2 0xee7621 +#define MP_CHOCOLATE3 0xcd661d +#define MP_CHOCOLATE4 0x8b4513 +#define MP_FIREBRICK1 0xff3030 +#define MP_FIREBRICK2 0xee2c2c +#define MP_FIREBRICK3 0xcd2626 +#define MP_FIREBRICK4 0x8b1a1a +#define MP_BROWN1 0xff4040 +#define MP_BROWN2 0xee3b3b +#define MP_BROWN3 0xcd3333 +#define MP_BROWN4 0x8b2323 +#define MP_SALMON1 0xff8c69 +#define MP_SALMON2 0xee8262 +#define MP_SALMON3 0xcd7054 +#define MP_SALMON4 0x8b4c39 +#define MP_LIGHTSALMON1 0xffa07a +#define MP_LIGHTSALMON2 0xee9572 +#define MP_LIGHTSALMON3 0xcd8162 +#define MP_LIGHTSALMON4 0x8b5742 +#define MP_ORANGE1 0xffa500 +#define MP_ORANGE2 0xee9a00 +#define MP_ORANGE3 0xcd8500 +#define MP_ORANGE4 0x8b5a00 +#define MP_DARKORANGE1 0xff7f00 +#define MP_DARKORANGE2 0xee7600 +#define MP_DARKORANGE3 0xcd6600 +#define MP_DARKORANGE4 0x8b4500 +#define MP_CORAL1 0xff7256 +#define MP_CORAL2 0xee6a50 +#define MP_CORAL3 0xcd5b45 +#define MP_CORAL4 0x8b3e2f +#define MP_TOMATO1 0xff6347 +#define MP_TOMATO2 0xee5c42 +#define MP_TOMATO3 0xcd4f39 +#define MP_TOMATO4 0x8b3626 +#define MP_ORANGERED1 0xff4500 +#define MP_ORANGERED2 0xee4000 +#define MP_ORANGERED3 0xcd3700 +#define MP_ORANGERED4 0x8b2500 +#define MP_RED1 0xff0000 +#define MP_RED2 0xee0000 +#define MP_RED3 0xcd0000 +#define MP_RED4 0x8b0000 +#define MP_DEEPPINK1 0xff1493 +#define MP_DEEPPINK2 0xee1289 +#define MP_DEEPPINK3 0xcd1076 +#define MP_DEEPPINK4 0x8b0a50 +#define MP_HOTPINK1 0xff6eb4 +#define MP_HOTPINK2 0xee6aa7 +#define MP_HOTPINK3 0xcd6090 +#define MP_HOTPINK4 0x8b3a62 +#define MP_PINK1 0xffb5c5 +#define MP_PINK2 0xeea9b8 +#define MP_PINK3 0xcd919e +#define MP_PINK4 0x8b636c +#define MP_LIGHTPINK1 0xffaeb9 +#define MP_LIGHTPINK2 0xeea2ad +#define MP_LIGHTPINK3 0xcd8c95 +#define MP_LIGHTPINK4 0x8b5f65 +#define MP_PALEVIOLETRED1 0xff82ab +#define MP_PALEVIOLETRED2 0xee799f +#define MP_PALEVIOLETRED3 0xcd6889 +#define MP_PALEVIOLETRED4 0x8b475d +#define MP_MAROON1 0xff34b3 +#define MP_MAROON2 0xee30a7 +#define MP_MAROON3 0xcd2990 +#define MP_MAROON4 0x8b1c62 +#define MP_VIOLETRED1 0xff3e96 +#define MP_VIOLETRED2 0xee3a8c +#define MP_VIOLETRED3 0xcd3278 +#define MP_VIOLETRED4 0x8b2252 +#define MP_MAGENTA1 0xff00ff +#define MP_MAGENTA2 0xee00ee +#define MP_MAGENTA3 0xcd00cd +#define MP_MAGENTA4 0x8b008b +#define MP_ORCHID1 0xff83fa +#define MP_ORCHID2 0xee7ae9 +#define MP_ORCHID3 0xcd69c9 +#define MP_ORCHID4 0x8b4789 +#define MP_PLUM1 0xffbbff +#define MP_PLUM2 0xeeaeee +#define MP_PLUM3 0xcd96cd +#define MP_PLUM4 0x8b668b +#define MP_MEDIUMORCHID1 0xe066ff +#define MP_MEDIUMORCHID2 0xd15fee +#define MP_MEDIUMORCHID3 0xb452cd +#define MP_MEDIUMORCHID4 0x7a378b +#define MP_DARKORCHID1 0xbf3eff +#define MP_DARKORCHID2 0xb23aee +#define MP_DARKORCHID3 0x9a32cd +#define MP_DARKORCHID4 0x68228b +#define MP_PURPLE1 0x9b30ff +#define MP_PURPLE2 0x912cee +#define MP_PURPLE3 0x7d26cd +#define MP_PURPLE4 0x551a8b +#define MP_MEDIUMPURPLE1 0xab82ff +#define MP_MEDIUMPURPLE2 0x9f79ee +#define MP_MEDIUMPURPLE3 0x8968cd +#define MP_MEDIUMPURPLE4 0x5d478b +#define MP_THISTLE1 0xffe1ff +#define MP_THISTLE2 0xeed2ee +#define MP_THISTLE3 0xcdb5cd +#define MP_THISTLE4 0x8b7b8b +#define MP_GRAY0 0x000000 +#define MP_GREY0 0x000000 +#define MP_GRAY1 0x030303 +#define MP_GREY1 0x030303 +#define MP_GRAY2 0x050505 +#define MP_GREY2 0x050505 +#define MP_GRAY3 0x080808 +#define MP_GREY3 0x080808 +#define MP_GRAY4 0x0a0a0a +#define MP_GREY4 0x0a0a0a +#define MP_GRAY5 0x0d0d0d +#define MP_GREY5 0x0d0d0d +#define MP_GRAY6 0x0f0f0f +#define MP_GREY6 0x0f0f0f +#define MP_GRAY7 0x121212 +#define MP_GREY7 0x121212 +#define MP_GRAY8 0x141414 +#define MP_GREY8 0x141414 +#define MP_GRAY9 0x171717 +#define MP_GREY9 0x171717 +#define MP_GRAY10 0x1a1a1a +#define MP_GREY10 0x1a1a1a +#define MP_GRAY11 0x1c1c1c +#define MP_GREY11 0x1c1c1c +#define MP_GRAY12 0x1f1f1f +#define MP_GREY12 0x1f1f1f +#define MP_GRAY13 0x212121 +#define MP_GREY13 0x212121 +#define MP_GRAY14 0x242424 +#define MP_GREY14 0x242424 +#define MP_GRAY15 0x262626 +#define MP_GREY15 0x262626 +#define MP_GRAY16 0x292929 +#define MP_GREY16 0x292929 +#define MP_GRAY17 0x2b2b2b +#define MP_GREY17 0x2b2b2b +#define MP_GRAY18 0x2e2e2e +#define MP_GREY18 0x2e2e2e +#define MP_GRAY19 0x303030 +#define MP_GREY19 0x303030 +#define MP_GRAY20 0x333333 +#define MP_GREY20 0x333333 +#define MP_GRAY21 0x363636 +#define MP_GREY21 0x363636 +#define MP_GRAY22 0x383838 +#define MP_GREY22 0x383838 +#define MP_GRAY23 0x3b3b3b +#define MP_GREY23 0x3b3b3b +#define MP_GRAY24 0x3d3d3d +#define MP_GREY24 0x3d3d3d +#define MP_GRAY25 0x404040 +#define MP_GREY25 0x404040 +#define MP_GRAY26 0x424242 +#define MP_GREY26 0x424242 +#define MP_GRAY27 0x454545 +#define MP_GREY27 0x454545 +#define MP_GRAY28 0x474747 +#define MP_GREY28 0x474747 +#define MP_GRAY29 0x4a4a4a +#define MP_GREY29 0x4a4a4a +#define MP_GRAY30 0x4d4d4d +#define MP_GREY30 0x4d4d4d +#define MP_GRAY31 0x4f4f4f +#define MP_GREY31 0x4f4f4f +#define MP_GRAY32 0x525252 +#define MP_GREY32 0x525252 +#define MP_GRAY33 0x545454 +#define MP_GREY33 0x545454 +#define MP_GRAY34 0x575757 +#define MP_GREY34 0x575757 +#define MP_GRAY35 0x595959 +#define MP_GREY35 0x595959 +#define MP_GRAY36 0x5c5c5c +#define MP_GREY36 0x5c5c5c +#define MP_GRAY37 0x5e5e5e +#define MP_GREY37 0x5e5e5e +#define MP_GRAY38 0x616161 +#define MP_GREY38 0x616161 +#define MP_GRAY39 0x636363 +#define MP_GREY39 0x636363 +#define MP_GRAY40 0x666666 +#define MP_GREY40 0x666666 +#define MP_GRAY41 0x696969 +#define MP_GREY41 0x696969 +#define MP_GRAY42 0x6b6b6b +#define MP_GREY42 0x6b6b6b +#define MP_GRAY43 0x6e6e6e +#define MP_GREY43 0x6e6e6e +#define MP_GRAY44 0x707070 +#define MP_GREY44 0x707070 +#define MP_GRAY45 0x737373 +#define MP_GREY45 0x737373 +#define MP_GRAY46 0x757575 +#define MP_GREY46 0x757575 +#define MP_GRAY47 0x787878 +#define MP_GREY47 0x787878 +#define MP_GRAY48 0x7a7a7a +#define MP_GREY48 0x7a7a7a +#define MP_GRAY49 0x7d7d7d +#define MP_GREY49 0x7d7d7d +#define MP_GRAY50 0x7f7f7f +#define MP_GREY50 0x7f7f7f +#define MP_GRAY51 0x828282 +#define MP_GREY51 0x828282 +#define MP_GRAY52 0x858585 +#define MP_GREY52 0x858585 +#define MP_GRAY53 0x878787 +#define MP_GREY53 0x878787 +#define MP_GRAY54 0x8a8a8a +#define MP_GREY54 0x8a8a8a +#define MP_GRAY55 0x8c8c8c +#define MP_GREY55 0x8c8c8c +#define MP_GRAY56 0x8f8f8f +#define MP_GREY56 0x8f8f8f +#define MP_GRAY57 0x919191 +#define MP_GREY57 0x919191 +#define MP_GRAY58 0x949494 +#define MP_GREY58 0x949494 +#define MP_GRAY59 0x969696 +#define MP_GREY59 0x969696 +#define MP_GRAY60 0x999999 +#define MP_GREY60 0x999999 +#define MP_GRAY61 0x9c9c9c +#define MP_GREY61 0x9c9c9c +#define MP_GRAY62 0x9e9e9e +#define MP_GREY62 0x9e9e9e +#define MP_GRAY63 0xa1a1a1 +#define MP_GREY63 0xa1a1a1 +#define MP_GRAY64 0xa3a3a3 +#define MP_GREY64 0xa3a3a3 +#define MP_GRAY65 0xa6a6a6 +#define MP_GREY65 0xa6a6a6 +#define MP_GRAY66 0xa8a8a8 +#define MP_GREY66 0xa8a8a8 +#define MP_GRAY67 0xababab +#define MP_GREY67 0xababab +#define MP_GRAY68 0xadadad +#define MP_GREY68 0xadadad +#define MP_GRAY69 0xb0b0b0 +#define MP_GREY69 0xb0b0b0 +#define MP_GRAY70 0xb3b3b3 +#define MP_GREY70 0xb3b3b3 +#define MP_GRAY71 0xb5b5b5 +#define MP_GREY71 0xb5b5b5 +#define MP_GRAY72 0xb8b8b8 +#define MP_GREY72 0xb8b8b8 +#define MP_GRAY73 0xbababa +#define MP_GREY73 0xbababa +#define MP_GRAY74 0xbdbdbd +#define MP_GREY74 0xbdbdbd +#define MP_GRAY75 0xbfbfbf +#define MP_GREY75 0xbfbfbf +#define MP_GRAY76 0xc2c2c2 +#define MP_GREY76 0xc2c2c2 +#define MP_GRAY77 0xc4c4c4 +#define MP_GREY77 0xc4c4c4 +#define MP_GRAY78 0xc7c7c7 +#define MP_GREY78 0xc7c7c7 +#define MP_GRAY79 0xc9c9c9 +#define MP_GREY79 0xc9c9c9 +#define MP_GRAY80 0xcccccc +#define MP_GREY80 0xcccccc +#define MP_GRAY81 0xcfcfcf +#define MP_GREY81 0xcfcfcf +#define MP_GRAY82 0xd1d1d1 +#define MP_GREY82 0xd1d1d1 +#define MP_GRAY83 0xd4d4d4 +#define MP_GREY83 0xd4d4d4 +#define MP_GRAY84 0xd6d6d6 +#define MP_GREY84 0xd6d6d6 +#define MP_GRAY85 0xd9d9d9 +#define MP_GREY85 0xd9d9d9 +#define MP_GRAY86 0xdbdbdb +#define MP_GREY86 0xdbdbdb +#define MP_GRAY87 0xdedede +#define MP_GREY87 0xdedede +#define MP_GRAY88 0xe0e0e0 +#define MP_GREY88 0xe0e0e0 +#define MP_GRAY89 0xe3e3e3 +#define MP_GREY89 0xe3e3e3 +#define MP_GRAY90 0xe5e5e5 +#define MP_GREY90 0xe5e5e5 +#define MP_GRAY91 0xe8e8e8 +#define MP_GREY91 0xe8e8e8 +#define MP_GRAY92 0xebebeb +#define MP_GREY92 0xebebeb +#define MP_GRAY93 0xededed +#define MP_GREY93 0xededed +#define MP_GRAY94 0xf0f0f0 +#define MP_GREY94 0xf0f0f0 +#define MP_GRAY95 0xf2f2f2 +#define MP_GREY95 0xf2f2f2 +#define MP_GRAY96 0xf5f5f5 +#define MP_GREY96 0xf5f5f5 +#define MP_GRAY97 0xf7f7f7 +#define MP_GREY97 0xf7f7f7 +#define MP_GRAY98 0xfafafa +#define MP_GREY98 0xfafafa +#define MP_GRAY99 0xfcfcfc +#define MP_GREY99 0xfcfcfc +#define MP_GRAY100 0xffffff +#define MP_GREY100 0xffffff +#define MP_DARKGREY 0xa9a9a9 +#define MP_DARKGRAY 0xa9a9a9 +#define MP_DARKBLUE 0x00008b +#define MP_DARKCYAN 0x008b8b +#define MP_DARKMAGENTA 0x8b008b +#define MP_DARKRED 0x8b0000 +#define MP_LIGHTGREEN 0x90ee90 diff --git a/Windows_Libs/Dev/Render/microprofile/microprofile_html.h b/Windows_Libs/Dev/Render/microprofile/microprofile_html.h new file mode 100644 index 0000000..49a1727 --- /dev/null +++ b/Windows_Libs/Dev/Render/microprofile/microprofile_html.h @@ -0,0 +1,17160 @@ +///start file generated from microprofile.html +#ifdef MICROPROFILE_EMBED_HTML +const char g_MicroProfileHtml_begin_0[] = +"\n" +"\n" +"\n" +"MicroProfile Capture\n" +"\n" +"\n" +" \n" +"\n" +"
drop .html file to compare
\n" +"
\n" +"
\n" +"
Group
\n" +"
Timer/Thread
\n" +"
\n" +"
Filter
\n" +"
Filter
\n" +"\n" +"\n" +"\n" +"
\n" +"
\n" +"History View:
\n" +"Click + Drag: Pan View
\n" +"Right Click + Drag : Zoom on region
\n" +"Click Frame : Center on frame
\n" +"
\n" +"Main View:
\n" +"Ctrl + Mouse up/down: Zoom
\n" +"Mousewheel : Zoom
\n" +"Right Click + Drag: Select region
\n" +"Ctrl + Shift + Drag: Select region
\n" +"Space: Zoom to Selection
\n" +"Ctrl + Drag: Pan
\n" +"Click + Drag: Pan
\n" +"hold alt: Show tooltip from timer view in detailed, detailed in timer
\n" +"x : Toggle View
\n" +"\\ : Switch color mode
\n" +"c : toggle collapse mode
\n" +"h : hightlight core types(context switch only)
\n" +"tab : go to filter view
\n" +"
\n" +"Detailed View:
\n" +"Tab: Go To Worst Instance
\n" +"Left/Right Arror: Next/Prev Instance
\n" +"Enter: Search for timer in view
\n" +"
\n" +"Timer Views:
\n" +"Tab: go to filtering
\n" +"Esc: Exit & Clear filter\n" +"
\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"
CloseClose, Never Show
\n" +"
\n" +"\n" +"\n" +""; + +const size_t g_MicroProfileHtml_end_6_size = sizeof(g_MicroProfileHtml_end_6); +const char* g_MicroProfileHtml_end[] = { +&g_MicroProfileHtml_end_0[0], +&g_MicroProfileHtml_end_1[0], +&g_MicroProfileHtml_end_2[0], +&g_MicroProfileHtml_end_3[0], +&g_MicroProfileHtml_end_4[0], +&g_MicroProfileHtml_end_5[0], +&g_MicroProfileHtml_end_6[0], +}; +size_t g_MicroProfileHtml_end_sizes[] = { +sizeof(g_MicroProfileHtml_end_0), +sizeof(g_MicroProfileHtml_end_1), +sizeof(g_MicroProfileHtml_end_2), +sizeof(g_MicroProfileHtml_end_3), +sizeof(g_MicroProfileHtml_end_4), +sizeof(g_MicroProfileHtml_end_5), +sizeof(g_MicroProfileHtml_end_6), +}; +size_t g_MicroProfileHtml_end_count = 7; +#endif //MICROPROFILE_EMBED_HTML + +///end file generated from microprofile.html +///start file generated from microprofilelive.html +#ifdef MICROPROFILE_EMBED_HTML +const char g_MicroProfileHtmlLive_begin_0[] = +"\n" +"\n" +"\n" +"MicroProfile Live\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"
drop .csv files to view CSV data
\n" +"
Filter
\n" +"
\n" +"History View:
\n" +"Right Click + Drag : Select Region
\n" +"Click + Drag: Move Selection
\n" +"Click Frame : Center on frame
\n" +"
\n" +"Main View:
\n" +"space: Freeze capturing
\n" +"x : Toggle View
\n" +"/ : Rotate connection port % 3
\n" +"Ctrl + Drag: Pan
\n" +"Click + Drag: Pan
\n" +"Enter: Capture selection/Next N Frames\n" +"
\n" +"\n" +"\n" +"\n" +"\n" +"
Close
\n" +"
\n" +"
\n" +"
\n" +"
\n" +"
\n" +"
\n" +" \n" +" \n" +"
\n" +"
\n" +"
\n" +"\n" +"\n" +"\n" +"\n" +"\n" +""; + +const size_t g_MicroProfileHtmlLive_begin_5_size = sizeof(g_MicroProfileHtmlLive_begin_5); +const char* g_MicroProfileHtmlLive_begin[] = { +&g_MicroProfileHtmlLive_begin_0[0], +&g_MicroProfileHtmlLive_begin_1[0], +&g_MicroProfileHtmlLive_begin_2[0], +&g_MicroProfileHtmlLive_begin_3[0], +&g_MicroProfileHtmlLive_begin_4[0], +&g_MicroProfileHtmlLive_begin_5[0], +}; +size_t g_MicroProfileHtmlLive_begin_sizes[] = { +sizeof(g_MicroProfileHtmlLive_begin_0), +sizeof(g_MicroProfileHtmlLive_begin_1), +sizeof(g_MicroProfileHtmlLive_begin_2), +sizeof(g_MicroProfileHtmlLive_begin_3), +sizeof(g_MicroProfileHtmlLive_begin_4), +sizeof(g_MicroProfileHtmlLive_begin_5), +}; +size_t g_MicroProfileHtmlLive_begin_count = 6; +const char* g_MicroProfileHtmlLive_end[] = { +""}; +size_t g_MicroProfileHtmlLive_end_sizes[] = { +0}; +size_t g_MicroProfileHtmlLive_end_count = 0; +#endif //MICROPROFILE_EMBED_HTML + +///end file generated from microprofilelive.html diff --git a/Windows_Libs/Dev/Render/microprofile/microprofile_icons.h b/Windows_Libs/Dev/Render/microprofile/microprofile_icons.h new file mode 100644 index 0000000..73c53ce --- /dev/null +++ b/Windows_Libs/Dev/Render/microprofile/microprofile_icons.h @@ -0,0 +1,333 @@ +#ifdef MICROPROFILE_EMBED_HTML +#include + +const uint32_t uprof_16[] = { + 0x474e5089, 0x0a1a0a0d, 0x0d000000, 0x52444849, 0x10000000, 0x10000000, 0x00000308, 0x0f2d2800, 0x00000053, 0x47527301, 0xc9d90142, 0x00007f2c, 0x48700900, 0x00007359, 0x0000130b, 0x0001130b, + 0x00189c9a, 0x50ff0000, 0x0045544c, 0x3d190000, 0x366b3818, 0x296ad96f, 0x6b382757, 0x214b2336, 0x37245226, 0x6b383569, 0x183e1936, 0x3569d76e, 0x411b336e, 0x2652281a, 0x19183d19, 0x6b38183d, + 0x46924936, 0x20275729, 0x57291e49, 0x4fa45327, 0x38193e1a, 0x7d40366b, 0x5cbd603e, 0x2665ce6a, 0x6b382456, 0x366b3836, 0x4557b05b, 0x4f244286, 0x2e673122, 0x38275929, 0xcc68366b, 0x27572963, + 0x3c265628, 0xb65c3a75, 0x27572958, 0x19183d19, 0x7439183d, 0x23532636, 0x6b316933, 0x512566d1, 0x366b3823, 0x66366b38, 0x6b3862c9, 0x376d3936, 0x29408043, 0x5d2a2757, 0x27572927, 0x2f275729, + 0x3d192d62, 0x53ac5718, 0x2f4a964d, 0x3d192d62, 0x438d4718, 0x521a411b, 0x6b384f9f, 0x5ec36336, 0x1938793a, 0x6a37183d, 0x3b773e35, 0x385bb95f, 0x3d19366b, 0x183d1918, 0x5947954b, 0x9a4f55b0, + 0x2a5e2d4c, 0x2b366b38, 0x67252959, 0x00002581, 0x52745500, 0xff00534e, 0xf6ffffff, 0x33ffffff, 0xffffffff, 0x2d1ff3ff, 0x23fff1ff, 0xffa97fff, 0xc9ffffff, 0xffffff89, 0xff30ffff, 0xffa2ff25, + 0xffae5bbd, 0x5cffff8b, 0x81ffc34c, 0xffb2ff67, 0x27a17a3e, 0x38ffffff, 0x57ff89ff, 0xff62ffff, 0x2337ffff, 0xffffffb3, 0x6bffccff, 0x00656578, 0x49c30000, 0x78544144, 0x458f3d9c, 0x0c40c316, + 0xa8753d43, 0x9999940d, 0xe1999999, 0x4ce967fe, 0x2f3ff692, 0x190b4b2c, 0x05897180, 0xfc2b3102, 0x3a2dbeb0, 0xb7a1a751, 0x6f84c5bc, 0xc1d53ee9, 0xc7ea53f8, 0x32cf12e7, 0xf06d6a51, 0xa570fa2e, + 0x0c806d49, 0x2ab19ecb, 0xb4115114, 0xecba0663, 0xd08fc610, 0xa0a2f390, 0x0890a0f5, 0x4d98129e, 0x461a182b, 0x4c1ae922, 0x0bba8b5e, 0x1b0edf65, 0xaeaa9b29, 0xdc71cc61, 0x859c6503, 0x39084930, + 0x3a1e8e40, 0x4210f798, 0x40299087, 0xd6614b89, 0xb46e0c46, 0xb8add787, 0x3bb6a6f6, 0x3b91d558, 0x4bec0669, 0x40ef19e3, 0x38ae9954, 0x9ae6fffe, 0xaaf97f7e, 0xa5be5faf, 0x2b110712, 0x00001d18, + 0x45490000, 0x42ae444e, 0x00008260, +}; + +const uint32_t uprof_16_len = 650; + +const uint32_t uprof_32[] = { + 0x474e5089, 0x0a1a0a0d, 0x0d000000, 0x52444849, 0x20000000, 0x20000000, 0x00000308, 0x8aa44400, 0x000000c6, 0x47527301, 0xc9d90142, 0x00007f2c, 0x48700900, 0x00007359, 0x0000130b, 0x0001130b, + 0x00189c9a, 0x50b90100, 0x0045544c, 0x3d190000, 0x366b3818, 0x296ad96f, 0x4b232757, 0x224d2421, 0x34193f1a, 0x6b383264, 0x366b3836, 0x3a255727, 0x5729386e, 0x2b662e27, 0x3e2a5f2c, 0x3d193b76, + 0x285e2b18, 0x19366b38, 0x6b38183d, 0x67d46c36, 0x1f225224, 0x6b381d49, 0x285c2a36, 0x2255ae59, 0x3d192050, 0x2f673218, 0x38366b38, 0x3d19366b, 0x69d76e18, 0x2d1d461e, 0x713b2a63, 0x214c2339, + 0x3366d16b, 0x56283164, 0x40834326, 0x192c602d, 0x7137183d, 0x5dc16235, 0x38468d49, 0x5729366b, 0x366b3827, 0x383b7c3e, 0xd06a366b, 0x4c9f5065, 0x3057b55b, 0x83412d63, 0x2757293e, 0x6637763a, + 0x6b3862c8, 0x27572936, 0x19366b38, 0x6c34183d, 0x3f864331, 0x693e7b40, 0x743c64cd, 0x366b383a, 0x29245126, 0xc4642757, 0x366b3860, 0x38469049, 0x4b213574, 0x366b3820, 0x3c3f7e41, 0xbb603978, + 0x2757295c, 0x2552ab56, 0xb75c2351, 0x2e643158, 0x29183d19, 0x3d192757, 0x2c612f18, 0x1c183d19, 0x57291b41, 0x366b3827, 0x56275729, 0x864552a7, 0x183d1943, 0x29366b38, 0x57292757, 0x183d1927, + 0x5a366b38, 0x3d1956b0, 0x366b3818, 0x29275729, 0x4e232757, 0x366b3822, 0x21275729, 0x3d19204e, 0x183d1918, 0x19366b38, 0x5729183d, 0x27572927, 0x20469649, 0x612c1e48, 0x30613229, 0x535ab85e, + 0x6b3850a2, 0x366b3836, 0x291b421c, 0x7f3d2757, 0x4488473a, 0x19183d19, 0x3d19183d, 0x366b3818, 0x38356a37, 0x944d366b, 0x27572949, 0x4c3d823f, 0x9d514998, 0x2554274e, 0x3850a653, 0x6b38366b, + 0x2a5c2c36, 0x40e8e81c, 0x93000000, 0x534e5274, 0xffffff00, 0xffffffff, 0xff26fdff, 0xfffffcff, 0xd5ff19ff, 0xffff1127, 0xffffddff, 0x3aff0cff, 0xffff3709, 0xffffffff, 0xffffffff, 0xffffffcd, + 0xff8b0643, 0xffffffa7, 0xff84ffff, 0xce2b1eff, 0xffffffc1, 0xffc8ffff, 0xff9eff14, 0xff38ffff, 0xffc7ffff, 0x04ffffff, 0x802371b6, 0x8e01d6ff, 0xc0b424ff, 0x33301ded, 0x6d99de10, 0xade3f703, + 0xf028f9ff, 0xffc30e8d, 0xffffff2a, 0x19aefbff, 0xd3ffffbe, 0xff492f94, 0xff22ffe9, 0xffffffff, 0x2cffd806, 0x002e00bf, 0x49ff0100, 0x78544144, 0x8993759c, 0x18511257, 0x016f1fc5, 0x14150505, + 0x40410104, 0x2b525c91, 0x4d295cb7, 0x36972d41, 0xd17db4b5, 0xf6cb2f68, 0x2feb353d, 0x1ad9beee, 0xce67ba06, 0xbf7ce77c, 0x337df73b, 0x66210def, 0x51a33a1d, 0x8ff921e7, 0xeef5dd6e, 0x3bd76852, + 0x77065733, 0x8d26a69f, 0xe6e0b17e, 0xac5a4abe, 0x7e66f537, 0x512d37f2, 0xb7c4d3a3, 0xdfa5394b, 0x57b3470c, 0xd4d554ed, 0x26a24a9c, 0x8664ab51, 0xf68ccabe, 0xd8c66bc8, 0x21a50eec, 0x343689d4, + 0xd51e54fa, 0xd00b3be7, 0x67a1da91, 0x0af62350, 0xc315ba74, 0x56d17d35, 0x6d10b480, 0xec1ac671, 0xd7463a42, 0xc07a9dc1, 0x10a7783c, 0x84350862, 0x8bf21d2a, 0xfb01e8f5, 0xc7007b00, 0x754208e5, + 0x7c60c0f4, 0x430057c1, 0x2251cbe8, 0x1a38aec2, 0x5f2c7861, 0x80595e50, 0x6036e83f, 0x2b708f04, 0x936f5f19, 0x9df5da7a, 0xece92576, 0x48b0f824, 0x11c8754d, 0x6a60e9dc, 0xcf084ea9, 0xdf31b20b, + 0xa161fedb, 0x2ae5727d, 0xe127b2c0, 0xf73c2397, 0x77090227, 0x15d2fdc0, 0x33c69723, 0xdefe8591, 0x900cbe01, 0x3f1a6bad, 0x5fc27a86, 0xd936adb0, 0x1896811c, 0x0d35ca79, 0x72b8a933, 0x9b0e9d67, + 0x5c8447aa, 0x333f7957, 0x6dc6bdeb, 0xa99e4261, 0x95b40986, 0xcb2e4cf4, 0x7f8d79e2, 0xaa0e6bc4, 0x9cc04586, 0xd74053cb, 0x03d2e4b1, 0xa370c32e, 0x1dc8315a, 0x3e86fa32, 0x20bacdcb, 0xb5d30679, + 0x4c7cac3a, 0x1911cdd9, 0xc5808031, 0x7a988092, 0xf0c0ae20, 0x569f860b, 0x513b49fe, 0xbe71cf47, 0x5b060c12, 0x5848fc2c, 0x6bd3943f, 0x0c954f9c, 0xdff6a770, 0xc6469eb1, 0x79e5aac1, 0x832ac713, + 0xb45d3cd5, 0x293093c4, 0xce03c8d6, 0xbcde06f3, 0xfcefe659, 0x96ddef71, 0x2ec70f84, 0x4560cafa, 0x7cfadff7, 0x5a8be7b5, 0x0e16017f, 0xe8187143, 0x00000636, 0x45490000, 0x42ae444e, 0x00008260, +}; + +const uint32_t uprof_32_len = 1214; + +const uint32_t uprof_192[] = { + 0x474e5089, 0x0a1a0a0d, 0x0d000000, 0x52444849, 0xc0000000, 0xc0000000, 0x00000308, 0x9c026500, 0x00000035, 0x47527301, 0xc9d90142, 0x00007f2c, 0x48700900, 0x00007359, 0x0000130b, 0x0001130b, + 0x00189c9a, 0x50c20100, 0x0045544c, 0x3d190000, 0x366b3818, 0x296ad96f, 0x4b232757, 0x224d2421, 0x1a69d66e, 0x421c193f, 0x2556271b, 0x253e9443, 0x592b2351, 0x3e844129, 0x3e60c565, 0x753b3a8a, + 0x3c8e4039, 0x60295e2b, 0x54265bbd, 0x2b5c2d24, 0x49357f39, 0xcd69468f, 0x5ab85e64, 0x3c62c967, 0x88443885, 0x3f804141, 0x3567d36c, 0xc1633276, 0x2b622d5e, 0x2f366f38, 0x6b382c69, 0x51a75536, + 0x513b7b3e, 0x6b384da0, 0x66d06a36, 0x4f2e6030, 0x6a334c99, 0x49954c31, 0x30347a37, 0x5c292e65, 0x1d471e26, 0x3350a354, 0x8c463071, 0x55af5943, 0x4e366b38, 0x6b384a9c, 0x57b45b36, 0x382e6e31, + 0x6b37366b, 0x27572935, 0x20366b38, 0x3d191f4b, 0x366b3818, 0x19366b38, 0x5729183d, 0x53ab5627, 0x38366b38, 0x3d19366b, 0x366b3818, 0x38183d19, 0x3d19366b, 0x366b3818, 0x29366b38, 0x57292757, + 0x366b3827, 0x29275729, 0x3e192757, 0x183d1918, 0x38183d19, 0x3d19366b, 0x183d1918, 0x19366b38, 0x5729183d, 0x366b3827, 0x29275729, 0x57292757, 0x366b3827, 0x38183d19, 0x6b38366b, 0x46944a36, + 0x38366b38, 0x6b38366b, 0x27572936, 0x29183d19, 0x57292757, 0x27572927, 0x19366b38, 0x5729183d, 0x366b3827, 0x19183d19, 0x3d19183d, 0x183d1918, 0x19183d19, 0x6b38183d, 0x27572936, 0x29366b38, + 0x3d192757, 0x183d1918, 0x19366b38, 0x6b38183d, 0x183d1936, 0x29275729, 0x3d192757, 0x366b3818, 0x29275729, 0x3d192757, 0x27572918, 0x45366b38, 0x57294098, 0x2d633027, 0x6d448c47, 0xa05068d6, + 0x62c8664d, 0x2868d56d, 0xd56d2656, 0xe6ab1468, 0x000000dc, 0x4e527496, 0xffff0053, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xfffcffff, + 0xff02ffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xfff6ffee, 0x03ff1dff, 0x0e03ffd6, 0xff0cfb40, 0xcf231507, 0xb61b2312, 0x85fd2495, 0x0b4ed0e7, 0x70cbaca5, 0xc7f79fde, 0xe7f03dc4, 0xff7853f3, + 0x864ca429, 0xa7dc17ec, 0x7173e5bf, 0x7e96bdb1, 0xb363d536, 0x5d89312e, 0x3e37645c, 0x8c2c9d49, 0x5bdb5366, 0x0593ff6c, 0x78b998d4, 0x67d57f01, 0x007fccad, 0x49831000, 0x78544144, 0xf95bed9c, + 0x1f47135f, 0x3b9b26ce, 0x1721b721, 0x80920490, 0x84120102, 0x4a9c8080, 0x0aada811, 0xa282d56a, 0xb5bc55a8, 0xd5eb155a, 0xe8f7db6a, 0x33befffb, 0xbb3639bb, 0x4863ddf3, 0xf1f3f636, 0x7491a1f9, + 0xbe679f74, 0x31677ce7, 0x2be2be18, 0xe819a2be, 0xb9313d9d, 0x7f7f7cf2, 0x7f6e6e6e, 0xe4cafaff, 0x56ded989, 0xa58b5253, 0x6b4fb995, 0x1c2fa777, 0x5631ef1d, 0xf47e33d0, 0xdaeaf4c2, 0x2fc9b9b3, + 0xcaef465a, 0xd31bd3d5, 0x8c24da35, 0xdb5c6f4f, 0x53356c9d, 0xf7f91002, 0xc9e4b697, 0xad6c4557, 0xb2fdde9e, 0xdafd716c, 0xee2ab8f2, 0xf4bd1c35, 0xb0d10be9, 0x4176bbb4, 0xa60af923, 0xb52fefdf, + 0xf3ef7d98, 0xeeb4976b, 0xee1d9d7d, 0xbd0ed4b6, 0x6107ab73, 0xb1b0d65f, 0x93fa22df, 0x475d68d7, 0x27fbe58c, 0xbbdf6774, 0xf451ba72, 0xefa67a11, 0x4f5a9ceb, 0x38da3fbc, 0xaeb61cfa, 0x93cf18e9, + 0xfaee0fef, 0x67a70904, 0xfb3fa1f5, 0xf409ba69, 0xaf967a11, 0x7f6121ea, 0x39f439b9, 0x37af7709, 0x86e4fe9b, 0x5a8176ba, 0x46a6fbc2, 0x06abd2f3, 0xcb001ea6, 0x6cabcdbb, 0xddcdabb3, 0x6b3d1e7e, + 0xde848a4d, 0xd03a5b57, 0xdbb0b8c7, 0x6bb3fe0c, 0x649dc8cd, 0xbc6fdf8c, 0x989a5611, 0x6d58087c, 0x5b6782b4, 0x6547f2ba, 0xb191ae6d, 0x3f5f74bc, 0x782aa9f7, 0x4b0538d6, 0x47d3ba6b, 0x28dd39e8, + 0xb52e2705, 0x8dc23f82, 0x2bd974c6, 0x346fe2ad, 0xc142346e, 0x6eb526f5, 0xf07cb0aa, 0xab7cf464, 0x8291fc85, 0xfb777683, 0xe47e6fcd, 0xcd60e971, 0xf356f3dd, 0x0fa71a37, 0x5b2b0712, 0x708fa6ad, + 0xbf9e8569, 0x0403edbe, 0xf662b6ea, 0x97a9ab6e, 0xd1574ab1, 0xfe14be96, 0xcd1ae346, 0x74e962c4, 0x30d769ab, 0x06c29f7e, 0x7d38f4cf, 0x9cf8c5a9, 0x8dbbfe66, 0x0c1d5c9e, 0xb416b547, 0x002fad13, + 0x56d558f5, 0xb556960e, 0x99edf3fd, 0x48c8c8cc, 0x823b972e, 0xd0303031, 0x23e8a1ce, 0x2f10ef2f, 0x77d7e1df, 0x4d623cc8, 0xd35c1853, 0x3498dfc6, 0x9c391111, 0x211f2e27, 0xb00d372f, 0x8c47c0c7, + 0x7fc0d2bf, 0xff1b4b6e, 0xd0991f6c, 0x4fd2cba8, 0x04041f5a, 0x586ecd64, 0x5df551c1, 0x0ed692d1, 0xb420007a, 0x485b3663, 0x9af40036, 0x053966dc, 0x8356d437, 0xf91af7de, 0x4201471b, 0x70523369, + 0x03bcb258, 0xcb431e58, 0x745e32cd, 0x6903ad4d, 0x2103c13e, 0x64947780, 0x1960c803, 0x06c65cbc, 0x3adaea5b, 0xda442ded, 0x04a3b32a, 0x0101441b, 0x21fd7e59, 0x555dc150, 0x10b7b402, 0x88d08009, + 0x07c0a899, 0xbb9f4084, 0xc1b61ebc, 0x27fc1555, 0x286097b5, 0x5477962b, 0x810a00ac, 0xdeb2a5a4, 0x59693d0d, 0xdef51339, 0x0a1fccd0, 0x6643ce81, 0x0a2382a2, 0x045ec306, 0xa2b4ea3d, 0x3ded3980, + 0x6953b65c, 0x01029147, 0xb5672825, 0x368396d5, 0xc714aa38, 0x0c087b5a, 0x66320428, 0x4836aa21, 0xadcbc87b, 0x0a1566fd, 0x7808286e, 0xcd043145, 0x758a0201, 0xd16eaa02, 0xe82c1801, 0xa1172e20, + 0xed2dbe47, 0x416299a5, 0x5ebfb159, 0xa1536540, 0x2f231810, 0x1ebe4317, 0xf7f052ef, 0x3c84c8c5, 0x72a3116e, 0x02012f82, 0x89cb243a, 0xdb3d938f, 0x218cd12c, 0xcb19408a, 0xf88cc458, 0x9af4054d, + 0x64e59252, 0x02321b05, 0x07f052ae, 0x424240ab, 0x7c746b88, 0x6108f2c0, 0xc68bd139, 0xb011c39f, 0x6d402341, 0x8e94f008, 0xa3e58115, 0xa216d584, 0xa444e00d, 0x00c38bbd, 0x8bab031a, 0x2eccc024, + 0x88da0202, 0xc60a49cb, 0x9525d3c1, 0xa100b201, 0xd9909120, 0x41015405, 0x22f456d9, 0xa59eada8, 0xbd40441a, 0x962b38b1, 0x80aa0291, 0xa2306c87, 0x26032d17, 0x1fc34678, 0x32698acc, 0x33862821, + 0x2f64e5e4, 0xb3a53722, 0xbe315754, 0x84509908, 0x0c81d325, 0xa059ca46, 0x74b0f202, 0xd7146a9f, 0xa0815401, 0x00700ab4, 0xe625660c, 0x2f521e2c, 0x10fa22da, 0xda142cdd, 0x9bb01cd1, 0x024b1940, + 0xf06c0306, 0x041e61a9, 0xc692c6f6, 0xb3940780, 0x8290bd00, 0x5d808269, 0x069022aa, 0x3cd6330e, 0x2018c999, 0xaa055c80, 0x0f517b10, 0x774664e1, 0x9401df1b, 0x5c6f5131, 0x0fb65405, 0x1c100bcb, + 0x933c7827, 0xe69ba00a, 0xcb8dedaa, 0x4f210170, 0xa844846a, 0xa486c1b7, 0xe2a15ea9, 0xfa0435af, 0x46a4e581, 0x2f7a8448, 0x3e888e92, 0x1a0009d1, 0x02101303, 0x10a72750, 0xbcea3c02, 0xedea3c48, + 0x02217ba5, 0xe47dabaa, 0xa601602a, 0x242350ba, 0x6b06d082, 0xe8270122, 0xf3a0642e, 0xc0cc897c, 0x127c840d, 0xfac48468, 0x45960a05, 0x8a8e78c5, 0x080331bf, 0x38332768, 0x1b872c06, 0x5e6a1121, + 0x6bb61e24, 0x01e0d1bd, 0x119a484e, 0x366a1c02, 0x14743510, 0x3c2338f3, 0x55018ad2, 0xc8cc3800, 0xd4224235, 0x41f57c46, 0x834af9a0, 0x40a3b407, 0x83003808, 0x21bd57d5, 0x885ee9e9, 0x3ae5828a, + 0x62e9cf01, 0xefcf3a18, 0x03ed9f90, 0xd55d58ca, 0xf2c378b3, 0xd17b2846, 0x7b295d78, 0xadab7745, 0xa317e830, 0x8772cd5d, 0x939f2e33, 0x37545e2a, 0xa56ba3a2, 0x1f4cc3a1, 0xd95004e0, 0xe9bf200e, + 0xf6ea2cae, 0x26f243fb, 0x5d517b28, 0xe71d5314, 0x4b80ed8d, 0x042d8094, 0xc8be00aa, 0x1657f419, 0x439dbb85, 0x7ab8020a, 0x508ffd90, 0x418bdd00, 0xf92f75e0, 0x396029b9, 0x677102ae, 0x3fc87459, + 0x628f97e3, 0x4745ef33, 0x201425fe, 0xe10ee18a, 0x24b67995, 0x944c5de2, 0xf75d00ff, 0x858fc8fa, 0x72f0666e, 0xbbdb657f, 0x84f0b6e3, 0x45ec0092, 0x59ff7fc9, 0xa22b3fe3, 0x3e721418, 0x30bd1262, + 0x9ff28910, 0x980aa069, 0x1e5d5324, 0xabe00840, 0x5e338b31, 0x278517e0, 0x652eae98, 0xcefe9601, 0x0135dbe9, 0x9d3a5ed5, 0x3d1472c9, 0xa6a3be02, 0x121857fa, 0x48af1891, 0x0a57baf6, 0xc61c7007, + 0x3bd67fc7, 0x3989e711, 0x7d774c43, 0x2311751e, 0x71fa552a, 0x0d09d09d, 0xd3b00698, 0x4db52358, 0xedac7b4f, 0x09d9e6f5, 0x21a6cb16, 0x202c9163, 0x5205532f, 0xc51f0761, 0x199db881, 0x3dfdd6f2, + 0x4840bfaf, 0xe5b0854b, 0x65404d45, 0xf3ed9d2a, 0x7b47d064, 0xf4db5929, 0x4474a7a2, 0x33a3bfc8, 0x0d951035, 0xd330e001, 0x55c72c06, 0xcc3130a0, 0x3760f147, 0xc2bcb8c3, 0x53f9773d, 0x54694e5d, + 0x676c87c4, 0x97ee026a, 0x77048dc9, 0x81d1b730, 0x91e71ad1, 0x508cb1d1, 0xf5946640, 0x708da675, 0x4bace815, 0xcaee8122, 0x89b800d1, 0x1fcec4ea, 0x01fdd94f, 0x618bad8e, 0x1de5bd86, 0x61523ee7, + 0x171dd31b, 0x87790ff3, 0x01359799, 0x3a2397e5, 0x27ace962, 0xb2c74c6d, 0x0e28b563, 0x4309b4ee, 0xb349e7da, 0x99096287, 0x394ca810, 0x4f51b827, 0x579873f8, 0x17e60160, 0xb66b2741, 0x970c43f8, + 0x1b4e027b, 0x7cbcd8c3, 0x15700a46, 0x4664db0d, 0x5e52db52, 0xd25e731b, 0xd847468b, 0x7a8b27bc, 0xd56dc3cc, 0xa6774e28, 0x1a4149d3, 0x2cdd45ca, 0x8728cb9b, 0x4e33eb80, 0xd0e71626, 0xadef17a0, + 0x63281fb9, 0x3982aec5, 0x52661ce4, 0x57516883, 0xc8308317, 0xd78b6595, 0x7e2d3e89, 0x9b06fe07, 0x09a81529, 0xd71f0bc0, 0x0c56ac04, 0x84c1b2c0, 0xd36318b1, 0x5f70939c, 0x48f95326, 0x53c026b6, + 0xe7d65964, 0xf9e7c1f1, 0xe1372772, 0x600e738c, 0xcab817b7, 0x7cde1d04, 0xd0863fc8, 0xb7cf63c6, 0xa65b31c4, 0x80d1e5c0, 0x5b8c2bb3, 0x4df461a3, 0x2bdd8fc0, 0xe7996d8e, 0xc037adf2, 0x04d4bf30, + 0xa477af70, 0x5b883b93, 0x1b98c466, 0x157df365, 0x963923ce, 0xba42d6b5, 0x8b479623, 0xf0097ddc, 0x722a6330, 0xbbf8b6e6, 0x53c72bb1, 0x3843f1e8, 0x5bc3a140, 0xe734e42a, 0x77630cbd, 0xaf01cc3a, + 0x70080b1d, 0x089e1d01, 0x94e65df9, 0x8c337f1f, 0xdc5767fe, 0xebd29d8f, 0xcec3600c, 0x5123cc30, 0x3b103127, 0x07b5b884, 0xeb04d18d, 0xd9d4a24e, 0x9a22cb1e, 0xa9eb2c1d, 0x1628d1e5, 0x198bb2c9, + 0x632a0263, 0x6e2ece63, 0xeee1ecff, 0xb571fef2, 0x73dbba15, 0x41857220, 0x329bcab4, 0xb44fe177, 0x2075e0be, 0x78b6c3e6, 0xc854ecaa, 0x99ce0287, 0xd632f202, 0x98b7e13f, 0xf4a46103, 0x5fc04d62, + 0xd8933a38, 0xc46cd839, 0x8647e9e7, 0x7db221c8, 0x13b674d1, 0xe71478ed, 0x2e4523a4, 0x0273f687, 0x1434c912, 0x20d2a602, 0x0b88284e, 0x2f6e8df2, 0xb8a1f8f7, 0x772d0f25, 0x30a9bcd8, 0x5c26540a, + 0xc3031ce0, 0xc2bb0e1e, 0xb4970605, 0x8df710f8, 0x609f1e23, 0xb98b4e42, 0xcdec30ab, 0x1826bb9b, 0xfeff80a1, 0xa853812f, 0xb7eb531b, 0x88027bb6, 0xebe8c961, 0xdbda46cc, 0x50add863, 0x38ac26cc, + 0x8e517394, 0xe8758e3f, 0x6a34c362, 0x905b889f, 0x272e3347, 0x3dd841ef, 0xd450d002, 0x050a6294, 0x9a2d086f, 0x63b0bb08, 0x1e217ff0, 0xc6185a77, 0x87f874e2, 0x9138a379, 0x29bdd977, 0x0cc5d397, + 0x2c7e1df9, 0xc0555ceb, 0x8a457f9f, 0x472cbebe, 0x968b102d, 0x8f31c7d0, 0xcee2332f, 0xe8238246, 0x3ab85407, 0x3b58c53c, 0x5e3ea1ca, 0x8398bb00, 0xb514c25c, 0xd405e3d9, 0x64763193, 0xccf1fe6d, + 0x7aaa17a0, 0xf0e31f26, 0xbc86cb72, 0xb2b65d13, 0xf00b5400, 0x61bcdb1e, 0x14abf96f, 0x4f0818d6, 0xc3c69360, 0x009d7b4e, 0x9e63f7c3, 0xa78062d2, 0x431cea56, 0x99d43dff, 0xa3b22088, 0x30ef2cd6, + 0x58f745c1, 0xb78478d4, 0xe40afe12, 0x90588a6f, 0x29d8bdad, 0x32e1fd06, 0x1418c09f, 0x6d9ca631, 0xf31fb3f8, 0x03faf199, 0x5cc3154c, 0x073b7738, 0x4eec0119, 0x1d77fe3f, 0x0533ce4e, 0x3bbdf031, + 0x532fea67, 0xe07531db, 0x83d44f0f, 0xc531655d, 0x324ccd65, 0xb2c7f6cf, 0x22f29dc9, 0x00a069e4, 0x42250253, 0xe38c00ec, 0xe4fcaa73, 0x11b55002, 0x338c033a, 0xfc4979ce, 0xc22330f3, 0xbaeee7f0, + 0x49bcc39f, 0x0122130b, 0x85775997, 0xe3ebeb3f, 0x4d6a38fe, 0x31b655b0, 0x8247762e, 0xe0e50e5c, 0xc3f3de86, 0x3d8c4294, 0x9162e364, 0xde80e467, 0x1a50e167, 0xa699f30e, 0x8fe62e2b, 0xfdd50bbe, + 0xfc121968, 0x3fa34820, 0x12a435b3, 0xc58c9b9c, 0xdcafd552, 0xf9412e51, 0xf57739ac, 0x700aa82b, 0xc18f2a94, 0xec73606e, 0x5845adc8, 0xab7176ec, 0x00a90e5d, 0x5dfa4dbb, 0xf6428205, 0xef209596, + 0x87c0fc13, 0x35573b87, 0xfa0fdb13, 0x4edf4634, 0x7627cf80, 0xbb24b647, 0x18a44392, 0x10c26982, 0xa53712b4, 0x75001402, 0x63583572, 0x26444f1e, 0x821a4452, 0x0156bca5, 0x9eee6682, 0x5a600166, + 0x467586a7, 0x043d7299, 0xcd47f4ed, 0x1691a002, 0xf2b5163d, 0x9a070467, 0xf7f95a2b, 0x129169c3, 0x633294d6, 0x04d49e11, 0xd0156bac, 0xd5b9daa6, 0xcad76822, 0x4d64fb7f, 0x015682c0, 0x0a9d89b2, + 0x7806cd09, 0x804d4dfa, 0x028d3be1, 0x1686cec0, 0x500d4211, 0x02b383c0, 0xf1b59701, 0x76e72b77, 0x14c9c88d, 0x4282002c, 0xd1dc306c, 0x68dd2026, 0xcd7ea6fe, 0xc06585fc, 0x9b5bf860, 0xf3469d80, + 0x8037698f, 0x0c1a8b30, 0x85ab7fbf, 0x55e1bbbf, 0x7f3b5da0, 0xc050a4e6, 0x7c514d0b, 0x1e25fa18, 0xa3c58005, 0xff9da5cf, 0x80a171fc, 0x08b5efa9, 0xe5e7ee80, 0xa087cc91, 0x30a195e4, 0x0ea1a6c0, + 0xba18a29a, 0x758392eb, 0x019c750c, 0xa83114e0, 0x478257cc, 0xb53402f5, 0xabbbd073, 0x9df20832, 0x06d10cc2, 0x1c7dcd49, 0xd4618a23, 0xe288690d, 0x14070a43, 0x95fd4a29, 0x236f632c, 0x2af68fc4, + 0x4eb730cc, 0xfdb80bd5, 0x3e017aa3, 0xc81837e8, 0xd8b9da41, 0xdaeb3825, 0x04891902, 0x851855ed, 0x017af1c0, 0x316a1a53, 0x797c0554, 0xed3c6982, 0xab96d392, 0x4a2ebf58, 0x021c5fc1, 0xf507c306, + 0x6ea9a002, 0x16f9b188, 0x26b1d605, 0xfc965973, 0x98a14b78, 0x6bce568b, 0x2f011a05, 0xd5d602c5, 0x12300107, 0x1106e3ea, 0x973160ec, 0x76e4cc59, 0x1981d2da, 0x1b6afab9, 0x74f9b176, 0x4dada3b6, + 0x81514c32, 0x02c5275b, 0xe5a93f0c, 0x4b394d0f, 0x8b491b34, 0x0225c773, 0x002b50fe, 0xc9a2ebba, 0x7890d425, 0xebf0c190, 0x80052a0f, 0xf91d7e83, 0x0a929fcf, 0x4dd95058, 0x473f6f40, 0x2d11aa68, + 0x55fca536, 0x54d0d4b7, 0xd2192a94, 0x804935e2, 0x850c62ca, 0xfc8e98c0, 0x935865c5, 0xf9e7410f, 0x1e028eac, 0x53a3f890, 0x6efe1a8d, 0x06b625a0, 0x30db9f94, 0x580b219c, 0x410fd350, 0x8eaa2d23, + 0xdf9b1292, 0x3aa80590, 0xbdd74014, 0x6a056314, 0x1bf8079c, 0x0130a8ae, 0x875696f4, 0x21b7fc7e, 0x7ea61803, 0x9d001656, 0xc70f350a, 0xe21171e9, 0x62b1aa8f, 0x43437526, 0x8c8e9a51, 0x3ed2db0e, + 0x8ac616ae, 0x3ce802a7, 0xbf8dd49f, 0xc29576f0, 0x5d6fe004, 0xfb7fc6e9, 0xa5009c8d, 0x05540af1, 0x09ba5d4c, 0xa46d28b8, 0xdf28f885, 0x9181e693, 0x64c52756, 0x32f30e8a, 0x4099c011, 0x2181113e, + 0x827af9a0, 0x8011b216, 0x41d2df21, 0xd19a79e7, 0x6dc8151a, 0xde099d5c, 0xce8009ca, 0xc69dbaf3, 0x97f12b1e, 0xa0a5109f, 0x78c51691, 0x8a9b0a9f, 0x1bf84006, 0xf42ca276, 0xc57a788b, 0xd686d030, + 0x638e0163, 0x517b7ae8, 0xce0618a7, 0x015c6201, 0x96f42953, 0x04fdcd16, 0x5214f605, 0xe0530568, 0x0dd013ab, 0x011f5a45, 0x512d1b60, 0xe863d400, 0xfa4d3957, 0xb3c5c084, 0x4e2bd4ca, 0x8271be44, + 0x5d11330e, 0x25ca330e, 0x7df20040, 0x46055407, 0xaf3fe0f5, 0x9d55032a, 0x4cba7488, 0xa6d02141, 0xe45f8087, 0x6e31389b, 0x87bd6613, 0x11e800da, 0x73c10f02, 0xf9d20024, 0xcb5ba1d8, 0x96b6c126, + 0x6a41e000, 0xce7a1dac, 0x25b8853f, 0x63218a7b, 0xa5a926ea, 0x3d38eb30, 0xbcfe61a9, 0x2b2800b6, 0x9614b620, 0x18d02856, 0x9b9015c0, 0x81fe90a2, 0x518eb840, 0x45627587, 0x03c15dd6, 0x86a809ad, + 0xac8ae4db, 0xd43f286f, 0x6c94809e, 0x80206ba0, 0x78755c93, 0xb4bcbe41, 0x9b46e6e8, 0xd8685b2a, 0xd22f6406, 0x9d54102b, 0x90141521, 0x5d0e8dc7, 0x1b9aa186, 0x740a2d08, 0xe403e508, 0x91a0211f, + 0xfcd9a5af, 0xc13fa91f, 0x76202828, 0x6aeb2c15, 0x536c206a, 0x82800ff5, 0x7e73e47b, 0xbdec1533, 0x847f1c6e, 0xd464e48f, 0xef7b06bc, 0x23e9235d, 0x22d303ac, 0xcfe85373, 0xf84c6b9f, 0x07f0b10a, + 0x8cd8e679, 0xbe7dbcaa, 0xc42be151, 0x3764afd4, 0xc15c3572, 0xd9944f6f, 0x2de16150, 0x5c1b9631, 0x371fe795, 0x0aaa7dde, 0x46b6277e, 0x9bcaad0d, 0x7d9a1a67, 0x10ee1224, 0xaab0d08d, 0x2fedccdc, + 0x1d3105e3, 0xe6102635, 0xf7b41dcf, 0x0f858354, 0x034243a4, 0xe6fcda6a, 0x1f246699, 0x847b8492, 0x50e07a8c, 0x13ad7bde, 0xf7db8c7d, 0x40f52908, 0x29b4fca1, 0xd4c61fa9, 0xc49fcdfa, 0xf6439e1d, + 0xcc395745, 0xf07f3d3f, 0x2c668b83, 0x54e4907c, 0x7cde50ca, 0x57eeefa9, 0xade57531, 0x42281828, 0xb351ef79, 0x092aaeea, 0xae5dee2f, 0x431a0c2b, 0xf3c3d6f9, 0xb75a9b27, 0xd14068d4, 0x4fca1940, + 0x356a9f1f, 0x3fd632fb, 0x2565a9dc, 0x6fe9ed75, 0x297f5ee7, 0xad4c39ec, 0xf979bc7f, 0x29556fbb, 0xf87bdf98, 0x8a2f99cb, 0x0b53197d, 0xf9ddee2f, 0x50d907e9, 0xee7a7ede, 0xe516bbf5, 0x5a98951c, + 0xdfc3f5ff, 0xfe6c1e8f, 0xdd5db7b6, 0xa7bdedf2, 0x7a3c1f0f, 0xcb4df2fc, 0x8e355623, 0xf2753d9f, 0x4f27bbe4, 0xf379bcde, 0x93bbdde4, 0xfb39bea7, 0x15fea18f, 0xc2cff15f, 0x69e401ff, 0x6f1037bd, + 0x000063a5, 0x45490000, 0x42ae444e, 0x00008260, +}; + +const uint32_t uprof_192_len = 4942; + +const uint32_t uprof_512[] = { + 0x474e5089, 0x0a1a0a0d, 0x0d000000, 0x52444849, 0x00020000, 0x00020000, 0x00000308, 0x24a6c300, 0x000000c8, 0x47527301, 0xc9d90142, 0x00007f2c, 0x48700900, 0x00007359, 0x0000130b, 0x0001130b, + 0x00189c9a, 0x50c20100, 0x0045544c, 0x3d190000, 0x366b3818, 0x296ad96f, 0x4b232757, 0x40994521, 0x2a224e24, 0x9644285a, 0x1b431c3f, 0x26193f1a, 0xbf612451, 0x68d56d5d, 0x42265628, 0xd76e3e93, + 0x48954b69, 0x2567d26c, 0x5f2b2354, 0x42894529, 0x2d468f49, 0x3d192b62, 0x5bbb5f18, 0x68366b38, 0xcf6a64cc, 0x346a3765, 0x5b2b5d2d, 0xab5657b2, 0x2e683053, 0x672e6e31, 0x873d62c9, 0x38703b38, + 0x65316834, 0xa25260c5, 0x3a763c4f, 0x3f2f6231, 0x70343a8b, 0x59b75d31, 0x3e50a654, 0x672e3b7a, 0x3378362b, 0x38327535, 0xaf59366b, 0x3f814255, 0x38275729, 0x8040347c, 0x4b994e3d, 0x381d481f, + 0x833b366b, 0x5ec16237, 0x44357338, 0x7f3a4285, 0x27572936, 0x21366b38, 0x6b381f4b, 0x183d1936, 0x4e366b38, 0x57294a9e, 0x366b3827, 0x384d9d50, 0x8e40366b, 0x366b383c, 0x38366b38, 0x6b38366b, + 0x366b3836, 0x19183d19, 0x3d19183d, 0x366b3818, 0x38366b38, 0x3d19366b, 0x438e4618, 0x38275729, 0x6b38366b, 0x366b3836, 0x29183d19, 0x57292757, 0x183d1927, 0x38366b38, 0x3d19366b, 0x27572918, + 0x19183d19, 0x5729183d, 0x366b3827, 0x38366b38, 0x5729366b, 0x183d1927, 0x29275729, 0x3d192757, 0x27572918, 0x29275729, 0x6b382757, 0x183d1936, 0x29275729, 0x3d192757, 0x366b3818, 0x38366b38, + 0x6b38366b, 0x366b3836, 0x29275729, 0x3d192757, 0x183d1918, 0x38183d19, 0x3d19366b, 0x366b3818, 0x19183d19, 0x5729183d, 0x366b3827, 0x29183d19, 0x3d192757, 0x27572918, 0x6e183d19, 0xd96f69d6, + 0x6ad96f6a, 0x6f52a756, 0xd96f6ad9, 0xf9a3176a, 0x00000094, 0x4e527496, 0xffff0053, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xff04ffff, 0xfffffffc, 0xffffffff, 0xffffffff, + 0xffffffff, 0xffffffff, 0xffffffff, 0xfffff6ff, 0xfffffffa, 0xffff02ff, 0x04ffffff, 0xfb0cff06, 0x2910ff17, 0x20ff95ff, 0x3489d5e9, 0xef2917f1, 0xffb1c0ce, 0xaf3cdcf1, 0x5163e83c, 0x324762a8, + 0x4426c00f, 0x58734b9f, 0x1b73c53f, 0x655bd14b, 0xb9dd94dc, 0x70e3c652, 0x8b217cb9, 0x69807832, 0x80a1e597, 0x86a3adc8, 0xac5507d2, 0x127dd934, 0x00bc7d27, 0x49422f00, 0x78544144, 0xf99ded9c, + 0x9a46d47f, 0xddba6987, 0x8f7df03e, 0xc19f17b1, 0x303e3076, 0x83180d8c, 0xcb9a7301, 0x662f1009, 0xe1308136, 0x92c0d848, 0x3930201d, 0x99612048, 0x6efff9ec, 0x492ae955, 0x6f4952a5, 0x5ddd5549, + 0x19f32fcf, 0x7a356da7, 0xdf7d555f, 0x8763b4ab, 0x183060c1, 0xc183060c, 0x0c183060, 0x60c18306, 0x060c1830, 0xf271d543, 0xd7f3a5f4, 0x6f6f6dae, 0x5f6c203f, 0xd2fe76bb, 0xbfaa9399, 0x4e432195, + 0xf67eda5e, 0xdbc3eafa, 0x795c6f37, 0x27717d75, 0x5fd7dd93, 0xf371badd, 0x6f8b9df6, 0x7f383eee, 0x3637f55a, 0xf6f4e148, 0x6e77abdd, 0x9bbcb95e, 0xd70e736d, 0xfb7f7d6f, 0x9747ebe2, 0x27feaa8e, + 0x7a715218, 0x2f8bd7fb, 0x6c4f5c6e, 0xb78b10f7, 0xf37c3e6e, 0x7fea8ce0, 0xa398418f, 0xbc5eefe7, 0x23f92af9, 0x8dfae7cf, 0x8db66f3b, 0xa39cd04c, 0xb9df5ed7, 0xcbc3b111, 0xdbadf760, 0xea303fbf, + 0x3cce4f40, 0xeda5987b, 0x72b82f29, 0xa379f5e7, 0xd2e2b40c, 0xe56fb7af, 0xcb8f7b61, 0x66bf7e2f, 0xce0b409c, 0x55fb7afc, 0xb877c6be, 0x8138d37e, 0x7cbf4e62, 0xf18d4578, 0xfedf2e1d, 0x8a0804d5, + 0x66e6f4b8, 0x2c7e7b2e, 0x9b6e2e57, 0xb66f2085, 0xf47e2a2f, 0xfdbe5cfd, 0xb90704cc, 0x9dd1f471, 0x1e2daacb, 0xafc5f166, 0x407207c6, 0x2c5beb4f, 0x903e34de, 0x6a1ed731, 0xc58b7d6b, 0xba4ccf17, + 0x5d2e2b30, 0x4fd839cd, 0x0f3bf5c7, 0x6aa9df54, 0xdfafe4e4, 0x67ef4ecb, 0x26abadc7, 0xf6cb9436, 0xbd75091d, 0xd37dd93c, 0x38d2056c, 0x6a86faf9, 0xf2e1a67b, 0xefaa4bc5, 0xfe70555c, 0xfc3d8561, + 0xbc5fbb1e, 0x18221934, 0xd0565f47, 0xd6e2cfce, 0xa618131b, 0x8a9bf4e7, 0xae7cfd90, 0x413b343f, 0xac5cce3a, 0xcfdfb5d8, 0xaadb17ee, 0x720565ef, 0x951e8efe, 0xff7c391e, 0xacfdf552, 0xaeddae30, + 0xcc3fadec, 0x0f466795, 0xa2fdb30a, 0xb98fccda, 0x80b8d775, 0x5537db10, 0x8c2b2a9b, 0xad70080b, 0x077e9f2a, 0x9c3102e3, 0x3fbdbaaf, 0x072315c8, 0xa77339a3, 0xb98fcdda, 0x36428261, 0x5356afa7, + 0xc5e3c7e0, 0x5af7d579, 0xd6be8e43, 0x2643d5bc, 0x729a1fbb, 0x5a478071, 0xcf6678f5, 0xb54ddff5, 0x0bfce298, 0x9fc906d5, 0x3a0d465b, 0x2b58be9c, 0x9b9fbf9b, 0xee100a46, 0x52ec9a2a, 0x5345e2cf, + 0xbe71de2b, 0x227ef36a, 0x7df5aabc, 0x84cd64e0, 0xa8bc517e, 0x07816ee9, 0x01f0d055, 0xcd4df165, 0x3f4f8766, 0x5fc485ac, 0x546a373c, 0x59acbe0c, 0xf16417f1, 0x05260d6a, 0x7d51df4e, 0xcae275db, + 0xc8f6d535, 0x9435479b, 0x776115f8, 0x27022d6d, 0xa21feecd, 0xb6c09496, 0x160cff11, 0x3848d75f, 0x79aaf470, 0xacdcd9fc, 0xa5e11a89, 0x67df55ef, 0x48fcb97d, 0xe7b275b5, 0x7b4c9559, 0x2ac5c464, + 0x1f2782d7, 0xbac3beaa, 0x53aaa373, 0xac6be7c3, 0x8b0d23ed, 0x295b54cf, 0xe59aee3b, 0x5587845f, 0xbd1c0dba, 0x56cefaa8, 0x0db9d50a, 0x63c8a99c, 0xb970d43e, 0x7a998f0a, 0x09bf7260, 0xb57bfdd8, + 0xd75e8525, 0x5247e37a, 0xb755576e, 0xfd1351d8, 0x154ae625, 0x26338109, 0xcb8297f9, 0x76423355, 0xa47e74de, 0x6aaef162, 0x91e1c9cb, 0x72d3fe89, 0xe0a41ab5, 0x4bfb536b, 0x4148154f, 0x7101fd93, + 0xc42bc2a3, 0x1fe74d27, 0x1d155b90, 0xf2369c0c, 0x0ae5cc1f, 0x633d17ee, 0x58127f8a, 0x112ed8ac, 0x61c9fe13, 0x88168577, 0xbfec99af, 0xb6ab5e2c, 0x981e1a65, 0xbf1e5ff0, 0x7266b6ab, 0x8fea9a5e, + 0x5a562e4c, 0xb199e84a, 0x561e5cbf, 0x35dc0796, 0x9dcd97f6, 0xbb146a4a, 0xf2bfd26b, 0xf80f20a9, 0x0b3fd8d5, 0x078a576e, 0x233fe798, 0x8c03c42a, 0x4ee333fd, 0xa3412825, 0x02a433ff, 0xff130162, + 0x1ef68a67, 0x3fd8c8f0, 0xb6aa2e5b, 0xc6079f30, 0x557359fe, 0x5c798db5, 0xecfff933, 0x7432b8d1, 0xf2ffd4de, 0xe166db40, 0x9ffea633, 0xed35bb0b, 0xfa6d3910, 0x5162727f, 0x93b12ecb, 0x372fffa6, + 0xa478ebae, 0xcdffe99c, 0x75fa5791, 0x7d555f8b, 0x1b8b6a4f, 0x988c4dba, 0xdcce6002, 0x68252bd6, 0xa3b94012, 0xbce84255, 0x8fe50049, 0xd380e946, 0x055fece6, 0xfce6d6ec, 0xfd35a380, 0x5c7527de, + 0xd06c65d7, 0xe22b000c, 0x80a11e8a, 0x865200e9, 0x35c0a116, 0xe3a80013, 0x9beb6a8d, 0x9462900a, 0x008c85d7, 0x21728b54, 0x8ac03268, 0x55b6a1b9, 0x8011a5fc, 0x24da51aa, 0x0064c978, 0xf8f0a3d5, + 0x0f4aa388, 0x9d9b9f00, 0xba9a9a9d, 0xe88c3880, 0x43ececec, 0xf6f6f56c, 0x3a1c471e, 0x31196874, 0x8d373737, 0xfb2108f8, 0xeaf853f2, 0xc9d5de53, 0x1fabbde5, 0xd99eaeb2, 0xd5163de5, 0xcda80735, + 0x26285d00, 0xd5d115a6, 0x188435d5, 0x7d5d841c, 0x3bd5f93a, 0x97dbabac, 0x5dce58ff, 0x1f30a9d8, 0x00ad48f4, 0x04dc9fd8, 0x25f55d34, 0x613bd5c8, 0xd40e2f57, 0x11bac835, 0x19c064aa, 0x83c03dc5, + 0xd48c0b30, 0xae01b5d5, 0x80740c3e, 0x1e893abe, 0x038952f0, 0xcc00ce28, 0xbc702d01, 0x3fc0790e, 0x38fcbea5, 0x75abf2f0, 0x49403d12, 0xac8de036, 0xc3929f6f, 0xadec0b40, 0x0a407883, 0x54a7e009, + 0x6e03d327, 0x38145fe6, 0x0004baaf, 0x9a3b835c, 0x9aea05b1, 0x54007a53, 0xab90b402, 0xdc80f3cb, 0x2a8e85c7, 0xae00021f, 0x0596c3c1, 0x41c07ad2, 0xda7403a0, 0xe923df97, 0x0b1ee46e, 0x43014f2a, + 0x3aade0d7, 0x024c0790, 0x275abf2f, 0xe4ae03d9, 0x56de129b, 0xae86029e, 0x107533c1, 0x03a8f80f, 0x9d52f978, 0xb5c80f6c, 0xfaa4f449, 0x18684026, 0xf10756d6, 0xd5e03e80, 0x9757798f, 0xf3bb01e5, + 0xa0d3179c, 0x06b80c09, 0xa81650b7, 0xf49bde6b, 0x700950ea, 0x07be4eb8, 0x136fcadc, 0x8001bf50, 0xda4ee0d7, 0x49f57502, 0x012a003d, 0xf032eba6, 0x140edc80, 0x000360d6, 0x75abc1af, 0x4b000f10, + 0x0cba8680, 0x4db7203c, 0xb0047940, 0x0706be13, 0x03d0902c, 0x1f57817a, 0x925d5c0f, 0x0914e407, 0x0a05d168, 0x1643835c, 0x2301e648, 0x10b80740, 0x0f1cbcba, 0x5447a7c8, 0x4380b787, 0x05b0e0d7, + 0xd413eaea, 0x1d018f67, 0x797c3a60, 0x4b901e29, 0x8cb5e83a, 0xba050a6f, 0x2d85cf06, 0x5a03c490, 0xc3df9781, 0x0f14bcb8, 0x7026a1c8, 0x302e8b46, 0xcfdc1ae8, 0x07b880b0, 0x3d5e071c, 0xf34901bc, + 0xc3fbec80, 0x9af8cb6e, 0xf06b8060, 0x04d33d50, 0x301e8275, 0x2980740b, 0x7865d5f6, 0x8facf5c0, 0xe120790e, 0xb8004a4b, 0x2db08f06, 0x2d01ea20, 0x6d600740, 0x203cb240, 0xa3a0eee3, 0x7600a41a, + 0x43e0d7c2, 0xe6200e51, 0xcbc05d01, 0x2c8024b3, 0x1938c80f, 0x1bfc3bf0, 0x06ba00ca, 0x803a468f, 0x010e0d88, 0xba083eaf, 0x67df1c7a, 0x50f503a9, 0xb835e080, 0x04da39b5, 0x02983602, 0x0484403a, + 0x5901e590, 0x8e5d40ea, 0xc1ae0382, 0x080b1cc3, 0xe80060d8, 0x40125100, 0xce5c0792, 0x9afc4bae, 0x011022fa, 0x72f706ba, 0x06d6201d, 0x33cbc043, 0x1e895063, 0x48515d90, 0xa140b397, 0xd70cf06b, + 0x58367102, 0x239d5e06, 0xc078e401, 0xc23d5662, 0x4cdfa0cf, 0x9835d016, 0xdc080127, 0x1314cd5e, 0xe3663e49, 0x80f60eae, 0x5fd8d93b, 0x27701093, 0x881e0d78, 0x1d5d40b3, 0x3808686f, 0x8e5c5491, + 0x03d83abd, 0x550509b2, 0x40b8020f, 0x58cee0d7, 0x203d700b, 0x8040095b, 0x12660dce, 0xe6d69e0a, 0x35d0106d, 0x02de3578, 0x4e800f1c, 0xa1a54524, 0x16407aa5, 0xd0e8c727, 0x5d016206, 0x06f14b83, + 0x1a1e8ba8, 0xc5244e82, 0x1e19004a, 0x12fe4570, 0x680e4ba0, 0x742a7827, 0x9102c852, 0x04bd901e, 0xf201e190, 0x683545c7, 0xae81b604, 0x1d0567c1, 0x8003c220, 0x58fc57ee, 0x0f30eaf0, 0x3bbb2578, + 0x7d0e92ec, 0x35d01660, 0x138ed198, 0x00606b01, 0x2424913a, 0x80782401, 0x1fa4a3ec, 0x548dbe82, 0x64706ba0, 0x03c84e2d, 0x5917eea0, 0x01ec9689, 0x1ac21392, 0xa9d0002d, 0x5c44b8d0, 0x2740078c, + 0xd12ba292, 0xee403d92, 0x02d1a012, 0xc5835d00, 0x07a47854, 0xe0860f40, 0x7ae5a254, 0x2a9965c0, 0x0005a740, 0xa82a153a, 0x003c23c2, 0xc25d7eea, 0x01ec0012, 0x8d009732, 0x1af00016, 0xac11e89c, + 0x420e0e0b, 0x04812fdd, 0xc4807a80, 0x468ce025, 0x2474000b, 0x5823e124, 0x81191817, 0x9125244e, 0x900f5000, 0x93e804b7, 0x848f8003, 0x016a4c1c, 0xa74007bf, 0x3fae9342, 0x96900f10, 0x4fad380b, + 0x153e1012, 0x120cd31a, 0xf75001e3, 0xfebc4d6b, 0x56403c40, 0x2a9f503a, 0x1af84ec0, 0x038537bc, 0x75001e78, 0x0feb14bf, 0x552403c0, 0x536a4e04, 0xd700c406, 0x66d88ee0, 0xd807b610, 0xdfba8046, + 0x4f5754f1, 0xa40f01ed, 0x7d368038, 0x1ae81800, 0x05953fbc, 0x4a800f5c, 0x4feb5480, 0x29b80f7a, 0x34fafec3, 0x80b66102, 0xe94be816, 0x80079600, 0xf5ba4025, 0xc807bd27, 0xa6d20d68, 0x9d009013, + 0x066a150a, 0x74023489, 0x04953928, 0x43c07880, 0x4ba00e09, 0x7405b02b, 0x19ac5f32, 0x5008ca24, 0x82134bf7, 0xbd392458, 0x7c376280, 0x649af456, 0x4e01020b, 0xbf8b1e85, 0x800f3cd5, 0x094a5fba, + 0xf800f000, 0x0cba2199, 0xa7405903, 0xdfc70f42, 0x80119e69, 0x025a97ee, 0xeb80f7a4, 0x69f0aad0, 0xf0c0b29d, 0x66f150a9, 0xc0331610, 0x0097a7ab, 0x3a003de9, 0x38c9a829, 0x327403b0, 0x0833413f, + 0x28256803, 0xef4804bd, 0x0e803701, 0x080c55a0, 0x9f650a9e, 0xb40c22cd, 0x01200917, 0x2c007bd2, 0xdd247c12, 0x53a02084, 0x6cc1bca1, 0x07ee8001, 0x3da90092, 0xc8647600, 0x8446f90b, 0x4d0a9d00, + 0x60120cd8, 0xfceaf02f, 0x3cacf4a9, 0x03221160, 0xd8046ab5, 0x150a9f09, 0xa366c96d, 0xc12910a2, 0x016d2003, 0x7701d55a, 0x92f2c782, 0x80f88337, 0x254122f6, 0xb80f4a40, 0xcd007002, 0x2a742024, + 0x563f6934, 0x0848e000, 0xf4a4025c, 0x5032f480, 0x5ce13393, 0xe9a153a0, 0x1f0eb18a, 0x412eaf03, 0x901e5802, 0xf4fb6cf4, 0xe3d112ef, 0xb50f4850, 0x10b41861, 0x10024329, 0xea407a92, 0x5966a06c, + 0x5e58f040, 0x071066ca, 0x4c48bd25, 0xda407a92, 0xb35ff161, 0xa06a6331, 0x9b69a153, 0x90f46c41, 0x1e848400, 0x03a53b70, 0x6c377e28, 0xde58e808, 0xe59dfc56, 0x757807b0, 0x90801259, 0x165203cc, + 0x7268cf85, 0x540b9c28, 0x0e1dc402, 0x0cda07c6, 0x10024d22, 0xba407992, 0xbd568054, 0x58e81880, 0x7dfc5b5e, 0x066d00e5, 0x08012791, 0xfbe03c89, 0xc3a00e54, 0xa068419b, 0xf12d7963, 0xd0079577, + 0x004a64fd, 0x480f2242, 0x25e07273, 0x680076dd, 0x70e45631, 0x801a68b0, 0x012a9757, 0x203c4908, 0x75a5bbcd, 0xa783034b, 0xa0fd0142, 0xa78f8c88, 0x0f02434d, 0xa015d348, 0x96000edb, 0xc537c100, + 0x8bda0325, 0x10025924, 0xf2407812, 0xca5d49b7, 0x80a994d0, 0x9b03e4f6, 0x21238041, 0x410025d2, 0xef9203dc, 0x82f59a01, 0x60f2c7c1, 0xe02520cd, 0x004be5d5, 0x2407b082, 0xdbb403df, 0x5899a001, + 0xb1fd29ee, 0x19910b40, 0xb0820048, 0x038d2407, 0x8102cbb4, 0x8e04f2c7, 0xa3a0e5e8, 0x7a882004, 0x905cd240, 0xa487af16, 0xf1004a81, 0x0098f9f3, 0x91b24247, 0x0f410400, 0x01d09178, 0x84066b34, + 0x9ac2854f, 0x3ff2da41, 0x31184497, 0xac9dd80f, 0xa7527c26, 0xd0084143, 0xc3e26c4c, 0x0b407c79, 0x9456b091, 0x203cc418, 0xb3584d59, 0xf09df076, 0x87e4d0a9, 0xdb40d0f3, 0x00933391, 0x480f2104, + 0xd3ae1ed6, 0x2a1670b9, 0xeaeaf2c7, 0x9d4f7cb2, 0xe03c4404, 0xa323a27a, 0x8506eb34, 0xe2c2854f, 0xfba066f0, 0xac012965, 0x01e0282f, 0xb5437a49, 0xa782034b, 0xf8716142, 0x99eaf02a, 0x8082004a, + 0x06492407, 0x000ad76a, 0x35850a9e, 0x5e059c83, 0x40095b3d, 0x2480f788, 0x17690607, 0xd7df8204, 0xbbba0ddc, 0xaca489c0, 0xd6210025, 0x82041203, 0x020bb547, 0x8158c5a1, 0x8071c537, 0xb9910c1e, + 0x5eb02004, 0xfc43e16f, 0x5cedb8d0, 0x68049a8c, 0x066b0618, 0x10025fd1, 0x7c403ce2, 0x5ebb4440, 0x2854f010, 0xda0419ac, 0x41cb0c8e, 0x01e31388, 0xba8243e2, 0x5fc383f5, 0x066b08d6, 0x0323b601, + 0x10135267, 0xdda201e3, 0x0049fae1, 0xf4563168, 0xea6c714d, 0x38140a81, 0xbe049a93, 0x83668807, 0xd00093f5, 0x37e150a9, 0x9e8131c5, 0x432b050d, 0x01eb8324, 0xbb4022a2, 0x48f02066, 0x0419ac08, + 0x979c8eda, 0x3cf02004, 0x482db440, 0x7d00093f, 0x70ec2359, 0x00918ab0, 0x08079e04, 0x8ead008a, 0x8b40c787, 0x14de89b1, 0x40a80617, 0x04009473, 0x6588078e, 0x68e7ce02, 0x79f2315a, 0xd014735f, + 0x2760a1b3, 0xda20a553, 0x01cac403, 0xe7854dba, 0x08e6bef6, 0xb0c8eda0, 0x10d2a993, 0x736201e9, 0x0e9d7ae2, 0x848e818c, 0x9c004cc0, 0x23360248, 0x03ca22a6, 0x21de8516, 0xeb5f2573, 0x0a9d03c6, + 0x26a6f34d, 0x8502a00a, 0x89293fed, 0x019100f2, 0x35da93a1, 0x30c34384, 0x1fa62d58, 0xfda17578, 0x72416c67, 0x03c230c1, 0xa623de44, 0x3a081a5f, 0x066b0a15, 0x8b23b681, 0x968c23cc, 0x8586b0cb, + 0x100f6c30, 0xd7e90549, 0x0095020c, 0x9c0b7bb0, 0x410a1248, 0x09e5ca57, 0xc06497d0, 0xa69100f4, 0x2034ed10, 0x78042474, 0xfdd01838, 0xeb124644, 0x76e5cb1b, 0xc16497d0, 0x814580f2, 0x86ed50ce, + 0x1861a102, 0x7a0419ac, 0x16489436, 0xe500ed1a, 0x21aefcb2, 0xabc40796, 0x1dbaa3c0, 0x0a9e080e, 0x38a6fd4d, 0x286cf402, 0x0af42c91, 0xc5580076, 0x03c3099a, 0x9a820be2, 0x276a24bd, 0xd6142a7c, + 0x489c020c, 0xa0953a12, 0x6c003b11, 0xd84d9578, 0x225fe203, 0x101e5da5, 0x6f40012a, 0x0c1f038a, 0x12e04171, 0x7232e5fd, 0xd7d03181, 0x01e4208c, 0x9d40b9f1, 0x804f155e, 0x37b06186, 0x86cf41c5, + 0x3552def2, 0xcbb514b6, 0x4031409f, 0xfa00bcb9, 0x3d04519e, 0x03ac72e0, 0x860b5768, 0xdec2854e, 0x476d0714, 0xf9f67776, 0xc440f972, 0xe5cbda35, 0x0579729e, 0x09233df4, 0x456c407a, 0x331a95f8, + 0x0a9d0347, 0x0e29bd85, 0xf5e49138, 0x7cc7a1a2, 0xc812fb1f, 0xb29afbf8, 0x7c2d0388, 0xf23d301e, 0xd7f6313f, 0xec0c112f, 0x833607c9, 0xb7802500, 0xbe45a0fc, 0x2c49aecc, 0x85f3cb95, 0x40e22cf6, + 0x0e03da0b, 0xc62bff16, 0x020bb401, 0xb0a153a0, 0x50129066, 0x48117a81, 0xb7a4647b, 0xb0a33c81, 0x89865caf, 0x9e6d0e55, 0xff178c07, 0xee01c62f, 0x1335b42a, 0xec2854e8, 0x3b683070, 0x66d46bb2, + 0xb3cde91c, 0x5680d12e, 0x44e142ff, 0x85a07136, 0xc5d301eb, 0x38c77fe2, 0x21337680, 0x5850a9d0, 0xfdd00833, 0x7b89e3bc, 0x853003b0, 0x008a02fe, 0xe503850b, 0xa3bd8672, 0x2e1c078e, 0xc637ff16, + 0x4b7b7401, 0x142a780c, 0x54020cd6, 0xda44dea0, 0xa6007618, 0x403b440a, 0x9f30a177, 0x61681c55, 0x62fec07b, 0xf27fe0f1, 0x12bc401d, 0x91ff1d30, 0x85c75d63, 0xb347dcff, 0xc342cfcc, 0x6e4b5830, + 0xb00934b7, 0x8c3d9244, 0x7686d32f, 0x42855d80, 0xa0716677, 0x01e2197d, 0xdee2c583, 0x85030398, 0x8b67823b, 0x411f71e3, 0x76116bc6, 0xd762c980, 0x431f8c47, 0xcd6142a7, 0x245ed020, 0x13050def, + 0x50c3d924, 0x3fed6881, 0x8e142850, 0x5a071764, 0x2b980f60, 0x8f4a8716, 0x8f17f6b9, 0x16745102, 0x0de378f7, 0x16546594, 0x97a7ebb8, 0x54e873f1, 0x17306c28, 0x1d547ee8, 0x4b010512, 0x8fb8154c, + 0x028500ec, 0x08907d7b, 0x012400f2, 0xb94f4b66, 0x12dc200e, 0xc3e6978e, 0xfac19f31, 0xeff0596d, 0x308ecfe1, 0x0a9d0e7e, 0x01cd9d85, 0x4bdea054, 0xd010508a, 0xd1e6dcce, 0xc26c687f, 0x1c65900e, + 0x4ff6d168, 0x2a1c0248, 0x0075cd7d, 0xf0e1e3e1, 0xcc70deb4, 0x870a7067, 0x203354f5, 0xe15d1fa3, 0x30c3439f, 0xf4083358, 0x15af286c, 0xeb7aa709, 0x89102a59, 0x7f6c05c3, 0x40f6071c, 0x6d00abbe, + 0x968c0248, 0x8075c6fe, 0xa76213c8, 0xff7cb647, 0xc7eab71f, 0x49520088, 0x6c255b8f, 0x787f9aac, 0x854e814f, 0x0714dec2, 0xc9d40094, 0xd6386330, 0x2bf2ad63, 0x07101848, 0x240e36c8, 0x063b6694, + 0x73bfa54b, 0x16b2201d, 0xf56c5938, 0x2bd27ec9, 0x49e0dfcd, 0xc8566715, 0x47a11ffe, 0x53f6ed0d, 0x1d00ffa1, 0x29bd8109, 0x1436780e, 0xda1d3475, 0x0c0a7bff, 0xc19b8815, 0xe0500ec8, 0x31648b14, + 0x5b3d9e56, 0xd73bfd2a, 0x00d2a201, 0x8fab3878, 0xb3d6fffa, 0x556b8871, 0x101f4692, 0xa0f40072, 0x96b2ac0f, 0x2854e814, 0xa80419ac, 0x3a012940, 0x42dea581, 0x968e0558, 0xfd8827cb, 0x71626d0b, + 0x8989db60, 0x35b76903, 0x94f4dab4, 0xe35e7f4a, 0x6cf7c039, 0xb5847d03, 0x5b30fd18, 0x3c9d7a7d, 0x51d47ea3, 0x26dd8d6c, 0xddd1c66c, 0x25860d2a, 0x142a7423, 0xa838a6f6, 0x7496a004, 0x291c0414, + 0x205587f4, 0x00e589b6, 0x10dc0e28, 0x384b4ef8, 0x9a32b4a1, 0x5fc0602a, 0x7a54a9a2, 0x888073c2, 0x16c94213, 0xc1ffe2dd, 0x776cb631, 0x70dd8141, 0x881c58ab, 0x435742d4, 0x18ac2469, 0xb0a153a0, + 0xcf01c537, 0x5072a286, 0x1635ff48, 0xd8150c0a, 0x42c31d88, 0x28dc0e2c, 0x244f1a13, 0xa0341caf, 0x4d0a17cb, 0x073c7ba5, 0xe681e760, 0x4ad0a3c8, 0x7f5904fd, 0xcf00f90b, 0x0428c3aa, 0x9cf64f40, + 0xa5a00380, 0x4254b3b6, 0xf0327f8a, 0x0012a01f, 0xd0183876, 0x70b1a8fd, 0x6cb0041c, 0xbcfd6f3c, 0xf181c760, 0x270e15eb, 0x601f6c17, 0xcc389696, 0xe143a290, 0x74132f08, 0x42079d94, 0xf49e1876, + 0xc47912d9, 0x07628e9e, 0xf8c8aacf, 0xd9f10333, 0x0774c422, 0x0d4a4f0e, 0x2062c55d, 0x091d0cbf, 0x00833581, 0x86550025, 0x7deeb31a, 0x709e0548, 0x38c10077, 0x0a12e710, 0x8da43d4d, 0x3fe131b8, + 0x1b52fd07, 0x1c17d0be, 0x34c33807, 0xccfa4e10, 0x0bda07a0, 0xd1238f04, 0x3ee49a56, 0xed38d3de, 0x8e97e238, 0x1d4fa011, 0x8386212c, 0x899a02aa, 0x060e1d85, 0x5a8a1b3d, 0x432c97a4, 0xda205487, + 0x0e394f95, 0xda071420, 0xa470a87f, 0xde71237f, 0xbe3809c0, 0xbb0038e3, 0x7a59041d, 0x8e9674a4, 0x76d0a6d2, 0xb1c5ed8d, 0x1e3b492e, 0x21768566, 0xc55cc290, 0xac1bd00e, 0x42a7401d, 0x1714dec5, + 0xcf02f3b5, 0xc2c7a286, 0x6f7cac27, 0x90f60550, 0x2846d95d, 0x9b87dc0e, 0x49330847, 0x012446c3, 0xfe6fc826, 0x978071c2, 0x3320fb2c, 0x1de3f6b4, 0x23fbbf7a, 0xf9ce38b4, 0x4904225d, 0x4f8f1e3b, + 0x99571217, 0xed2ff6c4, 0xe8466a09, 0x3348a854, 0xd4637cc8, 0xfdd0265c, 0x0f3c165a, 0x5eb4b0d8, 0x66920550, 0x07140730, 0xb2fb889e, 0xcbb06d38, 0xa4a9f8fa, 0x615e007b, 0xe5be6fc8, 0x28730038, + 0x7d678500, 0x9d3f52a5, 0x453925aa, 0x18844b9f, 0x0d591fc2, 0x6830baeb, 0x0e25e981, 0x581fda50, 0x4012a053, 0x6f0b2c64, 0xc8cc13a7, 0x4740a81a, 0x511cfbca, 0x61010567, 0x3ee1b815, 0x607151cf, + 0xfd0ae37f, 0xb4ddfadd, 0x898d1101, 0x157b00f6, 0xe33e142a, 0x80eb0038, 0x9ab459b8, 0x19d641ed, 0xd91a24f4, 0x112efddf, 0xac7910e4, 0x8763f8f7, 0xeeb64fa8, 0xcfa01cb1, 0x478060bf, 0x52a11842, + 0x24ff01f3, 0xca993d7e, 0x6fe860d9, 0xeb3804a8, 0x11c1b419, 0x68157efb, 0xb0b98335, 0xff09c0e2, 0xb502f8dc, 0x6c258c64, 0x8e737121, 0xc00e39ef, 0xa34f04c8, 0x7ed97975, 0x4f451d3c, 0x4b9f77f6, + 0xe12a4084, 0x6c47cc6c, 0x624fad86, 0x52b54b90, 0x2a74007b, 0xcb387594, 0xfe24c081, 0x97d3a928, 0x0bfac31b, 0x78d43678, 0xdf5ba34f, 0xfe6ab31b, 0x35681541, 0x846cb983, 0xb286c0e3, 0x9f70597f, + 0x480f7b62, 0x71ea0f90, 0x1ce7ce23, 0x2abc6007, 0xa067d908, 0xca675b01, 0x5b0cd3cb, 0xc82f368b, 0xa12ac16b, 0xcb896a54, 0xc0329317, 0xa500ed8c, 0x21f2be63, 0x75942a74, 0x6046db3e, 0xb8a9c61b, + 0xbbb1dc06, 0x4099bc10, 0x59453009, 0x01eed1db, 0x9941b815, 0x5e12b113, 0xa6044fd3, 0xd8a7dd46, 0xec124e5e, 0xb1e2de01, 0x708071c9, 0xc3ae6029, 0xa0b4e896, 0x366cb49b, 0x21f629cb, 0x915bf6b3, + 0x881c4c5d, 0xc77ec8fe, 0x525758fe, 0x54e845d9, 0x5688e328, 0xbd499bd2, 0xdc99a23c, 0x70fb23d2, 0x08405ec3, 0x1ce81500, 0xe2b7e6d5, 0x02a7da58, 0x973066ed, 0x8da0a970, 0x15bc7c64, 0x1bca81ab, + 0x89603d91, 0x3f12ecfa, 0x1e2d4431, 0x80768a7f, 0x06429bf0, 0x54bd18ec, 0xf626ebda, 0xc1bf9671, 0x460edb0a, 0x0a249598, 0x0446cbfc, 0xe152d683, 0x1593c35e, 0x68c00960, 0xe8113906, 0x1137c6ce, + 0x9c2692dd, 0x3ff0df62, 0x9456c04f, 0xdb46bc79, 0x1370ded6, 0x8f3ef4be, 0xfeca2377, 0x3139f085, 0xd02d5be8, 0x90ae62ad, 0xb7a35477, 0xc7b73407, 0xfc5e07ac, 0xb46bf850, 0x416a8403, 0x16761b78, + 0x48272ee8, 0xb43f6e3b, 0x3d6ba37b, 0x5bb92423, 0x14c56d2a, 0x398c700c, 0x003ae0d4, 0xbbb92e4e, 0xb42a740a, 0x44f6d0c8, 0xd6d151f2, 0x9f8e5384, 0x1e8042c7, 0x5e051dd0, 0x677bc91c, 0x010707da, + 0xdb65dcbb, 0xf9f7d15d, 0x43428077, 0xcbc40e30, 0xf09d3d53, 0xc6fff159, 0xb04b3504, 0x6f3aec14, 0x15a15a5e, 0x78b53f45, 0x00ed127c, 0x71e09ea1, 0x19b1f19c, 0xc773f670, 0x844b9eba, 0x34f99b40, + 0x5c74c4fe, 0x84891841, 0x78fecf2d, 0x0b7370c5, 0xaed0a9d0, 0x43d1fdb0, 0x373f42b6, 0x16434edd, 0x10b1c30a, 0x0084f258, 0xed768150, 0x6a7536f2, 0xf996a6e5, 0x2c2dcf66, 0x456b054e, 0x3a79784b, + 0xda7c2540, 0xdbe00969, 0xb3d84a03, 0xbffd9b1b, 0x5ffd413c, 0x31f87e3a, 0x478201da, 0xdfb92204, 0xce31c7b5, 0x1da99388, 0x48ed5670, 0x6f578789, 0x8c7f5cf2, 0xfd9e701e, 0x0f77bd71, 0x537400e5, + 0x942a7400, 0xbac40791, 0x074ed689, 0x578e791c, 0x780d1c54, 0x04a07925, 0x64902818, 0x76ed98dd, 0x152855ce, 0x398336e8, 0xcff46aec, 0x00059207, 0xe12706cb, 0x8713a253, 0x7cf2e378, 0x38395f79, + 0x3feed479, 0x6e5e8273, 0x80768d3e, 0xb7183540, 0xa7cf7801, 0x590f9cbb, 0x9d14af02, 0x9573efea, 0xf8414c5d, 0x7a5015a0, 0xd0014d50, 0xc1f0d0a9, 0x2f81e800, 0x5b518d37, 0x6e156fad, 0x02f30642, + 0x90f1e831, 0xe0be9ad4, 0x15287c95, 0x66d76138, 0xd9b7d03c, 0x0b22c547, 0xede1c2d6, 0xb0fe6301, 0x4603da91, 0x5be5fef1, 0x43b1c840, 0xa35fee1b, 0xc2858852, 0xf00ed1cf, 0x639e642a, 0x39d3f40d, + 0x56b53f69, 0x19d4b6ea, 0xec35f2d5, 0x62bf0e3a, 0x55c06384, 0x4d78039f, 0x50a9d001, 0x38100cdc, 0xf03c8194, 0x0a8ddedd, 0xef25f0ea, 0x818f8e9c, 0x9fb3ef17, 0xe8b7fc50, 0x8155dbad, 0xf5fe8dcd, + 0x5a1872dc, 0xbf5bb405, 0x4ffd25e6, 0x61c58238, 0x563cfede, 0xdabbf380, 0x09e2bee9, 0x7423a9ff, 0x97f0b10a, 0xb7f00768, 0x901ce003, 0x0d4bd0a7, 0xfc0ec5ad, 0x8f0ae334, 0xba6993f5, 0xf5a1737a, + 0x8facfb98, 0x3f71d2bb, 0xc17c20b7, 0xbc3c160e, 0x54e800a6, 0xf4007228, 0x1ed06c18, 0xedde0560, 0x10b8206e, 0x5e6de1d0, 0x40aa05da, 0x37050b97, 0x0105e3b5, 0xa51c0a94, 0xd419e24d, 0xecc7f0db, + 0xfa4bdf05, 0xd0877fd3, 0x862e1f64, 0x585dfbf7, 0x8cbfb85f, 0x4a1ec803, 0xf44fa015, 0xff80388b, 0x3a61b084, 0x34912f44, 0xc7eb21cd, 0xa0976dc7, 0xa8b8bd32, 0x69b7e8ef, 0x17e3e1d2, 0xddc64c0e, + 0x997ae53f, 0x9d0014db, 0xe24e2d0a, 0x992e8307, 0xbc0f58f5, 0xc811a037, 0x2dcbd0c4, 0x64b50934, 0x897828af, 0xab9ed5a6, 0x56a85702, 0x413c1700, 0x1bb53ce8, 0xa49c07ce, 0x733fb37f, 0x1721598c, + 0x37472bc0, 0x795c5ed9, 0x3b019d8b, 0xdfaff39b, 0xbe1d5747, 0xd2007414, 0xe71b4006, 0x7be5b5a0, 0xc0bd378f, 0x3f6ecd13, 0xfd7187ec, 0xc0976638, 0xe28e7af4, 0x400536e2, 0x838b42a7, 0x12a7e824, + 0x252c2769, 0xb42ce3dc, 0xa01d810d, 0xe86e4940, 0x6d92ca40, 0x1d5403cf, 0xc0aae316, 0x5ce3e0c6, 0xeec65db0, 0xbf75180a, 0xa5fd98dc, 0xfd492d28, 0x5d6215b5, 0xb0a92722, 0xdd1f6c90, 0x1fdebfee, + 0x423a7071, 0xe9300e7c, 0x26369199, 0xd5aef852, 0x72112ea8, 0x4dadc6c7, 0x60f7bf60, 0x81864bfb, 0x2701d009, 0x9a70a49e, 0xa153a002, 0x932a7168, 0x55d3cfa0, 0x85fb568d, 0x82179e2b, 0x6e5bcaa2, + 0x2717a097, 0x3e49600e, 0x151ce19f, 0xea4bef38, 0x38ad34b9, 0xbb9f7e62, 0x6a5f3842, 0x3466bc29, 0xc661c0e1, 0xdcc76589, 0x5ade6fc8, 0x7050dc2c, 0xc577cad4, 0xb49d003b, 0x6800dab1, 0xed1f2dac, + 0x4fcec38e, 0x178b16ce, 0x760f4fdc, 0x1e009b39, 0x313fb542, 0x0d164be3, 0x5ca83b17, 0xe60addbf, 0x800a69c7, 0x9022854e, 0x3dda9b7d, 0xd54a3fcd, 0x45ec7ae4, 0x47703eb6, 0x19b6e5ed, 0xce28839a, + 0x8bb6bfe1, 0x043a3e48, 0x09e05577, 0x4d800ed6, 0x6341be8a, 0x47426583, 0x6a5fda23, 0xe2ebf85f, 0x46b1b23e, 0xeec77f67, 0x2fece6d0, 0x7a0ff6fd, 0x71c3bec2, 0x63c3ba00, 0xf801b49c, 0xfcb6436c, + 0xdee7ea11, 0xfa8b8df3, 0x3f081478, 0x5aa4cbe1, 0x74e9ef6c, 0xf39e20e7, 0x47491aa1, 0x8d3334dc, 0x9d0014dd, 0x459c450a, 0x212b10b7, 0x709bb43b, 0x081d82ee, 0x54f784f6, 0x00b2b727, 0x8f1441cd, + 0x10d6ae3b, 0x2ad9dfc8, 0x03b99db0, 0xded01ed8, 0x281dad86, 0x3922fafc, 0xf6221e87, 0x2f9fec57, 0xc24eded9, 0x1983fc5d, 0xa0dc143b, 0x0e78cf5a, 0xe6549ff0, 0x036eca7e, 0x7e5b21a0, 0xc3e49d8b, + 0xab1e79b8, 0x6d603fa8, 0x2f57e802, 0x1543bfb8, 0x64499784, 0x43026def, 0x609018c1, 0x37400537, 0xb76cc461, 0x65adc375, 0x4e2bbdd0, 0xc7ebe820, 0x513f6e71, 0x608dcdce, 0xfe49d201, 0xf3f3e91c, + 0x054b7ddf, 0xc0bad0f6, 0x1be411cf, 0x8a5efd1b, 0xf6fc29d2, 0x268d36b0, 0x1676db92, 0xb37b6e86, 0x7c567058, 0x598e00bc, 0x9df291f2, 0xe00e786f, 0x021c7668, 0x436d06f6, 0x3a56fcb6, 0xd798e777, + 0x5bf7ea2a, 0x1d00c09b, 0xe3357a3d, 0x56e1f469, 0xb6e47eaa, 0x9ab1f982, 0xa153a002, 0x3f722d98, 0x45596d1b, 0x40acfd65, 0xa4b19db0, 0xaf997f62, 0x84ce025b, 0x0f3a0e21, 0xff9d52e8, 0x2054b5c9, + 0xba1cf9cb, 0x79695189, 0x0380f642, 0x42c15fcc, 0x41ffc177, 0x27e9ba3a, 0x4fea3d0e, 0xc03b463a, 0xacf95e1a, 0xfc521edc, 0x4e81412d, 0xb3d100ba, 0x89fbcc7e, 0xbb69de6d, 0x026d6fa8, 0x10a64ead, + 0x16cdd073, 0x6bbdb5a2, 0x70ec58ae, 0x86498416, 0x5348618b, 0x142a7400, 0x63853d63, 0xc54c3876, 0x9c10247c, 0xdd56240f, 0xd6d1363b, 0xd1020d08, 0x1afe48c2, 0xf06af36e, 0x0a96ec3f, 0x010aaff4, + 0x5003881a, 0x03d9043b, 0xab318256, 0x26f6d9f3, 0x5c2783fa, 0xa1204809, 0x70704060, 0x191fa01c, 0xfac715a5, 0x0d001b5e, 0xd02ff2de, 0x096e3beb, 0xf376b4e8, 0x8c438efd, 0x7def1d63, 0x13a382e2, + 0xc65baab1, 0x2365b01f, 0x72160ad5, 0x48f011b3, 0xf918e608, 0x904d36e7, 0x5a87a958, 0x78204906, 0xe144f982, 0x1944f535, 0x3548012c, 0x0b1240b0, 0x0de88133, 0x8606c0ab, 0x5dc0a0fc, 0x90eff40b, + 0x287b6fc2, 0x6a584873, 0xcac2dca7, 0xff50ad0c, 0x6a119c32, 0xb45bb80b, 0xf528ec03, 0x3437e923, 0xe1f15647, 0xead069b0, 0x7dba443c, 0x0096ec5d, 0x378fa8b7, 0x705f2181, 0x1c0d34e7, 0xc585256a, + 0x4fe2b765, 0x21f982b5, 0xa9d0014d, 0xdd08ca50, 0xdb422ca4, 0xec82049b, 0xe2449c40, 0xc4020965, 0xf3e2314a, 0x868e0341, 0xaf82e055, 0xbe661fb0, 0x205beedc, 0x318cefd0, 0x3820ac17, 0xfdf6682a, + 0x2c4856cb, 0x4e3773ec, 0x8d2a5014, 0x383f3ba2, 0xd001b74d, 0xee38ffa0, 0x12257885, 0x8afa4de5, 0xfaf1d6fa, 0x5627ded3, 0x0b1eb8ad, 0xc0fc0180, 0x825bda4f, 0x0fcc15af, 0x227408fa, 0xfad562c0, + 0x9bc01f07, 0x14fd2340, 0x5b03904c, 0x59388158, 0x0012e17f, 0xc86184b7, 0xa5fe60e1, 0x50c0ab1b, 0xe048320a, 0x7e231f59, 0x6cfec634, 0x1c6fdbe0, 0x47fd05f0, 0xadf0a288, 0xf8071c41, 0xd1f60a33, + 0x1e3e3c33, 0x859274f1, 0x09fe8879, 0xa2e9da89, 0xa8df093e, 0x1deedda5, 0x4439bf46, 0x3b61c184, 0xe40d9c12, 0x88a62999, 0xa0e0499b, 0x4837377d, 0x9bbfa7e7, 0x88111caa, 0x56788523, 0x6c5002c2, + 0x37610f25, 0x69ef90b9, 0x7cdab7ee, 0x8f1d1142, 0x534e5623, 0x843893e0, 0x81d77bf0, 0x1df2afd0, 0x3a4cb4a0, 0xdb98bab1, 0xf8cec933, 0x1f5c7762, 0x877acb40, 0x09bc71fb, 0xba4b2cf8, 0xc7657046, + 0x74f624da, 0xd7d1ba59, 0x3807e60a, 0xab614b95, 0xbda0b6fc, 0x8b3283bb, 0x5a046162, 0xe520b687, 0x42e45da0, 0x5bd7633e, 0x63013a75, 0xfc844c52, 0x6e620228, 0x50e1779f, 0xe54fffd0, 0xe33fc038, + 0xf1564ffb, 0x8e69a62d, 0x1613fe87, 0x796ec3f9, 0x877afc90, 0x1723dc5f, 0xf0ebee59, 0x112cc174, 0x30568105, 0x0004523f, 0x6a31d7a6, 0x5097ecb9, 0x609ca6f8, 0x0926c1de, 0xa0c24829, 0x7053f673, + 0xed0c6eef, 0x25e0f10a, 0xd16451ab, 0x5f856b20, 0x1507cedc, 0xf418a727, 0x6ffee42f, 0x5ccf6fa1, 0x57a44a00, 0xe01d17de, 0x376040fa, 0xb3e43d5f, 0xa0eeae2c, 0xf3bf7535, 0x091251c3, 0x18f944b6, + 0x75889c06, 0x1a1fb2e8, 0xb6f814fc, 0x1da8a5c1, 0x004247a0, 0x6fbc4360, 0x5b01b821, 0x3190b34f, 0x67bcb029, 0x1310964a, 0x44eda5e1, 0x38dddfbb, 0x01cb60e5, 0x117d734a, 0xe7af0cf2, 0x5c2e16be, + 0x150c181f, 0x92cc7df5, 0x9790cff4, 0x6091c019, 0x8fcc15b0, 0xc5902c8c, 0x8daec761, 0x4fa4be8d, 0x5419994f, 0x59ca7535, 0x78ad09fe, 0x3d0f9de3, 0x889f2c90, 0xd7744567, 0x4049f068, 0xf8071c26, + 0x1d218e77, 0x7ef8b5e3, 0x2d55bd60, 0x58c7376a, 0x5c5495dd, 0x50c12380, 0x068f982b, 0xa2706679, 0x47fe5dad, 0x0c092d0e, 0x2740cc1b, 0x1e4cfc11, 0x60dc6921, 0xf87f57e7, 0x6d760247, 0x8880d214, + 0xba56319e, 0x12d38680, 0x0e716ff6, 0x09754230, 0x1ebd605b, 0xec979bbd, 0x5657d9c7, 0x026b249a, 0xfbe00ca3, 0x6c1872a3, 0xfa84df66, 0x05617086, 0x621c8c3a, 0x36a211ff, 0x29f22b02, 0x0313eaf6, + 0x85a70be3, 0xeb1a14d3, 0x5d5dd300, 0xbbe1c301, 0xb0fec7d6, 0xc30dd605, 0x47210f3e, 0x8d76f0af, 0x1091d03d, 0xbe70c98a, 0x88c3499f, 0x0c3a8bc4, 0x6e8f429c, 0x489e4568, 0x854803be, 0xaffe16d3, + 0x96800e71, 0xc7ffc0e7, 0xe67e9349, 0x8aba1d27, 0xe00ffe83, 0x0ad83048, 0x0cde03e6, 0x0ece3142, 0x1d8cfdf7, 0xf8fcc0cc, 0xc605a3e2, 0x59ed2b83, 0x3067074e, 0x0ef1efe4, 0x2b6f6410, 0xacc7376f, + 0xe525056e, 0x2c15a32c, 0xd225dff0, 0xe0b319d1, 0xddc9f7d6, 0x0acc7dfb, 0xc23940ae, 0x8ffb0d19, 0x9a500ee4, 0xf3968dee, 0xd38ff411, 0xe03084f5, 0x8300d07e, 0xaba2d19d, 0x9b206fa7, 0xf5a1df87, + 0x3f98c26c, 0xa1c33873, 0x8070a5bf, 0x7d703434, 0x58c48f66, 0xa47261cc, 0xafc7a973, 0xaf7576c6, 0xcb4b0e76, 0xd818f139, 0xeabc51ea, 0xc3c6f0d3, 0x039396ff, 0x0f9f01f0, 0xe4d7491a, 0xd991c1c0, + 0x91d9a9a9, 0x0cc981e1, 0xa19a2cdc, 0xcb7db3fd, 0xd3ab33ab, 0xde0397ea, 0xa83f1101, 0x56413db6, 0xd44b5632, 0x11de024b, 0xb6ab9f31, 0x3f42413d, 0xa88cb6a8, 0x90ff3f36, 0x6daaa7a8, 0x0798918f, + 0x55848fd4, 0x1de02cc0, 0xb54f7713, 0xf3e031f1, 0x30663541, 0x3a500a79, 0x9f2283bc, 0x463e36a8, 0xae8e3f46, 0x27dbc6f4, 0xc5477873, 0x3e36a9ae, 0xa83f4646, 0x018fa8ae, 0x3b4587f8, 0x80a560f5, + 0x65f43287, 0x31f1b558, 0x40b06a9c, 0xe47b383d, 0xc7e98435, 0xb279bc4c, 0x140c5387, 0x8d6506aa, 0x2d60869c, 0x0240d219, 0xe9416b72, 0xa992018a, 0xa5b00962, 0x7598f6da, 0x8b40294e, 0x9e8cb05c, + 0xa9294d6c, 0x97863e36, 0xe3b1de09, 0x2fe6d56b, 0xa0d35f01, 0xdb55fb33, 0xd715f09e, 0x02a7d401, 0xac0258ab, 0x813db6aa, 0xb1dc0897, 0xfb6abee3, 0xe637a497, 0x16974369, 0x2df0b012, 0xfed601d7, + 0x7201daac, 0xa12bd029, 0x2de9b553, 0xf52079b8, 0x6be02099, 0xb046741a, 0xb86b3bad, 0x89f52079, 0xe111f000, 0xdaa032da, 0x9e9c36f2, 0x44ea8c60, 0x0e6cfc00, 0x6d56995d, 0x1a7e0b79, 0x7f8ec760, + 0x0c807628, 0xd2d573fb, 0xce459eca, 0xf6f83f9d, 0x0413dd57, 0x5f59a33d, 0xbcb6aace, 0x071887cd, 0xd212dc50, 0x994edaab, 0x929e3221, 0x5028fc07, 0x6d281c79, 0x8e375c30, 0x1e17d2e9, 0x2eaa0513, + 0x04ad5708, 0x786d5658, 0xb068ce07, 0x30e98a45, 0x0a9cc879, 0x910e8ad5, 0x81725de3, 0xa1661733, 0x75281eb4, 0xa627542a, 0xe1dddb54, 0xd5fd9c5d, 0xa7720186, 0xf2223542, 0xe1bcd85c, 0x2858050d, + 0x9ec9072d, 0x4985d50a, 0x6c60245d, 0x80c28010, 0xff67567f, 0xb542a62c, 0xc8091b40, 0x21cc213b, 0xec9094f6, 0x542a6ec8, 0x0207ea31, 0x012a621d, 0xc446bc98, 0x54eb5419, 0xb36a88e8, 0x54c66cbb, + 0xbb0a3002, 0x0a9bf4c2, 0x91955aed, 0x0c1ee099, 0x7efc2b9d, 0xc9750b30, 0x42a6fd60, 0x120bba07, 0x1875549a, 0xcde1b408, 0x4ba83a05, 0x15376b06, 0x372d87da, 0x1d4b760a, 0x626d0206, 0x6518c113, + 0xa9b3502a, 0xe57b71d0, 0x27a6819e, 0x542a8e23, 0x54068081, 0x92f502a8, 0x02dd8700, 0x0d6c0cfe, 0xe2d2a838, 0xa034447e, 0x4ea05cc2, 0xfd2da153, 0x051b7dd4, 0x062d337d, 0xf3e22d02, 0x87551880, + 0x15346804, 0xafc87ada, 0x67001adb, 0x1031ead7, 0x1a3bc168, 0x67514280, 0x21233eb0, 0x9b894367, 0x93dbf4fa, 0x36810316, 0x0ec11d31, 0xbac22a8a, 0xb30e854c, 0x0e9a779a, 0x8973fda7, 0x8ecf060f, + 0xb05ad8f5, 0xe2c3a285, 0x6d0a98f4, 0xccdaca89, 0x7a7efba4, 0xf05a040c, 0x51a3880e, 0x62d20a93, 0x2665b42a, 0x9f576eba, 0x8f4d5faa, 0x910b4081, 0x4d461034, 0x09035041, 0x5f97b030, 0xae499db6, + 0x08193505, 0x052810b4, 0x0111285a, 0x7850a9c0, 0x7f48d933, 0xa9bed56c, 0x404a6937, 0x0b408193, 0x68a00941, 0x1538043a, 0x26eb0bda, 0x1313ed6a, 0xc95b756b, 0x8193405c, 0x4cdc4240, 0x110a8788, + 0x1d0a9d00, 0xfe97d5dd, 0x8aaff797, 0x5a9cedcf, 0xe811b04e, 0x10481032, 0x04480095, 0x4004c828, 0xa14f42a7, 0x22dd3a2d, 0x309bde32, 0x7fd5db52, 0xa8353456, 0xcd140493, 0x88146002, 0x2854e000, + 0xa9f85379, 0x78fac1e4, 0x9bfca3ba, 0xd6453b46, 0x02065d01, 0x0059a209, 0x10a6828c, 0xfd85e360, 0x4486c082, 0xc703454d, 0xe8722f21, 0xe71b3d5e, 0xffe38704, 0x15887e4a, 0x2ff22c02, 0x42a74007, + 0x7bb6b5dd, 0x33612c4b, 0x42467879, 0xe8fd5ea7, 0x744b54f2, 0x40cba200, 0x10b04120, 0x80fb9160, 0x942a7418, 0xe69bc6da, 0xa062f93b, 0x5eef60eb, 0x4d9a6d99, 0x9d73a3dd, 0xe1c2c723, 0x81033681, + 0xa109ee04, 0x0f9f7241, 0x76854e02, 0xdb756e05, 0x2300e76a, 0xee1c1b5e, 0xce6d1e5c, 0xda472cca, 0x9757467a, 0x46a73ab7, 0x265a0786, 0x35070707, 0xe2c21339, 0x802f509d, 0x1dbce3c3, 0x58012800, + 0xeabb174d, 0x2ead8cf7, 0x76756ef7, 0x9a1ef56e, 0xa9ed199b, 0x972b29d7, 0xb1253f3b, 0xe0d9ee7f, 0xef22607e, 0xabdbea90, 0x6079c23f, 0x41039c8b, 0x36a854e8, 0x92da77b3, 0xe4c40824, 0x9d0617dc, + 0xde29d50a, 0x412493d6, 0xbe5ce620, 0x60c7e1cd, 0xfaa63ff6, 0x37cfeaf6, 0xdf201d09, 0x07a10303, 0x73556843, 0x5bee13d3, 0x9ae45802, 0x854e840c, 0xd772956a, 0x30999112, 0x01eeb93f, 0x8d50a9d0, + 0x2534eefa, 0x22d01a51, 0x38003dcf, 0x034c1212, 0xde244219, 0x0d6d7201, 0x8e854e85, 0xdabdbea8, 0x4a0eda23, 0x3dbf24f3, 0xaa153a00, 0xba1c01cf, 0xc8076292, 0x4e800f73, 0xdfc7f685, 0xc5271a81, + 0x1c67900e, 0x350a9d00, 0x9218a069, 0xf22c8166, 0x5380038b, 0xefaa2da1, 0xce24f6af, 0x55e45902, 0x854e800f, 0x0c50349a, 0x0e00eb49, 0x5203d579, 0x68d9dd8f, 0x27ad7f34, 0x47c44771, 0x184d5791, + 0xd26a153a, 0xac243140, 0x74e47b04, 0x2a700856, 0x6281a4d4, 0x84173b10, 0x22033e45, 0x11d0a9d0, 0x775837d5, 0x424207c4, 0x145e72e4, 0xa4d42a74, 0xd4486281, 0x9727ea0d, 0x5df43074, 0xc40d26a1, + 0xd81a4d27, 0x00549721, 0x26a15df4, 0x6527c40d, 0x8722c012, 0xa74208d7, 0x881a4d42, 0x24ca66cf, 0xed0e4580, 0x50a9d0e1, 0x93e20693, 0x443c1dac, 0xfa1032f6, 0x069350ae, 0x809003e2, 0x422bec98, + 0x08f82380, 0xb0efaadd, 0x05ef24de, 0xeb2e73f3, 0x2a74206c, 0x81a4c874, 0x0f1d08b8, 0x6ceb23cd, 0x742a7420, 0xd61df556, 0x8113649b, 0x897eb22c, 0xa15df414, 0x25c40d26, 0xb15125fe, 0x86145c64, + 0xa4c391be, 0xa7ac7881, 0xf3f2056a, 0x43fb5334, 0x1a4d42a7, 0xb85bcb88, 0x048e33fd, 0x5aa153a1, 0x1fa5c31d, 0x22d018c0, 0xe810bed3, 0xea9f6854, 0xe935ac5b, 0xac83017a, 0x0c5e7965, 0xa4d42a70, + 0x3824b881, 0x86478e18, 0xebf04135, 0x881a4dc2, 0x0c707bc3, 0xbb2e7168, 0x81972373, 0x69341ff6, 0x5ba90e20, 0x776482c1, 0x2a743076, 0x3881a4d4, 0x016921fc, 0x0c1f32c8, 0x6931091d, 0x46a00e20, + 0x9d593f00, 0x577e071a, 0x312756a8, 0x6973c66a, 0x4b76400b, 0xa15df800, 0x5bf07d35, 0x8017120c, 0xe80096cc, 0x0349a854, 0x01732345, 0x80096cc8, 0x0693004a, 0x02ea468a, 0x0012d590, 0x49a8577d, + 0x77224503, 0x0968c801, 0xd42bbf00, 0x012281a4, 0xc9048237, 0x9d001722, 0x7d541d0a, 0x5e45f597, 0x8b21c00e, 0x2a742074, 0x2281a4d4, 0xf4049691, 0x0022a0c8, 0x9ce8577d, 0x2dacdbea, 0x3400aab2, + 0xe0828bf2, 0x349a8577, 0xca921450, 0xf7a47e80, 0x42a74005, 0x281a4c3b, 0x007d3902, 0xb764d641, 0x42a74106, 0xb32f7a75, 0x0469485e, 0x4e7b230a, 0xba153a0c, 0xeb3efaa5, 0x2c1a700a, 0xc20db91a, + 0x9350a9d0, 0x53608a06, 0x9b302b42, 0xc5a752fb, 0xc5a153a1, 0x5a77d566, 0x02cc2453, 0x21352144, 0x350a9d0c, 0xb2362069, 0x5234c093, 0xa9d041b3, 0x77d529d0, 0xc832535a, 0x0c487901, 0xa153a105, + 0xe6c40d26, 0x00170cb3, 0x3bca0a14, 0x153a1a41, 0x4c40d26a, 0xd2cc7fd6, 0x8e4f32fe, 0x9a854e83, 0xc0131034, 0x48627099, 0x4e87171b, 0x10349a85, 0x13a0778b, 0xa5904481, 0xa153c103, 0x22c40d26, + 0x21680553, 0xa7420749, 0xaa638b42, 0x648eb5ef, 0xe46d00ab, 0x54e840e8, 0x0be65168, 0xec85abe0, 0xf7348072, 0x418774a5, 0x55c742a7, 0x491d6cdf, 0xe4a06af5, 0x010d68c8, 0x349a854f, 0xda2c9850, + 0xa8c85840, 0x42a7430b, 0xcc281a4d, 0xe055467a, 0x45e0f730, 0xa854e821, 0xc9850349, 0x041408b0, 0x1d0c22de, 0xa0693109, 0x006d7910, 0x0026f018, 0x69350a9e, 0x6df920a0, 0x67681800, 0xb42a7800, + 0xd6fdf557, 0x00371c8e, 0x8012300c, 0xf716854e, 0xbadfbeaa, 0x1b8e55f1, 0x16580600, 0x93004a84, 0x99b00a06, 0xcc301171, 0x083a5013, 0xd26a153a, 0xcf240140, 0xa9008b02, 0x42a78009, 0xe1bea82f, + 0x0d4b919a, 0x75e00820, 0xd0a9d032, 0x3beab4e2, 0x72ef17ae, 0x390401a9, 0x0c567e97, 0x6c5a153a, 0xf5cb7d50, 0xb0499322, 0x0427d278, 0x26a153a1, 0xf323e40d, 0xd228a02e, 0xa9e082c6, 0xb7d561d0, + 0x8ef22b5c, 0x52d63d00, 0xa7410a8e, 0x881a4d42, 0xd008ff26, 0x187f6523, 0x38b42a74, 0xeba6faaa, 0x605d0e44, 0x254e3cd1, 0x805b8603, 0xe9bea9d4, 0x004ca11a, 0xad7aa458, 0xe239b35c, 0x04ea1a59, + 0xaa5ac580, 0x519b406c, 0x935092f1, 0x9749a001, 0x201e300d, 0x19457587, 0x3d949a00, 0xdf54a7a2, 0xa720aabb, 0x293e502e, 0xea662b5f, 0x2ade7079, 0x29360016, 0x0a4641f3, 0x002d5142, 0x4109d21d, + 0xa8a00423, 0x690e8017, 0xccdb045b, 0xac011f5b, 0xeedaacff, 0x16d24f1e, 0x4c148d06, 0x600cd28f, 0xfb697b90, 0x108dea83, 0x4dce4948, 0x7ea471a0, 0x8291b0c3, 0x0024d0e9, 0x139548f0, 0x982918c6, + 0x603d2506, 0x211fd27c, 0x6260a467, 0x20fcc8fe, 0x0da4e490, 0xb8a61106, 0x027d1097, 0xa8c01a40, 0x89829180, 0x0a75ccd8, 0xe9739c4c, 0x8dbb0627, 0x43a38807, 0xea4d8805, 0x82918074, 0x012ad049, + 0xe03ee7e0, 0x05233108, 0x04305105, 0xfce40e24, 0x85052320, 0x1c02a2d0, 0x6094ea4d, 0x8484e4c9, 0x18012cd0, 0x03c031e6, 0xe58c148c, 0x4012fd17, 0x0a35d241, 0x1829185b, 0x1fd80543, 0x0a4601e6, + 0x12a9f972, 0xc763bfec, 0x18078853, 0x854d1829, 0x01e81fd8, 0x51460a46, 0xee07f631, 0x0846dd02, 0xfb20a930, 0x230162c3, 0x84549905, 0x7900f3fe, 0x61829180, 0xb0fecc2a, 0x1a19908c, 0xa56df108, + 0x17561fd9, 0x80305230, 0x365ff9e6, 0x9bef41f7, 0x63c04237, 0xb6d50bfd, 0xd013c74c, 0x03c6c289, 0xae35d61c, 0x0739f2ff, 0x0cd44e3b, 0x3536100d, 0x0a49001a, 0x63104fa0, 0xb9e220a4, 0xe08023c2, + 0x375ec1ac, 0xe160a46d, 0x9ffd96bd, 0x2f3d4809, 0x2179a840, 0xfd8699f0, 0x0a7909df, 0xf8d4084a, 0xa54568fc, 0x07b8a37f, 0x2d68d77b, 0xaaefc14b, 0x70d0fec1, 0x068776b0, 0xfc362b36, 0x160e720b, + 0x14a8d40c, 0xf8e9ae7c, 0x60d61a47, 0x49a9986f, 0xf2055829, 0x0526068f, 0x583cb06b, 0x068ff215, 0xb5a81956, 0xad75b14e, 0x01d23fa7, 0x25350214, 0x70579f05, 0xb59a37f6, 0x1b2f41af, 0x582921ac, + 0xc7bfdb65, 0xa0465063, 0x0560a466, 0xf2887f36, 0xac1e5604, 0xa9b82909, 0x0cb7f0ed, 0x0d60f180, 0x2acfc148, 0x0713f8bd, 0x76a81b6c, 0xc55eb829, 0x2c03bfcb, 0xf9eea81a, 0xff54ead1, 0x873ed641, + 0x2cd50344, 0xab543f05, 0x1f720ffa, 0x55ab05a4, 0x68566e0a, 0xd61a67eb, 0xa3f3307e, 0xfd5eaa41, 0xc907b9f9, 0x4e61570c, 0x4a4ab368, 0xfdace2ff, 0x5408b003, 0x67e8fcdd, 0x99f9dd15, 0x3e807b92, + 0x8292ea91, 0x5bf8f7b5, 0x15409480, 0xdb1ac149, 0xe12069fd, 0xc148b540, 0xf36a2fcd, 0xfb58b7f1, 0x48a37d3c, 0xb7c14875, 0x47fb12b5, 0xd4b7e4f1, 0x4205501e, 0xaeaa8fe0, 0x38f208fc, 0x74ac18b5, + 0xf155a80f, 0x0fb00417, 0xc0d0f454, 0x7f15dadb, 0x1ea49e41, 0xf9dcac21, 0x5135f7d1, 0x5ee611f8, 0x42a078da, 0x6fbbe0a5, 0xe01feacc, 0x70e4ca69, 0xcd810a25, 0xfb5d6b6f, 0x052b48b3, 0xd47e6f2a, + 0x9c11fe6c, 0x5e16a1fb, 0x8aa1ff45, 0xf3a4be46, 0xa8895d24, 0x67e0a482, 0x79f1fb46, 0x2e34b7f2, 0xf0522950, 0xf68c5ffd, 0x1a7ee58b, 0x2908a817, 0xbf3195f8, 0x405ca918, 0xcc68297b, 0x1b34809f, + 0xd052de81, 0x4f213f98, 0x1a044792, 0x307c0d0f, 0xe7392fd2, 0x433a4f3e, 0x5c14a9a2, 0x1b16a9ff, 0xf6b064be, 0x58826134, 0xeef82947, 0x01f6935b, 0x64b7ef70, 0x29374062, 0xfba6bef8, 0xe4f0a47d, + 0x813b44eb, 0xeb629d4e, 0xe6367f6f, 0xd3dac097, 0x00f027bf, 0xf3e0a46d, 0x2fdaccef, 0x883ffb8d, 0xcc5a032f, 0x307fae8f, 0xd65c9f61, 0x01ab0b1e, 0xfdf052f5, 0x9bdca637, 0x173ee701, 0x4a940a0d, + 0x98bf77c1, 0xdc665f72, 0x6dac5aff, 0xdc149944, 0xfd2cc9fc, 0x07ef7319, 0x92501f11, 0xfb7cda13, 0x0e7eb1a5, 0x40f889ac, 0x58d052ee, 0xc7ee573f, 0xa55c81ea, 0xcc5ff3e0, 0xe73b9fca, 0xafc6fe9e, + 0x0524e517, 0xeefabfd7, 0x357d6353, 0xfafbddac, 0x61c1395f, 0xf9f0520e, 0x889a5f4f, 0x1ee72d4f, 0xb67223ff, 0xf8295b2c, 0xa637c3ee, 0xe707a7cc, 0x96c0ff1e, 0x251d9904, 0x37edfcf2, 0x5f759a3f, + 0xfe3dce2b, 0xf9c23ffc, 0xe0a40ce2, 0x7cbf57fa, 0x9afcb269, 0xd7e9e4f2, 0xd9902f41, 0xd5feb629, 0xc118df4f, 0x1e4f3da7, 0x7fb7fe7f, 0x9ca1b4f3, 0x1fe781a1, 0xd8c2fdbe, 0xbb5882be, 0xefe0dc8f, + 0x829702ff, 0xe9b1ef9b, 0x50afd48d, 0xf83f21ce, 0x9eafb7e1, 0xfc3627af, 0x7e1ff7bb, 0x9fcbeef9, 0x7d4f313f, 0xbfa7f055, 0x2f97fa7e, 0x97f6f9bf, 0xf57d9fb7, 0xb64dbbc7, 0xfbbe6c3b, 0x1f67fde3, + 0xbbf6fa7e, 0xa9f2fe2f, 0xf3757bb1, 0xf27ebfa7, 0x7e9f8fc9, 0x08cfe9fa, 0xfe3f3ff4, 0x8d5fc9f8, 0x83060dc5, 0x183060c1, 0xc183060c, 0x0c183060, 0x60c18306, 0xff1f5430, 0x3cd2fa0f, 0x05b0d517, + 0x000000b2, 0x4e454900, 0x6042ae44, 0x00000082, +}; + +const uint32_t uprof_512_len = 12813; + +#endif //MICROPROFILE_EMBED_HTML diff --git a/Windows_Libs/Dev/Render/microprofile/stb/stb_sprintf.h b/Windows_Libs/Dev/Render/microprofile/stb/stb_sprintf.h new file mode 100644 index 0000000..d8011fa --- /dev/null +++ b/Windows_Libs/Dev/Render/microprofile/stb/stb_sprintf.h @@ -0,0 +1,1906 @@ +// stb_sprintf - v1.10 - public domain snprintf() implementation +// originally by Jeff Roberts / RAD Game Tools, 2015/10/20 +// http://github.com/nothings/stb +// +// allowed types: sc uidBboXx p AaGgEef n +// lengths : hh h ll j z t I64 I32 I +// +// Contributors: +// Fabian "ryg" Giesen (reformatting) +// github:aganm (attribute format) +// +// Contributors (bugfixes): +// github:d26435 +// github:trex78 +// github:account-login +// Jari Komppa (SI suffixes) +// Rohit Nirmal +// Marcin Wojdyr +// Leonard Ritter +// Stefano Zanotti +// Adam Allison +// Arvid Gerstmann +// Markus Kolb +// +// LICENSE: +// +// See end of file for license information. + +#ifndef STB_SPRINTF_H_INCLUDE +#define STB_SPRINTF_H_INCLUDE + +/* +Single file sprintf replacement. + +Originally written by Jeff Roberts at RAD Game Tools - 2015/10/20. +Hereby placed in public domain. + +This is a full sprintf replacement that supports everything that +the C runtime sprintfs support, including float/double, 64-bit integers, +hex floats, field parameters (%*.*d stuff), length reads backs, etc. + +Why would you need this if sprintf already exists? Well, first off, +it's *much* faster (see below). It's also much smaller than the CRT +versions code-space-wise. We've also added some simple improvements +that are super handy (commas in thousands, callbacks at buffer full, +for example). Finally, the format strings for MSVC and GCC differ +for 64-bit integers (among other small things), so this lets you use +the same format strings in cross platform code. + +It uses the standard single file trick of being both the header file +and the source itself. If you just include it normally, you just get +the header file function definitions. To get the code, you include +it from a C or C++ file and define STB_SPRINTF_IMPLEMENTATION first. + +It only uses va_args macros from the C runtime to do it's work. It +does cast doubles to S64s and shifts and divides U64s, which does +drag in CRT code on most platforms. + +It compiles to roughly 8K with float support, and 4K without. +As a comparison, when using MSVC static libs, calling sprintf drags +in 16K. + +API: +==== +int stbsp_sprintf( char * buf, char const * fmt, ... ) +int stbsp_snprintf( char * buf, int count, char const * fmt, ... ) + Convert an arg list into a buffer. stbsp_snprintf always returns + a zero-terminated string (unlike regular snprintf). + +int stbsp_vsprintf( char * buf, char const * fmt, va_list va ) +int stbsp_vsnprintf( char * buf, int count, char const * fmt, va_list va ) + Convert a va_list arg list into a buffer. stbsp_vsnprintf always returns + a zero-terminated string (unlike regular snprintf). + +int stbsp_vsprintfcb( STBSP_SPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va ) + typedef char * STBSP_SPRINTFCB( char const * buf, void * user, int len ); + Convert into a buffer, calling back every STB_SPRINTF_MIN chars. + Your callback can then copy the chars out, print them or whatever. + This function is actually the workhorse for everything else. + The buffer you pass in must hold at least STB_SPRINTF_MIN characters. + // you return the next buffer to use or 0 to stop converting + +void stbsp_set_separators( char comma, char period ) + Set the comma and period characters to use. + +FLOATS/DOUBLES: +=============== +This code uses a internal float->ascii conversion method that uses +doubles with error correction (double-doubles, for ~105 bits of +precision). This conversion is round-trip perfect - that is, an atof +of the values output here will give you the bit-exact double back. + +One difference is that our insignificant digits will be different than +with MSVC or GCC (but they don't match each other either). We also +don't attempt to find the minimum length matching float (pre-MSVC15 +doesn't either). + +If you don't need float or doubles at all, define STB_SPRINTF_NOFLOAT +and you'll save 4K of code space. + +64-BIT INTS: +============ +This library also supports 64-bit integers and you can use MSVC style or +GCC style indicators (%I64d or %lld). It supports the C99 specifiers +for size_t and ptr_diff_t (%jd %zd) as well. + +EXTRAS: +======= +Like some GCCs, for integers and floats, you can use a ' (single quote) +specifier and commas will be inserted on the thousands: "%'d" on 12345 +would print 12,345. + +For integers and floats, you can use a "$" specifier and the number +will be converted to float and then divided to get kilo, mega, giga or +tera and then printed, so "%$d" 1000 is "1.0 k", "%$.2d" 2536000 is +"2.53 M", etc. For byte values, use two $:s, like "%$$d" to turn +2536000 to "2.42 Mi". If you prefer JEDEC suffixes to SI ones, use three +$:s: "%$$$d" -> "2.42 M". To remove the space between the number and the +suffix, add "_" specifier: "%_$d" -> "2.53M". + +In addition to octal and hexadecimal conversions, you can print +integers in binary: "%b" for 256 would print 100. + +PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC): +=================================================================== +"%d" across all 32-bit ints (4.8x/4.0x faster than 32-/64-bit MSVC) +"%24d" across all 32-bit ints (4.5x/4.2x faster) +"%x" across all 32-bit ints (4.5x/3.8x faster) +"%08x" across all 32-bit ints (4.3x/3.8x faster) +"%f" across e-10 to e+10 floats (7.3x/6.0x faster) +"%e" across e-10 to e+10 floats (8.1x/6.0x faster) +"%g" across e-10 to e+10 floats (10.0x/7.1x faster) +"%f" for values near e-300 (7.9x/6.5x faster) +"%f" for values near e+300 (10.0x/9.1x faster) +"%e" for values near e-300 (10.1x/7.0x faster) +"%e" for values near e+300 (9.2x/6.0x faster) +"%.320f" for values near e-300 (12.6x/11.2x faster) +"%a" for random values (8.6x/4.3x faster) +"%I64d" for 64-bits with 32-bit values (4.8x/3.4x faster) +"%I64d" for 64-bits > 32-bit values (4.9x/5.5x faster) +"%s%s%s" for 64 char strings (7.1x/7.3x faster) +"...512 char string..." ( 35.0x/32.5x faster!) +*/ + +#if defined(__clang__) + #if defined(__has_feature) && defined(__has_attribute) + #if __has_feature(address_sanitizer) + #if __has_attribute(__no_sanitize__) + #define STBSP__ASAN __attribute__((__no_sanitize__("address"))) + #elif __has_attribute(__no_sanitize_address__) + #define STBSP__ASAN __attribute__((__no_sanitize_address__)) + #elif __has_attribute(__no_address_safety_analysis__) + #define STBSP__ASAN __attribute__((__no_address_safety_analysis__)) + #endif + #endif + #endif +#elif defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) + #if defined(__SANITIZE_ADDRESS__) && __SANITIZE_ADDRESS__ + #define STBSP__ASAN __attribute__((__no_sanitize_address__)) + #endif +#endif + +#ifndef STBSP__ASAN +#define STBSP__ASAN +#endif + +#ifdef STB_SPRINTF_STATIC +#define STBSP__PUBLICDEC static +#define STBSP__PUBLICDEF static STBSP__ASAN +#else +#ifdef __cplusplus +#define STBSP__PUBLICDEC extern "C" +#define STBSP__PUBLICDEF extern "C" STBSP__ASAN +#else +#define STBSP__PUBLICDEC extern +#define STBSP__PUBLICDEF STBSP__ASAN +#endif +#endif + +#if defined(__has_attribute) + #if __has_attribute(format) + #define STBSP__ATTRIBUTE_FORMAT(fmt,va) __attribute__((format(printf,fmt,va))) + #endif +#endif + +#ifndef STBSP__ATTRIBUTE_FORMAT +#define STBSP__ATTRIBUTE_FORMAT(fmt,va) +#endif + +#ifdef _MSC_VER +#define STBSP__NOTUSED(v) (void)(v) +#else +#define STBSP__NOTUSED(v) (void)sizeof(v) +#endif + +#include // for va_arg(), va_list() +#include // size_t, ptrdiff_t + +#ifndef STB_SPRINTF_MIN +#define STB_SPRINTF_MIN 512 // how many characters per callback +#endif +typedef char *STBSP_SPRINTFCB(const char *buf, void *user, int len); + +#ifndef STB_SPRINTF_DECORATE +#define STB_SPRINTF_DECORATE(name) stbsp_##name // define this before including if you want to change the names +#endif + +STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va); +STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsnprintf)(char *buf, int count, char const *fmt, va_list va); +STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...) STBSP__ATTRIBUTE_FORMAT(2,3); +STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...) STBSP__ATTRIBUTE_FORMAT(3,4); + +STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va); +STBSP__PUBLICDEC void STB_SPRINTF_DECORATE(set_separators)(char comma, char period); + +#endif // STB_SPRINTF_H_INCLUDE + +#ifdef STB_SPRINTF_IMPLEMENTATION + +#define stbsp__uint32 unsigned int +#define stbsp__int32 signed int + +#ifdef _MSC_VER +#define stbsp__uint64 unsigned __int64 +#define stbsp__int64 signed __int64 +#else +#define stbsp__uint64 unsigned long long +#define stbsp__int64 signed long long +#endif +#define stbsp__uint16 unsigned short + +#ifndef stbsp__uintptr +#if defined(__ppc64__) || defined(__powerpc64__) || defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64) || defined(__s390x__) +#define stbsp__uintptr stbsp__uint64 +#else +#define stbsp__uintptr stbsp__uint32 +#endif +#endif + +#ifndef STB_SPRINTF_MSVC_MODE // used for MSVC2013 and earlier (MSVC2015 matches GCC) +#if defined(_MSC_VER) && (_MSC_VER < 1900) +#define STB_SPRINTF_MSVC_MODE +#endif +#endif + +#ifdef STB_SPRINTF_NOUNALIGNED // define this before inclusion to force stbsp_sprintf to always use aligned accesses +#define STBSP__UNALIGNED(code) +#else +#define STBSP__UNALIGNED(code) code +#endif + +#ifndef STB_SPRINTF_NOFLOAT +// internal float utility functions +static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits); +static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value); +#define STBSP__SPECIAL 0x7000 +#endif + +static char stbsp__period = '.'; +static char stbsp__comma = ','; +static struct +{ + short temp; // force next field to be 2-byte aligned + char pair[201]; +} stbsp__digitpair = +{ + 0, + "00010203040506070809101112131415161718192021222324" + "25262728293031323334353637383940414243444546474849" + "50515253545556575859606162636465666768697071727374" + "75767778798081828384858687888990919293949596979899" +}; + +STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char pcomma, char pperiod) +{ + stbsp__period = pperiod; + stbsp__comma = pcomma; +} + +#define STBSP__LEFTJUST 1 +#define STBSP__LEADINGPLUS 2 +#define STBSP__LEADINGSPACE 4 +#define STBSP__LEADING_0X 8 +#define STBSP__LEADINGZERO 16 +#define STBSP__INTMAX 32 +#define STBSP__TRIPLET_COMMA 64 +#define STBSP__NEGATIVE 128 +#define STBSP__METRIC_SUFFIX 256 +#define STBSP__HALFWIDTH 512 +#define STBSP__METRIC_NOSPACE 1024 +#define STBSP__METRIC_1024 2048 +#define STBSP__METRIC_JEDEC 4096 + +static void stbsp__lead_sign(stbsp__uint32 fl, char *sign) +{ + sign[0] = 0; + if (fl & STBSP__NEGATIVE) { + sign[0] = 1; + sign[1] = '-'; + } else if (fl & STBSP__LEADINGSPACE) { + sign[0] = 1; + sign[1] = ' '; + } else if (fl & STBSP__LEADINGPLUS) { + sign[0] = 1; + sign[1] = '+'; + } +} + +static STBSP__ASAN stbsp__uint32 stbsp__strlen_limited(char const *s, stbsp__uint32 limit) +{ + char const * sn = s; + + // get up to 4-byte alignment + for (;;) { + if (((stbsp__uintptr)sn & 3) == 0) + break; + + if (!limit || *sn == 0) + return (stbsp__uint32)(sn - s); + + ++sn; + --limit; + } + + // scan over 4 bytes at a time to find terminating 0 + // this will intentionally scan up to 3 bytes past the end of buffers, + // but becase it works 4B aligned, it will never cross page boundaries + // (hence the STBSP__ASAN markup; the over-read here is intentional + // and harmless) + while (limit >= 4) { + stbsp__uint32 v = *(stbsp__uint32 *)sn; + // bit hack to find if there's a 0 byte in there + if ((v - 0x01010101) & (~v) & 0x80808080UL) + break; + + sn += 4; + limit -= 4; + } + + // handle the last few characters to find actual size + while (limit && *sn) { + ++sn; + --limit; + } + + return (stbsp__uint32)(sn - s); +} + +STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va) +{ + static char hex[] = "0123456789abcdefxp"; + static char hexu[] = "0123456789ABCDEFXP"; + char *bf; + char const *f; + int tlen = 0; + + bf = buf; + f = fmt; + for (;;) { + stbsp__int32 fw, pr, tz; + stbsp__uint32 fl; + + // macros for the callback buffer stuff + #define stbsp__chk_cb_bufL(bytes) \ + { \ + int len = (int)(bf - buf); \ + if ((len + (bytes)) >= STB_SPRINTF_MIN) { \ + tlen += len; \ + if (0 == (bf = buf = callback(buf, user, len))) \ + goto done; \ + } \ + } + #define stbsp__chk_cb_buf(bytes) \ + { \ + if (callback) { \ + stbsp__chk_cb_bufL(bytes); \ + } \ + } + #define stbsp__flush_cb() \ + { \ + stbsp__chk_cb_bufL(STB_SPRINTF_MIN - 1); \ + } // flush if there is even one byte in the buffer + #define stbsp__cb_buf_clamp(cl, v) \ + cl = v; \ + if (callback) { \ + int lg = STB_SPRINTF_MIN - (int)(bf - buf); \ + if (cl > lg) \ + cl = lg; \ + } + + // fast copy everything up to the next % (or end of string) + for (;;) { + while (((stbsp__uintptr)f) & 3) { + schk1: + if (f[0] == '%') + goto scandd; + schk2: + if (f[0] == 0) + goto endfmt; + stbsp__chk_cb_buf(1); + *bf++ = f[0]; + ++f; + } + for (;;) { + // Check if the next 4 bytes contain %(0x25) or end of string. + // Using the 'hasless' trick: + // https://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord + stbsp__uint32 v, c; + v = *(stbsp__uint32 *)f; + c = (~v) & 0x80808080; + if (((v ^ 0x25252525) - 0x01010101) & c) + goto schk1; + if ((v - 0x01010101) & c) + goto schk2; + if (callback) + if ((STB_SPRINTF_MIN - (int)(bf - buf)) < 4) + goto schk1; + #ifdef STB_SPRINTF_NOUNALIGNED + if(((stbsp__uintptr)bf) & 3) { + bf[0] = f[0]; + bf[1] = f[1]; + bf[2] = f[2]; + bf[3] = f[3]; + } else + #endif + { + *(stbsp__uint32 *)bf = v; + } + bf += 4; + f += 4; + } + } + scandd: + + ++f; + + // ok, we have a percent, read the modifiers first + fw = 0; + pr = -1; + fl = 0; + tz = 0; + + // flags + for (;;) { + switch (f[0]) { + // if we have left justify + case '-': + fl |= STBSP__LEFTJUST; + ++f; + continue; + // if we have leading plus + case '+': + fl |= STBSP__LEADINGPLUS; + ++f; + continue; + // if we have leading space + case ' ': + fl |= STBSP__LEADINGSPACE; + ++f; + continue; + // if we have leading 0x + case '#': + fl |= STBSP__LEADING_0X; + ++f; + continue; + // if we have thousand commas + case '\'': + fl |= STBSP__TRIPLET_COMMA; + ++f; + continue; + // if we have kilo marker (none->kilo->kibi->jedec) + case '$': + if (fl & STBSP__METRIC_SUFFIX) { + if (fl & STBSP__METRIC_1024) { + fl |= STBSP__METRIC_JEDEC; + } else { + fl |= STBSP__METRIC_1024; + } + } else { + fl |= STBSP__METRIC_SUFFIX; + } + ++f; + continue; + // if we don't want space between metric suffix and number + case '_': + fl |= STBSP__METRIC_NOSPACE; + ++f; + continue; + // if we have leading zero + case '0': + fl |= STBSP__LEADINGZERO; + ++f; + goto flags_done; + default: goto flags_done; + } + } + flags_done: + + // get the field width + if (f[0] == '*') { + fw = va_arg(va, stbsp__uint32); + ++f; + } else { + while ((f[0] >= '0') && (f[0] <= '9')) { + fw = fw * 10 + f[0] - '0'; + f++; + } + } + // get the precision + if (f[0] == '.') { + ++f; + if (f[0] == '*') { + pr = va_arg(va, stbsp__uint32); + ++f; + } else { + pr = 0; + while ((f[0] >= '0') && (f[0] <= '9')) { + pr = pr * 10 + f[0] - '0'; + f++; + } + } + } + + // handle integer size overrides + switch (f[0]) { + // are we halfwidth? + case 'h': + fl |= STBSP__HALFWIDTH; + ++f; + if (f[0] == 'h') + ++f; // QUARTERWIDTH + break; + // are we 64-bit (unix style) + case 'l': + fl |= ((sizeof(long) == 8) ? STBSP__INTMAX : 0); + ++f; + if (f[0] == 'l') { + fl |= STBSP__INTMAX; + ++f; + } + break; + // are we 64-bit on intmax? (c99) + case 'j': + fl |= (sizeof(size_t) == 8) ? STBSP__INTMAX : 0; + ++f; + break; + // are we 64-bit on size_t or ptrdiff_t? (c99) + case 'z': + fl |= (sizeof(ptrdiff_t) == 8) ? STBSP__INTMAX : 0; + ++f; + break; + case 't': + fl |= (sizeof(ptrdiff_t) == 8) ? STBSP__INTMAX : 0; + ++f; + break; + // are we 64-bit (msft style) + case 'I': + if ((f[1] == '6') && (f[2] == '4')) { + fl |= STBSP__INTMAX; + f += 3; + } else if ((f[1] == '3') && (f[2] == '2')) { + f += 3; + } else { + fl |= ((sizeof(void *) == 8) ? STBSP__INTMAX : 0); + ++f; + } + break; + default: break; + } + + // handle each replacement + switch (f[0]) { + #define STBSP__NUMSZ 512 // big enough for e308 (with commas) or e-307 + char num[STBSP__NUMSZ]; + char lead[8]; + char tail[8]; + char *s; + char const *h; + stbsp__uint32 l, n, cs; + stbsp__uint64 n64; +#ifndef STB_SPRINTF_NOFLOAT + double fv; +#endif + stbsp__int32 dp; + char const *sn; + + case 's': + // get the string + s = va_arg(va, char *); + if (s == 0) + s = (char *)"null"; + // get the length, limited to desired precision + // always limit to ~0u chars since our counts are 32b + l = stbsp__strlen_limited(s, (pr >= 0) ? pr : ~0u); + lead[0] = 0; + tail[0] = 0; + pr = 0; + dp = 0; + cs = 0; + // copy the string in + goto scopy; + + case 'c': // char + // get the character + s = num + STBSP__NUMSZ - 1; + *s = (char)va_arg(va, int); + l = 1; + lead[0] = 0; + tail[0] = 0; + pr = 0; + dp = 0; + cs = 0; + goto scopy; + + case 'n': // weird write-bytes specifier + { + int *d = va_arg(va, int *); + *d = tlen + (int)(bf - buf); + } break; + +#ifdef STB_SPRINTF_NOFLOAT + case 'A': // float + case 'a': // hex float + case 'G': // float + case 'g': // float + case 'E': // float + case 'e': // float + case 'f': // float + va_arg(va, double); // eat it + s = (char *)"No float"; + l = 8; + lead[0] = 0; + tail[0] = 0; + pr = 0; + cs = 0; + STBSP__NOTUSED(dp); + goto scopy; +#else + case 'A': // hex float + case 'a': // hex float + h = (f[0] == 'A') ? hexu : hex; + fv = va_arg(va, double); + if (pr == -1) + pr = 6; // default is 6 + // read the double into a string + if (stbsp__real_to_parts((stbsp__int64 *)&n64, &dp, fv)) + fl |= STBSP__NEGATIVE; + + s = num + 64; + + stbsp__lead_sign(fl, lead); + + if (dp == -1023) + dp = (n64) ? -1022 : 0; + else + n64 |= (((stbsp__uint64)1) << 52); + n64 <<= (64 - 56); + if (pr < 15) + n64 += ((((stbsp__uint64)8) << 56) >> (pr * 4)); +// add leading chars + +#ifdef STB_SPRINTF_MSVC_MODE + *s++ = '0'; + *s++ = 'x'; +#else + lead[1 + lead[0]] = '0'; + lead[2 + lead[0]] = 'x'; + lead[0] += 2; +#endif + *s++ = h[(n64 >> 60) & 15]; + n64 <<= 4; + if (pr) + *s++ = stbsp__period; + sn = s; + + // print the bits + n = pr; + if (n > 13) + n = 13; + if (pr > (stbsp__int32)n) + tz = pr - n; + pr = 0; + while (n--) { + *s++ = h[(n64 >> 60) & 15]; + n64 <<= 4; + } + + // print the expo + tail[1] = h[17]; + if (dp < 0) { + tail[2] = '-'; + dp = -dp; + } else + tail[2] = '+'; + n = (dp >= 1000) ? 6 : ((dp >= 100) ? 5 : ((dp >= 10) ? 4 : 3)); + tail[0] = (char)n; + for (;;) { + tail[n] = '0' + dp % 10; + if (n <= 3) + break; + --n; + dp /= 10; + } + + dp = (int)(s - sn); + l = (int)(s - (num + 64)); + s = num + 64; + cs = 1 + (3 << 24); + goto scopy; + + case 'G': // float + case 'g': // float + h = (f[0] == 'G') ? hexu : hex; + fv = va_arg(va, double); + if (pr == -1) + pr = 6; + else if (pr == 0) + pr = 1; // default is 6 + // read the double into a string + if (stbsp__real_to_str(&sn, &l, num, &dp, fv, (pr - 1) | 0x80000000)) + fl |= STBSP__NEGATIVE; + + // clamp the precision and delete extra zeros after clamp + n = pr; + if (l > (stbsp__uint32)pr) + l = pr; + while ((l > 1) && (pr) && (sn[l - 1] == '0')) { + --pr; + --l; + } + + // should we use %e + if ((dp <= -4) || (dp > (stbsp__int32)n)) { + if (pr > (stbsp__int32)l) + pr = l - 1; + else if (pr) + --pr; // when using %e, there is one digit before the decimal + goto doexpfromg; + } + // this is the insane action to get the pr to match %g semantics for %f + if (dp > 0) { + pr = (dp < (stbsp__int32)l) ? l - dp : 0; + } else { + pr = -dp + ((pr > (stbsp__int32)l) ? (stbsp__int32) l : pr); + } + goto dofloatfromg; + + case 'E': // float + case 'e': // float + h = (f[0] == 'E') ? hexu : hex; + fv = va_arg(va, double); + if (pr == -1) + pr = 6; // default is 6 + // read the double into a string + if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr | 0x80000000)) + fl |= STBSP__NEGATIVE; + doexpfromg: + tail[0] = 0; + stbsp__lead_sign(fl, lead); + if (dp == STBSP__SPECIAL) { + s = (char *)sn; + cs = 0; + pr = 0; + goto scopy; + } + s = num + 64; + // handle leading chars + *s++ = sn[0]; + + if (pr) + *s++ = stbsp__period; + + // handle after decimal + if ((l - 1) > (stbsp__uint32)pr) + l = pr + 1; + for (n = 1; n < l; n++) + *s++ = sn[n]; + // trailing zeros + tz = pr - (l - 1); + pr = 0; + // dump expo + tail[1] = h[0xe]; + dp -= 1; + if (dp < 0) { + tail[2] = '-'; + dp = -dp; + } else + tail[2] = '+'; +#ifdef STB_SPRINTF_MSVC_MODE + n = 5; +#else + n = (dp >= 100) ? 5 : 4; +#endif + tail[0] = (char)n; + for (;;) { + tail[n] = '0' + dp % 10; + if (n <= 3) + break; + --n; + dp /= 10; + } + cs = 1 + (3 << 24); // how many tens + goto flt_lead; + + case 'f': // float + fv = va_arg(va, double); + doafloat: + // do kilos + if (fl & STBSP__METRIC_SUFFIX) { + double divisor; + divisor = 1000.0f; + if (fl & STBSP__METRIC_1024) + divisor = 1024.0; + while (fl < 0x4000000) { + if ((fv < divisor) && (fv > -divisor)) + break; + fv /= divisor; + fl += 0x1000000; + } + } + if (pr == -1) + pr = 6; // default is 6 + // read the double into a string + if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr)) + fl |= STBSP__NEGATIVE; + dofloatfromg: + tail[0] = 0; + stbsp__lead_sign(fl, lead); + if (dp == STBSP__SPECIAL) { + s = (char *)sn; + cs = 0; + pr = 0; + goto scopy; + } + s = num + 64; + + // handle the three decimal varieties + if (dp <= 0) { + stbsp__int32 i; + // handle 0.000*000xxxx + *s++ = '0'; + if (pr) + *s++ = stbsp__period; + n = -dp; + if ((stbsp__int32)n > pr) + n = pr; + i = n; + while (i) { + if ((((stbsp__uintptr)s) & 3) == 0) + break; + *s++ = '0'; + --i; + } + while (i >= 4) { + *(stbsp__uint32 *)s = 0x30303030; + s += 4; + i -= 4; + } + while (i) { + *s++ = '0'; + --i; + } + if ((stbsp__int32)(l + n) > pr) + l = pr - n; + i = l; + while (i) { + *s++ = *sn++; + --i; + } + tz = pr - (n + l); + cs = 1 + (3 << 24); // how many tens did we write (for commas below) + } else { + cs = (fl & STBSP__TRIPLET_COMMA) ? ((600 - (stbsp__uint32)dp) % 3) : 0; + if ((stbsp__uint32)dp >= l) { + // handle xxxx000*000.0 + n = 0; + for (;;) { + if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) { + cs = 0; + *s++ = stbsp__comma; + } else { + *s++ = sn[n]; + ++n; + if (n >= l) + break; + } + } + if (n < (stbsp__uint32)dp) { + n = dp - n; + if ((fl & STBSP__TRIPLET_COMMA) == 0) { + while (n) { + if ((((stbsp__uintptr)s) & 3) == 0) + break; + *s++ = '0'; + --n; + } + while (n >= 4) { + *(stbsp__uint32 *)s = 0x30303030; + s += 4; + n -= 4; + } + } + while (n) { + if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) { + cs = 0; + *s++ = stbsp__comma; + } else { + *s++ = '0'; + --n; + } + } + } + cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens + if (pr) { + *s++ = stbsp__period; + tz = pr; + } + } else { + // handle xxxxx.xxxx000*000 + n = 0; + for (;;) { + if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) { + cs = 0; + *s++ = stbsp__comma; + } else { + *s++ = sn[n]; + ++n; + if (n >= (stbsp__uint32)dp) + break; + } + } + cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens + if (pr) + *s++ = stbsp__period; + if ((l - dp) > (stbsp__uint32)pr) + l = pr + dp; + while (n < l) { + *s++ = sn[n]; + ++n; + } + tz = pr - (l - dp); + } + } + pr = 0; + + // handle k,m,g,t + if (fl & STBSP__METRIC_SUFFIX) { + char idx; + idx = 1; + if (fl & STBSP__METRIC_NOSPACE) + idx = 0; + tail[0] = idx; + tail[1] = ' '; + { + if (fl >> 24) { // SI kilo is 'k', JEDEC and SI kibits are 'K'. + if (fl & STBSP__METRIC_1024) + tail[idx + 1] = "_KMGT"[fl >> 24]; + else + tail[idx + 1] = "_kMGT"[fl >> 24]; + idx++; + // If printing kibits and not in jedec, add the 'i'. + if (fl & STBSP__METRIC_1024 && !(fl & STBSP__METRIC_JEDEC)) { + tail[idx + 1] = 'i'; + idx++; + } + tail[0] = idx; + } + } + }; + + flt_lead: + // get the length that we copied + l = (stbsp__uint32)(s - (num + 64)); + s = num + 64; + goto scopy; +#endif + + case 'B': // upper binary + case 'b': // lower binary + h = (f[0] == 'B') ? hexu : hex; + lead[0] = 0; + if (fl & STBSP__LEADING_0X) { + lead[0] = 2; + lead[1] = '0'; + lead[2] = h[0xb]; + } + l = (8 << 4) | (1 << 8); + goto radixnum; + + case 'o': // octal + h = hexu; + lead[0] = 0; + if (fl & STBSP__LEADING_0X) { + lead[0] = 1; + lead[1] = '0'; + } + l = (3 << 4) | (3 << 8); + goto radixnum; + + case 'p': // pointer + fl |= (sizeof(void *) == 8) ? STBSP__INTMAX : 0; + pr = sizeof(void *) * 2; + fl &= ~STBSP__LEADINGZERO; // 'p' only prints the pointer with zeros + // fall through - to X + + case 'X': // upper hex + case 'x': // lower hex + h = (f[0] == 'X') ? hexu : hex; + l = (4 << 4) | (4 << 8); + lead[0] = 0; + if (fl & STBSP__LEADING_0X) { + lead[0] = 2; + lead[1] = '0'; + lead[2] = h[16]; + } + radixnum: + // get the number + if (fl & STBSP__INTMAX) + n64 = va_arg(va, stbsp__uint64); + else + n64 = va_arg(va, stbsp__uint32); + + s = num + STBSP__NUMSZ; + dp = 0; + // clear tail, and clear leading if value is zero + tail[0] = 0; + if (n64 == 0) { + lead[0] = 0; + if (pr == 0) { + l = 0; + cs = 0; + goto scopy; + } + } + // convert to string + for (;;) { + *--s = h[n64 & ((1 << (l >> 8)) - 1)]; + n64 >>= (l >> 8); + if (!((n64) || ((stbsp__int32)((num + STBSP__NUMSZ) - s) < pr))) + break; + if (fl & STBSP__TRIPLET_COMMA) { + ++l; + if ((l & 15) == ((l >> 4) & 15)) { + l &= ~15; + *--s = stbsp__comma; + } + } + }; + // get the tens and the comma pos + cs = (stbsp__uint32)((num + STBSP__NUMSZ) - s) + ((((l >> 4) & 15)) << 24); + // get the length that we copied + l = (stbsp__uint32)((num + STBSP__NUMSZ) - s); + // copy it + goto scopy; + + case 'u': // unsigned + case 'i': + case 'd': // integer + // get the integer and abs it + if (fl & STBSP__INTMAX) { + stbsp__int64 i64 = va_arg(va, stbsp__int64); + n64 = (stbsp__uint64)i64; + if ((f[0] != 'u') && (i64 < 0)) { + n64 = (stbsp__uint64)-i64; + fl |= STBSP__NEGATIVE; + } + } else { + stbsp__int32 i = va_arg(va, stbsp__int32); + n64 = (stbsp__uint32)i; + if ((f[0] != 'u') && (i < 0)) { + n64 = (stbsp__uint32)-i; + fl |= STBSP__NEGATIVE; + } + } + +#ifndef STB_SPRINTF_NOFLOAT + if (fl & STBSP__METRIC_SUFFIX) { + if (n64 < 1024) + pr = 0; + else if (pr == -1) + pr = 1; + fv = (double)(stbsp__int64)n64; + goto doafloat; + } +#endif + + // convert to string + s = num + STBSP__NUMSZ; + l = 0; + + for (;;) { + // do in 32-bit chunks (avoid lots of 64-bit divides even with constant denominators) + char *o = s - 8; + if (n64 >= 100000000) { + n = (stbsp__uint32)(n64 % 100000000); + n64 /= 100000000; + } else { + n = (stbsp__uint32)n64; + n64 = 0; + } + if ((fl & STBSP__TRIPLET_COMMA) == 0) { + do { + s -= 2; + *(stbsp__uint16 *)s = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2]; + n /= 100; + } while (n); + } + while (n) { + if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) { + l = 0; + *--s = stbsp__comma; + --o; + } else { + *--s = (char)(n % 10) + '0'; + n /= 10; + } + } + if (n64 == 0) { + if ((s[0] == '0') && (s != (num + STBSP__NUMSZ))) + ++s; + break; + } + while (s != o) + if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) { + l = 0; + *--s = stbsp__comma; + --o; + } else { + *--s = '0'; + } + } + + tail[0] = 0; + stbsp__lead_sign(fl, lead); + + // get the length that we copied + l = (stbsp__uint32)((num + STBSP__NUMSZ) - s); + if (l == 0) { + *--s = '0'; + l = 1; + } + cs = l + (3 << 24); + if (pr < 0) + pr = 0; + + scopy: + // get fw=leading/trailing space, pr=leading zeros + if (pr < (stbsp__int32)l) + pr = l; + n = pr + lead[0] + tail[0] + tz; + if (fw < (stbsp__int32)n) + fw = n; + fw -= n; + pr -= l; + + // handle right justify and leading zeros + if ((fl & STBSP__LEFTJUST) == 0) { + if (fl & STBSP__LEADINGZERO) // if leading zeros, everything is in pr + { + pr = (fw > pr) ? fw : pr; + fw = 0; + } else { + fl &= ~STBSP__TRIPLET_COMMA; // if no leading zeros, then no commas + } + } + + // copy the spaces and/or zeros + if (fw + pr) { + stbsp__int32 i; + stbsp__uint32 c; + + // copy leading spaces (or when doing %8.4d stuff) + if ((fl & STBSP__LEFTJUST) == 0) + while (fw > 0) { + stbsp__cb_buf_clamp(i, fw); + fw -= i; + while (i) { + if ((((stbsp__uintptr)bf) & 3) == 0) + break; + *bf++ = ' '; + --i; + } + while (i >= 4) { + *(stbsp__uint32 *)bf = 0x20202020; + bf += 4; + i -= 4; + } + while (i) { + *bf++ = ' '; + --i; + } + stbsp__chk_cb_buf(1); + } + + // copy leader + sn = lead + 1; + while (lead[0]) { + stbsp__cb_buf_clamp(i, lead[0]); + lead[0] -= (char)i; + while (i) { + *bf++ = *sn++; + --i; + } + stbsp__chk_cb_buf(1); + } + + // copy leading zeros + c = cs >> 24; + cs &= 0xffffff; + cs = (fl & STBSP__TRIPLET_COMMA) ? ((stbsp__uint32)(c - ((pr + cs) % (c + 1)))) : 0; + while (pr > 0) { + stbsp__cb_buf_clamp(i, pr); + pr -= i; + if ((fl & STBSP__TRIPLET_COMMA) == 0) { + while (i) { + if ((((stbsp__uintptr)bf) & 3) == 0) + break; + *bf++ = '0'; + --i; + } + while (i >= 4) { + *(stbsp__uint32 *)bf = 0x30303030; + bf += 4; + i -= 4; + } + } + while (i) { + if ((fl & STBSP__TRIPLET_COMMA) && (cs++ == c)) { + cs = 0; + *bf++ = stbsp__comma; + } else + *bf++ = '0'; + --i; + } + stbsp__chk_cb_buf(1); + } + } + + // copy leader if there is still one + sn = lead + 1; + while (lead[0]) { + stbsp__int32 i; + stbsp__cb_buf_clamp(i, lead[0]); + lead[0] -= (char)i; + while (i) { + *bf++ = *sn++; + --i; + } + stbsp__chk_cb_buf(1); + } + + // copy the string + n = l; + while (n) { + stbsp__int32 i; + stbsp__cb_buf_clamp(i, n); + n -= i; + STBSP__UNALIGNED(while (i >= 4) { + *(stbsp__uint32 volatile *)bf = *(stbsp__uint32 volatile *)s; + bf += 4; + s += 4; + i -= 4; + }) + while (i) { + *bf++ = *s++; + --i; + } + stbsp__chk_cb_buf(1); + } + + // copy trailing zeros + while (tz) { + stbsp__int32 i; + stbsp__cb_buf_clamp(i, tz); + tz -= i; + while (i) { + if ((((stbsp__uintptr)bf) & 3) == 0) + break; + *bf++ = '0'; + --i; + } + while (i >= 4) { + *(stbsp__uint32 *)bf = 0x30303030; + bf += 4; + i -= 4; + } + while (i) { + *bf++ = '0'; + --i; + } + stbsp__chk_cb_buf(1); + } + + // copy tail if there is one + sn = tail + 1; + while (tail[0]) { + stbsp__int32 i; + stbsp__cb_buf_clamp(i, tail[0]); + tail[0] -= (char)i; + while (i) { + *bf++ = *sn++; + --i; + } + stbsp__chk_cb_buf(1); + } + + // handle the left justify + if (fl & STBSP__LEFTJUST) + if (fw > 0) { + while (fw) { + stbsp__int32 i; + stbsp__cb_buf_clamp(i, fw); + fw -= i; + while (i) { + if ((((stbsp__uintptr)bf) & 3) == 0) + break; + *bf++ = ' '; + --i; + } + while (i >= 4) { + *(stbsp__uint32 *)bf = 0x20202020; + bf += 4; + i -= 4; + } + while (i--) + *bf++ = ' '; + stbsp__chk_cb_buf(1); + } + } + break; + + default: // unknown, just copy code + s = num + STBSP__NUMSZ - 1; + *s = f[0]; + l = 1; + fw = fl = 0; + lead[0] = 0; + tail[0] = 0; + pr = 0; + dp = 0; + cs = 0; + goto scopy; + } + ++f; + } +endfmt: + + if (!callback) + *bf = 0; + else + stbsp__flush_cb(); + +done: + return tlen + (int)(bf - buf); +} + +// cleanup +#undef STBSP__LEFTJUST +#undef STBSP__LEADINGPLUS +#undef STBSP__LEADINGSPACE +#undef STBSP__LEADING_0X +#undef STBSP__LEADINGZERO +#undef STBSP__INTMAX +#undef STBSP__TRIPLET_COMMA +#undef STBSP__NEGATIVE +#undef STBSP__METRIC_SUFFIX +#undef STBSP__NUMSZ +#undef stbsp__chk_cb_bufL +#undef stbsp__chk_cb_buf +#undef stbsp__flush_cb +#undef stbsp__cb_buf_clamp + +// ============================================================================ +// wrapper functions + +STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...) +{ + int result; + va_list va; + va_start(va, fmt); + result = STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va); + va_end(va); + return result; +} + +typedef struct stbsp__context { + char *buf; + int count; + int length; + char tmp[STB_SPRINTF_MIN]; +} stbsp__context; + +static char *stbsp__clamp_callback(const char *buf, void *user, int len) +{ + stbsp__context *c = (stbsp__context *)user; + c->length += len; + + if (len > c->count) + len = c->count; + + if (len) { + if (buf != c->buf) { + const char *s, *se; + char *d; + d = c->buf; + s = buf; + se = buf + len; + do { + *d++ = *s++; + } while (s < se); + } + c->buf += len; + c->count -= len; + } + + if (c->count <= 0) + return c->tmp; + return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can +} + +static char * stbsp__count_clamp_callback( const char * buf, void * user, int len ) +{ + stbsp__context * c = (stbsp__context*)user; + (void) sizeof(buf); + + c->length += len; + return c->tmp; // go direct into buffer if you can +} + +STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va ) +{ + stbsp__context c; + + if ( (count == 0) && !buf ) + { + c.length = 0; + + STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va ); + } + else + { + int l; + + c.buf = buf; + c.count = count; + c.length = 0; + + STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va ); + + // zero-terminate + l = (int)( c.buf - buf ); + if ( l >= count ) // should never be greater, only equal (or less) than count + l = count - 1; + buf[l] = 0; + } + + return c.length; +} + +STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...) +{ + int result; + va_list va; + va_start(va, fmt); + + result = STB_SPRINTF_DECORATE(vsnprintf)(buf, count, fmt, va); + va_end(va); + + return result; +} + +STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va) +{ + return STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va); +} + +// ======================================================================= +// low level float utility functions + +#ifndef STB_SPRINTF_NOFLOAT + +// copies d to bits w/ strict aliasing (this compiles to nothing on /Ox) +#define STBSP__COPYFP(dest, src) \ + { \ + int cn; \ + for (cn = 0; cn < 8; cn++) \ + ((char *)&dest)[cn] = ((char *)&src)[cn]; \ + } + +// get float info +static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value) +{ + double d; + stbsp__int64 b = 0; + + // load value and round at the frac_digits + d = value; + + STBSP__COPYFP(b, d); + + *bits = b & ((((stbsp__uint64)1) << 52) - 1); + *expo = (stbsp__int32)(((b >> 52) & 2047) - 1023); + + return (stbsp__int32)((stbsp__uint64) b >> 63); +} + +static double const stbsp__bot[23] = { + 1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009, 1e+010, 1e+011, + 1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019, 1e+020, 1e+021, 1e+022 +}; +static double const stbsp__negbot[22] = { + 1e-001, 1e-002, 1e-003, 1e-004, 1e-005, 1e-006, 1e-007, 1e-008, 1e-009, 1e-010, 1e-011, + 1e-012, 1e-013, 1e-014, 1e-015, 1e-016, 1e-017, 1e-018, 1e-019, 1e-020, 1e-021, 1e-022 +}; +static double const stbsp__negboterr[22] = { + -5.551115123125783e-018, -2.0816681711721684e-019, -2.0816681711721686e-020, -4.7921736023859299e-021, -8.1803053914031305e-022, 4.5251888174113741e-023, + 4.5251888174113739e-024, -2.0922560830128471e-025, -6.2281591457779853e-026, -3.6432197315497743e-027, 6.0503030718060191e-028, 2.0113352370744385e-029, + -3.0373745563400371e-030, 1.1806906454401013e-032, -7.7705399876661076e-032, 2.0902213275965398e-033, -7.1542424054621921e-034, -7.1542424054621926e-035, + 2.4754073164739869e-036, 5.4846728545790429e-037, 9.2462547772103625e-038, -4.8596774326570872e-039 +}; +static double const stbsp__top[13] = { + 1e+023, 1e+046, 1e+069, 1e+092, 1e+115, 1e+138, 1e+161, 1e+184, 1e+207, 1e+230, 1e+253, 1e+276, 1e+299 +}; +static double const stbsp__negtop[13] = { + 1e-023, 1e-046, 1e-069, 1e-092, 1e-115, 1e-138, 1e-161, 1e-184, 1e-207, 1e-230, 1e-253, 1e-276, 1e-299 +}; +static double const stbsp__toperr[13] = { + 8388608, + 6.8601809640529717e+028, + -7.253143638152921e+052, + -4.3377296974619174e+075, + -1.5559416129466825e+098, + -3.2841562489204913e+121, + -3.7745893248228135e+144, + -1.7356668416969134e+167, + -3.8893577551088374e+190, + -9.9566444326005119e+213, + 6.3641293062232429e+236, + -5.2069140800249813e+259, + -5.2504760255204387e+282 +}; +static double const stbsp__negtoperr[13] = { + 3.9565301985100693e-040, -2.299904345391321e-063, 3.6506201437945798e-086, 1.1875228833981544e-109, + -5.0644902316928607e-132, -6.7156837247865426e-155, -2.812077463003139e-178, -5.7778912386589953e-201, + 7.4997100559334532e-224, -4.6439668915134491e-247, -6.3691100762962136e-270, -9.436808465446358e-293, + 8.0970921678014997e-317 +}; + +#if defined(_MSC_VER) && (_MSC_VER <= 1200) +static stbsp__uint64 const stbsp__powten[20] = { + 1, + 10, + 100, + 1000, + 10000, + 100000, + 1000000, + 10000000, + 100000000, + 1000000000, + 10000000000, + 100000000000, + 1000000000000, + 10000000000000, + 100000000000000, + 1000000000000000, + 10000000000000000, + 100000000000000000, + 1000000000000000000, + 10000000000000000000U +}; +#define stbsp__tento19th ((stbsp__uint64)1000000000000000000) +#else +static stbsp__uint64 const stbsp__powten[20] = { + 1, + 10, + 100, + 1000, + 10000, + 100000, + 1000000, + 10000000, + 100000000, + 1000000000, + 10000000000ULL, + 100000000000ULL, + 1000000000000ULL, + 10000000000000ULL, + 100000000000000ULL, + 1000000000000000ULL, + 10000000000000000ULL, + 100000000000000000ULL, + 1000000000000000000ULL, + 10000000000000000000ULL +}; +#define stbsp__tento19th (1000000000000000000ULL) +#endif + +#define stbsp__ddmulthi(oh, ol, xh, yh) \ + { \ + double ahi = 0, alo, bhi = 0, blo; \ + stbsp__int64 bt; \ + oh = xh * yh; \ + STBSP__COPYFP(bt, xh); \ + bt &= ((~(stbsp__uint64)0) << 27); \ + STBSP__COPYFP(ahi, bt); \ + alo = xh - ahi; \ + STBSP__COPYFP(bt, yh); \ + bt &= ((~(stbsp__uint64)0) << 27); \ + STBSP__COPYFP(bhi, bt); \ + blo = yh - bhi; \ + ol = ((ahi * bhi - oh) + ahi * blo + alo * bhi) + alo * blo; \ + } + +#define stbsp__ddtoS64(ob, xh, xl) \ + { \ + double ahi = 0, alo, vh, t; \ + ob = (stbsp__int64)xh; \ + vh = (double)ob; \ + ahi = (xh - vh); \ + t = (ahi - xh); \ + alo = (xh - (ahi - t)) - (vh + t); \ + ob += (stbsp__int64)(ahi + alo + xl); \ + } + +#define stbsp__ddrenorm(oh, ol) \ + { \ + double s; \ + s = oh + ol; \ + ol = ol - (s - oh); \ + oh = s; \ + } + +#define stbsp__ddmultlo(oh, ol, xh, xl, yh, yl) ol = ol + (xh * yl + xl * yh); + +#define stbsp__ddmultlos(oh, ol, xh, yl) ol = ol + (xh * yl); + +static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__int32 power) // power can be -323 to +350 +{ + double ph, pl; + if ((power >= 0) && (power <= 22)) { + stbsp__ddmulthi(ph, pl, d, stbsp__bot[power]); + } else { + stbsp__int32 e, et, eb; + double p2h, p2l; + + e = power; + if (power < 0) + e = -e; + et = (e * 0x2c9) >> 14; /* %23 */ + if (et > 13) + et = 13; + eb = e - (et * 23); + + ph = d; + pl = 0.0; + if (power < 0) { + if (eb) { + --eb; + stbsp__ddmulthi(ph, pl, d, stbsp__negbot[eb]); + stbsp__ddmultlos(ph, pl, d, stbsp__negboterr[eb]); + } + if (et) { + stbsp__ddrenorm(ph, pl); + --et; + stbsp__ddmulthi(p2h, p2l, ph, stbsp__negtop[et]); + stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__negtop[et], stbsp__negtoperr[et]); + ph = p2h; + pl = p2l; + } + } else { + if (eb) { + e = eb; + if (eb > 22) + eb = 22; + e -= eb; + stbsp__ddmulthi(ph, pl, d, stbsp__bot[eb]); + if (e) { + stbsp__ddrenorm(ph, pl); + stbsp__ddmulthi(p2h, p2l, ph, stbsp__bot[e]); + stbsp__ddmultlos(p2h, p2l, stbsp__bot[e], pl); + ph = p2h; + pl = p2l; + } + } + if (et) { + stbsp__ddrenorm(ph, pl); + --et; + stbsp__ddmulthi(p2h, p2l, ph, stbsp__top[et]); + stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__top[et], stbsp__toperr[et]); + ph = p2h; + pl = p2l; + } + } + } + stbsp__ddrenorm(ph, pl); + *ohi = ph; + *olo = pl; +} + +// given a float value, returns the significant bits in bits, and the position of the +// decimal point in decimal_pos. +/-INF and NAN are specified by special values +// returned in the decimal_pos parameter. +// frac_digits is absolute normally, but if you want from first significant digits (got %g and %e), or in 0x80000000 +static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits) +{ + double d; + stbsp__int64 bits = 0; + stbsp__int32 expo, e, ng, tens; + + d = value; + STBSP__COPYFP(bits, d); + expo = (stbsp__int32)((bits >> 52) & 2047); + ng = (stbsp__int32)((stbsp__uint64) bits >> 63); + if (ng) + d = -d; + + if (expo == 2047) // is nan or inf? + { + *start = (bits & ((((stbsp__uint64)1) << 52) - 1)) ? "NaN" : "Inf"; + *decimal_pos = STBSP__SPECIAL; + *len = 3; + return ng; + } + + if (expo == 0) // is zero or denormal + { + if (((stbsp__uint64) bits << 1) == 0) // do zero + { + *decimal_pos = 1; + *start = out; + out[0] = '0'; + *len = 1; + return ng; + } + // find the right expo for denormals + { + stbsp__int64 v = ((stbsp__uint64)1) << 51; + while ((bits & v) == 0) { + --expo; + v >>= 1; + } + } + } + + // find the decimal exponent as well as the decimal bits of the value + { + double ph, pl; + + // log10 estimate - very specifically tweaked to hit or undershoot by no more than 1 of log10 of all expos 1..2046 + tens = expo - 1023; + tens = (tens < 0) ? ((tens * 617) / 2048) : (((tens * 1233) / 4096) + 1); + + // move the significant bits into position and stick them into an int + stbsp__raise_to_power10(&ph, &pl, d, 18 - tens); + + // get full as much precision from double-double as possible + stbsp__ddtoS64(bits, ph, pl); + + // check if we undershot + if (((stbsp__uint64)bits) >= stbsp__tento19th) + ++tens; + } + + // now do the rounding in integer land + frac_digits = (frac_digits & 0x80000000) ? ((frac_digits & 0x7ffffff) + 1) : (tens + frac_digits); + if ((frac_digits < 24)) { + stbsp__uint32 dg = 1; + if ((stbsp__uint64)bits >= stbsp__powten[9]) + dg = 10; + while ((stbsp__uint64)bits >= stbsp__powten[dg]) { + ++dg; + if (dg == 20) + goto noround; + } + if (frac_digits < dg) { + stbsp__uint64 r; + // add 0.5 at the right position and round + e = dg - frac_digits; + if ((stbsp__uint32)e >= 24) + goto noround; + r = stbsp__powten[e]; + bits = bits + (r / 2); + if ((stbsp__uint64)bits >= stbsp__powten[dg]) + ++tens; + bits /= r; + } + noround:; + } + + // kill long trailing runs of zeros + if (bits) { + stbsp__uint32 n; + for (;;) { + if (bits <= 0xffffffff) + break; + if (bits % 1000) + goto donez; + bits /= 1000; + } + n = (stbsp__uint32)bits; + while ((n % 1000) == 0) + n /= 1000; + bits = n; + donez:; + } + + // convert to string + out += 64; + e = 0; + for (;;) { + stbsp__uint32 n; + char *o = out - 8; + // do the conversion in chunks of U32s (avoid most 64-bit divides, worth it, constant denomiators be damned) + if (bits >= 100000000) { + n = (stbsp__uint32)(bits % 100000000); + bits /= 100000000; + } else { + n = (stbsp__uint32)bits; + bits = 0; + } + while (n) { + out -= 2; + *(stbsp__uint16 *)out = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2]; + n /= 100; + e += 2; + } + if (bits == 0) { + if ((e) && (out[0] == '0')) { + ++out; + --e; + } + break; + } + while (out != o) { + *--out = '0'; + ++e; + } + } + + *decimal_pos = tens; + *start = out; + *len = e; + return ng; +} + +#undef stbsp__ddmulthi +#undef stbsp__ddrenorm +#undef stbsp__ddmultlo +#undef stbsp__ddmultlos +#undef STBSP__SPECIAL +#undef STBSP__COPYFP + +#endif // STB_SPRINTF_NOFLOAT + +// clean up +#undef stbsp__uint16 +#undef stbsp__uint32 +#undef stbsp__int32 +#undef stbsp__uint64 +#undef stbsp__int64 +#undef STBSP__UNALIGNED + +#endif // STB_SPRINTF_IMPLEMENTATION + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ \ No newline at end of file