Source release 14.0.0

This commit is contained in:
John W. Bruce
2018-05-16 17:35:40 -07:00
parent 31381a1311
commit 3ab70cec4e
2053 changed files with 1585838 additions and 4614 deletions

View File

@@ -0,0 +1,53 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// This file contains the declarations for the EventMetric class and related
// types.
#ifndef WVCDM_METRICS_ATTRIBUTE_HANDLER_H_
#define WVCDM_METRICS_ATTRIBUTE_HANDLER_H_
#include "OEMCryptoCENC.h"
#include "field_tuples.h"
#include "log.h"
#include "metrics.pb.h"
#include "pow2bucket.h"
#include "value_metric.h"
#include "wv_cdm_types.h"
namespace wvcdm {
namespace metrics {
// This method is used to set the value of a single proto field.
// Specializations handle setting each value.
template <int I, typename F>
void SetAttributeField(const F& value, drm_metrics::Attributes* attributes);
// This class handles serializing the attributes for metric types that breakdown
// into multiple buckets. The template parameters should be specified in pairs.
// E.g. I1 and F1 should match. I1 should specify the field number in the proto
// and F1 should specify the type of the field to be set.
template <int I1, typename F1, int I2, typename F2, int I3, typename F3, int I4,
typename F4>
class AttributeHandler {
public:
// This method returns the serialized attribute proto for the given field
// values. The order of the field values must match the order in the
// template.
std::string GetSerializedAttributes(F1 field1, F2 field2, F3 field3,
F4 field4) const {
drm_metrics::Attributes attributes;
SetAttributeField<I1, F1>(field1, &attributes);
SetAttributeField<I2, F2>(field2, &attributes);
SetAttributeField<I3, F3>(field3, &attributes);
SetAttributeField<I4, F4>(field4, &attributes);
std::string serialized_attributes;
if (!attributes.SerializeToString(&serialized_attributes)) {
LOGE("Failed to serialize attribute proto.");
return "";
}
return serialized_attributes;
}
};
} // namespace metrics
} // namespace wvcdm
#endif // WVCDM_METRICS_ATTRIBUTE_HANDLER_H_

View File

@@ -11,9 +11,9 @@
#include <string>
#include <vector>
#include "attribute_handler.h"
#include "field_tuples.h"
#include "lock.h"
#include "metric_serialization.h"
namespace wvcdm {
namespace metrics {
@@ -22,23 +22,22 @@ class CounterMetricTest;
// This base class provides the common defintion used by all templated
// instances of CounterMetric.
class BaseCounterMetric : public MetricSerializable {
class BaseCounterMetric {
public:
// Send metric values to the MetricSerializer. |serializer| must
// not be null and is owned by the caller.
virtual void Serialize(MetricSerializer* serializer);
const std::map<std::string, int64_t>* GetValues() const {
return &value_map_;
};
protected:
// Instantiates a BaseCounterMetric.
BaseCounterMetric(const std::string& metric_name)
: metric_name_(metric_name) {}
BaseCounterMetric() {}
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);
void Increment(const std::string &counter_key, int64_t value);
private:
friend class CounterMetricTest;
@@ -48,8 +47,9 @@ class BaseCounterMetric : public MetricSerializable {
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.
* 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_;
};
@@ -67,136 +67,84 @@ class BaseCounterMetric : public MetricSerializable {
//
// Example usage:
//
// CounterMetric<int, int> my_metric("multiple/fields/metric",
// "request_type", // Field name.
// "error_code"); // Field name.
// CounterMetric<
// ::drm_metrics::Attributes::kErrorCodeFieldNumber,
// CdmResponseType,
// ::drm_metrics::Attributes::kCdmSecurityLevelFieldNumber,
// CdmSecurityLevel> my_metric;
//
// my_metric.Increment(1, 7, 23); // (counter value, request type, error code).
// // (counter value, error code, security level)
// my_metric.Increment(1, CdmResponseType::NO_ERROR, SecurityLevel::kLevel3);
//
// 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>
// The CounterMetric class serializes its values to a repeated field of
// drm_metrics::CounterMetric instances. The field numbers used for
// recording the drm_metrics::Attributes are specified in the template
// parameters above (e.g. kErrorCodeFieldNumber).
template <int I1 = 0, typename F1 = util::Unused, int I2 = 0,
typename F2 = util::Unused, int I3 = 0, typename F3 = util::Unused,
int I4 = 0, 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);
explicit CounterMetric() : BaseCounterMetric() {}
// 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());
F3 field3 = util::Unused(), F4 field4 = util::Unused()) {
Increment(1, field1, field2, field3, field4);
}
// 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());
void Increment(int64_t value, F1 field1 = util::Unused(),
F2 field2 = util::Unused(), F3 field3 = util::Unused(),
F4 field4 = util::Unused()) {
std::string key =
attribute_handler_.GetSerializedAttributes(field1, field2,
field3, field4);
BaseCounterMetric::Increment(key, value);
}
void ToProto(::google::protobuf::RepeatedPtrField<drm_metrics::CounterMetric>
*counters);
private:
friend class CounterMetricTest;
std::vector<std::string> field_names_;
AttributeHandler<I1, F1, I2, F2, I3, F3, I4, F4> attribute_handler_;
};
// 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);
// Partial specializations for CounterMetric template.
// Specialization for the CounterMetric with no attributes.
// For this, we don't deserialize and populate the attributes message.
template <>
inline void CounterMetric<0, util::Unused, 0, util::Unused, 0, util::Unused, 0,
util::Unused>::
ToProto(::google::protobuf::RepeatedPtrField<drm_metrics::CounterMetric>
*counters) {
const std::map<std::string, int64_t>* values = GetValues();
for (std::map<std::string, int64_t>::const_iterator it = values->begin();
it != values->end(); it++) {
drm_metrics::CounterMetric *new_counter = counters->Add();
new_counter->set_count(it->second);
}
}
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);
template <int I1, typename F1, int I2, typename F2, int I3, typename F3, int I4,
typename F4>
inline void CounterMetric<I1, F1, I2, F2, I3, F3, I4, F4>::ToProto(
::google::protobuf::RepeatedPtrField<drm_metrics::CounterMetric>
*counters) {
const std::map<std::string, int64_t>* values = GetValues();
for (std::map<std::string, int64_t>::const_iterator it = values->begin();
it != values->end(); it++) {
drm_metrics::CounterMetric *new_counter = counters->Add();
if (!new_counter->mutable_attributes()->ParseFromString(it->first)) {
LOGE("Failed to parse the attributes from a string.");
}
new_counter->set_count(it->second);
}
}
} // namespace metrics

