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 **arc_names;
70 static char **swap_names;
72 static int num_procstates;
73 static int num_cpustates;
74 static int num_memory;
77 static int *lprocstates;
78 static int *lcpustates;
83 static int *cpustate_columns;
84 static int cpustate_total_length;
85 static int cpustates_column;
87 static enum { OFF, ON, ERASE } header_status = ON;
89 static int string_count();
90 static void summary_format();
91 static void line_update();
96 int x_loadave_nompid = 15;
111 int x_idlecursor = 0;
112 int y_idlecursor = 5;
116 int Header_lines = 7;
123 /* first, deallocate any previous buffer that may have been there */
124 if (screenbuf != NULL)
129 /* calculate the current dimensions */
130 /* if operating in "dumb" mode, we only need one line */
131 lines = smart_terminal ? screen_length - Header_lines : 1;
135 /* we don't want more than MAX_COLS columns, since the machine-dependent
136 modules make static allocations based on MAX_COLS and we don't want
137 to run off the end of their buffers */
138 display_width = screen_width;
139 if (display_width >= MAX_COLS)
141 display_width = MAX_COLS - 1;
144 /* now, allocate space for the screen buffer */
145 screenbuf = (char *)malloc(lines * display_width);
146 if (screenbuf == (char *)NULL)
152 /* return number of lines available */
153 /* for dumb terminals, pretend like we can show any amount */
154 return(smart_terminal ? lines : Largest);
157 int display_updatecpus(statics)
159 struct statics *statics;
166 /* call resize to do the dirty work */
167 lines = display_resize();
169 num_cpus = statics->ncpus;
172 cpustates_column = 5; /* CPU: */
174 cpustates_column += 2; /* CPU 0: */
175 for (i = num_cpus; i > 9; i /= 10)
178 /* fill the "last" array with all -1s, to insure correct updating */
180 i = num_cpustates * num_cpus;
189 int display_init(statics)
191 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 lprocstates = (int *)malloc(num_procstates * sizeof(int));
209 cpustate_names = statics->cpustate_names;
211 swap_names = statics->swap_names;
212 num_swap = string_count(swap_names);
213 lswap = (int *)malloc(num_swap * sizeof(int));
214 num_cpustates = string_count(cpustate_names);
215 lcpustates = (int *)malloc(num_cpustates * sizeof(int) * statics->ncpus);
216 cpustate_columns = (int *)malloc(num_cpustates * sizeof(int));
218 memory_names = statics->memory_names;
219 num_memory = string_count(memory_names);
220 lmemory = (int *)malloc(num_memory * sizeof(int));
222 arc_names = statics->arc_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 */
242 i_loadave(mpid, avenrun)
250 /* i_loadave also clears the screen, since it is first */
253 /* mpid == -1 implies this system doesn't have an _mpid */
256 printf("last pid: %5d; ", mpid);
259 printf("load averages");
261 for (i = 0; i < 3; i++)
270 u_loadave(mpid, avenrun)
280 /* change screen only when value has really changed */
283 Move_to(x_lastpid, y_lastpid);
288 /* i remembers x coordinate to move to */
293 i = x_loadave_nompid;
296 /* move into position for load averages */
297 Move_to(i, y_loadave);
299 /* display new load averages */
300 /* we should optimize this and only display changes */
301 for (i = 0; i < 3; i++)
315 * Display the current time.
316 * "ctime" always returns a string that looks like this:
318 * Sun Sep 16 01:03:52 1973
319 * 012345678901234567890123
322 * We want indices 11 thru 18 (length 8).
327 Move_to(screen_width - 8, 0);
340 printf("%-8.8s\n", &(ctime(tod)[11]));
344 static int ltotal = 0;
345 static char procstates_buffer[MAX_COLS];
348 * *_procstates(total, brkdn, names) - print the process summary line
350 * Assumptions: cursor is at the beginning of the line on entry
354 i_procstates(total, brkdn)
362 /* write current number of processes and remember the value */
363 printf("%d processes:", total);
366 /* put out enough spaces to get to column 15 */
373 /* format and print the process state summary */
374 summary_format(procstates_buffer, brkdn, procstate_names);
375 fputs(procstates_buffer, stdout);
377 /* save the numbers for next time */
378 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
381 u_procstates(total, brkdn)
387 static char new[MAX_COLS];
390 /* update number of processes only if it has changed */
393 /* move and overwrite */
394 #if (x_procstate == 0)
395 Move_to(x_procstate, y_procstate);
397 /* cursor is already there...no motion needed */
398 /* assert(lastline == 1); */
402 /* if number of digits differs, rewrite the label */
403 if (digits(total) != digits(ltotal))
405 fputs(" processes:", stdout);
406 /* put out enough spaces to get to column 15 */
412 /* cursor may end up right where we want it!!! */
419 /* see if any of the state numbers has changed */
420 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
422 /* format and update the line */
423 summary_format(new, brkdn, procstate_names);
424 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
425 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
431 * *_cpustates(states, names) - print the cpu state percentages
433 * Assumptions: cursor is on the PREVIOUS line
436 /* cpustates_tag() calculates the correct tag to use to label the line */
438 char *cpustates_tag()
443 static char *short_tag = "CPU: ";
444 static char *long_tag = "CPU states: ";
446 /* if length + strlen(long_tag) >= screen_width, then we have to
447 use the shorter tag (we subtract 2 to account for ": ") */
448 if (cpustate_total_length + (int)strlen(long_tag) - 2 >= screen_width)
457 /* set cpustates_column accordingly then return result */
458 cpustates_column = strlen(use);
465 register int *states;
470 register char **names;
471 register char *thisname;
474 for (cpu = 0; cpu < num_cpus; cpu++) {
475 names = cpustate_names;
477 /* print tag and bump lastline */
481 value = printf("\nCPU %d: ", cpu);
482 while (value++ <= cpustates_column)
487 /* now walk thru the names and print the line */
488 while ((thisname = *names++) != NULL)
490 if (*thisname != '\0')
492 /* retrieve the value and remember it */
495 /* if percentage is >= 1000, print it as 100% */
496 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
497 (i++ % num_cpustates) == 0 ? "" : ", ",
504 /* copy over values into "last" array */
505 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
510 register int *states;
514 register char **names;
515 register char *thisname;
520 for (cpu = 0; cpu < num_cpus; cpu++) {
521 names = cpustate_names;
523 Move_to(cpustates_column, y_cpustates + cpu);
524 lastline = y_cpustates + cpu;
525 lp = lcpustates + (cpu * num_cpustates);
526 colp = cpustate_columns;
528 /* we could be much more optimal about this */
529 while ((thisname = *names++) != NULL)
531 if (*thisname != '\0')
533 /* did the value change since last time? */
536 /* yes, move and change */
537 Move_to(cpustates_column + *colp, y_cpustates + cpu);
538 lastline = y_cpustates + cpu;
540 /* retrieve value and remember it */
543 /* if percentage is >= 1000, print it as 100% */
544 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
545 ((double)value)/10.);
547 /* remember it for next time */
552 /* increment and move on */
564 register char **names;
565 register char *thisname;
569 for (cpu = 0; cpu < num_cpus; cpu++) {
570 names = cpustate_names;
572 /* show tag and bump lastline */
576 value = printf("\nCPU %d: ", cpu);
577 while (value++ <= cpustates_column)
582 while ((thisname = *names++) != NULL)
584 if (*thisname != '\0')
586 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
591 /* fill the "last" array with all -1s, to insure correct updating */
593 i = num_cpustates * num_cpus;
601 * *_memory(stats) - print "Memory: " followed by the memory summary string
603 * Assumptions: cursor is on "lastline"
604 * for i_memory ONLY: cursor is on the previous line
607 char memory_buffer[MAX_COLS];
614 fputs("\nMem: ", stdout);
617 /* format and print the memory summary */
618 summary_format(memory_buffer, stats, memory_names);
619 fputs(memory_buffer, stdout);
627 static char new[MAX_COLS];
629 /* format the new line */
630 summary_format(new, stats, memory_names);
631 line_update(memory_buffer, new, x_mem, y_mem);
635 * *_arc(stats) - print "ARC: " followed by the ARC summary string
637 * Assumptions: cursor is on "lastline"
638 * for i_arc ONLY: cursor is on the previous line
640 char arc_buffer[MAX_COLS];
647 if (arc_names == NULL)
650 fputs("\nARC: ", stdout);
653 /* format and print the memory summary */
654 summary_format(arc_buffer, stats, arc_names);
655 fputs(arc_buffer, stdout);
663 static char new[MAX_COLS];
665 if (arc_names == NULL)
668 /* format the new line */
669 summary_format(new, stats, arc_names);
670 line_update(arc_buffer, new, x_arc, y_arc);
675 * *_swap(stats) - print "Swap: " followed by the swap summary string
677 * Assumptions: cursor is on "lastline"
678 * for i_swap ONLY: cursor is on the previous line
681 char swap_buffer[MAX_COLS];
688 fputs("\nSwap: ", stdout);
691 /* format and print the swap summary */
692 summary_format(swap_buffer, stats, swap_names);
693 fputs(swap_buffer, stdout);
701 static char new[MAX_COLS];
703 /* format the new line */
704 summary_format(new, stats, swap_names);
705 line_update(swap_buffer, new, x_swap, y_swap);
709 * *_message() - print the next pending message line, or erase the one
712 * Note that u_message is (currently) the same as i_message.
714 * Assumptions: lastline is consistent
718 * i_message is funny because it gets its message asynchronously (with
719 * respect to screen updates).
722 static char next_msg[MAX_COLS + 5];
723 static int msglen = 0;
724 /* Invariant: msglen is always the length of the message currently displayed
725 on the screen (even when next_msg doesn't contain that message). */
730 while (lastline < y_message)
735 if (next_msg[0] != '\0')
738 msglen = strlen(next_msg);
743 (void) clear_eol(msglen);
754 static int header_length;
757 * Trim a header string to the current display width and return a newly
758 * allocated area with the trimmed header.
771 width = display_width;
772 header_length = strlen(text);
773 if (header_length >= width) {
774 s = malloc((width + 1) * sizeof(char));
777 strncpy(s, text, width);
784 * *_header(text) - print the header for the process area
786 * Assumptions: cursor is on the previous line and lastline is consistent
796 s = trim_header(text);
800 if (header_status == ON)
806 else if (header_status == ERASE)
816 char *text; /* ignored */
820 if (header_status == ERASE)
824 clear_eol(header_length);
830 * *_process(line, thisline) - print one process line
832 * Assumptions: lastline is consistent
835 i_process(line, thisline)
844 /* make sure we are on the correct line */
845 while (lastline < y_procs + line)
851 /* truncate the line to conform to our current screen width */
852 thisline[display_width] = '\0';
854 /* write the line out */
855 fputs(thisline, stdout);
857 /* copy it in to our buffer */
858 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
859 p = strecpy(base, thisline);
861 /* zero fill the rest of it */
862 memzero(p, display_width - (p - base));
865 u_process(line, newline)
872 register int screen_line = line + Header_lines;
873 register char *bufferline;
875 /* remember a pointer to the current line in the screen buffer */
876 bufferline = &screenbuf[lineindex(line)];
878 /* truncate the line to conform to our current screen width */
879 newline[display_width] = '\0';
881 /* is line higher than we went on the last display? */
884 /* yes, just ignore screenbuf and write it out directly */
885 /* get positioned on the correct line */
886 if (screen_line - lastline == 1)
893 Move_to(0, screen_line);
894 lastline = screen_line;
897 /* now write the line */
898 fputs(newline, stdout);
900 /* copy it in to the buffer */
901 optr = strecpy(bufferline, newline);
903 /* zero fill the rest of it */
904 memzero(optr, display_width - (optr - bufferline));
908 line_update(bufferline, newline, 0, line + Header_lines);
917 register int screen_line = hi + Header_lines;
924 /* need to blank the remainder of the screen */
925 /* but only if there is any screen left below this line */
926 if (lastline + 1 < screen_length)
928 /* efficiently move to the end of currently displayed info */
929 if (screen_line - lastline < 5)
931 while (lastline < screen_line)
939 Move_to(0, screen_line);
940 lastline = screen_line;
945 /* we can do this the easy way */
946 putcap(clear_to_end);
950 /* use clear_eol on each line */
952 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
961 /* move the cursor to a pleasant place */
962 Move_to(x_idlecursor, y_idlecursor);
963 lastline = y_idlecursor;
967 /* separate this display from the next with some vertical room */
968 fputs("\n\n", stdout);
981 else if (header_status == ON)
983 header_status = ERASE;
988 new_message(type, msgfmt, a1, a2, a3)
997 /* first, format the message */
998 (void) snprintf(next_msg, sizeof(next_msg), msgfmt, a1, a2, a3);
1002 /* message there already -- can we clear it? */
1005 /* yes -- write it and clear to end */
1006 i = strlen(next_msg);
1007 if ((type & MT_delayed) == 0)
1009 type & MT_standout ? standout(next_msg) :
1010 fputs(next_msg, stdout);
1011 (void) clear_eol(msglen - i);
1019 if ((type & MT_delayed) == 0)
1021 type & MT_standout ? standout(next_msg) : fputs(next_msg, stdout);
1022 msglen = strlen(next_msg);
1031 if (clear_eol(msglen) == 1)
1037 readline(buffer, size, numeric)
1044 register char *ptr = buffer;
1046 register char cnt = 0;
1047 register char maxcnt = 0;
1049 /* allow room for null terminator */
1053 while ((fflush(stdout), read(0, ptr, 1) > 0))
1055 /* newline means we are done */
1056 if ((ch = *ptr) == '\n' || ch == '\r')
1061 /* handle special editing characters */
1064 /* kill line -- account for overstriking */
1070 /* return null string */
1075 else if (ch == ch_erase)
1077 /* erase previous character */
1080 /* none to erase! */
1085 fputs("\b \b", stdout);
1090 /* check for character validity and buffer overflow */
1091 else if (cnt == size || (numeric && !isdigit(ch)) ||
1099 /* echo it and store it in the buffer */
1110 /* all done -- null terminate the string */
1113 /* account for the extra characters in the message area */
1114 /* (if terminal overstrikes, remember the furthest they went) */
1115 msglen += overstrike ? maxcnt : cnt;
1117 /* return either inputted number or string length */
1119 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1122 /* internal support routines */
1124 static int string_count(pp)
1132 while (*pp++ != NULL)
1139 static void summary_format(str, numbers, names)
1143 register char **names;
1148 register char *thisname;
1149 register int useM = No;
1151 /* format each number followed by its string */
1153 while ((thisname = *names++) != NULL)
1155 /* get the number to format */
1158 /* display only non-zero numbers */
1161 /* is this number in kilobytes? */
1162 if (thisname[0] == 'K')
1164 /* yes: format it as a memory value */
1165 p = strecpy(p, format_k(num));
1167 /* skip over the K, since it was included by format_k */
1168 p = strecpy(p, thisname+1);
1172 p = strecpy(p, itoa(num));
1173 p = strecpy(p, thisname);
1177 /* ignore negative numbers, but display corresponding string */
1180 p = strecpy(p, thisname);
1184 /* if the last two characters in the string are ", ", delete them */
1186 if (p >= str && p[0] == ',' && p[1] == ' ')
1192 static void line_update(old, new, start, line)
1202 register int newcol = start + 1;
1203 register int lastcol = start;
1204 char cursor_on_line = No;
1207 /* compare the two strings and only rewrite what has changed */
1210 fprintf(debug, "line_update, starting at %d\n", start);
1214 fputs("\n-\n", debug);
1217 /* start things off on the right foot */
1218 /* this is to make sure the invariants get set up right */
1219 if ((ch = *new++) != *old)
1221 if (line - lastline == 1 && start == 0)
1227 Move_to(start, line);
1229 cursor_on_line = Yes;
1237 * main loop -- check each character. If the old and new aren't the
1238 * same, then update the display. When the distance from the
1239 * current cursor position to the new change is small enough,
1240 * the characters that belong there are written to move the
1244 * lastcol is the column where the cursor currently is sitting
1245 * (always one beyond the end of the last mismatch).
1247 do /* yes, a do...while */
1249 if ((ch = *new++) != *old)
1251 /* new character is different from old */
1252 /* make sure the cursor is on top of this character */
1253 diff = newcol - lastcol;
1256 /* some motion is required--figure out which is shorter */
1257 if (diff < 6 && cursor_on_line)
1259 /* overwrite old stuff--get it out of the old buffer */
1260 printf("%.*s", diff, ¤t[lastcol-start]);
1264 /* use cursor addressing */
1265 Move_to(newcol, line);
1266 cursor_on_line = Yes;
1268 /* remember where the cursor is */
1269 lastcol = newcol + 1;
1273 /* already there, update position */
1277 /* write what we need to */
1280 /* at the end--terminate with a clear-to-end-of-line */
1281 (void) clear_eol(strlen(old));
1285 /* write the new character */
1288 /* put the new character in the screen buffer */
1292 /* update working column and screen buffer pointer */
1296 } while (ch != '\0');
1298 /* zero out the rest of the line buffer -- MUST BE DONE! */
1299 diff = display_width - newcol;
1305 /* remember where the current line is */
1313 * printable(str) - make the string pointed to by "str" into one that is
1314 * printable (i.e.: all ascii), by converting all non-printable
1315 * characters into '?'. Replacements are done in place and a pointer
1316 * to the original buffer is returned.
1319 char *printable(str)
1328 while ((ch = *ptr) != '\0')
1346 int days, hrs, mins, secs;
1348 if (bt->tv_sec != -1) {
1349 uptime = *tod - bt->tv_sec;
1350 days = uptime / 86400;
1352 hrs = uptime / 3600;
1358 * Display the uptime.
1363 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1369 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);