Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
clock_domain.hpp
Go to the documentation of this file.
1/*
2Internal helper: a stable identifier for "the clock this process reads" (a
3*clock domain*), used to key clock-offset consensus (clock_offset.hpp's
4ClockConsensus) so that several agents sharing one host's clock adopt one
5identical offset rather than each measuring independently and disagreeing by
6measurement noise.
7
8The domain is the *kernel*, not the host name: on Linux it is
9/proc/sys/kernel/random/boot_id, a value the kernel generates once at boot
10and keeps stable for its lifetime. This is the case that matters -- several
11containers sharing one kernel have different hostnames but read the exact
12same clock, so keying on hostname would split one clock domain into several
13and reintroduce the very disagreement this exists to prevent.
14
15macOS, Windows and any other platform fall back to the hostname. Correct for
16every non-container deployment; containers are not a macOS/Windows MADS
17target, so this is a documented limitation rather than a fragile
18uptime-based reconstruction of boot time.
19
20Not part of the installed SDK (src/detail/ is excluded from the LIB_HEADERS
21install glob in CMakeLists.txt, same as detail/plugin_cache.hpp).
22*/
23#pragma once
24
25#include <fstream>
26#include <string>
27
28#ifdef _WIN32
29#include <winsock2.h>
30#else
31#include <unistd.h>
32#endif
33
34#ifndef HOST_NAME_MAX
35#define HOST_NAME_MAX 255
36#endif
37
38namespace Mads::detail {
39
45inline const std::string &clock_domain_id() {
46 static const std::string id = [] {
47#ifdef __linux__
48 std::ifstream f("/proc/sys/kernel/random/boot_id");
49 if (f) {
50 std::string line;
51 std::getline(f, line);
52 if (!line.empty())
53 return line;
54 }
55#endif
56 char hostname[HOST_NAME_MAX + 1] = {0};
57 if (gethostname(hostname, HOST_NAME_MAX) == 0)
58 return std::string(hostname);
59 return std::string("unknown");
60 }();
61 return id;
62}
63
64} // namespace Mads::detail
#define HOST_NAME_MAX
Definition agent.hpp:59
const std::string & clock_domain_id()
Stable identifier for the clock this process reads. Computed once (function-local static,...