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.
39 #include "screen.h" /* interface to screen package */
40 #include "layout.h" /* defines for screen position layout */
43 #include "top.local.h"
45 #include "machine.h" /* we should eliminate this!!! */
52 /* imported from screen.c */
53 extern int overstrike;
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 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 **swap_names;
74 static int num_procstates;
75 static int num_cpustates;
76 static int num_memory;
79 static int *lprocstates;
80 static int *lcpustates;
85 static int *cpustate_columns;
86 static int cpustate_total_length;
87 static int cpustates_column;
89 static enum { OFF, ON, ERASE } header_status = ON;
91 static int string_count();
92 static void summary_format();
93 static void line_update();
98 int x_loadave_nompid = 15;
113 int x_idlecursor = 0;
114 int y_idlecursor = 5;
118 int Header_lines = 7;
125 /* first, deallocate any previous buffer that may have been there */
126 if (screenbuf != NULL)
131 /* calculate the current dimensions */
132 /* if operating in "dumb" mode, we only need one line */
133 lines = smart_terminal ? screen_length - Header_lines : 1;
137 /* we don't want more than MAX_COLS columns, since the machine-dependent
138 modules make static allocations based on MAX_COLS and we don't want
139 to run off the end of their buffers */
140 display_width = screen_width;
141 if (display_width >= MAX_COLS)
143 display_width = MAX_COLS - 1;
146 /* now, allocate space for the screen buffer */
147 screenbuf = (char *)malloc(lines * display_width);
148 if (screenbuf == (char *)NULL)
154 /* return number of lines available */
155 /* for dumb terminals, pretend like we can show any amount */
156 return(smart_terminal ? lines : Largest);
159 int display_updatecpus(statics)
161 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(statics)
193 struct statics *statics;
201 lines = display_updatecpus(statics);
203 /* only do the rest if we need to */
206 /* save pointers and allocate space for names */
207 procstate_names = statics->procstate_names;
208 num_procstates = string_count(procstate_names);
209 lprocstates = (int *)malloc(num_procstates * sizeof(int));
211 cpustate_names = statics->cpustate_names;
213 swap_names = statics->swap_names;
214 num_swap = string_count(swap_names);
215 lswap = (int *)malloc(num_swap * sizeof(int));
216 num_cpustates = string_count(cpustate_names);
217 lcpustates = (int *)malloc(num_cpustates * sizeof(int) * statics->ncpus);
218 cpustate_columns = (int *)malloc(num_cpustates * sizeof(int));
220 memory_names = statics->memory_names;
221 num_memory = string_count(memory_names);
222 lmemory = (int *)malloc(num_memory * sizeof(int));
224 arc_names = statics->arc_names;
226 /* calculate starting columns where needed */
227 cpustate_total_length = 0;
229 ip = cpustate_columns;
232 *ip++ = cpustate_total_length;
233 if ((i = strlen(*pp++)) > 0)
235 cpustate_total_length += i + 8;
240 /* return number of lines available */
245 i_loadave(mpid, avenrun)
253 /* i_loadave also clears the screen, since it is first */
256 /* mpid == -1 implies this system doesn't have an _mpid */
259 printf("last pid: %5d; ", mpid);
262 printf("load averages");
264 for (i = 0; i < 3; i++)
274 u_loadave(mpid, avenrun)
284 /* change screen only when value has really changed */
287 Move_to(x_lastpid, y_lastpid);
292 /* i remembers x coordinate to move to */
297 i = x_loadave_nompid;
300 /* move into position for load averages */
301 Move_to(i, y_loadave);
303 /* display new load averages */
304 /* we should optimize this and only display changes */
305 for (i = 0; i < 3; i++)
320 * Display the current time.
321 * "ctime" always returns a string that looks like this:
323 * Sun Sep 16 01:03:52 1973
324 * 012345678901234567890123
327 * We want indices 11 thru 18 (length 8).
332 Move_to(screen_width - 8, 0);
345 printf("%-8.8s\n", &(ctime(tod)[11]));
349 static int ltotal = 0;
350 static char procstates_buffer[MAX_COLS];
353 * *_procstates(total, brkdn, names) - print the process summary line
355 * Assumptions: cursor is at the beginning of the line on entry
360 i_procstates(total, brkdn)
368 /* write current number of processes and remember the value */
369 printf("%d processes:", total);
372 /* put out enough spaces to get to column 15 */
379 /* format and print the process state summary */
380 summary_format(procstates_buffer, brkdn, procstate_names);
381 fputs(procstates_buffer, stdout);
383 /* save the numbers for next time */
384 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
388 u_procstates(total, brkdn)
394 static char new[MAX_COLS];
397 /* update number of processes only if it has changed */
400 /* move and overwrite */
401 #if (x_procstate == 0)
402 Move_to(x_procstate, y_procstate);
404 /* cursor is already there...no motion needed */
405 /* assert(lastline == 1); */
409 /* if number of digits differs, rewrite the label */
410 if (digits(total) != digits(ltotal))
412 fputs(" processes:", stdout);
413 /* put out enough spaces to get to column 15 */
419 /* cursor may end up right where we want it!!! */
426 /* see if any of the state numbers has changed */
427 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
429 /* format and update the line */
430 summary_format(new, brkdn, procstate_names);
431 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
432 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
438 * *_cpustates(states, names) - print the cpu state percentages
440 * Assumptions: cursor is on the PREVIOUS line
443 /* cpustates_tag() calculates the correct tag to use to label the line */
445 char *cpustates_tag()
450 static char *short_tag = "CPU: ";
451 static char *long_tag = "CPU states: ";
453 /* if length + strlen(long_tag) >= screen_width, then we have to
454 use the shorter tag (we subtract 2 to account for ": ") */
455 if (cpustate_total_length + (int)strlen(long_tag) - 2 >= screen_width)
464 /* set cpustates_column accordingly then return result */
465 cpustates_column = strlen(use);
478 register char **names;
479 register char *thisname;
482 for (cpu = 0; cpu < num_cpus; cpu++) {
483 names = cpustate_names;
485 /* print tag and bump lastline */
489 value = printf("\nCPU %d: ", cpu);
490 while (value++ <= cpustates_column)
495 /* now walk thru the names and print the line */
496 while ((thisname = *names++) != NULL)
498 if (*thisname != '\0')
500 /* retrieve the value and remember it */
503 /* if percentage is >= 1000, print it as 100% */
504 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
505 (i++ % num_cpustates) == 0 ? "" : ", ",
512 /* copy over values into "last" array */
513 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
523 register char **names;
524 register char *thisname;
529 for (cpu = 0; cpu < num_cpus; cpu++) {
530 names = cpustate_names;
532 Move_to(cpustates_column, y_cpustates + cpu);
533 lastline = y_cpustates + cpu;
534 lp = lcpustates + (cpu * num_cpustates);
535 colp = cpustate_columns;
537 /* we could be much more optimal about this */
538 while ((thisname = *names++) != NULL)
540 if (*thisname != '\0')
542 /* did the value change since last time? */
545 /* yes, move and change */
546 Move_to(cpustates_column + *colp, y_cpustates + cpu);
547 lastline = y_cpustates + cpu;
549 /* retrieve value and remember it */
552 /* if percentage is >= 1000, print it as 100% */
553 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
554 ((double)value)/10.);
556 /* remember it for next time */
561 /* increment and move on */
574 register char **names;
575 register char *thisname;
579 for (cpu = 0; cpu < num_cpus; cpu++) {
580 names = cpustate_names;
582 /* show tag and bump lastline */
586 value = printf("\nCPU %d: ", cpu);
587 while (value++ <= cpustates_column)
592 while ((thisname = *names++) != NULL)
594 if (*thisname != '\0')
596 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
601 /* fill the "last" array with all -1s, to insure correct updating */
603 i = num_cpustates * num_cpus;
611 * *_memory(stats) - print "Memory: " followed by the memory summary string
613 * Assumptions: cursor is on "lastline"
614 * for i_memory ONLY: cursor is on the previous line
617 char memory_buffer[MAX_COLS];
625 fputs("\nMem: ", stdout);
628 /* format and print the memory summary */
629 summary_format(memory_buffer, stats, memory_names);
630 fputs(memory_buffer, stdout);
639 static char new[MAX_COLS];
641 /* format the new line */
642 summary_format(new, stats, memory_names);
643 line_update(memory_buffer, new, x_mem, y_mem);
647 * *_arc(stats) - print "ARC: " followed by the ARC summary string
649 * Assumptions: cursor is on "lastline"
650 * for i_arc ONLY: cursor is on the previous line
652 char arc_buffer[MAX_COLS];
660 if (arc_names == NULL)
663 fputs("\nARC: ", stdout);
666 /* format and print the memory summary */
667 summary_format(arc_buffer, stats, arc_names);
668 fputs(arc_buffer, stdout);
677 static char new[MAX_COLS];
679 if (arc_names == NULL)
682 /* format the new line */
683 summary_format(new, stats, arc_names);
684 line_update(arc_buffer, new, x_arc, y_arc);
689 * *_swap(stats) - print "Swap: " followed by the swap summary string
691 * Assumptions: cursor is on "lastline"
692 * for i_swap ONLY: cursor is on the previous line
695 char swap_buffer[MAX_COLS];
703 fputs("\nSwap: ", stdout);
706 /* format and print the swap summary */
707 summary_format(swap_buffer, stats, swap_names);
708 fputs(swap_buffer, stdout);
717 static char new[MAX_COLS];
719 /* format the new line */
720 summary_format(new, stats, swap_names);
721 line_update(swap_buffer, new, x_swap, y_swap);
725 * *_message() - print the next pending message line, or erase the one
728 * Note that u_message is (currently) the same as i_message.
730 * Assumptions: lastline is consistent
734 * i_message is funny because it gets its message asynchronously (with
735 * respect to screen updates).
738 static char next_msg[MAX_COLS + 5];
739 static int msglen = 0;
740 /* Invariant: msglen is always the length of the message currently displayed
741 on the screen (even when next_msg doesn't contain that message). */
747 while (lastline < y_message)
752 if (next_msg[0] != '\0')
755 msglen = strlen(next_msg);
760 (void) clear_eol(msglen);
772 static int header_length;
775 * Trim a header string to the current display width and return a newly
776 * allocated area with the trimmed header.
789 width = display_width;
790 header_length = strlen(text);
791 if (header_length >= width) {
792 s = malloc((width + 1) * sizeof(char));
795 strncpy(s, text, width);
802 * *_header(text) - print the header for the process area
804 * Assumptions: cursor is on the previous line and lastline is consistent
815 s = trim_header(text);
819 if (header_status == ON)
825 else if (header_status == ERASE)
836 char *text __unused; /* ignored */
840 if (header_status == ERASE)
844 clear_eol(header_length);
850 * *_process(line, thisline) - print one process line
852 * Assumptions: lastline is consistent
856 i_process(line, thisline)
865 /* make sure we are on the correct line */
866 while (lastline < y_procs + line)
872 /* truncate the line to conform to our current screen width */
873 thisline[display_width] = '\0';
875 /* write the line out */
876 fputs(thisline, stdout);
878 /* copy it in to our buffer */
879 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
880 p = strecpy(base, thisline);
882 /* zero fill the rest of it */
883 memzero(p, display_width - (p - base));
887 u_process(line, newline)
894 register int screen_line = line + Header_lines;
895 register char *bufferline;
897 /* remember a pointer to the current line in the screen buffer */
898 bufferline = &screenbuf[lineindex(line)];
900 /* truncate the line to conform to our current screen width */
901 newline[display_width] = '\0';
903 /* is line higher than we went on the last display? */
906 /* yes, just ignore screenbuf and write it out directly */
907 /* get positioned on the correct line */
908 if (screen_line - lastline == 1)
915 Move_to(0, screen_line);
916 lastline = screen_line;
919 /* now write the line */
920 fputs(newline, stdout);
922 /* copy it in to the buffer */
923 optr = strecpy(bufferline, newline);
925 /* zero fill the rest of it */
926 memzero(optr, display_width - (optr - bufferline));
930 line_update(bufferline, newline, 0, line + Header_lines);
940 register int screen_line = hi + Header_lines;
947 /* need to blank the remainder of the screen */
948 /* but only if there is any screen left below this line */
949 if (lastline + 1 < screen_length)
951 /* efficiently move to the end of currently displayed info */
952 if (screen_line - lastline < 5)
954 while (lastline < screen_line)
962 Move_to(0, screen_line);
963 lastline = screen_line;
968 /* we can do this the easy way */
969 putcap(clear_to_end);
973 /* use clear_eol on each line */
975 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
984 /* move the cursor to a pleasant place */
985 Move_to(x_idlecursor, y_idlecursor);
986 lastline = y_idlecursor;
990 /* separate this display from the next with some vertical room */
991 fputs("\n\n", stdout);
1005 else if (header_status == ON)
1007 header_status = ERASE;
1013 new_message(type, msgfmt, a1, a2, a3)
1022 /* first, format the message */
1023 (void) snprintf(next_msg, sizeof(next_msg), msgfmt, a1, a2, a3);
1027 /* message there already -- can we clear it? */
1030 /* yes -- write it and clear to end */
1031 i = strlen(next_msg);
1032 if ((type & MT_delayed) == 0)
1034 type & MT_standout ? standout(next_msg) :
1035 fputs(next_msg, stdout);
1036 (void) clear_eol(msglen - i);
1044 if ((type & MT_delayed) == 0)
1046 type & MT_standout ? standout(next_msg) : fputs(next_msg, stdout);
1047 msglen = strlen(next_msg);
1057 if (clear_eol(msglen) == 1)
1064 readline(buffer, size, numeric)
1071 register char *ptr = buffer;
1073 register char cnt = 0;
1074 register char maxcnt = 0;
1076 /* allow room for null terminator */
1080 while ((fflush(stdout), read(0, ptr, 1) > 0))
1082 /* newline means we are done */
1083 if ((ch = *ptr) == '\n' || ch == '\r')
1088 /* handle special editing characters */
1091 /* kill line -- account for overstriking */
1097 /* return null string */
1102 else if (ch == ch_erase)
1104 /* erase previous character */
1107 /* none to erase! */
1112 fputs("\b \b", stdout);
1117 /* check for character validity and buffer overflow */
1118 else if (cnt == size || (numeric && !isdigit(ch)) ||
1126 /* echo it and store it in the buffer */
1137 /* all done -- null terminate the string */
1140 /* account for the extra characters in the message area */
1141 /* (if terminal overstrikes, remember the furthest they went) */
1142 msglen += overstrike ? maxcnt : cnt;
1144 /* return either inputted number or string length */
1146 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1149 /* internal support routines */
1151 static int string_count(pp)
1159 while (*pp++ != NULL)
1166 static void summary_format(str, numbers, names)
1170 register char **names;
1175 register char *thisname;
1176 register int useM = No;
1178 /* format each number followed by its string */
1180 while ((thisname = *names++) != NULL)
1182 /* get the number to format */
1185 /* display only non-zero numbers */
1188 /* is this number in kilobytes? */
1189 if (thisname[0] == 'K')
1191 /* yes: format it as a memory value */
1192 p = strecpy(p, format_k(num));
1194 /* skip over the K, since it was included by format_k */
1195 p = strecpy(p, thisname+1);
1199 p = strecpy(p, itoa(num));
1200 p = strecpy(p, thisname);
1204 /* ignore negative numbers, but display corresponding string */
1207 p = strecpy(p, thisname);
1211 /* if the last two characters in the string are ", ", delete them */
1213 if (p >= str && p[0] == ',' && p[1] == ' ')
1219 static void line_update(old, new, start, line)
1229 register int newcol = start + 1;
1230 register int lastcol = start;
1231 char cursor_on_line = No;
1234 /* compare the two strings and only rewrite what has changed */
1237 fprintf(debug, "line_update, starting at %d\n", start);
1241 fputs("\n-\n", debug);
1244 /* start things off on the right foot */
1245 /* this is to make sure the invariants get set up right */
1246 if ((ch = *new++) != *old)
1248 if (line - lastline == 1 && start == 0)
1254 Move_to(start, line);
1256 cursor_on_line = Yes;
1264 * main loop -- check each character. If the old and new aren't the
1265 * same, then update the display. When the distance from the
1266 * current cursor position to the new change is small enough,
1267 * the characters that belong there are written to move the
1271 * lastcol is the column where the cursor currently is sitting
1272 * (always one beyond the end of the last mismatch).
1274 do /* yes, a do...while */
1276 if ((ch = *new++) != *old)
1278 /* new character is different from old */
1279 /* make sure the cursor is on top of this character */
1280 diff = newcol - lastcol;
1283 /* some motion is required--figure out which is shorter */
1284 if (diff < 6 && cursor_on_line)
1286 /* overwrite old stuff--get it out of the old buffer */
1287 printf("%.*s", diff, ¤t[lastcol-start]);
1291 /* use cursor addressing */
1292 Move_to(newcol, line);
1293 cursor_on_line = Yes;
1295 /* remember where the cursor is */
1296 lastcol = newcol + 1;
1300 /* already there, update position */
1304 /* write what we need to */
1307 /* at the end--terminate with a clear-to-end-of-line */
1308 (void) clear_eol(strlen(old));
1312 /* write the new character */
1315 /* put the new character in the screen buffer */
1319 /* update working column and screen buffer pointer */
1323 } while (ch != '\0');
1325 /* zero out the rest of the line buffer -- MUST BE DONE! */
1326 diff = display_width - newcol;
1332 /* remember where the current line is */
1340 * printable(str) - make the string pointed to by "str" into one that is
1341 * printable (i.e.: all ascii), by converting all non-printable
1342 * characters into '?'. Replacements are done in place and a pointer
1343 * to the original buffer is returned.
1346 char *printable(str)
1355 while ((ch = *ptr) != '\0')
1374 int days, hrs, mins, secs;
1376 if (bt->tv_sec != -1) {
1377 uptime = *tod - bt->tv_sec;
1378 days = uptime / 86400;
1380 hrs = uptime / 3600;
1386 * Display the uptime.
1391 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1397 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);