From 16e804b0687080952d19b6fcae92bd228e4865c5 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sat, 23 Dec 2017 18:15:15 +1100 Subject: [PATCH] [host] added tracing class to help profile slow code points --- host/Capture/DXGI.cpp | 4 ++ host/Makefile | 5 ++ host/TraceUtil.cpp | 25 ++++++++ host/TraceUtil.h | 79 +++++++++++++++++++++++++ host/looking-glass-host.vcxproj | 10 ++-- host/looking-glass-host.vcxproj.filters | 6 ++ host/main.cpp | 3 + 7 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 host/TraceUtil.cpp create mode 100644 host/TraceUtil.h diff --git a/host/Capture/DXGI.cpp b/host/Capture/DXGI.cpp index 4b4a0723..3f3309b3 100644 --- a/host/Capture/DXGI.cpp +++ b/host/Capture/DXGI.cpp @@ -21,6 +21,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA using namespace Capture; #include "common/debug.h" +#include "TraceUtil.h" DXGI::DXGI() : m_options(NULL), @@ -264,6 +265,7 @@ void DXGI::WaitForDesktop() GrabStatus DXGI::GrabFrame(FrameInfo & frame) { + TRACE; if (!m_initialized) return GRAB_STATUS_ERROR; @@ -446,7 +448,9 @@ GrabStatus DXGI::GrabFrame(FrameInfo & frame) frame.stride = m_mapping.RowPitch / 4; unsigned int size = m_height * m_mapping.RowPitch; + TRACE_START("DXGI Memory Copy"); m_memcpy.Copy(frame.buffer, m_mapping.pData, size < frame.bufferSize ? size : frame.bufferSize); + TRACE_END; return GRAB_STATUS_OK; } \ No newline at end of file diff --git a/host/Makefile b/host/Makefile index b81eeb77..b4e47814 100644 --- a/host/Makefile +++ b/host/Makefile @@ -25,6 +25,11 @@ OBJS = main.o \ Service.o \ Capture/DXGI.o +ifeq ($(ENABLE_TRACING),1) +CFLAGS += -DENABLE_TRACING +OBJS += TraceUtil.o +endif + ifeq ($(CONFIG_CAPTURE_NVFBC),1) CFLAGS += -DCONFIG_CAPTURE_NVFBC=1 -I../vendor OBJS += Capture/NvFBC.o diff --git a/host/TraceUtil.cpp b/host/TraceUtil.cpp new file mode 100644 index 00000000..b0e62566 --- /dev/null +++ b/host/TraceUtil.cpp @@ -0,0 +1,25 @@ +/* +Looking Glass - KVM FrameRelay (KVMFR) Client +Copyright (C) 2017 Geoffrey McRae +https://looking-glass.hostfission.com + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 2 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "TraceUtil.h" + +double TraceUtil::m_freq; +LARGE_INTEGER TraceUtil::m_last; +LARGE_INTEGER TraceUtil::m_start; +const char * TraceUtil::m_traceName; \ No newline at end of file diff --git a/host/TraceUtil.h b/host/TraceUtil.h new file mode 100644 index 00000000..d0e00ea8 --- /dev/null +++ b/host/TraceUtil.h @@ -0,0 +1,79 @@ +/* +Looking Glass - KVM FrameRelay (KVMFR) Client +Copyright (C) 2017 Geoffrey McRae +https://looking-glass.hostfission.com + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 2 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#pragma once + +#include +#include +#include "common/debug.h" + +#ifdef ENABLE_TRACING + #define TRACE TraceUtil::Trace(__FUNCTION__, __LINE__) + #define TRACE_START(x) TraceUtil::TraceStart(x) + #define TRACE_END TraceUtil::TraceEnd() +#else + #define TRACE + #define TRACE_START(x) + #define TRACE_END +#endif + +class TraceUtil +{ +private: + static double m_freq; + static LARGE_INTEGER m_last; + static LARGE_INTEGER m_start; + static const char * m_traceName; + +public: + static void Initialize() + { +#ifdef ENABLE_TRACING + LARGE_INTEGER now; + QueryPerformanceFrequency(&now); + m_freq = (double)now.QuadPart / 1000.0; + QueryPerformanceCounter(&m_last); +#endif + } + + static inline void Trace(const char * function, const unsigned int line) + { + LARGE_INTEGER now; + QueryPerformanceCounter(&now); + const double diff = (now.QuadPart - m_last.QuadPart) / m_freq; + m_last = now; + + DEBUG_INFO("Trace [%8.4f] %s:%u", diff, function, line); + } + + static inline void TraceStart(const char * traceName) + { + QueryPerformanceCounter(&m_start); + m_traceName = traceName; + } + + static inline void TraceEnd() + { + LARGE_INTEGER now; + QueryPerformanceCounter(&now); + const double diff = (now.QuadPart - m_start.QuadPart) / m_freq; + + DEBUG_INFO("Trace [%8.4f] %s", diff, m_traceName); + } +}; \ No newline at end of file diff --git a/host/looking-glass-host.vcxproj b/host/looking-glass-host.vcxproj index d2599a41..b11541e8 100644 --- a/host/looking-glass-host.vcxproj +++ b/host/looking-glass-host.vcxproj @@ -158,7 +158,7 @@ Level3 Disabled - WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions);DEBUG + ENABLE_TRACING;%(PreprocessorDefinitions) true ..\;.\;%(AdditionalIncludeDirectories) @@ -178,7 +178,7 @@ Level3 Disabled - CONFIG_CAPTURE_NVFBC;%(PreprocessorDefinitions) + ENABLE_TRACING;CONFIG_CAPTURE_NVFBC;%(PreprocessorDefinitions) true ..\;.\;%(AdditionalIncludeDirectories) @@ -198,7 +198,7 @@ Level3 Disabled - _DEBUG;_WINDOWS;%(PreprocessorDefinitions);DEBUG + ENABLE_TRACING;%(PreprocessorDefinitions) true ..\;.\;%(AdditionalIncludeDirectories) @@ -218,7 +218,7 @@ Level3 Disabled - CONFIG_CAPTURE_NVFBC;%(PreprocessorDefinitions) + ENABLE_TRACING;CONFIG_CAPTURE_NVFBC;%(PreprocessorDefinitions) true ..\;.\;%(AdditionalIncludeDirectories) @@ -337,6 +337,7 @@ + @@ -347,6 +348,7 @@ + diff --git a/host/looking-glass-host.vcxproj.filters b/host/looking-glass-host.vcxproj.filters index cfbd4fde..a6816a3d 100644 --- a/host/looking-glass-host.vcxproj.filters +++ b/host/looking-glass-host.vcxproj.filters @@ -45,6 +45,9 @@ Source Files + + Source Files + @@ -74,5 +77,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/host/main.cpp b/host/main.cpp index 105a8871..440afa96 100644 --- a/host/main.cpp +++ b/host/main.cpp @@ -24,6 +24,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "vendor/getopt/getopt.h" #include "CrashHandler.h" +#include "TraceUtil.h" #include "CaptureFactory.h" #include "Service.h" @@ -50,6 +51,8 @@ struct StartupArgs int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdParam, int iCmdShow) { CrashHandler::Initialize(); + TraceUtil::Initialize(); + SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); struct StartupArgs args;