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 */
355 while (i++ < (ps.thread ? 6 : 4))
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 printf(" %s:", ps.thread ? "threads" : "processes");
393 /* put out enough spaces to get to column 15 */
395 while (i++ < (ps.thread ? 6 : 4))
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;
423 int *hstates = states;
426 for (cpu = 0; cpu < num_cpus; cpu++) {
427 names = cpustate_names;
429 /* print tag and bump lastline */
433 value = printf("\nCPU %d: ", cpu);
434 while (value++ <= cpustates_column)
439 /* now walk thru the names and print the line */
440 while ((thisname = *names++) != NULL)
442 if (*thisname != '\0')
444 /* retrieve the value and remember it */
447 /* if percentage is >= 1000, print it as 100% */
448 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
449 (i++ % num_cpustates) == 0 ? "" : ", ",
456 /* copy over values into "last" array */
458 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
462 u_cpustates(int *states)
465 const char * const *names;
466 const char *thisname;
467 int *hstates = states;
472 for (cpu = 0; cpu < num_cpus; cpu++) {
473 names = cpustate_names;
475 Move_to(cpustates_column, y_cpustates + cpu);
476 lastline = y_cpustates + cpu;
477 lp = lcpustates + (cpu * num_cpustates);
478 colp = cpustate_columns;
480 /* we could be much more optimal about this */
481 while ((thisname = *names++) != NULL)
483 if (*thisname != '\0')
485 /* did the value change since last time? */
488 /* yes, move and change */
489 Move_to(cpustates_column + *colp, y_cpustates + cpu);
490 lastline = y_cpustates + cpu;
492 /* retrieve value and remember it */
495 /* if percentage is >= 1000, print it as 100% */
496 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
497 ((double)value)/10.);
499 /* remember it for next time */
504 /* increment and move on */
522 for (cpu = 0; cpu < num_cpus; cpu++) {
523 names = cpustate_names;
525 /* show tag and bump lastline */
529 value = printf("\nCPU %d: ", cpu);
530 while (value++ <= cpustates_column)
535 while ((thisname = *names++) != NULL)
537 if (*thisname != '\0')
539 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
544 /* fill the "last" array with all -1s, to insure correct updating */
545 for (i = 0; i < num_cpustates * num_cpus; ++i) {
551 * *_memory(stats) - print "Memory: " followed by the memory summary string
553 * Assumptions: cursor is on "lastline"
554 * for i_memory ONLY: cursor is on the previous line
557 static char *memory_buffer = NULL;
562 memory_buffer = setup_buffer(memory_buffer, 0);
564 fputs("\nMem: ", stdout);
567 /* format and print the memory summary */
568 summary_format(memory_buffer, stats, memory_names);
569 fputs(memory_buffer, stdout);
575 static char *new = NULL;
577 new = setup_buffer(new, 0);
579 /* format the new line */
580 summary_format(new, stats, memory_names);
581 line_update(memory_buffer, new, x_mem, y_mem);
585 * *_arc(stats) - print "ARC: " followed by the ARC summary string
587 * Assumptions: cursor is on "lastline"
588 * for i_arc ONLY: cursor is on the previous line
590 static char *arc_buffer = NULL;
595 arc_buffer = setup_buffer(arc_buffer, 0);
597 if (arc_names == NULL)
600 fputs("\nARC: ", stdout);
603 /* format and print the memory summary */
604 summary_format(arc_buffer, stats, arc_names);
605 fputs(arc_buffer, stdout);
611 static char *new = NULL;
613 new = setup_buffer(new, 0);
615 if (arc_names == NULL)
618 /* format the new line */
619 summary_format(new, stats, arc_names);
620 line_update(arc_buffer, new, x_arc, y_arc);
625 * *_carc(stats) - print "Compressed ARC: " followed by the summary string
627 * Assumptions: cursor is on "lastline"
628 * for i_carc ONLY: cursor is on the previous line
630 static char *carc_buffer = NULL;
635 carc_buffer = setup_buffer(carc_buffer, 0);
637 if (carc_names == NULL)
640 fputs("\n ", stdout);
643 /* format and print the memory summary */
644 summary_format(carc_buffer, stats, carc_names);
645 fputs(carc_buffer, stdout);
651 static char *new = NULL;
653 new = setup_buffer(new, 0);
655 if (carc_names == NULL)
658 /* format the new line */
659 summary_format(new, stats, carc_names);
660 line_update(carc_buffer, new, x_carc, y_carc);
664 * *_swap(stats) - print "Swap: " followed by the swap summary string
666 * Assumptions: cursor is on "lastline"
667 * for i_swap ONLY: cursor is on the previous line
670 static char *swap_buffer = NULL;
675 swap_buffer = setup_buffer(swap_buffer, 0);
677 fputs("\nSwap: ", stdout);
680 /* format and print the swap summary */
681 summary_format(swap_buffer, stats, swap_names);
682 fputs(swap_buffer, stdout);
688 static char *new = NULL;
690 new = setup_buffer(new, 0);
692 /* format the new line */
693 summary_format(new, stats, swap_names);
694 line_update(swap_buffer, new, x_swap, y_swap);
698 * *_message() - print the next pending message line, or erase the one
701 * Note that u_message is (currently) the same as i_message.
703 * Assumptions: lastline is consistent
707 * i_message is funny because it gets its message asynchronously (with
708 * respect to screen updates).
711 #define NEXT_MSG_ADDLEN 5
712 static char *next_msg = NULL;
713 static int msglen = 0;
714 /* Invariant: msglen is always the length of the message currently displayed
715 on the screen (even when next_msg doesn't contain that message). */
720 next_msg = setup_buffer(next_msg, NEXT_MSG_ADDLEN);
722 while (lastline < y_message)
727 if (next_msg[0] != '\0')
729 top_standout(next_msg);
730 msglen = strlen(next_msg);
735 (void) clear_eol(msglen);
746 static int header_length;
749 * Trim a header string to the current display width and return a newly
750 * allocated area with the trimmed header.
754 trim_header(const char *text)
760 width = screen_width;
761 header_length = strlen(text);
762 if (header_length >= width) {
763 s = strndup(text, width);
771 * *_header(text) - print the header for the process area
773 * Assumptions: cursor is on the previous line and lastline is consistent
777 i_header(const char *text)
781 s = trim_header(text);
785 if (header_status == ON)
791 else if (header_status == ERASE)
799 u_header(const char *text __unused)
802 if (header_status == ERASE)
806 clear_eol(header_length);
812 * *_process(line, thisline) - print one process line
814 * Assumptions: lastline is consistent
818 i_process(int line, char *thisline)
823 /* make sure we are on the correct line */
824 while (lastline < y_procs + line)
830 /* truncate the line to conform to our current screen width */
831 thisline[screen_width] = '\0';
833 /* write the line out */
834 fputs(thisline, stdout);
836 /* copy it in to our buffer */
837 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
838 p = stpcpy(base, thisline);
840 /* zero fill the rest of it */
841 memset(p, 0, screen_width - (p - base));
845 u_process(int line, char *newline)
848 int screen_line = line + Header_lines;
851 /* remember a pointer to the current line in the screen buffer */
852 bufferline = &screenbuf[lineindex(line)];
854 /* truncate the line to conform to our current screen width */
855 newline[screen_width] = '\0';
857 /* is line higher than we went on the last display? */
860 /* yes, just ignore screenbuf and write it out directly */
861 /* get positioned on the correct line */
862 if (screen_line - lastline == 1)
869 Move_to(0, screen_line);
870 lastline = screen_line;
873 /* now write the line */
874 fputs(newline, stdout);
876 /* copy it in to the buffer */
877 optr = stpcpy(bufferline, newline);
879 /* zero fill the rest of it */
880 memset(optr, 0, screen_width - (optr - bufferline));
884 line_update(bufferline, newline, 0, line + Header_lines);
891 int screen_line = hi + Header_lines;
898 /* need to blank the remainder of the screen */
899 /* but only if there is any screen left below this line */
900 if (lastline + 1 < screen_length)
902 /* efficiently move to the end of currently displayed info */
903 if (screen_line - lastline < 5)
905 while (lastline < screen_line)
913 Move_to(0, screen_line);
914 lastline = screen_line;
919 /* we can do this the easy way */
920 putcap(clear_to_end);
924 /* use clear_eol on each line */
926 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
935 /* move the cursor to a pleasant place */
936 Move_to(x_idlecursor, y_idlecursor);
937 lastline = y_idlecursor;
941 /* separate this display from the next with some vertical room */
942 fputs("\n\n", stdout);
947 display_header(int t)
954 else if (header_status == ON)
956 header_status = ERASE;
961 new_message(int type, const char *msgfmt, ...)
966 va_start(args, msgfmt);
968 /* first, format the message */
969 vsnprintf(next_msg, setup_buffer_bufsiz + NEXT_MSG_ADDLEN,
976 /* message there already -- can we clear it? */
979 /* yes -- write it and clear to end */
980 i = strlen(next_msg);
981 if ((type & MT_delayed) == 0)
983 if (type & MT_standout) {
984 top_standout(next_msg);
986 fputs(next_msg, stdout);
988 clear_eol(msglen - i);
996 if ((type & MT_delayed) == 0)
998 if (type & MT_standout) {
999 top_standout(next_msg);
1001 fputs(next_msg, stdout);
1003 msglen = strlen(next_msg);
1012 if (clear_eol(msglen) == 1)
1019 readline(char *buffer, int size, int numeric)
1026 /* allow room for null terminator */
1030 while ((fflush(stdout), read(0, ptr, 1) > 0))
1032 /* newline means we are done */
1033 if ((ch = *ptr) == '\n' || ch == '\r')
1038 /* handle special editing characters */
1041 /* kill line -- account for overstriking */
1047 /* return null string */
1052 else if (ch == ch_erase)
1054 /* erase previous character */
1057 /* none to erase! */
1062 fputs("\b \b", stdout);
1067 /* check for character validity and buffer overflow */
1068 else if (cnt == size || (numeric && !isdigit(ch)) ||
1076 /* echo it and store it in the buffer */
1087 /* all done -- null terminate the string */
1090 /* account for the extra characters in the message area */
1091 /* (if terminal overstrikes, remember the furthest they went) */
1092 msglen += overstrike ? maxcnt : cnt;
1094 /* return either inputted number or string length */
1096 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1099 /* internal support routines */
1102 summary_format(char *str, int *numbers, const char * const *names)
1106 const char *thisname;
1109 /* format each number followed by its string */
1111 while ((thisname = *names++) != NULL)
1113 /* get the number to format */
1116 /* display only non-zero numbers */
1119 /* is this number in kilobytes? */
1120 if (thisname[0] == 'K')
1122 /* yes: format it as a memory value */
1123 p = stpcpy(p, format_k(num));
1125 /* skip over the K, since it was included by format_k */
1126 p = stpcpy(p, thisname+1);
1128 /* is this number a ratio? */
1129 else if (thisname[0] == ':')
1131 (void) snprintf(rbuf, sizeof(rbuf), "%.2f",
1132 (float)*(numbers - 2) / (float)num);
1133 p = stpcpy(p, rbuf);
1134 p = stpcpy(p, thisname);
1138 p = stpcpy(p, itoa(num));
1139 p = stpcpy(p, thisname);
1143 /* ignore negative numbers, but display corresponding string */
1146 p = stpcpy(p, thisname);
1150 /* if the last two characters in the string are ", ", delete them */
1152 if (p >= str && p[0] == ',' && p[1] == ' ')
1159 line_update(char *old, char *new, int start, int line)
1163 int newcol = start + 1;
1164 int lastcol = start;
1165 char cursor_on_line = false;
1168 /* compare the two strings and only rewrite what has changed */
1171 fprintf(debug, "line_update, starting at %d\n", start);
1175 fputs("\n-\n", debug);
1178 /* start things off on the right foot */
1179 /* this is to make sure the invariants get set up right */
1180 if ((ch = *new++) != *old)
1182 if (line - lastline == 1 && start == 0)
1188 Move_to(start, line);
1190 cursor_on_line = true;
1198 * main loop -- check each character. If the old and new aren't the
1199 * same, then update the display. When the distance from the
1200 * current cursor position to the new change is small enough,
1201 * the characters that belong there are written to move the
1205 * lastcol is the column where the cursor currently is sitting
1206 * (always one beyond the end of the last mismatch).
1208 do /* yes, a do...while */
1210 if ((ch = *new++) != *old)
1212 /* new character is different from old */
1213 /* make sure the cursor is on top of this character */
1214 diff = newcol - lastcol;
1217 /* some motion is required--figure out which is shorter */
1218 if (diff < 6 && cursor_on_line)
1220 /* overwrite old stuff--get it out of the old buffer */
1221 printf("%.*s", diff, ¤t[lastcol-start]);
1225 /* use cursor addressing */
1226 Move_to(newcol, line);
1227 cursor_on_line = true;
1229 /* remember where the cursor is */
1230 lastcol = newcol + 1;
1234 /* already there, update position */
1238 /* write what we need to */
1241 /* at the end--terminate with a clear-to-end-of-line */
1242 (void) clear_eol(strlen(old));
1246 /* write the new character */
1249 /* put the new character in the screen buffer */
1253 /* update working column and screen buffer pointer */
1257 } while (ch != '\0');
1259 /* zero out the rest of the line buffer -- MUST BE DONE! */
1260 diff = screen_width - newcol;
1263 memset(old, 0, diff);
1266 /* remember where the current line is */
1274 * printable(str) - make the string pointed to by "str" into one that is
1275 * printable (i.e.: all ascii), by converting all non-printable
1276 * characters into '?'. Replacements are done in place and a pointer
1277 * to the original buffer is returned.
1281 printable(char str[])
1287 while ((ch = *ptr) != '\0')
1299 i_uptime(struct timeval *bt, time_t *tod)
1302 int days, hrs, mins, secs;
1304 if (bt->tv_sec != -1) {
1305 uptime = *tod - bt->tv_sec;
1306 days = uptime / 86400;
1308 hrs = uptime / 3600;
1314 * Display the uptime.
1319 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1325 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);
1329 #define SETUPBUFFER_REQUIRED_ADDBUFSIZ 2
1332 setup_buffer(char *buffer, int addlen)
1336 if (NULL == buffer) {
1337 setup_buffer_bufsiz = screen_width;
1338 b = calloc(setup_buffer_bufsiz + addlen +
1339 SETUPBUFFER_REQUIRED_ADDBUFSIZ,
1342 if (screen_width > setup_buffer_bufsiz) {
1343 setup_buffer_bufsiz = screen_width;
1345 b = calloc(setup_buffer_bufsiz + addlen +
1346 SETUPBUFFER_REQUIRED_ADDBUFSIZ,
1354 fprintf(stderr, "%s: can't allocate sufficient memory\n",