Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

258 satır
12 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(parse("ACTION_MODE armd\n", &c) != 0);
  49. CHECK(parse("ACTION_MODE armed\n", &c) != 0);
  50. CHECK(parse("ACTION_MODE dry-run\nACTION_MODE armed\n", &c) != 0);
  51. CHECK(parse("LOG_KEEP 0\n", &c) != 0);
  52. CHECK(parse("TERM_GRACE_SECONDS -1\n", &c) != 0);
  53. CHECK(parse("TERM_GRACE_SECONDS 1 garbage\n", &c) != 0);
  54. CHECK(parse("CRITICAL_TERM_GRACE_SECONDS 4\nEMERGENCY_TERM_GRACE_SECONDS 1\n"
  55. "FLOOR_KILL_DELAY_SECONDS 0\nMAX_CACHED_CANDIDATE_AGE_SECONDS 15\n", &c) == 0);
  56. CHECK(parse("MAX_EMERGENCY_VICTIMS_EPISODE 3\n", &c) != 0);
  57. CHECK(parse("MAX_FLOOR_VICTIMS_EPISODE 7\n", &c) != 0);
  58. CHECK(parse("UNKNOWN foo\n", &c) != 0);
  59. CHECK(parse("PRE_SECONDS 20\nPRESSURE_SECONDS 10\n", &c) != 0);
  60. CHECK(parse("zone global global allow\n", &c) != 0);
  61. CHECK(parse("zone 11111111-1111-4111-8111-111111111111 web allow\nACTION_MODE armed\n", &c) == 0 && c.armed);
  62. CHECK(parse("zone 11111111-1111-4111-8111-111111111111 web allow\nzone 11111111-1111-4111-8111-111111111111 db protect\n", &c) != 0);
  63. CHECK(!uuid_valid("11111111-1111-4111-8111-11111111111g"));
  64. CHECK(!uuid_valid("00000000-0000-0000-0000-000000000000"));
  65. {
  66. FILE *f = fopen("etc/o6h-memory-guard.conf", "r"); char error[128];
  67. CHECK(f != NULL); CHECK(config_read(f, &c, error, sizeof (error)) == 0);
  68. fclose(f); CHECK(!c.armed && c.pre_s == 1 && c.pressure_s == 2);
  69. }
  70. puts("PASS strict configuration, safe defaults, explicit UUID authorization");
  71. }
  72. static void protections(void)
  73. {
  74. config_t c = allowed(); process_t p = process(123), q;
  75. CHECK(!protected_process(&c, &p));
  76. q = p; q.zid = 0; CHECK(protected_process(&c, &q));
  77. q = p; q.pid = 1; CHECK(protected_process(&c, &q));
  78. q = p; q.pid = q.initpid; CHECK(protected_process(&c, &q));
  79. q = p; q.initpid = 0; CHECK(protected_process(&c, &q));
  80. q = p; q.zone_generation = 0; CHECK(protected_process(&c, &q));
  81. q = p; q.nlwp = 0; CHECK(protected_process(&c, &q));
  82. q = p; q.ppid = 0; CHECK(protected_process(&c, &q));
  83. q = p; strcpy(q.name, "sshd"); CHECK(protected_process(&c, &q));
  84. q = p; q.rss_kib = 100; CHECK(protected_process(&c, &q));
  85. q = p; q.uuid[0] = '2'; CHECK(protected_process(&c, &q));
  86. c.zones[0].policy = PROTECT; CHECK(protected_process(&c, &p));
  87. c.zones[0].policy = OBSERVE; CHECK(protected_process(&c, &p));
  88. c.zones[0].policy = ALLOW;
  89. strcpy(c.protected_names[c.nnames++], "php-fpm"); CHECK(protected_process(&c, &p));
  90. c.nnames = 0; c.protected_pids[c.npids++] = p.pid; CHECK(protected_process(&c, &p));
  91. q = p; CHECK(same_process(&p, &q));
  92. q.start_nsec++; CHECK(!same_process(&p, &q));
  93. q = p; q.zone_generation++; CHECK(!same_process(&p, &q));
  94. q = p; q.uuid[0] = '2'; CHECK(!same_process(&p, &q));
  95. puts("PASS mandatory protections, PID reuse and zone incarnation identity");
  96. }
  97. static void histories(void)
  98. {
  99. history_t h = {0}; delta_t d; unsigned i;
  100. for (i = 0; i <= 70; i++) history_add(&h, i, 10000 - 10 * i);
  101. d = history_delta(&h, 70, 5); CHECK(d.valid && d.value == -50 && d.age == 5);
  102. d = history_delta(&h, 70, 15); CHECK(d.valid && d.value == -150);
  103. d = history_delta(&h, 70, 60); CHECK(d.valid && d.value == -600);
  104. CHECK(!history_delta(&h, 90, 5).valid);
  105. memset(&h, 0, sizeof (h));
  106. history_add(&h, 0, 123); history_add(&h, 15, 100);
  107. CHECK(!history_delta(&h, 15, 5).valid);
  108. CHECK(history_delta(&h, 15, 15).value == -23);
  109. d.valid = 1; d.value = 100;
  110. CHECK(rank_score(1000, d) == 1200);
  111. CHECK(rank_score(UINT64_MAX - 10, d) == UINT64_MAX);
  112. d.value = -100; CHECK(rank_score(1000, d) == 1000);
  113. {
  114. uint64_t bytes = 0;
  115. CHECK(swap_physical_bytes_from_blocks(67024888, 58931824, &bytes) == 0);
  116. CHECK(bytes == UINT64_C(4143648768));
  117. CHECK(swap_physical_bytes_from_blocks(10, 11, &bytes) != 0);
  118. CHECK(swap_physical_bytes_from_blocks(UINT64_MAX, 0, &bytes) != 0);
  119. }
  120. puts("PASS timestamped 5/15/60s windows, missing data, signed growth, overflow");
  121. }
  122. static void states(void)
  123. {
  124. config_t c = allowed(); engine_t e; unsigned i;
  125. engine_init(&e);
  126. for (i = 0; i < 10; i++) step(&e, &c, i, 425950, 0);
  127. CHECK(e.state == NORMAL && !e.danger);
  128. step(&e, &c, 10, 100000, 0); CHECK(e.state == NORMAL);
  129. step(&e, &c, 11, 100000, 0); CHECK(e.state == PRE_PRESSURE);
  130. step(&e, &c, 12, 100000, 0);
  131. CHECK(e.state == PRESSURE && !e.danger);
  132. step(&e, &c, 21, 60000, 1); step(&e, &c, 22, 59000, 1);
  133. CHECK(!e.danger);
  134. step(&e, &c, 23, 58000, 1); CHECK(e.state == CRITICAL && e.danger);
  135. step(&e, &c, 24, 140000, 0); CHECK(e.state == RECOVERY && !e.danger);
  136. for (i = 25; i <= 39; i++) step(&e, &c, i, 140000, 0);
  137. CHECK(e.state == NORMAL);
  138. for (i = 40; i <= 56; i++) step(&e, &c, i, 150000, 0);
  139. CHECK(e.state == NORMAL);
  140. step(&e, &c, 57, 40000, 0); CHECK(e.state == EMERGENCY && e.danger && !e.floor);
  141. step(&e, &c, 58, 20000, 0); CHECK(e.floor);
  142. step(&e, &c, 59, 70000, 0); CHECK(e.state == RECOVERY && !e.danger);
  143. engine_init(&e);
  144. for (i = 0; i < 20; i++) step(&e, &c, i, 60000, 0);
  145. CHECK(!e.danger); /* low but stable, no scanner */
  146. engine_init(&e);
  147. step(&e, &c, 0, 60000, 1); step(&e, &c, 20, 59000, 1);
  148. CHECK(!e.danger); /* gaps do not count as sustained samples */
  149. engine_gap(&e); step(&e, &c, 21, 58000, 1); CHECK(!e.danger);
  150. step(&e, &c, 22, 57000, 1); step(&e, &c, 23, 56000, 1); CHECK(e.danger);
  151. {
  152. sample_t s = sample(1, 60000, 0);
  153. delta_t none = {0, 0, 0}, rising = {1, 5, 4096};
  154. engine_init(&e); engine_step(&e, &c, &s, none, rising, none);
  155. CHECK(!e.danger && e.critical_since < 0); /* physical swap alone is inert */
  156. engine_init(&e); engine_step(&e, &c, &s, none, none, rising);
  157. CHECK(!e.danger && e.critical_since < 0); /* pageout alone is inert */
  158. s.free = 150000;
  159. engine_init(&e); engine_step(&e, &c, &s, none, rising, rising);
  160. CHECK(e.state == NORMAL && e.low_since < 0); /* corroboration needs low memory */
  161. s.free = 60000;
  162. engine_init(&e);
  163. for (i = 0; i <= 3; i++) {
  164. s.at = i; engine_step(&e, &c, &s, none, rising, rising);
  165. }
  166. CHECK(e.state == CRITICAL && e.danger);
  167. }
  168. puts("PASS all six states, corroboration, hysteresis, recovery and sampling gaps");
  169. }
  170. static void real_incident_fixture(void)
  171. {
  172. config_t c = allowed(); engine_t e; FILE *f;
  173. unsigned t, rows = 0; uint64_t free_pages, nscan;
  174. char line[128];
  175. engine_init(&e);
  176. f = fopen("tests/fixtures/pageout_deadman_real_incident_2026_09.csv", "r");
  177. CHECK(f != NULL); CHECK(fgets(line, sizeof (line), f) != NULL);
  178. while (fscanf(f, "%u,%" SCNu64 ",%" SCNu64, &t, &free_pages, &nscan) == 3) {
  179. step(&e, &c, t, free_pages, nscan); rows++;
  180. if (t == 2) CHECK(e.state == PRE_PRESSURE);
  181. if (t == 3) CHECK(e.state == PRESSURE);
  182. }
  183. fclose(f);
  184. CHECK(rows == 5); CHECK(e.state == EMERGENCY && e.danger);
  185. puts("PASS pageout_deadman_real_incident_2026_09 exits NORMAL before minfree");
  186. }
  187. static void actions(void)
  188. {
  189. config_t c = allowed(); engine_t e; controller_t a = {0};
  190. process_t p = process(123); sample_t s; plan_t plan; unsigned i;
  191. engine_init(&e); e.state = CRITICAL; e.danger = 1;
  192. s = sample(100, 60000, 1);
  193. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
  194. action_commit(&a, &c, &s, &plan); CHECK(a.has_pending && a.nvictims == 1);
  195. printf("SIMULATED t=100 state=CRITICAL UUID=%s pid=123 WOULD_ACTION=SIGTERM\n", uuid);
  196. s.at = 104; plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == NONE);
  197. s.at = 105; plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  198. action_commit(&a, &c, &s, &plan); CHECK(!a.has_pending && a.nvictims == 1);
  199. puts("SIMULATED t=105 state=CRITICAL pid=123 WOULD_ACTION=SIGKILL reason=memory_not_recovered");
  200. p.pid++; s.at = 106; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  201. e.state = EMERGENCY; s.free = 40000;
  202. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
  203. action_commit(&a, &c, &s, &plan);
  204. s.at = 106.5; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  205. s.at = 107; CHECK(action_plan(&a, &c, &e, &s, &p).action == KILL);
  206. s.free = 20000; e.floor = 1;
  207. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  208. action_commit(&a, &c, &s, &plan);
  209. p.pid++; s.at++;
  210. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  211. action_commit(&a, &c, &s, &plan);
  212. puts("SIMULATED state=EMERGENCY floor=1 WOULD_ACTION=SIGKILL reason=floor");
  213. p.pid++; s.at++;
  214. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  215. action_commit(&a, &c, &s, &plan);
  216. p.pid++; s.at++;
  217. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  218. CHECK(!strcmp(plan.budget_override, "floor"));
  219. e.state = NORMAL; e.danger = 0; s.free = 160000;
  220. (void)action_plan(&a, &c, &e, &s, &p); CHECK(a.nvictims == 0);
  221. e.state = EMERGENCY; e.danger = 1; e.floor = 0; s.free = 40000;
  222. a.nvictims = c.max_victims; a.nrate = c.max_per_minute;
  223. e.state = CRITICAL;
  224. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  225. e.state = EMERGENCY;
  226. a.nvictims = c.max_victims; a.nrate = c.max_per_minute;
  227. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == TERM);
  228. CHECK(!strcmp(plan.budget_override, "emergency"));
  229. a.nvictims = c.max_emergency_victims;
  230. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  231. e.floor = 1; s.free = 20000;
  232. plan = action_plan(&a, &c, &e, &s, &p); CHECK(plan.action == KILL);
  233. CHECK(!strcmp(plan.budget_override, "floor"));
  234. a.nvictims = c.max_floor_victims;
  235. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  236. memset(&a, 0, sizeof (a)); e.floor = 0; e.state = CRITICAL; s.free = 60000;
  237. plan = action_plan(&a, &c, &e, &s, &p); action_commit(&a, &c, &s, &plan);
  238. s.free = 140000; e.danger = 0; e.state = RECOVERY;
  239. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE && !a.has_pending);
  240. for (i = 0; i < 2; i++) {
  241. c.armed = (int)i; memset(&a, 0, sizeof (a));
  242. e.danger = 1; e.state = EMERGENCY; e.floor = 1; s.free = 1000;
  243. p.zid = 0; CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  244. p.zid = 9; c.zones[0].policy = PROTECT;
  245. CHECK(action_plan(&a, &c, &e, &s, &p).action == NONE);
  246. c.zones[0].policy = ALLOW;
  247. }
  248. puts("PASS TERM/KILL sequencing, emergency grace, floor, cooldown, budgets and no-action protections");
  249. }
  250. int main(void)
  251. {
  252. configs(); protections(); histories(); states(); real_incident_fixture(); actions();
  253. printf("PASS %u checks; no native backend linked, no signals sent\n", checks);
  254. return 0;
  255. }