NetBSD/sbin/tunefs/tunefs.c

753 lines
20 KiB
C
Raw Permalink Normal View History

/* $NetBSD: tunefs.c,v 1.58 2023/01/07 19:41:30 chs Exp $ */
1993-03-21 12:45:37 +03:00
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\
The Regents of the University of California. All rights reserved.");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
#if 0
1997-09-16 16:54:42 +04:00
static char sccsid[] = "@(#)tunefs.c 8.3 (Berkeley) 5/3/95";
#else
__RCSID("$NetBSD: tunefs.c,v 1.58 2023/01/07 19:41:30 chs Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
/*
* tunefs: change layout parameters to an existing file system.
*/
#include <sys/param.h>
#include <sys/statvfs.h>
#include <ufs/ffs/fs.h>
#include <ufs/ffs/ffs_extern.h>
#include <ufs/ufs/ufs_wapbl.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/quota2.h>
#include <machine/bswap.h>
1994-04-12 09:03:23 +04:00
#include <err.h>
#include <errno.h>
1994-04-12 09:03:23 +04:00
#include <fcntl.h>
1993-03-21 12:45:37 +03:00
#include <fstab.h>
#include <paths.h>
#include <stdbool.h>
#include <stdio.h>
1994-04-12 09:03:23 +04:00
#include <stdlib.h>
#include <string.h>
1994-04-12 09:03:23 +04:00
#include <unistd.h>
#include <util.h>
1994-04-12 09:03:23 +04:00
/* the optimization warning string template */
#define OPTWARN "should optimize for %s with minfree %s %d%%"
1993-03-21 12:45:37 +03:00
union {
struct fs sb;
char data[MAXBSIZE];
} sbun, buf;
1993-03-21 12:45:37 +03:00
#define sblock sbun.sb
int fi;
long dev_bsize = 512;
int needswap = 0;
int is_ufs2 = 0;
int extattr = 0;
off_t sblockloc;
int userquota = 0;
int groupquota = 0;
#define Q2_EN (1)
#define Q2_IGN (0)
#define Q2_DIS (-1)
static off_t sblock_try[] = SBLOCKSEARCH;
1993-03-21 12:45:37 +03:00
static void bwrite(daddr_t, char *, int, const char *);
static void bread(daddr_t, char *, int, const char *);
static void change_log_info(long long);
static void getsb(struct fs *, const char *);
static int openpartition(const char *, int, char *, size_t);
static int isactive(int, struct statvfs *);
static void show_log_info(void);
2011-08-29 18:34:58 +04:00
__dead static void usage(void);
1994-04-12 09:03:23 +04:00
int
main(int argc, char *argv[])
1993-03-21 12:45:37 +03:00
{
int ch, aflag, pflag, Aflag, Fflag, Nflag, openflags;
const char *special, *chg[2];
char device[MAXPATHLEN];
int maxbpg, minfree, optim, secsize;
uint32_t i, avgfilesize, avgfpdir;
bool active;
long long logfilesize;
int secshift, fsbtodb;
2020-05-16 21:31:45 +03:00
const char *avalue, *pvalue, *name;
struct statvfs sfs;
2020-05-16 21:31:45 +03:00
aflag = pflag = Aflag = Fflag = Nflag = 0;
avalue = pvalue = NULL;
maxbpg = minfree = optim = secsize = -1;
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in FreeBSD (three commits; the initial work, man page updates, and a fix to ffs_reload()), with the following differences: - Be consistent between newfs(8) and tunefs(8) as to the options which set and control the tuning parameters for this work (avgfilesize & avgfpdir) - Use u_int16_t instead of u_int8_t to keep track of the number of contiguous directories (suggested by Chuck Silvers) - Work within our FFS_EI framework - Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to the same area of memory The new algorithm has a marked performance increase, especially when performing tasks such as untarring pkgsrc.tar.gz, etc. The original FreeBSD commit messages are attached: ===== mckusick 2001/04/10 01:39:00 PDT Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>. His description of the problem and solution follow. My own tests show speedups on typical filesystem intensive workloads of 5% to 12% which is very impressive considering the small amount of code change involved. ------ One day I noticed that some file operations run much faster on small file systems then on big ones. I've looked at the ffs algorithms, thought about them, and redesigned the dirpref algorithm. First I want to describe the results of my tests. These results are old and I have improved the algorithm after these tests were done. Nevertheless they show how big the perfomance speedup may be. I have done two file/directory intensive tests on a two OpenBSD systems with old and new dirpref algorithm. The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports". The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release. It contains 6596 directories and 13868 files. The test systems are: 1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for test is at wd1. Size of test file system is 8 Gb, number of cg=991, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=35 2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system at wd0, file system for test is at wd1. Size of test file system is 40 Gb, number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50 You can get more info about the test systems and methods at: http://www.ptci.ru/gluk/dirpref/old/dirpref.html Test Results tar -xzf ports.tar.gz rm -rf ports mode old dirpref new dirpref speedup old dirprefnew dirpref speedup First system normal 667 472 1.41 477 331 1.44 async 285 144 1.98 130 14 9.29 sync 768 616 1.25 477 334 1.43 softdep 413 252 1.64 241 38 6.34 Second system normal 329 81 4.06 263.5 93.5 2.81 async 302 25.7 11.75 112 2.26 49.56 sync 281 57.0 4.93 263 90.5 2.9 softdep 341 40.6 8.4 284 4.76 59.66 "old dirpref" and "new dirpref" columns give a test time in seconds. speedup - speed increasement in times, ie. old dirpref / new dirpref. ------ Algorithm description The old dirpref algorithm is described in comments: /* * Find a cylinder to place a directory. * * The policy implemented by this algorithm is to select from * among those cylinder groups with above the average number of * free inodes, the one with the smallest number of directories. */ A new directory is allocated in a different cylinder groups than its parent directory resulting in a directory tree that is spreaded across all the cylinder groups. This spreading out results in a non-optimal access to the directories and files. When we have a small filesystem it is not a problem but when the filesystem is big then perfomance degradation becomes very apparent. What I mean by a big file system ? 1. A big filesystem is a filesystem which occupy 20-30 or more percent of total drive space, i.e. first and last cylinder are physically located relatively far from each other. 2. It has a relatively large number of cylinder groups, for example more cylinder groups than 50% of the buffers in the buffer cache. The first results in long access times, while the second results in many buffers being used by metadata operations. Such operations use cylinder group blocks and on-disk inode blocks. The cylinder group block (fs->fs_cblkno) contains struct cg, inode and block bit maps. It is 2k in size for the default filesystem parameters. If new and parent directories are located in different cylinder groups then the system performs more input/output operations and uses more buffers. On filesystems with many cylinder groups, lots of cache buffers are used for metadata operations. My solution for this problem is very simple. I allocate many directories in one cylinder group. I also do some things, so that the new allocation method does not cause excessive fragmentation and all directory inodes will not be located at a location far from its file's inodes and data. The algorithm is: /* * Find a cylinder group to place a directory. * * The policy implemented by this algorithm is to allocate a * directory inode in the same cylinder group as its parent * directory, but also to reserve space for its files inodes * and data. Restrict the number of directories which may be * allocated one after another in the same cylinder group * without intervening allocation of files. * * If we allocate a first level directory then force allocation * in another cylinder group. */ My early versions of dirpref give me a good results for a wide range of file operations and different filesystem capacities except one case: those applications that create their entire directory structure first and only later fill this structure with files. My solution for such and similar cases is to limit a number of directories which may be created one after another in the same cylinder group without intervening file creations. For this purpose, I allocate an array of counters at mount time. This array is linked to the superblock fs->fs_contigdirs[cg]. Each time a directory is created the counter increases and each time a file is created the counter decreases. A 60Gb filesystem with 8mb/cg requires 10kb of memory for the counters array. The maxcontigdirs is a maximum number of directories which may be created without an intervening file creation. I found in my tests that the best performance occurs when I restrict the number of directories in one cylinder group such that all its files may be located in the same cylinder group. There may be some deterioration in performance if all the file inodes are in the same cylinder group as its containing directory, but their data partially resides in a different cylinder group. The maxcontigdirs value is calculated to try to prevent this condition. Since there is no way to know how many files and directories will be allocated later I added two optimization parameters in superblock/tunefs. They are: int32_t fs_avgfilesize; /* expected average file size */ int32_t fs_avgfpdir; /* expected # of files per directory */ These parameters have reasonable defaults but may be tweeked for special uses of a filesystem. They are only necessary in rare cases like better tuning a filesystem being used to store a squid cache. I have been using this algorithm for about 3 months. I have done a lot of testing on filesystems with different capacities, average filesize, average number of files per directory, and so on. I think this algorithm has no negative impact on filesystem perfomance. It works better than the default one in all cases. The new dirpref will greatly improve untarring/removing/coping of big directories, decrease load on cvs servers and much more. The new dirpref doesn't speedup a compilation process, but also doesn't slow it down. Obtained from: Grigoriy Orlov <gluk@ptci.ru> ===== ===== iedowse 2001/04/23 17:37:17 PDT Pre-dirpref versions of fsck may zero out the new superblock fields fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause panics if these fields were zeroed while a filesystem was mounted read-only, and then remounted read-write. Add code to ffs_reload() which copies the fs_contigdirs pointer from the previous superblock, and reinitialises fs_avgf* if necessary. Reviewed by: mckusick ===== ===== nik 2001/04/10 03:36:44 PDT Add information about the new options to newfs and tunefs which set the expected average file size and number of files per directory. Could do with some fleshing out. =====
2001-09-06 06:16:00 +04:00
avgfilesize = avgfpdir = -1;
logfilesize = -1;
secshift = 0;
chg[FS_OPTSPACE] = "space";
chg[FS_OPTTIME] = "time";
2020-05-16 21:31:45 +03:00
while ((ch = getopt(argc, argv, "AFNa:e:g:h:l:m:o:p:q:S:")) != -1) {
switch (ch) {
case 'A':
Aflag++;
break;
case 'F':
Fflag++;
break;
case 'N':
Nflag++;
break;
2020-05-16 21:31:45 +03:00
case 'a':
name = "ACLs";
avalue = optarg;
if (strcmp(avalue, "enable") &&
strcmp(avalue, "disable")) {
errx(10, "bad %s (options are %s)",
name, "`enable' or `disable'");
}
aflag = 1;
break;
case 'e':
maxbpg = strsuftoll(
"maximum blocks per file in a cylinder group",
optarg, 1, INT_MAX);
break;
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in FreeBSD (three commits; the initial work, man page updates, and a fix to ffs_reload()), with the following differences: - Be consistent between newfs(8) and tunefs(8) as to the options which set and control the tuning parameters for this work (avgfilesize & avgfpdir) - Use u_int16_t instead of u_int8_t to keep track of the number of contiguous directories (suggested by Chuck Silvers) - Work within our FFS_EI framework - Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to the same area of memory The new algorithm has a marked performance increase, especially when performing tasks such as untarring pkgsrc.tar.gz, etc. The original FreeBSD commit messages are attached: ===== mckusick 2001/04/10 01:39:00 PDT Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>. His description of the problem and solution follow. My own tests show speedups on typical filesystem intensive workloads of 5% to 12% which is very impressive considering the small amount of code change involved. ------ One day I noticed that some file operations run much faster on small file systems then on big ones. I've looked at the ffs algorithms, thought about them, and redesigned the dirpref algorithm. First I want to describe the results of my tests. These results are old and I have improved the algorithm after these tests were done. Nevertheless they show how big the perfomance speedup may be. I have done two file/directory intensive tests on a two OpenBSD systems with old and new dirpref algorithm. The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports". The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release. It contains 6596 directories and 13868 files. The test systems are: 1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for test is at wd1. Size of test file system is 8 Gb, number of cg=991, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=35 2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system at wd0, file system for test is at wd1. Size of test file system is 40 Gb, number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50 You can get more info about the test systems and methods at: http://www.ptci.ru/gluk/dirpref/old/dirpref.html Test Results tar -xzf ports.tar.gz rm -rf ports mode old dirpref new dirpref speedup old dirprefnew dirpref speedup First system normal 667 472 1.41 477 331 1.44 async 285 144 1.98 130 14 9.29 sync 768 616 1.25 477 334 1.43 softdep 413 252 1.64 241 38 6.34 Second system normal 329 81 4.06 263.5 93.5 2.81 async 302 25.7 11.75 112 2.26 49.56 sync 281 57.0 4.93 263 90.5 2.9 softdep 341 40.6 8.4 284 4.76 59.66 "old dirpref" and "new dirpref" columns give a test time in seconds. speedup - speed increasement in times, ie. old dirpref / new dirpref. ------ Algorithm description The old dirpref algorithm is described in comments: /* * Find a cylinder to place a directory. * * The policy implemented by this algorithm is to select from * among those cylinder groups with above the average number of * free inodes, the one with the smallest number of directories. */ A new directory is allocated in a different cylinder groups than its parent directory resulting in a directory tree that is spreaded across all the cylinder groups. This spreading out results in a non-optimal access to the directories and files. When we have a small filesystem it is not a problem but when the filesystem is big then perfomance degradation becomes very apparent. What I mean by a big file system ? 1. A big filesystem is a filesystem which occupy 20-30 or more percent of total drive space, i.e. first and last cylinder are physically located relatively far from each other. 2. It has a relatively large number of cylinder groups, for example more cylinder groups than 50% of the buffers in the buffer cache. The first results in long access times, while the second results in many buffers being used by metadata operations. Such operations use cylinder group blocks and on-disk inode blocks. The cylinder group block (fs->fs_cblkno) contains struct cg, inode and block bit maps. It is 2k in size for the default filesystem parameters. If new and parent directories are located in different cylinder groups then the system performs more input/output operations and uses more buffers. On filesystems with many cylinder groups, lots of cache buffers are used for metadata operations. My solution for this problem is very simple. I allocate many directories in one cylinder group. I also do some things, so that the new allocation method does not cause excessive fragmentation and all directory inodes will not be located at a location far from its file's inodes and data. The algorithm is: /* * Find a cylinder group to place a directory. * * The policy implemented by this algorithm is to allocate a * directory inode in the same cylinder group as its parent * directory, but also to reserve space for its files inodes * and data. Restrict the number of directories which may be * allocated one after another in the same cylinder group * without intervening allocation of files. * * If we allocate a first level directory then force allocation * in another cylinder group. */ My early versions of dirpref give me a good results for a wide range of file operations and different filesystem capacities except one case: those applications that create their entire directory structure first and only later fill this structure with files. My solution for such and similar cases is to limit a number of directories which may be created one after another in the same cylinder group without intervening file creations. For this purpose, I allocate an array of counters at mount time. This array is linked to the superblock fs->fs_contigdirs[cg]. Each time a directory is created the counter increases and each time a file is created the counter decreases. A 60Gb filesystem with 8mb/cg requires 10kb of memory for the counters array. The maxcontigdirs is a maximum number of directories which may be created without an intervening file creation. I found in my tests that the best performance occurs when I restrict the number of directories in one cylinder group such that all its files may be located in the same cylinder group. There may be some deterioration in performance if all the file inodes are in the same cylinder group as its containing directory, but their data partially resides in a different cylinder group. The maxcontigdirs value is calculated to try to prevent this condition. Since there is no way to know how many files and directories will be allocated later I added two optimization parameters in superblock/tunefs. They are: int32_t fs_avgfilesize; /* expected average file size */ int32_t fs_avgfpdir; /* expected # of files per directory */ These parameters have reasonable defaults but may be tweeked for special uses of a filesystem. They are only necessary in rare cases like better tuning a filesystem being used to store a squid cache. I have been using this algorithm for about 3 months. I have done a lot of testing on filesystems with different capacities, average filesize, average number of files per directory, and so on. I think this algorithm has no negative impact on filesystem perfomance. It works better than the default one in all cases. The new dirpref will greatly improve untarring/removing/coping of big directories, decrease load on cvs servers and much more. The new dirpref doesn't speedup a compilation process, but also doesn't slow it down. Obtained from: Grigoriy Orlov <gluk@ptci.ru> ===== ===== iedowse 2001/04/23 17:37:17 PDT Pre-dirpref versions of fsck may zero out the new superblock fields fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause panics if these fields were zeroed while a filesystem was mounted read-only, and then remounted read-write. Add code to ffs_reload() which copies the fs_contigdirs pointer from the previous superblock, and reinitialises fs_avgf* if necessary. Reviewed by: mckusick ===== ===== nik 2001/04/10 03:36:44 PDT Add information about the new options to newfs and tunefs which set the expected average file size and number of files per directory. Could do with some fleshing out. =====
2001-09-06 06:16:00 +04:00
case 'g':
avgfilesize = strsuftoll("average file size", optarg,
1, INT_MAX);
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in FreeBSD (three commits; the initial work, man page updates, and a fix to ffs_reload()), with the following differences: - Be consistent between newfs(8) and tunefs(8) as to the options which set and control the tuning parameters for this work (avgfilesize & avgfpdir) - Use u_int16_t instead of u_int8_t to keep track of the number of contiguous directories (suggested by Chuck Silvers) - Work within our FFS_EI framework - Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to the same area of memory The new algorithm has a marked performance increase, especially when performing tasks such as untarring pkgsrc.tar.gz, etc. The original FreeBSD commit messages are attached: ===== mckusick 2001/04/10 01:39:00 PDT Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>. His description of the problem and solution follow. My own tests show speedups on typical filesystem intensive workloads of 5% to 12% which is very impressive considering the small amount of code change involved. ------ One day I noticed that some file operations run much faster on small file systems then on big ones. I've looked at the ffs algorithms, thought about them, and redesigned the dirpref algorithm. First I want to describe the results of my tests. These results are old and I have improved the algorithm after these tests were done. Nevertheless they show how big the perfomance speedup may be. I have done two file/directory intensive tests on a two OpenBSD systems with old and new dirpref algorithm. The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports". The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release. It contains 6596 directories and 13868 files. The test systems are: 1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for test is at wd1. Size of test file system is 8 Gb, number of cg=991, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=35 2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system at wd0, file system for test is at wd1. Size of test file system is 40 Gb, number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50 You can get more info about the test systems and methods at: http://www.ptci.ru/gluk/dirpref/old/dirpref.html Test Results tar -xzf ports.tar.gz rm -rf ports mode old dirpref new dirpref speedup old dirprefnew dirpref speedup First system normal 667 472 1.41 477 331 1.44 async 285 144 1.98 130 14 9.29 sync 768 616 1.25 477 334 1.43 softdep 413 252 1.64 241 38 6.34 Second system normal 329 81 4.06 263.5 93.5 2.81 async 302 25.7 11.75 112 2.26 49.56 sync 281 57.0 4.93 263 90.5 2.9 softdep 341 40.6 8.4 284 4.76 59.66 "old dirpref" and "new dirpref" columns give a test time in seconds. speedup - speed increasement in times, ie. old dirpref / new dirpref. ------ Algorithm description The old dirpref algorithm is described in comments: /* * Find a cylinder to place a directory. * * The policy implemented by this algorithm is to select from * among those cylinder groups with above the average number of * free inodes, the one with the smallest number of directories. */ A new directory is allocated in a different cylinder groups than its parent directory resulting in a directory tree that is spreaded across all the cylinder groups. This spreading out results in a non-optimal access to the directories and files. When we have a small filesystem it is not a problem but when the filesystem is big then perfomance degradation becomes very apparent. What I mean by a big file system ? 1. A big filesystem is a filesystem which occupy 20-30 or more percent of total drive space, i.e. first and last cylinder are physically located relatively far from each other. 2. It has a relatively large number of cylinder groups, for example more cylinder groups than 50% of the buffers in the buffer cache. The first results in long access times, while the second results in many buffers being used by metadata operations. Such operations use cylinder group blocks and on-disk inode blocks. The cylinder group block (fs->fs_cblkno) contains struct cg, inode and block bit maps. It is 2k in size for the default filesystem parameters. If new and parent directories are located in different cylinder groups then the system performs more input/output operations and uses more buffers. On filesystems with many cylinder groups, lots of cache buffers are used for metadata operations. My solution for this problem is very simple. I allocate many directories in one cylinder group. I also do some things, so that the new allocation method does not cause excessive fragmentation and all directory inodes will not be located at a location far from its file's inodes and data. The algorithm is: /* * Find a cylinder group to place a directory. * * The policy implemented by this algorithm is to allocate a * directory inode in the same cylinder group as its parent * directory, but also to reserve space for its files inodes * and data. Restrict the number of directories which may be * allocated one after another in the same cylinder group * without intervening allocation of files. * * If we allocate a first level directory then force allocation * in another cylinder group. */ My early versions of dirpref give me a good results for a wide range of file operations and different filesystem capacities except one case: those applications that create their entire directory structure first and only later fill this structure with files. My solution for such and similar cases is to limit a number of directories which may be created one after another in the same cylinder group without intervening file creations. For this purpose, I allocate an array of counters at mount time. This array is linked to the superblock fs->fs_contigdirs[cg]. Each time a directory is created the counter increases and each time a file is created the counter decreases. A 60Gb filesystem with 8mb/cg requires 10kb of memory for the counters array. The maxcontigdirs is a maximum number of directories which may be created without an intervening file creation. I found in my tests that the best performance occurs when I restrict the number of directories in one cylinder group such that all its files may be located in the same cylinder group. There may be some deterioration in performance if all the file inodes are in the same cylinder group as its containing directory, but their data partially resides in a different cylinder group. The maxcontigdirs value is calculated to try to prevent this condition. Since there is no way to know how many files and directories will be allocated later I added two optimization parameters in superblock/tunefs. They are: int32_t fs_avgfilesize; /* expected average file size */ int32_t fs_avgfpdir; /* expected # of files per directory */ These parameters have reasonable defaults but may be tweeked for special uses of a filesystem. They are only necessary in rare cases like better tuning a filesystem being used to store a squid cache. I have been using this algorithm for about 3 months. I have done a lot of testing on filesystems with different capacities, average filesize, average number of files per directory, and so on. I think this algorithm has no negative impact on filesystem perfomance. It works better than the default one in all cases. The new dirpref will greatly improve untarring/removing/coping of big directories, decrease load on cvs servers and much more. The new dirpref doesn't speedup a compilation process, but also doesn't slow it down. Obtained from: Grigoriy Orlov <gluk@ptci.ru> ===== ===== iedowse 2001/04/23 17:37:17 PDT Pre-dirpref versions of fsck may zero out the new superblock fields fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause panics if these fields were zeroed while a filesystem was mounted read-only, and then remounted read-write. Add code to ffs_reload() which copies the fs_contigdirs pointer from the previous superblock, and reinitialises fs_avgf* if necessary. Reviewed by: mckusick ===== ===== nik 2001/04/10 03:36:44 PDT Add information about the new options to newfs and tunefs which set the expected average file size and number of files per directory. Could do with some fleshing out. =====
2001-09-06 06:16:00 +04:00
break;
case 'h':
avgfpdir = strsuftoll(
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in FreeBSD (three commits; the initial work, man page updates, and a fix to ffs_reload()), with the following differences: - Be consistent between newfs(8) and tunefs(8) as to the options which set and control the tuning parameters for this work (avgfilesize & avgfpdir) - Use u_int16_t instead of u_int8_t to keep track of the number of contiguous directories (suggested by Chuck Silvers) - Work within our FFS_EI framework - Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to the same area of memory The new algorithm has a marked performance increase, especially when performing tasks such as untarring pkgsrc.tar.gz, etc. The original FreeBSD commit messages are attached: ===== mckusick 2001/04/10 01:39:00 PDT Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>. His description of the problem and solution follow. My own tests show speedups on typical filesystem intensive workloads of 5% to 12% which is very impressive considering the small amount of code change involved. ------ One day I noticed that some file operations run much faster on small file systems then on big ones. I've looked at the ffs algorithms, thought about them, and redesigned the dirpref algorithm. First I want to describe the results of my tests. These results are old and I have improved the algorithm after these tests were done. Nevertheless they show how big the perfomance speedup may be. I have done two file/directory intensive tests on a two OpenBSD systems with old and new dirpref algorithm. The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports". The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release. It contains 6596 directories and 13868 files. The test systems are: 1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for test is at wd1. Size of test file system is 8 Gb, number of cg=991, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=35 2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system at wd0, file system for test is at wd1. Size of test file system is 40 Gb, number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50 You can get more info about the test systems and methods at: http://www.ptci.ru/gluk/dirpref/old/dirpref.html Test Results tar -xzf ports.tar.gz rm -rf ports mode old dirpref new dirpref speedup old dirprefnew dirpref speedup First system normal 667 472 1.41 477 331 1.44 async 285 144 1.98 130 14 9.29 sync 768 616 1.25 477 334 1.43 softdep 413 252 1.64 241 38 6.34 Second system normal 329 81 4.06 263.5 93.5 2.81 async 302 25.7 11.75 112 2.26 49.56 sync 281 57.0 4.93 263 90.5 2.9 softdep 341 40.6 8.4 284 4.76 59.66 "old dirpref" and "new dirpref" columns give a test time in seconds. speedup - speed increasement in times, ie. old dirpref / new dirpref. ------ Algorithm description The old dirpref algorithm is described in comments: /* * Find a cylinder to place a directory. * * The policy implemented by this algorithm is to select from * among those cylinder groups with above the average number of * free inodes, the one with the smallest number of directories. */ A new directory is allocated in a different cylinder groups than its parent directory resulting in a directory tree that is spreaded across all the cylinder groups. This spreading out results in a non-optimal access to the directories and files. When we have a small filesystem it is not a problem but when the filesystem is big then perfomance degradation becomes very apparent. What I mean by a big file system ? 1. A big filesystem is a filesystem which occupy 20-30 or more percent of total drive space, i.e. first and last cylinder are physically located relatively far from each other. 2. It has a relatively large number of cylinder groups, for example more cylinder groups than 50% of the buffers in the buffer cache. The first results in long access times, while the second results in many buffers being used by metadata operations. Such operations use cylinder group blocks and on-disk inode blocks. The cylinder group block (fs->fs_cblkno) contains struct cg, inode and block bit maps. It is 2k in size for the default filesystem parameters. If new and parent directories are located in different cylinder groups then the system performs more input/output operations and uses more buffers. On filesystems with many cylinder groups, lots of cache buffers are used for metadata operations. My solution for this problem is very simple. I allocate many directories in one cylinder group. I also do some things, so that the new allocation method does not cause excessive fragmentation and all directory inodes will not be located at a location far from its file's inodes and data. The algorithm is: /* * Find a cylinder group to place a directory. * * The policy implemented by this algorithm is to allocate a * directory inode in the same cylinder group as its parent * directory, but also to reserve space for its files inodes * and data. Restrict the number of directories which may be * allocated one after another in the same cylinder group * without intervening allocation of files. * * If we allocate a first level directory then force allocation * in another cylinder group. */ My early versions of dirpref give me a good results for a wide range of file operations and different filesystem capacities except one case: those applications that create their entire directory structure first and only later fill this structure with files. My solution for such and similar cases is to limit a number of directories which may be created one after another in the same cylinder group without intervening file creations. For this purpose, I allocate an array of counters at mount time. This array is linked to the superblock fs->fs_contigdirs[cg]. Each time a directory is created the counter increases and each time a file is created the counter decreases. A 60Gb filesystem with 8mb/cg requires 10kb of memory for the counters array. The maxcontigdirs is a maximum number of directories which may be created without an intervening file creation. I found in my tests that the best performance occurs when I restrict the number of directories in one cylinder group such that all its files may be located in the same cylinder group. There may be some deterioration in performance if all the file inodes are in the same cylinder group as its containing directory, but their data partially resides in a different cylinder group. The maxcontigdirs value is calculated to try to prevent this condition. Since there is no way to know how many files and directories will be allocated later I added two optimization parameters in superblock/tunefs. They are: int32_t fs_avgfilesize; /* expected average file size */ int32_t fs_avgfpdir; /* expected # of files per directory */ These parameters have reasonable defaults but may be tweeked for special uses of a filesystem. They are only necessary in rare cases like better tuning a filesystem being used to store a squid cache. I have been using this algorithm for about 3 months. I have done a lot of testing on filesystems with different capacities, average filesize, average number of files per directory, and so on. I think this algorithm has no negative impact on filesystem perfomance. It works better than the default one in all cases. The new dirpref will greatly improve untarring/removing/coping of big directories, decrease load on cvs servers and much more. The new dirpref doesn't speedup a compilation process, but also doesn't slow it down. Obtained from: Grigoriy Orlov <gluk@ptci.ru> ===== ===== iedowse 2001/04/23 17:37:17 PDT Pre-dirpref versions of fsck may zero out the new superblock fields fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause panics if these fields were zeroed while a filesystem was mounted read-only, and then remounted read-write. Add code to ffs_reload() which copies the fs_contigdirs pointer from the previous superblock, and reinitialises fs_avgf* if necessary. Reviewed by: mckusick ===== ===== nik 2001/04/10 03:36:44 PDT Add information about the new options to newfs and tunefs which set the expected average file size and number of files per directory. Could do with some fleshing out. =====
2001-09-06 06:16:00 +04:00
"expected number of files per directory",
optarg, 1, INT_MAX);
break;
case 'l':
logfilesize = strsuftoll("journal log file size",
optarg, 0, INT_MAX);
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in FreeBSD (three commits; the initial work, man page updates, and a fix to ffs_reload()), with the following differences: - Be consistent between newfs(8) and tunefs(8) as to the options which set and control the tuning parameters for this work (avgfilesize & avgfpdir) - Use u_int16_t instead of u_int8_t to keep track of the number of contiguous directories (suggested by Chuck Silvers) - Work within our FFS_EI framework - Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to the same area of memory The new algorithm has a marked performance increase, especially when performing tasks such as untarring pkgsrc.tar.gz, etc. The original FreeBSD commit messages are attached: ===== mckusick 2001/04/10 01:39:00 PDT Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>. His description of the problem and solution follow. My own tests show speedups on typical filesystem intensive workloads of 5% to 12% which is very impressive considering the small amount of code change involved. ------ One day I noticed that some file operations run much faster on small file systems then on big ones. I've looked at the ffs algorithms, thought about them, and redesigned the dirpref algorithm. First I want to describe the results of my tests. These results are old and I have improved the algorithm after these tests were done. Nevertheless they show how big the perfomance speedup may be. I have done two file/directory intensive tests on a two OpenBSD systems with old and new dirpref algorithm. The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports". The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release. It contains 6596 directories and 13868 files. The test systems are: 1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for test is at wd1. Size of test file system is 8 Gb, number of cg=991, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=35 2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system at wd0, file system for test is at wd1. Size of test file system is 40 Gb, number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50 You can get more info about the test systems and methods at: http://www.ptci.ru/gluk/dirpref/old/dirpref.html Test Results tar -xzf ports.tar.gz rm -rf ports mode old dirpref new dirpref speedup old dirprefnew dirpref speedup First system normal 667 472 1.41 477 331 1.44 async 285 144 1.98 130 14 9.29 sync 768 616 1.25 477 334 1.43 softdep 413 252 1.64 241 38 6.34 Second system normal 329 81 4.06 263.5 93.5 2.81 async 302 25.7 11.75 112 2.26 49.56 sync 281 57.0 4.93 263 90.5 2.9 softdep 341 40.6 8.4 284 4.76 59.66 "old dirpref" and "new dirpref" columns give a test time in seconds. speedup - speed increasement in times, ie. old dirpref / new dirpref. ------ Algorithm description The old dirpref algorithm is described in comments: /* * Find a cylinder to place a directory. * * The policy implemented by this algorithm is to select from * among those cylinder groups with above the average number of * free inodes, the one with the smallest number of directories. */ A new directory is allocated in a different cylinder groups than its parent directory resulting in a directory tree that is spreaded across all the cylinder groups. This spreading out results in a non-optimal access to the directories and files. When we have a small filesystem it is not a problem but when the filesystem is big then perfomance degradation becomes very apparent. What I mean by a big file system ? 1. A big filesystem is a filesystem which occupy 20-30 or more percent of total drive space, i.e. first and last cylinder are physically located relatively far from each other. 2. It has a relatively large number of cylinder groups, for example more cylinder groups than 50% of the buffers in the buffer cache. The first results in long access times, while the second results in many buffers being used by metadata operations. Such operations use cylinder group blocks and on-disk inode blocks. The cylinder group block (fs->fs_cblkno) contains struct cg, inode and block bit maps. It is 2k in size for the default filesystem parameters. If new and parent directories are located in different cylinder groups then the system performs more input/output operations and uses more buffers. On filesystems with many cylinder groups, lots of cache buffers are used for metadata operations. My solution for this problem is very simple. I allocate many directories in one cylinder group. I also do some things, so that the new allocation method does not cause excessive fragmentation and all directory inodes will not be located at a location far from its file's inodes and data. The algorithm is: /* * Find a cylinder group to place a directory. * * The policy implemented by this algorithm is to allocate a * directory inode in the same cylinder group as its parent * directory, but also to reserve space for its files inodes * and data. Restrict the number of directories which may be * allocated one after another in the same cylinder group * without intervening allocation of files. * * If we allocate a first level directory then force allocation * in another cylinder group. */ My early versions of dirpref give me a good results for a wide range of file operations and different filesystem capacities except one case: those applications that create their entire directory structure first and only later fill this structure with files. My solution for such and similar cases is to limit a number of directories which may be created one after another in the same cylinder group without intervening file creations. For this purpose, I allocate an array of counters at mount time. This array is linked to the superblock fs->fs_contigdirs[cg]. Each time a directory is created the counter increases and each time a file is created the counter decreases. A 60Gb filesystem with 8mb/cg requires 10kb of memory for the counters array. The maxcontigdirs is a maximum number of directories which may be created without an intervening file creation. I found in my tests that the best performance occurs when I restrict the number of directories in one cylinder group such that all its files may be located in the same cylinder group. There may be some deterioration in performance if all the file inodes are in the same cylinder group as its containing directory, but their data partially resides in a different cylinder group. The maxcontigdirs value is calculated to try to prevent this condition. Since there is no way to know how many files and directories will be allocated later I added two optimization parameters in superblock/tunefs. They are: int32_t fs_avgfilesize; /* expected average file size */ int32_t fs_avgfpdir; /* expected # of files per directory */ These parameters have reasonable defaults but may be tweeked for special uses of a filesystem. They are only necessary in rare cases like better tuning a filesystem being used to store a squid cache. I have been using this algorithm for about 3 months. I have done a lot of testing on filesystems with different capacities, average filesize, average number of files per directory, and so on. I think this algorithm has no negative impact on filesystem perfomance. It works better than the default one in all cases. The new dirpref will greatly improve untarring/removing/coping of big directories, decrease load on cvs servers and much more. The new dirpref doesn't speedup a compilation process, but also doesn't slow it down. Obtained from: Grigoriy Orlov <gluk@ptci.ru> ===== ===== iedowse 2001/04/23 17:37:17 PDT Pre-dirpref versions of fsck may zero out the new superblock fields fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause panics if these fields were zeroed while a filesystem was mounted read-only, and then remounted read-write. Add code to ffs_reload() which copies the fs_contigdirs pointer from the previous superblock, and reinitialises fs_avgf* if necessary. Reviewed by: mckusick ===== ===== nik 2001/04/10 03:36:44 PDT Add information about the new options to newfs and tunefs which set the expected average file size and number of files per directory. Could do with some fleshing out. =====
2001-09-06 06:16:00 +04:00
break;
case 'm':
minfree = strsuftoll("minimum percentage of free space",
optarg, 0, 99);
break;
case 'o':
if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
optim = FS_OPTSPACE;
else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
optim = FS_OPTTIME;
else
errx(10,
"bad %s (options are `space' or `time')",
"optimization preference");
break;
2020-05-16 21:31:45 +03:00
case 'p':
name = "POSIX1e ACLs";
pvalue = optarg;
if (strcmp(pvalue, "enable") &&
strcmp(pvalue, "disable")) {
errx(10, "bad %s (options are %s)",
name, "`enable' or `disable'");
}
pflag = 1;
break;
case 'q':
if (strcmp(optarg, "user") == 0)
userquota = Q2_EN;
else if (strcmp(optarg, "group") == 0)
groupquota = Q2_EN;
else if (strcmp(optarg, "nouser") == 0)
userquota = Q2_DIS;
else if (strcmp(optarg, "nogroup") == 0)
groupquota = Q2_DIS;
else
errx(11, "invalid quota type %s", optarg);
break;
2020-05-16 21:31:45 +03:00
case 'S':
secsize = strsuftoll("physical sector size",
optarg, 0, INT_MAX);
secshift = ffs(secsize) - 1;
if (secsize != 0 && 1 << secshift != secsize)
errx(12, "sector size %d is not a power of two", secsize);
break;
2020-05-16 21:31:45 +03:00
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1)
1994-04-12 09:03:23 +04:00
usage();
special = argv[0];
openflags = Nflag ? O_RDONLY : O_RDWR;
if (Fflag)
fi = open(special, openflags);
else {
fi = openpartition(special, openflags, device, sizeof(device));
special = device;
1993-03-21 12:45:37 +03:00
}
if (fi == -1)
err(1, "%s", special);
active = !Fflag && isactive(fi, &sfs);
1993-03-21 12:45:37 +03:00
getsb(&sblock, special);
#define CHANGEVAL(old, new, type, suffix) do \
if ((uint32_t)(new) != (uint32_t)-1) { \
if ((new) == (old)) \
warnx("%s remains unchanged at %d%s", \
(type), (old), (suffix)); \
else { \
warnx("%s changes from %d%s to %d%s", \
(type), (old), (suffix), (new), (suffix)); \
(old) = (new); \
} \
} while (/* CONSTCOND */0)
warnx("tuning %s", special);
CHANGEVAL(sblock.fs_maxbpg, maxbpg,
"maximum blocks per file in a cylinder group", "");
CHANGEVAL(sblock.fs_minfree, minfree,
"minimum percentage of free space", "%");
if (minfree != -1) {
if (minfree >= MINFREE &&
sblock.fs_optim == FS_OPTSPACE)
warnx(OPTWARN, "time", ">=", MINFREE);
if (minfree < MINFREE &&
sblock.fs_optim == FS_OPTTIME)
warnx(OPTWARN, "space", "<", MINFREE);
}
if (optim != -1) {
if (sblock.fs_optim == optim) {
warnx("%s remains unchanged as %s",
"optimization preference",
chg[optim]);
} else {
warnx("%s changes from %s to %s",
"optimization preference",
chg[sblock.fs_optim], chg[optim]);
sblock.fs_optim = optim;
if (sblock.fs_minfree >= MINFREE &&
optim == FS_OPTSPACE)
warnx(OPTWARN, "time", ">=", MINFREE);
if (sblock.fs_minfree < MINFREE &&
optim == FS_OPTTIME)
warnx(OPTWARN, "space", "<", MINFREE);
}
1993-03-21 12:45:37 +03:00
}
if (secsize != -1) {
if (secsize == 0) {
secsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1);
secshift = ffs(secsize) - 1;
}
if (secshift < DEV_BSHIFT)
warnx("sector size must be at least %d", DEV_BSIZE);
else if (secshift > sblock.fs_fshift)
warnx("sector size %d cannot be larger than fragment size %d",
secsize, sblock.fs_fsize);
else {
fsbtodb = sblock.fs_fshift - secshift;
if (fsbtodb == sblock.fs_fsbtodb) {
warnx("sector size remains unchanged as %d",
sblock.fs_fsize / FFS_FSBTODB(&sblock, 1));
} else {
warnx("sector size changed from %d to %d",
sblock.fs_fsize / FFS_FSBTODB(&sblock, 1),
secsize);
sblock.fs_fsbtodb = fsbtodb;
}
}
}
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in FreeBSD (three commits; the initial work, man page updates, and a fix to ffs_reload()), with the following differences: - Be consistent between newfs(8) and tunefs(8) as to the options which set and control the tuning parameters for this work (avgfilesize & avgfpdir) - Use u_int16_t instead of u_int8_t to keep track of the number of contiguous directories (suggested by Chuck Silvers) - Work within our FFS_EI framework - Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to the same area of memory The new algorithm has a marked performance increase, especially when performing tasks such as untarring pkgsrc.tar.gz, etc. The original FreeBSD commit messages are attached: ===== mckusick 2001/04/10 01:39:00 PDT Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>. His description of the problem and solution follow. My own tests show speedups on typical filesystem intensive workloads of 5% to 12% which is very impressive considering the small amount of code change involved. ------ One day I noticed that some file operations run much faster on small file systems then on big ones. I've looked at the ffs algorithms, thought about them, and redesigned the dirpref algorithm. First I want to describe the results of my tests. These results are old and I have improved the algorithm after these tests were done. Nevertheless they show how big the perfomance speedup may be. I have done two file/directory intensive tests on a two OpenBSD systems with old and new dirpref algorithm. The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports". The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release. It contains 6596 directories and 13868 files. The test systems are: 1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for test is at wd1. Size of test file system is 8 Gb, number of cg=991, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=35 2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system at wd0, file system for test is at wd1. Size of test file system is 40 Gb, number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50 You can get more info about the test systems and methods at: http://www.ptci.ru/gluk/dirpref/old/dirpref.html Test Results tar -xzf ports.tar.gz rm -rf ports mode old dirpref new dirpref speedup old dirprefnew dirpref speedup First system normal 667 472 1.41 477 331 1.44 async 285 144 1.98 130 14 9.29 sync 768 616 1.25 477 334 1.43 softdep 413 252 1.64 241 38 6.34 Second system normal 329 81 4.06 263.5 93.5 2.81 async 302 25.7 11.75 112 2.26 49.56 sync 281 57.0 4.93 263 90.5 2.9 softdep 341 40.6 8.4 284 4.76 59.66 "old dirpref" and "new dirpref" columns give a test time in seconds. speedup - speed increasement in times, ie. old dirpref / new dirpref. ------ Algorithm description The old dirpref algorithm is described in comments: /* * Find a cylinder to place a directory. * * The policy implemented by this algorithm is to select from * among those cylinder groups with above the average number of * free inodes, the one with the smallest number of directories. */ A new directory is allocated in a different cylinder groups than its parent directory resulting in a directory tree that is spreaded across all the cylinder groups. This spreading out results in a non-optimal access to the directories and files. When we have a small filesystem it is not a problem but when the filesystem is big then perfomance degradation becomes very apparent. What I mean by a big file system ? 1. A big filesystem is a filesystem which occupy 20-30 or more percent of total drive space, i.e. first and last cylinder are physically located relatively far from each other. 2. It has a relatively large number of cylinder groups, for example more cylinder groups than 50% of the buffers in the buffer cache. The first results in long access times, while the second results in many buffers being used by metadata operations. Such operations use cylinder group blocks and on-disk inode blocks. The cylinder group block (fs->fs_cblkno) contains struct cg, inode and block bit maps. It is 2k in size for the default filesystem parameters. If new and parent directories are located in different cylinder groups then the system performs more input/output operations and uses more buffers. On filesystems with many cylinder groups, lots of cache buffers are used for metadata operations. My solution for this problem is very simple. I allocate many directories in one cylinder group. I also do some things, so that the new allocation method does not cause excessive fragmentation and all directory inodes will not be located at a location far from its file's inodes and data. The algorithm is: /* * Find a cylinder group to place a directory. * * The policy implemented by this algorithm is to allocate a * directory inode in the same cylinder group as its parent * directory, but also to reserve space for its files inodes * and data. Restrict the number of directories which may be * allocated one after another in the same cylinder group * without intervening allocation of files. * * If we allocate a first level directory then force allocation * in another cylinder group. */ My early versions of dirpref give me a good results for a wide range of file operations and different filesystem capacities except one case: those applications that create their entire directory structure first and only later fill this structure with files. My solution for such and similar cases is to limit a number of directories which may be created one after another in the same cylinder group without intervening file creations. For this purpose, I allocate an array of counters at mount time. This array is linked to the superblock fs->fs_contigdirs[cg]. Each time a directory is created the counter increases and each time a file is created the counter decreases. A 60Gb filesystem with 8mb/cg requires 10kb of memory for the counters array. The maxcontigdirs is a maximum number of directories which may be created without an intervening file creation. I found in my tests that the best performance occurs when I restrict the number of directories in one cylinder group such that all its files may be located in the same cylinder group. There may be some deterioration in performance if all the file inodes are in the same cylinder group as its containing directory, but their data partially resides in a different cylinder group. The maxcontigdirs value is calculated to try to prevent this condition. Since there is no way to know how many files and directories will be allocated later I added two optimization parameters in superblock/tunefs. They are: int32_t fs_avgfilesize; /* expected average file size */ int32_t fs_avgfpdir; /* expected # of files per directory */ These parameters have reasonable defaults but may be tweeked for special uses of a filesystem. They are only necessary in rare cases like better tuning a filesystem being used to store a squid cache. I have been using this algorithm for about 3 months. I have done a lot of testing on filesystems with different capacities, average filesize, average number of files per directory, and so on. I think this algorithm has no negative impact on filesystem perfomance. It works better than the default one in all cases. The new dirpref will greatly improve untarring/removing/coping of big directories, decrease load on cvs servers and much more. The new dirpref doesn't speedup a compilation process, but also doesn't slow it down. Obtained from: Grigoriy Orlov <gluk@ptci.ru> ===== ===== iedowse 2001/04/23 17:37:17 PDT Pre-dirpref versions of fsck may zero out the new superblock fields fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause panics if these fields were zeroed while a filesystem was mounted read-only, and then remounted read-write. Add code to ffs_reload() which copies the fs_contigdirs pointer from the previous superblock, and reinitialises fs_avgf* if necessary. Reviewed by: mckusick ===== ===== nik 2001/04/10 03:36:44 PDT Add information about the new options to newfs and tunefs which set the expected average file size and number of files per directory. Could do with some fleshing out. =====
2001-09-06 06:16:00 +04:00
CHANGEVAL(sblock.fs_avgfilesize, avgfilesize,
"average file size", "");
CHANGEVAL(sblock.fs_avgfpdir, avgfpdir,
"expected number of files per directory", "");
if (logfilesize >= 0)
change_log_info(logfilesize);
if (userquota == Q2_EN || groupquota == Q2_EN)
sblock.fs_flags |= FS_DOQUOTA2;
if (sblock.fs_flags & FS_DOQUOTA2) {
sblock.fs_quota_magic = Q2_HEAD_MAGIC;
switch(userquota) {
case Q2_EN:
if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA))
== 0) {
printf("enabling user quotas\n");
sblock.fs_quota_flags |=
FS_Q2_DO_TYPE(USRQUOTA);
sblock.fs_quotafile[USRQUOTA] = 0;
}
break;
case Q2_DIS:
if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA))
!= 0) {
printf("disabling user quotas\n");
sblock.fs_quota_flags &=
~FS_Q2_DO_TYPE(USRQUOTA);
}
}
switch(groupquota) {
case Q2_EN:
if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
== 0) {
printf("enabling group quotas\n");
sblock.fs_quota_flags |=
FS_Q2_DO_TYPE(GRPQUOTA);
sblock.fs_quotafile[GRPQUOTA] = 0;
}
break;
case Q2_DIS:
if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
!= 0) {
printf("disabling group quotas\n");
sblock.fs_quota_flags &=
~FS_Q2_DO_TYPE(GRPQUOTA);
}
}
}
/*
* if we disabled all quotas, FS_DOQUOTA2 and associated inode(s) will
* be cleared by kernel or fsck.
*/
2020-05-16 21:31:45 +03:00
if (aflag) {
name = "NFSv4 ACLs";
2020-05-16 21:31:45 +03:00
if (strcmp(avalue, "enable") == 0) {
if (is_ufs2 && !extattr) {
warnx("%s not supported by this fs", name);
} else if (sblock.fs_flags & FS_NFS4ACLS) {
2020-05-16 21:31:45 +03:00
warnx("%s remains unchanged as enabled", name);
} else if (sblock.fs_flags & FS_POSIX1EACLS) {
warnx("%s and POSIX.1e ACLs are mutually "
"exclusive", name);
} else {
sblock.fs_flags |= FS_NFS4ACLS;
2020-05-16 21:31:45 +03:00
printf("%s set\n", name);
}
} else if (strcmp(avalue, "disable") == 0) {
if ((~sblock.fs_flags & FS_NFS4ACLS) == FS_NFS4ACLS) {
2020-05-16 21:31:45 +03:00
warnx("%s remains unchanged as disabled",
name);
} else {
sblock.fs_flags &= ~FS_NFS4ACLS;
2020-05-16 21:31:45 +03:00
printf("%s cleared\n", name);
}
}
}
if (pflag) {
name = "POSIX1e ACLs";
if (strcmp(pvalue, "enable") == 0) {
if (is_ufs2 && !extattr) {
warnx("%s not supported by this fs", name);
} else if (sblock.fs_flags & FS_POSIX1EACLS) {
2020-05-16 21:31:45 +03:00
warnx("%s remains unchanged as enabled", name);
} else if (sblock.fs_flags & FS_NFS4ACLS) {
2020-05-16 21:31:45 +03:00
warnx("%s and ACLs are mutually "
"exclusive", name);
} else {
sblock.fs_flags |= FS_POSIX1EACLS;
printf("%s set\n", name);
2020-05-16 21:31:45 +03:00
}
} else if (strcmp(pvalue, "disable") == 0) {
if ((~sblock.fs_flags & FS_POSIX1EACLS) ==
FS_POSIX1EACLS) {
warnx("%s remains unchanged as disabled",
name);
} else {
sblock.fs_flags &= ~FS_POSIX1EACLS;
printf("%s cleared\n", name);
2020-05-16 21:31:45 +03:00
}
}
}
1997-09-16 16:54:42 +04:00
if (Nflag) {
2020-05-16 21:31:45 +03:00
printf("%s: current settings of %s\n", getprogname(), special);
printf("\tmaximum contiguous block count %d\n",
1997-09-16 16:54:42 +04:00
sblock.fs_maxcontig);
printf("\tmaximum blocks per file in a cylinder group %d\n",
1997-09-16 16:54:42 +04:00
sblock.fs_maxbpg);
printf("\tminimum percentage of free space %d%%\n",
1997-09-16 16:54:42 +04:00
sblock.fs_minfree);
printf("\toptimization preference: %s\n", chg[sblock.fs_optim]);
printf("\taverage file size: %d\n", sblock.fs_avgfilesize);
printf("\texpected number of files per directory: %d\n",
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in FreeBSD (three commits; the initial work, man page updates, and a fix to ffs_reload()), with the following differences: - Be consistent between newfs(8) and tunefs(8) as to the options which set and control the tuning parameters for this work (avgfilesize & avgfpdir) - Use u_int16_t instead of u_int8_t to keep track of the number of contiguous directories (suggested by Chuck Silvers) - Work within our FFS_EI framework - Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to the same area of memory The new algorithm has a marked performance increase, especially when performing tasks such as untarring pkgsrc.tar.gz, etc. The original FreeBSD commit messages are attached: ===== mckusick 2001/04/10 01:39:00 PDT Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>. His description of the problem and solution follow. My own tests show speedups on typical filesystem intensive workloads of 5% to 12% which is very impressive considering the small amount of code change involved. ------ One day I noticed that some file operations run much faster on small file systems then on big ones. I've looked at the ffs algorithms, thought about them, and redesigned the dirpref algorithm. First I want to describe the results of my tests. These results are old and I have improved the algorithm after these tests were done. Nevertheless they show how big the perfomance speedup may be. I have done two file/directory intensive tests on a two OpenBSD systems with old and new dirpref algorithm. The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports". The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release. It contains 6596 directories and 13868 files. The test systems are: 1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for test is at wd1. Size of test file system is 8 Gb, number of cg=991, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=35 2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system at wd0, file system for test is at wd1. Size of test file system is 40 Gb, number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50 You can get more info about the test systems and methods at: http://www.ptci.ru/gluk/dirpref/old/dirpref.html Test Results tar -xzf ports.tar.gz rm -rf ports mode old dirpref new dirpref speedup old dirprefnew dirpref speedup First system normal 667 472 1.41 477 331 1.44 async 285 144 1.98 130 14 9.29 sync 768 616 1.25 477 334 1.43 softdep 413 252 1.64 241 38 6.34 Second system normal 329 81 4.06 263.5 93.5 2.81 async 302 25.7 11.75 112 2.26 49.56 sync 281 57.0 4.93 263 90.5 2.9 softdep 341 40.6 8.4 284 4.76 59.66 "old dirpref" and "new dirpref" columns give a test time in seconds. speedup - speed increasement in times, ie. old dirpref / new dirpref. ------ Algorithm description The old dirpref algorithm is described in comments: /* * Find a cylinder to place a directory. * * The policy implemented by this algorithm is to select from * among those cylinder groups with above the average number of * free inodes, the one with the smallest number of directories. */ A new directory is allocated in a different cylinder groups than its parent directory resulting in a directory tree that is spreaded across all the cylinder groups. This spreading out results in a non-optimal access to the directories and files. When we have a small filesystem it is not a problem but when the filesystem is big then perfomance degradation becomes very apparent. What I mean by a big file system ? 1. A big filesystem is a filesystem which occupy 20-30 or more percent of total drive space, i.e. first and last cylinder are physically located relatively far from each other. 2. It has a relatively large number of cylinder groups, for example more cylinder groups than 50% of the buffers in the buffer cache. The first results in long access times, while the second results in many buffers being used by metadata operations. Such operations use cylinder group blocks and on-disk inode blocks. The cylinder group block (fs->fs_cblkno) contains struct cg, inode and block bit maps. It is 2k in size for the default filesystem parameters. If new and parent directories are located in different cylinder groups then the system performs more input/output operations and uses more buffers. On filesystems with many cylinder groups, lots of cache buffers are used for metadata operations. My solution for this problem is very simple. I allocate many directories in one cylinder group. I also do some things, so that the new allocation method does not cause excessive fragmentation and all directory inodes will not be located at a location far from its file's inodes and data. The algorithm is: /* * Find a cylinder group to place a directory. * * The policy implemented by this algorithm is to allocate a * directory inode in the same cylinder group as its parent * directory, but also to reserve space for its files inodes * and data. Restrict the number of directories which may be * allocated one after another in the same cylinder group * without intervening allocation of files. * * If we allocate a first level directory then force allocation * in another cylinder group. */ My early versions of dirpref give me a good results for a wide range of file operations and different filesystem capacities except one case: those applications that create their entire directory structure first and only later fill this structure with files. My solution for such and similar cases is to limit a number of directories which may be created one after another in the same cylinder group without intervening file creations. For this purpose, I allocate an array of counters at mount time. This array is linked to the superblock fs->fs_contigdirs[cg]. Each time a directory is created the counter increases and each time a file is created the counter decreases. A 60Gb filesystem with 8mb/cg requires 10kb of memory for the counters array. The maxcontigdirs is a maximum number of directories which may be created without an intervening file creation. I found in my tests that the best performance occurs when I restrict the number of directories in one cylinder group such that all its files may be located in the same cylinder group. There may be some deterioration in performance if all the file inodes are in the same cylinder group as its containing directory, but their data partially resides in a different cylinder group. The maxcontigdirs value is calculated to try to prevent this condition. Since there is no way to know how many files and directories will be allocated later I added two optimization parameters in superblock/tunefs. They are: int32_t fs_avgfilesize; /* expected average file size */ int32_t fs_avgfpdir; /* expected # of files per directory */ These parameters have reasonable defaults but may be tweeked for special uses of a filesystem. They are only necessary in rare cases like better tuning a filesystem being used to store a squid cache. I have been using this algorithm for about 3 months. I have done a lot of testing on filesystems with different capacities, average filesize, average number of files per directory, and so on. I think this algorithm has no negative impact on filesystem perfomance. It works better than the default one in all cases. The new dirpref will greatly improve untarring/removing/coping of big directories, decrease load on cvs servers and much more. The new dirpref doesn't speedup a compilation process, but also doesn't slow it down. Obtained from: Grigoriy Orlov <gluk@ptci.ru> ===== ===== iedowse 2001/04/23 17:37:17 PDT Pre-dirpref versions of fsck may zero out the new superblock fields fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause panics if these fields were zeroed while a filesystem was mounted read-only, and then remounted read-write. Add code to ffs_reload() which copies the fs_contigdirs pointer from the previous superblock, and reinitialises fs_avgf* if necessary. Reviewed by: mckusick ===== ===== nik 2001/04/10 03:36:44 PDT Add information about the new options to newfs and tunefs which set the expected average file size and number of files per directory. Could do with some fleshing out. =====
2001-09-06 06:16:00 +04:00
sblock.fs_avgfpdir);
show_log_info();
printf("\tquotas");
if (sblock.fs_flags & FS_DOQUOTA2) {
if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA)) {
printf(" user");
if (sblock.fs_quota_flags &
FS_Q2_DO_TYPE(GRPQUOTA))
printf(",");
}
if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
printf(" group");
printf(" enabled\n");
} else {
printf(" disabled\n");
}
2020-05-16 21:31:45 +03:00
printf("\tPOSIX.1e ACLs %s\n",
(sblock.fs_flags & FS_POSIX1EACLS) ? "enabled" : "disabled");
printf("\tNFS4 ACLs %s\n",
(sblock.fs_flags & FS_NFS4ACLS) ? "enabled" : "disabled");
2020-05-16 21:31:45 +03:00
printf("%s: no changes made\n", getprogname());
return 0;
1997-09-16 16:54:42 +04:00
}
memcpy(&buf, (char *)&sblock, SBLOCKSIZE);
if (needswap)
ffs_sb_swap((struct fs*)&buf, (struct fs*)&buf);
/* write superblock to original coordinates (use old dev_bsize!) */
bwrite(sblockloc, buf.data, SBLOCKSIZE, special);
if (active) {
struct ufs_args args;
args.fspec = sfs.f_mntfromname;
if (mount(MOUNT_FFS, sfs.f_mntonname, sfs.f_flag | MNT_UPDATE,
&args, sizeof args) == -1)
warn("mount");
else
printf("%s: mount of %s on %s updated\n",
getprogname(), sfs.f_mntfromname, sfs.f_mntonname);
}
/* correct dev_bsize from possibly changed superblock data */
dev_bsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1);
1993-03-21 12:45:37 +03:00
if (Aflag)
for (i = 0; i < sblock.fs_ncg; i++)
bwrite(FFS_FSBTODB(&sblock, cgsblock(&sblock, i)),
buf.data, SBLOCKSIZE, special);
1993-03-21 12:45:37 +03:00
close(fi);
exit(0);
1994-04-12 09:03:23 +04:00
}
static int
isactive(int fd, struct statvfs *rsfs)
{
struct stat st0, st;
struct statvfs *sfs;
int n;
if (fstat(fd, &st0) == -1) {
warn("stat");
return 0;
}
if ((n = getmntinfo(&sfs, 0)) == -1) {
warn("getmntinfo");
return 0;
}
for (int i = 0; i < n; i++) {
if (stat(sfs[i].f_mntfromname, &st) == -1)
continue;
if (st.st_rdev != st0.st_rdev)
continue;
*rsfs = sfs[i];
return 1;
}
return 0;
}
static void
show_log_info(void)
{
const char *loc;
uint64_t size, blksize, logsize;
int print;
switch (sblock.fs_journal_location) {
case UFS_WAPBL_JOURNALLOC_NONE:
print = blksize = 0;
/* nothing */
break;
case UFS_WAPBL_JOURNALLOC_END_PARTITION:
loc = "end of partition";
size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT];
blksize = sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
print = 1;
break;
case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
loc = "in filesystem";
size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT];
blksize = sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
print = 1;
break;
default:
loc = "unknown";
size = blksize = 0;
print = 1;
break;
}
if (print) {
logsize = size * blksize;
printf("\tjournal log file location: %s\n", loc);
printf("\tjournal log file size: ");
if (logsize == 0)
printf("0\n");
else {
char sizebuf[8];
humanize_number(sizebuf, 6, size * blksize, "B",
HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
printf("%s (%" PRId64 " bytes)", sizebuf, logsize);
}
printf("\n");
printf("\tjournal log flags:");
if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG)
printf(" create-log");
if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG)
printf(" clear-log");
printf("\n");
}
}
static void
change_log_info(long long logfilesize)
{
/*
* NOTES:
* - only operate on in-filesystem log sizes
* - can't change size of existing log
* - if current is same, no action
* - if current is zero and new is non-zero, set flag to create log
* on next mount
* - if current is non-zero and new is zero, set flag to clear log
* on next mount
*/
int in_fs_log;
uint64_t old_size;
old_size = 0;
switch (sblock.fs_journal_location) {
case UFS_WAPBL_JOURNALLOC_END_PARTITION:
in_fs_log = 0;
old_size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT] *
sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
break;
case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
in_fs_log = 1;
old_size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] *
sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
break;
case UFS_WAPBL_JOURNALLOC_NONE:
default:
in_fs_log = 0;
old_size = 0;
break;
}
if (logfilesize == 0) {
/*
* Don't clear out the locators - the kernel might need
* these to find the log! Just set the "clear the log"
* flag and let the kernel do the rest.
*/
sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CLEAR_LOG;
sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CREATE_LOG;
warnx("log file size cleared from %" PRIu64 "", old_size);
return;
}
if (!in_fs_log && logfilesize > 0 && old_size > 0)
errx(1, "Can't change size of non-in-filesystem log");
if (old_size == (uint64_t)logfilesize && logfilesize > 0) {
/* no action */
warnx("log file size remains unchanged at %lld", logfilesize);
return;
}
if (old_size == 0) {
/* create new log of desired size next mount */
sblock.fs_journal_location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM;
sblock.fs_journallocs[UFS_WAPBL_INFS_ADDR] = 0;
sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] = logfilesize;
sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = 0;
sblock.fs_journallocs[UFS_WAPBL_INFS_INO] = 0;
sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CREATE_LOG;
sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CLEAR_LOG;
warnx("log file size set to %lld", logfilesize);
} else {
errx(1,
"Can't change existing log size from %" PRIu64 " to %lld",
old_size, logfilesize);
}
}
static void
usage(void)
1994-04-12 09:03:23 +04:00
{
fprintf(stderr, "usage: tunefs [-AFN] tuneup-options special-device\n");
1993-03-21 12:45:37 +03:00
fprintf(stderr, "where tuneup-options are:\n");
2020-05-16 21:31:45 +03:00
fprintf(stderr, "\t-a ACLS: `enable' or `disable'\n");
1993-03-21 12:45:37 +03:00
fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in FreeBSD (three commits; the initial work, man page updates, and a fix to ffs_reload()), with the following differences: - Be consistent between newfs(8) and tunefs(8) as to the options which set and control the tuning parameters for this work (avgfilesize & avgfpdir) - Use u_int16_t instead of u_int8_t to keep track of the number of contiguous directories (suggested by Chuck Silvers) - Work within our FFS_EI framework - Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to the same area of memory The new algorithm has a marked performance increase, especially when performing tasks such as untarring pkgsrc.tar.gz, etc. The original FreeBSD commit messages are attached: ===== mckusick 2001/04/10 01:39:00 PDT Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>. His description of the problem and solution follow. My own tests show speedups on typical filesystem intensive workloads of 5% to 12% which is very impressive considering the small amount of code change involved. ------ One day I noticed that some file operations run much faster on small file systems then on big ones. I've looked at the ffs algorithms, thought about them, and redesigned the dirpref algorithm. First I want to describe the results of my tests. These results are old and I have improved the algorithm after these tests were done. Nevertheless they show how big the perfomance speedup may be. I have done two file/directory intensive tests on a two OpenBSD systems with old and new dirpref algorithm. The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports". The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release. It contains 6596 directories and 13868 files. The test systems are: 1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for test is at wd1. Size of test file system is 8 Gb, number of cg=991, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=35 2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system at wd0, file system for test is at wd1. Size of test file system is 40 Gb, number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50 You can get more info about the test systems and methods at: http://www.ptci.ru/gluk/dirpref/old/dirpref.html Test Results tar -xzf ports.tar.gz rm -rf ports mode old dirpref new dirpref speedup old dirprefnew dirpref speedup First system normal 667 472 1.41 477 331 1.44 async 285 144 1.98 130 14 9.29 sync 768 616 1.25 477 334 1.43 softdep 413 252 1.64 241 38 6.34 Second system normal 329 81 4.06 263.5 93.5 2.81 async 302 25.7 11.75 112 2.26 49.56 sync 281 57.0 4.93 263 90.5 2.9 softdep 341 40.6 8.4 284 4.76 59.66 "old dirpref" and "new dirpref" columns give a test time in seconds. speedup - speed increasement in times, ie. old dirpref / new dirpref. ------ Algorithm description The old dirpref algorithm is described in comments: /* * Find a cylinder to place a directory. * * The policy implemented by this algorithm is to select from * among those cylinder groups with above the average number of * free inodes, the one with the smallest number of directories. */ A new directory is allocated in a different cylinder groups than its parent directory resulting in a directory tree that is spreaded across all the cylinder groups. This spreading out results in a non-optimal access to the directories and files. When we have a small filesystem it is not a problem but when the filesystem is big then perfomance degradation becomes very apparent. What I mean by a big file system ? 1. A big filesystem is a filesystem which occupy 20-30 or more percent of total drive space, i.e. first and last cylinder are physically located relatively far from each other. 2. It has a relatively large number of cylinder groups, for example more cylinder groups than 50% of the buffers in the buffer cache. The first results in long access times, while the second results in many buffers being used by metadata operations. Such operations use cylinder group blocks and on-disk inode blocks. The cylinder group block (fs->fs_cblkno) contains struct cg, inode and block bit maps. It is 2k in size for the default filesystem parameters. If new and parent directories are located in different cylinder groups then the system performs more input/output operations and uses more buffers. On filesystems with many cylinder groups, lots of cache buffers are used for metadata operations. My solution for this problem is very simple. I allocate many directories in one cylinder group. I also do some things, so that the new allocation method does not cause excessive fragmentation and all directory inodes will not be located at a location far from its file's inodes and data. The algorithm is: /* * Find a cylinder group to place a directory. * * The policy implemented by this algorithm is to allocate a * directory inode in the same cylinder group as its parent * directory, but also to reserve space for its files inodes * and data. Restrict the number of directories which may be * allocated one after another in the same cylinder group * without intervening allocation of files. * * If we allocate a first level directory then force allocation * in another cylinder group. */ My early versions of dirpref give me a good results for a wide range of file operations and different filesystem capacities except one case: those applications that create their entire directory structure first and only later fill this structure with files. My solution for such and similar cases is to limit a number of directories which may be created one after another in the same cylinder group without intervening file creations. For this purpose, I allocate an array of counters at mount time. This array is linked to the superblock fs->fs_contigdirs[cg]. Each time a directory is created the counter increases and each time a file is created the counter decreases. A 60Gb filesystem with 8mb/cg requires 10kb of memory for the counters array. The maxcontigdirs is a maximum number of directories which may be created without an intervening file creation. I found in my tests that the best performance occurs when I restrict the number of directories in one cylinder group such that all its files may be located in the same cylinder group. There may be some deterioration in performance if all the file inodes are in the same cylinder group as its containing directory, but their data partially resides in a different cylinder group. The maxcontigdirs value is calculated to try to prevent this condition. Since there is no way to know how many files and directories will be allocated later I added two optimization parameters in superblock/tunefs. They are: int32_t fs_avgfilesize; /* expected average file size */ int32_t fs_avgfpdir; /* expected # of files per directory */ These parameters have reasonable defaults but may be tweeked for special uses of a filesystem. They are only necessary in rare cases like better tuning a filesystem being used to store a squid cache. I have been using this algorithm for about 3 months. I have done a lot of testing on filesystems with different capacities, average filesize, average number of files per directory, and so on. I think this algorithm has no negative impact on filesystem perfomance. It works better than the default one in all cases. The new dirpref will greatly improve untarring/removing/coping of big directories, decrease load on cvs servers and much more. The new dirpref doesn't speedup a compilation process, but also doesn't slow it down. Obtained from: Grigoriy Orlov <gluk@ptci.ru> ===== ===== iedowse 2001/04/23 17:37:17 PDT Pre-dirpref versions of fsck may zero out the new superblock fields fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause panics if these fields were zeroed while a filesystem was mounted read-only, and then remounted read-write. Add code to ffs_reload() which copies the fs_contigdirs pointer from the previous superblock, and reinitialises fs_avgf* if necessary. Reviewed by: mckusick ===== ===== nik 2001/04/10 03:36:44 PDT Add information about the new options to newfs and tunefs which set the expected average file size and number of files per directory. Could do with some fleshing out. =====
2001-09-06 06:16:00 +04:00
fprintf(stderr, "\t-g average file size\n");
fprintf(stderr, "\t-h expected number of files per directory\n");
fprintf(stderr, "\t-l journal log file size (`0' to clear journal)\n");
1993-03-21 12:45:37 +03:00
fprintf(stderr, "\t-m minimum percentage of free space\n");
2020-05-16 21:31:45 +03:00
fprintf(stderr, "\t-p POSIX.1e ACLS: `enable' or `disable'\n");
fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
fprintf(stderr, "\t-q quota type (`[no]user' or `[no]group')\n");
fprintf(stderr, "\t-S sector size\n");
exit(2);
1993-03-21 12:45:37 +03:00
}
static void
getsb(struct fs *fs, const char *file)
1993-03-21 12:45:37 +03:00
{
int i;
for (i = 0; ; i++) {
if (sblock_try[i] == -1)
errx(5, "cannot find filesystem superblock");
bread(sblock_try[i] / dev_bsize, (char *)fs, SBLOCKSIZE, file);
switch(fs->fs_magic) {
case FS_UFS2EA_MAGIC:
extattr = 1;
/*FALLTHROUGH*/
case FS_UFS2_MAGIC:
is_ufs2 = 1;
/*FALLTHROUGH*/
case FS_UFS1_MAGIC:
break;
case FS_UFS2EA_MAGIC_SWAPPED:
extattr = 1;
/*FALLTHROUGH*/
case FS_UFS2_MAGIC_SWAPPED:
is_ufs2 = 1;
/*FALLTHROUGH*/
case FS_UFS1_MAGIC_SWAPPED:
warnx("%s: swapping byte order", file);
needswap = 1;
ffs_sb_swap(fs, fs);
break;
default:
continue;
}
if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
continue;
if ((is_ufs2 || fs->fs_old_flags & FS_FLAGS_UPDATED)
&& fs->fs_sblockloc != sblock_try[i])
continue;
break;
}
dev_bsize = fs->fs_fsize / FFS_FSBTODB(fs, 1);
sblockloc = sblock_try[i] / dev_bsize;
1993-03-21 12:45:37 +03:00
}
static void
bwrite(daddr_t blk, char *buffer, int size, const char *file)
1993-03-21 12:45:37 +03:00
{
off_t offset;
offset = (off_t)blk * dev_bsize;
if (lseek(fi, offset, SEEK_SET) == -1)
err(6, "%s: seeking to %lld", file, (long long)offset);
if (write(fi, buffer, size) != size)
err(7, "%s: writing %d bytes", file, size);
1993-03-21 12:45:37 +03:00
}
static void
bread(daddr_t blk, char *buffer, int cnt, const char *file)
1993-03-21 12:45:37 +03:00
{
off_t offset;
int i;
offset = (off_t)blk * dev_bsize;
if (lseek(fi, offset, SEEK_SET) == -1)
err(4, "%s: seeking to %lld", file, (long long)offset);
if ((i = read(fi, buffer, cnt)) != cnt)
errx(5, "%s: short read", file);
1993-03-21 12:45:37 +03:00
}
static int
openpartition(const char *name, int flags, char *device, size_t devicelen)
{
char specname[MAXPATHLEN];
char rawname[MAXPATHLEN];
const char *special, *raw;
struct fstab *fs;
int fd, oerrno;
fs = getfsfile(name);
special = fs ? fs->fs_spec : name;
raw = getfsspecname(specname, sizeof(specname), special);
if (raw == NULL)
err(1, "%s: %s", name, specname);
special = getdiskrawname(rawname, sizeof(rawname), raw);
if (special == NULL)
special = raw;
fd = opendisk(special, flags, device, devicelen, 0);
if (fd == -1 && errno == ENOENT) {
oerrno = errno;
strlcpy(device, special, devicelen);
errno = oerrno;
}
return (fd);
}