transcend-io / penumbra

Encrypt/decrypt anything in the browser using streams on background threads.
https://penumbra-example.vercel.app/
Apache License 2.0
145 stars 19 forks source link

Penumbra is getting high cpu usage even when not encrypting or decrypting #214

Closed yaronvhop closed 2 years ago

yaronvhop commented 2 years ago

Can be reproduced with a very small code on chrome 99:

// This is all of the Penumbra-relevant code:
const onReady = async (
  { detail: { penumbra } } = {
    detail: self,
  },
) => {
  penumbra.setWorkerLocation('/worker.penumbra.js');
};

if (!self.penumbra) {
  self.addEventListener('penumbra-ready', onReady);
} else {
  onReady();
}
<HTML>
<HEAD></HEAD>
<Body>
    <script src="main.penumbra.js" async defer></script>
    <script src="index.js"></script>
</Body>
</HTML>

Usually it uses high CPU for ~1min, but sometimes I see it going over an hour!

image

On FF does not seem to happen at all.

When profiling on Chrome, all I can see is a bunch of empty "Task" in the flame chart, without any JS code.

Any idea?

Thanks!

eligrey commented 2 years ago

Interesting. I will definitely look into this next week, as we shouldn't be doing anything in the background without jobs running.

If I had to guess, it could potentially be misreported usage due to idle worker overhead.

yaronvhop commented 2 years ago

Hey, I see the high CPU in Windows Task manager as well

image

In my PC 3 percent is a full core (32 threads CPU), it seems the numbers do match Chrome Task Manager, both in CPU % and in CPU time.

Thanks!

yaronvhop commented 2 years ago

A screenshot from the profiler, a very weird empty tasks

image

Seems to be some internal chrome issue. I am 100% clueless Hope you will have a better idea Thanks again!

eligrey commented 2 years ago

This issue seems to be gone in Chrome 103 with Penumbra 6.0

eligrey commented 2 years ago

If I had to guess this was overhead simply due to number of threads. It uses 1 thread per available logical CPU thread. The 3% CPU usage you were witnessing was likely OS overhead.

The flame chart looks much better now in Chrome 103 though.