2 * collectd - src/utils_taskstats.c
3 * Copyright (C) 2017 Florian octo Forster
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Florian octo Forster <octo at collectd.org>
24 #include "utils_taskstats.h"
28 #include "utils_time.h"
30 #include <libmnl/libmnl.h>
31 #include <linux/genetlink.h>
32 #include <linux/taskstats.h>
35 struct mnl_socket *nl;
38 uint16_t genl_id_taskstats;
42 /* nlmsg_errno returns the errno encoded in nlh or zero if not an error. */
43 static int nlmsg_errno(struct nlmsghdr *nlh, size_t sz) {
44 if (!mnl_nlmsg_ok(nlh, (int)sz)) {
45 ERROR("utils_taskstats: mnl_nlmsg_ok failed.");
49 if (nlh->nlmsg_type != NLMSG_ERROR) {
53 struct nlmsgerr *nlerr = mnl_nlmsg_get_payload(nlh);
54 /* (struct nlmsgerr).error holds a negative errno. */
55 return nlerr->error * (-1);
58 static int get_taskstats_attr_cb(const struct nlattr *attr, void *data) {
59 struct taskstats *ret_taskstats = data;
61 uint16_t type = mnl_attr_get_type(attr);
63 case TASKSTATS_TYPE_STATS:
64 if (mnl_attr_get_payload_len(attr) != sizeof(*ret_taskstats)) {
65 ERROR("utils_taskstats: mnl_attr_get_payload_len(attr) = %" PRIu32
67 mnl_attr_get_payload_len(attr), sizeof(*ret_taskstats));
70 struct taskstats *ts = mnl_attr_get_payload(attr);
71 memmove(ret_taskstats, ts, sizeof(*ret_taskstats));
74 case TASKSTATS_TYPE_AGGR_PID: /* fall through */
75 case TASKSTATS_TYPE_AGGR_TGID:
76 return mnl_attr_parse_nested(attr, get_taskstats_attr_cb, ret_taskstats);
78 case TASKSTATS_TYPE_PID: /* fall through */
79 case TASKSTATS_TYPE_TGID:
84 DEBUG("utils_taskstats: unknown attribute %" PRIu16
85 ", want one of TASKSTATS_TYPE_AGGR_PID/TGID, TASKSTATS_TYPE_STATS",
91 static int get_taskstats_msg_cb(const struct nlmsghdr *nlh, void *data) {
92 return mnl_attr_parse(nlh, sizeof(struct genlmsghdr), get_taskstats_attr_cb,
96 static int get_taskstats(ts_t *ts, uint32_t tgid,
97 struct taskstats *ret_taskstats) {
98 char buffer[MNL_SOCKET_BUFFER_SIZE];
99 uint32_t seq = ts->seq++;
101 struct nlmsghdr *nlh = mnl_nlmsg_put_header(buffer);
102 *nlh = (struct nlmsghdr){
103 .nlmsg_len = nlh->nlmsg_len,
104 .nlmsg_type = ts->genl_id_taskstats,
105 .nlmsg_flags = NLM_F_REQUEST,
107 .nlmsg_pid = ts->pid,
110 struct genlmsghdr *genh = mnl_nlmsg_put_extra_header(nlh, sizeof(*genh));
111 *genh = (struct genlmsghdr){
112 .cmd = TASKSTATS_CMD_GET,
113 .version = TASKSTATS_GENL_VERSION, // or TASKSTATS_VERSION?
116 // mnl_attr_put_u32(nlh, TASKSTATS_CMD_ATTR_PID, tgid);
117 mnl_attr_put_u32(nlh, TASKSTATS_CMD_ATTR_TGID, tgid);
119 if (mnl_socket_sendto(ts->nl, nlh, nlh->nlmsg_len) < 0) {
121 ERROR("utils_taskstats: mnl_socket_sendto() = %s", STRERROR(status));
125 int status = mnl_socket_recvfrom(ts->nl, buffer, sizeof(buffer));
128 ERROR("utils_taskstats: mnl_socket_recvfrom() = %s", STRERROR(status));
130 } else if (status == 0) {
131 ERROR("utils_taskstats: mnl_socket_recvfrom() = 0");
134 size_t buffer_size = (size_t)status;
136 if ((status = nlmsg_errno((void *)buffer, buffer_size)) != 0) {
137 ERROR("utils_taskstats: TASKSTATS_CMD_GET(TASKSTATS_CMD_ATTR_TGID = "
139 (uint32_t)tgid, STRERROR(status));
143 status = mnl_cb_run(buffer, buffer_size, seq, ts->port_id,
144 get_taskstats_msg_cb, ret_taskstats);
145 if (status < MNL_CB_STOP) {
146 ERROR("utils_taskstats: Parsing message failed.");
153 static int get_family_id_attr_cb(const struct nlattr *attr, void *data) {
154 uint16_t type = mnl_attr_get_type(attr);
155 if (type != CTRL_ATTR_FAMILY_ID) {
159 if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0) {
160 ERROR("mnl_attr_validate() = %s", STRERRNO);
164 uint16_t *ret_family_id = data;
165 *ret_family_id = mnl_attr_get_u16(attr);
169 static int get_family_id_msg_cb(const struct nlmsghdr *nlh, void *data) {
170 return mnl_attr_parse(nlh, sizeof(struct genlmsghdr), get_family_id_attr_cb,
174 /* get_family_id initializes ts->genl_id_taskstats. Returns 0 on success and
175 * an error code otherwise. */
176 static int get_family_id(ts_t *ts) {
177 char buffer[MNL_SOCKET_BUFFER_SIZE];
178 uint32_t seq = ts->seq++;
180 struct nlmsghdr *nlh = mnl_nlmsg_put_header(buffer);
181 *nlh = (struct nlmsghdr){
182 .nlmsg_len = nlh->nlmsg_len,
183 .nlmsg_type = GENL_ID_CTRL,
184 .nlmsg_flags = NLM_F_REQUEST,
186 .nlmsg_pid = ts->pid,
189 struct genlmsghdr *genh = mnl_nlmsg_put_extra_header(nlh, sizeof(*genh));
190 *genh = (struct genlmsghdr){
191 .cmd = CTRL_CMD_GETFAMILY, .version = 0x01,
194 mnl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, TASKSTATS_GENL_NAME);
196 assert(genh->cmd == CTRL_CMD_GETFAMILY);
197 assert(genh->version == TASKSTATS_GENL_VERSION);
199 if (mnl_socket_sendto(ts->nl, nlh, nlh->nlmsg_len) < 0) {
201 ERROR("utils_taskstats: mnl_socket_sendto() = %s", STRERROR(status));
205 ts->genl_id_taskstats = 0;
207 int status = mnl_socket_recvfrom(ts->nl, buffer, sizeof(buffer));
210 ERROR("utils_taskstats: mnl_socket_recvfrom() = %s", STRERROR(status));
212 } else if (status == 0) {
215 size_t buffer_size = (size_t)status;
217 if ((status = nlmsg_errno((void *)buffer, buffer_size)) != 0) {
218 ERROR("utils_taskstats: CTRL_CMD_GETFAMILY(\"%s\"): %s",
219 TASKSTATS_GENL_NAME, STRERROR(status));
223 status = mnl_cb_run(buffer, buffer_size, seq, ts->port_id,
224 get_family_id_msg_cb, &ts->genl_id_taskstats);
225 if (status < MNL_CB_STOP) {
226 ERROR("utils_taskstats: Parsing message failed.");
228 } else if (status == MNL_CB_STOP) {
233 if (ts->genl_id_taskstats == 0) {
234 ERROR("utils_taskstats: Netlink communication succeeded, but "
235 "genl_id_taskstats is still zero.");
242 void ts_destroy(ts_t *ts) {
247 if (ts->nl != NULL) {
248 mnl_socket_close(ts->nl);
255 ts_t *ts_create(void) {
256 ts_t *ts = calloc(1, sizeof(*ts));
258 ERROR("utils_taskstats: calloc failed: %s", STRERRNO);
262 if ((ts->nl = mnl_socket_open(NETLINK_GENERIC)) == NULL) {
263 ERROR("utils_taskstats: mnl_socket_open(NETLINK_GENERIC) = %s", STRERRNO);
268 if (mnl_socket_bind(ts->nl, 0, MNL_SOCKET_AUTOPID) != 0) {
269 ERROR("utils_taskstats: mnl_socket_bind() = %s", STRERRNO);
275 ts->port_id = mnl_socket_get_portid(ts->nl);
277 int status = get_family_id(ts);
279 ERROR("utils_taskstats: get_family_id() = %s", STRERROR(status));
287 int ts_delay_by_tgid(ts_t *ts, uint32_t tgid, ts_delay_t *out) {
288 if ((ts == NULL) || (out == NULL)) {
292 struct taskstats raw = {0};
294 int status = get_taskstats(ts, tgid, &raw);
300 .cpu_ns = raw.cpu_delay_total,
301 .blkio_ns = raw.blkio_delay_total,
302 .swapin_ns = raw.swapin_delay_total,
303 .freepages_ns = raw.freepages_delay_total,