platform_system_core/logd/LogBufferTest.h

95 lines
3.1 KiB
C
Raw Normal View History

/*
* Copyright (C) 2020 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.
*/
#pragma once
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include "ChattyLogBuffer.h"
#include "LogReaderList.h"
#include "LogStatistics.h"
#include "LogTags.h"
#include "PruneList.h"
#include "SerializedLogBuffer.h"
#include "SimpleLogBuffer.h"
struct LogMessage {
logger_entry entry;
std::string message;
bool regex_compare = false; // Only set for expected messages, when true 'message' should be
// interpretted as a regex.
};
// Compares the ordered list of expected and result, causing a test failure with appropriate
// information on failure.
void CompareLogMessages(const std::vector<LogMessage>& expected,
const std::vector<LogMessage>& result);
// Sets hdr_size and len parameters appropriately.
void FixupMessages(std::vector<LogMessage>* messages);
class TestWriter : public LogWriter {
public:
TestWriter(std::vector<LogMessage>* msgs, bool* released)
: LogWriter(0, true), msgs_(msgs), released_(released) {}
bool Write(const logger_entry& entry, const char* message) override {
msgs_->emplace_back(LogMessage{entry, std::string(message, entry.len), false});
return true;
}
void Release() {
if (released_) *released_ = true;
}
std::string name() const override { return "test_writer"; }
private:
std::vector<LogMessage>* msgs_;
bool* released_;
};
class LogBufferTest : public testing::TestWithParam<std::string> {
protected:
void SetUp() override {
if (GetParam() == "chatty") {
log_buffer_.reset(new ChattyLogBuffer(&reader_list_, &tags_, &prune_, &stats_));
} else if (GetParam() == "serialized") {
log_buffer_.reset(new SerializedLogBuffer(&reader_list_, &tags_, &stats_));
} else if (GetParam() == "simple") {
log_buffer_.reset(new SimpleLogBuffer(&reader_list_, &tags_, &stats_));
} else {
FAIL() << "Unknown buffer type selected for test";
}
log_id_for_each(i) { log_buffer_->SetSize(i, 1024 * 1024); }
}
void LogMessages(const std::vector<LogMessage>& messages) {
for (auto& [entry, message, _] : messages) {
log_buffer_->Log(static_cast<log_id_t>(entry.lid), log_time(entry.sec, entry.nsec),
entry.uid, entry.pid, entry.tid, message.c_str(), message.size());
}
}
LogReaderList reader_list_;
LogTags tags_;
PruneList prune_;
logd: optionally track the full size of log buffers ChattyLogBuffer ignores the metadata (timestamp, pid, std::list<> iterators, etc) of log entries when calculating the size used by a given log buffer. For example, if 1MB is the specified size of the 'main' log buffer, logd will use between ~1.3MB and ~2MB of overall memory for 'main' log buffer. LogStatistics does track the overall memory used and labels it 'Overhead', however this 'Overhead' is only informative and is not used for Pruning or Chatty calculations. This is problematic, since it makes logd's memory usage inconsistent: depending on the pattern of logging, there can be substantially more memory used than the specified log buffer size. This is further complicated by the fact that chatty messages are entirely metadata and therefore not counted as contributing to the log buffer size. This change would switch logd to always track the full size of log buffers, but there are two problems with this approach: 1) Unless users double their buffer sizes, then they'd have substantially fewer logs after the change 2) Chatty logic would change and it's difficult to evaluate. Therefore this change only provides the framework to track the full size of log buffers. This allows an apples to apples comparison of ChattyLogBuffer and SerializedLogBuffer. With this option enabled, logd reports the following values: ChattyLogBuffer: Total log size (logcat -g), 'Total' / 'Now' (logcat -S), and 'Overhead' (logcat -S) all report the full size of log entries including metadata. SerializedLogBuffer: Total log size (logcat -g) and 'Overhead' (logcat -S) report the compressed size of the log entries including metadata. 'Total' / 'Now' (logcat -S) reports the uncompressed size of the log entries that are available including metadata. Test: logging statistics are correct Change-Id: If17682af8bb605f31387d7b210b69a301dd48f07
2020-06-19 21:21:21 +02:00
LogStatistics stats_{false, true};
std::unique_ptr<LogBuffer> log_buffer_;
};