home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / h / vd2 / system / profile.h < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  4.9 KB  |  168 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. #ifndef f_VD2_SYSTEM_PROFILE_H
  27. #define f_VD2_SYSTEM_PROFILE_H
  28.  
  29. #include <vd2/system/vdtypes.h>
  30. #include <vd2/system/thread.h>
  31. #include <vd2/system/vdstl.h>
  32. #include <vector>
  33.  
  34. class VDRTProfiler;
  35.  
  36. void VDInitProfilingSystem();
  37. void VDDeinitProfilingSystem();
  38. VDRTProfiler *VDGetRTProfiler();
  39.  
  40. //
  41. //    VDRTProfiler        Real-time profiler
  42. //
  43. //    This class forms the base for a very simple real-time profiler: threads
  44. //    record events in channels, and periodically, someone swaps the active
  45. //    recording array with a second array, and draws the sampled events off
  46. //    that array.  In VirtualDub, this is done via RTProfileDisplay.  Events
  47. //    are sampled via the high-performance counter in Win32, but clients need
  48. //    never know this fact.
  49. //
  50. //    All methods in VDRTProfiler are thread-safe.  However, it is assumed
  51. //    that only one client will be calling Swap() and accessing the Paint
  52. //    channel set.  Swap() should be called from rather low-level code as
  53. //    it may introduce deadlocks otherwise.
  54. //
  55. //    Strings passed to VDRTProfiler must be constant data in the main EXE.
  56. //    No dynamic strings or DLLs.  The reason is that there is an
  57. //    indefinite delay between a call to FreeChannel() and the last time
  58. //    data from that channel is displayed.
  59. //
  60. //    Channels are not restricted to a particular thread; it is permissible
  61. //    to allocate a channel in one thread and use it in another.  However,
  62. //    channels must not be simultaneously used by two threads -- that will
  63. //    generate interesting output.
  64. //
  65. class VDRTProfiler {
  66. public:
  67.     enum CounterType {
  68.         kCounterTypeUint32,
  69.         kCounterTypeDouble
  70.     };
  71.  
  72. public:
  73.     VDRTProfiler();
  74.     ~VDRTProfiler();
  75.  
  76.     void BeginCollection();
  77.     void EndCollection();
  78.     void Swap();
  79.  
  80.     bool IsEnabled() const { return mbEnableCollection; }
  81.  
  82.     int AllocChannel(const char *name);
  83.     void FreeChannel(int ch);
  84.     void BeginEvent(int channel, uint32 color, const char *name);
  85.     void EndEvent(int channel);
  86.  
  87.     void RegisterCounterD(const char *name, const double *val);
  88.     void RegisterCounterU32(const char *name, const uint32 *val);
  89.     void RegisterCounter(const char *name, const void *val, CounterType type);
  90.     void UnregisterCounter(void *p);
  91.  
  92. public:
  93.     struct Event {
  94.         uint64        mStartTime;
  95.         uint64        mEndTime;            // only last 32 bits of counter
  96.         uint32        mColor;
  97.         const char *mpName;
  98.     };
  99.  
  100.     struct Channel {
  101.         const char            *mpName;
  102.         bool                mbEventPending;
  103.         vdfastvector<Event>    mEventList;
  104.     };
  105.  
  106.     struct Counter {
  107.         const char            *mpName;
  108.         const void            *mpData;
  109.         CounterType            mType;
  110.         union {
  111.             uint32 u32;
  112.             double d;
  113.         } mData, mDataLast;
  114.     };
  115.  
  116.     struct CounterByNamePred;
  117.  
  118.     typedef std::vector<Channel> tChannels;
  119.     typedef vdfastvector<Counter> Counters;
  120.  
  121.     VDCriticalSection        mLock;
  122.     tChannels                mChannelArray;
  123.     tChannels                mChannelArrayToPaint;
  124.     Counters                mCounterArray;
  125.     Counters                mCounterArrayToPaint;
  126.     uint64                    mPerfFreq;
  127.     uint64                    mSnapshotTime;
  128.  
  129.     volatile bool            mbEnableCollection;
  130. };
  131.  
  132. //
  133. //    VDRTProfileChannel
  134. //
  135. //    This helper simply makes channel acquisition easier.  It automatically
  136. //    stubs out if no profiler is available.  However, it's still advisable
  137. //    not to call this from your inner loop!
  138. //
  139. class VDRTProfileChannel {
  140. public:
  141.     VDRTProfileChannel(const char *name)
  142.         : mpProfiler(VDGetRTProfiler())
  143.         , mProfileChannel(mpProfiler ? mpProfiler->AllocChannel(name) : 0)
  144.     {
  145.     }
  146.     ~VDRTProfileChannel() {
  147.         if (mpProfiler)
  148.             mpProfiler->FreeChannel(mProfileChannel);
  149.     }
  150.  
  151.     void Begin(uint32 color, const char *name) {
  152.         if (mpProfiler)
  153.             mpProfiler->BeginEvent(mProfileChannel, color, name);
  154.     }
  155.  
  156.     void End() {
  157.         if (mpProfiler)
  158.             mpProfiler->EndEvent(mProfileChannel);
  159.     }
  160.  
  161. protected:
  162.     VDRTProfiler *const mpProfiler;
  163.     int mProfileChannel;
  164. };
  165.  
  166. #endif
  167.  
  168.