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

Commit 46b49da5 authored by Bertrand SIMONNET's avatar Bertrand SIMONNET Committed by chrome-internal-fetch
Browse files

Add an uploader to metrics_daemon

Metrics_daemon will now upload the metrics to Chrome's backend when Chrome is
not available.
This uses //components/metrics from chrome to use its protobuf definition and
the metrics common code.

This functionality is not yet enabled. It will be once the end-to-end test is
enabled.

BUG=chromium:358283, chromium:364579
TEST=FEATURES=test emerge-amd64-generic metrics

CQ-DEPEND=CL:205790
CQ-DEPEND=CL:206055

Change-Id: I87aaf7a2ac041581fa3ffd4ec61f73e933c00a52
Reviewed-on: https://chromium-review.googlesource.com/205810


Reviewed-by: default avatarBertrand Simonnet <bsimonnet@chromium.org>
Tested-by: default avatarBertrand Simonnet <bsimonnet@chromium.org>
Reviewed-by: default avatarLuigi Semenzato <semenzato@chromium.org>
Commit-Queue: Bertrand Simonnet <bsimonnet@chromium.org>
parent 4a6c942d
Loading
Loading
Loading
Loading
+85 −3
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
        'gthread-2.0',
        'libchrome-<(libbase_ver)',
        'libchromeos-<(libbase_ver)',
        'libcurl',
      ]
    },
    'cflags_cc': [
@@ -24,6 +25,8 @@
      'type': 'static_library',
      'dependencies': [
        '../metrics/libmetrics-<(libbase_ver).gyp:libmetrics-<(libbase_ver)',
        'libupload_service',
        'metrics_proto',
      ],
      'link_settings': {
        'libraries': [
@@ -35,7 +38,8 @@
        'persistent_integer.cc',
        'metrics_daemon.cc',
        'metrics_daemon_main.cc',
      ]
      ],
      'include_dirs': ['.'],
    },
    {
      'target_name': 'metrics_client',
@@ -47,6 +51,67 @@
        'metrics_client.cc',
      ]
    },
    {
      'target_name': 'libupload_service',
      'type': 'static_library',
      'dependencies': [
        'metrics_proto'
      ],
      'link_settings': {
        'libraries': [
          '-lgflags',
          '-lvboot_host',
        ],
      },
      'variables': {
        'exported_deps': [
          'protobuf-lite',
        ],
        'deps': [
          '<@(exported_deps)',
        ],
      },
      'all_dependent_settings': {
        'variables': {
          'deps+': [
            '<@(exported_deps)',
          ],
        },
      },
      'sources': [
        'uploader/upload_service.cc',
        'uploader/metrics_log.cc',
        'uploader/system_profile_cache.cc',
        'uploader/curl_sender.cc',
        'components/metrics/chromeos/metric_sample.cc',
        'components/metrics/chromeos/serialization_utils.cc',
        'components/metrics/metrics_log_base.cc',
        'components/metrics/metrics_log_manager.cc',
        'components/metrics/metrics_hashes.cc',
      ],
      'include_dirs': ['.']
    },
    {
      'target_name': 'metrics_proto',
      'type': 'static_library',
      'variables': {
        'proto_in_dir': 'components/metrics/proto/',
        'proto_out_dir': 'include/components/metrics/proto',
      },
      'sources': [
        '<(proto_in_dir)/chrome_user_metrics_extension.proto',
        '<(proto_in_dir)/histogram_event.proto',
        '<(proto_in_dir)/omnibox_event.proto',
        '<(proto_in_dir)/perf_data.proto',
        '<(proto_in_dir)/profiler_event.proto',
        '<(proto_in_dir)/sampled_profile.proto',
        '<(proto_in_dir)/system_profile.proto',
        '<(proto_in_dir)/user_action_event.proto',
      ],
      'includes': [
        '../../platform2/common-mk/protoc.gypi'
      ],
    },
  ],
  'conditions': [
    ['USE_passive_metrics == 1', {
@@ -94,6 +159,22 @@
            'timer_test.cc',
          ]
        },
        {
          'target_name': 'upload_service_test',
          'type': 'executable',
          'sources': [
            'persistent_integer.cc',
            'uploader/mock/sender_mock.cc',
            'uploader/upload_service_test.cc',
          ],
          'dependencies': [
            'libupload_service',
          ],
          'includes':[
            '../../platform2/common-mk/common_test.gypi',
          ],
          'include_dirs': ['.']
        },
      ],
    }],
    ['USE_passive_metrics == 1 and USE_test == 1', {
@@ -107,9 +188,10 @@
          'includes': ['../../platform2/common-mk/common_test.gypi'],
          'sources': [
            'metrics_daemon_test.cc',
          ]
          ],
          'include_dirs': ['.'],
        },
      ],
    }],
  ],
  ]
}
+13 −4
Original line number Diff line number Diff line
@@ -14,7 +14,6 @@
#include <string.h>
#include <time.h>

#include <base/at_exit.h>
#include <base/file_util.h>
#include <base/files/file_path.h>
#include <base/hash.h>
@@ -26,6 +25,7 @@
#include <base/sys_info.h>
#include <chromeos/dbus/service_constants.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "uploader/upload_service.h"

using base::FilePath;
using base::StringPrintf;
@@ -163,8 +163,6 @@ double MetricsDaemon::GetActiveTime() {
}

