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
13 * This file contains the routines that display information on the screen.
14 * Each section of the screen has two routines: one for initially writing
15 * all constant and dynamic text, and one for only updating the text that
16 * changes. The prefix "i_" is used on all the "initial" routines and the
17 * prefix "u_" is used for all the "updating" routines.
20 * None of the "i_" routines use any of the termcap capabilities.
21 * In this way, those routines can be safely used on terminals that
22 * have minimal (or nonexistant) terminal capabilities.
24 * The routines are called in this order: *_loadave, i_timeofday,
25 * *_procstates, *_cpustates, *_memory, *_message, *_header,
26 * *_process, u_endscreen.
29 #include <sys/cdefs.h>
30 #include <sys/resource.h>
45 #include "screen.h" /* interface to screen package */
46 #include "layout.h" /* defines for screen position layout */
49 #include "machine.h" /* we should eliminate this!!! */
57 static int last_hi = 0; /* used in u_process and u_endscreen */
58 static int lastline = 0;
60 #define lineindex(l) ((l)*screen_width)
63 /* things initialized by display_init and used thruout */
65 /* buffer of proc information lines for display updating */
66 static char *screenbuf = NULL;
68 static const char * const *procstate_names;
69 static const char * const *cpustate_names;
70 static const char * const *memory_names;
71 static const char * const *arc_names;
72 static const char * const *carc_names;
73 static const char * const *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 void summary_format(char *, int *, const char * const *);
93 static void line_update(char *, char *, int, int);
95 static int setup_buffer_bufsiz = 0;
96 static char * setup_buffer(char *, int);
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;
143 /* now, allocate space for the screen buffer */
144 screenbuf = calloc(lines, screen_width);
145 if (screenbuf == NULL)
151 /* return number of lines available */
152 /* for dumb terminals, pretend like we can show any amount */
153 return(smart_terminal ? lines : Largest);
157 display_updatecpus(struct statics *statics)
162 /* call resize to do the dirty work */
163 lines = display_resize();
165 num_cpus = statics->ncpus;
168 cpustates_column = 5; /* CPU: */
170 cpustates_column += 1 + digits(num_cpus); /* CPU #: */
173 /* fill the "last" array with all -1s, to insure correct updating */
174 for (i = 0; i < num_cpustates * num_cpus; ++i) {
182 display_init(struct statics * statics)
185 const char * const *pp;
189 lines = display_updatecpus(statics);
191 /* only do the rest if we need to */
194 /* save pointers and allocate space for names */
195 procstate_names = statics->procstate_names;
197 assert(num_procstates > 0);
198 lprocstates = calloc(num_procstates, sizeof(int));
200 cpustate_names = statics->cpustate_names;
202 swap_names = statics->swap_names;
204 assert(num_swap > 0);
205 lswap = calloc(num_swap, sizeof(int));
206 num_cpustates = CPUSTATES;
207 assert(num_cpustates > 0);
208 lcpustates = calloc(num_cpustates * sizeof(int), statics->ncpus);
209 cpustate_columns = calloc(num_cpustates, sizeof(int));
211 memory_names = statics->memory_names;
213 assert(num_memory > 0);
214 lmemory = calloc(num_memory, sizeof(int));
216 arc_names = statics->arc_names;
217 carc_names = statics->carc_names;
219 /* calculate starting columns where needed */
220 cpustate_total_length = 0;
222 ip = cpustate_columns;
225 *ip++ = cpustate_total_length;
226 if ((i = strlen(*pp++)) > 0)
228 cpustate_total_length += i + 8;
233 /* return number of lines available */
238 i_loadave(int mpid, double avenrun[])
242 /* i_loadave also clears the screen, since it is first */
245 /* mpid == -1 implies this system doesn't have an _mpid */
248 printf("last pid: %5d; ", mpid);
251 printf("load averages");
253 for (i = 0; i < 3; i++)
263 u_loadave(int mpid, double *avenrun)
269 /* change screen only when value has really changed */
272 Move_to(x_lastpid, y_lastpid);
277 /* i remembers x coordinate to move to */
282 i = x_loadave_nompid;
285 /* move into position for load averages */
286 Move_to(i, y_loadave);
288 /* display new load averages */
289 /* we should optimize this and only display changes */
290 for (i = 0; i < 3; i++)
299 i_timeofday(time_t *tod)
302 * Display the current time.
303 * "ctime" always returns a string that looks like this:
305 * Sun Sep 16 01:03:52 1973
306 * 012345678901234567890123
309 * We want indices 11 thru 18 (length 8).
314 Move_to(screen_width - 8, 0);
327 printf("%-8.8s\n", &(ctime(tod)[11]));
331 static int ltotal = 0;
332 static char *procstates_buffer = NULL;
335 * *_procstates(total, brkdn, names) - print the process summary line
337 * Assumptions: cursor is at the beginning of the line on entry
342 i_procstates(int total, int *brkdn)
346 procstates_buffer = setup_buffer(procstates_buffer, 0);
348 /* write current number of processes and remember the value */
349 printf("%d %s:", total, ps.thread ? "threads" : "processes");
352 /* put out enough spaces to get to column 15 */
354 while (i++ < (ps.thread ? 6 : 4))
359 /* format and print the process state summary */
360 summary_format(procstates_buffer, brkdn, procstate_names);
361 fputs(procstates_buffer, stdout);
363 /* save the numbers for next time */
364 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
368 u_procstates(int total, int *brkdn)
370 static char *new = NULL;
373 new = setup_buffer(new, 0);
375 /* update number of processes only if it has changed */
378 /* move and overwrite */
379 if (x_procstate == 0) {
380 Move_to(x_procstate, y_procstate);
383 /* cursor is already there...no motion needed */
384 assert(lastline == 1);
388 /* if number of digits differs, rewrite the label */
389 if (digits(total) != digits(ltotal))
391 printf(" %s:", ps.thread ? "threads" : "processes");
392 /* put out enough spaces to get to column 15 */
394 while (i++ < (ps.thread ? 6 : 4))
398 /* cursor may end up right where we want it!!! */
405 /* see if any of the state numbers has changed */
406 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
408 /* format and update the line */
409 summary_format(new, brkdn, procstate_names);
410 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
411 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
416 i_cpustates(int *states)
420 const char * const *names;
421 const char *thisname;
422 int *hstates = states;
425 for (cpu = 0; cpu < num_cpus; cpu++) {
426 names = cpustate_names;
428 /* print tag and bump lastline */
432 value = printf("\nCPU %d: ", cpu);
433 while (value++ <= cpustates_column)
438 /* now walk thru the names and print the line */
439 while ((thisname = *names++) != NULL)
441 if (*thisname != '\0')
443 /* retrieve the value and remember it */
446 /* if percentage is >= 1000, print it as 100% */
447 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
448 (i++ % num_cpustates) == 0 ? "" : ", ",
455 /* copy over values into "last" array */
457 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
461 u_cpustates(int *states)
464 const char * const *names;
465 const char *thisname;
466 int *hstates = states;
471 for (cpu = 0; cpu < num_cpus; cpu++) {
472 names = cpustate_names;
474 Move_to(cpustates_column, y_cpustates + cpu);
475 lastline = y_cpustates + cpu;
476 lp = lcpustates + (cpu * num_cpustates);
477 colp = cpustate_columns;
479 /* we could be much more optimal about this */
480 while ((thisname = *names++) != NULL)
482 if (*thisname != '\0')
484 /* did the value change since last time? */
487 /* yes, move and change */
488 Move_to(cpustates_column + *colp, y_cpustates + cpu);
489 lastline = y_cpustates + cpu;
491 /* retrieve value and remember it */
494 /* if percentage is >= 1000, print it as 100% */
495 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
496 ((double)value)/10.);
498 /* remember it for next time */
503 /* increment and move on */
517 const char * const *names;
518 const char *thisname;
521 for (cpu = 0; cpu < num_cpus; cpu++) {
522 names = cpustate_names;
524 /* show tag and bump lastline */
528 value = printf("\nCPU %d: ", cpu);
529 while (value++ <= cpustates_column)
534 while ((thisname = *names++) != NULL)
536 if (*thisname != '\0')
538 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
543 /* fill the "last" array with all -1s, to insure correct updating */
544 for (i = 0; i < num_cpustates * num_cpus; ++i) {
550 * *_memory(stats) - print "Memory: " followed by the memory summary string
552 * Assumptions: cursor is on "lastline"
553 * for i_memory ONLY: cursor is on the previous line
556 static char *memory_buffer = NULL;
561 memory_buffer = setup_buffer(memory_buffer, 0);
563 fputs("\nMem: ", stdout);
566 /* format and print the memory summary */
567 summary_format(memory_buffer, stats, memory_names);
568 fputs(memory_buffer, stdout);
574 static char *new = NULL;
576 new = setup_buffer(new, 0);
578 /* format the new line */
579 summary_format(new, stats, memory_names);
580 line_update(memory_buffer, new, x_mem, y_mem);
584 * *_arc(stats) - print "ARC: " followed by the ARC summary string
586 * Assumptions: cursor is on "lastline"
587 * for i_arc ONLY: cursor is on the previous line
589 static char *arc_buffer = NULL;
594 arc_buffer = setup_buffer(arc_buffer, 0);
596 if (arc_names == NULL)
599 fputs("\nARC: ", stdout);
602 /* format and print the memory summary */
603 summary_format(arc_buffer, stats, arc_names);
604 fputs(arc_buffer, stdout);
610 static char *new = NULL;
612 new = setup_buffer(new, 0);
614 if (arc_names == NULL)
617 /* format the new line */
618 summary_format(new, stats, arc_names);
619 line_update(arc_buffer, new, x_arc, y_arc);
624 * *_carc(stats) - print "Compressed ARC: " followed by the summary string
626 * Assumptions: cursor is on "lastline"
627 * for i_carc ONLY: cursor is on the previous line
629 static char *carc_buffer = NULL;
634 carc_buffer = setup_buffer(carc_buffer, 0);
636 if (carc_names == NULL)
639 fputs("\n ", stdout);
642 /* format and print the memory summary */
643 summary_format(carc_buffer, stats, carc_names);
644 fputs(carc_buffer, stdout);
650 static char *new = NULL;
652 new = setup_buffer(new, 0);
654 if (carc_names == NULL)
657 /* format the new line */
658 summary_format(new, stats, carc_names);
659 line_update(carc_buffer, new, x_carc, y_carc);
663 * *_swap(stats) - print "Swap: " followed by the swap summary string
665 * Assumptions: cursor is on "lastline"
666 * for i_swap ONLY: cursor is on the previous line
669 static char *swap_buffer = NULL;
674 swap_buffer = setup_buffer(swap_buffer, 0);
676 if (swap_names == NULL)
679 fputs("\nSwap: ", stdout);
682 /* format and print the swap summary */
683 summary_format(swap_buffer, stats, swap_names);
684 fputs(swap_buffer, stdout);
690 static char *new = NULL;
692 new = setup_buffer(new, 0);
694 if (swap_names == NULL)
697 /* format the new line */
698 summary_format(new, stats, swap_names);
699 line_update(swap_buffer, new, x_swap, y_swap);
703 * *_message() - print the next pending message line, or erase the one
706 * Note that u_message is (currently) the same as i_message.
708 * Assumptions: lastline is consistent
712 * i_message is funny because it gets its message asynchronously (with
713 * respect to screen updates).
716 #define NEXT_MSG_ADDLEN 5
717 static char *next_msg = NULL;
718 static int msglen = 0;
719 /* Invariant: msglen is always the length of the message currently displayed
720 on the screen (even when next_msg doesn't contain that message). */
725 next_msg = setup_buffer(next_msg, NEXT_MSG_ADDLEN);
727 while (lastline < y_message)
732 if (next_msg[0] != '\0')
734 top_standout(next_msg);
735 msglen = strlen(next_msg);
740 (void) clear_eol(msglen);
751 static int header_length;
754 * Trim a header string to the current display width and return a newly
755 * allocated area with the trimmed header.
759 trim_header(const char *text)
765 width = screen_width;
766 header_length = strlen(text);
767 if (header_length >= width) {
768 s = strndup(text, width);
776 * *_header(text) - print the header for the process area
778 * Assumptions: cursor is on the previous line and lastline is consistent
782 i_header(const char *text)
786 s = trim_header(text);
790 if (header_status == ON)
796 else if (header_status == ERASE)
804 u_header(const char *text __unused)
807 if (header_status == ERASE)
811 clear_eol(header_length);
817 * *_process(line, thisline) - print one process line
819 * Assumptions: lastline is consistent
823 i_process(int line, char *thisline)
828 /* make sure we are on the correct line */
829 while (lastline < y_procs + line)
835 /* truncate the line to conform to our current screen width */
836 int len = strlen(thisline);
837 if (screen_width < len)
839 thisline[screen_width] = '\0';
842 /* write the line out */
843 fputs(thisline, stdout);
845 /* copy it in to our buffer */
846 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
847 p = stpcpy(base, thisline);
849 /* zero fill the rest of it */
850 if (p - base < screen_width)
852 memset(p, 0, screen_width - (p - base));
857 u_process(int line, char *newline)
860 int screen_line = line + Header_lines;
863 /* remember a pointer to the current line in the screen buffer */
864 bufferline = &screenbuf[lineindex(line)];
866 /* truncate the line to conform to our current screen width */
867 int len = strlen(newline);
868 if (screen_width < len)
870 newline[screen_width] = '\0';
873 /* is line higher than we went on the last display? */
876 /* yes, just ignore screenbuf and write it out directly */
877 /* get positioned on the correct line */
878 if (screen_line - lastline == 1)
885 Move_to(0, screen_line);
886 lastline = screen_line;
889 /* now write the line */
890 fputs(newline, stdout);
892 /* copy it in to the buffer */
893 optr = stpcpy(bufferline, newline);
895 /* zero fill the rest of it */
896 if (optr - bufferline < screen_width)
898 memset(optr, 0, screen_width - (optr - bufferline));
903 line_update(bufferline, newline, 0, line + Header_lines);
910 int screen_line = hi + Header_lines;
917 /* need to blank the remainder of the screen */
918 /* but only if there is any screen left below this line */
919 if (lastline + 1 < screen_length)
921 /* efficiently move to the end of currently displayed info */
922 if (screen_line - lastline < 5)
924 while (lastline < screen_line)
932 Move_to(0, screen_line);
933 lastline = screen_line;
938 /* we can do this the easy way */
939 putcap(clear_to_end);
943 /* use clear_eol on each line */
945 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
954 /* move the cursor to a pleasant place */
955 Move_to(x_idlecursor, y_idlecursor);
956 lastline = y_idlecursor;
960 /* separate this display from the next with some vertical room */
961 fputs("\n\n", stdout);
966 display_header(int t)
973 else if (header_status == ON)
975 header_status = ERASE;
980 new_message(int type, const char *msgfmt, ...)
985 va_start(args, msgfmt);
987 /* first, format the message */
988 vsnprintf(next_msg, setup_buffer_bufsiz + NEXT_MSG_ADDLEN,
995 /* message there already -- can we clear it? */
998 /* yes -- write it and clear to end */
999 i = strlen(next_msg);
1000 if ((type & MT_delayed) == 0)
1002 if (type & MT_standout) {
1003 top_standout(next_msg);
1005 fputs(next_msg, stdout);
1007 clear_eol(msglen - i);
1015 if ((type & MT_delayed) == 0)
1017 if (type & MT_standout) {
1018 top_standout(next_msg);
1020 fputs(next_msg, stdout);
1022 msglen = strlen(next_msg);
1031 if (clear_eol(msglen) == 1)
1038 readline(char *buffer, int size, int numeric)
1045 /* allow room for null terminator */
1049 while ((fflush(stdout), read(0, ptr, 1) > 0))
1051 /* newline means we are done */
1052 if ((ch = *ptr) == '\n' || ch == '\r')
1057 /* handle special editing characters */
1060 /* kill line -- account for overstriking */
1066 /* return null string */
1071 else if (ch == ch_erase)
1073 /* erase previous character */
1076 /* none to erase! */
1081 fputs("\b \b", stdout);
1086 /* check for character validity and buffer overflow */
1087 else if (cnt == size || (numeric && !isdigit(ch)) ||
1095 /* echo it and store it in the buffer */
1106 /* all done -- null terminate the string */
1109 /* account for the extra characters in the message area */
1110 /* (if terminal overstrikes, remember the furthest they went) */
1111 msglen += overstrike ? maxcnt : cnt;
1113 /* return either inputted number or string length */
1115 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1118 /* internal support routines */
1121 summary_format(char *str, int *numbers, const char * const *names)
1125 const char *thisname;
1128 /* format each number followed by its string */
1130 while ((thisname = *names++) != NULL)
1132 /* get the number to format */
1135 /* display only non-zero numbers */
1138 /* is this number in kilobytes? */
1139 if (thisname[0] == 'K')
1141 /* yes: format it as a memory value */
1142 p = stpcpy(p, format_k(num));
1144 /* skip over the K, since it was included by format_k */
1145 p = stpcpy(p, thisname+1);
1147 /* is this number a ratio? */
1148 else if (thisname[0] == ':')
1150 (void) snprintf(rbuf, sizeof(rbuf), "%.2f",
1151 (float)*(numbers - 2) / (float)num);
1152 p = stpcpy(p, rbuf);
1153 p = stpcpy(p, thisname);
1157 p = stpcpy(p, itoa(num));
1158 p = stpcpy(p, thisname);
1162 /* ignore negative numbers, but display corresponding string */
1165 p = stpcpy(p, thisname);
1169 /* if the last two characters in the string are ", ", delete them */
1171 if (p >= str && p[0] == ',' && p[1] == ' ')
1178 line_update(char *old, char *new, int start, int line)
1182 int newcol = start + 1;
1183 int lastcol = start;
1184 char cursor_on_line = false;
1187 /* compare the two strings and only rewrite what has changed */
1190 fprintf(debug, "line_update, starting at %d\n", start);
1194 fputs("\n-\n", debug);
1197 /* start things off on the right foot */
1198 /* this is to make sure the invariants get set up right */
1199 if ((ch = *new++) != *old)
1201 if (line - lastline == 1 && start == 0)
1207 Move_to(start, line);
1209 cursor_on_line = true;
1212 lastcol = start + 1;
1217 * main loop -- check each character. If the old and new aren't the
1218 * same, then update the display. When the distance from the
1219 * current cursor position to the new change is small enough,
1220 * the characters that belong there are written to move the
1224 * lastcol is the column where the cursor currently is sitting
1225 * (always one beyond the end of the last mismatch).
1227 do /* yes, a do...while */
1229 if ((ch = *new++) != *old)
1231 /* new character is different from old */
1232 /* make sure the cursor is on top of this character */
1233 diff = newcol - lastcol;
1236 /* some motion is required--figure out which is shorter */
1237 if (diff < 6 && cursor_on_line)
1239 /* overwrite old stuff--get it out of the old buffer */
1240 printf("%.*s", diff, ¤t[lastcol-start]);
1244 /* use cursor addressing */
1245 Move_to(newcol, line);
1246 cursor_on_line = true;
1248 /* remember where the cursor is */
1249 lastcol = newcol + 1;
1253 /* already there, update position */
1257 /* write what we need to */
1260 /* at the end--terminate with a clear-to-end-of-line */
1261 (void) clear_eol(strlen(old));
1265 /* write the new character */
1268 /* put the new character in the screen buffer */
1272 /* update working column and screen buffer pointer */
1276 } while (ch != '\0');
1278 /* zero out the rest of the line buffer -- MUST BE DONE! */
1279 diff = screen_width - newcol;
1282 memset(old, 0, diff);
1285 /* remember where the current line is */
1293 i_uptime(struct timeval *bt, time_t *tod)
1296 int days, hrs, mins, secs;
1298 if (bt->tv_sec != -1) {
1299 uptime = *tod - bt->tv_sec;
1300 days = uptime / 86400;
1302 hrs = uptime / 3600;
1308 * Display the uptime.
1313 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1319 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);
1324 i_battery(int nbat, int batt)
1328 printf("; battery: %d%%", batt);
1332 #define SETUPBUFFER_MIN_SCREENWIDTH 80
1333 #define SETUPBUFFER_REQUIRED_ADDBUFSIZ 2
1336 setup_buffer(char *buffer, int addlen)
1338 size_t len, old_len;
1341 setup_buffer_bufsiz = screen_width;
1342 if (setup_buffer_bufsiz < SETUPBUFFER_MIN_SCREENWIDTH)
1344 setup_buffer_bufsiz = SETUPBUFFER_MIN_SCREENWIDTH;
1347 len = setup_buffer_bufsiz + addlen + SETUPBUFFER_REQUIRED_ADDBUFSIZ;
1348 new_buffer = calloc(len, sizeof(char));
1349 if (new_buffer == NULL)
1351 errx(4, "can't allocate sufficient memory");
1355 old_len = strlen(buffer);
1356 memcpy(new_buffer, buffer, old_len < len - 1 ? old_len : len - 1);