cmake-instrumentation.7.rst 17 KB

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