From bb85ec442dd7b5b0966d73bc6b868d33bb03e5e9 Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Fri, 31 Jan 2020 17:06:37 +0200 Subject: [core] Add hooks for setting experimental thread priorities for mbgl threads --- include/mbgl/util/thread.hpp | 38 +++++++++++++--------- next/CMakeLists.txt | 1 + platform/android/src/asset_manager_file_source.cpp | 11 ++++--- .../default/src/mbgl/storage/asset_file_source.cpp | 5 +-- .../src/mbgl/storage/database_file_source.cpp | 7 +++- .../default/src/mbgl/storage/local_file_source.cpp | 5 +-- .../src/mbgl/storage/main_resource_loader.cpp | 8 ++++- .../src/mbgl/storage/online_file_source.cpp | 11 ++++--- src/core-files.json | 3 ++ src/mbgl/util/thread.cpp | 19 +++++++++++ src/mbgl/util/thread_pool.cpp | 11 +++++-- 11 files changed, 88 insertions(+), 31 deletions(-) create mode 100644 src/mbgl/util/thread.cpp diff --git a/include/mbgl/util/thread.hpp b/include/mbgl/util/thread.hpp index ab0403e44e..4bc948fdbd 100644 --- a/include/mbgl/util/thread.hpp +++ b/include/mbgl/util/thread.hpp @@ -3,10 +3,10 @@ #include #include #include +#include #include #include #include -#include #include #include @@ -37,25 +37,20 @@ namespace util { // - A `RunLoop` is created for the `Object` thread. // - `Object` can use `Timer` and do asynchronous I/O, like wait for sockets events. // -template +template class Thread { public: - template - Thread(const std::string& name, Args&&... args) { - + template + Thread(std::function prioritySetter_, const std::string& name, TupleArgs&& args) { std::promise running_; running = running_.get_future(); - - auto capturedArgs = std::make_tuple(std::forward(args)...); - - thread = std::thread([ - this, - name, - capturedArgs = std::move(capturedArgs), - runningPromise = std::move(running_) - ] () mutable { + thread = std::thread([this, + name, + capturedArgs = std::forward(args), + runningPromise = std::move(running_), + prioritySetter = std::move(prioritySetter_)]() mutable { platform::setCurrentThreadName(name); - platform::makeThreadLowPriority(); + if (prioritySetter) prioritySetter(); platform::attachThread(); // narrowing the scope to release the Object before we detach the thread @@ -77,6 +72,14 @@ public: }); } + template + Thread(const std::string& name, Args&&... args) + : Thread([] { platform::makeThreadLowPriority(); }, name, std::make_tuple(std::forward(args)...)) {} + + template + Thread(std::function prioritySetter, const std::string& name, Args&&... args) + : Thread(std::move(prioritySetter), name, std::make_tuple(std::forward(args)...)) {} + ~Thread() { if (paused) { resume(); @@ -158,5 +161,10 @@ private: util::RunLoop* loop = nullptr; }; +// Returns function, that once invoked, will set a thread priority for +// a thread `threadType` based on a setting provided by corresponding +// Settings' platform::EXPERIMENTAL_THREAD_PRIORITY_* value. +std::function makeThreadPrioritySetter(std::string threadType); + } // namespace util } // namespace mbgl diff --git a/next/CMakeLists.txt b/next/CMakeLists.txt index e956f3e5db..50b1f62ccc 100644 --- a/next/CMakeLists.txt +++ b/next/CMakeLists.txt @@ -780,6 +780,7 @@ add_library( ${MBGL_ROOT}/src/mbgl/util/stopwatch.cpp ${MBGL_ROOT}/src/mbgl/util/stopwatch.hpp ${MBGL_ROOT}/src/mbgl/util/string.cpp + ${MBGL_ROOT}/src/mbgl/util/thread.cpp ${MBGL_ROOT}/src/mbgl/util/thread_local.hpp ${MBGL_ROOT}/src/mbgl/util/thread_pool.cpp ${MBGL_ROOT}/src/mbgl/util/thread_pool.hpp diff --git a/platform/android/src/asset_manager_file_source.cpp b/platform/android/src/asset_manager_file_source.cpp index 73ecec2b05..a8045a2b96 100644 --- a/platform/android/src/asset_manager_file_source.cpp +++ b/platform/android/src/asset_manager_file_source.cpp @@ -1,5 +1,6 @@ #include "asset_manager_file_source.hpp" +#include #include #include #include @@ -39,11 +40,13 @@ private: AAssetManager* assetManager; }; -AssetManagerFileSource::AssetManagerFileSource(jni::JNIEnv& env, const jni::Object& assetManager_) +AssetManagerFileSource::AssetManagerFileSource(jni::JNIEnv& env, + const jni::Object& assetManager_) : assetManager(jni::NewGlobal(env, assetManager_)), - impl(std::make_unique>("AssetManagerFileSource", - AAssetManager_fromJava(&env, jni::Unwrap(assetManager.get())))) { -} + impl(std::make_unique>( + util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_FILE), + "AssetManagerFileSource", + AAssetManager_fromJava(&env, jni::Unwrap(assetManager.get())))) {} AssetManagerFileSource::~AssetManagerFileSource() = default; diff --git a/platform/default/src/mbgl/storage/asset_file_source.cpp b/platform/default/src/mbgl/storage/asset_file_source.cpp index 7abd609b19..a9ede06508 100644 --- a/platform/default/src/mbgl/storage/asset_file_source.cpp +++ b/platform/default/src/mbgl/storage/asset_file_source.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -42,8 +43,8 @@ private: }; AssetFileSource::AssetFileSource(const std::string& root) - : impl(std::make_unique>("AssetFileSource", root)) { -} + : impl(std::make_unique>( + util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_FILE), "AssetFileSource", root)) {} AssetFileSource::~AssetFileSource() = default; diff --git a/platform/default/src/mbgl/storage/database_file_source.cpp b/platform/default/src/mbgl/storage/database_file_source.cpp index b46693849a..f0b4849cb0 100644 --- a/platform/default/src/mbgl/storage/database_file_source.cpp +++ b/platform/default/src/mbgl/storage/database_file_source.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,6 +8,7 @@ #include #include #include +#include #include namespace mbgl { @@ -149,7 +151,10 @@ class DatabaseFileSource::Impl { public: Impl(std::shared_ptr onlineFileSource, const std::string& cachePath) : thread(std::make_unique>( - "DatabaseFileSource", std::move(onlineFileSource), cachePath)) {} + util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_DATABASE), + "DatabaseFileSource", + std::move(onlineFileSource), + cachePath)) {} ActorRef actor() const { return thread->actor(); } diff --git a/platform/default/src/mbgl/storage/local_file_source.cpp b/platform/default/src/mbgl/storage/local_file_source.cpp index 54f12baf79..5009146c8e 100644 --- a/platform/default/src/mbgl/storage/local_file_source.cpp +++ b/platform/default/src/mbgl/storage/local_file_source.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -36,8 +37,8 @@ public: }; LocalFileSource::LocalFileSource() - : impl(std::make_unique>("LocalFileSource")) { -} + : impl(std::make_unique>( + util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_FILE), "LocalFileSource")) {} LocalFileSource::~LocalFileSource() = default; diff --git a/platform/default/src/mbgl/storage/main_resource_loader.cpp b/platform/default/src/mbgl/storage/main_resource_loader.cpp index 91ae0a1d71..fb69a8c4f4 100644 --- a/platform/default/src/mbgl/storage/main_resource_loader.cpp +++ b/platform/default/src/mbgl/storage/main_resource_loader.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -131,7 +132,12 @@ public: onlineFileSource(std::move(onlineFileSource_)), supportsCacheOnlyRequests_(bool(databaseFileSource)), thread(std::make_unique>( - "ResourceLoaderThread", assetFileSource, databaseFileSource, localFileSource, onlineFileSource)) {} + util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_WORKER), + "ResourceLoaderThread", + assetFileSource, + databaseFileSource, + localFileSource, + onlineFileSource)) {} std::unique_ptr request(const Resource& resource, Callback callback) { auto req = std::make_unique(std::move(callback)); diff --git a/platform/default/src/mbgl/storage/online_file_source.cpp b/platform/default/src/mbgl/storage/online_file_source.cpp index 0f5b438e1d..37b1f7ca8e 100644 --- a/platform/default/src/mbgl/storage/online_file_source.cpp +++ b/platform/default/src/mbgl/storage/online_file_source.cpp @@ -1,8 +1,8 @@ -#include +#include +#include #include #include - -#include +#include #include #include #include @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -293,7 +294,9 @@ private: class OnlineFileSource::Impl { public: - Impl() : thread(std::make_unique>("OnlineFileSource")) {} + Impl() + : thread(std::make_unique>( + util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_NETWORK), "OnlineFileSource")) {} std::unique_ptr request(Callback callback, Resource res) { auto req = std::make_unique(std::move(callback)); diff --git a/src/core-files.json b/src/core-files.json index 3c0e128b18..fe68df16bf 100644 --- a/src/core-files.json +++ b/src/core-files.json @@ -62,6 +62,7 @@ "src/mbgl/map/transform_state.cpp", "src/mbgl/math/log2.cpp", "src/mbgl/platform/gl_functions.cpp", + "src/mbgl/platform/settings.cpp", "src/mbgl/programs/background_program.cpp", "src/mbgl/programs/circle_program.cpp", "src/mbgl/programs/clipping_mask_program.cpp", @@ -316,6 +317,7 @@ "src/mbgl/util/rapidjson.cpp", "src/mbgl/util/stopwatch.cpp", "src/mbgl/util/string.cpp", + "src/mbgl/util/thread.cpp", "src/mbgl/util/thread_pool.cpp", "src/mbgl/util/tile_cover.cpp", "src/mbgl/util/tile_cover_impl.cpp", @@ -369,6 +371,7 @@ "mbgl/math/minmax.hpp": "include/mbgl/math/minmax.hpp", "mbgl/math/wrap.hpp": "include/mbgl/math/wrap.hpp", "mbgl/platform/gl_functions.hpp": "include/mbgl/platform/gl_functions.hpp", + "mbgl/platform/settings.hpp": "include/mbgl/platform/settings.hpp", "mbgl/platform/thread.hpp": "include/mbgl/platform/thread.hpp", "mbgl/renderer/query.hpp": "include/mbgl/renderer/query.hpp", "mbgl/renderer/renderer.hpp": "include/mbgl/renderer/renderer.hpp", diff --git a/src/mbgl/util/thread.cpp b/src/mbgl/util/thread.cpp new file mode 100644 index 0000000000..016d23a928 --- /dev/null +++ b/src/mbgl/util/thread.cpp @@ -0,0 +1,19 @@ +#include +#include +#include + +namespace mbgl { +namespace util { +std::function makeThreadPrioritySetter(std::string threadType_) { + return [threadType = std::move(threadType_)] { + auto& settings = platform::Settings::getInstance(); + auto value = settings.get(threadType); + if (auto* priority = value.getDouble()) { + platform::setCurrentThreadPriority(*priority); + } else { + platform::makeThreadLowPriority(); + } + }; +} +} // namespace util +} // namespace mbgl diff --git a/src/mbgl/util/thread_pool.cpp b/src/mbgl/util/thread_pool.cpp index 040e996dd4..e1c18a45b5 100644 --- a/src/mbgl/util/thread_pool.cpp +++ b/src/mbgl/util/thread_pool.cpp @@ -1,8 +1,9 @@ #include +#include +#include #include #include -#include namespace mbgl { @@ -17,7 +18,13 @@ void ThreadedSchedulerBase::terminate() { } std::thread ThreadedSchedulerBase::makeSchedulerThread(size_t index) { - return std::thread([this, index]() { + return std::thread([this, index] { + auto& settings = platform::Settings::getInstance(); + auto value = settings.get(platform::EXPERIMENTAL_THREAD_PRIORITY_WORKER); + if (auto* priority = value.getDouble()) { + platform::setCurrentThreadPriority(*priority); + } + platform::setCurrentThreadName(std::string{"Worker "} + util::toString(index + 1)); platform::attachThread(); -- cgit v1.2.1