|
- /* 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 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 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; }
- 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; return fail_sample ? -1 : 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;
- }
- int platform_signal(const config_t *c, const process_t *p, enum action action,
- int real, uint64_t ceiling)
- {
- unsigned j; int found = 0;
- CHECK(action == TERM || action == KILL);
- 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; }
- if (real) { CHECK(c->armed && fake_sample.free < ceiling); real_requests++; }
- else validation_requests++;
- if (fail_validation || p->pid == invalid_pid || !found) { errno = ESTALE; return -1; }
- return 0; /* This test backend cannot send any signal. */
- }
- 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(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;
- 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;
- scan_generation = 0; scan_complete = 0; 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 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_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 == 0);
- 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 == 0);
- 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=1.0.6") != 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 == 2 && 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 == 2);
- CHECK(syslog_records[0].priority == LOG_ERR &&
- strstr(syslog_records[0].message, "event=action_decision state=CRITICAL") != NULL &&
- strstr(syslog_records[0].message, "result=sent") != NULL);
- CHECK(syslog_records[1].priority == LOG_ERR &&
- strstr(syslog_records[1].message, "event=signal_sent state=CRITICAL") != NULL &&
- strstr(syslog_records[1].message, "signal=SIGTERM") != NULL &&
- strstr(syslog_records[1].message, "UUID=") != NULL &&
- strstr(syslog_records[1].message, "alias=web-a") != NULL &&
- strstr(syslog_records[1].message, "pid=123") != NULL &&
- strstr(syslog_records[1].message, "comm=php-fpm") != NULL &&
- strstr(syslog_records[1].message, "rss_kib=100000") != NULL &&
- strstr(syslog_records[1].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");
- fclose(log);
- printf("PASS %u runtime checks; all platform calls mocked, no signals sent\n", checks);
- return 0;
- }
|