[concurrency-interest] Concurrent telemetry gathering

Tim Peierls tim at peierls.net
Wed Nov 15 15:55:37 EST 2006


Do you really need avgIntvMinusOne? How about making avgIntv volatile, get
rid of avgInvMinusOne, remove the synchronized keyword from
setAverageInterval, and have run() look like this:

public final void run() {
    int a = avgIntv;
    int r = crecv.getAndSet(0);
    int x = cxmit.getAndSet(0);
    synchronized (this) {
        recv = ((recv * (a-1)) + r) / a;
        xmit = ((xmit * (a-1)) + x) / a;
    }
}

Also, make crecv and cxmit final, add @GuardedBy("this") to recv and xmit,
and make conn final in addition to transient.

What's the serializiability for? Will deserialization resubmit map entries
to the STPE?

--tim

On 11/15/06, Gregg Wonderly <gregg at cytetech.com> wrote:
>
> I'm working on collecting telemetry regarding bandwidth utilization
> through a
> "connection".  I've defined the following class, thinking that this should
> minimize concurrency for updating the numbers.
>
> In addition, there is a
> ConcurrentHashMap<OutboundConnection,ConnectionBandwidth> object that is
> accessed by threads of execution which are handling the I/O requests
> through the
> system.  That map is lazily initialized with putIfAbsent().  When the
> first
> entry is added, there is a ScheduledThreadPoolExecutor used to schedule a
> regular execution of "run" to create a time based running average of
> bandwidth
> usage.
>
> Is there anything else I should do differently?
>
> public class ConnectionBandwidth implements Runnable, Serializable {
>         private double recv, xmit;
>         private AtomicLong crecv = new AtomicLong(), cxmit = new
> AtomicLong();
>         private transient OutboundConnection conn;
>         private double avgIntv, avgIntvMinusOne;
>
>         public ConnectionBandwidth( OutboundConnection conn, int intv ) {
>                 this.conn = conn;
>                 avgIntv = intv;
>                 avgIntvMinusOne = intv - 1;
>         }
>
>         public synchronized void setAverageInterval( int intv ) {
>                 avgIntv = intv;
>                 avgIntvMinusOne = intv - 1;
>         }
>
>         public void bytesRecv( long val ) {
>                 crecv.addAndGet( val );
>         }
>
>         public void bytesXmit( long val ) {
>                 cxmit.addAndGet( val );
>         }
>
>         public final synchronized void run() {
>                 recv = ( (recv * avgIntvMinusOne ) + crecv.getAndSet(0) )
> / avgIntv;
>                 xmit = ( (xmit * avgIntvMinusOne ) + cxmit.getAndSet(0) )
> / avgIntv;
>         }
> }
>
> Gregg Wonderly
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20061115/243bef2f/attachment.html 


More information about the Concurrency-interest mailing list