Skip to content

Commit

Permalink
Merge branch 'master' into joey/aws-sns
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyzhao2018 authored May 24, 2024
2 parents 08ab696 + 11dba30 commit 483b389
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions dd-java-agent/cws-tls/src/main/java/datadog/cws/tls/ErpcTls.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,33 @@ public class ErpcTls implements Tls {
// Thread local storage
private Pointer tls;
private long maxThreads;
private int gettidSyscallId;

private final ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

public interface CLibrary extends Library {
CLibrary Instance = (CLibrary) Native.load("c", CLibrary.class);

NativeLong gettid();
NativeLong syscall(NativeLong number, Object... args);
}

static int getGettidSyscallId() {
String arch = System.getProperty("os.arch");
if (arch.equals("amd64")) {
return 186; // 186 is the syscall ID for "gettid" on amd64
} else if (arch.equals("arm64")) {
return 178; // 78 is the syscall ID for "gettid" on arm64
}
return 0;
}

static boolean isSupported() {
int syscallID = getGettidSyscallId();
if (syscallID == 0) {
return false;
}
try {
CLibrary.Instance.gettid();
CLibrary.Instance.syscall(new NativeLong(syscallID));
} catch (UnsatisfiedLinkError error) {
return false;
}
Expand All @@ -46,8 +61,8 @@ static boolean isSupported() {

private int getTID() {
Integer thread = threadLocal.get();
if (thread == null) {
int threadId = CLibrary.Instance.gettid().intValue();
if (thread == null && this.gettidSyscallId > 0) {
int threadId = CLibrary.Instance.syscall(new NativeLong(this.gettidSyscallId)).intValue();
threadLocal.set(threadId);
}
return thread;
Expand All @@ -57,6 +72,8 @@ public ErpcTls(int maxThreads, int refresh) {
Memory tls = new Memory(maxThreads * ENTRY_SIZE);
tls.clear();

this.gettidSyscallId = getGettidSyscallId();

this.maxThreads = maxThreads;
this.tls = tls;

Expand Down

0 comments on commit 483b389

Please sign in to comment.