static Try<Nothing> doIncreasePageCache(const vector<string>& tokens) { const Bytes UNIT = Megabytes(1); if (tokens.size() < 2) { return Error("Expect at least one argument"); } Try<Bytes> size = Bytes::parse(tokens[1]); if (size.isError()) { return Error("The first argument '" + tokens[1] + "' is not a byte size"); } // TODO(chzhcn): Currently, we assume the current working directory // is a temporary directory and will be cleaned up when the test // finishes. Since the child process will inherit the current // working directory from the parent process, that means the test // that uses this helper probably needs to inherit from // TemporaryDirectoryTest. Consider relaxing this constraint. Try<string> path = os::mktemp(path::join(os::getcwd(), "XXXXXX")); if (path.isError()) { return Error("Failed to create a temporary file: " + path.error()); } Try<int> fd = os::open(path.get(), O_WRONLY); if (fd.isError()) { return Error("Failed to open file: " + fd.error()); } // NOTE: We are doing round-down here to calculate the number of // writes to do. for (uint64_t i = 0; i < size->bytes() / UNIT.bytes(); i++) { // Write UNIT size to disk at a time. The content isn't important. Try<Nothing> write = os::write(fd.get(), string(UNIT.bytes(), 'a')); if (write.isError()) { os::close(fd.get()); return Error("Failed to write file: " + write.error()); } // Use fsync to make sure data is written to disk. Try<Nothing> fsync = os::fsync(fd.get()); if (fsync.isError()) { os::close(fd.get()); return Error("Failed to fsync: " + fsync.error()); } } os::close(fd.get()); return Nothing(); }