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.

634 lines
25 KiB

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