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 **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();
93 static void summary_format();
94 static void line_update();
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 = (char *)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(statics)
164 struct statics *statics;
171 /* call resize to do the dirty work */
172 lines = display_resize();
174 num_cpus = statics->ncpus;
177 cpustates_column = 5; /* CPU: */
179 cpustates_column += 2; /* CPU 0: */
180 for (i = num_cpus; i > 9; i /= 10)
183 /* fill the "last" array with all -1s, to insure correct updating */
185 i = num_cpustates * num_cpus;
194 int display_init(statics)
196 struct statics *statics;
204 lines = display_updatecpus(statics);
206 /* only do the rest if we need to */
209 /* save pointers and allocate space for names */
210 procstate_names = statics->procstate_names;
211 num_procstates = string_count(procstate_names);
212 lprocstates = (int *)malloc(num_procstates * sizeof(int));
214 cpustate_names = statics->cpustate_names;
216 swap_names = statics->swap_names;
217 num_swap = string_count(swap_names);
218 lswap = (int *)malloc(num_swap * sizeof(int));
219 num_cpustates = string_count(cpustate_names);
220 lcpustates = (int *)malloc(num_cpustates * sizeof(int) * statics->ncpus);
221 cpustate_columns = (int *)malloc(num_cpustates * sizeof(int));
223 memory_names = statics->memory_names;
224 num_memory = string_count(memory_names);
225 lmemory = (int *)malloc(num_memory * sizeof(int));
227 arc_names = statics->arc_names;
228 carc_names = statics->carc_names;
230 /* calculate starting columns where needed */
231 cpustate_total_length = 0;
233 ip = cpustate_columns;
236 *ip++ = cpustate_total_length;
237 if ((i = strlen(*pp++)) > 0)
239 cpustate_total_length += i + 8;
244 /* return number of lines available */
249 i_loadave(mpid, avenrun)
257 /* i_loadave also clears the screen, since it is first */
260 /* mpid == -1 implies this system doesn't have an _mpid */
263 printf("last pid: %5d; ", mpid);
266 printf("load averages");
268 for (i = 0; i < 3; i++)
278 u_loadave(mpid, avenrun)
288 /* change screen only when value has really changed */
291 Move_to(x_lastpid, y_lastpid);
296 /* i remembers x coordinate to move to */
301 i = x_loadave_nompid;
304 /* move into position for load averages */
305 Move_to(i, y_loadave);
307 /* display new load averages */
308 /* we should optimize this and only display changes */
309 for (i = 0; i < 3; i++)
324 * Display the current time.
325 * "ctime" always returns a string that looks like this:
327 * Sun Sep 16 01:03:52 1973
328 * 012345678901234567890123
331 * We want indices 11 thru 18 (length 8).
336 Move_to(screen_width - 8, 0);
349 printf("%-8.8s\n", &(ctime(tod)[11]));
353 static int ltotal = 0;
354 static char procstates_buffer[MAX_COLS];
357 * *_procstates(total, brkdn, names) - print the process summary line
359 * Assumptions: cursor is at the beginning of the line on entry
364 i_procstates(total, brkdn)
372 /* write current number of processes and remember the value */
373 printf("%d processes:", total);
376 /* put out enough spaces to get to column 15 */
383 /* format and print the process state summary */
384 summary_format(procstates_buffer, brkdn, procstate_names);
385 fputs(procstates_buffer, stdout);
387 /* save the numbers for next time */
388 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
392 u_procstates(total, brkdn)
398 static char new[MAX_COLS];
401 /* update number of processes only if it has changed */
404 /* move and overwrite */
405 #if (x_procstate == 0)
406 Move_to(x_procstate, y_procstate);
408 /* cursor is already there...no motion needed */
409 /* assert(lastline == 1); */
413 /* if number of digits differs, rewrite the label */
414 if (digits(total) != digits(ltotal))
416 fputs(" processes:", stdout);
417 /* put out enough spaces to get to column 15 */
423 /* cursor may end up right where we want it!!! */
430 /* see if any of the state numbers has changed */
431 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
433 /* format and update the line */
434 summary_format(new, brkdn, procstate_names);
435 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
436 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
442 * *_cpustates(states, names) - print the cpu state percentages
444 * Assumptions: cursor is on the PREVIOUS line
447 /* cpustates_tag() calculates the correct tag to use to label the line */
449 char *cpustates_tag()
454 static char *short_tag = "CPU: ";
455 static char *long_tag = "CPU states: ";
457 /* if length + strlen(long_tag) >= screen_width, then we have to
458 use the shorter tag (we subtract 2 to account for ": ") */
459 if (cpustate_total_length + (int)strlen(long_tag) - 2 >= screen_width)
468 /* set cpustates_column accordingly then return result */
469 cpustates_column = strlen(use);
482 register char **names;
483 register char *thisname;
486 for (cpu = 0; cpu < num_cpus; cpu++) {
487 names = cpustate_names;
489 /* print tag and bump lastline */
493 value = printf("\nCPU %d: ", cpu);
494 while (value++ <= cpustates_column)
499 /* now walk thru the names and print the line */
500 while ((thisname = *names++) != NULL)
502 if (*thisname != '\0')
504 /* retrieve the value and remember it */
507 /* if percentage is >= 1000, print it as 100% */
508 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
509 (i++ % num_cpustates) == 0 ? "" : ", ",
516 /* copy over values into "last" array */
517 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
527 register char **names;
528 register char *thisname;
533 for (cpu = 0; cpu < num_cpus; cpu++) {
534 names = cpustate_names;
536 Move_to(cpustates_column, y_cpustates + cpu);
537 lastline = y_cpustates + cpu;
538 lp = lcpustates + (cpu * num_cpustates);
539 colp = cpustate_columns;
541 /* we could be much more optimal about this */
542 while ((thisname = *names++) != NULL)
544 if (*thisname != '\0')
546 /* did the value change since last time? */
549 /* yes, move and change */
550 Move_to(cpustates_column + *colp, y_cpustates + cpu);
551 lastline = y_cpustates + cpu;
553 /* retrieve value and remember it */
556 /* if percentage is >= 1000, print it as 100% */
557 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
558 ((double)value)/10.);
560 /* remember it for next time */
565 /* increment and move on */
578 register char **names;
579 register char *thisname;
583 for (cpu = 0; cpu < num_cpus; cpu++) {
584 names = cpustate_names;
586 /* show tag and bump lastline */
590 value = printf("\nCPU %d: ", cpu);
591 while (value++ <= cpustates_column)
596 while ((thisname = *names++) != NULL)
598 if (*thisname != '\0')
600 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
605 /* fill the "last" array with all -1s, to insure correct updating */
607 i = num_cpustates * num_cpus;
615 * *_memory(stats) - print "Memory: " followed by the memory summary string
617 * Assumptions: cursor is on "lastline"
618 * for i_memory ONLY: cursor is on the previous line
621 char memory_buffer[MAX_COLS];
629 fputs("\nMem: ", stdout);
632 /* format and print the memory summary */
633 summary_format(memory_buffer, stats, memory_names);
634 fputs(memory_buffer, stdout);
643 static char new[MAX_COLS];
645 /* format the new line */
646 summary_format(new, stats, memory_names);
647 line_update(memory_buffer, new, x_mem, y_mem);
651 * *_arc(stats) - print "ARC: " followed by the ARC summary string
653 * Assumptions: cursor is on "lastline"
654 * for i_arc ONLY: cursor is on the previous line
656 char arc_buffer[MAX_COLS];
664 if (arc_names == NULL)
667 fputs("\nARC: ", stdout);
670 /* format and print the memory summary */
671 summary_format(arc_buffer, stats, arc_names);
672 fputs(arc_buffer, stdout);
681 static char new[MAX_COLS];
683 if (arc_names == NULL)
686 /* format the new line */
687 summary_format(new, stats, arc_names);
688 line_update(arc_buffer, new, x_arc, y_arc);
693 * *_carc(stats) - print "Compressed ARC: " followed by the summary string
695 * Assumptions: cursor is on "lastline"
696 * for i_carc ONLY: cursor is on the previous line
698 char carc_buffer[MAX_COLS];
706 if (carc_names == NULL)
709 fputs("\n ", stdout);
712 /* format and print the memory summary */
713 summary_format(carc_buffer, stats, carc_names);
714 fputs(carc_buffer, stdout);
723 static char new[MAX_COLS];
725 if (carc_names == NULL)
728 /* format the new line */
729 summary_format(new, stats, carc_names);
730 line_update(carc_buffer, new, x_carc, y_carc);
734 * *_swap(stats) - print "Swap: " followed by the swap summary string
736 * Assumptions: cursor is on "lastline"
737 * for i_swap ONLY: cursor is on the previous line
740 char swap_buffer[MAX_COLS];
748 fputs("\nSwap: ", stdout);
751 /* format and print the swap summary */
752 summary_format(swap_buffer, stats, swap_names);
753 fputs(swap_buffer, stdout);
762 static char new[MAX_COLS];
764 /* format the new line */
765 summary_format(new, stats, swap_names);
766 line_update(swap_buffer, new, x_swap, y_swap);
770 * *_message() - print the next pending message line, or erase the one
773 * Note that u_message is (currently) the same as i_message.
775 * Assumptions: lastline is consistent
779 * i_message is funny because it gets its message asynchronously (with
780 * respect to screen updates).
783 static char next_msg[MAX_COLS + 5];
784 static int msglen = 0;
785 /* Invariant: msglen is always the length of the message currently displayed
786 on the screen (even when next_msg doesn't contain that message). */
792 while (lastline < y_message)
797 if (next_msg[0] != '\0')
800 msglen = strlen(next_msg);
805 (void) clear_eol(msglen);
817 static int header_length;
820 * Trim a header string to the current display width and return a newly
821 * allocated area with the trimmed header.
834 width = display_width;
835 header_length = strlen(text);
836 if (header_length >= width) {
837 s = malloc((width + 1) * sizeof(char));
840 strncpy(s, text, width);
847 * *_header(text) - print the header for the process area
849 * Assumptions: cursor is on the previous line and lastline is consistent
860 s = trim_header(text);
864 if (header_status == ON)
870 else if (header_status == ERASE)
881 char *text __unused; /* ignored */
885 if (header_status == ERASE)
889 clear_eol(header_length);
895 * *_process(line, thisline) - print one process line
897 * Assumptions: lastline is consistent
901 i_process(line, thisline)
910 /* make sure we are on the correct line */
911 while (lastline < y_procs + line)
917 /* truncate the line to conform to our current screen width */
918 thisline[display_width] = '\0';
920 /* write the line out */
921 fputs(thisline, stdout);
923 /* copy it in to our buffer */
924 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
925 p = strecpy(base, thisline);
927 /* zero fill the rest of it */
928 memzero(p, display_width - (p - base));
932 u_process(line, newline)
939 register int screen_line = line + Header_lines;
940 register char *bufferline;
942 /* remember a pointer to the current line in the screen buffer */
943 bufferline = &screenbuf[lineindex(line)];
945 /* truncate the line to conform to our current screen width */
946 newline[display_width] = '\0';
948 /* is line higher than we went on the last display? */
951 /* yes, just ignore screenbuf and write it out directly */
952 /* get positioned on the correct line */
953 if (screen_line - lastline == 1)
960 Move_to(0, screen_line);
961 lastline = screen_line;
964 /* now write the line */
965 fputs(newline, stdout);
967 /* copy it in to the buffer */
968 optr = strecpy(bufferline, newline);
970 /* zero fill the rest of it */
971 memzero(optr, display_width - (optr - bufferline));
975 line_update(bufferline, newline, 0, line + Header_lines);
985 register int screen_line = hi + Header_lines;
992 /* need to blank the remainder of the screen */
993 /* but only if there is any screen left below this line */
994 if (lastline + 1 < screen_length)
996 /* efficiently move to the end of currently displayed info */
997 if (screen_line - lastline < 5)
999 while (lastline < screen_line)
1007 Move_to(0, screen_line);
1008 lastline = screen_line;
1013 /* we can do this the easy way */
1014 putcap(clear_to_end);
1018 /* use clear_eol on each line */
1020 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
1029 /* move the cursor to a pleasant place */
1030 Move_to(x_idlecursor, y_idlecursor);
1031 lastline = y_idlecursor;
1035 /* separate this display from the next with some vertical room */
1036 fputs("\n\n", stdout);
1050 else if (header_status == ON)
1052 header_status = ERASE;
1058 new_message(type, msgfmt, a1, a2, a3)
1067 /* first, format the message */
1068 (void) snprintf(next_msg, sizeof(next_msg), msgfmt, a1, a2, a3);
1072 /* message there already -- can we clear it? */
1075 /* yes -- write it and clear to end */
1076 i = strlen(next_msg);
1077 if ((type & MT_delayed) == 0)
1079 type & MT_standout ? standout(next_msg) :
1080 fputs(next_msg, stdout);
1081 (void) clear_eol(msglen - i);
1089 if ((type & MT_delayed) == 0)
1091 type & MT_standout ? standout(next_msg) : fputs(next_msg, stdout);
1092 msglen = strlen(next_msg);
1102 if (clear_eol(msglen) == 1)
1109 readline(buffer, size, numeric)
1116 register char *ptr = buffer;
1118 register char cnt = 0;
1119 register char maxcnt = 0;
1121 /* allow room for null terminator */
1125 while ((fflush(stdout), read(0, ptr, 1) > 0))
1127 /* newline means we are done */
1128 if ((ch = *ptr) == '\n' || ch == '\r')
1133 /* handle special editing characters */
1136 /* kill line -- account for overstriking */
1142 /* return null string */
1147 else if (ch == ch_erase)
1149 /* erase previous character */
1152 /* none to erase! */
1157 fputs("\b \b", stdout);
1162 /* check for character validity and buffer overflow */
1163 else if (cnt == size || (numeric && !isdigit(ch)) ||
1171 /* echo it and store it in the buffer */
1182 /* all done -- null terminate the string */
1185 /* account for the extra characters in the message area */
1186 /* (if terminal overstrikes, remember the furthest they went) */
1187 msglen += overstrike ? maxcnt : cnt;
1189 /* return either inputted number or string length */
1191 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1194 /* internal support routines */
1196 static int string_count(pp)
1204 while (*pp++ != NULL)
1211 static void summary_format(str, numbers, names)
1215 register char **names;
1220 register char *thisname;
1221 register int useM = No;
1224 /* format each number followed by its string */
1226 while ((thisname = *names++) != NULL)
1228 /* get the number to format */
1231 /* display only non-zero numbers */
1234 /* is this number in kilobytes? */
1235 if (thisname[0] == 'K')
1237 /* yes: format it as a memory value */
1238 p = strecpy(p, format_k(num));
1240 /* skip over the K, since it was included by format_k */
1241 p = strecpy(p, thisname+1);
1243 /* is this number a ratio? */
1244 else if (thisname[0] == ':')
1246 (void) snprintf(rbuf, sizeof(rbuf), "%.2f",
1247 (float)*(numbers - 2) / (float)num);
1248 p = strecpy(p, rbuf);
1249 p = strecpy(p, thisname);
1253 p = strecpy(p, itoa(num));
1254 p = strecpy(p, thisname);
1258 /* ignore negative numbers, but display corresponding string */
1261 p = strecpy(p, thisname);
1265 /* if the last two characters in the string are ", ", delete them */
1267 if (p >= str && p[0] == ',' && p[1] == ' ')
1273 static void line_update(old, new, start, line)
1283 register int newcol = start + 1;
1284 register int lastcol = start;
1285 char cursor_on_line = No;
1288 /* compare the two strings and only rewrite what has changed */
1291 fprintf(debug, "line_update, starting at %d\n", start);
1295 fputs("\n-\n", debug);
1298 /* start things off on the right foot */
1299 /* this is to make sure the invariants get set up right */
1300 if ((ch = *new++) != *old)
1302 if (line - lastline == 1 && start == 0)
1308 Move_to(start, line);
1310 cursor_on_line = Yes;
1318 * main loop -- check each character. If the old and new aren't the
1319 * same, then update the display. When the distance from the
1320 * current cursor position to the new change is small enough,
1321 * the characters that belong there are written to move the
1325 * lastcol is the column where the cursor currently is sitting
1326 * (always one beyond the end of the last mismatch).
1328 do /* yes, a do...while */
1330 if ((ch = *new++) != *old)
1332 /* new character is different from old */
1333 /* make sure the cursor is on top of this character */
1334 diff = newcol - lastcol;
1337 /* some motion is required--figure out which is shorter */
1338 if (diff < 6 && cursor_on_line)
1340 /* overwrite old stuff--get it out of the old buffer */
1341 printf("%.*s", diff, ¤t[lastcol-start]);
1345 /* use cursor addressing */
1346 Move_to(newcol, line);
1347 cursor_on_line = Yes;
1349 /* remember where the cursor is */
1350 lastcol = newcol + 1;
1354 /* already there, update position */
1358 /* write what we need to */
1361 /* at the end--terminate with a clear-to-end-of-line */
1362 (void) clear_eol(strlen(old));
1366 /* write the new character */
1369 /* put the new character in the screen buffer */
1373 /* update working column and screen buffer pointer */
1377 } while (ch != '\0');
1379 /* zero out the rest of the line buffer -- MUST BE DONE! */
1380 diff = display_width - newcol;
1386 /* remember where the current line is */
1394 * printable(str) - make the string pointed to by "str" into one that is
1395 * printable (i.e.: all ascii), by converting all non-printable
1396 * characters into '?'. Replacements are done in place and a pointer
1397 * to the original buffer is returned.
1400 char *printable(str)
1409 while ((ch = *ptr) != '\0')
1428 int days, hrs, mins, secs;
1430 if (bt->tv_sec != -1) {
1431 uptime = *tod - bt->tv_sec;
1432 days = uptime / 86400;
1434 hrs = uptime / 3600;
1440 * Display the uptime.
1445 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1451 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);