Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

106 Zeilen
4.2 KiB

  1. /* Real preparation workers and native dispatch code, mocked I/O only. */
  2. #include "guard.h"
  3. #include <sys/types.h>
  4. #include <time.h>
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. static unsigned checks;
  11. #define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL validation %d: %s\n", __LINE__, #x); exit(1); } checks++; } while (0)
  12. static volatile int disposed, gate;
  13. static int injected_errno, inherited_errno, calls, poison_clock;
  14. static long injected_rc;
  15. double monotime(void)
  16. {
  17. struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t);
  18. if (__sync_fetch_and_add(&poison_clock, 0)) errno = ENOMEM;
  19. return t.tv_sec + t.tv_nsec / 1e9;
  20. }
  21. static void pause_ms(unsigned ms)
  22. {
  23. struct timespec t = {ms / 1000, (long)(ms % 1000) * 1000000};
  24. while (nanosleep(&t, &t) && errno == EINTR) { }
  25. }
  26. int platform_validation_init(unsigned slots) { return slots == 16 ? 0 : -1; }
  27. int platform_prepare(unsigned slot, const config_t *c, const process_t *p,
  28. int real, uint64_t ceiling, int *handle, validation_t *t)
  29. {
  30. double start = monotime();
  31. (void)slot; (void)c; (void)real; (void)ceiling;
  32. if (p->pid == 1) pause_ms(1283);
  33. if (p->pid >= 100) while (__sync_fetch_and_add(&gate, 0)) pause_ms(1);
  34. *handle = p->pid;
  35. t->read_ms = 1000 * (monotime() - start);
  36. errno = EAGAIN; return 0;
  37. }
  38. void platform_dispose(int handle)
  39. { if (handle >= 0) __sync_fetch_and_add(&disposed, 1); }
  40. void validation_late_result(int pid, const validation_t *t, int error)
  41. { (void)pid; (void)t; (void)error; }
  42. static ssize_t injected_write(int fd, const void *buf, size_t n)
  43. {
  44. (void)fd; (void)buf; (void)n;
  45. calls++; errno = injected_errno; return injected_rc;
  46. }
  47. #define write injected_write
  48. #include "../src/signal.c"
  49. #undef write
  50. #include "../src/validation.c"
  51. int main(void)
  52. {
  53. config_t c; process_t p; validation_t t; signal_result_t r;
  54. int ticket, i, j;
  55. double start;
  56. const int previous[] = {EAGAIN, ENOMEM};
  57. const int errors[] = {0, ESRCH, EPERM, EINVAL, EAGAIN};
  58. memset(&c, 0, sizeof (c)); memset(&p, 0, sizeof (p));
  59. CHECK(validation_start() == 0);
  60. p.pid = 1; start = monotime();
  61. CHECK(validation_acquire(&c, &p, 1, 500, 50, &ticket, &t) == -1);
  62. CHECK(errno == ETIMEDOUT && ticket == -1);
  63. CHECK(monotime() - start < 0.150);
  64. CHECK(validation_acquire(&c, &p, 1, 500, 50, &ticket, &t) == -1 && errno == EBUSY);
  65. p.pid = 2;
  66. CHECK(validation_acquire(&c, &p, 1, 500, 50, &ticket, &t) == 0);
  67. CHECK(validation_handle(ticket) == 2 && monotime() - start < 0.200);
  68. validation_release(ticket);
  69. CHECK(calls == 0); /* A validator, including a late one, cannot signal. */
  70. pause_ms(1350);
  71. CHECK(__sync_fetch_and_add(&disposed, 0) == 2 && calls == 0);
  72. __sync_lock_test_and_set(&poison_clock, 1);
  73. for (i = 0; i < 2; i++) for (j = 0; j < 5; j++) {
  74. inherited_errno = previous[i]; injected_errno = errors[j];
  75. injected_rc = injected_errno ? -1 : (long)(2 * sizeof (long));
  76. errno = inherited_errno;
  77. platform_dispatch(10, KILL, &r);
  78. CHECK(r.attempted && r.rc == injected_rc && r.error == injected_errno);
  79. CHECK(r.returned >= r.attempt && r.attempt > 0);
  80. }
  81. injected_rc = 1; injected_errno = EAGAIN;
  82. platform_dispatch(10, KILL, &r);
  83. CHECK(r.rc == 1 && r.error == 0); /* short write is not an errno */
  84. injected_rc = (long)(2 * sizeof (long)); injected_errno = EAGAIN;
  85. platform_dispatch(10, TERM, &r);
  86. CHECK(r.error == 0 && r.rc == injected_rc);
  87. __sync_lock_test_and_set(&poison_clock, 0);
  88. __sync_lock_test_and_set(&gate, 1);
  89. for (i = 0; i < 16; i++) {
  90. p.pid = 100 + i;
  91. CHECK(validation_acquire(&c, &p, 1, 500, 10, &ticket, &t) == -1);
  92. CHECK(errno == ETIMEDOUT);
  93. }
  94. p.pid = 200;
  95. start = monotime();
  96. CHECK(validation_acquire(&c, &p, 1, 500, 50, &ticket, &t) == -1);
  97. CHECK(errno == EBUSY && monotime() - start < 0.050);
  98. __sync_lock_test_and_set(&gate, 0); pause_ms(50);
  99. p.pid = 2;
  100. CHECK(validation_acquire(&c, &p, 1, 500, 50, &ticket, &t) == 0);
  101. validation_release(ticket);
  102. printf("PASS %u validation checks: real 1283ms worker abandoned, fallback, late disposal, pool saturation; actual dispatch code with mocked write; no signals\n", checks);
  103. return 0;
  104. }