[zeromq-dev] High CPU load on polling timeout

Justin Azoff justin.azoff at gmail.com
Wed Nov 8 20:31:37 CET 2017


It's not really the polling that is causing this.... simplifying the
program to this:

#include <zmq.hpp>
#include <memory>
#include <iostream>
#include <unistd.h>

int main () {
    zmq::context_t context {1};
    std::unique_ptr<zmq::socket_t> socket {new zmq::socket_t{context, ZMQ_REQ}};
    socket->connect("tcp://localhost:33171");

    while (true) {
        socket->send(zmq::message_t {"test", 4});
        usleep(5000000);

        std::cout << "Timeout!\n";
        socket.reset(new zmq::socket_t{context, ZMQ_REQ});
        socket->connect("tcp://localhost:33171");
    }

    return 0;
}

and running

sudo tcpdump -n -i lo dst port 33171 | pv -l > /dev/null


shows initially 24 packets/sec, but after a few timeouts, it grows
linearly to 50,75,100,125 packets/sec

with the timeout of 500ms instead of 5s. it reaches 1000 packets/sec
pretty quickly.

So, why does it do this?  It's because zmq tries to finish sending
what you told it to.  In your case, there is a simple fix: disable
linger.

Simply add a call to

    int linger = 0;
    socket->setsockopt (ZMQ_LINGER, &linger, sizeof (linger));

before destroying the socket, and your problem will go away.

On Wed, Nov 8, 2017 at 1:59 PM, Luca Boccassi <luca.boccassi at gmail.com> wrote:
> On Wed, 2017-11-08 at 19:59 +0200, Mykola Ostrovskyy via zeromq-dev
> wrote:
>> 2017-11-08 15:54 GMT+02:00 Luca Boccassi <luca.boccassi at gmail.com>:
>>
>> >
>> > Heartbeats are now included in the protocol, check the various
>> > ZMQ_HEARTBEAT_* socket options
>> >
>> > --
>> > Kind regards,
>> > Luca Boccassi
>> >
>> >
>>
>> Luca,
>>
>> Thanks for the quick reply.
>>
>> From the first look ZMQ_HEARTBEAT_* stuff is quite different from
>> what I
>> have now. I will investigate more, but it looks like I would need
>> substantial redesign to switch to that.
>>
>> Is there a way to get the pattern described in the guide to work? Or
>> is it
>> explicitly not supposed to work anymore?
>>
>> After all, heart beat implementation is just one use case.
>> Hypothetically,
>> there can be different scenarios where you try to send some data to
>> REP
>> socket, time out waiting for reply, and eventually retrying to send
>> it
>> again.
>>
>>
>> Regards,
>> Mykola
>
> It can work - but don't continuously create and delete sockets, that's
> a known anti-pattern. They are all async operation that happen in the
> background. With such a small poll timeout, the connection might not
> even have been attempted when you destroy the socket.
>
> --
> Kind regards,
> Luca Boccassi
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> https://lists.zeromq.org/mailman/listinfo/zeromq-dev
>



-- 
- Justin



More information about the zeromq-dev mailing list