BSD 4_3_Reno release
[unix-history] / usr / src / bin / ps / ps.c
index 8a82930..d529999 100644 (file)
@@ -1,32 +1,35 @@
-/*
- * Copyright (c) 1989 The Regents of the University of California.
+/*-
+ * Copyright (c) 1990 The Regents of the University of California.
  * All rights reserved.
  *
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms is permitted
- * provided that all copyright information, including this notice,
- * is retained in all such forms, and that any documentation,
- * advertising or other materials related to such distribution and
- * use acknowledge that the software was
- * developed by the University of California, Berkeley.  The name
- * of the University may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms are permitted provided
+ * that: (1) source distributions retain this entire copyright notice and
+ * comment, and (2) distributions including binaries display the following
+ * acknowledgement:  ``This product includes software developed by the
+ * University of California, Berkeley and its contributors'' in the
+ * documentation or other materials provided with the distribution and in
+ * all advertising materials mentioning features or use of this software.
+ * 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
+"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ps.c       1.8 (Berkeley) 2/16/90";
+static char sccsid[] = "@(#)ps.c       5.28 (Berkeley) 6/26/90";
 #endif /* not lint */
 #endif /* not lint */
+
+#include <machine/pte.h>
+
 #include <sys/param.h>
 #include <sys/param.h>
-#include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/tty.h>
 #include <sys/user.h>
 #include <sys/ioctl.h>
 #include <sys/tty.h>
 #include <sys/user.h>
@@ -35,7 +38,6 @@ static char sccsid[] = "@(#)ps.c      1.8 (Berkeley) 2/16/90";
 #include <sys/text.h>
 #include <sys/stat.h>
 #include <sys/mbuf.h>
 #include <sys/text.h>
 #include <sys/stat.h>
 #include <sys/mbuf.h>
-#include <machine/pte.h>
 #include <nlist.h>
 #include <pwd.h>
 #include <math.h>
 #include <nlist.h>
 #include <pwd.h>
 #include <math.h>
@@ -127,8 +129,6 @@ struct var {
                0, pvar, 8, POFF(p_traceflag), LONG, "x"},
        {{"ktracep", "tracep"}, "KTRACEP",
                0, pvar, 8, POFF(p_tracep), LONG, "x"},
                0, pvar, 8, POFF(p_traceflag), LONG, "x"},
        {{"ktracep", "tracep"}, "KTRACEP",
                0, pvar, 8, POFF(p_tracep), LONG, "x"},
