Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126226 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 243DC1A00BC for ; Tue, 28 Jan 2025 01:14:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1738026675; bh=rIpphpk0TAzhYv1zKBg8K5k6x3xXsFC+B/PzXPDAneI=; h=From:Date:Subject:To:From; b=TOj2xaSLQLBuhGwlMEEvnYOuanhabYHhwrKvke8eBXTPTS+GiyMFXeulapF19aGDc O0jdJaQkQRfyDOxXRHJST2tEc1BSVh+VFlqXeBFLxao3YrXo9PqFWhhoe+k51Bscbs Po+Xo8L1qrCbDaqa289wWKFB73zSkzHeYKqJ181WoTMblvZOPz8795kGDgxabbZKDl 04qWSJn6YSHUZ1uenp55o36ZknFK5LTsj+1wMduiiSL5CTm9chea0TJfKt4o1Lj5MZ 1YPZ4/6j53AoFF5sLdCj2hsDVFf8kreMn7y1q8xAZZoNRlRwl9hGeDk/nCk/29EVPT UraGjNfsWvIjw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 428C518007C for ; Tue, 28 Jan 2025 01:11:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-qt1-f180.google.com (mail-qt1-f180.google.com [209.85.160.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 28 Jan 2025 01:11:13 +0000 (UTC) Received: by mail-qt1-f180.google.com with SMTP id d75a77b69052e-46c7855df10so84043571cf.3 for ; Mon, 27 Jan 2025 17:14:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ethaniel.com; s=google; t=1738026841; x=1738631641; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=fk7tOkx5sqma0sr5IHFKzQI0J4+rPyOlSJIzXkspXno=; b=DtHjg8YAwNJKavnDqe7uk1EB/PtyPRGsttgs+rGsNZ0AdHHVJN6JV2TzjMAR3ZwpPh TtsuG+vdgCaMeVgruFNfhEcE2IG0jhVs0fzgDEKXCTZMuS9KMbotYLgHdjbIkIbUU9dD i2mIaSw0ZSpvFpUo7ZGBtcvG3SA4DbCXmWdNQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738026841; x=1738631641; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=fk7tOkx5sqma0sr5IHFKzQI0J4+rPyOlSJIzXkspXno=; b=oOStR6AKGbWfZTiHbetLT9EpBfCtjozaOOyG/npMw+h9X4ok7hKoZXS4L3BbdDizVY th16C9awJEqhDjai4lbEwvs4aOSVL1Ws3cuHnZSpFsruBISXXdpbhxkTQIZ1Nczs12tq uimD0d3Fn9GhJR7YSft3P/01tB64UD32WkbC+QLrlKiMrumqVgGk9/8L1OuXeY8CPZwx 6x+Dt0CI/RGdPw+RaNE4BkoDoeb8gE0pGhz69/EkCp5XLPxTeF13mpEeHfH87ajt5oh0 1XHATbvy1C6Ky6gBUzLpBb96qP2ij9PxxuVxkTKB9VIsUKscC4sAbykmcwLB+J7u3P3c Un8w== X-Gm-Message-State: AOJu0YwShcqMBfaSExq67RNQzKLHBDw81KZNdSVu/fgbsfkLJoE4UgWY 5lpkWACjSfnonGgDXF+R5cFJ9HmToTFPC4N/4Li+xdHCYp09TtZNF+cqEvTKu1Nuwu+TsetKgfo hqH/w4FYEdcSfEs2P/m7Y3zQUUyGU6czxn1OL10WQvvuFDfrO0ZYH X-Gm-Gg: ASbGnctJ0n40o7FANqW/uWFBR/nOVGpdWpJaqkY3IROlNWF5bWOkEoa2CaZK25SMMAo 09C9i74jw2xQwxnuVg/fNtYfJc/bJ3u1VELP1b99sUYlLAKq8lJVNLTwuPQlJgb0T X-Google-Smtp-Source: AGHT+IFbdLffHKIVpv/Ttgu9EerzrJ/EJAmfuBYmPYE8xtQFfqBInwHSQ6E0LBkg8yOiCIXiQ1/jIaBzpK/byYrGpso= X-Received: by 2002:ac8:5a4e:0:b0:462:c14f:d13f with SMTP id d75a77b69052e-46e12b9659amr703578611cf.41.1738026841417; Mon, 27 Jan 2025 17:14:01 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Mon, 27 Jan 2025 19:13:35 -0600 X-Gm-Features: AWEUYZn5-nP-0blM58B3LXl-RQ7xqm9vmDHivrlpek_oqYrxGobYh8VMYePQp2Q Message-ID: Subject: [PHP-DEV] [RFC] Introducing pm.max_memory for PHP-FPM To: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000007e3c1a062cb9e8be" From: eth@ethaniel.com (Arkadiy Kulev) --0000000000007e3c1a062cb9e8be Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello everyone, I=E2=80=99d like to propose adding a new configuration directive, tentative= ly called *pm.max_memory*, to PHP-FPM. This directive would allow administrators to specify a per-child memory usage limit, after which the PHP-FPM child process would gracefully exit or restart. Background and Rationale In many production environments, especially those handling long-running or memory-intensive processes, *memory leaks* (whether in extensions, libraries, or user code) can slowly accumulate. We already have tools like pm.max_requests that recycle processes after a certain number of requests, but there are scenarios in which memory usage might skyrocket within fewer requests=E2=80=94or memory might slowly climb in a way that doesn=E2=80=99t= align well with request counts alone. A setting like pm.max_memory could: 1. *Mitigate slow leaks:* By enforcing a hard memory cap at the process level, ensuring no single worker balloons in size over time. 2. *Provide more granular control:* If certain scripts or pools are known to be memory-intensive, an admin could set a memory limit that=E2= =80=99s appropriate for that usage pattern, rather than tying it only to request counts. 3. *Complement system-level controls:* While cgroups and container memory limits exist, a built-in FPM mechanism can be friendlier than a system OOM kill, which might be abrupt and less predictable. Proposed Behavior - A new config directive, *pm.max_memory*, which, if set to a value above 0, indicates the maximum amount of RAM (in bytes) a PHP-FPM worker process is allowed to use (resident set size or a similar metric). - After handling a request, each worker would check its own memory usage. If it exceeds the threshold, it would gracefully exit or be terminated by the master process before picking up a new request. - By default, this setting could be *disabled* (pm.max_memory =3D 0), so it does not affect existing installations. Implementation Details and Challenges I am not proposing to implement this feature myself=E2=80=94I=E2=80=99m mor= e of a sysadmin and don=E2=80=99t have the necessary C knowledge to patch php-src. However,= here are some thoughts on what the implementation might involve: 1. *Measuring memory usage:* Likely via getrusage(), mallinfo() (on some platforms), or reading from /proc/self/statm on Linux. 2. *Graceful shutdown logic:* Ensuring no ongoing requests are abruptly killed, or at least minimizing the chance of partial request handling. 3. *Platform differences:* Some OSes might provide different APIs for measuring process memory usage. We=E2=80=99d need consistent cross-platf= orm behavior or documented differences. 4. *Interaction with pm.max_requests:* If both are set, a worker would exit on whichever limit it hits first (memory or request count). Alternatives - *Using pm.max_requests:* Currently the main workaround to mitigate leaks, but it=E2=80=99s less precise and can=E2=80=99t handle large memo= ry spikes that happen quickly. - *System-level OOM or cgroups:* This approach can kill the entire pool or container, which is often more disruptive than recycling a single wor= ker. Request for Feedback I=E2=80=99m posting this proposal to gather feedback on feasibility and int= erest. If there=E2=80=99s enough support, I=E2=80=99d be happy to collaborate with= anyone who can handle the technical side=E2=80=94writing up a formal RFC on the wiki or wo= rking on a patch. If there=E2=80=99s a consensus that this is better handled elsewhe= re (system-level or container-level controls), or that pm.max_requests is sufficient, please let me know your thoughts. *Key questions*: 1. Would a built-in memory cap be beneficial for a significant subset of PHP-FPM users? 2. Are there any major technical hurdles or objections to this approach? 3. Does anyone have suggestions on how best to measure memory usage accurately and portably across different platforms? Thank you for reading and considering this idea. I look forward to hearing your insights and am happy to clarify or discuss any aspect of this proposal further. Best regards, Sincerely, Arkadiy Kulev (Ark) --0000000000007e3c1a062cb9e8be Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Hello everyone,

I=E2=80=99d like to propose = adding a new configuration directive, tentatively called pm.m= ax_memory, to PHP-FPM. This directive would allow administr= ators to specify a per-child memory usage limit, after which the PHP-FPM ch= ild process would gracefully exit or restart.

Background and Rationa= le

In many production environments, especially those handling long-r= unning or memory-intensive processes, memory leaks (whethe= r in extensions, libraries, or user code) can slowly accumulate. We already= have tools like pm.max_requests that recycle processes after = a certain number of requests, but there are scenarios in which memory usage= might skyrocket within fewer requests=E2=80=94or memory might slowly climb= in a way that doesn=E2=80=99t align well with request counts alone.

= A setting like pm.max_memory could:

  1. Mitigat= e slow leaks: By enforcing a hard memory cap at the process level,= ensuring no single worker balloons in size over time.
  2. Prov= ide more granular control: If certain scripts or pools are known t= o be memory-intensive, an admin could set a memory limit that=E2=80=99s app= ropriate for that usage pattern, rather than tying it only to request count= s.
  3. Complement system-level controls: While cgroups= and container memory limits exist, a built-in FPM mechanism can be friendl= ier than a system OOM kill, which might be abrupt and less predictable.

Proposed Behavior

  • A new config directive, pm.max_memory, which, if set to a value above 0, indicates the maximum amount of RAM (in bytes) a PHP-FPM worker proce= ss is allowed to use (resident set size or a similar metric).
  • After= handling a request, each worker would check its own memory usage. If it ex= ceeds the threshold, it would gracefully exit or be terminated by the maste= r process before picking up a new request.
  • By default, this setting= could be disabled (pm.max_memory =3D 0), so = it does not affect existing installations.

Implementation Deta= ils and Challenges

I am not proposing to implement this feature myse= lf=E2=80=94I=E2=80=99m more of a sysadmin and don=E2=80=99t have the necess= ary C knowledge to patch php-src. However, here are some thoug= hts on what the implementation might involve:

  1. Measuring = memory usage: Likely via getrusage(), mallinfo(= ) (on some platforms), or reading from /proc/self/statm= on Linux.
  2. Graceful shutdown logic: Ensuring no on= going requests are abruptly killed, or at least minimizing the chance of pa= rtial request handling.
  3. Platform differences: Some= OSes might provide different APIs for measuring process memory usage. We= =E2=80=99d need consistent cross-platform behavior or documented difference= s.
  4. Interaction with pm.max_requests: = If both are set, a worker would exit on whichever limit it hits first (memo= ry or request count).

Alternatives

  • Using <= code>pm.max_requests: Currently the main workaround to miti= gate leaks, but it=E2=80=99s less precise and can=E2=80=99t handle large me= mory spikes that happen quickly.
  • System-level OOM or cgroup= s: This approach can kill the entire pool or container, which is o= ften more disruptive than recycling a single worker.

Request f= or Feedback

I=E2=80=99m posting this proposal to gather feedback on = feasibility and interest. If there=E2=80=99s enough support, I=E2=80=99d be= happy to collaborate with anyone who can handle the technical side=E2=80= =94writing up a formal RFC on the wiki or working on a patch. If there=E2= =80=99s a consensus that this is better handled elsewhere (system-level or = container-level controls), or that pm.max_requests is sufficie= nt, please let me know your thoughts.

Key questions:=

  1. Would a built-in memory cap be beneficial for a significant sub= set of PHP-FPM users?
  2. Are there any major technical hurdles or obje= ctions to this approach?
  3. Does anyone have suggestions on how best t= o measure memory usage accurately and portably across different platforms?<= /li>

Thank you for reading and considering this idea. I look forward= to hearing your insights and am happy to clarify or discuss any aspect of = this proposal further.

Best regards,

Sincerely,
Arkadiy= Kulev (Ark)
--0000000000007e3c1a062cb9e8be--