From: Paul L. <pl...@us...> - 2004-09-22 15:08:23
|
Update of /cvsroot/ltp/ltp/testcases/kernel/syscalls/madvise In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22253 Modified Files: madvise02.c Log Message: Overhaul madvise02. Removed some invalid testcases, fixed one case that was an invalid failure, and a lot of cleanup Index: madvise02.c =================================================================== RCS file: /cvsroot/ltp/ltp/testcases/kernel/syscalls/madvise/madvise02.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** madvise02.c 24 Aug 2004 20:32:21 -0000 1.1 --- madvise02.c 22 Sep 2004 15:08:14 -0000 1.2 *************** *** 62,90 **** * DETAILED DESCRIPTION * This is a test for the madvise(2) system call. It is intended ! * to provide a complete exposure of the system call. It tests madvise(2) for ! * all error conditions to occur correctly. * ! * (1) Test Case for EINVAL ! * a. length is negative ! * b. start is not page-aligned ! * c. advice is not a valid value ! * d. application is attempting to release * locked or shared pages (with MADV_DONTNEED) * ! * (2) Test Case for ENOMEM ! * a. addresses in the specified range are not currently mapped * or are outside the address space of the process * b. Not enough memory - paging in failed * ! * (3) Test Case for EIO ! * a. Paging in this area would exceed ! * the process's maximum resident size ! * ! * (4) Test Case for EBADF ! * a. the map exists, * but the area maps something that isn't a file. * ! * (5) Test Case for EAGAIN ! * a. a kernel resource was temporarily unavailable. * * Setup: --- 62,85 ---- * DETAILED DESCRIPTION * This is a test for the madvise(2) system call. It is intended ! * to provide a complete exposure of the system call. It tests ! * madvise(2) for all error conditions to occur correctly. * ! * (A) Test Case for EINVAL ! * 1. start is not page-aligned ! * 2. advice is not a valid value ! * 3. application is attempting to release * locked or shared pages (with MADV_DONTNEED) * ! * (B) Test Case for ENOMEM ! * 4. addresses in the specified range are not currently mapped * or are outside the address space of the process * b. Not enough memory - paging in failed * ! * (C) Test Case for EBADF ! * 5. the map exists, * but the area maps something that isn't a file. * ! * (D) Test Case for EAGAIN ! * 6. a kernel resource was temporarily unavailable. * * Setup: *************** *** 141,146 **** char *ptr_memory_allocated = NULL; char *tmp_memory_allocated = NULL; - struct rlimit rlim; /* For getting rlimit for DATA */ - int no_of_file_pages=0; /* Total no of pages in file */ char *msg=NULL; --- 136,139 ---- *************** *** 150,157 **** ! if ((msg = parse_opts(argc, argv, (option_t *) NULL, NULL)) != (char *) NULL) ! { ! tst_brkm(TBROK, NULL, ! "OPTION PARSING ERROR - %s", msg); tst_exit(); } --- 143,148 ---- ! if ((msg = parse_opts(argc, argv, (option_t *) NULL, NULL)) != (char *) NULL) { ! tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); tst_exit(); } *************** *** 166,177 **** sprintf(filename, "%s-out.%d", progname, getpid()); ! for(lc = 0; TEST_LOOPING(lc); lc++) ! { /* Reset Tst_count in case we are looping */ Tst_count = 0; /* Create a temporary file for testing */ ! if ((fd = open (filename, O_RDWR | O_CREAT, 0664)) < 0) ! { tst_brkm(TBROK, cleanup, "Could not open file \"%s\" with O_RDWR", --- 157,166 ---- sprintf(filename, "%s-out.%d", progname, getpid()); ! for(lc = 0; TEST_LOOPING(lc); lc++) { /* Reset Tst_count in case we are looping */ Tst_count = 0; /* Create a temporary file for testing */ ! if ((fd = open (filename, O_RDWR | O_CREAT, 0664)) < 0) { tst_brkm(TBROK, cleanup, "Could not open file \"%s\" with O_RDWR", *************** *** 179,184 **** } #ifdef MM_DEBUG ! tst_resm(TINFO, ! "filename = %s opened successfully", filename); #endif --- 168,172 ---- } #ifdef MM_DEBUG ! tst_resm(TINFO, "filename = %s opened successfully", filename); #endif *************** *** 186,193 **** /* Writing 40 KB of random data into this file [32 * 1280 = 40960] */ ! for(i=0; i<1280; i++) ! { ! if(write(fd, str_for_file, strlen(str_for_file)) < 0) ! { tst_brkm(TBROK, cleanup, "Could not write data to file \"%s\"", --- 174,179 ---- /* Writing 40 KB of random data into this file [32 * 1280 = 40960] */ ! for(i=0; i<1280; i++) { ! if(write(fd, str_for_file, strlen(str_for_file)) < 0) { tst_brkm(TBROK, cleanup, "Could not write data to file \"%s\"", *************** *** 197,213 **** /* Get file status for its size */ ! if(fstat(fd, &stat) < 0) ! { ! tst_brkm(TBROK, cleanup, ! "Could not stat file \"%s\"", filename); } /* Map the input file into memory */ ! if ((file = ! (char *) mmap (NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0)) == (char *)-1) ! { ! tst_brkm(TBROK, cleanup, ! "Could not mmap file \"%s\"", filename); } --- 183,194 ---- /* Get file status for its size */ ! if(fstat(fd, &stat) < 0) { ! tst_brkm(TBROK, cleanup, "Could not stat file \"%s\"", filename); } /* Map the input file into memory */ ! if ((file = (char *) mmap (NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0)) == (char *)-1) { ! tst_brkm(TBROK, cleanup, "Could not mmap file \"%s\"", filename); } *************** *** 216,339 **** #ifdef MM_DEBUG ! tst_resm(TINFO, ! "The Page size is %d", ! pagesize); #endif ! /* Test Case 1a */ ! TEST(madvise(file, -100, MADV_NORMAL)); ! check_and_print(EINVAL); ! ! /* Test Case 1b */ ! TEST(madvise(file+100, stat.st_size, MADV_NORMAL)); check_and_print(EINVAL); ! /* Test Case 1c */ ! TEST(madvise(file,stat.st_size,1212)); check_and_print(EINVAL); ! /* Test Case 1d */ ! if(mlock((void *)file,stat.st_size)<0) ! { ! tst_brkm(TBROK, cleanup, ! "Error in getting memory " "lock for the requested page(s)"); } ! TEST(madvise(file,stat.st_size,MADV_DONTNEED)); check_and_print(EINVAL); ! if(munmap(file,stat.st_size) < 0) ! { ! tst_brkm(TBROK, cleanup, ! "Error %d in munmap : %s", errno, strerror(errno)); ! } ! /* Test Case 2a */ ! if ((file = ! (char *) mmap (NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0)) == (char *)-1) ! { ! tst_brkm(TBROK, cleanup, ! "Could not mmap file"); ! } ! TEST(madvise(file,stat.st_size + 40960,MADV_NORMAL)); check_and_print(ENOMEM); ! /* Test Case 3a */ ! /* Set new Process RSS (in no of pages) ! for the current process */ ! if(stat.st_size == 0) ! { ! tst_brkm(TBROK, cleanup, ! "File size is 0, " ! "test can not proceed\n"); ! } ! ! no_of_file_pages = (stat.st_size / pagesize); ! if(stat.st_size % pagesize != 0) ! { ! no_of_file_pages++; ! } ! ! if(getrlimit(RLIMIT_RSS, &rlim) < 0) ! { ! tst_brkm(TBROK, cleanup, ! "Error %d in getrlimit : %s", ! errno, strerror(errno)); ! } ! ! #ifdef MM_DEBUG ! tst_resm(TINFO, ! "RLIM_INFINITY :: %d\n", ! RLIM_INFINITY); ! tst_resm(TINFO, ! "Current RSS [soft limit :: %d, " ! "hard limit :: %d]", ! rlim.rlim_cur, rlim.rlim_max); ! #endif ! ! /* Set the rlimit for RSS to half of the actual file size */ ! rlim.rlim_cur = no_of_file_pages / 2; ! ! if(setrlimit(RLIMIT_RSS, &rlim) < 0) ! { ! tst_brkm(TBROK, cleanup, ! "Error %d in setrlimit : %s", ! errno, strerror(errno)); ! } ! else ! { ! #ifdef MM_DEBUG ! tst_resm(TINFO, ! "New RSS rlimit has been set successfully"); ! #endif ! } ! ! if(getrlimit(RLIMIT_RSS, &rlim) < 0) ! { ! tst_brkm(TBROK, cleanup, ! "Error %d in getrlimit : %s", ! errno, strerror(errno)); ! } ! ! #ifdef MM_DEBUG ! tst_resm(TINFO, ! "RLIM_INFINITY :: %d", ! RLIM_INFINITY); ! tst_resm(TINFO, ! "Current RSS [soft limit is :: %d, " ! "hard limit :: %d]", ! rlim.rlim_cur, rlim.rlim_max); ! #endif ! ! /* Now give advice to kernel ! for keeping more than max RSS limit for the process */ ! TEST(madvise(file,stat.st_size,MADV_WILLNEED)); ! check_and_print(EIO); ! ! ! /* Test Case 4a */ /* Create one memory segment using malloc */ ptr_memory_allocated = (char *) malloc(5 * sizeof(pagesize)); --- 197,234 ---- #ifdef MM_DEBUG ! tst_resm(TINFO, "The Page size is %d", pagesize); #endif ! /* Test Case 1 */ ! TEST(madvise(file+100, stat.st_size, MADV_NORMAL)); check_and_print(EINVAL); ! /* Test Case 2 */ ! TEST(madvise(file,stat.st_size,1212)); check_and_print(EINVAL); ! /* Test Case 3 */ ! if(mlock((void *)file,stat.st_size)<0) { ! tst_brkm(TBROK, cleanup, "Error in getting memory " "lock for the requested page(s)"); } ! TEST(madvise(file,stat.st_size,MADV_DONTNEED)); check_and_print(EINVAL); ! if(munmap(file,stat.st_size) < 0) { ! tst_brkm(TBROK, cleanup, "Error %d in munmap : %s", errno, strerror(errno)); ! } ! /* Test Case 4 */ ! if ((file = (char *) mmap (NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0)) == (char *)-1) { ! tst_brkm(TBROK, cleanup, "Could not mmap file"); ! } ! TEST(madvise(file,stat.st_size + 40960,MADV_NORMAL)); check_and_print(ENOMEM); ! /* Test Case 5 */ /* Create one memory segment using malloc */ ptr_memory_allocated = (char *) malloc(5 * sizeof(pagesize)); *************** *** 341,361 **** tmp_memory_allocated = ptr_memory_allocated; tmp_memory_allocated = (char *)(((int) tmp_memory_allocated + pagesize-1) & ~(pagesize-1)); ! ! TEST(madvise(tmp_memory_allocated, 5 * pagesize, MADV_NORMAL)); check_and_print(EBADF); free((void *)ptr_memory_allocated); ! /* Finally Unmapping the whole file */ ! if(munmap(file,stat.st_size) < 0) ! { ! tst_brkm(TBROK, cleanup, ! "Error %d in munmap : %s", ! errno, strerror(errno)); ! } close(fd); } ! cleanup(); return 0; } --- 236,254 ---- tmp_memory_allocated = ptr_memory_allocated; tmp_memory_allocated = (char *)(((int) tmp_memory_allocated + pagesize-1) & ~(pagesize-1)); ! ! TEST(madvise(tmp_memory_allocated, 5 * pagesize, MADV_WILLNEED)); check_and_print(EBADF); free((void *)ptr_memory_allocated); ! /* Finally Unmapping the whole file */ ! if(munmap(file,stat.st_size) < 0) { ! tst_brkm(TBROK, cleanup, "Error %d in munmap : %s", ! errno, strerror(errno)); ! } close(fd); } ! cleanup(); return 0; } *************** *** 366,378 **** void setup(void) { ! /* capture signals */ ! tst_sig(NOFORK, DEF_HANDLER, cleanup); ! /* Pause if that option was specified */ ! TEST_PAUSE; /* Create temp directory and change to that */ tst_tmpdir(); ! } /* End setup() */ --- 259,271 ---- void setup(void) { ! /* capture signals */ ! tst_sig(NOFORK, DEF_HANDLER, cleanup); ! /* Pause if that option was specified */ ! TEST_PAUSE; /* Create temp directory and change to that */ tst_tmpdir(); ! }/* End setup() */ *************** *** 383,392 **** void cleanup(void) { ! /* ! * print timing stats if that option was specified. ! * print errno log if that option was specified. ! */ ! TEST_CLEANUP; ! /* Remove temp directory and files */ tst_rmdir(); --- 276,285 ---- void cleanup(void) { ! /* ! * print timing stats if that option was specified. ! * print errno log if that option was specified. ! */ ! TEST_CLEANUP; ! /* Remove temp directory and files */ tst_rmdir(); *************** *** 395,430 **** tst_exit(); ! } /* End cleanup() */ /*************************************************************** * check_and_print(extected_errno) - checks the returned value of call ! * and tests whether the returned errno is the expected errno or not ! * and prints the appropriate messages. ***************************************************************/ void check_and_print(int expected_errno) { ! if(TEST_RETURN == -1) ! { ! if(TEST_ERRNO == expected_errno) ! { ! tst_resm(TPASS, ! "expected failure - errno = %d : %s", TEST_ERRNO, strerror(TEST_ERRNO)); ! } ! else ! { ! tst_resm(TFAIL, ! "madvise failed with wrong errno, " ! "expected errno = %d, " ! "got errno = %d : %s", EINVAL, TEST_ERRNO, strerror(TEST_ERRNO)); } ! } ! else ! { ! tst_resm(TFAIL, ! "madvise failed, expected " ! "return value = -1, got %d", ! TEST_RETURN); } } --- 288,311 ---- tst_exit(); ! } /* End cleanup() */ /*************************************************************** * check_and_print(extected_errno) - checks the returned value of call ! * and tests whether the returned errno is the expected errno or not ! * and prints the appropriate messages. ***************************************************************/ void check_and_print(int expected_errno) { ! if(TEST_RETURN == -1) { ! if(TEST_ERRNO == expected_errno) { ! tst_resm(TPASS, "expected failure - errno = %d : %s", TEST_ERRNO, strerror(TEST_ERRNO)); ! } else { ! tst_resm(TFAIL, "got errno = %d : %s", EINVAL, TEST_ERRNO, strerror(TEST_ERRNO)); } ! } else { ! tst_resm(TFAIL, "madvise failed, expected " ! "return value = -1, got %d", TEST_RETURN); } } |