Repository navigation
Expand file tree
/
Copy pathThreadsTime.java
More file actions
105 lines (92 loc) · 3.4 KB
/
Copy pathThreadsTime.java
File metadata and controls
105 lines (92 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.HashMap;
import java.util.Collection;
public final class ThreadsTime
extends Thread
{
private class Times {
public long id;
public long startCpuTime;
public long startUserTime;
public long endCpuTime;
public long endUserTime;
}
private final long interval;
private final long threadId;
private final HashMap<Long,Times> history = new HashMap<Long,Times>();
/** Create a polling thread to track times. */
public ThreadsTime( final long interval ) {
super( "Thread time monitor" );
this.interval = interval;
threadId = getId( );
setDaemon( true );
this.start();
}
/** Run the thread until interrupted. */
public void run( ) {
while ( !isInterrupted( ) ) {
update( );
try { sleep( interval ); }
catch ( InterruptedException e ) { break; }
}
}
/** Update the hash table of thread times. */
private void update( ) {
final ThreadMXBean bean =
ManagementFactory.getThreadMXBean( );
final long[] ids = bean.getAllThreadIds( );
for ( long id : ids ) {
if ( id == threadId )
continue; // Exclude polling thread
final long c = bean.getThreadCpuTime( id );
final long u = bean.getThreadUserTime( id );
if ( c == -1 || u == -1 )
continue; // Thread died
Times times = history.get( id );
long usrCycle = 0;
long totCycle = 0;
if ( times == null ) {
times = new Times( );
times.id = id;
times.startCpuTime = c;
times.startUserTime = u;
times.endCpuTime = c;
times.endUserTime = u;
history.put( id, times );
} else {
usrCycle = u-times.endUserTime;
totCycle = c-times.endCpuTime;
times.endCpuTime = c;
times.endUserTime = u;
}
//long usr = times.endUserTime-times.startUserTime;
//long tot = times.endCpuTime-times.startCpuTime;
long thres=0;
if(totCycle>thres){
System.out.println(times.id+"\t usr:"+usrCycle/1000000f+"\t sys:"+(totCycle-usrCycle)/1000000f+"\t\t"+bean.getThreadInfo(id).getThreadName());
}
}
System.out.println(" ");
}
/** Get total CPU time so far in nanoseconds. */
public long getTotalCpuTime( ) {
final Collection<Times> hist = history.values( );
long time = 0L;
for ( Times times : hist )
time += times.endCpuTime - times.startCpuTime;
return time;
}
/** Get total user time so far in nanoseconds. */
public long getTotalUserTime( ) {
final Collection<Times> hist = history.values( );
long time = 0L;
for ( Times times : hist )
time += times.endUserTime - times.startUserTime;
return time;
}
/** Get total system time so far in nanoseconds. */
public long getTotalSystemTime( ) {
return getTotalCpuTime( ) - getTotalUserTime( );
}
}