Path: blob/main/tools/test/stress2/lib/resources.c
39536 views
/*-1* Copyright (c) 2008 Peter Holm <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25*/2627/* Get various resource limits for the tests */2829#include <sys/types.h>30#include <sys/sysctl.h>31#include <unistd.h>32#include <stdio.h>33#include <stdlib.h>34#include <fcntl.h>35#include <string.h>36#include <sys/stat.h>37#include <sys/param.h>38#include <sys/mount.h>39#include <kvm.h>40#include <vm/vm_param.h>41#include <errno.h>42#include <err.h>43#include <stdarg.h>44#include <libutil.h>4546#include "stress.h"4748static int lockfd;49static int dffd;50static int flags;51static char lockpath[128];52static char dfpath[128];5354static int64_t55inodes(void)56{57char path[MAXPATHLEN+1];58struct statfs buf;5960if (op->inodes != 0)61return (op->inodes);62if (getcwd(path, sizeof(path)) == NULL)63err(1, "getcwd()");6465if (statfs(path, &buf) < 0)66err(1, "statfs(%s)", path);67if (!strcmp(buf.f_fstypename, "msdosfs"))68buf.f_ffree = 9999;69flags = buf.f_flags & MNT_VISFLAGMASK;70if (op->verbose > 2)71printf("Free inodes on %s (%s): %jd\n", path,72buf.f_mntonname, buf.f_ffree);73return (buf.f_ffree);74}7576static int64_t77df(void)78{79char path[MAXPATHLEN+1];80struct statfs buf;8182if (op->kblocks != 0)83return (op->kblocks * (uint64_t)1024);8485if (getcwd(path, sizeof(path)) == NULL)86err(1, "getcwd()");8788if (statfs(path, &buf) < 0)89err(1, "statfs(%s)", path);90if (buf.f_bavail > (int64_t)buf.f_blocks || buf.f_bavail < 0) {91warnx("Corrupt statfs(%s). f_bavail = %jd!", path,92buf.f_bavail);93buf.f_bavail = 100;94}95if (op->verbose > 2)96printf("Free space on %s: %jd Mb\n", path, buf.f_bavail *97buf.f_bsize / 1024 / 1024);98return (buf.f_bavail * buf.f_bsize);99}100101int64_t102swap(void)103{104struct xswdev xsw;105size_t mibsize, size;106int mib[16], n;107int64_t sz;108109mibsize = sizeof mib / sizeof mib[0];110sz = 0;111112if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)113err(1, "sysctlnametomib()");114115for (n = 0; ; ++n) {116mib[mibsize] = n;117size = sizeof xsw;118if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)119break;120if (xsw.xsw_version != XSWDEV_VERSION)121errx(1, "xswdev version mismatch");122sz = sz + xsw.xsw_nblks - xsw.xsw_used;123}124if (errno != ENOENT)125err(1, "sysctl()");126127if (op->verbose > 2)128printf("Total free swap space %jd Mb\n",129sz * getpagesize() / 1024 / 1024);130131return (sz * getpagesize());132}133134unsigned long135usermem(void)136{137unsigned long mem;138size_t nlen = sizeof(mem);139140if (sysctlbyname("hw.usermem", &mem, &nlen, NULL, 0) == -1)141err(1, "sysctlbyname() %s:%d", __FILE__, __LINE__);142143if (op->verbose > 2)144printf("Total free user memory %lu Mb\n",145mem / 1024 / 1024);146147return (mem);148}149150static void151cleanupdf(void)152{153unlink(dfpath);154}155156void157getdf(int64_t *block, int64_t *inode)158{159int i, j;160char buf[128];161162snprintf(lockpath, sizeof(lockpath), "%s/lock", op->cd);163for (j = 0; j < 2; j++) {164for (i = 0; i < 10000; i++) {165if ((lockfd = open(lockpath,166O_CREAT | O_TRUNC | O_WRONLY |167O_EXCL, 0644)) != -1)168break;169usleep(10000); /* sleep 1/100 sec */170if (i > 0 && i % 1000 == 0)171fprintf(stderr, "%s is waiting for lock file"172" %s\n",173getprogname(), lockpath);174}175if (lockfd != -1)176break;177fprintf(stderr, "%s. Removing stale %s\n", getprogname(),178lockpath);179unlink(lockpath);180}181if (lockfd == -1)182errx(1, "%s. Can not create %s\n", getprogname(), lockpath);183snprintf(dfpath, sizeof(dfpath), "%s/df", op->cd);184if ((dffd = open(dfpath, O_RDWR, 0644)) == -1) {185if ((dffd = open(dfpath,186O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) {187unlink(lockpath);188err(1, "creat(%s) %s:%d", dfpath, __FILE__,189__LINE__);190}191atexit(cleanupdf);192*block = df();193*inode = inodes();194snprintf(buf, sizeof(buf), "%jd %jd", *block, *inode);195196if (write(dffd, buf, strlen(buf) + 1) !=197(ssize_t)strlen(buf) +1)198err(1, "write df. %s:%d", __FILE__, __LINE__);199} else {200if (read(dffd, buf, sizeof(buf)) < 1) {201system("ls -l /tmp/stressX.control");202unlink(lockpath);203err(1, "read df. %s:%d", __FILE__, __LINE__);204}205sscanf(buf, "%jd %jd", block, inode);206}207close(dffd);208}209210void211reservedf(int64_t blks, int64_t inos)212{213char buf[128];214int64_t blocks, inodes;215216if ((dffd = open(dfpath, O_RDWR, 0644)) == -1) {217warn("open(%s) %s:%d. %s", dfpath, __FILE__, __LINE__,218getprogname());219goto err;220}221if (read(dffd, buf, sizeof(buf)) < 1) {222warn("read df. %s:%d", __FILE__, __LINE__);223goto err;224}225sscanf(buf, "%jd %jd", &blocks, &inodes);226227if (op->verbose > 2)228printf("%-8s: reservefd(%9jdK, %6jd) out of (%9jdK, %6jd)\n",229getprogname(), blks/1024, inos, blocks/1024,230inodes);231blocks -= blks;232inodes -= inos;233234snprintf(buf, sizeof(buf), "%jd %jd", blocks, inodes);235if (blocks < 0 || inodes < 0)236printf("******************************** %s: %s\n",237getprogname(), buf);238if (lseek(dffd, 0, 0) == -1)239err(1, "lseek. %s:%d", __FILE__, __LINE__);240if (write(dffd, buf, strlen(buf) + 1) != (ssize_t)strlen(buf) +1)241warn("write df. %s:%d", __FILE__, __LINE__);242err:243close(dffd);244close(lockfd);245if (unlink(lockpath) == -1)246err(1, "unlink(%s)", lockpath);247}248249250