2 * Top users/processes display for Unix
5 * This program may be freely redistributed,
6 * but this entire comment MUST remain intact.
8 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
9 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
15 * This file contains the routines that display information on the screen.
16 * Each section of the screen has two routines: one for initially writing
17 * all constant and dynamic text, and one for only updating the text that
18 * changes. The prefix "i_" is used on all the "initial" routines and the
19 * prefix "u_" is used for all the "updating" routines.
22 * None of the "i_" routines use any of the termcap capabilities.
23 * In this way, those routines can be safely used on terminals that
24 * have minimal (or nonexistant) terminal capabilities.
26 * The routines are called in this order: *_loadave, i_timeofday,
27 * *_procstates, *_cpustates, *_memory, *_message, *_header,
28 * *_process, u_endscreen.
31 #include <sys/cdefs.h>
32 #include <sys/resource.h>
46 #include "screen.h" /* interface to screen package */
47 #include "layout.h" /* defines for screen position layout */
50 #include "machine.h" /* we should eliminate this!!! */
58 static int last_hi = 0; /* used in u_process and u_endscreen */
59 static int lastline = 0;
61 #define lineindex(l) ((l)*screen_width)
64 /* things initialized by display_init and used thruout */
66 /* buffer of proc information lines for display updating */
67 static char *screenbuf = NULL;
69 static const char * const *procstate_names;
70 static const char * const *cpustate_names;
71 static const char * const *memory_names;
72 static const char * const *arc_names;
73 static const char * const *carc_names;
74 static const char * const *swap_names;
76 static int num_procstates;
77 static int num_cpustates;
78 static int num_memory;
81 static int *lprocstates;
82 static int *lcpustates;
87 static int *cpustate_columns;
88 static int cpustate_total_length;
89 static int cpustates_column;
91 static enum { OFF, ON, ERASE } header_status = ON;
93 static void summary_format(char *, int *, const char * const *);
94 static void line_update(char *, char *, int, int);
96 static int setup_buffer_bufsiz = 0;
97 static char * setup_buffer(char *, int);
102 int x_loadave_nompid = 15;
119 int x_idlecursor = 0;
120 int y_idlecursor = 5;
124 int Header_lines = 7;
131 /* first, deallocate any previous buffer that may have been there */
132 if (screenbuf != NULL)
137 /* calculate the current dimensions */
138 /* if operating in "dumb" mode, we only need one line */
139 lines = smart_terminal ? screen_length - Header_lines : 1;
144 /* now, allocate space for the screen buffer */
145 screenbuf = calloc(lines, screen_width);
146 if (screenbuf == NULL)
152 /* return number of lines available */
153 /* for dumb terminals, pretend like we can show any amount */
154 return(smart_terminal ? lines : Largest);
158 display_updatecpus(struct statics *statics)
163 /* call resize to do the dirty work */
164 lines = display_resize();
166 num_cpus = statics->ncpus;
169 cpustates_column = 5; /* CPU: */
171 cpustates_column += 1 + digits(num_cpus); /* CPU #: */
174 /* fill the "last" array with all -1s, to insure correct updating */
175 for (i = 0; i < num_cpustates * num_cpus; ++i) {
183 display_init(struct statics * statics)
190 lines = display_updatecpus(statics);
192 /* only do the rest if we need to */
195 /* save pointers and allocate space for names */
196 procstate_names = statics->procstate_names;
198 assert(num_procstates > 0);
199 lprocstates = calloc(num_procstates, sizeof(int));
201 cpustate_names = statics->cpustate_names;
203 swap_names = statics->swap_names;
205 assert(num_swap > 0);
206 lswap = calloc(num_swap, sizeof(int));
207 num_cpustates = CPUSTATES;
208 assert(num_cpustates > 0);
209 lcpustates = calloc(num_cpustates * sizeof(int), statics->ncpus);
210 cpustate_columns = calloc(num_cpustates, sizeof(int));
212 memory_names = statics->memory_names;
214 assert(num_memory > 0);
215 lmemory = calloc(num_memory, sizeof(int));
217 arc_names = statics->arc_names;
218 carc_names = statics->carc_names;
220 /* calculate starting columns where needed */
221 cpustate_total_length = 0;
223 ip = cpustate_columns;
226 *ip++ = cpustate_total_length;
227 if ((i = strlen(*pp++)) > 0)
229 cpustate_total_length += i + 8;
234 /* return number of lines available */
239 i_loadave(int mpid, double avenrun[])
243 /* i_loadave also clears the screen, since it is first */
246 /* mpid == -1 implies this system doesn't have an _mpid */
249 printf("last pid: %5d; ", mpid);
252 printf("load averages");
254 for (i = 0; i < 3; i++)
264 u_loadave(int mpid, double *avenrun)
270 /* change screen only when value has really changed */
273 Move_to(x_lastpid, y_lastpid);
278 /* i remembers x coordinate to move to */
283 i = x_loadave_nompid;
286 /* move into position for load averages */
287 Move_to(i, y_loadave);
289 /* display new load averages */
290 /* we should optimize this and only display changes */
291 for (i = 0; i < 3; i++)
300 i_timeofday(time_t *tod)
303 * Display the current time.
304 * "ctime" always returns a string that looks like this:
306 * Sun Sep 16 01:03:52 1973
307 * 012345678901234567890123
310 * We want indices 11 thru 18 (length 8).
315 Move_to(screen_width - 8, 0);
328 printf("%-8.8s\n", &(ctime(tod)[11]));
332 static int ltotal = 0;
333 static char *procstates_buffer = NULL;
336 * *_procstates(total, brkdn, names) - print the process summary line
338 * Assumptions: cursor is at the beginning of the line on entry
343 i_procstates(int total, int *brkdn)
347 procstates_buffer = setup_buffer(procstates_buffer, 0);
349 /* write current number of processes and remember the value */
350 printf("%d %s:", total, (ps.thread) ? "threads" :"processes");
353 /* put out enough spaces to get to column 15 */
360 /* format and print the process state summary */
361 summary_format(procstates_buffer, brkdn, procstate_names);
362 fputs(procstates_buffer, stdout);
364 /* save the numbers for next time */
365 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
369 u_procstates(int total, int *brkdn)
371 static char *new = NULL;
374 new = setup_buffer(new, 0);
376 /* update number of processes only if it has changed */
379 /* move and overwrite */
380 if (x_procstate == 0) {
381 Move_to(x_procstate, y_procstate);
384 /* cursor is already there...no motion needed */
385 assert(lastline == 1);
389 /* if number of digits differs, rewrite the label */
390 if (digits(total) != digits(ltotal))
392 fputs(" processes:", stdout);
393 /* put out enough spaces to get to column 15 */
399 /* cursor may end up right where we want it!!! */
406 /* see if any of the state numbers has changed */
407 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
409 /* format and update the line */
410 summary_format(new, brkdn, procstate_names);
411 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
412 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
417 i_cpustates(int *states)
421 const char * const *names;
422 const char *thisname;
425 for (cpu = 0; cpu < num_cpus; cpu++) {
426 names = cpustate_names;
428 /* print tag and bump lastline */
432 value = printf("\nCPU %d: ", cpu);
433 while (value++ <= cpustates_column)
438 /* now walk thru the names and print the line */
439 while ((thisname = *names++) != NULL)
441 if (*thisname != '\0')
443 /* retrieve the value and remember it */
446 /* if percentage is >= 1000, print it as 100% */
447 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
448 (i++ % num_cpustates) == 0 ? "" : ", ",
455 /* copy over values into "last" array */
456 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
460 u_cpustates(int *states)
463 const char * const *names;
464 const char *thisname;
469 for (cpu = 0; cpu < num_cpus; cpu++) {
470 names = cpustate_names;
472 Move_to(cpustates_column, y_cpustates + cpu);
473 lastline = y_cpustates + cpu;
474 lp = lcpustates + (cpu * num_cpustates);
475 colp = cpustate_columns;
477 /* we could be much more optimal about this */
478 while ((thisname = *names++) != NULL)
480 if (*thisname != '\0')
482 /* did the value change since last time? */
485 /* yes, move and change */
486 Move_to(cpustates_column + *colp, y_cpustates + cpu);
487 lastline = y_cpustates + cpu;
489 /* retrieve value and remember it */
492 /* if percentage is >= 1000, print it as 100% */
493 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
494 ((double)value)/10.);
496 /* remember it for next time */
501 /* increment and move on */
517 for (cpu = 0; cpu < num_cpus; cpu++) {
518 names = cpustate_names;
520 /* show tag and bump lastline */
524 value = printf("\nCPU %d: ", cpu);
525 while (value++ <= cpustates_column)
530 while ((thisname = *names++) != NULL)
532 if (*thisname != '\0')
534 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
539 /* fill the "last" array with all -1s, to insure correct updating */
540 for (i = 0; i < num_cpustates * num_cpus; ++i) {
546 * *_memory(stats) - print "Memory: " followed by the memory summary string
548 * Assumptions: cursor is on "lastline"
549 * for i_memory ONLY: cursor is on the previous line
552 static char *memory_buffer = NULL;
557 memory_buffer = setup_buffer(memory_buffer, 0);
559 fputs("\nMem: ", stdout);
562 /* format and print the memory summary */
563 summary_format(memory_buffer, stats, memory_names);
564 fputs(memory_buffer, stdout);
570 static char *new = NULL;
572 new = setup_buffer(new, 0);
574 /* format the new line */
575 summary_format(new, stats, memory_names);
576 line_update(memory_buffer, new, x_mem, y_mem);
580 * *_arc(stats) - print "ARC: " followed by the ARC summary string
582 * Assumptions: cursor is on "lastline"
583 * for i_arc ONLY: cursor is on the previous line
585 static char *arc_buffer = NULL;
590 arc_buffer = setup_buffer(arc_buffer, 0);
592 if (arc_names == NULL)
595 fputs("\nARC: ", stdout);
598 /* format and print the memory summary */
599 summary_format(arc_buffer, stats, arc_names);
600 fputs(arc_buffer, stdout);
606 static char *new = NULL;
608 new = setup_buffer(new, 0);
610 if (arc_names == NULL)
613 /* format the new line */
614 summary_format(new, stats, arc_names);
615 line_update(arc_buffer, new, x_arc, y_arc);
620 * *_carc(stats) - print "Compressed ARC: " followed by the summary string
622 * Assumptions: cursor is on "lastline"
623 * for i_carc ONLY: cursor is on the previous line
625 static char *carc_buffer = NULL;
630 carc_buffer = setup_buffer(carc_buffer, 0);
632 if (carc_names == NULL)
635 fputs("\n ", stdout);
638 /* format and print the memory summary */
639 summary_format(carc_buffer, stats, carc_names);
640 fputs(carc_buffer, stdout);
646 static char *new = NULL;
648 new = setup_buffer(new, 0);
650 if (carc_names == NULL)
653 /* format the new line */
654 summary_format(new, stats, carc_names);
655 line_update(carc_buffer, new, x_carc, y_carc);
659 * *_swap(stats) - print "Swap: " followed by the swap summary string
661 * Assumptions: cursor is on "lastline"
662 * for i_swap ONLY: cursor is on the previous line
665 static char *swap_buffer = NULL;
670 swap_buffer = setup_buffer(swap_buffer, 0);
672 fputs("\nSwap: ", stdout);
675 /* format and print the swap summary */
676 summary_format(swap_buffer, stats, swap_names);
677 fputs(swap_buffer, stdout);
683 static char *new = NULL;
685 new = setup_buffer(new, 0);
687 /* format the new line */
688 summary_format(new, stats, swap_names);
689 line_update(swap_buffer, new, x_swap, y_swap);
693 * *_message() - print the next pending message line, or erase the one
696 * Note that u_message is (currently) the same as i_message.
698 * Assumptions: lastline is consistent
702 * i_message is funny because it gets its message asynchronously (with
703 * respect to screen updates).
706 #define NEXT_MSG_ADDLEN 5
707 static char *next_msg = NULL;
708 static int msglen = 0;
709 /* Invariant: msglen is always the length of the message currently displayed
710 on the screen (even when next_msg doesn't contain that message). */
715 next_msg = setup_buffer(next_msg, NEXT_MSG_ADDLEN);
717 while (lastline < y_message)
722 if (next_msg[0] != '\0')
724 top_standout(next_msg);
725 msglen = strlen(next_msg);
730 (void) clear_eol(msglen);
741 static int header_length;
744 * Trim a header string to the current display width and return a newly
745 * allocated area with the trimmed header.
749 trim_header(const char *text)
755 width = screen_width;
756 header_length = strlen(text);
757 if (header_length >= width) {
758 s = strndup(text, width);
766 * *_header(text) - print the header for the process area
768 * Assumptions: cursor is on the previous line and lastline is consistent
772 i_header(const char *text)
776 s = trim_header(text);
780 if (header_status == ON)
786 else if (header_status == ERASE)
794 u_header(const char *text __unused)
797 if (header_status == ERASE)
801 clear_eol(header_length);
807 * *_process(line, thisline) - print one process line
809 * Assumptions: lastline is consistent
813 i_process(int line, char *thisline)
818 /* make sure we are on the correct line */
819 while (lastline < y_procs + line)
825 /* truncate the line to conform to our current screen width */
826 thisline[screen_width] = '\0';
828 /* write the line out */
829 fputs(thisline, stdout);
831 /* copy it in to our buffer */
832 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
833 p = stpcpy(base, thisline);
835 /* zero fill the rest of it */
836 memset(p, 0, screen_width - (p - base));
840 u_process(int line, char *newline)
843 int screen_line = line + Header_lines;
846 /* remember a pointer to the current line in the screen buffer */
847 bufferline = &screenbuf[lineindex(line)];
849 /* truncate the line to conform to our current screen width */
850 newline[screen_width] = '\0';
852 /* is line higher than we went on the last display? */
855 /* yes, just ignore screenbuf and write it out directly */
856 /* get positioned on the correct line */
857 if (screen_line - lastline == 1)
864 Move_to(0, screen_line);
865 lastline = screen_line;
868 /* now write the line */
869 fputs(newline, stdout);
871 /* copy it in to the buffer */
872 optr = stpcpy(bufferline, newline);
874 /* zero fill the rest of it */
875 memset(optr, 0, screen_width - (optr - bufferline));
879 line_update(bufferline, newline, 0, line + Header_lines);
886 int screen_line = hi + Header_lines;
893 /* need to blank the remainder of the screen */
894 /* but only if there is any screen left below this line */
895 if (lastline + 1 < screen_length)
897 /* efficiently move to the end of currently displayed info */
898 if (screen_line - lastline < 5)
900 while (lastline < screen_line)
908 Move_to(0, screen_line);
909 lastline = screen_line;
914 /* we can do this the easy way */
915 putcap(clear_to_end);
919 /* use clear_eol on each line */
921 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
930 /* move the cursor to a pleasant place */
931 Move_to(x_idlecursor, y_idlecursor);
932 lastline = y_idlecursor;
936 /* separate this display from the next with some vertical room */
937 fputs("\n\n", stdout);
942 display_header(int t)
949 else if (header_status == ON)
951 header_status = ERASE;
956 new_message(int type, const char *msgfmt, ...)
961 va_start(args, msgfmt);
963 /* first, format the message */
964 vsnprintf(next_msg, setup_buffer_bufsiz + NEXT_MSG_ADDLEN,
971 /* message there already -- can we clear it? */
974 /* yes -- write it and clear to end */
975 i = strlen(next_msg);
976 if ((type & MT_delayed) == 0)
978 if (type & MT_standout) {
979 top_standout(next_msg);
981 fputs(next_msg, stdout);
983 clear_eol(msglen - i);
991 if ((type & MT_delayed) == 0)
993 if (type & MT_standout) {
994 top_standout(next_msg);
996 fputs(next_msg, stdout);
998 msglen = strlen(next_msg);
1007 if (clear_eol(msglen) == 1)
1014 readline(char *buffer, int size, int numeric)
1021 /* allow room for null terminator */
1025 while ((fflush(stdout), read(0, ptr, 1) > 0))
1027 /* newline means we are done */
1028 if ((ch = *ptr) == '\n' || ch == '\r')
1033 /* handle special editing characters */
1036 /* kill line -- account for overstriking */
1042 /* return null string */
1047 else if (ch == ch_erase)
1049 /* erase previous character */
1052 /* none to erase! */
1057 fputs("\b \b", stdout);
1062 /* check for character validity and buffer overflow */
1063 else if (cnt == size || (numeric && !isdigit(ch)) ||
1071 /* echo it and store it in the buffer */
1082 /* all done -- null terminate the string */
1085 /* account for the extra characters in the message area */
1086 /* (if terminal overstrikes, remember the furthest they went) */
1087 msglen += overstrike ? maxcnt : cnt;
1089 /* return either inputted number or string length */
1091 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1094 /* internal support routines */
1097 summary_format(char *str, int *numbers, const char * const *names)
1101 const char *thisname;
1104 /* format each number followed by its string */
1106 while ((thisname = *names++) != NULL)
1108 /* get the number to format */
1111 /* display only non-zero numbers */
1114 /* is this number in kilobytes? */
1115 if (thisname[0] == 'K')
1117 /* yes: format it as a memory value */
1118 p = stpcpy(p, format_k(num));
1120 /* skip over the K, since it was included by format_k */
1121 p = stpcpy(p, thisname+1);
1123 /* is this number a ratio? */
1124 else if (thisname[0] == ':')
1126 (void) snprintf(rbuf, sizeof(rbuf), "%.2f",
1127 (float)*(numbers - 2) / (float)num);
1128 p = stpcpy(p, rbuf);
1129 p = stpcpy(p, thisname);
1133 p = stpcpy(p, itoa(num));
1134 p = stpcpy(p, thisname);
1138 /* ignore negative numbers, but display corresponding string */
1141 p = stpcpy(p, thisname);
1145 /* if the last two characters in the string are ", ", delete them */
1147 if (p >= str && p[0] == ',' && p[1] == ' ')
1154 line_update(char *old, char *new, int start, int line)
1158 int newcol = start + 1;
1159 int lastcol = start;
1160 char cursor_on_line = false;
1163 /* compare the two strings and only rewrite what has changed */
1166 fprintf(debug, "line_update, starting at %d\n", start);
1170 fputs("\n-\n", debug);
1173 /* start things off on the right foot */
1174 /* this is to make sure the invariants get set up right */
1175 if ((ch = *new++) != *old)
1177 if (line - lastline == 1 && start == 0)
1183 Move_to(start, line);
1185 cursor_on_line = true;
1193 * main loop -- check each character. If the old and new aren't the
1194 * same, then update the display. When the distance from the
1195 * current cursor position to the new change is small enough,
1196 * the characters that belong there are written to move the
1200 * lastcol is the column where the cursor currently is sitting
1201 * (always one beyond the end of the last mismatch).
1203 do /* yes, a do...while */
1205 if ((ch = *new++) != *old)
1207 /* new character is different from old */
1208 /* make sure the cursor is on top of this character */
1209 diff = newcol - lastcol;
1212 /* some motion is required--figure out which is shorter */
1213 if (diff < 6 && cursor_on_line)
1215 /* overwrite old stuff--get it out of the old buffer */
1216 printf("%.*s", diff, ¤t[lastcol-start]);
1220 /* use cursor addressing */
1221 Move_to(newcol, line);
1222 cursor_on_line = true;
1224 /* remember where the cursor is */
1225 lastcol = newcol + 1;
1229 /* already there, update position */
1233 /* write what we need to */
1236 /* at the end--terminate with a clear-to-end-of-line */
1237 (void) clear_eol(strlen(old));
1241 /* write the new character */
1244 /* put the new character in the screen buffer */
1248 /* update working column and screen buffer pointer */
1252 } while (ch != '\0');
1254 /* zero out the rest of the line buffer -- MUST BE DONE! */
1255 diff = screen_width - newcol;
1258 memset(old, 0, diff);
1261 /* remember where the current line is */
1269 * printable(str) - make the string pointed to by "str" into one that is
1270 * printable (i.e.: all ascii), by converting all non-printable
1271 * characters into '?'. Replacements are done in place and a pointer
1272 * to the original buffer is returned.
1276 printable(char str[])
1282 while ((ch = *ptr) != '\0')
1294 i_uptime(struct timeval *bt, time_t *tod)
1297 int days, hrs, mins, secs;
1299 if (bt->tv_sec != -1) {
1300 uptime = *tod - bt->tv_sec;
1301 days = uptime / 86400;
1303 hrs = uptime / 3600;
1309 * Display the uptime.
1314 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1320 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);
1324 #define SETUPBUFFER_REQUIRED_ADDBUFSIZ 2
1327 setup_buffer(char *buffer, int addlen)
1331 if (NULL == buffer) {
1332 setup_buffer_bufsiz = screen_width;
1333 b = calloc(setup_buffer_bufsiz + addlen +
1334 SETUPBUFFER_REQUIRED_ADDBUFSIZ,
1337 if (screen_width > setup_buffer_bufsiz) {
1338 setup_buffer_bufsiz = screen_width;
1340 b = calloc(setup_buffer_bufsiz + addlen +
1341 SETUPBUFFER_REQUIRED_ADDBUFSIZ,
1349 fprintf(stderr, "%s: can't allocate sufficient memory\n",