| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/metrics/drive_metrics_provider.h" | 5 #include "components/metrics/drive_metrics_provider.h" |
| 6 | 6 |
| 7 #include <linux/kdev_t.h> // For MAJOR()/MINOR(). | 7 #include <linux/kdev_t.h> // For MAJOR()/MINOR(). |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/strings/string_util.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 | 17 |
| 17 #if defined(OS_CHROMEOS) | 18 #if defined(OS_CHROMEOS) |
| 18 #include "base/sys_info.h" | 19 #include "base/sys_info.h" |
| 19 #endif | 20 #endif |
| 20 | 21 |
| 21 namespace metrics { | 22 namespace metrics { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // See http://www.kernel.org/doc/Documentation/devices.txt for more info. | 26 // See http://www.kernel.org/doc/Documentation/devices.txt for more info. |
| 26 const int kFirstScsiMajorNumber = 8; | 27 const int kFirstScsiMajorNumber = 8; |
| 27 const int kPartitionsPerScsiDevice = 16; | 28 const int kPartitionsPerScsiDevice = 16; |
| 28 const char kRotationalFormat[] = "/sys/block/sd%c/queue/rotational"; | 29 const char kRotationalFormat[] = "/sys/block/sd%c/queue/rotational"; |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 // static | 33 // static |
| 33 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path, | 34 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path, |
| 34 bool* has_seek_penalty) { | 35 bool* has_seek_penalty) { |
| 35 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
| 36 std::string board = base::SysInfo::GetLsbReleaseBoard(); | 37 std::string board = base::SysInfo::GetStrippedReleaseBoard(); |
| 37 if (board != "unknown" && board != "parrot") { | 38 // There are "parrot", "parrot_ivb" and "parrot_freon" boards that have |
| 38 // All ChromeOS devices have SSDs. Except some parrots. | 39 // devices with rotating disks. All other ChromeOS devices have SSDs. |
| 40 if (board != "unknown" && |
| 41 !base::StartsWith(board, "parrot", base::CompareCase::SENSITIVE)) { |
| 39 *has_seek_penalty = false; | 42 *has_seek_penalty = false; |
| 40 return true; | 43 return true; |
| 41 } | 44 } |
| 42 #endif | 45 #endif |
| 43 | 46 |
| 44 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 47 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 45 if (!file.IsValid()) | 48 if (!file.IsValid()) |
| 46 return false; | 49 return false; |
| 47 | 50 |
| 48 struct stat path_stat; | 51 struct stat path_stat; |
| 49 int error = fstat(file.GetPlatformFile(), &path_stat); | 52 int error = fstat(file.GetPlatformFile(), &path_stat); |
| 50 if (error < 0 || MAJOR(path_stat.st_dev) != kFirstScsiMajorNumber) { | 53 if (error < 0 || MAJOR(path_stat.st_dev) != kFirstScsiMajorNumber) { |
| 51 // TODO(dbeam): support more SCSI major numbers (e.g. /dev/sdq+) and LVM? | 54 // TODO(dbeam): support more SCSI major numbers (e.g. /dev/sdq+) and LVM? |
| 52 return false; | 55 return false; |
| 53 } | 56 } |
| 54 | 57 |
| 55 char sdX = 'a' + MINOR(path_stat.st_dev) / kPartitionsPerScsiDevice; | 58 char sdX = 'a' + MINOR(path_stat.st_dev) / kPartitionsPerScsiDevice; |
| 56 std::string rotational_path = base::StringPrintf(kRotationalFormat, sdX); | 59 std::string rotational_path = base::StringPrintf(kRotationalFormat, sdX); |
| 57 std::string rotates; | 60 std::string rotates; |
| 58 if (!base::ReadFileToString(base::FilePath(rotational_path), &rotates)) | 61 if (!base::ReadFileToString(base::FilePath(rotational_path), &rotates)) |
| 59 return false; | 62 return false; |
| 60 | 63 |
| 61 *has_seek_penalty = rotates.substr(0, 1) == "1"; | 64 *has_seek_penalty = rotates.substr(0, 1) == "1"; |
| 62 return true; | 65 return true; |
| 63 } | 66 } |
| 64 | 67 |
| 65 } // namespace metrics | 68 } // namespace metrics |
| OLD | NEW |