hbtracy.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @file hbtracy.h
  3. * @brief Tracy profiler configuration and memory logging macros.
  4. *
  5. * $LicenseInfo:firstyear=2021&license=viewergpl$
  6. *
  7. * Copyright (c) 2021, Henri Beauchamp.
  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_HBTRACY_H
  33. #define LL_HBTRACY_H
  34. #if TRACY_ENABLE
  35. // Tracy configuration: this must match exactly the configuration used to
  36. // compile the Tracy library (since we use a pre-compiled library for the
  37. // server side linked to the viewer).
  38. //
  39. // Tracy server/profiler communications configuration:
  40. # define TRACY_ON_DEMAND 1
  41. # define TRACY_NO_BROADCAST 1
  42. # define TRACY_ONLY_LOCALHOST 1
  43. # define TRACY_ONLY_IPV4 1
  44. // No frame images capture:
  45. # define TRACY_NO_FRAME_IMAGE 1
  46. // For Debug and RelWithDebInfo builds, capture call stacks as well.
  47. # if LL_DEBUG || LL_NO_FORCE_INLINE
  48. // Capture three levels max.
  49. # define TRACY_CALLSTACK 3
  50. // Do not allow the collection of the program code.
  51. # define TRACY_NO_CODE_TRANSFER 1
  52. # endif
  53. # include "Tracy.hpp"
  54. // Allows to add messages to the trace: msg_str must be a std::string.
  55. # define LL_TRACY_MSG(msg_str) TracyMessage(msg_str.c_str(), msg_str.size())
  56. // Allows to add messages to the trace: msg must be a C-string litteral.
  57. # define LL_TRACY_MSGL(msg) TracyMessageL(msg)
  58. // This offers the possibility to add Tracy-only timers (e.g. for threads, or
  59. // parts of the code that are costly to time and we do not want to see slowed
  60. // down in release builds): the 'name' parameter can be anything (and shall not
  61. // be added to EFastTimerType) but, by convention, all such timers are named
  62. // following the TRC_* pattern in the viewer code.
  63. # define LL_TRACY_TIMER(name) ZoneScopedN(#name)
  64. // When TRACY_ENABLE is defined and greater than 2, we also enable fast timers.
  65. # if TRACY_ENABLE > 2
  66. # define LL_FAST_TIMERS_ENABLED 1
  67. # else
  68. # define LL_FAST_TIMERS_ENABLED 0
  69. # endif
  70. # if LL_FAST_TIMERS_ENABLED
  71. // Use both Tracy and fast timers
  72. # define LL_FAST_TIMER(name) LLFastTimer name(LLFastTimer::name); \
  73. ZoneScopedN(#name)
  74. # define LL_FAST_TIMERS(cond, name1, name2) \
  75. LLFastTimer ftm(cond ? LLFastTimer::name1 : LLFastTimer::name2); \
  76. ZoneScopedN(#name1)
  77. # else // LL_FAST_TIMERS_ENABLED
  78. // Replace fast timers with Tracy
  79. # define LL_FAST_TIMER(name) ZoneScopedN(#name)
  80. // Tracy only accepts constexpr parameters for zone names, so that name cannot
  81. // be made conditional. We simply use the first passed name, regardless of the
  82. // condition (we need two names for fast timers only when a (hard coded) dual-
  83. // parenting of that timer is needed; Tracy does not need this since parenting
  84. // is auto-determined).
  85. # define LL_FAST_TIMERS(cond, name1, name2) ZoneScopedN(#name1)
  86. # endif // LL_FAST_TIMERS_ENABLED
  87. // When TRACY_ENABLE is defined to 2 or 4, we also enable memory logging.
  88. // Note that only allocations done via the viewer custom allocators are
  89. // actually logged (which represents only part of the total used memory).
  90. // *TODO: use a malloc hook to log everything...
  91. # if TRACY_ENABLE == 2 || TRACY_ENABLE == 4
  92. # define LL_TRACY_ALLOC(ptr, size, name) if (ptr) TracyAllocN(ptr, size, name)
  93. # define LL_TRACY_FREE(ptr, name) if (ptr) TracyFreeN(ptr, name)
  94. // These string constant pointers are to be used for 'name' with the above two
  95. // defines, to flag the various memory pools/allocation types.
  96. extern const char* trc_mem_align;
  97. extern const char* trc_mem_align16;
  98. extern const char* trc_mem_image;
  99. // Only builds using jemalloc will see those used/reported (they will appear as
  100. // aligned memory types for the other builds):
  101. extern const char* trc_mem_volume;
  102. extern const char* trc_mem_volume64;
  103. extern const char* trc_mem_vertex;
  104. # else
  105. # define LL_TRACY_ALLOC(ptr, size, name)
  106. # define LL_TRACY_FREE(ptr, name)
  107. # endif
  108. #else // TRACY_ENABLE
  109. # define LL_FAST_TIMERS_ENABLED 1
  110. # define LL_FAST_TIMER(name) LLFastTimer name(LLFastTimer::name)
  111. # define LL_FAST_TIMERS(cond, name1, name2) \
  112. LLFastTimer ftm(cond ? LLFastTimer::name1 : LLFastTimer::name2)
  113. // This is a no-op for fast timers
  114. # define LL_TRACY_TIMER(name)
  115. // These are always no-operations when Tracy is not in use
  116. # define LL_TRACY_ALLOC(ptr, size, name)
  117. # define LL_TRACY_FREE(ptr, name)
  118. #endif // TRACY_ENABLE
  119. #endif // LL_HBTRACY_H