diff options
author | Tor Egge <Tor.Egge@broadpark.no> | 2020-04-15 22:18:18 +0200 |
---|---|---|
committer | Tor Egge <Tor.Egge@broadpark.no> | 2020-04-15 22:32:25 +0200 |
commit | ba77feb3690d4fe7df79a3565c169e0c8a33eae0 (patch) | |
tree | f7bcd51aa1e7ed30ce10a21ecd464531da2bc66e /fastos | |
parent | 3af346b53ecb0acf0512636d84cfa66f3317a7dd (diff) |
Move function to count open files to fastos.
Count open files on Darwin.
Diffstat (limited to 'fastos')
-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 |
4 files changed, 41 insertions, 0 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> |