cmake-instrumentation.7.rst 18 KB

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