Helper function to check whether the system uses swap. TODO(bbannier): Augment this implementation to explicitly check for swap on platforms other than Linux as well. It might make sense to move this function to stout once the implementation becomes more complete.
| 46 | // on platforms other than Linux as well. It might make sense to move this |
| 47 | // function to stout once the implementation becomes more complete. |
| 48 | Try<bool> hasSwap() { |
| 49 | // We read `/proc/swaps` and check whether any active swap |
| 50 | // partitions are listed. The format of that file is e.g., |
| 51 | // |
| 52 | // Filename Type Size Used Priority |
| 53 | // /dev/sda4 partition 1234567890 348 -1 |
| 54 | // /dev/sdb4 partition 1234567890 0 -2 |
| 55 | // |
| 56 | // If we find more than one line (including the header) the |
| 57 | // system likely uses swap. |
| 58 | int numberOfLines = 0; |
| 59 | |
| 60 | std::ifstream swaps("/proc/swaps"); |
| 61 | std::string line; |
| 62 | |
| 63 | while (std::getline(swaps, line)) { |
| 64 | ++numberOfLines; |
| 65 | } |
| 66 | |
| 67 | // Check for read errors. This provides a default implementation |
| 68 | // for platforms without proc filesystem as well. |
| 69 | if (!swaps.eof()) { |
| 70 | return Error("Could not determine whether this system uses swap"); |
| 71 | } |
| 72 | |
| 73 | return numberOfLines > 1; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | // The amount of memory in MB each balloon step consumes. |