Remove stale proc_compare code and use the shared one in libutil.

This commit is contained in:
christos 2011-10-21 02:26:09 +00:00
parent c85216986a
commit 924cacedb8
4 changed files with 31 additions and 135 deletions

View File

@ -1,13 +1,13 @@
# $NetBSD: Makefile,v 1.20 2011/08/17 13:48:11 christos Exp $
# $NetBSD: Makefile,v 1.21 2011/10/21 02:26:09 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93
.include <bsd.own.mk>
PROG= w
SRCS= fmt.c pr_time.c proc_compare.c w.c
SRCS= fmt.c pr_time.c w.c
MAN= w.1 uptime.1
DPADD= ${LIBKVM}
LDADD= -lkvm
DPADD= ${LIBKVM} ${LIBUTIL}
LDADD= -lkvm -lutil
LINKS= ${BINDIR}/w ${BINDIR}/uptime
CPPFLAGS+= -DSUPPORT_UTMP -DSUPPORT_UTMPX

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.6 2003/08/07 11:17:12 agc Exp $ */
/* $NetBSD: extern.h,v 1.7 2011/10/21 02:26:09 christos Exp $ */
/*-
* Copyright (c) 1993
@ -36,4 +36,3 @@ void fmt_puts(char *, int *);
void fmt_putc(int, int *);
void pr_attime(time_t *, time_t *);
void pr_idle(time_t);
int proc_compare(struct kinfo_proc2 *, struct kinfo_proc2 *);

View File

@ -1,123 +0,0 @@
/* $NetBSD: proc_compare.c,v 1.14 2007/11/05 20:46:08 ad Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)proc_compare.c 8.2 (Berkeley) 9/23/93";
#else
__RCSID("$NetBSD: proc_compare.c,v 1.14 2007/11/05 20:46:08 ad Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <sys/sysctl.h>
#include "extern.h"
/*
* Returns 1 if p2 is "better" than p1
*
* The algorithm for picking the "interesting" process is thus:
*
* 1) Only foreground processes are eligible - implied.
* 2) Runnable processes are favored over anything else. The runner
* with the highest CPU utilization is picked (p_pctcpu). Ties are
* broken by picking the highest pid.
* 3) The sleeper with the shortest sleep time is next. With ties,
* we pick out just "short-term" sleepers (P_SINTR == 0).
* 4) Further ties are broken by picking the highest pid.
*
* If you change this, be sure to consider making the change in the kernel
* too (^T in kern/tty.c).
*
* TODO - consider whether pctcpu should be used.
*/
#define ISRUN(p) (((p)->p_stat == LSRUN) || ((p)->p_stat == SIDL) || \
((p)->p_stat == LSONPROC))
#define TESTAB(a, b) ((a)<<1 | (b))
#define ONLYA 2
#define ONLYB 1
#define BOTH 3
int
proc_compare(struct kinfo_proc2 *p1, struct kinfo_proc2 *p2)
{
if (p1 == NULL)
return (1);
/*
* see if at least one of them is runnable
*/
switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
case ONLYA:
return (0);
case ONLYB:
return (1);
case BOTH:
/*
* tie - favor one with highest recent CPU utilization
*/
if (p2->p_pctcpu > p1->p_pctcpu)
return (1);
if (p1->p_pctcpu > p2->p_pctcpu)
return (0);
return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
}
/*
* weed out zombies
*/
switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
case ONLYA:
return (1);
case ONLYB:
return (0);
case BOTH:
return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
}
/*
* pick the one with the smallest sleep time
*/
if (p2->p_slptime > p1->p_slptime)
return (0);
if (p1->p_slptime > p2->p_slptime)
return (1);
/*
* favor one sleeping in a non-interruptible sleep
*/
if (p1->p_flag & L_SINTR && (p2->p_flag & L_SINTR) == 0)
return (1);
if (p2->p_flag & L_SINTR && (p1->p_flag & L_SINTR) == 0)
return (0);
return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: w.c,v 1.75 2011/09/16 15:39:30 joerg Exp $ */
/* $NetBSD: w.c,v 1.76 2011/10/21 02:26:09 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993, 1994
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
#if 0
static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 6/30/94";
#else
__RCSID("$NetBSD: w.c,v 1.75 2011/09/16 15:39:30 joerg Exp $");
__RCSID("$NetBSD: w.c,v 1.76 2011/10/21 02:26:09 christos Exp $");
#endif
#endif /* not lint */
@ -120,6 +120,8 @@ struct entry {
static void pr_args(struct kinfo_proc2 *);
static void pr_header(time_t *, int);
static int proc_compare_wrapper(const struct kinfo_proc2 *,
const struct kinfo_proc2 *);
#if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
static int ttystat(const char *, struct stat *);
static void process(struct entry *);
@ -302,9 +304,6 @@ main(int argc, char **argv)
/* Include trailing space because TTY header starts one column early. */
for (i = 0; i < nentries; i++, kp++) {
if (kp->p_stat == SIDL || kp->p_stat == SZOMB)
continue;
for (ep = ehead; ep != NULL; ep = ep->next) {
if (ep->tdev != 0 && ep->tdev == kp->p_tdev &&
kp->p__pgid == kp->p_tpgid) {
@ -312,7 +311,7 @@ main(int argc, char **argv)
* Proc is in foreground of this
* terminal
*/
if (proc_compare(ep->tp, kp))
if (proc_compare_wrapper(ep->tp, kp))
ep->tp = kp;
break;
}
@ -618,6 +617,27 @@ process(struct entry *ep)
}
#endif
static int
proc_compare_wrapper(const struct kinfo_proc2 *p1,
const struct kinfo_proc2 *p2)
{
struct kinfo_lwp *l1, *l2;
int cnt;
if (p1 == NULL)
return 1;
l1 = kvm_getlwps(kd, p1->p_pid, 0, sizeof(*l1), &cnt);
if (l1 == NULL || cnt == 0)
return 1;
l2 = kvm_getlwps(kd, p2->p_pid, 0, sizeof(*l1), &cnt);
if (l2 == NULL || cnt == 0)
return 0;
return proc_compare(p1, l1, p2, l2);
}
static void
usage(int wcmd)
{