OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 16, 2024 3:21 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Too many context switches in a microkernel?
PostPosted: Mon Aug 24, 2015 5:04 am 
Offline
Member
Member
User avatar

Joined: Fri Aug 21, 2009 5:54 am
Posts: 178
Location: Moscow, Russia
I've added context switch counters to my kernel, and the results got me thinking.

The timer is running at 200 Hz, and in Qemu i get 1000-1500 switches per second.
It's the voluntary preemption.
A driver thread would poll for events, find none and yield.
Services would poll for messages, find none and yield.
A terminal would poll for key press messages, find none and yield.
And so on.

There are flurries of activity when something happens, but most of the time the non-waiting processes just yield to each other, only slowed down by the kernel idle process issuing a hlt instruction.
Without it, i get 5500 switches per second in Qemu - i guess it runs as fast as it can switch.

So, how normal is that?
On one hand, you'd want the system to spend most of the time in idle/low power mode when nothing is happening.
On the other hand, you want fast responses when something does happen.
The scheduler can't really keep track of what would or wouldn't get an event, so i can't quite see a way to improve both things at once.
It's feasible to balance one way or the other - i.e. make the system idle until the next timer interrupt when a process yields.

Any thoughts?


Top
 Profile  
 
 Post subject: Re: Too many context switches in a microkernel?
PostPosted: Mon Aug 24, 2015 5:33 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
The code that generates the event should also wake up the process that's supposed to receive that event. A process that has no pending events or work should put itself to sleep. So instead of a Yield call you can use a wait call instead.

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject: Re: Too many context switches in a microkernel?
PostPosted: Mon Aug 24, 2015 5:37 am 
Offline
Member
Member

Joined: Thu Mar 25, 2010 11:26 pm
Posts: 1801
Location: Melbourne, Australia
Your design seems quite strange. I don't quite understand why you need to switch context to find that something doesn't need to run. In most microkernels the kernel either executes an idle thread or hlts when there is nothing to do. When a message or event arrives for a thread that thread is woken up to process the message. Once the processing is done it sleeps until another message or event arrives for it. Other threads are never considered for running by the scheduler.

_________________
If a trainstation is where trains stop, what is a workstation ?


Top
 Profile  
 
 Post subject: Re: Too many context switches in a microkernel?
PostPosted: Mon Aug 24, 2015 6:28 am 
Offline
Member
Member
User avatar

Joined: Fri Aug 21, 2009 5:54 am
Posts: 178
Location: Moscow, Russia
Hm.
The way it evolved, task scheduling and messaging are two separate systems with no communications.

So, in the simplest shape it's a matter of putting the waiting threads to sleep and make the message processor wake a thread when a message is posted for it?


Top
 Profile  
 
 Post subject: Re: Too many context switches in a microkernel?
PostPosted: Mon Aug 24, 2015 7:16 am 
Offline
Member
Member

Joined: Sat Mar 01, 2014 2:59 pm
Posts: 1146
Artlav wrote:
So, in the simplest shape it's a matter of putting the waiting threads to sleep and make the message processor wake a thread when a message is posted for it?
Yes.

_________________
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 96 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group