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. 4 5 #include "components/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/files/file.h" 12 #include "base/files/file_path.h" 13 #include "base/files/file_util.h" 14 #include "base/strings/string_util.h" 15 #include "base/strings/stringprintf.h" 16 #include "build/build_config.h" 17 18 namespace metrics { 19 namespace { 20 21 // See http://www.kernel.org/doc/Documentation/devices.txt for more info. 22 const int kFirstScsiMajorNumber = 8; 23 const int kPartitionsPerScsiDevice = 16; 24 const char kRotationalFormat[] = "/sys/block/sd%c/queue/rotational"; 25 26 } // namespace 27 28 // static HasSeekPenalty(const base::FilePath & path,bool * has_seek_penalty)29bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path, 30 bool* has_seek_penalty) { 31 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); 32 if (!file.IsValid()) 33 return false; 34 35 struct stat path_stat; 36 int error = fstat(file.GetPlatformFile(), &path_stat); 37 if (error < 0 || MAJOR(path_stat.st_dev) != kFirstScsiMajorNumber) { 38 // TODO(dbeam): support more SCSI major numbers (e.g. /dev/sdq+) and LVM? 39 return false; 40 } 41 42 char sdX = 'a' + MINOR(path_stat.st_dev) / kPartitionsPerScsiDevice; 43 std::string rotational_path = base::StringPrintf(kRotationalFormat, sdX); 44 std::string rotates; 45 if (!base::ReadFileToString(base::FilePath(rotational_path), &rotates)) 46 return false; 47 48 *has_seek_penalty = rotates.substr(0, 1) == "1"; 49 return true; 50 } 51 52 } // namespace metrics 53