Skip to contentSkip to conversation

lib: add new function tst_get_device_size

PATCH New Li Wang 2 participants 5 messages Jul 9, 2021 Library code

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH v3 1/3 lib: add new function tst_get_device_size

---
 doc/c-test-api.txt   | 10 ++++++
 include/tst_device.h |  7 ++++
 lib/tst_device.c     | 76 +++++++++++++++++++++++++-------------------
 3 files changed, 60 insertions(+), 33 deletions(-)
doc/c-test-api.txt +10 -0
index 4af1a9c97..e4ba858cf 100644
@@ -979,6 +979,16 @@ This function finds the block dev that this path belongs to, it uses stat functi
 to get the major/minor number of the path. Then scan them in "/proc/self/mountinfo"
 and list 2th column value after ' - ' string as its block dev if match succeeds.
 
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+
+uint64_t tst_get_device_size(const char *dev_path);
+-------------------------------------------------------------------------------
+
+This function gets size of the given block device, it checks the dev_path is
+valid first, if yes, return the size in MB, otherwise return -1.
+
 1.16 Formatting a device with a filesystem
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
include/tst_device.h +7 -0
index 1d1246e82..72c560c02 100644
@@ -7,10 +7,12 @@
 #define TST_DEVICE_H__
 
 #include <unistd.h>
+#include <stdint.h>
 
 struct tst_device {
 	const char *dev;
 	const char *fs_type;
+	uint64_t size;
 };
 
 /*
@@ -57,6 +59,11 @@ int tst_find_free_loopdev(const char *path, size_t path_len);
  */
 int tst_attach_device(const char *dev_path, const char *file_path);
 
+/*
+ * Get size (in MB) of the given device
+ */
+uint64_t tst_get_device_size(const char *dev_path);
+
 /*
  * Detaches a file from a loop device fd.
  *
lib/tst_device.c +43 -33

95 lines not on this page · raw message

Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v3 2/3 lib: limit the size of tmpfs in LTP

LTP mount and make use of the whole tmpfs of the test system, generally, it's fine. But if a test (e.g fallocate06) try to fill full in the filesystem, it takes too long to complete testing on a large memory system.

This patch adds a new function limit_tmpfs_mount_size with appending '-o size=xxM' to the mount options in prepare_device() which helps limit the tmpfs mount size.

---
 lib/tst_test.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
lib/tst_test.c +24 -1
index 55449c80b..b996f1b2e 100644
@@ -882,8 +882,26 @@ static void prepare_and_mount_dev_fs(const char *mntpoint)
 	}
 }
 
+static char *limit_tmpfs_mount_size(const char *mnt_data,
+		char *buf, size_t buf_size, const char *fs_type)
+{
+	if (strcmp(fs_type, "tmpfs"))
+		return mnt_data;
+
+	if (mnt_data)
+		snprintf(buf, buf_size, "%s,size=%luM", mnt_data, tdev.size);
+	else
+		snprintf(buf, buf_size, "size=%luM", tdev.size);
+
+	tst_res(TINFO, "Limiting tmpfs size to %luMB", tdev.size);
+
+	return buf;
+}
+
 static void prepare_device(void)
 {
+	char *mnt_data, buf[1024];
+
 	if (tst_test->format_device) {
 		SAFE_MKFS(tdev.dev, tdev.fs_type, tst_test->dev_fs_opts,
 			  tst_test->dev_extra_opts);
@@ -896,8 +914,11 @@ static void prepare_device(void)
 	}
 
 	if (tst_test->mount_device) {
+		mnt_data = limit_tmpfs_mount_size(tst_test->mnt_data,
+				buf, sizeof(buf), tdev.fs_type);
+
 		SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
-			   tst_test->mnt_flags, tst_test->mnt_data);
+			   tst_test->mnt_flags, mnt_data);
 		mntpoint_mounted = 1;
 	}
 }
@@ -1015,6 +1036,8 @@ static void do_setup(int argc, char *argv[])
 		if (!tdev.dev)
 			tst_brk(TCONF, "Failed to acquire device");
 
+		tdev.size = tst_get_device_size(tdev.dev);
+
 		tst_device = &tdev;
 
 		if (tst_test->dev_fs_type)
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v3 3/3 lib: mount tmpfs name as ltp-tmpfs

In reply to Li Wang

Given a specific name as "ltp-tmpfs" instead of the "/dev/loopX" string in order to make "tmpfs" filesystem more evident.

Achieve that in get_device_name() function.

---
 lib/tst_test.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
lib/tst_test.c +10 -2
index b996f1b2e..f4d9f8e3b 100644
@@ -898,6 +898,14 @@ static char *limit_tmpfs_mount_size(const char *mnt_data,
 	return buf;
 }
 
+static const char *get_device_name(const char *fs_type)
+{
+       if (!strcmp(fs_type, "tmpfs"))
+               return "ltp-tmpfs";
+       else
+               return tdev.dev;
+}
+
 static void prepare_device(void)
 {
 	char *mnt_data, buf[1024];
@@ -917,8 +925,8 @@ static void prepare_device(void)
 		mnt_data = limit_tmpfs_mount_size(tst_test->mnt_data,
 				buf, sizeof(buf), tdev.fs_type);
 
-		SAFE_MOUNT(tdev.dev, tst_test->mntpoint, tdev.fs_type,
-			   tst_test->mnt_flags, mnt_data);
+		SAFE_MOUNT(get_device_name(tdev.fs_type), tst_test->mntpoint,
+				tdev.fs_type, tst_test->mnt_flags, mnt_data);
 		mntpoint_mounted = 1;
 	}
 }
Signature · 1 line
2.31.1
author Applied
#4 lore raw

PATCH v3 1/3 lib: add new function tst_get_device_size

Quoted 5 lines · Cyril Hrubis

Hi!
The whole patchset looks good.

Merged, thanks for the review!

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210709/1ed451d4/attachment.htm>