Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

136 řádky
5.2 KiB

  1. /* Fixed preparation pool. A timed-out worker can never dispatch a signal. */
  2. #include "guard.h"
  3. #include <pthread.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <semaphore.h>
  8. #define VALIDATION_SLOTS 16
  9. enum job_state { FREE, RESERVED, QUEUED, RUNNING, READY, CLAIMED, RELEASE, ABANDONED };
  10. typedef struct {
  11. volatile int state;
  12. sem_t wake;
  13. const config_t *config; /* immutable after startup */
  14. process_t expected;
  15. int real, handle, result, error;
  16. uint64_t ceiling;
  17. validation_t timing;
  18. } validation_job_t;
  19. static validation_job_t jobs[VALIDATION_SLOTS];
  20. static int pool_started;
  21. static int state(validation_job_t *j) { return __sync_fetch_and_add(&j->state, 0); }
  22. static void *prepare_worker(void *arg)
  23. {
  24. validation_job_t *j = arg;
  25. for (;;) {
  26. int wait_rc;
  27. do { wait_rc = sem_wait(&j->wake); } while (wait_rc && errno == EINTR);
  28. if (wait_rc) return NULL;
  29. if (__sync_bool_compare_and_swap(&j->state, QUEUED, RUNNING)) {
  30. double begin = monotime();
  31. j->handle = -1;
  32. memset(&j->timing, 0, sizeof (j->timing));
  33. errno = 0;
  34. j->result = platform_prepare((unsigned)(j - jobs), j->config, &j->expected, j->real,
  35. j->ceiling, &j->handle, &j->timing);
  36. j->error = j->result ? errno : 0;
  37. j->timing.total_ms = 1000 * (monotime() - begin);
  38. if (__sync_bool_compare_and_swap(&j->state, RUNNING, READY))
  39. continue;
  40. /* Timeout won: discard even a successful preparation. */
  41. validation_late_result(j->expected.pid, &j->timing, j->error);
  42. platform_dispose(j->handle);
  43. __sync_lock_test_and_set(&j->state, FREE);
  44. } else if (__sync_bool_compare_and_swap(&j->state, RELEASE, RESERVED) ||
  45. __sync_bool_compare_and_swap(&j->state, ABANDONED, RESERVED)) {
  46. platform_dispose(j->handle);
  47. __sync_lock_test_and_set(&j->state, FREE);
  48. }
  49. }
  50. return NULL;
  51. }
  52. int validation_start(void)
  53. {
  54. unsigned i;
  55. if (pool_started) return 0;
  56. if (platform_validation_init(VALIDATION_SLOTS)) return -1;
  57. for (i = 0; i < VALIDATION_SLOTS; i++) {
  58. pthread_t thread;
  59. jobs[i].handle = -1;
  60. if (sem_init(&jobs[i].wake, 0, 0)) return -1;
  61. if (pthread_create(&thread, NULL, prepare_worker, &jobs[i])) return -1;
  62. (void)pthread_detach(thread);
  63. }
  64. pool_started = 1;
  65. return 0;
  66. }
  67. int validation_acquire(const config_t *c, const process_t *p, int real,
  68. uint64_t ceiling, unsigned budget_ms, int *ticket, validation_t *timing)
  69. {
  70. unsigned i;
  71. double begin = monotime(), deadline = begin + budget_ms / 1000.0;
  72. const struct timespec pause = {0, 250000};
  73. validation_job_t *j = NULL;
  74. memset(timing, 0, sizeof (*timing)); *ticket = -1;
  75. if (!pool_started) { errno = ENXIO; return -1; }
  76. /* Single caller owns request fields. Do not fill the pool with repeated
  77. * attempts on the same stuck PID incarnation after cache refreshes. */
  78. for (i = 0; i < VALIDATION_SLOTS; i++) {
  79. const process_t *old = &jobs[i].expected;
  80. if (state(&jobs[i]) != FREE && old->pid == p->pid &&
  81. old->start_sec == p->start_sec && old->start_nsec == p->start_nsec &&
  82. old->zone_generation == p->zone_generation && !strcmp(old->uuid, p->uuid)) {
  83. errno = EBUSY; return -1;
  84. }
  85. }
  86. for (i = 0; i < VALIDATION_SLOTS; i++)
  87. if (__sync_bool_compare_and_swap(&jobs[i].state, FREE, RESERVED)) {
  88. j = &jobs[i]; break;
  89. }
  90. if (!j) { errno = EBUSY; return -1; }
  91. j->config = c; j->expected = *p; j->real = real; j->ceiling = ceiling;
  92. j->handle = -1;
  93. __sync_lock_test_and_set(&j->state, QUEUED);
  94. (void)sem_post(&j->wake);
  95. for (;;) {
  96. if (monotime() >= deadline) {
  97. int s;
  98. do { s = state(j); }
  99. while (!__sync_bool_compare_and_swap(&j->state, s,
  100. s == READY ? RELEASE : ABANDONED));
  101. if (s == READY) (void)sem_post(&j->wake);
  102. timing->total_ms = 1000 * (monotime() - begin);
  103. errno = ETIMEDOUT; return -1;
  104. }
  105. if (__sync_bool_compare_and_swap(&j->state, READY, CLAIMED)) {
  106. int error = j->error;
  107. *timing = j->timing;
  108. timing->total_ms = 1000 * (monotime() - begin);
  109. if (j->result) {
  110. __sync_lock_test_and_set(&j->state, RELEASE);
  111. (void)sem_post(&j->wake);
  112. errno = error; return -1;
  113. }
  114. /* A delayed scheduler must not turn a late READY into authorization. */
  115. if (monotime() >= deadline) {
  116. __sync_lock_test_and_set(&j->state, RELEASE);
  117. (void)sem_post(&j->wake);
  118. errno = ETIMEDOUT; return -1;
  119. }
  120. *ticket = (int)i; return 0;
  121. }
  122. (void)nanosleep(&pause, NULL);
  123. }
  124. }
  125. int validation_handle(int ticket) { return jobs[ticket].handle; }
  126. void validation_release(int ticket)
  127. {
  128. if (ticket >= 0 && ticket < VALIDATION_SLOTS)
  129. if (__sync_bool_compare_and_swap(&jobs[ticket].state, CLAIMED, RELEASE))
  130. (void)sem_post(&jobs[ticket].wake);
  131. }