2
0

prof_thread_name.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "test/jemalloc_test.h"
  2. #ifdef JEMALLOC_PROF
  3. const char *malloc_conf = "prof:true,prof_active:false";
  4. #endif
  5. static void
  6. mallctl_thread_name_get_impl(const char *thread_name_expected, const char *func,
  7. int line)
  8. {
  9. const char *thread_name_old;
  10. size_t sz;
  11. sz = sizeof(thread_name_old);
  12. assert_d_eq(mallctl("thread.prof.name", &thread_name_old, &sz, NULL, 0),
  13. 0, "%s():%d: Unexpected mallctl failure reading thread.prof.name",
  14. func, line);
  15. assert_str_eq(thread_name_old, thread_name_expected,
  16. "%s():%d: Unexpected thread.prof.name value", func, line);
  17. }
  18. #define mallctl_thread_name_get(a) \
  19. mallctl_thread_name_get_impl(a, __func__, __LINE__)
  20. static void
  21. mallctl_thread_name_set_impl(const char *thread_name, const char *func,
  22. int line)
  23. {
  24. assert_d_eq(mallctl("thread.prof.name", NULL, NULL, &thread_name,
  25. sizeof(thread_name)), 0,
  26. "%s():%d: Unexpected mallctl failure reading thread.prof.name",
  27. func, line);
  28. mallctl_thread_name_get_impl(thread_name, func, line);
  29. }
  30. #define mallctl_thread_name_set(a) \
  31. mallctl_thread_name_set_impl(a, __func__, __LINE__)
  32. TEST_BEGIN(test_prof_thread_name_validation)
  33. {
  34. const char *thread_name;
  35. test_skip_if(!config_prof);
  36. mallctl_thread_name_get("");
  37. mallctl_thread_name_set("hi there");
  38. /* NULL input shouldn't be allowed. */
  39. thread_name = NULL;
  40. assert_d_eq(mallctl("thread.prof.name", NULL, NULL, &thread_name,
  41. sizeof(thread_name)), EFAULT,
  42. "Unexpected mallctl result writing \"%s\" to thread.prof.name",
  43. thread_name);
  44. /* '\n' shouldn't be allowed. */
  45. thread_name = "hi\nthere";
  46. assert_d_eq(mallctl("thread.prof.name", NULL, NULL, &thread_name,
  47. sizeof(thread_name)), EFAULT,
  48. "Unexpected mallctl result writing \"%s\" to thread.prof.name",
  49. thread_name);
  50. /* Simultaneous read/write shouldn't be allowed. */
  51. {
  52. const char *thread_name_old;
  53. size_t sz;
  54. sz = sizeof(thread_name_old);
  55. assert_d_eq(mallctl("thread.prof.name", &thread_name_old, &sz,
  56. &thread_name, sizeof(thread_name)), EPERM,
  57. "Unexpected mallctl result writing \"%s\" to "
  58. "thread.prof.name", thread_name);
  59. }
  60. mallctl_thread_name_set("");
  61. }
  62. TEST_END
  63. #define NTHREADS 4
  64. #define NRESET 25
  65. static void *
  66. thd_start(void *varg)
  67. {
  68. unsigned thd_ind = *(unsigned *)varg;
  69. char thread_name[16] = "";
  70. unsigned i;
  71. malloc_snprintf(thread_name, sizeof(thread_name), "thread %u", thd_ind);
  72. mallctl_thread_name_get("");
  73. mallctl_thread_name_set(thread_name);
  74. for (i = 0; i < NRESET; i++) {
  75. assert_d_eq(mallctl("prof.reset", NULL, NULL, NULL, 0), 0,
  76. "Unexpected error while resetting heap profile data");
  77. mallctl_thread_name_get(thread_name);
  78. }
  79. mallctl_thread_name_set(thread_name);
  80. mallctl_thread_name_set("");
  81. return (NULL);
  82. }
  83. TEST_BEGIN(test_prof_thread_name_threaded)
  84. {
  85. thd_t thds[NTHREADS];
  86. unsigned thd_args[NTHREADS];
  87. unsigned i;
  88. test_skip_if(!config_prof);
  89. for (i = 0; i < NTHREADS; i++) {
  90. thd_args[i] = i;
  91. thd_create(&thds[i], thd_start, (void *)&thd_args[i]);
  92. }
  93. for (i = 0; i < NTHREADS; i++)
  94. thd_join(thds[i], NULL);
  95. }
  96. TEST_END
  97. #undef NTHREADS
  98. #undef NRESET
  99. int
  100. main(void)
  101. {
  102. return (test(
  103. test_prof_thread_name_validation,
  104. test_prof_thread_name_threaded));
  105. }