winpty icon indicating copy to clipboard operation
winpty copied to clipboard

Reduce polling to reduce CPU/power consumption

Open rprichard opened this issue 9 years ago • 2 comments

Currently, winpty scans the console for changes about once every 25ms or so. It does this scanning even when no output is appearing (e.g. the default situation, most of the time). This seems wasteful of CPU time, and AIUI, regular polling is especially bad on laptops, where it could prevent a computer from entering some low-power state.

There are a couple ways to fix this:

  • Determine a maximum acceptable interval and increase the polling interval to the maximum after a period of no activity. If the interval is too long, a program could fill the entire console buffer resulting in lost output.
  • Use the Console WinEvents API to wake the agent up when a change happens.

To use the Console WinEvents API, the agent would register an event handler to receive console events on the system. The handler receives events from all consoles by default, which is suboptimal. It can be targeted at a specific PID. IIRC, on Windows 7, that PID is the conhost PID. There is no documented API to retrieve the conhost PID, but it can be determined by briefly collecting all console events and looking for a EVENT_CONSOLE_START_APPLICATION event. On earlier Windows versions, I think(?) the PID is that of the csrss.exe process.

The WinEvents API doesn't appear rich enough to supplant much of winpty's scraping, but I do wonder if winpty's latency could be improved somehow with, say, a fast path based on EVENT_CONSOLE_UPDATE_SIMPLE events.

rprichard avatar Oct 15 '15 05:10 rprichard

I tested the Console WinEvents API today, and I discovered that writing to an inactive console screen buffer doesn't generate events, so the WinEvents API won't work for removing polling from the error screen buffer in a two-screen-buffer mode.

rprichard avatar Apr 22 '16 15:04 rprichard

Apparently winpty-agent is part of VS Code and I see one of its processes constantly using 2 - 3 % CPU. Not sure if it's gonna be of any value but here's snapshot of perfview profile: image

nayato avatar Sep 21 '17 14:09 nayato