| 232 | |
| 233 | |
| 234 | static Try<Nothing> doIncreasePageCache(const vector<string>& tokens) |
| 235 | { |
| 236 | const Bytes UNIT = Megabytes(1); |
| 237 | |
| 238 | if (tokens.size() < 2) { |
| 239 | return Error("Expect at least one argument"); |
| 240 | } |
| 241 | |
| 242 | Try<Bytes> size = Bytes::parse(tokens[1]); |
| 243 | if (size.isError()) { |
| 244 | return Error("The first argument '" + tokens[1] + "' is not a byte size"); |
| 245 | } |
| 246 | |
| 247 | // TODO(chzhcn): Currently, we assume the current working directory |
| 248 | // is a temporary directory and will be cleaned up when the test |
| 249 | // finishes. Since the child process will inherit the current |
| 250 | // working directory from the parent process, that means the test |
| 251 | // that uses this helper probably needs to inherit from |
| 252 | // TemporaryDirectoryTest. Consider relaxing this constraint. |
| 253 | Try<string> path = os::mktemp(path::join(os::getcwd(), "XXXXXX")); |
| 254 | if (path.isError()) { |
| 255 | return Error("Failed to create a temporary file: " + path.error()); |
| 256 | } |
| 257 | |
| 258 | Try<int_fd> fd = os::open(path.get(), O_WRONLY); |
| 259 | if (fd.isError()) { |
| 260 | return Error("Failed to open file: " + fd.error()); |
| 261 | } |
| 262 | |
| 263 | // NOTE: We are doing round-down here to calculate the number of |
| 264 | // writes to do. |
| 265 | for (uint64_t i = 0; i < size->bytes() / UNIT.bytes(); i++) { |
| 266 | // Write UNIT size to disk at a time. The content isn't important. |
| 267 | Try<Nothing> write = os::write(fd.get(), string(UNIT.bytes(), 'a')); |
| 268 | if (write.isError()) { |
| 269 | os::close(fd.get()); |
| 270 | return Error("Failed to write file: " + write.error()); |
| 271 | } |
| 272 | |
| 273 | // Use fsync to make sure data is written to disk. |
| 274 | Try<Nothing> fsync = os::fsync(fd.get()); |
| 275 | if (fsync.isError()) { |
| 276 | os::close(fd.get()); |
| 277 | return Error("Failed to fsync: " + fsync.error()); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | os::close(fd.get()); |
| 282 | return Nothing(); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | const char MemoryTestHelper::NAME[] = "Memory"; |
nothing calls this directly
no test coverage detected