From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on starla X-Spam-Level: X-Spam-Status: No, score=-0.0 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_GREY autolearn=ham autolearn_force=no version=3.4.6 Received: from nue.mailmanlists.eu (nue.mailmanlists.eu [94.130.110.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 3FED41F542 for ; Tue, 30 May 2023 12:27:53 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; secure) header.d=ml.ruby-lang.org header.i=@ml.ruby-lang.org header.a=rsa-sha256 header.s=mail header.b=kFfGb8TQ; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sendgrid.net header.i=@sendgrid.net header.a=rsa-sha256 header.s=smtpapi header.b=h49kCCuB; dkim-atps=neutral Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 5A23F7E8EB; Tue, 30 May 2023 12:27:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1685449664; bh=bnTFNFbNXjapGESUssN+iHWEfVfRg7pEMfz4Epa5Ato=; h=Date:References:To:Reply-To:Subject:List-Id:List-Archive: List-Help:List-Owner:List-Post:List-Subscribe:List-Unsubscribe: From:Cc:From; b=kFfGb8TQXe36XJfuxN//oCv+2tniDH0VwIvJLiWuvKd1Yx9L87VPbotWV32a0X8Xf ypHQczho36OeKgvYqPprDzXaKmCpgaGtWkSxXDJj4OvtFN0pv+zEwQJ7FpTEaT/5ui gLJG7Ru3zN0B8OFhdn47paO+pwu2q33oy2viJh3s= Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 5D1087E66E for ; Tue, 30 May 2023 12:27:41 +0000 (UTC) Authentication-Results: nue.mailmanlists.eu; dkim=pass (1024-bit key; unprotected) header.d=sendgrid.net header.i=@sendgrid.net header.a=rsa-sha256 header.s=smtpapi header.b=h49kCCuB; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sendgrid.net; h=from:references:subject:mime-version:content-type: content-transfer-encoding:list-id:to:cc:content-type:from:subject:to; s=smtpapi; bh=3qYBNkxkux8fGrXtCPn9hyHerT2l87LRTjxO1UmZxCc=; b=h49kCCuBp9AEiBufZqn6fqKFvMQJ/BhL8MTwV/FQCvyRGs52IP1hvk3IF4u+LOzfudB5 JUReqjmv3+1NuUEpDnBXlHaxgsH4qugNNm95wlhNBFUBwFLNggz8UiijfvcEcumVHTaEpY N5WnPJ4kTXbKcghM9zcKuyj5ZwGKDYMA8= Received: by filterdrecv-77869f68cc-xxcxl with SMTP id filterdrecv-77869f68cc-xxcxl-1-6475EBBB-1A 2023-05-30 12:27:39.374130298 +0000 UTC m=+1687883.157134760 Received: from herokuapp.com (unknown) by geopod-ismtpd-7 (SG) with ESMTP id NHKQxGqlS52-0fDMzb6yeQ for ; Tue, 30 May 2023 12:27:39.309 +0000 (UTC) Date: Tue, 30 May 2023 12:27:39 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Misc X-Redmine-Issue-Id: 19122 X-Redmine-Issue-Author: smcgivern X-Redmine-Issue-Assignee: ioquatix X-Redmine-Sender: smcgivern X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-Redmine-MailingListIntegration-Message-Ids: 90168 X-SG-EID: =?us-ascii?Q?kbn0INE5KbBoqhUO9+QzwHC2YIbwAV+JA+RbO58sNhgGAf5ZU7k16=2F1Yp+ipAC?= =?us-ascii?Q?1+t4NYfWlfWeLD=2Fz2NwbU94U30JJHg3=2FMNm2LHp?= =?us-ascii?Q?Ijq3dPdkr6l7bUvJQf0oPkDm76yiqTSDuzZbwCw?= =?us-ascii?Q?5kh5nlGiW+EdsZM44pfiuAs2HNFhYgzg3ETCMIa?= =?us-ascii?Q?qvU4kzT7nHCLsW8EKF=2FxPDJOg+hRptqGyUjUmDq?= =?us-ascii?Q?m+yQIQX7VrEVSQ9P1O3hJtoL9x2gPRBSFjxvh=2FB?= =?us-ascii?Q?NjvH+zPf7vMD2RCbe9mx6MV5Pge9Iyk2w7RpQ8D?= =?us-ascii?Q?gPU=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== Message-ID-Hash: V3ZENYEWSM4G66FEDHCZK6CMVENZKEKK X-Message-ID-Hash: V3ZENYEWSM4G66FEDHCZK6CMVENZKEKK X-MailFrom: bounces+313651-b711-ruby-core=ml.ruby-lang.org@sendgrid.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list Reply-To: Ruby developers Subject: [ruby-core:113702] [Ruby master Misc#19122] Use MADV_DONTNEED instead of MADV_FREE when freeing a Fiber's stack List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: "smcgivern (Sean McGivern) via ruby-core" Cc: "smcgivern (Sean McGivern)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #19122 has been updated by smcgivern (Sean McGivern). ioquatix (Samuel Williams) wrote in #note-5: > If you want to use a specific mode (OS specific), you can do this: > > On Linux, find the mode, e.g. `MADV_DONTNEED = 6` (https://github.com/torvalds/linux/blob/933174ae28ba72ab8de5b35cb7c98fc211235096/arch/alpha/include/uapi/asm/mman.h#L50). > > Shift it one bit to the left (i.e. multiply by 2) `= 12`. > > Then run Ruby like this: > > ``` > > RUBY_SHARED_FIBER_POOL_FREE_STACKS=12 ruby > ``` Thanks for adding this! > As for making this the default, I suppose we could consider it but we'd need to confirm the performance impact. I would prefer to have high performance by default. RSS is a poor measurement IMHO. What about USS and PSS? Are they impacted the same way? Can we subtract the usage that can be later dropped? I mean, as I said in the original description, this is the only part of Ruby that works this way, which makes it surprising. If usage of fibers becomes more common in the wild, or `MADV_FREE` gets used in other areas where Ruby frees memory, then I would predict more reports noting this behaviour. Other languages have reverted their usage of `MADV_FREE` for similar reasons. We can construct another metric that gives us exactly what we want (for cAdvisor, we'd need something like https://github.com/google/cadvisor/issues/3197), but not through PSS or USS; both of those track RSS closely here, at least in my tests. We need to look at either `active_anon + inactive_anon` (for a cgroups `memory.stat` file), or `LazyFree` plus some other things I'm not sure about (for a process's `smaps_rollup`). This won't be the default in most monitoring tools, or in `top` et al, so as I said above, that wouldn't necessarily stop issues coming in. ---------------------------------------- Misc #19122: Use MADV_DONTNEED instead of MADV_FREE when freeing a Fiber's stack https://bugs.ruby-lang.org/issues/19122#change-103348 * Author: smcgivern (Sean McGivern) * Status: Open * Priority: Normal * Assignee: ioquatix (Samuel Williams) ---------------------------------------- I'd like to propose that Ruby stops using MADV_FREE when freeing a Fiber's stack, and switches to using MADV_DONTNEED even when MADV_FREE is supported. MADV_FREE is used in one place in the Ruby codebase, when freeing the stack of a freed Fiber: https://git.ruby-lang.org/ruby.git/tree/cont.c#n683 The comment for `fiber_pool_stack_free` says: ```c // We advise the operating system that the stack memory pages are no longer being used. // This introduce some performance overhead but allows system to relaim memory when there is pressure. ``` Where possible (i.e. on Linux 4.5 and later), `fiber_pool_stack_free` uses `MADV_FREE` over `MADV_DONTNEED`. This has the side effect that memory statistics such as RSS will not reduce until and unless the OS actually reclaims that memory. If that doesn't happen, then the reported memory usage via RSS will be much higher than the 'real' memory usage. If this was pervasive throughtout the Ruby codebase then that would be one thing, but currently this is just for Fiber. This means that: 1. A program that doesn't use Fiber will have somewhat reliable RSS statistics on recent Linux. 2. A program that heavily uses Fiber (such as something using Async::HTTP) will see an inflated RSS statistic. Go made a similar change to the one I'm proposing here for similar reasons: https://github.com/golang/go/issues/42330 > While `MADV_FREE` is somewhat faster than `MADV_DONTNEED`, it doesn't affect many of the statistics that `MADV_DONTNEED` does until the memory is actually reclaimed. This generally leads to poor user experience, like confusing stats in `top` and other monitoring tools; and bad integration with management systems that respond to memory usage. > [...] > I propose we change the default to prefer `MADV_DONTNEED` over `MADV_FREE`, to favor user-friendliness and minimal surprise over performance. I think it's become clear that Linux's implementation of `MADV_FREE` ultimately doesn't meet our needs. As an aside, MADV_FREE was not used in Ruby 3.1 (https://bugs.ruby-lang.org/issues/19101), and I haven't found any bugs filed about this behaviour other than that one. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/