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.
 
 
 
 

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