Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 5 | #ifndef METRICS_LIBRARY_H_ |
| 6 | #define METRICS_LIBRARY_H_ |
| 7 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 8 | #include <sys/types.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 11 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 12 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 13 | class MetricsLibraryInterface { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 14 | public: |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 15 | 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. |
| 23 | class MetricsLibrary : public MetricsLibraryInterface { |
| 24 | public: |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 25 | MetricsLibrary(); |
| 26 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 27 | // Initializes the library. |
| 28 | void Init(); |
| 29 | |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 30 | // Sends histogram data to Chrome for transport to UMA and returns |
| 31 | // true on success. This method results in the equivalent of an |
| 32 | // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS |
| 33 | // inside Chrome (see base/histogram.h). |
| 34 | // |
| 35 | // |sample| is the sample value to be recorded (|min| <= |sample| < |max|). |
| 36 | // |min| is the minimum value of the histogram samples (|min| > 0). |
| 37 | // |max| is the maximum value of the histogram samples. |
| 38 | // |nbuckets| is the number of histogram buckets. |
| 39 | // [0,min) is the implicit underflow bucket. |
| 40 | // [|max|,infinity) is the implicit overflow bucket. |
Darin Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame^] | 41 | // |
| 42 | // Note that the memory allocated in Chrome for each histogram is |
| 43 | // proportional to the number of buckets. Therefore, it is strongly |
| 44 | // recommended to keep this number low (e.g., 50 is normal, while |
| 45 | // 100 is high). |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 46 | bool SendToUMA(const std::string& name, int sample, |
| 47 | int min, int max, int nbuckets); |
| 48 | |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 49 | // Sends linear histogram data to Chrome for transport to UMA and |
| 50 | // returns true on success. This method results in the equivalent of |
| 51 | // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION |
| 52 | // inside Chrome (see base/histogram.h). |
| 53 | // |
| 54 | // |sample| is the sample value to be recorded (1 <= |sample| < |max|). |
| 55 | // |max| is the maximum value of the histogram samples. |
| 56 | // 0 is the implicit underflow bucket. |
| 57 | // [|max|,infinity) is the implicit overflow bucket. |
Darin Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame^] | 58 | // |
| 59 | // An enumaration histogram requires |max| + 1 number of |
| 60 | // buckets. Note that the memory allocated in Chrome for each |
| 61 | // histogram is proportional to the number of buckets. Therefore, it |
| 62 | // is strongly recommended to keep this number low (e.g., 50 is |
| 63 | // normal, while 100 is high). |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 64 | bool SendEnumToUMA(const std::string& name, int sample, int max); |
| 65 | |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 66 | // Sends to Autotest and returns true on success. |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 67 | static bool SendToAutotest(const std::string& name, int value); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 68 | |
| 69 | private: |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 70 | friend class CMetricsLibraryTest; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 71 | friend class MetricsLibraryTest; |
| 72 | FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage); |
| 73 | FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong); |
| 74 | FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome); |
| 75 | FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation); |
| 76 | |
| 77 | // Sends message of size |length| to Chrome for transport to UMA and |
| 78 | // returns true on success. |
| 79 | bool SendMessageToChrome(int32_t length, const char* message); |
| 80 | |
| 81 | // Formats a name/value message for Chrome in |buffer| and returns the |
| 82 | // length of the message or a negative value on error. |
| 83 | // |
| 84 | // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 | |
| 85 | // |
| 86 | // The arbitrary |format| argument covers the non-LENGTH portion of the |
| 87 | // message. The caller is responsible to store the \0 character |
| 88 | // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value). |
| 89 | int32_t FormatChromeMessage(int32_t buffer_size, char* buffer, |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 90 | const char* format, ...); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 91 | |
| 92 | const char* uma_events_file_; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 95 | #endif // METRICS_LIBRARY_H_ |