From 74a7b3ff69e7669bc8875bafde55b8edc682151d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 23 Apr 2008 12:44:00 +0000 Subject: [PATCH] * Wrote a test application that tries to fragment your disk by creating many small files, and then deleting every other. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25116 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/add-ons/kernel/file_systems/Jamfile | 1 + .../kernel/file_systems/fragmenter/Jamfile | 5 + .../file_systems/fragmenter/fragmenter.cpp | 163 ++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 src/tests/add-ons/kernel/file_systems/fragmenter/Jamfile create mode 100644 src/tests/add-ons/kernel/file_systems/fragmenter/fragmenter.cpp diff --git a/src/tests/add-ons/kernel/file_systems/Jamfile b/src/tests/add-ons/kernel/file_systems/Jamfile index c7786f4b53..92040e6597 100644 --- a/src/tests/add-ons/kernel/file_systems/Jamfile +++ b/src/tests/add-ons/kernel/file_systems/Jamfile @@ -3,6 +3,7 @@ SubDir HAIKU_TOP src tests add-ons kernel file_systems ; SubInclude HAIKU_TOP src tests add-ons kernel file_systems bfs ; SubInclude HAIKU_TOP src tests add-ons kernel file_systems cdda ; SubInclude HAIKU_TOP src tests add-ons kernel file_systems fs_shell ; +SubInclude HAIKU_TOP src tests add-ons kernel file_systems fragmenter ; #SubInclude HAIKU_TOP src tests add-ons kernel file_systems iso9660 ; SubInclude HAIKU_TOP src tests add-ons kernel file_systems udf ; SubInclude HAIKU_TOP src tests add-ons kernel file_systems userlandfs ; diff --git a/src/tests/add-ons/kernel/file_systems/fragmenter/Jamfile b/src/tests/add-ons/kernel/file_systems/fragmenter/Jamfile new file mode 100644 index 0000000000..7e76204e8c --- /dev/null +++ b/src/tests/add-ons/kernel/file_systems/fragmenter/Jamfile @@ -0,0 +1,5 @@ +SubDir HAIKU_TOP src tests add-ons kernel file_systems fragmenter ; + +BinCommand fragmenter : + fragmenter.cpp +; diff --git a/src/tests/add-ons/kernel/file_systems/fragmenter/fragmenter.cpp b/src/tests/add-ons/kernel/file_systems/fragmenter/fragmenter.cpp new file mode 100644 index 0000000000..d7f586175e --- /dev/null +++ b/src/tests/add-ons/kernel/file_systems/fragmenter/fragmenter.cpp @@ -0,0 +1,163 @@ +/* + * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Distributed under the terms of the MIT License. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +extern const char* __progname; +const char* kProgramName = __progname; + +const int32_t kDefaultFiles = -1; +const off_t kDefaultFileSize = 4096; + + +static void +usage(int status) +{ + printf("usage: %s [--files ] [--size ]\n", + kProgramName); + printf("options:\n"); + printf(" -f --files Number of files to be created. Defaults to as " + "many as fit.\n"); + printf(" -h --size Size of each file. Defaults to 4KB.\n"); + + exit(status); +} + + +static bool +create_file(int32_t i, const char* suffix, const char* buffer, size_t size) +{ + char name[64]; + snprintf(name, sizeof(name), "fragments/%06d%s", i, suffix); + + int fd = open(name, O_CREAT | O_WRONLY | O_TRUNC, 0644); + if (fd < 0) { + fprintf(stderr, "%s: Could not create file %d: %s\n", kProgramName, + i, strerror(errno)); + return false; + } + + if (write(fd, buffer, size) < (ssize_t)size) { + fprintf(stderr, "%s: Could not write file %d: %s\n", kProgramName, + i, strerror(errno)); + return false; + } + + close(fd); + return true; +} + + +int +main(int argc, char** argv) +{ + int32_t numFiles = kDefaultFiles; + off_t fileSize = kDefaultFileSize; + + int optionIndex = 0; + int opt; + static struct option longOptions[] = { + {"help", no_argument, 0, 'h'}, + {"size", required_argument, 0, 's'}, + {"files", required_argument, 0, 'f'}, + {0, 0, 0, 0} + }; + + do { + opt = getopt_long(argc, argv, "hs:f:", longOptions, &optionIndex); + switch (opt) { + case -1: + // end of arguments, do nothing + break; + + case 'f': + numFiles = strtoul(optarg, NULL, 0); + break; + + case 's': + fileSize = strtoul(optarg, NULL, 0); + break; + + case 'h': + default: + usage(0); + break; + } + } while (opt != -1); + + // fill buffer + + char* buffer = (char*)malloc(fileSize); + if (buffer == NULL) { + fprintf(stderr, "%s: not enough memory.\n", kProgramName); + exit(1); + } + + for (uint32_t i = 0; i < fileSize; i++) { + buffer[i] = (char)(i & 0xff); + } + + // create files + + if (numFiles > 0) + printf("Creating %d files...\n", numFiles); + else + printf("Creating as many files as fit...\n"); + + mkdir("fragments", 0777); + + int32_t filesCreated = 0; + bigtime_t lastTime = 0; + + for (int32_t i = 0; i < numFiles || numFiles < 0; i++) { + if (!create_file(i, "", buffer, fileSize) + || !create_file(i, ".remove", buffer, fileSize)) + break; + + filesCreated++; + + if (lastTime + 250000LL < system_time()) { + printf("%10d\33[1A\n", filesCreated); + lastTime = system_time(); + } + } + + free(buffer); + + // delete fragmentation files + + printf("Deleting %d temporary files...\n", filesCreated); + + for (int32_t i = 0; i < filesCreated; i++) { + char name[64]; + snprintf(name, sizeof(name), "fragments/%06d.remove", i); + + if (remove(name) != 0) { + fprintf(stderr, "%s: Could not remove file %d: %s\n", + kProgramName, i, strerror(errno)); + } + + if (lastTime + 250000LL < system_time()) { + printf("%10d\33[1A\n", i); + lastTime = system_time(); + } + } + + printf(" \33[1A\n"); + // delete progress count + + return 0; +}