caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Török Edwin" <edwin+ml-ocaml@etorok.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Thread behaviour
Date: Mon, 30 Sep 2013 19:01:14 +0300	[thread overview]
Message-ID: <5249A04A.4010800@etorok.net> (raw)
In-Reply-To: <CABooLwOstn7yCXwGr_6n62tKN5ZPDoLbr-7=pSRMdmUQptuYYA@mail.gmail.com>

On 09/30/2013 06:12 PM, Tom Ridge wrote:
> On 30 September 2013 10:18, Xavier Leroy <Xavier.Leroy@inria.fr> wrote:
>> On 2013-09-27 12:10, Tom Ridge wrote:
>>> I have a little program which creates a thread, and then sits in a loop:
>>> [...]
>>> When I run the program I get the output:
>>>
>>> 1
>>> 2
>>>
>>> and the program then sits in the loop.
>>
>> On my machine (OCaml 4.01.0, Ubuntu 12.04 LTS), I sometimes see what
>> you see, and sometimes I see the expected output:
>>
>> 1
>> 2
>> 3
>> hello
>> 4
>>
>> It all depends on the whim of the OS scheduler.  OCaml has no control
>> over it.  And you shoudn't expect any kind of fairness from the OS
>> scheduler, esp. Linux's, which gladly jettisons any pretense of
>> fairness in the hope of getting better throughput.
>>
> 
> Ah! You are saying that the problem (maybe) lies with the Linux scheduler.
> This had never occurred to me. Probably because I assumed the Linux
> phrase "completely fair scheduler" meant something (although
> admittedly, I never tried to find out what).

I think there are two things to consider here:
 * deciding which process(es) to run on the CPU(s), when there are multiple runnable processes/threads (i.e. they are not blocked/sleeping). This is called CFS in Linux, and you can check how well it behaves with top %CPU, /proc/sched_debug, etc.
Although it has some heuristics too, for example related to pipe(2) handling.

 * deciding how to lock a mutex / what thread to wake up when a mutex is released/ condvar is signaled

This is handled first in libpthread (IIRC there is some limited user-space spinning involved), then handed off to the kernel via futex(7).
Depending how its implemented there can be fairness issues, see also 'thundering herd' problem, etc.
AFAIK the kernel uses ticket locks for its spinlocks to get more fairness since quite some time (see http://lwn.net/Articles/267968/ and http://lwn.net/Articles/531254/), but I lost track whether futexes actually use this mechanism or something else. If there is something wrong with the fairness in thread scheduling I'd expect it to be in this part though.

As far as OCaml is concerned one might try to implement something similar to ticket locks in userspace
(or have an array of condition variables and wake up directly the thread we want, and track their execution times?), but it won't be as efficient as if the kernel was (fixed) to do it properly.

Best regards,
--Edwin

  reply	other threads:[~2013-09-30 16:01 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-27 10:10 Tom Ridge
2013-09-27 10:22 ` Simon Cruanes
2013-09-27 10:27 ` Romain Bardou
2013-09-27 10:51   ` Benedikt Grundmann
2013-09-28 19:09     ` Tom Ridge
2013-09-29  7:54       ` Tom Ridge
2013-09-29 12:37         ` Yaron Minsky
2013-09-29 16:25           ` Tom Ridge
2013-09-29 16:46             ` Chet Murthy
2013-09-29 17:18               ` Tom Ridge
2013-09-29 17:47                 ` Chet Murthy
2013-09-30  8:24                   ` Romain Bardou
2013-10-07 14:57                     ` Goswin von Brederlow
2013-09-30  8:16       ` Romain Bardou
2013-10-01  3:32         ` Ivan Gotovchits
2013-10-07 14:49       ` Goswin von Brederlow
2013-09-30  9:18 ` Xavier Leroy
2013-09-30 15:12   ` Tom Ridge
2013-09-30 16:01     ` Török Edwin [this message]
2013-09-30 16:56     ` Gabriel Kerneis
2013-09-30 18:18       ` Alain Frisch
2013-10-01  5:01   ` Pierre Chambart
2013-10-01  7:21     ` Gabriel Kerneis
2013-10-02 10:37     ` Wojciech Meyer
2013-10-02 11:52       ` Francois Berenger
2013-10-02 11:58         ` Wojciech Meyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5249A04A.4010800@etorok.net \
    --to=edwin+ml-ocaml@etorok.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).