Create two new metric types to simplify metrics.
This is part one of a mult-part change to revise some metrics. Several metrics are currently EventMetric type when they should be a simpler type. Test: Added unit tests for the new types. Also, re-ran existing tests. Verified playback works with Google Play, and re-ran Widevine GTS tests. Bug: 36220619 Change-Id: I2ec8fc355f66ad4834dd722aacd22541fb9c94ad
This commit is contained in:
205
libwvdrmengine/cdm/metrics/include/counter_metric.h
Normal file
205
libwvdrmengine/cdm/metrics/include/counter_metric.h
Normal file
@@ -0,0 +1,205 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the declarations for the Metric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_COUNTER_METRIC_H_
|
||||
#define WVCDM_METRICS_COUNTER_METRIC_H_
|
||||
|
||||
#include <cstdarg>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "field_tuples.h"
|
||||
#include "lock.h"
|
||||
#include "metric_serialization.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
class CounterMetricTest;
|
||||
|
||||
// This base class provides the common defintion used by all templated
|
||||
// instances of CounterMetric.
|
||||
class BaseCounterMetric : public MetricSerializable {
|
||||
public:
|
||||
// Send metric values to the MetricSerializer. |serializer| must
|
||||
// not be null and is owned by the caller.
|
||||
virtual void Serialize(MetricSerializer* serializer);
|
||||
|
||||
protected:
|
||||
// Instantiates a BaseCounterMetric.
|
||||
BaseCounterMetric(const std::string& metric_name)
|
||||
: metric_name_(metric_name) {}
|
||||
|
||||
virtual ~BaseCounterMetric() {}
|
||||
|
||||
// Increment will look for an existing instance of the field names and
|
||||
// add the new value to the existing value. If the instance does not exist,
|
||||
// this method will create it.
|
||||
void Increment(const std::string& field_names_values, int64_t value);
|
||||
|
||||
private:
|
||||
friend class CounterMetricTest;
|
||||
const std::string metric_name_;
|
||||
// value_map_ contains a mapping from the field name/value pairs to the
|
||||
// counter(int64_t) instance.
|
||||
std::map<std::string, int64_t> value_map_;
|
||||
|
||||
/*
|
||||
* This locks the internal state of the counter metric to ensure safety across
|
||||
* multiple threads preventing the caller from worrying about locking.
|
||||
*/
|
||||
Lock internal_lock_;
|
||||
};
|
||||
|
||||
// The CounterMetric class is used to track a counter such as the number of
|
||||
// times a method is called. It can also track a delta that could be positive
|
||||
// or negative.
|
||||
//
|
||||
// The CounterMetric class supports the ability to keep track of multiple
|
||||
// variations of the count based on certain "field" values. E.g. keep track of
|
||||
// the counts of success and failure counts for a method call. Or keep track of
|
||||
// number of times the method was called with a particular parameter.
|
||||
// Fields define what variations to track. Each Field is a separate dimension.
|
||||
// The count for each combination of field values is tracked independently.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// CounterMetric<int, int> my_metric("multiple/fields/metric",
|
||||
// "request_type", // Field name.
|
||||
// "error_code"); // Field name.
|
||||
//
|
||||
// my_metric.Increment(1, 7, 23); // (counter value, request type, error code).
|
||||
//
|
||||
// The CounterMetric supports serialization. A call to Serialize will serialize
|
||||
// all values to the provided MetricsSerializer instance.
|
||||
//
|
||||
// example:
|
||||
//
|
||||
// class MyMetricSerializer : public MetricSerializer {
|
||||
// // Add implementation here.
|
||||
// }
|
||||
//
|
||||
// MyMetricSerializer serializer;
|
||||
// my_metric.Serialize(&serializer);
|
||||
template<typename F1=util::Unused,
|
||||
typename F2=util::Unused,
|
||||
typename F3=util::Unused,
|
||||
typename F4=util::Unused>
|
||||
class CounterMetric : public BaseCounterMetric {
|
||||
public:
|
||||
// Create a CounterMetric instance with the name |metric_name|.
|
||||
CounterMetric(const std::string& metric_name);
|
||||
|
||||
// Overloaded constructors with variable field name arguments. The number
|
||||
// of |field_name| arguments must match the number of used Field type
|
||||
// arguments.
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name);
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2);
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3);
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3,
|
||||
const char* field_name4);
|
||||
|
||||
// Increment will update the counter value associated with the provided
|
||||
// field values.
|
||||
void Increment(F1 field1 = util::Unused(), F2 field2 = util::Unused(),
|
||||
F3 field3 = util::Unused(), F4 field4 = util::Unused());
|
||||
|
||||
// Increment will add the value to the counter associated with the provided
|
||||
// field values.
|
||||
void Increment(int64_t value,
|
||||
F1 field1 = util::Unused(), F2 field2 = util::Unused(),
|
||||
F3 field3 = util::Unused(), F4 field4 = util::Unused());
|
||||
|
||||
private:
|
||||
friend class CounterMetricTest;
|
||||
std::vector<std::string> field_names_;
|
||||
};
|
||||
|
||||
// Overloaded template constructor implementations for CounterMetric.
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(1);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(2);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(3);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(4);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2, field_name3);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3,
|
||||
const char* field_name4)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(5);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2,
|
||||
field_name3, field_name4);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
void CounterMetric<F1, F2, F3, F4>::Increment(
|
||||
F1 field1, F2 field2, F3 field3, F4 field4) {
|
||||
std::string field_name_values =
|
||||
util::MakeFieldNameString(field_names_, field1, field2, field3, field4);
|
||||
BaseCounterMetric::Increment(field_name_values, 1);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
void CounterMetric<F1, F2, F3, F4>::Increment(
|
||||
int64_t value, F1 field1, F2 field2, F3 field3, F4 field4) {
|
||||
std::string field_name_values =
|
||||
util::MakeFieldNameString(field_names_, field1, field2, field3, field4);
|
||||
BaseCounterMetric::Increment(field_name_values, value);
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_COUNTER_METRIC_H_
|
||||
@@ -6,18 +6,13 @@
|
||||
#define WVCDM_METRICS_EVENT_METRIC_H_
|
||||
|
||||
#include <cstdarg>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "lock.h"
|
||||
|
||||
#include "distribution.h"
|
||||
#include "field_tuples.h"
|
||||
#include "lock.h"
|
||||
#include "metric_serialization.h"
|
||||
|
||||
namespace wvcdm {
|
||||
@@ -60,14 +55,6 @@ class BaseEventMetric : public MetricSerializable {
|
||||
Lock internal_lock_;
|
||||
};
|
||||
|
||||
// This is a placeholder type for unused type parameters.
|
||||
struct Unused {
|
||||
// Required for compilation. Should never be used.
|
||||
inline friend std::ostream& operator<< (std::ostream& out, const Unused&)
|
||||
{ return out; }
|
||||
};
|
||||
|
||||
|
||||
// This class converts the size_t value into the highest power of two
|
||||
// below the value. E.g. for 7, the value is 4. For 11, the value is 8.
|
||||
// This class is intended to simplify the use of EventMetric Fields that may
|
||||
@@ -113,7 +100,7 @@ class Pow2Bucket {
|
||||
// on certain "field" values. For example, if a particular operation can run in
|
||||
// one of two modes, it's useful to track the latency of the operation in each
|
||||
// mode separately. You can use Fields to define how to breakdown the
|
||||
// statistics. Each Field is a separate dimention. The statistics for each
|
||||
// statistics. Each Field is a separate dimension. The statistics for each
|
||||
// combination of field values are tracked independently.
|
||||
//
|
||||
// Example usage:
|
||||
@@ -123,19 +110,22 @@ class Pow2Bucket {
|
||||
//
|
||||
// my_metric.Record(1, 7, 23); // (latency value, request type, error code).
|
||||
//
|
||||
// A MetricNotification may be used to allow the EventMetric instance to
|
||||
// notify that an update occurred and provide the updated value.
|
||||
// The EventMetric supports serialization. A call to Serialize will
|
||||
// serialize all values to the provided MetricsSerializer instance.
|
||||
//
|
||||
// class MyMetricNotification : public MetricNotification {
|
||||
// example:
|
||||
//
|
||||
// class MyMetricSerializer : public MetricSerializer {
|
||||
// // Add implementation here.
|
||||
// }
|
||||
//
|
||||
// MyMetricNotification notification;
|
||||
// my_metric.Publish(notification);
|
||||
template<typename F1=Unused,
|
||||
typename F2=Unused,
|
||||
typename F3=Unused,
|
||||
typename F4=Unused>
|
||||
// MyMetricSerializer serializer;
|
||||
// my_metric.Serialize(&serializer);
|
||||
//
|
||||
template<typename F1=util::Unused,
|
||||
typename F2=util::Unused,
|
||||
typename F3=util::Unused,
|
||||
typename F4=util::Unused>
|
||||
class EventMetric : public BaseEventMetric {
|
||||
public:
|
||||
// Create an EventMetric instance with the name |metric_name|.
|
||||
@@ -162,116 +152,16 @@ class EventMetric : public BaseEventMetric {
|
||||
// Record will update the statistics of the EventMetric broken down by the
|
||||
// given field values.
|
||||
void Record(double value,
|
||||
F1 field1 = Unused(),
|
||||
F2 field2 = Unused(),
|
||||
F3 field3 = Unused(),
|
||||
F4 field4 = Unused());
|
||||
F1 field1 = util::Unused(),
|
||||
F2 field2 = util::Unused(),
|
||||
F3 field3 = util::Unused(),
|
||||
F4 field4 = util::Unused());
|
||||
|
||||
private:
|
||||
friend class EventMetricTest;
|
||||
std::vector<std::string> field_names_;
|
||||
};
|
||||
|
||||
// This is an internal namespace for helper functions only.
|
||||
namespace impl {
|
||||
|
||||
// This method formats the collection of field name/value pairs.
|
||||
// The format of the string is:
|
||||
//
|
||||
// [{field:value[&field:value]*}]
|
||||
//
|
||||
// If there are no pairs, returns a blank string.
|
||||
//
|
||||
// TODO(blueeyes): Add a check for the count of field_names and the count
|
||||
// of field values (check that the fields are not type Unused).
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
std::string MakeFieldNameString(const std::vector<std::string>& field_names,
|
||||
const F1 field1, const F2 field2,
|
||||
const F3 field3, const F4 field4) {
|
||||
std::stringstream field_name_and_values;
|
||||
std::vector<std::string>::const_iterator field_name_iterator =
|
||||
field_names.begin();
|
||||
if (field_name_iterator == field_names.end()) {
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
// There is at least one name/value pair. prepend open brace.
|
||||
field_name_and_values << "{";
|
||||
field_name_and_values << *field_name_iterator << ':' << field1;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field2;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field3;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field4;
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
|
||||
// This specialization of the helper method is a shortcut for EventMetric
|
||||
// instances with no fields.
|
||||
template<>
|
||||
inline std::string MakeFieldNameString<Unused, Unused, Unused, Unused>(
|
||||
const std::vector<std::string>& /* field_names */,
|
||||
const Unused /* unused1 */, const Unused /* unused2 */,
|
||||
const Unused /* unused3 */, const Unused /* unused4 */) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// This helper function appends the field names to a vector of strings.
|
||||
inline void AppendFieldNames(std::vector<std::string>* field_name_vector,
|
||||
int field_count, ...) {
|
||||
va_list field_names;
|
||||
|
||||
va_start(field_names, field_count);
|
||||
for (int x = 0; x < field_count; x++) {
|
||||
field_name_vector->push_back(va_arg(field_names, const char*));
|
||||
}
|
||||
va_end(field_names);
|
||||
}
|
||||
|
||||
// These helper methods and FirstUnusedType assure that there is no mismatch
|
||||
// between the specified types for EventMetrics and the constructors and
|
||||
// methods used for the specializations.
|
||||
template <bool>
|
||||
struct CompileAssert {};
|
||||
#define COMPILE_ASSERT(expr, msg) \
|
||||
typedef impl::CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
|
||||
|
||||
template <typename T> struct is_unused { static const bool value = false; };
|
||||
template <> struct is_unused<Unused> { static const bool value = true; };
|
||||
|
||||
template <typename F1, typename F2, typename F3, typename F4>
|
||||
class FirstUnusedType {
|
||||
static const bool a = is_unused<F1>::value;
|
||||
static const bool b = is_unused<F2>::value;
|
||||
static const bool c = is_unused<F3>::value;
|
||||
static const bool d = is_unused<F4>::value;
|
||||
// Check that all types after the first Unused are also Unused.
|
||||
COMPILE_ASSERT(a <= b, Invalid_Unused_At_Position_2);
|
||||
COMPILE_ASSERT(b <= c, Invalid_Unused_At_Position_3);
|
||||
COMPILE_ASSERT(c <= d, Invalid_Unused_At_Position_4);
|
||||
|
||||
public:
|
||||
static const int value = 5 - (a + b + c + d);
|
||||
};
|
||||
|
||||
// Asserts that no Unused types exist before N; after N, are all Unused types.
|
||||
#define ASSERT_METRIC_UNUSED_START_FROM(N) \
|
||||
COMPILE_ASSERT((\
|
||||
impl::FirstUnusedType<F1, F2, F3, F4>::value) == N, \
|
||||
Unused_Start_From_##N)
|
||||
|
||||
} // namespace impl
|
||||
|
||||
// Overloaded template constructor implementations for EventMetric.
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
@@ -286,8 +176,8 @@ EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const char* field_name)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(2);
|
||||
impl::AppendFieldNames(&field_names_,
|
||||
impl::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
@@ -297,8 +187,8 @@ EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const char* field_name2)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(3);
|
||||
impl::AppendFieldNames(&field_names_,
|
||||
impl::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
@@ -309,8 +199,8 @@ EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const char* field_name3)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(4);
|
||||
impl::AppendFieldNames(&field_names_,
|
||||
impl::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2, field_name3);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
@@ -322,8 +212,8 @@ EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const char* field_name4)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(5);
|
||||
impl::AppendFieldNames(&field_names_,
|
||||
impl::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2,
|
||||
field_name3, field_name4);
|
||||
}
|
||||
@@ -332,7 +222,7 @@ template<typename F1, typename F2, typename F3, typename F4>
|
||||
void EventMetric<F1, F2, F3, F4>::Record(
|
||||
double value, F1 field1, F2 field2, F3 field3, F4 field4) {
|
||||
std::string field_name_values =
|
||||
impl::MakeFieldNameString(field_names_, field1, field2, field3, field4);
|
||||
util::MakeFieldNameString(field_names_, field1, field2, field3, field4);
|
||||
BaseEventMetric::Record(field_name_values, value);
|
||||
}
|
||||
|
||||
|
||||
125
libwvdrmengine/cdm/metrics/include/field_tuples.h
Normal file
125
libwvdrmengine/cdm/metrics/include/field_tuples.h
Normal file
@@ -0,0 +1,125 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the helper classes and methods for using field tuples
|
||||
// used by metrics classes to record variations of a single metric.
|
||||
#ifndef WVCDM_METRICS_FIELD_TUPLES_H_
|
||||
#define WVCDM_METRICS_FIELD_TUPLES_H_
|
||||
|
||||
#include <cstdarg>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
namespace util {
|
||||
|
||||
// This is a placeholder type for unused type parameters. It aids in supporting
|
||||
// templated classes with "variable" type arguments.
|
||||
struct Unused {
|
||||
// Required for compilation. Should never be used.
|
||||
inline friend std::ostream& operator<< (std::ostream& out, const Unused&)
|
||||
{ return out; }
|
||||
};
|
||||
|
||||
// This utility method formats the collection of field name/value pairs.
|
||||
// The format of the string is:
|
||||
//
|
||||
// [{field:value[&field:value]*}]
|
||||
//
|
||||
// If there are no pairs, returns a blank string.
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
std::string MakeFieldNameString(const std::vector<std::string>& field_names,
|
||||
const F1 field1, const F2 field2,
|
||||
const F3 field3, const F4 field4) {
|
||||
std::stringstream field_name_and_values;
|
||||
std::vector<std::string>::const_iterator field_name_iterator =
|
||||
field_names.begin();
|
||||
if (field_name_iterator == field_names.end()) {
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
// There is at least one name/value pair. Prepend open brace.
|
||||
field_name_and_values << "{";
|
||||
field_name_and_values << *field_name_iterator << ':' << field1;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field2;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field3;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field4;
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
|
||||
// This specialization of the helper method is a shortcut for class
|
||||
// instances with no fields.
|
||||
template<>
|
||||
inline std::string MakeFieldNameString<Unused, Unused, Unused, Unused>(
|
||||
const std::vector<std::string>& /* field_names */,
|
||||
const Unused /* unused1 */, const Unused /* unused2 */,
|
||||
const Unused /* unused3 */, const Unused /* unused4 */) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// This helper function appends the field names to a vector of strings.
|
||||
inline void AppendFieldNames(std::vector<std::string>* field_name_vector,
|
||||
int field_count, ...) {
|
||||
va_list field_names;
|
||||
|
||||
va_start(field_names, field_count);
|
||||
for (int x = 0; x < field_count; x++) {
|
||||
field_name_vector->push_back(va_arg(field_names, const char*));
|
||||
}
|
||||
va_end(field_names);
|
||||
}
|
||||
|
||||
// These helper methods and FirstUnusedType assure that there is no mismatch
|
||||
// between the specified types for metrics type parameters and the constructors
|
||||
// and methods used for the specializations.
|
||||
template <bool>
|
||||
struct CompileAssert {};
|
||||
#define COMPILE_ASSERT(expr, msg) \
|
||||
typedef util::CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
|
||||
|
||||
template <typename T> struct is_unused { static const bool value = false; };
|
||||
template <> struct is_unused<Unused> { static const bool value = true; };
|
||||
|
||||
template <typename F1, typename F2, typename F3, typename F4>
|
||||
class FirstUnusedType {
|
||||
static const bool a = is_unused<F1>::value;
|
||||
static const bool b = is_unused<F2>::value;
|
||||
static const bool c = is_unused<F3>::value;
|
||||
static const bool d = is_unused<F4>::value;
|
||||
// Check that all types after the first Unused are also Unused.
|
||||
COMPILE_ASSERT(a <= b, Invalid_Unused_At_Position_2);
|
||||
COMPILE_ASSERT(b <= c, Invalid_Unused_At_Position_3);
|
||||
COMPILE_ASSERT(c <= d, Invalid_Unused_At_Position_4);
|
||||
|
||||
public:
|
||||
static const int value = 5 - (a + b + c + d);
|
||||
};
|
||||
|
||||
// Asserts that no Unused types exist before N; after N, are all Unused types.
|
||||
#define ASSERT_METRIC_UNUSED_START_FROM(N) \
|
||||
COMPILE_ASSERT((\
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value) == N, \
|
||||
Unused_Start_From_##N)
|
||||
|
||||
} // namespace util
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_FIELD_TUPLES_H_
|
||||
|
||||
100
libwvdrmengine/cdm/metrics/include/value_metric.h
Normal file
100
libwvdrmengine/cdm/metrics/include/value_metric.h
Normal file
@@ -0,0 +1,100 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the declarations for the Metric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_VALUE_METRIC_H_
|
||||
#define WVCDM_METRICS_VALUE_METRIC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
#include "metric_serialization.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// Private namespace for some helper implementation functions.
|
||||
namespace impl {
|
||||
|
||||
// These helper functions map the templated ValueMetric class
|
||||
// Serialize call to the MetricSerializer explicit calls.
|
||||
template<typename T>
|
||||
void Serialize(MetricSerializer* serializer,
|
||||
const std::string& metric_name, const T& t);
|
||||
|
||||
inline void SerializeError(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const int& error_code) {
|
||||
serializer->SetInt32(metric_name + "/error", error_code);
|
||||
}
|
||||
|
||||
} // namespace impl
|
||||
|
||||
// The Metric class supports storing a single value which can be overwritten.
|
||||
// the Metric class also supports the MetricSerializer interface through
|
||||
// which the value can be serialized. If the value was never given a value
|
||||
// or an error code, then the metric will not serialize anything.
|
||||
//
|
||||
// Example Usage:
|
||||
// Metric<string> cdm_version("drm/cdm/version")
|
||||
// .Record("a.b.c.d");
|
||||
//
|
||||
// MyMetricSerializerImpl serialzer;
|
||||
// cdm_version.Serialize(&serializer);
|
||||
//
|
||||
// Example Error Usage:
|
||||
//
|
||||
// Metric<string> cdm_version("drm/cdm/version")
|
||||
// .SetError(error_code);
|
||||
//
|
||||
// Note that serialization is the same. But the ValueMetric will serialize
|
||||
// the error code to <metric_name>/error instead of just <metric_name>.
|
||||
template<typename T>
|
||||
class ValueMetric : public MetricSerializable {
|
||||
public:
|
||||
// Constructs a metric with the given metric name.
|
||||
explicit ValueMetric(const std::string& metric_name)
|
||||
: metric_name_(metric_name), error_code_(0),
|
||||
has_error_(false), has_value_(false) {}
|
||||
|
||||
// Serialize the metric name and value using the given serializer.
|
||||
// Caller owns |serializer| which cannot be null.
|
||||
virtual void Serialize(MetricSerializer* serializer) {
|
||||
if (has_value_) {
|
||||
impl::Serialize(serializer, metric_name_, value_);
|
||||
} else if (has_error_) {
|
||||
impl::SerializeError(serializer, metric_name_, error_code_);
|
||||
} else {
|
||||
// Do nothing if there is no value and no error.
|
||||
}
|
||||
}
|
||||
|
||||
// Record the value of the metric.
|
||||
void Record(const T& value) {
|
||||
value_ = value;
|
||||
has_value_ = true;
|
||||
has_error_ = false;
|
||||
}
|
||||
|
||||
// Set the error code if an error was encountered.
|
||||
void SetError(int error_code) {
|
||||
error_code_ = error_code;
|
||||
has_value_ = false;
|
||||
has_error_ = true;
|
||||
}
|
||||
|
||||
// Get the current value of the metric.
|
||||
const T& GetValue() { return value_; }
|
||||
|
||||
private:
|
||||
std::string metric_name_;
|
||||
T value_;
|
||||
int error_code_;
|
||||
bool has_error_;
|
||||
bool has_value_;
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_VALUE_METRIC_H_
|
||||
Reference in New Issue
Block a user