一个轻巧高效的多线程c++stream风格异步日志(二)


前言

本文紧接上一篇文章: 介绍上文中的一条条日志是如何异步导入本地文件的.

首先会简单介绍下LogFile类,之后会具体讲解下AsyncLogging中的双缓冲机制.

整个日志模块的结构图,

LogFile类

LogFile日志文件类 完成日志文件的管理工作.

rollFile() :滚动文件 当日志超过m_rollSize大小时会滚动一个新的日志文件出来.

getLogFileName() :用与滚动日志时,给日志文件取名,以滚动时间作为后缀.

m_mutex :用于append()数据时,给文件上锁.

append() :黏入日志.

flush() :冲刷缓冲.

LogFile 有一个AppendFIle类,它是最终用于操作本地文件的类.

append() : 里面会调用系统函数fwrite()写入本地文件.

flush() : 冲刷缓冲.

writtenBytes() : 获取已写字节数.

AsyncLogging类

AsyncLogging异步日志类, 完成日志的异步写入工作.

介绍它的接口前,先描述下它的工作逻辑.

AsyncLogging 有以下述几类缓存.

m_currentBuffer : 指向当前接收其他线程append过来的日志的缓存.

m_buffers : 用于存放当前已写满或过了冲刷周期的日志缓存的指针容器.

m_nextBuffer : 指向当m_currentBuffer满后用于替代m_currentBuffer的缓存.

backupBuffer1 : 备用缓存.

backupBuffer2 : 备用缓存.

buffersToWrite : 和m_buffers通过交换swap()后append()到LogFile的指针容器.

AsyncLogging 使用的双缓冲机制 有两个缓存容器 : m_buffers 、buffersToWrite 交替使用 . 一下我们简称为 A 和 B .

A 用于接收 其他线程 append() 进来的日志.

B 用于将目前已接受的缓存 写入 日志文件. 当B写完时 , clean() B , 交换A,B,如此往复.

优点 : 新建的日志不必等待磁盘操作,也避免了每条新日志都触发日志线程,而是将多条日志拼程一个大的buffer 传送给日志线程写入文件. 相当于批处理, 减少线程唤醒频率 ,降低开销。

另外 ,为了及时将 日志消息写入文件, 即是 buffer A 中还没有push进来日志 也会每三秒 执行一次上述的写入操作.

AsyncLogging使用一个更大的LogBuffer来保存一条条Logger传送过来的日志.

Mutex :用来控制多线程的写入.

Condition : 用来等待缓冲区中的数据.

Thread : 使用一个线程处理缓存的交换,以及日志的写入.

AsyncLogging实现

下面会给出AsyncLogging的简单实现.

实际上还有几个备用缓存,这里没有加上去,以便于理解程序; 备用缓存主要是为了减少反复new 操作带来的系统开销,

#ifndef _ASYNC_LOGGING_HH
#define _ASYNC_LOGGING_HH
#include "MutexLock.hh"
#include "Thread.hh"
#include "LogStream.hh"
#include "ptr_vector.hh"
#include "Condition.hh" #include <string> class AsyncLogging
{
public:
AsyncLogging(const std::string filePath, off_t rollSize, int flushInterval = 3);
~AsyncLogging(); void start(){
m_isRunning = true;
m_thread.start();
} void stop(){
m_isRunning = false;
m_cond.notify();
} void append(const char *logline, int len); private:
AsyncLogging(const AsyncLogging&);
AsyncLogging& operator=(const AsyncLogging&); void threadRoutine(); typedef LogBuffer<kLargeBuffer> Buffer;
typedef oneself::ptr_vector<Buffer> BufferVector;
typedef oneself::auto_ptr<Buffer> BufferPtr; const int m_flushInterval;
bool m_isRunning;
off_t m_rollSize;
std::string m_filePath;
Thread m_thread;
MutexLock m_mutex;
Condition m_cond; BufferPtr m_currentBuffer;
BufferVector m_buffers;
}; #endif //AsyncLogging.cpp
#include "AsyncLogging.hh"
#include "LogFile.hh"
#include <assert.h>
#include <stdio.h> AsyncLogging::AsyncLogging(const std::string filePath, off_t rollSize, int flushInterval)
:m_filePath(filePath),
m_rollSize(2048),
m_flushInterval(flushInterval),
m_isRunning(false),
m_thread(std::bind(&AsyncLogging::threadRoutine, this)),
m_mutex(),
m_cond(m_mutex),
m_currentBuffer(new Buffer),
m_buffers()
{
} AsyncLogging::~AsyncLogging(){
if(m_isRunning) stop();
} void AsyncLogging::append(const char* logline, int len){
MutexLockGuard lock(m_mutex);
if(m_currentBuffer->avail() > len){
m_currentBuffer->append(logline, len);
}
else{
m_buffers.push_back(m_currentBuffer.release()); m_currentBuffer.reset(new Buffer); m_currentBuffer->append(logline, len);
m_cond.notify();
}
} void AsyncLogging::threadRoutine(){
assert(m_isRunning == true);
LogFile output(m_filePath, m_rollSize, false);
BufferVector buffersToWrite;
buffersToWrite.reserve(8); while(m_isRunning){
assert(buffersToWrite.empty());
{
MutexLockGuard lock(m_mutex);
if(m_buffers.empty()){
m_cond.waitForSeconds(m_flushInterval);
}
m_buffers.push_back(m_currentBuffer.release());
m_currentBuffer.reset(new Buffer);
m_buffers.swap(buffersToWrite);
} assert(!buffersToWrite.empty()); for(size_t i = 0; i < buffersToWrite.size(); ++i){
output.append(buffersToWrite[i]->data(), buffersToWrite[i]->length());
} buffersToWrite.clear();
output.flush();
} output.flush();
}

