Path: blob/main/contrib/llvm-project/compiler-rt/lib/memprof/memprof_thread.h
35236 views
//===-- memprof_thread.h ---------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of MemProfiler, a memory profiler.9//10// MemProf-private header for memprof_thread.cpp.11//===----------------------------------------------------------------------===//1213#ifndef MEMPROF_THREAD_H14#define MEMPROF_THREAD_H1516#include "memprof_allocator.h"17#include "memprof_internal.h"18#include "memprof_stats.h"19#include "sanitizer_common/sanitizer_common.h"20#include "sanitizer_common/sanitizer_libc.h"21#include "sanitizer_common/sanitizer_thread_registry.h"2223namespace __sanitizer {24struct DTLS;25} // namespace __sanitizer2627namespace __memprof {2829class MemprofThread;3031// These objects are created for every thread and are never deleted,32// so we can find them by tid even if the thread is long dead.33struct MemprofThreadContext final : public ThreadContextBase {34explicit MemprofThreadContext(int tid)35: ThreadContextBase(tid), announced(false),36destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),37thread(nullptr) {}38bool announced;39u8 destructor_iterations;40u32 stack_id;41MemprofThread *thread;4243void OnCreated(void *arg) override;44void OnFinished() override;4546struct CreateThreadContextArgs {47MemprofThread *thread;48StackTrace *stack;49};50};5152// MemprofThreadContext objects are never freed, so we need many of them.53COMPILER_CHECK(sizeof(MemprofThreadContext) <= 256);5455// MemprofThread are stored in TSD and destroyed when the thread dies.56class MemprofThread {57public:58static MemprofThread *Create(thread_callback_t start_routine, void *arg,59u32 parent_tid, StackTrace *stack,60bool detached);61static void TSDDtor(void *tsd);62void Destroy();6364struct InitOptions;65void Init(const InitOptions *options = nullptr);6667thread_return_t ThreadStart(tid_t os_id,68atomic_uintptr_t *signal_thread_is_registered);6970uptr stack_top();71uptr stack_bottom();72uptr stack_size();73uptr tls_begin() { return tls_begin_; }74uptr tls_end() { return tls_end_; }75DTLS *dtls() { return dtls_; }76u32 tid() { return context_->tid; }77MemprofThreadContext *context() { return context_; }78void set_context(MemprofThreadContext *context) { context_ = context; }7980bool AddrIsInStack(uptr addr);8182// True is this thread is currently unwinding stack (i.e. collecting a stack83// trace). Used to prevent deadlocks on platforms where libc unwinder calls84// malloc internally. See PR17116 for more details.85bool isUnwinding() const { return unwinding_; }86void setUnwinding(bool b) { unwinding_ = b; }8788MemprofThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }89MemprofStats &stats() { return stats_; }9091private:92// NOTE: There is no MemprofThread constructor. It is allocated93// via mmap() and *must* be valid in zero-initialized state.9495void SetThreadStackAndTls(const InitOptions *options);9697struct StackBounds {98uptr bottom;99uptr top;100};101StackBounds GetStackBounds() const;102103MemprofThreadContext *context_;104thread_callback_t start_routine_;105void *arg_;106107uptr stack_top_;108uptr stack_bottom_;109110uptr tls_begin_;111uptr tls_end_;112DTLS *dtls_;113114MemprofThreadLocalMallocStorage malloc_storage_;115MemprofStats stats_;116bool unwinding_;117};118119// Returns a single instance of registry.120ThreadRegistry &memprofThreadRegistry();121122// Must be called under ThreadRegistryLock.123MemprofThreadContext *GetThreadContextByTidLocked(u32 tid);124125// Get the current thread. May return 0.126MemprofThread *GetCurrentThread();127void SetCurrentThread(MemprofThread *t);128u32 GetCurrentTidOrInvalid();129130// Used to handle fork().131void EnsureMainThreadIDIsCorrect();132} // namespace __memprof133134#endif // MEMPROF_THREAD_H135136137