Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

506 line
26 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 sample_t fake_sample;
  7. static process_t fixtures[32];
  8. static unsigned nfixtures, real_requests, validation_requests, walk_requests, checks;
  9. static int fail_validation, fail_sample, partial, invalid_pid;
  10. typedef struct { int priority; char message[1536]; } syslog_record_t;
  11. static syslog_record_t syslog_records[64];
  12. static unsigned syslog_count, syslog_open_count, syslog_close_count;
  13. static int syslog_facility, fail_syslog;
  14. static double syslog_delay_seconds;
  15. static char syslog_ident[64];
  16. #define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL runtime line %d: %s\n", __LINE__, #x); exit(1); } checks++; } while (0)
  17. double monotime(void) { return fake_now; }
  18. int secure_file(const char *p, int flags, unsigned mode)
  19. { (void)p; (void)flags; (void)mode; errno = EPERM; return -1; }
  20. int secure_directory(const char *p) { (void)p; return -1; }
  21. int platform_init(void) { return 0; }
  22. void platform_fini(void) { }
  23. int platform_syslog_open(const char *ident, int facility)
  24. {
  25. syslog_open_count++; syslog_facility = facility;
  26. snprintf(syslog_ident, sizeof (syslog_ident), "%s", ident);
  27. return fail_syslog ? -1 : 0;
  28. }
  29. int platform_syslog_write(int priority, const char *message)
  30. {
  31. if (fail_syslog) { errno = EIO; return -1; }
  32. fake_now += syslog_delay_seconds;
  33. CHECK(syslog_count < sizeof (syslog_records) / sizeof (syslog_records[0]));
  34. syslog_records[syslog_count].priority = priority;
  35. snprintf(syslog_records[syslog_count].message,
  36. sizeof (syslog_records[syslog_count].message), "%s", message);
  37. syslog_count++;
  38. return 0;
  39. }
  40. void platform_syslog_close(void) { syslog_close_count++; }
  41. int platform_sample(sample_t *s)
  42. { *s = fake_sample; s->at = fake_now; return fail_sample ? -1 : 0; }
  43. int platform_walk(int (*fn)(const process_t *, void *), void *arg, unsigned budget)
  44. {
  45. unsigned j; (void)budget;
  46. walk_requests++;
  47. for (j = 0; j < nfixtures; j++) if (fn(&fixtures[j], arg)) return -1;
  48. return partial ? -1 : 0;
  49. }
  50. int platform_signal(const config_t *c, const process_t *p, enum action action,
  51. int real, uint64_t ceiling, double *attempt_mono, double *return_mono)
  52. {
  53. unsigned j; int found = 0;
  54. CHECK(action == TERM || action == KILL);
  55. CHECK(!protected_process(c, p));
  56. for (j = 0; j < nfixtures; j++)
  57. if (fixtures[j].pid == p->pid && same_process(&fixtures[j], p)) { found = 1; break; }
  58. if (real) {
  59. CHECK(c->armed && fake_sample.free < ceiling); real_requests++;
  60. if (attempt_mono) *attempt_mono = fake_now;
  61. if (return_mono) *return_mono = fake_now;
  62. }
  63. else validation_requests++;
  64. if (fail_validation || p->pid == invalid_pid || !found) { errno = ESTALE; return -1; }
  65. return 0; /* This test backend cannot send any signal. */
  66. }
  67. static void reset(void)
  68. {
  69. defaults(&cfg); cfg.log_mib = 100;
  70. cfg.nzones = 2;
  71. strcpy(cfg.zones[0].uuid, "11111111-1111-4111-8111-111111111111");
  72. strcpy(cfg.zones[0].alias, "web-a"); cfg.zones[0].policy = ALLOW;
  73. strcpy(cfg.zones[1].uuid, "22222222-2222-4222-8222-222222222222");
  74. strcpy(cfg.zones[1].alias, "web-b"); cfg.zones[1].policy = ALLOW;
  75. memset(processes, 0, sizeof (processes)); memset(zone_stats, 0, sizeof (zone_stats));
  76. memset(candidate_cache, 0, sizeof (candidate_cache));
  77. memset(published_cache, 0, sizeof (published_cache));
  78. memset(metrics, 0, sizeof (metrics));
  79. memset(&controller, 0, sizeof (controller)); memset(fixtures, 0, sizeof (fixtures));
  80. engine_init(&engine); engine.state = CRITICAL; engine.danger = 1;
  81. stopping = 0; started = 0; log_ok = 1; log_bytes = 0;
  82. async_logging = scanner_async = 0;
  83. event_head = event_tail = event_count = dropped_events = 0;
  84. event_sequence = 0; scan_requested = scan_snapshot_requested = 0;
  85. syslog_ready = syslog_init_failed = 0;
  86. syslog_count = syslog_open_count = syslog_close_count = 0;
  87. syslog_facility = -1; fail_syslog = 0; syslog_ident[0] = 0;
  88. syslog_delay_seconds = 0;
  89. scan_generation = 0; scan_complete = published_scan_complete = 0;
  90. scanner_latest_complete_scan_at = latest_complete_scan_at = 0;
  91. published_latest_complete_scan_at = 0;
  92. fake_now = 100; partial = fail_sample = fail_validation = invalid_pid = 0;
  93. real_requests = validation_requests = walk_requests = 0;
  94. memset(&fake_sample, 0, sizeof (fake_sample)); fake_sample.at = fake_now;
  95. fake_sample.free = 60000; fake_sample.lots = 130875;
  96. fake_sample.des = 65437; fake_sample.min = 49077;
  97. fixtures[0].pid = 123; fixtures[0].ppid = 100; fixtures[0].zid = 9;
  98. fixtures[0].initpid = 99; fixtures[0].nlwp = 1; fixtures[0].start_sec = 1000;
  99. fixtures[0].zone_generation = 20; fixtures[0].rss_kib = 100000;
  100. strcpy(fixtures[0].uuid, cfg.zones[0].uuid); strcpy(fixtures[0].name, "php-fpm");
  101. nfixtures = 1;
  102. scan_processes(0);
  103. }
  104. static void populate_many_candidates(unsigned count)
  105. {
  106. unsigned i;
  107. reset();
  108. CHECK(count <= sizeof (fixtures) / sizeof (fixtures[0]));
  109. for (i = 0; i < count; i++) {
  110. fixtures[i] = fixtures[0]; fixtures[i].pid = 1000 + (int)i;
  111. fixtures[i].start_sec += i; fixtures[i].start_nsec += i;
  112. fixtures[i].rss_kib = 2000000 - 1000 * i;
  113. }
  114. nfixtures = count; fake_now = 101; scan_processes(0);
  115. for (i = 0; i < count; i++) CHECK(candidate_cache[i].valid);
  116. }
  117. static int queued_contains(const char *text)
  118. {
  119. unsigned i;
  120. for (i = 0; i < event_count; i++) {
  121. unsigned slot = (event_head + i) % EVENT_QUEUE_SIZE;
  122. if (strstr(event_queue[slot].body, text)) return 1;
  123. }
  124. return 0;
  125. }
  126. static void critical_path_faults(void)
  127. {
  128. struct timespec before, after; double elapsed_ms; unsigned walks;
  129. struct stat st_before, st_after;
  130. {
  131. FILE *fixture; char line[160], event[64];
  132. unsigned seconds, rows = 0, decision_at = 0, sent_at = 0;
  133. uint64_t free_pages, nscan;
  134. fixture = fopen("tests/fixtures/panic32_daemon_stall_2026_09_22.csv", "r");
  135. CHECK(fixture != NULL); CHECK(fgets(line, sizeof (line), fixture) != NULL);
  136. while (fscanf(fixture, "%u,%" SCNu64 ",%" SCNu64 ",%63s",
  137. &seconds, &free_pages, &nscan, event) == 4) {
  138. rows++;
  139. if (!strcmp(event, "kill_decision")) decision_at = seconds;
  140. if (!strcmp(event, "kill_sent_after_145s")) sent_at = seconds;
  141. }
  142. fclose(fixture);
  143. CHECK(rows == 7 && sent_at - decision_at == 145);
  144. }
  145. {
  146. queued_event_t event;
  147. reset(); async_logging = 1; syslog_ready = 1;
  148. syslog_delay_seconds = 0.200;
  149. memset(&event, 0, sizeof (event));
  150. strcpy(event.body, "event=test_syslog_stall");
  151. event.wall_time = time(NULL); event.mono_time = fake_now;
  152. event.syslog_channel = 1; event.priority = LOG_WARNING; event.sequence = 1;
  153. CHECK(write_event_now(&event) == 0);
  154. CHECK(queued_contains("event=logging_stall"));
  155. CHECK(queued_contains("channel=syslog"));
  156. CHECK(queued_contains("syslog_latency_ms=200.000"));
  157. }
  158. populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
  159. engine.danger = 1; fake_sample.free = 40000; fake_now += 180;
  160. async_logging = 1; fail_syslog = 1;
  161. CHECK(fstat(logfd, &st_before) == 0); walks = walk_requests;
  162. clock_gettime(CLOCK_MONOTONIC, &before); act(&fake_sample);
  163. clock_gettime(CLOCK_MONOTONIC, &after);
  164. elapsed_ms = 1000.0 * (after.tv_sec - before.tv_sec) +
  165. (after.tv_nsec - before.tv_nsec) / 1000000.0;
  166. CHECK(real_requests == 1 && validation_requests == 1);
  167. CHECK(elapsed_ms < 100.0 && walk_requests == walks && syslog_count == 0);
  168. CHECK(fstat(logfd, &st_after) == 0 && st_after.st_size == st_before.st_size);
  169. CHECK(queued_contains("candidate_source=stale_cache_revalidated"));
  170. CHECK(queued_contains("dispatch_latency_ms="));
  171. CHECK(queued_contains("candidate_validate_latency_ms="));
  172. CHECK(queued_contains("kill_latency_ms="));
  173. CHECK(queued_contains("log_enqueue_latency_ms="));
  174. fake_now += cfg.emergency_term_s;
  175. clock_gettime(CLOCK_MONOTONIC, &before); act(&fake_sample);
  176. clock_gettime(CLOCK_MONOTONIC, &after);
  177. elapsed_ms = 1000.0 * (after.tv_sec - before.tv_sec) +
  178. (after.tv_nsec - before.tv_nsec) / 1000000.0;
  179. CHECK(real_requests == 2 && validation_requests == 2 && elapsed_ms < 100.0);
  180. CHECK(queued_contains("signal=SIGKILL"));
  181. puts("PASS panic32 fault: logger/syslog stopped 180s, stale cache revalidated, signal dispatch <100ms");
  182. populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
  183. engine.danger = 1; fake_sample.free = 40000; async_logging = scanner_async = 1;
  184. scan_requested = 1; walks = walk_requests;
  185. act(&fake_sample);
  186. CHECK(real_requests == 1 && walk_requests == walks);
  187. puts("PASS stalled/incomplete scanner cannot delay or trigger a rescue-path proc scan");
  188. populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
  189. engine.danger = 1; fake_sample.free = 40000; async_logging = 1;
  190. walks = walk_requests;
  191. while (controller.nvictims < cfg.max_emergency_victims) {
  192. act(&fake_sample);
  193. if (controller.has_pending) { fake_now += cfg.emergency_term_s; act(&fake_sample); }
  194. fake_now += 0.01;
  195. }
  196. CHECK(controller.nvictims == 8 && real_requests == 16 && walk_requests == walks);
  197. act(&fake_sample); CHECK(real_requests == 16);
  198. puts("PASS 16-entry stale rescue cache supplies all 8 EMERGENCY victims without scan");
  199. populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
  200. engine.danger = engine.floor = 1; fake_sample.free = 20000; async_logging = 1;
  201. walks = walk_requests;
  202. while (controller.nvictims < cfg.max_floor_victims) {
  203. act(&fake_sample); fake_now += 0.01;
  204. }
  205. CHECK(controller.nvictims == 12 && real_requests == 12 && walk_requests == walks);
  206. act(&fake_sample); CHECK(real_requests == 12);
  207. puts("PASS 16-entry stale rescue cache supplies all 12 FLOOR victims without scan");
  208. populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
  209. engine.danger = 1; fake_sample.free = 40000; async_logging = 1;
  210. CHECK(pthread_mutex_lock(&event_mutex) == 0);
  211. clock_gettime(CLOCK_MONOTONIC, &before); act(&fake_sample);
  212. clock_gettime(CLOCK_MONOTONIC, &after);
  213. CHECK(pthread_mutex_unlock(&event_mutex) == 0);
  214. elapsed_ms = 1000.0 * (after.tv_sec - before.tv_sec) +
  215. (after.tv_nsec - before.tv_nsec) / 1000000.0;
  216. CHECK(real_requests == 1 && elapsed_ms < 100.0 && dropped_events > 0);
  217. puts("PASS saturated/locked event queue drops telemetry without delaying the signal");
  218. }
  219. static void load_global_ranking_fixture(void)
  220. {
  221. FILE *f;
  222. char line[256], uuid[64], alias[64], name[32];
  223. int zid, pid;
  224. uint64_t generation, rss;
  225. reset();
  226. cfg.nzones = cfg.nnames = 0;
  227. strcpy(cfg.protected_names[cfg.nnames++], "mysqld");
  228. strcpy(cfg.protected_names[cfg.nnames++], "zone-overhead");
  229. memset(processes, 0, sizeof (processes));
  230. memset(zone_stats, 0, sizeof (zone_stats));
  231. memset(candidate_cache, 0, sizeof (candidate_cache));
  232. memset(&controller, 0, sizeof (controller));
  233. memset(fixtures, 0, sizeof (fixtures));
  234. scan_generation = 0; scan_complete = 0; latest_complete_scan_at = 0;
  235. nfixtures = 0; fake_now = 100;
  236. f = fopen("tests/fixtures/global_victim_ranking_smartos_2026_09.csv", "r");
  237. CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
  238. while (fscanf(f, " %63[^,],%63[^,],%d,%" SCNu64 ",%d,%31[^,],%" SCNu64,
  239. uuid, alias, &zid, &generation, &pid, name, &rss) == 7) {
  240. process_t *p;
  241. const zone_rule_t *rule = zone_rule(&cfg, uuid);
  242. CHECK(nfixtures < sizeof (fixtures) / sizeof (fixtures[0]));
  243. if (!rule) {
  244. zone_rule_t *z;
  245. CHECK(cfg.nzones < MAX_ZONES); z = &cfg.zones[cfg.nzones++];
  246. strcpy(z->uuid, uuid); strcpy(z->alias, alias); z->policy = ALLOW;
  247. }
  248. p = &fixtures[nfixtures++];
  249. p->pid = pid; p->ppid = 100; p->zid = zid; p->initpid = 9000 + zid;
  250. p->nlwp = 1; p->start_sec = 1000 + pid; p->start_nsec = pid;
  251. p->zone_generation = generation; p->rss_kib = rss;
  252. strcpy(p->uuid, uuid); strcpy(p->name, name);
  253. }
  254. fclose(f); CHECK(nfixtures == 16 && cfg.nzones == 5);
  255. scan_processes(1);
  256. }
  257. int main(void)
  258. {
  259. FILE *log = tmpfile(); char contents[65536]; size_t n;
  260. const process_t *chosen;
  261. unsigned second;
  262. CHECK(log != NULL); logfd = fileno(log);
  263. reset(); act(&fake_sample);
  264. CHECK(real_requests == 0 && validation_requests == 1 && controller.has_pending);
  265. fake_now += 5; scan_processes(0); act(&fake_sample);
  266. CHECK(real_requests == 0 && !controller.has_pending);
  267. rewind(log); n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  268. CHECK(strstr(contents, "WOULD_ACTION=SIGTERM") != NULL);
  269. CHECK(strstr(contents, "WOULD_ACTION=SIGKILL") != NULL);
  270. CHECK(strstr(contents, "ACTION_INTENT=") == NULL);
  271. CHECK(fseek(log, 0, SEEK_END) == 0);
  272. puts("PASS actual daemon dry-run path records WOULD_ACTION=SIGTERM/SIGKILL; zero real requests");
  273. reset();
  274. fake_sample.arc_ok = 1;
  275. fake_sample.swap_virtual_ok = fake_sample.swap_physical_ok = fake_sample.pageout_ok = 1;
  276. for (second = 0; second <= 60; second++) {
  277. fake_sample.at = 100 + second;
  278. fake_sample.arc = UINT64_C(6000000000) + 1000 * second;
  279. fake_sample.swap_alloc = 7000000 + 3 * second;
  280. fake_sample.swap_reserved = 480000 + 2 * second;
  281. fake_sample.swap_available = 6200000 - second;
  282. fake_sample.swap_physical_used_bytes = UINT64_C(4143648768) + 4096 * second;
  283. fake_sample.anon_pageout_pages = 1000 + 2 * second;
  284. record_pressure_metrics(&fake_sample);
  285. record_auxiliary_metrics(&fake_sample);
  286. }
  287. log_sample(&fake_sample);
  288. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  289. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  290. CHECK(strstr(contents, "swap_committed_ok=1 swap_committed_pages=7480300") != NULL);
  291. CHECK(strstr(contents, "swap_physical_used_ok=1 swap_physical_used_bytes=4143894528") != NULL);
  292. CHECK(strstr(contents, "anon_pageout_ok=1 anon_pageout_pages=1120") != NULL);
  293. CHECK(strstr(contents, "swap_alloc_pages=7000180 swap_reserved_pages=480120 swap_available_pages=6199940") != NULL);
  294. CHECK(strstr(contents, "metric=swap_committed_pages d5=+25@5.00s d15=+75@15.00s d60=+300@60.00s") != NULL);
  295. CHECK(strstr(contents, "metric=swap_physical_used_bytes d5=+20480@5.00s d15=+61440@15.00s d60=+245760@60.00s") != NULL);
  296. CHECK(strstr(contents, "metric=anon_pageout_pages d5=+10@5.00s d15=+30@15.00s d60=+120@60.00s") != NULL);
  297. CHECK(strstr(contents, "swap_used_pages") == NULL);
  298. CHECK(fseek(log, 0, SEEK_END) == 0);
  299. puts("PASS continuous sample contract and 5/15/60 deltas for commitment, physical swap and pageout");
  300. reset(); cfg.armed = 1; act(&fake_sample);
  301. CHECK(real_requests == 1);
  302. fake_now += 5; scan_processes(0); act(&fake_sample); CHECK(real_requests == 2);
  303. reset(); cfg.armed = 1; started = 80; fake_now = 101; scan_processes(0);
  304. CHECK(candidate_cache[0].valid && engine.danger && walk_requests == 2);
  305. act(&fake_sample); CHECK(real_requests == 0 && candidate_cache[0].valid);
  306. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  307. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  308. CHECK(strstr(contents, "event=action_blocked reason=armed_warmup") != NULL);
  309. CHECK(fseek(log, 0, SEEK_END) == 0);
  310. reset(); cfg.armed = 1; log_ok = 0; act(&fake_sample); CHECK(real_requests == 1);
  311. reset(); cfg.armed = 1; partial = 1; fake_now++; scan_processes(0);
  312. act(&fake_sample); CHECK(real_requests == 1 && !scan_complete);
  313. reset(); cfg.armed = 1; fail_sample = 1; act(&fake_sample); CHECK(real_requests == 0);
  314. reset(); cfg.armed = 1; fail_validation = 1; act(&fake_sample); CHECK(real_requests == 0);
  315. reset(); cfg.armed = 1; fake_sample.free = 150000; act(&fake_sample); CHECK(real_requests == 0);
  316. reset(); cfg.armed = 1; fake_now += 16; act(&fake_sample); CHECK(real_requests == 1);
  317. reset(); cfg.armed = 1; stopping = 1; act(&fake_sample); CHECK(real_requests == 0);
  318. reset(); fake_sample.free = 20000; act(&fake_sample);
  319. CHECK(!controller.has_pending && controller.nvictims == 1);
  320. puts("PASS armed-path mock requests, explicit warmup, stale cache, logging/sampling failures, stop and fresh floor");
  321. reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
  322. fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
  323. strcpy(fixtures[1].uuid, cfg.zones[1].uuid); nfixtures = 2;
  324. fake_now = 101; scan_processes(0); chosen = candidate(); CHECK(chosen && chosen->pid == 124);
  325. /* Small zone grows fast enough to outweigh a larger steady zone. */
  326. fixtures[0].rss_kib = 600000; fake_now = 161; scan_processes(0);
  327. chosen = candidate(); CHECK(chosen && chosen->pid == 123);
  328. cfg.zones[0].policy = PROTECT; fake_now = 162; scan_processes(0);
  329. chosen = candidate(); CHECK(chosen && chosen->pid == 124);
  330. fixtures[1].zid = 0; fake_now = 163; scan_processes(0); CHECK(candidate() == NULL);
  331. puts("PASS global score uses process RSS/growth; protected zones and global zone excluded");
  332. load_global_ranking_fixture();
  333. CHECK(candidate_cache[0].valid && candidate_cache[1].valid && candidate_cache[2].valid);
  334. CHECK(!strcmp(candidate_cache[0].p.name, "clamd"));
  335. CHECK(!strcmp(candidate_cache[1].p.name, "clamd"));
  336. CHECK(!strcmp(candidate_cache[2].p.name, "clamd"));
  337. CHECK(candidate_cache[0].p.rss_kib > 1300000 &&
  338. candidate_cache[2].p.rss_kib > 1300000);
  339. CHECK(strcmp(candidate_cache[0].p.uuid, candidate_cache[1].p.uuid));
  340. CHECK(strcmp(candidate_cache[1].p.uuid, candidate_cache[2].p.uuid));
  341. for (second = 0; second < CACHED_CANDIDATES; second++) {
  342. CHECK(strcmp(candidate_cache[second].p.name, "mysqld"));
  343. CHECK(strcmp(candidate_cache[second].p.name, "zone-overhead"));
  344. }
  345. for (second = 0; second < nfixtures; second++)
  346. if (!strcmp(fixtures[second].name, "mysqld"))
  347. CHECK(protected_process(&cfg, &fixtures[second]));
  348. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  349. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  350. {
  351. char *mysql = strstr(contents, "comm=mysqld");
  352. char *ineligible = mysql ? strstr(mysql, "eligible=0") : NULL;
  353. char *line_end = mysql ? strchr(mysql, '\n') : NULL;
  354. CHECK(mysql && ineligible && line_end && ineligible < line_end);
  355. }
  356. CHECK(fseek(log, 0, SEEK_END) == 0);
  357. fixtures[nfixtures] = fixtures[0]; fixtures[nfixtures].pid = 9001;
  358. fixtures[nfixtures].zid = 90; fixtures[nfixtures].zone_generation = 900;
  359. fixtures[nfixtures].rss_kib = UINT64_C(9000000);
  360. strcpy(fixtures[nfixtures].uuid, "66666666-6666-4666-8666-666666666666");
  361. strcpy(fixtures[nfixtures].name, "unmapped-hog"); nfixtures++;
  362. fixtures[nfixtures] = fixtures[0]; fixtures[nfixtures].pid = 9002;
  363. fixtures[nfixtures].zid = 0; fixtures[nfixtures].rss_kib = UINT64_C(11000000);
  364. strcpy(fixtures[nfixtures].name, "global-hog"); nfixtures++;
  365. fake_now = 101; scan_processes(0);
  366. for (second = 0; second < CACHED_CANDIDATES; second++)
  367. CHECK(candidate_cache[second].p.pid != 9001 && candidate_cache[second].p.pid != 9002);
  368. for (second = 0; second < nfixtures; second++)
  369. if (fixtures[second].pid == 1103) fixtures[second].rss_kib = 600000;
  370. fake_now = 106; scan_processes(0);
  371. CHECK(candidate_cache[0].p.pid == 1103);
  372. CHECK(candidate_cache[0].growth5_component > 0 &&
  373. candidate_cache[0].total_score > candidate_cache[1].total_score);
  374. {
  375. cached_candidate_t scored;
  376. zone_t zone;
  377. memset(&scored, 0, sizeof (scored)); memset(&zone, 0, sizeof (zone));
  378. scored.p.rss_kib = 100000; scored.d5.valid = scored.d15.valid = 1;
  379. scored.d60.valid = 1; scored.d5.value = 1000;
  380. scored.d15.value = 2000; scored.d60.value = 3000;
  381. scan_at = 60; history_add(&zone.history, 0, 1000000);
  382. history_add(&zone.history, 60, 1100000); zone.rss = 1100000;
  383. score_candidate(&scored, &zone);
  384. CHECK(scored.growth5_component == 4000);
  385. CHECK(scored.growth15_component == 4000);
  386. CHECK(scored.growth60_component == 3000);
  387. CHECK(scored.zone_component <= scored.p.rss_kib / 4);
  388. }
  389. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  390. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  391. CHECK(strstr(contents, "rss_component=") != NULL);
  392. CHECK(strstr(contents, "growth5_component=") != NULL);
  393. CHECK(strstr(contents, "growth15_component=") != NULL);
  394. CHECK(strstr(contents, "growth60_component=") != NULL);
  395. CHECK(strstr(contents, "zone_component=") != NULL);
  396. CHECK(strstr(contents, "total_score=") != NULL);
  397. CHECK(fseek(log, 0, SEEK_END) == 0);
  398. puts("PASS SmartOS fixture produces a global cross-zone top-3 and bounded zone context");
  399. reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
  400. fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
  401. strcpy(fixtures[1].uuid, cfg.zones[1].uuid);
  402. fixtures[2] = fixtures[0]; fixtures[2].pid = 125; fixtures[2].rss_kib = 800000;
  403. nfixtures = 3; fake_now = 101; scan_processes(0);
  404. CHECK(candidate_cache[0].valid && candidate_cache[1].valid && candidate_cache[2].valid);
  405. invalid_pid = candidate_cache[0].p.pid;
  406. partial = 1; fake_now = 102; scan_processes(0);
  407. CHECK(!scan_complete && candidate_cache[0].p.pid == invalid_pid);
  408. {
  409. unsigned walks_before = walk_requests;
  410. engine.state = EMERGENCY; engine.danger = 1; fake_sample.free = 40000;
  411. act(&fake_sample);
  412. CHECK(walk_requests == walks_before); /* O(1): no /proc walk in action path. */
  413. CHECK(controller.has_pending && controller.pending.pid != invalid_pid);
  414. CHECK(validation_requests >= 2 && real_requests == 0);
  415. }
  416. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  417. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  418. CHECK(strstr(contents, "candidate_source=fallback_candidate") != NULL);
  419. CHECK(strstr(contents, "scan_complete=0") != NULL);
  420. CHECK(fseek(log, 0, SEEK_END) == 0);
  421. puts("PASS incomplete scan preserves cache; emergency uses O(1) fallback without a new walk");
  422. reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
  423. fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
  424. strcpy(fixtures[1].uuid, cfg.zones[1].uuid); nfixtures = 2;
  425. fake_now = 101; scan_processes(0);
  426. for (second = 0; second < nfixtures; second++)
  427. if (fixtures[second].pid == candidate_cache[0].p.pid) fixtures[second].start_nsec++;
  428. engine.state = EMERGENCY; engine.danger = 1; fake_sample.free = 40000;
  429. act(&fake_sample);
  430. CHECK(controller.has_pending && !same_process(&controller.pending, &candidate_cache[0].p));
  431. puts("PASS PID/start-time reuse rejects rank 1 and selects cached fallback");
  432. reset();
  433. CHECK(platform_syslog_open("o6h-memory-guard", cfg.syslog_facility) == 0);
  434. syslog_ready = 1;
  435. CHECK(!strcmp(syslog_ident, "o6h-memory-guard") && syslog_facility == 5 &&
  436. syslog_open_count == 1);
  437. emit_important(LOG_INFO, "event=start version=%s mode=dry-run", VERSION);
  438. CHECK(syslog_count == 1 && syslog_records[0].priority == LOG_INFO &&
  439. strstr(syslog_records[0].message, "event=start version=1.0.7") != NULL);
  440. log_sample(&fake_sample); scan_processes(0);
  441. CHECK(syslog_count == 1); /* High-volume sample/delta/proc/cache events stay local. */
  442. {
  443. char message[512]; sample_t s = fake_sample;
  444. s.free = s.min;
  445. CHECK(!transition_message(message, sizeof (message), NORMAL, EMERGENCY, &s, 1));
  446. CHECK(strstr(message, "reason=threshold_jump") != NULL);
  447. CHECK(strstr(message, "skipped_states=PRE-PRESSURE,PRESSURE,CRITICAL") != NULL);
  448. syslog_event(state_priority(EMERGENCY, 0), "%s", message);
  449. CHECK(syslog_count == 2 && syslog_records[1].priority == LOG_CRIT);
  450. }
  451. cfg.armed = 0; act(&fake_sample);
  452. CHECK(syslog_count == 2 && real_requests == 0);
  453. reset(); cfg.armed = 1; started = 80; fake_now = 100;
  454. CHECK(platform_syslog_open("o6h-memory-guard", cfg.syslog_facility) == 0);
  455. syslog_ready = 1; act(&fake_sample);
  456. CHECK(real_requests == 0 && syslog_count == 1 &&
  457. syslog_records[0].priority == LOG_WARNING &&
  458. strstr(syslog_records[0].message, "event=action_blocked reason=armed_warmup") != NULL);
  459. reset(); cfg.armed = 1;
  460. CHECK(platform_syslog_open("o6h-memory-guard", cfg.syslog_facility) == 0);
  461. syslog_ready = 1; act(&fake_sample);
  462. CHECK(real_requests == 1 && syslog_count == 2);
  463. CHECK(syslog_records[0].priority == LOG_ERR &&
  464. strstr(syslog_records[0].message, "event=action_decision state=CRITICAL") != NULL &&
  465. strstr(syslog_records[0].message, "result=sent") != NULL);
  466. CHECK(syslog_records[1].priority == LOG_ERR &&
  467. strstr(syslog_records[1].message, "event=signal_sent state=CRITICAL") != NULL &&
  468. strstr(syslog_records[1].message, "signal=SIGTERM") != NULL &&
  469. strstr(syslog_records[1].message, "UUID=") != NULL &&
  470. strstr(syslog_records[1].message, "alias=web-a") != NULL &&
  471. strstr(syslog_records[1].message, "pid=123") != NULL &&
  472. strstr(syslog_records[1].message, "comm=php-fpm") != NULL &&
  473. strstr(syslog_records[1].message, "rss_kib=100000") != NULL &&
  474. strstr(syslog_records[1].message, "free_pages=60000") != NULL);
  475. reset(); cfg.armed = 1; fail_syslog = 1; syslog_ready = 1;
  476. act(&fake_sample); CHECK(real_requests == 1 && controller.has_pending);
  477. reset(); cfg.syslog_enabled = 0; syslog_ready = 0;
  478. emit_important(LOG_INFO, "event=start disabled_test=yes"); CHECK(syslog_count == 0);
  479. CHECK(state_priority(PRE_PRESSURE, 0) == LOG_NOTICE);
  480. CHECK(state_priority(PRESSURE, 0) == LOG_WARNING);
  481. CHECK(state_priority(CRITICAL, 0) == LOG_ERR);
  482. CHECK(state_priority(EMERGENCY, 0) == LOG_CRIT);
  483. CHECK(state_priority(RECOVERY, 0) == LOG_INFO);
  484. CHECK(state_priority(NORMAL, 1) == LOG_CRIT);
  485. log_sampling_overrun(4.459, &fake_sample);
  486. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  487. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  488. CHECK(strstr(contents, "event=sampling_overrun late_seconds=4.459 state=CRITICAL free_pages=60000") != NULL);
  489. puts("PASS selective native syslog ident/facility/severity, exclusions, signal ordering, disable and failure isolation");
  490. critical_path_faults();
  491. fclose(log);
  492. printf("PASS %u runtime checks; all platform calls mocked, no signals sent\n", checks);
  493. return 0;
  494. }