holoscan::CPUResourceMonitor

Beta
View as Markdown

CPUResourceMonitor class.

This class is responsible for monitoring the CPU resources. It provides the information about the CPU resources (through holoscan::CPUInfo) to the SystemResourceManager class.

The following holoscan::CPUMetricFlag flags are supported:

  • DEFAULT: Default CPU metrics (CORE_COUNT | CPU_COUNT | AVAILABLE_PROCESSOR_COUNT)
  • CORE_COUNT: Number of cores (num_cores)
  • CPU_COUNT: Number of CPUs (num_cpus)
  • AVAILABLE_PROCESSOR_COUNT: Number of available processors (num_processors)
  • CPU_USAGE: CPU usage (cpu_usage)
  • MEMORY_USAGE: Memory usage (memory_total, memory_free, memory_available, memory_usage)
  • SHARED_MEMORY_USAGE: Shared memory usage (shared_memory_total, shared_memory_free, shared_memory_available, shared_memory_usage)
  • ALL: All CPU metrics

CPU usage information is based on the /proc/stat file and it calculates the CPU usage between the current and the previous CPU usage information, so it is necessary to call update() method at least once before calling cpu_info() method.

Example:

#include <holoscan/cpu_resource_monitor.hpp>

Example

#include <chrono> // std::chrono::seconds
#include <thread> // std::this_thread::sleep_for
#include <holoscan/core/system/system_resource_manager.hpp>
#include <holoscan/logger/logger.hpp>
...
holoscan::Topology topology;
topology.load();
holoscan::CPUResourceMonitor cpu_resource_monitor(topology.context());
holoscan::CPUInfo cpu_info = cpu_resource_monitor.cpu_info(holoscan::CPUMetricFlag::ALL);
std::this_thread::sleep_for(std::chrono::seconds(1)); // wait for cpu usage update
// Update CPU usage info
cpu_info = system_resource_manager.cpu_monitor()->update(holoscan::CPUMetricFlag::CPU_USAGE);
HOLOSCAN_LOG_INFO("CPU cores: {}", cpu_info.num_cores);
HOLOSCAN_LOG_INFO("CPU cpus: {}", cpu_info.num_cpus);
HOLOSCAN_LOG_INFO("CPU processors: {}", cpu_info.num_processors);
HOLOSCAN_LOG_INFO("CPU usage: {}", cpu_info.cpu_usage);
HOLOSCAN_LOG_INFO("CPU memory total: {}", cpu_info.memory_total);
HOLOSCAN_LOG_INFO("CPU memory free: {}", cpu_info.memory_free);
HOLOSCAN_LOG_INFO("CPU memory available: {}", cpu_info.memory_available);
HOLOSCAN_LOG_INFO("CPU memory usage: {}", cpu_info.memory_usage);
HOLOSCAN_LOG_INFO("CPU shared memory total: {}", cpu_info.shared_memory_total);
HOLOSCAN_LOG_INFO("CPU shared memory free: {}", cpu_info.shared_memory_free);
HOLOSCAN_LOG_INFO("CPU shared memory available: {}", cpu_info.shared_memory_available);
HOLOSCAN_LOG_INFO("CPU shared memory usage: {}", cpu_info.shared_memory_usage);

Constructors

CPUResourceMonitor

holoscan::CPUResourceMonitor::CPUResourceMonitor(holoscan::CPUResourceMonitor::CPUResourceMonitor(
void *context,
uint64_t metric_flags = kDefaultCpuMetrics
)

Construct a new CPUResourceMonitor object.

This constructor creates a new CPUResourceMonitor object.

Parameters

context
void *

The topology context (from holoscan::Topology::context()).

metric_flags
uint64_tDefaults to kDefaultCpuMetrics

The metric flags (default: CORE_COUNT | CPU_COUNT | AVAILABLE_PROCESSOR_COUNT)

Destructor

~CPUResourceMonitor

virtual holoscan::CPUResourceMonitor::~CPUResourceMonitor() = defaultvirtual holoscan::CPUResourceMonitor::~CPUResourceMonitor() = default

Methods

metric_flags

void holoscan::CPUResourceMonitor::metric_flags(
uint64_t metric_flags
)

Set metric flags.

This function sets the metric flags.

Parameters

metric_flags
uint64_t

The metric flags

update

CPUInfo holoscan::CPUResourceMonitor::update(CPUInfo holoscan::CPUResourceMonitor::update(
uint64_t metric_flags = CPUMetricFlag::DEFAULT
)

Update the CPU information and cache it.

This function updates information for the CPU with the given metric flags and returns the CPU information. If the metric flags are not provided, the existing metric flags are used. It also caches the CPU information.

Returns: The CPU information.

Parameters

metric_flags
uint64_tDefaults to CPUMetricFlag::DEFAULT

The metric flags.

cpu_info

CPUInfo holoscan::CPUResourceMonitor::cpu_info(CPUInfo holoscan::CPUResourceMonitor::cpu_info(
uint64_t metric_flags = CPUMetricFlag::DEFAULT
)

Get the CPU information.

If the metric flags are provided, it returns the CPU information based on the given metric flags. If the metric flags are not provided, it returns the cached CPU information.

Returns: The CPU information.

Parameters

metric_flags
uint64_tDefaults to CPUMetricFlag::DEFAULT

The metric flags.

cpu_set

cpu_set_t holoscan::CPUResourceMonitor::cpu_set() const

Get the CPU set.

The returned value (cpu_set_t) can be used to check whether the given CPU is available or not.

See https://linux.die.net/man/2/sched_getaffinity for more details.

Example:

Returns: The CPU set.

Example

#include <holoscan/core/system/system_resource_manager.hpp>
#include <holoscan/logger/logger.hpp>
...
holoscan::Topology topology;
topology.load();
holoscan::CPUResourceMonitor cpu_resource_monitor(topology.context());
holoscan::CPUInfo cpu_info = cpu_resource_monitor.cpu_info();
cpu_set_t cpu_set = cpu_resource_monitor.cpu_set();
for (int i = 0; i < cpu_info.num_cpus; i++) {
if (CPU_ISSET(i, &cpu_set)) {
HOLOSCAN_LOG_INFO("CPU {} is available", i);
}
}

Member variables

NameTypeDescription
context_void *The context of the CPU resource monitor.
metric_flags_uint64_tThe metric flags.
is_cached_boolThe flag to indicate whether the CPU information is cached.
cpu_info_CPUInfoThe cached CPU information.
cpu_set_cpu_set_tThe cached CPU set.
is_last_total_stats_valid_boolThe flag to indicate whether the last total cpu usage stats are valid.
last_total_stats_uint64_tThe last total cpu usage stats.