-       {{"cursig"}, "CURSIG",
-               0, pvar, 2, POFF(p_cursig), CHAR, "d"},
        {{"sig", "pending"}, "PENDING",
                0, pvar, 8, POFF(p_sig), LONG, "x"},
        {{"sigmask", "blocked"}, "BLOCKED",
        {{"sig", "pending"}, "PENDING",
                0, pvar, 8, POFF(p_sig), LONG, "x"},
        {{"sigmask", "blocked"}, "BLOCKED",
@@ -165,7 +165,7 @@ struct var {
                USER, uvar, 3, UOFF(u_acflag), SHORT, "x"},
        {{"start"}, "STARTED", USER|LJUST, started, 8},
        {{"lstart"}, "STARTED", USER|LJUST, lstarted, 28},
                USER, uvar, 3, UOFF(u_acflag), SHORT, "x"},
        {{"start"}, "STARTED", USER|LJUST, started, 8},
        {{"lstart"}, "STARTED", USER|LJUST, lstarted, 28},
-       {{"cputime", "time"}, "TIME", USER, cputime, 6},
+       {{"cputime", "time"}, "TIME", USER, cputime, 9},
        {{"p_ru"}, "P_RU", 0, pvar, 6, POFF(p_ru), KPTR, "x"},
        {{"pcpu", "%cpu"}, "%CPU", NLIST, pcpu, 4},
        {{"pmem", "%mem"}, "%MEM", NLIST, pmem, 4},
        {{"p_ru"}, "P_RU", 0, pvar, 6, POFF(p_ru), KPTR, "x"},
        {{"pcpu", "%cpu"}, "%CPU", NLIST, pcpu, 4},
        {{"pmem", "%mem"}, "%MEM", NLIST, pmem, 4},
@@ -222,7 +222,7 @@ struct combovar {
 #define LFMT \
        "uid pid ppid cp pri nice vsz rss wchan state tname cputime comm"
 #define        JFMT    "user pid ppid pgid sess jobc state tname cputime comm"
 #define LFMT \
        "uid pid ppid cp pri nice vsz rss wchan state tname cputime comm"
 #define        JFMT    "user pid ppid pgid sess jobc state tname cputime comm"
-#define        SFMT    "user pid cursig sig sigmask sigignore sigcatch tname comm"
+#define        SFMT    "uid pid sig sigmask sigignore sigcatch stat tname comm"
 #define        VFMT \
        "pid tt state time sl re pagein vsz rss lim tsiz trs %cpu %mem comm"
 #define UFMT \
 #define        VFMT \
        "pid tt state time sl re pagein vsz rss lim tsiz trs %cpu %mem comm"
 #define UFMT \
@@ -299,7 +299,7 @@ main (argc, argv)
        if (argc > 1)
                argv[1] = kludge_oldps_options(argv[1]);
 
        if (argc > 1)
                argv[1] = kludge_oldps_options(argv[1]);
 
-       while ((ch = getopt(argc, argv, "o:O:wlvujnsaxt:p:SCLmchT")) != EOF)
+       while ((ch = getopt(argc, argv, "o:O:wlvujnsaxt:p:SCLmrhTg")) != EOF)
                switch((char)ch) {
                case 'o':
                        parsefmt(optarg);
                switch((char)ch) {
                case 'o':
                        parsefmt(optarg);
@@ -371,6 +371,7 @@ main (argc, argv)
                }
                case 'p':
                        pid = atoi(optarg);
                }
                case 'p':
                        pid = atoi(optarg);
+                       xflg++;
                        break;
                case 'S':
                        sumrusage++;
                        break;
                case 'S':
                        sumrusage++;
@@ -410,12 +411,14 @@ main (argc, argv)
                case 'm':
                        sortby = SORTMEM;
                        break;
                case 'm':
                        sortby = SORTMEM;
                        break;
-               case 'c':
+               case 'r':
                        sortby = SORTCPU;
                        break;
                case 'h':
                        prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
                        break;
                        sortby = SORTCPU;
                        break;
                case 'h':
                        prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
                        break;
+               case 'g':
+                       break;  /* no-op */
                case '?':
                default:
                        fprintf(stderr, "usage: %s\n", USAGE);
                case '?':
                default:
                        fprintf(stderr, "usage: %s\n", USAGE);
@@ -448,8 +451,10 @@ main (argc, argv)
         * and adjusting header widths as appropiate.
         */
        scanvars();
         * and adjusting header widths as appropiate.
         */
        scanvars();
+#ifdef notdef
        if (sortby == SORTCPU)
                neednlist = 1;
        if (sortby == SORTCPU)
                neednlist = 1;
+#endif
        if (neednlist)
                donlist();
        /*
        if (neednlist)
                donlist();
        /*
@@ -464,7 +469,8 @@ main (argc, argv)
        } else if (pid != -1) {
                what = KINFO_PROC_PID;
                flag = pid;
        } else if (pid != -1) {
                what = KINFO_PROC_PID;
                flag = pid;
-       }
+       } else
+               what = KINFO_PROC_ALL;
        /*
         * select procs
         */
        /*
         * select procs
         */
@@ -704,7 +710,9 @@ state(k, v)
                *cp++ = 'V';
        if (flag & (SSYS|SLOCK|SULOCK|SKEEP|SPHYSIO))
                *cp++ = 'L';
                *cp++ = 'V';
        if (flag & (SSYS|SLOCK|SULOCK|SKEEP|SPHYSIO))
                *cp++ = 'L';
-       if ((flag & SCTTY) && k->ki_e->e_pgid == k->ki_e->e_pgid)
+       if (k->ki_e->e_flag & EPROC_SLEADER)
+               *cp++ = 's';
+       if ((flag & SCTTY) && k->ki_e->e_pgid == k->ki_e->e_tpgid)
                *cp++ = '+';
        *cp = '\0';
        printf("%-*s", v->width, buf);
                *cp++ = '+';
        *cp = '\0';
        printf("%-*s", v->width, buf);
@@ -765,7 +773,7 @@ tname(k, v)
                if (strncmp(tname, "tty", 3) == 0)
                        tname += 3;
                printf("%*.*s%c", v->width-1, v->width-1, tname,
                if (strncmp(tname, "tty", 3) == 0)
                        tname += 3;
                printf("%*.*s%c", v->width-1, v->width-1, tname,
-                       k->ki_p->p_flag & SCTTY ? ' ' : '-');
+                       k->ki_e->e_flag & EPROC_CTTY ? ' ' : '-');
        }
 }
 
        }
 }
 
