Refactoring dio_append.c using LTP API
PATCH v3 1/1 Refactoring dio_append.c using LTP API
Signed-off-by Andrea Cervesato <[email protected]>
--- testcases/kernel/io/ltp-aiodio/dio_append.c | 167 +++++++------------- 1 file changed, 55 insertions(+), 112 deletions(-)
testcases/kernel/io/ltp-aiodio/dio_append.c +55 -112
@@ -1,143 +1,86 @@ - +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2004 Daniel McNeil <[email protected]> -* 2004 Open Source Development Lab -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -* -* Module: .c +* 2004 Open Source Development Lab +* 2004 Marty Ridgeway <[email protected]> +* Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]> */ -/* -* Change History: -* -* 2/2004 Marty Ridgeway ([email protected]) Changes to adapt to LTP +/*\ +* [Description] * -*/ -/* * dio_append - append zeroed data to a file using O_DIRECT while * a 2nd process is doing buffered reads and check if the buffer * reads always see zero. */ #define _GNU_SOURCE -#include <stdlib.h> -#include <sys/types.h> -#include <signal.h> -#include <errno.h> -#include <fcntl.h> -#include <stdio.h> -#include <unistd.h> -#include <memory.h> -#include <limits.h> +#include "tst_test.h" +#include "common.h" -#include "test.h" -#define NUM_CHILDREN 8 +static int *run_child; -#include "common_checkzero.h" +static char *str_numchildren; +static char *str_filesize; -int read_eof (char *filename) -{ -int fd; -int i; -int r; -char buf[4096 ]; +static int numchildren; +static long long filesize; -while ((fd =open (filename,O_RDONLY )) <0 ) { -sleep (1 );/* wait for file to be created */ - } - -for (i =0 ; i <1000000 ; i++) { -off_t offset; -char *bufoff; - - offset =lseek (fd,SEEK_END ,0 ); - r =read (fd, buf,4096 ); -if (r >0 ) { -if ((bufoff =check_zero (buf, r))) { -fprintf (stderr,"non-zero read at offset %p\n" , - offset + bufoff); -exit (1 ); - } - } - } -return 0 ; -} - -void dio_append (char *filename) +static void setup (void ) { -int fd; -void *bufptr =NULL ; -int i; -int w; + numchildren =16 ; + filesize =64 *1024 ; - fd =open (filename,O_DIRECT |O_WRONLY |O_CREAT ,0666 ); +if (tst_parse_int (str_numchildren, &numchildren,1 ,INT_MAX )) +tst_brk (TBROK ,"Invalid number of children '%s'" , str_numchildren); -if (fd <0 ) { -perror ("cannot create file" ); -return ; - } +if (tst_parse_filesize (str_filesize, &filesize,1 ,LLONG_MAX )) +tst_brk (TBROK ,"Invalid file size '%s'" , str_filesize); -TEST (posix_memalign (&bufptr,4096 ,64 *1024 )); -if (TEST_RETURN ) { -tst_resm (TBROK |TRERRNO ,"cannot malloc aligned memory" ); -close (fd); -return ; - } + run_child =SAFE_MMAP (NULL ,sizeof (int ),PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +} -memset (bufptr,0 ,64 *1024 ); -for (i =0 ; i <1000 ; i++) { -if ((w =write (fd, bufptr,64 *1024 )) !=64 *1024 ) { -fprintf (stderr,"write %d returned %d\n" , i, w); - } - } +static void cleanup (void ) +{ +SAFE_MUNMAP (run_child,sizeof (int )); } -int main (void ) +static void run (void ) { -char filename[PATH_MAX ]; -int pid[NUM_CHILDREN ]; -int num_children =1 ; +char *filename ="dio_append" ; +int status;int i; -snprintf (filename,sizeof (filename),"%s/aiodio/file" , -getenv ("TMP" ) ?getenv ("TMP" ) :"/tmp" ); - -printf ("Begin dio_append test...\n" ); - -for (i =0 ; i < num_children; i++) { -if ((pid[i] =fork ()) ==0 ) { -/* child */ -return read_eof (filename); - }else if (pid[i] <0 ) { -/* error */ -perror ("fork error" ); -break ; - }else { -/* Parent */ -continue ; + *run_child =1 ; + +for (i =0 ; i < numchildren; i++) { +if (!SAFE_FORK ()) { +io_read (filename, filesize, run_child); +return ; } } -/* -* Parent appends to end of file using direct i/o -*/ +tst_res (TINFO ,"Parent append to file" ); -dio_append (filename); +io_append (filename,0 ,O_DIRECT |O_WRONLY |O_CREAT , filesize,1000 ); -for (i =0 ; i < num_children; i++) { -kill (pid[i],SIGTERM ); - } -return 0 ; +if (SAFE_WAITPID (-1 , &status,WNOHANG )) +tst_res (TFAIL ,"Non zero bytes read" ); +else +tst_res (TPASS ,"All bytes read were zeroed" ); + + *run_child =0 ; } + +static struct tst_test test = { + .test_all = run, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir =1 , + .forks_child =1 , + .options = (struct tst_option []) { + {"n:" , &str_numchildren,"Number of threads (default 1000)" }, + {"s:" , &str_filesize,"Size of file (default 64K)" }, + {} + }, +};
Signature · 5 lines
2.34.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp