From 42a22f0824c4618b864582804ce1440b548a462f Mon Sep 17 00:00:00 2001 From: Ralf Kirchner Date: Wed, 5 Dec 2012 13:43:34 +0100 Subject: dosfs: Cluster write optimization Separate cluster write from sector write for quick file write. New test fstests/fsdosfswrite01. --- testsuites/fstests/fsdosfswrite01/Makefile.am | 19 ++ .../fstests/fsdosfswrite01/fsdosfswrite01.doc | 15 ++ .../fstests/fsdosfswrite01/fsdosfswrite01.scn | 0 testsuites/fstests/fsdosfswrite01/init.c | 296 +++++++++++++++++++++ 4 files changed, 330 insertions(+) create mode 100644 testsuites/fstests/fsdosfswrite01/Makefile.am create mode 100644 testsuites/fstests/fsdosfswrite01/fsdosfswrite01.doc create mode 100644 testsuites/fstests/fsdosfswrite01/fsdosfswrite01.scn create mode 100644 testsuites/fstests/fsdosfswrite01/init.c (limited to 'testsuites/fstests/fsdosfswrite01') diff --git a/testsuites/fstests/fsdosfswrite01/Makefile.am b/testsuites/fstests/fsdosfswrite01/Makefile.am new file mode 100644 index 0000000000..2d21751178 --- /dev/null +++ b/testsuites/fstests/fsdosfswrite01/Makefile.am @@ -0,0 +1,19 @@ +rtems_tests_PROGRAMS = fsdosfswrite01 +fsdosfswrite01_SOURCES = init.c + +dist_rtems_tests_DATA = fsdosfswrite01.scn fsdosfswrite01.doc + +include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP@.cfg +include $(top_srcdir)/../automake/compile.am +include $(top_srcdir)/../automake/leaf.am + +AM_CPPFLAGS += -I$(top_srcdir)/../support/include + +LINK_OBJS = $(fsdosfswrite01_OBJECTS) +LINK_LIBS = $(fsdosfswrite01_LDLIBS) + +fsdosfswrite01$(EXEEXT): $(fsdosfswrite01_OBJECTS) $(fsdosfswrite01_DEPENDENCIES) + @rm -f fsdosfswrite01$(EXEEXT) + $(make-exe) + +include $(top_srcdir)/../automake/local.am diff --git a/testsuites/fstests/fsdosfswrite01/fsdosfswrite01.doc b/testsuites/fstests/fsdosfswrite01/fsdosfswrite01.doc new file mode 100644 index 0000000000..e18bf06a39 --- /dev/null +++ b/testsuites/fstests/fsdosfswrite01/fsdosfswrite01.doc @@ -0,0 +1,15 @@ +This file describes the directives and concepts tested by this test set. + +test set name: fsdosfswrite01 + +directives: + - fat_file_write() + - fat_file_write_fat32_or_non_root_dir() + +concepts: + - Avoiding uneccessary device reads is to make sure that writing to the device + does not get slowed down unneccesarily. + - Verify that appending data to a file does not result in reading the new + clusters from device. + - Verify writing a whole cluster does not result in reading the cluster from + device. diff --git a/testsuites/fstests/fsdosfswrite01/fsdosfswrite01.scn b/testsuites/fstests/fsdosfswrite01/fsdosfswrite01.scn new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testsuites/fstests/fsdosfswrite01/init.c b/testsuites/fstests/fsdosfswrite01/init.c new file mode 100644 index 0000000000..1b231497ec --- /dev/null +++ b/testsuites/fstests/fsdosfswrite01/init.c @@ -0,0 +1,296 @@ +/* + * Copyright (c) 2012 embedded brains GmbH. All rights reserved. + * + * embedded brains GmbH + * Obere Lagerstr. 30 + * 82178 Puchheim + * Germany + * + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + */ + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +#include "tmacros.h" +#include +#include +#include +#include +#include + +#define MAX_PATH_LENGTH 100 /* Maximum number of characters per path */ +#define SECTOR_SIZE 512 /* sector size (bytes) */ +#define FAT16_MAX_CLN 65525 /* maximum + 1 number of clusters for FAT16 */ +#define FAT16_DEFAULT_SECTORS_PER_CLUSTER 32 /* Default number of sectors per cluster for FAT16 */ +#define SECTORS_PER_CLUSTER 2 + +static void format_and_mount( const char *dev_name, const char *mount_dir ) +{ + static const msdos_format_request_param_t rqdata = { + .sectors_per_cluster = SECTORS_PER_CLUSTER, + .quick_format = true + }; + + int rv; + + + rv = msdos_format( dev_name, &rqdata ); + rtems_test_assert( rv == 0 ); + + rv = mount( dev_name, + mount_dir, + RTEMS_FILESYSTEM_TYPE_DOSFS, + RTEMS_FILESYSTEM_READ_WRITE, + NULL ); + rtems_test_assert( rv == 0 ); +} + +static void do_fsync( const char *file ) +{ + int rv; + int fd; + + + fd = open( file, O_RDONLY ); + rtems_test_assert( fd >= 0 ); + + rv = fsync( fd ); + rtems_test_assert( rv == 0 ); + + rv = close( fd ); + rtems_test_assert( rv == 0 ); +} + +static void check_block_stats( const char *dev_name, + const char *mount_dir, + const rtems_blkdev_stats *expected_stats ) +{ + int fd; + int rv; + rtems_blkdev_stats actual_stats; + + + do_fsync( mount_dir ); + + fd = open( dev_name, O_RDONLY ); + rtems_test_assert( fd >= 0 ); + + rv = ioctl( fd, RTEMS_BLKIO_GETDEVSTATS, &actual_stats ); + rtems_test_assert( rv == 0 ); + rtems_test_assert( memcmp( &actual_stats, expected_stats, + sizeof( actual_stats ) ) == 0 ); + + rv = close( fd ); + rtems_test_assert( rv == 0 ); +} + +static void reset_block_stats( const char *dev_name, const char *mount_dir ) +{ + int fd; + int rv; + + + do_fsync( mount_dir ); + + fd = open( dev_name, O_RDONLY ); + rtems_test_assert( fd >= 0 ); + + rv = ioctl( fd, RTEMS_BLKIO_PURGEDEV ); + rtems_test_assert( rv == 0 ); + + rv = ioctl( fd, RTEMS_BLKIO_RESETDEVSTATS ); + rtems_test_assert( rv == 0 ); + + rv = close( fd ); + rtems_test_assert( rv == 0 ); +} + +static int create_file( const char *file_name ) +{ + mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; + + + return creat( file_name, mode ); +} + +static void test_normal_file_write( + const char *dev_name, + const char *mount_dir, + const char *file_name ) +{ + static const rtems_blkdev_stats complete_block_stats = { + .read_hits = 0, + .read_misses = 0, + .read_ahead_transfers = 0, + .read_blocks = 0, + .read_errors = 0, + .write_transfers = 1, + .write_blocks = 1, + .write_errors = 0 + }; + static const rtems_blkdev_stats new_block_stats = { + .read_hits = 8, + .read_misses = 2, + .read_ahead_transfers = 0, + .read_blocks = 2, + .read_errors = 0, + .write_transfers = 1, + .write_blocks = 4, + .write_errors = 0 + }; + + int rv; + int fd; + ssize_t num_bytes; + uint8_t cluster_buf[SECTOR_SIZE + * SECTORS_PER_CLUSTER]; + uint32_t cluster_size = sizeof( cluster_buf ); + off_t off; + + + memset( cluster_buf, 0xFE, cluster_size ); + + format_and_mount( dev_name, mount_dir ); + + fd = create_file( file_name ); + rtems_test_assert( fd >= 0 ); + + num_bytes = write( fd, cluster_buf, cluster_size ); + rtems_test_assert( (ssize_t) cluster_size == num_bytes ); + + off = lseek( fd, 0, SEEK_SET ); + rtems_test_assert( off == 0 ); + + reset_block_stats( dev_name, mount_dir ); + + /* Write a complete cluster into an existing file space */ + num_bytes = write( fd, cluster_buf, cluster_size ); + rtems_test_assert( (ssize_t) cluster_size == num_bytes ); + + check_block_stats( dev_name, mount_dir, &complete_block_stats ); + reset_block_stats( dev_name, mount_dir ); + + num_bytes = write( fd, cluster_buf, cluster_size ); + rtems_test_assert( (ssize_t) cluster_size == num_bytes ); + + /* Write a new partial cluster into a new file space */ + num_bytes = write( fd, cluster_buf, 1 ); + rtems_test_assert( num_bytes == 1 ); + + check_block_stats( dev_name, mount_dir, &new_block_stats ); + + rv = close( fd ); + rtems_test_assert( 0 == rv ); + + rv = unmount( mount_dir ); + rtems_test_assert( 0 == rv ); +} + +static void test_fat12_root_directory_write( const char *dev_name, + const char *mount_dir, + const char *file_name ) +{ + static const rtems_blkdev_stats fat12_root_dir_stats = { + .read_hits = 11, + .read_misses = 2, + .read_ahead_transfers = 0, + .read_blocks = 2, + .read_errors = 0, + .write_transfers = 1, + .write_blocks = 1, + .write_errors = 0 + }; + + int fd; + int rv; + + + format_and_mount( dev_name, mount_dir ); + + reset_block_stats( dev_name, mount_dir ); + + fd = create_file( file_name ); + rtems_test_assert( fd >= 0 ); + + rv = close( fd ); + rtems_test_assert( rv == 0 ); + + check_block_stats( dev_name, mount_dir, &fat12_root_dir_stats ); + + rv = unmount( mount_dir ); + rtems_test_assert( rv == 0 ); +} + +static void test( void ) +{ + static const char dev_name[] = "/dev/sda"; + static const char mount_dir[] = "/mnt"; + static const char file_name[] = "/mnt/file.txt"; + + rtems_status_code sc; + int rv; + + + sc = rtems_disk_io_initialize(); + rtems_test_assert( sc == RTEMS_SUCCESSFUL ); + + rv = mkdir( mount_dir, S_IRWXU | S_IRWXG | S_IRWXO ); + rtems_test_assert( 0 == rv ); + + /* A 1.44 MB disk */ + sc = rtems_sparse_disk_create_and_register( + dev_name, + SECTOR_SIZE, + 64, + 2880, + 0 + ); + rtems_test_assert( RTEMS_SUCCESSFUL == sc ); + + test_fat12_root_directory_write( dev_name, mount_dir, file_name ); + + test_normal_file_write( dev_name, mount_dir, file_name ); + + rv = unlink( dev_name ); + rtems_test_assert( rv == 0 ); +} + +static void Init( rtems_task_argument arg ) +{ + puts( "\n\n*** TEST FSDOSFSWRITE 1 ***" ); + + test(); + + puts( "*** END OF TEST FSDOSFSWRITE 1 ***" ); + + rtems_test_exit( 0 ); +} + +#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER +#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER +#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK + +#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM + +#define CONFIGURE_FILESYSTEM_DOSFS + +/* 1 device file for blkstats + 1 file for writing + 1 mount_dir + stdin + stdout + stderr + device file when mounted */ +#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 8 + +#define CONFIGURE_UNLIMITED_OBJECTS +#define CONFIGURE_UNIFIED_WORK_AREAS + +#define CONFIGURE_INIT_TASK_STACK_SIZE ( 32 * 1024 ) + +#define CONFIGURE_RTEMS_INIT_TASKS_TABLE + +#define CONFIGURE_BDBUF_BUFFER_MAX_SIZE ( 32 * 1024 ) + +#define CONFIGURE_INIT + +#include \ No newline at end of file -- cgit v1.2.3