NFE - Change default RX ring size from 128 -> 256, Adjust moderation timer.
[dragonfly.git] / contrib / top / display.c
bloba4c4c3c886c252fc527c7048060145d72218c427
1 /*
2 * Top users/processes display for Unix
3 * Version 3
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
11 * $FreeBSD: src/contrib/top/display.c,v 1.4.6.3 2003/04/27 15:32:26 dwmalone Exp $
12 * $DragonFly: src/contrib/top/display.c,v 1.3 2006/10/03 12:20:11 y0netan1 Exp $
16 * This file contains the routines that display information on the screen.
17 * Each section of the screen has two routines: one for initially writing
18 * all constant and dynamic text, and one for only updating the text that
19 * changes. The prefix "i_" is used on all the "initial" routines and the
20 * prefix "u_" is used for all the "updating" routines.
22 * ASSUMPTIONS:
23 * None of the "i_" routines use any of the termcap capabilities.
24 * In this way, those routines can be safely used on terminals that
25 * have minimal (or nonexistant) terminal capabilities.
27 * The routines are called in this order: *_loadave, i_timeofday,
28 * *_procstates, *_cpustates, *_memory, *_message, *_header,
29 * *_process, u_endscreen.
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include "os.h"
35 #include <ctype.h>
36 #include <time.h>
37 #include <curses.h>
38 #include <term.h>
39 #include <unistd.h>
41 #include "screen.h" /* interface to screen package */
42 #include "layout.h" /* defines for screen position layout */
43 #include "display.h"
44 #include "top.h"
45 #include "top.local.h"
46 #include "boolean.h"
47 #include "machine.h" /* we should eliminate this!!! */
48 #include "utils.h"
50 #ifdef DEBUG
51 FILE *debug;
52 #endif
54 /* imported from screen.c */
55 extern int overstrike;
57 static int lmpid = 0;
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)
64 char *printable(char *str);
66 /* things initialized by display_init and used thruout */
68 /* buffer of proc information lines for display updating */
69 char *screenbuf = NULL;
71 static const char **procstate_names;
72 static const char **cpustate_names;
73 static const char **memory_names;
74 static const char **swap_names;
76 static int num_procstates;
77 static int num_cpustates;
78 static int num_memory;
79 static int num_swap;
81 static int *lprocstates;
82 static int *lmemory;
83 static int *lswap;
85 static int *cpustate_columns;
86 static int cpustate_total_length;
88 static enum { OFF, ON, ERASE } header_status = ON;
90 static int string_count(const char **);
91 static void summary_format(char *str, int *numbers, const char **names);
92 static void line_update(char *old, char *new, int start, int line);
94 int
95 display_resize(void)
97 int xlines;
99 /* first, deallocate any previous buffer that may have been there */
100 if (screenbuf != NULL)
102 free(screenbuf);
105 /* calculate the current dimensions */
106 /* if operating in "dumb" mode, we only need one line */
107 xlines = smart_terminal ? screen_length - Header_lines : 1;
109 if (xlines < 0)
110 xlines = 0;
111 /* we don't want more than MAX_COLS columns, since the machine-dependent
112 modules make static allocations based on MAX_COLS and we don't want
113 to run off the end of their buffers */
114 display_width = screen_width;
115 if (display_width >= MAX_COLS)
117 display_width = MAX_COLS - 1;
120 /* now, allocate space for the screen buffer */
121 screenbuf = (char *)malloc(xlines * display_width);
122 if (screenbuf == (char *)NULL)
124 /* oops! */
125 return(-1);
128 /* return number of lines available */
129 /* for dumb terminals, pretend like we can show any amount */
130 return(smart_terminal ? xlines : Largest);
134 display_init(struct statics *statics)
136 int xlines;
137 const char **pp;
138 int *ip;
139 int i;
141 /* call resize to do the dirty work */
142 xlines = display_resize();
144 /* only do the rest if we need to */
145 if (xlines > -1)
147 /* save pointers and allocate space for names */
148 procstate_names = statics->procstate_names;
149 num_procstates = string_count(procstate_names);
150 lprocstates = (int *)malloc(num_procstates * sizeof(int));
152 cpustate_names = statics->cpustate_names;
154 swap_names = statics->swap_names;
155 num_swap = string_count(swap_names);
156 lswap = (int *)malloc(num_swap * sizeof(int));
157 num_cpustates = string_count(cpustate_names);
158 cpustate_columns = (int *)malloc(num_cpustates * sizeof(int));
160 memory_names = statics->memory_names;
161 num_memory = string_count(memory_names);
162 lmemory = (int *)malloc(num_memory * sizeof(int));
164 /* calculate starting columns where needed */
165 cpustate_total_length = 0;
166 pp = cpustate_names;
167 ip = cpustate_columns;
168 while (*pp != NULL)
170 *ip++ = cpustate_total_length;
171 if ((i = strlen(*pp++)) > 0)
173 cpustate_total_length += i + 8;
178 /* return number of lines available */
179 return(xlines);
182 void
183 i_loadave(int mpid, double *avenrun)
185 int i;
187 /* i_loadave also clears the screen, since it is first */
188 clear_myscreen();
190 /* mpid == -1 implies this system doesn't have an _mpid */
191 if (mpid != -1)
193 printf("last pid: %5d; ", mpid);
196 printf("load averages");
198 for (i = 0; i < 3; i++)
200 printf("%c %5.2f",
201 i == 0 ? ':' : ',',
202 avenrun[i]);
204 lmpid = mpid;
207 void
208 u_loadave(int mpid, double *avenrun)
210 register int i;
212 if (mpid != -1)
214 /* change screen only when value has really changed */
215 if (mpid != lmpid)
217 Move_to(x_lastpid, y_lastpid);
218 printf("%5d", mpid);
219 lmpid = mpid;
222 /* i remembers x coordinate to move to */
223 i = x_loadave;
225 else
227 i = x_loadave_nompid;
230 /* move into position for load averages */
231 Move_to(i, y_loadave);
233 /* display new load averages */
234 /* we should optimize this and only display changes */
235 for (i = 0; i < 3; i++)
237 printf("%s%5.2f",
238 i == 0 ? "" : ", ",
239 avenrun[i]);
243 void
244 i_timeofday(time_t *tod)
247 * Display the current time.
248 * "ctime" always returns a string that looks like this:
250 * Sun Sep 16 01:03:52 1973
251 * 012345678901234567890123
252 * 1 2
254 * We want indices 11 thru 18 (length 8).
257 if (smart_terminal)
259 Move_to(screen_width - 8, 0);
261 else
263 fputs(" ", stdout);
265 #ifdef DEBUG
267 char *foo;
268 foo = ctime(tod);
269 fputs(foo, stdout);
271 #endif
272 printf("%-8.8s\n", &(ctime(tod)[11]));
273 lastline = 1;
276 static int ltotal = 0;
277 static char procstates_buffer[MAX_COLS];
280 * *_procstates(total, brkdn, names) - print the process summary line
282 * Assumptions: cursor is at the beginning of the line on entry
283 * lastline is valid
286 void
287 i_procstates(int total, int *brkdn)
289 register int i;
291 /* write current number of processes and remember the value */
292 printf("%d processes:", total);
293 ltotal = total;
295 /* put out enough spaces to get to column 15 */
296 i = digits(total);
297 while (i++ < 4)
299 putchar(' ');
302 /* format and print the process state summary */
303 summary_format(procstates_buffer, brkdn, procstate_names);
304 fputs(procstates_buffer, stdout);
306 /* save the numbers for next time */
307 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
310 void
311 u_procstates(int total, int *brkdn)
313 static char new[MAX_COLS];
314 register int i;
316 /* update number of processes only if it has changed */
317 if (ltotal != total)
319 /* move and overwrite */
320 #if (x_procstate == 0)
321 Move_to(x_procstate, y_procstate);
322 #else
323 /* cursor is already there...no motion needed */
324 /* assert(lastline == 1); */
325 #endif
326 printf("%d", total);
328 /* if number of digits differs, rewrite the label */
329 if (digits(total) != digits(ltotal))
331 fputs(" processes:", stdout);
332 /* put out enough spaces to get to column 15 */
333 i = digits(total);
334 while (i++ < 4)
336 putchar(' ');
338 /* cursor may end up right where we want it!!! */
341 /* save new total */
342 ltotal = total;
345 /* see if any of the state numbers has changed */
346 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
348 /* format and update the line */
349 summary_format(new, brkdn, procstate_names);
350 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
351 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
356 * *_cpustates(states, names) - print the cpu state percentages
358 * Assumptions: cursor is on the PREVIOUS line
361 void
362 i_cpustates(struct system_info *si)
364 int i;
365 int value;
366 const char **names;
367 const char *thisname;
368 int *states = si->cpustates;
369 int cpu;
371 /* print tag and bump lastline */
372 for (cpu = 0; cpu < n_cpus; ++cpu) {
373 if (n_cpus > 1)
374 printf("\nCPU%d states: ", cpu);
375 else
376 printf("\nCPU states: ");
377 lastline++;
379 /* now walk thru the names and print the line */
380 names = cpustate_names;
381 i = 0;
382 while ((thisname = *names++) != NULL)
384 if (*thisname != '\0')
386 /* retrieve the value and remember it */
387 value = *states++;
388 /* if percentage is >= 1000, print it as 100% */
389 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
390 i++ == 0 ? "" : ", ",
391 ((float)value)/10.,
392 thisname);
398 void
399 z_cpustates(struct system_info *si __unused)
401 int i;
402 const char **names;
403 const char *thisname;
404 int cpu;
406 /* show tag and bump lastline */
407 for (cpu = 0; cpu < n_cpus; ++cpu) {
408 if (n_cpus > 1)
409 printf("\nCPU%d states: ", cpu);
410 else
411 printf("\nCPU states: ");
412 lastline++;
414 i = 0;
415 names = cpustate_names;
416 while ((thisname = *names++) != NULL)
418 if (*thisname != '\0')
420 printf("%s %% %s", i++ == 0 ? "" : ", ", thisname);
427 * *_memory(stats) - print "Memory: " followed by the memory summary string
429 * Assumptions: cursor is on "lastline"
430 * for i_memory ONLY: cursor is on the previous line
433 char memory_buffer[MAX_COLS];
435 void
436 i_memory(int *stats)
438 fputs("\nMem: ", stdout);
439 lastline++;
441 /* format and print the memory summary */
442 summary_format(memory_buffer, stats, memory_names);
443 fputs(memory_buffer, stdout);
446 void
447 u_memory(int *stats)
449 static char new[MAX_COLS];
451 /* format the new line */
452 summary_format(new, stats, memory_names);
453 line_update(memory_buffer, new, x_mem, y_mem);
457 * *_swap(stats) - print "Swap: " followed by the swap summary string
459 * Assumptions: cursor is on "lastline"
460 * for i_swap ONLY: cursor is on the previous line
463 char swap_buffer[MAX_COLS];
465 void
466 i_swap(int *stats)
468 fputs("\nSwap: ", stdout);
469 lastline++;
471 /* format and print the swap summary */
472 summary_format(swap_buffer, stats, swap_names);
473 fputs(swap_buffer, stdout);
476 void
477 u_swap(int *stats)
479 static char new[MAX_COLS];
481 /* format the new line */
482 summary_format(new, stats, swap_names);
483 line_update(swap_buffer, new, x_swap, y_swap);
487 * *_message() - print the next pending message line, or erase the one
488 * that is there.
490 * Note that u_message is (currently) the same as i_message.
492 * Assumptions: lastline is consistent
496 * i_message is funny because it gets its message asynchronously (with
497 * respect to screen updates).
500 static char next_msg[MAX_COLS + 5];
501 static int msglen = 0;
502 /* Invariant: msglen is always the length of the message currently displayed
503 on the screen (even when next_msg doesn't contain that message). */
505 void
506 i_message(void)
508 while (lastline < y_message)
510 fputc('\n', stdout);
511 lastline++;
513 if (next_msg[0] != '\0')
515 dostandout(next_msg);
516 msglen = strlen(next_msg);
517 next_msg[0] = '\0';
519 else if (msglen > 0)
521 (void) clear_eol(msglen);
522 msglen = 0;
526 void
527 u_message(void)
529 i_message();
532 static int header_length;
535 * *_header(text) - print the header for the process area
537 * Assumptions: cursor is on the previous line and lastline is consistent
540 void
541 i_header(char *text)
543 header_length = strlen(text);
544 if (header_status == ON)
546 putchar('\n');
547 fputs(text, stdout);
548 lastline++;
550 else if (header_status == ERASE)
552 header_status = OFF;
556 /*ARGSUSED*/
557 void
558 u_header(char *text __unused)
560 if (header_status == ERASE)
562 putchar('\n');
563 lastline++;
564 clear_eol(header_length);
565 header_status = OFF;
570 * *_process(line, thisline) - print one process line
572 * Assumptions: lastline is consistent
575 void
576 i_process(int line, char *thisline)
578 register char *p;
579 register char *base;
581 /* make sure we are on the correct line */
582 while (lastline < y_procs + line)
584 putchar('\n');
585 lastline++;
588 /* truncate the line to conform to our current screen width */
589 thisline[display_width] = '\0';
591 /* write the line out */
592 fputs(thisline, stdout);
594 /* copy it in to our buffer */
595 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
596 p = strecpy(base, thisline);
598 /* zero fill the rest of it */
599 memzero(p, display_width - (p - base));
602 void
603 u_process(int xline, char *xnewline)
605 char *optr;
606 int screen_line = xline + Header_lines;
607 char *bufferline;
609 /* remember a pointer to the current line in the screen buffer */
610 bufferline = &screenbuf[lineindex(xline)];
612 /* truncate the line to conform to our current screen width */
613 newline[display_width] = '\0';
615 /* is line higher than we went on the last display? */
616 if (xline >= last_hi)
618 /* yes, just ignore screenbuf and write it out directly */
619 /* get positioned on the correct line */
620 if (screen_line - lastline == 1)
622 putchar('\n');
623 lastline++;
625 else
627 Move_to(0, screen_line);
628 lastline = screen_line;
631 /* now write the line */
632 fputs(xnewline, stdout);
634 /* copy it in to the buffer */
635 optr = strecpy(bufferline, xnewline);
637 /* zero fill the rest of it */
638 memzero(optr, display_width - (optr - bufferline));
640 else
642 line_update(bufferline, xnewline, 0, xline + Header_lines);
646 void
647 u_endscreen(int hi)
649 int screen_line = hi + Header_lines;
650 int i;
652 if (smart_terminal)
654 if (hi < last_hi)
656 /* need to blank the remainder of the screen */
657 /* but only if there is any screen left below this line */
658 if (lastline + 1 < screen_length)
660 /* efficiently move to the end of currently displayed info */
661 if (screen_line - lastline < 5)
663 while (lastline < screen_line)
665 putchar('\n');
666 lastline++;
669 else
671 Move_to(0, screen_line);
672 lastline = screen_line;
675 if (clear_to_end)
677 /* we can do this the easy way */
678 putcap(clear_to_end);
680 else
682 /* use clear_eol on each line */
683 i = hi;
684 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
686 putchar('\n');
691 last_hi = hi;
693 /* move the cursor to a pleasant place */
694 Move_to(x_idlecursor, y_idlecursor);
695 lastline = y_idlecursor;
697 else
699 /* separate this display from the next with some vertical room */
700 fputs("\n\n", stdout);
704 void
705 display_header(int t)
707 if (t)
709 header_status = ON;
711 else if (header_status == ON)
713 header_status = ERASE;
717 /*VARARGS2*/
718 void
719 new_message(int type, const char *msgfmt, ...)
721 va_list va;
722 int i;
724 /* first, format the message */
725 va_start(va, msgfmt);
726 vsnprintf(next_msg, sizeof(next_msg), msgfmt, va);
727 va_end(va);
729 if (msglen > 0)
731 /* message there already -- can we clear it? */
732 if (!overstrike)
734 /* yes -- write it and clear to end */
735 i = strlen(next_msg);
736 if ((type & MT_delayed) == 0)
738 if (type & MT_standout)
739 dostandout(next_msg);
740 else
741 fputs(next_msg, stdout);
742 (void) clear_eol(msglen - i);
743 msglen = i;
744 next_msg[0] = '\0';
748 else
750 if ((type & MT_delayed) == 0)
752 if (type & MT_standout)
753 dostandout(next_msg);
754 else
755 fputs(next_msg, stdout);
756 msglen = strlen(next_msg);
757 next_msg[0] = '\0';
762 void
763 clear_message(void)
765 if (clear_eol(msglen) == 1)
767 putchar('\r');
772 readline(char *buffer, int size, int numeric)
774 char *ptr = buffer;
775 char ch;
776 char cnt = 0;
777 char maxcnt = 0;
779 /* allow room for null terminator */
780 size -= 1;
782 /* read loop */
783 while ((fflush(stdout), read(0, ptr, 1) > 0))
785 /* newline means we are done */
786 if ((ch = *ptr) == '\n' || ch == '\r')
788 break;
791 /* handle special editing characters */
792 if (ch == ch_kill)
794 /* kill line -- account for overstriking */
795 if (overstrike)
797 msglen += maxcnt;
800 /* return null string */
801 *buffer = '\0';
802 putchar('\r');
803 return(-1);
805 else if (ch == ch_erase)
807 /* erase previous character */
808 if (cnt <= 0)
810 /* none to erase! */
811 putchar('\7');
813 else
815 fputs("\b \b", stdout);
816 ptr--;
817 cnt--;
820 /* check for character validity and buffer overflow */
821 else if (cnt == size || (numeric && !isdigit(ch)) ||
822 !isprint(ch))
824 /* not legal */
825 putchar('\7');
827 else
829 /* echo it and store it in the buffer */
830 putchar(ch);
831 ptr++;
832 cnt++;
833 if (cnt > maxcnt)
835 maxcnt = cnt;
840 /* all done -- null terminate the string */
841 *ptr = '\0';
843 /* account for the extra characters in the message area */
844 /* (if terminal overstrikes, remember the furthest they went) */
845 msglen += overstrike ? maxcnt : cnt;
847 /* return either inputted number or string length */
848 putchar('\r');
849 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
852 /* internal support routines */
854 static int
855 string_count(const char **pp)
857 int cnt;
859 cnt = 0;
860 while (*pp++ != NULL)
862 cnt++;
864 return(cnt);
867 static void
868 summary_format(char *str, int *numbers, const char **names)
870 char *p;
871 int num;
872 const char *thisname;
874 /* format each number followed by its string */
875 p = str;
876 while ((thisname = *names++) != NULL)
878 /* get the number to format */
879 num = *numbers++;
881 /* display only non-zero numbers */
882 if (num > 0)
884 /* is this number in kilobytes? */
885 if (thisname[0] == 'K')
887 /* yes: format it as a memory value */
888 p = strecpy(p, format_k(num));
890 /* skip over the K, since it was included by format_k */
891 p = strecpy(p, thisname+1);
893 else
895 p = strecpy(p, ltoa(num));
896 p = strecpy(p, thisname);
900 /* ignore negative numbers, but display corresponding string */
901 else if (num < 0)
903 p = strecpy(p, thisname);
907 /* if the last two characters in the string are ", ", delete them */
908 p -= 2;
909 if (p >= str && p[0] == ',' && p[1] == ' ')
911 *p = '\0';
915 static void
916 line_update(char *old, char *new, int start, int line)
918 register int ch;
919 register int diff;
920 register int newcol = start + 1;
921 register int lastcol = start;
922 char cursor_on_line = No;
923 char *current;
925 /* compare the two strings and only rewrite what has changed */
926 current = old;
927 #ifdef DEBUG
928 fprintf(debug, "line_update, starting at %d\n", start);
929 fputs(old, debug);
930 fputc('\n', debug);
931 fputs(new, debug);
932 fputs("\n-\n", debug);
933 #endif
935 /* start things off on the right foot */
936 /* this is to make sure the invariants get set up right */
937 if ((ch = *new++) != *old)
939 if (line - lastline == 1 && start == 0)
941 putchar('\n');
943 else
945 Move_to(start, line);
947 cursor_on_line = Yes;
948 putchar(ch);
949 *old = ch;
950 lastcol = 1;
952 old++;
955 * main loop -- check each character. If the old and new aren't the
956 * same, then update the display. When the distance from the
957 * current cursor position to the new change is small enough,
958 * the characters that belong there are written to move the
959 * cursor over.
961 * Invariants:
962 * lastcol is the column where the cursor currently is sitting
963 * (always one beyond the end of the last mismatch).
965 do /* yes, a do...while */
967 if ((ch = *new++) != *old)
969 /* new character is different from old */
970 /* make sure the cursor is on top of this character */
971 diff = newcol - lastcol;
972 if (diff > 0)
974 /* some motion is required--figure out which is shorter */
975 if (diff < 6 && cursor_on_line)
977 /* overwrite old stuff--get it out of the old buffer */
978 printf("%.*s", diff, &current[lastcol-start]);
980 else
982 /* use cursor addressing */
983 Move_to(newcol, line);
984 cursor_on_line = Yes;
986 /* remember where the cursor is */
987 lastcol = newcol + 1;
989 else
991 /* already there, update position */
992 lastcol++;
995 /* write what we need to */
996 if (ch == '\0')
998 /* at the end--terminate with a clear-to-end-of-line */
999 (void) clear_eol(strlen(old));
1001 else
1003 /* write the new character */
1004 putchar(ch);
1006 /* put the new character in the screen buffer */
1007 *old = ch;
1010 /* update working column and screen buffer pointer */
1011 newcol++;
1012 old++;
1014 } while (ch != '\0');
1016 /* zero out the rest of the line buffer -- MUST BE DONE! */
1017 diff = display_width - newcol;
1018 if (diff > 0)
1020 memzero(old, diff);
1023 /* remember where the current line is */
1024 if (cursor_on_line)
1026 lastline = line;
1031 * printable(str) - make the string pointed to by "str" into one that is
1032 * printable (i.e.: all ascii), by converting all non-printable
1033 * characters into '?'. Replacements are done in place and a pointer
1034 * to the original buffer is returned.
1037 char *
1038 printable(char *str)
1040 char *ptr;
1041 char ch;
1043 ptr = str;
1044 while ((ch = *ptr) != '\0')
1046 if (!isprint(ch))
1048 *ptr = '?';
1050 ptr++;
1052 return(str);
1055 void
1056 i_uptime(struct timeval *bt, time_t *tod)
1058 time_t uptime;
1059 int days, hrs, mins, secs;
1061 if (bt->tv_sec != -1) {
1062 uptime = *tod - bt->tv_sec;
1063 uptime += 30;
1064 days = uptime / 86400;
1065 uptime %= 86400;
1066 hrs = uptime / 3600;
1067 uptime %= 3600;
1068 mins = uptime / 60;
1069 secs = uptime % 60;
1072 * Display the uptime.
1075 if (smart_terminal)
1077 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1079 else
1081 fputs(" ", stdout);
1083 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);