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.
43 #include "screen.h" /* interface to screen package */
44 #include "layout.h" /* defines for screen position layout */
48 #include "machine.h" /* we should eliminate this!!! */
55 /* imported from screen.c */
56 extern int overstrike;
59 static int last_hi = 0; /* used in u_process and u_endscreen */
60 static int lastline = 0;
61 static int display_width = MAX_COLS;
63 #define lineindex(l) ((l)*display_width)
66 /* things initialized by display_init and used thruout */
68 /* buffer of proc information lines for display updating */
69 static char *screenbuf = NULL;
71 static char **procstate_names;
72 static char **cpustate_names;
73 static char **memory_names;
74 static char **arc_names;
75 static char **carc_names;
76 static char **swap_names;
78 static int num_procstates;
79 static int num_cpustates;
80 static int num_memory;
83 static int *lprocstates;
84 static int *lcpustates;
89 static int *cpustate_columns;
90 static int cpustate_total_length;
91 static int cpustates_column;
93 static enum { OFF, ON, ERASE } header_status = ON;
95 static int string_count(char **);
96 static void summary_format(char *, int *, char **);
97 static void line_update(char *, char *, int, 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;
143 /* we don't want more than MAX_COLS columns, since the machine-dependent
144 modules make static allocations based on MAX_COLS and we don't want
145 to run off the end of their buffers */
146 display_width = screen_width;
147 if (display_width >= MAX_COLS)
149 display_width = MAX_COLS - 1;
152 /* now, allocate space for the screen buffer */
153 screenbuf = (char *)malloc(lines * display_width);
154 if (screenbuf == (char *)NULL)
160 /* return number of lines available */
161 /* for dumb terminals, pretend like we can show any amount */
162 return(smart_terminal ? lines : Largest);
165 int display_updatecpus(statics)
167 struct statics *statics;
174 /* call resize to do the dirty work */
175 lines = display_resize();
177 num_cpus = statics->ncpus;
180 cpustates_column = 5; /* CPU: */
182 cpustates_column += 2; /* CPU 0: */
183 for (i = num_cpus; i > 9; i /= 10)
186 /* fill the "last" array with all -1s, to insure correct updating */
188 i = num_cpustates * num_cpus;
197 int display_init(statics)
199 struct statics *statics;
207 lines = display_updatecpus(statics);
209 /* only do the rest if we need to */
212 /* save pointers and allocate space for names */
213 procstate_names = statics->procstate_names;
214 num_procstates = string_count(procstate_names);
215 lprocstates = (int *)malloc(num_procstates * sizeof(int));
217 cpustate_names = statics->cpustate_names;
219 swap_names = statics->swap_names;
220 num_swap = string_count(swap_names);
221 lswap = (int *)malloc(num_swap * sizeof(int));
222 num_cpustates = string_count(cpustate_names);
223 lcpustates = (int *)malloc(num_cpustates * sizeof(int) * statics->ncpus);
224 cpustate_columns = (int *)malloc(num_cpustates * sizeof(int));
226 memory_names = statics->memory_names;
227 num_memory = string_count(memory_names);
228 lmemory = (int *)malloc(num_memory * sizeof(int));
230 arc_names = statics->arc_names;
231 carc_names = statics->carc_names;
233 /* calculate starting columns where needed */
234 cpustate_total_length = 0;
236 ip = cpustate_columns;
239 *ip++ = cpustate_total_length;
240 if ((i = strlen(*pp++)) > 0)
242 cpustate_total_length += i + 8;
247 /* return number of lines available */
252 i_loadave(mpid, avenrun)
260 /* i_loadave also clears the screen, since it is first */
263 /* mpid == -1 implies this system doesn't have an _mpid */
266 printf("last pid: %5d; ", mpid);
269 printf("load averages");
271 for (i = 0; i < 3; i++)
281 u_loadave(mpid, avenrun)
291 /* change screen only when value has really changed */
294 Move_to(x_lastpid, y_lastpid);
299 /* i remembers x coordinate to move to */
304 i = x_loadave_nompid;
307 /* move into position for load averages */
308 Move_to(i, y_loadave);
310 /* display new load averages */
311 /* we should optimize this and only display changes */
312 for (i = 0; i < 3; i++)
327 * Display the current time.
328 * "ctime" always returns a string that looks like this:
330 * Sun Sep 16 01:03:52 1973
331 * 012345678901234567890123
334 * We want indices 11 thru 18 (length 8).
339 Move_to(screen_width - 8, 0);
352 printf("%-8.8s\n", &(ctime(tod)[11]));
356 static int ltotal = 0;
357 static char procstates_buffer[MAX_COLS];
360 * *_procstates(total, brkdn, names) - print the process summary line
362 * Assumptions: cursor is at the beginning of the line on entry
367 i_procstates(total, brkdn)
375 /* write current number of processes and remember the value */
376 printf("%d processes:", total);
379 /* put out enough spaces to get to column 15 */
386 /* format and print the process state summary */
387 summary_format(procstates_buffer, brkdn, procstate_names);
388 fputs(procstates_buffer, stdout);
390 /* save the numbers for next time */
391 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
395 u_procstates(total, brkdn)
401 static char new[MAX_COLS];
404 /* update number of processes only if it has changed */
407 /* move and overwrite */
408 #if (x_procstate == 0)
409 Move_to(x_procstate, y_procstate);
411 /* cursor is already there...no motion needed */
412 /* assert(lastline == 1); */
416 /* if number of digits differs, rewrite the label */
417 if (digits(total) != digits(ltotal))
419 fputs(" processes:", stdout);
420 /* put out enough spaces to get to column 15 */
426 /* cursor may end up right where we want it!!! */
433 /* see if any of the state numbers has changed */
434 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
436 /* format and update the line */
437 summary_format(new, brkdn, procstate_names);
438 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
439 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
455 for (cpu = 0; cpu < num_cpus; cpu++) {
456 names = cpustate_names;
458 /* print tag and bump lastline */
462 value = printf("\nCPU %d: ", cpu);
463 while (value++ <= cpustates_column)
468 /* now walk thru the names and print the line */
469 while ((thisname = *names++) != NULL)
471 if (*thisname != '\0')
473 /* retrieve the value and remember it */
476 /* if percentage is >= 1000, print it as 100% */
477 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
478 (i++ % num_cpustates) == 0 ? "" : ", ",
485 /* copy over values into "last" array */
486 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
502 for (cpu = 0; cpu < num_cpus; cpu++) {
503 names = cpustate_names;
505 Move_to(cpustates_column, y_cpustates + cpu);
506 lastline = y_cpustates + cpu;
507 lp = lcpustates + (cpu * num_cpustates);
508 colp = cpustate_columns;
510 /* we could be much more optimal about this */
511 while ((thisname = *names++) != NULL)
513 if (*thisname != '\0')
515 /* did the value change since last time? */
518 /* yes, move and change */
519 Move_to(cpustates_column + *colp, y_cpustates + cpu);
520 lastline = y_cpustates + cpu;
522 /* retrieve value and remember it */
525 /* if percentage is >= 1000, print it as 100% */
526 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
527 ((double)value)/10.);
529 /* remember it for next time */
534 /* increment and move on */
552 for (cpu = 0; cpu < num_cpus; cpu++) {
553 names = cpustate_names;
555 /* show tag and bump lastline */
559 value = printf("\nCPU %d: ", cpu);
560 while (value++ <= cpustates_column)
565 while ((thisname = *names++) != NULL)
567 if (*thisname != '\0')
569 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
574 /* fill the "last" array with all -1s, to insure correct updating */
576 i = num_cpustates * num_cpus;
584 * *_memory(stats) - print "Memory: " followed by the memory summary string
586 * Assumptions: cursor is on "lastline"
587 * for i_memory ONLY: cursor is on the previous line
590 static char memory_buffer[MAX_COLS];
595 fputs("\nMem: ", stdout);
598 /* format and print the memory summary */
599 summary_format(memory_buffer, stats, memory_names);
600 fputs(memory_buffer, stdout);
606 static char new[MAX_COLS];
608 /* format the new line */
609 summary_format(new, stats, memory_names);
610 line_update(memory_buffer, new, x_mem, y_mem);
614 * *_arc(stats) - print "ARC: " followed by the ARC summary string
616 * Assumptions: cursor is on "lastline"
617 * for i_arc ONLY: cursor is on the previous line
619 static char arc_buffer[MAX_COLS];
624 if (arc_names == NULL)
627 fputs("\nARC: ", stdout);
630 /* format and print the memory summary */
631 summary_format(arc_buffer, stats, arc_names);
632 fputs(arc_buffer, stdout);
641 static char new[MAX_COLS];
643 if (arc_names == NULL)
646 /* format the new line */
647 summary_format(new, stats, arc_names);
648 line_update(arc_buffer, new, x_arc, y_arc);
653 * *_carc(stats) - print "Compressed ARC: " followed by the summary string
655 * Assumptions: cursor is on "lastline"
656 * for i_carc ONLY: cursor is on the previous line
658 static char carc_buffer[MAX_COLS];
663 if (carc_names == NULL)
666 fputs("\n ", stdout);
669 /* format and print the memory summary */
670 summary_format(carc_buffer, stats, carc_names);
671 fputs(carc_buffer, stdout);
680 static char new[MAX_COLS];
682 if (carc_names == NULL)
685 /* format the new line */
686 summary_format(new, stats, carc_names);
687 line_update(carc_buffer, new, x_carc, y_carc);
691 * *_swap(stats) - print "Swap: " followed by the swap summary string
693 * Assumptions: cursor is on "lastline"
694 * for i_swap ONLY: cursor is on the previous line
697 static char swap_buffer[MAX_COLS];
702 fputs("\nSwap: ", stdout);
705 /* format and print the swap summary */
706 summary_format(swap_buffer, stats, swap_names);
707 fputs(swap_buffer, stdout);
713 static char new[MAX_COLS];
715 /* format the new line */
716 summary_format(new, stats, swap_names);
717 line_update(swap_buffer, new, x_swap, y_swap);
721 * *_message() - print the next pending message line, or erase the one
724 * Note that u_message is (currently) the same as i_message.
726 * Assumptions: lastline is consistent
730 * i_message is funny because it gets its message asynchronously (with
731 * respect to screen updates).
734 static char next_msg[MAX_COLS + 5];
735 static int msglen = 0;
736 /* Invariant: msglen is always the length of the message currently displayed
737 on the screen (even when next_msg doesn't contain that message). */
743 while (lastline < y_message)
748 if (next_msg[0] != '\0')
750 top_standout(next_msg);
751 msglen = strlen(next_msg);
756 (void) clear_eol(msglen);
768 static int header_length;
771 * Trim a header string to the current display width and return a newly
772 * allocated area with the trimmed header.
785 width = display_width;
786 header_length = strlen(text);
787 if (header_length >= width) {
788 s = malloc((width + 1) * sizeof(char));
791 strncpy(s, text, width);
798 * *_header(text) - print the header for the process area
800 * Assumptions: cursor is on the previous line and lastline is consistent
811 s = trim_header(text);
815 if (header_status == ON)
821 else if (header_status == ERASE)
832 char *text __unused; /* ignored */
836 if (header_status == ERASE)
840 clear_eol(header_length);
846 * *_process(line, thisline) - print one process line
848 * Assumptions: lastline is consistent
852 i_process(line, thisline)
861 /* make sure we are on the correct line */
862 while (lastline < y_procs + line)
868 /* truncate the line to conform to our current screen width */
869 thisline[display_width] = '\0';
871 /* write the line out */
872 fputs(thisline, stdout);
874 /* copy it in to our buffer */
875 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
876 p = strecpy(base, thisline);
878 /* zero fill the rest of it */
879 bzero(p, display_width - (p - base));
883 u_process(line, newline)
890 int screen_line = line + Header_lines;
893 /* remember a pointer to the current line in the screen buffer */
894 bufferline = &screenbuf[lineindex(line)];
896 /* truncate the line to conform to our current screen width */
897 newline[display_width] = '\0';
899 /* is line higher than we went on the last display? */
902 /* yes, just ignore screenbuf and write it out directly */
903 /* get positioned on the correct line */
904 if (screen_line - lastline == 1)
911 Move_to(0, screen_line);
912 lastline = screen_line;
915 /* now write the line */
916 fputs(newline, stdout);
918 /* copy it in to the buffer */
919 optr = strecpy(bufferline, newline);
921 /* zero fill the rest of it */
922 bzero(optr, display_width - (optr - bufferline));
926 line_update(bufferline, newline, 0, line + Header_lines);
936 int screen_line = hi + Header_lines;
943 /* need to blank the remainder of the screen */
944 /* but only if there is any screen left below this line */
945 if (lastline + 1 < screen_length)
947 /* efficiently move to the end of currently displayed info */
948 if (screen_line - lastline < 5)
950 while (lastline < screen_line)
958 Move_to(0, screen_line);
959 lastline = screen_line;
964 /* we can do this the easy way */
965 putcap(clear_to_end);
969 /* use clear_eol on each line */
971 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
980 /* move the cursor to a pleasant place */
981 Move_to(x_idlecursor, y_idlecursor);
982 lastline = y_idlecursor;
986 /* separate this display from the next with some vertical room */
987 fputs("\n\n", stdout);
992 display_header(int t)
999 else if (header_status == ON)
1001 header_status = ERASE;
1006 new_message(int type, char *msgfmt, ...)
1011 va_start(args, msgfmt);
1013 /* first, format the message */
1014 vsnprintf(next_msg, sizeof(next_msg), msgfmt, args);
1020 /* message there already -- can we clear it? */
1023 /* yes -- write it and clear to end */
1024 i = strlen(next_msg);
1025 if ((type & MT_delayed) == 0)
1027 type & MT_standout ? top_standout(next_msg) :
1028 fputs(next_msg, stdout);
1029 (void) clear_eol(msglen - i);
1037 if ((type & MT_delayed) == 0)
1039 type & MT_standout ? top_standout(next_msg) : fputs(next_msg, stdout);
1040 msglen = strlen(next_msg);
1050 if (clear_eol(msglen) == 1)
1057 readline(buffer, size, numeric)
1069 /* allow room for null terminator */
1073 while ((fflush(stdout), read(0, ptr, 1) > 0))
1075 /* newline means we are done */
1076 if ((ch = *ptr) == '\n' || ch == '\r')
1081 /* handle special editing characters */
1084 /* kill line -- account for overstriking */
1090 /* return null string */
1095 else if (ch == ch_erase)
1097 /* erase previous character */
1100 /* none to erase! */
1105 fputs("\b \b", stdout);
1110 /* check for character validity and buffer overflow */
1111 else if (cnt == size || (numeric && !isdigit(ch)) ||
1119 /* echo it and store it in the buffer */
1130 /* all done -- null terminate the string */
1133 /* account for the extra characters in the message area */
1134 /* (if terminal overstrikes, remember the furthest they went) */
1135 msglen += overstrike ? maxcnt : cnt;
1137 /* return either inputted number or string length */
1139 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1142 /* internal support routines */
1144 static int string_count(char **pp)
1149 while (*pp++ != NULL)
1156 static void summary_format(char *str, int *numbers, char **names)
1163 /* format each number followed by its string */
1165 while ((thisname = *names++) != NULL)
1167 /* get the number to format */
1170 /* display only non-zero numbers */
1173 /* is this number in kilobytes? */
1174 if (thisname[0] == 'K')
1176 /* yes: format it as a memory value */
1177 p = strecpy(p, format_k(num));
1179 /* skip over the K, since it was included by format_k */
1180 p = strecpy(p, thisname+1);
1182 /* is this number a ratio? */
1183 else if (thisname[0] == ':')
1185 (void) snprintf(rbuf, sizeof(rbuf), "%.2f",
1186 (float)*(numbers - 2) / (float)num);
1187 p = strecpy(p, rbuf);
1188 p = strecpy(p, thisname);
1192 p = strecpy(p, itoa(num));
1193 p = strecpy(p, thisname);
1197 /* ignore negative numbers, but display corresponding string */
1200 p = strecpy(p, thisname);
1204 /* if the last two characters in the string are ", ", delete them */
1206 if (p >= str && p[0] == ',' && p[1] == ' ')
1212 static void line_update(old, new, start, line)
1222 int newcol = start + 1;
1223 int lastcol = start;
1224 char cursor_on_line = No;
1227 /* compare the two strings and only rewrite what has changed */
1230 fprintf(debug, "line_update, starting at %d\n", start);
1234 fputs("\n-\n", debug);
1237 /* start things off on the right foot */
1238 /* this is to make sure the invariants get set up right */
1239 if ((ch = *new++) != *old)
1241 if (line - lastline == 1 && start == 0)
1247 Move_to(start, line);
1249 cursor_on_line = Yes;
1257 * main loop -- check each character. If the old and new aren't the
1258 * same, then update the display. When the distance from the
1259 * current cursor position to the new change is small enough,
1260 * the characters that belong there are written to move the
1264 * lastcol is the column where the cursor currently is sitting
1265 * (always one beyond the end of the last mismatch).
1267 do /* yes, a do...while */
1269 if ((ch = *new++) != *old)
1271 /* new character is different from old */
1272 /* make sure the cursor is on top of this character */
1273 diff = newcol - lastcol;
1276 /* some motion is required--figure out which is shorter */
1277 if (diff < 6 && cursor_on_line)
1279 /* overwrite old stuff--get it out of the old buffer */
1280 printf("%.*s", diff, ¤t[lastcol-start]);
1284 /* use cursor addressing */
1285 Move_to(newcol, line);
1286 cursor_on_line = Yes;
1288 /* remember where the cursor is */
1289 lastcol = newcol + 1;
1293 /* already there, update position */
1297 /* write what we need to */
1300 /* at the end--terminate with a clear-to-end-of-line */
1301 (void) clear_eol(strlen(old));
1305 /* write the new character */
1308 /* put the new character in the screen buffer */
1312 /* update working column and screen buffer pointer */
1316 } while (ch != '\0');
1318 /* zero out the rest of the line buffer -- MUST BE DONE! */
1319 diff = display_width - newcol;
1325 /* remember where the current line is */
1333 * printable(str) - make the string pointed to by "str" into one that is
1334 * printable (i.e.: all ascii), by converting all non-printable
1335 * characters into '?'. Replacements are done in place and a pointer
1336 * to the original buffer is returned.
1339 char *printable(str)
1348 while ((ch = *ptr) != '\0')
1367 int days, hrs, mins, secs;
1369 if (bt->tv_sec != -1) {
1370 uptime = *tod - bt->tv_sec;
1371 days = uptime / 86400;
1373 hrs = uptime / 3600;
1379 * Display the uptime.
1384 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1390 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);