Linux/Posix epoll performance deficit
With following code I see an unusal high cpu load (constantly around 5-8%) for a simple server application, which I do not think is acceptable for this kind of application in idle mode.
I experimented with the epoll_wait call and tried passing -1 instead of 0 for the last argument with leads to the expected behaviour of epoll'ling. But then all the other nodepp relevant parts like timers or even information about connection closing do not arrive at the application logic.
Perhaps I am doing something wrong or is this by design?
server code:
#include <nodepp/tcp.h>
#include <nodepp/json.h>
#include <nodepp/timer.h>
#include <apify/apify.h>
using namespace nodepp;
void onMain() {
using socket = socket_t;
auto server = tcp::server();
auto app = apify::add<socket>();
// Logging Middleware executes before any subsequent route logic
auto logger_middleware = [=]( apify_t<socket> cli, function_t<void> next ) {
console::log("[LOG]", cli.method, "\"", cli.path, "\"", " \"", cli.message, "\"");
next();
};
app.add( logger_middleware );
app.on("PINGREQ",nullptr,[=]( apify_t<socket> cli ){
cli.emit( cli.method, cli.path, cli.message );
});
app.on("PINGACK",nullptr,[=]( apify_t<socket> cli ){
cli.done();
});
app.on("AUTH", "/login", [=]( apify_t<socket> cli ) {
console::log("-> Login message received:", cli.message);
cli.emit("SUCCESS", nullptr, "Authentication successful.");
});
app.on("GET", "/user/:id", [=]( apify_t<socket> cli ) {
string_t user_id = cli.params["id"];
console::log("-> Request for user:", user_id);
const object_t ppt({ {"id", user_id} });
cli.emit("USER_DATA", nullptr, json::stringify(ppt));
});
app.on(nullptr, nullptr, [=]( apify_t<socket> cli ) {
console::warning("-> Unmatched Route:", cli.method, cli.path);
cli.emit("ERROR", nullptr, "Route or Method not supported.");
});
server.onConnect([=]( socket cli ){
console::log("connected", cli.get_peername() );
cli.set_timeout(0);
ptr_t<ulong> last_ping = new ulong(process::millis());
cli.onData([=]( string_t data ){
console::log("data: ", data );
*last_ping = process::millis();
app.next(cli, data);
});
cli.onDrain.once([=](){
console::log("closed", cli.get_peername());
});
timer::add( [=]() -> int {
if(cli.is_closed()) {
console::log("client disconnected");
return -1;
}
if((process::millis() - *last_ping) < 5000){
return 1;
}
if((process::millis() - *last_ping) < 10000){
auto app_cli = apify::add( cli );
app_cli.emit("PINGREQ", nullptr, nullptr);
return 1;
}
console::log("client timed out");
cli.close();
return -1;
}, 1000 );
stream::pipe( cli );
});
server.listen("localhost", 8000,[=](socket){
console::log( "tcp://localhost:8000");
});
console::log( "Started" );
}
client code:
#include <nodepp/nodepp.h>
#include <nodepp/tcp.h>
#include <nodepp/timer.h>
#include <apify/apify.h>
using namespace nodepp;
namespace dummy {
// inline tcp_t client( const string_t& uri, uint port, agent_t* opt = nullptr ){
inline tcp_t client( agent_t* opt = nullptr ){
tcp_t skt ( nullptr, opt );
// skt.onSocket.once([=]( socket_t cli ){
// // auto hrv = type::cast<http_t> (cli);
// // if( !generator::ws::client( hrv, uri ) )
// // { skt.onConnect.skip(); return; }
// process::add([=](){
// skt.onConnect.resume( );
// skt.onConnect.emit(cli);
// return -1;
// });
// });
skt.onConnect.once([=]( socket_t cli ){
cli.onDrain.once([=](){ cli.free(); });
cli.set_timeout(0);
cli.resume();
stream::pipe(cli);
});
// skt.connect( uri, port );
// skt.connect( url::hostname(uri), url::port(uri) );
return skt;
}
}
void onMain() {
using socket = socket_t;
auto client = dummy::client();
auto app = apify::add<socket>();
// Logging Middleware executes before any subsequent route logic
auto logger_middleware = [=]( apify_t<socket> cli, function_t<void> next ) {
console::log("[LOG]", cli.method, "\"", cli.path, "\"", " \"", cli.message, "\"");
next();
};
app.add( logger_middleware );
app.on("PINGREQ",nullptr,[=]( apify_t<socket> cli ){
cli.emit( "PINGACK", nullptr, nullptr );
});
app.on("SUCCESS", nullptr, [=]( apify_t<socket> res ) {
console::done("Login Successful:", res.message );
res.done();
});
app.on("USER_DATA", nullptr, [=]( apify_t<socket> res ) {
console::done("user data:", res.message );
res.done();
});
app.on("ERROR", nullptr, [=]( apify_t<socket> res ) {
console::error("Error:", res.message );
res.done();
});
app.on(nullptr, nullptr, [=]( apify_t<socket> cli ) {
console::warning("-> Unmatched Route:", cli.method, cli.path );
});
client.onConnect([=]( socket cli ){
cli.onData([=]( string_t data ){
// console::log("data: ", data );
app.next( cli, data );
});
cli.onDrain.once([=](){
console::log("closed", cli.get_peername());
});
auto app_cli = apify::add( cli );
app_cli.emit("AUTH", "/login", "user:pass");
app_cli.emit("GET", "/user/16", "fetch");
timer::add( [=]() -> int {
console::log("client forced shutdown");
cli.close();
return -1;
}, 100 );
});
client.connect( "localhost", 8000, []( socket_t cli ){
console::log("-> tcp://localhost:8000");
});
console::log( "Started" );
}
strace ./server (added process::delay(500) before if((obj->len=epoll_wait( obj->pd, &obj->ev, obj->ev.size(), 0 ))<=0 ) in posix/epoll.h for better readability)
write(1, "Started \n\33[0m", 13Started
) = 13
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) = 4
setsockopt(4, SOL_SOCKET, SO_SNDBUF, [65536], 4) = 0
setsockopt(4, SOL_SOCKET, SO_RCVBUF, [65536], 4) = 0
fcntl(4, F_GETFL) = 0x2 (flags O_RDWR)
fcntl(4, F_SETFL, O_RDWR|O_NONBLOCK) = 0
setsockopt(4, SOL_IPV6, IPV6_V6ONLY, [0], 4) = -1 ENOPROTOOPT (Protocol not available)
setsockopt(4, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
setsockopt(4, SOL_SOCKET, SO_REUSEPORT, [1], 4) = 0
setsockopt(4, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
setsockopt(4, SOL_SOCKET, SO_SNDBUF, [65536], 4) = 0
setsockopt(4, SOL_SOCKET, SO_RCVBUF, [65536], 4) = 0
brk(0x56314f625000) = 0x56314f625000
setsockopt(4, SOL_SOCKET, SO_REUSEPORT, [1], 4) = 0
setsockopt(4, SOL_SOCKET, SO_KEEPALIVE, [0], 4) = 0
setsockopt(4, SOL_SOCKET, SO_BROADCAST, [0], 4) = 0
bind(4, {sa_family=AF_INET, sin_port=htons(8000), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
prlimit64(0, RLIMIT_NOFILE, NULL, {rlim_cur=1024, rlim_max=512*1024}) = 0
listen(4, 1024) = 0
write(1, "tcp://localhost:8000 \n\33[0m", 26tcp://localhost:8000
) = 26
epoll_ctl(3, EPOLL_CTL_ADD, 4, {events=EPOLLIN, data=0x56314f5e3c80}) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [{events=EPOLLIN, data=0x56314f5e3c80}], 1024, 0) = 1
accept(4, {sa_family=AF_INET, sin_port=htons(36212), sin_addr=inet_addr("127.0.0.1")}, [16]) = 5
fcntl(5, F_GETFL) = 0x2 (flags O_RDWR)
fcntl(5, F_SETFL, O_RDWR|O_NONBLOCK) = 0
setsockopt(5, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
setsockopt(5, SOL_SOCKET, SO_SNDBUF, [65536], 4) = 0
setsockopt(5, SOL_SOCKET, SO_RCVBUF, [65536], 4) = 0
setsockopt(5, SOL_SOCKET, SO_REUSEPORT, [1], 4) = 0
setsockopt(5, SOL_SOCKET, SO_KEEPALIVE, [0], 4) = 0
setsockopt(5, SOL_SOCKET, SO_BROADCAST, [0], 4) = 0
getpeername(5, 0x7fff79a1f468, [0 => 16]) = 0
write(1, "connected 0.0.0.0\0\0\0\0\0\0\0\0 \n\33[0m", 31connected 0.0.0.0
) = 31
recvfrom(5, "QVVUSA==.L2xvZ2lu.dXNlcjpwYXNz.R"..., 65536, 0, NULL, NULL) = 58
write(1, "data: QVVUSA==.L2xvZ2lu.dXNlcjp"..., 71data: QVVUSA==.L2xvZ2lu.dXNlcjpwYXNz.R0VU.L3VzZXIvMTY=.ZmV0Y2g=.
) = 71
write(1, "[LOG] AUTH \" /login \" \" user:p"..., 43[LOG] AUTH " /login " " user:pass "
) = 43
write(1, "-> Login message received: user:"..., 42-> Login message received: user:pass
) = 42
sendto(5, "U1VDQ0VTUw==..QXV0aGVudGljYXRpb2"..., 51, 0, NULL, 0) = 51
write(1, "[LOG] GET \" /user/16 \" \" fetch"..., 40[LOG] GET " /user/16 " " fetch "
) = 40
write(1, "-> Request for user: 16 \n\33[0m", 29-> Request for user: 16
) = 29
sendto(5, "VVNFUl9EQVRB..eyJpZCI6IjE2In0=.", 31, 0, NULL, 0) = 31
recvfrom(5, 0x56314f6141f0, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
accept(4, 0x56314f5e4098, [16]) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x56314f6141f0, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x56314f6141f0, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
recvfrom(5, 0x56314f6141f0, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
recvfrom(5, "", 65536, 0, NULL, NULL) = 0
getpeername(5, {sa_family=AF_INET, sin_port=htons(36212), sin_addr=inet_addr("127.0.0.1")}, [16]) = 0
write(1, "closed 127.0.0.1\0\0\0\0\0\0 \n\33[0m", 28closed 127.0.0.1
) = 28
shutdown(5, SHUT_RDWR) = 0
close(5) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
write(1, "client disconnected \n\33[0m", 25client disconnected
) = 25
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}^Cstrace: Process 541056 detached
<detached ...>
SIGINT: Signal Interrupt
strace ./client
[...]
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(4, 0x565142fa4270, 65536, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
write(1, "client forced shutdown \n\33[0m", 28client forced shutdown
) = 28
shutdown(4, SHUT_RDWR) = 0
close(4) = 0
getpeername(4, 0x7ffd57ef2248, [16]) = -1 EBADF (Bad file descriptor)
write(1, "closed 127.0.0.1 \n\33[0m", 22closed 127.0.0.1
) = 22
brk(0x565142fc5000) = 0x565142fc5000
close(3) = 0
exit_group(0) = ?
+++ exited with 0 +++
your code looks fine, you are not using any internal loops that would typically block the event-loop. The CPU consumption you're seeing is not caused by epoll (poll.h), but by the main task scheduler within the (nodepp.h) framework.
The crucial function is process::next(), which is the core of Nodepp's event-loop. In current versions, it is designed to conditionally call yield() every 64 ticks:
EDIT: I've made this to ""make my server run faster"", but then I realized that by using clustering and -O3 solves the problem.
inline int next(){
static uchar count=0; if( count%64==0 ){ yield(); } count++; // <--- yield() is only called every 64 tasks
coStart
if( !_poll_.empty() ) { _poll_.next(); coNext; }
if( !_loop_.empty() ) { _loop_.next(); coNext; }
if( !_foop_.empty() ) { _foop_.next(); coNext; }
coStop }
so, If you change the implementation of process::next() to call yield() on every tick of the event-loop, the CPU consumption should drop drastically to expected levels:
inline int next(){
// static uchar count=0; if( count%64==0 ){ yield(); } count++ ;
yield() ; // <--- yield() is called on every tick
coStart
if( !_poll_.empty() ) { _poll_.next(); coNext; }
if( !_loop_.empty() ) { _loop_.next(); coNext; }
if( !_foop_.empty() ) { _foop_.next(); coNext; }
coStop }
I recommend you test your application with this modified configuration.
If this change resolves your high CPU issue, please let me know so I can commit these changes to the Nodepp codebase.
The mentioned change indeed improved the cpu load a bit. Now I get a load of 2%. But I do not think that this is what a server application - doing nothing productive - should have. Other frameworks I've used in the past e.g. libuv while "doing nothing" have an absolute load of 0% while polling/waiting for incoming events or waiting for timers to fire.
The main problem in my eyes is that evpoll is not used as it should, blocking until something to be handled arrives.
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=1000000}, NULL) = 0
epoll_wait(3, [], 1024, 0) = 0
evpoll_wait actually should wait for an event been triggered, data incoming through fs or socket or a timer expired. So this function should be used with -1 timeout instead of 0 in my humble opinion, not keeping the processor busy doing nothing instead of polling until something shall be done.
I think libuv has implemented this in a way more complicated of using - especially in c++ - but at the current state it handles the resources provided in a more performant way.
I'll have a closer look in how this is actually implemented in nodepp, perhaps there is an opportunity for a more robust way of handling those signals and the system resources at the same time.
EDIT: https://linux.die.net/man/2/timerfd_create looks promising. All nodes stored in loop could be triggered accordingly. Timers after there timeout, other functions right away for direct execution. But I do not have any clue how this can be done for the other platforms nodepp can be used with...
🤔 What you are asking for is possible, but Nodepp wouldn't be single-threaded anymore (which is what the framework promises to be ).
I have actually been thinking about this for some time ago, and the only way to use a blocking call like epoll_wait(..., -1) without freezing the entire event-loop is by creating an exclusive worker thread for epoll (or kqueue on BSD/macOS) and allowing it to block indefinitely.
Since I recently added support for atomic variables, I'm no longer worried about data races in this type of scenario.
Therefore, I was considering creating a thread pool and spinning up the sub-loops ( file-loop, poll-loop, task-loop ). So only when it detects available tasks, I would create a worker thread, and remove it when all tasks are done. In this way, I believe I could reduce CPU consumption to 0% ( theoretically, but I've never tried this kind of approach before ).
Now, about timers, I have done some reading, and it seems that on Windows, it is possible to handle them using CreateTimerQueueTimer. I also need to implement IOCP. In previous versions of Nodepp, I implemented WSApoll, but it was ineffective; So I had implemented nopoll.h as a temporal solution, but I have to work on implementing IOCP.
So, the dilemma is this: what you are asking for is possible and would achieve the desired 0% CPU load, but it would compromise Nodepp's promise of being single-threaded.
But don't worry, promises were made to be broken? or were laws? anyway, I will work on this.
🤔 What you are asking for is possible, but Nodepp wouldn't be single-threaded anymore (which is what the framework promises to be ).
I have actually been thinking about this for some time ago, and the only way to use a blocking call like
epoll_wait(..., -1)without freezing the entire event-loop is by creating an exclusive worker thread for epoll (or kqueue on BSD/macOS) and allowing it to block indefinitely.
In my opinion it would be possible - while still being single threaded - "abusing" the timeout argument therefore. So the system is kinda blocking for a certain time while still listening for incoming events. Here is how libuv's implementation works: https://docs.libuv.org/en/v1.x/design.html
Poll timeout is calculated. Before blocking for I/O the loop calculates for how long it should block. These are the rules when calculating the timeout:
* If the loop was run with the UV_RUN_NOWAIT flag, the timeout is 0.
* If the loop is going to be stopped ([uv_stop()](https://docs.libuv.org/en/v1.x/loop.html#c.uv_stop) was called), the timeout is 0.
* If there are no active handles or requests, the timeout is 0.
* If there are any idle handles active, the timeout is 0.
* If there are any handles pending to be closed, the timeout is 0.
* If none of the above cases matches, the timeout of the closest timer is taken, or if there are no active timers, infinity.
So instead of the current call to yield() and wait for a predefined (small) amount of time the kernel shall "yield" for a dynamic amount of time. All tasks (loop, foop and poll stuff) are already registered within the process::next framework. There is just a simple value missing, the estimated time this task will have nothing to do. Timers have an quite exact estamitation (dynamic timeout), io processes shall rely on evpoll and consorts (-1 timeout). Other tasks can fire directly, while evpoll waits with 0 timeout as it is already been done in the past. In the meantime cpu is completly idle, all resources are well managed and the performance shall not be negatively affected at all.
Since I recently added support for atomic variables, I'm no longer worried about data races in this type of scenario.
Therefore, I was considering creating a thread pool and spinning up the sub-loops (
file-loop,poll-loop,task-loop). So only when it detects available tasks, I would create a worker thread, and remove it when all tasks are done. In this way, I believe I could reduce CPU consumption to 0% ( theoretically, but I've never tried this kind of approach before ).
As already mentioned forcing multiple threads by design is not the way this project should go. Especially considering the support of mcus like arduino or esp32 which (mostly) do not support threads at all.
Now, about timers, I have done some reading, and it seems that on Windows, it is possible to handle them using
CreateTimerQueueTimer. I also need to implementIOCP. In previous versions of Nodepp, I implementedWSApoll, but it was ineffective; So I had implementednopoll.has a temporal solution, but I have to work on implementingIOCP.
IOCP as an well-established alternative to the current implementation sounds great. 😃
So, the dilemma is this: what you are asking for is possible and would achieve the desired 0% CPU load, but it would compromise Nodepp's promise of being single-threaded.
But don't worry, promises were made to be broken? or were laws? anyway, I will work on this.
😄
Just as a comparison with the sigcalls of a simple tcp server implemented using libuv
/// clang++ -o main main.cpp -luv && ./main
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <uv.h>
uv_loop_t *loop;
struct sockaddr_in addr;
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
buf->base = (char*)malloc(suggested_size);
buf->len = suggested_size;
}
void echo_write(uv_write_t *req, int status) {
if (status) {
fprintf(stderr, "Write error %s\n", uv_strerror(status));
}
free(req);
}
void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
if (nread < 0) {
if (nread != UV_EOF) {
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
uv_close((uv_handle_t*) client, NULL);
}
} else if (nread > 0) {
printf("data %s", buf->base);
uv_write_t *req = (uv_write_t *) malloc(sizeof(uv_write_t));
uv_buf_t wrbuf = uv_buf_init(buf->base, nread);
uv_write(req, client, &wrbuf, 1, echo_write);
}
if (buf->base) {
free(buf->base);
}
}
void on_new_connection(uv_stream_t *server, int status) {
if (status < 0) {
fprintf(stderr, "New connection error %s\n", uv_strerror(status));
return;
}
uv_tcp_t *client = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
uv_tcp_init(loop, client);
if (uv_accept(server, (uv_stream_t*) client) == 0) {
uv_read_start((uv_stream_t*)client, alloc_buffer, echo_read);
} else {
uv_close((uv_handle_t*) client, NULL);
}
}
int main() {
loop = uv_default_loop();
uv_tcp_t server;
uv_tcp_init(loop, &server);
uv_ip4_addr("0.0.0.0", 7000, &addr);
uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0);
int r = uv_listen((uv_stream_t*)&server, 128, on_new_connection);
if (r) {
fprintf(stderr, "Listen error %s\n", uv_strerror(r));
return 1;
}
uv_timer_t timer_req;
uv_timer_init(loop, &timer_req);
uv_timer_start(&timer_req, [](uv_timer_t* handle) {
fprintf(stdout, "timer\n");
}, 1000, 5000);
return uv_run(loop, UV_RUN_DEFAULT);
}
mmap(NULL, 8256, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_POPULATE, 4, 0) = 0x7f09cf8bb000
mmap(NULL, 16384, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_POPULATE, 4, 0x10000000) = 0x7f09cf8b7000
pipe2([5, 6], O_CLOEXEC) = 0
write(6, "*", 1) = 1
futex(0x7f09cf874670, FUTEX_WAKE_PRIVATE, 2147483647) = 0
pipe2([7, 8], O_NONBLOCK|O_CLOEXEC) = 0
eventfd2(0, EFD_CLOEXEC|EFD_NONBLOCK) = 9
openat(AT_FDCWD, "/dev/null", O_RDONLY|O_CLOEXEC) = 10
socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 11
setsockopt(11, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
bind(11, {sa_family=AF_INET, sin_port=htons(7000), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
listen(11, 128) = 0
io_uring_enter(4, 3, 3, IORING_ENTER_GETEVENTS, NULL, 0) = 3
epoll_pwait(3, [], 1024, 997, NULL, 8) = 0
fstat(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(0x88, 0x11), ...}) = 0
write(1, "timer\n", 6timer
) = 6
epoll_pwait(3, [{events=EPOLLIN, data=0xb}], 1024, 5000, NULL, 8) = 1
accept4(11, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK) = 12
io_uring_enter(4, 1, 1, IORING_ENTER_GETEVENTS, NULL, 0) = 1
epoll_pwait(3, [{events=EPOLLIN, data=0xc}], 1024, 4175, NULL, 8) = 1
read(12, "test\n", 65536) = 5
write(1, "data test\n", 10data test
) = 10
write(12, "test\n", 5) = 5
io_uring_enter(4, 1, 1, IORING_ENTER_GETEVENTS, NULL, 0) = 1
epoll_pwait(3, [{events=EPOLLIN, data=0xc}], 1024, 4174, NULL, 8) = 1
read(12, "", 65536) = 0
epoll_pwait(3, [{events=EPOLLIN, data=0xc}], 1024, 3173, NULL, 8) = 1
epoll_ctl(3, EPOLL_CTL_DEL, 12, 0x7ffc81090df0) = 0
epoll_pwait(3, [], 1024, 3172, NULL, 8) = 0
write(1, "timer\n", 6timer
) = 6
epoll_pwait(3^Cstrace: Process 568567 detached
<detached ...>
Dude you were right, I have never thought I could hit 10k request too faster; The problem was definitely the manual yield() function, I mean the first process::next() was too CPU heavy and the second was too slow. This was the biggest problem I was facing for long time and I used to think this was a core limitation of Nodepp's architecture, but I was wrong.
I can't believe Moving the sleep logic directly into epoll_wait allows the Kernel to act as scheduler. It drastically drops idle CPU usage to almost 0%~0.4% and improves throughput massively.
with yield - heavy CPU Usage Version & using -O3 flag - 10K requests:
Concurrency Level: 1000
Time taken for tests: 9.664 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 690000 bytes
HTML transferred: 250000 bytes
Requests per second: 1034.79 [#/sec] (mean)
Time per request: 966.380 [ms] (mean)
Time per request: 0.966 [ms] (mean, across all concurrent requests)
Transfer rate: 69.73 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 17.3 0 88
Processing: 28 908 170.8 954 1031
Waiting: 16 907 170.9 954 1031
Total: 103 914 154.6 955 1031
with yield() - lazy timeout version & using -O3 flag - 10k requests:
It is taking to long, and is too late right now.
with epoll_wait & using -O3 flag - 10k requests:
Concurrency Level: 1000
Time taken for tests: 2.152 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 690000 bytes
HTML transferred: 250000 bytes
Requests per second: 4647.00 [#/sec] (mean)
Time per request: 215.192 [ms] (mean)
Time per request: 0.215 [ms] (mean, across all concurrent requests)
Transfer rate: 313.13 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 28 26.8 21 136
Processing: 30 170 61.8 170 368
Waiting: 26 162 58.1 162 338
Total: 108 198 47.7 192 369
this is what I've done:
// nodepp.h
inline int next(){
static long x=0; _poll_.next(); coStart do { /*--*/ coNext; // <- let epoll_wait yield main thread
x=_loop_.size(); while( x-->0 ){ _loop_.next(); coNext; }
x=_foop_.size(); while( x-->0 ){ _foop_.next(); coNext; }
} while( !process::should_close() ) ; coStop }
// epoll.h - small timer penalization system
do { obj->stamp = process::now(); /*------*/
obj->dif = process::now()-obj->stamp;
obj->stamp = process::now(); /*------*/
if ( obj->dif<10 ) { obj->dif=10-obj->dif; }
else /*---------*/ { obj->dif=TIMEOUT; }} while(0);
if((obj->len=epoll_wait( obj->pd, &obj->ev, obj->ev.size(), obj->dif ))<=0 )
{ coEnd; } obj->y=0; coNext;
// [...]
Thanks for the wake-up call, dude!. I will commit this changes soon.
Resolution: Implementing Deterministic Polling for 0% Idle CPU
dude, it wasn't easy 3 week working hard ( More or less - even during an air raid - literally ) to make nodepp truly consumes 0% CPU. First, I want to thank you for your patience and for pointing out this behavior. Your observation was correct: the framework was prioritizing throughput over energy efficiency, leading to the "spinning" behavior you noticed.
I have just pushed Nodepp V1.4.0, which resolves this by implementing a Calculated Blocking Strategy within the kernel_t ( aka poll.h 2.0 ) reactor.
How it works:
We have moved away from the busy-wait loop. Nodepp now dynamically calculates the epoll_wait timeout based on the next scheduled event, exactly how libuv do as well:
- If tasks are pending: The timeout is 0 ( immediate return ) to maintain maximum throughput except on windows which IOCP requires a small timeout to work correctly.
- If timers are scheduled: The timeout is exactly the duration until the next timer fires.
- If the system is idle: The timeout becomes -1 (infinite block), allowing the OS to put the thread to sleep.
Addressing the "Single-Threaded" Concern:
You’ll be happy to know we did not break the law. Nodepp remains 100% Single-Threaded even we added share-nothing architecture for distributed systems. We achieved 0% CPU load not by adding background threads, but by optimizing the Silicon-Logic Parity of our main reactor. By teaching the event loop how to "sleep" intelligently, we now match the efficiency of libuv while maintaining the zero-dependency, flat memory profile that defines Nodepp.
The Result:
- Idle CPU Load: 0.0% ( Verified via top and tasks managers on windows ).
- Responsiveness: Sub-millisecond (The OS wakes the process immediately on I/O interrupts).
- Sustainability: This update marks a major milestone in our "Green Computing" initiative, reducing the energy footprint of idle instances to near zero.
I am closing this issue as Resolved. Thank you for helping us make Nodepp the most efficient runtime for the Post-Moore era.