1 // ========================================================================
2 // Copyright (c) Webtide LLC
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14
15 package org.eclipse.jetty.monitor.thread;
16
17
18 /* ------------------------------------------------------------ */
19 /**
20 */
21 public class ThreadMonitorInfo
22 {
23 private Thread _thread;
24 private StackTraceElement[] _stackTrace;
25
26 private boolean _threadSpinning = false;
27 private int _traceCount = -1;
28
29 private long _prevCpuTime;
30 private long _prevSampleTime;
31 private long _currCpuTime;
32 private long _currSampleTime;
33
34
35 /* ------------------------------------------------------------ */
36 /**
37 * Instantiates a new thread monitor info.
38 *
39 * @param thread the thread this object is created for
40 */
41 public ThreadMonitorInfo(Thread thread)
42 {
43 _thread = thread;
44 }
45
46 /* ------------------------------------------------------------ */
47 /**
48 * @return Id of the thread
49 */
50 public long getThreadId()
51 {
52 return _thread.getId();
53 }
54
55 /* ------------------------------------------------------------ */
56 /**
57 * Gets the thread name.
58 *
59 * @return the thread name
60 */
61 public String getThreadName()
62 {
63 return _thread.getName();
64 }
65
66 /* ------------------------------------------------------------ */
67 /**
68 * Gets the thread state.
69 *
70 * @return the thread state
71 */
72 public String getThreadState()
73 {
74 return _thread.getState().toString();
75 }
76
77 /* ------------------------------------------------------------ */
78 /**
79 * Gets the stack trace.
80 *
81 * @return the stack trace
82 */
83 public StackTraceElement[] getStackTrace()
84 {
85 return _stackTrace;
86 }
87
88 /* ------------------------------------------------------------ */
89 /**
90 * Sets the stack trace.
91 *
92 * @param stackTrace the new stack trace
93 */
94 public void setStackTrace(StackTraceElement[] stackTrace)
95 {
96 _stackTrace = stackTrace;
97 }
98
99 /* ------------------------------------------------------------ */
100 /**
101 * Checks if is spinning.
102 *
103 * @return true, if is spinning
104 */
105 public boolean isSpinning()
106 {
107 return _threadSpinning;
108 }
109
110 /* ------------------------------------------------------------ */
111 /**
112 * Sets the spinning flag.
113 *
114 * @param value the new value
115 */
116 public void setSpinning(boolean value)
117 {
118 _threadSpinning = value;
119 }
120
121 /* ------------------------------------------------------------ */
122 /**
123 * Sets the trace count.
124 *
125 * @param traceCount the new trace count
126 */
127 public void setTraceCount(int traceCount)
128 {
129 _traceCount = traceCount;
130 }
131
132 /* ------------------------------------------------------------ */
133 /**
134 * Gets the trace count.
135 *
136 * @return the trace count
137 */
138 public int getTraceCount()
139 {
140 return _traceCount;
141 }
142
143 /* ------------------------------------------------------------ */
144 /**
145 * @return the CPU time of the thread
146 */
147 public long getCpuTime()
148 {
149 return _currCpuTime;
150 }
151
152 /* ------------------------------------------------------------ */
153 /**
154 * Set the CPU time.
155 *
156 * @param ns new CPU time
157 */
158 public void setCpuTime(long ns)
159 {
160 _prevCpuTime = _currCpuTime;
161 _currCpuTime = ns;
162 }
163
164 /* ------------------------------------------------------------ */
165 /**
166 * @return the time of sample
167 */
168 public long getSampleTime()
169 {
170 return _currSampleTime;
171 }
172
173 /* ------------------------------------------------------------ */
174 /**
175 * Sets the sample time.
176 *
177 * @param ns the time of sample
178 */
179 public void setSampleTime(long ns)
180 {
181 _prevSampleTime = _currSampleTime;
182 _currSampleTime = ns;
183 }
184
185 /* ------------------------------------------------------------ */
186 /**
187 * Gets the CPU utilization.
188 *
189 * @return the CPU utilization percentage
190 */
191 public float getCpuUtilization()
192 {
193 long elapsedCpuTime = _currCpuTime - _prevCpuTime;
194 long elapsedNanoTime = _currSampleTime - _prevSampleTime;
195
196 return elapsedNanoTime > 0 ? Math.min((elapsedCpuTime * 100.0f) / elapsedNanoTime, 100.0f) : 0;
197 }
198 }