logd: move current sequence from LogBufferElement to LogBuffer

This is required for tests that are aware of sequence numbers to pass;
each new LogBuffer instance should start from sequence = 1, which
isn't the case if the current sequence number is a static.

Test: unit tests
Change-Id: Ie488f8ac5e22b946b7e6237d1d5caf14929c0ec3
This commit is contained in:
Tom Cherry 2020-05-21 13:56:33 -07:00
parent f29cd20df5
commit a3c5ff5d3f
6 changed files with 14 additions and 12 deletions

View file

@ -177,7 +177,8 @@ int ChattyLogBuffer::Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pi
// exact entry with time specified in ms or us precision.
if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
LogBufferElement elem(log_id, realtime, uid, pid, tid, msg, len);
auto sequence = sequence_.fetch_add(1, std::memory_order_relaxed);
LogBufferElement elem(log_id, realtime, uid, pid, tid, sequence, msg, len);
// b/137093665: don't coalesce security messages.
if (log_id == LOG_ID_SECURITY) {

View file

@ -18,6 +18,7 @@
#include <sys/types.h>
#include <atomic>
#include <list>
#include <optional>
#include <string>
@ -67,6 +68,8 @@ class ChattyLogBuffer : public LogBuffer {
unsigned long GetSize(log_id_t id) override;
int SetSize(log_id_t id, unsigned long size) override;
uint64_t sequence() const override { return sequence_.load(std::memory_order_relaxed); }
private:
void maybePrune(log_id_t id) REQUIRES(lock_);
void kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) REQUIRES_SHARED(lock_);
@ -92,6 +95,8 @@ class ChattyLogBuffer : public LogBuffer {
RwLock lock_;
std::atomic<uint64_t> sequence_ = 1;
// This always contains a copy of the last message logged, for deduplication.
std::optional<LogBufferElement> last_logged_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
// This contains an element if duplicate messages are seen.

View file

@ -53,4 +53,6 @@ class LogBuffer {
virtual bool Clear(log_id_t id, uid_t uid) = 0;
virtual unsigned long GetSize(log_id_t id) = 0;
virtual int SetSize(log_id_t id, unsigned long size) = 0;
virtual uint64_t sequence() const = 0;
};

View file

@ -30,14 +30,12 @@
#include "LogStatistics.h"
#include "LogUtils.h"
atomic_int_fast64_t LogBufferElement::sequence(1);
LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
pid_t tid, const char* msg, uint16_t len)
pid_t tid, uint64_t sequence, const char* msg, uint16_t len)
: mUid(uid),
mPid(pid),
mTid(tid),
mSequence(sequence.fetch_add(1, memory_order_relaxed)),
mSequence(sequence),
mRealTime(realtime),
mMsgLen(len),
mLogId(log_id),

View file

@ -16,7 +16,6 @@
#pragma once
#include <stdatomic.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
@ -51,14 +50,12 @@ class __attribute__((packed)) LogBufferElement {
const uint8_t mLogId;
bool mDropped;
static atomic_int_fast64_t sequence;
// assumption: mDropped == true
size_t populateDroppedMessage(char*& buffer, LogStatistics* parent, bool lastSame);
public:
LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
pid_t tid, const char* msg, uint16_t len);
LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
uint64_t sequence, const char* msg, uint16_t len);
LogBufferElement(const LogBufferElement& elem);
LogBufferElement(LogBufferElement&& elem);
~LogBufferElement();
@ -91,7 +88,6 @@ class __attribute__((packed)) LogBufferElement {
return mDropped ? nullptr : mMsg;
}
uint64_t getSequence() const { return mSequence; }
static uint64_t getCurrentSequence() { return sequence.load(memory_order_relaxed); }
log_time getRealTime(void) const {
return mRealTime;
}

View file

@ -200,7 +200,7 @@ bool LogReader::onDataAvailable(SocketClient* cli) {
if (nonBlock) {
return false;
}
sequence = LogBufferElement::getCurrentSequence();
sequence = log_buffer_->sequence();
}
}