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 | * |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 17 | * AUTHORS: |
| 18 | * Prashant P Yendigeri <prashant.yendigeri@wipro.com> |
| 19 | * Robbie Williamson <robbiew@us.ibm.com> |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 20 | * |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 21 | * DESCRIPTION |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 22 | * 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> |
robbiew | 9563ccc | 2005-10-03 18:24:13 +0000 | [diff] [blame] | 32 | #include <time.h> |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | #include <errno.h> |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 35 | #include <stdint.h> |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 36 | |
| 37 | #include "test.h" |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 38 | #include "safe_macros.h" |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 39 | |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 40 | static void setup(void); |
| 41 | static void cleanup(void); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 42 | |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 43 | TCID_DEFINE(pselect01); |
| 44 | int TST_TOTAL = 9; |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 45 | |
| 46 | #define FILENAME "pselect01_test" |
| 47 | #define LOOP_COUNT 4 |
| 48 | |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 49 | static int fd; |
| 50 | |
| 51 | static void pselect_verify(void) |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 52 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 53 | fd_set readfds; |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 54 | struct timespec tv, tv_start, tv_end; |
| 55 | long real_nsec, total_nsec; |
Garrett Cooper | 4fffe0e | 2010-07-22 01:52:03 -0700 | [diff] [blame] | 56 | double real_sec; |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 57 | int total_sec, retval; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 58 | FD_ZERO(&readfds); |
| 59 | FD_SET(fd, &readfds); |
| 60 | tv.tv_sec = 0; |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 61 | tv.tv_nsec = 0; |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 62 | |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 63 | 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"); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | for (total_sec = 1; total_sec <= LOOP_COUNT; total_sec++) { |
| 70 | FD_ZERO(&readfds); |
| 71 | FD_SET(0, &readfds); |
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 | tv.tv_sec = total_sec; |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 74 | tv.tv_nsec = 0; |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 75 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | tst_resm(TINFO, |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 77 | "Testing basic pselect sanity,Sleeping for %jd secs", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 78 | (intmax_t) tv.tv_sec); |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 79 | clock_gettime(CLOCK_REALTIME, &tv_start); |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 80 | pselect(0, &readfds, NULL, NULL, &tv, NULL); |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 81 | clock_gettime(CLOCK_REALTIME, &tv_end); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 82 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 83 | real_sec = (0.5 + (tv_end.tv_sec - tv_start.tv_sec + |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | 1e-9 * (tv_end.tv_nsec - tv_start.tv_nsec))); |
Garrett Cooper | 4fffe0e | 2010-07-22 01:52:03 -0700 | [diff] [blame] | 85 | if (abs(real_sec - total_sec) < 0.2 * total_sec) |
| 86 | tst_resm(TPASS, "Sleep time was correct " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | "(%lf/%d < 20 %%)", real_sec, total_sec); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | else |
Garrett Cooper | 573045d | 2010-11-22 14:12:54 -0800 | [diff] [blame] | 89 | tst_resm(TFAIL, "Sleep time was incorrect (%d/%lf " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | ">= 20%%)", total_sec, real_sec); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | } |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 92 | |
| 93 | #ifdef DEBUG |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 94 | tst_resm(TINFO, "Now checking nsec sleep precision"); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 95 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 96 | for (total_nsec = 1e8; total_nsec <= LOOP_COUNT * 1e8; |
| 97 | total_nsec += 1e8) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | FD_ZERO(&readfds); |
| 99 | FD_SET(0, &readfds); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 100 | |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 101 | tv.tv_sec = 0; |
| 102 | tv.tv_nsec = total_nsec; |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | tst_resm(TINFO, |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 105 | "Testing basic pselect sanity,Sleeping for %ld nano secs", |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 106 | tv.tv_nsec); |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 107 | clock_gettime(CLOCK_REALTIME, &tv_start); |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 108 | pselect(0, &readfds, NULL, NULL, &tv, NULL); |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 109 | clock_gettime(CLOCK_REALTIME, &tv_end); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 110 | |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 111 | real_nsec = (tv_end.tv_sec - tv_start.tv_sec) * 1e9 + |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 112 | tv_end.tv_nsec - tv_start.tv_nsec; |
subrata_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 113 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | /* allow 20% error */ |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 115 | if (abs(real_nsec - tv.tv_nsec) < 0.2 * total_nsec) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | tst_resm(TPASS, "Sleep time was correct"); |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 117 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | 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_modak | 3ef30fd | 2009-04-18 19:05:23 +0000 | [diff] [blame] | 123 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | "Sleep time was incorrect:%ld nsec vs expected %ld nsec", |
Garrett Cooper | 956d771 | 2010-07-21 02:23:47 -0700 | [diff] [blame] | 125 | real_nsec, total_nsec); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 128 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 130 | int main(int argc, char *argv[]) |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 131 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 132 | const char *msg; |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 133 | int lc; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 134 | |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 135 | 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 | |
| 150 | static void setup(void) |
| 151 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | tst_tmpdir(); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 154 | |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 155 | fd = SAFE_OPEN(cleanup, FILENAME, O_CREAT | O_RDWR, 0777); |
| 156 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 158 | } |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 159 | |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 160 | static void cleanup(void) |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 161 | { |
Cyril Hrubis | 6ca7f9e | 2014-03-27 16:37:29 +0100 | [diff] [blame] | 162 | if (fd && close(fd)) |
| 163 | tst_resm(TWARN | TERRNO, "close() failed"); |
robbiew | 2ee5ca4 | 2005-05-26 15:35:58 +0000 | [diff] [blame] | 164 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 166 | } |