Program Listing for File periodic.hpp
↰ Return to documentation for file (include/holoscan/core/conditions/gxf/periodic.hpp
)
/*
* SPDX-FileCopyrightText: Copyright (c) 2023-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_CONDITIONS_GXF_PERIODIC_HPP
#define HOLOSCAN_CORE_CONDITIONS_GXF_PERIODIC_HPP
#include <string>
#include <chrono>
#include <gxf/std/scheduling_terms.hpp>
#include "../../gxf/gxf_condition.hpp"
namespace holoscan {
enum class PeriodicConditionPolicy {
// scheduler will try to "catch up" on missed ticks
// eg. assume recess period of 100ms:
// tick 0 at 0ms -> next_target_ = 100ms
// tick 1 at 250ms -> next_target_ = 200ms (next_target_ < timestamp)
// tick 2 at 255ms -> next_target_ = 300ms (double tick before 300ms)
kCatchUpMissedTicks,
// scheduler guarantees recess period will have passed before next tick
// eg. assume recess period of 100ms:
// tick 0 at 0ms -> next_target_ = 100ms
// tick 1 at 101ms -> next_target_ = 201ms
// tick 2 at 350ms -> next_target_ = 450ms
kMinTimeBetweenTicks,
// scheduler will not try to "catch up" on missed ticks
// eg. assume recess period of 100ms:
// tick 0 at 0ms -> next_target_ = 100ms
// tick 1 at 250ms -> next_target_ = 300ms (single tick before 300ms)
// tick 2 at 305ms -> next_target_ = 400ms
kNoCatchUpMissedTicks
};
class PeriodicCondition : public gxf::GXFCondition {
public:
HOLOSCAN_CONDITION_FORWARD_ARGS_SUPER(PeriodicCondition, GXFCondition)
// using PeriodicConditionPolicy = nvidia::gxf::PeriodicSchedulingPolicy;
PeriodicCondition() = default;
explicit PeriodicCondition(
int64_t recess_period_ns,
PeriodicConditionPolicy policy = PeriodicConditionPolicy::kCatchUpMissedTicks);
template <typename Rep, typename Period>
explicit PeriodicCondition(
std::chrono::duration<Rep, Period> recess_period_duration,
PeriodicConditionPolicy policy = PeriodicConditionPolicy::kCatchUpMissedTicks)
: recess_period_ns_(
std::chrono::duration_cast<std::chrono::nanoseconds>(recess_period_duration).count()) {
recess_period_ = std::to_string(recess_period_ns_);
switch (policy) {
case PeriodicConditionPolicy::kCatchUpMissedTicks:
policy_ = YAML::Node("CatchUpMissedTicks");
break;
case PeriodicConditionPolicy::kMinTimeBetweenTicks:
policy_ = YAML::Node("MinTimeBetweenTicks");
break;
case PeriodicConditionPolicy::kNoCatchUpMissedTicks:
policy_ = YAML::Node("NoCatchUpMissedTicks");
break;
default:
HOLOSCAN_LOG_ERROR("Unrecognized policy enum value: {}", static_cast<int>(policy));
}
}
nvidia::gxf::PeriodicSchedulingPolicy convertToGXFPolicy(PeriodicConditionPolicy policy) {
switch (policy) {
case PeriodicConditionPolicy::kCatchUpMissedTicks:
return nvidia::gxf::PeriodicSchedulingPolicy::kCatchUpMissedTicks;
case PeriodicConditionPolicy::kMinTimeBetweenTicks:
return nvidia::gxf::PeriodicSchedulingPolicy::kMinTimeBetweenTicks;
case PeriodicConditionPolicy::kNoCatchUpMissedTicks:
return nvidia::gxf::PeriodicSchedulingPolicy::kNoCatchUpMissedTicks;
default:
throw std::runtime_error("Unknown PeriodicConditionPolicy value");
}
}
PeriodicCondition(const std::string& name, nvidia::gxf::PeriodicSchedulingTerm* term);
const char* gxf_typename() const override { return "nvidia::gxf::PeriodicSchedulingTerm"; }
void initialize() override;
void setup(ComponentSpec& spec) override;
PeriodicConditionPolicy policy() {
auto gxf_policy = policy_.get().as<std::string>();
if (gxf_policy == "CatchUpMissedTicks") {
return PeriodicConditionPolicy::kCatchUpMissedTicks;
} else if (gxf_policy == "MinTimeBetweenTicks") {
return PeriodicConditionPolicy::kMinTimeBetweenTicks;
} else if (gxf_policy == "NoCatchUpMissedTicks") {
return PeriodicConditionPolicy::kNoCatchUpMissedTicks;
} else {
throw std::runtime_error(fmt::format("unknown mode: {}", gxf_policy));
}
}
void recess_period(int64_t recess_period_ns);
template <typename Rep, typename Period>
void recess_period(std::chrono::duration<Rep, Period> recess_period_duration) {
int64_t recess_period_ns =
std::chrono::duration_cast<std::chrono::nanoseconds>(recess_period_duration).count();
recess_period(recess_period_ns);
}
int64_t recess_period_ns();
int64_t last_run_timestamp();
nvidia::gxf::PeriodicSchedulingTerm* get() const;
private:
Parameter<std::string> recess_period_;
int64_t recess_period_ns_ = 0;
Parameter<YAML::Node> policy_; // = YAML::Node("CatchUpMissedTicks");
};
} // namespace holoscan
#endif/* HOLOSCAN_CORE_CONDITIONS_GXF_PERIODIC_HPP */