cmake-instrumentation.7.rst 21 KB

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