Source code of Windows XP (NT5)
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.

647 lines
26 KiB

  1. Event Tracing API Samples
  2. =========================
  3. This readme describes the three sample implementations of using the Event
  4. Trace API's supplied with the SDK and a brief description of event tracing.
  5. The readme is organized into 3 sections - Overview, Description of Event
  6. Tracing and Using the Event Tracing samples.
  7. For more details please refer the Platform SDK documentation on Event Tracing.
  8. OVERVIEW
  9. ========
  10. An event simply represents any activity of interest, especially with respect
  11. to performance. It is typically an activity related to the usage of a resource
  12. such as the processor, disk, etc. Examples of operating system events are disk
  13. I/O and page fault events. Examples of application events are the start and
  14. end of a certain transaction (in the case of the Directory Service it could be
  15. the start and end of a search operation).
  16. A trace is a discrete set of events that provide a view of the activity of
  17. interest over a period of time. For each event,information related to that
  18. event is recorded. Event tracing is therefore an ordered set of events,
  19. generally recorded to a buffer for subsequent processing.
  20. Event tracing works on the WMI model (details of the WMI model can be obtained
  21. from the Platform SDK documentation for WMI). There exists a Provider, a
  22. Controller and a Consumer, which act independently of each other.
  23. The Provider could be the Operating System or the Directory service, which
  24. registers its events (those that can be traced). Each event is associated with
  25. a specific GUID (which is unique). A list of all the events and their GUIDS can
  26. be obtained by running WBEMTest from the RUN menu (for details refer SDK
  27. dcumentation on 'using WBEMTest'). After registering, the Provider carries on
  28. with its usual activity.
  29. When tracing is required, the Controller takes over. The controller creates a
  30. buffer, where the event traces are to be recorded. Tracing is then enabled for
  31. those events that the Controller would like to monitor (this is usually done by
  32. supplying the GUID of that event). The Controller can be made more complex by
  33. giving it the ability to control various parameters with respect to the buffer,
  34. the log file, the type of tracing, etc.
  35. The consumer takes the traces that are logged, and converts them to a human
  36. readable form for further analysis. The Consumer can be as sophisticated as the
  37. developer intends, for instance, an application that triggers another event
  38. based on some value in the event trace log.
  39. Event tracing has a number of uses. It supplements counters, in that it allows
  40. one to derive various metrics not possible using simple raw counters. A
  41. specific example is the response time metric. Event tracing allows more
  42. detailed analysis of system events and hence can be useful for capacity
  43. planning. Event tracing can also be used (potentially by developers) for
  44. initial problem identification and the detection of system bottlenecks.
  45. Note: Since event tracing is more expensive than regular performance counters,
  46. it should not be used on a 7 * 24 basis.
  47. Description of EVENT TRACING samples
  48. ====================================
  49. There are three samples of event tracing tools included with the Event Tracing
  50. resource kit - Tracelog, Tracedmp and Tracedp.
  51. TRACELOG
  52. --------
  53. TraceLog is a Collection Control utility. It uses the Collection Control
  54. portion of the Event Tracing API to start and stop (among other functions)
  55. logging sessions. The logging sessions can be provided by the NT Kernel Logger
  56. or any provider, such as the sample trace data provider, tracedp.exe.
  57. When using logging, this utility specifies WMI to create an ETL file containing
  58. information about the trace events in a binary format. The TraceDmp sample can
  59. be used to read the ETL file and output a CSV and Summary text files.
  60. The basic functions of Tracelog with respect to event tracing are -
  61. Starting, stopping, updating and querying a trace session
  62. Setting up a buffer to which the event traces are to be logged by the
  63. provider
  64. Creating a log file to which the buffer is flushed. (Note that it
  65. could also be set to real time mode in which case the buffer would be
  66. delivered directly to the consumer that wants it)
  67. Providing more specific control over system level tracing
  68. Controlling the level of tracing required
  69. Simply put, tracelog first creates a circular buffer and enables tracing. The
  70. provider (like the Operating System or an application, such as the DS) now
  71. starts tracing events. These traces are written to the buffer. When a buffer is
  72. filled, the data is written to a log file or if real time mode is set, then the
  73. consumer (such as tracedmp or another application) takes the data from the
  74. buffer itself.
  75. Usage: tracelog [actions] [options] | [-h | -help | -?]
  76. Input parameters [actions] - actions you issue with the command. These include
  77. starting, stopping, updating, querying, and flushing a logging session, listing
  78. active logging sessions, enabling and disabling event providers.
  79. Input Parameters [options] - optional parameters you issue with the action
  80. you specified.
  81. Actions
  82. -------
  83. -start [logger name]
  84. begins a tracing session named [logger name]. You need to give a logger name
  85. for the events you would like to trace but if it is a system trace, you need
  86. not specify any logger name, since the default logger name would be taken as
  87. 'NT kernel logger' (see example 1 & 2).
  88. Note System and application (like the DS) traces can be started simultaneously,
  89. but you would have to specify a different logger name for the application trace
  90. (see example 3).
  91. -stop [logger name]
  92. stops a tracing session named [logger name]. You have to specify the instance
  93. name of the events for which you would like tracing to discontinue. For
  94. stopping system tracing, you need not specify any logger name (see example 4).
  95. -update [logger name]
  96. updates the [logger name] session. This is useful when you want to change the
  97. file name of the log file (maybe directing it to a different disk) or change
  98. some buffer parameters, or change to real time mode, etc.
  99. The following can be updated for the kernel logger (system trace) -
  100. "-rt" mode switch. To switch to and from real time mode.
  101. "-f <logfile name>" to switch logfile.
  102. "-ft <n>" to change flush timer.
  103. "-max <n>" to change maximum buffers.
  104. "-nodisk" "-noprocess" "-nothread" "-nonet" "-fio" "-pf" "-hf" "-img" "-cm"
  105. flags.
  106. Note: These flags apply only to the NT kernel logger. All updates should be
  107. provided in a single update statement. If we wanted to switch to real time
  108. processing and increase the max number of buffers, we would issue the command
  109. Tracelog -update -rt -max 40 (all in one statement)
  110. -flush [logger name]
  111. flushes the buffers used for [logger name] into a file. All the buffer used in
  112. the sessions are emptied.
  113. -enable [logger name]
  114. enables event provider(s) for the [logger name] tracing session. Event
  115. providers are specified with GUIDs given in a file. Thus, this action is used
  116. with -guid option.
  117. -disable [logger name]
  118. disables event provider(s) for the [logger name] tracing session. Event
  119. providers are specified with GUIDs given in a file. Thus, this action is used
  120. with -guid option.
  121. "-guid <file>" <file> contains a list of GUIDS for event tracing (each GUID
  122. corresponds to one event provider). One cannot just provide a GUID, even if
  123. it is just one GUID; it has to be included in a file. The exception to this
  124. is if you wish to enable Windows system tracing where no GUID file is
  125. necessary (see example 1).
  126. -enumguid
  127. enumerate Registered Trace Guids.
  128. -x
  129. stops all traces (system and otherwise). This is a complete halt to event
  130. tracing.
  131. -l
  132. is a query to list all the ongoing traces.
  133. -q
  134. is a query to list the system trace only.
  135. Buffer parameters
  136. -----------------
  137. -b <n bytes>
  138. specifies the buffer size. You would generally set the size to be multiples of
  139. the page size (for x86 page size=4Kb). A small size increases the flush
  140. frequency. The kernel, depending on the memory capacity, chooses the default.
  141. -min <n>
  142. number of buffers to pre-allocate. If the logging is frequent, then you want to
  143. set a higher number. Default is 2.
  144. -max <n>
  145. maximum buffers in pool. This limits the amount of memory consumed for each
  146. tracing session. Default is 25.
  147. -ft <n sec>
  148. after a buffer gets filled up, it gets flushed to the log file or to the
  149. consumer (in case of real time tracing). This option allows you to specify the
  150. time after which to force a flush, especially useful for real time tracing.
  151. -age <n min>
  152. if a buffer has been allocated but isn't being used (for the last n minutes),
  153. it is freed. This is generally useful for light tracing, so that memory is
  154. freed. Remember this has nothing to do with the maximum buffers that have been
  155. allocated. That value remains the same. Default is 15 minutes.
  156. Log file options
  157. ----------------
  158. -f <name>
  159. specifies the log file to which the buffer will be flushed. The consumer will
  160. use this file for its analysis. The default name and location is
  161. c:\logfile.etl.
  162. Use a different file name for each instance of tracing required
  163. (see example 5).
  164. -seq <n Mbytes>
  165. indicates that the logging will be sequential and the file size is n Mbytes. By
  166. default, logging is sequential.
  167. -cir <n Mbytes>
  168. indicates that logging will be circular. After the file size is reached,
  169. logging starts from the beginning of the file again. The header information is
  170. not lost however.
  171. -rt
  172. enables real time tracing (see example 6).
  173. Provide more specific control over system level (kernel) tracing
  174. ----------------------------------------------------------------
  175. In order to understand these controls, it is important to understand a little
  176. about system level tracing. The following events can be traced at the system
  177. level -
  178. Process start/end
  179. Disk I/O
  180. Network Tcp/ip, Udp/ip
  181. Thread start/end
  182. Image Load
  183. Registry calls
  184. File I/O
  185. Page Fault
  186. Of these the first 4 are enabled by default. The last 4 aren't enabled, as this
  187. would generate a lot of extra load (resource utilization), which we would like
  188. to avoid.
  189. -noprocess
  190. disables process start/end tracing
  191. -nonet
  192. disables network tracing
  193. -nothread
  194. disables thread start/end tracing
  195. -nodisk
  196. disables disk I/O tracing
  197. -fio
  198. enables file I/O tracing
  199. -pf
  200. enables tracing of soft page faults
  201. -hf
  202. enables tracing of hard page faults. Hard page faults are those that involve a
  203. physical read (i.e. read from the disk).
  204. -img
  205. enables image load tracing
  206. -cm
  207. enables registry calls tracing
  208. -um
  209. enables private process tracing. In this case, the buffer is established in the
  210. private process space instead of the kernel space (which is the default
  211. behavior).
  212. Control the level of tracing required
  213. -------------------------------------
  214. In order to use the following options, a provider would need to have this
  215. functionality enabled in code. One would therefore have to check with the
  216. provider (like the Operating System or the Directory service), before using
  217. these options.
  218. -level <n>
  219. a provider could have number of levels of tracing. A higher number would
  220. indicate a deeper level of tracing.
  221. -flags <n>
  222. by supplying a flag, more specific tracing can be achieved. The flag passed
  223. depends on what functionality the provider has implemented.
  224. Output (display) Parameters - those that appear on the screen when the command
  225. executes
  226. +----------------------------------------------------------------------------+
  227. |Logger Name Name of the logging instance. For the kernel it is |
  228. | 'NT Kernel Logger', else it defaults to what you |
  229. | have provided (see example 2) |
  230. | |
  231. |Logger Id Id of the logger |
  232. |Logger Thread Id Thread ID of the logger |
  233. |Buffer Size The size of the buffer allocated |
  234. |Maximum Buffers The maximum buffers in pool |
  235. |Minimum Buffers The number of buffers to pre-allocate |
  236. |Number of Buffers The number of buffers being currently used |
  237. |Free Buffers The number of buffers in the free list |
  238. |Buffers Written The number of buffers that have already been written |
  239. |Events Lost The events lost (generally in case there is sudden |
  240. | activity and there aren't enough buffers |
  241. | pre-allocated, events could getlost) |
  242. |Log Buffers Lost As the name suggests |
  243. |Real Time Buffers Lost As the name suggests |
  244. |Log File Mode Whether sequential or circular |
  245. |Enabled tracing The various events in the kernel for which tracing |
  246. | has been enabled (like process, disk, network, etc) |
  247. |Log Filename Name and path of the log file |
  248. +----------------------------------------------------------------------------+
  249. Examples
  250. --------
  251. 1. tracelog -start
  252. Starts system event tracing with the log file being the default
  253. c:\logfile.etl
  254. 2. tracelog -enable ds -guid control.guid
  255. Enables event providers specified in file "control.guid" for the tracing
  256. session "ds".
  257. 3. tracelog -start (starts system tracing)
  258. tracelog -start ds -guid control.guid -f c:\dslogfile.etl (starts DS
  259. tracing)
  260. 4. tracelog -stop
  261. tracelog -stop ds
  262. 5. tracelog -start -f kernel_log.etl
  263. tracelog -start ds -guid control.guid -f ds_log.etl
  264. 6. tracelog -update
  265. If current logger is a real-time logger, this will switch current logger to
  266. non real-time, sequential logger.
  267. 7. tracelog -l
  268. Lists all active logging sessions.
  269. TRACEDMP
  270. --------
  271. TraceDmp is a Data Consumer sample. It uses the Event Tracing API to read the
  272. .etl log files (or consume events from the log file) created by TraceLog or by
  273. consuming the real-time events enabled in a data provider. TraceDmp decodes
  274. event data using the format obtained from WMI and outputs the data in a CSV
  275. file that conveniently loads into a database or spreadsheet program. A summary
  276. file is also created to show the sum of all events by each event type during
  277. the session time. You can use TraceDmp flags to specify only a summary file and
  278. omit the CSV file output.
  279. Tracedmp gives us two ways to view the data obtained from event tracing.
  280. Summary.txt file - As the name suggests, this gives us a summary of the events
  281. traced.
  282. CSV (comma-separated format) file - This file sorts events in chronological
  283. order (increasing order of the time-stamp) thus giving us a more detailed view
  284. for each event.
  285. For tracedmp to work, you need to use a log file. A log file is written by the
  286. Event Tracer in a specific format - a header and some variable data. This
  287. format is interpreted by tracedmp. Whilst the header is fixed, the variable data
  288. has to be interpreted separately for each event (eg., process start/end tracing
  289. and disk I/O tracing have their own variable structure). This variable portion
  290. is determined by tracedmp through a lookup in the WMI namespace.
  291. System event descritptors are already present in the WMI namespace by default.
  292. For other custom events, providers should provide the WMI MOF file with the
  293. event layout descriptions. Before running tracedmp, you should register those
  294. event layouts by running mofcomp.exe on that mof file. For instance, the
  295. custom events generated by tracedp.exe are registered as follows:
  296. mofcomp -N:root\wmi tracedp.mof
  297. The format is based on the WMI format, which uses the MOF structure. Details
  298. on event descriptors can be obtained by examining the mof file or by running
  299. WBEMTest.
  300. Tracedmp can also be used for real time event tracing in which case the tool
  301. will read from the buffer itself instead of from the file. The format and
  302. process is similar to that described earlier for log files.
  303. Usage: tracedmp [options] <etl file> | [-? | -h | -help]
  304. The options are explained below:
  305. -o <file>
  306. specify the name of the csv and summary file to which you would like the
  307. results to appear. The default files are dumpfile.csv and summary.txt and are
  308. placed in the tracedmp directory (see example 1).
  309. -rt [logger name]
  310. use this option if real time tracing is being performed. Ensure that tracelog
  311. is also in real time mode and set the logger file name to that provided in the
  312. tracelog command (see example 2).
  313. -summary
  314. select this option if only the summary file is required. In this case, the csv
  315. file is not created.
  316. Output files
  317. -------------
  318. Summary.txt - This contains a count of all the events that occurred while
  319. tracing was being performed. The GUID's for each event are also displayed. It
  320. is important to understand the difference between Event type - start/end and
  321. Event type - DCstart/DCend (DC=Data Collection). The former are those event
  322. that started and ended after tracing had begun, i.e. they had their lifetime
  323. within the tracing period. There are however processes (or threads) that had
  324. begun even before tracing had been turned on, and continue even after tracing
  325. is completed. For such events, their beginning is the time tracing was turned
  326. on and their end is the time tracing was turned off. Hence we have DCstart and
  327. DCend.
  328. Dumpfile.csv - This file can be opened in MSExcel. It contains a list of the
  329. events as they occurred in chronological order. The various fields are:
  330. +----------------------------------------------------------------------------+
  331. |Event name Name of the event being traced |
  332. |----------------------------------------------------------------------------+
  333. |TID Thread Identifer |
  334. |Clock-time The timestamp of the event occurrence |
  335. |Kernel (ms) Time taken by that event in the kernel space |
  336. |User (ms) Time taken by the event in the user space |
  337. |User data ... The variable portion of the header. This is |
  338. | based on the MOF structure, provided in the |
  339. | MOFdata.guid file. This could be more than |
  340. | one column of data. To be able to interpret |
  341. | it, one would have tolook at the above file, |
  342. | or run WEBMTest as explained above. |
  343. |IID Instance ID |
  344. |PIID Parent Instance ID (to which the IID relates) |
  345. +----------------------------------------------------------------------------+
  346. NOTE: IID and PIID are the last two columns (rightmost columns) of the output
  347. file and should not be mistaken for User Data as this field could span multiple
  348. columns.
  349. Examples
  350. Tracedmp c:\logfile.etl
  351. Tracedmp -o myoutput c:\logfile.etl
  352. In this case, the output files will be myoutput.csv and myoutput.txt
  353. Tracedmp -rt ds
  354. This assumes that the ds is being traced in real time mode using the tracelog
  355. command.
  356. TRACEDP
  357. =======
  358. TraceDp is a Data Provider sample. It demonstrates how you can provide event
  359. trace data to the logger or a Data Consumer from user mode code. For a kernel
  360. mode driver there are kernel mode equivalent APIs.
  361. Usage: TraceDp [options] [number of events] [-? | -h | -help]
  362. -UseEventTraceHeader This is the default
  363. -UseEventInstanceHeader
  364. -UseMofPtrFlag
  365. [number of events] default is 5000
  366. -UseEventTraceHeader
  367. Instructs Tracdp to use the EVent trace header for logging
  368. -UseEventInstanceHeader
  369. Instructs Tracedp to generate instance id's for subseuqent use by the tracelog
  370. sample.
  371. -UseMofptrFlag
  372. Instructs Tracedp to use the MOF ptr flag to reference the logged data. When
  373. this option is used, tracedp generates custom events containing various
  374. types of data as specified in tracedp.mof.
  375. USING THE EVENT TRACING SAMPLES
  376. ===============================
  377. The following examples show how to use TraceLog's default settings, logging
  378. registry activity, and using TraceLog to control the TraceDP Data Provider
  379. sample.
  380. Using the Sample's Defaults
  381. ===========================
  382. 1) Start the NT Kernel Logger by using only the -start command. By default the
  383. events are placed in the c:\LogFile.etl file.
  384. C:\TraceLog>tracelog -start
  385. 2) To stop the session use -stop.
  386. C:\TraceLog>tracelog -stop
  387. After you have started and stop a logging session you should find a file
  388. named c:\LogFile.etl. This files store logging events between the time
  389. you use tracelog -start and tracelog -stop.
  390. By default TraceLog will log information regarding processes, threads, disk,
  391. and network I/O.
  392. Using the NT Kernel Logger to Trace Registry Usage
  393. ==================================================
  394. There are various details about kernel processing which is not logged by
  395. default. You can also trace file I/O, page faults, image load information,
  396. registry operations, and process private information. The following is an
  397. example of retrieve trace information when creating a registry key and
  398. adding a registry value.
  399. 1) Start the NT Kernel Logger to get Registry traces. The -cm switch stands for
  400. Configuration Manager, another name for Registry. Also the -noprocess,
  401. -nothread, -nodisk, and -nonet switches are used to remove the clutter so you
  402. can focus on the registry events.
  403. C:\TraceLog>tracelog -start -noprocess -nothread -nodisk -nonet -cm
  404. 2) After starting the logging session, use the registry editor to create a key
  405. at a location of your choice and create a string value.
  406. 3) Next, stop the logging session for the NT Kernel Logger
  407. C:\TraceLog>tracelog -stop
  408. 4) Use the TraceDmp utility to dump the information to a CSV file and Summary.
  409. Go to the tracedmp directory and run tracedmp with no arguments. By default
  410. it will read the C:\LogFile.Etl and produce the DumpFile.csv and Summary.txt
  411. files. Below is an example of the Summary.txt.
  412. Files Processed:
  413. C:\Logfile.Etl
  414. Total Buffers Processed 8
  415. Total Events Processed 999
  416. Total Events Lost 0
  417. Elapsed Time 33 sec
  418. +----------------------------------------------------------------------------+
  419. |EventCount EventName EventType Guid |
  420. +----------------------------------------------------------------------------+
  421. | 29 Process DCStart 3d6fa8d0-fe05-11d0-9dda-00c04fd7ba7c|
  422. | 29 Process DCEnd 3d6fa8d0-fe05-11d0-9dda-00c04fd7ba7c|
  423. | 305 Thread DCStart 3d6fa8d1-fe05-11d0-9dda-00c04fd7ba7c|
  424. | 304 Thread DCEnd 3d6fa8d1-fe05-11d0-9dda-00c04fd7ba7c|
  425. | 2 Registry Create ae53722e-c863-11d2-8659-00c04fa321a1|
  426. | 176 Registry Open ae53722e-c863-11d2-8659-00c04fa321a1|
  427. | 86 Registry Query ae53722e-c863-11d2-8659-00c04fa321a1|
  428. | 1 Registry SetValue ae53722e-c863-11d2-8659-00c04fa321a1|
  429. | 23 Registry QueryValue ae53722e-c863-11d2-8659-00c04fa321a1|
  430. | 40 Registry EnumerateKey ae53722e-c863-11d2-8659-00c04fa321a1|
  431. | 1 Registry EnumerateValueKey ae53722e-c863-11d2-8659-00c04fa321a1|
  432. | 2 Registry Flush ae53722e-c863-11d2-8659-00c04fa321a1|
  433. | 1 Header General 68fdd900-4a3e-11d1-84f4-0000f80464e3|
  434. +----------------------------------------------------------------------------+
  435. Using TraceLog to Control the TraceDp Sample
  436. =============================================
  437. TraceLog can be used with the TraceDp (Sample Data Provider) example to create
  438. an etl file. But first add the control guid to the Control.Guid file in the
  439. TraceLog directory. This is used as input for TraceLog.
  440. 1) Open Control.Guid with Notepad.exe and add the following line.
  441. d58c126f-b309-11d1-969e-0000f875a5bc tracedp
  442. The name at the end of the GUID is arbitrary. The name will appear in the
  443. output CSV and summary text files.
  444. 2) Start the TraceDp sample data provide. The following is an example usage
  445. C:\tracedp>tracedp -UseMofPtrFlag
  446. You should see the following output:
  447. Trace registered successfully
  448. Testing Logger with 5000 events
  449. 3) Start the logging session and provide the Control.Guid file as input. You'll
  450. need to start a new command prompt.
  451. C:\TraceLog>tracelog -start tracedp -guid control.guid
  452. Note that the name after -start is a session name and is arbitrary. At this
  453. point you should see the following text output with "."s printed to show
  454. progress:
  455. Logging enabled to 0x0000000000000002
  456. ..........................
  457. 4) After you are satisfied with the amount of data, stop the logging session.
  458. C:\TraceLog>tracelog -stop tracedp -guid control.guid
  459. 5) Register the events generated by tracedp onto WMI by running mofcomp. The
  460. WMI MOF file is given as tracedp.mof in the tracdp directory.
  461. 6) Now use TraceDmp to dump the data that was logged.
  462. C:\TraceDmp>tracedmp
  463. The following is an example the of Summary.txt file that is output.
  464. Files Processed:
  465. test.etl
  466. Total Buffers Processed 10
  467. Total Events Processed 1001
  468. Total Events Lost 0
  469. Start Time 0x01C08729527557B9
  470. End Time 0x01C0872A1B1C4BFA
  471. Elapsed Time 336 sec
  472. +------------------------------------------------------------------------------+
  473. | Count Event Name Event Type Guid |
  474. +------------------------------------------------------------------------------+
  475. | 1 EventTrace Header {68fdd900-4a3e-11d1-84f4-0000f80464e3}|
  476. | 250 TraceDP Strings {ce5b1020-8ea9-11d0-a4ec-00a0c9062910}|
  477. | 250 TraceDP Integers {ce5b1020-8ea9-11d0-a4ec-00a0c9062910}|
  478. | 250 TraceDP Floats {ce5b1020-8ea9-11d0-a4ec-00a0c9062910}|
  479. | 250 TraceDP Arrays {ce5b1020-8ea9-11d0-a4ec-00a0c9062910}|
  480. +------------------------------------------------------------------------------+