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

Commit 239b8268 authored by Sam Leffler's avatar Sam Leffler
Browse files

remove TimeToDrop support

Now that flimflam has native support this can be purged.

TEST=run FEATURES=test emerge-x86-generic metrics; verify TimeToDrop is still being recorded w/ about:histograms/Network

Review URL: http://codereview.chromium.org/3233004
parent 4c5daa47
Loading
Loading
Loading
Loading
+1 −118
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ using std::string;

#define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error")
#define DBUS_IFACE_CRASH_REPORTER "org.chromium.CrashReporter"
#define DBUS_IFACE_FLIMFLAM_MANAGER "org.chromium.flimflam.Manager"
#define DBUS_IFACE_POWER_MANAGER "org.chromium.PowerManager"
#define DBUS_IFACE_SESSION_MANAGER "org.chromium.SessionManagerInterface"

@@ -51,13 +50,6 @@ const int MetricsDaemon::kMetricDailyUseTimeMin = 1;
const int MetricsDaemon::kMetricDailyUseTimeMax = kMinutesPerDay;
const int MetricsDaemon::kMetricDailyUseTimeBuckets = 50;

const char MetricsDaemon::kMetricTimeToNetworkDropName[] =
    "Network.TimeToDrop";
const int MetricsDaemon::kMetricTimeToNetworkDropMin = 1;
const int MetricsDaemon::kMetricTimeToNetworkDropMax =
    8 /* hours */ * kMinutesPerHour * kSecondsPerMinute;
const int MetricsDaemon::kMetricTimeToNetworkDropBuckets = 50;

// crash interval metrics
const char MetricsDaemon::kMetricKernelCrashIntervalName[] =
    "Logging.KernelCrashInterval";
@@ -103,12 +95,6 @@ const char* MetricsDaemon::kDBusMatches_[] = {
  "path='/',"
  "member='UserCrash'",

  "type='signal',"
  "sender='org.chromium.flimflam',"
  "interface='" DBUS_IFACE_FLIMFLAM_MANAGER "',"
  "path='/',"
  "member='StateChanged'",

  "type='signal',"
  "interface='" DBUS_IFACE_POWER_MANAGER "',"
  "path='/'",
@@ -120,12 +106,6 @@ const char* MetricsDaemon::kDBusMatches_[] = {
  "member='SessionStateChanged'",
};

// static
const char* MetricsDaemon::kNetworkStates_[] = {
#define STATE(name, capname) #name,
#include "network_states.h"
};

// static
const char* MetricsDaemon::kPowerStates_[] = {
#define STATE(name, capname) #name,
@@ -138,56 +118,8 @@ const char* MetricsDaemon::kSessionStates_[] = {
#include "session_states.h"
};

// Invokes a remote method over D-Bus that takes no input arguments
// and returns a string result. The method call is issued with a 2
// second blocking timeout. Returns an empty string on failure or
// timeout.
static string DBusGetString(DBusConnection* connection,
                            const string& destination,
                            const string& path,
                            const string& interface,
                            const string& method) {
  DBusMessage* message =
      dbus_message_new_method_call(destination.c_str(),
                                   path.c_str(),
                                   interface.c_str(),
                                   method.c_str());
  if (!message) {
    DLOG(WARNING) << "DBusGetString: unable to allocate a message";
    return "";
  }

  DBusError error;
  dbus_error_init(&error);
  const int kTimeout = 2000;  // ms
  DLOG(INFO) << "DBusGetString: dest=" << destination << " path=" << path
             << " iface=" << interface << " method=" << method;
  DBusMessage* reply =
      dbus_connection_send_with_reply_and_block(connection, message, kTimeout,
                                                &error);
  dbus_message_unref(message);
  if (dbus_error_is_set(&error) || !reply) {
    DLOG(WARNING) << "DBusGetString: call failed";
    return "";
  }
  DBusMessageIter iter;
  dbus_message_iter_init(reply, &iter);
  if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
    NOTREACHED();
    dbus_message_unref(reply);
    return "";
  }
  const char* c_result = "";
  dbus_message_iter_get_basic(&iter, &c_result);
  string result = c_result;
  DLOG(INFO) << "DBusGetString: result=" << result;
  dbus_message_unref(reply);
  return result;
}

MetricsDaemon::MetricsDaemon()
    : network_state_(kUnknownNetworkState),
      power_state_(kUnknownPowerState),
    : power_state_(kUnknownPowerState),
      session_state_(kUnknownSessionState),
      user_active_(false),
      usemon_interval_(0),
@@ -317,11 +249,6 @@ void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib) {
  // the registered D-Bus matches is successful. The daemon is not
  // activated for D-Bus messages that don't match.
  CHECK(dbus_connection_add_filter(connection, MessageFilter, this, NULL));

  // Initializes the current network state by retrieving it from flimflam.
  string state_name = DBusGetString(connection, "org.chromium.flimflam", "/",
                                    DBUS_IFACE_FLIMFLAM_MANAGER, "GetState");
  NetStateChanged(state_name.c_str(), TimeTicks::Now());
}

