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.
5 #include "chrome/browser/metrics/drive_metrics_provider.h"
7 #include <CoreFoundation/CoreFoundation.h>
8 #include <DiskArbitration/DiskArbitration.h>
9 #include <Foundation/Foundation.h>
10 #include <IOKit/IOKitLib.h>
11 #include <IOKit/storage/IOStorageDeviceCharacteristics.h>
15 #include "base/files/file_path.h"
16 #include "base/mac/foundation_util.h"
17 #include "base/mac/mac_util.h"
18 #include "base/mac/scoped_cftyperef.h"
19 #include "base/mac/scoped_ioobject.h"
22 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path,
23 bool* has_seek_penalty) {
24 struct stat path_stat;
25 if (stat(path.value().c_str(), &path_stat) < 0)
28 const char* dev_name = devname(path_stat.st_dev, S_IFBLK);
32 std::string bsd_name("/dev/");
33 bsd_name.append(dev_name);
35 base::ScopedCFTypeRef<DASessionRef> session(
36 DASessionCreate(kCFAllocatorDefault));
40 base::ScopedCFTypeRef<DADiskRef> disk(DADiskCreateFromBSDName(
41 kCFAllocatorDefault, session, bsd_name.c_str()));
45 base::mac::ScopedIOObject<io_object_t> io_media(DADiskCopyIOMedia(disk));
46 base::ScopedCFTypeRef<CFDictionaryRef> characteristics(
47 static_cast<CFDictionaryRef>(IORegistryEntrySearchCFProperty(
50 CFSTR(kIOPropertyDeviceCharacteristicsKey),
52 kIORegistryIterateRecursively | kIORegistryIterateParents)));
56 CFStringRef type_ref = base::mac::GetValueFromDictionary<CFStringRef>(
57 characteristics, CFSTR(kIOPropertyMediumTypeKey));
61 NSString* type = base::mac::CFToNSCast(type_ref);
62 if ([type isEqualToString:@kIOPropertyMediumTypeRotationalKey]) {
63 *has_seek_penalty = true;
65 } else if ([type isEqualToString:@kIOPropertyMediumTypeSolidStateKey]) {
66 *has_seek_penalty = false;
70 // TODO(dbeam): should I look for these Rotational/Solid State keys in
71 // |characteristics|? What if I find device characteristic but there's no
72 // type? Assume rotational?