From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v3 1/7] lib: Add library functions for sync related syscalls
Date: Tue, 19 Feb 2019 15:06:27 +0100 [thread overview]
Message-ID: <20190219140627.GC32031@rei.lan> (raw)
In-Reply-To: <1550568500-10871-2-git-send-email-sumit.garg@linaro.org>
Hi!
> Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
> ---
> include/tst_sync_device.h | 17 ++++++++++
> lib/tst_sync_device.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 97 insertions(+)
> create mode 100644 include/tst_sync_device.h
> create mode 100644 lib/tst_sync_device.c
>
> diff --git a/include/tst_sync_device.h b/include/tst_sync_device.h
> new file mode 100644
> index 0000000..b07c490
> --- /dev/null
> +++ b/include/tst_sync_device.h
> @@ -0,0 +1,17 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2019 Linaro Limited. All rights reserved.
> + * Author: Sumit Garg <sumit.garg@linaro.org>
> + */
> +
> +#ifndef TST_SYNC_DEVICE_H__
> +#define TST_SYNC_DEVICE_H__
> +
> +#include <stdbool.h>
> +
> +void tst_sync_device_init(const char *dev);
> +int tst_sync_device_write(const char *file, unsigned int size_mb);
> +bool tst_sync_device_check(unsigned int size_mb);
> +void tst_sync_device_cleanup(void);
> +
> +#endif
> diff --git a/lib/tst_sync_device.c b/lib/tst_sync_device.c
> new file mode 100644
> index 0000000..5a0a17c
> --- /dev/null
> +++ b/lib/tst_sync_device.c
> @@ -0,0 +1,80 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2019 Linaro Limited. All rights reserved.
> + * Author: Sumit Garg <sumit.garg@linaro.org>
> + */
> +
> +#include <stdlib.h>
> +#include <stdio.h>
> +#include <sys/types.h>
> +
> +#define TST_NO_DEFAULT_MAIN
> +#include "tst_test.h"
> +#include "lapi/stat.h"
> +#include "tst_sync_device.h"
> +
> +#define SIZE_MB (1024*1024)
> +#define MODE 0644
> +
> +static char dev_stat_path[1024];
> +static char *buffer;
> +static int fd;
> +static unsigned long prev_write_sec;
> +
> +void tst_sync_device_init(const char *dev)
> +{
> + struct stat st;
> +
> + snprintf(dev_stat_path, sizeof(dev_stat_path), "/sys/block/%s/stat",
> + strrchr(dev, '/') + 1);
> +
> + if (stat(dev_stat_path, &st) != 0)
> + tst_brk(TCONF, "Test device stat file: %s not found",
> + dev_stat_path);
> +
> + buffer = SAFE_MALLOC(SIZE_MB);
> +
> + memset(buffer, 0, SIZE_MB);
> +}
> +
> +int tst_sync_device_write(const char *file, unsigned int size_mb)
> +{
> + unsigned int counter;
^
It's kind of strange to name this variable anything
else but i.
> + SAFE_FILE_SCANF(dev_stat_path, "%*s %*s %*s %*s %*s %*s %lu",
> + &prev_write_sec);
> +
> + fd = SAFE_OPEN(file, O_RDWR|O_CREAT, MODE);
^
Extra space.
> + /* Filling the test file */
^
Useless comment, it's pretty clear what we do here.
> + for (counter = 0; counter < size_mb; counter++)
> + SAFE_WRITE(1, fd, buffer, SIZE_MB);
> +
> + return fd;
> +}
> +
> +bool tst_sync_device_check(unsigned int size_mb)
> +{
> + unsigned long write_sec = 0;
> + bool res = false;
> +
> + SAFE_FILE_SCANF(dev_stat_path, "%*s %*s %*s %*s %*s %*s %lu",
> + &write_sec);
> +
> + if ((write_sec - prev_write_sec) * 512 >=
> + (size_mb * SIZE_MB))
> + res = true;
> +
> + SAFE_CLOSE(fd);
> +
> + return res;
> +}
I do not like that this API is tailored just to this specific usecase.
I would rather see the code that writes the file separated from the code
that measures the bytes written.
Maybe we just need a tst_dev_bytes_written() function that would return
the bytes written since the last call of the function so that we could
do:
fd = SAFE_OPEN(...);
tst_dev_blocks_written(tst_device.dev);
tst_fill_fd(fd, 0, TST_MB, size_mb);
TEST(fdsync(fd));
if (TST_RET)
tst_brk(TFAIL | TTERRNO, "syncfd() failed with %i", TST_RET);
written = tst_dev_blocks_written(tst_device.dev);
SAFE_CLOSE(fd);
if (written >= size_mb * TST_DEV_BLOCKS_IN_MB)
tst_res(TPASS, ...);
else
tst_res(TFAIL, ...);
The test a bit longer, but the library functions are more reusable, if
you do 'git grep -B 1 SAFE_WRITE' you can see that the tst_fill_fd
function that loops over SAFE_WRITE could be used in a few places
already.
> +void tst_sync_device_cleanup(void)
> +{
> + if (buffer)
> + free(buffer);
> +
> + if (fd > 0)
> + SAFE_CLOSE(fd);
> +}
> --
> 2.7.4
>
--
Cyril Hrubis
chrubis@suse.cz
next prev parent reply other threads:[~2019-02-19 14:06 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-19 9:28 [LTP] [PATCH v3 0/7] syscalls: add sync device test-cases Sumit Garg
2019-02-19 9:28 ` [LTP] [PATCH v3 1/7] lib: Add library functions for sync related syscalls Sumit Garg
2019-02-19 14:06 ` Cyril Hrubis [this message]
2019-02-20 7:57 ` Sumit Garg
2019-02-20 12:03 ` Cyril Hrubis
2019-02-20 12:08 ` Sumit Garg
2019-02-19 9:28 ` [LTP] [PATCH v3 2/7] syscalls: add syncfs() sync device test-case Sumit Garg
2019-02-19 9:28 ` [LTP] [PATCH v3 3/7] syscalls/sync: add " Sumit Garg
2019-02-19 14:09 ` Cyril Hrubis
2019-02-20 8:20 ` Sumit Garg
2019-02-19 9:28 ` [LTP] [PATCH v3 4/7] syscalls/fsync: " Sumit Garg
2019-02-19 9:28 ` [LTP] [PATCH v3 5/7] syscalls/fdatasync: " Sumit Garg
2019-02-19 9:28 ` [LTP] [PATCH v3 6/7] syscalls/sync_file_range: Use C library wrapper if present Sumit Garg
2019-02-19 14:19 ` Cyril Hrubis
2019-02-20 8:27 ` Sumit Garg
2019-02-19 9:28 ` [LTP] [PATCH v3 7/7] syscalls/sync_file_range: add sync device test-case Sumit Garg
2019-02-19 14:20 ` Cyril Hrubis
2019-02-20 8:34 ` Sumit Garg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190219140627.GC32031@rei.lan \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox