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.
41 #include "screen.h" /* interface to screen package */
42 #include "layout.h" /* defines for screen position layout */
45 #include "top.local.h"
47 #include "machine.h" /* we should eliminate this!!! */
54 /* imported from screen.c */
55 extern int overstrike;
58 static int last_hi = 0; /* used in u_process and u_endscreen */
59 static int lastline = 0;
60 static int display_width = MAX_COLS;
62 #define lineindex(l) ((l)*display_width)
65 /* things initialized by display_init and used thruout */
67 /* buffer of proc information lines for display updating */
68 char *screenbuf = NULL;
70 static char **procstate_names;
71 static char **cpustate_names;
72 static char **memory_names;
73 static char **arc_names;
74 static char **carc_names;
75 static char **swap_names;
77 static int num_procstates;
78 static int num_cpustates;
79 static int num_memory;
82 static int *lprocstates;
83 static int *lcpustates;
88 static int *cpustate_columns;
89 static int cpustate_total_length;
90 static int cpustates_column;
92 static enum { OFF, ON, ERASE } header_status = ON;
94 static int string_count();
95 static void summary_format();
96 static void line_update();
101 int x_loadave_nompid = 15;
118 int x_idlecursor = 0;
119 int y_idlecursor = 5;
123 int Header_lines = 7;
130 /* first, deallocate any previous buffer that may have been there */
131 if (screenbuf != NULL)
136 /* calculate the current dimensions */
137 /* if operating in "dumb" mode, we only need one line */
138 lines = smart_terminal ? screen_length - Header_lines : 1;
142 /* we don't want more than MAX_COLS columns, since the machine-dependent
143 modules make static allocations based on MAX_COLS and we don't want
144 to run off the end of their buffers */
145 display_width = screen_width;
146 if (display_width >= MAX_COLS)
148 display_width = MAX_COLS - 1;
151 /* now, allocate space for the screen buffer */
152 screenbuf = (char *)malloc(lines * display_width);
153 if (screenbuf == (char *)NULL)
159 /* return number of lines available */
160 /* for dumb terminals, pretend like we can show any amount */
161 return(smart_terminal ? lines : Largest);
164 int display_updatecpus(statics)
166 struct statics *statics;
173 /* call resize to do the dirty work */
174 lines = display_resize();
176 num_cpus = statics->ncpus;
179 cpustates_column = 5; /* CPU: */
181 cpustates_column += 2; /* CPU 0: */
182 for (i = num_cpus; i > 9; i /= 10)
185 /* fill the "last" array with all -1s, to insure correct updating */
187 i = num_cpustates * num_cpus;
196 int display_init(statics)
198 struct statics *statics;
206 lines = display_updatecpus(statics);
208 /* only do the rest if we need to */
211 /* save pointers and allocate space for names */
212 procstate_names = statics->procstate_names;
213 num_procstates = string_count(procstate_names);
214 lprocstates = (int *)malloc(num_procstates * sizeof(int));
216 cpustate_names = statics->cpustate_names;
218 swap_names = statics->swap_names;
219 num_swap = string_count(swap_names);
220 lswap = (int *)malloc(num_swap * sizeof(int));
221 num_cpustates = string_count(cpustate_names);
222 lcpustates = (int *)malloc(num_cpustates * sizeof(int) * statics->ncpus);
223 cpustate_columns = (int *)malloc(num_cpustates * sizeof(int));
225 memory_names = statics->memory_names;
226 num_memory = string_count(memory_names);
227 lmemory = (int *)malloc(num_memory * sizeof(int));
229 arc_names = statics->arc_names;
230 carc_names = statics->carc_names;
232 /* calculate starting columns where needed */
233 cpustate_total_length = 0;
235 ip = cpustate_columns;
238 *ip++ = cpustate_total_length;
239 if ((i = strlen(*pp++)) > 0)
241 cpustate_total_length += i + 8;
246 /* return number of lines available */
251 i_loadave(mpid, avenrun)
259 /* i_loadave also clears the screen, since it is first */
262 /* mpid == -1 implies this system doesn't have an _mpid */
265 printf("last pid: %5d; ", mpid);
268 printf("load averages");
270 for (i = 0; i < 3; i++)
280 u_loadave(mpid, avenrun)
290 /* change screen only when value has really changed */
293 Move_to(x_lastpid, y_lastpid);
298 /* i remembers x coordinate to move to */
303 i = x_loadave_nompid;
306 /* move into position for load averages */
307 Move_to(i, y_loadave);
309 /* display new load averages */
310 /* we should optimize this and only display changes */
311 for (i = 0; i < 3; i++)
326 * Display the current time.
327 * "ctime" always returns a string that looks like this:
329 * Sun Sep 16 01:03:52 1973
330 * 012345678901234567890123
333 * We want indices 11 thru 18 (length 8).
338 Move_to(screen_width - 8, 0);
351 printf("%-8.8s\n", &(ctime(tod)[11]));
355 static int ltotal = 0;
356 static char procstates_buffer[MAX_COLS];
359 * *_procstates(total, brkdn, names) - print the process summary line
361 * Assumptions: cursor is at the beginning of the line on entry
366 i_procstates(total, brkdn)
374 /* write current number of processes and remember the value */
375 printf("%d processes:", total);
378 /* put out enough spaces to get to column 15 */
385 /* format and print the process state summary */
386 summary_format(procstates_buffer, brkdn, procstate_names);
387 fputs(procstates_buffer, stdout);
389 /* save the numbers for next time */
390 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
394 u_procstates(total, brkdn)
400 static char new[MAX_COLS];
403 /* update number of processes only if it has changed */
406 /* move and overwrite */
407 #if (x_procstate == 0)
408 Move_to(x_procstate, y_procstate);
410 /* cursor is already there...no motion needed */
411 /* assert(lastline == 1); */
415 /* if number of digits differs, rewrite the label */
416 if (digits(total) != digits(ltotal))
418 fputs(" processes:", stdout);
419 /* put out enough spaces to get to column 15 */
425 /* cursor may end up right where we want it!!! */
432 /* see if any of the state numbers has changed */
433 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
435 /* format and update the line */
436 summary_format(new, brkdn, procstate_names);
437 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
438 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
444 * *_cpustates(states, names) - print the cpu state percentages
446 * Assumptions: cursor is on the PREVIOUS line
449 /* cpustates_tag() calculates the correct tag to use to label the line */
451 char *cpustates_tag()
456 static char *short_tag = "CPU: ";
457 static char *long_tag = "CPU states: ";
459 /* if length + strlen(long_tag) >= screen_width, then we have to
460 use the shorter tag (we subtract 2 to account for ": ") */
461 if (cpustate_total_length + (int)strlen(long_tag) - 2 >= screen_width)
470 /* set cpustates_column accordingly then return result */
471 cpustates_column = strlen(use);
484 register char **names;
485 register char *thisname;
488 for (cpu = 0; cpu < num_cpus; cpu++) {
489 names = cpustate_names;
491 /* print tag and bump lastline */
495 value = printf("\nCPU %d: ", cpu);
496 while (value++ <= cpustates_column)
501 /* now walk thru the names and print the line */
502 while ((thisname = *names++) != NULL)
504 if (*thisname != '\0')
506 /* retrieve the value and remember it */
509 /* if percentage is >= 1000, print it as 100% */
510 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
511 (i++ % num_cpustates) == 0 ? "" : ", ",
518 /* copy over values into "last" array */
519 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
529 register char **names;
530 register char *thisname;
535 for (cpu = 0; cpu < num_cpus; cpu++) {
536 names = cpustate_names;
538 Move_to(cpustates_column, y_cpustates + cpu);
539 lastline = y_cpustates + cpu;
540 lp = lcpustates + (cpu * num_cpustates);
541 colp = cpustate_columns;
543 /* we could be much more optimal about this */
544 while ((thisname = *names++) != NULL)
546 if (*thisname != '\0')
548 /* did the value change since last time? */
551 /* yes, move and change */
552 Move_to(cpustates_column + *colp, y_cpustates + cpu);
553 lastline = y_cpustates + cpu;
555 /* retrieve value and remember it */
558 /* if percentage is >= 1000, print it as 100% */
559 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
560 ((double)value)/10.);
562 /* remember it for next time */
567 /* increment and move on */
580 register char **names;
581 register char *thisname;
585 for (cpu = 0; cpu < num_cpus; cpu++) {
586 names = cpustate_names;
588 /* show tag and bump lastline */
592 value = printf("\nCPU %d: ", cpu);
593 while (value++ <= cpustates_column)
598 while ((thisname = *names++) != NULL)
600 if (*thisname != '\0')
602 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
607 /* fill the "last" array with all -1s, to insure correct updating */
609 i = num_cpustates * num_cpus;
617 * *_memory(stats) - print "Memory: " followed by the memory summary string
619 * Assumptions: cursor is on "lastline"
620 * for i_memory ONLY: cursor is on the previous line
623 char memory_buffer[MAX_COLS];
631 fputs("\nMem: ", stdout);
634 /* format and print the memory summary */
635 summary_format(memory_buffer, stats, memory_names);
636 fputs(memory_buffer, stdout);
645 static char new[MAX_COLS];
647 /* format the new line */
648 summary_format(new, stats, memory_names);
649 line_update(memory_buffer, new, x_mem, y_mem);
653 * *_arc(stats) - print "ARC: " followed by the ARC summary string
655 * Assumptions: cursor is on "lastline"
656 * for i_arc ONLY: cursor is on the previous line
658 char arc_buffer[MAX_COLS];
666 if (arc_names == NULL)
669 fputs("\nARC: ", stdout);
672 /* format and print the memory summary */
673 summary_format(arc_buffer, stats, arc_names);
674 fputs(arc_buffer, stdout);
683 static char new[MAX_COLS];
685 if (arc_names == NULL)
688 /* format the new line */
689 summary_format(new, stats, arc_names);
690 line_update(arc_buffer, new, x_arc, y_arc);
695 * *_carc(stats) - print "Compressed ARC: " followed by the summary string
697 * Assumptions: cursor is on "lastline"
698 * for i_carc ONLY: cursor is on the previous line
700 char carc_buffer[MAX_COLS];
708 if (carc_names == NULL)
711 fputs("\n ", stdout);
714 /* format and print the memory summary */
715 summary_format(carc_buffer, stats, carc_names);
716 fputs(carc_buffer, stdout);
725 static char new[MAX_COLS];
727 if (carc_names == NULL)
730 /* format the new line */
731 summary_format(new, stats, carc_names);
732 line_update(carc_buffer, new, x_carc, y_carc);
736 * *_swap(stats) - print "Swap: " followed by the swap summary string
738 * Assumptions: cursor is on "lastline"
739 * for i_swap ONLY: cursor is on the previous line
742 char swap_buffer[MAX_COLS];
750 fputs("\nSwap: ", stdout);
753 /* format and print the swap summary */
754 summary_format(swap_buffer, stats, swap_names);
755 fputs(swap_buffer, stdout);
764 static char new[MAX_COLS];
766 /* format the new line */
767 summary_format(new, stats, swap_names);
768 line_update(swap_buffer, new, x_swap, y_swap);
772 * *_message() - print the next pending message line, or erase the one
775 * Note that u_message is (currently) the same as i_message.
777 * Assumptions: lastline is consistent
781 * i_message is funny because it gets its message asynchronously (with
782 * respect to screen updates).
785 static char next_msg[MAX_COLS + 5];
786 static int msglen = 0;
787 /* Invariant: msglen is always the length of the message currently displayed
788 on the screen (even when next_msg doesn't contain that message). */
794 while (lastline < y_message)
799 if (next_msg[0] != '\0')
801 top_standout(next_msg);
802 msglen = strlen(next_msg);
807 (void) clear_eol(msglen);
819 static int header_length;
822 * Trim a header string to the current display width and return a newly
823 * allocated area with the trimmed header.
836 width = display_width;
837 header_length = strlen(text);
838 if (header_length >= width) {
839 s = malloc((width + 1) * sizeof(char));
842 strncpy(s, text, width);
849 * *_header(text) - print the header for the process area
851 * Assumptions: cursor is on the previous line and lastline is consistent
862 s = trim_header(text);
866 if (header_status == ON)
872 else if (header_status == ERASE)
883 char *text __unused; /* ignored */
887 if (header_status == ERASE)
891 clear_eol(header_length);
897 * *_process(line, thisline) - print one process line
899 * Assumptions: lastline is consistent
903 i_process(line, thisline)
912 /* make sure we are on the correct line */
913 while (lastline < y_procs + line)
919 /* truncate the line to conform to our current screen width */
920 thisline[display_width] = '\0';
922 /* write the line out */
923 fputs(thisline, stdout);
925 /* copy it in to our buffer */
926 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
927 p = strecpy(base, thisline);
929 /* zero fill the rest of it */
930 memzero(p, display_width - (p - base));
934 u_process(line, newline)
941 register int screen_line = line + Header_lines;
942 register char *bufferline;
944 /* remember a pointer to the current line in the screen buffer */
945 bufferline = &screenbuf[lineindex(line)];
947 /* truncate the line to conform to our current screen width */
948 newline[display_width] = '\0';
950 /* is line higher than we went on the last display? */
953 /* yes, just ignore screenbuf and write it out directly */
954 /* get positioned on the correct line */
955 if (screen_line - lastline == 1)
962 Move_to(0, screen_line);
963 lastline = screen_line;
966 /* now write the line */
967 fputs(newline, stdout);
969 /* copy it in to the buffer */
970 optr = strecpy(bufferline, newline);
972 /* zero fill the rest of it */
973 memzero(optr, display_width - (optr - bufferline));
977 line_update(bufferline, newline, 0, line + Header_lines);
987 register int screen_line = hi + Header_lines;
994 /* need to blank the remainder of the screen */
995 /* but only if there is any screen left below this line */
996 if (lastline + 1 < screen_length)
998 /* efficiently move to the end of currently displayed info */
999 if (screen_line - lastline < 5)
1001 while (lastline < screen_line)
1009 Move_to(0, screen_line);
1010 lastline = screen_line;
1015 /* we can do this the easy way */
1016 putcap(clear_to_end);
1020 /* use clear_eol on each line */
1022 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
1031 /* move the cursor to a pleasant place */
1032 Move_to(x_idlecursor, y_idlecursor);
1033 lastline = y_idlecursor;
1037 /* separate this display from the next with some vertical room */
1038 fputs("\n\n", stdout);
1052 else if (header_status == ON)
1054 header_status = ERASE;
1060 new_message(type, msgfmt, a1, a2, a3)
1069 /* first, format the message */
1070 (void) snprintf(next_msg, sizeof(next_msg), msgfmt, a1, a2, a3);
1074 /* message there already -- can we clear it? */
1077 /* yes -- write it and clear to end */
1078 i = strlen(next_msg);
1079 if ((type & MT_delayed) == 0)
1081 type & MT_standout ? top_standout(next_msg) :
1082 fputs(next_msg, stdout);
1083 (void) clear_eol(msglen - i);
1091 if ((type & MT_delayed) == 0)
1093 type & MT_standout ? top_standout(next_msg) : fputs(next_msg, stdout);
1094 msglen = strlen(next_msg);
1104 if (clear_eol(msglen) == 1)
1111 readline(buffer, size, numeric)
1118 register char *ptr = buffer;
1120 register char cnt = 0;
1121 register char maxcnt = 0;
1123 /* allow room for null terminator */
1127 while ((fflush(stdout), read(0, ptr, 1) > 0))
1129 /* newline means we are done */
1130 if ((ch = *ptr) == '\n' || ch == '\r')
1135 /* handle special editing characters */
1138 /* kill line -- account for overstriking */
1144 /* return null string */
1149 else if (ch == ch_erase)
1151 /* erase previous character */
1154 /* none to erase! */
1159 fputs("\b \b", stdout);
1164 /* check for character validity and buffer overflow */
1165 else if (cnt == size || (numeric && !isdigit(ch)) ||
1173 /* echo it and store it in the buffer */
1184 /* all done -- null terminate the string */
1187 /* account for the extra characters in the message area */
1188 /* (if terminal overstrikes, remember the furthest they went) */
1189 msglen += overstrike ? maxcnt : cnt;
1191 /* return either inputted number or string length */
1193 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1196 /* internal support routines */
1198 static int string_count(pp)
1206 while (*pp++ != NULL)
1213 static void summary_format(str, numbers, names)
1217 register char **names;
1222 register char *thisname;
1223 register int useM = No;
1226 /* format each number followed by its string */
1228 while ((thisname = *names++) != NULL)
1230 /* get the number to format */
1233 /* display only non-zero numbers */
1236 /* is this number in kilobytes? */
1237 if (thisname[0] == 'K')
1239 /* yes: format it as a memory value */
1240 p = strecpy(p, format_k(num));
1242 /* skip over the K, since it was included by format_k */
1243 p = strecpy(p, thisname+1);
1245 /* is this number a ratio? */
1246 else if (thisname[0] == ':')
1248 (void) snprintf(rbuf, sizeof(rbuf), "%.2f",
1249 (float)*(numbers - 2) / (float)num);
1250 p = strecpy(p, rbuf);
1251 p = strecpy(p, thisname);
1255 p = strecpy(p, itoa(num));
1256 p = strecpy(p, thisname);
1260 /* ignore negative numbers, but display corresponding string */
1263 p = strecpy(p, thisname);
1267 /* if the last two characters in the string are ", ", delete them */
1269 if (p >= str && p[0] == ',' && p[1] == ' ')
1275 static void line_update(old, new, start, line)
1285 register int newcol = start + 1;
1286 register int lastcol = start;
1287 char cursor_on_line = No;
1290 /* compare the two strings and only rewrite what has changed */
1293 fprintf(debug, "line_update, starting at %d\n", start);
1297 fputs("\n-\n", debug);
1300 /* start things off on the right foot */
1301 /* this is to make sure the invariants get set up right */
1302 if ((ch = *new++) != *old)
1304 if (line - lastline == 1 && start == 0)
1310 Move_to(start, line);
1312 cursor_on_line = Yes;
1320 * main loop -- check each character. If the old and new aren't the
1321 * same, then update the display. When the distance from the
1322 * current cursor position to the new change is small enough,
1323 * the characters that belong there are written to move the
1327 * lastcol is the column where the cursor currently is sitting
1328 * (always one beyond the end of the last mismatch).
1330 do /* yes, a do...while */
1332 if ((ch = *new++) != *old)
1334 /* new character is different from old */
1335 /* make sure the cursor is on top of this character */
1336 diff = newcol - lastcol;
1339 /* some motion is required--figure out which is shorter */
1340 if (diff < 6 && cursor_on_line)
1342 /* overwrite old stuff--get it out of the old buffer */
1343 printf("%.*s", diff, ¤t[lastcol-start]);
1347 /* use cursor addressing */
1348 Move_to(newcol, line);
1349 cursor_on_line = Yes;
1351 /* remember where the cursor is */
1352 lastcol = newcol + 1;
1356 /* already there, update position */
1360 /* write what we need to */
1363 /* at the end--terminate with a clear-to-end-of-line */
1364 (void) clear_eol(strlen(old));
1368 /* write the new character */
1371 /* put the new character in the screen buffer */
1375 /* update working column and screen buffer pointer */
1379 } while (ch != '\0');
1381 /* zero out the rest of the line buffer -- MUST BE DONE! */
1382 diff = display_width - newcol;
1388 /* remember where the current line is */
1396 * printable(str) - make the string pointed to by "str" into one that is
1397 * printable (i.e.: all ascii), by converting all non-printable
1398 * characters into '?'. Replacements are done in place and a pointer
1399 * to the original buffer is returned.
1402 char *printable(str)
1411 while ((ch = *ptr) != '\0')
1430 int days, hrs, mins, secs;
1432 if (bt->tv_sec != -1) {
1433 uptime = *tod - bt->tv_sec;
1434 days = uptime / 86400;
1436 hrs = uptime / 3600;
1442 * Display the uptime.
1447 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1453 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);