Bring in FreeBSD's stress2 stress testing suite.
[dragonfly.git] / test / stress / stress2 / lib / resources.c
1 /*-
2  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27
28 /* Get various resource limits for the tests */
29
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <kvm.h>
41 #include <vm/vm_param.h>
42 #include <errno.h>
43 #include <err.h>
44 #include <stdarg.h>
45 #include <libutil.h>
46
47 #include "stress.h"
48
49 static int lockfd;
50 static int dffd;
51 static int flags;
52 static char lockpath[128];
53 static char dfpath[128];
54
55 static int64_t
56 inodes(void)
57 {
58         char path[MAXPATHLEN+1];
59         struct statfs buf;
60
61         if (op->inodes != 0)
62                 return (op->inodes);
63         if (getcwd(path, sizeof(path)) == NULL)
64                 err(1, "getcwd()");
65
66         if (statfs(path, &buf) < 0)
67                 err(1, "statfs(%s)", path);
68         if (!strcmp(buf.f_fstypename, "msdosfs"))
69                         buf.f_ffree = 9999;
70         flags = buf.f_flags & MNT_VISFLAGMASK;
71         if (op->verbose > 2)
72                 printf("Free inodes on %s (%s): %jd\n", path, buf.f_mntonname,
73                     (intmax_t)buf.f_ffree);
74         return (buf.f_ffree);
75 }
76
77 static int64_t
78 df(void)
79 {
80         char path[MAXPATHLEN+1];
81         struct statfs buf;
82
83         if (op->kblocks != 0)
84                 return (op->kblocks * (uint64_t)1024);
85
86         if (getcwd(path, sizeof(path)) == NULL)
87                 err(1, "getcwd()");
88
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);
94                 buf.f_bavail = 100;
95         }
96         if (op->verbose > 2)
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);
100 }
101
102
103 int64_t
104 swap(void)
105 {
106 #error "please put the amount of free swap (in bytes) in sz" /* REMOVE HERE! */
107         /*
108          * Currently, DragonFly has no way of determining free swap as non-root
109          *
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).
112          */
113         int64_t sz = 1073741824; /* EDIT HERE! */
114
115         if (op->verbose > 2)
116                 printf("Total free swap space %jd Mb\n",
117                     (intmax_t)(sz / 1024 / 1024));
118
119         return (sz);
120 }
121
122 unsigned long
123 usermem(void)
124 {
125         unsigned long mem;
126         size_t nlen = sizeof(mem);
127
128         if (sysctlbyname("hw.usermem", &mem, &nlen, NULL, 0) == -1)
129                 err(1, "sysctlbyname() %s:%d", __FILE__, __LINE__);
130
131         if (op->verbose > 2)
132                 printf("Total free user memory %lu Mb\n",
133                         mem / 1024 / 1024);
134
135         return (mem);
136 }
137
138 void cleanupdf()
139 {
140         unlink(dfpath);
141 }
142
143 void
144 getdf(int64_t *block, int64_t *inode)
145 {
146         int i, j;
147         char buf[128];
148
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)
154                                 break;
155                         usleep(10000); /* sleep 1/100 sec */
156                 }
157                 if (lockfd != -1)
158                         break;
159                 fprintf(stderr, "%s. Removing stale %s\n", getprogname(), lockpath);
160                 unlink(lockpath);
161         }
162         if (lockfd == -1)
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) {
168                         unlink(lockpath);
169                         err(1, "creat(%s) %s:%d", dfpath, __FILE__, __LINE__);
170                 }
171                 atexit(cleanupdf);
172                 *block = df();
173                 *inode = inodes();
174                 snprintf(buf, sizeof(buf), "%jd %jd", *block, *inode);
175
176                 if (write(dffd, buf, strlen(buf) + 1) != strlen(buf) +1)
177                         err(1, "write df. %s:%d", __FILE__, __LINE__);
178         } else {
179                 if (read(dffd, buf, sizeof(buf)) < 1) {
180                         system("ls -l /tmp/stressX.control");
181                         unlink(lockpath);
182                         err(1, "read df. %s:%d", __FILE__, __LINE__);
183                 }
184                 sscanf(buf, "%jd %jd", block, inode);
185         }
186         close(dffd);
187 }
188
189
190 void
191 reservedf(int64_t blks, int64_t inos)
192 {
193         char buf[128];
194         int64_t blocks, inodes;
195
196         if ((dffd = open(dfpath, O_RDWR, 0644)) == -1) {
197                 warn("open(%s) %s:%d. %s", dfpath, __FILE__, __LINE__, getprogname());
198                 goto err;
199         }
200         if (read(dffd, buf, sizeof(buf)) < 1) {
201                 warn("read df. %s:%d", __FILE__, __LINE__);
202                 goto err;
203         }
204         sscanf(buf, "%jd %jd", &blocks, &inodes);
205
206         if (op->verbose > 2)
207                 printf("%-8s: reservefd(%9jdK, %6jd) out of (%9jdK, %6jd)\n",
208                                 getprogname(), blks/1024, inos, blocks/1024, inodes);
209         blocks -= blks;
210         inodes -= inos;
211
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__);
219 err:
220         close(dffd);
221         close(lockfd);
222         if (unlink(lockpath) == -1)
223                 err(1, "unlink(%s)", lockpath);
224 }
225
226 /* The UFS2 soft update lag problem causes a lot of confusion, so for now add the err() function here */
227
228 static void
229 vpr(int code, const char *fmt, va_list ap)
230 {
231         char path[MAXPATHLEN+1];
232         char siz[5], ino[5];
233         int64_t s, i;
234
235         s = df();
236         i = inodes();
237
238         if (errno == ENOSPC && (flags & MNT_SOFTDEP) && (flags & MNT_QUOTA) == 0 &&
239                         s > 100 && i > 100) {
240                 if (getcwd(path, sizeof(path)) == NULL)
241                         err(1, "getcwd()");
242
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);
247
248                 printf("A syscall has failed with ENOSPC even though free disk "
249                         "space for %s is reported as %s and %s inodes.\n",
250                         path, siz, ino);
251         }
252
253
254         fprintf(stderr, "%s: ", getprogname());
255         if (fmt != NULL) {
256                 vfprintf(stderr, fmt, ap);
257                 fprintf(stderr, ": ");
258         }
259         fprintf(stderr, "%s\n", strerror(code));
260 }
261
262 void
263 err(int eval, const char *fmt, ...)
264 {
265         va_list ap;
266         int code = errno;
267
268         va_start(ap, fmt);
269         vpr(code, fmt, ap);
270         va_end(ap);
271         exit(eval);
272 }
273
274 void
275 warn(const char *fmt, ...)
276 {
277         va_list ap;
278         int code = errno;
279
280         va_start(ap, fmt);
281         vpr(code, fmt, ap);
282         va_end(ap);
283 }