Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 12 Nov 2014 17:24:02 -0500
From:      J David <j.david.lists@gmail.com>
To:        J David <j.david.lists@gmail.com>,  "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>,  "freebsd-questions@freebsd.org" <freebsd-questions@freebsd.org>
Subject:   Re: How thread-friendly is kevent?
Message-ID:  <CABXB=RTm8XhBWE309=3V_tArgtEZkMkFuGvcywxmYTkDhb+u8A@mail.gmail.com>
In-Reply-To: <20141112084909.GV24601@funkthat.com>
References:  <CABXB=RQWxu-d30raZ+FcrnrGsr5gG2Za_=cx8-jCnLSgJDSF=Q@mail.gmail.com> <20141110071353.GO24601@funkthat.com> <CABXB=RStLz6J9L3--KsM308-0h0N5ZeZZvw1GbDi+ZvKO4U64g@mail.gmail.com> <20141112084909.GV24601@funkthat.com>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
On Wed, Nov 12, 2014 at 3:49 AM, John-Mark Gurney <jmg@funkthat.com> wrote:
> This is odd...  I would expect that the event w/o _ONESHOT and _DISPATCH
> to be delivered many times...  Is it possible you have locks in your
> userland side of things that make this less likely?

Nope, the test code is (intentionally) entirely lock-free in userland.

> I have an idea that should only be a few lines of changes that would
> prevent all the threads waking up...  As we lock the kq before doing
> the wakeup, we can change KQ_SLEEP from a flag to a count for how many
> threads are sleeping for an event, and if non-zero, do a wakeup_one...
> Then when kqueue_scan is about to exit, check to see if there are
> still events and threads waiting, and then do another wakeup_one...

This sounds like it could optimize some workloads at substantial
penalties for others.  If pursued, maybe it needs its own flag.

> Currently, KQ_SLEEP is only a flag, so we have to do wakeup to make
> sure everyone wakes up...
>
> Well, if you don't have _ONESHOT and _DISPATCH, any changes I make
> should make it more reliable that all threads get the events dispatched
> to them... :)

Using _DISPATCH is no problem, although a solution that didn't require
two kevent()-calls per event would obviously be better when every
syscall matters.  Albeit that is largely an issue on VM's where the
syscall penalty is artificially large.  In production, this will of
course run on bare metal.

The other option is do wrap kevent() with a mutex on the user side.
That's what Apache does with accept(), IIRC.

> But some of this is making sure you only run enough threads as
> necessary...

That's almost always true.  But, almost always, determining the
correct value of "enough" requires a blood sacrifice. :)

Thanks!



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?CABXB=RTm8XhBWE309=3V_tArgtEZkMkFuGvcywxmYTkDhb+u8A>