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

Commit 5fc82ef7 authored by Todd Poynor's avatar Todd Poynor Committed by Gerrit Code Review
Browse files

Merge "metrics_collector: add libmetricscollectorservice binder service"

parents aae38d97 694553d0
Loading
Loading
Loading
Loading
+24 −3
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ metrics_collector_common := \
  collectors/cpu_usage_collector.cc \
  collectors/disk_usage_collector.cc \
  metrics_collector.cc \
  metrics_collector_service_trampoline.cc \
  persistent_integer.cc

metricsd_common := \
@@ -69,6 +70,7 @@ metrics_includes := external/gtest/include \
  $(LOCAL_PATH)/include
libmetrics_shared_libraries := libchrome libbinder libbrillo libutils
metrics_collector_shared_libraries := $(libmetrics_shared_libraries) \
  libbrillo-binder \
  libbrillo-dbus \
  libbrillo-http \
  libchrome-dbus \
@@ -77,6 +79,8 @@ metrics_collector_shared_libraries := $(libmetrics_shared_libraries) \
  librootdev \
  libweaved

metrics_collector_static_libraries := libmetricscollectorservice

metricsd_shared_libraries := \
  libbinder \
  libbrillo \
@@ -86,7 +90,7 @@ metricsd_shared_libraries := \
  libupdate_engine_client \
  libutils

# Static proxy library for the binder interface.
# Static proxy library for the metricsd binder interface.
# ========================================================
include $(CLEAR_VARS)
LOCAL_MODULE := metricsd_binder_proxy
@@ -94,6 +98,21 @@ LOCAL_SHARED_LIBRARIES := libbinder libutils
LOCAL_SRC_FILES := aidl/android/brillo/metrics/IMetricsd.aidl
include $(BUILD_STATIC_LIBRARY)

# Static library for the metrics_collector binder interface.
# ==========================================================
include $(CLEAR_VARS)
LOCAL_MODULE := libmetricscollectorservice
LOCAL_SHARED_LIBRARIES := libbinder libbrillo-binder libchrome libutils
LOCAL_CPP_EXTENSION := $(metrics_cpp_extension)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := \
  aidl/android/brillo/metrics/IMetricsCollectorService.aidl \
  metrics_collector_service_impl.cc \
  metrics_collector_service_client.cc
LOCAL_RTTI_FLAG := -fno-rtti
include $(BUILD_STATIC_LIBRARY)

# Shared library for metrics.
# ========================================================
include $(CLEAR_VARS)
@@ -151,7 +170,8 @@ LOCAL_RTTI_FLAG := -frtti
LOCAL_SHARED_LIBRARIES := $(metrics_collector_shared_libraries)
LOCAL_SRC_FILES := $(metrics_collector_common) \
  metrics_collector_main.cc
LOCAL_STATIC_LIBRARIES := metricsd_binder_proxy
LOCAL_STATIC_LIBRARIES := metricsd_binder_proxy \
  $(metrics_collector_static_libraries)
include $(BUILD_EXECUTABLE)

# metricsd daemon.
@@ -197,7 +217,8 @@ LOCAL_RTTI_FLAG := -frtti
LOCAL_SHARED_LIBRARIES := $(metrics_collector_shared_libraries)
LOCAL_SRC_FILES := $(metrics_collector_tests_sources) \
  $(metrics_collector_common)
LOCAL_STATIC_LIBRARIES := libBionicGtestMain libgmock metricsd_binder_proxy
LOCAL_STATIC_LIBRARIES := libBionicGtestMain libgmock metricsd_binder_proxy \
  $(metrics_collector_static_libraries)
include $(BUILD_NATIVE_TEST)

# Weave schema files
+21 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.brillo.metrics;

interface IMetricsCollectorService {
  oneway void notifyUserCrash();
}
+44 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Client interface to IMetricsCollectorService.

#ifndef METRICS_METRICS_COLLECTOR_SERVICE_CLIENT_H_
#define METRICS_METRICS_COLLECTOR_SERVICE_CLIENT_H_

#include "android/brillo/metrics/IMetricsCollectorService.h"

class MetricsCollectorServiceClient {
 public:
  MetricsCollectorServiceClient() = default;
  ~MetricsCollectorServiceClient() = default;

  // Initialize.  Returns true if OK, or false if IMetricsCollectorService
  // is not registered.
  bool Init();

  // Called by crash_reporter to report a userspace crash event.  Returns
  // true if successfully called the IMetricsCollectorService method of the
  // same name, or false if the service was not registered at Init() time.
  bool notifyUserCrash();

 private:
  // IMetricsCollectorService binder proxy
  android::sp<android::brillo::metrics::IMetricsCollectorService>
      metrics_collector_service_;
};

#endif  // METRICS_METRICS_COLLECTOR_SERVICE_CLIENT_H_
+5 −74
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
#include <dbus/message.h>

#include "constants.h"
#include "metrics_collector_service_trampoline.h"

using base::FilePath;
using base::StringPrintf;
@@ -46,11 +47,6 @@ using std::vector;

