measure.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //------------------------------------------------------------------------------
  2. // File: Measure.h
  3. //
  4. // Desc: DirectShow base classes.
  5. //
  6. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8. /*
  9. The idea is to pepper the source code with interesting measurements and
  10. have the last few thousand of these recorded in a circular buffer that
  11. can be post-processed to give interesting numbers.
  12. WHAT THE LOG LOOKS LIKE:
  13. Time (sec) Type Delta Incident_Name
  14. 0.055,41 NOTE -. Incident Nine - Another note
  15. 0.055,42 NOTE 0.000,01 Incident Nine - Another note
  16. 0.055,44 NOTE 0.000,02 Incident Nine - Another note
  17. 0.055,45 STOP -. Incident Eight - Also random
  18. 0.055,47 START -. Incident Seven - Random
  19. 0.055,49 NOTE 0.000,05 Incident Nine - Another note
  20. ------- <etc. there is a lot of this> ----------------
  21. 0.125,60 STOP 0.000,03 Msr_Stop
  22. 0.125,62 START -. Msr_Start
  23. 0.125,63 START -. Incident Two - Start/Stop
  24. 0.125,65 STOP 0.000,03 Msr_Start
  25. 0.125,66 START -. Msr_Stop
  26. 0.125,68 STOP 0.000,05 Incident Two - Start/Stop
  27. 0.125,70 STOP 0.000,04 Msr_Stop
  28. 0.125,72 START -. Msr_Start
  29. 0.125,73 START -. Incident Two - Start/Stop
  30. 0.125,75 STOP 0.000,03 Msr_Start
  31. 0.125,77 START -. Msr_Stop
  32. 0.125,78 STOP 0.000,05 Incident Two - Start/Stop
  33. 0.125,80 STOP 0.000,03 Msr_Stop
  34. 0.125,81 NOTE -. Incident Three - single Note
  35. 0.125,83 START -. Incident Four - Start, no stop
  36. 0.125,85 START -. Incident Five - Single Start/Stop
  37. 0.125,87 STOP 0.000,02 Incident Five - Single Start/Stop
  38. Number Average StdDev Smallest Largest Incident_Name
  39. 10 0.000,58 0.000,10 0.000,55 0.000,85 Incident One - Note
  40. 50 0.000,05 0.000,00 0.000,05 0.000,05 Incident Two - Start/Stop
  41. 1 -. -. -. -. Incident Three - single Note
  42. 0 -. -. -. -. Incident Four - Start, no stop
  43. 1 0.000,02 -. 0.000,02 0.000,02 Incident Five - Single Start/Stop
  44. 0 -. -. -. -. Incident Six - zero occurrences
  45. 100 0.000,25 0.000,12 0.000,02 0.000,62 Incident Seven - Random
  46. 100 0.000,79 0.000,48 0.000,02 0.001,92 Incident Eight - Also random
  47. 5895 0.000,01 0.000,01 0.000,01 0.000,56 Incident Nine - Another note
  48. 10 0.000,03 0.000,00 0.000,03 0.000,04 Msr_Note
  49. 50 0.000,03 0.000,00 0.000,03 0.000,04 Msr_Start
  50. 50 0.000,04 0.000,03 0.000,03 0.000,31 Msr_Stop
  51. WHAT IT MEANS:
  52. The log shows what happened and when. Each line shows the time at which
  53. something happened (see WHAT YOU CODE below) what it was that happened
  54. and (if approporate) the time since the corresponding previous event
  55. (that's the delta column).
  56. The statistics show how many times each event occurred, what the average
  57. delta time was, also the standard deviation, largest and smalles delta.
  58. WHAT YOU CODE:
  59. Before anything else executes: - register your ids
  60. int id1 = Msr_Register("Incident One - Note");
  61. int id2 = Msr_Register("Incident Two - Start/Stop");
  62. int id3 = Msr_Register("Incident Three - single Note");
  63. etc.
  64. At interesting moments:
  65. // To measure a repetitive event - e.g. end of bitblt to screen
  66. Msr_Note(Id9); // e.g. "video frame hiting the screen NOW!"
  67. or
  68. // To measure an elapsed time e.g. time taken to decode an MPEG B-frame
  69. Msr_Start(Id2); // e.g. "Starting to decode MPEG B-frame"
  70. . . .
  71. MsrStop(Id2); // "Finished MPEG decode"
  72. At the end:
  73. HANDLE hFile;
  74. hFile = CreateFile("Perf.log", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
  75. Msr_Dump(hFile); // This writes the log out to the file
  76. CloseHandle(hFile);
  77. or
  78. Msr_Dump(NULL); // This writes it to DbgLog((LOG_TRACE,0, ... ));
  79. // but if you are writing it out to the debugger
  80. // then the times are probably all garbage because
  81. // the debugger can make things run awfully slow.
  82. A given id should be used either for start / stop or Note calls. If Notes
  83. are mixed in with Starts and Stops their statistics will be gibberish.
  84. If you code the calls in upper case i.e. MSR_START(idMunge); then you get
  85. macros which will turn into nothing unless PERF is defined.
  86. You can reset the statistical counts for a given id by calling Reset(Id).
  87. They are reset by default at the start.
  88. It logs Reset as a special incident, so you can see it in the log.
  89. The log is a circular buffer in storage (to try to minimise disk I/O).
  90. It overwrites the oldest entries once full. The statistics include ALL
  91. incidents since the last Reset, whether still visible in the log or not.
  92. */
  93. #ifndef __MEASURE__
  94. #define __MEASURE__
  95. #ifdef PERF
  96. #define MSR_INIT() Msr_Init()
  97. #define MSR_TERMINATE() Msr_Terminate()
  98. #define MSR_REGISTER(a) Msr_Register(a)
  99. #define MSR_RESET(a) Msr_Reset(a)
  100. #define MSR_CONTROL(a) Msr_Control(a)
  101. #define MSR_START(a) Msr_Start(a)
  102. #define MSR_STOP(a) Msr_Stop(a)
  103. #define MSR_NOTE(a) Msr_Note(a)
  104. #define MSR_INTEGER(a,b) Msr_Integer(a,b)
  105. #define MSR_DUMP(a) Msr_Dump(a)
  106. #define MSR_DUMPSTATS(a) Msr_DumpStats(a)
  107. #else
  108. #define MSR_INIT() ((void)0)
  109. #define MSR_TERMINATE() ((void)0)
  110. #define MSR_REGISTER(a) 0
  111. #define MSR_RESET(a) ((void)0)
  112. #define MSR_CONTROL(a) ((void)0)
  113. #define MSR_START(a) ((void)0)
  114. #define MSR_STOP(a) ((void)0)
  115. #define MSR_NOTE(a) ((void)0)
  116. #define MSR_INTEGER(a,b) ((void)0)
  117. #define MSR_DUMP(a) ((void)0)
  118. #define MSR_DUMPSTATS(a) ((void)0)
  119. #endif
  120. #ifdef __cplusplus
  121. extern "C" {
  122. #endif
  123. // This must be called first - (called by the DllEntry)
  124. void WINAPI Msr_Init(void);
  125. // Call this last to clean up (or just let it fall off the end - who cares?)
  126. void WINAPI Msr_Terminate(void);
  127. // Call this to get an Id for an "incident" that you can pass to Start, Stop or Note
  128. // everything that's logged is called an "incident".
  129. int WINAPI Msr_Register(__in LPTSTR Incident);
  130. // Reset the statistical counts for an incident
  131. void WINAPI Msr_Reset(int Id);
  132. // Reset all the counts for all incidents
  133. #define MSR_RESET_ALL 0
  134. #define MSR_PAUSE 1
  135. #define MSR_RUN 2
  136. void WINAPI Msr_Control(int iAction);
  137. // log the start of an operation
  138. void WINAPI Msr_Start(int Id);
  139. // log the end of an operation
  140. void WINAPI Msr_Stop(int Id);
  141. // log a one-off or repetitive operation
  142. void WINAPI Msr_Note(int Id);
  143. // log an integer (on which we can see statistics later)
  144. void WINAPI Msr_Integer(int Id, int n);
  145. // print out all the vaialable log (it may have wrapped) and then the statistics.
  146. // When the log wraps you lose log but the statistics are still complete.
  147. // hFIle==NULL => use DbgLog
  148. // otherwise hFile must have come from CreateFile or OpenFile.
  149. void WINAPI Msr_Dump(HANDLE hFile);
  150. // just dump the statistics - never mind the log
  151. void WINAPI Msr_DumpStats(HANDLE hFile);
  152. // Type definitions in case you want to declare a pointer to the dump functions
  153. // (makes it a trifle easier to do dynamic linking
  154. // i.e. LoadModule, GetProcAddress and call that)
  155. // Typedefs so can declare MSR_DUMPPROC *MsrDumpStats; or whatever
  156. typedef void WINAPI MSR_DUMPPROC(HANDLE hFile);
  157. typedef void WINAPI MSR_CONTROLPROC(int iAction);
  158. #ifdef __cplusplus
  159. }
  160. #endif
  161. #endif // __MEASURE__