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.

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