File: time-aux.c

package info (click to toggle)
time 1.9-0.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,900 kB
  • sloc: ansic: 10,793; sh: 5,505; makefile: 63
file content (386 lines) | stat: -rw-r--r-- 8,073 bytes parent folder | download | duplicates (3)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <stdarg.h>
#include <unistd.h>
#include <limits.h>

/* 1 second = 1E9 nanoseconds */
#define NS_1E9 1000000000

enum {
  EXIT_CANCELED = 125
};

static void
errx (int eval, const char *fmt, ...)
{
  va_list ap;
  va_start(ap, fmt);
  vfprintf (stderr, fmt, ap);
  va_end(ap);
  fputc ( '\n', stderr);
  exit (eval);
}

static int
get_exit_code (const char* num)
{
  long int l = 0;
  char *pch = NULL ;

  errno = 0;
  l = strtol (optarg, &pch, 10);
  if (errno != 0 || l < 0 || l > 255 || pch == optarg || *pch != '\0' )
    errx (EXIT_CANCELED, "invalid exit code '%s'", optarg);

  return (int)l;
}


static void
do_malloc (const char* optarg)
{
  long int l = 0, i = 0, multiplier = 1 ;
  void *p = NULL ;
  char* pch = NULL;

  errno = 0;
  l = strtol (optarg, &pch, 10);
  if (errno != 0 || pch == optarg || l <= 0 )
    errx (EXIT_CANCELED, "invalid malloc request '%s'", optarg);

  /* Optional multiplier */
  switch (*pch)
    {
    case 'b':
    case 'B':
      multiplier = 1;
      pch++;
      break;

    case 'k':
    case 'K':
      multiplier = 1024;
      pch++;
      break;
    case 'm':
    case 'M':
      multiplier = 1024 * 1024 ;
      pch++;
      break;
    case 'g':
    case 'G':
      multiplier = 1024 * 1024 * 1024 ;
      pch++;
      break;
    case '\0':
      break;
    }
  if (*pch != '\0')
    errx (EXIT_CANCELED, "invalid malloc request '%s' " \
          "(multiplier error)", optarg);

  l = l * multiplier ;
  if ( l < 1 || l > 1024 * 1024 * 1024 )
    errx (EXIT_CANCELED, "invalid malloc request '%s' " \
          "(size too large/small)", optarg);

  p = malloc (l);
  if (p==NULL)
    errx (EXIT_FAILURE, "malloc failed (%ld bytes), errno=%d", l, errno);

  /* access the memory to ensure it is resident, not virtual */
  pch = (char*)p;
  for (i = 0; i < l ; i += 1000, pch += 1000)
    *pch = 4;
}

static void
parse_timespec (const char* s, struct timespec /*out*/ *ts)
{
  long int sleep_s = 0, sleep_ns = 0, l = 0;
  char* pch = NULL;

  errno = 0;
  l = strtol (s, &pch, 10);
  if (errno != 0 || pch == s || l <= 0 )
    errx (EXIT_CANCELED, "invalid time '%s'", s);

  /* Optional multiplier */
  switch (*pch)
    {
    case 'n': /* nanoseconds */
      sleep_ns = l;
      ++pch;
      if (*pch == 's')
	++pch;
      break;

    case 'u': /* microseconds */

      /* prevent signed overflow */
      if (l >(LONG_MAX/1000))
	errx (EXIT_CANCELED, "invalid time '%s' " \
	      "(value too high for microseconds/u units", s);

      sleep_ns = l * 1000 ;
      ++pch;
      if (*pch == 's')
	++pch;
      break;

    case 's': /* seconds */
      sleep_s = l;
      ++pch;
      break;

    case 'm': /* minutes */
    case 'M':
      sleep_s = l * 60 ;
      ++pch;
      break;

    case '\0':
      errx (EXIT_CANCELED, "missing time unit for value '%s' (ns/us/s/m)",
	    optarg, s);
      break;

    default:
      errx (EXIT_CANCELED, "invalid time unit '%c' for time '%s' ",
	    *pch, s);
    }
  if (*pch != '\0')
    errx (EXIT_CANCELED, "invalid time '%s' (unit error)", s);

  /* Normalize nano-seconds/seconds */
  if (sleep_ns >= NS_1E9)
    {
      sleep_s = sleep_ns / NS_1E9;
      sleep_ns = sleep_ns % NS_1E9;
    }

  if (
      ((sleep_s<=0) && (sleep_ns<=0))
      ||
      (sleep_s > 24 * 60 * 60 )
      )
    errx (EXIT_CANCELED, "invalid time '%s' " \
          "(too large/small)", optarg);

  ts->tv_sec = sleep_s;
  ts->tv_nsec = sleep_ns;
}

static void
busy_user_sleep (const struct timespec *sleep)
{
  struct timespec start, cur;
  long int diff_s, diff_ns;
  long int sleep_s = sleep->tv_sec;
  long int sleep_ns = sleep->tv_nsec;

  if (clock_gettime (CLOCK_MONOTONIC, &start)!=0)
    errx (EXIT_FAILURE, "clock_gettime failed, errno=%d", errno);

  while (1) {
    if (clock_gettime (CLOCK_MONOTONIC, &cur)!=0)
      errx (EXIT_FAILURE, "clock_gettime failed, errno=%d", errno);

    diff_s = cur.tv_sec - start.tv_sec;
    diff_ns = cur.tv_nsec - start.tv_nsec;

    if (diff_ns < 0) {
      --diff_s ;
      diff_ns += 1E9;
    }

    if ((diff_s >= sleep_s) && (diff_ns >= sleep_ns))
      break;

    /* waste some cycles */
    diff_s = diff_ns % 100;
  }
}