View File

@@ -27,23 +27,23 @@ class Distribution {
Distribution();
// Uses the provided sample value to update the computed statistics.
void Record(double value);
void Record(float value);
// Return the value for each of the stats computed about the series of
// values (min, max, count, etc.).
double Min() const { return min_; }
double Max() const { return max_; }
double Mean() const { return mean_; }
int64_t Count() const { return count_; }
float Min() const { return min_; }
float Max() const { return max_; }
float Mean() const { return mean_; }
uint64_t Count() const { return count_; }
double Variance() const {
return count_ == 0 ? 0.0 : sum_squared_deviation_ / count_;
}
private:
int64_t count_;
double min_;
double max_;
double mean_;
uint64_t count_;
float min_;
float max_;
float mean_;
double sum_squared_deviation_;
};

View File

@@ -10,10 +10,14 @@
#include <string>
#include <vector>
#include "OEMCryptoCENC.h"
#include "attribute_handler.h"
#include "distribution.h"
#include "field_tuples.h"
#include "lock.h"
#include "metric_serialization.h"
#include "log.h"
#include "metrics.pb.h"
#include "pow2bucket.h"
#include "shared_ptr.h"
namespace wvcdm {
namespace metrics {
@@ -22,31 +26,26 @@ class EventMetricTest;
// This base class provides the common defintion used by all templated
// instances of EventMetric.
class BaseEventMetric : public MetricSerializable {
class BaseEventMetric {
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 BaseEventMetric.
BaseEventMetric(const std::string& metric_name)
: metric_name_(metric_name) {}
BaseEventMetric() {}
virtual ~BaseEventMetric();
// Record will look for an existing instance of the Distribution identified
// by the field_names_values string and update it. If the instance does
// by the distribution_key string and update it. If the instance does
// not exist, this will create it.
void Record(const std::string& field_names_values, double value);
void Record(const std::string &distribution_key, double value);
// value_map_ contains a mapping from the string key (attribute name/values)
// to the distribution instance which holds the metric information
// (min, max, sum, etc.).
std::map<std::string, Distribution *> value_map_;
private:
friend class EventMetricTest;
const std::string metric_name_;
// value_map_ contains a mapping from the field name/value pairs to the
// distribution instance which holds the metric information (min, max, sum,
// etc.).
std::map<std::string, Distribution*> value_map_;
/*
* This locks the internal state of the event metric to ensure safety across
@@ -55,39 +54,6 @@ class BaseEventMetric : public MetricSerializable {
Lock internal_lock_;
};
// 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
// have many possible values, but we want to bucket them into a small set of
// numbers (32 or 64).
class Pow2Bucket {
public:
explicit Pow2Bucket(size_t value) : value_(GetLowerBucket(value)) {}
Pow2Bucket(const Pow2Bucket& value) : value_(value.value_) {}
// Support for converting to string.
friend std::ostream& operator<<(std::ostream& os, const Pow2Bucket& log)
{ return os << log.value_; }
private:
inline size_t GetLowerBucket(size_t value) {
if (!value) {
return 0;
}
size_t log = 0;
while (value) {
log++;
value >>= 1;
}
return 1u << (log - 1);
}
size_t value_;
};
// The EventMetric class is used to capture statistics about an event such as
// a method call. EventMetric keeps track of the count, mean, min, max and
// variance for the value being measured. For example, we may want to track the
@@ -105,125 +71,100 @@ class Pow2Bucket {
//
// Example usage:
//
// EventMetric<int, int> my_metric("multiple/fields/metric",
// "request_type", "error_code", // Field names);
//
// my_metric.Record(1, 7, 23); // (latency value, request type, error code).
// EventMetric<
// ::drm_metrics::Attributes::kErrorCodeFieldNumber,
// CdmResponseType,
// ::drm_metrics::Attributes::kCdmSecurityLevelFieldNumber,
// CdmSecurityLevel> my_metric;
//
// The EventMetric supports serialization. A call to Serialize will
// serialize all values to the provided MetricsSerializer instance.
// // (latency value, error code, security level)
// my_metric.Increment(1, CdmResponseType::NO_ERROR, SecurityLevel::kLevel3);
//
// 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>
// The EventMetric class serializes its values to a repeated field of
// drm_metrics::DistributionMetric instances. The field numbers used for
// recording the drm_metrics::Attributes are specified in the template
// parameters above (e.g. kErrorCodeFieldNumber).
template <int I1 = 0, typename F1 = util::Unused, int I2 = 0,
typename F2 = util::Unused, int I3 = 0, typename F3 = util::Unused,
int I4 = 0, typename F4 = util::Unused>
class EventMetric : public BaseEventMetric {
public:
// Create an EventMetric instance with the name |metric_name|.
EventMetric(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.
EventMetric(const std::string& metric_name,
const char* field_name);
EventMetric(const std::string& metric_name,
const char* field_name1,
const char* field_name2);
EventMetric(const std::string& metric_name,
const char* field_name1,
const char* field_name2,
const char* field_name3);
EventMetric(const std::string& metric_name,
const char* field_name1,
const char* field_name2,
const char* field_name3,
const char* field_name4);
// Create an EventMetric instance with no attribute breakdowns.
explicit EventMetric() : BaseEventMetric(){}
// Record will update the statistics of the EventMetric broken down by the
// given field values.
void Record(double value,
F1 field1 = util::Unused(),
F2 field2 = util::Unused(),
F3 field3 = util::Unused(),
F4 field4 = util::Unused());
void Record(double value, F1 field1 = util::Unused(),
F2 field2 = util::Unused(), F3 field3 = util::Unused(),
F4 field4 = util::Unused()) {
std::string key =
attribute_handler_.GetSerializedAttributes(field1, field2,
field3, field4);
BaseEventMetric::Record(key, value);
}
const std::map<std::string, Distribution *>* GetDistributions() const {
return &value_map_;
};
void ToProto(
::google::protobuf::RepeatedPtrField<drm_metrics::DistributionMetric>
*distributions_proto);
private:
friend class EventMetricTest;
std::vector<std::string> field_names_;
AttributeHandler<I1, F1, I2, F2, I3, F3, I4, F4> attribute_handler_;
inline void SetDistributionValues(
const Distribution &distribution,
drm_metrics::DistributionMetric *metric_proto) {
metric_proto->set_mean(distribution.Mean());
metric_proto->set_operation_count(distribution.Count());
if (distribution.Count() > 1) {
metric_proto->set_min(distribution.Min());
metric_proto->set_max(distribution.Max());
metric_proto->set_mean(distribution.Mean());
metric_proto->set_variance(distribution.Variance());
metric_proto->set_operation_count(distribution.Count());
}
}
};
// Overloaded template constructor implementations for EventMetric.
template<typename F1, typename F2, typename F3, typename F4>
EventMetric<F1, F2, F3, F4>::EventMetric(
const std::string& metric_name)
: BaseEventMetric(metric_name) {
ASSERT_METRIC_UNUSED_START_FROM(1);
// Partial Specializations of Event Metrics.
// Specialization for the event metric with no attributes.
// For this, we don't deserialize and populate the attributes message.
template <>
inline void EventMetric<0, util::Unused, 0, util::Unused, 0, util::Unused, 0,
util::Unused>::
ToProto(
::google::protobuf::RepeatedPtrField<drm_metrics::DistributionMetric>
*distributions_proto) {
const std::map<std::string, Distribution *>* distributions
= GetDistributions();
for (std::map<std::string, Distribution *>::const_iterator it =
distributions->begin(); it != distributions->end(); it++) {
drm_metrics::DistributionMetric *new_metric = distributions_proto->Add();
SetDistributionValues(*it->second, new_metric);
}
}
template<typename F1, typename F2, typename F3, typename F4>
EventMetric<F1, F2, F3, F4>::EventMetric(
const std::string& metric_name,
const char* field_name)
: BaseEventMetric(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>
EventMetric<F1, F2, F3, F4>::EventMetric(
const std::string& metric_name,
const char* field_name1,
const char* field_name2)
: BaseEventMetric(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>
EventMetric<F1, F2, F3, F4>::EventMetric(
const std::string& metric_name,
const char* field_name1,
const char* field_name2,
const char* field_name3)
: BaseEventMetric(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>
EventMetric<F1, F2, F3, F4>::EventMetric(
const std::string& metric_name,
const char* field_name1,
const char* field_name2,
const char* field_name3,
const char* field_name4)
: BaseEventMetric(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 EventMetric<F1, F2, F3, F4>::Record(
double value, F1 field1, F2 field2, F3 field3, F4 field4) {
std::string field_name_values =
util::MakeFieldNameString(field_names_, field1, field2, field3, field4);
BaseEventMetric::Record(field_name_values, value);
template <int I1, typename F1, int I2, typename F2, int I3, typename F3, int I4,
typename F4>
inline void EventMetric<I1, F1, I2, F2, I3, F3, I4, F4>::ToProto(
::google::protobuf::RepeatedPtrField<drm_metrics::DistributionMetric>
*distributions_proto) {
const std::map<std::string, Distribution *>* distributions
= GetDistributions();
for (std::map<std::string, Distribution *>::const_iterator it =
distributions->begin(); it != distributions->end(); it++) {
drm_metrics::DistributionMetric *new_metric = distributions_proto->Add();
if (!new_metric->mutable_attributes()->ParseFromString(it->first)) {
LOGE("Failed to parse the attributes from a string.");
}
SetDistributionValues(*it->second, new_metric);
}
}
} // namespace metrics

View File

@@ -5,13 +5,8 @@
#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 {
@@ -24,105 +19,13 @@ namespace util {
// 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; }
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_

View File

@@ -1,46 +0,0 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// This file contains the declarations for the Metric class and related
// types.
#ifndef WVCDM_METRICS_METRIC_SERIALIZATION_H_
#define WVCDM_METRICS_METRIC_SERIALIZATION_H_
namespace wvcdm {
namespace metrics {
// The MetricSerializer is implemented by the code that instantiates
// MetricSerializable instances. The caller provides this MetricSerializer
// instance to MetricSerializable::Serialize. In turn, Serialize will make a
// call to Set* for each value to be Serialized. The MetricSerialiable
// implementation may set zero or more values on the MetricSerializer.
class MetricSerializer {
public:
virtual ~MetricSerializer() {};
// The metric_id is the metric name, plus the metric part name, and the
// metric field name/value string. E.g.
// name: "drm/cdm/decrypt/duration"
// part: "mean"
// field name/value string: "{error_code:0&buffer_size:1024}"
// becomes: "drm/cdm/decrypt/duration/mean/{error_code:0&buffer_size:1024}".
virtual void SetString(const std::string& metric_id,
const std::string& value) = 0;
virtual void SetInt32(const std::string& metric_id, int32_t value) = 0;
virtual void SetInt64(const std::string& metric_id, int64_t value) = 0;
virtual void SetDouble(const std::string& metric_id, double value) = 0;
};
// This abstract class merely provides the definition for serializing the value
// of the metric.
class MetricSerializable {
public:
virtual ~MetricSerializable() { }
// Serialize metric values to the MetricSerializer. |serializer| must
// not be null and is owned by the caller.
virtual void Serialize(MetricSerializer* serializer) = 0;
};
} // namespace metrics
} // namespace wvcdm
#endif // WVCDM_METRICS_METRIC_SERIALIZATION_H_

View File

@@ -6,14 +6,14 @@
#ifndef WVCDM_METRICS_METRICS_GROUP_H_
#define WVCDM_METRICS_METRICS_GROUP_H_
#include <ostream>
#include <stddef.h>
#include <stdint.h>
#include <ostream>
#include "OEMCryptoCENC.h"
#include "counter_metric.h"
#include "event_metric.h"
#include "metrics.pb.h"
#include "OEMCryptoCENC.h"
#include "value_metric.h"
#include "wv_cdm_types.h"
@@ -29,9 +29,9 @@
// long total_time = 0;
// long error_code = TimeSomeOperation(&total_time);
// M_RECORD(my_metrics, my_metric_name, total_time, error_code);
#define M_RECORD(GROUP, METRIC, TIME, ...) \
if ( GROUP ) { \
( GROUP ) -> METRIC . Record( TIME, ##__VA_ARGS__ ); \
#define M_RECORD(GROUP, METRIC, TIME, ...) \
if (GROUP) { \
(GROUP)->METRIC.Record(TIME, ##__VA_ARGS__); \
}
// This definition automatically times an operation and records the time and
@@ -44,108 +44,187 @@
// my_metrics_collection,
// oemcrypto_initialize_,
// sts);
#define M_TIME(CALL, GROUP, METRIC, ...) \
if ( GROUP ) { \
wvcdm::metrics::TimerMetric timer; \
timer.Start(); \
CALL; \
( GROUP ) -> METRIC . Record(timer.AsUs(), ##__VA_ARGS__ ); \
} else { \
CALL; \
#define M_TIME(CALL, GROUP, METRIC, ...) \
if (GROUP) { \
wvcdm::metrics::TimerMetric timer; \
timer.Start(); \
CALL; \
(GROUP)->METRIC.Record(timer.AsUs(), ##__VA_ARGS__); \
} else { \
CALL; \
}
namespace wvcdm {
namespace metrics {
namespace {
// Short name definitions to ease AttributeHandler definitions.
// Internal namespace to help simplify declarations.
const int kErrorCodeFieldNumber =
::drm_metrics::Attributes::kErrorCodeFieldNumber;
const int kErrorCodeBoolFieldNumber =
::drm_metrics::Attributes::kErrorCodeBoolFieldNumber;
const int kCdmSecurityLevelFieldNumber =
::drm_metrics::Attributes::kCdmSecurityLevelFieldNumber;
const int kSecurityLevelFieldNumber =
::drm_metrics::Attributes::kSecurityLevelFieldNumber;
const int kLengthFieldNumber =
::drm_metrics::Attributes::kLengthFieldNumber;
const int kEncryptAlgorithmFieldNumber =
::drm_metrics::Attributes::kEncryptionAlgorithmFieldNumber;
const int kSigningAlgorithmFieldNumber =
::drm_metrics::Attributes::kSigningAlgorithmFieldNumber;
const int kOemCryptoResultFieldNumber =
::drm_metrics::Attributes::kOemCryptoResultFieldNumber;
const int kKeyStatusTypeFieldNumber =
::drm_metrics::Attributes::kKeyStatusTypeFieldNumber;
const int kEventTypeFieldNumber =
::drm_metrics::Attributes::kEventTypeFieldNumber;
} // anonymous namespace
// This enum defines the conditions encountered during OEMCrypto Initialization
// in oemcrypto_adapter_dynamic.
typedef enum OEMCryptoInitializationMode {
OEMCrypto_INITIALIZED_USING_IN_APP = 0,
OEMCrypto_INITIALIZED_FORCING_L3 = 1,
OEMCrypto_INITIALIZED_USING_L3_NO_L1_LIBRARY_PATH = 2,
OEMCrypto_INITIALIZED_USING_L3_L1_OPEN_FAILED = 3,
OEMCrypto_INITIALIZED_USING_L3_L1_LOAD_FAILED = 4,
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_INITIALIZE_L1 = 5,
OEMCrypto_INITIALIZED_USING_L3_WRONG_L1_VERSION = 6,
OEMCrypto_INITIALIZED_USING_L1_WITH_KEYBOX = 7,
OEMCrypto_INITIALIZED_USING_L1_WITH_CERTIFICATE = 8,
OEMCrypto_INITIALIZED_USING_L1_CERTIFICATE_MIX = 9,
OEMCrypto_INITIALIZED_USING_L3_BAD_KEYBOX = 10,
OEMCrypto_INITIALIZED_USING_IN_APP = 0,
OEMCrypto_INITIALIZED_FORCING_L3 = 1,
OEMCrypto_INITIALIZED_USING_L3_NO_L1_LIBRARY_PATH = 2,
OEMCrypto_INITIALIZED_USING_L3_L1_OPEN_FAILED = 3,
OEMCrypto_INITIALIZED_USING_L3_L1_LOAD_FAILED = 4,
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_INITIALIZE_L1 = 5,
OEMCrypto_INITIALIZED_USING_L3_WRONG_L1_VERSION = 6,
OEMCrypto_INITIALIZED_USING_L1_WITH_KEYBOX = 7,
OEMCrypto_INITIALIZED_USING_L1_WITH_CERTIFICATE = 8,
OEMCrypto_INITIALIZED_USING_L1_CERTIFICATE_MIX = 9,
OEMCrypto_INITIALIZED_USING_L3_BAD_KEYBOX = 10,
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_OPEN_FACTORY_KEYBOX = 11,
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_INSTALL_KEYBOX = 12,
OEMCrypto_INITIALIZED_USING_L1_INSTALLED_KEYBOX = 13,
OEMCrypto_INITIALIZED_USING_L3_INVALID_L1 = 14,
OEMCrypto_INITIALIZED_USING_L1_WITH_PROVISIONING_3_0 = 15,
OEMCrypto_INITIALIZED_L3_INITIALIZATION_FAILED = 16
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_INSTALL_KEYBOX = 12,
OEMCrypto_INITIALIZED_USING_L1_INSTALLED_KEYBOX = 13,
OEMCrypto_INITIALIZED_USING_L3_INVALID_L1 = 14,
OEMCrypto_INITIALIZED_USING_L1_WITH_PROVISIONING_3_0 = 15,
OEMCrypto_INITIALIZED_L3_INITIALIZATION_FAILED = 16
} OEMCryptoInitializationMode;
// This class contains metrics for Crypto Session and OEM Crypto.
class CryptoMetrics {
public:
CryptoMetrics();
void Serialize(drm_metrics::MetricsGroup* metrics);
void Serialize(drm_metrics::WvCdmMetrics::CryptoMetrics *crypto_metrics);
/* CRYPTO SESSION */
// TODO(blueeyes): Convert this to crypto_session_default_security_level_.
ValueMetric<CdmSecurityLevel> crypto_session_security_level_;
CounterMetric<CdmResponseType> crypto_session_delete_all_usage_reports_;
CounterMetric<CdmResponseType> crypto_session_delete_multiple_usage_information_;
EventMetric<CdmResponseType, Pow2Bucket, CdmEncryptionAlgorithm> crypto_session_generic_decrypt_;
EventMetric<CdmResponseType, Pow2Bucket, CdmEncryptionAlgorithm> crypto_session_generic_encrypt_;
EventMetric<CdmResponseType, Pow2Bucket, CdmSigningAlgorithm> crypto_session_generic_sign_;
EventMetric<CdmResponseType, Pow2Bucket, CdmSigningAlgorithm> crypto_session_generic_verify_;
CounterMetric<bool> crypto_session_get_device_unique_id_;
CounterMetric<bool> crypto_session_get_token_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
crypto_session_delete_all_usage_reports_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
crypto_session_delete_multiple_usage_information_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType, kLengthFieldNumber,
Pow2Bucket, kEncryptAlgorithmFieldNumber, CdmEncryptionAlgorithm>
crypto_session_generic_decrypt_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType, kLengthFieldNumber,
Pow2Bucket, kEncryptAlgorithmFieldNumber, CdmEncryptionAlgorithm>
crypto_session_generic_encrypt_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType, kLengthFieldNumber,
Pow2Bucket, kSigningAlgorithmFieldNumber, CdmSigningAlgorithm>
crypto_session_generic_sign_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType, kLengthFieldNumber,
Pow2Bucket, kSigningAlgorithmFieldNumber, CdmSigningAlgorithm>
crypto_session_generic_verify_;
CounterMetric<kErrorCodeBoolFieldNumber, bool>
crypto_session_get_device_unique_id_;
CounterMetric<kErrorCodeBoolFieldNumber, bool> crypto_session_get_token_;
ValueMetric<double> crypto_session_life_span_;
EventMetric<bool> crypto_session_load_certificate_private_key_;
EventMetric<CdmResponseType, SecurityLevel> crypto_session_open_; // This is the requested security level.
EventMetric<kErrorCodeBoolFieldNumber, bool>
crypto_session_load_certificate_private_key_;
// This uses the requested security level.
EventMetric<kErrorCodeFieldNumber, CdmResponseType, kSecurityLevelFieldNumber,
SecurityLevel>
crypto_session_open_;
ValueMetric<uint32_t> crypto_session_system_id_;
EventMetric<CdmResponseType> crypto_session_update_usage_information_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType>
crypto_session_update_usage_information_;
ValueMetric<bool> crypto_session_usage_information_support_;
/* OEMCRYPTO */
ValueMetric<uint32_t> oemcrypto_api_version_;
CounterMetric<OEMCryptoResult> oemcrypto_close_session_;
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_copy_buffer_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_close_session_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult, kLengthFieldNumber,
Pow2Bucket>
oemcrypto_copy_buffer_;
ValueMetric<OEMCrypto_HDCP_Capability> oemcrypto_current_hdcp_capability_;
CounterMetric<OEMCryptoResult> oemcrypto_deactivate_usage_entry_;
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_decrypt_cenc_;
CounterMetric<OEMCryptoResult> oemcrypto_delete_usage_entry_;
CounterMetric<OEMCryptoResult> oemcrypto_delete_usage_table_;
EventMetric<OEMCryptoResult> oemcrypto_derive_keys_from_session_key_;
CounterMetric<OEMCryptoResult> oemcrypto_force_delete_usage_entry_;
EventMetric<OEMCryptoResult> oemcrypto_generate_derived_keys_;
CounterMetric<OEMCryptoResult> oemcrypto_generate_nonce_;
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generate_rsa_signature_;
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generate_signature_;
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_decrypt_;
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_encrypt_;
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_sign_;
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_verify_;
CounterMetric<OEMCryptoResult> oemcrypto_get_device_id_;
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_get_key_data_;
CounterMetric<OEMCryptoResult> oemcrypto_get_oem_public_certificate_;
CounterMetric<OEMCryptoResult> oemcrypto_get_random_;
EventMetric<OEMCryptoResult> oemcrypto_initialize_;
EventMetric<OEMCryptoResult> oemcrypto_install_keybox_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_deactivate_usage_entry_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult, kLengthFieldNumber,
Pow2Bucket>
oemcrypto_decrypt_cenc_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_delete_usage_entry_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_delete_usage_table_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_derive_keys_from_session_key_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_force_delete_usage_entry_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_generate_derived_keys_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_generate_nonce_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult, kLengthFieldNumber,
Pow2Bucket>
oemcrypto_generate_rsa_signature_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult, kLengthFieldNumber,
Pow2Bucket>
oemcrypto_generate_signature_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult, kLengthFieldNumber,
Pow2Bucket>
oemcrypto_generic_decrypt_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult, kLengthFieldNumber,
Pow2Bucket>
oemcrypto_generic_encrypt_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult, kLengthFieldNumber,
Pow2Bucket>
oemcrypto_generic_sign_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult, kLengthFieldNumber,
Pow2Bucket>
oemcrypto_generic_verify_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_get_device_id_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult, kLengthFieldNumber,
Pow2Bucket>
oemcrypto_get_key_data_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_get_oem_public_certificate_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_get_random_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_initialize_;
ValueMetric<bool> oemcrypto_is_anti_rollback_hw_present_;
ValueMetric<bool> oemcrypto_is_keybox_valid_;
EventMetric<OEMCryptoResult> oemcrypto_load_device_rsa_key_;
EventMetric<OEMCryptoResult> oemcrypto_load_keys_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_load_device_rsa_key_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_load_entitled_keys_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_load_keys_;
ValueMetric<OEMCrypto_HDCP_Capability> oemcrypto_max_hdcp_capability_;
ValueMetric<size_t> oemcrypto_max_number_of_sessions_;
ValueMetric<size_t> oemcrypto_number_of_open_sessions_;
ValueMetric<OEMCrypto_ProvisioningMethod> oemcrypto_provisioning_method_;
EventMetric<OEMCryptoResult> oemcrypto_refresh_keys_;
CounterMetric<OEMCryptoResult> oemcrypto_report_usage_;
EventMetric<OEMCryptoResult> oemcrypto_rewrap_device_rsa_key_;
EventMetric<OEMCryptoResult> oemcrypto_rewrap_device_rsa_key_30_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_refresh_keys_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_report_usage_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_rewrap_device_rsa_key_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_rewrap_device_rsa_key_30_;
ValueMetric<uint16_t> oemcrypto_security_patch_level_;
EventMetric<OEMCryptoResult> oemcrypto_select_key_;
ValueMetric<bool> oemcrypto_supports_usage_table_;
CounterMetric<OEMCryptoResult> oemcrypto_update_usage_table_;
EventMetric<OEMCryptoResult> oemcrypto_wrap_keybox_;
EventMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_select_key_;
ValueMetric<CdmUsageSupportType> oemcrypto_usage_table_support_;
CounterMetric<kOemCryptoResultFieldNumber, OEMCryptoResult>
oemcrypto_update_usage_table_;
};
// This class contains session-scoped metrics. All properties and
@@ -157,11 +236,12 @@ class SessionMetrics {
// Sets the session id of the metrics group. This allows the session
// id to be captured and reported as part of the collection of metrics.
void SetSessionId(const CdmSessionId& session_id) {
session_id_ = session_id; }
void SetSessionId(const CdmSessionId &session_id) {
session_id_ = session_id;
}
// Returns the session id or an empty session id if it has not been set.
const CdmSessionId& GetSessionId() const { return session_id_; }
const CdmSessionId &GetSessionId() const { return session_id_; }
// Marks the metrics object as completed and ready for serialization.
void SetCompleted() { completed_ = true; }
@@ -172,20 +252,23 @@ class SessionMetrics {
// Returns a pointer to the crypto metrics belonging to the engine instance.
// This instance retains ownership of the object.
CryptoMetrics* GetCryptoMetrics() { return &crypto_metrics_; }
CryptoMetrics *GetCryptoMetrics() { return &crypto_metrics_; }
// Metrics collected at the session level.
ValueMetric<double> cdm_session_life_span_; // Milliseconds.
EventMetric<CdmResponseType> cdm_session_renew_key_;
CounterMetric<CdmResponseType> cdm_session_restore_offline_session_;
CounterMetric<CdmResponseType> cdm_session_restore_usage_session_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType> cdm_session_renew_key_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_session_restore_offline_session_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_session_restore_usage_session_;
// Serialize the session metrics to the provided |metric_group|.
// |metric_group| is owned by the caller and must not be null.
void Serialize(drm_metrics::MetricsGroup* metric_group);
void Serialize(drm_metrics::WvCdmMetrics::SessionMetrics *session_metrics);
private:
void SerializeSessionMetrics(drm_metrics::MetricsGroup* metric_group);
void SerializeSessionMetrics(
drm_metrics::WvCdmMetrics::SessionMetrics *session_metrics);
CdmSessionId session_id_;
bool completed_;
CryptoMetrics crypto_metrics_;
@@ -211,7 +294,7 @@ class OemCryptoDynamicAdapterMetrics {
// Serialize the session metrics to the provided |metric_group|.
// |metric_group| is owned by the caller and must not be null.
void Serialize(drm_metrics::MetricsGroup* metric_group);
void Serialize(drm_metrics::WvCdmMetrics::EngineMetrics *engine_metrics);
// Clears the existing metric values.
void Clear();
@@ -228,7 +311,7 @@ class OemCryptoDynamicAdapterMetrics {
// initialization is guaranteed to be threadsafe. We return the reference to
// avoid non-guaranteed destructor order problems. Effectively, the destructor
// is never run for the created instance.
OemCryptoDynamicAdapterMetrics& GetDynamicAdapterMetricsInstance();
OemCryptoDynamicAdapterMetrics &GetDynamicAdapterMetricsInstance();
// This class contains engine-scoped metrics. All properties and
// statistics related to operations within the engine, but outside
@@ -241,7 +324,7 @@ class EngineMetrics {
// Add a new SessionMetrics instance and return a pointer to the caller.
// The new SessionMetrics instance is owned by this EngineMetrics instance
// and will exist until RemoveSession is called or this object is deleted.
SessionMetrics* AddSession();
SessionMetrics *AddSession();
// Removes the metrics object for the given session id. This should only
// be called when the SessionMetrics instance is no longer in use.
@@ -250,7 +333,7 @@ class EngineMetrics {
// Returns a pointer to the crypto metrics belonging to the engine instance.
// The CryptoMetrics instance is still owned by this object and will exist
// until this object is deleted.
CryptoMetrics* GetCryptoMetrics() { return &crypto_metrics_; }
CryptoMetrics *GetCryptoMetrics() { return &crypto_metrics_; }
// Serialize engine and session metrics into a serialized MetricsGroup
// instance and output that instance to the provided |metric_group|.
@@ -260,42 +343,66 @@ class EngineMetrics {
// |clear_sessions| indicates that this call should clear sessions metrics
// for those sessions that were serialized. This allows atomic
// serialization and closing of session-level metrics.
void Serialize(drm_metrics::MetricsGroup* metric_group, bool completed_only,
bool clear_serialized_sessions);
// void Serialize(drm_metrics::MetricsGroup* metric_group, bool
// completed_only,
// bool clear_serialized_sessions);
void Serialize(drm_metrics::WvCdmMetrics *engine_metrics);
void SetAppPackageName(const std::string& app_package_name);
void SetAppPackageName(const std::string &app_package_name);
// Metrics recorded at the engine level.
EventMetric<CdmResponseType> cdm_engine_add_key_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType> cdm_engine_add_key_;
ValueMetric<std::string> cdm_engine_cdm_version_;
CounterMetric<CdmResponseType> cdm_engine_close_session_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_close_session_;
ValueMetric<int64_t> cdm_engine_creation_time_millis_;
EventMetric<CdmResponseType, Pow2Bucket> cdm_engine_decrypt_;
CounterMetric<bool> cdm_engine_find_session_for_key_;
EventMetric<CdmResponseType> cdm_engine_generate_key_request_;
EventMetric<CdmResponseType> cdm_engine_get_provisioning_request_;
EventMetric<CdmResponseType> cdm_engine_get_usage_info_;
EventMetric<CdmResponseType> cdm_engine_handle_provisioning_response_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType, kLengthFieldNumber,
Pow2Bucket>
cdm_engine_decrypt_;
CounterMetric<kErrorCodeBoolFieldNumber, bool>
cdm_engine_find_session_for_key_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_generate_key_request_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_get_provisioning_request_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_get_secure_stop_ids_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_get_usage_info_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_handle_provisioning_response_;
ValueMetric<double> cdm_engine_life_span_; // Milliseconds
CounterMetric<CdmResponseType> cdm_engine_open_key_set_session_;
CounterMetric<CdmResponseType> cdm_engine_open_session_;
EventMetric<CdmResponseType> cdm_engine_query_key_status_;
CounterMetric<CdmResponseType> cdm_engine_release_all_usage_info_;
CounterMetric<CdmResponseType> cdm_engine_release_usage_info_;
CounterMetric<CdmResponseType> cdm_engine_remove_keys_;
EventMetric<CdmResponseType> cdm_engine_restore_key_;
CounterMetric<CdmResponseType, CdmSecurityLevel> cdm_engine_unprovision_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_open_key_set_session_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_open_session_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_query_key_status_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_release_all_usage_info_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_release_usage_info_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_remove_all_usage_info_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType> cdm_engine_remove_keys_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType>
cdm_engine_remove_usage_info_;
EventMetric<kErrorCodeFieldNumber, CdmResponseType> cdm_engine_restore_key_;
CounterMetric<kErrorCodeFieldNumber, CdmResponseType,
kCdmSecurityLevelFieldNumber, CdmSecurityLevel>
cdm_engine_unprovision_;
private:
Lock session_metrics_lock_;
std::vector<metrics::SessionMetrics*> session_metrics_list_;
std::vector<metrics::SessionMetrics *> session_metrics_list_;
CryptoMetrics crypto_metrics_;
std::string app_package_name_;
void SerializeEngineMetrics(drm_metrics::MetricsGroup* out);
void SerializeEngineMetrics(
drm_metrics::WvCdmMetrics::EngineMetrics *engine_metrics);
};
} // namespace metrics
} // namespace wvcdm
#endif // WVCDM_METRICS_METRICS_GROUP_H_
#endif // WVCDM_METRICS_METRICS_GROUP_H_

View File

@@ -0,0 +1,50 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// This file contains the declaration of the Pow2Bucket class which
// is a convenient way to bucketize sampled values into powers of 2.
#ifndef WVCDM_METRICS_POW2BUCKET_H_
#define WVCDM_METRICS_POW2BUCKET_H_
namespace wvcdm {
namespace metrics {
// 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
// have many possible values, but we want to bucket them into a small set of
// numbers (32 or 64).
class Pow2Bucket {
public:
explicit Pow2Bucket(size_t value) : value_(GetLowerBucket(value)) {}
Pow2Bucket(const Pow2Bucket &value) : value_(value.value_) {}
size_t value() const { return value_; }
// Support for converting to string.
friend std::ostream &operator<<(std::ostream &os, const Pow2Bucket &log) {
return os << log.value_;
}
private:
inline size_t GetLowerBucket(size_t value) {
if (!value) {
return 0;
}
size_t log = 0;
while (value) {
log++;
value >>= 1;
}
return 1u << (log - 1);
}
size_t value_;
};
} // namespace metrics
} // namespace wvcdm
#endif // WVCDM_METRICS_POW2BUCKET_H_

View File

@@ -8,69 +8,44 @@
#include <stdint.h>
#include <string>
#include "metric_serialization.h"
#include "metrics.pb.h"
namespace wvcdm {
namespace metrics {
// Private namespace for some helper implementation functions.
// Internal namespace for helper methods.
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);
// Helper function for setting a value in the proto.
template <typename T>
void SetValue(drm_metrics::ValueMetric *value_proto, const T &value);
inline void SerializeError(MetricSerializer* serializer,
const std::string& metric_name,
const int& error_code) {
serializer->SetInt32(metric_name + "/error", error_code);
}
} // namespace impl
} // 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.
// The ValueMetric class supports storing a single, overwritable value or an
// error code. This class can be serialized to a drm_metrics::ValueMetric proto.
// If an error or value was not provided, this metric will serialize to nullptr.
//
// Example Usage:
// Metric<string> cdm_version("drm/cdm/version")
// .Record("a.b.c.d");
//
// MyMetricSerializerImpl serialzer;
// cdm_version.Serialize(&serializer);
// Metric<string> cdm_version().Record("a.b.c.d");
// std::unique_ptr<drm_metrics::ValueMetric> mymetric(
// cdm_version.ToProto());
//
// Example Error Usage:
//
// Metric<string> cdm_version("drm/cdm/version")
// .SetError(error_code);
// Metric<string> cdm_version().SetError(error_code);
// std::unique_ptr<drm_metrics::ValueMetric> mymetric(
// cdm_version.ToProto());
//
// 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 {
template <typename T>
class ValueMetric {
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.
}
}
explicit ValueMetric()
: error_code_(0), has_error_(false), has_value_(false) {}
// Record the value of the metric.
void Record(const T& value) {
void Record(const T &value) {
value_ = value;
has_value_ = true;
has_error_ = false;
@@ -83,8 +58,11 @@ class ValueMetric : public MetricSerializable {
has_error_ = true;
}
// Get the current value of the metric.
const T& GetValue() { return value_; }
bool HasValue() const { return has_value_; }
const T &GetValue() const { return value_; }
bool HasError() const { return has_error_; }
int GetError() const { return error_code_; }
// Clears the indicators that the metric or error was set.
void Clear() {
@@ -92,8 +70,23 @@ class ValueMetric : public MetricSerializable {
has_error_ = false;
}
// Returns a new ValueMetric proto containing the metric value or the
// error code. If neither the error or value are set, it returns nullptr.
drm_metrics::ValueMetric *ToProto() {
if (has_error_) {
drm_metrics::ValueMetric *value_proto = new drm_metrics::ValueMetric;
value_proto->set_error_code(error_code_);
return value_proto;
} else if (has_value_) {
drm_metrics::ValueMetric *value_proto = new drm_metrics::ValueMetric;
impl::SetValue(value_proto, value_);
return value_proto;
}
return NULL;
}
private:
std::string metric_name_;
T value_;
int error_code_;
bool has_error_;