cmake-instrumentation.7.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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`
  27. and :prop_gbl:`RULE_LAUNCH_CUSTOM` global properties to use the
  28. ``ctest --instrument`` launcher in order to capture details of each compile,
  29. link, and custom command respectively. If the project has been configured with
  30. :module:`CTestUseLaunchers`, ``ctest --instrument`` will also include the
  31. behavior usually performed by ``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. In order for the submitted ``Build.xml`` file to group the snippet files
  97. correctly, all configure and build commands should be executed with CTest in
  98. Dashboard Client mode.
  99. .. _`cmake-instrumentation API v1`:
  100. API v1
  101. ======
  102. The API version specifies the layout of the instrumentation directory, as well
  103. as the general format of the query files and :command:`cmake_instrumentation`
  104. command arguments.
  105. The Instrumentation API v1 is housed in the ``instrumentation/v1/`` directory
  106. under either ``<build>/.cmake/`` for output data and project-level queries, or
  107. ``<config_dir>/`` for user-level queries. The ``v1`` component of this
  108. directory is what signifies the API version. It has the following
  109. subdirectories:
  110. ``query/``
  111. Holds query files written by users or clients. Any file with the ``.json``
  112. file extension will be recognized as a query file. These files are owned by
  113. whichever client or user creates them.
  114. ``query/generated/``
  115. Holds query files generated by a CMake project with the
  116. :command:`cmake_instrumentation` command or the
  117. :envvar:`CTEST_USE_INSTRUMENTATION` variable. These files are owned by CMake
  118. and are deleted and regenerated automatically during the CMake configure step.
  119. ``data/``
  120. Holds instrumentation data collected on the project. CMake owns all data
  121. files, they should never be removed by other processes. Data collected here
  122. remains until after `Indexing`_ occurs and all `Callbacks`_ are executed.
  123. ``data/index/``
  124. A subset of the collected data, containing any
  125. `v1 Index Files <v1 Index File_>`_.
  126. ``data/content/``
  127. A subset of the collected data, containing any
  128. `v1 CMake Content Files <v1 CMake Content File_>`_.
  129. ``data/trace/``
  130. A subset of the collected data, containing the `Google Trace File`_ created
  131. from the most recent `Indexing`_. Unlike other data files, the most recent
  132. trace file remains even after `Indexing`_ occurs and all `Callbacks`_ are
  133. executed, until the next time `Indexing`_ occurs.
  134. ``cdash/``
  135. Holds temporary files used internally to generate XML content to be submitted
  136. to CDash.
  137. .. _`cmake-instrumentation v1 Query Files`:
  138. v1 Query Files
  139. --------------
  140. Any file with the ``.json`` extension under the ``instrumentation/v1/query/``
  141. directory is recognized as a query for instrumentation data.
  142. These files must contain a JSON object with the following keys. The ``version``
  143. key is required, but all other fields are optional.
  144. ``version``
  145. The Data version of snippet file to generate, an integer. Currently the only
  146. supported version is ``1``.
  147. ``callbacks``
  148. A list of command-line strings for `Callbacks`_ to handle collected
  149. instrumentation data. Whenever these callbacks are executed, the full path to
  150. a `v1 Index File`_ is appended to the arguments included in the string.
  151. ``hooks``
  152. A list of strings specifying when `Indexing`_ should occur automatically.
  153. These are the intervals when instrumentation data should be collated and user
  154. `Callbacks`_ should be invoked to handle the data. Elements in this list
  155. should be one of the following:
  156. * ``postGenerate``
  157. * ``preBuild`` (called when ``ninja`` or ``make`` is invoked)
  158. * ``postBuild`` (called when ``ninja`` or ``make`` completes)
  159. * ``preCMakeBuild`` (called when ``cmake --build`` is invoked)
  160. * ``postCMakeBuild`` (called when ``cmake --build`` completes)
  161. * ``postCMakeInstall``
  162. * ``postCTest``
  163. ``preBuild`` and ``postBuild`` are not supported when using the
  164. :generator:`MSYS Makefiles` or :generator:`FASTBuild` generators.
  165. Additionally, they will not be triggered when the build tool is invoked by
  166. ``cmake --build``.
  167. ``options``
  168. A list of strings used to enable certain optional behavior, including the
  169. collection of certain additional data. Elements in this list should be one of
  170. the following:
  171. ``staticSystemInformation``
  172. Enables collection of the static information about the host machine CMake
  173. is being run from. This data is collected during `Indexing`_ and is
  174. included in the generated `v1 Index File`_.
  175. ``dynamicSystemInformation``
  176. Enables collection of the dynamic information about the host machine
  177. CMake is being run from. Data is collected for every `v1 Snippet File`_
  178. generated by CMake, and includes information from immediately before and
  179. after the command is executed.
  180. ``cdashSubmit``
  181. Enables including instrumentation data in CDash. This does not
  182. automatically enable ``dynamicSystemInformation``, but is otherwise
  183. equivalent to having the :envvar:`CTEST_USE_INSTRUMENTATION` environment
  184. variable enabled.
  185. ``cdashVerbose``
  186. Enables including the full untruncated commands in data submitted to
  187. CDash. Equivalent to having the
  188. :envvar:`CTEST_USE_VERBOSE_INSTRUMENTATION` environment variable enabled.
  189. ``trace``
  190. Enables generation of a `Google Trace File`_ during `Indexing`_ to
  191. visualize data from the `v1 Snippet Files <v1 Snippet File_>`_ collected.
  192. The ``callbacks`` listed will be invoked during the specified hooks
  193. *at a minimum*. When there are multiple query files, the ``callbacks``,
  194. ``hooks`` and ``options`` between them will be merged. Therefore, if any query
  195. file includes any ``hooks``, every ``callback`` across all query files will be
  196. executed at every ``hook`` across all query files. Additionally, if any query
  197. file requests optional data using the ``options`` field, any related data will
  198. be present in all snippet files. User written ``callbacks`` should be able to
  199. handle the presence of this optional data, since it may be requested by an
  200. unrelated query.
  201. Example:
  202. .. code-block:: json
  203. {
  204. "version": 1,
  205. "callbacks": [
  206. "/usr/bin/python callback.py",
  207. "/usr/bin/cmake -P callback.cmake arg",
  208. ],
  209. "hooks": [
  210. "postCMakeBuild",
  211. "postCMakeInstall"
  212. ],
  213. "options": [
  214. "staticSystemInformation",
  215. "dynamicSystemInformation",
  216. "cdashSubmit",
  217. "trace"
  218. ]
  219. }
  220. In this example, after every ``cmake --build`` or ``cmake --install``
  221. invocation, an index file ``index-<timestamp>.json`` will be generated in
  222. ``<build>/.cmake/instrumentation/v1/data/index`` containing a list of data
  223. snippet files created since the previous indexing. The commands
  224. ``/usr/bin/python callback.py index-<timestamp>.json`` and
  225. ``/usr/bin/cmake -P callback.cmake arg index-<timestamp>.json`` will be executed
  226. in that order. The index file will contain the ``staticSystemInformation`` data
  227. and each snippet file listed in the index will contain the
  228. ``dynamicSystemInformation`` data. Additionally, the index file will contain
  229. the path to the generated `Google Trace File`_. Once both callbacks have completed,
  230. the index file and data files listed by it (including snippet files, but not
  231. the trace file) will be deleted from the project build tree. The instrumentation
  232. data will be present in the XML files submitted to CDash, but with truncated
  233. command strings because ``cdashVerbose`` was not enabled.
  234. .. _`cmake-instrumentation Data v1`:
  235. Data v1
  236. =======
  237. Data version specifies the contents of the output files generated by the CMake
  238. instrumentation API as part of the `Data Collection`_ and `Indexing`_. A new
  239. version number will be created whenever previously included data is removed or
  240. reformatted such that scripts written to parse this data may become
  241. incompatible with the new format. There are four types of data files generated:
  242. the `v1 Snippet File`_, `v1 Index File`_, `v1 CMake Content File`_, and the
  243. `Google Trace File`_. When using the `API v1`_, these files live in
  244. ``<build>/.cmake/instrumentation/v1/data/`` under the project build tree.
  245. .. _`cmake-instrumentation v1 Snippet File`:
  246. v1 Snippet File
  247. ---------------
  248. Snippet files are generated for every compile, link and custom command invoked
  249. as part of the CMake build or install step and contain instrumentation data about
  250. the command executed. Additionally, snippet files are created for the following:
  251. * The CMake configure step
  252. * The CMake generate step
  253. * Entire build step (executed with ``cmake --build``)
  254. * Entire install step (executed with ``cmake --install``)
  255. * Each ``ctest`` invocation
  256. * Each individual test executed by ``ctest``.
  257. These files remain in the build tree until after `Indexing`_ occurs and any
  258. user-specified `Callbacks`_ are executed.
  259. Snippet files have a filename with the syntax ``<role>-<hash>-<timestamp>.json``
  260. and contain the following data:
  261. ``version``
  262. The Data version of the snippet file, an integer. Currently the version is
  263. always ``1``.
  264. ``command``
  265. The full command executed. Excluded when ``role`` is ``build``.
  266. ``workingDir``
  267. The working directory in which the ``command`` was executed.
  268. ``result``
  269. The exit-value of the command, an integer.
  270. ``role``
  271. The type of command executed, which will be one of the following values:
  272. * ``configure``: the CMake configure step
  273. * ``generate``: the CMake generate step
  274. * ``compile``: an individual compile step invoked during the build
  275. * ``link``: an individual link step invoked during the build
  276. * ``custom``: an individual custom command invoked during the build
  277. * ``build``: a complete ``make`` or ``ninja`` invocation
  278. (not through ``cmake --build``).
  279. * ``cmakeBuild``: a complete ``cmake --build`` invocation
  280. * ``cmakeInstall``: a complete ``cmake --install`` invocation
  281. * ``install``: an individual ``cmake -P cmake_install.cmake`` invocation
  282. * ``ctest``: a complete ``ctest`` invocation
  283. * ``test``: a single test executed by CTest
  284. ``target``
  285. The CMake target associated with the command. Only included when ``role`` is
  286. ``compile`` or ``link``. In conjunction with ``cmakeContent``, this can
  287. be used to look up the target type and labels.
  288. ``timeStart``
  289. Time at which the command started, expressed as the number of milliseconds
  290. since the system epoch.
  291. ``duration``
  292. The duration that the command ran for, expressed in milliseconds.
  293. ``outputs``
  294. The command's output file(s), an array. Only included when ``role`` is one
  295. of: ``compile``, ``link``, ``custom``.
  296. ``outputSizes``
  297. The size(s) in bytes of the ``outputs``, an array. For files which do not
  298. exist, the size is 0. Included under the same conditions as the ``outputs``
  299. field.
  300. ``source``
  301. The source file being compiled. Only included when ``role`` is ``compile``.
  302. ``language``
  303. The language of the source file being compiled. Only included when ``role`` is
  304. ``compile`` or ``link``.
  305. ``testName``
  306. The name of the test being executed. Only included when ``role`` is ``test``.
  307. ``config``
  308. The type of build, such as ``Release`` or ``Debug``. Only included when
  309. ``role`` is one of: ``compile``, ``link``, ``custom``, ``install``,
  310. ``test``.
  311. ``dynamicSystemInformation``
  312. Specifies the dynamic information collected about the host machine
  313. CMake is being run from. Data is collected for every snippet file
  314. generated by CMake, with data immediately before and after the command is
  315. executed. Only included when enabled by the `v1 Query Files`_.
  316. ``beforeHostMemoryUsed``
  317. The Host Memory Used in KiB at ``timeStart``.
  318. ``afterHostMemoryUsed``
  319. The Host Memory Used in KiB at ``timeStart + duration``.
  320. ``beforeCPULoadAverage``
  321. The Average CPU Load at ``timeStart``, or ``null`` if it cannot be
  322. determined.
  323. ``afterCPULoadAverage``
  324. The Average CPU Load at ``timeStart + duration``, or ``null`` if it cannot
  325. be determined.
  326. ``cmakeContent``
  327. The path to a `v1 CMake Content` file located under ``data``, which
  328. contains information about the CMake configure and generate steps
  329. responsible for generating the ``command`` in this snippet.
  330. Example:
  331. .. code-block:: json
  332. {
  333. "version": 1,
  334. "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\"",
  335. "role" : "compile",
  336. "return" : 1,
  337. "target": "main",
  338. "language" : "C++",
  339. "outputs" : [ "CMakeFiles/main.dir/main.cxx.o" ],
  340. "outputSizes" : [ 0 ],
  341. "source" : "<src>/main.cxx",
  342. "config" : "Debug",
  343. "dynamicSystemInformation" :
  344. {
  345. "afterCPULoadAverage" : 2.3500000000000001,
  346. "afterHostMemoryUsed" : 6635680.0
  347. "beforeCPULoadAverage" : 2.3500000000000001,
  348. "beforeHostMemoryUsed" : 6635832.0
  349. },
  350. "timeStart" : 1737053448177,
  351. "duration" : 31,
  352. "cmakeContent" : "content/cmake-2025-07-11T12-46-32-0572.json"
  353. }
  354. v1 Index File
  355. -------------
  356. Index files contain a list of `v1 Snippet Files <v1 Snippet File_>`_. It
  357. serves as an entry point for navigating the instrumentation data. They are
  358. generated whenever `Indexing`_ occurs and deleted after any user-specified
  359. `Callbacks`_ are executed.
  360. ``version``
  361. The Data version of the index file, an integer. Currently the version is
  362. always ``1``.
  363. ``buildDir``
  364. The build directory of the CMake project.
  365. ``dataDir``
  366. The full path to the ``<build>/.cmake/instrumentation/v1/data/`` directory.
  367. ``hook``
  368. The name of the hook responsible for generating the index file. In addition
  369. to the hooks that can be specified by one of the `v1 Query Files`_, this value may
  370. be set to ``manual`` if indexing is performed by invoking
  371. ``ctest --collect-instrumentation <build>``.
  372. ``snippets``
  373. Contains a list of `v1 Snippet Files <v1 Snippet File_>`_. This includes all
  374. snippet files generated since the previous index file was created. The file
  375. paths are relative to ``dataDir``.
  376. ``trace``:
  377. Contains the path to the `Google Trace File`_. This includes data from all
  378. corresponding ``snippets`` in the index file. The file path is relative to
  379. ``dataDir``. Only included when enabled by the `v1 Query Files`_.
  380. ``staticSystemInformation``
  381. Specifies the static information collected about the host machine
  382. CMake is being run from. Only included when enabled by the `v1 Query Files`_.
  383. * ``OSName``
  384. * ``OSPlatform``
  385. * ``OSRelease``
  386. * ``OSVersion``
  387. * ``familyId``
  388. * ``hostname``
  389. * ``is64Bits``
  390. * ``modelId``
  391. * ``numberOfLogicalCPU``
  392. * ``numberOfPhysicalCPU``
  393. * ``processorAPICID``
  394. * ``processorCacheSize``
  395. * ``processorClockFrequency``
  396. * ``processorName``
  397. * ``totalPhysicalMemory``
  398. * ``totalVirtualMemory``
  399. * ``vendorID``
  400. * ``vendorString``
  401. Example:
  402. .. code-block:: json
  403. {
  404. "version": 1,
  405. "hook": "manual",
  406. "buildDir": "<build>",
  407. "dataDir": "<build>/.cmake/instrumentation/v1/data",
  408. "snippets": [
  409. "configure-<hash>-<timestamp>.json",
  410. "generate-<hash>-<timestamp>.json",
  411. "compile-<hash>-<timestamp>.json",
  412. "compile-<hash>-<timestamp>.json",
  413. "link-<hash>-<timestamp>.json",
  414. "install-<hash>-<timestamp>.json",
  415. "ctest-<hash>-<timestamp>.json",
  416. "test-<hash>-<timestamp>.json",
  417. "test-<hash>-<timestamp>.json",
  418. ],
  419. "trace": "trace/trace-<timestamp>.json"
  420. }
  421. .. _`cmake-instrumentation v1 CMake Content File`:
  422. v1 CMake Content File
  423. ---------------------
  424. CMake content files contain information about the CMake configure and generate
  425. steps. Each `v1 Snippet File`_ provides the path to one of these files
  426. corresponding to the CMake invocation responsible for generating its command.
  427. Each CMake content file contains the following:
  428. ``custom``
  429. An object containing arbitrary JSON data specified by the user with the
  430. :ref:`cmake_instrumentation CUSTOM_CONTENT` functionality of the
  431. :command:`cmake_instrumentation` command.
  432. ``targets``
  433. An object containing CMake targets, indexed by name, that have corresponding
  434. instrumentation data. Each target contains the following:
  435. ``type``
  436. The target type. One of ``EXECUTABLE``, ``STATIC_LIBRARY``,
  437. ``SHARED_LIBRARY`` or ``OBJECT_LIBRARY``.
  438. ``labels``
  439. The :prop_tgt:`LABELS` property of the target.
  440. Google Trace File
  441. -----------------
  442. Trace files follow the `Google Trace Event Format`_. They include data from
  443. all `v1 Snippet Files <v1 Snippet File_>`_ listed in the current index file.
  444. These files remain in the build tree even after `Indexing`_ occurs and all
  445. `Callbacks`_ are executed, until the next time `Indexing`_ occurs.
  446. Trace files are stored in the ``JSON Array Format``, where each
  447. `v1 Snippet File`_ corresponds to a single trace event object. Each trace
  448. event contains the following data:
  449. ``name``
  450. A descriptive name generated by CMake based on the given snippet data.
  451. ``cat``
  452. The ``role`` from the `v1 Snippet File`_.
  453. ``ph``
  454. Currently, always ``"X"`` to represent ``Complete Events``.
  455. ``ts``
  456. The ``timeStart`` from the `v1 Snippet File`_, converted from milliseconds to
  457. microseconds.
  458. ``dur``
  459. The ``duration`` from the `v1 Snippet File`_, converted from milliseconds to
  460. microseconds.
  461. ``pid``
  462. Unused (always zero).
  463. ``tid``
  464. An integer ranging from zero to the number of concurrent jobs with which the
  465. processes being indexed ran. This is a synthetic ID calculated by CMake
  466. based on the ``ts`` and ``dur`` of all snippet files being indexed in
  467. order to produce a more useful visualization of the process concurrency.
  468. ``args``
  469. Contains all data from the `v1 Snippet File`_ corresponding to this trace event.
  470. .. _`Google Trace Event Format`: https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview