1*f14fb602SLionel Sambuc /* $NetBSD: proc_compare.c,v 1.1 2011/10/21 02:09:00 christos Exp $ */
2*f14fb602SLionel Sambuc
3*f14fb602SLionel Sambuc /*-
4*f14fb602SLionel Sambuc * Copyright (c) 1990, 1993
5*f14fb602SLionel Sambuc * The Regents of the University of California. All rights reserved.
6*f14fb602SLionel Sambuc *
7*f14fb602SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*f14fb602SLionel Sambuc * modification, are permitted provided that the following conditions
9*f14fb602SLionel Sambuc * are met:
10*f14fb602SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*f14fb602SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*f14fb602SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*f14fb602SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*f14fb602SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*f14fb602SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
16*f14fb602SLionel Sambuc * may be used to endorse or promote products derived from this software
17*f14fb602SLionel Sambuc * without specific prior written permission.
18*f14fb602SLionel Sambuc *
19*f14fb602SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*f14fb602SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*f14fb602SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*f14fb602SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*f14fb602SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*f14fb602SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*f14fb602SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*f14fb602SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*f14fb602SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*f14fb602SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*f14fb602SLionel Sambuc * SUCH DAMAGE.
30*f14fb602SLionel Sambuc */
31*f14fb602SLionel Sambuc #ifndef _STANDALONE
32*f14fb602SLionel Sambuc # ifndef _KERNEL
33*f14fb602SLionel Sambuc
34*f14fb602SLionel Sambuc # if HAVE_NBTOOL_CONFIG_H
35*f14fb602SLionel Sambuc # include "nbtool_config.h"
36*f14fb602SLionel Sambuc # endif
37*f14fb602SLionel Sambuc
38*f14fb602SLionel Sambuc # include <sys/cdefs.h>
39*f14fb602SLionel Sambuc # if defined(LIBC_SCCS) && !defined(lint)
40*f14fb602SLionel Sambuc __RCSID("$NetBSD: proc_compare.c,v 1.1 2011/10/21 02:09:00 christos Exp $");
41*f14fb602SLionel Sambuc # endif
42*f14fb602SLionel Sambuc
43*f14fb602SLionel Sambuc # include <sys/types.h>
44*f14fb602SLionel Sambuc # include <sys/inttypes.h>
45*f14fb602SLionel Sambuc # include <sys/sysctl.h>
46*f14fb602SLionel Sambuc # include <stdio.h>
47*f14fb602SLionel Sambuc # include <util.h>
48*f14fb602SLionel Sambuc # include <errno.h>
49*f14fb602SLionel Sambuc # define PROC struct kinfo_proc2
50*f14fb602SLionel Sambuc # define LWP struct kinfo_lwp
51*f14fb602SLionel Sambuc # define P_RTIME_SEC p_rtime_sec
52*f14fb602SLionel Sambuc # define P_RTIME_USEC p_rtime_usec
53*f14fb602SLionel Sambuc # else
54*f14fb602SLionel Sambuc # include <sys/cdefs.h>
55*f14fb602SLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: proc_compare.c,v 1.1 2011/10/21 02:09:00 christos Exp $");
56*f14fb602SLionel Sambuc # include <sys/param.h>
57*f14fb602SLionel Sambuc # include <sys/inttypes.h>
58*f14fb602SLionel Sambuc # include <sys/systm.h>
59*f14fb602SLionel Sambuc # include <sys/proc.h>
60*f14fb602SLionel Sambuc # include <sys/lwp.h>
61*f14fb602SLionel Sambuc # include <lib/libkern/libkern.h>
62*f14fb602SLionel Sambuc # define PROC struct proc
63*f14fb602SLionel Sambuc # define LWP struct lwp
64*f14fb602SLionel Sambuc # define P_RTIME_SEC p_rtime.sec
65*f14fb602SLionel Sambuc # define P_RTIME_USEC p_rtime.frac
66*f14fb602SLionel Sambuc # endif
67*f14fb602SLionel Sambuc /*
68*f14fb602SLionel Sambuc * Returns 1 if p2 is "better" than p1
69*f14fb602SLionel Sambuc *
70*f14fb602SLionel Sambuc * The algorithm for picking the "interesting" process is thus:
71*f14fb602SLionel Sambuc *
72*f14fb602SLionel Sambuc * 1) Only foreground processes are eligible - implied.
73*f14fb602SLionel Sambuc * 2) Runnable processes are favored over anything else. The runner
74*f14fb602SLionel Sambuc * with the highest CPU utilization is picked (l_pctcpu). Ties are
75*f14fb602SLionel Sambuc * broken by picking the highest pid.
76*f14fb602SLionel Sambuc * 3) The sleeper with the shortest sleep time is next. With ties,
77*f14fb602SLionel Sambuc * we pick out just "short-term" sleepers (P_SINTR == 0).
78*f14fb602SLionel Sambuc * 4) Further ties are broken by picking the one started last.
79*f14fb602SLionel Sambuc * 5) Finally the one with the biggest pid wins, but that is nonsense
80*f14fb602SLionel Sambuc * because of pid randomization.
81*f14fb602SLionel Sambuc */
82*f14fb602SLionel Sambuc #define ISRUN(p) ((p)->p_nrlwps > 0)
83*f14fb602SLionel Sambuc #define TESTAB(a, b) (((a) << 1) | (b))
84*f14fb602SLionel Sambuc #define ONLYA 2
85*f14fb602SLionel Sambuc #define ONLYB 1
86*f14fb602SLionel Sambuc #define BOTH 3
87*f14fb602SLionel Sambuc
88*f14fb602SLionel Sambuc int
proc_compare(const PROC * p1,const LWP * l1,const PROC * p2,const LWP * l2)89*f14fb602SLionel Sambuc proc_compare(const PROC *p1, const LWP *l1, const PROC *p2, const LWP *l2)
90*f14fb602SLionel Sambuc {
91*f14fb602SLionel Sambuc /*
92*f14fb602SLionel Sambuc * see if at least one of them is runnable
93*f14fb602SLionel Sambuc */
94*f14fb602SLionel Sambuc switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
95*f14fb602SLionel Sambuc case ONLYA:
96*f14fb602SLionel Sambuc return 0;
97*f14fb602SLionel Sambuc case ONLYB:
98*f14fb602SLionel Sambuc return 1;
99*f14fb602SLionel Sambuc case BOTH:
100*f14fb602SLionel Sambuc /*
101*f14fb602SLionel Sambuc * tie - favor one with highest recent CPU utilization
102*f14fb602SLionel Sambuc */
103*f14fb602SLionel Sambuc if (l2->l_pctcpu > l1->l_pctcpu)
104*f14fb602SLionel Sambuc return 1;
105*f14fb602SLionel Sambuc goto out;
106*f14fb602SLionel Sambuc }
107*f14fb602SLionel Sambuc /*
108*f14fb602SLionel Sambuc * weed out zombies
109*f14fb602SLionel Sambuc */
110*f14fb602SLionel Sambuc switch (TESTAB(P_ZOMBIE(p1), P_ZOMBIE(p2))) {
111*f14fb602SLionel Sambuc case ONLYA:
112*f14fb602SLionel Sambuc return 1;
113*f14fb602SLionel Sambuc case ONLYB:
114*f14fb602SLionel Sambuc return 0;
115*f14fb602SLionel Sambuc case BOTH:
116*f14fb602SLionel Sambuc goto out;
117*f14fb602SLionel Sambuc }
118*f14fb602SLionel Sambuc /*
119*f14fb602SLionel Sambuc * pick the one with the smallest sleep time
120*f14fb602SLionel Sambuc */
121*f14fb602SLionel Sambuc if (l1->l_slptime < l2->l_slptime)
122*f14fb602SLionel Sambuc return 0;
123*f14fb602SLionel Sambuc if (l2->l_slptime < l1->l_slptime)
124*f14fb602SLionel Sambuc return 1;
125*f14fb602SLionel Sambuc
126*f14fb602SLionel Sambuc /*
127*f14fb602SLionel Sambuc * favor one sleeping in a non-interruptible sleep
128*f14fb602SLionel Sambuc */
129*f14fb602SLionel Sambuc if ((l1->l_flag & LW_SINTR) && (l2->l_flag & LW_SINTR) == 0)
130*f14fb602SLionel Sambuc return 0;
131*f14fb602SLionel Sambuc if ((l2->l_flag & LW_SINTR) && (l1->l_flag & LW_SINTR) == 0)
132*f14fb602SLionel Sambuc return 1;
133*f14fb602SLionel Sambuc out:
134*f14fb602SLionel Sambuc /* tie, return the one with the smallest realtime */
135*f14fb602SLionel Sambuc if (p1->P_RTIME_SEC < p2->P_RTIME_SEC)
136*f14fb602SLionel Sambuc return 0;
137*f14fb602SLionel Sambuc if (p2->P_RTIME_SEC < p1->P_RTIME_SEC)
138*f14fb602SLionel Sambuc return 1;
139*f14fb602SLionel Sambuc if (p1->P_RTIME_USEC < p2->P_RTIME_USEC)
140*f14fb602SLionel Sambuc return 0;
141*f14fb602SLionel Sambuc if (p2->P_RTIME_USEC < p1->P_RTIME_USEC)
142*f14fb602SLionel Sambuc return 1;
143*f14fb602SLionel Sambuc
144*f14fb602SLionel Sambuc return p2->p_pid > p1->p_pid; /* Nonsense */
145*f14fb602SLionel Sambuc }
146*f14fb602SLionel Sambuc #endif /* STANDALONE */
147