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

Function get_fd_count

src/bvar/default_variables.cpp:338–377  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

336// ==================================================
337
338static int get_fd_count(int limit) {
339#if defined(OS_LINUX)
340 butil::DirReaderPosix dr("/proc/self/fd");
341 int count = 0;
342 if (!dr.IsValid()) {
343 PLOG(WARNING) << "Fail to open /proc/self/fd";
344 return -1;
345 }
346 // Have to limit the scaning which consumes a lot of CPU when #fd
347 // are huge (100k+)
348 for (; dr.Next() && count <= limit + 3; ++count) {}
349 return count - 3 /* skipped ., .. and the fd in dr*/;
350#elif defined(OS_MACOSX)
351 // TODO(zhujiashun): following code will cause core dump with some
352 // probability under mac when program exits. Fix it.
353 /*
354 static pid_t pid = getpid();
355 std::ostringstream oss;
356 char cmdbuf[128];
357 snprintf(cmdbuf, sizeof(cmdbuf),
358 "lsof -p %ld | grep -v \"txt\" | wc -l", (long)pid);
359 if (butil::read_command_output(oss, cmdbuf) != 0) {
360 LOG(ERROR) << "Fail to read open files";
361 return -1;
362 }
363 const std::string& result = oss.str();
364 int count = 0;
365 if (sscanf(result.c_str(), "%d", &count) != 1) {
366 PLOG(WARNING) << "Fail to sscanf";
367 return -1;
368 }
369 // skipped . and first column line
370 count = count - 2;
371 return std::min(count, limit);
372 */
373 return 0;
374#else
375 return 0;
376#endif
377}
378
379extern PassiveStatus<int> g_fd_num;
380

Callers 1

operator()Method · 0.85

Calls 2

IsValidMethod · 0.45
NextMethod · 0.45

Tested by

no test coverage detected