Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

102 lines
3.7 KiB

  1. #ifndef O6H_GUARD_H
  2. #define O6H_GUARD_H
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #include <stdio.h>
  6. #define HIST 64
  7. #define MAX_ZONES 256
  8. #define MAX_PROCS 4096
  9. #define MAX_RULES 128
  10. #define MAX_VICTIMS 32
  11. #define MAX_SWAP_DEVICES 64
  12. #define CONF_PATH "/opt/custom/etc/o6h-memory-guard.conf"
  13. #define LOG_DIR "/opt/custom/var/log/o6h-memory-guard"
  14. #define RUN_DIR "/opt/custom/var/run"
  15. #define VERSION "1.0.4"
  16. enum state { NORMAL, PRE_PRESSURE, PRESSURE, CRITICAL, EMERGENCY, RECOVERY };
  17. enum policy { OBSERVE, ALLOW, PROTECT };
  18. enum action { NONE, TERM, KILL };
  19. typedef struct { double at; uint64_t value; } point_t;
  20. typedef struct { point_t p[HIST]; unsigned next, count; } history_t;
  21. typedef struct { int valid; double age; int64_t value; } delta_t;
  22. typedef struct {
  23. double at;
  24. uint64_t free, lots, des, min, scan, kernel, locked;
  25. uint64_t arc, swap_alloc, swap_reserved, swap_available;
  26. uint64_t swap_physical_used_bytes, anon_pageout_pages;
  27. int arc_ok, swap_virtual_ok, swap_physical_ok, pageout_ok;
  28. } sample_t;
  29. typedef struct { char uuid[37], alias[64]; enum policy policy; } zone_rule_t;
  30. typedef struct {
  31. int armed;
  32. unsigned pre_s, pressure_s, critical_s, recovery_s, critical_term_s;
  33. unsigned emergency_term_s, floor_kill_delay_s, max_cached_candidate_age_s;
  34. unsigned normal_scan_s, scan_budget_ms, cooldown_s, max_victims;
  35. unsigned max_emergency_victims, max_floor_victims;
  36. unsigned max_per_minute, log_mib, log_keep;
  37. uint64_t min_rss_kib;
  38. zone_rule_t zones[MAX_ZONES]; size_t nzones;
  39. char protected_names[MAX_RULES][32]; size_t nnames;
  40. int protected_pids[MAX_RULES]; size_t npids;
  41. } config_t;
  42. typedef struct {
  43. int pid, ppid, zid, initpid, nlwp;
  44. uint32_t uid, euid;
  45. int64_t start_sec, start_nsec;
  46. uint64_t zone_generation, rss_kib;
  47. char uuid[64], name[32];
  48. } process_t;
  49. typedef struct {
  50. enum state state;
  51. double low_since, critical_since, recovery_since, last_at;
  52. uint64_t last_free;
  53. unsigned low_samples;
  54. int have_last, danger, floor;
  55. } engine_t;
  56. typedef struct {
  57. process_t pending;
  58. int has_pending;
  59. double term_at, next_at;
  60. process_t victims[MAX_VICTIMS]; unsigned nvictims;
  61. double rate[64]; unsigned nrate;
  62. } controller_t;
  63. typedef struct {
  64. enum action action;
  65. process_t target;
  66. const char *reason;
  67. const char *budget_override;
  68. } plan_t;
  69. void defaults(config_t *);
  70. int config_read(FILE *, config_t *, char *, size_t);
  71. int uuid_valid(const char *);
  72. const zone_rule_t *zone_rule(const config_t *, const char *);
  73. int protected_process(const config_t *, const process_t *);
  74. int same_process(const process_t *, const process_t *);
  75. void history_add(history_t *, double, uint64_t);
  76. delta_t history_delta(const history_t *, double, unsigned);
  77. uint64_t rank_score(uint64_t, delta_t);
  78. int swap_physical_bytes_from_blocks(uint64_t, uint64_t, uint64_t *);
  79. const char *state_name(enum state);
  80. void engine_init(engine_t *);
  81. void engine_gap(engine_t *);
  82. void engine_step(engine_t *, const config_t *, const sample_t *, delta_t,
  83. delta_t, delta_t);
  84. int was_victim(const controller_t *, const process_t *);
  85. plan_t action_plan(controller_t *, const config_t *, const engine_t *,
  86. const sample_t *, const process_t *);
  87. void action_commit(controller_t *, const config_t *, const sample_t *, const plan_t *);
  88. /* Native backend. No backend is linked into the portable policy tests. */
  89. int platform_init(void);
  90. void platform_fini(void);
  91. int platform_sample(sample_t *);
  92. int platform_walk(int (*)(const process_t *, void *), void *, unsigned);
  93. int platform_signal(const config_t *, const process_t *, enum action, int, uint64_t);
  94. double monotime(void);
  95. int secure_file(const char *, int, unsigned);
  96. int secure_directory(const char *);
  97. #endif