/* $Id: lb.c 795 2013-04-22 05:12:27Z aqua $
*
* lookbusy -- a tool for producing synthetic CPU, memory consumption and
* disk loads on a host.
*
* Copyright (c) 2006, Devin Carraway <lookbusy@devin.com>.
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/* and, redundantly ... */
static const char *copyright =
"Copyright (c) 2006-2008, by Devin Carraway <lookbusy@devin.com>\n"
"$Id: lb.c 795 2013-04-22 05:12:27Z aqua $\n"
"\n"
"This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 2 of the License, or\n"
"(at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
"";
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define VERSION "1.4"
#define PACKAGE "lookbusy"
#endif
#include <stdio.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#define _GNU_SOURCE
#include <getopt.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <inttypes.h>
#include <ctype.h>
#include <regex.h>
#include <stdarg.h>
#include <math.h>
#ifndef HAVE_STRTOL
#define strtol(x,e,b) atol(x)
#endif
#ifndef HAVE_STRTOLL
#define strtoll(x,e,b) atol(x)
#endif
#ifdef HAVE_SYSCONF
#define LB_PAGE_SIZE sysconf(_SC_PAGESIZE)
#else
#define LB_PAGE_SIZE 4096
#endif
#ifdef _FILE_OFFSET_BITS
#if _FILE_OFFSET_BITS == 64
#define HAVE_LFS 1
#endif
#else
#define HAVE_LFS (sizeof(off_t) >= 8)
#endif
#define PI 3.14159265358979323846
enum cpu_util_mode {
UTIL_MODE_FIXED = 0,
UTIL_MODE_CURVE
};
enum cpu_util_mode c_cpu_util_mode = UTIL_MODE_FIXED;
static int utc = 0;
static int c_cpu_curve_period = 86400; /* seconds */
static int c_cpu_curve_peak = 60 * 60 * 13; /* 1PM local time */
static int c_cpu_util_l = 50, c_cpu_util_h = 50; /* percent */
static size_t c_mem_util = 0; /* bytes */
static long c_mem_stir_sleep = 1000; /* 1000 usec / 1 ms */
static off_t c_disk_util = 0; /* MB */
static char **c_disk_churn_paths;
static size_t c_disk_churn_paths_n;
static size_t c_disk_churn_block_size = 32 * 1024; /* bytes */
static size_t c_disk_churn_step_size = 4 * 1024; /* bytes */
static long c_disk_churn_sleep = 100; /* ms */
static int ncpus = -1; /* autodetect */
static int verbosity = 1;
static char *mem_stir_buffer;
static pid_t *cpu_pids;
static size_t n_cpu_pids;
static pid_t *disk_pids;
static size_t n_disk_pids;
static pid_t mem_pid;
typedef void (*spinner_fn)(long long, long long, long long, void*, void *);
static int say(int level, const char *fmt, ...)
{
va_list ap;
int r;
if (level > verbosity) return 0;
va_start(ap, fmt);
r = vprintf(fmt, ap);
va_end(ap);
return r;
}
static int err(const char *fmt, ...)
{
va_list ap;
int r;
va_start(ap, fmt);
r = vfprintf(stderr, fmt, ap);
va_end(ap);
return r;
}
static int parse_timespan(const char *str, int *r)
{
regex_t ex;
int e;
static const char *pattern =
"^[[:space:]]*([0-9]+)([dhms]?)[[:space:]]*$";
if ((e = regcomp(&ex,pattern, REG_EXTENDED)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regcomp(%s): %s\n", pattern, errbuf);
return -1;
}
regmatch_t matches[3];
if ((e = regexec(&ex, str, 3, matches, 0)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
if (e != REG_NOMATCH)
err("regexec: Couldn't match '%s' in '%s': %s\n",
pattern, str, errbuf);
return -1;
}
*r = (int)strtol(str + matches[1].rm_so, NULL, 10) *
(matches[2].rm_so == -1 ? 1 :
*(str + matches[2].rm_so) == 'd' ? 86400 :
*(str + matches[2].rm_so) == 'h' ? 3600 :
*(str + matches[2].rm_so) == 'm' ? 60 : 1);
return 0;
}
static int parse_large_size(const char *str, off_t *r)
{
regex_t ex;
int e;
static const char *pattern = HAVE_LFS ?
"^[[:space:]]*([0-9]+)(b|kb?|mb?|gb?|tb?)?[[:space:]]*$" :
"^[[:space:]]*([0-9]+)(b|kb?|mb?|gb?)?[[:space:]]*$";
if ((e = regcomp(&ex,pattern, REG_EXTENDED | REG_ICASE)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regcomp(%s): %s\n", pattern, errbuf);
return -1;
}
regmatch_t matches[3];
if ((e = regexec(&ex, str, 3, matches, 0)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
if (e != REG_NOMATCH)
err("regexec: Couldn't match '%s' in '%s': %s\n",
pattern, str, errbuf);
return -1;
}
*r = (off_t)strtoll(str + matches[1].rm_so, NULL, 10) *
(matches[2].rm_so == -1 ? 1 :
tolower(*(str + matches[2].rm_so)) == 't' ? (off_t)1 << 40 :
tolower(*(str + matches[2].rm_so)) == 'g' ? (off_t)1 << 30 :
tolower(*(str + matches[2].rm_so)) == 'm' ? (off_t)1 << 20 :
tolower(*(str + matches[2].rm_so)) == 'k' ? (off_t)1 << 10 : 1);
return 0;
}
static int parse_size(const char *str, size_t *r)
{
regex_t ex;
int e;
static const char *pattern =
"^[[:space:]]*([0-9]+)(b|kb?|mb?|gb?)?[[:space:]]*$";
if ((e = regcomp(&ex,pattern, REG_EXTENDED | REG_ICASE)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regcomp(%s): %s\n", pattern, errbuf);
return -1;
}
regmatch_t matches[3];
if ((e = regexec(&ex, str, 3, matches, 0)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
if (e != REG_NOMATCH)
err("regexec: Couldn't match '%s' in '%s': %s\n",
pattern, str, errbuf);
return -1;
}
*r = (size_t)strtoll(str + matches[1].rm_so, NULL, 10) *
(matches[2].rm_so == -1 ? 1 :
tolower(*(str + matches[2].rm_so)) == 'g' ? 1024 * 1024 * 1024 :
tolower(*(str + matches[2].rm_so)) == 'm' ? 1024 * 1024 :
tolower(*(str + matches[2].rm_so)) == 'k' ? 1024 : 1);
return 0;
}
static int parse_int_range(const char *str, int *start, int *end)
{
regex_t ex;
int e;
static const char *pattern =
"^[[:space:]]*([0-9]+)[[:space:]]*(-([0-9]+))?[[:space:]]*$";
if ((e = regcomp(&ex,pattern, REG_EXTEN