profile_funclevel.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. /////////////////////////////////////////////////////////////////////////EA-V1
  19. // $File: //depot/GeneralsMD/Staging/code/Libraries/Source/profile/profile_funclevel.h $
  20. // $Author: mhoffe $
  21. // $Revision: #3 $
  22. // $DateTime: 2003/07/09 10:57:23 $
  23. //
  24. // ©2003 Electronic Arts
  25. //
  26. // Function level profiling
  27. //////////////////////////////////////////////////////////////////////////////
  28. #ifdef _MSC_VER
  29. # pragma once
  30. #endif
  31. #ifndef PROFILE_FUNCLEVEL_H // Include guard
  32. #define PROFILE_FUNCLEVEL_H
  33. /**
  34. \brief The function level profiler.
  35. Note that this class exists even if the current build configuration
  36. is not _PROFILE. In these cases all calls will simply return
  37. empty data.
  38. */
  39. class ProfileFuncLevel
  40. {
  41. friend class Profile;
  42. // no, no copying allowed!
  43. ProfileFuncLevel(const ProfileFuncLevel&);
  44. ProfileFuncLevel& operator=(const ProfileFuncLevel&);
  45. public:
  46. class Id;
  47. class Thread;
  48. /// \brief A list of function level profile IDs
  49. class IdList
  50. {
  51. friend Id;
  52. public:
  53. IdList(void): m_ptr(0) {}
  54. /**
  55. \brief Enumerates the list of IDs.
  56. \note These values are not sorted in any way.
  57. \param index index value, >=0
  58. \param id return buffer for ID value
  59. \param countPtr return buffer for count, if given
  60. \return true if ID found at given index, false if not
  61. */
  62. bool Enum(unsigned index, Id &id, unsigned *countPtr=0) const;
  63. private:
  64. /// internal value
  65. void *m_ptr;
  66. };
  67. /// \brief A function level profile ID.
  68. class Id
  69. {
  70. friend IdList;
  71. friend Thread;
  72. public:
  73. Id(void): m_funcPtr(0) {}
  74. /// special 'frame' numbers
  75. enum
  76. {
  77. /// return the total value/count
  78. Total = 0xffffffff
  79. };
  80. /**
  81. \brief Returns the source file this Id is in.
  82. \return source file name, may be NULL
  83. */
  84. const char *GetSource(void) const;
  85. /**
  86. \brief Returns the function name for this Id.
  87. \return function name, may be NULL
  88. */
  89. const char *GetFunction(void) const;
  90. /**
  91. \brief Returns function address.
  92. \return function address
  93. */
  94. unsigned GetAddress(void) const;
  95. /**
  96. \brief Returns the line number for this Id.
  97. \return line number, 0 if unknown
  98. */
  99. unsigned GetLine(void) const;
  100. /**
  101. \brief Determine call counts.
  102. \param frame number of recorded frame, or Total
  103. \return number of calls
  104. */
  105. unsigned _int64 GetCalls(unsigned frame) const;
  106. /**
  107. \brief Determine time spend in this function and its children.
  108. \param frame number of recorded frame, or Total
  109. \return time spend (in CPU ticks)
  110. */
  111. unsigned _int64 GetTime(unsigned frame) const;
  112. /**
  113. \brief Determine time spend in this function only (exclude
  114. any time spend in child functions).
  115. \param frame number of recorded frame, or Total
  116. \return time spend in this function alone (in CPU ticks)
  117. */
  118. unsigned _int64 GetFunctionTime(unsigned frame) const;
  119. /**
  120. \brief Determine the list of caller Ids.
  121. \param frame number of recorded frame, or Total
  122. \return Caller Id list (actually just a handle value)
  123. */
  124. IdList GetCaller(unsigned frame) const;
  125. private:
  126. /// internal function pointer
  127. void *m_funcPtr;
  128. };
  129. /// \brief a profiled thread
  130. class Thread
  131. {
  132. friend ProfileFuncLevel;
  133. public:
  134. Thread(void): m_threadID(0) {}
  135. /**
  136. \brief Enumerates the list of known function level profile values.
  137. \note These values are not sorted in any way.
  138. \param index index value, >=0
  139. \param id return buffer for ID value
  140. \return true if ID found at given index, false if not
  141. */
  142. bool EnumProfile(unsigned index, Id &id) const;
  143. /**
  144. \brief Returns a unique thread ID (not related to Windows thread ID)
  145. \return profile thread ID
  146. */
  147. unsigned GetId(void) const
  148. {
  149. return unsigned(m_threadID);
  150. }
  151. private:
  152. /// internal thread ID
  153. class ProfileFuncLevelTracer *m_threadID;
  154. };
  155. /**
  156. \brief Enumerates the list of known and profiled threads.
  157. \note These values are not sorted in any way.
  158. \param index index value, >=0
  159. \param thread return buffer for thread handle
  160. \return true if Thread found, false if not
  161. */
  162. static bool EnumThreads(unsigned index, Thread &thread);
  163. private:
  164. /** \internal
  165. Undocumented default constructor. Initializes function level profiler.
  166. We can make this private as well so nobody accidently tries to create
  167. another instance.
  168. */
  169. ProfileFuncLevel(void);
  170. /**
  171. \brief The only function level profiler instance.
  172. */
  173. static ProfileFuncLevel Instance;
  174. };
  175. #endif // PROFILE_FUNCLEVEL_H