DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 4B3M04ej2511484
Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com
Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com
DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 4B3M04ej2511484
Authentication-Results: delorie.com;
	dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=V3LdkRBe
X-Recipient: archive-cygwin@delorie.com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 035F93858C56
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
	s=default; t=1733263202;
	bh=tXFobwfHTW6C10PfTVfO6smNK9n/AlMNQ+p1OX2tGHE=;
	h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe:
	 List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:
	 From;
	b=V3LdkRBeR17I/AIwXKGHYBOn7oR9vtoxFMQBJRQ6HxRHTEZscM4uhR3e7mVxl2mAy
	 AoRahPaFfonbi34G7RfWB25186NeYpxRPcZHbRKYgzo3KIdqmcP/Pi3WPcGXQx7zP6
	 +8qvLhxjI5DbtgGkY+XVjeuxxlz8HHr/vgfAiKlc=
X-Original-To: cygwin@cygwin.com
Delivered-To: cygwin@cygwin.com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 266533858C41
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 266533858C41
ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1733263131; cv=pass;
 b=DCpPvK+rZXbF3f2SDpDfg4yM51zXicxt6ss/X9/rMS1+ZqC9263m1YEV+23O+M4TDNnJ5A388IVThYUa+RF3Q+WLhNhAU0OYCfSyPY4U1+hp8bQh8H4uadSociTziMZ7/qKjrkkwVqsDBJ8VElk4Z33U0jq5reSoJ1Wu3f1YqpU=
ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key;
 t=1733263131; c=relaxed/simple;
 bh=WjaMBkPbOP8J+RiZ9rhsZeuxUMXSvFcuvkxrXf2kalI=;
 h=DKIM-Signature:Message-ID:Date:Subject:To:From:MIME-Version;
 b=lGrdyff++uo3tw/8oOj9qDLzdsX34JrAG4hTd4XH0z1OtRMgBVa7U1ysUShcS9QEvqBsvuPlC0IVos3doOIzO+DTxiv5tC3WKvq4DwncO3h86hIWMOuL9e/Gp9KLI0HaYIKMGhOGiTGWSTgQTASQxwhQtRm68I6L3B3vInSTJD0=
ARC-Authentication-Results: i=2; server2.sourceware.org
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 266533858C41
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
 b=Y38A2/jOgQtxGNsxM5SINXwevLz+k4pmC8qVqxdmqu2MszVS+7XazyFp5JsOntDK0fKJbrgtdctCWzBDdRemn5BvVNNl9vzgzLwbiVd8ofvexRi+Gelgj6YHEeouMoO3xtlWvi0VJWaRxLZrHcwU0TWLngLL0nqG3X7IOID0CKqTus2qQO64TzfKdaoE6CKk7SwfHy43PM1/14XWuvvD7xegk9nqWcPLNa4AAqXnW7EkEusD5SM0yuwhwXKQcnKNPxj9Z5sbs3grr6pXzHbUbOh8IUHHQilFweaTfq9lP+AyIOIwFQK5arSc6fmJyRM/7ErEspj5/37uyOWp4yb6lg==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; 
 s=arcselector10001;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
 bh=7mREaenM/xlz5WyRrlsVFbvXthWe6XpZoRaOsylMPo8=;
 b=JyqYnxk3teRtdotngtOKhq6OL/e5cHeHprYuKzuKa9ajUwl1X+slk62IhCECmB2ydrxVSVd17MmwS+WlOvNUYvLhB4kvXy7NK5E0osp3+b0sQZnWpXViNjE/alJY0xrLqKeIboxzQ1QC90+VYMkdVl6pWXSSoR10VzxtK5b+EavTi76H4q82TAXj62Mtnn5jyFcogsTASP0spzUGtdTeTKBEYL7gqq55REA1oidLc0LsFwnG5pFE+WSwBl7Sxp61L/vCPRPDNhop+4qm3xWyAJteXzzmcUyd4O3vnr36C+4AvGwO00JiiSkvF6UQlqEsuVnVIymCUuGLNwG27KQX8Q==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none;
 dkim=none; arc=none
Message-ID: <PH7PR11MB64521BADC2AEC9B131AD49C4A4362@PH7PR11MB6452.namprd11.prod.outlook.com>
Date: Tue, 3 Dec 2024 13:58:45 -0800
User-Agent: Mozilla Thunderbird
Subject: Re: Calling GetConsoleProcessList in tight loop allocates new buffer
 via condrv ioctl results in excessive page-faults with Windows
 Terminal
To: Takashi Yano <takashi.yano@nifty.ne.jp>, cygwin@cygwin.com
References: <PH7PR11MB64522B9A097A9A5D963E5C23A4352@PH7PR11MB6452.namprd11.prod.outlook.com>
 <20241203204242.4bd45fc54e31431ceaf94816@nifty.ne.jp>
Content-Language: en-US
In-Reply-To: <20241203204242.4bd45fc54e31431ceaf94816@nifty.ne.jp>
X-ClientProxiedBy: BY3PR10CA0014.namprd10.prod.outlook.com
 (2603:10b6:a03:255::19) To PH7PR11MB6452.namprd11.prod.outlook.com
 (2603:10b6:510:1f3::12)
