forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.c
188 lines (165 loc) · 4.02 KB
/
job.c
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
/*
* Copyright (C) 2017 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 <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> 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_PARALLEL;
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_SEQUENTIAL;
g_opt_flags &= ~OPT_FLAGS_SEQUENTIAL;
return 1;
}
err:
fprintf(stderr, "Cannot have both run sequential "
"and run parallel in jobfile %s\n",
jobfile);
return -1;
}
/*
* parse_jobfile()
* parse a jobfile, turn job commands into
* individual stress-ng options
*/
int parse_jobfile(
const int argc,
char **argv,
const char *jobfile,
main_opts_t *opts)
{
FILE *fp;
char buf[4096];
char *new_argv[MAX_ARGS];
int ret = -1;
uint32_t flag = 0;
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;
}
while (fgets(buf, sizeof(buf), fp)) {
char *ptr = buf;
int argc = 1;
memset(new_argv, 0, sizeof(new_argv));
new_argv[0] = argv[0];
/* remove \n */
chop(buf, '\n');
/* remove comments */
chop(buf, '#');
if (!*ptr)
continue;
/* skip leading blanks */
while (ISBLANK(*ptr))
ptr++;
while (argc < MAX_ARGS && *ptr) {
new_argv[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 (argc > 1) {
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, argc, new_argv, &flag);
if (rc < 0) {
ret = -1;
goto err;
} else if (rc == 1) {
continue;
}
/* prepend -- to command to make them into stress-ng options */
snprintf(tmp, len, "--%s", new_argv[1]);
new_argv[1] = tmp;
parse_opts(argc, new_argv, opts);
new_argv[1] = NULL;
}
}
ret = 0;
err:
(void)fclose(fp);
return ret;
}