Program Listing for File cpu_thread.hpp
↰ Return to documentation for file (include/holoscan/core/resources/gxf/cpu_thread.hpp)
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HOLOSCAN_CORE_RESOURCES_GXF_CPU_THREAD_HPP
#define HOLOSCAN_CORE_RESOURCES_GXF_CPU_THREAD_HPP
#include <string>
#include <vector>
#include <gxf/std/cpu_thread.hpp>
#include "yaml-cpp/yaml.h"
#include "../../component_spec.hpp"
#include "../../errors.hpp"
#include "../../expected.hpp"
#include "../../gxf/gxf_resource.hpp"
#include "../../parameter.hpp"
// YAML conversion support for SchedulingPolicy
template <>
struct YAML::convert<nvidia::gxf::SchedulingPolicy> {
static Node encode(const nvidia::gxf::SchedulingPolicy& rhs) {
Node node;
switch (rhs) {
case nvidia::gxf::SchedulingPolicy::kFirstInFirstOut:
node = "SCHED_FIFO";
break;
case nvidia::gxf::SchedulingPolicy::kRoundRobin:
node = "SCHED_RR";
break;
case nvidia::gxf::SchedulingPolicy::kDeadline:
node = "SCHED_DEADLINE";
break;
default:
node = static_cast<int32_t>(rhs); // fallback to numeric value
break;
}
return node;
}
static bool decode(const Node& node, nvidia::gxf::SchedulingPolicy& rhs) {
if (!node.IsScalar())
return false;
const std::string value = node.Scalar();
// Support string values
if (value == "SCHED_FIFO") {
rhs = nvidia::gxf::SchedulingPolicy::kFirstInFirstOut;
return true;
} else if (value == "SCHED_RR") {
rhs = nvidia::gxf::SchedulingPolicy::kRoundRobin;
return true;
} else if (value == "SCHED_DEADLINE") {
rhs = nvidia::gxf::SchedulingPolicy::kDeadline;
return true;
}
// Support using the numeric enum values as well
try {
int32_t numeric_value = std::stoi(value);
switch (numeric_value) {
case static_cast<int32_t>(nvidia::gxf::SchedulingPolicy::kFirstInFirstOut):
rhs = nvidia::gxf::SchedulingPolicy::kFirstInFirstOut;
return true;
case static_cast<int32_t>(nvidia::gxf::SchedulingPolicy::kRoundRobin):
rhs = nvidia::gxf::SchedulingPolicy::kRoundRobin;
return true;
case static_cast<int32_t>(nvidia::gxf::SchedulingPolicy::kDeadline):
rhs = nvidia::gxf::SchedulingPolicy::kDeadline;
return true;
default:
break; // Invalid numeric value, fall through to return false
}
} catch (...) {
// Not a valid number, continue to return false
}
return false; // Invalid value
}
};
namespace holoscan {
using SchedulingPolicy = nvidia::gxf::SchedulingPolicy;
class CPUThread : public gxf::GXFResource {
public:
HOLOSCAN_RESOURCE_FORWARD_ARGS_SUPER(CPUThread, gxf::GXFResource)
explicit CPUThread(bool pin_entity = true) : pin_entity_(pin_entity) { name_ = "cpu_thread"; }
const char* gxf_typename() const override { return "nvidia::gxf::CPUThread"; }
void setup(ComponentSpec& spec) override;
void initialize() override;
bool pinned() const;
std::vector<uint32_t> pin_cores() const;
holoscan::expected<SchedulingPolicy, holoscan::RuntimeError> sched_policy() const;
holoscan::expected<uint32_t, holoscan::RuntimeError> sched_priority() const;
holoscan::expected<uint64_t, holoscan::RuntimeError> sched_runtime() const;
holoscan::expected<uint64_t, holoscan::RuntimeError> sched_deadline() const;
holoscan::expected<uint64_t, holoscan::RuntimeError> sched_period() const;
bool is_realtime() const;
private:
Parameter<bool> pin_entity_{false};
Parameter<std::vector<uint32_t>> pin_cores_;
Parameter<YAML::Node> sched_policy_;
Parameter<uint32_t> sched_priority_;
Parameter<uint64_t> sched_runtime_;
Parameter<uint64_t> sched_deadline_;
Parameter<uint64_t> sched_period_;
};
} // namespace holoscan
#endif/* HOLOSCAN_CORE_RESOURCES_GXF_CPU_THREAD_HPP */