Suggestion for Async_log_helper
I am not completely familiar with the async logging feature. So if this is a dumb comment please close this thread.
Looking at async_log_helper there are sleeps and thread yields in there. The sleep_or_yield method has some curious code in it and the flushes to file only occur periodically (200ms I think?). Hence watching the log in real time really doesn't work unless the coarse 200ms is acceptable. I am sure this is for efficiency with the I/O.
One suggestion I have is instead of sleeping/yielding with the time checks, you might want to look at a blocking queue where you actually have 2 threads sharing a queue. The calling thread (the app code) causes a log record to be placed on your blocking queue. This wakes up the second thread (the async logging thread) to read the queue until empty, then it goes back into suspended state. Maybe its my misunderstanding of the code in sleep_or_yield but a blocking queue seems much more CPU efficient rather than sleeping at certain points in time. All that said, you may have done this for maximum speed to avoid mutexes, again I am not totally familiar with this design. If this is of any interest, I have a very nice blockingqueue.h header file that I could supply.
By the way I did see the mpmc_bounded_q.h that you are using. But I don't see any suspends and the code is considerably more complex than using a blocking queue.
@batduder yes, that's the problem with lock free queue - the code cannot simply wait on a mutex when it is empty, so those ugly sleep and yields are used instead of normal blocking.
There were some talks about hibrid solution but it seems even more complex.
Someone should bench the current solution against an aleternatice impementation of spdlog that use a normal blocking queue to see if the perfomance gain really worth the lockfree complexity.
@gabime Got it. I think you can close this thread if you want.
Hey folks! I realize this issue is closed, but I stumbled across this today and just wanted to bump the thread - I'd also like to see an implementation of an async file logger that uses a blocking queue or C++0x's new condition_variable to avoid periodically checking / sleeping for intervals. I'm using spdlog in a command-line application that needs to have the smallest possible battery impact, and the current async log implementation is causing CPU wakes 5-6x a second when no new log messages have arrived which isn't ideal (at least Xcode is unhappy about it on my Mac.)
I'm going to hack together a new async logger based on a mutex and condition_variable—if the performance implications aren't too bad, I'll let you know.
..to avoid periodically checking / sleeping for intervals
Right, thats the downside of non blocking queues. If you could find a way to block on the current non-blocking queue (insead of reverting to traditional blocking queue) - that would be really great (I am not even sure it is possible).
Hey @gabime thanks for the response—I think you're right that it may not be possible.
Here's what I ended up doing for my application: https://gist.github.com/bengotow/82e081fb94a17b905ec85ca9736c9c74. My project doesn't produce a huge volume of logs and I was primarily using spdlog in async mode because it offered the flush_interval_ms feature. I switched off async mode and wrote a custom sink subclass that implements the "flush within X ms" behavior using a helper thread and condition_variable. It probably isn't a good solution for scenarios where the goal is the highest-possible log throughput, but it idles more quietly than async mode.
Also just as a side-note, I really appreciate the formatter and sink interfaces in spdlog - I also needed to log thread names and a custom formatter made that really easy. Good stuff!
Thanks - I am glad you find it useful.