Skip to content

Commit 4bd422b

Browse files
committed
hmon: parametrizable background health thread
Expose parameters to background thread.
1 parent 1662c32 commit 4bd422b

19 files changed

Lines changed: 785 additions & 37 deletions

File tree

Cargo.lock

Lines changed: 26 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ signal-hook = "0.3.18"
2121

2222
monitor_rs = { path = "src/launch_manager_daemon/health_monitor_lib/rust_bindings" } # Temporary API
2323
health_monitoring_lib = { path = "src/health_monitoring_lib" }
24-
score_log = { git = "https://github.com/eclipse-score/baselibs_rust.git", tag = "v0.0.4" }
25-
score_testing_macros = { git = "https://github.com/eclipse-score/baselibs_rust.git", tag = "v0.0.4" }
26-
stdout_logger = { git = "https://github.com/eclipse-score/baselibs_rust.git", tag = "v0.0.4" }
27-
containers = { git = "https://github.com/eclipse-score/baselibs_rust.git", tag = "v0.0.4" }
24+
score_log = { git = "https://github.com/eclipse-score/baselibs_rust.git", tag = "v0.1.1" }
25+
score_testing_macros = { git = "https://github.com/eclipse-score/baselibs_rust.git", tag = "v0.1.1" }
26+
stdout_logger = { git = "https://github.com/eclipse-score/baselibs_rust.git", tag = "v0.1.1" }
27+
containers = { git = "https://github.com/eclipse-score/baselibs_rust.git", tag = "v0.1.1" }
28+
thread = { git = "https://github.com/eclipse-score/baselibs_rust.git", tag = "v0.1.1" }
2829

2930
[workspace.lints.clippy]
3031
std_instead_of_core = "warn"

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pip.parse(
133133
)
134134
use_repo(pip, "score_lifecycle_pip")
135135

136-
bazel_dep(name = "score_baselibs_rust", version = "0.1.0")
136+
bazel_dep(name = "score_baselibs_rust", version = "0.1.1")
137137
bazel_dep(name = "score_baselibs", version = "0.2.4")
138138
bazel_dep(name = "score_logging", version = "0.1.0")
139139

MODULE.bazel.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/health_monitoring_lib/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ load("@score_baselibs//:bazel/unit_tests.bzl", "cc_gtest_unit_test")
1717

1818
COMMON_DEPS = [
1919
"@score_baselibs_rust//src/containers:containers",
20+
"@score_baselibs_rust//src/thread:thread",
2021
"@score_baselibs_rust//src/log/score_log:score_log",
2122
"//src/launch_manager_daemon/health_monitor_lib/rust_bindings:monitor_rs",
2223
]
@@ -31,11 +32,13 @@ CC_SOURCES = [
3132
"cpp/heartbeat_monitor.cpp",
3233
"cpp/logic_monitor.cpp",
3334
"cpp/health_monitor.cpp",
35+
"cpp/thread.cpp",
3436
]
3537

