Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

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