void MetricsDaemon::Loop() {
@@ -355,13 +282,6 @@ DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection,
    CHECK(strcmp(dbus_message_get_member(message),
                 "UserCrash") == 0);
    daemon->ProcessUserCrash();
  } else if (strcmp(interface, DBUS_IFACE_FLIMFLAM_MANAGER) == 0) {
    CHECK(strcmp(dbus_message_get_member(message),
                 "StateChanged") == 0);

    char* state_name;
    dbus_message_iter_get_basic(&iter, &state_name);
    daemon->NetStateChanged(state_name, ticks);
  } else if (strcmp(interface, DBUS_IFACE_POWER_MANAGER) == 0) {
    const char* member = dbus_message_get_member(message);
    if (strcmp(member, "ScreenIsLocked") == 0) {
@@ -388,43 +308,6 @@ DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection,
  return DBUS_HANDLER_RESULT_HANDLED;
}

void MetricsDaemon::NetStateChanged(const char* state_name, TimeTicks ticks) {
  DLOG(INFO) << "network state: " << state_name;

  NetworkState state = LookupNetworkState(state_name);

  // Logs the time in seconds between the network going online to
  // going offline (or, more precisely, going not online) in order to
  // measure the mean time to network dropping. Going offline as part
  // of suspend-to-RAM is not logged as network drop -- the assumption
  // is that the message for suspend-to-RAM comes before the network
  // offline message which seems to and should be the case.
  if (state != kNetworkStateOnline &&
      network_state_ == kNetworkStateOnline &&
      power_state_ != kPowerStateMem) {
    TimeDelta since_online = ticks - network_state_last_;
    int online_time = static_cast<int>(since_online.InSeconds());
    SendMetric(kMetricTimeToNetworkDropName, online_time,
               kMetricTimeToNetworkDropMin,
               kMetricTimeToNetworkDropMax,
               kMetricTimeToNetworkDropBuckets);
  }

  network_state_ = state;
  network_state_last_ = ticks;
}

MetricsDaemon::NetworkState
MetricsDaemon::LookupNetworkState(const char* state_name) {
  for (int i = 0; i < kNumberNetworkStates; i++) {
    if (strcmp(state_name, kNetworkStates_[i]) == 0) {
      return static_cast<NetworkState>(i);
    }
  }
  DLOG(WARNING) << "unknown network connection state: " << state_name;
  return kUnknownNetworkState;
}

void MetricsDaemon::PowerStateChanged(const char* state_name, Time now) {
  DLOG(INFO) << "power state: " << state_name;
  power_state_ = LookupPowerState(state_name);
+0 −27
Original line number Diff line number Diff line
@@ -42,13 +42,10 @@ class MetricsDaemon {
  FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoLast);
  FRIEND_TEST(MetricsDaemonTest, GetHistogramPath);
  FRIEND_TEST(MetricsDaemonTest, IsNewEpoch);
  FRIEND_TEST(MetricsDaemonTest, LookupNetworkState);
  FRIEND_TEST(MetricsDaemonTest, LookupPowerState);
  FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState);
  FRIEND_TEST(MetricsDaemonTest, LookupSessionState);
  FRIEND_TEST(MetricsDaemonTest, MessageFilter);
  FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop);
  FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend);
  FRIEND_TEST(MetricsDaemonTest, PowerStateChanged);
  FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash);
  FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown);
