Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "chrome/browser/metrics/drive_metrics_provider.h" | |
| 6 | |
| 7 #include <linux/kdev_t.h> // For MAJOR()/MINOR(). | |
| 8 #include <sys/stat.h> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/files/file.h" | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/files/file_util.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 16 | |
| 17 #if defined(OS_CHROMEOS) | |
| 18 #include "base/sys_info.h" | |
| 19 #endif | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // See http://www.kernel.org/doc/Documentation/devices.txt for more info. | |
| 24 const int kScsiMajorNumber = 8; | |
| 25 const char kRotationalFormat[] = "/sys/block/sd%s/queue/rotational"; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 // static | |
| 30 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path, | |
| 31 bool* has_seek_penalty) { | |
| 32 #if defined(OS_CHROMEOS) | |
| 33 std::string board = base::SysInfo::GetLsbReleaseBoard(); | |
| 34 if (board != "unknown" && board != "parrot") { | |
| 35 // All ChromeOS devices have SSDs. Except some parrots. | |
| 36 *has_seek_penalty = false; | |
| 37 return true; | |
| 38 } | |
| 39 #endif | |
| 40 | |
| 41 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 42 if (!file.IsValid()) | |
| 43 return false; | |
| 44 | |
| 45 struct stat path_stat; | |
| 46 int error = fstat(file.GetPlatformFile(), &path_stat); | |
| 47 if (error < 0 || MAJOR(path_stat.st_dev) != kScsiMajorNumber) { | |
| 48 // TODO(dbeam): support non-SCSI drives (e.g., LVM)? | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 uint16 minor = MINOR(path_stat.st_dev) / 16; | |
|
Lei Zhang
2015/04/03 22:45:57
Why is this divided by 16?
Dan Beam
2015/04/03 22:47:09
16 partitions per drive
Lei Zhang
2015/04/03 22:51:10
Oh, I forgot about that. That's why I was thinking
Dan Beam
2015/04/03 23:01:40
ignoring /dev/sdq+ for now
| |
| 53 std::string sdXX; | |
| 54 if (minor >= 26) | |
| 55 sdXX += 'a' + (minor / 26) - 1; | |
|
Dan Beam
2015/04/03 22:27:05
maybe this will
| |
| 56 sdXX += 'a' + minor % 26; | |
| 57 | |
| 58 std::string rotational = base::StringPrintf(kRotationalFormat, sdXX.c_str()); | |
| 59 std::string rotates; | |
| 60 if (!base::ReadFileToString(base::FilePath(rotational), &rotates)) | |
| 61 return false; | |
| 62 | |
| 63 *has_seek_penalty = rotates.substr(0, 1) == "1"; | |
| 64 return true; | |
| 65 } | |
| OLD | NEW |