Add statsd microbenchmark and fix a crash in LogEvent
+ We should benchmark the core functions in the critical path.
1. LogEvent parsing
2. Log matching
3. Event processing in various metrics
....
+ Using microbenchmark, we can quantify the optimization we do in the future.
---------------------------------------------------------
Benchmark Time CPU Iterations
---------------------------------------------------------
BM_LogEventCreation 4772 ns 4705 ns 145738
Test: make -j64 statsd_benchmark
Bug: 72242322
Change-Id: I43aa704ffdc7cd21e02ef9038eff66ca7022dbfb
This commit is contained in:
@@ -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))
|
||||
|
||||
29
cmds/statsd/benchmark/hello_world_benchmark.cpp
Normal file
29
cmds/statsd/benchmark/hello_world_benchmark.cpp
Normal file
@@ -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);
|
||||
74
cmds/statsd/benchmark/log_event_benchmark.cpp
Normal file
74
cmds/statsd/benchmark/log_event_benchmark.cpp
Normal file
@@ -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
|
||||
19
cmds/statsd/benchmark/main.cpp
Normal file
19
cmds/statsd/benchmark/main.cpp
Normal file
@@ -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();
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user