C++ C++;低延迟线程异步缓冲流(用于日志记录)–;促进
问题: 3而下面的循环包含已注释掉的代码。我搜索(“TAG1”、“TAG2”和“TAG3”),以便于识别。我只是想让while循环在测试条件变为true后再继续,同时尽可能减少CPU资源。我第一次尝试使用Boost条件变量,但是有一个竞争条件。将线程休眠“x”微秒是低效的,因为无法精确地确定唤醒时间。最后,boost::this_thread::yield()似乎没有任何作用。可能是因为双核系统上只有2个活动线程。具体来说,我如何使下面的三个标记区域更有效地运行,同时尽可能少地引入不必要的阻塞 背景 目标: 我有一个记录大量数据的应用程序。在分析之后,我发现日志操作(将文本或二进制文件记录到本地硬盘上的文件)花费了很多时间。我的目标是通过将非线程直接写调用替换为对线程缓冲流记录器的调用来减少日志数据调用的延迟 探讨的备选方案:C++ C++;低延迟线程异步缓冲流(用于日志记录)–;促进,c++,multithreading,boost,asynchronous,low-latency,C++,Multithreading,Boost,Asynchronous,Low Latency,问题: 3而下面的循环包含已注释掉的代码。我搜索(“TAG1”、“TAG2”和“TAG3”),以便于识别。我只是想让while循环在测试条件变为true后再继续,同时尽可能减少CPU资源。我第一次尝试使用Boost条件变量,但是有一个竞争条件。将线程休眠“x”微秒是低效的,因为无法精确地确定唤醒时间。最后,boost::this_thread::yield()似乎没有任何作用。可能是因为双核系统上只有2个活动线程。具体来说,我如何使下面的三个标记区域更有效地运行,同时尽可能少地引入不必要的阻塞
- 将2005年代的慢速硬盘升级到SSD…可能。成本不高…但涉及大量工作。。。200多台电脑需要升级
- Boost ASIO…我不需要所有的proactor/网络开销,而是寻找更简单、更轻的东西
- 生产者和消费者线程模式,应用程序将数据写入缓冲区,后台线程稍后将数据写入磁盘。因此,最终目标是让应用层调用的writeMessage函数在稍后某个时间以FIFO顺序正确/完整地将数据记录到日志文件时尽快返回
- 只有一个应用程序线程,只有一个编写器线程
- 基于环形缓冲区。这个决定的原因是使用尽可能少的锁,理想情况下…如果我错了,请纠正我…我认为我不需要任何锁
- 缓冲区是一个静态分配的字符数组,但出于性能原因,如果需要/需要,可以将其移动到堆中
- 缓冲区有一个起始指针,指向应该写入文件的下一个字符。缓冲区有一个结束指针,指向要写入文件的最后一个字符之后的数组索引。结束指针从不经过开始指针。如果传入的消息大于缓冲区,那么写入程序将等待缓冲区清空,并将新消息直接写入文件,而不将过大的消息放入缓冲区(一旦缓冲区清空,工作线程将不会写入任何内容,因此不会发生争用)李>
- 写入程序(工作线程)仅更新环缓冲区的开始指针
- 主(应用程序线程)仅更新环缓冲区的结束指针,并且,它仅在有可用空间时将新数据插入缓冲区……否则,它要么等待缓冲区中的空间变为可用,要么如上所述直接写入
- 工作线程持续检查是否有要写入的数据(由缓冲区开始指针!=缓冲区结束指针的大小写指示)。如果没有要写入的数据,理想情况下,工作线程应该在应用程序线程将某些内容插入缓冲区(并更改缓冲区的结束指针,使其不再指向与开始指针相同的索引)后进入睡眠状态并醒来。我在下面所做的就是不断地检查这个条件。这是一种非常糟糕/低效的等待缓冲区的方式
- 在我2009年使用SSD的双核笔记本电脑上,我看到线程/缓冲基准测试与直接写入的总写入时间约为1:6(0.609秒与0.095秒),但变化很大。通常,缓冲写基准实际上比直接写慢。我认为,这种可变性是由于等待缓冲区中的空间释放、等待缓冲区清空以及让工作线程等待工作变为可用的实现不佳所致。我已经测量到一些while循环消耗超过10000个周期,我怀疑这些周期实际上是在争夺其他线程(工作线程或应用程序)完成等待的计算所需的硬件资源
- 输出似乎得到了验证。启用测试模式,并使用10的小缓冲区大小作为压力测试,我将数百MBs的输出进行了差异,发现它与输入相等
#ifndef BufferedLogStream_h
#define BufferedLogStream_h
#include <stdio.h>
#include <iostream>
#include <iostream>
#include <cstdlib>
#include "boost\chrono\chrono.hpp"
#include "boost\thread\thread.hpp"
#include "boost\thread\locks.hpp"
#include "boost\thread\mutex.hpp"
#include "boost\thread\condition_variable.hpp"
#include <time.h>
using namespace std;
#define BENCHMARK_STR_SIZE 128
#define NUM_BENCHMARK_WRITES 524288
#define TEST 0
#define BENCHMARK 1
#define WORKER_LOOP_WAIT_MICROSEC 20
#define MAIN_LOOP_WAIT_MICROSEC 10
#if(TEST)
#define BUFFER_SIZE 10
#else
#define BUFFER_SIZE 33554432 //4 MB
#endif
class BufferedLogStream {
public:
BufferedLogStream();
void openFile(char* filename);
void flush();
void close();
inline void writeMessage(const char* message, unsigned int length);
void writeMessage(string message);
bool operator() () { return start != end; }
private:
void threadedWriter();
inline bool hasSomethingToWrite();
inline unsigned int getFreeSpaceInBuffer();
void appendStringToBuffer(const char* message, unsigned int length);
FILE* fp;
char* start;
char* end;
char* endofringbuffer;
char ringbuffer[BUFFER_SIZE];
bool workerthreadkeepalive;
boost::mutex mtx;
boost::condition_variable waitforempty;
boost::mutex workmtx;
boost::condition_variable waitforwork;
#if(TEST)
struct testbuffer {
int length;
char message[BUFFER_SIZE * 2];
};
public:
void test();
private:
void getNextRandomTest(testbuffer &tb);
FILE* datatowrite;
#endif
#if(BENCHMARK)
public:
void runBenchmark();
private:
void initBenchmarkString();
void runDirectWriteBaseline();
void runBufferedWriteBenchmark();
char benchmarkstr[BENCHMARK_STR_SIZE];
#endif
};
#if(TEST)
int main() {
BufferedLogStream* bl = new BufferedLogStream();
bl->openFile("replicated.txt");
bl->test();
bl->close();
cout << "Done" << endl;
cin.get();
return 0;
}
#endif
#if(BENCHMARK)
int main() {
BufferedLogStream* bl = new BufferedLogStream();
bl->runBenchmark();
cout << "Done" << endl;
cin.get();
return 0;
}
#endif //for benchmark
#endif
\ifndef BufferedLogStream\u h
#定义BufferedLogStream_h
#包括
#包括
#包括
#包括
#包括“boost\chrono\chrono.hpp”
#包括“boost\thread\thread.hpp”
#包括“boost\thread\locks.hpp”
#包括“boost\thread\mutex.hpp”
#包括“boost\thread\condition\u variable.hpp”
#包括
使用名称空间std;
#定义基准结构尺寸128
#定义NUM_BENCHMARK_写入524288
#定义测试0
#定义基准1
#定义工作循环等待微秒20
#定义主循环等待微秒10
#如果(测试)
#定义缓冲区大小10
#否则
#定义缓冲区大小33554432//4 MB
#恩迪夫
类BufferedLogStream{
公众:
BufferedLogStream();
作废openFile(字符*文件名);
无效冲洗();
无效关闭();
内联无效写消息(常量字符*消息,无符号整数长度);
无效写消息(字符串消息);
bool运算符()({返回开始!=结束;}
私人:
void threadedWriter();
#include "BufferedLogStream.h"
BufferedLogStream::BufferedLogStream() {
fp = NULL;
start = ringbuffer;
end = ringbuffer;
endofringbuffer = ringbuffer + BUFFER_SIZE;
workerthreadkeepalive = true;
}
void BufferedLogStream::openFile(char* filename) {
if(fp) close();
workerthreadkeepalive = true;
boost::thread t2(&BufferedLogStream::threadedWriter, this);
fp = fopen(filename, "w+b");
}
void BufferedLogStream::flush() {
fflush(fp);
}
void BufferedLogStream::close() {
workerthreadkeepalive = false;
if(!fp) return;
while(hasSomethingToWrite()) {
boost::unique_lock<boost::mutex> u(mtx);
waitforempty.wait_for(u, boost::chrono::microseconds(MAIN_LOOP_WAIT_MICROSEC));
}
flush();
fclose(fp);
fp = NULL;
}
void BufferedLogStream::threadedWriter() {
while(true) {
if(start != end) {
char* currentend = end;
if(start < currentend) {
fwrite(start, 1, currentend - start, fp);
}
else if(start > currentend) {
if(start != endofringbuffer) fwrite(start, 1, endofringbuffer - start, fp);
fwrite(ringbuffer, 1, currentend - ringbuffer, fp);
}
start = currentend;
waitforempty.notify_one();
}
else { //start == end...no work to do
if(!workerthreadkeepalive) return;
boost::unique_lock<boost::mutex> u(workmtx);
waitforwork.wait_for(u, boost::chrono::microseconds(WORKER_LOOP_WAIT_MICROSEC));
}
}
}
bool BufferedLogStream::hasSomethingToWrite() {
return start != end;
}
void BufferedLogStream::writeMessage(string message) {
writeMessage(message.c_str(), message.length());
}
unsigned int BufferedLogStream::getFreeSpaceInBuffer() {
if(end > start) return (start - ringbuffer) + (endofringbuffer - end) - 1;
if(end == start) return BUFFER_SIZE-1;
return start - end - 1; //case where start > end
}
void BufferedLogStream::appendStringToBuffer(const char* message, unsigned int length) {
if(end + length <= endofringbuffer) { //most common case for appropriately-sized buffer
memcpy(end, message, length);
end += length;
}
else {
int lengthtoendofbuffer = endofringbuffer - end;
if(lengthtoendofbuffer > 0) memcpy(end, message, lengthtoendofbuffer);
int remainderlength = length - lengthtoendofbuffer;
memcpy(ringbuffer, message + lengthtoendofbuffer, remainderlength);
end = ringbuffer + remainderlength;
}
}
void BufferedLogStream::writeMessage(const char* message, unsigned int length) {
if(length > BUFFER_SIZE - 1) { //if string is too large for buffer, wait for buffer to empty and bypass buffer, write directly to file
while(hasSomethingToWrite()); {
boost::unique_lock<boost::mutex> u(mtx);
waitforempty.wait_for(u, boost::chrono::microseconds(MAIN_LOOP_WAIT_MICROSEC));
}
fwrite(message, 1, length, fp);
}
else {
//wait until there is enough free space to insert new string
while(getFreeSpaceInBuffer() < length) {
boost::unique_lock<boost::mutex> u(mtx);
waitforempty.wait_for(u, boost::chrono::microseconds(MAIN_LOOP_WAIT_MICROSEC));
}
appendStringToBuffer(message, length);
}
waitforwork.notify_one();
}
#if(TEST)
void BufferedLogStream::getNextRandomTest(testbuffer &tb) {
tb.length = 1 + (rand() % (int)(BUFFER_SIZE * 1.05));
for(int i = 0; i < tb.length; i++) {
tb.message[i] = rand() % 26 + 65;
}
tb.message[tb.length] = '\n';
tb.length++;
tb.message[tb.length] = '\0';
}
void BufferedLogStream::test() {
cout << "Buffer size is: " << BUFFER_SIZE << endl;
testbuffer tb;
datatowrite = fopen("orig.txt", "w+b");
for(unsigned int i = 0; i < 7000000; i++) {
if(i % 1000000 == 0) cout << i << endl;
getNextRandomTest(tb);
writeMessage(tb.message, tb.length);
fwrite(tb.message, 1, tb.length, datatowrite);
}
fflush(datatowrite);
fclose(datatowrite);
}
#endif
#if(BENCHMARK)
void BufferedLogStream::initBenchmarkString() {
for(unsigned int i = 0; i < BENCHMARK_STR_SIZE - 1; i++) {
benchmarkstr[i] = rand() % 26 + 65;
}
benchmarkstr[BENCHMARK_STR_SIZE - 1] = '\n';
}
void BufferedLogStream::runDirectWriteBaseline() {
clock_t starttime = clock();
fp = fopen("BenchMarkBaseline.txt", "w+b");
for(unsigned int i = 0; i < NUM_BENCHMARK_WRITES; i++) {
fwrite(benchmarkstr, 1, BENCHMARK_STR_SIZE, fp);
}
fflush(fp);
fclose(fp);
clock_t elapsedtime = clock() - starttime;
cout << "Direct write baseline took " << ((double) elapsedtime) / CLOCKS_PER_SEC << " seconds." << endl;
}
void BufferedLogStream::runBufferedWriteBenchmark() {
clock_t starttime = clock();
openFile("BufferedBenchmark.txt");
cout << "Opend file" << endl;
for(unsigned int i = 0; i < NUM_BENCHMARK_WRITES; i++) {
writeMessage(benchmarkstr, BENCHMARK_STR_SIZE);
}
cout << "Wrote" << endl;
close();
cout << "Close" << endl;
clock_t elapsedtime = clock() - starttime;
cout << "Buffered write took " << ((double) elapsedtime) / CLOCKS_PER_SEC << " seconds." << endl;
}
void BufferedLogStream::runBenchmark() {
cout << "Buffer size is: " << BUFFER_SIZE << endl;
initBenchmarkString();
runDirectWriteBaseline();
runBufferedWriteBenchmark();
}
#endif
// pseudocode
void write(const char* message) {
int len = strlen(message); // TODO: round to cache line size
const char* old_prepare_ptr;
// Prepare step
while(1)
{
old_prepare_ptr = prepare_ptr;
if (
CAS(&prepare_ptr,
old_prepare_ptr,
prepare_ptr + len) == old_prepare_ptr
)
break;
// retry if another thread perform prepare op.
}
// Write message
memcpy((void*)old_prepare_ptr, (void*)message, len);
// Commit step
while(1)
{
const char* old_commit_ptr = commit_ptr;
if (
CAS(&commit_ptr,
old_commit_ptr,
old_commit_ptr + len) == old_commit_ptr
)
break;
// retry if another thread commits
}
notify_worker_thread();
}
template<typename T, size_t N>
class concurrent_queue
{
// T can be wrapped into struct with padding in order to avoid false sharing
mutable boost::lockfree::spsc_queue<T, boost::lockfree::capacity<N>> q;
mutable mutex m;
mutable condition_variable c;
void wait() const
{
unique_lock<mutex> u(m);
c.wait_for(u, chrono::microseconds(1)); // Or whatever period you need.
// Timeout is required, because modification happens not under mutex
// and notification can be lost.
// Another option is just to use sleep/yield, without notifications.
}
void notify() const
{
c.notify_one();
}
public:
void push(const T &t)
{
while(!q.push(t))
wait();
notify();
}
void pop(T &result)
{
while(!q.pop(result))
wait();
notify();
}
};
concurrent<ostream*> x(&cout); // starts thread internally
// ...
// x acts as function object.
// It's function call operator accepts action
// which is performed on wrapped object in separate thread.
int i = 42;
x([i](ostream *out){ *out << "i=" << i; }); // passing lambda as action
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <condition_variable>
#include <iostream>
#include <cstddef>
#include <thread>
#include <chrono>
#include <mutex>
using namespace std;
/*********************************************/
template<typename T, size_t N>
class concurrent_queue
{
mutable boost::lockfree::spsc_queue<T, boost::lockfree::capacity<N>> q;
mutable mutex m;
mutable condition_variable c;
void wait() const
{
unique_lock<mutex> u(m);
c.wait_for(u, chrono::microseconds(1));
}
void notify() const
{
c.notify_one();
}
public:
void push(const T &t)
{
while(!q.push(t))
wait();
notify();
}
void pop(T &result)
{
while(!q.pop(result))
wait();
notify();
}
};
/*********************************************/
template<typename T, typename F>
class concurrent
{
typedef boost::optional<F> Job;
mutable concurrent_queue<Job, 16> q; // use custom size
mutable T x;
thread worker;
public:
concurrent(T x)
: x{x}, worker{[this]
{
Job j;
while(true)
{
q.pop(j);
if(!j) break;
(*j)(this->x); // you may need to handle exceptions in some way
}
}}
{}
void operator()(const F &f)
{
q.push(Job{f});
}
~concurrent()
{
q.push(Job{});
worker.join();
}
};
/*********************************************/
struct LogEntry
{
struct Formatter
{
typedef void result_type;
ostream *out;
void operator()(double x) const
{
*out << "floating point: " << x << endl;
}
void operator()(int x) const
{
*out << "integer: " << x << endl;
}
};
boost::variant<int, double> data;
void operator()(ostream *out)
{
boost::apply_visitor(Formatter{out}, data);
}
};
/*********************************************/
int main()
{
concurrent<ostream*, LogEntry> log{&cout};
for(int i=0; i!=1024; ++i)
{
log({i});
log({i/10.});
}
}