@@ -64,14 +61,6 @@ class MetricsDaemon {
  FRIEND_TEST(MetricsDaemonTest, SetUserActiveState);
  FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);

  // The network states (see network_states.h).
  enum NetworkState {
    kUnknownNetworkState = -1, // Initial/unknown network state.
#define STATE(name, capname) kNetworkState ## capname,
#include "network_states.h"
    kNumberNetworkStates
  };

  // The power states (see power_states.h).
  enum PowerState {
    kUnknownPowerState = -1, // Initial/unknown power state.
@@ -116,10 +105,6 @@ class MetricsDaemon {
  static const char kMetricKernelCrashesWeeklyName[];
  static const char kMetricKernelCrashIntervalName[];
  static const char kMetricsPath[];
  static const int  kMetricTimeToNetworkDropBuckets;
  static const int  kMetricTimeToNetworkDropMax;
  static const int  kMetricTimeToNetworkDropMin;
  static const char kMetricTimeToNetworkDropName[];
  static const char kMetricUncleanShutdownIntervalName[];
  static const char kMetricUncleanShutdownsDailyName[];
  static const char kMetricUncleanShutdownsWeeklyName[];
@@ -130,9 +115,6 @@ class MetricsDaemon {
  // D-Bus message match strings.
  static const char* kDBusMatches_[];

  // Array of network states.
  static const char* kNetworkStates_[kNumberNetworkStates];

  // Array of power states.
  static const char* kPowerStates_[kNumberPowerStates];

@@ -161,12 +143,6 @@ class MetricsDaemon {
                                         DBusMessage* message,
                                         void* user_data);

  // Processes network state change.
  void NetStateChanged(const char* state_name, base::TimeTicks ticks);

  // Given the state name, returns the state id.
  NetworkState LookupNetworkState(const char* state_name);

  // Processes power state change.
  void PowerStateChanged(const char* state_name, base::Time now);

@@ -247,9 +223,6 @@ class MetricsDaemon {
  // The metrics library handle.
  MetricsLibraryInterface* metrics_lib_;

  // Current network state.
  NetworkState network_state_;

  // Timestamps last network state update.  This timestamp is used to
  // sample the time from the network going online to going offline so
  // TimeTicks ensures a monotonically increasing TimeDelta.
+0 −80
Original line number Diff line number Diff line
@@ -37,12 +37,6 @@ class TestTicks : public TimeTicks {
      : TimeTicks(seconds * Time::kMicrosecondsPerSecond) {}
};

// Overloaded for test failure printing purposes.
static std::ostream& operator<<(std::ostream& o, const TimeTicks& ticks) {
  o << ticks.ToInternalValue() << "us";
  return o;
};

// Overloaded for test failure printing purposes.
static std::ostream& operator<<(std::ostream& o, const Time& time) {
  o << time.ToInternalValue() << "us";
@@ -114,8 +108,6 @@ class MetricsDaemonTest : public testing::Test {

    EXPECT_FALSE(daemon_.user_active_);
    EXPECT_TRUE(daemon_.user_active_last_.is_null());
    EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, daemon_.network_state_);
    EXPECT_TRUE(daemon_.network_state_last_.is_null());
    EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_);
    EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_);

@@ -190,15 +182,6 @@ class MetricsDaemonTest : public testing::Test {
                 MetricsDaemon::kMetricDailyUseTimeBuckets);
  }

  // Adds a metrics library mock expectation that the specified time
  // to network dropping metric will be generated.
  void ExpectTimeToNetworkDropMetric(int sample) {
    ExpectMetric(MetricsDaemon::kMetricTimeToNetworkDropName, sample,
                 MetricsDaemon::kMetricTimeToNetworkDropMin,
                 MetricsDaemon::kMetricTimeToNetworkDropMax,
                 MetricsDaemon::kMetricTimeToNetworkDropBuckets);
  }

  // Converts from seconds to a Time object.
  Time TestTime(int64 seconds) {
    return Time::FromInternalValue(seconds * Time::kMicrosecondsPerSecond);
@@ -280,15 +263,6 @@ TEST_F(MetricsDaemonTest, ReportDailyUse) {
  MetricsDaemon::ReportDailyUse(&daemon_, /* tag */ 60, /* count */ -5);
}

TEST_F(MetricsDaemonTest, LookupNetworkState) {
  EXPECT_EQ(MetricsDaemon::kNetworkStateOnline,
            daemon_.LookupNetworkState("online"));
  EXPECT_EQ(MetricsDaemon::kNetworkStateOffline,
            daemon_.LookupNetworkState("offline"));
  EXPECT_EQ(MetricsDaemon::kUnknownNetworkState,
            daemon_.LookupNetworkState("somestate"));
}

TEST_F(MetricsDaemonTest, LookupPowerState) {
  EXPECT_EQ(MetricsDaemon::kPowerStateOn,
            daemon_.LookupPowerState("on"));
@@ -342,16 +316,6 @@ TEST_F(MetricsDaemonTest, MessageFilter) {
  EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res);
  DeleteDBusMessage(msg);

  msg = NewDBusSignalString("/",
                            "org.chromium.flimflam.Manager",
                            "StateChanged",
                            "online");
  EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, daemon_.network_state_);
  res = MetricsDaemon::MessageFilter(/* connection */ NULL, msg, &daemon_);
  EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, daemon_.network_state_);
  EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res);
  DeleteDBusMessage(msg);

  msg = NewDBusSignalString("/",
                            "org.chromium.PowerManager",
                            "PowerStateChanged",
@@ -393,50 +357,6 @@ TEST_F(MetricsDaemonTest, MessageFilter) {
  DeleteDBusMessage(msg);
}

TEST_F(MetricsDaemonTest, NetStateChangedSimpleDrop) {
  daemon_.NetStateChanged("online", TestTicks(10));
  EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, daemon_.network_state_);
  EXPECT_EQ(TestTicks(10), daemon_.network_state_last_);

  ExpectTimeToNetworkDropMetric(20);
  daemon_.NetStateChanged("offline", TestTicks(30));
  EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, daemon_.network_state_);
  EXPECT_EQ(TestTicks(30), daemon_.network_state_last_);
}

