Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

353 lines
17 KiB

  1. #include "guard.h"
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <inttypes.h>
  6. static unsigned checks;
  7. #define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #x); exit(1); } checks++; } while (0)
  8. static const char *uuid = "11111111-1111-4111-8111-111111111111";
  9. static config_t allowed(void)
  10. {
  11. config_t c; defaults(&c);
  12. c.nzones = 1; strcpy(c.zones[0].uuid, uuid);
  13. strcpy(c.zones[0].alias, "fixture-web"); c.zones[0].policy = ALLOW;
  14. return c;
  15. }
  16. static process_t process(int pid)
  17. {
  18. process_t p; memset(&p, 0, sizeof (p));
  19. p.pid = pid; p.ppid = 100; p.zid = 9; p.initpid = 99; p.nlwp = 1;
  20. p.zone_generation = 20; p.start_sec = 1000; p.start_nsec = 123;
  21. p.rss_kib = 1048576; strcpy(p.uuid, uuid); strcpy(p.name, "php-fpm");
  22. return p;
  23. }
  24. static sample_t sample(double at, uint64_t free_pages, uint64_t scanner)
  25. {
  26. sample_t s; memset(&s, 0, sizeof (s)); s.at = at; s.free = free_pages;
  27. s.lots = 130875; s.des = 65437; s.min = 49077; s.scan = scanner;
  28. return s;
  29. }
  30. static void step(engine_t *e, config_t *c, double at, uint64_t free_pages, uint64_t scanner)
  31. {
  32. sample_t s = sample(at, free_pages, scanner); delta_t d = {0, 0, 0};
  33. engine_step(e, c, &s, d, d, d);
  34. }
  35. static int parse(const char *text, config_t *c)
  36. {
  37. FILE *f = tmpfile(); int result; char error[128];
  38. CHECK(f != NULL); CHECK(fputs(text, f) >= 0); rewind(f);
  39. result = config_read(f, c, error, sizeof (error)); fclose(f); return result;
  40. }
  41. static void configs(void)
  42. {
  43. config_t c;
  44. CHECK(parse("# defaults\n", &c) == 0 && !c.armed && c.pre_s == 1 &&
  45. c.pressure_s == 2 && c.max_cached_candidate_age_s == 15 &&
  46. c.max_emergency_victims == 8 && c.max_floor_victims == 12);
  47. CHECK(parse("ACTION_MODE dry-run\nprotect_process postgres\n", &c) == 0);
  48. CHECK(c.syslog_enabled && c.syslog_facility == 5);
  49. CHECK(parse("SYSLOG_ENABLED no\nSYSLOG_FACILITY local0\n", &c) == 0 &&
  50. !c.syslog_enabled && c.syslog_facility == 0);
  51. CHECK(parse("SYSLOG_ENABLED yes\nSYSLOG_FACILITY local7\n", &c) == 0 &&
  52. c.syslog_enabled && c.syslog_facility == 7);
  53. CHECK(parse("SYSLOG_ENABLED maybe\n", &c) != 0);
  54. CHECK(parse("SYSLOG_FACILITY daemon\n", &c) != 0);
  55. CHECK(parse("SYSLOG_FACILITY local8\n", &c) != 0);
  56. CHECK(parse("SYSLOG_FACILITY local5\nSYSLOG_FACILITY local6\n", &c) != 0);
  57. CHECK(parse("ACTION_MODE armd\n", &c) != 0);
  58. CHECK(parse("ACTION_MODE armed\n", &c) != 0);
  59. CHECK(parse("ACTION_MODE dry-run\nACTION_MODE armed\n", &c) != 0);
  60. CHECK(parse("LOG_KEEP 0\n", &c) != 0);
  61. CHECK(parse("TERM_GRACE_SECONDS -1\n", &c) != 0);
  62. CHECK(parse("TERM_GRACE_SECONDS 1 garbage\n", &c) != 0);
  63. CHECK(parse("CRITICAL_TERM_GRACE_SECONDS 4\nEMERGENCY_TERM_GRACE_SECONDS 1\n"
  64. "FLOOR_KILL_DELAY_SECONDS 0\nMAX_CACHED_CANDIDATE_AGE_SECONDS 15\n", &c) == 0);
  65. CHECK(parse("MAX_EMERGENCY_VICTIMS_EPISODE 3\n", &c) != 0);
  66. CHECK(parse("MAX_FLOOR_VICTIMS_EPISODE 7\n", &c) != 0);
  67. CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 16\n", &c) == 0 && c.max_hard_floor_victims == 16);
  68. CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 17\n", &c) != 0);
  69. CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 11\n", &c) != 0);
  70. CHECK(parse("MAX_HARD_FLOOR_VICTIMS_EPISODE 16\nMAX_HARD_FLOOR_VICTIMS_EPISODE 16\n", &c) != 0);
  71. CHECK(parse("UNKNOWN foo\n", &c) != 0);
  72. CHECK(parse("PRE_SECONDS 20\nPRESSURE_SECONDS 10\n", &c) != 0);
  73. CHECK(parse("zone global global allow\n", &c) != 0);
  74. CHECK(parse("zone 11111111-1111-4111-8111-111111111111 web allow\nACTION_MODE armed\n", &c) == 0 && c.armed);
  75. CHECK(parse("zone 11111111-1111-4111-8111-111111111111 web allow\nzone 11111111-1111-4111-8111-111111111111 db protect\n", &c) != 0);
  76. CHECK(!uuid_valid("11111111-1111-4111-8111-11111111111g"));
  77. CHECK(!uuid_valid("00000000-0000-0000-0000-000000000000"));
  78. {
  79. FILE *f = fopen("etc/o6h-memory-guard.conf", "r"); char error[128];
  80. CHECK(f != NULL); CHECK(config_read(f, &c, error, sizeof (error)) == 0);
  81. fclose(f); CHECK(!c.armed && c.pre_s == 1 && c.pressure_s == 2);
  82. }
  83. puts("PASS strict configuration, safe defaults, explicit UUID authorization");
  84. }
  85. static void protections(void)
  86. {
  87. config_t c = allowed(); process_t p = process(123), q;
  88. CHECK(!protected_process(&c, &p));
  89. q = p; q.zid = 0; CHECK(protected_process(&c, &q));
  90. q = p; q.pid = 1; CHECK(protected_process(&c, &q));
  91. q = p; q.pid = q.initpid; CHECK(protected_process(&c, &q));
  92. q = p; q.initpid = 0; CHECK(protected_process(&c, &q));
  93. q = p; q.zone_generation = 0; CHECK(protected_process(&c, &q));
  94. q = p; q.nlwp = 0; CHECK(protected_process(&c, &q));
  95. q = p; q.ppid = 0; CHECK(protected_process(&c, &q));
  96. q = p; strcpy(q.name, "sshd"); CHECK(protected_process(&c, &q));
  97. q = p; q.rss_kib = 100; CHECK(protected_process(&c, &q));
  98. q = p; q.uuid[0] = '2'; CHECK(protected_process(&c, &q));
  99. c.zones[0].policy = PROTECT; CHECK(protected_process(&c, &p));
  100. c.zones[0].policy = OBSERVE; CHECK(protected_process(&c, &p));
  101. c.zones[0].policy = ALLOW;
  102. strcpy(c.protected_names[c.nnames++], "php-fpm"); CHECK(protected_process(&c, &p));
  103. c.nnames = 0; c.protected_pids[c.npids++] = p.pid; CHECK(protected_process(&c, &p));
  104. q = p; CHECK(same_process(&p, &q));
  105. q.start_nsec++; CHECK(!same_process(&p, &q));
  106. q = p; q.zone_generation++; CHECK(!same_process(&p, &q));
  107. q = p; q.uuid[0] = '2'; CHECK(!same_process(&p, &q));
  108. puts("PASS mandatory protections, PID reuse and zone incarnation identity");
  109. }
  110. static void histories(void)
  111. {
  112. history_t h = {0}; delta_t d; unsigned i;
  113. for (i = 0; i <= 70; i++) history_add(&h, i, 10000 - 10 * i);
  114. d = history_delta(&h, 70, 5); CHECK(d.valid && d.value == -50 && d.age == 5);
  115. d = history_delta(&h, 70, 15); CHECK(d.valid && d.value == -150);
  116. d = history_delta(&h, 70, 60); CHECK(d.valid && d.value == -600);
  117. CHECK(!history_delta(&h, 90, 5).valid);
  118. memset(&h, 0, sizeof (h));
  119. history_add(&h, 0, 123); history_add(&h, 15, 100);
  120. CHECK(!history_delta(&h, 15, 5).valid);
  121. CHECK(history_delta(&h, 15, 15).value == -23);
  122. d.valid = 1; d.value = 100;
  123. CHECK(rank_score(1000, d) == 1200);
  124. CHECK(rank_score(UINT64_MAX - 10, d) == UINT64_MAX);
  125. d.value = -100; CHECK(rank_score(1000, d) == 1000);
  126. {
  127. uint64_t bytes = 0;
  128. CHECK(swap_physical_bytes_from_blocks(67024888, 58931824, &bytes) == 0);
  129. CHECK(bytes == UINT64_C(4143648768));
  130. CHECK(swap_physical_bytes_from_blocks(10, 11, &bytes) != 0);
  131. CHECK(swap_physical_bytes_from_blocks(UINT64_MAX, 0, &bytes) != 0);
  132. }
  133. puts("PASS timestamped 5/15/60s windows, missing data, signed growth, overflow");
  134. }
  135. static void states(void)
  136. {
  137. config_t c = allowed(); engine_t e; unsigned i;
  138. engine_init(&e);
  139. for (i = 0; i < 10; i++) step(&e, &c, i, 425950, 0);
  140. CHECK(e.state == NORMAL && !e.danger);
  141. step(&e, &c, 10, 100000, 0); CHECK(e.state == NORMAL);
  142. step(&e, &c, 11, 100000, 0); CHECK(e.state == PRE_PRESSURE);
  143. step(&e, &c, 12, 100000, 0);
  144. CHECK(e.state == PRESSURE && !e.danger);
  145. step(&e, &c, 21, 60000, 1);
  146. CHECK(e.state == CRITICAL && e.danger); /* delayed threshold jump */
  147. step(&e, &c, 22, 59000, 1); CHECK(e.danger);
  148. step(&e, &c, 23, 58000, 1); CHECK(e.state == CRITICAL && e.danger);
  149. step(&e, &c, 24, 140000, 0); CHECK(e.state == RECOVERY && !e.danger);
  150. for (i = 25; i <= 39; i++) step(&e, &c, i, 140000, 0);
  151. CHECK(e.state == NORMAL);
  152. for (i = 40; i <= 56; i++) step(&e, &c, i, 150000, 0);
  153. CHECK(e.state == NORMAL);
  154. step(&e, &c, 57, 40000, 0); CHECK(e.state == EMERGENCY && e.danger && !e.floor);
  155. step(&e, &c, 58, 20000, 0); CHECK(e.floor);
  156. step(&e, &c, 59, 70000, 0); CHECK(e.state == RECOVERY && !e.danger);
  157. engine_init(&e);
  158. for (i = 0; i < 20; i++) step(&e, &c, i, 60000, 0);
  159. CHECK(!e.danger); /* low but stable, no scanner */
  160. engine_init(&e);
  161. step(&e, &c, 0, 60000, 1); step(&e, &c, 20, 59000, 1);
  162. CHECK(e.danger); /* monotonic elapsed time survives delayed sampling */
  163. engine_gap(&e); step(&e, &c, 21, 58000, 1); CHECK(!e.danger);
  164. step(&e, &c, 22, 57000, 1); step(&e, &c, 23, 56000, 1); CHECK(e.danger);
  165. engine_init(&e);
  166. step(&e, &c, 0.0, 150000, 0);
  167. step(&e, &c, 1.8, 100000, 0); CHECK(e.state == NORMAL);
  168. step(&e, &c, 4.2, 95000, 0); CHECK(e.state == PRESSURE && !e.danger);
  169. engine_init(&e);
  170. step(&e, &c, 0.0, 150000, 0);
  171. step(&e, &c, 3.1, 65000, 1); CHECK(e.state == CRITICAL && e.danger);
  172. {
  173. sample_t s = sample(1, 60000, 0);
  174. delta_t none = {0, 0, 0}, rising = {1, 5, 4096};
  175. engine_init(&e); engine_step(&e, &c, &s, none, rising, none);
  176. CHECK(!e.danger && e.critical_since < 0); /* physical swap alone is inert */
  177. engine_init(&e); engine_step(&e, &c, &s, none, none, rising);
  178. CHECK(!e.danger && e.critical_since < 0); /* pageout alone is inert */
  179. s.free = 150000;
  180. engine_init(&e); engine_step(&e, &c, &s, none, rising, rising);
  181. CHECK(e.state == NORMAL && e.low_since < 0); /* corroboration needs low memory */
  182. s.free = 60000;
  183. engine_init(&e);
  184. for (i = 0; i <= 3; i++) {
  185. s.at = i; engine_step(&e, &c, &s, none, rising, rising);
  186. }
  187. CHECK(e.state == CRITICAL && e.danger);
  188. }
  189. puts("PASS all six states, monotonic overruns, threshold jumps, corroboration and recovery");
  190. }
  191. static void successful_avoidance_fixture(void)
  192. {
  193. config_t c = allowed(); engine_t e; FILE *f; char line[256], event[64];
  194. double at, late; uint64_t free_pages, nscan; unsigned rows = 0;
  195. engine_init(&e);
  196. f = fopen("tests/fixtures/successful_pageout_deadman_avoidance_2026_09_21.csv", "r");
  197. CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
  198. while (fscanf(f, "%lf,%" SCNu64 ",%" SCNu64 ",%lf,%63s",
  199. &at, &free_pages, &nscan, &late, event) == 5) {
  200. step(&e, &c, at, free_pages, nscan); rows++;
  201. CHECK(late >= 0.0);
  202. if (!strcmp(event, "emergency")) CHECK(e.state == EMERGENCY && e.danger);
  203. if (!strcmp(event, "recovery")) CHECK(e.state == RECOVERY && !e.danger);
  204. if (!strcmp(event, "normal")) CHECK(e.state == NORMAL && !e.danger);
  205. }
  206. fclose(f); CHECK(rows == 7); CHECK(e.state == NORMAL);
  207. puts("PASS successful_pageout_deadman_avoidance_2026_09_21 incident fixture");
  208. }
  209. static void real_incident_fixture(void)
  210. {
  211. config_t c = allowed(); engine_t e; FILE *f;
  212. unsigned t, rows = 0; uint64_t free_pages, nscan;
  213. char line[128];
  214. engine_init(&e);
  215. f = fopen("tests/fixtures/pageout_deadman_real_incident_2026_09.csv", "r");
  216. CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
  217. while (fscanf(f, "%u,%" SCNu64 ",%" SCNu64, &t, &free_pages, &nscan) == 3) {
  218. step(&e, &c, t, free_pages, nscan); rows++;
  219. if (t == 2) CHECK(e.state == PRE_PRESSURE);
  220. if (t == 3) CHECK(e.state == PRESSURE);
  221. }
  222. fclose(f);
  223. CHECK(rows == 5); CHECK(e.state == EMERGENCY && e.danger);
  224. puts("PASS pageout_deadman_real_incident_2026_09 exits NORMAL before minfree");
  225. }
  226. static void actions(void)
  227. {
  228. config_t c = allowed(); engine_t e; controller_t a = {0};
  229. process_t p = process(123); sample_t s; plan_t plan; unsigned i;
  230. engine_init(&e); e.state = CRITICAL; e.danger = 1;
  231. s = sample(100, 60000, 1);
  232. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
  233. action_commit(&a, &c, &s, &plan); CHECK(a.has_pending && a.nvictims == 1);
  234. printf("SIMULATED t=100 state=CRITICAL UUID=%s pid=123 WOULD_ACTION=SIGTERM\n", uuid);
  235. s.at = 104; plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == NONE);
  236. s.at = 105; plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  237. action_commit(&a, &c, &s, &plan); CHECK(!a.has_pending && a.nvictims == 1);
  238. puts("SIMULATED t=105 state=CRITICAL pid=123 WOULD_ACTION=SIGKILL reason=memory_not_recovered");
  239. p.pid++; s.at = 106; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  240. e.state = EMERGENCY; s.free = 40000;
  241. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
  242. action_commit(&a, &c, &s, &plan);
  243. s.at = 106.5; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  244. s.at = 107; CHECK(action_plan(&a, &c, &e, &s, &p).action == KILL);
  245. s.free = 20000; e.floor = 1;
  246. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  247. action_commit(&a, &c, &s, &plan);
  248. p.pid++; s.at++;
  249. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  250. action_commit(&a, &c, &s, &plan);
  251. puts("SIMULATED state=EMERGENCY floor=1 WOULD_ACTION=SIGKILL reason=floor");
  252. p.pid++; s.at++;
  253. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  254. action_commit(&a, &c, &s, &plan);
  255. p.pid++; s.at++;
  256. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  257. CHECK(!strcmp(plan.budget_override, "floor"));
  258. e.state = NORMAL; e.danger = 0; s.free = 160000;
  259. e.recovery_since = s.at - c.recovery_s;
  260. (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 0);
  261. e.state = EMERGENCY; e.danger = 1; e.floor = 0; s.free = 40000;
  262. a.nvictims = c.max_victims; a.nrate = c.max_per_minute;
  263. e.state = CRITICAL;
  264. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  265. e.state = EMERGENCY;
  266. a.nvictims = c.max_victims; a.nrate = c.max_per_minute;
  267. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
  268. CHECK(!strcmp(plan.budget_override, "emergency"));
  269. a.nvictims = c.max_emergency_victims;
  270. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  271. e.floor = 1; s.free = 20000;
  272. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  273. CHECK(!strcmp(plan.budget_override, "floor"));
  274. a.nvictims = c.max_floor_victims;
  275. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  276. memset(&a, 0, sizeof (a)); e.floor = 0; e.state = CRITICAL; s.free = 60000;
  277. plan = action_plan(&a, &c, &e, &s, &p); action_commit(&a, &c, &s, &plan);
  278. s.free = 140000; e.danger = 0; e.state = RECOVERY;
  279. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE && !a.has_pending);
  280. for (i = 0; i < 2; i++) {
  281. c.armed = (int)i; memset(&a, 0, sizeof (a));
  282. e.danger = 1; e.state = EMERGENCY; e.floor = 1; s.free = 1000;
  283. p.zid = 0; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  284. p.zid = 9; c.zones[0].policy = PROTECT;
  285. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  286. c.zones[0].policy = ALLOW;
  287. }
  288. puts("PASS TERM/KILL sequencing, emergency grace, floor, cooldown, budgets and no-action protections");
  289. }
  290. static void hard_floor_policy(void)
  291. {
  292. config_t c = allowed();
  293. controller_t a;
  294. engine_t e;
  295. sample_t s = sample(100, 6513, 1000000);
  296. process_t p = process(3000);
  297. delta_t growing = {1, 5, 100}, invalid = {0, 0, 0};
  298. unsigned i;
  299. memset(&a, 0, sizeof (a)); engine_init(&e);
  300. a.nvictims = c.max_floor_victims; s.pageout_ok = 1;
  301. for (i = 0; i < 6; i++) {
  302. s.at = 100 + i;
  303. engine_step(&e, &c, &s, invalid, growing, growing);
  304. CHECK(e.hard_floor_ready == (i == 5));
  305. CHECK((action_plan(&a, &c, &e, &s, &p).action == KILL) == (i == 5));
  306. }
  307. CHECK(!strcmp(action_plan(&a, &c, &e, &s, &p).budget_override, "hard_floor"));
  308. a.nvictims = 16;
  309. CHECK(!strcmp(action_plan(&a, &c, &e, &s, &p).reason, "hard_floor_victim_budget_exhausted"));
  310. a.nvictims = 12; s.at += 2;
  311. engine_step(&e, &c, &s, invalid, growing, growing);
  312. CHECK(!e.hard_floor_ready);
  313. for (i = 0; i < 8; i++) {
  314. s.at++; engine_step(&e, &c, &s, invalid, growing, invalid);
  315. CHECK(!e.hard_floor_ready);
  316. }
  317. s.scan = 0;
  318. for (i = 0; i < 8; i++) {
  319. s.at++; engine_step(&e, &c, &s, invalid, growing, growing);
  320. CHECK(!e.hard_floor_ready);
  321. }
  322. s.free = 70000; s.at++;
  323. engine_step(&e, &c, &s, invalid, invalid, invalid);
  324. CHECK(e.state == RECOVERY);
  325. (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 12);
  326. s.at += 2; s.free = 6513;
  327. engine_step(&e, &c, &s, invalid, invalid, invalid);
  328. CHECK(e.state == EMERGENCY && !e.hard_floor_ready);
  329. (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 12);
  330. engine_gap(&e); s.at++; s.free = s.lots;
  331. (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 12);
  332. s.free = 6513; s.at++; engine_step(&e, &c, &s, invalid, invalid, invalid);
  333. s.free = s.lots;
  334. for (i = 0; i <= c.recovery_s; i++) {
  335. s.at++; engine_step(&e, &c, &s, invalid, invalid, invalid);
  336. (void)action_plan(&a, &c, &e, &s, &p);
  337. CHECK(a.nvictims == (i == c.recovery_s ? 0u : 12u));
  338. }
  339. CHECK(e.state == NORMAL);
  340. puts("PASS hard-floor persistence, finite ceiling, missing corroboration, gap, brief/stable recovery");
  341. }
  342. int main(void)
  343. {
  344. configs(); protections(); histories(); states(); real_incident_fixture();
  345. successful_avoidance_fixture(); actions(); hard_floor_policy();
  346. printf("PASS %u checks; no native backend linked, no signals sent\n", checks);
  347. return 0;
  348. }