drive_metrics_provider_mac.mm 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/metrics/drive_metrics_provider.h"
  5. #include <CoreFoundation/CoreFoundation.h>
  6. #include <DiskArbitration/DiskArbitration.h>
  7. #import <Foundation/Foundation.h>
  8. #include <IOKit/IOKitLib.h>
  9. #include <IOKit/storage/IOStorageDeviceCharacteristics.h>
  10. #include <stdlib.h>
  11. #include <sys/stat.h>
  12. #include "base/files/file_path.h"
  13. #include "base/mac/foundation_util.h"
  14. #include "base/mac/mac_util.h"
  15. #include "base/mac/scoped_cftyperef.h"
  16. #include "base/mac/scoped_ioobject.h"
  17. namespace metrics {
  18. // static
  19. bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path,
  20. bool* has_seek_penalty) {
  21. struct stat path_stat;
  22. if (stat(path.value().c_str(), &path_stat) < 0)
  23. return false;
  24. const char* dev_name = devname(path_stat.st_dev, S_IFBLK);
  25. if (!dev_name)
  26. return false;
  27. std::string bsd_name("/dev/");
  28. bsd_name.append(dev_name);
  29. base::ScopedCFTypeRef<DASessionRef> session(
  30. DASessionCreate(kCFAllocatorDefault));
  31. if (!session)
  32. return false;
  33. base::ScopedCFTypeRef<DADiskRef> disk(
  34. DADiskCreateFromBSDName(kCFAllocatorDefault, session, bsd_name.c_str()));
  35. if (!disk)
  36. return false;
  37. base::mac::ScopedIOObject<io_object_t> io_media(DADiskCopyIOMedia(disk));
  38. base::ScopedCFTypeRef<CFDictionaryRef> characteristics(
  39. static_cast<CFDictionaryRef>(IORegistryEntrySearchCFProperty(
  40. io_media, kIOServicePlane, CFSTR(kIOPropertyDeviceCharacteristicsKey),
  41. kCFAllocatorDefault,
  42. kIORegistryIterateRecursively | kIORegistryIterateParents)));
  43. if (!characteristics)
  44. return false;
  45. CFStringRef type_ref = base::mac::GetValueFromDictionary<CFStringRef>(
  46. characteristics, CFSTR(kIOPropertyMediumTypeKey));
  47. if (!type_ref)
  48. return false;
  49. NSString* type = base::mac::CFToNSCast(type_ref);
  50. if ([type isEqualToString:@kIOPropertyMediumTypeRotationalKey]) {
  51. *has_seek_penalty = true;
  52. return true;
  53. }
  54. if ([type isEqualToString:@kIOPropertyMediumTypeSolidStateKey]) {
  55. *has_seek_penalty = false;
  56. return true;
  57. }
  58. // TODO(dbeam): should I look for these Rotational/Solid State keys in
  59. // |characteristics|? What if I find device characteristic but there's no
  60. // type? Assume rotational?
  61. return false;
  62. }
  63. } // namespace metrics