| 282 | }; |
| 283 | |
| 284 | static bool read_load_average(LoadAverage &m) { |
| 285 | #if defined(OS_LINUX) |
| 286 | butil::ScopedFILE fp("/proc/loadavg", "r"); |
| 287 | if (NULL == fp) { |
| 288 | PLOG_ONCE(WARNING) << "Fail to open /proc/loadavg"; |
| 289 | return false; |
| 290 | } |
| 291 | m = LoadAverage(); |
| 292 | errno = 0; |
| 293 | if (fscanf(fp, "%lf %lf %lf", |
| 294 | &m.loadavg_1m, &m.loadavg_5m, &m.loadavg_15m) != 3) { |
| 295 | PLOG(WARNING) << "Fail to fscanf"; |
| 296 | return false; |
| 297 | } |
| 298 | return true; |
| 299 | #elif defined(OS_MACOSX) |
| 300 | std::ostringstream oss; |
| 301 | if (butil::read_command_output(oss, "sysctl -n vm.loadavg") != 0) { |
| 302 | LOG(ERROR) << "Fail to read loadavg"; |
| 303 | return false; |
| 304 | } |
| 305 | const std::string& result = oss.str(); |
| 306 | if (sscanf(result.c_str(), "{ %lf %lf %lf }", |
| 307 | &m.loadavg_1m, &m.loadavg_5m, &m.loadavg_15m) != 3) { |
| 308 | PLOG(WARNING) << "Fail to sscanf"; |
| 309 | return false; |
| 310 | } |
| 311 | return true; |
| 312 | |
| 313 | #else |
| 314 | return false; |
| 315 | #endif |
| 316 | } |
| 317 | |
| 318 | class LoadAverageReader { |
| 319 | public: |
no test coverage detected