diff options
author | 2018-01-25 02:04:11 +0000 | |
---|---|---|
committer | 2018-01-25 02:04:11 +0000 | |
commit | b5f12bbd2f30f93803dbb266c45d30cc44df3d36 (patch) | |
tree | 926eac9dc34d6279cc14b43d317eeb1de6b55dfb | |
parent | 07c15cb88a8b1092754fb64d729699ee9449dc21 (diff) | |
parent | 48d75189ccbfc06f24f315ff5b8a1a42cf8207c5 (diff) |
Merge "Add statsd microbenchmark and fix a crash in LogEvent"
-rw-r--r-- | cmds/statsd/Android.mk | 40 | ||||
-rw-r--r-- | cmds/statsd/benchmark/hello_world_benchmark.cpp | 29 | ||||
-rw-r--r-- | cmds/statsd/benchmark/log_event_benchmark.cpp | 74 | ||||
-rw-r--r-- | cmds/statsd/benchmark/main.cpp | 19 | ||||
-rw-r--r-- | cmds/statsd/src/logd/LogEvent.cpp | 8 | ||||
-rw-r--r-- | cmds/statsd/src/logd/LogEvent.h | 2 |
6 files changed, 168 insertions, 4 deletions
diff --git a/cmds/statsd/Android.mk b/cmds/statsd/Android.mk index a7daa3f2b63a..9200f6445c01 100644 --- a/cmds/statsd/Android.mk +++ b/cmds/statsd/Android.mk @@ -221,6 +221,44 @@ LOCAL_STATIC_JAVA_LIBRARIES := \ include $(BUILD_STATIC_JAVA_LIBRARY) +############################## +# statsd micro benchmark +############################## + +include $(CLEAR_VARS) +LOCAL_MODULE := statsd_benchmark + +LOCAL_SRC_FILES := $(statsd_common_src) \ + benchmark/main.cpp \ + benchmark/hello_world_benchmark.cpp \ + benchmark/log_event_benchmark.cpp + +LOCAL_C_INCLUDES := $(statsd_common_c_includes) + +LOCAL_CFLAGS := -Wall \ + -Werror \ + -Wno-unused-parameter \ + -Wno-unused-variable \ + -Wno-unused-function \ + +# Bug: http://b/29823425 Disable -Wvarargs for Clang update to r271374 +LOCAL_CFLAGS += -Wno-varargs + +LOCAL_AIDL_INCLUDES := $(statsd_common_aidl_includes) + +LOCAL_STATIC_LIBRARIES := \ + $(statsd_common_static_libraries) + +LOCAL_SHARED_LIBRARIES := $(statsd_common_shared_libraries) \ + libgtest_prod + +LOCAL_PROTOC_OPTIMIZE_TYPE := lite + +LOCAL_MODULE_TAGS := eng tests + +include $(BUILD_NATIVE_BENCHMARK) + + statsd_common_src:= statsd_common_aidl_includes:= statsd_common_c_includes:= @@ -228,6 +266,4 @@ statsd_common_static_libraries:= statsd_common_shared_libraries:= -############################## - include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/cmds/statsd/benchmark/hello_world_benchmark.cpp b/cmds/statsd/benchmark/hello_world_benchmark.cpp new file mode 100644 index 000000000000..c732d394bf64 --- /dev/null +++ b/cmds/statsd/benchmark/hello_world_benchmark.cpp @@ -0,0 +1,29 @@ +/* + * 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. + */ +#include "benchmark/benchmark.h" + +static void BM_StringCreation(benchmark::State& state) { + while (state.KeepRunning()) std::string empty_string; +} +// Register the function as a benchmark +BENCHMARK(BM_StringCreation); + +// Define another benchmark +static void BM_StringCopy(benchmark::State& state) { + std::string x = "hello"; + while (state.KeepRunning()) std::string copy(x); +} +BENCHMARK(BM_StringCopy); diff --git a/cmds/statsd/benchmark/log_event_benchmark.cpp b/cmds/statsd/benchmark/log_event_benchmark.cpp new file mode 100644 index 000000000000..43addc28f074 --- /dev/null +++ b/cmds/statsd/benchmark/log_event_benchmark.cpp @@ -0,0 +1,74 @@ +/* + * 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. + */ +#include <vector> +#include "benchmark/benchmark.h" +#include "logd/LogEvent.h" + +namespace android { +namespace os { +namespace statsd { + +using std::vector; + +/* Special markers for android_log_list_element type */ +static const char EVENT_TYPE_LIST_STOP = '\n'; /* declare end of list */ +static const char EVENT_TYPE_UNKNOWN = '?'; /* protocol error */ + +static const char EVENT_TYPE_INT = 0; +static const char EVENT_TYPE_LONG = 1; +static const char EVENT_TYPE_STRING = 2; +static const char EVENT_TYPE_LIST = 3; +static const char EVENT_TYPE_FLOAT = 4; + +static const int kLogMsgHeaderSize = 28; + +static void write4Bytes(int val, vector<char>* buffer) { + buffer->push_back(static_cast<char>(val)); + buffer->push_back(static_cast<char>((val >> 8) & 0xFF)); + buffer->push_back(static_cast<char>((val >> 16) & 0xFF)); + buffer->push_back(static_cast<char>((val >> 24) & 0xFF)); +} + +static void getSimpleLogMsgData(log_msg* msg) { + vector<char> buffer; + // stats_log tag id + write4Bytes(1937006964, &buffer); + buffer.push_back(EVENT_TYPE_LIST); + buffer.push_back(2); // field counts; + buffer.push_back(EVENT_TYPE_INT); + write4Bytes(10 /* atom id */, &buffer); + buffer.push_back(EVENT_TYPE_INT); + write4Bytes(99 /* a value to log*/, &buffer); + buffer.push_back(EVENT_TYPE_LIST_STOP); + + msg->entry_v1.len = buffer.size(); + msg->entry.hdr_size = kLogMsgHeaderSize; + msg->entry_v1.sec = time(nullptr); + std::copy(buffer.begin(), buffer.end(), msg->buf + kLogMsgHeaderSize); +} + +static void BM_LogEventCreation(benchmark::State& state) { + log_msg msg; + getSimpleLogMsgData(&msg); + while (state.KeepRunning()) { + benchmark::DoNotOptimize(LogEvent(msg)); + } +} +BENCHMARK(BM_LogEventCreation); + +} // namespace statsd +} // namespace os +} // namespace android diff --git a/cmds/statsd/benchmark/main.cpp b/cmds/statsd/benchmark/main.cpp new file mode 100644 index 000000000000..08921f3c3f52 --- /dev/null +++ b/cmds/statsd/benchmark/main.cpp @@ -0,0 +1,19 @@ +/* + * 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. + */ + +#include <benchmark/benchmark.h> + +BENCHMARK_MAIN(); diff --git a/cmds/statsd/src/logd/LogEvent.cpp b/cmds/statsd/src/logd/LogEvent.cpp index 34fa3c404d10..1ca793c81878 100644 --- a/cmds/statsd/src/logd/LogEvent.cpp +++ b/cmds/statsd/src/logd/LogEvent.cpp @@ -42,6 +42,7 @@ LogEvent::LogEvent(log_msg& msg) { mLogUid = msg.entry_v4.uid; init(mContext); if (mContext) { + // android_log_destroy will set mContext to NULL android_log_destroy(&mContext); } } @@ -64,12 +65,17 @@ void LogEvent::init() { mContext = create_android_log_parser(buffer, len); init(mContext); // destroy the context to save memory. - android_log_destroy(&mContext); + if (mContext) { + // android_log_destroy will set mContext to NULL + android_log_destroy(&mContext); + } } } LogEvent::~LogEvent() { if (mContext) { + // This is for the case when LogEvent is created using the test interface + // but init() isn't called. android_log_destroy(&mContext); } } diff --git a/cmds/statsd/src/logd/LogEvent.h b/cmds/statsd/src/logd/LogEvent.h index 3d6984cea97d..5a4efd4b54a4 100644 --- a/cmds/statsd/src/logd/LogEvent.h +++ b/cmds/statsd/src/logd/LogEvent.h @@ -156,7 +156,7 @@ private: // This field is used when statsD wants to create log event object and write fields to it. After // calling init() function, this object would be destroyed to save memory usage. // When the log event is created from log msg, this field is never initiated. - android_log_context mContext; + android_log_context mContext = NULL; uint64_t mTimestampNs; |