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.

315 lines
10 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. dpsump.c
  5. Abstract:
  6. test program
  7. Author:
  8. 16-Jan-1997 AlanWar
  9. Revision History:
  10. 7-Jan-2001 insungp
  11. --*/
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <ole2.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <ntdddisk.h>
  20. #include <wmium.h>
  21. #include <mountmgr.h>
  22. GUID DiskPerfGuid = {0xBDD865D1,0xD7C1,0x11d0,0xA5,0x01,0x00,0xA0,0xC9,0x06,0x29,0x10};
  23. BYTE Buffer[4096];
  24. ULONG QueryAllDataAndDump(LPGUID Guid)
  25. {
  26. WMIHANDLE WmiHandle;
  27. ULONG Size;
  28. ULONG Status;
  29. PWNODE_ALL_DATA WAD;
  30. WCHAR InstanceName[MAX_PATH];
  31. PWCHAR InstanceNamePtr;
  32. ULONG InstanceNameOffset;
  33. PDISK_PERFORMANCE DiskPerformance;
  34. Status = WmiOpenBlock(Guid, 0, &WmiHandle);
  35. if (Status != ERROR_SUCCESS)
  36. {
  37. printf("WMIOpenBlock %d\n", Status);
  38. return(Status);
  39. }
  40. Size = sizeof(Buffer);
  41. Status = WmiQueryAllDataW(WmiHandle, &Size, Buffer);
  42. if (Status != ERROR_SUCCESS)
  43. {
  44. printf("WMIQueryAllData %d\n", Status);
  45. } else {
  46. WAD = (PWNODE_ALL_DATA)Buffer;
  47. while (1)
  48. {
  49. DiskPerformance = (PDISK_PERFORMANCE)( (PUCHAR)WAD + WAD->DataBlockOffset);
  50. InstanceNameOffset = *((PULONG)( (PUCHAR)WAD + WAD->OffsetInstanceNameOffsets));
  51. InstanceNamePtr = (PWCHAR)( (PUCHAR)WAD + InstanceNameOffset);
  52. memcpy(InstanceName, InstanceNamePtr+1, *InstanceNamePtr);
  53. InstanceName[(*InstanceNamePtr)/sizeof(WCHAR)] = 0;
  54. printf("%ws\n", InstanceName);
  55. printf(" BytesRead = %x%x\n",
  56. DiskPerformance->BytesRead.HighPart,
  57. DiskPerformance->BytesRead.LowPart);
  58. printf(" BytesWritten = %x%x\n",
  59. DiskPerformance->BytesWritten.HighPart,
  60. DiskPerformance->BytesWritten.LowPart);
  61. printf(" ReadTime = %x%x\n",
  62. DiskPerformance->ReadTime.HighPart,
  63. DiskPerformance->ReadTime.LowPart);
  64. printf(" WriteTime = %x%x\n",
  65. DiskPerformance->WriteTime.HighPart,
  66. DiskPerformance->WriteTime.LowPart);
  67. printf(" ReadCount = %x\n", DiskPerformance->ReadCount);
  68. printf(" WriteCount = %x\n", DiskPerformance->WriteCount);
  69. printf(" QueueDepth = %x\n", DiskPerformance->QueueDepth);
  70. printf("\n\n");
  71. if (WAD->WnodeHeader.Linkage == 0)
  72. {
  73. break;
  74. }
  75. WAD = (PWNODE_ALL_DATA)((PUCHAR)WAD + WAD->WnodeHeader.Linkage);
  76. }
  77. }
  78. WmiCloseBlock(WmiHandle);
  79. return(Status);
  80. }
  81. ULONG
  82. QueryUsingIoctl()
  83. {
  84. ULONG nDisk, i;
  85. SYSTEM_DEVICE_INFORMATION DeviceInfo;
  86. NTSTATUS status;
  87. UNICODE_STRING UnicodeName;
  88. OBJECT_ATTRIBUTES ObjectAttributes;
  89. IO_STATUS_BLOCK IoStatus;
  90. WCHAR devname[256];
  91. PWCHAR s;
  92. HANDLE PartitionHandle, MountMgrHandle, VolumeHandle;
  93. DWORD ReturnedBytes, MountError;
  94. DISK_PERFORMANCE DiskPerformance;
  95. status = NtQuerySystemInformation(SystemDeviceInformation, &DeviceInfo, sizeof(DeviceInfo), NULL);
  96. if (!NT_SUCCESS(status)) {
  97. printf("NtQuerySystemInformation returns %X\n", status);
  98. }
  99. nDisk = DeviceInfo.NumberOfDisks;
  100. // for each physical disk
  101. for (i = 0; i < nDisk; i++) {
  102. swprintf(devname, L"\\Device\\Harddisk%d\\Partition0", i);
  103. RtlInitUnicodeString(&UnicodeName, devname);
  104. InitializeObjectAttributes(
  105. &ObjectAttributes,
  106. &UnicodeName,
  107. OBJ_CASE_INSENSITIVE,
  108. NULL,
  109. NULL
  110. );
  111. // opening a partition handle for physical drives
  112. status = NtOpenFile(
  113. &PartitionHandle,
  114. FILE_READ_ATTRIBUTES | SYNCHRONIZE,
  115. &ObjectAttributes,
  116. &IoStatus,
  117. FILE_SHARE_READ | FILE_SHARE_WRITE,
  118. FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE
  119. );
  120. if ( !NT_SUCCESS(status) ) {
  121. printf("Error %x: Cannot open disk %ws\n", GetLastError(), devname);
  122. continue;
  123. }
  124. // sending IOCTL over to Partition Handle
  125. if (!DeviceIoControl(PartitionHandle,
  126. IOCTL_DISK_PERFORMANCE,
  127. NULL,
  128. 0,
  129. &DiskPerformance,
  130. sizeof(DISK_PERFORMANCE),
  131. &ReturnedBytes,
  132. NULL
  133. )) {
  134. printf("Error %x: Cannot get Disk Performance Information for %ws\n", GetLastError(),
  135. devname);
  136. }
  137. else {
  138. printf("Physical Drive %d\n", i);
  139. printf(" BytesRead = %x%x\n",
  140. DiskPerformance.BytesRead.HighPart,
  141. DiskPerformance.BytesRead.LowPart);
  142. printf(" BytesWritten = %x%x\n",
  143. DiskPerformance.BytesWritten.HighPart,
  144. DiskPerformance.BytesWritten.LowPart);
  145. printf(" ReadTime = %x%x\n",
  146. DiskPerformance.ReadTime.HighPart,
  147. DiskPerformance.ReadTime.LowPart);
  148. printf(" WriteTime = %x%x\n",
  149. DiskPerformance.WriteTime.HighPart,
  150. DiskPerformance.WriteTime.LowPart);
  151. printf(" ReadCount = %x\n", DiskPerformance.ReadCount);
  152. printf(" WriteCount = %x\n", DiskPerformance.WriteCount);
  153. printf(" QueueDepth = %x\n", DiskPerformance.QueueDepth);
  154. printf("\n\n");
  155. }
  156. NtClose(PartitionHandle);
  157. }
  158. MountMgrHandle = FindFirstVolumeW(devname, sizeof(devname));
  159. if (MountMgrHandle == NULL) {
  160. printf("Cannot find first volume\n");
  161. return 0;
  162. }
  163. s = (PWCHAR) &devname[wcslen(devname)-1];
  164. if (*s == L'\\') {
  165. *s = UNICODE_NULL;
  166. }
  167. VolumeHandle = CreateFileW(devname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
  168. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
  169. if (VolumeHandle != INVALID_HANDLE_VALUE) {
  170. RtlZeroMemory(&DiskPerformance, sizeof(DISK_PERFORMANCE));
  171. // sending IOCTL over to a volume handle
  172. if (!DeviceIoControl(VolumeHandle,
  173. IOCTL_DISK_PERFORMANCE,
  174. NULL,
  175. 0,
  176. &DiskPerformance,
  177. sizeof(DISK_PERFORMANCE),
  178. &ReturnedBytes,
  179. NULL
  180. )) {
  181. printf("IOCTL failed for %ws %d\n", devname, GetLastError());
  182. }
  183. else {
  184. printf("%ws\n", devname);
  185. printf(" BytesRead = %x%x\n",
  186. DiskPerformance.BytesRead.HighPart,
  187. DiskPerformance.BytesRead.LowPart);
  188. printf(" BytesWritten = %x%x\n",
  189. DiskPerformance.BytesWritten.HighPart,
  190. DiskPerformance.BytesWritten.LowPart);
  191. printf(" ReadTime = %x%x\n",
  192. DiskPerformance.ReadTime.HighPart,
  193. DiskPerformance.ReadTime.LowPart);
  194. printf(" WriteTime = %x%x\n",
  195. DiskPerformance.WriteTime.HighPart,
  196. DiskPerformance.WriteTime.LowPart);
  197. printf(" ReadCount = %x\n", DiskPerformance.ReadCount);
  198. printf(" WriteCount = %x\n", DiskPerformance.WriteCount);
  199. printf(" QueueDepth = %x\n", DiskPerformance.QueueDepth);
  200. printf("\n\n");
  201. }
  202. CloseHandle(VolumeHandle);
  203. }
  204. else {
  205. printf("Error %x: Cannot open volume %ws\n", GetLastError(), devname);
  206. }
  207. while (FindNextVolumeW(MountMgrHandle, devname, sizeof(devname))) {
  208. s = (PWCHAR) &devname[wcslen(devname)-1];
  209. if (*s == L'\\') {
  210. *s = UNICODE_NULL;
  211. }
  212. VolumeHandle = CreateFileW(devname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
  213. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
  214. if (VolumeHandle != INVALID_HANDLE_VALUE) {
  215. RtlZeroMemory(&DiskPerformance, sizeof(DISK_PERFORMANCE));
  216. if (!DeviceIoControl(VolumeHandle,
  217. IOCTL_DISK_PERFORMANCE,
  218. NULL,
  219. 0,
  220. &DiskPerformance,
  221. sizeof(DISK_PERFORMANCE),
  222. &ReturnedBytes,
  223. NULL
  224. )) {
  225. printf("IOCTL failed for %ws %d\n", devname, GetLastError());
  226. continue;
  227. }
  228. printf("%ws\n", devname);
  229. printf(" BytesRead = %x%x\n",
  230. DiskPerformance.BytesRead.HighPart,
  231. DiskPerformance.BytesRead.LowPart);
  232. printf(" BytesWritten = %x%x\n",
  233. DiskPerformance.BytesWritten.HighPart,
  234. DiskPerformance.BytesWritten.LowPart);
  235. printf(" ReadTime = %x%x\n",
  236. DiskPerformance.ReadTime.HighPart,
  237. DiskPerformance.ReadTime.LowPart);
  238. printf(" WriteTime = %x%x\n",
  239. DiskPerformance.WriteTime.HighPart,
  240. DiskPerformance.WriteTime.LowPart);
  241. printf(" ReadCount = %x\n", DiskPerformance.ReadCount);
  242. printf(" WriteCount = %x\n", DiskPerformance.WriteCount);
  243. printf(" QueueDepth = %x\n", DiskPerformance.QueueDepth);
  244. printf("\n\n");
  245. CloseHandle(VolumeHandle);
  246. }
  247. else {
  248. printf("Error %x: Cannot open volume %ws\n", GetLastError(), devname);
  249. }
  250. }
  251. FindVolumeClose(MountMgrHandle);
  252. return ERROR_SUCCESS;
  253. }
  254. int _cdecl main(int argc, char *argv[])
  255. {
  256. if ((argc > 1) && (!strcmp(argv[1], "-ioctl"))) {
  257. QueryUsingIoctl();
  258. }
  259. else {
  260. QueryAllDataAndDump(&DiskPerfGuid);
  261. }
  262. return(0);
  263. }