Commit 9740ea9d authored by gejun's avatar gejun

r35260,r35264: Use DirReaderPosix instead of FileEnumerator in get_fd_count to…

r35260,r35264: Use DirReaderPosix instead of FileEnumerator in get_fd_count to avoid the cost of uselss stat()
parent 39869c13
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
#include "butil/memory/singleton_on_pthread_once.h" #include "butil/memory/singleton_on_pthread_once.h"
#include "butil/scoped_lock.h" #include "butil/scoped_lock.h"
#include "butil/files/scoped_file.h" #include "butil/files/scoped_file.h"
#include "butil/files/file_enumerator.h" #include "butil/files/dir_reader_posix.h"
#include "butil/file_util.h" #include "butil/file_util.h"
#include "bvar/passive_status.h" #include "bvar/passive_status.h"
...@@ -249,18 +249,16 @@ public: ...@@ -249,18 +249,16 @@ public:
// ================================================== // ==================================================
static int get_fd_count(int limit) { static int get_fd_count(int limit) {
butil::FileEnumerator fd_enum(butil::FilePath("/proc/self/fd"), butil::DirReaderPosix dr("/proc/self/fd");
false/*non recursive*/,
butil::FileEnumerator::FILES);
int count = 0; int count = 0;
if (!dr.IsValid()) {
PLOG(WARNING) << "Fail to open /proc/self/fd";
return -1;
}
// Have to limit the scaning which consumes a lot of CPU when #fd // Have to limit the scaning which consumes a lot of CPU when #fd
// are huge (100k+) // are huge (100k+)
for (butil::FilePath name = fd_enum.Next(); for (; dr.Next() && count <= limit + 3; ++count) {}
!name.empty() && count <= limit; return count - 3 /* skipped ., .. and the fd in dr*/;
name = fd_enum.Next(), ++count) {}
// FileEnumerator already filtered . and .., due to its implementation,
// the fd created by opendir is not counted as well.
return count;
} }
extern PassiveStatus<int> g_fd_num; extern PassiveStatus<int> g_fd_num;
......
...@@ -242,7 +242,7 @@ friend class AddLatency; ...@@ -242,7 +242,7 @@ friend class AddLatency;
static const size_t SAMPLE_SIZE = SAMPLE_SIZE_IN; static const size_t SAMPLE_SIZE = SAMPLE_SIZE_IN;
PercentileSamples() { PercentileSamples() {
memset(this, 0, sizeof(PercentileSamples)); memset(this, 0, sizeof(*this));
} }
~PercentileSamples() { ~PercentileSamples() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment