plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /******************************************************************************/ |
| 2 | /* */ |
| 3 | /* Copyright (c) International Business Machines Corp., 2001 */ |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 4 | /* Copyright (c) 2001 Manoj Iyer <manjo@austin.ibm.com> */ |
| 5 | /* Copyright (c) 2003 Robbie Williamson <robbiew@us.ibm.com> */ |
| 6 | /* Copyright (c) 2004 Paul Larson <plars@linuxtestproject.org> */ |
| 7 | /* Copyright (c) 2007 <rsalveti@linux.vnet.ibm.com> */ |
| 8 | /* Copyright (c) 2007 Suzuki K P <suzuki@in.ibm.com> */ |
| 9 | /* Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz> */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 10 | /* */ |
| 11 | /* This program is free software; you can redistribute it and/or modify */ |
| 12 | /* it under the terms of the GNU General Public License as published by */ |
| 13 | /* the Free Software Foundation; either version 2 of the License, or */ |
| 14 | /* (at your option) any later version. */ |
| 15 | /* */ |
| 16 | /* This program is distributed in the hope that it will be useful, */ |
| 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */ |
| 19 | /* the GNU General Public License for more details. */ |
| 20 | /* */ |
| 21 | /* You should have received a copy of the GNU General Public License */ |
| 22 | /* along with this program; if not, write to the Free Software */ |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 23 | /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | /* */ |
| 25 | /******************************************************************************/ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | /******************************************************************************/ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | /* Description: Test the LINUX memory manager. The program is aimed at */ |
| 28 | /* stressing the memory manager by simultanious map/unmap/read */ |
| 29 | /* by light weight processes, the test is scheduled to run for */ |
| 30 | /* a mininum of 24 hours. */ |
| 31 | /* */ |
| 32 | /* Create two light weight processes X and Y. */ |
| 33 | /* X - maps, writes and unmap a file in a loop. */ |
| 34 | /* Y - read from this mapped region in a loop. */ |
| 35 | /* read must be a success between map and unmap of the region. */ |
| 36 | /* */ |
| 37 | /******************************************************************************/ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <sched.h> |
| 43 | #include <unistd.h> |
| 44 | #include <errno.h> |
| 45 | #include <sys/mman.h> |
| 46 | #include <sched.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <signal.h> |
| 49 | #include <sys/time.h> |
| 50 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | #include <setjmp.h> |
| 52 | #include <pthread.h> |
| 53 | #include <signal.h> |
iyermanoj | c839ac4 | 2001-11-09 23:24:13 +0000 | [diff] [blame] | 54 | #include <string.h> |
robbiew | 173b045 | 2003-04-16 19:46:42 +0000 | [diff] [blame] | 55 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 57 | #define OPT_MISSING(prog, opt) do { \ |
| 58 | fprintf(stderr, "%s: option -%c ", prog, opt); \ |
| 59 | fprintf(stderr, "requires an argument\n"); \ |
| 60 | usage(prog); \ |
| 61 | } while (0) |
iyermanoj | e37f2d1 | 2001-10-25 17:47:59 +0000 | [diff] [blame] | 62 | |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 63 | static int verbose_print = 0; |
Dave Kleikamp | 3324212 | 2013-08-28 08:07:23 -0500 | [diff] [blame] | 64 | static char *volatile map_address; |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 65 | static jmp_buf jmpbuf; |
| 66 | static volatile char read_lock = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
vapier | 475916e | 2007-07-21 20:58:17 +0000 | [diff] [blame] | 68 | char *TCID = "mmap1"; |
| 69 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 71 | static void sig_handler(int signal, siginfo_t * info, void *ut) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | { |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 73 | switch (signal) { |
| 74 | case SIGALRM: |
| 75 | tst_resm(TPASS, "Test ended, success"); |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 76 | _exit(TPASS); |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 77 | case SIGSEGV: |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 78 | longjmp(jmpbuf, 1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | break; |
| 80 | default: |
| 81 | fprintf(stderr, "Unexpected signal - %d --- exiting\n", signal); |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 82 | _exit(TBROK); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Signal handler that is active, when file is mapped, eg. we do not expect |
| 88 | * SIGSEGV to be delivered. |
| 89 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | static void sig_handler_mapped(int signal, siginfo_t * info, void *ut) |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 91 | { |
| 92 | switch (signal) { |
| 93 | case SIGALRM: |
| 94 | tst_resm(TPASS, "Test ended, success"); |
| 95 | _exit(TPASS); |
| 96 | case SIGSEGV: |
| 97 | tst_resm(TINFO, "[%lu] Unexpected page fault at %p", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | pthread_self(), info->si_addr); |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 99 | _exit(TFAIL); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 100 | break; |
| 101 | default: |
| 102 | fprintf(stderr, "Unexpected signal - %d --- exiting\n", signal); |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 103 | _exit(TBROK); |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 104 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 107 | int mkfile(int size) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | { |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 109 | char template[] = "/tmp/ashfileXXXXXX"; |
| 110 | int fd, i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 112 | if ((fd = mkstemp(template)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | tst_brkm(TBROK | TERRNO, NULL, "mkstemp() failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 115 | unlink(template); |
| 116 | |
| 117 | for (i = 0; i < size; i++) |
| 118 | if (write(fd, "a", 1) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | tst_brkm(TBROK | TERRNO, NULL, "write() failed"); |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 120 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 121 | if (write(fd, "\0", 1) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | tst_brkm(TBROK | TERRNO, NULL, "write() failed"); |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | if (fsync(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | tst_brkm(TBROK | TERRNO, NULL, "fsync() failed"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 126 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 127 | return fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Cyril Hrubis | e01b27f | 2011-11-03 19:08:56 +0100 | [diff] [blame] | 130 | void *map_write_unmap(void *ptr) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | { |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 132 | struct sigaction sa; |
Cyril Hrubis | e01b27f | 2011-11-03 19:08:56 +0100 | [diff] [blame] | 133 | long *args = ptr; |
| 134 | long i; |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 135 | int j; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 137 | tst_resm(TINFO, "[%lu] - map, change contents, unmap files %ld times", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 138 | pthread_self(), args[2]); |
iyermanoj | e37f2d1 | 2001-10-25 17:47:59 +0000 | [diff] [blame] | 139 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 140 | if (verbose_print) |
| 141 | tst_resm(TINFO, "map_write_unmap() arguments are: " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 142 | "fd - arg[0]: %ld; " |
| 143 | "size of file - arg[1]: %ld; " |
| 144 | "num of map/write/unmap - arg[2]: %ld", |
| 145 | args[0], args[1], args[2]); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | |
Cyril Hrubis | e01b27f | 2011-11-03 19:08:56 +0100 | [diff] [blame] | 147 | for (i = 0; i < args[2]; i++) { |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 148 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 149 | map_address = mmap(0, (size_t) args[1], PROT_WRITE | PROT_READ, |
| 150 | MAP_SHARED, (int)args[0], 0); |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 151 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | if (map_address == (void *)-1) { |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 153 | perror("map_write_unmap(): mmap()"); |
| 154 | pthread_exit((void *)1); |
| 155 | } |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 156 | |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 157 | while (read_lock) |
| 158 | sched_yield(); |
| 159 | |
| 160 | sigfillset(&sa.sa_mask); |
| 161 | sigdelset(&sa.sa_mask, SIGSEGV); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 162 | sa.sa_flags = SA_SIGINFO | SA_NODEFER; |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 163 | sa.sa_sigaction = sig_handler_mapped; |
| 164 | |
| 165 | if (sigaction(SIGSEGV, &sa, NULL)) { |
| 166 | perror("map_write_unmap(): sigaction()"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 167 | pthread_exit((void *)1); |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 168 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 170 | if (verbose_print) |
| 171 | tst_resm(TINFO, "map address = %p", map_address); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 173 | for (j = 0; j < args[1]; j++) { |
| 174 | map_address[j] = 'a'; |
| 175 | if (random() % 2) |
| 176 | sched_yield(); |
| 177 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 178 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 179 | if (verbose_print) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | tst_resm(TINFO, |
| 181 | "[%ld] times done: of total [%ld] iterations, " |
| 182 | "map_write_unmap():memset() content of memory = %s", |
| 183 | i, args[2], (char *)map_address); |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 184 | |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 185 | sigfillset(&sa.sa_mask); |
| 186 | sigdelset(&sa.sa_mask, SIGSEGV); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 187 | sa.sa_flags = SA_SIGINFO | SA_NODEFER; |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 188 | sa.sa_sigaction = sig_handler; |
| 189 | |
| 190 | if (sigaction(SIGSEGV, &sa, NULL)) { |
| 191 | perror("map_write_unmap(): sigaction()"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 192 | pthread_exit((void *)1); |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 193 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 195 | if (munmap(map_address, (size_t) args[1]) == -1) { |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 196 | perror("map_write_unmap(): mmap()"); |
| 197 | pthread_exit((void *)1); |
| 198 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Cyril Hrubis | 4e2bab8 | 2014-09-24 16:34:35 +0200 | [diff] [blame] | 201 | pthread_exit(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Cyril Hrubis | e01b27f | 2011-11-03 19:08:56 +0100 | [diff] [blame] | 204 | void *read_mem(void *ptr) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | { |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 206 | long i; |
Cyril Hrubis | e01b27f | 2011-11-03 19:08:56 +0100 | [diff] [blame] | 207 | long *args = ptr; |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 208 | int j; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 209 | |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 210 | tst_resm(TINFO, "[%lu] - read contents of memory %p %ld times", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 211 | pthread_self(), map_address, args[2]); |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 212 | |
| 213 | if (verbose_print) |
| 214 | tst_resm(TINFO, "read_mem() arguments are: " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 215 | "number of reads to be performed - arg[2]: %ld; " |
| 216 | "read from address %p", args[2], map_address); |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 217 | |
Cyril Hrubis | e01b27f | 2011-11-03 19:08:56 +0100 | [diff] [blame] | 218 | for (i = 0; i < args[2]; i++) { |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 219 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 220 | if (verbose_print) |
| 221 | tst_resm(TINFO, "read_mem() in while loop %ld times " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 222 | "to go %ld times", i, args[2]); |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 223 | |
| 224 | if (setjmp(jmpbuf) == 1) { |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 225 | read_lock = 0; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 226 | if (verbose_print) |
| 227 | tst_resm(TINFO, "page fault occurred due to " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 228 | "a read after an unmap"); |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 229 | } else { |
Dave Kleikamp | 3324212 | 2013-08-28 08:07:23 -0500 | [diff] [blame] | 230 | if (verbose_print) { |
| 231 | read_lock = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 232 | tst_resm(TINFO, |
| 233 | "read_mem(): content of memory: %s", |
| 234 | (char *)map_address); |
Dave Kleikamp | 3324212 | 2013-08-28 08:07:23 -0500 | [diff] [blame] | 235 | read_lock = 0; |
| 236 | } |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 237 | for (j = 0; j < args[1]; j++) { |
| 238 | read_lock = 1; |
| 239 | if (map_address[j] != 'a') |
| 240 | pthread_exit((void *)-1); |
| 241 | read_lock = 0; |
| 242 | if (random() % 2) |
| 243 | sched_yield(); |
| 244 | } |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Cyril Hrubis | 4e2bab8 | 2014-09-24 16:34:35 +0200 | [diff] [blame] | 248 | pthread_exit(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 251 | static void usage(char *progname) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 252 | { |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 253 | fprintf(stderr, "Usage: %s -d -l -s -v -x\n" |
| 254 | "\t -h help, usage message.\n" |
| 255 | "\t -l number of mmap/write/unmap default: 1000\n" |
| 256 | "\t -s size of the file to be mmapped default: 1024 bytes\n" |
| 257 | "\t -v print more info. default: quiet\n" |
| 258 | "\t -x test execution time default: 24 Hrs\n", |
| 259 | progname); |
iyermanoj | e37f2d1 | 2001-10-25 17:47:59 +0000 | [diff] [blame] | 260 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 261 | exit(-1); |
| 262 | } |
iyermanoj | e37f2d1 | 2001-10-25 17:47:59 +0000 | [diff] [blame] | 263 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 264 | struct signal_info { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 265 | int signum; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 266 | char *signame; |
| 267 | }; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 268 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 269 | static struct signal_info sig_info[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 270 | {SIGHUP, "SIGHUP"}, |
| 271 | {SIGINT, "SIGINT"}, |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 272 | {SIGQUIT, "SIGQUIT"}, |
| 273 | {SIGABRT, "SIGABRT"}, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 274 | {SIGBUS, "SIGBUS"}, |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 275 | {SIGSEGV, "SIGSEGV"}, |
| 276 | {SIGALRM, "SIGALRM"}, |
| 277 | {SIGUSR1, "SIGUSR1"}, |
| 278 | {SIGUSR2, "SIGUSR2"}, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 279 | {-1, "ENDSIG"} |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 280 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 281 | |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 282 | int main(int argc, char **argv) |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 283 | { |
Cyril Hrubis | ea8e6bf | 2011-11-03 19:36:10 +0100 | [diff] [blame] | 284 | int c, i; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 285 | int file_size; |
| 286 | int num_iter; |
| 287 | double exec_time; |
| 288 | int fd; |
Dave Kleikamp | 981d33a | 2013-05-06 11:47:30 -0500 | [diff] [blame] | 289 | void *status; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 290 | pthread_t thid[2]; |
| 291 | long chld_args[3]; |
| 292 | extern char *optarg; |
| 293 | struct sigaction sigptr; |
Cyril Hrubis | ea8e6bf | 2011-11-03 19:36:10 +0100 | [diff] [blame] | 294 | int ret; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 295 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 296 | /* set up the default values */ |
| 297 | file_size = 1024; |
| 298 | num_iter = 1000; |
| 299 | exec_time = 24; |
| 300 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 301 | while ((c = getopt(argc, argv, "hvl:s:x:")) != -1) { |
| 302 | switch (c) { |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 303 | case 'h': |
| 304 | usage(argv[0]); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 305 | break; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 306 | case 'l': |
| 307 | if ((num_iter = atoi(optarg)) == 0) |
| 308 | OPT_MISSING(argv[0], optopt); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 309 | else if (num_iter < 0) |
| 310 | printf |
| 311 | ("WARNING: bad argument. Using default %d\n", |
| 312 | (num_iter = 1000)); |
| 313 | break; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 314 | case 's': |
| 315 | if ((file_size = atoi(optarg)) == 0) |
| 316 | OPT_MISSING(argv[0], optopt); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 317 | else if (file_size < 0) |
| 318 | printf |
| 319 | ("WARNING: bad argument. Using default %d\n", |
| 320 | (file_size = 1024)); |
| 321 | break; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 322 | case 'v': |
| 323 | verbose_print = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 324 | break; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 325 | case 'x': |
| 326 | exec_time = atof(optarg); |
| 327 | if (exec_time == 0) |
| 328 | OPT_MISSING(argv[0], optopt); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 329 | else if (exec_time < 0) |
| 330 | printf |
| 331 | ("WARNING: bad argument. Using default %.0f\n", |
| 332 | (exec_time = 24)); |
| 333 | break; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 334 | default: |
| 335 | usage(argv[0]); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 336 | break; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 337 | } |
| 338 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 339 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 340 | if (verbose_print) |
| 341 | tst_resm(TINFO, "Input parameters are: File size: %d; " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 342 | "Scheduled to run: %lf hours; " |
| 343 | "Number of mmap/write/read: %d", |
| 344 | file_size, exec_time, num_iter); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 345 | |
Cyril Hrubis | fea169d | 2011-11-09 16:28:30 +0100 | [diff] [blame] | 346 | alarm(exec_time * 3600); |
subrata_modak | 4fb9139 | 2007-07-25 09:24:05 +0000 | [diff] [blame] | 347 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 348 | /* Do not mask SIGSEGV, as we are interested in handling it. */ |
Cyril Hrubis | 80ec134 | 2011-11-03 19:39:37 +0100 | [diff] [blame] | 349 | sigptr.sa_sigaction = sig_handler; |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 350 | sigfillset(&sigptr.sa_mask); |
| 351 | sigdelset(&sigptr.sa_mask, SIGSEGV); |
| 352 | sigptr.sa_flags = SA_SIGINFO | SA_NODEFER; |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 353 | |
Cyril Hrubis | ea8e6bf | 2011-11-03 19:36:10 +0100 | [diff] [blame] | 354 | for (i = 0; sig_info[i].signum != -1; i++) { |
| 355 | if (sigaction(sig_info[i].signum, &sigptr, NULL) == -1) { |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 356 | perror("man(): sigaction()"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 357 | fprintf(stderr, |
| 358 | "could not set handler for %s, errno = %d\n", |
| 359 | sig_info[i].signame, errno); |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 360 | exit(-1); |
| 361 | } |
| 362 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 363 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 364 | for (;;) { |
Cyril Hrubis | ea8e6bf | 2011-11-03 19:36:10 +0100 | [diff] [blame] | 365 | if ((fd = mkfile(file_size)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 366 | tst_brkm(TBROK, NULL, |
| 367 | "main(): mkfile(): Failed to create temp file"); |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 368 | |
Cyril Hrubis | ea8e6bf | 2011-11-03 19:36:10 +0100 | [diff] [blame] | 369 | if (verbose_print) |
| 370 | tst_resm(TINFO, "Tmp file created"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 371 | |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 372 | chld_args[0] = fd; |
| 373 | chld_args[1] = file_size; |
| 374 | chld_args[2] = num_iter; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 375 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 376 | if ((ret = |
| 377 | pthread_create(&thid[0], NULL, map_write_unmap, |
| 378 | chld_args))) |
| 379 | tst_brkm(TBROK, NULL, "main(): pthread_create(): %s", |
| 380 | strerror(ret)); |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 381 | |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 382 | tst_resm(TINFO, "created writing thread[%lu]", thid[0]); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 383 | |
Cyril Hrubis | ea8e6bf | 2011-11-03 19:36:10 +0100 | [diff] [blame] | 384 | if ((ret = pthread_create(&thid[1], NULL, read_mem, chld_args))) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 385 | tst_brkm(TBROK, NULL, "main(): pthread_create(): %s", |
| 386 | strerror(ret)); |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 387 | |
Cyril Hrubis | d72816d | 2011-11-03 21:15:53 +0100 | [diff] [blame] | 388 | tst_resm(TINFO, "created reading thread[%lu]", thid[1]); |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 389 | |
Cyril Hrubis | ea8e6bf | 2011-11-03 19:36:10 +0100 | [diff] [blame] | 390 | for (i = 0; i < 2; i++) { |
Dave Kleikamp | 981d33a | 2013-05-06 11:47:30 -0500 | [diff] [blame] | 391 | if ((ret = pthread_join(thid[i], &status))) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 392 | tst_brkm(TBROK, NULL, |
| 393 | "main(): pthread_join(): %s", |
| 394 | strerror(ret)); |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 395 | |
Dave Kleikamp | 981d33a | 2013-05-06 11:47:30 -0500 | [diff] [blame] | 396 | if (status) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 397 | tst_brkm(TFAIL, NULL, |
| 398 | "thread [%lu] - process exited " |
Dave Kleikamp | 981d33a | 2013-05-06 11:47:30 -0500 | [diff] [blame] | 399 | "with %ld", thid[i], (long)status); |
Cyril Hrubis | 6538d51 | 2011-11-03 18:32:34 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | close(fd); |
| 403 | } |
| 404 | |
| 405 | exit(0); |
| 406 | } |