Build metrics_dump tool in Android.

Replace Makefile with Android.bp.
Remove duplication of protos in proto directory.

Since we are now building the metrics_dump tool
under Android, use frameworks metrics.proto
directly. Also, reference cdm's wv_metrics.proto
from the cdm directory instead of creating a
subset in proto directory.

bug: 161783052
bug: 170607430

Test: build
  m -j128 metrics_dump
Test: metrics_dump [bugreport from adt-3-r.zip]
Test: metrics_dump [bugreport from sabrina-q.gz]
Test: metrics_dump --widevine [adb shell dumpsys media.metrics output]
Change-Id: I82c7e723453ac2a6335cb2bb732a376d535b9ea3
This commit is contained in:
Edwin Wong
2020-09-29 15:45:57 -07:00
parent 1381746ecb
commit d5d0652d4f
12 changed files with 372 additions and 451 deletions

View File

@@ -4,12 +4,12 @@
//
// simple base64 decoder
#include "base64decode.h"
#include <iostream>
#include <memory>
#include <string>
#include "base64decode.h"
namespace base64 {
using namespace std;

View File

@@ -11,7 +11,7 @@
#include <sstream>
#include <string>
#include "mediadrm_metrics.pb.h"
#include "metrics.pb.h"
namespace mediadrm_metrics {

View File

@@ -4,114 +4,106 @@
//
// Format metric output from |adb shell dumpsys media.metrics|
#include <algorithm>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include "base64decode.h"
#include "mediadrm_metrics.h"
#include "parse_metrics.h"
#include "wv_metrics.h"
namespace metrics_dump {
using namespace base64;
using namespace mediadrm_metrics;
using namespace wv_metrics;
using std::cerr;
using std::cout;
using std::endl;
using std::string;
using std::vector;
string selected_one;
std::vector<string> excluded_ones;
// Look for metrics pattern
// q-metrics:
// [:item:item:]
// r-metrics:
// {item, (item), (item)}
//
// @return true if metrics is formatted prior to R release
bool is_q_metrics(const string &line) {
int left_brackets = std::count(line.begin(), line.end(), '[');
int right_brackets = std::count(line.begin(), line.end(), ']');
return (left_brackets == 1 && right_brackets == 1 &&
line.find('(') == string::npos && line.find(')') == string::npos);
}
// Process one line of metrics
void process_one_metric(const string &line) {
std::istringstream fields(line);
#define READ_TOKEN(NAME) \
if (!std::getline(fields, NAME, ':')) { \
cerr << "error: expected |" #NAME "| field in '" << line << "'" << endl; \
}
string index, version, component, session, uid, package, package_version;
string pid, finalized, timestamp, item_count;
READ_TOKEN(index);
READ_TOKEN(version);
READ_TOKEN(component);
READ_TOKEN(session);
READ_TOKEN(uid);
READ_TOKEN(package);
READ_TOKEN(package_version);
READ_TOKEN(pid);
READ_TOKEN(finalized);
READ_TOKEN(timestamp);
READ_TOKEN(item_count);
MediaMetrics metrics;
parse_metrics(line, &metrics);
// handle specific package and component selection
if (selected_one.size() && package != selected_one &&
component != selected_one) {
if (selected_one.size() && metrics.package != selected_one &&
metrics.component != selected_one) {
return;
}
if (std::find(excluded_ones.begin(), excluded_ones.end(), package) !=
if (std::find(excluded_ones.begin(), excluded_ones.end(), metrics.package) !=
excluded_ones.end()) {
return;
}
if (std::find(excluded_ones.begin(), excluded_ones.end(), component) !=
excluded_ones.end()) {
if (std::find(excluded_ones.begin(), excluded_ones.end(),
metrics.component) != excluded_ones.end()) {
return;
}
cout << "===================================================================="
"============="
<< endl;
char timebuf[64];
time_t time_s = stoll(timestamp) / 1000000000LL;
strftime(timebuf, sizeof(timebuf), "%a %b %e %H:%M:%S", localtime(&time_s));
cout << timebuf << " timestamp: " << time_s << endl << endl;
if (is_q_metrics(line)) {
char timebuf[128];
time_t time_s = stoll(metrics.timestamp) / 1000000000LL;
strftime(timebuf, sizeof(timebuf), "%a %b %e %H:%M:%S", localtime(&time_s));
cout << timebuf << " timestamp: " << time_s << endl << endl;
} else {
cout << metrics.timestamp << endl << endl;
}
cout << " "
<< "APK PACKAGE -> METRICS COMPONENT" << endl;
cout << " " << package << " -> " << component << " | uid:" << uid
<< " pid:" << pid << endl;
cout << " " << metrics.package << " -> " << metrics.component << endl;
cout << "\tuid:" << metrics.uid << " pid:" << metrics.pid << endl;
size_t items = std::stoi(item_count);
for (size_t i = 0; i < items; i++) {
string parameter, value;
if (!std::getline(fields, parameter, '=')) {
cerr << "error: expected |parameter| in item " << i << " in '" << line
<< "'" << endl;
}
if (!std::getline(fields, value, ':')) {
cerr << "error: expected |value| in item " << i << " in '" << line << "'"
<< endl;
}
if (parameter == "serialized_metrics") {
for (auto property : metrics.properties) {
string key = property.first;
string value = property.second;
if (key == "serialized_metrics") {
string decoded = Base64::Decode(value);
if (component == "drm.vendor.Google.WidevineCDM") {
drm_metrics::WvCdmMetrics metrics;
if (!metrics.ParseFromString(decoded)) {
if (metrics.component == "drm.vendor.google.widevinecdm") {
drm_metrics::WvCdmMetrics wv_metrics;
if (!wv_metrics.ParseFromString(decoded)) {
cerr << "failed to parse proto string" << endl;
} else {
string result;
FormatWvCdmMetrics(metrics, result);
FormatWvCdmMetrics(wv_metrics, result);
cout << endl << result << endl;
}
} else {
android::drm_metrics::DrmFrameworkMetrics metrics;
if (!metrics.ParseFromString(decoded)) {
android::drm_metrics::DrmFrameworkMetrics fw_metrics;
if (!fw_metrics.ParseFromString(decoded)) {
cerr << "failed to parse proto string" << endl;
} else {
string result;
FormatDrmFrameworkMetrics(metrics, result);
mediadrm_metrics::FormatDrmFrameworkMetrics(fw_metrics, result);
cout << endl << result << endl;
}
}
} else {
cout << " " << parameter << ": " << value << endl;
cout << " " << key << ": " << value << endl;
}
}
}
@@ -170,7 +162,7 @@ int main(int argc, char **argv) {
} else if (arg == "--no-gms") {
metrics_dump::excluded_ones.push_back("com.google.android.gms");
} else if (arg == "--widevine") {
metrics_dump::selected_one = "drm.vendor.Google.WidevineCDM";
metrics_dump::selected_one = "drm.vendor.google.widevinecdm";
} else if (arg == "--mediadrm") {
metrics_dump::selected_one = "mediadrm";
} else if (i == argc - 1) {
@@ -185,9 +177,12 @@ int main(int argc, char **argv) {
} else {
// args with a parameter
if (arg == "--exclude") {
metrics_dump::excluded_ones.push_back(argv[++i]);
string lowercase_argv = argv[++i];
metrics_dump::to_lower(lowercase_argv);
metrics_dump::excluded_ones.push_back(lowercase_argv);
} else if (arg == "--select") {
metrics_dump::selected_one = argv[++i];
metrics_dump::to_lower(metrics_dump::selected_one);
} else {
usage();
}

View File

@@ -0,0 +1,117 @@
// Copyright 2020 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// Format metric output from |adb shell dumpsys media.metrics|
#include "parse_metrics.h"
#include <algorithm>
#include <map>
#include <regex>
#include <string>
#include <utility>
#include <vector>
namespace metrics_dump {
using std::string;
using std::vector;
void to_lower(string& lower) {
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
}
void parse_metrics(const string& line, MediaMetrics* metrics) {
string regex_delimiters;
bool is_r_metrics = is_q_metrics(line) == false;
int properties_index = 0;
if (is_r_metrics) {
// Format of mediadrm metrics (R and forward):
// index: {key, (timestamp), (package, pid, uid), (properties, ...)}
// properties in this format: (key=value, key=value, ...)
// key(component) and packagename may contain '.'
// timestamp contains ':' (month-day h:m:s.n)
// Note: we want to preserve spaces within a metric field
regex_delimiters = "[^()|,|^{}]+";
properties_index = kPropertiesR;
} else {
// Format of mediadrm metrics before R release:
// index: [version:component:session:uid:package:package_version:
// pid:finalized:timestamp:item_count:properties:]
// properties in this format: :key=value:key=value:...:
regex_delimiters = "[^:|^[\\]|\\s]+";
properties_index = kPropertiesQ;
}
std::regex delimiters(regex_delimiters);
auto tokens_begin =
std::sregex_iterator(line.begin(), line.end(), delimiters);
auto tokens_end = std::sregex_iterator();
vector<string> tokens;
vector<string> properties;
int count = 0;
for (std::sregex_iterator itr = tokens_begin; itr != tokens_end; ++itr) {
std::smatch match = *itr;
string token = match.str();
// timestamp contains space, so we do not want to filter
// all spaces; here we filter out token that contains just spaces
if (token.find_first_not_of(' ') != string::npos) {
if (count < properties_index) {
tokens.push_back(token);
count++;
} else {
// trim leading spaces in token
size_t first_non_space = token.find_first_not_of(' ');
string trimmed_token = token.substr(first_non_space);
properties.push_back(trimmed_token);
}
}
}
string component, package;
if (is_r_metrics) {
component = tokens[kComponentR];
package = tokens[kPackageR];
to_lower(component);
to_lower(package);
metrics->component = component;
metrics->package = package;
metrics->pid = tokens[kPidR];
metrics->timestamp = tokens[kTimestampR];
metrics->uid = tokens[kUidR];
// the following fields are not used by the tool
metrics->index = tokens[kIndexR];
} else {
component = tokens[kComponentQ];
package = tokens[kPackageQ];
to_lower(component);
to_lower(package);
metrics->component = component;
metrics->package = package;
metrics->timestamp = tokens[kTimestampQ];
metrics->uid = tokens[kUidQ];
metrics->pid = tokens[kPidQ];
// the following fields are not used by the tool
metrics->finalized = tokens[kFinalizedQ];
metrics->index = tokens[kIndexQ];
metrics->item_count = tokens[kItemCountQ];
metrics->package_version = tokens[kPackageVersionQ];
metrics->session = tokens[kSessionQ];
metrics->version = tokens[kVersionQ];
}
for (auto property : properties) {
size_t equal_pos = property.find_first_of('=');
string key = property.substr(0, equal_pos);
string value = property.substr(equal_pos + 1);
metrics->properties.push_back(std::pair<string, string>(key, value));
}
}
} // namespace metrics_dump

View File

@@ -17,7 +17,9 @@
namespace wv_metrics {
using namespace drm_metrics;
// The following declaration causes
// ambiguous namespace error in metrics_dump.cpp:
// using namespace drm_metrics;
using std::string;
using std::to_string;
@@ -35,7 +37,7 @@ string FormatOEMCryptoResult(int oemcrypto_result) {
return " " + os.str();
}
string FormatOEMCryptoInitializeMode(const ValueMetric& vm) {
string FormatOEMCryptoInitializeMode(const drm_metrics::ValueMetric& vm) {
std::map<int, string> translations = {
{0, "USING_IN_APP"},
{1, "FORCING_L3"},
@@ -53,18 +55,26 @@ string FormatOEMCryptoInitializeMode(const ValueMetric& vm) {
{13, "USING_L1_INSTALLED_KEYBOX"},
{14, "USING_L3_INVALID_L1"},
{15, "USING_L1_WITH_PROVISIONING_3_0"},
{16, "L3_INITIALIZATION_FAILED"}};
{16, "L3_INITIALIZATION_FAILED"},
{17, "L3_RNG_FAILED"},
{18, "L3_SAVE_DEVICE_KEYS_FAILED"},
{19, "L3_READ_DEVICE_KEYS_FAILED"},
{20, "L3_VERIFY_DEVICE_KEYS_FAILED"}};
return translations[vm.int_value()];
}
string FormatOEMCryptoHdcpCapability(const ValueMetric& vm) {
std::map<int, string> translations = {{0, "HDCP_NONE"}, {1, "HDCP_V1"},
{2, "HDCP_V2"}, {3, "HDCP_V2_1"},
{4, "HDCP_V2_2"}, {0xff, "NO_OUTPUT"}};
string FormatOEMCryptoHdcpCapability(const drm_metrics::ValueMetric& vm) {
std::map<int, string> translations = {{0, "HDCP_NONE"},
{1, "HDCP_V1"},
{2, "HDCP_V2"},
{3, "HDCP_V2_1"},
{4, "HDCP_V2_2"},
{5, "HDCP_V2_3"},
{0xff, "HDCP_NO_DIGITAL_OUTPUT"}};
return translations[vm.int_value()];
}
string FormatOEMCryptoProvisioningMethod(const ValueMetric& vm) {
string FormatOEMCryptoProvisioningMethod(const drm_metrics::ValueMetric& vm) {
std::map<int, string> translations = {{0, "PROVISIONING_ERROR"},
{1, "DRM_CERTIFICATE"},
{2, "KEYBOX"},
@@ -72,7 +82,7 @@ string FormatOEMCryptoProvisioningMethod(const ValueMetric& vm) {
return translations[vm.int_value()];
}
string FormatAttributes(const Attributes& attributes) {
string FormatAttributes(const drm_metrics::Attributes& attributes) {
string result;
if (attributes.has_error_code()) {
result.append("error_code:");
@@ -137,7 +147,7 @@ string FormatAttributes(const Attributes& attributes) {
}
}
string FormatCounterMetric(const CounterMetric& cm) {
string FormatCounterMetric(const drm_metrics::CounterMetric& cm) {
string result;
if (cm.has_count()) {
result = string("count=") + to_string(cm.count());
@@ -148,7 +158,7 @@ string FormatCounterMetric(const CounterMetric& cm) {
return result;
}
string FormatDistributionMetric(const DistributionMetric& dm) {
string FormatDistributionMetric(const drm_metrics::DistributionMetric& dm) {
string result;
if (dm.has_min()) {
std::ostringstream buffer;
@@ -183,7 +193,7 @@ string FormatDistributionMetric(const DistributionMetric& dm) {
return result;
}
string FormatValueMetric(const ValueMetric& vm) {
string FormatValueMetric(const drm_metrics::ValueMetric& vm) {
string result;
if (vm.has_error_code()) {
result.append("error(" + to_string(vm.error_code()));
@@ -257,7 +267,7 @@ string FormatValueMetric(const ValueMetric& vm) {
FormatCryptoMetrics(metrics.NAME(), INDENT + kIndentPerLine, result); \
}
void FormatCryptoMetrics(const WvCdmMetrics_CryptoMetrics metrics,
void FormatCryptoMetrics(const drm_metrics::WvCdmMetrics_CryptoMetrics metrics,
const string& indent, string& result) {
// Crypto Session Metrics.
FORMAT_OPTIONAL_VALUE(crypto_session_security_level, indent);
@@ -286,6 +296,12 @@ void FormatCryptoMetrics(const WvCdmMetrics_CryptoMetrics metrics,
FORMAT_REPEATED_DISTRIBUTION(usage_table_header_update_entry_time_us, indent);
FORMAT_REPEATED_COUNTER(usage_table_header_load_entry, indent);
// Usage Table LRU Metrics
FORMAT_OPTIONAL_VALUE(usage_table_header_lru_usage_info_count, indent);
FORMAT_OPTIONAL_VALUE(usage_table_header_lru_offline_license_count, indent);
FORMAT_OPTIONAL_VALUE(usage_table_header_lru_evicted_entry_staleness_s,
indent);
FORMAT_OPTIONAL_VALUE(usage_table_header_lru_evicted_entry_type, indent);
// OemCrypto metrics.
FORMAT_OPTIONAL_VALUE(oemcrypto_api_version, indent);
FORMAT_REPEATED_COUNTER(oemcrypto_close_session, indent);
@@ -342,10 +358,24 @@ void FormatCryptoMetrics(const WvCdmMetrics_CryptoMetrics metrics,
FORMAT_OPTIONAL_VALUE(oemcrypto_set_sandbox, indent);
FORMAT_REPEATED_COUNTER(oemcrypto_set_decrypt_hash, indent);
FORMAT_OPTIONAL_VALUE(oemcrypto_resource_rating_tier, indent);
FORMAT_REPEATED_DISTRIBUTION(oemcrypto_prep_and_sign_license_request_time_us,
indent);
FORMAT_REPEATED_DISTRIBUTION(oemcrypto_prep_and_sign_renewal_request_time_us,
indent);
FORMAT_REPEATED_DISTRIBUTION(
oemcrypto_prep_and_sign_provisioning_request_time_us, indent);
FORMAT_REPEATED_DISTRIBUTION(oemcrypto_load_license_time_us, indent);
FORMAT_REPEATED_DISTRIBUTION(oemcrypto_load_renewal_time_us, indent);
FORMAT_REPEATED_DISTRIBUTION(oemcrypto_load_provisioning_time_us, indent);
FORMAT_OPTIONAL_VALUE(oemcrypto_minor_api_version, indent);
;
FORMAT_OPTIONAL_VALUE(oemcrypto_maximum_usage_table_header_size, indent);
}
void FormatSessionMetrics(const WvCdmMetrics_SessionMetrics& metrics,
const string& indent, string& result) {
void FormatSessionMetrics(
const drm_metrics::WvCdmMetrics_SessionMetrics& metrics,
const string& indent, string& result) {
FORMAT_OPTIONAL_VALUE(session_id, indent);
FORMAT_OPTIONAL_CRYPTO_METRICS(crypto_metrics, indent);
FORMAT_OPTIONAL_VALUE(cdm_session_life_span_ms, indent);
@@ -358,7 +388,7 @@ void FormatSessionMetrics(const WvCdmMetrics_SessionMetrics& metrics,
FORMAT_OPTIONAL_VALUE(license_service_version, indent);
}
void FormatEngineMetrics(const WvCdmMetrics_EngineMetrics& metrics,
void FormatEngineMetrics(const drm_metrics::WvCdmMetrics_EngineMetrics& metrics,
const string& indent, string& result) {
FORMAT_OPTIONAL_CRYPTO_METRICS(crypto_metrics, indent);
@@ -366,8 +396,6 @@ void FormatEngineMetrics(const WvCdmMetrics_EngineMetrics& metrics,
FORMAT_OPTIONAL_INITIALIZATION_MODE(oemcrypto_initialization_mode, indent);
FORMAT_OPTIONAL_VALUE(oemcrypto_l1_api_version, indent);
FORMAT_OPTIONAL_VALUE(oemcrypto_l1_min_api_version, indent);
FORMAT_OPTIONAL_VALUE(level3_oemcrypto_initialization_error, indent);
FORMAT_OPTIONAL_VALUE(previous_oemcrypto_initialization_failure, indent);
// CdmEngine Metrics.
FORMAT_OPTIONAL_VALUE(app_package_name, indent);
@@ -395,9 +423,12 @@ void FormatEngineMetrics(const WvCdmMetrics_EngineMetrics& metrics,
FORMAT_REPEATED_COUNTER(cdm_engine_remove_usage_info, indent);
FORMAT_REPEATED_DISTRIBUTION(cdm_engine_restore_key_time_us, indent);
FORMAT_REPEATED_COUNTER(cdm_engine_unprovision, indent);
FORMAT_OPTIONAL_VALUE(level3_oemcrypto_initialization_error, indent);
FORMAT_OPTIONAL_VALUE(previous_oemcrypto_initialization_failure, indent);
}
void FormatWvCdmMetrics(const WvCdmMetrics& metrics, string& result) {
void FormatWvCdmMetrics(const drm_metrics::WvCdmMetrics& metrics,
string& result) {
string indent = kIndentPerLine;
string next_indent = indent + kIndentPerLine;