cmake-instrumentation.7.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. .. cmake-manual-description: CMake Instrumentation
  2. cmake-instrumentation(7)
  3. ************************
  4. .. versionadded:: 4.0
  5. .. only:: html
  6. .. contents::
  7. Introduction
  8. ============
  9. .. note::
  10. This feature is only available when experimental support for instrumentation
  11. has been enabled by the ``CMAKE_EXPERIMENTAL_INSTRUMENTATION`` gate.
  12. The CMake Instrumentation API allows for the collection of timing data, target
  13. information and system diagnostic information during the configure, generate,
  14. build, test and install steps for a CMake project.
  15. This feature is only available for projects using the :ref:`Makefile Generators`
  16. or the :ref:`Ninja Generators`.
  17. All interactions with the CMake instrumentation API must specify both an API
  18. version and a Data version. At this time, there is only one version for each of
  19. these: the `API v1`_ and `Data v1`_.
  20. Data Collection
  21. ---------------
  22. Whenever a command is executed with
  23. instrumentation enabled, a `v1 Snippet File`_ is created in the project build
  24. tree with data specific to that command. These files remain until after
  25. `Indexing`_ occurs.
  26. CMake sets the :prop_gbl:`RULE_LAUNCH_COMPILE`, :prop_gbl:`RULE_LAUNCH_LINK` and
  27. :prop_gbl:`RULE_LAUNCH_CUSTOM` global properties to use the
  28. ``ctest --instrument`` launcher in order to capture details of each compile, link
  29. and custom command respectively. If the project has been configured with :module:`CTestUseLaunchers`,
  30. ``ctest --instrument`` will also include the behavior usually performed by
  31. ``ctest --launch``.
  32. Indexing
  33. --------
  34. Indexing is the process of collating generated instrumentation data. Indexing
  35. occurs at specific intervals called hooks, such as after every build. These
  36. hooks are configured as part of the `v1 Query Files`_. Whenever a hook is
  37. triggered, an index file is generated containing a list of snippet files newer
  38. than the previous indexing.
  39. Indexing and can also be performed by manually invoking
  40. ``ctest --collect-instrumentation``.
  41. Callbacks
  42. ---------
  43. As part of the `v1 Query Files`_, users can provide a list of callbacks
  44. intended to handle data collected by this feature.
  45. Whenever `Indexing`_ occurs, each provided callback is executed, passing the
  46. path to the generated index file as an argument.
  47. These callbacks, defined either at the user-level or project-level should read
  48. the instrumentation data and perform any desired handling of it. The index file
  49. and its listed snippets are automatically deleted by CMake once all callbacks
  50. have completed. Note that a callback should never move or delete these data
  51. files manually as they may be needed by other callbacks.
  52. Enabling Instrumentation
  53. ========================
  54. Instrumentation can be enabled either for an individual CMake project, or
  55. for all CMake projects configured and built by a user. For both cases,
  56. see the `v1 Query Files`_ for details on configuring this feature.
  57. Enabling Instrumentation at the Project-Level
  58. ---------------------------------------------
  59. Project code can contain instrumentation queries with the
  60. :command:`cmake_instrumentation` command.
  61. In addition, query files can be placed manually under
  62. ``<build>/.cmake/instrumentation/<version>/query/`` at the top of a build tree.
  63. This version of CMake supports only one version schema, `API v1`_.
  64. Enabling Instrumentation at the User-Level
  65. ------------------------------------------
  66. Instrumentation can be configured at the user-level by placing query files in
  67. the :envvar:`CMAKE_CONFIG_DIR` under
  68. ``<config_dir>/instrumentation/<version>/query/``.
  69. .. _`cmake-instrumentation API v1`:
  70. API v1
  71. ======
  72. The API version specifies both the subdirectory layout of the instrumentation data,
  73. and the format of the query files.
  74. The Instrumentation API v1 is housed in the ``instrumentation/v1/`` directory
  75. under either ``<build>/.cmake/`` for output data and project-level queries, or
  76. ``<config_dir>/`` for user-level queries. The ``v1`` component of this
  77. directory is what signifies the API version. It has the following
  78. subdirectories:
  79. ``query/``
  80. Holds query files written by users or clients. Any file with the ``.json``
  81. file extension will be recognized as a query file. These files are owned by
  82. whichever client or user creates them.
  83. ``query/generated/``
  84. Holds query files generated by a CMake project with the
  85. :command:`cmake_instrumentation` command. These files are owned by CMake and
  86. are deleted and regenerated automatically during the CMake configure step.
  87. ``data/``
  88. Holds instrumentation data collected on the project. CMake owns all data
  89. files, they should never be removed by other processes. Data collected here
  90. remains until after `Indexing`_ occurs and all `Callbacks`_ are executed.
  91. .. _`cmake-instrumentation v1 Query Files`:
  92. v1 Query Files
  93. --------------
  94. Any file with the ``.json`` extension under the ``instrumentation/v1/query/``
  95. directory is recognized as a query for instrumentation data.
  96. These files must contain a JSON object with the following keys. The ``version``
  97. key is required, but all other fields are optional.
  98. ``version``
  99. The Data version of snippet file to generate, an integer. Currently the only
  100. supported version is ``1``.
  101. ``callbacks``
  102. A list of command-line strings for `Callbacks`_ to handle collected
  103. instrumentation data. Whenever these callbacks are executed, the full path to
  104. a `v1 Index File`_ is appended to the arguments included in the string.
  105. ``hooks``
  106. A list of strings specifying when `Indexing`_ should occur automatically.
  107. These are the intervals when instrumentation data should be collated and user
  108. `Callbacks`_ should be invoked to handle the data. Elements in this list
  109. should be one of the following:
  110. * ``postGenerate``
  111. * ``preBuild`` (called when ``ninja`` or ``make`` is invoked; unavailable on Windows)
  112. * ``postBuild`` (called when ``ninja`` or ``make`` completes; unavailable on Windows)
  113. * ``preCMakeBuild`` (called when ``cmake --build`` is invoked)
  114. * ``postCMakeBuild`` (called when ``cmake --build`` completes)
  115. * ``postInstall``
  116. * ``postTest``
  117. ``queries``
  118. A list of strings specifying additional optional data to collect during
  119. instrumentation. Elements in this list should be one of the following:
  120. ``staticSystemInformation``
  121. Enables collection of the static information about the host machine CMake
  122. is being run from. This data is collected during `Indexing`_ and is
  123. included in the generated `v1 Index File`_.
  124. ``dynamicSystemInformation``
  125. Enables collection of the dynamic information about the host machine
  126. CMake is being run from. Data is collected for every `v1 Snippet File`_
  127. generated by CMake, and includes information from immediately before and
  128. after the command is executed.
  129. The ``callbacks`` listed will be invoked during the specified hooks
  130. *at a minimum*. When there are multiple query files, the ``callbacks``,
  131. ``hooks`` and ``queries`` between them will be merged. Therefore, if any query
  132. file includes any ``hooks``, every ``callback`` across all query files will be
  133. executed at every ``hook`` across all query files. Additionally, if any query
  134. file includes any optional ``queries``, the optional query data will be present
  135. in all data files.
  136. Example:
  137. .. code-block:: json
  138. {
  139. "version": 1,
  140. "callbacks": [
  141. "/usr/bin/python callback.py",
  142. "/usr/bin/cmake -P callback.cmake arg",
  143. ],
  144. "hooks": [
  145. "postCMakeBuild",
  146. "postInstall"
  147. ],
  148. "queries": [
  149. "staticSystemInformation",
  150. "dynamicSystemInformation"
  151. ]
  152. }
  153. In this example, after every ``cmake --build`` or ``cmake --install``
  154. invocation, an index file ``index-<hash>.json`` will be generated in
  155. ``<build>/.cmake/instrumentation/v1/data`` containing a list of data snippet
  156. files created since the previous indexing. The commands
  157. ``/usr/bin/python callback.py index-<hash>.json`` and
  158. ``/usr/bin/cmake -P callback.cmake arg index-<hash>.json`` will be executed in
  159. that order. The index file will contain the ``staticSystemInformation`` data and
  160. each snippet file listed in the index will contain the
  161. ``dynamicSystemInformation`` data. Once both callbacks have completed, the index
  162. file and all snippet files listed by it will be deleted from the project build
  163. tree.
  164. .. _`cmake-instrumentation Data v1`:
  165. Data v1
  166. =======
  167. Data version specifies the contents of the output files generated by the CMake
  168. instrumentation API as part of the `Data Collection`_ and `Indexing`_. There are
  169. two types of data files generated: the `v1 Snippet File`_ and `v1 Index File`_.
  170. When using the `API v1`_, these files live in
  171. ``<build>/.cmake/instrumentation/v1/data/`` under the project build tree.
  172. v1 Snippet File
  173. ---------------
  174. Snippet files are generated for every compile, link and custom command invoked
  175. as part of the CMake build or install step and contain instrumentation data about
  176. the command executed. Additionally, snippet files are created for the following:
  177. * The CMake configure step
  178. * The CMake generate step
  179. * Entire build step (executed with ``cmake --build``)
  180. * Entire install step (executed with ``cmake --install``)
  181. * Each ``ctest`` invocation
  182. * Each individual test executed by ``ctest``.
  183. These files remain in the build tree until after `Indexing`_ occurs and any
  184. user-specified `Callbacks`_ are executed.
  185. Snippet files have a filename with the syntax ``<role>-<timestamp>-<hash>.json``
  186. and contain the following data:
  187. ``version``
  188. The Data version of the snippet file, an integer. Currently the version is
  189. always ``1``.
  190. ``command``
  191. The full command executed. Excluded when ``role`` is ``build``.
  192. ``result``
  193. The exit-value of the command, an integer.
  194. ``role``
  195. The type of command executed, which will be one of the following values:
  196. * ``configure``: the CMake configure step
  197. * ``generate``: the CMake generate step
  198. * ``compile``: an individual compile step invoked during the build
  199. * ``link``: an individual link step invoked during the build
  200. * ``custom``: an individual custom command invoked during the build
  201. * ``build``: a complete ``make`` or ``ninja`` invocation. Only generated if ``preBuild`` or ``postBuild`` hooks are enabled.
  202. * ``cmakeBuild``: a complete ``cmake --build`` invocation
  203. * ``cmakeInstall``: a complete ``cmake --install`` invocation
  204. * ``install``: an individual ``cmake -P cmake_install.cmake`` invocation
  205. * ``ctest``: a complete ``ctest`` invocation
  206. * ``test``: a single test executed by CTest
  207. ``target``
  208. The CMake target associated with the command. Only included when ``role`` is
  209. one of ``compile``, ``link``, ``custom``.
  210. ``targetType``
  211. The :prop_tgt:`TYPE` of the target. Only included when ``role`` is
  212. ``link``.
  213. ``targetLabels``
  214. The :prop_tgt:`LABELS` of the target. Only included when ``role`` is
  215. ``link``.
  216. ``timeStart``
  217. Time at which the command started, expressed as the number of milliseconds
  218. since the system epoch.
  219. ``duration``
  220. The duration that the command ran for, expressed in milliseconds.
  221. ``outputs``
  222. The command's output file(s), an array. Only included when ``role`` is one
  223. of: ``compile``, ``link``, ``custom``.
  224. ``outputSizes``
  225. The size(s) in bytes of the ``outputs``, an array. For files which do not
  226. exist, the size is 0. Included under the same conditions as the ``outputs``
  227. field.
  228. ``source``
  229. The source file being compiled. Only included when ``role`` is ``compile``.
  230. ``language``
  231. The language of the source file being compiled. Only included when ``role`` is
  232. ``compile``.
  233. ``testName``
  234. The name of the test being executed. Only included when ``role`` is ``test``.
  235. ``config``
  236. The type of build, such as ``Release`` or ``Debug``. Only included when
  237. ``role`` is ``compile``, ``link`` or ``test``.
  238. ``dynamicSystemInformation``
  239. Specifies the dynamic information collected about the host machine
  240. CMake is being run from. Data is collected for every snippet file
  241. generated by CMake, with data immediately before and after the command is
  242. executed. Only included when enabled by the `v1 Query Files`_.
  243. ``beforeHostMemoryUsed``
  244. The Host Memory Used in KiB at ``timeStart``.
  245. ``afterHostMemoryUsed``
  246. The Host Memory Used in KiB at ``timeStop``.
  247. ``beforeCPULoadAverage``
  248. The Average CPU Load at ``timeStart``.
  249. ``afterCPULoadAverage``
  250. The Average CPU Load at ``timeStop``.
  251. Example:
  252. .. code-block:: json
  253. {
  254. "version": 1,
  255. "command" : "/usr/bin/c++ -MD -MT CMakeFiles/main.dir/main.cxx.o -MF CMakeFiles/main.dir/main.cxx.o.d -o CMakeFiles/main.dir/main.cxx.o -c <src>/main.cxx",
  256. "role" : "compile",
  257. "return" : 1,
  258. "target": "main",
  259. "language" : "C++",
  260. "outputs" : [ "CMakeFiles/main.dir/main.cxx.o" ],
  261. "outputSizes" : [ 0 ],
  262. "source" : "<src>/main.cxx",
  263. "config" : "Debug",
  264. "dynamicSystemInformation" :
  265. {
  266. "afterCPULoadAverage" : 2.3500000000000001,
  267. "afterHostMemoryUsed" : 6635680.0
  268. "beforeCPULoadAverage" : 2.3500000000000001,
  269. "beforeHostMemoryUsed" : 6635832.0
  270. },
  271. "timeStart" : 1737053448177,
  272. "duration" : 31
  273. }
  274. v1 Index File
  275. -------------
  276. Index files contain a list of `v1 Snippet File`_. It serves as an entry point
  277. for navigating the instrumentation data. They are generated whenever `Indexing`_
  278. occurs and deleted after any user-specified `Callbacks`_ are executed.
  279. ``version``
  280. The Data version of the index file, an integer. Currently the version is
  281. always ``1``.
  282. ``buildDir``
  283. The build directory of the CMake project.
  284. ``dataDir``
  285. The full path to the ``<build>/.cmake/instrumentation/v1/data/`` directory.
  286. ``hook``
  287. The name of the hook responsible for generating the index file. In addition
  288. to the hooks that can be specified by one of the `v1 Query Files`_, this value may
  289. be set to ``manual`` if indexing is performed by invoking
  290. ``ctest --collect-instrumentation``.
  291. ``snippets``
  292. Contains a list of `v1 Snippet File`_. This includes all snippet files
  293. generated since the previous index file was created. The file paths are
  294. relative to ``dataDir``.
  295. ``staticSystemInformation``
  296. Specifies the static information collected about the host machine
  297. CMake is being run from. Only included when enabled by the `v1 Query Files`_.
  298. * ``OSName``
  299. * ``OSPlatform``
  300. * ``OSRelease``
  301. * ``OSVersion``
  302. * ``familyId``
  303. * ``hostname``
  304. * ``is64Bits``
  305. * ``modelId``
  306. * ``numberOfLogicalCPU``
  307. * ``numberOfPhysicalCPU``
  308. * ``processorAPICID``
  309. * ``processorCacheSize``
  310. * ``processorClockFrequency``
  311. * ``processorName``
  312. * ``totalPhysicalMemory``
  313. * ``totalVirtualMemory``
  314. * ``vendorID``
  315. * ``vendorString``
  316. Example:
  317. .. code-block:: json
  318. {
  319. "version": 1,
  320. "hook": "manual",
  321. "buildDir": "<build>",
  322. "dataDir": "<build>/.cmake/instrumentation/v1/data",
  323. "snippets": [
  324. "configure-<timestamp>-<hash>.json",
  325. "generate-<timestamp>-<hash>.json",
  326. "compile-<timestamp>-<hash>.json",
  327. "compile-<timestamp>-<hash>.json",
  328. "link-<timestamp>-<hash>.json",
  329. "install-<timestamp>-<hash>.json",
  330. "ctest-<timestamp>-<hash>.json",
  331. "test-<timestamp>-<hash>.json",
  332. "test-<timestamp>-<hash>.json",
  333. ]
  334. }