2 * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 /* Get various resource limits for the tests */
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
38 #include <sys/param.h>
39 #include <sys/mount.h>
41 #include <vm/vm_param.h>
52 static char lockpath[128];
53 static char dfpath[128];
58 char path[MAXPATHLEN+1];
63 if (getcwd(path, sizeof(path)) == NULL)
66 if (statfs(path, &buf) < 0)
67 err(1, "statfs(%s)", path);
68 if (!strcmp(buf.f_fstypename, "msdosfs"))
70 flags = buf.f_flags & MNT_VISFLAGMASK;
72 printf("Free inodes on %s (%s): %jd\n", path, buf.f_mntonname,
73 (intmax_t)buf.f_ffree);
80 char path[MAXPATHLEN+1];
84 return (op->kblocks * (uint64_t)1024);
86 if (getcwd(path, sizeof(path)) == NULL)
89 if (statfs(path, &buf) < 0)
90 err(1, "statfs(%s)", path);
91 if (buf.f_bavail > buf.f_blocks || buf.f_bavail < 0) {
92 warnx("Corrupt statfs(%s). f_bavail = %jd!", path,
93 (intmax_t)buf.f_bavail);
97 printf("Free space on %s: %jd Mb\n", path,
98 (intmax_t)(buf.f_bavail * buf.f_bsize / 1024 / 1024));
99 return (buf.f_bavail * buf.f_bsize);
106 #error "please put the amount of free swap (in bytes) in sz" /* REMOVE HERE! */
108 * Currently, DragonFly has no way of determining free swap as non-root
110 * Please remove the #error line above this comment and modify the
111 * line below it with the amount of free swap you have (in bytes).
113 int64_t sz = 1073741824; /* EDIT HERE! */
116 printf("Total free swap space %jd Mb\n",
117 (intmax_t)(sz / 1024 / 1024));
126 size_t nlen = sizeof(mem);
128 if (sysctlbyname("hw.usermem", &mem, &nlen, NULL, 0) == -1)
129 err(1, "sysctlbyname() %s:%d", __FILE__, __LINE__);
132 printf("Total free user memory %lu Mb\n",
144 getdf(int64_t *block, int64_t *inode)
149 snprintf(lockpath, sizeof(lockpath), "%s/lock", op->cd);
150 for (j = 0; j < 10; j++) {
151 for (i = 0; i < 10000; i++) {
152 if ((lockfd = open(lockpath,
153 O_CREAT | O_TRUNC | O_WRONLY | O_EXCL, 0644)) != -1)
155 usleep(10000); /* sleep 1/100 sec */
159 fprintf(stderr, "%s. Removing stale %s\n", getprogname(), lockpath);
163 errx(1, "%s. Can not create %s\n", getprogname(), lockpath);
164 snprintf(dfpath, sizeof(dfpath), "%s/df", op->cd);
165 if ((dffd = open(dfpath, O_RDWR, 0644)) == -1) {
166 if ((dffd = open(dfpath,
167 O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) {
169 err(1, "creat(%s) %s:%d", dfpath, __FILE__, __LINE__);
174 snprintf(buf, sizeof(buf), "%jd %jd", *block, *inode);
176 if (write(dffd, buf, strlen(buf) + 1) != strlen(buf) +1)
177 err(1, "write df. %s:%d", __FILE__, __LINE__);
179 if (read(dffd, buf, sizeof(buf)) < 1) {
180 system("ls -l /tmp/stressX.control");
182 err(1, "read df. %s:%d", __FILE__, __LINE__);
184 sscanf(buf, "%jd %jd", block, inode);
191 reservedf(int64_t blks, int64_t inos)
194 int64_t blocks, inodes;
196 if ((dffd = open(dfpath, O_RDWR, 0644)) == -1) {
197 warn("open(%s) %s:%d. %s", dfpath, __FILE__, __LINE__, getprogname());
200 if (read(dffd, buf, sizeof(buf)) < 1) {
201 warn("read df. %s:%d", __FILE__, __LINE__);
204 sscanf(buf, "%jd %jd", &blocks, &inodes);
207 printf("%-8s: reservefd(%9jdK, %6jd) out of (%9jdK, %6jd)\n",
208 getprogname(), blks/1024, inos, blocks/1024, inodes);
212 snprintf(buf, sizeof(buf), "%jd %jd", blocks, inodes);
213 if (blocks < 0 || inodes < 0)
214 printf("******************************** %s: %s\n", getprogname(), buf);
215 if (lseek(dffd, 0, 0) == -1)
216 err(1, "lseek. %s:%d", __FILE__, __LINE__);
217 if (write(dffd, buf, strlen(buf) + 1) != strlen(buf) +1)
218 warn("write df. %s:%d", __FILE__, __LINE__);
222 if (unlink(lockpath) == -1)
223 err(1, "unlink(%s)", lockpath);
226 /* The UFS2 soft update lag problem causes a lot of confusion, so for now add the err() function here */
229 vpr(int code, const char *fmt, va_list ap)
231 char path[MAXPATHLEN+1];
238 if (errno == ENOSPC && (flags & MNT_SOFTDEP) && (flags & MNT_QUOTA) == 0 &&
239 s > 100 && i > 100) {
240 if (getcwd(path, sizeof(path)) == NULL)
243 humanize_number(siz, sizeof(siz), s, "",
244 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
245 humanize_number(ino, sizeof(ino), i, "",
246 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
248 printf("A syscall has failed with ENOSPC even though free disk "
249 "space for %s is reported as %s and %s inodes.\n",
254 fprintf(stderr, "%s: ", getprogname());
256 vfprintf(stderr, fmt, ap);
257 fprintf(stderr, ": ");
259 fprintf(stderr, "%s\n", strerror(code));
263 err(int eval, const char *fmt, ...)
275 warn(const char *fmt, ...)