drive_metrics_provider_linux.cc 1.6 KB

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