thread_event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "jemalloc/internal/jemalloc_preamble.h"
  2. #include "jemalloc/internal/jemalloc_internal_includes.h"
  3. #include "jemalloc/internal/thread_event.h"
  4. /*
  5. * Signatures for event specific functions. These functions should be defined
  6. * by the modules owning each event. The signatures here verify that the
  7. * definitions follow the right format.
  8. *
  9. * The first two are functions computing new / postponed event wait time. New
  10. * event wait time is the time till the next event if an event is currently
  11. * being triggered; postponed event wait time is the time till the next event
  12. * if an event should be triggered but needs to be postponed, e.g. when the TSD
  13. * is not nominal or during reentrancy.
  14. *
  15. * The third is the event handler function, which is called whenever an event
  16. * is triggered. The parameter is the elapsed time since the last time an
  17. * event of the same type was triggered.
  18. */
  19. #define E(event, condition_unused, is_alloc_event_unused) \
  20. uint64_t event##_new_event_wait(tsd_t *tsd); \
  21. uint64_t event##_postponed_event_wait(tsd_t *tsd); \
  22. void event##_event_handler(tsd_t *tsd, uint64_t elapsed);
  23. ITERATE_OVER_ALL_EVENTS
  24. #undef E
  25. /* Signatures for internal functions fetching elapsed time. */
  26. #define E(event, condition_unused, is_alloc_event_unused) \
  27. static uint64_t event##_fetch_elapsed(tsd_t *tsd);
  28. ITERATE_OVER_ALL_EVENTS
  29. #undef E
  30. static uint64_t
  31. tcache_gc_fetch_elapsed(tsd_t *tsd) {
  32. return TE_INVALID_ELAPSED;
  33. }
  34. static uint64_t
  35. tcache_gc_dalloc_fetch_elapsed(tsd_t *tsd) {
  36. return TE_INVALID_ELAPSED;
  37. }
  38. static uint64_t
  39. prof_sample_fetch_elapsed(tsd_t *tsd) {
  40. uint64_t last_event = thread_allocated_last_event_get(tsd);
  41. uint64_t last_sample_event = prof_sample_last_event_get(tsd);
  42. prof_sample_last_event_set(tsd, last_event);
  43. return last_event - last_sample_event;
  44. }
  45. static uint64_t
  46. stats_interval_fetch_elapsed(tsd_t *tsd) {
  47. uint64_t last_event = thread_allocated_last_event_get(tsd);
  48. uint64_t last_stats_event = stats_interval_last_event_get(tsd);
  49. stats_interval_last_event_set(tsd, last_event);
  50. return last_event - last_stats_event;
  51. }
  52. static uint64_t
  53. peak_alloc_fetch_elapsed(tsd_t *tsd) {
  54. return TE_INVALID_ELAPSED;
  55. }
  56. static uint64_t
  57. peak_dalloc_fetch_elapsed(tsd_t *tsd) {
  58. return TE_INVALID_ELAPSED;
  59. }
  60. /* Per event facilities done. */
  61. static bool
  62. te_ctx_has_active_events(te_ctx_t *ctx) {
  63. assert(config_debug);
  64. #define E(event, condition, alloc_event) \
  65. if (condition && alloc_event == ctx->is_alloc) { \
  66. return true; \
  67. }
  68. ITERATE_OVER_ALL_EVENTS
  69. #undef E
  70. return false;
  71. }
  72. static uint64_t
  73. te_next_event_compute(tsd_t *tsd, bool is_alloc) {
  74. uint64_t wait = TE_MAX_START_WAIT;
  75. #define E(event, condition, alloc_event) \
  76. if (is_alloc == alloc_event && condition) { \
  77. uint64_t event_wait = \
  78. event##_event_wait_get(tsd); \
  79. assert(event_wait <= TE_MAX_START_WAIT); \
  80. if (event_wait > 0U && event_wait < wait) { \
  81. wait = event_wait; \
  82. } \
  83. }
  84. ITERATE_OVER_ALL_EVENTS
  85. #undef E
  86. assert(wait <= TE_MAX_START_WAIT);
  87. return wait;
  88. }
  89. static void
  90. te_assert_invariants_impl(tsd_t *tsd, te_ctx_t *ctx) {
  91. uint64_t current_bytes = te_ctx_current_bytes_get(ctx);
  92. uint64_t last_event = te_ctx_last_event_get(ctx);
  93. uint64_t next_event = te_ctx_next_event_get(ctx);
  94. uint64_t next_event_fast = te_ctx_next_event_fast_get(ctx);
  95. assert(last_event != next_event);
  96. if (next_event > TE_NEXT_EVENT_FAST_MAX || !tsd_fast(tsd)) {
  97. assert(next_event_fast == 0U);
  98. } else {
  99. assert(next_event_fast == next_event);
  100. }
  101. /* The subtraction is intentionally susceptible to underflow. */
  102. uint64_t interval = next_event - last_event;
  103. /* The subtraction is intentionally susceptible to underflow. */
  104. assert(current_bytes - last_event < interval);
  105. uint64_t min_wait = te_next_event_compute(tsd, te_ctx_is_alloc(ctx));
  106. /*
  107. * next_event should have been pushed up only except when no event is
  108. * on and the TSD is just initialized. The last_event == 0U guard
  109. * below is stronger than needed, but having an exactly accurate guard
  110. * is more complicated to implement.
  111. */
  112. assert((!te_ctx_has_active_events(ctx) && last_event == 0U) ||
  113. interval == min_wait ||
  114. (interval < min_wait && interval == TE_MAX_INTERVAL));
  115. }
  116. void
  117. te_assert_invariants_debug(tsd_t *tsd) {
  118. te_ctx_t ctx;
  119. te_ctx_get(tsd, &ctx, true);
  120. te_assert_invariants_impl(tsd, &ctx);
  121. te_ctx_get(tsd, &ctx, false);
  122. te_assert_invariants_impl(tsd, &ctx);
  123. }
  124. /*
  125. * Synchronization around the fast threshold in tsd --
  126. * There are two threads to consider in the synchronization here:
  127. * - The owner of the tsd being updated by a slow path change
  128. * - The remote thread, doing that slow path change.
  129. *
  130. * As a design constraint, we want to ensure that a slow-path transition cannot
  131. * be ignored for arbitrarily long, and that if the remote thread causes a
  132. * slow-path transition and then communicates with the owner thread that it has
  133. * occurred, then the owner will go down the slow path on the next allocator
  134. * operation (so that we don't want to just wait until the owner hits its slow
  135. * path reset condition on its own).
  136. *
  137. * Here's our strategy to do that:
  138. *
  139. * The remote thread will update the slow-path stores to TSD variables, issue a
  140. * SEQ_CST fence, and then update the TSD next_event_fast counter. The owner
  141. * thread will update next_event_fast, issue an SEQ_CST fence, and then check
  142. * its TSD to see if it's on the slow path.
  143. * This is fairly straightforward when 64-bit atomics are supported. Assume that
  144. * the remote fence is sandwiched between two owner fences in the reset pathway.
  145. * The case where there is no preceding or trailing owner fence (i.e. because
  146. * the owner thread is near the beginning or end of its life) can be analyzed
  147. * similarly. The owner store to next_event_fast preceding the earlier owner
  148. * fence will be earlier in coherence order than the remote store to it, so that
  149. * the owner thread will go down the slow path once the store becomes visible to
  150. * it, which is no later than the time of the second fence.
  151. * The case where we don't support 64-bit atomics is trickier, since word
  152. * tearing is possible. We'll repeat the same analysis, and look at the two
  153. * owner fences sandwiching the remote fence. The next_event_fast stores done
  154. * alongside the earlier owner fence cannot overwrite any of the remote stores
  155. * (since they precede the earlier owner fence in sb, which precedes the remote
  156. * fence in sc, which precedes the remote stores in sb). After the second owner
  157. * fence there will be a re-check of the slow-path variables anyways, so the
  158. * "owner will notice that it's on the slow path eventually" guarantee is
  159. * satisfied. To make sure that the out-of-band-messaging constraint is as well,
  160. * note that either the message passing is sequenced before the second owner
  161. * fence (in which case the remote stores happen before the second set of owner
  162. * stores, so malloc sees a value of zero for next_event_fast and goes down the
  163. * slow path), or it is not (in which case the owner sees the tsd slow-path
  164. * writes on its previous update). This leaves open the possibility that the
  165. * remote thread will (at some arbitrary point in the future) zero out one half
  166. * of the owner thread's next_event_fast, but that's always safe (it just sends
  167. * it down the slow path earlier).
  168. */
  169. static void
  170. te_ctx_next_event_fast_update(te_ctx_t *ctx) {
  171. uint64_t next_event = te_ctx_next_event_get(ctx);
  172. uint64_t next_event_fast = (next_event <= TE_NEXT_EVENT_FAST_MAX) ?
  173. next_event : 0U;
  174. te_ctx_next_event_fast_set(ctx, next_event_fast);
  175. }
  176. void
  177. te_recompute_fast_threshold(tsd_t *tsd) {
  178. if (tsd_state_get(tsd) != tsd_state_nominal) {
  179. /* Check first because this is also called on purgatory. */
  180. te_next_event_fast_set_non_nominal(tsd);
  181. return;
  182. }
  183. te_ctx_t ctx;
  184. te_ctx_get(tsd, &ctx, true);
  185. te_ctx_next_event_fast_update(&ctx);
  186. te_ctx_get(tsd, &ctx, false);
  187. te_ctx_next_event_fast_update(&ctx);
  188. atomic_fence(ATOMIC_SEQ_CST);
  189. if (tsd_state_get(tsd) != tsd_state_nominal) {
  190. te_next_event_fast_set_non_nominal(tsd);
  191. }
  192. }
  193. static void
  194. te_adjust_thresholds_helper(tsd_t *tsd, te_ctx_t *ctx,
  195. uint64_t wait) {
  196. /*
  197. * The next threshold based on future events can only be adjusted after
  198. * progressing the last_event counter (which is set to current).
  199. */
  200. assert(te_ctx_current_bytes_get(ctx) == te_ctx_last_event_get(ctx));
  201. assert(wait <= TE_MAX_START_WAIT);
  202. uint64_t next_event = te_ctx_last_event_get(ctx) + (wait <=
  203. TE_MAX_INTERVAL ? wait : TE_MAX_INTERVAL);
  204. te_ctx_next_event_set(tsd, ctx, next_event);
  205. }
  206. static uint64_t
  207. te_clip_event_wait(uint64_t event_wait) {
  208. assert(event_wait > 0U);
  209. if (TE_MIN_START_WAIT > 1U &&
  210. unlikely(event_wait < TE_MIN_START_WAIT)) {
  211. event_wait = TE_MIN_START_WAIT;
  212. }
  213. if (TE_MAX_START_WAIT < UINT64_MAX &&
  214. unlikely(event_wait > TE_MAX_START_WAIT)) {
  215. event_wait = TE_MAX_START_WAIT;
  216. }
  217. return event_wait;
  218. }
  219. void
  220. te_event_trigger(tsd_t *tsd, te_ctx_t *ctx) {
  221. /* usize has already been added to thread_allocated. */
  222. uint64_t bytes_after = te_ctx_current_bytes_get(ctx);
  223. /* The subtraction is intentionally susceptible to underflow. */
  224. uint64_t accumbytes = bytes_after - te_ctx_last_event_get(ctx);
  225. te_ctx_last_event_set(ctx, bytes_after);
  226. bool allow_event_trigger = tsd_nominal(tsd) &&
  227. tsd_reentrancy_level_get(tsd) == 0;
  228. bool is_alloc = ctx->is_alloc;
  229. uint64_t wait = TE_MAX_START_WAIT;
  230. #define E(event, condition, alloc_event) \
  231. bool is_##event##_triggered = false; \
  232. if (is_alloc == alloc_event && condition) { \
  233. uint64_t event_wait = event##_event_wait_get(tsd); \
  234. assert(event_wait <= TE_MAX_START_WAIT); \
  235. if (event_wait > accumbytes) { \
  236. event_wait -= accumbytes; \
  237. } else if (!allow_event_trigger) { \
  238. event_wait = event##_postponed_event_wait(tsd); \
  239. } else { \
  240. is_##event##_triggered = true; \
  241. event_wait = event##_new_event_wait(tsd); \
  242. } \
  243. event_wait = te_clip_event_wait(event_wait); \
  244. event##_event_wait_set(tsd, event_wait); \
  245. if (event_wait < wait) { \
  246. wait = event_wait; \
  247. } \
  248. }
  249. ITERATE_OVER_ALL_EVENTS
  250. #undef E
  251. assert(wait <= TE_MAX_START_WAIT);
  252. te_adjust_thresholds_helper(tsd, ctx, wait);
  253. te_assert_invariants(tsd);
  254. #define E(event, condition, alloc_event) \
  255. if (is_alloc == alloc_event && condition && \
  256. is_##event##_triggered) { \
  257. assert(allow_event_trigger); \
  258. uint64_t elapsed = event##_fetch_elapsed(tsd); \
  259. event##_event_handler(tsd, elapsed); \
  260. }
  261. ITERATE_OVER_ALL_EVENTS
  262. #undef E
  263. te_assert_invariants(tsd);
  264. }
  265. static void
  266. te_init(tsd_t *tsd, bool is_alloc) {
  267. te_ctx_t ctx;
  268. te_ctx_get(tsd, &ctx, is_alloc);
  269. /*
  270. * Reset the last event to current, which starts the events from a clean
  271. * state. This is necessary when re-init the tsd event counters.
  272. *
  273. * The event counters maintain a relationship with the current bytes:
  274. * last_event <= current < next_event. When a reinit happens (e.g.
  275. * reincarnated tsd), the last event needs progressing because all
  276. * events start fresh from the current bytes.
  277. */
  278. te_ctx_last_event_set(&ctx, te_ctx_current_bytes_get(&ctx));
  279. uint64_t wait = TE_MAX_START_WAIT;
  280. #define E(event, condition, alloc_event) \
  281. if (is_alloc == alloc_event && condition) { \
  282. uint64_t event_wait = event##_new_event_wait(tsd); \
  283. event_wait = te_clip_event_wait(event_wait); \
  284. event##_event_wait_set(tsd, event_wait); \
  285. if (event_wait < wait) { \
  286. wait = event_wait; \
  287. } \
  288. }
  289. ITERATE_OVER_ALL_EVENTS
  290. #undef E
  291. te_adjust_thresholds_helper(tsd, &ctx, wait);
  292. }
  293. void
  294. tsd_te_init(tsd_t *tsd) {
  295. /* Make sure no overflow for the bytes accumulated on event_trigger. */
  296. assert(TE_MAX_INTERVAL <= UINT64_MAX - SC_LARGE_MAXCLASS + 1);
  297. te_init(tsd, true);
  298. te_init(tsd, false);
  299. te_assert_invariants(tsd);
  300. }