How can I measure CPU time of a specific set of threads?
To get CPU clock ID
of every thread you can use: pthread_getcpuclockid
and using this CPU clock ID
you can retrieve the current thread CPU time using: clock_gettime.
Following is the sample code to demonstrate the same:
struct timespec currTime;
clockid_t threadClockId;
//! Get thread clock Id
pthread_getcpuclockid(pthread_self(), &threadClockId);
//! Using thread clock Id get the clock time
clock_gettime(threadClockId, &currTime);
Obtaining CPU thread usage in Java
You can use ThreadMXBean
to get cpu usage statistics from all running threads. In the example below the CPU usage per thread
is calculated:
private int sampleTime = 10000;
private ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
private RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
private OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
private Map<Long, Long> threadInitialCPU = new HashMap<Long, Long>();
private Map<Long, Float> threadCPUUsage = new HashMap<Long, Float>();
private long initialUptime = runtimeMxBean.getUptime();
ThreadInfo[] threadInfos = threadMxBean.dumpAllThreads(false, false);
for (ThreadInfo info : threadInfos) {
threadInitialCPU.put(info.getThreadId(), threadMxBean.getThreadCpuTime(info.getThreadId()));
}
try {Thread.sleep(sampleTime);} catch (InterruptedException e) {}
long upTime = runtimeMxBean.getUptime();
Map<Long, Long> threadCurrentCPU = new HashMap<Long, Long>();
ThreadInfo[] threadInfos = threadMxBean.dumpAllThreads(false, false);
for (ThreadInfo info : threadInfos) {
threadCurrentCPU.put(info.getThreadId(), threadMxBean.getThreadCpuTime(info.getThreadId()));
}
// CPU over all processes
//int nrCPUs = osMxBean.getAvailableProcessors();
// total CPU: CPU % can be more than 100% (devided over multiple cpus)
long nrCPUs = 1;
// elapsedTime is in ms.
long elapsedTime = (upTime - initialUptime);
for (ThreadInfo info : threadInfos) {
// elapsedCpu is in ns
Long initialCPU = threadInitialCPU.get(info.getThreadId());
if (initialCPU != null) {
long elapsedCpu = threadCurrentCPU.get(info.getThreadId()) - initialCPU;
float cpuUsage = elapsedCpu / (elapsedTime * 1000000F * nrCPUs);
threadCPUUsage.put(info.getThreadId(), cpuUsage);
}
}
// threadCPUUsage contains cpu % per thread
System.out.println(threadCPUUsage);
// You can use osMxBean.getThreadInfo(theadId) to get information on every thread reported in threadCPUUsage and analyze the most CPU intentive threads
Related Topics
Using Hardware Performance Counters in Linux
Way Cross Compile C/C++ Code to Run on Windows, Linux, and MAC Os
In C++, What Is a "Namespace Alias"
Why Is Std::Function Not Equality Comparable
Redirect Stdout/Stderr to a String
How to Statically Link Standard Library to My C++ Program
How to Enable _Int128 on Visual Studio
What Is a Non-Trivial Constructor in C++
C++11 Stl Containers and Thread Safety
Get Computer Name and Logged User Name
External Library Throws Undefined Reference Errors in Qt Creator
Computing Length of a C String at Compile Time. Is This Really a Constexpr
Floating Point Format for Std::Ostream
How Do Exceptions Work (Behind the Scenes) in C++
How to Solve "Unresolved Inclusion: <Iostream>" in a C++ File in Eclipse Cdt