Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

698 líneas
36 KiB

  1. /* Exercise the actual daemon selection/action path. Native backend NOT linked. */
  2. #define main guard_program_main
  3. #include "../src/main.c"
  4. #undef main
  5. static double fake_now = 100;
  6. static double refresh_age, dispatch_delay;
  7. static sample_t fake_sample;
  8. static process_t fixtures[32];
  9. static unsigned nfixtures, real_requests, validation_requests, walk_requests, checks;
  10. static int fail_validation, fail_sample, partial, invalid_pid;
  11. typedef struct { int priority; char message[1536]; } syslog_record_t;
  12. static syslog_record_t syslog_records[64];
  13. static unsigned syslog_count, syslog_open_count, syslog_close_count;
  14. static int syslog_facility, fail_syslog;
  15. static double syslog_delay_seconds;
  16. static char syslog_ident[64];
  17. #define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL runtime line %d: %s\n", __LINE__, #x); exit(1); } checks++; } while (0)
  18. double monotime(void) { return fake_now; }
  19. int secure_file(const char *p, int flags, unsigned mode)
  20. { (void)p; (void)flags; (void)mode; errno = EPERM; return -1; }
  21. int secure_directory(const char *p) { (void)p; return -1; }
  22. int platform_init(void) { return 0; }
  23. void platform_fini(void) { }
  24. int platform_syslog_open(const char *ident, int facility)
  25. {
  26. syslog_open_count++; syslog_facility = facility;
  27. snprintf(syslog_ident, sizeof (syslog_ident), "%s", ident);
  28. return fail_syslog ? -1 : 0;
  29. }
  30. int platform_syslog_write(int priority, const char *message)
  31. {
  32. if (fail_syslog) { errno = EIO; return -1; }
  33. fake_now += syslog_delay_seconds;
  34. CHECK(syslog_count < sizeof (syslog_records) / sizeof (syslog_records[0]));
  35. syslog_records[syslog_count].priority = priority;
  36. snprintf(syslog_records[syslog_count].message,
  37. sizeof (syslog_records[syslog_count].message), "%s", message);
  38. syslog_count++;
  39. return 0;
  40. }
  41. void platform_syslog_close(void) { syslog_close_count++; }
  42. int platform_sample(sample_t *s)
  43. { *s = fake_sample; s->at = fake_now - refresh_age;
  44. if (fail_sample) { memset(s, 0, sizeof (*s)); errno = EIO; return -1; }
  45. return 0; }
  46. int platform_walk(int (*fn)(const process_t *, void *), void *arg, unsigned budget)
  47. {
  48. unsigned j; (void)budget;
  49. walk_requests++;
  50. for (j = 0; j < nfixtures; j++) if (fn(&fixtures[j], arg)) return -1;
  51. return partial ? -1 : 0;
  52. }
  53. static int mock_signal_errno, mock_validation_errno, mock_timeout_pid;
  54. static int mock_ticket_pid;
  55. int validation_start(void) { return 0; }
  56. int validation_handle(int ticket) { return ticket; }
  57. void validation_release(int ticket) { (void)ticket; }
  58. int validation_acquire(const config_t *c, const process_t *p, int real,
  59. uint64_t ceiling, unsigned budget_ms, int *ticket, validation_t *timing)
  60. {
  61. unsigned j; int found = 0;
  62. (void)real; (void)ceiling;
  63. memset(timing, 0, sizeof (*timing));
  64. CHECK(!protected_process(c, p));
  65. for (j = 0; j < nfixtures; j++)
  66. if (fixtures[j].pid == p->pid && same_process(&fixtures[j], p)) { found = 1; break; }
  67. validation_requests++;
  68. if (p->pid == mock_timeout_pid) {
  69. fake_now += budget_ms / 1000.0; timing->total_ms = budget_ms;
  70. errno = ETIMEDOUT; return -1;
  71. }
  72. if (fail_validation || p->pid == invalid_pid || !found) { errno = ESTALE; return -1; }
  73. *ticket = p->pid; mock_ticket_pid = p->pid; errno = mock_validation_errno;
  74. return 0; /* This test backend cannot send any signal. */
  75. }
  76. void platform_dispatch(int handle, enum action action, signal_result_t *out)
  77. {
  78. CHECK(handle == mock_ticket_pid && (action == TERM || action == KILL));
  79. real_requests++;
  80. memset(out, 0, sizeof (*out)); out->attempted = 1;
  81. out->attempt = fake_now; fake_now += dispatch_delay; out->returned = fake_now;
  82. out->write_ms = out->total_ms = 1000 * dispatch_delay;
  83. out->rc = mock_signal_errno ? -1 : (long)(2 * sizeof (long));
  84. out->error = mock_signal_errno;
  85. }
  86. static void reset(void)
  87. {
  88. defaults(&cfg); cfg.log_mib = 100;
  89. cfg.nzones = 2;
  90. strcpy(cfg.zones[0].uuid, "11111111-1111-4111-8111-111111111111");
  91. strcpy(cfg.zones[0].alias, "web-a"); cfg.zones[0].policy = ALLOW;
  92. strcpy(cfg.zones[1].uuid, "22222222-2222-4222-8222-222222222222");
  93. strcpy(cfg.zones[1].alias, "web-b"); cfg.zones[1].policy = ALLOW;
  94. memset(processes, 0, sizeof (processes)); memset(zone_stats, 0, sizeof (zone_stats));
  95. memset(candidate_cache, 0, sizeof (candidate_cache));
  96. memset(published_cache, 0, sizeof (published_cache));
  97. memset(rejected_identity, 0, sizeof (rejected_identity));
  98. rejected_identity_next = rejected_identity_count = 0;
  99. memset(metrics, 0, sizeof (metrics));
  100. memset(&controller, 0, sizeof (controller)); memset(fixtures, 0, sizeof (fixtures));
  101. engine_init(&engine); engine.state = CRITICAL; engine.danger = 1;
  102. stopping = 0; started = 0; log_ok = 1; log_bytes = 0;
  103. async_logging = scanner_async = 0;
  104. event_head = event_tail = event_count = dropped_events = 0;
  105. event_sequence = 0; scan_requested = scan_snapshot_requested = 0;
  106. syslog_ready = syslog_init_failed = 0;
  107. syslog_count = syslog_open_count = syslog_close_count = 0;
  108. syslog_facility = -1; fail_syslog = 0; syslog_ident[0] = 0;
  109. syslog_delay_seconds = 0;
  110. refresh_age = dispatch_delay = 0;
  111. mock_signal_errno = mock_validation_errno = mock_timeout_pid = 0;
  112. scan_generation = 0; scan_complete = published_scan_complete = 0;
  113. scanner_latest_complete_scan_at = latest_complete_scan_at = 0;
  114. published_latest_complete_scan_at = 0;
  115. fake_now = 100; partial = fail_sample = fail_validation = invalid_pid = 0;
  116. real_requests = validation_requests = walk_requests = 0;
  117. memset(&fake_sample, 0, sizeof (fake_sample)); fake_sample.at = fake_now;
  118. fake_sample.free = 60000; fake_sample.lots = 130875;
  119. fake_sample.des = 65437; fake_sample.min = 49077;
  120. fixtures[0].pid = 123; fixtures[0].ppid = 100; fixtures[0].zid = 9;
  121. fixtures[0].initpid = 99; fixtures[0].nlwp = 1; fixtures[0].start_sec = 1000;
  122. fixtures[0].zone_generation = 20; fixtures[0].rss_kib = 100000;
  123. strcpy(fixtures[0].uuid, cfg.zones[0].uuid); strcpy(fixtures[0].name, "php-fpm");
  124. nfixtures = 1;
  125. scan_processes(0);
  126. }
  127. static void populate_many_candidates(unsigned count)
  128. {
  129. unsigned i;
  130. reset();
  131. CHECK(count <= sizeof (fixtures) / sizeof (fixtures[0]));
  132. for (i = 0; i < count; i++) {
  133. fixtures[i] = fixtures[0]; fixtures[i].pid = 1000 + (int)i;
  134. fixtures[i].start_sec += i; fixtures[i].start_nsec += i;
  135. fixtures[i].rss_kib = 2000000 - 1000 * i;
  136. }
  137. nfixtures = count; fake_now = 101; scan_processes(0);
  138. for (i = 0; i < count; i++) CHECK(candidate_cache[i].valid);
  139. }
  140. static int queued_contains(const char *text)
  141. {
  142. unsigned i;
  143. for (i = 0; i < event_count; i++) {
  144. unsigned slot = (event_head + i) % EVENT_QUEUE_SIZE;
  145. if (strstr(event_queue[slot].body, text)) return 1;
  146. }
  147. return 0;
  148. }
  149. static void panic33_fixture(void)
  150. {
  151. FILE *f;
  152. char line[256];
  153. unsigned seconds, incomplete, expected, j, rows = 0;
  154. uint64_t free_pages;
  155. delta_t d = {1, 5, 100};
  156. int removed = 0;
  157. populate_many_candidates(16);
  158. cfg.armed = 1; async_logging = 1;
  159. engine_init(&engine);
  160. f = fopen("tests/fixtures/panic33_persistent_floor_budget_exhaustion_2026_10_08.csv", "r");
  161. CHECK(f != NULL);
  162. while (fgets(line, sizeof (line), f)) {
  163. if (line[0] == '#') continue;
  164. CHECK(sscanf(line, "%" SCNu64 ",%u,%u,%u", &free_pages, &seconds, &incomplete, &expected) == 4);
  165. for (j = 0; j < seconds; j++) {
  166. fake_now++; fake_sample.at = fake_now; fake_sample.free = free_pages;
  167. fake_sample.scan = 1000000; fake_sample.pageout_ok = 1;
  168. if (incomplete && !removed) {
  169. /* Cached PID disappeared; its replacement is smaller than survivors. */
  170. fixtures[8].pid = 2000; fixtures[8].start_sec++;
  171. fixtures[8].rss_kib = 100000; removed = 1;
  172. }
  173. engine_step(&engine, &cfg, &fake_sample, d, d, d);
  174. refresh_candidate_cache();
  175. act(&fake_sample);
  176. partial = (int)incomplete;
  177. scan_processes(0);
  178. }
  179. CHECK(controller.nvictims == expected);
  180. rows++;
  181. }
  182. fclose(f);
  183. CHECK(rows == 7 && !scan_complete);
  184. CHECK(queued_contains("reason=emergency_victim_budget_exhausted"));
  185. CHECK(queued_contains("event=budget_override reason=persistent_floor"));
  186. CHECK(queued_contains("event=victim_budget tier=hard_floor used=16 limit=16 remaining=0"));
  187. CHECK(was_victim(&controller, &fixtures[8]));
  188. CHECK(real_requests > 16); /* TERM escalation plus direct FLOOR KILL. */
  189. fake_sample.free = 6513; fake_sample.at = ++fake_now;
  190. act(&fake_sample);
  191. CHECK(queued_contains("reason=hard_floor_victim_budget_exhausted"));
  192. CHECK(controller.nvictims == 16);
  193. puts("PASS panic33: partial recoveries, budgets, dead PID replacement, incomplete scan, hard ceiling16");
  194. }
  195. static void partial_cache_tests(void)
  196. {
  197. process_t replacement, survivor;
  198. unsigned i;
  199. int found = 0;
  200. populate_many_candidates(16);
  201. survivor = candidate_cache[1].p;
  202. invalidate_candidate(&candidate_cache[0].p);
  203. scanner_async = 1;
  204. replacement = fixtures[0]; replacement.pid = 9000; replacement.start_sec++;
  205. replacement.rss_kib = 70000;
  206. scan_at = ++fake_now; scan_generation++;
  207. /* Callback publishes before platform_walk returns (or blocks). */
  208. CHECK(visit(&replacement, NULL) == 0);
  209. refresh_candidate_cache();
  210. for (i = 0; i < CACHED_CANDIDATES; i++)
  211. if (candidate_cache[i].valid && same_process(&candidate_cache[i].p, &replacement)) found = 1;
  212. CHECK(found && !scan_complete);
  213. CHECK(same_process(&candidate_cache[0].p, &survivor));
  214. CHECK(pthread_mutex_lock(&cache_mutex) == 0);
  215. refresh_candidate_cache(); /* trylock only: must return while scanner holds lock. */
  216. CHECK(pthread_mutex_unlock(&cache_mutex) == 0);
  217. replacement.pid = 9001; replacement.rss_kib = 65536;
  218. CHECK(visit(&replacement, NULL) == 0);
  219. refresh_candidate_cache();
  220. CHECK(same_process(&candidate_cache[0].p, &survivor));
  221. CHECK(candidate_cache[15].p.pid == 9000);
  222. scanner_async = 0;
  223. puts("PASS incremental pre-return publication, survivor ordering, bounded merge and nonblocking handoff");
  224. }
  225. static void critical_path_faults(void)
  226. {
  227. struct timespec before, after; double elapsed_ms; unsigned walks;
  228. struct stat st_before, st_after;
  229. {
  230. FILE *fixture; char line[160], event[64];
  231. unsigned seconds, rows = 0, decision_at = 0, sent_at = 0;
  232. uint64_t free_pages, nscan;
  233. fixture = fopen("tests/fixtures/panic32_daemon_stall_2026_09_22.csv", "r");
  234. CHECK(fixture != NULL); CHECK(fgets(line, sizeof (line), fixture) != NULL);
  235. while (fscanf(fixture, "%u,%" SCNu64 ",%" SCNu64 ",%63s",
  236. &seconds, &free_pages, &nscan, event) == 4) {
  237. rows++;
  238. if (!strcmp(event, "kill_decision")) decision_at = seconds;
  239. if (!strcmp(event, "kill_sent_after_145s")) sent_at = seconds;
  240. }
  241. fclose(fixture);
  242. CHECK(rows == 7 && sent_at - decision_at == 145);
  243. }
  244. {
  245. queued_event_t event;
  246. reset(); async_logging = 1; syslog_ready = 1;
  247. syslog_delay_seconds = 0.200;
  248. memset(&event, 0, sizeof (event));
  249. strcpy(event.body, "event=test_syslog_stall");
  250. event.wall_time = time(NULL); event.mono_time = fake_now;
  251. event.syslog_channel = 1; event.priority = LOG_WARNING; event.sequence = 1;
  252. CHECK(write_event_now(&event) == 0);
  253. CHECK(queued_contains("event=logging_stall"));
  254. CHECK(queued_contains("channel=syslog"));
  255. CHECK(queued_contains("syslog_latency_ms=200.000"));
  256. }
  257. populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
  258. engine.danger = 1; fake_sample.free = 40000; fake_now += 180;
  259. async_logging = 1; fail_syslog = 1;
  260. CHECK(fstat(logfd, &st_before) == 0); walks = walk_requests;
  261. clock_gettime(CLOCK_MONOTONIC, &before); act(&fake_sample);
  262. clock_gettime(CLOCK_MONOTONIC, &after);
  263. elapsed_ms = 1000.0 * (after.tv_sec - before.tv_sec) +
  264. (after.tv_nsec - before.tv_nsec) / 1000000.0;
  265. CHECK(real_requests == 1 && validation_requests == 1);
  266. CHECK(elapsed_ms < 100.0 && walk_requests == walks && syslog_count == 0);
  267. CHECK(fstat(logfd, &st_after) == 0 && st_after.st_size == st_before.st_size);
  268. CHECK(queued_contains("candidate_source=stale_cache_revalidated"));
  269. CHECK(queued_contains("dispatch_latency_ms="));
  270. CHECK(queued_contains("candidate_validate_latency_ms="));
  271. CHECK(queued_contains("kill_latency_ms="));
  272. CHECK(queued_contains("log_enqueue_latency_ms="));
  273. fake_now += cfg.emergency_term_s;
  274. clock_gettime(CLOCK_MONOTONIC, &before); act(&fake_sample);
  275. clock_gettime(CLOCK_MONOTONIC, &after);
  276. elapsed_ms = 1000.0 * (after.tv_sec - before.tv_sec) +
  277. (after.tv_nsec - before.tv_nsec) / 1000000.0;
  278. CHECK(real_requests == 2 && validation_requests == 2 && elapsed_ms < 100.0);
  279. CHECK(queued_contains("signal=SIGKILL"));
  280. puts("PASS panic32 fault: logger/syslog stopped 180s, stale cache revalidated, signal dispatch <100ms");
  281. populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
  282. engine.danger = 1; fake_sample.free = 40000; async_logging = scanner_async = 1;
  283. scan_requested = 1; walks = walk_requests;
  284. act(&fake_sample);
  285. CHECK(real_requests == 1 && walk_requests == walks);
  286. puts("PASS stalled/incomplete scanner cannot delay or trigger a rescue-path proc scan");
  287. populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
  288. engine.danger = 1; fake_sample.free = 40000; async_logging = 1;
  289. walks = walk_requests;
  290. while (controller.nvictims < cfg.max_emergency_victims) {
  291. act(&fake_sample);
  292. if (controller.has_pending) { fake_now += cfg.emergency_term_s; act(&fake_sample); }
  293. fake_now += 0.01;
  294. }
  295. CHECK(controller.nvictims == 8 && real_requests == 16 && walk_requests == walks);
  296. act(&fake_sample); CHECK(real_requests == 16);
  297. puts("PASS 16-entry stale rescue cache supplies all 8 EMERGENCY victims without scan");
  298. populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
  299. engine.danger = engine.floor = 1; fake_sample.free = 20000; async_logging = 1;
  300. walks = walk_requests;
  301. while (controller.nvictims < cfg.max_floor_victims) {
  302. act(&fake_sample); fake_now += 0.01;
  303. }
  304. CHECK(controller.nvictims == 12 && real_requests == 12 && walk_requests == walks);
  305. act(&fake_sample); CHECK(real_requests == 12);
  306. puts("PASS 16-entry stale rescue cache supplies all 12 FLOOR victims without scan");
  307. populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
  308. engine.danger = 1; fake_sample.free = 40000; async_logging = 1;
  309. CHECK(pthread_mutex_lock(&event_mutex) == 0);
  310. clock_gettime(CLOCK_MONOTONIC, &before); act(&fake_sample);
  311. clock_gettime(CLOCK_MONOTONIC, &after);
  312. CHECK(pthread_mutex_unlock(&event_mutex) == 0);
  313. elapsed_ms = 1000.0 * (after.tv_sec - before.tv_sec) +
  314. (after.tv_nsec - before.tv_nsec) / 1000000.0;
  315. CHECK(real_requests == 1 && elapsed_ms < 100.0 && dropped_events > 0);
  316. puts("PASS saturated/locked event queue drops telemetry without delaying the signal");
  317. }
  318. static void floor_errno_fixture(void)
  319. {
  320. FILE *f; char line[160], event[80];
  321. unsigned seconds, rows = 0, i, j;
  322. uint64_t free_pages, scans;
  323. delta_t zero = {0, 0, 0};
  324. const int prior[] = {EAGAIN, ENOMEM};
  325. const int errors[] = {0, ESRCH, EPERM, EINVAL, EAGAIN};
  326. populate_many_candidates(16); cfg.armed = 1; async_logging = 1;
  327. engine_init(&engine);
  328. f = fopen("tests/fixtures/floor_free_pages_486_2026_09_23.csv", "r");
  329. CHECK(f != NULL && fgets(line, sizeof (line), f) != NULL);
  330. while (fscanf(f, "%u,%" SCNu64 ",%" SCNu64 ",%79s", &seconds,
  331. &free_pages, &scans, event) == 4) {
  332. fake_now = 200 + seconds; fake_sample.at = fake_now;
  333. fake_sample.free = free_pages; fake_sample.scan = scans;
  334. engine_step(&engine, &cfg, &fake_sample, zero, zero, zero);
  335. if (seconds == 1) {
  336. CHECK(engine.state == EMERGENCY && engine.floor);
  337. mock_timeout_pid = candidate_cache[0].p.pid;
  338. act(&fake_sample);
  339. CHECK(real_requests == 1 && mock_ticket_pid == candidate_cache[1].p.pid);
  340. CHECK(queued_contains("reason=validation_timeout rank=1 latency_ms=50.000"));
  341. CHECK(queued_contains("rank=2"));
  342. CHECK(fake_now - fake_sample.at < 0.100);
  343. }
  344. rows++;
  345. }
  346. fclose(f); CHECK(rows == 3 && engine.state == RECOVERY);
  347. for (i = 0; i < 2; i++) for (j = 0; j < 5; j++) {
  348. char expected[120];
  349. reset(); cfg.armed = 1; async_logging = 1;
  350. mock_validation_errno = prior[i]; mock_signal_errno = errors[j];
  351. act(&fake_sample);
  352. snprintf(expected, sizeof (expected), "result=%s errno=%d syscall=write operation=PCKILL attempted=1 rc=%ld errno_name=%s",
  353. errors[j] ? "failed" : "sent", errors[j],
  354. errors[j] ? -1L : (long)(2 * sizeof (long)), signal_errno_name(errors[j]));
  355. CHECK(queued_contains(expected));
  356. CHECK(queued_contains("event=signal_sent") == (errors[j] == 0));
  357. CHECK(queued_contains("validate_start_time_ms="));
  358. }
  359. puts("PASS floor486 fixture, 50ms timeout fallback rank1->2, syscall errno attribution and validation substep logs");
  360. }
  361. static void diagnostic_fixtures(void)
  362. {
  363. FILE *f; char line[128], stamp[32], expected[128];
  364. unsigned signals, failure, rows = 0; uint64_t free_pages; double ms;
  365. reset(); cfg.armed = 1; async_logging = 1; engine.state = EMERGENCY;
  366. f = fopen("tests/fixtures/refresh_recovery_and_failure.csv", "r");
  367. CHECK(f && fgets(line, sizeof (line), f));
  368. while (fscanf(f, "%" SCNu64 ",%u,%u", &free_pages, &failure, &signals) == 3) {
  369. fake_sample.free = free_pages; fail_sample = (int)failure;
  370. act(&fake_sample); CHECK(real_requests == signals); rows++;
  371. }
  372. fclose(f); CHECK(rows == 3);
  373. CHECK(queued_contains("reason=free_above_desfree state=EMERGENCY free_pages=70000 desfree=65437 minfree=49077 refresh_ok=1"));
  374. CHECK(queued_contains("reason=refresh_failed state=EMERGENCY free_pages=20000 desfree=65437 minfree=49077 refresh_ok=0"));
  375. CHECK(queued_contains("refresh_errno=5 refresh_error=EIO"));
  376. reset(); cfg.armed = 1; async_logging = 1; refresh_age = 2.1;
  377. act(&fake_sample); CHECK(real_requests == 0);
  378. CHECK(queued_contains("reason=refresh_failed")); CHECK(queued_contains("refresh_error=stale_sample"));
  379. reset(); cfg.armed = 1; async_logging = 1; fake_sample.free = fake_sample.des;
  380. act(&fake_sample); CHECK(real_requests == 0 && queued_contains("reason=free_above_desfree"));
  381. f = fopen("tests/fixtures/pckill_latency_2026_09_28.csv", "r");
  382. CHECK(f && fgets(line, sizeof (line), f)); rows = 0;
  383. while (fscanf(f, "%31[^,],%lf\n", stamp, &ms) == 2) {
  384. reset(); cfg.armed = 1; async_logging = 1; fake_sample.free = 486;
  385. engine.state = EMERGENCY; dispatch_delay = ms / 1000;
  386. act(&fake_sample);
  387. CHECK(real_requests == 1 && queued_contains("event=signal_sent"));
  388. CHECK(queued_contains("event=signal_dispatch_slow signal=SIGKILL"));
  389. snprintf(expected, sizeof (expected), "pckill_write_latency_ms=%.3f", ms);
  390. CHECK(queued_contains(expected)); rows++;
  391. }
  392. fclose(f); CHECK(rows == 5);
  393. reset(); cfg.armed = 1; async_logging = 1; dispatch_delay = .000010;
  394. act(&fake_sample); CHECK(queued_contains("pckill_write_latency_ms=0.010"));
  395. CHECK(!queued_contains("event=signal_dispatch_slow"));
  396. reset(); cfg.armed = 1; async_logging = 1; dispatch_delay = .100;
  397. act(&fake_sample); CHECK(!queued_contains("event=signal_dispatch_slow"));
  398. reset(); cfg.armed = 1; async_logging = 1; dispatch_delay = .101;
  399. act(&fake_sample); CHECK(queued_contains("event=signal_dispatch_slow signal=SIGTERM"));
  400. puts("PASS recovery/failure/stale refresh reasons, desfree equality, no second victim, PCKILL subphases and slow-event threshold");
  401. }
  402. static void load_global_ranking_fixture(void)
  403. {
  404. FILE *f;
  405. char line[256], uuid[64], alias[64], name[32];
  406. int zid, pid;
  407. uint64_t generation, rss;
  408. reset();
  409. cfg.nzones = cfg.nnames = 0;
  410. strcpy(cfg.protected_names[cfg.nnames++], "mysqld");
  411. strcpy(cfg.protected_names[cfg.nnames++], "zone-overhead");
  412. memset(processes, 0, sizeof (processes));
  413. memset(zone_stats, 0, sizeof (zone_stats));
  414. memset(candidate_cache, 0, sizeof (candidate_cache));
  415. memset(&controller, 0, sizeof (controller));
  416. memset(fixtures, 0, sizeof (fixtures));
  417. scan_generation = 0; scan_complete = 0; latest_complete_scan_at = 0;
  418. nfixtures = 0; fake_now = 100;
  419. f = fopen("tests/fixtures/global_victim_ranking_smartos_2026_09.csv", "r");
  420. CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
  421. while (fscanf(f, " %63[^,],%63[^,],%d,%" SCNu64 ",%d,%31[^,],%" SCNu64,
  422. uuid, alias, &zid, &generation, &pid, name, &rss) == 7) {
  423. process_t *p;
  424. const zone_rule_t *rule = zone_rule(&cfg, uuid);
  425. CHECK(nfixtures < sizeof (fixtures) / sizeof (fixtures[0]));
  426. if (!rule) {
  427. zone_rule_t *z;
  428. CHECK(cfg.nzones < MAX_ZONES); z = &cfg.zones[cfg.nzones++];
  429. strcpy(z->uuid, uuid); strcpy(z->alias, alias); z->policy = ALLOW;
  430. }
  431. p = &fixtures[nfixtures++];
  432. p->pid = pid; p->ppid = 100; p->zid = zid; p->initpid = 9000 + zid;
  433. p->nlwp = 1; p->start_sec = 1000 + pid; p->start_nsec = pid;
  434. p->zone_generation = generation; p->rss_kib = rss;
  435. strcpy(p->uuid, uuid); strcpy(p->name, name);
  436. }
  437. fclose(f); CHECK(nfixtures == 16 && cfg.nzones == 5);
  438. scan_processes(1);
  439. }
  440. int main(void)
  441. {
  442. FILE *log = tmpfile(); char contents[65536]; size_t n;
  443. const process_t *chosen;
  444. unsigned second;
  445. CHECK(log != NULL); logfd = fileno(log);
  446. reset(); act(&fake_sample);
  447. CHECK(real_requests == 0 && validation_requests == 1 && controller.has_pending);
  448. fake_now += 5; scan_processes(0); act(&fake_sample);
  449. CHECK(real_requests == 0 && !controller.has_pending);
  450. rewind(log); n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  451. CHECK(strstr(contents, "WOULD_ACTION=SIGTERM") != NULL);
  452. CHECK(strstr(contents, "WOULD_ACTION=SIGKILL") != NULL);
  453. CHECK(strstr(contents, "ACTION_INTENT=") == NULL);
  454. CHECK(fseek(log, 0, SEEK_END) == 0);
  455. puts("PASS actual daemon dry-run path records WOULD_ACTION=SIGTERM/SIGKILL; zero real requests");
  456. reset();
  457. fake_sample.arc_ok = 1;
  458. fake_sample.swap_virtual_ok = fake_sample.swap_physical_ok = fake_sample.pageout_ok = 1;
  459. for (second = 0; second <= 60; second++) {
  460. fake_sample.at = 100 + second;
  461. fake_sample.arc = UINT64_C(6000000000) + 1000 * second;
  462. fake_sample.swap_alloc = 7000000 + 3 * second;
  463. fake_sample.swap_reserved = 480000 + 2 * second;
  464. fake_sample.swap_available = 6200000 - second;
  465. fake_sample.swap_physical_used_bytes = UINT64_C(4143648768) + 4096 * second;
  466. fake_sample.anon_pageout_pages = 1000 + 2 * second;
  467. record_pressure_metrics(&fake_sample);
  468. record_auxiliary_metrics(&fake_sample);
  469. }
  470. log_sample(&fake_sample);
  471. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  472. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  473. CHECK(strstr(contents, "swap_committed_ok=1 swap_committed_pages=7480300") != NULL);
  474. CHECK(strstr(contents, "swap_physical_used_ok=1 swap_physical_used_bytes=4143894528") != NULL);
  475. CHECK(strstr(contents, "anon_pageout_ok=1 anon_pageout_pages=1120") != NULL);
  476. CHECK(strstr(contents, "swap_alloc_pages=7000180 swap_reserved_pages=480120 swap_available_pages=6199940") != NULL);
  477. CHECK(strstr(contents, "metric=swap_committed_pages d5=+25@5.00s d15=+75@15.00s d60=+300@60.00s") != NULL);
  478. CHECK(strstr(contents, "metric=swap_physical_used_bytes d5=+20480@5.00s d15=+61440@15.00s d60=+245760@60.00s") != NULL);
  479. CHECK(strstr(contents, "metric=anon_pageout_pages d5=+10@5.00s d15=+30@15.00s d60=+120@60.00s") != NULL);
  480. CHECK(strstr(contents, "swap_used_pages") == NULL);
  481. CHECK(fseek(log, 0, SEEK_END) == 0);
  482. puts("PASS continuous sample contract and 5/15/60 deltas for commitment, physical swap and pageout");
  483. reset(); cfg.armed = 1; act(&fake_sample);
  484. CHECK(real_requests == 1);
  485. fake_now += 5; scan_processes(0); act(&fake_sample); CHECK(real_requests == 2);
  486. reset(); cfg.armed = 1; started = 80; fake_now = 101; scan_processes(0);
  487. CHECK(candidate_cache[0].valid && engine.danger && walk_requests == 2);
  488. act(&fake_sample); CHECK(real_requests == 0 && candidate_cache[0].valid);
  489. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  490. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  491. CHECK(strstr(contents, "event=action_blocked reason=armed_warmup") != NULL);
  492. CHECK(fseek(log, 0, SEEK_END) == 0);
  493. reset(); cfg.armed = 1; log_ok = 0; act(&fake_sample); CHECK(real_requests == 1);
  494. reset(); cfg.armed = 1; partial = 1; fake_now++; scan_processes(0);
  495. act(&fake_sample); CHECK(real_requests == 1 && !scan_complete);
  496. reset(); cfg.armed = 1; fail_sample = 1; act(&fake_sample); CHECK(real_requests == 0);
  497. reset(); cfg.armed = 1; fail_validation = 1; act(&fake_sample); CHECK(real_requests == 0);
  498. reset(); cfg.armed = 1; fake_sample.free = 150000; act(&fake_sample); CHECK(real_requests == 0);
  499. reset(); cfg.armed = 1; fake_now += 16; act(&fake_sample); CHECK(real_requests == 1);
  500. reset(); cfg.armed = 1; stopping = 1; act(&fake_sample); CHECK(real_requests == 0);
  501. reset(); fake_sample.free = 20000; act(&fake_sample);
  502. CHECK(!controller.has_pending && controller.nvictims == 1);
  503. puts("PASS armed-path mock requests, explicit warmup, stale cache, logging/sampling failures, stop and fresh floor");
  504. reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
  505. fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
  506. strcpy(fixtures[1].uuid, cfg.zones[1].uuid); nfixtures = 2;
  507. fake_now = 101; scan_processes(0); chosen = candidate(); CHECK(chosen && chosen->pid == 124);
  508. /* Small zone grows fast enough to outweigh a larger steady zone. */
  509. fixtures[0].rss_kib = 600000; fake_now = 161; scan_processes(0);
  510. chosen = candidate(); CHECK(chosen && chosen->pid == 123);
  511. cfg.zones[0].policy = PROTECT; fake_now = 162; scan_processes(0);
  512. chosen = candidate(); CHECK(chosen && chosen->pid == 124);
  513. fixtures[1].zid = 0; fake_now = 163; scan_processes(0); CHECK(candidate() == NULL);
  514. puts("PASS global score uses process RSS/growth; protected zones and global zone excluded");
  515. load_global_ranking_fixture();
  516. CHECK(candidate_cache[0].valid && candidate_cache[1].valid && candidate_cache[2].valid);
  517. CHECK(!strcmp(candidate_cache[0].p.name, "clamd"));
  518. CHECK(!strcmp(candidate_cache[1].p.name, "clamd"));
  519. CHECK(!strcmp(candidate_cache[2].p.name, "clamd"));
  520. CHECK(candidate_cache[0].p.rss_kib > 1300000 &&
  521. candidate_cache[2].p.rss_kib > 1300000);
  522. CHECK(strcmp(candidate_cache[0].p.uuid, candidate_cache[1].p.uuid));
  523. CHECK(strcmp(candidate_cache[1].p.uuid, candidate_cache[2].p.uuid));
  524. for (second = 0; second < CACHED_CANDIDATES; second++) {
  525. CHECK(strcmp(candidate_cache[second].p.name, "mysqld"));
  526. CHECK(strcmp(candidate_cache[second].p.name, "zone-overhead"));
  527. }
  528. for (second = 0; second < nfixtures; second++)
  529. if (!strcmp(fixtures[second].name, "mysqld"))
  530. CHECK(protected_process(&cfg, &fixtures[second]));
  531. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  532. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  533. {
  534. char *mysql = strstr(contents, "comm=mysqld");
  535. char *ineligible = mysql ? strstr(mysql, "eligible=0") : NULL;
  536. char *line_end = mysql ? strchr(mysql, '\n') : NULL;
  537. CHECK(mysql && ineligible && line_end && ineligible < line_end);
  538. }
  539. CHECK(fseek(log, 0, SEEK_END) == 0);
  540. fixtures[nfixtures] = fixtures[0]; fixtures[nfixtures].pid = 9001;
  541. fixtures[nfixtures].zid = 90; fixtures[nfixtures].zone_generation = 900;
  542. fixtures[nfixtures].rss_kib = UINT64_C(9000000);
  543. strcpy(fixtures[nfixtures].uuid, "66666666-6666-4666-8666-666666666666");
  544. strcpy(fixtures[nfixtures].name, "unmapped-hog"); nfixtures++;
  545. fixtures[nfixtures] = fixtures[0]; fixtures[nfixtures].pid = 9002;
  546. fixtures[nfixtures].zid = 0; fixtures[nfixtures].rss_kib = UINT64_C(11000000);
  547. strcpy(fixtures[nfixtures].name, "global-hog"); nfixtures++;
  548. fake_now = 101; scan_processes(0);
  549. for (second = 0; second < CACHED_CANDIDATES; second++)
  550. CHECK(candidate_cache[second].p.pid != 9001 && candidate_cache[second].p.pid != 9002);
  551. for (second = 0; second < nfixtures; second++)
  552. if (fixtures[second].pid == 1103) fixtures[second].rss_kib = 600000;
  553. fake_now = 106; scan_processes(0);
  554. CHECK(candidate_cache[0].p.pid == 1103);
  555. CHECK(candidate_cache[0].growth5_component > 0 &&
  556. candidate_cache[0].total_score > candidate_cache[1].total_score);
  557. {
  558. cached_candidate_t scored;
  559. zone_t zone;
  560. memset(&scored, 0, sizeof (scored)); memset(&zone, 0, sizeof (zone));
  561. scored.p.rss_kib = 100000; scored.d5.valid = scored.d15.valid = 1;
  562. scored.d60.valid = 1; scored.d5.value = 1000;
  563. scored.d15.value = 2000; scored.d60.value = 3000;
  564. scan_at = 60; history_add(&zone.history, 0, 1000000);
  565. history_add(&zone.history, 60, 1100000); zone.rss = 1100000;
  566. score_candidate(&scored, &zone);
  567. CHECK(scored.growth5_component == 4000);
  568. CHECK(scored.growth15_component == 4000);
  569. CHECK(scored.growth60_component == 3000);
  570. CHECK(scored.zone_component <= scored.p.rss_kib / 4);
  571. }
  572. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  573. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  574. CHECK(strstr(contents, "rss_component=") != NULL);
  575. CHECK(strstr(contents, "growth5_component=") != NULL);
  576. CHECK(strstr(contents, "growth15_component=") != NULL);
  577. CHECK(strstr(contents, "growth60_component=") != NULL);
  578. CHECK(strstr(contents, "zone_component=") != NULL);
  579. CHECK(strstr(contents, "total_score=") != NULL);
  580. CHECK(fseek(log, 0, SEEK_END) == 0);
  581. puts("PASS SmartOS fixture produces a global cross-zone top-3 and bounded zone context");
  582. reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
  583. fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
  584. strcpy(fixtures[1].uuid, cfg.zones[1].uuid);
  585. fixtures[2] = fixtures[0]; fixtures[2].pid = 125; fixtures[2].rss_kib = 800000;
  586. nfixtures = 3; fake_now = 101; scan_processes(0);
  587. CHECK(candidate_cache[0].valid && candidate_cache[1].valid && candidate_cache[2].valid);
  588. invalid_pid = candidate_cache[0].p.pid;
  589. partial = 1; fake_now = 102; scan_processes(0);
  590. CHECK(!scan_complete && candidate_cache[0].p.pid == invalid_pid);
  591. {
  592. unsigned walks_before = walk_requests;
  593. engine.state = EMERGENCY; engine.danger = 1; fake_sample.free = 40000;
  594. act(&fake_sample);
  595. CHECK(walk_requests == walks_before); /* O(1): no /proc walk in action path. */
  596. CHECK(controller.has_pending && controller.pending.pid != invalid_pid);
  597. CHECK(validation_requests >= 2 && real_requests == 0);
  598. }
  599. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  600. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  601. CHECK(strstr(contents, "candidate_source=fallback_candidate") != NULL);
  602. CHECK(strstr(contents, "scan_complete=0") != NULL);
  603. CHECK(fseek(log, 0, SEEK_END) == 0);
  604. puts("PASS incomplete scan preserves cache; emergency uses O(1) fallback without a new walk");
  605. reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
  606. fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
  607. strcpy(fixtures[1].uuid, cfg.zones[1].uuid); nfixtures = 2;
  608. fake_now = 101; scan_processes(0);
  609. for (second = 0; second < nfixtures; second++)
  610. if (fixtures[second].pid == candidate_cache[0].p.pid) fixtures[second].start_nsec++;
  611. engine.state = EMERGENCY; engine.danger = 1; fake_sample.free = 40000;
  612. act(&fake_sample);
  613. CHECK(controller.has_pending && !same_process(&controller.pending, &candidate_cache[0].p));
  614. puts("PASS PID/start-time reuse rejects rank 1 and selects cached fallback");
  615. reset();
  616. CHECK(platform_syslog_open("o6h-memory-guard", cfg.syslog_facility) == 0);
  617. syslog_ready = 1;
  618. CHECK(!strcmp(syslog_ident, "o6h-memory-guard") && syslog_facility == 5 &&
  619. syslog_open_count == 1);
  620. emit_important(LOG_INFO, "event=start version=%s mode=dry-run", VERSION);
  621. CHECK(syslog_count == 1 && syslog_records[0].priority == LOG_INFO &&
  622. strstr(syslog_records[0].message, "event=start version=" VERSION) != NULL);
  623. log_sample(&fake_sample); scan_processes(0);
  624. CHECK(syslog_count == 1); /* High-volume sample/delta/proc/cache events stay local. */
  625. {
  626. char message[512]; sample_t s = fake_sample;
  627. s.free = s.min;
  628. CHECK(!transition_message(message, sizeof (message), NORMAL, EMERGENCY, &s, 1));
  629. CHECK(strstr(message, "reason=threshold_jump") != NULL);
  630. CHECK(strstr(message, "skipped_states=PRE-PRESSURE,PRESSURE,CRITICAL") != NULL);
  631. syslog_event(state_priority(EMERGENCY, 0), "%s", message);
  632. CHECK(syslog_count == 2 && syslog_records[1].priority == LOG_CRIT);
  633. }
  634. cfg.armed = 0; act(&fake_sample);
  635. CHECK(syslog_count == 3 && real_requests == 0);
  636. reset(); cfg.armed = 1; started = 80; fake_now = 100;
  637. CHECK(platform_syslog_open("o6h-memory-guard", cfg.syslog_facility) == 0);
  638. syslog_ready = 1; act(&fake_sample);
  639. CHECK(real_requests == 0 && syslog_count == 1 &&
  640. syslog_records[0].priority == LOG_WARNING &&
  641. strstr(syslog_records[0].message, "event=action_blocked reason=armed_warmup") != NULL);
  642. reset(); cfg.armed = 1;
  643. CHECK(platform_syslog_open("o6h-memory-guard", cfg.syslog_facility) == 0);
  644. syslog_ready = 1; act(&fake_sample);
  645. CHECK(real_requests == 1 && syslog_count == 3);
  646. CHECK(strstr(syslog_records[0].message, "event=victim_budget tier=normal") != NULL);
  647. CHECK(syslog_records[1].priority == LOG_ERR &&
  648. strstr(syslog_records[1].message, "event=action_decision state=CRITICAL") != NULL &&
  649. strstr(syslog_records[1].message, "result=sent") != NULL);
  650. CHECK(syslog_records[2].priority == LOG_ERR &&
  651. strstr(syslog_records[2].message, "event=signal_sent state=CRITICAL") != NULL &&
  652. strstr(syslog_records[2].message, "signal=SIGTERM") != NULL &&
  653. strstr(syslog_records[2].message, "UUID=") != NULL &&
  654. strstr(syslog_records[2].message, "alias=web-a") != NULL &&
  655. strstr(syslog_records[2].message, "pid=123") != NULL &&
  656. strstr(syslog_records[2].message, "comm=php-fpm") != NULL &&
  657. strstr(syslog_records[2].message, "rss_kib=100000") != NULL &&
  658. strstr(syslog_records[2].message, "free_pages=60000") != NULL);
  659. reset(); cfg.armed = 1; fail_syslog = 1; syslog_ready = 1;
  660. act(&fake_sample); CHECK(real_requests == 1 && controller.has_pending);
  661. reset(); cfg.syslog_enabled = 0; syslog_ready = 0;
  662. emit_important(LOG_INFO, "event=start disabled_test=yes"); CHECK(syslog_count == 0);
  663. CHECK(state_priority(PRE_PRESSURE, 0) == LOG_NOTICE);
  664. CHECK(state_priority(PRESSURE, 0) == LOG_WARNING);
  665. CHECK(state_priority(CRITICAL, 0) == LOG_ERR);
  666. CHECK(state_priority(EMERGENCY, 0) == LOG_CRIT);
  667. CHECK(state_priority(RECOVERY, 0) == LOG_INFO);
  668. CHECK(state_priority(NORMAL, 1) == LOG_CRIT);
  669. log_sampling_overrun(4.459, &fake_sample);
  670. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  671. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  672. CHECK(strstr(contents, "event=sampling_overrun late_seconds=4.459 state=CRITICAL free_pages=60000") != NULL);
  673. puts("PASS selective native syslog ident/facility/severity, exclusions, signal ordering, disable and failure isolation");
  674. critical_path_faults();
  675. floor_errno_fixture();
  676. diagnostic_fixtures();
  677. panic33_fixture();
  678. partial_cache_tests();
  679. fclose(log);
  680. printf("PASS %u runtime checks; all platform calls mocked, no signals sent\n", checks);
  681. return 0;
  682. }