-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure profilers work well together
- Loading branch information
Showing
5 changed files
with
96 additions
and
14 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/StackWalkMutex.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc. | ||
|
||
#include "StackWalkMutex.h" | ||
|
||
#include <sys/syscall.h> | ||
#include <time.h> | ||
|
||
|
||
StackWalkMutex::StackWalkMutex() : | ||
_isLockTaken{0}, _ownerTid{0} | ||
{ | ||
} | ||
|
||
void StackWalkMutex::lock() | ||
{ | ||
pid_t tid = syscall(SYS_gettid); | ||
|
||
// Attempt to acquire the lock using atomic operation | ||
while (__sync_lock_test_and_set(&_isLockTaken, 1) != 0) { | ||
struct timespec ts = {0, 1000000}; // Sleep for 1 millisecond | ||
nanosleep(&ts, NULL); | ||
// or sched_yield(); | ||
} | ||
_ownerTid = tid; | ||
} | ||
|
||
bool StackWalkMutex::try_lock() | ||
{ | ||
auto acquired = __sync_lock_test_and_set(&_isLockTaken, 1) == 0; | ||
if (acquired) | ||
{ | ||
_ownerTid = syscall(SYS_gettid); | ||
} | ||
return acquired; | ||
} | ||
|
||
void StackWalkMutex::unlock() | ||
{ | ||
_ownerTid = 0; | ||
__sync_lock_release(&_isLockTaken); | ||
} |
36 changes: 36 additions & 0 deletions
36
profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/StackWalkMutex.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc. | ||
|
||
#pragma once | ||
|
||
#include <signal.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
|
||
|
||
/// This is temporary, to make sure that the profiler(s) works correctly together. | ||
// | ||
// This mutex is a simple/naive non-recursive mutex | ||
class StackWalkMutex | ||
{ | ||
public: | ||
StackWalkMutex(); | ||
~StackWalkMutex() = default; | ||
|
||
StackWalkMutex(StackWalkMutex const&) = delete; | ||
StackWalkMutex& operator=(StackWalkMutex const&) = delete; | ||
|
||
StackWalkMutex(StackWalkMutex&&) = delete; | ||
StackWalkMutex& operator=(StackWalkMutex&&) = delete; | ||
|
||
// make it usable with std::unique_lock | ||
// so naming will be different from the rest of the code | ||
void lock(); | ||
void unlock(); | ||
bool try_lock(); | ||
|
||
private: | ||
volatile sig_atomic_t _isLockTaken; | ||
// mainly for debug reason | ||
pid_t _ownerTid; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters