From b7d42c8748fbe2be4822d2a905b3f82912f21437 Mon Sep 17 00:00:00 2001 From: Rokas Kupstys Date: Wed, 28 Jun 2017 21:05:10 +0300 Subject: [PATCH 1/8] easy_profiler third party dependency deleted old profiler code --- Source/Atomic/Core/EventProfiler.cpp | 45 - Source/Atomic/Core/EventProfiler.h | 91 - Source/Atomic/Core/Profiler.cpp | 144 - Source/Atomic/Core/Profiler.h | 260 -- .../ThirdParty/easy_profiler/.gitattributes | 7 + Source/ThirdParty/easy_profiler/.gitignore | 4 + Source/ThirdParty/easy_profiler/.travis.yml | 69 + .../ThirdParty/easy_profiler/CMakeLists.txt | 38 + .../ThirdParty/easy_profiler/LICENSE.APACHE | 177 + Source/ThirdParty/easy_profiler/LICENSE.MIT | 18 + Source/ThirdParty/easy_profiler/README.md | 223 ++ .../easy_profiler_core/CMakeLists.txt | 228 ++ .../easy_profiler_core/LICENSE.APACHE | 177 + .../easy_profiler_core/LICENSE.MIT | 18 + .../easy_profiler_core/block.cpp | 240 ++ .../easy_profiler_core/cmake/config.cmake.in | 4 + .../easy_profiler_core/current_time.h | 174 + .../easy_profiler_core/easy_socket.cpp | 364 +++ .../easy_profiler_core/event_trace_status.h | 27 + .../easy_profiler_core/event_trace_win.cpp | 544 ++++ .../easy_profiler_core/event_trace_win.h | 129 + .../easy_profiler_core/hashed_cstr.h | 298 ++ .../include/easy/easy_compiler_support.h | 156 + .../include/easy/easy_net.h | 149 + .../include/easy/easy_socket.h | 128 + .../include/easy/profiler.h | 966 ++++++ .../include/easy/profiler_aux.h | 203 ++ .../include/easy/profiler_colors.h | 412 +++ .../easy_profiler_core/include/easy/reader.h | 445 +++ .../include/easy/serialized_block.h | 141 + .../easy_profiler_core/outstream.h | 115 + .../easy_profiler_core/profile_manager.cpp | 1953 +++++++++++ .../easy_profiler_core/profile_manager.h | 603 ++++ .../easy_profiler_core/reader.cpp | 1025 ++++++ .../easy_profiler_core/resources.rc | 30 + .../easy_profiler_core/spin_lock.h | 126 + .../easy_profiler/profiler_gui/CMakeLists.txt | 67 + .../profiler_gui/blocks_graphics_view.cpp | 2466 ++++++++++++++ .../profiler_gui/blocks_graphics_view.h | 349 ++ .../profiler_gui/blocks_tree_widget.cpp | 1320 ++++++++ .../profiler_gui/blocks_tree_widget.h | 217 ++ .../easy_profiler/profiler_gui/common_types.h | 454 +++ .../profiler_gui/descriptors_tree_widget.cpp | 919 ++++++ .../profiler_gui/descriptors_tree_widget.h | 211 ++ .../profiler_gui/easy_chronometer_item.cpp | 398 +++ .../profiler_gui/easy_chronometer_item.h | 171 + .../profiler_gui/easy_frame_rate_viewer.cpp | 339 ++ .../profiler_gui/easy_frame_rate_viewer.h | 125 + .../profiler_gui/easy_graphics_item.cpp | 1474 +++++++++ .../profiler_gui/easy_graphics_item.h | 195 ++ .../profiler_gui/easy_graphics_scrollbar.cpp | 2084 ++++++++++++ .../profiler_gui/easy_graphics_scrollbar.h | 342 ++ .../profiler_gui/easy_qtimer.cpp | 85 + .../easy_profiler/profiler_gui/easy_qtimer.h | 89 + .../easy_profiler/profiler_gui/globals.cpp | 115 + .../easy_profiler/profiler_gui/globals.h | 237 ++ .../profiler_gui/globals_qobjects.cpp | 72 + .../profiler_gui/globals_qobjects.h | 94 + .../profiler_gui/icons/attribution.txt | 24 + .../profiler_gui/icons/collapse.svg | 16 + .../profiler_gui/icons/colors-black.svg | 31 + .../profiler_gui/icons/colors.svg | 41 + .../profiler_gui/icons/delete.svg | 10 + .../profiler_gui/icons/expand.svg | 15 + .../easy_profiler/profiler_gui/icons/lan.svg | 25 + .../profiler_gui/icons/lan_on.svg | 25 + .../easy_profiler/profiler_gui/icons/list.svg | 28 + .../easy_profiler/profiler_gui/icons/logo.ico | Bin 0 -> 179025 bytes .../easy_profiler/profiler_gui/icons/logo.svg | 18 + .../easy_profiler/profiler_gui/icons/off.svg | 12 + .../profiler_gui/icons/open-folder.svg | 14 + .../profiler_gui/icons/open-folder2.svg | 16 + .../easy_profiler/profiler_gui/icons/play.svg | 11 + .../profiler_gui/icons/reload-folder2.svg | 24 + .../profiler_gui/icons/reload.svg | 13 + .../easy_profiler/profiler_gui/icons/save.svg | 73 + .../profiler_gui/icons/search-next.svg | 23 + .../profiler_gui/icons/search-prev.svg | 23 + .../profiler_gui/icons/settings.svg | 39 + .../profiler_gui/icons/statistics.svg | 14 + .../profiler_gui/icons/statistics2.svg | 16 + .../easy_profiler/profiler_gui/icons/stop.svg | 11 + .../easy_profiler/profiler_gui/icons/wifi.svg | 18 + .../profiler_gui/icons/wifi_on.svg | 18 + .../easy_profiler/profiler_gui/main.cpp | 94 + .../profiler_gui/main_window.cpp | 2858 +++++++++++++++++ .../easy_profiler/profiler_gui/main_window.h | 320 ++ .../easy_profiler/profiler_gui/resources.qrc | 29 + .../easy_profiler/profiler_gui/resources.rc | 33 + .../profiler_gui/tree_widget_item.cpp | 253 ++ .../profiler_gui/tree_widget_item.h | 162 + .../profiler_gui/tree_widget_loader.cpp | 1003 ++++++ .../profiler_gui/tree_widget_loader.h | 136 + .../easy_profiler/reader/CMakeLists.txt | 3 + .../ThirdParty/easy_profiler/reader/main.cpp | 146 + .../easy_profiler/sample/CMakeLists.txt | 16 + .../easy_profiler/sample/express_sample.cpp | 83 + .../ThirdParty/easy_profiler/sample/main.cpp | 263 ++ .../easy_profiler/sample/main_clock.cpp | 249 ++ .../scripts/context_switch_logger.stp | 37 + .../ThirdParty/easy_profiler/scripts/test.sh | 57 + 101 files changed, 27314 insertions(+), 540 deletions(-) delete mode 100644 Source/Atomic/Core/EventProfiler.cpp delete mode 100644 Source/Atomic/Core/EventProfiler.h delete mode 100644 Source/Atomic/Core/Profiler.cpp delete mode 100644 Source/Atomic/Core/Profiler.h create mode 100644 Source/ThirdParty/easy_profiler/.gitattributes create mode 100644 Source/ThirdParty/easy_profiler/.gitignore create mode 100644 Source/ThirdParty/easy_profiler/.travis.yml create mode 100644 Source/ThirdParty/easy_profiler/CMakeLists.txt create mode 100644 Source/ThirdParty/easy_profiler/LICENSE.APACHE create mode 100644 Source/ThirdParty/easy_profiler/LICENSE.MIT create mode 100644 Source/ThirdParty/easy_profiler/README.md create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/CMakeLists.txt create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/LICENSE.APACHE create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/LICENSE.MIT create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/block.cpp create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/cmake/config.cmake.in create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/current_time.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/easy_socket.cpp create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_status.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_win.cpp create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_win.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/hashed_cstr.h create mode 100755 Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_compiler_support.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_net.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_socket.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler_aux.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler_colors.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/reader.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/serialized_block.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/outstream.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/profile_manager.cpp create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/profile_manager.h create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/reader.cpp create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/resources.rc create mode 100644 Source/ThirdParty/easy_profiler/easy_profiler_core/spin_lock.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/CMakeLists.txt create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/blocks_graphics_view.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/blocks_graphics_view.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/blocks_tree_widget.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/blocks_tree_widget.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/common_types.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/descriptors_tree_widget.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/descriptors_tree_widget.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/easy_chronometer_item.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/easy_chronometer_item.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/easy_frame_rate_viewer.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/easy_frame_rate_viewer.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_item.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_item.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_scrollbar.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_scrollbar.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/easy_qtimer.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/easy_qtimer.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/globals.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/globals.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/globals_qobjects.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/globals_qobjects.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/attribution.txt create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/collapse.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/colors-black.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/colors.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/delete.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/expand.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/lan.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/lan_on.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/list.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/logo.ico create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/logo.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/off.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/open-folder.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/open-folder2.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/play.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/reload-folder2.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/reload.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/save.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/search-next.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/search-prev.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/settings.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/statistics.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/statistics2.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/stop.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/wifi.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/icons/wifi_on.svg create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/main.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/main_window.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/main_window.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/resources.qrc create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/resources.rc create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_item.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_item.h create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_loader.cpp create mode 100644 Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_loader.h create mode 100644 Source/ThirdParty/easy_profiler/reader/CMakeLists.txt create mode 100644 Source/ThirdParty/easy_profiler/reader/main.cpp create mode 100644 Source/ThirdParty/easy_profiler/sample/CMakeLists.txt create mode 100644 Source/ThirdParty/easy_profiler/sample/express_sample.cpp create mode 100644 Source/ThirdParty/easy_profiler/sample/main.cpp create mode 100644 Source/ThirdParty/easy_profiler/sample/main_clock.cpp create mode 100644 Source/ThirdParty/easy_profiler/scripts/context_switch_logger.stp create mode 100644 Source/ThirdParty/easy_profiler/scripts/test.sh diff --git a/Source/Atomic/Core/EventProfiler.cpp b/Source/Atomic/Core/EventProfiler.cpp deleted file mode 100644 index 7b1ac26b5a..0000000000 --- a/Source/Atomic/Core/EventProfiler.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// -// Copyright (c) 2008-2017 the Urho3D project. -// -// 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. -// - -#include "../Precompiled.h" - -#include "../Core/EventProfiler.h" - -#include "../DebugNew.h" - -namespace Atomic -{ - -bool EventProfiler::active = false; - -EventProfiler::EventProfiler(Context* context) : - Profiler(context) -{ - // FIXME: Is there a cleaner way? - delete root_; - current_ = root_ = new EventProfilerBlock(0, "RunFrame"); - delete [] root_->name_; - root_->name_ = new char[sizeof("RunFrame")]; - memcpy(root_->name_, "RunFrame", sizeof("RunFrame")); -} - -} diff --git a/Source/Atomic/Core/EventProfiler.h b/Source/Atomic/Core/EventProfiler.h deleted file mode 100644 index 670256c530..0000000000 --- a/Source/Atomic/Core/EventProfiler.h +++ /dev/null @@ -1,91 +0,0 @@ -// -// Copyright (c) 2008-2017 the Urho3D project. -// -// 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. -// - -#pragma once - -#include "../Core/Profiler.h" - -namespace Atomic -{ - -/// Event profiling data for one block in the event profiling tree. -class ATOMIC_API EventProfilerBlock : public ProfilerBlock -{ -public: - /// Construct with the specified parent block and event ID. - EventProfilerBlock(EventProfilerBlock* parent, StringHash eventID) : - ProfilerBlock(parent, EventNameRegistrar::GetEventName(eventID).CString()), - eventID_(eventID) - { - } - - /// Return child block with the specified event ID. - EventProfilerBlock* GetChild(StringHash eventID) - { - for (PODVector::Iterator i = children_.Begin(); i != children_.End(); ++i) - { - EventProfilerBlock* eventProfilerBlock = static_cast(*i); - if (eventProfilerBlock->eventID_ == eventID) - return eventProfilerBlock; - } - - EventProfilerBlock* newBlock = new EventProfilerBlock(this, eventID); - children_.Push(newBlock); - - return newBlock; - } - - /// Event ID. - StringHash eventID_; -}; - -/// Hierarchical performance event profiler subsystem. -class ATOMIC_API EventProfiler : public Profiler -{ - ATOMIC_OBJECT(EventProfiler, Profiler); - -public: - /// Construct. - EventProfiler(Context* context); - - /// Activate the event profiler to collect information. This incurs slight performance hit on each SendEvent. By default inactive. - static void SetActive(bool newActive) { active = newActive; } - /// Return true if active. - static bool IsActive() { return active; } - - /// Begin timing a profiling block based on an event ID. - void BeginBlock(StringHash eventID) - { - // Profiler supports only the main thread currently - if (!Thread::IsMainThread()) - return; - - current_ = static_cast(current_)->GetChild(eventID); - current_->Begin(); - } - -private: - /// Profiler active. Default false. - static bool active; -}; - -} diff --git a/Source/Atomic/Core/Profiler.cpp b/Source/Atomic/Core/Profiler.cpp deleted file mode 100644 index 711edd917f..0000000000 --- a/Source/Atomic/Core/Profiler.cpp +++ /dev/null @@ -1,144 +0,0 @@ -// -// Copyright (c) 2008-2017 the Urho3D project. -// -// 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. -// - -#include "../Precompiled.h" - -#include "../Core/Profiler.h" - -#include - -#include "../DebugNew.h" - -namespace Atomic -{ - -Profiler::Profiler(Context* context) : - Object(context), - current_(0), - root_(0), - intervalFrames_(0) -{ - current_ = root_ = new ProfilerBlock(0, "RunFrame"); -} - -Profiler::~Profiler() -{ - delete root_; - root_ = 0; -} - -void Profiler::BeginFrame() -{ - // End the previous frame if any - if (root_->count_) - EndFrame(); - - root_->Begin(); -} - -void Profiler::EndFrame() -{ - EndBlock(); - ++intervalFrames_; - root_->EndFrame(); - current_ = root_; -} - -void Profiler::BeginInterval() -{ - root_->BeginInterval(); - intervalFrames_ = 0; -} - -const String& Profiler::PrintData(bool showUnused, bool showTotal, unsigned maxDepth) const -{ - static String output; - - if (!showTotal) - output = "Block Cnt Avg Max Frame Total\n\n"; - else - { - output = "Block Last frame Whole execution time\n\n"; - output += " Cnt Avg Max Total Cnt Avg Max Total\n\n"; - } - - if (!maxDepth) - maxDepth = 1; - - PrintData(root_, output, 0, maxDepth, showUnused, showTotal); - - return output; -} - -void Profiler::PrintData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, - bool showTotal) const -{ - static const int LINE_MAX_LENGTH = 256; - static const int NAME_MAX_LENGTH = 30; - - char line[LINE_MAX_LENGTH]; - char indentedName[LINE_MAX_LENGTH]; - - if (depth >= maxDepth) - return; - - // Do not print any block that does not collect critical data - if (showUnused || block->intervalCount_ || (showTotal && block->totalCount_)) - { - memset(indentedName, ' ', NAME_MAX_LENGTH); - indentedName[depth++] = 0; - strncat(indentedName, block->name_, NAME_MAX_LENGTH - depth); - indentedName[strlen(indentedName)] = ' '; - indentedName[NAME_MAX_LENGTH] = 0; - - if (!showTotal) - { - float avg = block->intervalTime_ / block->intervalCount_ / 1000.0f; - float max = block->intervalMaxTime_ / 1000.0f; - float frame = block->intervalTime_ / (intervalFrames_ ? intervalFrames_ : 1) / 1000.0f; - float all = block->intervalTime_ / 1000.0f; - - sprintf(line, "%s %5u %8.3f %8.3f %8.3f %9.3f\n", indentedName, Min(block->intervalCount_, 99999U), - avg, max, frame, all); - } - else - { - float avg = (block->frameCount_ ? block->frameTime_ / block->frameCount_ : 0.0f) / 1000.0f; - float max = block->frameMaxTime_ / 1000.0f; - float all = block->frameTime_ / 1000.0f; - - float totalAvg = block->totalTime_ / block->totalCount_ / 1000.0f; - float totalMax = block->totalMaxTime_ / 1000.0f; - float totalAll = block->totalTime_ / 1000.0f; - - sprintf(line, "%s %5u %8.3f %8.3f %9.3f %7u %9.3f %9.3f %11.3f\n", indentedName, Min(block->frameCount_, 99999U), - avg, max, all, Min(block->totalCount_, 99999U), totalAvg, totalMax, totalAll); - } - - output += String(line); - } - - for (PODVector::ConstIterator i = block->children_.Begin(); i != block->children_.End(); ++i) - PrintData(*i, output, depth, maxDepth, showUnused, showTotal); -} - -} diff --git a/Source/Atomic/Core/Profiler.h b/Source/Atomic/Core/Profiler.h deleted file mode 100644 index 8c61e847b7..0000000000 --- a/Source/Atomic/Core/Profiler.h +++ /dev/null @@ -1,260 +0,0 @@ -// -// Copyright (c) 2008-2017 the Urho3D project. -// -// 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. -// - -#pragma once - -#include "../Container/Str.h" -#include "../Core/Thread.h" -#include "../Core/Timer.h" - -namespace Atomic -{ - -/// Profiling data for one block in the profiling tree. -class ATOMIC_API ProfilerBlock -{ -public: - /// Construct with the specified parent block and name. - ProfilerBlock(ProfilerBlock* parent, const char* name) : - name_(0), - time_(0), - maxTime_(0), - count_(0), - parent_(parent), - frameTime_(0), - frameMaxTime_(0), - frameCount_(0), - intervalTime_(0), - intervalMaxTime_(0), - intervalCount_(0), - totalTime_(0), - totalMaxTime_(0), - totalCount_(0) - { - if (name) - { - unsigned nameLength = String::CStringLength(name); - name_ = new char[nameLength + 1]; - memcpy(name_, name, nameLength + 1); - } - } - - /// Destruct. Free the child blocks. - virtual ~ProfilerBlock() - { - for (PODVector::Iterator i = children_.Begin(); i != children_.End(); ++i) - { - delete *i; - *i = 0; - } - - delete [] name_; - } - - /// Begin timing. - void Begin() - { - timer_.Reset(); - ++count_; - } - - /// End timing. - void End() - { - long long time = timer_.GetUSec(false); - if (time > maxTime_) - maxTime_ = time; - time_ += time; - } - - /// End profiling frame and update interval and total values. - void EndFrame() - { - frameTime_ = time_; - frameMaxTime_ = maxTime_; - frameCount_ = count_; - intervalTime_ += time_; - if (maxTime_ > intervalMaxTime_) - intervalMaxTime_ = maxTime_; - intervalCount_ += count_; - totalTime_ += time_; - if (maxTime_ > totalMaxTime_) - totalMaxTime_ = maxTime_; - totalCount_ += count_; - time_ = 0; - maxTime_ = 0; - count_ = 0; - - for (PODVector::Iterator i = children_.Begin(); i != children_.End(); ++i) - (*i)->EndFrame(); - } - - /// Begin new profiling interval. - void BeginInterval() - { - intervalTime_ = 0; - intervalMaxTime_ = 0; - intervalCount_ = 0; - - for (PODVector::Iterator i = children_.Begin(); i != children_.End(); ++i) - (*i)->BeginInterval(); - } - - /// Return child block with the specified name. - ProfilerBlock* GetChild(const char* name) - { - for (PODVector::Iterator i = children_.Begin(); i != children_.End(); ++i) - { - if (!String::Compare((*i)->name_, name, true)) - return *i; - } - - ProfilerBlock* newBlock = new ProfilerBlock(this, name); - children_.Push(newBlock); - - return newBlock; - } - - /// Block name. - char* name_; - /// High-resolution timer for measuring the block duration. - HiresTimer timer_; - /// Time on current frame. - long long time_; - /// Maximum time on current frame. - long long maxTime_; - /// Calls on current frame. - unsigned count_; - /// Parent block. - ProfilerBlock* parent_; - /// Child blocks. - PODVector children_; - /// Time on the previous frame. - long long frameTime_; - /// Maximum time on the previous frame. - long long frameMaxTime_; - /// Calls on the previous frame. - unsigned frameCount_; - /// Time during current profiler interval. - long long intervalTime_; - /// Maximum time during current profiler interval. - long long intervalMaxTime_; - /// Calls during current profiler interval. - unsigned intervalCount_; - /// Total accumulated time. - long long totalTime_; - /// All-time maximum time. - long long totalMaxTime_; - /// Total accumulated calls. - unsigned totalCount_; -}; - -/// Hierarchical performance profiler subsystem. -class ATOMIC_API Profiler : public Object -{ - ATOMIC_OBJECT(Profiler, Object); - -public: - /// Construct. - Profiler(Context* context); - /// Destruct. - virtual ~Profiler(); - - /// Begin timing a profiling block. - void BeginBlock(const char* name) - { - // Profiler supports only the main thread currently - if (!Thread::IsMainThread()) - return; - - current_ = current_->GetChild(name); - current_->Begin(); - } - - /// End timing the current profiling block. - void EndBlock() - { - if (!Thread::IsMainThread()) - return; - - current_->End(); - if (current_->parent_) - current_ = current_->parent_; - } - - /// Begin the profiling frame. Called by HandleBeginFrame(). - void BeginFrame(); - /// End the profiling frame. Called by HandleEndFrame(). - void EndFrame(); - /// Begin a new interval. - void BeginInterval(); - - /// Return profiling data as text output. This method is not thread-safe. - const String& PrintData(bool showUnused = false, bool showTotal = false, unsigned maxDepth = M_MAX_UNSIGNED) const; - /// Return the current profiling block. - const ProfilerBlock* GetCurrentBlock() { return current_; } - /// Return the root profiling block. - const ProfilerBlock* GetRootBlock() { return root_; } - -protected: - /// Return profiling data as text output for a specified profiling block. - void PrintData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const; - - /// Current profiling block. - ProfilerBlock* current_; - /// Root profiling block. - ProfilerBlock* root_; - /// Frames in the current interval. - unsigned intervalFrames_; -}; - -/// Helper class for automatically beginning and ending a profiling block -class ATOMIC_API AutoProfileBlock -{ -public: - /// Construct. Begin a profiling block with the specified name and optional call count. - AutoProfileBlock(Profiler* profiler, const char* name) : - profiler_(profiler) - { - if (profiler_) - profiler_->BeginBlock(name); - } - - /// Destruct. End the profiling block. - ~AutoProfileBlock() - { - if (profiler_) - profiler_->EndBlock(); - } - -private: - /// Profiler. - Profiler* profiler_; -}; - -#ifdef ATOMIC_PROFILING -#define ATOMIC_PROFILE(name) Atomic::AutoProfileBlock profile_ ## name (GetSubsystem(), #name) -#else -#define ATOMIC_PROFILE(name) -#endif - -} diff --git a/Source/ThirdParty/easy_profiler/.gitattributes b/Source/ThirdParty/easy_profiler/.gitattributes new file mode 100644 index 0000000000..a34d4501f8 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/.gitattributes @@ -0,0 +1,7 @@ +* text=auto + +*.ico binary + +*.cpp text +*.h text + diff --git a/Source/ThirdParty/easy_profiler/.gitignore b/Source/ThirdParty/easy_profiler/.gitignore new file mode 100644 index 0000000000..e4373d38a0 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/.gitignore @@ -0,0 +1,4 @@ +bin +*build* +.idea +*.user diff --git a/Source/ThirdParty/easy_profiler/.travis.yml b/Source/ThirdParty/easy_profiler/.travis.yml new file mode 100644 index 0000000000..8a6d673ead --- /dev/null +++ b/Source/ThirdParty/easy_profiler/.travis.yml @@ -0,0 +1,69 @@ +sudo: required +dist: trusty +language: cpp +cache: ccache +matrix: + include: + - compiler: gcc + os: linux + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-5 + - cmake + - cmake-data + env: + -CXX_COMPILER=g++-5 + -C_COMPILER=gcc-5 + - os: osx + osx_image: xcode8 + #compiler: clang + env: + #-CXX_COMPILER=clang++ + #-C_COMPILER=clang + -CXX_COMPILER=g++-5 + -C_COMPILER=gcc-5 +script: + - mkdir build + - cd build + - cmake --version + - if [ "$TRAVIS_OS_NAME" = "linux" ]; then + source /opt/qt55/bin/qt55-env.sh; + qmake -v; + else + export PATH=/usr/local/opt/qt/bin:$PATH; + fi + - cmake -DCMAKE_CXX_COMPILER=$CXX_COMPILER -DCMAKE_C_COMPILER=$C_COMPILER .. && make -j3 +after_success: + - cd ../sample && $CXX_COMPILER -std=c++11 -O3 main_clock.cpp -o test_clock && ./test_clock + - ./build_express_test.sh +before_install: + - if [ "$TRAVIS_OS_NAME" = "linux" ]; then + sudo add-apt-repository --yes ppa:beineri/opt-qt551-trusty; + sudo apt-get update -qq; + else + brew update; + fi +install: + - if [ "$TRAVIS_OS_NAME" = "linux" ]; then + sudo apt-get -y install qt55tools qt55script qt55base qt55svg; + else + brew install gcc5; + brew install qt5; + brew link --force qt5; + export HOMEBREW_QT5_VERSION=$(brew list --versions qt5 | rev | cut -d' ' -f1 | rev); + ln -s /usr/local/Cellar/qt/$HOMEBREW_QT5_VERSION/mkspecs /usr/local/mkspecs; + ln -s /usr/local/Cellar/qt/$HOMEBREW_QT5_VERSION/plugins /usr/local/plugins; + echo "ls -l /usr/local/mkspecs"; + ls -l /usr/local/mkspecs; + echo "ls -l /usr/local/plugins"; + ls -l /usr/local/plugins; + echo "/usr/local/Cellar/qt/"; + ls -l /usr/local/Cellar/qt/; + fi + + + + diff --git a/Source/ThirdParty/easy_profiler/CMakeLists.txt b/Source/ThirdParty/easy_profiler/CMakeLists.txt new file mode 100644 index 0000000000..09a0d30868 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 2.8.12.1) +project(easy_profiler CXX) + +set(EASY_PROGRAM_VERSION_MAJOR 1) +set(EASY_PROGRAM_VERSION_MINOR 3) +set(EASY_PROGRAM_VERSION_PATCH 0) +set(EASY_PRODUCT_VERSION_STRING "${EASY_PROGRAM_VERSION_MAJOR}.${EASY_PROGRAM_VERSION_MINOR}.${EASY_PROGRAM_VERSION_PATCH}") + +# ATOMIC BEGIN +set(EASY_OPTION_LIB_STATIC ON CACHE BOOL "" FORCE) +set(EASY_OPTION_PREDEFINED_COLORS ON CACHE BOOL "" FORCE) +set(EASY_PROFILER_NO_SAMPLES ON CACHE BOOL "" FORCE) +if ("${CMAKE_SOURCE_DIR}" STREQUAL "${easy_profiler_SOURCE_DIR}") + # Do not set these paths if easy_profiler is added as dependency to other projects. + set_property(GLOBAL PROPERTY USE_FOLDERS ON) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin) + # set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_LIST_DIR}/sdk) +endif () +# ATOMIC END + +macro(easy_define_target_option TARGET SOURCE_OPTION TARGET_DEFINITION) + if (${SOURCE_OPTION}) + set(_VALUE 1) + else () + set(_VALUE 0) + endif () + target_compile_options(${TARGET} PUBLIC -D${TARGET_DEFINITION}=${_VALUE}) +endmacro() + +add_subdirectory(easy_profiler_core) +add_subdirectory(profiler_gui) + +if (NOT EASY_PROFILER_NO_SAMPLES) + add_subdirectory(sample) + add_subdirectory(reader) +endif () diff --git a/Source/ThirdParty/easy_profiler/LICENSE.APACHE b/Source/ThirdParty/easy_profiler/LICENSE.APACHE new file mode 100644 index 0000000000..f433b1a53f --- /dev/null +++ b/Source/ThirdParty/easy_profiler/LICENSE.APACHE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/Source/ThirdParty/easy_profiler/LICENSE.MIT b/Source/ThirdParty/easy_profiler/LICENSE.MIT new file mode 100644 index 0000000000..384014596d --- /dev/null +++ b/Source/ThirdParty/easy_profiler/LICENSE.MIT @@ -0,0 +1,18 @@ +Copyright (c) 2017 Sergey Yagovtsev, Victor Zarubkin + +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. \ No newline at end of file diff --git a/Source/ThirdParty/easy_profiler/README.md b/Source/ThirdParty/easy_profiler/README.md new file mode 100644 index 0000000000..89d77230ca --- /dev/null +++ b/Source/ThirdParty/easy_profiler/README.md @@ -0,0 +1,223 @@ +# easy_profiler [![1.2.0](https://img.shields.io/badge/version-1.2.0-009688.svg)](https://github.com/yse/easy_profiler/releases) + +[![Build Status](https://travis-ci.org/yse/easy_profiler.svg?branch=develop)](https://travis-ci.org/yse/easy_profiler) + +[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0) + + +1. [About](#about) +2. [Key features](#key-features) +3. [Usage](#usage) + - [Prepare build system](#prepare-build-system) + - [General build system](#general) + - [CMake](#build-with-cmake) + - [Add profiling blocks](#add-profiling-blocks) + - [Collect blocks](#collect-blocks) + - [Collect via network](#collect-via-network) + - [Collect via file](#collect-via-file) + - [Note about context-switch](#note-about-context-switch) +4. [Build](#build) + - [Linux](#linux) + - [Windows](#windows) +5. [License](#license) + +# About +Lightweight cross-platform profiler library for c++ + +You can profile any function in you code. Furthermore this library provide measuring time of any block of code. +For example, information for 12 millions of blocks is using less than 300Mb of memory. +Working profiler slows your application execution for only 1-2%. + +![Block time](https://hsto.org/files/3e4/afe/8b7/3e4afe8b77ac4ad3a6f8c805be4b7f13.png) +_Average overhead per block is about 15ns/block (tested on Intel Core i7-5930K 3.5GHz, Win7)_ + +Disabled profiler will not affect your application execution in any way. You can leave it in your Release build +and enable it at run-time at any moment during application launch to see what is happening at the moment. + +Also the library can capture system's context switch events between threads. Context switch information includes +duration, target thread id, thread owner process id, thread owner process name. + +You can see the results of measuring in simple GUI application which provides full statistics and renders beautiful time-line. + +![GUI screenshot](https://cloud.githubusercontent.com/assets/1775230/24852044/a0b1edd0-1dde-11e7-8736-7052b840ad06.png) +_Profiling CryEngine SDK example_ + +# Key features + +- Extremely low overhead +- Low additional memory usage +- Cross-platform +- Measuring over network +- Capture thread context-switch events +- Fully remove integration via defines +- GUI could be connected to an application which is already profiling (so you can profile initialization of your application) +- Monitor main thread fps at real-time in GUI even if profiling is disabled or draw your own HUD/fps-plot directly in your application using data provided by profiler +- Configurable timer type with CMakeLists or defines + +# Usage + +## Prepare build system + +### General + +First of all you can specify path to include directory which contains `include/profiler` directory and define macro `BUILD_WITH_EASY_PROFILER`. +For linking with easy_profiler you can specify path to library. + +### Build with cmake + +If you are using `cmake` set `CMAKE_PREFIX_PATH` to `lib/cmake/easy_profiler` directory (from [release](https://github.com/yse/easy_profiler/releases) package) and use function `find_package(easy_profiler)` with `target_link_libraries(... easy_profiler)`. Example: + +``` cmake +project(app_for_profiling) + +set(SOURCES + main.cpp +) + +#CMAKE_PREFIX_PATH should be set to /lib/cmake/easy_profiler +find_package(easy_profiler REQUIRED) + +add_executable(app_for_profiling ${SOURCES}) + +target_link_libraries(app_for_profiling easy_profiler) +``` + +## Add profiling blocks + +Example of usage. + +This code snippet will generate block with function name and Magenta color: +```cpp +#include + +void frame() { + EASY_FUNCTION(profiler::colors::Magenta); // Magenta block with name "frame" + prepareRender(); + calculatePhysics(); +} +``` + +To profile any block you may do this as following. +You can specify these blocks also with Google material design colors or just set name of the block +(in this case it will have default color which is `Amber100`): +```cpp +#include + +void foo() { + // some code + EASY_BLOCK("Calculating sum"); // Block with default color + int sum = 0; + for (int i = 0; i < 10; ++i) { + EASY_BLOCK("Addition", profiler::colors::Red); // Scoped red block (no EASY_END_BLOCK needed) + sum += i; + } + EASY_END_BLOCK; // This ends "Calculating sum" block + + EASY_BLOCK("Calculating multiplication", profiler::colors::Blue500); // Blue block + int mul = 1; + for (int i = 1; i < 11; ++i) + mul *= i; + //EASY_END_BLOCK; // This is not needed because all blocks are ended on destructor when closing braces met +} +``` + +You can also use your own colors. easy_profiler is using standard 32-bit ARGB color format. +Example: +```cpp +#include + +void bar() { + EASY_FUNCTION(0xfff080aa); // Function block with custom color + // some code +} +``` +## Collect blocks + +There are two ways to cature blocks + +### Collect via network + +It's most prefered and convenient approach in many case. + +1. Initialize listening by `profiler::startListen()`. It's start new thread to listen on `28077` port the start-capture-signal from gui-application. +2. To stop listening you can call `profiler::stopListen()` function. + +### Collect via file + +1. Enable profiler by `EASY_PROFILER_ENABLE` macro +2. Dump blocks to file in any place you want by `profiler::dumpBlocksToFile("test_profile.prof")` function + +Example: +```cpp +int main() +{ + EASY_PROFILER_ENABLE; + /* do work*/ + profiler::dumpBlocksToFile("test_profile.prof"); +} +``` + +### Note about context-switch + +To capture a thread context-switch event you need: + +- On Windows: run profiling application "as administrator" +- On linux: you can run special `systemtap` script with root privileges as follow (example on Fedora): +```bash +#stap -o /tmp/cs_profiling_info.log scripts/context_switch_logger.stp name APPLICATION_NAME +``` +APPLICATION_NAME - name of profiling application + +# Build + +## Prerequisites + +For core: +* compiler with c++11 support +* cmake 3.0 or higher + +For GUI: +* Qt 5.3.0 or higher + +## Linux + +```bash +$ mkdir build +$ cd build +$ cmake .. +$ make +``` + +## Windows + +If you are using QtCreator IDE you can just open `CMakeLists.txt` file in root directory. +If you are using Visual Studio you can generate solution by cmake generator command. +Examples shows how to generate Win64 solution for Visual Studio 2013. To generate for another version use proper cmake generator (-G "name of generator"). + +### Way 1 +Specify path to cmake scripts in Qt5 dir (usually in lib/cmake subdir) and execute cmake generator command, +for example: +```batch +$ mkdir build +$ cd build +$ cmake -DCMAKE_PREFIX_PATH="C:\Qt\5.3\msvc2013_64\lib\cmake" .. -G "Visual Studio 12 2013 Win64" +``` + +### Way 2 +Create system variable "Qt5Widgets_DIR" and set it's value to "[path-to-Qt5-binaries]\lib\cmake\Qt5Widgets". +For example, "C:\Qt\5.3\msvc2013_64\lib\cmake\Qt5Widgets". +And then run cmake generator as follows: +```batch +$ mkdir build +$ cd build +$ cmake .. -G "Visual Studio 12 2013 Win64" +``` + +# License + +Licensed under either of +- MIT license ([LICENSE.MIT](LICENSE.MIT) or http://opensource.org/licenses/MIT) +- Apache License, Version 2.0, ([LICENSE.APACHE](LICENSE.APACHE) or http://www.apache.org/licenses/LICENSE-2.0) + +at your option. diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/CMakeLists.txt b/Source/ThirdParty/easy_profiler/easy_profiler_core/CMakeLists.txt new file mode 100644 index 0000000000..510eb82faf --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/CMakeLists.txt @@ -0,0 +1,228 @@ +message(STATUS "") +message(STATUS "EASY_PROFILER.Core version = ${EASY_PRODUCT_VERSION_STRING}") +message(STATUS "") + +set(EASY_DEFAULT_PORT 28077 CACHE STRING "Default listening port") +set(EASY_OPTION_LISTEN OFF CACHE BOOL "Enable automatic startListen on startup") +set(EASY_OPTION_PROFILE_SELF OFF CACHE BOOL "Enable self profiling (measure time for internal storage expand)") +set(EASY_OPTION_PROFILE_SELF_BLOCKS_ON OFF CACHE BOOL "Storage expand default status (profiler::ON or profiler::OFF)") +set(EASY_OPTION_LOG OFF CACHE BOOL "Print errors to stderr") +set(EASY_OPTION_PREDEFINED_COLORS ON CACHE BOOL "Use predefined set of colors (see profiler_colors.h). If you want to use your own colors palette you can turn this option OFF") +set(EASY_OPTION_LIB_STATIC OFF CACHE BOOL "Build easy_profiler as static library.") +if (WIN32) + set(EASY_OPTION_EVENT_TRACING ON CACHE BOOL "Enable event tracing by default") + set(EASY_OPTION_LOW_PRIORITY_EVENT_TRACING ON CACHE BOOL "Set low priority for event tracing thread") +endif (WIN32) +set(BUILD_WITH_CHRONO_STEADY_CLOCK OFF CACHE BOOL "Use std::chrono::steady_clock as a timer" ) +set(BUILD_WITH_CHRONO_HIGH_RESOLUTION_CLOCK OFF CACHE BOOL "Use std::chrono::high_resolution_clock as a timer") + +message(STATUS "-------- EASY_PROFILER OPTIONS: --------") +if (BUILD_WITH_CHRONO_STEADY_CLOCK) + message(STATUS " Use std::chrono::steady_clock as a timer") +elseif (BUILD_WITH_CHRONO_HIGH_RESOLUTION_CLOCK) + message(STATUS " Use std::chrono::high_resolution_clock as a timer") +else () + if (WIN32) + message(STATUS " Use QueryPerformanceCounter as a timer") + else () + message(STATUS " Use rtdsc as a timer") + endif () +endif () + +message(STATUS " Default listening port = ${EASY_DEFAULT_PORT}") +message(STATUS " Auto-start listening = ${EASY_OPTION_LISTEN}") +message(STATUS " Profile self = ${EASY_OPTION_PROFILE_SELF}") +message(STATUS " Profile self blocks initial status = ${EASY_OPTION_PROFILE_SELF_BLOCKS_ON}") +if (WIN32) + message(STATUS " Event tracing = ${EASY_OPTION_EVENT_TRACING}") + if (EASY_OPTION_LOW_PRIORITY_EVENT_TRACING) + message(STATUS " Event tracing has low priority = Yes") + else () + message(STATUS " Event tracing has low priority = No") + endif (EASY_OPTION_LOW_PRIORITY_EVENT_TRACING) +endif (WIN32) +message(STATUS " Log messages = ${EASY_OPTION_LOG}") +message(STATUS " Use EasyProfiler colors palette = ${EASY_OPTION_PREDEFINED_COLORS}") +if (EASY_OPTION_LIB_STATIC) + set(EASY_OPTION_LIB_TYPE STATIC) +else () + set(EASY_OPTION_LIB_TYPE SHARED) +endif () +message(STATUS " Library type: ${EASY_OPTION_LIB_TYPE}") +message(STATUS "------ END EASY_PROFILER OPTIONS -------") +message(STATUS "") + +set(CPP_FILES + block.cpp + profile_manager.cpp + reader.cpp + event_trace_win.cpp + easy_socket.cpp +) + +set(H_FILES + profile_manager.h + spin_lock.h + event_trace_win.h + current_time.h +) + +set(INCLUDE_FILES + include/easy/profiler.h + include/easy/reader.h + include/easy/easy_net.h + include/easy/easy_socket.h + include/easy/easy_compiler_support.h + include/easy/profiler_aux.h + include/easy/profiler_colors.h + include/easy/reader.h + include/easy/serialized_block.h +) + +source_group(include FILES ${INCLUDE_FILES}) + +set(SOURCES + ${CPP_FILES} + ${H_FILES} + ${INCLUDE_FILES} +) + +add_library(easy_profiler ${EASY_OPTION_LIB_TYPE} ${SOURCES} resources.rc) +target_include_directories(easy_profiler PUBLIC + $ + $ # /include +) +target_compile_definitions(easy_profiler PRIVATE + -D_BUILD_PROFILER=1 + #-DEASY_PROFILER_API_DISABLED # uncomment this to disable profiler api only (you will have to rebuild only easy_profiler) +) +if (EASY_OPTION_LIB_STATIC) + target_compile_definitions(easy_profiler PUBLIC -DEASY_PROFILER_STATIC=1) +endif () +target_compile_definitions(easy_profiler PUBLIC + -DEASY_PROFILER_VERSION_MAJOR=${EASY_PROGRAM_VERSION_MAJOR} + -DEASY_PROFILER_VERSION_MINOR=${EASY_PROGRAM_VERSION_MINOR} + -DEASY_PROFILER_VERSION_PATCH=${EASY_PROGRAM_VERSION_PATCH} + -DEASY_DEFAULT_PORT=${EASY_DEFAULT_PORT} + -DBUILD_WITH_EASY_PROFILER=1 +) + +easy_define_target_option(easy_profiler BUILD_WITH_CHRONO_STEADY_CLOCK EASY_CHRONO_STEADY_CLOCK) +easy_define_target_option(easy_profiler BUILD_WITH_CHRONO_HIGH_RESOLUTION_CLOCK EASY_CHRONO_HIGHRES_CLOCK) +easy_define_target_option(easy_profiler EASY_OPTION_LISTEN EASY_OPTION_START_LISTEN_ON_STARTUP) +easy_define_target_option(easy_profiler EASY_OPTION_PROFILE_SELF EASY_OPTION_MEASURE_STORAGE_EXPAND) +easy_define_target_option(easy_profiler EASY_OPTION_PROFILE_SELF_BLOCKS_ON EASY_OPTION_STORAGE_EXPAND_BLOCKS_ON) +if (WIN32) + easy_define_target_option(easy_profiler EASY_OPTION_EVENT_TRACING EASY_OPTION_EVENT_TRACING_ENABLED) + easy_define_target_option(easy_profiler EASY_OPTION_LOW_PRIORITY_EVENT_TRACING EASY_OPTION_LOW_PRIORITY_EVENT_TRACING) +endif () +easy_define_target_option(easy_profiler EASY_OPTION_LOG EASY_OPTION_LOG_ENABLED) +easy_define_target_option(easy_profiler EASY_OPTION_PREDEFINED_COLORS EASY_OPTION_BUILTIN_COLORS) + +if (UNIX) + target_compile_options(easy_profiler PRIVATE -Wall -Wno-long-long -Wno-reorder -Wno-braced-scalar-init -pedantic -O3) + target_link_libraries(easy_profiler pthread) +elseif (WIN32) + target_compile_definitions(easy_profiler PRIVATE -D_WIN32_WINNT=0x0600 -D_CRT_SECURE_NO_WARNINGS -D_WINSOCK_DEPRECATED_NO_WARNINGS) + target_link_libraries(easy_profiler ws2_32 psapi) +endif () + +if (MINGW) + target_compile_definitions(easy_profiler PRIVATE -DSTRSAFE_NO_DEPRECATE) +endif () + +if (APPLE) + target_compile_options(easy_profiler PUBLIC -std=gnu++11) +else () + + + if (CMAKE_VERSION VERSION_LESS "3.1") + if (NOT MSVC) + target_compile_options(easy_profiler PUBLIC $<$:-std=gnu++11>) + endif () + else() + + if (NOT MSVC) + target_compile_options(easy_profiler PUBLIC -std=gnu++11) + endif () + + set_target_properties(easy_profiler PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON) + endif () +endif () + +# ATOMIC BEGIN +if (0) +#### +# Installation +set(config_install_dir "lib/cmake/${PROJECT_NAME}") +set(include_install_dir "include") + +set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated") + +# Configuration +set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake") +set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake") +set(targets_export_name "${PROJECT_NAME}Targets") + +include(CMakePackageConfigHelpers) +include(InstallRequiredSystemLibraries) + +write_basic_package_version_file( + "${version_config}" + VERSION + ${EASY_PRODUCT_VERSION_STRING} + COMPATIBILITY + SameMajorVersion +) + +configure_package_config_file( + "cmake/config.cmake.in" + "${project_config}" + INSTALL_DESTINATION "${config_install_dir}" +) + +install( + FILES "${project_config}" "${version_config}" + DESTINATION "${config_install_dir}" +) + +install( + FILES + ${INCLUDE_FILES} + DESTINATION + include/easy +) + +install( + FILES + LICENSE.MIT + LICENSE.APACHE + DESTINATION + . +) + +install( + TARGETS + easy_profiler + EXPORT + ${targets_export_name} + DESTINATION + bin + INCLUDES DESTINATION "${include_install_dir}" +) + +install( + EXPORT "${targets_export_name}" + DESTINATION "${config_install_dir}" +) +endif () + +if (MSVC) + add_library(easy_profiler_md ${EASY_OPTION_LIB_TYPE} ${SOURCES} resources.rc) + foreach(prop COMPILE_DEFINITIONS COMPILE_OPTIONS CXX_STANDARD CXX_STANDARD_REQUIRED INCLUDE_DIRECTORIES INTERFACE_COMPILE_DEFINITIONS INTERFACE_COMPILE_OPTIONS INTERFACE_INCLUDE_DIRECTORIES INTERFACE_LINK_LIBRARIES) + get_target_property(val easy_profiler ${prop}) + set_target_properties(easy_profiler_md PROPERTIES ${prop} "${val}") + endforeach() + target_compile_options(easy_profiler_md PUBLIC $<$:/MDd> $<$>:/MD>) +endif () +# ATOMIC END diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/LICENSE.APACHE b/Source/ThirdParty/easy_profiler/easy_profiler_core/LICENSE.APACHE new file mode 100644 index 0000000000..f433b1a53f --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/LICENSE.APACHE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/LICENSE.MIT b/Source/ThirdParty/easy_profiler/easy_profiler_core/LICENSE.MIT new file mode 100644 index 0000000000..384014596d --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/LICENSE.MIT @@ -0,0 +1,18 @@ +Copyright (c) 2017 Sergey Yagovtsev, Victor Zarubkin + +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. \ No newline at end of file diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/block.cpp b/Source/ThirdParty/easy_profiler/easy_profiler_core/block.cpp new file mode 100644 index 0000000000..c399913ce7 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/block.cpp @@ -0,0 +1,240 @@ +/************************************************************************ +* file name : block.cpp +* ----------------- : +* creation time : 2016/02/16 +* authors : Sergey Yagovtsev, Victor Zarubkin +* emails : yse.sey@gmail.com, v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of profiling blocks +* : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include "profile_manager.h" +#include "current_time.h" + +using namespace profiler; + +#ifndef EASY_PROFILER_API_DISABLED +Event::Event(timestamp_t _begin_time) : m_begin(_begin_time), m_end(0) +{ + +} + +Event::Event(timestamp_t _begin_time, timestamp_t _end_time) : m_begin(_begin_time), m_end(_end_time) +{ + +} + +BaseBlockData::BaseBlockData(timestamp_t _begin_time, block_id_t _descriptor_id) + : Event(_begin_time) + , m_id(_descriptor_id) +{ + +} + +BaseBlockData::BaseBlockData(timestamp_t _begin_time, timestamp_t _end_time, block_id_t _descriptor_id) + : Event(_begin_time, _end_time) + , m_id(_descriptor_id) +{ + +} + +Block::Block(Block&& that) + : BaseBlockData(that.m_begin, that.m_id) + , m_name(that.m_name) + , m_status(that.m_status) + , m_isScoped(that.m_isScoped) +{ + m_end = that.m_end; + that.m_end = that.m_begin; +} + +Block::Block(timestamp_t _begin_time, block_id_t _descriptor_id, const char* _runtimeName) + : BaseBlockData(_begin_time, _descriptor_id) + , m_name(_runtimeName) + , m_status(::profiler::ON) + , m_isScoped(true) +{ + +} + +Block::Block(timestamp_t _begin_time, timestamp_t _end_time, block_id_t _descriptor_id, const char* _runtimeName) + : BaseBlockData(_begin_time, _end_time, _descriptor_id) + , m_name(_runtimeName) + , m_status(::profiler::ON) + , m_isScoped(true) +{ + +} + +Block::Block(const BaseBlockDescriptor* _descriptor, const char* _runtimeName, bool _scoped) + : BaseBlockData(1ULL, _descriptor->id()) + , m_name(_runtimeName) + , m_status(_descriptor->status()) + , m_isScoped(_scoped) +{ + +} + +void Block::start() +{ + m_begin = getCurrentTime(); +} + +void Block::start(timestamp_t _time) +{ + m_begin = _time; +} + +void Block::finish() +{ + m_end = getCurrentTime(); +} + +void Block::finish(timestamp_t _time) +{ + m_end = _time; +} + +Block::~Block() +{ + if (!finished()) + ::profiler::endBlock(); +} +#else +Event::Event(timestamp_t) : m_begin(0), m_end(0) +{ + +} + +Event::Event(timestamp_t, timestamp_t) : m_begin(0), m_end(0) +{ + +} + +BaseBlockData::BaseBlockData(timestamp_t, block_id_t) + : Event(0, 0) + , m_id(~0U) +{ + +} + +BaseBlockData::BaseBlockData(timestamp_t, timestamp_t, block_id_t) + : Event(0, 0) + , m_id(~0U) +{ + +} + +Block::Block(Block&& that) + : BaseBlockData(0, ~0U) + , m_name("") + , m_status(::profiler::OFF) + , m_isScoped(that.m_isScoped) +{ +} + +Block::Block(timestamp_t, block_id_t, const char*) + : BaseBlockData(0, ~0U) + , m_name("") + , m_status(::profiler::OFF) + , m_isScoped(true) +{ + +} + +Block::Block(timestamp_t, timestamp_t, block_id_t, const char*) + : BaseBlockData(0, ~0U) + , m_name("") + , m_status(::profiler::OFF) + , m_isScoped(true) +{ + +} + +Block::Block(const BaseBlockDescriptor*, const char*, bool _scoped) + : BaseBlockData(0, ~0U) + , m_name("") + , m_status(::profiler::OFF) + , m_isScoped(_scoped) +{ + +} + +void Block::start() +{ +} + +void Block::start(timestamp_t) +{ +} + +void Block::finish() +{ +} + +void Block::finish(timestamp_t) +{ +} + +Block::~Block() +{ +} +#endif + +////////////////////////////////////////////////////////////////////////// + +CSwitchEvent::CSwitchEvent(timestamp_t _begin_time, thread_id_t _tid) + : Event(_begin_time) + , m_thread_id(_tid) +{ + +} + +CSwitchBlock::CSwitchBlock(timestamp_t _begin_time, thread_id_t _tid, const char* _runtimeName) + : CSwitchEvent(_begin_time, _tid) + , m_name(_runtimeName) +{ + +} + +////////////////////////////////////////////////////////////////////////// diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/cmake/config.cmake.in b/Source/ThirdParty/easy_profiler/easy_profiler_core/cmake/config.cmake.in new file mode 100644 index 0000000000..9b4c9ee03e --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/cmake/config.cmake.in @@ -0,0 +1,4 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake") +check_required_components("@PROJECT_NAME@") diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/current_time.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/current_time.h new file mode 100644 index 0000000000..c4e7422941 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/current_time.h @@ -0,0 +1,174 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin + +Licensed under either of + * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +at your option. + +The MIT License + 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. + + +The Apache License, Version 2.0 (the "License"); + You may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +**/ + +#ifndef EASY_PROFILER_CURRENT_TIME_H +#define EASY_PROFILER_CURRENT_TIME_H + +#include + +#if defined(_WIN32) && defined(_MSC_VER) && _MSC_VER <= 1800 +// std::chrono for MSVC2013 is broken - it has very low resolution of 16ms +// restrict usage of std::chrono +# if EASY_CHRONO_HIGHRES_CLOCK +# undef EASY_CHRONO_HIGHRES_CLOCK +# endif +# if EASY_CHRONO_STEADY_CLOCK +# undef EASY_CHRONO_STEADY_CLOCK +# endif +#endif + +#if EASY_CHRONO_HIGHRES_CLOCK +# include +# define EASY_CHRONO_CLOCK std::chrono::high_resolution_clock +#elif EASY_CHRONO_STEADY_CLOCK +# include +# define EASY_CHRONO_CLOCK std::chrono::steady_clock +#elif defined(_WIN32) +# include +#else +# include +# include +# ifdef __ARM_ARCH +# include +# endif//__ARM_ARCH +#endif + +static inline profiler::timestamp_t getCurrentTime() +{ +#if EASY_CHRONO_HIGHRES_CLOCK || EASY_CHRONO_STEADY_CLOCK + return (profiler::timestamp_t)EASY_CHRONO_CLOCK::now().time_since_epoch().count(); +#elif defined(_WIN32) + //see https://msdn.microsoft.com/library/windows/desktop/dn553408(v=vs.85).aspx + LARGE_INTEGER elapsedMicroseconds; + if (!QueryPerformanceCounter(&elapsedMicroseconds)) + return 0; + return (profiler::timestamp_t)elapsedMicroseconds.QuadPart; +#else// not _WIN32 + +#if (defined(__GNUC__) || defined(__ICC)) + + // part of code from google/benchmark library (Licensed under the Apache License, Version 2.0) + // see https://github.com/google/benchmark/blob/master/src/cycleclock.h#L111 + #if defined(__i386__) + int64_t ret; + __asm__ volatile("rdtsc" : "=A"(ret)); + return ret; + #elif defined(__x86_64__) || defined(__amd64__) + uint64_t low, high; + __asm__ volatile("rdtsc" : "=a"(low), "=d"(high)); + return (high << 32) | low; + #elif defined(__powerpc__) || defined(__ppc__) + // This returns a time-base, which is not always precisely a cycle-count. + int64_t tbl, tbu0, tbu1; + asm("mftbu %0" : "=r"(tbu0)); + asm("mftb %0" : "=r"(tbl)); + asm("mftbu %0" : "=r"(tbu1)); + tbl &= -static_cast(tbu0 == tbu1); + // high 32 bits in tbu1; low 32 bits in tbl (tbu0 is garbage) + return (tbu1 << 32) | tbl; + #elif defined(__sparc__) + int64_t tick; + asm(".byte 0x83, 0x41, 0x00, 0x00"); + asm("mov %%g1, %0" : "=r"(tick)); + return tick; + #elif defined(__ia64__) + int64_t itc; + asm("mov %0 = ar.itc" : "=r"(itc)); + return itc; + #elif defined(COMPILER_MSVC) && defined(_M_IX86) + // Older MSVC compilers (like 7.x) don't seem to support the + // __rdtsc intrinsic properly, so I prefer to use _asm instead + // when I know it will work. Otherwise, I'll use __rdtsc and hope + // the code is being compiled with a non-ancient compiler. + _asm rdtsc + #elif defined(COMPILER_MSVC) + return __rdtsc(); + #elif defined(__aarch64__) + // System timer of ARMv8 runs at a different frequency than the CPU's. + // The frequency is fixed, typically in the range 1-50MHz. It can be + // read at CNTFRQ special register. We assume the OS has set up + // the virtual timer properly. + int64_t virtual_timer_value; + asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value)); + return virtual_timer_value; + #elif defined(__ARM_ARCH) + #if (__ARM_ARCH >= 6) // V6 is the earliest arch that has a standard cyclecount + uint32_t pmccntr; + uint32_t pmuseren; + uint32_t pmcntenset; + // Read the user mode perf monitor counter access permissions. + asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r"(pmuseren)); + if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. + asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r"(pmcntenset)); + if (pmcntenset & 0x80000000ul) { // Is it counting? + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(pmccntr)); + // The counter is set up to count every 64th cycle + return static_cast(pmccntr) * 64; // Should optimize to << 6 + } + } + #endif + struct timeval tv; + gettimeofday(&tv, nullptr); + return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; + #elif defined(__mips__) + // mips apparently only allows rdtsc for superusers, so we fall + // back to gettimeofday. It's possible clock_gettime would be better. + struct timeval tv; + gettimeofday(&tv, nullptr); + return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; + #else + #warning You need to define fast getCurrentTime() for your OS and CPU + return std::chrono::high_resolution_clock::now().time_since_epoch().count(); + #define EASY_CHRONO_CLOCK std::chrono::high_resolution_clock + #endif + +#else // not _WIN32, __GNUC__, __ICC + #warning You need to define fast getCurrentTime() for your OS and CPU + return std::chrono::high_resolution_clock::now().time_since_epoch().count(); + #define EASY_CHRONO_CLOCK std::chrono::high_resolution_clock +#endif + +#endif +} + + +#endif // EASY_PROFILER_CURRENT_TIME_H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/easy_socket.cpp b/Source/ThirdParty/easy_profiler/easy_profiler_core/easy_socket.cpp new file mode 100644 index 0000000000..f19b685bdf --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/easy_socket.cpp @@ -0,0 +1,364 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin + +Licensed under either of +* MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +at your option. + +The MIT License +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. + + +The Apache License, Version 2.0 (the "License"); +You may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +**/ + +#include + +#include +#include + +#ifdef _WIN32 +#pragma comment (lib, "Ws2_32.lib") +#pragma comment (lib, "Mswsock.lib") +#pragma comment (lib, "AdvApi32.lib") +#else +#include +#include +#endif + +bool EasySocket::checkSocket(socket_t s) const +{ + return s > 0; +} + +int EasySocket::_close(EasySocket::socket_t s) +{ +#ifdef _WIN32 + return ::closesocket(s); +#else + return ::close(s); +#endif +} + +void EasySocket::setBlocking(EasySocket::socket_t s, bool blocking) +{ + +#ifdef _WIN32 + u_long iMode = blocking ? 0 : 1;//0 - blocking, 1 - non blocking + ioctlsocket(s, FIONBIO, &iMode); +#else + const int iMode = blocking ? 0 : 1;//0 - blocking, 1 - non blocking + ioctl(s, FIONBIO, (char *)&iMode); +#endif +} + +int EasySocket::bind(uint16_t portno) +{ + if (!checkSocket(m_socket)) return -1; + + struct sockaddr_in serv_addr; + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons(portno); + auto res = ::bind(m_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); + return res; +} + +void EasySocket::flush() +{ + if (m_socket){ + _close(m_socket); + } + if (m_replySocket != m_socket){ + _close(m_replySocket); + } +#ifdef _WIN32 + m_socket = 0; + m_replySocket = 0; +#else + wsaret = 0; +#endif +} + +void EasySocket::checkResult(int result) +{ + //printf("Errno: %s\n", strerror(errno)); + if(result >= 0){ + m_state = CONNECTION_STATE_SUCCESS; + return; + }else if(result == -1){ + + + int error_code = 0; + +#ifdef _WIN32 + error_code = WSAGetLastError(); + const int CONNECTION_ABORTED = WSAECONNABORTED; + const int CONNECTION_RESET = WSAECONNRESET; + const int CONNECTION_IN_PROGRESS = WSAEINPROGRESS; +#else + error_code = errno; + const int CONNECTION_ABORTED = ECONNABORTED; + const int CONNECTION_RESET = ECONNRESET; + const int CONNECTION_IN_PROGRESS = EINPROGRESS; + const int CONNECTION_BROKEN_PIPE = EPIPE; + const int CONNECTION_ENOENT = ENOENT; +#endif + + switch(error_code) + { + case CONNECTION_ABORTED: + case CONNECTION_RESET: +#ifndef _WIN32 + case CONNECTION_BROKEN_PIPE: + case CONNECTION_ENOENT: +#endif + m_state = CONNECTION_STATE_DISCONNECTED; + break; + case CONNECTION_IN_PROGRESS: + m_state = CONNECTION_STATE_IN_PROGRESS; + break; + default: + break; + } + } +} + +void EasySocket::init() +{ + if (wsaret == 0) + { + int protocol = 0; +#ifdef _WIN32 + protocol = IPPROTO_TCP; +#endif + m_socket = socket(AF_INET, SOCK_STREAM, protocol); + if (!checkSocket(m_socket)) { + return; + } + }else + return; + + setBlocking(m_socket,true); +#ifndef _WIN32 + wsaret = 1; +#endif + int opt = 1; + setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt)); +} + +EasySocket::EasySocket() +{ +#ifdef _WIN32 + WSADATA wsaData; + wsaret = WSAStartup(0x101, &wsaData); +#else + wsaret = 0; +#endif + init(); +#ifndef _WIN32 + wsaret = 1; +#endif +} + +EasySocket::~EasySocket() +{ + flush(); +#ifdef _WIN32 + if (wsaret == 0) + WSACleanup(); +#endif +} + +int EasySocket::send(const void *buf, size_t nbyte) +{ + if(!checkSocket(m_replySocket)) return -1; + int res = 0; +#if defined(_WIN32) || defined(__APPLE__) + res = ::send(m_replySocket, (const char*)buf, (int)nbyte, 0); +#else + res = ::send(m_replySocket,buf,nbyte,MSG_NOSIGNAL); +#endif + checkResult(res); + return res; +} + +int EasySocket::receive(void *buf, size_t nbyte) +{ + if(!checkSocket(m_replySocket)) return -1; + int res = 0; +#ifdef _WIN32 + res = ::recv(m_replySocket, (char*)buf, (int)nbyte, 0); +#else + res = ::read(m_replySocket,buf,nbyte); +#endif + + checkResult(res); + if (res == 0){ + m_state = CONNECTION_STATE_DISCONNECTED; + } + return res; +} + +int EasySocket::listen(int count) +{ + if(!checkSocket(m_socket)) return -1; + int res = ::listen(m_socket,count); + checkResult(res); + return res; +} + +int EasySocket::accept() +{ + if(!checkSocket(m_socket)) return -1; + + fd_set fdread, fdwrite, fdexcl; + timeval tv = { 0 }; + FD_ZERO (&fdread); + FD_SET (m_socket, &fdread); + fdwrite = fdread; + fdexcl = fdread; + tv.tv_sec = 0; tv.tv_usec = 500; + + int rc =select (m_socket+1, &fdread, &fdwrite, &fdexcl, &tv); + + if(rc <= 0){ + //there is no connection for accept + return -1; + } + m_replySocket = ::accept(m_socket,nullptr,nullptr); + + checkResult((int)m_replySocket); + if(checkSocket(m_replySocket)) + { + int send_buffer = 64*1024*1024; + int send_buffer_sizeof = sizeof(int); + setsockopt(m_replySocket, SOL_SOCKET, SO_SNDBUF, (char*)&send_buffer, send_buffer_sizeof); + + //int flag = 1; + //int result = setsockopt(m_replySocket,IPPROTO_TCP,TCP_NODELAY,(char *)&flag,sizeof(int)); + + // Apple doesn't have MSG_NOSIGNAL, work around it +#ifdef __APPLE__ + int value = 1; + setsockopt(m_replySocket, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value)); +#endif + + //setBlocking(m_replySocket,true); + } + return (int)m_replySocket; +} + +bool EasySocket::setAddress(const char *serv, uint16_t portno) +{ + server = gethostbyname(serv); + if (server == NULL) { + return false; + //fprintf(stderr,"ERROR, no such host\n"); + } + memset((char *)&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length); + + serv_addr.sin_port = htons(portno); + + return true; +} + +int EasySocket::connect() +{ + if (server == NULL || m_socket <=0 ) { + return -1; + //fprintf(stderr,"ERROR, no such host\n"); + } + int res = 0; + //TODO: more intelligence +#ifndef _WIN32 + setBlocking(m_socket,false); + + int counter = 0; + int sleepMs = 20; + int waitSec = 1; + int waitMs = waitSec*1000/sleepMs; + + while(counter++ < waitMs) + { + res = ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr)); + + // on Apple, treat EISCONN error as success +#ifdef __APPLE__ + if (res == -1 && errno == EISCONN) + { + res = 0; + break; + } +#endif + + checkResult(res); + + if (res == 0) + break; + + if (m_state == CONNECTION_STATE_IN_PROGRESS) + { + std::this_thread::sleep_for(std::chrono::milliseconds(sleepMs)); + continue; + } + + + if(m_state != CONNECTION_STATE_IN_PROGRESS && m_state != CONNECTION_STATE_SUCCESS ) + break; + } + + setBlocking(m_socket,true); +#else + res = ::connect(m_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); + checkResult(res); +#endif + if(res == 0){ + + struct timeval tv; + + tv.tv_sec = 1; + tv.tv_usec = 0; + + setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); + +#ifdef __APPLE__ + // Apple doesn't have MSG_NOSIGNAL, work around it + int value = 1; + setsockopt(m_socket, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value)); +#endif + + m_replySocket = m_socket; + } + return res; +} diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_status.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_status.h new file mode 100644 index 0000000000..12c4476195 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_status.h @@ -0,0 +1,27 @@ + + + +#ifndef EASY_PROFILER__EVENT_TRACE_STATUS__H_ +#define EASY_PROFILER__EVENT_TRACE_STATUS__H_ + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +namespace profiler { + + enum EventTracingEnableStatus : unsigned char + { + EVENT_TRACING_LAUNCHED_SUCCESSFULLY = 0, + EVENT_TRACING_NOT_ENOUGH_ACCESS_RIGHTS, + EVENT_TRACING_WAS_LAUNCHED_BY_SOMEBODY_ELSE, + EVENT_TRACING_BAD_PROPERTIES_SIZE, + EVENT_TRACING_OPEN_TRACE_ERROR, + EVENT_TRACING_MISTERIOUS_ERROR, + }; + +} // END of namespace profiler. + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_PROFILER__EVENT_TRACE_STATUS__H_ diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_win.cpp b/Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_win.cpp new file mode 100644 index 0000000000..53cd463bd9 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_win.cpp @@ -0,0 +1,544 @@ +/************************************************************************ +* file name : event_trace_win.cpp +* ----------------- : +* creation time : 2016/09/04 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of EasyEventTracer class used for tracing +* : Windows system events to get context switches. +* ----------------- : +* change log : * 2016/09/04 Victor Zarubkin: initial commit. +* : +* : * 2016/09/13 Victor Zarubkin: get process id and process name +* : of the owner of thread with id == CSwitch::NewThreadId. +* : +* : * 2016/09/17 Victor Zarubkin: added log messages printing. +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifdef _WIN32 +#include +#include +#include +#include +#include "profile_manager.h" +#include "current_time.h" + +#include "event_trace_win.h" +#include + +#ifdef __MINGW32__ +#include +#endif + +//#include + +#if EASY_OPTION_LOG_ENABLED != 0 +# include + +# ifndef EASY_ERRORLOG +# define EASY_ERRORLOG ::std::cerr +# endif + +# ifndef EASY_LOG +# define EASY_LOG ::std::cerr +# endif + +# ifndef EASY_ERROR +# define EASY_ERROR(LOG_MSG) EASY_ERRORLOG << "EasyProfiler ERROR: " << LOG_MSG +# endif + +# ifndef EASY_WARNING +# define EASY_WARNING(LOG_MSG) EASY_ERRORLOG << "EasyProfiler WARNING: " << LOG_MSG +# endif + +# ifndef EASY_LOGMSG +# define EASY_LOGMSG(LOG_MSG) EASY_LOG << "EasyProfiler INFO: " << LOG_MSG +# endif + +# ifndef EASY_LOG_ONLY +# define EASY_LOG_ONLY(CODE) CODE +# endif + +#else + +# ifndef EASY_ERROR +# define EASY_ERROR(LOG_MSG) +# endif + +# ifndef EASY_WARNING +# define EASY_WARNING(LOG_MSG) +# endif + +# ifndef EASY_LOGMSG +# define EASY_LOGMSG(LOG_MSG) +# endif + +# ifndef EASY_LOG_ONLY +# define EASY_LOG_ONLY(CODE) +# endif + +#endif + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +//extern ProfileManager& MANAGER; +#define MANAGER ProfileManager::instance() + +extern const ::profiler::color_t EASY_COLOR_INTERNAL_EVENT; + +#ifdef __MINGW32__ +::std::atomic TRACING_END_TIME = ATOMIC_VAR_INIT(~0ULL); +char KERNEL_LOGGER[] = KERNEL_LOGGER_NAME; +#else +::std::atomic_uint64_t TRACING_END_TIME = ATOMIC_VAR_INIT(~0ULL); +#endif + +namespace profiler { + + const decltype(EVENT_DESCRIPTOR::Opcode) SWITCH_CONTEXT_OPCODE = 36; + const int RAW_TIMESTAMP_TIME_TYPE = 1; + + ////////////////////////////////////////////////////////////////////////// + + struct ProcessInfo { + std::string name; + processid_t id = 0; + int8_t valid = 0; + }; + + ////////////////////////////////////////////////////////////////////////// + + // CSwitch class + // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa964744(v=vs.85).aspx + // EventType = 36 + struct CSwitch + { + 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; + }; + + ////////////////////////////////////////////////////////////////////////// + + typedef ::std::unordered_map thread_process_info_map; + typedef ::std::unordered_map process_info_map; + + // Using static is safe because processTraceEvent() is called from one thread + process_info_map PROCESS_INFO_TABLE; + thread_process_info_map THREAD_PROCESS_INFO_TABLE = ([](){ thread_process_info_map initial; initial[0U] = nullptr; return ::std::move(initial); })(); + + ////////////////////////////////////////////////////////////////////////// + + void WINAPI processTraceEvent(PEVENT_RECORD _traceEvent) + { + if (_traceEvent->EventHeader.EventDescriptor.Opcode != SWITCH_CONTEXT_OPCODE) + return; + + if (sizeof(CSwitch) != _traceEvent->UserDataLength) + return; + + EASY_FUNCTION(EASY_COLOR_INTERNAL_EVENT, ::profiler::OFF); + + auto _contextSwitchEvent = reinterpret_cast(_traceEvent->UserData); + const auto time = static_cast<::profiler::timestamp_t>(_traceEvent->EventHeader.TimeStamp.QuadPart); + if (time > TRACING_END_TIME.load(::std::memory_order_acquire)) + return; + + DWORD pid = 0; + const char* process_name = ""; + + // Trying to get target process name and id + auto it = THREAD_PROCESS_INFO_TABLE.find(_contextSwitchEvent->NewThreadId); + if (it == THREAD_PROCESS_INFO_TABLE.end()) + { + auto hThread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, _contextSwitchEvent->NewThreadId); + if (hThread != nullptr) + { + pid = GetProcessIdOfThread(hThread); + auto pinfo = &PROCESS_INFO_TABLE[pid]; + + if (pinfo->valid == 0) + { + if (pinfo->name.empty()) + { + static char numbuf[128] = {}; + sprintf(numbuf, "%u", pid); + pinfo->name = numbuf; + pinfo->id = pid; + } + + /* + According to documentation, using GetModuleBaseName() requires + PROCESS_QUERY_INFORMATION | PROCESS_VM_READ access rights. + But it works fine with PROCESS_QUERY_LIMITED_INFORMATION instead of PROCESS_QUERY_INFORMATION. + + See https://msdn.microsoft.com/en-us/library/windows/desktop/ms683196(v=vs.85).aspx + */ + + //auto hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); + //if (hProc == nullptr) + auto hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ, FALSE, pid); + if (hProc != nullptr) + { + static TCHAR buf[MAX_PATH] = {}; // Using static is safe because processTraceEvent() is called from one thread + auto len = GetModuleBaseName(hProc, 0, buf, MAX_PATH); + + if (len != 0) + { + pinfo->name.reserve(pinfo->name.size() + 2 + len); + pinfo->name.append(" ", 1); + pinfo->name.append(buf, len); + pinfo->valid = 1; + } + + CloseHandle(hProc); + } + else + { + //auto err = GetLastError(); + //printf("OpenProcess(%u) fail: GetLastError() == %u\n", pid, err); + pinfo->valid = -1; + + if (pid == 4) { + pinfo->name.reserve(pinfo->name.size() + 8); + pinfo->name.append(" System", 7); + } + } + } + + process_name = pinfo->name.c_str(); + THREAD_PROCESS_INFO_TABLE[_contextSwitchEvent->NewThreadId] = pinfo; + + CloseHandle(hThread); + } + else + { + //printf("Can not OpenThread(%u);\n", _contextSwitchEvent->NewThreadId); + THREAD_PROCESS_INFO_TABLE[_contextSwitchEvent->NewThreadId] = nullptr; + } + } + else + { + auto pinfo = it->second; + if (pinfo != nullptr) + process_name = pinfo->name.c_str(); + else if (it->first == 0) + process_name = "System Idle"; + else if (it->first == 4) + process_name = "System"; + } + + MANAGER.beginContextSwitch(_contextSwitchEvent->OldThreadId, time, _contextSwitchEvent->NewThreadId, process_name); + MANAGER.endContextSwitch(_contextSwitchEvent->NewThreadId, pid, time); + } + + ////////////////////////////////////////////////////////////////////////// + +#ifndef EASY_MAGIC_STATIC_CPP11 + class EasyEventTracerInstance { + friend EasyEventTracer; + EasyEventTracer instance; + } EASY_EVENT_TRACER; +#endif + + EasyEventTracer& EasyEventTracer::instance() + { +#ifndef EASY_MAGIC_STATIC_CPP11 + return EASY_EVENT_TRACER.instance; +#else + static EasyEventTracer tracer; + return tracer; +#endif + } + + EasyEventTracer::EasyEventTracer() + { + m_lowPriority = ATOMIC_VAR_INIT(EASY_OPTION_LOW_PRIORITY_EVENT_TRACING); + } + + EasyEventTracer::~EasyEventTracer() + { + disable(); + } + + bool EasyEventTracer::isLowPriority() const + { + return m_lowPriority.load(::std::memory_order_acquire); + } + + void EasyEventTracer::setLowPriority(bool _value) + { + m_lowPriority.store(_value, ::std::memory_order_release); + } + + bool setPrivilege(HANDLE hToken, LPCSTR _privelegeName) + { + bool success = false; + + if (hToken) + { + LUID privilegyId; + if (LookupPrivilegeValue(NULL, _privelegeName, &privilegyId)) + { + TOKEN_PRIVILEGES tokenPrivilege; + tokenPrivilege.PrivilegeCount = 1; + tokenPrivilege.Privileges[0].Luid = privilegyId; + tokenPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + success = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivilege, sizeof(TOKEN_PRIVILEGES), NULL, NULL) != FALSE; + } + } + + EASY_LOG_ONLY( + if (!success) + EASY_WARNING("Failed to set " << _privelegeName << " privelege for the application.\n"); + ) + + return success; + } + + void EasyEventTracer::setProcessPrivileges() + { + static bool alreadySet = false; + if (alreadySet) + return; + + alreadySet = true; + + HANDLE hToken = nullptr; + if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) + { +#if EASY_OPTION_LOG_ENABLED != 0 + const bool success = setPrivilege(hToken, SE_DEBUG_NAME); + if (!success) + EASY_WARNING("Some context switch events could not get process name.\n"); +#else + setPrivilege(hToken, SE_DEBUG_NAME); +#endif + + CloseHandle(hToken); + } + EASY_LOG_ONLY( + else { + EASY_WARNING("Failed to open process to adjust priveleges.\n"); + } + ) + } + + ::profiler::EventTracingEnableStatus EasyEventTracer::startTrace(bool _force, int _step) + { + auto startTraceResult = StartTrace(&m_sessionHandle, KERNEL_LOGGER_NAME, props()); + switch (startTraceResult) + { + case ERROR_SUCCESS: + return EVENT_TRACING_LAUNCHED_SUCCESSFULLY; + + case ERROR_ALREADY_EXISTS: + { + if (_force) + { + // Try to stop another event tracing session to force launch self session. + + if (_step == 0) + { + /* + According to https://msdn.microsoft.com/en-us/library/windows/desktop/aa363696(v=vs.85).aspx + SessionHandle is ignored (and could be NULL) if SessionName is not NULL, + and you only need to set the Wnode.BufferSize, Wnode.Guid, LoggerNameOffset, and LogFileNameOffset + in EVENT_TRACE_PROPERTIES structure if ControlCode is EVENT_TRACE_CONTROL_STOP. + All data is already set for m_properties to the moment. Simply copy m_properties and use the copy. + + This method supposed to be faster than launching console window and executing shell command, + but if that would not work, return to using shell command "logman stop". + */ + + // static is safe because we are guarded by spin-lock m_spin + static Properties p = ([]{ Properties prp; strncpy(prp.sessionName, KERNEL_LOGGER_NAME, sizeof(prp.sessionName)); return prp; })(); + p.base = m_properties.base; // Use copy of m_properties to make sure m_properties will not be changed + + // Stop another session + ControlTrace(NULL, KERNEL_LOGGER_NAME, reinterpret_cast(&p), EVENT_TRACE_CONTROL_STOP); + + // Console window variant: + //if (32 >= (int)ShellExecute(NULL, NULL, "logman", "stop \"" KERNEL_LOGGER_NAME "\" -ets", NULL, SW_HIDE)) + // return EVENT_TRACING_WAS_LAUNCHED_BY_SOMEBODY_ELSE; + } + + if (_step < 4) + { + // Command executed successfully. Wait for a few time until tracing session finish. + ::std::this_thread::sleep_for(::std::chrono::milliseconds(500)); + return startTrace(true, ++_step); + } + } + + EASY_ERROR("Event tracing not launched: ERROR_ALREADY_EXISTS. To stop another session execute cmd: logman stop \"" << KERNEL_LOGGER_NAME << "\" -ets\n"); + return EVENT_TRACING_WAS_LAUNCHED_BY_SOMEBODY_ELSE; + } + + case ERROR_ACCESS_DENIED: + EASY_ERROR("Event tracing not launched: ERROR_ACCESS_DENIED. Try to launch your application as Administrator.\n"); + return EVENT_TRACING_NOT_ENOUGH_ACCESS_RIGHTS; + + case ERROR_BAD_LENGTH: + EASY_ERROR("Event tracing not launched: ERROR_BAD_LENGTH. It seems that your KERNEL_LOGGER_NAME differs from \"" << m_properties.sessionName << "\". Try to re-compile easy_profiler or contact EasyProfiler developers.\n"); + return EVENT_TRACING_BAD_PROPERTIES_SIZE; + } + + EASY_ERROR("Event tracing not launched: StartTrace() returned " << startTraceResult << ::std::endl); + + return EVENT_TRACING_MISTERIOUS_ERROR; + } + + ::profiler::EventTracingEnableStatus EasyEventTracer::enable(bool _force) + { + ::profiler::guard_lock<::profiler::spin_lock> lock(m_spin); + if (m_bEnabled) + return EVENT_TRACING_LAUNCHED_SUCCESSFULLY; + + /* + Trying to set debug privilege for current process + to be able to get other process information (process name). + */ + EasyEventTracer::setProcessPrivileges(); + + // Clear properties + memset(&m_properties, 0, sizeof(m_properties)); + m_properties.base.Wnode.BufferSize = sizeof(m_properties); + m_properties.base.Wnode.Flags = WNODE_FLAG_TRACED_GUID; + m_properties.base.Wnode.ClientContext = RAW_TIMESTAMP_TIME_TYPE; + m_properties.base.Wnode.Guid = SystemTraceControlGuid; + m_properties.base.LoggerNameOffset = sizeof(m_properties.base); + m_properties.base.EnableFlags = EVENT_TRACE_FLAG_CSWITCH; + m_properties.base.LogFileMode = EVENT_TRACE_REAL_TIME_MODE; + + // Start event tracing + auto res = startTrace(_force); + if (res != EVENT_TRACING_LAUNCHED_SUCCESSFULLY) + return res; + + memset(&m_trace, 0, sizeof(m_trace)); +#ifdef __MINGW32__ + m_trace.LoggerName = KERNEL_LOGGER; +#else + m_trace.LoggerName = KERNEL_LOGGER_NAME; +#endif + m_trace.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME | PROCESS_TRACE_MODE_EVENT_RECORD | PROCESS_TRACE_MODE_RAW_TIMESTAMP; + m_trace.EventRecordCallback = ::profiler::processTraceEvent; + + m_openedHandle = OpenTrace(&m_trace); + if (m_openedHandle == INVALID_PROCESSTRACE_HANDLE) + { + EASY_ERROR("Event tracing not launched: OpenTrace() returned invalid handle.\n"); + return EVENT_TRACING_OPEN_TRACE_ERROR; + } + + /* + Have to launch a thread to process events because according to MSDN documentation: + + The ProcessTrace function blocks the thread until it delivers all events, the BufferCallback function returns FALSE, + or you call CloseTrace. If the consumer is consuming events in real time, the ProcessTrace function returns after + the controller stops the trace session. (Note that there may be a several-second delay before the function returns.) + + https://msdn.microsoft.com/en-us/library/windows/desktop/aa364093(v=vs.85).aspx + */ + m_processThread = ::std::thread([this](bool _lowPriority) + { + if (_lowPriority) // Set low priority for event tracing thread + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST); + EASY_THREAD_SCOPE("EasyProfiler.ETW"); + ProcessTrace(&m_openedHandle, 1, 0, 0); + + }, m_lowPriority.load(::std::memory_order_acquire)); + + m_bEnabled = true; + + EASY_LOGMSG("Event tracing launched\n"); + return EVENT_TRACING_LAUNCHED_SUCCESSFULLY; + } + + void EasyEventTracer::disable() + { + ::profiler::guard_lock<::profiler::spin_lock> lock(m_spin); + if (!m_bEnabled) + return; + + EASY_LOGMSG("Event tracing is stopping...\n"); + + TRACING_END_TIME.store(getCurrentTime(), ::std::memory_order_release); + + ControlTrace(m_openedHandle, KERNEL_LOGGER_NAME, props(), EVENT_TRACE_CONTROL_STOP); + CloseTrace(m_openedHandle); + + // Wait for ProcessTrace to finish to make sure no processTraceEvent() will be called later. + if (m_processThread.joinable()) + m_processThread.join(); + + m_bEnabled = false; + + // processTraceEvent() is not called anymore. Clean static maps is safe. + PROCESS_INFO_TABLE.clear(); + THREAD_PROCESS_INFO_TABLE.clear(); + THREAD_PROCESS_INFO_TABLE[0U] = nullptr; + + TRACING_END_TIME.store(~0ULL, ::std::memory_order_release); + + EASY_LOGMSG("Event tracing stopped\n"); + } + +} // END of namespace profiler. + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#endif // _WIN32 diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_win.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_win.h new file mode 100644 index 0000000000..7c112de607 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/event_trace_win.h @@ -0,0 +1,129 @@ +/************************************************************************ +* file name : event_trace_win.h +* ----------------- : +* creation time : 2016/09/04 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of EasyEventTracer class used for tracing +* : Windows system events to get context switches. +* ----------------- : +* change log : * 2016/09/04 Victor Zarubkin: initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_PROFILER_EVENT_TRACE_WINDOWS_H +#define EASY_PROFILER_EVENT_TRACE_WINDOWS_H +#ifdef _WIN32 + +#define INITGUID // This is to enable using SystemTraceControlGuid in evntrace.h. +#include +#include +#include +#include +#include +#include +#include +#include "event_trace_status.h" +#include "spin_lock.h" + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +namespace profiler { + + class EasyEventTracer EASY_FINAL + { +#ifndef EASY_MAGIC_STATIC_CPP11 + friend class EasyEventTracerInstance; +#endif + +#pragma pack(push, 1) + struct Properties { + EVENT_TRACE_PROPERTIES base; + char sessionName[sizeof(KERNEL_LOGGER_NAME)]; + }; +#pragma pack(pop) + + ::std::thread m_processThread; + Properties m_properties; + EVENT_TRACE_LOGFILE m_trace; + ::profiler::spin_lock m_spin; + ::std::atomic_bool m_lowPriority; + TRACEHANDLE m_sessionHandle = INVALID_PROCESSTRACE_HANDLE; + TRACEHANDLE m_openedHandle = INVALID_PROCESSTRACE_HANDLE; + bool m_bEnabled = false; + + public: + + static EasyEventTracer& instance(); + ~EasyEventTracer(); + + bool isLowPriority() const; + + ::profiler::EventTracingEnableStatus enable(bool _force = false); + void disable(); + void setLowPriority(bool _value); + static void setProcessPrivileges(); + + private: + + EasyEventTracer(); + + inline EVENT_TRACE_PROPERTIES* props() + { + return reinterpret_cast(&m_properties); + } + + ::profiler::EventTracingEnableStatus startTrace(bool _force, int _step = 0); + + }; // END of class EasyEventTracer. + +} // END of namespace profiler. + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#endif // _WIN32 +#endif // EASY_PROFILER_EVENT_TRACE_WINDOWS_H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/hashed_cstr.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/hashed_cstr.h new file mode 100644 index 0000000000..075f2b1f72 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/hashed_cstr.h @@ -0,0 +1,298 @@ +/************************************************************************ +* file name : hashed_str.h +* ----------------- : +* creation time : 2016/09/11 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains definition of C-strings with calculated hash-code. +* : These strings may be used as optimized keys for std::unordered_map. +* ----------------- : +* change log : * 2016/09/11 Victor Zarubkin: Initial commit. Moved sources from reader.cpp +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_PROFILER__HASHED_CSTR__H_ +#define EASY_PROFILER__HASHED_CSTR__H_ + +#include +#include +#include + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#if 0 == 1//defined(_MSC_VER)// && _MSC_VER >= 1800 +# define EASY_PROFILER_HASHED_CSTR_DEFINED + +namespace profiler { + + /** \brief Simple C-string pointer with length. + + It is used as base class for a key in std::unordered_map. + It is used to get better performance than std::string. + It simply stores a pointer and a length, there is no + any memory allocation and copy. + + \warning Make sure you know what you are doing. You have to be sure that + pointed C-string will exist until you finish using this cstring. + + \ingroup profiler + */ + class cstring + { + protected: + + const char* m_str; + size_t m_len; + + public: + + cstring(const char* _str) : m_str(_str), m_len(strlen(_str)) + { + } + + cstring(const char* _str, size_t _len) : m_str(_str), m_len(_len) + { + } + + cstring(const cstring&) = default; + cstring& operator = (const cstring&) = default; + + inline bool operator == (const cstring& _other) const + { + return m_len == _other.m_len && !strncmp(m_str, _other.m_str, m_len); + } + + inline bool operator != (const cstring& _other) const + { + return !operator == (_other); + } + + inline bool operator < (const cstring& _other) const + { + if (m_len == _other.m_len) + { + return strncmp(m_str, _other.m_str, m_len) < 0; + } + + return m_len < _other.m_len; + } + + inline const char* c_str() const + { + return m_str; + } + + inline size_t size() const + { + return m_len; + } + + }; // END of class cstring. + + /** \brief cstring with precalculated hash. + + This is used to calculate hash for C-string and to cache it + to be used in the future without recurring hash calculatoin. + + \note This class is used as a key in std::unordered_map. + + \ingroup profiler + */ + class hashed_cstr : public cstring + { + typedef cstring Parent; + + size_t m_hash; + + public: + + hashed_cstr(const char* _str) : Parent(_str), m_hash(0) + { + m_hash = ::std::_Hash_seq((const unsigned char *)m_str, m_len); + } + + hashed_cstr(const char* _str, size_t _hash_code) : Parent(_str), m_hash(_hash_code) + { + } + + hashed_cstr(const char* _str, size_t _len, size_t _hash_code) : Parent(_str, _len), m_hash(_hash_code) + { + } + + hashed_cstr(const hashed_cstr&) = default; + hashed_cstr& operator = (const hashed_cstr&) = default; + + inline bool operator == (const hashed_cstr& _other) const + { + return m_hash == _other.m_hash && Parent::operator == (_other); + } + + inline bool operator != (const hashed_cstr& _other) const + { + return !operator == (_other); + } + + inline size_t hcode() const + { + return m_hash; + } + + }; // END of class hashed_cstr. + +} // END of namespace profiler. + +namespace std { + + /** \brief Simply returns precalculated hash of a C-string. */ + template <> struct hash<::profiler::hashed_cstr> { + typedef ::profiler::hashed_cstr argument_type; + typedef size_t result_type; + inline size_t operator () (const ::profiler::hashed_cstr& _str) const { + return _str.hcode(); + } + }; + +} // END of namespace std. + +#else //////////////////////////////////////////////////////////////////// + +// TODO: Create hashed_cstr for Linux (need to use Linux version of std::_Hash_seq) + +#endif + +namespace profiler { + + class hashed_stdstring + { + ::std::string m_str; + size_t m_hash; + + public: + + hashed_stdstring(const char* _str) : m_str(_str), m_hash(::std::hash<::std::string>()(m_str)) + { + } + + hashed_stdstring(const ::std::string& _str) : m_str(_str), m_hash(::std::hash<::std::string>()(m_str)) + { + } + + hashed_stdstring(::std::string&& _str) : m_str(::std::forward<::std::string&&>(_str)), m_hash(::std::hash<::std::string>()(m_str)) + { + } + + hashed_stdstring(hashed_stdstring&& _other) : m_str(::std::move(_other.m_str)), m_hash(_other.m_hash) + { + } + + hashed_stdstring(const char* _str, size_t _hash_code) : m_str(_str), m_hash(_hash_code) + { + } + + hashed_stdstring(const ::std::string& _str, size_t _hash_code) : m_str(_str), m_hash(_hash_code) + { + } + + hashed_stdstring(::std::string&& _str, size_t _hash_code) : m_str(::std::forward<::std::string&&>(_str)), m_hash(_hash_code) + { + } + + hashed_stdstring(const hashed_stdstring&) = default; + hashed_stdstring& operator = (const hashed_stdstring&) = default; + + hashed_stdstring& operator = (hashed_stdstring&& _other) + { + m_str = ::std::move(_other.m_str); + m_hash = _other.m_hash; + return *this; + } + + inline bool operator == (const hashed_stdstring& _other) const + { + return m_hash == _other.m_hash && m_str == _other.m_str; + } + + inline bool operator != (const hashed_stdstring& _other) const + { + return !operator == (_other); + } + + inline size_t hcode() const + { + return m_hash; + } + + inline const char* c_str() const + { + return m_str.c_str(); + } + + inline size_t size() const + { + return m_str.size(); + } + + }; // END of class hashed_stdstring. + +} // END of namespace profiler. + +namespace std { + + /** \brief Simply returns precalculated hash of a std::string. */ + template <> struct hash<::profiler::hashed_stdstring> { + typedef ::profiler::hashed_stdstring argument_type; + typedef size_t result_type; + inline size_t operator () (const ::profiler::hashed_stdstring& _str) const { + return _str.hcode(); + } + }; + +} // END of namespace std. + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_PROFILER__HASHED_CSTR__H_ diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_compiler_support.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_compiler_support.h new file mode 100755 index 0000000000..db2ade08d3 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_compiler_support.h @@ -0,0 +1,156 @@ +/************************************************************************ +* file name : easy_compiler_support.h +* ----------------- : +* creation time : 2016/09/22 +* authors : Victor Zarubkin, Sergey Yagovtsev +* emails : v.s.zarubkin@gmail.com, yse.sey@gmail.com +* ----------------- : +* description : This file contains auxiliary profiler macros for different compiler support. +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_PROFILER_COMPILER_SUPPORT_H +#define EASY_PROFILER_COMPILER_SUPPORT_H + +#include + +//#define EASY_CODE_WRAP(Code) Code + +#if defined(_WIN32) && !defined(EASY_PROFILER_STATIC) +// Visual Studio and MinGW +# ifdef _BUILD_PROFILER +# define PROFILER_API __declspec(dllexport) +# else +# define PROFILER_API __declspec(dllimport) +# endif +#endif + + + +#if defined (_MSC_VER) +////////////////////////////////////////////////////////////////////////// +// Visual Studio + +# define __func__ __FUNCTION__ + +# if _MSC_VER <= 1800 +// There is no support for C++11 thread_local keyword prior to Visual Studio 2015. Use __declspec(thread) instead. +// There is also no support for C++11 magic statics feature :( So it becomes slightly harder to initialize static vars - additional "if" for each profiler block. +# define EASY_THREAD_LOCAL __declspec(thread) +# define EASY_LOCAL_STATIC_PTR(VarType, VarName, VarInitializer)\ + __declspec(thread) static VarType VarName = 0;\ + if (!VarName)\ + VarName = VarInitializer +# endif + +#elif defined (__clang__) +////////////////////////////////////////////////////////////////////////// +// Clang Compiler + +# if (__clang_major__ == 3 && __clang_minor__ < 3) || (__clang_major__ < 3) +// There is no support for C++11 thread_local keyword prior to clang 3.3. Use __thread instead. +# define EASY_THREAD_LOCAL __thread +# endif + +# if (__clang_major__ == 2 && __clang_minor__ < 9) || (__clang_major__ < 2) +// There is no support for C++11 magic statics feature prior to clang 2.9. It becomes slightly harder to initialize static vars - additional "if" for each profiler block. +# define EASY_LOCAL_STATIC_PTR(VarType, VarName, VarInitializer)\ + EASY_THREAD_LOCAL static VarType VarName = 0;\ + if (!VarName)\ + VarName = VarInitializer + +// There is no support for C++11 final keyword prior to clang 2.9 +# define EASY_FINAL +# endif + +#elif defined(__GNUC__) +////////////////////////////////////////////////////////////////////////// +// GNU Compiler + +# if (__GNUC__ == 4 && __GNUC_MINOR__ < 8) || (__GNUC__ < 4) +// There is no support for C++11 thread_local keyword prior to gcc 4.8. Use __thread instead. +# define EASY_THREAD_LOCAL __thread +# endif + +# if (__GNUC__ == 4 && __GNUC_MINOR__ < 3) || (__GNUC__ < 4) +// There is no support for C++11 magic statics feature prior to gcc 4.3. It becomes slightly harder to initialize static vars - additional "if" for each profiler block. +# define EASY_LOCAL_STATIC_PTR(VarType, VarName, VarInitializer)\ + EASY_THREAD_LOCAL static VarType VarName = 0;\ + if (!VarName)\ + VarName = VarInitializer +# endif + +# if (__GNUC__ == 4 && __GNUC_MINOR__ < 7) || (__GNUC__ < 4) +// There is no support for C++11 final keyword prior to gcc 4.7 +# define EASY_FINAL +# endif + +#endif +// END // TODO: Add other compilers support +////////////////////////////////////////////////////////////////////////// + + + +////////////////////////////////////////////////////////////////////////// +// Default values + +#ifndef EASY_THREAD_LOCAL +# define EASY_THREAD_LOCAL thread_local +# define EASY_THREAD_LOCAL_CPP11 +#endif + +#ifndef EASY_LOCAL_STATIC_PTR +# define EASY_LOCAL_STATIC_PTR(VarType, VarName, VarInitializer) static VarType VarName = VarInitializer +# define EASY_MAGIC_STATIC_CPP11 +#endif + +#ifndef EASY_FINAL +# define EASY_FINAL final +#endif + +#ifndef PROFILER_API +# define PROFILER_API +#endif + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_PROFILER_COMPILER_SUPPORT_H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_net.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_net.h new file mode 100644 index 0000000000..ad8c382499 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_net.h @@ -0,0 +1,149 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin + +Licensed under either of + * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +at your option. + +The MIT License + 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. + + +The Apache License, Version 2.0 (the "License"); + You may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +**/ + +#ifndef EASY_NET_H +#define EASY_NET_H + +#include + +namespace profiler { +namespace net { + +const uint32_t EASY_MESSAGE_SIGN = 20160909; + +#pragma pack(push,1) + +enum MessageType : uint8_t +{ + MESSAGE_TYPE_ZERO = 0, + + MESSAGE_TYPE_REQUEST_START_CAPTURE, + MESSAGE_TYPE_REPLY_START_CAPTURING, + MESSAGE_TYPE_REQUEST_STOP_CAPTURE, + + MESSAGE_TYPE_REPLY_BLOCKS, + MESSAGE_TYPE_REPLY_BLOCKS_END, + + MESSAGE_TYPE_ACCEPTED_CONNECTION, + + MESSAGE_TYPE_REQUEST_BLOCKS_DESCRIPTION, + MESSAGE_TYPE_REPLY_BLOCKS_DESCRIPTION, + MESSAGE_TYPE_REPLY_BLOCKS_DESCRIPTION_END, + + MESSAGE_TYPE_EDIT_BLOCK_STATUS, + + MESSAGE_TYPE_EVENT_TRACING_STATUS, + MESSAGE_TYPE_EVENT_TRACING_PRIORITY, + MESSAGE_TYPE_CHECK_CONNECTION, + + MESSAGE_TYPE_REQUEST_MAIN_FRAME_TIME_MAX_AVG_US, + MESSAGE_TYPE_REPLY_MAIN_FRAME_TIME_MAX_AVG_US, +}; + +struct Message +{ + uint32_t magic_number = EASY_MESSAGE_SIGN; + MessageType type = MESSAGE_TYPE_ZERO; + + bool isEasyNetMessage() const + { + return EASY_MESSAGE_SIGN == magic_number; + } + + Message() = default; + Message(MessageType _t):type(_t){} +}; + +struct DataMessage : public Message { + uint32_t size = 0; // bytes + DataMessage(MessageType _t = MESSAGE_TYPE_REPLY_BLOCKS) : Message(_t) {} + DataMessage(uint32_t _s, MessageType _t = MESSAGE_TYPE_REPLY_BLOCKS) : Message(_t), size(_s) {} + const char* data() const { return reinterpret_cast(this) + sizeof(DataMessage); } +}; + +struct BlockStatusMessage : public Message { + uint32_t id; + uint8_t status; + BlockStatusMessage(uint32_t _id, uint8_t _status) : Message(MESSAGE_TYPE_EDIT_BLOCK_STATUS), id(_id), status(_status) { } +private: + BlockStatusMessage() = delete; +}; + +struct EasyProfilerStatus : public Message +{ + bool isProfilerEnabled; + bool isEventTracingEnabled; + bool isLowPriorityEventTracing; + + EasyProfilerStatus(bool _enabled, bool _ETenabled, bool _ETlowp) + : Message(MESSAGE_TYPE_ACCEPTED_CONNECTION) + , isProfilerEnabled(_enabled) + , isEventTracingEnabled(_ETenabled) + , isLowPriorityEventTracing(_ETlowp) + { + } + +private: + + EasyProfilerStatus() = delete; +}; + +struct BoolMessage : public Message { + bool flag = false; + BoolMessage(MessageType _t, bool _flag = false) : Message(_t), flag(_flag) { } + BoolMessage() = default; +}; + +struct TimestampMessage : public Message { + uint32_t maxValue = 0; + uint32_t avgValue = 0; + TimestampMessage(MessageType _t, uint32_t _maxValue, uint32_t _avgValue) : Message(_t), maxValue(_maxValue), avgValue(_avgValue) { } + TimestampMessage() = default; +}; + +#pragma pack(pop) + +}//net + +}//profiler + +#endif // EASY_NET_H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_socket.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_socket.h new file mode 100644 index 0000000000..ed9a868a60 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/easy_socket.h @@ -0,0 +1,128 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin + +Licensed under either of + * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +at your option. + +The MIT License + 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. + + +The Apache License, Version 2.0 (the "License"); + You may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +**/ +#ifndef EASY________SOCKET_________H +#define EASY________SOCKET_________H + +#include +#include +#ifndef _WIN32 +#include +#include +#include +#include +#include +#include +#include //for android-build +#else + +#define WIN32_LEAN_AND_MEAN + +#include +#include +#include +#include +#endif + +class PROFILER_API EasySocket +{ +public: + +#ifdef _WIN32 + typedef SOCKET socket_t; +#else + typedef int socket_t; +#endif + + enum ConnectionState + { + CONNECTION_STATE_UNKNOWN, + CONNECTION_STATE_SUCCESS, + + CONNECTION_STATE_DISCONNECTED, + CONNECTION_STATE_IN_PROGRESS + }; + +private: + + void checkResult(int result); + bool checkSocket(socket_t s) const; + static int _close(socket_t s); + void setBlocking(socket_t s, bool blocking); + + socket_t m_socket = 0; + socket_t m_replySocket = 0; + + int wsaret = -1; + + struct hostent * server; + struct sockaddr_in serv_addr; + + ConnectionState m_state = CONNECTION_STATE_UNKNOWN; + +public: + + EasySocket(); + ~EasySocket(); + + int send(const void *buf, size_t nbyte); + int receive(void *buf, size_t nbyte); + int listen(int count=5); + int accept(); + int bind(uint16_t portno); + + bool setAddress(const char* serv, uint16_t port); + int connect(); + + void flush(); + void init(); + + void setState(ConnectionState state){m_state=state;} + ConnectionState state() const{return m_state;} + + bool isDisconnected() const + { + return m_state == CONNECTION_STATE_UNKNOWN || + m_state == CONNECTION_STATE_DISCONNECTED; + } +}; + +#endif // EASY________SOCKET_________H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler.h new file mode 100644 index 0000000000..cc3a8e2a2f --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler.h @@ -0,0 +1,966 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin + +Licensed under either of + * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +at your option. + +The MIT License + 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. + + +The Apache License, Version 2.0 (the "License"); + You may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +**/ + +#ifndef EASY_PROFILER_H +#define EASY_PROFILER_H + +#include + +#if defined ( __clang__ ) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" +#endif + +// +// BUILD_WITH_EASY_PROFILER is defined in CMakeLists.txt if your project is linked to easy_profiler. +// + +// +// DISABLE_EASY_PROFILER may be defined manually in source-file before #include +// to disable profiler for certain source-file or project. +// + +#if defined(BUILD_WITH_EASY_PROFILER) && !defined(DISABLE_EASY_PROFILER) + +/** +\defgroup profiler EasyProfiler +*/ + + +/** Indicates that EasyProfiler is used. + +\ingroup profiler +*/ +#define USING_EASY_PROFILER + + +// EasyProfiler core API: + +/** Macro for beginning of a scoped block with custom name and color. + +\code + #include + void foo() + { + // some code ... + + EASY_BLOCK("Check something", profiler::OFF); // Disabled block (There is possibility to enable this block later via GUI) + if(something){ + EASY_BLOCK("Calling bar()"); // Block with default color + bar(); + } + else{ + EASY_BLOCK("Calling baz()", profiler::colors::Red); // Red block + baz(); + } + EASY_END_BLOCK; // End of "Check something" block (Even if "Check something" is disabled, this EASY_END_BLOCK will not end any other block). + + EASY_BLOCK("Some another block", profiler::colors::Blue, profiler::ON_WITHOUT_CHILDREN); // Block with Blue color without + // some another code... + EASY_BLOCK("Calculate sum"); // This block will not be profiled because it's parent is ON_WITHOUT_CHILDREN + int sum = 0; + for (int i = 0; i < 10; ++i) + sum += i; + EASY_END_BLOCK; // End of "Calculate sum" block + } +\endcode + +Block will be automatically completed by destructor. + +\ingroup profiler +*/ +# define EASY_BLOCK(name, ...)\ + EASY_LOCAL_STATIC_PTR(const ::profiler::BaseBlockDescriptor*, EASY_UNIQUE_DESC(__LINE__), ::profiler::registerDescription(::profiler::extract_enable_flag(__VA_ARGS__),\ + EASY_UNIQUE_LINE_ID, EASY_COMPILETIME_NAME(name), __FILE__, __LINE__, ::profiler::BLOCK_TYPE_BLOCK, ::profiler::extract_color(__VA_ARGS__),\ + ::std::is_base_of<::profiler::ForceConstStr, decltype(name)>::value));\ + ::profiler::Block EASY_UNIQUE_BLOCK(__LINE__)(EASY_UNIQUE_DESC(__LINE__), EASY_RUNTIME_NAME(name));\ + ::profiler::beginBlock(EASY_UNIQUE_BLOCK(__LINE__)); + +/** Macro for beginning of a non-scoped block with custom name and color. + +You must end such block manually with EASY_END_BLOCK. + +\code + #include + void foo() { + EASY_NONSCOPED_BLOCK("Callback"); // Begin block which would not be finished when function returns. + + // some code ... + } + + void bar() { + // some another code... + + EASY_END_BLOCK; // This, as always, ends last opened block. You have to take care about blocks order by yourself. + } + + void baz() { + foo(); // non-scoped block begins here + + // some code... + + bar(); // non-scoped block ends here + } +\endcode + +Block will be automatically completed by destructor. + +\ingroup profiler +*/ +#define EASY_NONSCOPED_BLOCK(name, ...)\ + EASY_LOCAL_STATIC_PTR(const ::profiler::BaseBlockDescriptor*, EASY_UNIQUE_DESC(__LINE__), ::profiler::registerDescription(::profiler::extract_enable_flag(__VA_ARGS__),\ + EASY_UNIQUE_LINE_ID, EASY_COMPILETIME_NAME(name), __FILE__, __LINE__, ::profiler::BLOCK_TYPE_BLOCK, ::profiler::extract_color(__VA_ARGS__),\ + ::std::is_base_of<::profiler::ForceConstStr, decltype(name)>::value));\ + ::profiler::beginNonScopedBlock(EASY_UNIQUE_DESC(__LINE__), EASY_RUNTIME_NAME(name)); + +/** Macro for beginning of a block with function name and custom color. + +\code + #include + void foo(){ + EASY_FUNCTION(); // Block with name="foo" and default color + //some code... + } + + void bar(){ + EASY_FUNCTION(profiler::colors::Green); // Green block with name="bar" + //some code... + } + + void baz(){ + EASY_FUNCTION(profiler::FORCE_ON); // Force enabled block with name="baz" and default color (This block will be profiled even if it's parent is OFF_RECURSIVE) + // som code... + } +\endcode + +Name of the block automatically created with function name. + +\ingroup profiler +*/ +# define EASY_FUNCTION(...)\ + EASY_LOCAL_STATIC_PTR(const ::profiler::BaseBlockDescriptor*, EASY_UNIQUE_DESC(__LINE__), ::profiler::registerDescription(::profiler::extract_enable_flag(__VA_ARGS__),\ + EASY_UNIQUE_LINE_ID, __func__, __FILE__, __LINE__, ::profiler::BLOCK_TYPE_BLOCK, ::profiler::extract_color(__VA_ARGS__), false));\ + ::profiler::Block EASY_UNIQUE_BLOCK(__LINE__)(EASY_UNIQUE_DESC(__LINE__), "");\ + ::profiler::beginBlock(EASY_UNIQUE_BLOCK(__LINE__)); // this is to avoid compiler warning about unused variable + +/** Macro for completion of last opened block explicitly. + +\code +#include +int foo() +{ + // some code ... + + int sum = 0; + EASY_BLOCK("Calculating sum"); + for (int i = 0; i < 10; ++i){ + sum += i; + } + EASY_END_BLOCK; + + // some antoher code here ... + + return sum; +} +\endcode + +\ingroup profiler +*/ +# define EASY_END_BLOCK ::profiler::endBlock(); + +/** Macro for creating event marker with custom name and color. + +Event marker is a block with zero duration and special type. + +\warning Event marker ends immidiately and calling EASY_END_BLOCK after EASY_EVENT +will end previously opened EASY_BLOCK or EASY_FUNCTION. + +\ingroup profiler +*/ +# define EASY_EVENT(name, ...)\ + EASY_LOCAL_STATIC_PTR(const ::profiler::BaseBlockDescriptor*, EASY_UNIQUE_DESC(__LINE__), ::profiler::registerDescription(\ + ::profiler::extract_enable_flag(__VA_ARGS__), EASY_UNIQUE_LINE_ID, EASY_COMPILETIME_NAME(name),\ + __FILE__, __LINE__, ::profiler::BLOCK_TYPE_EVENT, ::profiler::extract_color(__VA_ARGS__),\ + ::std::is_base_of<::profiler::ForceConstStr, decltype(name)>::value));\ + ::profiler::storeEvent(EASY_UNIQUE_DESC(__LINE__), EASY_RUNTIME_NAME(name)); + +/** Macro for enabling profiler. + +\ingroup profiler +*/ +# define EASY_PROFILER_ENABLE ::profiler::setEnabled(true); + +/** Macro for disabling profiler. + +\ingroup profiler +*/ +# define EASY_PROFILER_DISABLE ::profiler::setEnabled(false); + +/** Macro for current thread registration. + +\note If this thread has been already registered then nothing happens. + +\ingroup profiler +*/ +# define EASY_THREAD(name)\ + EASY_THREAD_LOCAL static const char* EASY_TOKEN_CONCATENATE(unique_profiler_thread_name, __LINE__) = 0;\ + if (!EASY_TOKEN_CONCATENATE(unique_profiler_thread_name, __LINE__))\ + EASY_TOKEN_CONCATENATE(unique_profiler_thread_name, __LINE__) = ::profiler::registerThread(name); + +/** Macro for current thread registration and creating a thread guard object. + +\note If this thread has been already registered then nothing happens. + +\note Also creates thread guard which marks thread as "expired" on it's destructor +and creates "ThreadFinished" profiler event. + +\ingroup profiler +*/ +# define EASY_THREAD_SCOPE(name)\ + EASY_THREAD_LOCAL static const char* EASY_TOKEN_CONCATENATE(unique_profiler_thread_name, __LINE__) = 0;\ + ::profiler::ThreadGuard EASY_TOKEN_CONCATENATE(unique_profiler_thread_guard, __LINE__);\ + if (!EASY_TOKEN_CONCATENATE(unique_profiler_thread_name, __LINE__))\ + EASY_TOKEN_CONCATENATE(unique_profiler_thread_name, __LINE__) = ::profiler::registerThreadScoped(name,\ + EASY_TOKEN_CONCATENATE(unique_profiler_thread_guard, __LINE__)); + +/** Macro for main thread registration. + +This is just for user's comfort. There is no difference for EasyProfiler GUI between different threads. + +\ingroup profiler +*/ +# define EASY_MAIN_THREAD EASY_THREAD("Main") + +/** Enable or disable event tracing (context switch events). + +\note Default value is controlled by EASY_OPTION_EVENT_TRACING_ENABLED macro. + +\note Change will take effect on the next call to EASY_PROFILER_ENABLE. + +\sa EASY_PROFILER_ENABLE, EASY_OPTION_EVENT_TRACING_ENABLED + +\ingroup profiler +*/ +# define EASY_SET_EVENT_TRACING_ENABLED(isEnabled) ::profiler::setEventTracingEnabled(isEnabled); + +/** Set event tracing thread priority (low or normal). + +Event tracing with low priority will affect your application performance much more less, but +it can be late to gather information about thread/process (thread could be finished to the moment +when event tracing thread will be awaken) and you will not see process name and process id +information in GUI for such threads. You will still be able to see all context switch events. + +Event tracing with normal priority could gather more information about processes but potentially +it could affect performance as it has more work to do. Usually you will not notice any performance +breakdown, but if you care about that then you change set event tracing priority level to low. + +\sa EASY_OPTION_LOW_PRIORITY_EVENT_TRACING + +\ingroup profiler +*/ +# define EASY_SET_LOW_PRIORITY_EVENT_TRACING(isLowPriority) ::profiler::setLowPriorityEventTracing(isLowPriority); + +/** Macro for setting temporary log-file path for Unix event tracing system. + +\note Default value is "/tmp/cs_profiling_info.log". + +\ingroup profiler +*/ +# define EASY_EVENT_TRACING_SET_LOG(filename) ::profiler::setContextSwitchLogFilename(filename); + +/** Macro returning current path to the temporary log-file for Unix event tracing system. + +\ingroup profiler +*/ +# define EASY_EVENT_TRACING_LOG ::profiler::getContextSwitchLogFilename(); + +// EasyProfiler settings: + +/** If != 0 then EasyProfiler will measure time for blocks storage expansion. +If 0 then EasyProfiler will be compiled without blocks of code responsible +for measuring these events. + +These are "EasyProfiler.ExpandStorage" blocks on a diagram. + +\ingroup profiler +*/ +# ifndef EASY_OPTION_MEASURE_STORAGE_EXPAND +# define EASY_OPTION_MEASURE_STORAGE_EXPAND 0 +# endif + +# if EASY_OPTION_MEASURE_STORAGE_EXPAND != 0 +/** If true then "EasyProfiler.ExpandStorage" blocks are enabled by default and will be +writed to output file or translated over the net. +If false then you need to enable these blocks via GUI if you want to see them. + +\ingroup profiler +*/ +# ifndef EASY_OPTION_STORAGE_EXPAND_BLOCKS_ON +# define EASY_OPTION_STORAGE_EXPAND_BLOCKS_ON true +# endif + +# endif // EASY_OPTION_MEASURE_STORAGE_EXPAND != 0 + +/** If true then EasyProfiler event tracing is enabled by default +and will be turned on and off when you call profiler::setEnabled(). +Otherwise, it have to be turned on via GUI and then it will be +turned on/off with next calls of profiler::setEnabled(). + +\ingroup profiler +*/ +# ifndef EASY_OPTION_EVENT_TRACING_ENABLED +# define EASY_OPTION_EVENT_TRACING_ENABLED true +# endif + +/** If true then EasyProfiler.ETW thread (Event tracing for Windows) will have low priority by default. + +\sa EASY_SET_LOW_PRIORITY_EVENT_TRACING + +\note You can always change priority level via GUI or API while profiling session is not launched. +You don't need to rebuild or restart your application for that. + +\ingroup profiler +*/ +# ifndef EASY_OPTION_LOW_PRIORITY_EVENT_TRACING +# define EASY_OPTION_LOW_PRIORITY_EVENT_TRACING true +# endif + + +/** If != 0 then EasyProfiler will print error messages into stderr. +Otherwise, no log messages will be printed. + +\ingroup profiler +*/ +# ifndef EASY_OPTION_LOG_ENABLED +# define EASY_OPTION_LOG_ENABLED 0 +# endif + +/** If != 0 then EasyProfiler will start listening thread immidiately on ProfileManager initialization. + +\sa startListen + +\ingroup profiler +*/ +# ifndef EASY_OPTION_START_LISTEN_ON_STARTUP +# define EASY_OPTION_START_LISTEN_ON_STARTUP 0 +# endif + +#else // #ifdef BUILD_WITH_EASY_PROFILER + +# define EASY_BLOCK(...) +# define EASY_NONSCOPED_BLOCK(...) +# define EASY_FUNCTION(...) +# define EASY_END_BLOCK +# define EASY_PROFILER_ENABLE +# define EASY_PROFILER_DISABLE +# define EASY_EVENT(...) +# define EASY_THREAD(...) +# define EASY_THREAD_SCOPE(...) +# define EASY_MAIN_THREAD +# define EASY_SET_EVENT_TRACING_ENABLED(isEnabled) +# define EASY_SET_LOW_PRIORITY_EVENT_TRACING(isLowPriority) + +# ifndef _WIN32 +# define EASY_EVENT_TRACING_SET_LOG(filename) +# define EASY_EVENT_TRACING_LOG "" +# endif + +# ifndef EASY_OPTION_MEASURE_STORAGE_EXPAND +# define EASY_OPTION_MEASURE_STORAGE_EXPAND 0 +# endif + +# ifndef EASY_OPTION_EVENT_TRACING_ENABLED +# define EASY_OPTION_EVENT_TRACING_ENABLED false +# endif + +# ifndef EASY_OPTION_LOW_PRIORITY_EVENT_TRACING +# define EASY_OPTION_LOW_PRIORITY_EVENT_TRACING true +# endif + +# ifndef EASY_OPTION_LOG_ENABLED +# define EASY_OPTION_LOG_ENABLED 0 +# endif + +# ifndef EASY_OPTION_START_LISTEN_ON_STARTUP +# define EASY_OPTION_START_LISTEN_ON_STARTUP 0 +# endif + +#endif // #ifndef BUILD_WITH_EASY_PROFILER + +# ifndef EASY_DEFAULT_PORT +# define EASY_DEFAULT_PORT 28077 +# endif + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +class NonscopedBlock; +class ProfileManager; +struct ThreadStorage; + +namespace profiler { + + ////////////////////////////////////////////////////////////////////// + // Core types + + const uint16_t DEFAULT_PORT = EASY_DEFAULT_PORT; + + typedef uint64_t timestamp_t; + typedef uint64_t thread_id_t; + typedef uint32_t block_id_t; + + enum BlockType : uint8_t + { + BLOCK_TYPE_EVENT = 0, + BLOCK_TYPE_BLOCK, + + BLOCK_TYPES_NUMBER + }; + typedef BlockType block_type_t; + + enum Duration : uint8_t + { + TICKS = 0, ///< CPU ticks + MICROSECONDS ///< Microseconds + }; + + //*********************************************** + +#pragma pack(push,1) + class PROFILER_API BaseBlockDescriptor + { + friend ::ProfileManager; + friend ::ThreadStorage; + + protected: + + block_id_t m_id; ///< This descriptor id (We can afford this spending because there are much more blocks than descriptors) + int m_line; ///< Line number in the source file + color_t m_color; ///< Color of the block packed into 1-byte structure + block_type_t m_type; ///< Type of the block (See BlockType) + EasyBlockStatus m_status; ///< If false then blocks with such id() will not be stored by profiler during profile session + + BaseBlockDescriptor(block_id_t _id, EasyBlockStatus _status, int _line, block_type_t _block_type, color_t _color); + + public: + + inline block_id_t id() const { return m_id; } + inline int line() const { return m_line; } + inline color_t color() const { return m_color; } + inline block_type_t type() const { return m_type; } + inline EasyBlockStatus status() const { return m_status; } + + }; // END of class BaseBlockDescriptor. + + //*********************************************** + + class PROFILER_API Event + { + friend ::ProfileManager; + + protected: + + timestamp_t m_begin; + timestamp_t m_end; + + public: + + Event(const Event&) = default; + Event(timestamp_t _begin_time); + Event(timestamp_t _begin_time, timestamp_t _end_time); + + inline timestamp_t begin() const { return m_begin; } + inline timestamp_t end() const { return m_end; } + inline timestamp_t duration() const { return m_end - m_begin; } + + private: + + Event() = delete; + + }; // END class Event. + + class PROFILER_API BaseBlockData : public Event + { + friend ::ProfileManager; + + protected: + + block_id_t m_id; + + public: + + BaseBlockData(const BaseBlockData&) = default; + BaseBlockData(timestamp_t _begin_time, block_id_t _id); + BaseBlockData(timestamp_t _begin_time, timestamp_t _end_time, block_id_t _id); + + inline block_id_t id() const { return m_id; } + inline void setId(block_id_t _id) { m_id = _id; } + + private: + + BaseBlockData() = delete; + + }; // END of class BaseBlockData. + + class PROFILER_API CSwitchEvent : public Event + { + thread_id_t m_thread_id; + + public: + + CSwitchEvent() = default; + CSwitchEvent(const CSwitchEvent&) = default; + CSwitchEvent(timestamp_t _begin_time, thread_id_t _tid); + + inline thread_id_t tid() const { return m_thread_id; } + + }; // END of class CSwitchEvent. +#pragma pack(pop) + + //*********************************************** + + class PROFILER_API Block : public BaseBlockData + { + friend ::ProfileManager; + friend ::ThreadStorage; + friend ::NonscopedBlock; + + const char* m_name; + EasyBlockStatus m_status; + bool m_isScoped; + + private: + + void start(); + void start(timestamp_t _time); + void finish(); + void finish(timestamp_t _time); + inline bool finished() const { return m_end >= m_begin; } + inline EasyBlockStatus status() const { return m_status; } + inline void setStatus(EasyBlockStatus _status) { m_status = _status; } + + public: + + Block(Block&& that); + Block(const BaseBlockDescriptor* _desc, const char* _runtimeName, bool _scoped = true); + Block(timestamp_t _begin_time, block_id_t _id, const char* _runtimeName); + Block(timestamp_t _begin_time, timestamp_t _end_time, block_id_t _id, const char* _runtimeName); + ~Block(); + + inline const char* name() const { return m_name; } + + private: + + Block(const Block&) = delete; + Block& operator = (const Block&) = delete; + + }; // END of class Block. + + //*********************************************** + + class PROFILER_API ThreadGuard EASY_FINAL { + friend ::ProfileManager; + thread_id_t m_id = 0; + public: + ~ThreadGuard(); + }; // END of class ThreadGuard. + + ////////////////////////////////////////////////////////////////////// + // Core API + // Note: it is better to use macros defined above than a direct calls to API. + +#ifdef BUILD_WITH_EASY_PROFILER + extern "C" { + + /** Returns current time in ticks. + + You can use it if you want to store block explicitly. + + \ingroup profiler + */ + PROFILER_API timestamp_t currentTime(); + + /** Convert ticks to nanoseconds. + + \ingroup profiler + */ + PROFILER_API timestamp_t toNanoseconds(timestamp_t _ticks); + + /** Convert ticks to microseconds. + + \ingroup profiler + */ + PROFILER_API timestamp_t toMicroseconds(timestamp_t _ticks); + + /** Registers static description of a block. + + It is general information which is common for all such blocks. + Includes color, block type (see BlockType), file-name, line-number, compile-time name of a block and enable-flag. + + \note This API function is used by EASY_EVENT, EASY_BLOCK, EASY_FUNCTION macros. + There is no need to invoke this function explicitly. + + \ingroup profiler + */ + PROFILER_API const BaseBlockDescriptor* registerDescription(EasyBlockStatus _status, const char* _autogenUniqueId, const char* _compiletimeName, const char* _filename, int _line, block_type_t _block_type, color_t _color, bool _copyName = false); + + /** Stores event in the blocks list. + + An event ends instantly and has zero duration. + + \note There is no need to invoke this function explicitly - use EASY_EVENT macro instead. + + \param _desc Reference to the previously registered description. + \param _runtimeName Standard zero-terminated string which will be copied to the events buffer. + + \note _runtimeName must be an empty string ("") if you do not want to set name to the event at run-time. + + \ingroup profiler + */ + PROFILER_API void storeEvent(const BaseBlockDescriptor* _desc, const char* _runtimeName = ""); + + /** Stores block explicitly in the blocks list. + + Use this function for additional flexibility if you want to set block duration manually. + + \param _desc Reference to the previously registered description. + \param _runtimeName Standard zero-terminated string which will be copied to the events buffer. + \param _beginTime begin time of the block + \param _endTime end time of the block + + \note _runtimeName must be an empty string ("") if you do not want to set name to the block at run-time. + + \ingroup profiler + */ + PROFILER_API void storeBlock(const BaseBlockDescriptor* _desc, const char* _runtimeName, timestamp_t _beginTime, timestamp_t _endTime); + + /** Begins scoped block. + + \ingroup profiler + */ + PROFILER_API void beginBlock(Block& _block); + + /** Begins non-scoped block. + + \param _desc Reference to the previously registered description (see registerDescription). + \param _runtimeName Standard zero-terminated string which will be copied to the block buffer when block will end. + + \note There is no need to invoke this function explicitly - use EASY_NONSCOPED_BLOCK macro instead. + EASY_NONSCOPED_BLOCK macro could be used for higher flexibility if you have to begin block in one + function and end it in another one. + + \note _runtimeName must be an empty string ("") if you do not want to set name to the block at run-time. + \note _runtimeName is copied only when block ends so you must ensure it's validity until block end. + + \warning You have to end this block explicitly. + + \ingroup profiler + */ + PROFILER_API void beginNonScopedBlock(const BaseBlockDescriptor* _desc, const char* _runtimeName = ""); + + /** Ends last started block. + + Use this only if you want to finish block explicitly. + + \ingroup profiler + */ + PROFILER_API void endBlock(); + + /** Enable or disable profiler. + + \ingroup profiler + */ + PROFILER_API void setEnabled(bool _isEnable); + PROFILER_API bool isEnabled(); + + /** Save all gathered blocks into file. + + \note This also disables profiler. + + \ingroup profiler + */ + PROFILER_API uint32_t dumpBlocksToFile(const char* _filename); + + /** Register current thread and give it a name. + + \note Only first call of registerThread() for the current thread will have an effect. + + \ingroup profiler + */ + PROFILER_API const char* registerThreadScoped(const char* _name, ThreadGuard&); + + /** Register current thread and give it a name. + + \note Only first call of registerThread() for the current thread will have an effect. + + \ingroup profiler + */ + PROFILER_API const char* registerThread(const char* _name); + + /** Enable or disable event tracing. + + \note This change will take an effect on the next call of setEnabled(true); + + \sa setEnabled, EASY_SET_EVENT_TRACING_ENABLED + + \ingroup profiler + */ + PROFILER_API void setEventTracingEnabled(bool _isEnable); + PROFILER_API bool isEventTracingEnabled(); + + /** Set event tracing thread priority (low or normal). + + \note This change will take effect on the next call of setEnabled(true); + + \sa setEnabled, EASY_SET_LOW_PRIORITY_EVENT_TRACING + + \ingroup profiler + */ + PROFILER_API void setLowPriorityEventTracing(bool _isLowPriority); + PROFILER_API bool isLowPriorityEventTracing(); + + /** Set temporary log-file path for Unix event tracing system. + + \note Default value is "/tmp/cs_profiling_info.log". + + \ingroup profiler + */ + PROFILER_API void setContextSwitchLogFilename(const char* _name); + + /** Returns current path to the temporary log-file for Unix event tracing system. + + \ingroup profiler + */ + PROFILER_API const char* getContextSwitchLogFilename(); + + PROFILER_API void startListen(uint16_t _port = ::profiler::DEFAULT_PORT); + PROFILER_API void stopListen(); + PROFILER_API bool isListening(); + + /** Returns current major version. + + \ingroup profiler + */ + PROFILER_API uint8_t versionMajor(); + + /** Returns current minor version. + + \ingroup profiler + */ + PROFILER_API uint8_t versionMinor(); + + /** Returns current version patch. + + \ingroup profiler + */ + PROFILER_API uint16_t versionPatch(); + + /** Returns current version in 32-bit integer format. + + \ingroup profiler + */ + PROFILER_API uint32_t version(); + + /** Returns current version in 32-bit integer format. + + \ingroup profiler + */ + PROFILER_API const char* versionName(); + + /** Returns true if current thread has been marked as Main. + Otherwise, returns false. + */ + PROFILER_API bool isMainThread(); + + /** Returns last frame duration for current thread. + + \param _durationCast desired duration units (could be cpu-ticks or microseconds) + */ + PROFILER_API timestamp_t this_thread_frameTime(Duration _durationCast = ::profiler::MICROSECONDS); + + /** Returns local max of frame duration for current thread. + + Local max is maximum frame duration since last frameTimeLocalMax() call. + + \param _durationCast desired duration units (could be cpu-ticks or microseconds) + */ + PROFILER_API timestamp_t this_thread_frameTimeLocalMax(Duration _durationCast = ::profiler::MICROSECONDS); + + /** Returns local average of frame duration for current thread. + + Local average is average frame duration since last frameTimeLocalAvg() call. + + \param _durationCast desired duration units (could be cpu-ticks or microseconds) + */ + PROFILER_API timestamp_t this_thread_frameTimeLocalAvg(Duration _durationCast = ::profiler::MICROSECONDS); + + /** Returns last frame duration for main thread. + + \param _durationCast desired duration units (could be cpu-ticks or microseconds) + */ + PROFILER_API timestamp_t main_thread_frameTime(Duration _durationCast = ::profiler::MICROSECONDS); + + /** Returns local max of frame duration for main thread. + + Local max is maximum frame duration since last frameTimeLocalMax() call. + + \param _durationCast desired duration units (could be cpu-ticks or microseconds) + */ + PROFILER_API timestamp_t main_thread_frameTimeLocalMax(Duration _durationCast = ::profiler::MICROSECONDS); + + /** Returns local average of frame duration for main thread. + + Local average is average frame duration since last frameTimeLocalAvg() call. + + \param _durationCast desired duration units (could be cpu-ticks or microseconds) + */ + PROFILER_API timestamp_t main_thread_frameTimeLocalAvg(Duration _durationCast = ::profiler::MICROSECONDS); + + } +#else + inline timestamp_t currentTime() { return 0; } + inline timestamp_t toNanoseconds(timestamp_t) { return 0; } + inline timestamp_t toMicroseconds(timestamp_t) { return 0; } + inline const BaseBlockDescriptor* registerDescription(EasyBlockStatus, const char*, const char*, const char*, int, block_type_t, color_t, bool = false) + { return reinterpret_cast(0xbad); } + inline void endBlock() { } + inline void setEnabled(bool) { } + inline bool isEnabled() { return false; } + inline void storeEvent(const BaseBlockDescriptor*, const char* = "") { } + inline void storeBlock(const BaseBlockDescriptor*, const char*, timestamp_t, timestamp_t) { } + inline void beginBlock(Block&) { } + inline void beginNonScopedBlock(const BaseBlockDescriptor*, const char* = "") { } + inline uint32_t dumpBlocksToFile(const char*) { return 0; } + inline const char* registerThreadScoped(const char*, ThreadGuard&) { return ""; } + inline const char* registerThread(const char*) { return ""; } + inline void setEventTracingEnabled(bool) { } + inline bool isEventTracingEnabled() { return false; } + inline void setLowPriorityEventTracing(bool) { } + inline bool isLowPriorityEventTracing() { return false; } + inline void setContextSwitchLogFilename(const char*) { } + inline const char* getContextSwitchLogFilename() { return ""; } + inline void startListen(uint16_t = ::profiler::DEFAULT_PORT) { } + inline void stopListen() { } + inline bool isListening() { return false; } + inline uint8_t versionMajor() { return 0; } + inline uint8_t versionMinor() { return 0; } + inline uint16_t versionPatch() { return 0; } + inline uint32_t version() { return 0; } + inline const char* versionName() { return "v0.0.0_disabled"; } + inline bool isMainThread() { return false; } + inline timestamp_t this_thread_frameTime(Duration = ::profiler::MICROSECONDS) { return 0; } + inline timestamp_t this_thread_frameTimeLocalMax(Duration = ::profiler::MICROSECONDS) { return 0; } + inline timestamp_t this_thread_frameTimeLocalAvg(Duration = ::profiler::MICROSECONDS) { return 0; } + inline timestamp_t main_thread_frameTime(Duration = ::profiler::MICROSECONDS) { return 0; } + inline timestamp_t main_thread_frameTimeLocalMax(Duration = ::profiler::MICROSECONDS) { return 0; } + inline timestamp_t main_thread_frameTimeLocalAvg(Duration = ::profiler::MICROSECONDS) { return 0; } +#endif + + /** API functions binded to current thread. + + \ingroup profiler + */ + namespace this_thread { + + inline const char* registrate(const char* _name) { + return ::profiler::registerThread(_name); + } + + inline const char* registrate(const char* _name, ThreadGuard& _threadGuard) { + return ::profiler::registerThreadScoped(_name, _threadGuard); + } + + inline timestamp_t frameTime(Duration _durationCast = ::profiler::MICROSECONDS) { + return ::profiler::this_thread_frameTime(_durationCast); + } + + inline timestamp_t frameTimeLocalMax(Duration _durationCast = ::profiler::MICROSECONDS) { + return ::profiler::this_thread_frameTimeLocalMax(_durationCast); + } + + inline timestamp_t frameTimeLocalAvg(Duration _durationCast = ::profiler::MICROSECONDS) { + return ::profiler::this_thread_frameTimeLocalAvg(_durationCast); + } + + inline bool isMain() { + return ::profiler::isMainThread(); + } + + } // END of namespace this_thread. + + /** API functions binded to main thread. + + Could be called from any thread. + + \ingroup profiler + */ + namespace main_thread { + + inline timestamp_t frameTime(Duration _durationCast = ::profiler::MICROSECONDS) { + return ::profiler::main_thread_frameTime(_durationCast); + } + + inline timestamp_t frameTimeLocalMax(Duration _durationCast = ::profiler::MICROSECONDS) { + return ::profiler::main_thread_frameTimeLocalMax(_durationCast); + } + + inline timestamp_t frameTimeLocalAvg(Duration _durationCast = ::profiler::MICROSECONDS) { + return ::profiler::main_thread_frameTimeLocalAvg(_durationCast); + } + + /** Always returns true. + */ + inline bool isMain() { + return true; + } + + } // END of namespace main_thread. + + ////////////////////////////////////////////////////////////////////// + +} // END of namespace profiler. + +#if defined ( __clang__ ) +# pragma clang diagnostic pop +#endif + +#endif // EASY_PROFILER_H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler_aux.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler_aux.h new file mode 100644 index 0000000000..9236094bb4 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler_aux.h @@ -0,0 +1,203 @@ +/************************************************************************ +* file name : profiler_aux.h +* ----------------- : +* creation time : 2016/06/11 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : This file contains auxiliary profiler macros and funcitons. +* ----------------- : +* change log : * 2016/06/11 Victor Zarubkin: Moved sources from profiler.h +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_PROFILER_AUX_H +#define EASY_PROFILER_AUX_H + +#include + +#include +#include + +////////////////////////////////////////////////////////////////////////// + +namespace profiler { + + enum EasyBlockStatus : uint8_t { + OFF = 0, ///< The block is OFF + ON = 1, ///< The block is ON (but if it's parent block is off recursively then this block will be off too) + FORCE_ON = ON | 2, ///< The block is ALWAYS ON (even if it's parent has turned off all children) + OFF_RECURSIVE = 4, ///< The block is OFF and all of it's children by call-stack are also OFF. + ON_WITHOUT_CHILDREN = ON | OFF_RECURSIVE, ///< The block is ON but all of it's children are OFF. + FORCE_ON_WITHOUT_CHILDREN = FORCE_ON | OFF_RECURSIVE, ///< The block is ALWAYS ON but all of it's children are OFF. + }; + +} + +////////////////////////////////////////////////////////////////////////// + +#include +#include + +# define EASY_STRINGIFY(a) #a +# define EASY_STRINGIFICATION(a) EASY_STRINGIFY(a) +# define EASY_TOKEN_JOIN(x, y) x ## y +# define EASY_TOKEN_CONCATENATE(x, y) EASY_TOKEN_JOIN(x, y) +# define EASY_UNIQUE_BLOCK(x) EASY_TOKEN_CONCATENATE(unique_profiler_mark_name_, x) +# define EASY_UNIQUE_FRAME_COUNTER(x) EASY_TOKEN_CONCATENATE(unique_profiler_frame_mark_name_, x) +# define EASY_UNIQUE_DESC(x) EASY_TOKEN_CONCATENATE(unique_profiler_descriptor_, x) + +#ifdef BUILD_WITH_EASY_PROFILER + +namespace profiler { + + template struct NameSwitch; + + class ForceConstStr EASY_FINAL { + friend NameSwitch; + friend NameSwitch; + + const char* c_str; + + ForceConstStr() = delete; + ForceConstStr(const ForceConstStr&) = delete; + ForceConstStr(ForceConstStr&&) = delete; + ForceConstStr& operator = (const ForceConstStr&) = delete; + ForceConstStr& operator = (ForceConstStr&&) = delete; + + public: + + ForceConstStr(const char* _str) : c_str(_str) {} + ForceConstStr(const ::std::string& _str) : c_str(_str.c_str()) {} + }; + + template struct NameSwitch EASY_FINAL { + static const char* runtime_name(const char* name) { return name; } + static const char* runtime_name(const ::std::string& name) { return name.c_str(); } + static const char* runtime_name(const ForceConstStr&) { return ""; } + + template + static const char* compiletime_name(const T&, const char* autoGeneratedName) { return autoGeneratedName; } + static const char* compiletime_name(const char*, const char* autoGeneratedName) { return autoGeneratedName; } + static const char* compiletime_name(const ForceConstStr& name, const char*) { return name.c_str; } + }; + + template <> struct NameSwitch EASY_FINAL { + static const char* runtime_name(const char*) { return ""; } + static const char* runtime_name(const ::std::string& name) { return name.c_str(); } + static const char* runtime_name(const ForceConstStr&) { return ""; } + + template + static const char* compiletime_name(const T&, const char* autoGeneratedName) { return autoGeneratedName; } + static const char* compiletime_name(const char* name, const char*) { return name; } + static const char* compiletime_name(const ForceConstStr& name, const char*) { return name.c_str; } + }; + + //*********************************************** + + inline color_t extract_color() { + return ::profiler::colors::Default; + } + + template + inline color_t extract_color(::profiler::EasyBlockStatus, TArgs...) { + return ::profiler::colors::Default; + } + + template + inline color_t extract_color(color_t _color, TArgs...) { + return _color; + } + + template + inline color_t extract_color(T, color_t _color, TArgs...) { + return _color; + } + + template + inline color_t extract_color(TArgs...) { + static_assert(sizeof...(TArgs) < 2, "No profiler::color_t in arguments list for EASY_BLOCK(name, ...)!"); + return ::profiler::colors::Default; + } + + //*********************************************** + + inline EasyBlockStatus extract_enable_flag() { + return ::profiler::ON; + } + + template + inline EasyBlockStatus extract_enable_flag(T, ::profiler::EasyBlockStatus _flag, TArgs...) { + return _flag; + } + + template + inline EasyBlockStatus extract_enable_flag(::profiler::EasyBlockStatus _flag, TArgs...) { + return _flag; + } + + template + inline EasyBlockStatus extract_enable_flag(TArgs...) { + static_assert(sizeof...(TArgs) < 2, "No EasyBlockStatus in arguments list for EASY_BLOCK(name, ...)!"); + return ::profiler::ON; + } + + //*********************************************** + +} // END of namespace profiler. + +# define EASY_UNIQUE_LINE_ID __FILE__ ":" EASY_STRINGIFICATION(__LINE__) +# define EASY_COMPILETIME_NAME(name) ::profiler::NameSwitch<::std::is_reference::value>::compiletime_name(name, EASY_UNIQUE_LINE_ID) +# define EASY_RUNTIME_NAME(name) ::profiler::NameSwitch<::std::is_reference::value>::runtime_name(name) +# define EASY_CONST_NAME(name) ::profiler::ForceConstStr(name) + +#else + +# define EASY_CONST_NAME(name) + +#endif // BUILD_WITH_EASY_PROFILER + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_PROFILER_AUX_H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler_colors.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler_colors.h new file mode 100644 index 0000000000..c40e0715fa --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/profiler_colors.h @@ -0,0 +1,412 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin + +Licensed under either of + * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +at your option. + +The MIT License + 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. + + +The Apache License, Version 2.0 (the "License"); + You may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +**/ + +#ifndef EASY_PROFILER_COLORS_H +#define EASY_PROFILER_COLORS_H + +#include + +////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////// + +namespace profiler { + + typedef uint32_t color_t; // Standard four-byte ARGB color format + + namespace colors { + + ///< Change alpha for color. Only 8 major bytes (0xff000000) used from alpha. + inline color_t modify_alpha32(color_t _color, color_t _alpha) { + return (_alpha & 0xff000000) | (_color & 0x00ffffff); + } + + ///< Change alpha for color. + inline color_t modify_alpha8(color_t _color, uint8_t _alpha) { + return (static_cast(_alpha) << 24) | (_color & 0x00ffffff); + } + + ///< Create color from ARGB components. + inline color_t color(uint8_t _red, uint8_t _green, uint8_t _blue, uint8_t _alpha = 0xff) { + return (static_cast(_alpha) << 24) | (static_cast(_red) << 16) | (static_cast(_green) << 8) | static_cast(_blue); + } + +#if !defined(EASY_OPTION_BUILTIN_COLORS) || EASY_OPTION_BUILTIN_COLORS != 0 + // Google Material Design colors + // See https://material.google.com/style/color.html + + const color_t Red50 = 0xffffebee; + const color_t Red100 = 0xffffcdd2; + const color_t Red200 = 0xffef9a9a; + const color_t Red300 = 0xffe57373; + const color_t Red400 = 0xffef5350; + const color_t Red500 = 0xfff44336; + const color_t Red600 = 0xffe53935; + const color_t Red700 = 0xffd32f2f; + const color_t Red800 = 0xffc62828; + const color_t Red900 = 0xffb71c1c; + const color_t RedA100 = 0xffff8a80; + const color_t RedA200 = 0xffff5252; + const color_t RedA400 = 0xffff1744; + const color_t RedA700 = 0xffd50000; + + const color_t Pink50 = 0xfffce4ec; + const color_t Pink100 = 0xfff8bbd0; + const color_t Pink200 = 0xfff48fb1; + const color_t Pink300 = 0xfff06292; + const color_t Pink400 = 0xffec407a; + const color_t Pink500 = 0xffe91e63; + const color_t Pink600 = 0xffd81b60; + const color_t Pink700 = 0xffc2185b; + const color_t Pink800 = 0xffad1457; + const color_t Pink900 = 0xff880e4f; + const color_t PinkA100 = 0xffff80ab; + const color_t PinkA200 = 0xffff4081; + const color_t PinkA400 = 0xfff50057; + const color_t PinkA700 = 0xffc51162; + + const color_t Purple50 = 0xfff3e5f5; + const color_t Purple100 = 0xffe1bee7; + const color_t Purple200 = 0xffce93d8; + const color_t Purple300 = 0xffba68c8; + const color_t Purple400 = 0xffab47bc; + const color_t Purple500 = 0xff9c27b0; + const color_t Purple600 = 0xff8e24aa; + const color_t Purple700 = 0xff7b1fa2; + const color_t Purple800 = 0xff6a1b9a; + const color_t Purple900 = 0xff4a148c; + const color_t PurpleA100 = 0xffea80fc; + const color_t PurpleA200 = 0xffe040fb; + const color_t PurpleA400 = 0xffd500f9; + const color_t PurpleA700 = 0xffaa00ff; + + const color_t DeepPurple50 = 0xffede7f6; + const color_t DeepPurple100 = 0xffd1c4e9; + const color_t DeepPurple200 = 0xffb39ddb; + const color_t DeepPurple300 = 0xff9575cd; + const color_t DeepPurple400 = 0xff7e57c2; + const color_t DeepPurple500 = 0xff673ab7; + const color_t DeepPurple600 = 0xff5e35b1; + const color_t DeepPurple700 = 0xff512da8; + const color_t DeepPurple800 = 0xff4527a0; + const color_t DeepPurple900 = 0xff311b92; + const color_t DeepPurpleA100 = 0xffb388ff; + const color_t DeepPurpleA200 = 0xff7c4dff; + const color_t DeepPurpleA400 = 0xff651fff; + const color_t DeepPurpleA700 = 0xff6200ea; + + const color_t Indigo50 = 0xffe8eaf6; + const color_t Indigo100 = 0xffc5cae9; + const color_t Indigo200 = 0xff9fa8da; + const color_t Indigo300 = 0xff7986cb; + const color_t Indigo400 = 0xff5c6bc0; + const color_t Indigo500 = 0xff3f51b5; + const color_t Indigo600 = 0xff3949ab; + const color_t Indigo700 = 0xff303f9f; + const color_t Indigo800 = 0xff283593; + const color_t Indigo900 = 0xff1a237e; + const color_t IndigoA100 = 0xff8c9eff; + const color_t IndigoA200 = 0xff536dfe; + const color_t IndigoA400 = 0xff3d5afe; + const color_t IndigoA700 = 0xff304ffe; + + const color_t Blue50 = 0xffe3f2fd; + const color_t Blue100 = 0xffbbdefb; + const color_t Blue200 = 0xff90caf9; + const color_t Blue300 = 0xff64b5f6; + const color_t Blue400 = 0xff42a5f5; + const color_t Blue500 = 0xff2196f3; + const color_t Blue600 = 0xff1e88e5; + const color_t Blue700 = 0xff1976d2; + const color_t Blue800 = 0xff1565c0; + const color_t Blue900 = 0xff0d47a1; + const color_t BlueA100 = 0xff82b1ff; + const color_t BlueA200 = 0xff448aff; + const color_t BlueA400 = 0xff2979ff; + const color_t BlueA700 = 0xff2962ff; + + const color_t LightBlue50 = 0xffe1f5fe; + const color_t LightBlue100 = 0xffb3e5fc; + const color_t LightBlue200 = 0xff81d4fa; + const color_t LightBlue300 = 0xff4fc3f7; + const color_t LightBlue400 = 0xff29b6f6; + const color_t LightBlue500 = 0xff03a9f4; + const color_t LightBlue600 = 0xff039be5; + const color_t LightBlue700 = 0xff0288d1; + const color_t LightBlue800 = 0xff0277bd; + const color_t LightBlue900 = 0xff01579b; + const color_t LightBlueA100 = 0xff80d8ff; + const color_t LightBlueA200 = 0xff40c4ff; + const color_t LightBlueA400 = 0xff00b0ff; + const color_t LightBlueA700 = 0xff0091ea; + + const color_t Cyan50 = 0xffe0f7fa; + const color_t Cyan100 = 0xffb2ebf2; + const color_t Cyan200 = 0xff80deea; + const color_t Cyan300 = 0xff4dd0e1; + const color_t Cyan400 = 0xff26c6da; + const color_t Cyan500 = 0xff00bcd4; + const color_t Cyan600 = 0xff00acc1; + const color_t Cyan700 = 0xff0097a7; + const color_t Cyan800 = 0xff00838f; + const color_t Cyan900 = 0xff006064; + const color_t CyanA100 = 0xff84ffff; + const color_t CyanA200 = 0xff18ffff; + const color_t CyanA400 = 0xff00e5ff; + const color_t CyanA700 = 0xff00b8d4; + + const color_t Teal50 = 0xffe0f2f1; + const color_t Teal100 = 0xffb2dfdb; + const color_t Teal200 = 0xff80cbc4; + const color_t Teal300 = 0xff4db6ac; + const color_t Teal400 = 0xff26a69a; + const color_t Teal500 = 0xff009688; + const color_t Teal600 = 0xff00897b; + const color_t Teal700 = 0xff00796b; + const color_t Teal800 = 0xff00695c; + const color_t Teal900 = 0xff004d40; + const color_t TealA100 = 0xffa7ffeb; + const color_t TealA200 = 0xff64ffda; + const color_t TealA400 = 0xff1de9b6; + const color_t TealA700 = 0xff00bfa5; + + const color_t Green50 = 0xffe8f5e9; + const color_t Green100 = 0xffc8e6c9; + const color_t Green200 = 0xffa5d6a7; + const color_t Green300 = 0xff81c784; + const color_t Green400 = 0xff66bb6a; + const color_t Green500 = 0xff4caf50; + const color_t Green600 = 0xff43a047; + const color_t Green700 = 0xff388e3c; + const color_t Green800 = 0xff2e7d32; + const color_t Green900 = 0xff1b5e20; + const color_t GreenA100 = 0xffb9f6ca; + const color_t GreenA200 = 0xff69f0ae; + const color_t GreenA400 = 0xff00e676; + const color_t GreenA700 = 0xff00c853; + + const color_t LightGreen50 = 0xfff1f8e9; + const color_t LightGreen100 = 0xffdcedc8; + const color_t LightGreen200 = 0xffc5e1a5; + const color_t LightGreen300 = 0xffaed581; + const color_t LightGreen400 = 0xff9ccc65; + const color_t LightGreen500 = 0xff8bc34a; + const color_t LightGreen600 = 0xff7cb342; + const color_t LightGreen700 = 0xff689f38; + const color_t LightGreen800 = 0xff558b2f; + const color_t LightGreen900 = 0xff33691e; + const color_t LightGreenA100 = 0xffccff90; + const color_t LightGreenA200 = 0xffb2ff59; + const color_t LightGreenA400 = 0xff76ff03; + const color_t LightGreenA700 = 0xff64dd17; + + const color_t Lime50 = 0xfff9ebe7; + const color_t Lime100 = 0xfff0f4c3; + const color_t Lime200 = 0xffe6ee9c; + const color_t Lime300 = 0xffdce775; + const color_t Lime400 = 0xffd4e157; + const color_t Lime500 = 0xffcddc39; + const color_t Lime600 = 0xffc0ca33; + const color_t Lime700 = 0xffafb42b; + const color_t Lime800 = 0xff9e9d24; + const color_t Lime900 = 0xff827717; + const color_t LimeA100 = 0xfff4ff81; + const color_t LimeA200 = 0xffeeff41; + const color_t LimeA400 = 0xffc6ff00; + const color_t LimeA700 = 0xffaeea00; + + const color_t Yellow50 = 0xfffffde7; + const color_t Yellow100 = 0xfffff9c4; + const color_t Yellow200 = 0xfffff59d; + const color_t Yellow300 = 0xfffff176; + const color_t Yellow400 = 0xffffee58; + const color_t Yellow500 = 0xffffeb3b; + const color_t Yellow600 = 0xfffdd835; + const color_t Yellow700 = 0xfffbc02d; + const color_t Yellow800 = 0xfff9a825; + const color_t Yellow900 = 0xfff57f17; + const color_t YellowA100 = 0xffffff8d; + const color_t YellowA200 = 0xffffff00; + const color_t YellowA400 = 0xffffea00; + const color_t YellowA700 = 0xffffd600; + + const color_t Amber50 = 0xfffff8e1; + const color_t Amber100 = 0xffffecb3; + const color_t Amber200 = 0xffffe082; + const color_t Amber300 = 0xffffd54f; + const color_t Amber400 = 0xffffca28; + const color_t Amber500 = 0xffffc107; + const color_t Amber600 = 0xffffb300; + const color_t Amber700 = 0xffffa000; + const color_t Amber800 = 0xffff8f00; + const color_t Amber900 = 0xffff6f00; + const color_t AmberA100 = 0xffffe57f; + const color_t AmberA200 = 0xffffd740; + const color_t AmberA400 = 0xffffc400; + const color_t AmberA700 = 0xffffab00; + + const color_t Orange50 = 0xfffff3e0; + const color_t Orange100 = 0xffffe0b2; + const color_t Orange200 = 0xffffcc80; + const color_t Orange300 = 0xffffb74d; + const color_t Orange400 = 0xffffa726; + const color_t Orange500 = 0xffff9800; + const color_t Orange600 = 0xfffb8c00; + const color_t Orange700 = 0xfff57c00; + const color_t Orange800 = 0xffef6c00; + const color_t Orange900 = 0xffe65100; + const color_t OrangeA100 = 0xffffd180; + const color_t OrangeA200 = 0xffffab40; + const color_t OrangeA400 = 0xffff9100; + const color_t OrangeA700 = 0xffff6d00; + + const color_t DeepOrange50 = 0xfffbe9e7; + const color_t DeepOrange100 = 0xffffccbc; + const color_t DeepOrange200 = 0xffffab91; + const color_t DeepOrange300 = 0xffff8a65; + const color_t DeepOrange400 = 0xffff7043; + const color_t DeepOrange500 = 0xffff5722; + const color_t DeepOrange600 = 0xfff4511e; + const color_t DeepOrange700 = 0xffe64a19; + const color_t DeepOrange800 = 0xffd84315; + const color_t DeepOrange900 = 0xffbf360c; + const color_t DeepOrangeA100 = 0xffff9e80; + const color_t DeepOrangeA200 = 0xffff6e40; + const color_t DeepOrangeA400 = 0xffff3d00; + const color_t DeepOrangeA700 = 0xffdd2c00; + + const color_t Brown50 = 0xffefebe9; + const color_t Brown100 = 0xffd7ccc8; + const color_t Brown200 = 0xffbcaaa4; + const color_t Brown300 = 0xffa1887f; + const color_t Brown400 = 0xff8d6e63; + const color_t Brown500 = 0xff795548; + const color_t Brown600 = 0xff6d4c41; + const color_t Brown700 = 0xff5d4037; + const color_t Brown800 = 0xff4e342e; + const color_t Brown900 = 0xff3e2723; + + const color_t Grey50 = 0xfffafafa; + const color_t Grey100 = 0xfff5f5f5; + const color_t Grey200 = 0xffeeeeee; + const color_t Grey300 = 0xffe0e0e0; + const color_t Grey400 = 0xffbdbdbd; + const color_t Grey500 = 0xff9e9e9e; + const color_t Grey600 = 0xff757575; + const color_t Grey700 = 0xff616161; + const color_t Grey800 = 0xff424242; + const color_t Grey900 = 0xff212121; + + const color_t BlueGrey50 = 0xffeceff1; + const color_t BlueGrey100 = 0xffcfd8dc; + const color_t BlueGrey200 = 0xffb0bec5; + const color_t BlueGrey300 = 0xff90a4ae; + const color_t BlueGrey400 = 0xff78909c; + const color_t BlueGrey500 = 0xff607d8b; + const color_t BlueGrey600 = 0xff546e7a; + const color_t BlueGrey700 = 0xff455a64; + const color_t BlueGrey800 = 0xff37474f; + const color_t BlueGrey900 = 0xff263238; + + const color_t Black = 0xff000000; + const color_t White = 0xffffffff; + const color_t Null = 0x00000000; + + + const color_t Red = Red500; + const color_t DarkRed = Red900; + const color_t Coral = Red200; + const color_t RichRed = 0xffff0000; + const color_t Pink = Pink500; + const color_t Rose = PinkA100; + const color_t Purple = Purple500; + const color_t Magenta = PurpleA200; + const color_t DarkMagenta = PurpleA700; + const color_t DeepPurple = DeepPurple500; + const color_t Indigo = Indigo500; + const color_t Blue = Blue500; + const color_t DarkBlue = Blue900; + const color_t RichBlue = 0xff0000ff; + const color_t LightBlue = LightBlue500; + const color_t SkyBlue = LightBlueA100; + const color_t Navy = LightBlue800; + const color_t Cyan = Cyan500; + const color_t DarkCyan = Cyan900; + const color_t Teal = Teal500; + const color_t DarkTeal = Teal900; + const color_t Green = Green500; + const color_t DarkGreen = Green900; + const color_t RichGreen = 0xff00ff00; + const color_t LightGreen = LightGreen500; + const color_t Mint = LightGreen900; + const color_t Lime = Lime500; + const color_t Olive = Lime900; + const color_t Yellow = Yellow500; + const color_t RichYellow = YellowA200; + const color_t Amber = Amber500; + const color_t Gold = Amber300; + const color_t PaleGold = AmberA100; + const color_t Orange = Orange500; + const color_t Skin = Orange100; + const color_t DeepOrange = DeepOrange500; + const color_t Brick = DeepOrange900; + const color_t Brown = Brown500; + const color_t DarkBrown = Brown900; + const color_t CreamWhite = Orange50; + const color_t Wheat = Amber100; + const color_t Grey = Grey500; + const color_t Dark = Grey900; + const color_t Silver = Grey300; + const color_t BlueGrey = BlueGrey500; + + const color_t Default = Wheat; +#else + const color_t Default = 0xffffecb3; +#endif // #if !defined(EASY_OPTION_BUILTIN_COLORS) || EASY_OPTION_BUILTIN_COLORS == 0 + + } // END of namespace colors. + +} // END of namespace profiler. + +////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////// + +#endif // EASY_PROFILER_COLORS_H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/reader.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/reader.h new file mode 100644 index 0000000000..25d905d7cd --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/reader.h @@ -0,0 +1,445 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin + +Licensed under either of + * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +at your option. + +The MIT License + 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. + + +The Apache License, Version 2.0 (the "License"); + You may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +**/ + +#ifndef PROFILER_READER____H +#define PROFILER_READER____H + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include + +#include +#include + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace profiler { + + typedef uint32_t calls_number_t; + typedef uint32_t block_index_t; + + template + struct hash : public ::std::hash { + using ::std::hash::operator(); + }; + + template + struct hash { + inline size_t operator () (T _value) const { + return static_cast(_value); + } + }; + + template + struct passthrough_hash : public hash sizeof(size_t))> { + using hash sizeof(size_t))>::operator(); + }; + +#pragma pack(push, 1) + struct BlockStatistics EASY_FINAL + { + ::profiler::timestamp_t total_duration; ///< Total duration of all block calls + ::profiler::timestamp_t total_children_duration; ///< Total duration of all children of all block calls + ::profiler::block_index_t min_duration_block; ///< Will be used in GUI to jump to the block with min duration + ::profiler::block_index_t max_duration_block; ///< Will be used in GUI to jump to the block with max duration + ::profiler::block_index_t parent_block; ///< Index of block which is "parent" for "per_parent_stats" or "frame" for "per_frame_stats" or thread-id for "per_thread_stats" + ::profiler::calls_number_t calls_number; ///< Block calls number + + explicit BlockStatistics(::profiler::timestamp_t _duration, ::profiler::block_index_t _block_index, ::profiler::block_index_t _parent_index) + : total_duration(_duration) + , total_children_duration(0) + , min_duration_block(_block_index) + , max_duration_block(_block_index) + , parent_block(_parent_index) + , calls_number(1) + { + } + + //BlockStatistics() = default; + + inline ::profiler::timestamp_t average_duration() const + { + return total_duration / calls_number; + } + + }; // END of struct BlockStatistics. +#pragma pack(pop) + + extern "C" PROFILER_API void release_stats(BlockStatistics*& _stats); + + ////////////////////////////////////////////////////////////////////////// + + class BlocksTree EASY_FINAL + { + typedef BlocksTree This; + + public: + + typedef ::std::vector blocks_t; + typedef ::std::vector<::profiler::block_index_t> children_t; + + children_t children; ///< List of children blocks. May be empty. + + union { + ::profiler::SerializedBlock* node; ///< Pointer to serilized data for regular block (id, name, begin, end etc.) + ::profiler::SerializedCSwitch* cs; ///< Pointer to serilized data for context switch (thread_id, name, begin, end etc.) + }; + + ::profiler::BlockStatistics* per_parent_stats; ///< Pointer to statistics for this block within the parent (may be nullptr for top-level blocks) + ::profiler::BlockStatistics* per_frame_stats; ///< Pointer to statistics for this block within the frame (may be nullptr for top-level blocks) + ::profiler::BlockStatistics* per_thread_stats; ///< Pointer to statistics for this block within the bounds of all frames per current thread + uint8_t depth; ///< Maximum number of sublevels (maximum children depth) + + BlocksTree() + : node(nullptr) + , per_parent_stats(nullptr) + , per_frame_stats(nullptr) + , per_thread_stats(nullptr) + , depth(0) + { + + } + + BlocksTree(This&& that) : BlocksTree() + { + make_move(::std::forward(that)); + } + + This& operator = (This&& that) + { + make_move(::std::forward(that)); + return *this; + } + + ~BlocksTree() + { + release_stats(per_thread_stats); + release_stats(per_parent_stats); + release_stats(per_frame_stats); + } + + bool operator < (const This& other) const + { + if (!node || !other.node) + return false; + return node->begin() < other.node->begin(); + } + + void shrink_to_fit() + { + //for (auto& child : children) + // child.shrink_to_fit(); + + // shrink version 1: + //children.shrink_to_fit(); + + // shrink version 2: + //children_t new_children; + //new_children.reserve(children.size()); + //::std::move(children.begin(), children.end(), ::std::back_inserter(new_children)); + //new_children.swap(children); + } + + private: + + BlocksTree(const This&) = delete; + This& operator = (const This&) = delete; + + void make_move(This&& that) + { + if (per_thread_stats != that.per_thread_stats) + release_stats(per_thread_stats); + + if (per_parent_stats != that.per_parent_stats) + release_stats(per_parent_stats); + + if (per_frame_stats != that.per_frame_stats) + release_stats(per_frame_stats); + + children = ::std::move(that.children); + node = that.node; + per_parent_stats = that.per_parent_stats; + per_frame_stats = that.per_frame_stats; + per_thread_stats = that.per_thread_stats; + depth = that.depth; + + that.node = nullptr; + that.per_parent_stats = nullptr; + that.per_frame_stats = nullptr; + that.per_thread_stats = nullptr; + } + + }; // END of class BlocksTree. + + ////////////////////////////////////////////////////////////////////////// + + class BlocksTreeRoot EASY_FINAL + { + typedef BlocksTreeRoot This; + + public: + + BlocksTree::children_t children; ///< List of children indexes + BlocksTree::children_t sync; ///< List of context-switch events + BlocksTree::children_t events; ///< List of events indexes + std::string thread_name; ///< Name of this thread + ::profiler::timestamp_t profiled_time; ///< Profiled time of this thread (sum of all children duration) + ::profiler::timestamp_t wait_time; ///< Wait time of this thread (sum of all context switches) + ::profiler::thread_id_t thread_id; ///< System Id of this thread + ::profiler::block_index_t frames_number; ///< Total frames number (top-level blocks) + ::profiler::block_index_t blocks_number; ///< Total blocks number including their children + uint8_t depth; ///< Maximum stack depth (number of levels) + + BlocksTreeRoot() : profiled_time(0), wait_time(0), thread_id(0), frames_number(0), blocks_number(0), depth(0) + { + } + + BlocksTreeRoot(This&& that) + : children(::std::move(that.children)) + , sync(::std::move(that.sync)) + , events(::std::move(that.events)) + , thread_name(::std::move(that.thread_name)) + , profiled_time(that.profiled_time) + , wait_time(that.wait_time) + , thread_id(that.thread_id) + , frames_number(that.frames_number) + , blocks_number(that.blocks_number) + , depth(that.depth) + { + } + + This& operator = (This&& that) + { + children = ::std::move(that.children); + sync = ::std::move(that.sync); + events = ::std::move(that.events); + thread_name = ::std::move(that.thread_name); + profiled_time = that.profiled_time; + wait_time = that.wait_time; + thread_id = that.thread_id; + frames_number = that.frames_number; + blocks_number = that.blocks_number; + depth = that.depth; + return *this; + } + + inline bool got_name() const + { + return !thread_name.empty(); + } + + inline const char* name() const + { + return thread_name.c_str(); + } + + bool operator < (const This& other) const + { + return thread_id < other.thread_id; + } + + private: + + BlocksTreeRoot(const This&) = delete; + This& operator = (const This&) = delete; + + }; // END of class BlocksTreeRoot. + + typedef ::profiler::BlocksTree::blocks_t blocks_t; + + typedef ::std::unordered_map<::profiler::thread_id_t, ::profiler::BlocksTreeRoot, ::profiler::passthrough_hash<::profiler::thread_id_t> > thread_blocks_tree_t; + + ////////////////////////////////////////////////////////////////////////// + + class PROFILER_API SerializedData EASY_FINAL + { + char* m_data; + size_t m_size; + + public: + + SerializedData() : m_data(nullptr), m_size(0) + { + } + + SerializedData(SerializedData&& that) : m_data(that.m_data), m_size(that.m_size) + { + that.m_data = nullptr; + that.m_size = 0; + } + + ~SerializedData() + { + clear(); + } + + void set(uint64_t _size); + void extend(uint64_t _size); + + SerializedData& operator = (SerializedData&& that) + { + set(that.m_data, that.m_size); + that.m_data = nullptr; + that.m_size = 0; + return *this; + } + + char* operator [] (uint64_t i) + { + return m_data + i; + } + + const char* operator [] (uint64_t i) const + { + return m_data + i; + } + + bool empty() const + { + return m_size == 0; + } + + uint64_t size() const + { + return m_size; + } + + char* data() + { + return m_data; + } + + const char* data() const + { + return m_data; + } + + void clear() + { + set(nullptr, 0); + } + + void swap(SerializedData& other) + { + char* d = other.m_data; + uint64_t sz = other.m_size; + + other.m_data = m_data; + other.m_size = m_size; + + m_data = d; + m_size = sz; + } + + private: + + void set(char* _data, uint64_t _size); + + SerializedData(const SerializedData&) = delete; + SerializedData& operator = (const SerializedData&) = delete; + + }; // END of class SerializedData. + + ////////////////////////////////////////////////////////////////////////// + + typedef ::std::vector descriptors_list_t; + +} // END of namespace profiler. + +extern "C" { + + PROFILER_API ::profiler::block_index_t fillTreesFromFile(::std::atomic& progress, const char* filename, + ::profiler::SerializedData& serialized_blocks, + ::profiler::SerializedData& serialized_descriptors, + ::profiler::descriptors_list_t& descriptors, + ::profiler::blocks_t& _blocks, + ::profiler::thread_blocks_tree_t& threaded_trees, + uint32_t& total_descriptors_number, + uint32_t& version, + bool gather_statistics, + ::std::stringstream& _log); + + PROFILER_API ::profiler::block_index_t fillTreesFromStream(::std::atomic& progress, ::std::stringstream& str, + ::profiler::SerializedData& serialized_blocks, + ::profiler::SerializedData& serialized_descriptors, + ::profiler::descriptors_list_t& descriptors, + ::profiler::blocks_t& _blocks, + ::profiler::thread_blocks_tree_t& threaded_trees, + uint32_t& total_descriptors_number, + uint32_t& version, + bool gather_statistics, + ::std::stringstream& _log); + + PROFILER_API bool readDescriptionsFromStream(::std::atomic& progress, ::std::stringstream& str, + ::profiler::SerializedData& serialized_descriptors, + ::profiler::descriptors_list_t& descriptors, + ::std::stringstream& _log); +} + +inline ::profiler::block_index_t fillTreesFromFile(const char* filename, ::profiler::SerializedData& serialized_blocks, + ::profiler::SerializedData& serialized_descriptors, + ::profiler::descriptors_list_t& descriptors, ::profiler::blocks_t& _blocks, + ::profiler::thread_blocks_tree_t& threaded_trees, + uint32_t& total_descriptors_number, + uint32_t& version, + bool gather_statistics, + ::std::stringstream& _log) +{ + ::std::atomic progress = ATOMIC_VAR_INIT(0); + return fillTreesFromFile(progress, filename, serialized_blocks, serialized_descriptors, descriptors, _blocks, threaded_trees, total_descriptors_number, version, gather_statistics, _log); +} + +inline bool readDescriptionsFromStream(::std::stringstream& str, + ::profiler::SerializedData& serialized_descriptors, + ::profiler::descriptors_list_t& descriptors, + ::std::stringstream& _log) +{ + ::std::atomic progress = ATOMIC_VAR_INIT(0); + return readDescriptionsFromStream(progress, str, serialized_descriptors, descriptors, _log); +} + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#endif // PROFILER_READER____H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/serialized_block.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/serialized_block.h new file mode 100644 index 0000000000..69e0d40565 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/include/easy/serialized_block.h @@ -0,0 +1,141 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin + +Licensed under either of + * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +at your option. + +The MIT License + 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. + + +The Apache License, Version 2.0 (the "License"); + You may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +**/ + +#ifndef EASY_PROFILER_SERIALIZED_BLOCK_H +#define EASY_PROFILER_SERIALIZED_BLOCK_H + +#include + +class CSwitchBlock; + +namespace profiler { + + ////////////////////////////////////////////////////////////////////////// + + class PROFILER_API SerializedBlock EASY_FINAL : public BaseBlockData + { + friend ::ProfileManager; + friend ::ThreadStorage; + + public: + + inline const char* data() const { return reinterpret_cast(this); } + + ///< Run-time block name is stored right after main BaseBlockData data + inline const char* name() const { return data() + sizeof(BaseBlockData); } + + private: + + SerializedBlock(const Block& block, uint16_t name_length); + + SerializedBlock(const SerializedBlock&) = delete; + SerializedBlock& operator = (const SerializedBlock&) = delete; + ~SerializedBlock() = delete; + + }; // END of SerializedBlock. + + ////////////////////////////////////////////////////////////////////////// + + class PROFILER_API SerializedCSwitch EASY_FINAL : public CSwitchEvent + { + friend ::ProfileManager; + friend ::ThreadStorage; + + public: + + inline const char* data() const { return reinterpret_cast(this); } + + ///< Run-time block name is stored right after main CSwitchEvent data + inline const char* name() const { return data() + sizeof(CSwitchEvent); } + + private: + + SerializedCSwitch(const CSwitchBlock& block, uint16_t name_length); + + SerializedCSwitch(const SerializedCSwitch&) = delete; + SerializedCSwitch& operator = (const SerializedCSwitch&) = delete; + ~SerializedCSwitch() = delete; + + }; // END of SerializedCSwitch. + + ////////////////////////////////////////////////////////////////////////// + +#pragma pack(push, 1) + class PROFILER_API SerializedBlockDescriptor EASY_FINAL : public BaseBlockDescriptor + { + uint16_t m_nameLength; ///< Length of the name including trailing '\0' sybmol + + public: + + inline const char* data() const { + return reinterpret_cast(this); + } + + ///< Name is stored right after m_nameLength + inline const char* name() const { + static const auto shift = sizeof(BaseBlockDescriptor) + sizeof(decltype(m_nameLength)); + return data() + shift; + } + + ///< File name is stored right after the name + inline const char* file() const { + return name() + m_nameLength; + } + + inline void setStatus(EasyBlockStatus _status) { + m_status = _status; + } + + private: + + SerializedBlockDescriptor(const SerializedBlockDescriptor&) = delete; + SerializedBlockDescriptor& operator = (const SerializedBlockDescriptor&) = delete; + ~SerializedBlockDescriptor() = delete; + + }; // END of SerializedBlockDescriptor. +#pragma pack(pop) + + ////////////////////////////////////////////////////////////////////////// + +} // END of namespace profiler. + +#endif // EASY_PROFILER_SERIALIZED_BLOCK_H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/outstream.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/outstream.h new file mode 100644 index 0000000000..f27762d33e --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/outstream.h @@ -0,0 +1,115 @@ +/************************************************************************ +* file name : outstream.h +* ----------------- : +* creation time : 2016/09/11 +* authors : Sergey Yagovtsev, Victor Zarubkin +* emails : yse.sey@gmail.com, v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains definition of output stream helpers. +* ----------------- : +* change log : * 2016/09/11 Victor Zarubkin: Initial commit. Moved sources from profiler_manager.h/.cpp +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_PROFILER__OUTPUT_STREAM__H_ +#define EASY_PROFILER__OUTPUT_STREAM__H_ + +#include +#include + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +namespace profiler { + + class OStream + { + ::std::stringstream m_stream; + + public: + + explicit OStream() : m_stream(std::ios_base::out | std::ios_base::binary) + { + + } + + template void write(const char* _data, T _size) + { + m_stream.write(_data, _size); + } + + template void write(const T& _data) + { + m_stream.write((const char*)&_data, sizeof(T)); + } + + ::std::stringstream& stream() + { + return m_stream; + } + + const ::std::stringstream& stream() const + { + return m_stream; + } + + void clear() + { +#if defined(__GNUC__) && __GNUC__ < 5 + // gcc 4 has a known bug which has been solved in gcc 5: + // std::stringstream has no swap() method :( + m_stream.str(::std::string()); +#else + ::std::stringstream().swap(m_stream); +#endif + } + + }; // END of class OStream. + +} // END of namespace profiler. + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_PROFILER__OUTPUT_STREAM__H_ diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/profile_manager.cpp b/Source/ThirdParty/easy_profiler/easy_profiler_core/profile_manager.cpp new file mode 100644 index 0000000000..73947e6363 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/profile_manager.cpp @@ -0,0 +1,1953 @@ +/************************************************************************ +* file name : profile_manager.cpp +* ----------------- : +* creation time : 2016/02/16 +* authors : Sergey Yagovtsev, Victor Zarubkin +* emails : yse.sey@gmail.com, v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of Profile manager and implement access c-function +* : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include +#include +#include +#include "profile_manager.h" + +#include +#include +#include + +#include "event_trace_win.h" +#include "current_time.h" + +#ifdef __APPLE__ +#include +#include +#endif + +#if EASY_OPTION_LOG_ENABLED != 0 +# include + +# ifndef EASY_ERRORLOG +# define EASY_ERRORLOG ::std::cerr +# endif + +# ifndef EASY_LOG +# define EASY_LOG ::std::cerr +# endif + +# ifndef EASY_ERROR +# define EASY_ERROR(LOG_MSG) EASY_ERRORLOG << "EasyProfiler ERROR: " << LOG_MSG +# endif + +# ifndef EASY_WARNING +# define EASY_WARNING(LOG_MSG) EASY_ERRORLOG << "EasyProfiler WARNING: " << LOG_MSG +# endif + +# ifndef EASY_LOGMSG +# define EASY_LOGMSG(LOG_MSG) EASY_LOG << "EasyProfiler INFO: " << LOG_MSG +# endif + +# ifndef EASY_LOG_ONLY +# define EASY_LOG_ONLY(CODE) CODE +# endif + +#else + +# ifndef EASY_ERROR +# define EASY_ERROR(LOG_MSG) +# endif + +# ifndef EASY_WARNING +# define EASY_WARNING(LOG_MSG) +# endif + +# ifndef EASY_LOGMSG +# define EASY_LOGMSG(LOG_MSG) +# endif + +# ifndef EASY_LOG_ONLY +# define EASY_LOG_ONLY(CODE) +# endif + +#endif + +#ifdef min +#undef min +#endif + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +using namespace profiler; + +////////////////////////////////////////////////////////////////////////// + +#if !defined(EASY_PROFILER_VERSION_MAJOR) || !defined(EASY_PROFILER_VERSION_MINOR) || !defined(EASY_PROFILER_VERSION_PATCH) +# ifdef _WIN32 +# error EASY_PROFILER_VERSION_MAJOR and EASY_PROFILER_VERSION_MINOR and EASY_PROFILER_VERSION_PATCH macros must be defined +# else +# error "EASY_PROFILER_VERSION_MAJOR and EASY_PROFILER_VERSION_MINOR and EASY_PROFILER_VERSION_PATCH macros must be defined" +# endif +#endif + +# define EASY_PROFILER_PRODUCT_VERSION "v" EASY_STRINGIFICATION(EASY_PROFILER_VERSION_MAJOR) "." \ + EASY_STRINGIFICATION(EASY_PROFILER_VERSION_MINOR) "." \ + EASY_STRINGIFICATION(EASY_PROFILER_VERSION_PATCH) + +# define EASY_VERSION_INT(v_major, v_minor, v_patch) ((static_cast(v_major) << 24) | (static_cast(v_minor) << 16) | static_cast(v_patch)) +extern const uint32_t PROFILER_SIGNATURE = ('E' << 24) | ('a' << 16) | ('s' << 8) | 'y'; +extern const uint32_t EASY_CURRENT_VERSION = EASY_VERSION_INT(EASY_PROFILER_VERSION_MAJOR, EASY_PROFILER_VERSION_MINOR, EASY_PROFILER_VERSION_PATCH); +# undef EASY_VERSION_INT + +////////////////////////////////////////////////////////////////////////// + +# define EASY_PROF_DISABLED 0 +# define EASY_PROF_ENABLED 1 +# define EASY_PROF_DUMP 2 + +////////////////////////////////////////////////////////////////////////// + +//auto& MANAGER = ProfileManager::instance(); +# define MANAGER ProfileManager::instance() +const uint8_t FORCE_ON_FLAG = profiler::FORCE_ON & ~profiler::ON; + +#if defined(EASY_CHRONO_CLOCK) +const int64_t CPU_FREQUENCY = EASY_CHRONO_CLOCK::period::den / EASY_CHRONO_CLOCK::period::num; +# define TICKS_TO_US(ticks) ticks * 1000000LL / CPU_FREQUENCY +#elif defined(_WIN32) +const decltype(LARGE_INTEGER::QuadPart) CPU_FREQUENCY = ([](){ LARGE_INTEGER freq; QueryPerformanceFrequency(&freq); return freq.QuadPart; })(); +# define TICKS_TO_US(ticks) ticks * 1000000LL / CPU_FREQUENCY +#else +int64_t calculate_cpu_frequency() +{ + double g_TicksPerNanoSec; + uint64_t begin = 0, end = 0; +#ifdef __APPLE__ + clock_serv_t cclock; + mach_timespec_t begints, endts; + host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); + clock_get_time(cclock, &begints); +#else + struct timespec begints, endts; + clock_gettime(CLOCK_MONOTONIC, &begints); +#endif + begin = getCurrentTime(); + volatile uint64_t i; + for (i = 0; i < 100000000; i++); /* must be CPU intensive */ + end = getCurrentTime(); +#ifdef __APPLE__ + clock_get_time(cclock, &endts); + mach_port_deallocate(mach_task_self(), cclock); +#else + clock_gettime(CLOCK_MONOTONIC, &endts); +#endif + struct timespec tmpts; + const int NANO_SECONDS_IN_SEC = 1000000000; + tmpts.tv_sec = endts.tv_sec - begints.tv_sec; + tmpts.tv_nsec = endts.tv_nsec - begints.tv_nsec; + if (tmpts.tv_nsec < 0) + { + tmpts.tv_sec--; + tmpts.tv_nsec += NANO_SECONDS_IN_SEC; + } + + uint64_t nsecElapsed = tmpts.tv_sec * 1000000000LL + tmpts.tv_nsec; + g_TicksPerNanoSec = (double)(end - begin) / (double)nsecElapsed; + + int64_t cpu_frequency = int(g_TicksPerNanoSec * 1000000); + + return cpu_frequency; +} + +static std::atomic CPU_FREQUENCY = ATOMIC_VAR_INIT(1); +# define TICKS_TO_US(ticks) ticks * 1000 / CPU_FREQUENCY.load(std::memory_order_acquire) +#endif + +extern const profiler::color_t EASY_COLOR_INTERNAL_EVENT = 0xffffffff; // profiler::colors::White +const profiler::color_t EASY_COLOR_THREAD_END = 0xff212121; // profiler::colors::Dark +const profiler::color_t EASY_COLOR_START = 0xff4caf50; // profiler::colors::Green +const profiler::color_t EASY_COLOR_END = 0xfff44336; // profiler::colors::Red + +////////////////////////////////////////////////////////////////////////// + +EASY_THREAD_LOCAL static ::ThreadStorage* THIS_THREAD = nullptr; +EASY_THREAD_LOCAL static int32_t THIS_THREAD_STACK_SIZE = 0; +EASY_THREAD_LOCAL static profiler::timestamp_t THIS_THREAD_FRAME_T = 0ULL; +EASY_THREAD_LOCAL static bool THIS_THREAD_FRAME = false; +EASY_THREAD_LOCAL static bool THIS_THREAD_HALT = false; +EASY_THREAD_LOCAL static bool THIS_THREAD_IS_MAIN = false; + +EASY_THREAD_LOCAL static profiler::timestamp_t THIS_THREAD_FRAME_T_MAX = 0ULL; +EASY_THREAD_LOCAL static profiler::timestamp_t THIS_THREAD_FRAME_T_CUR = 0ULL; +EASY_THREAD_LOCAL static profiler::timestamp_t THIS_THREAD_FRAME_T_ACC = 0ULL; +EASY_THREAD_LOCAL static uint32_t THIS_THREAD_N_FRAMES = 0; +EASY_THREAD_LOCAL static bool THIS_THREAD_FRAME_T_RESET_MAX = false; +EASY_THREAD_LOCAL static bool THIS_THREAD_FRAME_T_RESET_AVG = false; + +////////////////////////////////////////////////////////////////////////// + +#ifdef BUILD_WITH_EASY_PROFILER +# define EASY_EVENT_RES(res, name, ...)\ + EASY_LOCAL_STATIC_PTR(const ::profiler::BaseBlockDescriptor*, EASY_UNIQUE_DESC(__LINE__), MANAGER.addBlockDescriptor(\ + ::profiler::extract_enable_flag(__VA_ARGS__), EASY_UNIQUE_LINE_ID, EASY_COMPILETIME_NAME(name),\ + __FILE__, __LINE__, ::profiler::BLOCK_TYPE_EVENT, ::profiler::extract_color(__VA_ARGS__)));\ + res = MANAGER.storeBlock(EASY_UNIQUE_DESC(__LINE__), EASY_RUNTIME_NAME(name)) + +# define EASY_FORCE_EVENT(timestamp, name, ...)\ + EASY_LOCAL_STATIC_PTR(const ::profiler::BaseBlockDescriptor*, EASY_UNIQUE_DESC(__LINE__), addBlockDescriptor(\ + ::profiler::extract_enable_flag(__VA_ARGS__), EASY_UNIQUE_LINE_ID, EASY_COMPILETIME_NAME(name),\ + __FILE__, __LINE__, ::profiler::BLOCK_TYPE_EVENT, ::profiler::extract_color(__VA_ARGS__)));\ + storeBlockForce(EASY_UNIQUE_DESC(__LINE__), EASY_RUNTIME_NAME(name), timestamp) + +# define EASY_FORCE_EVENT2(timestamp, name, ...)\ + EASY_LOCAL_STATIC_PTR(const ::profiler::BaseBlockDescriptor*, EASY_UNIQUE_DESC(__LINE__), addBlockDescriptor(\ + ::profiler::extract_enable_flag(__VA_ARGS__), EASY_UNIQUE_LINE_ID, EASY_COMPILETIME_NAME(name),\ + __FILE__, __LINE__, ::profiler::BLOCK_TYPE_EVENT, ::profiler::extract_color(__VA_ARGS__)));\ + storeBlockForce2(EASY_UNIQUE_DESC(__LINE__), EASY_RUNTIME_NAME(name), timestamp) + +# define EASY_FORCE_EVENT3(ts, timestamp, name, ...)\ + EASY_LOCAL_STATIC_PTR(const ::profiler::BaseBlockDescriptor*, EASY_UNIQUE_DESC(__LINE__), addBlockDescriptor(\ + ::profiler::extract_enable_flag(__VA_ARGS__), EASY_UNIQUE_LINE_ID, EASY_COMPILETIME_NAME(name),\ + __FILE__, __LINE__, ::profiler::BLOCK_TYPE_EVENT, ::profiler::extract_color(__VA_ARGS__)));\ + storeBlockForce2(ts, EASY_UNIQUE_DESC(__LINE__), EASY_RUNTIME_NAME(name), timestamp) +#else +# ifndef EASY_PROFILER_API_DISABLED +# define EASY_PROFILER_API_DISABLED +# endif +# define EASY_EVENT_RES(res, name, ...) +# define EASY_FORCE_EVENT(timestamp, name, ...) +# define EASY_FORCE_EVENT2(timestamp, name, ...) +# define EASY_FORCE_EVENT3(ts, timestamp, name, ...) +#endif + +////////////////////////////////////////////////////////////////////////// + +extern "C" { + +#if !defined(EASY_PROFILER_API_DISABLED) + PROFILER_API timestamp_t currentTime() + { + return getCurrentTime(); + } + + PROFILER_API timestamp_t toNanoseconds(timestamp_t _ticks) + { +#if defined(EASY_CHRONO_CLOCK) || defined(_WIN32) + return _ticks * 1000000000LL / CPU_FREQUENCY; +#else + return _ticks / CPU_FREQUENCY.load(std::memory_order_acquire); +#endif + } + + PROFILER_API timestamp_t toMicroseconds(timestamp_t _ticks) + { + return TICKS_TO_US(_ticks); + } + + PROFILER_API const BaseBlockDescriptor* registerDescription(EasyBlockStatus _status, const char* _autogenUniqueId, const char* _name, const char* _filename, int _line, block_type_t _block_type, color_t _color, bool _copyName) + { + return MANAGER.addBlockDescriptor(_status, _autogenUniqueId, _name, _filename, _line, _block_type, _color, _copyName); + } + + PROFILER_API void endBlock() + { + MANAGER.endBlock(); + } + + PROFILER_API void setEnabled(bool isEnable) + { + MANAGER.setEnabled(isEnable); + } + + PROFILER_API bool isEnabled() + { + return MANAGER.isEnabled(); + } + + PROFILER_API void storeEvent(const BaseBlockDescriptor* _desc, const char* _runtimeName) + { + MANAGER.storeBlock(_desc, _runtimeName); + } + + PROFILER_API void storeBlock(const BaseBlockDescriptor* _desc, const char* _runtimeName, timestamp_t _beginTime, timestamp_t _endTime) + { + MANAGER.storeBlock(_desc, _runtimeName, _beginTime, _endTime); + } + + PROFILER_API void beginBlock(Block& _block) + { + MANAGER.beginBlock(_block); + } + + PROFILER_API void beginNonScopedBlock(const BaseBlockDescriptor* _desc, const char* _runtimeName) + { + MANAGER.beginNonScopedBlock(_desc, _runtimeName); + } + + PROFILER_API uint32_t dumpBlocksToFile(const char* filename) + { + return MANAGER.dumpBlocksToFile(filename); + } + + PROFILER_API const char* registerThreadScoped(const char* name, ThreadGuard& threadGuard) + { + return MANAGER.registerThread(name, threadGuard); + } + + PROFILER_API const char* registerThread(const char* name) + { + return MANAGER.registerThread(name); + } + + PROFILER_API void setEventTracingEnabled(bool _isEnable) + { + MANAGER.setEventTracingEnabled(_isEnable); + } + + PROFILER_API bool isEventTracingEnabled() + { + return MANAGER.isEventTracingEnabled(); + } + +# ifdef _WIN32 + PROFILER_API void setLowPriorityEventTracing(bool _isLowPriority) + { + EasyEventTracer::instance().setLowPriority(_isLowPriority); + } + + PROFILER_API bool isLowPriorityEventTracing() + { + return EasyEventTracer::instance().isLowPriority(); + } +# else + PROFILER_API void setLowPriorityEventTracing(bool) { } + PROFILER_API bool isLowPriorityEventTracing() { return false; } +# endif + + PROFILER_API void setContextSwitchLogFilename(const char* name) + { + return MANAGER.setContextSwitchLogFilename(name); + } + + PROFILER_API const char* getContextSwitchLogFilename() + { + return MANAGER.getContextSwitchLogFilename(); + } + + PROFILER_API void startListen(uint16_t _port) + { + return MANAGER.startListen(_port); + } + + PROFILER_API void stopListen() + { + return MANAGER.stopListen(); + } + + PROFILER_API bool isListening() + { + return MANAGER.isListening(); + } + + PROFILER_API bool isMainThread() + { + return THIS_THREAD_IS_MAIN; + } + + PROFILER_API timestamp_t this_thread_frameTime(Duration _durationCast) + { + if (_durationCast == profiler::TICKS) + return THIS_THREAD_FRAME_T_CUR; + return TICKS_TO_US(THIS_THREAD_FRAME_T_CUR); + } + + PROFILER_API timestamp_t this_thread_frameTimeLocalMax(Duration _durationCast) + { + THIS_THREAD_FRAME_T_RESET_MAX = true; + if (_durationCast == profiler::TICKS) + return THIS_THREAD_FRAME_T_MAX; + return TICKS_TO_US(THIS_THREAD_FRAME_T_MAX); + } + + PROFILER_API timestamp_t this_thread_frameTimeLocalAvg(Duration _durationCast) + { + THIS_THREAD_FRAME_T_RESET_AVG = true; + auto avgDuration = THIS_THREAD_N_FRAMES > 0 ? THIS_THREAD_FRAME_T_ACC / THIS_THREAD_N_FRAMES : 0; + if (_durationCast == profiler::TICKS) + return avgDuration; + return TICKS_TO_US(avgDuration); + } + + PROFILER_API timestamp_t main_thread_frameTime(Duration _durationCast) + { + const auto ticks = THIS_THREAD_IS_MAIN ? THIS_THREAD_FRAME_T_CUR : MANAGER.curFrameDuration(); + if (_durationCast == profiler::TICKS) + return ticks; + return TICKS_TO_US(ticks); + } + + PROFILER_API timestamp_t main_thread_frameTimeLocalMax(Duration _durationCast) + { + if (THIS_THREAD_IS_MAIN) + { + THIS_THREAD_FRAME_T_RESET_MAX = true; + if (_durationCast == profiler::TICKS) + return THIS_THREAD_FRAME_T_MAX; + return TICKS_TO_US(THIS_THREAD_FRAME_T_MAX); + } + + if (_durationCast == profiler::TICKS) + return MANAGER.maxFrameDuration(); + return TICKS_TO_US(MANAGER.maxFrameDuration()); + } + + PROFILER_API timestamp_t main_thread_frameTimeLocalAvg(Duration _durationCast) + { + if (THIS_THREAD_IS_MAIN) + { + THIS_THREAD_FRAME_T_RESET_AVG = true; + auto avgDuration = THIS_THREAD_N_FRAMES > 0 ? THIS_THREAD_FRAME_T_ACC / THIS_THREAD_N_FRAMES : 0; + if (_durationCast == profiler::TICKS) + return avgDuration; + return TICKS_TO_US(avgDuration); + } + + if (_durationCast == profiler::TICKS) + return MANAGER.avgFrameDuration(); + return TICKS_TO_US(MANAGER.avgFrameDuration()); + } + +#else + PROFILER_API timestamp_t currentTime() { return 0; } + PROFILER_API timestamp_t toNanoseconds(timestamp_t) { return 0; } + PROFILER_API timestamp_t toMicroseconds(timestamp_t) { return 0; } + PROFILER_API const BaseBlockDescriptor* registerDescription(EasyBlockStatus, const char*, const char*, const char*, int, block_type_t, color_t, bool) { return reinterpret_cast(0xbad); } + PROFILER_API void endBlock() { } + PROFILER_API void setEnabled(bool) { } + PROFILER_API bool isEnabled() { return false; } + PROFILER_API void storeEvent(const BaseBlockDescriptor*, const char*) { } + PROFILER_API void storeBlock(const BaseBlockDescriptor*, const char*, timestamp_t, timestamp_t) { } + PROFILER_API void beginBlock(Block&) { } + PROFILER_API void beginNonScopedBlock(const BaseBlockDescriptor*, const char*) { } + PROFILER_API uint32_t dumpBlocksToFile(const char*) { return 0; } + PROFILER_API const char* registerThreadScoped(const char*, ThreadGuard&) { return ""; } + PROFILER_API const char* registerThread(const char*) { return ""; } + PROFILER_API void setEventTracingEnabled(bool) { } + PROFILER_API bool isEventTracingEnabled() { return false; } + PROFILER_API void setLowPriorityEventTracing(bool) { } + PROFILER_API bool isLowPriorityEventTracing(bool) { return false; } + PROFILER_API void setContextSwitchLogFilename(const char*) { } + PROFILER_API const char* getContextSwitchLogFilename() { return ""; } + PROFILER_API void startListen(uint16_t) { } + PROFILER_API void stopListen() { } + PROFILER_API bool isListening() { return false; } + + PROFILER_API bool isMainThread() { return false; } + PROFILER_API timestamp_t this_thread_frameTime(Duration) { return 0; } + PROFILER_API timestamp_t this_thread_frameTimeLocalMax(Duration) { return 0; } + PROFILER_API timestamp_t this_thread_frameTimeLocalAvg(Duration) { return 0; } + PROFILER_API timestamp_t main_thread_frameTime(Duration) { return 0; } + PROFILER_API timestamp_t main_thread_frameTimeLocalMax(Duration) { return 0; } + PROFILER_API timestamp_t main_thread_frameTimeLocalAvg(Duration) { return 0; } +#endif + + PROFILER_API uint8_t versionMajor() + { + static_assert(0 <= EASY_PROFILER_VERSION_MAJOR && EASY_PROFILER_VERSION_MAJOR <= 255, "EASY_PROFILER_VERSION_MAJOR must be defined in range [0, 255]"); + return EASY_PROFILER_VERSION_MAJOR; + } + + PROFILER_API uint8_t versionMinor() + { + static_assert(0 <= EASY_PROFILER_VERSION_MINOR && EASY_PROFILER_VERSION_MINOR <= 255, "EASY_PROFILER_VERSION_MINOR must be defined in range [0, 255]"); + return EASY_PROFILER_VERSION_MINOR; + } + + PROFILER_API uint16_t versionPatch() + { + static_assert(0 <= EASY_PROFILER_VERSION_PATCH && EASY_PROFILER_VERSION_PATCH <= 65535, "EASY_PROFILER_VERSION_PATCH must be defined in range [0, 65535]"); + return EASY_PROFILER_VERSION_PATCH; + } + + PROFILER_API uint32_t version() + { + return EASY_CURRENT_VERSION; + } + + PROFILER_API const char* versionName() + { + return EASY_PROFILER_PRODUCT_VERSION +#ifdef EASY_PROFILER_API_DISABLED + "_disabled" +#endif + ; + } + +} + +////////////////////////////////////////////////////////////////////////// + +SerializedBlock::SerializedBlock(const Block& block, uint16_t name_length) + : BaseBlockData(block) +{ + auto pName = const_cast(name()); + if (name_length) strncpy(pName, block.name(), name_length); + pName[name_length] = 0; +} + +SerializedCSwitch::SerializedCSwitch(const CSwitchBlock& block, uint16_t name_length) + : CSwitchEvent(block) +{ + auto pName = const_cast(name()); + if (name_length) strncpy(pName, block.name(), name_length); + pName[name_length] = 0; +} + +////////////////////////////////////////////////////////////////////////// + +BaseBlockDescriptor::BaseBlockDescriptor(block_id_t _id, EasyBlockStatus _status, int _line, block_type_t _block_type, color_t _color) + : m_id(_id) + , m_line(_line) + , m_type(_block_type) + , m_color(_color) + , m_status(_status) +{ + +} + +////////////////////////////////////////////////////////////////////////// + +#ifndef EASY_BLOCK_DESC_FULL_COPY +# define EASY_BLOCK_DESC_FULL_COPY 1 +#endif + +#if EASY_BLOCK_DESC_FULL_COPY == 0 +# define EASY_BLOCK_DESC_STRING const char* +# define EASY_BLOCK_DESC_STRING_LEN(s) static_cast(strlen(s) + 1) +# define EASY_BLOCK_DESC_STRING_VAL(s) s +#else +# define EASY_BLOCK_DESC_STRING std::string +# define EASY_BLOCK_DESC_STRING_LEN(s) static_cast(s.size() + 1) +# define EASY_BLOCK_DESC_STRING_VAL(s) s.c_str() +#endif + +class BlockDescriptor : public BaseBlockDescriptor +{ + friend ProfileManager; + + EASY_BLOCK_DESC_STRING m_filename; ///< Source file name where this block is declared + EASY_BLOCK_DESC_STRING m_name; ///< Static name of all blocks of the same type (blocks can have dynamic name) which is, in pair with descriptor id, a unique block identifier + +public: + + BlockDescriptor(block_id_t _id, EasyBlockStatus _status, const char* _name, const char* _filename, int _line, block_type_t _block_type, color_t _color) + : BaseBlockDescriptor(_id, _status, _line, _block_type, _color) + , m_filename(_filename) + , m_name(_name) + { + } + + const char* name() const { + return EASY_BLOCK_DESC_STRING_VAL(m_name); + } + + const char* filename() const { + return EASY_BLOCK_DESC_STRING_VAL(m_filename); + } + + uint16_t nameSize() const { + return EASY_BLOCK_DESC_STRING_LEN(m_name); + } + + uint16_t filenameSize() const { + return EASY_BLOCK_DESC_STRING_LEN(m_filename); + } + +}; // END of class BlockDescriptor. + +////////////////////////////////////////////////////////////////////////// + +NonscopedBlock::NonscopedBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, bool) + : profiler::Block(_desc, _runtimeName, false), m_runtimeName(nullptr) +{ + +} + +NonscopedBlock::~NonscopedBlock() +{ + // Actually destructor should not be invoked because StackBuffer do manual memory management + + m_end = m_begin; // to restrict profiler::Block to invoke profiler::endBlock() on destructor. + free(m_runtimeName); +} + +void NonscopedBlock::copyname() +{ + // Here we need to copy m_name to m_runtimeName to ensure that + // it would be alive to the moment we will serialize the block + + if ((m_status & profiler::ON) == 0) + return; + + if (*m_name != 0) + { + auto len = strlen(m_name); + m_runtimeName = static_cast(malloc(len + 1)); + + // memcpy should be faster than strncpy because we know + // actual bytes number and both strings have the same size + memcpy(m_runtimeName, m_name, len); + + m_runtimeName[len] = 0; + m_name = m_runtimeName; + } + else + { + m_name = ""; + } +} + +void NonscopedBlock::destroy() +{ + // free memory used by m_runtimeName + free(m_runtimeName); + m_name = ""; +} + +////////////////////////////////////////////////////////////////////////// + +ThreadStorage::ThreadStorage() : nonscopedBlocks(16), id(getCurrentThreadId()), allowChildren(true), named(false), guarded(false) +#ifndef _WIN32 +, pthread_id(pthread_self()) +#endif + +{ + expired = ATOMIC_VAR_INIT(0); + frame = ATOMIC_VAR_INIT(false); +} + +void ThreadStorage::storeBlock(const profiler::Block& block) +{ +#if EASY_OPTION_MEASURE_STORAGE_EXPAND != 0 + EASY_LOCAL_STATIC_PTR(const BaseBlockDescriptor*, desc,\ + MANAGER.addBlockDescriptor(EASY_OPTION_STORAGE_EXPAND_BLOCKS_ON ? profiler::ON : profiler::OFF, EASY_UNIQUE_LINE_ID, "EasyProfiler.ExpandStorage",\ + __FILE__, __LINE__, profiler::BLOCK_TYPE_BLOCK, EASY_COLOR_INTERNAL_EVENT)); + + EASY_THREAD_LOCAL static profiler::timestamp_t beginTime = 0ULL; + EASY_THREAD_LOCAL static profiler::timestamp_t endTime = 0ULL; +#endif + + auto name_length = static_cast(strlen(block.name())); + auto size = static_cast(sizeof(BaseBlockData) + name_length + 1); + +#if EASY_OPTION_MEASURE_STORAGE_EXPAND != 0 + const bool expanded = (desc->m_status & profiler::ON) && blocks.closedList.need_expand(size); + if (expanded) beginTime = getCurrentTime(); +#endif + + auto data = blocks.closedList.allocate(size); + +#if EASY_OPTION_MEASURE_STORAGE_EXPAND != 0 + if (expanded) endTime = getCurrentTime(); +#endif + + ::new (data) SerializedBlock(block, name_length); + blocks.usedMemorySize += size; + +#if EASY_OPTION_MEASURE_STORAGE_EXPAND != 0 + if (expanded) + { + profiler::Block b(beginTime, desc->id(), ""); + b.finish(endTime); + + size = static_cast(sizeof(BaseBlockData) + 1); + data = blocks.closedList.allocate(size); + ::new (data) SerializedBlock(b, 0); + blocks.usedMemorySize += size; + } +#endif +} + +void ThreadStorage::storeCSwitch(const CSwitchBlock& block) +{ + auto name_length = static_cast(strlen(block.name())); + auto size = static_cast(sizeof(CSwitchEvent) + name_length + 1); + auto data = sync.closedList.allocate(size); + ::new (data) SerializedCSwitch(block, name_length); + sync.usedMemorySize += size; +} + +void ThreadStorage::clearClosed() +{ + blocks.clearClosed(); + sync.clearClosed(); +} + +void ThreadStorage::popSilent() +{ + if (!blocks.openedList.empty()) + { + Block& top = blocks.openedList.back(); + top.m_end = top.m_begin; + if (!top.m_isScoped) + nonscopedBlocks.pop(); + blocks.openedList.pop_back(); + } +} + +////////////////////////////////////////////////////////////////////////// + +ThreadGuard::~ThreadGuard() +{ +#ifndef EASY_PROFILER_API_DISABLED + if (m_id != 0 && THIS_THREAD != nullptr && THIS_THREAD->id == m_id) + { + bool isMarked = false; + EASY_EVENT_RES(isMarked, "ThreadFinished", EASY_COLOR_THREAD_END, ::profiler::FORCE_ON); + THIS_THREAD->frame.store(false, std::memory_order_release); + THIS_THREAD->expired.store(isMarked ? 2 : 1, std::memory_order_release); + THIS_THREAD = nullptr; + } +#endif +} + +////////////////////////////////////////////////////////////////////////// + +ProfileManager::ProfileManager() : +#ifdef _WIN32 + m_processId(GetProcessId(GetCurrentProcess())) +#else + m_processId((processid_t)getpid()) +#endif + , m_usedMemorySize(0) + , m_beginTime(0) + , m_endTime(0) +{ + m_profilerStatus = ATOMIC_VAR_INIT(EASY_PROF_DISABLED); + m_isEventTracingEnabled = ATOMIC_VAR_INIT(EASY_OPTION_EVENT_TRACING_ENABLED); + m_isAlreadyListening = ATOMIC_VAR_INIT(false); + m_stopListen = ATOMIC_VAR_INIT(false); + + m_mainThreadId = ATOMIC_VAR_INIT(0); + m_frameMax = ATOMIC_VAR_INIT(0); + m_frameAvg = ATOMIC_VAR_INIT(0); + m_frameCur = ATOMIC_VAR_INIT(0); + m_frameMaxReset = ATOMIC_VAR_INIT(false); + m_frameAvgReset = ATOMIC_VAR_INIT(false); + +#if !defined(EASY_PROFILER_API_DISABLED) && EASY_OPTION_START_LISTEN_ON_STARTUP != 0 + startListen(profiler::DEFAULT_PORT); +#endif + +#if !defined(EASY_PROFILER_API_DISABLED) && !defined(EASY_CHRONO_CLOCK) && !defined(_WIN32) + const int64_t cpu_frequency = calculate_cpu_frequency(); + CPU_FREQUENCY.store(cpu_frequency, std::memory_order_release); +#endif +} + +ProfileManager::~ProfileManager() +{ +#ifndef EASY_PROFILER_API_DISABLED + stopListen(); +#endif + + for (auto desc : m_descriptors) { +#if EASY_BLOCK_DESC_FULL_COPY == 0 + if (desc) + desc->~BlockDescriptor(); + free(desc); +#else + delete desc; +#endif + } +} + +#ifndef EASY_MAGIC_STATIC_CPP11 +class ProfileManagerInstance { + friend ProfileManager; + ProfileManager instance; +} PROFILE_MANAGER; +#endif + +////////////////////////////////////////////////////////////////////////// + +ProfileManager& ProfileManager::instance() +{ +#ifndef EASY_MAGIC_STATIC_CPP11 + return PROFILE_MANAGER.instance; +#else + ///C++11 makes possible to create Singleton without any warry about thread-safeness + ///http://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/ + static ProfileManager profileManager; + return profileManager; +#endif +} + +////////////////////////////////////////////////////////////////////////// + +ThreadStorage& ProfileManager::_threadStorage(profiler::thread_id_t _thread_id) +{ + return m_threads[_thread_id]; +} + +ThreadStorage* ProfileManager::_findThreadStorage(profiler::thread_id_t _thread_id) +{ + auto it = m_threads.find(_thread_id); + return it != m_threads.end() ? &it->second : nullptr; +} + +////////////////////////////////////////////////////////////////////////// + +const BaseBlockDescriptor* ProfileManager::addBlockDescriptor(EasyBlockStatus _defaultStatus, + const char* _autogenUniqueId, + const char* _name, + const char* _filename, + int _line, + block_type_t _block_type, + color_t _color, + bool _copyName) +{ + guard_lock_t lock(m_storedSpin); + + descriptors_map_t::key_type key(_autogenUniqueId); + auto it = m_descriptorsMap.find(key); + if (it != m_descriptorsMap.end()) + return m_descriptors[it->second]; + + const auto nameLen = strlen(_name); + m_usedMemorySize += sizeof(profiler::SerializedBlockDescriptor) + nameLen + strlen(_filename) + 2; + +#if EASY_BLOCK_DESC_FULL_COPY == 0 + BlockDescriptor* desc = nullptr; + + if (_copyName) + { + void* data = malloc(sizeof(BlockDescriptor) + nameLen + 1); + char* name = reinterpret_cast(data) + sizeof(BlockDescriptor); + strncpy(name, _name, nameLen); + desc = ::new (data)BlockDescriptor(static_cast(m_descriptors.size()), _defaultStatus, name, _filename, _line, _block_type, _color); + } + else + { + void* data = malloc(sizeof(BlockDescriptor)); + desc = ::new (data)BlockDescriptor(static_cast(m_descriptors.size()), _defaultStatus, _name, _filename, _line, _block_type, _color); + } +#else + auto desc = new BlockDescriptor(static_cast(m_descriptors.size()), _defaultStatus, _name, _filename, _line, _block_type, _color); +#endif + + m_descriptors.emplace_back(desc); + m_descriptorsMap.emplace(key, desc->id()); + + return desc; +} + +////////////////////////////////////////////////////////////////////////// + +bool ProfileManager::storeBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName) +{ + const auto state = m_profilerStatus.load(std::memory_order_acquire); + if (state == EASY_PROF_DISABLED || !(_desc->m_status & profiler::ON)) + return false; + + if (state == EASY_PROF_DUMP) + { + if (THIS_THREAD == nullptr || THIS_THREAD->blocks.openedList.empty()) + return false; + } + else if (THIS_THREAD == nullptr) + { + THIS_THREAD = &threadStorage(getCurrentThreadId()); + } + +#if EASY_ENABLE_BLOCK_STATUS != 0 + if (!THIS_THREAD->allowChildren && !(_desc->m_status & FORCE_ON_FLAG)) + return false; +#endif + + profiler::Block b(_desc, _runtimeName); + b.start(); + b.m_end = b.m_begin; + + THIS_THREAD->storeBlock(b); + + return true; +} + +bool ProfileManager::storeBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, profiler::timestamp_t _beginTime, profiler::timestamp_t _endTime) +{ + const auto state = m_profilerStatus.load(std::memory_order_acquire); + if (state == EASY_PROF_DISABLED || !(_desc->m_status & profiler::ON)) + return false; + + if (state == EASY_PROF_DUMP) + { + if (THIS_THREAD == nullptr || THIS_THREAD->blocks.openedList.empty()) + return false; + } + else if (THIS_THREAD == nullptr) + { + THIS_THREAD = &threadStorage(getCurrentThreadId()); + } + +#if EASY_ENABLE_BLOCK_STATUS != 0 + if (!THIS_THREAD->allowChildren && !(_desc->m_status & FORCE_ON_FLAG)) + return false; +#endif + + profiler::Block b(_beginTime, _endTime, _desc->id(), _runtimeName); + THIS_THREAD->storeBlock(b); + b.m_end = b.m_begin; + + return true; +} + +////////////////////////////////////////////////////////////////////////// + +void ProfileManager::storeBlockForce(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, ::profiler::timestamp_t& _timestamp) +{ + if (!(_desc->m_status & profiler::ON)) + return; + + if (THIS_THREAD == nullptr) + THIS_THREAD = &threadStorage(getCurrentThreadId()); + +#if EASY_ENABLE_BLOCK_STATUS != 0 + if (!THIS_THREAD->allowChildren && !(_desc->m_status & FORCE_ON_FLAG)) + return; +#endif + + profiler::Block b(_desc, _runtimeName); + b.start(); + b.m_end = b.m_begin; + + _timestamp = b.m_begin; + THIS_THREAD->storeBlock(b); +} + +void ProfileManager::storeBlockForce2(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, ::profiler::timestamp_t _timestamp) +{ + if (!(_desc->m_status & profiler::ON)) + return; + + if (THIS_THREAD == nullptr) + THIS_THREAD = &threadStorage(getCurrentThreadId()); + +#if EASY_ENABLE_BLOCK_STATUS != 0 + if (!THIS_THREAD->allowChildren && !(_desc->m_status & FORCE_ON_FLAG)) + return; +#endif + + profiler::Block b(_desc, _runtimeName); + b.m_end = b.m_begin = _timestamp; + + THIS_THREAD->storeBlock(b); +} + +void ProfileManager::storeBlockForce2(ThreadStorage& _registeredThread, const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, ::profiler::timestamp_t _timestamp) +{ + profiler::Block b(_desc, _runtimeName); + b.m_end = b.m_begin = _timestamp; + _registeredThread.storeBlock(b); +} + +////////////////////////////////////////////////////////////////////////// + +void ProfileManager::beginBlock(Block& _block) +{ + if (THIS_THREAD == nullptr) + THIS_THREAD = &threadStorage(getCurrentThreadId()); + + if (++THIS_THREAD_STACK_SIZE > 1) + { + _block.m_status = profiler::OFF; + THIS_THREAD->blocks.openedList.emplace_back(_block); + return; + } + + const auto state = m_profilerStatus.load(std::memory_order_acquire); + if (state == EASY_PROF_DISABLED) + { + THIS_THREAD_HALT = false; + _block.m_status = profiler::OFF; + THIS_THREAD->blocks.openedList.emplace_back(_block); + beginFrame(); + return; + } + + bool empty = true; + if (state == EASY_PROF_DUMP) + { + if (THIS_THREAD_HALT || THIS_THREAD->blocks.openedList.empty()) + { + _block.m_status = profiler::OFF; + THIS_THREAD->blocks.openedList.emplace_back(_block); + + if (!THIS_THREAD_HALT) + { + THIS_THREAD_HALT = true; + beginFrame(); + } + + return; + } + + empty = false; + } + else + { + empty = THIS_THREAD->blocks.openedList.empty(); + } + + THIS_THREAD_HALT = false; + THIS_THREAD_STACK_SIZE = 0; + +#if EASY_ENABLE_BLOCK_STATUS != 0 + if (THIS_THREAD->allowChildren) + { +#endif + if (_block.m_status & profiler::ON) + _block.start(); +#if EASY_ENABLE_BLOCK_STATUS != 0 + THIS_THREAD->allowChildren = !(_block.m_status & profiler::OFF_RECURSIVE); + } + else if (_block.m_status & FORCE_ON_FLAG) + { + _block.start(); + _block.m_status = profiler::FORCE_ON_WITHOUT_CHILDREN; + } + else + { + _block.m_status = profiler::OFF_RECURSIVE; + } +#endif + + if (empty) + { + beginFrame(); + THIS_THREAD->frame.store(true, std::memory_order_release); + } + + THIS_THREAD->blocks.openedList.emplace_back(_block); +} + +void ProfileManager::beginNonScopedBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName) +{ + if (THIS_THREAD == nullptr) + THIS_THREAD = &threadStorage(getCurrentThreadId()); + + NonscopedBlock& b = THIS_THREAD->nonscopedBlocks.push(_desc, _runtimeName, false); + beginBlock(b); + b.copyname(); +} + +void ProfileManager::beginContextSwitch(profiler::thread_id_t _thread_id, profiler::timestamp_t _time, profiler::thread_id_t _target_thread_id, const char* _target_process, bool _lockSpin) +{ + auto ts = _lockSpin ? findThreadStorage(_thread_id) : _findThreadStorage(_thread_id); + if (ts != nullptr) + // Dirty hack: _target_thread_id will be written to the field "block_id_t m_id" + // and will be available calling method id(). + ts->sync.openedList.emplace_back(_time, _target_thread_id, _target_process); +} + +////////////////////////////////////////////////////////////////////////// + +void ProfileManager::endBlock() +{ + if (--THIS_THREAD_STACK_SIZE > 0) + { + THIS_THREAD->popSilent(); + return; + } + + THIS_THREAD_STACK_SIZE = 0; + if (THIS_THREAD_HALT || m_profilerStatus.load(std::memory_order_acquire) == EASY_PROF_DISABLED) + { + THIS_THREAD->popSilent(); + endFrame(); + return; + } + + if (THIS_THREAD->blocks.openedList.empty()) + return; + + Block& top = THIS_THREAD->blocks.openedList.back(); + if (top.m_status & profiler::ON) + { + if (!top.finished()) + top.finish(); + THIS_THREAD->storeBlock(top); + } + else + { + top.m_end = top.m_begin; // this is to restrict endBlock() call inside ~Block() + } + + if (!top.m_isScoped) + THIS_THREAD->nonscopedBlocks.pop(); + + THIS_THREAD->blocks.openedList.pop_back(); + const bool empty = THIS_THREAD->blocks.openedList.empty(); + if (empty) + { + THIS_THREAD->frame.store(false, std::memory_order_release); + endFrame(); +#if EASY_ENABLE_BLOCK_STATUS != 0 + THIS_THREAD->allowChildren = true; + } + else + { + THIS_THREAD->allowChildren = !(THIS_THREAD->blocks.openedList.back().get().m_status & profiler::OFF_RECURSIVE); + } +#else + } +#endif +} + +void ProfileManager::endContextSwitch(profiler::thread_id_t _thread_id, processid_t _process_id, profiler::timestamp_t _endtime, bool _lockSpin) +{ + ThreadStorage* ts = nullptr; + if (_process_id == m_processId) + // If thread owned by current process then create new ThreadStorage if there is no one + ts = _lockSpin ? &threadStorage(_thread_id) : &_threadStorage(_thread_id); + else + // If thread owned by another process OR _process_id IS UNKNOWN then do not create ThreadStorage for this + ts = _lockSpin ? findThreadStorage(_thread_id) : _findThreadStorage(_thread_id); + + if (ts == nullptr || ts->sync.openedList.empty()) + return; + + CSwitchBlock& lastBlock = ts->sync.openedList.back(); + lastBlock.m_end = _endtime; + + ts->storeCSwitch(lastBlock); + ts->sync.openedList.pop_back(); +} + +////////////////////////////////////////////////////////////////////////// + +void ProfileManager::beginFrame() +{ + if (!THIS_THREAD_FRAME) + { + THIS_THREAD_FRAME_T = getCurrentTime(); + THIS_THREAD_FRAME = true; + } +} + +void ProfileManager::endFrame() +{ + if (!THIS_THREAD_FRAME) + return; + + const profiler::timestamp_t duration = getCurrentTime() - THIS_THREAD_FRAME_T; + + THIS_THREAD_FRAME = false; + + if (THIS_THREAD_FRAME_T_RESET_MAX) + { + THIS_THREAD_FRAME_T_RESET_MAX = false; + THIS_THREAD_FRAME_T_MAX = 0; + } + + THIS_THREAD_FRAME_T_CUR = duration; + if (duration > THIS_THREAD_FRAME_T_MAX) + THIS_THREAD_FRAME_T_MAX = duration; + + if (THIS_THREAD_N_FRAMES > 10000) + THIS_THREAD_FRAME_T_RESET_AVG = true; + + if (THIS_THREAD_IS_MAIN) + { + if (m_frameAvgReset.exchange(false, std::memory_order_release) || THIS_THREAD_FRAME_T_RESET_AVG) + { + if (THIS_THREAD_N_FRAMES > 0) + m_frameAvg.store(THIS_THREAD_FRAME_T_ACC / THIS_THREAD_N_FRAMES, std::memory_order_release); + THIS_THREAD_FRAME_T_RESET_AVG = false; + THIS_THREAD_FRAME_T_ACC = duration; + THIS_THREAD_N_FRAMES = 1; + } + else + { + THIS_THREAD_FRAME_T_ACC += duration; + ++THIS_THREAD_N_FRAMES; + m_frameAvg.store(THIS_THREAD_FRAME_T_ACC / THIS_THREAD_N_FRAMES, std::memory_order_release); + } + + auto maxDuration = m_frameMax.load(std::memory_order_acquire); + if (m_frameMaxReset.exchange(false, std::memory_order_release)) + maxDuration = 0; + + if (duration > maxDuration) + m_frameMax.store(duration, std::memory_order_release); + + if (m_frameMaxReset.exchange(false, std::memory_order_release)) + maxDuration = 0; + + m_frameCur.store(duration, std::memory_order_release); + } + else if (THIS_THREAD_FRAME_T_RESET_AVG) + { + THIS_THREAD_FRAME_T_RESET_AVG = false; + THIS_THREAD_FRAME_T_ACC = duration; + THIS_THREAD_N_FRAMES = 1; + } + else + { + THIS_THREAD_FRAME_T_ACC += duration; + ++THIS_THREAD_N_FRAMES; + } +} + +profiler::timestamp_t ProfileManager::maxFrameDuration() +{ + auto duration = m_frameMax.load(std::memory_order_acquire); + m_frameMaxReset.store(true, std::memory_order_release); + return duration; +} + +profiler::timestamp_t ProfileManager::avgFrameDuration() +{ + auto duration = m_frameAvg.load(std::memory_order_acquire); + m_frameAvgReset.store(true, std::memory_order_release); + return duration; +} + +profiler::timestamp_t ProfileManager::curFrameDuration() const +{ + return m_frameCur.load(std::memory_order_acquire); +} + +////////////////////////////////////////////////////////////////////////// + +void ProfileManager::enableEventTracer() +{ +#ifdef _WIN32 + if (m_isEventTracingEnabled.load(std::memory_order_acquire)) + EasyEventTracer::instance().enable(true); +#endif +} + +void ProfileManager::disableEventTracer() +{ +#ifdef _WIN32 + EasyEventTracer::instance().disable(); +#endif +} + +void ProfileManager::setEnabled(bool isEnable) +{ + guard_lock_t lock(m_dumpSpin); + + auto time = getCurrentTime(); + const auto status = isEnable ? EASY_PROF_ENABLED : EASY_PROF_DISABLED; + const auto prev = m_profilerStatus.exchange(status, std::memory_order_release); + if (prev == status) + return; + + if (isEnable) + { + EASY_LOGMSG("Enabled profiling\n"); + enableEventTracer(); + m_beginTime = time; + } + else + { + EASY_LOGMSG("Disabled profiling\n"); + disableEventTracer(); + m_endTime = time; + } +} + +bool ProfileManager::isEnabled() const +{ + return m_profilerStatus.load(std::memory_order_acquire) == EASY_PROF_ENABLED; +} + +void ProfileManager::setEventTracingEnabled(bool _isEnable) +{ + m_isEventTracingEnabled.store(_isEnable, std::memory_order_release); +} + +bool ProfileManager::isEventTracingEnabled() const +{ + return m_isEventTracingEnabled.load(std::memory_order_acquire); +} + +////////////////////////////////////////////////////////////////////////// + +char ProfileManager::checkThreadExpired(ThreadStorage& _registeredThread) +{ + const char val = _registeredThread.expired.load(std::memory_order_acquire); + if (val != 0) + return val; + + if (_registeredThread.guarded) + return 0; + +#ifdef _WIN32 + + // Check thread for Windows + + DWORD exitCode = 0; + auto hThread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)_registeredThread.id); + if (hThread == nullptr || GetExitCodeThread(hThread, &exitCode) == FALSE || exitCode != STILL_ACTIVE) + { + // Thread has been expired + _registeredThread.expired.store(1, std::memory_order_release); + if (hThread != nullptr) + CloseHandle(hThread); + return 1; + } + + if (hThread != nullptr) + CloseHandle(hThread); + + return 0; + +#else + + return 0;//pthread_kill(_registeredThread.pthread_id, 0) != 0; + +#endif + +} + +////////////////////////////////////////////////////////////////////////// + +uint32_t ProfileManager::dumpBlocksToStream(profiler::OStream& _outputStream, bool _lockSpin) +{ + EASY_LOGMSG("dumpBlocksToStream(_lockSpin = " << _lockSpin << ")...\n"); + + if (_lockSpin) + m_dumpSpin.lock(); + + const auto state = m_profilerStatus.load(std::memory_order_acquire); + +#ifndef _WIN32 + const bool eventTracingEnabled = m_isEventTracingEnabled.load(std::memory_order_acquire); +#endif + + if (state == EASY_PROF_ENABLED) { + m_profilerStatus.store(EASY_PROF_DUMP, std::memory_order_release); + disableEventTracer(); + m_endTime = getCurrentTime(); + } + + + // This is to make sure that no new descriptors or new threads will be + // added until we finish sending data. + //m_spin.lock(); + // This is the only place using both spins, so no dead-lock will occur + + + // Wait for some time to be sure that all operations which began before setEnabled(false) will be finished. + // This is much better than inserting spin-lock or atomic variable check into each store operation. + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + + // wait for all threads finish opened frames + EASY_LOG_ONLY(bool logged = false); + for (auto it = m_threads.begin(), end = m_threads.end(); it != end;) + { + if (!it->second.frame.load(std::memory_order_acquire)) + { + ++it; + EASY_LOG_ONLY(logged = false); + } + else + { + EASY_LOG_ONLY( + if (!logged) + { + logged = true; + if (it->second.named) + EASY_WARNING("Waiting for thread \"" << it->second.name << "\" finish opened frame (which is top EASY_BLOCK for this thread)...\n"); + else + EASY_WARNING("Waiting for thread " << it->first << " finish opened frame (which is top EASY_BLOCK for this thread)...\n"); + } + ); + + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + } + + m_profilerStatus.store(EASY_PROF_DISABLED, std::memory_order_release); + + EASY_LOGMSG("All threads have closed frames\n"); + EASY_LOGMSG("Disabled profiling\n"); + + m_spin.lock(); + m_storedSpin.lock(); + // TODO: think about better solution because this one is not 100% safe... + + const profiler::timestamp_t now = getCurrentTime(); + const profiler::timestamp_t endtime = m_endTime == 0 ? now : std::min(now, m_endTime); + +#ifndef _WIN32 + if (eventTracingEnabled) + { + // Read thread context switch events from temporary file + + EASY_LOGMSG("Writing context switch events...\n"); + + uint64_t timestamp = 0; + profiler::thread_id_t thread_from = 0, thread_to = 0; + + std::ifstream infile(m_csInfoFilename.c_str()); + if(infile.is_open()) + { + EASY_LOG_ONLY(uint32_t num = 0); + std::string next_task_name; + pid_t process_to = 0; + while (infile >> timestamp >> thread_from >> thread_to >> next_task_name >> process_to) + { + beginContextSwitch(thread_from, timestamp, thread_to, next_task_name.c_str(), false); + endContextSwitch(thread_to, (processid_t)process_to, timestamp, false); + EASY_LOG_ONLY(++num); + } + + EASY_LOGMSG("Done, " << num << " context switch events wrote\n"); + } + EASY_LOG_ONLY( + else { + EASY_ERROR("Can not open context switch log-file \"" << m_csInfoFilename << "\"\n"); + } + ) + } +#endif + + bool mainThreadExpired = false; + + // Calculate used memory total size and total blocks number + uint64_t usedMemorySize = 0; + uint32_t blocks_number = 0; + for (auto it = m_threads.begin(), end = m_threads.end(); it != end;) + { + auto& t = it->second; + uint32_t num = static_cast(t.blocks.closedList.size()) + static_cast(t.sync.closedList.size()); + + const char expired = checkThreadExpired(t); + if (num == 0 && (expired != 0 || !t.guarded)) { + // Remove thread if it contains no profiled information and has been finished or is not guarded. + profiler::thread_id_t id = it->first; + if (!mainThreadExpired && m_mainThreadId.compare_exchange_weak(id, 0, std::memory_order_release, std::memory_order_acquire)) + mainThreadExpired = true; + m_threads.erase(it++); + continue; + } + + if (expired == 1) { + EASY_FORCE_EVENT3(t, endtime, "ThreadExpired", EASY_COLOR_THREAD_END); + ++num; + } + + usedMemorySize += t.blocks.usedMemorySize + t.sync.usedMemorySize; + blocks_number += num; + ++it; + } + + // Write profiler signature and version + _outputStream.write(PROFILER_SIGNATURE); + _outputStream.write(EASY_CURRENT_VERSION); + _outputStream.write(m_processId); + + // Write CPU frequency to let GUI calculate real time value from CPU clocks +#if defined(EASY_CHRONO_CLOCK) || defined(_WIN32) + _outputStream.write(CPU_FREQUENCY); +#else + EASY_LOGMSG("Calculating CPU frequency\n"); + const int64_t cpu_frequency = calculate_cpu_frequency(); + _outputStream.write(cpu_frequency * 1000LL); + EASY_LOGMSG("Done calculating CPU frequency\n"); + + CPU_FREQUENCY.store(cpu_frequency, std::memory_order_release); +#endif + + // Write begin and end time + _outputStream.write(m_beginTime); + _outputStream.write(m_endTime); + + // Write blocks number and used memory size + _outputStream.write(blocks_number); + _outputStream.write(usedMemorySize); + _outputStream.write(static_cast(m_descriptors.size())); + _outputStream.write(m_usedMemorySize); + + // Write block descriptors + for (const auto descriptor : m_descriptors) + { + const auto name_size = descriptor->nameSize(); + const auto filename_size = descriptor->filenameSize(); + const auto size = static_cast(sizeof(profiler::SerializedBlockDescriptor) + name_size + filename_size); + + _outputStream.write(size); + _outputStream.write(*descriptor); + _outputStream.write(name_size); + _outputStream.write(descriptor->name(), name_size); + _outputStream.write(descriptor->filename(), filename_size); + } + + // Write blocks and context switch events for each thread + for (auto it = m_threads.begin(), end = m_threads.end(); it != end;) + { + auto& t = it->second; + + _outputStream.write(it->first); + + const auto name_size = static_cast(t.name.size() + 1); + _outputStream.write(name_size); + _outputStream.write(name_size > 1 ? t.name.c_str() : "", name_size); + + _outputStream.write(t.sync.closedList.size()); + if (!t.sync.closedList.empty()) + t.sync.closedList.serialize(_outputStream); + + _outputStream.write(t.blocks.closedList.size()); + if (!t.blocks.closedList.empty()) + t.blocks.closedList.serialize(_outputStream); + + t.clearClosed(); + //t.blocks.openedList.clear(); + t.sync.openedList.clear(); + + if (t.expired.load(std::memory_order_acquire) != 0) + { + // Remove expired thread after writing all profiled information + profiler::thread_id_t id = it->first; + if (!mainThreadExpired && m_mainThreadId.compare_exchange_weak(id, 0, std::memory_order_release, std::memory_order_acquire)) + mainThreadExpired = true; + m_threads.erase(it++); + } + else + { + ++it; + } + } + + m_storedSpin.unlock(); + m_spin.unlock(); + + if (_lockSpin) + m_dumpSpin.unlock(); + + EASY_LOGMSG("Done dumpBlocksToStream(). Dumped " << blocks_number << " blocks\n"); + + return blocks_number; +} + +uint32_t ProfileManager::dumpBlocksToFile(const char* _filename) +{ + EASY_LOGMSG("dumpBlocksToFile(\"" << _filename << "\")...\n"); + + std::ofstream outputFile(_filename, std::fstream::binary); + if (!outputFile.is_open()) + { + EASY_ERROR("Can not open \"" << _filename << "\" for writing\n"); + return 0; + } + + profiler::OStream outputStream; + + // Replace outputStream buffer to outputFile buffer to avoid redundant copying + typedef ::std::basic_iostream stringstream_parent; + stringstream_parent& s = outputStream.stream(); + auto oldbuf = s.rdbuf(outputFile.rdbuf()); + + // Write data directly to file + const auto blocksNumber = dumpBlocksToStream(outputStream, true); + + // Restore old outputStream buffer to avoid possible second memory free on stringstream destructor + s.rdbuf(oldbuf); + + EASY_LOGMSG("Done dumpBlocksToFile()\n"); + + return blocksNumber; +} + +const char* ProfileManager::registerThread(const char* name, ThreadGuard& threadGuard) +{ + if (THIS_THREAD == nullptr) + THIS_THREAD = &threadStorage(getCurrentThreadId()); + + THIS_THREAD->guarded = true; + if (!THIS_THREAD->named) + { + THIS_THREAD->named = true; + THIS_THREAD->name = name; + + if (THIS_THREAD->name == "Main") + { + profiler::thread_id_t id = 0; + THIS_THREAD_IS_MAIN = m_mainThreadId.compare_exchange_weak(id, THIS_THREAD->id, std::memory_order_release, std::memory_order_acquire); + } + } + + threadGuard.m_id = THIS_THREAD->id; + + return THIS_THREAD->name.c_str(); +} + +const char* ProfileManager::registerThread(const char* name) +{ + if (THIS_THREAD == nullptr) + THIS_THREAD = &threadStorage(getCurrentThreadId()); + + if (!THIS_THREAD->named) + { + THIS_THREAD->named = true; + THIS_THREAD->name = name; + + if (THIS_THREAD->name == "Main") + { + profiler::thread_id_t id = 0; + THIS_THREAD_IS_MAIN = m_mainThreadId.compare_exchange_weak(id, THIS_THREAD->id, std::memory_order_release, std::memory_order_acquire); + } + } + + return THIS_THREAD->name.c_str(); +} + +void ProfileManager::setBlockStatus(block_id_t _id, EasyBlockStatus _status) +{ + if (m_profilerStatus.load(std::memory_order_acquire) != EASY_PROF_DISABLED) + return; // Changing blocks statuses is restricted while profile session is active + + guard_lock_t lock(m_storedSpin); + if (_id < m_descriptors.size()) + { + auto desc = m_descriptors[_id]; + lock.unlock(); + desc->m_status = _status; + } +} + +void ProfileManager::startListen(uint16_t _port) +{ + if (!m_isAlreadyListening.exchange(true, std::memory_order_release)) + { + m_stopListen.store(false, std::memory_order_release); + m_listenThread = std::thread(&ProfileManager::listen, this, _port); + } +} + +void ProfileManager::stopListen() +{ + m_stopListen.store(true, std::memory_order_release); + if (m_listenThread.joinable()) + m_listenThread.join(); + m_isAlreadyListening.store(false, std::memory_order_release); + + EASY_LOGMSG("Listening stopped\n"); +} + +bool ProfileManager::isListening() const +{ + return m_isAlreadyListening.load(std::memory_order_acquire); +} + +////////////////////////////////////////////////////////////////////////// + +void ProfileManager::listen(uint16_t _port) +{ + EASY_THREAD_SCOPE("EasyProfiler.Listen"); + + EASY_LOGMSG("Listening started\n"); + + EasySocket socket; + profiler::net::Message replyMessage(profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING); + + socket.bind(_port); + int bytes = 0; + while (!m_stopListen.load(std::memory_order_acquire)) + { + bool hasConnect = false; + + socket.listen(); + socket.accept(); + + hasConnect = true; + + // Send reply + { + const bool wasLowPriorityET = +#ifdef _WIN32 + EasyEventTracer::instance().isLowPriority(); +#else + false; +#endif + const profiler::net::EasyProfilerStatus connectionReply(m_profilerStatus.load(std::memory_order_acquire) == EASY_PROF_ENABLED, m_isEventTracingEnabled.load(std::memory_order_acquire), wasLowPriorityET); + bytes = socket.send(&connectionReply, sizeof(profiler::net::EasyProfilerStatus)); + hasConnect = bytes > 0; + } + + while (hasConnect && !m_stopListen.load(std::memory_order_acquire)) + { + char buffer[256] = {}; + + bytes = socket.receive(buffer, 255); + + hasConnect = bytes > 0; + + char *buf = &buffer[0]; + + if (bytes > 0) + { + profiler::net::Message* message = (profiler::net::Message*)buf; + if (!message->isEasyNetMessage()){ + continue; + } + + switch (message->type) + { + case profiler::net::MESSAGE_TYPE_CHECK_CONNECTION: + { + EASY_LOGMSG("receive MESSAGE_TYPE_CHECK_CONNECTION\n"); + break; + } + + case profiler::net::MESSAGE_TYPE_REQUEST_MAIN_FRAME_TIME_MAX_AVG_US: + { + profiler::timestamp_t maxDuration = maxFrameDuration(), avgDuration = avgFrameDuration(); + maxDuration = TICKS_TO_US(maxDuration); + avgDuration = TICKS_TO_US(avgDuration); + const profiler::net::TimestampMessage reply(profiler::net::MESSAGE_TYPE_REPLY_MAIN_FRAME_TIME_MAX_AVG_US, (uint32_t)maxDuration, (uint32_t)avgDuration); + bytes = socket.send(&reply, sizeof(profiler::net::TimestampMessage)); + hasConnect = bytes > 0; + break; + } + + case profiler::net::MESSAGE_TYPE_REQUEST_START_CAPTURE: + { + EASY_LOGMSG("receive REQUEST_START_CAPTURE\n"); + + ::profiler::timestamp_t t = 0; + EASY_FORCE_EVENT(t, "StartCapture", EASY_COLOR_START, profiler::OFF); + + m_dumpSpin.lock(); + const auto prev = m_profilerStatus.exchange(EASY_PROF_ENABLED, std::memory_order_release); + if (prev != EASY_PROF_ENABLED) { + enableEventTracer(); + m_beginTime = t; + } + m_dumpSpin.unlock(); + + replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING; + bytes = socket.send(&replyMessage, sizeof(replyMessage)); + hasConnect = bytes > 0; + + break; + } + + case profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE: + { + EASY_LOGMSG("receive REQUEST_STOP_CAPTURE\n"); + + m_dumpSpin.lock(); + auto time = getCurrentTime(); + const auto prev = m_profilerStatus.exchange(EASY_PROF_DUMP, std::memory_order_release); + if (prev == EASY_PROF_ENABLED) { + disableEventTracer(); + m_endTime = time; + } + EASY_FORCE_EVENT2(m_endTime, "StopCapture", EASY_COLOR_END, profiler::OFF); + + //TODO + //if connection aborted - ignore this part + + profiler::OStream os; + dumpBlocksToStream(os, false); + m_dumpSpin.unlock(); + + const auto size = os.stream().tellp(); + static const decltype(size) badSize = -1; + if (size != badSize) + { + const profiler::net::DataMessage dm(static_cast(size), profiler::net::MESSAGE_TYPE_REPLY_BLOCKS); + + const size_t packet_size = sizeof(dm) + dm.size; + std::string sendbuf; + sendbuf.reserve(packet_size + 1); + + if (sendbuf.capacity() >= packet_size) // check if there is enough memory + { + sendbuf.append((const char*)&dm, sizeof(dm)); + sendbuf += os.stream().str(); // TODO: Avoid double-coping data from stringstream! + os.clear(); + + bytes = socket.send(sendbuf.c_str(), packet_size); + hasConnect = bytes > 0; + } + else + { + EASY_ERROR("Can not send blocks. Not enough memory for allocating " << packet_size << " bytes"); + } + } + else + { + EASY_ERROR("Can not send blocks. Bad std::stringstream.tellp() == -1"); + } + + replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_BLOCKS_END; + bytes = socket.send(&replyMessage, sizeof(replyMessage)); + hasConnect = bytes > 0; + + break; + } + + case profiler::net::MESSAGE_TYPE_REQUEST_BLOCKS_DESCRIPTION: + { + EASY_LOGMSG("receive REQUEST_BLOCKS_DESCRIPTION\n"); + + profiler::OStream os; + + // Write profiler signature and version + os.write(PROFILER_SIGNATURE); + os.write(EASY_CURRENT_VERSION); + + // Write block descriptors + m_storedSpin.lock(); + os.write(static_cast(m_descriptors.size())); + os.write(m_usedMemorySize); + for (const auto descriptor : m_descriptors) + { + const auto name_size = descriptor->nameSize(); + const auto filename_size = descriptor->filenameSize(); + const auto size = static_cast(sizeof(profiler::SerializedBlockDescriptor) + name_size + filename_size); + + os.write(size); + os.write(*descriptor); + os.write(name_size); + os.write(descriptor->name(), name_size); + os.write(descriptor->filename(), filename_size); + } + m_storedSpin.unlock(); + // END of Write block descriptors. + + const auto size = os.stream().tellp(); + static const decltype(size) badSize = -1; + if (size != badSize) + { + const profiler::net::DataMessage dm(static_cast(size), profiler::net::MESSAGE_TYPE_REPLY_BLOCKS_DESCRIPTION); + + const size_t packet_size = sizeof(dm) + dm.size; + std::string sendbuf; + sendbuf.reserve(packet_size + 1); + + if (sendbuf.capacity() >= packet_size) // check if there is enough memory + { + sendbuf.append((const char*)&dm, sizeof(dm)); + sendbuf += os.stream().str(); // TODO: Avoid double-coping data from stringstream! + os.clear(); + + bytes = socket.send(sendbuf.c_str(), packet_size); + hasConnect = bytes > 0; + } + else + { + EASY_ERROR("Can not send block descriptions. Not enough memory for allocating " << packet_size << " bytes"); + } + } + else + { + EASY_ERROR("Can not send block descriptions. Bad std::stringstream.tellp() == -1"); + } + + replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_BLOCKS_DESCRIPTION_END; + bytes = socket.send(&replyMessage, sizeof(replyMessage)); + hasConnect = bytes > 0; + + break; + } + + case profiler::net::MESSAGE_TYPE_EDIT_BLOCK_STATUS: + { + auto data = reinterpret_cast(message); + + EASY_LOGMSG("receive EDIT_BLOCK_STATUS id=" << data->id << " status=" << data->status << std::endl); + + setBlockStatus(data->id, static_cast<::profiler::EasyBlockStatus>(data->status)); + + break; + } + + case profiler::net::MESSAGE_TYPE_EVENT_TRACING_STATUS: + { + auto data = reinterpret_cast(message); + + EASY_LOGMSG("receive EVENT_TRACING_STATUS on=" << data->flag << std::endl); + + m_isEventTracingEnabled.store(data->flag, std::memory_order_release); + break; + } + + case profiler::net::MESSAGE_TYPE_EVENT_TRACING_PRIORITY: + { +#if defined(_WIN32) || EASY_OPTION_LOG_ENABLED != 0 + auto data = reinterpret_cast(message); +#endif + + EASY_LOGMSG("receive EVENT_TRACING_PRIORITY low=" << data->flag << std::endl); + +#ifdef _WIN32 + EasyEventTracer::instance().setLowPriority(data->flag); +#endif + break; + } + + default: + break; + } + + //nn_freemsg (buf); + } + } + + + + } + +} + +////////////////////////////////////////////////////////////////////////// + diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/profile_manager.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/profile_manager.h new file mode 100644 index 0000000000..82ae4cb85c --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/profile_manager.h @@ -0,0 +1,603 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin + +Licensed under either of + * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +at your option. + +The MIT License + 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. + + +The Apache License, Version 2.0 (the "License"); + You may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +**/ + +#ifndef EASY_PROFILER_MANAGER_H +#define EASY_PROFILER_MANAGER_H + +#include +#include + +#include "spin_lock.h" +#include "outstream.h" +#include "hashed_cstr.h" + +#include +#include +#include +#include +#include +#include + +////////////////////////////////////////////////////////////////////////// + +#ifdef _WIN32 +#include +#elif defined(__APPLE__) +#include +#include +#else +#include +#include +#include +#include +#include +#endif + +#ifdef max +#undef max +#endif + +inline profiler::thread_id_t getCurrentThreadId() +{ +#ifdef _WIN32 + return (profiler::thread_id_t)::GetCurrentThreadId(); +#elif defined(__APPLE__) +# if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_6) || \ + (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0) + EASY_THREAD_LOCAL static uint64_t _id = 0; + if (!_id) + pthread_threadid_np(NULL, &_id); + return (profiler::thread_id_t)_id; +# else + return (profiler::thread_id_t)pthread_self(); +# endif +#else + EASY_THREAD_LOCAL static const profiler::thread_id_t _id = (profiler::thread_id_t)syscall(__NR_gettid); + return _id; +#endif +} + +namespace profiler { + + class SerializedBlock; + + struct do_not_calc_hash { + template inline size_t operator()(T _value) const { + return static_cast(_value); + } + }; + +} + +////////////////////////////////////////////////////////////////////////// + +#ifndef EASY_ENABLE_BLOCK_STATUS +# define EASY_ENABLE_BLOCK_STATUS 1 +#endif + +#ifndef EASY_ENABLE_ALIGNMENT +# define EASY_ENABLE_ALIGNMENT 0 +#endif + +#ifndef EASY_ALIGNMENT_SIZE +# define EASY_ALIGNMENT_SIZE 64 +#endif + + +#if EASY_ENABLE_ALIGNMENT == 0 +# define EASY_ALIGNED(TYPE, VAR, A) TYPE VAR +# define EASY_MALLOC(MEMSIZE, A) malloc(MEMSIZE) +# define EASY_FREE(MEMPTR) free(MEMPTR) +#else +# if defined(_MSC_VER) +# define EASY_ALIGNED(TYPE, VAR, A) __declspec(align(A)) TYPE VAR +# define EASY_MALLOC(MEMSIZE, A) _aligned_malloc(MEMSIZE, A) +# define EASY_FREE(MEMPTR) _aligned_free(MEMPTR) +# elif defined(__GNUC__) +# define EASY_ALIGNED(TYPE, VAR, A) TYPE VAR __attribute__(aligned(A)) +# else +# define EASY_ALIGNED(TYPE, VAR, A) TYPE VAR +# endif +#endif + +template +class chunk_allocator +{ + struct chunk { EASY_ALIGNED(int8_t, data[N], EASY_ALIGNMENT_SIZE); chunk* prev = nullptr; }; + + struct chunk_list + { + chunk* last = nullptr; + + ~chunk_list() + { + clear(); + } + + void clear() + { + do { + auto p = last; + last = last->prev; + EASY_FREE(p); + } while (last != nullptr); + } + + chunk& back() + { + return *last; + } + + void emplace_back() + { + auto prev = last; + last = ::new (EASY_MALLOC(sizeof(chunk), EASY_ALIGNMENT_SIZE)) chunk(); + last->prev = prev; + *(uint16_t*)last->data = 0; + } + + /** Invert current chunks list to enable to iterate over chunks list in direct order. + + This method is used by serialize(). + */ + void invert() + { + chunk* next = nullptr; + + while (last->prev != nullptr) { + auto p = last->prev; + last->prev = next; + next = last; + last = p; + } + + last->prev = next; + } + }; + + //typedef std::list chunk_list; + + chunk_list m_chunks; + uint32_t m_size; + uint16_t m_shift; + +public: + + chunk_allocator() : m_size(0), m_shift(0) + { + m_chunks.emplace_back(); + } + + /** Allocate n bytes. + + Automatically checks if there is enough preserved memory to store additional n bytes + and allocates additional buffer if needed. + */ + void* allocate(uint16_t n) + { + ++m_size; + + if (!need_expand(n)) + { + int8_t* data = m_chunks.back().data + m_shift; + m_shift += n + sizeof(uint16_t); + + *(uint16_t*)data = n; + data = data + sizeof(uint16_t); + + if (m_shift + 1 < N) + *(uint16_t*)(data + n) = 0; + + return data; + } + + m_shift = n + sizeof(uint16_t); + m_chunks.emplace_back(); + auto data = m_chunks.back().data; + + *(uint16_t*)data = n; + data = data + sizeof(uint16_t); + + *(uint16_t*)(data + n) = 0; + return data; + } + + /** Check if current storage is not enough to store additional n bytes. + */ + inline bool need_expand(uint16_t n) const + { + return (m_shift + n + sizeof(uint16_t)) > N; + } + + inline uint32_t size() const + { + return m_size; + } + + inline bool empty() const + { + return m_size == 0; + } + + void clear() + { + m_size = 0; + m_shift = 0; + m_chunks.clear(); + m_chunks.emplace_back(); + } + + /** Serialize data to stream. + + \warning Data will be cleared after serialization. + */ + void serialize(profiler::OStream& _outputStream) + { + // Chunks are stored in reversed order (stack). + // To be able to iterate them in direct order we have to invert chunks list. + m_chunks.invert(); + + // Iterate over chunks and perform blocks serialization + auto current = m_chunks.last; + do { + const int8_t* data = current->data; + uint16_t i = 0; + while (i + 1 < N && *(uint16_t*)data != 0) { + const uint16_t size = sizeof(uint16_t) + *(uint16_t*)data; + _outputStream.write((const char*)data, size); + data = data + size; + i += size; + } + current = current->prev; + } while (current != nullptr); + + clear(); + } + +}; // END of class chunk_allocator. + +////////////////////////////////////////////////////////////////////////// + +class NonscopedBlock : public profiler::Block +{ + char* m_runtimeName; ///< a copy of _runtimeName to make it safe to begin block in one function and end it in another + + NonscopedBlock() = delete; + NonscopedBlock(const NonscopedBlock&) = delete; + NonscopedBlock(NonscopedBlock&&) = delete; + NonscopedBlock& operator = (const NonscopedBlock&) = delete; + NonscopedBlock& operator = (NonscopedBlock&&) = delete; + +public: + + NonscopedBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, bool = false); + ~NonscopedBlock(); + + /** Copy string from m_name to m_runtimeName to make it safe to end block in another function. + + Performs any work if block is ON and m_name != "" + */ + void copyname(); + + void destroy(); + +}; // END of class NonscopedBlock. + +////////////////////////////////////////////////////////////////////////// + +template +inline void destroy_elem(T*) +{ + +} + +inline void destroy_elem(NonscopedBlock* _elem) +{ + _elem->destroy(); +} + +template +class StackBuffer +{ + struct chunk { int8_t data[sizeof(T)]; }; + + std::list m_overflow; ///< List of additional stack elements if current capacity of buffer is not enough + T* m_buffer; ///< Contiguous buffer used for stack + uint32_t m_size; ///< Current size of stack + uint32_t m_capacity; ///< Current capacity of m_buffer + uint32_t m_maxcapacity; ///< Maximum used capacity including m_buffer and m_overflow + +public: + + StackBuffer(uint32_t N) : m_buffer(static_cast(malloc(N * sizeof(T)))), m_size(0), m_capacity(N), m_maxcapacity(N) + { + } + + ~StackBuffer() + { + for (uint32_t i = 0; i < m_size; ++i) + destroy_elem(m_buffer + i); + + free(m_buffer); + + for (auto& elem : m_overflow) + destroy_elem(reinterpret_cast(elem.data + 0)); + } + + template + T& push(TArgs ... _args) + { + if (m_size < m_capacity) + return *(::new (m_buffer + m_size++) T(_args...)); + + m_overflow.emplace_back(); + const uint32_t cap = m_capacity + static_cast(m_overflow.size()); + if (m_maxcapacity < cap) + m_maxcapacity = cap; + + return *(::new (m_overflow.back().data + 0) T(_args...)); + } + + void pop() + { + if (m_overflow.empty()) + { + // m_size should not be equal to 0 here because ProfileManager behavior does not allow such situation + destroy_elem(m_buffer + --m_size); + + if (m_size == 0 && m_maxcapacity > m_capacity) + { + // When stack gone empty we can resize buffer to use enough space in the future + free(m_buffer); + m_maxcapacity = m_capacity = std::max(m_maxcapacity, m_capacity << 1); + m_buffer = static_cast(malloc(m_capacity * sizeof(T))); + } + + return; + } + + destroy_elem(reinterpret_cast(m_overflow.back().data + 0)); + m_overflow.pop_back(); + } + +}; // END of class StackBuffer. + +////////////////////////////////////////////////////////////////////////// + +template +struct BlocksList +{ + BlocksList() = default; + + std::vector openedList; + chunk_allocator closedList; + uint64_t usedMemorySize = 0; + + void clearClosed() { + //closedList.clear(); + usedMemorySize = 0; + } + +}; // END of struct BlocksList. + +////////////////////////////////////////////////////////////////////////// + +class CSwitchBlock : public profiler::CSwitchEvent +{ + const char* m_name; + +public: + + CSwitchBlock(profiler::timestamp_t _begin_time, profiler::thread_id_t _tid, const char* _runtimeName); + inline const char* name() const { return m_name; } +}; + +////////////////////////////////////////////////////////////////////////// + +const uint16_t SIZEOF_BLOCK = sizeof(profiler::BaseBlockData) + 1 + sizeof(uint16_t); // SerializedBlock stores BaseBlockData + at least 1 character for name ('\0') + 2 bytes for size of serialized data +const uint16_t SIZEOF_CSWITCH = sizeof(profiler::CSwitchEvent) + 1 + sizeof(uint16_t); // SerializedCSwitch also stores additional 4 bytes to be able to save 64-bit thread_id + +struct ThreadStorage +{ + StackBuffer nonscopedBlocks; + BlocksList, SIZEOF_BLOCK * (uint16_t)128U> blocks; + BlocksList sync; + + std::string name; ///< Thread name + +#ifndef _WIN32 + const pthread_t pthread_id; ///< Thread pointer +#endif + + const profiler::thread_id_t id; ///< Thread ID + std::atomic expired; ///< Is thread expired + std::atomic_bool frame; ///< Is new frame opened + bool allowChildren; ///< False if one of previously opened blocks has OFF_RECURSIVE or ON_WITHOUT_CHILDREN status + bool named; ///< True if thread name was set + bool guarded; ///< True if thread has been registered using ThreadGuard + + void storeBlock(const profiler::Block& _block); + void storeCSwitch(const CSwitchBlock& _block); + void clearClosed(); + void popSilent(); + + ThreadStorage(); + +}; // END of struct ThreadStorage. + +////////////////////////////////////////////////////////////////////////// + +typedef uint64_t processid_t; + +class BlockDescriptor; + +class ProfileManager +{ +#ifndef EASY_MAGIC_STATIC_CPP11 + friend class ProfileManagerInstance; +#endif + + ProfileManager(); + ProfileManager(const ProfileManager& p) = delete; + ProfileManager& operator=(const ProfileManager&) = delete; + + typedef profiler::guard_lock guard_lock_t; + typedef std::map map_of_threads_stacks; + typedef std::vector block_descriptors_t; + +#ifdef EASY_PROFILER_HASHED_CSTR_DEFINED + typedef std::unordered_map descriptors_map_t; +#else + typedef std::unordered_map descriptors_map_t; +#endif + + const processid_t m_processId; + + map_of_threads_stacks m_threads; + block_descriptors_t m_descriptors; + descriptors_map_t m_descriptorsMap; + uint64_t m_usedMemorySize; + profiler::timestamp_t m_beginTime; + profiler::timestamp_t m_endTime; + std::atomic m_frameMax; + std::atomic m_frameAvg; + std::atomic m_frameCur; + profiler::spin_lock m_spin; + profiler::spin_lock m_storedSpin; + profiler::spin_lock m_dumpSpin; + std::atomic m_mainThreadId; + std::atomic m_profilerStatus; + std::atomic_bool m_isEventTracingEnabled; + std::atomic_bool m_isAlreadyListening; + std::atomic_bool m_frameMaxReset; + std::atomic_bool m_frameAvgReset; + + std::string m_csInfoFilename = "/tmp/cs_profiling_info.log"; + + uint32_t dumpBlocksToStream(profiler::OStream& _outputStream, bool _lockSpin); + void setBlockStatus(profiler::block_id_t _id, profiler::EasyBlockStatus _status); + + std::thread m_listenThread; + void listen(uint16_t _port); + + std::atomic_bool m_stopListen; + +public: + + static ProfileManager& instance(); + ~ProfileManager(); + + const profiler::BaseBlockDescriptor* addBlockDescriptor(profiler::EasyBlockStatus _defaultStatus, + const char* _autogenUniqueId, + const char* _name, + const char* _filename, + int _line, + profiler::block_type_t _block_type, + profiler::color_t _color, + bool _copyName = false); + + bool storeBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName); + bool storeBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, profiler::timestamp_t _beginTime, profiler::timestamp_t _endTime); + void beginBlock(profiler::Block& _block); + void beginNonScopedBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName); + void endBlock(); + profiler::timestamp_t maxFrameDuration(); + profiler::timestamp_t avgFrameDuration(); + profiler::timestamp_t curFrameDuration() const; + void setEnabled(bool isEnable); + bool isEnabled() const; + void setEventTracingEnabled(bool _isEnable); + bool isEventTracingEnabled() const; + uint32_t dumpBlocksToFile(const char* filename); + const char* registerThread(const char* name, profiler::ThreadGuard& threadGuard); + const char* registerThread(const char* name); + + void setContextSwitchLogFilename(const char* name) + { + m_csInfoFilename = name; + } + + const char* getContextSwitchLogFilename() const + { + return m_csInfoFilename.c_str(); + } + + void beginContextSwitch(profiler::thread_id_t _thread_id, profiler::timestamp_t _time, profiler::thread_id_t _target_thread_id, const char* _target_process, bool _lockSpin = true); + void endContextSwitch(profiler::thread_id_t _thread_id, processid_t _process_id, profiler::timestamp_t _endtime, bool _lockSpin = true); + void startListen(uint16_t _port); + void stopListen(); + bool isListening() const; + +private: + + void beginFrame(); + void endFrame(); + + void enableEventTracer(); + void disableEventTracer(); + + char checkThreadExpired(ThreadStorage& _registeredThread); + + void storeBlockForce(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, ::profiler::timestamp_t& _timestamp); + void storeBlockForce2(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, ::profiler::timestamp_t _timestamp); + void storeBlockForce2(ThreadStorage& _registeredThread, const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, ::profiler::timestamp_t _timestamp); + + ThreadStorage& _threadStorage(profiler::thread_id_t _thread_id); + ThreadStorage* _findThreadStorage(profiler::thread_id_t _thread_id); + + inline ThreadStorage& threadStorage(profiler::thread_id_t _thread_id) + { + guard_lock_t lock(m_spin); + return _threadStorage(_thread_id); + } + + inline ThreadStorage* findThreadStorage(profiler::thread_id_t _thread_id) + { + guard_lock_t lock(m_spin); + return _findThreadStorage(_thread_id); + } + +}; // END of class ProfileManager. + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_PROFILER_MANAGER_H diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/reader.cpp b/Source/ThirdParty/easy_profiler/easy_profiler_core/reader.cpp new file mode 100644 index 0000000000..314424d0c0 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/reader.cpp @@ -0,0 +1,1025 @@ +/************************************************************************ +* file name : reader.cpp +* ----------------- : +* creation time : 2016/06/19 +* authors : Sergey Yagovtsev, Victor Zarubkin +* emails : yse.sey@gmail.com, v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of fillTreesFromFile function +* : which reads profiler file and fill profiler blocks tree. +* ----------------- : +* change log : * 2016/06/19 Sergey Yagovtsev: First fillTreesFromFile implementation. +* : +* : * 2016/06/25 Victor Zarubkin: Removed unnecessary memory allocation and copy +* : when creating and inserting blocks into the tree. +* : +* : * 2016/06/26 Victor Zarubkin: Added statistics gathering (min, max, average duration, +* : number of block calls). +* : * 2016/06/26 Victor Zarubkin, Sergey Yagovtsev: Added statistics gathering for root +* : blocks in the tree. +* : +* : * 2016/06/29 Victor Zarubkin: Added calculaton of total children number for blocks. +* : +* : * 2016/06/30 Victor Zarubkin: Added this header. +* : Added tree depth calculation. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include + +#include "hashed_cstr.h" + +#include +#include +#include +#include +#include +#include + +////////////////////////////////////////////////////////////////////////// + +typedef uint64_t processid_t; + +extern const uint32_t PROFILER_SIGNATURE; +extern const uint32_t EASY_CURRENT_VERSION; + +# define EASY_VERSION_INT(v_major, v_minor, v_patch) ((static_cast(v_major) << 24) | (static_cast(v_minor) << 16) | static_cast(v_patch)) +const uint32_t MIN_COMPATIBLE_VERSION = EASY_VERSION_INT(0, 1, 0); ///< minimal compatible version (.prof file format was not changed seriously since this version) +const uint32_t EASY_V_100 = EASY_VERSION_INT(1, 0, 0); ///< in v1.0.0 some additional data were added into .prof file +const uint32_t EASY_V_130 = EASY_VERSION_INT(1, 3, 0); ///< in v1.3.0 changed sizeof(thread_id_t) uint32_t -> uint64_t +# undef EASY_VERSION_INT + +const uint64_t TIME_FACTOR = 1000000000ULL; + +// TODO: use 128 bit integer operations for better accuracy +#define EASY_USE_FLOATING_POINT_CONVERSION + +#ifdef EASY_USE_FLOATING_POINT_CONVERSION + +// Suppress warnings about double to uint64 conversion +# ifdef _MSC_VER +# pragma warning(disable:4244) +# elif defined(__GNUC__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wconversion" +# pragma GCC diagnostic ignored "-Wsign-conversion" +# elif defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wconversion" +# pragma clang diagnostic ignored "-Wsign-conversion" +# endif + +# define EASY_CONVERT_TO_NANO(t, freq, factor) t *= factor + +#else + +# define EASY_CONVERT_TO_NANO(t, freq, factor) t *= TIME_FACTOR; t /= freq + +#endif + +////////////////////////////////////////////////////////////////////////// + +inline bool isCompatibleVersion(uint32_t _version) +{ + return _version >= MIN_COMPATIBLE_VERSION; +} + +inline void write(::std::stringstream& _stream, const char* _value, size_t _size) +{ + _stream.write(_value, _size); +} + +template +inline void write(::std::stringstream& _stream, const T& _value) +{ + _stream.write((const char*)&_value, sizeof(T)); +} + +////////////////////////////////////////////////////////////////////////// + +namespace profiler { + + void SerializedData::set(char* _data, uint64_t _size) + { + delete [] m_data; + m_data = _data; + m_size = _size; + } + + void SerializedData::set(uint64_t _size) + { + if (_size != 0) + set(new char[_size], _size); + else + set(nullptr, 0); + } + + void SerializedData::extend(uint64_t _size) + { + auto olddata = m_data; + auto oldsize = m_size; + + m_size = oldsize + _size; + m_data = new char[m_size]; + + if (olddata != nullptr) { + memcpy(m_data, olddata, oldsize); + delete [] olddata; + } + } + + extern "C" PROFILER_API void release_stats(BlockStatistics*& _stats) + { + if (_stats == nullptr) + return; + + if (--_stats->calls_number == 0) + delete _stats; + + _stats = nullptr; + } + +} + +////////////////////////////////////////////////////////////////////////// + +#ifdef EASY_PROFILER_HASHED_CSTR_DEFINED + +typedef ::std::unordered_map<::profiler::block_id_t, ::profiler::BlockStatistics*, ::profiler::passthrough_hash<::profiler::block_id_t> > StatsMap; + +/** \note It is absolutely safe to use hashed_cstr (which simply stores pointer) because std::unordered_map, +which uses it as a key, exists only inside fillTreesFromFile function. */ +typedef ::std::unordered_map<::profiler::hashed_cstr, ::profiler::block_id_t> IdMap; + +typedef ::std::unordered_map<::profiler::hashed_cstr, ::profiler::BlockStatistics*> CsStatsMap; + +#else + +// TODO: Create optimized version of profiler::hashed_cstr for Linux too. +typedef ::std::unordered_map<::profiler::block_id_t, ::profiler::BlockStatistics*, ::profiler::passthrough_hash<::profiler::block_id_t> > StatsMap; +typedef ::std::unordered_map<::profiler::hashed_stdstring, ::profiler::block_id_t> IdMap; +typedef ::std::unordered_map<::profiler::hashed_stdstring, ::profiler::BlockStatistics*> CsStatsMap; + +#endif + +////////////////////////////////////////////////////////////////////////// + +/** \brief Updates statistics for a profiler block. + +\param _stats_map Storage of statistics for blocks. +\param _current Pointer to the current block. +\param _stats Reference to the variable where pointer to the block statistics must be written. + +\note All blocks with similar name have the same pointer to statistics information. + +\note As all profiler block keeps a pointer to it's statistics, all similar blocks +automatically receive statistics update. + +*/ +::profiler::BlockStatistics* update_statistics(StatsMap& _stats_map, const ::profiler::BlocksTree& _current, ::profiler::block_index_t _current_index, ::profiler::block_index_t _parent_index, const ::profiler::blocks_t& _blocks, bool _calculate_children = true) +{ + auto duration = _current.node->duration(); + //StatsMap::key_type key(_current.node->name()); + //auto it = _stats_map.find(key); + auto it = _stats_map.find(_current.node->id()); + if (it != _stats_map.end()) + { + // Update already existing statistics + + auto stats = it->second; // write pointer to statistics into output (this is BlocksTree:: per_thread_stats or per_parent_stats or per_frame_stats) + + ++stats->calls_number; // update calls number of this block + stats->total_duration += duration; // update summary duration of all block calls + + if (_calculate_children) + { + for (auto i : _current.children) + stats->total_children_duration += _blocks[i].node->duration(); + } + + if (duration > _blocks[stats->max_duration_block].node->duration()) + { + // update max duration + stats->max_duration_block = _current_index; + //stats->max_duration = duration; + } + + if (duration < _blocks[stats->min_duration_block].node->duration()) + { + // update min duraton + stats->min_duration_block = _current_index; + //stats->min_duration = duration; + } + + // average duration is calculated inside average_duration() method by dividing total_duration to the calls_number + + return stats; + } + + // This is first time the block appear in the file. + // Create new statistics. + auto stats = new ::profiler::BlockStatistics(duration, _current_index, _parent_index); + //_stats_map.emplace(key, stats); + _stats_map.emplace(_current.node->id(), stats); + + if (_calculate_children) + { + for (auto i : _current.children) + stats->total_children_duration += _blocks[i].node->duration(); + } + + return stats; +} + +::profiler::BlockStatistics* update_statistics(CsStatsMap& _stats_map, const ::profiler::BlocksTree& _current, ::profiler::block_index_t _current_index, ::profiler::block_index_t _parent_index, const ::profiler::blocks_t& _blocks, bool _calculate_children = true) +{ + auto duration = _current.node->duration(); + CsStatsMap::key_type key(_current.node->name()); + auto it = _stats_map.find(key); + if (it != _stats_map.end()) + { + // Update already existing statistics + + auto stats = it->second; // write pointer to statistics into output (this is BlocksTree:: per_thread_stats or per_parent_stats or per_frame_stats) + + ++stats->calls_number; // update calls number of this block + stats->total_duration += duration; // update summary duration of all block calls + + if (_calculate_children) + { + for (auto i : _current.children) + stats->total_children_duration += _blocks[i].node->duration(); + } + + if (duration > _blocks[stats->max_duration_block].node->duration()) + { + // update max duration + stats->max_duration_block = _current_index; + //stats->max_duration = duration; + } + + if (duration < _blocks[stats->min_duration_block].node->duration()) + { + // update min duraton + stats->min_duration_block = _current_index; + //stats->min_duration = duration; + } + + // average duration is calculated inside average_duration() method by dividing total_duration to the calls_number + + return stats; + } + + // This is first time the block appear in the file. + // Create new statistics. + auto stats = new ::profiler::BlockStatistics(duration, _current_index, _parent_index); + _stats_map.emplace(key, stats); + + if (_calculate_children) + { + for (auto i : _current.children) + stats->total_children_duration += _blocks[i].node->duration(); + } + + return stats; +} + +////////////////////////////////////////////////////////////////////////// + +void update_statistics_recursive(StatsMap& _stats_map, ::profiler::BlocksTree& _current, ::profiler::block_index_t _current_index, ::profiler::block_index_t _parent_index, ::profiler::blocks_t& _blocks) +{ + _current.per_frame_stats = update_statistics(_stats_map, _current, _current_index, _parent_index, _blocks, false); + for (auto i : _current.children) + { + _current.per_frame_stats->total_children_duration += _blocks[i].node->duration(); + update_statistics_recursive(_stats_map, _blocks[i], i, _parent_index, _blocks); + } +} + +////////////////////////////////////////////////////////////////////////// + +/*void validate_pointers(::std::atomic& _progress, const char* _oldbase, ::profiler::SerializedData& _serialized_blocks, ::profiler::blocks_t& _blocks, size_t _size) +{ + if (_oldbase == nullptr) + { + _progress.store(25, ::std::memory_order_release); + return; + } + + for (size_t i = 0; i < _size; ++i) + { + auto& tree = _blocks[i]; + auto dist = ::std::distance(_oldbase, reinterpret_cast(tree.node)); + tree.node = reinterpret_cast<::profiler::SerializedBlock*>(_serialized_blocks.data() + dist); + _progress.store(20 + static_cast(5 * i / _size), ::std::memory_order_release); + } +} + +void validate_pointers(::std::atomic& _progress, const char* _oldbase, ::profiler::SerializedData& _serialized_descriptors, ::profiler::descriptors_list_t& _descriptors, size_t _size) +{ + if (_oldbase == nullptr) + { + _progress.store(5, ::std::memory_order_release); + return; + } + + for (size_t i = 0; i < _size; ++i) + { + auto dist = ::std::distance(_oldbase, reinterpret_cast(_descriptors[i])); + _descriptors[i] = reinterpret_cast<::profiler::SerializedBlockDescriptor*>(_serialized_descriptors.data() + dist); + _progress.store(static_cast(5 * i / _size)); + } +}*/ + +////////////////////////////////////////////////////////////////////////// + +extern "C" { + + PROFILER_API ::profiler::block_index_t fillTreesFromFile(::std::atomic& progress, const char* filename, + ::profiler::SerializedData& serialized_blocks, + ::profiler::SerializedData& serialized_descriptors, + ::profiler::descriptors_list_t& descriptors, + ::profiler::blocks_t& blocks, + ::profiler::thread_blocks_tree_t& threaded_trees, + uint32_t& total_descriptors_number, + uint32_t& version, + bool gather_statistics, + ::std::stringstream& _log) + { + auto oldprogress = progress.exchange(0, ::std::memory_order_release); + if (oldprogress < 0) + { + _log << "Reading was interrupted"; + return 0; + } + + ::std::ifstream inFile(filename, ::std::fstream::binary); + if (!inFile.is_open()) + { + _log << "Can not open file " << filename; + return 0; + } + + ::std::stringstream str; + + // Replace str buffer to inFile buffer to avoid redundant copying + typedef ::std::basic_iostream<::std::stringstream::char_type, ::std::stringstream::traits_type> stringstream_parent; + stringstream_parent& s = str; + auto oldbuf = s.rdbuf(inFile.rdbuf()); + + // Read data from file + auto result = fillTreesFromStream(progress, str, serialized_blocks, serialized_descriptors, descriptors, blocks, + threaded_trees, total_descriptors_number, version, gather_statistics, _log); + + // Restore old str buffer to avoid possible second memory free on stringstream destructor + s.rdbuf(oldbuf); + + return result; + } + + ////////////////////////////////////////////////////////////////////////// + + PROFILER_API ::profiler::block_index_t fillTreesFromStream(::std::atomic& progress, ::std::stringstream& inFile, + ::profiler::SerializedData& serialized_blocks, + ::profiler::SerializedData& serialized_descriptors, + ::profiler::descriptors_list_t& descriptors, + ::profiler::blocks_t& blocks, + ::profiler::thread_blocks_tree_t& threaded_trees, + uint32_t& total_descriptors_number, + uint32_t& version, + bool gather_statistics, + ::std::stringstream& _log) + { + EASY_FUNCTION(::profiler::colors::Cyan); + + auto oldprogress = progress.exchange(0, ::std::memory_order_release); + if (oldprogress < 0) + { + _log << "Reading was interrupted"; + return 0; + } + + uint32_t signature = 0; + inFile.read((char*)&signature, sizeof(uint32_t)); + if (signature != PROFILER_SIGNATURE) + { + _log << "Wrong signature " << signature << "\nThis is not EasyProfiler file/stream."; + return 0; + } + + version = 0; + inFile.read((char*)&version, sizeof(uint32_t)); + if (!isCompatibleVersion(version)) + { + _log << "Incompatible version: v" << (version >> 24) << "." << ((version & 0x00ff0000) >> 16) << "." << (version & 0x0000ffff); + return 0; + } + + processid_t pid = 0; + if (version > EASY_V_100) + { + if (version < EASY_V_130) + { + uint32_t old_pid = 0; + inFile.read((char*)&old_pid, sizeof(uint32_t)); + pid = old_pid; + } + else + { + inFile.read((char*)&pid, sizeof(processid_t)); + } + } + + int64_t file_cpu_frequency = 0LL; + inFile.read((char*)&file_cpu_frequency, sizeof(int64_t)); + uint64_t cpu_frequency = file_cpu_frequency; + const double conversion_factor = static_cast(TIME_FACTOR) / static_cast(cpu_frequency); + + ::profiler::timestamp_t begin_time = 0ULL; + ::profiler::timestamp_t end_time = 0ULL; + inFile.read((char*)&begin_time, sizeof(::profiler::timestamp_t)); + inFile.read((char*)&end_time, sizeof(::profiler::timestamp_t)); + if (cpu_frequency != 0) + { + EASY_CONVERT_TO_NANO(begin_time, cpu_frequency, conversion_factor); + EASY_CONVERT_TO_NANO(end_time, cpu_frequency, conversion_factor); + } + + uint32_t total_blocks_number = 0; + inFile.read((char*)&total_blocks_number, sizeof(uint32_t)); + if (total_blocks_number == 0) + { + _log << "Profiled blocks number == 0"; + return 0; + } + + uint64_t memory_size = 0; + inFile.read((char*)&memory_size, sizeof(decltype(memory_size))); + if (memory_size == 0) + { + _log << "Wrong memory size == 0 for " << total_blocks_number << " blocks"; + return 0; + } + + total_descriptors_number = 0; + inFile.read((char*)&total_descriptors_number, sizeof(uint32_t)); + if (total_descriptors_number == 0) + { + _log << "Blocks description number == 0"; + return 0; + } + + uint64_t descriptors_memory_size = 0; + inFile.read((char*)&descriptors_memory_size, sizeof(decltype(descriptors_memory_size))); + if (descriptors_memory_size == 0) + { + _log << "Wrong memory size == 0 for " << total_descriptors_number << " blocks descriptions"; + return 0; + } + + descriptors.reserve(total_descriptors_number); + //const char* olddata = append_regime ? serialized_descriptors.data() : nullptr; + serialized_descriptors.set(descriptors_memory_size); + //validate_pointers(progress, olddata, serialized_descriptors, descriptors, descriptors.size()); + + uint64_t i = 0; + while (!inFile.eof() && descriptors.size() < total_descriptors_number) + { + uint16_t sz = 0; + inFile.read((char*)&sz, sizeof(sz)); + if (sz == 0) + { + descriptors.push_back(nullptr); + continue; + } + + //if (i + sz > descriptors_memory_size) { + // printf("FILE CORRUPTED\n"); + // return 0; + //} + + char* data = serialized_descriptors[i]; + inFile.read(data, sz); + auto descriptor = reinterpret_cast<::profiler::SerializedBlockDescriptor*>(data); + descriptors.push_back(descriptor); + + i += sz; + auto oldprogress = progress.exchange(static_cast(15 * i / descriptors_memory_size), ::std::memory_order_release); + if (oldprogress < 0) + { + _log << "Reading was interrupted"; + return 0; // Loading interrupted + } + } + + typedef ::std::unordered_map<::profiler::thread_id_t, StatsMap, ::profiler::passthrough_hash<::profiler::thread_id_t> > PerThreadStats; + PerThreadStats parent_statistics, frame_statistics; + IdMap identification_table; + + blocks.reserve(total_blocks_number); + //olddata = append_regime ? serialized_blocks.data() : nullptr; + serialized_blocks.set(memory_size); + //validate_pointers(progress, olddata, serialized_blocks, blocks, blocks.size()); + + i = 0; + uint32_t read_number = 0; + ::profiler::block_index_t blocks_counter = 0; + ::std::vector name; + + const size_t thread_id_t_size = version < EASY_V_130 ? sizeof(uint32_t) : sizeof(::profiler::thread_id_t); + + while (!inFile.eof() && read_number < total_blocks_number) + { + EASY_BLOCK("Read thread data", ::profiler::colors::DarkGreen); + + ::profiler::thread_id_t thread_id = 0; + inFile.read((char*)&thread_id, thread_id_t_size); + + auto& root = threaded_trees[thread_id]; + + uint16_t name_size = 0; + inFile.read((char*)&name_size, sizeof(uint16_t)); + if (name_size != 0) + { + name.resize(name_size); + inFile.read(name.data(), name_size); + root.thread_name = name.data(); + } + + CsStatsMap per_thread_statistics_cs; + + uint32_t blocks_number_in_thread = 0; + inFile.read((char*)&blocks_number_in_thread, sizeof(decltype(blocks_number_in_thread))); + auto threshold = read_number + blocks_number_in_thread; + while (!inFile.eof() && read_number < threshold) + { + EASY_BLOCK("Read context switch", ::profiler::colors::Green); + + ++read_number; + + uint16_t sz = 0; + inFile.read((char*)&sz, sizeof(sz)); + if (sz == 0) + { + _log << "Bad CSwitch block size == 0"; + return 0; + } + + char* data = serialized_blocks[i]; + inFile.read(data, sz); + i += sz; + auto baseData = reinterpret_cast<::profiler::SerializedCSwitch*>(data); + auto t_begin = reinterpret_cast<::profiler::timestamp_t*>(data); + auto t_end = t_begin + 1; + + if (cpu_frequency != 0) + { + EASY_CONVERT_TO_NANO(*t_begin, cpu_frequency, conversion_factor); + EASY_CONVERT_TO_NANO(*t_end, cpu_frequency, conversion_factor); + } + + if (*t_end > begin_time) + { + if (*t_begin < begin_time) + *t_begin = begin_time; + + blocks.emplace_back(); + ::profiler::BlocksTree& tree = blocks.back(); + tree.cs = baseData; + const auto block_index = blocks_counter++; + + root.wait_time += baseData->duration(); + root.sync.emplace_back(block_index); + + if (gather_statistics) + { + EASY_BLOCK("Gather per thread statistics", ::profiler::colors::Coral); + tree.per_thread_stats = update_statistics(per_thread_statistics_cs, tree, block_index, ~0U, blocks);//, thread_id, blocks); + } + } + + auto oldprogress = progress.exchange(20 + static_cast(70 * i / memory_size), ::std::memory_order_release); + if (oldprogress < 0) + { + _log << "Reading was interrupted"; + return 0; // Loading interrupted + } + } + + if (inFile.eof()) + break; + + StatsMap per_thread_statistics; + + blocks_number_in_thread = 0; + inFile.read((char*)&blocks_number_in_thread, sizeof(decltype(blocks_number_in_thread))); + threshold = read_number + blocks_number_in_thread; + while (!inFile.eof() && read_number < threshold) + { + EASY_BLOCK("Read block", ::profiler::colors::Green); + + ++read_number; + + uint16_t sz = 0; + inFile.read((char*)&sz, sizeof(sz)); + if (sz == 0) + { + _log << "Bad block size == 0"; + return 0; + } + + char* data = serialized_blocks[i]; + inFile.read(data, sz); + i += sz; + auto baseData = reinterpret_cast<::profiler::SerializedBlock*>(data); + if (baseData->id() >= total_descriptors_number) + { + _log << "Bad block id == " << baseData->id(); + return 0; + } + + auto desc = descriptors[baseData->id()]; + if (desc == nullptr) + { + _log << "Bad block id == " << baseData->id() << ". Description is null."; + return 0; + } + + auto t_begin = reinterpret_cast<::profiler::timestamp_t*>(data); + auto t_end = t_begin + 1; + + if (cpu_frequency != 0) + { + EASY_CONVERT_TO_NANO(*t_begin, cpu_frequency, conversion_factor); + EASY_CONVERT_TO_NANO(*t_end, cpu_frequency, conversion_factor); + } + + if (*t_end >= begin_time) + { + if (*t_begin < begin_time) + *t_begin = begin_time; + + blocks.emplace_back(); + ::profiler::BlocksTree& tree = blocks.back(); + tree.node = baseData; + const auto block_index = blocks_counter++; + + if (*tree.node->name() != 0) + { + // If block has runtime name then generate new id for such block. + // Blocks with the same name will have same id. + + IdMap::key_type key(tree.node->name()); + auto it = identification_table.find(key); + if (it != identification_table.end()) + { + // There is already block with such name, use it's id + baseData->setId(it->second); + } + else + { + // There were no blocks with such name, generate new id and save it in the table for further usage. + auto id = static_cast<::profiler::block_id_t>(descriptors.size()); + identification_table.emplace(key, id); + if (descriptors.capacity() == descriptors.size()) + descriptors.reserve((descriptors.size() * 3) >> 1); + descriptors.push_back(descriptors[baseData->id()]); + baseData->setId(id); + } + } + + if (!root.children.empty()) + { + auto& back = blocks[root.children.back()]; + auto t1 = back.node->end(); + auto mt0 = tree.node->begin(); + if (mt0 < t1)//parent - starts earlier than last ends + { + //auto lower = ::std::lower_bound(root.children.begin(), root.children.end(), tree); + /**/ + EASY_BLOCK("Find children", ::profiler::colors::Blue); + auto rlower1 = ++root.children.rbegin(); + for (; rlower1 != root.children.rend() && !(mt0 > blocks[*rlower1].node->begin()); ++rlower1); + auto lower = rlower1.base(); + ::std::move(lower, root.children.end(), ::std::back_inserter(tree.children)); + + root.children.erase(lower, root.children.end()); + EASY_END_BLOCK; + + if (gather_statistics) + { + EASY_BLOCK("Gather statistic within parent", ::profiler::colors::Magenta); + auto& per_parent_statistics = parent_statistics[thread_id]; + per_parent_statistics.clear(); + + //per_parent_statistics.reserve(tree.children.size()); // this gives slow-down on Windows + //per_parent_statistics.reserve(tree.children.size() * 2); // this gives no speed-up on Windows + // TODO: check this behavior on Linux + + for (auto i : tree.children) + { + auto& child = blocks[i]; + child.per_parent_stats = update_statistics(per_parent_statistics, child, i, block_index, blocks); + if (tree.depth < child.depth) + tree.depth = child.depth; + } + } + else + { + for (auto i : tree.children) + { + const auto& child = blocks[i]; + if (tree.depth < child.depth) + tree.depth = child.depth; + } + } + + if (tree.depth == 254) + { + // 254 because we need 1 additional level for root (thread). + // In other words: real stack depth = 1 root block + 254 children + + if (*tree.node->name() != 0) + _log << "Stack depth exceeded value of 254\nfor block \"" << desc->name() << "\""; + else + _log << "Stack depth exceeded value of 254\nfor block \"" << desc->name() << "\"\nfrom file \"" << desc->file() << "\":" << desc->line(); + + return 0; + } + + ++tree.depth; + } + } + + ++root.blocks_number; + root.children.emplace_back(block_index);// ::std::move(tree)); + if (desc->type() == ::profiler::BLOCK_TYPE_EVENT) + root.events.emplace_back(block_index); + + + if (gather_statistics) + { + EASY_BLOCK("Gather per thread statistics", ::profiler::colors::Coral); + tree.per_thread_stats = update_statistics(per_thread_statistics, tree, block_index, ~0U, blocks);//, thread_id, blocks); + } + } + + auto oldprogress = progress.exchange(20 + static_cast(70 * i / memory_size), ::std::memory_order_release); + if (oldprogress < 0) + { + _log << "Reading was interrupted"; + return 0; // Loading interrupted + } + } + } + + if (progress.load(::std::memory_order_acquire) < 0) + { + _log << "Reading was interrupted"; + return 0; // Loading interrupted + } + + EASY_BLOCK("Gather statistics for roots", ::profiler::colors::Purple); + if (gather_statistics) + { + ::std::vector<::std::thread> statistics_threads; + statistics_threads.reserve(threaded_trees.size()); + + for (auto& it : threaded_trees) + { + auto& root = it.second; + root.thread_id = it.first; + //root.tree.shrink_to_fit(); + + auto& per_frame_statistics = frame_statistics[root.thread_id]; + auto& per_parent_statistics = parent_statistics[it.first]; + per_parent_statistics.clear(); + + statistics_threads.emplace_back(::std::thread([&per_parent_statistics, &per_frame_statistics, &blocks, &descriptors](::profiler::BlocksTreeRoot& root) + { + //::std::sort(root.sync.begin(), root.sync.end(), [&blocks](::profiler::block_index_t left, ::profiler::block_index_t right) + //{ + // return blocks[left].node->begin() < blocks[right].node->begin(); + //}); + + ::profiler::block_index_t cs_index = 0; + for (auto i : root.children) + { + auto& frame = blocks[i]; + + if (descriptors[frame.node->id()]->type() == ::profiler::BLOCK_TYPE_BLOCK) + ++root.frames_number; + + frame.per_parent_stats = update_statistics(per_parent_statistics, frame, i, ~0U, blocks);//, root.thread_id, blocks); + + per_frame_statistics.clear(); + update_statistics_recursive(per_frame_statistics, frame, i, i, blocks); + + if (cs_index < root.sync.size()) + { + CsStatsMap frame_stats_cs; + do { + + auto j = root.sync[cs_index]; + auto& cs = blocks[j]; + if (cs.node->end() < frame.node->begin()) + continue; + if (cs.node->begin() > frame.node->end()) + break; + cs.per_frame_stats = update_statistics(frame_stats_cs, cs, cs_index, i, blocks); + + } while (++cs_index < root.sync.size()); + } + + if (root.depth < frame.depth) + root.depth = frame.depth; + + root.profiled_time += frame.node->duration(); + } + + ++root.depth; + }, ::std::ref(root))); + } + + int j = 0, n = static_cast(statistics_threads.size()); + for (auto& t : statistics_threads) + { + t.join(); + progress.store(90 + (10 * ++j) / n, ::std::memory_order_release); + } + } + else + { + int j = 0, n = static_cast(threaded_trees.size()); + for (auto& it : threaded_trees) + { + auto& root = it.second; + root.thread_id = it.first; + + //::std::sort(root.sync.begin(), root.sync.end(), [&blocks](::profiler::block_index_t left, ::profiler::block_index_t right) + //{ + // return blocks[left].node->begin() < blocks[right].node->begin(); + //}); + + //root.tree.shrink_to_fit(); + for (auto i : root.children) + { + auto& frame = blocks[i]; + + if (descriptors[frame.node->id()]->type() == ::profiler::BLOCK_TYPE_BLOCK) + ++root.frames_number; + + if (root.depth < frame.depth) + root.depth = frame.depth; + + root.profiled_time += frame.node->duration(); + } + + ++root.depth; + + progress.store(90 + (10 * ++j) / n, ::std::memory_order_release); + } + } + // No need to delete BlockStatistics instances - they will be deleted inside BlocksTree destructors + + return blocks_counter; + } + + ////////////////////////////////////////////////////////////////////////// + + PROFILER_API bool readDescriptionsFromStream(::std::atomic& progress, ::std::stringstream& inFile, + ::profiler::SerializedData& serialized_descriptors, + ::profiler::descriptors_list_t& descriptors, + ::std::stringstream& _log) + { + EASY_FUNCTION(::profiler::colors::Cyan); + + progress.store(0); + + uint32_t signature = 0; + inFile.read((char*)&signature, sizeof(uint32_t)); + if (signature != PROFILER_SIGNATURE) + { + _log << "Wrong file signature.\nThis is not EasyProfiler file/stream."; + return false; + } + + uint32_t version = 0; + inFile.read((char*)&version, sizeof(uint32_t)); + if (!isCompatibleVersion(version)) + { + _log << "Incompatible version: v" << (version >> 24) << "." << ((version & 0x00ff0000) >> 16) << "." << (version & 0x0000ffff); + return false; + } + + uint32_t total_descriptors_number = 0; + inFile.read((char*)&total_descriptors_number, sizeof(decltype(total_descriptors_number))); + if (total_descriptors_number == 0) + { + _log << "Blocks description number == 0"; + return false; + } + + uint64_t descriptors_memory_size = 0; + inFile.read((char*)&descriptors_memory_size, sizeof(decltype(descriptors_memory_size))); + if (descriptors_memory_size == 0) + { + _log << "Wrong memory size == 0 for " << total_descriptors_number << " blocks descriptions"; + return false; + } + + descriptors.reserve(total_descriptors_number); + //const char* olddata = append_regime ? serialized_descriptors.data() : nullptr; + serialized_descriptors.set(descriptors_memory_size); + //validate_pointers(progress, olddata, serialized_descriptors, descriptors, descriptors.size()); + + uint64_t i = 0; + while (!inFile.eof() && descriptors.size() < total_descriptors_number) + { + uint16_t sz = 0; + inFile.read((char*)&sz, sizeof(sz)); + if (sz == 0) + { + descriptors.push_back(nullptr); + continue; + } + + //if (i + sz > descriptors_memory_size) { + // printf("FILE CORRUPTED\n"); + // return 0; + //} + + char* data = serialized_descriptors[i]; + inFile.read(data, sz); + auto descriptor = reinterpret_cast<::profiler::SerializedBlockDescriptor*>(data); + descriptors.push_back(descriptor); + + i += sz; + auto oldprogress = progress.exchange(static_cast(100 * i / descriptors_memory_size), ::std::memory_order_release); + if (oldprogress < 0) + { + _log << "Reading was interrupted"; + return false; // Loading interrupted + } + } + + return !descriptors.empty(); + } + + ////////////////////////////////////////////////////////////////////////// + +} + +#undef EASY_CONVERT_TO_NANO + +#ifdef EASY_USE_FLOATING_POINT_CONVERSION +# ifdef _MSC_VER +# pragma warning(default:4244) +# elif defined(__GNUC__) +# pragma GCC diagnostic pop +# elif defined(__clang__) +# pragma clang diagnostic pop +# endif +# undef EASY_USE_FLOATING_POINT_CONVERSION +#endif diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/resources.rc b/Source/ThirdParty/easy_profiler/easy_profiler_core/resources.rc new file mode 100644 index 0000000000..e68eb4d8cc --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/resources.rc @@ -0,0 +1,30 @@ +1 VERSIONINFO + +# define EASY_STRINGIFY(a) #a +# define EASY_STRINGIFICATION(a) EASY_STRINGIFY(a) + +#define EASY_PROFILER_PRODUCT_VERSION "v" EASY_STRINGIFICATION(EASY_PROFILER_VERSION_MAJOR) "." \ + EASY_STRINGIFICATION(EASY_PROFILER_VERSION_MINOR) "." \ + EASY_STRINGIFICATION(EASY_PROFILER_VERSION_PATCH) + +FILEVERSION EASY_PROFILER_VERSION_MAJOR, EASY_PROFILER_VERSION_MINOR, EASY_PROFILER_VERSION_PATCH +PRODUCTVERSION EASY_PROFILER_VERSION_MAJOR, EASY_PROFILER_VERSION_MINOR, EASY_PROFILER_VERSION_PATCH +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "080904b0" + BEGIN + VALUE "CompanyName", "EasySolutions" + VALUE "FileDescription", "Lightweight profiler library for c++" + VALUE "LegalCopyright", "Copyright (C) 2016-2017 Victor Zarubkin, Sergey Yagovtsev" + VALUE "LegalTrademarks1", "All Rights Reserved" + VALUE "LegalTrademarks2", "All Rights Reserved" + VALUE "ProductName", "easy_profiler lib" + VALUE "ProductVersion", EASY_PROFILER_PRODUCT_VERSION + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x809, 1200 + END +END diff --git a/Source/ThirdParty/easy_profiler/easy_profiler_core/spin_lock.h b/Source/ThirdParty/easy_profiler/easy_profiler_core/spin_lock.h new file mode 100644 index 0000000000..705540dd17 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/easy_profiler_core/spin_lock.h @@ -0,0 +1,126 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin + +Licensed under either of + * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +at your option. + +The MIT License + 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. + + +The Apache License, Version 2.0 (the "License"); + You may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +**/ + +#ifndef EASY_PROFILER_SPIN_LOCK_H +#define EASY_PROFILER_SPIN_LOCK_H + +#define EASY_USE_CRITICAL_SECTION // Use CRITICAL_SECTION instead of std::atomic_flag + +#if defined(_WIN32) && defined(EASY_USE_CRITICAL_SECTION) +#include +#else +#include +#endif + +namespace profiler { + +#if defined(_WIN32) && defined(EASY_USE_CRITICAL_SECTION) + // std::atomic_flag on Windows works slower than critical section, so we will use it instead of std::atomic_flag... + // By the way, Windows critical sections are slower than std::atomic_flag on Unix. + class spin_lock { CRITICAL_SECTION m_lock; public: + + void lock() { + EnterCriticalSection(&m_lock); + } + + void unlock() { + LeaveCriticalSection(&m_lock); + } + + spin_lock() { + InitializeCriticalSection(&m_lock); + } + + ~spin_lock() { + DeleteCriticalSection(&m_lock); + } + }; +#else + // std::atomic_flag on Unix works fine and very fast (almost instant!) + class spin_lock { ::std::atomic_flag m_lock; public: + + void lock() { + while (m_lock.test_and_set(::std::memory_order_acquire)); + } + + void unlock() { + m_lock.clear(::std::memory_order_release); + } + + spin_lock() { + m_lock.clear(); + } + }; +#endif + + template + class guard_lock + { + T& m_lock; + bool m_isLocked = false; + + public: + + explicit guard_lock(T& m) : m_lock(m) { + m_lock.lock(); + m_isLocked = true; + } + + ~guard_lock() { + unlock(); + } + + inline void unlock() { + if (m_isLocked) { + m_lock.unlock(); + m_isLocked = false; + } + } + }; + +} // END of namespace profiler. + +#ifdef EASY_USE_CRITICAL_SECTION +# undef EASY_USE_CRITICAL_SECTION +#endif + +#endif // EASY_PROFILER_SPIN_LOCK_H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/CMakeLists.txt b/Source/ThirdParty/easy_profiler/profiler_gui/CMakeLists.txt new file mode 100644 index 0000000000..61e49e2c87 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/CMakeLists.txt @@ -0,0 +1,67 @@ +#set(CMAKE_PREFIX_PATH f:/qt/5.5/5.6/msvc2013_64/lib/cmake) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +find_package(Qt5Widgets) + +if (NOT Qt5Widgets_FOUND) + if (NOT("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") AND WIN32) + set(APPLICATION_PLATFORM WIN32) + endif () + add_executable(profiler_gui ${APPLICATION_PLATFORM} + main.cpp + blocks_graphics_view.h + blocks_graphics_view.cpp + blocks_tree_widget.h + blocks_tree_widget.cpp + descriptors_tree_widget.h + descriptors_tree_widget.cpp + easy_chronometer_item.h + easy_chronometer_item.cpp + easy_frame_rate_viewer.h + easy_frame_rate_viewer.cpp + easy_graphics_item.h + easy_graphics_item.cpp + easy_graphics_scrollbar.h + easy_graphics_scrollbar.cpp + easy_qtimer.h + easy_qtimer.cpp + globals.h + globals.cpp + globals_qobjects.cpp + main_window.h + main_window.cpp + tree_widget_item.h + tree_widget_item.cpp + tree_widget_loader.h + tree_widget_loader.cpp + resources.qrc + resources.rc + ) + # ATOMIC BEGIN + target_link_libraries(profiler_gui Qt5::Widgets) + set_target_properties(profiler_gui PROPERTIES OUTPUT_NAME Profiler) + set_property(TARGET profiler_gui PROPERTY RUNTIME_OUTPUT_DIRECTORY ${ATOMIC_SOURCE_DIR}/Artifacts/Build/Profiler) + if (TARGET easy_profiler_md) + target_link_libraries(profiler_gui easy_profiler_md) + else () + target_link_libraries(profiler_gui easy_profiler) + endif () + # ATOMIC END + if (WIN32) + target_compile_definitions(profiler_gui PRIVATE -D_WIN32_WINNT=0x0600) + endif () + if (MINGW) + target_compile_definitions(profiler_gui PRIVATE -DSTRSAFE_NO_DEPRECATE) + endif () +else () + # ATOMIC BEGIN + message(WARNING + "Qt5 not found! Profiler GUI application will not be built!\n" + "You may have to specify path to Qt SDK in CMAKE_PREFIX_PATH variable." + ) + # ATOMIC END +endif () + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/blocks_graphics_view.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/blocks_graphics_view.cpp new file mode 100644 index 0000000000..22f43680dd --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/blocks_graphics_view.cpp @@ -0,0 +1,2466 @@ +/************************************************************************ +* file name : blocks_graphics_view.cpp +* ----------------- : +* creation time : 2016/06/26 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of GraphicsScene and GraphicsView and +* : it's auxiliary classes for displyaing easy_profiler blocks tree. +* ----------------- : +* change log : * 2016/06/26 Victor Zarubkin: Moved sources from graphics_view.h +* : and renamed classes from My* to Prof*. +* : +* : * 2016/06/27 Victor Zarubkin: Added text shifting relatively to it's parent item. +* : Disabled border lines painting because of vertical lines painting bug. +* : Changed height of blocks. Variable thread-block height. +* : +* : * 2016/06/29 Victor Zarubkin: Highly optimized painting performance and memory consumption. +* : +* : * 2016/06/30 Victor Zarubkin: Replaced doubles with floats (in ProfBlockItem) for less memory consumption. +* : +* : * 2016/09/15 Victor Zarubkin: Moved sources of EasyGraphicsItem and EasyChronometerItem to separate files. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "blocks_graphics_view.h" +#include "easy_graphics_item.h" +#include "easy_chronometer_item.h" +#include "easy_graphics_scrollbar.h" +#include "globals.h" + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +const qreal MIN_SCALE = pow(::profiler_gui::SCALING_COEFFICIENT_INV, 70); // Up to 1000 sec scale +const qreal MAX_SCALE = pow(::profiler_gui::SCALING_COEFFICIENT, 45); // ~23000 --- Up to 10 ns scale +const qreal BASE_SCALE = pow(::profiler_gui::SCALING_COEFFICIENT_INV, 25); // ~0.003 + +const uint16_t TIMELINE_ROW_SIZE = 20; + +const QRgb BACKGROUND_1 = ::profiler::colors::Grey300; +const QRgb BACKGROUND_2 = ::profiler::colors::White; +const QRgb TIMELINE_BACKGROUND = 0x20000000 | (::profiler::colors::Grey800 & 0x00ffffff);// 0x20303030; + +const int IDLE_TIMER_INTERVAL = 200; // 5Hz +const uint64_t IDLE_TIME = 400; + +const int FLICKER_INTERVAL = 10; // 100Hz +const qreal FLICKER_FACTOR = 16.0 / FLICKER_INTERVAL; + +const auto BG_FONT = ::profiler_gui::EFont("Helvetica", 10, QFont::Bold); +const auto CHRONOMETER_FONT = ::profiler_gui::EFont("Helvetica", 16, QFont::Bold); + +#ifdef max +#undef max +#endif + +#ifdef min +#undef min +#endif + +////////////////////////////////////////////////////////////////////////// + +inline int sign(int _value) { return _value < 0 ? -1 : 1; } +inline int absmin(int _a, int _b) { return abs(_a) < abs(_b) ? _a : _b; } +inline qreal clamp(qreal _minValue, qreal _value, qreal _maxValue) { return _value < _minValue ? _minValue : (_value > _maxValue ? _maxValue : _value); } + +////////////////////////////////////////////////////////////////////////// + +template +inline T logn(T _value) +{ + static const double div = 1.0 / log2((double)N); + return log2(_value) * div; +} + +////////////////////////////////////////////////////////////////////////// + +EasyBoldLabel::EasyBoldLabel(const QString& _text, QWidget* _parent) : QLabel(_text, _parent) +{ + auto f = font(); + f.setBold(true); + setFont(f); +} + +EasyBoldLabel::~EasyBoldLabel() +{ + +} + +////////////////////////////////////////////////////////////////////////// + +void EasyBackgroundItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem*, QWidget*) +{ + auto const sceneView = static_cast(scene()->parent()); + const auto visibleSceneRect = sceneView->visibleSceneRect(); + const auto currentScale = sceneView->scale(); + const auto offset = sceneView->offset(); + const auto left = offset * currentScale; + const auto h = visibleSceneRect.height(); + const auto visibleBottom = h - 1; + + QRectF rect; + + _painter->save(); + _painter->setTransform(QTransform::fromTranslate(-x(), -y())); + + const auto& items = sceneView->getItems(); + if (!items.empty()) + { + static const uint16_t OVERLAP = ::profiler_gui::THREADS_ROW_SPACING >> 1; + static const QBrush brushes[2] = {QColor::fromRgb(BACKGROUND_1), QColor::fromRgb(BACKGROUND_2)}; + int i = -1; + + // Draw background + _painter->setPen(Qt::NoPen); + for (auto item : items) + { + ++i; + + auto br = item->boundingRect(); + auto top = item->y() + br.top() - visibleSceneRect.top(); + auto bottom = top + br.height(); + + if (top > h || bottom < 0) + continue; + + if (item->threadId() == EASY_GLOBALS.selected_thread) + _painter->setBrush(QBrush(QColor::fromRgb(::profiler_gui::SELECTED_THREAD_BACKGROUND))); + else + _painter->setBrush(brushes[i & 1]); + + rect.setRect(0, top - OVERLAP, visibleSceneRect.width(), br.height() + ::profiler_gui::THREADS_ROW_SPACING); + const auto dh = rect.bottom() - visibleBottom; + if (dh > 0) + rect.setHeight(rect.height() - dh); + + if (rect.top() < 0) + rect.setTop(0); + + _painter->drawRect(rect); + } + } + + // Draw timeline scale marks ---------------- + _painter->setBrush(QColor::fromRgba(TIMELINE_BACKGROUND)); + + const auto sceneStep = sceneView->timelineStep(); + const auto factor = ::profiler_gui::timeFactor(sceneStep); + const auto step = sceneStep * currentScale; + auto first = static_cast(offset / sceneStep); + const int odd = first & 1; + const auto nsteps = (1 + odd) * 2 + static_cast(visibleSceneRect.width() / step); + first -= odd; + + QPen pen(Qt::darkGray); + pen.setWidth(2); + _painter->setPen(pen); + _painter->drawLine(QPointF(0, h), QPointF(visibleSceneRect.width(), h)); + _painter->setPen(Qt::darkGray); + + QLineF marks[20]; + qreal first_x = first * sceneStep; + const auto textWidth = QFontMetricsF(_painter->font(), sceneView).width(QString::number(static_cast(0.5 + first_x * factor))) * ::profiler_gui::FONT_METRICS_FACTOR + 10; + const int n = 1 + static_cast(textWidth / step); + int next = first % n; + if (next) + next = n - next; + + first_x *= currentScale; + for (int i = 0; i < nsteps; ++i, --next) + { + auto current = first_x - left + step * i; + + if ((i & 1) == 0) + { + rect.setRect(current, 0, step, h); + _painter->drawRect(rect); + + for (int j = 0; j < 20; ++j) + { + auto xmark = current + j * step * 0.1; + marks[j].setLine(xmark, h, xmark, h + ((j % 5) ? 4 : 8)); + } + + _painter->drawLines(marks, 20); + } + + if (next <= 0) + { + next = n; + _painter->setPen(Qt::black); + _painter->drawText(QPointF(current + 1, h + 17), QString::number(static_cast(0.5 + (current + left) * factor / currentScale))); + _painter->setPen(Qt::darkGray); + } + + // TEST + // this is for testing (order of lines will be painted): + //_painter->setPen(Qt::black); + //_painter->drawText(QPointF(current + step * 0.4, h - 20), QString::number(i)); + //_painter->setPen(Qt::gray); + // TEST + } + // END Draw timeline scale marks ~~~~~~~~~~~~ + + _painter->restore(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTimelineIndicatorItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem*, QWidget*) +{ + const auto sceneView = static_cast(scene()->parent()); + const auto visibleSceneRect = sceneView->visibleSceneRect(); + const auto step = sceneView->timelineStep() * sceneView->scale(); + const QString text = ::profiler_gui::autoTimeStringInt(units2microseconds(sceneView->timelineStep())); // Displayed text + + // Draw scale indicator + _painter->save(); + _painter->setTransform(QTransform::fromTranslate(-x(), -y())); + //_painter->setCompositionMode(QPainter::CompositionMode_Difference); + _painter->setBrush(Qt::NoBrush); + + QPen pen(Qt::black); + pen.setWidth(3); + _painter->setPen(pen); + + _painter->drawLine(QLineF(visibleSceneRect.width() - 9 - step, visibleSceneRect.height() - 10, visibleSceneRect.width() - 11, visibleSceneRect.height() - 10)); + + _painter->setPen(Qt::black); + _painter->drawLine(QLineF(visibleSceneRect.width() - 10 - step, visibleSceneRect.height() - 6, visibleSceneRect.width() - 10 - step, visibleSceneRect.height() - 14)); + _painter->drawLine(QLineF(visibleSceneRect.width() - 10, visibleSceneRect.height() - 6, visibleSceneRect.width() - 10, visibleSceneRect.height() - 14)); + + _painter->setPen(Qt::black); + _painter->setFont(BG_FONT); + _painter->drawText(QRectF(visibleSceneRect.width() - 10 - step, visibleSceneRect.height() - 63, step, 50), Qt::AlignRight | Qt::AlignBottom | Qt::TextDontClip, text); + + _painter->restore(); +} + +////////////////////////////////////////////////////////////////////////// + +EasyGraphicsView::EasyGraphicsView(QWidget* _parent) + : Parent(_parent) + , m_beginTime(::std::numeric_limits::max()) + , m_sceneWidth(0) + , m_scale(1) + , m_offset(0) + , m_timelineStep(0) + , m_idleTime(0) + , m_mouseButtons(Qt::NoButton) + , m_pScrollbar(nullptr) + , m_chronometerItem(nullptr) + , m_chronometerItemAux(nullptr) + , m_popupWidget(nullptr) + , m_flickerSpeedX(0) + , m_flickerSpeedY(0) + , m_flickerCounterX(0) + , m_flickerCounterY(0) + , m_bDoubleClick(false) + , m_bUpdatingRect(false) + , m_bEmpty(true) +{ + initMode(); + setScene(new QGraphicsScene(this)); + updateVisibleSceneRect(); +} + +EasyGraphicsView::~EasyGraphicsView() +{ +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::removePopup(bool _removeFromScene) +{ + if (m_popupWidget != nullptr) + { + auto widget = m_popupWidget->widget(); + widget->setParent(nullptr); + m_popupWidget->setWidget(nullptr); + delete widget; + + if (_removeFromScene) + scene()->removeItem(m_popupWidget); + + m_popupWidget = nullptr; + } +} + +////////////////////////////////////////////////////////////////////////// + +qreal EasyGraphicsView::sceneWidth() const +{ + return m_sceneWidth; +} + +qreal EasyGraphicsView::chronoTime() const +{ + return m_chronometerItem->width(); +} + +qreal EasyGraphicsView::chronoTimeAux() const +{ + return m_chronometerItemAux->width(); +} + +////////////////////////////////////////////////////////////////////////// + +EasyChronometerItem* EasyGraphicsView::createChronometer(bool _main) +{ + auto chronoItem = new EasyChronometerItem(_main); + chronoItem->setColor(_main ? ::profiler_gui::CHRONOMETER_COLOR : ::profiler_gui::CHRONOMETER_COLOR2); + chronoItem->setBoundingRect(sceneRect()); + chronoItem->hide(); + scene()->addItem(chronoItem); + + return chronoItem; +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::clear() +{ + const QSignalBlocker blocker(this), sceneBlocker(scene()); // block all scene signals (otherwise clear() would be extremely slow!) + + // Stop flicking + m_flickerTimer.stop(); + m_flickerSpeedX = 0; + m_flickerSpeedY = 0; + m_flickerCounterX = 0; + m_flickerCounterY = 0; + + // Clear all items + removePopup(); + scene()->clear(); + m_items.clear(); + m_selectedBlocks.clear(); + + m_beginTime = ::std::numeric_limits::max(); // reset begin time + m_scale = 1; // scale back to initial 100% scale + m_timelineStep = 1; + m_offset = 0; // scroll back to the beginning of the scene + + m_idleTimer.stop(); + m_idleTime = 0; + + // Reset necessary flags + m_bEmpty = true; + + m_sceneWidth = 10; + setSceneRect(0, 0, 10, 10); + + // notify ProfTreeWidget that selection was reset + emit intervalChanged(m_selectedBlocks, m_beginTime, 0, 0, false); +} + +void EasyGraphicsView::setTree(const ::profiler::thread_blocks_tree_t& _blocksTree) +{ + // clear scene + clear(); + + if (_blocksTree.empty()) + { + return; + } + + auto bgItem = new EasyBackgroundItem(); + scene()->addItem(bgItem); + + // set new blocks tree + // calculate scene size and fill it with items + + // Calculating start and end time + ::profiler::timestamp_t finish = 0, busyTime = 0; + ::profiler::thread_id_t longestTree = 0, mainTree = 0; + for (const auto& threadTree : _blocksTree) + { + const auto& t = threadTree.second; + + auto timestart = m_beginTime; + auto timefinish = finish; + + if (!t.children.empty()) + timestart = blocksTree(t.children.front()).node->begin(); + if (!t.sync.empty()) + timestart = ::std::min(timestart, blocksTree(t.sync.front()).node->begin()); + + if (!t.children.empty()) + timefinish = blocksTree(t.children.back()).node->end(); + if (!t.sync.empty()) + timefinish = ::std::max(timefinish, blocksTree(t.sync.back()).node->end()); + + if (m_beginTime > timestart) + m_beginTime = timestart; + + if (finish < timefinish) + finish = timefinish; + + if (t.profiled_time > busyTime) { + busyTime = t.profiled_time; + longestTree = threadTree.first; + } + + if (mainTree == 0 && !strcmp(t.name(), "Main")) + mainTree = threadTree.first; + } + + const decltype(m_beginTime) additional_offset = (finish - m_beginTime) / 20; // Additional 5% before first block and after last block + finish += additional_offset; + m_beginTime -= ::std::min(m_beginTime, additional_offset); + EASY_GLOBALS.begin_time = m_beginTime; + + // Sort threads by name + ::std::vector<::std::reference_wrapper > sorted_roots; + sorted_roots.reserve(_blocksTree.size()); + for (const auto& threadTree : _blocksTree) + sorted_roots.push_back(threadTree.second); + ::std::sort(sorted_roots.begin(), sorted_roots.end(), [](const ::profiler::BlocksTreeRoot& _a, const ::profiler::BlocksTreeRoot& _b) { + return _a.thread_name < _b.thread_name; + }); + + // Filling scene with items + m_items.reserve(_blocksTree.size()); + qreal y = TIMELINE_ROW_SIZE; + const EasyGraphicsItem *longestItem = nullptr, *mainThreadItem = nullptr; + for (const ::profiler::BlocksTreeRoot& t : sorted_roots) + { + if (m_items.size() == 0xff) + { + qWarning() << "Warning: Maximum threads number (255 threads) exceeded! See EasyGraphicsView::setTree() : " << __LINE__ << " in file " << __FILE__; + break; + } + + // fill scene with new items + qreal h = 0, x = 0; + + if (!t.children.empty()) + x = time2position(blocksTree(t.children.front()).node->begin()); + else if (!t.sync.empty()) + x = time2position(blocksTree(t.sync.front()).node->begin()); + + auto item = new EasyGraphicsItem(static_cast(m_items.size()), t); + if (t.depth) + item->setLevels(t.depth); + item->setPos(0, y); + + qreal children_duration = 0; + + if (!t.children.empty()) + { + uint32_t dummy = 0; + children_duration = setTree(item, t.children, h, dummy, y, 0); + } + else + { + if (!t.sync.empty()) + children_duration = time2position(blocksTree(t.sync.back()).node->end()) - x; + h = ::profiler_gui::GRAPHICS_ROW_SIZE; + } + + item->setBoundingRect(0, 0, children_duration + x, h); + m_items.push_back(item); + scene()->addItem(item); + + y += h + ::profiler_gui::THREADS_ROW_SPACING; + + if (longestTree == t.thread_id) + longestItem = item; + + if (mainTree == t.thread_id) + mainThreadItem = item; + } + + // Calculating scene rect + m_sceneWidth = time2position(finish); + setSceneRect(0, 0, m_sceneWidth, y + TIMELINE_ROW_SIZE); + + // Center view on the beginning of the scene + updateVisibleSceneRect(); + setScrollbar(m_pScrollbar); + + // Create new chronometer item (previous item was destroyed by scene on scene()->clear()). + // It will be shown on mouse right button click. + m_chronometerItemAux = createChronometer(false); + m_chronometerItem = createChronometer(true); + + bgItem->setBoundingRect(0, 0, m_sceneWidth, y); + auto indicator = new EasyTimelineIndicatorItem(); + indicator->setBoundingRect(0, 0, m_sceneWidth, y); + scene()->addItem(indicator); + + // Setting flags + m_bEmpty = false; + + scaleTo(BASE_SCALE); + + + emit treeChanged(); + + if (mainThreadItem != nullptr) + { + longestItem = mainThreadItem; + } + + if (longestItem != nullptr) + { + EASY_GLOBALS.selected_thread = longestItem->threadId(); + emit EASY_GLOBALS.events.selectedThreadChanged(longestItem->threadId()); + + scrollTo(longestItem); + m_pScrollbar->setHistogramSource(longestItem->threadId(), longestItem->items(0)); + if (!longestItem->items(0).empty()) + m_pScrollbar->setValue(longestItem->items(0).front().left() - m_pScrollbar->sliderWidth() * 0.25); + } + + m_idleTimer.start(IDLE_TIMER_INTERVAL); +} + +const EasyGraphicsView::Items &EasyGraphicsView::getItems() const +{ + return m_items; +} + +qreal EasyGraphicsView::setTree(EasyGraphicsItem* _item, const ::profiler::BlocksTree::children_t& _children, qreal& _height, uint32_t& _maxDepthChild, qreal _y, short _level) +{ + if (_children.empty()) + { + return 0; + } + + const auto level = static_cast(_level); + const auto n = static_cast(_children.size()); + _item->reserve(level, n); + + _maxDepthChild = 0; + uint8_t maxDepth = 0; + const short next_level = _level + 1; + bool warned = false; + qreal total_duration = 0, prev_end = 0, maxh = 0; + qreal start_time = -1; + uint32_t j = 0; + for (auto child_index : _children) + { + auto& gui_block = easyBlock(child_index); + const auto& child = gui_block.tree; + if (child.depth > maxDepth) + { + maxDepth = child.depth; + _maxDepthChild = j; + } + + auto xbegin = time2position(child.node->begin()); + if (start_time < 0) + { + start_time = xbegin; + } + + auto duration = time2position(child.node->end()) - xbegin; + + //const auto dt = xbegin - prev_end; + //if (dt < 0) + //{ + // duration += dt; + // xbegin -= dt; + //} + + //static const qreal MIN_DURATION = 0.25; + //if (duration < MIN_DURATION) + // duration = MIN_DURATION; + + const auto i = _item->addItem(level); + auto& b = _item->getItem(level, i); + + gui_block.graphics_item = _item->index(); + gui_block.graphics_item_level = level; + gui_block.graphics_item_index = i; + + if (next_level < 256 && next_level < _item->levels() && !child.children.empty()) + { + b.children_begin = static_cast(_item->items(static_cast(next_level)).size()); + } + else + { + ::profiler_gui::set_max(b.children_begin); + } + + qreal h = 0; + qreal children_duration = 0; + uint32_t maxDepthChild = 0; + + if (next_level < 256) + { + children_duration = setTree(_item, child.children, h, maxDepthChild, _y + ::profiler_gui::GRAPHICS_ROW_SIZE_FULL, next_level); + } + else if (!child.children.empty() && !warned) + { + warned = true; + qWarning() << "Warning: Maximum blocks depth (255) exceeded! See EasyGraphicsView::setTree() : " << __LINE__ << " in file " << __FILE__; + } + + if (duration < children_duration) + { + duration = children_duration; + } + + if (h > maxh) + { + maxh = h; + } + + b.block = child_index;// &child; + +#ifndef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + b.neighbours = n; + b.state = j > 0 || level == 0 ? 0 : -1; +#else + b.max_depth_child = maxDepthChild; +#endif + + b.setPos(xbegin, duration); + //b.totalHeight = ::profiler_gui::GRAPHICS_ROW_SIZE + h; + + prev_end = xbegin + duration; + total_duration = prev_end - start_time; + + ++j; + } + + _height += ::profiler_gui::GRAPHICS_ROW_SIZE_FULL + maxh; + + return total_duration; +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::setScrollbar(EasyGraphicsScrollbar* _scrollbar) +{ + auto const prevScrollbar = m_pScrollbar; + const bool makeConnect = prevScrollbar == nullptr || prevScrollbar != _scrollbar; + + if (prevScrollbar != nullptr && prevScrollbar != _scrollbar) + { + disconnect(prevScrollbar, &EasyGraphicsScrollbar::valueChanged, this, &This::onGraphicsScrollbarValueChange); + disconnect(prevScrollbar, &EasyGraphicsScrollbar::wheeled, this, &This::onGraphicsScrollbarWheel); + } + + m_pScrollbar = _scrollbar; + m_pScrollbar->clear(); + m_pScrollbar->setRange(0, m_sceneWidth); + + auto vbar = verticalScrollBar(); + const int vbar_width = (vbar != nullptr && vbar->isVisible() ? vbar->width() + 2 : 0); + m_pScrollbar->setSliderWidth(m_visibleSceneRect.width() + vbar_width); + + if (makeConnect) + { + connect(m_pScrollbar, &EasyGraphicsScrollbar::valueChanged, this, &This::onGraphicsScrollbarValueChange); + connect(m_pScrollbar, &EasyGraphicsScrollbar::wheeled, this, &This::onGraphicsScrollbarWheel); + } + + EASY_GLOBALS.selected_thread = 0; + emit EASY_GLOBALS.events.selectedThreadChanged(0); +} + +////////////////////////////////////////////////////////////////////////// + +int EasyGraphicsView::updateVisibleSceneRect() +{ + m_visibleSceneRect = mapToScene(rect()).boundingRect(); + + auto vbar = verticalScrollBar(); + int vbar_width = 0; + if (vbar && vbar->isVisible()) + vbar_width = vbar->width() + 2; + + m_visibleSceneRect.setWidth(m_visibleSceneRect.width() - vbar_width); + m_visibleSceneRect.setHeight(m_visibleSceneRect.height() - TIMELINE_ROW_SIZE); + + return vbar_width; +} + +void EasyGraphicsView::updateTimelineStep(qreal _windowWidth) +{ + const auto time = units2microseconds(_windowWidth); + if (time < 100) + m_timelineStep = 1e-2; + else if (time < 10e3) + m_timelineStep = 1; + else if (time < 10e6) + m_timelineStep = 1e3; + else + m_timelineStep = 1e6; + + const auto optimal_steps = static_cast(40 * m_visibleSceneRect.width() / 1500); + auto steps = time / m_timelineStep; + while (steps > optimal_steps) { + m_timelineStep *= 10; + steps *= 0.1; + } + + m_timelineStep = microseconds2units(m_timelineStep); +} + +void EasyGraphicsView::repaintScene() +{ + scene()->update(m_visibleSceneRect); + emit sceneUpdated(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::scaleTo(qreal _scale) +{ + if (m_bEmpty) + { + return; + } + + // have to limit scale because of Qt's QPainter feature: it doesn't draw text + // with very big coordinates (but it draw rectangles with the same coordinates good). + m_scale = clamp(MIN_SCALE, _scale, MAX_SCALE); + const int vbar_width = updateVisibleSceneRect(); + + // Update slider width for scrollbar + const auto windowWidth = (m_visibleSceneRect.width() + vbar_width) / m_scale; + m_pScrollbar->setSliderWidth(windowWidth); + + updateTimelineStep(windowWidth); + repaintScene(); +} + +void EasyGraphicsView::wheelEvent(QWheelEvent* _event) +{ + m_idleTime = 0; + + if (!m_bEmpty) + onWheel(mapToScene(_event->pos()).x(), _event->delta()); + _event->accept(); +} + +void EasyGraphicsView::onGraphicsScrollbarWheel(qreal _mouseX, int _wheelDelta) +{ + m_idleTime = 0; + + for (auto item : m_items) + { + if (item->threadId() == EASY_GLOBALS.selected_thread) + { + scrollTo(item); + break; + } + } + + onWheel(_mouseX, _wheelDelta); +} + +void EasyGraphicsView::scrollTo(const EasyGraphicsItem* _item) +{ + m_bUpdatingRect = true; + auto vbar = verticalScrollBar(); + vbar->setValue(_item->y() + (_item->boundingRect().height() - vbar->pageStep()) * 0.5); + m_bUpdatingRect = false; +} + +void EasyGraphicsView::onWheel(qreal _mouseX, int _wheelDelta) +{ + const decltype(m_scale) scaleCoeff = _wheelDelta > 0 ? ::profiler_gui::SCALING_COEFFICIENT : ::profiler_gui::SCALING_COEFFICIENT_INV; + + // Remember current mouse position + _mouseX = clamp(0., _mouseX, m_sceneWidth); + const auto mousePosition = m_offset + _mouseX / m_scale; + + // have to limit scale because of Qt's QPainter feature: it doesn't draw text + // with very big coordinates (but it draw rectangles with the same coordinates good). + m_scale = clamp(MIN_SCALE, m_scale * scaleCoeff, MAX_SCALE); + + //updateVisibleSceneRect(); // Update scene rect + + // Update slider width for scrollbar + auto vbar = verticalScrollBar(); + const int vbar_width = (vbar != nullptr && vbar->isVisible() ? vbar->width() + 2 : 0); + const auto windowWidth = (m_visibleSceneRect.width() + vbar_width) / m_scale; + m_pScrollbar->setSliderWidth(windowWidth); + + // Calculate new offset to simulate QGraphicsView::AnchorUnderMouse scaling behavior + m_offset = clamp(0., mousePosition - _mouseX / m_scale, m_sceneWidth - windowWidth); + + // Update slider position + m_bUpdatingRect = true; // To be sure that updateVisibleSceneRect will not be called by scrollbar change + m_pScrollbar->setValue(m_offset); + m_bUpdatingRect = false; + + updateVisibleSceneRect(); // Update scene rect + updateTimelineStep(windowWidth); + repaintScene(); // repaint scene +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::mousePressEvent(QMouseEvent* _event) +{ + m_idleTime = 0; + + if (m_bEmpty) + { + _event->accept(); + return; + } + + m_mouseButtons = _event->buttons(); + m_mousePressPos = _event->pos(); + + if (m_mouseButtons & Qt::LeftButton) + { + if (m_chronometerItemAux->isVisible() && (m_chronometerItemAux->hoverLeft() || m_chronometerItemAux->hoverRight())) + { + m_chronometerItemAux->setReverse(m_chronometerItemAux->hoverLeft()); + m_bDoubleClick = true; + } + else if (m_chronometerItem->isVisible() && (m_chronometerItem->hoverLeft() || m_chronometerItem->hoverRight())) + { + m_chronometerItem->setReverse(m_chronometerItem->hoverLeft()); + m_mouseButtons = Qt::RightButton; + return; + } + } + + if (m_mouseButtons & Qt::RightButton) + { + if (m_chronometerItem->isVisible() && (m_chronometerItem->hoverLeft() || m_chronometerItem->hoverRight())) + { + m_chronometerItem->setReverse(m_chronometerItem->hoverLeft()); + } + else + { + const auto mouseX = m_offset + mapToScene(m_mousePressPos).x() / m_scale; + m_chronometerItem->setLeftRight(mouseX, mouseX); + m_chronometerItem->hide(); + m_pScrollbar->hideChrono(); + } + } + + _event->accept(); +} + +void EasyGraphicsView::mouseDoubleClickEvent(QMouseEvent* _event) +{ + m_idleTime = 0; + + if (m_bEmpty) + { + _event->accept(); + return; + } + + m_mouseButtons = _event->buttons(); + m_mousePressPos = _event->pos(); + m_bDoubleClick = true; + + if (m_mouseButtons & Qt::LeftButton) + { + const auto mouseX = m_offset + mapToScene(m_mousePressPos).x() / m_scale; + m_chronometerItemAux->setLeftRight(mouseX, mouseX); + m_chronometerItemAux->hide(); + emit sceneUpdated(); + } + + _event->accept(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::mouseReleaseEvent(QMouseEvent* _event) +{ + m_idleTime = 0; + + if (m_bEmpty) + { + _event->accept(); + return; + } + + bool chronoHidden = false; + bool changedSelection = false, changedSelectedItem = false; + if (m_mouseButtons & Qt::RightButton) + { + if (m_chronometerItem->isVisible() && m_chronometerItem->width() < 1e-6) + { + m_chronometerItem->hide(); + m_pScrollbar->hideChrono(); + } + + if (!m_selectedBlocks.empty()) + { + changedSelection = true; + m_selectedBlocks.clear(); + } + + if (m_chronometerItem->isVisible()) + { + //printf("INTERVAL: {%lf, %lf} ms\n", m_chronometerItem->left(), m_chronometerItem->right()); + + for (auto item : m_items) + { + if (!EASY_GLOBALS.only_current_thread_hierarchy || item->threadId() == EASY_GLOBALS.selected_thread) + item->getBlocks(m_chronometerItem->left(), m_chronometerItem->right(), m_selectedBlocks); + } + + if (!m_selectedBlocks.empty()) + { + changedSelection = true; + } + } + } + + const ::profiler_gui::EasyBlock* selectedBlock = nullptr; + ::profiler::thread_id_t selectedBlockThread = 0; + const auto previouslySelectedBlock = EASY_GLOBALS.selected_block; + if (m_mouseButtons & Qt::LeftButton) + { + bool clicked = false; + + if (m_chronometerItemAux->isVisible() && m_chronometerItemAux->width() < 1e-6) + { + chronoHidden = true; + m_chronometerItemAux->hide(); + } + else if (m_chronometerItem->isVisible() && m_chronometerItem->hoverIndicator()) + { + // Jump to selected zone + clicked = true; + m_flickerSpeedX = m_flickerSpeedY = 0; + m_pScrollbar->setValue(m_chronometerItem->left() + m_chronometerItem->width() * 0.5 - m_pScrollbar->sliderHalfWidth()); + } + + if (!clicked && m_mouseMovePath.manhattanLength() < 5) + { + // Handle Click + + //clicked = true; + auto mouseClickPos = mapToScene(m_mousePressPos); + if (mouseClickPos.x() >= 0) + { + mouseClickPos.setX(m_offset + mouseClickPos.x() / m_scale); + + // Try to select one of item blocks + for (auto item : m_items) + { + ::profiler::block_index_t i = ~0U; + auto block = item->intersect(mouseClickPos, i); + if (block) + { + changedSelectedItem = true; + selectedBlock = block; + selectedBlockThread = item->threadId(); + EASY_GLOBALS.selected_block = i; + EASY_GLOBALS.selected_block_id = easyBlock(i).tree.node->id(); + break; + } + } + + if (!changedSelectedItem && !::profiler_gui::is_max(EASY_GLOBALS.selected_block)) + { + changedSelectedItem = true; + ::profiler_gui::set_max(EASY_GLOBALS.selected_block); + ::profiler_gui::set_max(EASY_GLOBALS.selected_block_id); + } + } + } + } + + m_bDoubleClick = false; + m_mouseButtons = _event->buttons(); + m_mouseMovePath = QPoint(); + _event->accept(); + + if (changedSelection) + { + emit intervalChanged(m_selectedBlocks, m_beginTime, position2time(m_chronometerItem->left()), position2time(m_chronometerItem->right()), m_chronometerItem->reverse()); + } + + if (changedSelectedItem) + { + m_bUpdatingRect = true; + if (selectedBlock != nullptr && previouslySelectedBlock == EASY_GLOBALS.selected_block && !selectedBlock->tree.children.empty()) + { + EASY_GLOBALS.gui_blocks[previouslySelectedBlock].expanded = !EASY_GLOBALS.gui_blocks[previouslySelectedBlock].expanded; + emit EASY_GLOBALS.events.itemsExpandStateChanged(); + } + emit EASY_GLOBALS.events.selectedBlockChanged(EASY_GLOBALS.selected_block); + + if (EASY_GLOBALS.selecting_block_changes_thread && selectedBlock != nullptr && EASY_GLOBALS.selected_thread != selectedBlockThread) + { + EASY_GLOBALS.selected_thread = selectedBlockThread; + + m_pScrollbar->lock(); + emit EASY_GLOBALS.events.selectedThreadChanged(EASY_GLOBALS.selected_thread); + m_pScrollbar->unlock(); + } + m_bUpdatingRect = false; + + if (selectedBlock != nullptr && selectedBlockThread == EASY_GLOBALS.selected_thread) + m_pScrollbar->setHistogramSource(EASY_GLOBALS.selected_thread, EASY_GLOBALS.selected_block_id); + else + { + for (auto item : m_items) + { + if (item->threadId() == EASY_GLOBALS.selected_thread) + { + m_pScrollbar->setHistogramSource(EASY_GLOBALS.selected_thread, item->items(0)); + break; + } + } + } + + repaintScene(); + } + else if (chronoHidden) + { + emit sceneUpdated(); + } +} + +////////////////////////////////////////////////////////////////////////// + +bool EasyGraphicsView::moveChrono(EasyChronometerItem* _chronometerItem, qreal _mouseX) +{ + if (_chronometerItem->reverse()) + { + if (_mouseX > _chronometerItem->right()) + { + _chronometerItem->setReverse(false); + _chronometerItem->setLeftRight(_chronometerItem->right(), _mouseX); + + if (_chronometerItem->hoverLeft()) + { + _chronometerItem->setHoverLeft(false); + _chronometerItem->setHoverRight(true); + } + } + else + { + _chronometerItem->setLeftRight(_mouseX, _chronometerItem->right()); + } + } + else + { + if (_mouseX < _chronometerItem->left()) + { + _chronometerItem->setReverse(true); + _chronometerItem->setLeftRight(_mouseX, _chronometerItem->left()); + + if (_chronometerItem->hoverRight()) + { + _chronometerItem->setHoverLeft(true); + _chronometerItem->setHoverRight(false); + } + } + else + { + _chronometerItem->setLeftRight(_chronometerItem->left(), _mouseX); + } + } + + if (!_chronometerItem->isVisible() && _chronometerItem->width() > 1e-6) + { + _chronometerItem->show(); + return true; + } + + return false; +} + +void EasyGraphicsView::mouseMoveEvent(QMouseEvent* _event) +{ + m_idleTime = 0; + + if (m_bEmpty || (m_mouseButtons == 0 && !m_chronometerItem->isVisible() && !m_chronometerItemAux->isVisible())) + { + _event->accept(); + return; + } + + bool needUpdate = false; + const auto pos = _event->pos(); + const auto delta = pos - m_mousePressPos; + m_mousePressPos = pos; + + if (m_mouseButtons != 0) + { + m_mouseMovePath.setX(m_mouseMovePath.x() + qAbs(delta.x())); + m_mouseMovePath.setY(m_mouseMovePath.y() + qAbs(delta.y())); + } + + auto mouseScenePos = mapToScene(m_mousePressPos); + mouseScenePos.setX(m_offset + mouseScenePos.x() / m_scale); + const auto x = clamp(0., mouseScenePos.x(), m_sceneWidth); + + if (m_mouseButtons & Qt::RightButton) + { + bool showItem = moveChrono(m_chronometerItem, x); + m_pScrollbar->setChronoPos(m_chronometerItem->left(), m_chronometerItem->right()); + + if (showItem) + { + m_pScrollbar->showChrono(); + } + + needUpdate = true; + } + + if (m_mouseButtons & Qt::LeftButton) + { + if (m_bDoubleClick) + { + moveChrono(m_chronometerItemAux, x); + } + else + { + auto vbar = verticalScrollBar(); + + m_bUpdatingRect = true; // Block scrollbars from updating scene rect to make it possible to do it only once + vbar->setValue(vbar->value() - delta.y()); + m_pScrollbar->setValue(m_pScrollbar->value() - delta.x() / m_scale); + m_bUpdatingRect = false; + // Seems like an ugly stub, but QSignalBlocker is also a bad decision + // because if scrollbar does not emit valueChanged signal then viewport does not move + + updateVisibleSceneRect(); // Update scene visible rect only once + + // Update flicker speed + m_flickerSpeedX += delta.x() >> 1; + m_flickerSpeedY += delta.y(); + if (!m_flickerTimer.isActive()) + { + // If flicker timer is not started, then start it + m_flickerTimer.start(FLICKER_INTERVAL); + } + } + + needUpdate = true; + } + + if (m_mouseButtons == 0) + { + if (m_chronometerItem->isVisible()) + { + auto prevValue = m_chronometerItem->hoverIndicator(); + m_chronometerItem->setHoverIndicator(m_chronometerItem->indicatorContains(mouseScenePos)); + needUpdate = needUpdate || (prevValue != m_chronometerItem->hoverIndicator()); + + prevValue = m_chronometerItem->hoverLeft(); + m_chronometerItem->setHoverLeft(m_chronometerItem->hoverLeft(mouseScenePos.x())); + needUpdate = needUpdate || (prevValue != m_chronometerItem->hoverLeft()); + + if (!m_chronometerItem->hoverLeft()) + { + prevValue = m_chronometerItem->hoverRight(); + m_chronometerItem->setHoverRight(m_chronometerItem->hoverRight(mouseScenePos.x())); + needUpdate = needUpdate || (prevValue != m_chronometerItem->hoverRight()); + } + } + + if (m_chronometerItemAux->isVisible()) + { + auto prevValue = m_chronometerItemAux->hoverLeft(); + m_chronometerItemAux->setHoverLeft(m_chronometerItemAux->hoverLeft(mouseScenePos.x())); + needUpdate = needUpdate || (prevValue != m_chronometerItemAux->hoverLeft()); + + if (!m_chronometerItemAux->hoverLeft()) + { + prevValue = m_chronometerItemAux->hoverRight(); + m_chronometerItemAux->setHoverRight(m_chronometerItemAux->hoverRight(mouseScenePos.x())); + needUpdate = needUpdate || (prevValue != m_chronometerItemAux->hoverRight()); + } + } + } + + if (needUpdate) + { + repaintScene(); // repaint scene + } + + _event->accept(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::keyPressEvent(QKeyEvent* _event) +{ + static const int KeyStep = 100; + + const int key = _event->key(); + m_idleTime = 0; + + switch (key) + { + case Qt::Key_Right: + case Qt::Key_6: + { + m_pScrollbar->setValue(m_pScrollbar->value() + KeyStep / m_scale); + break; + } + + case Qt::Key_Left: + case Qt::Key_4: + { + m_pScrollbar->setValue(m_pScrollbar->value() - KeyStep / m_scale); + break; + } + + case Qt::Key_Up: + case Qt::Key_8: + { + auto vbar = verticalScrollBar(); + vbar->setValue(vbar->value() - KeyStep); + break; + } + + case Qt::Key_Down: + case Qt::Key_2: + { + auto vbar = verticalScrollBar(); + vbar->setValue(vbar->value() + KeyStep); + break; + } + + case Qt::Key_Plus: + case Qt::Key_Equal: + { + onWheel(mapToScene(mapFromGlobal(QCursor::pos())).x(), KeyStep); + break; + } + + case Qt::Key_Minus: + { + onWheel(mapToScene(mapFromGlobal(QCursor::pos())).x(), -KeyStep); + break; + } + } + + //m_keys.insert(key); + _event->accept(); +} + +void EasyGraphicsView::keyReleaseEvent(QKeyEvent* _event) +{ + //const int key = _event->key(); + m_idleTime = 0; + + //m_keys.erase(key); + _event->accept(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::resizeEvent(QResizeEvent* _event) +{ + Parent::resizeEvent(_event); + + const QRectF previousRect = m_visibleSceneRect; + const int vbar_width = updateVisibleSceneRect(); // Update scene visible rect only once + + // Update slider width for scrollbar + const auto windowWidth = (m_visibleSceneRect.width() + vbar_width) / m_scale; + m_pScrollbar->setSliderWidth(windowWidth); + + // Calculate new offset to save old screen center + const auto deltaWidth = m_visibleSceneRect.width() - previousRect.width(); + m_offset = clamp(0., m_offset - deltaWidth * 0.5 / m_scale, m_sceneWidth - windowWidth); + + // Update slider position + m_bUpdatingRect = true; // To be sure that updateVisibleSceneRect will not be called by scrollbar change + m_pScrollbar->setValue(m_offset); + m_bUpdatingRect = false; + + repaintScene(); // repaint scene +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::initMode() +{ + // TODO: find mode with least number of bugs :) + // There are always some display bugs... + + setCacheMode(QGraphicsView::CacheNone); + setTransformationAnchor(QGraphicsView::AnchorUnderMouse); + setViewportUpdateMode(QGraphicsView::FullViewportUpdate); + setOptimizationFlag(QGraphicsView::DontSavePainterState, true); + + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &This::onScrollbarValueChange); + connect(&m_flickerTimer, &QTimer::timeout, this, &This::onFlickerTimeout); + connect(&m_idleTimer, &QTimer::timeout, this, &This::onIdleTimeout); + + auto globalSignals = &EASY_GLOBALS.events; + connect(globalSignals, &::profiler_gui::EasyGlobalSignals::hierarchyFlagChanged, this, &This::onHierarchyFlagChange); + connect(globalSignals, &::profiler_gui::EasyGlobalSignals::selectedThreadChanged, this, &This::onSelectedThreadChange); + connect(globalSignals, &::profiler_gui::EasyGlobalSignals::selectedBlockChanged, this, &This::onSelectedBlockChange); + connect(globalSignals, &::profiler_gui::EasyGlobalSignals::itemsExpandStateChanged, this, &This::onRefreshRequired); + connect(globalSignals, &::profiler_gui::EasyGlobalSignals::refreshRequired, this, &This::onRefreshRequired); + + connect(globalSignals, &::profiler_gui::EasyGlobalSignals::selectedBlockIdChanged, [this](::profiler::block_id_t) + { + if (::profiler_gui::is_max(EASY_GLOBALS.selected_block_id)) + { + if (EASY_GLOBALS.selected_thread != 0) + { + for (auto item : m_items) + { + if (item->threadId() == EASY_GLOBALS.selected_thread) + { + m_pScrollbar->setHistogramSource(EASY_GLOBALS.selected_thread, item->items(0)); + break; + } + } + } + else + { + m_pScrollbar->setHistogramSource(0, nullptr); + } + } + else + m_pScrollbar->setHistogramSource(EASY_GLOBALS.selected_thread, EASY_GLOBALS.selected_block_id); + onRefreshRequired(); + }); + + connect(globalSignals, &::profiler_gui::EasyGlobalSignals::threadNameDecorationChanged, this, &This::onThreadViewChanged); + connect(globalSignals, &::profiler_gui::EasyGlobalSignals::hexThreadIdChanged, this, &This::onThreadViewChanged); + + connect(globalSignals, &::profiler_gui::EasyGlobalSignals::blocksTreeModeChanged, [this]() + { + if (!m_selectedBlocks.empty()) + emit intervalChanged(m_selectedBlocks, m_beginTime, position2time(m_chronometerItem->left()), position2time(m_chronometerItem->right()), m_chronometerItem->reverse()); + }); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::onThreadViewChanged() +{ + if (m_bEmpty) + return; + + for (auto item : m_items) + item->validateName(); + + emit treeChanged(); + + updateVisibleSceneRect(); + onHierarchyFlagChange(EASY_GLOBALS.only_current_thread_hierarchy); + + repaintScene(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::onScrollbarValueChange(int) +{ + if (!m_bUpdatingRect && !m_bEmpty) + updateVisibleSceneRect(); +} + +void EasyGraphicsView::onGraphicsScrollbarValueChange(qreal _value) +{ + if (!m_bEmpty) + { + m_offset = _value; + if (!m_bUpdatingRect) + { + updateVisibleSceneRect(); + repaintScene(); + } + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::onFlickerTimeout() +{ + ++m_flickerCounterX; + ++m_flickerCounterY; + + if (m_mouseButtons & Qt::LeftButton) + { + // Fast slow-down and stop if mouse button is pressed, no flicking. + m_flickerSpeedX >>= 1; + m_flickerSpeedY >>= 1; + if (m_flickerSpeedX == -1) m_flickerSpeedX = 0; + if (m_flickerSpeedY == -1) m_flickerSpeedY = 0; + } + else + { + // Flick when mouse button is not pressed + + auto vbar = verticalScrollBar(); + + m_bUpdatingRect = true; // Block scrollbars from updating scene rect to make it possible to do it only once + m_pScrollbar->setValue(m_pScrollbar->value() - m_flickerSpeedX / m_scale); + vbar->setValue(vbar->value() - m_flickerSpeedY); + m_bUpdatingRect = false; + // Seems like an ugly stub, but QSignalBlocker is also a bad decision + // because if scrollbar does not emit valueChanged signal then viewport does not move + + updateVisibleSceneRect(); // Update scene visible rect only once + repaintScene(); // repaint scene + + const int dx = static_cast(sign(m_flickerSpeedX) * m_flickerCounterX / FLICKER_FACTOR); + const int dy = static_cast(sign(m_flickerSpeedY) * m_flickerCounterY / FLICKER_FACTOR); + + if (abs(dx) > 0) + { + m_flickerSpeedX -= absmin(dx, m_flickerSpeedX); + m_flickerCounterX = 0; + } + + if (abs(dy) > 0) + { + m_flickerSpeedY -= absmin(dy, m_flickerSpeedY); + m_flickerCounterY = 0; + } + } + + if (m_flickerSpeedX == 0 && m_flickerSpeedY == 0) + { + // Flicker stopped, no timer needed. + m_flickerTimer.stop(); + m_flickerSpeedX = 0; + m_flickerSpeedY = 0; + m_flickerCounterX = 0; + m_flickerCounterY = 0; + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::onIdleTimeout() +{ + m_idleTime += IDLE_TIMER_INTERVAL; + + if (m_idleTime < IDLE_TIME) + { + removePopup(true); + return; + } + + if (m_popupWidget != nullptr) + return; + + auto scenePos = mapToScene(mapFromGlobal(QCursor::pos())); + + if (scenePos.x() < m_visibleSceneRect.left() || scenePos.x() > m_visibleSceneRect.right()) + return; + + if (scenePos.y() < m_visibleSceneRect.top() || scenePos.y() > m_visibleSceneRect.bottom()) + return; + + decltype(scenePos) pos(m_offset + scenePos.x() / m_scale, scenePos.y()); + + // Try to select one of context switches or items + for (auto item : m_items) + { + ::profiler::block_index_t i = ~0U; + auto block = item->intersect(pos, i); + if (block) + { + const auto& itemBlock = block->tree; + const auto& itemDesc = easyDescriptor(itemBlock.node->id()); + auto name = *itemBlock.node->name() != 0 ? itemBlock.node->name() : itemDesc.name(); + + auto widget = new QWidget(nullptr, Qt::FramelessWindowHint); + if (widget == nullptr) + return; + + widget->setAttribute(Qt::WA_ShowWithoutActivating, true); + widget->setFocusPolicy(Qt::NoFocus); + + auto lay = new QGridLayout(widget); + if (lay == nullptr) + return; + + int row = 0; + if (itemDesc.type() == ::profiler::BLOCK_TYPE_BLOCK) + { + //lay->addWidget(new QLabel("Name:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new EasyBoldLabel(::profiler_gui::toUnicode(name), widget), row, 0, 1, 5, Qt::AlignHCenter); + ++row; + + const auto duration = itemBlock.node->duration(); + lay->addWidget(new QLabel("Duration:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, duration, 3), widget), row, 1, 1, 3, Qt::AlignLeft); + ++row; + + ::profiler::timestamp_t children_duration = 0; + for (auto child : itemBlock.children) + children_duration += easyBlock(child).tree.node->duration(); + + const auto self_duration = duration - children_duration; + const auto self_percent = duration == 0 ? 100. : ::profiler_gui::percentReal(self_duration, duration); + lay->addWidget(new QLabel("Self:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(QString("%1 (%2%)").arg(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, self_duration, 3)).arg(QString::number(self_percent, 'g', 3)), widget), row, 1, 1, 3, Qt::AlignLeft); + ++row; + } + else + { + lay->addWidget(new EasyBoldLabel("User defined event", widget), row, 0, 1, 2, Qt::AlignHCenter); + ++row; + + lay->addWidget(new QLabel("Name:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(::profiler_gui::toUnicode(name), widget), row, 1, Qt::AlignLeft); + ++row; + } + + if (itemBlock.per_thread_stats) + { + if (itemDesc.type() == ::profiler::BLOCK_TYPE_BLOCK) + { + const auto duration = itemBlock.node->duration(); + + lay->addWidget(new QLabel("Average:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, itemBlock.per_thread_stats->average_duration(), 3), widget), row, 1, 1, 3, Qt::AlignLeft); + ++row; + + // Calculate idle/active time + { + auto threadRoot = item->root(); + + ::profiler::block_index_t ind = 0; + auto it = ::std::lower_bound(threadRoot->sync.begin(), threadRoot->sync.end(), itemBlock.node->begin(), [](::profiler::block_index_t _cs_index, ::profiler::timestamp_t _val) + { + return EASY_GLOBALS.gui_blocks[_cs_index].tree.node->begin() < _val; + }); + + if (it != threadRoot->sync.end()) + { + ind = it - threadRoot->sync.begin(); + if (ind > 0) + --ind; + } + else + { + ind = static_cast<::profiler::block_index_t>(threadRoot->sync.size()); + } + + ::profiler::timestamp_t idleTime = 0; + for (::profiler::block_index_t ncs = static_cast<::profiler::block_index_t>(threadRoot->sync.size()); ind < ncs; ++ind) + { + auto cs_index = threadRoot->sync[ind]; + const auto cs = EASY_GLOBALS.gui_blocks[cs_index].tree.node; + + if (cs->begin() > itemBlock.node->end()) + break; + + if (itemBlock.node->begin() <= cs->begin() && cs->end() <= itemBlock.node->end()) + idleTime += cs->duration(); + } + + const auto active_time = duration - idleTime; + const auto active_percent = duration == 0 ? 100. : ::profiler_gui::percentReal(active_time, duration); + lay->addWidget(new QLabel("Active time:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(QString("%1 (%2%)").arg(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, active_time, 3)).arg(QString::number(active_percent, 'g', 3)), widget), row, 1, 1, 3, Qt::AlignLeft); + ++row; + } + + lay->addWidget(new EasyBoldLabel("-------- Statistics --------", widget), row, 0, 1, 5, Qt::AlignHCenter); + lay->addWidget(new QLabel("per ", widget), row + 1, 0, Qt::AlignRight); + lay->addWidget(new QLabel("This %:", widget), row + 2, 0, Qt::AlignRight); + lay->addWidget(new QLabel("Sum %:", widget), row + 3, 0, Qt::AlignRight); + lay->addWidget(new QLabel("Sum self %:", widget), row + 4, 0, Qt::AlignRight); + lay->addWidget(new QLabel("N Calls:", widget), row + 5, 0, Qt::AlignRight); + + lay->addWidget(new QLabel("Thread", widget), row + 1, 1, Qt::AlignHCenter); + + auto percent = ::profiler_gui::percentReal(duration, item->root()->profiled_time); + lay->addWidget(new QLabel(0.005 < percent && percent < 0.5001 ? QString::number(percent, 'f', 2) : QString::number(static_cast(0.5 + percent)), widget), row + 2, 1, Qt::AlignHCenter); + + lay->addWidget(new QLabel(QString::number(::profiler_gui::percent(itemBlock.per_thread_stats->total_duration, item->root()->profiled_time)), widget), row + 3, 1, Qt::AlignHCenter); + + lay->addWidget(new QLabel(QString::number(::profiler_gui::percent(itemBlock.per_thread_stats->total_duration - itemBlock.per_thread_stats->total_children_duration, item->root()->profiled_time)), widget), row + 4, 1, Qt::AlignHCenter); + + lay->addWidget(new QLabel(QString::number(itemBlock.per_thread_stats->calls_number), widget), row + 5, 1, Qt::AlignHCenter); + + int col = 1; + + if (itemBlock.per_frame_stats->parent_block != i && !::profiler_gui::is_max(itemBlock.per_frame_stats->parent_block)) + { + ++col; + auto frame_duration = blocksTree(itemBlock.per_frame_stats->parent_block).node->duration(); + + lay->addWidget(new QLabel("Frame", widget), row + 1, col, Qt::AlignHCenter); + + percent = ::profiler_gui::percentReal(duration, frame_duration); + lay->addWidget(new QLabel(0.005 < percent && percent < 0.5001 ? QString::number(percent, 'f', 2) : QString::number(static_cast(0.5 + percent)), widget), row + 2, col, Qt::AlignHCenter); + + percent = ::profiler_gui::percentReal(itemBlock.per_frame_stats->total_duration, frame_duration); + lay->addWidget(new QLabel(0.005 < percent && percent < 0.5001 ? QString::number(percent, 'f', 2) : QString::number(static_cast(0.5 + percent)), widget), row + 3, col, Qt::AlignHCenter); + + percent = ::profiler_gui::percentReal(itemBlock.per_frame_stats->total_duration - itemBlock.per_frame_stats->total_children_duration, frame_duration); + lay->addWidget(new QLabel(0.005 < percent && percent < 0.5001 ? QString::number(percent, 'f', 2) : QString::number(static_cast(0.5 + percent)), widget), row + 4, col, Qt::AlignHCenter); + + lay->addWidget(new QLabel(QString::number(itemBlock.per_frame_stats->calls_number), widget), row + 5, col, Qt::AlignHCenter); + } + + if (!::profiler_gui::is_max(itemBlock.per_parent_stats->parent_block))// != item->threadId()) + { + ++col; + auto parent_duration = blocksTree(itemBlock.per_parent_stats->parent_block).node->duration(); + + lay->addWidget(new QLabel("Parent", widget), row + 1, col, Qt::AlignHCenter); + + percent = ::profiler_gui::percentReal(duration, parent_duration); + lay->addWidget(new QLabel(0.005 < percent && percent < 0.5001 ? QString::number(percent, 'f', 2) : QString::number(static_cast(0.5 + percent)), widget), row + 2, col, Qt::AlignHCenter); + + percent = ::profiler_gui::percentReal(itemBlock.per_parent_stats->total_duration, parent_duration); + lay->addWidget(new QLabel(0.005 < percent && percent < 0.5001 ? QString::number(percent, 'f', 2) : QString::number(static_cast(0.5 + percent)), widget), row + 3, col, Qt::AlignHCenter); + + percent = ::profiler_gui::percentReal(itemBlock.per_parent_stats->total_duration - itemBlock.per_parent_stats->total_children_duration, parent_duration); + lay->addWidget(new QLabel(0.005 < percent && percent < 0.5001 ? QString::number(percent, 'f', 2) : QString::number(static_cast(0.5 + percent)), widget), row + 4, col, Qt::AlignHCenter); + + lay->addWidget(new QLabel(QString::number(itemBlock.per_parent_stats->calls_number), widget), row + 5, col, Qt::AlignHCenter); + + ++col; + } + } + else + { + lay->addWidget(new QLabel("N calls/Thread:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(QString::number(itemBlock.per_thread_stats->calls_number), widget), row, 1, Qt::AlignLeft); + } + } + + m_popupWidget = new QGraphicsProxyWidget(); + m_popupWidget->setWidget(widget); + + break; + } + + auto cse = item->intersectEvent(pos); + if (cse) + { + const auto& itemBlock = cse->tree; + + auto widget = new QWidget(nullptr, Qt::FramelessWindowHint); + if (widget == nullptr) + return; + + widget->setAttribute(Qt::WA_ShowWithoutActivating, true); + widget->setFocusPolicy(Qt::NoFocus); + + auto lay = new QGridLayout(widget); + if (lay == nullptr) + return; + + int row = 0; + lay->addWidget(new EasyBoldLabel("Context switch event", widget), row, 0, 1, 3, Qt::AlignHCenter); + ++row; + + lay->addWidget(new QLabel("Thread:", widget), row, 0, Qt::AlignRight); + + const char* process_name = ""; + ::profiler::thread_id_t tid = 0; + if (EASY_GLOBALS.version < ::profiler_gui::V130) + { + tid = cse->tree.node->id(); + process_name = cse->tree.node->name(); + } + else + { + tid = cse->tree.cs->tid(); + process_name = cse->tree.cs->name(); + } + + auto it = EASY_GLOBALS.profiler_blocks.find(tid); + + if (it != EASY_GLOBALS.profiler_blocks.end()) + { + if (EASY_GLOBALS.hex_thread_id) + lay->addWidget(new QLabel(QString("0x%1 %2").arg(tid, 0, 16).arg(it->second.name()), widget), row, 1, 1, 2, Qt::AlignLeft); + else + lay->addWidget(new QLabel(QString("%1 %2").arg(tid).arg(it->second.name()), widget), row, 1, 1, 2, Qt::AlignLeft); + } + else if (EASY_GLOBALS.hex_thread_id) + lay->addWidget(new QLabel(QString("0x%1").arg(tid, 0, 16), widget), row, 1, 1, 2, Qt::AlignLeft); + else + lay->addWidget(new QLabel(QString::number(tid), widget), row, 1, 1, 2, Qt::AlignLeft); + ++row; + + lay->addWidget(new QLabel("Process:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(process_name, widget), row, 1, 1, 2, Qt::AlignLeft); + ++row; + + const auto duration = itemBlock.node->duration(); + lay->addWidget(new QLabel("Duration:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, duration, 3), widget), row, 1, 1, 2, Qt::AlignLeft); + ++row; + + if (itemBlock.per_thread_stats) + { + lay->addWidget(new QLabel("Sum:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, itemBlock.per_thread_stats->total_duration, 3), widget), row, 1, 1, 2, Qt::AlignLeft); + ++row; + + lay->addWidget(new EasyBoldLabel("-------- Statistics --------", widget), row, 0, 1, 3, Qt::AlignHCenter); + lay->addWidget(new QLabel("per ", widget), row + 1, 0, Qt::AlignRight); + lay->addWidget(new QLabel("This %:", widget), row + 2, 0, Qt::AlignRight); + lay->addWidget(new QLabel("Sum %:", widget), row + 3, 0, Qt::AlignRight); + lay->addWidget(new QLabel("N Calls:", widget), row + 4, 0, Qt::AlignRight); + + lay->addWidget(new QLabel("Thread", widget), row + 1, 1, Qt::AlignHCenter); + + auto percent = ::profiler_gui::percentReal(duration, item->root()->profiled_time); + lay->addWidget(new QLabel(0.005 < percent && percent < 0.5001 ? QString::number(percent, 'f', 2) : QString::number(static_cast(0.5 + percent)), widget), row + 2, 1, Qt::AlignHCenter); + + lay->addWidget(new QLabel(QString::number(::profiler_gui::percent(itemBlock.per_thread_stats->total_duration, item->root()->profiled_time)), widget), row + 3, 1, Qt::AlignHCenter); + + lay->addWidget(new QLabel(QString::number(itemBlock.per_thread_stats->calls_number), widget), row + 4, 1, Qt::AlignHCenter); + + if (itemBlock.per_frame_stats && !::profiler_gui::is_max(itemBlock.per_frame_stats->parent_block)) + { + int col = 2; + auto frame_duration = blocksTree(itemBlock.per_frame_stats->parent_block).node->duration(); + + lay->addWidget(new QLabel("Frame", widget), row + 1, col, Qt::AlignHCenter); + + percent = ::profiler_gui::percentReal(duration, frame_duration); + lay->addWidget(new QLabel(0.005 < percent && percent < 0.5001 ? QString::number(percent, 'f', 2) : QString::number(static_cast(0.5 + percent)), widget), row + 2, col, Qt::AlignHCenter); + + percent = ::profiler_gui::percentReal(itemBlock.per_frame_stats->total_duration, frame_duration); + lay->addWidget(new QLabel(0.005 < percent && percent < 0.5001 ? QString::number(percent, 'f', 2) : QString::number(static_cast(0.5 + percent)), widget), row + 3, col, Qt::AlignHCenter); + + lay->addWidget(new QLabel(QString::number(itemBlock.per_frame_stats->calls_number), widget), row + 4, col, Qt::AlignHCenter); + } + } + + m_popupWidget = new QGraphicsProxyWidget(); + m_popupWidget->setWidget(widget); + + break; + } + } + + if (m_popupWidget != nullptr) + { + auto effect = new QGraphicsDropShadowEffect(); + effect->setBlurRadius(5); + effect->setOffset(3, 3); + m_popupWidget->setGraphicsEffect(effect); + + scene()->addItem(m_popupWidget); + + auto br = m_popupWidget->boundingRect(); + if (scenePos.y() + br.height() > m_visibleSceneRect.bottom()) + scenePos.setY(::std::max(scenePos.y() - br.height(), m_visibleSceneRect.top())); + + if (scenePos.x() + br.width() > m_visibleSceneRect.right()) + scenePos.setX(::std::max(scenePos.x() - br.width(), m_visibleSceneRect.left())); + + m_popupWidget->setPos(scenePos); + m_popupWidget->setOpacity(0.95); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::onHierarchyFlagChange(bool) +{ + bool changedSelection = false; + + if (!m_selectedBlocks.empty()) + { + changedSelection = true; + m_selectedBlocks.clear(); + } + + if (m_chronometerItem->isVisible()) + { + for (auto item : m_items) + { + if (!EASY_GLOBALS.only_current_thread_hierarchy || item->threadId() == EASY_GLOBALS.selected_thread) + item->getBlocks(m_chronometerItem->left(), m_chronometerItem->right(), m_selectedBlocks); + } + + if (!m_selectedBlocks.empty()) + { + changedSelection = true; + } + } + + if (changedSelection) + { + emit intervalChanged(m_selectedBlocks, m_beginTime, position2time(m_chronometerItem->left()), position2time(m_chronometerItem->right()), m_chronometerItem->reverse()); + } +} + +void EasyGraphicsView::onSelectedThreadChange(::profiler::thread_id_t _id) +{ + if (m_pScrollbar == nullptr || m_pScrollbar->hystThread() == _id) + { + return; + } + + if (_id == 0) + { + m_pScrollbar->setHistogramSource(0, nullptr); + return; + } + + for (auto item : m_items) + { + if (item->threadId() == _id) + { + m_pScrollbar->setHistogramSource(_id, item->items(0)); + + bool changedSelection = false; + if (EASY_GLOBALS.only_current_thread_hierarchy) + { + if (!m_selectedBlocks.empty()) + { + changedSelection = true; + m_selectedBlocks.clear(); + } + + if (m_chronometerItem->isVisible()) + { + item->getBlocks(m_chronometerItem->left(), m_chronometerItem->right(), m_selectedBlocks); + if (!m_selectedBlocks.empty()) + changedSelection = true; + } + } + + if (changedSelection) + { + emit intervalChanged(m_selectedBlocks, m_beginTime, position2time(m_chronometerItem->left()), position2time(m_chronometerItem->right()), m_chronometerItem->reverse()); + } + + repaintScene(); + return; + } + } + + m_pScrollbar->setHistogramSource(0, nullptr); + repaintScene(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::onSelectedBlockChange(unsigned int _block_index) +{ + if (!m_bUpdatingRect) + { + if (_block_index < EASY_GLOBALS.gui_blocks.size()) + { + // Scroll to item + + const auto& guiblock = EASY_GLOBALS.gui_blocks[_block_index]; + const auto thread_item = m_items[guiblock.graphics_item]; + const auto& item = thread_item->items(guiblock.graphics_item_level)[guiblock.graphics_item_index]; + + m_flickerSpeedX = m_flickerSpeedY = 0; + + m_bUpdatingRect = true; + verticalScrollBar()->setValue(static_cast(thread_item->levelY(guiblock.graphics_item_level) - m_visibleSceneRect.height() * 0.5)); + m_pScrollbar->setValue(item.left() + item.width() * 0.5 - m_pScrollbar->sliderHalfWidth()); + + if (EASY_GLOBALS.selecting_block_changes_thread && EASY_GLOBALS.selected_thread != thread_item->threadId()) + { + EASY_GLOBALS.selected_thread = thread_item->threadId(); + + m_pScrollbar->lock(); + emit EASY_GLOBALS.events.selectedThreadChanged(EASY_GLOBALS.selected_thread); + m_pScrollbar->unlock(); + } + + m_pScrollbar->setHistogramSource(EASY_GLOBALS.selected_thread, guiblock.tree.node->id()); + + m_bUpdatingRect = false; + } + else if (EASY_GLOBALS.selected_thread != 0) + { + for (auto item : m_items) + { + if (item->threadId() == EASY_GLOBALS.selected_thread) + { + m_pScrollbar->setHistogramSource(EASY_GLOBALS.selected_thread, item->items(0)); + break; + } + } + } + else + { + m_pScrollbar->setHistogramSource(0, nullptr); + } + + updateVisibleSceneRect(); + repaintScene(); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsView::onRefreshRequired() +{ + if (!m_bUpdatingRect) + { + repaintScene(); + } +} + +////////////////////////////////////////////////////////////////////////// + +EasyGraphicsViewWidget::EasyGraphicsViewWidget(QWidget* _parent) + : QWidget(_parent) + , m_scrollbar(new EasyGraphicsScrollbar(this)) + , m_view(new EasyGraphicsView(this)) + , m_threadNamesWidget(new EasyThreadNamesWidget(m_view, m_scrollbar->height(), this)) +{ + initWidget(); +} + +void EasyGraphicsViewWidget::initWidget() +{ + auto lay = new QGridLayout(this); + lay->setContentsMargins(1, 0, 1, 0); + lay->addWidget(m_threadNamesWidget, 0, 0, 2, 1); + lay->setSpacing(1); + lay->addWidget(m_view, 0, 1); + lay->setSpacing(1); + lay->addWidget(m_scrollbar, 1, 1); + setLayout(lay); + + m_view->setScrollbar(m_scrollbar); +} + +EasyGraphicsViewWidget::~EasyGraphicsViewWidget() +{ + +} + +EasyGraphicsView* EasyGraphicsViewWidget::view() +{ + return m_view; +} + +void EasyGraphicsViewWidget::clear() +{ + m_scrollbar->clear(); + m_threadNamesWidget->clear(); + m_view->clear(); +} + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +void EasyThreadNameItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem*, QWidget*) +{ + auto const parentView = static_cast(scene()->parent()); + const auto view = parentView->view(); + const auto& items = view->getItems(); + if (items.empty()) + return; + + const auto visibleSceneRect = view->visibleSceneRect(); + const auto h = visibleSceneRect.height() + TIMELINE_ROW_SIZE - 2; + const auto w = parentView->width();//parentView->sceneRect().width(); + + static const uint16_t OVERLAP = ::profiler_gui::THREADS_ROW_SPACING >> 1; + static const QBrush brushes[2] = {QColor::fromRgb(BACKGROUND_1), QColor::fromRgb(BACKGROUND_2)}; + int i = -1; + + QRectF rect; + + _painter->resetTransform(); + + // Draw thread names + auto default_font = _painter->font(); + _painter->setFont(BG_FONT); + for (auto item : items) + { + ++i; + + auto br = item->boundingRect(); + auto top = item->y() + br.top() - visibleSceneRect.top() - OVERLAP; + auto hgt = br.height() + ::profiler_gui::THREADS_ROW_SPACING; + auto bottom = top + hgt; + + if (top > h || bottom < 0) + continue; + + if (item->threadId() == EASY_GLOBALS.selected_thread) + _painter->setBrush(QBrush(QColor::fromRgb(::profiler_gui::SELECTED_THREAD_BACKGROUND))); + else + _painter->setBrush(brushes[i & 1]); + + if (top < 0) + { + hgt += top; + top = 0; + } + + const auto dh = top + hgt - h; + if (dh > 0) + hgt -= dh; + + rect.setRect(0, top, w, hgt); + + _painter->setPen(Qt::NoPen); + _painter->drawRect(rect); + + rect.translate(-5, 0); + _painter->setPen(QColor::fromRgb(::profiler::colors::Dark)); + _painter->drawText(rect, Qt::AlignRight | Qt::AlignVCenter, item->threadName()); + } + + const auto rect_bottom = rect.bottom(); + if (rect_bottom < h) + { + ++i; + rect.translate(5, rect.height()); + rect.setHeight(h - rect_bottom); + _painter->setBrush(brushes[i & 1]); + _painter->setPen(Qt::NoPen); + _painter->drawRect(rect); + } + + // Draw separator between thread names area and information area + _painter->setPen(Qt::darkGray); + _painter->drawLine(QLineF(0, h, w, h)); + _painter->drawLine(QLineF(0, h + 2, w, h + 2)); + + // Draw information + _painter->setFont(CHRONOMETER_FONT); + QFontMetricsF fm(CHRONOMETER_FONT, parentView); + const qreal th = fm.height(); // Calculate displayed text height + const qreal time1 = view->chronoTime(); + const qreal time2 = view->chronoTimeAux(); + + auto y = h + 2; + + auto drawTimeText = [&rect, &w, &y, &fm, &_painter](qreal time, qreal th, QRgb color) + { + if (time > 0) + { + const QString text = ::profiler_gui::autoTimeStringReal(time); // Displayed text + rect.setRect(0, y, w, th); + + _painter->setPen(color); + _painter->drawText(rect, Qt::AlignCenter, text); + + y += th; + } + }; + + drawTimeText(time1, th, ::profiler_gui::CHRONOMETER_COLOR.rgb() & 0x00ffffff); + drawTimeText(time2, th, ::profiler_gui::CHRONOMETER_COLOR2.rgb() & 0x00ffffff); +} + +////////////////////////////////////////////////////////////////////////// + +EasyThreadNamesWidget::EasyThreadNamesWidget(EasyGraphicsView* _view, int _additionalHeight, QWidget* _parent) + : Parent(_parent) + , m_idleTime(0) + , m_view(_view) + , m_popupWidget(nullptr) + , m_maxLength(100) + , m_additionalHeight(_additionalHeight + 1) +{ + setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored); + + setScene(new QGraphicsScene(this)); + + setCacheMode(QGraphicsView::CacheNone); + setViewportUpdateMode(QGraphicsView::FullViewportUpdate); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setFixedWidth(m_maxLength); + + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::selectedThreadChanged, [this](::profiler::thread_id_t){ repaintScene(); }); + connect(m_view, &EasyGraphicsView::treeChanged, this, &This::onTreeChange); + connect(m_view, &EasyGraphicsView::sceneUpdated, this, &This::repaintScene); + connect(m_view->verticalScrollBar(), &QScrollBar::valueChanged, verticalScrollBar(), &QScrollBar::setValue); + connect(m_view->verticalScrollBar(), &QScrollBar::rangeChanged, this, &This::setVerticalScrollbarRange); + connect(&m_idleTimer, &QTimer::timeout, this, &This::onIdleTimeout); +} + +EasyThreadNamesWidget::~EasyThreadNamesWidget() +{ + +} + +void EasyThreadNamesWidget::removePopup(bool _removeFromScene) +{ + if (m_popupWidget != nullptr) + { + auto widget = m_popupWidget->widget(); + widget->setParent(nullptr); + m_popupWidget->setWidget(nullptr); + delete widget; + + if (_removeFromScene) + scene()->removeItem(m_popupWidget); + + m_popupWidget = nullptr; + } +} + +void EasyThreadNamesWidget::clear() +{ + const QSignalBlocker b(this); + removePopup(); + scene()->clear(); + + m_maxLength = 100; + setFixedWidth(m_maxLength); + + m_idleTimer.stop(); + m_idleTime = 0; +} + +void EasyThreadNamesWidget::setVerticalScrollbarRange(int _minValue, int _maxValue) +{ + verticalScrollBar()->setRange(_minValue, _maxValue + m_additionalHeight); +} + +void EasyThreadNamesWidget::onTreeChange() +{ + const QSignalBlocker b(this); + removePopup(); + scene()->clear(); + + m_idleTimer.stop(); + m_idleTime = 0; + + QFontMetricsF fm(BG_FONT, this); + qreal maxLength = 100; + const auto& graphicsItems = m_view->getItems(); + for (auto graphicsItem : graphicsItems) + maxLength = ::std::max(maxLength, (10 + fm.width(graphicsItem->threadName())) * ::profiler_gui::FONT_METRICS_FACTOR); + + auto vbar = verticalScrollBar(); + auto viewBar = m_view->verticalScrollBar(); + + setVerticalScrollbarRange(viewBar->minimum(), viewBar->maximum()); + vbar->setSingleStep(viewBar->singleStep()); + vbar->setPageStep(viewBar->pageStep()); + + auto r = m_view->sceneRect(); + setSceneRect(0, r.top(), maxLength, r.height() + m_additionalHeight); + + auto item = new EasyThreadNameItem(); + item->setPos(0, 0); + item->setBoundingRect(sceneRect()); + scene()->addItem(item); + + m_maxLength = static_cast(maxLength); + setFixedWidth(m_maxLength); + + m_idleTimer.start(IDLE_TIMER_INTERVAL); +} + +void EasyThreadNamesWidget::onIdleTimeout() +{ + static const uint16_t OVERLAP = ::profiler_gui::THREADS_ROW_SPACING >> 1; + + m_idleTime += IDLE_TIMER_INTERVAL; + + if (m_idleTime < IDLE_TIME) + { + removePopup(true); + return; + } + + if (m_popupWidget != nullptr) + return; + + auto visibleSceneRect = mapToScene(rect()).boundingRect(); + auto scenePos = mapToScene(mapFromGlobal(QCursor::pos())); + + if (scenePos.x() < visibleSceneRect.left() || scenePos.x() > visibleSceneRect.right()) + { + if (m_idleTime > 3000) + setFixedWidth(m_maxLength); + return; + } + + if (scenePos.y() < visibleSceneRect.top() || scenePos.y() > visibleSceneRect.bottom()) + { + if (m_idleTime > 3000) + setFixedWidth(m_maxLength); + return; + } + + auto const parentView = static_cast(scene()->parent()); + const auto view = parentView->view(); + + if (scenePos.y() > view->visibleSceneRect().bottom()) + { + if (m_idleTime > 3000) + setFixedWidth(m_maxLength); + return; + } + + const qreal y = scenePos.y() - visibleSceneRect.top(); + + const auto& items = view->getItems(); + if (items.empty()) + { + if (m_idleTime > 3000) + setFixedWidth(m_maxLength); + return; + } + + EasyGraphicsItem* intersectingItem = nullptr; + for (auto item : items) + { + auto br = item->boundingRect(); + auto top = item->y() + br.top() - visibleSceneRect.top() - OVERLAP; + auto hgt = br.height() + ::profiler_gui::THREADS_ROW_SPACING; + auto bottom = top + hgt; + + if (bottom < y || y < top) + continue; + + intersectingItem = item; + + break; + } + + if (intersectingItem != nullptr) + { + auto widget = new QWidget(nullptr, Qt::FramelessWindowHint); + if (widget == nullptr) + return; + + widget->setAttribute(Qt::WA_ShowWithoutActivating, true); + widget->setFocusPolicy(Qt::NoFocus); + + auto lay = new QGridLayout(widget); + if (lay == nullptr) + return; + + int row = 0; + + lay->addWidget(new EasyBoldLabel(intersectingItem->threadName(), widget), row, 0, 1, 2, Qt::AlignHCenter); + ++row; + + ::profiler::timestamp_t duration = 0; + const auto& root = *intersectingItem->root(); + if (!root.children.empty()) + duration = easyBlock(root.children.back()).tree.node->end() - easyBlock(root.children.front()).tree.node->begin(); + + lay->addWidget(new QLabel("Duration:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, duration, 3), widget), row, 1, Qt::AlignLeft); + ++row; + + lay->addWidget(new QLabel("Profiled:", widget), row, 0, Qt::AlignRight); + if (duration) + { + lay->addWidget(new QLabel(QString("%1 (%2%)").arg(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, root.profiled_time, 3)) + .arg(QString::number(100. * (double)root.profiled_time / (double)duration, 'f', 2)), widget), row, 1, Qt::AlignLeft); + } + else + { + lay->addWidget(new QLabel(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, root.profiled_time, 3), widget), row, 1, Qt::AlignLeft); + } + ++row; + + lay->addWidget(new QLabel("Wait:", widget), row, 0, Qt::AlignRight); + if (duration) + { + lay->addWidget(new QLabel(QString("%1 (%2%)").arg(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, root.wait_time, 3)) + .arg(QString::number(100. * (double)root.wait_time / (double)duration, 'f', 2)), widget), row, 1, Qt::AlignLeft); + } + else + { + lay->addWidget(new QLabel(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, root.wait_time, 3), widget), row, 1, Qt::AlignLeft); + } + ++row; + + const auto eventsSize = root.events.size(); + + lay->addWidget(new QLabel("Frames:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(QString::number(root.frames_number), widget), row, 1, Qt::AlignLeft); + ++row; + + lay->addWidget(new QLabel("Blocks:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(QString::number(root.blocks_number - eventsSize), widget), row, 1, Qt::AlignLeft); + ++row; + + lay->addWidget(new QLabel("Markers:", widget), row, 0, Qt::AlignRight); + lay->addWidget(new QLabel(QString::number(eventsSize), widget), row, 1, Qt::AlignLeft); + ++row; + + m_popupWidget = new QGraphicsProxyWidget(); + if (m_popupWidget != nullptr) + { + auto effect = new QGraphicsDropShadowEffect(); + effect->setBlurRadius(5); + effect->setOffset(3, 3); + m_popupWidget->setGraphicsEffect(effect); + + m_popupWidget->setWidget(widget); + scene()->addItem(m_popupWidget); + + auto br = m_popupWidget->boundingRect(); + + if (maximumWidth() < br.width()) + { + setFixedWidth(static_cast(br.width())); + visibleSceneRect.setWidth(br.width()); + } + + if (scenePos.y() + br.height() > visibleSceneRect.bottom()) + scenePos.setY(::std::max(scenePos.y() - br.height(), visibleSceneRect.top())); + + if (scenePos.x() + br.width() > visibleSceneRect.right()) + scenePos.setX(::std::max(scenePos.x() - br.width(), visibleSceneRect.left())); + + m_popupWidget->setPos(scenePos); + m_popupWidget->setOpacity(0.95); + } + } +} + +void EasyThreadNamesWidget::repaintScene() +{ + scene()->update(); +} + +void EasyThreadNamesWidget::mousePressEvent(QMouseEvent* _event) +{ + m_idleTime = 0; + + QMouseEvent e(_event->type(), _event->pos() - QPointF(sceneRect().width(), 0), _event->button(), _event->buttons() & ~Qt::RightButton, _event->modifiers()); + m_view->mousePressEvent(&e); + _event->accept(); +} + +void EasyThreadNamesWidget::mouseDoubleClickEvent(QMouseEvent* _event) +{ + static const auto OVERLAP = ::profiler_gui::THREADS_ROW_SPACING >> 1; + + m_idleTime = 0; + + auto y = mapToScene(_event->pos()).y(); + const auto& items = m_view->getItems(); + for (auto item : items) + { + auto br = item->boundingRect(); + auto top = item->y() + br.top() - OVERLAP; + auto bottom = top + br.height() + OVERLAP; + + if (y < top || y > bottom) + continue; + + const auto thread_id = item->threadId(); + if (thread_id != EASY_GLOBALS.selected_thread) + { + EASY_GLOBALS.selected_thread = thread_id; + emit EASY_GLOBALS.events.selectedThreadChanged(thread_id); + } + + break; + } + + _event->accept(); +} + +void EasyThreadNamesWidget::mouseReleaseEvent(QMouseEvent* _event) +{ + m_idleTime = 0; + + QMouseEvent e(_event->type(), _event->pos() - QPointF(sceneRect().width(), 0), _event->button(), _event->buttons() & ~Qt::RightButton, _event->modifiers()); + m_view->mouseReleaseEvent(&e); + _event->accept(); +} + +void EasyThreadNamesWidget::mouseMoveEvent(QMouseEvent* _event) +{ + m_idleTime = 0; + + QMouseEvent e(_event->type(), _event->pos() - QPointF(sceneRect().width(), 0), _event->button(), _event->buttons() & ~Qt::RightButton, _event->modifiers()); + m_view->mouseMoveEvent(&e); + _event->accept(); +} + +void EasyThreadNamesWidget::keyPressEvent(QKeyEvent* _event) +{ + m_idleTime = 0; + m_view->keyPressEvent(_event); +} + +void EasyThreadNamesWidget::keyReleaseEvent(QKeyEvent* _event) +{ + m_idleTime = 0; + m_view->keyReleaseEvent(_event); +} + +void EasyThreadNamesWidget::wheelEvent(QWheelEvent* _event) +{ + m_idleTime = 0; + + auto vbar = m_view->verticalScrollBar(); + if (vbar != nullptr) + { + _event->accept(); + vbar->setValue(vbar->value() - _event->delta()); + } +} + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/blocks_graphics_view.h b/Source/ThirdParty/easy_profiler/profiler_gui/blocks_graphics_view.h new file mode 100644 index 0000000000..3603716f26 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/blocks_graphics_view.h @@ -0,0 +1,349 @@ +/************************************************************************ +* file name : blocks_graphics_view.h +* ----------------- : +* creation time : 2016/06/26 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of GraphicsScene and GraphicsView and +* : it's auxiliary classes for displyaing easy_profiler blocks tree. +* ----------------- : +* change log : * 2016/06/26 Victor Zarubkin: moved sources from graphics_view.h +* : and renamed classes from My* to Prof*. +* : +* : * 2016/06/29 Victor Zarubkin: Highly optimized painting performance and memory consumption. +* : +* : * 2016/06/30 Victor Zarubkin: Replaced doubles with floats (in ProfBlockItem) for less memory consumption. +* : +* : * 2016/09/15 Victor Zarubkin: Moved sources of EasyGraphicsItem and EasyChronometerItem to separate files. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_GRAPHICS_VIEW_H +#define EASY_GRAPHICS_VIEW_H + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "common_types.h" + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +class QGraphicsProxyWidget; +class EasyGraphicsView; +class EasyGraphicsItem; +class EasyGraphicsScrollbar; +class EasyChronometerItem; + +////////////////////////////////////////////////////////////////////////// + +#define EASY_QGRAPHICSITEM(ClassName) \ +class ClassName : public QGraphicsItem { \ + QRectF m_boundingRect; \ +public: \ + ClassName() : QGraphicsItem() {} \ + virtual ~ClassName() {} \ + void paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget = nullptr) override; \ + QRectF boundingRect() const override { return m_boundingRect; } \ + void setBoundingRect(qreal x, qreal y, qreal w, qreal h) { m_boundingRect.setRect(x, y, w, h); } \ + void setBoundingRect(const QRectF& _rect) { m_boundingRect = _rect; } \ +} + +EASY_QGRAPHICSITEM(EasyBackgroundItem); +EASY_QGRAPHICSITEM(EasyTimelineIndicatorItem); +EASY_QGRAPHICSITEM(EasyThreadNameItem); + +#undef EASY_QGRAPHICSITEM + +////////////////////////////////////////////////////////////////////////// + +struct EasyBoldLabel : public QLabel { + EasyBoldLabel(const QString& _text, QWidget* _parent = nullptr); + virtual ~EasyBoldLabel(); +}; + +////////////////////////////////////////////////////////////////////////// + +class EasyGraphicsView : public QGraphicsView +{ + Q_OBJECT + +private: + + typedef QGraphicsView Parent; + typedef EasyGraphicsView This; + typedef ::std::vector Items; + //typedef ::std::unordered_set > Keys; + + Items m_items; ///< Array of all EasyGraphicsItem items + //Keys m_keys; ///< Pressed keyboard keys + ::profiler_gui::TreeBlocks m_selectedBlocks; ///< Array of items which were selected by selection zone (EasyChronometerItem) + QTimer m_flickerTimer; ///< Timer for flicking behavior + QTimer m_idleTimer; ///< + QRectF m_visibleSceneRect; ///< Visible scene rectangle + ::profiler::timestamp_t m_beginTime; ///< Begin time of profiler session. Used to reduce values of all begin and end times of profiler blocks. + qreal m_sceneWidth; ///< + qreal m_scale; ///< Current scale + qreal m_offset; ///< Have to use manual offset for all scene content instead of using scrollbars because QScrollBar::value is 32-bit integer :( + qreal m_timelineStep; ///< + uint64_t m_idleTime; ///< + QPoint m_mousePressPos; ///< Last mouse global position (used by mousePressEvent and mouseMoveEvent) + QPoint m_mouseMovePath; ///< Mouse move path between press and release of any button + Qt::MouseButtons m_mouseButtons; ///< Pressed mouse buttons + EasyGraphicsScrollbar* m_pScrollbar; ///< Pointer to the graphics scrollbar widget + EasyChronometerItem* m_chronometerItem; ///< Pointer to the EasyChronometerItem which is displayed when you press right mouse button and move mouse left or right. This item is used to select blocks to display in tree widget. + EasyChronometerItem* m_chronometerItemAux; ///< Pointer to the EasyChronometerItem which is displayed when you double click left mouse button and move mouse left or right. This item is used only to measure time. + QGraphicsProxyWidget* m_popupWidget; ///< + int m_flickerSpeedX; ///< Current flicking speed x + int m_flickerSpeedY; ///< Current flicking speed y + int m_flickerCounterX; + int m_flickerCounterY; + bool m_bDoubleClick; ///< Is mouse buttons double clicked + bool m_bUpdatingRect; ///< Stub flag which is used to avoid excess calculations on some scene update (flicking, scaling and so on) + bool m_bEmpty; ///< Indicates whether scene is empty and has no items + +public: + + explicit EasyGraphicsView(QWidget* _parent = nullptr); + virtual ~EasyGraphicsView(); + + // Public virtual methods + + void wheelEvent(QWheelEvent* _event) override; + void mousePressEvent(QMouseEvent* _event) override; + void mouseDoubleClickEvent(QMouseEvent* _event) override; + void mouseReleaseEvent(QMouseEvent* _event) override; + void mouseMoveEvent(QMouseEvent* _event) override; + void keyPressEvent(QKeyEvent* _event) override; + void keyReleaseEvent(QKeyEvent* _event) override; + void resizeEvent(QResizeEvent* _event) override; + + void dragEnterEvent(QDragEnterEvent*) override {} + +public: + + // Public non-virtual methods + + qreal sceneWidth() const; + qreal chronoTime() const; + qreal chronoTimeAux() const; + + void setScrollbar(EasyGraphicsScrollbar* _scrollbar); + void clear(); + + void setTree(const ::profiler::thread_blocks_tree_t& _blocksTree); + + const Items& getItems() const; + +signals: + + // Signals + + void sceneUpdated(); + void treeChanged(); + void intervalChanged(const ::profiler_gui::TreeBlocks& _blocks, ::profiler::timestamp_t _session_begin_time, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict); + +private: + + // Private non-virtual methods + + void removePopup(bool _removeFromScene = false); + + EasyChronometerItem* createChronometer(bool _main = true); + bool moveChrono(EasyChronometerItem* _chronometerItem, qreal _mouseX); + void initMode(); + int updateVisibleSceneRect(); + void updateTimelineStep(qreal _windowWidth); + void scaleTo(qreal _scale); + void scrollTo(const EasyGraphicsItem* _item); + void onWheel(qreal _mouseX, int _wheelDelta); + qreal setTree(EasyGraphicsItem* _item, const ::profiler::BlocksTree::children_t& _children, qreal& _height, uint32_t& _maxDepthChild, qreal _y, short _level); + +private slots: + + // Private Slots + + void repaintScene(); + void onGraphicsScrollbarWheel(qreal _mouseX, int _wheelDelta); + void onScrollbarValueChange(int); + void onGraphicsScrollbarValueChange(qreal); + void onFlickerTimeout(); + void onIdleTimeout(); + void onHierarchyFlagChange(bool _value); + void onSelectedThreadChange(::profiler::thread_id_t _id); + void onSelectedBlockChange(unsigned int _block_index); + void onRefreshRequired(); + void onThreadViewChanged(); + +public: + + // Public inline methods + + inline qreal scale() const + { + return m_scale; + } + + inline qreal offset() const + { + return m_offset; + } + + inline const QRectF& visibleSceneRect() const + { + return m_visibleSceneRect; + } + + inline qreal timelineStep() const + { + return m_timelineStep; + } + + inline qreal time2position(const profiler::timestamp_t& _time) const + { + return PROF_MICROSECONDS(qreal(_time - m_beginTime)); + //return PROF_MILLISECONDS(qreal(_time - m_beginTime)); + } + + inline ::profiler::timestamp_t position2time(qreal _pos) const + { + return PROF_FROM_MICROSECONDS(_pos); + //return PROF_FROM_MILLISECONDS(_pos); + } + +}; // END of class EasyGraphicsView. + +////////////////////////////////////////////////////////////////////////// + +class EasyThreadNamesWidget : public QGraphicsView +{ + Q_OBJECT + +private: + + typedef QGraphicsView Parent; + typedef EasyThreadNamesWidget This; + + QTimer m_idleTimer; ///< + uint64_t m_idleTime; ///< + EasyGraphicsView* m_view; ///< + QGraphicsProxyWidget* m_popupWidget; ///< + int m_maxLength; ///< + const int m_additionalHeight; ///< + +public: + + explicit EasyThreadNamesWidget(EasyGraphicsView* _view, int _additionalHeight, QWidget* _parent = nullptr); + virtual ~EasyThreadNamesWidget(); + + void mousePressEvent(QMouseEvent* _event) override; + void mouseDoubleClickEvent(QMouseEvent* _event) override; + void mouseReleaseEvent(QMouseEvent* _event) override; + void mouseMoveEvent(QMouseEvent* _event) override; + void keyPressEvent(QKeyEvent* _event) override; + void keyReleaseEvent(QKeyEvent* _event) override; + void wheelEvent(QWheelEvent* _event) override; + + void dragEnterEvent(QDragEnterEvent*) override {} + + void clear(); + + const EasyGraphicsView* view() const + { + return m_view; + } + +private: + + void removePopup(bool _removeFromScene = false); + +private slots: + + void setVerticalScrollbarRange(int _minValue, int _maxValue); + void onTreeChange(); + void onIdleTimeout(); + void repaintScene(); + +}; // END of class EasyThreadNamesWidget. + +////////////////////////////////////////////////////////////////////////// + +class EasyGraphicsViewWidget : public QWidget +{ + Q_OBJECT + +private: + + EasyGraphicsScrollbar* m_scrollbar; + EasyGraphicsView* m_view; + EasyThreadNamesWidget* m_threadNamesWidget; + +public: + + explicit EasyGraphicsViewWidget(QWidget* _parent = nullptr); + virtual ~EasyGraphicsViewWidget(); + + EasyGraphicsView* view(); + void clear(); + +private: + + void initWidget(); + +}; // END of class EasyGraphicsViewWidget. + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_GRAPHICS_VIEW_H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/blocks_tree_widget.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/blocks_tree_widget.cpp new file mode 100644 index 0000000000..f9b4755617 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/blocks_tree_widget.cpp @@ -0,0 +1,1320 @@ +/************************************************************************ +* file name : blocks_tree_widget.cpp +* ----------------- : +* creation time : 2016/06/26 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of EasyTreeWidget and it's auxiliary classes +* : for displyaing easy_profiler blocks tree. +* ----------------- : +* change log : * 2016/06/26 Victor Zarubkin: Moved sources from tree_view.h +* : and renamed classes from My* to Prof*. +* : +* : * 2016/06/27 Victor Zarubkin: Added possibility to colorize rows +* : with profiler blocks' colors. +* : Also added displaying frame statistics for blocks. +* : Disabled sorting by name to save order of threads displayed on graphics view. +* : +* : * 2016/06/29 Victor Zarubkin: Added clearSilent() method. +* : +* : * 2016/08/18 Victor Zarubkin: Moved sources of TreeWidgetItem into tree_widget_item.h/.cpp +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "blocks_tree_widget.h" +#include "globals.h" + +#ifdef _WIN32 +#include + +#ifdef __MINGW32__ +#include +#endif + +#endif + +#ifdef max +#undef max +#endif + +#ifdef min +#undef min +#endif + +////////////////////////////////////////////////////////////////////////// + +const int HIERARCHY_BUILDER_TIMER_INTERVAL = 40; + +const bool SIMPLIFIED_REGIME_COLUMNS[COL_COLUMNS_NUMBER] = { + true, //COL_NAME, + true, //COL_BEGIN, + true, //COL_DURATION, + true, //COL_SELF_DURATION, + false, //COL_DURATION_SUM_PER_PARENT, + false, //COL_DURATION_SUM_PER_FRAME, + true, //COL_DURATION_SUM_PER_THREAD, + true, //COL_SELF_DURATION_PERCENT, + false, //COL_PERCENT_PER_PARENT, + true, //COL_PERCENT_PER_FRAME, + false, //COL_PERCENT_SUM_PER_PARENT, + false, //COL_PERCENT_SUM_PER_FRAME, + true, //COL_PERCENT_SUM_PER_THREAD, + true, //COL_END, + true, //COL_MIN_PER_FRAME, + true, //COL_MAX_PER_FRAME, + true, //COL_AVERAGE_PER_FRAME, + true, //COL_NCALLS_PER_FRAME, + true, //COL_MIN_PER_THREAD, + true, //COL_MAX_PER_THREAD, + true, //COL_AVERAGE_PER_THREAD, + true, //COL_NCALLS_PER_THREAD, + false, //COL_MIN_PER_PARENT, + false, //COL_MAX_PER_PARENT, + false, //COL_AVERAGE_PER_PARENT, + false, //COL_NCALLS_PER_PARENT, + true, //COL_ACTIVE_TIME, + true //COL_ACTIVE_PERCENT, +}; + +////////////////////////////////////////////////////////////////////////// + +EasyTreeWidget::EasyTreeWidget(QWidget* _parent) + : Parent(_parent) + , m_beginTime(::std::numeric_limits::max()) + , m_lastFound(nullptr) + , m_progress(nullptr) + , m_hintLabel(nullptr) + , m_mode(EasyTreeMode_Plain) + , m_bColorRows(true) + , m_bLocked(false) + , m_bSilentExpandCollapse(false) +{ + memset(m_columnsHiddenStatus, 0, sizeof(m_columnsHiddenStatus)); + + setAutoFillBackground(false); + setAlternatingRowColors(true); + setItemsExpandable(true); + setAnimated(true); + setSortingEnabled(false); + setColumnCount(COL_COLUMNS_NUMBER); + + auto header_item = new QTreeWidgetItem(); + auto f = header()->font(); + f.setBold(true); + header()->setFont(f);// ::profiler_gui::EFont("Helvetica", 9, QFont::Bold)); + + header_item->setText(COL_NAME, "Name"); + + header_item->setText(COL_BEGIN, "Begin, ms"); + + header_item->setText(COL_DURATION, "Duration"); + header_item->setText(COL_SELF_DURATION, "Self dur."); + //header_item->setToolTip(COL_SELF_DURATION, ""); + header_item->setText(COL_DURATION_SUM_PER_PARENT, "Total / Parent"); + header_item->setText(COL_DURATION_SUM_PER_FRAME, "Total / Frame"); + header_item->setText(COL_DURATION_SUM_PER_THREAD, "Total / Thread"); + + header_item->setText(COL_SELF_DURATION_PERCENT, "Self %"); + header_item->setText(COL_PERCENT_PER_PARENT, "% / Parent"); + header_item->setText(COL_PERCENT_PER_FRAME, "% / Frame"); + header_item->setText(COL_PERCENT_SUM_PER_FRAME, "Sum % / Frame"); + header_item->setText(COL_PERCENT_SUM_PER_PARENT, "Sum % / Parent"); + header_item->setText(COL_PERCENT_SUM_PER_THREAD, "Sum % / Thread"); + + header_item->setText(COL_END, "End, ms"); + + header_item->setText(COL_MIN_PER_FRAME, "Min / Frame"); + header_item->setText(COL_MAX_PER_FRAME, "Max / Frame"); + header_item->setText(COL_AVERAGE_PER_FRAME, "Avg / Frame"); + header_item->setText(COL_NCALLS_PER_FRAME, "N Calls / Frame"); + + header_item->setText(COL_MIN_PER_PARENT, "Min / Parent"); + header_item->setText(COL_MAX_PER_PARENT, "Max / Parent"); + header_item->setText(COL_AVERAGE_PER_PARENT, "Avg / Parent"); + header_item->setText(COL_NCALLS_PER_PARENT, "N Calls / Parent"); + + header_item->setText(COL_MIN_PER_THREAD, "Min / Thread"); + header_item->setText(COL_MAX_PER_THREAD, "Max / Thread"); + header_item->setText(COL_AVERAGE_PER_THREAD, "Avg / Thread"); + header_item->setText(COL_NCALLS_PER_THREAD, "N Calls / Thread"); + + header_item->setText(COL_ACTIVE_TIME, "Active time"); + header_item->setText(COL_ACTIVE_PERCENT, "Active %"); + + auto color = QColor::fromRgb(::profiler::colors::DeepOrange900); + header_item->setForeground(COL_MIN_PER_THREAD, color); + header_item->setForeground(COL_MAX_PER_THREAD, color); + header_item->setForeground(COL_AVERAGE_PER_THREAD, color); + header_item->setForeground(COL_NCALLS_PER_THREAD, color); + header_item->setForeground(COL_PERCENT_SUM_PER_THREAD, color); + header_item->setForeground(COL_DURATION_SUM_PER_THREAD, color); + + color = QColor::fromRgb(::profiler::colors::Blue900); + header_item->setForeground(COL_MIN_PER_FRAME, color); + header_item->setForeground(COL_MAX_PER_FRAME, color); + header_item->setForeground(COL_AVERAGE_PER_FRAME, color); + header_item->setForeground(COL_NCALLS_PER_FRAME, color); + header_item->setForeground(COL_PERCENT_SUM_PER_FRAME, color); + header_item->setForeground(COL_DURATION_SUM_PER_FRAME, color); + header_item->setForeground(COL_PERCENT_PER_FRAME, color); + + color = QColor::fromRgb(::profiler::colors::Teal900); + header_item->setForeground(COL_MIN_PER_PARENT, color); + header_item->setForeground(COL_MAX_PER_PARENT, color); + header_item->setForeground(COL_AVERAGE_PER_PARENT, color); + header_item->setForeground(COL_NCALLS_PER_PARENT, color); + header_item->setForeground(COL_PERCENT_SUM_PER_PARENT, color); + header_item->setForeground(COL_DURATION_SUM_PER_PARENT, color); + header_item->setForeground(COL_PERCENT_PER_PARENT, color); + + setHeaderItem(header_item); + + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::selectedThreadChanged, this, &This::onSelectedThreadChange); + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::selectedBlockChanged, this, &This::onSelectedBlockChange); + connect(&m_fillTimer, &QTimer::timeout, this, &This::onFillTimerTimeout); + + loadSettings(); + + if (m_mode == EasyTreeMode_Full) + { + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + m_columnsHiddenStatus[i] = isColumnHidden(i) ? 1 : 0; + } + else + { + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + { + if (SIMPLIFIED_REGIME_COLUMNS[i]) + { + if (isColumnHidden(i)) + m_columnsHiddenStatus[i] = 1; + } + else if (!isColumnHidden(i)) + { + setColumnHidden(i, true); + } + } + } + + m_progress = new QProgressDialog("Building blocks hierarchy...", "", 0, 100, this, Qt::FramelessWindowHint); + m_progress->setAttribute(Qt::WA_TranslucentBackground); + m_progress->setCancelButton(nullptr); + m_progress->setValue(100); + //m_progress->hide(); + + m_hintLabel = new QLabel("Use Right Mouse Button on the Diagram to build a hierarchy...\nPress and hold, move, release", this); + m_hintLabel->setAlignment(Qt::AlignCenter); + m_hintLabel->setStyleSheet("QLabel { color: gray; font: 12pt; }"); + + QTimer::singleShot(1500, this, &This::alignProgressBar); +} + +EasyTreeWidget::~EasyTreeWidget() +{ + saveSettings(); + delete m_progress; + delete m_hintLabel; +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::onFillTimerTimeout() +{ + if (m_hierarchyBuilder.done()) + { + m_fillTimer.stop(); + + ThreadedItems toplevelitems; + m_hierarchyBuilder.takeItems(m_items); + m_hierarchyBuilder.takeTopLevelItems(toplevelitems); + m_hierarchyBuilder.interrupt(); + { + const QSignalBlocker b(this); + for (auto& item : toplevelitems) + { + addTopLevelItem(item.second); + m_roots[item.first] = item.second; + } + } + + if (m_progress) + { + m_progress->setValue(100); + //m_progress->hide(); + } + + m_bLocked = false; + m_inputBlocks.clear(); + + setSortingEnabled(true); + + sortByColumn(COL_BEGIN, Qt::AscendingOrder); // sort by begin time + if (m_mode == EasyTreeMode_Plain) // and after that, sort by frame % + sortByColumn(COL_PERCENT_PER_FRAME, Qt::DescendingOrder); + + //resizeColumnToContents(COL_NAME); + resizeColumnsToContents(); + + connect(this, &Parent::itemExpanded, this, &This::onItemExpand); + connect(this, &Parent::itemCollapsed, this, &This::onItemCollapse); + connect(this, &Parent::currentItemChanged, this, &This::onCurrentItemChange); + onSelectedThreadChange(EASY_GLOBALS.selected_thread); + onSelectedBlockChange(EASY_GLOBALS.selected_block); + } + else + { + m_progress->setValue(m_hierarchyBuilder.progress()); + } +} + +void EasyTreeWidget::setTree(const unsigned int _blocksNumber, const ::profiler::thread_blocks_tree_t& _blocksTree) +{ + clearSilent(); + + if (!_blocksTree.empty()) + { + m_bLocked = true; + m_hintLabel->hide(); + m_progress->setValue(0); + m_progress->show(); + m_hierarchyBuilder.fillTree(m_beginTime, _blocksNumber, _blocksTree, m_bColorRows, m_mode); + m_fillTimer.start(HIERARCHY_BUILDER_TIMER_INTERVAL); + } + + //StubLocker l; + //ThreadedItems toplevelitems; + //FillTreeClass::setTreeInternal1(l, m_items, toplevelitems, m_beginTime, _blocksNumber, _blocksTree, m_bColorRows); + //{ + // const QSignalBlocker b(this); + // for (auto& item : toplevelitems) + // { + // addTopLevelItem(item.second); + // m_roots[item.first] = item.second; + // if (item.first == EASY_GLOBALS.selected_thread) + // item.second->colorize(true); + // } + //} +} + +void EasyTreeWidget::setTreeBlocks(const ::profiler_gui::TreeBlocks& _blocks, ::profiler::timestamp_t _session_begin_time, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict) +{ + clearSilent(); + + m_beginTime = _session_begin_time; + _left += m_beginTime;// - ::std::min(m_beginTime, 1000ULL); + _right += m_beginTime;// + 1000; + + m_inputBlocks = _blocks; + if (!m_inputBlocks.empty()) + { + m_bLocked = true; + m_hintLabel->hide(); + m_progress->setValue(0); + m_progress->show(); + m_hierarchyBuilder.fillTreeBlocks(m_inputBlocks, _session_begin_time, _left, _right, _strict, m_bColorRows, m_mode); + m_fillTimer.start(HIERARCHY_BUILDER_TIMER_INTERVAL); + } + + //StubLocker l; + //ThreadedItems toplevelitems; + //FillTreeClass::setTreeInternal2(l, m_items, toplevelitems, m_beginTime, _blocks, _left, _right, _strict, m_bColorRows); + //{ + // const QSignalBlocker b(this); + // for (auto& item : toplevelitems) + // { + // addTopLevelItem(item.second); + // m_roots[item.first] = item.second; + // if (item.first == EASY_GLOBALS.selected_thread) + // item.second->colorize(true); + // } + //} + + //setSortingEnabled(true); + //sortByColumn(COL_BEGIN, Qt::AscendingOrder); + //resizeColumnToContents(COL_NAME); + + //connect(this, &Parent::itemExpanded, this, &This::onItemExpand); + //connect(this, &Parent::itemCollapsed, this, &This::onItemCollapse); + //onSelectedBlockChange(EASY_GLOBALS.selected_block); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::clearSilent(bool _global) +{ + const QSignalBlocker b(this); + + m_hierarchyBuilder.interrupt(); + + if (m_progress) + { + m_progress->setValue(100); + //m_progress->hide(); + } + + m_hintLabel->show(); + + m_bLocked = false; + m_beginTime = ::std::numeric_limits::max(); + + setSortingEnabled(false); + disconnect(this, &Parent::itemExpanded, this, &This::onItemExpand); + disconnect(this, &Parent::itemCollapsed, this, &This::onItemCollapse); + disconnect(this, &Parent::currentItemChanged, this, &This::onCurrentItemChange); + m_lastFound = nullptr; + m_lastSearch.clear(); + + if (!_global) + { + if (EASY_GLOBALS.collapse_items_on_tree_close) +#ifdef EASY_TREE_WIDGET__USE_VECTOR + for (auto item : m_items) +#else + for (auto& item : m_items) +#endif + { +#ifdef EASY_TREE_WIDGET__USE_VECTOR + auto& gui_block = item->guiBlock(); + gui_block.expanded = false; + ::profiler_gui::set_max(gui_block.tree_item); +#else + item.second->guiBlock().expanded = false; +#endif + } +#ifdef EASY_TREE_WIDGET__USE_VECTOR + else for (auto item : m_items) + { + ::profiler_gui::set_max(item->guiBlock().tree_item); + } +#endif + } + + m_items.clear(); + m_roots.clear(); + + ::std::vector topLevelItems; + topLevelItems.reserve(topLevelItemCount()); + for (int i = topLevelItemCount() - 1; i >= 0; --i) + topLevelItems.push_back(takeTopLevelItem(i)); + + auto deleter_thread = ::std::thread([](decltype(topLevelItems) _items) + { +#ifdef _WIN32 + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST); +#endif + + for (auto item : _items) + delete item; + + }, ::std::move(topLevelItems)); + + deleter_thread.detach(); + + //clear(); + + if (!_global) + emit EASY_GLOBALS.events.itemsExpandStateChanged(); +} + +////////////////////////////////////////////////////////////////////////// + +int EasyTreeWidget::findNext(const QString& _str, Qt::MatchFlags _flags) +{ + if (m_bLocked || _str.isEmpty()) + return 0; + + const bool isNewSearch = (m_lastSearch != _str); + auto itemsList = findItems(_str, Qt::MatchContains | Qt::MatchRecursive | _flags, COL_NAME); + + if (!isNewSearch) + { + if (!itemsList.empty()) + { + bool stop = false; + decltype(m_lastFound) next = nullptr; + for (auto item : itemsList) + { + if (item->parent() == nullptr) + continue; + + if (stop) + { + next = item; + break; + } + + stop = item == m_lastFound; + } + + m_lastFound = next == nullptr ? itemsList.front() : next; + } + else + { + m_lastFound = nullptr; + } + } + else + { + m_lastSearch = _str; + m_lastFound = !itemsList.empty() ? itemsList.front() : nullptr; + } + + if (m_lastFound != nullptr) + { + scrollToItem(m_lastFound, QAbstractItemView::PositionAtCenter); + setCurrentItem(m_lastFound); + } + + return itemsList.size(); +} + +int EasyTreeWidget::findPrev(const QString& _str, Qt::MatchFlags _flags) +{ + if (m_bLocked || _str.isEmpty()) + return 0; + + const bool isNewSearch = (m_lastSearch != _str); + auto itemsList = findItems(_str, Qt::MatchContains | Qt::MatchRecursive | _flags, COL_NAME); + + if (!isNewSearch) + { + if (!itemsList.empty()) + { + decltype(m_lastFound) prev = nullptr; + for (auto item : itemsList) + { + if (item->parent() == nullptr) + continue; + + if (item == m_lastFound) + break; + + prev = item; + } + + m_lastFound = prev == nullptr ? itemsList.back() : prev; + } + else + { + m_lastFound = nullptr; + } + } + else + { + m_lastSearch = _str; + m_lastFound = !itemsList.empty() ? itemsList.front() : nullptr; + } + + if (m_lastFound != nullptr) + { + scrollToItem(m_lastFound, QAbstractItemView::PositionAtCenter); + setCurrentItem(m_lastFound); + } + + return itemsList.size(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::contextMenuEvent(QContextMenuEvent* _event) +{ + if (m_bLocked) + { + _event->accept(); + return; + } + + const auto col = currentColumn(); + auto item = static_cast(currentItem()); + QMenu menu; + menu.setToolTipsVisible(true); + QAction* action = nullptr; + + if (!m_items.empty()) + { + action = menu.addAction("Expand all"); + connect(action, &QAction::triggered, this, &This::onExpandAllClicked); + SET_ICON(action, ":/Expand"); + + action = menu.addAction("Collapse all"); + connect(action, &QAction::triggered, this, &This::onCollapseAllClicked); + SET_ICON(action, ":/Collapse"); + + if (item != nullptr && col >= 0) + { + menu.addSeparator(); + + action = menu.addAction("Expand all children"); + connect(action, &QAction::triggered, this, &This::onExpandAllChildrenClicked); + SET_ICON(action, ":/Expand"); + + action = menu.addAction("Collapse all children"); + connect(action, &QAction::triggered, this, &This::onCollapseAllChildrenClicked); + SET_ICON(action, ":/Collapse"); + } + + menu.addSeparator(); + } + + action = menu.addAction("Hierarchy mode"); + action->setToolTip("Display full blocks hierarchy"); + action->setCheckable(true); + action->setChecked(m_mode == EasyTreeMode_Full); + action->setData((quint32)EasyTreeMode_Full); + connect(action, &QAction::triggered, this, &This::onModeChange); + + action = menu.addAction("Plain mode"); + action->setToolTip("Display plain list of blocks per frame.\nSome columns are disabled with this mode."); + action->setCheckable(true); + action->setChecked(m_mode == EasyTreeMode_Plain); + action->setData((quint32)EasyTreeMode_Plain); + connect(action, &QAction::triggered, this, &This::onModeChange); + + menu.addSeparator(); + + action = menu.addAction("Color rows"); + action->setToolTip("Colorize rows with same colors as on diagram"); + action->setCheckable(true); + action->setChecked(m_bColorRows); + connect(action, &QAction::triggered, this, &This::onColorizeRowsTriggered); + if (m_bColorRows) { + auto f = action->font(); + f.setBold(true); + action->setFont(f); + SET_ICON(action, ":/Color"); + } + else SET_ICON(action, ":/NoColor"); + + if (item != nullptr && item->parent() != nullptr) + { + if (col >= 0) + { + switch (col) + { + case COL_MIN_PER_THREAD: + case COL_MIN_PER_PARENT: + case COL_MIN_PER_FRAME: + case COL_MAX_PER_THREAD: + case COL_MAX_PER_PARENT: + case COL_MAX_PER_FRAME: + { + auto& block = item->block(); + auto i = ::profiler_gui::numeric_max(); + switch (col) + { + case COL_MIN_PER_THREAD: i = block.per_thread_stats->min_duration_block; break; + case COL_MIN_PER_PARENT: i = block.per_parent_stats->min_duration_block; break; + case COL_MIN_PER_FRAME: i = block.per_frame_stats->min_duration_block; break; + case COL_MAX_PER_THREAD: i = block.per_thread_stats->max_duration_block; break; + case COL_MAX_PER_PARENT: i = block.per_parent_stats->max_duration_block; break; + case COL_MAX_PER_FRAME: i = block.per_frame_stats->max_duration_block; break; + } + + if (i != ::profiler_gui::numeric_max(i)) + { + menu.addSeparator(); + auto itemAction = new QAction("Jump to such item", nullptr); + itemAction->setData(i); + itemAction->setToolTip("Jump to item with min/max duration (depending on clicked column)"); + connect(itemAction, &QAction::triggered, this, &This::onJumpToItemClicked); + menu.addAction(itemAction); + } + + break; + } + } + } + + const auto& desc = easyDescriptor(item->block().node->id()); + auto submenu = menu.addMenu("Block status"); + submenu->setToolTipsVisible(true); + +#define ADD_STATUS_ACTION(NameValue, StatusValue, ToolTipValue)\ + action = submenu->addAction(NameValue);\ + action->setCheckable(true);\ + action->setChecked(desc.status() == StatusValue);\ + action->setData(static_cast(StatusValue));\ + action->setToolTip(ToolTipValue);\ + connect(action, &QAction::triggered, this, &This::onBlockStatusChangeClicked) + + ADD_STATUS_ACTION("Off", ::profiler::OFF, "Do not profile this block."); + ADD_STATUS_ACTION("On", ::profiler::ON, "Profile this block\nif parent enabled children."); + ADD_STATUS_ACTION("Force-On", ::profiler::FORCE_ON, "Always profile this block even\nif it's parent disabled children."); + ADD_STATUS_ACTION("Off-recursive", ::profiler::OFF_RECURSIVE, "Do not profile neither this block\nnor it's children."); + ADD_STATUS_ACTION("On-without-children", ::profiler::ON_WITHOUT_CHILDREN, "Profile this block, but\ndo not profile it's children."); + ADD_STATUS_ACTION("Force-On-without-children", ::profiler::FORCE_ON_WITHOUT_CHILDREN, "Always profile this block, but\ndo not profile it's children."); +#undef ADD_STATUS_ACTION + + submenu->setEnabled(EASY_GLOBALS.connected); + if (!EASY_GLOBALS.connected) + submenu->setTitle(QString("%1 (connection needed)").arg(submenu->title())); + } + + menu.addSeparator(); + + auto hidemenu = menu.addMenu("Select columns"); + auto hdr = headerItem(); + + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + { + auto columnAction = new QAction(hdr->text(i), nullptr); + columnAction->setData(i); + columnAction->setCheckable(true); + columnAction->setChecked(m_columnsHiddenStatus[i] == 0);// !isColumnHidden(i)); + if (m_mode == EasyTreeMode_Full || SIMPLIFIED_REGIME_COLUMNS[i]) + connect(columnAction, &QAction::triggered, this, &This::onHideShowColumn); + else + columnAction->setEnabled(false); + hidemenu->addAction(columnAction); + } + + menu.exec(QCursor::pos()); + + _event->accept(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::resizeEvent(QResizeEvent* _event) +{ + Parent::resizeEvent(_event); + alignProgressBar(); +} + +void EasyTreeWidget::moveEvent(QMoveEvent* _event) +{ + Parent::moveEvent(_event); + alignProgressBar(); +} + +void EasyTreeWidget::alignProgressBar() +{ + auto center = rect().center(); + auto pos = mapToGlobal(center); + m_progress->move(pos.x() - (m_progress->width() >> 1), pos.y() - (m_progress->height() >> 1)); + m_hintLabel->move(center.x() - (m_hintLabel->width() >> 1), std::max(center.y() - (m_hintLabel->height() >> 1), header()->height())); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::onJumpToItemClicked(bool) +{ + auto action = qobject_cast(sender()); + if (action == nullptr) + return; + + auto block_index = action->data().toUInt(); + EASY_GLOBALS.selected_block = block_index; + if (block_index < EASY_GLOBALS.gui_blocks.size()) + EASY_GLOBALS.selected_block_id = easyBlock(block_index).tree.node->id(); + else + ::profiler_gui::set_max(EASY_GLOBALS.selected_block_id); + emit EASY_GLOBALS.events.selectedBlockChanged(block_index); +} + +void EasyTreeWidget::onCollapseAllClicked(bool) +{ + const QSignalBlocker b(this); + + m_bSilentExpandCollapse = true; + collapseAll(); + m_bSilentExpandCollapse = false; + + if (EASY_GLOBALS.bind_scene_and_tree_expand_status) + { +#ifdef EASY_TREE_WIDGET__USE_VECTOR + for (auto item : m_items) + item->guiBlock().expanded = false; +#else + for (auto& item : m_items) + item.second->guiBlock().expanded = false; +#endif + emit EASY_GLOBALS.events.itemsExpandStateChanged(); + } +} + +void EasyTreeWidget::onExpandAllClicked(bool) +{ + const QSignalBlocker b(this); + + m_bSilentExpandCollapse = true; + expandAll(); + resizeColumnsToContents(); + m_bSilentExpandCollapse = false; + + if (EASY_GLOBALS.bind_scene_and_tree_expand_status) + { +#ifdef EASY_TREE_WIDGET__USE_VECTOR + for (auto item : m_items){ + auto& b = item->guiBlock(); +#else + for (auto& item : m_items){ + auto& b = item.second->guiBlock(); +#endif + b.expanded = !b.tree.children.empty(); + } + + emit EASY_GLOBALS.events.itemsExpandStateChanged(); + } +} + +void EasyTreeWidget::onCollapseAllChildrenClicked(bool) +{ + auto current = static_cast(currentItem()); + if (current != nullptr) + { + const QSignalBlocker b(this); + + m_bSilentExpandCollapse = true; + current->collapseAll(); + m_bSilentExpandCollapse = false; + + emit EASY_GLOBALS.events.itemsExpandStateChanged(); + } +} + +void EasyTreeWidget::onExpandAllChildrenClicked(bool) +{ + auto current = static_cast(currentItem()); + if (current != nullptr) + { + const QSignalBlocker b(this); + + m_bSilentExpandCollapse = true; + current->expandAll(); + resizeColumnsToContents(); + m_bSilentExpandCollapse = false; + + emit EASY_GLOBALS.events.itemsExpandStateChanged(); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::onBlockStatusChangeClicked(bool _checked) +{ + if (!_checked) + return; + + auto item = static_cast(currentItem()); + if (item == nullptr) + return; + + auto action = qobject_cast(sender()); + if (action != nullptr) + { + auto& desc = easyDescriptor(item->block().node->id()); + desc.setStatus(static_cast<::profiler::EasyBlockStatus>(action->data().toUInt())); + emit EASY_GLOBALS.events.blockStatusChanged(desc.id(), desc.status()); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::onItemExpand(QTreeWidgetItem* _item) +{ + if (!EASY_GLOBALS.bind_scene_and_tree_expand_status || _item->parent() == nullptr) + { + resizeColumnsToContents(); + return; + } + + static_cast(_item)->guiBlock().expanded = true; + + if (!m_bSilentExpandCollapse) + { + resizeColumnsToContents(); + emit EASY_GLOBALS.events.itemsExpandStateChanged(); + } +} + +void EasyTreeWidget::onItemCollapse(QTreeWidgetItem* _item) +{ + if (!EASY_GLOBALS.bind_scene_and_tree_expand_status || _item->parent() == nullptr) + return; + + static_cast(_item)->guiBlock().expanded = false; + + if (!m_bSilentExpandCollapse) + emit EASY_GLOBALS.events.itemsExpandStateChanged(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::onCurrentItemChange(QTreeWidgetItem* _item, QTreeWidgetItem* _previous) +{ + if (_previous != nullptr) + { + auto f = font(); + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + _previous->setFont(i, f); + } + + if (_item == nullptr) + { + ::profiler_gui::set_max(EASY_GLOBALS.selected_block); + ::profiler_gui::set_max(EASY_GLOBALS.selected_block_id); + } + else + { + auto f = font(); + f.setBold(true); + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + _item->setFont(i, f); + + EASY_GLOBALS.selected_block = static_cast(_item)->block_index(); + if (EASY_GLOBALS.selected_block < EASY_GLOBALS.gui_blocks.size()) + EASY_GLOBALS.selected_block_id = easyBlock(EASY_GLOBALS.selected_block).tree.node->id(); + else + ::profiler_gui::set_max(EASY_GLOBALS.selected_block_id); + } + + disconnect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::selectedBlockChanged, this, &This::onSelectedBlockChange); + emit EASY_GLOBALS.events.selectedBlockChanged(EASY_GLOBALS.selected_block); + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::selectedBlockChanged, this, &This::onSelectedBlockChange); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::onColorizeRowsTriggered(bool _colorize) +{ + const QSignalBlocker b(this); + + m_bColorRows = _colorize; + + auto current = currentItem(); + collapseAll(); // Without collapseAll() changing items process is VERY VERY SLOW. + // TODO: Find the reason of such behavior. QSignalBlocker(this) does not help. QSignalBlocker(item) does not work, because items are not inherited from QObject. + +#ifdef EASY_TREE_WIDGET__USE_VECTOR + for (auto item : m_items) + { + if (item->parent() != nullptr) + item->colorize(m_bColorRows); + } +#else + for (auto& item : m_items) + { + if (item.second->parent() != nullptr) + item.second->colorize(m_bColorRows); + } +#endif + + // Scroll back to previously selected item + if (current) + { + scrollToItem(current, QAbstractItemView::PositionAtCenter); + setCurrentItem(current); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::onSelectedThreadChange(::profiler::thread_id_t _id) +{ + for (auto& it : m_roots) + { + it.second->colorize(it.first == _id); + } +} + +void EasyTreeWidget::onSelectedBlockChange(uint32_t _block_index) +{ + disconnect(this, &Parent::currentItemChanged, this, &This::onCurrentItemChange); + + EasyTreeWidgetItem* item = nullptr; + + if (_block_index < EASY_GLOBALS.gui_blocks.size()) + { +#ifdef EASY_TREE_WIDGET__USE_VECTOR + const auto i = easyBlock(_block_index).tree_item; + if (i < m_items.size()) + item = m_items[i]; +#else + auto it = m_items.find(_block_index); + if (it != m_items.end()) + item = it->second; +#endif + } + + if (item != nullptr) + { + //const QSignalBlocker b(this); + auto previous = currentItem(); + auto f = font(); + if (previous != nullptr) for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + previous->setFont(i, f); + + if (EASY_GLOBALS.bind_scene_and_tree_expand_status) + { + m_bSilentExpandCollapse = true; + setCurrentItem(item); + scrollToItem(item, QAbstractItemView::PositionAtCenter); + if (item->guiBlock().expanded) + expandItem(item); + else + collapseItem(item); + resizeColumnsToContents(); + m_bSilentExpandCollapse = false; + + emit EASY_GLOBALS.events.itemsExpandStateChanged(); + } + else + { + disconnect(this, &Parent::itemExpanded, this, &This::onItemExpand); + setCurrentItem(item); + scrollToItem(item, QAbstractItemView::PositionAtCenter); + resizeColumnsToContents(); + connect(this, &Parent::itemExpanded, this, &This::onItemExpand); + } + + f.setBold(true); + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + item->setFont(i, f); + } + else + { + auto previous = currentItem(); + if (previous != nullptr) + { + auto f = font(); + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + previous->setFont(i, f); + } + + setCurrentItem(item); + } + + connect(this, &Parent::currentItemChanged, this, &This::onCurrentItemChange); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::resizeColumnsToContents() +{ + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + { + resizeColumnToContents(i); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::onHideShowColumn(bool) +{ + auto action = qobject_cast(sender()); + if (action == nullptr) + return; + + const auto col = action->data().toInt(); + const bool hideCol = m_columnsHiddenStatus[col] == 0; + setColumnHidden(col, hideCol); + m_columnsHiddenStatus[col] = hideCol ? 1 : 0; +} + +void EasyTreeWidget::onModeChange(bool) +{ + auto action = qobject_cast(sender()); + if (action == nullptr) + return; + + const auto prev = m_mode; + m_mode = static_cast(action->data().toUInt()); + + if (m_mode == prev) + return; + + if (m_mode == EasyTreeMode_Full) + { + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + setColumnHidden(i, m_columnsHiddenStatus[i] != 0); + } + else + { + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + setColumnHidden(i, m_columnsHiddenStatus[i] != 0 || !SIMPLIFIED_REGIME_COLUMNS[i]); + } + + emit EASY_GLOBALS.events.blocksTreeModeChanged(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidget::loadSettings() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("tree_widget"); + + auto val = settings.value("color_rows"); + if (!val.isNull()) + m_bColorRows = val.toBool(); + + val = settings.value("regime"); + if (!val.isNull()) + m_mode = static_cast(val.toUInt()); + + val = settings.value("columns"); + if (!val.isNull()) + { + auto byteArray = val.toByteArray(); + memcpy(m_columnsHiddenStatus, byteArray.constData(), ::std::min(sizeof(m_columnsHiddenStatus), (size_t)byteArray.size())); + } + + auto state = settings.value("headerState").toByteArray(); + if (!state.isEmpty()) + header()->restoreState(state); + + settings.endGroup(); +} + +void EasyTreeWidget::saveSettings() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("tree_widget"); + settings.setValue("color_rows", m_bColorRows); + settings.setValue("regime", static_cast(m_mode)); + settings.setValue("columns", QByteArray(m_columnsHiddenStatus, COL_COLUMNS_NUMBER)); + settings.setValue("headerState", header()->saveState()); + settings.endGroup(); +} + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +EasyHierarchyWidget::EasyHierarchyWidget(QWidget* _parent) : Parent(_parent) + , m_tree(new EasyTreeWidget(this)) + , m_searchBox(new QLineEdit(this)) + , m_foundNumber(new QLabel("Found 0 matches", this)) + , m_searchButton(nullptr) + , m_bCaseSensitiveSearch(false) +{ + loadSettings(); + + m_searchBox->setFixedWidth(200); + m_searchBox->setContentsMargins(5, 0, 0, 0); + + QMenu* menu = new QMenu(this); + m_searchButton = menu->menuAction(); + m_searchButton->setText("Find next"); + m_searchButton->setIcon(QIcon(":/Search-next")); + m_searchButton->setData(true); + connect(m_searchButton, &QAction::triggered, this, &This::findNext); + + auto actionGroup = new QActionGroup(this); + actionGroup->setExclusive(true); + + auto a = new QAction(tr("Find next"), actionGroup); + a->setCheckable(true); + a->setChecked(true); + connect(a, &QAction::triggered, this, &This::findNextFromMenu); + menu->addAction(a); + + a = new QAction(tr("Find previous"), actionGroup); + a->setCheckable(true); + connect(a, &QAction::triggered, this, &This::findPrevFromMenu); + menu->addAction(a); + + menu->addSeparator(); + a = menu->addAction("Case sensitive"); + a->setCheckable(true); + a->setChecked(m_bCaseSensitiveSearch); + connect(a, &QAction::triggered, [this](bool _checked){ m_bCaseSensitiveSearch = _checked; }); + menu->addAction(a); + + auto tb = new QToolBar(this); + tb->setIconSize(::profiler_gui::ICONS_SIZE); + tb->setContentsMargins(0, 0, 0, 0); + tb->addAction(m_searchButton); + tb->addWidget(m_searchBox); + + auto searchbox = new QHBoxLayout(); + searchbox->setContentsMargins(0, 0, 5, 0); + searchbox->addWidget(tb); + searchbox->addStretch(100); + searchbox->addWidget(m_foundNumber, Qt::AlignRight); + + auto lay = new QVBoxLayout(this); + lay->setContentsMargins(1, 1, 1, 1); + lay->addLayout(searchbox); + lay->addWidget(m_tree); + + connect(m_searchBox, &QLineEdit::returnPressed, this, &This::onSeachBoxReturnPressed); +} + +EasyHierarchyWidget::~EasyHierarchyWidget() +{ + saveSettings(); +} + +void EasyHierarchyWidget::loadSettings() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("EasyHierarchyWidget"); + + auto val = settings.value("case_sensitive"); + if (!val.isNull()) + m_bCaseSensitiveSearch = val.toBool(); + + settings.endGroup(); +} + +void EasyHierarchyWidget::saveSettings() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("EasyHierarchyWidget"); + settings.setValue("case_sensitive", m_bCaseSensitiveSearch); + settings.endGroup(); +} + +void EasyHierarchyWidget::keyPressEvent(QKeyEvent* _event) +{ + if (_event->key() == Qt::Key_F3) + { + if (_event->modifiers() & Qt::ShiftModifier) + findPrev(true); + else + findNext(true); + } + + _event->accept(); +} + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void EasyHierarchyWidget::contextMenuEvent(QContextMenuEvent* _event) +{ + m_tree->contextMenuEvent(_event); +} + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +EasyTreeWidget* EasyHierarchyWidget::tree() +{ + return m_tree; +} + +void EasyHierarchyWidget::clear(bool _global) +{ + m_tree->clearSilent(_global); + m_foundNumber->setText(QString("Found 0 matches")); +} + +void EasyHierarchyWidget::onSeachBoxReturnPressed() +{ + if (m_searchButton->data().toBool() == true) + findNext(true); + else + findPrev(true); +} + +void EasyHierarchyWidget::findNext(bool) +{ + auto matches = m_tree->findNext(m_searchBox->text(), m_bCaseSensitiveSearch ? Qt::MatchCaseSensitive : Qt::MatchFlags()); + + if (matches == 1) + m_foundNumber->setText(QString("Found 1 match")); + else + m_foundNumber->setText(QString("Found %1 matches").arg(matches)); +} + +void EasyHierarchyWidget::findPrev(bool) +{ + auto matches = m_tree->findPrev(m_searchBox->text(), m_bCaseSensitiveSearch ? Qt::MatchCaseSensitive : Qt::MatchFlags()); + + if (matches == 1) + m_foundNumber->setText(QString("Found 1 match")); + else + m_foundNumber->setText(QString("Found %1 matches").arg(matches)); +} + +void EasyHierarchyWidget::findNextFromMenu(bool _checked) +{ + if (!_checked) + return; + + if (m_searchButton->data().toBool() == false) + { + m_searchButton->setData(true); + m_searchButton->setText(tr("Find next")); + m_searchButton->setIcon(QIcon(":/Search-next")); + disconnect(m_searchButton, &QAction::triggered, this, &This::findPrev); + connect(m_searchButton, &QAction::triggered, this, &This::findNext); + } + + findNext(true); +} + +void EasyHierarchyWidget::findPrevFromMenu(bool _checked) +{ + if (!_checked) + return; + + if (m_searchButton->data().toBool() == true) + { + m_searchButton->setData(false); + m_searchButton->setText(tr("Find prev")); + m_searchButton->setIcon(QIcon(":/Search-prev")); + disconnect(m_searchButton, &QAction::triggered, this, &This::findNext); + connect(m_searchButton, &QAction::triggered, this, &This::findPrev); + } + + findPrev(true); +} + +////////////////////////////////////////////////////////////////////////// diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/blocks_tree_widget.h b/Source/ThirdParty/easy_profiler/profiler_gui/blocks_tree_widget.h new file mode 100644 index 0000000000..a847269bae --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/blocks_tree_widget.h @@ -0,0 +1,217 @@ +/************************************************************************ +* file name : blocks_tree_widget.h +* ----------------- : +* creation time : 2016/06/26 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of EasyTreeWidget and it's auxiliary classes +* : for displyaing EasyProfiler blocks tree. +* ----------------- : +* change log : * 2016/06/26 Victor Zarubkin: moved sources from tree_view.h +* : and renamed classes from My* to Prof*. +* : +* : * 2016/06/27 Victor Zarubkin: Added possibility to colorize rows +* : with profiler blocks' colors. +* : +* : * 2016/06/29 Victor Zarubkin: Added clearSilent() method. +* : +* : * 2016/08/18 Victor Zarubkin: Added loading blocks hierarchy in separate thread; +* : Moved sources of TreeWidgetItem into tree_widget_item.h/.cpp +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_TREE_WIDGET_H +#define EASY_TREE_WIDGET_H + +#include +#include + +#include "tree_widget_loader.h" +#include "tree_widget_item.h" + +#include + +////////////////////////////////////////////////////////////////////////// + +class EasyTreeWidget : public QTreeWidget +{ + Q_OBJECT + + typedef QTreeWidget Parent; + typedef EasyTreeWidget This; + +protected: + + EasyTreeWidgetLoader m_hierarchyBuilder; + Items m_items; + RootsMap m_roots; + ::profiler_gui::TreeBlocks m_inputBlocks; + QTimer m_fillTimer; + QString m_lastSearch; + QTreeWidgetItem* m_lastFound; + ::profiler::timestamp_t m_beginTime; + class QProgressDialog* m_progress; + class QLabel* m_hintLabel; + EasyTreeMode m_mode; + bool m_bColorRows; + bool m_bLocked; + bool m_bSilentExpandCollapse; + char m_columnsHiddenStatus[COL_COLUMNS_NUMBER]; + +public: + + explicit EasyTreeWidget(QWidget* _parent = nullptr); + virtual ~EasyTreeWidget(); + + void contextMenuEvent(QContextMenuEvent* _event) override; + + void clearSilent(bool _global = false); + int findNext(const QString& _str, Qt::MatchFlags _flags); + int findPrev(const QString& _str, Qt::MatchFlags _flags); + +public slots: + + void setTree(const unsigned int _blocksNumber, const ::profiler::thread_blocks_tree_t& _blocksTree); + + void setTreeBlocks(const ::profiler_gui::TreeBlocks& _blocks, ::profiler::timestamp_t _session_begin_time, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict); + +protected: + + void resizeEvent(QResizeEvent* _event) override; + void moveEvent(QMoveEvent* _event) override; + +private slots: + + void onJumpToItemClicked(bool); + + void onCollapseAllClicked(bool); + + void onExpandAllClicked(bool); + + void onCollapseAllChildrenClicked(bool); + + void onExpandAllChildrenClicked(bool); + + void onItemExpand(QTreeWidgetItem* _item); + void onItemCollapse(QTreeWidgetItem* _item); + void onCurrentItemChange(QTreeWidgetItem* _item, QTreeWidgetItem*); + + void onColorizeRowsTriggered(bool _colorize); + + void onSelectedThreadChange(::profiler::thread_id_t _id); + + void onSelectedBlockChange(uint32_t _block_index); + + void onBlockStatusChangeClicked(bool); + + void resizeColumnsToContents(); + + void onHideShowColumn(bool); + void onModeChange(bool); + + void onFillTimerTimeout(); + +protected: + + void loadSettings(); + void saveSettings(); + void alignProgressBar(); + +}; // END of class EasyTreeWidget. + +////////////////////////////////////////////////////////////////////////// + +class EasyHierarchyWidget : public QWidget +{ + Q_OBJECT + + typedef QWidget Parent; + typedef EasyHierarchyWidget This; + +private: + + EasyTreeWidget* m_tree; + class QLineEdit* m_searchBox; + class QLabel* m_foundNumber; + class QAction* m_searchButton; + bool m_bCaseSensitiveSearch; + +public: + + // Public virtual methods + + explicit EasyHierarchyWidget(QWidget* _parent = nullptr); + virtual ~EasyHierarchyWidget(); + void keyPressEvent(QKeyEvent* _event) override; + void contextMenuEvent(QContextMenuEvent* _event) override; + +public: + + // Public non-virtual methods + + EasyTreeWidget* tree(); + void clear(bool _global = false); + +private slots: + + // Private slots + + void onSeachBoxReturnPressed(); + void findNext(bool); + void findPrev(bool); + void findNextFromMenu(bool); + void findPrevFromMenu(bool); + +private: + + // Private non-virtual methods + + void loadSettings(); + void saveSettings(); + +}; // END of class EasyHierarchyWidget. + + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_TREE_WIDGET_H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/common_types.h b/Source/ThirdParty/easy_profiler/profiler_gui/common_types.h new file mode 100644 index 0000000000..a2fd9ba535 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/common_types.h @@ -0,0 +1,454 @@ +/************************************************************************ +* file name : common_types.h +* ----------------- : +* creation time : 2016/07/31 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of common types for both GraphicsView +* : and TreeWidget. +* ----------------- : +* change log : * 2016/07/31 Victor Zarubkin: initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_PROFILER__GUI_COMMON_TYPES_H +#define EASY_PROFILER__GUI_COMMON_TYPES_H + +#include +#include +#include +#include +#include +#include +#include + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#define PROF_MICROSECONDS(timestamp) ((timestamp) * 1e-3) +//#define PROF_MICROSECONDS(timestamp) (timestamp) + +#define PROF_FROM_MICROSECONDS(to_timestamp) ((to_timestamp) * 1e3) +//#define PROF_FROM_MICROSECONDS(to_timestamp) (to_timestamp) + +#define PROF_MILLISECONDS(timestamp) ((timestamp) * 1e-6) +//#define PROF_MILLISECONDS(timestamp) ((timestamp) * 1e-3) + +#define PROF_FROM_MILLISECONDS(to_timestamp) ((to_timestamp) * 1e6) +//#define PROF_FROM_MILLISECONDS(to_timestamp) ((to_timestamp) * 1e3) + +#define PROF_NANOSECONDS(timestamp) (timestamp) +//#define PROF_NANOSECONDS(timestamp) ((timestamp) * 1000) + +////////////////////////////////////////////////////////////////////////// + +inline qreal units2microseconds(qreal _value) +{ + return _value; + //return _value * 1e3; +} + +inline qreal microseconds2units(qreal _value) +{ + return _value; + //return _value * 1e-3; +} + +////////////////////////////////////////////////////////////////////////// + +namespace profiler_gui { + +inline QRgb toRgb(uint32_t _red, uint32_t _green, uint32_t _blue) +{ + return (_red << 16) + (_green << 8) + _blue; +} + +inline QRgb fromProfilerRgb(uint32_t _red, uint32_t _green, uint32_t _blue) +{ + if (_red == 0 && _green == 0 && _blue == 0) + return ::profiler::colors::Default; + return toRgb(_red, _green, _blue) | 0x00141414; +} + +inline bool isLightColor(::profiler::color_t _color) +{ + const auto sum = 255. - (((_color & 0x00ff0000) >> 16) * 0.299 + ((_color & 0x0000ff00) >> 8) * 0.587 + (_color & 0x000000ff) * 0.114); + return sum < 76.5 || ((_color & 0xff000000) >> 24) < 0x80; +} + +inline bool isLightColor(::profiler::color_t _color, qreal _maxSum) +{ + const auto sum = 255. - (((_color & 0x00ff0000) >> 16) * 0.299 + ((_color & 0x0000ff00) >> 8) * 0.587 + (_color & 0x000000ff) * 0.114); + return sum < _maxSum || ((_color & 0xff000000) >> 24) < 0x80; +} + +inline ::profiler::color_t textColorForFlag(bool _is_light) +{ + return _is_light ? ::profiler::colors::Dark : ::profiler::colors::CreamWhite; +} + +inline ::profiler::color_t textColorForRgb(::profiler::color_t _color) +{ + return isLightColor(_color) ? ::profiler::colors::Dark : ::profiler::colors::CreamWhite; +} + +////////////////////////////////////////////////////////////////////////// + +#define EASY_GRAPHICS_ITEM_RECURSIVE_PAINT +//#undef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + +#pragma pack(push, 1) +struct EasyBlockItem Q_DECL_FINAL +{ + qreal x; ///< x coordinate of the item (this is made qreal=double to avoid mistakes on very wide scene) + float w; ///< Width of the item + ::profiler::block_index_t block; ///< Index of profiler block + +#ifndef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + ::profiler::block_index_t neighbours; ///< Number of neighbours (parent.children.size()) + uint32_t children_begin; ///< Index of first child item on the next sublevel + int8_t state; ///< 0 = no change, 1 = paint, -1 = do not paint +#else + ::profiler::block_index_t max_depth_child; ///< Index of child with maximum tree depth + uint32_t children_begin; ///< Index of first child item on the next sublevel +#endif + + // Possible optimizations: + // 1) We can save 1 more byte per block if we will use char instead of short + real time calculations for "totalHeight" var; + // 2) We can save 12 bytes per block if "x" and "w" vars will be removed (all this information exist inside BlocksTree), + // but this requires runtime x-coodinate calculation because BlocksTree has x value in nanoseconds. + + inline void setPos(qreal _x, float _w) { x = _x; w = _w; } + inline qreal left() const { return x; } + inline qreal right() const { return x + w; } + inline float width() const { return w; } + +}; // END of struct EasyBlockItem. + +//#define EASY_TREE_WIDGET__USE_VECTOR +struct EasyBlock Q_DECL_FINAL +{ + ::profiler::BlocksTree tree; +#ifdef EASY_TREE_WIDGET__USE_VECTOR + uint32_t tree_item; +#endif + uint32_t graphics_item_index; + uint8_t graphics_item_level; + uint8_t graphics_item; + bool expanded; + + EasyBlock() = default; + + EasyBlock(EasyBlock&& that) + : tree(::std::move(that.tree)) +#ifdef EASY_TREE_WIDGET__USE_VECTOR + , tree_item(that.tree_item) +#endif + , graphics_item_index(that.graphics_item_index) + , graphics_item_level(that.graphics_item_level) + , graphics_item(that.graphics_item) + , expanded(that.expanded) + { + } + +private: + + EasyBlock(const EasyBlock&) = delete; +}; +#pragma pack(pop) + +typedef ::std::vector EasyItems; +typedef ::std::vector EasyBlocks; + +////////////////////////////////////////////////////////////////////////// + +struct EasySelectedBlock Q_DECL_FINAL +{ + const ::profiler::BlocksTreeRoot* root; + ::profiler::block_index_t tree; + + EasySelectedBlock() : root(nullptr), tree(0xffffffff) + { + } + + EasySelectedBlock(const ::profiler::BlocksTreeRoot* _root, const ::profiler::block_index_t _tree) + : root(_root) + , tree(_tree) + { + } + +}; // END of struct EasySelectedBlock. + +typedef ::std::vector TreeBlocks; + +////////////////////////////////////////////////////////////////////////// + +enum TimeUnits : int8_t +{ + TimeUnits_ms = 0, + TimeUnits_us, + TimeUnits_ns, + TimeUnits_auto + +}; // END of enum TimeUnits. + +inline qreal timeFactor(qreal _interval) +{ + if (_interval < 1) // interval in nanoseconds + return 1e3; + + if (_interval < 1e3) // interval in microseconds + return 1; + + if (_interval < 1e6) // interval in milliseconds + return 1e-3; + + // interval in seconds + return 1e-6; +} + +inline QString autoTimeStringReal(qreal _interval, int _precision = 1) +{ + if (_interval < 1) // interval in nanoseconds + return QString("%1 ns").arg(static_cast(_interval * 1e3)); + + if (_interval < 1e3) // interval in microseconds + return QString("%1 us").arg(_interval, 0, 'f', _precision); + + if (_interval < 1e6) // interval in milliseconds + return QString("%1 ms").arg(_interval * 1e-3, 0, 'f', _precision); + + // interval in seconds + return QString("%1 s").arg(_interval * 1e-6, 0, 'f', _precision); +} + +inline QString autoTimeStringInt(qreal _interval) +{ + if (_interval < 1) // interval in nanoseconds + return QString("%1 ns").arg(static_cast(_interval * 1e3)); + + if (_interval < 1e3) // interval in microseconds + return QString("%1 us").arg(static_cast(_interval)); + + if (_interval < 1e6) // interval in milliseconds + return QString("%1 ms").arg(static_cast(_interval * 1e-3)); + + // interval in seconds + return QString("%1 s").arg(static_cast(_interval * 1e-6)); +} + +inline QString autoTimeStringRealNs(::profiler::timestamp_t _interval, int _precision = 1) +{ + if (_interval < 1000) // interval in nanoseconds + return QString("%1 ns").arg(_interval); + + if (_interval < 1000000) // interval in microseconds + return QString("%1 us").arg(_interval * 1e-3, 0, 'f', _precision); + + if (_interval < 1000000000U) // interval in milliseconds + return QString("%1 ms").arg(_interval * 1e-6, 0, 'f', _precision); + + // interval in seconds + return QString("%1 s").arg(_interval * 1e-9, 0, 'f', _precision); +} + +inline QString autoTimeStringIntNs(::profiler::timestamp_t _interval) +{ + if (_interval < 1000) // interval in nanoseconds + return QString("%1 ns").arg(_interval); + + if (_interval < 1000000) // interval in microseconds + return QString("%1 us").arg(static_cast(_interval * 1e-3)); + + if (_interval < 1000000000U) // interval in milliseconds + return QString("%1 ms").arg(static_cast(_interval * 1e-6)); + + // interval in seconds + return QString("%1 s").arg(static_cast(_interval * 1e-9)); +} + +inline QString timeStringReal(TimeUnits _units, qreal _interval, int _precision = 1) +{ + switch (_units) + { + case TimeUnits_ms:{ + const char fmt = _interval <= 1 ? 'g' : 'f'; + return QString("%1 ms").arg(_interval * 1e-3, 0, fmt, _precision); + } + + case TimeUnits_us: + return QString("%1 us").arg(_interval, 0, 'f', _precision); + + case TimeUnits_ns: + return QString("%1 ns").arg(static_cast(_interval * 1e3)); + + case TimeUnits_auto: + default: + return autoTimeStringReal(_interval, _precision); + } + + return QString(); +} + +inline QString timeStringRealNs(TimeUnits _units, ::profiler::timestamp_t _interval, int _precision = 1) +{ + switch (_units) + { + case TimeUnits_ms:{ + const char fmt = _interval <= 1000 ? 'g' : 'f'; + return QString("%1 ms").arg(_interval * 1e-6, 0, fmt, _precision); + } + + case TimeUnits_us: + return QString("%1 us").arg(_interval * 1e-3, 0, 'f', _precision); + + case TimeUnits_ns: + return QString("%1 ns").arg(_interval); + + case TimeUnits_auto: + default: + return autoTimeStringRealNs(_interval, _precision); + } + + return QString(); +} + +inline QString timeStringInt(TimeUnits _units, qreal _interval) +{ + switch (_units) + { + case TimeUnits_ms: + return QString("%1 ms").arg(static_cast(_interval * 1e-3)); + + case TimeUnits_us: + return QString("%1 us").arg(static_cast(_interval)); + + case TimeUnits_ns: + return QString("%1 ns").arg(static_cast(_interval * 1e3)); + + case TimeUnits_auto: + default: + return autoTimeStringInt(_interval); + } + + return QString(); +} + +inline QString timeStringIntNs(TimeUnits _units, ::profiler::timestamp_t _interval) +{ + switch (_units) + { + case TimeUnits_ms: + return QString("%1 ms").arg(static_cast(_interval * 1e-6)); + + case TimeUnits_us: + return QString("%1 us").arg(static_cast(_interval * 1e-3)); + + case TimeUnits_ns: + return QString("%1 ns").arg(_interval); + + case TimeUnits_auto: + default: + return autoTimeStringIntNs(_interval); + } + + return QString(); +} + +////////////////////////////////////////////////////////////////////////// + +template inline T numeric_max() { + return ::std::numeric_limits::max(); +} + +template inline T numeric_max(T) { + return ::std::numeric_limits::max(); +} + +template inline void set_max(T& _value) { + _value = ::std::numeric_limits::max(); +} + +template inline bool is_max(const T& _value) { + return _value == ::std::numeric_limits::max(); +} + +////////////////////////////////////////////////////////////////////////// + +inline double percentReal(::profiler::timestamp_t _partial, ::profiler::timestamp_t _total) +{ + return _total ? 100. * static_cast(_partial) / static_cast(_total) : 0.; +} + +inline int percent(::profiler::timestamp_t _partial, ::profiler::timestamp_t _total) +{ + return static_cast(0.5 + percentReal(_partial, _total)); +} + +////////////////////////////////////////////////////////////////////////// + +inline QFont EFont(QFont::StyleHint _hint, const char* _family, int _size, int _weight = -1) +{ + QFont f; + f.setStyleHint(_hint, QFont::PreferMatch); + f.setFamily(_family); + f.setPointSize(_size); + f.setWeight(_weight); + return f; +} + +inline QFont EFont(const char* _family, int _size, int _weight = -1) +{ + return EFont(QFont::Helvetica, _family, _size, _weight); +} + +////////////////////////////////////////////////////////////////////////// + +} // END of namespace profiler_gui. + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_PROFILER__GUI_COMMON_TYPES_H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/descriptors_tree_widget.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/descriptors_tree_widget.cpp new file mode 100644 index 0000000000..a152924708 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/descriptors_tree_widget.cpp @@ -0,0 +1,919 @@ +/************************************************************************ +* file name : descriptors_tree_widget.cpp +* ----------------- : +* creation time : 2016/09/17 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of EasyDescTreeWidget and it's auxiliary classes +* : for displyaing EasyProfiler blocks descriptors tree. +* ----------------- : +* change log : * 2016/09/17 Victor Zarubkin: initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "descriptors_tree_widget.h" +#include "globals.h" + +#ifdef _WIN32 +#include + +#ifdef __MINGW32__ +#include +#endif + +#endif + +#ifdef max +#undef max +#endif + +#ifdef min +#undef min +#endif + +////////////////////////////////////////////////////////////////////////// + +enum DescColumns +{ + DESC_COL_FILE_LINE = 0, + DESC_COL_TYPE, + DESC_COL_NAME, + DESC_COL_STATUS, + + DESC_COL_COLUMNS_NUMBER +}; + +////////////////////////////////////////////////////////////////////////// + +::profiler::EasyBlockStatus nextStatus(::profiler::EasyBlockStatus _status) +{ + switch (_status) + { + case ::profiler::OFF: + return ::profiler::ON; + + case ::profiler::ON: + return ::profiler::FORCE_ON; + + case ::profiler::FORCE_ON: + return ::profiler::OFF_RECURSIVE; + + case ::profiler::OFF_RECURSIVE: + return ::profiler::ON_WITHOUT_CHILDREN; + + case ::profiler::ON_WITHOUT_CHILDREN: + return ::profiler::FORCE_ON_WITHOUT_CHILDREN; + + case ::profiler::FORCE_ON_WITHOUT_CHILDREN: + return ::profiler::OFF; + } + + return ::profiler::OFF; +} + +const char* statusText(::profiler::EasyBlockStatus _status) +{ + switch (_status) + { + case ::profiler::OFF: + return "OFF"; + + case ::profiler::ON: + return "ON"; + + case ::profiler::FORCE_ON: + return "FORCE_ON"; + + case ::profiler::OFF_RECURSIVE: + return "OFF_RECURSIVE"; + + case ::profiler::ON_WITHOUT_CHILDREN: + return "ON_WITHOUT_CHILDREN"; + + case ::profiler::FORCE_ON_WITHOUT_CHILDREN: + return "FORCE_ON_WITHOUT_CHILDREN"; + } + + return ""; +} + +::profiler::color_t statusColor(::profiler::EasyBlockStatus _status) +{ + switch (_status) + { + case ::profiler::OFF: + return ::profiler::colors::Red900; + + case ::profiler::ON: + return ::profiler::colors::LightGreen900; + + case ::profiler::FORCE_ON: + return ::profiler::colors::LightGreen900; + + case ::profiler::OFF_RECURSIVE: + return ::profiler::colors::Red900; + + case ::profiler::ON_WITHOUT_CHILDREN: + return ::profiler::colors::Lime900; + + case ::profiler::FORCE_ON_WITHOUT_CHILDREN: + return ::profiler::colors::Lime900; + } + + return ::profiler::colors::Black; +} + +////////////////////////////////////////////////////////////////////////// + +EasyDescWidgetItem::EasyDescWidgetItem(::profiler::block_id_t _desc, Parent* _parent) : Parent(_parent), m_desc(_desc) +{ + +} + +EasyDescWidgetItem::~EasyDescWidgetItem() +{ + +} + +bool EasyDescWidgetItem::operator < (const Parent& _other) const +{ + const auto col = treeWidget()->sortColumn(); + + switch (col) + { + case DESC_COL_FILE_LINE: + { + if (parent() != nullptr) + return data(col, Qt::UserRole).toInt() < _other.data(col, Qt::UserRole).toInt(); + } + } + + return Parent::operator < (_other); +} + +////////////////////////////////////////////////////////////////////////// + +EasyDescTreeWidget::EasyDescTreeWidget(QWidget* _parent) + : Parent(_parent) + , m_lastFound(nullptr) + , m_lastSearchColumn(-1) + , m_searchColumn(DESC_COL_NAME) + , m_bLocked(false) +{ + setAutoFillBackground(false); + setAlternatingRowColors(true); + setItemsExpandable(true); + setAnimated(true); + setSortingEnabled(false); + setColumnCount(DESC_COL_COLUMNS_NUMBER); + + auto header_item = new QTreeWidgetItem(); + header_item->setText(DESC_COL_FILE_LINE, "File/Line"); + header_item->setText(DESC_COL_TYPE, "Type"); + header_item->setText(DESC_COL_NAME, "Name"); + header_item->setText(DESC_COL_STATUS, "Status"); + setHeaderItem(header_item); + + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::selectedBlockChanged, this, &This::onSelectedBlockChange); + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::blockStatusChanged, this, &This::onBlockStatusChange); + connect(this, &Parent::itemExpanded, this, &This::onItemExpand); + connect(this, &Parent::itemDoubleClicked, this, &This::onDoubleClick); + connect(this, &Parent::currentItemChanged, this, &This::onCurrentItemChange); + + loadSettings(); +} + +EasyDescTreeWidget::~EasyDescTreeWidget() +{ + if (::profiler_gui::is_max(EASY_GLOBALS.selected_block) && !::profiler_gui::is_max(EASY_GLOBALS.selected_block_id)) + { + ::profiler_gui::set_max(EASY_GLOBALS.selected_block_id); + emit EASY_GLOBALS.events.refreshRequired(); + } + + saveSettings(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyDescTreeWidget::contextMenuEvent(QContextMenuEvent* _event) +{ + _event->accept(); + + QMenu menu; + menu.setToolTipsVisible(true); + auto action = menu.addAction("Expand all"); + SET_ICON(action, ":/Expand"); + connect(action, &QAction::triggered, this, &This::expandAll); + + action = menu.addAction("Collapse all"); + SET_ICON(action, ":/Collapse"); + connect(action, &QAction::triggered, this, &This::collapseAll); + + menu.addSeparator(); + auto submenu = menu.addMenu("Search by"); + auto header_item = headerItem(); + for (int i = 0; i < DESC_COL_STATUS; ++i) + { + if (i == DESC_COL_TYPE) + continue; + + action = submenu->addAction(header_item->text(i)); + action->setData(i); + action->setCheckable(true); + if (i == m_searchColumn) + action->setChecked(true); + connect(action, &QAction::triggered, this, &This::onSearchColumnChange); + } + + auto item = currentItem(); + if (item != nullptr && item->parent() != nullptr && currentColumn() >= DESC_COL_TYPE) + { + const auto& desc = easyDescriptor(static_cast(item)->desc()); + + menu.addSeparator(); + auto submenu = menu.addMenu("Change status"); + submenu->setToolTipsVisible(true); + +#define ADD_STATUS_ACTION(NameValue, StatusValue, ToolTipValue)\ + action = submenu->addAction(NameValue);\ + action->setCheckable(true);\ + action->setChecked(desc.status() == StatusValue);\ + action->setData(static_cast(StatusValue));\ + action->setToolTip(ToolTipValue);\ + connect(action, &QAction::triggered, this, &This::onBlockStatusChangeClicked) + + ADD_STATUS_ACTION("Off", ::profiler::OFF, "Do not profile this block."); + ADD_STATUS_ACTION("On", ::profiler::ON, "Profile this block\nif parent enabled children."); + ADD_STATUS_ACTION("Force-On", ::profiler::FORCE_ON, "Always profile this block even\nif it's parent disabled children."); + ADD_STATUS_ACTION("Off-recursive", ::profiler::OFF_RECURSIVE, "Do not profile neither this block\nnor it's children."); + ADD_STATUS_ACTION("On-without-children", ::profiler::ON_WITHOUT_CHILDREN, "Profile this block, but\ndo not profile it's children."); + ADD_STATUS_ACTION("Force-On-without-children", ::profiler::FORCE_ON_WITHOUT_CHILDREN, "Always profile this block, but\ndo not profile it's children."); +#undef ADD_STATUS_ACTION + + submenu->setEnabled(EASY_GLOBALS.connected); + if (!EASY_GLOBALS.connected) + submenu->setTitle(QString("%1 (connection needed)").arg(submenu->title())); + } + + menu.exec(QCursor::pos()); +} + +void EasyDescTreeWidget::onSearchColumnChange(bool) +{ + auto action = qobject_cast(sender()); + if (action != nullptr) + m_searchColumn = action->data().toInt(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyDescTreeWidget::clearSilent(bool _global) +{ + const QSignalBlocker b(this); + + setSortingEnabled(false); + m_lastFound = nullptr; + m_lastSearch.clear(); + + m_highlightItems.clear(); + m_items.clear(); + + ::std::vector topLevelItems; + topLevelItems.reserve(topLevelItemCount()); + for (int i = topLevelItemCount() - 1; i >= 0; --i) + { + const bool expanded = !_global && topLevelItem(i)->isExpanded(); + auto item = takeTopLevelItem(i); + if (expanded) + m_expandedFilesTemp.insert(item->text(DESC_COL_FILE_LINE).toStdString()); + topLevelItems.push_back(item); + } + + auto deleter_thread = ::std::thread([](decltype(topLevelItems) _items) + { +#ifdef _WIN32 + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST); +#endif + + for (auto item : _items) + delete item; + + }, ::std::move(topLevelItems)); + + deleter_thread.detach(); + + //clear(); +} + +////////////////////////////////////////////////////////////////////////// + +struct FileItems +{ + typedef ::std::unordered_map > Items; + Items children; + QTreeWidgetItem* item = nullptr; +}; + +void EasyDescTreeWidget::build() +{ + auto f = font(); + f.setBold(true); + + typedef ::std::unordered_map<::std::string, FileItems> Files; + Files fileItems; + + m_items.resize(EASY_GLOBALS.descriptors.size()); + memset(m_items.data(), 0, sizeof(void*) * m_items.size()); + + const QSignalBlocker b(this); + ::profiler::block_id_t id = 0; + for (auto desc : EASY_GLOBALS.descriptors) + { + if (desc != nullptr) + { + auto& p = fileItems[desc->file()]; + if (p.item == nullptr) + { + p.item = new QTreeWidgetItem(); + p.item->setText(DESC_COL_FILE_LINE, QString(desc->file()).remove(QRegExp("^(\\.{2}\\\\+|\\/+)+"))); + p.item->setText(DESC_COL_TYPE, "F"); + p.item->setToolTip(DESC_COL_TYPE, "File"); + } + + auto it = p.children.find(desc->line()); + if (it == p.children.end()) + { + auto item = new EasyDescWidgetItem(desc->id(), p.item); + item->setText(DESC_COL_FILE_LINE, QString::number(desc->line())); + item->setData(DESC_COL_FILE_LINE, Qt::UserRole, desc->line()); + item->setText(DESC_COL_NAME, desc->name()); + + if (desc->type() == ::profiler::BLOCK_TYPE_BLOCK) + { + item->setText(DESC_COL_TYPE, "B"); + item->setToolTip(DESC_COL_TYPE, "Block"); + } + else + { + item->setText(DESC_COL_TYPE, "E"); + item->setToolTip(DESC_COL_TYPE, "Event"); + } + + item->setFont(DESC_COL_STATUS, f); + item->setText(DESC_COL_STATUS, statusText(desc->status())); + item->setForeground(DESC_COL_STATUS, QColor::fromRgba(statusColor(desc->status()))); + + m_items[id] = item; + p.children.insert(::std::make_pair(desc->line(), item)); + } + else + { + m_items[id] = it->second; + } + } + + ++id; + } + + for (auto& p : fileItems) + { + addTopLevelItem(p.second.item); + if (m_expandedFilesTemp.find(p.first) != m_expandedFilesTemp.end()) + p.second.item->setExpanded(true); + } + + m_expandedFilesTemp.clear(); + setSortingEnabled(true); + sortByColumn(DESC_COL_FILE_LINE, Qt::AscendingOrder); + resizeColumnsToContents(); + QTimer::singleShot(100, [this](){ onSelectedBlockChange(EASY_GLOBALS.selected_block); }); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyDescTreeWidget::onItemExpand(QTreeWidgetItem*) +{ + resizeColumnsToContents(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyDescTreeWidget::onDoubleClick(QTreeWidgetItem* _item, int _column) +{ + if (!EASY_GLOBALS.connected) + return; + + if (_column >= DESC_COL_TYPE && _item->parent() != nullptr) + { + auto item = static_cast(_item); + auto& desc = easyDescriptor(item->desc()); + desc.setStatus(nextStatus(desc.status())); + + item->setText(DESC_COL_STATUS, statusText(desc.status())); + item->setForeground(DESC_COL_STATUS, QColor::fromRgba(statusColor(desc.status()))); + + m_bLocked = true; + emit EASY_GLOBALS.events.blockStatusChanged(desc.id(), desc.status()); + m_bLocked = false; + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyDescTreeWidget::onCurrentItemChange(QTreeWidgetItem* _item, QTreeWidgetItem* _prev) +{ + if (_prev != nullptr) + { + auto f = font(); + for (int i = 0; i < DESC_COL_STATUS; ++i) + _prev->setFont(i, f); + } + + if (_item != nullptr) + { + auto f = font(); + f.setBold(true); + for (int i = 0; i < DESC_COL_STATUS; ++i) + _item->setFont(i, f); + + if (::profiler_gui::is_max(EASY_GLOBALS.selected_block) && _item->parent() != nullptr) + { + const auto id = static_cast(_item)->desc(); + if (EASY_GLOBALS.selected_block_id != id) + { + EASY_GLOBALS.selected_block_id = id; + emit EASY_GLOBALS.events.selectedBlockIdChanged(id); + } + } + } + else if (::profiler_gui::is_max(EASY_GLOBALS.selected_block) && !::profiler_gui::is_max(EASY_GLOBALS.selected_block_id)) + { + ::profiler_gui::set_max(EASY_GLOBALS.selected_block_id); + emit EASY_GLOBALS.events.selectedBlockIdChanged(EASY_GLOBALS.selected_block_id); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyDescTreeWidget::onBlockStatusChangeClicked(bool _checked) +{ + if (!_checked || !EASY_GLOBALS.connected) + return; + + auto item = currentItem(); + if (item == nullptr || item->parent() == nullptr) + return; + + auto action = qobject_cast(sender()); + if (action != nullptr) + { + auto& desc = easyDescriptor(static_cast(item)->desc()); + desc.setStatus(static_cast<::profiler::EasyBlockStatus>(action->data().toUInt())); + item->setText(DESC_COL_STATUS, statusText(desc.status())); + item->setForeground(DESC_COL_STATUS, QColor::fromRgba(statusColor(desc.status()))); + + m_bLocked = true; + emit EASY_GLOBALS.events.blockStatusChanged(desc.id(), desc.status()); + m_bLocked = false; + } +} + +void EasyDescTreeWidget::onBlockStatusChange(::profiler::block_id_t _id, ::profiler::EasyBlockStatus _status) +{ + if (m_bLocked) + return; + + auto item = m_items[_id]; + if (item == nullptr) + return; + + auto& desc = easyDescriptor(item->desc()); + item->setText(DESC_COL_STATUS, statusText(desc.status())); + item->setForeground(DESC_COL_STATUS, QColor::fromRgba(statusColor(desc.status()))); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyDescTreeWidget::resizeColumnsToContents() +{ + for (int i = 0; i < DESC_COL_COLUMNS_NUMBER; ++i) + resizeColumnToContents(i); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyDescTreeWidget::onSelectedBlockChange(uint32_t _block_index) +{ + if (::profiler_gui::is_max(_block_index)) + return; + + auto item = m_items[blocksTree(_block_index).node->id()]; + if (item == nullptr) + return; + + scrollToItem(item, QAbstractItemView::PositionAtCenter); + setCurrentItem(item); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyDescTreeWidget::resetHighlight() +{ + for (auto item : m_highlightItems) { + for (int i = 0; i < DESC_COL_COLUMNS_NUMBER; ++i) + item->setBackground(i, Qt::NoBrush); + } + m_highlightItems.clear(); +} + +void EasyDescTreeWidget::loadSettings() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("desc_tree_widget"); + + auto val = settings.value("searchColumn"); + if (!val.isNull()) + m_searchColumn = val.toInt(); + + settings.endGroup(); +} + +void EasyDescTreeWidget::saveSettings() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("desc_tree_widget"); + + settings.setValue("searchColumn", m_searchColumn); + + settings.endGroup(); +} + +////////////////////////////////////////////////////////////////////////// + +int EasyDescTreeWidget::findNext(const QString& _str, Qt::MatchFlags _flags) +{ + if (_str.isEmpty()) + { + resetHighlight(); + m_lastSearchColumn = m_searchColumn; + return 0; + } + + const bool isNewSearch = (m_lastSearchColumn != m_searchColumn || m_lastSearch != _str); + auto itemsList = findItems(_str, Qt::MatchContains | Qt::MatchRecursive | _flags, m_searchColumn); + + if (!isNewSearch) + { + if (!itemsList.empty()) + { + bool stop = false; + decltype(m_lastFound) next = nullptr; + for (auto item : itemsList) + { + if (stop) + { + next = item; + break; + } + + stop = item == m_lastFound; + } + + m_lastFound = next == nullptr ? itemsList.front() : next; + } + else + { + m_lastFound = nullptr; + } + } + else + { + resetHighlight(); + + m_lastSearchColumn = m_searchColumn; + m_lastSearch = _str; + m_lastFound = !itemsList.empty() ? itemsList.front() : nullptr; + + for (auto item : itemsList) + { + m_highlightItems.push_back(item); + for (int i = 0; i < DESC_COL_COLUMNS_NUMBER; ++i) + item->setBackgroundColor(i, QColor::fromRgba(0x80000000 | (0x00ffffff & ::profiler::colors::Yellow))); + } + } + + if (m_lastFound != nullptr) + { + scrollToItem(m_lastFound, QAbstractItemView::PositionAtCenter); + setCurrentItem(m_lastFound); + } + + return itemsList.size(); +} + +int EasyDescTreeWidget::findPrev(const QString& _str, Qt::MatchFlags _flags) +{ + if (_str.isEmpty()) + { + resetHighlight(); + m_lastSearchColumn = m_searchColumn; + return 0; + } + + const bool isNewSearch = (m_lastSearchColumn != m_searchColumn || m_lastSearch != _str); + auto itemsList = findItems(_str, Qt::MatchContains | Qt::MatchRecursive | _flags, m_searchColumn); + + if (!isNewSearch) + { + if (!itemsList.empty()) + { + decltype(m_lastFound) prev = nullptr; + for (auto item : itemsList) + { + if (item == m_lastFound) + break; + + prev = item; + } + + m_lastFound = prev == nullptr ? itemsList.back() : prev; + } + else + { + m_lastFound = nullptr; + } + } + else + { + resetHighlight(); + + m_lastSearchColumn = m_searchColumn; + m_lastSearch = _str; + m_lastFound = !itemsList.empty() ? itemsList.front() : nullptr; + + m_highlightItems.reserve(itemsList.size()); + for (auto item : itemsList) + { + m_highlightItems.push_back(item); + for (int i = 0; i < DESC_COL_COLUMNS_NUMBER; ++i) + item->setBackgroundColor(i, QColor::fromRgba(0x80000000 | (0x00ffffff & ::profiler::colors::Yellow))); + } + } + + if (m_lastFound != nullptr) + { + scrollToItem(m_lastFound, QAbstractItemView::PositionAtCenter); + setCurrentItem(m_lastFound); + } + + return itemsList.size(); +} + +////////////////////////////////////////////////////////////////////////// + +EasyDescWidget::EasyDescWidget(QWidget* _parent) : Parent(_parent) + , m_tree(new EasyDescTreeWidget(this)) + , m_searchBox(new QLineEdit(this)) + , m_foundNumber(new QLabel("Found 0 matches", this)) + , m_searchButton(nullptr) + , m_bCaseSensitiveSearch(false) +{ + loadSettings(); + + m_searchBox->setFixedWidth(200); + m_searchBox->setContentsMargins(5, 0, 0, 0); + + auto tb = new QToolBar(this); + tb->setIconSize(::profiler_gui::ICONS_SIZE); + auto refreshButton = tb->addAction(QIcon(":/Reload"), tr("Refresh blocks list")); + refreshButton->setEnabled(EASY_GLOBALS.connected); + refreshButton->setToolTip(tr("Refresh blocks list.\nConnection needed.")); + connect(refreshButton, &QAction::triggered, &EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::blocksRefreshRequired); + + + + QMenu* menu = new QMenu(this); + m_searchButton = menu->menuAction(); + m_searchButton->setText("Find next"); + m_searchButton->setIcon(QIcon(":/Search-next")); + m_searchButton->setData(true); + connect(m_searchButton, &QAction::triggered, this, &This::findNext); + + auto actionGroup = new QActionGroup(this); + actionGroup->setExclusive(true); + + auto a = new QAction(tr("Find next"), actionGroup); + a->setCheckable(true); + a->setChecked(true); + connect(a, &QAction::triggered, this, &This::findNextFromMenu); + menu->addAction(a); + + a = new QAction(tr("Find previous"), actionGroup); + a->setCheckable(true); + connect(a, &QAction::triggered, this, &This::findPrevFromMenu); + menu->addAction(a); + + menu->addSeparator(); + a = menu->addAction("Case sensitive"); + a->setCheckable(true); + a->setChecked(m_bCaseSensitiveSearch); + connect(a, &QAction::triggered, [this](bool _checked){ m_bCaseSensitiveSearch = _checked; }); + menu->addAction(a); + + tb->addSeparator(); + tb->addAction(m_searchButton); + tb->addWidget(m_searchBox); + + auto searchbox = new QHBoxLayout(); + searchbox->setContentsMargins(0, 0, 5, 0); + searchbox->addWidget(tb); + searchbox->addStretch(100); + searchbox->addWidget(m_foundNumber, Qt::AlignRight); + + auto lay = new QVBoxLayout(this); + lay->setContentsMargins(1, 1, 1, 1); + lay->addLayout(searchbox); + lay->addWidget(m_tree); + + connect(m_searchBox, &QLineEdit::returnPressed, this, &This::onSeachBoxReturnPressed); + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::connectionChanged, refreshButton, &QAction::setEnabled); +} + +EasyDescWidget::~EasyDescWidget() +{ + saveSettings(); +} + +void EasyDescWidget::loadSettings() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("EasyDescWidget"); + + auto val = settings.value("case_sensitive"); + if (!val.isNull()) + m_bCaseSensitiveSearch = val.toBool(); + + settings.endGroup(); +} + +void EasyDescWidget::saveSettings() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("EasyDescWidget"); + settings.setValue("case_sensitive", m_bCaseSensitiveSearch); + settings.endGroup(); +} + +void EasyDescWidget::keyPressEvent(QKeyEvent* _event) +{ + if (_event->key() == Qt::Key_F3) + { + if (_event->modifiers() & Qt::ShiftModifier) + findPrev(true); + else + findNext(true); + } + + _event->accept(); +} + +void EasyDescWidget::contextMenuEvent(QContextMenuEvent* _event) +{ + m_tree->contextMenuEvent(_event); +} + +void EasyDescWidget::build() +{ + m_tree->clearSilent(false); + m_foundNumber->setText(QString("Found 0 matches")); + m_tree->build(); +} + +void EasyDescWidget::clear() +{ + m_tree->clearSilent(true); + m_foundNumber->setText(QString("Found 0 matches")); +} + +void EasyDescWidget::onSeachBoxReturnPressed() +{ + if (m_searchButton->data().toBool() == true) + findNext(true); + else + findPrev(true); +} + +void EasyDescWidget::findNext(bool) +{ + auto matches = m_tree->findNext(m_searchBox->text(), m_bCaseSensitiveSearch ? Qt::MatchCaseSensitive : Qt::MatchFlags()); + + if (matches == 1) + m_foundNumber->setText(QString("Found 1 match")); + else + m_foundNumber->setText(QString("Found %1 matches").arg(matches)); +} + +void EasyDescWidget::findPrev(bool) +{ + auto matches = m_tree->findPrev(m_searchBox->text(), m_bCaseSensitiveSearch ? Qt::MatchCaseSensitive : Qt::MatchFlags()); + + if (matches == 1) + m_foundNumber->setText(QString("Found 1 match")); + else + m_foundNumber->setText(QString("Found %1 matches").arg(matches)); +} + +void EasyDescWidget::findNextFromMenu(bool _checked) +{ + if (!_checked) + return; + + if (m_searchButton->data().toBool() == false) + { + m_searchButton->setData(true); + m_searchButton->setText(tr("Find next")); + m_searchButton->setIcon(QIcon(":/Search-next")); + disconnect(m_searchButton, &QAction::triggered, this, &This::findPrev); + connect(m_searchButton, &QAction::triggered, this, &This::findNext); + } + + findNext(true); +} + +void EasyDescWidget::findPrevFromMenu(bool _checked) +{ + if (!_checked) + return; + + if (m_searchButton->data().toBool() == true) + { + m_searchButton->setData(false); + m_searchButton->setText(tr("Find prev")); + m_searchButton->setIcon(QIcon(":/Search-prev")); + disconnect(m_searchButton, &QAction::triggered, this, &This::findNext); + connect(m_searchButton, &QAction::triggered, this, &This::findPrev); + } + + findPrev(true); +} + +////////////////////////////////////////////////////////////////////////// diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/descriptors_tree_widget.h b/Source/ThirdParty/easy_profiler/profiler_gui/descriptors_tree_widget.h new file mode 100644 index 0000000000..69d7209d28 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/descriptors_tree_widget.h @@ -0,0 +1,211 @@ +/************************************************************************ +* file name : descriptors_tree_widget.h +* ----------------- : +* creation time : 2016/09/17 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of EasyDescTreeWidget and it's auxiliary classes +* : for displyaing EasyProfiler blocks descriptors tree. +* ----------------- : +* change log : * 2016/09/17 Victor Zarubkin: initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_DESCRIPTORS_WIDGET_H +#define EASY_DESCRIPTORS_WIDGET_H + +#include +#include + +#include +#include + +#include + +////////////////////////////////////////////////////////////////////////// + +class EasyDescWidgetItem : public QTreeWidgetItem +{ + typedef QTreeWidgetItem Parent; + typedef EasyDescWidgetItem This; + + ::profiler::block_id_t m_desc; + +public: + + explicit EasyDescWidgetItem(::profiler::block_id_t _desc, Parent* _parent = nullptr); + virtual ~EasyDescWidgetItem(); + + bool operator < (const Parent& _other) const override; + +public: + + // Public inline methods + + inline ::profiler::block_id_t desc() const + { + return m_desc; + } + +}; // END of class EasyDescWidgetItem. + +////////////////////////////////////////////////////////////////////////// + +class EasyDescTreeWidget : public QTreeWidget +{ + Q_OBJECT + + typedef QTreeWidget Parent; + typedef EasyDescTreeWidget This; + + typedef ::std::vector Items; + typedef ::std::vector TreeItems; + typedef ::std::unordered_set<::std::string> ExpandedFiles; + +protected: + + ExpandedFiles m_expandedFilesTemp; + Items m_items; + TreeItems m_highlightItems; + QString m_lastSearch; + QTreeWidgetItem* m_lastFound; + int m_lastSearchColumn; + int m_searchColumn; + bool m_bLocked; + +public: + + // Public virtual methods + + explicit EasyDescTreeWidget(QWidget* _parent = nullptr); + virtual ~EasyDescTreeWidget(); + void contextMenuEvent(QContextMenuEvent* _event) override; + +public: + + // Public non-virtual methods + + int findNext(const QString& _str, Qt::MatchFlags _flags); + int findPrev(const QString& _str, Qt::MatchFlags _flags); + +public slots: + + void clearSilent(bool _global = false); + void build(); + +private slots: + + void onSearchColumnChange(bool); + void onBlockStatusChangeClicked(bool); + void onCurrentItemChange(QTreeWidgetItem* _item, QTreeWidgetItem* _prev); + void onItemExpand(QTreeWidgetItem* _item); + void onDoubleClick(QTreeWidgetItem* _item, int _column); + void onSelectedBlockChange(uint32_t _block_index); + void onBlockStatusChange(::profiler::block_id_t _id, ::profiler::EasyBlockStatus _status); + void resizeColumnsToContents(); + +private: + + // Private methods + + void resetHighlight(); + void loadSettings(); + void saveSettings(); + +}; // END of class EasyDescTreeWidget. + +////////////////////////////////////////////////////////////////////////// + +class EasyDescWidget : public QWidget +{ + Q_OBJECT + + typedef QWidget Parent; + typedef EasyDescWidget This; + +private: + + EasyDescTreeWidget* m_tree; + class QLineEdit* m_searchBox; + class QLabel* m_foundNumber; + class QAction* m_searchButton; + bool m_bCaseSensitiveSearch; + +public: + + // Public virtual methods + + explicit EasyDescWidget(QWidget* _parent = nullptr); + virtual ~EasyDescWidget(); + void keyPressEvent(QKeyEvent* _event) override; + void contextMenuEvent(QContextMenuEvent* _event) override; + +public: + + // Public non-virtual methods + + void build(); + void clear(); + +private slots: + + void onSeachBoxReturnPressed(); + void findNext(bool); + void findPrev(bool); + void findNextFromMenu(bool); + void findPrevFromMenu(bool); + +private: + + // Private non-virtual slots + + void loadSettings(); + void saveSettings(); + +}; // END of class EasyDescWidget. + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_DESCRIPTORS_WIDGET_H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/easy_chronometer_item.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/easy_chronometer_item.cpp new file mode 100644 index 0000000000..e500861059 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/easy_chronometer_item.cpp @@ -0,0 +1,398 @@ +/************************************************************************ +* file name : easy_chronometer_item.cpp +* ----------------- : +* creation time : 2016/09/15 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of EasyChronometerItem. +* ----------------- : +* change log : * 2016/09/15 Victor Zarubkin: moved sources from blocks_graphics_view.cpp +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include +#include +#include +#include "blocks_graphics_view.h" +#include "easy_chronometer_item.h" +#include "globals.h" + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +const auto CHRONOMETER_FONT = ::profiler_gui::EFont("Helvetica", 16, QFont::Bold); + +#ifdef max +#undef max +#endif + +#ifdef min +#undef min +#endif + +////////////////////////////////////////////////////////////////////////// + +EasyChronometerItem::EasyChronometerItem(bool _main) + : Parent() + , m_color(::profiler_gui::CHRONOMETER_COLOR) + , m_left(0) + , m_right(0) + , m_bMain(_main) + , m_bReverse(false) + , m_bHoverIndicator(false) + , m_bHoverLeftBorder(false) + , m_bHoverRightBorder(false) +{ + m_indicator.reserve(3); +} + +EasyChronometerItem::~EasyChronometerItem() +{ +} + +QRectF EasyChronometerItem::boundingRect() const +{ + return m_boundingRect; +} + +void EasyChronometerItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem*, QWidget*) +{ + auto const sceneView = view(); + const auto currentScale = sceneView->scale(); + const auto offset = sceneView->offset(); + const auto visibleSceneRect = sceneView->visibleSceneRect(); + auto sceneLeft = offset, sceneRight = offset + visibleSceneRect.width() / currentScale; + + if (m_bMain) + m_indicator.clear(); + + if (m_left > sceneRight || m_right < sceneLeft) + { + // This item is out of screen + + if (m_bMain) + { + const int size = m_bHoverIndicator ? 12 : 10; + auto vcenter = visibleSceneRect.top() + visibleSceneRect.height() * 0.5; + auto color = QColor::fromRgb(m_color.rgb()); + auto pen = _painter->pen(); + pen.setColor(color); + + m_indicator.clear(); + if (m_left > sceneRight) + { + sceneRight = (sceneRight - offset) * currentScale; + m_indicator.push_back(QPointF(sceneRight - size, vcenter - size)); + m_indicator.push_back(QPointF(sceneRight, vcenter)); + m_indicator.push_back(QPointF(sceneRight - size, vcenter + size)); + } + else + { + sceneLeft = (sceneLeft - offset) * currentScale; + m_indicator.push_back(QPointF(sceneLeft + size, vcenter - size)); + m_indicator.push_back(QPointF(sceneLeft, vcenter)); + m_indicator.push_back(QPointF(sceneLeft + size, vcenter + size)); + } + + _painter->save(); + _painter->setTransform(QTransform::fromTranslate(-x(), -y()), true); + _painter->setBrush(m_bHoverIndicator ? QColor::fromRgb(0xffff0000) : color); + _painter->setPen(pen); + _painter->drawPolygon(m_indicator); + _painter->restore(); + } + + return; + } + + auto selectedInterval = width(); + QRectF rect((m_left - offset) * currentScale, visibleSceneRect.top(), ::std::max(selectedInterval * currentScale, 1.0), visibleSceneRect.height()); + selectedInterval = units2microseconds(selectedInterval); + + const QString text = ::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, selectedInterval); // Displayed text + const auto textRect = QFontMetricsF(CHRONOMETER_FONT, sceneView).boundingRect(text); // Calculate displayed text boundingRect + const auto rgb = m_color.rgb() & 0x00ffffff; + + + + // Paint!-------------------------- + _painter->save(); + + // instead of scrollbar we're using manual offset + _painter->setTransform(QTransform::fromTranslate(-x(), -y()), true); + + if (m_left < sceneLeft) + rect.setLeft(0); + + if (m_right > sceneRight) + rect.setWidth((sceneRight - offset) * currentScale - rect.left()); + + // draw transparent rectangle + auto vcenter = rect.top() + rect.height() * 0.5; + QLinearGradient g(rect.left(), vcenter, rect.right(), vcenter); + g.setColorAt(0, m_color); + g.setColorAt(0.2, QColor::fromRgba(0x14000000 | rgb)); + g.setColorAt(0.8, QColor::fromRgba(0x14000000 | rgb)); + g.setColorAt(1, m_color); + _painter->setBrush(g); + _painter->setPen(Qt::NoPen); + _painter->drawRect(rect); + + // draw left and right borders + _painter->setBrush(Qt::NoBrush); + if (m_bMain && !m_bReverse) + { + QPen p(QColor::fromRgba(0xd0000000 | rgb)); + p.setStyle(Qt::DotLine); + _painter->setPen(p); + } + else + { + _painter->setPen(QColor::fromRgba(0xd0000000 | rgb)); + } + + if (m_left > sceneLeft) + { + if (m_bHoverLeftBorder) + { + // Set bold if border is hovered + QPen p = _painter->pen(); + p.setWidth(3); + _painter->setPen(p); + } + + _painter->drawLine(QPointF(rect.left(), rect.top()), QPointF(rect.left(), rect.bottom())); + } + + if (m_right < sceneRight) + { + if (m_bHoverLeftBorder) + { + // Restore width + QPen p = _painter->pen(); + p.setWidth(1); + _painter->setPen(p); + } + else if (m_bHoverRightBorder) + { + // Set bold if border is hovered + QPen p = _painter->pen(); + p.setWidth(3); + _painter->setPen(p); + } + + _painter->drawLine(QPointF(rect.right(), rect.top()), QPointF(rect.right(), rect.bottom())); + + // This is not necessary because another setPen() invoked for draw text + //if (m_bHoverRightBorder) + //{ + // // Restore width + // QPen p = _painter->pen(); + // p.setWidth(1); + // _painter->setPen(p); + //} + } + + // draw text + _painter->setCompositionMode(QPainter::CompositionMode_Difference); // This lets the text to be visible on every background + _painter->setRenderHint(QPainter::TextAntialiasing); + _painter->setPen(0x00ffffff - rgb); + _painter->setFont(CHRONOMETER_FONT); + + int textFlags = 0; + switch (EASY_GLOBALS.chrono_text_position) + { + case ::profiler_gui::ChronoTextPosition_Top: + textFlags = Qt::AlignTop | Qt::AlignHCenter; + if (!m_bMain) rect.setTop(rect.top() + textRect.height() * 0.75); + break; + + case ::profiler_gui::ChronoTextPosition_Center: + textFlags = Qt::AlignCenter; + if (!m_bMain) rect.setTop(rect.top() + textRect.height() * 1.5); + break; + + case ::profiler_gui::ChronoTextPosition_Bottom: + textFlags = Qt::AlignBottom | Qt::AlignHCenter; + if (!m_bMain) rect.setHeight(rect.height() - textRect.height() * 0.75); + break; + } + + const auto textRect_width = textRect.width() * ::profiler_gui::FONT_METRICS_FACTOR; + if (textRect_width < rect.width()) + { + // Text will be drawed inside rectangle + _painter->drawText(rect, textFlags, text); + _painter->restore(); + return; + } + + const auto w = textRect_width / currentScale; + if (m_right + w < sceneRight) + { + // Text will be drawed to the right of rectangle + rect.translate(rect.width(), 0); + textFlags &= ~Qt::AlignHCenter; + textFlags |= Qt::AlignLeft; + } + else if (m_left - w > sceneLeft) + { + // Text will be drawed to the left of rectangle + rect.translate(-rect.width(), 0); + textFlags &= ~Qt::AlignHCenter; + textFlags |= Qt::AlignRight; + } + //else // Text will be drawed inside rectangle + + _painter->drawText(rect, textFlags | Qt::TextDontClip, text); + + _painter->restore(); + // END Paint!~~~~~~~~~~~~~~~~~~~~~~ +} + +void EasyChronometerItem::hide() +{ + m_bHoverIndicator = false; + m_bHoverLeftBorder = false; + m_bHoverRightBorder = false; + m_bReverse = false; + Parent::hide(); +} + +bool EasyChronometerItem::indicatorContains(const QPointF& _pos) const +{ + if (m_indicator.empty()) + return false; + + const auto itemX = toItem(_pos.x()); + return m_indicator.containsPoint(QPointF(itemX, _pos.y()), Qt::OddEvenFill); +} + +void EasyChronometerItem::setHoverLeft(bool _hover) +{ + m_bHoverLeftBorder = _hover; +} + +void EasyChronometerItem::setHoverRight(bool _hover) +{ + m_bHoverRightBorder = _hover; +} + +bool EasyChronometerItem::hoverLeft(qreal _x) const +{ + const auto dx = fabs(_x - m_left) * view()->scale(); + return dx < 4; +} + +bool EasyChronometerItem::hoverRight(qreal _x) const +{ + const auto dx = fabs(_x - m_right) * view()->scale(); + return dx < 4; +} + +QPointF EasyChronometerItem::toItem(const QPointF& _pos) const +{ + const auto sceneView = view(); + return QPointF((_pos.x() - sceneView->offset()) * sceneView->scale() - x(), _pos.y()); +} + +qreal EasyChronometerItem::toItem(qreal _x) const +{ + const auto sceneView = view(); + return (_x - sceneView->offset()) * sceneView->scale() - x(); +} + +void EasyChronometerItem::setColor(const QColor& _color) +{ + m_color = _color; +} + +void EasyChronometerItem::setBoundingRect(qreal x, qreal y, qreal w, qreal h) +{ + m_boundingRect.setRect(x, y, w, h); +} + +void EasyChronometerItem::setBoundingRect(const QRectF& _rect) +{ + m_boundingRect = _rect; +} + +void EasyChronometerItem::setLeftRight(qreal _left, qreal _right) +{ + if (_left < _right) + { + m_left = _left; + m_right = _right; + } + else + { + m_left = _right; + m_right = _left; + } +} + +void EasyChronometerItem::setReverse(bool _reverse) +{ + m_bReverse = _reverse; +} + +void EasyChronometerItem::setHoverIndicator(bool _hover) +{ + m_bHoverIndicator = _hover; +} + +const EasyGraphicsView* EasyChronometerItem::view() const +{ + return static_cast(scene()->parent()); +} + +EasyGraphicsView* EasyChronometerItem::view() +{ + return static_cast(scene()->parent()); +} + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/easy_chronometer_item.h b/Source/ThirdParty/easy_profiler/profiler_gui/easy_chronometer_item.h new file mode 100644 index 0000000000..1c20865e0b --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/easy_chronometer_item.h @@ -0,0 +1,171 @@ +/************************************************************************ +* file name : easy_chronometer_item.h +* ----------------- : +* creation time : 2016/09/15 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of EasyChronometerItem - an item +* : used to display selected interval on graphics scene. +* ----------------- : +* change log : * 2016/09/15 Victor Zarubkin: moved sources from blocks_graphics_view.h +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_CHRONOMETER_ITEM_H +#define EASY_CHRONOMETER_ITEM_H + +#include +#include +#include +#include + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +class QWidget; +class QPainter; +class QStyleOptionGraphicsItem; +class EasyGraphicsView; + +class EasyChronometerItem : public QGraphicsItem +{ + typedef QGraphicsItem Parent; + typedef EasyChronometerItem This; + + QPolygonF m_indicator; ///< Indicator displayed when this chrono item is out of screen (displaying only for main item) + QRectF m_boundingRect; ///< boundingRect (see QGraphicsItem) + QColor m_color; ///< Color of the item + qreal m_left, m_right; ///< Left and right bounds of the selection zone + bool m_bMain; ///< Is this chronometer main (true, by default) + bool m_bReverse; ///< + bool m_bHoverIndicator; ///< Mouse hover above indicator + bool m_bHoverLeftBorder; + bool m_bHoverRightBorder; + +public: + + explicit EasyChronometerItem(bool _main = true); + virtual ~EasyChronometerItem(); + + // Public virtual methods + + QRectF boundingRect() const override; + void paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget = nullptr) override; + +public: + + // Public non-virtual methods + + void hide(); + + void setColor(const QColor& _color); + + void setBoundingRect(qreal x, qreal y, qreal w, qreal h); + void setBoundingRect(const QRectF& _rect); + + void setLeftRight(qreal _left, qreal _right); + + void setReverse(bool _reverse); + + void setHoverIndicator(bool _hover); + + bool indicatorContains(const QPointF& _pos) const; + + void setHoverLeft(bool _hover); + void setHoverRight(bool _hover); + + bool hoverLeft(qreal _x) const; + bool hoverRight(qreal _x) const; + + QPointF toItem(const QPointF& _pos) const; + qreal toItem(qreal _x) const; + + inline bool hoverIndicator() const + { + return m_bHoverIndicator; + } + + inline bool hoverLeft() const + { + return m_bHoverLeftBorder; + } + + inline bool hoverRight() const + { + return m_bHoverRightBorder; + } + + inline bool reverse() const + { + return m_bReverse; + } + + inline qreal left() const + { + return m_left; + } + + inline qreal right() const + { + return m_right; + } + + inline qreal width() const + { + return m_right - m_left; + } + +private: + + ///< Returns pointer to the EasyGraphicsView widget. + const EasyGraphicsView* view() const; + EasyGraphicsView* view(); + +}; // END of class EasyChronometerItem. + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_CHRONOMETER_ITEM_H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/easy_frame_rate_viewer.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/easy_frame_rate_viewer.cpp new file mode 100644 index 0000000000..43e3367633 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/easy_frame_rate_viewer.cpp @@ -0,0 +1,339 @@ +/************************************************************************ +* file name : easy_frame_rate_viewer.cpp +* ----------------- : +* creation time : 2017/04/02 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : This file contains implementation of EasyFrameRateViewer widget. +* ----------------- : +* change log : * 2017/04/02 Victor Zarubkin: Initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include +#include +#include +#include +#include +#include "easy_frame_rate_viewer.h" +#include "globals.h" + +const int INTERVAL_WIDTH = 20; + +////////////////////////////////////////////////////////////////////////// + +EasyFPSGraphicsItem::EasyFPSGraphicsItem() : Parent(nullptr) +{ + +} + +EasyFPSGraphicsItem::~EasyFPSGraphicsItem() +{ + +} + +////////////////////////////////////////////////////////////////////////// + +QRectF EasyFPSGraphicsItem::boundingRect() const +{ + return m_boundingRect; +} + +void EasyFPSGraphicsItem::setBoundingRect(const QRectF& _boundingRect) +{ + m_boundingRect = _boundingRect; +} + +void EasyFPSGraphicsItem::setBoundingRect(qreal x, qreal y, qreal w, qreal h) +{ + m_boundingRect.setRect(x, y, w, h); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyFPSGraphicsItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem*, QWidget*) +{ + if (m_frames.empty()) + return; + + const auto fontMetrics = QFontMetrics(scene()->font()); + const int fontHeight = fontMetrics.height() + 2; + const qreal h = m_boundingRect.height() - (fontHeight << 1) - 4; + if (h < 0) + return; + + const qreal halfWidth = m_boundingRect.width() * 0.5 - INTERVAL_WIDTH; + const int halfMax = static_cast(0.5 + halfWidth / INTERVAL_WIDTH); + const int half = static_cast(m_frames.size() / 2); + const qreal top = fontHeight, bottom = h + 4 + fontHeight; + qreal y; + + _painter->save(); + + _painter->drawLine(QPointF(0, top), QPointF(m_boundingRect.width(), top)); + _painter->drawLine(QPointF(0, bottom), QPointF(m_boundingRect.width(), bottom)); + + _painter->setPen(Qt::lightGray); + y = m_boundingRect.height() * 0.5; + _painter->drawLine(QPointF(0, y), QPointF(m_boundingRect.width(), y)); + y -= h * 0.25; + _painter->drawLine(QPointF(0, y), QPointF(m_boundingRect.width(), y)); + y += h * 0.5; + _painter->drawLine(QPointF(0, y), QPointF(m_boundingRect.width(), y)); + + m_points1.reserve(m_frames.size()); + m_points2.reserve(m_frames.size()); + int n = 0; + qreal x = m_boundingRect.width() * 0.5 + std::min(halfMax, half) * INTERVAL_WIDTH, localMax = 0, localMin = 1e30; + const qreal xCurrent = x; + for (int i = static_cast(m_frames.size()) - 1; i > -1 && x >= 0; --i, x -= INTERVAL_WIDTH, ++n) + { + const auto& val = m_frames[i]; + + if (val.first > localMax) + localMax = val.first; + if (val.first < localMin) + localMin = val.first; + m_points1.emplace_back(x, static_cast(val.first) + 1e-3); + + if (val.second > localMax) + localMax = val.second; + if (val.second < localMin) + localMin = val.second; + m_points2.emplace_back(x, static_cast(val.second) + 1e-3); + + _painter->drawLine(QPointF(x, top + 1), QPointF(x, bottom - 1)); + } + + const auto delta = std::max(localMax - localMin, 1e-3); + _painter->setPen(Qt::black); + + qreal frameTime = std::max(localMax, 1.); + _painter->drawText(5, 0, m_boundingRect.width() - 10, fontHeight, Qt::AlignVCenter | Qt::AlignLeft, QString("Slowest %1 FPS (%2)") + .arg(static_cast(1e6 / frameTime)).arg(::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, localMax, 1))); + + frameTime = std::max(m_frames.back().first, 1U); + _painter->drawText(5, 0, xCurrent - 5, fontHeight, Qt::AlignVCenter | Qt::AlignRight, QString("Max current %1 FPS (%2)") + .arg(static_cast(1e6 / frameTime)).arg(::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, m_frames.back().first, 1))); + + frameTime = std::max(m_frames.back().second, 1U); + _painter->drawText(5, bottom, xCurrent - 5, fontHeight, Qt::AlignVCenter | Qt::AlignRight, QString("Avg current %1 FPS (%2)") + .arg(static_cast(1e6 / frameTime)).arg(::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, m_frames.back().second, 1))); + + frameTime = std::max(localMin, 1.); + _painter->drawText(5, bottom, m_boundingRect.width() - 10, fontHeight, Qt::AlignVCenter | Qt::AlignLeft, QString("Fastest %1 FPS (%2)") + .arg(static_cast(1e6 / frameTime)).arg(::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, localMin, 1))); + + if (localMin < EASY_GLOBALS.frame_time && EASY_GLOBALS.frame_time < localMax) + { + y = fontHeight + 2 + h * (1. - (EASY_GLOBALS.frame_time - localMin) / delta); + _painter->setPen(Qt::DashLine); + _painter->drawLine(QPointF(0, y), QPointF(m_boundingRect.width(), y)); + } + + for (int i = 0; i < n; ++i) + { + auto& point1 = m_points1[i]; + point1.setY(fontHeight + 2 + h * (1. - (point1.y() - localMin) / delta)); + + auto& point2 = m_points2[i]; + point2.setY(fontHeight + 2 + h * (1. - (point2.y() - localMin) / delta)); + } + + _painter->setRenderHint(QPainter::Antialiasing, true); + + QPen pen(QColor::fromRgba(0x80ff0000)); + pen.setWidth(EASY_GLOBALS.fps_widget_line_width); + _painter->setPen(pen); + if (n > 1) + { + _painter->drawPolyline(m_points1.data(), n); + + pen.setColor(QColor::fromRgba(0x800000ff)); + _painter->setPen(pen); + _painter->drawPolyline(m_points2.data(), n); + } + else + { + _painter->drawPoint(m_points1.back()); + + pen.setColor(QColor::fromRgba(0x800000ff)); + _painter->setPen(pen); + _painter->drawPoint(m_points2.back()); + } + + const auto txtTop = ::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, localMin + delta * 0.75, 1); + const auto txtMid = ::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, localMin + delta * 0.5, 1); + const auto txtBtm = ::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, localMin + delta * 0.25, 1); + + _painter->setPen(Qt::NoPen); + _painter->setBrush(Qt::white); + _painter->drawRect(0, top + 1, std::max({fontMetrics.width(txtTop), fontMetrics.width(txtMid), fontMetrics.width(txtBtm)}) + 8, bottom - top - 1); + + _painter->setPen(Qt::black); + _painter->setBrush(Qt::NoBrush); + + y = m_boundingRect.height() * 0.5; + _painter->drawText(5, y - (fontHeight >> 1), m_boundingRect.width(), fontHeight, Qt::AlignVCenter | Qt::AlignLeft, txtMid); + + y -= h * 0.25; + _painter->drawText(5, y - (fontHeight >> 1), m_boundingRect.width(), fontHeight, Qt::AlignVCenter | Qt::AlignLeft, txtTop); + + y += h * 0.5; + _painter->drawText(5, y - (fontHeight >> 1), m_boundingRect.width(), fontHeight, Qt::AlignVCenter | Qt::AlignLeft, txtBtm); + + _painter->restore(); + + m_points1.clear(); + m_points2.clear(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyFPSGraphicsItem::clear() +{ + m_frames.clear(); +} + +void EasyFPSGraphicsItem::addPoint(uint32_t _maxFrameTime, uint32_t _avgFrameTime) +{ + m_frames.emplace_back(_maxFrameTime, _avgFrameTime); + if (static_cast(m_frames.size()) > EASY_GLOBALS.max_fps_history) + m_frames.pop_front(); +} + +////////////////////////////////////////////////////////////////////////// + +EasyFrameRateViewer::EasyFrameRateViewer(QWidget* _parent) : Parent(_parent), m_fpsItem(nullptr) +{ + setCacheMode(QGraphicsView::CacheNone); + //setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); + setViewportUpdateMode(QGraphicsView::FullViewportUpdate); + setOptimizationFlag(QGraphicsView::DontSavePainterState, true); + + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + + setContentsMargins(0, 0, 0, 0); + setScene(new QGraphicsScene(this)); + scene()->setSceneRect(0, 0, 50, 50); + + m_fpsItem = new EasyFPSGraphicsItem(); + m_fpsItem->setPos(0, 0); + m_fpsItem->setBoundingRect(0, 0, 50, 50); + scene()->addItem(m_fpsItem); + + centerOn(0, 0); + + // Dirty hack for QDockWidget stupid initial size policy :( + setFixedHeight(10); // Set very small height to enable appropriate minimum height on the application startup + QTimer::singleShot(100, [this]() + { + // Now set appropriate minimum height + setMinimumHeight((QFontMetrics(scene()->font()).height() + 3) * 6); + setMaximumHeight(minimumHeight() * 20); + }); +} + +EasyFrameRateViewer::~EasyFrameRateViewer() +{ + +} + +void EasyFrameRateViewer::clear() +{ + m_fpsItem->clear(); + scene()->update(); +} + +void EasyFrameRateViewer::addPoint(uint32_t _maxFrameTime, uint32_t _avgFrameTime) +{ + m_fpsItem->addPoint(_maxFrameTime, _avgFrameTime); + scene()->update(); +} + +void EasyFrameRateViewer::resizeEvent(QResizeEvent* _event) +{ + Parent::resizeEvent(_event); + + auto size = _event->size(); + m_fpsItem->setBoundingRect(0, 0, size.width(), size.height()); + + scene()->setSceneRect(m_fpsItem->boundingRect()); + scene()->update(); +} + +void EasyFrameRateViewer::hideEvent(QHideEvent* _event) +{ + Parent::hideEvent(_event); + EASY_GLOBALS.fps_enabled = isVisible(); + clear(); +} + +void EasyFrameRateViewer::showEvent(QShowEvent* _event) +{ + Parent::showEvent(_event); + EASY_GLOBALS.fps_enabled = isVisible(); + clear(); +} + +void EasyFrameRateViewer::contextMenuEvent(QContextMenuEvent* _event) +{ + QMenu menu; + QAction* action = nullptr; + + action = menu.addAction(QIcon(":/Delete"), "Clear"); + connect(action, &QAction::triggered, [this](bool){ clear(); }); + + action = menu.addAction("Close"); + connect(action, &QAction::triggered, [this](bool){ parentWidget()->hide(); }); + + menu.exec(QCursor::pos()); + + _event->accept(); +} + +////////////////////////////////////////////////////////////////////////// + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/easy_frame_rate_viewer.h b/Source/ThirdParty/easy_profiler/profiler_gui/easy_frame_rate_viewer.h new file mode 100644 index 0000000000..d74d314885 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/easy_frame_rate_viewer.h @@ -0,0 +1,125 @@ +/************************************************************************ +* file name : easy_frame_rate_viewer.cpp +* ----------------- : +* creation time : 2017/04/02 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : This file contains declaration of EasyFrameRateViewer widget. +* ----------------- : +* change log : * 2017/04/02 Victor Zarubkin: Initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY__FRAME_RATE_VIEWER__H +#define EASY__FRAME_RATE_VIEWER__H + +#include +#include +#include +#include +#include +#include + +////////////////////////////////////////////////////////////////////////// + +class EasyFPSGraphicsItem : public QGraphicsItem +{ + typedef QGraphicsItem Parent; + typedef EasyFPSGraphicsItem This; + typedef std::deque > FrameTimes; + + std::vector m_points1, m_points2; + FrameTimes m_frames; + QRectF m_boundingRect; + +public: + + explicit EasyFPSGraphicsItem(); + virtual ~EasyFPSGraphicsItem(); + + QRectF boundingRect() const override; + void paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget = nullptr) override; + + void setBoundingRect(const QRectF& _boundingRect); + void setBoundingRect(qreal x, qreal y, qreal w, qreal h); + + void clear(); + void addPoint(uint32_t _maxFrameTime, uint32_t _avgFrameTime); + +}; // END of class EasyFPSGraphicsItem. + +////////////////////////////////////////////////////////////////////////// + +class EasyFrameRateViewer : public QGraphicsView +{ + Q_OBJECT + +private: + + typedef QGraphicsView Parent; + typedef EasyFrameRateViewer This; + + EasyFPSGraphicsItem* m_fpsItem; + +public: + + explicit EasyFrameRateViewer(QWidget* _parent = nullptr); + virtual ~EasyFrameRateViewer(); + + void resizeEvent(QResizeEvent* _event) override; + void hideEvent(QHideEvent* _event) override; + void showEvent(QShowEvent* _event) override; + void contextMenuEvent(QContextMenuEvent* _event) override; + +public slots: + + void clear(); + void addPoint(uint32_t _maxFrameTime, uint32_t _avgFrameTime); + +}; // END of class EasyFrameRateViewer. + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY__FRAME_RATE_VIEWER__H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_item.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_item.cpp new file mode 100644 index 0000000000..1aab802f1a --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_item.cpp @@ -0,0 +1,1474 @@ +/************************************************************************ +* file name : easy_graphics_item.cpp +* ----------------- : +* creation time : 2016/09/15 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of EasyGraphicsItem. +* ----------------- : +* change log : * 2016/09/15 Victor Zarubkin: Moved sources from blocks_graphics_view.cpp +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include +#include +#include +#include "easy_graphics_item.h" +#include "blocks_graphics_view.h" +#include "globals.h" + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +enum BlockItemState : int8_t +{ + BLOCK_ITEM_DO_PAINT_FIRST = -2, + BLOCK_ITEM_DO_NOT_PAINT = -1, + BLOCK_ITEM_UNCHANGED, + BLOCK_ITEM_DO_PAINT +}; + +////////////////////////////////////////////////////////////////////////// + +const int MIN_SYNC_SPACING = 1; +const int MIN_SYNC_SIZE = 3; +const QRgb BORDERS_COLOR = ::profiler::colors::Grey600 & 0x00ffffff;// 0x00686868; + +inline QRgb selectedItemBorderColor(::profiler::color_t _color) { + return ::profiler_gui::isLightColor(_color, 192) ? ::profiler::colors::Black : ::profiler::colors::RichRed; +} + +const QPen HIGHLIGHTER_PEN = ([]() -> QPen { QPen p(::profiler::colors::Black); p.setStyle(Qt::DotLine); p.setWidth(2); return p; })(); +const auto ITEMS_FONT = ::profiler_gui::EFont("Helvetica", 10, QFont::Medium); +const auto SELECTED_ITEM_FONT = ::profiler_gui::EFont("Helvetica", 10, QFont::Bold); + +#ifdef max +#undef max +#endif + +#ifdef min +#undef min +#endif + +////////////////////////////////////////////////////////////////////////// + +EasyGraphicsItem::EasyGraphicsItem(uint8_t _index, const::profiler::BlocksTreeRoot& _root) + : QGraphicsItem(nullptr) + , m_threadName(::profiler_gui::decoratedThreadName(EASY_GLOBALS.use_decorated_thread_name, _root, EASY_GLOBALS.hex_thread_id)) + , m_pRoot(&_root) + , m_index(_index) +{ +} + +EasyGraphicsItem::~EasyGraphicsItem() +{ +} + +void EasyGraphicsItem::validateName() +{ + m_threadName = ::profiler_gui::decoratedThreadName(EASY_GLOBALS.use_decorated_thread_name, *m_pRoot, EASY_GLOBALS.hex_thread_id); +} + +const EasyGraphicsView* EasyGraphicsItem::view() const +{ + return static_cast(scene()->parent()); +} + +////////////////////////////////////////////////////////////////////////// + +QRectF EasyGraphicsItem::boundingRect() const +{ + return m_boundingRect; +} + +////////////////////////////////////////////////////////////////////////// + +struct EasyPainterInformation EASY_FINAL +{ + const QRectF visibleSceneRect; + QRectF rect; + QBrush brush; + const qreal visibleBottom; + const qreal currentScale; + const qreal offset; + const qreal sceneLeft; + const qreal sceneRight; + const qreal dx; + QRgb previousColor; + QRgb textColor; + Qt::PenStyle previousPenStyle; + bool is_light; + bool selectedItemsWasPainted; + + explicit EasyPainterInformation(const EasyGraphicsView* sceneView) + : visibleSceneRect(sceneView->visibleSceneRect()) + , visibleBottom(visibleSceneRect.bottom() - 1) + , currentScale(sceneView->scale()) + , offset(sceneView->offset()) + , sceneLeft(offset) + , sceneRight(offset + visibleSceneRect.width() / currentScale) + , dx(offset * currentScale) + , previousColor(0) + , textColor(0) + , previousPenStyle(Qt::NoPen) + , is_light(false) + , selectedItemsWasPainted(false) + { + brush.setStyle(Qt::SolidPattern); + } + + EasyPainterInformation() = delete; +}; + +#ifdef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT +void EasyGraphicsItem::paintChildren(const float _minWidth, const int _narrowSizeHalf, const uint8_t _levelsNumber, QPainter* _painter, struct EasyPainterInformation& p, ::profiler_gui::EasyBlockItem& _item, const ::profiler_gui::EasyBlock& _itemBlock, RightBounds& _rightBounds, uint8_t _level, int8_t _mode) +{ + if (_level >= _levelsNumber || _itemBlock.tree.children.empty()) + return; + + const auto top = levelY(_level); + if (top > p.visibleBottom) + return; + + qreal& prevRight = _rightBounds[_level]; + auto& level = m_levels[_level]; + const short next_level = _level + 1; + + uint32_t neighbours = (uint32_t)_itemBlock.tree.children.size(); + uint32_t last = neighbours - 1; + uint32_t neighbour = 0; + + if (_mode == BLOCK_ITEM_DO_PAINT_FIRST) + { + neighbour = last = _item.max_depth_child; + neighbours = neighbour + 1; + } + + for (uint32_t i = _item.children_begin + neighbour; neighbour < neighbours; ++i, ++neighbour) + { + auto& item = level[i]; + + if (item.left() > p.sceneRight) + break; // This is first totally invisible item. No need to check other items. + + if (item.right() < p.sceneLeft) + continue; // This item is not visible + + const auto& itemBlock = easyBlock(item.block); + const uint16_t totalHeight = itemBlock.tree.depth * ::profiler_gui::GRAPHICS_ROW_SIZE_FULL + ::profiler_gui::GRAPHICS_ROW_SIZE; + if ((top + totalHeight) < p.visibleSceneRect.top()) + continue; // This item is not visible + + const auto item_width = ::std::max(item.width(), _minWidth); + auto x = item.left() * p.currentScale - p.dx; + auto w = item_width * p.currentScale; + //const auto right = x + w; + if ((x + w) <= prevRight) + { + // This item is not visible + if (!(EASY_GLOBALS.hide_narrow_children && w < EASY_GLOBALS.blocks_narrow_size)) + paintChildren(_minWidth, _narrowSizeHalf, _levelsNumber, _painter, p, item, itemBlock, _rightBounds, next_level, BLOCK_ITEM_DO_PAINT_FIRST); + continue; + } + + if (x < prevRight) + { + w -= prevRight - x; + x = prevRight; + } + + if (EASY_GLOBALS.hide_minsize_blocks && w < EASY_GLOBALS.blocks_size_min) + continue; // Hide blocks (except top-level blocks) which width is less than 1 pixel + + const auto& itemDesc = easyDescriptor(itemBlock.tree.node->id()); + + int h = 0, flags = 0; + bool do_paint_children = false; + if ((EASY_GLOBALS.hide_narrow_children && w < EASY_GLOBALS.blocks_narrow_size) || !itemBlock.expanded) + { + // Items which width is less than 20 will be painted as big rectangles which are hiding it's children + + //x = item.left() * p.currentScale - p.dx; + h = totalHeight; + const auto dh = top + h - p.visibleBottom; + if (dh > 0) + h -= dh; + + if (item.block == EASY_GLOBALS.selected_block) + p.selectedItemsWasPainted = true; + + const bool colorChange = (p.previousColor != itemDesc.color()); + if (colorChange) + { + // Set background color brush for rectangle + p.previousColor = itemDesc.color(); + //p.inverseColor = 0xffffffff - p.previousColor; + p.is_light = ::profiler_gui::isLightColor(p.previousColor); + p.textColor = ::profiler_gui::textColorForFlag(p.is_light); + p.brush.setColor(p.previousColor); + _painter->setBrush(p.brush); + } + + if (EASY_GLOBALS.highlight_blocks_with_same_id && (EASY_GLOBALS.selected_block_id == itemBlock.tree.node->id() + || (::profiler_gui::is_max(EASY_GLOBALS.selected_block) && EASY_GLOBALS.selected_block_id == itemDesc.id()))) + { + if (p.previousPenStyle != Qt::DotLine) + { + p.previousPenStyle = Qt::DotLine; + _painter->setPen(HIGHLIGHTER_PEN); + } + } + else if (EASY_GLOBALS.draw_graphics_items_borders) + { + if (p.previousPenStyle != Qt::SolidLine)// || colorChange) + { + // Restore pen for item which is wide enough to paint borders + p.previousPenStyle = Qt::SolidLine; + _painter->setPen(BORDERS_COLOR);//BORDERS_COLOR & inverseColor); + } + } + else if (p.previousPenStyle != Qt::NoPen) + { + p.previousPenStyle = Qt::NoPen; + _painter->setPen(Qt::NoPen); + } + + const auto wprev = w; + decltype(w) dw = 0; + if (item.left() < p.sceneLeft) + { + // if item left border is out of screen then attach text to the left border of the screen + // to ensure text is always visible for items presenting on the screen. + w += (item.left() - p.sceneLeft) * p.currentScale; + x = p.sceneLeft * p.currentScale - p.dx - 2; + w += 2; + dw = 2; + } + + if (item.right() > p.sceneRight) + { + w -= (item.right() - p.sceneRight) * p.currentScale; + w += 2; + dw += 2; + } + + if (w < EASY_GLOBALS.blocks_size_min) + w = EASY_GLOBALS.blocks_size_min; + + // Draw rectangle + p.rect.setRect(x, top, w, h); + _painter->drawRect(p.rect); + + prevRight = p.rect.right() + EASY_GLOBALS.blocks_spacing; + //skip_children(next_level, item.children_begin); + if (wprev < EASY_GLOBALS.blocks_narrow_size) + continue; + + if (totalHeight > ::profiler_gui::GRAPHICS_ROW_SIZE) + flags = Qt::AlignCenter; + else if (!(item.width() < 1)) + flags = Qt::AlignHCenter; + + if (dw > 1) + { + w -= dw; + x += 2; + } + } + else + { + if (item.block == EASY_GLOBALS.selected_block) + p.selectedItemsWasPainted = true; + + const bool colorChange = (p.previousColor != itemDesc.color()); + if (colorChange) + { + // Set background color brush for rectangle + p.previousColor = itemDesc.color(); + //p.inverseColor = 0xffffffff - p.previousColor; + p.is_light = ::profiler_gui::isLightColor(p.previousColor); + p.textColor = ::profiler_gui::textColorForFlag(p.is_light); + p.brush.setColor(p.previousColor); + _painter->setBrush(p.brush); + } + + if (EASY_GLOBALS.highlight_blocks_with_same_id && (EASY_GLOBALS.selected_block_id == itemBlock.tree.node->id() + || (::profiler_gui::is_max(EASY_GLOBALS.selected_block) && EASY_GLOBALS.selected_block_id == itemDesc.id()))) + { + if (p.previousPenStyle != Qt::DotLine) + { + p.previousPenStyle = Qt::DotLine; + _painter->setPen(HIGHLIGHTER_PEN); + } + } + else if (EASY_GLOBALS.draw_graphics_items_borders) + { + if (p.previousPenStyle != Qt::SolidLine)// || colorChange) + { + // Restore pen for item which is wide enough to paint borders + p.previousPenStyle = Qt::SolidLine; + _painter->setPen(BORDERS_COLOR);// BORDERS_COLOR & inverseColor); + } + } + else if (p.previousPenStyle != Qt::NoPen) + { + p.previousPenStyle = Qt::NoPen; + _painter->setPen(Qt::NoPen); + } + + // Draw rectangle + //x = item.left() * currentScale - p.dx; + h = ::profiler_gui::GRAPHICS_ROW_SIZE; + const auto dh = top + h - p.visibleBottom; + if (dh > 0) + h -= dh; + + const auto wprev = w; + decltype(w) dw = 0; + if (item.left() < p.sceneLeft) + { + // if item left border is out of screen then attach text to the left border of the screen + // to ensure text is always visible for items presenting on the screen. + w += (item.left() - p.sceneLeft) * p.currentScale; + x = p.sceneLeft * p.currentScale - p.dx - 2; + w += 2; + dw = 2; + } + + if (item.right() > p.sceneRight) + { + w -= (item.right() - p.sceneRight) * p.currentScale; + w += 2; + dw += 2; + } + + if (w < EASY_GLOBALS.blocks_size_min) + w = EASY_GLOBALS.blocks_size_min; + + p.rect.setRect(x, top, w, h); + _painter->drawRect(p.rect); + + prevRight = p.rect.right() + EASY_GLOBALS.blocks_spacing; + if (wprev < EASY_GLOBALS.blocks_narrow_size) + { + paintChildren(_minWidth, _narrowSizeHalf, _levelsNumber, _painter, p, item, itemBlock, _rightBounds, next_level, wprev < _narrowSizeHalf ? BLOCK_ITEM_DO_PAINT_FIRST : BLOCK_ITEM_DO_PAINT); + continue; + } + + if (!(item.width() < 1)) + flags = Qt::AlignHCenter; + + if (dw > 1) + { + w -= dw; + x += 2; + } + + do_paint_children = true; + } + + // Draw text----------------------------------- + p.rect.setRect(x + 1, top, w - 1, h); + + // text will be painted with inverse color + //auto textColor = inverseColor < 0x00808080 ? profiler::colors::Black : profiler::colors::White; + //if (textColor == previousColor) textColor = 0; + _painter->setPen(p.textColor); + + if (item.block == EASY_GLOBALS.selected_block) + _painter->setFont(SELECTED_ITEM_FONT); + + // drawing text + auto name = *itemBlock.tree.node->name() != 0 ? itemBlock.tree.node->name() : itemDesc.name(); + _painter->drawText(p.rect, flags, ::profiler_gui::toUnicode(name)); + + // restore previous pen color + if (p.previousPenStyle == Qt::NoPen) + _painter->setPen(Qt::NoPen); + else if (p.previousPenStyle == Qt::DotLine) + { + _painter->setPen(HIGHLIGHTER_PEN); + } + else + _painter->setPen(BORDERS_COLOR);// BORDERS_COLOR & inverseColor); // restore pen for rectangle painting + + // restore font + if (item.block == EASY_GLOBALS.selected_block) + _painter->setFont(ITEMS_FONT); + // END Draw text~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + if (do_paint_children) + paintChildren(_minWidth, _narrowSizeHalf, _levelsNumber, _painter, p, item, itemBlock, _rightBounds, next_level, _mode); + } +} +#endif + +void EasyGraphicsItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem*, QWidget*) +{ + const bool gotItems = !m_levels.empty() && !m_levels.front().empty(); + const bool gotSync = !m_pRoot->sync.empty(); + + if (!gotItems && !gotSync) + { + return; + } + + EasyPainterInformation p(view()); + + _painter->save(); + _painter->setFont(ITEMS_FONT); + + // Reset indices of first visible item for each layer + const auto levelsNumber = levels(); + m_rightBounds[0] = -1e100; + for (uint8_t i = 1; i < levelsNumber; ++i) { + ::profiler_gui::set_max(m_levelsIndexes[i]); + m_rightBounds[i] = -1e100; + } + + + // Search for first visible top-level item + if (gotItems) + { + auto& level0 = m_levels.front(); + auto first = ::std::lower_bound(level0.begin(), level0.end(), p.sceneLeft, [](const ::profiler_gui::EasyBlockItem& _item, qreal _value) + { + return _item.left() < _value; + }); + + if (first != level0.end()) + { + m_levelsIndexes[0] = first - level0.begin(); + if (m_levelsIndexes[0] > 0) + m_levelsIndexes[0] -= 1; + } + else + { + m_levelsIndexes[0] = static_cast(level0.size() - 1); + } + } + + + + // This is to make _painter->drawText() work properly + // (it seems there is a bug in Qt5.6 when drawText called for big coordinates, + // drawRect at the same time called for actually same coordinates + // works fine without using this additional shifting) + //const auto dx = p.offset * p.currentScale; + + // Shifting coordinates to current screen offset + _painter->setTransform(QTransform::fromTranslate(0, -y()), true); + + + + if (EASY_GLOBALS.draw_graphics_items_borders) + { + p.previousPenStyle = Qt::SolidLine; + _painter->setPen(BORDERS_COLOR); + } + else + { + _painter->setPen(Qt::NoPen); + } + + + const auto MIN_WIDTH = EASY_GLOBALS.enable_zero_length ? 0.f : 0.25f; + + + // Iterate through layers and draw visible items + if (gotItems) + { + const int narrow_size_half = EASY_GLOBALS.blocks_narrow_size >> 1; + +#ifndef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + static const auto MAX_CHILD_INDEX = ::profiler_gui::numeric_max(); + auto const dont_skip_children = [this, &levelsNumber](short next_level, decltype(::profiler_gui::EasyBlockItem::children_begin) children_begin, int8_t _state) + { + if (next_level < levelsNumber && children_begin != MAX_CHILD_INDEX) + { + if (m_levelsIndexes[next_level] == MAX_CHILD_INDEX) + { + // Mark first potentially visible child item on next sublevel + m_levelsIndexes[next_level] = children_begin; + } + + // Mark children items that we want to draw them + m_levels[next_level][children_begin].state = _state; + } + }; +#endif + + //size_t iterations = 0; +#ifndef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + for (uint8_t l = 0; l < levelsNumber; ++l) +#else + for (uint8_t l = 0; l < 1; ++l) +#endif + { + auto& level = m_levels[l]; + const short next_level = l + 1; + + const auto top = levelY(l); + if (top > p.visibleBottom) + break; + + //qreal& prevRight = m_rightBounds[l]; + qreal prevRight = -1e100; + uint32_t neighbour = 0; + for (uint32_t i = m_levelsIndexes[l], end = static_cast(level.size()); i < end; ++i, ++neighbour) + { + //++iterations; + + auto& item = level[i]; + + if (item.left() > p.sceneRight) + break; // This is first totally invisible item. No need to check other items. + +#ifndef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + char state = BLOCK_ITEM_DO_PAINT; + if (item.state != BLOCK_ITEM_UNCHANGED) + { + neighbour = 0; // first block in parent's children list + state = item.state; + item.state = BLOCK_ITEM_DO_NOT_PAINT; + } +#endif + + if (item.right() < p.sceneLeft) + continue; // This item is not visible + +#ifndef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + if (state == BLOCK_ITEM_DO_NOT_PAINT) + { + // This item is not visible + if (neighbour < item.neighbours) + i += item.neighbours - neighbour - 1; // Skip all neighbours + continue; + } + + if (state == BLOCK_ITEM_DO_PAINT_FIRST && item.children_begin == MAX_CHILD_INDEX && next_level < levelsNumber && neighbour < (item.neighbours-1)) + // Paint only first child which has own children + continue; // This item has no children and would not be painted +#endif + + const auto& itemBlock = easyBlock(item.block); + const uint16_t totalHeight = itemBlock.tree.depth * ::profiler_gui::GRAPHICS_ROW_SIZE_FULL + ::profiler_gui::GRAPHICS_ROW_SIZE; + if ((top + totalHeight) < p.visibleSceneRect.top()) + continue; // This item is not visible + + const auto item_width = ::std::max(item.width(), MIN_WIDTH); + auto x = item.left() * p.currentScale - p.dx; + auto w = item_width * p.currentScale; + if ((x + w) <= prevRight) + { + // This item is not visible +#ifdef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + if (!EASY_GLOBALS.hide_narrow_children || w >= EASY_GLOBALS.blocks_narrow_size) + paintChildren(MIN_WIDTH, narrow_size_half, levelsNumber, _painter, p, item, itemBlock, m_rightBounds, next_level, BLOCK_ITEM_DO_PAINT_FIRST); +#else + if (!(EASY_GLOBALS.hide_narrow_children && w < EASY_GLOBALS.blocks_narrow_size) && l > 0) + dont_skip_children(next_level, item.children_begin, BLOCK_ITEM_DO_PAINT_FIRST); +#endif + continue; + } + + if (x < prevRight) + { + w -= prevRight - x; + x = prevRight; + } + +#ifndef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + if (EASY_GLOBALS.hide_minsize_blocks && w < EASY_GLOBALS.blocks_size_min && l > 0) + continue; // Hide blocks (except top-level blocks) which width is less than 1 pixel + + if (state == BLOCK_ITEM_DO_PAINT_FIRST && neighbour < item.neighbours) + { + // Paint only first child which has own children + i += item.neighbours - neighbour - 1; // Skip all neighbours + } +#endif + + const auto& itemDesc = easyDescriptor(itemBlock.tree.node->id()); + int h = 0, flags = 0; + +#ifdef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + bool do_paint_children = false; +#endif + + if ((EASY_GLOBALS.hide_narrow_children && w < EASY_GLOBALS.blocks_narrow_size) || !itemBlock.expanded) + { + // Items which width is less than 20 will be painted as big rectangles which are hiding it's children + + //x = item.left() * p.currentScale - p.dx; + h = totalHeight; + const auto dh = top + h - p.visibleBottom; + if (dh > 0) + h -= dh; + + if (item.block == EASY_GLOBALS.selected_block) + p.selectedItemsWasPainted = true; + + const bool colorChange = (p.previousColor != itemDesc.color()); + if (colorChange) + { + // Set background color brush for rectangle + p.previousColor = itemDesc.color(); + //p.inverseColor = 0xffffffff - p.previousColor; + p.is_light = ::profiler_gui::isLightColor(p.previousColor); + p.textColor = ::profiler_gui::textColorForFlag(p.is_light); + p.brush.setColor(p.previousColor); + _painter->setBrush(p.brush); + } + + if (EASY_GLOBALS.highlight_blocks_with_same_id && (EASY_GLOBALS.selected_block_id == itemBlock.tree.node->id() + || (::profiler_gui::is_max(EASY_GLOBALS.selected_block) && EASY_GLOBALS.selected_block_id == itemDesc.id()))) + { + if (p.previousPenStyle != Qt::DotLine) + { + p.previousPenStyle = Qt::DotLine; + _painter->setPen(HIGHLIGHTER_PEN); + } + } + else if (EASY_GLOBALS.draw_graphics_items_borders) + { + if (p.previousPenStyle != Qt::SolidLine)// || colorChange) + { + // Restore pen for item which is wide enough to paint borders + p.previousPenStyle = Qt::SolidLine; + _painter->setPen(BORDERS_COLOR);//BORDERS_COLOR & inverseColor); + } + } + else if (p.previousPenStyle != Qt::NoPen) + { + p.previousPenStyle = Qt::NoPen; + _painter->setPen(Qt::NoPen); + } + + const auto wprev = w; + decltype(w) dw = 0; + if (item.left() < p.sceneLeft) + { + // if item left border is out of screen then attach text to the left border of the screen + // to ensure text is always visible for items presenting on the screen. + w += (item.left() - p.sceneLeft) * p.currentScale; + x = p.sceneLeft * p.currentScale - p.dx - 2; + w += 2; + dw = 2; + } + + if (item.right() > p.sceneRight) + { + w -= (item.right() - p.sceneRight) * p.currentScale; + w += 2; + dw += 2; + } + + if (w < EASY_GLOBALS.blocks_size_min) + w = EASY_GLOBALS.blocks_size_min; + + // Draw rectangle + p.rect.setRect(x, top, w, h); + _painter->drawRect(p.rect); + + prevRight = p.rect.right() + EASY_GLOBALS.blocks_spacing; + //skip_children(next_level, item.children_begin); + if (wprev < EASY_GLOBALS.blocks_narrow_size) + continue; + + if (totalHeight > ::profiler_gui::GRAPHICS_ROW_SIZE) + flags = Qt::AlignCenter; + else if (!(item.width() < 1)) + flags = Qt::AlignHCenter; + + if (dw > 1) { + w -= dw; + x += 2; + } + } + else + { + if (item.block == EASY_GLOBALS.selected_block) + p.selectedItemsWasPainted = true; + + const bool colorChange = (p.previousColor != itemDesc.color()); + if (colorChange) + { + // Set background color brush for rectangle + p.previousColor = itemDesc.color(); + //p.inverseColor = 0xffffffff - p.previousColor; + p.is_light = ::profiler_gui::isLightColor(p.previousColor); + p.textColor = ::profiler_gui::textColorForFlag(p.is_light); + p.brush.setColor(p.previousColor); + _painter->setBrush(p.brush); + } + + if (EASY_GLOBALS.highlight_blocks_with_same_id && (EASY_GLOBALS.selected_block_id == itemBlock.tree.node->id() + || (::profiler_gui::is_max(EASY_GLOBALS.selected_block) && EASY_GLOBALS.selected_block_id == itemDesc.id()))) + { + if (p.previousPenStyle != Qt::DotLine) + { + p.previousPenStyle = Qt::DotLine; + _painter->setPen(HIGHLIGHTER_PEN); + } + } + else if (EASY_GLOBALS.draw_graphics_items_borders) + { + if (p.previousPenStyle != Qt::SolidLine)// || colorChange) + { + // Restore pen for item which is wide enough to paint borders + p.previousPenStyle = Qt::SolidLine; + _painter->setPen(BORDERS_COLOR);// BORDERS_COLOR & inverseColor); + } + } + else if (p.previousPenStyle != Qt::NoPen) + { + p.previousPenStyle = Qt::NoPen; + _painter->setPen(Qt::NoPen); + } + + // Draw rectangle + //x = item.left() * currentScale - p.dx; + h = ::profiler_gui::GRAPHICS_ROW_SIZE; + const auto dh = top + h - p.visibleBottom; + if (dh > 0) + h -= dh; + + const auto wprev = w; + decltype(w) dw = 0; + if (item.left() < p.sceneLeft) + { + // if item left border is out of screen then attach text to the left border of the screen + // to ensure text is always visible for items presenting on the screen. + w += (item.left() - p.sceneLeft) * p.currentScale; + x = p.sceneLeft * p.currentScale - p.dx - 2; + w += 2; + dw = 2; + } + + if (item.right() > p.sceneRight) + { + w -= (item.right() - p.sceneRight) * p.currentScale; + w += 2; + dw += 2; + } + + if (w < EASY_GLOBALS.blocks_size_min) + w = EASY_GLOBALS.blocks_size_min; + + p.rect.setRect(x, top, w, h); + _painter->drawRect(p.rect); + + prevRight = p.rect.right() + EASY_GLOBALS.blocks_spacing; + if (wprev < EASY_GLOBALS.blocks_narrow_size) + { +#ifndef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + dont_skip_children(next_level, item.children_begin, wprev < narrow_size_half ? BLOCK_ITEM_DO_PAINT_FIRST : BLOCK_ITEM_DO_PAINT); +#else + paintChildren(MIN_WIDTH, narrow_size_half, levelsNumber, _painter, p, item, itemBlock, m_rightBounds, next_level, wprev < narrow_size_half ? BLOCK_ITEM_DO_PAINT_FIRST : BLOCK_ITEM_DO_PAINT); +#endif + continue; + } + +#ifndef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + dont_skip_children(next_level, item.children_begin, BLOCK_ITEM_DO_PAINT); +#endif + if (!(item.width() < 1)) + flags = Qt::AlignHCenter; + + if (dw > 1) { + w -= dw; + x += 2; + } + +#ifdef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + do_paint_children = true; +#endif + } + + // Draw text----------------------------------- + p.rect.setRect(x + 1, top, w - 1, h); + + // text will be painted with inverse color + //auto textColor = inverseColor < 0x00808080 ? profiler::colors::Black : profiler::colors::White; + //if (textColor == previousColor) textColor = 0; + _painter->setPen(p.textColor); + + if (item.block == EASY_GLOBALS.selected_block) + _painter->setFont(SELECTED_ITEM_FONT); + + // drawing text + auto name = *itemBlock.tree.node->name() != 0 ? itemBlock.tree.node->name() : itemDesc.name(); + _painter->drawText(p.rect, flags, ::profiler_gui::toUnicode(name)); + + // restore previous pen color + if (p.previousPenStyle == Qt::NoPen) + _painter->setPen(Qt::NoPen); + else if (p.previousPenStyle == Qt::DotLine) + { + _painter->setPen(HIGHLIGHTER_PEN); + } + else + _painter->setPen(BORDERS_COLOR);// BORDERS_COLOR & inverseColor); // restore pen for rectangle painting + + // restore font + if (item.block == EASY_GLOBALS.selected_block) + _painter->setFont(ITEMS_FONT); + // END Draw text~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +#ifdef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + if (do_paint_children) + paintChildren(MIN_WIDTH, narrow_size_half, levelsNumber, _painter, p, item, itemBlock, m_rightBounds, next_level, BLOCK_ITEM_DO_PAINT); +#endif + } + } + + if (EASY_GLOBALS.selected_block < EASY_GLOBALS.gui_blocks.size()) + { + const auto& guiblock = EASY_GLOBALS.gui_blocks[EASY_GLOBALS.selected_block]; + if (guiblock.graphics_item == m_index) + { + const auto& item = m_levels[guiblock.graphics_item_level][guiblock.graphics_item_index]; + if (item.left() < p.sceneRight && item.right() > p.sceneLeft) + { + const auto& itemBlock = easyBlock(item.block); + const auto item_width = ::std::max(item.width(), MIN_WIDTH); + auto top = levelY(guiblock.graphics_item_level); + auto w = ::std::max(item_width * p.currentScale, 1.0); + decltype(top) h = (!itemBlock.expanded || + (w < EASY_GLOBALS.blocks_narrow_size && EASY_GLOBALS.hide_narrow_children)) + ? (itemBlock.tree.depth * ::profiler_gui::GRAPHICS_ROW_SIZE_FULL + ::profiler_gui::GRAPHICS_ROW_SIZE) + : ::profiler_gui::GRAPHICS_ROW_SIZE; + + auto dh = top + h - p.visibleBottom; + if (dh < h) + { + if (dh > 0) + h -= dh; + + const auto& itemDesc = easyDescriptor(itemBlock.tree.node->id()); + + QPen pen(Qt::SolidLine); + pen.setJoinStyle(Qt::MiterJoin); + pen.setColor(selectedItemBorderColor(itemDesc.color()));//Qt::red); + pen.setWidth(3); + _painter->setPen(pen); + + if (!p.selectedItemsWasPainted) + { + p.brush.setColor(itemDesc.color());// SELECTED_ITEM_COLOR); + _painter->setBrush(p.brush); + } + else + { + _painter->setBrush(Qt::NoBrush); + } + + auto x = item.left() * p.currentScale - p.dx; + decltype(w) dw = 0; + if (item.left() < p.sceneLeft) + { + // if item left border is out of screen then attach text to the left border of the screen + // to ensure text is always visible for items presenting on the screen. + w += (item.left() - p.sceneLeft) * p.currentScale; + x = p.sceneLeft * p.currentScale - p.dx - 2; + w += 2; + dw = 2; + } + + if (item.right() > p.sceneRight) + { + w -= (item.right() - p.sceneRight) * p.currentScale; + w += 2; + dw += 2; + } + + p.rect.setRect(x, top, w, h); + _painter->drawRect(p.rect); + + if (!p.selectedItemsWasPainted && w > EASY_GLOBALS.blocks_narrow_size) + { + if (dw > 1) { + w -= dw; + x += 2; + } + + // Draw text----------------------------------- + p.rect.setRect(x + 1, top, w - 1, h); + + // text will be painted with inverse color + //auto textColor = 0x00ffffff - previousColor; + //if (textColor == previousColor) textColor = 0; + p.textColor = ::profiler_gui::textColorForRgb(itemDesc.color());// SELECTED_ITEM_COLOR); + _painter->setPen(p.textColor); + + _painter->setFont(SELECTED_ITEM_FONT); + + // drawing text + auto name = *itemBlock.tree.node->name() != 0 ? itemBlock.tree.node->name() : itemDesc.name(); + _painter->drawText(p.rect, Qt::AlignCenter, ::profiler_gui::toUnicode(name)); + // END Draw text~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + } + } + } + } + } + + //printf("%u: %llu\n", m_index, iterations); + } + + + + if (gotSync) + { + const auto sceneView = view(); + auto firstSync = ::std::lower_bound(m_pRoot->sync.begin(), m_pRoot->sync.end(), p.sceneLeft, [&sceneView](::profiler::block_index_t _index, qreal _value) + { + return sceneView->time2position(blocksTree(_index).node->begin()) < _value; + }); + + if (firstSync != m_pRoot->sync.end()) + { + if (firstSync != m_pRoot->sync.begin()) + --firstSync; + } + else if (!m_pRoot->sync.empty()) + { + firstSync = m_pRoot->sync.begin() + m_pRoot->sync.size() - 1; + } + //firstSync = m_pRoot->sync.begin(); + + p.previousColor = 0; + qreal prevRight = -1e100, top = y() - 4, h = 3; + if (top + h < p.visibleBottom) + { + _painter->setPen(BORDERS_COLOR); + + for (auto it = firstSync, end = m_pRoot->sync.end(); it != end; ++it) + { + const auto& item = blocksTree(*it); + auto left = sceneView->time2position(item.node->begin()); + + if (left > p.sceneRight) + break; // This is first totally invisible item. No need to check other items. + + decltype(left) width = sceneView->time2position(item.node->end()) - left; + if (left + width < p.sceneLeft) // This item is not visible + continue; + + left *= p.currentScale; + left -= p.dx; + width *= p.currentScale; + if (left + width <= prevRight) // This item is not visible + continue; + + if (left < prevRight) + { + width -= prevRight - left; + left = prevRight; + } + + if (width < MIN_SYNC_SIZE) + width = MIN_SYNC_SIZE; + + const ::profiler::thread_id_t tid = EASY_GLOBALS.version < ::profiler_gui::V130 ? item.node->id() : item.cs->tid(); + const bool self_thread = tid != 0 && EASY_GLOBALS.profiler_blocks.find(tid) != EASY_GLOBALS.profiler_blocks.end(); + + ::profiler::color_t color = 0; + if (self_thread) + color = ::profiler::colors::Coral; + else if (item.node->id() == 0) + color = ::profiler::colors::Black; + else + color = ::profiler::colors::RedA400; + + if (p.previousColor != color) + { + p.previousColor = color; + _painter->setBrush(QColor::fromRgb(color)); + } + + p.rect.setRect(left, top, width, h); + _painter->drawRect(p.rect); + prevRight = left + width + MIN_SYNC_SPACING; + } + } + } + + + + if (EASY_GLOBALS.enable_event_markers && !m_pRoot->events.empty()) + { + const auto sceneView = view(); + auto first = ::std::lower_bound(m_pRoot->events.begin(), m_pRoot->events.end(), p.offset, [&sceneView](::profiler::block_index_t _index, qreal _value) + { + return sceneView->time2position(blocksTree(_index).node->begin()) < _value; + }); + + if (first != m_pRoot->events.end()) + { + if (first != m_pRoot->events.begin()) + --first; + } + else if (!m_pRoot->events.empty()) + { + first = m_pRoot->events.begin() + m_pRoot->events.size() - 1; + } + + p.previousColor = 0; + qreal prevRight = -1e100, top = y() + boundingRect().height() - 1, h = 3; + if (top + h < p.visibleBottom) + { + _painter->setPen(BORDERS_COLOR); + + for (auto it = first, end = m_pRoot->events.end(); it != end; ++it) + { + const auto& item = blocksTree(*it); + auto left = sceneView->time2position(item.node->begin()); + + if (left > p.sceneRight) + break; // This is first totally invisible item. No need to check other items. + + decltype(left) width = MIN_WIDTH; + if (left + width < p.sceneLeft) // This item is not visible + continue; + + left *= p.currentScale; + left -= p.dx; + width *= p.currentScale; + if (width < 2) width = 2; + + if (left + width <= prevRight) // This item is not visible + continue; + + if (left < prevRight) + { + width -= prevRight - left; + left = prevRight; + } + + if (width < 2) + width = 2; + + ::profiler::color_t color = easyDescriptor(item.node->id()).color(); + if (p.previousColor != color) + { + p.previousColor = color; + _painter->setBrush(QColor::fromRgb(color)); + } + + p.rect.setRect(left, top, width, h); + _painter->drawRect(p.rect); + prevRight = left + width + 2; + } + } + } + + + + _painter->restore(); +} + +////////////////////////////////////////////////////////////////////////// + +const ::profiler::BlocksTreeRoot* EasyGraphicsItem::root() const +{ + return m_pRoot; +} + +const QString& EasyGraphicsItem::threadName() const +{ + return m_threadName; +} + +////////////////////////////////////////////////////////////////////////// + +QRect EasyGraphicsItem::getRect() const +{ + return view()->mapFromScene(m_boundingRect).boundingRect(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsItem::getBlocks(qreal _left, qreal _right, ::profiler_gui::TreeBlocks& _blocks) const +{ + // Search for first visible top-level item + auto& level0 = m_levels.front(); + auto first = ::std::lower_bound(level0.begin(), level0.end(), _left, [](const ::profiler_gui::EasyBlockItem& _item, qreal _value) + { + return _item.left() < _value; + }); + + size_t itemIndex = 0; + if (first != level0.end()) + { + itemIndex = first - level0.begin(); + if (itemIndex > 0) + itemIndex -= 1; + } + else + { + itemIndex = level0.size() - 1; + } + + // Add all visible top-level items into array of visible blocks + for (size_t i = itemIndex, end = level0.size(); i < end; ++i) + { + const auto& item = level0[i]; + + if (item.left() > _right) + { + // First invisible item. No need to check other items. + break; + } + + if (item.right() < _left) + { + // This item is not visible yet + // This is just to be sure + continue; + } + + _blocks.emplace_back(m_pRoot, item.block); + } +} + +////////////////////////////////////////////////////////////////////////// + +const ::profiler_gui::EasyBlock* EasyGraphicsItem::intersect(const QPointF& _pos, ::profiler::block_index_t& _blockIndex) const +{ + if (m_levels.empty() || m_levels.front().empty()) + { + return nullptr; + } + + const auto& level0 = m_levels.front(); + const auto top = y(); + + if (top > _pos.y()) + { + return nullptr; + } + + static const auto OVERLAP = ::profiler_gui::THREADS_ROW_SPACING >> 1; + const auto bottom = top + m_levels.size() * ::profiler_gui::GRAPHICS_ROW_SIZE_FULL + OVERLAP; + if (bottom < _pos.y()) + { + return nullptr; + } + + const unsigned int levelIndex = static_cast(_pos.y() - top) / ::profiler_gui::GRAPHICS_ROW_SIZE_FULL; + if (levelIndex >= m_levels.size()) + { + // The Y position is out of blocks range + + if (EASY_GLOBALS.enable_event_markers && !m_pRoot->events.empty()) + { + // If event indicators are enabled then try to intersect with one of event indicators + + const auto& sceneView = view(); + auto first = ::std::lower_bound(m_pRoot->events.begin(), m_pRoot->events.end(), _pos.x(), [&sceneView](::profiler::block_index_t _index, qreal _value) + { + return sceneView->time2position(blocksTree(_index).node->begin()) < _value; + }); + + if (first != m_pRoot->events.end()) + { + if (first != m_pRoot->events.begin()) + --first; + } + else if (!m_pRoot->events.empty()) + { + first = m_pRoot->events.begin() + m_pRoot->events.size() - 1; + } + + const auto MIN_WIDTH = EASY_GLOBALS.enable_zero_length ? 0.f : 0.25f; + const auto currentScale = sceneView->scale(); + const auto dw = 5. / currentScale; + + for (auto it = first, end = m_pRoot->events.end(); it != end; ++it) + { + _blockIndex = *it; + const auto& item = easyBlock(_blockIndex); + auto left = sceneView->time2position(item.tree.node->begin()); + + if (left - dw > _pos.x()) + break; // This is first totally invisible item. No need to check other items. + + decltype(left) width = MIN_WIDTH; + if (left + width + dw < _pos.x()) // This item is not visible + continue; + + return &item; + } + } + + return nullptr; + } + + // The Y position is inside blocks range + + const auto MIN_WIDTH = EASY_GLOBALS.enable_zero_length ? 0.f : 0.25f; + + const auto currentScale = view()->scale(); + const auto dw = 5. / currentScale; + unsigned int i = 0; + size_t itemIndex = ::std::numeric_limits::max(); + size_t firstItem = 0, lastItem = static_cast(level0.size()); + while (i <= levelIndex) + { + const auto& level = m_levels[i]; + + // Search for first visible item + auto first = ::std::lower_bound(level.begin() + firstItem, level.begin() + lastItem, _pos.x(), [](const ::profiler_gui::EasyBlockItem& _item, qreal _value) + { + return _item.left() < _value; + }); + + if (first != level.end()) + { + itemIndex = first - level.begin(); + if (itemIndex != 0) + --itemIndex; + } + else + { + itemIndex = level.size() - 1; + } + + for (auto size = level.size(); itemIndex < size; ++itemIndex) + { + const auto& item = level[itemIndex]; + static const auto MAX_CHILD_INDEX = ::profiler_gui::numeric_max(item.children_begin); + + if (item.left() - dw > _pos.x()) + { + return nullptr; + } + + const auto item_width = ::std::max(item.width(), MIN_WIDTH); + if (item.left() + item_width + dw < _pos.x()) + { + continue; + } + + const auto w = item_width * currentScale; + const auto& guiItem = easyBlock(item.block); + if (i == levelIndex || (w < EASY_GLOBALS.blocks_narrow_size && EASY_GLOBALS.hide_narrow_children) || !guiItem.expanded) + { + _blockIndex = item.block; + return &guiItem; + } + + if (item.children_begin == MAX_CHILD_INDEX) + { + if (itemIndex != 0) + { + auto j = itemIndex; + firstItem = 0; + do { + + --j; + const auto& item2 = level[j]; + if (item2.children_begin != MAX_CHILD_INDEX) + { + firstItem = item2.children_begin; + break; + } + + } while (j != 0); + } + else + { + firstItem = 0; + } + } + else + { + firstItem = item.children_begin; + } + + lastItem = m_levels[i + 1].size(); + for (auto j = itemIndex + 1; j < size; ++j) + { + const auto& item2 = level[j]; + if (item2.children_begin != MAX_CHILD_INDEX) + { + lastItem = item2.children_begin; + break; + } + } + + break; + } + + ++i; + } + + return nullptr; +} + +const ::profiler_gui::EasyBlock* EasyGraphicsItem::intersectEvent(const QPointF& _pos) const +{ + if (m_pRoot->sync.empty()) + { + return nullptr; + } + + const auto top = y() - 6; + + if (top > _pos.y()) + { + return nullptr; + } + + const auto bottom = top + 5; + if (bottom < _pos.y()) + { + return nullptr; + } + + const auto sceneView = view(); + auto firstSync = ::std::lower_bound(m_pRoot->sync.begin(), m_pRoot->sync.end(), _pos.x(), [&sceneView](::profiler::block_index_t _index, qreal _value) + { + return sceneView->time2position(blocksTree(_index).node->begin()) < _value; + }); + + if (firstSync == m_pRoot->sync.end()) + firstSync = m_pRoot->sync.begin() + m_pRoot->sync.size() - 1; + else if (firstSync != m_pRoot->sync.begin()) + --firstSync; + + const auto dw = 4. / view()->scale(); + for (auto it = firstSync, end = m_pRoot->sync.end(); it != end; ++it) + { + const auto& item = easyBlock(*it); + + const auto left = sceneView->time2position(item.tree.node->begin()) - dw; + if (left > _pos.x()) + break; + + const auto right = sceneView->time2position(item.tree.node->end()) + dw; + if (right < _pos.x()) + continue; + + return &item; + } + + return nullptr; +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsItem::setBoundingRect(qreal x, qreal y, qreal w, qreal h) +{ + m_boundingRect.setRect(x, y, w, h); +} + +void EasyGraphicsItem::setBoundingRect(const QRectF& _rect) +{ + m_boundingRect = _rect; +} + +////////////////////////////////////////////////////////////////////////// + +::profiler::thread_id_t EasyGraphicsItem::threadId() const +{ + return m_pRoot->thread_id; +} + +////////////////////////////////////////////////////////////////////////// + +uint8_t EasyGraphicsItem::levels() const +{ + return static_cast(m_levels.size()); +} + +float EasyGraphicsItem::levelY(uint8_t _level) const +{ + return y() + static_cast(_level) * static_cast(::profiler_gui::GRAPHICS_ROW_SIZE_FULL); +} + +void EasyGraphicsItem::setLevels(uint8_t _levels) +{ + typedef decltype(m_levelsIndexes) IndexesT; + static const auto MAX_CHILD_INDEX = ::profiler_gui::numeric_max(); + + m_levels.resize(_levels); + m_levelsIndexes.resize(_levels, MAX_CHILD_INDEX); + m_rightBounds.resize(_levels, -1e100); +} + +void EasyGraphicsItem::reserve(uint8_t _level, unsigned int _items) +{ + m_levels[_level].reserve(_items); +} + +////////////////////////////////////////////////////////////////////////// + +const EasyGraphicsItem::Children& EasyGraphicsItem::items(uint8_t _level) const +{ + return m_levels[_level]; +} + +const ::profiler_gui::EasyBlockItem& EasyGraphicsItem::getItem(uint8_t _level, unsigned int _index) const +{ + return m_levels[_level][_index]; +} + +::profiler_gui::EasyBlockItem& EasyGraphicsItem::getItem(uint8_t _level, unsigned int _index) +{ + return m_levels[_level][_index]; +} + +unsigned int EasyGraphicsItem::addItem(uint8_t _level) +{ + m_levels[_level].emplace_back(); + return static_cast(m_levels[_level].size() - 1); +} + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_item.h b/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_item.h new file mode 100644 index 0000000000..a980e588fe --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_item.h @@ -0,0 +1,195 @@ +/************************************************************************ +* file name : easy_graphics_item.h +* ----------------- : +* creation time : 2016/09/15 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of EasyGraphicsItem - an item +* : used to draw profiler blocks on graphics scene. +* ----------------- : +* change log : * 2016/09/15 Victor Zarubkin: moved sources from blocks_graphics_view.h/.cpp +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_GRAPHICS_ITEM_H +#define EASY_GRAPHICS_ITEM_H + +#include + +#include +#include +#include + +#include + +#include "common_types.h" + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +class EasyGraphicsView; + +class EasyGraphicsItem : public QGraphicsItem +{ + typedef ::profiler_gui::EasyItems Children; + typedef ::std::vector DrawIndexes; + typedef ::std::vector RightBounds; + typedef ::std::vector Sublevels; + + DrawIndexes m_levelsIndexes; ///< Indexes of first item on each level from which we must start painting + RightBounds m_rightBounds; ///< + Sublevels m_levels; ///< Arrays of items for each level + + QRectF m_boundingRect; ///< boundingRect (see QGraphicsItem) + QString m_threadName; ///< + const ::profiler::BlocksTreeRoot* m_pRoot; ///< Pointer to the root profiler block (thread block). Used by ProfTreeWidget to restore hierarchy. + uint8_t m_index; ///< This item's index in the list of items of EasyGraphicsView + +public: + + explicit EasyGraphicsItem(uint8_t _index, const::profiler::BlocksTreeRoot& _root); + virtual ~EasyGraphicsItem(); + + // Public virtual methods + + QRectF boundingRect() const override; + + void paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget = nullptr) override; + +public: + + // Public non-virtual methods + + void validateName(); + + const ::profiler::BlocksTreeRoot* root() const; + const QString& threadName() const; + + QRect getRect() const; + + void setBoundingRect(qreal x, qreal y, qreal w, qreal h); + void setBoundingRect(const QRectF& _rect); + + ::profiler::thread_id_t threadId() const; + + ///< Returns number of levels + uint8_t levels() const; + + float levelY(uint8_t _level) const; + + /** \brief Sets number of levels. + + \note Must be set before doing anything else. + + \param _levels Desired number of levels */ + void setLevels(uint8_t _levels); + + /** \brief Reserves memory for desired number of items on specified level. + + \param _level Index of the level + \param _items Desired number of items on this level */ + void reserve(uint8_t _level, unsigned int _items); + + /**\brief Returns reference to the array of items of specified level. + + \param _level Index of the level */ + const Children& items(uint8_t _level) const; + + /**\brief Returns reference to the item with required index on specified level. + + \param _level Index of the level + \param _index Index of required item */ + const ::profiler_gui::EasyBlockItem& getItem(uint8_t _level, unsigned int _index) const; + + /**\brief Returns reference to the item with required index on specified level. + + \param _level Index of the level + \param _index Index of required item */ + ::profiler_gui::EasyBlockItem& getItem(uint8_t _level, unsigned int _index); + + /** \brief Adds new item to required level. + + \param _level Index of the level + + \retval Index of the new created item */ + unsigned int addItem(uint8_t _level); + + /** \brief Finds top-level blocks which are intersects with required selection zone. + + \note Found blocks will be added into the array of selected blocks. + + \param _left Left bound of the selection zone + \param _right Right bound of the selection zone + \param _blocks Reference to the array of selected blocks */ + void getBlocks(qreal _left, qreal _right, ::profiler_gui::TreeBlocks& _blocks) const; + + const ::profiler_gui::EasyBlock* intersect(const QPointF& _pos, ::profiler::block_index_t& _blockIndex) const; + const ::profiler_gui::EasyBlock* intersectEvent(const QPointF& _pos) const; + +private: + + ///< Returns pointer to the EasyGraphicsView widget. + const EasyGraphicsView* view() const; + +#ifdef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT + void paintChildren(const float _minWidth, const int _narrowSizeHalf, const uint8_t _levelsNumber, QPainter* _painter, struct EasyPainterInformation& p, ::profiler_gui::EasyBlockItem& _item, const ::profiler_gui::EasyBlock& _itemBlock, RightBounds& _rightBounds, uint8_t _level, int8_t _mode); +#endif + +public: + + // Public inline methods + + ///< Returns this item's index in the list of graphics items of EasyGraphicsView + inline uint8_t index() const + { + return m_index; + } + +}; // END of class EasyGraphicsItem. + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_GRAPHICS_ITEM_H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_scrollbar.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_scrollbar.cpp new file mode 100644 index 0000000000..a8de2e03b6 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_scrollbar.cpp @@ -0,0 +1,2084 @@ +/************************************************************************ +* file name : easy_graphics_scrollbar.cpp +* ----------------- : +* creation time : 2016/07/04 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : . +* ----------------- : +* change log : * 2016/07/04 Victor Zarubkin: Initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include "easy_graphics_scrollbar.h" +#include "globals.h" + + +// TODO: use profiler_core/spin_lock.h + +#if defined(_WIN32) && defined(EASY_GUI_USE_CRITICAL_SECTION) +# include +# ifdef min +# undef min +# endif +# ifdef max +# undef max +# endif + +namespace profiler_gui { + void spin_lock::lock() { + EnterCriticalSection((CRITICAL_SECTION*)m_lock); + } + + void spin_lock::unlock() { + LeaveCriticalSection((CRITICAL_SECTION*)m_lock); + } + + spin_lock::spin_lock() : m_lock(new CRITICAL_SECTION) { + InitializeCriticalSection((CRITICAL_SECTION*)m_lock); + } + + spin_lock::~spin_lock() { + DeleteCriticalSection((CRITICAL_SECTION*)m_lock); + delete ((CRITICAL_SECTION*)m_lock); + } +} +#endif + +////////////////////////////////////////////////////////////////////////// + +const int DEFAULT_TOP = -40; +const int DEFAULT_HEIGHT = 80; +const int INDICATOR_SIZE = 6; +const int INDICATOR_SIZE_x2 = INDICATOR_SIZE << 1; +const int HIST_COLUMN_MIN_HEIGHT = 2; +const int WORKER_THREAD_CHECK_INTERVAL = 40; +const int BOUNDARY_TIMER_INTERVAL = 100; + +////////////////////////////////////////////////////////////////////////// + +inline qreal clamp(qreal _minValue, qreal _value, qreal _maxValue) +{ + return (_value < _minValue ? _minValue : (_value > _maxValue ? _maxValue : _value)); +} + +inline qreal sqr(qreal _value) +{ + return _value * _value; +} + +inline qreal calculate_color1(qreal h, qreal, qreal k) +{ + return ::std::min(h * k, 0.9999999); +} + +inline qreal calculate_color2(qreal, qreal duration, qreal k) +{ + return ::std::min(sqr(sqr(duration)) * k, 0.9999999); +} + +////////////////////////////////////////////////////////////////////////// + +EasyGraphicsSliderItem::EasyGraphicsSliderItem(bool _main) : Parent(), m_halfwidth(0), m_bMain(_main) +{ + m_indicator.reserve(3); + + if (_main) + { + m_indicator.push_back(QPointF(0, DEFAULT_TOP + INDICATOR_SIZE)); + m_indicator.push_back(QPointF(-INDICATOR_SIZE, DEFAULT_TOP)); + m_indicator.push_back(QPointF(INDICATOR_SIZE, DEFAULT_TOP)); + } + else + { + m_indicator.push_back(QPointF(0, DEFAULT_TOP + DEFAULT_HEIGHT - INDICATOR_SIZE)); + m_indicator.push_back(QPointF(-INDICATOR_SIZE, DEFAULT_TOP + DEFAULT_HEIGHT)); + m_indicator.push_back(QPointF(INDICATOR_SIZE, DEFAULT_TOP + DEFAULT_HEIGHT)); + } + + setWidth(1); + setBrush(Qt::SolidPattern); +} + +EasyGraphicsSliderItem::~EasyGraphicsSliderItem() +{ + +} + +void EasyGraphicsSliderItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget) +{ + if (static_cast(scene()->parent())->bindMode()) + { + return; + } + + const auto currentScale = static_cast(scene()->parent())->getWindowScale(); + const auto br = rect(); + + qreal w = width() * currentScale; + QRectF r(br.left() * currentScale, br.top() + INDICATOR_SIZE, w, br.height() - INDICATOR_SIZE_x2); + const auto r_right = r.right(); + const auto r_bottom = r.bottom(); + auto b = brush(); + + _painter->save(); + _painter->setTransform(QTransform::fromScale(1.0 / currentScale, 1), true); + _painter->setBrush(b); + + if (w > 1) + { + _painter->setPen(Qt::NoPen); + _painter->drawRect(r); + + // Draw left and right borders + auto cmode = _painter->compositionMode(); + if (m_bMain) _painter->setCompositionMode(QPainter::CompositionMode_Exclusion); + _painter->setPen(QColor::fromRgba(0xe0000000 | b.color().rgb())); + _painter->drawLine(QPointF(r.left(), r.top()), QPointF(r.left(), r_bottom)); + _painter->drawLine(QPointF(r_right, r.top()), QPointF(r_right, r_bottom)); + if (!m_bMain) _painter->setCompositionMode(cmode); + } + else + { + _painter->setPen(QColor::fromRgba(0xe0000000 | b.color().rgb())); + _painter->drawLine(QPointF(r.left(), r.top()), QPointF(r.left(), r_bottom)); + if (m_bMain) _painter->setCompositionMode(QPainter::CompositionMode_Exclusion); + } + + // Draw triangle indicators for small slider + _painter->setTransform(QTransform::fromTranslate(r.left() + w * 0.5, 0), true); + _painter->setPen(b.color().rgb()); + _painter->drawPolygon(m_indicator); + + _painter->restore(); +} + +qreal EasyGraphicsSliderItem::width() const +{ + return m_halfwidth * 2.0; +} + +qreal EasyGraphicsSliderItem::halfwidth() const +{ + return m_halfwidth; +} + +void EasyGraphicsSliderItem::setWidth(qreal _width) +{ + m_halfwidth = _width * 0.5; + setRect(-m_halfwidth, DEFAULT_TOP, _width, DEFAULT_HEIGHT); +} + +void EasyGraphicsSliderItem::setHalfwidth(qreal _halfwidth) +{ + m_halfwidth = _halfwidth; + setRect(-m_halfwidth, DEFAULT_TOP, m_halfwidth * 2.0, DEFAULT_HEIGHT); +} + +void EasyGraphicsSliderItem::setColor(QRgb _color) +{ + setColor(QColor::fromRgba(_color)); +} + +void EasyGraphicsSliderItem::setColor(const QColor& _color) +{ + auto b = brush(); + b.setColor(_color); + setBrush(b); +} + +////////////////////////////////////////////////////////////////////////// + +EasyHistogramItem::EasyHistogramItem() : Parent(nullptr) + , m_threadDuration(0) + , m_threadProfiledTime(0) + , m_threadWaitTime(0) + , m_pSource(nullptr) + , m_workerImage(nullptr) + , m_topDuration(0) + , m_maxDuration(0) + , m_minDuration(0) + , m_imageOrigin(0) + , m_imageScale(1) + , m_workerImageOrigin(0) + , m_workerImageScale(1) + , m_workerTopDuration(0) + , m_workerBottomDuration(0) + , m_blockTotalDuraion(0) + , m_timer(::std::bind(&This::onTimeout, this)) + , m_boundaryTimer([this](){ updateImage(); }, true) + , m_pProfilerThread(nullptr) + , m_threadId(0) + , m_blockId(::profiler_gui::numeric_max()) + , m_timeouts(0) + , m_timeUnits(::profiler_gui::TimeUnits_auto) + , m_regime(Hist_Pointer) + , m_bPermitImageUpdate(false) +{ + m_bReady = ATOMIC_VAR_INIT(false); +} + +EasyHistogramItem::~EasyHistogramItem() +{ + m_bReady.store(true, ::std::memory_order_release); + if (m_workerThread.joinable()) + m_workerThread.join(); + delete m_workerImage; +} + +QRectF EasyHistogramItem::boundingRect() const +{ + return m_boundingRect; +} + +void EasyHistogramItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget) +{ + if (!m_bPermitImageUpdate || (m_regime == Hist_Pointer && m_pSource == nullptr) || (m_regime == Hist_Id && (m_threadId == 0 || ::profiler_gui::is_max(m_blockId)))) + return; + + if (m_regime == Hist_Pointer) + paintByPtr(_painter); + else + paintById(_painter); +} + +void EasyHistogramItem::paintBusyIndicator(QPainter* _painter, qreal _current_scale) +{ + const auto width = m_boundingRect.width() * _current_scale; + const auto h = _painter->fontMetrics().height(); + + _painter->setPen(Qt::black); + _painter->drawText(QRectF(0, m_boundingRect.top(), width, m_boundingRect.height() - h), + Qt::AlignCenter, "Generating image"); + _painter->drawText(QRectF(0, m_boundingRect.top() + h, width, m_boundingRect.height() - h), + Qt::AlignCenter, QString(m_timeouts, QChar('.'))); +} + +void EasyHistogramItem::paintMouseIndicator(QPainter* _painter, qreal _top, qreal _bottom, qreal _width, qreal _height, qreal _top_width, qreal _mouse_y, qreal _delta_time, int _font_h) +{ + if (_font_h != 0 && _top < _mouse_y && _mouse_y < _bottom) + { + const int half_font_h = _font_h >> 1; + + _painter->setPen(Qt::blue); + + const auto mouseStr = ::profiler_gui::timeStringReal(m_timeUnits, m_bottomDuration + _delta_time * (_bottom - _mouse_y) / _height, 3); + qreal mouseIndicatorRight = _width; + if (_mouse_y < _top + half_font_h) + mouseIndicatorRight = _top_width; + + qreal mouseIndicatorLeft = 0; + const QRectF rect(0, _mouse_y - _font_h, _width, _font_h << 1); + if (_mouse_y > _bottom - half_font_h) + { + _painter->drawText(rect, Qt::AlignLeft | Qt::AlignTop, mouseStr); + } + else if (_mouse_y < _top + half_font_h) + { + _painter->drawText(rect, Qt::AlignLeft | Qt::AlignBottom, mouseStr); + } + else + { + _painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, mouseStr); + mouseIndicatorLeft = _painter->fontMetrics().width(mouseStr) + 3; + } + + _painter->drawLine(QLineF(mouseIndicatorLeft, _mouse_y, mouseIndicatorRight, _mouse_y)); + } +} + +void EasyHistogramItem::paintByPtr(QPainter* _painter) +{ + const auto widget = static_cast(scene()->parent()); + const bool bindMode = widget->bindMode(); + const auto currentScale = widget->getWindowScale(); + const auto bottom = m_boundingRect.bottom(); + const auto width = m_boundingRect.width() * currentScale; + const auto dtime = m_topDuration - m_bottomDuration; + const auto maxColumnHeight = m_boundingRect.height(); + const auto coeff = (m_boundingRect.height() - HIST_COLUMN_MIN_HEIGHT) / (dtime > 1e-3 ? dtime : 1.); + + QRectF rect; + QBrush brush(Qt::SolidPattern); + //QRgb previousColor = 0; + + _painter->save(); + _painter->setTransform(QTransform::fromScale(1.0 / currentScale, 1), true); + + if (!m_pSource->empty()) + { + _painter->setPen(Qt::NoPen); + + if (!bindMode) + _painter->drawImage(0, m_boundingRect.top(), m_mainImage); + else + { + const auto range = widget->sliderWidth(); + const auto minimum = widget->value(); + const auto slider_k = widget->range() / range; + + /*if (false)//slider_k < 8) + { + _painter->setTransform(QTransform::fromScale(slider_k, 1), true); + _painter->drawImage((widget->minimum() - minimum) * currentScale, m_boundingRect.top(), m_mainImage); + _painter->setTransform(QTransform::fromScale(1. / slider_k, 1), true); + } + else*/ + { + const auto deltaScale = slider_k / m_imageScale; + _painter->setTransform(QTransform::fromScale(deltaScale, 1), true); + _painter->drawImage((widget->minimum() + m_imageOrigin - minimum) * currentScale * m_imageScale, m_boundingRect.top(), m_mainImage); + _painter->setTransform(QTransform::fromScale(1. / deltaScale, 1), true); + } + + /*if (false) + { + const bool gotFrame = EASY_GLOBALS.frame_time > 1e-6f; + qreal frameCoeff = 1; + if (gotFrame) + { + if (EASY_GLOBALS.frame_time <= m_bottomDuration) + frameCoeff = m_boundingRect.height(); + else + frameCoeff = 0.9 / EASY_GLOBALS.frame_time; + } + + auto const calculate_color = gotFrame ? calculate_color2 : calculate_color1; + auto const k = gotFrame ? sqr(sqr(frameCoeff)) : 1.0 / m_boundingRect.height(); + + const auto& items = *m_pSource; + const auto maximum = minimum + range; + const auto realScale = currentScale * slider_k; + const auto offset = minimum * realScale; + + auto first = ::std::lower_bound(items.begin(), items.end(), minimum, [](const ::profiler_gui::EasyBlockItem& _item, qreal _value) + { + return _item.left() < _value; + }); + + if (first != items.end()) + { + if (first != items.begin()) + --first; + } + else + { + first = items.begin() + items.size() - 1; + } + + qreal previous_x = -1e30, previous_h = -1e30; + for (auto it = first, end = items.end(); it != end; ++it) + { + // Draw rectangle + + if (it->left() > maximum) + break; + + if (it->right() < minimum) + continue; + + const qreal item_x = it->left() * realScale - offset; + const qreal item_w = ::std::max(it->width() * realScale, 1.0); + const qreal item_r = item_x + item_w; + const qreal h = it->width() <= m_bottomDuration ? HIST_COLUMN_MIN_HEIGHT : + (it->width() > m_topDuration ? maxColumnHeight : (HIST_COLUMN_MIN_HEIGHT + (it->width() - m_bottomDuration) * coeff)); + + if (h < previous_h && item_r < previous_x) + continue; + + const auto col = calculate_color(h, it->width(), k); + const auto color = 0x00ffffff & QColor::fromHsvF((1.0 - col) * 0.375, 0.85, 0.85).rgb(); + + if (previousColor != color) + { + // Set background color brush for rectangle + previousColor = color; + brush.setColor(QColor::fromRgba(0xc0000000 | color)); + _painter->setBrush(brush); + } + + rect.setRect(item_x, bottom - h, item_w, h); + _painter->drawRect(rect); + + previous_x = item_r; + previous_h = h; + } + }*/ + } + } + + //if (!m_bReady.load(::std::memory_order_acquire)) + // paintBusyIndicator(_painter, currentScale); + + qreal top_width = width, bottom_width = width; + int font_h = 0; + if (!m_topDurationStr.isEmpty()) + { + rect.setRect(0, m_boundingRect.top() - INDICATOR_SIZE, width - 3, m_boundingRect.height() + INDICATOR_SIZE_x2); + + if (m_timeUnits != EASY_GLOBALS.time_units) + { + m_timeUnits = EASY_GLOBALS.time_units; + m_topDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_topDuration, 3); + m_bottomDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_bottomDuration, 3); + } + + auto fm = _painter->fontMetrics(); + font_h = fm.height(); + //bottom_width -= fm.width(m_bottomDurationStr) + 7; + top_width -= fm.width(m_topDurationStr) + 7; + + _painter->setPen(m_topDuration < m_maxDuration ? Qt::darkRed : Qt::black); + _painter->drawText(rect, Qt::AlignRight | Qt::AlignTop, m_topDurationStr); + + rect.setRect(0, bottom, width - 3, font_h); + _painter->setPen(m_bottomDuration > m_minDuration ? Qt::darkRed : Qt::black); + _painter->drawText(rect, Qt::AlignRight | Qt::AlignTop, m_bottomDurationStr); + } + + _painter->setPen(Qt::darkGray); + _painter->drawLine(QLineF(0, bottom, bottom_width, bottom)); + _painter->drawLine(QLineF(0, m_boundingRect.top(), top_width, m_boundingRect.top())); + + paintMouseIndicator(_painter, m_boundingRect.top(), bottom, width, maxColumnHeight - HIST_COLUMN_MIN_HEIGHT, top_width, m_mouseY, dtime, font_h); + + if (m_bottomDuration < EASY_GLOBALS.frame_time && EASY_GLOBALS.frame_time < m_topDuration) + { + // Draw marker displaying expected frame_time step + const auto h = bottom - (EASY_GLOBALS.frame_time - m_bottomDuration) * coeff; + _painter->setPen(Qt::DashLine); + + auto w = width; + const auto boundary = INDICATOR_SIZE - font_h; + if (h < (m_boundingRect.top() - boundary)) + w = top_width; + else if (h > (bottom + boundary)) + w = bottom_width; + + _painter->drawLine(QLineF(0, h, w, h)); + } + + _painter->setPen(Qt::black); + rect.setRect(0, bottom + 2, width, widget->defaultFontHeight()); + const auto eventsSize = m_pProfilerThread->events.size(); + _painter->drawText(rect, Qt::AlignHCenter | Qt::TextDontClip, QString("%1 | duration: %2 | profiled: %3 (%4%) | wait: %5 (%6%) | %7 frames | %8 blocks | %9 markers") + .arg(m_threadName) + .arg(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, m_threadDuration)) + .arg(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, m_threadProfiledTime)) + .arg(m_threadDuration ? QString::number(100. * (double)m_threadProfiledTime / (double)m_threadDuration, 'f', 2) : QString("0")) + .arg(::profiler_gui::timeStringRealNs(EASY_GLOBALS.time_units, m_threadWaitTime)) + .arg(m_threadDuration ? QString::number(100. * (double)m_threadWaitTime / (double)m_threadDuration, 'f', 2) : QString("0")) + .arg(m_pProfilerThread->frames_number) + .arg(m_pProfilerThread->blocks_number - eventsSize) + .arg(eventsSize)); + + _painter->drawText(rect, Qt::AlignLeft, bindMode ? " MODE: zoom" : " MODE: overview"); + + _painter->restore(); +} + +void EasyHistogramItem::paintById(QPainter* _painter) +{ + const auto widget = static_cast(scene()->parent()); + const bool bindMode = widget->bindMode(); + const auto currentScale = widget->getWindowScale(); + const auto bottom = m_boundingRect.bottom(); + const auto width = m_boundingRect.width() * currentScale; + const auto dtime = m_topDuration - m_bottomDuration; + const auto maxColumnHeight = m_boundingRect.height(); + const auto coeff = (m_boundingRect.height() - HIST_COLUMN_MIN_HEIGHT) / (dtime > 1e-3 ? dtime : 1.); + + QRectF rect; + QBrush brush(Qt::SolidPattern); + //QRgb previousColor = 0; + + _painter->save(); + _painter->setTransform(QTransform::fromScale(1.0 / currentScale, 1), true); + + const auto& items = m_selectedBlocks; + if (!items.empty()) + { + _painter->setPen(Qt::NoPen); + + if (!bindMode) + _painter->drawImage(0, m_boundingRect.top(), m_mainImage); + else + { + const auto range = widget->sliderWidth(); + auto minimum = widget->value(); + const auto slider_k = widget->range() / range; + + /*if (false)//slider_k < 8) + { + _painter->setTransform(QTransform::fromScale(slider_k, 1), true); + _painter->drawImage((widget->minimum() - minimum) * currentScale, m_boundingRect.top(), m_mainImage); + _painter->setTransform(QTransform::fromScale(1. / slider_k, 1), true); + } + else*/ + { + const auto deltaScale = slider_k / m_imageScale; + _painter->setTransform(QTransform::fromScale(deltaScale, 1), true); + _painter->drawImage((widget->minimum() + m_imageOrigin - minimum) * currentScale * m_imageScale, m_boundingRect.top(), m_mainImage); + _painter->setTransform(QTransform::fromScale(1. / deltaScale, 1), true); + } + + /*if (false) + { + minimum *= 1e3; + const auto maximum = minimum + range * 1e3; + const auto realScale = currentScale * slider_k; + const auto offset = minimum * realScale; + + auto first = ::std::lower_bound(items.begin(), items.end(), minimum + EASY_GLOBALS.begin_time, [](::profiler::block_index_t _item, qreal _value) + { + return easyBlock(_item).tree.node->begin() < _value; + }); + + if (first != items.end()) + { + if (first != items.begin()) + --first; + } + else + { + first = items.begin() + (items.size() - 1); + } + + auto last = ::std::upper_bound(first, items.end(), maximum + EASY_GLOBALS.begin_time, [](qreal _value, ::profiler::block_index_t _item) + { + return _value < easyBlock(_item).tree.node->begin(); + }); + + const auto n = static_cast(::std::distance(first, last)); + + if (n > 0) + { + const bool gotFrame = EASY_GLOBALS.frame_time > 1e-6f; + qreal frameCoeff = 1; + if (gotFrame) + { + if (EASY_GLOBALS.frame_time <= m_bottomDuration) + frameCoeff = m_boundingRect.height(); + else + frameCoeff = 0.9 / EASY_GLOBALS.frame_time; + } + + auto const calculate_color = gotFrame ? calculate_color2 : calculate_color1; + auto const k = gotFrame ? sqr(sqr(frameCoeff)) : 1.0 / m_boundingRect.height(); + + const auto draw = [this, &previousColor, &brush, &_painter](qreal x, qreal y, qreal w, qreal h, QRgb color) + { + m_spin.lock(); + + if (previousColor != color) + { + // Set background color brush for rectangle + previousColor = color; + brush.setColor(QColor::fromRgba(0xc0000000 | color)); + _painter->setBrush(brush); + } + + _painter->drawRect(QRectF(x, y, w, h)); + + m_spin.unlock(); + }; + + ::std::vector<::std::thread> threads; + const auto n_threads = ::std::min(n, ::std::thread::hardware_concurrency()); + threads.reserve(n_threads); + const auto n_items = n / n_threads; + for (uint32_t i = 0; i < n_threads; ++i) + { + auto begin = first + i * n_items; + threads.emplace_back([this, &draw, &maximum, &minimum, &realScale, &offset, &coeff, &calculate_color, &k, &bottom, &maxColumnHeight](decltype(begin) it, decltype(begin) end) + { + qreal previous_x = -1e30, previous_h = -1e30; + + //for (auto it = first, end = items.end(); it != end; ++it) + for (; it != end; ++it) + { + // Draw rectangle + const auto item = easyBlock(*it).tree.node; + + const auto beginTime = item->begin() - EASY_GLOBALS.begin_time; + if (beginTime > maximum) + break; + + const auto endTime = item->end() - EASY_GLOBALS.begin_time; + if (endTime < minimum) + continue; + + const qreal duration = item->duration() * 1e-3; + const qreal item_x = (beginTime * realScale - offset) * 1e-3; + const qreal item_w = ::std::max(duration * realScale, 1.0); + const qreal item_r = item_x + item_w; + const qreal h = duration <= m_bottomDuration ? HIST_COLUMN_MIN_HEIGHT : + (duration > m_topDuration ? maxColumnHeight : (HIST_COLUMN_MIN_HEIGHT + (duration - m_bottomDuration) * coeff)); + + if (h < previous_h && item_r < previous_x) + continue; + + const auto col = calculate_color(h, duration, k); + const auto color = 0x00ffffff & QColor::fromHsvF((1.0 - col) * 0.375, 0.85, 0.85).rgb(); + + draw(item_x, bottom - h, item_w, h, color); + //if (previousColor != color) + //{ + // // Set background color brush for rectangle + // previousColor = color; + // brush.setColor(QColor::fromRgba(0xc0000000 | color)); + // _painter->setBrush(brush); + //} + + //rect.setRect(item_x, bottom - h, item_w, h); + //_painter->drawRect(rect); + + previous_x = item_r; + previous_h = h; + } + }, begin, i == (n_threads - 1) ? items.end() : begin + n_items); + } + + for (auto& t : threads) + t.join(); + } + }*/ + } + } + + //if (!m_bReady.load(::std::memory_order_acquire)) + // paintBusyIndicator(_painter, currentScale); + + qreal top_width = width, bottom_width = width; + int font_h = 0; + if (!m_topDurationStr.isEmpty()) + { + rect.setRect(0, m_boundingRect.top() - INDICATOR_SIZE, width - 3, m_boundingRect.height() + INDICATOR_SIZE_x2); + + if (m_timeUnits != EASY_GLOBALS.time_units) + { + m_timeUnits = EASY_GLOBALS.time_units; + m_topDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_topDuration, 3); + m_bottomDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_bottomDuration, 3); + } + + auto fm = _painter->fontMetrics(); + font_h = fm.height(); + //bottom_width -= fm.width(m_bottomDurationStr) + 7; + top_width -= fm.width(m_topDurationStr) + 7; + + _painter->setPen(Qt::black); + _painter->setPen(m_topDuration < m_maxDuration ? Qt::darkRed : Qt::black); + _painter->drawText(rect, Qt::AlignRight | Qt::AlignTop, m_topDurationStr); + + rect.setRect(0, bottom, width - 3, font_h); + _painter->setPen(m_bottomDuration > m_minDuration ? Qt::darkRed : Qt::black); + _painter->drawText(rect, Qt::AlignRight | Qt::AlignTop, m_bottomDurationStr); + } + + _painter->setPen(Qt::darkGray); + _painter->drawLine(QLineF(0, bottom, bottom_width, bottom)); + _painter->drawLine(QLineF(0, m_boundingRect.top(), top_width, m_boundingRect.top())); + + paintMouseIndicator(_painter, m_boundingRect.top(), bottom, width, maxColumnHeight - HIST_COLUMN_MIN_HEIGHT, top_width, m_mouseY, dtime, font_h); + + if (m_bottomDuration < EASY_GLOBALS.frame_time && EASY_GLOBALS.frame_time < m_topDuration) + { + // Draw marker displaying required frame_time step + const auto h = bottom - (EASY_GLOBALS.frame_time - m_bottomDuration) * coeff; + _painter->setPen(Qt::DashLine); + + auto w = width; + const auto boundary = INDICATOR_SIZE - font_h; + if (h < (m_boundingRect.top() - boundary)) + w = top_width; + else if (h >(bottom + boundary)) + w = bottom_width; + + _painter->drawLine(QLineF(0, h, w, h)); + } + + _painter->setPen(Qt::black); + rect.setRect(0, bottom + 2, width, widget->defaultFontHeight()); + + if (!m_selectedBlocks.empty()) + { + _painter->drawText(rect, Qt::AlignHCenter | Qt::TextDontClip, QString("%1 | %2 | %3 calls | %4% of thread profiled time") + .arg(m_threadName).arg(m_blockName).arg(m_selectedBlocks.size()) + .arg(m_threadProfiledTime ? QString::number(100. * (double)m_blockTotalDuraion / (double)m_threadProfiledTime, 'f', 2) : QString("100"))); + } + else + { + _painter->drawText(rect, Qt::AlignHCenter | Qt::TextDontClip, QString("%1 | %2 | 0 calls").arg(m_threadName).arg(m_blockName)); + } + + _painter->drawText(rect, Qt::AlignLeft, bindMode ? " MODE: zoom" : " MODE: overview"); + + _painter->restore(); +} + +::profiler::thread_id_t EasyHistogramItem::threadId() const +{ + return m_threadId; +} + +void EasyHistogramItem::setBoundingRect(const QRectF& _rect) +{ + m_boundingRect = _rect; +} + +void EasyHistogramItem::setBoundingRect(qreal x, qreal y, qreal w, qreal h) +{ + m_boundingRect.setRect(x, y, w, h); +} + +void EasyHistogramItem::rebuildSource(HistRegime _regime) +{ + if (m_regime == _regime) + rebuildSource(); +} + +void EasyHistogramItem::rebuildSource() +{ + if (m_regime == Hist_Id) + { + m_regime = Hist_Pointer; + setSource(m_threadId, m_blockId); + } + else + { + m_regime = Hist_Id; + setSource(m_threadId, m_pSource); + } +} + +void EasyHistogramItem::setSource(::profiler::thread_id_t _thread_id, const ::profiler_gui::EasyItems* _items) +{ + if (m_regime == Hist_Pointer && m_threadId == _thread_id && m_pSource == _items) + return; + + m_timer.stop(); + m_boundaryTimer.stop(); + + m_bReady.store(true, ::std::memory_order_release); + if (m_workerThread.joinable()) + m_workerThread.join(); + + m_blockName.clear(); + m_blockTotalDuraion = 0; + + delete m_workerImage; + m_workerImage = nullptr; + m_imageOriginUpdate = m_imageOrigin = 0; + m_imageScaleUpdate = m_imageScale = 1; + + m_selectedBlocks.clear(); + { ::profiler::BlocksTree::children_t().swap(m_selectedBlocks); } + + m_bPermitImageUpdate = false; + m_regime = Hist_Pointer; + m_pSource = _items; + m_threadId = _thread_id; + ::profiler_gui::set_max(m_blockId); + + if (m_pSource != nullptr) + { + if (m_pSource->empty()) + { + m_pSource = nullptr; + } + else + { + const auto& root = EASY_GLOBALS.profiler_blocks[_thread_id]; + m_threadName = ::profiler_gui::decoratedThreadName(EASY_GLOBALS.use_decorated_thread_name, root, EASY_GLOBALS.hex_thread_id); + + if (root.children.empty()) + m_threadDuration = 0; + else + m_threadDuration = easyBlock(root.children.back()).tree.node->end() - easyBlock(root.children.front()).tree.node->begin(); + + m_threadProfiledTime = root.profiled_time; + m_threadWaitTime = root.wait_time; + m_pProfilerThread = &root; + m_timeUnits = EASY_GLOBALS.time_units; + + m_bReady.store(false, ::std::memory_order_release); + m_workerThread = ::std::thread([this](const ::profiler_gui::EasyItems* _source) + { + m_maxDuration = 0; + m_minDuration = 1e30; + + bool empty = true; + for (const auto& item : *_source) + { + if (m_bReady.load(::std::memory_order_acquire)) + return; + + if (easyDescriptor(easyBlock(item.block).tree.node->id()).type() == ::profiler::BLOCK_TYPE_EVENT) + continue; + + const auto w = item.width(); + + if (w > m_maxDuration) + m_maxDuration = w; + + if (w < m_minDuration) + m_minDuration = w; + + empty = false; + } + + if ((m_maxDuration - m_minDuration) < 1e-3) + { + if (m_minDuration > 0.1) + { + m_minDuration -= 0.1; + } + else + { + m_maxDuration = 0.1; + m_minDuration = 0; + } + } + + m_topDuration = m_maxDuration; + m_bottomDuration = m_minDuration; + + if (!empty) + { + m_topDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_topDuration, 3); + m_bottomDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_bottomDuration, 3); + } + else + { + m_topDurationStr.clear(); + m_bottomDurationStr.clear(); + } + + m_bReady.store(true, ::std::memory_order_release); + + }, m_pSource); + + m_timeouts = 3; + m_timer.start(WORKER_THREAD_CHECK_INTERVAL); + show(); + } + } + + if (m_pSource == nullptr) + { + m_pProfilerThread = nullptr; + m_topDurationStr.clear(); + m_bottomDurationStr.clear(); + m_threadName.clear(); + hide(); + } +} + +void EasyHistogramItem::setSource(::profiler::thread_id_t _thread_id, ::profiler::block_id_t _block_id) +{ + if (m_regime == Hist_Id && m_threadId == _thread_id && m_blockId == _block_id) + return; + + m_bPermitImageUpdate = false; // Set to false because m_workerThread have to parse input data first. This will be set to true when m_workerThread finish - see onTimeout() + m_regime = Hist_Id; + + m_timer.stop(); + m_boundaryTimer.stop(); + + m_bReady.store(true, ::std::memory_order_release); + if (m_workerThread.joinable()) + m_workerThread.join(); + + m_pSource = nullptr; + m_topDurationStr.clear(); + m_bottomDurationStr.clear(); + m_blockName.clear(); + m_blockTotalDuraion = 0; + + delete m_workerImage; + m_workerImage = nullptr; + m_imageOriginUpdate = m_imageOrigin = 0; + m_imageScaleUpdate = m_imageScale = 1; + + m_selectedBlocks.clear(); + { ::profiler::BlocksTree::children_t().swap(m_selectedBlocks); } + + m_threadId = _thread_id; + m_blockId = _block_id; + + if (m_threadId != 0 && !::profiler_gui::is_max(m_blockId)) + { + m_blockName = ::profiler_gui::toUnicode(easyDescriptor(m_blockId).name()); + + const auto& root = EASY_GLOBALS.profiler_blocks[_thread_id]; + m_threadName = ::profiler_gui::decoratedThreadName(EASY_GLOBALS.use_decorated_thread_name, root, EASY_GLOBALS.hex_thread_id); + m_pProfilerThread = &root; + m_timeUnits = EASY_GLOBALS.time_units; + + if (root.children.empty()) + { + m_threadDuration = 0; + m_threadProfiledTime = 0; + m_threadWaitTime = 0; + + m_topDuration = m_maxDuration = 0; + m_bottomDuration = m_minDuration = 1e30; + + m_bPermitImageUpdate = true; + + m_bReady.store(true, ::std::memory_order_release); + } + else + { + m_threadDuration = easyBlock(root.children.back()).tree.node->end() - easyBlock(root.children.front()).tree.node->begin(); + m_threadProfiledTime = root.profiled_time; + m_threadWaitTime = root.wait_time; + + m_bReady.store(false, ::std::memory_order_release); + m_workerThread = ::std::thread([this](decltype(root) profiler_thread, ::profiler::block_index_t selected_block, bool _showOnlyTopLevelBlocks) + { + typedef ::std::vector<::std::pair<::profiler::block_index_t, ::profiler::block_index_t> > Stack; + + m_maxDuration = 0; + m_minDuration = 1e30; + //const auto& profiler_thread = EASY_GLOBALS.profiler_blocks[m_threadId]; + Stack stack; + stack.reserve(profiler_thread.depth); + + const bool has_selected_block = !::profiler_gui::is_max(selected_block); + + for (auto frame : profiler_thread.children) + { + const auto& frame_block = easyBlock(frame).tree; + if (frame_block.node->id() == m_blockId || (!has_selected_block && m_blockId == easyDescriptor(frame_block.node->id()).id())) + { + m_selectedBlocks.push_back(frame); + + const auto w = frame_block.node->duration(); + if (w > m_maxDuration) + m_maxDuration = w; + + if (w < m_minDuration) + m_minDuration = w; + + m_blockTotalDuraion += w; + } + + if (_showOnlyTopLevelBlocks) + continue; + + stack.push_back(::std::make_pair(frame, 0U)); + while (!stack.empty()) + { + if (m_bReady.load(::std::memory_order_acquire)) + return; + + auto& top = stack.back(); + const auto& top_children = easyBlock(top.first).tree.children; + const auto stack_size = stack.size(); + for (auto end = top_children.size(); top.second < end; ++top.second) + { + if (m_bReady.load(::std::memory_order_acquire)) + return; + + const auto child_index = top_children[top.second]; + const auto& child = easyBlock(child_index).tree; + if (child.node->id() == m_blockId || (!has_selected_block && m_blockId == easyDescriptor(child.node->id()).id())) + { + m_selectedBlocks.push_back(child_index); + + const auto w = child.node->duration(); + if (w > m_maxDuration) + m_maxDuration = w; + + if (w < m_minDuration) + m_minDuration = w; + + m_blockTotalDuraion += w; + } + + if (!child.children.empty()) + { + ++top.second; + stack.push_back(::std::make_pair(child_index, 0U)); + break; + } + } + + if (stack_size == stack.size()) + { + stack.pop_back(); + } + } + } + + if (m_selectedBlocks.empty()) + { + m_topDurationStr.clear(); + m_bottomDurationStr.clear(); + } + else + { + if (has_selected_block) + { + const auto& item = easyBlock(selected_block).tree; + if (*item.node->name() != 0) + m_blockName = ::profiler_gui::toUnicode(item.node->name()); + } + + m_maxDuration *= 1e-3; + m_minDuration *= 1e-3; + + if ((m_maxDuration - m_minDuration) < 1e-3) + { + if (m_minDuration > 0.1) + { + m_minDuration -= 0.1; + } + else + { + m_maxDuration = 0.1; + m_minDuration = 0; + } + } + + m_topDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_maxDuration, 3); + m_bottomDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_minDuration, 3); + } + + + + m_topDuration = m_maxDuration; + m_bottomDuration = m_minDuration; + + m_bReady.store(true, ::std::memory_order_release); + + }, std::ref(root), EASY_GLOBALS.selected_block, EASY_GLOBALS.display_only_frames_on_histogram); + + m_timeouts = 3; + m_timer.start(WORKER_THREAD_CHECK_INTERVAL); + } + + show(); + } + else + { + m_pProfilerThread = nullptr; + m_threadName.clear(); + hide(); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyHistogramItem::validateName() +{ + if (m_threadName.isEmpty()) + return; + m_threadName = ::profiler_gui::decoratedThreadName(EASY_GLOBALS.use_decorated_thread_name, EASY_GLOBALS.profiler_blocks[m_threadId], EASY_GLOBALS.hex_thread_id); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyHistogramItem::onTimeout() +{ + if (!isVisible()) + { + m_timer.stop(); + return; + } + + if (++m_timeouts > 8) + m_timeouts = 3; + + if (m_bReady.load(::std::memory_order_acquire)) + { + m_timer.stop(); + if (!m_bPermitImageUpdate) + { + // Worker thread have finished parsing input data (when setSource(_block_id) was called) + m_bPermitImageUpdate = true; // From now we can update an image + updateImage(); + } + else + { + // Image updated + + if (m_workerThread.joinable()) + m_workerThread.join(); + + m_workerImage->swap(m_mainImage); + delete m_workerImage; + m_workerImage = nullptr; + + m_imageOriginUpdate = m_imageOrigin = m_workerImageOrigin; + m_imageScaleUpdate = m_imageScale = m_workerImageScale; + + if (EASY_GLOBALS.auto_adjust_histogram_height && !m_topDurationStr.isEmpty()) + { + m_topDuration = m_workerTopDuration; + m_bottomDuration = m_workerBottomDuration; + + m_topDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_topDuration, 3); + m_bottomDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_bottomDuration, 3); + } + } + } + + scene()->update(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyHistogramItem::pickTopBoundary(qreal _y) +{ + if (m_bPermitImageUpdate && m_boundingRect.top() < _y && _y < m_boundingRect.bottom() && !m_topDurationStr.isEmpty()) + { + m_topDuration = m_bottomDuration + (m_topDuration - m_bottomDuration) * (m_boundingRect.bottom() - _y) / (m_boundingRect.height() - HIST_COLUMN_MIN_HEIGHT); + m_topDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_topDuration, 3); + m_boundaryTimer.stop(); + updateImage(); + scene()->update(); // to update top-boundary text right now + } +} + +void EasyHistogramItem::increaseTopBoundary() +{ + if (m_bPermitImageUpdate && m_topDuration < m_maxDuration && !m_topDurationStr.isEmpty()) + { + auto step = 0.05 * (m_maxDuration - m_bottomDuration); + if (m_topDuration < (m_bottomDuration + 1.25 * step)) + step = 0.1 * (m_topDuration - m_bottomDuration); + + m_topDuration = std::min(m_maxDuration, m_topDuration + step); + m_topDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_topDuration, 3); + updateImage(); + scene()->update(); // to update top-boundary text right now + + m_boundaryTimer.stop(); + m_boundaryTimer.start(BOUNDARY_TIMER_INTERVAL); + } +} + +void EasyHistogramItem::decreaseTopBoundary() +{ + if (m_bPermitImageUpdate && m_topDuration > m_bottomDuration && !m_topDurationStr.isEmpty()) + { + auto step = 0.05 * (m_maxDuration - m_bottomDuration); + if (m_topDuration < (m_bottomDuration + 1.25 * step)) + step = std::max(0.1 * (m_topDuration - m_bottomDuration), 0.3); + + if (m_topDuration > (m_bottomDuration + 1.25 * step)) + { + m_topDuration = std::max(m_bottomDuration + step, m_topDuration - step); + m_topDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_topDuration, 3); + scene()->update(); // to update top-boundary text right now + + m_boundaryTimer.stop(); + m_boundaryTimer.start(BOUNDARY_TIMER_INTERVAL); + } + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyHistogramItem::pickBottomBoundary(qreal _y) +{ + if (m_bPermitImageUpdate && m_boundingRect.top() < _y && _y < m_boundingRect.bottom() && !m_bottomDurationStr.isEmpty()) + { + m_bottomDuration = m_bottomDuration + (m_topDuration - m_bottomDuration) * (m_boundingRect.bottom() - _y) / (m_boundingRect.height() - HIST_COLUMN_MIN_HEIGHT); + m_bottomDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_bottomDuration, 3); + m_boundaryTimer.stop(); + updateImage(); + scene()->update(); // to update top-boundary text right now + } +} + +void EasyHistogramItem::increaseBottomBoundary() +{ + if (m_bPermitImageUpdate && m_bottomDuration < m_topDuration && !m_bottomDurationStr.isEmpty()) + { + auto step = 0.05 * (m_topDuration - m_minDuration); + if (m_bottomDuration > (m_topDuration - 1.25 * step)) + step = 0.1 * (m_topDuration - m_bottomDuration); + + if (m_bottomDuration < (m_topDuration - 1.25 * step)) + { + m_bottomDuration = std::min(m_topDuration - step, m_bottomDuration + step); + m_bottomDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_bottomDuration, 3); + scene()->update(); // to update bottom-boundary text right now + + m_boundaryTimer.stop(); + m_boundaryTimer.start(BOUNDARY_TIMER_INTERVAL); + } + } +} + +void EasyHistogramItem::decreaseBottomBoundary() +{ + if (m_bPermitImageUpdate && m_bottomDuration > m_minDuration && !m_bottomDurationStr.isEmpty()) + { + auto step = 0.05 * (m_topDuration - m_minDuration); + if (m_bottomDuration > (m_topDuration - 1.25 * step)) + step = std::max(0.1 * (m_topDuration - m_bottomDuration), 0.3); + + m_bottomDuration = std::max(m_minDuration, m_bottomDuration - step); + m_bottomDurationStr = ::profiler_gui::timeStringReal(m_timeUnits, m_bottomDuration, 3); + scene()->update(); // to update top-boundary text right now + + m_boundaryTimer.stop(); + m_boundaryTimer.start(BOUNDARY_TIMER_INTERVAL); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyHistogramItem::setMouseY(qreal _mouseY) +{ + m_mouseY = _mouseY; +} + +void EasyHistogramItem::pickFrameTime(qreal _y) const +{ + if (m_bPermitImageUpdate && m_boundingRect.top() < _y && _y < m_boundingRect.bottom() && !m_topDurationStr.isEmpty()) + { + EASY_GLOBALS.frame_time = m_bottomDuration + (m_topDuration - m_bottomDuration) * (m_boundingRect.bottom() - _y) / (m_boundingRect.height() - HIST_COLUMN_MIN_HEIGHT); + emit EASY_GLOBALS.events.expectedFrameTimeChanged(); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyHistogramItem::onValueChanged() +{ + const auto widget = static_cast(scene()->parent()); + + if (!widget->bindMode()) + return; + + m_boundaryTimer.stop(); + + const auto sliderWidth_inv = 1.0 / widget->sliderWidth(); + const auto k = widget->range() * sliderWidth_inv; + + const auto deltaScale = m_imageScaleUpdate < k ? (k / m_imageScaleUpdate) : (m_imageScaleUpdate / k); + if (deltaScale > 4) { + updateImage(); + return; + } + + const auto deltaOffset = (widget->value() - m_imageOriginUpdate) * sliderWidth_inv; + if (deltaOffset < 1.5 || deltaOffset > 4.5) { + updateImage(); + return; + } + + m_boundaryTimer.start(BOUNDARY_TIMER_INTERVAL); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyHistogramItem::onModeChanged() +{ + if (!m_bPermitImageUpdate) + return; + + const auto widget = static_cast(scene()->parent()); + + if (!widget->bindMode() && EASY_GLOBALS.auto_adjust_histogram_height) + { + m_topDuration = m_maxDuration; + m_bottomDuration = m_minDuration; + } + + m_boundaryTimer.stop(); + updateImage(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyHistogramItem::cancelImageUpdate() +{ + if (!m_bPermitImageUpdate) + return; + + m_bReady.store(true, ::std::memory_order_release); + if (m_workerThread.joinable()) + m_workerThread.join(); + m_bReady.store(false, ::std::memory_order_release); + + delete m_workerImage; + m_workerImage = nullptr; + + m_imageOriginUpdate = m_imageOrigin; + m_imageScaleUpdate = m_imageScale; + + m_timer.stop(); +} + +void EasyHistogramItem::updateImage() +{ + if (!m_bPermitImageUpdate) + return; + + const auto widget = static_cast(scene()->parent()); + + m_bReady.store(true, ::std::memory_order_release); + if (m_workerThread.joinable()) + m_workerThread.join(); + m_bReady.store(false, ::std::memory_order_release); + + delete m_workerImage; + m_workerImage = nullptr; + + m_imageScaleUpdate = widget->range() / widget->sliderWidth(); + m_imageOriginUpdate = widget->bindMode() ? (widget->value() - widget->sliderWidth() * 3) : widget->minimum(); + + m_workerThread = ::std::thread([this](QRectF _boundingRect, HistRegime _regime, qreal _current_scale, + qreal _minimum, qreal _maximum, qreal _range, qreal _value, qreal _width, qreal _top_duration, qreal _bottom_duration, + bool _bindMode, float _frame_time, ::profiler::timestamp_t _begin_time, qreal _origin, bool _autoAdjustHist) + { + updateImage(_boundingRect, _regime, _current_scale, _minimum, _maximum, _range, _value, _width, _top_duration, _bottom_duration, _bindMode, _frame_time, _begin_time, _origin, _autoAdjustHist); + m_bReady.store(true, ::std::memory_order_release); + }, m_boundingRect, m_regime, widget->getWindowScale(), widget->minimum(), widget->maximum(), widget->range(), widget->value(), widget->sliderWidth(), + m_topDuration, m_bottomDuration, widget->bindMode(), EASY_GLOBALS.frame_time, EASY_GLOBALS.begin_time, m_imageOriginUpdate, EASY_GLOBALS.auto_adjust_histogram_height); + + m_timeouts = 3; + m_timer.start(WORKER_THREAD_CHECK_INTERVAL); +} + +void EasyHistogramItem::updateImage(QRectF _boundingRect, HistRegime _regime, qreal _current_scale, + qreal _minimum, qreal _maximum, qreal _range, + qreal _value, qreal _width, qreal _top_duration, qreal _bottom_duration, + bool _bindMode, float _frame_time, ::profiler::timestamp_t _begin_time, + qreal _origin, bool _autoAdjustHist) +{ + const auto bottom = _boundingRect.height();//_boundingRect.bottom(); + const auto screenWidth = _boundingRect.width() * _current_scale; + const auto maxColumnHeight = _boundingRect.height(); + const auto viewScale = _range / _width; + + if (_bindMode) + { + m_workerImageScale = viewScale; + m_workerImageOrigin = _value - _width * 3; + m_workerImage = new QImage(screenWidth * 7 + 0.5, _boundingRect.height(), QImage::Format_ARGB32); + } + else + { + m_workerImageScale = 1; + m_workerImageOrigin = _minimum; + m_workerImage = new QImage(screenWidth + 0.5, _boundingRect.height(), QImage::Format_ARGB32); + } + + m_workerImage->fill(0); + QPainter p(m_workerImage); + p.setPen(Qt::NoPen); + + QRectF rect; + QBrush brush(Qt::SolidPattern); + QRgb previousColor = 0; + + qreal previous_x = -1e30, previous_h = -1e30, offset = 0.; + auto realScale = _current_scale; + + const bool gotFrame = _frame_time > 1e-6f; + qreal frameCoeff = 1; + if (gotFrame) + { + if (_frame_time <= _bottom_duration) + frameCoeff = _boundingRect.height(); + else + frameCoeff = 0.9 / _frame_time; + } + + auto const calculate_color = gotFrame ? calculate_color2 : calculate_color1; + auto const k = gotFrame ? sqr(sqr(frameCoeff)) : 1.0 / _boundingRect.height(); + + if (_regime == Hist_Pointer) + { + const auto& items = *m_pSource; + if (items.empty()) + return; + + auto first = items.begin(); + + if (_bindMode) + { + _minimum = m_workerImageOrigin; + _maximum = m_workerImageOrigin + _width * 7; + realScale *= viewScale; + offset = _minimum * realScale; + + first = ::std::lower_bound(items.begin(), items.end(), _minimum, [](const ::profiler_gui::EasyBlockItem& _item, qreal _value) + { + return _item.left() < _value; + }); + + if (first != items.end()) + { + if (first != items.begin()) + --first; + } + else + { + first = items.begin() + items.size() - 1; + } + + if (_autoAdjustHist) + { + const auto maxVal = _value + _width; + decltype(_top_duration) maxDuration = 0; + decltype(_bottom_duration) minDuration = 1e30; + size_t iterations = 0; + for (auto it = first, end = items.end(); it != end; ++it) + { + // Draw rectangle + if (it->left() > maxVal) + break; + + if (it->right() < _value) + continue; + + if (maxDuration < it->width()) + maxDuration = it->width(); + + if (minDuration > it->width()) + minDuration = it->width(); + + ++iterations; + } + + if (iterations) + { + _top_duration = maxDuration; + _bottom_duration = minDuration; + + if ((_top_duration - _bottom_duration) < 1e-3) + { + if (_bottom_duration > 0.1) + { + _bottom_duration -= 0.1; + } + else + { + _top_duration = 0.1; + _bottom_duration = 0; + } + } + } + } + } + + const auto dtime = _top_duration - _bottom_duration; + const auto coeff = (_boundingRect.height() - HIST_COLUMN_MIN_HEIGHT) / (dtime > 1e-3 ? dtime : 1.); + + for (auto it = first, end = items.end(); it != end; ++it) + { + // Draw rectangle + if (it->left() > _maximum) + break; + + if (it->right() < _minimum) + continue; + + const qreal item_x = it->left() * realScale - offset; + const qreal item_w = ::std::max(it->width() * realScale, 1.0); + const qreal item_r = item_x + item_w; + const qreal h = it->width() <= _bottom_duration ? HIST_COLUMN_MIN_HEIGHT : + (it->width() > _top_duration ? maxColumnHeight : (HIST_COLUMN_MIN_HEIGHT + (it->width() - _bottom_duration) * coeff)); + + if (h < previous_h && item_r < previous_x) + continue; + + const auto col = calculate_color(h, it->width(), k); + const auto color = 0x00ffffff & QColor::fromHsvF((1.0 - col) * 0.375, 0.85, 0.85).rgb(); + + if (previousColor != color) + { + // Set background color brush for rectangle + previousColor = color; + brush.setColor(QColor::fromRgba(0xc0000000 | color)); + p.setBrush(brush); + } + + rect.setRect(item_x, bottom - h, item_w, h); + p.drawRect(rect); + + previous_x = item_r; + previous_h = h; + } + } + else + { + auto first = m_selectedBlocks.begin(); + + if (_bindMode) + { + _minimum = m_workerImageOrigin; + _maximum = m_workerImageOrigin + _width * 7; + realScale *= viewScale; + offset = _minimum * 1e3 * realScale; + + first = ::std::lower_bound(m_selectedBlocks.begin(), m_selectedBlocks.end(), _minimum * 1e3 + _begin_time, [](::profiler::block_index_t _item, qreal _value) + { + return easyBlock(_item).tree.node->begin() < _value; + }); + + if (first != m_selectedBlocks.end()) + { + if (first != m_selectedBlocks.begin()) + --first; + } + else + { + first = m_selectedBlocks.begin() + m_selectedBlocks.size() - 1; + } + + _minimum *= 1e3; + _maximum *= 1e3; + + if (_autoAdjustHist) + { + const auto minVal = _value * 1e3, maxVal = (_value + _width) * 1e3; + decltype(_top_duration) maxDuration = 0; + decltype(_bottom_duration) minDuration = 1e30; + size_t iterations = 0; + for (auto it = first, end = m_selectedBlocks.end(); it != end; ++it) + { + const auto item = easyBlock(*it).tree.node; + + const auto beginTime = item->begin() - _begin_time; + if (beginTime > maxVal) + break; + + const auto endTime = item->end() - _begin_time; + if (endTime < minVal) + continue; + + const qreal duration = item->duration() * 1e-3; + + if (maxDuration < duration) + maxDuration = duration; + + if (minDuration > duration) + minDuration = duration; + + ++iterations; + } + + if (iterations) + { + _top_duration = maxDuration; + _bottom_duration = minDuration; + + if ((_top_duration - _bottom_duration) < 1e-3) + { + if (_bottom_duration > 0.1) + { + _bottom_duration -= 0.1; + } + else + { + _top_duration = 0.1; + _bottom_duration = 0; + } + } + } + } + } + else + { + _minimum *= 1e3; + _maximum *= 1e3; + } + + const auto dtime = _top_duration - _bottom_duration; + const auto coeff = (_boundingRect.height() - HIST_COLUMN_MIN_HEIGHT) / (dtime > 1e-3 ? dtime : 1.); + + for (auto it = first, end = m_selectedBlocks.end(); it != end; ++it) + { + // Draw rectangle + const auto item = easyBlock(*it).tree.node; + + const auto beginTime = item->begin() - _begin_time; + if (beginTime > _maximum) + break; + + const auto endTime = item->end() - _begin_time; + if (endTime < _minimum) + continue; + + const qreal duration = item->duration() * 1e-3; + const qreal item_x = (beginTime * realScale - offset) * 1e-3; + const qreal item_w = ::std::max(duration * realScale, 1.0); + const qreal item_r = item_x + item_w; + const auto h = duration <= _bottom_duration ? HIST_COLUMN_MIN_HEIGHT : + (duration > _top_duration ? maxColumnHeight : (HIST_COLUMN_MIN_HEIGHT + (duration - _bottom_duration) * coeff)); + + if (h < previous_h && item_r < previous_x) + continue; + + const auto col = calculate_color(h, duration, k); + const auto color = 0x00ffffff & QColor::fromHsvF((1.0 - col) * 0.375, 0.85, 0.85).rgb(); + + if (previousColor != color) + { + // Set background color brush for rectangle + previousColor = color; + brush.setColor(QColor::fromRgba(0xc0000000 | color)); + p.setBrush(brush); + } + + rect.setRect(item_x, bottom - h, item_w, h); + p.drawRect(rect); + + previous_x = item_r; + previous_h = h; + } + } + + m_workerTopDuration = _top_duration; + m_workerBottomDuration = _bottom_duration; +} + +////////////////////////////////////////////////////////////////////////// + +EasyGraphicsScrollbar::EasyGraphicsScrollbar(QWidget* _parent) + : Parent(_parent) + , m_minimumValue(0) + , m_maximumValue(500) + , m_value(10) + , m_windowScale(1) + , m_mouseButtons(Qt::NoButton) + , m_slider(nullptr) + , m_chronometerIndicator(nullptr) + , m_histogramItem(nullptr) + , m_defaultFontHeight(0) + , m_bScrolling(false) + , m_bBindMode(false) + , m_bLocked(false) +{ + setCacheMode(QGraphicsView::CacheNone); + setTransformationAnchor(QGraphicsView::AnchorUnderMouse); + //setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); + setViewportUpdateMode(QGraphicsView::FullViewportUpdate); + setOptimizationFlag(QGraphicsView::DontSavePainterState, true); + + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + + setContentsMargins(0, 0, 0, 0); + + auto selfScene = new QGraphicsScene(this); + m_defaultFontHeight = QFontMetrics(selfScene->font()).height(); + selfScene->setSceneRect(0, DEFAULT_TOP, 500, DEFAULT_HEIGHT + m_defaultFontHeight + 2); + setFixedHeight(DEFAULT_HEIGHT + m_defaultFontHeight + 2); + + setScene(selfScene); + + m_histogramItem = new EasyHistogramItem(); + m_histogramItem->setPos(0, 0); + m_histogramItem->setBoundingRect(0, DEFAULT_TOP + INDICATOR_SIZE, scene()->width(), DEFAULT_HEIGHT - INDICATOR_SIZE_x2); + selfScene->addItem(m_histogramItem); + m_histogramItem->hide(); + + m_chronometerIndicator = new EasyGraphicsSliderItem(false); + m_chronometerIndicator->setPos(0, 0); + m_chronometerIndicator->setColor(0x40000000 | ::profiler_gui::CHRONOMETER_COLOR.rgba()); + selfScene->addItem(m_chronometerIndicator); + m_chronometerIndicator->hide(); + + m_slider = new EasyGraphicsSliderItem(true); + m_slider->setPos(0, 0); + m_slider->setColor(0x40c0c0c0); + selfScene->addItem(m_slider); + m_slider->hide(); + + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::expectedFrameTimeChanged, [this]() + { + if (m_histogramItem->isVisible()) + { + m_histogramItem->updateImage(); + scene()->update(); + } + }); + + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::autoAdjustHistogramChanged, [this]() + { + if (m_histogramItem->isVisible()) + m_histogramItem->onModeChanged(); + }); + + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::displayOnlyFramesOnHistogramChanged, [this]() + { + if (m_histogramItem->isVisible()) + m_histogramItem->rebuildSource(EasyHistogramItem::Hist_Id); + }); + + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::threadNameDecorationChanged, this, &This::onThreadViewChanged); + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::hexThreadIdChanged, this, &This::onThreadViewChanged); + + centerOn(0, 0); +} + +EasyGraphicsScrollbar::~EasyGraphicsScrollbar() +{ + +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsScrollbar::onThreadViewChanged() +{ + if (m_histogramItem->isVisible()) + { + m_histogramItem->validateName(); + scene()->update(); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsScrollbar::clear() +{ + setHistogramSource(0, nullptr); + hideChrono(); + setRange(0, 100); + setSliderWidth(2); + setValue(0); +} + +////////////////////////////////////////////////////////////////////////// + +bool EasyGraphicsScrollbar::bindMode() const +{ + return m_bBindMode; +} + +qreal EasyGraphicsScrollbar::getWindowScale() const +{ + return m_windowScale; +} + +::profiler::thread_id_t EasyGraphicsScrollbar::hystThread() const +{ + return m_histogramItem->threadId(); +} + +qreal EasyGraphicsScrollbar::minimum() const +{ + return m_minimumValue; +} + +qreal EasyGraphicsScrollbar::maximum() const +{ + return m_maximumValue; +} + +qreal EasyGraphicsScrollbar::range() const +{ + return m_maximumValue - m_minimumValue; +} + +qreal EasyGraphicsScrollbar::value() const +{ + return m_value; +} + +qreal EasyGraphicsScrollbar::sliderWidth() const +{ + return m_slider->width(); +} + +qreal EasyGraphicsScrollbar::sliderHalfWidth() const +{ + return m_slider->halfwidth(); +} + +int EasyGraphicsScrollbar::defaultFontHeight() const +{ + return m_defaultFontHeight; +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsScrollbar::setValue(qreal _value) +{ + m_value = clamp(m_minimumValue, _value, ::std::max(m_minimumValue, m_maximumValue - m_slider->width())); + m_slider->setX(m_value + m_slider->halfwidth()); + emit valueChanged(m_value); + + if (m_histogramItem->isVisible()) + m_histogramItem->onValueChanged(); +} + +void EasyGraphicsScrollbar::setRange(qreal _minValue, qreal _maxValue) +{ + const auto oldRange = range(); + const auto oldValue = oldRange < 1e-3 ? 0.0 : m_value / oldRange; + + m_minimumValue = _minValue; + m_maximumValue = _maxValue; + scene()->setSceneRect(_minValue, DEFAULT_TOP, _maxValue - _minValue, DEFAULT_HEIGHT + m_defaultFontHeight + 4); + + m_histogramItem->cancelImageUpdate(); + m_histogramItem->setBoundingRect(_minValue, DEFAULT_TOP + INDICATOR_SIZE, _maxValue, DEFAULT_HEIGHT - INDICATOR_SIZE_x2); + + emit rangeChanged(); + + setValue(_minValue + oldValue * range()); + + onWindowWidthChange(width()); + + if (m_histogramItem->isVisible()) + m_histogramItem->updateImage(); +} + +void EasyGraphicsScrollbar::setSliderWidth(qreal _width) +{ + m_slider->setWidth(_width); + setValue(m_value); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsScrollbar::setChronoPos(qreal _left, qreal _right) +{ + m_chronometerIndicator->setWidth(_right - _left); + m_chronometerIndicator->setX(_left + m_chronometerIndicator->halfwidth()); +} + +void EasyGraphicsScrollbar::showChrono() +{ + m_chronometerIndicator->show(); +} + +void EasyGraphicsScrollbar::hideChrono() +{ + m_chronometerIndicator->hide(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsScrollbar::setHistogramSource(::profiler::thread_id_t _thread_id, const ::profiler_gui::EasyItems* _items) +{ + if (m_bLocked) + return; + + m_histogramItem->setSource(_thread_id, _items); + m_slider->setVisible(m_histogramItem->isVisible()); + scene()->update(); +} + +void EasyGraphicsScrollbar::setHistogramSource(::profiler::thread_id_t _thread_id, ::profiler::block_id_t _block_id) +{ + if (m_bLocked) + return; + + m_histogramItem->setSource(_thread_id, _block_id); + m_slider->setVisible(m_histogramItem->isVisible()); + scene()->update(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsScrollbar::mousePressEvent(QMouseEvent* _event) +{ + _event->accept(); + + m_mouseButtons = _event->buttons(); + + if (m_mouseButtons & Qt::LeftButton) + { + if (_event->modifiers() & Qt::ControlModifier) + { + m_histogramItem->pickBottomBoundary(mapToScene(_event->pos()).y()); + } + else if (_event->modifiers() & Qt::ShiftModifier) + { + m_histogramItem->pickTopBoundary(mapToScene(_event->pos()).y()); + } + else + { + m_bScrolling = true; + m_mousePressPos = _event->pos(); + if (!m_bBindMode) + setValue(mapToScene(m_mousePressPos).x() - m_minimumValue - m_slider->halfwidth()); + } + } + + if (m_mouseButtons & Qt::RightButton) + { + if (_event->modifiers()) + { + m_histogramItem->pickFrameTime(mapToScene(_event->pos()).y()); + } + else + { + m_bBindMode = !m_bBindMode; + if (m_histogramItem->isVisible()) + m_histogramItem->onModeChanged(); + } + } + + //QGraphicsView::mousePressEvent(_event); +} + +void EasyGraphicsScrollbar::mouseReleaseEvent(QMouseEvent* _event) +{ + m_mouseButtons = _event->buttons(); + m_bScrolling = false; + _event->accept(); + //QGraphicsView::mouseReleaseEvent(_event); +} + +void EasyGraphicsScrollbar::mouseMoveEvent(QMouseEvent* _event) +{ + const auto pos = _event->pos(); + + if (m_mouseButtons & Qt::LeftButton) + { + const auto delta = pos - m_mousePressPos; + m_mousePressPos = pos; + + if (m_bScrolling) + { + auto realScale = m_windowScale; + if (m_bBindMode) + realScale *= -range() / sliderWidth(); + setValue(m_value + delta.x() / realScale); + } + } + + if (m_histogramItem->isVisible()) + { + m_histogramItem->setMouseY(mapToScene(pos).y()); + scene()->update(); + } +} + +void EasyGraphicsScrollbar::wheelEvent(QWheelEvent* _event) +{ + _event->accept(); + + if (_event->modifiers() & Qt::ShiftModifier) + { + // Shift + mouse wheel will change histogram top boundary + + if (m_histogramItem->isVisible()) + { + if (_event->delta() > 0) + m_histogramItem->increaseTopBoundary(); + else + m_histogramItem->decreaseTopBoundary(); + } + + return; + } + + if (_event->modifiers() & Qt::ControlModifier) + { + // Ctrl + mouse wheel will change histogram bottom boundary + + if (m_histogramItem->isVisible()) + { + if (_event->delta() > 0) + m_histogramItem->increaseBottomBoundary(); + else + m_histogramItem->decreaseBottomBoundary(); + } + + return; + } + + if (!m_bBindMode) + { + const auto w = m_slider->halfwidth() * (_event->delta() < 0 ? ::profiler_gui::SCALING_COEFFICIENT : ::profiler_gui::SCALING_COEFFICIENT_INV); + setValue(mapToScene(_event->pos()).x() - m_minimumValue - w); + emit wheeled(w * m_windowScale, _event->delta()); + } + else + { + const auto x = (mapToScene(_event->pos()).x() - m_minimumValue) * m_windowScale; + emit wheeled(x, _event->delta()); + } +} + +void EasyGraphicsScrollbar::resizeEvent(QResizeEvent* _event) +{ + onWindowWidthChange(_event->size().width()); + if (m_histogramItem->isVisible()) + m_histogramItem->updateImage(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyGraphicsScrollbar::onWindowWidthChange(qreal _width) +{ + const auto oldScale = m_windowScale; + const auto scrollingRange = range(); + + if (scrollingRange < 1e-3) + { + m_windowScale = 1; + } + else + { + m_windowScale = _width / scrollingRange; + } + + scale(m_windowScale / oldScale, 1); +} + +////////////////////////////////////////////////////////////////////////// diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_scrollbar.h b/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_scrollbar.h new file mode 100644 index 0000000000..06a8ec7a3f --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/easy_graphics_scrollbar.h @@ -0,0 +1,342 @@ +/************************************************************************ +* file name : easy_graphics_scrollbar.h +* ----------------- : +* creation time : 2016/07/04 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : This file contains declaration of +* ----------------- : +* change log : * 2016/07/04 Victor Zarubkin: Initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY__GRAPHICS_SCROLLBAR__H +#define EASY__GRAPHICS_SCROLLBAR__H + +#include +#include +#include +#include +#include +#include +#include +#include +#include "easy_qtimer.h" +#include "common_types.h" + +////////////////////////////////////////////////////////////////////////// + +// TODO: use profiler_core/spin_lock.h + +#define EASY_GUI_USE_CRITICAL_SECTION // Use CRITICAL_SECTION instead of std::atomic_flag +#if defined(_WIN32) && defined(EASY_GUI_USE_CRITICAL_SECTION) +namespace profiler_gui { + // std::atomic_flag on Windows works slower than critical section, so we will use it instead of std::atomic_flag... + // By the way, Windows critical sections are slower than std::atomic_flag on Unix. + class spin_lock { void* m_lock; public: + void lock(); + void unlock(); + spin_lock(); + ~spin_lock(); + }; +#else +namespace profiler_gui { + // std::atomic_flag on Unix works fine and very fast (almost instant!) + class spin_lock { + ::std::atomic_flag m_lock; public: + + void lock() { + while (m_lock.test_and_set(::std::memory_order_acquire)); + } + + void unlock() { + m_lock.clear(::std::memory_order_release); + } + + spin_lock() { + m_lock.clear(); + } + }; +#endif + +} // END of namespace profiler_gui. + +////////////////////////////////////////////////////////////////////////// + +class EasyGraphicsSliderItem : public QGraphicsRectItem +{ + typedef QGraphicsRectItem Parent; + typedef EasyGraphicsSliderItem This; + +private: + + QPolygonF m_indicator; + qreal m_halfwidth; + bool m_bMain; + +public: + + explicit EasyGraphicsSliderItem(bool _main); + virtual ~EasyGraphicsSliderItem(); + + void paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget = nullptr) override; + + qreal width() const; + qreal halfwidth() const; + + void setWidth(qreal _width); + void setHalfwidth(qreal _halfwidth); + + void setColor(QRgb _color); + void setColor(const QColor& _color); + +}; // END of class EasyGraphicsSliderItem. + +////////////////////////////////////////////////////////////////////////// + +class EasyHistogramItem : public QGraphicsItem +{ + typedef QGraphicsItem Parent; + typedef EasyHistogramItem This; + +public: + + enum HistRegime : uint8_t { Hist_Pointer, Hist_Id }; + +private: + + QRectF m_boundingRect; + qreal m_topDuration; + qreal m_bottomDuration; + qreal m_maxDuration; + qreal m_minDuration; + qreal m_mouseY; + qreal m_imageOrigin; + qreal m_imageScale; + qreal m_imageOriginUpdate; + qreal m_imageScaleUpdate; + qreal m_workerImageOrigin; + qreal m_workerImageScale; + qreal m_workerTopDuration; + qreal m_workerBottomDuration; + ::profiler::timestamp_t m_blockTotalDuraion; + QString m_topDurationStr; + QString m_bottomDurationStr; + QString m_threadName; + QString m_blockName; + ::profiler::BlocksTree::children_t m_selectedBlocks; + QImage m_mainImage; + EasyQTimer m_timer; + EasyQTimer m_boundaryTimer; + ::std::thread m_workerThread; + ::profiler::timestamp_t m_threadDuration; + ::profiler::timestamp_t m_threadProfiledTime; + ::profiler::timestamp_t m_threadWaitTime; + const ::profiler_gui::EasyItems* m_pSource; + QImage* m_workerImage; + const ::profiler::BlocksTreeRoot* m_pProfilerThread; + ::profiler::thread_id_t m_threadId; + ::profiler::block_index_t m_blockId; + int m_timeouts; + ::profiler_gui::TimeUnits m_timeUnits; + HistRegime m_regime; + bool m_bPermitImageUpdate; ///< Is false when m_workerThread is parsing input dataset (when setSource(_block_id) is called) + ::profiler_gui::spin_lock m_spin; + ::std::atomic_bool m_bReady; + +public: + + explicit EasyHistogramItem(); + virtual ~EasyHistogramItem(); + + // Public virtual methods + + QRectF boundingRect() const override; + void paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget = nullptr) override; + +public: + + // Public non-virtual methods + + ::profiler::thread_id_t threadId() const; + + void setBoundingRect(const QRectF& _rect); + void setBoundingRect(qreal x, qreal y, qreal w, qreal h); + + void setSource(::profiler::thread_id_t _thread_id, const ::profiler_gui::EasyItems* _items); + void setSource(::profiler::thread_id_t _thread_id, ::profiler::block_id_t _block_id); + void rebuildSource(HistRegime _regime); + void rebuildSource(); + void validateName(); + void updateImage(); + void cancelImageUpdate(); + + void pickTopBoundary(qreal _y); + void increaseTopBoundary(); + void decreaseTopBoundary(); + + void pickBottomBoundary(qreal _y); + void increaseBottomBoundary(); + void decreaseBottomBoundary(); + + void setMouseY(qreal _mouseY); + void pickFrameTime(qreal _y) const; + + void onValueChanged(); + void onModeChanged(); + +private: + + void paintBusyIndicator(QPainter* _painter, qreal _current_scale); + void paintMouseIndicator(QPainter* _painter, qreal _top, qreal _bottom, qreal _width, qreal _height, qreal _top_width, qreal _mouse_y, qreal _delta_time, int _font_h); + void paintByPtr(QPainter* _painter); + void paintById(QPainter* _painter); + void onTimeout(); + void updateImage(QRectF _boundingRect, HistRegime _regime, qreal _current_scale, + qreal _minimum, qreal _maximum, qreal _range, + qreal _value, qreal _width, qreal _top_duration, qreal _bottom_duration, bool _bindMode, + float _frame_time, ::profiler::timestamp_t _begin_time, qreal _origin, bool _autoAdjustHist); + +}; // END of class EasyHistogramItem. + +////////////////////////////////////////////////////////////////////////// + +class EasyGraphicsScrollbar : public QGraphicsView +{ + Q_OBJECT + +private: + + typedef QGraphicsView Parent; + typedef EasyGraphicsScrollbar This; + + qreal m_minimumValue; + qreal m_maximumValue; + qreal m_value; + qreal m_windowScale; + QPoint m_mousePressPos; + Qt::MouseButtons m_mouseButtons; + EasyGraphicsSliderItem* m_slider; + EasyGraphicsSliderItem* m_chronometerIndicator; + EasyHistogramItem* m_histogramItem; + int m_defaultFontHeight; + bool m_bScrolling; + bool m_bBindMode; + bool m_bLocked; + +public: + + explicit EasyGraphicsScrollbar(QWidget* _parent = nullptr); + virtual ~EasyGraphicsScrollbar(); + + // Public virtual methods + + void mousePressEvent(QMouseEvent* _event) override; + void mouseReleaseEvent(QMouseEvent* _event) override; + void mouseMoveEvent(QMouseEvent* _event) override; + void wheelEvent(QWheelEvent* _event) override; + void resizeEvent(QResizeEvent* _event) override; + + void dragEnterEvent(QDragEnterEvent*) override {} + +public: + + // Public non-virtual methods + + void clear(); + + bool bindMode() const; + qreal getWindowScale() const; + ::profiler::thread_id_t hystThread() const; + + qreal minimum() const; + qreal maximum() const; + qreal range() const; + qreal value() const; + qreal sliderWidth() const; + qreal sliderHalfWidth() const; + int defaultFontHeight() const; + + void setValue(qreal _value); + void setRange(qreal _minValue, qreal _maxValue); + void setSliderWidth(qreal _width); + void setChronoPos(qreal _left, qreal _right); + void showChrono(); + void hideChrono(); + + void setHistogramSource(::profiler::thread_id_t _thread_id, const::profiler_gui::EasyItems* _items); + void setHistogramSource(::profiler::thread_id_t _thread_id, ::profiler::block_id_t _block_id); + + inline void setHistogramSource(::profiler::thread_id_t _thread_id, const ::profiler_gui::EasyItems& _items) + { + setHistogramSource(_thread_id, &_items); + } + + inline void lock() + { + m_bLocked = true; + } + + inline void unlock() + { + m_bLocked = false; + } + +signals: + + void rangeChanged(); + void valueChanged(qreal _value); + void wheeled(qreal _mouseX, int _wheelDelta); + +private slots: + + void onThreadViewChanged(); + void onWindowWidthChange(qreal _width); + +}; // END of class EasyGraphicsScrollbar. + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY__GRAPHICS_SCROLLBAR__H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/easy_qtimer.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/easy_qtimer.cpp new file mode 100644 index 0000000000..4eb174031e --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/easy_qtimer.cpp @@ -0,0 +1,85 @@ +/************************************************************************ +* file name : easy_qtimer.h +* ----------------- : +* creation time : 2016/12/05 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : This file contains implementation of EasyQTimer class used to +* : connect QTimer to non-QObject classes. +* ----------------- : +* change log : * 2016/12/05 Victor Zarubkin: Initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include "easy_qtimer.h" + +////////////////////////////////////////////////////////////////////////// + +EasyQTimer::EasyQTimer() + : QObject() +{ + connect(&m_timer, &QTimer::timeout, [this](){ m_handler(); }); +} + +EasyQTimer::EasyQTimer(::std::function&& _handler, bool _isSignleShot) + : QObject() + , m_handler(::std::forward<::std::function&&>(_handler)) +{ + m_timer.setSingleShot(_isSignleShot); + connect(&m_timer, &QTimer::timeout, [this](){ m_handler(); }); +} + +EasyQTimer::~EasyQTimer() +{ + +} + +void EasyQTimer::setHandler(::std::function&& _handler) +{ + m_handler = _handler; +} + +////////////////////////////////////////////////////////////////////////// + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/easy_qtimer.h b/Source/ThirdParty/easy_profiler/profiler_gui/easy_qtimer.h new file mode 100644 index 0000000000..818e628ecf --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/easy_qtimer.h @@ -0,0 +1,89 @@ +/************************************************************************ +* file name : easy_qtimer.h +* ----------------- : +* creation time : 2016/12/05 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : This file contains declaration of EasyQTimer class used to +* : connect QTimer to non-QObject classes. +* ----------------- : +* change log : * 2016/12/05 Victor Zarubkin: Initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY__QTIMER__H +#define EASY__QTIMER__H + +#include +#include + +////////////////////////////////////////////////////////////////////////// + +class EasyQTimer : public QObject +{ + Q_OBJECT + +private: + + QTimer m_timer; + ::std::function m_handler; + +public: + + EasyQTimer(); + EasyQTimer(::std::function&& _handler, bool _isSignleShot = false); + virtual ~EasyQTimer(); + + void setHandler(::std::function&& _handler); + + inline void start(int msec) { m_timer.start(msec); } + inline void stop() { if (m_timer.isActive()) m_timer.stop(); } + inline bool isActive() const { return m_timer.isActive(); } + +}; // END of class EasyQTimer. + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY__QTIMER__H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/globals.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/globals.cpp new file mode 100644 index 0000000000..4aa6499190 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/globals.cpp @@ -0,0 +1,115 @@ +/************************************************************************ +* file name : globals.cpp +* ----------------- : +* creation time : 2016/08/03 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of global constants and variables for profiler gui. +* ----------------- : +* change log : * 2016/08/03 Victor Zarubkin: initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#define IGNORE_GLOBALS_DECLARATION +#include "globals.h" +#undef IGNORE_GLOBALS_DECLARATION + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +namespace profiler_gui { + + EasyGlobals& EasyGlobals::instance() + { + // It's okay even without C++11 "magic statics" feature because first call happens + // on application initialization - there is only one thread and no data races occur. + static EasyGlobals globals; + return globals; + } + + EasyGlobals::EasyGlobals() + : begin_time(0) + , selected_thread(0U) + , selected_block(::profiler_gui::numeric_max()) + , selected_block_id(::profiler_gui::numeric_max()) + , version(0) + , frame_time(16700) + , blocks_spacing(0) + , blocks_size_min(2) + , blocks_narrow_size(20) + , max_fps_history(90) + , fps_timer_interval(500) + , fps_widget_line_width(2) + , chrono_text_position(ChronoTextPosition_Top) + , time_units(TimeUnits_ms) + , connected(false) + , fps_enabled(true) + , use_decorated_thread_name(false) + , hex_thread_id(false) + , enable_event_markers(true) + , enable_statistics(true) + , enable_zero_length(true) + , add_zero_blocks_to_hierarchy(false) + , draw_graphics_items_borders(true) + , hide_narrow_children(false) + , hide_minsize_blocks(false) + , display_only_relevant_stats(true) + , collapse_items_on_tree_close(false) + , all_items_expanded_by_default(true) + , only_current_thread_hierarchy(false) + , highlight_blocks_with_same_id(true) + , selecting_block_changes_thread(true) + , auto_adjust_histogram_height(true) + , display_only_frames_on_histogram(false) + , bind_scene_and_tree_expand_status(true) + { + + } + +} // END of namespace profiler_gui. + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/globals.h b/Source/ThirdParty/easy_profiler/profiler_gui/globals.h new file mode 100644 index 0000000000..7489f62c7d --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/globals.h @@ -0,0 +1,237 @@ +/************************************************************************ +* file name : globals.h +* ----------------- : +* creation time : 2016/08/03 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of global constants and variables for profiler gui. +* ----------------- : +* change log : * 2016/08/03 Victor Zarubkin: initial commit. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_PROFILER__GUI_GLOBALS_H +#define EASY_PROFILER__GUI_GLOBALS_H + +#include +#include +#include +#include +#include +#include "common_types.h" +#include "globals_qobjects.h" + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +namespace profiler_gui { + + const QString ORGANAZATION_NAME = "EasyProfiler"; + const QString APPLICATION_NAME = "Easy profiler gui application"; + + const QColor CHRONOMETER_COLOR = QColor::fromRgba(0x40000000 | (::profiler::colors::RichBlue & 0x00ffffff));// 0x402020c0); + const QColor CHRONOMETER_COLOR2 = QColor::fromRgba(0x40000000 | (::profiler::colors::Dark & 0x00ffffff));// 0x40408040); + const QRgb SELECTED_THREAD_BACKGROUND = 0x00e0e060; + const QRgb SELECTED_THREAD_FOREGROUND = 0x00ffffff - SELECTED_THREAD_BACKGROUND; + + const qreal SCALING_COEFFICIENT = 1.25; + const qreal SCALING_COEFFICIENT_INV = 1.0 / SCALING_COEFFICIENT; + + const uint32_t V130 = 0x01030000; + + const QSize ICONS_SIZE(28, 28); + const uint16_t GRAPHICS_ROW_SIZE = 18; + const uint16_t GRAPHICS_ROW_SPACING = 0; + const uint16_t GRAPHICS_ROW_SIZE_FULL = GRAPHICS_ROW_SIZE + GRAPHICS_ROW_SPACING; + const uint16_t THREADS_ROW_SPACING = 8; + +#ifdef _WIN32 + const qreal FONT_METRICS_FACTOR = 1.05; +#else + const qreal FONT_METRICS_FACTOR = 1.; +#endif + + ////////////////////////////////////////////////////////////////////////// + + template + inline auto toUnicode(const T& _inputString) -> decltype(QTextCodec::codecForLocale()->toUnicode(_inputString)) + { + return QTextCodec::codecForLocale()->toUnicode(_inputString); + } + + ////////////////////////////////////////////////////////////////////////// + + inline QString decoratedThreadName(bool _use_decorated_thread_name, const::profiler::BlocksTreeRoot& _root, const QString& _unicodeThreadWord, bool _hex = false) + { + if (_root.got_name()) + { + QString rootname(toUnicode(_root.name())); + if (!_use_decorated_thread_name || rootname.contains(_unicodeThreadWord, Qt::CaseInsensitive)) + { + if (_hex) + return QString("%1 0x%2").arg(rootname).arg(_root.thread_id, 0, 16); + return QString("%1 %2").arg(rootname).arg(_root.thread_id); + } + + if (_hex) + return QString("%1 Thread 0x%2").arg(rootname).arg(_root.thread_id, 0, 16); + return QString("%1 Thread %2").arg(rootname).arg(_root.thread_id); + } + + if (_hex) + return QString("Thread 0x%1").arg(_root.thread_id, 0, 16); + return QString("Thread %1").arg(_root.thread_id); + } + + inline QString decoratedThreadName(bool _use_decorated_thread_name, const ::profiler::BlocksTreeRoot& _root, bool _hex = false) + { + if (_root.got_name()) + { + QString rootname(toUnicode(_root.name())); + if (!_use_decorated_thread_name || rootname.contains(toUnicode("thread"), Qt::CaseInsensitive)) + { + if (_hex) + return QString("%1 0x%2").arg(rootname).arg(_root.thread_id, 0, 16); + return QString("%1 %2").arg(rootname).arg(_root.thread_id); + } + + if (_hex) + return QString("%1 Thread 0x%2").arg(rootname).arg(_root.thread_id, 0, 16); + return QString("%1 Thread %2").arg(rootname).arg(_root.thread_id); + } + + if (_hex) + return QString("Thread 0x%1").arg(_root.thread_id, 0, 16); + return QString("Thread %1").arg(_root.thread_id); + } + + ////////////////////////////////////////////////////////////////////////// + + enum ChronometerTextPosition : int8_t + { + ChronoTextPosition_Center = 0, + ChronoTextPosition_Top, + ChronoTextPosition_Bottom, + + }; // END of enum ChronometerTextPosition. + + ////////////////////////////////////////////////////////////////////////// + + struct EasyGlobals Q_DECL_FINAL + { + static EasyGlobals& instance(); + + EasyGlobalSignals events; ///< Global signals + ::profiler::thread_blocks_tree_t profiler_blocks; ///< Profiler blocks tree loaded from file + ::profiler::descriptors_list_t descriptors; ///< Profiler block descriptors list + EasyBlocks gui_blocks; ///< Profiler graphics blocks builded by GUI + ::profiler::timestamp_t begin_time; ///< + ::profiler::thread_id_t selected_thread; ///< Current selected thread id + ::profiler::block_index_t selected_block; ///< Current selected profiler block index + ::profiler::block_id_t selected_block_id; ///< Current selected profiler block id + uint32_t version; ///< Opened file version (files may have different format) + float frame_time; ///< Expected frame time value in microseconds to be displayed at minimap on graphics scrollbar + int blocks_spacing; ///< Minimum blocks spacing on diagram + int blocks_size_min; ///< Minimum blocks size on diagram + int blocks_narrow_size; ///< Width indicating narrow blocks + int max_fps_history; ///< Max frames history displayed in FPS Monitor + int fps_timer_interval; ///< Interval in milliseconds for sending network requests to the profiled application (used by FPS Monitor) + int fps_widget_line_width; ///< Line width in pixels of FPS lines for FPS Monitor + ChronometerTextPosition chrono_text_position; ///< Selected interval text position + TimeUnits time_units; ///< Units type for time (milliseconds, microseconds, nanoseconds or auto-definition) + bool connected; ///< Is connected to source (to be able to capture profiling information) + bool fps_enabled; ///< Is FPS Monitor enabled + bool use_decorated_thread_name; ///< Add "Thread" to the name of each thread (if there is no one) + bool hex_thread_id; ///< Use hex view for thread-id instead of decimal + bool enable_event_markers; ///< Enable event indicators painting (These are narrow rectangles at the bottom of each thread) + bool enable_statistics; ///< Enable gathering and using statistics (Disable if you want to consume less memory) + bool enable_zero_length; ///< Enable zero length blocks (if true, then such blocks will have width == 1 pixel on each scale) + bool add_zero_blocks_to_hierarchy; ///< Enable adding zero blocks into hierarchy tree + bool draw_graphics_items_borders; ///< Draw borders for graphics blocks or not + bool hide_narrow_children; ///< Hide children for narrow graphics blocks (See blocks_narrow_size) + bool hide_minsize_blocks; ///< Hide blocks which screen size is less than blocks_size_min + bool display_only_relevant_stats; ///< Display only relevant information in ProfTreeWidget (excludes min, max, average times if there are only 1 calls number) + bool collapse_items_on_tree_close; ///< Collapse all items which were displayed in the hierarchy tree after tree close/reset + bool all_items_expanded_by_default; ///< Expand all items after file is opened + bool only_current_thread_hierarchy; ///< Build hierarchy tree for current thread only + bool highlight_blocks_with_same_id; ///< Highlight all blocks with same id on diagram + bool selecting_block_changes_thread; ///< If true then current selected thread will change every time you select block + bool auto_adjust_histogram_height; ///< Automatically adjust histogram height to the visible region + bool display_only_frames_on_histogram; ///< Display only top-level blocks on histogram when drawing histogram by block id + bool bind_scene_and_tree_expand_status; /** \brief If true then items on graphics scene and in the tree (blocks hierarchy) are binded on each other + so expanding/collapsing items on scene also expands/collapse items in the tree. */ + + private: + + EasyGlobals(); + + }; // END of struct EasyGlobals. + + ////////////////////////////////////////////////////////////////////////// + +} // END of namespace profiler_gui. + +#ifndef IGNORE_GLOBALS_DECLARATION +static ::profiler_gui::EasyGlobals& EASY_GLOBALS = ::profiler_gui::EasyGlobals::instance(); + +inline ::profiler_gui::EasyBlock& easyBlock(::profiler::block_index_t i) { + return EASY_GLOBALS.gui_blocks[i]; +} + +inline ::profiler::SerializedBlockDescriptor& easyDescriptor(::profiler::block_id_t i) { + return *EASY_GLOBALS.descriptors[i]; +} + +inline ::profiler::BlocksTree& blocksTree(::profiler::block_index_t i) { + return easyBlock(i).tree; +} +#endif + +#define SET_ICON(objectName, iconName) { QIcon icon(iconName); if (!icon.isNull()) objectName->setIcon(icon); } + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_PROFILER__GUI_GLOBALS_H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/globals_qobjects.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/globals_qobjects.cpp new file mode 100644 index 0000000000..62319b1f60 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/globals_qobjects.cpp @@ -0,0 +1,72 @@ +/************************************************************************ +* file name : globals_qobjects.cpp +* ----------------- : +* creation time : 2016/08/08 +* authors : Victor Zarubkin, Sergey Yagovtsev +* email : v.s.zarubkin@gmail.com, yse.sey@gmail.com +* ----------------- : +* description : The file contains implementation of EasyGlobalSignals QObject class. +* ----------------- : +* change log : * 2016/08/08 Sergey Yagovtsev: moved sources from globals.cpp +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include "globals_qobjects.h" + +////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// + +namespace profiler_gui { + + EasyGlobalSignals::EasyGlobalSignals() : QObject() + { + } + + EasyGlobalSignals::~EasyGlobalSignals() + { + } + +} // END of namespace profiler_gui. + +////////////////////////////////////////////////////////////////////////// diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/globals_qobjects.h b/Source/ThirdParty/easy_profiler/profiler_gui/globals_qobjects.h new file mode 100644 index 0000000000..b34d9248ec --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/globals_qobjects.h @@ -0,0 +1,94 @@ +/************************************************************************ +* file name : globals_qobjects.h +* ----------------- : +* creation time : 2016/08/08 +* authors : Victor Zarubkin, Sergey Yagovtsev +* email : v.s.zarubkin@gmail.com, yse.sey@gmail.com +* ----------------- : +* description : The file contains declaration of EasyGlobalSignals QObject class. +* ----------------- : +* change log : * 2016/08/08 Sergey Yagovtsev: moved sources from globals.h +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_GLOBALS_QOBJECTS_H +#define EASY_GLOBALS_QOBJECTS_H + +#include +#include + +namespace profiler_gui { + + class EasyGlobalSignals Q_DECL_FINAL : public QObject + { + Q_OBJECT + + public: + + EasyGlobalSignals(); + virtual ~EasyGlobalSignals(); + + signals: + + void selectedThreadChanged(::profiler::thread_id_t _id); + void selectedBlockChanged(uint32_t _block_index); + void selectedBlockIdChanged(::profiler::block_id_t _id); + void itemsExpandStateChanged(); + void blockStatusChanged(::profiler::block_id_t _id, ::profiler::EasyBlockStatus _status); + void connectionChanged(bool _connected); + void blocksRefreshRequired(bool); + void expectedFrameTimeChanged(); + void autoAdjustHistogramChanged(); + void displayOnlyFramesOnHistogramChanged(); + void hierarchyFlagChanged(bool); + void threadNameDecorationChanged(); + void hexThreadIdChanged(); + void refreshRequired(); + void blocksTreeModeChanged(); + + }; // END of class EasyGlobalSignals. + +} // END of namespace profiler_gui. + +#endif // EASY_GLOBALS_QOBJECTS_H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/attribution.txt b/Source/ThirdParty/easy_profiler/profiler_gui/icons/attribution.txt new file mode 100644 index 0000000000..88e12b1765 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/attribution.txt @@ -0,0 +1,24 @@ +logo.svg - Icon made by Freepik from www.flaticon.com +off.svg - Icon made by Freepik from www.flaticon.com +open-folder.svg - Icon made by Freepik from www.flaticon.com +open-folder2.svg - Icon made by Freepik from www.flaticon.com +reload-folder2.svg - Icon made by Freepik from www.flaticon.com +reload.svg - Icon made by Freepik from www.flaticon.com +expand.svg - Icon made by Freepik from www.flaticon.com +collapse.svg - Icon made by Freepik from www.flaticon.com +colors.svg - Icon made by Freepik from www.flaticon.com +colors-black.svg - Icon made by Freepik from www.flaticon.com +save.svg - Icon made by Freepik from www.flaticon.com +statistics.svg - Icon made by Freepik from www.flaticon.com +statistics2.svg - Icon made by Freepik from www.flaticon.com +lan.svg - Icon made by Freepik from www.flaticon.com +lan_on.svg - Icon made by Freepik from www.flaticon.com +wifi.svg - Icon made by Freepik from www.flaticon.com +wifi_on.svg - Icon made by Freepik from www.flaticon.com +play.svg - Icon made by Google from www.flaticon.com +stop.svg - Icon made by Google from www.flaticon.com +delete.svg - Icon made by Google from www.flaticon.com +list.svg - Icon made by Freepik from www.flaticon.com +search-prev.svg - Icon made by Freepik from www.flaticon.com +search-next.svg - Icon made by Freepik from www.flaticon.com +settings.svg - Icon made by Freepik from www.flaticon.com \ No newline at end of file diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/collapse.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/collapse.svg new file mode 100644 index 0000000000..3bf9ed48be --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/collapse.svg @@ -0,0 +1,16 @@ + + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/colors-black.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/colors-black.svg new file mode 100644 index 0000000000..13c8182659 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/colors-black.svg @@ -0,0 +1,31 @@ + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/colors.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/colors.svg new file mode 100644 index 0000000000..a8d580a3ce --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/colors.svg @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/delete.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/delete.svg new file mode 100644 index 0000000000..35a80161a5 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/delete.svg @@ -0,0 +1,10 @@ + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/expand.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/expand.svg new file mode 100644 index 0000000000..44ccaa3b63 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/expand.svg @@ -0,0 +1,15 @@ + + + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/lan.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/lan.svg new file mode 100644 index 0000000000..0f0124d82f --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/lan.svg @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/lan_on.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/lan_on.svg new file mode 100644 index 0000000000..41da42168f --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/lan_on.svg @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/list.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/list.svg new file mode 100644 index 0000000000..38ac6ed1c9 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/list.svg @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/logo.ico b/Source/ThirdParty/easy_profiler/profiler_gui/icons/logo.ico new file mode 100644 index 0000000000000000000000000000000000000000..9d72f93046aa235a2fc6af79572dfc4eb50fe688 GIT binary patch literal 179025 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í zJ6B_&xe(7b+{XRADw=N-p13rw`3$`Bu4_x6W7F^t$0O%6usMx+H+oWic(-xx`oELb z`7j;)(2tebz9l@I~wP%(|0fQj(N~LRMg*e(o#E6Jy2Wlst3}G_S>dB zRwnF`@DcdLpWm@rcPfY>px-7gJs~A|0&tgmu2JC zhgWu;H+gou@6b2YLq7F;UVWqL?VYLQU(a#~t8u+o-sf{YLcTkK-a&H9wX4o$6JKQf zcrxJ!!8-Eyzu zTDv6juTDjCR(CuLxt2{@Pl5KwoAj%KI}+KBhY22X|H8F9lF0v^`1e}R`J8FG#`l_+ zRE0b{W%xBK@}9;ky*=`;avYFE{-46X?54Z?UuzY8{4nyaZ}B+Ot`f(LhqR7zw};%y z$LJ*T|DI1A4>rzSeR@^QyDif-$gMH4yWDiHwi`$V_@kbC#?hJej8;`R|6Q4bxAc*H zZ4&t(=M#tCJv=v!`QJiZlVMDnXwQ5=eV14HZ{>QReH!@FT9a2C*CXdSkgoPVm$>vT zraEb&Hs$Y^e~o2>=Km9){C91XyQxJQ^RIs7v_OqApeN*R)Bde*C%NU;SjPH2;UGG|uhx1OgFBw(Tsxb*>v=`8-|6vc zGwCk>Q<3i*54kmOxiLuIJQN-}x3^DjH_ zDtPsSYGYTD_w!*fB-7@de$$ozEaaI3UiDRWET!KPCtM5A9$l|^mq%jP{*C$2HRM0l zzMw++w|_@tKjhQ;mY0lcxPE!EWpDfwwifuUCn&w1{0@fN!3Ut{*3PmGa$KSOlYR!= z0$ywXl23N8uE|d-F5wS{W#FaXpSj+rd3b{6FHfhSgOu}6x>}>|3YypY$)@_7O18_w z*xyWfn0Wl21u(YA?_lfTeRPmaTSzJY$fCTr2CY$g^>^F3uD&oW?*S6e!O#c1^sH;r zO`lfL$7lU}^m12o ze^@Ni~ZLfq{?N9Kr;BrS6SDk(+{9&X?x$SyzAMd zSGk+~%l$l2zJlH-ZPnWYLG?RmT}X#MWdGG4XF?77+(hMW@~<)K^Fi|$)Suli%SJy7 zveACy(Te0<_H6;Y1%HF;Mn3Y?4ie47G#=>%N0@Nwc9BW8tTCufq_aM(BffV)c5x^B zMV?xN_M(__cDu^^NH_ulJp-tSZtQPw?vAV%Ko6Jz>IV|_o2nlgyGnPOx2leJfLa=d z`~C3^P1*hWp#Ic7?yU53_mc~`Wgqqg*~u0#1QNA@Rrm?RiJ-Y{Sv~hlN7qZgS!S(A zNC&3;%gUW^Csum7$CFR@$cy^8MEReeC?3E2whmMezOd`UW4MR=`LC?&u%3EPI_OugTp1B1rzLf*`nm9(o$801$yXa<_^Yt1BHo6wwpE@*uqA2fEX^sHE)Dk#vW1Nd@4w%kJdD+460 zuMm`eyTNt~?C(8f+h0@2w7(Hyz7@ge2`q^TXuG9fV=&*aFyF`c!?nNSP++^+_Gd?u zbu;a7o-V>#*Kw_Q5jufeXx}f)mRw}Ul25MGfle&7$W{J!@ZAr~<$E5=xB2b{pROo? z(2h_>wp_wy?kE+sR+($*x3HEKp}sw#2x{0L->4;5<)V_otKF! zpbgyn0g7NCroJ!x>U-n9%K3*d0hWQ^dWq`ac+eQD?DtBI45_-rrTSh7O5011szZ~Y zV=V8&NKW*{*3H5*uVzo=txa;5b!r-(cVqML_={RcAI`#$`hE3d%BNRcx~BDTt!Y)b zO%TUHa6h=yQWIAcWqnd!~v(1~c4d3&MhE1OzJti^EC+MEg*EPBO~bR# zA3CfsCj>tr_Xcjl08U*z_Bc4=fw#@#}Fx;z^DAK3?YS<5b| z{swN(_}v381NVCSY|^+E*?S}VT1ZC5M1E&c4!6hhE|9G5bid|W)1VCjzjfhbaMqix zvApvhX%b$f{Z&7wS$5@3?P7eh@RVHAb(g!!;9o)H$6s|&-?|Z;^1t0YJgz-yd|wH& zo3fF88pE^Hvq}4VSNiKYKOj*a@N*#ahjR5k61(w=#^FY4i7S>*xb|&gYbW8|Qa`ZxL`$Yaz}spR|a4q5ZU}$gk&I z+E=qE1qQZ?J`;AyB71Z%#FKx9E6m5uBSjacDE|h+JqocDXcUfhJk2GG#&q2h$Ungq zW@J|69leW|ivG2~=u%&0CAfhc4PCOR{&WG~klgzSob7i?IMyW#Ia2AjQkEYX^o+M1 zIP3C8&O7)D&kgSP0%yOs06V7ktMPrZWl$0MHHPd8PWiQ0>AVE;UymQ>I9zkmZpi*@ z3T03c`Hv!9&6%C@Yg~7F0{L_B^MfPYe>on_JJgpTf1*ChRWz&Q?(JpY8a-=gEb#?QBd>WwoG{g~_O zJ=;Y|GL7E9t%; zIQ6GB$M+%7dJ6vbg@?f@KkXoYc&q4>%zd{^515Cf`gX*6%&aKp? z_xM(lz7?hRums$7U*`$sCtmeaF_;H#+0j*ATh^NPAg%*a+3!yHBcLa!A8_VD$Li1T zY!!VrkMDPOA&s%H6wEx$Q+~q8M(7z{6y65S@!WAQ<=Sn;RTItu&0P!Nuw?dY6Nl_+ zCJckk;7(^f*G93H_^(#cp4ajY=PjB$QMXFQT-=RxNh8_ovCfB8@G4cSi7*wxp?@-DiR zYgfXZ@EnW<=~i{aFP@uXdFLODtjB}&?p&W-!Fk|s<~Z|4deYxjun_#x_mU~dHQ9Uh z`|9VNb;he*jd#iw^q9C+Z`Iz;h1a1Noc1+{oNhm=`>IFX;53lz%7faHQ|~4pvK3u| z%IxKjG*q7t2aV51f^_GWE6DjW!o2_ykY23hkWc%~y}pCBk?Rk^fnIW@a*Z^Vm%7j% z-hio~w%{ed+RiZO0T)At*Zx`L%Yd7~X_qDY6_B>?r<(jChw7F3AoUk&tD0wc$^R4A zGePahcmJ!s-}4|l55>6p{^)~}o&ByR*|fOG@i%Dhpfks-z)Su$w!ap*qA7U(E}WeQ zl|vh74VALrh4j^qQ*FZu*Q3&+e|qWPeQg0@bK^u`>9q`wcS47|n`etA%RQ{8C>zU??j`W5v<`m&!Vz|*iEyvBLXd}u5?6S_jB z?so{9H{?(r4gftr90AIQQ)V4&ys--ULOvulABiVR>2@Ku3~AdDdz9p6$}Q4%G2B9J z7sDlzYW%bIec@W$n+pDL38wu_A-7ws>zVpFAO85koem)F`$Ki6Q2m6A;BT@SQh*vg8wxa+>!4lBe#;+fftXp9@41v4hEXaVg z2}oQT!yE(m!&s1w^OMQlKF#@d!mNhR;AS`$Dl8*$Xnd;ihRRXp=9WuqE-KrpFd7r_ zIc^br1JbYb=L{pAYW&+DROWG?%f`z9VO8H6!(h;y+UP>A?AG_l)1B`NU%@)&H7z2a zcV<6P*Ork_ZqJGKyo%@D?eWtCW`Q%Tmsq6y77&iafDYsjXu?97HAg)kS;0P(0a=l^yT|e_ev9}tUNtgq;aGh~T?o`?<49dGBt5lbTOWD<;Mj9~$o1pkJ@^$=zDrt1A9<2GGXnX%);xmh%hMnG{04_( zw?9cb&bHE<^Mkw;$aUGF$)NhG{%AC|7Wu68fK=Orr)wj_>B@&OJBi4(8({;;-zL__d-3i1cao4# za(xSWh7yE<$agbqv9H-_DqYpZcHkx7^;|c0d2=*&`3p^Qmk&xpK0On81da!vNSFR@+UCgBCIkyg;Qp|!NmJ4NhsK{CK$@Fp1f=5ri{ zokDR<<^ChcwqRrO-{IM5FVfr!@#>tpm#lo6f1U`#!N@nAW6yUJxqb$W1o||~#>~i$ zzWOM#sZIsy*OHZQchb2Kq!S~b#!Q~ye&qTQ@G|@Y^1mS*>(rC+o9du>wqtbVHMW+8aa(Yc-hH^3^8-`^RFe!@N@)|NLf3Q)$$%GZcAG@ns_p!9x&=HNLF z11JLd7t0v;JxM$6hkS|JNnrViSK~O1lZ@^p<21 zN3tp3=fVh(Oh!J958CELd+ihSSuXx%H}41ahbA88GHu7^#_s9Ce($H5BYljFOM$tC zCF@S|5ms{T2B$zL_!72(DVGJ*pW9nRhMz%2@GFW|X;C-y&I!aeoxH^Xx<29|;u z^O`thOK#7NcB8M}*vXbnV;}WZLbg!!}L^k;$t=e!3 zoC#Ub8#KRmma%Mz+K1L6&VrMm+Q?nGTo7HP(5t%Pr!Z$2g%Nu3n&&zw&Oa?WO0n^jM1fzOEiu zg^EM9^q7c;Otp$6A8w(}w}tkz0rhQa>v|UPJ$wM_=VS*C2aR<|J+(i=sz2Hb z)UQ1bvMm~O`PmnZyCu_C@H9l>U`Sd3!l*6EUR(wm$I51ybk#Oj!94iRb~T36b+t|T zTMz2@yTSetxEdPoz>}ac6>;TnX3b=B>u5pGrs28W>C5yS;Ab#nE4i9yPi1ZJ ziQHKC9=umd-t#rb(|AK=@)8^marA4LYhJ9kZdQNq95*>- z(7a_6&qPMFjC{~H8oO#ZGAIv{VIkZBzI(a2r}}mz%mV2S+ty|x&#s=dbe2<3r+6`Wn}${zm)?fqSa!H-Pd@nEdY1r``vpexoZ!JCif_n^< zYY&|730Ffg@Y@_0Ok)k3@$e?+o;GLPL684SIXp-G9ZS3_1GSASK_<2Ak8`z;M_~hO zrhR?PcMv{7e$6q1rse66=ZzzImtdeBx6*$O%GE>8rIP_5{rto?4hB$h*?iNzA7!wNxRw53s9TnK4mht5p8)l~&ElNs3;me8&P$Mn%2#b-b$m8{!(8?S z=J4}~U+Jrkod#thaC{rN%}6Qr*;q564Od`fQS_Z>QtukVRp=}&?) zz?uFDoPP|;{}OB}`CmUbLHe?JJz>9iG!K8hKi{5cN8EEk>5qd`z?ps~=WhWvCvO$) zzCX`_60NVw20u|2iO&J?Wx!Ri7?l1{IM(T%^ZnrgP(SyxY$xCM{*d@LfnWU#n*L$L z*%LHQQ2LL7hC*e3oM*tLpgO{u)Oj;`c1j)C7_SZlO}`0ojseQn>O(KUEjN$lT()0j zqBekx`DC25A%CD|VU&G*T(E5zlVeSO2{i*J%Km7tESD@dMCl0Nz)rOPdeCP?1!{}IL zycFo;tbP3_;T`}A7b2ZTAl)yA*$@Wx8NTh%Ll-rPOLamv>OvR=s@KZD(*G8&0O{%& z*xkfII(na~0o)2&H`^V|?Le*_XMfzE!RXx2)tI+Vpojx{BEJ3M9?(3?FI`=ioqGV{ z*6o1FmU$ySwS|W8Hn{7akw^Ab<)C_^seIc0h)-=H16016f3E`N)#O?I%fF!=NY=n< zvsR!l3F^Cu*{q#pLakybvMFR4!bLUCZFX?a@H@xb_b3jPKv50isV0cqf^@3wD6CNf z_%!7o)DMM1hrt<8wob8U;&lq;5o8~Zf_zZ9e+|-w+C~Y?0FA5EW;DhN^gay!6t`@8 zeNele2AUhFj1})1ke%HMx~8$+>u?z;9J%rTOUYmEL+I)zRg4E zM_y;o6@6~LWRVT+4CUr1N{nk_Zx{-j@V_EE`pP?eJMK&7YHRIyonsZ*+~1hXeA_be z(O~wjOdy>45xF~MfJ@R#4k)kdqp|h_8|SW@g^cR!onsGY8agJ8b@O;vxHsWcKFF5W z*Qp@Rt3f?@7rB3-A05>+{M{%IncZi;0v%?tK_ z(SKUsa8$c~9;|kNtMY9PQz+k`*vqW)o$nL>Kxf@^9uqzrCV|TLYB1%i`lxg z6j4^_$WwmL_z9~xy1`GNHdG%>{WN88BiFDe=Pcs;0KJLdJ;#%Nc8Z@c3a@^67Hk96 zA&pC%*^?jX=?hT#tj~_T(VK4vQcpIO5U*^G=IvJe+?Ri$xlc<_-$uL2pTzf+&c&a~ zs9gN^9N_}|jfZWNHS?s$hVsn)Q^w_UNM|u&UgDl~p*C|cNY8@-8=gOp`u{L{ns2~g zL(ntXa%tdC`BWJ7AIQ^oCF_t=nUB58y!2&knc7J++s|{j^FZ~Tx$DJ+%rm+m$5+HV z5YB|1#sm13d`H5Y@B?fF)rsF>hmGvMbiEj)8|Dz!*u5B6x||NWR&G5@nZd768ppw9 z@FZZH%ARTdhhwFSJ+#`*8Mu2Bm*yv$XX%|-jTw&s-?1uTs>4B`@ZDe$C=aTC{Xu`q zr&U*ot1f8%aS!Yd)uEjJohO7-8A|?nVD#d43%R$SGhEPP!qtEV@Nba5opI}2wqEVN zX3#jD{s^aXknPI>jU&`9l!p2b_2-X(?mL6+bX>yAOFUD8J$UxCN2t(7qozMF!MIjy zEIbI+LPZ(CtzuwIW})Y2+j)X!sL%O9AlHn;a2(X1yeWgYq(_Pmd0O`J9TuO_qqZW8-Q$K^)5e?^<6Znas4U-gG4Ls?mKthiK1 z+QKAQgWdcb`!SyN2={oZguTNvSSJ|C8rrw`T@DYykw!p{r32Zdp0JMoW(4+eCiX&h zSZ&(XUv1vT_-SFw$l$l}FB>79Mji2GaDEZGn!)&Ldh_t4u{+1%j?+5d%oy_>!f71) z0@McORqoxa)yzXT6It(&EpW-?eU5GXlyX`?c#RKF1eMKU@GNU6^I5YPN1w@dWo%yUQ6W==bxKldRJX?0G94#WB-a%a&O?cHG)id=~ zl8^n^lCRHL*2~@NZ5odZ2hG{b5s7tZO1)8C{Su6P`*M5>*MA7dI!|gE{&5E3ocdFI zLrwV^moU=()gb-719d=kr6#n7iPX~{vm);gCA|7*6X#Cx;a71f9AgP;2NB;w_8M_Ai|u0vE$_!i=XqMRTIhS8o=cZi#REYmPe%*nit) z6z_c%v48YC>b1TjqCQ6HW`OEN7nq8U=CaQED9_kh;ioppn3*{D!w>aZx;?vuedRn4 zdw5jK$dGsWJrk4yfNe6q-Oir~!Xu)^UVi>GCE}-BMl+%KKYT56X1! z2~{&zEAOkZLr3*mR>nlc(tRhfdBg@V~?iM_7>4+ z{KULz1^fQL&x!V(Oy5Bq)_5cbcMnN`D|uvN=+D|sA>2&b>;}qQ?NzpbdD-S)Xmg9P z5ySDf3~1wdT3%83Bgo@Z?E9SFBJ!EWrpA7FxxbLF&&bDQ!nFa}654H>f%F;3JjdN9 z$mVKYz6|yz@SURb)K}dE`@osB;~}}RZc}-`ME#~oSMy1=vHni@4k|Xjo$RXWyUMEp z;RmBSUf=-LU8ZWa#^1t2?G$sH5 literal 0 HcmV?d00001 diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/logo.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/logo.svg new file mode 100644 index 0000000000..87756e5d56 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/logo.svg @@ -0,0 +1,18 @@ + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/off.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/off.svg new file mode 100644 index 0000000000..57ce672b00 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/off.svg @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/open-folder.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/open-folder.svg new file mode 100644 index 0000000000..7500c1603c --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/open-folder.svg @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/open-folder2.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/open-folder2.svg new file mode 100644 index 0000000000..70cc9c53f4 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/open-folder2.svg @@ -0,0 +1,16 @@ + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/play.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/play.svg new file mode 100644 index 0000000000..8bbdf01fa0 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/play.svg @@ -0,0 +1,11 @@ + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/reload-folder2.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/reload-folder2.svg new file mode 100644 index 0000000000..1f6d7a662d --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/reload-folder2.svg @@ -0,0 +1,24 @@ + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/reload.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/reload.svg new file mode 100644 index 0000000000..796319e581 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/reload.svg @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/save.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/save.svg new file mode 100644 index 0000000000..e81c73940c --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/save.svg @@ -0,0 +1,73 @@ + + +image/svg+xml \ No newline at end of file diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/search-next.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/search-next.svg new file mode 100644 index 0000000000..e9ff44d805 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/search-next.svg @@ -0,0 +1,23 @@ + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/search-prev.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/search-prev.svg new file mode 100644 index 0000000000..8dbd6fb4e7 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/search-prev.svg @@ -0,0 +1,23 @@ + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/settings.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/settings.svg new file mode 100644 index 0000000000..783969c7c1 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/settings.svg @@ -0,0 +1,39 @@ + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/statistics.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/statistics.svg new file mode 100644 index 0000000000..bbb4428d62 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/statistics.svg @@ -0,0 +1,14 @@ + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/statistics2.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/statistics2.svg new file mode 100644 index 0000000000..3f5c3addcd --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/statistics2.svg @@ -0,0 +1,16 @@ + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/stop.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/stop.svg new file mode 100644 index 0000000000..a914d1ecc7 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/stop.svg @@ -0,0 +1,11 @@ + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/wifi.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/wifi.svg new file mode 100644 index 0000000000..cb04aceae9 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/wifi.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/icons/wifi_on.svg b/Source/ThirdParty/easy_profiler/profiler_gui/icons/wifi_on.svg new file mode 100644 index 0000000000..461e2b772d --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/icons/wifi_on.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/main.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/main.cpp new file mode 100644 index 0000000000..7d19cb3fe7 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/main.cpp @@ -0,0 +1,94 @@ +/************************************************************************ +* file name : main.cpp +* ----------------- : +* creation time : 2016/04/29 +* authors : Sergey Yagovtsev, Victor Zarubkin +* email : yse.sey@gmail.com, v.s.zarubkin@gmail.com +* ----------------- : +* description : Main file for EasyProfiler GUI. +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include +#include + +#include "main_window.h" + +#include + + +#if defined(_WIN32) && defined (_BUILD_RELEASE_) +#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup") +#endif + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + //QFileSystemModel *model = new QFileSystemModel; + //model->setRootPath(QDir::currentPath()); +// const char* filename = 0; +// if(argc > 1 && argv[1]){ +// filename = argv[1]; +// }else{ +// return 255; +// } + +// QFile file(filename); +// file.open(QIODevice::ReadOnly); +// TreeModel model(file.readAll()); +// file.close(); + + +// QTreeView *tree = new QTreeView(); +// tree->setModel(&model); +// +// tree->show(); + + auto now = ::std::chrono::duration_cast(::std::chrono::system_clock::now().time_since_epoch()).count() >> 1; + srand((unsigned int)now); + + EasyMainWindow window; + window.show(); + + return app.exec(); +} diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/main_window.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/main_window.cpp new file mode 100644 index 0000000000..8c5ecea79a --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/main_window.cpp @@ -0,0 +1,2858 @@ +/************************************************************************ +* file name : main_window.cpp +* ----------------- : +* creation time : 2016/06/26 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of MainWindow for easy_profiler GUI. +* ----------------- : +* change log : * 2016/06/26 Victor Zarubkin: Initial commit. +* : +* : * 2016/06/27 Victor Zarubkin: Passing blocks number to EasyTreeWidget::setTree(). +* : +* : * 2016/06/29 Victor Zarubkin: Added menu with tests. +* : +* : * 2016/06/30 Sergey Yagovtsev: Open file by command line argument +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "main_window.h" +#include "blocks_tree_widget.h" +#include "blocks_graphics_view.h" +#include "descriptors_tree_widget.h" +#include "easy_frame_rate_viewer.h" +#include "globals.h" + +#include + +#ifdef max +#undef max +#endif + +#ifdef min +#undef min +#endif + +////////////////////////////////////////////////////////////////////////// + +#define EASY_DEFAULT_WINDOW_TITLE "EasyProfiler" + +const int LOADER_TIMER_INTERVAL = 40; +const auto NETWORK_CACHE_FILE = "easy_profiler_stream.cache"; + +////////////////////////////////////////////////////////////////////////// + +inline void clear_stream(std::stringstream& _stream) +{ +#if defined(__GNUC__) && __GNUC__ < 5 + // gcc 4 has a known bug which has been solved in gcc 5: + // std::stringstream has no swap() method :( + _stream.str(std::string()); +#else + std::stringstream().swap(_stream); +#endif +} + +////////////////////////////////////////////////////////////////////////// + +EasyMainWindow::EasyMainWindow() : Parent(), m_lastAddress("localhost"), m_lastPort(::profiler::DEFAULT_PORT) +{ + { QIcon icon(":/logo"); if (!icon.isNull()) QApplication::setWindowIcon(icon); } + + setObjectName("ProfilerGUI_MainWindow"); + setWindowTitle(EASY_DEFAULT_WINDOW_TITLE); + setDockNestingEnabled(true); + setAcceptDrops(true); + resize(800, 600); + + setStatusBar(nullptr); + + m_graphicsView = new QDockWidget("Diagram", this); + m_graphicsView->setObjectName("ProfilerGUI_Diagram"); + m_graphicsView->setMinimumHeight(50); + m_graphicsView->setAllowedAreas(Qt::AllDockWidgetAreas); + + auto graphicsView = new EasyGraphicsViewWidget(this); + m_graphicsView->setWidget(graphicsView); + + m_treeWidget = new QDockWidget("Hierarchy", this); + m_treeWidget->setObjectName("ProfilerGUI_Hierarchy"); + m_treeWidget->setMinimumHeight(50); + m_treeWidget->setAllowedAreas(Qt::AllDockWidgetAreas); + + auto treeWidget = new EasyHierarchyWidget(this); + m_treeWidget->setWidget(treeWidget); + + m_fpsViewer = new QDockWidget("FPS Monitor", this); + m_fpsViewer->setObjectName("ProfilerGUI_FPS"); + m_fpsViewer->setWidget(new EasyFrameRateViewer(this)); + m_fpsViewer->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); + + addDockWidget(Qt::TopDockWidgetArea, m_graphicsView); + addDockWidget(Qt::BottomDockWidgetArea, m_treeWidget); + addDockWidget(Qt::TopDockWidgetArea, m_fpsViewer); + +#if EASY_GUI_USE_DESCRIPTORS_DOCK_WINDOW != 0 + auto descTree = new EasyDescWidget(); + m_descTreeWidget = new QDockWidget("Blocks"); + m_descTreeWidget->setObjectName("ProfilerGUI_Blocks"); + m_descTreeWidget->setMinimumHeight(50); + m_descTreeWidget->setAllowedAreas(Qt::AllDockWidgetAreas); + m_descTreeWidget->setWidget(descTree); + addDockWidget(Qt::BottomDockWidgetArea, m_descTreeWidget); +#endif + + + loadSettings(); + + + auto toolbar = addToolBar("FileToolbar"); + toolbar->setIconSize(::profiler_gui::ICONS_SIZE); + toolbar->setObjectName("ProfilerGUI_FileToolbar"); + toolbar->setContentsMargins(1, 0, 1, 0); + + m_loadActionMenu = new QMenu(this); + auto action = m_loadActionMenu->menuAction(); + action->setText("Open file"); + action->setIcon(QIcon(":/Open")); + connect(action, &QAction::triggered, this, &This::onOpenFileClicked); + toolbar->addAction(action); + + for (const auto& f : m_lastFiles) + { + action = new QAction(f, this); + connect(action, &QAction::triggered, this, &This::onOpenFileClicked); + m_loadActionMenu->addAction(action); + } + + m_saveAction = toolbar->addAction(QIcon(":/Save"), tr("Save"), this, SLOT(onSaveFileClicked(bool))); + m_deleteAction = toolbar->addAction(QIcon(":/Delete"), tr("Clear all"), this, SLOT(onDeleteClicked(bool))); + + m_saveAction->setEnabled(false); + m_deleteAction->setEnabled(false); + + + + toolbar = addToolBar("ProfileToolbar"); + toolbar->setIconSize(::profiler_gui::ICONS_SIZE); + toolbar->setObjectName("ProfilerGUI_ProfileToolbar"); + toolbar->setContentsMargins(1, 0, 1, 0); + + toolbar->addAction(QIcon(":/List"), tr("Blocks"), this, SLOT(onEditBlocksClicked(bool))); + m_captureAction = toolbar->addAction(QIcon(":/Start"), tr("Capture"), this, SLOT(onCaptureClicked(bool))); + m_captureAction->setEnabled(false); + + toolbar->addSeparator(); + m_connectAction = toolbar->addAction(QIcon(":/Connection"), tr("Connect"), this, SLOT(onConnectClicked(bool))); + + auto lbl = new QLabel("Address:", toolbar); + lbl->setContentsMargins(5, 0, 2, 0); + toolbar->addWidget(lbl); + m_addressEdit = new QLineEdit(); + m_addressEdit->setToolTip("Enter IP-address or host name"); + //QRegExp rx("^0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})(\\.0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})){3}$"); + //m_addressEdit->setValidator(new QRegExpValidator(rx, m_addressEdit)); + m_addressEdit->setText(m_lastAddress); + m_addressEdit->setFixedWidth((m_addressEdit->fontMetrics().width(QString("255.255.255.255")) * 3) / 2); + toolbar->addWidget(m_addressEdit); + + lbl = new QLabel("Port:", toolbar); + lbl->setContentsMargins(5, 0, 2, 0); + toolbar->addWidget(lbl); + m_portEdit = new QLineEdit(); + m_portEdit->setValidator(new QIntValidator(1, 65535, m_portEdit)); + m_portEdit->setText(QString::number(m_lastPort)); + m_portEdit->setFixedWidth(m_portEdit->fontMetrics().width(QString("000000")) + 10); + toolbar->addWidget(m_portEdit); + + connect(m_addressEdit, &QLineEdit::returnPressed, [this](){ onConnectClicked(true); }); + connect(m_portEdit, &QLineEdit::returnPressed, [this](){ onConnectClicked(true); }); + + + + toolbar = addToolBar("SetupToolbar"); + toolbar->setIconSize(::profiler_gui::ICONS_SIZE); + toolbar->setObjectName("ProfilerGUI_SetupToolbar"); + toolbar->setContentsMargins(1, 0, 1, 0); + + toolbar->addAction(QIcon(":/Expand"), "Expand all", this, SLOT(onExpandAllClicked(bool))); + toolbar->addAction(QIcon(":/Collapse"), "Collapse all", this, SLOT(onCollapseAllClicked(bool))); + + toolbar->addSeparator(); + auto menu = new QMenu("Settings", this); + menu->setToolTipsVisible(true); + + QToolButton* toolButton = new QToolButton(toolbar); + toolButton->setIcon(QIcon(":/Settings")); + toolButton->setMenu(menu); + toolButton->setPopupMode(QToolButton::InstantPopup); + toolbar->addWidget(toolButton); + + action = menu->addAction("Statistics enabled"); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.enable_statistics); + connect(action, &QAction::triggered, this, &This::onEnableDisableStatistics); + if (EASY_GLOBALS.enable_statistics) + { + auto f = action->font(); + f.setBold(true); + action->setFont(f); + SET_ICON(action, ":/Stats"); + } + else + { + action->setText("Statistics disabled"); + SET_ICON(action, ":/Stats-off"); + } + + + action = menu->addAction("Only frames on histogram"); + action->setToolTip("Display only top-level blocks on histogram."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.display_only_frames_on_histogram); + connect(action, &QAction::triggered, [this](bool _checked) + { + EASY_GLOBALS.display_only_frames_on_histogram = _checked; + emit EASY_GLOBALS.events.displayOnlyFramesOnHistogramChanged(); + }); + + + menu->addSeparator(); + auto submenu = menu->addMenu("View"); + submenu->setToolTipsVisible(true); + action = submenu->addAction("Draw items' borders"); + action->setToolTip("Draw borders for blocks on diagram.\nThis reduces performance."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.draw_graphics_items_borders); + connect(action, &QAction::triggered, [this](bool _checked){ EASY_GLOBALS.draw_graphics_items_borders = _checked; refreshDiagram(); }); + + action = submenu->addAction("Overlap narrow children"); + action->setToolTip("Children blocks will be overlaped by narrow\nparent blocks. See also \'Blocks narrow size\'.\nThis improves performance."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.hide_narrow_children); + connect(action, &QAction::triggered, [this](bool _checked){ EASY_GLOBALS.hide_narrow_children = _checked; refreshDiagram(); }); + + action = submenu->addAction("Hide min-size blocks"); + action->setToolTip("Hides blocks which screen size\nis less than \'Min blocks size\'."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.hide_minsize_blocks); + connect(action, &QAction::triggered, [this](bool _checked){ EASY_GLOBALS.hide_minsize_blocks = _checked; refreshDiagram(); }); + + action = submenu->addAction("Build hierarchy only for current thread"); + action->setToolTip("Hierarchy tree will be built\nfor blocks from current thread only.\nThis improves performance\nand saves a lot of memory."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.only_current_thread_hierarchy); + connect(action, &QAction::triggered, this, &This::onHierarchyFlagChange); + + action = submenu->addAction("Add zero blocks to hierarchy"); + action->setToolTip("Zero duration blocks will be added into hierarchy tree.\nThis reduces performance and increases memory consumption."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.add_zero_blocks_to_hierarchy); + connect(action, &QAction::triggered, [this](bool _checked) + { + EASY_GLOBALS.add_zero_blocks_to_hierarchy = _checked; + emit EASY_GLOBALS.events.hierarchyFlagChanged(_checked); + }); + + action = submenu->addAction("Enable zero duration blocks on diagram"); + action->setToolTip("If checked then allows diagram to paint zero duration blocks\nwith 1px width on each scale. Otherwise, such blocks will be resized\nto 250ns duration."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.enable_zero_length); + connect(action, &QAction::triggered, [this](bool _checked){ EASY_GLOBALS.enable_zero_length = _checked; refreshDiagram(); }); + + action = submenu->addAction("Highlight similar blocks"); + action->setToolTip("Highlight all visible blocks which are similar\nto the current selected block.\nThis reduces performance."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.highlight_blocks_with_same_id); + connect(action, &QAction::triggered, [this](bool _checked){ EASY_GLOBALS.highlight_blocks_with_same_id = _checked; refreshDiagram(); }); + + action = submenu->addAction("Collapse blocks on tree reset"); + action->setToolTip("This collapses all blocks on diagram\nafter hierarchy tree reset."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.collapse_items_on_tree_close); + connect(action, &QAction::triggered, this, &This::onCollapseItemsAfterCloseChanged); + + action = submenu->addAction("Expand all on file open"); + action->setToolTip("If checked then all blocks on diagram\nwill be initially expanded."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.all_items_expanded_by_default); + connect(action, &QAction::triggered, this, &This::onAllItemsExpandedByDefaultChange); + + action = submenu->addAction("Bind diagram and tree expand"); + action->setToolTip("Expanding/collapsing blocks at diagram expands/collapses\nblocks at hierarchy tree and wise versa."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.bind_scene_and_tree_expand_status); + connect(action, &QAction::triggered, this, &This::onBindExpandStatusChange); + + action = submenu->addAction("Selecting block changes current thread"); + action->setToolTip("Automatically select thread while selecting a block.\nIf not checked then you will have to select current thread\nmanually double clicking on thread name on a diagram."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.selecting_block_changes_thread); + connect(action, &QAction::triggered, [this](bool _checked){ EASY_GLOBALS.selecting_block_changes_thread = _checked; }); + + action = submenu->addAction("Draw event markers"); + action->setToolTip("Display event markers under the blocks\n(even if event-blocks are not visible).\nThis slightly reduces performance."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.enable_event_markers); + connect(action, &QAction::triggered, [this](bool _checked) + { + EASY_GLOBALS.enable_event_markers = _checked; + refreshDiagram(); + }); + + action = submenu->addAction("Automatically adjust histogram height"); + action->setToolTip("You do not need to adjust boundaries manually,\nbut this restricts you from adjusting boundaries at all (zoom mode).\nYou can still adjust boundaries in overview mode though."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.auto_adjust_histogram_height); + connect(action, &QAction::triggered, [](bool _checked) + { + EASY_GLOBALS.auto_adjust_histogram_height = _checked; + emit EASY_GLOBALS.events.autoAdjustHistogramChanged(); + }); + + action = submenu->addAction("Use decorated thread names"); + action->setToolTip("Add \'Thread\' word into thread name if there is no one already.\nExamples: \'Render\' will change to \'Render Thread\'\n\'WorkerThread\' will not change."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.use_decorated_thread_name); + connect(action, &QAction::triggered, [this](bool _checked) + { + EASY_GLOBALS.use_decorated_thread_name = _checked; + emit EASY_GLOBALS.events.threadNameDecorationChanged(); + }); + + action = submenu->addAction("Display hex thread id"); + action->setToolTip("Display hex thread id instead of decimal."); + action->setCheckable(true); + action->setChecked(EASY_GLOBALS.hex_thread_id); + connect(action, &QAction::triggered, [this](bool _checked) + { + EASY_GLOBALS.hex_thread_id = _checked; + emit EASY_GLOBALS.events.hexThreadIdChanged(); + }); + + submenu->addSeparator(); + auto actionGroup = new QActionGroup(this); + actionGroup->setExclusive(true); + + action = new QAction("Chrono text at top", actionGroup); + action->setToolTip("Draw duration of selected interval\nat the top of the screen."); + action->setCheckable(true); + action->setData(static_cast(::profiler_gui::ChronoTextPosition_Top)); + if (EASY_GLOBALS.chrono_text_position == ::profiler_gui::ChronoTextPosition_Top) + action->setChecked(true); + submenu->addAction(action); + connect(action, &QAction::triggered, this, &This::onChronoTextPosChanged); + + action = new QAction("Chrono text at center", actionGroup); + action->setToolTip("Draw duration of selected interval\nat the center of the screen."); + action->setCheckable(true); + action->setData(static_cast(::profiler_gui::ChronoTextPosition_Center)); + if (EASY_GLOBALS.chrono_text_position == ::profiler_gui::ChronoTextPosition_Center) + action->setChecked(true); + submenu->addAction(action); + connect(action, &QAction::triggered, this, &This::onChronoTextPosChanged); + + action = new QAction("Chrono text at bottom", actionGroup); + action->setToolTip("Draw duration of selected interval\nat the bottom of the screen."); + action->setCheckable(true); + action->setData(static_cast(::profiler_gui::ChronoTextPosition_Bottom)); + if (EASY_GLOBALS.chrono_text_position == ::profiler_gui::ChronoTextPosition_Bottom) + action->setChecked(true); + submenu->addAction(action); + connect(action, &QAction::triggered, this, &This::onChronoTextPosChanged); + + submenu->addSeparator(); + auto w = new QWidget(submenu); + auto l = new QHBoxLayout(w); + l->setContentsMargins(33, 1, 1, 1); + l->addWidget(new QLabel("Min blocks spacing, px", w), 0, Qt::AlignLeft); + auto spinbox = new QSpinBox(w); + spinbox->setRange(0, 400); + spinbox->setValue(EASY_GLOBALS.blocks_spacing); + spinbox->setFixedWidth(50); + connect(spinbox, SIGNAL(valueChanged(int)), this, SLOT(onSpacingChange(int))); + l->addWidget(spinbox); + w->setLayout(l); + auto waction = new QWidgetAction(submenu); + waction->setDefaultWidget(w); + submenu->addAction(waction); + + w = new QWidget(submenu); + l = new QHBoxLayout(w); + l->setContentsMargins(33, 1, 1, 1); + l->addWidget(new QLabel("Min blocks size, px", w), 0, Qt::AlignLeft); + spinbox = new QSpinBox(w); + spinbox->setRange(1, 400); + spinbox->setValue(EASY_GLOBALS.blocks_size_min); + spinbox->setFixedWidth(50); + connect(spinbox, SIGNAL(valueChanged(int)), this, SLOT(onMinSizeChange(int))); + l->addWidget(spinbox); + w->setLayout(l); + waction = new QWidgetAction(submenu); + waction->setDefaultWidget(w); + submenu->addAction(waction); + + w = new QWidget(submenu); + l = new QHBoxLayout(w); + l->setContentsMargins(33, 1, 1, 1); + l->addWidget(new QLabel("Blocks narrow size, px", w), 0, Qt::AlignLeft); + spinbox = new QSpinBox(w); + spinbox->setRange(1, 400); + spinbox->setValue(EASY_GLOBALS.blocks_narrow_size); + spinbox->setFixedWidth(50); + connect(spinbox, SIGNAL(valueChanged(int)), this, SLOT(onNarrowSizeChange(int))); + l->addWidget(spinbox); + w->setLayout(l); + waction = new QWidgetAction(submenu); + waction->setDefaultWidget(w); + submenu->addAction(waction); + + + + + submenu = menu->addMenu("FPS Monitor"); + w = new QWidget(submenu); + l = new QHBoxLayout(w); + l->setContentsMargins(33, 1, 1, 1); + l->addWidget(new QLabel("Request interval, ms", w), 0, Qt::AlignLeft); + spinbox = new QSpinBox(w); + spinbox->setRange(1, 600000); + spinbox->setValue(EASY_GLOBALS.fps_timer_interval); + spinbox->setFixedWidth(50); + connect(spinbox, SIGNAL(valueChanged(int)), this, SLOT(onFpsIntervalChange(int))); + l->addWidget(spinbox); + w->setLayout(l); + waction = new QWidgetAction(submenu); + waction->setDefaultWidget(w); + submenu->addAction(waction); + + w = new QWidget(submenu); + l = new QHBoxLayout(w); + l->setContentsMargins(33, 1, 1, 1); + l->addWidget(new QLabel("Max history size", w), 0, Qt::AlignLeft); + spinbox = new QSpinBox(w); + spinbox->setRange(2, 200); + spinbox->setValue(EASY_GLOBALS.max_fps_history); + spinbox->setFixedWidth(50); + connect(spinbox, SIGNAL(valueChanged(int)), this, SLOT(onFpsHistoryChange(int))); + l->addWidget(spinbox); + w->setLayout(l); + waction = new QWidgetAction(submenu); + waction->setDefaultWidget(w); + submenu->addAction(waction); + + w = new QWidget(submenu); + l = new QHBoxLayout(w); + l->setContentsMargins(33, 1, 1, 1); + l->addWidget(new QLabel("Line width, px", w), 0, Qt::AlignLeft); + spinbox = new QSpinBox(w); + spinbox->setRange(1, 6); + spinbox->setValue(EASY_GLOBALS.fps_widget_line_width); + spinbox->setFixedWidth(50); + connect(spinbox, SIGNAL(valueChanged(int)), this, SLOT(onFpsMonitorLineWidthChange(int))); + l->addWidget(spinbox); + w->setLayout(l); + waction = new QWidgetAction(submenu); + waction->setDefaultWidget(w); + submenu->addAction(waction); + + + + + submenu = menu->addMenu("Units"); + actionGroup = new QActionGroup(this); + actionGroup->setExclusive(true); + action = new QAction("Auto", actionGroup); + action->setCheckable(true); + action->setData(static_cast(::profiler_gui::TimeUnits_auto)); + if (EASY_GLOBALS.time_units == ::profiler_gui::TimeUnits_auto) + action->setChecked(true); + submenu->addAction(action); + connect(action, &QAction::triggered, this, &This::onUnitsChanged); + + action = new QAction("Milliseconds", actionGroup); + action->setCheckable(true); + action->setData(static_cast(::profiler_gui::TimeUnits_ms)); + if (EASY_GLOBALS.time_units == ::profiler_gui::TimeUnits_ms) + action->setChecked(true); + submenu->addAction(action); + connect(action, &QAction::triggered, this, &This::onUnitsChanged); + + action = new QAction("Microseconds", actionGroup); + action->setCheckable(true); + action->setData(static_cast(::profiler_gui::TimeUnits_us)); + if (EASY_GLOBALS.time_units == ::profiler_gui::TimeUnits_us) + action->setChecked(true); + submenu->addAction(action); + connect(action, &QAction::triggered, this, &This::onUnitsChanged); + + action = new QAction("Nanoseconds", actionGroup); + action->setCheckable(true); + action->setData(static_cast(::profiler_gui::TimeUnits_ns)); + if (EASY_GLOBALS.time_units == ::profiler_gui::TimeUnits_ns) + action->setChecked(true); + submenu->addAction(action); + connect(action, &QAction::triggered, this, &This::onUnitsChanged); + + + submenu = menu->addMenu("Remote"); + m_eventTracingEnableAction = submenu->addAction("Event tracing enabled"); + m_eventTracingEnableAction->setCheckable(true); + m_eventTracingEnableAction->setEnabled(false); + connect(m_eventTracingEnableAction, &QAction::triggered, this, &This::onEventTracingEnableChange); + + m_eventTracingPriorityAction = submenu->addAction("Low priority event tracing"); + m_eventTracingPriorityAction->setCheckable(true); + m_eventTracingPriorityAction->setChecked(EASY_OPTION_LOW_PRIORITY_EVENT_TRACING); + m_eventTracingPriorityAction->setEnabled(false); + connect(m_eventTracingPriorityAction, &QAction::triggered, this, &This::onEventTracingPriorityChange); + + + submenu = menu->addMenu("Encoding"); + actionGroup = new QActionGroup(this); + actionGroup->setExclusive(true); + + auto default_codec_mib = QTextCodec::codecForLocale()->mibEnum(); + foreach(int mib, QTextCodec::availableMibs()) + { + auto codec = QTextCodec::codecForMib(mib)->name(); + + action = new QAction(codec, actionGroup); + action->setCheckable(true); + if (mib == default_codec_mib) + action->setChecked(true); + + submenu->addAction(action); + connect(action, &QAction::triggered, this, &This::onEncodingChanged); + } + + auto tb_height = toolbar->height() + 4; + toolbar = addToolBar("FrameToolbar"); + toolbar->setIconSize(::profiler_gui::ICONS_SIZE); + toolbar->setObjectName("ProfilerGUI_FrameToolbar"); + toolbar->setContentsMargins(1, 0, 1, 0); + toolbar->setMinimumHeight(tb_height); + + lbl = new QLabel("Expected frame time:", toolbar); + lbl->setContentsMargins(5, 2, 2, 2); + toolbar->addWidget(lbl); + + m_frameTimeEdit = new QLineEdit(); + m_frameTimeEdit->setFixedWidth(70); + auto val = new QDoubleValidator(m_frameTimeEdit); + val->setLocale(QLocale::c()); + val->setBottom(0); + m_frameTimeEdit->setValidator(val); + m_frameTimeEdit->setText(QString::number(EASY_GLOBALS.frame_time * 1e-3)); + connect(m_frameTimeEdit, &QLineEdit::editingFinished, this, &This::onFrameTimeEditFinish); + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::expectedFrameTimeChanged, this, &This::onFrameTimeChanged); + toolbar->addWidget(m_frameTimeEdit); + + lbl = new QLabel("ms", toolbar); + lbl->setContentsMargins(5, 2, 1, 1); + toolbar->addWidget(lbl); + + + connect(graphicsView->view(), &EasyGraphicsView::intervalChanged, treeWidget->tree(), &EasyTreeWidget::setTreeBlocks); + connect(&m_readerTimer, &QTimer::timeout, this, &This::onFileReaderTimeout); + connect(&m_listenerTimer, &QTimer::timeout, this, &This::onListenerTimerTimeout); + connect(&m_fpsRequestTimer, &QTimer::timeout, this, &This::onFrameTimeRequestTimeout); + + + m_progress = new QProgressDialog("Loading file...", "Cancel", 0, 100, this); + m_progress->setFixedWidth(300); + m_progress->setWindowTitle(EASY_DEFAULT_WINDOW_TITLE); + m_progress->setModal(true); + m_progress->setValue(100); + //m_progress->hide(); + connect(m_progress, &QProgressDialog::canceled, this, &This::onFileReaderCancel); + + loadGeometry(); + + if(QCoreApplication::arguments().size() > 1) + { + auto opened_filename = QCoreApplication::arguments().at(1); + loadFile(opened_filename); + } + + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::blockStatusChanged, this, &This::onBlockStatusChange); + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::blocksRefreshRequired, this, &This::onGetBlockDescriptionsClicked); +} + +EasyMainWindow::~EasyMainWindow() +{ + delete m_progress; +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::dragEnterEvent(QDragEnterEvent* drag_event) +{ + if (drag_event->mimeData()->hasUrls()) + drag_event->acceptProposedAction(); +} + +void EasyMainWindow::dragMoveEvent(QDragMoveEvent* drag_event) +{ + if (drag_event->mimeData()->hasUrls()) + drag_event->acceptProposedAction(); +} + +void EasyMainWindow::dragLeaveEvent(QDragLeaveEvent* drag_event) +{ + drag_event->accept(); +} + +void EasyMainWindow::dropEvent(QDropEvent* drop_event) +{ + const auto& urls = drop_event->mimeData()->urls(); + if (!urls.empty()) + { + if (m_bNetworkFileRegime) + { + // Warn user about unsaved network information and suggest to save + auto result = QMessageBox::question(this, "Unsaved session", "You have unsaved data!\nSave before opening new file?", QMessageBox::Yes, QMessageBox::No, QMessageBox::Cancel); + if (result == QMessageBox::Yes) + { + onSaveFileClicked(true); + } + else if (result != QMessageBox::No) + { + // User cancelled opening new file + return; + } + } + + loadFile(urls.front().toLocalFile()); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onOpenFileClicked(bool) +{ + auto action = qobject_cast(sender()); + if (action == nullptr) + return; + + QString filename; + + if (action == m_loadActionMenu->menuAction()) + filename = QFileDialog::getOpenFileName(this, "Open EasyProfiler File", m_lastFiles.empty() ? QString() : m_lastFiles.front(), "EasyProfiler File (*.prof);;All Files (*.*)"); + else + filename = action->text(); + + if (!filename.isEmpty()) + { + if (m_bNetworkFileRegime) + { + // Warn user about unsaved network information and suggest to save + auto result = QMessageBox::question(this, "Unsaved session", "You have unsaved data!\nSave before opening new file?", QMessageBox::Yes, QMessageBox::No, QMessageBox::Cancel); + if (result == QMessageBox::Yes) + { + onSaveFileClicked(true); + } + else if (result != QMessageBox::No) + { + // User cancelled opening new file + return; + } + } + + loadFile(filename); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::addFileToList(const QString& filename) +{ + m_lastFiles.push_front(filename); + + auto action = new QAction(filename, this); + connect(action, &QAction::triggered, this, &This::onOpenFileClicked); + auto fileActions = m_loadActionMenu->actions(); + if (fileActions.empty()) + m_loadActionMenu->addAction(action); + else + m_loadActionMenu->insertAction(fileActions.front(), action); + + if (m_lastFiles.size() > 10) + { + // Keep 10 files at the list + m_lastFiles.pop_back(); + m_loadActionMenu->removeAction(fileActions.back()); + delete fileActions.back(); + } + + m_bOpenedCacheFile = filename.contains(NETWORK_CACHE_FILE); + + if (m_bOpenedCacheFile) + setWindowTitle(QString(EASY_DEFAULT_WINDOW_TITLE " - [%1] - UNSAVED network cache file").arg(m_lastFiles.front())); + else + setWindowTitle(QString(EASY_DEFAULT_WINDOW_TITLE " - [%1]").arg(m_lastFiles.front())); +} + +void EasyMainWindow::loadFile(const QString& filename) +{ + const auto i = filename.lastIndexOf(QChar('/')); + const auto j = filename.lastIndexOf(QChar('\\')); + m_progress->setLabelText(QString("Loading %1...").arg(filename.mid(::std::max(i, j) + 1))); + + m_progress->setValue(0); + m_progress->show(); + m_readerTimer.start(LOADER_TIMER_INTERVAL); + m_reader.load(filename); +} + +void EasyMainWindow::readStream(::std::stringstream& data) +{ + m_progress->setLabelText(tr("Reading from stream...")); + + m_progress->setValue(0); + m_progress->show(); + m_readerTimer.start(LOADER_TIMER_INTERVAL); + m_reader.load(data); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onSaveFileClicked(bool) +{ + if (m_serializedBlocks.empty()) + return; + + QString lastFile = m_lastFiles.empty() ? QString() : m_lastFiles.front(); + + const auto i = lastFile.lastIndexOf(QChar('/')); + const auto j = lastFile.lastIndexOf(QChar('\\')); + auto k = ::std::max(i, j); + + QString dir; + if (k > 0) + dir = lastFile.mid(0, ++k); + + if (m_bNetworkFileRegime) + { + // Current file is network cache file, use current system time as output file name + + if (!dir.isEmpty()) + dir += QDateTime::currentDateTime().toString("/yyyy-MM-dd_HH-mm-ss.prof"); + else + dir = QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm-ss.prof"); + } + else if (m_bOpenedCacheFile) + { + // Opened old network cache file, use it's last modification time as output file name + + QFileInfo fileInfo(lastFile); + if (!fileInfo.exists()) + { + // Can not open the file! + + QMessageBox::warning(this, "Warning", "Can not open source file.\nSaving incomplete.", QMessageBox::Close); + + m_lastFiles.pop_front(); + auto action = m_loadActionMenu->actions().front(); + m_loadActionMenu->removeAction(action); + delete action; + + return; + } + + if (!dir.isEmpty()) + dir += fileInfo.lastModified().toString("/yyyy-MM-dd_HH-mm-ss.prof"); + else + dir = fileInfo.lastModified().toString("yyyy-MM-dd_HH-mm-ss.prof"); + } + else + { + dir = lastFile; + } + + auto filename = QFileDialog::getSaveFileName(this, "Save EasyProfiler File", dir, "EasyProfiler File (*.prof);;All Files (*.*)"); + if (!filename.isEmpty()) + { + // Check if the same file has been selected + { + QFileInfo fileInfo1(m_bNetworkFileRegime ? QString(NETWORK_CACHE_FILE) : lastFile), fileInfo2(filename); + if (fileInfo1.exists() && fileInfo2.exists() && fileInfo1 == fileInfo2) + { + // Selected the same file - do nothing + return; + } + } + + bool inOk = false, outOk = false; + int8_t retry1 = -1; + while (++retry1 < 4) + { + ::std::ifstream inFile(m_bNetworkFileRegime ? NETWORK_CACHE_FILE : lastFile.toStdString().c_str(), ::std::fstream::binary); + if (!inFile.is_open()) + { + ::std::this_thread::sleep_for(::std::chrono::milliseconds(500)); + continue; + } + + inOk = true; + + int8_t retry2 = -1; + while (++retry2 < 4) + { + ::std::ofstream outFile(filename.toStdString(), ::std::fstream::binary); + if (!outFile.is_open()) + { + ::std::this_thread::sleep_for(::std::chrono::milliseconds(500)); + continue; + } + + outFile << inFile.rdbuf(); + outOk = true; + break; + } + + break; + } + + if (outOk) + { + if (m_bNetworkFileRegime) + { + // Remove temporary network cahche file + QFile::remove(QString(NETWORK_CACHE_FILE)); + } + else if (m_bOpenedCacheFile) + { + // Remove old temporary network cahche file + + QFile::remove(lastFile.toStdString().c_str()); + + m_lastFiles.pop_front(); + auto action = m_loadActionMenu->actions().front(); + m_loadActionMenu->removeAction(action); + delete action; + } + + addFileToList(filename); + + m_bNetworkFileRegime = false; + } + else if (inOk) + { + QMessageBox::warning(this, "Warning", "Can not open destination file.\nSaving incomplete.", QMessageBox::Close); + } + else + { + if (m_bNetworkFileRegime) + QMessageBox::warning(this, "Warning", "Can not open network cache file.\nSaving incomplete.", QMessageBox::Close); + else + QMessageBox::warning(this, "Warning", "Can not open source file.\nSaving incomplete.", QMessageBox::Close); + } + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::clear() +{ + static_cast(m_treeWidget->widget())->clear(true); + static_cast(m_graphicsView->widget())->clear(); + +#if EASY_GUI_USE_DESCRIPTORS_DOCK_WINDOW != 0 + static_cast(m_descTreeWidget->widget())->clear(); +#endif + if (m_dialogDescTree != nullptr) + m_dialogDescTree->clear(); + + EASY_GLOBALS.selected_thread = 0; + ::profiler_gui::set_max(EASY_GLOBALS.selected_block); + ::profiler_gui::set_max(EASY_GLOBALS.selected_block_id); + EASY_GLOBALS.profiler_blocks.clear(); + EASY_GLOBALS.descriptors.clear(); + EASY_GLOBALS.gui_blocks.clear(); + + m_serializedBlocks.clear(); + m_serializedDescriptors.clear(); + + m_saveAction->setEnabled(false); + m_deleteAction->setEnabled(false); + + if (m_bNetworkFileRegime) + QFile::remove(QString(NETWORK_CACHE_FILE)); + + m_bNetworkFileRegime = false; + m_bOpenedCacheFile = false; + + setWindowTitle(EASY_DEFAULT_WINDOW_TITLE); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::refreshDiagram() +{ + static_cast(m_graphicsView->widget())->view()->scene()->update(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onDeleteClicked(bool) +{ + int button = QMessageBox::Yes; + if (m_bNetworkFileRegime) + button = QMessageBox::question(this, "Clear all profiled data", "All profiled data and network cache file\nare going to be deleted!\nContinue?", QMessageBox::Yes, QMessageBox::No); + + if (button == QMessageBox::Yes) + clear(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onExitClicked(bool) +{ + close(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onEncodingChanged(bool) +{ + auto _sender = qobject_cast(sender()); + auto name = _sender->text(); + QTextCodec *codec = QTextCodec::codecForName(name.toStdString().c_str()); + QTextCodec::setCodecForLocale(codec); +} + +void EasyMainWindow::onChronoTextPosChanged(bool) +{ + auto _sender = qobject_cast(sender()); + EASY_GLOBALS.chrono_text_position = static_cast<::profiler_gui::ChronometerTextPosition>(_sender->data().toInt()); + refreshDiagram(); +} + +void EasyMainWindow::onUnitsChanged(bool) +{ + auto _sender = qobject_cast(sender()); + EASY_GLOBALS.time_units = static_cast<::profiler_gui::TimeUnits>(_sender->data().toInt()); +} + +void EasyMainWindow::onEnableDisableStatistics(bool _checked) +{ + EASY_GLOBALS.enable_statistics = _checked; + + auto action = qobject_cast(sender()); + if (action != nullptr) + { + auto f = action->font(); + f.setBold(_checked); + action->setFont(f); + + if (_checked) + { + action->setText("Statistics enabled"); + SET_ICON(action, ":/Stats"); + } + else + { + action->setText("Statistics disabled"); + SET_ICON(action, ":/Stats-off"); + } + } +} + +void EasyMainWindow::onCollapseItemsAfterCloseChanged(bool _checked) +{ + EASY_GLOBALS.collapse_items_on_tree_close = _checked; +} + +void EasyMainWindow::onAllItemsExpandedByDefaultChange(bool _checked) +{ + EASY_GLOBALS.all_items_expanded_by_default = _checked; +} + +void EasyMainWindow::onBindExpandStatusChange(bool _checked) +{ + EASY_GLOBALS.bind_scene_and_tree_expand_status = _checked; +} + +void EasyMainWindow::onHierarchyFlagChange(bool _checked) +{ + EASY_GLOBALS.only_current_thread_hierarchy = _checked; + emit EASY_GLOBALS.events.hierarchyFlagChanged(_checked); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onExpandAllClicked(bool) +{ + for (auto& block : EASY_GLOBALS.gui_blocks) + block.expanded = true; + + emit EASY_GLOBALS.events.itemsExpandStateChanged(); + + auto tree = static_cast(m_treeWidget->widget())->tree(); + const QSignalBlocker b(tree); + tree->expandAll(); +} + +void EasyMainWindow::onCollapseAllClicked(bool) +{ + for (auto& block : EASY_GLOBALS.gui_blocks) + block.expanded = false; + + emit EASY_GLOBALS.events.itemsExpandStateChanged(); + + auto tree = static_cast(m_treeWidget->widget())->tree(); + const QSignalBlocker b(tree); + tree->collapseAll(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onSpacingChange(int _value) +{ + EASY_GLOBALS.blocks_spacing = _value; + refreshDiagram(); +} + +void EasyMainWindow::onMinSizeChange(int _value) +{ + EASY_GLOBALS.blocks_size_min = _value; + refreshDiagram(); +} + +void EasyMainWindow::onNarrowSizeChange(int _value) +{ + EASY_GLOBALS.blocks_narrow_size = _value; + refreshDiagram(); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onFpsIntervalChange(int _value) +{ + EASY_GLOBALS.fps_timer_interval = _value; + + if (m_fpsRequestTimer.isActive()) + m_fpsRequestTimer.stop(); + + if (EASY_GLOBALS.connected) + m_fpsRequestTimer.start(_value); +} + +void EasyMainWindow::onFpsHistoryChange(int _value) +{ + EASY_GLOBALS.max_fps_history = _value; +} + +void EasyMainWindow::onFpsMonitorLineWidthChange(int _value) +{ + EASY_GLOBALS.fps_widget_line_width = _value; +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onEditBlocksClicked(bool) +{ + if (m_descTreeDialog != nullptr) + { + m_descTreeDialog->raise(); + return; + } + + m_descTreeDialog = new QDialog(); + m_descTreeDialog->setAttribute(Qt::WA_DeleteOnClose, true); + m_descTreeDialog->setWindowTitle(EASY_DEFAULT_WINDOW_TITLE); + m_descTreeDialog->resize(800, 600); + connect(m_descTreeDialog, &QDialog::finished, this, &This::onDescTreeDialogClose); + + auto l = new QVBoxLayout(m_descTreeDialog); + m_dialogDescTree = new EasyDescWidget(m_descTreeDialog); + l->addWidget(m_dialogDescTree); + m_descTreeDialog->setLayout(l); + + m_dialogDescTree->build(); + m_descTreeDialog->show(); +} + +void EasyMainWindow::onDescTreeDialogClose(int) +{ + disconnect(m_descTreeDialog, &QDialog::finished, this, &This::onDescTreeDialogClose); + m_dialogDescTree = nullptr; + m_descTreeDialog = nullptr; +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::closeEvent(QCloseEvent* close_event) +{ + if (m_bNetworkFileRegime) + { + // Warn user about unsaved network information and suggest to save + if (QMessageBox::Yes == QMessageBox::question(this, "Unsaved session", "You unsaved data!\nSave before exit?", QMessageBox::Yes, QMessageBox::No)) + { + onSaveFileClicked(true); + } + } + + saveSettingsAndGeometry(); + + if (m_descTreeDialog != nullptr) + { + m_descTreeDialog->reject(); + m_descTreeDialog = nullptr; + m_dialogDescTree = nullptr; + } + + Parent::closeEvent(close_event); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::loadSettings() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("main"); + + auto last_files = settings.value("last_files"); + if (!last_files.isNull()) + m_lastFiles = last_files.toStringList(); + + auto last_addr = settings.value("ip_address"); + if (!last_addr.isNull()) + m_lastAddress = last_addr.toString(); + + auto last_port = settings.value("port"); + if (!last_port.isNull()) + m_lastPort = (uint16_t)last_port.toUInt(); + + + auto val = settings.value("chrono_text_position"); + if (!val.isNull()) + EASY_GLOBALS.chrono_text_position = static_cast<::profiler_gui::ChronometerTextPosition>(val.toInt()); + + val = settings.value("time_units"); + if (!val.isNull()) + EASY_GLOBALS.time_units = static_cast<::profiler_gui::TimeUnits>(val.toInt()); + + + val = settings.value("frame_time"); + if (!val.isNull()) + EASY_GLOBALS.frame_time = val.toFloat(); + + val = settings.value("blocks_spacing"); + if (!val.isNull()) + EASY_GLOBALS.blocks_spacing = val.toInt(); + + val = settings.value("blocks_size_min"); + if (!val.isNull()) + EASY_GLOBALS.blocks_size_min = val.toInt(); + + val = settings.value("blocks_narrow_size"); + if (!val.isNull()) + EASY_GLOBALS.blocks_narrow_size = val.toInt(); + + + auto flag = settings.value("draw_graphics_items_borders"); + if (!flag.isNull()) + EASY_GLOBALS.draw_graphics_items_borders = flag.toBool(); + + flag = settings.value("hide_narrow_children"); + if (!flag.isNull()) + EASY_GLOBALS.hide_narrow_children = flag.toBool(); + + flag = settings.value("hide_minsize_blocks"); + if (!flag.isNull()) + EASY_GLOBALS.hide_minsize_blocks = flag.toBool(); + + flag = settings.value("collapse_items_on_tree_close"); + if (!flag.isNull()) + EASY_GLOBALS.collapse_items_on_tree_close = flag.toBool(); + + flag = settings.value("all_items_expanded_by_default"); + if (!flag.isNull()) + EASY_GLOBALS.all_items_expanded_by_default = flag.toBool(); + + flag = settings.value("only_current_thread_hierarchy"); + if (!flag.isNull()) + EASY_GLOBALS.only_current_thread_hierarchy = flag.toBool(); + + flag = settings.value("enable_zero_length"); + if (!flag.isNull()) + EASY_GLOBALS.enable_zero_length = flag.toBool(); + + flag = settings.value("add_zero_blocks_to_hierarchy"); + if (!flag.isNull()) + EASY_GLOBALS.add_zero_blocks_to_hierarchy = flag.toBool(); + + + flag = settings.value("highlight_blocks_with_same_id"); + if (!flag.isNull()) + EASY_GLOBALS.highlight_blocks_with_same_id = flag.toBool(); + + flag = settings.value("bind_scene_and_tree_expand_status"); + if (!flag.isNull()) + EASY_GLOBALS.bind_scene_and_tree_expand_status = flag.toBool(); + + flag = settings.value("selecting_block_changes_thread"); + if (!flag.isNull()) + EASY_GLOBALS.selecting_block_changes_thread = flag.toBool(); + + flag = settings.value("enable_event_indicators"); + if (!flag.isNull()) + EASY_GLOBALS.enable_event_markers = flag.toBool(); + + flag = settings.value("auto_adjust_histogram_height"); + if (!flag.isNull()) + EASY_GLOBALS.auto_adjust_histogram_height = flag.toBool(); + + flag = settings.value("display_only_frames_on_histogram"); + if (!flag.isNull()) + EASY_GLOBALS.display_only_frames_on_histogram = flag.toBool(); + + flag = settings.value("use_decorated_thread_name"); + if (!flag.isNull()) + EASY_GLOBALS.use_decorated_thread_name = flag.toBool(); + + flag = settings.value("hex_thread_id"); + if (!flag.isNull()) + EASY_GLOBALS.hex_thread_id = flag.toBool(); + + flag = settings.value("fps_timer_interval"); + if (!flag.isNull()) + EASY_GLOBALS.fps_timer_interval = flag.toInt(); + + flag = settings.value("max_fps_history"); + if (!flag.isNull()) + EASY_GLOBALS.max_fps_history = flag.toInt(); + + flag = settings.value("fps_widget_line_width"); + if (!flag.isNull()) + EASY_GLOBALS.fps_widget_line_width = flag.toInt(); + + flag = settings.value("enable_statistics"); + if (!flag.isNull()) + EASY_GLOBALS.enable_statistics = flag.toBool(); + + QString encoding = settings.value("encoding", "UTF-8").toString(); + auto default_codec_mib = QTextCodec::codecForName(encoding.toStdString().c_str())->mibEnum(); + auto default_codec = QTextCodec::codecForMib(default_codec_mib); + QTextCodec::setCodecForLocale(default_codec); + + settings.endGroup(); +} + +void EasyMainWindow::loadGeometry() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("main"); + + auto geometry = settings.value("geometry").toByteArray(); + if (!geometry.isEmpty()) + restoreGeometry(geometry); + + auto state = settings.value("windowState").toByteArray(); + if (!state.isEmpty()) + restoreState(state); + + settings.endGroup(); +} + +void EasyMainWindow::saveSettingsAndGeometry() +{ + QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); + settings.beginGroup("main"); + + settings.setValue("geometry", this->saveGeometry()); + settings.setValue("windowState", this->saveState()); + settings.setValue("last_files", m_lastFiles); + settings.setValue("ip_address", m_lastAddress); + settings.setValue("port", (quint32)m_lastPort); + settings.setValue("chrono_text_position", static_cast(EASY_GLOBALS.chrono_text_position)); + settings.setValue("time_units", static_cast(EASY_GLOBALS.time_units)); + settings.setValue("frame_time", EASY_GLOBALS.frame_time); + settings.setValue("blocks_spacing", EASY_GLOBALS.blocks_spacing); + settings.setValue("blocks_size_min", EASY_GLOBALS.blocks_size_min); + settings.setValue("blocks_narrow_size", EASY_GLOBALS.blocks_narrow_size); + settings.setValue("draw_graphics_items_borders", EASY_GLOBALS.draw_graphics_items_borders); + settings.setValue("hide_narrow_children", EASY_GLOBALS.hide_narrow_children); + settings.setValue("hide_minsize_blocks", EASY_GLOBALS.hide_minsize_blocks); + settings.setValue("collapse_items_on_tree_close", EASY_GLOBALS.collapse_items_on_tree_close); + settings.setValue("all_items_expanded_by_default", EASY_GLOBALS.all_items_expanded_by_default); + settings.setValue("only_current_thread_hierarchy", EASY_GLOBALS.only_current_thread_hierarchy); + settings.setValue("enable_zero_length", EASY_GLOBALS.enable_zero_length); + settings.setValue("add_zero_blocks_to_hierarchy", EASY_GLOBALS.add_zero_blocks_to_hierarchy); + settings.setValue("highlight_blocks_with_same_id", EASY_GLOBALS.highlight_blocks_with_same_id); + settings.setValue("bind_scene_and_tree_expand_status", EASY_GLOBALS.bind_scene_and_tree_expand_status); + settings.setValue("selecting_block_changes_thread", EASY_GLOBALS.selecting_block_changes_thread); + settings.setValue("enable_event_indicators", EASY_GLOBALS.enable_event_markers); + settings.setValue("auto_adjust_histogram_height", EASY_GLOBALS.auto_adjust_histogram_height); + settings.setValue("display_only_frames_on_histogram", EASY_GLOBALS.display_only_frames_on_histogram); + settings.setValue("use_decorated_thread_name", EASY_GLOBALS.use_decorated_thread_name); + settings.setValue("hex_thread_id", EASY_GLOBALS.hex_thread_id); + settings.setValue("enable_statistics", EASY_GLOBALS.enable_statistics); + settings.setValue("fps_timer_interval", EASY_GLOBALS.fps_timer_interval); + settings.setValue("max_fps_history", EASY_GLOBALS.max_fps_history); + settings.setValue("fps_widget_line_width", EASY_GLOBALS.fps_widget_line_width); + settings.setValue("encoding", QTextCodec::codecForLocale()->name()); + + settings.endGroup(); +} + +void EasyMainWindow::setDisconnected(bool _showMessage) +{ + if (m_fpsRequestTimer.isActive()) + m_fpsRequestTimer.stop(); + + if (_showMessage) + QMessageBox::warning(this, "Warning", "Connection has lost", QMessageBox::Close); + + EASY_GLOBALS.connected = false; + m_captureAction->setEnabled(false); + SET_ICON(m_connectAction, ":/Connection"); + m_connectAction->setText(tr("Connect")); + + m_eventTracingEnableAction->setEnabled(false); + m_eventTracingPriorityAction->setEnabled(false); + + m_addressEdit->setEnabled(true); + m_portEdit->setEnabled(true); + + emit EASY_GLOBALS.events.connectionChanged(false); + +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onFrameTimeRequestTimeout() +{ + if (EASY_GLOBALS.fps_enabled && EASY_GLOBALS.connected && (m_listener.regime() == LISTENER_IDLE || m_listener.regime() == LISTENER_CAPTURE)) + { + if (m_listener.requestFrameTime()) + { + QTimer::singleShot(100, this, &This::checkFrameTimeReady); + } + else if (!m_listener.connected()) + { + setDisconnected(); + } + } +} + +void EasyMainWindow::checkFrameTimeReady() +{ + if (EASY_GLOBALS.fps_enabled && EASY_GLOBALS.connected && (m_listener.regime() == LISTENER_IDLE || m_listener.regime() == LISTENER_CAPTURE)) + { + uint32_t maxTime = 0, avgTime = 0; + if (m_listener.frameTime(maxTime, avgTime)) + { + static_cast(m_fpsViewer->widget())->addPoint(maxTime, avgTime); + } + else if (m_fpsRequestTimer.isActive()) + { + QTimer::singleShot(100, this, &This::checkFrameTimeReady); + } + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onListenerTimerTimeout() +{ + if (!m_listener.connected()) + { + if (m_listener.regime() == LISTENER_CAPTURE_RECEIVE) + m_listener.finalizeCapture(); + m_listenerDialog->reject(); + } + else if (m_listener.regime() == LISTENER_CAPTURE_RECEIVE) + { + if (m_listener.captured()) + { + if (m_listenerTimer.isActive()) + m_listenerTimer.stop(); + + m_listener.finalizeCapture(); + + m_listenerDialog->accept(); + m_listenerDialog = nullptr; + + if (m_listener.size() != 0) + { + readStream(m_listener.data()); + m_listener.clearData(); + } + } + } +} + +void EasyMainWindow::onListenerDialogClose(int _result) +{ + if (m_listener.regime() != LISTENER_CAPTURE_RECEIVE || !m_listener.connected()) + { + if (m_listenerTimer.isActive()) + m_listenerTimer.stop(); + } + + disconnect(m_listenerDialog, &QDialog::finished, this, &This::onListenerDialogClose); + m_listenerDialog = nullptr; + + switch (m_listener.regime()) + { + case LISTENER_CAPTURE: + { + m_listenerDialog = new QMessageBox(QMessageBox::Information, "Receiving data...", "This process may take some time.", QMessageBox::Cancel, this); + m_listenerDialog->setAttribute(Qt::WA_DeleteOnClose, true); + m_listenerDialog->show(); + + m_listener.stopCapture(); + + if (m_listener.regime() != LISTENER_CAPTURE_RECEIVE) + { + m_listenerDialog->reject(); + m_listenerDialog = nullptr; + } + else + { + connect(m_listenerDialog, &QDialog::finished, this, &This::onListenerDialogClose); + m_listenerTimer.start(250); + } + + break; + } + + case LISTENER_CAPTURE_RECEIVE: + { + if (!m_listener.captured()) + { + if (_result == QDialog::Accepted) + { + m_listenerDialog = new QMessageBox(QMessageBox::Information, "Receiving data...", "This process may take some time.", QMessageBox::Cancel, this); + connect(m_listenerDialog, &QDialog::finished, this, &This::onListenerDialogClose); + m_listenerDialog->setAttribute(Qt::WA_DeleteOnClose, true); + m_listenerDialog->show(); + } + else + { + m_listener.finalizeCapture(); + m_listener.clearData(); + + if (m_listener.connected()) + { + // make reconnect to clear socket buffers + std::string address = m_listener.address(); + + profiler::net::EasyProfilerStatus reply(false, false, false); + if (m_listener.connect(address.c_str(), m_listener.port(), reply)) + { + disconnect(m_eventTracingEnableAction, &QAction::triggered, this, &This::onEventTracingEnableChange); + disconnect(m_eventTracingPriorityAction, &QAction::triggered, this, &This::onEventTracingPriorityChange); + + m_eventTracingEnableAction->setChecked(reply.isEventTracingEnabled); + m_eventTracingPriorityAction->setChecked(reply.isLowPriorityEventTracing); + + connect(m_eventTracingEnableAction, &QAction::triggered, this, &This::onEventTracingEnableChange); + connect(m_eventTracingPriorityAction, &QAction::triggered, this, &This::onEventTracingPriorityChange); + + if (reply.isProfilerEnabled) + { + // Connected application is already profiling. + // Show capture dialog immediately + onCaptureClicked(true); + } + } + } + } + + break; + } + + if (m_listenerTimer.isActive()) + m_listenerTimer.stop(); + + m_listener.finalizeCapture(); + + if (m_listener.size() != 0) + { + readStream(m_listener.data()); + m_listener.clearData(); + } + + break; + } + + case LISTENER_DESCRIBE: + { + break; + } + + default: + return; + } + + if (!m_listener.connected()) + { + setDisconnected(); + } +} + + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onFileReaderTimeout() +{ + if (m_reader.done()) + { + auto nblocks = m_reader.size(); + if (nblocks != 0) + { + static_cast(m_treeWidget->widget())->clear(true); + + ::profiler::SerializedData serialized_blocks; + ::profiler::SerializedData serialized_descriptors; + ::profiler::descriptors_list_t descriptors; + ::profiler::blocks_t blocks; + ::profiler::thread_blocks_tree_t threads_map; + QString filename; + uint32_t descriptorsNumberInFile = 0; + uint32_t version = 0; + m_reader.get(serialized_blocks, serialized_descriptors, descriptors, blocks, threads_map, descriptorsNumberInFile, version, filename); + + if (threads_map.size() > 0xff) + { + if (m_reader.isFile()) + qWarning() << "Warning: file " << filename << " contains " << threads_map.size() << " threads!"; + else + qWarning() << "Warning: input stream contains " << threads_map.size() << " threads!"; + qWarning() << "Warning: Currently, maximum number of displayed threads is 255! Some threads will not be displayed."; + } + + m_bNetworkFileRegime = !m_reader.isFile(); + if (!m_bNetworkFileRegime) + { + auto index = m_lastFiles.indexOf(filename, 0); + if (index == -1) + { + // This file is totally new. Add it to the list. + addFileToList(filename); + } + else + { + if (index != 0) + { + // This file has been already loaded. Move it to the front. + m_lastFiles.move(index, 0); + auto fileActions = m_loadActionMenu->actions(); + auto action = fileActions.at(index); + m_loadActionMenu->removeAction(action); + m_loadActionMenu->insertAction(fileActions.front(), action); + } + + m_bOpenedCacheFile = filename.contains(NETWORK_CACHE_FILE); + + if (m_bOpenedCacheFile) + setWindowTitle(QString(EASY_DEFAULT_WINDOW_TITLE " - [%1] - UNSAVED network cache file").arg(filename)); + else + setWindowTitle(QString(EASY_DEFAULT_WINDOW_TITLE " - [%1]").arg(filename)); + } + } + else + { + m_bOpenedCacheFile = false; + setWindowTitle(EASY_DEFAULT_WINDOW_TITLE " - UNSAVED network cache"); + } + + m_serializedBlocks = ::std::move(serialized_blocks); + m_serializedDescriptors = ::std::move(serialized_descriptors); + m_descriptorsNumberInFile = descriptorsNumberInFile; + EASY_GLOBALS.selected_thread = 0; + EASY_GLOBALS.version = version; + ::profiler_gui::set_max(EASY_GLOBALS.selected_block); + ::profiler_gui::set_max(EASY_GLOBALS.selected_block_id); + EASY_GLOBALS.profiler_blocks.swap(threads_map); + EASY_GLOBALS.descriptors.swap(descriptors); + + EASY_GLOBALS.gui_blocks.clear(); + EASY_GLOBALS.gui_blocks.resize(nblocks); + memset(EASY_GLOBALS.gui_blocks.data(), 0, sizeof(::profiler_gui::EasyBlock) * nblocks); + for (decltype(nblocks) i = 0; i < nblocks; ++i) { + auto& guiblock = EASY_GLOBALS.gui_blocks[i]; + guiblock.tree = ::std::move(blocks[i]); +#ifdef EASY_TREE_WIDGET__USE_VECTOR + ::profiler_gui::set_max(guiblock.tree_item); +#endif + } + + static_cast(m_graphicsView->widget())->view()->setTree(EASY_GLOBALS.profiler_blocks); + +#if EASY_GUI_USE_DESCRIPTORS_DOCK_WINDOW != 0 + static_cast(m_descTreeWidget->widget())->build(); +#endif + if (m_dialogDescTree != nullptr) + m_dialogDescTree->build(); + + m_saveAction->setEnabled(true); + m_deleteAction->setEnabled(true); + } + else + { + QMessageBox::warning(this, "Warning", QString("Can not read profiled blocks.\n\nReason:\n%1").arg(m_reader.getError()), QMessageBox::Close); + + if (m_reader.isFile()) + { + auto index = m_lastFiles.indexOf(m_reader.filename(), 0); + if (index >= 0) + { + // Remove unexisting file from list + m_lastFiles.removeAt(index); + auto action = m_loadActionMenu->actions().at(index); + m_loadActionMenu->removeAction(action); + delete action; + } + } + } + + m_reader.interrupt(); + + m_readerTimer.stop(); + m_progress->setValue(100); + //m_progress->hide(); + + if (EASY_GLOBALS.all_items_expanded_by_default) + { + onExpandAllClicked(true); + } + } + else + { + m_progress->setValue(m_reader.progress()); + } +} + +void EasyMainWindow::onFileReaderCancel() +{ + m_readerTimer.stop(); + m_reader.interrupt(); + m_progress->setValue(100); + //m_progress->hide(); +} + +////////////////////////////////////////////////////////////////////////// + +EasyFileReader::EasyFileReader() +{ + +} + +EasyFileReader::~EasyFileReader() +{ + interrupt(); +} + +const bool EasyFileReader::isFile() const +{ + return m_isFile; +} + +bool EasyFileReader::done() const +{ + return m_bDone.load(::std::memory_order_acquire); +} + +int EasyFileReader::progress() const +{ + return m_progress.load(::std::memory_order_acquire); +} + +unsigned int EasyFileReader::size() const +{ + return m_size.load(::std::memory_order_acquire); +} + +const QString& EasyFileReader::filename() const +{ + return m_filename; +} + +void EasyFileReader::load(const QString& _filename) +{ + interrupt(); + + m_isFile = true; + m_filename = _filename; + m_thread = ::std::thread([this](bool _enableStatistics) { + m_size.store(fillTreesFromFile(m_progress, m_filename.toStdString().c_str(), m_serializedBlocks, m_serializedDescriptors, + m_descriptors, m_blocks, m_blocksTree, m_descriptorsNumberInFile, m_version, _enableStatistics, m_errorMessage), ::std::memory_order_release); + m_progress.store(100, ::std::memory_order_release); + m_bDone.store(true, ::std::memory_order_release); + }, EASY_GLOBALS.enable_statistics); +} + +void EasyFileReader::load(::std::stringstream& _stream) +{ + interrupt(); + + m_isFile = false; + m_filename.clear(); + +#if defined(__GNUC__) && __GNUC__ < 5 && !defined(__llvm__) + // gcc 4 has a known bug which has been solved in gcc 5: + // std::stringstream has no swap() method :( + // have to copy all contents... Use gcc 5 or higher! +#pragma message "Warning: in gcc 4 and lower std::stringstream has no swap()! Memory consumption may increase! Better use gcc 5 or higher instead." + m_stream.str(_stream.str()); +#else + m_stream.swap(_stream); +#endif + + m_thread = ::std::thread([this](bool _enableStatistics) { + ::std::ofstream cache_file(NETWORK_CACHE_FILE, ::std::fstream::binary); + if (cache_file.is_open()) { + cache_file << m_stream.str(); + cache_file.close(); + } + m_size.store(fillTreesFromStream(m_progress, m_stream, m_serializedBlocks, m_serializedDescriptors, m_descriptors, + m_blocks, m_blocksTree, m_descriptorsNumberInFile, m_version, _enableStatistics, m_errorMessage), ::std::memory_order_release); + m_progress.store(100, ::std::memory_order_release); + m_bDone.store(true, ::std::memory_order_release); + }, EASY_GLOBALS.enable_statistics); +} + +void EasyFileReader::interrupt() +{ + m_progress.store(-100, ::std::memory_order_release); + if (m_thread.joinable()) + m_thread.join(); + + m_bDone.store(false, ::std::memory_order_release); + m_progress.store(0, ::std::memory_order_release); + m_size.store(0, ::std::memory_order_release); + m_serializedBlocks.clear(); + m_serializedDescriptors.clear(); + m_descriptors.clear(); + m_blocks.clear(); + m_blocksTree.clear(); + m_descriptorsNumberInFile = 0; + m_version = 0; + + clear_stream(m_stream); + clear_stream(m_errorMessage); +} + +void EasyFileReader::get(::profiler::SerializedData& _serializedBlocks, ::profiler::SerializedData& _serializedDescriptors, + ::profiler::descriptors_list_t& _descriptors, ::profiler::blocks_t& _blocks, + ::profiler::thread_blocks_tree_t& _tree, uint32_t& _descriptorsNumberInFile, uint32_t& _version, QString& _filename) +{ + if (done()) + { + m_serializedBlocks.swap(_serializedBlocks); + m_serializedDescriptors.swap(_serializedDescriptors); + ::profiler::descriptors_list_t(::std::move(m_descriptors)).swap(_descriptors); + m_blocks.swap(_blocks); + m_blocksTree.swap(_tree); + m_filename.swap(_filename); + _descriptorsNumberInFile = m_descriptorsNumberInFile; + _version = m_version; + } +} + +QString EasyFileReader::getError() +{ + return QString(m_errorMessage.str().c_str()); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onEventTracingPriorityChange(bool _checked) +{ + if (EASY_GLOBALS.connected) + m_listener.send(profiler::net::BoolMessage(profiler::net::MESSAGE_TYPE_EVENT_TRACING_PRIORITY, _checked)); +} + +void EasyMainWindow::onEventTracingEnableChange(bool _checked) +{ + if (EASY_GLOBALS.connected) + m_listener.send(profiler::net::BoolMessage(profiler::net::MESSAGE_TYPE_EVENT_TRACING_STATUS, _checked)); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onFrameTimeEditFinish() +{ + auto text = m_frameTimeEdit->text(); + if (text.contains(QChar(','))) + { + text.remove(QChar('.')).replace(QChar(','), QChar('.')); + m_frameTimeEdit->setText(text); + } + + EASY_GLOBALS.frame_time = text.toFloat() * 1e3f; + + disconnect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::expectedFrameTimeChanged, this, &This::onFrameTimeChanged); + emit EASY_GLOBALS.events.expectedFrameTimeChanged(); + connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::expectedFrameTimeChanged, this, &This::onFrameTimeChanged); +} + +void EasyMainWindow::onFrameTimeChanged() +{ + m_frameTimeEdit->setText(QString::number(EASY_GLOBALS.frame_time * 1e-3)); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onConnectClicked(bool) +{ + if (EASY_GLOBALS.connected) + { + // Disconnect if already connected + m_listener.disconnect(); + setDisconnected(false); + return; + } + + QString address = m_addressEdit->text(); + const decltype(m_lastPort) port = m_portEdit->text().toUShort(); + const bool isSameAddress = (EASY_GLOBALS.connected && m_listener.port() == port && address.toStdString() == m_listener.address()); + + profiler::net::EasyProfilerStatus reply(false, false, false); + if (!m_listener.connect(address.toStdString().c_str(), port, reply)) + { + /*if (EASY_GLOBALS.connected && !isSameAddress) + { + if (QMessageBox::warning(this, "Warning", QString("Cannot connect to %1\n\nRestore previous connection?").arg(address), + QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) + { + if (!m_listener.connect(m_lastAddress.toStdString().c_str(), m_lastPort, reply)) + { + QMessageBox::warning(this, "Warning", "Cannot restore previous connection", QMessageBox::Close); + setDisconnected(false); + m_lastAddress = ::std::move(address); + m_lastPort = port; + } + else + { + m_addressEdit->setText(m_lastAddress); + m_portEdit->setText(QString::number(m_lastPort)); + //QMessageBox::information(this, "Information", "Previous connection restored", QMessageBox::Close); + } + } + else + { + setDisconnected(false); + m_lastAddress = ::std::move(address); + m_lastPort = port; + } + } + else*/ + { + QMessageBox::warning(this, "Warning", QString("Cannot connect to %1").arg(address), QMessageBox::Close); + if (EASY_GLOBALS.connected) + setDisconnected(false); + + if (!isSameAddress) + { + m_lastAddress = ::std::move(address); + m_lastPort = port; + } + } + + return; + } + + m_lastAddress = ::std::move(address); + m_lastPort = port; + + qInfo() << "Connected successfully"; + EASY_GLOBALS.connected = true; + m_captureAction->setEnabled(true); + SET_ICON(m_connectAction, ":/Connection-on"); + m_connectAction->setText(tr("Disconnect")); + + if (m_fpsViewer->isVisible()) + static_cast(m_fpsViewer->widget())->clear(); + + if (!m_fpsRequestTimer.isActive()) + m_fpsRequestTimer.start(EASY_GLOBALS.fps_timer_interval); + + disconnect(m_eventTracingEnableAction, &QAction::triggered, this, &This::onEventTracingEnableChange); + disconnect(m_eventTracingPriorityAction, &QAction::triggered, this, &This::onEventTracingPriorityChange); + + m_eventTracingEnableAction->setEnabled(true); + m_eventTracingPriorityAction->setEnabled(true); + + m_eventTracingEnableAction->setChecked(reply.isEventTracingEnabled); + m_eventTracingPriorityAction->setChecked(reply.isLowPriorityEventTracing); + + connect(m_eventTracingEnableAction, &QAction::triggered, this, &This::onEventTracingEnableChange); + connect(m_eventTracingPriorityAction, &QAction::triggered, this, &This::onEventTracingPriorityChange); + + m_addressEdit->setEnabled(false); + m_portEdit->setEnabled(false); + + emit EASY_GLOBALS.events.connectionChanged(true); + + if (reply.isProfilerEnabled) + { + // Connected application is already profiling. + // Show capture dialog immediately + onCaptureClicked(true); + } +} + +void EasyMainWindow::onCaptureClicked(bool) +{ + if (!EASY_GLOBALS.connected) + { + QMessageBox::warning(this, "Warning", "No connection with profiling app", QMessageBox::Close); + return; + } + + if (m_listener.regime() != LISTENER_IDLE) + { + if (m_listener.regime() == LISTENER_CAPTURE || m_listener.regime() == LISTENER_CAPTURE_RECEIVE) + QMessageBox::warning(this, "Warning", "Already capturing frames.\nFinish old capturing session first.", QMessageBox::Close); + else + QMessageBox::warning(this, "Warning", "Capturing blocks description.\nFinish old capturing session first.", QMessageBox::Close); + return; + } + + if (!m_listener.startCapture()) + { + // Connection lost. Try to restore connection. + + profiler::net::EasyProfilerStatus reply(false, false, false); + if (!m_listener.connect(m_lastAddress.toStdString().c_str(), m_lastPort, reply)) + { + setDisconnected(); + return; + } + + if (!m_listener.startCapture()) + { + setDisconnected(); + return; + } + } + + m_listenerTimer.start(250); + + m_listenerDialog = new QMessageBox(QMessageBox::Information, "Capturing frames...", "Close this dialog to stop capturing.", QMessageBox::NoButton, this); + + auto button = new QToolButton(m_listenerDialog); + button->setAutoRaise(true); + button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + button->setIconSize(::profiler_gui::ICONS_SIZE); + button->setIcon(QIcon(":/Stop")); + button->setText("Stop"); + m_listenerDialog->addButton(button, QMessageBox::AcceptRole); + + m_listenerDialog->setAttribute(Qt::WA_DeleteOnClose, true); + connect(m_listenerDialog, &QDialog::finished, this, &This::onListenerDialogClose); + m_listenerDialog->show(); +} + +void EasyMainWindow::onGetBlockDescriptionsClicked(bool) +{ + if (!EASY_GLOBALS.connected) + { + QMessageBox::warning(this, "Warning", "No connection with profiling app", QMessageBox::Close); + return; + } + + if (m_listener.regime() != LISTENER_IDLE) + { + if (m_listener.regime() == LISTENER_DESCRIBE) + QMessageBox::warning(this, "Warning", "Already capturing blocks description.\nFinish old capturing session first.", QMessageBox::Close); + else + QMessageBox::warning(this, "Warning", "Capturing capturing frames.\nFinish old capturing session first.", QMessageBox::Close); + return; + } + + m_listenerDialog = new QMessageBox(QMessageBox::Information, "Waiting for blocks...", "This may take some time.", QMessageBox::NoButton, this); + m_listenerDialog->setAttribute(Qt::WA_DeleteOnClose, true); + m_listenerDialog->show(); + + m_listener.requestBlocksDescription(); + + m_listenerDialog->reject(); + m_listenerDialog = nullptr; + + if (m_listener.size() != 0) + { + // Read descriptions from stream + + decltype(EASY_GLOBALS.descriptors) descriptors; + decltype(m_serializedDescriptors) serializedDescriptors; + ::std::stringstream errorMessage; + if (readDescriptionsFromStream(m_listener.data(), serializedDescriptors, descriptors, errorMessage)) + { + // Merge old and new descriptions + + bool cancel = false; + const bool doFlush = m_descriptorsNumberInFile > descriptors.size(); + if (doFlush && !m_serializedBlocks.empty()) + { + auto button = QMessageBox::question(this, "Information", + QString("New blocks description number = %1\nis less than the old one = %2.\nTo avoid possible conflicts\nall profiled data will be deleted.\nContinue?") + .arg(descriptors.size()) + .arg(m_descriptorsNumberInFile), + QMessageBox::Yes, QMessageBox::No); + + if (button == QMessageBox::Yes) + clear(); // Clear all contents because new descriptors list conflicts with old one + else + cancel = true; + } + + if (!cancel) + { + if (!doFlush && m_descriptorsNumberInFile < EASY_GLOBALS.descriptors.size()) + { + // There are dynamically added descriptors, add them to the new list too + + auto newnumber = static_cast(descriptors.size()); + auto size = static_cast(EASY_GLOBALS.descriptors.size()); + auto diff = newnumber - size; + decltype(newnumber) failnumber = 0; + + descriptors.reserve(descriptors.size() + EASY_GLOBALS.descriptors.size() - m_descriptorsNumberInFile); + for (auto i = m_descriptorsNumberInFile; i < size; ++i) + { + auto id = EASY_GLOBALS.descriptors[i]->id(); + if (id < newnumber) + descriptors.push_back(descriptors[id]); + else + ++failnumber; + } + + if (failnumber != 0) + { + // There are some errors... + + // revert changes + descriptors.resize(newnumber); + + // clear all profiled data to avoid conflicts + auto button = QMessageBox::question(this, "Information", + "There are errors while merging block descriptions lists.\nTo avoid possible conflicts\nall profiled data will be deleted.\nContinue?", + QMessageBox::Yes, QMessageBox::No); + + if (button == QMessageBox::Yes) + clear(); // Clear all contents because new descriptors list conflicts with old one + else + cancel = true; + } + + if (!cancel && diff != 0) + { + for (auto& b : EASY_GLOBALS.gui_blocks) + { + if (b.tree.node->id() >= m_descriptorsNumberInFile) + b.tree.node->setId(b.tree.node->id() + diff); + } + + m_descriptorsNumberInFile = newnumber; + } + } + + if (!cancel) + { + EASY_GLOBALS.descriptors.swap(descriptors); + m_serializedDescriptors.swap(serializedDescriptors); + m_descriptorsNumberInFile = static_cast(EASY_GLOBALS.descriptors.size()); + + if (m_descTreeDialog != nullptr) + { +#if EASY_GUI_USE_DESCRIPTORS_DOCK_WINDOW != 0 + static_cast(m_descTreeWidget->widget())->build(); +#endif + m_dialogDescTree->build(); + m_descTreeDialog->raise(); + } + else + { + onEditBlocksClicked(true); + } + } + } + } + else + { + QMessageBox::warning(this, "Warning", QString("Can not read blocks description from stream.\n\nReason:\n%1").arg(errorMessage.str().c_str()), QMessageBox::Close); + } + + m_listener.clearData(); + } + + if (!m_listener.connected()) + { + setDisconnected(); + } +} + +////////////////////////////////////////////////////////////////////////// + +void EasyMainWindow::onBlockStatusChange(::profiler::block_id_t _id, ::profiler::EasyBlockStatus _status) +{ + if (EASY_GLOBALS.connected) + m_listener.send(profiler::net::BlockStatusMessage(_id, static_cast(_status))); +} + +////////////////////////////////////////////////////////////////////////// + +EasySocketListener::EasySocketListener() : m_receivedSize(0), m_port(0), m_regime(LISTENER_IDLE) +{ + m_bInterrupt = ATOMIC_VAR_INIT(false); + m_bConnected = ATOMIC_VAR_INIT(false); + m_bStopReceive = ATOMIC_VAR_INIT(false); + m_bFrameTimeReady = ATOMIC_VAR_INIT(false); + m_bCaptureReady = ATOMIC_VAR_INIT(false); + m_frameMax = ATOMIC_VAR_INIT(0); + m_frameAvg = ATOMIC_VAR_INIT(0); +} + +EasySocketListener::~EasySocketListener() +{ + m_bInterrupt.store(true, ::std::memory_order_release); + if (m_thread.joinable()) + m_thread.join(); +} + +bool EasySocketListener::connected() const +{ + return m_bConnected.load(::std::memory_order_acquire); +} + +bool EasySocketListener::captured() const +{ + return m_bCaptureReady.load(::std::memory_order_acquire); +} + +EasyListenerRegime EasySocketListener::regime() const +{ + return m_regime; +} + +uint64_t EasySocketListener::size() const +{ + return m_receivedSize; +} + +::std::stringstream& EasySocketListener::data() +{ + return m_receivedData; +} + +const ::std::string& EasySocketListener::address() const +{ + return m_address; +} + +uint16_t EasySocketListener::port() const +{ + return m_port; +} + +void EasySocketListener::clearData() +{ + clear_stream(m_receivedData); + m_receivedSize = 0; +} + +void EasySocketListener::disconnect() +{ + if (connected()) + { + m_bInterrupt.store(true, ::std::memory_order_release); + if (m_thread.joinable()) + m_thread.join(); + + m_bConnected.store(false, ::std::memory_order_release); + m_bInterrupt.store(false, ::std::memory_order_release); + m_bCaptureReady.store(false, ::std::memory_order_release); + m_bStopReceive.store(false, ::std::memory_order_release); + } + + m_address.clear(); + m_port = 0; + + m_easySocket.flush(); + m_easySocket.init(); +} + +bool EasySocketListener::connect(const char* _ipaddress, uint16_t _port, profiler::net::EasyProfilerStatus& _reply) +{ + if (connected()) + { + m_bInterrupt.store(true, ::std::memory_order_release); + if (m_thread.joinable()) + m_thread.join(); + + m_bConnected.store(false, ::std::memory_order_release); + m_bInterrupt.store(false, ::std::memory_order_release); + m_bCaptureReady.store(false, ::std::memory_order_release); + m_bStopReceive.store(false, ::std::memory_order_release); + } + + m_address.clear(); + m_port = 0; + + //m_easySocket.flush(); + //m_easySocket.init(); + int res = m_easySocket.setAddress(_ipaddress, _port); + res = m_easySocket.connect(); + + const bool isConnected = res == 0; + if (isConnected) + { + static const size_t buffer_size = sizeof(profiler::net::EasyProfilerStatus) << 1; + char buffer[buffer_size] = {}; + int bytes = 0; + + while (true) + { + bytes = m_easySocket.receive(buffer, buffer_size); + + if (bytes == -1) + { + if (m_easySocket.isDisconnected()) + return false; + bytes = 0; + continue; + } + + break; + } + + if (bytes == 0) + { + m_address = _ipaddress; + m_port = _port; + m_bConnected.store(isConnected, ::std::memory_order_release); + return isConnected; + } + + size_t seek = bytes; + while (seek < sizeof(profiler::net::EasyProfilerStatus)) + { + bytes = m_easySocket.receive(buffer + seek, buffer_size - seek); + + if (bytes == -1) + { + if (m_easySocket.isDisconnected()) + return false; + break; + } + + seek += bytes; + } + + auto message = reinterpret_cast(buffer); + if (message->isEasyNetMessage() && message->type == profiler::net::MESSAGE_TYPE_ACCEPTED_CONNECTION) + _reply = *message; + + m_address = _ipaddress; + m_port = _port; + } + + m_bConnected.store(isConnected, ::std::memory_order_release); + return isConnected; +} + +bool EasySocketListener::startCapture() +{ + //if (m_thread.joinable()) + //{ + // m_bInterrupt.store(true, ::std::memory_order_release); + // m_thread.join(); + // m_bInterrupt.store(false, ::std::memory_order_release); + //} + + clearData(); + + profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_START_CAPTURE); + m_easySocket.send(&request, sizeof(request)); + + if (m_easySocket.isDisconnected()) { + m_bConnected.store(false, ::std::memory_order_release); + return false; + } + + m_regime = LISTENER_CAPTURE; + m_bCaptureReady.store(false, ::std::memory_order_release); + //m_thread = ::std::thread(&EasySocketListener::listenCapture, this); + + return true; +} + +void EasySocketListener::stopCapture() +{ + //if (!m_thread.joinable() || m_regime != LISTENER_CAPTURE) + // return; + + if (m_regime != LISTENER_CAPTURE) + return; + + //m_bStopReceive.store(true, ::std::memory_order_release); + profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE); + m_easySocket.send(&request, sizeof(request)); + + //m_thread.join(); + + if (m_easySocket.isDisconnected()) { + m_bConnected.store(false, ::std::memory_order_release); + m_bStopReceive.store(false, ::std::memory_order_release); + m_regime = LISTENER_IDLE; + m_bCaptureReady.store(true, ::std::memory_order_release); + return; + } + + m_regime = LISTENER_CAPTURE_RECEIVE; + if (m_thread.joinable()) + { + m_bInterrupt.store(true, ::std::memory_order_release); + m_thread.join(); + m_bInterrupt.store(false, ::std::memory_order_release); + } + + m_thread = ::std::thread(&EasySocketListener::listenCapture, this); + + //m_regime = LISTENER_IDLE; + //m_bStopReceive.store(false, ::std::memory_order_release); +} + +void EasySocketListener::finalizeCapture() +{ + if (m_thread.joinable()) + { + m_bInterrupt.store(true, ::std::memory_order_release); + m_thread.join(); + m_bInterrupt.store(false, ::std::memory_order_release); + } + + m_regime = LISTENER_IDLE; + m_bCaptureReady.store(false, ::std::memory_order_release); + m_bStopReceive.store(false, ::std::memory_order_release); +} + +void EasySocketListener::requestBlocksDescription() +{ + if (m_thread.joinable()) + { + m_bInterrupt.store(true, ::std::memory_order_release); + m_thread.join(); + m_bInterrupt.store(false, ::std::memory_order_release); + } + + clearData(); + + profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_BLOCKS_DESCRIPTION); + m_easySocket.send(&request, sizeof(request)); + + if(m_easySocket.isDisconnected() ){ + m_bConnected.store(false, ::std::memory_order_release); + } + + m_regime = LISTENER_DESCRIBE; + listenDescription(); + m_regime = LISTENER_IDLE; +} + +bool EasySocketListener::frameTime(uint32_t& _maxTime, uint32_t& _avgTime) +{ + if (m_bFrameTimeReady.exchange(false, ::std::memory_order_acquire)) + { + _maxTime = m_frameMax.load(::std::memory_order_acquire); + _avgTime = m_frameAvg.load(::std::memory_order_acquire); + return true; + } + + return false; +} + +bool EasySocketListener::requestFrameTime() +{ + if (m_regime != LISTENER_IDLE && m_regime != LISTENER_CAPTURE) + return false; + + if (m_thread.joinable()) + { + m_bInterrupt.store(true, ::std::memory_order_release); + m_thread.join(); + m_bInterrupt.store(false, ::std::memory_order_release); + } + + profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_MAIN_FRAME_TIME_MAX_AVG_US); + m_easySocket.send(&request, sizeof(request)); + + if (m_easySocket.isDisconnected()) + { + m_bConnected.store(false, ::std::memory_order_release); + return false; + } + + m_bFrameTimeReady.store(false, ::std::memory_order_release); + m_thread = ::std::thread(&EasySocketListener::listenFrameTime, this); + + return true; +} + +////////////////////////////////////////////////////////////////////////// + +void EasySocketListener::listenCapture() +{ + // TODO: Merge functions listenCapture() and listenDescription() + + static const int buffer_size = 8 * 1024 * 1024; + char* buffer = new char[buffer_size]; + int seek = 0, bytes = 0; + auto timeBegin = ::std::chrono::system_clock::now(); + + bool isListen = true, disconnected = false; + while (isListen && !m_bInterrupt.load(::std::memory_order_acquire)) + { + if (m_bStopReceive.load(::std::memory_order_acquire)) + { + profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE); + m_easySocket.send(&request, sizeof(request)); + m_bStopReceive.store(false, ::std::memory_order_release); + } + + if ((bytes - seek) == 0) + { + bytes = m_easySocket.receive(buffer, buffer_size); + + if (bytes == -1) + { + if (m_easySocket.isDisconnected()) + { + m_bConnected.store(false, ::std::memory_order_release); + isListen = false; + disconnected = true; + } + + seek = 0; + bytes = 0; + + continue; + } + + seek = 0; + } + + if (bytes == 0) + { + isListen = false; + break; + } + + char* buf = buffer + seek; + + if (bytes > 0) + { + auto message = reinterpret_cast(buf); + if (!message->isEasyNetMessage()) + continue; + + switch (message->type) + { + case profiler::net::MESSAGE_TYPE_ACCEPTED_CONNECTION: + { + qInfo() << "Receive MESSAGE_TYPE_ACCEPTED_CONNECTION"; + //m_easySocket.send(&request, sizeof(request)); + seek += sizeof(profiler::net::Message); + break; + } + + case profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING: + { + qInfo() << "Receive MESSAGE_TYPE_REPLY_START_CAPTURING"; + seek += sizeof(profiler::net::Message); + break; + } + + case profiler::net::MESSAGE_TYPE_REPLY_BLOCKS_END: + { + qInfo() << "Receive MESSAGE_TYPE_REPLY_BLOCKS_END"; + seek += sizeof(profiler::net::Message); + + const auto dt = ::std::chrono::duration_cast(::std::chrono::system_clock::now() - timeBegin); + const auto bytesNumber = m_receivedData.str().size(); + qInfo() << "recieved " << bytesNumber << " bytes, " << dt.count() << " ms, average speed = " << double(bytesNumber) * 1e3 / double(dt.count()) / 1024. << " kBytes/sec"; + + seek = 0; + bytes = 0; + + isListen = false; + + break; + } + + case profiler::net::MESSAGE_TYPE_REPLY_BLOCKS: + { + qInfo() << "Receive MESSAGE_TYPE_REPLY_BLOCKS"; + + seek += sizeof(profiler::net::DataMessage); + profiler::net::DataMessage* dm = (profiler::net::DataMessage*)message; + timeBegin = std::chrono::system_clock::now(); + + int neededSize = dm->size; + + + buf = buffer + seek; + auto bytesNumber = ::std::min((int)dm->size, bytes - seek); + m_receivedSize += bytesNumber; + m_receivedData.write(buf, bytesNumber); + neededSize -= bytesNumber; + + if (neededSize == 0) + seek += bytesNumber; + else + { + seek = 0; + bytes = 0; + } + + + int loaded = 0; + while (neededSize > 0) + { + bytes = m_easySocket.receive(buffer, buffer_size); + + if (bytes == -1) + { + if (m_easySocket.isDisconnected()) + { + m_bConnected.store(false, ::std::memory_order_release); + isListen = false; + disconnected = true; + neededSize = 0; + } + + break; + } + + buf = buffer; + int toWrite = ::std::min(bytes, neededSize); + m_receivedSize += toWrite; + m_receivedData.write(buf, toWrite); + neededSize -= toWrite; + loaded += toWrite; + seek = toWrite; + } + + if (m_bStopReceive.load(::std::memory_order_acquire)) + { + profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE); + m_easySocket.send(&request, sizeof(request)); + m_bStopReceive.store(false, ::std::memory_order_release); + } + + break; + } + + default: + //qInfo() << "Receive unknown " << message->type; + break; + } + } + } + + if (disconnected) + clearData(); + + delete [] buffer; + + m_bCaptureReady.store(true, ::std::memory_order_release); +} + +void EasySocketListener::listenDescription() +{ + // TODO: Merge functions listenDescription() and listenCapture() + + static const int buffer_size = 8 * 1024 * 1024; + char* buffer = new char[buffer_size]; + int seek = 0, bytes = 0; + + bool isListen = true, disconnected = false; + while (isListen && !m_bInterrupt.load(::std::memory_order_acquire)) + { + if ((bytes - seek) == 0) + { + bytes = m_easySocket.receive(buffer, buffer_size); + + if (bytes == -1) + { + if (m_easySocket.isDisconnected()) + { + m_bConnected.store(false, ::std::memory_order_release); + isListen = false; + disconnected = true; + } + + seek = 0; + bytes = 0; + + continue; + } + + seek = 0; + } + + if (bytes == 0) + { + isListen = false; + break; + } + + char* buf = buffer + seek; + + if (bytes > 0) + { + auto message = reinterpret_cast(buf); + if (!message->isEasyNetMessage()) + continue; + + switch (message->type) + { + case profiler::net::MESSAGE_TYPE_ACCEPTED_CONNECTION: + { + qInfo() << "Receive MESSAGE_TYPE_ACCEPTED_CONNECTION"; + seek += sizeof(profiler::net::Message); + break; + } + + case profiler::net::MESSAGE_TYPE_REPLY_BLOCKS_DESCRIPTION_END: + { + qInfo() << "Receive MESSAGE_TYPE_REPLY_BLOCKS_DESCRIPTION_END"; + seek += sizeof(profiler::net::Message); + + seek = 0; + bytes = 0; + + isListen = false; + + break; + } + + case profiler::net::MESSAGE_TYPE_REPLY_BLOCKS_DESCRIPTION: + { + qInfo() << "Receive MESSAGE_TYPE_REPLY_BLOCKS"; + + seek += sizeof(profiler::net::DataMessage); + profiler::net::DataMessage* dm = (profiler::net::DataMessage*)message; + int neededSize = dm->size; + + buf = buffer + seek; + auto bytesNumber = ::std::min((int)dm->size, bytes - seek); + m_receivedSize += bytesNumber; + m_receivedData.write(buf, bytesNumber); + neededSize -= bytesNumber; + + if (neededSize == 0) + seek += bytesNumber; + else{ + seek = 0; + bytes = 0; + } + + int loaded = 0; + while (neededSize > 0) + { + bytes = m_easySocket.receive(buffer, buffer_size); + + if (bytes == -1) + { + if (m_easySocket.isDisconnected()) + { + m_bConnected.store(false, ::std::memory_order_release); + isListen = false; + disconnected = true; + neededSize = 0; + } + + break; + } + + buf = buffer; + int toWrite = ::std::min(bytes, neededSize); + m_receivedSize += toWrite; + m_receivedData.write(buf, toWrite); + neededSize -= toWrite; + loaded += toWrite; + seek = toWrite; + } + + break; + } + + default: + break; + } + } + } + + if (disconnected) + clearData(); + + delete[] buffer; +} + +void EasySocketListener::listenFrameTime() +{ + // TODO: Merge functions listenDescription() and listenCapture() + + static const int buffer_size = sizeof(::profiler::net::TimestampMessage) << 2; + char buffer[buffer_size] = {}; + int seek = 0, bytes = 0; + + bool isListen = true; + while (isListen && !m_bInterrupt.load(::std::memory_order_acquire)) + { + if ((bytes - seek) == 0) + { + bytes = m_easySocket.receive(buffer, buffer_size); + + if (bytes == -1) + { + if (m_easySocket.isDisconnected()) + { + m_bConnected.store(false, ::std::memory_order_release); + isListen = false; + } + + seek = 0; + bytes = 0; + + continue; + } + + seek = 0; + } + + if (bytes == 0) + { + isListen = false; + break; + } + + char* buf = buffer + seek; + + if (bytes > 0) + { + auto message = reinterpret_cast(buf); + if (!message->isEasyNetMessage()) + continue; + + switch (message->type) + { + case profiler::net::MESSAGE_TYPE_ACCEPTED_CONNECTION: + case profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING: + { + seek += sizeof(profiler::net::Message); + break; + } + + case profiler::net::MESSAGE_TYPE_REPLY_MAIN_FRAME_TIME_MAX_AVG_US: + { + //qInfo() << "Receive MESSAGE_TYPE_REPLY_MAIN_FRAME_TIME_MAX_AVG_US"; + + seek += sizeof(profiler::net::TimestampMessage); + if (seek <= buffer_size) + { + profiler::net::TimestampMessage* timestampMessage = (profiler::net::TimestampMessage*)message; + m_frameMax.store(timestampMessage->maxValue, ::std::memory_order_release); + m_frameAvg.store(timestampMessage->avgValue, ::std::memory_order_release); + m_bFrameTimeReady.store(true, ::std::memory_order_release); + } + + isListen = false; + break; + } + + default: + break; + } + } + } +} + +////////////////////////////////////////////////////////////////////////// + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/main_window.h b/Source/ThirdParty/easy_profiler/profiler_gui/main_window.h new file mode 100644 index 0000000000..55eeba00f5 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/main_window.h @@ -0,0 +1,320 @@ +/************************************************************************ +* file name : main_window.h +* ----------------- : +* creation time : 2016/06/26 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of MainWindow for easy_profiler GUI. +* ----------------- : +* change log : * 2016/06/26 Victor Zarubkin: initial commit. +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_PROFILER_GUI__MAIN_WINDOW__H +#define EASY_PROFILER_GUI__MAIN_WINDOW__H + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#ifdef max +#undef max +#endif + +#ifdef min +#undef min +#endif + +////////////////////////////////////////////////////////////////////////// + +#define EASY_GUI_USE_DESCRIPTORS_DOCK_WINDOW 0 + +class QDockWidget; + +namespace profiler { namespace net { struct EasyProfilerStatus; } } + +////////////////////////////////////////////////////////////////////////// + +class EasyFileReader Q_DECL_FINAL +{ + ::profiler::SerializedData m_serializedBlocks; ///< + ::profiler::SerializedData m_serializedDescriptors; ///< + ::profiler::descriptors_list_t m_descriptors; ///< + ::profiler::blocks_t m_blocks; ///< + ::profiler::thread_blocks_tree_t m_blocksTree; ///< + ::std::stringstream m_stream; ///< + ::std::stringstream m_errorMessage; ///< + QString m_filename; ///< + uint32_t m_descriptorsNumberInFile = 0; ///< + uint32_t m_version = 0; ///< + ::std::thread m_thread; ///< + ::std::atomic_bool m_bDone; ///< + ::std::atomic m_progress; ///< + ::std::atomic m_size; ///< + bool m_isFile = false; ///< + +public: + + EasyFileReader(); + ~EasyFileReader(); + + const bool isFile() const; + bool done() const; + int progress() const; + unsigned int size() const; + const QString& filename() const; + + void load(const QString& _filename); + void load(::std::stringstream& _stream); + void interrupt(); + void get(::profiler::SerializedData& _serializedBlocks, ::profiler::SerializedData& _serializedDescriptors, + ::profiler::descriptors_list_t& _descriptors, ::profiler::blocks_t& _blocks, ::profiler::thread_blocks_tree_t& _tree, + uint32_t& _descriptorsNumberInFile, uint32_t& _version, QString& _filename); + + QString getError(); + +}; // END of class EasyFileReader. + +////////////////////////////////////////////////////////////////////////// + +enum EasyListenerRegime : uint8_t +{ + LISTENER_IDLE = 0, + LISTENER_CAPTURE, + LISTENER_CAPTURE_RECEIVE, + LISTENER_DESCRIBE +}; + +class EasySocketListener Q_DECL_FINAL +{ + EasySocket m_easySocket; ///< + ::std::string m_address; ///< + ::std::stringstream m_receivedData; ///< + ::std::thread m_thread; ///< + uint64_t m_receivedSize; ///< + uint16_t m_port; ///< + ::std::atomic m_frameMax; ///< + ::std::atomic m_frameAvg; ///< + ::std::atomic_bool m_bInterrupt; ///< + ::std::atomic_bool m_bConnected; ///< + ::std::atomic_bool m_bStopReceive; ///< + ::std::atomic_bool m_bCaptureReady; ///< + ::std::atomic_bool m_bFrameTimeReady; ///< + EasyListenerRegime m_regime; ///< + +public: + + EasySocketListener(); + ~EasySocketListener(); + + bool connected() const; + bool captured() const; + EasyListenerRegime regime() const; + uint64_t size() const; + const ::std::string& address() const; + uint16_t port() const; + + ::std::stringstream& data(); + void clearData(); + + void disconnect(); + bool connect(const char* _ipaddress, uint16_t _port, ::profiler::net::EasyProfilerStatus& _reply); + + bool startCapture(); + void stopCapture(); + void finalizeCapture(); + void requestBlocksDescription(); + + bool frameTime(uint32_t& _maxTime, uint32_t& _avgTime); + bool requestFrameTime(); + + template + inline void send(const T& _message) { + m_easySocket.send(&_message, sizeof(T)); + } + +private: + + void listenCapture(); + void listenDescription(); + void listenFrameTime(); + +}; // END of class EasySocketListener. + +////////////////////////////////////////////////////////////////////////// + +class EasyMainWindow : public QMainWindow +{ + Q_OBJECT + +protected: + + typedef EasyMainWindow This; + typedef QMainWindow Parent; + + QStringList m_lastFiles; + QString m_lastAddress; + QDockWidget* m_treeWidget = nullptr; + QDockWidget* m_graphicsView = nullptr; + QDockWidget* m_fpsViewer = nullptr; + +#if EASY_GUI_USE_DESCRIPTORS_DOCK_WINDOW != 0 + QDockWidget* m_descTreeWidget = nullptr; +#endif + + class QProgressDialog* m_progress = nullptr; + class QDialog* m_descTreeDialog = nullptr; + class EasyDescWidget* m_dialogDescTree = nullptr; + class QMessageBox* m_listenerDialog = nullptr; + QTimer m_readerTimer; + QTimer m_listenerTimer; + QTimer m_fpsRequestTimer; + ::profiler::SerializedData m_serializedBlocks; + ::profiler::SerializedData m_serializedDescriptors; + EasyFileReader m_reader; + EasySocketListener m_listener; + + class QLineEdit* m_addressEdit = nullptr; + class QLineEdit* m_portEdit = nullptr; + class QLineEdit* m_frameTimeEdit = nullptr; + + class QMenu* m_loadActionMenu = nullptr; + class QAction* m_saveAction = nullptr; + class QAction* m_deleteAction = nullptr; + + class QAction* m_captureAction = nullptr; + class QAction* m_connectAction = nullptr; + class QAction* m_eventTracingEnableAction = nullptr; + class QAction* m_eventTracingPriorityAction = nullptr; + + uint32_t m_descriptorsNumberInFile = 0; + uint16_t m_lastPort = 0; + bool m_bNetworkFileRegime = false; + bool m_bOpenedCacheFile = false; + +public: + + explicit EasyMainWindow(); + virtual ~EasyMainWindow(); + + // Public virtual methods + + void closeEvent(QCloseEvent* close_event) override; + void dragEnterEvent(QDragEnterEvent* drag_event) override; + void dragMoveEvent(QDragMoveEvent* drag_event) override; + void dragLeaveEvent(QDragLeaveEvent* drag_event) override; + void dropEvent(QDropEvent* drop_event) override; + +protected slots: + + void onOpenFileClicked(bool); + void onSaveFileClicked(bool); + void onDeleteClicked(bool); + void onExitClicked(bool); + void onEncodingChanged(bool); + void onChronoTextPosChanged(bool); + void onUnitsChanged(bool); + void onEnableDisableStatistics(bool); + void onCollapseItemsAfterCloseChanged(bool); + void onAllItemsExpandedByDefaultChange(bool); + void onBindExpandStatusChange(bool); + void onHierarchyFlagChange(bool); + void onExpandAllClicked(bool); + void onCollapseAllClicked(bool); + void onSpacingChange(int _value); + void onMinSizeChange(int _value); + void onNarrowSizeChange(int _value); + void onFpsIntervalChange(int _value); + void onFpsHistoryChange(int _value); + void onFpsMonitorLineWidthChange(int _value); + void onFileReaderTimeout(); + void onFrameTimeRequestTimeout(); + void onListenerTimerTimeout(); + void onFileReaderCancel(); + void onEditBlocksClicked(bool); + void onDescTreeDialogClose(int); + void onListenerDialogClose(int); + void onCaptureClicked(bool); + void onGetBlockDescriptionsClicked(bool); + void onConnectClicked(bool); + void onEventTracingPriorityChange(bool _checked); + void onEventTracingEnableChange(bool _checked); + void onFrameTimeEditFinish(); + void onFrameTimeChanged(); + + void onBlockStatusChange(::profiler::block_id_t _id, ::profiler::EasyBlockStatus _status); + + void checkFrameTimeReady(); + +private: + + // Private non-virtual methods + + void clear(); + + void refreshDiagram(); + + void addFileToList(const QString& filename); + void loadFile(const QString& filename); + void readStream(::std::stringstream& data); + + void loadSettings(); + void loadGeometry(); + void saveSettingsAndGeometry(); + + void setDisconnected(bool _showMessage = true); + +}; // END of class EasyMainWindow. + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_PROFILER_GUI__MAIN_WINDOW__H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/resources.qrc b/Source/ThirdParty/easy_profiler/profiler_gui/resources.qrc new file mode 100644 index 0000000000..5f6fd8a713 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/resources.qrc @@ -0,0 +1,29 @@ + + + icons/logo.svg + icons/off.svg + icons/open-folder2.svg + icons/reload-folder2.svg + icons/reload.svg + icons/expand.svg + icons/collapse.svg + icons/colors.svg + icons/colors-black.svg + icons/save.svg + icons/statistics.svg + icons/statistics2.svg + icons/lan.svg + icons/lan_on.svg + icons/wifi.svg + icons/wifi_on.svg + icons/lan.svg + icons/lan_on.svg + icons/play.svg + icons/stop.svg + icons/delete.svg + icons/list.svg + icons/search-next.svg + icons/search-prev.svg + icons/settings.svg + + diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/resources.rc b/Source/ThirdParty/easy_profiler/profiler_gui/resources.rc new file mode 100644 index 0000000000..7962c43217 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/resources.rc @@ -0,0 +1,33 @@ +IDI_ICON1 ICON DISCARDABLE "icons/logo.ico" +1 VERSIONINFO +FILEVERSION EASY_PROFILER_VERSION_MAJOR, EASY_PROFILER_VERSION_MINOR, EASY_PROFILER_VERSION_PATCH +PRODUCTVERSION EASY_PROFILER_VERSION_MAJOR, EASY_PROFILER_VERSION_MINOR, EASY_PROFILER_VERSION_PATCH + +# define EASY_STRINGIFY(a) #a +# define EASY_STRINGIFICATION(a) EASY_STRINGIFY(a) + +#define EASY_PROFILER_PRODUCT_VERSION "v" EASY_STRINGIFICATION(EASY_PROFILER_VERSION_MAJOR) "." \ + EASY_STRINGIFICATION(EASY_PROFILER_VERSION_MINOR) "." \ + EASY_STRINGIFICATION(EASY_PROFILER_VERSION_PATCH) + +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "080904b0" + BEGIN + VALUE "CompanyName", "EasySolutions" + VALUE "FileDescription", "EasyProfiler" + VALUE "InternalName", "profiler_gui" + VALUE "LegalCopyright", "Copyright (C) 2016-2017 Victor Zarubkin, Sergey Yagovtsev" + VALUE "LegalTrademarks1", "All Rights Reserved" + VALUE "LegalTrademarks2", "All Rights Reserved" + VALUE "OriginalFilename", "profiler_gui.exe" + VALUE "ProductName", "easy_profiler gui application" + VALUE "ProductVersion", EASY_PROFILER_PRODUCT_VERSION + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x809, 1200 + END +END diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_item.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_item.cpp new file mode 100644 index 0000000000..a955f96ca6 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_item.cpp @@ -0,0 +1,253 @@ +/************************************************************************ +* file name : tree_widget_item.cpp +* ----------------- : +* creation time : 2016/08/18 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of EasyTreeWidgetItem. +* ----------------- : +* change log : * 2016/08/18 Victor Zarubkin: Moved sources from blocks_tree_widget.cpp +* : and renamed classes from Prof* to Easy*. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include "tree_widget_item.h" +#include "globals.h" + +////////////////////////////////////////////////////////////////////////// + +EasyTreeWidgetItem::EasyTreeWidgetItem(const ::profiler::block_index_t _treeBlock, Parent* _parent) + : Parent(_parent) + , m_block(_treeBlock) + , m_customBGColor(0) + , m_customTextColor(0) +{ + +} + +EasyTreeWidgetItem::~EasyTreeWidgetItem() +{ +} + +bool EasyTreeWidgetItem::operator < (const Parent& _other) const +{ + const auto col = treeWidget()->sortColumn(); + + switch (col) + { + //case COL_UNKNOWN: + case COL_NAME: + { + if (parent() == nullptr) + return false; // Do not sort topLevelItems by name + return Parent::operator < (_other); + } + + case COL_NCALLS_PER_THREAD: + case COL_NCALLS_PER_PARENT: + case COL_NCALLS_PER_FRAME: + { + return data(col, Qt::UserRole).toUInt() < _other.data(col, Qt::UserRole).toUInt(); + } + + case COL_SELF_DURATION_PERCENT: + case COL_PERCENT_PER_PARENT: + case COL_PERCENT_PER_FRAME: + case COL_PERCENT_SUM_PER_PARENT: + case COL_PERCENT_SUM_PER_FRAME: + case COL_PERCENT_SUM_PER_THREAD: + { + return data(col, Qt::UserRole).toInt() < _other.data(col, Qt::UserRole).toInt(); + } + + case COL_ACTIVE_PERCENT: + { + return data(col, Qt::UserRole).toDouble() < _other.data(col, Qt::UserRole).toDouble(); + } + + default: + { + // durations min, max, average + return data(col, Qt::UserRole).toULongLong() < _other.data(col, Qt::UserRole).toULongLong(); + } + } + + return false; +} + +::profiler::block_index_t EasyTreeWidgetItem::block_index() const +{ + return m_block; +} + +::profiler_gui::EasyBlock& EasyTreeWidgetItem::guiBlock() +{ + return easyBlock(m_block); +} + +const ::profiler::BlocksTree& EasyTreeWidgetItem::block() const +{ + return blocksTree(m_block); +} + +::profiler::timestamp_t EasyTreeWidgetItem::duration() const +{ + if (parent() != nullptr) + return block().node->duration(); + return data(COL_DURATION, Qt::UserRole).toULongLong(); +} + +::profiler::timestamp_t EasyTreeWidgetItem::selfDuration() const +{ + return data(COL_SELF_DURATION, Qt::UserRole).toULongLong(); +} + +void EasyTreeWidgetItem::setTimeSmart(int _column, ::profiler_gui::TimeUnits _units, const ::profiler::timestamp_t& _time, const QString& _prefix) +{ + const ::profiler::timestamp_t nanosecondsTime = PROF_NANOSECONDS(_time); + + setData(_column, Qt::UserRole, (quint64)nanosecondsTime); + setToolTip(_column, QString("%1 ns").arg(nanosecondsTime)); + setText(_column, QString("%1%2").arg(_prefix).arg(::profiler_gui::timeStringRealNs(_units, nanosecondsTime, 3))); + +// if (_time < 1e3) +// { +// setText(_column, QString("%1%2 ns").arg(_prefix).arg(nanosecondsTime)); +// } +// else if (_time < 1e6) +// { +// setText(_column, QString("%1%2 us").arg(_prefix).arg(double(nanosecondsTime) * 1e-3, 0, 'f', 3)); +// } +// else if (_time < 1e9) +// { +// setText(_column, QString("%1%2 ms").arg(_prefix).arg(double(nanosecondsTime) * 1e-6, 0, 'f', 3)); +// } +// else +// { +// setText(_column, QString("%1%2 s").arg(_prefix).arg(double(nanosecondsTime) * 1e-9, 0, 'f', 3)); +// } +} + +void EasyTreeWidgetItem::setTimeSmart(int _column, ::profiler_gui::TimeUnits _units, const ::profiler::timestamp_t& _time) +{ + const ::profiler::timestamp_t nanosecondsTime = PROF_NANOSECONDS(_time); + + setData(_column, Qt::UserRole, (quint64)nanosecondsTime); + setToolTip(_column, QString("%1 ns").arg(nanosecondsTime)); + setText(_column, ::profiler_gui::timeStringRealNs(_units, nanosecondsTime, 3)); +} + +void EasyTreeWidgetItem::setTimeMs(int _column, const ::profiler::timestamp_t& _time) +{ + const ::profiler::timestamp_t nanosecondsTime = PROF_NANOSECONDS(_time); + setData(_column, Qt::UserRole, (quint64)nanosecondsTime); + setToolTip(_column, QString("%1 ns").arg(nanosecondsTime)); + setText(_column, QString::number(double(nanosecondsTime) * 1e-6, 'g', 9)); +} + +void EasyTreeWidgetItem::setTimeMs(int _column, const ::profiler::timestamp_t& _time, const QString& _prefix) +{ + const ::profiler::timestamp_t nanosecondsTime = PROF_NANOSECONDS(_time); + setData(_column, Qt::UserRole, (quint64)nanosecondsTime); + setToolTip(_column, QString("%1 ns").arg(nanosecondsTime)); + setText(_column, QString("%1%2").arg(_prefix).arg(double(nanosecondsTime) * 1e-6, 0, 'g', 9)); +} + +void EasyTreeWidgetItem::setBackgroundColor(QRgb _color) +{ + m_customBGColor = _color; +} + +void EasyTreeWidgetItem::setTextColor(QRgb _color) +{ + m_customTextColor = _color; +} + +void EasyTreeWidgetItem::colorize(bool _colorize) +{ + if (_colorize) + { + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + { + setBackground(i, QColor::fromRgb(m_customBGColor)); + setForeground(i, QColor::fromRgb(m_customTextColor)); + } + } + else + { + const QBrush nobrush; + for (int i = 0; i < COL_COLUMNS_NUMBER; ++i) + { + setBackground(i, nobrush); + setForeground(i, nobrush); + } + } +} + +void EasyTreeWidgetItem::collapseAll() +{ + for (int i = 0, childrenNumber = childCount(); i < childrenNumber; ++i) + { + static_cast(child(i))->collapseAll(); + } + + setExpanded(false); + if (parent() != nullptr) + guiBlock().expanded = false; +} + +void EasyTreeWidgetItem::expandAll() +{ + for (int i = 0, childrenNumber = childCount(); i < childrenNumber; ++i) + { + static_cast(child(i))->expandAll(); + } + + setExpanded(true); + if (parent() != nullptr) + guiBlock().expanded = true; +} + +////////////////////////////////////////////////////////////////////////// diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_item.h b/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_item.h new file mode 100644 index 0000000000..9126c867b7 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_item.h @@ -0,0 +1,162 @@ +/************************************************************************ +* file name : tree_widget_item.h +* ----------------- : +* creation time : 2016/08/18 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of EasyTreeWidgetItem +* : for displyaing EasyProfiler blocks tree. +* ----------------- : +* change log : * 2016/08/18 Victor Zarubkin: moved sources from blocks_tree_widget.h +* : and renamed classes from Prof* to Easy*. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_TREE_WIDGET_ITEM_H +#define EASY_TREE_WIDGET_ITEM_H + +#include +#include +#include + +#include "common_types.h" + +////////////////////////////////////////////////////////////////////////// + +enum EasyColumnsIndexes +{ + COL_UNKNOWN = -1, + + COL_NAME = 0, + + COL_BEGIN, + + COL_DURATION, + COL_SELF_DURATION, + COL_DURATION_SUM_PER_PARENT, + COL_DURATION_SUM_PER_FRAME, + COL_DURATION_SUM_PER_THREAD, + + COL_SELF_DURATION_PERCENT, + COL_PERCENT_PER_PARENT, + COL_PERCENT_PER_FRAME, + COL_PERCENT_SUM_PER_PARENT, + COL_PERCENT_SUM_PER_FRAME, + COL_PERCENT_SUM_PER_THREAD, + + COL_END, + + COL_MIN_PER_FRAME, + COL_MAX_PER_FRAME, + COL_AVERAGE_PER_FRAME, + COL_NCALLS_PER_FRAME, + + COL_MIN_PER_THREAD, + COL_MAX_PER_THREAD, + COL_AVERAGE_PER_THREAD, + COL_NCALLS_PER_THREAD, + + COL_MIN_PER_PARENT, + COL_MAX_PER_PARENT, + COL_AVERAGE_PER_PARENT, + COL_NCALLS_PER_PARENT, + + COL_ACTIVE_TIME, + COL_ACTIVE_PERCENT, + + COL_COLUMNS_NUMBER +}; + +////////////////////////////////////////////////////////////////////////// + +class EasyTreeWidgetItem : public QTreeWidgetItem +{ + typedef QTreeWidgetItem Parent; + typedef EasyTreeWidgetItem This; + + const ::profiler::block_index_t m_block; + QRgb m_customBGColor; + QRgb m_customTextColor; + +public: + + using Parent::setBackgroundColor; + using Parent::setTextColor; + + explicit EasyTreeWidgetItem(const ::profiler::block_index_t _treeBlock = ::profiler_gui::numeric_max(), Parent* _parent = nullptr); + virtual ~EasyTreeWidgetItem(); + + bool operator < (const Parent& _other) const override; + +public: + + ::profiler::block_index_t block_index() const; + ::profiler_gui::EasyBlock& guiBlock(); + const ::profiler::BlocksTree& block() const; + + ::profiler::timestamp_t duration() const; + ::profiler::timestamp_t selfDuration() const; + + void setTimeSmart(int _column, ::profiler_gui::TimeUnits _units, const ::profiler::timestamp_t& _time, const QString& _prefix); + void setTimeSmart(int _column, ::profiler_gui::TimeUnits _units, const ::profiler::timestamp_t& _time); + + void setTimeMs(int _column, const ::profiler::timestamp_t& _time); + void setTimeMs(int _column, const ::profiler::timestamp_t& _time, const QString& _prefix); + + void setBackgroundColor(QRgb _color); + + void setTextColor(QRgb _color); + + void colorize(bool _colorize); + + void collapseAll(); + + void expandAll(); + +}; // END of class EasyTreeWidgetItem. + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_TREE_WIDGET_ITEM_H diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_loader.cpp b/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_loader.cpp new file mode 100644 index 0000000000..2744e91894 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_loader.cpp @@ -0,0 +1,1003 @@ +/************************************************************************ +* file name : tree_widget_loader.h +* ----------------- : +* creation time : 2016/08/18 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains implementation of EasyTreeWidgetLoader which aim is +* : to load EasyProfiler blocks hierarchy in separate thread. +* ----------------- : +* change log : * 2016/08/18 Victor Zarubkin: moved sources from blocks_tree_widget.h/.cpp +* : and renamed Prof* to Easy*. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#include "tree_widget_loader.h" +#include "tree_widget_item.h" +#include "globals.h" + +#ifdef _WIN32 +#include + +#ifdef __MINGW32__ +#include +#endif + +#endif + +#ifdef max +#undef max +#endif + +#ifdef min +#undef min +#endif + +////////////////////////////////////////////////////////////////////////// + +EasyTreeWidgetLoader::EasyTreeWidgetLoader() + : m_bDone(ATOMIC_VAR_INIT(false)) + , m_bInterrupt(ATOMIC_VAR_INIT(false)) + , m_progress(ATOMIC_VAR_INIT(0)) + , m_mode(EasyTreeMode_Full) +{ +} + +EasyTreeWidgetLoader::~EasyTreeWidgetLoader() +{ + interrupt(true); +} + +bool EasyTreeWidgetLoader::done() const +{ + return m_bDone.load(); +} + +void EasyTreeWidgetLoader::setDone() +{ + m_bDone.store(true); + //m_progress.store(100); +} + +void EasyTreeWidgetLoader::setProgress(int _progress) +{ + m_progress.store(_progress); +} + +bool EasyTreeWidgetLoader::interrupted() const +{ + return m_bInterrupt.load(); +} + +int EasyTreeWidgetLoader::progress() const +{ + return m_progress.load(); +} + +void EasyTreeWidgetLoader::takeTopLevelItems(ThreadedItems& _output) +{ + if (done()) + { + _output = ::std::move(m_topLevelItems); + m_topLevelItems.clear(); + } +} + +void EasyTreeWidgetLoader::takeItems(Items& _output) +{ + if (done()) + { + _output = ::std::move(m_items); + m_items.clear(); + } +} + +void EasyTreeWidgetLoader::interrupt(bool _wait) +{ + m_bInterrupt.store(true); + if (m_thread.joinable()) + m_thread.join(); + + m_bInterrupt.store(false); + m_bDone.store(false); + m_progress.store(0); + + if (!_wait) + { + auto deleter_thread = ::std::thread([](decltype(m_topLevelItems) _items) + { +#ifdef _WIN32 + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST); +#endif + + for (auto item : _items) + delete item.second; + + }, ::std::move(m_topLevelItems)); + + deleter_thread.detach(); + } + else + { + for (auto item : m_topLevelItems) + delete item.second; + } + + m_items.clear(); + m_topLevelItems.clear(); + m_iditems.clear(); +} + +void EasyTreeWidgetLoader::fillTree(::profiler::timestamp_t& _beginTime, const unsigned int _blocksNumber, const ::profiler::thread_blocks_tree_t& _blocksTree, bool _colorizeRows, EasyTreeMode _mode) +{ + interrupt(); + m_mode = _mode; + m_thread = ::std::thread(&EasyTreeWidgetLoader::setTreeInternal1, this, + ::std::ref(_beginTime), _blocksNumber, ::std::ref(_blocksTree), _colorizeRows, + EASY_GLOBALS.add_zero_blocks_to_hierarchy, EASY_GLOBALS.use_decorated_thread_name, EASY_GLOBALS.hex_thread_id, EASY_GLOBALS.time_units); +} + +void EasyTreeWidgetLoader::fillTreeBlocks(const::profiler_gui::TreeBlocks& _blocks, ::profiler::timestamp_t _beginTime, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, bool _colorizeRows, EasyTreeMode _mode) +{ + interrupt(); + m_mode = _mode; + m_thread = ::std::thread(&EasyTreeWidgetLoader::setTreeInternal2, this, + _beginTime, ::std::ref(_blocks), _left, _right, _strict, _colorizeRows, + EASY_GLOBALS.add_zero_blocks_to_hierarchy, EASY_GLOBALS.use_decorated_thread_name, EASY_GLOBALS.hex_thread_id, EASY_GLOBALS.time_units); +} + +////////////////////////////////////////////////////////////////////////// + +void EasyTreeWidgetLoader::setTreeInternal1(::profiler::timestamp_t& _beginTime, const unsigned int _blocksNumber, const ::profiler::thread_blocks_tree_t& _blocksTree, bool _colorizeRows, bool _addZeroBlocks, bool _decoratedThreadNames, bool _hexThreadId, ::profiler_gui::TimeUnits _units) +{ + m_items.reserve(_blocksNumber + _blocksTree.size()); // _blocksNumber does not include Thread root blocks + + ::profiler::timestamp_t finishtime = 0; + for (const auto& threadTree : _blocksTree) + { + const auto node_block = blocksTree(threadTree.second.children.front()).node; + const auto startTime = node_block->begin(); + const auto endTime = node_block->end(); + + if (_beginTime > startTime) + _beginTime = startTime; + + if (finishtime < endTime) + finishtime = endTime; + } + + //const QSignalBlocker b(this); + const auto u_thread = ::profiler_gui::toUnicode("thread"); + int i = 0; + const int total = static_cast(_blocksTree.size()); + for (const auto& threadTree : _blocksTree) + { + if (interrupted()) + break; + + const auto& root = threadTree.second; + auto item = new EasyTreeWidgetItem(); + item->setText(COL_NAME, ::profiler_gui::decoratedThreadName(_decoratedThreadNames, root, u_thread, _hexThreadId)); + + ::profiler::timestamp_t duration = 0; + if (!root.children.empty()) + duration = blocksTree(root.children.back()).node->end() - blocksTree(root.children.front()).node->begin(); + + item->setTimeSmart(COL_DURATION, _units, duration); + item->setBackgroundColor(::profiler_gui::SELECTED_THREAD_BACKGROUND); + item->setTextColor(::profiler_gui::SELECTED_THREAD_FOREGROUND); + + //_items.push_back(item); + + item->setTimeSmart(COL_SELF_DURATION, _units, root.profiled_time); + + ::profiler::timestamp_t children_duration = 0; + const auto children_items_number = setTreeInternal(root, 0, _beginTime, root.children, item, nullptr, _beginTime, finishtime + 1000000000ULL, false, children_duration, _colorizeRows, _addZeroBlocks, _units); + + if (children_items_number > 0) + { + //total_items += children_items_number + 1; + //addTopLevelItem(item); + //m_roots[threadTree.first] = item; + m_topLevelItems.emplace_back(root.thread_id, item); + } + else + { + //_items.pop_back(); + delete item; + } + + setProgress((100 * ++i) / total); + } + + setDone(); + //return total_items; +} + +////////////////////////////////////////////////////////////////////////// + +// auto calculateTotalChildrenNumber(const ::profiler::BlocksTree& _tree) -> decltype(_tree.children.size()) +// { +// auto children_number = _tree.children.size(); +// for (auto i : _tree.children) +// children_number += calculateTotalChildrenNumber(blocksTree(i)); +// return children_number; +// } + +typedef ::std::unordered_map<::profiler::thread_id_t, ::profiler::block_index_t, ::profiler::passthrough_hash<::profiler::thread_id_t> > BeginEndIndicesMap; + +void EasyTreeWidgetLoader::setTreeInternal2(const ::profiler::timestamp_t& _beginTime, const ::profiler_gui::TreeBlocks& _blocks, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, bool _colorizeRows, bool _addZeroBlocks, bool _decoratedThreadNames, bool _hexThreadId, ::profiler_gui::TimeUnits _units) +{ + //size_t blocksNumber = 0; + //for (const auto& block : _blocks) + // blocksNumber += calculateTotalChildrenNumber(*block.tree); + // //blocksNumber += block.tree->total_children_number; + //m_items.reserve(blocksNumber + _blocks.size()); // blocksNumber does not include root blocks + + BeginEndIndicesMap beginEndMap; + RootsMap threadsMap; + + auto const setTree = (m_mode == EasyTreeMode_Full) ? &EasyTreeWidgetLoader::setTreeInternal : &EasyTreeWidgetLoader::setTreeInternalPlain; + + const auto u_thread = ::profiler_gui::toUnicode("thread"); + int i = 0, total = static_cast(_blocks.size()); + //const QSignalBlocker b(this); + for (const auto& block : _blocks) + { + if (interrupted()) + break; + + auto& gui_block = easyBlock(block.tree); + const auto startTime = gui_block.tree.node->begin(); + const auto endTime = gui_block.tree.node->end(); + if (startTime > _right || endTime < _left) + { + setProgress((90 * ++i) / total); + continue; + } + + ::profiler::timestamp_t duration = 0; + EasyTreeWidgetItem* thread_item = nullptr; + ::profiler::block_index_t& firstCswitch = beginEndMap[block.root->thread_id]; + auto thread_item_it = threadsMap.find(block.root->thread_id); + if (thread_item_it != threadsMap.end()) + { + thread_item = thread_item_it->second; + } + else + { + thread_item = new EasyTreeWidgetItem(); + thread_item->setText(COL_NAME, ::profiler_gui::decoratedThreadName(_decoratedThreadNames, *block.root, u_thread, _hexThreadId)); + + if (!block.root->children.empty()) + duration = blocksTree(block.root->children.back()).node->end() - blocksTree(block.root->children.front()).node->begin(); + + thread_item->setTimeSmart(COL_DURATION, _units, duration); + thread_item->setBackgroundColor(::profiler_gui::SELECTED_THREAD_BACKGROUND); + thread_item->setTextColor(::profiler_gui::SELECTED_THREAD_FOREGROUND); + + // Sum of all children durations: + thread_item->setTimeSmart(COL_SELF_DURATION, _units, block.root->profiled_time); + + threadsMap.insert(::std::make_pair(block.root->thread_id, thread_item)); + + firstCswitch = 0; + auto it = ::std::lower_bound(block.root->sync.begin(), block.root->sync.end(), _left, [](::profiler::block_index_t ind, decltype(_left) _val) + { + return EASY_GLOBALS.gui_blocks[ind].tree.node->begin() < _val; + }); + + if (it != block.root->sync.end()) + { + firstCswitch = it - block.root->sync.begin(); + if (firstCswitch > 0) + --firstCswitch; + } + else + { + firstCswitch = static_cast<::profiler::block_index_t>(block.root->sync.size()); + } + } + + bool hasContextSwitch = false; + ::profiler::timestamp_t idleTime = 0; + for (::profiler::block_index_t ind = firstCswitch, ncs = static_cast<::profiler::block_index_t>(block.root->sync.size()); ind < ncs; ++ind) + { + auto cs_index = block.root->sync[ind]; + const auto cs = EASY_GLOBALS.gui_blocks[cs_index].tree.node; + + if (cs->begin() > endTime) + { + if (!hasContextSwitch) + firstCswitch = ind; + break; + } + + if (startTime <= cs->begin() && cs->end() <= endTime) + { + if (!hasContextSwitch) + { + firstCswitch = ind; + hasContextSwitch = true; + } + + idleTime += cs->duration(); + } + } + + auto item = new EasyTreeWidgetItem(block.tree, thread_item); + duration = endTime - startTime; + + auto name = *gui_block.tree.node->name() != 0 ? gui_block.tree.node->name() : easyDescriptor(gui_block.tree.node->id()).name(); + item->setText(COL_NAME, ::profiler_gui::toUnicode(name)); + item->setTimeSmart(COL_DURATION, _units, duration); + + auto active_time = duration - idleTime; + auto active_percent = duration == 0 ? 100. : ::profiler_gui::percentReal(active_time, duration); + item->setTimeSmart(COL_ACTIVE_TIME, _units, active_time); + item->setText(COL_ACTIVE_PERCENT, QString::number(active_percent, 'g', 3)); + item->setData(COL_ACTIVE_PERCENT, Qt::UserRole, active_percent); + + item->setTimeMs(COL_BEGIN, startTime - _beginTime); + item->setTimeMs(COL_END, endTime - _beginTime); + + item->setData(COL_PERCENT_PER_FRAME, Qt::UserRole, 0); + + auto percentage_per_thread = ::profiler_gui::percent(duration, block.root->profiled_time); + item->setData(COL_PERCENT_PER_PARENT, Qt::UserRole, percentage_per_thread); + item->setText(COL_PERCENT_PER_PARENT, QString::number(percentage_per_thread)); + + if (gui_block.tree.per_thread_stats != nullptr) // if there is per_thread_stats then there are other stats also + { + const ::profiler::BlockStatistics* per_thread_stats = gui_block.tree.per_thread_stats; + const ::profiler::BlockStatistics* per_parent_stats = gui_block.tree.per_parent_stats; + const ::profiler::BlockStatistics* per_frame_stats = gui_block.tree.per_frame_stats; + + + if (per_thread_stats->calls_number > 1 || !EASY_GLOBALS.display_only_relevant_stats) + { + item->setTimeSmart(COL_MIN_PER_THREAD, _units, easyBlock(per_thread_stats->min_duration_block).tree.node->duration()); + item->setTimeSmart(COL_MAX_PER_THREAD, _units, easyBlock(per_thread_stats->max_duration_block).tree.node->duration()); + item->setTimeSmart(COL_AVERAGE_PER_THREAD, _units, per_thread_stats->average_duration()); + item->setTimeSmart(COL_DURATION_SUM_PER_THREAD, _units, per_thread_stats->total_duration); + } + + item->setData(COL_NCALLS_PER_THREAD, Qt::UserRole, per_thread_stats->calls_number); + item->setText(COL_NCALLS_PER_THREAD, QString::number(per_thread_stats->calls_number)); + + percentage_per_thread = ::profiler_gui::percent(per_thread_stats->total_duration, block.root->profiled_time); + item->setData(COL_PERCENT_SUM_PER_THREAD, Qt::UserRole, percentage_per_thread); + item->setText(COL_PERCENT_SUM_PER_THREAD, QString::number(percentage_per_thread)); + + + if (per_parent_stats->calls_number > 1 || !EASY_GLOBALS.display_only_relevant_stats) + { + item->setTimeSmart(COL_MIN_PER_PARENT, _units, easyBlock(per_parent_stats->min_duration_block).tree.node->duration()); + item->setTimeSmart(COL_MAX_PER_PARENT, _units, easyBlock(per_parent_stats->max_duration_block).tree.node->duration()); + item->setTimeSmart(COL_AVERAGE_PER_PARENT, _units, per_parent_stats->average_duration()); + item->setTimeSmart(COL_DURATION_SUM_PER_PARENT, _units, per_parent_stats->total_duration); + } + + item->setData(COL_NCALLS_PER_PARENT, Qt::UserRole, per_parent_stats->calls_number); + item->setText(COL_NCALLS_PER_PARENT, QString::number(per_parent_stats->calls_number)); + + + if (per_frame_stats->calls_number > 1 || !EASY_GLOBALS.display_only_relevant_stats) + { + item->setTimeSmart(COL_MIN_PER_FRAME, _units, easyBlock(per_frame_stats->min_duration_block).tree.node->duration()); + item->setTimeSmart(COL_MAX_PER_FRAME, _units, easyBlock(per_frame_stats->max_duration_block).tree.node->duration()); + item->setTimeSmart(COL_AVERAGE_PER_FRAME, _units, per_frame_stats->average_duration()); + item->setTimeSmart(COL_DURATION_SUM_PER_FRAME, _units, per_frame_stats->total_duration); + } + + item->setData(COL_NCALLS_PER_FRAME, Qt::UserRole, per_frame_stats->calls_number); + item->setText(COL_NCALLS_PER_FRAME, QString::number(per_frame_stats->calls_number)); + } + else + { + item->setData(COL_PERCENT_SUM_PER_THREAD, Qt::UserRole, 0); + item->setText(COL_PERCENT_SUM_PER_THREAD, ""); + } + + const auto color = easyDescriptor(gui_block.tree.node->id()).color(); + //const auto bgColor = ::profiler_gui::fromProfilerRgb(::profiler::colors::get_red(color), ::profiler::colors::get_green(color), ::profiler::colors::get_blue(color)); + const auto fgColor = ::profiler_gui::textColorForRgb(color);//0x00ffffff - bgColor; + item->setBackgroundColor(color); + item->setTextColor(fgColor); + +#ifdef EASY_TREE_WIDGET__USE_VECTOR + auto item_index = static_cast(m_items.size()); + m_items.push_back(item); +#endif + + size_t children_items_number = 0; + ::profiler::timestamp_t children_duration = 0; + if (!gui_block.tree.children.empty()) + { + m_iditems.clear(); + children_items_number = (this->*setTree)(*block.root, firstCswitch, _beginTime, gui_block.tree.children, item, item, _left, _right, _strict, children_duration, _colorizeRows, _addZeroBlocks, _units); + if (interrupted()) + break; + } + + int percentage = 100; + auto self_duration = duration - children_duration; + if (children_duration > 0 && duration > 0) + { + percentage = static_cast(0.5 + 100. * static_cast(self_duration) / static_cast(duration)); + } + + item->setTimeSmart(COL_SELF_DURATION, _units, self_duration); + item->setData(COL_SELF_DURATION_PERCENT, Qt::UserRole, percentage); + item->setText(COL_SELF_DURATION_PERCENT, QString::number(percentage)); + + if (children_items_number > 0 || !_strict || (startTime >= _left && endTime <= _right)) + { + //total_items += children_items_number + 1; +#ifdef EASY_TREE_WIDGET__USE_VECTOR + gui_block.tree_item = item_index; +#endif + + if (_colorizeRows) + item->colorize(_colorizeRows); + + if (gui_block.expanded) + item->setExpanded(true); + +#ifndef EASY_TREE_WIDGET__USE_VECTOR + m_items.insert(::std::make_pair(block.tree, item)); +#endif + } + else + { +#ifdef EASY_TREE_WIDGET__USE_VECTOR + m_items.pop_back(); +#endif + delete item; + } + + setProgress((90 * ++i) / total); + } + + i = 0; + total = static_cast(threadsMap.size()); + for (auto& it : threadsMap) + { + auto item = it.second; + + if (item->childCount() > 0) + { + //addTopLevelItem(item); + //m_roots[it.first] = item; + + //_items.push_back(item); + m_topLevelItems.emplace_back(it.first, item); + + //++total_items; + } + else + { + delete item; + } + + setProgress(90 + (10 * ++i) / total); + } + + setDone(); + //return total_items; +} + +////////////////////////////////////////////////////////////////////////// + +size_t EasyTreeWidgetLoader::setTreeInternal(const ::profiler::BlocksTreeRoot& _threadRoot, ::profiler::block_index_t _firstCswitch, const ::profiler::timestamp_t& _beginTime, const ::profiler::BlocksTree::children_t& _children, EasyTreeWidgetItem* _parent, EasyTreeWidgetItem* _frame, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, ::profiler::timestamp_t& _duration, bool _colorizeRows, bool _addZeroBlocks, ::profiler_gui::TimeUnits _units) +{ + auto const setTree = m_mode == EasyTreeMode_Full ? &EasyTreeWidgetLoader::setTreeInternal : &EasyTreeWidgetLoader::setTreeInternalPlain; + + size_t total_items = 0; + for (auto child_index : _children) + { + if (interrupted()) + break; + + auto& gui_block = easyBlock(child_index); + const auto& child = gui_block.tree; + const auto startTime = child.node->begin(); + const auto endTime = child.node->end(); + const auto duration = endTime - startTime; + + if (duration == 0 && !_addZeroBlocks) + continue; + + _duration += duration; + + if (startTime > _right || endTime < _left) + continue; + + bool hasContextSwitch = false; + ::profiler::timestamp_t idleTime = 0; + for (::profiler::block_index_t ind = _firstCswitch, ncs = static_cast<::profiler::block_index_t>(_threadRoot.sync.size()); ind < ncs; ++ind) + { + auto cs_index = _threadRoot.sync[ind]; + const auto cs = EASY_GLOBALS.gui_blocks[cs_index].tree.node; + + if (cs->begin() > endTime) + { + if (!hasContextSwitch) + _firstCswitch = ind; + break; + } + + if (startTime <= cs->begin() && cs->end() <= endTime) + { + if (!hasContextSwitch) + { + _firstCswitch = ind; + hasContextSwitch = true; + } + + idleTime += cs->duration(); + } + } + + auto item = new EasyTreeWidgetItem(child_index, _parent); + + auto name = *child.node->name() != 0 ? child.node->name() : easyDescriptor(child.node->id()).name(); + item->setText(COL_NAME, ::profiler_gui::toUnicode(name)); + item->setTimeSmart(COL_DURATION, _units, duration); + + auto active_time = duration - idleTime; + auto active_percent = duration == 0 ? 100. : ::profiler_gui::percentReal(active_time, duration); + item->setTimeSmart(COL_ACTIVE_TIME, _units, active_time); + item->setText(COL_ACTIVE_PERCENT, QString::number(active_percent, 'g', 3)); + item->setData(COL_ACTIVE_PERCENT, Qt::UserRole, active_percent); + + item->setTimeMs(COL_BEGIN, startTime - _beginTime); + item->setTimeMs(COL_END, endTime - _beginTime); + item->setData(COL_PERCENT_SUM_PER_THREAD, Qt::UserRole, 0); + + if (child.per_thread_stats != nullptr) // if there is per_thread_stats then there are other stats also + { + const ::profiler::BlockStatistics* per_thread_stats = child.per_thread_stats; + const ::profiler::BlockStatistics* per_parent_stats = child.per_parent_stats; + const ::profiler::BlockStatistics* per_frame_stats = child.per_frame_stats; + + auto parent_duration = _parent->duration(); + auto percentage = duration == 0 ? 0 : ::profiler_gui::percent(duration, parent_duration); + auto percentage_sum = ::profiler_gui::percent(per_parent_stats->total_duration, parent_duration); + item->setData(COL_PERCENT_PER_PARENT, Qt::UserRole, percentage); + item->setText(COL_PERCENT_PER_PARENT, QString::number(percentage)); + item->setData(COL_PERCENT_SUM_PER_PARENT, Qt::UserRole, percentage_sum); + item->setText(COL_PERCENT_SUM_PER_PARENT, QString::number(percentage_sum)); + + if (_frame != nullptr) + { + if (_parent != _frame) + { + parent_duration = _frame->duration(); + percentage = duration == 0 ? 0 : ::profiler_gui::percent(duration, parent_duration); + percentage_sum = ::profiler_gui::percent(per_frame_stats->total_duration, parent_duration); + } + + item->setData(COL_PERCENT_PER_FRAME, Qt::UserRole, percentage); + item->setText(COL_PERCENT_PER_FRAME, QString::number(percentage)); + item->setData(COL_PERCENT_SUM_PER_FRAME, Qt::UserRole, percentage_sum); + item->setText(COL_PERCENT_SUM_PER_FRAME, QString::number(percentage_sum)); + } + else + { + item->setData(COL_PERCENT_PER_FRAME, Qt::UserRole, 0); + item->setData(COL_PERCENT_SUM_PER_FRAME, Qt::UserRole, 0); + + auto percentage_per_thread = ::profiler_gui::percent(duration, _threadRoot.profiled_time); + item->setData(COL_PERCENT_PER_PARENT, Qt::UserRole, percentage_per_thread); + item->setText(COL_PERCENT_PER_PARENT, QString::number(percentage_per_thread)); + } + + + if (per_thread_stats->calls_number > 1 || !EASY_GLOBALS.display_only_relevant_stats) + { + item->setTimeSmart(COL_MIN_PER_THREAD, _units, easyBlock(per_thread_stats->min_duration_block).tree.node->duration()); + item->setTimeSmart(COL_MAX_PER_THREAD, _units, easyBlock(per_thread_stats->max_duration_block).tree.node->duration()); + item->setTimeSmart(COL_AVERAGE_PER_THREAD, _units, per_thread_stats->average_duration()); + item->setTimeSmart(COL_DURATION_SUM_PER_THREAD, _units, per_thread_stats->total_duration); + } + + item->setData(COL_NCALLS_PER_THREAD, Qt::UserRole, per_thread_stats->calls_number); + item->setText(COL_NCALLS_PER_THREAD, QString::number(per_thread_stats->calls_number)); + + auto percentage_per_thread = ::profiler_gui::percent(per_thread_stats->total_duration, _threadRoot.profiled_time); + item->setData(COL_PERCENT_SUM_PER_THREAD, Qt::UserRole, percentage_per_thread); + item->setText(COL_PERCENT_SUM_PER_THREAD, QString::number(percentage_per_thread)); + + + if (per_parent_stats->calls_number > 1 || !EASY_GLOBALS.display_only_relevant_stats) + { + item->setTimeSmart(COL_MIN_PER_PARENT, _units, easyBlock(per_parent_stats->min_duration_block).tree.node->duration()); + item->setTimeSmart(COL_MAX_PER_PARENT, _units, easyBlock(per_parent_stats->max_duration_block).tree.node->duration()); + item->setTimeSmart(COL_AVERAGE_PER_PARENT, _units, per_parent_stats->average_duration()); + item->setTimeSmart(COL_DURATION_SUM_PER_PARENT, _units, per_parent_stats->total_duration); + } + + item->setData(COL_NCALLS_PER_PARENT, Qt::UserRole, per_parent_stats->calls_number); + item->setText(COL_NCALLS_PER_PARENT, QString::number(per_parent_stats->calls_number)); + + + if (per_frame_stats->calls_number > 1 || !EASY_GLOBALS.display_only_relevant_stats) + { + item->setTimeSmart(COL_MIN_PER_FRAME, _units, easyBlock(per_frame_stats->min_duration_block).tree.node->duration()); + item->setTimeSmart(COL_MAX_PER_FRAME, _units, easyBlock(per_frame_stats->max_duration_block).tree.node->duration()); + item->setTimeSmart(COL_AVERAGE_PER_FRAME, _units, per_frame_stats->average_duration()); + item->setTimeSmart(COL_DURATION_SUM_PER_FRAME, _units, per_frame_stats->total_duration); + } + + item->setData(COL_NCALLS_PER_FRAME, Qt::UserRole, per_frame_stats->calls_number); + item->setText(COL_NCALLS_PER_FRAME, QString::number(per_frame_stats->calls_number)); + } + else + { + if (_frame == nullptr) + { + auto percentage_per_thread = ::profiler_gui::percent(duration, _threadRoot.profiled_time); + item->setData(COL_PERCENT_PER_PARENT, Qt::UserRole, percentage_per_thread); + item->setText(COL_PERCENT_PER_PARENT, QString::number(percentage_per_thread)); + } + else + { + item->setData(COL_PERCENT_PER_PARENT, Qt::UserRole, 0); + } + + item->setData(COL_PERCENT_SUM_PER_PARENT, Qt::UserRole, 0); + item->setData(COL_PERCENT_SUM_PER_THREAD, Qt::UserRole, 0); + } + + const auto color = easyDescriptor(child.node->id()).color(); + //const auto bgColor = ::profiler_gui::fromProfilerRgb(::profiler::colors::get_red(color), ::profiler::colors::get_green(color), ::profiler::colors::get_blue(color)); + const auto fgColor = ::profiler_gui::textColorForRgb(color);// 0x00ffffff - bgColor; + item->setBackgroundColor(color); + item->setTextColor(fgColor); + +#ifdef EASY_TREE_WIDGET__USE_VECTOR + auto item_index = static_cast(m_items.size()); + m_items.push_back(item); +#endif + + size_t children_items_number = 0; + ::profiler::timestamp_t children_duration = 0; + if (!child.children.empty()) + { + m_iditems.clear(); + children_items_number = (this->*setTree)(_threadRoot, _firstCswitch, _beginTime, child.children, item, _frame ? _frame : item, _left, _right, _strict, children_duration, _colorizeRows, _addZeroBlocks, _units); + if (interrupted()) + break; + } + + int percentage = 100; + auto self_duration = duration - children_duration; + if (children_duration > 0 && duration > 0) + { + percentage = ::profiler_gui::percent(self_duration, duration); + } + + item->setTimeSmart(COL_SELF_DURATION, _units, self_duration); + item->setData(COL_SELF_DURATION_PERCENT, Qt::UserRole, percentage); + item->setText(COL_SELF_DURATION_PERCENT, QString::number(percentage)); + + if (children_items_number > 0 || !_strict || (startTime >= _left && endTime <= _right)) + { + total_items += children_items_number + 1; +#ifdef EASY_TREE_WIDGET__USE_VECTOR + gui_block.tree_item = item_index; +#endif + + if (_colorizeRows) + item->colorize(_colorizeRows); + + if (gui_block.expanded) + item->setExpanded(true); + +#ifndef EASY_TREE_WIDGET__USE_VECTOR + m_items.insert(::std::make_pair(child_index, item)); +#endif + } + else + { +#ifdef EASY_TREE_WIDGET__USE_VECTOR + m_items.pop_back(); +#endif + delete item; + } + } + + return total_items; +} + +////////////////////////////////////////////////////////////////////////// + +::profiler::timestamp_t EasyTreeWidgetLoader::calculateChildrenDurationRecursive(const ::profiler::BlocksTree::children_t& _children, ::profiler::block_id_t _id) +{ + ::profiler::timestamp_t total_duration = 0; + + for (auto child_index : _children) + { + if (interrupted()) + break; + + const auto& gui_block = easyBlock(child_index); + total_duration += gui_block.tree.node->duration(); + if (gui_block.tree.node->id() == _id) + total_duration += calculateChildrenDurationRecursive(gui_block.tree.children, _id); + } + + return total_duration; +} + +size_t EasyTreeWidgetLoader::setTreeInternalPlain(const ::profiler::BlocksTreeRoot& _threadRoot, ::profiler::block_index_t _firstCswitch, const ::profiler::timestamp_t& _beginTime, const ::profiler::BlocksTree::children_t& _children, EasyTreeWidgetItem*, EasyTreeWidgetItem* _frame, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, ::profiler::timestamp_t& _duration, bool _colorizeRows, bool _addZeroBlocks, ::profiler_gui::TimeUnits _units) +{ + size_t total_items = 0; + for (auto child_index : _children) + { + if (interrupted()) + break; + + const auto& gui_block = easyBlock(child_index); + const auto& child = gui_block.tree; + const auto startTime = child.node->begin(); + const auto endTime = child.node->end(); + const auto duration = endTime - startTime; + + _duration += duration; + + auto it = m_iditems.find(child.node->id()); + if (it != m_iditems.end()) + { + ++total_items; + + ::profiler::timestamp_t children_duration = 0; + if (!child.children.empty()) + { + setTreeInternalPlain(_threadRoot, _firstCswitch, _beginTime, child.children, _frame, _frame, _left, _right, _strict, children_duration, _colorizeRows, _addZeroBlocks, _units); + if (interrupted()) + break; + } + + if (it->second != nullptr && child.per_frame_stats != nullptr) + { + auto item = it->second; + + //auto children_duration = calculateChildrenDurationRecursive(child.children, it->first); + if (children_duration != 0) + { + auto self_duration = item->data(COL_SELF_DURATION, Qt::UserRole).toULongLong() - children_duration; + + int percentage = 100; + if (child.per_frame_stats->total_duration > 0) + percentage = ::profiler_gui::percent(self_duration, child.per_frame_stats->total_duration); + + item->setTimeSmart(COL_SELF_DURATION, _units, self_duration); + item->setData(COL_SELF_DURATION_PERCENT, Qt::UserRole, percentage); + item->setText(COL_SELF_DURATION_PERCENT, QString::number(percentage)); + } + + bool hasContextSwitch = false; + ::profiler::timestamp_t idleTime = 0; + for (::profiler::block_index_t ind = _firstCswitch, ncs = static_cast<::profiler::block_index_t>(_threadRoot.sync.size()); ind < ncs; ++ind) + { + auto cs_index = _threadRoot.sync[ind]; + const auto cs = EASY_GLOBALS.gui_blocks[cs_index].tree.node; + + if (cs->begin() > endTime) + { + if (!hasContextSwitch) + _firstCswitch = ind; + break; + } + + if (startTime <= cs->begin() && cs->end() <= endTime) + { + if (!hasContextSwitch) + { + _firstCswitch = ind; + hasContextSwitch = true; + } + + idleTime += cs->duration(); + } + } + + auto active_time = item->data(COL_ACTIVE_TIME, Qt::UserRole).toULongLong() - idleTime; + auto active_percent = child.per_frame_stats->total_duration == 0 ? 100. : ::profiler_gui::percentReal(active_time, child.per_frame_stats->total_duration); + item->setTimeSmart(COL_ACTIVE_TIME, _units, active_time); + item->setText(COL_ACTIVE_PERCENT, QString::number(active_percent, 'g', 3)); + item->setData(COL_ACTIVE_PERCENT, Qt::UserRole, active_percent); + } + + continue; + } + + if (startTime > _right || endTime < _left) + continue; + + bool hasContextSwitch = false; + ::profiler::timestamp_t idleTime = 0; + for (::profiler::block_index_t ind = _firstCswitch, ncs = static_cast<::profiler::block_index_t>(_threadRoot.sync.size()); ind < ncs; ++ind) + { + auto cs_index = _threadRoot.sync[ind]; + const auto cs = EASY_GLOBALS.gui_blocks[cs_index].tree.node; + + if (cs->begin() > endTime) + { + if (!hasContextSwitch) + _firstCswitch = ind; + break; + } + + if (startTime <= cs->begin() && cs->end() <= endTime) + { + if (!hasContextSwitch) + { + _firstCswitch = ind; + hasContextSwitch = true; + } + + idleTime += cs->duration(); + } + } + + auto item = new EasyTreeWidgetItem(child_index, _frame); + + auto name = *child.node->name() != 0 ? child.node->name() : easyDescriptor(child.node->id()).name(); + item->setText(COL_NAME, ::profiler_gui::toUnicode(name)); + + if (child.per_thread_stats != nullptr) // if there is per_thread_stats then there are other stats also + { + const ::profiler::BlockStatistics* per_thread_stats = child.per_thread_stats; + if (per_thread_stats->calls_number > 1 || !EASY_GLOBALS.display_only_relevant_stats) + { + item->setTimeSmart(COL_MIN_PER_THREAD, _units, easyBlock(per_thread_stats->min_duration_block).tree.node->duration()); + item->setTimeSmart(COL_MAX_PER_THREAD, _units, easyBlock(per_thread_stats->max_duration_block).tree.node->duration()); + item->setTimeSmart(COL_AVERAGE_PER_THREAD, _units, per_thread_stats->average_duration()); + } + + item->setTimeSmart(COL_DURATION_SUM_PER_THREAD, _units, per_thread_stats->total_duration); + item->setData(COL_NCALLS_PER_THREAD, Qt::UserRole, per_thread_stats->calls_number); + item->setText(COL_NCALLS_PER_THREAD, QString::number(per_thread_stats->calls_number)); + + auto percentage_per_thread = ::profiler_gui::percent(per_thread_stats->total_duration, _threadRoot.profiled_time); + item->setData(COL_PERCENT_SUM_PER_THREAD, Qt::UserRole, percentage_per_thread); + item->setText(COL_PERCENT_SUM_PER_THREAD, QString::number(percentage_per_thread)); + + const ::profiler::BlockStatistics* per_frame_stats = child.per_frame_stats; + const auto percentage_sum = ::profiler_gui::percent(per_frame_stats->total_duration, _frame->duration()); + item->setData(COL_PERCENT_PER_FRAME, Qt::UserRole, percentage_sum); + item->setText(COL_PERCENT_PER_FRAME, QString::number(percentage_sum)); + + if (per_frame_stats->calls_number > 1 || !EASY_GLOBALS.display_only_relevant_stats) + { + item->setTimeSmart(COL_MIN_PER_FRAME, _units, easyBlock(per_frame_stats->min_duration_block).tree.node->duration()); + item->setTimeSmart(COL_MAX_PER_FRAME, _units, easyBlock(per_frame_stats->max_duration_block).tree.node->duration()); + item->setTimeSmart(COL_AVERAGE_PER_FRAME, _units, per_frame_stats->average_duration()); + } + + item->setTimeSmart(COL_DURATION, _units, per_frame_stats->total_duration); + item->setData(COL_NCALLS_PER_FRAME, Qt::UserRole, per_frame_stats->calls_number); + item->setText(COL_NCALLS_PER_FRAME, QString::number(per_frame_stats->calls_number)); + } + else + { + item->setData(COL_PERCENT_SUM_PER_THREAD, Qt::UserRole, 0); + item->setData(COL_PERCENT_PER_FRAME, Qt::UserRole, 0); + } + + const auto color = easyDescriptor(child.node->id()).color(); + const auto fgColor = ::profiler_gui::textColorForRgb(color);// 0x00ffffff - bgColor; + item->setBackgroundColor(color); + item->setTextColor(fgColor); + +#ifdef EASY_TREE_WIDGET__USE_VECTOR + auto item_index = static_cast(m_items.size()); + m_items.push_back(item); +#endif + m_iditems[child.node->id()] = nullptr; + + size_t children_items_number = 0; + ::profiler::timestamp_t children_duration = 0; + if (!child.children.empty()) + { + children_items_number = setTreeInternalPlain(_threadRoot, _firstCswitch, _beginTime, child.children, _frame, _frame, _left, _right, _strict, children_duration, _colorizeRows, _addZeroBlocks, _units); + if (interrupted()) + break; + } + + m_iditems[child.node->id()] = item; + + if (child.per_frame_stats != nullptr) + { + int percentage = 100; + auto self_duration = child.per_frame_stats->total_duration - children_duration; + if (child.per_frame_stats->total_duration > 0) + percentage = ::profiler_gui::percent(self_duration, child.per_frame_stats->total_duration); + + item->setTimeSmart(COL_SELF_DURATION, _units, self_duration); + item->setData(COL_SELF_DURATION_PERCENT, Qt::UserRole, percentage); + item->setText(COL_SELF_DURATION_PERCENT, QString::number(percentage)); + + auto active_time = child.per_frame_stats->total_duration - idleTime; + auto active_percent = child.per_frame_stats->total_duration == 0 ? 100. : ::profiler_gui::percentReal(active_time, child.per_frame_stats->total_duration); + item->setTimeSmart(COL_ACTIVE_TIME, _units, active_time); + item->setText(COL_ACTIVE_PERCENT, QString::number(active_percent, 'g', 3)); + item->setData(COL_ACTIVE_PERCENT, Qt::UserRole, active_percent); + } + + if (children_items_number > 0 || !_strict || (startTime >= _left && endTime <= _right)) + { + total_items += children_items_number + 1; +#ifdef EASY_TREE_WIDGET__USE_VECTOR + gui_block.tree_item = item_index; +#endif + + if (_colorizeRows) + item->colorize(_colorizeRows); + + if (gui_block.expanded) + item->setExpanded(true); + +#ifndef EASY_TREE_WIDGET__USE_VECTOR + m_items.insert(::std::make_pair(child_index, item)); +#endif + } + else + { +#ifdef EASY_TREE_WIDGET__USE_VECTOR + m_items.pop_back(); +#endif + delete item; + m_iditems.erase(gui_block.tree.node->id()); + } + } + + return total_items; +} + +////////////////////////////////////////////////////////////////////////// diff --git a/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_loader.h b/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_loader.h new file mode 100644 index 0000000000..1c5e476b21 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/profiler_gui/tree_widget_loader.h @@ -0,0 +1,136 @@ +/************************************************************************ +* file name : tree_widget_loader.h +* ----------------- : +* creation time : 2016/08/18 +* author : Victor Zarubkin +* email : v.s.zarubkin@gmail.com +* ----------------- : +* description : The file contains declaration of EasyTreeWidgetLoader which aim is +* : to load EasyProfiler blocks hierarchy in separate thread. +* ----------------- : +* change log : * 2016/08/18 Victor Zarubkin: moved sources from blocks_tree_widget.h/.cpp +* : and renamed Prof* to Easy*. +* : +* : * +* ----------------- : +* license : Lightweight profiler library for c++ +* : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin +* : +* : Licensed under either of +* : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT) +* : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0) +* : at your option. +* : +* : The MIT License +* : +* : 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. +* : +* : The Apache License, Version 2.0 (the "License") +* : +* : You may not use this file except in compliance with the License. +* : You may obtain a copy of the License at +* : +* : http://www.apache.org/licenses/LICENSE-2.0 +* : +* : Unless required by applicable law or agreed to in writing, software +* : distributed under the License is distributed on an "AS IS" BASIS, +* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* : See the License for the specific language governing permissions and +* : limitations under the License. +************************************************************************/ + +#ifndef EASY_TREE_WIDGET_LOADER_H +#define EASY_TREE_WIDGET_LOADER_H + +#include +#include +#include +#include + +#include + +#include "common_types.h" + +////////////////////////////////////////////////////////////////////////// + +class EasyTreeWidgetItem; + +#ifndef EASY_TREE_WIDGET__USE_VECTOR +typedef ::std::unordered_map<::profiler::block_index_t, EasyTreeWidgetItem*, ::profiler::passthrough_hash<::profiler::block_index_t> > Items; +#else +typedef ::std::vector Items; +#endif + +typedef ::std::vector<::std::pair<::profiler::thread_id_t, EasyTreeWidgetItem*> > ThreadedItems; +typedef ::std::unordered_map<::profiler::thread_id_t, EasyTreeWidgetItem*, ::profiler::passthrough_hash<::profiler::thread_id_t> > RootsMap; +typedef ::std::unordered_map<::profiler::block_id_t, EasyTreeWidgetItem*, ::profiler::passthrough_hash<::profiler::block_index_t> > IdItems; + +////////////////////////////////////////////////////////////////////////// + +enum EasyTreeMode : uint8_t +{ + EasyTreeMode_Full, + EasyTreeMode_Plain +}; + +////////////////////////////////////////////////////////////////////////// + +class EasyTreeWidgetLoader Q_DECL_FINAL +{ + ThreadedItems m_topLevelItems; ///< + Items m_items; ///< + IdItems m_iditems; ///< + ::std::thread m_thread; ///< + ::std::atomic_bool m_bDone; ///< + ::std::atomic_bool m_bInterrupt; ///< + ::std::atomic m_progress; ///< + EasyTreeMode m_mode; ///< + +public: + + EasyTreeWidgetLoader(); + ~EasyTreeWidgetLoader(); + + int progress() const; + bool done() const; + + void takeTopLevelItems(ThreadedItems& _output); + void takeItems(Items& _output); + + void interrupt(bool _wait = false); + void fillTree(::profiler::timestamp_t& _beginTime, const unsigned int _blocksNumber, const ::profiler::thread_blocks_tree_t& _blocksTree, bool _colorizeRows, EasyTreeMode _mode); + void fillTreeBlocks(const::profiler_gui::TreeBlocks& _blocks, ::profiler::timestamp_t _beginTime, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, bool _colorizeRows, EasyTreeMode _mode); + +private: + + bool interrupted() const; + void setDone(); + void setProgress(int _progress); + + void setTreeInternal1(::profiler::timestamp_t& _beginTime, const unsigned int _blocksNumber, const ::profiler::thread_blocks_tree_t& _blocksTree, bool _colorizeRows, bool _addZeroBlocks, bool _decoratedThreadNames, bool _hexThreadId, ::profiler_gui::TimeUnits _units); + void setTreeInternal2(const ::profiler::timestamp_t& _beginTime, const ::profiler_gui::TreeBlocks& _blocks, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, bool _colorizeRows, bool _addZeroBlocks, bool _decoratedThreadNames, bool _hexThreadId, ::profiler_gui::TimeUnits _units); + size_t setTreeInternal(const ::profiler::BlocksTreeRoot& _threadRoot, ::profiler::block_index_t _firstCswitch, const ::profiler::timestamp_t& _beginTime, const ::profiler::BlocksTree::children_t& _children, EasyTreeWidgetItem* _parent, EasyTreeWidgetItem* _frame, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, ::profiler::timestamp_t& _duration, bool _colorizeRows, bool _addZeroBlocks, ::profiler_gui::TimeUnits _units); + size_t setTreeInternalPlain(const ::profiler::BlocksTreeRoot& _threadRoot, ::profiler::block_index_t _firstCswitch, const ::profiler::timestamp_t& _beginTime, const ::profiler::BlocksTree::children_t& _children, EasyTreeWidgetItem* _parent, EasyTreeWidgetItem* _frame, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, ::profiler::timestamp_t& _duration, bool _colorizeRows, bool _addZeroBlocks, ::profiler_gui::TimeUnits _units); + + ::profiler::timestamp_t calculateChildrenDurationRecursive(const ::profiler::BlocksTree::children_t& _children, ::profiler::block_id_t _id); + +}; // END of class EasyTreeWidgetLoader. + +////////////////////////////////////////////////////////////////////////// + +#endif // EASY_TREE_WIDGET_LOADER_H diff --git a/Source/ThirdParty/easy_profiler/reader/CMakeLists.txt b/Source/ThirdParty/easy_profiler/reader/CMakeLists.txt new file mode 100644 index 0000000000..903c910018 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/reader/CMakeLists.txt @@ -0,0 +1,3 @@ + +add_executable(profiler_reader main.cpp) +target_link_libraries(profiler_reader easy_profiler) diff --git a/Source/ThirdParty/easy_profiler/reader/main.cpp b/Source/ThirdParty/easy_profiler/reader/main.cpp new file mode 100644 index 0000000000..34a93604da --- /dev/null +++ b/Source/ThirdParty/easy_profiler/reader/main.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class TreePrinter +{ + struct Info{ + std::string name; + std::string info; + }; + std::vector m_rows; + +public: + TreePrinter(){ + + } + void addNewRow(int level) + { + + } + + void printTree() + { + for (auto& row : m_rows){ + std::cout << row.name << " " << row.info << std::endl; + } + } +}; + + +void printTree(TreePrinter& printer, const ::profiler::BlocksTree& tree, int level = 0, profiler::timestamp_t parent_dur = 0, profiler::timestamp_t root_dur = 0) +{ + // + //if (tree.node){ + // auto duration = tree.node->block()->duration(); + // float duration_ms = duration / 1e6f; + // float percent = parent_dur ? float(duration) / float(parent_dur)*100.0f : 100.0f; + // float rpercent = root_dur ? float(duration) / float(root_dur)*100.0f : 100.0f; + // std::cout << std::string(level, '\t') << tree.node->getName() + // << std::string(5 - level, '\t') + // /*<< std::string(level, ' ')*/ << percent << "%| " + // << rpercent << "%| " + // << duration_ms << " ms" + // << std::endl; + // if (root_dur == 0){ + // root_dur = tree.node->block()->duration(); + // } + //} + //else{ + // root_dur = 0; + //} + // + + //for (const auto& i : tree.children){ + + // printTree(printer, i, level + 1, tree.node ? tree.node->block()->duration() : 0, root_dur); + //} +} + +int main(int argc, char* argv[]) +{ + + ::profiler::thread_blocks_tree_t threaded_trees; + + ::std::string filename;// = "test.prof"; + if (argc > 1 && argv[1]) + { + filename = argv[1]; + } + else + { + std::cout << "Specify prof file: "; + std::getline(std::cin, filename); + //return 255; + } + + ::std::string dump_filename; + if (argc > 2 && argv[2]) + { + dump_filename = argv[2]; + } + else + { + std::cout << "Specify output prof file: "; + std::getline(std::cin, dump_filename); + } + + if (dump_filename.size() > 2) + { + EASY_PROFILER_ENABLE; + std::cout << "Will dump reader prof file to " << dump_filename << std::endl; + } + else + { + dump_filename.clear(); + } + + + auto start = std::chrono::system_clock::now(); + + ::profiler::SerializedData serialized_blocks, serialized_descriptors; + ::profiler::descriptors_list_t descriptors; + ::profiler::blocks_t blocks; + ::std::stringstream errorMessage; + uint32_t descriptorsNumberInFile = 0; + uint32_t version = 0; + auto blocks_counter = fillTreesFromFile(filename.c_str(), serialized_blocks, serialized_descriptors, descriptors, blocks, + threaded_trees, descriptorsNumberInFile, version, true, errorMessage); + if (blocks_counter == 0) + std::cout << "Can not read blocks from file " << filename.c_str() << "\nReason: " << errorMessage.str(); + + auto end = std::chrono::system_clock::now(); + + std::cout << "Blocks count: " << blocks_counter << std::endl; + std::cout << "dT = " << std::chrono::duration_cast(end - start).count() << " usec" << std::endl; + //for (const auto & i : threaded_trees){ + // TreePrinter p; + // std::cout << std::string(20, '=') << " thread "<< i.first << " "<< std::string(20, '=') << std::endl; + // printTree(p, i.second.tree,-1); + //} + + if (!dump_filename.empty()) + { + auto bcount = profiler::dumpBlocksToFile(dump_filename.c_str()); + + std::cout << "Blocks count for reader: " << bcount << std::endl; + } + + //char c; + //::std::cin >> c; + + + return 0; +} diff --git a/Source/ThirdParty/easy_profiler/sample/CMakeLists.txt b/Source/ThirdParty/easy_profiler/sample/CMakeLists.txt new file mode 100644 index 0000000000..dbc31a36f2 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/sample/CMakeLists.txt @@ -0,0 +1,16 @@ +set(CPP_FILES + main.cpp +) + +set(SOURCES + ${CPP_FILES} +) + +link_directories(${CMAKE_SOURCE_DIR}/../bin) + +add_executable(profiler_sample ${SOURCES}) +target_link_libraries(profiler_sample easy_profiler) + +add_executable(profiler_sample_disabled_profiler ${SOURCES}) +target_link_libraries(profiler_sample_disabled_profiler easy_profiler) +target_compile_definitions(profiler_sample_disabled_profiler PRIVATE DISABLE_EASY_PROFILER) diff --git a/Source/ThirdParty/easy_profiler/sample/express_sample.cpp b/Source/ThirdParty/easy_profiler/sample/express_sample.cpp new file mode 100644 index 0000000000..4212c48aa2 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/sample/express_sample.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +int OBJECTS = 500; + +void modellingThread(){ + EASY_THREAD("Modelling"); + + static const int N = OBJECTS; + + volatile double *pos[N]; + for (int i = 0; i < N; ++i) + { + pos[i] = new volatile double[3]; + } + + { + EASY_BLOCK("Collisions"); + volatile int i, j; + volatile double dist; + for (i = 0; i < N; ++i) + { + for (j = i + 1; j < N; ++j) + { + EASY_BLOCK("Check"); + volatile double v[3]; + v[0] = pos[i][0] - pos[j][0]; + v[1] = pos[i][1] - pos[j][1]; + v[2] = pos[i][2] - pos[j][2]; + dist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2]; + if (dist < 10000) + { + dist *= dist; + } + } + } + } + + for (int i = 0; i < N; ++i) + { + delete [] pos[i]; + } +} + +////////////////////////////////////////////////////////////////////////// + +int main(int argc, char* argv[]) +{ + if (argc > 1 && argv[1]){ + OBJECTS = std::atoi(argv[1]); + } + + std::cout << "Objects count: " << OBJECTS << std::endl; + + auto start = std::chrono::system_clock::now(); + + + EASY_PROFILER_ENABLE; + EASY_MAIN_THREAD; + + + modellingThread(); + + auto end = std::chrono::system_clock::now(); + auto elapsed = + std::chrono::duration_cast(end - start); + + std::cout << "Elapsed time: " << elapsed.count() << " usec" << std::endl; + + auto blocks_count = profiler::dumpBlocksToFile("test.prof"); + + std::cout << "Blocks count: " << blocks_count << std::endl; + + return 0; +} diff --git a/Source/ThirdParty/easy_profiler/sample/main.cpp b/Source/ThirdParty/easy_profiler/sample/main.cpp new file mode 100644 index 0000000000..f62573cd1b --- /dev/null +++ b/Source/ThirdParty/easy_profiler/sample/main.cpp @@ -0,0 +1,263 @@ +//#define FULL_DISABLE_PROFILER +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +std::condition_variable cv; +std::mutex cv_m; +int g_i = 0; + +int OBJECTS = 500; +int MODELLING_STEPS = 1500; +int RENDER_STEPS = 1500; +int RESOURCE_LOADING_COUNT = 50; + +//#define SAMPLE_NETWORK_TEST + +void localSleep(int magic=200000) +{ + //PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue); + volatile int i = 0; + for (; i < magic; ++i); +} + +void loadingResources(){ + EASY_FUNCTION(profiler::colors::DarkCyan); + localSleep(); +// std::this_thread::sleep_for(std::chrono::milliseconds(50)); +} + +void prepareMath(){ + EASY_FUNCTION(profiler::colors::Green); + int* intarray = new int[OBJECTS]; + for (int i = 0; i < OBJECTS; ++i) + intarray[i] = i * i; + delete[] intarray; + //std::this_thread::sleep_for(std::chrono::milliseconds(3)); +} + +void calcIntersect(){ + EASY_FUNCTION(profiler::colors::Gold); + //int* intarray = new int[OBJECTS * OBJECTS]; + int* intarray = new int[OBJECTS]; + for (int i = 0; i < OBJECTS; ++i) + { + for (int j = i; j < OBJECTS; ++j) + //intarray[i * OBJECTS + j] = i * j - i / 2 + (OBJECTS - j) * 5; + intarray[j] = i * j - i / 2 + (OBJECTS - j) * 5; + } + delete[] intarray; + //std::this_thread::sleep_for(std::chrono::milliseconds(4)); +} + +double multModel(double i) +{ + EASY_FUNCTION(profiler::colors::PaleGold); + return i * sin(i) * cos(i); +} + +void calcPhys(){ + EASY_FUNCTION(profiler::colors::Amber); + double* intarray = new double[OBJECTS]; + for (int i = 0; i < OBJECTS; ++i) + intarray[i] = multModel(double(i)) + double(i / 3) - double((OBJECTS - i) / 2); + calcIntersect(); + delete[] intarray; +} + +double calcSubbrain(int i) +{ + EASY_FUNCTION(profiler::colors::Navy); + return i * i * i - i / 10 + (OBJECTS - i) * 7 ; +} + +void calcBrain(){ + EASY_FUNCTION(profiler::colors::LightBlue); + double* intarray = new double[OBJECTS]; + for (int i = 0; i < OBJECTS; ++i) + intarray[i] = calcSubbrain(i) + double(i * 180 / 3); + delete[] intarray; + //std::this_thread::sleep_for(std::chrono::milliseconds(3)); +} + +void calculateBehavior(){ + EASY_FUNCTION(profiler::colors::Blue); + calcPhys(); + calcBrain(); +} + +void modellingStep(){ + EASY_FUNCTION(); + prepareMath(); + calculateBehavior(); +} + +void prepareRender(){ + EASY_FUNCTION(profiler::colors::Brick); + localSleep(); + //std::this_thread::sleep_for(std::chrono::milliseconds(8)); + +} + +int multPhys(int i) +{ + EASY_FUNCTION(profiler::colors::Red700, profiler::ON); + return i * i * i * i / 100; +} + +int calcPhysicForObject(int i) +{ + EASY_FUNCTION(profiler::colors::Red); + return multPhys(i) + i / 3 - (OBJECTS - i) * 15; +} + +void calculatePhysics(){ + EASY_FUNCTION(profiler::colors::Red); + unsigned int* intarray = new unsigned int[OBJECTS]; + for (int i = 0; i < OBJECTS; ++i) + intarray[i] = calcPhysicForObject(i); + delete[] intarray; + //std::this_thread::sleep_for(std::chrono::milliseconds(8)); +} + +void frame(){ + EASY_FUNCTION(profiler::colors::Magenta); + prepareRender(); + calculatePhysics(); +} + +void loadingResourcesThread(){ + //std::unique_lock lk(cv_m); + //cv.wait(lk, []{return g_i == 1; }); + EASY_THREAD("Resource loading"); +#ifdef SAMPLE_NETWORK_TEST + while (true) { +#else + for(int i = 0; i < RESOURCE_LOADING_COUNT; i++){ +#endif + loadingResources(); + EASY_EVENT("Resources Loading!", profiler::colors::Cyan); + localSleep(1200000); + //std::this_thread::sleep_for(std::chrono::milliseconds(20)); + } +} + +void modellingThread(){ + //std::unique_lock lk(cv_m); + //cv.wait(lk, []{return g_i == 1; }); + EASY_THREAD("Modelling"); +#ifdef SAMPLE_NETWORK_TEST + while (true) { +#else + for (int i = 0; i < MODELLING_STEPS; i++){ +#endif + EASY_END_BLOCK; + EASY_NONSCOPED_BLOCK("Frame"); + modellingStep(); + + localSleep(1200000); + //std::this_thread::sleep_for(std::chrono::milliseconds(20)); + } + EASY_END_BLOCK; +} + +void renderThread(){ + //std::unique_lock lk(cv_m); + //cv.wait(lk, []{return g_i == 1; }); + EASY_THREAD("Render"); +#ifdef SAMPLE_NETWORK_TEST + while (true) { +#else + for (int i = 0; i < RENDER_STEPS; i++){ +#endif + frame(); + localSleep(1200000); + //std::this_thread::sleep_for(std::chrono::milliseconds(20)); + } +} + +////////////////////////////////////////////////////////////////////////// + +int main(int argc, char* argv[]) +{ + if (argc > 1 && argv[1]){ + OBJECTS = std::atoi(argv[1]); + } + if (argc > 2 && argv[2]){ + MODELLING_STEPS = std::atoi(argv[2]); + } + if (argc > 3 && argv[3]){ + RENDER_STEPS = std::atoi(argv[3]); + } + if (argc > 4 && argv[4]){ + RESOURCE_LOADING_COUNT = std::atoi(argv[4]); + } + + std::cout << "Objects count: " << OBJECTS << std::endl; + std::cout << "Render steps: " << MODELLING_STEPS << std::endl; + std::cout << "Modelling steps: " << RENDER_STEPS << std::endl; + std::cout << "Resource loading count: " << RESOURCE_LOADING_COUNT << std::endl; + + auto start = std::chrono::system_clock::now(); + +#ifndef SAMPLE_NETWORK_TEST + EASY_PROFILER_ENABLE; +#endif + + EASY_MAIN_THREAD; + profiler::startListen(); + + std::vector threads; + //for (int i=0; i < 3; i++) + { + threads.emplace_back(loadingResourcesThread); + threads.emplace_back(renderThread); + threads.emplace_back(modellingThread); + } + + cv_m.lock(); + g_i = 1; + cv_m.unlock(); + cv.notify_all(); + +#ifndef SAMPLE_NETWORK_TEST + std::atomic_bool stop = ATOMIC_VAR_INIT(false); + auto frame_time_printer_thread = std::thread([&stop]() + { + while (!stop.load(std::memory_order_acquire)) + { + std::cout << "Frame time: max " << profiler::main_thread::frameTimeLocalMax() << " us // avg " << profiler::main_thread::frameTimeLocalAvg() << " us\n"; + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } + }); +#endif + + modellingThread(); + +#ifndef SAMPLE_NETWORK_TEST + stop.store(true, std::memory_order_release); + frame_time_printer_thread.join(); +#endif + + for(auto& t : threads) + t.join(); + + auto end = std::chrono::system_clock::now(); + auto elapsed = + std::chrono::duration_cast(end - start); + + std::cout << "Elapsed time: " << elapsed.count() << " usec" << std::endl; + + auto blocks_count = profiler::dumpBlocksToFile("test.prof"); + + std::cout << "Blocks count: " << blocks_count << std::endl; + + return 0; +} diff --git a/Source/ThirdParty/easy_profiler/sample/main_clock.cpp b/Source/ThirdParty/easy_profiler/sample/main_clock.cpp new file mode 100644 index 0000000000..95fa00877b --- /dev/null +++ b/Source/ThirdParty/easy_profiler/sample/main_clock.cpp @@ -0,0 +1,249 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +static inline uint64_t getCurrentTime() +{ +#if defined(__i386__) + int64_t ret; + __asm__ volatile("rdtsc" : "=A"(ret)); + return ret; +#elif defined(__x86_64__) || defined(__amd64__) + uint64_t low, high; + __asm__ volatile("rdtsc" : "=a"(low), "=d"(high)); + return (high << 32) | low; +#endif +} + +int OBJECTS = 500; +#define STR(x) #x + +int64_t calculate_cpu_frequency()//per sec +{ + double g_TicksPerNanoSec; + struct timespec begints, endts; + uint64_t begin = 0, end = 0; + clock_gettime(CLOCK_MONOTONIC, &begints); + begin = getCurrentTime(); + volatile uint64_t i; + for (i = 0; i < 100000000; i++); /* must be CPU intensive */ + end = getCurrentTime(); + clock_gettime(CLOCK_MONOTONIC, &endts); + struct timespec tmpts; + const int NANO_SECONDS_IN_SEC = 1000000000; + tmpts.tv_sec = endts.tv_sec - begints.tv_sec; + tmpts.tv_nsec = endts.tv_nsec - begints.tv_nsec; + if (tmpts.tv_nsec < 0) + { + tmpts.tv_sec--; + tmpts.tv_nsec += NANO_SECONDS_IN_SEC; + } + + uint64_t nsecElapsed = tmpts.tv_sec * 1000000000LL + tmpts.tv_nsec; + g_TicksPerNanoSec = (double)(end - begin) / (double)nsecElapsed; + + int64_t cpu_frequency = int(g_TicksPerNanoSec * 1000); + + return cpu_frequency; +} + +const auto CPU_FREQUENCY = calculate_cpu_frequency(); + +# define TICKS_TO_US(ticks) ticks / CPU_FREQUENCY + +void localSleep(int magic=200000) +{ + volatile int i = 0; + for (; i < magic; ++i); +} + +template +auto calcDelta(int magic=200000) -> decltype(Clock::now().time_since_epoch().count()) +{ + auto start = Clock::now().time_since_epoch().count(); + localSleep(magic); + auto end = Clock::now().time_since_epoch().count(); + return end - start; +} + +template +double calcDuration(int objects) +{ + const auto frequency = Clock::period::den / Clock::period::num; + + auto start = Clock::now(); + + decltype(Clock::now().time_since_epoch().count()) summ = 0; + + for (int i=0; i < objects; i++) + { + summ += calcDelta(); + } + + summ = summ * 1000000LL / frequency; + + auto end = Clock::now(); + auto elapsed = + std::chrono::duration_cast(end - start); + + + return (elapsed.count()-summ)/double(objects)/2.0; +} + +uint64_t calcDeltaRdtsc(int magic=200000) +{ + auto start = getCurrentTime(); + localSleep(magic); + auto end = getCurrentTime(); + return end - start; +} + +double calcDurationByRdtsc(int objects) +{ + auto start = getCurrentTime(); + + uint64_t summ = 0; + + for (int i=0; i < objects; i++) + { + summ += calcDeltaRdtsc(); + } + + auto end = getCurrentTime(); + return TICKS_TO_US((end - start - summ))/double(objects)/2.0; +} + +uint64_t calcDeltaSysCall(int magic, int type) +{ + timespec tp0,tp1; + syscall(SYS_clock_gettime, type, &tp0); + auto start = tp0.tv_sec*1000000000+tp0.tv_nsec; + localSleep(magic); + syscall(SYS_clock_gettime, type, &tp1); + auto end = tp1.tv_sec*1000000000+tp1.tv_nsec; + return end - start; +} + +double calcDurationBySyscall(int objects, int type) +{ + timespec tp0,tp1; + syscall(SYS_clock_gettime, type, &tp0); + auto start = tp0.tv_sec*1000000000+tp0.tv_nsec; + + uint64_t summ = 0; + + for (int i=0; i < objects; i++) + { + summ += calcDeltaSysCall(200000,type); + } + + syscall(SYS_clock_gettime, type, &tp1); + auto end = tp1.tv_sec*1000000000+tp1.tv_nsec; + return (end - start - summ)/double(objects)/2.0/1000.0; +} + +uint64_t calcDeltaSysGetTime(int magic, int type) +{ + timespec tp0,tp1; + clock_gettime(type, &tp0); + auto start = tp0.tv_sec*1000000000+tp0.tv_nsec; + localSleep(magic); + clock_gettime(type, &tp1); + auto end = tp1.tv_sec*1000000000+tp1.tv_nsec; + return end - start; +} + +double calcDurationByGetTime(int objects, int type) +{ + timespec tp0,tp1; + clock_gettime(type, &tp0); + auto start = tp0.tv_sec*1000000000+tp0.tv_nsec; + + uint64_t summ = 0; + + for (int i=0; i < objects; i++) + { + summ += calcDeltaSysGetTime(200000,type); + } + + clock_gettime(type, &tp1); + auto end = tp1.tv_sec*1000000000+tp1.tv_nsec; + return (end - start - summ)/double(objects)/2.0/1000.0; +} + +uint64_t calcDeltaSysGetTimeOfDay(int magic=200000) +{ + timeval tv0,tv1; + gettimeofday(&tv0,0); + auto start = tv0.tv_sec*1000000+tv0.tv_usec; + localSleep(magic); + gettimeofday(&tv1, 0); + auto end = tv1.tv_sec*1000000+tv1.tv_usec; + return end - start; +} + +double calcDurationByGetTimeOfDay(int objects) +{ + timeval tv0,tv1; + gettimeofday(&tv0,0); + auto start = tv0.tv_sec*1000000+tv0.tv_usec; + + uint64_t summ = 0; + + for (int i=0; i < objects; i++) + { + summ += calcDeltaSysGetTimeOfDay(); + } + + gettimeofday(&tv1, 0); + auto end = tv1.tv_sec*1000000+tv1.tv_usec; + return (end - start - summ)/double(objects)/2.0; +} + +int main(int argc, char* argv[]) +{ + if (argc > 1 && argv[1]){ + OBJECTS = std::atoi(argv[1]); + } + + + std::cout << STR(std::chrono::steady_clock) << ": "<(OBJECTS) << " usec\n"; + std::cout << STR(std::chrono::high_resolution_clock)<< ": " << calcDuration(OBJECTS) << " usec\n"; + std::cout << STR(std::chrono::system_clock)<< ": " << calcDuration(OBJECTS) << " usec\n"; + + std::cout << "\n"; + + std::cout << "rdtsc: " << calcDurationByRdtsc(OBJECTS) << " usec\n"; + + std::cout << "\n"; + + std::cout << "syscall(SYS_clock_gettime, CLOCK_MONOTONIC): " << calcDurationBySyscall(OBJECTS,CLOCK_MONOTONIC) << " usec\n"; + std::cout << "syscall(SYS_clock_gettime, CLOCK_REALTIME): " << calcDurationBySyscall(OBJECTS,CLOCK_REALTIME) << " usec\n"; + std::cout << "syscall(SYS_clock_gettime, CLOCK_MONOTONIC_RAW): " << calcDurationBySyscall(OBJECTS,CLOCK_MONOTONIC_RAW) << " usec\n"; + std::cout << "syscall(SYS_clock_gettime, CLOCK_MONOTONIC_COARSE): " << calcDurationBySyscall(OBJECTS,CLOCK_MONOTONIC_COARSE) << " usec\n"; + std::cout << "syscall(SYS_clock_gettime, CLOCK_REALTIME_COARSE): " << calcDurationBySyscall(OBJECTS,CLOCK_REALTIME_COARSE) << " usec\n"; + + std::cout << "\n"; + + std::cout << "clock_gettime(CLOCK_MONOTONIC): " << calcDurationByGetTime(OBJECTS,CLOCK_MONOTONIC) << " usec\n"; + std::cout << "clock_gettime(CLOCK_REALTIME): " << calcDurationByGetTime(OBJECTS,CLOCK_REALTIME) << " usec\n"; + std::cout << "clock_gettime(CLOCK_MONOTONIC_RAW): " << calcDurationByGetTime(OBJECTS,CLOCK_MONOTONIC_RAW) << " usec\n"; + std::cout << "clock_gettime(CLOCK_MONOTONIC_COARSE): " << calcDurationByGetTime(OBJECTS,CLOCK_MONOTONIC_COARSE) << " usec\n"; + std::cout << "clock_gettime(CLOCK_REALTIME_COARSE): " << calcDurationByGetTime(OBJECTS,CLOCK_REALTIME_COARSE) << " usec\n"; + + std::cout << "\n"; + + std::cout << "gettimeofday(): " << calcDurationByGetTimeOfDay(OBJECTS) << " usec\n"; + + return 0; +} diff --git a/Source/ThirdParty/easy_profiler/scripts/context_switch_logger.stp b/Source/ThirdParty/easy_profiler/scripts/context_switch_logger.stp new file mode 100644 index 0000000000..fd105375cd --- /dev/null +++ b/Source/ThirdParty/easy_profiler/scripts/context_switch_logger.stp @@ -0,0 +1,37 @@ +global target_pid +global target_name + +probe scheduler.ctxswitch { + + if (target_pid != 0 + && next_pid != target_pid + && prev_pid != target_pid) + next + + if (target_name != "" + && prev_task_name != target_name + && next_task_name != target_name) + next + + //printf("Switch from %d(%s) to %d(%s) at %d\n",prev_tid, prev_task_name,next_tid,next_task_name, gettimeofday_ns()) + printf("%d %d %d %s %d\n",gettimeofday_ns(),prev_tid, next_tid, next_task_name,next_pid ) + //printf("%d %d %d\n",gettimeofday_ns(),prev_tid, next_tid ) +} + +probe begin +{ + target_pid = 0 + target_name = "" + + %( $# == 1 || $# > 2 %? + log("Wrong number of arguments, use none, 'pid nr' or 'name proc'") + exit() + %) + + %( $# == 2 %? + if(@1 == "pid") + target_pid = strtol(@2, 10) + if(@1 == "name") + target_name = @2 + %) +} diff --git a/Source/ThirdParty/easy_profiler/scripts/test.sh b/Source/ThirdParty/easy_profiler/scripts/test.sh new file mode 100644 index 0000000000..4072207883 --- /dev/null +++ b/Source/ThirdParty/easy_profiler/scripts/test.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +unamestr=`uname` +SUBDIR="./bin" +if [[ ! "$unamestr" == 'Linux' ]]; then + SUBDIR="./bin/Release/" +fi + +DISABLED_PROF=$SUBDIR/profiler_sample_disabled_profiler +ENABLED_PROF=$SUBDIR/profiler_sample + +TEMP_FILE_ENABLE="enable.info" +TEMP_FILE_DISABLE="disable.info" +RESULT_FILE="result.csv" +RESULT_FILE_TMP="result.csv.tmp" + +HEADER="Blocks count, dT prof enabled usec, dT prof disabled usec,delta, usec/block" + +#echo "Blocks count, dT prof enabled usec, dT prof disabled usec,delta, usec/block" > $RESULT_FILE + +rm -rf $RESULT_FILE + +for i in {1..9} +do + OBJECTS_COUNT=$(($i*100)) + for j in {10..15} + do + RENDER_COUNT=$(($j*100)) + for k in {10..15} + do + MODELLING_COUNT=$(($k*100)) + $ENABLED_PROF $OBJECTS_COUNT $RENDER_COUNT $MODELLING_COUNT > $TEMP_FILE_ENABLE + $DISABLED_PROF $OBJECTS_COUNT $RENDER_COUNT $MODELLING_COUNT > $TEMP_FILE_DISABLE + DT_ENA=`cat $TEMP_FILE_ENABLE | grep Elapsed| awk '{print $3}'` + N_ENA=`cat $TEMP_FILE_ENABLE | grep Blocks| awk '{print $3}'` + N_DIS=`cat $TEMP_FILE_DISABLE | grep Elapsed| awk '{print $3}'` + + DELTA=$(($DT_ENA-$N_DIS)) + USEC_BLOCK=`awk "BEGIN{print $DELTA/$N_ENA}"` + + echo $N_ENA,$DT_ENA,$N_DIS,$DELTA,$USEC_BLOCK >> $RESULT_FILE + done + done + echo $i + +done + +cat $RESULT_FILE | sort > $RESULT_FILE_TMP + +echo $HEADER > $RESULT_FILE +cat $RESULT_FILE_TMP >> $RESULT_FILE + +rm -rf $TEMP_FILE_ENABLE +rm -rf $TEMP_FILE_DISABLE +rm -rf $RESULT_FILE_TMP + +echo "See result in $RESULT_FILE" From cef1940b0b100c592f5bcf939a314ebdb6285064 Mon Sep 17 00:00:00 2001 From: Rokas Kupstys Date: Wed, 28 Jun 2017 16:46:51 +0300 Subject: [PATCH 2/8] easy_profiler integration --- Build/CMake/Modules/AtomicCommon.cmake | 10 +- CMakeLists.txt | 8 + Script/AtomicNET/AtomicNET/Core/Profiler.cs | 28 +++ Script/AtomicNET/AtomicNETProject.json | 7 +- Script/AtomicNET/AtomicProject.json | 6 +- Script/Packages/Atomic/Core.json | 2 +- Source/Atomic/CMakeLists.txt | 2 +- Source/Atomic/Core/Context.cpp | 22 +-- Source/Atomic/Core/Object.cpp | 33 +++- Source/Atomic/Core/Object.h | 3 + Source/Atomic/Core/Profiler.cpp | 160 ++++++++++++++++++ Source/Atomic/Core/Profiler.h | 117 +++++++++++++ Source/Atomic/Core/Timer.cpp | 36 ++-- Source/Atomic/Engine/Application.cpp | 7 + Source/Atomic/Engine/Engine.cpp | 44 ++--- Source/Atomic/Engine/Engine.h | 2 - Source/Atomic/Engine/EngineDefs.h | 2 + Source/Atomic/IK/IKConverters.h | 5 + Source/Atomic/Math/StringHash.cpp | 47 +++-- Source/Atomic/Math/StringHash.h | 14 +- Source/Atomic/Physics/PhysicsWorld.cpp | 16 +- Source/Atomic/Resource/BackgroundLoader.cpp | 12 +- Source/Atomic/Resource/Resource.cpp | 12 +- Source/Atomic/UI/SystemUI/DebugHud.cpp | 10 +- .../AtomicEditor/Application/AEEditorApp.cpp | 2 + Source/AtomicNET/NETNative/NETCInterop.cpp | 17 ++ Source/CMakeLists.txt | 1 + Source/ThirdParty/CMakeLists.txt | 4 + 28 files changed, 484 insertions(+), 145 deletions(-) create mode 100644 Script/AtomicNET/AtomicNET/Core/Profiler.cs create mode 100644 Source/Atomic/Core/Profiler.cpp create mode 100644 Source/Atomic/Core/Profiler.h diff --git a/Build/CMake/Modules/AtomicCommon.cmake b/Build/CMake/Modules/AtomicCommon.cmake index 80d6693b62..f469087237 100644 --- a/Build/CMake/Modules/AtomicCommon.cmake +++ b/Build/CMake/Modules/AtomicCommon.cmake @@ -185,10 +185,16 @@ endmacro() macro(setup_executable) cmake_parse_arguments(ARG "PRIVATE;TOOL;NODEPS" "" "" ${ARGN}) check_source_files() - add_executable(${TARGET_NAME} ${ARG_UNPARSED_ARGUMENTS} ${SOURCE_FILES}) - setup_target() + if (ARG_TOOL) + if (DEFINED ATOMIC_TOOL_DIR) + set (TOOL_DIR ${ATOMIC_TOOL_DIR}) + else () + set (TOOL_DIR ${ATOMIC_SOURCE_DIR}/Artifacts/Build/${TARGET_NAME}) + endif () + set_property(TARGET ${TARGET_NAME} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${TOOL_DIR}) + endif () endmacro() # Macro for replacing substrings in every variable specified in the list. diff --git a/CMakeLists.txt b/CMakeLists.txt index 653d15dfef..50f144f5a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,14 @@ include(AtomicGit) include(AtomicUtils) include(AtomicCommon) +if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") + set (ATOMIC_RELEASE_OFF OFF) + set (ATOMIC_RELEASE_ON ON) +else () + set (ATOMIC_RELEASE_OFF ON) + set (ATOMIC_RELEASE_ON OFF) +endif () + add_definitions(-DATOMIC_ROOT_SOURCE_DIR="${ATOMIC_SOURCE_DIR}" -DATOMIC_ROOT_BUILD_DIR="${CMAKE_BINARY_DIR}") if (NOT DEFINED ATOMIC_DEV_BUILD) diff --git a/Script/AtomicNET/AtomicNET/Core/Profiler.cs b/Script/AtomicNET/AtomicNET/Core/Profiler.cs new file mode 100644 index 0000000000..2f7b149824 --- /dev/null +++ b/Script/AtomicNET/AtomicNET/Core/Profiler.cs @@ -0,0 +1,28 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace AtomicEngine +{ + public partial class Profiler : AObject + { + public static void Block(string name, Action block, uint color = 0xffffecb3, + ProfilerBlockStatus status = ProfilerBlockStatus.ON, + [CallerFilePath] string file = "", + [CallerLineNumber] int line = 0) + { +#if ATOMIC_PROFILING + var profiler = AtomicNET.Context.GetProfiler(); + if (profiler != null) + csi_Atomic_Profiler_BeginBlock(profiler, name, file, line, color, (byte)status); +#endif + block(); +#if ATOMIC_PROFILING + profiler?.EndBlock(); +#endif + } + + [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern void csi_Atomic_Profiler_BeginBlock(IntPtr self, string name, string file, int line, uint argb, byte status); + } +} diff --git a/Script/AtomicNET/AtomicNETProject.json b/Script/AtomicNET/AtomicNETProject.json index 4a6c5174c2..63d0051709 100644 --- a/Script/AtomicNET/AtomicNETProject.json +++ b/Script/AtomicNET/AtomicNETProject.json @@ -23,6 +23,7 @@ "name": "AtomicNET", "atomicNET" : true, "outputType" : "Library", + "defineConstants" : ["ATOMIC_PROFILING"], "rootNamespace" : "AtomicEngine", "assemblyName" : "AtomicNET", "assemblyOutputPath" : "..\\..\\$ATOMIC_CONFIG$\\Portable\\", @@ -44,7 +45,7 @@ "assemblyDocFile" : true, "platforms" : ["desktop"], "outputType" : "Library", - "defineConstants" : ["ATOMIC_DESKTOP"], + "defineConstants" : ["ATOMIC_DESKTOP", "ATOMIC_PROFILING"], "rootNamespace" : "AtomicGameEngine", "assemblyName" : "AtomicNET", "assemblyOutputPath" : "..\\..\\$ATOMIC_CONFIG$\\Desktop\\", @@ -87,7 +88,7 @@ "atomicNET" : true, "platforms" : ["android"], "outputType" : "Library", - "defineConstants" : ["ATOMIC_ANDROID"], + "defineConstants" : ["ATOMIC_ANDROID", "ATOMIC_PROFILING"], "rootNamespace" : "AtomicGameEngine", "assemblyName" : "AtomicNET", "assemblyOutputPath" : "..\\..\\$ATOMIC_CONFIG$\\Android\\", @@ -113,7 +114,7 @@ "platforms" : ["ios"], "outputType" : "Library", "projectTypeGuids" :[ "8FFB629D-F513-41CE-95D2-7ECE97B6EEEC", "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC" ], - "defineConstants" : ["ATOMIC_IOS"], + "defineConstants" : ["ATOMIC_IOS", "ATOMIC_PROFILING"], "rootNamespace" : "AtomicGameEngine", "assemblyName" : "AtomicNET", "assemblyOutputPath" : "..\\..\\$ATOMIC_CONFIG$\\iOS\\", diff --git a/Script/AtomicNET/AtomicProject.json b/Script/AtomicNET/AtomicProject.json index 34e67592e1..1f9e607333 100644 --- a/Script/AtomicNET/AtomicProject.json +++ b/Script/AtomicNET/AtomicProject.json @@ -35,7 +35,7 @@ "name": "$ATOMIC_PROJECT_NAME$.Desktop", "platforms" : ["desktop"], "outputType" : "Exe", - "defineConstants" : ["ATOMIC_DESKTOP"], + "defineConstants" : ["ATOMIC_DESKTOP", "ATOMIC_PROFILING"], "rootNamespace" : "", "assemblyName" : "$ATOMIC_PROJECT_NAME$", "assemblyOutputPath" : "$ATOMIC_PROJECT_ROOT$\\AtomicNET\\$ATOMIC_CONFIG$\\Bin\\Desktop", @@ -60,7 +60,7 @@ "name": "$ATOMIC_PROJECT_NAME$.Android", "platforms" : ["android"], "outputType" : "Library", - "defineConstants" : ["ATOMIC_ANDROID"], + "defineConstants" : ["ATOMIC_ANDROID", "ATOMIC_PROFILING"], "rootNamespace" : "", "assemblyName" : "$ATOMIC_PROJECT_NAME$", "projectTypeGuids" : ["EFBA0AD7-5A72-4C68-AF49-83D382785DCF", "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC"], @@ -89,7 +89,7 @@ "name": "$ATOMIC_PROJECT_NAME$.iOS", "platforms" : ["ios"], "outputType" : "Exe", - "defineConstants" : ["ATOMIC_IOS"], + "defineConstants" : ["ATOMIC_IOS", "ATOMIC_PROFILING"], "rootNamespace" : "", "assemblyName" : "$ATOMIC_PROJECT_NAME$", "projectGuid" : "071BD84E-7518-11E6-C78E-005056C00008", diff --git a/Script/Packages/Atomic/Core.json b/Script/Packages/Atomic/Core.json index ab3c5b0212..eb004aea93 100755 --- a/Script/Packages/Atomic/Core.json +++ b/Script/Packages/Atomic/Core.json @@ -12,7 +12,7 @@ "", "" ], - "classes" : ["Context", "Object", "AtomicBuildInfo", "Time"], + "classes" : ["Context", "Object", "AtomicBuildInfo", "Time", "Profiler"], "classes_rename" : { "Object" : "AObject" }, diff --git a/Source/Atomic/CMakeLists.txt b/Source/Atomic/CMakeLists.txt index 02d1b0e08c..3bd76d6037 100644 --- a/Source/Atomic/CMakeLists.txt +++ b/Source/Atomic/CMakeLists.txt @@ -177,9 +177,9 @@ if (NOT WEB) target_link_libraries (Atomic libcurl Civetweb kNet) endif() -option (ATOMIC_PROFILING "Enable profiler" ON) if (ATOMIC_PROFILING) target_compile_definitions (Atomic PUBLIC -DATOMIC_PROFILING=1) + target_link_libraries (Atomic easy_profiler) endif () option (ATOMIC_LOGGING "Enable logging" ON) diff --git a/Source/Atomic/Core/Context.cpp b/Source/Atomic/Core/Context.cpp index dfcb755e4c..1a39a8030a 100644 --- a/Source/Atomic/Core/Context.cpp +++ b/Source/Atomic/Core/Context.cpp @@ -23,7 +23,9 @@ #include "../Precompiled.h" #include "../Core/Context.h" -#include "../Core/EventProfiler.h" +// ATOMIC BEGIN +#include "../Core/Profiler.h" +// ATOMIC END #include "../IO/Log.h" #ifndef MINI_URHO @@ -446,30 +448,12 @@ void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash e void Context::BeginSendEvent(Object* sender, StringHash eventType) { -#ifdef ATOMIC_PROFILING - if (EventProfiler::IsActive()) - { - EventProfiler* eventProfiler = GetSubsystem(); - if (eventProfiler) - eventProfiler->BeginBlock(eventType); - } -#endif - eventSenders_.Push(sender); } void Context::EndSendEvent() { eventSenders_.Pop(); - -#ifdef ATOMIC_PROFILING - if (EventProfiler::IsActive()) - { - EventProfiler* eventProfiler = GetSubsystem(); - if (eventProfiler) - eventProfiler->EndBlock(); - } -#endif } } diff --git a/Source/Atomic/Core/Object.cpp b/Source/Atomic/Core/Object.cpp index 7eddf80018..a74b8bf89f 100644 --- a/Source/Atomic/Core/Object.cpp +++ b/Source/Atomic/Core/Object.cpp @@ -25,6 +25,9 @@ #include "../Core/Context.h" #include "../Core/Thread.h" #include "../IO/Log.h" +// ATOMIC BEGIN +#include "../Core/Profiler.h" +// ATOMIC END #include "../DebugNew.h" @@ -302,8 +305,34 @@ void Object::SendEvent(StringHash eventType) SendEvent(eventType, noEventData); } - +// ATOMIC BEGIN void Object::SendEvent(StringHash eventType, VariantMap& eventData) +{ +#if ATOMIC_PROFILING + bool eventProfilingEnabled = false; + if (Profiler* profiler = GetSubsystem()) + eventProfilingEnabled = profiler->GetEventProfilingEnabled(); + + if (eventProfilingEnabled) + SendEventProfiled(eventType, eventData); + else +#endif + SendEventNonProfiled(eventType, eventData); +} + +void Object::SendEventProfiled(StringHash eventType, VariantMap& eventData) +{ +#if ATOMIC_PROFILING + String eventName; + if (!StringHash::GetSignificantString(eventType, eventName)) + eventName = eventType.ToString(); + ATOMIC_PROFILE_SCOPED(eventName.CString(), PROFILER_COLOR_EVENTS); +#endif + SendEventNonProfiled(eventType, eventData); +} + +void Object::SendEventNonProfiled(StringHash eventType, VariantMap& eventData) +// ATOMIC END { if (!Thread::IsMainThread()) { @@ -402,7 +431,7 @@ void Object::SendEvent(StringHash eventType, VariantMap& eventData) context->EndSendEvent(); // ATOMIC BEGIN - context->GlobalEndSendEvent(this,eventType, eventData); + context->GlobalEndSendEvent(this, eventType, eventData); // ATOMIC END } diff --git a/Source/Atomic/Core/Object.h b/Source/Atomic/Core/Object.h index 96ffe3948a..5cd494c56c 100644 --- a/Source/Atomic/Core/Object.h +++ b/Source/Atomic/Core/Object.h @@ -222,6 +222,9 @@ class ATOMIC_API Object : public RefCounted /// Execution context. Context* context_; + void SendEventProfiled(StringHash eventType, VariantMap& eventData); + void SendEventNonProfiled(StringHash eventType, VariantMap& eventData); + private: /// Find the first event handler with no specific sender. EventHandler* FindEventHandler(StringHash eventType, EventHandler** previous = 0) const; diff --git a/Source/Atomic/Core/Profiler.cpp b/Source/Atomic/Core/Profiler.cpp new file mode 100644 index 0000000000..bdfdd09b09 --- /dev/null +++ b/Source/Atomic/Core/Profiler.cpp @@ -0,0 +1,160 @@ +// +// Copyright (c) 2017 the Atomic project. +// +// 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. +// + +#include "../Precompiled.h" +#include "../Core/Profiler.h" +#include "../Core/StringUtils.h" + +namespace Atomic +{ + +Profiler::Profiler(Context* context) + : Object(context) +{ + SetEnabled(true); +#if !ATOMIC_PROFILING + enableEventProfiling_ = false; +#endif +} + +Profiler::~Profiler() +{ +} + +void Profiler::SetEnabled(bool enabled) +{ +#if ATOMIC_PROFILING + ::profiler::setEnabled(enabled); +#endif +} + +bool Profiler::GetEnabled() const +{ +#if ATOMIC_PROFILING + return ::profiler::isEnabled(); +#else + return false; +#endif +} + +void Profiler::StartListen(unsigned short port) +{ +#if ATOMIC_PROFILING + ::profiler::startListen(port); +#endif +} + +void Profiler::StopListen() +{ +#if ATOMIC_PROFILING + ::profiler::stopListen(); +#endif +} + +bool Profiler::GetListening() const +{ +#if ATOMIC_PROFILING + return ::profiler::isListening(); +#else + return false; +#endif +} + +void Profiler::SetEventTracingEnabled(bool enable) +{ +#if ATOMIC_PROFILING + ::profiler::setEventTracingEnabled(enable); +#endif +} + +bool Profiler::GetEventTracingEnabled() +{ +#if ATOMIC_PROFILING + return ::profiler::isEventTracingEnabled(); +#else + return false; +#endif +} + +void Profiler::SetLowPriorityEventTracing(bool isLowPriority) +{ +#if ATOMIC_PROFILING + ::profiler::setLowPriorityEventTracing(isLowPriority); +#endif +} + +bool Profiler::GetLowPriorityEventTracing() +{ +#if ATOMIC_PROFILING + return ::profiler::isLowPriorityEventTracing(); +#else + return false; +#endif +} + +void Profiler::SaveProfilerData(const String& filePath) +{ +#if ATOMIC_PROFILING + ::profiler::dumpBlocksToFile(filePath.CString()); +#endif +} + +void Profiler::SetEventProfilingEnabled(bool enabled) +{ +#if ATOMIC_PROFILING + enableEventProfiling_ = enabled; +#endif +} + +bool Profiler::GetEventProfilingEnabled() const +{ + return enableEventProfiling_; +} + +void Profiler::BeginBlock(const char* name, const char* file, int line, unsigned int color, unsigned char status) +{ +#if ATOMIC_PROFILING + // Line used as starting hash value for efficiency. + // This is likely to not play well with hot code reload. + unsigned hash = StringHash::Calculate(file, (unsigned)line); + HashMap::Iterator it = blockDescriptorCache_.Find(hash); + const ::profiler::BaseBlockDescriptor* desc = 0; + if (it == blockDescriptorCache_.End()) + { + String uniqueName = ToString("%s:%d", file, line); + desc = ::profiler::registerDescription((::profiler::EasyBlockStatus)status, uniqueName.CString(), name, file, + line, ::profiler::BLOCK_TYPE_BLOCK, color, true); + } + else + desc = it->second_; + ::profiler::beginNonScopedBlock(desc, name); +#endif +} + +void Profiler::EndBlock() +{ +#if ATOMIC_PROFILING + ::profiler::endBlock(); +#endif +} + +} diff --git a/Source/Atomic/Core/Profiler.h b/Source/Atomic/Core/Profiler.h new file mode 100644 index 0000000000..f465b71c3c --- /dev/null +++ b/Source/Atomic/Core/Profiler.h @@ -0,0 +1,117 @@ +// +// Copyright (c) 2017 the Atomic project. +// +// 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. +// + +#pragma once + +#include "../Container/Str.h" +#include "../Core/Thread.h" +#include "../Core/Timer.h" + +#if ATOMIC_PROFILING +# include +#else +namespace profiler { class BaseBlockDescriptor {}; }; +#endif + +namespace Atomic +{ + +static const int PROFILER_DEFAULT_PORT = 28077; +static const uint32_t PROFILER_COLOR_DEFAULT = 0xffffecb3; +static const uint32_t PROFILER_COLOR_EVENTS = 0xffff9800; +static const uint32_t PROFILER_COLOR_RESOURCES = 0xff00bcd4; + +// Copied from easy_profiler +enum ProfilerBlockStatus +{ + OFF = 0, + ON = 1, + FORCE_ON = ON | 2, + OFF_RECURSIVE = 4, + ON_WITHOUT_CHILDREN = ON | OFF_RECURSIVE, + FORCE_ON_WITHOUT_CHILDREN = FORCE_ON | OFF_RECURSIVE, +}; + + +/// Hierarchical performance profiler subsystem. +class ATOMIC_API Profiler : public Object +{ + ATOMIC_OBJECT(Profiler, Object); + +public: + /// Construct. + Profiler(Context* context); + /// Destruct. + virtual ~Profiler(); + + /// Enables or disables profiler. + void SetEnabled(bool enabled); + /// Returns true if profiler is enabled, false otherwise. + bool GetEnabled() const; + /// Enables or disables event profiling. + void SetEventProfilingEnabled(bool enabled); + /// Returns true if event profiling is enabled, false otherwise. + bool GetEventProfilingEnabled() const; + /// Starts listening for incoming profiler tool connections. + void StartListen(unsigned short port=PROFILER_DEFAULT_PORT); + /// Stops listening for incoming profiler tool connections. + void StopListen(); + /// Returns true if profiler is currently listening for incoming connections. + bool GetListening() const; + /// Enables or disables event tracing. This is windows-specific, does nothing on other OS. + void SetEventTracingEnabled(bool enable); + /// Returns true if event tracing is enabled, false otherwise. + bool GetEventTracingEnabled(); + /// Enables or disables low priority event tracing. This is windows-specific, does nothing on other OS. + void SetLowPriorityEventTracing(bool isLowPriority); + /// Returns true if low priority event tracing is enabled, false otherwise. + bool GetLowPriorityEventTracing(); + /// Save profiler data to a file. + void SaveProfilerData(const String& filePath); + /// Begin non-scoped profiled block. Block has to be terminated with call to EndBlock(). This is slow and is for + /// integration with scripting lnaguages. Use ATOMIC_PROFILE* macros when writing c++ code instead. + void BeginBlock(const char* name, const char* file, int line, unsigned int argb=PROFILER_COLOR_DEFAULT, + unsigned char status=ProfilerBlockStatus::ON); + /// End block started with BeginBlock(). + void EndBlock(); + +private: + + bool enableEventProfiling_ = true; + HashMap blockDescriptorCache_; +}; + +#if ATOMIC_PROFILING +# define ATOMIC_PROFILE(name, ...) EASY_BLOCK(#name, __VA_ARGS__) +# define ATOMIC_PROFILE_SCOPED(name, ...) EASY_BLOCK(name, __VA_ARGS__) +# define ATOMIC_PROFILE_NONSCOPED(name, ...) EASY_NONSCOPED_BLOCK(name, __VA_ARGS__) +# define ATOMIC_PROFILE_END(...) EASY_END_BLOCK +# define ATOMIC_PROFILE_THREAD(name) EASY_THREAD(name) +#else +# define ATOMIC_PROFILE(name, ...) +# define ATOMIC_PROFILE_NONSCOPED(name, ...) +# define ATOMIC_PROFILE_SCOPED(name, ...) +# define ATOMIC_PROFILE_END(...) +# define ATOMIC_PROFILE_THREAD(name) +#endif + +} diff --git a/Source/Atomic/Core/Timer.cpp b/Source/Atomic/Core/Timer.cpp index a9bb1220c9..cc88fc3a21 100644 --- a/Source/Atomic/Core/Timer.cpp +++ b/Source/Atomic/Core/Timer.cpp @@ -110,36 +110,24 @@ void Time::BeginFrame(float timeStep) timeStep_ = timeStep; - Profiler* profiler = GetSubsystem(); - if (profiler) - profiler->BeginFrame(); +// ATOMIC BEGIN + ATOMIC_PROFILE(BeginFrame); + // Frame begin event + using namespace BeginFrame; - { - ATOMIC_PROFILE(BeginFrame); - - // Frame begin event - using namespace BeginFrame; - - VariantMap& eventData = GetEventDataMap(); - eventData[P_FRAMENUMBER] = frameNumber_; - eventData[P_TIMESTEP] = timeStep_; - SendEvent(E_BEGINFRAME, eventData); - } + VariantMap& eventData = GetEventDataMap(); + eventData[P_FRAMENUMBER] = frameNumber_; + eventData[P_TIMESTEP] = timeStep_; + SendEvent(E_BEGINFRAME, eventData); } void Time::EndFrame() { - { - ATOMIC_PROFILE(EndFrame); - - // Frame end event - SendEvent(E_ENDFRAME); - } - - Profiler* profiler = GetSubsystem(); - if (profiler) - profiler->EndFrame(); + ATOMIC_PROFILE(EndFrame); + // Frame end event + SendEvent(E_ENDFRAME); } +// ATOMIC END void Time::SetTimerPeriod(unsigned mSec) { diff --git a/Source/Atomic/Engine/Application.cpp b/Source/Atomic/Engine/Application.cpp index 0291265b05..a4de5c03fa 100644 --- a/Source/Atomic/Engine/Application.cpp +++ b/Source/Atomic/Engine/Application.cpp @@ -25,6 +25,9 @@ #include "../Engine/Application.h" #include "../IO/IOEvents.h" #include "../IO/Log.h" +// ATOMIC BEGIN +#include "../Core/Profiler.h" +// ATOMIC END #ifdef IOS #include "../Graphics/Graphics.h" @@ -87,6 +90,10 @@ Application::Application(Context* context) : int Application::Run() { + // ATOMIC BEGIN + // Profiler requires main thread to be named "Main" as fps calculations depend on it. + ATOMIC_PROFILE_THREAD("Main"); + // ATOMIC END #if !defined(__GNUC__) || __EXCEPTIONS try { diff --git a/Source/Atomic/Engine/Engine.cpp b/Source/Atomic/Engine/Engine.cpp index 55d043000d..abd8e4cda4 100644 --- a/Source/Atomic/Engine/Engine.cpp +++ b/Source/Atomic/Engine/Engine.cpp @@ -25,7 +25,10 @@ #include "../Audio/Audio.h" #include "../Core/Context.h" #include "../Core/CoreEvents.h" -#include "../Core/EventProfiler.h" +// ATOMIC BEGIN +#include "../Core/Profiler.h" +#include "../Engine/EngineDefs.h" +// ATOMIC END #include "../Core/ProcessUtils.h" #include "../Core/WorkQueue.h" #include "../Engine/Engine.h" @@ -370,11 +373,14 @@ bool Engine::Initialize(const VariantMap& parameters) #endif #ifdef ATOMIC_PROFILING - if (GetParameter(parameters, EP_EVENT_PROFILER, true).GetBool()) + // ATOMIC BEGIN + if (Profiler* profiler = GetSubsystem()) { - context_->RegisterSubsystem(new EventProfiler(context_)); - EventProfiler::SetActive(true); + if (GetParameter(parameters, EP_PROFILER_LISTEN, false).GetBool()) + profiler->StartListen((unsigned short)GetParameter(parameters, EP_PROFILER_PORT, PROFILER_DEFAULT_PORT).GetInt()); + profiler->SetEventProfilingEnabled(GetParameter(parameters, EP_EVENT_PROFILER, true).GetBool()); } + // ATOMIC END #endif // ATOMIC BEGIN @@ -553,8 +559,10 @@ bool Engine::InitializeResourceCache(const VariantMap& parameters, bool removeOl return true; } +// ATOMIC BEGIN void Engine::RunFrame() { + ATOMIC_PROFILE(RunFrame); assert(initialized_); // If not headless, and the graphics subsystem no longer has a window open, assume we should exit @@ -570,18 +578,8 @@ void Engine::RunFrame() Input* input = GetSubsystem(); Audio* audio = GetSubsystem