denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
96.96k stars 5.35k forks source link

DevTools reports 100% CPU while doing nothing #21620

Open alexgleason opened 10 months ago

alexgleason commented 10 months ago

Version: Deno 1.39.0
Chromium: 120.0.6099.71

I created a simple reproduction script, gg.ts:

while (true) {
  await new Promise((resolve) => setTimeout(resolve, 1000));
}

Running it with deno run --inspect gg.ts and then viewing it in Chromium DevTools, the histogram at the top shows 100% CPU usage.

image

You can check my results in your DevTools here: CPU-20231217T142344.cpuprofile

This started happening recently, and I don't know why. The CPU itself is not close to 100% on any core when viewed with htop. The problem does not occur when profiling a normal webpage in the same browser, just when inspecting Deno.

bartlomieju commented 10 months ago

I believe this diagram doesn't show actual CPU usage, but show percentage of time the program spents in CPU. The (program) you see on your flamegraph is Devtools way of saying that it's running native code (the timer implementation) and the actual JavaScript code (all the function below eventLoopTick take 0ms - which is timer firing). So if anything I guess it's poor representation from Devtools perspective.

As a side note: timers are known to have poor performance: https://github.com/denoland/deno/issues/20356 and there's ongoing work to speed them up: https://github.com/denoland/deno/pull/20428

alexgleason commented 10 months ago

I was able to dig up an old screenshot from some Deno profiling I did on November 20th:

Screenshot from 2023-11-20 19-21-32

The CPU chart at the top looks correct in this version.

I tried reverting my codebase to an old version (and downgrading to Deno 1.36), and I'm still having the problem in this issue. So that suggests to me something changed in Chromium rather than Deno, that makes Deno profiling a lot less useful.

bartlomieju commented 10 months ago

I tried reverting my codebase to an old version (and downgrading to Deno 1.36), and I'm still having the problem in this issue. So that suggests to me something changed in Chromium rather than Deno, that makes Deno profiling a lot less useful.

I agree, IIRC a couple months back (program) was colored in gray and didn't count towards "Scripting" category (which it does now).

Chrome DevTools devs asked for feedback on this panel, which appears to just been closed: https://github.com/ChromeDevTools/rfcs/discussions/2 I suggest to open an issue with them. I'll be happy to coordinate on that.

alexgleason commented 10 months ago

Thanks @bartlomieju, opened an issue here: https://bugs.chromium.org/p/chromium/issues/detail?id=1512463

june07 commented 2 months ago

See https://github.com/denoland/deno/issues/24877#issuecomment-2276989796 for a workaround/fix that is just as applicable to this issue.

alexgleason commented 2 weeks ago

This issue is still occurring on Deno 2.0 and Chromium 129 (the latest version). The people on the Chromium issue tracker didn't want to install Deno to test it. :joy: