Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

286 行
16 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. #define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL runtime line %d: %s\n", __LINE__, #x); exit(1); } checks++; } while (0)
  11. double monotime(void) { return fake_now; }
  12. int secure_file(const char *p, int flags, unsigned mode)
  13. { (void)p; (void)flags; (void)mode; errno = EPERM; return -1; }
  14. int secure_directory(const char *p) { (void)p; return -1; }
  15. int platform_init(void) { return 0; }
  16. void platform_fini(void) { }
  17. int platform_sample(sample_t *s)
  18. { *s = fake_sample; s->at = fake_now; return fail_sample ? -1 : 0; }
  19. int platform_walk(int (*fn)(const process_t *, void *), void *arg, unsigned budget)
  20. {
  21. unsigned j; (void)budget;
  22. walk_requests++;
  23. for (j = 0; j < nfixtures; j++) if (fn(&fixtures[j], arg)) return -1;
  24. return partial ? -1 : 0;
  25. }
  26. int platform_signal(const config_t *c, const process_t *p, enum action action,
  27. int real, uint64_t ceiling)
  28. {
  29. unsigned j; int found = 0;
  30. CHECK(action == TERM || action == KILL);
  31. CHECK(!protected_process(c, p));
  32. for (j = 0; j < nfixtures; j++)
  33. if (fixtures[j].pid == p->pid && same_process(&fixtures[j], p)) { found = 1; break; }
  34. if (real) { CHECK(c->armed && fake_sample.free < ceiling); real_requests++; }
  35. else validation_requests++;
  36. if (fail_validation || p->pid == invalid_pid || !found) { errno = ESTALE; return -1; }
  37. return 0; /* This test backend cannot send any signal. */
  38. }
  39. static void reset(void)
  40. {
  41. defaults(&cfg); cfg.log_mib = 100;
  42. cfg.nzones = 2;
  43. strcpy(cfg.zones[0].uuid, "11111111-1111-4111-8111-111111111111");
  44. strcpy(cfg.zones[0].alias, "web-a"); cfg.zones[0].policy = ALLOW;
  45. strcpy(cfg.zones[1].uuid, "22222222-2222-4222-8222-222222222222");
  46. strcpy(cfg.zones[1].alias, "web-b"); cfg.zones[1].policy = ALLOW;
  47. memset(processes, 0, sizeof (processes)); memset(zone_stats, 0, sizeof (zone_stats));
  48. memset(candidate_cache, 0, sizeof (candidate_cache));
  49. memset(metrics, 0, sizeof (metrics));
  50. memset(&controller, 0, sizeof (controller)); memset(fixtures, 0, sizeof (fixtures));
  51. engine_init(&engine); engine.state = CRITICAL; engine.danger = 1;
  52. stopping = 0; started = 0; log_ok = 1; log_bytes = 0;
  53. scan_generation = 0; scan_complete = 0; latest_complete_scan_at = 0;
  54. fake_now = 100; partial = fail_sample = fail_validation = invalid_pid = 0;
  55. real_requests = validation_requests = walk_requests = 0;
  56. memset(&fake_sample, 0, sizeof (fake_sample)); fake_sample.at = fake_now;
  57. fake_sample.free = 60000; fake_sample.lots = 130875;
  58. fake_sample.des = 65437; fake_sample.min = 49077;
  59. fixtures[0].pid = 123; fixtures[0].ppid = 100; fixtures[0].zid = 9;
  60. fixtures[0].initpid = 99; fixtures[0].nlwp = 1; fixtures[0].start_sec = 1000;
  61. fixtures[0].zone_generation = 20; fixtures[0].rss_kib = 100000;
  62. strcpy(fixtures[0].uuid, cfg.zones[0].uuid); strcpy(fixtures[0].name, "php-fpm");
  63. nfixtures = 1;
  64. scan_processes(0);
  65. }
  66. static void load_global_ranking_fixture(void)
  67. {
  68. FILE *f;
  69. char line[256], uuid[64], alias[64], name[32];
  70. int zid, pid;
  71. uint64_t generation, rss;
  72. reset();
  73. cfg.nzones = cfg.nnames = 0;
  74. strcpy(cfg.protected_names[cfg.nnames++], "mysqld");
  75. strcpy(cfg.protected_names[cfg.nnames++], "zone-overhead");
  76. memset(processes, 0, sizeof (processes));
  77. memset(zone_stats, 0, sizeof (zone_stats));
  78. memset(candidate_cache, 0, sizeof (candidate_cache));
  79. memset(&controller, 0, sizeof (controller));
  80. memset(fixtures, 0, sizeof (fixtures));
  81. scan_generation = 0; scan_complete = 0; latest_complete_scan_at = 0;
  82. nfixtures = 0; fake_now = 100;
  83. f = fopen("tests/fixtures/global_victim_ranking_smartos_2026_09.csv", "r");
  84. CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
  85. while (fscanf(f, " %63[^,],%63[^,],%d,%" SCNu64 ",%d,%31[^,],%" SCNu64,
  86. uuid, alias, &zid, &generation, &pid, name, &rss) == 7) {
  87. process_t *p;
  88. const zone_rule_t *rule = zone_rule(&cfg, uuid);
  89. CHECK(nfixtures < sizeof (fixtures) / sizeof (fixtures[0]));
  90. if (!rule) {
  91. zone_rule_t *z;
  92. CHECK(cfg.nzones < MAX_ZONES); z = &cfg.zones[cfg.nzones++];
  93. strcpy(z->uuid, uuid); strcpy(z->alias, alias); z->policy = ALLOW;
  94. }
  95. p = &fixtures[nfixtures++];
  96. p->pid = pid; p->ppid = 100; p->zid = zid; p->initpid = 9000 + zid;
  97. p->nlwp = 1; p->start_sec = 1000 + pid; p->start_nsec = pid;
  98. p->zone_generation = generation; p->rss_kib = rss;
  99. strcpy(p->uuid, uuid); strcpy(p->name, name);
  100. }
  101. fclose(f); CHECK(nfixtures == 16 && cfg.nzones == 5);
  102. scan_processes(1);
  103. }
  104. int main(void)
  105. {
  106. FILE *log = tmpfile(); char contents[65536]; size_t n;
  107. const process_t *chosen;
  108. unsigned second;
  109. CHECK(log != NULL); logfd = fileno(log);
  110. reset(); act(&fake_sample);
  111. CHECK(real_requests == 0 && validation_requests == 1 && controller.has_pending);
  112. fake_now += 5; scan_processes(0); act(&fake_sample);
  113. CHECK(real_requests == 0 && !controller.has_pending);
  114. rewind(log); n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  115. CHECK(strstr(contents, "WOULD_ACTION=SIGTERM") != NULL);
  116. CHECK(strstr(contents, "WOULD_ACTION=SIGKILL") != NULL);
  117. CHECK(strstr(contents, "ACTION_INTENT=") == NULL);
  118. CHECK(fseek(log, 0, SEEK_END) == 0);
  119. puts("PASS actual daemon dry-run path records WOULD_ACTION=SIGTERM/SIGKILL; zero real requests");
  120. reset();
  121. fake_sample.arc_ok = 1;
  122. fake_sample.swap_virtual_ok = fake_sample.swap_physical_ok = fake_sample.pageout_ok = 1;
  123. for (second = 0; second <= 60; second++) {
  124. fake_sample.at = 100 + second;
  125. fake_sample.arc = UINT64_C(6000000000) + 1000 * second;
  126. fake_sample.swap_alloc = 7000000 + 3 * second;
  127. fake_sample.swap_reserved = 480000 + 2 * second;
  128. fake_sample.swap_available = 6200000 - second;
  129. fake_sample.swap_physical_used_bytes = UINT64_C(4143648768) + 4096 * second;
  130. fake_sample.anon_pageout_pages = 1000 + 2 * second;
  131. record_metrics(&fake_sample);
  132. }
  133. log_sample(&fake_sample);
  134. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  135. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  136. CHECK(strstr(contents, "swap_committed_ok=1 swap_committed_pages=7480300") != NULL);
  137. CHECK(strstr(contents, "swap_physical_used_ok=1 swap_physical_used_bytes=4143894528") != NULL);
  138. CHECK(strstr(contents, "anon_pageout_ok=1 anon_pageout_pages=1120") != NULL);
  139. CHECK(strstr(contents, "swap_alloc_pages=7000180 swap_reserved_pages=480120 swap_available_pages=6199940") != NULL);
  140. CHECK(strstr(contents, "metric=swap_committed_pages d5=+25@5.00s d15=+75@15.00s d60=+300@60.00s") != NULL);
  141. CHECK(strstr(contents, "metric=swap_physical_used_bytes d5=+20480@5.00s d15=+61440@15.00s d60=+245760@60.00s") != NULL);
  142. CHECK(strstr(contents, "metric=anon_pageout_pages d5=+10@5.00s d15=+30@15.00s d60=+120@60.00s") != NULL);
  143. CHECK(strstr(contents, "swap_used_pages") == NULL);
  144. CHECK(fseek(log, 0, SEEK_END) == 0);
  145. puts("PASS continuous sample contract and 5/15/60 deltas for commitment, physical swap and pageout");
  146. reset(); cfg.armed = 1; act(&fake_sample);
  147. CHECK(real_requests == 1);
  148. fake_now += 5; scan_processes(0); act(&fake_sample); CHECK(real_requests == 2);
  149. reset(); cfg.armed = 1; started = 80; fake_now = 101; scan_processes(0);
  150. CHECK(candidate_cache[0].valid && engine.danger && walk_requests == 2);
  151. act(&fake_sample); CHECK(real_requests == 0 && candidate_cache[0].valid);
  152. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  153. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  154. CHECK(strstr(contents, "event=action_blocked reason=armed_warmup") != NULL);
  155. CHECK(fseek(log, 0, SEEK_END) == 0);
  156. reset(); cfg.armed = 1; log_ok = 0; act(&fake_sample); CHECK(real_requests == 0);
  157. reset(); cfg.armed = 1; partial = 1; fake_now++; scan_processes(0);
  158. act(&fake_sample); CHECK(real_requests == 1 && !scan_complete);
  159. reset(); cfg.armed = 1; fail_sample = 1; act(&fake_sample); CHECK(real_requests == 0);
  160. reset(); cfg.armed = 1; fail_validation = 1; act(&fake_sample); CHECK(real_requests == 0);
  161. reset(); cfg.armed = 1; fake_sample.free = 150000; act(&fake_sample); CHECK(real_requests == 0);
  162. reset(); cfg.armed = 1; fake_now += 16; act(&fake_sample); CHECK(real_requests == 0);
  163. reset(); cfg.armed = 1; stopping = 1; act(&fake_sample); CHECK(real_requests == 0);
  164. reset(); fake_sample.free = 20000; act(&fake_sample);
  165. CHECK(!controller.has_pending && controller.nvictims == 1);
  166. puts("PASS armed-path mock requests, explicit warmup, stale cache, logging/sampling failures, stop and fresh floor");
  167. reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
  168. fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
  169. strcpy(fixtures[1].uuid, cfg.zones[1].uuid); nfixtures = 2;
  170. fake_now = 101; scan_processes(0); chosen = candidate(); CHECK(chosen && chosen->pid == 124);
  171. /* Small zone grows fast enough to outweigh a larger steady zone. */
  172. fixtures[0].rss_kib = 600000; fake_now = 161; scan_processes(0);
  173. chosen = candidate(); CHECK(chosen && chosen->pid == 123);
  174. cfg.zones[0].policy = PROTECT; fake_now = 162; scan_processes(0);
  175. chosen = candidate(); CHECK(chosen && chosen->pid == 124);
  176. fixtures[1].zid = 0; fake_now = 163; scan_processes(0); CHECK(candidate() == NULL);
  177. puts("PASS global score uses process RSS/growth; protected zones and global zone excluded");
  178. load_global_ranking_fixture();
  179. CHECK(candidate_cache[0].valid && candidate_cache[1].valid && candidate_cache[2].valid);
  180. CHECK(!strcmp(candidate_cache[0].p.name, "clamd"));
  181. CHECK(!strcmp(candidate_cache[1].p.name, "clamd"));
  182. CHECK(!strcmp(candidate_cache[2].p.name, "clamd"));
  183. CHECK(candidate_cache[0].p.rss_kib > 1300000 &&
  184. candidate_cache[2].p.rss_kib > 1300000);
  185. CHECK(strcmp(candidate_cache[0].p.uuid, candidate_cache[1].p.uuid));
  186. CHECK(strcmp(candidate_cache[1].p.uuid, candidate_cache[2].p.uuid));
  187. for (second = 0; second < CACHED_CANDIDATES; second++) {
  188. CHECK(strcmp(candidate_cache[second].p.name, "mysqld"));
  189. CHECK(strcmp(candidate_cache[second].p.name, "zone-overhead"));
  190. }
  191. for (second = 0; second < nfixtures; second++)
  192. if (!strcmp(fixtures[second].name, "mysqld"))
  193. CHECK(protected_process(&cfg, &fixtures[second]));
  194. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  195. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  196. {
  197. char *mysql = strstr(contents, "comm=mysqld");
  198. char *ineligible = mysql ? strstr(mysql, "eligible=0") : NULL;
  199. char *line_end = mysql ? strchr(mysql, '\n') : NULL;
  200. CHECK(mysql && ineligible && line_end && ineligible < line_end);
  201. }
  202. CHECK(fseek(log, 0, SEEK_END) == 0);
  203. fixtures[nfixtures] = fixtures[0]; fixtures[nfixtures].pid = 9001;
  204. fixtures[nfixtures].zid = 90; fixtures[nfixtures].zone_generation = 900;
  205. fixtures[nfixtures].rss_kib = UINT64_C(9000000);
  206. strcpy(fixtures[nfixtures].uuid, "66666666-6666-4666-8666-666666666666");
  207. strcpy(fixtures[nfixtures].name, "unmapped-hog"); nfixtures++;
  208. fixtures[nfixtures] = fixtures[0]; fixtures[nfixtures].pid = 9002;
  209. fixtures[nfixtures].zid = 0; fixtures[nfixtures].rss_kib = UINT64_C(11000000);
  210. strcpy(fixtures[nfixtures].name, "global-hog"); nfixtures++;
  211. fake_now = 101; scan_processes(0);
  212. for (second = 0; second < CACHED_CANDIDATES; second++)
  213. CHECK(candidate_cache[second].p.pid != 9001 && candidate_cache[second].p.pid != 9002);
  214. for (second = 0; second < nfixtures; second++)
  215. if (fixtures[second].pid == 1103) fixtures[second].rss_kib = 600000;
  216. fake_now = 106; scan_processes(0);
  217. CHECK(candidate_cache[0].p.pid == 1103);
  218. CHECK(candidate_cache[0].growth5_component > 0 &&
  219. candidate_cache[0].total_score > candidate_cache[1].total_score);
  220. {
  221. cached_candidate_t scored;
  222. zone_t zone;
  223. memset(&scored, 0, sizeof (scored)); memset(&zone, 0, sizeof (zone));
  224. scored.p.rss_kib = 100000; scored.d5.valid = scored.d15.valid = 1;
  225. scored.d60.valid = 1; scored.d5.value = 1000;
  226. scored.d15.value = 2000; scored.d60.value = 3000;
  227. scan_at = 60; history_add(&zone.history, 0, 1000000);
  228. history_add(&zone.history, 60, 1100000); zone.rss = 1100000;
  229. score_candidate(&scored, &zone);
  230. CHECK(scored.growth5_component == 4000);
  231. CHECK(scored.growth15_component == 4000);
  232. CHECK(scored.growth60_component == 3000);
  233. CHECK(scored.zone_component <= scored.p.rss_kib / 4);
  234. }
  235. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  236. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  237. CHECK(strstr(contents, "rss_component=") != NULL);
  238. CHECK(strstr(contents, "growth5_component=") != NULL);
  239. CHECK(strstr(contents, "growth15_component=") != NULL);
  240. CHECK(strstr(contents, "growth60_component=") != NULL);
  241. CHECK(strstr(contents, "zone_component=") != NULL);
  242. CHECK(strstr(contents, "total_score=") != NULL);
  243. CHECK(fseek(log, 0, SEEK_END) == 0);
  244. puts("PASS SmartOS fixture produces a global cross-zone top-3 and bounded zone context");
  245. reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
  246. fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
  247. strcpy(fixtures[1].uuid, cfg.zones[1].uuid);
  248. fixtures[2] = fixtures[0]; fixtures[2].pid = 125; fixtures[2].rss_kib = 800000;
  249. nfixtures = 3; fake_now = 101; scan_processes(0);
  250. CHECK(candidate_cache[0].valid && candidate_cache[1].valid && candidate_cache[2].valid);
  251. invalid_pid = candidate_cache[0].p.pid;
  252. partial = 1; fake_now = 102; scan_processes(0);
  253. CHECK(!scan_complete && candidate_cache[0].p.pid == invalid_pid);
  254. {
  255. unsigned walks_before = walk_requests;
  256. engine.state = EMERGENCY; engine.danger = 1; fake_sample.free = 40000;
  257. act(&fake_sample);
  258. CHECK(walk_requests == walks_before); /* O(1): no /proc walk in action path. */
  259. CHECK(controller.has_pending && controller.pending.pid != invalid_pid);
  260. CHECK(validation_requests >= 2 && real_requests == 0);
  261. }
  262. CHECK(fflush(log) == 0 && fseek(log, 0, SEEK_SET) == 0);
  263. n = fread(contents, 1, sizeof (contents) - 1, log); contents[n] = 0;
  264. CHECK(strstr(contents, "candidate_source=fallback_candidate") != NULL);
  265. CHECK(strstr(contents, "scan_complete=0") != NULL);
  266. CHECK(fseek(log, 0, SEEK_END) == 0);
  267. puts("PASS incomplete scan preserves cache; emergency uses O(1) fallback without a new walk");
  268. reset(); fixtures[1] = fixtures[0]; fixtures[1].pid = 124;
  269. fixtures[1].rss_kib = 900000; fixtures[1].zid = 10;
  270. strcpy(fixtures[1].uuid, cfg.zones[1].uuid); nfixtures = 2;
  271. fake_now = 101; scan_processes(0);
  272. for (second = 0; second < nfixtures; second++)
  273. if (fixtures[second].pid == candidate_cache[0].p.pid) fixtures[second].start_nsec++;
  274. engine.state = EMERGENCY; engine.danger = 1; fake_sample.free = 40000;
  275. act(&fake_sample);
  276. CHECK(controller.has_pending && !same_process(&controller.pending, &candidate_cache[0].p));
  277. puts("PASS PID/start-time reuse rejects rank 1 and selects cached fallback");
  278. fclose(log);
  279. printf("PASS %u runtime checks; all platform calls mocked, no signals sent\n", checks);
  280. return 0;
  281. }