xref: /freebsd/tools/test/stress2/misc/nanosleep.sh (revision 19261079)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# A simplistic regression test for r200510:
30
31[ `sysctl -n kern.hz` -lt 1000 ] && exit 0
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > nanosleep.c
38mycc -o nanosleep -Wall -Wextra nanosleep.c || exit 1
39rm -f nanosleep.c
40
41/tmp/nanosleep && s=0 || s=$?
42
43rm -f /tmp/nanosleep
44exit $s
45EOF
46#include <sys/limits.h>
47#include <sys/time.h>
48
49#include <err.h>
50#include <stdio.h>
51#include <strings.h>
52#include <time.h>
53
54#define N 20000
55
56int
57main(void)
58{
59	struct timespec rmt, rqt;
60	struct timespec finish, start, res;
61	long m;
62	int i;
63
64	m = LONG_MAX;
65	for (i = 0; i < 100; i++) {
66		rqt.tv_sec  = 0;
67		rqt.tv_nsec = N;
68		clock_gettime(CLOCK_REALTIME_PRECISE, &start);
69		if (nanosleep(&rqt, &rmt) == -1)
70			err(1, "nanosleep");
71		clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
72		timespecsub(&finish, &start, &res);
73		if (res.tv_nsec < N)
74			errx(1, "Short sleep: %ld", res.tv_nsec);
75//		fprintf(stderr, "asked for %f, got %f\n", (double)N / 1e9,
76//		    (double)res.tv_nsec / 1e9);
77		if (m > res.tv_nsec)
78			m = res.tv_nsec;
79	}
80	if (m > 2 * N) {
81		fprintf(stderr, "nanosleep(%fs). Best value is %fs.\n",
82		    (double)N / 1e9, (double)m / 1e9);
83		errx(1, "FAIL");
84	}
85
86	return (0);
87}
88