增加备用缓存

增加备用缓存优化上面程序,上面程序一共在两个地方执行了new操作.

1.m_currentBuffer 填满时,需要把它填进容器的时候.

2.到时间了需要把m_currentBuffer里面的内容写入本地文件时,会把它当前的内容移出来,这时候需要new一个新缓存来给m_currentBuffer.

于是我们准备一个m_nextBuffer来做m_currentBuffer的备用缓存.同时在线程中增加两个backupBuffer 给m_nextBuffer 当备用缓存;当日志量大到不够用的时候, 再考虑用new 操作来动态添加缓存。

#ifndef _ASYNC_LOGGING_HH
#define _ASYNC_LOGGING_HH
#include "MutexLock.hh"
#include "Thread.hh"
#include "LogStream.hh"
#include "ptr_vector.hh" #include "Condition.hh"
#include <memory>
#include <string> class AsyncLogging
{
public:
AsyncLogging(const std::string filePath, off_t rollSize, int flushInterval = 3);
~AsyncLogging(); void start(){
m_isRunning = true;
m_thread.start();
} void stop(){
m_isRunning = false;
m_cond.notify();
} void append(const char *logline, int len); private:
AsyncLogging(const AsyncLogging&);
AsyncLogging& operator=(const AsyncLogging&); void threadRoutine(); typedef LogBuffer<kLargeBuffer> Buffer;
typedef myself::ptr_vector<Buffer> BufferVector;
typedef std::unique_ptr<Buffer> BufferPtr; const int m_flushInterval;
bool m_isRunning;
off_t m_rollSize;
std::string m_filePath;
Thread m_thread;
MutexLock m_mutex;
Condition m_cond; BufferPtr m_currentBuffer;
BufferPtr m_nextBuffer;
BufferVector m_buffers;
}; #endif //AsynvLogging.cpp
#include "AsyncLogging.hh"
#include "LogFile.hh"
#include <assert.h>
#include <stdio.h> AsyncLogging::AsyncLogging(const std::string filePath, off_t rollSize, int flushInterval)
:m_filePath(filePath),
m_rollSize(rollSize),
m_flushInterval(flushInterval),
m_isRunning(false),
m_thread(std::bind(&AsyncLogging::threadRoutine, this)),
m_mutex(),
m_cond(m_mutex),
m_currentBuffer(new Buffer),
m_nextBuffer(new Buffer),
m_buffers()
{
} AsyncLogging::~AsyncLogging(){
if(m_isRunning) stop();
} void AsyncLogging::append(const char* logline, int len){
MutexLockGuard lock(m_mutex);
if(m_currentBuffer->avail() > len){
m_currentBuffer->append(logline, len);
}
else{
m_buffers.push_back(m_currentBuffer.release()); if(m_nextBuffer){
m_currentBuffer = std::move(m_nextBuffer);
}
else{
m_currentBuffer.reset(new Buffer);
} m_currentBuffer->append(logline, len);
m_cond.notify();
}
} void AsyncLogging::threadRoutine(){
assert(m_isRunning == true);
LogFile output(m_filePath, m_rollSize, false);
BufferPtr backupBuffer1(new Buffer);
BufferPtr backupBuffer2(new Buffer);
BufferVector buffersToWrite;
buffersToWrite.reserve(8); while(m_isRunning){
assert(buffersToWrite.empty());
{
MutexLockGuard lock(m_mutex);
if(m_buffers.empty()){
m_cond.waitForSeconds(m_flushInterval);
}
m_buffers.push_back(m_currentBuffer.release());
m_currentBuffer = std::move(backupBuffer1);
m_buffers.swap(buffersToWrite);
if(!m_nextBuffer)
m_nextBuffer = std::move(backupBuffer2);
} assert(!buffersToWrite.empty()); for(size_t i = 0; i < buffersToWrite.size(); ++i){
output.append(buffersToWrite[i]->data(), buffersToWrite[i]->length());
} if(buffersToWrite.size() > 2)
{
// drop non-bzero-ed buffers, avoid trashing
buffersToWrite.resize(2);
} if(!backupBuffer1)
{
assert(!buffersToWrite.empty());
backupBuffer1 = std::move(buffersToWrite.pop_back());
backupBuffer1->reset();
} if(!backupBuffer2)
{
assert(!buffersToWrite.empty());
backupBuffer2 = std::move(buffersToWrite.pop_back());
backupBuffer2->reset();
} buffersToWrite.clear();
output.flush();
} output.flush();
}

