cmake-instrumentation.7.rst 12 KB

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