1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! Re-deriving an adopted sheep's start time from the operating system.
//!
//! [`Handover`](super::Handover) carries no `started_at`, and no serializer
//! for it would help: a [`tokio::time::Instant`] has no epoch and means
//! nothing outside the runtime that read it. The successor asks the process
//! table instead, which is authoritative about a pid it did not spawn in a
//! way a carried value could never be.
//!
//! Without this, a sheep that has been up three days reports `0s` in
//! `shep flock`'s UPTIME column the moment a handover completes. That is
//! the one column an operator reads to see whether anything moved, so a
//! zero there says the opposite of what the handover achieved.
//!
//! # What the operating system is asked for
//!
//! One number: the second the process started, since the Unix epoch. Both
//! platforms this module compiles for fill it while the process table row is
//! built, unconditionally, rather than under a [`ProcessRefreshKind`]
//! branch. macOS reads `pbi_start_tvsec` out of `proc_pidinfo`; Linux takes
//! field 22 of `/proc/<pid>/stat`, divides by the clock tick and adds the
//! boot time. `limits::sample`'s `.with_cpu()` tuning, whose whole point is
//! that a field IS gated on the refresh kind and silently reads zero
//! without it, therefore does not apply here.
//!
//! The answer is a wall-clock second, and the field being filled is a
//! monotonic [`Instant`], so the conversion goes through an age: how long
//! ago the process started, subtracted from now. That is the only bridge
//! between the two clocks, and it is why every step of it saturates.
use ;
use ;
use Instant;
/// The refresh kind [`start_epoch_secs`] asks sysinfo for.
///
/// Neither memory nor CPU: the start time is not gated on the refresh kind
/// on either platform (see this module's doc), so widening it would cost an
/// adoption the syscalls of the 15-second memory poll for figures nothing
/// here reads.
///
/// `.without_tasks()` for the reason `limits::sample` gives at length. Its
/// symptom cannot reach this module, which looks up exactly one pid and
/// reads no total, but on Linux it is still a `readdir` of
/// `/proc/<pid>/task/` and a row per thread, bought for nothing.
/// This machine's wall clock, in seconds since the Unix epoch.
///
/// A clock set before the epoch reads as `0`, which makes every process look
/// older than the epoch and so gives an age of zero once
/// [`age_from_epoch`] has saturated. Same answer as every other unusable
/// clock reading below, reached the same way.
/// The second `pid` started at, as the operating system reports it, or
/// `None` if it will not say.
///
/// A [`System`] of this call's own, built here and dropped at the end of it,
/// rather than the one `SysinfoSampler` retains for the memory poll. The
/// reason is not the one that makes `identify` do the same thing (a name
/// read out of a retained table is frozen at the pid's first sighting) but
/// a plainer one: this runs during a rehydrate, before a supervisor exists,
/// and there is no retained table to borrow.
///
/// Only `pid` is refreshed, never the whole machine. Adoption asks this once
/// per carried sheep, and a whole-table walk per sheep would make the cost
/// of a handover quadratic in a quantity (the machine's process count) that
/// has nothing to do with the flock's size.
///
/// A reported `0` is read as unknown rather than as the epoch. It is
/// sysinfo's initial value for the field, and a process that genuinely
/// started at midnight on 1 January 1970 is not a case worth preferring
/// over a field that was never filled.
/// The age of a process that started at `started_secs`, seen from a wall
/// clock reading `now_secs`, both in seconds since the Unix epoch.
///
/// Saturating rather than asserting, and that is a deliberate answer rather
/// than defensive habit. Neither reading is under this daemon's control: a
/// wall clock moves backwards whenever NTP steps it, a VM resumes from a
/// snapshot, or an operator sets the date, and a process started before such
/// a step then reports a start time in the future. Nothing can recover the
/// true age from two readings of a clock that lied about one of them, so
/// there is no repair to attempt and no invariant to assert. Zero is the
/// honest answer and also the harmless one, since it is what an operator
/// already sees from a fresh spawn.
///
/// The alternatives are both worse than a wrong column. A panic would take a
/// supervisor down over a clock adjustment, in the one code path whose
/// entire purpose is that a flock survives; a wrapping subtraction would
/// report the sheep as having started some hundreds of billions of years
/// ago, which the UPTIME column would print in full.
/// The `started_at` an adopted sheep running as `pid` should carry.
///
/// The process's age comes from the operating system and is subtracted from
/// this runtime's own clock, so a sheep that has been up three days is
/// adopted as three days old and `shep flock` reports its real uptime across
/// the handover.
///
/// Never fails, and deliberately: a pid the process table will not describe
/// falls back to [`Instant::now`], which reports an uptime restarting at the
/// handover. That is the case where the sheep exited between the
/// predecessor writing the blob and this image reading it, and one sheep's
/// uptime column is not worth refusing a whole rehydrate over.
pub