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.
45 #include "screen.h" /* interface to screen package */
46 #include "layout.h" /* defines for screen position layout */
49 #include "machine.h" /* we should eliminate this!!! */
57 static int last_hi = 0; /* used in u_process and u_endscreen */
58 static int lastline = 0;
59 static int display_width = MAX_COLS;
61 #define lineindex(l) ((l)*display_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 int string_count(char **);
94 static void summary_format(char *, int *, char **);
95 static void line_update(char *, char *, int, int);
100 int x_loadave_nompid = 15;
117 int x_idlecursor = 0;
118 int y_idlecursor = 5;
122 int Header_lines = 7;
129 /* first, deallocate any previous buffer that may have been there */
130 if (screenbuf != NULL)
135 /* calculate the current dimensions */
136 /* if operating in "dumb" mode, we only need one line */
137 lines = smart_terminal ? screen_length - Header_lines : 1;
141 /* we don't want more than MAX_COLS columns, since the machine-dependent
142 modules make static allocations based on MAX_COLS and we don't want
143 to run off the end of their buffers */
144 display_width = screen_width;
145 if (display_width >= MAX_COLS)
147 display_width = MAX_COLS - 1;
150 /* now, allocate space for the screen buffer */
151 screenbuf = calloc(lines, display_width);
152 if (screenbuf == NULL)
158 /* return number of lines available */
159 /* for dumb terminals, pretend like we can show any amount */
160 return(smart_terminal ? lines : Largest);
163 int display_updatecpus(struct statics *statics)
169 /* call resize to do the dirty work */
170 lines = display_resize();
172 num_cpus = statics->ncpus;
175 cpustates_column = 5; /* CPU: */
177 cpustates_column += 2; /* CPU 0: */
178 for (i = num_cpus; i > 9; i /= 10)
181 /* fill the "last" array with all -1s, to insure correct updating */
183 i = num_cpustates * num_cpus;
192 int display_init(struct statics * statics)
199 lines = display_updatecpus(statics);
201 /* only do the rest if we need to */
204 /* save pointers and allocate space for names */
205 procstate_names = statics->procstate_names;
206 num_procstates = string_count(procstate_names);
207 assert(num_procstates > 0);
208 lprocstates = calloc(num_procstates, sizeof(int));
210 cpustate_names = statics->cpustate_names;
212 swap_names = statics->swap_names;
213 num_swap = string_count(swap_names);
214 assert(num_swap > 0);
215 lswap = calloc(num_swap, sizeof(int));
216 num_cpustates = string_count(cpustate_names);
217 assert(num_cpustates > 0);
218 lcpustates = calloc(num_cpustates * sizeof(int), statics->ncpus);
219 cpustate_columns = calloc(num_cpustates, sizeof(int));
221 memory_names = statics->memory_names;
222 num_memory = string_count(memory_names);
223 assert(num_memory > 0);
224 lmemory = calloc(num_memory, sizeof(int));
226 arc_names = statics->arc_names;
227 carc_names = statics->carc_names;
229 /* calculate starting columns where needed */
230 cpustate_total_length = 0;
232 ip = cpustate_columns;
235 *ip++ = cpustate_total_length;
236 if ((i = strlen(*pp++)) > 0)
238 cpustate_total_length += i + 8;
243 /* return number of lines available */
248 i_loadave(int mpid, double avenrun[])
252 /* i_loadave also clears the screen, since it is first */
255 /* mpid == -1 implies this system doesn't have an _mpid */
258 printf("last pid: %5d; ", mpid);
261 printf("load averages");
263 for (i = 0; i < 3; i++)
273 u_loadave(int mpid, double *avenrun)
279 /* change screen only when value has really changed */
282 Move_to(x_lastpid, y_lastpid);
287 /* i remembers x coordinate to move to */
292 i = x_loadave_nompid;
295 /* move into position for load averages */
296 Move_to(i, y_loadave);
298 /* display new load averages */
299 /* we should optimize this and only display changes */
300 for (i = 0; i < 3; i++)
309 i_timeofday(time_t *tod)
312 * Display the current time.
313 * "ctime" always returns a string that looks like this:
315 * Sun Sep 16 01:03:52 1973
316 * 012345678901234567890123
319 * We want indices 11 thru 18 (length 8).
324 Move_to(screen_width - 8, 0);
337 printf("%-8.8s\n", &(ctime(tod)[11]));
341 static int ltotal = 0;
342 static char procstates_buffer[MAX_COLS];
345 * *_procstates(total, brkdn, names) - print the process summary line
347 * Assumptions: cursor is at the beginning of the line on entry
352 i_procstates(int total, int *brkdn)
356 /* write current number of processes and remember the value */
357 printf("%d processes:", total);
360 /* put out enough spaces to get to column 15 */
367 /* format and print the process state summary */
368 summary_format(procstates_buffer, brkdn, procstate_names);
369 fputs(procstates_buffer, stdout);
371 /* save the numbers for next time */
372 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
376 u_procstates(int total, int *brkdn)
378 static char new[MAX_COLS];
381 /* update number of processes only if it has changed */
384 /* move and overwrite */
385 #if (x_procstate == 0)
386 Move_to(x_procstate, y_procstate);
388 /* cursor is already there...no motion needed */
389 /* assert(lastline == 1); */
393 /* if number of digits differs, rewrite the label */
394 if (digits(total) != digits(ltotal))
396 fputs(" processes:", stdout);
397 /* put out enough spaces to get to column 15 */
403 /* cursor may end up right where we want it!!! */
410 /* see if any of the state numbers has changed */
411 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
413 /* format and update the line */
414 summary_format(new, brkdn, procstate_names);
415 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
416 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
421 i_cpustates(int *states)
429 for (cpu = 0; cpu < num_cpus; cpu++) {
430 names = cpustate_names;
432 /* print tag and bump lastline */
436 value = printf("\nCPU %d: ", cpu);
437 while (value++ <= cpustates_column)
442 /* now walk thru the names and print the line */
443 while ((thisname = *names++) != NULL)
445 if (*thisname != '\0')
447 /* retrieve the value and remember it */
450 /* if percentage is >= 1000, print it as 100% */
451 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
452 (i++ % num_cpustates) == 0 ? "" : ", ",
459 /* copy over values into "last" array */
460 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
464 u_cpustates(int *states)
473 for (cpu = 0; cpu < num_cpus; cpu++) {
474 names = cpustate_names;
476 Move_to(cpustates_column, y_cpustates + cpu);
477 lastline = y_cpustates + cpu;
478 lp = lcpustates + (cpu * num_cpustates);
479 colp = cpustate_columns;
481 /* we could be much more optimal about this */
482 while ((thisname = *names++) != NULL)
484 if (*thisname != '\0')
486 /* did the value change since last time? */
489 /* yes, move and change */
490 Move_to(cpustates_column + *colp, y_cpustates + cpu);
491 lastline = y_cpustates + cpu;
493 /* retrieve value and remember it */
496 /* if percentage is >= 1000, print it as 100% */
497 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
498 ((double)value)/10.);
500 /* remember it for next time */
505 /* 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 */
546 i = num_cpustates * num_cpus;
554 * *_memory(stats) - print "Memory: " followed by the memory summary string
556 * Assumptions: cursor is on "lastline"
557 * for i_memory ONLY: cursor is on the previous line
560 static char memory_buffer[MAX_COLS];
565 fputs("\nMem: ", stdout);
568 /* format and print the memory summary */
569 summary_format(memory_buffer, stats, memory_names);
570 fputs(memory_buffer, stdout);
576 static char new[MAX_COLS];
578 /* format the new line */
579 summary_format(new, stats, memory_names);
580 line_update(memory_buffer, new, x_mem, y_mem);
584 * *_arc(stats) - print "ARC: " followed by the ARC summary string
586 * Assumptions: cursor is on "lastline"
587 * for i_arc ONLY: cursor is on the previous line
589 static char arc_buffer[MAX_COLS];
594 if (arc_names == NULL)
597 fputs("\nARC: ", stdout);
600 /* format and print the memory summary */
601 summary_format(arc_buffer, stats, arc_names);
602 fputs(arc_buffer, stdout);
608 static char new[MAX_COLS];
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[MAX_COLS];
630 if (carc_names == NULL)
633 fputs("\n ", stdout);
636 /* format and print the memory summary */
637 summary_format(carc_buffer, stats, carc_names);
638 fputs(carc_buffer, stdout);
644 static char new[MAX_COLS];
646 if (carc_names == NULL)
649 /* format the new line */
650 summary_format(new, stats, carc_names);
651 line_update(carc_buffer, new, x_carc, y_carc);
655 * *_swap(stats) - print "Swap: " followed by the swap summary string
657 * Assumptions: cursor is on "lastline"
658 * for i_swap ONLY: cursor is on the previous line
661 static char swap_buffer[MAX_COLS];
666 fputs("\nSwap: ", stdout);
669 /* format and print the swap summary */
670 summary_format(swap_buffer, stats, swap_names);
671 fputs(swap_buffer, stdout);
677 static char new[MAX_COLS];
679 /* format the new line */
680 summary_format(new, stats, swap_names);
681 line_update(swap_buffer, new, x_swap, y_swap);
685 * *_message() - print the next pending message line, or erase the one
688 * Note that u_message is (currently) the same as i_message.
690 * Assumptions: lastline is consistent
694 * i_message is funny because it gets its message asynchronously (with
695 * respect to screen updates).
698 static char next_msg[MAX_COLS + 5];
699 static int msglen = 0;
700 /* Invariant: msglen is always the length of the message currently displayed
701 on the screen (even when next_msg doesn't contain that message). */
707 while (lastline < y_message)
712 if (next_msg[0] != '\0')
714 top_standout(next_msg);
715 msglen = strlen(next_msg);
720 (void) clear_eol(msglen);
731 static int header_length;
734 * Trim a header string to the current display width and return a newly
735 * allocated area with the trimmed header.
739 trim_header(const char *text)
745 width = display_width;
746 header_length = strlen(text);
747 if (header_length >= width) {
748 s = strndup(text, width);
756 * *_header(text) - print the header for the process area
758 * Assumptions: cursor is on the previous line and lastline is consistent
762 i_header(const char *text)
766 s = trim_header(text);
770 if (header_status == ON)
776 else if (header_status == ERASE)
784 u_header(const char *text __unused)
787 if (header_status == ERASE)
791 clear_eol(header_length);
797 * *_process(line, thisline) - print one process line
799 * Assumptions: lastline is consistent
803 i_process(int line, char *thisline)
808 /* make sure we are on the correct line */
809 while (lastline < y_procs + line)
815 /* truncate the line to conform to our current screen width */
816 thisline[display_width] = '\0';
818 /* write the line out */
819 fputs(thisline, stdout);
821 /* copy it in to our buffer */
822 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
823 p = stpcpy(base, thisline);
825 /* zero fill the rest of it */
826 bzero(p, display_width - (p - base));
830 u_process(int line, char *newline)
833 int screen_line = line + Header_lines;
836 /* remember a pointer to the current line in the screen buffer */
837 bufferline = &screenbuf[lineindex(line)];
839 /* truncate the line to conform to our current screen width */
840 newline[display_width] = '\0';
842 /* is line higher than we went on the last display? */
845 /* yes, just ignore screenbuf and write it out directly */
846 /* get positioned on the correct line */
847 if (screen_line - lastline == 1)
854 Move_to(0, screen_line);
855 lastline = screen_line;
858 /* now write the line */
859 fputs(newline, stdout);
861 /* copy it in to the buffer */
862 optr = stpcpy(bufferline, newline);
864 /* zero fill the rest of it */
865 bzero(optr, display_width - (optr - bufferline));
869 line_update(bufferline, newline, 0, line + Header_lines);
876 int screen_line = hi + Header_lines;
883 /* need to blank the remainder of the screen */
884 /* but only if there is any screen left below this line */
885 if (lastline + 1 < screen_length)
887 /* efficiently move to the end of currently displayed info */
888 if (screen_line - lastline < 5)
890 while (lastline < screen_line)
898 Move_to(0, screen_line);
899 lastline = screen_line;
904 /* we can do this the easy way */
905 putcap(clear_to_end);
909 /* use clear_eol on each line */
911 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
920 /* move the cursor to a pleasant place */
921 Move_to(x_idlecursor, y_idlecursor);
922 lastline = y_idlecursor;
926 /* separate this display from the next with some vertical room */
927 fputs("\n\n", stdout);
932 display_header(int t)
939 else if (header_status == ON)
941 header_status = ERASE;
946 new_message(int type, const char *msgfmt, ...)
951 va_start(args, msgfmt);
953 /* first, format the message */
954 vsnprintf(next_msg, sizeof(next_msg), msgfmt, args);
960 /* message there already -- can we clear it? */
963 /* yes -- write it and clear to end */
964 i = strlen(next_msg);
965 if ((type & MT_delayed) == 0)
967 type & MT_standout ? top_standout(next_msg) :
968 fputs(next_msg, stdout);
969 (void) clear_eol(msglen - i);
977 if ((type & MT_delayed) == 0)
979 type & MT_standout ? top_standout(next_msg) : fputs(next_msg, stdout);
980 msglen = strlen(next_msg);
989 if (clear_eol(msglen) == 1)
996 readline(char *buffer, int size, int numeric)
1003 /* allow room for null terminator */
1007 while ((fflush(stdout), read(0, ptr, 1) > 0))
1009 /* newline means we are done */
1010 if ((ch = *ptr) == '\n' || ch == '\r')
1015 /* handle special editing characters */
1018 /* kill line -- account for overstriking */
1024 /* return null string */
1029 else if (ch == ch_erase)
1031 /* erase previous character */
1034 /* none to erase! */
1039 fputs("\b \b", stdout);
1044 /* check for character validity and buffer overflow */
1045 else if (cnt == size || (numeric && !isdigit(ch)) ||
1053 /* echo it and store it in the buffer */
1064 /* all done -- null terminate the string */
1067 /* account for the extra characters in the message area */
1068 /* (if terminal overstrikes, remember the furthest they went) */
1069 msglen += overstrike ? maxcnt : cnt;
1071 /* return either inputted number or string length */
1073 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1076 /* internal support routines */
1078 static int string_count(char **pp)
1083 while (*pp++ != NULL)
1090 static void summary_format(char *str, int *numbers, char **names)
1097 /* format each number followed by its string */
1099 while ((thisname = *names++) != NULL)
1101 /* get the number to format */
1104 /* display only non-zero numbers */
1107 /* is this number in kilobytes? */
1108 if (thisname[0] == 'K')
1110 /* yes: format it as a memory value */
1111 p = stpcpy(p, format_k(num));
1113 /* skip over the K, since it was included by format_k */
1114 p = stpcpy(p, thisname+1);
1116 /* is this number a ratio? */
1117 else if (thisname[0] == ':')
1119 (void) snprintf(rbuf, sizeof(rbuf), "%.2f",
1120 (float)*(numbers - 2) / (float)num);
1121 p = stpcpy(p, rbuf);
1122 p = stpcpy(p, thisname);
1126 p = stpcpy(p, itoa(num));
1127 p = stpcpy(p, thisname);
1131 /* ignore negative numbers, but display corresponding string */
1134 p = stpcpy(p, thisname);
1138 /* if the last two characters in the string are ", ", delete them */
1140 if (p >= str && p[0] == ',' && p[1] == ' ')
1147 line_update(char *old, char *new, int start, int line)
1151 int newcol = start + 1;
1152 int lastcol = start;
1153 char cursor_on_line = false;
1156 /* compare the two strings and only rewrite what has changed */
1159 fprintf(debug, "line_update, starting at %d\n", start);
1163 fputs("\n-\n", debug);
1166 /* start things off on the right foot */
1167 /* this is to make sure the invariants get set up right */
1168 if ((ch = *new++) != *old)
1170 if (line - lastline == 1 && start == 0)
1176 Move_to(start, line);
1178 cursor_on_line = true;
1186 * main loop -- check each character. If the old and new aren't the
1187 * same, then update the display. When the distance from the
1188 * current cursor position to the new change is small enough,
1189 * the characters that belong there are written to move the
1193 * lastcol is the column where the cursor currently is sitting
1194 * (always one beyond the end of the last mismatch).
1196 do /* yes, a do...while */
1198 if ((ch = *new++) != *old)
1200 /* new character is different from old */
1201 /* make sure the cursor is on top of this character */
1202 diff = newcol - lastcol;
1205 /* some motion is required--figure out which is shorter */
1206 if (diff < 6 && cursor_on_line)
1208 /* overwrite old stuff--get it out of the old buffer */
1209 printf("%.*s", diff, ¤t[lastcol-start]);
1213 /* use cursor addressing */
1214 Move_to(newcol, line);
1215 cursor_on_line = true;
1217 /* remember where the cursor is */
1218 lastcol = newcol + 1;
1222 /* already there, update position */
1226 /* write what we need to */
1229 /* at the end--terminate with a clear-to-end-of-line */
1230 (void) clear_eol(strlen(old));
1234 /* write the new character */
1237 /* put the new character in the screen buffer */
1241 /* update working column and screen buffer pointer */
1245 } while (ch != '\0');
1247 /* zero out the rest of the line buffer -- MUST BE DONE! */
1248 diff = display_width - newcol;
1254 /* remember where the current line is */
1262 * printable(str) - make the string pointed to by "str" into one that is
1263 * printable (i.e.: all ascii), by converting all non-printable
1264 * characters into '?'. Replacements are done in place and a pointer
1265 * to the original buffer is returned.
1269 printable(char str[])
1275 while ((ch = *ptr) != '\0')
1287 i_uptime(struct timeval *bt, time_t *tod)
1290 int days, hrs, mins, secs;
1292 if (bt->tv_sec != -1) {
1293 uptime = *tod - bt->tv_sec;
1294 days = uptime / 86400;
1296 hrs = uptime / 3600;
1302 * Display the uptime.
1307 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1313 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);