xv6, line by line
user/stressfs.c

user/stressfs.c

C · 50 lines · annotated 100% · user program / library · upstream

About this file

stressfs is a small file-system stress test. It starts five processes that run at the same time, each creating its own file in the root directory (stressfs0 to stressfs4), writing 20 blocks of 512 bytes to it, and reading them back.

The files are separate, so the processes never share a file’s data. They do share everything underneath: the root directory that all five add an entry to, the free bitmap and inode blocks, the buffer cache, the write-ahead log that groups their writes into transactions, and the disk driver’s queue. Running them concurrently on three CPUs checks the locking in all of those layers. The program checks nothing itself: it ignores the return values of open, write and read and never compares the data it reads. A failure shows up only as a kernel panic, a hang, or wrong files afterwards; ls should show five files of 10240 bytes each.

The comment at the top is from the x86 version of xv6 and is out of date (see the note on lines 1–8).

Read before: kernel/sysfile.c (sys_open) and kernel/file.c (filewrite). Read next: user/logstress.c, a heavier test of the log.

1// Demonstrate that moving the "acquire" in iderw after the loop that
2// appends to the idequeue results in a race.
4// For this to work, you should also add a spin within iderw's
5// idequeue traversal loop. Adding the following demonstrated a panic
6// after about 5 runs of stressfs in QEMU on a 2.1GHz CPU:
7// for (i = 0; i < 40000; i++)
8// asm volatile("");
16int
17main(int argc, char *argv[])
19 int fd, i;
20 char path[] = "stressfs0";
21 char data[512];
23 printf("stressfs starting\n");
24 memset(data, 'a', sizeof(data));
26 for (i = 0; i < 4; i++)
27 if (fork() > 0)
28 break;
30 printf("write %d\n", i);
32 path[8] += i;
34 for (i = 0; i < 20; i++) {
35 // printf(fd, "%d\n", i);
36 write(fd, data, sizeof(data));
37 }
40 printf("read\n");
43 for (i = 0; i < 20; i++)
44 read(fd, data, sizeof(data));
47 wait(0);
49 exit(0);