disk_darwin.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <CoreFoundation/CoreFoundation.h>
  2. #include <IOKit/IOKitLib.h>
  3. #include <IOKit/storage/IOBlockStorageDriver.h>
  4. #include <IOKit/storage/IOMedia.h>
  5. #include <IOKit/IOBSD.h>
  6. // The iterator of all things disk. Allocated by StartIOCounterFetch, released
  7. // by EndIOCounterFetch.
  8. static io_iterator_t diskIter;
  9. // Begins fetching IO counters.
  10. //
  11. // Returns 1 if the fetch started successfully, false otherwise.
  12. //
  13. // If the fetch was started successfully, you must call EndIOCounterFetch once
  14. // done to release resources.
  15. int StartIOCounterFetch()
  16. {
  17. if (IOServiceGetMatchingServices(kIOMasterPortDefault,
  18. IOServiceMatching(kIOMediaClass),
  19. &diskIter) != kIOReturnSuccess) {
  20. return 0;
  21. }
  22. return 1;
  23. }
  24. // Releases resources from fetching IO counters.
  25. void EndIOCounterFetch()
  26. {
  27. IOObjectRelease(diskIter);
  28. }
  29. // The current disk entry of interest. Allocated by FetchNextDisk(), released by
  30. // ReadDiskInfo().
  31. static io_registry_entry_t diskEntry;
  32. // The parent of diskEntry. Same lifetimes.
  33. static io_registry_entry_t parentEntry;
  34. // Fetches the next disk. Note that a disk entry is allocated, and will be held
  35. // until it is processed and freed by ReadDiskInfo.
  36. int FetchNextDisk()
  37. {
  38. while ((diskEntry = IOIteratorNext(diskIter)) != 0) {
  39. // We are iterating IOMedia. We need to get the parent too (IOBSD).
  40. if (IORegistryEntryGetParentEntry(diskEntry, kIOServicePlane, &parentEntry) != kIOReturnSuccess) {
  41. // something is wrong...
  42. IOObjectRelease(diskEntry);
  43. continue;
  44. }
  45. if (!IOObjectConformsTo(parentEntry, "IOBlockStorageDriver")) {
  46. // no use to us, try the next disk
  47. IOObjectRelease(diskEntry);
  48. IOObjectRelease(parentEntry);
  49. continue;
  50. }
  51. // Got a disk OK.
  52. return 1;
  53. }
  54. // No more disks.
  55. return 0;
  56. }
  57. // Reads the current disk (from iteration) info into DiskInfo struct.
  58. // Once done, all resources from the current iteration of reading are freed,
  59. // ready for FetchNextDisk() to be called again.
  60. int ReadDiskInfo(DiskInfo *info)
  61. {
  62. // Parent props. Allocated by us.
  63. CFDictionaryRef parentProps = NULL;
  64. // Disk props. Allocated by us.
  65. CFDictionaryRef diskProps = NULL;
  66. // Disk stats, fetched by us, but not allocated by us.
  67. CFDictionaryRef stats = NULL;
  68. if (IORegistryEntryCreateCFProperties(diskEntry, (CFMutableDictionaryRef *)&parentProps,
  69. kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
  70. {
  71. // can't get parent props, give up
  72. CFRelease(parentProps);
  73. IOObjectRelease(diskEntry);
  74. IOObjectRelease(parentEntry);
  75. return -1;
  76. }
  77. if (IORegistryEntryCreateCFProperties(parentEntry, (CFMutableDictionaryRef *)&diskProps,
  78. kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
  79. {
  80. // can't get disk props, give up
  81. CFRelease(parentProps);
  82. CFRelease(diskProps);
  83. IOObjectRelease(diskEntry);
  84. IOObjectRelease(parentEntry);
  85. return -1;
  86. }
  87. // Start fetching
  88. CFStringRef cfDiskName = (CFStringRef)CFDictionaryGetValue(parentProps, CFSTR(kIOBSDNameKey));
  89. CFStringGetCString(cfDiskName, info->DiskName, MAX_DISK_NAME, CFStringGetSystemEncoding());
  90. stats = (CFDictionaryRef)CFDictionaryGetValue( diskProps, CFSTR(kIOBlockStorageDriverStatisticsKey));
  91. if (stats == NULL) {
  92. // stat fetch failed...
  93. CFRelease(parentProps);
  94. CFRelease(diskProps);
  95. IOObjectRelease(parentEntry);
  96. IOObjectRelease(diskEntry);
  97. return -1;
  98. }
  99. CFNumberRef cfnum;
  100. if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsReadsKey)))) {
  101. CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->Reads);
  102. } else {
  103. info->Reads = 0;
  104. }
  105. if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsWritesKey)))) {
  106. CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->Writes);
  107. } else {
  108. info->Writes = 0;
  109. }
  110. if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsBytesReadKey)))) {
  111. CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->ReadBytes);
  112. } else {
  113. info->ReadBytes = 0;
  114. }
  115. if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsBytesWrittenKey)))) {
  116. CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->WriteBytes);
  117. } else {
  118. info->WriteBytes = 0;
  119. }
  120. if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsTotalReadTimeKey)))) {
  121. CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->ReadTime);
  122. } else {
  123. info->ReadTime = 0;
  124. }
  125. if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsTotalWriteTimeKey)))) {
  126. CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->WriteTime);
  127. } else {
  128. info->WriteTime = 0;
  129. }
  130. // note: read/write time are in ns, but we want ms.
  131. info->ReadTime = info->ReadTime / 1000 / 1000;
  132. info->WriteTime = info->WriteTime / 1000 / 1000;
  133. CFRelease(parentProps);
  134. CFRelease(diskProps);
  135. IOObjectRelease(parentEntry);
  136. IOObjectRelease(diskEntry);
  137. return 0;
  138. }