llfasttimer.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**
  2. * @file llfasttimer.h
  3. * @brief Declaration of a fast timer.
  4. *
  5. * $LicenseInfo:firstyear=2004&license=viewergpl$
  6. *
  7. * Copyright (c) 2004-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLFASTTIMER_H
  33. #define LL_LLFASTTIMER_H
  34. #include "hbtracy.h"
  35. #if LL_FAST_TIMERS_ENABLED
  36. #include "llthread.h" // For is_main_thread()
  37. // We only support the TSC for x86 CPUs, for now...
  38. #if SSE2NEON
  39. # define LL_FASTTIMER_USE_RDTSC 0
  40. #else
  41. # define LL_FASTTIMER_USE_RDTSC 1
  42. #endif
  43. # if LL_FASTTIMER_USE_RDTSC
  44. // Include the right header for __rdtsc()
  45. # if LL_WINDOWS
  46. # include <intrin.h>
  47. # else
  48. // Saddly, this header includes a shitload of other headers, which slows down
  49. // the compilation of modules including llfasttimer.h: worked around, for cmake
  50. // v3.16 and newer, by including <llfasttimer.h> in target_precompile_headers
  51. // for all projects making use of llfasttimer.h in 2 or more of their modules.
  52. # include <x86intrin.h>
  53. # endif
  54. # else
  55. # include "lltimer.h" // For getCurrentClockCount()
  56. # endif
  57. // Define to 1 to determine the maximum depth (last check done in July 2021
  58. // with 14 as the result). HB
  59. # define LL_FAST_TIMERS_CHECK_MAX_DEPTH 0
  60. // Maximum fast timers nesting (see the comment above)
  61. constexpr S32 FTM_MAX_DEPTH = 20;
  62. // Number of records in history
  63. constexpr S32 FTM_HISTORY_NUM = 256;
  64. class LLFastTimer
  65. {
  66. public:
  67. enum EFastTimerType
  68. {
  69. // High level
  70. FTM_FRAME,
  71. FTM_MEMORY_CHECK,
  72. FTM_RENDER,
  73. FTM_IDLE,
  74. FTM_POST_DISPLAY,
  75. // Common messaging components
  76. FTM_PUMP,
  77. FTM_PUMP_EVENT,
  78. FTM_PUMP_SERVICE,
  79. FTM_PUMP_IO,
  80. FTM_PROCESS_SOCKET_READER,
  81. FTM_PROCESS_SOCKET_WRITER,
  82. FTM_PROCESS_SERVER_SOCKET,
  83. FTM_PUMP_CALLBACK_CHAIN,
  84. // Common simulation components
  85. FTM_UPDATE_ANIMATION,
  86. FTM_UPDATE_HIDDEN_ANIMATION,
  87. FTM_UPDATE_MOTIONS,
  88. FTM_MOTION_ON_UPDATE,
  89. FTM_APPLY_MORPH_TARGET,
  90. FTM_POLYSKELETAL_DISTORTION_APPLY,
  91. FTM_UPDATE_TERRAIN,
  92. FTM_UPDATE_PRIMITIVES,
  93. FTM_UPDATE_PARTICLES,
  94. FTM_SIMULATE_PARTICLES,
  95. FTM_SIM_PART_SORT,
  96. FTM_UPDATE_SKY,
  97. FTM_UPDATE_TEXTURES,
  98. FTM_UPDATE_WLPARAM,
  99. FTM_UPDATE_WATER,
  100. FTM_UPDATE_CLOUDS,
  101. FTM_UPDATE_GRASS,
  102. FTM_UPDATE_TREE,
  103. // Common render components
  104. FTM_IMPOSTORS_UPDATE,
  105. FTM_IMPOSTOR_MARK_VISIBLE,
  106. FTM_IMPOSTOR_SETUP,
  107. FTM_IMPOSTOR_ALLOCATE,
  108. FTM_IMPOSTOR_RESIZE,
  109. FTM_IMPOSTOR_BACKGROUND,
  110. FTM_GEN_SUN_SHADOW,
  111. FTM_BIND_DEFERRED,
  112. FTM_RENDER_DEFERRED,
  113. FTM_ATMOSPHERICS,
  114. FTM_SUN_SHADOW,
  115. FTM_SOFTEN_SHADOW,
  116. FTM_LOCAL_LIGHTS,
  117. FTM_PROJECTORS,
  118. FTM_FULLSCREEN_LIGHTS,
  119. FTM_SHADOW_RENDER,
  120. FTM_SHADOW_TERRAIN,
  121. FTM_SHADOW_AVATAR,
  122. FTM_SHADOW_SIMPLE,
  123. FTM_SHADOW_ALPHA,
  124. FTM_SHADOW_TREE,
  125. FTM_RENDER_GEOMETRY,
  126. FTM_RENDER_TERRAIN,
  127. FTM_AVATAR_FACE,
  128. FTM_RENDER_SIMPLE,
  129. FTM_RENDER_FULLBRIGHT,
  130. FTM_RENDER_GLOW,
  131. FTM_RENDER_GRASS,
  132. FTM_RENDER_INVISIBLE,
  133. FTM_RENDER_SHINY,
  134. FTM_RENDER_BUMP,
  135. FTM_RENDER_MATERIALS,
  136. FTM_RENDER_TREES,
  137. FTM_VOLUME_GEOM,
  138. FTM_FACE_GET_GEOM,
  139. FTM_FACE_GEOM_INDEX,
  140. FTM_FACE_GEOM_POSITION,
  141. FTM_FACE_GEOM_COLOR,
  142. FTM_FACE_GEOM_EMISSIVE,
  143. FTM_FACE_GEOM_NORMAL,
  144. FTM_FACE_GEOM_TANGENT,
  145. FTM_FACE_GEOM_WEIGHTS,
  146. FTM_FACE_GEOM_TEXTURE,
  147. FTM_RENDER_CHARACTERS,
  148. FTM_RENDER_AVATARS,
  149. FTM_RIGGED_VBO,
  150. FTM_RENDER_OCCLUSION,
  151. FTM_OCCLUSION_ALLOCATE,
  152. FTM_PUSH_OCCLUSION_VERTS,
  153. FTM_OCCLUSION_BEGIN_QUERY,
  154. FTM_OCCLUSION_DRAW_WATER,
  155. FTM_OCCLUSION_DRAW,
  156. FTM_OCCLUSION_END_QUERY,
  157. FTM_RENDER_ALPHA,
  158. FTM_RENDER_CLOUDS,
  159. FTM_RENDER_WATER,
  160. FTM_RENDER_WL_SKY,
  161. FTM_VISIBLE_CLOUD,
  162. FTM_RENDER_TIMER,
  163. FTM_RENDER_UI,
  164. FTM_RENDER_SPELLCHECK,
  165. FTM_REBUILD_GROUPS,
  166. FTM_RESET_VB,
  167. FTM_RENDER_BLOOM,
  168. FTM_RENDER_FONTS_BATCHED,
  169. FTM_RENDER_FONTS_SERIALIZED,
  170. FTM_RESIZE_SCREEN_TEXTURE,
  171. FTM_UPDATE_GL,
  172. // newview specific
  173. FTM_MESSAGES,
  174. FTM_MOUSEHANDLER,
  175. FTM_KEYHANDLER,
  176. FTM_STATESORT,
  177. FTM_STATESORT_DRAWABLE,
  178. FTM_STATESORT_POSTSORT,
  179. FTM_REBUILD_PRIORITY_GROUPS,
  180. FTM_REBUILD_MESH,
  181. FTM_REBUILD_VBO,
  182. FTM_ADD_GEOMETRY_COUNT,
  183. FTM_CREATE_VB,
  184. FTM_GET_GEOMETRY,
  185. FTM_REBUILD_VOLUME_FACE_LIST,
  186. FTM_VOLUME_TEXTURES,
  187. FTM_REBUILD_VOLUME_GEN_DRAW_INFO,
  188. FTM_GEN_DRAW_INFO_SORT,
  189. FTM_GEN_DRAW_INFO_FACE_SIZE,
  190. FTM_REGISTER_FACE,
  191. FTM_REBUILD_GRASS_VB,
  192. FTM_REBUILD_TERRAIN_VB,
  193. FTM_REBUILD_PARTICLE_VBO,
  194. FTM_REBUILD_PARTICLE_GEOM,
  195. FTM_POOLS,
  196. FTM_POOLRENDER,
  197. FTM_IDLE_CB,
  198. FTM_MEDIA_UPDATE,
  199. FTM_MEDIA_UPDATE_INTEREST,
  200. FTM_MEDIA_CALCULATE_INTEREST,
  201. FTM_MEDIA_SORT,
  202. FTM_MEDIA_MISC,
  203. FTM_MEDIA_SORT2,
  204. FTM_MEDIA_GET_DATA,
  205. FTM_MEDIA_SET_SUBIMAGE,
  206. FTM_MEDIA_DO_UPDATE,
  207. FTM_MATERIALS_IDLE,
  208. FTM_IDLE_CB_RADAR,
  209. FTM_WORLD_UPDATE,
  210. FTM_UPDATE_MOVE,
  211. FTM_OCTREE_BALANCE,
  212. FTM_CULL,
  213. FTM_CULL_VOCACHE,
  214. FTM_CULL_REBOUND,
  215. FTM_FRUSTUM_CULL,
  216. FTM_OCCLUSION_EARLY_FAIL,
  217. FTM_DISPLAY_UPDATE_GEOM,
  218. FTM_GEO_UPDATE,
  219. FTM_GEO_SKY,
  220. FTM_GEN_VOLUME,
  221. FTM_GEN_TRIANGLES,
  222. FTM_GEN_FLEX,
  223. FTM_DO_FLEXIBLE_UPDATE,
  224. FTM_FLEXIBLE_REBUILD,
  225. FTM_PROCESS_PARTITIONQ,
  226. FTM_PIPELINE_CREATE,
  227. FTM_AUDIO_UPDATE,
  228. FTM_RESET_DRAWORDER,
  229. FTM_OBJECTLIST_UPDATE,
  230. FTM_OBJECTLIST_COPY,
  231. FTM_AVATAR_UPDATE,
  232. FTM_AV_CHECK_TEX_LOADING,
  233. FTM_AV_RELEASE_OLD_TEX,
  234. FTM_AV_UPDATE_TEXTURES,
  235. FTM_JOINT_UPDATE,
  236. FTM_PHYSICS_UPDATE,
  237. FTM_ATTACHMENT_UPDATE,
  238. FTM_LOD_UPDATE,
  239. FTM_CULL_AVATARS,
  240. FTM_UPDATE_RIGGED_VOLUME,
  241. FTM_RIGGED_OCTREE,
  242. FTM_AREASEARCH_UPDATE,
  243. FTM_REGION_UPDATE,
  244. FTM_UPD_LANDPATCHES,
  245. FTM_UPD_PARCELOVERLAY,
  246. FTM_UPD_CACHEDOBJECTS,
  247. FTM_CLEANUP,
  248. FTM_CLEANUP_DRAWABLE,
  249. FTM_UNLINK,
  250. FTM_REMOVE_FROM_LIGHT_SET,
  251. FTM_REMOVE_FROM_MOVE_LIST,
  252. FTM_REMOVE_FROM_SPATIAL_PARTITION,
  253. FTM_RLV,
  254. FTM_IDLE_LUA_THREAD,
  255. FTM_NETWORK,
  256. FTM_IDLE_NETWORK,
  257. FTM_CREATE_OBJECT,
  258. // FTM_LOAD_AVATAR,
  259. FTM_PROCESS_MESSAGES,
  260. FTM_PROCESS_OBJECTS,
  261. FTM_PROCESS_IMAGES,
  262. FTM_SHIFT_OBJECTS,
  263. FTM_PIPELINE_SHIFT,
  264. FTM_SHIFT_DRAWABLE,
  265. FTM_SHIFT_OCTREE,
  266. FTM_SHIFT_HUD,
  267. FTM_REGION_SHIFT,
  268. FTM_IMAGE_UPDATE,
  269. FTM_IMAGE_UPDATE_CLASS,
  270. FTM_IMAGE_UPDATE_BUMP,
  271. FTM_IMAGE_UPDATE_LIST,
  272. FTM_IMAGE_CALLBACKS,
  273. FTM_BUMP_SOURCE_STANDARD_LOADED,
  274. FTM_BUMP_GEN_NORMAL,
  275. FTM_BUMP_CREATE_TEXTURE,
  276. FTM_BUMP_SOURCE_LOADED,
  277. FTM_BUMP_SOURCE_ENTRIES_UPDATE,
  278. FTM_BUMP_SOURCE_MIN_MAX,
  279. FTM_BUMP_SOURCE_RGB2LUM,
  280. FTM_BUMP_SOURCE_RESCALE,
  281. FTM_BUMP_SOURCE_CREATE,
  282. FTM_BUMP_SOURCE_GEN_NORMAL,
  283. FTM_IMAGE_CREATE,
  284. FTM_IMAGE_UPDATE_PRIO,
  285. FTM_IMAGE_FETCH,
  286. FTM_IMAGE_MARK_DIRTY,
  287. FTM_IMAGE_STATS,
  288. FTM_TEXTURE_UNBIND,
  289. FTM_VFILE_WAIT,
  290. FTM_FLEXIBLE_UPDATE,
  291. FTM_OCCLUSION_WAIT,
  292. FTM_OCCLUSION_READBACK,
  293. FTM_SET_OCCLUSION_STATE,
  294. FTM_HUD_UPDATE,
  295. FTM_HUD_EFFECTS,
  296. FTM_HUD_OBJECTS,
  297. FTM_SWAP,
  298. FTM_INVENTORY,
  299. FTM_AUTO_SELECT,
  300. FTM_ARRANGE,
  301. FTM_FILTER,
  302. FTM_REFRESH,
  303. FTM_SORT,
  304. FTM_PICK,
  305. FTM_TEXTURE_CACHE,
  306. FTM_DECODE,
  307. FTM_SLEEP,
  308. FTM_FPS_LIMITING,
  309. FTM_FETCH,
  310. FTM_OTHER, // Special, used by display code
  311. FTM_NUM_TYPES
  312. };
  313. public:
  314. LLFastTimer() = delete;
  315. LLFastTimer(const LLFastTimer&) = delete;
  316. LL_INLINE LLFastTimer(EFastTimerType type) noexcept
  317. {
  318. mActive = sFastTimersEnabled && is_main_thread() &&
  319. sCurDepth < FTM_MAX_DEPTH;
  320. if (mActive)
  321. {
  322. sType[sCurDepth] = sCurType; // Store the previous type
  323. sCurType = type;
  324. sStart[sCurDepth++] = getCPUClockCount();
  325. #if LL_FAST_TIMERS_CHECK_MAX_DEPTH
  326. checkMaxDepth();
  327. #endif
  328. }
  329. }
  330. LL_INLINE ~LLFastTimer()
  331. {
  332. if (mActive)
  333. {
  334. U64 delta = getCPUClockCount() - sStart[--sCurDepth];
  335. sCounter[sCurType] += delta;
  336. ++sCalls[sCurType];
  337. sCurType = sType[sCurDepth]; // Restore the previous type
  338. // Subtract delta from parents
  339. for (S32 i = 0; i < sCurDepth; ++i)
  340. {
  341. sStart[i] += delta;
  342. }
  343. }
  344. }
  345. static void enabledFastTimers(bool enable);
  346. static bool fastTimersEnabled() { return sFastTimersEnabled; }
  347. static void reset();
  348. static U64 countsPerSecond() { return sClockResolution; }
  349. private:
  350. LL_INLINE static U64 getCPUClockCount()
  351. {
  352. #if LL_FASTTIMER_USE_RDTSC // Fast, TSC-based implementation.
  353. # if 0 // Useless for the ~1µs resolution we need.
  354. _mm_lfence();
  355. # endif
  356. return (U64)__rdtsc();
  357. #else // Slower, non TSC function.
  358. return LLTimer::getCurrentClockCount();
  359. #endif
  360. }
  361. #if LL_FAST_TIMERS_CHECK_MAX_DEPTH
  362. LL_NO_INLINE static void checkMaxDepth();
  363. #endif
  364. private:
  365. bool mActive;
  366. static U64 sClockResolution;
  367. static EFastTimerType sType[FTM_MAX_DEPTH];
  368. static EFastTimerType sCurType;
  369. static S32 sCurDepth;
  370. static U64 sStart[FTM_MAX_DEPTH];
  371. #if LL_FAST_TIMERS_CHECK_MAX_DEPTH
  372. static S32 sMaxDepth;
  373. #endif
  374. static bool sFastTimersEnabled;
  375. public:
  376. static bool sPauseHistory;
  377. static bool sResetHistory;
  378. static S32 sCurFrameIndex;
  379. static S32 sLastFrameIndex;
  380. static U64 sCounter[FTM_NUM_TYPES];
  381. static U64 sCalls[FTM_NUM_TYPES];
  382. static U64 sCountAverage[FTM_NUM_TYPES];
  383. static U64 sCallAverage[FTM_NUM_TYPES];
  384. static U64 sCountHistory[FTM_HISTORY_NUM][FTM_NUM_TYPES];
  385. static U64 sCallHistory[FTM_HISTORY_NUM][FTM_NUM_TYPES];
  386. };
  387. #endif // LL_FAST_TIMERS_ENABLED
  388. #endif // LL_LLFASTTIMER_H