#include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { static const size_t buf_size = 128 * 1024; static const int read_count = 10; uint8_t *test_mem; uint8_t *buf; uint8_t *p; uint64_t arc_max; size_t read_size; size_t sysctl_len; size_t file_size; size_t mem_size; size_t to_read; size_t to_write; size_t write_size; off_t read_offset; u_int page_size; u_int page_count; int fd; int i; if (argc < 2) { fprintf (stderr, "usage: %s \n", argv[0]); return (1); } sysctl_len = sizeof(page_size); if (sysctlbyname("vm.stats.vm.v_page_size", &page_size, &sysctl_len, NULL, 0) == -1) { perror("stats.vm.v_page_size"); return (1); } sysctl_len = sizeof(page_count); if (sysctlbyname("vm.stats.vm.v_page_count", &page_count, &sysctl_len, NULL, 0) == -1) { perror("stats.vm.v_page_count"); return (1); } sysctl_len = sizeof(arc_max); if (sysctlbyname("kstat.zfs.misc.arcstats.c_max", &arc_max, &sysctl_len, NULL, 0) == -1) { perror("kstat.zfs.misc.arcstats.c_max"); return (1); } mem_size = ((size_t)page_count * page_size / 8) * 7; test_mem = malloc(mem_size); if (test_mem == NULL) { perror("malloc"); return (1); } for (p = test_mem; p < test_mem + mem_size; p += page_size) *p = 'z'; printf("allocated and dirtied %zu bytes of anonymous memory\n", mem_size); buf = malloc(buf_size); for (p = buf; p < buf + buf_size; p++) *p = rand(); fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC, 0600); if (fd == -1) { perror("open"); return (1); } file_size = arc_max; to_write = file_size; while (to_write > 0) { write_size = MIN(to_write, buf_size); if (write(fd, buf, write_size) != write_size) { perror("write"); return (1); } to_write -= write_size; } printf("created a file of size %zu bytes\n", file_size); for (i = 0; i < read_count; i++) { for (to_read = buf_size; to_read < file_size; to_read += buf_size) { read_offset = 0; while (read_offset < to_read) { read_size = MIN(to_read - read_offset, buf_size); if (pread(fd, buf, read_size, read_offset) != read_size) { perror("read"); return (1); } read_offset += read_size; } } printf("."); } return (0); }