3638
CC_HDRS = [
3739
"cpp/include/score/hm/common.h",
3840
"cpp/include/score/hm/tag.h",
41+
"cpp/include/score/hm/thread.h",
3942
"cpp/include/score/hm/deadline/deadline_monitor.h",
4043
"cpp/include/score/hm/heartbeat/heartbeat_monitor.h",
4144
"cpp/include/score/hm/logic/logic_monitor.h",

src/health_monitoring_lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ path = "rust/lib.rs"
1414
workspace = true
1515

1616
[dependencies]
17+
thread.workspace = true
1718
score_log.workspace = true
1819
score_testing_macros.workspace = true
1920
containers.workspace = true

src/health_monitoring_lib/cpp/health_monitor.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ FFICode health_monitor_builder_destroy(FFIHandle health_monitor_builder_handle);
2828
FFICode health_monitor_builder_build(FFIHandle health_monitor_builder_handle,
2929
uint32_t supervisor_cycle_ms,
3030
uint32_t internal_cycle_ms,
31+
FFIHandle thread_parameters_handle,
3132
FFIHandle* health_monitor_handle_out);
3233
FFICode health_monitor_builder_add_deadline_monitor(FFIHandle health_monitor_builder_handle,
3334
const MonitorTag* monitor_tag,
@@ -125,17 +126,33 @@ HealthMonitorBuilder HealthMonitorBuilder::with_supervisor_api_cycle(std::chrono
125126
return std::move(*this);
126127
}
127128

129+
HealthMonitorBuilder HealthMonitorBuilder::thread_parameters(score::hm::ThreadParameters&& thread_parameters) &&
130+
{
131+
thread_parameters_ = std::move(thread_parameters);
132+
return std::move(*this);
133+
}
134+
128135
score::cpp::expected<HealthMonitor, Error> HealthMonitorBuilder::build() &&
129136
{
130137
auto health_monitor_builder_handle = health_monitor_builder_handle_.drop_by_rust();
131138
SCORE_LANGUAGE_FUTURECPP_PRECONDITION(health_monitor_builder_handle.has_value());
132139

133140
uint32_t supervisor_duration_ms = static_cast<uint32_t>(supervisor_api_cycle_duration_.count());
134141
uint32_t internal_duration_ms = static_cast<uint32_t>(internal_processing_cycle_duration_.count());
142+
FFIHandle thread_parameters_handle{nullptr};
143+
if (thread_parameters_.has_value())
144+
{
145+
auto rust_handle{thread_parameters_.value().drop_by_rust()};
146+
SCORE_LANGUAGE_FUTURECPP_ASSERT(rust_handle.has_value());
147+
thread_parameters_handle = rust_handle.value();
148+
}
135149

136150
FFIHandle health_monitor_handle{nullptr};
137-
auto result{health_monitor_builder_build(
138-
health_monitor_builder_handle.value(), supervisor_duration_ms, internal_duration_ms, &health_monitor_handle)};
151+
auto result{health_monitor_builder_build(health_monitor_builder_handle.value(),
152+
supervisor_duration_ms,
153+
internal_duration_ms,
154+
thread_parameters_handle,
155+
&health_monitor_handle)};
139156
if (result != kSuccess)
140157
{
141158
return score::cpp::unexpected(static_cast<Error>(result));

src/health_monitoring_lib/cpp/include/score/hm/health_monitor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <score/hm/heartbeat/heartbeat_monitor.h>
1919
#include <score/hm/logic/logic_monitor.h>
2020
#include <score/hm/tag.h>
21+
#include <score/hm/thread.h>
2122

2223
namespace score::hm
2324
{
@@ -59,6 +60,9 @@ class HealthMonitorBuilder final
5960
/// This duration determines how often the health monitor checks deadlines.
6061
HealthMonitorBuilder with_internal_processing_cycle(std::chrono::milliseconds cycle_duration) &&;
6162

63+
/// Sets the monitoring thread parameters.
64+
HealthMonitorBuilder thread_parameters(score::hm::ThreadParameters&& thread_parameters) &&;
65+
6266
/// Build a new `HealthMonitor` instance based on provided parameters.
6367
score::cpp::expected<HealthMonitor, Error> build() &&;
6468

@@ -67,6 +71,7 @@ class HealthMonitorBuilder final
6771

6872
std::chrono::milliseconds supervisor_api_cycle_duration_;
6973
std::chrono::milliseconds internal_processing_cycle_duration_;
74+
std::optional<ThreadParameters> thread_parameters_;
7075
};
7176

7277
class HealthMonitor final
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/********************************************************************************
2+
* Copyright (c) 2026 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
#ifndef SCORE_HM_THREAD_H
14+
#define SCORE_HM_THREAD_H
15+
16+
#include "common.h"
17+
#include <cstdint>
18+
#include <vector>
19+
20+
namespace score::hm
21+
{
22+
23+
class HealthMonitorBuilder;
24+
25+
/// Scheduler policy.
26+
enum class SchedulerPolicy : int32_t
27+
{
28+
Other,
29+
Fifo,
30+
RoundRobin,
31+
};
32+
33+
/// Get min thread priority for given policy.
34+
int32_t scheduler_policy_priority_min(SchedulerPolicy scheduler_policy);
35+
36+
/// Get max thread priority for given policy.
37+
int32_t scheduler_policy_priority_max(SchedulerPolicy scheduler_policy);
38+
39+
class SchedulerParameters final
40+
{
41+
public:
42+
/// Create a new `SchedulerParameters`.
43+
/// Priority must be in allowed range for the scheduler policy.
44+
SchedulerParameters(SchedulerPolicy policy, int32_t priority);
45+
46+
/// Scheduler policy.
47+
SchedulerPolicy policy() const;
48+
49+
/// Thread priority.
50+
int32_t priority() const;
51+
52+
private:
53+
SchedulerPolicy policy_;
54+
int32_t priority_;
55+
};
56+
57+
/// Thread parameters.
58+
class ThreadParameters final : public internal::RustDroppable<ThreadParameters>
59+
{
60+
public:
61+
/// Create a new `ThreadParameters` containing default values.
62+
ThreadParameters();
63+
64+
/// Scheduler parameters, including scheduler policy and thread priority.
65+
ThreadParameters scheduler_parameters(SchedulerParameters scheduler_parameters) &&;
66+
67+
/// Set thread affinity - array of CPU core IDs that the thread can run on.
68+
ThreadParameters affinity(const std::vector<size_t>& affinity) &&;
69+
70+
/// Set stack size.
71+
ThreadParameters stack_size(size_t stack_size) &&;
72+
73+
protected:
74+
std::optional<internal::FFIHandle> _drop_by_rust_impl()
75+
{
76+
return thread_parameters_handle_.drop_by_rust();
77+
}
78+
79+
private:
80+
internal::DroppableFFIHandle thread_parameters_handle_;
81+
82+
// Allow to hide `drop_by_rust` implementation.
83+
friend class internal::RustDroppable<ThreadParameters>;
84+
85+
// Allow `HealthMonitorBuilder` to access `drop_by_rust` implementation.
86+
friend class score::hm::HealthMonitorBuilder;
87+
};
88+
89+
} // namespace score::hm
90+
91+
#endif // SCORE_HM_THREAD_H

src/health_monitoring_lib/cpp/tests/health_monitor_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ TEST_F(HealthMonitorTest, TestName)
5656
auto logic_monitor_builder =
5757
logic::LogicMonitorBuilder{from_state}.add_state(from_state, std::vector{to_state}).add_state(to_state, {});
5858

59+
// Thread parameters.
60+
auto thread_parameters{ThreadParameters().affinity(std::vector<size_t>{0})};
61+
5962
auto hmon_result{HealthMonitorBuilder()
6063
.add_deadline_monitor(deadline_monitor_tag, std::move(deadline_monitor_builder))
6164
.add_heartbeat_monitor(heartbeat_monitor_tag, std::move(heartbeat_monitor_builder))
6265
.add_logic_monitor(logic_monitor_tag, std::move(logic_monitor_builder))
6366
.with_internal_processing_cycle(std::chrono::milliseconds(50))
6467
.with_supervisor_api_cycle(std::chrono::milliseconds(50))
68+
.thread_parameters(std::move(thread_parameters))
6569
.build()};
6670
EXPECT_TRUE(hmon_result.has_value());
6771
auto hm{std::move(hmon_result.value())};

0 commit comments

Comments
 (0)