| 336 | // ================================================== |
| 337 | |
| 338 | static 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 | |
| 379 | extern PassiveStatus<int> g_fd_num; |
| 380 |
no test coverage detected