1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*
* Copyright (C) 2017-2019 Canonical, Ltd.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#define MAX_ARGS (64)
#define RUN_SEQUENTIAL (0x01)
#define RUN_PARALLEL (0x02)
#define ISBLANK(ch) isblank((int)(ch))
/*
* chop()
* chop off end of line that matches char ch
*/
static void chop(char *str, const char ch)
{
char *ptr = strchr(str, ch);
if (ptr)
*ptr = '\0';
}
/*
* parse_run()
* parse the special job file "run" command
* that informs stress-ng to run the job file
* stressors sequentially or in parallel
*/
static int parse_run(
const char *jobfile,
int argc,
char **argv,
uint32_t *flag)
{
if (argc < 3)
return 0;
if (strcmp(argv[1], "run"))
return 0;
if (!strcmp(argv[2], "sequential") ||
!strcmp(argv[2], "sequentially") ||
!strcmp(argv[2], "seq")) {
if (*flag & RUN_PARALLEL)
goto err;
*flag |= RUN_SEQUENTIAL;
g_opt_flags |= OPT_FLAGS_SEQUENTIAL;
return 1;
}
if (!strcmp(argv[2], "parallel") ||
!strcmp(argv[2], "par") ||
!strcmp(argv[2], "together")) {
if (*flag & RUN_SEQUENTIAL)
goto err;
*flag |= RUN_PARALLEL;
g_opt_flags &= ~OPT_FLAGS_SEQUENTIAL;
g_opt_flags |= OPT_FLAGS_ALL;
return 1;
}
err:
(void)fprintf(stderr, "Cannot have both run sequential "
"and run parallel in jobfile %s\n",
jobfile);
return -1;
}
/*
* generic job error message
*/
static void parse_error(
const uint32_t lineno,
const char *line)
{
fprintf(stderr, "error in line %" PRIu32 ": '%s'\n",
lineno, line);
}
/*
* parse_jobfile()
* parse a jobfile, turn job commands into
* individual stress-ng options
*/
int parse_jobfile(
const int argc,
char **argv,
const char *jobfile)
{
NOCLOBBER FILE *fp;
char buf[4096];
char *new_argv[MAX_ARGS];
char txt[sizeof(buf)];
int ret;
uint32_t flag;
static uint32_t lineno;
if (!jobfile) {
if (argc < 2)
return 0;
fp = fopen(argv[1], "r");
if (!fp)
return 0;
} else {
fp = fopen(jobfile, "r");
}
if (!fp) {
(void)fprintf(stderr, "Cannot open jobfile '%s'\n", jobfile);
return -1;
}
if (setjmp(g_error_env) == 1) {
parse_error(lineno, txt);
ret = -1;
goto err;
}
flag = 0;
ret = -1;
while (fgets(buf, sizeof(buf), fp)) {
char *ptr = buf;
int new_argc = 1;
(void)memset(new_argv, 0, sizeof(new_argv));
new_argv[0] = argv[0];
lineno++;
/* remove \n */
chop(buf, '\n');
(void)shim_strlcpy(txt, buf, sizeof(txt) - 1);
/* remove comments */
chop(buf, '#');
if (!*ptr)
continue;
/* skip leading blanks */
while (ISBLANK(*ptr))
ptr++;
while (new_argc < MAX_ARGS && *ptr) {
new_argv[new_argc++] = ptr;
/* eat up chars until eos or blank */
while (*ptr && !ISBLANK(*ptr))
ptr++;
if (!*ptr)
break;
*ptr++ = '\0';
/* skip over blanks */
while (ISBLANK(*ptr))
ptr++;
}
/* managed to get any tokens? */
if (new_argc > 1) {
const size_t len = strlen(new_argv[1]) + 3;
char tmp[len];
int rc;
/* Must check for --job -h option! */
if (!strcmp(new_argv[1], "job") ||
!strcmp(new_argv[1], "j")) {
fprintf(stderr, "Cannot read job file in from a job script!\n");
goto err;
}
/* Check for job run option */
rc = parse_run(jobfile, new_argc, new_argv, &flag);
if (rc < 0) {
ret = -1;
parse_error(lineno, txt);
goto err;
} else if (rc == 1) {
continue;
}
/* prepend -- to command to make them into stress-ng options */
(void)snprintf(tmp, len, "--%s", new_argv[1]);
new_argv[1] = tmp;
if (parse_opts(new_argc, new_argv, true) != EXIT_SUCCESS) {
parse_error(lineno, txt);
ret = -1;
goto err;
}
new_argv[1] = NULL;
}
}
ret = 0;
err:
(void)fclose(fp);
return ret;
}
|