Yao Chen 8a8d16ceea Statsd CPU optimization.
The key change is to revamp how we parse/store/match a log event, especially how we match repeated
field and attribution nodes, and how we construct dimensions and compare them.

+ We use a integer to encode the field of a log element. And also encode the FieldMatcher into an
integer and a bit mask. The log matching becomes 2 integer operations.

+ Dimension is stored as encoded field and value pair. Checking if 2 dimensions are equal is then
  becoming checking if the underlying integers are equal. The integers are stored contiguously
  in memory, so it's much faster than previous tree structure.

Start review from FieldValue.h

Test: statsd_test + new unit tests

Bug: 72659059

Change-Id: Iec8daeacdd3f39ab297c10ab9cd7b710a9c42e86
2018-02-12 10:38:45 -08:00

167 lines
5.3 KiB
C++

// 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.
#include <gtest/gtest.h>
#include <log/log_event_list.h>
#include "src/logd/LogEvent.h"
#ifdef __ANDROID__
namespace android {
namespace os {
namespace statsd {
TEST(LogEventTest, TestLogParsing) {
LogEvent event1(1, 2000);
std::vector<AttributionNode> nodes;
AttributionNode node1;
node1.set_uid(1000);
node1.set_tag("tag1");
nodes.push_back(node1);
AttributionNode node2;
node2.set_uid(2000);
node2.set_tag("tag2");
nodes.push_back(node2);
event1.write(nodes);
event1.write("hello");
event1.write((int32_t)10);
event1.write((int64_t)20);
event1.write((float)1.1);
event1.init();
const auto& items = event1.getValues();
EXPECT_EQ((size_t)8, items.size());
EXPECT_EQ(1, event1.GetTagId());
const FieldValue& item0 = event1.getValues()[0];
EXPECT_EQ(0x2010101, item0.mField.getField());
EXPECT_EQ(Type::INT, item0.mValue.getType());
EXPECT_EQ(1000, item0.mValue.int_value);
const FieldValue& item1 = event1.getValues()[1];
EXPECT_EQ(0x2010182, item1.mField.getField());
EXPECT_EQ(Type::STRING, item1.mValue.getType());
EXPECT_EQ("tag1", item1.mValue.str_value);
const FieldValue& item2 = event1.getValues()[2];
EXPECT_EQ(0x2018201, item2.mField.getField());
EXPECT_EQ(Type::INT, item2.mValue.getType());
EXPECT_EQ(2000, item2.mValue.int_value);
const FieldValue& item3 = event1.getValues()[3];
EXPECT_EQ(0x2018282, item3.mField.getField());
EXPECT_EQ(Type::STRING, item3.mValue.getType());
EXPECT_EQ("tag2", item3.mValue.str_value);
const FieldValue& item4 = event1.getValues()[4];
EXPECT_EQ(0x20000, item4.mField.getField());
EXPECT_EQ(Type::STRING, item4.mValue.getType());
EXPECT_EQ("hello", item4.mValue.str_value);
const FieldValue& item5 = event1.getValues()[5];
EXPECT_EQ(0x30000, item5.mField.getField());
EXPECT_EQ(Type::INT, item5.mValue.getType());
EXPECT_EQ(10, item5.mValue.int_value);
const FieldValue& item6 = event1.getValues()[6];
EXPECT_EQ(0x40000, item6.mField.getField());
EXPECT_EQ(Type::LONG, item6.mValue.getType());
EXPECT_EQ((int64_t)20, item6.mValue.long_value);
const FieldValue& item7 = event1.getValues()[7];
EXPECT_EQ(0x50000, item7.mField.getField());
EXPECT_EQ(Type::FLOAT, item7.mValue.getType());
EXPECT_EQ((float)1.1, item7.mValue.float_value);
}
TEST(LogEventTest, TestLogParsing2) {
LogEvent event1(1, 2000);
std::vector<AttributionNode> nodes;
event1.write("hello");
// repeated msg can be in the middle
AttributionNode node1;
node1.set_uid(1000);
node1.set_tag("tag1");
nodes.push_back(node1);
AttributionNode node2;
node2.set_uid(2000);
node2.set_tag("tag2");
nodes.push_back(node2);
event1.write(nodes);
event1.write((int32_t)10);
event1.write((int64_t)20);
event1.write((float)1.1);
event1.init();
const auto& items = event1.getValues();
EXPECT_EQ((size_t)8, items.size());
EXPECT_EQ(1, event1.GetTagId());
const FieldValue& item = event1.getValues()[0];
EXPECT_EQ(0x00010000, item.mField.getField());
EXPECT_EQ(Type::STRING, item.mValue.getType());
EXPECT_EQ("hello", item.mValue.str_value);
const FieldValue& item0 = event1.getValues()[1];
EXPECT_EQ(0x2020101, item0.mField.getField());
EXPECT_EQ(Type::INT, item0.mValue.getType());
EXPECT_EQ(1000, item0.mValue.int_value);
const FieldValue& item1 = event1.getValues()[2];
EXPECT_EQ(0x2020182, item1.mField.getField());
EXPECT_EQ(Type::STRING, item1.mValue.getType());
EXPECT_EQ("tag1", item1.mValue.str_value);
const FieldValue& item2 = event1.getValues()[3];
EXPECT_EQ(0x2028201, item2.mField.getField());
EXPECT_EQ(Type::INT, item2.mValue.getType());
EXPECT_EQ(2000, item2.mValue.int_value);
const FieldValue& item3 = event1.getValues()[4];
EXPECT_EQ(0x2028282, item3.mField.getField());
EXPECT_EQ(Type::STRING, item3.mValue.getType());
EXPECT_EQ("tag2", item3.mValue.str_value);
const FieldValue& item5 = event1.getValues()[5];
EXPECT_EQ(0x30000, item5.mField.getField());
EXPECT_EQ(Type::INT, item5.mValue.getType());
EXPECT_EQ(10, item5.mValue.int_value);
const FieldValue& item6 = event1.getValues()[6];
EXPECT_EQ(0x40000, item6.mField.getField());
EXPECT_EQ(Type::LONG, item6.mValue.getType());
EXPECT_EQ((int64_t)20, item6.mValue.long_value);
const FieldValue& item7 = event1.getValues()[7];
EXPECT_EQ(0x50000, item7.mField.getField());
EXPECT_EQ(Type::FLOAT, item7.mValue.getType());
EXPECT_EQ((float)1.1, item7.mValue.float_value);
}
} // namespace statsd
} // namespace os
} // namespace android
#else
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif