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.
31 #include <sys/cdefs.h>
32 #include <sys/resource.h>
47 #include "screen.h" /* interface to screen package */
48 #include "layout.h" /* defines for screen position layout */
51 #include "machine.h" /* we should eliminate this!!! */
59 static int last_hi = 0; /* used in u_process and u_endscreen */
60 static int lastline = 0;
62 #define lineindex(l) ((l)*screen_width)
65 /* things initialized by display_init and used thruout */
67 /* buffer of proc information lines for display updating */
68 static char *screenbuf = NULL;
70 static const char * const *procstate_names;
71 static const char * const *cpustate_names;
72 static const char * const *memory_names;
73 static const char * const *arc_names;
74 static const char * const *carc_names;
75 static const char * const *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 void summary_format(char *, int *, const char * const *);
95 static void line_update(char *, char *, int, int);
97 static int setup_buffer_bufsiz = 0;
98 static char * setup_buffer(char *, int);
103 int x_loadave_nompid = 15;
120 int x_idlecursor = 0;
121 int y_idlecursor = 5;
125 int Header_lines = 7;
132 /* first, deallocate any previous buffer that may have been there */
133 if (screenbuf != NULL)
138 /* calculate the current dimensions */
139 /* if operating in "dumb" mode, we only need one line */
140 lines = smart_terminal ? screen_length - Header_lines : 1;
145 /* now, allocate space for the screen buffer */
146 screenbuf = calloc(lines, screen_width);
147 if (screenbuf == NULL)
153 /* return number of lines available */
154 /* for dumb terminals, pretend like we can show any amount */
155 return(smart_terminal ? lines : Largest);
159 display_updatecpus(struct statics *statics)
164 /* call resize to do the dirty work */
165 lines = display_resize();
167 num_cpus = statics->ncpus;
170 cpustates_column = 5; /* CPU: */
172 cpustates_column += 1 + digits(num_cpus); /* CPU #: */
175 /* fill the "last" array with all -1s, to insure correct updating */
176 for (i = 0; i < num_cpustates * num_cpus; ++i) {
184 display_init(struct statics * statics)
191 lines = display_updatecpus(statics);
193 /* only do the rest if we need to */
196 /* save pointers and allocate space for names */
197 procstate_names = statics->procstate_names;
199 assert(num_procstates > 0);
200 lprocstates = calloc(num_procstates, sizeof(int));
202 cpustate_names = statics->cpustate_names;
204 swap_names = statics->swap_names;
206 assert(num_swap > 0);
207 lswap = calloc(num_swap, sizeof(int));
208 num_cpustates = CPUSTATES;
209 assert(num_cpustates > 0);
210 lcpustates = calloc(num_cpustates * sizeof(int), statics->ncpus);
211 cpustate_columns = calloc(num_cpustates, sizeof(int));
213 memory_names = statics->memory_names;
215 assert(num_memory > 0);
216 lmemory = calloc(num_memory, sizeof(int));
218 arc_names = statics->arc_names;
219 carc_names = statics->carc_names;
221 /* calculate starting columns where needed */
222 cpustate_total_length = 0;
224 ip = cpustate_columns;
227 *ip++ = cpustate_total_length;
228 if ((i = strlen(*pp++)) > 0)
230 cpustate_total_length += i + 8;
235 /* return number of lines available */
240 i_loadave(int mpid, double avenrun[])
244 /* i_loadave also clears the screen, since it is first */
247 /* mpid == -1 implies this system doesn't have an _mpid */
250 printf("last pid: %5d; ", mpid);
253 printf("load averages");
255 for (i = 0; i < 3; i++)
265 u_loadave(int mpid, double *avenrun)
271 /* change screen only when value has really changed */
274 Move_to(x_lastpid, y_lastpid);
279 /* i remembers x coordinate to move to */
284 i = x_loadave_nompid;
287 /* move into position for load averages */
288 Move_to(i, y_loadave);
290 /* display new load averages */
291 /* we should optimize this and only display changes */
292 for (i = 0; i < 3; i++)
301 i_timeofday(time_t *tod)
304 * Display the current time.
305 * "ctime" always returns a string that looks like this:
307 * Sun Sep 16 01:03:52 1973
308 * 012345678901234567890123
311 * We want indices 11 thru 18 (length 8).
316 Move_to(screen_width - 8, 0);
329 printf("%-8.8s\n", &(ctime(tod)[11]));
333 static int ltotal = 0;
334 static char *procstates_buffer = NULL;
337 * *_procstates(total, brkdn, names) - print the process summary line
339 * Assumptions: cursor is at the beginning of the line on entry
344 i_procstates(int total, int *brkdn)
348 procstates_buffer = setup_buffer(procstates_buffer, 0);
350 /* write current number of processes and remember the value */
351 printf("%d %s:", total, ps.thread ? "threads" : "processes");
354 /* put out enough spaces to get to column 15 */
356 while (i++ < (ps.thread ? 6 : 4))
361 /* format and print the process state summary */
362 summary_format(procstates_buffer, brkdn, procstate_names);
363 fputs(procstates_buffer, stdout);
365 /* save the numbers for next time */
366 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
370 u_procstates(int total, int *brkdn)
372 static char *new = NULL;
375 new = setup_buffer(new, 0);
377 /* update number of processes only if it has changed */
380 /* move and overwrite */
381 if (x_procstate == 0) {
382 Move_to(x_procstate, y_procstate);
385 /* cursor is already there...no motion needed */
386 assert(lastline == 1);
390 /* if number of digits differs, rewrite the label */
391 if (digits(total) != digits(ltotal))
393 printf(" %s:", ps.thread ? "threads" : "processes");
394 /* put out enough spaces to get to column 15 */
396 while (i++ < (ps.thread ? 6 : 4))
400 /* cursor may end up right where we want it!!! */
407 /* see if any of the state numbers has changed */
408 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
410 /* format and update the line */
411 summary_format(new, brkdn, procstate_names);
412 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
413 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
418 i_cpustates(int *states)
422 const char * const *names;
423 const char *thisname;
424 int *hstates = states;
427 for (cpu = 0; cpu < num_cpus; cpu++) {
428 names = cpustate_names;
430 /* print tag and bump lastline */
434 value = printf("\nCPU %d: ", cpu);
435 while (value++ <= cpustates_column)
440 /* now walk thru the names and print the line */
441 while ((thisname = *names++) != NULL)
443 if (*thisname != '\0')
445 /* retrieve the value and remember it */
448 /* if percentage is >= 1000, print it as 100% */
449 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
450 (i++ % num_cpustates) == 0 ? "" : ", ",
457 /* copy over values into "last" array */
459 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
463 u_cpustates(int *states)
466 const char * const *names;
467 const char *thisname;
468 int *hstates = states;
473 for (cpu = 0; cpu < num_cpus; cpu++) {
474 names = cpustate_names;
476 Move_to(cpustates_column, y_cpustates + cpu);
477 lastline = y_cpustates + cpu;
478 lp = lcpustates + (cpu * num_cpustates);
479 colp = cpustate_columns;
481 /* we could be much more optimal about this */
482 while ((thisname = *names++) != NULL)
484 if (*thisname != '\0')
486 /* did the value change since last time? */
489 /* yes, move and change */
490 Move_to(cpustates_column + *colp, y_cpustates + cpu);
491 lastline = y_cpustates + cpu;
493 /* retrieve value and remember it */
496 /* if percentage is >= 1000, print it as 100% */
497 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
498 ((double)value)/10.);
500 /* remember it for next time */
505 /* increment and move on */
523 for (cpu = 0; cpu < num_cpus; cpu++) {
524 names = cpustate_names;
526 /* show tag and bump lastline */
530 value = printf("\nCPU %d: ", cpu);
531 while (value++ <= cpustates_column)
536 while ((thisname = *names++) != NULL)
538 if (*thisname != '\0')
540 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
545 /* fill the "last" array with all -1s, to insure correct updating */
546 for (i = 0; i < num_cpustates * num_cpus; ++i) {
552 * *_memory(stats) - print "Memory: " followed by the memory summary string
554 * Assumptions: cursor is on "lastline"
555 * for i_memory ONLY: cursor is on the previous line
558 static char *memory_buffer = NULL;
563 memory_buffer = setup_buffer(memory_buffer, 0);
565 fputs("\nMem: ", stdout);
568 /* format and print the memory summary */
569 summary_format(memory_buffer, stats, memory_names);
570 fputs(memory_buffer, stdout);
576 static char *new = NULL;
578 new = setup_buffer(new, 0);
580 /* format the new line */
581 summary_format(new, stats, memory_names);
582 line_update(memory_buffer, new, x_mem, y_mem);
586 * *_arc(stats) - print "ARC: " followed by the ARC summary string
588 * Assumptions: cursor is on "lastline"
589 * for i_arc ONLY: cursor is on the previous line
591 static char *arc_buffer = NULL;
596 arc_buffer = setup_buffer(arc_buffer, 0);
598 if (arc_names == NULL)
601 fputs("\nARC: ", stdout);
604 /* format and print the memory summary */
605 summary_format(arc_buffer, stats, arc_names);
606 fputs(arc_buffer, stdout);
612 static char *new = NULL;
614 new = setup_buffer(new, 0);
616 if (arc_names == NULL)
619 /* format the new line */
620 summary_format(new, stats, arc_names);
621 line_update(arc_buffer, new, x_arc, y_arc);
626 * *_carc(stats) - print "Compressed ARC: " followed by the summary string
628 * Assumptions: cursor is on "lastline"
629 * for i_carc ONLY: cursor is on the previous line
631 static char *carc_buffer = NULL;
636 carc_buffer = setup_buffer(carc_buffer, 0);
638 if (carc_names == NULL)
641 fputs("\n ", stdout);
644 /* format and print the memory summary */
645 summary_format(carc_buffer, stats, carc_names);
646 fputs(carc_buffer, stdout);
652 static char *new = NULL;
654 new = setup_buffer(new, 0);
656 if (carc_names == NULL)
659 /* format the new line */
660 summary_format(new, stats, carc_names);
661 line_update(carc_buffer, new, x_carc, y_carc);
665 * *_swap(stats) - print "Swap: " followed by the swap summary string
667 * Assumptions: cursor is on "lastline"
668 * for i_swap ONLY: cursor is on the previous line
671 static char *swap_buffer = NULL;
676 swap_buffer = setup_buffer(swap_buffer, 0);
678 fputs("\nSwap: ", stdout);
681 /* format and print the swap summary */
682 summary_format(swap_buffer, stats, swap_names);
683 fputs(swap_buffer, stdout);
689 static char *new = NULL;
691 new = setup_buffer(new, 0);
693 /* format the new line */
694 summary_format(new, stats, swap_names);
695 line_update(swap_buffer, new, x_swap, y_swap);
699 * *_message() - print the next pending message line, or erase the one
702 * Note that u_message is (currently) the same as i_message.
704 * Assumptions: lastline is consistent
708 * i_message is funny because it gets its message asynchronously (with
709 * respect to screen updates).
712 #define NEXT_MSG_ADDLEN 5
713 static char *next_msg = NULL;
714 static int msglen = 0;
715 /* Invariant: msglen is always the length of the message currently displayed
716 on the screen (even when next_msg doesn't contain that message). */
721 next_msg = setup_buffer(next_msg, NEXT_MSG_ADDLEN);
723 while (lastline < y_message)
728 if (next_msg[0] != '\0')
730 top_standout(next_msg);
731 msglen = strlen(next_msg);
736 (void) clear_eol(msglen);
747 static int header_length;
750 * Trim a header string to the current display width and return a newly
751 * allocated area with the trimmed header.
755 trim_header(const char *text)
761 width = screen_width;
762 header_length = strlen(text);
763 if (header_length >= width) {
764 s = strndup(text, width);
772 * *_header(text) - print the header for the process area
774 * Assumptions: cursor is on the previous line and lastline is consistent
778 i_header(const char *text)
782 s = trim_header(text);
786 if (header_status == ON)
792 else if (header_status == ERASE)
800 u_header(const char *text __unused)
803 if (header_status == ERASE)
807 clear_eol(header_length);
813 * *_process(line, thisline) - print one process line
815 * Assumptions: lastline is consistent
819 i_process(int line, char *thisline)
824 /* make sure we are on the correct line */
825 while (lastline < y_procs + line)
831 /* truncate the line to conform to our current screen width */
832 thisline[screen_width] = '\0';
834 /* write the line out */
835 fputs(thisline, stdout);
837 /* copy it in to our buffer */
838 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
839 p = stpcpy(base, thisline);
841 /* zero fill the rest of it */
842 memset(p, 0, screen_width - (p - base));
846 u_process(int line, char *newline)
849 int screen_line = line + Header_lines;
852 /* remember a pointer to the current line in the screen buffer */
853 bufferline = &screenbuf[lineindex(line)];
855 /* truncate the line to conform to our current screen width */
856 newline[screen_width] = '\0';
858 /* is line higher than we went on the last display? */
861 /* yes, just ignore screenbuf and write it out directly */
862 /* get positioned on the correct line */
863 if (screen_line - lastline == 1)
870 Move_to(0, screen_line);
871 lastline = screen_line;
874 /* now write the line */
875 fputs(newline, stdout);
877 /* copy it in to the buffer */
878 optr = stpcpy(bufferline, newline);
880 /* zero fill the rest of it */
881 memset(optr, 0, screen_width - (optr - bufferline));
885 line_update(bufferline, newline, 0, line + Header_lines);
892 int screen_line = hi + Header_lines;
899 /* need to blank the remainder of the screen */
900 /* but only if there is any screen left below this line */
901 if (lastline + 1 < screen_length)
903 /* efficiently move to the end of currently displayed info */
904 if (screen_line - lastline < 5)
906 while (lastline < screen_line)
914 Move_to(0, screen_line);
915 lastline = screen_line;
920 /* we can do this the easy way */
921 putcap(clear_to_end);
925 /* use clear_eol on each line */
927 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
936 /* move the cursor to a pleasant place */
937 Move_to(x_idlecursor, y_idlecursor);
938 lastline = y_idlecursor;
942 /* separate this display from the next with some vertical room */
943 fputs("\n\n", stdout);
948 display_header(int t)
955 else if (header_status == ON)
957 header_status = ERASE;
962 new_message(int type, const char *msgfmt, ...)
967 va_start(args, msgfmt);
969 /* first, format the message */
970 vsnprintf(next_msg, setup_buffer_bufsiz + NEXT_MSG_ADDLEN,
977 /* message there already -- can we clear it? */
980 /* yes -- write it and clear to end */
981 i = strlen(next_msg);
982 if ((type & MT_delayed) == 0)
984 if (type & MT_standout) {
985 top_standout(next_msg);
987 fputs(next_msg, stdout);
989 clear_eol(msglen - i);
997 if ((type & MT_delayed) == 0)
999 if (type & MT_standout) {
1000 top_standout(next_msg);
1002 fputs(next_msg, stdout);
1004 msglen = strlen(next_msg);
1013 if (clear_eol(msglen) == 1)
1020 readline(char *buffer, int size, int numeric)
1027 /* allow room for null terminator */
1031 while ((fflush(stdout), read(0, ptr, 1) > 0))
1033 /* newline means we are done */
1034 if ((ch = *ptr) == '\n' || ch == '\r')
1039 /* handle special editing characters */
1042 /* kill line -- account for overstriking */
1048 /* return null string */
1053 else if (ch == ch_erase)
1055 /* erase previous character */
1058 /* none to erase! */
1063 fputs("\b \b", stdout);
1068 /* check for character validity and buffer overflow */
1069 else if (cnt == size || (numeric && !isdigit(ch)) ||
1077 /* echo it and store it in the buffer */
1088 /* all done -- null terminate the string */
1091 /* account for the extra characters in the message area */
1092 /* (if terminal overstrikes, remember the furthest they went) */
1093 msglen += overstrike ? maxcnt : cnt;
1095 /* return either inputted number or string length */
1097 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1100 /* internal support routines */
1103 summary_format(char *str, int *numbers, const char * const *names)
1107 const char *thisname;
1110 /* format each number followed by its string */
1112 while ((thisname = *names++) != NULL)
1114 /* get the number to format */
1117 /* display only non-zero numbers */
1120 /* is this number in kilobytes? */
1121 if (thisname[0] == 'K')
1123 /* yes: format it as a memory value */
1124 p = stpcpy(p, format_k(num));
1126 /* skip over the K, since it was included by format_k */
1127 p = stpcpy(p, thisname+1);
1129 /* is this number a ratio? */
1130 else if (thisname[0] == ':')
1132 (void) snprintf(rbuf, sizeof(rbuf), "%.2f",
1133 (float)*(numbers - 2) / (float)num);
1134 p = stpcpy(p, rbuf);
1135 p = stpcpy(p, thisname);
1139 p = stpcpy(p, itoa(num));
1140 p = stpcpy(p, thisname);
1144 /* ignore negative numbers, but display corresponding string */
1147 p = stpcpy(p, thisname);
1151 /* if the last two characters in the string are ", ", delete them */
1153 if (p >= str && p[0] == ',' && p[1] == ' ')
1160 line_update(char *old, char *new, int start, int line)
1164 int newcol = start + 1;
1165 int lastcol = start;
1166 char cursor_on_line = false;
1169 /* compare the two strings and only rewrite what has changed */
1172 fprintf(debug, "line_update, starting at %d\n", start);
1176 fputs("\n-\n", debug);
1179 /* start things off on the right foot */
1180 /* this is to make sure the invariants get set up right */
1181 if ((ch = *new++) != *old)
1183 if (line - lastline == 1 && start == 0)
1189 Move_to(start, line);
1191 cursor_on_line = true;
1199 * main loop -- check each character. If the old and new aren't the
1200 * same, then update the display. When the distance from the
1201 * current cursor position to the new change is small enough,
1202 * the characters that belong there are written to move the
1206 * lastcol is the column where the cursor currently is sitting
1207 * (always one beyond the end of the last mismatch).
1209 do /* yes, a do...while */
1211 if ((ch = *new++) != *old)
1213 /* new character is different from old */
1214 /* make sure the cursor is on top of this character */
1215 diff = newcol - lastcol;
1218 /* some motion is required--figure out which is shorter */
1219 if (diff < 6 && cursor_on_line)
1221 /* overwrite old stuff--get it out of the old buffer */
1222 printf("%.*s", diff, ¤t[lastcol-start]);
1226 /* use cursor addressing */
1227 Move_to(newcol, line);
1228 cursor_on_line = true;
1230 /* remember where the cursor is */
1231 lastcol = newcol + 1;
1235 /* already there, update position */
1239 /* write what we need to */
1242 /* at the end--terminate with a clear-to-end-of-line */
1243 (void) clear_eol(strlen(old));
1247 /* write the new character */
1250 /* put the new character in the screen buffer */
1254 /* update working column and screen buffer pointer */
1258 } while (ch != '\0');
1260 /* zero out the rest of the line buffer -- MUST BE DONE! */
1261 diff = screen_width - newcol;
1264 memset(old, 0, diff);
1267 /* remember where the current line is */
1275 * printable(str) - make the string pointed to by "str" into one that is
1276 * printable (i.e.: all ascii), by converting all non-printable
1277 * characters into '?'. Replacements are done in place and a pointer
1278 * to the original buffer is returned.
1282 printable(char str[])
1288 while ((ch = *ptr) != '\0')
1300 i_uptime(struct timeval *bt, time_t *tod)
1303 int days, hrs, mins, secs;
1305 if (bt->tv_sec != -1) {
1306 uptime = *tod - bt->tv_sec;
1307 days = uptime / 86400;
1309 hrs = uptime / 3600;
1315 * Display the uptime.
1320 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1326 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);
1330 #define SETUPBUFFER_REQUIRED_ADDBUFSIZ 2
1333 setup_buffer(char *buffer, int addlen)
1337 if (NULL == buffer) {
1338 setup_buffer_bufsiz = screen_width;
1339 b = calloc(setup_buffer_bufsiz + addlen +
1340 SETUPBUFFER_REQUIRED_ADDBUFSIZ,
1343 if (screen_width > setup_buffer_bufsiz) {
1344 setup_buffer_bufsiz = screen_width;
1346 b = calloc(setup_buffer_bufsiz + addlen +
1347 SETUPBUFFER_REQUIRED_ADDBUFSIZ,
1355 errx(4, "can't allocate sufficient memory");