|
- /* Exercise the actual daemon selection/action path. Native backend NOT linked. */
- #define main guard_program_main
- #include "../src/main.c"
- #undef main
-
- static double fake_now = 100;
- static double refresh_age, dispatch_delay;
- static sample_t fake_sample;
- static process_t fixtures[32];
- static unsigned nfixtures, real_requests, validation_requests, walk_requests, checks;
- static int fail_validation, fail_sample, partial, invalid_pid;
- typedef struct { int priority; char message[1536]; } syslog_record_t;
- static syslog_record_t syslog_records[64];
- static unsigned syslog_count, syslog_open_count, syslog_close_count;
- static int syslog_facility, fail_syslog;
- static double syslog_delay_seconds;
- static char syslog_ident[64];
- #define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL runtime line %d: %s\n", __LINE__, #x); exit(1); } checks++; } while (0)
-
- double monotime(void) { return fake_now; }
- int secure_file(const char *p, int flags, unsigned mode)
- { (void)p; (void)flags; (void)mode; errno = EPERM; return -1; }
- int secure_directory(const char *p) { (void)p; return -1; }
- int platform_init(void) { return 0; }
- void platform_fini(void) { }
- int platform_syslog_open(const char *ident, int facility)
- {
- syslog_open_count++; syslog_facility = facility;
- snprintf(syslog_ident, sizeof (syslog_ident), "%s", ident);
- return fail_syslog ? -1 : 0;
- }
- int platform_syslog_write(int priority, const char *message)
- {
- if (fail_syslog) { errno = EIO; return -1; }
- fake_now += syslog_delay_seconds;
- CHECK(syslog_count < sizeof (syslog_records) / sizeof (syslog_records[0]));
- syslog_records[syslog_count].priority = priority;
- snprintf(syslog_records[syslog_count].message,
- sizeof (syslog_records[syslog_count].message), "%s", message);
- syslog_count++;
- return 0;
- }
- void platform_syslog_close(void) { syslog_close_count++; }
- int platform_sample(sample_t *s)
- { *s = fake_sample; s->at = fake_now - refresh_age;
- if (fail_sample) { memset(s, 0, sizeof (*s)); errno = EIO; return -1; }
- return 0; }
- int platform_walk(int (*fn)(const process_t *, void *), void *arg, unsigned budget)
- {
- unsigned j; (void)budget;
- walk_requests++;
- for (j = 0; j < nfixtures; j++) if (fn(&fixtures[j], arg)) return -1;
- return partial ? -1 : 0;
- }
- static int mock_signal_errno, mock_validation_errno, mock_timeout_pid;
- static int mock_ticket_pid;
- int validation_start(void) { return 0; }
- int validation_handle(int ticket) { return ticket; }
- void validation_release(int ticket) { (void)ticket; }
- int validation_acquire(const config_t *c, const process_t *p, int real,
- uint64_t ceiling, unsigned budget_ms, int *ticket, validation_t *timing)
- {
- unsigned j; int found = 0;
- (void)real; (void)ceiling;
- memset(timing, 0, sizeof (*timing));
- CHECK(!protected_process(c, p));
- for (j = 0; j < nfixtures; j++)
- if (fixtures[j].pid == p->pid && same_process(&fixtures[j], p)) { found = 1; break; }
- validation_requests++;
- if (p->pid == mock_timeout_pid) {
- fake_now += budget_ms / 1000.0; timing->total_ms = budget_ms;
- errno = ETIMEDOUT; return -1;
- }
- if (fail_validation || p->pid == invalid_pid || !found) { errno = ESTALE; return -1; }
- *ticket = p->pid; mock_ticket_pid = p->pid; errno = mock_validation_errno;
- return 0; /* This test backend cannot send any signal. */
- }
- void platform_dispatch(int handle, enum action action, signal_result_t *out)
- {
- CHECK(handle == mock_ticket_pid && (action == TERM || action == KILL));
- real_requests++;
- memset(out, 0, sizeof (*out)); out->attempted = 1;
- out->attempt = fake_now; fake_now += dispatch_delay; out->returned = fake_now;
- out->write_ms = out->total_ms = 1000 * dispatch_delay;
- out->rc = mock_signal_errno ? -1 : (long)(2 * sizeof (long));
- out->error = mock_signal_errno;
- }
- static void reset(void)
- {
- defaults(&cfg); cfg.log_mib = 100;
- cfg.nzones = 2;
- strcpy(cfg.zones[0].uuid, "11111111-1111-4111-8111-111111111111");
- strcpy(cfg.zones[0].alias, "web-a"); cfg.zones[0].policy = ALLOW;
- strcpy(cfg.zones[1].uuid, "22222222-2222-4222-8222-222222222222");
- strcpy(cfg.zones[1].alias, "web-b"); cfg.zones[1].policy = ALLOW;
- memset(processes, 0, sizeof (processes)); memset(zone_stats, 0, sizeof (zone_stats));
- memset(candidate_cache, 0, sizeof (candidate_cache));
- memset(published_cache, 0, sizeof (published_cache));
- memset(rejected_identity, 0, sizeof (rejected_identity));
- rejected_identity_next = rejected_identity_count = 0;
- memset(metrics, 0, sizeof (metrics));
- memset(&controller, 0, sizeof (controller)); memset(fixtures, 0, sizeof (fixtures));
- engine_init(&engine); engine.state = CRITICAL; engine.danger = 1;
- stopping = 0; started = 0; log_ok = 1; log_bytes = 0;
- async_logging = scanner_async = 0;
- event_head = event_tail = event_count = dropped_events = 0;
- event_sequence = 0; scan_requested = scan_snapshot_requested = 0;
- syslog_ready = syslog_init_failed = 0;
- syslog_count = syslog_open_count = syslog_close_count = 0;
- syslog_facility = -1; fail_syslog = 0; syslog_ident[0] = 0;
- syslog_delay_seconds = 0;
- refresh_age = dispatch_delay = 0;
- mock_signal_errno = mock_validation_errno = mock_timeout_pid = 0;
- scan_generation = 0; scan_complete = published_scan_complete = 0;
- scanner_latest_complete_scan_at = latest_complete_scan_at = 0;
- published_latest_complete_scan_at = 0;
- fake_now = 100; partial = fail_sample = fail_validation = invalid_pid = 0;
- real_requests = validation_requests = walk_requests = 0;
- memset(&fake_sample, 0, sizeof (fake_sample)); fake_sample.at = fake_now;
- fake_sample.free = 60000; fake_sample.lots = 130875;
- fake_sample.des = 65437; fake_sample.min = 49077;
- fixtures[0].pid = 123; fixtures[0].ppid = 100; fixtures[0].zid = 9;
- fixtures[0].initpid = 99; fixtures[0].nlwp = 1; fixtures[0].start_sec = 1000;
- fixtures[0].zone_generation = 20; fixtures[0].rss_kib = 100000;
- strcpy(fixtures[0].uuid, cfg.zones[0].uuid); strcpy(fixtures[0].name, "php-fpm");
- nfixtures = 1;
- scan_processes(0);
- }
-
- static void populate_many_candidates(unsigned count)
- {
- unsigned i;
- reset();
- CHECK(count <= sizeof (fixtures) / sizeof (fixtures[0]));
- for (i = 0; i < count; i++) {
- fixtures[i] = fixtures[0]; fixtures[i].pid = 1000 + (int)i;
- fixtures[i].start_sec += i; fixtures[i].start_nsec += i;
- fixtures[i].rss_kib = 2000000 - 1000 * i;
- }
- nfixtures = count; fake_now = 101; scan_processes(0);
- for (i = 0; i < count; i++) CHECK(candidate_cache[i].valid);
- }
-
- static int queued_contains(const char *text)
- {
- unsigned i;
- for (i = 0; i < event_count; i++) {
- unsigned slot = (event_head + i) % EVENT_QUEUE_SIZE;
- if (strstr(event_queue[slot].body, text)) return 1;
- }
- return 0;
- }
-
- static void panic33_fixture(void)
- {
- FILE *f;
- char line[256];
- unsigned seconds, incomplete, expected, j, rows = 0;
- uint64_t free_pages;
- delta_t d = {1, 5, 100};
- int removed = 0;
- populate_many_candidates(16);
- cfg.armed = 1; async_logging = 1;
- engine_init(&engine);
- f = fopen("tests/fixtures/panic33_persistent_floor_budget_exhaustion_2026_10_08.csv", "r");
- CHECK(f != NULL);
- while (fgets(line, sizeof (line), f)) {
- if (line[0] == '#') continue;
- CHECK(sscanf(line, "%" SCNu64 ",%u,%u,%u", &free_pages, &seconds, &incomplete, &expected) == 4);
- for (j = 0; j < seconds; j++) {
- fake_now++; fake_sample.at = fake_now; fake_sample.free = free_pages;
- fake_sample.scan = 1000000; fake_sample.pageout_ok = 1;
- if (incomplete && !removed) {
- /* Cached PID disappeared; its replacement is smaller than survivors. */
- fixtures[8].pid = 2000; fixtures[8].start_sec++;
- fixtures[8].rss_kib = 100000; removed = 1;
- }
- engine_step(&engine, &cfg, &fake_sample, d, d, d);
- refresh_candidate_cache();
- act(&fake_sample);
- partial = (int)incomplete;
- scan_processes(0);
- }
- CHECK(controller.nvictims == expected);
- rows++;
- }
- fclose(f);
- CHECK(rows == 7 && !scan_complete);
- CHECK(queued_contains("reason=emergency_victim_budget_exhausted"));
- CHECK(queued_contains("event=budget_override reason=persistent_floor"));
- CHECK(queued_contains("event=victim_budget tier=hard_floor used=16 limit=16 remaining=0"));
- CHECK(was_victim(&controller, &fixtures[8]));
- CHECK(real_requests > 16); /* TERM escalation plus direct FLOOR KILL. */
- fake_sample.free = 6513; fake_sample.at = ++fake_now;
- act(&fake_sample);
- CHECK(queued_contains("reason=hard_floor_victim_budget_exhausted"));
- CHECK(controller.nvictims == 16);
- puts("PASS panic33: partial recoveries, budgets, dead PID replacement, incomplete scan, hard ceiling16");
- }
-
- static void partial_cache_tests(void)
- {
- process_t replacement, survivor;
- unsigned i;
- int found = 0;
- populate_many_candidates(16);
- survivor = candidate_cache[1].p;
- invalidate_candidate(&candidate_cache[0].p);
- scanner_async = 1;
- replacement = fixtures[0]; replacement.pid = 9000; replacement.start_sec++;
- replacement.rss_kib = 70000;
- scan_at = ++fake_now; scan_generation++;
- /* Callback publishes before platform_walk returns (or blocks). */
- CHECK(visit(&replacement, NULL) == 0);
- refresh_candidate_cache();
- for (i = 0; i < CACHED_CANDIDATES; i++)
- if (candidate_cache[i].valid && same_process(&candidate_cache[i].p, &replacement)) found = 1;
- CHECK(found && !scan_complete);
- CHECK(same_process(&candidate_cache[0].p, &survivor));
- CHECK(pthread_mutex_lock(&cache_mutex) == 0);
- refresh_candidate_cache(); /* trylock only: must return while scanner holds lock. */
- CHECK(pthread_mutex_unlock(&cache_mutex) == 0);
- replacement.pid = 9001; replacement.rss_kib = 65536;
- CHECK(visit(&replacement, NULL) == 0);
- refresh_candidate_cache();
- CHECK(same_process(&candidate_cache[0].p, &survivor));
- CHECK(candidate_cache[15].p.pid == 9000);
- scanner_async = 0;
- puts("PASS incremental pre-return publication, survivor ordering, bounded merge and nonblocking handoff");
- }
-
- static void critical_path_faults(void)
- {
- struct timespec before, after; double elapsed_ms; unsigned walks;
- struct stat st_before, st_after;
- {
- FILE *fixture; char line[160], event[64];
- unsigned seconds, rows = 0, decision_at = 0, sent_at = 0;
- uint64_t free_pages, nscan;
- fixture = fopen("tests/fixtures/panic32_daemon_stall_2026_09_22.csv", "r");
- CHECK(fixture != NULL); CHECK(fgets(line, sizeof (line), fixture) != NULL);
- while (fscanf(fixture, "%u,%" SCNu64 ",%" SCNu64 ",%63s",
- &seconds, &free_pages, &nscan, event) == 4) {
- rows++;
- if (!strcmp(event, "kill_decision")) decision_at = seconds;
- if (!strcmp(event, "kill_sent_after_145s")) sent_at = seconds;
- }
- fclose(fixture);
- CHECK(rows == 7 && sent_at - decision_at == 145);
- }
- {
- queued_event_t event;
- reset(); async_logging = 1; syslog_ready = 1;
- syslog_delay_seconds = 0.200;
- memset(&event, 0, sizeof (event));
- strcpy(event.body, "event=test_syslog_stall");
- event.wall_time = time(NULL); event.mono_time = fake_now;
- event.syslog_channel = 1; event.priority = LOG_WARNING; event.sequence = 1;
- CHECK(write_event_now(&event) == 0);
- CHECK(queued_contains("event=logging_stall"));
- CHECK(queued_contains("channel=syslog"));
- CHECK(queued_contains("syslog_latency_ms=200.000"));
- }
- populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
- engine.danger = 1; fake_sample.free = 40000; fake_now += 180;
- async_logging = 1; fail_syslog = 1;
- CHECK(fstat(logfd, &st_before) == 0); walks = walk_requests;
- clock_gettime(CLOCK_MONOTONIC, &before); act(&fake_sample);
- clock_gettime(CLOCK_MONOTONIC, &after);
- elapsed_ms = 1000.0 * (after.tv_sec - before.tv_sec) +
- (after.tv_nsec - before.tv_nsec) / 1000000.0;
- CHECK(real_requests == 1 && validation_requests == 1);
- CHECK(elapsed_ms < 100.0 && walk_requests == walks && syslog_count == 0);
- CHECK(fstat(logfd, &st_after) == 0 && st_after.st_size == st_before.st_size);
- CHECK(queued_contains("candidate_source=stale_cache_revalidated"));
- CHECK(queued_contains("dispatch_latency_ms="));
- CHECK(queued_contains("candidate_validate_latency_ms="));
- CHECK(queued_contains("kill_latency_ms="));
- CHECK(queued_contains("log_enqueue_latency_ms="));
- fake_now += cfg.emergency_term_s;
- clock_gettime(CLOCK_MONOTONIC, &before); act(&fake_sample);
- clock_gettime(CLOCK_MONOTONIC, &after);
- elapsed_ms = 1000.0 * (after.tv_sec - before.tv_sec) +
- (after.tv_nsec - before.tv_nsec) / 1000000.0;
- CHECK(real_requests == 2 && validation_requests == 2 && elapsed_ms < 100.0);
- CHECK(queued_contains("signal=SIGKILL"));
- puts("PASS panic32 fault: logger/syslog stopped 180s, stale cache revalidated, signal dispatch <100ms");
-
- populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
- engine.danger = 1; fake_sample.free = 40000; async_logging = scanner_async = 1;
- scan_requested = 1; walks = walk_requests;
- act(&fake_sample);
- CHECK(real_requests == 1 && walk_requests == walks);
- puts("PASS stalled/incomplete scanner cannot delay or trigger a rescue-path proc scan");
-
- populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
- engine.danger = 1; fake_sample.free = 40000; async_logging = 1;
- walks = walk_requests;
- while (controller.nvictims < cfg.max_emergency_victims) {
- act(&fake_sample);
- if (controller.has_pending) { fake_now += cfg.emergency_term_s; act(&fake_sample); }
- fake_now += 0.01;
- }
- CHECK(controller.nvictims == 8 && real_requests == 16 && walk_requests == walks);
- act(&fake_sample); CHECK(real_requests == 16);
- puts("PASS 16-entry stale rescue cache supplies all 8 EMERGENCY victims without scan");
-
- populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
- engine.danger = engine.floor = 1; fake_sample.free = 20000; async_logging = 1;
- walks = walk_requests;
- while (controller.nvictims < cfg.max_floor_victims) {
- act(&fake_sample); fake_now += 0.01;
- }
- CHECK(controller.nvictims == 12 && real_requests == 12 && walk_requests == walks);
- act(&fake_sample); CHECK(real_requests == 12);
- puts("PASS 16-entry stale rescue cache supplies all 12 FLOOR victims without scan");
-
- populate_many_candidates(16); cfg.armed = 1; engine.state = EMERGENCY;
- engine.danger = 1; fake_sample.free = 40000; async_logging = 1;
- CHECK(pthread_mutex_lock(&event_mutex) == 0);
- clock_gettime(CLOCK_MONOTONIC, &before); act(&fake_sample);
- clock_gettime(CLOCK_MONOTONIC, &after);
- CHECK(pthread_mutex_unlock(&event_mutex) == 0);
- elapsed_ms = 1000.0 * (after.tv_sec - before.tv_sec) +
- (after.tv_nsec - before.tv_nsec) / 1000000.0;
- CHECK(real_requests == 1 && elapsed_ms < 100.0 && dropped_events > 0);
- puts("PASS saturated/locked event queue drops telemetry without delaying the signal");
- }
-
- static void floor_errno_fixture(void)
- {
- FILE *f; char line[160], event[80];
- unsigned seconds, rows = 0, i, j;
- uint64_t free_pages, scans;
- delta_t zero = {0, 0, 0};
- const int prior[] = {EAGAIN, ENOMEM};
- const int errors[] = {0, ESRCH, EPERM, EINVAL, EAGAIN};
- populate_many_candidates(16); cfg.armed = 1; async_logging = 1;
- engine_init(&engine);
- f = fopen("tests/fixtures/floor_free_pages_486_2026_09_23.csv", "r");
- CHECK(f != NULL && fgets(line, sizeof (line), f) != NULL);
- while (fscanf(f, "%u,%" SCNu64 ",%" SCNu64 ",%79s", &seconds,
- &free_pages, &scans, event) == 4) {
- fake_now = 200 + seconds; fake_sample.at = fake_now;
- fake_sample.free = free_pages; fake_sample.scan = scans;
- engine_step(&engine, &cfg, &fake_sample, zero, zero, zero);
- if (seconds == 1) {
- CHECK(engine.state == EMERGENCY && engine.floor);
- mock_timeout_pid = candidate_cache[0].p.pid;
- act(&fake_sample);
- CHECK(real_requests == 1 && mock_ticket_pid == candidate_cache[1].p.pid);
- CHECK(queued_contains("reason=validation_timeout rank=1 latency_ms=50.000"));
- CHECK(queued_contains("rank=2"));
- CHECK(fake_now - fake_sample.at < 0.100);
- }
- rows++;
- }
- fclose(f); CHECK(rows == 3 && engine.state == RECOVERY);
- for (i = 0; i < 2; i++) for (j = 0; j < 5; j++) {
- char expected[120];
- reset(); cfg.armed = 1; async_logging = 1;
- mock_validation_errno = prior[i]; mock_signal_errno = errors[j];
- act(&fake_sample);
- snprintf(expected, sizeof (expected), "result=%s errno=%d syscall=write operation=PCKILL attempted=1 rc=%ld errno_name=%s",
- errors[j] ? "failed" : "sent", errors[j],
- errors[j] ? -1L : (long)(2 * sizeof (long)), signal_errno_name(errors[j]));
- CHECK(queued_contains(expected));
- CHECK(queued_contains("event=signal_sent") == (errors[j] == 0));
- CHECK(queued_contains("validate_start_time_ms="));
- }
- puts("PASS floor486 fixture, 50ms timeout fallback rank1->2, syscall errno attribution and validation substep logs");
- }
-
- static void diagnostic_fixtures(void)
- {
- FILE *f; char line[128], stamp[32], expected[128];
- unsigned signals, failure, rows = 0; uint64_t free_pages; double ms;
- reset(); cfg.armed = 1; async_logging = 1; engine.state = EMERGENCY;
- f = fopen("tests/fixtures/refresh_recovery_and_failure.csv", "r");
- CHECK(f && fgets(line, sizeof (line), f));
- while (fscanf(f, "%" SCNu64 ",%u,%u", &free_pages, &failure, &signals) == 3) {
- fake_sample.free = free_pages; fail_sample = (int)failure;
- act(&fake_sample); CHECK(real_requests == signals); rows++;
- }
- fclose(f); CHECK(rows == 3);
- CHECK(queued_contains("reason=free_above_desfree state=EMERGENCY free_pages=70000 desfree=65437 minfree=49077 refresh_ok=1"));
- CHECK(queued_contains("reason=refresh_failed state=EMERGENCY free_pages=20000 desfree=65437 minfree=49077 refresh_ok=0"));
- CHECK(queued_contains("refresh_errno=5 refresh_error=EIO"));
- reset(); cfg.armed = 1; async_logging = 1; refresh_age = 2.1;
- act(&fake_sample); CHECK(real_requests == 0);
- CHECK(queued_contains("reason=refresh_failed")); CHECK(queued_contains("refresh_error=stale_sample"));
- reset(); cfg.armed = 1; async_logging = 1; fake_sample.free = fake_sample.des;
- act(&fake_sample); CHECK(real_requests == 0 && queued_contains("reason=free_above_desfree"));
- f = fopen("tests/fixtures/pckill_latency_2026_09_28.csv", "r");
- CHECK(f && fgets(line, sizeof (line), f)); rows = 0;
- while (fscanf(f, "%31[^,],%lf\n", stamp, &ms) == 2) {
- reset(); cfg.armed = 1; async_logging = 1; fake_sample.free = 486;
- engine.state = EMERGENCY; dispatch_delay = ms / 1000;
- act(&fake_sample);
- CHECK(real_requests == 1 && queued_contains("event=signal_sent"));
- CHECK(queued_contains("event=signal_dispatch_slow signal=SIGKILL"));
- snprintf(expected, sizeof (expected), "pckill_write_latency_ms=%.3f", ms);
- CHECK(queued_contains(expected)); rows++;
- }
- fclose(f); CHECK(rows == 5);
- reset(); cfg.armed = 1; async_logging = 1; dispatch_delay = .000010;
- act(&fake_sample); CHECK(queued_contains("pckill_write_latency_ms=0.010"));
- CHECK(!queued_contains("event=signal_dispatch_slow"));
- reset(); cfg.armed = 1; async_logging = 1; dispatch_delay = .100;
- act(&fake_sample); CHECK(!queued_contains("event=signal_dispatch_slow"));
- reset(); cfg.armed = 1; async_logging = 1; dispatch_delay = .101;
- act(&fake_sample); CHECK(queued_contains("event=signal_dispatch_slow signal=SIGTERM"));
- puts("PASS recovery/failure/stale refresh reasons, desfree equality, no second victim, PCKILL subphases and slow-event threshold");
- }
-
- static void load_global_ranking_fixture(void)
- {
- FILE *f;
- char line[256], uuid[64], alias[64], name[32];
- int zid, pid;
- uint64_t generation, rss;
- reset();
- cfg.nzones = cfg.nnames = 0;
- strcpy(cfg.protected_names[cfg.nnames++], "mysqld");
- strcpy(cfg.protected_names[cfg.nnames++], "zone-overhead");
- memset(processes, 0, sizeof (processes));
- memset(zone_stats, 0, sizeof (zone_stats));
- memset(candidate_cache, 0, sizeof (candidate_cache));
- memset(&controller, 0, sizeof (controller));
- memset(fixtures, 0, sizeof (fixtures));
- scan_generation = 0; scan_complete = 0; latest_complete_scan_at = 0;
- nfixtures = 0; fake_now = 100;
- f = fopen("tests/fixtures/global_victim_ranking_smartos_2026_09.csv", "r");
- CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
- while (fscanf(f, " %63[^,],%63[^,],%d,%" SCNu64 ",%d,%31[^,],%" SCNu64,
- uuid, alias, &zid, &generation, &pid, name, &rss) == 7) {
- process_t *p;
- const zone_rule_t *rule = zone_rule(&cfg, uuid);
- CHECK(nfixtures < sizeof (fixtures) / sizeof (fixtures[0]));
- if (!rule) {
- zone_rule_t *z;
- CHECK(cfg.nzones < MAX_ZONES); z = &cfg.zones[cfg.nzones++];
- strcpy(z->uuid, uuid); strcpy(z->alias, alias); z->policy = ALLOW;
- }
- p = &fixtures[nfixtures++];
- p->pid = pid; p->ppid = 100; p->zid = zid; p->initpid = 9000 + zid;
- p->nlwp = 1; p->start_sec = 1000 + pid; p->start_nsec = pid;
- p->zone_generation = generation; p->rss_kib = rss;
- strcpy(p->uuid, uuid); strcpy(p->name, name);
- }
- fclose(f); CHECK(nfixtures == 16 && cfg.nzones == 5);
- scan_processes(1);
- }
-
- int main(void)
- {
- FILE *log = tmpfile(); char contents[65536]; size_t n;
- const process_t *chosen;
- unsigned second;
- CHECK(log != NULL); logfd = fileno(log);
- reset(); act(&fake_sample);
- CHECK(real_requests == 0 && validation_requests == 1 && controller.has_pending);
- fake_now += 5; scan_processes(0); act(&fake_sample);
- CHECK(real_requests == 0 && !controller.has_pending);
- rewind(log); n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
- CHECK(strstr(contents, "WOULD_ACTION=SIGTERM") != NULL);
- CHECK(strstr(contents, "WOULD_ACTION=SIGKILL") != NULL);
- CHECK(strstr(contents, "ACTION_INTENT=") == NULL);
- CHECK(fseek(log, 0, SEEK_END) == 0);
- puts("PASS actual daemon dry-run path records WOULD_ACTION=SIGTERM/SIGKILL; zero real requests");
- reset();
- fake_sample.arc_ok = 1;
- fake_sample.swap_virtual_ok = fake_sample.swap_physical_ok = fake_sample.pageout_ok = 1;
- for (second = 0; second <= 60; second++) {
- fake_sample.at = 100 + second;
- fake_sample.arc = UINT64_C(6000000000) + 1000 * second;
- fake_sample.swap_alloc = 7000000 + 3 * second;
- fake_sample.swap_reserved = 480000 + 2 * second;
- fake_sample.swap_available = 6200000 - second;
- fake_sample.swap_physical_used_bytes = UINT64_C(4143648768) + 4096 * second;
- fake_sample.anon_pageout_pages = 1000 + 2 * second;
- record_pressure_metrics(&fake_sample);
- record_auxiliary_metrics(&fake_sample);
- }
- log_sample(&fake_sample);
- CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
- n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
- CHECK(strstr(contents, "swap_committed_ok=1 swap_committed_pages=7480300") != NULL);
- CHECK(strstr(contents, "swap_physical_used_ok=1 swap_physical_used_bytes=4143894528") != NULL);
- CHECK(strstr(contents, "anon_pageout_ok=1 anon_pageout_pages=1120") != NULL);
- CHECK(strstr(contents, "swap_alloc_pages=7000180 swap_reserved_pages=480120 swap_available_pages=6199940") != NULL);
- CHECK(strstr(contents, "metric=swap_committed_pages d5=+25@5.00s d15=+75@15.00s d60=+300@60.00s") != NULL);
- CHECK(strstr(contents, "metric=swap_physical_used_bytes d5=+20480@5.00s d15=+61440@15.00s d60=+245760@60.00s") != NULL);
- CHECK(strstr(contents, "metric=anon_pageout_pages d5=+10@5.00s d15=+30@15.00s d60=+120@60.00s") != NULL);
- CHECK(strstr(contents, "swap_used_pages") == NULL);
- CHECK(fseek(log, 0, SEEK_END) == 0);
- puts("PASS continuous sample contract and 5/15/60 deltas for commitment, physical swap and pageout");
- reset(); cfg.armed = 1; act(&fake_sample);
- CHECK(real_requests == 1);
- fake_now += 5; scan_processes(0); act(&fake_sample); CHECK(real_requests == 2);
- reset(); cfg.armed = 1; started = 80; fake_now = 101; scan_processes(0);
- CHECK(candidate_cache[0].valid && engine.danger && walk_requests == 2);
- act(&fake_sample); CHECK(real_requests == 0 && candidate_cache[0].valid);
- CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
- n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
- CHECK(strstr(contents, "event=action_blocked reason=armed_warmup") != NULL);
- CHECK(fseek(log, 0, SEEK_END) == 0);
- reset(); cfg.armed = 1; log_ok = 0; act(&fake_sample); CHECK(real_requests == 1);
- reset(); cfg.armed = 1; partial = 1; fake_now++; scan_processes(0);
- act(&fake_sample); CHECK(real_requests == 1 && !scan_complete);
- reset(); cfg.armed = 1; fail_sample = 1; act(&fake_sample); CHECK(real_requests == 0);
- reset(); cfg.armed = 1; fail_validation = 1; act(&fake_sample); CHECK(real_requests == 0);
- reset(); cfg.armed = 1; fake_sample.free = 150000; act(&fake_sample); CHECK(real_requests == 0);
- reset(); cfg.armed = 1; fake_now += 16; act(&fake_sample); CHECK(real_requests == 1);
- reset(); cfg.armed = 1; stopping = 1; act(&fake_sample); CHECK(real_requests == 0);
- reset(); fake_sample.free = 20000; act(&fake_sample);
- CHECK(!controller.has_pending && controller.nvictims == 1);
- puts("PASS armed-path mock requests, explicit warmup, stale cache, logging/sampling failures, stop and fresh floor");
- reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
- fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
- strcpy(fixtures[1].uuid, cfg.zones[1].uuid); nfixtures = 2;
- fake_now = 101; scan_processes(0); chosen = candidate(); CHECK(chosen && chosen->pid == 124);
- /* Small zone grows fast enough to outweigh a larger steady zone. */
- fixtures[0].rss_kib = 600000; fake_now = 161; scan_processes(0);
- chosen = candidate(); CHECK(chosen && chosen->pid == 123);
- cfg.zones[0].policy = PROTECT; fake_now = 162; scan_processes(0);
- chosen = candidate(); CHECK(chosen && chosen->pid == 124);
- fixtures[1].zid = 0; fake_now = 163; scan_processes(0); CHECK(candidate() == NULL);
- puts("PASS global score uses process RSS/growth; protected zones and global zone excluded");
- load_global_ranking_fixture();
- CHECK(candidate_cache[0].valid && candidate_cache[1].valid && candidate_cache[2].valid);
- CHECK(!strcmp(candidate_cache[0].p.name, "clamd"));
- CHECK(!strcmp(candidate_cache[1].p.name, "clamd"));
- CHECK(!strcmp(candidate_cache[2].p.name, "clamd"));
- CHECK(candidate_cache[0].p.rss_kib > 1300000 &&
- candidate_cache[2].p.rss_kib > 1300000);
- CHECK(strcmp(candidate_cache[0].p.uuid, candidate_cache[1].p.uuid));
- CHECK(strcmp(candidate_cache[1].p.uuid, candidate_cache[2].p.uuid));
- for (second = 0; second < CACHED_CANDIDATES; second++) {
- CHECK(strcmp(candidate_cache[second].p.name, "mysqld"));
- CHECK(strcmp(candidate_cache[second].p.name, "zone-overhead"));
- }
- for (second = 0; second < nfixtures; second++)
- if (!strcmp(fixtures[second].name, "mysqld"))
- CHECK(protected_process(&cfg, &fixtures[second]));
- CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
- n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
- {
- char *mysql = strstr(contents, "comm=mysqld");
- char *ineligible = mysql ? strstr(mysql, "eligible=0") : NULL;
- char *line_end = mysql ? strchr(mysql, '\n') : NULL;
- CHECK(mysql && ineligible && line_end && ineligible < line_end);
- }
- CHECK(fseek(log, 0, SEEK_END) == 0);
- fixtures[nfixtures] = fixtures[0]; fixtures[nfixtures].pid = 9001;
- fixtures[nfixtures].zid = 90; fixtures[nfixtures].zone_generation = 900;
- fixtures[nfixtures].rss_kib = UINT64_C(9000000);
- strcpy(fixtures[nfixtures].uuid, "66666666-6666-4666-8666-666666666666");
- strcpy(fixtures[nfixtures].name, "unmapped-hog"); nfixtures++;
- fixtures[nfixtures] = fixtures[0]; fixtures[nfixtures].pid = 9002;
- fixtures[nfixtures].zid = 0; fixtures[nfixtures].rss_kib = UINT64_C(11000000);
- strcpy(fixtures[nfixtures].name, "global-hog"); nfixtures++;
- fake_now = 101; scan_processes(0);
- for (second = 0; second < CACHED_CANDIDATES; second++)
- CHECK(candidate_cache[second].p.pid != 9001 && candidate_cache[second].p.pid != 9002);
- for (second = 0; second < nfixtures; second++)
- if (fixtures[second].pid == 1103) fixtures[second].rss_kib = 600000;
- fake_now = 106; scan_processes(0);
- CHECK(candidate_cache[0].p.pid == 1103);
- CHECK(candidate_cache[0].growth5_component > 0 &&
- candidate_cache[0].total_score > candidate_cache[1].total_score);
- {
- cached_candidate_t scored;
- zone_t zone;
- memset(&scored, 0, sizeof (scored)); memset(&zone, 0, sizeof (zone));
- scored.p.rss_kib = 100000; scored.d5.valid = scored.d15.valid = 1;
- scored.d60.valid = 1; scored.d5.value = 1000;
- scored.d15.value = 2000; scored.d60.value = 3000;
- scan_at = 60; history_add(&zone.history, 0, 1000000);
- history_add(&zone.history, 60, 1100000); zone.rss = 1100000;
- score_candidate(&scored, &zone);
- CHECK(scored.growth5_component == 4000);
- CHECK(scored.growth15_component == 4000);
- CHECK(scored.growth60_component == 3000);
- CHECK(scored.zone_component <= scored.p.rss_kib / 4);
- }
- CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
- n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
- CHECK(strstr(contents, "rss_component=") != NULL);
- CHECK(strstr(contents, "growth5_component=") != NULL);
- CHECK(strstr(contents, "growth15_component=") != NULL);
- CHECK(strstr(contents, "growth60_component=") != NULL);
- CHECK(strstr(contents, "zone_component=") != NULL);
- CHECK(strstr(contents, "total_score=") != NULL);
- CHECK(fseek(log, 0, SEEK_END) == 0);
- puts("PASS SmartOS fixture produces a global cross-zone top-3 and bounded zone context");
- reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
- fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
- strcpy(fixtures[1].uuid, cfg.zones[1].uuid);
- fixtures[2] = fixtures[0]; fixtures[2].pid = 125; fixtures[2].rss_kib = 800000;
- nfixtures = 3; fake_now = 101; scan_processes(0);
- CHECK(candidate_cache[0].valid && candidate_cache[1].valid && candidate_cache[2].valid);
- invalid_pid = candidate_cache[0].p.pid;
- partial = 1; fake_now = 102; scan_processes(0);
- CHECK(!scan_complete && candidate_cache[0].p.pid == invalid_pid);
- {
- unsigned walks_before = walk_requests;
- engine.state = EMERGENCY; engine.danger = 1; fake_sample.free = 40000;
- act(&fake_sample);
- CHECK(walk_requests == walks_before); /* O(1): no /proc walk in action path. */
- CHECK(controller.has_pending && controller.pending.pid != invalid_pid);
- CHECK(validation_requests >= 2 && real_requests == 0);
- }
- CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
- n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
- CHECK(strstr(contents, "candidate_source=fallback_candidate") != NULL);
- CHECK(strstr(contents, "scan_complete=0") != NULL);
- CHECK(fseek(log, 0, SEEK_END) == 0);
- puts("PASS incomplete scan preserves cache; emergency uses O(1) fallback without a new walk");
- reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
- fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
- strcpy(fixtures[1].uuid, cfg.zones[1].uuid); nfixtures = 2;
- fake_now = 101; scan_processes(0);
- for (second = 0; second < nfixtures; second++)
- if (fixtures[second].pid == candidate_cache[0].p.pid) fixtures[second].start_nsec++;
- engine.state = EMERGENCY; engine.danger = 1; fake_sample.free = 40000;
- act(&fake_sample);
- CHECK(controller.has_pending && !same_process(&controller.pending, &candidate_cache[0].p));
- puts("PASS PID/start-time reuse rejects rank 1 and selects cached fallback");
- reset();
- CHECK(platform_syslog_open("o6h-memory-guard", cfg.syslog_facility) == 0);
- syslog_ready = 1;
- CHECK(!strcmp(syslog_ident, "o6h-memory-guard") && syslog_facility == 5 &&
- syslog_open_count == 1);
- emit_important(LOG_INFO, "event=start version=%s mode=dry-run", VERSION);
- CHECK(syslog_count == 1 && syslog_records[0].priority == LOG_INFO &&
- strstr(syslog_records[0].message, "event=start version=" VERSION) != NULL);
- log_sample(&fake_sample); scan_processes(0);
- CHECK(syslog_count == 1); /* High-volume sample/delta/proc/cache events stay local. */
- {
- char message[512]; sample_t s = fake_sample;
- s.free = s.min;
- CHECK(!transition_message(message, sizeof (message), NORMAL, EMERGENCY, &s, 1));
- CHECK(strstr(message, "reason=threshold_jump") != NULL);
- CHECK(strstr(message, "skipped_states=PRE-PRESSURE,PRESSURE,CRITICAL") != NULL);
- syslog_event(state_priority(EMERGENCY, 0), "%s", message);
- CHECK(syslog_count == 2 && syslog_records[1].priority == LOG_CRIT);
- }
- cfg.armed = 0; act(&fake_sample);
- CHECK(syslog_count == 3 && real_requests == 0);
- reset(); cfg.armed = 1; started = 80; fake_now = 100;
- CHECK(platform_syslog_open("o6h-memory-guard", cfg.syslog_facility) == 0);
- syslog_ready = 1; act(&fake_sample);
- CHECK(real_requests == 0 && syslog_count == 1 &&
- syslog_records[0].priority == LOG_WARNING &&
- strstr(syslog_records[0].message, "event=action_blocked reason=armed_warmup") != NULL);
- reset(); cfg.armed = 1;
- CHECK(platform_syslog_open("o6h-memory-guard", cfg.syslog_facility) == 0);
- syslog_ready = 1; act(&fake_sample);
- CHECK(real_requests == 1 && syslog_count == 3);
- CHECK(strstr(syslog_records[0].message, "event=victim_budget tier=normal") != NULL);
- CHECK(syslog_records[1].priority == LOG_ERR &&
- strstr(syslog_records[1].message, "event=action_decision state=CRITICAL") != NULL &&
- strstr(syslog_records[1].message, "result=sent") != NULL);
- CHECK(syslog_records[2].priority == LOG_ERR &&
- strstr(syslog_records[2].message, "event=signal_sent state=CRITICAL") != NULL &&
- strstr(syslog_records[2].message, "signal=SIGTERM") != NULL &&
- strstr(syslog_records[2].message, "UUID=") != NULL &&
- strstr(syslog_records[2].message, "alias=web-a") != NULL &&
- strstr(syslog_records[2].message, "pid=123") != NULL &&
- strstr(syslog_records[2].message, "comm=php-fpm") != NULL &&
- strstr(syslog_records[2].message, "rss_kib=100000") != NULL &&
- strstr(syslog_records[2].message, "free_pages=60000") != NULL);
- reset(); cfg.armed = 1; fail_syslog = 1; syslog_ready = 1;
- act(&fake_sample); CHECK(real_requests == 1 && controller.has_pending);
- reset(); cfg.syslog_enabled = 0; syslog_ready = 0;
- emit_important(LOG_INFO, "event=start disabled_test=yes"); CHECK(syslog_count == 0);
- CHECK(state_priority(PRE_PRESSURE, 0) == LOG_NOTICE);
- CHECK(state_priority(PRESSURE, 0) == LOG_WARNING);
- CHECK(state_priority(CRITICAL, 0) == LOG_ERR);
- CHECK(state_priority(EMERGENCY, 0) == LOG_CRIT);
- CHECK(state_priority(RECOVERY, 0) == LOG_INFO);
- CHECK(state_priority(NORMAL, 1) == LOG_CRIT);
- log_sampling_overrun(4.459, &fake_sample);
- CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
- n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
- CHECK(strstr(contents, "event=sampling_overrun late_seconds=4.459 state=CRITICAL free_pages=60000") != NULL);
- puts("PASS selective native syslog ident/facility/severity, exclusions, signal ordering, disable and failure isolation");
- critical_path_faults();
- floor_errno_fixture();
- diagnostic_fixtures();
- panic33_fixture();
- partial_cache_tests();
- fclose(log);
- printf("PASS %u runtime checks; all platform calls mocked, no signals sent\n", checks);
- return 0;
- }
|