reSIProcate/stack  9694
TimeAccumulate.hxx
Go to the documentation of this file.
00001 #ifndef TimeAccumulate_hxx
00002 #define TimeAccumulate_hxx
00003 
00004 #include <map>
00005 
00006 #include "rutil/Data.hxx"
00007 #include "rutil/Lock.hxx"
00008 #include "rutil/Mutex.hxx"
00009 #include "rutil/Timer.hxx"
00010 
00011 namespace resip
00012 {
00013 
00019 class TimeAccumulate
00020 {
00021    private:
00022       struct Accumulator
00023       {
00024             UInt64 totalTime;
00025             size_t count;
00026       };
00027 
00028    public:
00029 #if 1
00030       TimeAccumulate(const Data& name)
00031          : mName(name),
00032            mStart(Timer::getTimeMs())
00033       {}
00034 
00035       ~TimeAccumulate()
00036       {
00037          UInt64 end = Timer::getTimeMs();
00038          end -= mStart;
00039          Lock lock(TimeAccumulate::mMutex);
00040 
00041          Accumulator& acc = TimeAccumulate::mTimes[mName];
00042          acc.count += 1;
00043          acc.totalTime += end;
00044       }
00045 #else
00046       TimeAccumulate(const char* chars)
00047          : mName(Data::Empty),
00048            mStart(0)
00049       {}
00050 
00051       TimeAccumulate(const Data& name)
00052          : mName(Data::Empty),
00053            mStart(0)
00054       {}
00055 
00056       ~TimeAccumulate()
00057       {
00058       }
00059 #endif
00060       
00061       static UInt64 getTime(const Data& name)
00062       {
00063          Lock lock(TimeAccumulate::mMutex);
00064          return TimeAccumulate::mTimes[name].totalTime;
00065       }
00066       
00067       static size_t getCount(const Data& name)
00068       {
00069          Lock lock(TimeAccumulate::mMutex);
00070          return TimeAccumulate::mTimes[name].count;
00071       }
00072 
00073       static void dump();
00074       static void clear();
00075 
00076       class Guard
00077       {
00078          public:
00079             explicit Guard(UInt64& accumulator)
00080                : mAccumulator(accumulator)
00081             {
00082                mAccumulator -= Timer::getTimeMs();
00083             }
00084             ~Guard()
00085             {
00086                mAccumulator += Timer::getTimeMs();
00087             }
00088 
00089          private:
00090             UInt64& mAccumulator;
00091       };
00092 
00093    private:
00094       typedef std::map<Data, Accumulator> TimeMap;
00095 
00096       const Data mName;
00097       const UInt64 mStart;
00098 
00099       static Mutex mMutex;
00100       static TimeMap mTimes;
00101 };
00102 
00103 }
00104 #endif
00105 
00106 /* ====================================================================
00107  * The Vovida Software License, Version 1.0 
00108  * 
00109  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00110  * 
00111  * Redistribution and use in source and binary forms, with or without
00112  * modification, are permitted provided that the following conditions
00113  * are met:
00114  * 
00115  * 1. Redistributions of source code must retain the above copyright
00116  *    notice, this list of conditions and the following disclaimer.
00117  * 
00118  * 2. Redistributions in binary form must reproduce the above copyright
00119  *    notice, this list of conditions and the following disclaimer in
00120  *    the documentation and/or other materials provided with the
00121  *    distribution.
00122  * 
00123  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00124  *    and "Vovida Open Communication Application Library (VOCAL)" must
00125  *    not be used to endorse or promote products derived from this
00126  *    software without prior written permission. For written
00127  *    permission, please contact vocal@vovida.org.
00128  *
00129  * 4. Products derived from this software may not be called "VOCAL", nor
00130  *    may "VOCAL" appear in their name, without prior written
00131  *    permission of Vovida Networks, Inc.
00132  * 
00133  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00134  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00135  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00136  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00137  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00138  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00139  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00140  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00141  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00142  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00144  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00145  * DAMAGE.
00146  * 
00147  * ====================================================================
00148  * 
00149  * This software consists of voluntary contributions made by Vovida
00150  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00151  * Inc.  For more information on Vovida Networks, Inc., please see
00152  * <http://www.vovida.org/>.
00153  *
00154  */