home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / system / source / profile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  6.1 KB  |  235 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. #include "stdafx.h"
  27. #include <windows.h>
  28. #include <vd2/system/profile.h>
  29.  
  30. ///////////////////////////////////////////////////////////////////////////
  31.  
  32. VDRTProfiler *g_pCentralProfiler;
  33.  
  34. void VDInitProfilingSystem() {
  35.     if (!g_pCentralProfiler)
  36.         g_pCentralProfiler = new VDRTProfiler;
  37. }
  38.  
  39. void VDDeinitProfilingSystem() {
  40.     delete g_pCentralProfiler;
  41.     g_pCentralProfiler = 0;
  42. }
  43.  
  44. VDRTProfiler *VDGetRTProfiler() {
  45.     return g_pCentralProfiler;
  46. }
  47.  
  48. ///////////////////////////////////////////////////////////////////////////
  49.  
  50. VDRTProfiler::VDRTProfiler()
  51.     : mbEnableCollection(false)
  52. {
  53.     LARGE_INTEGER freq;
  54.     QueryPerformanceFrequency(&freq);
  55.     mPerfFreq = freq.QuadPart;
  56. }
  57.  
  58. VDRTProfiler::~VDRTProfiler() {
  59. }
  60.  
  61. void VDRTProfiler::BeginCollection() {
  62.     mbEnableCollection = true;
  63. }
  64.  
  65. void VDRTProfiler::EndCollection() {
  66.     mbEnableCollection = false;
  67. }
  68.  
  69. void VDRTProfiler::Swap() {
  70.     vdsynchronized(mLock) {
  71.         LARGE_INTEGER tim;
  72.         QueryPerformanceCounter(&tim);
  73.  
  74.         mSnapshotTime = tim.QuadPart;
  75.  
  76.         // update channels
  77.         uint32 channelCount = mChannelArray.size();
  78.         mChannelArrayToPaint.resize(channelCount);
  79.  
  80.         for(uint32 i=0; i<channelCount; ++i) {
  81.             Channel& src = mChannelArray[i];
  82.             Channel& dst = mChannelArrayToPaint[i];
  83.  
  84.             dst.mpName = src.mpName;
  85.  
  86.             dst.mEventList.clear();
  87.             dst.mEventList.swap(src.mEventList);
  88.             if (src.mbEventPending) {
  89.                 src.mEventList.push_back(dst.mEventList.back());
  90.                 src.mEventList.back().mEndTime = mSnapshotTime;
  91.             }
  92.         }
  93.  
  94.         // update counters
  95.         Counters::iterator itC(mCounterArray.begin()), itCEnd(mCounterArray.end());
  96.         for(; itC != itCEnd; ++itC) {
  97.             Counter& ctr = *itC;
  98.  
  99.             ctr.mDataLast = ctr.mData;
  100.  
  101.             switch(ctr.mType) {
  102.                 case kCounterTypeUint32:
  103.                     ctr.mData.u32 = *(const uint32 *)ctr.mpData;
  104.                     break;
  105.                 case kCounterTypeDouble:
  106.                     ctr.mData.d = *(const double *)ctr.mpData;
  107.                     break;
  108.             }
  109.         }
  110.  
  111.         mCounterArrayToPaint = mCounterArray;
  112.     }
  113. }
  114.  
  115. int VDRTProfiler::AllocChannel(const char *name) {
  116.     uint32 i;
  117.  
  118.     vdsynchronized(mLock) {
  119.         const uint32 nChannels = mChannelArray.size();
  120.  
  121.         for(i=0; i<nChannels; ++i)
  122.             if (!mChannelArray[i].mpName)
  123.                 break;
  124.  
  125.         if (mChannelArray.size() <= i)
  126.             mChannelArray.resize(i + 1);
  127.  
  128.         mChannelArray[i].mpName = name;
  129.         mChannelArray[i].mbEventPending = false;
  130.     }
  131.  
  132.     return (int)i;
  133. }
  134.  
  135. void VDRTProfiler::FreeChannel(int ch) {
  136.     vdsynchronized(mLock) {
  137.         mChannelArray[ch].mpName = 0;
  138.         mChannelArray[ch].mEventList.clear();
  139.     }
  140. }
  141.  
  142. void VDRTProfiler::BeginEvent(int channel, uint32 color, const char *name) {
  143.     if (mbEnableCollection) {
  144.         LARGE_INTEGER tim;
  145.         QueryPerformanceCounter(&tim);
  146.         vdsynchronized(mLock) {
  147.             Channel& chan = mChannelArray[channel];
  148.  
  149.             if (!chan.mbEventPending) {
  150.                 chan.mbEventPending = true;
  151.                 chan.mEventList.push_back(Event());
  152.                 Event& ev = chan.mEventList.back();
  153.                 ev.mpName = name;
  154.                 ev.mColor = color;
  155.                 ev.mStartTime = tim.QuadPart;
  156.                 ev.mEndTime = tim.QuadPart;
  157.             }
  158.         }
  159.     }
  160. }
  161.  
  162. void VDRTProfiler::EndEvent(int channel) {
  163.     if (mbEnableCollection) {
  164.         LARGE_INTEGER tim;
  165.  
  166.         QueryPerformanceCounter(&tim);
  167.         vdsynchronized(mLock) {
  168.             Channel& chan = mChannelArray[channel];
  169.  
  170.             if (chan.mbEventPending) {
  171.                 chan.mEventList.back().mEndTime = tim.QuadPart;
  172.                 chan.mbEventPending = false;
  173.             }
  174.         }
  175.     }
  176. }
  177.  
  178. void VDRTProfiler::RegisterCounterU32(const char *name, const uint32 *val) {
  179.     RegisterCounter(name, val, kCounterTypeUint32);
  180. }
  181.  
  182. void VDRTProfiler::RegisterCounterD(const char *name, const double *val) {
  183.     RegisterCounter(name, val, kCounterTypeDouble);
  184. }
  185.  
  186. struct VDRTProfiler::CounterByNamePred {
  187.     bool operator()(const char *name1, const char *name2) const {
  188.         return strcmp(name1, name2) < 0;
  189.     }
  190.  
  191.     bool operator()(const char *name1, const Counter& ctr) const {
  192.         return strcmp(name1, ctr.mpName) < 0;
  193.     }
  194.  
  195.     bool operator()(const Counter& ctr, const char *name2) const {
  196.         return strcmp(ctr.mpName, name2) < 0;
  197.     }
  198.  
  199.     bool operator()(const Counter& ctr1, const Counter& ctr2) const {
  200.         return strcmp(ctr1.mpName, ctr2.mpName) < 0;
  201.     }
  202. };
  203.  
  204. void VDRTProfiler::RegisterCounter(const char *name, const void *val, CounterType type) {
  205.     VDASSERT(val);
  206.  
  207.     vdsynchronized(mLock) {
  208.         Counters::iterator itBegin(mCounterArray.end());
  209.         Counters::iterator itEnd(mCounterArray.end());
  210.         Counters::iterator it(std::upper_bound(itBegin, itEnd, name, CounterByNamePred()));
  211.  
  212.         it = mCounterArray.insert(it, Counter());
  213.         Counter& ctr = *it;
  214.  
  215.         memset(&ctr.mData, 0, sizeof ctr.mData);
  216.         memset(&ctr.mDataLast, 0, sizeof ctr.mDataLast);
  217.         ctr.mpData = val;
  218.         ctr.mpName = name;
  219.         ctr.mType = type;
  220.     }
  221. }
  222.  
  223. void VDRTProfiler::UnregisterCounter(void *p) {
  224.     vdsynchronized(mLock) {
  225.         Counters::iterator it(mCounterArray.begin()), itEnd(mCounterArray.end());
  226.         for(; it!=itEnd; ++it) {
  227.             const Counter& counter = *it;
  228.             if (counter.mpData == p) {
  229.                 mCounterArray.erase(it);
  230.                 return;
  231.             }
  232.         }
  233.     }
  234. }
  235.