memprof_thread.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //===-- memprof_thread.h ---------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of MemProfiler, a memory profiler.
  10. //
  11. // MemProf-private header for memprof_thread.cpp.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef MEMPROF_THREAD_H
  14. #define MEMPROF_THREAD_H
  15. #include "memprof_allocator.h"
  16. #include "memprof_internal.h"
  17. #include "memprof_stats.h"
  18. #include "sanitizer_common/sanitizer_common.h"
  19. #include "sanitizer_common/sanitizer_libc.h"
  20. #include "sanitizer_common/sanitizer_thread_registry.h"
  21. namespace __sanitizer {
  22. struct DTLS;
  23. } // namespace __sanitizer
  24. namespace __memprof {
  25. class MemprofThread;
  26. // These objects are created for every thread and are never deleted,
  27. // so we can find them by tid even if the thread is long dead.
  28. struct MemprofThreadContext final : public ThreadContextBase {
  29. explicit MemprofThreadContext(int tid)
  30. : ThreadContextBase(tid), announced(false),
  31. destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
  32. thread(nullptr) {}
  33. bool announced;
  34. u8 destructor_iterations;
  35. u32 stack_id;
  36. MemprofThread *thread;
  37. void OnCreated(void *arg) override;
  38. void OnFinished() override;
  39. struct CreateThreadContextArgs {
  40. MemprofThread *thread;
  41. StackTrace *stack;
  42. };
  43. };
  44. // MemprofThreadContext objects are never freed, so we need many of them.
  45. COMPILER_CHECK(sizeof(MemprofThreadContext) <= 256);
  46. // MemprofThread are stored in TSD and destroyed when the thread dies.
  47. class MemprofThread {
  48. public:
  49. static MemprofThread *Create(thread_callback_t start_routine, void *arg,
  50. u32 parent_tid, StackTrace *stack,
  51. bool detached);
  52. static void TSDDtor(void *tsd);
  53. void Destroy();
  54. struct InitOptions;
  55. void Init(const InitOptions *options = nullptr);
  56. thread_return_t ThreadStart(tid_t os_id,
  57. atomic_uintptr_t *signal_thread_is_registered);
  58. uptr stack_top();
  59. uptr stack_bottom();
  60. uptr stack_size();
  61. uptr tls_begin() { return tls_begin_; }
  62. uptr tls_end() { return tls_end_; }
  63. DTLS *dtls() { return dtls_; }
  64. u32 tid() { return context_->tid; }
  65. MemprofThreadContext *context() { return context_; }
  66. void set_context(MemprofThreadContext *context) { context_ = context; }
  67. bool AddrIsInStack(uptr addr);
  68. // True is this thread is currently unwinding stack (i.e. collecting a stack
  69. // trace). Used to prevent deadlocks on platforms where libc unwinder calls
  70. // malloc internally. See PR17116 for more details.
  71. bool isUnwinding() const { return unwinding_; }
  72. void setUnwinding(bool b) { unwinding_ = b; }
  73. MemprofThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
  74. MemprofStats &stats() { return stats_; }
  75. private:
  76. // NOTE: There is no MemprofThread constructor. It is allocated
  77. // via mmap() and *must* be valid in zero-initialized state.
  78. void SetThreadStackAndTls(const InitOptions *options);
  79. struct StackBounds {
  80. uptr bottom;
  81. uptr top;
  82. };
  83. StackBounds GetStackBounds() const;
  84. MemprofThreadContext *context_;
  85. thread_callback_t start_routine_;
  86. void *arg_;
  87. uptr stack_top_;
  88. uptr stack_bottom_;
  89. uptr tls_begin_;
  90. uptr tls_end_;
  91. DTLS *dtls_;
  92. MemprofThreadLocalMallocStorage malloc_storage_;
  93. MemprofStats stats_;
  94. bool unwinding_;
  95. };
  96. // Returns a single instance of registry.
  97. ThreadRegistry &memprofThreadRegistry();
  98. // Must be called under ThreadRegistryLock.
  99. MemprofThreadContext *GetThreadContextByTidLocked(u32 tid);
  100. // Get the current thread. May return 0.
  101. MemprofThread *GetCurrentThread();
  102. void SetCurrentThread(MemprofThread *t);
  103. u32 GetCurrentTidOrInvalid();
  104. // Used to handle fork().
  105. void EnsureMainThreadIDIsCorrect();
  106. } // namespace __memprof
  107. #endif // MEMPROF_THREAD_H