Files
poco/Foundation/include/Poco/AsyncChannel.h
Aleksandar Fabijanic 1850dc16aa Benchmark and FastLogger (#5081)
* fix(SharedLibrary): Missing DLLs not reported #5069

* fix(CMake): not producing proper binary names #5070

* fix(SharedLibrary): disable shared lib tests in static build #5069

* fix(misc): add pdjson links to gitignore, remove unused var in SharedLibrary, harden TaskManagerTest

* fic(ci): separate oracle and sqlserver odbc (out of disk space) (#5075)

* fic(ci): separate oracle and sqlserver odbc (out of disk space)

* use oracle odbc driver

* use oracle free

* ad db user

* postpone adding user after build

* remove default tablespace (does not exist)

* reinstate all ci jobs

* add postgresl odb tests to ci

* remove spurious syminks

* fix gitignore (pdjson)

* Remove VS projects #5076

* chore: revert leftover ODB IP address

* fix(CodeQL): float comparison alerts

* fix: compile errors

* chore: upgrade asan to macos-14 (tryout)

* fix: .gitignore symlinks; XML Makefile wrong pattern

* Optimize PatternFormatter and Timezone performance #5078

PatternFormatter:
- Cache node name (Environment::nodeName()) to avoid repeated syscalls
- Add extractBasename() for efficient %O format specifier
- Add string reserve(128) to reduce reallocations during formatting

Timezone:
- Cache UTC offset to avoid repeated syscalls (8x speedup for %L patterns)
- Auto-detect TZ environment variable changes to invalidate cache
- Add reloadCache() method for explicit cache refresh

Tests:
- Add TimezoneTest::testUtcOffsetCaching()
- Add PatternFormatterTest::testExtractBasename()

* fix: use Path::separatorin extractBasename #5078

* Add Benchmark #5080

* enh(Logging): move constructors for Message and Logger #5078

* chore(AsyncNotificationCenter): eliminate MSVC warnings

* enh(build): c++20 support #5084

* feat(CppUnit): print class name
execute all named tests (not only the first one)
accept test name with class (eg. testrunner LoggerTest::testLogger) #5083

* feat(Benchmark): Add Logger/FastLogger comparison benchmarks and Windows support

- Add LoggerBench.cpp with AsyncChannel vs FastLogger benchmarks
- Add compare.sh (Linux/macOS) and compare.ps1 (Windows) scripts
- Add LOGGER_BENCHMARK.md with cross-platform benchmark results
- Update README.md with Windows build instructions (Ninja, CMAKE_PREFIX_PATH)
- Add error message when -- options are used on Windows (should use /)
- Update CMakeLists.txt and Makefile to include LoggerBench #5080

* feat(FastLogger): #5078
FastLogger provides a Poco-compatible wrapper around the Quill logging
library, offering significant performance improvements over AsyncChannel
through lock-free SPSC queues and backend thread processing.

Key features:
- Drop-in replacement for Poco::Logger with FastLogger::get()
- Support for all standard Poco channels (Console, File, Rotating, etc.)
- XML/properties configuration via FastLoggerConfigurator
- Thread affinity for backend worker on Linux and Windows
- Log file rotation with size and time-based policies

Performance (CPU time - calling thread latency):
- Linux: 31-70x faster than AsyncChannel
- Windows: 23-87x faster than AsyncChannel
- macOS: Limited improvement due to lack of thread affinity support

New files:
- Foundation/include/Poco/FastLogger.h
- Foundation/src/FastLogger.cpp
- Util/include/Poco/Util/FastLoggerConfigurator.h
- Util/src/FastLoggerConfigurator.cpp
- dependencies/quill/ (header-only Quill 7.5.0 library)

* fix(cmake): disable FastLogger on emscripten (not supported) #5078

* feat(FastLogger): add cpuAfinity config parameter #5087

* fix(FastLogger): Fix lock-order-inversion in FastLogger (TSAN) #5078

* fix(cmake): build not stripping release binaries #5085

* fix(PCRE): fails to compile with clang/c++20 #5131

* feat(AsyncChannel): add CPU affinity property #5087

* feat(SpinlockMutex): make it adaptive #5132

* feat(AsyncChannel): add CPU affinity property #5087

* chore: remove leftover file commited by mistake

* feat(build): allow FastLogger to be fully disabled at build time #5078

Build system changes:
- Add POCO_NO_FASTLOGGER compile definition in CMake when ENABLE_FASTLOGGER=OFF
  to prevent Config.h from auto-enabling FastLogger
- Add ifdef guards around FastLogger tests in LoggingTestSuite.cpp
- Exclude FastLoggerTest.cpp and FastLoggerChannelsTest.cpp from CMake build
  when FastLogger is disabled
- Add POCO_NO_FASTLOGGER support to Make build system for Foundation and Util
- Add CI jobs to verify builds work without FastLogger (CMake and Make)

Code changes:
- Add LoggingConfigurator::configure() convenience method for quick logging setup

* fix(ci): testrunner args

* chore(progen): remove leftover script #5076

* fix(test): give ANC a bit more time to process

* fix(ci): set env before test run

* chore(doc): quill license

* feat(Channel): add log(Message&&) #5133

* fix(ci): set env before test run

* fix(TestRunner): don't search children #5083

* feat: lock-free queues #5134

* feat(Benchmark): various comparisons

* chore: cleanup benchmark
2025-12-22 21:06:43 +01:00

138 lines
3.8 KiB
C++

//
// AsyncChannel.h
//
// Library: Foundation
// Package: Logging
// Module: AsyncChannel
//
// Definition of the AsyncChannel class.
//
// Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Foundation_AsyncChannel_INCLUDED
#define Foundation_AsyncChannel_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Channel.h"
#include "Poco/Thread.h"
#include "Poco/Mutex.h"
#include "Poco/Runnable.h"
#include "Poco/AutoPtr.h"
#include "Poco/NotificationQueue.h"
#include <atomic>
namespace Poco {
class Foundation_API AsyncChannel: public Channel, public Runnable
/// A channel uses a separate thread for logging.
///
/// Using this channel can help to improve the performance of
/// applications that produce huge amounts of log messages or
/// that write log messages to multiple channels simultaneously.
///
/// All log messages are put into a queue and this queue is
/// then processed by a separate thread.
{
public:
using Ptr = AutoPtr<AsyncChannel>;
AsyncChannel(Channel::Ptr pChannel = nullptr, Thread::Priority prio = Thread::PRIO_NORMAL);
/// Creates the AsyncChannel and connects it to
/// the given channel.
void setChannel(Channel::Ptr pChannel);
/// Connects the AsyncChannel to the given target channel.
/// All messages will be forwarded to this channel.
Channel::Ptr getChannel() const;
/// Returns the target channel.
void open() override;
/// Opens the channel and creates the
/// background logging thread.
void close() override;
/// Closes the channel and stops the background
/// logging thread.
void log(const Message& msg) override;
/// Queues the message for processing by the
/// background thread.
void log(Message&& msg) override;
/// Queues the message for processing by the
/// background thread, moving the message to avoid copying.
void setProperty(const std::string& name, const std::string& value) override;
/// Sets or changes a configuration property.
///
/// The "channel" property allows setting the target
/// channel via the LoggingRegistry.
/// The "channel" property is set-only.
///
/// The "priority" property allows setting the thread
/// priority. The following values are supported:
/// * lowest
/// * low
/// * normal (default)
/// * high
/// * highest
///
/// The "priority" property is set-only.
///
/// The "queueSize" property allows to limit the number
/// of messages in the queue. If the queue is full and
/// new messages are logged, these are dropped until the
/// queue has free capacity again. The number of dropped
/// messages is recorded, and a log message indicating the
/// number of dropped messages will be generated when the
/// queue has free capacity again.
/// In addition to an unsigned integer specifying the size,
/// this property can have the values "none" or "unlimited",
/// which disable the queue size limit. A size of 0 also
/// removes the limit.
///
/// The "queueSize" property is set-only.
///
/// The "enableCpuAffinity" property pins the background
/// logging thread to a specific CPU core (the last core by
/// default). This can reduce latency variance by avoiding
/// thread migration. Values: "true" or "false" (default).
/// Only supported on Linux and Windows.
///
/// The "enableCpuAffinity" property is set-only.
protected:
~AsyncChannel() override;
void run() override;
void setPriority(const std::string& value);
private:
template <typename M>
void logImpl(M&& msg);
Channel::Ptr _pChannel;
Thread _thread;
FastMutex _threadMutex;
FastMutex _channelMutex;
NotificationQueue _queue;
std::size_t _queueSize = 0;
std::size_t _dropCount = 0;
std::atomic<bool> _closed;
bool _enableCpuAffinity = false;
};
} // namespace Poco
#endif // Foundation_AsyncChannel_INCLUDED