static void
busy_sys_sleep (const struct timespec *sleep)
{
  struct timespec start, cur;
  long int diff_s, diff_ns;
  long int sleep_s = sleep->tv_sec;
  long int sleep_ns = sleep->tv_nsec;
  pid_t pid,ppid;
  uid_t uid,euid;
  gid_t gid,egid;
  char buf[100];

  if (clock_gettime (CLOCK_MONOTONIC, &start)!=0)
    errx (EXIT_FAILURE, "clock_gettime failed, errno=%d", errno);

  while (1) {
    if (clock_gettime (CLOCK_MONOTONIC, &cur)!=0)
      errx (EXIT_FAILURE, "clock_gettime failed, errno=%d", errno);

    diff_s = cur.tv_sec - start.tv_sec;
    diff_ns = cur.tv_nsec - start.tv_nsec;

    if (diff_ns < 0) {
      --diff_s ;
      diff_ns += 1E9;
    }

    if ((diff_s >= sleep_s) && (diff_ns >= sleep_ns))
      break;

    /* waste some cycles in the kernel using system calls */
    pid = getpid();
    ppid = getppid();
    uid = getuid();
    euid = geteuid();
    gid = getgid();
    egid = getegid();
    getcwd (buf, 10 + (pid+ppid+uid+euid+gid+egid)&0xF);
  }
}

static void
safe_nanosleep (const struct timespec *ts)
{
  int i;
  struct timespec req,rem;
  req = *ts;
  while (1)
    {
      errno = 0 ;
      i = nanosleep (&req, &rem);
      if (i==0)
	break;
      else if (i==-1 && errno == EINTR)
	req = rem;
      else
	errx (EXIT_FAILURE, "nanosleep failed, errno=%d", errno);
    }
}


static void
do_busy_user_sleep (const char* optarg)
{
  struct timespec ts;

  parse_timespec (optarg, &ts);
  busy_user_sleep (&ts);
}

static void
do_busy_sys_sleep (const char* optarg)
{
  struct timespec ts;

  parse_timespec (optarg, &ts);
  busy_sys_sleep (&ts);
}

static void
do_sleep (const char* optarg)
{
  struct timespec ts;

  parse_timespec (optarg, &ts);
  safe_nanosleep (&ts);
}

static void
do_half_busy_sleep (const char* optarg)
{
  const unsigned int iters = 4 ;
  struct timespec ts, ts2;

  parse_timespec (optarg, &ts);

  ts2.tv_sec = ts.tv_sec / iters ;
  ts2.tv_nsec = ts.tv_nsec / iters +
    ( ( ts.tv_sec % iters ) * NS_1E9 ) / iters ;

  for (int i=0; i<iters/2 ; ++i)
    {
      busy_user_sleep (&ts2);
      safe_nanosleep (&ts2);
    }
}


static void
usage (void)
{
  puts("Usage: time-aux [OPTIONS]\n\
Wastes time and memory to test GNU time\n\
\n\
OPTIONS\n\
 -b TIME   waste TIME in a CPU busy loop (consuming 'user' time)\n\
 -e N      terminate with exit code N (default: 0)\n\
 -h        this help screen\n\
 -H TIME   waste/sleep TIME (half in busy loop, half in sleep)\n\
 -k TIME   waste TIME in kernel syscalls (approx.)\n\
 -m SIZE   allocate SIZE bytes memory (accepts K/M/G suffix)\n\
 -s TIME   sleep TIME without wasting CPU time\n\
\n\
TIME must have a suffix:\n\
m (minutes), s (seconds), us (microseconds), ns (nanoseconds)\n\
\n\
Example:\n\
Allocate 200 MBs, sleep for 1 second, then busy-loop for 1 second,\n\
then terminate with exit code 4:\n\
  time-aux -m 200M -s 1s -b 1s -e 4\n\
\n\
");
  exit(0);
}

int main (int argc, char *argv[])
{
  int c;
  int rc = 0 ;

  while ( (c = getopt(argc,argv,"e:m:b:s:H:k:h")) != -1 )
    {
      switch (c)
        {
        case 'e': /* Set exit code */
          rc = get_exit_code (optarg);
          break;

        case 'm': /* Malloc */
          do_malloc (optarg);
          break;

	case 'h':
	  usage();
	  break;

        case 'b': /* busy loop, waste CPU cycles for X seconds*/
          do_busy_user_sleep (optarg);
          break;

        case 's': /* sleep without wasting CPU cycles in user mode */
          do_sleep (optarg);
          break;

        case 'H': /* half busy loop, half sleep */
          do_half_busy_sleep (optarg);
          break;

        case 'k': /* busy loop, wasting kernel/system time */
          do_busy_sys_sleep (optarg);
          break;

        default:
          errx (EXIT_CANCELED,"invalid option");
        }
    }

  return rc;
}