cmake-instrumentation.7.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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
  16. :ref:`Makefile Generators`, :ref:`Ninja Generators` or :generator:`FASTBuild`.
  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 <build>``.
  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. Enabling Instrumentation for CDash Submissions
  70. ----------------------------------------------
  71. You can enable instrumentation when using CTest in :ref:`Dashboard Client`
  72. mode by setting the :envvar:`CTEST_USE_INSTRUMENTATION` environment variable.
  73. Doing so automatically enables the ``dynamicSystemInformation`` option.
  74. The following table shows how each type of instrumented command gets mapped
  75. to a corresponding type of CTest XML file.
  76. =================================================== ==================
  77. :ref:`Snippet Role <cmake-instrumentation Data v1>` CTest XML File
  78. =================================================== ==================
  79. ``configure`` ``Configure.xml``
  80. ``generate`` ``Configure.xml``
  81. ``compile`` ``Build.xml``
  82. ``link`` ``Build.xml``
  83. ``custom`` ``Build.xml``
  84. ``build`` unused!
  85. ``cmakeBuild`` ``Build.xml``
  86. ``cmakeInstall`` ``Build.xml``
  87. ``install`` ``Build.xml``
  88. ``ctest`` ``Build.xml``
  89. ``test`` ``Test.xml``
  90. =================================================== ==================
  91. By default the command line reported to CDash is truncated at the first space.
  92. You can instead choose to report the full command line (including arguments)
  93. by setting :envvar:`CTEST_USE_VERBOSE_INSTRUMENTATION` to 1.
  94. Alternatively, you can use the `v1 Query Files`_ to enable instrumentation for
  95. CDash using the ``cdashSubmit`` and ``cdashVerbose`` options.
  96. .. _`cmake-instrumentation API v1`:
  97. API v1
  98. ======
  99. The API version specifies the layout of the instrumentation directory, as well
  100. as the general format of the query files and :command:`cmake_instrumentation`
  101. command arguments.
  102. The Instrumentation API v1 is housed in the ``instrumentation/v1/`` directory
  103. under either ``<build>/.cmake/`` for output data and project-level queries, or
  104. ``<config_dir>/`` for user-level queries. The ``v1`` component of this
  105. directory is what signifies the API version. It has the following
  106. subdirectories:
  107. ``query/``
  108. Holds query files written by users or clients. Any file with the ``.json``
  109. file extension will be recognized as a query file. These files are owned by
  110. whichever client or user creates them.
  111. ``query/generated/``
  112. Holds query files generated by a CMake project with the
  113. :command:`cmake_instrumentation` command or the
  114. :envvar:`CTEST_USE_INSTRUMENTATION` variable. These files are owned by CMake
  115. and are deleted and regenerated automatically during the CMake configure step.
  116. ``data/``
  117. Holds instrumentation data collected on the project. CMake owns all data
  118. files, they should never be removed by other processes. Data collected here
  119. remains until after `Indexing`_ occurs and all `Callbacks`_ are executed.
  120. ``data/content/``
  121. A subset of the collected data, containing any
  122. :ref:`cmake_instrumentation Configure Content` files.
  123. ``cdash/``
  124. Holds temporary files used internally to generate XML content to be submitted
  125. to CDash.
  126. .. _`cmake-instrumentation v1 Query Files`:
  127. v1 Query Files
  128. --------------
  129. Any file with the ``.json`` extension under the ``instrumentation/v1/query/``
  130. directory is recognized as a query for instrumentation data.
  131. These files must contain a JSON object with the following keys. The ``version``
  132. key is required, but all other fields are optional.
  133. ``version``
  134. The Data version of snippet file to generate, an integer. Currently the only
  135. supported version is ``1``.
  136. ``callbacks``
  137. A list of command-line strings for `Callbacks`_ to handle collected
  138. instrumentation data. Whenever these callbacks are executed, the full path to
  139. a `v1 Index File`_ is appended to the arguments included in the string.
  140. ``hooks``
  141. A list of strings specifying when `Indexing`_ should occur automatically.
  142. These are the intervals when instrumentation data should be collated and user
  143. `Callbacks`_ should be invoked to handle the data. Elements in this list
  144. should be one of the following:
  145. * ``postGenerate``
  146. * ``preBuild`` (called when ``ninja`` or ``make`` is invoked)
  147. * ``postBuild`` (called when ``ninja`` or ``make`` completes)
  148. * ``preCMakeBuild`` (called when ``cmake --build`` is invoked)
  149. * ``postCMakeBuild`` (called when ``cmake --build`` completes)
  150. * ``postInstall``
  151. * ``postTest``
  152. ``preBuild`` and ``postBuild`` are not supported when using the
  153. :generator:`MSYS Makefiles` or :generator:`FASTBuild` generators.
  154. Additionally, they will not be triggered when the build tool is invoked by
  155. ``cmake --build``.
  156. ``options``
  157. A list of strings used to enable certain optional behavior, including the
  158. collection of certain additional data. Elements in this list should be one of
  159. the following:
  160. ``staticSystemInformation``
  161. Enables collection of the static information about the host machine CMake
  162. is being run from. This data is collected during `Indexing`_ and is
  163. included in the generated `v1 Index File`_.
  164. ``dynamicSystemInformation``
  165. Enables collection of the dynamic information about the host machine
  166. CMake is being run from. Data is collected for every `v1 Snippet File`_
  167. generated by CMake, and includes information from immediately before and
  168. after the command is executed.
  169. ``cdashSubmit``
  170. Enables including instrumentation data in CDash. This does not
  171. automatically enable ``dynamicSystemInformation``, but is otherwise
  172. equivalent to having the :envvar:`CTEST_USE_INSTRUMENTATION` environment
  173. variable enabled.
  174. ``cdashVerbose``
  175. Enables including the full untruncated commands in data submitted to
  176. CDash. Equivalent to having the
  177. :envvar:`CTEST_USE_VERBOSE_INSTRUMENTATION` environment variable enabled.
  178. The ``callbacks`` listed will be invoked during the specified hooks
  179. *at a minimum*. When there are multiple query files, the ``callbacks``,
  180. ``hooks`` and ``options`` between them will be merged. Therefore, if any query
  181. file includes any ``hooks``, every ``callback`` across all query files will be
  182. executed at every ``hook`` across all query files. Additionally, if any query
  183. file requests optional data using the ``options`` field, any related data will
  184. be present in all snippet files. User written ``callbacks`` should be able to
  185. handle the presence of this optional data, since it may be requested by an
  186. unrelated query.
  187. Example:
  188. .. code-block:: json
  189. {
  190. "version": 1,
  191. "callbacks": [
  192. "/usr/bin/python callback.py",
  193. "/usr/bin/cmake -P callback.cmake arg",
  194. ],
  195. "hooks": [
  196. "postCMakeBuild",
  197. "postInstall"
  198. ],
  199. "options": [
  200. "staticSystemInformation",
  201. "dynamicSystemInformation",
  202. "cdashSubmit"
  203. ]
  204. }
  205. In this example, after every ``cmake --build`` or ``cmake --install``
  206. invocation, an index file ``index-<timestamp>.json`` will be generated in
  207. ``<build>/.cmake/instrumentation/v1/data`` containing a list of data snippet
  208. files created since the previous indexing. The commands
  209. ``/usr/bin/python callback.py index-<timestamp>.json`` and
  210. ``/usr/bin/cmake -P callback.cmake arg index-<timestamp>.json`` will be executed
  211. in that order. The index file will contain the ``staticSystemInformation`` data
  212. and each snippet file listed in the index will contain the
  213. ``dynamicSystemInformation`` data. Once both callbacks have completed, the index
  214. file and all snippet files listed by it will be deleted from the project build
  215. tree. The instrumentation data will be present in the XML files submitted to
  216. CDash, but with truncated command strings because ``cdashVerbose`` was not
  217. enabled.
  218. .. _`cmake-instrumentation Data v1`:
  219. Data v1
  220. =======
  221. Data version specifies the contents of the output files generated by the CMake
  222. instrumentation API as part of the `Data Collection`_ and `Indexing`_. A new
  223. version number will be created whenever previously included data is removed or
  224. reformatted such that scripts written to parse this data may become
  225. incompatible with the new format. There are two types of data files generated:
  226. the `v1 Snippet File`_ and `v1 Index File`_. When using the `API v1`_, these
  227. files live in ``<build>/.cmake/instrumentation/v1/data/`` under the project
  228. build tree.
  229. .. _`cmake-instrumentation v1 Snippet File`:
  230. v1 Snippet File
  231. ---------------
  232. Snippet files are generated for every compile, link and custom command invoked
  233. as part of the CMake build or install step and contain instrumentation data about
  234. the command executed. Additionally, snippet files are created for the following:
  235. * The CMake configure step
  236. * The CMake generate step
  237. * Entire build step (executed with ``cmake --build``)
  238. * Entire install step (executed with ``cmake --install``)
  239. * Each ``ctest`` invocation
  240. * Each individual test executed by ``ctest``.
  241. These files remain in the build tree until after `Indexing`_ occurs and any
  242. user-specified `Callbacks`_ are executed.
  243. Snippet files have a filename with the syntax ``<role>-<hash>-<timestamp>.json``
  244. and contain the following data:
  245. ``version``
  246. The Data version of the snippet file, an integer. Currently the version is
  247. always ``1``.
  248. ``command``
  249. The full command executed. Excluded when ``role`` is ``build``.
  250. ``workingDir``
  251. The working directory in which the ``command`` was executed.
  252. ``result``
  253. The exit-value of the command, an integer.
  254. ``role``
  255. The type of command executed, which will be one of the following values:
  256. * ``configure``: the CMake configure step
  257. * ``generate``: the CMake generate step
  258. * ``compile``: an individual compile step invoked during the build
  259. * ``link``: an individual link step invoked during the build
  260. * ``custom``: an individual custom command invoked during the build
  261. * ``build``: a complete ``make`` or ``ninja`` invocation. Only generated if ``preBuild`` or ``postBuild`` hooks are enabled.
  262. * ``cmakeBuild``: a complete ``cmake --build`` invocation
  263. * ``cmakeInstall``: a complete ``cmake --install`` invocation
  264. * ``install``: an individual ``cmake -P cmake_install.cmake`` invocation
  265. * ``ctest``: a complete ``ctest`` invocation
  266. * ``test``: a single test executed by CTest
  267. ``target``
  268. The CMake target associated with the command. Only included when ``role`` is
  269. ``compile`` or ``link``.
  270. ``targetType``
  271. The :prop_tgt:`TYPE` of the target. Only included when ``role`` is
  272. ``link``.
  273. ``targetLabels``
  274. The :prop_tgt:`LABELS` of the target. Only included when ``role`` is
  275. ``link``.
  276. ``timeStart``
  277. Time at which the command started, expressed as the number of milliseconds
  278. since the system epoch.
  279. ``duration``
  280. The duration that the command ran for, expressed in milliseconds.
  281. ``outputs``
  282. The command's output file(s), an array. Only included when ``role`` is one
  283. of: ``compile``, ``link``, ``custom``.
  284. ``outputSizes``
  285. The size(s) in bytes of the ``outputs``, an array. For files which do not
  286. exist, the size is 0. Included under the same conditions as the ``outputs``
  287. field.
  288. ``source``
  289. The source file being compiled. Only included when ``role`` is ``compile``.
  290. ``language``
  291. The language of the source file being compiled. Only included when ``role`` is
  292. ``compile``.
  293. ``testName``
  294. The name of the test being executed. Only included when ``role`` is ``test``.
  295. ``config``
  296. The type of build, such as ``Release`` or ``Debug``. Only included when
  297. ``role`` is ``compile``, ``link`` or ``test``.
  298. ``dynamicSystemInformation``
  299. Specifies the dynamic information collected about the host machine
  300. CMake is being run from. Data is collected for every snippet file
  301. generated by CMake, with data immediately before and after the command is
  302. executed. Only included when enabled by the `v1 Query Files`_.
  303. ``beforeHostMemoryUsed``
  304. The Host Memory Used in KiB at ``timeStart``.
  305. ``afterHostMemoryUsed``
  306. The Host Memory Used in KiB at ``timeStop``.
  307. ``beforeCPULoadAverage``
  308. The Average CPU Load at ``timeStart``.
  309. ``afterCPULoadAverage``
  310. The Average CPU Load at ``timeStop``.
  311. ``configureContent``
  312. The path to a :ref:`cmake_instrumentation Configure Content` file located under ``data``,
  313. which may contain information about the CMake configure step corresponding
  314. to this data.
  315. Example:
  316. .. code-block:: json
  317. {
  318. "version": 1,
  319. "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\"",
  320. "role" : "compile",
  321. "return" : 1,
  322. "target": "main",
  323. "language" : "C++",
  324. "outputs" : [ "CMakeFiles/main.dir/main.cxx.o" ],
  325. "outputSizes" : [ 0 ],
  326. "source" : "<src>/main.cxx",
  327. "config" : "Debug",
  328. "dynamicSystemInformation" :
  329. {
  330. "afterCPULoadAverage" : 2.3500000000000001,
  331. "afterHostMemoryUsed" : 6635680.0
  332. "beforeCPULoadAverage" : 2.3500000000000001,
  333. "beforeHostMemoryUsed" : 6635832.0
  334. },
  335. "timeStart" : 1737053448177,
  336. "duration" : 31,
  337. "configureContent" : "content/configure-2025-07-11T12-46-32-0572.json"
  338. }
  339. v1 Index File
  340. -------------
  341. Index files contain a list of `v1 Snippet File`_. It serves as an entry point
  342. for navigating the instrumentation data. They are generated whenever `Indexing`_
  343. occurs and deleted after any user-specified `Callbacks`_ are executed.
  344. ``version``
  345. The Data version of the index file, an integer. Currently the version is
  346. always ``1``.
  347. ``buildDir``
  348. The build directory of the CMake project.
  349. ``dataDir``
  350. The full path to the ``<build>/.cmake/instrumentation/v1/data/`` directory.
  351. ``hook``
  352. The name of the hook responsible for generating the index file. In addition
  353. to the hooks that can be specified by one of the `v1 Query Files`_, this value may
  354. be set to ``manual`` if indexing is performed by invoking
  355. ``ctest --collect-instrumentation <build>``.
  356. ``snippets``
  357. Contains a list of `v1 Snippet File`_. This includes all snippet files
  358. generated since the previous index file was created. The file paths are
  359. relative to ``dataDir``.
  360. ``staticSystemInformation``
  361. Specifies the static information collected about the host machine
  362. CMake is being run from. Only included when enabled by the `v1 Query Files`_.
  363. * ``OSName``
  364. * ``OSPlatform``
  365. * ``OSRelease``
  366. * ``OSVersion``
  367. * ``familyId``
  368. * ``hostname``
  369. * ``is64Bits``
  370. * ``modelId``
  371. * ``numberOfLogicalCPU``
  372. * ``numberOfPhysicalCPU``
  373. * ``processorAPICID``
  374. * ``processorCacheSize``
  375. * ``processorClockFrequency``
  376. * ``processorName``
  377. * ``totalPhysicalMemory``
  378. * ``totalVirtualMemory``
  379. * ``vendorID``
  380. * ``vendorString``
  381. Example:
  382. .. code-block:: json
  383. {
  384. "version": 1,
  385. "hook": "manual",
  386. "buildDir": "<build>",
  387. "dataDir": "<build>/.cmake/instrumentation/v1/data",
  388. "snippets": [
  389. "configure-<hash>-<timestamp>.json",
  390. "generate-<hash>-<timestamp>.json",
  391. "compile-<hash>-<timestamp>.json",
  392. "compile-<hash>-<timestamp>.json",
  393. "link-<hash>-<timestamp>.json",
  394. "install-<hash>-<timestamp>.json",
  395. "ctest-<hash>-<timestamp>.json",
  396. "test-<hash>-<timestamp>.json",
  397. "test-<hash>-<timestamp>.json",
  398. ]
  399. }