2010-04-14 22:32:20 +02:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#ifndef METRICS_DAEMON_H_
|
|
|
|
#define METRICS_DAEMON_H_
|
|
|
|
|
2010-04-27 20:02:18 +02:00
|
|
|
#include <dbus/dbus.h>
|
2010-05-04 01:45:37 +02:00
|
|
|
#include <glib.h>
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-06-11 00:59:53 +02:00
|
|
|
#include <base/scoped_ptr.h>
|
|
|
|
#include <base/time.h>
|
|
|
|
#include <gtest/gtest_prod.h> // for FRIEND_TEST
|
|
|
|
|
2010-05-12 22:05:45 +02:00
|
|
|
#include "metrics_library.h"
|
|
|
|
|
2010-06-11 00:59:53 +02:00
|
|
|
namespace chromeos_metrics { class TaggedCounterInterface; }
|
2010-05-06 01:06:37 +02:00
|
|
|
|
2010-04-14 22:32:20 +02:00
|
|
|
class MetricsDaemon {
|
|
|
|
|
|
|
|
public:
|
2010-06-11 00:59:53 +02:00
|
|
|
MetricsDaemon();
|
|
|
|
~MetricsDaemon();
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-05-18 20:00:59 +02:00
|
|
|
// Initializes.
|
|
|
|
void Init(bool testing, MetricsLibraryInterface* metrics_lib);
|
|
|
|
|
2010-04-27 20:02:18 +02:00
|
|
|
// Does all the work. If |run_as_daemon| is true, daemonizes by
|
2010-05-06 01:06:37 +02:00
|
|
|
// forking.
|
|
|
|
void Run(bool run_as_daemon);
|
2010-04-14 22:32:20 +02:00
|
|
|
|
|
|
|
private:
|
2010-05-06 01:06:37 +02:00
|
|
|
friend class MetricsDaemonTest;
|
2010-06-24 21:10:26 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, CheckKernelCrash);
|
2010-06-11 00:59:53 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, DailyUseReporter);
|
2010-06-24 21:10:26 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, KernelCrashIntervalReporter);
|
2010-05-06 01:06:37 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, LookupNetworkState);
|
|
|
|
FRIEND_TEST(MetricsDaemonTest, LookupPowerState);
|
|
|
|
FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState);
|
|
|
|
FRIEND_TEST(MetricsDaemonTest, LookupSessionState);
|
2010-05-06 01:19:39 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, MessageFilter);
|
2010-05-12 22:05:45 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop);
|
|
|
|
FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend);
|
2010-05-06 01:06:37 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, PowerStateChanged);
|
2010-06-24 21:10:26 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash);
|
2010-06-17 00:58:06 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash);
|
2010-05-06 01:06:37 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
|
2010-05-18 20:00:59 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, SendMetric);
|
2010-05-06 01:06:37 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
|
2010-06-11 00:59:53 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, SetUserActiveState);
|
2010-06-04 22:14:19 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
|
2010-06-17 00:58:06 +02:00
|
|
|
FRIEND_TEST(MetricsDaemonTest, UserCrashIntervalReporter);
|
2010-05-06 01:06:37 +02:00
|
|
|
|
2010-04-27 20:02:18 +02:00
|
|
|
// The network states (see network_states.h).
|
|
|
|
enum NetworkState {
|
|
|
|
kUnknownNetworkState = -1, // Initial/unknown network state.
|
2010-04-14 22:32:20 +02:00
|
|
|
#define STATE(name, capname) kNetworkState ## capname,
|
|
|
|
#include "network_states.h"
|
|
|
|
kNumberNetworkStates
|
2010-04-27 20:02:18 +02:00
|
|
|
};
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-04-27 20:02:18 +02:00
|
|
|
// The power states (see power_states.h).
|
|
|
|
enum PowerState {
|
|
|
|
kUnknownPowerState = -1, // Initial/unknown power state.
|
|
|
|
#define STATE(name, capname) kPowerState ## capname,
|
|
|
|
#include "power_states.h"
|
|
|
|
kNumberPowerStates
|
|
|
|
};
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-05-04 01:45:37 +02:00
|
|
|
// The user session states (see session_states.h).
|
|
|
|
enum SessionState {
|
|
|
|
kUnknownSessionState = -1, // Initial/unknown user session state.
|
|
|
|
#define STATE(name, capname) kSessionState ## capname,
|
|
|
|
#include "session_states.h"
|
|
|
|
kNumberSessionStates
|
|
|
|
};
|
|
|
|
|
|
|
|
// Data record for aggregating daily usage.
|
|
|
|
class UseRecord {
|
|
|
|
public:
|
|
|
|
UseRecord() : day_(0), seconds_(0) {}
|
|
|
|
int day_;
|
|
|
|
int seconds_;
|
|
|
|
};
|
|
|
|
|
2010-05-06 01:06:37 +02:00
|
|
|
// Metric parameters.
|
|
|
|
static const char kMetricDailyUseTimeName[];
|
|
|
|
static const int kMetricDailyUseTimeMin;
|
|
|
|
static const int kMetricDailyUseTimeMax;
|
|
|
|
static const int kMetricDailyUseTimeBuckets;
|
2010-06-24 21:10:26 +02:00
|
|
|
static const char kMetricKernelCrashIntervalName[];
|
|
|
|
static const int kMetricKernelCrashIntervalMin;
|
|
|
|
static const int kMetricKernelCrashIntervalMax;
|
|
|
|
static const int kMetricKernelCrashIntervalBuckets;
|
2010-05-06 01:06:37 +02:00
|
|
|
static const char kMetricTimeToNetworkDropName[];
|
|
|
|
static const int kMetricTimeToNetworkDropMin;
|
|
|
|
static const int kMetricTimeToNetworkDropMax;
|
|
|
|
static const int kMetricTimeToNetworkDropBuckets;
|
2010-06-17 00:58:06 +02:00
|
|
|
static const char kMetricUserCrashIntervalName[];
|
|
|
|
static const int kMetricUserCrashIntervalMin;
|
|
|
|
static const int kMetricUserCrashIntervalMax;
|
|
|
|
static const int kMetricUserCrashIntervalBuckets;
|
2010-05-06 01:06:37 +02:00
|
|
|
|
2010-05-04 01:45:37 +02:00
|
|
|
// 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];
|
|
|
|
|
|
|
|
// Array of user session states.
|
|
|
|
static const char* kSessionStates_[kNumberSessionStates];
|
|
|
|
|
2010-04-14 22:32:20 +02:00
|
|
|
// Creates the event loop and enters it.
|
|
|
|
void Loop();
|
|
|
|
|
2010-04-27 20:02:18 +02:00
|
|
|
// D-Bus filter callback.
|
|
|
|
static DBusHandlerResult MessageFilter(DBusConnection* connection,
|
|
|
|
DBusMessage* message,
|
|
|
|
void* user_data);
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-04-27 20:02:18 +02:00
|
|
|
// Processes network state change.
|
2010-06-04 22:14:19 +02:00
|
|
|
void NetStateChanged(const char* state_name, base::TimeTicks ticks);
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-04-27 20:02:18 +02:00
|
|
|
// Given the state name, returns the state id.
|
|
|
|
NetworkState LookupNetworkState(const char* state_name);
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-04-27 20:02:18 +02:00
|
|
|
// Processes power state change.
|
2010-06-04 22:14:19 +02:00
|
|
|
void PowerStateChanged(const char* state_name, base::Time now);
|
2010-04-27 20:02:18 +02:00
|
|
|
|
|
|
|
// Given the state name, returns the state id.
|
|
|
|
PowerState LookupPowerState(const char* state_name);
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-05-04 01:45:37 +02:00
|
|
|
// Processes user session state change.
|
2010-06-04 22:14:19 +02:00
|
|
|
void SessionStateChanged(const char* state_name, base::Time now);
|
2010-05-04 01:45:37 +02:00
|
|
|
|
|
|
|
// Given the state name, returns the state id.
|
|
|
|
SessionState LookupSessionState(const char* state_name);
|
|
|
|
|
|
|
|
// Updates the user-active state to |active| and logs the usage data
|
|
|
|
// since the last update. If the user has just become active,
|
|
|
|
// reschedule the daily use monitor for more frequent updates --
|
|
|
|
// this is followed by an exponential back-off (see UseMonitor).
|
2010-06-04 22:14:19 +02:00
|
|
|
// While in active use, this method should be called at intervals no
|
|
|
|
// longer than kUseMonitorIntervalMax otherwise new use time will be
|
|
|
|
// discarded.
|
|
|
|
void SetUserActiveState(bool active, base::Time now);
|
2010-05-04 01:45:37 +02:00
|
|
|
|
|
|
|
// Updates the daily usage file, if necessary, by adding |seconds|
|
|
|
|
// of active use to the |day| since Epoch. If there's usage data for
|
|
|
|
// day in the past in the usage file, that data is sent to UMA and
|
|
|
|
// removed from the file. If there's already usage data for |day| in
|
|
|
|
// the usage file, the |seconds| are accumulated.
|
|
|
|
void LogDailyUseRecord(int day, int seconds);
|
|
|
|
|
2010-06-17 00:58:06 +02:00
|
|
|
// Updates the active use time and logs time between user-space
|
|
|
|
// process crashes.
|
|
|
|
void ProcessUserCrash();
|
|
|
|
|
2010-06-24 21:10:26 +02:00
|
|
|
// Updates the active use time and logs time between kernel crashes.
|
|
|
|
void ProcessKernelCrash();
|
|
|
|
|
|
|
|
// Checks if a kernel crash has been detected and processes if so.
|
|
|
|
// The method assumes that a kernel crash has happened if
|
|
|
|
// |crash_file| exists.
|
|
|
|
void CheckKernelCrash(const std::string& crash_file);
|
|
|
|
|
2010-05-04 01:45:37 +02:00
|
|
|
// Callbacks for the daily use monitor. The daily use monitor uses
|
|
|
|
// LogDailyUseRecord to aggregate current usage data and send it to
|
|
|
|
// UMA, if necessary. It also reschedules itself using an
|
|
|
|
// exponentially bigger interval (up to a certain maximum) -- so
|
|
|
|
// usage is monitored less frequently with longer active use.
|
|
|
|
static gboolean UseMonitorStatic(gpointer data);
|
|
|
|
bool UseMonitor();
|
|
|
|
|
|
|
|
// Schedules or reschedules a daily use monitor for |interval|
|
|
|
|
// seconds from now. |backoff| mode is used by the use monitor to
|
|
|
|
// reschedule itself. If there's a monitor scheduled already and
|
|
|
|
// |backoff| is false, unschedules it first. Doesn't schedule a
|
|
|
|
// monitor for more than kUseMonitorIntervalMax seconds in the
|
|
|
|
// future (see metrics_daemon.cc). Returns true if a new use monitor
|
|
|
|
// was scheduled, false otherwise (note that if |backoff| is false a
|
|
|
|
// new use monitor will always be scheduled).
|
|
|
|
bool ScheduleUseMonitor(int interval, bool backoff);
|
|
|
|
|
|
|
|
// Unschedules a scheduled use monitor, if any.
|
|
|
|
void UnscheduleUseMonitor();
|
|
|
|
|
2010-05-18 20:00:59 +02:00
|
|
|
// Sends a regular (exponential) histogram sample to Chrome for
|
|
|
|
// transport to UMA. See MetricsLibrary::SendToUMA in
|
|
|
|
// metrics_library.h for a description of the arguments.
|
|
|
|
void SendMetric(const std::string& name, int sample,
|
|
|
|
int min, int max, int nbuckets);
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-06-17 00:58:06 +02:00
|
|
|
// TaggedCounter callback to process aggregated daily usage data and
|
|
|
|
// send to UMA.
|
2010-06-11 00:59:53 +02:00
|
|
|
static void DailyUseReporter(void* data, int tag, int count);
|
|
|
|
|
2010-06-17 00:58:06 +02:00
|
|
|
// TaggedCounter callback to process time between user-space process
|
|
|
|
// crashes and send to UMA.
|
|
|
|
static void UserCrashIntervalReporter(void* data, int tag, int count);
|
|
|
|
|
2010-06-24 21:10:26 +02:00
|
|
|
// TaggedCounter callback to process time between kernel crashes and
|
|
|
|
// send to UMA.
|
|
|
|
static void KernelCrashIntervalReporter(void* data, int tag, int count);
|
|
|
|
|
2010-05-06 01:06:37 +02:00
|
|
|
// Test mode.
|
2010-05-04 01:45:37 +02:00
|
|
|
bool testing_;
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-05-12 22:05:45 +02:00
|
|
|
// The metrics library handle.
|
|
|
|
MetricsLibraryInterface* metrics_lib_;
|
|
|
|
|
2010-05-04 01:45:37 +02:00
|
|
|
// Current network state.
|
|
|
|
NetworkState network_state_;
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-06-04 22:14:19 +02:00
|
|
|
// 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.
|
|
|
|
base::TimeTicks network_state_last_;
|
2010-05-04 01:45:37 +02:00
|
|
|
|
|
|
|
// Current power state.
|
|
|
|
PowerState power_state_;
|
|
|
|
|
|
|
|
// Current user session state.
|
|
|
|
SessionState session_state_;
|
|
|
|
|
|
|
|
// Is the user currently active: power is on, user session has
|
|
|
|
// started, screen is not locked.
|
|
|
|
bool user_active_;
|
|
|
|
|
2010-06-17 00:58:06 +02:00
|
|
|
// Timestamps last user active update. Active use time is aggregated
|
|
|
|
// each day before sending to UMA so using time since the epoch as
|
|
|
|
// the timestamp.
|
2010-06-04 22:14:19 +02:00
|
|
|
base::Time user_active_last_;
|
2010-05-04 01:45:37 +02:00
|
|
|
|
2010-06-17 00:58:06 +02:00
|
|
|
// Daily active use time in seconds.
|
2010-06-11 00:59:53 +02:00
|
|
|
scoped_ptr<chromeos_metrics::TaggedCounterInterface> daily_use_;
|
2010-05-04 01:45:37 +02:00
|
|
|
|
2010-06-17 00:58:06 +02:00
|
|
|
// Active use time between user-space process crashes.
|
|
|
|
scoped_ptr<chromeos_metrics::TaggedCounterInterface> user_crash_interval_;
|
|
|
|
|
2010-06-24 21:10:26 +02:00
|
|
|
// Active use time between kernel crashes.
|
|
|
|
scoped_ptr<chromeos_metrics::TaggedCounterInterface> kernel_crash_interval_;
|
|
|
|
|
2010-05-04 01:45:37 +02:00
|
|
|
// Sleep period until the next daily usage aggregation performed by
|
|
|
|
// the daily use monitor (see ScheduleUseMonitor).
|
|
|
|
int usemon_interval_;
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-05-04 01:45:37 +02:00
|
|
|
// Scheduled daily use monitor source (see ScheduleUseMonitor).
|
|
|
|
GSource* usemon_source_;
|
2010-04-14 22:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // METRICS_DAEMON_H_
|