WPROFILE.CPP 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Library profiler *
  23. * *
  24. * File Name : WPROFILE.CPP *
  25. * *
  26. * Programmer : Steve Tall *
  27. * *
  28. * Start Date : 11/17/95 *
  29. * *
  30. * Last Update : November 20th 1995 [ST] *
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Overview: *
  34. * *
  35. * New System *
  36. * ~~~~~~~~~~~ *
  37. * *
  38. * The new profiler system creates a seperate thread and then starts a timer off there. The *
  39. * timer in the second thread uses GetThreadContext to sample the IP address of each user *
  40. * thread. This system has the advantage of being able to sample what is happening in all the *
  41. * threads we own not just the main thread. Another advantage is that it doesnt require a *
  42. * major recompilation. *
  43. * The disadvantage is that we dont really know what is going on when the IP is outside the *
  44. * scope of our threads - We could be in direct draw, direct sound or even something like the *
  45. * VMM and there is no way to tell. *
  46. * *
  47. * *
  48. * *
  49. * Old System *
  50. * ~~~~~~~~~~~ *
  51. * *
  52. * The profiler works by using the function prologue and epilogue hooks available in Watcom *
  53. * to register the current functions address in a global variable and then sampling the *
  54. * contents of the variable using a windows timer which runs at up to 1000 samples per second.*
  55. * *
  56. * Compile the code to be sampled with the -ep and -ee flags to enable the prologue (__PRO) *
  57. * and epilogue (__EPI) calls to be generated. *
  58. * At the beginning of the section to be profiled (just before main loop normally) call the *
  59. * Start_Profiler function to start sampling. At the end of the section, call Stop_Profiler *
  60. * which will stop the timer and write the profile data to disk in the PROFILE.BIN file. *
  61. * *
  62. * Use PROFILE.EXE to view the results of the session. *
  63. * *
  64. * The addition of prologue and epilogue code will slow down the product and the profiler *
  65. * allocates a huge buffer for data so it should not be linked in unless it is going to be *
  66. * used. *
  67. * *
  68. * The advantage of the prologue/epilogue approach is that all samples represent valid *
  69. * addresses within our code so we get valid results we can use even when the IP is in system *
  70. * code. *
  71. * *
  72. *---------------------------------------------------------------------------------------------*
  73. * *
  74. * Functions: *
  75. * Start_Profiler -- initialises the profiler data and starts gathering data *
  76. * Stop_Profiler -- stops the timer and writes the profile data to disk *
  77. * *
  78. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  79. #define WIN32
  80. #ifndef _WIN32 // Denzil 6/2/98 Watcom 11.0 complains without this check
  81. #define _WIN32
  82. #endif // _WIN32
  83. #include <windows.h>
  84. #include <windowsx.h>
  85. #include <wwstd.h>
  86. #include <rawfile.h>
  87. #include <file.h>
  88. #include "profile.h"
  89. #include <vdmdbg.h>
  90. #include <timer.h>
  91. #define PROFILE
  92. extern "C"{
  93. #ifdef PROFILE
  94. unsigned ProfileList [PROFILE_RATE*MAX_PROFILE_TIME];
  95. #else
  96. unsigned ProfileList [2];
  97. #endif
  98. unsigned ProfilePtr;
  99. }
  100. extern "C" void Old_Profiler_Callback ( UINT, UINT , DWORD, DWORD, DWORD );
  101. extern "C" void New_Profiler_Callback (void);
  102. extern "C" {
  103. extern unsigned ProfileFunctionAddress;
  104. }
  105. unsigned long ProfilerEvent; //Handle for profiler callback
  106. unsigned long ProfilerThread; //Handle for profiler thread
  107. HANDLE CCThreadHandle;
  108. CONTEXT ThreadContext;
  109. #if (PROFILE_SYSTEM == NEW_PROFILE_SYSTEM)
  110. /***********************************************************************************************
  111. * Thread_Callback -- gets the IP address of our thread and registers it *
  112. * *
  113. * *
  114. * *
  115. * INPUT: Windows timer callback parms - not used *
  116. * *
  117. * OUTPUT: Nothing *
  118. * *
  119. * WARNINGS: None *
  120. * *
  121. * HISTORY: *
  122. * 1/2/96 6:37AM ST : Created *
  123. *=============================================================================================*/
  124. void CALLBACK Thread_Callback (UINT,UINT,DWORD,DWORD,DWORD)
  125. {
  126. ThreadContext.ContextFlags = VDMCONTEXT_CONTROL;
  127. if (!InTimerCallback){
  128. GetThreadContext ( CCThreadHandle , &ThreadContext );
  129. }else{
  130. GetThreadContext (TimerThreadHandle , &ThreadContext);
  131. }
  132. ProfileFunctionAddress = ThreadContext.Eip;
  133. New_Profiler_Callback();
  134. }
  135. /***********************************************************************************************
  136. * Profile_Thread -- this is the thread our profiler runs in. It just starts off a timer and *
  137. * then buggers off into an infinite message loop. We shouldnt get messages *
  138. * here as this isnt our primary thread *
  139. * *
  140. * INPUT: Nothing *
  141. * *
  142. * OUTPUT: Nothing *
  143. * *
  144. * WARNINGS: None *
  145. * *
  146. * HISTORY: *
  147. * 1/2/96 6:39AM ST : Created *
  148. *=============================================================================================*/
  149. void Profile_Thread (void)
  150. {
  151. MSG msg;
  152. ProfilerEvent = timeSetEvent (1000/PROFILE_RATE , 1 , Thread_Callback , 0 , TIME_PERIODIC);
  153. //ProfilerEvent = timeSetEvent (100 , 1 , Thread_Callback , 0 , TIME_ONESHOT);
  154. do {
  155. GetMessage(&msg,NULL,0,0);
  156. } while(1);
  157. }
  158. #endif //(PROFILE_SYSTEM == OLD_PROFILE_SYSTEM)
  159. /***********************************************************************************************
  160. * Start_Profiler -- initialises the profiler system and starts sampling *
  161. * *
  162. * *
  163. * *
  164. * INPUT: Nothing *
  165. * *
  166. * OUTPUT: Nothing *
  167. * *
  168. * WARNINGS: There may be a pause when sampling starts as Win95 does some VMM stuff *
  169. * *
  170. * HISTORY: *
  171. * 11/20/95 5:12PM ST : Created *
  172. *=============================================================================================*/
  173. void __cdecl Start_Profiler (void)
  174. {
  175. #ifdef PROFILE
  176. if (!ProfilerEvent){
  177. memset (&ProfileList[0],-1,PROFILE_RATE*MAX_PROFILE_TIME*4);
  178. }
  179. Profile_Init();
  180. if (!ProfilerEvent){
  181. #if (PROFILE_SYSTEM == OLD_PROFILE_SYSTEM)
  182. /*
  183. ** Old profile system - just set up a timer to monitor the global variable based on
  184. ** the last place __PRO was called from
  185. */
  186. ProfilerEvent = timeSetEvent (1000/PROFILE_RATE , 1 , (void CALLBACK (UINT,UINT,DWORD,DWORD,DWORD))Old_Profiler_Callback , 0 , TIME_PERIODIC);
  187. #else
  188. /*
  189. ** New profile system - create a second thread that will do all the profiling
  190. ** using GetThreadContext
  191. */
  192. if ( DuplicateHandle( GetCurrentProcess(), GetCurrentThread() , GetCurrentProcess() ,&CCThreadHandle , 0 , TRUE , DUPLICATE_SAME_ACCESS) ){
  193. ProfilerEvent= (unsigned)CreateThread(NULL,2048,(LPTHREAD_START_ROUTINE)&Profile_Thread,NULL,0,&ProfilerThread);
  194. }
  195. #endif
  196. }
  197. #else
  198. ProfilerEvent = 0;
  199. #endif
  200. }
  201. /***********************************************************************************************
  202. * Stop_Profiler -- stops the sampling timer and writes the colledted data to disk *
  203. * *
  204. * *
  205. * *
  206. * INPUT: Nothing *
  207. * *
  208. * OUTPUT: Nothing *
  209. * *
  210. * WARNINGS: Writes to file PROFILE.BIN *
  211. * *
  212. * HISTORY: *
  213. * 11/20/95 5:13PM ST : Created *
  214. *=============================================================================================*/
  215. void __cdecl Stop_Profiler (void)
  216. {
  217. if (ProfilerEvent){
  218. #if (PROFILE_SYSTEM == OLD_PROFILE_SYSTEM)
  219. //
  220. // Old system - just remove the timer event
  221. //
  222. timeKillEvent(ProfilerEvent);
  223. #else
  224. //
  225. // New system - kill the profiling thread
  226. //
  227. TerminateThread((HANDLE)ProfilerThread,0);
  228. #endif
  229. ProfilerEvent=NULL;
  230. Profile_End();
  231. int handle = Open_File ( "profile.bin" , WRITE );
  232. if (handle != WW_ERROR){
  233. Write_File (handle , &ProfileList[0] , ProfilePtr*4);
  234. Close_File (handle);
  235. }
  236. }
  237. }