3

I ran into a problem with Firefox on Xubuntu. Sometimes, Firefox hogs one CPU core, while all other cores are idling. Website buildup was very slow and very annoying.

I noticed that when I leave my computer alone for some time (without a reboot), something magical happens, and then Firefox again uses all CPU cores equally.

Somewhere, I read that CPU cores are attached to items in cache/buffer, and so it's actually better when a (multi-core) program temporarily uses one CPU core, instead of freeing up the other cores from their cache/buffer jobs.

Can somebody give some further explanation on how CPU loads are balanced?

closed as off-topic by Ipor Sircer, schily, Rui F Ribeiro, Romeo Ninov, G-Man Jun 15 '18 at 4:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Requests for learning materials (tutorials, how-tos etc.) are off topic. The only exception is questions about where to find official documentation (e.g. POSIX specifications). See the Help Center and our Community Meta for more information." – Ipor Sircer, schily, Rui F Ribeiro, Romeo Ninov
If this question can be reworded to fit the rules in the help center, please edit the question.

2

In general, UNIX and Linux scheduling assumes lots of short programs (think ls, ps, grep ...) so it will start a program on one CPU. As programs run longer (think Firefox, or Chrome), the kernel realizes the load is imbalanced and will migrate the processes to different CPUs. This is done by the migration job (as seen by running ps).

https://superuser.com/questions/440906/what-is-the-migration-process

To understand how migration is done, refer to: https://stackoverflow.com/questions/49707124/how-does-linux-kernel-migrate-the-process-among-multiple-cores

 * This is how migration works:
 *
 * 1) we invoke migration_cpu_stop() on the target CPU using
 *    stop_one_cpu().
 * 2) stopper starts to run (implicitly forcing the migrated thread
 *    off the CPU) 
 * 3) it checks whether the migrated task is still in the wrong runqueue.
 * 4) if it's in the wrong runqueue then the migration thread removes
 *    it and puts it into the right queue.
 * 5) stopper completes and stop_one_cpu() returns and the migration
 *    is done.

To understand how load is determined refer to: https://github.com/torvalds/linux/blob/master/kernel/sched/fair.c and look at the comments around migration. But overall, the scheduler has to look at:

  • how busy the CPU is
  • the cost to migrate the process
  • the number of system faults generated

Then it has to determine which process to migrate and which CPU to migrate to.

This is all fairly complicated and so if you wish to understand this further, I recommend looking at: https://blog.acolyer.org/2016/04/26/the-linux-scheduler-a-decade-of-wasted-cores/ and the original paper: The Linux Scheduler: a Decade of Wasted Cores.

Not the answer you're looking for? Browse other questions tagged or ask your own question.