Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What about multiple core load management with Web Workers?

When using web workers, let's say on a quad core system or on a octo core system, how is the load handled?

I mean, let's say I have 4 workers. How will they dispatch on the 4 CPU cores according to the current load? Who handles the load? (The system ?) Are the full 4 (or 8) CPUs used?

like image 914
Samuel Maisonneuve Avatar asked May 13 '14 02:05

Samuel Maisonneuve


1 Answers

You basically have two options to get an answer to this question and there's likely a different answer for each browser and perhaps even each browser/platform combination.

Option 1) is to study the code for the browser and figure out how it actually allocates these web worker threads. This probably requires not only seeing what the browser code does, but then also requires understanding how the specific OS handles that with regards to multiple CPUs. Obviously, this can only be done by someone with access to the source code of the browser (available for open source browsers).

Option 2) is to build a test app and see what you can learn from the test app.

I took the task of building a test app and then see what I can learn from it in the current versions of popular browsers. Here's the test page if you want to play with webWorkers on your system and see how the CPU load works:

http://files.the-friend-family.com/demos/webworker.html

By default the test page starts up 8 web workers. You can dial it back to any number of web workers by just unchecking or checking checkboxes on the page. Then, you can examine a system CPU monitor under different conditions. When a webworker is checked, it is running calculations to calculate prime numbers. In order to allow messaging to the webworker, it runs 1 second of calculations, pauses briefly (milliseconds) to see if there are any messages from the main browser page to process and then runs 1 more second of calculations, etc... Each second, it messages back to the main browser window with it's latest prime number.

Here's what I could conclude when running this page on my Quad Core i7-3770K CPU with Windows 8. The system monitor on my Windows 8 OS, shows 8 CPUs for my Quad Core (something about hyperthreading makes it show 8 CPUs on a quad-core processor, I think).

IE 11

8 workers running, 100% overall load, all CPUs show as saturated 100%
4 workers running, 65% overall load, all CPUs show some work, none show 100%
2 workers running, 32% overall load, 2 CPUs are near 100%, other CPUs have spikes
  of activity
1 worker running, 18% overall load, 1 CPU shows most of the work, but other 
  CPUs have spikes of activity

Chrome 34

8 workers running, 100% overall load, all CPUs show as 100% all the time
4 workers running, 63% overall load, all CPUs show some work, none show 100%
2 workers running, 32% overall load, 1 CPU near 100%, second CPU shows 
  lots of work, others show sporadic spikes of activity
1 worker running, 17% overall load, 1 CPU shows most of the work, 
  but other CPUS have spikes of activity

Firefox 29

8 workers running, 30% overall load, 4 CPUs show 40% load, others show 20% load
4 workers running, 40% overall load, 4 CPUs show 80% load, others show 5% load
2 workers running, 32% overall load, 2 CPUs show near 100% load, others show 
  spikes of activity
1 worker running, 18% overall load, 1 CPU near 100%, but also has lulls of 
  no activity where others CPUs are doing the work

So, I conclude that when there's a single web worker it appears to be mostly running on a particular core, but it isn't locked to that core, it just tends to have most of the work done on that core.

With two web workers, most the work appears to be done by two particular cores but again it doesn't look like a hard binding as sometimes the other cores do some of the work.

With four web workers, IE and Chrome seems to share the work among all the processors about equally with none pegged. Firefox tends to use some of the processors for most of the work and others not very much.

With eight web workers, IE and Chrome involve 100% of all processors. Firefox almost appears to be protecting the system in some way as overall CPU load actually goes down when going from 4 web workers to 8 web workers and none of the CPUs are anywhere near 100%.


A web worker does not appear to be hard bound to a particular core. One would have to look at the source code for a particular browser on a particular platform to know if it is the browser code or the OS that is managing the core usage. I would guess (though I don't actually know), that the browser is using an OS-level thread for each web worker and then it is up to the OS how to map that to the available cores vs. the other load on the system.

Just found this info on Firefox on MDN:

The Worker interface spawns real OS-level threads

like image 108
jfriend00 Avatar answered Sep 28 '22 05:09

jfriend00