Refactored atomic updates of max and min into utility class Atomics.
This commit is contained in:
parent
f94f9a066a
commit
7f39b8b7a4
|
@ -64,6 +64,7 @@ import org.eclipse.jetty.spdy.frames.SynStreamFrame;
|
|||
import org.eclipse.jetty.spdy.frames.WindowUpdateFrame;
|
||||
import org.eclipse.jetty.spdy.generator.Generator;
|
||||
import org.eclipse.jetty.spdy.parser.Parser;
|
||||
import org.eclipse.jetty.util.Atomics;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
||||
|
@ -831,16 +832,7 @@ public class StandardSession implements ISession, Parser.Listener, Handler<Stand
|
|||
{
|
||||
int streamId = stream.getId();
|
||||
if (stream.isClosed() && streamId % 2 != streamIds.get() % 2)
|
||||
{
|
||||
// Non-blocking atomic update
|
||||
int oldValue = lastStreamId.get();
|
||||
while (streamId > oldValue)
|
||||
{
|
||||
if (lastStreamId.compareAndSet(oldValue,streamId))
|
||||
break;
|
||||
oldValue = lastStreamId.get();
|
||||
}
|
||||
}
|
||||
Atomics.updateMax(lastStreamId, streamId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
// ========================================================================
|
||||
// Copyright (c) 2012 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
|
||||
package org.eclipse.jetty.util;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public class Atomics
|
||||
{
|
||||
private Atomics()
|
||||
{
|
||||
}
|
||||
|
||||
public static void updateMin(AtomicLong currentMin, long newValue)
|
||||
{
|
||||
long oldValue = currentMin.get();
|
||||
while (newValue < oldValue)
|
||||
{
|
||||
if (currentMin.compareAndSet(oldValue, newValue))
|
||||
break;
|
||||
oldValue = currentMin.get();
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateMax(AtomicLong currentMax, long newValue)
|
||||
{
|
||||
long oldValue = currentMax.get();
|
||||
while (newValue > oldValue)
|
||||
{
|
||||
if (currentMax.compareAndSet(oldValue, newValue))
|
||||
break;
|
||||
oldValue = currentMax.get();
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateMin(AtomicInteger currentMin, int newValue)
|
||||
{
|
||||
int oldValue = currentMin.get();
|
||||
while (newValue < oldValue)
|
||||
{
|
||||
if (currentMin.compareAndSet(oldValue, newValue))
|
||||
break;
|
||||
oldValue = currentMin.get();
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateMax(AtomicInteger currentMax, int newValue)
|
||||
{
|
||||
int oldValue = currentMax.get();
|
||||
while (newValue > oldValue)
|
||||
{
|
||||
if (currentMax.compareAndSet(oldValue, newValue))
|
||||
break;
|
||||
oldValue = currentMax.get();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,17 +4,19 @@
|
|||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
// The Eclipse Public License is available at
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
|
||||
package org.eclipse.jetty.util.statistic;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.eclipse.jetty.util.Atomics;
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Statistics on a counter value.
|
||||
|
@ -22,9 +24,9 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||
* Keep total, current and maximum values of a counter that
|
||||
* can be incremented and decremented. The total refers only
|
||||
* to increments.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class CounterStatistic
|
||||
public class CounterStatistic
|
||||
{
|
||||
protected final AtomicLong _max = new AtomicLong();
|
||||
protected final AtomicLong _curr = new AtomicLong();
|
||||
|
@ -39,11 +41,11 @@ public class CounterStatistic
|
|||
/* ------------------------------------------------------------ */
|
||||
public void reset(final long value)
|
||||
{
|
||||
_max.set(value);
|
||||
_max.set(value);
|
||||
_curr.set(value);
|
||||
_total.set(0); // total always set to 0 to properly calculate cumulative total
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* @param delta the amount to add to the count
|
||||
|
@ -53,15 +55,9 @@ public class CounterStatistic
|
|||
long value=_curr.addAndGet(delta);
|
||||
if (delta > 0)
|
||||
_total.addAndGet(delta);
|
||||
long oldValue = _max.get();
|
||||
while (value > oldValue)
|
||||
{
|
||||
if (_max.compareAndSet(oldValue, value))
|
||||
break;
|
||||
oldValue = _max.get();
|
||||
}
|
||||
Atomics.updateMax(_max,value);
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* @param delta the amount to subtract the count by.
|
||||
|
@ -70,7 +66,7 @@ public class CounterStatistic
|
|||
{
|
||||
add(-delta);
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
*/
|
||||
|
@ -78,7 +74,7 @@ public class CounterStatistic
|
|||
{
|
||||
add(1);
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
*/
|
||||
|
@ -95,7 +91,7 @@ public class CounterStatistic
|
|||
{
|
||||
return _max.get();
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* @return current value
|
||||
|
@ -104,7 +100,7 @@ public class CounterStatistic
|
|||
{
|
||||
return _curr.get();
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* @return total value
|
||||
|
@ -113,9 +109,4 @@ public class CounterStatistic
|
|||
{
|
||||
return _total.get();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
protected void upxdateMax(long value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,23 +4,25 @@
|
|||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
// The Eclipse Public License is available at
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
|
||||
package org.eclipse.jetty.util.statistic;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.eclipse.jetty.util.Atomics;
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* SampledStatistics
|
||||
* <p>
|
||||
* Provides max, total, mean, count, variance, and standard
|
||||
* Provides max, total, mean, count, variance, and standard
|
||||
* deviation of continuous sequence of samples.
|
||||
* <p>
|
||||
* Calculates estimates of mean, variance, and standard deviation
|
||||
|
@ -53,25 +55,17 @@ public class SampleStatistic
|
|||
{
|
||||
long total = _total.addAndGet(sample);
|
||||
long count = _count.incrementAndGet();
|
||||
|
||||
|
||||
if (count>1)
|
||||
{
|
||||
long mean10 = total*10/count;
|
||||
long delta10 = sample*10 - mean10;
|
||||
_totalVariance100.addAndGet(delta10*delta10);
|
||||
}
|
||||
|
||||
long oldMax = _max.get();
|
||||
while (sample > oldMax)
|
||||
{
|
||||
if (_max.compareAndSet(oldMax, sample))
|
||||
break;
|
||||
oldMax = _max.get();
|
||||
}
|
||||
|
||||
|
||||
Atomics.updateMax(_max, sample);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* @return the max value
|
||||
*/
|
||||
|
@ -80,37 +74,31 @@ public class SampleStatistic
|
|||
return _max.get();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public long getTotal()
|
||||
{
|
||||
return _total.get();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public long getCount()
|
||||
{
|
||||
return _count.get();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public double getMean()
|
||||
{
|
||||
return (double)_total.get()/_count.get();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public double getVariance()
|
||||
{
|
||||
final long variance100 = _totalVariance100.get();
|
||||
final long count = _count.get();
|
||||
|
||||
|
||||
return count>1?((double)variance100)/100.0/(count-1):0.0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public double getStdDev()
|
||||
{
|
||||
return Math.sqrt(getVariance());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue