public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Samuel Thibault <samuel.thibault@gnu.org>
To: Sergey Bugaev <bugaevc@gmail.com>
Cc: libc-alpha@sourceware.org, bug-hurd@gnu.org
Subject: Re: [PATCH 1/1] hurd: Avoid extra ctty RPCs in init_dtable ()
Date: Mon, 17 Apr 2023 14:08:36 +0200	[thread overview]
Message-ID: <20230417120836.jcjkpjzhvmpqceie@begin> (raw)
In-Reply-To: <20230415161718.80668-2-bugaevc@gmail.com>

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 <bugaevc@gmail.com>
> ---
>  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.

  reply	other threads:[~2023-04-17 12:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-15 16:17 [PATCH 0/1] Let's improve/fix ccty handling Sergey Bugaev
2023-04-15 16:17 ` [PATCH 1/1] hurd: Avoid extra ctty RPCs in init_dtable () Sergey Bugaev
2023-04-17 12:08   ` Samuel Thibault [this message]
2023-04-15 17:58 ` [PATCH 0/1] Let's improve/fix ccty handling Samuel Thibault
2023-04-15 19:08   ` [PATCH] hurd: Run init_pids () before init_dtable () Sergey Bugaev
2023-04-17 21:05     ` Samuel Thibault
2023-04-17 12:14 ` [PATCH 0/1] Let's improve/fix ccty handling Samuel Thibault

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230417120836.jcjkpjzhvmpqceie@begin \
    --to=samuel.thibault@gnu.org \
    --cc=bug-hurd@gnu.org \
    --cc=bugaevc@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).