You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
14 KiB

  1. .. cmake-manual-description: CMake Instrumentation
  2. cmake-instrumentation(7)
  3. ************************
  4. .. versionadded:: 4.0
  5. .. only:: html
  6. .. contents::
  7. Introduction
  8. ============
  9. The CMake Instrumentation API allows for the collection of timing data, target
  10. information and system diagnostic information during the configure, generate,
  11. build, test and install steps for a CMake project.
  12. This feature is only available for projects using the :ref:`Makefile Generators`
  13. or the :ref:`Ninja Generators`.
  14. All interactions with the CMake instrumentation API must specify both an API
  15. version and a Data version. At this time, there is only one version for each of
  16. these: the `API v1`_ and `Data v1`_.
  17. Data Collection
  18. ---------------
  19. Whenever a command is executed with
  20. instrumentation enabled, a `v1 Snippet File`_ is created in the project build
  21. tree with data specific to that command. These files remain until after
  22. `Indexing`_ occurs.
  23. CMake sets the :prop_gbl:`RULE_LAUNCH_COMPILE`, :prop_gbl:`RULE_LAUNCH_LINK` and
  24. :prop_gbl:`RULE_LAUNCH_CUSTOM` global properties to use the
  25. ``ctest --instrument`` launcher in order to capture details of each compile, link
  26. and custom command respectively. If the project has been configured with :module:`CTestUseLaunchers`,
  27. ``ctest --instrument`` will also include the behavior usually performed by
  28. ``ctest --launch``.
  29. Indexing
  30. --------
  31. Indexing is the process of collating generated instrumentation data. Indexing
  32. occurs at specific intervals called hooks, such as after every build. These
  33. hooks are configured as part of the `v1 Query Files`_. Whenever a hook is
  34. triggered, an index file is generated containing a list of snippet files newer
  35. than the previous indexing.
  36. Indexing and can also be performed by manually invoking
  37. ``ctest --collect-instrumentation``.
  38. Callbacks
  39. ---------
  40. As part of the `v1 Query Files`_, users can provide a list of callbacks
  41. intended to handle data collected by this feature.
  42. Whenever `Indexing`_ occurs, each provided callback is executed, passing the
  43. path to the generated index file as an argument.
  44. These callbacks, defined either at the user-level or project-level should read
  45. the instrumentation data and perform any desired handling of it. The index file
  46. and its listed snippets are automatically deleted by CMake once all callbacks
  47. have completed. Note that a callback should never move or delete these data
  48. files manually as they may be needed by other callbacks.
  49. Enabling Instrumentation
  50. ========================
  51. Instrumentation can be enabled either for an individual CMake project, or
  52. for all CMake projects configured and built by a user. For both cases,
  53. see the `v1 Query Files`_ for details on configuring this feature.
  54. Enabling Instrumentation at the Project-Level
  55. ---------------------------------------------
  56. Project code can contain instrumentation queries with the
  57. :command:`cmake_instrumentation` command.
  58. In addition, query files can be placed manually under
  59. ``<build>/.cmake/instrumentation/<version>/query/`` at the top of a build tree.
  60. This version of CMake supports only one version schema, `API v1`_.
  61. Enabling Instrumentation at the User-Level
  62. ------------------------------------------
  63. Instrumentation can be configured at the user-level by placing query files in
  64. the :envvar:`CMAKE_CONFIG_DIR` under
  65. ``<config_dir>/instrumentation/<version>/query/``.
  66. .. _`cmake-instrumentation API v1`:
  67. API v1
  68. ======
  69. The API version specifies both the subdirectory layout of the instrumentation data,
  70. and the format of the query files.
  71. The Instrumentation API v1 is housed in the ``instrumentation/v1/`` directory
  72. under either ``<build>/.cmake/`` for output data and project-level queries, or
  73. ``<config_dir>/`` for user-level queries. The ``v1`` component of this
  74. directory is what signifies the API version. It has the following
  75. subdirectories:
  76. ``query/``
  77. Holds query files written by users or clients. Any file with the ``.json``
  78. file extension will be recognized as a query file. These files are owned by
  79. whichever client or user creates them.
  80. ``query/generated/``
  81. Holds query files generated by a CMake project with the
  82. :command:`cmake_instrumentation` command. These files are owned by CMake and
  83. are deleted and regenerated automatically during the CMake configure step.
  84. ``data/``
  85. Holds instrumentation data collected on the project. CMake owns all data
  86. files, they should never be removed by other processes. Data collected here
  87. remains until after `Indexing`_ occurs and all `Callbacks`_ are executed.
  88. .. _`cmake-instrumentation v1 Query Files`:
  89. v1 Query Files
  90. --------------
  91. Any file with the ``.json`` extension under the ``instrumentation/v1/query/``
  92. directory is recognized as a query for instrumentation data.
  93. These files must contain a JSON object with the following keys. The ``version``
  94. key is required, but all other fields are optional.
  95. ``version``
  96. The Data version of snippet file to generate, an integer. Currently the only
  97. supported version is ``1``.
  98. ``callbacks``
  99. A list of command-line strings for `Callbacks`_ to handle collected
  100. instrumentation data. Whenever these callbacks are executed, the full path to
  101. a `v1 Index File`_ is appended to the arguments included in the string.
  102. ``hooks``
  103. A list of strings specifying when `Indexing`_ should occur automatically.
  104. These are the intervals when instrumentation data should be collated and user
  105. `Callbacks`_ should be invoked to handle the data. Elements in this list
  106. should be one of the following:
  107. * ``postGenerate``
  108. * ``preBuild`` (called when ``ninja`` or ``make`` is invoked; unavailable on Windows)
  109. * ``postBuild`` (called when ``ninja`` or ``make`` completes; unavailable on Windows)
  110. * ``preCMakeBuild`` (called when ``cmake --build`` is invoked)
  111. * ``postCMakeBuild`` (called when ``cmake --build`` completes)
  112. * ``postInstall``
  113. * ``postTest``
  114. ``queries``
  115. A list of strings specifying additional optional data to collect during
  116. instrumentation. Elements in this list should be one of the following:
  117. ``staticSystemInformation``
  118. Enables collection of the static information about the host machine CMake
  119. is being run from. This data is collected during `Indexing`_ and is
  120. included in the generated `v1 Index File`_.
  121. ``dynamicSystemInformation``
  122. Enables collection of the dynamic information about the host machine
  123. CMake is being run from. Data is collected for every `v1 Snippet File`_
  124. generated by CMake, and includes information from immediately before and
  125. after the command is executed.
  126. The ``callbacks`` listed will be invoked during the specified hooks
  127. *at a minimum*. When there are multiple query files, the ``callbacks``,
  128. ``hooks`` and ``queries`` between them will be merged. Therefore, if any query
  129. file includes any ``hooks``, every ``callback`` across all query files will be
  130. executed at every ``hook`` across all query files. Additionally, if any query
  131. file includes any optional ``queries``, the optional query data will be present
  132. in all data files.
  133. Example:
  134. .. code-block:: json
  135. {
  136. "version": 1,
  137. "callbacks": [
  138. "/usr/bin/python callback.py",
  139. "/usr/bin/cmake -P callback.cmake arg",
  140. ],
  141. "hooks": [
  142. "postCMakeBuild",
  143. "postInstall"
  144. ],
  145. "queries": [
  146. "staticSystemInformation",
  147. "dynamicSystemInformation"
  148. ]
  149. }
  150. In this example, after every ``cmake --build`` or ``cmake --install``
  151. invocation, an index file ``index-<hash>.json`` will be generated in
  152. ``<build>/.cmake/instrumentation/v1/data`` containing a list of data snippet
  153. files created since the previous indexing. The commands
  154. ``/usr/bin/python callback.py index-<hash>.json`` and
  155. ``/usr/bin/cmake -P callback.cmake arg index-<hash>.json`` will be executed in
  156. that order. The index file will contain the ``staticSystemInformation`` data and
  157. each snippet file listed in the index will contain the
  158. ``dynamicSystemInformation`` data. Once both callbacks have completed, the index
  159. file and all snippet files listed by it will be deleted from the project build
  160. tree.
  161. .. _`cmake-instrumentation Data v1`:
  162. Data v1
  163. =======
  164. Data version specifies the contents of the output files generated by the CMake
  165. instrumentation API as part of the `Data Collection`_ and `Indexing`_. There are
  166. two types of data files generated: the `v1 Snippet File`_ and `v1 Index File`_.
  167. When using the `API v1`_, these files live in
  168. ``<build>/.cmake/instrumentation/v1/data/`` under the project build tree.
  169. v1 Snippet File
  170. ---------------
  171. Snippet files are generated for every compile, link and custom command invoked
  172. as part of the CMake build or install step and contain instrumentation data about
  173. the command executed. Additionally, snippet files are created for the following:
  174. * The CMake configure step
  175. * The CMake generate step
  176. * Entire build step (executed with ``cmake --build``)
  177. * Entire install step (executed with ``cmake --install``)
  178. * Each ``ctest`` invocation
  179. * Each individual test executed by ``ctest``.
  180. These files remain in the build tree until after `Indexing`_ occurs and any
  181. user-specified `Callbacks`_ are executed.
  182. Snippet files have a filename with the syntax ``<role>-<timestamp>-<hash>.json``
  183. and contain the following data:
  184. ``version``
  185. The Data version of the snippet file, an integer. Currently the version is
  186. always ``1``.
  187. ``command``
  188. The full command executed.
  189. ``result``
  190. The exit-value of the command, an integer.
  191. ``role``
  192. The type of command executed, which will be one of the following values:
  193. * ``compile``
  194. * ``link``
  195. * ``custom``
  196. * ``cmakeBuild``
  197. * ``install``
  198. * ``ctest``
  199. * ``test``
  200. ``target``
  201. The CMake target associated with the command. Only included when ``role`` is
  202. one of ``compile``, ``link``, ``custom``.
  203. ``targetType``
  204. The :prop_tgt:`TYPE` of the target. Only included when ``role`` is
  205. ``link``.
  206. ``targetLabels``
  207. The :prop_tgt:`LABELS` of the target. Only included when ``role`` is
  208. ``link``.
  209. ``timeStart``
  210. Time at which the command started, expressed as the number of milliseconds
  211. since the system epoch.
  212. ``duration``
  213. The duration that the command ran for, expressed in milliseconds.
  214. ``outputs``
  215. The command's output file(s), an array. Only included when ``role`` is one
  216. of: ``compile``, ``link``, ``custom``.
  217. ``outputSizes``
  218. The size(s) in bytes of the ``outputs``, an array. For files which do not
  219. exist, the size is 0. Included under the same conditions as the ``outputs``
  220. field.
  221. ``source``
  222. The source file being compiled. Only included when ``role`` is ``compile``.
  223. ``language``
  224. The language of the source file being compiled. Only included when ``role`` is
  225. ``compile``.
  226. ``testName``
  227. The name of the test being executed. Only included when ``role`` is ``test``.
  228. ``config``
  229. The type of build, such as ``Release`` or ``Debug``. Only included when
  230. ``role`` is ``compile``, ``link`` or ``test``.
  231. ``dynamicSystemInformation``
  232. Specifies the dynamic information collected about the host machine
  233. CMake is being run from. Data is collected for every snippet file
  234. generated by CMake, with data immediately before and after the command is
  235. executed. Only included when enabled by the `v1 Query Files`_.
  236. ``beforeHostMemoryUsed``
  237. The Host Memory Used in KiB at ``timeStart``.
  238. ``afterHostMemoryUsed``
  239. The Host Memory Used in KiB at ``timeStop``.
  240. ``beforeCPULoadAverage``
  241. The Average CPU Load at ``timeStart``.
  242. ``afterCPULoadAverage``
  243. The Average CPU Load at ``timeStop``.
  244. Example:
  245. .. code-block:: json
  246. {
  247. "version": 1,
  248. "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",
  249. "role" : "compile",
  250. "return" : 1,
  251. "target": "main",
  252. "language" : "C++",
  253. "outputs" : [ "CMakeFiles/main.dir/main.cxx.o" ],
  254. "outputSizes" : [ 0 ],
  255. "source" : "<src>/main.cxx",
  256. "config" : "Debug",
  257. "dynamicSystemInformation" :
  258. {
  259. "afterCPULoadAverage" : 2.3500000000000001,
  260. "afterHostMemoryUsed" : 6635680.0
  261. "beforeCPULoadAverage" : 2.3500000000000001,
  262. "beforeHostMemoryUsed" : 6635832.0
  263. },
  264. "timeStart" : 1737053448177,
  265. "duration" : 31
  266. }
  267. v1 Index File
  268. -------------
  269. Index files contain a list of `v1 Snippet File`_. It serves as an entry point
  270. for navigating the instrumentation data. They are generated whenever `Indexing`_
  271. occurs and deleted after any user-specified `Callbacks`_ are executed.
  272. ``version``
  273. The Data version of the index file, an integer. Currently the version is
  274. always ``1``.
  275. ``buildDir``
  276. The build directory of the CMake project.
  277. ``dataDir``
  278. The full path to the ``<build>/.cmake/instrumentation/v1/data/`` directory.
  279. ``hook``
  280. The name of the hook responsible for generating the index file. In addition
  281. to the hooks that can be specified by one of the `v1 Query Files`_, this value may
  282. be set to ``manual`` if indexing is performed by invoking
  283. ``ctest --collect-instrumentation``.
  284. ``snippets``
  285. Contains a list of `v1 Snippet File`_. This includes all snippet files
  286. generated since the previous index file was created. The file paths are
  287. relative to ``dataDir``.
  288. ``staticSystemInformation``
  289. Specifies the static information collected about the host machine
  290. CMake is being run from. Only included when enabled by the `v1 Query Files`_.
  291. * ``OSName``
  292. * ``OSPlatform``
  293. * ``OSRelease``
  294. * ``OSVersion``
  295. * ``familyId``
  296. * ``hostname``
  297. * ``is64Bits``
  298. * ``modelId``
  299. * ``numberOfLogicalCPU``
  300. * ``numberOfPhysicalCPU``
  301. * ``processorAPICID``
  302. * ``processorCacheSize``
  303. * ``processorClockFrequency``
  304. * ``processorName``
  305. * ``totalPhysicalMemory``
  306. * ``totalVirtualMemory``
  307. * ``vendorID``
  308. * ``vendorString``
  309. Example:
  310. .. code-block:: json
  311. {
  312. "version": 1,
  313. "hook": "manual",
  314. "buildDir": "<build>",
  315. "dataDir": "<build>/.cmake/instrumentation/v1/data",
  316. "snippets": [
  317. "configure-<timestamp>-<hash>.json",
  318. "generate-<timestamp>-<hash>.json",
  319. "compile-<timestamp>-<hash>.json",
  320. "compile-<timestamp>-<hash>.json",
  321. "link-<timestamp>-<hash>.json",
  322. "install-<timestamp>-<hash>.json",
  323. "ctest-<timestamp>-<hash>.json",
  324. "test-<timestamp>-<hash>.json",
  325. "test-<timestamp>-<hash>.json",
  326. ]
  327. }