Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is context switching using up significant time?

I have been having problem with an app (which uses both java and C++ and OpenCV) which appears to be very inconsistent in the amount of time it takes to perform various tasks. To help diagnose this, I made a function in java (called one_off_speed_test()) which did nothing but a series of integer maths problems in a loop that takes about half a second, and then prints the time taken to the log. If I call this function repeatedly from within onCreate() then the time taken for each call is very consistent (+= 3%), but if I call it from within onCameraFrame(), a function that OpenCV calls when it has an image ready from the camera, then the time taken for the maths test in each fram varies by anything up to a factor of two. I decided to try the execution sampler in eclipse/DDMS and see if I could work out what was happening. I saw that when I clicked on one_off_speed_test(), it listed the parents and children of that function, along with a line saying "(context switch)". Then on that row, under a column labelled "Incl Real Time", it says "66%". Now I'm not very expert in using DDMS, and I only have a hazy idea about context switching, but from the description so far, does it look like I have a problem with context switching taking up a lot of time? Or am I misunderstanding the DDMS output.

enter image description here

like image 509
Mick Avatar asked Jul 11 '14 17:07

Mick


People also ask

What is context switching and how does it work?

A context switching helps to share a single CPU across all processes to complete its execution and store the system's tasks status. When the process reloads in the system, the execution of the process starts at the same point where there is conflicting.

What are the disadvantages of context switching in Linux?

The disadvantage of context switching is that it requires some time for context switching i.e. the context switching time. Time is required to save the context of one process that is in the running state and then getting the context of another process that is about to come in the running state.

Is context switching killing your developer productivity?

But the cognitive load needed to revisit code increases with each hour that passes after we create a new pull request. And it turns out that idle time and context switching, particularly in the pull request process, are developer productivity killers. What is Context Switching? Why Is Context Switching Time Consuming? What Is Context Switching?

How much time should you spend on context switching each day?

One study found most people average only 3 minutes on any given task (and only 2 minutes on a digital tool before moving on). Reducing the amount of context switching in your day requires a holistic approach to the way you work. There’s no one-size-fits-all solution. Instead, you’ll need to consider: Your schedule.


1 Answers

Context switch describes the time spent to execute other threads. So, when your function is called from onCameraFrame(), it shares CPU with other threads, not necessarily threads that belong to your app.

See also answers https://stackoverflow.com/a/10969757/192373, https://stackoverflow.com/a/17902682/192373

In the posted example, onCameraFrame() spent 14.413665 sec on the wall clock, of which 4.814454 sec was used by one_off_speed_test() (presumably, for 10 frames), and 9.596984 sec was spent waiting for other threads. This makes sense, because onCameraFrame() callback competes for the CPU resource with the camera service, which runs in a separate system process.

like image 53
Alex Cohn Avatar answered Oct 11 '22 10:10

Alex Cohn