blob: 52da94d27a5cb122f6c91c68aa2969422df53d17 [file] [log] [blame]
Darin Petkov65b01462010-04-14 13:32:20 -07001// 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
3// found in the LICENSE file.
4
Darin Petkov65b01462010-04-14 13:32:20 -07005#ifndef METRICS_LIBRARY_H_
6#define METRICS_LIBRARY_H_
7
Darin Petkov11b8eb32010-05-18 11:00:59 -07008#include <sys/types.h>
Darin Petkov65b01462010-04-14 13:32:20 -07009#include <string>
10
Darin Petkov11b8eb32010-05-18 11:00:59 -070011#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov65b01462010-04-14 13:32:20 -070012
Darin Petkovfc91b422010-05-12 13:05:45 -070013class MetricsLibraryInterface {
Darin Petkov65b01462010-04-14 13:32:20 -070014 public:
Darin Petkovfc91b422010-05-12 13:05:45 -070015 virtual void Init() = 0;
16 virtual bool SendToUMA(const std::string& name, int sample,
17 int min, int max, int nbuckets) = 0;
18 virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0;
19 virtual ~MetricsLibraryInterface() {}
20};
21
22// Library used to send metrics to both Autotest and Chrome/UMA.
23class MetricsLibrary : public MetricsLibraryInterface {
24 public:
Darin Petkov11b8eb32010-05-18 11:00:59 -070025 MetricsLibrary();
26
Darin Petkovfc91b422010-05-12 13:05:45 -070027 // Initializes the library.
28 void Init();
29
Ken Mixter4c5daa42010-08-26 18:35:06 -070030 // Returns whether or not metrics collection is enabled.
31 bool AreMetricsEnabled();
32
Darin Petkovc2526a12010-04-21 14:24:04 -070033 // Sends histogram data to Chrome for transport to UMA and returns
34 // true on success. This method results in the equivalent of an
35 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS
36 // inside Chrome (see base/histogram.h).
37 //
38 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|).
39 // |min| is the minimum value of the histogram samples (|min| > 0).
40 // |max| is the maximum value of the histogram samples.
41 // |nbuckets| is the number of histogram buckets.
42 // [0,min) is the implicit underflow bucket.
43 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070044 //
45 // Note that the memory allocated in Chrome for each histogram is
46 // proportional to the number of buckets. Therefore, it is strongly
47 // recommended to keep this number low (e.g., 50 is normal, while
48 // 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070049 bool SendToUMA(const std::string& name, int sample,
50 int min, int max, int nbuckets);
51
Darin Petkov5b7dce12010-04-21 15:45:10 -070052 // Sends linear histogram data to Chrome for transport to UMA and
53 // returns true on success. This method results in the equivalent of
54 // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION
55 // inside Chrome (see base/histogram.h).
56 //
57 // |sample| is the sample value to be recorded (1 <= |sample| < |max|).
58 // |max| is the maximum value of the histogram samples.
59 // 0 is the implicit underflow bucket.
60 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070061 //
62 // An enumaration histogram requires |max| + 1 number of
63 // buckets. Note that the memory allocated in Chrome for each
64 // histogram is proportional to the number of buckets. Therefore, it
65 // is strongly recommended to keep this number low (e.g., 50 is
66 // normal, while 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070067 bool SendEnumToUMA(const std::string& name, int sample, int max);
68
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070069 // Sends to Autotest and returns true on success.
Darin Petkovc2526a12010-04-21 14:24:04 -070070 static bool SendToAutotest(const std::string& name, int value);
Darin Petkov11b8eb32010-05-18 11:00:59 -070071
72 private:
Sam Leffler10b301d2010-06-17 14:22:43 -070073 friend class CMetricsLibraryTest;
Darin Petkov11b8eb32010-05-18 11:00:59 -070074 friend class MetricsLibraryTest;
Ken Mixter4c5daa42010-08-26 18:35:06 -070075 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
Darin Petkov11b8eb32010-05-18 11:00:59 -070076 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
77 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
78 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
79 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
80
81 // Sends message of size |length| to Chrome for transport to UMA and
82 // returns true on success.
83 bool SendMessageToChrome(int32_t length, const char* message);
84
85 // Formats a name/value message for Chrome in |buffer| and returns the
86 // length of the message or a negative value on error.
87 //
88 // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 |
89 //
90 // The arbitrary |format| argument covers the non-LENGTH portion of the
91 // message. The caller is responsible to store the \0 character
92 // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value).
93 int32_t FormatChromeMessage(int32_t buffer_size, char* buffer,
David James3b3add52010-06-04 15:01:19 -070094 const char* format, ...);
Darin Petkov11b8eb32010-05-18 11:00:59 -070095
Ken Mixter4c5daa42010-08-26 18:35:06 -070096 // Time at which we last checked if metrics were enabled.
97 static time_t cached_enabled_time_;
98
99 // Cached state of whether or not metrics were enabled.
100 static bool cached_enabled_;
101
Darin Petkov11b8eb32010-05-18 11:00:59 -0700102 const char* uma_events_file_;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700103 const char* consent_file_;
Darin Petkov65b01462010-04-14 13:32:20 -0700104};
105
David James3b3add52010-06-04 15:01:19 -0700106#endif // METRICS_LIBRARY_H_