Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124568 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 9656E1A00B7 for ; Wed, 24 Jul 2024 11:54:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1721822157; bh=3a4xLM6tsE3MBhsY/cHLDKV9A/UehsdlpqtKYVUO0WM=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=maTJW7ZSRjFa724rxwRi7t/DHztajRwmNuSFlOyhAO6TTu3nuJdO/U0oCqkGxpN2Z yG6qh9NQjprfqRQGr8U5wJ3J7bSelG63AnbqxNHrd0nQcDGG+8FYF0IG9HVmaQJKTh a/9HC7p2dNw1dohdQEqyAr6Nq76OQt9QHM44FHU1TMUCCdwzfyB/TmM205wmEquNkI lS0hGLAYs0xbJRRoR4QLT2SS72xLAkKPQcOPUUj9HCKhGQgCvM25Ev/H6LlxMDcZNz JMCBViJYsiXey17XyOnJYY/yWei/LgYYrzDi/r0jHsM4slBn+vJduopXLy61lIBu5n 6CI5ci5KIAJ2g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CDD7D180056 for ; Wed, 24 Jul 2024 11:55:56 +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_PASS,FREEMAIL_FROM, 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-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 ; Wed, 24 Jul 2024 11:55:56 +0000 (UTC) Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-70d1d6369acso580386b3a.0 for ; Wed, 24 Jul 2024 04:54:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721822061; x=1722426861; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=3a4xLM6tsE3MBhsY/cHLDKV9A/UehsdlpqtKYVUO0WM=; b=XN95UMvblp4qvIor+k6/9hWzY9aNXubcAenpX9RWo6ftUwiKDa5VduVSDVIZ414inx 3T+NQuuAt5x7TgzRL+eW97GP2tT+V54gugsHYGOrCu2zVWXQmaqwfjnuEoyvxi780Uld zEpJS88TVkVYjtkGEtDq4x/AsSYO1mrZTf9AjArlWgfLxUAlQPBgztNTHBAUaUlneavZ e0qdQwhfA6Kh8a2DVmUpWv8AlCD5AdEN0nD+qmRNutRjLZQvXMvkk7e0NnRXwVS3IG4n cZkHPxYuouGye92auRPmb1snQ40A1dqjE4Hea6IZTmOFEzOyh4xfVyhgZJen86DcdhYB ooiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721822061; x=1722426861; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3a4xLM6tsE3MBhsY/cHLDKV9A/UehsdlpqtKYVUO0WM=; b=xRGahKwRL3n7NnEs9VUKoTAcl26m2dSV+aPkWaf+VlEQAol9+m5k5o5Usa7VLA+1ut K+K/n3G/XUYsf0S80WyZKgGsYgCGpis1sVDDi9jIeRUmVvkpNv+gBLmQtN9LqsKkUqGc 1YH70CyMQ3ZWVodu+/Xk3XLrMOa1brNA9o/T5LYILgDaNRSMdC25wT//nVVxd76G3fbq 8EYQ4I/jRsplEpRFa3YxDK6KEI/7ExvcAu+2yF/caUA6R9djIgtmgnqtJ4ZT0fbweObs SOh3bk2vy6RV9HsMulpr7HP1x0+/IEe+hipsF/DkvJhG/u6emGEn2waSlWG4axjlmAXD wl8A== X-Gm-Message-State: AOJu0YyKeMwy2FM5WfnnXuy4Tfu4d5/8zrkijY/OfhkSt5i1goo44GaA uLJvpbhf9lm/T1CuqyadrKbbV8tmIjgYYLRixBeN4TrZcovd6dRvZstwZS7HXRT+3kKpvC0dQMo spjizJfcc+5cMYYz0R8nGHM2UL36PNoYf X-Google-Smtp-Source: AGHT+IEGAyiMTYdg36lD2d6F1E7k5PXYCsDYiSyfQjxVDWzloRuRR5jsgpNFNQD90sKxK0SYgjk7UPEztg0QdigJaso= X-Received: by 2002:a17:90b:4ac7:b0:2c2:d590:808e with SMTP id 98e67ed59e1d1-2cdb93d68bamr2655568a91.13.1721822060635; Wed, 24 Jul 2024 04:54:20 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <9828b776-660f-44ad-91df-11bea14230a4@app.fastmail.com> In-Reply-To: <9828b776-660f-44ad-91df-11bea14230a4@app.fastmail.com> Date: Wed, 24 Jul 2024 18:54:09 +0700 Message-ID: Subject: Re: [PHP-DEV] tsrm question To: Rob Landers Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: pierre.php@gmail.com (Pierre Joye) Hi Rob, On Wed, Jul 24, 2024 at 3:17=E2=80=AFPM Rob Landers wro= te: > > Hello internals, > > Last night I went down a rabbit hole with Frankenphp: https://github.com/= dunglas/frankenphp/pull/933 > > It isn't 100% clear to me what values `ts_resource(id)` holds and if it n= eeds to be freed/allocated per request or per thread. The performance impac= t is huge to reallocate on every request (mostly due to the global mutex du= ring allocation). Is anyone familiar with this and could help reviewing the= changes there? I suppose you already looked, but if not, maybe it gives you some direction= (s). The key part is kind of documented here: https://github.com/php/php-src/blob/8e93eb2e79cea5fcca6769b46a429de042660da= 9/TSRM/TSRM.c#L417 tsrm knows nothing about requests but only threads.Its jobs are about threads data. A good start (surely already looked) are apache and litespeed (or embed) for php8, however besides the API names change, the behavior or goals of each ts_ or tsrm_ functions remain the same as what we had with 5.x. One with a very similar modus operandi than Franken was ISAPI for IIS. Mind the names changes, dapt to php8's new names (and some behaviors) but the basics and core flows have been kept. If you know (~) how many threads franken is going to need, tsrm allows you to preallocate the rsc slots using tsrm_startup_ex. It uses tsrm_startup, a more powerful version, which lets franken define how many rsc per thread will be preallocated. I suppose that could help a bit performance wise: https://github.com/php/php-src/blob/8e93eb2e79cea5fcca6769b46a429de042660da= 9/main/main.c#L2716C13-L2716C25 As per the last question, about when to free them: tsrm_shutdown (franken shutdown) will free all rsc When a thread is detached (franken kills it for example), ts_free_thread needs to be called to kill its rsc. They will be freed anyway too but good to be clean. The tsrm resource management will also automatically purge orphan rsrc (f.e. when a thread dies unexpectedly and ts_free_thread could not be called). Long story short, the TSRM API is very flexible, how and when you alloc/free rsrc is basically up to you. It is possible to keep some around in the root thread (and be used in other threads, given the root thread id is known (can be 0 or else depending how franken managed them. I hope it helps and did not say too many outdated/wrong explanations :). Btw, Welting rewrote that part with Dmitry back then to fix long standing issues (and drop TSRM_LS/DC/CC uses), not sure if he is still around but he may help. Best, -- Pierre @pierrejoye | http://www.libgd.org