blob: 3672564fbf71146af84c133280169407606f7652 [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 *
17 */
18/**********************************************************
19 *
20 * TEST IDENTIFIER : pselect01
21 *
22 * EXECUTED BY : root / superuser
23 *
24 * TEST TITLE : Basic tests for pselect01(2)
25 *
26 * TEST CASE TOTAL : 1
27 *
28 * AUTHOR : Prashant P Yendigeri
29 * <prashant.yendigeri@wipro.com>
30 * Robbie Williamson
31 * <robbiew@us.ibm.com>
32 *
33 * DESCRIPTION
34 * This is a Phase I test for the pselect01(2) system call.
35 * It is intended to provide a limited exposure of the system call.
36 *
37 **********************************************************/
38
39#include <stdio.h>
40#include <fcntl.h>
41#include <sys/select.h>
42#include <sys/time.h>
43#include <sys/types.h>
robbiew9563ccc2005-10-03 18:24:13 +000044#include <time.h>
robbiew2ee5ca42005-05-26 15:35:58 +000045#include <unistd.h>
46#include <errno.h>
subrata_modak923b23f2009-11-02 13:57:16 +000047#include <stdint.h>
robbiew2ee5ca42005-05-26 15:35:58 +000048
49#include "test.h"
50#include "usctest.h"
51
vapieraa354722006-05-26 06:26:37 +000052void setup();
53void cleanup();
robbiew2ee5ca42005-05-26 15:35:58 +000054
subrata_modak56207ce2009-03-23 13:35:39 +000055char *TCID = "pselect01"; /* Test program identifier. */
56int TST_TOTAL = 1; /* Total number of test cases. */
robbiew2ee5ca42005-05-26 15:35:58 +000057
58#define FILENAME "pselect01_test"
59#define LOOP_COUNT 4
60
robbiew9563ccc2005-10-03 18:24:13 +000061int main()
robbiew2ee5ca42005-05-26 15:35:58 +000062{
Garrett Cooper956d7712010-07-21 02:23:47 -070063 int ret_pselect, fd;
subrata_modak56207ce2009-03-23 13:35:39 +000064 fd_set readfds;
subrata_modak56207ce2009-03-23 13:35:39 +000065 int retval;
Garrett Cooper956d7712010-07-21 02:23:47 -070066 struct timespec tv, tv_start, tv_end;
67 long real_nsec, total_nsec;
Garrett Cooper4fffe0e2010-07-22 01:52:03 -070068 double real_sec;
69 int total_sec;
robbiew2ee5ca42005-05-26 15:35:58 +000070
subrata_modak56207ce2009-03-23 13:35:39 +000071 setup();
subrata_modak4bb656a2009-02-26 12:02:09 +000072
subrata_modak56207ce2009-03-23 13:35:39 +000073 fd = open(FILENAME, O_CREAT | O_RDWR, 0777);
74 if (fd < 0) {
75 tst_resm(TBROK, "Opening %s...Failed....err %d", FILENAME,
76 errno);
77 cleanup();
78 }
79 FD_ZERO(&readfds);
80 FD_SET(fd, &readfds);
81 tv.tv_sec = 0;
subrata_modak3ef30fd2009-04-18 19:05:23 +000082 tv.tv_nsec = 0;
robbiew2ee5ca42005-05-26 15:35:58 +000083
subrata_modak3ef30fd2009-04-18 19:05:23 +000084 ret_pselect = pselect(fd, &readfds, 0, 0, &tv, NULL);
subrata_modak56207ce2009-03-23 13:35:39 +000085 if (ret_pselect >= 0) {
86 tst_resm(TPASS, "Basic pselect syscall testing....OK");
87 } else
88 tst_resm(TFAIL,
89 "Basic pselect syscall testing....FAILED, err %d",
90 errno);
91 close(fd);
92 remove(FILENAME);
robbiew2ee5ca42005-05-26 15:35:58 +000093
subrata_modak56207ce2009-03-23 13:35:39 +000094 for (total_sec = 1; total_sec <= LOOP_COUNT; total_sec++) {
95 FD_ZERO(&readfds);
96 FD_SET(0, &readfds);
subrata_modak4bb656a2009-02-26 12:02:09 +000097
subrata_modak56207ce2009-03-23 13:35:39 +000098 tv.tv_sec = total_sec;
subrata_modak3ef30fd2009-04-18 19:05:23 +000099 tv.tv_nsec = 0;
robbiew2ee5ca42005-05-26 15:35:58 +0000100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 tst_resm(TINFO,
subrata_modak923b23f2009-11-02 13:57:16 +0000102 "Testing basic pselect sanity,Sleeping for %jd secs",
103 (intmax_t)tv.tv_sec);
Garrett Cooper956d7712010-07-21 02:23:47 -0700104 clock_gettime(CLOCK_REALTIME, &tv_start);
subrata_modak56207ce2009-03-23 13:35:39 +0000105 retval =
Garrett Cooper956d7712010-07-21 02:23:47 -0700106 pselect(0, &readfds, NULL, NULL, &tv,
subrata_modak56207ce2009-03-23 13:35:39 +0000107 NULL);
Garrett Cooper956d7712010-07-21 02:23:47 -0700108 clock_gettime(CLOCK_REALTIME, &tv_end);
robbiew2ee5ca42005-05-26 15:35:58 +0000109
Garrett Cooper2c282152010-12-16 00:55:50 -0800110 real_sec = (0.5 + (tv_end.tv_sec - tv_start.tv_sec +
Garrett Cooper4fffe0e2010-07-22 01:52:03 -0700111 1e-9 * (tv_end.tv_nsec - tv_start.tv_nsec)));
112 if (abs(real_sec - total_sec) < 0.2 * total_sec)
113 tst_resm(TPASS, "Sleep time was correct "
114 "(%lf/%d < 20 %%)", real_sec, total_sec);
subrata_modak56207ce2009-03-23 13:35:39 +0000115 else
Garrett Cooper573045d2010-11-22 14:12:54 -0800116 tst_resm(TFAIL, "Sleep time was incorrect (%d/%lf "
Garrett Cooper4fffe0e2010-07-22 01:52:03 -0700117 ">= 20%%)", total_sec, real_sec);
subrata_modak56207ce2009-03-23 13:35:39 +0000118 }
robbiew2ee5ca42005-05-26 15:35:58 +0000119
120#ifdef DEBUG
subrata_modak3ef30fd2009-04-18 19:05:23 +0000121 tst_resm(TINFO, "Now checking nsec sleep precision");
robbiew2ee5ca42005-05-26 15:35:58 +0000122#endif
subrata_modak3ef30fd2009-04-18 19:05:23 +0000123 for (total_nsec = 1e8; total_nsec <= LOOP_COUNT * 1e8; total_nsec += 1e8) {
subrata_modak56207ce2009-03-23 13:35:39 +0000124 FD_ZERO(&readfds);
125 FD_SET(0, &readfds);
robbiew2ee5ca42005-05-26 15:35:58 +0000126
subrata_modak3ef30fd2009-04-18 19:05:23 +0000127 tv.tv_sec = 0;
128 tv.tv_nsec = total_nsec;
robbiew2ee5ca42005-05-26 15:35:58 +0000129
subrata_modak56207ce2009-03-23 13:35:39 +0000130 tst_resm(TINFO,
subrata_modak923b23f2009-11-02 13:57:16 +0000131 "Testing basic pselect sanity,Sleeping for %ld nano secs",
subrata_modak3ef30fd2009-04-18 19:05:23 +0000132 tv.tv_nsec);
Garrett Cooper956d7712010-07-21 02:23:47 -0700133 clock_gettime(CLOCK_REALTIME, &tv_start);
subrata_modak56207ce2009-03-23 13:35:39 +0000134 retval =
subrata_modak3ef30fd2009-04-18 19:05:23 +0000135 pselect(0, &readfds, NULL, NULL, &tv,
subrata_modak56207ce2009-03-23 13:35:39 +0000136 NULL);
Garrett Cooper956d7712010-07-21 02:23:47 -0700137 clock_gettime(CLOCK_REALTIME, &tv_end);
subrata_modak4bb656a2009-02-26 12:02:09 +0000138
subrata_modak56207ce2009-03-23 13:35:39 +0000139 /* Changed total_sec compare to an at least vs an exact compare */
subrata_modak4bb656a2009-02-26 12:02:09 +0000140
Garrett Cooper956d7712010-07-21 02:23:47 -0700141 real_nsec = (tv_end.tv_sec - tv_start.tv_sec) * 1e9 +
142 tv_end.tv_nsec - tv_start.tv_nsec;
subrata_modak3ef30fd2009-04-18 19:05:23 +0000143
Garrett Cooperba7ee422010-07-21 02:49:39 -0700144 /* allow 20% error*/
145 if (abs(real_nsec - tv.tv_nsec) < 0.2 * total_nsec)
subrata_modak56207ce2009-03-23 13:35:39 +0000146 tst_resm(TPASS, "Sleep time was correct");
147 else {
148 tst_resm(TWARN,
149 "This test could fail if the system was under load");
150 tst_resm(TWARN,
151 "due to the limitation of the way it calculates the");
152 tst_resm(TWARN, "system call execution time.");
subrata_modak3ef30fd2009-04-18 19:05:23 +0000153 tst_resm(TFAIL,
Garrett Cooper956d7712010-07-21 02:23:47 -0700154 "Sleep time was incorrect:%ld nsec vs expected %ld nsec",
155 real_nsec, total_nsec);
subrata_modak56207ce2009-03-23 13:35:39 +0000156 }
157 }
158 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800159 tst_exit();
160 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800161
robbiew2ee5ca42005-05-26 15:35:58 +0000162}
subrata_modak56207ce2009-03-23 13:35:39 +0000163
robbiew2ee5ca42005-05-26 15:35:58 +0000164/***************************************************************
165 * setup() - performs all ONE TIME setup for this test.
166 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000167void setup()
robbiew2ee5ca42005-05-26 15:35:58 +0000168{
Garrett Cooper2c282152010-12-16 00:55:50 -0800169
subrata_modak56207ce2009-03-23 13:35:39 +0000170 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew2ee5ca42005-05-26 15:35:58 +0000171
subrata_modak56207ce2009-03-23 13:35:39 +0000172 /* create temporary directory */
173 tst_tmpdir();
robbiew2ee5ca42005-05-26 15:35:58 +0000174
subrata_modak56207ce2009-03-23 13:35:39 +0000175 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800176}
robbiew2ee5ca42005-05-26 15:35:58 +0000177
178/***************************************************************
179 * cleanup() - performs all ONE TIME cleanup for this test at
180 * completion or premature exit.
181 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000182void cleanup()
robbiew2ee5ca42005-05-26 15:35:58 +0000183{
subrata_modak56207ce2009-03-23 13:35:39 +0000184 /*
185 * print timing stats if that option was specified.
186 * print errno log if that option was specified.
187 */
188 TEST_CLEANUP;
robbiew2ee5ca42005-05-26 15:35:58 +0000189
subrata_modak56207ce2009-03-23 13:35:39 +0000190 tst_rmdir();
robbiew2ee5ca42005-05-26 15:35:58 +0000191
Chris Dearmanec6edca2012-10-17 19:54:01 -0700192}