Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
map_resources.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file map_resources.h
20 * @brief Apply and periodically check the survey process's low-memory policy.
21 * @details The address-space ceiling, current RSS, and system-available reserve
22 * are different measurements. Checking all three helps a job stop with a
23 * resumable checkpoint before it crowds the camera board, but does not prove
24 * whole-board safety or replace physical RSS/PSS/CMA/concurrency measurements.
25 */
26#ifndef PANGEA_MAP_RESOURCES_H
27#define PANGEA_MAP_RESOURCES_H
28
29#include "map_config.h"
30#include "opencv_parallel.hpp"
31#include <opencv2/core.hpp>
32#include <sys/resource.h>
33#include <sched.h>
34#include <linux/ioprio.h>
35#include <sys/syscall.h>
36#include <chrono>
37#include <fstream>
38
39namespace pangea_survey {
40class MapResourcePressure : public std::runtime_error {
41public:
42 using std::runtime_error::runtime_error;
43};
44/**
45 * @brief Read a named Linux memory counter and convert its KiB value to bytes.
46 * @param[in] path procfs file containing the counter.
47 * @param[in] key Exact counter name, including its trailing colon.
48 * @return Counter value in bytes.
49 * @throws std::runtime_error If the counter is unavailable.
50 */
51inline std::uint64_t procMemory(const char* path, const char* key) {
52 std::ifstream input(path);
53 std::string line;
54 while (std::getline(input, line)) {
55 std::istringstream fields(line);
56 std::string name; std::uint64_t value = 0;
57 if ((fields >> name >> value) && name == key) return value * 1024;
58 }
59 throw std::runtime_error("Required memory telemetry is unavailable");
60}
61/**
62 * @brief Enforce the configured bounded-work policy for a survey process.
63 * @details Low-memory mode limits address space, worker count, open files and
64 * scheduler priority. Periodic checks also preserve system-available memory;
65 * they are admission/overload guards, not a hard RSS cap or a whole-board proof.
66 */
69 std::uint64_t limit_, reserve_;
70 std::chrono::steady_clock::time_point checked_{};
71public:
72 /**
73 * @brief Establish process limits for the selected runtime profile.
74 * @param[in] config Validated map configuration.
75 * @throws std::runtime_error If the profile is unsupported or any required
76 * Linux resource policy cannot be established.
77 */
78 explicit MapResources(const MapConfig& config) : bounded_(config.lowMemory),
79 limit_(static_cast<std::uint64_t>(config.memoryMiB) * 1024 * 1024),
80 reserve_(static_cast<std::uint64_t>(config.reserveMiB) * 1024 * 1024) {
82 if (!bounded_) {
83 if (procMemory("/proc/meminfo", "MemTotal:") <= 1024ull * 1024 * 1024)
84 throw std::runtime_error("This board requires runtime.profile=low_memory and Great XYZ");
85 return;
86 }
87 cv::setNumThreads(1);
88 struct rlimit inherited{};
89 if (getrlimit(RLIMIT_AS, &inherited) != 0) throw std::runtime_error("Cannot inspect address limit");
90 const rlim_t cap = std::min<rlim_t>(limit_, inherited.rlim_cur);
91 struct rlimit address{cap, std::min<rlim_t>(inherited.rlim_max, cap)};
92 struct rlimit files{64, 64}, core{0, 0};
93 struct sched_param priority{};
94 if (setrlimit(RLIMIT_AS, &address) != 0 || setrlimit(RLIMIT_NOFILE, &files) != 0 ||
95 setrlimit(RLIMIT_CORE, &core) != 0 || setpriority(PRIO_PROCESS, 0, 19) != 0 ||
96 sched_setscheduler(0, SCHED_IDLE, &priority) != 0 ||
97 syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)) != 0)
98 throw std::runtime_error("Cannot establish low-memory process policy");
99 checkpoint();
100 }
101 /**
102 * @brief Check current process RSS and system reserve at a bounded cadence.
103 * @throws std::runtime_error If memory headroom closes; existing checkpoints
104 * are intentionally left in place for a later resume.
105 */
106 void checkpoint() {
107 if (!bounded_) return;
108 const auto now = std::chrono::steady_clock::now();
109 if (now - checked_ < std::chrono::milliseconds(250)) return;
110 checked_ = now;
111 const auto resident = procMemory("/proc/self/status", "VmRSS:");
112 const auto available = procMemory("/proc/meminfo", "MemAvailable:");
113 if (resident > limit_ || available < reserve_ + limit_ - std::min(resident, limit_))
114 throw MapResourcePressure("RAM headroom closed; working checkpoint retained for resume");
115 }
116};
117}
118#endif
std::chrono::steady_clock::time_point checked_
MapResources(const MapConfig &config)
Establish process limits for the selected runtime profile.
void checkpoint()
Check current process RSS and system reserve at a bounded cadence.
Own the survey mapper's validated configuration and mode defaults.
std::uint64_t procMemory(const char *path, const char *key)
Read a named Linux memory counter and convert its KiB value to bytes.
const std::shared_ptr< ThreadPoolBackend > & ensure_installed()
Install the process-wide Sensoraia OpenCV parallel backend once.
Header-only C++17 cv::parallel::ParallelForAPI backend for Sensoraia.
static pthread_cond_t available
Definition pose_log.c:73
Fully parsed settings shared by orthomap and XYZ export.
Definition map_config.h:41