Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8a2a635d authored by Bertrand Simonnet's avatar Bertrand Simonnet Committed by Gerrit Code Review
Browse files

Merge "metricsd: Specify directory for persistent integers."

parents 07d9de70 a7bc1c14
Loading
Loading
Loading
Loading
+32 −31
Original line number Diff line number Diff line
@@ -149,8 +149,7 @@ uint32_t MetricsCollector::GetOsVersionHash() {
  return version_hash;
}

void MetricsCollector::Init(bool testing,
                         MetricsLibraryInterface* metrics_lib,
void MetricsCollector::Init(bool testing, MetricsLibraryInterface* metrics_lib,
                            const string& diskstats_path,
                            const base::FilePath& metrics_directory) {
  CHECK(metrics_lib);
@@ -159,41 +158,43 @@ void MetricsCollector::Init(bool testing,
  metrics_lib_ = metrics_lib;

  daily_active_use_.reset(
      new PersistentInteger("Platform.UseTime.PerDay"));
      new PersistentInteger("Platform.UseTime.PerDay", metrics_directory_));
  version_cumulative_active_use_.reset(
      new PersistentInteger("Platform.CumulativeUseTime"));
      new PersistentInteger("Platform.CumulativeUseTime", metrics_directory_));
  version_cumulative_cpu_use_.reset(
      new PersistentInteger("Platform.CumulativeCpuTime"));
      new PersistentInteger("Platform.CumulativeCpuTime", metrics_directory_));

  kernel_crash_interval_.reset(
      new PersistentInteger("Platform.KernelCrashInterval"));
  unclean_shutdown_interval_.reset(
      new PersistentInteger("Platform.UncleanShutdownInterval"));
  kernel_crash_interval_.reset(new PersistentInteger(
      "Platform.KernelCrashInterval", metrics_directory_));
  unclean_shutdown_interval_.reset(new PersistentInteger(
      "Platform.UncleanShutdownInterval", metrics_directory_));
  user_crash_interval_.reset(
      new PersistentInteger("Platform.UserCrashInterval"));
      new PersistentInteger("Platform.UserCrashInterval", metrics_directory_));

  any_crashes_daily_count_.reset(
      new PersistentInteger("Platform.AnyCrashes.PerDay"));
      new PersistentInteger("Platform.AnyCrashes.PerDay", metrics_directory_));
  any_crashes_weekly_count_.reset(
      new PersistentInteger("Platform.AnyCrashes.PerWeek"));
      new PersistentInteger("Platform.AnyCrashes.PerWeek", metrics_directory_));
  user_crashes_daily_count_.reset(
      new PersistentInteger("Platform.UserCrashes.PerDay"));
  user_crashes_weekly_count_.reset(
      new PersistentInteger("Platform.UserCrashes.PerWeek"));
  kernel_crashes_daily_count_.reset(
      new PersistentInteger("Platform.KernelCrashes.PerDay"));
  kernel_crashes_weekly_count_.reset(
      new PersistentInteger("Platform.KernelCrashes.PerWeek"));
  kernel_crashes_version_count_.reset(
      new PersistentInteger("Platform.KernelCrashesSinceUpdate"));
  unclean_shutdowns_daily_count_.reset(
      new PersistentInteger("Platform.UncleanShutdown.PerDay"));
  unclean_shutdowns_weekly_count_.reset(
      new PersistentInteger("Platform.UncleanShutdowns.PerWeek"));

  daily_cycle_.reset(new PersistentInteger("daily.cycle"));
  weekly_cycle_.reset(new PersistentInteger("weekly.cycle"));
  version_cycle_.reset(new PersistentInteger("version.cycle"));
      new PersistentInteger("Platform.UserCrashes.PerDay", metrics_directory_));
  user_crashes_weekly_count_.reset(new PersistentInteger(
      "Platform.UserCrashes.PerWeek", metrics_directory_));
  kernel_crashes_daily_count_.reset(new PersistentInteger(
      "Platform.KernelCrashes.PerDay", metrics_directory_));
  kernel_crashes_weekly_count_.reset(new PersistentInteger(
      "Platform.KernelCrashes.PerWeek", metrics_directory_));
  kernel_crashes_version_count_.reset(new PersistentInteger(
      "Platform.KernelCrashesSinceUpdate", metrics_directory_));
  unclean_shutdowns_daily_count_.reset(new PersistentInteger(
      "Platform.UncleanShutdown.PerDay", metrics_directory_));
  unclean_shutdowns_weekly_count_.reset(new PersistentInteger(
      "Platform.UncleanShutdowns.PerWeek", metrics_directory_));

  daily_cycle_.reset(new PersistentInteger("daily.cycle", metrics_directory_));
  weekly_cycle_.reset(
      new PersistentInteger("weekly.cycle", metrics_directory_));
  version_cycle_.reset(
      new PersistentInteger("version.cycle", metrics_directory_));

  disk_usage_collector_.reset(new DiskUsageCollector(metrics_lib_));
  averaged_stats_collector_.reset(
+0 −3
Original line number Diff line number Diff line
@@ -45,9 +45,6 @@ class MetricsCollectorTest : public testing::Test {
  virtual void SetUp() {
    brillo::FlagHelper::Init(0, nullptr, "");
    EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());

    chromeos_metrics::PersistentInteger::SetMetricsDirectory(
        temp_dir_.path().value());
    daemon_.Init(true, &metrics_lib_, "", temp_dir_.path());
  }

+12 −19
Original line number Diff line number Diff line
@@ -23,19 +23,15 @@

#include "constants.h"


namespace chromeos_metrics {

// Static class member instantiation.
std::string PersistentInteger::metrics_directory_ = metrics::kMetricsDirectory;

PersistentInteger::PersistentInteger(const std::string& name) :
      value_(0),
PersistentInteger::PersistentInteger(const std::string& name,
                                     const base::FilePath& directory)
    : value_(0),
      version_(kVersion),
      name_(name),
      synced_(false) {
  backing_file_name_ = metrics_directory_ + name_;
}
      backing_file_path_(directory.Append(name_)),
      synced_(false) {}

PersistentInteger::~PersistentInteger() {}

@@ -62,23 +58,25 @@ void PersistentInteger::Add(int64_t x) {
}

void PersistentInteger::Write() {
  int fd = HANDLE_EINTR(open(backing_file_name_.c_str(),
  int fd = HANDLE_EINTR(open(backing_file_path_.value().c_str(),
                             O_WRONLY | O_CREAT | O_TRUNC,
                             S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH));
  PCHECK(fd >= 0) << "cannot open " << backing_file_name_ << " for writing";
  PCHECK(fd >= 0) << "cannot open " << backing_file_path_.value()
                  << " for writing";
  PCHECK((HANDLE_EINTR(write(fd, &version_, sizeof(version_))) ==
          sizeof(version_)) &&
         (HANDLE_EINTR(write(fd, &value_, sizeof(value_))) ==
          sizeof(value_)))
      << "cannot write to " << backing_file_name_;
      << "cannot write to " << backing_file_path_.value();
  close(fd);
  synced_ = true;
}

bool PersistentInteger::Read() {
  int fd = HANDLE_EINTR(open(backing_file_name_.c_str(), O_RDONLY));
  int fd = HANDLE_EINTR(open(backing_file_path_.value().c_str(), O_RDONLY));
  if (fd < 0) {
    PLOG(WARNING) << "cannot open " << backing_file_name_ << " for reading";
    PLOG(WARNING) << "cannot open " << backing_file_path_.value()
                  << " for reading";
    return false;
  }
  int32_t version;
@@ -95,9 +93,4 @@ bool PersistentInteger::Read() {
  return read_succeeded;
}

void PersistentInteger::SetMetricsDirectory(const std::string& directory) {
  metrics_directory_ = directory;
}


}  // namespace chromeos_metrics
+4 −7
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@

#include <string>

#include <base/files/file_path.h>

namespace chromeos_metrics {

// PersistentIntegers is a named 64-bit integer value backed by a file.
@@ -29,7 +31,7 @@ namespace chromeos_metrics {

class PersistentInteger {
 public:
  explicit PersistentInteger(const std::string& name);
  PersistentInteger(const std::string& name, const base::FilePath& directory);

  // Virtual only because of mock.
  virtual ~PersistentInteger();
@@ -50,10 +52,6 @@ class PersistentInteger {
  // Virtual only because of mock.
  virtual void Add(int64_t x);

  // Sets the directory path for all persistent integers.
  // This is used in unittests to change where the counters are stored.
  static void SetMetricsDirectory(const std::string& directory);

 private:
  static const int kVersion = 1001;

@@ -68,8 +66,7 @@ class PersistentInteger {
  int64_t value_;
  int32_t version_;
  std::string name_;
  std::string backing_file_name_;
  static std::string metrics_directory_;
  base::FilePath backing_file_path_;
  bool synced_;
};

+4 −3
Original line number Diff line number Diff line
@@ -27,8 +27,9 @@ namespace chromeos_metrics {

class PersistentIntegerMock : public PersistentInteger {
 public:
  explicit PersistentIntegerMock(const std::string& name)
      : PersistentInteger(name) {}
  explicit PersistentIntegerMock(const std::string& name,
                                 const base::FilePath& directory)
      : PersistentInteger(name, directory) {}
  MOCK_METHOD1(Add, void(int64_t count));
};

Loading