1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/drive_metrics_provider.h"
7 #include <CoreFoundation/CoreFoundation.h>
8 #include <DiskArbitration/DiskArbitration.h>
9 #import <Foundation/Foundation.h>
10 #include <IOKit/IOKitLib.h>
11 #include <IOKit/storage/IOStorageDeviceCharacteristics.h>
15 #include "base/apple/bridging.h"
16 #include "base/apple/foundation_util.h"
17 #include "base/apple/scoped_cftyperef.h"
18 #include "base/files/file_path.h"
19 #include "base/mac/mac_util.h"
20 #include "base/mac/scoped_ioobject.h"
25 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path,
26 bool* has_seek_penalty) {
27 struct stat path_stat;
28 if (stat(path.value().c_str(), &path_stat) < 0)
31 const char* dev_name = devname(path_stat.st_dev, S_IFBLK);
35 std::string bsd_name("/dev/");
36 bsd_name.append(dev_name);
38 base::apple::ScopedCFTypeRef<DASessionRef> session(
39 DASessionCreate(kCFAllocatorDefault));
43 base::apple::ScopedCFTypeRef<DADiskRef> disk(
44 DADiskCreateFromBSDName(kCFAllocatorDefault, session, bsd_name.c_str()));
48 base::mac::ScopedIOObject<io_object_t> io_media(DADiskCopyIOMedia(disk));
49 base::apple::ScopedCFTypeRef<CFDictionaryRef> characteristics(
50 static_cast<CFDictionaryRef>(IORegistryEntrySearchCFProperty(
51 io_media, kIOServicePlane, CFSTR(kIOPropertyDeviceCharacteristicsKey),
53 kIORegistryIterateRecursively | kIORegistryIterateParents)));
57 CFStringRef type_ref = base::apple::GetValueFromDictionary<CFStringRef>(
58 characteristics, CFSTR(kIOPropertyMediumTypeKey));
62 NSString* type = base::apple::CFToNSPtrCast(type_ref);
63 if ([type isEqualToString:@kIOPropertyMediumTypeRotationalKey]) {
64 *has_seek_penalty = true;
67 if ([type isEqualToString:@kIOPropertyMediumTypeSolidStateKey]) {
68 *has_seek_penalty = false;
72 // TODO(dbeam): should I look for these Rotational/Solid State keys in
73 // |characteristics|? What if I find device characteristic but there's no
74 // type? Assume rotational?
78 } // namespace metrics