TEST_F(MetricsDaemonTest, NetStateChangedSuspend) {
  daemon_.NetStateChanged("offline", TestTicks(30));
  EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, daemon_.network_state_);
  EXPECT_EQ(TestTicks(30), daemon_.network_state_last_);

  daemon_.NetStateChanged("online", TestTicks(60));
  EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, daemon_.network_state_);
  EXPECT_EQ(TestTicks(60), daemon_.network_state_last_);

  daemon_.power_state_ = MetricsDaemon::kPowerStateMem;
  daemon_.NetStateChanged("offline", TestTicks(85));
  EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, daemon_.network_state_);
  EXPECT_EQ(TestTicks(85), daemon_.network_state_last_);

  daemon_.NetStateChanged("somestate", TestTicks(90));
  EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, daemon_.network_state_);
  EXPECT_EQ(TestTicks(90), daemon_.network_state_last_);

  daemon_.NetStateChanged("offline", TestTicks(95));
  EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, daemon_.network_state_);
  EXPECT_EQ(TestTicks(95), daemon_.network_state_last_);

  daemon_.power_state_ = MetricsDaemon::kPowerStateOn;
  daemon_.NetStateChanged("online", TestTicks(105));
  EXPECT_EQ(MetricsDaemon::kNetworkStateOnline, daemon_.network_state_);
  EXPECT_EQ(TestTicks(105), daemon_.network_state_last_);

  ExpectTimeToNetworkDropMetric(3);
  daemon_.NetStateChanged("offline", TestTicks(108));
  EXPECT_EQ(MetricsDaemon::kNetworkStateOffline, daemon_.network_state_);
  EXPECT_EQ(TestTicks(108), daemon_.network_state_last_);
}

TEST_F(MetricsDaemonTest, PowerStateChanged) {
  ExpectActiveUseUpdate(7, 0);
  daemon_.SetUserActiveState(/* active */ true,

metrics/network_states.h

deleted100644 → 0
+0 −28
Original line number Diff line number Diff line
// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// A table of network states, to be included when building tabular things.
//
// This file is used to construct two things: an enumerated type in
// metrics_daemon.h, and a table of structures with state names in
// metrics_daemon.cc.  Including this file ensures that the two tables are
// always in sync (and saves typing).  I don't know of other ways of achieving
// the same result in C/C++, but it doesn't mean there isn't one.

// Before you include this file, define STATE to do something useful, or else
// if will be a no-op.  STATE will be undefined on exit.  Don't worry about
// collisions for the STATE macro (as long as it's a macro) because the
// compiler will flag them---in that case, just change the name.  If someone is
// misguided enough to use STATE for something other than a macro, the error
// messages will be slightly more complicated.


#ifndef STATE
#define STATE(name, capname)
#endif

STATE(offline, Offline)
STATE(online, Online)

#undef STATE