2018-11-14 01:11:13 +00:00
|
|
|
#include "TaskStatsInfoGetter.h"
|
2018-05-17 16:01:41 +00:00
|
|
|
#include <Common/Exception.h>
|
2023-02-06 19:12:24 +00:00
|
|
|
#include <base/defines.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2018-05-17 16:01:41 +00:00
|
|
|
|
2018-08-22 05:56:06 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-09-15 10:35:12 +00:00
|
|
|
#if defined(OS_LINUX)
|
2018-08-22 05:56:06 +00:00
|
|
|
|
2018-11-14 01:11:13 +00:00
|
|
|
#include "hasLinuxCapability.h"
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/unaligned.h>
|
2022-11-21 14:16:27 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2018-08-22 02:54:24 +00:00
|
|
|
|
2022-05-08 17:01:47 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2018-05-17 16:01:41 +00:00
|
|
|
#include <sys/socket.h>
|
2018-08-22 05:56:06 +00:00
|
|
|
#include <linux/genetlink.h>
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/taskstats.h>
|
|
|
|
#include <linux/capability.h>
|
2018-05-17 16:01:41 +00:00
|
|
|
|
2020-05-09 23:35:20 +00:00
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
|
2022-07-14 11:42:25 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wnested-anon-types"
|
2020-05-09 23:35:20 +00:00
|
|
|
#endif
|
2018-08-21 21:05:30 +00:00
|
|
|
|
2018-08-19 04:43:58 +00:00
|
|
|
/// Basic idea is motivated by "iotop" tool.
|
2018-06-04 14:16:27 +00:00
|
|
|
/// More info: https://www.kernel.org/doc/Documentation/accounting/taskstats.txt
|
2018-05-17 16:01:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NETLINK_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:16:36 +00:00
|
|
|
// Replace NLMSG_OK with explicit casts since that system macro contains signedness bugs which are not going to be fixed.
|
|
|
|
static inline bool is_nlmsg_ok(const struct nlmsghdr * const nlh, const ssize_t len)
|
|
|
|
{
|
|
|
|
return len >= static_cast<ssize_t>(sizeof(*nlh)) && nlh->nlmsg_len >= sizeof(*nlh) && static_cast<size_t>(len) >= nlh->nlmsg_len;
|
|
|
|
}
|
2018-05-17 16:01:41 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
/** The message contains:
|
|
|
|
* - Netlink protocol header;
|
|
|
|
* - Generic Netlink (is a sub-protocol of Netlink that we use) protocol header;
|
|
|
|
* - Payload
|
|
|
|
* -- that itself is a list of "Attributes" (sub-messages), each of them contains length (including header), type, and its own payload.
|
|
|
|
* -- and attribute payload may be represented by the list of embedded attributes.
|
|
|
|
*/
|
2018-06-04 14:16:27 +00:00
|
|
|
struct NetlinkMessage
|
2018-05-17 16:01:41 +00:00
|
|
|
{
|
2018-08-22 03:11:46 +00:00
|
|
|
static size_t constexpr MAX_MSG_SIZE = 1024;
|
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
alignas(NLMSG_ALIGNTO) ::nlmsghdr header;
|
2018-05-17 16:01:41 +00:00
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
struct Attribute
|
|
|
|
{
|
|
|
|
::nlattr header;
|
2018-05-17 16:01:41 +00:00
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
alignas(NLMSG_ALIGNTO) char payload[0];
|
|
|
|
|
|
|
|
const Attribute * next() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const Attribute *>(reinterpret_cast<const char *>(this) + NLA_ALIGN(header.nla_len));
|
|
|
|
}
|
|
|
|
};
|
2018-05-17 16:01:41 +00:00
|
|
|
|
2018-08-22 03:12:55 +00:00
|
|
|
union alignas(NLMSG_ALIGNTO)
|
2018-08-22 02:54:24 +00:00
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
::genlmsghdr generic_header;
|
2018-05-17 16:01:41 +00:00
|
|
|
|
2018-08-22 03:12:55 +00:00
|
|
|
union alignas(NLMSG_ALIGNTO)
|
2018-08-22 02:54:24 +00:00
|
|
|
{
|
|
|
|
char buf[MAX_MSG_SIZE];
|
|
|
|
Attribute attribute; /// First attribute. There may be more.
|
|
|
|
} payload;
|
|
|
|
};
|
2018-05-17 16:01:41 +00:00
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
::nlmsgerr error;
|
|
|
|
};
|
2018-05-17 16:01:41 +00:00
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
const Attribute * end() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const Attribute *>(reinterpret_cast<const char *>(this) + header.nlmsg_len);
|
|
|
|
}
|
2018-06-04 14:16:27 +00:00
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
void send(int fd) const
|
2018-05-17 16:01:41 +00:00
|
|
|
{
|
2018-08-22 02:54:24 +00:00
|
|
|
const char * request_buf = reinterpret_cast<const char *>(this);
|
|
|
|
ssize_t request_size = header.nlmsg_len;
|
2018-06-04 14:16:27 +00:00
|
|
|
|
2019-06-16 18:12:14 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
::sockaddr_nl nladdr{};
|
|
|
|
::sockaddr sockaddr;
|
|
|
|
};
|
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
nladdr.nl_family = AF_NETLINK;
|
2018-06-04 14:16:27 +00:00
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
while (true)
|
2018-05-17 16:01:41 +00:00
|
|
|
{
|
2019-06-16 18:12:14 +00:00
|
|
|
ssize_t bytes_sent = ::sendto(fd, request_buf, request_size, 0, &sockaddr, sizeof(nladdr));
|
2018-08-22 02:54:24 +00:00
|
|
|
|
|
|
|
if (bytes_sent <= 0)
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
throwFromErrno("Can't send a Netlink command", ErrorCodes::NETLINK_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes_sent > request_size)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NETLINK_ERROR, "Wrong result of sendto system call: bytes_sent is greater than request size");
|
2018-08-22 02:54:24 +00:00
|
|
|
|
|
|
|
if (bytes_sent == request_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
request_buf += bytes_sent;
|
|
|
|
request_size -= bytes_sent;
|
2018-05-17 16:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-22 02:54:24 +00:00
|
|
|
|
|
|
|
void receive(int fd)
|
|
|
|
{
|
|
|
|
ssize_t bytes_received = ::recv(fd, this, sizeof(*this), 0);
|
|
|
|
|
2018-08-29 21:30:39 +00:00
|
|
|
if (header.nlmsg_type == NLMSG_ERROR)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NETLINK_ERROR, "Can't receive Netlink response: error {}", std::to_string(error.error));
|
2018-08-29 21:30:39 +00:00
|
|
|
|
2019-07-18 18:24:38 +00:00
|
|
|
if (!is_nlmsg_ok(&header, bytes_received))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NETLINK_ERROR, "Can't receive Netlink response: wrong number of bytes received");
|
2018-08-22 02:54:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
NetlinkMessage query(
|
|
|
|
int fd,
|
|
|
|
UInt16 type,
|
|
|
|
UInt32 pid,
|
|
|
|
UInt8 command,
|
|
|
|
UInt16 attribute_type,
|
|
|
|
const void * attribute_data,
|
|
|
|
int attribute_size)
|
|
|
|
{
|
2018-09-16 00:25:25 +00:00
|
|
|
NetlinkMessage request{};
|
2018-08-22 02:54:24 +00:00
|
|
|
|
|
|
|
request.header.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); /// Length of both headers.
|
|
|
|
request.header.nlmsg_type = type;
|
|
|
|
request.header.nlmsg_flags = NLM_F_REQUEST; /// A request.
|
|
|
|
request.header.nlmsg_seq = 0;
|
|
|
|
request.header.nlmsg_pid = pid;
|
|
|
|
|
|
|
|
request.generic_header.cmd = command;
|
|
|
|
request.generic_header.version = 1;
|
|
|
|
|
|
|
|
request.payload.attribute.header.nla_type = attribute_type;
|
2018-12-04 12:18:37 +00:00
|
|
|
request.payload.attribute.header.nla_len = attribute_size + NLA_HDRLEN;
|
2018-08-22 02:54:24 +00:00
|
|
|
|
|
|
|
memcpy(&request.payload.attribute.payload, attribute_data, attribute_size);
|
|
|
|
|
|
|
|
request.header.nlmsg_len += NLMSG_ALIGN(request.payload.attribute.header.nla_len);
|
|
|
|
|
|
|
|
request.send(fd);
|
|
|
|
|
|
|
|
NetlinkMessage response;
|
|
|
|
response.receive(fd);
|
|
|
|
|
|
|
|
return response;
|
2018-05-17 16:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
UInt16 getFamilyIdImpl(int fd)
|
2018-05-17 16:01:41 +00:00
|
|
|
{
|
2018-08-22 02:54:24 +00:00
|
|
|
NetlinkMessage answer = query(fd, GENL_ID_CTRL, getpid(), CTRL_CMD_GETFAMILY, CTRL_ATTR_FAMILY_NAME, TASKSTATS_GENL_NAME, strlen(TASKSTATS_GENL_NAME) + 1);
|
|
|
|
|
|
|
|
/// NOTE Why the relevant info is located in the second attribute?
|
|
|
|
const NetlinkMessage::Attribute * attr = answer.payload.attribute.next();
|
|
|
|
|
|
|
|
if (attr->header.nla_type != CTRL_ATTR_FAMILY_ID)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NETLINK_ERROR, "Received wrong attribute as an answer to GET_FAMILY Netlink command");
|
2018-08-22 02:54:24 +00:00
|
|
|
|
|
|
|
return unalignedLoad<UInt16>(attr->payload);
|
2018-05-17 16:01:41 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:24:55 +00:00
|
|
|
|
|
|
|
bool checkPermissionsImpl()
|
|
|
|
{
|
2018-11-14 01:11:13 +00:00
|
|
|
static bool res = hasLinuxCapability(CAP_NET_ADMIN);
|
|
|
|
if (!res)
|
2018-08-29 21:30:39 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/// Check that we can successfully initialize TaskStatsInfoGetter.
|
|
|
|
/// It will ask about family id through Netlink.
|
|
|
|
/// On some LXC containers we have capability but we still cannot use Netlink.
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
TaskStatsInfoGetter();
|
|
|
|
}
|
2022-11-21 14:16:27 +00:00
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
if (e.code() == ErrorCodes::NETLINK_ERROR)
|
|
|
|
{
|
|
|
|
/// This error happens all the time when running inside Docker - consider it ok,
|
|
|
|
/// don't create noise with this error.
|
2023-01-17 00:19:44 +00:00
|
|
|
LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), getCurrentExceptionMessageAndPattern(/* with_stacktrace */ false));
|
2022-11-21 14:16:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-29 21:30:39 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-06-14 14:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-22 02:54:24 +00:00
|
|
|
UInt16 getFamilyId(int fd)
|
2018-06-14 14:29:42 +00:00
|
|
|
{
|
2018-08-22 00:24:55 +00:00
|
|
|
/// It is thread and exception safe since C++11 and even before.
|
2018-08-22 02:54:24 +00:00
|
|
|
static UInt16 res = getFamilyIdImpl(fd);
|
2018-08-22 00:24:55 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TaskStatsInfoGetter::checkPermissions()
|
|
|
|
{
|
|
|
|
static bool res = checkPermissionsImpl();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TaskStatsInfoGetter::TaskStatsInfoGetter()
|
|
|
|
{
|
2018-06-14 14:29:42 +00:00
|
|
|
netlink_socket_fd = ::socket(PF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
|
|
|
|
if (netlink_socket_fd < 0)
|
2018-08-21 16:49:20 +00:00
|
|
|
throwFromErrno("Can't create PF_NETLINK socket", ErrorCodes::NETLINK_ERROR);
|
2018-08-21 16:40:54 +00:00
|
|
|
|
2022-08-04 03:37:27 +00:00
|
|
|
try
|
|
|
|
{
|
2022-08-04 04:34:36 +00:00
|
|
|
/// On some containerized environments, operation on Netlink socket could hang forever.
|
|
|
|
/// We set reasonably small timeout to overcome this issue.
|
2018-08-22 02:54:24 +00:00
|
|
|
|
2022-08-04 04:34:36 +00:00
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 50000;
|
2018-08-21 21:05:30 +00:00
|
|
|
|
2022-08-04 04:34:36 +00:00
|
|
|
if (0 != ::setsockopt(netlink_socket_fd, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char *>(&tv), sizeof(tv)))
|
|
|
|
throwFromErrno("Can't set timeout on PF_NETLINK socket", ErrorCodes::NETLINK_ERROR);
|
2018-06-14 14:29:42 +00:00
|
|
|
|
2022-08-04 04:34:36 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
::sockaddr_nl addr{};
|
|
|
|
::sockaddr sockaddr;
|
|
|
|
};
|
|
|
|
addr.nl_family = AF_NETLINK;
|
2018-06-14 14:29:42 +00:00
|
|
|
|
2022-08-04 04:34:36 +00:00
|
|
|
if (::bind(netlink_socket_fd, &sockaddr, sizeof(addr)) < 0)
|
|
|
|
throwFromErrno("Can't bind PF_NETLINK socket", ErrorCodes::NETLINK_ERROR);
|
2018-06-14 14:29:42 +00:00
|
|
|
|
2022-08-04 04:34:36 +00:00
|
|
|
taskstats_family_id = getFamilyId(netlink_socket_fd);
|
2022-08-04 03:37:27 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2022-08-04 04:34:36 +00:00
|
|
|
if (netlink_socket_fd >= 0)
|
2023-02-06 19:12:24 +00:00
|
|
|
{
|
|
|
|
int err = close(netlink_socket_fd);
|
|
|
|
chassert(!err || errno == EINTR);
|
|
|
|
}
|
2022-08-04 04:34:36 +00:00
|
|
|
throw;
|
2022-08-04 03:37:27 +00:00
|
|
|
}
|
2018-06-14 14:29:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 21:05:30 +00:00
|
|
|
|
2020-04-22 05:39:31 +00:00
|
|
|
void TaskStatsInfoGetter::getStat(::taskstats & out_stats, pid_t tid) const
|
2018-05-17 16:01:41 +00:00
|
|
|
{
|
2018-08-22 02:54:24 +00:00
|
|
|
NetlinkMessage answer = query(netlink_socket_fd, taskstats_family_id, tid, TASKSTATS_CMD_GET, TASKSTATS_CMD_ATTR_PID, &tid, sizeof(tid));
|
2018-05-17 16:01:41 +00:00
|
|
|
|
2022-04-17 10:50:06 +00:00
|
|
|
const NetlinkMessage::Attribute * attr = &answer.payload.attribute;
|
|
|
|
if (attr->header.nla_type != TASKSTATS_TYPE_AGGR_PID)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NETLINK_ERROR, "Expected TASKSTATS_TYPE_AGGR_PID");
|
2022-04-17 10:50:06 +00:00
|
|
|
|
|
|
|
/// TASKSTATS_TYPE_AGGR_PID
|
|
|
|
const NetlinkMessage::Attribute * nested_attr = reinterpret_cast<const NetlinkMessage::Attribute *>(attr->payload);
|
|
|
|
if (nested_attr->header.nla_type != TASKSTATS_TYPE_PID)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NETLINK_ERROR, "Expected TASKSTATS_TYPE_PID");
|
2022-04-17 10:50:06 +00:00
|
|
|
if (nested_attr == nested_attr->next())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NETLINK_ERROR, "No TASKSTATS_TYPE_STATS packet after TASKSTATS_TYPE_PID");
|
2022-04-17 10:50:06 +00:00
|
|
|
nested_attr = nested_attr->next();
|
|
|
|
if (nested_attr->header.nla_type != TASKSTATS_TYPE_STATS)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NETLINK_ERROR, "Expected TASKSTATS_TYPE_STATS");
|
2022-04-17 10:50:06 +00:00
|
|
|
|
|
|
|
out_stats = unalignedLoad<::taskstats>(nested_attr->payload);
|
|
|
|
|
|
|
|
if (attr->next() != answer.end())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NETLINK_ERROR, "Unexpected end of response");
|
2018-05-17 16:01:41 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 21:05:30 +00:00
|
|
|
|
|
|
|
TaskStatsInfoGetter::~TaskStatsInfoGetter()
|
|
|
|
{
|
|
|
|
if (netlink_socket_fd >= 0)
|
2023-02-06 19:12:24 +00:00
|
|
|
{
|
|
|
|
int err = close(netlink_socket_fd);
|
|
|
|
chassert(!err || errno == EINTR);
|
|
|
|
}
|
2018-08-21 21:05:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 16:01:41 +00:00
|
|
|
}
|
2018-08-22 05:56:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
bool TaskStatsInfoGetter::checkPermissions()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-11 12:01:58 +00:00
|
|
|
TaskStatsInfoGetter::TaskStatsInfoGetter() = default;
|
|
|
|
TaskStatsInfoGetter::~TaskStatsInfoGetter() = default;
|
2018-08-22 05:56:06 +00:00
|
|
|
|
2020-04-22 14:56:31 +00:00
|
|
|
void TaskStatsInfoGetter::getStat(::taskstats &, pid_t) const
|
2018-08-22 05:56:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|