From 2123a647a32672c94ad92557789790aadaebb8b6 Mon Sep 17 00:00:00 2001 From: Hartmut Kaiser Date: Mon, 13 May 2024 15:29:40 -0500 Subject: [PATCH] Starting to fix CodeQL security warnings --- .github/workflows/codeql.yml | 5 +- .../parcel_coalescing/counter_registry.hpp | 11 +- .../src/coalescing_counter_registry.cpp | 74 ++- .../coalescing/src/performance_counters.cpp | 465 +++++++++--------- .../affinity/src/parse_affinity_options.cpp | 120 ++--- .../parallel/algorithms/detail/dispatch.hpp | 6 +- .../include/hpx/parallel/algorithms/merge.hpp | 2 +- .../hpx/parallel/algorithms/nth_element.hpp | 2 +- .../hpx/parallel/algorithms/partial_sort.hpp | 1 + .../parallel/algorithms/partial_sort_copy.hpp | 1 + .../hpx/parallel/algorithms/partition.hpp | 2 +- .../include/hpx/parallel/algorithms/sort.hpp | 2 +- .../hpx/parallel/algorithms/stable_sort.hpp | 2 +- .../parse_command_line_local.hpp | 6 +- libs/core/errors/include/hpx/errors/error.hpp | 4 +- .../include/hpx/executors/exception_list.hpp | 73 +-- .../src/utf8_codecvt_facet.cpp | 10 +- .../hpx/runtime_local/os_thread_type.hpp | 12 +- .../shared_priority_queue_scheduler.hpp | 374 ++++++++------ .../src/detail/background_thread.cpp | 5 +- .../threading_base/detail/switch_status.hpp | 4 +- .../threading_base/src/register_thread.cpp | 8 +- .../threading_base/src/set_thread_state.cpp | 77 +-- libs/full/init_runtime/src/hpx_init.cpp | 10 +- .../src/connection_handler_tcp.cpp | 1 + .../hpx/performance_counters/registry.hpp | 4 +- .../server/arithmetics_counter_extended.hpp | 4 +- .../performance_counters/src/registry.cpp | 278 +++++------ .../src/server/action_invocation_counter.cpp | 67 ++- .../src/server/arithmetics_counter.cpp | 89 ++-- .../server/arithmetics_counter_extended.cpp | 97 ++-- .../src/server/component_instance_counter.cpp | 100 ++-- .../src/server/elapsed_time_counter.cpp | 42 +- .../src/server/statistics_counter.cpp | 112 ++--- 34 files changed, 981 insertions(+), 1089 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 5a2332a5822b..bd03d0d2300f 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -50,7 +50,9 @@ jobs: queries: security-and-quality - name: Install CMake - uses: ssrobins/install-cmake@v1 + uses: jwlawson/actions-setup-cmake@v2.0 + with: + cmake-version: '3.22.x' - name: Install Ninja uses: seanmiddleditch/gha-setup-ninja@master @@ -80,6 +82,7 @@ jobs: # filter out all files from downloaded dependencies patterns: | -**/_deps/** + -**/concurrentqueue.hpp input: sarif-results/cpp.sarif output: sarif-results/cpp.sarif diff --git a/components/parcel_plugins/coalescing/include/hpx/parcel_coalescing/counter_registry.hpp b/components/parcel_plugins/coalescing/include/hpx/parcel_coalescing/counter_registry.hpp index 312759163a17..43d61094dbbe 100644 --- a/components/parcel_plugins/coalescing/include/hpx/parcel_coalescing/counter_registry.hpp +++ b/components/parcel_plugins/coalescing/include/hpx/parcel_coalescing/counter_registry.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2022 Hartmut Kaiser +// Copyright (c) 2016-2024 Hartmut Kaiser // // SPDX-License-Identifier: BSL-1.0 // Distributed under the Boost Software License, Version 1.0. (See accompanying @@ -60,10 +60,11 @@ namespace hpx::plugins::parcel { void register_action(std::string const& name); void register_action(std::string const& name, - get_counter_type num_parcels, get_counter_type num_messages, - get_counter_type time_between_parcels, - get_counter_type average_time_between_parcels, - get_counter_values_creator_type + get_counter_type const& num_parcels, + get_counter_type const& num_messages, + get_counter_type const& time_between_parcels, + get_counter_type const& average_time_between_parcels, + get_counter_values_creator_type const& time_between_parcels_histogram_creator); get_counter_type get_parcels_counter(std::string const& name) const; diff --git a/components/parcel_plugins/coalescing/src/coalescing_counter_registry.cpp b/components/parcel_plugins/coalescing/src/coalescing_counter_registry.cpp index 40b7a0824c62..d4c67fa1c8b9 100644 --- a/components/parcel_plugins/coalescing/src/coalescing_counter_registry.cpp +++ b/components/parcel_plugins/coalescing/src/coalescing_counter_registry.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2022 Hartmut Kaiser +// Copyright (c) 2016-2024 Hartmut Kaiser // // SPDX-License-Identifier: BSL-1.0 // Distributed under the Boost Software License, Version 1.0. (See accompanying @@ -30,10 +30,12 @@ namespace hpx::plugins::parcel { /////////////////////////////////////////////////////////////////////////// void coalescing_counter_registry::register_action(std::string const& name, - get_counter_type num_parcels, get_counter_type num_messages, - get_counter_type num_parcels_per_message, - get_counter_type average_time_between_parcels, - get_counter_values_creator_type time_between_parcels_histogram_creator) + get_counter_type const& num_parcels, + get_counter_type const& num_messages, + get_counter_type const& num_parcels_per_message, + get_counter_type const& average_time_between_parcels, + get_counter_values_creator_type const& + time_between_parcels_histogram_creator) { if (name.empty()) { @@ -56,29 +58,28 @@ namespace hpx::plugins::parcel { else { // replace the existing functions - (*it).second.num_parcels = num_parcels; - (*it).second.num_messages = num_messages; - (*it).second.num_parcels_per_message = num_parcels_per_message; - (*it).second.average_time_between_parcels = + it->second.num_parcels = num_parcels; + it->second.num_messages = num_messages; + it->second.num_parcels_per_message = num_parcels_per_message; + it->second.average_time_between_parcels = average_time_between_parcels; - (*it).second.time_between_parcels_histogram_creator = + it->second.time_between_parcels_histogram_creator = time_between_parcels_histogram_creator; - if ((*it).second.min_boundary != (*it).second.max_boundary) + if (it->second.min_boundary != it->second.max_boundary) { // instantiate actual histogram collection coalescing_counter_registry::get_counter_values_type result; - time_between_parcels_histogram_creator( - (*it).second.min_boundary, (*it).second.max_boundary, - (*it).second.num_buckets, result); + time_between_parcels_histogram_creator(it->second.min_boundary, + it->second.max_boundary, it->second.num_buckets, result); } // silence warnings - (void) (*it).second.num_parcels; - (void) (*it).second.num_messages; - (void) (*it).second.num_parcels_per_message; - (void) (*it).second.average_time_between_parcels; - (void) (*it).second.time_between_parcels_histogram_creator; + (void) it->second.num_parcels; + (void) it->second.num_messages; + (void) it->second.num_parcels_per_message; + (void) it->second.average_time_between_parcels; + (void) it->second.time_between_parcels_histogram_creator; } } @@ -114,9 +115,8 @@ namespace hpx::plugins::parcel { HPX_THROW_EXCEPTION(hpx::error::bad_parameter, "coalescing_counter_registry::get_num_parcels_counter", "unknown action type"); - return get_counter_type(); } - return (*it).second.num_parcels; + return it->second.num_parcels; } coalescing_counter_registry::get_counter_type @@ -132,9 +132,8 @@ namespace hpx::plugins::parcel { HPX_THROW_EXCEPTION(hpx::error::bad_parameter, "coalescing_counter_registry::get_num_messages_counter", "unknown action type"); - return get_counter_type(); } - return (*it).second.num_messages; + return it->second.num_messages; } coalescing_counter_registry::get_counter_type @@ -150,9 +149,8 @@ namespace hpx::plugins::parcel { HPX_THROW_EXCEPTION(hpx::error::bad_parameter, "coalescing_counter_registry::get_num_messages_counter", "unknown action type"); - return get_counter_type(); } - return (*it).second.num_parcels_per_message; + return it->second.num_parcels_per_message; } coalescing_counter_registry::get_counter_type @@ -169,9 +167,8 @@ namespace hpx::plugins::parcel { "coalescing_counter_registry::" "get_average_time_between_parcels_counter", "unknown action type"); - return get_counter_type(); } - return (*it).second.average_time_between_parcels; + return it->second.average_time_between_parcels; } coalescing_counter_registry::get_counter_values_type @@ -189,20 +186,19 @@ namespace hpx::plugins::parcel { "coalescing_counter_registry::" "get_time_between_parcels_histogram_counter", "unknown action type"); - return &coalescing_counter_registry::empty_histogram; } - if ((*it).second.time_between_parcels_histogram_creator.empty()) + if (it->second.time_between_parcels_histogram_creator.empty()) { // no parcel of this type has been sent yet - (*it).second.min_boundary = min_boundary; - (*it).second.max_boundary = max_boundary; - (*it).second.num_buckets = num_buckets; + it->second.min_boundary = min_boundary; + it->second.max_boundary = max_boundary; + it->second.num_buckets = num_buckets; return coalescing_counter_registry::get_counter_values_type(); } coalescing_counter_registry::get_counter_values_type result; - (*it).second.time_between_parcels_histogram_creator( + it->second.time_between_parcels_histogram_creator( min_boundary, max_boundary, num_buckets, result); return result; } @@ -281,11 +277,11 @@ namespace hpx::plugins::parcel { for (map_type::const_iterator it = map_.begin(); it != end; ++it) { - if (!std::regex_match((*it).first, rx)) + if (!std::regex_match(it->first, rx)) continue; performance_counters::counter_path_elements cp = p; - cp.parameters_ = (*it).first; + cp.parameters_ = it->first; if (!additional_parameters.empty()) cp.parameters_ += additional_parameters; @@ -304,7 +300,7 @@ namespace hpx::plugins::parcel { for (map_type::const_iterator it = map_.begin(); it != end; ++it) { - types += " " + (*it).first + "\n"; + types += " " + it->first + "\n"; } } @@ -348,10 +344,10 @@ namespace hpx::plugins::parcel { // compose a list of known action types std::string types; map_type::const_iterator end = map_.end(); - for (map_type::const_iterator it = map_.begin(); it != end; - ++it) + for (map_type::const_iterator it_ct = map_.begin(); + it_ct != end; ++it_ct) { - types += " " + (*it).first + "\n"; + types += " " + it_ct->first + "\n"; } l.unlock(); diff --git a/components/parcel_plugins/coalescing/src/performance_counters.cpp b/components/parcel_plugins/coalescing/src/performance_counters.cpp index 722367e78075..0fbe53f50457 100644 --- a/components/parcel_plugins/coalescing/src/performance_counters.cpp +++ b/components/parcel_plugins/coalescing/src/performance_counters.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2022 Hartmut Kaiser +// Copyright (c) 2016-2024 Hartmut Kaiser // // SPDX-License-Identifier: BSL-1.0 // Distributed under the Boost Software License, Version 1.0. (See accompanying @@ -90,57 +90,53 @@ namespace hpx::plugins::parcel { hpx::performance_counters::counter_info const& info, hpx::error_code& ec) { - switch (info.type_) + if (info.type_ != + performance_counters::counter_type::monotonically_increasing) { - case performance_counters::counter_type::monotonically_increasing: - { - performance_counters::counter_path_elements paths; - performance_counters::get_counter_path_elements( - info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; - - if (paths.parentinstance_is_basename_) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "num_parcels_counter_creator", - "invalid counter name for number of parcels (instance " - "name must not be a valid base counter name)"); - return naming::invalid_gid; - } - - if (paths.parameters_.empty()) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "num_parcels_counter_creator", - "invalid counter parameter for number of parcels: must " - "specify an action type"); - return naming::invalid_gid; - } - - // ask registry - hpx::function f = - coalescing_counter_registry::instance().get_parcels_counter( - paths.parameters_); + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "num_parcels_counter_creator", + "invalid counter type requested"); + return naming::invalid_gid; + } - if (!f.empty()) - { - return performance_counters::detail::create_raw_counter( - info, HPX_MOVE(f), ec); - } + performance_counters::counter_path_elements paths; + performance_counters::get_counter_path_elements( + info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; - // the counter is not available yet, create surrogate function - return performance_counters::detail::create_raw_counter( - info, num_parcels_counter_surrogate(paths.parameters_), ec); + if (paths.parentinstance_is_basename_) + { + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "num_parcels_counter_creator", + "invalid counter name for number of parcels (instance " + "name must not be a valid base counter name)"); + return naming::invalid_gid; } - break; - default: + if (paths.parameters_.empty()) + { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "num_parcels_counter_creator", - "invalid counter type requested"); + "invalid counter parameter for number of parcels: must " + "specify an action type"); return naming::invalid_gid; } + + // ask registry + hpx::function f = + coalescing_counter_registry::instance().get_parcels_counter( + paths.parameters_); + + if (!f.empty()) + { + return performance_counters::detail::create_raw_counter( + info, HPX_MOVE(f), ec); + } + + // the counter is not available yet, create surrogate function + return performance_counters::detail::create_raw_counter( + info, num_parcels_counter_surrogate(paths.parameters_), ec); } /////////////////////////////////////////////////////////////////////////// @@ -173,57 +169,53 @@ namespace hpx::plugins::parcel { hpx::performance_counters::counter_info const& info, hpx::error_code& ec) { - switch (info.type_) + if (info.type_ != + performance_counters::counter_type::monotonically_increasing) { - case performance_counters::counter_type::monotonically_increasing: - { - performance_counters::counter_path_elements paths; - performance_counters::get_counter_path_elements( - info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; - - if (paths.parentinstance_is_basename_) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "num_messages_counter_creator", - "invalid counter name for number of parcels (instance " - "name must not be a valid base counter name)"); - return naming::invalid_gid; - } - - if (paths.parameters_.empty()) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "num_messages_counter_creator", - "invalid counter parameter for number of parcels: must " - "specify an action type"); - return naming::invalid_gid; - } - - // ask registry - hpx::function f = - coalescing_counter_registry::instance().get_messages_counter( - paths.parameters_); + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "num_messages_counter_creator", + "invalid counter type requested"); + return naming::invalid_gid; + } - if (!f.empty()) - { - return performance_counters::detail::create_raw_counter( - info, HPX_MOVE(f), ec); - } + performance_counters::counter_path_elements paths; + performance_counters::get_counter_path_elements( + info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; - // the counter is not available yet, create surrogate function - return performance_counters::detail::create_raw_counter( - info, num_messages_counter_surrogate(paths.parameters_), ec); + if (paths.parentinstance_is_basename_) + { + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "num_messages_counter_creator", + "invalid counter name for number of parcels (instance " + "name must not be a valid base counter name)"); + return naming::invalid_gid; } - break; - default: + if (paths.parameters_.empty()) + { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "num_messages_counter_creator", - "invalid counter type requested"); + "invalid counter parameter for number of parcels: must " + "specify an action type"); return naming::invalid_gid; } + + // ask registry + hpx::function f = + coalescing_counter_registry::instance().get_messages_counter( + paths.parameters_); + + if (!f.empty()) + { + return performance_counters::detail::create_raw_counter( + info, HPX_MOVE(f), ec); + } + + // the counter is not available yet, create surrogate function + return performance_counters::detail::create_raw_counter( + info, num_messages_counter_surrogate(paths.parameters_), ec); } /////////////////////////////////////////////////////////////////////////// @@ -257,58 +249,52 @@ namespace hpx::plugins::parcel { hpx::performance_counters::counter_info const& info, hpx::error_code& ec) { - switch (info.type_) - { - case performance_counters::counter_type::average_count: + if (info.type_ != performance_counters::counter_type::average_count) { - performance_counters::counter_path_elements paths; - performance_counters::get_counter_path_elements( - info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; - - if (paths.parentinstance_is_basename_) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "num_parcels_per_message_counter_creator", - "invalid counter name for number of parcels (instance " - "name must not be a valid base counter name)"); - return naming::invalid_gid; - } - - if (paths.parameters_.empty()) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "num_parcels_per_message_counter_creator", - "invalid counter parameter for number of parcels: must " - "specify an action type"); - return naming::invalid_gid; - } - - // ask registry - hpx::function f = - coalescing_counter_registry::instance() - .get_parcels_per_message_counter(paths.parameters_); + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "num_parcels_per_message_counter_creator", + "invalid counter type requested"); + return naming::invalid_gid; + } - if (!f.empty()) - { - return performance_counters::detail::create_raw_counter( - info, HPX_MOVE(f), ec); - } + performance_counters::counter_path_elements paths; + performance_counters::get_counter_path_elements( + info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; - // the counter is not available yet, create surrogate function - return performance_counters::detail::create_raw_counter(info, - num_parcels_per_message_counter_surrogate(paths.parameters_), - ec); + if (paths.parentinstance_is_basename_) + { + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "num_parcels_per_message_counter_creator", + "invalid counter name for number of parcels (instance " + "name must not be a valid base counter name)"); + return naming::invalid_gid; } - break; - default: + if (paths.parameters_.empty()) + { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "num_parcels_per_message_counter_creator", - "invalid counter type requested"); + "invalid counter parameter for number of parcels: must " + "specify an action type"); return naming::invalid_gid; } + + // ask registry + hpx::function f = + coalescing_counter_registry::instance() + .get_parcels_per_message_counter(paths.parameters_); + + if (!f.empty()) + { + return performance_counters::detail::create_raw_counter( + info, HPX_MOVE(f), ec); + } + + // the counter is not available yet, create surrogate function + return performance_counters::detail::create_raw_counter(info, + num_parcels_per_message_counter_surrogate(paths.parameters_), ec); } /////////////////////////////////////////////////////////////////////////// @@ -343,60 +329,53 @@ namespace hpx::plugins::parcel { hpx::performance_counters::counter_info const& info, hpx::error_code& ec) { - switch (info.type_) + if (info.type_ != performance_counters::counter_type::average_timer) { - case performance_counters::counter_type::average_timer: - { - performance_counters::counter_path_elements paths; - performance_counters::get_counter_path_elements( - info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; - - if (paths.parentinstance_is_basename_) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "average_time_between_parcels_counter_creator", - "invalid counter name for number of parcels (instance " - "name must not be a valid base counter name)"); - return naming::invalid_gid; - } - - if (paths.parameters_.empty()) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "average_time_between_parcels_counter_creator", - "invalid counter parameter for number of parcels: must " - "specify an action type"); - return naming::invalid_gid; - } - - // ask registry - hpx::function f = - coalescing_counter_registry::instance() - .get_average_time_between_parcels_counter( - paths.parameters_); + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "average_time_between_parcels_counter_creator", + "invalid counter type requested"); + return naming::invalid_gid; + } - if (!f.empty()) - { - return performance_counters::detail::create_raw_counter( - info, HPX_MOVE(f), ec); - } + performance_counters::counter_path_elements paths; + performance_counters::get_counter_path_elements( + info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; - // the counter is not available yet, create surrogate function - return performance_counters::detail::create_raw_counter(info, - average_time_between_parcels_counter_surrogate( - paths.parameters_), - ec); + if (paths.parentinstance_is_basename_) + { + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "average_time_between_parcels_counter_creator", + "invalid counter name for number of parcels (instance " + "name must not be a valid base counter name)"); + return naming::invalid_gid; } - break; - default: + if (paths.parameters_.empty()) + { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "average_time_between_parcels_counter_creator", - "invalid counter type requested"); + "invalid counter parameter for number of parcels: must " + "specify an action type"); return naming::invalid_gid; } + + // ask registry + hpx::function f = + coalescing_counter_registry::instance() + .get_average_time_between_parcels_counter(paths.parameters_); + + if (!f.empty()) + { + return performance_counters::detail::create_raw_counter( + info, HPX_MOVE(f), ec); + } + + // the counter is not available yet, create surrogate function + return performance_counters::detail::create_raw_counter(info, + average_time_between_parcels_counter_surrogate(paths.parameters_), + ec); } /////////////////////////////////////////////////////////////////////////// @@ -467,89 +446,84 @@ namespace hpx::plugins::parcel { hpx::performance_counters::counter_info const& info, hpx::error_code& ec) { - switch (info.type_) + if (info.type_ != performance_counters::counter_type::histogram) { - case performance_counters::counter_type::histogram: - { - performance_counters::counter_path_elements paths; - performance_counters::get_counter_path_elements( - info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "time_between_parcels_histogram_counter_creator", + "invalid counter type requested"); + return naming::invalid_gid; + } - if (paths.parentinstance_is_basename_) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "time_between_parcels_histogram_counter_creator", - "invalid counter name for " - "time-between-parcels histogram (instance " - "name must not be a valid base counter name)"); - return naming::invalid_gid; - } + performance_counters::counter_path_elements paths; + performance_counters::get_counter_path_elements( + info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; - if (paths.parameters_.empty()) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "time_between_parcels_histogram_counter_creator", - "invalid counter parameter for " - "time-between-parcels histogram: must " - "specify an action type"); - return naming::invalid_gid; - } + if (paths.parentinstance_is_basename_) + { + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "time_between_parcels_histogram_counter_creator", + "invalid counter name for " + "time-between-parcels histogram (instance " + "name must not be a valid base counter name)"); + return naming::invalid_gid; + } - // split parameters, extract separate values - std::vector params; - hpx::string_util::split(params, paths.parameters_, - hpx::string_util::is_any_of(","), - hpx::string_util::token_compress_mode::off); + if (paths.parameters_.empty()) + { + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "time_between_parcels_histogram_counter_creator", + "invalid counter parameter for " + "time-between-parcels histogram: must " + "specify an action type"); + return naming::invalid_gid; + } - std::int64_t min_boundary = 0; - std::int64_t max_boundary = 1000000; // 1ms - std::int64_t num_buckets = 20; + // split parameters, extract separate values + std::vector params; + hpx::string_util::split(params, paths.parameters_, + hpx::string_util::is_any_of(","), + hpx::string_util::token_compress_mode::off); - if (params.empty() || params[0].empty()) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "time_between_parcels_histogram_counter_creator", - "invalid counter parameter for " - "time-between-parcels histogram: " - "must specify an action type"); - return naming::invalid_gid; - } + std::int64_t min_boundary = 0; + std::int64_t max_boundary = 1000000; // 1ms + std::int64_t num_buckets = 20; - if (params.size() > 1 && !params[1].empty()) - min_boundary = util::from_string(params[1]); - if (params.size() > 2 && !params[2].empty()) - max_boundary = util::from_string(params[2]); - if (params.size() > 3 && !params[3].empty()) - num_buckets = util::from_string(params[3]); + if (params.empty() || params[0].empty()) + { + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "time_between_parcels_histogram_counter_creator", + "invalid counter parameter for " + "time-between-parcels histogram: " + "must specify an action type"); + return naming::invalid_gid; + } - // ask registry - hpx::function(bool)> f = - coalescing_counter_registry::instance() - .get_time_between_parcels_histogram_counter( - params[0], min_boundary, max_boundary, num_buckets); + if (params.size() > 1 && !params[1].empty()) + min_boundary = util::from_string(params[1]); + if (params.size() > 2 && !params[2].empty()) + max_boundary = util::from_string(params[2]); + if (params.size() > 3 && !params[3].empty()) + num_buckets = util::from_string(params[3]); - if (!f.empty()) - { - return performance_counters::detail::create_raw_counter( - info, HPX_MOVE(f), ec); - } + // ask registry + hpx::function(bool)> f = + coalescing_counter_registry::instance() + .get_time_between_parcels_histogram_counter( + params[0], min_boundary, max_boundary, num_buckets); - // the counter is not available yet, create surrogate function - return performance_counters::detail::create_raw_counter(info, - time_between_parcels_histogram_counter_surrogate( - params[0], min_boundary, max_boundary, num_buckets), - ec); + if (!f.empty()) + { + return performance_counters::detail::create_raw_counter( + info, HPX_MOVE(f), ec); } - break; - default: - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "time_between_parcels_histogram_counter_creator", - "invalid counter type requested"); - return naming::invalid_gid; - } + // the counter is not available yet, create surrogate function + return performance_counters::detail::create_raw_counter(info, + time_between_parcels_histogram_counter_surrogate( + params[0], min_boundary, max_boundary, num_buckets), + ec); } /////////////////////////////////////////////////////////////////////////// @@ -608,8 +582,7 @@ namespace hpx::plugins::parcel { // Install the counter types, un-installation of the types is handled // automatically. - install_counter_types( - counter_types, sizeof(counter_types) / sizeof(counter_types[0])); + install_counter_types(counter_types, std::size(counter_types)); } /////////////////////////////////////////////////////////////////////////// diff --git a/libs/core/affinity/src/parse_affinity_options.cpp b/libs/core/affinity/src/parse_affinity_options.cpp index 90d4533e5f40..c5b666ca78f2 100644 --- a/libs/core/affinity/src/parse_affinity_options.cpp +++ b/libs/core/affinity/src/parse_affinity_options.cpp @@ -371,9 +371,7 @@ namespace hpx::threads::detail { { std::vector masks; - switch (s.type_) - { - case spec_type::type::core: + if (s.type_ == spec_type::type::core) { std::size_t base = 0; std::size_t num_cores; @@ -412,34 +410,29 @@ namespace hpx::threads::detail { } bounds_type const bounds = extract_bounds(s, num_cores, ec); - if (ec) - break; - - for (std::int64_t const index : bounds) + if (!ec) { - mask_type mask = t.init_core_affinity_mask_from_core( - static_cast(index + base)); - masks.emplace_back( - static_cast(index), mask & socket_mask); + for (std::int64_t const index : bounds) + { + mask_type mask = t.init_core_affinity_mask_from_core( + static_cast(index + base)); + masks.emplace_back( + static_cast(index), mask & socket_mask); + } } } - break; - - case spec_type::type::unknown: + else if (s.type_ == spec_type::type::unknown) { mask_type const mask = extract_machine_mask(t, ec); masks.emplace_back( static_cast(-1), mask & socket_mask); } - break; - - default: + else + { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "extract_core_mask", "unexpected specification type {}", spec_type::type_name(s.type_)); - break; } - return masks; } @@ -449,9 +442,7 @@ namespace hpx::threads::detail { { std::vector masks; - switch (s.type_) - { - case spec_type::type::pu: + if (s.type_ == spec_type::type::pu) { std::size_t socket_base = 0; if (static_cast(-1) != socket) @@ -481,54 +472,50 @@ namespace hpx::threads::detail { } bounds_type const bounds = extract_bounds(s, num_pus, ec); - if (ec) - break; - - std::size_t const num_cores = t.get_number_of_cores(); - for (std::int64_t const index : bounds) + if (!ec) { - std::size_t base_core = socket_base; - if (static_cast(-1) != core) //-V1051 - { - base_core += core; - } - else + std::size_t const num_cores = t.get_number_of_cores(); + for (std::int64_t const index : bounds) { - // find core the given pu belongs to - std::size_t base = 0; - for (/**/; base_core < num_cores; ++base_core) + std::size_t base_core = socket_base; + if (static_cast(-1) != core) //-V1051 + { + base_core += core; + } + else { - std::size_t const num_core_pus = - t.get_number_of_core_pus(base_core); - if (base + num_core_pus > - static_cast(index)) + // find core the given pu belongs to + std::size_t base = 0; + for (/**/; base_core < num_cores; ++base_core) { - break; + std::size_t const num_core_pus = + t.get_number_of_core_pus(base_core); + if (base + num_core_pus > + static_cast(index)) + { + break; + } + base += num_core_pus; } - base += num_core_pus; } - } - mask_type mask = t.init_thread_affinity_mask( - base_core, static_cast(index)); - masks.emplace_back( - static_cast(index), mask & core_mask); + mask_type mask = t.init_thread_affinity_mask( + base_core, static_cast(index)); + masks.emplace_back( + static_cast(index), mask & core_mask); + } } } - break; - - case spec_type::type::unknown: + else if (s.type_ == spec_type::type::unknown) { mask_type const mask = extract_machine_mask(t, ec); masks.emplace_back(static_cast(-1), mask & core_mask); } - break; - - default: + else + { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "extract_pu_mask", "unexpected specification type {}", spec_type::type_name(s.type_)); - break; } return masks; @@ -674,7 +661,7 @@ namespace hpx::threads::detail { break; // get the pu masks (i.e. overall affinity masks) for - // all of the core masks + // all the core masks extract_pu_affinities(t, specs, static_cast(-1), core_masks, affinities, ec); @@ -692,7 +679,7 @@ namespace hpx::threads::detail { break; // get the pu masks (i.e. overall affinity masks) for - // all of the core masks + // all the core masks extract_pu_affinities( t, specs, get_index(smi), core_masks, affinities, ec); if (ec) @@ -1191,9 +1178,7 @@ namespace hpx::threads { // been initialized yet threads::topology const& t = threads::create_topology(); - switch (mappings.which()) - { - case 0: + if (mappings.which() == 0) { detail::decode_distribution( boost::get(mappings), t, affinities, @@ -1202,9 +1187,7 @@ namespace hpx::threads { if (ec) return; } - break; - - case 1: + else if (mappings.which() == 1) { if (use_process_mask) { @@ -1270,17 +1253,8 @@ namespace hpx::threads { } } } - // 26819: Unannotated fallthrough between switch labels -#if defined(HPX_MSVC) -#pragma warning(push) -#pragma warning(disable : 26819) -#endif - break; -#if defined(HPX_MSVC) -#pragma warning(pop) -#endif - - default: + else + { HPX_ASSERT(false); } } diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/dispatch.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/dispatch.hpp index 3c6e38a33d01..7920a88ed1d3 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/dispatch.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/dispatch.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2007-2023 Hartmut Kaiser +// Copyright (c) 2007-2024 Hartmut Kaiser // Copyright (c) 2021 Giannis Gonidelis // // SPDX-License-Identifier: BSL-1.0 @@ -19,6 +19,7 @@ #include #include +#include #if defined(HPX_HAVE_CXX17_STD_EXECUTION_POLICES) #include #endif @@ -129,7 +130,8 @@ namespace hpx::parallel::detail { return hpx::parallel::detail::handle_exception, - hpx::util::unused_type, local_result_type>>::call(); + hpx::util::unused_type, + local_result_type>>::call(std::current_exception()); } #endif } diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/merge.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/merge.hpp index 1e915d247b1e..0897d76bf856 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/merge.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/merge.hpp @@ -1,5 +1,5 @@ // Copyright (c) 2017 Taeguk Kwon -// Copyright (c) 2017-2023 Hartmut Kaiser +// Copyright (c) 2017-2024 Hartmut Kaiser // // SPDX-License-Identifier: BSL-1.0 // Distributed under the Boost Software License, Version 1.0. (See accompanying diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/nth_element.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/nth_element.hpp index 9aa362b99385..6ecb9ae73e6a 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/nth_element.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/nth_element.hpp @@ -1,5 +1,5 @@ // Copyright (c) 2014 Grant Mercer -// Copyright (c) 2017-2023 Hartmut Kaiser +// Copyright (c) 2017-2024 Hartmut Kaiser // Copyright (c) 2020 Francisco Jose Tapia // Copyright (c) 2021 Akhil J Nair // diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort.hpp index 4954efad6cf2..939e54459c7d 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort.hpp @@ -1,5 +1,6 @@ // Copyright (c) 2020 Francisco Jose Tapia // Copyright (c) 2021 Akhil J Nair +// Copyright (c) 2020-2024 Hartmut Kaiser // // SPDX-License-Identifier: BSL-1.0 // Distributed under the Boost Software License, Version 1.0. (See accompanying diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort_copy.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort_copy.hpp index 52131dcb0f61..55b6ed41f016 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort_copy.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort_copy.hpp @@ -1,5 +1,6 @@ // Copyright (c) 2020 Francisco Jose Tapia // Copyright (c) 2021 Akhil J Nair +// Copyright (c) 2020-2024 Hartmut Kaiser // // SPDX-License-Identifier: BSL-1.0 // Distributed under the Boost Software License, Version 1.0. (See accompanying diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/partition.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/partition.hpp index cce4bb860979..ba5d718e14b8 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/partition.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/partition.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2014-2023 Hartmut Kaiser +// Copyright (c) 2014-2024 Hartmut Kaiser // Copyright (c) 2017 Taeguk Kwon // Copyright (c) 2021 Akhil J Nair // diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/sort.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/sort.hpp index 0095c97e3358..752fd6aa1f47 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/sort.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/sort.hpp @@ -1,5 +1,5 @@ // Copyright (c) 2015 John Biddiscombe -// Copyright (c) 2015-2023 Hartmut Kaiser +// Copyright (c) 2015-2024 Hartmut Kaiser // Copyright (c) 2015-2019 Francisco Jose Tapia // Copyright (c) 2018 Taeguk Kwon // Copyright (c) 2021 Akhil J Nair diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/stable_sort.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/stable_sort.hpp index 723ee6ffb8b0..665feabd39e5 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/stable_sort.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/stable_sort.hpp @@ -1,5 +1,5 @@ // Copyright (c) 2015-2017 Francisco Jose Tapia -// Copyright (c) 2020-2023 Hartmut Kaiser +// Copyright (c) 2020-2024 Hartmut Kaiser // Copyright (c) 2021 Akhil J Nair // // SPDX-License-Identifier: BSL-1.0 diff --git a/libs/core/command_line_handling_local/include/hpx/command_line_handling_local/parse_command_line_local.hpp b/libs/core/command_line_handling_local/include/hpx/command_line_handling_local/parse_command_line_local.hpp index ba7ebc417d24..c639ef6b8e99 100644 --- a/libs/core/command_line_handling_local/include/hpx/command_line_handling_local/parse_command_line_local.hpp +++ b/libs/core/command_line_handling_local/include/hpx/command_line_handling_local/parse_command_line_local.hpp @@ -20,9 +20,9 @@ namespace hpx::util { enum class commandline_error_mode : std::uint8_t { - return_on_error, - rethrow_on_error, - allow_unregistered, + return_on_error = 1, + rethrow_on_error = 2, + allow_unregistered = 3, ignore_aliases = 0x40, report_missing_config_file = 0x80 }; diff --git a/libs/core/errors/include/hpx/errors/error.hpp b/libs/core/errors/include/hpx/errors/error.hpp index 16b62c7f1b9a..c433a0f932bd 100644 --- a/libs/core/errors/include/hpx/errors/error.hpp +++ b/libs/core/errors/include/hpx/errors/error.hpp @@ -78,7 +78,7 @@ namespace hpx { assertion_failure = 25, ///< null_thread_id = 26, - ///< Attempt to invoke a API function from a non-HPX thread + ///< Attempt to invoke an API function from a non-HPX thread invalid_data = 27, ///< yield_aborted = 28, @@ -142,7 +142,7 @@ namespace hpx { ///< race, retry /// \cond NOINTERNAL - last_error, + last_error = 57, system_error_flag = 0x4000L, diff --git a/libs/core/executors/include/hpx/executors/exception_list.hpp b/libs/core/executors/include/hpx/executors/exception_list.hpp index 2a01b0861084..993a9193cd97 100644 --- a/libs/core/executors/include/hpx/executors/exception_list.hpp +++ b/libs/core/executors/include/hpx/executors/exception_list.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2007-2023 Hartmut Kaiser +// Copyright (c) 2007-2024 Hartmut Kaiser // 2017-2018 Taeguk Kwon // // SPDX-License-Identifier: BSL-1.0 @@ -35,26 +35,6 @@ namespace hpx::parallel { { using type = Result; - [[noreturn]] static Result call() - { - try - { - throw; //-V667 - } - catch (std::bad_alloc const&) - { - throw; - } - catch (hpx::exception_list const&) - { - throw; - } - catch (...) - { - throw hpx::exception_list(std::current_exception()); - } - } - static Result call(hpx::future f) { HPX_ASSERT(f.has_exception()); @@ -92,27 +72,6 @@ namespace hpx::parallel { { using type = future; - static future call() - { - try - { - throw; //-V667 - } - catch (std::bad_alloc const& e) - { - return hpx::make_exceptional_future(e); - } - catch (hpx::exception_list const& el) - { - return hpx::make_exceptional_future(el); - } - catch (...) - { - return hpx::make_exceptional_future( - hpx::exception_list(std::current_exception())); - } - } - static future call(future f) { HPX_ASSERT(f.has_exception()); @@ -165,11 +124,6 @@ namespace hpx::parallel { { using type = Result; - [[noreturn]] static Result call() - { - exception_list_termination_handler(); - } - [[noreturn]] static hpx::future call(hpx::future&&) { exception_list_termination_handler(); @@ -190,11 +144,6 @@ namespace hpx::parallel { { using type = future; - [[noreturn]] static future call() - { - exception_list_termination_handler(); - } - [[noreturn]] static future call(future) { exception_list_termination_handler(); @@ -214,26 +163,6 @@ namespace hpx::parallel { { using type = Result; - [[noreturn]] static Result call() - { - try - { - throw; //-V667 - } - catch (std::bad_alloc const&) - { - throw; - } - catch (hpx::exception_list const&) - { - throw; - } - catch (...) - { - throw hpx::exception_list(std::current_exception()); - } - } - [[noreturn]] static Result call(std::exception_ptr const& e) { try diff --git a/libs/core/program_options/src/utf8_codecvt_facet.cpp b/libs/core/program_options/src/utf8_codecvt_facet.cpp index 67aad71783df..0ae3df672381 100644 --- a/libs/core/program_options/src/utf8_codecvt_facet.cpp +++ b/libs/core/program_options/src/utf8_codecvt_facet.cpp @@ -16,6 +16,7 @@ #include #include +#include #include // for multi-byte conversion routines #include @@ -63,7 +64,7 @@ namespace hpx::program_options::detail { // number of "continuing octets" encoding the character int const cont_octet_count = static_cast(get_cont_octet_count(*from)); - const wchar_t octet1_modifier_table[] = { + wchar_t const octet1_modifier_table[] = { 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}; // The unsigned char conversion is necessary in case char is @@ -117,14 +118,15 @@ namespace hpx::program_options::detail { wchar_t const*& from_next, char* to, char* to_end, char*& to_next) const { // RG - consider merging this table with the other one - const wchar_t octet1_modifier_table[] = { + wchar_t const octet1_modifier_table[] = { 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}; constexpr wchar_t max_wchar = (std::numeric_limits::max)(); while (from != from_end && to != to_end) { // Check for invalid UCS-4 character - if (*from > max_wchar) + if (static_cast(*from) > + static_cast(max_wchar)) { from_next = from; to_next = to; @@ -134,7 +136,7 @@ namespace hpx::program_options::detail { int const cont_octet_count = get_cont_octet_out_count(*from); // RG - comment this formula better - int shift_exponent = (cont_octet_count) *6; + int shift_exponent = cont_octet_count * 6; // Process the first character *to++ = static_cast(octet1_modifier_table[cont_octet_count] + diff --git a/libs/core/runtime_local/include/hpx/runtime_local/os_thread_type.hpp b/libs/core/runtime_local/include/hpx/runtime_local/os_thread_type.hpp index 929a9a258a07..21bece24b15c 100644 --- a/libs/core/runtime_local/include/hpx/runtime_local/os_thread_type.hpp +++ b/libs/core/runtime_local/include/hpx/runtime_local/os_thread_type.hpp @@ -19,12 +19,12 @@ namespace hpx::runtime_local { enum class os_thread_type { unknown = -1, - main_thread = 0, ///< kernel thread represents main thread - worker_thread, ///< kernel thread is used to schedule HPX threads - io_thread, ///< kernel thread can be used for IO operations - timer_thread, ///< kernel is used by timer operations - parcel_thread, ///< kernel is used by networking operations - custom_thread ///< kernel is registered by the application + main_thread = 0, ///< kernel thread represents main thread + worker_thread = 1, ///< kernel thread is used to schedule HPX threads + io_thread = 2, ///< kernel thread can be used for IO operations + timer_thread = 3, ///< kernel is used by timer operations + parcel_thread = 4, ///< kernel is used by networking operations + custom_thread = 5 ///< kernel is registered by the application }; /// Return a human-readable name representing one of the kernel thread types diff --git a/libs/core/schedulers/include/hpx/schedulers/shared_priority_queue_scheduler.hpp b/libs/core/schedulers/include/hpx/schedulers/shared_priority_queue_scheduler.hpp index 69ad15e9343d..b712bad7e411 100644 --- a/libs/core/schedulers/include/hpx/schedulers/shared_priority_queue_scheduler.hpp +++ b/libs/core/schedulers/include/hpx/schedulers/shared_priority_queue_scheduler.hpp @@ -295,6 +295,110 @@ namespace hpx::threads::policies { ->cleanup_terminated(local_num, delete_all); } + void create_thread_none(std::size_t& thread_num, std::size_t local_num, + std::size_t& domain_num, std::size_t& q_index) + { + auto msg = spq_deb.declare_variable(nullptr); + + spq_deb.set(msg, "HINT_NONE "); + // Create thread on this worker thread if possible + if (local_num == static_cast(-1)) + { + // clang-format off + using namespace hpx::threads::detail; + spq_deb.debug(debug::str<>("create_thread") + , "x-pool", "num_workers_", num_workers_ + , "thread_number" + , "global", get_thread_nums_tss().global_thread_num + , "local", get_thread_nums_tss().local_thread_num + , "pool", get_thread_nums_tss().thread_pool_num + , "parent offset", parent_pool_->get_thread_offset() + , parent_pool_->get_pool_name()); + // clang-format on + + // This is a task being injected from a thread on another + // pool - we can schedule on any thread available + thread_num = numa_holder_[0].thread_queue(0)->worker_next( + static_cast(num_workers_)); + } + else if (!round_robin_) /* thread parent */ + { + if constexpr (spq_deb.is_enabled()) + { + domain_num = d_lookup_[thread_num]; + q_index = q_lookup_[thread_num]; + + spq_deb.debug(debug::str<>("create_thread"), + "assign_work_thread_parent", "thread_num", thread_num, + "pool", parent_pool_->get_pool_name()); + } + } + else /*(round_robin)*/ + { + domain_num = d_lookup_[thread_num]; + q_index = q_lookup_[thread_num]; + spq_deb.debug(debug::str<>("create_thread"), + "assign_work_round_robin", "thread_num", thread_num, "pool", + parent_pool_->get_pool_name(), + typename thread_holder_type::queue_data_print( + numa_holder_[domain_num].thread_queue( + static_cast(q_index)))); + thread_num = + numa_holder_[domain_num] + .thread_queue(static_cast(q_index)) + ->worker_next(static_cast(num_workers_)); + } + thread_num = select_active_pu(thread_num); + // cppcheck-suppress redundantAssignment + domain_num = d_lookup_[thread_num]; //-V519 + // cppcheck-suppress redundantAssignment + q_index = q_lookup_[thread_num]; //-V519 + } + + void create_thread_thread(std::size_t& thread_num, + std::size_t local_num, std::size_t& domain_num, + std::size_t& q_index) + { + auto msg = spq_deb.declare_variable(nullptr); + + spq_deb.set(msg, "HINT_THREAD"); + // @TODO. We should check that the thread num is valid + // Create thread on requested worker thread + thread_num = select_active_pu(data.schedulehint.hint); + domain_num = d_lookup_[thread_num]; + q_index = q_lookup_[thread_num]; + } + + void create_thread_numa(thread_init_data& data, std::size_t& thread_num, + std::size_t local_num, std::size_t& domain_num, + std::size_t& q_index) + { + auto msg = spq_deb.declare_variable(nullptr); + + // Create thread on requested NUMA domain + spq_deb.set(msg, "HINT_NUMA "); + // TODO: This case does not handle suspended PUs. + domain_num = fast_mod(data.schedulehint.hint, num_domains_); + // if the thread creating the new task is on the domain + // assigned to the new task - try to reuse the core as well + if (local_num != static_cast(-1) && + d_lookup_[local_num] == domain_num) + { + thread_num = local_num; //-V1048 + q_index = q_lookup_[thread_num]; + } + else + { + // first queue on this domain + thread_num = q_offset_[domain_num]; + // offset by some counter, modules num queues on domain + thread_num += + numa_holder_[domain_num].thread_queue(0)->worker_next( + q_counts_[domain_num]); + q_index = q_lookup_[thread_num]; + } + } + // ------------------------------------------------------------ // create a new thread and schedule it if the initial state is equal to // pending @@ -312,8 +416,8 @@ namespace hpx::threads::policies { std::size_t const local_num = local_thread_number(); std::size_t thread_num = local_num; - std::size_t domain_num; - std::size_t q_index; + std::size_t domain_num = 0; + std::size_t q_index = 0; auto msg = spq_deb.declare_variable(nullptr); @@ -321,99 +425,19 @@ namespace hpx::threads::policies { switch (data.schedulehint.mode) { case thread_schedule_hint_mode::none: - { - spq_deb.set(msg, "HINT_NONE "); - // Create thread on this worker thread if possible - if (local_num == static_cast(-1)) - { - // clang-format off - using namespace hpx::threads::detail; - spq_deb.debug(debug::str<>("create_thread") - , "x-pool", "num_workers_", num_workers_ - , "thread_number" - , "global", get_thread_nums_tss().global_thread_num - , "local", get_thread_nums_tss().local_thread_num - , "pool", get_thread_nums_tss().thread_pool_num - , "parent offset", parent_pool_->get_thread_offset() - , parent_pool_->get_pool_name()); - // clang-format on - - // This is a task being injected from a thread on another - // pool - we can schedule on any thread available - thread_num = numa_holder_[0].thread_queue(0)->worker_next( - static_cast(num_workers_)); - } - else if (!round_robin_) /* thread parent */ - { - if constexpr (spq_deb.is_enabled()) - { - domain_num = d_lookup_[thread_num]; - q_index = q_lookup_[thread_num]; - - spq_deb.debug(debug::str<>("create_thread"), - "assign_work_thread_parent", "thread_num", - thread_num, "pool", parent_pool_->get_pool_name()); - } - } - else /*(round_robin)*/ - { - domain_num = d_lookup_[thread_num]; - q_index = q_lookup_[thread_num]; - spq_deb.debug(debug::str<>("create_thread"), - "assign_work_round_robin", "thread_num", thread_num, - "pool", parent_pool_->get_pool_name(), - typename thread_holder_type::queue_data_print( - numa_holder_[domain_num].thread_queue( - static_cast(q_index)))); - thread_num = - numa_holder_[domain_num] - .thread_queue(static_cast(q_index)) - ->worker_next( - static_cast(num_workers_)); - } - thread_num = select_active_pu(thread_num); - // cppcheck-suppress redundantAssignment - domain_num = d_lookup_[thread_num]; //-V519 - // cppcheck-suppress redundantAssignment - q_index = q_lookup_[thread_num]; //-V519 + create_thread_none(thread_num, local_num, domain_num, q_index); break; - } + case thread_schedule_hint_mode::thread: - { - spq_deb.set(msg, "HINT_THREAD"); - // @TODO. We should check that the thread num is valid - // Create thread on requested worker thread - thread_num = select_active_pu(data.schedulehint.hint); - domain_num = d_lookup_[thread_num]; - q_index = q_lookup_[thread_num]; + create_thread_thread( + thread_num, local_num, domain_num, q_index); break; - } + case thread_schedule_hint_mode::numa: - { - // Create thread on requested NUMA domain - spq_deb.set(msg, "HINT_NUMA "); - // TODO: This case does not handle suspended PUs. - domain_num = fast_mod(data.schedulehint.hint, num_domains_); - // if the thread creating the new task is on the domain - // assigned to the new task - try to reuse the core as well - if (local_num != static_cast(-1) && - d_lookup_[local_num] == domain_num) - { - thread_num = local_num; //-V1048 - q_index = q_lookup_[thread_num]; - } - else - { - // first queue on this domain - thread_num = q_offset_[domain_num]; - // offset by some counter, modules num queues on domain - thread_num += - numa_holder_[domain_num].thread_queue(0)->worker_next( - q_counts_[domain_num]); - q_index = q_lookup_[thread_num]; - } + create_thread_numa( + data, thread_num, local_num, domain_num, q_index); break; - } + default: HPX_THROW_EXCEPTION(hpx::error::bad_parameter, "shared_priority_queue_scheduler::create_thread", @@ -711,6 +735,95 @@ namespace hpx::threads::policies { return !added_tasks; } + void schedule_thread_none(threads::thread_id_ref_type thrd, + std::size_t& thread_num, std::size_t local_num, + std::size_t& domain_num, std::size_t& q_index, bool allow_fallback) + { + auto msg = spq_deb.declare_variable(nullptr); + + // Create thread on this worker thread if possible + spq_deb.set(msg, "HINT_NONE "); + if (local_num == static_cast(-1)) + { + // This is a task being injected from a thread on another + // pool - we can schedule on any thread available + thread_num = + numa_holder_[0].thread_queue(0)->worker_next(num_workers_); + q_index = 0; + // clang-format off + using namespace hpx::threads::detail; + spq_deb.debug(debug::str<>("schedule_thread") + , "x-pool thread schedule" + , "num_workers_", num_workers_ + , "thread_number" + , "global", get_thread_nums_tss().global_thread_num + , "local", get_thread_nums_tss().local_thread_num + , "pool", get_thread_nums_tss().thread_pool_num + , "parent offset", parent_pool_->get_thread_offset() + , parent_pool_->get_pool_name(), + debug::threadinfo(&thrd)); + // clang-format on + } + else if (!round_robin_) /*assign_parent*/ + { + domain_num = d_lookup_[thread_num]; + q_index = q_lookup_[thread_num]; + spq_deb.debug(debug::str<>("schedule_thread"), + "assign_work_thread_parent", "thread_num", thread_num, + debug::threadinfo(&thrd)); + } + else /*(round_robin_)*/ + { + domain_num = d_lookup_[thread_num]; + q_index = q_lookup_[thread_num]; + thread_num = + numa_holder_[domain_num].thread_queue(q_index)->worker_next( + num_workers_); + spq_deb.debug(debug::str<>("schedule_thread"), + "assign_work_round_robin", "thread_num", thread_num, + debug::threadinfo(&thrd)); + } + thread_num = select_active_pu(thread_num, allow_fallback); + } + + void schedule_thread_thread(std::size_t& thread_num, + std::size_t& domain_num, std::size_t& q_index, bool allow_fallback, + threads::thread_schedule_hint schedulehint) + { + auto msg = spq_deb.declare_variable(nullptr); + + // @TODO. We should check that the thread num is valid + // Create thread on requested worker thread + spq_deb.set(msg, "HINT_THREAD"); + spq_deb.debug(debug::str<>("schedule_thread"), + "received HINT_THREAD", debug::dec<3>(schedulehint.hint)); + thread_num = select_active_pu(schedulehint.hint, allow_fallback); + domain_num = d_lookup_[thread_num]; + q_index = q_lookup_[thread_num]; + } + + void schedule_thread_numa(std::size_t thread_num, + std::size_t& domain_num, std::size_t& q_index, + threads::thread_schedule_hint schedulehint) const + { + auto msg = spq_deb.declare_variable(nullptr); + + // Create thread on requested NUMA domain + spq_deb.set(msg, "HINT_NUMA "); + // TODO: This case does not handle suspended PUs. + domain_num = fast_mod(schedulehint.hint, num_domains_); + // if the thread creating the new task is on the domain + // assigned to the new task - try to reuse the core as well + if (d_lookup_[thread_num] == domain_num) + { + q_index = q_lookup_[thread_num]; + } + else + { + throw std::runtime_error("counter problem in thread scheduler"); + } + } + // Schedule the passed thread void schedule_thread(threads::thread_id_ref_type thrd, threads::thread_schedule_hint schedulehint, bool allow_fallback, @@ -732,84 +845,19 @@ namespace hpx::threads::policies { switch (schedulehint.mode) { case thread_schedule_hint_mode::none: - { - // Create thread on this worker thread if possible - spq_deb.set(msg, "HINT_NONE "); - if (local_num == static_cast(-1)) - { - // This is a task being injected from a thread on another - // pool - we can schedule on any thread available - thread_num = numa_holder_[0].thread_queue(0)->worker_next( - num_workers_); - q_index = 0; - // clang-format off - using namespace hpx::threads::detail; - spq_deb.debug(debug::str<>("schedule_thread") - , "x-pool thread schedule" - , "num_workers_", num_workers_ - , "thread_number" - , "global", get_thread_nums_tss().global_thread_num - , "local", get_thread_nums_tss().local_thread_num - , "pool", get_thread_nums_tss().thread_pool_num - , "parent offset", parent_pool_->get_thread_offset() - , parent_pool_->get_pool_name(), - debug::threadinfo(&thrd)); - // clang-format on - } - else if (!round_robin_) /*assign_parent*/ - { - domain_num = d_lookup_[thread_num]; - q_index = q_lookup_[thread_num]; - spq_deb.debug(debug::str<>("schedule_thread"), - "assign_work_thread_parent", "thread_num", thread_num, - debug::threadinfo(&thrd)); - } - else /*(round_robin_)*/ - { - domain_num = d_lookup_[thread_num]; - q_index = q_lookup_[thread_num]; - thread_num = numa_holder_[domain_num] - .thread_queue(q_index) - ->worker_next(num_workers_); - spq_deb.debug(debug::str<>("schedule_thread"), - "assign_work_round_robin", "thread_num", thread_num, - debug::threadinfo(&thrd)); - } - thread_num = select_active_pu(thread_num, allow_fallback); + schedule_thread_none(thrd, thread_num, local_num, domain_num, + q_index, allow_fallback); break; - } + case thread_schedule_hint_mode::thread: - { - // @TODO. We should check that the thread num is valid - // Create thread on requested worker thread - spq_deb.set(msg, "HINT_THREAD"); - spq_deb.debug(debug::str<>("schedule_thread"), - "received HINT_THREAD", debug::dec<3>(schedulehint.hint)); - thread_num = - select_active_pu(schedulehint.hint, allow_fallback); - domain_num = d_lookup_[thread_num]; - q_index = q_lookup_[thread_num]; + schedule_thread_thread(thread_num, domain_num, q_index, + allow_fallback, schedulehint); break; - } + case thread_schedule_hint_mode::numa: - { - // Create thread on requested NUMA domain - spq_deb.set(msg, "HINT_NUMA "); - // TODO: This case does not handle suspended PUs. - domain_num = fast_mod(schedulehint.hint, num_domains_); - // if the thread creating the new task is on the domain - // assigned to the new task - try to reuse the core as well - if (d_lookup_[thread_num] == domain_num) - { - q_index = q_lookup_[thread_num]; - } - else - { - throw std::runtime_error( - "counter problem in thread scheduler"); - } + schedule_thread_numa(thread_num, domain_num, q_index, + allow_fallback, schedulehint); break; - } default: HPX_THROW_EXCEPTION(hpx::error::bad_parameter, diff --git a/libs/core/thread_pools/src/detail/background_thread.cpp b/libs/core/thread_pools/src/detail/background_thread.cpp index 4982df481111..ff5727edcb6e 100644 --- a/libs/core/thread_pools/src/detail/background_thread.cpp +++ b/libs/core/thread_pools/src/detail/background_thread.cpp @@ -118,7 +118,8 @@ namespace hpx::threads::detail { // allow to change the state the thread will be switched to after // execution - thread_state operator=(thread_result_type&& new_state) noexcept + switch_status_background& operator=( + thread_result_type&& new_state) noexcept { prev_state_ = thread_state( new_state.first, prev_state_.state_ex(), prev_state_.tag() + 1); @@ -126,7 +127,7 @@ namespace hpx::threads::detail { { next_thread_id_ = HPX_MOVE(new_state.second); } - return prev_state_; + return *this; } // Get the state this thread was in before execution (usually pending), diff --git a/libs/core/threading_base/include/hpx/threading_base/detail/switch_status.hpp b/libs/core/threading_base/include/hpx/threading_base/detail/switch_status.hpp index 6a735c0f8f0c..f9d4b47d9238 100644 --- a/libs/core/threading_base/include/hpx/threading_base/detail/switch_status.hpp +++ b/libs/core/threading_base/include/hpx/threading_base/detail/switch_status.hpp @@ -48,7 +48,7 @@ namespace hpx::threads::detail { // allow to change the state the thread will be switched to after // execution - thread_state operator=(thread_result_type&& new_state) noexcept + switch_status& operator=(thread_result_type&& new_state) noexcept { prev_state_ = thread_state( new_state.first, prev_state_.state_ex(), prev_state_.tag() + 1); @@ -56,7 +56,7 @@ namespace hpx::threads::detail { { next_thread_id_ = HPX_MOVE(new_state.second); } - return prev_state_; + return *this; } // Get the state this thread was in before execution (usually pending), diff --git a/libs/core/threading_base/src/register_thread.cpp b/libs/core/threading_base/src/register_thread.cpp index b8184162d40b..226c970e6235 100644 --- a/libs/core/threading_base/src/register_thread.cpp +++ b/libs/core/threading_base/src/register_thread.cpp @@ -28,9 +28,11 @@ namespace hpx::threads { // run and free all registered exit functions for this thread auto* p = get_self_id_data(); - - p->run_thread_exit_callbacks(); - p->free_thread_exit_callbacks(); + if (HPX_LIKELY(p != nullptr)) + { + p->run_thread_exit_callbacks(); + p->free_thread_exit_callbacks(); + } return threads::thread_result_type( threads::thread_schedule_state::terminated, diff --git a/libs/core/threading_base/src/set_thread_state.cpp b/libs/core/threading_base/src/set_thread_state.cpp index 76370c1ad254..0e388f9d8bf9 100644 --- a/libs/core/threading_base/src/set_thread_state.cpp +++ b/libs/core/threading_base/src/set_thread_state.cpp @@ -61,6 +61,36 @@ namespace hpx::threads::detail { return {thread_schedule_state::terminated, invalid_thread_id}; } + namespace { + + thread_state reschedule_active(thread_id_type const& thrd, + thread_schedule_state new_state, thread_restart_state new_state_ex, + thread_priority priority, thread_state previous_state, + error_code& ec) + { + // schedule a new thread to set the state + LTM_(warning).format( + "set_thread_state: thread is currently active, " + "scheduling new thread, thread({}), description({}), " + "new state({})", + thrd, get_thread_id_data(thrd)->get_description(), + get_thread_state_name(new_state)); + + thread_init_data data( + hpx::bind(&set_active_state, thread_id_ref_type(thrd), + new_state, new_state_ex, priority, previous_state), + "set state for active thread", priority); + + create_work( + get_thread_id_data(thrd)->get_scheduler_base(), data, ec); + + if (&ec != &throws) + ec = make_success_code(); + + return previous_state; // done + } + } // namespace + /////////////////////////////////////////////////////////////////////////// thread_state set_thread_state(thread_id_type const& thrd, thread_schedule_state new_state, thread_restart_state new_state_ex, @@ -118,45 +148,22 @@ namespace hpx::threads::detail { { if (retry_on_active) { - // schedule a new thread to set the state - LTM_(warning).format( - "set_thread_state: thread is currently active, " - "scheduling new thread, thread({}), description({}), " - "new state({})", - thrd, get_thread_id_data(thrd)->get_description(), - get_thread_state_name(new_state)); - - thread_init_data data( - hpx::bind(&set_active_state, thread_id_ref_type(thrd), - new_state, new_state_ex, priority, previous_state), - "set state for active thread", priority); - - create_work(get_thread_id_data(thrd)->get_scheduler_base(), - data, ec); - - if (&ec != &throws) - ec = make_success_code(); + return reschedule_active(thrd, new_state, new_state_ex, + priority, previous_state, ec); } - else - { - hpx::execution_base::this_thread::yield_k( - k, "hpx::threads::detail::set_thread_state"); - ++k; - - LTM_(warning).format( - "set_thread_state: thread is currently active, but not " - "scheduling new thread because retry_on_active = " - "false, thread({}), description({}), new state({})", - thrd, get_thread_id_data(thrd)->get_description(), - get_thread_state_name(new_state)); - continue; - } + hpx::execution_base::this_thread::yield_k( + k, "hpx::threads::detail::set_thread_state"); + ++k; - if (&ec != &throws) - ec = make_success_code(); + LTM_(warning).format( + "set_thread_state: thread is currently active, but not " + "scheduling new thread because retry_on_active = " + "false, thread({}), description({}), new state({})", + thrd, get_thread_id_data(thrd)->get_description(), + get_thread_state_name(new_state)); - return previous_state; // done + continue; } case thread_schedule_state::deleted: [[fallthrough]]; diff --git a/libs/full/init_runtime/src/hpx_init.cpp b/libs/full/init_runtime/src/hpx_init.cpp index 92485a237984..eb28a983f2f0 100644 --- a/libs/full/init_runtime/src/hpx_init.cpp +++ b/libs/full/init_runtime/src/hpx_init.cpp @@ -968,15 +968,12 @@ namespace hpx { // Command line handling should have updated this by now. HPX_ASSERT(cmdline.rtcfg_.mode_ != runtime_mode::default_); - switch (cmdline.rtcfg_.mode_) - { - case runtime_mode::local: + if (cmdline.rtcfg_.mode_ == runtime_mode::local) { LPROGRESS_ << "creating local runtime"; rt.reset(new hpx::runtime(cmdline.rtcfg_, true)); - break; } - default: + else { #if defined(HPX_HAVE_DISTRIBUTED_RUNTIME) for (auto const& registry : component_registries) @@ -989,7 +986,6 @@ namespace hpx { LPROGRESS_ << "creating distributed runtime"; rt.reset(new hpx::runtime_distributed(cmdline.rtcfg_, &hpx::detail::pre_main, &hpx::detail::post_main)); - break; #else HPX_THROW_EXCEPTION(hpx::error::invalid_status, "run_or_start", @@ -1000,10 +996,8 @@ namespace hpx { "Recompile HPX with HPX_WITH_DISTRIBUTED_RUNTIME=ON or " "change the runtime mode.", get_runtime_mode_name(cmdline.rtcfg_.mode_)); - break; #endif } - } // Store application defined command line options rt->set_app_options(params.desc_cmdline); diff --git a/libs/full/parcelport_tcp/src/connection_handler_tcp.cpp b/libs/full/parcelport_tcp/src/connection_handler_tcp.cpp index a6f997dd44f4..e4e81a285c38 100644 --- a/libs/full/parcelport_tcp/src/connection_handler_tcp.cpp +++ b/libs/full/parcelport_tcp/src/connection_handler_tcp.cpp @@ -81,6 +81,7 @@ namespace hpx::parcelset::policies::tcp { connection_handler::~connection_handler() { HPX_ASSERT(acceptor_ == nullptr); + delete acceptor_; // silence overeager security reports } bool connection_handler::do_run() diff --git a/libs/full/performance_counters/include/hpx/performance_counters/registry.hpp b/libs/full/performance_counters/include/hpx/performance_counters/registry.hpp index c5d2d854524a..4fd3c1bb31b9 100644 --- a/libs/full/performance_counters/include/hpx/performance_counters/registry.hpp +++ b/libs/full/performance_counters/include/hpx/performance_counters/registry.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2007-2022 Hartmut Kaiser +// Copyright (c) 2007-2024 Hartmut Kaiser // // SPDX-License-Identifier: BSL-1.0 // Distributed under the Boost Software License, Version 1.0. (See accompanying @@ -56,7 +56,7 @@ namespace hpx { namespace performance_counters { /// \brief Call the supplied function for all registered counter types. counter_status discover_counter_types( discover_counter_func discover_counter, discover_counters_mode mode, - error_code& ec = throws); + error_code& ec = throws) const; /// \brief Call the supplied function for the given registered counter type. counter_status discover_counter_type(std::string const& fullname, diff --git a/libs/full/performance_counters/include/hpx/performance_counters/server/arithmetics_counter_extended.hpp b/libs/full/performance_counters/include/hpx/performance_counters/server/arithmetics_counter_extended.hpp index 8d108844d88e..52eaed78d1cf 100644 --- a/libs/full/performance_counters/include/hpx/performance_counters/server/arithmetics_counter_extended.hpp +++ b/libs/full/performance_counters/include/hpx/performance_counters/server/arithmetics_counter_extended.hpp @@ -17,7 +17,7 @@ #include /////////////////////////////////////////////////////////////////////////////// -namespace hpx { namespace performance_counters { namespace server { +namespace hpx::performance_counters::server { /////////////////////////////////////////////////////////////////////////// // This counter exposes the result of an arithmetic operation The counter @@ -56,4 +56,4 @@ namespace hpx { namespace performance_counters { namespace server { // base counters to be queried performance_counter_set counters_; }; -}}} // namespace hpx::performance_counters::server +} // namespace hpx::performance_counters::server diff --git a/libs/full/performance_counters/src/registry.cpp b/libs/full/performance_counters/src/registry.cpp index b8263cc98da6..34ff266e2e4d 100644 --- a/libs/full/performance_counters/src/registry.cpp +++ b/libs/full/performance_counters/src/registry.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2007-2022 Hartmut Kaiser +// Copyright (c) 2007-2024 Hartmut Kaiser // Copyright (c) 2011 Bryce Lelbach // // SPDX-License-Identifier: BSL-1.0 @@ -47,7 +47,7 @@ namespace hpx { namespace performance_counters { registry::counter_type_map_type::iterator registry::locate_counter_type( std::string const& type_name) { - counter_type_map_type::iterator it = countertypes_.find(type_name); + auto it = countertypes_.find(type_name); if (it == countertypes_.end()) { // if the full type is not available, try to locate the object name @@ -64,8 +64,7 @@ namespace hpx { namespace performance_counters { registry::counter_type_map_type::const_iterator registry::locate_counter_type(std::string const& type_name) const { - counter_type_map_type::const_iterator it = - countertypes_.find(type_name); + auto it = countertypes_.find(type_name); if (it == countertypes_.end()) { // if the full type is not available, try to locate the object name @@ -91,7 +90,7 @@ namespace hpx { namespace performance_counters { if (!status_is_valid(status)) return status; - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it != countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -131,17 +130,15 @@ namespace hpx { namespace performance_counters { if (type_name.find_first_of("*?[]") == std::string::npos) { - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { // compose a list of known counter types std::string types; - counter_type_map_type::const_iterator end = countertypes_.end(); - for (counter_type_map_type::const_iterator it = - countertypes_.begin(); - it != end; ++it) + auto end = countertypes_.end(); + for (auto it_ct = countertypes_.begin(); it_ct != end; ++it_ct) { - types += " " + (*it).first + "\n"; + types += " " + it_ct->first + "\n"; } HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -158,11 +155,11 @@ namespace hpx { namespace performance_counters { &expand_counter_info, _1, discover_counter, std::ref(ec)); } - counter_info info = (*it).second.info_; + counter_info info = it->second.info_; info.fullname_ = fullname; - if (!(*it).second.discover_counters_.empty() && - !(*it).second.discover_counters_( + if (!it->second.discover_counters_.empty() && + !it->second.discover_counters_( info, discover_counter, mode, ec)) { return counter_status::invalid_data; @@ -195,17 +192,17 @@ namespace hpx { namespace performance_counters { countertypes_.begin(); it != end; ++it) { - if (!std::regex_match((*it).first, rx)) + if (!std::regex_match(it->first, rx)) continue; found_one = true; // propagate parameters - counter_info info = (*it).second.info_; + counter_info info = it->second.info_; if (!p.parameters_.empty()) info.fullname_ += "@" + p.parameters_; - if (!(*it).second.discover_counters_.empty() && - !(*it).second.discover_counters_( + if (!it->second.discover_counters_.empty() && + !it->second.discover_counters_( info, discover_counter, mode, ec)) { return counter_status::invalid_data; @@ -216,12 +213,10 @@ namespace hpx { namespace performance_counters { { // compose a list of known counter types std::string types; - counter_type_map_type::const_iterator end = countertypes_.end(); - for (counter_type_map_type::const_iterator it = - countertypes_.begin(); - it != end; ++it) + auto end_ct = countertypes_.end(); + for (auto it = countertypes_.begin(); it != end_ct; ++it) { - types += " " + (*it).first + "\n"; + types += " " + it->first + "\n"; } HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -242,7 +237,7 @@ namespace hpx { namespace performance_counters { /// \brief Call the supplied function for all registered counter types. counter_status registry::discover_counter_types( discover_counter_func discover_counter, discover_counters_mode mode, - error_code& ec) + error_code& ec) const { // Introducing this temporary silence a report about a potential memory // from clang's static analyzer @@ -258,11 +253,10 @@ namespace hpx { namespace performance_counters { discover_counter_ = HPX_MOVE(discover_counter); } - for (counter_type_map_type::value_type const& d : countertypes_) + for (auto const& [k, v] : countertypes_) { - if (!d.second.discover_counters_.empty() && - !d.second.discover_counters_( - d.second.info_, discover_counter_, mode, ec)) + if (!v.discover_counters_.empty() && + !v.discover_counters_(v.info_, discover_counter_, mode, ec)) { return counter_status::invalid_data; } @@ -286,18 +280,15 @@ namespace hpx { namespace performance_counters { if (!status_is_valid(status)) return status; - counter_type_map_type::const_iterator it = - locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { // compose a list of known counter types std::string types; - counter_type_map_type::const_iterator end = countertypes_.end(); - for (counter_type_map_type::const_iterator it = - countertypes_.begin(); - it != end; ++it) + auto end = countertypes_.end(); + for (auto it_ct = countertypes_.begin(); it_ct != end; ++it_ct) { - types += " " + (*it).first + "\n"; + types += " " + it_ct->first + "\n"; } HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -307,7 +298,7 @@ namespace hpx { namespace performance_counters { return counter_status::counter_type_unknown; } - if ((*it).second.create_counter_.empty()) + if (it->second.create_counter_.empty()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "registry::get_counter_create_function", @@ -315,7 +306,7 @@ namespace hpx { namespace performance_counters { return counter_status::invalid_data; } - func = (*it).second.create_counter_; + func = it->second.create_counter_; if (&ec != &throws) ec = make_success_code(); @@ -334,8 +325,7 @@ namespace hpx { namespace performance_counters { if (!status_is_valid(status)) return status; - counter_type_map_type::const_iterator it = - locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -344,7 +334,7 @@ namespace hpx { namespace performance_counters { return counter_status::counter_type_unknown; } - if ((*it).second.discover_counters_.empty()) + if (it->second.discover_counters_.empty()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "registry::get_counter_discovery_function", @@ -353,7 +343,7 @@ namespace hpx { namespace performance_counters { return counter_status::invalid_data; } - func = (*it).second.discover_counters_; + func = it->second.discover_counters_; if (&ec != &throws) ec = make_success_code(); @@ -371,7 +361,7 @@ namespace hpx { namespace performance_counters { if (!status_is_valid(status)) return status; - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -426,6 +416,15 @@ namespace hpx { namespace performance_counters { return create_raw_counter(info, func, id, ec); } + namespace { + + constexpr bool is_not_counter_type( + counter_type ct, counter_type ct1, counter_type ct2) noexcept + { + return ct != ct1 || ct != ct2; + } + } // namespace + counter_status registry::create_raw_counter(counter_info const& info, hpx::function const& f, naming::gid_type& id, error_code& ec) @@ -437,7 +436,7 @@ namespace hpx { namespace performance_counters { if (!status_is_valid(status)) return status; - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -447,19 +446,18 @@ namespace hpx { namespace performance_counters { } // make sure the counter type requested is supported - if ((counter_type::raw != (*it).second.info_.type_ || - counter_type::raw != info.type_) && - (counter_type::monotonically_increasing != - (*it).second.info_.type_ || - counter_type::monotonically_increasing != info.type_) && - (counter_type::aggregating != (*it).second.info_.type_ || - counter_type::aggregating != info.type_) && - (counter_type::elapsed_time != (*it).second.info_.type_ || - counter_type::elapsed_time != info.type_) && - (counter_type::average_count != (*it).second.info_.type_ || - counter_type::average_count != info.type_) && - (counter_type::average_timer != (*it).second.info_.type_ || - counter_type::average_timer != info.type_)) + if (is_not_counter_type( + counter_type::raw, it->second.info_.type_, info.type_) && + is_not_counter_type(counter_type::monotonically_increasing, + it->second.info_.type_, info.type_) && + is_not_counter_type(counter_type::aggregating, + it->second.info_.type_, info.type_) && + is_not_counter_type(counter_type::elapsed_time, + it->second.info_.type_, info.type_) && + is_not_counter_type(counter_type::average_count, + it->second.info_.type_, info.type_) && + is_not_counter_type(counter_type::average_timer, + it->second.info_.type_, info.type_)) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "registry::create_raw_counter", @@ -473,14 +471,14 @@ namespace hpx { namespace performance_counters { // make sure parent instance name is set properly counter_info complemented_info = info; - complement_counter_info(complemented_info, (*it).second.info_, ec); + complement_counter_info(complemented_info, it->second.info_, ec); if (ec) return counter_status::invalid_data; // create the counter as requested try { - typedef components::component counter_t; + using counter_t = components::component; id = components::server::construct(complemented_info, f); } catch (hpx::exception const& e) @@ -522,7 +520,7 @@ namespace hpx { namespace performance_counters { if (!status_is_valid(status)) return status; - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -532,9 +530,9 @@ namespace hpx { namespace performance_counters { } // make sure the counter type requested is supported - if (!((counter_type::histogram == (*it).second.info_.type_ && + if (!((counter_type::histogram == it->second.info_.type_ && counter_type::histogram == info.type_) || - (counter_type::raw_values == (*it).second.info_.type_ && + (counter_type::raw_values == it->second.info_.type_ && counter_type::raw_values == info.type_))) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -546,14 +544,14 @@ namespace hpx { namespace performance_counters { // make sure parent instance name is set properly counter_info complemented_info = info; - complement_counter_info(complemented_info, (*it).second.info_, ec); + complement_counter_info(complemented_info, it->second.info_, ec); if (ec) return counter_status::invalid_data; // create the counter as requested try { - typedef components::component counter_t; + using counter_t = components::component; id = components::server::construct(complemented_info, f); } catch (hpx::exception const& e) @@ -586,7 +584,7 @@ namespace hpx { namespace performance_counters { if (!status_is_valid(status)) return status; - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -597,7 +595,7 @@ namespace hpx { namespace performance_counters { // make sure parent instance name is set properly counter_info complemented_info = info; - complement_counter_info(complemented_info, (*it).second.info_, ec); + complement_counter_info(complemented_info, it->second.info_, ec); if (ec) return counter_status::invalid_data; @@ -608,8 +606,8 @@ namespace hpx { namespace performance_counters { { case counter_type::elapsed_time: { - typedef components::component - counter_t; + using counter_t = + components::component; id = components::server::construct(complemented_info); } @@ -671,7 +669,7 @@ namespace hpx { namespace performance_counters { if (!status_is_valid(status)) return status; - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -681,7 +679,7 @@ namespace hpx { namespace performance_counters { } // make sure the requested counter type is supported - if (counter_type::aggregating != (*it).second.info_.type_ || + if (counter_type::aggregating != it->second.info_.type_ || counter_type::aggregating != info.type_) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -694,7 +692,7 @@ namespace hpx { namespace performance_counters { // make sure parent instance name is set properly counter_info complemented_info = info; - complement_counter_info(complemented_info, (*it).second.info_, ec); + complement_counter_info(complemented_info, it->second.info_, ec); if (ec) return counter_status::invalid_data; @@ -717,13 +715,12 @@ namespace hpx { namespace performance_counters { // create base counter only if it does not exist yet if (p.countername_ == "average") { - typedef hpx::components::component< + using counter_t = hpx::components::component< hpx::performance_counters::server::statistics_counter< - boost::accumulators::tag::mean>> - counter_t; + boost::accumulators::tag::mean>>; if (parameters.size() > 1) - reset_base_counter = (parameters[1] != 0) ? true : false; + reset_base_counter = parameters[1] != 0; gid = components::server::construct( complemented_info, base_counter_name, sample_interval, 0, @@ -731,13 +728,12 @@ namespace hpx { namespace performance_counters { } else if (p.countername_ == "stddev") { - typedef hpx::components::component< + using counter_t = hpx::components::component< hpx::performance_counters::server::statistics_counter< - boost::accumulators::tag::variance>> - counter_t; + boost::accumulators::tag::variance>>; if (parameters.size() > 1) - reset_base_counter = (parameters[1] != 0) ? true : false; + reset_base_counter = parameters[1] != 0; gid = components::server::construct( complemented_info, base_counter_name, sample_interval, 0, @@ -745,17 +741,16 @@ namespace hpx { namespace performance_counters { } else if (p.countername_ == "rolling_average") { - typedef hpx::components::component< + using counter_t = hpx::components::component< hpx::performance_counters::server::statistics_counter< - boost::accumulators::tag::rolling_mean>> - counter_t; + boost::accumulators::tag::rolling_mean>>; std::size_t window_size = 10; // default rolling window size if (parameters.size() > 1) window_size = parameters[1]; if (parameters.size() > 2) - reset_base_counter = (parameters[2] != 0) ? true : false; + reset_base_counter = parameters[2] != 0; gid = components::server::construct( complemented_info, base_counter_name, sample_interval, @@ -763,17 +758,16 @@ namespace hpx { namespace performance_counters { } else if (p.countername_ == "rolling_stddev") { - typedef hpx::components::component< + using counter_t = hpx::components::component< hpx::performance_counters::server::statistics_counter< - boost::accumulators::tag::rolling_variance>> - counter_t; + boost::accumulators::tag::rolling_variance>>; std::size_t window_size = 10; // default rolling window size if (parameters.size() > 1) window_size = parameters[1]; if (parameters.size() > 2) - reset_base_counter = (parameters[2] != 0) ? true : false; + reset_base_counter = parameters[2] != 0; gid = components::server::construct( complemented_info, base_counter_name, sample_interval, @@ -781,13 +775,12 @@ namespace hpx { namespace performance_counters { } else if (p.countername_ == "median") { - typedef hpx::components::component< + using counter_t = hpx::components::component< hpx::performance_counters::server::statistics_counter< - boost::accumulators::tag::median>> - counter_t; + boost::accumulators::tag::median>>; if (parameters.size() > 1) - reset_base_counter = (parameters[1] != 0) ? true : false; + reset_base_counter = parameters[1] != 0; gid = components::server::construct( complemented_info, base_counter_name, sample_interval, 0, @@ -795,13 +788,12 @@ namespace hpx { namespace performance_counters { } else if (p.countername_ == "max") { - typedef hpx::components::component< + using counter_t = hpx::components::component< hpx::performance_counters::server::statistics_counter< - boost::accumulators::tag::max>> - counter_t; + boost::accumulators::tag::max>>; if (parameters.size() > 1) - reset_base_counter = (parameters[1] != 0) ? true : false; + reset_base_counter = parameters[1] != 0; gid = components::server::construct( complemented_info, base_counter_name, sample_interval, 0, @@ -809,13 +801,12 @@ namespace hpx { namespace performance_counters { } else if (p.countername_ == "min") { - typedef hpx::components::component< + using counter_t = hpx::components::component< hpx::performance_counters::server::statistics_counter< - boost::accumulators::tag::min>> - counter_t; + boost::accumulators::tag::min>>; if (parameters.size() > 1) - reset_base_counter = (parameters[1] != 0) ? true : false; + reset_base_counter = parameters[1] != 0; gid = components::server::construct( complemented_info, base_counter_name, sample_interval, 0, @@ -823,16 +814,16 @@ namespace hpx { namespace performance_counters { } else if (p.countername_ == "rolling_min") { - typedef hpx::components::component> - counter_t; + using counter_t = hpx::components::component< + hpx::performance_counters::server::statistics_counter< + hpx::util::tag::rolling_min>>; std::size_t window_size = 10; // default rolling window size if (parameters.size() > 1) window_size = parameters[1]; if (parameters.size() > 2) - reset_base_counter = (parameters[2] != 0) ? true : false; + reset_base_counter = parameters[2] != 0; gid = components::server::construct( complemented_info, base_counter_name, sample_interval, @@ -840,16 +831,16 @@ namespace hpx { namespace performance_counters { } else if (p.countername_ == "rolling_max") { - typedef hpx::components::component> - counter_t; + using counter_t = hpx::components::component< + hpx::performance_counters::server::statistics_counter< + hpx::util::tag::rolling_max>>; std::size_t window_size = 10; // default rolling window size if (parameters.size() > 1) window_size = parameters[1]; if (parameters.size() > 2) - reset_base_counter = (parameters[2] != 0) ? true : false; + reset_base_counter = parameters[2] != 0; gid = components::server::construct( complemented_info, base_counter_name, sample_interval, @@ -897,7 +888,7 @@ namespace hpx { namespace performance_counters { if (!status_is_valid(status)) return status; - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -907,7 +898,7 @@ namespace hpx { namespace performance_counters { } // make sure the requested counter type is supported - if (counter_type::aggregating != (*it).second.info_.type_ || + if (counter_type::aggregating != it->second.info_.type_ || counter_type::aggregating != info.type_) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -919,7 +910,7 @@ namespace hpx { namespace performance_counters { // make sure parent instance name is set properly counter_info complemented_info = info; - complement_counter_info(complemented_info, (*it).second.info_, ec); + complement_counter_info(complemented_info, it->second.info_, ec); if (ec) return counter_status::invalid_data; @@ -935,33 +926,33 @@ namespace hpx { namespace performance_counters { // create base counter only if it does not exist yet if (p.countername_ == "add") { - typedef hpx::components::component>> - counter_t; + using counter_t = + hpx::components::component>>; gid = components::server::construct( complemented_info, base_counter_names); } else if (p.countername_ == "subtract") { - typedef hpx::components::component>> - counter_t; + using counter_t = + hpx::components::component>>; gid = components::server::construct( complemented_info, base_counter_names); } else if (p.countername_ == "multiply") { - typedef hpx::components::component>> - counter_t; + using counter_t = hpx::components::component< + hpx::performance_counters::server::arithmetics_counter< + std::multiplies>>; gid = components::server::construct( complemented_info, base_counter_names); } else if (p.countername_ == "divide") { - typedef hpx::components::component>> - counter_t; + using counter_t = + hpx::components::component>>; gid = components::server::construct( complemented_info, base_counter_names); } @@ -1008,7 +999,7 @@ namespace hpx { namespace performance_counters { if (!status_is_valid(status)) return status; - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -1018,7 +1009,7 @@ namespace hpx { namespace performance_counters { } // make sure the requested counter type is supported - if (counter_type::aggregating != (*it).second.info_.type_ || + if (counter_type::aggregating != it->second.info_.type_ || counter_type::aggregating != info.type_) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -1030,7 +1021,7 @@ namespace hpx { namespace performance_counters { // make sure parent instance name is set properly counter_info complemented_info = info; - complement_counter_info(complemented_info, (*it).second.info_, ec); + complement_counter_info(complemented_info, it->second.info_, ec); if (ec) return counter_status::invalid_data; @@ -1046,55 +1037,50 @@ namespace hpx { namespace performance_counters { // create base counter only if it does not exist yet if (p.countername_ == "mean") { - typedef hpx::components::component> - counter_t; + using counter_t = + hpx::components::component>; gid = components::server::construct( complemented_info, base_counter_names); } else if (p.countername_ == "variance") { - typedef hpx::components::component< + using counter_t = hpx::components::component< performance_counters::server::arithmetics_counter_extended< - boost::accumulators::tag::variance>> - counter_t; + boost::accumulators::tag::variance>>; gid = components::server::construct( complemented_info, base_counter_names); } else if (p.countername_ == "median") { - typedef hpx::components::component< + using counter_t = hpx::components::component< performance_counters::server::arithmetics_counter_extended< - boost::accumulators::tag::median>> - counter_t; + boost::accumulators::tag::median>>; gid = components::server::construct( complemented_info, base_counter_names); } else if (p.countername_ == "min") { - typedef hpx::components::component< + using counter_t = hpx::components::component< performance_counters::server::arithmetics_counter_extended< - boost::accumulators::tag::min>> - counter_t; + boost::accumulators::tag::min>>; gid = components::server::construct( complemented_info, base_counter_names); } else if (p.countername_ == "max") { - typedef hpx::components::component< + using counter_t = hpx::components::component< performance_counters::server::arithmetics_counter_extended< - boost::accumulators::tag::max>> - counter_t; + boost::accumulators::tag::max>>; gid = components::server::construct( complemented_info, base_counter_names); } else if (p.countername_ == "count") { - typedef hpx::components::component< + using counter_t = hpx::components::component< performance_counters::server::arithmetics_counter_extended< - boost::accumulators::tag::count>> - counter_t; + boost::accumulators::tag::count>>; gid = components::server::construct( complemented_info, base_counter_names); } @@ -1146,7 +1132,7 @@ namespace hpx { namespace performance_counters { return status; // make sure the type of the new counter is known to the registry - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -1208,7 +1194,7 @@ namespace hpx { namespace performance_counters { return status; // make sure the type of the counter is known to the registry - counter_type_map_type::iterator it = locate_counter_type(type_name); + auto it = locate_counter_type(type_name); if (it == countertypes_.end()) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, @@ -1217,7 +1203,7 @@ namespace hpx { namespace performance_counters { return counter_status::counter_type_unknown; } - info = (*it).second.info_; + info = it->second.info_; if (&ec != &throws) ec = make_success_code(); diff --git a/libs/full/performance_counters/src/server/action_invocation_counter.cpp b/libs/full/performance_counters/src/server/action_invocation_counter.cpp index ac9bcdae0136..aa9f3b026c24 100644 --- a/libs/full/performance_counters/src/server/action_invocation_counter.cpp +++ b/libs/full/performance_counters/src/server/action_invocation_counter.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2007-2022 Hartmut Kaiser +// Copyright (c) 2007-2024 Hartmut Kaiser // // SPDX-License-Identifier: BSL-1.0 // Distributed under the Boost Software License, Version 1.0. (See accompanying @@ -15,7 +15,7 @@ #include /////////////////////////////////////////////////////////////////////////////// -namespace hpx { namespace performance_counters { +namespace hpx::performance_counters { /////////////////////////////////////////////////////////////////////////// // Discoverer function for action invocation counters @@ -68,47 +68,42 @@ namespace hpx { namespace performance_counters { hpx::actions::detail::invocation_count_registry& registry, error_code& ec) { - switch (info.type_) + if (info.type_ != counter_type::monotonically_increasing) { - case counter_type::monotonically_increasing: + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "action_invocation_counter_creator", + "invalid counter type requested"); + return naming::invalid_gid; + } + + counter_path_elements paths; + get_counter_path_elements(info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; + + if (paths.parentinstance_is_basename_) { - counter_path_elements paths; - get_counter_path_elements(info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; - - if (paths.parentinstance_is_basename_) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "action_invocation_counter_creator", - "invalid action invocation counter name (instance name " - "must not be a valid base counter name)"); - return naming::invalid_gid; - } - - if (paths.parameters_.empty()) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "action_invocation_counter_creator", - "invalid action invocation counter parameter: must " - "specify an action type"); - return naming::invalid_gid; - } - - // ask registry - hpx::function f = - registry.get_invocation_counter(paths.parameters_); - - return detail::create_raw_counter(info, HPX_MOVE(f), ec); + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "action_invocation_counter_creator", + "invalid action invocation counter name (instance name " + "must not be a valid base counter name)"); + return naming::invalid_gid; } - break; - default: + if (paths.parameters_.empty()) + { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "action_invocation_counter_creator", - "invalid counter type requested"); + "invalid action invocation counter parameter: must " + "specify an action type"); return naming::invalid_gid; } + + // ask registry + hpx::function f = + registry.get_invocation_counter(paths.parameters_); + + return detail::create_raw_counter(info, HPX_MOVE(f), ec); } naming::gid_type local_action_invocation_counter_creator( @@ -128,4 +123,4 @@ namespace hpx { namespace performance_counters { info, invocation_count_registry::remote_instance(), ec); } #endif -}} // namespace hpx::performance_counters +} // namespace hpx::performance_counters diff --git a/libs/full/performance_counters/src/server/arithmetics_counter.cpp b/libs/full/performance_counters/src/server/arithmetics_counter.cpp index d5ec5894d8b0..0775a3245351 100644 --- a/libs/full/performance_counters/src/server/arithmetics_counter.cpp +++ b/libs/full/performance_counters/src/server/arithmetics_counter.cpp @@ -230,66 +230,63 @@ namespace hpx::performance_counters::detail { naming::gid_type arithmetics_counter_creator( counter_info const& info, error_code& ec) { - switch (info.type_) + if (info.type_ != counter_type::aggregating) { - case counter_type::aggregating: + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "arithmetics_counter_creator", + "invalid counter type requested"); + + return naming::invalid_gid; + } + + counter_path_elements paths; + get_counter_path_elements(info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; + + if (!paths.parameters_.empty()) { - counter_path_elements paths; - get_counter_path_elements(info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; + // try to interpret the additional parameter as a list of + // two performance counter names + std::vector names; + hpx::string_util::split( + names, paths.parameters_, hpx::string_util::is_any_of(",")); - if (!paths.parameters_.empty()) + if (names.empty()) { - // try to interpret the additional parameter as a list of - // two performance counter names - std::vector names; - hpx::string_util::split( - names, paths.parameters_, hpx::string_util::is_any_of(",")); + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "arithmetics_counter_creator", + "the parameter specification for an arithmetic counter " + "has to expand to at least one counter name: {}", + paths.parameters_); + return naming::invalid_gid; + } - if (names.empty()) + for (std::string const& name : names) + { + if (counter_status::valid_data != + get_counter_path_elements(name, paths, ec) || + ec) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "arithmetics_counter_creator", - "the parameter specification for an arithmetic counter " - "has to expand to at least one counter name: {}", - paths.parameters_); + "the given (expanded) counter name is not " + "a validly formed performance counter name: {}", + name); return naming::invalid_gid; } - - for (std::string const& name : names) - { - if (counter_status::valid_data != - get_counter_path_elements(name, paths, ec) || - ec) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "arithmetics_counter_creator", - "the given (expanded) counter name is not " - "a validly formed performance counter name: {}", - name); - return naming::invalid_gid; - } - } - - return create_arithmetics_counter(info, names, ec); } - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "arithmetics_counter_creator", - "the parameter specification for an arithmetic counter " - "has to be a comma separated list of performance " - "counter names, none is given: {}", - remove_counter_prefix(info.fullname_)); + return create_arithmetics_counter(info, names, ec); } - break; - default: - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "arithmetics_counter_creator", - "invalid counter type requested"); - break; - } + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "arithmetics_counter_creator", + "the parameter specification for an arithmetic counter " + "has to be a comma separated list of performance " + "counter names, none is given: {}", + remove_counter_prefix(info.fullname_)); + return naming::invalid_gid; } } // namespace hpx::performance_counters::detail diff --git a/libs/full/performance_counters/src/server/arithmetics_counter_extended.cpp b/libs/full/performance_counters/src/server/arithmetics_counter_extended.cpp index c04ca8b7b67d..f75f04aea5df 100644 --- a/libs/full/performance_counters/src/server/arithmetics_counter_extended.cpp +++ b/libs/full/performance_counters/src/server/arithmetics_counter_extended.cpp @@ -275,74 +275,67 @@ HPX_DEFINE_GET_COMPONENT_TYPE(count_arithmetics_counter_type::wrapped_type) /////////////////////////////////////////////////////////////////////////////// namespace hpx::performance_counters::detail { - /// Creation function for aggregating performance counters to be registered - /// with the counter types. + // Creation function for aggregating performance counters to be registered + // with the counter types. naming::gid_type arithmetics_counter_extended_creator( counter_info const& info, error_code& ec) { - switch (info.type_) + if (info.type_ != counter_type::aggregating) { - case counter_type::aggregating: + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "arithmetics_counter_extended_creator", + "invalid counter type requested"); + return naming::invalid_gid; + } + + counter_path_elements paths; + get_counter_path_elements(info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; + + if (!paths.parameters_.empty()) { - counter_path_elements paths; - get_counter_path_elements(info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; + // try to interpret the additional parameter as a list of + // two performance counter names + std::vector names; + hpx::string_util::split( + names, paths.parameters_, hpx::string_util::is_any_of(",")); - if (!paths.parameters_.empty()) + if (names.empty()) { - // try to interpret the additional parameter as a list of - // two performance counter names - std::vector names; - hpx::string_util::split( - names, paths.parameters_, hpx::string_util::is_any_of(",")); + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "arithmetics_counter_extended_creator", + "the parameter specification for an arithmetic counter " + "has to expand to at least one counter name: {}", + paths.parameters_); + return naming::invalid_gid; + } - if (names.empty()) + for (std::string const& name : names) + { + counter_path_elements paths; + if (counter_status::valid_data != + get_counter_path_elements(name, paths, ec) || + ec) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "arithmetics_counter_extended_creator", - "the parameter specification for an arithmetic counter " - "has to expand to at least one counter name: {}", - paths.parameters_); + "the given (expanded) counter name is not " + "a validly formed performance counter name: {}", + name); return naming::invalid_gid; } - - for (std::string const& name : names) - { - counter_path_elements paths; - if (counter_status::valid_data != - get_counter_path_elements(name, paths, ec) || - ec) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "arithmetics_counter_extended_creator", - "the given (expanded) counter name is not " - "a validly formed performance counter name: {}", - name); - return naming::invalid_gid; - } - } - - return create_arithmetics_counter_extended(info, names, ec); - } - else - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "arithmetics_counter_extended_creator", - "the parameter specification for an arithmetic counter " - "has to be a comma separated list of performance " - "counter names, none is given: {}", - remove_counter_prefix(info.fullname_)); } - } - break; - default: - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "arithmetics_counter_extended_creator", - "invalid counter type requested"); - break; + return create_arithmetics_counter_extended(info, names, ec); } + + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "arithmetics_counter_extended_creator", + "the parameter specification for an arithmetic counter " + "has to be a comma separated list of performance " + "counter names, none is given: {}", + remove_counter_prefix(info.fullname_)); return naming::invalid_gid; } } // namespace hpx::performance_counters::detail diff --git a/libs/full/performance_counters/src/server/component_instance_counter.cpp b/libs/full/performance_counters/src/server/component_instance_counter.cpp index 1f03c02da41d..ece78e42297b 100644 --- a/libs/full/performance_counters/src/server/component_instance_counter.cpp +++ b/libs/full/performance_counters/src/server/component_instance_counter.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2007-2019 Hartmut Kaiser +// Copyright (c) 2007-2024 Hartmut Kaiser // // SPDX-License-Identifier: BSL-1.0 // Distributed under the Boost Software License, Version 1.0. (See accompanying @@ -17,7 +17,7 @@ #include /////////////////////////////////////////////////////////////////////////////// -namespace hpx { namespace performance_counters { namespace detail { +namespace hpx::performance_counters::detail { /////////////////////////////////////////////////////////////////////////// // Extract the current number of instances for the given component type @@ -31,68 +31,62 @@ namespace hpx { namespace performance_counters { namespace detail { naming::gid_type component_instance_counter_creator( counter_info const& info, error_code& ec) { - switch (info.type_) + if (info.type_ != counter_type::raw) { - case counter_type::raw: - { - counter_path_elements paths; - get_counter_path_elements(info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; - - if (paths.parentinstance_is_basename_) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "component_instance_counter_creator", - "invalid instance counter name (instance name must not " - "be a valid base counter name)"); - return naming::invalid_gid; - } - - if (paths.parameters_.empty()) - { - std::stringstream strm; - strm << "invalid instance counter parameter: must specify " - "a component type\n" - "known component types:\n"; + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "component_instance_counter_creator", + "invalid counter type requested"); + return naming::invalid_gid; + } - components::enumerate_instance_counts( - [&strm](components::component_type type) -> bool { - strm << " " << agas::get_component_type_name(type) - << "\n"; - return true; - }); + counter_path_elements paths; + get_counter_path_elements(info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "component_instance_counter_creator", strm.str()); + if (paths.parentinstance_is_basename_) + { + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "component_instance_counter_creator", + "invalid instance counter name (instance name must not " + "be a valid base counter name)"); + return naming::invalid_gid; + } - return naming::invalid_gid; - } + if (paths.parameters_.empty()) + { + std::stringstream strm; + strm << "invalid instance counter parameter: must specify " + "a component type\n" + "known component types:\n"; - // ask AGAS to resolve the component type - components::component_type type = - agas::get_component_id(paths.parameters_); + components::enumerate_instance_counts( + [&strm](components::component_type type) -> bool { + strm << " " << agas::get_component_type_name(type) << "\n"; + return true; + }); - if (type == to_int(hpx::components::component_enum_type::invalid)) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "component_instance_counter_creator", - "invalid component type as counter parameter: {}", - paths.parameters_); - return naming::invalid_gid; - } + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "component_instance_counter_creator", strm.str()); - hpx::function f = - hpx::bind_front(&get_instance_count, type); - return create_raw_counter(info, HPX_MOVE(f), ec); + return naming::invalid_gid; } - break; - default: + // ask AGAS to resolve the component type + components::component_type type = + agas::get_component_id(paths.parameters_); + + if (type == to_int(hpx::components::component_enum_type::invalid)) + { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "component_instance_counter_creator", - "invalid counter type requested"); + "invalid component type as counter parameter: {}", + paths.parameters_); return naming::invalid_gid; } + + hpx::function f = + hpx::bind_front(&get_instance_count, type); + return create_raw_counter(info, HPX_MOVE(f), ec); } -}}} // namespace hpx::performance_counters::detail +} // namespace hpx::performance_counters::detail diff --git a/libs/full/performance_counters/src/server/elapsed_time_counter.cpp b/libs/full/performance_counters/src/server/elapsed_time_counter.cpp index 5396ac712032..349cc7ab1bcc 100644 --- a/libs/full/performance_counters/src/server/elapsed_time_counter.cpp +++ b/libs/full/performance_counters/src/server/elapsed_time_counter.cpp @@ -101,34 +101,30 @@ namespace hpx::performance_counters::detail { naming::gid_type uptime_counter_creator( counter_info const& info, error_code& ec) { - switch (info.type_) - { - case counter_type::elapsed_time: + if (info.type_ != counter_type::elapsed_time) { - // verify the validity of the counter instance name - counter_path_elements paths; - get_counter_path_elements(info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; - - // allowed counter names: /runtime(locality#%d/*)/uptime - if (paths.parentinstance_is_basename_) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "uptime_counter_creator", - "invalid counter instance parent name: {}", - paths.parentinstancename_); - return naming::invalid_gid; - } - - // create the counter - return create_counter(info, ec); + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "uptime_counter_creator", "invalid counter type requested"); + return naming::invalid_gid; } - default: + // verify the validity of the counter instance name + counter_path_elements paths; + get_counter_path_elements(info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; + + // allowed counter names: /runtime(locality#%d/*)/uptime + if (paths.parentinstance_is_basename_) + { HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "uptime_counter_creator", "invalid counter type requested"); + "uptime_counter_creator", + "invalid counter instance parent name: {}", + paths.parentinstancename_); return naming::invalid_gid; } + + // create the counter + return create_counter(info, ec); } } // namespace hpx::performance_counters::detail diff --git a/libs/full/performance_counters/src/server/statistics_counter.cpp b/libs/full/performance_counters/src/server/statistics_counter.cpp index b965875bf5f9..d41c0130c226 100644 --- a/libs/full/performance_counters/src/server/statistics_counter.cpp +++ b/libs/full/performance_counters/src/server/statistics_counter.cpp @@ -755,57 +755,50 @@ namespace hpx::performance_counters::detail { naming::gid_type statistics_counter_creator( counter_info const& info, error_code& ec) { - switch (info.type_) + if (info.type_ != counter_type::aggregating) { - case counter_type::aggregating: + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "statistics_counter_creator", "invalid counter type requested"); + return naming::invalid_gid; + } + + counter_path_elements paths; + get_counter_path_elements(info.fullname_, paths, ec); + if (ec) + return naming::invalid_gid; + + if (!paths.parentinstance_is_basename_) { - counter_path_elements paths; - get_counter_path_elements(info.fullname_, paths, ec); - if (ec) - return naming::invalid_gid; + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "statistics_counter_creator", + "invalid aggregate counter " + "name (instance name must be valid base counter name)"); + return naming::invalid_gid; + } + + std::string base_name; + get_counter_name(paths.parentinstancename_, base_name, ec); + if (ec) + return naming::invalid_gid; - if (!paths.parentinstance_is_basename_) + std::vector parameters; + if (!paths.parameters_.empty()) + { + // try to interpret the additional parameters + namespace x3 = boost::spirit::x3; + if (!x3::parse(paths.parameters_.begin(), paths.parameters_.end(), + x3::uint_ % ',', parameters)) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "statistics_counter_creator", - "invalid aggregate counter " - "name (instance name must be valid base counter name)"); + "invalid parameter specification format for " + "this counter: {}", + paths.parameters_); return naming::invalid_gid; } - - std::string base_name; - get_counter_name(paths.parentinstancename_, base_name, ec); - if (ec) - return naming::invalid_gid; - - std::vector parameters; - if (!paths.parameters_.empty()) + if (paths.countername_.find("rolling") != std::string::npos) { - // try to interpret the additional parameters - namespace x3 = boost::spirit::x3; - if (!x3::parse(paths.parameters_.begin(), - paths.parameters_.end(), x3::uint_ % ',', parameters)) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "statistics_counter_creator", - "invalid parameter specification format for " - "this counter: {}", - paths.parameters_); - return naming::invalid_gid; - } - if (paths.countername_.find("rolling") != std::string::npos) - { - if (parameters.size() > 3) - { - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "statistics_counter_creator", - "too many parameter specifications for " - "this counter: {}", - paths.parameters_); - return naming::invalid_gid; - } - } - else if (parameters.size() > 2) + if (parameters.size() > 3) { HPX_THROWS_IF(ec, hpx::error::bad_parameter, "statistics_counter_creator", @@ -815,25 +808,28 @@ namespace hpx::performance_counters::detail { return naming::invalid_gid; } } - else if (paths.countername_.find("rolling") != std::string::npos) + else if (parameters.size() > 2) { - parameters.push_back(1000); // sample interval - parameters.push_back(10); // rolling window - parameters.push_back(0); // don't reset underlying counter - } - else - { - parameters.push_back(1000); // sample interval - parameters.push_back(0); // don't reset underlying counter + HPX_THROWS_IF(ec, hpx::error::bad_parameter, + "statistics_counter_creator", + "too many parameter specifications for " + "this counter: {}", + paths.parameters_); + return naming::invalid_gid; } - return create_statistics_counter(info, base_name, parameters, ec); } - break; - - default: - HPX_THROWS_IF(ec, hpx::error::bad_parameter, - "statistics_counter_creator", "invalid counter type requested"); - return naming::invalid_gid; + else if (paths.countername_.find("rolling") != std::string::npos) + { + parameters.push_back(1000); // sample interval + parameters.push_back(10); // rolling window + parameters.push_back(0); // don't reset underlying counter } + else + { + parameters.push_back(1000); // sample interval + parameters.push_back(0); // don't reset underlying counter + } + + return create_statistics_counter(info, base_name, parameters, ec); } } // namespace hpx::performance_counters::detail