prof_sys_thread_name.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/prof_sys.h"
  3. static const char *test_thread_name = "test_name";
  4. static int
  5. test_prof_sys_thread_name_read_error(char *buf, size_t limit) {
  6. return ENOSYS;
  7. }
  8. static int
  9. test_prof_sys_thread_name_read(char *buf, size_t limit) {
  10. assert(strlen(test_thread_name) < limit);
  11. strncpy(buf, test_thread_name, limit);
  12. return 0;
  13. }
  14. static int
  15. test_prof_sys_thread_name_read_clear(char *buf, size_t limit) {
  16. assert(limit > 0);
  17. buf[0] = '\0';
  18. return 0;
  19. }
  20. TEST_BEGIN(test_prof_sys_thread_name) {
  21. test_skip_if(!config_prof);
  22. bool oldval;
  23. size_t sz = sizeof(oldval);
  24. assert_d_eq(mallctl("opt.prof_sys_thread_name", &oldval, &sz, NULL, 0),
  25. 0, "mallctl failed");
  26. assert_true(oldval, "option was not set correctly");
  27. const char *thread_name;
  28. sz = sizeof(thread_name);
  29. assert_d_eq(mallctl("thread.prof.name", &thread_name, &sz, NULL, 0), 0,
  30. "mallctl read for thread name should not fail");
  31. expect_str_eq(thread_name, "", "Initial thread name should be empty");
  32. thread_name = test_thread_name;
  33. assert_d_eq(mallctl("thread.prof.name", NULL, NULL, &thread_name, sz),
  34. ENOENT, "mallctl write for thread name should fail");
  35. assert_ptr_eq(thread_name, test_thread_name,
  36. "Thread name should not be touched");
  37. prof_sys_thread_name_read = test_prof_sys_thread_name_read_error;
  38. void *p = malloc(1);
  39. free(p);
  40. assert_d_eq(mallctl("thread.prof.name", &thread_name, &sz, NULL, 0), 0,
  41. "mallctl read for thread name should not fail");
  42. assert_str_eq(thread_name, "",
  43. "Thread name should stay the same if the system call fails");
  44. prof_sys_thread_name_read = test_prof_sys_thread_name_read;
  45. p = malloc(1);
  46. free(p);
  47. assert_d_eq(mallctl("thread.prof.name", &thread_name, &sz, NULL, 0), 0,
  48. "mallctl read for thread name should not fail");
  49. assert_str_eq(thread_name, test_thread_name,
  50. "Thread name should be changed if the system call succeeds");
  51. prof_sys_thread_name_read = test_prof_sys_thread_name_read_clear;
  52. p = malloc(1);
  53. free(p);
  54. assert_d_eq(mallctl("thread.prof.name", &thread_name, &sz, NULL, 0), 0,
  55. "mallctl read for thread name should not fail");
  56. expect_str_eq(thread_name, "", "Thread name should be updated if the "
  57. "system call returns a different name");
  58. }
  59. TEST_END
  60. int
  61. main(void) {
  62. return test(
  63. test_prof_sys_thread_name);
  64. }