summaryrefslogtreecommitdiff
path: root/chromium/components/metrics/drive_metrics_provider_mac.mm
blob: a6a37614d0cccf595d2c7175bfd869d6c2d0af79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/metrics/drive_metrics_provider.h"

#include <CoreFoundation/CoreFoundation.h>
#include <DiskArbitration/DiskArbitration.h>
#include <Foundation/Foundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
#include <stdlib.h>
#include <sys/stat.h>

#include "base/files/file_path.h"
#include "base/mac/foundation_util.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_ioobject.h"

namespace metrics {

// static
bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path,
                                          bool* has_seek_penalty) {
  struct stat path_stat;
  if (stat(path.value().c_str(), &path_stat) < 0)
    return false;

  const char* dev_name = devname(path_stat.st_dev, S_IFBLK);
  if (!dev_name)
    return false;

  std::string bsd_name("/dev/");
  bsd_name.append(dev_name);

  base::ScopedCFTypeRef<DASessionRef> session(
      DASessionCreate(kCFAllocatorDefault));
  if (!session)
    return false;

  base::ScopedCFTypeRef<DADiskRef> disk(
      DADiskCreateFromBSDName(kCFAllocatorDefault, session, bsd_name.c_str()));
  if (!disk)
    return false;

  base::mac::ScopedIOObject<io_object_t> io_media(DADiskCopyIOMedia(disk));
  base::ScopedCFTypeRef<CFDictionaryRef> characteristics(
      static_cast<CFDictionaryRef>(IORegistryEntrySearchCFProperty(
          io_media, kIOServicePlane, CFSTR(kIOPropertyDeviceCharacteristicsKey),
          kCFAllocatorDefault,
          kIORegistryIterateRecursively | kIORegistryIterateParents)));
  if (!characteristics)
    return false;

  CFStringRef type_ref = base::mac::GetValueFromDictionary<CFStringRef>(
      characteristics, CFSTR(kIOPropertyMediumTypeKey));
  if (!type_ref)
    return false;

  NSString* type = base::mac::CFToNSCast(type_ref);
  if ([type isEqualToString:@kIOPropertyMediumTypeRotationalKey]) {
    *has_seek_penalty = true;
    return true;
  } else if ([type isEqualToString:@kIOPropertyMediumTypeSolidStateKey]) {
    *has_seek_penalty = false;
    return true;
  }

  // TODO(dbeam): should I look for these Rotational/Solid State keys in
  // |characteristics|? What if I find device characteristic but there's no
  // type? Assume rotational?
  return false;
}

}  // namespace metrics