void MetricsDaemon::Run(bool run_as_daemon) {
  base::AtExitManager at_exit_manager;

  if (run_as_daemon && daemon(0, 0) != 0)
    return;

@@ -188,6 +186,10 @@ void MetricsDaemon::Run(bool run_as_daemon) {
  Loop();
}

void MetricsDaemon::RunUploaderTest() {
  upload_service_->UploadEvent();
}

uint32 MetricsDaemon::GetOsVersionHash() {
  static uint32 cached_version_hash = 0;
  static bool version_hash_is_cached = false;
@@ -205,7 +207,9 @@ uint32 MetricsDaemon::GetOsVersionHash() {
  return cached_version_hash;
}

void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib,
void MetricsDaemon::Init(bool testing,
                         bool uploader_active,
                         MetricsLibraryInterface* metrics_lib,
                         const string& diskstats_path,
                         const string& vmstats_path,
                         const string& scaling_max_freq_path,
@@ -305,6 +309,11 @@ void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib,

  update_stats_timeout_id_ =
      g_timeout_add(kUpdateStatsIntervalMs, &HandleUpdateStatsTimeout, this);

  if (uploader_active) {
    upload_service_.reset(new UploadService());
    upload_service_->Init();
  }
}

void MetricsDaemon::Loop() {
+9 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#include "metrics/metrics_library.h"
#include "metrics/persistent_integer.h"
#include "uploader/upload_service.h"

using chromeos_metrics::PersistentInteger;

@@ -27,7 +28,9 @@ class MetricsDaemon {
  ~MetricsDaemon();

  // Initializes.
  void Init(bool testing, MetricsLibraryInterface* metrics_lib,
  void Init(bool testing,
            bool uploader_active,
            MetricsLibraryInterface* metrics_lib,
            const std::string& diskstats_path,
            const std::string& vmstats_path,
            const std::string& cpuinfo_max_freq_path,
@@ -37,6 +40,9 @@ class MetricsDaemon {
  // forking.
  void Run(bool run_as_daemon);

  // Triggers an upload event and exit. (Used to test UploadService)
  void RunUploaderTest();

 protected:
  // Used also by the unit tests.
  static const char kComprDataSizeName[];
@@ -363,6 +369,8 @@ class MetricsDaemon {
  std::string vmstats_path_;
  std::string scaling_max_freq_path_;
  std::string cpuinfo_max_freq_path_;

  scoped_ptr<UploadService> upload_service_;
};

#endif  // METRICS_METRICS_DAEMON_H_
+27 −4
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include <base/at_exit.h>
#include <base/command_line.h>
#include <base/logging.h>
#include <base/strings/string_util.h>
@@ -17,8 +17,19 @@ const char kScalingMaxFreqPath[] =
const char kCpuinfoMaxFreqPath[] =
    "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";

// TODO(bsimonnet): Change this to use base::CommandLine (crbug.com/375017)
DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)");

// The uploader is disabled by default on ChromeOS as Chrome is responsible for
// sending the metrics.
DEFINE_bool(uploader, false, "activate the uploader");

// Upload the metrics once and exit. (used for testing)
DEFINE_bool(
    uploader_test,
    false,
    "run the uploader once and exit");

// Returns the path to the disk stats in the sysfs.  Returns the null string if
// it cannot find the disk stats file.
static
@@ -51,11 +62,23 @@ int main(int argc, char** argv) {
  // Also log to stderr when not running as daemon.
  chromeos::InitLog(chromeos::kLogToSyslog | chromeos::kLogHeader |
                    (FLAGS_daemon ? 0 : chromeos::kLogToStderr));

  MetricsLibrary metrics_lib;
  metrics_lib.Init();
  MetricsDaemon daemon;
  daemon.Init(false, &metrics_lib, MetricsMainDiskStatsPath(),
      "/proc/vmstat", kScalingMaxFreqPath, kCpuinfoMaxFreqPath);
  daemon.Init(false,
              FLAGS_uploader | FLAGS_uploader_test,
              &metrics_lib,
              MetricsMainDiskStatsPath(),
              "/proc/vmstat",
              kScalingMaxFreqPath,
              kCpuinfoMaxFreqPath);


  base::AtExitManager at_exit_manager;
  if (FLAGS_uploader_test) {
    daemon.RunUploaderTest();
    return 0;
  }

  daemon.Run(FLAGS_daemon);
}
+7 −2
Original line number Diff line number Diff line
@@ -58,8 +58,13 @@ class MetricsDaemonTest : public testing::Test {
    CreateUint64ValueFile(base::FilePath(kFakeScalingMaxFreqPath), 10000000);

    chromeos_metrics::PersistentInteger::SetTestingMode(true);
    daemon_.Init(true, &metrics_lib_, kFakeDiskStatsName, kFakeVmStatsName,
        kFakeScalingMaxFreqPath, kFakeCpuinfoMaxFreqPath);
    daemon_.Init(true,
                 false,
                 &metrics_lib_,
                 kFakeDiskStatsName,
                 kFakeVmStatsName,
                 kFakeScalingMaxFreqPath,
                 kFakeCpuinfoMaxFreqPath);

    // Replace original persistent values with mock ones.
    daily_active_use_mock_ =
Loading