blob: 5f51303d7559f19bc3fd11a63f5c9a1415c33e8a [file] [log] [blame]
// Copyright 2015 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 THERMALD_METRICS_REPORTER_H_
#define THERMALD_METRICS_REPORTER_H_
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "metrics/metrics_library.h"
#include "thermald/key_value_publisher.h"
#include "thermald/temperature_monitor_interface.h"
namespace thermald {
// Class to report the thermal state of the device to UMA.
class MetricsReporter {
public:
// Constructs a metrics reporter which reports the thermal state of the
// device at the specified period. The metrics reporter subscribes to the
// given publisher object to be notified on changes in the thermal output
// values. Currently only changes in the state of a thermal zone are
// processed. MetricsReporter does not take ownership of the output publisher
// and the metrics library, so they must out-live the metrics reporter.
MetricsReporter(const std::string &uma_metric_prefix,
KeyValuePublisher *output_publisher,
MetricsLibraryInterface *metrics_lib,
base::TimeDelta reporting_period);
// Adds a temperature to be included in the reports of the thermal state.
// The metrics reporter subscribes to the given temperature monitor to be
// notified on temperature changes.
void AddTemperature(const std::string &name,
TemperatureMonitorInterface *temperature_monitor);
// Starts the periodic reporting of the thermal state.
void Start();
// Stops the periodic reporting of the thermal state.
void Stop();
private:
friend class MetricsReporterTest;
// Structure with the current value of a monitored temperature and the name
// of the UMA histogram for this temperature.
struct TemperatureMetricsData {
int current_value;
std::string histogram;
};
// Structure with the current state of a thermal zone and the name of the
// UMA histogram for this zone.
struct ThermalStateMetricsData {
int current_state;
std::string histogram;
};
void ReportMetrics();
void OnOutputChanged(const std::string &key, int value);
void OnTemperatureChanged(const std::string &name, int value);
// Histogram prefix used to consolidate the UMA metrics for new products in
// the backend. If histogram prefix is not configured, then default prefix
// will be "Platform".
const std::string histogram_prefix_;
// Map with the current values of the monitored temperatures and the names
// of the corresponding histograms.
std::map<std::string, TemperatureMetricsData> temperature_metrics_data_;
// Map with the current state of the thermal zones and the name of the
// corresponding histograms.
std::map<std::string, ThermalStateMetricsData> thermal_state_metrics_data_;
MetricsLibraryInterface *metrics_lib_;
const base::TimeDelta reporting_period_;
std::unique_ptr<base::RepeatingTimer> timer_;
#if BASE_VER < 860220
std::unique_ptr<KeyValuePublisher::Subscription> subscription_outputs_;
std::vector<std::unique_ptr<TemperatureMonitorInterface::Subscription>>
temperature_monitor_subscriptions_;
#else
base::CallbackListSubscription subscription_outputs_;
std::vector<base::CallbackListSubscription>
temperature_monitor_subscriptions_;
#endif
base::WeakPtrFactory<MetricsReporter> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(MetricsReporter);
};
} // namespace thermald
#endif // THERMALD_METRICS_REPORTER_H_