PROFILE.CPP 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 : PROFILE.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. * The profiler works by using the function prologue and epilogue hooks available in Watcom *
  35. * to register the current functions address in a global variable and then sampling the *
  36. * contents of the variable using a windows timer which runs at up to 1000 samples per second. *
  37. * *
  38. * Compile the code to be sampled with the -ep and -ee flags to enable the prologue (__PRO) *
  39. * and epilogue (__EPI) calls to be generated. *
  40. * At the beginning of the section to be profiled (just before main loop normally) call the *
  41. * Start_Profiler function to start sampling. At the end of the section, call Stop_Profiler *
  42. * which will stop the timer and write the profile data to disk in the PROFILE.BIN file. *
  43. * Use *
  44. * *
  45. *---------------------------------------------------------------------------------------------*
  46. * *
  47. * Functions: *
  48. * Start_Profiler -- initialises the profiler data and starts gathering data *
  49. * Stop_Profiler -- stops the timer and writes the profile data to disk *
  50. * *
  51. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  52. #define WIN32
  53. #ifndef _WIN32 // Denzil 6/2/98 Watcom 11.0 complains without this check
  54. #define _WIN32
  55. #endif // _WIN32
  56. #include <windows.h>
  57. #include <windowsx.h>
  58. #include <wwstd.h>
  59. #include <rawfile.h>
  60. #include <file.h>
  61. #include "profile.h"
  62. extern "C"{
  63. unsigned ProfileList [PROFILE_RATE*60*MAX_PROFILE_TIME];
  64. unsigned ProfilePtr;
  65. }
  66. extern "C" void Profiler_Callback ( UINT, UINT , DWORD, DWORD, DWORD );
  67. unsigned ProfilerEvent;
  68. void Start_Profiler (void)
  69. {
  70. memset (&ProfileList[0],-1,PROFILE_RATE*60*MAX_PROFILE_TIME*4);
  71. Copy_CHK();
  72. ProfilerEvent = timeSetEvent (1000/PROFILE_RATE , 1 , (void CALLBACK (UINT,UINT,DWORD,DWORD,DWORD))Profiler_Callback , 0 , TIME_PERIODIC);
  73. }
  74. void Stop_Profiler (void)
  75. {
  76. if (ProfilerEvent){
  77. timeKillEvent(ProfilerEvent);
  78. ProfilerEvent=NULL;
  79. int handle = Open_File ( "profile.bin" , WRITE );
  80. if (handle != WW_ERROR){
  81. Write_File (handle , &ProfileList[0] , ProfilePtr*4);
  82. Close_File (handle);
  83. }
  84. }
  85. }