JackDrogon commented on code in PR #26013:
URL: https://github.com/apache/doris/pull/26013#discussion_r1375607875


##########
be/src/util/doris_metrics.cpp:
##########
@@ -367,28 +367,31 @@ void DorisMetrics::_update_process_fd_num() {
     int64_t pid = getpid();
 
     // fd used
-    std::stringstream ss;
-    ss << "/proc/" << pid << "/fd/";
     int64_t count = 0;
-    auto cb = [&count](const io::FileInfo& file) -> bool {
-        count += 1;
-        return true;
-    };
-    Status st = io::global_local_filesystem()->iterate_directory(ss.str(), cb);
-    if (!st.ok()) {
-        LOG(WARNING) << "failed to count fd: " << st;
+    std::error_code ec;
+    std::filesystem::directory_iterator dict_iter(fmt::format("/proc/{}/fd/", 
pid), ec);
+    if (ec) {
+        LOG(WARNING) << "failed to count fd: " << ec.message();
         process_fd_num_used->set_value(0);
         return;
     }
+    for (const auto& entry : dict_iter) {
+        ec.clear();
+        // Ignore error_code checking, because the fd maybe was closed or file 
was deleted.
+        if (!entry.is_regular_file(ec)) {
+            continue;
+        }
+        ++count;
+    }
+
     process_fd_num_used->set_value(count);
 
+    auto limits = fmt::format("/proc/{}/limits", pid);

Review Comment:
   can use ‘/proc/self/limits’



##########
be/src/util/doris_metrics.cpp:
##########
@@ -367,28 +367,31 @@ void DorisMetrics::_update_process_fd_num() {
     int64_t pid = getpid();
 
     // fd used
-    std::stringstream ss;
-    ss << "/proc/" << pid << "/fd/";
     int64_t count = 0;
-    auto cb = [&count](const io::FileInfo& file) -> bool {
-        count += 1;
-        return true;
-    };
-    Status st = io::global_local_filesystem()->iterate_directory(ss.str(), cb);
-    if (!st.ok()) {
-        LOG(WARNING) << "failed to count fd: " << st;
+    std::error_code ec;
+    std::filesystem::directory_iterator dict_iter(fmt::format("/proc/{}/fd/", 
pid), ec);

Review Comment:
   use ‘/proc/self/fd/'



##########
be/src/util/doris_metrics.cpp:
##########
@@ -367,28 +367,31 @@ void DorisMetrics::_update_process_fd_num() {
     int64_t pid = getpid();
 
     // fd used
-    std::stringstream ss;
-    ss << "/proc/" << pid << "/fd/";
     int64_t count = 0;
-    auto cb = [&count](const io::FileInfo& file) -> bool {
-        count += 1;
-        return true;
-    };
-    Status st = io::global_local_filesystem()->iterate_directory(ss.str(), cb);
-    if (!st.ok()) {
-        LOG(WARNING) << "failed to count fd: " << st;
+    std::error_code ec;
+    std::filesystem::directory_iterator dict_iter(fmt::format("/proc/{}/fd/", 
pid), ec);

Review Comment:
   use ‘/proc/self/fd/'



##########
be/src/util/doris_metrics.cpp:
##########
@@ -367,28 +367,31 @@ void DorisMetrics::_update_process_fd_num() {
     int64_t pid = getpid();
 
     // fd used
-    std::stringstream ss;
-    ss << "/proc/" << pid << "/fd/";
     int64_t count = 0;
-    auto cb = [&count](const io::FileInfo& file) -> bool {
-        count += 1;
-        return true;
-    };
-    Status st = io::global_local_filesystem()->iterate_directory(ss.str(), cb);
-    if (!st.ok()) {
-        LOG(WARNING) << "failed to count fd: " << st;
+    std::error_code ec;
+    std::filesystem::directory_iterator dict_iter(fmt::format("/proc/{}/fd/", 
pid), ec);
+    if (ec) {
+        LOG(WARNING) << "failed to count fd: " << ec.message();
         process_fd_num_used->set_value(0);
         return;
     }
+    for (const auto& entry : dict_iter) {

Review Comment:
   use count_if to replace this for loop



##########
be/src/util/doris_metrics.cpp:
##########
@@ -367,28 +367,31 @@ void DorisMetrics::_update_process_fd_num() {
     int64_t pid = getpid();
 
     // fd used
-    std::stringstream ss;
-    ss << "/proc/" << pid << "/fd/";
     int64_t count = 0;
-    auto cb = [&count](const io::FileInfo& file) -> bool {
-        count += 1;
-        return true;
-    };
-    Status st = io::global_local_filesystem()->iterate_directory(ss.str(), cb);
-    if (!st.ok()) {
-        LOG(WARNING) << "failed to count fd: " << st;
+    std::error_code ec;
+    std::filesystem::directory_iterator dict_iter(fmt::format("/proc/{}/fd/", 
pid), ec);
+    if (ec) {
+        LOG(WARNING) << "failed to count fd: " << ec.message();
         process_fd_num_used->set_value(0);
         return;
     }
+    for (const auto& entry : dict_iter) {
+        ec.clear();
+        // Ignore error_code checking, because the fd maybe was closed or file 
was deleted.
+        if (!entry.is_regular_file(ec)) {
+            continue;
+        }
+        ++count;
+    }
+
     process_fd_num_used->set_value(count);
 
+    auto limits = fmt::format("/proc/{}/limits", pid);

Review Comment:
   can use ‘/proc/self/limits’



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to