3b73576ab9
* Instead of reading the output line-by-line, figure out what lines we need ahead-of-time and issue as few read calls as possible. On Windows 8 and up, we issue just one read call. On earlier versions, we avoid reading more than a certain amount. * This change reduces the CPU usage. e.g. In my Windows 10 VM, the idle CPU usage of winpty-agent.exe+conhost.exe combined, with an empty console, dropped from ~3.6% to ~1.4%. In a Windows 7 VM, I measured a reduction of CPU from ~1.6% to 0.6%. * Increase the MAX_CONSOLE_WIDTH from 500 to 2500. The limiting factor now is that LargeConsoleRead reads at least one line at a line, but we don't want to read more than 2500 characters in one call on old operating systems. * Fix the attribute handling in scanForDirtyLines. (The assignment to newAttr was dead.) The 2500 limit is arbitrary and could probably be increased. The actual hard limit depends on the OS and is around 17000. My understanding is that the limit is based upon the need to allocate I/O buffers within a shared 64KiB heap, and I'm worried about heap fragmentation. I know that 2500 is safe, because winpty has been issuing reads of almost 3000 characters already to find the sync marker. Fixes https://github.com/rprichard/winpty/issues/44
120 lines
3.7 KiB
C++
120 lines
3.7 KiB
C++
// Copyright (c) 2011-2012 Ryan Prichard
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to
|
|
// deal in the Software without restriction, including without limitation the
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
// IN THE SOFTWARE.
|
|
|
|
#ifndef AGENT_H
|
|
#define AGENT_H
|
|
|
|
#include <windows.h>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "EventLoop.h"
|
|
#include "DsrSender.h"
|
|
#include "Coord.h"
|
|
#include "SmallRect.h"
|
|
#include "ConsoleLine.h"
|
|
#include "Terminal.h"
|
|
#include "LargeConsoleRead.h"
|
|
|
|
class Win32Console;
|
|
class ConsoleInput;
|
|
class ReadBuffer;
|
|
class NamedPipe;
|
|
struct ConsoleScreenBufferInfo;
|
|
|
|
// We must be able to issue a single ReadConsoleOutputW call of
|
|
// MAX_CONSOLE_WIDTH characters, and a single read of approximately several
|
|
// hundred fewer characters than BUFFER_LINE_COUNT.
|
|
const int BUFFER_LINE_COUNT = 3000;
|
|
const int MAX_CONSOLE_WIDTH = 2500;
|
|
const int SYNC_MARKER_LEN = 16;
|
|
|
|
class Agent : public EventLoop, public DsrSender
|
|
{
|
|
public:
|
|
Agent(LPCWSTR controlPipeName,
|
|
LPCWSTR dataPipeName,
|
|
int initialCols,
|
|
int initialRows);
|
|
virtual ~Agent();
|
|
void sendDsr();
|
|
|
|
private:
|
|
NamedPipe *makeSocket(LPCWSTR pipeName);
|
|
void resetConsoleTracking(
|
|
Terminal::SendClearFlag sendClear, const SmallRect &windowRect);
|
|
|
|
private:
|
|
void pollControlSocket();
|
|
void handlePacket(ReadBuffer &packet);
|
|
int handleStartProcessPacket(ReadBuffer &packet);
|
|
int handleSetSizePacket(ReadBuffer &packet);
|
|
void pollDataSocket();
|
|
|
|
protected:
|
|
virtual void onPollTimeout();
|
|
virtual void onPipeIo(NamedPipe *namedPipe);
|
|
|
|
private:
|
|
void markEntireWindowDirty(const SmallRect &windowRect);
|
|
void scanForDirtyLines(const SmallRect &windowRect);
|
|
void clearBufferLines(int firstRow, int count, WORD attributes);
|
|
void resizeImpl(const ConsoleScreenBufferInfo &origInfo);
|
|
void resizeWindow(int cols, int rows);
|
|
void syncConsoleContentAndSize(bool forceResize);
|
|
void syncConsoleTitle();
|
|
void directScrapeOutput(const ConsoleScreenBufferInfo &info);
|
|
void scrollingScrapeOutput(const ConsoleScreenBufferInfo &info);
|
|
void reopenConsole();
|
|
void freezeConsole();
|
|
void unfreezeConsole();
|
|
void syncMarkerText(CHAR_INFO (&output)[SYNC_MARKER_LEN]);
|
|
int findSyncMarker();
|
|
void createSyncMarker(int row);
|
|
|
|
private:
|
|
Win32Console *m_console;
|
|
NamedPipe *m_controlSocket;
|
|
NamedPipe *m_dataSocket;
|
|
bool m_closingDataSocket;
|
|
Terminal *m_terminal;
|
|
ConsoleInput *m_consoleInput;
|
|
HANDLE m_childProcess;
|
|
int m_childExitCode;
|
|
|
|
int m_syncRow;
|
|
int m_syncCounter;
|
|
|
|
bool m_directMode;
|
|
Coord m_ptySize;
|
|
int m_scrapedLineCount;
|
|
int m_scrolledCount;
|
|
int m_maxBufferedLine;
|
|
LargeConsoleReadBuffer m_readBuffer;
|
|
std::vector<ConsoleLine> m_bufferData;
|
|
int m_dirtyWindowTop;
|
|
int m_dirtyLineCount;
|
|
|
|
std::wstring m_currentTitle;
|
|
};
|
|
|
|
#endif // AGENT_H
|