From 9c1debe330006b2b4d5c08a55905789c57369609 Mon Sep 17 00:00:00 2001 From: Yao Chen Date: Mon, 19 Feb 2018 14:39:19 -0800 Subject: [PATCH] Add annotation to atoms that represent a state change in atoms.proto + A state change atom can have one exclusive state field, and any number of primary key fields. When there is primary key in the atom, it means the state belongs to the primary key. For example, message UidProcessStateChanged { optional int32 uid = 1 [(stateFieldOption).option = PRIMARY]; optional android.app.ProcessStateEnum state = 2 [(stateFieldOption).option = EXCLUSIVE]; } When there is no primary key fields in the atom, the state is global. For example, message ScreenStateChanged { optional android.view.DisplayStateEnum state = 1 [(stateFieldOption).option = EXCLUSIVE]; } + The annotation is consumed by stats_log_api_gen to generate a static map from the state atoms to its primary fields, and exclusive fields + stats_log.proto is splitted into 2 proto files, because statsd needs proto lite, and c++ lite proto library cannot properly ignore the field options which requires full proto. This CL doesn't change any logic in the statsd yet. A separate CL will use the field option information to correctly track the state. Test: added unit tests in stats_log_api_gen_test. and statsd_test pases. Change-Id: I9e8a979fe81ba60efd4d854bb7087ce4b2b147ec --- cmds/statsd/Android.bp | 4 + cmds/statsd/Android.mk | 20 +++- cmds/statsd/src/HashableDimensionKey.cpp | 2 + cmds/statsd/src/atom_field_options.proto | 70 +++++++++++ cmds/statsd/src/atoms.proto | 10 +- cmds/statsd/src/guardrail/StatsdStats.h | 2 +- cmds/statsd/src/logd/LogEvent.cpp | 7 +- cmds/statsd/src/logd/LogEvent.h | 31 +++-- cmds/statsd/src/matchers/matcher_util.h | 4 +- cmds/statsd/src/packages/UidMap.h | 2 +- cmds/statsd/src/perfetto/perfetto_config.proto | 1 - cmds/statsd/src/stats_log.proto | 112 +---------------- cmds/statsd/src/stats_log_common.proto | 132 +++++++++++++++++++++ cmds/statsd/src/stats_log_util.h | 2 +- cmds/statsd/src/stats_util.h | 2 +- cmds/statsd/src/statsd_config.proto | 1 - cmds/statsd/src/subscriber/SubscriberReporter.cpp | 2 + cmds/statsd/src/subscriber/SubscriberReporter.h | 1 - cmds/statsd/tests/FieldValue_test.cpp | 29 ++--- cmds/statsd/tests/LogEntryMatcher_test.cpp | 10 +- cmds/statsd/tests/LogEvent_test.cpp | 12 +- .../condition/SimpleConditionTracker_test.cpp | 4 +- cmds/statsd/tests/e2e/Attribution_e2e_test.cpp | 39 +++--- .../tests/e2e/DimensionInCondition_e2e_test.cpp | 48 ++++---- .../tests/e2e/MetricConditionLink_e2e_test.cpp | 8 +- .../statsd/tests/e2e/WakelockDuration_e2e_test.cpp | 12 +- cmds/statsd/tests/statsd_test_util.cpp | 26 ++-- cmds/statsd/tests/statsd_test_util.h | 23 ++-- cmds/statsd/tools/Android.mk | 2 +- cmds/statsd/tools/dogfood/Android.mk | 1 - cmds/statsd/tools/loadtest/Android.mk | 10 +- .../android/statsd/loadtest/LoadtestActivity.java | 2 +- .../statsd/loadtest/StatsdStatsRecorder.java | 5 +- tools/stats_log_api_gen/Android.bp | 4 + tools/stats_log_api_gen/Collation.cpp | 40 ++++++- tools/stats_log_api_gen/Collation.h | 3 + tools/stats_log_api_gen/main.cpp | 38 ++++++ tools/stats_log_api_gen/test.proto | 66 +++++++++++ tools/stats_log_api_gen/test_collation.cpp | 13 ++ 39 files changed, 536 insertions(+), 264 deletions(-) create mode 100644 cmds/statsd/src/atom_field_options.proto create mode 100644 cmds/statsd/src/stats_log_common.proto diff --git a/cmds/statsd/Android.bp b/cmds/statsd/Android.bp index a5eae15b50da..b5660995fa36 100644 --- a/cmds/statsd/Android.bp +++ b/cmds/statsd/Android.bp @@ -21,6 +21,7 @@ cc_library_host_shared { name: "libstats_proto_host", srcs: [ "src/atoms.proto", + "src/atom_field_options.proto", ], shared_libs: [ @@ -30,6 +31,9 @@ cc_library_host_shared { proto: { type: "full", export_proto_headers: true, + include_dirs: [ + "external/protobuf/src", + ], }, export_shared_lib_headers: [ diff --git a/cmds/statsd/Android.mk b/cmds/statsd/Android.mk index 740fdc0af5f1..244fbce132ac 100644 --- a/cmds/statsd/Android.mk +++ b/cmds/statsd/Android.mk @@ -17,9 +17,8 @@ LOCAL_PATH:= $(call my-dir) statsd_common_src := \ ../../core/java/android/os/IStatsCompanionService.aidl \ ../../core/java/android/os/IStatsManager.aidl \ - src/stats_log.proto \ + src/stats_log_common.proto \ src/statsd_config.proto \ - src/atoms.proto \ src/FieldValue.cpp \ src/stats_log_util.cpp \ src/anomaly/AnomalyMonitor.cpp \ @@ -168,6 +167,9 @@ LOCAL_CFLAGS += \ LOCAL_SRC_FILES := \ $(statsd_common_src) \ + src/atom_field_options.proto \ + src/atoms.proto \ + src/stats_log.proto \ tests/AnomalyMonitor_test.cpp \ tests/anomaly/AnomalyTracker_test.cpp \ tests/ConfigManager_test.cpp \ @@ -202,9 +204,13 @@ LOCAL_STATIC_LIBRARIES := \ $(statsd_common_static_libraries) \ libgmock -LOCAL_SHARED_LIBRARIES := $(statsd_common_shared_libraries) +LOCAL_PROTOC_OPTIMIZE_TYPE := full -LOCAL_PROTOC_OPTIMIZE_TYPE := lite +LOCAL_PROTOC_FLAGS := \ + -Iexternal/protobuf/src + +LOCAL_SHARED_LIBRARIES := $(statsd_common_shared_libraries) \ + libprotobuf-cpp-full include $(BUILD_NATIVE_TEST) @@ -217,6 +223,7 @@ LOCAL_MODULE := statsdprotolite LOCAL_SRC_FILES := \ src/stats_log.proto \ + src/stats_log_common.proto \ src/statsd_config.proto \ src/perfetto/perfetto_config.proto \ src/atoms.proto @@ -226,6 +233,9 @@ LOCAL_PROTOC_OPTIMIZE_TYPE := lite LOCAL_STATIC_JAVA_LIBRARIES := \ platformprotoslite +LOCAL_PROTOC_FLAGS := \ + -Iexternal/protobuf/src + include $(BUILD_STATIC_JAVA_LIBRARY) ############################## @@ -261,8 +271,6 @@ LOCAL_SHARED_LIBRARIES := $(statsd_common_shared_libraries) \ libgtest_prod \ libstatslog -LOCAL_PROTOC_OPTIMIZE_TYPE := lite - LOCAL_MODULE_TAGS := eng tests include $(BUILD_NATIVE_BENCHMARK) diff --git a/cmds/statsd/src/HashableDimensionKey.cpp b/cmds/statsd/src/HashableDimensionKey.cpp index d901bd669591..3c69f9ed683f 100644 --- a/cmds/statsd/src/HashableDimensionKey.cpp +++ b/cmds/statsd/src/HashableDimensionKey.cpp @@ -22,6 +22,8 @@ namespace android { namespace os { namespace statsd { + +using std::string; using std::vector; android::hash_t hashDimension(const HashableDimensionKey& value) { diff --git a/cmds/statsd/src/atom_field_options.proto b/cmds/statsd/src/atom_field_options.proto new file mode 100644 index 000000000000..19d00b7dc4a5 --- /dev/null +++ b/cmds/statsd/src/atom_field_options.proto @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +syntax = "proto2"; + +package android.os.statsd; +option java_package = "com.android.os"; +option java_multiple_files = true; +option java_outer_classname = "AtomFieldOptions"; + +import "google/protobuf/descriptor.proto"; + +enum StateField { + // Default value for fields that are not primary or exclusive state. + STATE_FIELD_UNSET = 0; + // Fields that represent the key that the state belongs to. + PRIMARY = 1; + // The field that represents the state. It's an exclusive state. + EXCLUSIVE = 2; +} + +// Used to annotate an atom that reprsents a state change. A state change atom must have exactly ONE +// exclusive state field, and any number of primary key fields. +// For example, +// message UidProcessStateChanged { +// optional int32 uid = 1 [(stateFieldOption).option = PRIMARY]; +// optional android.app.ProcessStateEnum state = 2 [(stateFieldOption).option = EXCLUSIVE]; +// } +// Each of this UidProcessStateChanged atom represents a state change for a specific uid. +// A new state automatically overrides the previous state. +// +// If the atom has 2 or more primary fields, it means the combination of the primary fields are +// the primary key. +// For example: +// message ThreadStateChanged { +// optional int32 pid = 1 [(stateFieldOption).option = PRIMARY]; +// optional int32 tid = 2 [(stateFieldOption).option = PRIMARY]; +// optional int32 state = 3 [(stateFieldOption).option = EXCLUSIVE]; +// } +// +// Sometimes, there is no primary key field, when the state is GLOBAL. +// For example, +// +// message ScreenStateChanged { +// optional android.view.DisplayStateEnum state = 1 [(stateFieldOption).option = EXCLUSIVE]; +// } +// +// Only fields of primary types can be annotated. AttributionNode cannot be primary keys (and they +// usually are not). +message StateAtomFieldOption { + optional StateField option = 1 [default = STATE_FIELD_UNSET]; +} + +extend google.protobuf.FieldOptions { + // Flags to decorate an atom that presents a state change. + optional StateAtomFieldOption stateFieldOption = 50000; +} \ No newline at end of file diff --git a/cmds/statsd/src/atoms.proto b/cmds/statsd/src/atoms.proto index 5d6953c58f84..9af31b9ead80 100644 --- a/cmds/statsd/src/atoms.proto +++ b/cmds/statsd/src/atoms.proto @@ -21,6 +21,7 @@ package android.os.statsd; option java_package = "com.android.os"; option java_outer_classname = "AtomsProto"; +import "frameworks/base/cmds/statsd/src/atom_field_options.proto"; import "frameworks/base/core/proto/android/app/enums.proto"; import "frameworks/base/core/proto/android/os/enums.proto"; import "frameworks/base/core/proto/android/server/enums.proto"; @@ -179,7 +180,7 @@ message AttributionNode { */ message ScreenStateChanged { // New screen state, from frameworks/base/core/proto/android/view/enums.proto. - optional android.view.DisplayStateEnum state = 1; + optional android.view.DisplayStateEnum state = 1 [(stateFieldOption).option = EXCLUSIVE]; } /** @@ -189,10 +190,10 @@ message ScreenStateChanged { * frameworks/base/services/core/java/com/android/server/am/BatteryStatsService.java */ message UidProcessStateChanged { - optional int32 uid = 1; // TODO: should be a string tagged w/ uid annotation + optional int32 uid = 1 [(stateFieldOption).option = PRIMARY]; // The state, from frameworks/base/core/proto/android/app/enums.proto. - optional android.app.ProcessStateEnum state = 2; + optional android.app.ProcessStateEnum state = 2 [(stateFieldOption).option = EXCLUSIVE]; } /** @@ -1522,5 +1523,4 @@ message RemainingBatteryCapacity { */ message FullBatteryCapacity { optional int32 capacity_uAh = 1; -} - +} \ No newline at end of file diff --git a/cmds/statsd/src/guardrail/StatsdStats.h b/cmds/statsd/src/guardrail/StatsdStats.h index 7baa5e57679e..8c16e4e9c2eb 100644 --- a/cmds/statsd/src/guardrail/StatsdStats.h +++ b/cmds/statsd/src/guardrail/StatsdStats.h @@ -16,7 +16,7 @@ #pragma once #include "config/ConfigKey.h" -#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" +#include "frameworks/base/cmds/statsd/src/stats_log_common.pb.h" #include "statslog.h" #include diff --git a/cmds/statsd/src/logd/LogEvent.cpp b/cmds/statsd/src/logd/LogEvent.cpp index f07fc66dbcbb..d282b86f52bd 100644 --- a/cmds/statsd/src/logd/LogEvent.cpp +++ b/cmds/statsd/src/logd/LogEvent.cpp @@ -26,9 +26,10 @@ namespace os { namespace statsd { using namespace android::util; +using android::util::ProtoOutputStream; using std::ostringstream; using std::string; -using android::util::ProtoOutputStream; +using std::vector; LogEvent::LogEvent(log_msg& msg) { mContext = @@ -130,7 +131,7 @@ bool LogEvent::write(float value) { return false; } -bool LogEvent::write(const std::vector& nodes) { +bool LogEvent::write(const std::vector& nodes) { if (mContext) { if (android_log_write_list_begin(mContext) < 0) { return false; @@ -148,7 +149,7 @@ bool LogEvent::write(const std::vector& nodes) { return false; } -bool LogEvent::write(const AttributionNode& node) { +bool LogEvent::write(const AttributionNodeInternal& node) { if (mContext) { if (android_log_write_list_begin(mContext) < 0) { return false; diff --git a/cmds/statsd/src/logd/LogEvent.h b/cmds/statsd/src/logd/LogEvent.h index b3084d54d848..24d624d9d9be 100644 --- a/cmds/statsd/src/logd/LogEvent.h +++ b/cmds/statsd/src/logd/LogEvent.h @@ -17,7 +17,6 @@ #pragma once #include "FieldValue.h" -#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" #include #include @@ -32,8 +31,26 @@ namespace android { namespace os { namespace statsd { -using std::string; -using std::vector; +struct AttributionNodeInternal { + void set_uid(int32_t id) { + mUid = id; + } + + void set_tag(const std::string& value) { + mTag = value; + } + + int32_t uid() const { + return mUid; + } + + const std::string& tag() const { + return mTag; + } + + int32_t mUid; + std::string mTag; +}; /** * Wrapper for the log_msg structure. */ @@ -89,15 +106,15 @@ public: bool write(int32_t value); bool write(uint64_t value); bool write(int64_t value); - bool write(const string& value); + bool write(const std::string& value); bool write(float value); - bool write(const std::vector& nodes); - bool write(const AttributionNode& node); + bool write(const std::vector& nodes); + bool write(const AttributionNodeInternal& node); /** * Return a string representation of this event. */ - string ToString() const; + std::string ToString() const; /** * Write this object to a ProtoOutputStream. diff --git a/cmds/statsd/src/matchers/matcher_util.h b/cmds/statsd/src/matchers/matcher_util.h index 872cd8e64575..ae946d16b352 100644 --- a/cmds/statsd/src/matchers/matcher_util.h +++ b/cmds/statsd/src/matchers/matcher_util.h @@ -24,10 +24,10 @@ #include #include #include -#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" +#include "frameworks/base/cmds/statsd/src/stats_log_common.pb.h" #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" -#include "stats_util.h" #include "packages/UidMap.h" +#include "stats_util.h" namespace android { namespace os { diff --git a/cmds/statsd/src/packages/UidMap.h b/cmds/statsd/src/packages/UidMap.h index f1da452bf5b9..c41e0aaca3d2 100644 --- a/cmds/statsd/src/packages/UidMap.h +++ b/cmds/statsd/src/packages/UidMap.h @@ -18,7 +18,7 @@ #include "config/ConfigKey.h" #include "config/ConfigListener.h" -#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" +#include "frameworks/base/cmds/statsd/src/stats_log_common.pb.h" #include "packages/PackageInfoListener.h" #include diff --git a/cmds/statsd/src/perfetto/perfetto_config.proto b/cmds/statsd/src/perfetto/perfetto_config.proto index dc868f997a63..56d12f8d81d4 100644 --- a/cmds/statsd/src/perfetto/perfetto_config.proto +++ b/cmds/statsd/src/perfetto/perfetto_config.proto @@ -15,7 +15,6 @@ */ syntax = "proto2"; -option optimize_for = LITE_RUNTIME; package perfetto.protos; diff --git a/cmds/statsd/src/stats_log.proto b/cmds/statsd/src/stats_log.proto index b427485fd705..272e90be662d 100644 --- a/cmds/statsd/src/stats_log.proto +++ b/cmds/statsd/src/stats_log.proto @@ -15,7 +15,6 @@ */ syntax = "proto2"; -option optimize_for = LITE_RUNTIME; package android.os.statsd; @@ -23,6 +22,7 @@ option java_package = "com.android.os"; option java_outer_classname = "StatsLog"; import "frameworks/base/cmds/statsd/src/atoms.proto"; +import "frameworks/base/cmds/statsd/src/stats_log_common.proto"; message DimensionsValue { optional int32 field = 1; @@ -115,33 +115,6 @@ message GaugeMetricData { repeated GaugeBucketInfo bucket_info = 3; } -message UidMapping { - message PackageInfoSnapshot { - message PackageInfo { - optional string name = 1; - - optional int64 version = 2; - - optional int32 uid = 3; - } - optional int64 elapsed_timestamp_nanos = 1; - - repeated PackageInfo package_info = 2; - } - repeated PackageInfoSnapshot snapshots = 1; - - message Change { - optional bool deletion = 1; - - optional int64 elapsed_timestamp_nanos = 2; - optional string app = 3; - optional int32 uid = 4; - - optional int64 version = 5; - } - repeated Change changes = 2; -} - message StatsLogReport { optional int64 metric_id = 1; @@ -191,87 +164,4 @@ message ConfigMetricsReportList { optional ConfigKey config_key = 1; repeated ConfigMetricsReport reports = 2; -} - -message StatsdStatsReport { - optional int32 stats_begin_time_sec = 1; - - optional int32 stats_end_time_sec = 2; - - message MatcherStats { - optional int64 id = 1; - optional int32 matched_times = 2; - } - - message ConditionStats { - optional int64 id = 1; - optional int32 max_tuple_counts = 2; - } - - message MetricStats { - optional int64 id = 1; - optional int32 max_tuple_counts = 2; - } - - message AlertStats { - optional int64 id = 1; - optional int32 alerted_times = 2; - } - - message ConfigStats { - optional int32 uid = 1; - optional int64 id = 2; - optional int32 creation_time_sec = 3; - optional int32 deletion_time_sec = 4; - optional int32 metric_count = 5; - optional int32 condition_count = 6; - optional int32 matcher_count = 7; - optional int32 alert_count = 8; - optional bool is_valid = 9; - - repeated int32 broadcast_sent_time_sec = 10; - repeated int32 data_drop_time_sec = 11; - repeated int32 dump_report_time_sec = 12; - repeated MatcherStats matcher_stats = 13; - repeated ConditionStats condition_stats = 14; - repeated MetricStats metric_stats = 15; - repeated AlertStats alert_stats = 16; - } - - repeated ConfigStats config_stats = 3; - - message AtomStats { - optional int32 tag = 1; - optional int32 count = 2; - } - - repeated AtomStats atom_stats = 7; - - message UidMapStats { - optional int32 snapshots = 1; - optional int32 changes = 2; - optional int32 bytes_used = 3; - optional int32 dropped_snapshots = 4; - optional int32 dropped_changes = 5; - } - optional UidMapStats uidmap_stats = 8; - - message AnomalyAlarmStats { - optional int32 alarms_registered = 1; - } - optional AnomalyAlarmStats anomaly_alarm_stats = 9; - - message PulledAtomStats { - optional int32 atom_id = 1; - optional int64 total_pull = 2; - optional int64 total_pull_from_cache = 3; - optional int64 min_pull_interval_sec = 4; - } - repeated PulledAtomStats pulled_atom_stats = 10; - - message LoggerErrorStats { - optional int32 logger_disconnection_sec = 1; - optional int32 error_code = 2; - } - repeated LoggerErrorStats logger_error_stats = 11; } \ No newline at end of file diff --git a/cmds/statsd/src/stats_log_common.proto b/cmds/statsd/src/stats_log_common.proto new file mode 100644 index 000000000000..aeecd23c65f4 --- /dev/null +++ b/cmds/statsd/src/stats_log_common.proto @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +syntax = "proto2"; + +package android.os.statsd; + +option java_package = "com.android.os"; +option java_outer_classname = "StatsLogCommon"; + +message UidMapping { + message PackageInfoSnapshot { + message PackageInfo { + optional string name = 1; + + optional int64 version = 2; + + optional int32 uid = 3; + } + optional int64 elapsed_timestamp_nanos = 1; + + repeated PackageInfo package_info = 2; + } + repeated PackageInfoSnapshot snapshots = 1; + + message Change { + optional bool deletion = 1; + + optional int64 elapsed_timestamp_nanos = 2; + optional string app = 3; + optional int32 uid = 4; + + optional int64 version = 5; + } + repeated Change changes = 2; +} + +message StatsdStatsReport { + optional int32 stats_begin_time_sec = 1; + + optional int32 stats_end_time_sec = 2; + + message MatcherStats { + optional int64 id = 1; + optional int32 matched_times = 2; + } + + message ConditionStats { + optional int64 id = 1; + optional int32 max_tuple_counts = 2; + } + + message MetricStats { + optional int64 id = 1; + optional int32 max_tuple_counts = 2; + } + + message AlertStats { + optional int64 id = 1; + optional int32 alerted_times = 2; + } + + message ConfigStats { + optional int32 uid = 1; + optional int64 id = 2; + optional int32 creation_time_sec = 3; + optional int32 deletion_time_sec = 4; + optional int32 metric_count = 5; + optional int32 condition_count = 6; + optional int32 matcher_count = 7; + optional int32 alert_count = 8; + optional bool is_valid = 9; + + repeated int32 broadcast_sent_time_sec = 10; + repeated int32 data_drop_time_sec = 11; + repeated int32 dump_report_time_sec = 12; + repeated MatcherStats matcher_stats = 13; + repeated ConditionStats condition_stats = 14; + repeated MetricStats metric_stats = 15; + repeated AlertStats alert_stats = 16; + } + + repeated ConfigStats config_stats = 3; + + message AtomStats { + optional int32 tag = 1; + optional int32 count = 2; + } + + repeated AtomStats atom_stats = 7; + + message UidMapStats { + optional int32 snapshots = 1; + optional int32 changes = 2; + optional int32 bytes_used = 3; + optional int32 dropped_snapshots = 4; + optional int32 dropped_changes = 5; + } + optional UidMapStats uidmap_stats = 8; + + message AnomalyAlarmStats { + optional int32 alarms_registered = 1; + } + optional AnomalyAlarmStats anomaly_alarm_stats = 9; + + message PulledAtomStats { + optional int32 atom_id = 1; + optional int64 total_pull = 2; + optional int64 total_pull_from_cache = 3; + optional int64 min_pull_interval_sec = 4; + } + repeated PulledAtomStats pulled_atom_stats = 10; + + message LoggerErrorStats { + optional int32 logger_disconnection_sec = 1; + optional int32 error_code = 2; + } + repeated LoggerErrorStats logger_error_stats = 11; +} \ No newline at end of file diff --git a/cmds/statsd/src/stats_log_util.h b/cmds/statsd/src/stats_log_util.h index 6a5123d8c844..71b3e4758969 100644 --- a/cmds/statsd/src/stats_log_util.h +++ b/cmds/statsd/src/stats_log_util.h @@ -19,7 +19,7 @@ #include #include "FieldValue.h" #include "HashableDimensionKey.h" -#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" +#include "frameworks/base/cmds/statsd/src/stats_log_common.pb.h" #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" #include "guardrail/StatsdStats.h" diff --git a/cmds/statsd/src/stats_util.h b/cmds/statsd/src/stats_util.h index 31f51a7ac0a4..c4b47dcea8a2 100644 --- a/cmds/statsd/src/stats_util.h +++ b/cmds/statsd/src/stats_util.h @@ -18,7 +18,7 @@ #include #include "HashableDimensionKey.h" -#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" +#include "frameworks/base/cmds/statsd/src/stats_log_common.pb.h" #include "logd/LogReader.h" #include diff --git a/cmds/statsd/src/statsd_config.proto b/cmds/statsd/src/statsd_config.proto index 5a326a47eb24..875027564546 100644 --- a/cmds/statsd/src/statsd_config.proto +++ b/cmds/statsd/src/statsd_config.proto @@ -15,7 +15,6 @@ */ syntax = "proto2"; -option optimize_for = LITE_RUNTIME; package android.os.statsd; diff --git a/cmds/statsd/src/subscriber/SubscriberReporter.cpp b/cmds/statsd/src/subscriber/SubscriberReporter.cpp index 9f68fc4c20be..95ecf8033f4c 100644 --- a/cmds/statsd/src/subscriber/SubscriberReporter.cpp +++ b/cmds/statsd/src/subscriber/SubscriberReporter.cpp @@ -27,6 +27,8 @@ namespace android { namespace os { namespace statsd { +using std::vector; + void SubscriberReporter::setBroadcastSubscriber(const ConfigKey& configKey, int64_t subscriberId, const sp& intentSender) { diff --git a/cmds/statsd/src/subscriber/SubscriberReporter.h b/cmds/statsd/src/subscriber/SubscriberReporter.h index c7d1a5ba9425..50100df56ff8 100644 --- a/cmds/statsd/src/subscriber/SubscriberReporter.h +++ b/cmds/statsd/src/subscriber/SubscriberReporter.h @@ -21,7 +21,6 @@ #include "config/ConfigKey.h" #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" // subscription -#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" // DimensionsValue #include "android/os/StatsDimensionsValue.h" #include "HashableDimensionKey.h" diff --git a/cmds/statsd/tests/FieldValue_test.cpp b/cmds/statsd/tests/FieldValue_test.cpp index f1ad0c88b242..5846761cb8e9 100644 --- a/cmds/statsd/tests/FieldValue_test.cpp +++ b/cmds/statsd/tests/FieldValue_test.cpp @@ -14,9 +14,10 @@ * limitations under the License. */ #include -#include "src/logd/LogEvent.h" +#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" #include "matchers/matcher_util.h" +#include "src/logd/LogEvent.h" #include "stats_log_util.h" #include "stats_util.h" #include "subscriber/SubscriberReporter.h" @@ -64,19 +65,19 @@ TEST(AtomMatcherTest, TestFilter) { vector matchers; translateFieldMatcher(matcher1, &matchers); - AttributionNode attribution_node1; + AttributionNodeInternal attribution_node1; attribution_node1.set_uid(1111); attribution_node1.set_tag("location1"); - AttributionNode attribution_node2; + AttributionNodeInternal attribution_node2; attribution_node2.set_uid(2222); attribution_node2.set_tag("location2"); - AttributionNode attribution_node3; + AttributionNodeInternal attribution_node3; attribution_node3.set_uid(3333); attribution_node3.set_tag("location3"); - std::vector attribution_nodes = {attribution_node1, attribution_node2, - attribution_node3}; + std::vector attribution_nodes = {attribution_node1, attribution_node2, + attribution_node3}; // Set up the event LogEvent event(10, 12345); @@ -154,19 +155,19 @@ TEST(AtomMatcherTest, TestSubDimension) { } TEST(AtomMatcherTest, TestMetric2ConditionLink) { - AttributionNode attribution_node1; + AttributionNodeInternal attribution_node1; attribution_node1.set_uid(1111); attribution_node1.set_tag("location1"); - AttributionNode attribution_node2; + AttributionNodeInternal attribution_node2; attribution_node2.set_uid(2222); attribution_node2.set_tag("location2"); - AttributionNode attribution_node3; + AttributionNodeInternal attribution_node3; attribution_node3.set_uid(3333); attribution_node3.set_tag("location3"); - std::vector attribution_nodes = {attribution_node1, attribution_node2, - attribution_node3}; + std::vector attribution_nodes = {attribution_node1, attribution_node2, + attribution_node3}; // Set up the event LogEvent event(10, 12345); @@ -298,15 +299,15 @@ TEST(AtomMatcherTest, TestWriteDimensionToProto) { } TEST(AtomMatcherTest, TestWriteAtomToProto) { - AttributionNode attribution_node1; + AttributionNodeInternal attribution_node1; attribution_node1.set_uid(1111); attribution_node1.set_tag("location1"); - AttributionNode attribution_node2; + AttributionNodeInternal attribution_node2; attribution_node2.set_uid(2222); attribution_node2.set_tag("location2"); - std::vector attribution_nodes = {attribution_node1, attribution_node2}; + std::vector attribution_nodes = {attribution_node1, attribution_node2}; // Set up the event LogEvent event(4, 12345); diff --git a/cmds/statsd/tests/LogEntryMatcher_test.cpp b/cmds/statsd/tests/LogEntryMatcher_test.cpp index 1023ea40aa55..2320a9d89f6b 100644 --- a/cmds/statsd/tests/LogEntryMatcher_test.cpp +++ b/cmds/statsd/tests/LogEntryMatcher_test.cpp @@ -60,19 +60,19 @@ TEST(AtomMatcherTest, TestSimpleMatcher) { TEST(AtomMatcherTest, TestAttributionMatcher) { UidMap uidMap; - AttributionNode attribution_node1; + AttributionNodeInternal attribution_node1; attribution_node1.set_uid(1111); attribution_node1.set_tag("location1"); - AttributionNode attribution_node2; + AttributionNodeInternal attribution_node2; attribution_node2.set_uid(2222); attribution_node2.set_tag("location2"); - AttributionNode attribution_node3; + AttributionNodeInternal attribution_node3; attribution_node3.set_uid(3333); attribution_node3.set_tag("location3"); - std::vector attribution_nodes = - { attribution_node1, attribution_node2, attribution_node3 }; + std::vector attribution_nodes = {attribution_node1, attribution_node2, + attribution_node3}; // Set up the event LogEvent event(TAG_ID, 0); diff --git a/cmds/statsd/tests/LogEvent_test.cpp b/cmds/statsd/tests/LogEvent_test.cpp index b64921501275..2fcde29fbbdb 100644 --- a/cmds/statsd/tests/LogEvent_test.cpp +++ b/cmds/statsd/tests/LogEvent_test.cpp @@ -25,14 +25,14 @@ namespace statsd { TEST(LogEventTest, TestLogParsing) { LogEvent event1(1, 2000); - std::vector nodes; + std::vector nodes; - AttributionNode node1; + AttributionNodeInternal node1; node1.set_uid(1000); node1.set_tag("tag1"); nodes.push_back(node1); - AttributionNode node2; + AttributionNodeInternal node2; node2.set_uid(2000); node2.set_tag("tag2"); nodes.push_back(node2); @@ -92,17 +92,17 @@ TEST(LogEventTest, TestLogParsing) { TEST(LogEventTest, TestLogParsing2) { LogEvent event1(1, 2000); - std::vector nodes; + std::vector nodes; event1.write("hello"); // repeated msg can be in the middle - AttributionNode node1; + AttributionNodeInternal node1; node1.set_uid(1000); node1.set_tag("tag1"); nodes.push_back(node1); - AttributionNode node2; + AttributionNodeInternal node2; node2.set_uid(2000); node2.set_tag("tag2"); nodes.push_back(node2); diff --git a/cmds/statsd/tests/condition/SimpleConditionTracker_test.cpp b/cmds/statsd/tests/condition/SimpleConditionTracker_test.cpp index 038d44936838..3dc3fd1160f1 100644 --- a/cmds/statsd/tests/condition/SimpleConditionTracker_test.cpp +++ b/cmds/statsd/tests/condition/SimpleConditionTracker_test.cpp @@ -58,9 +58,9 @@ SimplePredicate getWakeLockHeldCondition(bool countNesting, bool defaultFalse, } void writeAttributionNodesToEvent(LogEvent* event, const std::vector &uids) { - std::vector nodes; + std::vector nodes; for (size_t i = 0; i < uids.size(); ++i) { - AttributionNode node; + AttributionNodeInternal node; node.set_uid(uids[i]); nodes.push_back(node); } diff --git a/cmds/statsd/tests/e2e/Attribution_e2e_test.cpp b/cmds/statsd/tests/e2e/Attribution_e2e_test.cpp index 022800427b11..7a7e000fb98f 100644 --- a/cmds/statsd/tests/e2e/Attribution_e2e_test.cpp +++ b/cmds/statsd/tests/e2e/Attribution_e2e_test.cpp @@ -75,41 +75,40 @@ TEST(AttributionE2eTest, TestAttributionMatchAndSlice) { android::String16("APP3"), 333 /* uid */, 2 /* version code*/); // GMS core node is in the middle. - std::vector attributions1 = - {CreateAttribution(111, "App1"), CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(333, "App3")}; + std::vector attributions1 = {CreateAttribution(111, "App1"), + CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(333, "App3")}; // GMS core node is the last one. - std::vector attributions2 = - {CreateAttribution(111, "App1"), CreateAttribution(333, "App3"), - CreateAttribution(222, "GMSCoreModule1")}; + std::vector attributions2 = {CreateAttribution(111, "App1"), + CreateAttribution(333, "App3"), + CreateAttribution(222, "GMSCoreModule1")}; // GMS core node is the first one. - std::vector attributions3 = - {CreateAttribution(222, "GMSCoreModule1"), CreateAttribution(333, "App3")}; + std::vector attributions3 = {CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(333, "App3")}; // Single GMS core node. - std::vector attributions4 = - {CreateAttribution(222, "GMSCoreModule1")}; + std::vector attributions4 = {CreateAttribution(222, "GMSCoreModule1")}; // GMS core has another uid. - std::vector attributions5 = - {CreateAttribution(111, "App1"), CreateAttribution(444, "GMSCoreModule2"), - CreateAttribution(333, "App3")}; + std::vector attributions5 = {CreateAttribution(111, "App1"), + CreateAttribution(444, "GMSCoreModule2"), + CreateAttribution(333, "App3")}; // Multiple GMS core nodes. - std::vector attributions6 = - {CreateAttribution(444, "GMSCoreModule2"), CreateAttribution(222, "GMSCoreModule1")}; + std::vector attributions6 = {CreateAttribution(444, "GMSCoreModule2"), + CreateAttribution(222, "GMSCoreModule1")}; // No GMS core nodes. - std::vector attributions7 = - {CreateAttribution(111, "App1"), CreateAttribution(333, "App3")}; - std::vector attributions8 = {CreateAttribution(111, "App1")}; + std::vector attributions7 = {CreateAttribution(111, "App1"), + CreateAttribution(333, "App3")}; + std::vector attributions8 = {CreateAttribution(111, "App1")}; // GMS core node with isolated uid. const int isolatedUid = 666; - std::vector attributions9 = - {CreateAttribution(isolatedUid, "GMSCoreModule3")}; + std::vector attributions9 = { + CreateAttribution(isolatedUid, "GMSCoreModule3")}; std::vector> events; // Events 1~4 are in the 1st bucket. diff --git a/cmds/statsd/tests/e2e/DimensionInCondition_e2e_test.cpp b/cmds/statsd/tests/e2e/DimensionInCondition_e2e_test.cpp index 4dffd13bef66..01348bd664aa 100644 --- a/cmds/statsd/tests/e2e/DimensionInCondition_e2e_test.cpp +++ b/cmds/statsd/tests/e2e/DimensionInCondition_e2e_test.cpp @@ -78,13 +78,13 @@ TEST(DimensionInConditionE2eTest, TestCountMetricNoLink) { EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); - std::vector attributions1 = {CreateAttribution(111, "App1"), - CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(222, "GMSCoreModule2")}; + std::vector attributions1 = {CreateAttribution(111, "App1"), + CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(222, "GMSCoreModule2")}; - std::vector attributions2 = {CreateAttribution(333, "App2"), - CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(555, "GMSCoreModule2")}; + std::vector attributions2 = {CreateAttribution(333, "App2"), + CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(555, "GMSCoreModule2")}; std::vector> events; events.push_back( @@ -284,13 +284,13 @@ TEST(DimensionInConditionE2eTest, TestCountMetricWithLink) { auto processor = CreateStatsLogProcessor(bucketStartTimeNs / NS_PER_SEC, config, cfgKey); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); - std::vector attributions1 = {CreateAttribution(111, "App1"), - CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(222, "GMSCoreModule2")}; + std::vector attributions1 = {CreateAttribution(111, "App1"), + CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(222, "GMSCoreModule2")}; - std::vector attributions2 = {CreateAttribution(333, "App2"), - CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(555, "GMSCoreModule2")}; + std::vector attributions2 = {CreateAttribution(333, "App2"), + CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(555, "GMSCoreModule2")}; std::vector> events; @@ -464,13 +464,13 @@ TEST(DimensionInConditionE2eTest, TestDurationMetricNoLink) { EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); - std::vector attributions1 = {CreateAttribution(111, "App1"), - CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(222, "GMSCoreModule2")}; + std::vector attributions1 = { + CreateAttribution(111, "App1"), CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(222, "GMSCoreModule2")}; - std::vector attributions2 = {CreateAttribution(333, "App2"), - CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(555, "GMSCoreModule2")}; + std::vector attributions2 = { + CreateAttribution(333, "App2"), CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(555, "GMSCoreModule2")}; std::vector> events; @@ -629,13 +629,13 @@ TEST(DimensionInConditionE2eTest, TestDurationMetricWithLink) { EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); - std::vector attributions1 = {CreateAttribution(111, "App1"), - CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(222, "GMSCoreModule2")}; + std::vector attributions1 = { + CreateAttribution(111, "App1"), CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(222, "GMSCoreModule2")}; - std::vector attributions2 = {CreateAttribution(333, "App2"), - CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(555, "GMSCoreModule2")}; + std::vector attributions2 = { + CreateAttribution(333, "App2"), CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(555, "GMSCoreModule2")}; std::vector> events; diff --git a/cmds/statsd/tests/e2e/MetricConditionLink_e2e_test.cpp b/cmds/statsd/tests/e2e/MetricConditionLink_e2e_test.cpp index 1b51780a59bc..c874d92a1b01 100644 --- a/cmds/statsd/tests/e2e/MetricConditionLink_e2e_test.cpp +++ b/cmds/statsd/tests/e2e/MetricConditionLink_e2e_test.cpp @@ -134,8 +134,8 @@ TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks1) { CreateScreenStateChangedEvent(android::view::DisplayStateEnum::DISPLAY_STATE_ON, bucketStartTimeNs + 2 * bucketSizeNs - 100); - std::vector attributions = - {CreateAttribution(appUid, "App1"), CreateAttribution(appUid + 1, "GMSCoreModule1")}; + std::vector attributions = { + CreateAttribution(appUid, "App1"), CreateAttribution(appUid + 1, "GMSCoreModule1")}; auto syncOnEvent1 = CreateSyncStartEvent(attributions, "ReadEmail", bucketStartTimeNs + 50); auto syncOffEvent1 = @@ -249,8 +249,8 @@ TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks2) { CreateScreenStateChangedEvent(android::view::DisplayStateEnum::DISPLAY_STATE_ON, bucketStartTimeNs + 2 * bucketSizeNs - 100); - std::vector attributions = {CreateAttribution(appUid, "App1"), - CreateAttribution(appUid + 1, "GMSCoreModule1")}; + std::vector attributions = { + CreateAttribution(appUid, "App1"), CreateAttribution(appUid + 1, "GMSCoreModule1")}; auto syncOnEvent1 = CreateSyncStartEvent(attributions, "ReadEmail", bucketStartTimeNs + 50); auto syncOffEvent1 = CreateSyncEndEvent(attributions, "ReadEmail", bucketStartTimeNs + bucketSizeNs + 300); diff --git a/cmds/statsd/tests/e2e/WakelockDuration_e2e_test.cpp b/cmds/statsd/tests/e2e/WakelockDuration_e2e_test.cpp index efdab9822984..9153795365a2 100644 --- a/cmds/statsd/tests/e2e/WakelockDuration_e2e_test.cpp +++ b/cmds/statsd/tests/e2e/WakelockDuration_e2e_test.cpp @@ -60,13 +60,13 @@ StatsdConfig CreateStatsdConfig(DurationMetric::AggregationType aggregationType) return config; } -std::vector attributions1 = {CreateAttribution(111, "App1"), - CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(222, "GMSCoreModule2")}; +std::vector attributions1 = {CreateAttribution(111, "App1"), + CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(222, "GMSCoreModule2")}; -std::vector attributions2 = {CreateAttribution(111, "App2"), - CreateAttribution(222, "GMSCoreModule1"), - CreateAttribution(222, "GMSCoreModule2")}; +std::vector attributions2 = {CreateAttribution(111, "App2"), + CreateAttribution(222, "GMSCoreModule1"), + CreateAttribution(222, "GMSCoreModule2")}; /* Events: diff --git a/cmds/statsd/tests/statsd_test_util.cpp b/cmds/statsd/tests/statsd_test_util.cpp index b7acef75fafb..7568348108c4 100644 --- a/cmds/statsd/tests/statsd_test_util.cpp +++ b/cmds/statsd/tests/statsd_test_util.cpp @@ -291,8 +291,8 @@ std::unique_ptr CreateScreenBrightnessChangedEvent( } std::unique_ptr CreateWakelockStateChangedEvent( - const std::vector& attributions, const string& wakelockName, - const WakelockStateChanged::State state, uint64_t timestampNs) { + const std::vector& attributions, const string& wakelockName, + const WakelockStateChanged::State state, uint64_t timestampNs) { auto event = std::make_unique(android::util::WAKELOCK_STATE_CHANGED, timestampNs); event->write(attributions); event->write(android::os::WakeLockLevelEnum::PARTIAL_WAKE_LOCK); @@ -303,15 +303,15 @@ std::unique_ptr CreateWakelockStateChangedEvent( } std::unique_ptr CreateAcquireWakelockEvent( - const std::vector& attributions, - const string& wakelockName, uint64_t timestampNs) { + const std::vector& attributions, const string& wakelockName, + uint64_t timestampNs) { return CreateWakelockStateChangedEvent( attributions, wakelockName, WakelockStateChanged::ACQUIRE, timestampNs); } std::unique_ptr CreateReleaseWakelockEvent( - const std::vector& attributions, - const string& wakelockName, uint64_t timestampNs) { + const std::vector& attributions, const string& wakelockName, + uint64_t timestampNs) { return CreateWakelockStateChangedEvent( attributions, wakelockName, WakelockStateChanged::RELEASE, timestampNs); } @@ -339,8 +339,8 @@ std::unique_ptr CreateMoveToForegroundEvent(const int uid, uint64_t ti } std::unique_ptr CreateSyncStateChangedEvent( - const std::vector& attributions, - const string& name, const SyncStateChanged::State state, uint64_t timestampNs) { + const std::vector& attributions, const string& name, + const SyncStateChanged::State state, uint64_t timestampNs) { auto event = std::make_unique(android::util::SYNC_STATE_CHANGED, timestampNs); event->write(attributions); event->write(name); @@ -350,12 +350,14 @@ std::unique_ptr CreateSyncStateChangedEvent( } std::unique_ptr CreateSyncStartEvent( - const std::vector& attributions, const string& name, uint64_t timestampNs){ + const std::vector& attributions, const string& name, + uint64_t timestampNs) { return CreateSyncStateChangedEvent(attributions, name, SyncStateChanged::ON, timestampNs); } std::unique_ptr CreateSyncEndEvent( - const std::vector& attributions, const string& name, uint64_t timestampNs) { + const std::vector& attributions, const string& name, + uint64_t timestampNs) { return CreateSyncStateChangedEvent(attributions, name, SyncStateChanged::OFF, timestampNs); } @@ -396,8 +398,8 @@ sp CreateStatsLogProcessor(const long timeBaseSec, const Stat return processor; } -AttributionNode CreateAttribution(const int& uid, const string& tag) { - AttributionNode attribution; +AttributionNodeInternal CreateAttribution(const int& uid, const string& tag) { + AttributionNodeInternal attribution; attribution.set_uid(uid); attribution.set_tag(tag); return attribution; diff --git a/cmds/statsd/tests/statsd_test_util.h b/cmds/statsd/tests/statsd_test_util.h index 5d83ed7c1666..1708cc31bf14 100644 --- a/cmds/statsd/tests/statsd_test_util.h +++ b/cmds/statsd/tests/statsd_test_util.h @@ -15,10 +15,11 @@ #pragma once #include +#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" -#include "statslog.h" -#include "src/logd/LogEvent.h" #include "src/StatsLogProcessor.h" +#include "src/logd/LogEvent.h" +#include "statslog.h" namespace android { namespace os { @@ -119,11 +120,13 @@ std::unique_ptr CreateMoveToForegroundEvent(const int uid, uint64_t ti // Create log event when the app sync starts. std::unique_ptr CreateSyncStartEvent( - const std::vector& attributions, const string& name, uint64_t timestampNs); + const std::vector& attributions, const string& name, + uint64_t timestampNs); // Create log event when the app sync ends. std::unique_ptr CreateSyncEndEvent( - const std::vector& attributions, const string& name, uint64_t timestampNs); + const std::vector& attributions, const string& name, + uint64_t timestampNs); // Create log event when the app sync ends. std::unique_ptr CreateAppCrashEvent( @@ -131,20 +134,20 @@ std::unique_ptr CreateAppCrashEvent( // Create log event for acquiring wakelock. std::unique_ptr CreateAcquireWakelockEvent( - const std::vector& attributions, - const string& wakelockName, uint64_t timestampNs); + const std::vector& attributions, const string& wakelockName, + uint64_t timestampNs); // Create log event for releasing wakelock. std::unique_ptr CreateReleaseWakelockEvent( - const std::vector& attributions, - const string& wakelockName, uint64_t timestampNs); + const std::vector& attributions, const string& wakelockName, + uint64_t timestampNs); // Create log event for releasing wakelock. std::unique_ptr CreateIsolatedUidChangedEvent( int isolatedUid, int hostUid, bool is_create, uint64_t timestampNs); -// Helper function to create an AttributionNode proto. -AttributionNode CreateAttribution(const int& uid, const string& tag); +// Helper function to create an AttributionNodeInternal proto. +AttributionNodeInternal CreateAttribution(const int& uid, const string& tag); // Create a statsd log event processor upon the start time in seconds, config and key. sp CreateStatsLogProcessor(const long timeBaseSec, const StatsdConfig& config, diff --git a/cmds/statsd/tools/Android.mk b/cmds/statsd/tools/Android.mk index faf2d2c7d4e7..7253c9637026 100644 --- a/cmds/statsd/tools/Android.mk +++ b/cmds/statsd/tools/Android.mk @@ -16,5 +16,5 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -# Include the sub-makefiles +#Include the sub-makefiles include $(call all-makefiles-under,$(LOCAL_PATH)) \ No newline at end of file diff --git a/cmds/statsd/tools/dogfood/Android.mk b/cmds/statsd/tools/dogfood/Android.mk index a65095f4188d..c7e4c7b71793 100644 --- a/cmds/statsd/tools/dogfood/Android.mk +++ b/cmds/statsd/tools/dogfood/Android.mk @@ -24,7 +24,6 @@ LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res LOCAL_STATIC_JAVA_LIBRARIES := platformprotoslite \ statsdprotolite -LOCAL_PROTOC_OPTIMIZE_TYPE := lite LOCAL_PRIVILEGED_MODULE := true LOCAL_DEX_PREOPT := false LOCAL_CERTIFICATE := platform diff --git a/cmds/statsd/tools/loadtest/Android.mk b/cmds/statsd/tools/loadtest/Android.mk index f5722c2c3d19..091f184ad304 100644 --- a/cmds/statsd/tools/loadtest/Android.mk +++ b/cmds/statsd/tools/loadtest/Android.mk @@ -19,15 +19,11 @@ include $(CLEAR_VARS) LOCAL_PACKAGE_NAME := StatsdLoadtest LOCAL_SRC_FILES := $(call all-java-files-under, src) -LOCAL_SRC_FILES += ../../src/stats_log.proto \ - ../../src/atoms.proto \ - ../../src/perfetto/perfetto_config.proto \ - ../../src/statsd_config.proto -LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/../../src/ + LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res -LOCAL_STATIC_JAVA_LIBRARIES := platformprotoslite +LOCAL_STATIC_JAVA_LIBRARIES := platformprotoslite \ + statsdprotolite -LOCAL_PROTOC_OPTIMIZE_TYPE := lite LOCAL_CERTIFICATE := platform LOCAL_PRIVILEGED_MODULE := true LOCAL_DEX_PREOPT := false diff --git a/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/LoadtestActivity.java b/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/LoadtestActivity.java index bed4d9809692..a12def0db9fb 100644 --- a/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/LoadtestActivity.java +++ b/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/LoadtestActivity.java @@ -50,7 +50,7 @@ import android.widget.Toast; import com.android.os.StatsLog.ConfigMetricsReport; import com.android.os.StatsLog.ConfigMetricsReportList; -import com.android.os.StatsLog.StatsdStatsReport; +import com.android.os.StatsLogCommon.StatsdStatsReport; import com.android.internal.os.StatsdConfigProto.TimeUnit; import java.util.ArrayList; diff --git a/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/StatsdStatsRecorder.java b/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/StatsdStatsRecorder.java index e63150f95316..58cbcd89581e 100644 --- a/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/StatsdStatsRecorder.java +++ b/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/StatsdStatsRecorder.java @@ -16,11 +16,8 @@ package com.android.statsd.loadtest; import android.content.Context; -import android.util.Log; -import com.android.os.StatsLog.StatsdStatsReport; +import com.android.os.StatsLogCommon.StatsdStatsReport; import com.android.internal.os.StatsdConfigProto.TimeUnit; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public class StatsdStatsRecorder extends PerfDataRecorder { private static final String TAG = "loadtest.StatsdStatsRecorder"; diff --git a/tools/stats_log_api_gen/Android.bp b/tools/stats_log_api_gen/Android.bp index dc2ed433d345..948422c868d6 100644 --- a/tools/stats_log_api_gen/Android.bp +++ b/tools/stats_log_api_gen/Android.bp @@ -62,10 +62,14 @@ cc_test_host { shared_libs: [ "libstats_proto_host", + "libprotobuf-cpp-full", ], proto: { type: "full", + include_dirs: [ + "external/protobuf/src", + ], }, } diff --git a/tools/stats_log_api_gen/Collation.cpp b/tools/stats_log_api_gen/Collation.cpp index 0e57f7ff0ed2..ab106d708748 100644 --- a/tools/stats_log_api_gen/Collation.cpp +++ b/tools/stats_log_api_gen/Collation.cpp @@ -41,12 +41,12 @@ AtomDecl::AtomDecl() } AtomDecl::AtomDecl(const AtomDecl& that) - :code(that.code), - name(that.name), - message(that.message), - fields(that.fields) -{ -} + : code(that.code), + name(that.name), + message(that.message), + fields(that.fields), + primaryFields(that.primaryFields), + exclusiveField(that.exclusiveField) {} AtomDecl::AtomDecl(int c, const string& n, const string& m) :code(c), @@ -237,6 +237,31 @@ int collate_atom(const Descriptor *atom, AtomDecl *atomDecl, signature->push_back(javaType); } atomDecl->fields.push_back(atField); + + if (field->options().GetExtension(os::statsd::stateFieldOption).option() == + os::statsd::StateField::PRIMARY) { + if (javaType == JAVA_TYPE_UNKNOWN || + javaType == JAVA_TYPE_ATTRIBUTION_CHAIN || + javaType == JAVA_TYPE_OBJECT || javaType == JAVA_TYPE_BYTE_ARRAY) { + errorCount++; + } + atomDecl->primaryFields.push_back(it->first); + } + + if (field->options().GetExtension(os::statsd::stateFieldOption).option() == + os::statsd::StateField::EXCLUSIVE) { + if (javaType == JAVA_TYPE_UNKNOWN || + javaType == JAVA_TYPE_ATTRIBUTION_CHAIN || + javaType == JAVA_TYPE_OBJECT || javaType == JAVA_TYPE_BYTE_ARRAY) { + errorCount++; + } + + if (atomDecl->exclusiveField == 0) { + atomDecl->exclusiveField = it->first; + } else { + errorCount++; + } + } } return errorCount; @@ -318,6 +343,9 @@ int collate_atoms(const Descriptor *descriptor, Atoms *atoms) { AtomDecl atomDecl(atomField->number(), atomField->name(), atom->name()); vector signature; errorCount += collate_atom(atom, &atomDecl, &signature); + if (atomDecl.primaryFields.size() != 0 && atomDecl.exclusiveField == 0) { + errorCount++; + } atoms->signatures.insert(signature); atoms->decls.insert(atomDecl); diff --git a/tools/stats_log_api_gen/Collation.h b/tools/stats_log_api_gen/Collation.h index 0455eca62da8..edba3e222224 100644 --- a/tools/stats_log_api_gen/Collation.h +++ b/tools/stats_log_api_gen/Collation.h @@ -81,6 +81,9 @@ struct AtomDecl { string message; vector fields; + vector primaryFields; + int exclusiveField = 0; + AtomDecl(); AtomDecl(const AtomDecl& that); AtomDecl(int code, const string& name, const string& message); diff --git a/tools/stats_log_api_gen/main.cpp b/tools/stats_log_api_gen/main.cpp index f0628c099e2e..80985f8c9ff4 100644 --- a/tools/stats_log_api_gen/main.cpp +++ b/tools/stats_log_api_gen/main.cpp @@ -320,6 +320,7 @@ write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributio fprintf(out, "\n"); fprintf(out, "#include \n"); fprintf(out, "#include \n"); + fprintf(out, "#include \n"); fprintf(out, "#include \n"); fprintf(out, "\n"); @@ -397,6 +398,43 @@ write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributio fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", maxPushedAtomId); + fprintf(out, "struct StateAtomFieldOptions {\n"); + fprintf(out, " std::vector primaryFields;\n"); + fprintf(out, " int exclusiveField;\n"); + fprintf(out, "\n"); + fprintf(out, + " static std::map " + "getStateAtomFieldOptions() {\n"); + fprintf(out, " std::map options;\n"); + fprintf(out, " StateAtomFieldOptions opt;\n"); + for (set::const_iterator atom = atoms.decls.begin(); + atom != atoms.decls.end(); atom++) { + if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) { + continue; + } + fprintf(out, + "\n // Adding primary and exclusive fields for atom " + "(%d)%s\n", + atom->code, atom->name.c_str()); + fprintf(out, " opt.primaryFields.clear();\n"); + for (const auto& field : atom->primaryFields) { + fprintf(out, " opt.primaryFields.push_back(%d);\n", field); + } + + fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField); + fprintf(out, " options[static_cast(%s)] = opt;\n", + make_constant_name(atom->name).c_str()); + } + + fprintf(out, " return options;\n"); + fprintf(out, " }\n"); + fprintf(out, "};\n"); + + fprintf(out, + "const static std::map " + "kStateAtomsFieldOptions = " + "StateAtomFieldOptions::getStateAtomFieldOptions();\n"); + // Print write methods fprintf(out, "//\n"); fprintf(out, "// Write methods\n"); diff --git a/tools/stats_log_api_gen/test.proto b/tools/stats_log_api_gen/test.proto index 66cbee8f219b..264a865e3b39 100644 --- a/tools/stats_log_api_gen/test.proto +++ b/tools/stats_log_api_gen/test.proto @@ -17,6 +17,7 @@ syntax = "proto2"; import "frameworks/base/cmds/statsd/src/atoms.proto"; +import "frameworks/base/cmds/statsd/src/atom_field_options.proto"; package android.stats_log_api_gen; @@ -108,3 +109,68 @@ message BadAttributionNodePosition { oneof event { BadAttributionNodePositionAtom bad = 1; } } +message BadStateAtoms { + oneof event { + BadStateAtom1 bad1 = 1; + BadStateAtom2 bad2 = 2; + BadStateAtom3 bad3 = 3; + } +} + +message GoodStateAtoms { + oneof event { + GoodStateAtom1 good1 = 1; + GoodStateAtom2 good2 = 2; + } +} + +// The atom has only primary field but no exclusive state field. +message BadStateAtom1 { + optional int32 uid = 1 + [(android.os.statsd.stateFieldOption).option = PRIMARY]; +} + +// Only primative types can be annotated. +message BadStateAtom2 { + repeated android.os.statsd.AttributionNode attribution = 1 + [(android.os.statsd.stateFieldOption).option = PRIMARY]; + optional int32 state = 2 + [(android.os.statsd.stateFieldOption).option = EXCLUSIVE]; +} + +// Having 2 exclusive state field in the atom means the atom is badly designed. +// E.g., putting bluetooth state and wifi state in the same atom. +message BadStateAtom3 { + optional int32 uid = 1 + [(android.os.statsd.stateFieldOption).option = PRIMARY]; + optional int32 state = 2 + [(android.os.statsd.stateFieldOption).option = EXCLUSIVE]; + optional int32 state2 = 3 + [(android.os.statsd.stateFieldOption).option = EXCLUSIVE]; +} + +message GoodStateAtom1 { + optional int32 uid = 1 + [(android.os.statsd.stateFieldOption).option = PRIMARY]; + optional int32 state = 2 + [(android.os.statsd.stateFieldOption).option = EXCLUSIVE]; +} + +// Atoms can have exclusive state field, but no primary field. That means +// the state is globally exclusive (e.g., DisplayState). +message GoodStateAtom2 { + optional int32 uid = 1; + optional int32 state = 2 + [(android.os.statsd.stateFieldOption).option = EXCLUSIVE]; +} + +// We can have more than one primary fields. That means their combination is a +// primary key. +message GoodStateAtom3 { + optional int32 uid = 1 + [(android.os.statsd.stateFieldOption).option = PRIMARY]; + optional int32 tid = 2 + [(android.os.statsd.stateFieldOption).option = PRIMARY]; + optional int32 state = 3 + [(android.os.statsd.stateFieldOption).option = EXCLUSIVE]; +} \ No newline at end of file diff --git a/tools/stats_log_api_gen/test_collation.cpp b/tools/stats_log_api_gen/test_collation.cpp index 9e22cd92fd53..1936d9667948 100644 --- a/tools/stats_log_api_gen/test_collation.cpp +++ b/tools/stats_log_api_gen/test_collation.cpp @@ -199,5 +199,18 @@ TEST(CollationTest, FailBadAttributionNodePosition) { EXPECT_EQ(1, errorCount); } +TEST(CollationTest, FailOnBadStateAtomOptions) { + Atoms atoms; + int errorCount = collate_atoms(BadStateAtoms::descriptor(), &atoms); + + EXPECT_EQ(3, errorCount); +} + +TEST(CollationTest, PassOnGoodStateAtomOptions) { + Atoms atoms; + int errorCount = collate_atoms(GoodStateAtoms::descriptor(), &atoms); + EXPECT_EQ(0, errorCount); +} + } // namespace stats_log_api_gen } // namespace android \ No newline at end of file -- 2.11.0