X-Microsoft-Original-Message-ID: <db88c5b6-f444-4a3b-b8f4-ef9cf5521591@outlook.com>
MIME-Version: 1.0
X-MS-Exchange-MessageSentRepresentingType: 1
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: PH7PR11MB6452:EE_|SN7PR11MB8027:EE_
X-MS-Office365-Filtering-Correlation-Id: dc4c4196-596a-4dfb-ab65-08dd13e5aa98
X-Microsoft-Antispam: BCL:0;
 ARA:14566002|461199028|6090799003|19110799003|5072599009|8060799006|15080799006|10035399004|440099028|3412199025|4302099013|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Q3VGRaZmVjQUFzUVBR?=
 =?utf-8?B?Z1N0TlNjbzJ4bC9YVjl4eE9YR1NUY3NxbXZCNkVvVFBKZDc1WGg0NHJKWVVr?=
 =?utf-8?B?U2hYaUUzMmhINjBHQVpyS1ZYN3laOWZ6L09VUDFyc0FyQTJyZXF2QjF1b2VS?=
 =?utf-8?B?YXJ2V0Q0MDhXSkFqK0tRYWpkaXBXbkxBdlp3WVArVzE4MitpTUsvem1SVmEy?=
 =?utf-8?B?NXArZnY0a0Zhck8zTjNBM2tRZ2IwRnlaeG9DTVhUQ1FKVnFuUHJCSjhCdEVU?=
 =?utf-8?B?U1VsZ1djVnF2a3ArY1VJT2ZmWCtKZXlCQ0lXUVJ0ZGwzZ0xlWFpjVVNDdzUx?=
 =?utf-8?B?THNlQVVCN0hVZVpIQXZodFlQcDdCRGc3dDIxNktJY29vMWJHN0tFdloyQkQv?=
 =?utf-8?B?TEQ4T1pWcld0MktpM0MwcS9MU3MvTUgzTWZvNlBPMk1mZlQ4QUhWbkg5ZjZL?=
 =?utf-8?B?QWhibDcxaG9SdWlzL1RjcDE1UTduTWVRTjNERVIyblVLZWorWDduRmdES211?=
 =?utf-8?B?VDg5ZDdOY1RSVkQxby80MVUrRHowdy9WQVZMNlZGZ3NQZENaNWkrcnQrL3V4?=
 =?utf-8?B?R2VIYVM1MGVpblJUQWRacWZBK3orRE5WaEZ4aXNoNHZWMU5ON0UrSWxLbG5L?=
 =?utf-8?B?QWlMY3plZFVoamlkNmpJa3hZNVc1c2psZVlGU1kwL20xVWhFeWNtSXdKeGRF?=
 =?utf-8?Q?va+7ULeM3A7p7kQSWP/if3B0VHYNFYH4NZbA8Tscksxs=3D?=
X-OriginatorOrg: outlook.com
X-MS-Exchange-CrossTenant-Network-Message-Id: dc4c4196-596a-4dfb-ab65-08dd13e5aa98
X-MS-Exchange-CrossTenant-AuthSource: PH7PR11MB6452.namprd11.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 21:58:48.9085 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa
X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000
X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR11MB8027
X-BeenThere: cygwin@cygwin.com
X-Mailman-Version: 2.1.30
Precedence: list
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-request@cygwin.com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=subscribe>
From: Steven Buehler via Cygwin <cygwin@cygwin.com>
Reply-To: Steven Buehler <buehlersj@outlook.com>
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="us-ascii"; Format="flowed"
Errors-To: cygwin-bounces~archive-cygwin=delorie.com@cygwin.com
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie.com@cygwin.com>

On 12/3/2024 3:42 AM, Takashi Yano wrote:
> On Mon, 2 Dec 2024 19:57:25 +0000
> Steven Buehler wrote:
>> I am experiencing an abnormal number of page-faults per second (averaging 800 to over 2000) when using Cygwin within the Windows Terminal app. This produces visible stutters and cursor movement during terminal screen redraws.
>>
>> I have opened an issue on the Windows Terminal GitHub issues page. The initial investigation by one of the Windows Terminal developers has determined that "Cygwin is calling console APIs in its steady state. It looks like it's calling GetConsoleProcessList in a tight loop, which results in the allocation of a new buffer that is returned to their process via condrv's ioctl interface. I don't think there's anything we can do about that, other than stopping them from doing so."
>>
>> Following this response, I am attempting to bring this issue to the Cygwin developer team's awareness for a possible resolution. Please see https://github.com/microsoft/terminal/issues/18264 for a detailed discussion and accompanying video demonstration of the page-fault counter.
> 
> Thanks for the report.
> However, I cannot reproduce your problem.
> Which cygwin version do you use?
> 

I am using Cygwin version 3.5.4.

FWIW, the GitHub issue at 
https://github.com/microsoft/terminal/issues/18264 has just been updated 
with the following comment by lhecker:

I figured out why this happens: When we read from the console pipe, we 
currently need to provide a buffer where the message is copied into, 
similar to ReadFile. In order to slightly amortize the cost of 
allocating a buffer we reuse the previous allocation unless the previous 
one was >16KiB and the next one is less than half the size. This avoids 
issues where a huge 100MB buffer never gets freed.
This however does not work well for this usage of the API, because the 
GetConsoleProcessList call needs 128KiB and the PeekConsoleInput call 
only needs 20 (?) bytes. This causes the buffer to be repeatedly freed 
and reallocated.

IMO there's 3 things we should do:

    * cygwin should avoid making "large" data requests if there's no 
reason for that. That DWORD buf[65536]; buffer is 128KiB large and in 
practice maybe 16 byte of that will ever be used. It could improve the 
code by only increasing the request size if the return value is equal to 
the buffer size.

    * We should increase the cutoff size from 16KiB to 128KiB, because 
that's the size of the cat chunk size anyway.

    * We should switch to a cheap allocator for processing console 
messages in the future, because malloc is a poor fit for it. But this 
doesn't fix the issue for large allocations, since we still need to be 
conservative about our resident set size. This means that even with a 
custom allocator, cygwin's usage pattern may still be suboptimal.


-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple
