18a272653SPeter Holm /*-
28a272653SPeter Holm * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
38a272653SPeter Holm * All rights reserved.
48a272653SPeter Holm *
58a272653SPeter Holm * Redistribution and use in source and binary forms, with or without
68a272653SPeter Holm * modification, are permitted provided that the following conditions
78a272653SPeter Holm * are met:
88a272653SPeter Holm * 1. Redistributions of source code must retain the above copyright
98a272653SPeter Holm * notice, this list of conditions and the following disclaimer.
108a272653SPeter Holm * 2. Redistributions in binary form must reproduce the above copyright
118a272653SPeter Holm * notice, this list of conditions and the following disclaimer in the
128a272653SPeter Holm * documentation and/or other materials provided with the distribution.
138a272653SPeter Holm *
148a272653SPeter Holm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158a272653SPeter Holm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168a272653SPeter Holm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178a272653SPeter Holm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188a272653SPeter Holm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198a272653SPeter Holm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208a272653SPeter Holm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218a272653SPeter Holm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228a272653SPeter Holm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238a272653SPeter Holm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248a272653SPeter Holm * SUCH DAMAGE.
258a272653SPeter Holm *
268a272653SPeter Holm */
278a272653SPeter Holm
288a272653SPeter Holm /* Get various resource limits for the tests */
298a272653SPeter Holm
308a272653SPeter Holm #include <sys/types.h>
318a272653SPeter Holm #include <sys/sysctl.h>
328a272653SPeter Holm #include <unistd.h>
338a272653SPeter Holm #include <stdio.h>
348a272653SPeter Holm #include <stdlib.h>
358a272653SPeter Holm #include <fcntl.h>
368a272653SPeter Holm #include <string.h>
378a272653SPeter Holm #include <sys/stat.h>
388a272653SPeter Holm #include <sys/param.h>
398a272653SPeter Holm #include <sys/mount.h>
408a272653SPeter Holm #include <kvm.h>
418a272653SPeter Holm #include <vm/vm_param.h>
428a272653SPeter Holm #include <errno.h>
438a272653SPeter Holm #include <err.h>
448a272653SPeter Holm #include <stdarg.h>
458a272653SPeter Holm #include <libutil.h>
468a272653SPeter Holm
478a272653SPeter Holm #include "stress.h"
488a272653SPeter Holm
498a272653SPeter Holm static int lockfd;
508a272653SPeter Holm static int dffd;
518a272653SPeter Holm static int flags;
528a272653SPeter Holm static char lockpath[128];
538a272653SPeter Holm static char dfpath[128];
548a272653SPeter Holm
558a272653SPeter Holm static int64_t
inodes(void)568a272653SPeter Holm inodes(void)
578a272653SPeter Holm {
588a272653SPeter Holm char path[MAXPATHLEN+1];
598a272653SPeter Holm struct statfs buf;
608a272653SPeter Holm
618a272653SPeter Holm if (op->inodes != 0)
628a272653SPeter Holm return (op->inodes);
638a272653SPeter Holm if (getcwd(path, sizeof(path)) == NULL)
648a272653SPeter Holm err(1, "getcwd()");
658a272653SPeter Holm
668a272653SPeter Holm if (statfs(path, &buf) < 0)
678a272653SPeter Holm err(1, "statfs(%s)", path);
688a272653SPeter Holm if (!strcmp(buf.f_fstypename, "msdosfs"))
698a272653SPeter Holm buf.f_ffree = 9999;
708a272653SPeter Holm flags = buf.f_flags & MNT_VISFLAGMASK;
718a272653SPeter Holm if (op->verbose > 2)
728a272653SPeter Holm printf("Free inodes on %s (%s): %jd\n", path,
738a272653SPeter Holm buf.f_mntonname, buf.f_ffree);
748a272653SPeter Holm return (buf.f_ffree);
758a272653SPeter Holm }
768a272653SPeter Holm
778a272653SPeter Holm static int64_t
df(void)788a272653SPeter Holm df(void)
798a272653SPeter Holm {
808a272653SPeter Holm char path[MAXPATHLEN+1];
818a272653SPeter Holm struct statfs buf;
828a272653SPeter Holm
838a272653SPeter Holm if (op->kblocks != 0)
848a272653SPeter Holm return (op->kblocks * (uint64_t)1024);
858a272653SPeter Holm
868a272653SPeter Holm if (getcwd(path, sizeof(path)) == NULL)
878a272653SPeter Holm err(1, "getcwd()");
888a272653SPeter Holm
898a272653SPeter Holm if (statfs(path, &buf) < 0)
908a272653SPeter Holm err(1, "statfs(%s)", path);
918a272653SPeter Holm if (buf.f_bavail > (int64_t)buf.f_blocks || buf.f_bavail < 0) {
928a272653SPeter Holm warnx("Corrupt statfs(%s). f_bavail = %jd!", path,
938a272653SPeter Holm buf.f_bavail);
948a272653SPeter Holm buf.f_bavail = 100;
958a272653SPeter Holm }
968a272653SPeter Holm if (op->verbose > 2)
978a272653SPeter Holm printf("Free space on %s: %jd Mb\n", path, buf.f_bavail *
988a272653SPeter Holm buf.f_bsize / 1024 / 1024);
998a272653SPeter Holm return (buf.f_bavail * buf.f_bsize);
1008a272653SPeter Holm }
1018a272653SPeter Holm
1028a272653SPeter Holm int64_t
swap(void)1038a272653SPeter Holm swap(void)
1048a272653SPeter Holm {
1058a272653SPeter Holm struct xswdev xsw;
1068a272653SPeter Holm size_t mibsize, size;
1078a272653SPeter Holm int mib[16], n;
1088a272653SPeter Holm int64_t sz;
1098a272653SPeter Holm
1108a272653SPeter Holm mibsize = sizeof mib / sizeof mib[0];
1118a272653SPeter Holm sz = 0;
1128a272653SPeter Holm
1138a272653SPeter Holm if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
1148a272653SPeter Holm err(1, "sysctlnametomib()");
1158a272653SPeter Holm
1168a272653SPeter Holm for (n = 0; ; ++n) {
1178a272653SPeter Holm mib[mibsize] = n;
1188a272653SPeter Holm size = sizeof xsw;
1198a272653SPeter Holm if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
1208a272653SPeter Holm break;
1218a272653SPeter Holm if (xsw.xsw_version != XSWDEV_VERSION)
1228a272653SPeter Holm errx(1, "xswdev version mismatch");
1238a272653SPeter Holm sz = sz + xsw.xsw_nblks - xsw.xsw_used;
1248a272653SPeter Holm }
1258a272653SPeter Holm if (errno != ENOENT)
1268a272653SPeter Holm err(1, "sysctl()");
1278a272653SPeter Holm
1288a272653SPeter Holm if (op->verbose > 2)
1298a272653SPeter Holm printf("Total free swap space %jd Mb\n",
1308a272653SPeter Holm sz * getpagesize() / 1024 / 1024);
1318a272653SPeter Holm
1328a272653SPeter Holm return (sz * getpagesize());
1338a272653SPeter Holm }
1348a272653SPeter Holm
1358a272653SPeter Holm unsigned long
usermem(void)1368a272653SPeter Holm usermem(void)
1378a272653SPeter Holm {
1388a272653SPeter Holm unsigned long mem;
1398a272653SPeter Holm size_t nlen = sizeof(mem);
1408a272653SPeter Holm
1418a272653SPeter Holm if (sysctlbyname("hw.usermem", &mem, &nlen, NULL, 0) == -1)
1428a272653SPeter Holm err(1, "sysctlbyname() %s:%d", __FILE__, __LINE__);
1438a272653SPeter Holm
1448a272653SPeter Holm if (op->verbose > 2)
1458a272653SPeter Holm printf("Total free user memory %lu Mb\n",
1468a272653SPeter Holm mem / 1024 / 1024);
1478a272653SPeter Holm
1488a272653SPeter Holm return (mem);
1498a272653SPeter Holm }
1508a272653SPeter Holm
1518a272653SPeter Holm static void
cleanupdf(void)152*7861fce3SPeter Holm cleanupdf(void)
1538a272653SPeter Holm {
1548a272653SPeter Holm unlink(dfpath);
1558a272653SPeter Holm }
1568a272653SPeter Holm
1578a272653SPeter Holm void
getdf(int64_t * block,int64_t * inode)1588a272653SPeter Holm getdf(int64_t *block, int64_t *inode)
1598a272653SPeter Holm {
1608a272653SPeter Holm int i, j;
1618a272653SPeter Holm char buf[128];
1628a272653SPeter Holm
1638a272653SPeter Holm snprintf(lockpath, sizeof(lockpath), "%s/lock", op->cd);
1648a272653SPeter Holm for (j = 0; j < 2; j++) {
1658a272653SPeter Holm for (i = 0; i < 10000; i++) {
1668a272653SPeter Holm if ((lockfd = open(lockpath,
1678a272653SPeter Holm O_CREAT | O_TRUNC | O_WRONLY |
1688a272653SPeter Holm O_EXCL, 0644)) != -1)
1698a272653SPeter Holm break;
1708a272653SPeter Holm usleep(10000); /* sleep 1/100 sec */
1718a272653SPeter Holm if (i > 0 && i % 1000 == 0)
1728a272653SPeter Holm fprintf(stderr, "%s is waiting for lock file"
1738a272653SPeter Holm " %s\n",
1748a272653SPeter Holm getprogname(), lockpath);
1758a272653SPeter Holm }
1768a272653SPeter Holm if (lockfd != -1)
1778a272653SPeter Holm break;
1788a272653SPeter Holm fprintf(stderr, "%s. Removing stale %s\n", getprogname(),
1798a272653SPeter Holm lockpath);
1808a272653SPeter Holm unlink(lockpath);
1818a272653SPeter Holm }
1828a272653SPeter Holm if (lockfd == -1)
1838a272653SPeter Holm errx(1, "%s. Can not create %s\n", getprogname(), lockpath);
1848a272653SPeter Holm snprintf(dfpath, sizeof(dfpath), "%s/df", op->cd);
1858a272653SPeter Holm if ((dffd = open(dfpath, O_RDWR, 0644)) == -1) {
1868a272653SPeter Holm if ((dffd = open(dfpath,
1878a272653SPeter Holm O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) {
1888a272653SPeter Holm unlink(lockpath);
1898a272653SPeter Holm err(1, "creat(%s) %s:%d", dfpath, __FILE__,
1908a272653SPeter Holm __LINE__);
1918a272653SPeter Holm }
1928a272653SPeter Holm atexit(cleanupdf);
1938a272653SPeter Holm *block = df();
1948a272653SPeter Holm *inode = inodes();
1958a272653SPeter Holm snprintf(buf, sizeof(buf), "%jd %jd", *block, *inode);
1968a272653SPeter Holm
1978a272653SPeter Holm if (write(dffd, buf, strlen(buf) + 1) !=
1988a272653SPeter Holm (ssize_t)strlen(buf) +1)
1998a272653SPeter Holm err(1, "write df. %s:%d", __FILE__, __LINE__);
2008a272653SPeter Holm } else {
2018a272653SPeter Holm if (read(dffd, buf, sizeof(buf)) < 1) {
2028a272653SPeter Holm system("ls -l /tmp/stressX.control");
2038a272653SPeter Holm unlink(lockpath);
2048a272653SPeter Holm err(1, "read df. %s:%d", __FILE__, __LINE__);
2058a272653SPeter Holm }
2068a272653SPeter Holm sscanf(buf, "%jd %jd", block, inode);
2078a272653SPeter Holm }
2088a272653SPeter Holm close(dffd);
2098a272653SPeter Holm }
2108a272653SPeter Holm
2118a272653SPeter Holm void
reservedf(int64_t blks,int64_t inos)2128a272653SPeter Holm reservedf(int64_t blks, int64_t inos)
2138a272653SPeter Holm {
2148a272653SPeter Holm char buf[128];
2158a272653SPeter Holm int64_t blocks, inodes;
2168a272653SPeter Holm
2178a272653SPeter Holm if ((dffd = open(dfpath, O_RDWR, 0644)) == -1) {
2188a272653SPeter Holm warn("open(%s) %s:%d. %s", dfpath, __FILE__, __LINE__,
2198a272653SPeter Holm getprogname());
2208a272653SPeter Holm goto err;
2218a272653SPeter Holm }
2228a272653SPeter Holm if (read(dffd, buf, sizeof(buf)) < 1) {
2238a272653SPeter Holm warn("read df. %s:%d", __FILE__, __LINE__);
2248a272653SPeter Holm goto err;
2258a272653SPeter Holm }
2268a272653SPeter Holm sscanf(buf, "%jd %jd", &blocks, &inodes);
2278a272653SPeter Holm
2288a272653SPeter Holm if (op->verbose > 2)
2298a272653SPeter Holm printf("%-8s: reservefd(%9jdK, %6jd) out of (%9jdK, %6jd)\n",
2308a272653SPeter Holm getprogname(), blks/1024, inos, blocks/1024,
2318a272653SPeter Holm inodes);
2328a272653SPeter Holm blocks -= blks;
2338a272653SPeter Holm inodes -= inos;
2348a272653SPeter Holm
2358a272653SPeter Holm snprintf(buf, sizeof(buf), "%jd %jd", blocks, inodes);
2368a272653SPeter Holm if (blocks < 0 || inodes < 0)
2378a272653SPeter Holm printf("******************************** %s: %s\n",
2388a272653SPeter Holm getprogname(), buf);
2398a272653SPeter Holm if (lseek(dffd, 0, 0) == -1)
2408a272653SPeter Holm err(1, "lseek. %s:%d", __FILE__, __LINE__);
2418a272653SPeter Holm if (write(dffd, buf, strlen(buf) + 1) != (ssize_t)strlen(buf) +1)
2428a272653SPeter Holm warn("write df. %s:%d", __FILE__, __LINE__);
2438a272653SPeter Holm err:
2448a272653SPeter Holm close(dffd);
2458a272653SPeter Holm close(lockfd);
2468a272653SPeter Holm if (unlink(lockpath) == -1)
2478a272653SPeter Holm err(1, "unlink(%s)", lockpath);
2488a272653SPeter Holm }
249