THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
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.

181 lines
4.0KB

  1. #include "cache.h"
  2. #include "thread-utils.h"
  3. #include "trace2/tr2_tls.h"
  4. /*
  5. * Initialize size of the thread stack for nested regions.
  6. * This is used to store nested region start times. Note that
  7. * this stack is per-thread and not per-trace-key.
  8. */
  9. #define TR2_REGION_NESTING_INITIAL_SIZE (100)
  10. static struct tr2tls_thread_ctx *tr2tls_thread_main;
  11. static uint64_t tr2tls_us_start_process;
  12. static pthread_mutex_t tr2tls_mutex;
  13. static pthread_key_t tr2tls_key;
  14. static int tr2_next_thread_id; /* modify under lock */
  15. void tr2tls_start_process_clock(void)
  16. {
  17. if (tr2tls_us_start_process)
  18. return;
  19. /*
  20. * Keep the absolute start time of the process (i.e. the main
  21. * process) in a fixed variable since other threads need to
  22. * access it. This allows them to do that without a lock on
  23. * main thread's array data (because of reallocs).
  24. */
  25. tr2tls_us_start_process = getnanotime() / 1000;
  26. }
  27. struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_name,
  28. uint64_t us_thread_start)
  29. {
  30. struct tr2tls_thread_ctx *ctx = xcalloc(1, sizeof(*ctx));
  31. /*
  32. * Implicitly "tr2tls_push_self()" to capture the thread's start
  33. * time in array_us_start[0]. For the main thread this gives us the
  34. * application run time.
  35. */
  36. ctx->alloc = TR2_REGION_NESTING_INITIAL_SIZE;
  37. ctx->array_us_start = (uint64_t *)xcalloc(ctx->alloc, sizeof(uint64_t));
  38. ctx->array_us_start[ctx->nr_open_regions++] = us_thread_start;
  39. ctx->thread_id = tr2tls_locked_increment(&tr2_next_thread_id);
  40. strbuf_init(&ctx->thread_name, 0);
  41. if (ctx->thread_id)
  42. strbuf_addf(&ctx->thread_name, "th%02d:", ctx->thread_id);
  43. strbuf_addstr(&ctx->thread_name, thread_name);
  44. if (ctx->thread_name.len > TR2_MAX_THREAD_NAME)
  45. strbuf_setlen(&ctx->thread_name, TR2_MAX_THREAD_NAME);
  46. pthread_setspecific(tr2tls_key, ctx);
  47. return ctx;
  48. }
  49. struct tr2tls_thread_ctx *tr2tls_get_self(void)
  50. {
  51. struct tr2tls_thread_ctx *ctx;
  52. if (!HAVE_THREADS)
  53. return tr2tls_thread_main;
  54. ctx = pthread_getspecific(tr2tls_key);
  55. /*
  56. * If the thread-proc did not call trace2_thread_start(), we won't
  57. * have any TLS data associated with the current thread. Fix it
  58. * here and silently continue.
  59. */
  60. if (!ctx)
  61. ctx = tr2tls_create_self("unknown", getnanotime() / 1000);
  62. return ctx;
  63. }
  64. int tr2tls_is_main_thread(void)
  65. {
  66. if (!HAVE_THREADS)
  67. return 1;
  68. return pthread_getspecific(tr2tls_key) == tr2tls_thread_main;
  69. }
  70. void tr2tls_unset_self(void)
  71. {
  72. struct tr2tls_thread_ctx *ctx;
  73. ctx = tr2tls_get_self();
  74. pthread_setspecific(tr2tls_key, NULL);
  75. free(ctx->array_us_start);
  76. free(ctx);
  77. }
  78. void tr2tls_push_self(uint64_t us_now)
  79. {
  80. struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
  81. ALLOC_GROW(ctx->array_us_start, ctx->nr_open_regions + 1, ctx->alloc);
  82. ctx->array_us_start[ctx->nr_open_regions++] = us_now;
  83. }
  84. void tr2tls_pop_self(void)
  85. {
  86. struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
  87. if (!ctx->nr_open_regions)
  88. BUG("no open regions in thread '%s'", ctx->thread_name.buf);
  89. ctx->nr_open_regions--;
  90. }
  91. void tr2tls_pop_unwind_self(void)
  92. {
  93. struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
  94. while (ctx->nr_open_regions > 1)
  95. tr2tls_pop_self();
  96. }
  97. uint64_t tr2tls_region_elasped_self(uint64_t us)
  98. {
  99. struct tr2tls_thread_ctx *ctx;
  100. uint64_t us_start;
  101. ctx = tr2tls_get_self();
  102. if (!ctx->nr_open_regions)
  103. return 0;
  104. us_start = ctx->array_us_start[ctx->nr_open_regions - 1];
  105. return us - us_start;
  106. }
  107. uint64_t tr2tls_absolute_elapsed(uint64_t us)
  108. {
  109. if (!tr2tls_thread_main)
  110. return 0;
  111. return us - tr2tls_us_start_process;
  112. }
  113. void tr2tls_init(void)
  114. {
  115. tr2tls_start_process_clock();
  116. pthread_key_create(&tr2tls_key, NULL);
  117. init_recursive_mutex(&tr2tls_mutex);
  118. tr2tls_thread_main =
  119. tr2tls_create_self("main", tr2tls_us_start_process);
  120. }
  121. void tr2tls_release(void)
  122. {
  123. tr2tls_unset_self();
  124. tr2tls_thread_main = NULL;
  125. pthread_mutex_destroy(&tr2tls_mutex);
  126. pthread_key_delete(tr2tls_key);
  127. }
  128. int tr2tls_locked_increment(int *p)
  129. {
  130. int current_value;
  131. pthread_mutex_lock(&tr2tls_mutex);
  132. current_value = *p;
  133. *p = current_value + 1;
  134. pthread_mutex_unlock(&tr2tls_mutex);
  135. return current_value;
  136. }