blob: d6ae186515648267219bacea03a738840cfde66a [file] [log] [blame]
robbiew2ee5ca42005-05-26 15:35:58 +00001/*
2 * Copyright (c) International Business Machines Corp., 2005
3 * Copyright (c) Wipro Technologies Ltd, 2005. All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080014 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew2ee5ca42005-05-26 15:35:58 +000016 *
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +010017 * AUTHORS:
18 * Prashant P Yendigeri <prashant.yendigeri@wipro.com>
19 * Robbie Williamson <robbiew@us.ibm.com>
robbiew2ee5ca42005-05-26 15:35:58 +000020 *
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +010021 * DESCRIPTION
robbiew2ee5ca42005-05-26 15:35:58 +000022 * This is a Phase I test for the pselect01(2) system call.
23 * It is intended to provide a limited exposure of the system call.
24 *
25 **********************************************************/
26
27#include <stdio.h>
28#include <fcntl.h>
29#include <sys/select.h>
30#include <sys/time.h>
31#include <sys/types.h>
robbiew9563ccc2005-10-03 18:24:13 +000032#include <time.h>
robbiew2ee5ca42005-05-26 15:35:58 +000033#include <unistd.h>
34#include <errno.h>
subrata_modak923b23f2009-11-02 13:57:16 +000035#include <stdint.h>
robbiew2ee5ca42005-05-26 15:35:58 +000036
37#include "test.h"
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +010038#include "safe_macros.h"
robbiew2ee5ca42005-05-26 15:35:58 +000039
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +010040static void setup(void);
41static void cleanup(void);
robbiew2ee5ca42005-05-26 15:35:58 +000042
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +010043TCID_DEFINE(pselect01);
44int TST_TOTAL = 9;
robbiew2ee5ca42005-05-26 15:35:58 +000045
46#define FILENAME "pselect01_test"
47#define LOOP_COUNT 4
48
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +010049static int fd;
50
51static void pselect_verify(void)
robbiew2ee5ca42005-05-26 15:35:58 +000052{
subrata_modak56207ce2009-03-23 13:35:39 +000053 fd_set readfds;
Garrett Cooper956d7712010-07-21 02:23:47 -070054 struct timespec tv, tv_start, tv_end;
55 long real_nsec, total_nsec;
Garrett Cooper4fffe0e2010-07-22 01:52:03 -070056 double real_sec;
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +010057 int total_sec, retval;
subrata_modak56207ce2009-03-23 13:35:39 +000058 FD_ZERO(&readfds);
59 FD_SET(fd, &readfds);
60 tv.tv_sec = 0;
subrata_modak3ef30fd2009-04-18 19:05:23 +000061 tv.tv_nsec = 0;
robbiew2ee5ca42005-05-26 15:35:58 +000062
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +010063 retval = pselect(fd, &readfds, 0, 0, &tv, NULL);
64 if (retval >= 0)
65 tst_resm(TPASS, "pselect() succeeded retval=%i", retval);
66 else
67 tst_resm(TFAIL | TERRNO, "pselect() failed unexpectedly");
robbiew2ee5ca42005-05-26 15:35:58 +000068
subrata_modak56207ce2009-03-23 13:35:39 +000069 for (total_sec = 1; total_sec <= LOOP_COUNT; total_sec++) {
70 FD_ZERO(&readfds);
71 FD_SET(0, &readfds);
subrata_modak4bb656a2009-02-26 12:02:09 +000072
subrata_modak56207ce2009-03-23 13:35:39 +000073 tv.tv_sec = total_sec;
subrata_modak3ef30fd2009-04-18 19:05:23 +000074 tv.tv_nsec = 0;
robbiew2ee5ca42005-05-26 15:35:58 +000075
subrata_modak56207ce2009-03-23 13:35:39 +000076 tst_resm(TINFO,
subrata_modak923b23f2009-11-02 13:57:16 +000077 "Testing basic pselect sanity,Sleeping for %jd secs",
Wanlong Gao354ebb42012-12-07 10:10:04 +080078 (intmax_t) tv.tv_sec);
Garrett Cooper956d7712010-07-21 02:23:47 -070079 clock_gettime(CLOCK_REALTIME, &tv_start);
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +010080 pselect(0, &readfds, NULL, NULL, &tv, NULL);
Garrett Cooper956d7712010-07-21 02:23:47 -070081 clock_gettime(CLOCK_REALTIME, &tv_end);
robbiew2ee5ca42005-05-26 15:35:58 +000082
Garrett Cooper2c282152010-12-16 00:55:50 -080083 real_sec = (0.5 + (tv_end.tv_sec - tv_start.tv_sec +
Wanlong Gao354ebb42012-12-07 10:10:04 +080084 1e-9 * (tv_end.tv_nsec - tv_start.tv_nsec)));
Garrett Cooper4fffe0e2010-07-22 01:52:03 -070085 if (abs(real_sec - total_sec) < 0.2 * total_sec)
86 tst_resm(TPASS, "Sleep time was correct "
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 "(%lf/%d < 20 %%)", real_sec, total_sec);
subrata_modak56207ce2009-03-23 13:35:39 +000088 else
Garrett Cooper573045d2010-11-22 14:12:54 -080089 tst_resm(TFAIL, "Sleep time was incorrect (%d/%lf "
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 ">= 20%%)", total_sec, real_sec);
subrata_modak56207ce2009-03-23 13:35:39 +000091 }
robbiew2ee5ca42005-05-26 15:35:58 +000092
93#ifdef DEBUG
subrata_modak3ef30fd2009-04-18 19:05:23 +000094 tst_resm(TINFO, "Now checking nsec sleep precision");
robbiew2ee5ca42005-05-26 15:35:58 +000095#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +080096 for (total_nsec = 1e8; total_nsec <= LOOP_COUNT * 1e8;
97 total_nsec += 1e8) {
subrata_modak56207ce2009-03-23 13:35:39 +000098 FD_ZERO(&readfds);
99 FD_SET(0, &readfds);
robbiew2ee5ca42005-05-26 15:35:58 +0000100
subrata_modak3ef30fd2009-04-18 19:05:23 +0000101 tv.tv_sec = 0;
102 tv.tv_nsec = total_nsec;
robbiew2ee5ca42005-05-26 15:35:58 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104 tst_resm(TINFO,
subrata_modak923b23f2009-11-02 13:57:16 +0000105 "Testing basic pselect sanity,Sleeping for %ld nano secs",
subrata_modak3ef30fd2009-04-18 19:05:23 +0000106 tv.tv_nsec);
Garrett Cooper956d7712010-07-21 02:23:47 -0700107 clock_gettime(CLOCK_REALTIME, &tv_start);
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +0100108 pselect(0, &readfds, NULL, NULL, &tv, NULL);
Garrett Cooper956d7712010-07-21 02:23:47 -0700109 clock_gettime(CLOCK_REALTIME, &tv_end);
subrata_modak4bb656a2009-02-26 12:02:09 +0000110
Garrett Cooper956d7712010-07-21 02:23:47 -0700111 real_nsec = (tv_end.tv_sec - tv_start.tv_sec) * 1e9 +
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 tv_end.tv_nsec - tv_start.tv_nsec;
subrata_modak3ef30fd2009-04-18 19:05:23 +0000113
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 /* allow 20% error */
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +0100115 if (abs(real_nsec - tv.tv_nsec) < 0.2 * total_nsec) {
subrata_modak56207ce2009-03-23 13:35:39 +0000116 tst_resm(TPASS, "Sleep time was correct");
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +0100117 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000118 tst_resm(TWARN,
119 "This test could fail if the system was under load");
120 tst_resm(TWARN,
121 "due to the limitation of the way it calculates the");
122 tst_resm(TWARN, "system call execution time.");
subrata_modak3ef30fd2009-04-18 19:05:23 +0000123 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 "Sleep time was incorrect:%ld nsec vs expected %ld nsec",
Garrett Cooper956d7712010-07-21 02:23:47 -0700125 real_nsec, total_nsec);
subrata_modak56207ce2009-03-23 13:35:39 +0000126 }
127 }
robbiew2ee5ca42005-05-26 15:35:58 +0000128}
subrata_modak56207ce2009-03-23 13:35:39 +0000129
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +0100130int main(int argc, char *argv[])
robbiew2ee5ca42005-05-26 15:35:58 +0000131{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200132 const char *msg;
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +0100133 int lc;
Garrett Cooper2c282152010-12-16 00:55:50 -0800134
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +0100135 msg = parse_opts(argc, argv, NULL, NULL);
136 if (msg != NULL)
137 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
138
139 setup();
140
141 for (lc = 0; TEST_LOOPING(lc); lc++) {
142 tst_count = 0;
143 pselect_verify();
144 }
145
146 cleanup();
147 tst_exit();
148}
149
150static void setup(void)
151{
subrata_modak56207ce2009-03-23 13:35:39 +0000152 tst_sig(NOFORK, DEF_HANDLER, cleanup);
subrata_modak56207ce2009-03-23 13:35:39 +0000153 tst_tmpdir();
robbiew2ee5ca42005-05-26 15:35:58 +0000154
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +0100155 fd = SAFE_OPEN(cleanup, FILENAME, O_CREAT | O_RDWR, 0777);
156
subrata_modak56207ce2009-03-23 13:35:39 +0000157 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800158}
robbiew2ee5ca42005-05-26 15:35:58 +0000159
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +0100160static void cleanup(void)
robbiew2ee5ca42005-05-26 15:35:58 +0000161{
Cyril Hrubis6ca7f9e2014-03-27 16:37:29 +0100162 if (fd && close(fd))
163 tst_resm(TWARN | TERRNO, "close() failed");
robbiew2ee5ca42005-05-26 15:35:58 +0000164
subrata_modak56207ce2009-03-23 13:35:39 +0000165 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700166}