Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
clock_domain.hpp
Go to the documentation of this file.
1
/*
2
Internal helper: a stable identifier for "the clock this process reads" (a
3
*clock domain*), used to key clock-offset consensus (clock_offset.hpp's
4
ClockConsensus) so that several agents sharing one host's clock adopt one
5
identical offset rather than each measuring independently and disagreeing by
6
measurement noise.
7
8
The 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
10
and keeps stable for its lifetime. This is the case that matters -- several
11
containers sharing one kernel have different hostnames but read the exact
12
same clock, so keying on hostname would split one clock domain into several
13
and reintroduce the very disagreement this exists to prevent.
14
15
macOS, Windows and any other platform fall back to the hostname. Correct for
16
every non-container deployment; containers are not a macOS/Windows MADS
17
target, so this is a documented limitation rather than a fragile
18
uptime-based reconstruction of boot time.
19
20
Not part of the installed SDK (src/detail/ is excluded from the LIB_HEADERS
21
install 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
38
namespace
Mads::detail
{
39
45
inline
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
HOST_NAME_MAX
#define HOST_NAME_MAX
Definition
agent.hpp:59
Mads::detail
Definition
clock_domain.hpp:38
Mads::detail::clock_domain_id
const std::string & clock_domain_id()
Stable identifier for the clock this process reads. Computed once (function-local static,...
Definition
clock_domain.hpp:45
src
detail
clock_domain.hpp
Generated by
1.9.8