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.
44 #include "screen.h" /* interface to screen package */
45 #include "layout.h" /* defines for screen position layout */
48 #include "machine.h" /* we should eliminate this!!! */
56 static int last_hi = 0; /* used in u_process and u_endscreen */
57 static int lastline = 0;
58 static int display_width = MAX_COLS;
60 #define lineindex(l) ((l)*display_width)
63 /* things initialized by display_init and used thruout */
65 /* buffer of proc information lines for display updating */
66 static char *screenbuf = NULL;
68 static char **procstate_names;
69 static char **cpustate_names;
70 static char **memory_names;
71 static char **arc_names;
72 static char **carc_names;
73 static char **swap_names;
75 static int num_procstates;
76 static int num_cpustates;
77 static int num_memory;
80 static int *lprocstates;
81 static int *lcpustates;
86 static int *cpustate_columns;
87 static int cpustate_total_length;
88 static int cpustates_column;
90 static enum { OFF, ON, ERASE } header_status = ON;
92 static int string_count(char **);
93 static void summary_format(char *, int *, char **);
94 static void line_update(char *, char *, int, int);
99 int x_loadave_nompid = 15;
116 int x_idlecursor = 0;
117 int y_idlecursor = 5;
121 int Header_lines = 7;
128 /* first, deallocate any previous buffer that may have been there */
129 if (screenbuf != NULL)
134 /* calculate the current dimensions */
135 /* if operating in "dumb" mode, we only need one line */
136 lines = smart_terminal ? screen_length - Header_lines : 1;
140 /* we don't want more than MAX_COLS columns, since the machine-dependent
141 modules make static allocations based on MAX_COLS and we don't want
142 to run off the end of their buffers */
143 display_width = screen_width;
144 if (display_width >= MAX_COLS)
146 display_width = MAX_COLS - 1;
149 /* now, allocate space for the screen buffer */
150 screenbuf = malloc(lines * display_width);
151 if (screenbuf == (char *)NULL)
157 /* return number of lines available */
158 /* for dumb terminals, pretend like we can show any amount */
159 return(smart_terminal ? lines : Largest);
162 int display_updatecpus(struct statics *statics)
168 /* call resize to do the dirty work */
169 lines = display_resize();
171 num_cpus = statics->ncpus;
174 cpustates_column = 5; /* CPU: */
176 cpustates_column += 2; /* CPU 0: */
177 for (i = num_cpus; i > 9; i /= 10)
180 /* fill the "last" array with all -1s, to insure correct updating */
182 i = num_cpustates * num_cpus;
191 int display_init(struct statics * statics)
198 lines = display_updatecpus(statics);
200 /* only do the rest if we need to */
203 /* save pointers and allocate space for names */
204 procstate_names = statics->procstate_names;
205 num_procstates = string_count(procstate_names);
206 lprocstates = malloc(num_procstates * sizeof(int));
208 cpustate_names = statics->cpustate_names;
210 swap_names = statics->swap_names;
211 num_swap = string_count(swap_names);
212 lswap = malloc(num_swap * sizeof(int));
213 num_cpustates = string_count(cpustate_names);
214 lcpustates = malloc(num_cpustates * sizeof(int) * statics->ncpus);
215 cpustate_columns = malloc(num_cpustates * sizeof(int));
217 memory_names = statics->memory_names;
218 num_memory = string_count(memory_names);
219 lmemory = malloc(num_memory * sizeof(int));
221 arc_names = statics->arc_names;
222 carc_names = statics->carc_names;
224 /* calculate starting columns where needed */
225 cpustate_total_length = 0;
227 ip = cpustate_columns;
230 *ip++ = cpustate_total_length;
231 if ((i = strlen(*pp++)) > 0)
233 cpustate_total_length += i + 8;
238 /* return number of lines available */
243 i_loadave(int mpid, double avenrun[])
247 /* i_loadave also clears the screen, since it is first */
250 /* mpid == -1 implies this system doesn't have an _mpid */
253 printf("last pid: %5d; ", mpid);
256 printf("load averages");
258 for (i = 0; i < 3; i++)
268 u_loadave(int mpid, double *avenrun)
274 /* change screen only when value has really changed */
277 Move_to(x_lastpid, y_lastpid);
282 /* i remembers x coordinate to move to */
287 i = x_loadave_nompid;
290 /* move into position for load averages */
291 Move_to(i, y_loadave);
293 /* display new load averages */
294 /* we should optimize this and only display changes */
295 for (i = 0; i < 3; i++)
304 i_timeofday(time_t *tod)
307 * Display the current time.
308 * "ctime" always returns a string that looks like this:
310 * Sun Sep 16 01:03:52 1973
311 * 012345678901234567890123
314 * We want indices 11 thru 18 (length 8).
319 Move_to(screen_width - 8, 0);
332 printf("%-8.8s\n", &(ctime(tod)[11]));
336 static int ltotal = 0;
337 static char procstates_buffer[MAX_COLS];
340 * *_procstates(total, brkdn, names) - print the process summary line
342 * Assumptions: cursor is at the beginning of the line on entry
347 i_procstates(int total, int *brkdn)
351 /* write current number of processes and remember the value */
352 printf("%d processes:", total);
355 /* put out enough spaces to get to column 15 */
362 /* format and print the process state summary */
363 summary_format(procstates_buffer, brkdn, procstate_names);
364 fputs(procstates_buffer, stdout);
366 /* save the numbers for next time */
367 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
371 u_procstates(int total, int *brkdn)
373 static char new[MAX_COLS];
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);
383 /* cursor is already there...no motion needed */
384 /* assert(lastline == 1); */
388 /* if number of digits differs, rewrite the label */
389 if (digits(total) != digits(ltotal))
391 fputs(" processes:", stdout);
392 /* put out enough spaces to get to column 15 */
398 /* cursor may end up right where we want it!!! */
405 /* see if any of the state numbers has changed */
406 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
408 /* format and update the line */
409 summary_format(new, brkdn, procstate_names);
410 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
411 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
416 i_cpustates(int *states)
424 for (cpu = 0; cpu < num_cpus; cpu++) {
425 names = cpustate_names;
427 /* print tag and bump lastline */
431 value = printf("\nCPU %d: ", cpu);
432 while (value++ <= cpustates_column)
437 /* now walk thru the names and print the line */
438 while ((thisname = *names++) != NULL)
440 if (*thisname != '\0')
442 /* retrieve the value and remember it */
445 /* if percentage is >= 1000, print it as 100% */
446 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
447 (i++ % num_cpustates) == 0 ? "" : ", ",
454 /* copy over values into "last" array */
455 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
459 u_cpustates(int *states)
468 for (cpu = 0; cpu < num_cpus; cpu++) {
469 names = cpustate_names;
471 Move_to(cpustates_column, y_cpustates + cpu);
472 lastline = y_cpustates + cpu;
473 lp = lcpustates + (cpu * num_cpustates);
474 colp = cpustate_columns;
476 /* we could be much more optimal about this */
477 while ((thisname = *names++) != NULL)
479 if (*thisname != '\0')
481 /* did the value change since last time? */
484 /* yes, move and change */
485 Move_to(cpustates_column + *colp, y_cpustates + cpu);
486 lastline = y_cpustates + cpu;
488 /* retrieve value and remember it */
491 /* if percentage is >= 1000, print it as 100% */
492 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
493 ((double)value)/10.);
495 /* remember it for next time */
500 /* 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 */
541 i = num_cpustates * num_cpus;
549 * *_memory(stats) - print "Memory: " followed by the memory summary string
551 * Assumptions: cursor is on "lastline"
552 * for i_memory ONLY: cursor is on the previous line
555 static char memory_buffer[MAX_COLS];
560 fputs("\nMem: ", stdout);
563 /* format and print the memory summary */
564 summary_format(memory_buffer, stats, memory_names);
565 fputs(memory_buffer, stdout);
571 static char new[MAX_COLS];
573 /* format the new line */
574 summary_format(new, stats, memory_names);
575 line_update(memory_buffer, new, x_mem, y_mem);
579 * *_arc(stats) - print "ARC: " followed by the ARC summary string
581 * Assumptions: cursor is on "lastline"
582 * for i_arc ONLY: cursor is on the previous line
584 static char arc_buffer[MAX_COLS];
589 if (arc_names == NULL)
592 fputs("\nARC: ", stdout);
595 /* format and print the memory summary */
596 summary_format(arc_buffer, stats, arc_names);
597 fputs(arc_buffer, stdout);
603 static char new[MAX_COLS];
605 if (arc_names == NULL)
608 /* format the new line */
609 summary_format(new, stats, arc_names);
610 line_update(arc_buffer, new, x_arc, y_arc);
615 * *_carc(stats) - print "Compressed ARC: " followed by the summary string
617 * Assumptions: cursor is on "lastline"
618 * for i_carc ONLY: cursor is on the previous line
620 static char carc_buffer[MAX_COLS];
625 if (carc_names == NULL)
628 fputs("\n ", stdout);
631 /* format and print the memory summary */
632 summary_format(carc_buffer, stats, carc_names);
633 fputs(carc_buffer, stdout);
639 static char new[MAX_COLS];
641 if (carc_names == NULL)
644 /* format the new line */
645 summary_format(new, stats, carc_names);
646 line_update(carc_buffer, new, x_carc, y_carc);
650 * *_swap(stats) - print "Swap: " followed by the swap summary string
652 * Assumptions: cursor is on "lastline"
653 * for i_swap ONLY: cursor is on the previous line
656 static char swap_buffer[MAX_COLS];
661 fputs("\nSwap: ", stdout);
664 /* format and print the swap summary */
665 summary_format(swap_buffer, stats, swap_names);
666 fputs(swap_buffer, stdout);
672 static char new[MAX_COLS];
674 /* format the new line */
675 summary_format(new, stats, swap_names);
676 line_update(swap_buffer, new, x_swap, y_swap);
680 * *_message() - print the next pending message line, or erase the one
683 * Note that u_message is (currently) the same as i_message.
685 * Assumptions: lastline is consistent
689 * i_message is funny because it gets its message asynchronously (with
690 * respect to screen updates).
693 static char next_msg[MAX_COLS + 5];
694 static int msglen = 0;
695 /* Invariant: msglen is always the length of the message currently displayed
696 on the screen (even when next_msg doesn't contain that message). */
702 while (lastline < y_message)
707 if (next_msg[0] != '\0')
709 top_standout(next_msg);
710 msglen = strlen(next_msg);
715 (void) clear_eol(msglen);
726 static int header_length;
729 * Trim a header string to the current display width and return a newly
730 * allocated area with the trimmed header.
734 trim_header(char *text)
740 width = display_width;
741 header_length = strlen(text);
742 if (header_length >= width) {
743 s = malloc((width + 1) * sizeof(char));
746 strncpy(s, text, width);
753 * *_header(text) - print the header for the process area
755 * Assumptions: cursor is on the previous line and lastline is consistent
763 s = trim_header(text);
767 if (header_status == ON)
773 else if (header_status == ERASE)
781 u_header(char *text __unused)
784 if (header_status == ERASE)
788 clear_eol(header_length);
794 * *_process(line, thisline) - print one process line
796 * Assumptions: lastline is consistent
800 i_process(int line, char *thisline)
805 /* make sure we are on the correct line */
806 while (lastline < y_procs + line)
812 /* truncate the line to conform to our current screen width */
813 thisline[display_width] = '\0';
815 /* write the line out */
816 fputs(thisline, stdout);
818 /* copy it in to our buffer */
819 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
820 p = strecpy(base, thisline);
822 /* zero fill the rest of it */
823 bzero(p, display_width - (p - base));
827 u_process(int line, char *newline)
830 int screen_line = line + Header_lines;
833 /* remember a pointer to the current line in the screen buffer */
834 bufferline = &screenbuf[lineindex(line)];
836 /* truncate the line to conform to our current screen width */
837 newline[display_width] = '\0';
839 /* is line higher than we went on the last display? */
842 /* yes, just ignore screenbuf and write it out directly */
843 /* get positioned on the correct line */
844 if (screen_line - lastline == 1)
851 Move_to(0, screen_line);
852 lastline = screen_line;
855 /* now write the line */
856 fputs(newline, stdout);
858 /* copy it in to the buffer */
859 optr = strecpy(bufferline, newline);
861 /* zero fill the rest of it */
862 bzero(optr, display_width - (optr - bufferline));
866 line_update(bufferline, newline, 0, line + Header_lines);
873 int screen_line = hi + Header_lines;
880 /* need to blank the remainder of the screen */
881 /* but only if there is any screen left below this line */
882 if (lastline + 1 < screen_length)
884 /* efficiently move to the end of currently displayed info */
885 if (screen_line - lastline < 5)
887 while (lastline < screen_line)
895 Move_to(0, screen_line);
896 lastline = screen_line;
901 /* we can do this the easy way */
902 putcap(clear_to_end);
906 /* use clear_eol on each line */
908 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
917 /* move the cursor to a pleasant place */
918 Move_to(x_idlecursor, y_idlecursor);
919 lastline = y_idlecursor;
923 /* separate this display from the next with some vertical room */
924 fputs("\n\n", stdout);
929 display_header(int t)
936 else if (header_status == ON)
938 header_status = ERASE;
943 new_message(int type, char *msgfmt, ...)
948 va_start(args, msgfmt);
950 /* first, format the message */
951 vsnprintf(next_msg, sizeof(next_msg), msgfmt, args);
957 /* message there already -- can we clear it? */
960 /* yes -- write it and clear to end */
961 i = strlen(next_msg);
962 if ((type & MT_delayed) == 0)
964 type & MT_standout ? top_standout(next_msg) :
965 fputs(next_msg, stdout);
966 (void) clear_eol(msglen - i);
974 if ((type & MT_delayed) == 0)
976 type & MT_standout ? top_standout(next_msg) : fputs(next_msg, stdout);
977 msglen = strlen(next_msg);
986 if (clear_eol(msglen) == 1)
993 readline(char *buffer, int size, int numeric)
1000 /* allow room for null terminator */
1004 while ((fflush(stdout), read(0, ptr, 1) > 0))
1006 /* newline means we are done */
1007 if ((ch = *ptr) == '\n' || ch == '\r')
1012 /* handle special editing characters */
1015 /* kill line -- account for overstriking */
1021 /* return null string */
1026 else if (ch == ch_erase)
1028 /* erase previous character */
1031 /* none to erase! */
1036 fputs("\b \b", stdout);
1041 /* check for character validity and buffer overflow */
1042 else if (cnt == size || (numeric && !isdigit(ch)) ||
1050 /* echo it and store it in the buffer */
1061 /* all done -- null terminate the string */
1064 /* account for the extra characters in the message area */
1065 /* (if terminal overstrikes, remember the furthest they went) */
1066 msglen += overstrike ? maxcnt : cnt;
1068 /* return either inputted number or string length */
1070 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1073 /* internal support routines */
1075 static int string_count(char **pp)
1080 while (*pp++ != NULL)
1087 static void summary_format(char *str, int *numbers, char **names)
1094 /* format each number followed by its string */
1096 while ((thisname = *names++) != NULL)
1098 /* get the number to format */
1101 /* display only non-zero numbers */
1104 /* is this number in kilobytes? */
1105 if (thisname[0] == 'K')
1107 /* yes: format it as a memory value */
1108 p = strecpy(p, format_k(num));
1110 /* skip over the K, since it was included by format_k */
1111 p = strecpy(p, thisname+1);
1113 /* is this number a ratio? */
1114 else if (thisname[0] == ':')
1116 (void) snprintf(rbuf, sizeof(rbuf), "%.2f",
1117 (float)*(numbers - 2) / (float)num);
1118 p = strecpy(p, rbuf);
1119 p = strecpy(p, thisname);
1123 p = strecpy(p, itoa(num));
1124 p = strecpy(p, thisname);
1128 /* ignore negative numbers, but display corresponding string */
1131 p = strecpy(p, thisname);
1135 /* if the last two characters in the string are ", ", delete them */
1137 if (p >= str && p[0] == ',' && p[1] == ' ')
1144 line_update(char *old, char *new, int start, int line)
1148 int newcol = start + 1;
1149 int lastcol = start;
1150 char cursor_on_line = false;
1153 /* compare the two strings and only rewrite what has changed */
1156 fprintf(debug, "line_update, starting at %d\n", start);
1160 fputs("\n-\n", debug);
1163 /* start things off on the right foot */
1164 /* this is to make sure the invariants get set up right */
1165 if ((ch = *new++) != *old)
1167 if (line - lastline == 1 && start == 0)
1173 Move_to(start, line);
1175 cursor_on_line = true;
1183 * main loop -- check each character. If the old and new aren't the
1184 * same, then update the display. When the distance from the
1185 * current cursor position to the new change is small enough,
1186 * the characters that belong there are written to move the
1190 * lastcol is the column where the cursor currently is sitting
1191 * (always one beyond the end of the last mismatch).
1193 do /* yes, a do...while */
1195 if ((ch = *new++) != *old)
1197 /* new character is different from old */
1198 /* make sure the cursor is on top of this character */
1199 diff = newcol - lastcol;
1202 /* some motion is required--figure out which is shorter */
1203 if (diff < 6 && cursor_on_line)
1205 /* overwrite old stuff--get it out of the old buffer */
1206 printf("%.*s", diff, ¤t[lastcol-start]);
1210 /* use cursor addressing */
1211 Move_to(newcol, line);
1212 cursor_on_line = true;
1214 /* remember where the cursor is */
1215 lastcol = newcol + 1;
1219 /* already there, update position */
1223 /* write what we need to */
1226 /* at the end--terminate with a clear-to-end-of-line */
1227 (void) clear_eol(strlen(old));
1231 /* write the new character */
1234 /* put the new character in the screen buffer */
1238 /* update working column and screen buffer pointer */
1242 } while (ch != '\0');
1244 /* zero out the rest of the line buffer -- MUST BE DONE! */
1245 diff = display_width - newcol;
1251 /* remember where the current line is */
1259 * printable(str) - make the string pointed to by "str" into one that is
1260 * printable (i.e.: all ascii), by converting all non-printable
1261 * characters into '?'. Replacements are done in place and a pointer
1262 * to the original buffer is returned.
1266 printable(char str[])
1272 while ((ch = *ptr) != '\0')
1284 i_uptime(struct timeval *bt, time_t *tod)
1287 int days, hrs, mins, secs;
1289 if (bt->tv_sec != -1) {
1290 uptime = *tod - bt->tv_sec;
1291 days = uptime / 86400;
1293 hrs = uptime / 3600;
1299 * Display the uptime.
1304 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1310 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);