namespace {

const char kCrashReporterInterface[] = "org.chromium.CrashReporter";
const char kCrashReporterUserCrashSignal[] = "UserCrash";
const char kCrashReporterMatchRule[] =
    "type='signal',interface='%s',path='/',member='%s'";

const int kSecondsPerMinute = 60;
const int kMinutesPerHour = 60;
const int kHoursPerDay = 24;
@@ -130,6 +126,10 @@ int MetricsCollector::Run() {
    version_cumulative_cpu_use_->Set(0);
  }

  // Start metricscollectorservice via trampoline
  MetricsCollectorServiceTrampoline metricscollectorservice_trampoline(this);
  metricscollectorservice_trampoline.Run();

  return brillo::DBusDaemon::Run();
}

@@ -223,28 +223,6 @@ int MetricsCollector::OnInit() {
  bus_->AssertOnDBusThread();
  CHECK(bus_->SetUpAsyncOperations());

  if (bus_->is_connected()) {
    const std::string match_rule =
        base::StringPrintf(kCrashReporterMatchRule,
                           kCrashReporterInterface,
                           kCrashReporterUserCrashSignal);

    bus_->AddFilterFunction(&MetricsCollector::MessageFilter, this);

    DBusError error;
    dbus_error_init(&error);
    bus_->AddMatch(match_rule, &error);

    if (dbus_error_is_set(&error)) {
      LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got "
          << error.name << ": " << error.message;
      return EX_SOFTWARE;
    }
  } else {
    LOG(ERROR) << "DBus isn't connected.";
    return EX_UNAVAILABLE;
  }

  device_ = weaved::Device::CreateInstance(
      bus_,
      base::Bind(&MetricsCollector::UpdateWeaveState, base::Unretained(this)));
@@ -268,23 +246,6 @@ int MetricsCollector::OnInit() {
}

void MetricsCollector::OnShutdown(int* return_code) {
  if (!testing_ && bus_->is_connected()) {
    const std::string match_rule =
        base::StringPrintf(kCrashReporterMatchRule,
                           kCrashReporterInterface,
                           kCrashReporterUserCrashSignal);

    bus_->RemoveFilterFunction(&MetricsCollector::MessageFilter, this);

    DBusError error;
    dbus_error_init(&error);
    bus_->RemoveMatch(match_rule, &error);

    if (dbus_error_is_set(&error)) {
      LOG(ERROR) << "Failed to remove match rule \"" << match_rule << "\". Got "
          << error.name << ": " << error.message;
    }
  }
  brillo::DBusDaemon::OnShutdown(return_code);
}

@@ -340,36 +301,6 @@ void MetricsCollector::UpdateWeaveState() {
  }
}

// static
DBusHandlerResult MetricsCollector::MessageFilter(DBusConnection* connection,
                                                   DBusMessage* message,
                                                   void* user_data) {
  int message_type = dbus_message_get_type(message);
  if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) {
    DLOG(WARNING) << "unexpected message type " << message_type;
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  }

  // Signal messages always have interfaces.
  const std::string interface(dbus_message_get_interface(message));
  const std::string member(dbus_message_get_member(message));
  DLOG(INFO) << "Got " << interface << "." << member << " D-Bus signal";

  MetricsCollector* daemon = static_cast<MetricsCollector*>(user_data);

  DBusMessageIter iter;
  dbus_message_iter_init(message, &iter);
  if (interface == kCrashReporterInterface) {
    CHECK_EQ(member, kCrashReporterUserCrashSignal);
    daemon->ProcessUserCrash();
  } else {
    // Ignore messages from the bus itself.
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  }

  return DBUS_HANDLER_RESULT_HANDLED;
}

void MetricsCollector::ProcessUserCrash() {
  // Counts the active time up to now.
  UpdateStats(TimeTicks::Now(), Time::Now());
+4 −9
Original line number Diff line number Diff line
@@ -63,6 +63,10 @@ class MetricsCollector : public brillo::DBusDaemon {
  // Returns the active time since boot (uptime minus sleep time) in seconds.
  static double GetActiveTime();

  // Updates the active use time and logs time between user-space
  // process crashes.  Called via MetricsCollectorServiceTrampoline.
  void ProcessUserCrash();

 protected:
  // Used also by the unit tests.
  static const char kComprDataSizeName[];
@@ -108,11 +112,6 @@ class MetricsCollector : public brillo::DBusDaemon {
    int value;               // value from /proc/meminfo
  };

  // D-Bus filter callback.
  static DBusHandlerResult MessageFilter(DBusConnection* connection,
                                         DBusMessage* message,
                                         void* user_data);

  // Enables metrics reporting.
  void OnEnableMetrics(const std::weak_ptr<weaved::Command>& cmd);

@@ -122,10 +121,6 @@ class MetricsCollector : public brillo::DBusDaemon {
  // Updates the weave device state.
  void UpdateWeaveState();

  // Updates the active use time and logs time between user-space
  // process crashes.
  void ProcessUserCrash();

  // Updates the active use time and logs time between kernel crashes.
  void ProcessKernelCrash();

Loading