bootstat: drop event_log_list_builder
Use android_log_event_context class instead Test: manual Bug: 31456426 Bug: 31750617 Bug: 30465923 Change-Id: I8c58d6bdd1fd921f32a1a6f42bec38937833c0e4
This commit is contained in:
parent
aeaaf81c2c
commit
343b76e449
|
@ -16,7 +16,6 @@
|
|||
|
||||
bootstat_lib_src_files = [
|
||||
"boot_event_record_store.cpp",
|
||||
"event_log_list_builder.cpp",
|
||||
"histogram_logger.cpp",
|
||||
"uptime_parser.cpp",
|
||||
]
|
||||
|
@ -88,7 +87,6 @@ cc_test {
|
|||
],
|
||||
srcs: [
|
||||
"boot_event_record_store_test.cpp",
|
||||
"event_log_list_builder_test.cpp",
|
||||
"testrunner.cpp",
|
||||
],
|
||||
}
|
||||
|
|
|
@ -35,7 +35,6 @@
|
|||
#include <cutils/properties.h>
|
||||
|
||||
#include "boot_event_record_store.h"
|
||||
#include "event_log_list_builder.h" /* ToDo: switch to liblog implementation */
|
||||
#include "histogram_logger.h"
|
||||
#include "uptime_parser.h"
|
||||
|
||||
|
|
|
@ -1,108 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2016 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 "event_log_list_builder.h"
|
||||
|
||||
#include <cinttypes>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <log/log.h>
|
||||
#include <android-base/logging.h>
|
||||
|
||||
namespace {
|
||||
|
||||
const size_t MAX_EVENT_PAYLOAD_SIZE = 512 - 1; // Leave room for final '\n'.
|
||||
const size_t EVENT_TYPE_SIZE = 1; // Size in bytes of the event type marker.
|
||||
|
||||
} // namespace
|
||||
|
||||
EventLogListBuilder::EventLogListBuilder()
|
||||
: payload_count_(0),
|
||||
payload_size_(0),
|
||||
payload_(std::make_unique<uint8_t[]>(MAX_EVENT_PAYLOAD_SIZE)) {
|
||||
memset(payload_.get(), 0, MAX_EVENT_PAYLOAD_SIZE);
|
||||
|
||||
// Set up the top-level EventLog data type.
|
||||
AppendByte(EVENT_TYPE_LIST);
|
||||
|
||||
// Skip over the byte prepresenting the number of items in the list. This
|
||||
// value is set in Release().
|
||||
payload_size_++;
|
||||
}
|
||||
|
||||
bool EventLogListBuilder::Append(int value) {
|
||||
DCHECK_NE(static_cast<uint8_t*>(nullptr), payload_.get());
|
||||
|
||||
if (!IsSpaceAvailable(sizeof(value) + EVENT_TYPE_SIZE)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AppendByte(EVENT_TYPE_INT);
|
||||
AppendData(&value, sizeof(value));
|
||||
|
||||
payload_count_++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EventLogListBuilder::Append(const std::string& value) {
|
||||
DCHECK_NE(static_cast<uint8_t*>(nullptr), payload_.get());
|
||||
|
||||
int len = value.length();
|
||||
if (!IsSpaceAvailable(sizeof(len) + len)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AppendByte(EVENT_TYPE_STRING);
|
||||
AppendData(&len, sizeof(len));
|
||||
AppendData(value.c_str(), len);
|
||||
|
||||
payload_count_++;
|
||||
return true;
|
||||
}
|
||||
|
||||
void EventLogListBuilder::Release(std::unique_ptr<uint8_t[]>* log,
|
||||
size_t* size) {
|
||||
// Finalize the log payload.
|
||||
payload_[1] = payload_count_;
|
||||
|
||||
// Return the log payload.
|
||||
*size = payload_size_;
|
||||
*log = std::move(payload_);
|
||||
}
|
||||
|
||||
void EventLogListBuilder::AppendData(const void* data, size_t size) {
|
||||
DCHECK_LT(payload_size_ + size, MAX_EVENT_PAYLOAD_SIZE);
|
||||
memcpy(&payload_[payload_size_], data, size);
|
||||
payload_size_ += size;
|
||||
}
|
||||
|
||||
void EventLogListBuilder::AppendByte(uint8_t byte) {
|
||||
DCHECK_LT(payload_size_ + sizeof(byte), MAX_EVENT_PAYLOAD_SIZE);
|
||||
payload_[payload_size_++] = byte;
|
||||
}
|
||||
|
||||
bool EventLogListBuilder::IsSpaceAvailable(size_t value_size) {
|
||||
size_t space_needed = value_size + EVENT_TYPE_SIZE;
|
||||
if (payload_size_ + space_needed > MAX_EVENT_PAYLOAD_SIZE) {
|
||||
size_t remaining = MAX_EVENT_PAYLOAD_SIZE - payload_size_;
|
||||
LOG(WARNING) << "Not enough space for value. remain=" <<
|
||||
remaining << "; needed=" << space_needed;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2016 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.
|
||||
*/
|
||||
|
||||
#ifndef EVENT_LOG_LIST_BUILDER_H_
|
||||
#define EVENT_LOG_LIST_BUILDER_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include <android-base/macros.h>
|
||||
|
||||
// EventLogListBuilder provides a mechanism to build an EventLog list
|
||||
// consisting of int and string EventLog values.
|
||||
//
|
||||
// NOTE: This class does not provide the ability to append an embedded list,
|
||||
// i.e., a list containing a list.
|
||||
class EventLogListBuilder {
|
||||
public:
|
||||
EventLogListBuilder();
|
||||
|
||||
// Append a single value of a specified type.
|
||||
bool Append(int value);
|
||||
bool Append(const std::string& value);
|
||||
|
||||
// Finalizes construction of the EventLog list and releases the data
|
||||
// to the caller. Caller takes ownership of the payload. No further calls
|
||||
// to append* may be made once the payload is acquired by the caller.
|
||||
void Release(std::unique_ptr<uint8_t[]>* log, size_t* size);
|
||||
|
||||
private:
|
||||
// Appends |data| of the given |size| to the payload.
|
||||
void AppendData(const void* data, size_t size);
|
||||
|
||||
// Appends a single byte to the payload.
|
||||
void AppendByte(uint8_t byte);
|
||||
|
||||
// Returns true iff the remaining capacity in |payload_| is large enough to
|
||||
// accommodate |value_size| bytes. The space required to log the event type
|
||||
// is included in the internal calculation so must not be passed in to
|
||||
// |value_size|.
|
||||
bool IsSpaceAvailable(size_t value_size);
|
||||
|
||||
// The number of items in the EventLog list.
|
||||
size_t payload_count_;
|
||||
|
||||
// The size of the data stored in |payload_|. Used to track where to insert
|
||||
// new data.
|
||||
size_t payload_size_;
|
||||
|
||||
// The payload constructed by calls to log*. The payload may only contain
|
||||
// MAX_EVENT_PAYLOAD (512) bytes.
|
||||
std::unique_ptr<uint8_t[]> payload_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(EventLogListBuilder);
|
||||
};
|
||||
|
||||
#endif // EVENT_LOG_LIST_BUILDER_H_
|
|
@ -1,114 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2016 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 "event_log_list_builder.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <log/log.h>
|
||||
|
||||
using testing::ElementsAreArray;
|
||||
|
||||
TEST(EventLogListBuilder, Empty) {
|
||||
EventLogListBuilder builder;
|
||||
|
||||
const uint8_t EXPECTED_LOG[] = {
|
||||
EVENT_TYPE_LIST,
|
||||
0, // Number of items in the list.
|
||||
};
|
||||
|
||||
std::unique_ptr<uint8_t[]> log;
|
||||
size_t size;
|
||||
builder.Release(&log, &size);
|
||||
EXPECT_EQ(2U, size);
|
||||
|
||||
uint8_t* log_data = log.get();
|
||||
EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size),
|
||||
ElementsAreArray(EXPECTED_LOG));
|
||||
}
|
||||
|
||||
TEST(EventLogListBuilder, SingleInt) {
|
||||
EventLogListBuilder builder;
|
||||
|
||||
const uint8_t EXPECTED_LOG[] = {
|
||||
EVENT_TYPE_LIST,
|
||||
1, // Number of items in the list.
|
||||
EVENT_TYPE_INT,
|
||||
42, 0, 0, 0, // 4 byte integer value.
|
||||
};
|
||||
|
||||
builder.Append(42);
|
||||
|
||||
std::unique_ptr<uint8_t[]> log;
|
||||
size_t size;
|
||||
builder.Release(&log, &size);
|
||||
EXPECT_EQ(7U, size);
|
||||
|
||||
uint8_t* log_data = log.get();
|
||||
EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size),
|
||||
ElementsAreArray(EXPECTED_LOG));
|
||||
}
|
||||
|
||||
TEST(EventLogListBuilder, SingleString) {
|
||||
EventLogListBuilder builder;
|
||||
|
||||
const uint8_t EXPECTED_LOG[] = {
|
||||
EVENT_TYPE_LIST,
|
||||
1, // Number of items in the list.
|
||||
EVENT_TYPE_STRING,
|
||||
5, 0, 0, 0, // 4 byte length of the string.
|
||||
'D', 'r', 'o', 'i', 'd',
|
||||
};
|
||||
|
||||
builder.Append("Droid");
|
||||
|
||||
std::unique_ptr<uint8_t[]> log;
|
||||
size_t size;
|
||||
builder.Release(&log, &size);
|
||||
EXPECT_EQ(12U, size);
|
||||
|
||||
uint8_t* log_data = log.get();
|
||||
EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size),
|
||||
ElementsAreArray(EXPECTED_LOG));
|
||||
}
|
||||
|
||||
TEST(EventLogListBuilder, IntThenString) {
|
||||
EventLogListBuilder builder;
|
||||
|
||||
const uint8_t EXPECTED_LOG[] = {
|
||||
EVENT_TYPE_LIST,
|
||||
2, // Number of items in the list.
|
||||
EVENT_TYPE_INT,
|
||||
42, 0, 0, 0, // 4 byte integer value.
|
||||
EVENT_TYPE_STRING,
|
||||
5, 0, 0, 0, // 4 byte length of the string.
|
||||
'D', 'r', 'o', 'i', 'd',
|
||||
};
|
||||
|
||||
builder.Append(42);
|
||||
builder.Append("Droid");
|
||||
|
||||
std::unique_ptr<uint8_t[]> log;
|
||||
size_t size;
|
||||
builder.Release(&log, &size);
|
||||
EXPECT_EQ(17U, size);
|
||||
|
||||
uint8_t* log_data = log.get();
|
||||
EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size),
|
||||
ElementsAreArray(EXPECTED_LOG));
|
||||
}
|
|
@ -17,27 +17,16 @@
|
|||
#include "histogram_logger.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <log/log.h>
|
||||
|
||||
#include "event_log_list_builder.h"
|
||||
|
||||
namespace bootstat {
|
||||
|
||||
void LogHistogram(const std::string& event, int32_t data) {
|
||||
LOG(INFO) << "Logging histogram: " << event << " " << data;
|
||||
|
||||
EventLogListBuilder log_builder;
|
||||
log_builder.Append(event);
|
||||
log_builder.Append(data);
|
||||
|
||||
std::unique_ptr<uint8_t[]> log;
|
||||
size_t size;
|
||||
log_builder.Release(&log, &size);
|
||||
|
||||
android_bWriteLog(HISTOGRAM_LOG_TAG, log.get(), size);
|
||||
android_log_event_context log(HISTOGRAM_LOG_TAG);
|
||||
log << event << data << LOG_ID_EVENTS;
|
||||
}
|
||||
|
||||
} // namespace bootstat
|
||||
|
|
Loading…
Reference in New Issue