From 93b07dfac9c42668eb2930fab9d357750a445d04 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Sun, 12 Jan 2020 09:55:44 +0100 Subject: [PATCH] Added some logging facility for callback and file loading times --- cmake/SfizzConfig.cmake | 2 +- src/CMakeLists.txt | 1 + src/sfizz/Config.h | 1 + src/sfizz/Logger.cpp | 41 +++++++++++++++++++++++++++ src/sfizz/Logger.h | 62 +++++++++++++++++++++++++++++++++++++++++ src/sfizz/Resources.h | 2 ++ 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/sfizz/Logger.cpp create mode 100644 src/sfizz/Logger.h diff --git a/cmake/SfizzConfig.cmake b/cmake/SfizzConfig.cmake index 8fa9a40c..01271759 100644 --- a/cmake/SfizzConfig.cmake +++ b/cmake/SfizzConfig.cmake @@ -16,7 +16,7 @@ if (UNIX) add_compile_options(-ffast-math) add_compile_options(-fno-omit-frame-pointer) # For debugging purposes add_compile_options(-fPIC) - endif() +endif() if (WIN32) find_package(LibSndFile REQUIRED) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6fd32a59..c442f9df 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ set (SFIZZ_SOURCES sfizz/SfzHelpers.cpp sfizz/Oversampler.cpp sfizz/FloatEnvelopes.cpp + sfizz/Logger.cpp ) include (SfizzSIMDSourceFilesCheck) diff --git a/src/sfizz/Config.h b/src/sfizz/Config.h index 8f69a244..204539ec 100644 --- a/src/sfizz/Config.h +++ b/src/sfizz/Config.h @@ -42,6 +42,7 @@ namespace config { constexpr int defaultSamplesPerBlock { 1024 }; constexpr int maxBlockSize { 8192 }; constexpr int preloadSize { 8192 }; + constexpr int loggerQueueSize { 16 }; constexpr size_t numChannels { 2 }; constexpr int numBackgroundThreads { 4 }; constexpr int numVoices { 64 }; diff --git a/src/sfizz/Logger.cpp b/src/sfizz/Logger.cpp new file mode 100644 index 00000000..5214bc5a --- /dev/null +++ b/src/sfizz/Logger.cpp @@ -0,0 +1,41 @@ +#include "Logger.h" +using namespace std::chrono_literals; +sfz::Logger::~Logger() +{ + keepRunning.clear(); + loggingThread.join(); +} + + +void sfz::Logger::logCallbackTime(std::chrono::duration value) +{ + callbackTimeQueue.try_push(value); +} +void sfz::Logger::logFileWaitTime(std::chrono::duration value) +{ + fileWaitTimeQueue.try_push(value); +} +void sfz::Logger::logFileLoadTime(std::chrono::duration value, uint32_t fileSize, absl::string_view filename) +{ + FileLoadTime toPush { value, fileSize, filename }; + fileLoadTimeQueue.try_push(toPush); +} + +void sfz::Logger::moveEvents() +{ + while(keepRunning.test_and_set()) { + std::chrono::duration callbackTime; + while (callbackTimeQueue.try_pop(callbackTime)) + callbackTimes.push_back(callbackTime); + + std::chrono::duration waitTime; + while (fileWaitTimeQueue.try_pop(waitTime)) + waitTimes.push_back(waitTime); + + sfz::FileLoadTime loadTime; + while (fileLoadTimeQueue.try_pop(loadTime)) + loadTimes.push_back(loadTime); + + std::this_thread::sleep_for(50ms); + } +} diff --git a/src/sfizz/Logger.h b/src/sfizz/Logger.h new file mode 100644 index 00000000..2fe6cc31 --- /dev/null +++ b/src/sfizz/Logger.h @@ -0,0 +1,62 @@ +// Copyright (c) 2019-2020, Paul Ferrand +// All rights reserved. + +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: + +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. + +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#pragma once +#include "Config.h" +#include "atomic_queue/atomic_queue.h" +#include +#include +#include +#include "absl/strings/string_view.h" + +namespace sfz +{ + +struct FileLoadTime +{ + std::chrono::duration value; + uint32_t fileSize; + absl::string_view filename; +}; + +class Logger +{ +public: + Logger() = default; + ~Logger(); + void logCallbackTime(std::chrono::duration value); + void logFileWaitTime(std::chrono::duration value); + void logFileLoadTime(std::chrono::duration value, uint32_t fileSize, absl::string_view filename); +private: + void moveEvents(); + atomic_queue::AtomicQueue2, config::loggerQueueSize, true, true, false, true> callbackTimeQueue; + atomic_queue::AtomicQueue2 fileLoadTimeQueue; + atomic_queue::AtomicQueue2, config::loggerQueueSize, true, true, false, true> fileWaitTimeQueue; + std::vector> callbackTimes; + std::vector loadTimes; + std::vector> waitTimes; + std::atomic_flag keepRunning; + std::thread loggingThread { &Logger::moveEvents, this }; +}; + +} diff --git a/src/sfizz/Resources.h b/src/sfizz/Resources.h index 3fb24a5f..b6813e8e 100644 --- a/src/sfizz/Resources.h +++ b/src/sfizz/Resources.h @@ -1,10 +1,12 @@ #pragma once #include "FilePool.h" +#include "Logger.h" namespace sfz { struct Resources { FilePool filePool; + Logger logger; }; }