-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
modules/tools-misc/src/main/java/com/wizzardo/tools/misc/CpuAndAllocationStats.java
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,76 @@ | ||
package com.wizzardo.tools.misc; | ||
|
||
import com.sun.management.ThreadMXBean; | ||
|
||
import java.lang.management.ManagementFactory; | ||
|
||
public class CpuAndAllocationStats { | ||
|
||
public static final boolean CPU_TIME_TRACING_SUPPORTED; | ||
public static final boolean ALLOCATION_TRACING_SUPPORTED; | ||
protected static final ThreadLocal<CpuAndAllocationStats> THREAD_LOCAL = ThreadLocal.withInitial(CpuAndAllocationStats::new); | ||
|
||
static { | ||
ThreadMXBean threadMXBean = (ThreadMXBean) ManagementFactory.getThreadMXBean(); | ||
CPU_TIME_TRACING_SUPPORTED = threadMXBean.isThreadCpuTimeSupported() && threadMXBean.isThreadCpuTimeEnabled(); | ||
ALLOCATION_TRACING_SUPPORTED = threadMXBean.isThreadAllocatedMemorySupported() && threadMXBean.isThreadAllocatedMemoryEnabled(); | ||
} | ||
|
||
protected final long threadId; | ||
protected long cpuTime; | ||
protected long cpuUserTime; | ||
protected long allocation; | ||
protected ThreadMXBean threadMXBean; | ||
|
||
public static CpuAndAllocationStats get() { | ||
return THREAD_LOCAL.get(); | ||
} | ||
|
||
public CpuAndAllocationStats() { | ||
threadId = Thread.currentThread().getId(); | ||
threadMXBean = (com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean(); | ||
} | ||
|
||
public void reset() { | ||
if (CPU_TIME_TRACING_SUPPORTED) { | ||
cpuTime = getTotalCpuTime(); | ||
cpuUserTime = getTotalCpuUserTime(); | ||
} | ||
if (ALLOCATION_TRACING_SUPPORTED) { | ||
allocation = getTotalAllocation(); | ||
} | ||
} | ||
|
||
public long getCountedCpuTime() { | ||
return getTotalCpuTime() - cpuTime; | ||
} | ||
|
||
public long getTotalCpuTime() { | ||
if (!CPU_TIME_TRACING_SUPPORTED) | ||
return 0; | ||
|
||
return threadMXBean.getCurrentThreadCpuTime(); | ||
} | ||
|
||
public long getCountedCpuUserTime() { | ||
return getTotalCpuUserTime() - cpuUserTime; | ||
} | ||
|
||
public long getTotalCpuUserTime() { | ||
if (!CPU_TIME_TRACING_SUPPORTED) | ||
return 0; | ||
|
||
return threadMXBean.getCurrentThreadUserTime(); | ||
} | ||
|
||
public long getCountedAllocation() { | ||
return getTotalAllocation() - allocation; | ||
} | ||
|
||
public long getTotalAllocation() { | ||
if (!ALLOCATION_TRACING_SUPPORTED) | ||
return 0; | ||
|
||
return threadMXBean.getThreadAllocatedBytes(threadId); | ||
} | ||
} |