From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id C7F6F3858C2C for ; Mon, 17 Apr 2023 12:08:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C7F6F3858C2C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gnu.org Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1poNey-0000Ng-KC; Mon, 17 Apr 2023 08:08:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=In-Reply-To:MIME-Version:References:Subject:To:From: Date; bh=bTX3eVGtWN2JaxNhXH6U1P3350T4AIkf9xhEN2qUJuU=; b=fAAbOGkTQOniCWJyLbw5 2snt5HOwK9GEcvXNRRLsl2qe1QyjYrWvEf+uk8bUSJ29PQKmFcwaFIZkbv2GZ2BvIX20PrKo0lXI6 S0MIifhtiVdHsfdOLypLSBtfmLx2MCct5Ym1KkYwe00s6j+Lra2dW+QEFh6mq4TyOk4WW44vZCITC sZqXRiXGacwwWqM+I4tV+GSRkk+6lx02WIW3HtTNaqhP3H8CY2G6Sy+E5dknScMEHXuSb83bljriZ Psqo9n8sW5yUK7nnNXnvsFbzywtLancSZ+gnEZ7pZ6btTo6QE/sEw5TSZFsYdSfs3JO9ER7zxhkI3 RGTJOp3OQHfxUQ==; Received: from 2a01cb008c251f00de41a9fffe47ec49.ipv6.abo.wanadoo.fr ([2a01:cb00:8c25:1f00:de41:a9ff:fe47:ec49] helo=begin.home) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1poNey-0002uA-0S; Mon, 17 Apr 2023 08:08:40 -0400 Received: from samy by begin.home with local (Exim 4.96) (envelope-from ) id 1poNeu-0050it-3C; Mon, 17 Apr 2023 14:08:36 +0200 Date: Mon, 17 Apr 2023 14:08:36 +0200 From: Samuel Thibault To: Sergey Bugaev Cc: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: Re: [PATCH 1/1] hurd: Avoid extra ctty RPCs in init_dtable () Message-ID: <20230417120836.jcjkpjzhvmpqceie@begin> Mail-Followup-To: Sergey Bugaev , libc-alpha@sourceware.org, bug-hurd@gnu.org References: <20230415161718.80668-1-bugaevc@gmail.com> <20230415161718.80668-2-bugaevc@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230415161718.80668-2-bugaevc@gmail.com> Organization: I am not organized User-Agent: NeoMutt/20170609 (1.8.3) X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_PASS,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Applied, thanks! Sergey Bugaev, le sam. 15 avril 2023 19:17:18 +0300, a ecrit: > It is common to have (some of) stdin, stdout and stderr point to the > very same port. We were making the ctty RPCs that _hurd_port2fd () does > for each one of them separately: > > 1. term_getctty () > 2. mach_port_deallocate () > 3. term_open_ctty () > > Instead, let's detect this case and duplicate the ctty port we already > have. This means we do 1 RPC instead of 3 (and create a single protid > on the server side) if the file is our ctty, and no RPCs instead of 1 > if it's not. A clear win! > > Signed-off-by: Sergey Bugaev > --- > hurd/dtable.c | 46 +++++++++++++++++++++++++++++++++++++++------- > 1 file changed, 39 insertions(+), 7 deletions(-) > > diff --git a/hurd/dtable.c b/hurd/dtable.c > index 5dd04131..41f4d7af 100644 > --- a/hurd/dtable.c > +++ b/hurd/dtable.c > @@ -60,18 +60,50 @@ init_dtable (void) > _hurd_dtable[i] = NULL; > else > { > + int copy; > /* Allocate a new file descriptor structure. */ > struct hurd_fd *new = malloc (sizeof (struct hurd_fd)); > if (new == NULL) > __libc_fatal ("hurd: Can't allocate initial file descriptors\n"); > > - /* Initialize the port cells. */ > - _hurd_port_init (&new->port, MACH_PORT_NULL); > - _hurd_port_init (&new->ctty, MACH_PORT_NULL); > - > - /* Install the port in the descriptor. > - This sets up all the ctty magic. */ > - _hurd_port2fd (new, _hurd_init_dtable[i], 0); > + /* See if this file descriptor is the same as a previous one we have > + already installed. In this case, we can just copy over the same > + ctty port without making any more RPCs. We only check the the > + immediately preceding fd and fd 0 -- this should be enough to > + handle the common cases while not requiring quadratic > + complexity. */ > + if (i > 0 && _hurd_init_dtable[i] == _hurd_init_dtable[i - 1]) > + copy = i - 1; > + else if (i > 0 && _hurd_init_dtable[i] == _hurd_init_dtable[0]) > + copy = 0; > + else > + copy = -1; > + > + if (copy < 0) > + { > + /* Initialize the port cells. */ > + _hurd_port_init (&new->port, MACH_PORT_NULL); > + _hurd_port_init (&new->ctty, MACH_PORT_NULL); > + > + /* Install the port in the descriptor. > + This sets up all the ctty magic. */ > + _hurd_port2fd (new, _hurd_init_dtable[i], 0); > + } > + else > + { > + /* Copy over ctty from the already set up file descriptor that > + contains the same port. We can access the contents of the > + cell without any locking since no one could have seen it > + yet. */ > + mach_port_t ctty = _hurd_dtable[copy]->ctty.port; > + > + if (MACH_PORT_VALID (ctty)) > + __mach_port_mod_refs (__mach_task_self (), ctty, > + MACH_PORT_RIGHT_SEND, +1); > + > + _hurd_port_init (&new->port, _hurd_init_dtable[i]); > + _hurd_port_init (&new->ctty, ctty); > + } > > _hurd_dtable[i] = new; > } > -- > 2.39.2 > -- Samuel --- Pour une évaluation indépendante, transparente et rigoureuse ! Je soutiens la Commission d'Évaluation de l'Inria.