| 55 | #endif |
| 56 | |
| 57 | void OsInfo::Init() { |
| 58 | DCHECK(!initialized_); |
| 59 | struct stat info; |
| 60 | // Read from /etc/os-release |
| 61 | if (stat("/etc/os-release", &info) == 0) { |
| 62 | ifstream os_distribution("/etc/os-release", ios::in); |
| 63 | string line; |
| 64 | while (os_distribution.good() && !os_distribution.eof()) { |
| 65 | getline(os_distribution, line); |
| 66 | vector<string> fields; |
| 67 | split(fields, line, is_any_of("="), token_compress_on); |
| 68 | if (fields[0].compare("PRETTY_NAME") == 0) { |
| 69 | os_distribution_ = fields[1].data(); |
| 70 | // remove quotes around os distribution |
| 71 | os_distribution_.erase( |
| 72 | remove(os_distribution_.begin(), os_distribution_.end(), '\"'), |
| 73 | os_distribution_.end()); |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | if (os_distribution.is_open()) os_distribution.close(); |
| 78 | } else if (stat("/etc/redhat-release", &info) == 0) { |
| 79 | // Only old distributions like centos 6, redhat 6 |
| 80 | ifstream os_distribution("/etc/redhat-release", ios::in); |
| 81 | if (os_distribution.good()) getline(os_distribution, os_distribution_); |
| 82 | if (os_distribution.is_open()) os_distribution.close(); |
| 83 | } |
| 84 | |
| 85 | // Read from /proc/version |
| 86 | ifstream os_version("/proc/version", ios::in); |
| 87 | if (os_version.good()) getline(os_version, os_version_); |
| 88 | if (os_version.is_open()) os_version.close(); |
| 89 | |
| 90 | // Read the current clocksource to see if CLOCK_MONOTONIC is known to be fast. "tsc" is |
| 91 | // fast, while "xen" is slow (40 times slower than "tsc" on EC2). If CLOCK_MONOTONIC is |
| 92 | // known to be slow, we use CLOCK_MONOTONIC_COARSE, which uses jiffies, with a |
| 93 | // resolution measured in milliseconds, rather than nanoseconds. |
| 94 | std::ifstream clocksource_file( |
| 95 | "/sys/devices/system/clocksource/clocksource0/current_clocksource"); |
| 96 | if (clocksource_file.good()) { |
| 97 | std::string clocksource; |
| 98 | clocksource_file >> clocksource; |
| 99 | clock_name_ = "clocksource: '" + clocksource + "', clockid_t: "; |
| 100 | if (HAVE_CLOCK_MONOTONIC_COARSE && clocksource != "tsc") { |
| 101 | clock_name_ += "CLOCK_MONOTONIC_COARSE"; |
| 102 | fast_clock_ = CLOCK_MONOTONIC_COARSE; |
| 103 | } else { |
| 104 | clock_name_ += "CLOCK_MONOTONIC"; |
| 105 | fast_clock_ = CLOCK_MONOTONIC; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | initialized_ = true; |
| 110 | } |
| 111 | |
| 112 | string OsInfo::DebugString() { |
| 113 | DCHECK(initialized_); |