Skip to content

Commit 9545ea4

Browse files
committed
perf(daemon): hash the executable image once per process; announce missed hook deadlines
The build-identity fingerprint is a SHA-256 over the entire ~295 MB executable: ~1.1 s per hash on an M5 Pro, ~2.3 s on the machine where this was first measured. A daemon pays it twice at start: the supervisor's startup capture and the runtime service's active-image check. - Cache the fingerprint per process at runtime_process_image_reference_acquire, keyed by (device, inode, size, mtime, change time): the identity the acquire path already verifies as stable across the hash, so a rebuilt or replaced binary rolls the key. On Windows the change time is FILE_BASIC_INFO.ChangeTime, which the kernel maintains and SetFileTime cannot reset. Every hit is still bracketed by the same before/after stat and process-maps checks, so a cached digest stays bound to the verified image. A freshly hashed digest is committed only after the acquire chain, including the post-hash stat check, has passed, and only under the identity observed after the hash, so an image that changed under the hash is never cached. The cache is process-local: it removes the daemon's second start-up hash (cold start 6.5 s -> 5.3 s with a 296 MB image); a fresh hook process still hashes its own image once. - A missed hook deadline is announced on stderr as well as the timeouts log, so a fired deadline is never a silent 0-byte exit 0. The breadcrumb is formatted before the log path is resolved, so stderr gets it even when the log cannot be opened. CBM_HOOK_DEADLINE_MS is unchanged. SHA-256 itself is unchanged: one portable scalar implementation on every platform. Refs #1335, #2058 Signed-off-by: Brian McNaboe <bmcnaboe@gmail.com>
1 parent 80eb92a commit 9545ea4

5 files changed

Lines changed: 556 additions & 23 deletions

File tree

‎src/cli/hook_augment.c‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,31 @@ static size_t g_ha_crumb_len = 0;
9696

9797
static void ha_deadline_exit(int sig) {
9898
(void)sig;
99-
if (g_ha_crumb_fd >= 0 && g_ha_crumb_len > 0) {
100-
ssize_t w = write(g_ha_crumb_fd, g_ha_crumb_msg, g_ha_crumb_len);
101-
(void)w;
99+
/* async-signal-safe only: write() the prepared breadcrumb to the timeouts
100+
* log (when it opened) and ALWAYS to stderr, so a fired deadline is a
101+
* visible event, never a silent 0-byte exit(0). */
102+
if (g_ha_crumb_len > 0) {
103+
if (g_ha_crumb_fd >= 0) {
104+
ssize_t w = write(g_ha_crumb_fd, g_ha_crumb_msg, g_ha_crumb_len);
105+
(void)w;
106+
}
107+
ssize_t e = write(STDERR_FILENO, g_ha_crumb_msg, g_ha_crumb_len);
108+
(void)e;
102109
}
103110
_exit(0);
104111
}
105112

106113
static void ha_open_crumb_log(int deadline_ms) {
114+
/* Pre-format the breadcrumb unconditionally: the handler may only write() a
115+
* prepared buffer, and formatting it before the log path is resolved means
116+
* stderr still gets the line even when the file cannot be opened (no HOME,
117+
* unwritable cache dir) — the "never silent" guarantee holds regardless. */
118+
int n = snprintf(g_ha_crumb_msg, sizeof(g_ha_crumb_msg),
119+
"hook-augment: deadline_exceeded ms=%d pid=%ld (raise via "
120+
"CBM_HOOK_DEADLINE_MS)\n",
121+
deadline_ms, (long)getpid());
122+
g_ha_crumb_len = (n > 0 && n < (int)sizeof(g_ha_crumb_msg)) ? (size_t)n : 0;
123+
107124
const char *override = getenv("CBM_HOOK_TIMEOUT_LOG"); /* tests + power users */
108125
char path[CBM_SZ_1K];
109126
if (override && override[0]) {
@@ -119,14 +136,6 @@ static void ha_open_crumb_log(int deadline_ms) {
119136
snprintf(path, sizeof(path), "%s/hook-augment-timeouts.log", dir);
120137
}
121138
g_ha_crumb_fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
122-
if (g_ha_crumb_fd < 0) {
123-
return;
124-
}
125-
int n = snprintf(g_ha_crumb_msg, sizeof(g_ha_crumb_msg),
126-
"hook-augment: deadline_exceeded ms=%d pid=%ld (raise via "
127-
"CBM_HOOK_DEADLINE_MS)\n",
128-
deadline_ms, (long)getpid());
129-
g_ha_crumb_len = (n > 0 && n < (int)sizeof(g_ha_crumb_msg)) ? (size_t)n : 0;
130139
}
131140

132141
int cbm_hook_augment_deadline_ms_for_testing(void) {

0 commit comments

Comments
 (0)