2002-02-02 04:17:29 +05:30
|
|
|
/*
|
|
|
|
* tload.c - terminal version of xload
|
|
|
|
*
|
|
|
|
* Options:
|
|
|
|
* -s initial scaling exponent (default = 6)
|
|
|
|
* -d delay
|
|
|
|
*
|
|
|
|
* Copyright (c) 1992 Branko Lankester
|
|
|
|
* /proc changes by David Engel (david@ods.com)
|
|
|
|
* Made a little more efficient by Michael K. Johnson (johnsonm@sunsite.unc.edu)
|
2012-03-02 17:59:36 +05:30
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
2012-03-02 17:59:36 +05:30
|
|
|
|
2011-06-05 16:49:35 +05:30
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
2011-06-05 16:49:35 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2011-06-05 16:49:35 +05:30
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
1970-01-01 05:30:00 +05:30
|
|
|
#include <limits.h>
|
2020-06-30 10:30:00 +05:30
|
|
|
|
|
|
|
#include "c.h"
|
|
|
|
#include "fileutils.h"
|
|
|
|
#include "nls.h"
|
|
|
|
#include "strutils.h"
|
|
|
|
#include "xalloc.h"
|
|
|
|
|
2021-01-19 11:30:00 +05:30
|
|
|
#include <proc/misc.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
static char *screen;
|
|
|
|
|
|
|
|
static int nrows = 25;
|
|
|
|
static int ncols = 80;
|
|
|
|
static int scr_size;
|
1970-01-01 05:30:00 +05:30
|
|
|
static int fd = STDOUT_FILENO;
|
2011-12-18 18:59:19 +05:30
|
|
|
static unsigned int dly = 5;
|
2002-02-02 04:17:29 +05:30
|
|
|
static jmp_buf jb;
|
|
|
|
|
2011-06-05 16:49:35 +05:30
|
|
|
static void alrm(int signo __attribute__ ((__unused__)))
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2011-06-05 16:49:35 +05:30
|
|
|
signal(SIGALRM, alrm);
|
|
|
|
alarm(dly);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void setsize(int i)
|
|
|
|
{
|
2011-06-05 16:49:35 +05:30
|
|
|
struct winsize win;
|
|
|
|
|
|
|
|
signal(SIGWINCH, setsize);
|
|
|
|
if (ioctl(fd, TIOCGWINSZ, &win) != -1) {
|
|
|
|
if (win.ws_col > 0)
|
|
|
|
ncols = win.ws_col;
|
|
|
|
if (win.ws_row > 0)
|
|
|
|
nrows = win.ws_row;
|
|
|
|
}
|
1970-01-01 05:30:00 +05:30
|
|
|
if (ncols < 2 || ncols >= INT_MAX)
|
|
|
|
xerrx(EXIT_FAILURE, _("screen too small or too large"));
|
|
|
|
if (nrows < 2 || nrows >= INT_MAX / ncols)
|
|
|
|
xerrx(EXIT_FAILURE, _("screen too small or too large"));
|
2011-06-05 16:49:35 +05:30
|
|
|
scr_size = nrows * ncols;
|
2017-02-19 20:42:42 +05:30
|
|
|
if (scr_size < 2)
|
1970-01-01 05:30:00 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("screen too small"));
|
2011-06-05 16:49:35 +05:30
|
|
|
if (screen == NULL)
|
2011-10-10 00:59:26 +05:30
|
|
|
screen = (char *)xmalloc(scr_size);
|
2011-06-05 16:49:35 +05:30
|
|
|
else
|
2011-10-10 00:59:26 +05:30
|
|
|
screen = (char *)xrealloc(screen, scr_size);
|
2011-06-05 16:49:35 +05:30
|
|
|
|
|
|
|
memset(screen, ' ', scr_size - 1);
|
|
|
|
*(screen + scr_size - 2) = '\0';
|
|
|
|
if (i)
|
1970-01-01 05:30:00 +05:30
|
|
|
longjmp(jb, 1);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2011-10-09 15:55:19 +05:30
|
|
|
static void __attribute__ ((__noreturn__)) usage(FILE * out)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2011-10-09 15:55:19 +05:30
|
|
|
fputs(USAGE_HEADER, out);
|
2011-12-17 23:02:47 +05:30
|
|
|
fprintf(out,
|
|
|
|
_(" %s [options] [tty]\n"), program_invocation_short_name);
|
2011-10-09 15:55:19 +05:30
|
|
|
fputs(USAGE_OPTIONS, out);
|
2013-10-11 04:37:10 +05:30
|
|
|
fputs(_(" -d, --delay <secs> update delay in seconds\n"), out);
|
|
|
|
fputs(_(" -s, --scale <num> vertical scale\n"), out);
|
2011-10-09 15:55:19 +05:30
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(USAGE_HELP, out);
|
|
|
|
fputs(USAGE_VERSION, out);
|
|
|
|
fprintf(out, USAGE_MAN_TAIL("tload(1)"));
|
2011-06-05 16:49:35 +05:30
|
|
|
|
|
|
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-06-05 16:49:35 +05:30
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int lines, row, col = 0;
|
|
|
|
int i, opt;
|
|
|
|
double av[3];
|
2011-12-18 20:28:48 +05:30
|
|
|
static double max_scale = 0, scale_fact;
|
2011-12-18 18:59:19 +05:30
|
|
|
long tmpdly;
|
2011-06-05 16:49:35 +05:30
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{"scale", required_argument, NULL, 's'},
|
|
|
|
{"delay", required_argument, NULL, 'd'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
2013-02-20 23:01:48 +05:30
|
|
|
#ifdef HAVE_PROGRAM_INVOCATION_NAME
|
2012-01-03 13:18:43 +05:30
|
|
|
program_invocation_name = program_invocation_short_name;
|
2013-02-20 23:01:48 +05:30
|
|
|
#endif
|
2011-12-07 17:57:21 +05:30
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2012-03-23 18:02:24 +05:30
|
|
|
atexit(close_stdout);
|
2011-12-07 17:57:21 +05:30
|
|
|
|
|
|
|
while ((opt =
|
|
|
|
getopt_long(argc, argv, "s:d:Vh", longopts, NULL)) != -1)
|
2011-06-05 16:49:35 +05:30
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
2011-12-18 20:28:48 +05:30
|
|
|
max_scale = strtod_or_err(optarg, _("failed to parse argument"));
|
|
|
|
if (max_scale < 0)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("scale cannot be negative"));
|
2011-06-05 16:49:35 +05:30
|
|
|
break;
|
|
|
|
case 'd':
|
2011-12-18 18:59:19 +05:30
|
|
|
tmpdly = strtol_or_err(optarg, _("failed to parse argument"));
|
|
|
|
if (tmpdly < 1)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("delay must be positive integer"));
|
2011-12-18 18:59:19 +05:30
|
|
|
else if (UINT_MAX < tmpdly)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("too large delay value"));
|
2011-12-18 18:59:19 +05:30
|
|
|
dly = tmpdly;
|
2011-06-05 16:49:35 +05:30
|
|
|
break;
|
|
|
|
case 'V':
|
2011-10-09 15:55:19 +05:30
|
|
|
printf(PROCPS_NG_VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
2011-06-05 16:49:35 +05:30
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage(stdout);
|
|
|
|
default:
|
|
|
|
usage(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > optind)
|
1970-01-01 05:30:00 +05:30
|
|
|
if ((fd = open(argv[optind], O_WRONLY)) == -1)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerr(EXIT_FAILURE, _("can not open tty"));
|
2011-06-05 16:49:35 +05:30
|
|
|
|
|
|
|
setsize(0);
|
|
|
|
|
2011-12-18 20:28:48 +05:30
|
|
|
if (max_scale == 0)
|
2011-06-05 16:49:35 +05:30
|
|
|
max_scale = nrows;
|
|
|
|
|
|
|
|
scale_fact = max_scale;
|
|
|
|
|
|
|
|
setjmp(jb);
|
|
|
|
col = 0;
|
|
|
|
alrm(0);
|
|
|
|
|
|
|
|
while (1) {
|
2022-05-31 17:05:40 +05:30
|
|
|
int rc;
|
2011-06-05 16:49:35 +05:30
|
|
|
|
|
|
|
if (scale_fact < max_scale)
|
|
|
|
scale_fact *= 2.0; /* help it drift back up. */
|
|
|
|
|
2022-05-31 17:05:40 +05:30
|
|
|
if ((rc = procps_loadavg(&av[0], &av[1], &av[2])) < 0)
|
|
|
|
{
|
|
|
|
if (rc == -ENOENT)
|
|
|
|
xerrx(EXIT_FAILURE,
|
|
|
|
_("Load average file /proc/loadavg does not exist"));
|
|
|
|
else
|
|
|
|
xerrx(EXIT_FAILURE,
|
|
|
|
_("Unable to get load average"));
|
|
|
|
}
|
2011-06-05 16:49:35 +05:30
|
|
|
|
2014-06-07 06:01:02 +05:30
|
|
|
do {
|
2012-01-22 03:59:02 +05:30
|
|
|
lines = av[0] * scale_fact;
|
|
|
|
row = nrows - 1;
|
|
|
|
|
|
|
|
while (0 <= --lines) {
|
|
|
|
*(screen + row * ncols + col) = '*';
|
|
|
|
if (--row < 0) {
|
|
|
|
scale_fact /= 2.0;
|
|
|
|
break;
|
|
|
|
}
|
2011-06-05 16:49:35 +05:30
|
|
|
}
|
2014-06-07 06:01:02 +05:30
|
|
|
} while (0 <= lines);
|
2012-01-22 03:59:02 +05:30
|
|
|
|
2011-06-05 16:49:35 +05:30
|
|
|
while (row >= 0)
|
|
|
|
*(screen + row-- * ncols + col) = ' ';
|
|
|
|
|
|
|
|
for (i = 1;; ++i) {
|
|
|
|
char *p;
|
|
|
|
row = nrows - (i * scale_fact);
|
1970-01-01 05:30:00 +05:30
|
|
|
if (row < 0 || row >= nrows)
|
2011-06-05 16:49:35 +05:30
|
|
|
break;
|
|
|
|
if (*(p = screen + row * ncols + col) == ' ')
|
|
|
|
*p = '-';
|
|
|
|
else
|
|
|
|
*p = '=';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++col == ncols) {
|
|
|
|
--col;
|
|
|
|
memmove(screen, screen + 1, scr_size - 1);
|
|
|
|
|
|
|
|
for (row = nrows - 2; row >= 0; --row)
|
|
|
|
*(screen + row * ncols + col) = ' ';
|
|
|
|
}
|
1970-01-01 05:30:00 +05:30
|
|
|
i = snprintf(screen, scr_size, " %.2f, %.2f, %.2f", av[0], av[1], av[2]);
|
|
|
|
if (i > 0 && i < scr_size)
|
2011-06-05 16:49:35 +05:30
|
|
|
screen[i] = ' ';
|
|
|
|
|
2012-01-21 20:54:33 +05:30
|
|
|
if (write(fd, "\033[H", 3) < 0)
|
|
|
|
xerr(EXIT_FAILURE, _("writing to tty failed"));
|
|
|
|
if (write(fd, screen, scr_size - 1) < 0)
|
|
|
|
xerr(EXIT_FAILURE, _("writing to tty failed"));
|
2011-06-05 16:49:35 +05:30
|
|
|
pause();
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
}
|