Extra:Partial Context Switching

From COMP15212 Wiki
Revision as of 12:46, 26 July 2019 by gravatar Yuron [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This should only be read if you are already happy with the principle of context switching; it contains some quite advanced material but links context switching and some exception processing.

A modern processor may contain several semi-separate functional units; a good example is the Floating-Point Unit (FPU) which handles additions, subtractions, multiplications, divisions etc. on values in floating point format (such as this). This is often separate from the integer unit which handles the integer calculations.

Other units may also be included for digital signal processing or graphics acceleration: a couple of examples are Intel’s SSE and ARM’s NEON units.

Register context

These additional units hold some process/thread context if they are present; this means (in principle) they need to be copied out and new values inserted on each context switch. However relatively few processes use floating-point calculations – user processes may but most system processes will not – and even then these may be rare. Thus copying values in and out which are never used is an unnecessary expense.

A typical ‘trick’ is to delay switching floating-point (etc.) registers on the assumption that the next process(es) to run won’t care. It may even be that only one active process uses the unit so they will still be in place when it runs again; even if not, a number of switches can be avoided, reducing the context-switching overhead.

Context switches required

There needs to be some way of detecting if the floating-point (etc.) unit is used by the application process. A typical approach is to switch the unit ‘off’ and run anyway: if a relevant instruction is encountered the processor cannot then run it and an exception results. The exception handler can then perform the relevant switch, re-enable the unit and return.

Multiprocessors

Of course, if this approach is used it causes more complications for the scheduler if there are multiple processor cores. It may favour the same core for a particular process rather than try to move the state. This could, for example, involve delaying the scheduling of a process until the relevant core is available.

Other implications are left for the reader …

Interrupts

A somewhat different ‘partial context switch’ occurs every time a procedure call is made, in that processor registers may be saved and reused. A particular example is during an [Interrupt_Service_Routine interrupt service routine] – a ‘procedure call’ by the O.S. outside the user’s control.

Most interrupt service routines will need to ‘borrow’ some processor registers but it is imperative that they must not change anything the application could notice. This could be done by running the interrupt service as a separate process but usually full (expensive) context switching is avoided for efficiency and only a subset of the user’s context is (temporarily) affected.