public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: <mgix@mgix.com>
To: <rusty@rustcorp.com.au>, "David Schwartz" <davids@webmaster.com>
Cc: <linux-kernel@vger.kernel.org>, <mingo@redhat.com>
Subject: RE: Question about sched_yield()
Date: Tue, 18 Jun 2002 13:42:20 -0700	[thread overview]
Message-ID: <AMEKICHCJFIFEDIBLGOBEEELCBAA.mgix@mgix.com> (raw)
In-Reply-To: <E17KPS1-0003pP-00@wagner.rustcorp.com.au>



> Given that yield is "sleep for some time but I won't tell you what I'm
> doing", I have no sympathy for yield users 8)

Here's something interesting at last.

True, when userland calls sched_yield, the kernel is left in the dark.

However, I am not aware of any alternative to communicate what
I really want to the scheduler, and here's why. If anyone has
ideas on how to do this better, please, I'm all ears.

It's basically about spinlocks and the cost of task switching.

I'm trying to implement "smart" spinlocks.

Regular blocking lock based on some kind of kernel object are:

	1. Slow because every unsuccessful use of the lock will
         trigger a task switch. There is no way to have two threads
         exchange messages at a very fast rate with a blocking lock.

	2. Expensive in terms of kernel resource consumption: you end
         up allocating a kernel object each time you need a lock.
         If your app's locking is sufficienly fine-grained, you end
         up allocating a huge number of kernel objects.

	3. To avoid problem 2., you can try and allocate kernel objects
         on the fly, but then performance gets even worse: every lock
         acquisition/release requiring the creation/destruction of a
         kernel object.

The alternative, userland spinlocks :

	1. Cost zero in term of kernel resource consumption.

	2. Are a major catastrophe when running on a UP box
         (you can spin as long as you want, nothing's going to
          change since there's only one CPU and you're hogging it)

	3. A CPU hog at best when running on an SMP boxes: the spinning
         thread gobbles up a whole 100% of a CPU.

"Smart" spinlocks basically try and do it this way:

	int spinLoops= GetNumberOfProcsICanRunOn() > 1 ? someBigNumber : 1;
	while(1)
	{
		int n= spinLoops;
		while(n--) tryAndGetTheSpinLock();
		if(gotIt) break;
		sched_yield();
	}

These seem to have all the qualities I want:

1. On a UP box, the thread will poke at the spinlock only once,
   and then yield and *hopefully* get scheduled till later, giving
   a chance to the lock owner to finish its stuff and release.

   Net result: spinning thread gives no CPU consumption, no kernel
   object allocated, and proper locking behaviour.

2. On an SMP box, the thread will bang on the spinlock a large
   number of times, hoping to get it before it gets taskswitched away.
   If it does, great: no time lost.
   If it doesn't, we're out of luck, yield the CPU and try again next time.

   Net result: tunable balance between CPU consumption and task switching
   costs, no kernel object allocated, and proper locking behaviour.


I'm sure this issue has been beaten to death before, but I
just wanted to gove my $.02

	- Mgix





  parent reply	other threads:[~2002-06-18 20:42 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-15 22:15 Question about sched_yield() mgix
2002-06-16 14:43 ` [patch] " Ingo Molnar
2002-06-18  0:46 ` David Schwartz
2002-06-18  0:55   ` Robert Love
2002-06-18  1:51     ` mgix
2002-06-18  3:18     ` David Schwartz
2002-06-18  9:36     ` David Schwartz
2002-06-18 16:58       ` Chris Friesen
2002-06-18 17:12         ` Richard B. Johnson
2002-06-18 17:19           ` mgix
2002-06-18 18:01             ` David Schwartz
2002-06-18 18:05               ` mgix
2002-06-18 19:11                 ` David Schwartz
2002-06-18 16:58                   ` Rob Landley
2002-06-18 19:25                   ` Robert Love
2002-06-18 19:53                     ` David Schwartz
2002-06-18 20:12                       ` mgix
2002-06-18 20:42                         ` David Schwartz
2002-06-18 20:47                           ` mgix
2002-06-18 22:00                             ` David Schwartz
2002-06-18 22:28                           ` Ingo Molnar
2002-06-18 20:08                     ` Richard B. Johnson
2002-06-19 11:10                     ` Bill Davidsen
2002-06-19 12:04                       ` Ingo Molnar
2002-06-18 22:43               ` Olivier Galibert
2002-06-18 18:21             ` Richard B. Johnson
2002-06-18 17:13         ` Robert Love
2002-06-18 18:00           ` David Schwartz
2002-06-18 22:45             ` Stevie O
2002-06-19  2:11               ` David Schwartz
2002-06-19  2:52                 ` Stevie O
2002-06-20 20:31               ` David Schwartz
2002-06-18 17:23         ` Rik van Riel
2002-06-18 17:50           ` Chris Friesen
2002-06-18  1:41   ` mgix
2002-06-18  3:21     ` David Schwartz
2002-06-18  3:52       ` mgix
2002-06-18  4:55   ` Ingo Molnar
2002-06-19 11:24     ` Bill Davidsen
2002-06-19 11:47       ` scheduler timeslice distribution, threads, processes. [was: Re: Question about sched_yield()] Ingo Molnar
2002-06-18 18:56   ` Question about sched_yield() Rusty Russell
2002-06-18 19:12     ` David Schwartz
2002-06-18 20:19       ` Rusty Russell
2002-06-18 20:40         ` David Schwartz
2002-06-18 20:42         ` mgix [this message]
2002-06-18 22:03           ` David Schwartz
2002-06-18 22:36           ` Ingo Molnar
2002-06-19 11:29     ` Bill Davidsen
2002-06-19 14:03       ` Rusty Russell
2002-06-19 22:25         ` Bill Davidsen
2002-06-19 22:37           ` Ingo Molnar
2002-06-19  2:10   ` jw schultz

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=AMEKICHCJFIFEDIBLGOBEEELCBAA.mgix@mgix.com \
    --to=mgix@mgix.com \
    --cc=davids@webmaster.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=rusty@rustcorp.com.au \
    /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