ClickHouse/src/Common/getCurrentProcessFDCount.cpp
Azat Khuzhin 4e76629aaf Fixes for -Wshorten-64-to-32
- lots of static_cast
- add safe_cast
- types adjustments
  - config
  - IStorage::read/watch
  - ...
- some TODO's (to convert types in future)

P.S. That was quite a journey...

v2: fixes after rebase
v3: fix conflicts after #42308 merged
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
2022-10-21 13:25:19 +02:00

47 lines
1.1 KiB
C++

#include <Common/getCurrentProcessFDCount.h>
#include <Common/ShellCommand.h>
#include <IO/WriteBufferFromString.h>
#include <unistd.h>
#include <fmt/format.h>
#include <IO/ReadHelpers.h>
#include <filesystem>
Int64 getCurrentProcessFDCount()
{
namespace fs = std::filesystem;
Int64 result = -1;
#if defined(OS_LINUX) || defined(OS_DARWIN)
using namespace DB;
Int32 pid = getpid();
auto proc_fs_path = fmt::format("/proc/{}/fd", pid);
if (fs::exists(proc_fs_path))
{
result = std::distance(fs::directory_iterator(proc_fs_path), fs::directory_iterator{});
}
else if (fs::exists("/dev/fd"))
{
result = std::distance(fs::directory_iterator("/dev/fd"), fs::directory_iterator{});
}
else
{
/// Then try lsof command
String by_lsof = fmt::format("lsof -p {} | wc -l", pid);
auto command = ShellCommand::execute(by_lsof);
try
{
readIntText(result, command->out);
command->wait();
}
catch (...)
{
}
}
#endif
return result;
}