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