void drop_caches_every(int *fds, char **file_names, int num_files, int interval, bool daemonize) { if (daemonize) { pid_t pid = fork(); if (pid < 0) die("cannot fork"); if (pid > 0) return; } for (;;) { drop_caches(fds, file_names, num_files); sleep(interval); } }
int main(int argc, char **argv) { int opt; bool daemonize = false; char **file_names; int *fds; int num_files; int interval = 0; while ((opt = getopt(argc, argv, "hi:D")) != -1) { switch (opt) { case 'h': help(EXIT_SUCCESS); case 'i': interval = atoi(optarg); break; case 'D': daemonize = true; break; default: help(EXIT_FAILURE); } } file_names = argv + optind; num_files = argc - optind; if (num_files <= 0) help(EXIT_FAILURE); fds = open_files(file_names, num_files); if (interval) drop_caches_every(fds, file_names, num_files, interval, daemonize); else drop_caches(fds, file_names, num_files); return 0; }
int main(int argc, char** argv) { const char* library_path = "libfoo.so"; if (argc >= 2) library_path = argv[1]; { ScopedTimer null_timer("empty"); } // Load the library with dlopen(). void* lib; drop_caches(); { ScopedTimer timer("dlopen"); lib = dlopen(library_path, RTLD_NOW); } if (!lib) Panic("Could not load library with dlopen(): %s\n", dlerror()); dlclose(lib); crazy_library_t* library; crazy_context_t* context = crazy_context_create(); // Ensure the program looks in its own directory too. crazy_context_add_search_path_for_address(context, reinterpret_cast<void*>(&main)); // Load the library with the crazy linker. drop_caches(); { ScopedTimer timer("crazy_linker"); // Load libfoo.so if (!crazy_library_open(&library, library_path, context)) { Panic("Could not open library: %s\n", crazy_context_get_error(context)); } } crazy_library_close(library); // Load the library with the crazy linker. Preload libOpenSLES.so drop_caches(); void* sles_lib = dlopen("libOpenSLES.so", RTLD_NOW); { ScopedTimer timer("crazy_linker (preload libOpenSLES.so)"); // Load libfoo.so if (!crazy_library_open(&library, library_path, context)) { Panic("Could not open library: %s\n", crazy_context_get_error(context)); } } crazy_library_close(library); dlclose(sles_lib); // Load the library with the crazy linker. Preload libOpenSLES.so { drop_caches(); void* sys1_lib = dlopen("libandroid.so", RTLD_NOW); void* sys2_lib = dlopen("libjnigraphics.so", RTLD_NOW); void* sys3_lib = dlopen("libOpenSLES.so", RTLD_NOW); { ScopedTimer timer("crazy_linker (preload 3 system libs)"); // Load libfoo.so if (!crazy_library_open(&library, library_path, context)) { Panic("Could not open library: %s\n", crazy_context_get_error(context)); } } crazy_library_close(library); dlclose(sys3_lib); dlclose(sys2_lib); dlclose(sys1_lib); } // Load the library with the crazy linker. Create a shared RELRO as well. drop_caches(); { ScopedTimer timer("crazy_linker (with RELRO)"); // Load libfoo.so if (!crazy_library_open(&library, library_path, context)) { Panic("Could not open library: %s\n", crazy_context_get_error(context)); } if (!crazy_library_enable_relro_sharing(library, context)) { Panic("Could not create shared RELRO: %s\n", crazy_context_get_error(context)); } } crazy_library_close(library); printf("OK\n"); return 0; }
static void test_readahead(void) { unsigned long read_bytes, read_bytes_ra; long usec, usec_ra; unsigned long cached_max, cached_low, cached, cached_ra; char proc_io_fname[128]; sprintf(proc_io_fname, "/proc/%u/io", getpid()); /* find out how much can cache hold if we read whole file */ read_testfile(0, testfile, testfile_size, &read_bytes, &usec, &cached); cached_max = get_cached_size(); sync(); drop_caches(); cached_low = get_cached_size(); cached_max = cached_max - cached_low; tst_resm(TINFO, "read_testfile(0)"); read_testfile(0, testfile, testfile_size, &read_bytes, &usec, &cached); cached = cached - cached_low; sync(); drop_caches(); cached_low = get_cached_size(); tst_resm(TINFO, "read_testfile(1)"); read_testfile(1, testfile, testfile_size, &read_bytes_ra, &usec_ra, &cached_ra); cached_ra = cached_ra - cached_low; tst_resm(TINFO, "read_testfile(0) took: %ld usec", usec); tst_resm(TINFO, "read_testfile(1) took: %ld usec", usec_ra); if (has_file(proc_io_fname, 0)) { tst_resm(TINFO, "read_testfile(0) read: %ld bytes", read_bytes); tst_resm(TINFO, "read_testfile(1) read: %ld bytes", read_bytes_ra); /* actual number of read bytes depends on total RAM */ if (read_bytes_ra < read_bytes) tst_resm(TPASS, "readahead saved some I/O"); else tst_resm(TFAIL, "readahead failed to save any I/O"); } else { tst_resm(TCONF, "Your system doesn't have /proc/pid/io," " unable to determine read bytes during test"); } tst_resm(TINFO, "cache can hold at least: %ld kB", cached_max); tst_resm(TINFO, "read_testfile(0) used cache: %ld kB", cached); tst_resm(TINFO, "read_testfile(1) used cache: %ld kB", cached_ra); if (cached_max * 1024 >= testfile_size) { /* * if cache can hold ~testfile_size then cache increase * for readahead should be at least testfile_size/2 */ if (cached_ra * 1024 > testfile_size / 2) tst_resm(TPASS, "using cache as expected"); else tst_resm(TWARN, "using less cache than expected"); } else { tst_resm(TCONF, "Page cache on your system is too small " "to hold whole testfile."); } }
int main (int argc, char *argv[]) { u8 *buf; int fd; ssize_t haveRead; size_t toRead; unsigned i; unsigned bufsize; unsigned n; u64 size; u64 rest; u64 l; drop_caches(); Option.file_size = 0; Option.iterations = 1; Option.loops = 1; punyopt(argc, argv, myopt, "b:"); n = Option.iterations; bufsize = 1 << Bufsize_log2; buf = emalloc(bufsize); size = Option.file_size; if (!size) { size = memtotal() / FRACTION_OF_MEMORY; } if (Hog_memory) { hog_leave_memory(size / FRACTION_OF_FILE_SIZE); } fd = open(Option.file, O_RDWR | O_CREAT | O_TRUNC, 0666); fill_file(fd, size); for (l = 0; l < Option.loops; l++) { startTimer(); for (i = 0; i < n; ++i) { for (rest = size; rest; rest -= haveRead) { if (rest > bufsize) { toRead = bufsize; } else { toRead = rest; } haveRead = read(fd, buf, toRead); if (haveRead != toRead) { if (haveRead == -1) { perror("read"); exit(1); } fprintf(stderr, "toRead=%llu != haveRead=%lld\n", (u64)toRead, (s64)haveRead); exit(1); } } lseek(fd, 0, 0); } stopTimer(); printf("size=%lld n=%d ", size, n); prTimer(); printf("\t%6.4g MiB/s\n", (double)(n * size) / get_avg() / MEBI); } close(fd); unlink(Option.file); return 0; }
static void test_readahead(unsigned int n) { unsigned long read_bytes, read_bytes_ra; long long usec, usec_ra; unsigned long cached_high, cached_low, cached, cached_ra; int ret; struct tcase *tc = &tcases[n]; tst_res(TINFO, "Test #%d: %s", n, tc->tname); if (tc->use_overlay && !ovl_mounted) { tst_res(TCONF, "overlayfs is not configured in this kernel."); return; } create_testfile(tc->use_overlay); /* find out how much can cache hold if we read whole file */ read_testfile(tc, 0, testfile, testfile_size, &read_bytes, &usec, &cached); cached_high = get_cached_size(); sync(); drop_caches(); cached_low = get_cached_size(); cached_max = MAX(cached_max, cached_high - cached_low); tst_res(TINFO, "read_testfile(0)"); read_testfile(tc, 0, testfile, testfile_size, &read_bytes, &usec, &cached); if (cached > cached_low) cached = cached - cached_low; else cached = 0; sync(); drop_caches(); cached_low = get_cached_size(); tst_res(TINFO, "read_testfile(1)"); ret = read_testfile(tc, 1, testfile, testfile_size, &read_bytes_ra, &usec_ra, &cached_ra); if (ret == EINVAL) { if (tc->use_fadvise && (!tc->use_overlay || !fadvise_supported)) { fadvise_supported = 0; tst_res(TCONF, "CONFIG_ADVISE_SYSCALLS not configured " "in kernel?"); return; } if (!tc->use_overlay || !readahead_supported) { readahead_supported = 0; tst_res(TCONF, "readahead not supported on %s", tst_device->fs_type); return; } } if (ret) { tst_res(TFAIL | TTERRNO, "%s failed on %s", tc->use_fadvise ? "fadvise" : "readahead", tc->use_overlay ? "overlayfs" : tst_device->fs_type); return; } if (cached_ra > cached_low) cached_ra = cached_ra - cached_low; else cached_ra = 0; tst_res(TINFO, "read_testfile(0) took: %lli usec", usec); tst_res(TINFO, "read_testfile(1) took: %lli usec", usec_ra); if (has_file(PROC_IO_FNAME, 0)) { tst_res(TINFO, "read_testfile(0) read: %ld bytes", read_bytes); tst_res(TINFO, "read_testfile(1) read: %ld bytes", read_bytes_ra); /* actual number of read bytes depends on total RAM */ if (read_bytes_ra < read_bytes) tst_res(TPASS, "readahead saved some I/O"); else tst_res(TFAIL, "readahead failed to save any I/O"); } else { tst_res(TCONF, "Your system doesn't have /proc/self/io," " unable to determine read bytes during test"); } tst_res(TINFO, "cache can hold at least: %ld kB", cached_max); tst_res(TINFO, "read_testfile(0) used cache: %ld kB", cached); tst_res(TINFO, "read_testfile(1) used cache: %ld kB", cached_ra); if (cached_max * 1024 >= testfile_size) { /* * if cache can hold ~testfile_size then cache increase * for readahead should be at least testfile_size/2 */ if (cached_ra * 1024 > testfile_size / 2) tst_res(TPASS, "using cache as expected"); else if (!cached_ra) tst_res(TFAIL, "readahead failed to use any cache"); else tst_res(TWARN, "using less cache than expected"); } else { tst_res(TCONF, "Page cache on your system is too small " "to hold whole testfile."); } }