OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef METRICS_DAEMON_H_ | 5 #ifndef METRICS_DAEMON_H_ |
6 #define METRICS_DAEMON_H_ | 6 #define METRICS_DAEMON_H_ |
7 | 7 |
8 #include <dbus/dbus-glib.h> | 8 #include <dbus/dbus.h> |
9 #include <sys/time.h> | |
10 #include <time.h> | 9 #include <time.h> |
11 | 10 |
12 class MetricsDaemon { | 11 class MetricsDaemon { |
13 | 12 |
14 public: | 13 public: |
15 MetricsDaemon() | 14 MetricsDaemon() |
16 : network_state_id_(kUnknownNetworkStateId) { | 15 : testing_(false), |
17 } | 16 network_state_(kUnknownNetworkState), |
| 17 network_state_changed_(0), |
| 18 power_state_(kUnknownPowerState) {} |
18 ~MetricsDaemon() {} | 19 ~MetricsDaemon() {} |
19 | 20 |
20 // Does all the work. If |run_as_daemon| is true, daemonize by forking. If | 21 // Does all the work. If |run_as_daemon| is true, daemonizes by |
21 // |testing| is true, log the stats instead of sending them to Chrome. | 22 // forking. If |testing| is true, logs the stats instead of sending |
| 23 // them to Chrome. |
22 void Run(bool run_as_daemon, bool testing); | 24 void Run(bool run_as_daemon, bool testing); |
23 | 25 |
24 private: | 26 private: |
25 // Shared with Chrome for transport. | 27 // The network states (see network_states.h). |
26 static const char* kMetricsFilePath; | 28 enum NetworkState { |
27 static const int kMetricsMessageMaxLength = 4096; | 29 kUnknownNetworkState = -1, // Initial/unknown network state. |
28 | |
29 // The network states. See network_states.h. | |
30 typedef enum { | |
31 // Initial/unknown network state id. | |
32 kUnknownNetworkStateId = -1, | |
33 #define STATE(name, capname) kNetworkState ## capname, | 30 #define STATE(name, capname) kNetworkState ## capname, |
34 #include "network_states.h" | 31 #include "network_states.h" |
35 kNumberNetworkStates | 32 kNumberNetworkStates |
36 } NetworkStateId; | 33 }; |
37 | 34 |
38 typedef struct { | 35 // The power states (see power_states.h). |
39 const char* name; | 36 enum PowerState { |
40 const char* stat_name; | 37 kUnknownPowerState = -1, // Initial/unknown power state. |
41 } NetworkState; | 38 #define STATE(name, capname) kPowerState ## capname, |
| 39 #include "power_states.h" |
| 40 kNumberPowerStates |
| 41 }; |
42 | 42 |
43 // Initializes. | 43 // Initializes. |
44 void Init(bool testing); | 44 void Init(bool testing); |
45 | 45 |
46 // Creates the event loop and enters it. | 46 // Creates the event loop and enters it. |
47 void Loop(); | 47 void Loop(); |
48 | 48 |
49 // Static callback for network events on DBus. | 49 // D-Bus filter callback. |
50 static void StaticNetSignalHandler(::DBusGProxy* proxy, const char* property, | 50 static DBusHandlerResult MessageFilter(DBusConnection* connection, |
51 const ::GValue* value, void* data); | 51 DBusMessage* message, |
| 52 void* user_data); |
52 | 53 |
53 // Callback for network events on DBus. | 54 // Processes network state change. |
54 void NetSignalHandler(::DBusGProxy* proxy, const char* property, | 55 void NetStateChanged(const char* state_name); |
55 const ::GValue* value); | |
56 | 56 |
57 // This is called at each network state change. The new state is identified | 57 // Given the state name, returns the state id. |
58 // by the string @newstate. As a side effect, this method ships to Chrome | 58 NetworkState LookupNetworkState(const char* state_name); |
59 // (or prints to stdout when testing) the name and duration of the state | |
60 // that has ended. | |
61 void LogNetworkStateChange(const char* newstate); | |
62 | 59 |
63 // Given a string with the name of a state, returns the id for the state. | 60 // Processes power state change. |
64 NetworkStateId GetNetworkStateId(const char* state_name); | 61 void PowerStateChanged(const char* state_name); |
| 62 |
| 63 // Given the state name, returns the state id. |
| 64 PowerState LookupPowerState(const char* state_name); |
65 | 65 |
66 // Sends a stat to Chrome for transport to UMA (or prints it for | 66 // Sends a stat to Chrome for transport to UMA (or prints it for |
67 // testing). See MetricsLibrary::SendToChrome in metrics_library.h | 67 // testing). See MetricsLibrary::SendToChrome in metrics_library.h |
68 // for a description of the arguments. | 68 // for a description of the arguments. |
69 void PublishMetric(const char* name, int sample, | 69 void PublishMetric(const char* name, int sample, |
70 int min, int max, int nbuckets); | 70 int min, int max, int nbuckets); |
71 | 71 |
72 #if 0 | 72 // D-Bus message match strings. |
73 // Fetches a name-value hash table from DBus. | 73 static const char* dbus_matches_[]; |
74 bool GetProperties(::DBusGProxy* proxy, ::GHashTable** table); | |
75 | 74 |
76 // The type descriptor for a glib hash table. | 75 // Array of network states. |
77 GType hashtable_gtype; | 76 static const char* network_states_[kNumberNetworkStates]; |
78 #endif | |
79 | 77 |
80 // Array of network states of interest. | 78 // Array of power states. |
81 static NetworkState network_states_[kNumberNetworkStates]; | 79 static const char* power_states_[kNumberPowerStates]; |
82 | 80 |
83 bool testing_; // just testing | 81 bool testing_; // just testing |
84 NetworkStateId network_state_id_; // id of current state | 82 NetworkState network_state_; // current network state |
85 struct timeval network_state_start_; // when current state was entered | 83 time_t network_state_changed_; // timestamp last net state change |
| 84 PowerState power_state_; // current power state |
86 }; | 85 }; |
87 | 86 |
88 #endif // METRICS_DAEMON_H_ | 87 #endif // METRICS_DAEMON_H_ |
OLD | NEW |