1 23 24 package com.sun.enterprise.web.connector.grizzly; 25 26 import java.util.concurrent.ConcurrentHashMap ; 27 import java.util.concurrent.Future ; 28 import java.util.concurrent.ScheduledThreadPoolExecutor ; 29 import java.util.concurrent.TimeUnit ; 30 31 37 public class PipelineStatistic { 38 39 42 private int port = -1; 43 44 45 48 private boolean started = false; 49 50 51 54 private int maxQueueSizeInBytes = -1; 55 56 57 61 private int totalCount; 62 63 64 68 private int peakCount; 69 70 71 74 private int overflowCount; 75 76 77 80 private ScheduledThreadPoolExecutor countAverageExecutor; 81 82 83 86 private Statistic lastMinuteStat = new Statistic(1 * 60); 87 88 89 92 private Statistic lastFiveMinuteStat = new Statistic(5 * 60); 93 94 95 98 private Statistic lastFifteenMinuteStat = new Statistic(15 * 60); 99 100 101 104 private ConcurrentHashMap <Integer ,Statistic> stats = 105 new ConcurrentHashMap <Integer ,Statistic>(); 106 107 110 private Pipeline processorPipeline; 111 112 113 116 private Future futures[] = new Future [3]; 117 118 119 122 private int totalAcceptCount; 123 124 125 127 128 134 public PipelineStatistic(int port) { 135 this.port = port; 136 137 countAverageExecutor = new ScheduledThreadPoolExecutor (3, 138 new GrizzlyThreadFactory("GrizzlyPipelineStat", 139 port,Thread.NORM_PRIORITY)); 140 } 141 142 143 146 public void start(){ 147 if ( started ) return; 148 149 futures[0] = countAverageExecutor.scheduleAtFixedRate(lastMinuteStat, 1 , 150 lastMinuteStat.getSeconds(), TimeUnit.SECONDS); 151 futures[1] = countAverageExecutor.scheduleAtFixedRate(lastFiveMinuteStat, 1 , 152 lastFiveMinuteStat.getSeconds(), TimeUnit.SECONDS); 153 futures[2] = countAverageExecutor.scheduleAtFixedRate(lastFifteenMinuteStat, 1 , 154 lastFifteenMinuteStat.getSeconds(), TimeUnit.SECONDS); 155 156 stats.put(lastMinuteStat.getSeconds(), lastMinuteStat); 157 stats.put(lastFiveMinuteStat.getSeconds(), lastFiveMinuteStat); 158 stats.put(lastFifteenMinuteStat.getSeconds(), lastFifteenMinuteStat); 159 160 started = true; 161 } 162 163 164 167 public void stop(){ 168 if ( !started ) return; 169 170 for (Future future: futures){ 171 future.cancel(true); 172 } 173 174 stats.clear(); 175 started = false; 176 } 177 178 179 182 public boolean gather(int queueLength){ 183 if ( queueLength == maxQueueSizeInBytes){ 184 overflowCount++; 185 return false; 186 } 187 188 if ( queueLength > 0 ) 189 totalCount++; 190 191 if (queueLength > peakCount) { 193 peakCount = queueLength; 194 } 195 return true; 196 } 197 198 199 202 public int getCountOverflows(){ 203 return overflowCount; 204 } 205 206 207 214 public int getPeakQueued(){ 215 return peakCount; 216 } 217 218 219 224 public int getMaxQueued() { 225 return maxQueueSizeInBytes; 226 } 227 228 229 234 public int getCountTotalConnections() { 235 return totalAcceptCount; 236 } 237 238 239 243 public void setQueueSizeInBytes(int maxQueueSizeInBytesCount){ 244 this.maxQueueSizeInBytes = maxQueueSizeInBytesCount; 245 } 246 247 248 252 public int getQueueSizeInBytes(){ 253 return maxQueueSizeInBytes; 254 } 255 256 257 266 public int getCountTotalQueued() { 267 return totalCount; 268 } 269 270 271 276 public int getCountQueued() { 277 int size = 0; 278 279 if (processorPipeline != null) { 280 size += processorPipeline.size(); 281 } 282 283 return size; 284 } 285 286 287 296 public int getTicksTotalQueued() { 297 return -1; } 299 300 301 306 public int getCountQueued1MinuteAverage() { 307 return getCountAverage(1); 308 } 309 310 311 316 public int getCountQueued5MinuteAverage() { 317 return getCountAverage(5); 318 } 319 320 321 326 public int getCountQueued15MinuteAverage() { 327 return getCountAverage(15); 328 } 329 330 331 334 void incrementTotalAcceptCount() { 335 totalAcceptCount++; 336 } 337 338 void setProcessorPipeline(Pipeline processorPipeline) { 339 this.processorPipeline = processorPipeline; 340 } 341 342 345 354 private int getCountAverage(int minutes){ 355 Statistic stat = stats.get((minutes * 60)); 356 return (stat == null ? 0 : stat.average()); 357 } 358 359 360 363 class Statistic implements Runnable { 364 365 int lastCount = 0; 366 int average = 0; 367 int seconds; 368 369 public Statistic(int seconds){ 370 this.seconds = seconds; 371 } 372 373 374 public void run() { 375 average = totalCount - lastCount; 376 lastCount = totalCount; 377 } 378 379 public int average(){ 380 return average; 381 } 382 383 384 public int getSeconds(){ 385 return seconds; 386 } 387 } 388 389 390 } 391 | Popular Tags |