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