robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame^] | 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 16 | * |
| 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> |
robbiew | 9563ccc | 2005-10-03 18:24:13 +0000 | [diff] [blame] | 44 | #include <time.h> |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 45 | #include <unistd.h> |
| 46 | #include <errno.h> |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 47 | #include <stdint.h> |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 48 | |
| 49 | #include "test.h" |
| 50 | #include "usctest.h" |
| 51 | |
vapier | aa35472 | 2006-05-26 06:26:37 +0000 | [diff] [blame] | 52 | void setup(); |
| 53 | void cleanup(); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 54 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 55 | char *TCID = "pselect01"; /* Test program identifier. */ |
| 56 | int TST_TOTAL = 1; /* Total number of test cases. */ |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 57 | |
| 58 | #define FILENAME "pselect01_test" |
| 59 | #define LOOP_COUNT 4 |
| 60 | |
robbiew | 9563ccc | 2005-10-03 18:24:13 +0000 | [diff] [blame] | 61 | int main() |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 62 | { |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 63 | int ret_pselect, fd; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | fd_set readfds; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 65 | int retval; |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 66 | struct timespec tv, tv_start, tv_end; |
| 67 | long real_nsec, total_nsec; |
Garrett Cooper | 4fffe0e | 2010-07-22 01:52:03 -0700 | [diff] [blame] | 68 | double real_sec; |
| 69 | int total_sec; |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 70 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | setup(); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 72 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | 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_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 82 | tv.tv_nsec = 0; |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 83 | |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 84 | ret_pselect = pselect(fd, &readfds, 0, 0, &tv, NULL); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | 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); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 93 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | for (total_sec = 1; total_sec <= LOOP_COUNT; total_sec++) { |
| 95 | FD_ZERO(&readfds); |
| 96 | FD_SET(0, &readfds); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 97 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | tv.tv_sec = total_sec; |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 99 | tv.tv_nsec = 0; |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | tst_resm(TINFO, |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 102 | "Testing basic pselect sanity,Sleeping for %jd secs", |
| 103 | (intmax_t)tv.tv_sec); |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 104 | clock_gettime(CLOCK_REALTIME, &tv_start); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | retval = |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 106 | pselect(0, &readfds, NULL, NULL, &tv, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | NULL); |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 108 | clock_gettime(CLOCK_REALTIME, &tv_end); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 109 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 110 | real_sec = (0.5 + (tv_end.tv_sec - tv_start.tv_sec + |
Garrett Cooper | 4fffe0e | 2010-07-22 01:52:03 -0700 | [diff] [blame] | 111 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | else |
Garrett Cooper | 573045d | 2010-11-22 14:12:54 -0800 | [diff] [blame] | 116 | tst_resm(TFAIL, "Sleep time was incorrect (%d/%lf " |
Garrett Cooper | 4fffe0e | 2010-07-22 01:52:03 -0700 | [diff] [blame] | 117 | ">= 20%%)", total_sec, real_sec); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | } |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 119 | |
| 120 | #ifdef DEBUG |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 121 | tst_resm(TINFO, "Now checking nsec sleep precision"); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 122 | #endif |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 123 | for (total_nsec = 1e8; total_nsec <= LOOP_COUNT * 1e8; total_nsec += 1e8) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | FD_ZERO(&readfds); |
| 125 | FD_SET(0, &readfds); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 126 | |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 127 | tv.tv_sec = 0; |
| 128 | tv.tv_nsec = total_nsec; |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 129 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | tst_resm(TINFO, |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 131 | "Testing basic pselect sanity,Sleeping for %ld nano secs", |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 132 | tv.tv_nsec); |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 133 | clock_gettime(CLOCK_REALTIME, &tv_start); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | retval = |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 135 | pselect(0, &readfds, NULL, NULL, &tv, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | NULL); |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 137 | clock_gettime(CLOCK_REALTIME, &tv_end); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 138 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 139 | /* Changed total_sec compare to an at least vs an exact compare */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 140 | |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 141 | real_nsec = (tv_end.tv_sec - tv_start.tv_sec) * 1e9 + |
| 142 | tv_end.tv_nsec - tv_start.tv_nsec; |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 143 | |
Garrett Cooper | ba7ee42 | 2010-07-21 02:49:39 -0700 | [diff] [blame] | 144 | /* allow 20% error*/ |
| 145 | if (abs(real_nsec - tv.tv_nsec) < 0.2 * total_nsec) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | 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_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 153 | tst_resm(TFAIL, |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 154 | "Sleep time was incorrect:%ld nsec vs expected %ld nsec", |
| 155 | real_nsec, total_nsec); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 159 | tst_exit(); |
| 160 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 161 | |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 162 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 164 | /*************************************************************** |
| 165 | * setup() - performs all ONE TIME setup for this test. |
| 166 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | void setup() |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 168 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 169 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 171 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 172 | /* create temporary directory */ |
| 173 | tst_tmpdir(); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 174 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 176 | } |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 177 | |
| 178 | /*************************************************************** |
| 179 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 180 | * completion or premature exit. |
| 181 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | void cleanup() |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 183 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 184 | /* |
| 185 | * print timing stats if that option was specified. |
| 186 | * print errno log if that option was specified. |
| 187 | */ |
| 188 | TEST_CLEANUP; |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 189 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | tst_rmdir(); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 191 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 192 | } |