@@ -824,7 +832,8 @@ wchan(k, v)
                printf("%-*s", v->width, "-");
 }
 
                printf("%-*s", v->width, "-");
 }
 
-#define pgtok(a)        ((a)/(1024/NBPG))
+#define pgtok(a)        (((a)*NBPG)/1024)
+#define pgtok(a)        (((a)*NBPG)/1024)
 
 vsize(k, v)
        struct kinfo *k;
 
 vsize(k, v)
        struct kinfo *k;
@@ -858,18 +867,33 @@ cputime(k, v)
        struct var *v;
 {
        long secs;
        struct var *v;
 {
        long secs;
-       char obuff[8];
+       long psecs;     /* "parts" of a second. first micro, then centi */
+       char obuff[128];
 
 
-       if (k->ki_p->p_stat == SZOMB || k->ki_u == NULL)
+       if (k->ki_p->p_stat == SZOMB || k->ki_u == NULL) {
                secs = 0;
                secs = 0;
-       else {
+               psecs = 0;
+       } else {
                secs = k->ki_p->p_utime.tv_sec + 
                        k->ki_p->p_stime.tv_sec;
                secs = k->ki_p->p_utime.tv_sec + 
                        k->ki_p->p_stime.tv_sec;
-               if (sumrusage)
+               psecs = k->ki_p->p_utime.tv_usec + 
+                       k->ki_p->p_stime.tv_usec;
+               if (sumrusage) {
                        secs += k->ki_u->u_cru.ru_utime.tv_sec + 
                                k->ki_u->u_cru.ru_stime.tv_sec;
                        secs += k->ki_u->u_cru.ru_utime.tv_sec + 
                                k->ki_u->u_cru.ru_stime.tv_sec;
+                       psecs += k->ki_u->u_cru.ru_utime.tv_usec + 
+                               k->ki_u->u_cru.ru_stime.tv_usec;
+               }
+               /*
+                * round and scale to 100's
+                */
+               psecs = (psecs + 5000) / 10000;
+               if (psecs >= 100) {
+                       psecs -= 100;
+                       secs++;
+               }
        }
        }
-       sprintf(obuff, "%3ld:%02ld", secs/60, secs%60);
+       sprintf(obuff, "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
        printf("%*s", v->width, obuff);
 }
 
        printf("%*s", v->width, obuff);
 }
 
@@ -886,7 +910,7 @@ getpcpu(k)
        struct proc *p = k->ki_p;
 #define        fxtofl(fixpt)   ((double)(fixpt) / fscale)
 
        struct proc *p = k->ki_p;
 #define        fxtofl(fixpt)   ((double)(fixpt) / fscale)
 
-       if (p->p_time == 0 || (p->p_flag & SLOAD) == 0)
+       if (p->p_time == 0 || (p->p_flag & SLOAD) == 0) /* XXX - I don't like this */
                return (0.0);
        if (rawcpu)
                return (100.0 * fxtofl(p->p_pctcpu));
                return (0.0);
        if (rawcpu)
                return (100.0 * fxtofl(p->p_pctcpu));
@@ -1138,6 +1162,10 @@ pscomp(k1, k2)
 
        if (sortby == SORTCPU)
                return (getpcpu(k2) - getpcpu(k1));
 
        if (sortby == SORTCPU)
                return (getpcpu(k2) - getpcpu(k1));
+#ifdef notyet
+       if (sortby == SORTRUN)
+               return (proc_compare(k1->ki_p, k2->ki_p));
+#endif
        if (sortby == SORTMEM)
                return (VSIZE(k2) - VSIZE(k1));
        i =  k1->ki_e->e_tdev - k2->ki_e->e_tdev;
        if (sortby == SORTMEM)
                return (VSIZE(k2) - VSIZE(k1));
        i =  k1->ki_e->e_tdev - k2->ki_e->e_tdev;