From owner-svn-src-head@freebsd.org Wed Jul 4 13:31:17 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id B502D10359D7; Wed, 4 Jul 2018 13:31:17 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 689987AA33; Wed, 4 Jul 2018 13:31:17 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4A2693C25; Wed, 4 Jul 2018 13:31:17 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w64DVHwY007498; Wed, 4 Jul 2018 13:31:17 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w64DVG5U007495; Wed, 4 Jul 2018 13:31:16 GMT (envelope-from kib@FreeBSD.org) Message-Id: <201807041331.w64DVG5U007495@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Wed, 4 Jul 2018 13:31:16 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r335939 - head/lib/libc/gen X-SVN-Group: head X-SVN-Commit-Author: kib X-SVN-Commit-Paths: head/lib/libc/gen X-SVN-Commit-Revision: 335939 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Jul 2018 13:31:18 -0000 Author: kib Date: Wed Jul 4 13:31:16 2018 New Revision: 335939 URL: https://svnweb.freebsd.org/changeset/base/335939 Log: Add setproctitle_fast(3) for frequent callers. Some applications, notably PostgreSQL, want to call setproctitle() very often. It's slow. Provide an alternative cheap way of updating process titles without making any syscalls, instead requiring other processes (top, ps etc) to do a bit more work to retrieve the data. This uses a pre-existing code path inherited from ancient BSD, which always did it that way. Submitted by: Thomas Munro MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D16111 Modified: head/lib/libc/gen/Symbol.map head/lib/libc/gen/setproctitle.3 head/lib/libc/gen/setproctitle.c Modified: head/lib/libc/gen/Symbol.map ============================================================================== --- head/lib/libc/gen/Symbol.map Wed Jul 4 13:28:43 2018 (r335938) +++ head/lib/libc/gen/Symbol.map Wed Jul 4 13:31:16 2018 (r335939) @@ -419,6 +419,7 @@ FBSD_1.5 { scandir; scandir_b; sem_clockwait_np; + setproctitle_fast; }; FBSDprivate_1.0 { Modified: head/lib/libc/gen/setproctitle.3 ============================================================================== --- head/lib/libc/gen/setproctitle.3 Wed Jul 4 13:28:43 2018 (r335938) +++ head/lib/libc/gen/setproctitle.3 Wed Jul 4 13:31:16 2018 (r335939) @@ -20,7 +20,7 @@ .\" $FreeBSD$ .\" .\" The following requests are required for all man pages. -.Dd December 16, 1995 +.Dd July 4, 2018 .Dt SETPROCTITLE 3 .Os .Sh NAME @@ -31,12 +31,20 @@ .In unistd.h .Ft void .Fn setproctitle "const char *fmt" "..." +.Ft void +.Fn setproctitle_fast "const char *fmt" "..." .Sh DESCRIPTION The .Fn setproctitle library routine sets the process title that appears on the .Xr ps 1 command. +The +.Fn setproctitle_fast +variant is optimized for high frequency updates, but may make the +.Xr ps 1 +command slightly slower by not updating the kernel cache of the program +arguments. .Pp The title is set from the executable's name, followed by the result of a @@ -96,6 +104,10 @@ The function first appeared in .Fx 2.2 . +The +.Fn setproctitle_fast +function first appeared in +.Fx 12 . Other operating systems have similar functions. .Sh AUTHORS Modified: head/lib/libc/gen/setproctitle.c ============================================================================== --- head/lib/libc/gen/setproctitle.c Wed Jul 4 13:28:43 2018 (r335938) +++ head/lib/libc/gen/setproctitle.c Wed Jul 4 13:31:16 2018 (r335939) @@ -55,8 +55,8 @@ struct old_ps_strings { #define SPT_BUFSIZE 2048 /* from other parts of sendmail */ -void -setproctitle(const char *fmt, ...) +static char * +setproctitle_internal(const char *fmt, va_list ap) { static struct ps_strings *ps_strings; static char *buf = NULL; @@ -67,27 +67,23 @@ setproctitle(const char *fmt, ...) char **nargvp; int nargc; int i; - va_list ap; size_t len; unsigned long ul_ps_strings; - int oid[4]; if (buf == NULL) { buf = malloc(SPT_BUFSIZE); if (buf == NULL) - return; + return (NULL); nargv[0] = buf; } if (obuf == NULL ) { obuf = malloc(SPT_BUFSIZE); if (obuf == NULL) - return; + return (NULL); *obuf = '\0'; } - va_start(ap, fmt); - if (fmt) { buf[SPT_BUFSIZE - 1] = '\0'; @@ -114,22 +110,13 @@ setproctitle(const char *fmt, ...) kbuf = obuf; } else /* Nothing to restore */ - return; + return (NULL); - va_end(ap); - - /* Set the title into the kernel cached command line */ - oid[0] = CTL_KERN; - oid[1] = KERN_PROC; - oid[2] = KERN_PROC_ARGS; - oid[3] = getpid(); - sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1); - if (ps_strings == NULL) { len = sizeof(ul_ps_strings); if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL, 0) == -1) - return; + return (NULL); ps_strings = (struct ps_strings *)ul_ps_strings; } @@ -138,7 +125,7 @@ setproctitle(const char *fmt, ...) * Should not happen. */ if (ps_strings->ps_argvstr == NULL) - return; + return (NULL); /* style #3 */ if (oargc == -1) { @@ -167,4 +154,52 @@ setproctitle(const char *fmt, ...) } ps_strings->ps_nargvstr = nargc; ps_strings->ps_argvstr = nargvp; + + return (nargvp[0]); +} + +static int fast_update = 0; + +void +setproctitle_fast(const char *fmt, ...) +{ + va_list ap; + char *buf; + int oid[4]; + + va_start(ap, fmt); + buf = setproctitle_internal(fmt, ap); + va_end(ap); + + if (buf && !fast_update) { + /* Tell the kernel to start looking in user-space */ + oid[0] = CTL_KERN; + oid[1] = KERN_PROC; + oid[2] = KERN_PROC_ARGS; + oid[3] = getpid(); + sysctl(oid, 4, 0, 0, "", 0); + fast_update = 1; + } +} + +void +setproctitle(const char *fmt, ...) +{ + va_list ap; + char *buf; + int oid[4]; + + va_start(ap, fmt); + buf = setproctitle_internal(fmt, ap); + va_end(ap); + + if (buf != NULL) { + /* Set the title into the kernel cached command line */ + oid[0] = CTL_KERN; + oid[1] = KERN_PROC; + oid[2] = KERN_PROC_ARGS; + oid[3] = getpid(); + sysctl(oid, 4, 0, 0, buf, strlen(buf) + 1); + fast_update = 0; + } }