From b16ee5b35a8c99e4493df5a9d79265114a464739 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Tue, 18 Aug 2020 10:07:38 +0200 Subject: [PATCH] Adapt to changes in Ghoul regarding string_view Fix compile error if Trace logging is not enabled Limit the number of threads to a maximum of 4 for asset initialization --- include/openspace/util/screenlog.h | 4 +- .../rendering/renderableatmosphere.cpp | 43 ++++++++++--------- modules/server/src/connection.cpp | 4 ++ modules/server/src/topics/timetopic.cpp | 6 ++- modules/server/src/topics/topic.cpp | 5 +++ src/engine/openspaceengine.cpp | 11 ++--- src/rendering/renderengine.cpp | 4 +- src/scene/asset.cpp | 6 +++ src/scene/assetloader.cpp | 21 ++++++++- src/scene/assetmanager.cpp | 2 + src/scene/profile.cpp | 8 +++- src/util/screenlog.cpp | 12 +++--- src/util/synchronizationwatcher.cpp | 5 ++- src/util/timemanager.cpp | 6 +++ 14 files changed, 97 insertions(+), 40 deletions(-) diff --git a/include/openspace/util/screenlog.h b/include/openspace/util/screenlog.h index c6ded67ff1..8c496ff57f 100644 --- a/include/openspace/util/screenlog.h +++ b/include/openspace/util/screenlog.h @@ -91,8 +91,8 @@ public: * \param category The category of the log message * \param message The actual log message that was transmitted */ - void log(ghoul::logging::LogLevel level, const std::string& category, - const std::string& message) override; + void log(ghoul::logging::LogLevel level, std::string_view category, + std::string_view message) override; /** * This method removes all the stored LogEntry%s that have expired, calculated by diff --git a/modules/atmosphere/rendering/renderableatmosphere.cpp b/modules/atmosphere/rendering/renderableatmosphere.cpp index 671cc2c3c9..7b31587b74 100644 --- a/modules/atmosphere/rendering/renderableatmosphere.cpp +++ b/modules/atmosphere/rendering/renderableatmosphere.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -55,29 +56,29 @@ namespace { static const char* _loggerCat = "RenderableAtmosphere"; - const char* KeyShadowGroup = "ShadowGroup"; - const char* KeyShadowSource = "Source"; - const char* KeyShadowCaster = "Caster"; + constexpr const char* KeyShadowGroup = "ShadowGroup"; + constexpr const char* KeyShadowSource = "Source"; + constexpr const char* KeyShadowCaster = "Caster"; - const char* keyAtmosphere = "Atmosphere"; - const char* keyAtmosphereRadius = "AtmosphereRadius"; - const char* keyPlanetRadius = "PlanetRadius"; - const char* keyAverageGroundReflectance = "PlanetAverageGroundReflectance"; - const char* keyRayleigh = "Rayleigh"; - const char* keyRayleighHeightScale = "H_R"; - const char* keyOzone = "Ozone"; - const char* keyOzoneHeightScale = "H_O"; - const char* keyMie = "Mie"; - const char* keyMieHeightScale = "H_M"; - const char* keyMiePhaseConstant = "G"; - const char* keyImage = "Image"; - const char* keyToneMappingOp = "ToneMapping"; - const char* keyATMDebug = "Debug"; - const char* keyTextureScale = "PreCalculatedTextureScale"; - const char* keySaveTextures = "SaveCalculatedTextures"; + constexpr const char* keyAtmosphere = "Atmosphere"; + constexpr const char* keyAtmosphereRadius = "AtmosphereRadius"; + constexpr const char* keyPlanetRadius = "PlanetRadius"; + constexpr const char* keyAverageGroundReflectance = "PlanetAverageGroundReflectance"; + constexpr const char* keyRayleigh = "Rayleigh"; + constexpr const char* keyRayleighHeightScale = "H_R"; + constexpr const char* keyOzone = "Ozone"; + constexpr const char* keyOzoneHeightScale = "H_O"; + constexpr const char* keyMie = "Mie"; + constexpr const char* keyMieHeightScale = "H_M"; + constexpr const char* keyMiePhaseConstant = "G"; + constexpr const char* keyImage = "Image"; + constexpr const char* keyToneMappingOp = "ToneMapping"; + constexpr const char* keyATMDebug = "Debug"; + constexpr const char* keyTextureScale = "PreCalculatedTextureScale"; + constexpr const char* keySaveTextures = "SaveCalculatedTextures"; constexpr openspace::properties::Property::PropertyInfo AtmosphereHeightInfo = { - "atmmosphereHeight", + "atmosphereHeight", "Atmosphere Height (KM)", "The thickness of the atmosphere in Km" }; @@ -715,6 +716,8 @@ glm::dmat4 RenderableAtmosphere::computeModelTransformMatrix( } void RenderableAtmosphere::render(const RenderData& data, RendererTasks& renderTask) { + ZoneScoped + if (_atmosphereEnabled) { DeferredcasterTask task{ _deferredcaster.get(), data }; renderTask.deferredcasterTasks.push_back(task); diff --git a/modules/server/src/connection.cpp b/modules/server/src/connection.cpp index 89cb02a96c..7e1b98891c 100644 --- a/modules/server/src/connection.cpp +++ b/modules/server/src/connection.cpp @@ -207,10 +207,14 @@ void Connection::handleJson(const nlohmann::json& json) { } void Connection::sendMessage(const std::string& message) { + ZoneScoped + _socket->putMessage(message); } void Connection::sendJson(const nlohmann::json& json) { + ZoneScoped + sendMessage(json.dump()); } diff --git a/modules/server/src/topics/timetopic.cpp b/modules/server/src/topics/timetopic.cpp index 789b9da924..71d422948c 100644 --- a/modules/server/src/topics/timetopic.cpp +++ b/modules/server/src/topics/timetopic.cpp @@ -30,6 +30,7 @@ #include #include #include +#include namespace { constexpr const char* EventKey = "event"; @@ -97,10 +98,13 @@ void TimeTopic::handleJson(const nlohmann::json& json) { } void TimeTopic::sendCurrentTime() { + ZoneScoped + const json timeJson = { { "time", global::timeManager.time().ISO8601() } }; - _connection->sendJson(wrappedPayload(timeJson)); + const json payload = wrappedPayload(timeJson); + _connection->sendJson(payload); _lastUpdateTime = std::chrono::system_clock::now(); } diff --git a/modules/server/src/topics/topic.cpp b/modules/server/src/topics/topic.cpp index 440252caab..3453c2d739 100644 --- a/modules/server/src/topics/topic.cpp +++ b/modules/server/src/topics/topic.cpp @@ -26,6 +26,7 @@ #include #include +#include namespace openspace { @@ -35,6 +36,8 @@ void Topic::initialize(Connection* connection, size_t topicId) { } nlohmann::json Topic::wrappedPayload(const nlohmann::json& payload) const { + ZoneScoped + // TODO: add message time nlohmann::json j = { { "topic", _topicId }, @@ -44,6 +47,8 @@ nlohmann::json Topic::wrappedPayload(const nlohmann::json& payload) const { } nlohmann::json Topic::wrappedError(std::string message, int code) { + ZoneScoped + nlohmann::json j = { { "topic", _topicId }, { "status", "error" }, diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index e9216124cb..1b78001201 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -262,11 +262,9 @@ void OpenSpaceEngine::initialize() { #endif // WIN32 #ifndef GHOUL_LOGGING_ENABLE_TRACE - LogLevel level = ghoul::logging::levelFromString(_configuration->logging.level); - if (level == ghoul::logging::LogLevel::Trace) { LWARNING( - "Desired logging level is set to 'Trace' but application was " << + "Desired logging level is set to 'Trace' but application was " "compiled without Trace support" ); } @@ -712,8 +710,11 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) { std::unique_ptr sceneInitializer; if (global::configuration.useMultithreadedInitialization) { - unsigned int nAvailableThreads = std::thread::hardware_concurrency(); - unsigned int nThreads = nAvailableThreads == 0 ? 2 : nAvailableThreads - 1; + unsigned int nAvailableThreads = std::min( + std::thread::hardware_concurrency() - 1, + 4u + ); + unsigned int nThreads = nAvailableThreads == 0 ? 2 : nAvailableThreads; sceneInitializer = std::make_unique(nThreads); } else { diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index c952a43ebd..a83a69979c 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -1400,7 +1400,7 @@ void RenderEngine::renderScreenLog() { std::array buf; { - std::fill(buf.begin(), buf.end(), 0); + std::fill(buf.begin(), buf.end(), char(0)); char* end = fmt::format_to( buf.data(), "{:<15} {}{}", @@ -1438,7 +1438,7 @@ void RenderEngine::renderScreenLog() { }(it->level); const std::string_view lvl = ghoul::to_string(it->level); - std::fill(buf.begin(), buf.end(), 0); + std::fill(buf.begin(), buf.end(), char(0)); char* end = fmt::format_to(buf.data(), "({})", lvl); std::string_view levelText = std::string_view(buf.data(), end - buf.data()); RenderFont( diff --git a/src/scene/asset.cpp b/src/scene/asset.cpp index 5cc5e52b2d..8c5909bada 100644 --- a/src/scene/asset.cpp +++ b/src/scene/asset.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -94,6 +95,8 @@ Asset::State Asset::state() const { } void Asset::setState(Asset::State state) { + ZoneScoped + if (_state == state) { return; } @@ -188,6 +191,7 @@ void Asset::clearSynchronizations() { void Asset::syncStateChanged(ResourceSynchronization* sync, ResourceSynchronization::State state) { + ZoneScoped if (state == ResourceSynchronization::State::Resolved) { if (!isSynchronized() && isSyncResolveReady()) { @@ -488,6 +492,8 @@ void Asset::unloadIfUnwanted() { } bool Asset::initialize() { + ZoneScoped + if (isInitialized()) { return true; } diff --git a/src/scene/assetloader.cpp b/src/scene/assetloader.cpp index 9642504ccf..246a5399cf 100644 --- a/src/scene/assetloader.cpp +++ b/src/scene/assetloader.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include "assetloader_lua.inl" @@ -497,11 +498,15 @@ ghoul::filesystem::Directory AssetLoader::currentDirectory() const { } std::shared_ptr AssetLoader::add(const std::string& identifier) { + ZoneScoped + setCurrentAsset(_rootAsset.get()); return request(identifier); } void AssetLoader::remove(const std::string& identifier) { + ZoneScoped + setCurrentAsset(_rootAsset.get()); unrequest(identifier); } @@ -530,6 +535,8 @@ const std::string& AssetLoader::assetRootDirectory() const { } void AssetLoader::callOnInitialize(Asset* asset) { + ZoneScoped + for (int init : _onInitializationFunctionRefs[asset]) { lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, init); if (lua_pcall(*_luaState, 0, 0, 0) != LUA_OK) { @@ -543,7 +550,9 @@ void AssetLoader::callOnInitialize(Asset* asset) { } } -void AssetLoader::callOnDeinitialize(Asset * asset) { +void AssetLoader::callOnDeinitialize(Asset* asset) { + ZoneScoped + const std::vector& funs = _onDeinitializationFunctionRefs[asset]; for (auto it = funs.rbegin(); it != funs.rend(); it++) { lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, *it); @@ -559,6 +568,8 @@ void AssetLoader::callOnDeinitialize(Asset * asset) { } void AssetLoader::callOnDependencyInitialize(Asset* asset, Asset* dependant) { + ZoneScoped + for (int init : _onDependencyInitializationFunctionRefs[dependant][asset]) { lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, init); if (lua_pcall(*_luaState, 0, 0, 0) != LUA_OK) { @@ -577,6 +588,8 @@ void AssetLoader::callOnDependencyInitialize(Asset* asset, Asset* dependant) { } void AssetLoader::callOnDependencyDeinitialize(Asset* asset, Asset* dependant) { + ZoneScoped + const std::vector& funs = _onDependencyDeinitializationFunctionRefs[dependant][asset]; @@ -595,6 +608,8 @@ void AssetLoader::callOnDependencyDeinitialize(Asset* asset, Asset* dependant) { } int AssetLoader::localResourceLua(Asset* asset) { + ZoneScoped + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::localResourceLua"); std::string name = ghoul::lua::value( @@ -613,6 +628,8 @@ int AssetLoader::localResourceLua(Asset* asset) { } int AssetLoader::syncedResourceLua(Asset* asset) { + ZoneScoped + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::syncedResourceLua"); ghoul::Dictionary d; @@ -633,6 +650,8 @@ int AssetLoader::syncedResourceLua(Asset* asset) { } void AssetLoader::setCurrentAsset(Asset* asset) { + ZoneScoped + const int top = lua_gettop(*_luaState); _currentAsset = asset; diff --git a/src/scene/assetmanager.cpp b/src/scene/assetmanager.cpp index 06282fa109..f93a278a6b 100644 --- a/src/scene/assetmanager.cpp +++ b/src/scene/assetmanager.cpp @@ -58,6 +58,7 @@ bool AssetManager::update() { // Add assets for (const std::pair& c : _pendingStateChangeCommands) { + ZoneScopedN("(add) Pending State Change") const std::string& path = c.first; const bool add = c.second; if (add) { @@ -67,6 +68,7 @@ bool AssetManager::update() { } // Remove assets for (const std::pair& c : _pendingStateChangeCommands) { + ZoneScopedN("(remove) Pending State change") const std::string& path = c.first; const bool remove = !c.second; if (remove && _assetLoader.has(path)) { diff --git a/src/scene/profile.cpp b/src/scene/profile.cpp index 8c9e908ed8..f351928e2a 100644 --- a/src/scene/profile.cpp +++ b/src/scene/profile.cpp @@ -498,6 +498,8 @@ void Profile::setIgnoreUpdates(bool ignoreUpdates) { } void Profile::addAsset(const std::string& path) { + ZoneScoped + if (_ignoreUpdates) { return; } @@ -519,13 +521,15 @@ void Profile::addAsset(const std::string& path) { } void Profile::removeAsset(const std::string& path) { + ZoneScoped + if (_ignoreUpdates) { return; } const auto it = std::find_if( - assets.begin(), - assets.end(), + assets.cbegin(), + assets.cend(), [path](const Asset& a) { return a.path == path; } ); diff --git a/src/util/screenlog.cpp b/src/util/screenlog.cpp index c5ff3f67e2..9425298cf9 100644 --- a/src/util/screenlog.cpp +++ b/src/util/screenlog.cpp @@ -26,14 +26,14 @@ #include -using std::string; - namespace openspace { ScreenLog::ScreenLog(std::chrono::seconds timeToLive, LogLevel logLevel) : _timeToLive(std::move(timeToLive)) , _logLevel(logLevel) -{} +{ + _entries.reserve(64); +} void ScreenLog::removeExpiredEntries() { std::lock_guard guard(_mutex); @@ -48,15 +48,15 @@ void ScreenLog::removeExpiredEntries() { _entries.erase(rit, _entries.end() ); } -void ScreenLog::log(LogLevel level, const string& category, const string& message) { +void ScreenLog::log(LogLevel level, std::string_view category, std::string_view message) { std::lock_guard guard(_mutex); if (level >= _logLevel) { _entries.push_back({ level, std::chrono::steady_clock::now(), Log::timeString(), - category, - message + std::string(category), + std::string(message) }); } } diff --git a/src/util/synchronizationwatcher.cpp b/src/util/synchronizationwatcher.cpp index 2fe4377611..e176b7af8c 100644 --- a/src/util/synchronizationwatcher.cpp +++ b/src/util/synchronizationwatcher.cpp @@ -24,6 +24,7 @@ #include +#include #include namespace openspace { @@ -75,8 +76,9 @@ void SynchronizationWatcher::unwatchSynchronization(WatchHandle watchHandle) { ), _pendingNotifications.end()); } - void SynchronizationWatcher::notify() { + ZoneScoped + std::vector notifications; { std::lock_guard guard(_mutex); @@ -85,6 +87,7 @@ void SynchronizationWatcher::notify() { } for (const NotificationData& n : notifications) { + ZoneScopedN("Notification") std::shared_ptr sync = n.synchronization.lock(); if (!sync) { continue; diff --git a/src/util/timemanager.cpp b/src/util/timemanager.cpp index ca95d401fd..505eaad5f7 100644 --- a/src/util/timemanager.cpp +++ b/src/util/timemanager.cpp @@ -139,9 +139,11 @@ void TimeManager::preSynchronization(double dt) { const double newTime = time().j2000Seconds(); if (newTime != _lastTime) { + ZoneScopedN("newTime != _lastTime") using K = const CallbackHandle; using V = TimeChangeCallback; for (const std::pair& it : _timeChangeCallbacks) { + ZoneScopedN("tcc") it.second(); } } @@ -149,16 +151,20 @@ void TimeManager::preSynchronization(double dt) { _timePaused != _lastTimePaused || _targetDeltaTime != _lastTargetDeltaTime) { + ZoneScopedN("delta time changed") using K = const CallbackHandle; using V = TimeChangeCallback; for (const std::pair& it : _deltaTimeChangeCallbacks) { + ZoneScopedN("dtcc") it.second(); } } if (_timelineChanged) { + ZoneScopedN("timeline changed") using K = const CallbackHandle; using V = TimeChangeCallback; for (const std::pair& it : _timelineChangeCallbacks) { + ZoneScopedN("tlcc") it.second(); } }