结语

本文主要介绍了muduo中AsyncLogging类的实现,其中的双缓存机制.

LogFile类及AppendFIle类 分别是日志文件管理类和本地文件的基本操作类. 不难理解,感兴趣的话可以看看muduo的源码,本文不再往下写了,如果想要全部源码可以留言。

最新源码:

https://github.com/BethlyRoseDaisley/SimpleMuduo/tree/master/AsyncLogging

一个轻巧高效的多线程c++stream风格异步日志(二)的更多相关文章

  1. 一个轻巧高效的多线程c++stream风格异步日志(一)

    一个轻巧高效的多线程c++stream风格异步日志 一个轻巧高效的多线程c++stream风格异步日志 前言 功能需求 性能需求 Logger实现 LogStream类 Logger类 LogStre ...

  2. WaitForSingleObject与WaitForMultipleObjects用法详解(好用,而且进入一个非常高效沉睡状态,只占用极少的CPU时间片)

    在多线程下面,有时候会希望等待某一线程完成了再继续做其他事情,要实现这个目的,可以使用Windows API函数WaitForSingleObject,或者WaitForMultipleObjects ...

  3. [转]一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程

    一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程 希望此文能给初学多线程编程的朋友带来帮助,也希望牛人多多指出错误. 另外感谢以下链接的作者给予,给我的学习带来了很大帮助 http ...

  4. (转)log4j(四)——如何控制不同风格的日志信息的输出?

    一:测试环境与log4j(一)——为什么要使用log4j?一样,这里不再重述 1 老规矩,先来个栗子,然后再聊聊感受 import org.apache.log4j.*; //by godtrue p ...

  5. python 多进程/多线程/协程 同步异步

    这篇主要是对概念的理解: 1.异步和多线程区别:二者不是一个同等关系,异步是最终目的,多线程只是我们实现异步的一种手段.异步是当一个调用请求发送给被调用者,而调用者不用等待其结果的返回而可以做其它的事 ...

  6. log4j(四)——如何控制不同风格的日志信息的输出?

    一:测试环境与log4j(一)——为什么要使用log4j?一样,这里不再重述 二:老规矩,先来个栗子,然后再聊聊感受 import org.apache.log4j.*; //by godtrue p ...

  7. Java8新特性 Stream流式思想(二)

    如何获取Stream流刚开始写博客,有一些不到位的地方,还请各位论坛大佬见谅,谢谢! package cn.com.zq.demo01.Stream.test01.Stream; import org ...

  8. 写一个Windows上的守护进程(4)日志其余

    写一个Windows上的守护进程(4)日志其余 这次把和日志相关的其他东西一并说了. 一.vaformat C++日志接口通常有两种形式:流输入形式,printf形式. 我采用printf形式,因为流 ...

  9. Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token

    原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf?   因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...

随机推荐

  1. 8.30 牛客OI赛制测试赛1 F题 子序列

    题目描述 给出一个长度为n的序列,你需要计算出所有长度为k的子序列中,除最大最小数之外所有数的乘积相乘的结果 输入描述: 第一行一个整数T,表示数据组数.对于每组数据,第一行两个整数N,k,含义如题所 ...

  2. 装饰器python

    装饰器 你是一家视频网站的后端开发工程师,你们网站有以下几个版块 1 2 3 4 5 6 7 8 9 10 11 def home():     print("---首页----" ...

  3. Flink窗口介绍及应用

    Windows是Flink流计算的核心,本文将概括的介绍几种窗口的概念,重点只放在窗口的应用上. 本实验的数据采用自拟电影评分数据(userId, movieId, rating, timestamp ...

  4. Java基础-SSM之mybatis多对多关联

    Java基础-SSM之mybatis多对多关联 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表) 1>.创建teas,stus,links表 u ...

  5. JMS学习(六)--提高非持久订阅者的可靠性 以及 订阅恢复策略

    一,非持久订阅者 和 实时消费消息 在这篇文章中区分了Domain为Pub/Sub.Destination为Topic时,消费者有两种:持久订阅者 和 非持久订阅者. 对于持久订阅者而言,只要订阅了某 ...

  6. [整理]Visual Studio 的Application Insights

    简单介绍 Application Insights(预览版) Visual Studio 的Application Insights插件简介 Application Insights for Visu ...

  7. 矩阵乘法优化DP

    本文讲一下一些基本的矩阵优化DP的方法技巧. 定义三个矩阵A,B,C,其中行和列分别为$m\times n,n \times p,m\times p$,(其中行是从上往下数的,列是从左往右数的) $C ...

  8. Django之模板语法

    Django框架之第三篇模板语法(重要!!!) 一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 一.模板语法之变量:语法为 {{ }}: ...

  9. mongodb导入json文件

    mongoimport --db test  --collection item --jsonArray  item.json

  10. Linux中断(interrupt)子系统之三:中断流控处理层【转】

    转自:http://blog.csdn.net/droidphone/article/details/7489756 1.  中断流控层简介 早期的内核版本中,几乎所有的中断都是由__do_IRQ函数 ...