LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 06-08-2015, 03:28 AM   #1
bkk87
LQ Newbie
 
Registered: May 2015
Posts: 3

Rep: Reputation: Disabled
Using Netlink to get Taskstats data (libnl-3)


Hey, I want to read out linux kernel statistics of a single thread using netlink socket and taskstats.

I could get taskstats to work using a python wrapper (https://github.com/facebook/gnlpy) but I want to do a C implementation.

After setting up the socket, the message parameters and do the sending, the receiving

Code:
nl_recvmsgs_default(sock)
always returns an error code -7 ("Invalid input data or parameter") or -12 ("Object not found") depending on how I create the message to send.

I checked all method invocations before nl_recvmsgs_default(sock) but dont get any error back. I guess I am missing a part in setting up the message or socket, but dont know what part it is.

Quote:
#include <stdlib.h>
#include <unistd.h>
#include <linux/taskstats.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>

int callback_message(struct nl_msg *, void *);

int main(int argc, char ** argv) {
struct nl_sock * sock;
struct nl_msg * msg;
int family;

sock = nl_socket_alloc();

// Connect to generic netlink socket on kernel side
genl_connect(sock);

// get the id for the TASKSTATS generic family
family = genl_ctrl_resolve(sock, "TASKSTATS");

// Allocate a new netlink message and inherit netlink message header.
msg = nlmsg_alloc();

genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, 0, TASKSTATS_CMD_GET, TASKSTATS_VERSION))

//error code: -7 NLE_INVAL "Invalid input data or parameter",
nla_put_string(msg, TASKSTATS_CMD_ATTR_REGISTER_CPUMASK, "0");

//error code: -12 NLE_OBJ_NOTFOUND "Obj not found"
//nla_put_string(msg, TASKSTATS_CMD_ATTR_PID, "583");

nl_send_auto(sock, msg);

nlmsg_free(msg);

// specify a callback for inbound messages
nl_socket_modify_cb(sock, NL_CB_MSG_IN, NL_CB_CUSTOM, callback_message, NULL);

// gives error code -7 or -12 depending on the two nla_put_string alternatives above
printf("recv code (0 = success): d", nl_recvmsgs_default(sock));
}
Quote:
int callback_message(struct nl_msg * nlmsg, void * arg) {

struct nlmsghdr * nlhdr;
struct nlattr * nlattrs[TASKSTATS_TYPE_MAX + 1];
struct nlattr * nlattr;
struct taskstats * stats;
int rem;

nlhdr = nlmsg_hdr(nlmsg);
int answer;

if ((answer = genlmsg_parse(nlhdr, 0, nlattrs, TASKSTATS_TYPE_MAX, NULL))
< 0) {
printf("error parsing msg\n");
}

if ((nlattr = nlattrs[TASKSTATS_TYPE_AGGR_TGID]) || (nlattr =
nlattrs[TASKSTATS_TYPE_AGGR_PID]) || (nlattr =
nlattrs[TASKSTATS_TYPE_NULL])) {
stats = nla_data(nla_next(nla_data(nlattr), &rem));

printf("---\n");
printf("pid: %u\n", stats->ac_pid);
printf("command: %s\n", stats->ac_comm);
printf("status: %u\n", stats->ac_exitcode);
printf("time:\n");
printf(" start: %u\n", stats->ac_btime);
printf(" elapsed: %llu\n", stats->ac_etime);
printf(" user: %llu\n", stats->ac_utime);
printf(" system: %llu\n", stats->ac_stime);
printf("memory:\n");
printf(" bytetime:\n");
printf(" rss: %llu\n", stats->coremem);
printf(" vsz: %llu\n", stats->virtmem);
printf(" peak:\n");
printf(" rss: %llu\n", stats->hiwater_rss);
printf(" vsz: %llu\n", stats->hiwater_vm);
printf("io:\n");
printf(" bytes:\n");
printf(" read: %llu\n", stats->read_char);
printf(" write: %llu\n", stats->write_char);
printf(" syscalls:\n");
printf(" read: %llu\n", stats->read_syscalls);
printf(" write: %llu\n", stats->write_syscalls);
} else {
printf("unknown attribute format received\n");
}
return 0;
}
Does someone of you have a hint what I am doing wrong or what is missing to set up a correct netlink/taskstats message?

Thanks in advance, bkk

Last edited by bkk87; 06-10-2015 at 06:51 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wi-Fi scan or ProbeRequest & ProbeResponse using libnl library yfaney Linux - Wireless Networking 0 01-29-2015 02:45 AM
[SOLVED] Building hostapd for nl80211 driver with libnl-1.1 Darshan P Linux - Software 0 10-31-2011 02:26 AM
help w/ netlink communications rrlangly Linux - Kernel 1 04-06-2010 11:46 PM
Netlink Example for Linux 2.6 saurabhchokshi Programming 3 07-18-2009 02:48 AM
netlink documentation alaios Programming 1 08-17-2005 01:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 09:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration