Leaked source code of windows server 2003
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.

2365 lines
63 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1991 - 1999
  3. Module Name:
  4. diskperf.c
  5. Abstract:
  6. This driver monitors disk accesses capturing performance data.
  7. Environment:
  8. kernel mode only
  9. Notes:
  10. --*/
  11. #define INITGUID
  12. #include "ntddk.h"
  13. #include "ntdddisk.h"
  14. #include "stdarg.h"
  15. #include "stdio.h"
  16. #include <ntddvol.h>
  17. #include <mountdev.h>
  18. #include "wmistr.h"
  19. #include "wmidata.h"
  20. #include "wmiguid.h"
  21. #include "wmilib.h"
  22. #ifdef POOL_TAGGING
  23. #ifdef ExAllocatePool
  24. #undef ExAllocatePool
  25. #endif
  26. #define ExAllocatePool(a,b) ExAllocatePoolWithTag(a,b,'frPD')
  27. #endif
  28. #define DISKPERF_MAXSTR 64
  29. //
  30. // Device Extension
  31. //
  32. typedef struct _DEVICE_EXTENSION {
  33. //
  34. // Back pointer to device object
  35. //
  36. PDEVICE_OBJECT DeviceObject;
  37. //
  38. // Target Device Object
  39. //
  40. PDEVICE_OBJECT TargetDeviceObject;
  41. //
  42. // Physical device object
  43. //
  44. PDEVICE_OBJECT PhysicalDeviceObject;
  45. //
  46. // Disk number for reference in WMI
  47. //
  48. ULONG DiskNumber;
  49. //
  50. // If device is enabled for counting always
  51. //
  52. LONG EnabledAlways;
  53. //
  54. // Use to keep track of Volume info from ntddvol.h
  55. //
  56. WCHAR StorageManagerName[8];
  57. //
  58. // Disk performance counters
  59. // and locals used to compute counters
  60. //
  61. ULONG Processors;
  62. PDISK_PERFORMANCE DiskCounters; // per processor counters
  63. LARGE_INTEGER LastIdleClock;
  64. LONG QueueDepth;
  65. LONG CountersEnabled;
  66. //
  67. // must synchronize paging path notifications
  68. //
  69. KEVENT PagingPathCountEvent;
  70. ULONG PagingPathCount;
  71. //
  72. // Physical Device name or WMI Instance Name
  73. //
  74. UNICODE_STRING PhysicalDeviceName;
  75. WCHAR PhysicalDeviceNameBuffer[DISKPERF_MAXSTR];
  76. //
  77. // Private context for using WmiLib
  78. //
  79. WMILIB_CONTEXT WmilibContext;
  80. } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
  81. #define DEVICE_EXTENSION_SIZE sizeof(DEVICE_EXTENSION)
  82. #define PROCESSOR_COUNTERS_SIZE FIELD_OFFSET(DISK_PERFORMANCE, QueryTime)
  83. /*
  84. Layout of Per Processor Counters is a contiguous block of memory:
  85. Processor 1
  86. +-----------------------+ +-----------------------+
  87. |PROCESSOR_COUNTERS_SIZE| ... |PROCESSOR_COUNTERS_SIZE|
  88. +-----------------------+ +-----------------------+
  89. where PROCESSOR_COUNTERS_SIZE is less than sizeof(DISK_PERFORMANCE) since
  90. we only put those we actually use for counting.
  91. */
  92. UNICODE_STRING DiskPerfRegistryPath;
  93. //
  94. // Function declarations
  95. //
  96. NTSTATUS
  97. DriverEntry(
  98. IN PDRIVER_OBJECT DriverObject,
  99. IN PUNICODE_STRING RegistryPath
  100. );
  101. NTSTATUS
  102. DiskPerfForwardIrpSynchronous(
  103. IN PDEVICE_OBJECT DeviceObject,
  104. IN PIRP Irp
  105. );
  106. NTSTATUS
  107. DiskPerfAddDevice(
  108. IN PDRIVER_OBJECT DriverObject,
  109. IN PDEVICE_OBJECT PhysicalDeviceObject
  110. );
  111. NTSTATUS
  112. DiskPerfDispatchPnp(
  113. IN PDEVICE_OBJECT DeviceObject,
  114. IN PIRP Irp
  115. );
  116. NTSTATUS
  117. DiskPerfDispatchPower(
  118. IN PDEVICE_OBJECT DeviceObject,
  119. IN PIRP Irp
  120. );
  121. NTSTATUS
  122. DiskPerfStartDevice(
  123. IN PDEVICE_OBJECT DeviceObject,
  124. IN PIRP Irp
  125. );
  126. NTSTATUS
  127. DiskPerfRemoveDevice(
  128. IN PDEVICE_OBJECT DeviceObject,
  129. IN PIRP Irp
  130. );
  131. NTSTATUS
  132. DiskPerfSendToNextDriver(
  133. IN PDEVICE_OBJECT DeviceObject,
  134. IN PIRP Irp
  135. );
  136. NTSTATUS
  137. DiskPerfCreate(
  138. IN PDEVICE_OBJECT DeviceObject,
  139. IN PIRP Irp
  140. );
  141. NTSTATUS
  142. DiskPerfReadWrite(
  143. IN PDEVICE_OBJECT DeviceObject,
  144. IN PIRP Irp
  145. );
  146. NTSTATUS
  147. DiskPerfIoCompletion(
  148. IN PDEVICE_OBJECT DeviceObject,
  149. IN PIRP Irp,
  150. IN PVOID Context
  151. );
  152. NTSTATUS
  153. DiskPerfDeviceControl(
  154. IN PDEVICE_OBJECT DeviceObject,
  155. IN PIRP Irp
  156. );
  157. NTSTATUS
  158. DiskPerfShutdownFlush(
  159. IN PDEVICE_OBJECT DeviceObject,
  160. IN PIRP Irp
  161. );
  162. VOID
  163. DiskPerfUnload(
  164. IN PDRIVER_OBJECT DriverObject
  165. );
  166. NTSTATUS DiskPerfWmi(
  167. IN PDEVICE_OBJECT DeviceObject,
  168. IN PIRP Irp
  169. );
  170. VOID
  171. DiskPerfLogError(
  172. IN PDEVICE_OBJECT DeviceObject,
  173. IN ULONG UniqueId,
  174. IN NTSTATUS ErrorCode,
  175. IN NTSTATUS Status
  176. );
  177. NTSTATUS
  178. DiskPerfRegisterDevice(
  179. IN PDEVICE_OBJECT DeviceObject
  180. );
  181. NTSTATUS
  182. DiskPerfIrpCompletion(
  183. IN PDEVICE_OBJECT DeviceObject,
  184. IN PIRP Irp,
  185. IN PVOID Context
  186. );
  187. NTSTATUS
  188. DiskperfQueryWmiRegInfo(
  189. IN PDEVICE_OBJECT DeviceObject,
  190. OUT ULONG *RegFlags,
  191. OUT PUNICODE_STRING InstanceName,
  192. OUT PUNICODE_STRING *RegistryPath,
  193. OUT PUNICODE_STRING MofResourceName,
  194. OUT PDEVICE_OBJECT *Pdo
  195. );
  196. NTSTATUS
  197. DiskperfQueryWmiDataBlock(
  198. IN PDEVICE_OBJECT DeviceObject,
  199. IN PIRP Irp,
  200. IN ULONG GuidIndex,
  201. IN ULONG InstanceIndex,
  202. IN ULONG InstanceCount,
  203. IN OUT PULONG InstanceLengthArray,
  204. IN ULONG BufferAvail,
  205. OUT PUCHAR Buffer
  206. );
  207. VOID
  208. DiskPerfSyncFilterWithTarget(
  209. IN PDEVICE_OBJECT FilterDevice,
  210. IN PDEVICE_OBJECT TargetDevice
  211. );
  212. NTSTATUS
  213. DiskperfWmiFunctionControl(
  214. IN PDEVICE_OBJECT DeviceObject,
  215. IN PIRP Irp,
  216. IN ULONG GuidIndex,
  217. IN WMIENABLEDISABLECONTROL Function,
  218. IN BOOLEAN Enable
  219. );
  220. VOID
  221. DiskPerfAddCounters(
  222. IN OUT PDISK_PERFORMANCE TotalCounters,
  223. IN PDISK_PERFORMANCE NewCounters,
  224. IN LARGE_INTEGER Frequency
  225. );
  226. #if DBG
  227. ULONG DiskPerfDebug = 0;
  228. VOID
  229. DiskPerfDebugPrint(
  230. ULONG DebugPrintLevel,
  231. PCCHAR DebugMessage,
  232. ...
  233. );
  234. #define DebugPrint(x) DiskPerfDebugPrint x
  235. #else
  236. #define DebugPrint(x)
  237. #endif
  238. //
  239. // Define the sections that allow for discarding (i.e. paging) some of
  240. // the code.
  241. //
  242. #ifdef ALLOC_PRAGMA
  243. #pragma alloc_text (INIT, DriverEntry)
  244. #pragma alloc_text (PAGE, DiskPerfCreate)
  245. #pragma alloc_text (PAGE, DiskPerfAddDevice)
  246. #pragma alloc_text (PAGE, DiskPerfDispatchPnp)
  247. #pragma alloc_text (PAGE, DiskPerfStartDevice)
  248. #pragma alloc_text (PAGE, DiskPerfRemoveDevice)
  249. #pragma alloc_text (PAGE, DiskPerfUnload)
  250. #pragma alloc_text (PAGE, DiskPerfWmi)
  251. #pragma alloc_text (PAGE, DiskperfQueryWmiRegInfo)
  252. #pragma alloc_text (PAGE, DiskperfQueryWmiDataBlock)
  253. #pragma alloc_text (PAGE, DiskPerfRegisterDevice)
  254. #pragma alloc_text (PAGE, DiskPerfSyncFilterWithTarget)
  255. #endif
  256. WMIGUIDREGINFO DiskperfGuidList[] =
  257. {
  258. { &DiskPerfGuid,
  259. 1,
  260. 0
  261. }
  262. };
  263. #define DiskperfGuidCount (sizeof(DiskperfGuidList) / sizeof(WMIGUIDREGINFO))
  264. #define USE_PERF_CTR
  265. #ifdef USE_PERF_CTR
  266. #define DiskPerfGetClock(a, b) (a) = KeQueryPerformanceCounter((b))
  267. #else
  268. #define DiskPerfGetClock(a, b) KeQuerySystemTime(&(a))
  269. #endif
  270. NTSTATUS
  271. DriverEntry(
  272. IN PDRIVER_OBJECT DriverObject,
  273. IN PUNICODE_STRING RegistryPath
  274. )
  275. /*++
  276. Routine Description:
  277. Installable driver initialization entry point.
  278. This entry point is called directly by the I/O manager to set up the disk
  279. performance driver. The driver object is set up and then the Pnp manager
  280. calls DiskPerfAddDevice to attach to the boot devices.
  281. Arguments:
  282. DriverObject - The disk performance driver object.
  283. RegistryPath - pointer to a unicode string representing the path,
  284. to driver-specific key in the registry.
  285. Return Value:
  286. STATUS_SUCCESS if successful
  287. --*/
  288. {
  289. ULONG ulIndex;
  290. PDRIVER_DISPATCH * dispatch;
  291. //
  292. // Remember registry path
  293. //
  294. DiskPerfRegistryPath.MaximumLength = RegistryPath->Length
  295. + sizeof(UNICODE_NULL);
  296. DiskPerfRegistryPath.Buffer = ExAllocatePool(
  297. PagedPool,
  298. DiskPerfRegistryPath.MaximumLength);
  299. if (DiskPerfRegistryPath.Buffer != NULL)
  300. {
  301. RtlCopyUnicodeString(&DiskPerfRegistryPath, RegistryPath);
  302. } else {
  303. DiskPerfRegistryPath.Length = 0;
  304. DiskPerfRegistryPath.MaximumLength = 0;
  305. }
  306. //
  307. // Create dispatch points
  308. //
  309. for (ulIndex = 0, dispatch = DriverObject->MajorFunction;
  310. ulIndex <= IRP_MJ_MAXIMUM_FUNCTION;
  311. ulIndex++, dispatch++) {
  312. *dispatch = DiskPerfSendToNextDriver;
  313. }
  314. //
  315. // Set up the device driver entry points.
  316. //
  317. DriverObject->MajorFunction[IRP_MJ_CREATE] = DiskPerfCreate;
  318. DriverObject->MajorFunction[IRP_MJ_READ] = DiskPerfReadWrite;
  319. DriverObject->MajorFunction[IRP_MJ_WRITE] = DiskPerfReadWrite;
  320. DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DiskPerfDeviceControl;
  321. DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = DiskPerfWmi;
  322. DriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = DiskPerfShutdownFlush;
  323. DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS] = DiskPerfShutdownFlush;
  324. DriverObject->MajorFunction[IRP_MJ_PNP] = DiskPerfDispatchPnp;
  325. DriverObject->MajorFunction[IRP_MJ_POWER] = DiskPerfDispatchPower;
  326. DriverObject->DriverExtension->AddDevice = DiskPerfAddDevice;
  327. DriverObject->DriverUnload = DiskPerfUnload;
  328. return(STATUS_SUCCESS);
  329. } // end DriverEntry()
  330. #define FILTER_DEVICE_PROPOGATE_FLAGS 0
  331. #define FILTER_DEVICE_PROPOGATE_CHARACTERISTICS (FILE_REMOVABLE_MEDIA | \
  332. FILE_READ_ONLY_DEVICE | \
  333. FILE_FLOPPY_DISKETTE \
  334. )
  335. VOID
  336. DiskPerfSyncFilterWithTarget(
  337. IN PDEVICE_OBJECT FilterDevice,
  338. IN PDEVICE_OBJECT TargetDevice
  339. )
  340. {
  341. ULONG propFlags;
  342. PAGED_CODE();
  343. //
  344. // Propogate all useful flags from target to diskperf. MountMgr will look
  345. // at the diskperf object capabilities to figure out if the disk is
  346. // a removable and perhaps other things.
  347. //
  348. propFlags = TargetDevice->Flags & FILTER_DEVICE_PROPOGATE_FLAGS;
  349. FilterDevice->Flags |= propFlags;
  350. propFlags = TargetDevice->Characteristics & FILTER_DEVICE_PROPOGATE_CHARACTERISTICS;
  351. FilterDevice->Characteristics |= propFlags;
  352. }
  353. NTSTATUS
  354. DiskPerfAddDevice(
  355. IN PDRIVER_OBJECT DriverObject,
  356. IN PDEVICE_OBJECT PhysicalDeviceObject
  357. )
  358. /*++
  359. Routine Description:
  360. Creates and initializes a new filter device object FiDO for the
  361. corresponding PDO. Then it attaches the device object to the device
  362. stack of the drivers for the device.
  363. Arguments:
  364. DriverObject - Disk performance driver object.
  365. PhysicalDeviceObject - Physical Device Object from the underlying layered driver
  366. Return Value:
  367. NTSTATUS
  368. --*/
  369. {
  370. NTSTATUS status;
  371. IO_STATUS_BLOCK ioStatus;
  372. PDEVICE_OBJECT filterDeviceObject;
  373. PDEVICE_EXTENSION deviceExtension;
  374. PIRP irp;
  375. STORAGE_DEVICE_NUMBER number;
  376. ULONG registrationFlag = 0;
  377. PWMILIB_CONTEXT wmilibContext;
  378. PCHAR buffer;
  379. ULONG buffersize;
  380. PAGED_CODE();
  381. //
  382. // Create a filter device object for this device (partition).
  383. //
  384. DebugPrint((2, "DiskPerfAddDevice: Driver %X Device %X\n",
  385. DriverObject, PhysicalDeviceObject));
  386. status = IoCreateDevice(DriverObject,
  387. DEVICE_EXTENSION_SIZE,
  388. NULL,
  389. FILE_DEVICE_DISK,
  390. FILE_DEVICE_SECURE_OPEN,
  391. FALSE,
  392. &filterDeviceObject);
  393. if (!NT_SUCCESS(status)) {
  394. DebugPrint((1, "DiskPerfAddDevice: Cannot create filterDeviceObject\n"));
  395. return status;
  396. }
  397. filterDeviceObject->Flags |= DO_DIRECT_IO;
  398. deviceExtension = (PDEVICE_EXTENSION) filterDeviceObject->DeviceExtension;
  399. RtlZeroMemory(deviceExtension, DEVICE_EXTENSION_SIZE);
  400. DiskPerfGetClock(deviceExtension->LastIdleClock, NULL);
  401. DebugPrint((10, "DiskPerfAddDevice: LIC=%I64u\n",
  402. deviceExtension->LastIdleClock));
  403. //
  404. // Allocate per processor counters. NOTE: To save some memory, it does
  405. // allocate memory beyond QueryTime. Remember to expand size if there
  406. // is a need to use anything beyond this
  407. //
  408. deviceExtension->Processors = KeNumberProcessors;
  409. buffersize= PROCESSOR_COUNTERS_SIZE * deviceExtension->Processors;
  410. buffer = (PCHAR) ExAllocatePool(NonPagedPool, buffersize);
  411. if (buffer != NULL) {
  412. RtlZeroMemory(buffer, buffersize);
  413. deviceExtension->DiskCounters = (PDISK_PERFORMANCE) buffer;
  414. }
  415. else {
  416. DiskPerfLogError(
  417. filterDeviceObject,
  418. 513,
  419. STATUS_SUCCESS,
  420. IO_ERR_INSUFFICIENT_RESOURCES);
  421. }
  422. //
  423. // Attaches the device object to the highest device object in the chain and
  424. // return the previously highest device object, which is passed to
  425. // IoCallDriver when pass IRPs down the device stack
  426. //
  427. deviceExtension->PhysicalDeviceObject = PhysicalDeviceObject;
  428. deviceExtension->TargetDeviceObject =
  429. IoAttachDeviceToDeviceStack(filterDeviceObject, PhysicalDeviceObject);
  430. if (deviceExtension->TargetDeviceObject == NULL) {
  431. IoDeleteDevice(filterDeviceObject);
  432. DebugPrint((1, "DiskPerfAddDevice: Unable to attach %X to target %X\n",
  433. filterDeviceObject, PhysicalDeviceObject));
  434. return STATUS_NO_SUCH_DEVICE;
  435. }
  436. //
  437. // Save the filter device object in the device extension
  438. //
  439. deviceExtension->DeviceObject = filterDeviceObject;
  440. deviceExtension->PhysicalDeviceName.Buffer
  441. = deviceExtension->PhysicalDeviceNameBuffer;
  442. KeInitializeEvent(&deviceExtension->PagingPathCountEvent,
  443. NotificationEvent, TRUE);
  444. //
  445. // Initialize WMI library context
  446. //
  447. wmilibContext = &deviceExtension->WmilibContext;
  448. RtlZeroMemory(wmilibContext, sizeof(WMILIB_CONTEXT));
  449. wmilibContext->GuidCount = DiskperfGuidCount;
  450. wmilibContext->GuidList = DiskperfGuidList;
  451. wmilibContext->QueryWmiRegInfo = DiskperfQueryWmiRegInfo;
  452. wmilibContext->QueryWmiDataBlock = DiskperfQueryWmiDataBlock;
  453. wmilibContext->WmiFunctionControl = DiskperfWmiFunctionControl;
  454. //
  455. // default to DO_POWER_PAGABLE
  456. //
  457. filterDeviceObject->Flags |= DO_POWER_PAGABLE;
  458. //
  459. // Clear the DO_DEVICE_INITIALIZING flag
  460. //
  461. filterDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
  462. return STATUS_SUCCESS;
  463. } // end DiskPerfAddDevice()
  464. NTSTATUS
  465. DiskPerfDispatchPnp(
  466. IN PDEVICE_OBJECT DeviceObject,
  467. IN PIRP Irp
  468. )
  469. /*++
  470. Routine Description:
  471. Dispatch for PNP
  472. Arguments:
  473. DeviceObject - Supplies the device object.
  474. Irp - Supplies the I/O request packet.
  475. Return Value:
  476. NTSTATUS
  477. --*/
  478. {
  479. PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
  480. NTSTATUS status;
  481. PDEVICE_EXTENSION deviceExtension;
  482. PAGED_CODE();
  483. DebugPrint((2, "DiskPerfDispatchPnp: Device %X Irp %X\n",
  484. DeviceObject, Irp));
  485. switch(irpSp->MinorFunction) {
  486. case IRP_MN_START_DEVICE:
  487. //
  488. // Call the Start Routine handler to schedule a completion routine
  489. //
  490. DebugPrint((3,
  491. "DiskPerfDispatchPnp: Schedule completion for START_DEVICE"));
  492. status = DiskPerfStartDevice(DeviceObject, Irp);
  493. break;
  494. case IRP_MN_REMOVE_DEVICE:
  495. {
  496. //
  497. // Call the Remove Routine handler to schedule a completion routine
  498. //
  499. DebugPrint((3,
  500. "DiskPerfDispatchPnp: Schedule completion for REMOVE_DEVICE"));
  501. status = DiskPerfRemoveDevice(DeviceObject, Irp);
  502. break;
  503. }
  504. case IRP_MN_DEVICE_USAGE_NOTIFICATION:
  505. {
  506. PIO_STACK_LOCATION irpStack;
  507. ULONG count;
  508. BOOLEAN setPagable;
  509. DebugPrint((3,
  510. "DiskPerfDispatchPnp: Processing DEVICE_USAGE_NOTIFICATION"));
  511. irpStack = IoGetCurrentIrpStackLocation(Irp);
  512. if (irpStack->Parameters.UsageNotification.Type != DeviceUsageTypePaging) {
  513. status = DiskPerfSendToNextDriver(DeviceObject, Irp);
  514. break; // out of case statement
  515. }
  516. deviceExtension = DeviceObject->DeviceExtension;
  517. //
  518. // wait on the paging path event
  519. //
  520. status = KeWaitForSingleObject(&deviceExtension->PagingPathCountEvent,
  521. Executive, KernelMode,
  522. FALSE, NULL);
  523. //
  524. // if removing last paging device, need to set DO_POWER_PAGABLE
  525. // bit here, and possible re-set it below on failure.
  526. //
  527. setPagable = FALSE;
  528. if (!irpStack->Parameters.UsageNotification.InPath &&
  529. deviceExtension->PagingPathCount == 1 ) {
  530. //
  531. // removing the last paging file
  532. // must have DO_POWER_PAGABLE bits set
  533. //
  534. if (DeviceObject->Flags & DO_POWER_INRUSH) {
  535. DebugPrint((3, "DiskPerfDispatchPnp: last paging file "
  536. "removed but DO_POWER_INRUSH set, so not "
  537. "setting PAGABLE bit "
  538. "for DO %p\n", DeviceObject));
  539. } else {
  540. DebugPrint((2, "DiskPerfDispatchPnp: Setting PAGABLE "
  541. "bit for DO %p\n", DeviceObject));
  542. DeviceObject->Flags |= DO_POWER_PAGABLE;
  543. setPagable = TRUE;
  544. }
  545. }
  546. //
  547. // send the irp synchronously
  548. //
  549. status = DiskPerfForwardIrpSynchronous(DeviceObject, Irp);
  550. //
  551. // now deal with the failure and success cases.
  552. // note that we are not allowed to fail the irp
  553. // once it is sent to the lower drivers.
  554. //
  555. if (NT_SUCCESS(status)) {
  556. IoAdjustPagingPathCount(
  557. &deviceExtension->PagingPathCount,
  558. irpStack->Parameters.UsageNotification.InPath);
  559. if (irpStack->Parameters.UsageNotification.InPath) {
  560. if (deviceExtension->PagingPathCount == 1) {
  561. //
  562. // first paging file addition
  563. //
  564. DebugPrint((3, "DiskPerfDispatchPnp: Clearing PAGABLE bit "
  565. "for DO %p\n", DeviceObject));
  566. DeviceObject->Flags &= ~DO_POWER_PAGABLE;
  567. }
  568. }
  569. } else {
  570. //
  571. // cleanup the changes done above
  572. //
  573. if (setPagable == TRUE) {
  574. DeviceObject->Flags &= ~DO_POWER_PAGABLE;
  575. setPagable = FALSE;
  576. }
  577. }
  578. //
  579. // set the event so the next one can occur.
  580. //
  581. KeSetEvent(&deviceExtension->PagingPathCountEvent,
  582. IO_NO_INCREMENT, FALSE);
  583. //
  584. // and complete the irp
  585. //
  586. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  587. return status;
  588. break;
  589. }
  590. default:
  591. DebugPrint((3,
  592. "DiskPerfDispatchPnp: Forwarding irp"));
  593. //
  594. // Simply forward all other Irps
  595. //
  596. return DiskPerfSendToNextDriver(DeviceObject, Irp);
  597. }
  598. return status;
  599. } // end DiskPerfDispatchPnp()
  600. NTSTATUS
  601. DiskPerfIrpCompletion(
  602. IN PDEVICE_OBJECT DeviceObject,
  603. IN PIRP Irp,
  604. IN PVOID Context
  605. )
  606. /*++
  607. Routine Description:
  608. Forwarded IRP completion routine. Set an event and return
  609. STATUS_MORE_PROCESSING_REQUIRED. Irp forwarder will wait on this
  610. event and then re-complete the irp after cleaning up.
  611. Arguments:
  612. DeviceObject is the device object of the WMI driver
  613. Irp is the WMI irp that was just completed
  614. Context is a PKEVENT that forwarder will wait on
  615. Return Value:
  616. STATUS_MORE_PORCESSING_REQUIRED
  617. --*/
  618. {
  619. PKEVENT Event = (PKEVENT) Context;
  620. UNREFERENCED_PARAMETER(DeviceObject);
  621. UNREFERENCED_PARAMETER(Irp);
  622. KeSetEvent(Event, IO_NO_INCREMENT, FALSE);
  623. return(STATUS_MORE_PROCESSING_REQUIRED);
  624. } // end DiskPerfIrpCompletion()
  625. NTSTATUS
  626. DiskPerfStartDevice(
  627. IN PDEVICE_OBJECT DeviceObject,
  628. IN PIRP Irp
  629. )
  630. /*++
  631. Routine Description:
  632. This routine is called when a Pnp Start Irp is received.
  633. It will schedule a completion routine to initialize and register with WMI.
  634. Arguments:
  635. DeviceObject - a pointer to the device object
  636. Irp - a pointer to the irp
  637. Return Value:
  638. Status of processing the Start Irp
  639. --*/
  640. {
  641. PDEVICE_EXTENSION deviceExtension;
  642. KEVENT event;
  643. NTSTATUS status;
  644. PAGED_CODE();
  645. deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  646. status = DiskPerfForwardIrpSynchronous(DeviceObject, Irp);
  647. DiskPerfSyncFilterWithTarget(DeviceObject,
  648. deviceExtension->TargetDeviceObject);
  649. //
  650. // Complete WMI registration
  651. //
  652. DiskPerfRegisterDevice(DeviceObject);
  653. //
  654. // Complete the Irp
  655. //
  656. Irp->IoStatus.Status = status;
  657. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  658. return status;
  659. }
  660. NTSTATUS
  661. DiskPerfRemoveDevice(
  662. IN PDEVICE_OBJECT DeviceObject,
  663. IN PIRP Irp
  664. )
  665. /*++
  666. Routine Description:
  667. This routine is called when the device is to be removed.
  668. It will de-register itself from WMI first, detach itself from the
  669. stack before deleting itself.
  670. Arguments:
  671. DeviceObject - a pointer to the device object
  672. Irp - a pointer to the irp
  673. Return Value:
  674. Status of removing the device
  675. --*/
  676. {
  677. NTSTATUS status;
  678. PDEVICE_EXTENSION deviceExtension;
  679. PWMILIB_CONTEXT wmilibContext;
  680. PAGED_CODE();
  681. deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  682. //
  683. // Remove registration with WMI first
  684. //
  685. IoWMIRegistrationControl(DeviceObject, WMIREG_ACTION_DEREGISTER);
  686. //
  687. // quickly zero out the count first to invalid the structure
  688. //
  689. wmilibContext = &deviceExtension->WmilibContext;
  690. InterlockedExchange(
  691. (PLONG) &(wmilibContext->GuidCount),
  692. (LONG) 0);
  693. RtlZeroMemory(wmilibContext, sizeof(WMILIB_CONTEXT));
  694. status = DiskPerfForwardIrpSynchronous(DeviceObject, Irp);
  695. IoDetachDevice(deviceExtension->TargetDeviceObject);
  696. IoDeleteDevice(DeviceObject);
  697. //
  698. // Complete the Irp
  699. //
  700. Irp->IoStatus.Status = status;
  701. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  702. return status;
  703. }
  704. NTSTATUS
  705. DiskPerfSendToNextDriver(
  706. IN PDEVICE_OBJECT DeviceObject,
  707. IN PIRP Irp
  708. )
  709. /*++
  710. Routine Description:
  711. This routine sends the Irp to the next driver in line
  712. when the Irp is not processed by this driver.
  713. Arguments:
  714. DeviceObject
  715. Irp
  716. Return Value:
  717. NTSTATUS
  718. --*/
  719. {
  720. PDEVICE_EXTENSION deviceExtension;
  721. IoSkipCurrentIrpStackLocation(Irp);
  722. deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  723. return IoCallDriver(deviceExtension->TargetDeviceObject, Irp);
  724. } // end DiskPerfSendToNextDriver()
  725. NTSTATUS
  726. DiskPerfDispatchPower(
  727. IN PDEVICE_OBJECT DeviceObject,
  728. IN PIRP Irp
  729. )
  730. {
  731. PDEVICE_EXTENSION deviceExtension;
  732. PoStartNextPowerIrp(Irp);
  733. IoSkipCurrentIrpStackLocation(Irp);
  734. deviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
  735. return PoCallDriver(deviceExtension->TargetDeviceObject, Irp);
  736. } // end DiskPerfDispatchPower
  737. NTSTATUS
  738. DiskPerfForwardIrpSynchronous(
  739. IN PDEVICE_OBJECT DeviceObject,
  740. IN PIRP Irp
  741. )
  742. /*++
  743. Routine Description:
  744. This routine sends the Irp to the next driver in line
  745. when the Irp needs to be processed by the lower drivers
  746. prior to being processed by this one.
  747. Arguments:
  748. DeviceObject
  749. Irp
  750. Return Value:
  751. NTSTATUS
  752. --*/
  753. {
  754. PDEVICE_EXTENSION deviceExtension;
  755. KEVENT event;
  756. NTSTATUS status;
  757. KeInitializeEvent(&event, NotificationEvent, FALSE);
  758. deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  759. //
  760. // copy the irpstack for the next device
  761. //
  762. IoCopyCurrentIrpStackLocationToNext(Irp);
  763. //
  764. // set a completion routine
  765. //
  766. IoSetCompletionRoutine(Irp, DiskPerfIrpCompletion,
  767. &event, TRUE, TRUE, TRUE);
  768. //
  769. // call the next lower device
  770. //
  771. status = IoCallDriver(deviceExtension->TargetDeviceObject, Irp);
  772. //
  773. // wait for the actual completion
  774. //
  775. if (status == STATUS_PENDING) {
  776. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  777. status = Irp->IoStatus.Status;
  778. }
  779. return status;
  780. } // end DiskPerfForwardIrpSynchronous()
  781. NTSTATUS
  782. DiskPerfCreate(
  783. IN PDEVICE_OBJECT DeviceObject,
  784. IN PIRP Irp
  785. )
  786. /*++
  787. Routine Description:
  788. This routine services open commands. It establishes
  789. the driver's existance by returning status success.
  790. Arguments:
  791. DeviceObject - Context for the activity.
  792. Irp - The device control argument block.
  793. Return Value:
  794. NT Status
  795. --*/
  796. {
  797. PAGED_CODE();
  798. UNREFERENCED_PARAMETER(DeviceObject);
  799. Irp->IoStatus.Status = STATUS_SUCCESS;
  800. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  801. return STATUS_SUCCESS;
  802. } // end DiskPerfCreate()
  803. NTSTATUS
  804. DiskPerfReadWrite(
  805. IN PDEVICE_OBJECT DeviceObject,
  806. IN PIRP Irp
  807. )
  808. /*++
  809. Routine Description:
  810. This is the driver entry point for read and write requests
  811. to disks to which the diskperf driver has attached.
  812. This driver collects statistics and then sets a completion
  813. routine so that it can collect additional information when
  814. the request completes. Then it calls the next driver below
  815. it.
  816. Arguments:
  817. DeviceObject
  818. Irp
  819. Return Value:
  820. NTSTATUS
  821. --*/
  822. {
  823. PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  824. PIO_STACK_LOCATION currentIrpStack = IoGetCurrentIrpStackLocation(Irp);
  825. PIO_STACK_LOCATION nextIrpStack = IoGetNextIrpStackLocation(Irp);
  826. ULONG processor = (ULONG) KeGetCurrentProcessorNumber();
  827. PDISK_PERFORMANCE partitionCounters = NULL;
  828. LONG queueLen;
  829. PLARGE_INTEGER timeStamp;
  830. if (deviceExtension->DiskCounters != NULL) {
  831. partitionCounters = (PDISK_PERFORMANCE)
  832. ((PCHAR) deviceExtension->DiskCounters
  833. + (processor*PROCESSOR_COUNTERS_SIZE));
  834. }
  835. //
  836. // Device is not initialized properly. Blindly pass the irp along
  837. //
  838. if (deviceExtension->CountersEnabled <= 0 ||
  839. deviceExtension->PhysicalDeviceNameBuffer[0] == 0 ||
  840. partitionCounters == NULL) {
  841. return DiskPerfSendToNextDriver(DeviceObject, Irp);
  842. }
  843. //
  844. // Increment queue depth counter.
  845. //
  846. queueLen = InterlockedIncrement(&deviceExtension->QueueDepth);
  847. //
  848. // Copy current stack to next stack.
  849. //
  850. *nextIrpStack = *currentIrpStack;
  851. //
  852. // Time stamp current request start.
  853. //
  854. timeStamp = (PLARGE_INTEGER) &currentIrpStack->Parameters.Read;
  855. DiskPerfGetClock(*timeStamp, NULL);
  856. DebugPrint((10, "DiskPerfReadWrite: TS=%I64u\n", *timeStamp));
  857. if (queueLen == 1) {
  858. partitionCounters->IdleTime.QuadPart
  859. += timeStamp->QuadPart -
  860. deviceExtension->LastIdleClock.QuadPart;
  861. deviceExtension->LastIdleClock.QuadPart = timeStamp->QuadPart;
  862. }
  863. //
  864. // Set completion routine callback.
  865. //
  866. IoSetCompletionRoutine(Irp,
  867. DiskPerfIoCompletion,
  868. DeviceObject,
  869. TRUE,
  870. TRUE,
  871. TRUE);
  872. //
  873. // Return the results of the call to the disk driver.
  874. //
  875. return IoCallDriver(deviceExtension->TargetDeviceObject,
  876. Irp);
  877. } // end DiskPerfReadWrite()
  878. NTSTATUS
  879. DiskPerfIoCompletion(
  880. IN PDEVICE_OBJECT DeviceObject,
  881. IN PIRP Irp,
  882. IN PVOID Context
  883. )
  884. /*++
  885. Routine Description:
  886. This routine will get control from the system at the completion of an IRP.
  887. It will calculate the difference between the time the IRP was started
  888. and the current time, and decrement the queue depth.
  889. Arguments:
  890. DeviceObject - for the IRP.
  891. Irp - The I/O request that just completed.
  892. Context - Not used.
  893. Return Value:
  894. The IRP status.
  895. --*/
  896. {
  897. PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  898. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  899. PDISK_PERFORMANCE partitionCounters;
  900. LARGE_INTEGER timeStampComplete;
  901. PLARGE_INTEGER difference;
  902. KIRQL currentIrql;
  903. LONG queueLen;
  904. UNREFERENCED_PARAMETER(Context);
  905. //
  906. // Get the per processor partition counters
  907. // NOTE: DiskPerfReadWrite already check to see if this buffer is NON
  908. // NULL before scheduling this completion routine, so we assume that it
  909. // is always non-NULL when we get here
  910. //
  911. partitionCounters = (PDISK_PERFORMANCE)
  912. ((PCHAR) deviceExtension->DiskCounters
  913. + ((ULONG)KeGetCurrentProcessorNumber()
  914. * PROCESSOR_COUNTERS_SIZE));
  915. //
  916. // Time stamp current request complete.
  917. //
  918. if (partitionCounters == NULL) { // just in case
  919. return STATUS_SUCCESS;
  920. };
  921. difference = (PLARGE_INTEGER) &irpStack->Parameters.Read;
  922. DiskPerfGetClock(timeStampComplete, NULL);
  923. difference->QuadPart = timeStampComplete.QuadPart - difference->QuadPart;
  924. DebugPrint((10, "DiskPerfIoCompletion: TS=%I64u diff %I64u\n",
  925. timeStampComplete, difference->QuadPart));
  926. //
  927. // Decrement the queue depth counters for the volume. This is
  928. // done without the spinlock using the Interlocked functions.
  929. // This is the only
  930. // legal way to do this.
  931. //
  932. queueLen = InterlockedDecrement(&deviceExtension->QueueDepth);
  933. if (queueLen < 0) { // do not over-decrement. Only happens at start
  934. queueLen = InterlockedIncrement(&deviceExtension->QueueDepth);
  935. }
  936. if (queueLen == 0) {
  937. deviceExtension->LastIdleClock = timeStampComplete;
  938. }
  939. //
  940. // Update counters
  941. //
  942. if (irpStack->MajorFunction == IRP_MJ_READ) {
  943. //
  944. // Add bytes in this request to bytes read counters.
  945. //
  946. partitionCounters->BytesRead.QuadPart += Irp->IoStatus.Information;
  947. //
  948. // Increment read requests processed counters.
  949. //
  950. partitionCounters->ReadCount++;
  951. //
  952. // Calculate request processing time.
  953. //
  954. partitionCounters->ReadTime.QuadPart += difference->QuadPart;
  955. DebugPrint((11, "Added RT delta %I64u total %I64u qlen=%d\n",
  956. difference->QuadPart, partitionCounters->ReadTime.QuadPart,
  957. queueLen));
  958. }
  959. else {
  960. //
  961. // Add bytes in this request to bytes write counters.
  962. //
  963. partitionCounters->BytesWritten.QuadPart += Irp->IoStatus.Information;
  964. //
  965. // Increment write requests processed counters.
  966. //
  967. partitionCounters->WriteCount++;
  968. //
  969. // Calculate request processing time.
  970. //
  971. partitionCounters->WriteTime.QuadPart += difference->QuadPart;
  972. DebugPrint((11, "Added WT delta %I64u total %I64u qlen=%d\n",
  973. difference->QuadPart, partitionCounters->WriteTime.QuadPart,
  974. queueLen));
  975. }
  976. if (Irp->Flags & IRP_ASSOCIATED_IRP) {
  977. partitionCounters->SplitCount++;
  978. }
  979. if (Irp->PendingReturned) {
  980. IoMarkIrpPending(Irp);
  981. }
  982. return STATUS_SUCCESS;
  983. } // DiskPerfIoCompletion
  984. NTSTATUS
  985. DiskPerfDeviceControl(
  986. PDEVICE_OBJECT DeviceObject,
  987. PIRP Irp
  988. )
  989. /*++
  990. Routine Description:
  991. This device control dispatcher handles only the disk performance
  992. device control. All others are passed down to the disk drivers.
  993. The disk performane device control returns a current snapshot of
  994. the performance data.
  995. Arguments:
  996. DeviceObject - Context for the activity.
  997. Irp - The device control argument block.
  998. Return Value:
  999. Status is returned.
  1000. --*/
  1001. {
  1002. PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  1003. PIO_STACK_LOCATION currentIrpStack = IoGetCurrentIrpStackLocation(Irp);
  1004. DebugPrint((2, "DiskPerfDeviceControl: DeviceObject %X Irp %X\n",
  1005. DeviceObject, Irp));
  1006. if (currentIrpStack->Parameters.DeviceIoControl.IoControlCode ==
  1007. IOCTL_DISK_PERFORMANCE) {
  1008. NTSTATUS status;
  1009. KIRQL currentIrql;
  1010. //
  1011. // Verify user buffer is large enough for the performance data.
  1012. //
  1013. if (currentIrpStack->Parameters.DeviceIoControl.OutputBufferLength <
  1014. sizeof(DISK_PERFORMANCE)) {
  1015. //
  1016. // Indicate unsuccessful status and no data transferred.
  1017. //
  1018. status = STATUS_BUFFER_TOO_SMALL;
  1019. Irp->IoStatus.Information = 0;
  1020. }
  1021. else {
  1022. ULONG i;
  1023. PDISK_PERFORMANCE totalCounters;
  1024. PDISK_PERFORMANCE diskCounters = deviceExtension->DiskCounters;
  1025. LARGE_INTEGER frequency, perfctr;
  1026. if (diskCounters == NULL) {
  1027. Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
  1028. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  1029. return STATUS_UNSUCCESSFUL;
  1030. }
  1031. if (InterlockedCompareExchange(&deviceExtension->EnabledAlways, 1, 0) == 0)
  1032. {
  1033. InterlockedIncrement(&deviceExtension->CountersEnabled);
  1034. //
  1035. // reset per processor counters only
  1036. //
  1037. if (deviceExtension->DiskCounters != NULL)
  1038. {
  1039. RtlZeroMemory(deviceExtension->DiskCounters, PROCESSOR_COUNTERS_SIZE * deviceExtension->Processors);
  1040. }
  1041. DiskPerfGetClock(deviceExtension->LastIdleClock, NULL);
  1042. deviceExtension->QueueDepth = 0;
  1043. DebugPrint((10, "DiskPerfDeviceControl: LIC=%I64u\n", deviceExtension->LastIdleClock));
  1044. DebugPrint((3, "DiskPerfDeviceControl: Counters enabled %d\n", deviceExtension->CountersEnabled));
  1045. }
  1046. totalCounters = (PDISK_PERFORMANCE) Irp->AssociatedIrp.SystemBuffer;
  1047. RtlZeroMemory(totalCounters, sizeof(DISK_PERFORMANCE));
  1048. #ifdef USE_PERF_CTR
  1049. perfctr = KeQueryPerformanceCounter(&frequency);
  1050. #endif
  1051. KeQuerySystemTime(&totalCounters->QueryTime);
  1052. for (i=0; i<deviceExtension->Processors; i++) {
  1053. DiskPerfAddCounters(totalCounters, diskCounters, frequency);
  1054. diskCounters = (PDISK_PERFORMANCE)
  1055. ((PCHAR) diskCounters + PROCESSOR_COUNTERS_SIZE);
  1056. }
  1057. totalCounters->QueueDepth = deviceExtension->QueueDepth;
  1058. if (totalCounters->QueueDepth == 0) {
  1059. LARGE_INTEGER difference;
  1060. difference.QuadPart =
  1061. #ifdef USE_PERF_CTR
  1062. perfctr.QuadPart
  1063. #else
  1064. totalCounters->QueryTime.QuadPart
  1065. #endif
  1066. - deviceExtension->LastIdleClock.QuadPart;
  1067. if (difference.QuadPart > 0) {
  1068. totalCounters->IdleTime.QuadPart +=
  1069. #ifdef USE_PERF_CTR
  1070. 10000000 * difference.QuadPart / frequency.QuadPart;
  1071. #else
  1072. difference.QuadPart;
  1073. #endif
  1074. }
  1075. }
  1076. totalCounters->StorageDeviceNumber
  1077. = deviceExtension->DiskNumber;
  1078. RtlCopyMemory(
  1079. &totalCounters->StorageManagerName[0],
  1080. &deviceExtension->StorageManagerName[0],
  1081. 8 * sizeof(WCHAR));
  1082. status = STATUS_SUCCESS;
  1083. Irp->IoStatus.Information = sizeof(DISK_PERFORMANCE);
  1084. }
  1085. //
  1086. // Complete request.
  1087. //
  1088. Irp->IoStatus.Status = status;
  1089. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  1090. return status;
  1091. }
  1092. else {
  1093. //
  1094. // Set current stack back one.
  1095. //
  1096. Irp->CurrentLocation++,
  1097. Irp->Tail.Overlay.CurrentStackLocation++;
  1098. //
  1099. // Pass unrecognized device control requests
  1100. // down to next driver layer.
  1101. //
  1102. return IoCallDriver(deviceExtension->TargetDeviceObject, Irp);
  1103. }
  1104. } // end DiskPerfDeviceControl()
  1105. NTSTATUS DiskPerfWmi(
  1106. IN PDEVICE_OBJECT DeviceObject,
  1107. IN PIRP Irp
  1108. )
  1109. /*++
  1110. Routine Description:
  1111. This routine handles any WMI requests for information. Since the disk
  1112. information is read-only, is always collected and does not have any
  1113. events only QueryAllData, QuerySingleInstance and GetRegInfo requests
  1114. are supported.
  1115. Arguments:
  1116. DeviceObject - Context for the activity.
  1117. Irp - The device control argument block.
  1118. Return Value:
  1119. Status is returned.
  1120. --*/
  1121. {
  1122. PIO_STACK_LOCATION irpSp;
  1123. NTSTATUS status;
  1124. PWMILIB_CONTEXT wmilibContext;
  1125. SYSCTL_IRP_DISPOSITION disposition;
  1126. PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  1127. PAGED_CODE();
  1128. DebugPrint((2, "DiskPerfWmi: DeviceObject %X Irp %X\n",
  1129. DeviceObject, Irp));
  1130. wmilibContext = &deviceExtension->WmilibContext;
  1131. if (wmilibContext->GuidCount == 0) // wmilibContext is not valid
  1132. {
  1133. DebugPrint((3, "DiskPerfWmi: WmilibContext invalid"));
  1134. return DiskPerfSendToNextDriver(DeviceObject, Irp);
  1135. }
  1136. irpSp = IoGetCurrentIrpStackLocation(Irp);
  1137. DebugPrint((3, "DiskPerfWmi: Calling WmiSystemControl\n"));
  1138. status = WmiSystemControl(wmilibContext,
  1139. DeviceObject,
  1140. Irp,
  1141. &disposition);
  1142. switch (disposition)
  1143. {
  1144. case IrpProcessed:
  1145. {
  1146. break;
  1147. }
  1148. case IrpNotCompleted:
  1149. {
  1150. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  1151. break;
  1152. }
  1153. // case IrpForward:
  1154. // case IrpNotWmi:
  1155. default:
  1156. {
  1157. status = DiskPerfSendToNextDriver(DeviceObject, Irp);
  1158. break;
  1159. }
  1160. }
  1161. return(status);
  1162. }
  1163. NTSTATUS
  1164. DiskPerfShutdownFlush(
  1165. IN PDEVICE_OBJECT DeviceObject,
  1166. IN PIRP Irp
  1167. )
  1168. /*++
  1169. Routine Description:
  1170. This routine is called for a shutdown and flush IRPs. These are sent by the
  1171. system before it actually shuts down or when the file system does a flush.
  1172. Arguments:
  1173. DriverObject - Pointer to device object to being shutdown by system.
  1174. Irp - IRP involved.
  1175. Return Value:
  1176. NT Status
  1177. --*/
  1178. {
  1179. PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  1180. //
  1181. // Set current stack back one.
  1182. //
  1183. DebugPrint((2, "DiskPerfShutdownFlush: DeviceObject %X Irp %X\n",
  1184. DeviceObject, Irp));
  1185. Irp->CurrentLocation++,
  1186. Irp->Tail.Overlay.CurrentStackLocation++;
  1187. return IoCallDriver(deviceExtension->TargetDeviceObject, Irp);
  1188. } // end DiskPerfShutdownFlush()
  1189. VOID
  1190. DiskPerfUnload(
  1191. IN PDRIVER_OBJECT DriverObject
  1192. )
  1193. /*++
  1194. Routine Description:
  1195. Free all the allocated resources, etc.
  1196. Arguments:
  1197. DriverObject - pointer to a driver object.
  1198. Return Value:
  1199. VOID.
  1200. --*/
  1201. {
  1202. PAGED_CODE();
  1203. return;
  1204. }
  1205. NTSTATUS
  1206. DiskPerfRegisterDevice(
  1207. IN PDEVICE_OBJECT DeviceObject
  1208. )
  1209. /*++
  1210. Routine Description:
  1211. Routine to initialize a proper name for the device object, and
  1212. register it with WMI
  1213. Arguments:
  1214. DeviceObject - pointer to a device object to be initialized.
  1215. Return Value:
  1216. Status of the initialization. NOTE: If the registration fails,
  1217. the device name in the DeviceExtension will be left as empty.
  1218. --*/
  1219. {
  1220. NTSTATUS status;
  1221. IO_STATUS_BLOCK ioStatus;
  1222. KEVENT event;
  1223. PDEVICE_EXTENSION deviceExtension;
  1224. PIRP irp;
  1225. STORAGE_DEVICE_NUMBER number;
  1226. ULONG registrationFlag = 0;
  1227. PAGED_CODE();
  1228. DebugPrint((2, "DiskPerfRegisterDevice: DeviceObject %X\n",
  1229. DeviceObject));
  1230. deviceExtension = DeviceObject->DeviceExtension;
  1231. KeInitializeEvent(&event, NotificationEvent, FALSE);
  1232. //
  1233. // Request for the device number
  1234. //
  1235. irp = IoBuildDeviceIoControlRequest(
  1236. IOCTL_STORAGE_GET_DEVICE_NUMBER,
  1237. deviceExtension->TargetDeviceObject,
  1238. NULL,
  1239. 0,
  1240. &number,
  1241. sizeof(number),
  1242. FALSE,
  1243. &event,
  1244. &ioStatus);
  1245. if (!irp) {
  1246. DiskPerfLogError(
  1247. DeviceObject,
  1248. 256,
  1249. STATUS_SUCCESS,
  1250. IO_ERR_INSUFFICIENT_RESOURCES);
  1251. DebugPrint((3, "DiskPerfRegisterDevice: Fail to build irp\n"));
  1252. return STATUS_INSUFFICIENT_RESOURCES;
  1253. }
  1254. status = IoCallDriver(deviceExtension->TargetDeviceObject, irp);
  1255. if (status == STATUS_PENDING) {
  1256. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  1257. status = ioStatus.Status;
  1258. }
  1259. if (NT_SUCCESS(status)) {
  1260. //
  1261. // Remember the disk number for use as parameter in DiskIoNotifyRoutine
  1262. //
  1263. deviceExtension->DiskNumber = number.DeviceNumber;
  1264. //
  1265. // Create device name for each partition
  1266. //
  1267. swprintf(
  1268. deviceExtension->PhysicalDeviceNameBuffer,
  1269. L"\\Device\\Harddisk%d\\Partition%d",
  1270. number.DeviceNumber, number.PartitionNumber);
  1271. RtlInitUnicodeString(&deviceExtension->PhysicalDeviceName, &deviceExtension->PhysicalDeviceNameBuffer[0]);
  1272. //
  1273. // Set default name for physical disk
  1274. //
  1275. RtlCopyMemory(
  1276. &(deviceExtension->StorageManagerName[0]),
  1277. L"PhysDisk",
  1278. 8 * sizeof(WCHAR));
  1279. DebugPrint((3, "DiskPerfRegisterDevice: Device name %ws\n",
  1280. deviceExtension->PhysicalDeviceNameBuffer));
  1281. }
  1282. else {
  1283. // request for partition's information failed, try volume
  1284. ULONG outputSize = sizeof(MOUNTDEV_NAME);
  1285. PMOUNTDEV_NAME output;
  1286. VOLUME_NUMBER volumeNumber;
  1287. ULONG nameSize;
  1288. output = ExAllocatePool(PagedPool, outputSize);
  1289. if (!output) {
  1290. DiskPerfLogError(
  1291. DeviceObject,
  1292. 257,
  1293. STATUS_SUCCESS,
  1294. IO_ERR_INSUFFICIENT_RESOURCES);
  1295. return STATUS_INSUFFICIENT_RESOURCES;
  1296. }
  1297. KeInitializeEvent(&event, NotificationEvent, FALSE);
  1298. irp = IoBuildDeviceIoControlRequest(
  1299. IOCTL_MOUNTDEV_QUERY_DEVICE_NAME,
  1300. deviceExtension->TargetDeviceObject, NULL, 0,
  1301. output, outputSize, FALSE, &event, &ioStatus);
  1302. if (!irp) {
  1303. ExFreePool(output);
  1304. DiskPerfLogError(
  1305. DeviceObject,
  1306. 258,
  1307. STATUS_SUCCESS,
  1308. IO_ERR_INSUFFICIENT_RESOURCES);
  1309. return STATUS_INSUFFICIENT_RESOURCES;
  1310. }
  1311. status = IoCallDriver(deviceExtension->TargetDeviceObject, irp);
  1312. if (status == STATUS_PENDING) {
  1313. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  1314. status = ioStatus.Status;
  1315. }
  1316. if (status == STATUS_BUFFER_OVERFLOW) {
  1317. outputSize = sizeof(MOUNTDEV_NAME) + output->NameLength;
  1318. ExFreePool(output);
  1319. output = ExAllocatePool(PagedPool, outputSize);
  1320. if (!output) {
  1321. DiskPerfLogError(
  1322. DeviceObject,
  1323. 258,
  1324. STATUS_SUCCESS,
  1325. IO_ERR_INSUFFICIENT_RESOURCES);
  1326. return STATUS_INSUFFICIENT_RESOURCES;
  1327. }
  1328. KeInitializeEvent(&event, NotificationEvent, FALSE);
  1329. irp = IoBuildDeviceIoControlRequest(
  1330. IOCTL_MOUNTDEV_QUERY_DEVICE_NAME,
  1331. deviceExtension->TargetDeviceObject, NULL, 0,
  1332. output, outputSize, FALSE, &event, &ioStatus);
  1333. if (!irp) {
  1334. ExFreePool(output);
  1335. DiskPerfLogError(
  1336. DeviceObject, 259,
  1337. STATUS_SUCCESS,
  1338. IO_ERR_INSUFFICIENT_RESOURCES);
  1339. return STATUS_INSUFFICIENT_RESOURCES;
  1340. }
  1341. status = IoCallDriver(deviceExtension->TargetDeviceObject, irp);
  1342. if (status == STATUS_PENDING) {
  1343. KeWaitForSingleObject(
  1344. &event,
  1345. Executive,
  1346. KernelMode,
  1347. FALSE,
  1348. NULL
  1349. );
  1350. status = ioStatus.Status;
  1351. }
  1352. }
  1353. if (!NT_SUCCESS(status)) {
  1354. ExFreePool(output);
  1355. DiskPerfLogError(
  1356. DeviceObject,
  1357. 260,
  1358. STATUS_SUCCESS,
  1359. IO_ERR_CONFIGURATION_ERROR);
  1360. return status;
  1361. }
  1362. //
  1363. // Since we get the volume name instead of the disk number,
  1364. // set it to a dummy value
  1365. // Todo: Instead of passing the disk number back to the user app.
  1366. // for tracing, pass the STORAGE_DEVICE_NUMBER structure instead.
  1367. deviceExtension->DiskNumber = -1;
  1368. nameSize = min(output->NameLength, sizeof(deviceExtension->PhysicalDeviceNameBuffer) - sizeof(WCHAR));
  1369. wcsncpy(deviceExtension->PhysicalDeviceNameBuffer, output->Name, nameSize / sizeof(WCHAR));
  1370. RtlInitUnicodeString(&deviceExtension->PhysicalDeviceName, &deviceExtension->PhysicalDeviceNameBuffer[0]);
  1371. ExFreePool(output);
  1372. //
  1373. // Now, get the VOLUME_NUMBER information
  1374. //
  1375. outputSize = sizeof(VOLUME_NUMBER);
  1376. RtlZeroMemory(&volumeNumber, sizeof(VOLUME_NUMBER));
  1377. KeInitializeEvent(&event, NotificationEvent, FALSE);
  1378. irp = IoBuildDeviceIoControlRequest(
  1379. IOCTL_VOLUME_QUERY_VOLUME_NUMBER,
  1380. deviceExtension->TargetDeviceObject, NULL, 0,
  1381. &volumeNumber,
  1382. sizeof(VOLUME_NUMBER),
  1383. FALSE, &event, &ioStatus);
  1384. if (!irp) {
  1385. DiskPerfLogError(
  1386. DeviceObject,
  1387. 265,
  1388. STATUS_SUCCESS,
  1389. IO_ERR_INSUFFICIENT_RESOURCES);
  1390. return STATUS_INSUFFICIENT_RESOURCES;
  1391. }
  1392. status = IoCallDriver(deviceExtension->TargetDeviceObject, irp);
  1393. if (status == STATUS_PENDING) {
  1394. KeWaitForSingleObject(&event, Executive,
  1395. KernelMode, FALSE, NULL);
  1396. status = ioStatus.Status;
  1397. }
  1398. if (!NT_SUCCESS(status) ||
  1399. volumeNumber.VolumeManagerName[0] == (WCHAR) UNICODE_NULL) {
  1400. RtlCopyMemory(
  1401. &deviceExtension->StorageManagerName[0],
  1402. L"LogiDisk",
  1403. 8 * sizeof(WCHAR));
  1404. if (NT_SUCCESS(status))
  1405. deviceExtension->DiskNumber = volumeNumber.VolumeNumber;
  1406. }
  1407. else {
  1408. RtlCopyMemory(
  1409. &deviceExtension->StorageManagerName[0],
  1410. &volumeNumber.VolumeManagerName[0],
  1411. 8 * sizeof(WCHAR));
  1412. deviceExtension->DiskNumber = volumeNumber.VolumeNumber;
  1413. }
  1414. DebugPrint((3, "DiskPerfRegisterDevice: Device name %ws\n",
  1415. deviceExtension->PhysicalDeviceNameBuffer));
  1416. }
  1417. status = IoWMIRegistrationControl(DeviceObject,
  1418. WMIREG_ACTION_REGISTER | registrationFlag );
  1419. if (! NT_SUCCESS(status)) {
  1420. DiskPerfLogError(
  1421. DeviceObject,
  1422. 261,
  1423. STATUS_SUCCESS,
  1424. IO_ERR_INTERNAL_ERROR);
  1425. }
  1426. return status;
  1427. }
  1428. VOID
  1429. DiskPerfLogError(
  1430. IN PDEVICE_OBJECT DeviceObject,
  1431. IN ULONG UniqueId,
  1432. IN NTSTATUS ErrorCode,
  1433. IN NTSTATUS Status
  1434. )
  1435. /*++
  1436. Routine Description:
  1437. Routine to log an error with the Error Logger
  1438. Arguments:
  1439. DeviceObject - the device object responsible for the error
  1440. UniqueId - an id for the error
  1441. Status - the status of the error
  1442. Return Value:
  1443. None
  1444. --*/
  1445. {
  1446. PIO_ERROR_LOG_PACKET errorLogEntry;
  1447. errorLogEntry = (PIO_ERROR_LOG_PACKET)
  1448. IoAllocateErrorLogEntry(
  1449. DeviceObject,
  1450. (UCHAR)(sizeof(IO_ERROR_LOG_PACKET) + sizeof(DEVICE_OBJECT))
  1451. );
  1452. if (errorLogEntry != NULL) {
  1453. errorLogEntry->ErrorCode = ErrorCode;
  1454. errorLogEntry->UniqueErrorValue = UniqueId;
  1455. errorLogEntry->FinalStatus = Status;
  1456. //
  1457. // The following is necessary because DumpData is of type ULONG
  1458. // and DeviceObject can be more than that
  1459. //
  1460. RtlCopyMemory(
  1461. &errorLogEntry->DumpData[0],
  1462. &DeviceObject,
  1463. sizeof(DEVICE_OBJECT));
  1464. errorLogEntry->DumpDataSize = sizeof(DEVICE_OBJECT);
  1465. IoWriteErrorLogEntry(errorLogEntry);
  1466. }
  1467. }
  1468. NTSTATUS
  1469. DiskperfQueryWmiRegInfo(
  1470. IN PDEVICE_OBJECT DeviceObject,
  1471. OUT ULONG *RegFlags,
  1472. OUT PUNICODE_STRING InstanceName,
  1473. OUT PUNICODE_STRING *RegistryPath,
  1474. OUT PUNICODE_STRING MofResourceName,
  1475. OUT PDEVICE_OBJECT *Pdo
  1476. )
  1477. /*++
  1478. Routine Description:
  1479. This routine is a callback into the driver to retrieve information about
  1480. the guids being registered.
  1481. Implementations of this routine may be in paged memory
  1482. Arguments:
  1483. DeviceObject is the device whose registration information is needed
  1484. *RegFlags returns with a set of flags that describe all of the guids being
  1485. registered for this device. If the device wants enable and disable
  1486. collection callbacks before receiving queries for the registered
  1487. guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
  1488. returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
  1489. the instance name is determined from the PDO associated with the
  1490. device object. Note that the PDO must have an associated devnode. If
  1491. WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
  1492. name for the device. These flags are ORed into the flags specified
  1493. by the GUIDREGINFO for each guid.
  1494. InstanceName returns with the instance name for the guids if
  1495. WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
  1496. caller will call ExFreePool with the buffer returned.
  1497. *RegistryPath returns with the registry path of the driver. This is
  1498. required
  1499. MofResourceName returns with the name of the MOF resource attached to
  1500. the binary file. If the driver does not have a mof resource attached
  1501. then this can be returned unmodified. If a value is returned then
  1502. it is NOT freed.
  1503. *Pdo returns with the device object for the PDO associated with this
  1504. device if the WMIREG_FLAG_INSTANCE_PDO flag is retured in
  1505. *RegFlags.
  1506. Return Value:
  1507. status
  1508. --*/
  1509. {
  1510. USHORT size;
  1511. NTSTATUS status;
  1512. PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  1513. PAGED_CODE();
  1514. size = deviceExtension->PhysicalDeviceName.Length + sizeof(UNICODE_NULL);
  1515. InstanceName->Buffer = ExAllocatePool(PagedPool,
  1516. size);
  1517. if (InstanceName->Buffer != NULL)
  1518. {
  1519. *RegistryPath = &DiskPerfRegistryPath;
  1520. *RegFlags = WMIREG_FLAG_INSTANCE_PDO | WMIREG_FLAG_EXPENSIVE;
  1521. *Pdo = deviceExtension->PhysicalDeviceObject;
  1522. status = STATUS_SUCCESS;
  1523. } else {
  1524. status = STATUS_INSUFFICIENT_RESOURCES;
  1525. }
  1526. return(status);
  1527. }
  1528. NTSTATUS
  1529. DiskperfQueryWmiDataBlock(
  1530. IN PDEVICE_OBJECT DeviceObject,
  1531. IN PIRP Irp,
  1532. IN ULONG GuidIndex,
  1533. IN ULONG InstanceIndex,
  1534. IN ULONG InstanceCount,
  1535. IN OUT PULONG InstanceLengthArray,
  1536. IN ULONG BufferAvail,
  1537. OUT PUCHAR Buffer
  1538. )
  1539. /*++
  1540. Routine Description:
  1541. This routine is a callback into the driver to query for the contents of
  1542. all instances of a data block. When the driver has finished filling the
  1543. data block it must call WmiCompleteRequest to complete the irp. The
  1544. driver can return STATUS_PENDING if the irp cannot be completed
  1545. immediately.
  1546. Arguments:
  1547. DeviceObject is the device whose data block is being queried
  1548. Irp is the Irp that makes this request
  1549. GuidIndex is the index into the list of guids provided when the
  1550. device registered
  1551. InstanceCount is the number of instnaces expected to be returned for
  1552. the data block.
  1553. InstanceLengthArray is a pointer to an array of ULONG that returns the
  1554. lengths of each instance of the data block. If this is NULL then
  1555. there was not enough space in the output buffer to fufill the request
  1556. so the irp should be completed with the buffer needed.
  1557. BufferAvail on entry has the maximum size available to write the data
  1558. blocks.
  1559. Buffer on return is filled with the returned data blocks. Note that each
  1560. instance of the data block must be aligned on a 8 byte boundry.
  1561. Return Value:
  1562. status
  1563. --*/
  1564. {
  1565. NTSTATUS status;
  1566. PDEVICE_EXTENSION deviceExtension;
  1567. ULONG sizeNeeded;
  1568. KIRQL currentIrql;
  1569. PDISK_PERFORMANCE totalCounters;
  1570. PDISK_PERFORMANCE diskCounters;
  1571. PWMI_DISK_PERFORMANCE diskPerformance;
  1572. ULONG deviceNameSize;
  1573. PWCHAR diskNamePtr;
  1574. PAGED_CODE();
  1575. deviceExtension = DeviceObject->DeviceExtension;
  1576. if (GuidIndex == 0)
  1577. {
  1578. deviceNameSize = deviceExtension->PhysicalDeviceName.Length +
  1579. sizeof(USHORT);
  1580. sizeNeeded = ((sizeof(WMI_DISK_PERFORMANCE) + 1) & ~1) +
  1581. deviceNameSize;
  1582. diskCounters = deviceExtension->DiskCounters;
  1583. if (diskCounters == NULL)
  1584. {
  1585. status = STATUS_UNSUCCESSFUL;
  1586. }
  1587. else if (BufferAvail >= sizeNeeded)
  1588. {
  1589. //
  1590. // Update idle time if disk has been idle
  1591. //
  1592. ULONG i;
  1593. LARGE_INTEGER perfctr, frequency;
  1594. RtlZeroMemory(Buffer, sizeof(WMI_DISK_PERFORMANCE));
  1595. diskPerformance = (PWMI_DISK_PERFORMANCE)Buffer;
  1596. totalCounters = (PDISK_PERFORMANCE)diskPerformance;
  1597. KeQuerySystemTime(&totalCounters->QueryTime);
  1598. #ifdef USE_PERF_CTR
  1599. perfctr = KeQueryPerformanceCounter(&frequency);
  1600. #endif
  1601. for (i=0; i<deviceExtension->Processors; i++) {
  1602. DiskPerfAddCounters( totalCounters, diskCounters, frequency);
  1603. DebugPrint((11,
  1604. "DiskPerfQueryWmiDataBlock: R%d %I64u W%d%I64u ", i,
  1605. diskCounters->ReadTime, diskCounters->WriteTime));
  1606. diskCounters = (PDISK_PERFORMANCE)
  1607. ((PCHAR)diskCounters + PROCESSOR_COUNTERS_SIZE);
  1608. }
  1609. DebugPrint((11, "\n"));
  1610. totalCounters->QueueDepth = deviceExtension->QueueDepth;
  1611. DebugPrint((9,
  1612. "QueryWmiDataBlock: Dev %X RT %I64u WT %I64u Rds %d Wts %d freq %I64u\n",
  1613. totalCounters,
  1614. totalCounters->ReadTime, totalCounters->WriteTime,
  1615. totalCounters->ReadCount, totalCounters->WriteCount,
  1616. frequency));
  1617. if (totalCounters->QueueDepth == 0) {
  1618. LARGE_INTEGER difference;
  1619. difference.QuadPart
  1620. #ifdef USE_PERF_CTR
  1621. = perfctr.QuadPart -
  1622. #else
  1623. = totalCounters->QueryTime.QuadPart -
  1624. #endif
  1625. deviceExtension->LastIdleClock.QuadPart;
  1626. if (frequency.QuadPart > 0) {
  1627. totalCounters->IdleTime.QuadPart +=
  1628. #ifdef USE_PERF_CTR
  1629. 10000000 * difference.QuadPart / frequency.QuadPart;
  1630. #else
  1631. difference.QuadPart;
  1632. #endif
  1633. }
  1634. }
  1635. totalCounters->StorageDeviceNumber
  1636. = deviceExtension->DiskNumber;
  1637. RtlCopyMemory(
  1638. &totalCounters->StorageManagerName[0],
  1639. &deviceExtension->StorageManagerName[0],
  1640. 8 * sizeof(WCHAR));
  1641. diskNamePtr = (PWCHAR)(Buffer +
  1642. ((sizeof(DISK_PERFORMANCE) + 1) & ~1));
  1643. *diskNamePtr++ = deviceExtension->PhysicalDeviceName.Length;
  1644. RtlCopyMemory(diskNamePtr,
  1645. deviceExtension->PhysicalDeviceName.Buffer,
  1646. deviceExtension->PhysicalDeviceName.Length);
  1647. *InstanceLengthArray = sizeNeeded;
  1648. status = STATUS_SUCCESS;
  1649. } else {
  1650. status = STATUS_BUFFER_TOO_SMALL;
  1651. }
  1652. } else {
  1653. status = STATUS_WMI_GUID_NOT_FOUND;
  1654. sizeNeeded = 0;
  1655. }
  1656. status = WmiCompleteRequest(
  1657. DeviceObject,
  1658. Irp,
  1659. status,
  1660. sizeNeeded,
  1661. IO_NO_INCREMENT);
  1662. return(status);
  1663. }
  1664. NTSTATUS
  1665. DiskperfWmiFunctionControl(
  1666. IN PDEVICE_OBJECT DeviceObject,
  1667. IN PIRP Irp,
  1668. IN ULONG GuidIndex,
  1669. IN WMIENABLEDISABLECONTROL Function,
  1670. IN BOOLEAN Enable
  1671. )
  1672. /*++
  1673. Routine Description:
  1674. This routine is a callback into the driver to query for enabling or
  1675. disabling events and data collection. When the driver has finished it
  1676. must call WmiCompleteRequest to complete the irp. The driver can return
  1677. STATUS_PENDING if the irp cannot be completed immediately.
  1678. Arguments:
  1679. DeviceObject is the device whose events or data collection are being
  1680. enabled or disabled
  1681. Irp is the Irp that makes this request
  1682. GuidIndex is the index into the list of guids provided when the
  1683. device registered
  1684. Function differentiates between event and data collection operations
  1685. Enable indicates whether to enable or disable
  1686. Return Value:
  1687. status
  1688. --*/
  1689. {
  1690. NTSTATUS status;
  1691. PDEVICE_EXTENSION deviceExtension;
  1692. ULONG i;
  1693. deviceExtension = DeviceObject->DeviceExtension;
  1694. if (GuidIndex == 0)
  1695. {
  1696. if (Function == WmiDataBlockControl) {
  1697. if (Enable) {
  1698. if (InterlockedIncrement(&deviceExtension->CountersEnabled) == 1) {
  1699. //
  1700. // Reset per processor counters to 0
  1701. //
  1702. if (deviceExtension->DiskCounters != NULL) {
  1703. RtlZeroMemory(
  1704. deviceExtension->DiskCounters,
  1705. PROCESSOR_COUNTERS_SIZE * deviceExtension->Processors);
  1706. }
  1707. DiskPerfGetClock(deviceExtension->LastIdleClock, NULL);
  1708. DebugPrint((10,
  1709. "DiskPerfWmiFunctionControl: LIC=%I64u\n",
  1710. deviceExtension->LastIdleClock));
  1711. deviceExtension->QueueDepth = 0;
  1712. DebugPrint((3, "DiskPerfWmi: Counters enabled %d\n",
  1713. deviceExtension->CountersEnabled));
  1714. }
  1715. } else {
  1716. if (InterlockedDecrement(&deviceExtension->CountersEnabled)
  1717. <= 0) {
  1718. deviceExtension->CountersEnabled = 0;
  1719. deviceExtension->QueueDepth = 0;
  1720. DebugPrint((3, "DiskPerfWmi: Counters disabled %d\n",
  1721. deviceExtension->CountersEnabled));
  1722. }
  1723. }
  1724. }
  1725. status = STATUS_SUCCESS;
  1726. } else {
  1727. status = STATUS_WMI_GUID_NOT_FOUND;
  1728. }
  1729. status = WmiCompleteRequest(
  1730. DeviceObject,
  1731. Irp,
  1732. status,
  1733. 0,
  1734. IO_NO_INCREMENT);
  1735. return(status);
  1736. }
  1737. VOID
  1738. DiskPerfAddCounters(
  1739. IN OUT PDISK_PERFORMANCE TotalCounters,
  1740. IN PDISK_PERFORMANCE NewCounters,
  1741. IN LARGE_INTEGER Frequency
  1742. )
  1743. {
  1744. TotalCounters->BytesRead.QuadPart += NewCounters->BytesRead.QuadPart;
  1745. TotalCounters->BytesWritten.QuadPart+= NewCounters->BytesWritten.QuadPart;
  1746. TotalCounters->ReadCount += NewCounters->ReadCount;
  1747. TotalCounters->WriteCount += NewCounters->WriteCount;
  1748. TotalCounters->SplitCount += NewCounters->SplitCount;
  1749. #ifdef USE_PERF_CTR
  1750. if (Frequency.QuadPart > 0) {
  1751. TotalCounters->ReadTime.QuadPart +=
  1752. NewCounters->ReadTime.QuadPart * 10000000 / Frequency.QuadPart;
  1753. TotalCounters->WriteTime.QuadPart +=
  1754. NewCounters->WriteTime.QuadPart * 10000000 / Frequency.QuadPart;
  1755. TotalCounters->IdleTime.QuadPart +=
  1756. NewCounters->IdleTime.QuadPart * 10000000 / Frequency.QuadPart;
  1757. }
  1758. else
  1759. #endif
  1760. {
  1761. TotalCounters->ReadTime.QuadPart += NewCounters->ReadTime.QuadPart;
  1762. TotalCounters->WriteTime.QuadPart += NewCounters->WriteTime.QuadPart;
  1763. TotalCounters->IdleTime.QuadPart += NewCounters->IdleTime.QuadPart;
  1764. }
  1765. }
  1766. #if DBG
  1767. VOID
  1768. DiskPerfDebugPrint(
  1769. ULONG DebugPrintLevel,
  1770. PCCHAR DebugMessage,
  1771. ...
  1772. )
  1773. /*++
  1774. Routine Description:
  1775. Debug print for all DiskPerf
  1776. Arguments:
  1777. Debug print level between 0 and 3, with 3 being the most verbose.
  1778. Return Value:
  1779. None
  1780. --*/
  1781. {
  1782. va_list ap;
  1783. va_start(ap, DebugMessage);
  1784. if ((DebugPrintLevel <= (DiskPerfDebug & 0x0000ffff)) ||
  1785. ((1 << (DebugPrintLevel + 15)) & DiskPerfDebug)) {
  1786. DbgPrint(DebugMessage, ap);
  1787. }
  1788. va_end(ap);
  1789. }
  1790. #endif