Skip to content

Commit

Permalink
Adding support for generic counter_raw_values performance counter type
Browse files Browse the repository at this point in the history
- this fixes #3102
  • Loading branch information
hkaiser committed Jan 15, 2018
1 parent f988a10 commit 204d29c
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 17 deletions.
Expand Up @@ -18,6 +18,7 @@ std::atomic<std::int64_t> counter(0);

std::int64_t some_performance_data(bool reset)
{
++counter;
return hpx::util::get_and_reset_value(counter, reset);
}

Expand Down
3 changes: 2 additions & 1 deletion hpx/include/performance_counters.hpp
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2017 Hartmut Kaiser
// Copyright (c) 2007-2018 Hartmut Kaiser
// Copyright (c) 2011 Bryce Lelbach
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -14,6 +14,7 @@
#include <hpx/performance_counters/manage_counter_type.hpp>
#include <hpx/performance_counters/performance_counter.hpp>
#include <hpx/performance_counters/performance_counter_set.hpp>
#include <hpx/util/get_and_reset_value.hpp>

#endif

6 changes: 6 additions & 0 deletions hpx/performance_counters/counter_creators.hpp
Expand Up @@ -15,6 +15,7 @@

#include <cstdint>
#include <string>
#include <vector>

///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace performance_counters
Expand Down Expand Up @@ -106,6 +107,11 @@ namespace hpx { namespace performance_counters
counter_info const&, hpx::util::function_nonser<std::int64_t(bool)> const&,
error_code&);

HPX_API_EXPORT naming::gid_type locality_raw_values_counter_creator(
counter_info const&,
hpx::util::function_nonser<std::vector<std::int64_t>(bool)> const&,
error_code&);

///////////////////////////////////////////////////////////////////////////
/// Creation function for raw counters. The passed function is encapsulating
/// the actual value to monitor. This function checks the validity of the
Expand Down
10 changes: 9 additions & 1 deletion hpx/performance_counters/counters.hpp
Expand Up @@ -153,7 +153,15 @@ namespace hpx { namespace performance_counters
/// and upper boundaries, and the size of the histogram buckets. All
/// remaining values in the returned array represent the number of
/// measurements for each of the buckets in the histogram.
counter_histogram
counter_histogram,

/// \a counter_raw_values exposes an array of measured values
/// instead of a single value as many of the other counter types.
/// Counters of this type expose a \a counter_value_array instead of a
/// \a counter_value. Those will also not implement the
/// \a get_counter_value() functionality. The results are exposed
/// through a separate \a get_counter_values_array() function.
counter_raw_values
};
#endif

Expand Down
10 changes: 9 additions & 1 deletion hpx/performance_counters/counters_fwd.hpp
Expand Up @@ -124,7 +124,15 @@ namespace hpx { namespace performance_counters
// and upper boundaries, and the size of the histogram buckets. All
// remaining values in the returned array represent the number of
// measurements for each of the buckets in the histogram.
counter_histogram
counter_histogram,

/// \a counter_raw_values exposes an array of measured values
/// instead of a single value as many of the other counter types.
/// Counters of this type expose a \a counter_value_array instead of a
/// \a counter_value. Those will also not implement the
/// \a get_counter_value() functionality. The results are exposed
/// through a separate \a get_counter_values_array() function.
counter_raw_values
};

///////////////////////////////////////////////////////////////////////////
Expand Down
52 changes: 51 additions & 1 deletion hpx/performance_counters/manage_counter_type.hpp
@@ -1,5 +1,5 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2007-2013 Hartmut Kaiser
// Copyright (c) 2007-2018 Hartmut Kaiser
// Copyright (c) 2011 Bryce Adelstein-Lelbach
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -19,6 +19,7 @@
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>

