From owner-svn-src-stable@freebsd.org Wed Jul 18 09:54:34 2018 Return-Path: Delivered-To: svn-src-stable@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 1E61F1041E24; Wed, 18 Jul 2018 09:54:34 +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 C59F672616; Wed, 18 Jul 2018 09:54:33 +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 A6E1918A59; Wed, 18 Jul 2018 09:54:33 +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 w6I9sXvQ076532; Wed, 18 Jul 2018 09:54:33 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w6I9sWWd076529; Wed, 18 Jul 2018 09:54:32 GMT (envelope-from kib@FreeBSD.org) Message-Id: <201807180954.w6I9sWWd076529@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Wed, 18 Jul 2018 09:54:32 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r336449 - in stable/11: include lib/libc/gen X-SVN-Group: stable-11 X-SVN-Commit-Author: kib X-SVN-Commit-Paths: in stable/11: include lib/libc/gen X-SVN-Commit-Revision: 336449 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Jul 2018 09:54:34 -0000 Author: kib Date: Wed Jul 18 09:54:32 2018 New Revision: 336449 URL: https://svnweb.freebsd.org/changeset/base/336449 Log: MFC r335939, r336088: Add setproctitle_fast(3) for frequent callers. Modified: stable/11/include/unistd.h stable/11/lib/libc/gen/Symbol.map stable/11/lib/libc/gen/setproctitle.3 stable/11/lib/libc/gen/setproctitle.c Directory Properties: stable/11/ (props changed) Modified: stable/11/include/unistd.h ============================================================================== --- stable/11/include/unistd.h Wed Jul 18 09:32:43 2018 (r336448) +++ stable/11/include/unistd.h Wed Jul 18 09:54:32 2018 (r336449) @@ -569,6 +569,7 @@ int setloginclass(const char *); void *setmode(const char *); int setpgrp(pid_t, pid_t); /* obsoleted by setpgid() */ void setproctitle(const char *_fmt, ...) __printf0like(1, 2); +void setproctitle_fast(const char *_fmt, ...) __printf0like(1, 2); int setresgid(gid_t, gid_t, gid_t); int setresuid(uid_t, uid_t, uid_t); int setrgid(gid_t); Modified: stable/11/lib/libc/gen/Symbol.map ============================================================================== --- stable/11/lib/libc/gen/Symbol.map Wed Jul 18 09:32:43 2018 (r336448) +++ stable/11/lib/libc/gen/Symbol.map Wed Jul 18 09:54:32 2018 (r336449) @@ -420,6 +420,7 @@ FBSD_1.4 { FBSD_1.5 { sem_clockwait_np; + setproctitle_fast; }; FBSDprivate_1.0 { Modified: stable/11/lib/libc/gen/setproctitle.3 ============================================================================== --- stable/11/lib/libc/gen/setproctitle.3 Wed Jul 18 09:32:43 2018 (r336448) +++ stable/11/lib/libc/gen/setproctitle.3 Wed Jul 18 09:54:32 2018 (r336449) @@ -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: stable/11/lib/libc/gen/setproctitle.c ============================================================================== --- stable/11/lib/libc/gen/setproctitle.c Wed Jul 18 09:32:43 2018 (r336448) +++ stable/11/lib/libc/gen/setproctitle.c Wed Jul 18 09:54:32 2018 (r336449) @@ -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; + } }