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.
36 #include "screen.h" /* interface to screen package */
37 #include "layout.h" /* defines for screen position layout */
40 #include "top.local.h"
42 #include "machine.h" /* we should eliminate this!!! */
49 /* imported from screen.c */
50 extern int overstrike;
53 static int last_hi = 0; /* used in u_process and u_endscreen */
54 static int lastline = 0;
55 static int display_width = MAX_COLS;
57 #define lineindex(l) ((l)*display_width)
61 /* things initialized by display_init and used thruout */
63 /* buffer of proc information lines for display updating */
64 char *screenbuf = NULL;
66 static char **procstate_names;
67 static char **cpustate_names;
68 static char **memory_names;
69 static char **swap_names;
71 static int num_procstates;
72 static int num_cpustates;
73 static int num_memory;
76 static int *lprocstates;
77 static int *lcpustates;
82 static int *cpustate_columns;
83 static int cpustate_total_length;
84 static int cpustates_column;
86 static enum { OFF, ON, ERASE } header_status = ON;
88 static int string_count();
89 static void summary_format();
90 static void line_update();
95 int x_loadave_nompid = 15;
108 int x_idlecursor = 0;
109 int y_idlecursor = 5;
113 int Header_lines = 7;
120 /* first, deallocate any previous buffer that may have been there */
121 if (screenbuf != NULL)
126 /* calculate the current dimensions */
127 /* if operating in "dumb" mode, we only need one line */
128 lines = smart_terminal ? screen_length - Header_lines : 1;
132 /* we don't want more than MAX_COLS columns, since the machine-dependent
133 modules make static allocations based on MAX_COLS and we don't want
134 to run off the end of their buffers */
135 display_width = screen_width;
136 if (display_width >= MAX_COLS)
138 display_width = MAX_COLS - 1;
141 /* now, allocate space for the screen buffer */
142 screenbuf = (char *)malloc(lines * display_width);
143 if (screenbuf == (char *)NULL)
149 /* return number of lines available */
150 /* for dumb terminals, pretend like we can show any amount */
151 return(smart_terminal ? lines : Largest);
154 int display_init(statics)
156 struct statics *statics;
164 /* call resize to do the dirty work */
165 lines = display_resize();
166 num_cpus = statics->ncpus;
167 cpustates_column = 5; /* CPU: */
169 cpustates_column += 2; /* CPU 0: */
170 for (i = num_cpus; i > 9; i /= 10)
173 /* only do the rest if we need to */
176 /* save pointers and allocate space for names */
177 procstate_names = statics->procstate_names;
178 num_procstates = string_count(procstate_names);
179 lprocstates = (int *)malloc(num_procstates * sizeof(int));
181 cpustate_names = statics->cpustate_names;
183 swap_names = statics->swap_names;
184 num_swap = string_count(swap_names);
185 lswap = (int *)malloc(num_swap * sizeof(int));
186 num_cpustates = string_count(cpustate_names);
187 lcpustates = (int *)malloc(num_cpustates * sizeof(int) * num_cpus);
188 cpustate_columns = (int *)malloc(num_cpustates * sizeof(int));
190 memory_names = statics->memory_names;
191 num_memory = string_count(memory_names);
192 lmemory = (int *)malloc(num_memory * sizeof(int));
194 /* calculate starting columns where needed */
195 cpustate_total_length = 0;
197 ip = cpustate_columns;
200 *ip++ = cpustate_total_length;
201 if ((i = strlen(*pp++)) > 0)
203 cpustate_total_length += i + 8;
208 /* return number of lines available */
212 i_loadave(mpid, avenrun)
220 /* i_loadave also clears the screen, since it is first */
223 /* mpid == -1 implies this system doesn't have an _mpid */
226 printf("last pid: %5d; ", mpid);
229 printf("load averages");
231 for (i = 0; i < 3; i++)
240 u_loadave(mpid, avenrun)
250 /* change screen only when value has really changed */
253 Move_to(x_lastpid, y_lastpid);
258 /* i remembers x coordinate to move to */
263 i = x_loadave_nompid;
266 /* move into position for load averages */
267 Move_to(i, y_loadave);
269 /* display new load averages */
270 /* we should optimize this and only display changes */
271 for (i = 0; i < 3; i++)
285 * Display the current time.
286 * "ctime" always returns a string that looks like this:
288 * Sun Sep 16 01:03:52 1973
289 * 012345678901234567890123
292 * We want indices 11 thru 18 (length 8).
297 Move_to(screen_width - 8, 0);
310 printf("%-8.8s\n", &(ctime(tod)[11]));
314 static int ltotal = 0;
315 static char procstates_buffer[MAX_COLS];
318 * *_procstates(total, brkdn, names) - print the process summary line
320 * Assumptions: cursor is at the beginning of the line on entry
324 i_procstates(total, brkdn)
332 /* write current number of processes and remember the value */
333 printf("%d processes:", total);
336 /* put out enough spaces to get to column 15 */
343 /* format and print the process state summary */
344 summary_format(procstates_buffer, brkdn, procstate_names);
345 fputs(procstates_buffer, stdout);
347 /* save the numbers for next time */
348 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
351 u_procstates(total, brkdn)
357 static char new[MAX_COLS];
360 /* update number of processes only if it has changed */
363 /* move and overwrite */
364 #if (x_procstate == 0)
365 Move_to(x_procstate, y_procstate);
367 /* cursor is already there...no motion needed */
368 /* assert(lastline == 1); */
372 /* if number of digits differs, rewrite the label */
373 if (digits(total) != digits(ltotal))
375 fputs(" processes:", stdout);
376 /* put out enough spaces to get to column 15 */
382 /* cursor may end up right where we want it!!! */
389 /* see if any of the state numbers has changed */
390 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
392 /* format and update the line */
393 summary_format(new, brkdn, procstate_names);
394 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
395 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
401 * *_cpustates(states, names) - print the cpu state percentages
403 * Assumptions: cursor is on the PREVIOUS line
406 /* cpustates_tag() calculates the correct tag to use to label the line */
408 char *cpustates_tag()
413 static char *short_tag = "CPU: ";
414 static char *long_tag = "CPU states: ";
416 /* if length + strlen(long_tag) >= screen_width, then we have to
417 use the shorter tag (we subtract 2 to account for ": ") */
418 if (cpustate_total_length + (int)strlen(long_tag) - 2 >= screen_width)
427 /* set cpustates_column accordingly then return result */
428 cpustates_column = strlen(use);
435 register int *states;
440 register char **names;
441 register char *thisname;
444 for (cpu = 0; cpu < num_cpus; cpu++) {
445 names = cpustate_names;
447 /* print tag and bump lastline */
451 printf("\nCPU %d: ", cpu);
454 /* now walk thru the names and print the line */
455 Move_to(cpustates_column, y_cpustates + cpu);
456 while ((thisname = *names++) != NULL)
458 if (*thisname != '\0')
460 /* retrieve the value and remember it */
463 /* if percentage is >= 1000, print it as 100% */
464 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
465 (i++ % num_cpustates) == 0 ? "" : ", ",
472 /* copy over values into "last" array */
473 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
478 register int *states;
482 register char **names;
483 register char *thisname;
488 for (cpu = 0; cpu < num_cpus; cpu++) {
489 names = cpustate_names;
491 Move_to(cpustates_column, y_cpustates + cpu);
492 lastline = y_cpustates + cpu;
493 lp = lcpustates + (cpu * num_cpustates);
494 colp = cpustate_columns;
496 /* we could be much more optimal about this */
497 while ((thisname = *names++) != NULL)
499 if (*thisname != '\0')
501 /* did the value change since last time? */
504 /* yes, move and change */
505 Move_to(cpustates_column + *colp, y_cpustates + cpu);
506 lastline = y_cpustates + cpu;
508 /* retrieve value and remember it */
511 /* if percentage is >= 1000, print it as 100% */
512 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
513 ((double)value)/10.);
515 /* remember it for next time */
520 /* increment and move on */
532 register char **names;
533 register char *thisname;
537 for (cpu = 0; cpu < num_cpus; cpu++) {
538 names = cpustate_names;
540 /* show tag and bump lastline */
544 printf("\nCPU %d: ", cpu);
547 Move_to(cpustates_column, y_cpustates + cpu);
548 while ((thisname = *names++) != NULL)
550 if (*thisname != '\0')
552 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
557 /* fill the "last" array with all -1s, to insure correct updating */
559 i = num_cpustates * num_cpus;
567 * *_memory(stats) - print "Memory: " followed by the memory summary string
569 * Assumptions: cursor is on "lastline"
570 * for i_memory ONLY: cursor is on the previous line
573 char memory_buffer[MAX_COLS];
580 fputs("\nMem: ", stdout);
583 /* format and print the memory summary */
584 summary_format(memory_buffer, stats, memory_names);
585 fputs(memory_buffer, stdout);
593 static char new[MAX_COLS];
595 /* format the new line */
596 summary_format(new, stats, memory_names);
597 line_update(memory_buffer, new, x_mem, y_mem);
601 * *_swap(stats) - print "Swap: " followed by the swap summary string
603 * Assumptions: cursor is on "lastline"
604 * for i_swap ONLY: cursor is on the previous line
607 char swap_buffer[MAX_COLS];
614 fputs("\nSwap: ", stdout);
617 /* format and print the swap summary */
618 summary_format(swap_buffer, stats, swap_names);
619 fputs(swap_buffer, stdout);
627 static char new[MAX_COLS];
629 /* format the new line */
630 summary_format(new, stats, swap_names);
631 line_update(swap_buffer, new, x_swap, y_swap);
635 * *_message() - print the next pending message line, or erase the one
638 * Note that u_message is (currently) the same as i_message.
640 * Assumptions: lastline is consistent
644 * i_message is funny because it gets its message asynchronously (with
645 * respect to screen updates).
648 static char next_msg[MAX_COLS + 5];
649 static int msglen = 0;
650 /* Invariant: msglen is always the length of the message currently displayed
651 on the screen (even when next_msg doesn't contain that message). */
656 while (lastline < y_message)
661 if (next_msg[0] != '\0')
664 msglen = strlen(next_msg);
669 (void) clear_eol(msglen);
680 static int header_length;
683 * Trim a header string to the current display width and return a newly
684 * allocated area with the trimmed header.
697 width = screen_width;
698 header_length = strlen(text);
699 if (header_length >= width) {
700 s = malloc((width + 1) * sizeof(char));
703 strncpy(s, text, width);
706 s = malloc((width + 1) * sizeof(char));
709 strncpy(s, text, width);
710 while (screen_width > header_length)
711 s[header_length++] = ' ';
718 * *_header(text) - print the header for the process area
720 * Assumptions: cursor is on the previous line and lastline is consistent
730 s = trim_header(text);
734 if (header_status == ON)
737 standout(text, stdout);
740 else if (header_status == ERASE)
750 char *text; /* ignored */
754 if (header_status == ERASE)
758 clear_eol(header_length);
764 * *_process(line, thisline) - print one process line
766 * Assumptions: lastline is consistent
769 i_process(line, thisline)
778 /* make sure we are on the correct line */
779 while (lastline < y_procs + line)
785 /* truncate the line to conform to our current screen width */
786 thisline[display_width] = '\0';
788 /* write the line out */
789 fputs(thisline, stdout);
791 /* copy it in to our buffer */
792 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
793 p = strecpy(base, thisline);
795 /* zero fill the rest of it */
796 memzero(p, display_width - (p - base));
799 u_process(line, newline)
806 register int screen_line = line + Header_lines;
807 register char *bufferline;
809 /* remember a pointer to the current line in the screen buffer */
810 bufferline = &screenbuf[lineindex(line)];
812 /* truncate the line to conform to our current screen width */
813 newline[display_width] = '\0';
815 /* is line higher than we went on the last display? */
818 /* yes, just ignore screenbuf and write it out directly */
819 /* get positioned on the correct line */
820 if (screen_line - lastline == 1)
827 Move_to(0, screen_line);
828 lastline = screen_line;
831 /* now write the line */
832 fputs(newline, stdout);
834 /* copy it in to the buffer */
835 optr = strecpy(bufferline, newline);
837 /* zero fill the rest of it */
838 memzero(optr, display_width - (optr - bufferline));
842 line_update(bufferline, newline, 0, line + Header_lines);
851 register int screen_line = hi + Header_lines;
858 /* need to blank the remainder of the screen */
859 /* but only if there is any screen left below this line */
860 if (lastline + 1 < screen_length)
862 /* efficiently move to the end of currently displayed info */
863 if (screen_line - lastline < 5)
865 while (lastline < screen_line)
873 Move_to(0, screen_line);
874 lastline = screen_line;
879 /* we can do this the easy way */
880 putcap(clear_to_end);
884 /* use clear_eol on each line */
886 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
895 /* move the cursor to a pleasant place */
896 Move_to(x_idlecursor, y_idlecursor);
897 lastline = y_idlecursor;
901 /* separate this display from the next with some vertical room */
902 fputs("\n\n", stdout);
915 else if (header_status == ON)
917 header_status = ERASE;
922 new_message(type, msgfmt, a1, a2, a3)
931 /* first, format the message */
932 (void) snprintf(next_msg, sizeof(next_msg), msgfmt, a1, a2, a3);
936 /* message there already -- can we clear it? */
939 /* yes -- write it and clear to end */
940 i = strlen(next_msg);
941 if ((type & MT_delayed) == 0)
943 type & MT_standout ? standout(next_msg) :
944 fputs(next_msg, stdout);
945 (void) clear_eol(msglen - i);
953 if ((type & MT_delayed) == 0)
955 type & MT_standout ? standout(next_msg) : fputs(next_msg, stdout);
956 msglen = strlen(next_msg);
965 if (clear_eol(msglen) == 1)
971 readline(buffer, size, numeric)
978 register char *ptr = buffer;
980 register char cnt = 0;
981 register char maxcnt = 0;
983 /* allow room for null terminator */
987 while ((fflush(stdout), read(0, ptr, 1) > 0))
989 /* newline means we are done */
990 if ((ch = *ptr) == '\n' || ch == '\r')
995 /* handle special editing characters */
998 /* kill line -- account for overstriking */
1004 /* return null string */
1009 else if (ch == ch_erase)
1011 /* erase previous character */
1014 /* none to erase! */
1019 fputs("\b \b", stdout);
1024 /* check for character validity and buffer overflow */
1025 else if (cnt == size || (numeric && !isdigit(ch)) ||
1033 /* echo it and store it in the buffer */
1044 /* all done -- null terminate the string */
1047 /* account for the extra characters in the message area */
1048 /* (if terminal overstrikes, remember the furthest they went) */
1049 msglen += overstrike ? maxcnt : cnt;
1051 /* return either inputted number or string length */
1053 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1056 /* internal support routines */
1058 static int string_count(pp)
1066 while (*pp++ != NULL)
1073 static void summary_format(str, numbers, names)
1077 register char **names;
1082 register char *thisname;
1083 register int useM = No;
1085 /* format each number followed by its string */
1087 while ((thisname = *names++) != NULL)
1089 /* get the number to format */
1092 /* display only non-zero numbers */
1095 /* is this number in kilobytes? */
1096 if (thisname[0] == 'K')
1098 /* yes: format it as a memory value */
1099 p = strecpy(p, format_k(num));
1101 /* skip over the K, since it was included by format_k */
1102 p = strecpy(p, thisname+1);
1106 p = strecpy(p, itoa(num));
1107 p = strecpy(p, thisname);
1111 /* ignore negative numbers, but display corresponding string */
1114 p = strecpy(p, thisname);
1118 /* if the last two characters in the string are ", ", delete them */
1120 if (p >= str && p[0] == ',' && p[1] == ' ')
1126 static void line_update(old, new, start, line)
1136 register int newcol = start + 1;
1137 register int lastcol = start;
1138 char cursor_on_line = No;
1141 /* compare the two strings and only rewrite what has changed */
1144 fprintf(debug, "line_update, starting at %d\n", start);
1148 fputs("\n-\n", debug);
1151 /* start things off on the right foot */
1152 /* this is to make sure the invariants get set up right */
1153 if ((ch = *new++) != *old)
1155 if (line - lastline == 1 && start == 0)
1161 Move_to(start, line);
1163 cursor_on_line = Yes;
1171 * main loop -- check each character. If the old and new aren't the
1172 * same, then update the display. When the distance from the
1173 * current cursor position to the new change is small enough,
1174 * the characters that belong there are written to move the
1178 * lastcol is the column where the cursor currently is sitting
1179 * (always one beyond the end of the last mismatch).
1181 do /* yes, a do...while */
1183 if ((ch = *new++) != *old)
1185 /* new character is different from old */
1186 /* make sure the cursor is on top of this character */
1187 diff = newcol - lastcol;
1190 /* some motion is required--figure out which is shorter */
1191 if (diff < 6 && cursor_on_line)
1193 /* overwrite old stuff--get it out of the old buffer */
1194 printf("%.*s", diff, ¤t[lastcol-start]);
1198 /* use cursor addressing */
1199 Move_to(newcol, line);
1200 cursor_on_line = Yes;
1202 /* remember where the cursor is */
1203 lastcol = newcol + 1;
1207 /* already there, update position */
1211 /* write what we need to */
1214 /* at the end--terminate with a clear-to-end-of-line */
1215 (void) clear_eol(strlen(old));
1219 /* write the new character */
1222 /* put the new character in the screen buffer */
1226 /* update working column and screen buffer pointer */
1230 } while (ch != '\0');
1232 /* zero out the rest of the line buffer -- MUST BE DONE! */
1233 diff = display_width - newcol;
1239 /* remember where the current line is */
1247 * printable(str) - make the string pointed to by "str" into one that is
1248 * printable (i.e.: all ascii), by converting all non-printable
1249 * characters into '?'. Replacements are done in place and a pointer
1250 * to the original buffer is returned.
1253 char *printable(str)
1262 while ((ch = *ptr) != '\0')
1280 int days, hrs, mins, secs;
1282 if (bt->tv_sec != -1) {
1283 uptime = *tod - bt->tv_sec;
1284 days = uptime / 86400;
1286 hrs = uptime / 3600;
1292 * Display the uptime.
1297 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1303 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);