namespace hpx { namespace performance_counters
{
Expand Down Expand Up @@ -68,6 +69,55 @@ namespace hpx { namespace performance_counters
std::string const& helptext = "", std::string const& uom = "",
error_code& ec = throws);

/// Install a new generic performance counter type returning an
/// array of values in a way, that will uninstall it automatically during
/// shutdown.
///
/// The function \a install_counter_type will register a new generic
/// counter type that returns an array of values based on the provided
/// function. The counter
/// type will be automatically unregistered during system shutdown. Any
/// consumer querying any instance of this this counter type will cause the
/// provided function to be called and the returned array value to be
/// exposed as the counter value.
///
/// The counter type is registered such that there can be one counter
/// instance per locality. The expected naming scheme for the counter
/// instances is: \c '/objectname{locality#<*>/total}/countername' where
/// '<*>' is a zero based integer identifying the locality the counter
/// is created on.
///
/// \param name [in] The global virtual name of the counter type. This
/// name is expected to have the format /objectname/countername.
/// \param counter_value [in] The function to call whenever the counter
/// value (array of values) is requested by a consumer.
/// \param helptext [in, optional] A longer descriptive text shown to the
/// user to explain the nature of the counters created from
/// this type.
/// \param uom [in] The unit of measure for the new performance counter
/// type.
/// \param ec [in,out] this represents the error status on exit,
/// if this is pre-initialized to \a hpx#throws
/// the function will throw on error instead.
///
/// \note As long as \a ec is not pre-initialized to \a hpx::throws this
/// function doesn't throw but returns the result code using the
/// parameter \a ec. Otherwise it throws an instance of hpx::exception.
///
/// \returns If successful, this function returns \a status_valid_data,
/// otherwise it will either throw an exception or return an
/// error_code from the enum \a counter_status (also, see
/// note related to parameter \a ec).
///
/// \note The counter type registry is a locality based service. You will
/// have to register each counter type on every locality where a
/// corresponding performance counter will be created.
HPX_EXPORT counter_status install_counter_type(std::string const& name,
hpx::util::function_nonser<std::vector<std::int64_t>(bool)> const&
counter_value,
std::string const& helptext = "", std::string const& uom = "",
error_code& ec = throws);

/// \brief Install a new performance counter type in a way, which will
/// uninstall it automatically during shutdown.
///
Expand Down
11 changes: 11 additions & 0 deletions hpx/util/get_and_reset_value.hpp
Expand Up @@ -8,6 +8,7 @@

#include <atomic>
#include <cstdint>
#include <vector>

namespace hpx { namespace util
{
Expand Down Expand Up @@ -36,6 +37,16 @@ namespace hpx { namespace util
value.store(0);
return result;
}

inline std::vector<std::int64_t> get_and_reset_value(
std::vector<std::int64_t>& value, bool reset)
{
std::vector<std::int64_t> result = value;
if (reset)
value.clear();

return result;
}
}}

#endif
28 changes: 28 additions & 0 deletions src/performance_counters/counter_creators.cpp
Expand Up @@ -18,6 +18,7 @@

#include <cstdint>
#include <string>
#include <vector>

///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace performance_counters
Expand Down Expand Up @@ -446,6 +447,33 @@ namespace hpx { namespace performance_counters
return naming::invalid_gid;
}

naming::gid_type locality_raw_values_counter_creator(counter_info const& info,
hpx::util::function_nonser<std::vector<std::int64_t>(bool)> const& f,
error_code& ec)
{
// 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;

if (paths.parentinstance_is_basename_) {
HPX_THROWS_IF(ec, bad_parameter,
"locality_raw_values_counter_creator",
"invalid counter instance parent name: " +
paths.parentinstancename_);
return naming::invalid_gid;
}

if (paths.instancename_ == "total" && paths.instanceindex_ == -1)
{
return detail::create_raw_counter(info, f, ec); // overall counter
}

HPX_THROWS_IF(ec, bad_parameter, "locality_raw_values_counter_creator",
"invalid counter instance name: " + paths.instancename_);
return naming::invalid_gid;
}

namespace detail
{
naming::gid_type retrieve_agas_counter(std::string const& name,
Expand Down
3 changes: 2 additions & 1 deletion src/performance_counters/counters.cpp
Expand Up @@ -601,12 +601,13 @@ namespace hpx { namespace performance_counters
"counter_average_timer",
"counter_elapsed_time",
"counter_histogram",
"counter_raw_values",
};
}

char const* get_counter_type_name(counter_type type)
{
if (type < counter_text || type > counter_histogram)
if (type < counter_text || type > counter_raw_values)
return "unknown";
return strings::counter_type_names[type];
}
Expand Down
15 changes: 15 additions & 0 deletions src/performance_counters/manage_counter_type.cpp
Expand Up @@ -140,6 +140,21 @@ namespace hpx { namespace performance_counters
HPX_PERFORMANCE_COUNTER_V1, uom, ec);
}

counter_status install_counter_type(std::string const& name,
hpx::util::function_nonser<std::vector<std::int64_t>(bool)> const&
counter_value,
std::string const& helptext, std::string const& uom, error_code& ec)
{
using hpx::util::placeholders::_1;
using hpx::util::placeholders::_2;
return install_counter_type(name, counter_raw_values, helptext,
util::bind(
&hpx::performance_counters::locality_raw_values_counter_creator,
_1, counter_value, _2),
&hpx::performance_counters::locality_counter_discoverer,
HPX_PERFORMANCE_COUNTER_V1, uom, ec);
}

/// Install several new performance counter types in a way, which will
/// uninstall them automatically during shutdown.
void install_counter_types(generic_counter_type_data const* data,
Expand Down
10 changes: 8 additions & 2 deletions src/performance_counters/performance_counter_set.cpp
Expand Up @@ -269,8 +269,11 @@ namespace hpx { namespace performance_counters
// reset all performance counters
for (std::size_t i = 0; i != ids.size(); ++i)
{
if (infos_[i].type_ == counter_histogram)
if (infos_[i].type_ == counter_histogram ||
infos_[i].type_ == counter_raw_values)
{
continue;
}

using performance_counters::stubs::performance_counter;
v.push_back(performance_counter::get_value(
Expand Down Expand Up @@ -311,8 +314,11 @@ namespace hpx { namespace performance_counters
// reset all performance counters
for (std::size_t i = 0; i != ids.size(); ++i)
{
if (infos_[i].type_ != counter_histogram)
if (infos_[i].type_ != counter_histogram &&
infos_[i].type_ != counter_raw_values)
{
continue;
}

using performance_counters::stubs::performance_counter;
v.push_back(performance_counter::get_values_array(
Expand Down
8 changes: 5 additions & 3 deletions src/performance_counters/registry.cpp
Expand Up @@ -488,12 +488,14 @@ namespace hpx { namespace performance_counters
}

// make sure the counter type requested is supported
if (counter_histogram != (*it).second.info_.type_ ||
counter_histogram != info.type_)
if (!((counter_histogram == (*it).second.info_.type_ &&
counter_histogram == info.type_) ||
(counter_raw_values == (*it).second.info_.type_ &&
counter_raw_values == info.type_)))
{
HPX_THROWS_IF(ec, bad_parameter, "registry::create_raw_counter",
"invalid counter type requested (only counter_histogram "
"is supported)");
"or counter_raw_values is supported)");
return status_counter_type_unknown;
}

Expand Down
7 changes: 5 additions & 2 deletions src/performance_counters/server/raw_values_counter.cpp
Expand Up @@ -33,10 +33,13 @@ namespace hpx { namespace performance_counters { namespace server
hpx::util::function_nonser<std::vector<std::int64_t>(bool)> f)
: base_type_holder(info), f_(std::move(f)), reset_(false)
{
if (info.type_ != counter_histogram) {
if (info.type_ != counter_histogram &&
info.type_ != counter_raw_values)
{
HPX_THROW_EXCEPTION(bad_parameter,
"raw_values_counter::raw_values_counter",
"unexpected counter type specified for raw_values_counter");
"unexpected counter type specified for raw_values_counter "
"should be counter_histogram or counter_raw_values");
}
}

Expand Down
24 changes: 20 additions & 4 deletions src/util/query_counters.cpp
Expand Up @@ -267,8 +267,12 @@ namespace hpx { namespace util
// now print array value counters
for (std::size_t i = 0; i != infos.size(); ++i)
{
if (infos[i].type_ != performance_counters::counter_histogram)
if (infos[i].type_ != performance_counters::counter_histogram &&
infos[i].type_ != performance_counters::counter_raw_values)
{
continue;
}

if (!first)
output << ",";
first = false;
Expand All @@ -294,8 +298,12 @@ namespace hpx { namespace util
// now print array value counters
for (std::size_t i = 0; i != counter_shortnames_.size(); ++i)
{
if (infos[i].type_ != performance_counters::counter_histogram)
if (infos[i].type_ != performance_counters::counter_histogram &&
infos[i].type_ != performance_counters::counter_raw_values)
{
continue;
}

if (!first)
output << ",";
first = false;
Expand Down Expand Up @@ -411,8 +419,12 @@ namespace hpx { namespace util

for (std::size_t i = 0; i != infos.size(); ++i)
{
if (infos[i].type_ == performance_counters::counter_histogram)
if (infos[i].type_ == performance_counters::counter_histogram ||
infos[i].type_ == performance_counters::counter_raw_values)
{
continue;
}

indicies.push_back(i);
}

Expand Down Expand Up @@ -461,8 +473,12 @@ namespace hpx { namespace util

for (std::size_t i = 0; i != infos.size(); ++i)
{
if (infos[i].type_ != performance_counters::counter_histogram)
if (infos[i].type_ != performance_counters::counter_histogram &&
infos[i].type_ != performance_counters::counter_raw_values)
{
continue;
}

indicies.push_back(i);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/performance_counter/CMakeLists.txt
@@ -1,10 +1,11 @@
# Copyright (c) 2007-2012 Hartmut Kaiser
# Copyright (c) 2007-2018 Hartmut Kaiser
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

set(tests
all_counters
counter_raw_values
path_elements)

foreach(test ${tests})
Expand Down

0 comments on commit 204d29c

Please sign in to comment.