Repository: terminal
Issue: WT slow to return to idle CPU usage
Labels: Needs-Repro,Area-Performance,Issue-Bug,Product-Terminal

<!-- 
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨

I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING:
1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement.
2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement.
3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number).
4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement.
5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement.

All good? Then proceed!
-->

<!--
This bug tracker is monitored by Windows Terminal development team and other technical folks.

**Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**.
Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue.

If this is an application crash, please also provide a Feedback Hub submission link so we can find your diagnostic data on the backend. Use the category "Apps > Windows Terminal (Preview)" and choose "Share My Feedback" after submission to get the link.

Please use this form and describe your issue, concisely but precisely, with as much detail as possible.

-->

# Environment

```none
Windows build number: [run `[Environment]::OSVersion` for powershell, or `ver` for cmd]
Windows Terminal version (if applicable):

Microsoft Windows 10 Pro for Workstations
10.0.19042.685 (2009, 20H2)
WindowsTerminalPreview_1.5.3242.0_x64

Any other software?
```

# Steps to reproduce

<!-- A description of how to trigger this bug. -->

Monitor WT's CPU usage (VBScript to do so included).  See that it's low after startup, idling, with a single (also idling) shell.  Open/clode several tabs ... return to idle state with one idle shell.  Observe that it takes a long time (30 min in my experiment) for WT to return to idle (i.e., almost no) CPU usage.

# Expected behavior

(Naively) I'd expect it to return to low CPU usage quickly.

<!-- A description of what you're expecting, possibly containing screenshots or reference material. -->

# Actual behavior

It doesn't happen quickly.
<!-- What's actually happening? -->

I'm not complaining or suggesting that something's wrong.  But the behavior described below is odd and I'm curious about it.

Using a script (far below), outside WT, I monitored the total CPU use of WT.  Here's the output of of the monitoring process together with my play-by-play.

> v:\> cscript //nologo wtmonitor.vbs
> 17:57:31  Total CPU: 0.53125 s
> 17:58:31  Total CPU: 0.53125 s
> 17:59:31  Total CPU: 0.53125 s

That's a newly started WT with one tab; both WT and the shell are idle.  WT is using very little CPU.  In the next minute I will open (+) and close (X) a new tab 10 times.

> 18:00:31  Total CPU: 4.5 s

That used a little CPU.  WT is back to one tab; it and the shell are idle.  Now I'll do nothing ... back to the monitoring ...

> 18:01:31  Total CPU: 8.015625 s
> 18:02:31  Total CPU: 10.375 s
> 18:03:31  Total CPU: 11.5625 s
> 18:04:31  Total CPU: 12.078125 s
> 18:05:31  Total CPU: 13.859375 s
> 18:06:31  Total CPU: 17.5625 s
> 18:07:31  Total CPU: 19.890625 s
> 18:08:31  Total CPU: 20.140625 s
> 18:09:31  Total CPU: 22.875 s
> 18:10:32  Total CPU: 23.96875 s
> 18:11:32  Total CPU: 24.21875 s
> 18:12:32  Total CPU: 26.46875 s
> 18:13:32  Total CPU: 30.171875 s
> 18:14:32  Total CPU: 33.859375 s
> 18:15:32  Total CPU: 37.484375 s
> 18:16:32  Total CPU: 38.4375 s
> 18:17:32  Total CPU: 39.09375 s
> 18:18:32  Total CPU: 42.5625 s
> 18:19:32  Total CPU: 45.0625 s
> 18:20:32  Total CPU: 48.125 s
> 18:21:32  Total CPU: 50.34375 s
> 18:22:32  Total CPU: 50.96875 s
> 18:23:32  Total CPU: 52.71875 s
> 18:24:32  Total CPU: 54.65625 s
> 18:25:32  Total CPU: 55.59375 s
> 18:26:32  Total CPU: 59.203125 s
> 18:27:32  Total CPU: 62.90625 s
> 18:28:32  Total CPU: 66.703125 s
> 18:29:32  Total CPU: 68.828125 s
> 18:30:32  Total CPU: 72.15625 s
> 18:31:32  Total CPU: 72.15625 s
> 18:32:32  Total CPU: 72.15625 s

It finally calmed down.  And as long as WT is idle, it continues to use almost no CPU.  But during that 30 minutes it was using, on the average, a little more than 2 seconds CPU per minute.  That's over 3.3% which is non-trivial.  From here it looked like WT was idling.  What was it doing?

Here's the VB script I used (outside WT) to momitor the CPU usage.

> Set iWMI = GetObject("winmgmts:\\.\root\CIMV2")
> For i=1 to 2 step 0
> 	Set colItems = iWMI.ExecQuery("SELECT KernelModeTime,UserModeTime FROM Win32_Process where Name = 'windowsterminal.exe'")
> 	For Each objItem in colItems
> 		kt = CLng(objItem.KernelModeTime)
> 		ut = CLng(objItem.UserModeTime)
> 		total = (kt + ut) / 10000000
> 		Wscript.Echo Time() & "  Total CPU: " & total & " s"
> 	Next
> 	Wscript.Sleep 60000
> Next