Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b5f12bbd authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add statsd microbenchmark and fix a crash in LogEvent"

parents 07c15cb8 48d75189
Loading
Loading
Loading
Loading
+38 −2
Original line number Diff line number Diff line
@@ -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 −0
Original line number Diff line number Diff line
/*
 * 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 −0
Original line number Diff line number Diff line
/*
 * 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 −0
Original line number Diff line number Diff line
/*
 * 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();
+7 −1
Original line number Diff line number Diff line
@@ -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.
        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);
    }
}
Loading