Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize: support for single log message bigger than muduo::detail::kLargeBuffer #702

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions muduo/base/AsyncLogging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,12 @@ AsyncLogging::AsyncLogging(const string& basename,

void AsyncLogging::append(const char* logline, int len)
{
muduo::MutexLockGuard lock(mutex_);
if (currentBuffer_->avail() > len)
if (!running_)
{
currentBuffer_->append(logline, len);
}
else
{
buffers_.push_back(std::move(currentBuffer_));

if (nextBuffer_)
{
currentBuffer_ = std::move(nextBuffer_);
}
else
{
currentBuffer_.reset(new Buffer); // Rarely happens
}
currentBuffer_->append(logline, len);
cond_.notify();
return;
}
muduo::MutexLockGuard lock(mutex_);
append_unlocked(logline, len);
}

void AsyncLogging::threadFunc()
Expand Down Expand Up @@ -134,3 +120,32 @@ void AsyncLogging::threadFunc()
output.flush();
}

void AsyncLogging::append_unlocked(const char* buf, int len)
{
mutex_.assertLocked();
while (len > kBufferSize)
{
append_unlocked(buf, kBufferSize);
buf += kBufferSize;
len -= kBufferSize;
}
if (currentBuffer_->avail() > len)
{
currentBuffer_->append(buf, len);
}
else
{
buffers_.push_back(std::move(currentBuffer_));

if (nextBuffer_)
{
currentBuffer_ = std::move(nextBuffer_);
}
else
{
currentBuffer_.reset(new Buffer); // Rarely happens
}
currentBuffer_->append(buf, len);
cond_.notify();
}
}
4 changes: 3 additions & 1 deletion muduo/base/AsyncLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ class AsyncLogging : noncopyable
private:

void threadFunc();
void append_unlocked(const char* buf, int len) REQUIRES(mutex_);

typedef muduo::detail::FixedBuffer<muduo::detail::kLargeBuffer> Buffer;
static const int kBufferSize = muduo::detail::kLargeBuffer;
typedef muduo::detail::FixedBuffer<kBufferSize> Buffer;
typedef std::vector<std::unique_ptr<Buffer>> BufferVector;
typedef BufferVector::value_type BufferPtr;

Expand Down