cmake-instrumentation.7.rst 21 KB

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