diff options
-rw-r--r-- | fastos/src/vespa/fastos/linux_file.cpp | 24 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/linux_file.h | 1 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/unix_file.cpp | 15 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/unix_file.h | 1 | ||||
-rw-r--r-- | searchcore/src/vespa/searchcore/proton/server/proton.cpp | 21 |
5 files changed, 42 insertions, 20 deletions
diff --git a/fastos/src/vespa/fastos/linux_file.cpp b/fastos/src/vespa/fastos/linux_file.cpp index 56d9e246a97..0f91a8ff41c 100644 --- a/fastos/src/vespa/fastos/linux_file.cpp +++ b/fastos/src/vespa/fastos/linux_file.cpp @@ -13,6 +13,9 @@ #include <unistd.h> #include <fcntl.h> #include "file_rw_ops.h" +#include <cstdio> +#include <cstring> +#include <system_error> using fastos::File_RW_Ops; @@ -431,6 +434,27 @@ FastOS_Linux_File::InitializeClass() return FastOS_UNIX_File::InitializeClass(); } +int +FastOS_Linux_File::count_open_files() +{ + static const char * const fd_dir_name = "/proc/self/fd"; + int count = 0; + DIR *dp = opendir(fd_dir_name); + if (dp != nullptr) { + struct dirent *ptr; + while ((ptr = readdir(dp)) != nullptr) { + if ((strcmp(".", ptr->d_name) != 0) && (strcmp("..", ptr->d_name) != 0)) { + ++count; + } + } + closedir(dp); + } else { + std::error_code ec(errno, std::system_category()); + fprintf(stderr, "could not scan directory %s: %s\n", fd_dir_name, ec.message().c_str()); + } + return count; +} + #include <vespa/fastos/backtrace.h> void forceStaticLinkOf_backtrace() diff --git a/fastos/src/vespa/fastos/linux_file.h b/fastos/src/vespa/fastos/linux_file.h index e304b6f0ce8..06f54de6870 100644 --- a/fastos/src/vespa/fastos/linux_file.h +++ b/fastos/src/vespa/fastos/linux_file.h @@ -45,6 +45,7 @@ public: static bool InitializeClass(); static size_t getMaxDirectIOMemAlign(); static void *allocateGenericDirectIOBuffer(size_t byteSize, void *&realPtr); + static int count_open_files(); private: ssize_t internalWrite2(const void *buffer, size_t len); ssize_t readUnalignedEnd(void *buffer, size_t length, int64_t readOffset); diff --git a/fastos/src/vespa/fastos/unix_file.cpp b/fastos/src/vespa/fastos/unix_file.cpp index 06a48a26482..ef52d34e49f 100644 --- a/fastos/src/vespa/fastos/unix_file.cpp +++ b/fastos/src/vespa/fastos/unix_file.cpp @@ -21,6 +21,10 @@ #else #include <sys/mount.h> #endif +#ifdef __APPLE__ +#include <libproc.h> +#include <sys/proc_info.h> +#endif #include "file_rw_ops.h" using fastos::File_RW_Ops; @@ -498,6 +502,17 @@ int64_t FastOS_UNIX_File::GetFreeDiskSpace (const char *path) return freeSpace; } +int +FastOS_UNIX_File::count_open_files() +{ +#ifdef __APPLE__ + int buffer_size = proc_pidinfo(getpid(), PROC_PIDLISTFDS, 0, nullptr, 0); + return buffer_size / sizeof(proc_fdinfo); +#else + return 0; +#endif +} + FastOS_UNIX_DirectoryScan::FastOS_UNIX_DirectoryScan(const char *searchPath) : FastOS_DirectoryScanInterface(searchPath), _statRun(false), diff --git a/fastos/src/vespa/fastos/unix_file.h b/fastos/src/vespa/fastos/unix_file.h index 3dffe1fc089..3c3ffedc171 100644 --- a/fastos/src/vespa/fastos/unix_file.h +++ b/fastos/src/vespa/fastos/unix_file.h @@ -96,6 +96,7 @@ public: static Error TranslateError(const int osError); static std::string getErrorString(const int osError); static int64_t GetFreeDiskSpace (const char *path); + static int count_open_files(); }; #include <dirent.h> diff --git a/searchcore/src/vespa/searchcore/proton/server/proton.cpp b/searchcore/src/vespa/searchcore/proton/server/proton.cpp index 1ab1ba72b4f..c886b371064 100644 --- a/searchcore/src/vespa/searchcore/proton/server/proton.cpp +++ b/searchcore/src/vespa/searchcore/proton/server/proton.cpp @@ -685,25 +685,6 @@ Proton::prepareRestart() namespace { -int countOpenFiles() -{ - static const char * const fd_dir_name = "/proc/self/fd"; - int count = 0; - DIR *dp = opendir(fd_dir_name); - if (dp != nullptr) { - struct dirent *ptr; - while ((ptr = readdir(dp)) != nullptr) { - if (strcmp(".", ptr->d_name) == 0) continue; - if (strcmp("..", ptr->d_name) == 0) continue; - ++count; - } - closedir(dp); - } else { - LOG(warning, "could not scan directory %s: %s", fd_dir_name, strerror(errno)); - } - return count; -} - void updateExecutorMetrics(ExecutorMetrics &metrics, const vespalib::ThreadStackExecutor::Stats &stats) @@ -730,7 +711,7 @@ Proton::updateMetrics(const vespalib::MonitorGuard &) metrics.resourceUsage.memory.set(usageState.memoryState().usage()); metrics.resourceUsage.memoryUtilization.set(usageState.memoryState().utilization()); metrics.resourceUsage.memoryMappings.set(usageFilter.getMemoryStats().getMappingsCount()); - metrics.resourceUsage.openFileDescriptors.set(countOpenFiles()); + metrics.resourceUsage.openFileDescriptors.set(FastOS_File::count_open_files()); metrics.resourceUsage.feedingBlocked.set((usageFilter.acceptWriteOperation() ? 0.0 : 1.0)); } { |