Re: [chromium-dev] JavaScript worker optimization issue

193 views
Skip to first unread message

Filip Wookeng

unread,
Dec 1, 2022, 6:41:07 PM12/1/22
to Chromium-dev, joenot...@google.com, Filip Wookeng, Chromium-dev, scheduler-dev, km...@chromium.org

I analysed the problem before I posted this. It's not memory related or anything like this. The site has anti bot counter measure script it seems which creates POW challenge and offloads it to workers. They create like 10 different workers that compute very heavy mathematical tasks for 10~ seconds sometimes longer depending on some of their criteria. I analysed the spikes in CPU when I visit the site and when this happens and when I close the tab. I even tested myself to create 10 different workers doing mathematical heavy tasks and my system and every other system behaves same.

I'm trying to understand is there some way I can force the workers to work in a single thread instead opening every new thread for every new worker? Because right now the more workers there are doing this the more CPU intensive this becomes. I understand workers shouldnt be so heavy, but keep in mind they are doing very mathematical heavy operations and it's indeed causing this behavior. I tried it on 3 different Windows machines.

I fixed the problem by overriding script to do eval on the blob instead creating worker but I am trying to find internal fix for this problem. Do you know if something is this is possible?
четвртак, 1. децембар 2022. у 22:55:22 UTC+1 joenot...@google.com је написао/ла:

On Thu, Dec 1, 2022 at 3:19 PM K. Moon <km...@chromium.org> wrote:
Are you sure it's the CPU? The operating system (which OS are you using?) is responsible for scheduling processes in a fair manner, and I would expect it to try and maintain interactive responsiveness. However, if you're running out of memory (possible if you have a lot of workers spawning and using up memory), your system could be swapping, which is more likely to create severe performance degradation.

This isn't really a chromium-dev question, though, more of a Chrome users question.

On Thu, Dec 1, 2022 at 10:24 AM Filip Wookeng <filipak...@gmail.com> wrote:
Hello,

Whenever I visit some site that nonoptimaly creates 10 worker threads, my PC just gets very slow and Chrome process just dies off. This is due to the fact that I have 4 cores or 2 cores and this site sometimes does very intensive CPU operations for whatever they are doing it. I am wondering can I somehow override core of Workers so that when new Worker is created it gets created under same thread that current JavaScript runs instead deploying new thread every time and causing mass chaos on my computer? 

Developers should be using Navigator.hardwareConcurrency check but most of them just statically creates N amount of worker threads that can do intensive CPU operation and it's not very good for me and my friends who browse this site very often. 

JavaScript code of creating Worker can be something like :
const worker = new Worker("worker.js"); 
  worker.onmessage = (msg) => { output.textContent = `Result: ${msg.data}`; };

Or worker.js can also be a blob previously defined.

More information how you can create Worker can be found here https://developer.mozilla.org/en-US/docs/Web/API/Worker

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/eb0ead90-8a5b-4ad4-a5c4-4f0d017f2d74n%40chromium.org.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.

K. Moon

unread,
Dec 1, 2022, 6:46:28 PM12/1/22
to Filip Wookeng, Chromium-dev, joenot...@google.com, scheduler-dev
Just out of curiosity, have you tried the experiment on a non-Windows machine? I'm wondering if the Windows scheduler's behavior is poor, or if something else is going on.

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.

K. Moon

unread,
Dec 1, 2022, 6:51:08 PM12/1/22
to Filip Wookeng, Chromium-dev, joenot...@google.com, scheduler-dev
Also, can you file a bug on crbug.com/new to look into whether this is something that can be improved? To be clear, I think the main problem to address is, "my PC just gets very slow and Chrome process just dies off"; a site using a lot of CPU, by itself, isn't a significant problem, but the browser and operating system should remain responsive to user input.

Bruce Dawson

unread,
Dec 2, 2022, 2:53:43 PM12/2/22
to filipak...@gmail.com, Chromium-dev, joenot...@google.com, scheduler-dev, km...@chromium.org
I don't think there is anything that Chrome could or should do about this. Worker threads are a powerful tool and I can't see how we can prevent abuse without also preventing valid uses.

We could require permissions in order to spawn worker threads but that would be a web-standards question and would require that users answer questions which they may have no way to understand the implications of.

It sounds like this website is misbehaving. Your best bet is to contact the site developer and ask them to not do that. They could do these operations on a single thread, or they could adjust the number of threads based on navigator.hardwareConcurrency. But, as a Chrome for Windows developer I don't see a way that Chrome can make ten CPU-busy threads on a 2/4-core machine behave well.

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to a topic in the Google Groups "Chromium-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/a/chromium.org/d/topic/chromium-dev/tr-_us7ULGk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/539228ac-f2d8-45b0-9597-f2eeaaa218een%40chromium.org.


--
Bruce Dawson, he/him

K. Moon

unread,
Dec 5, 2022, 9:32:56 AM12/5/22
to Bruce Dawson, Filip Wookeng, Chromium-dev, joenot...@google.com, scheduler-dev
I don't think it's strictly true that nothing can be done; for example, on Linux, scheduling is divided by task groups, not individual processes, so 10 busy threads in a single task group might still only be allowed to grab, say, a third of the available CPU time, leaving plenty for other processes. I would expect other operating system schedulers to have similar capabilities.

I think these sort of specific discussions might be better to have on a bug, though. There might be an existing bug, too.

K. Moon

unread,
Dec 5, 2022, 9:43:41 AM12/5/22
to Bruce Dawson, Filip Wookeng, Chromium-dev, joenot...@google.com, scheduler-dev
(That is, of course, if other processes want the CPU. If the Web site is the only process that wants the CPU, then yes, it should be allowed to consume all the CPU. But then this shouldn't impact interactive performance, because there's no external competition for CPU time anyway.)

Jayson Adams

unread,
Dec 5, 2022, 12:16:44 PM12/5/22
to Chromium-dev, K. Moon, Filip Wookeng, Chromium-dev, joenot...@google.com, scheduler-dev, bruce...@google.com
> If the Web site is the only process that wants the CPU, then yes, it should be allowed to consume all the CPU.

In general, I think we want to shy away from the notion that Chrome and the programs it hosts are OK to consume all resources as long as they're not in use by other programs. For memory, as Bruce has pointed out in the past, the OS often uses free RAM as a file cache, so consuming all available memory can hurt performance. For a website consuming all available CPU cycles to perform non-useful work, that can drain my laptop battery pretty quickly.

Best,

__jayson


To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to a topic in the Google Groups "Chromium-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/a/chromium.org/d/topic/chromium-dev/tr-_us7ULGk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chromium-dev+unsubscribe@chromium.org.


--
Bruce Dawson, he/him

K. Moon

unread,
Dec 5, 2022, 6:21:59 PM12/5/22
to Jayson Adams, Chromium-dev, Filip Wookeng, joenot...@google.com, scheduler-dev, bruce...@google.com
I think the important point is that the user chose to run this CPU-devouring site, and unless we want to make value judgments about the proper use of CPU time, that is the user's choice; it's certainly easy to imagine legitimate use cases for a site that spins up a bunch of CPU-intensive threads (although it's typically not efficient to schedule more CPU-intensive threads than there are available processing units, so that part probably wouldn't happen in legitimate use cases).

The user should be empowered to discover such usage, of course, and terminate it if it's undesirable. (I think I heard about separate efforts to work on something like that, but I don't recall the details.)

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to a topic in the Google Groups "Chromium-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/a/chromium.org/d/topic/chromium-dev/tr-_us7ULGk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chromium-dev...@chromium.org.


--
Bruce Dawson, he/him

Reply all
Reply to author
Forward
0 new messages