MCPcopy Create free account
hub / github.com/apache/brpc / read_load_average

Function read_load_average

src/bvar/default_variables.cpp:284–316  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

282};
283
284static 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
318class LoadAverageReader {
319public:

Callers 1

operator()Method · 0.85

Calls 3

LoadAverageClass · 0.85
read_command_outputFunction · 0.85
c_strMethod · 0.45

Tested by

no test coverage detected