2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Linux task stats reporting tool. Queries and prints out the kernel's
19 * taskstats structure for a given process or thread group id. See
20 * https://www.kernel.org/doc/Documentation/accounting/ for more information
21 * about the reported fields.
26 #include <netlink-types.h>
27 #include <netlink/attr.h>
28 #include <netlink/genl/genl.h>
29 #include <netlink/handlers.h>
30 #include <netlink/msg.h>
36 #include <linux/taskstats.h>
38 struct TaskStatistics {
41 struct taskstats stats;
44 int send_command(struct nl_sock* netlink_socket, uint16_t nlmsg_type,
45 uint32_t nlmsg_pid, uint8_t genl_cmd, uint16_t nla_type,
46 void* nla_data, int nla_len) {
47 struct nl_msg* message = nlmsg_alloc();
50 int header_length = 0;
51 int flags = NLM_F_REQUEST;
52 genlmsg_put(message, nlmsg_pid, seq, nlmsg_type, header_length, flags,
54 nla_put(message, nla_type, nla_len, nla_data);
56 /* Override the header flags since we don't want NLM_F_ACK. */
57 struct nlmsghdr* header = nlmsg_hdr(message);
58 header->nlmsg_flags = flags;
60 int result = nl_send(netlink_socket, message);
65 int print_receive_error(struct sockaddr_nl* address, struct nlmsgerr* error,
67 fprintf(stderr, "Netlink receive error: %s\n", strerror(-error->error));
71 int parse_family_id(struct nl_msg* msg, void* arg) {
72 struct genlmsghdr* gnlh = (struct genlmsghdr*)nlmsg_data(nlmsg_hdr(msg));
73 struct nlattr* attr = genlmsg_attrdata(gnlh, 0);
74 int remaining = genlmsg_attrlen(gnlh, 0);
77 if (attr->nla_type == CTRL_ATTR_FAMILY_ID) {
78 *((int*)arg) = nla_get_u16(attr);
81 } while ((attr = nla_next(attr, &remaining)));
85 int get_family_id(struct nl_sock* netlink_socket, const char* name) {
86 if (send_command(netlink_socket, GENL_ID_CTRL, getpid(),
88 CTRL_ATTR_FAMILY_NAME,
89 (void*)name, strlen(name) + 1) < 0) {
94 struct nl_cb* callbacks = nl_cb_get(nl_cb_alloc(NL_CB_VALID));
95 nl_cb_set(callbacks, NL_CB_VALID, NL_CB_DEFAULT, &parse_family_id,
97 nl_cb_err(callbacks, NL_CB_DEFAULT, &print_receive_error, NULL);
99 if (nl_recvmsgs(netlink_socket, callbacks) < 0) {
102 nl_cb_put(callbacks);
106 void parse_aggregate_task_stats(struct nlattr* attr, int attr_size,
107 struct TaskStatistics* stats) {
109 switch (attr->nla_type) {
110 case TASKSTATS_TYPE_PID:
111 stats->pid = nla_get_u32(attr);
113 case TASKSTATS_TYPE_TGID:
114 stats->tgid = nla_get_u32(attr);
116 case TASKSTATS_TYPE_STATS:
117 nla_memcpy(&stats->stats, attr, sizeof(stats->stats));
122 } while ((attr = nla_next(attr, &attr_size)));
125 int parse_task_stats(struct nl_msg* msg, void* arg) {
126 struct TaskStatistics* stats = (struct TaskStatistics*)arg;
127 struct genlmsghdr* gnlh = (struct genlmsghdr*)nlmsg_data(nlmsg_hdr(msg));
128 struct nlattr* attr = genlmsg_attrdata(gnlh, 0);
129 int remaining = genlmsg_attrlen(gnlh, 0);
132 switch (attr->nla_type) {
133 case TASKSTATS_TYPE_AGGR_PID:
134 case TASKSTATS_TYPE_AGGR_TGID:
135 parse_aggregate_task_stats(nla_data(attr), nla_len(attr),
141 } while ((attr = nla_next(attr, &remaining)));
145 int query_task_stats(struct nl_sock* netlink_socket, int family_id,
146 int command_type, int parameter,
147 struct TaskStatistics* stats) {
148 memset(stats, 0, sizeof(*stats));
149 int result = send_command(netlink_socket, family_id, getpid(),
150 TASKSTATS_CMD_GET, command_type, ¶meter,
156 struct nl_cb* callbacks = nl_cb_get(nl_cb_alloc(NL_CB_VALID));
157 nl_cb_set(callbacks, NL_CB_VALID, NL_CB_DEFAULT, &parse_task_stats, stats);
158 nl_cb_err(callbacks, NL_CB_DEFAULT, &print_receive_error, &family_id);
160 result = nl_recvmsgs(netlink_socket, callbacks);
164 nl_cb_put(callbacks);
165 return stats->pid || stats->tgid;
168 double average_ms(uint64_t total, uint64_t count) {
172 return ((double)total) / count / 1e6;
175 uint64_t average_ns(uint64_t total, uint64_t count) {
179 return total / count;
182 void print_task_stats(const struct TaskStatistics* stats,
183 int human_readable) {
184 const struct taskstats* s = &stats->stats;
185 printf("Basic task statistics\n");
186 printf("---------------------\n");
187 printf("%-25s%d\n", "Stats version:", s->version);
188 printf("%-25s%d\n", "Exit code:", s->ac_exitcode);
189 printf("%-25s0x%x\n", "Flags:", s->ac_flag);
190 printf("%-25s%d\n", "Nice value:", s->ac_nice);
191 printf("%-25s%s\n", "Command name:", s->ac_comm);
192 printf("%-25s%d\n", "Scheduling discipline:", s->ac_sched);
193 printf("%-25s%d\n", "UID:", s->ac_uid);
194 printf("%-25s%d\n", "GID:", s->ac_gid);
195 printf("%-25s%d\n", "PID:", s->ac_pid);
196 printf("%-25s%d\n", "PPID:", s->ac_ppid);
198 if (human_readable) {
199 time_t begin_time = s->ac_btime;
200 printf("%-25s%s", "Begin time:", ctime(&begin_time));
202 printf("%-25s%d sec\n", "Begin time:", s->ac_btime);
204 printf("%-25s%llu usec\n", "Elapsed time:", s->ac_etime);
205 printf("%-25s%llu usec\n", "User CPU time:", s->ac_utime);
206 printf("%-25s%llu\n", "Minor page faults:", s->ac_minflt);
207 printf("%-25s%llu\n", "Major page faults:", s->ac_majflt);
208 printf("%-25s%llu usec\n", "Scaled user time:", s->ac_utimescaled);
209 printf("%-25s%llu usec\n", "Scaled system time:", s->ac_stimescaled);
211 printf("\nDelay accounting\n");
212 printf("----------------\n");
213 printf(" %15s%15s%15s%15s%15s%15s\n",
215 human_readable ? "Delay (ms)" : "Delay (ns)",
221 if (!human_readable) {
222 printf("CPU %15llu%15llu%15llu%15llu%15llu%15llu\n",
225 average_ns(s->cpu_delay_total, s->cpu_count),
226 s->cpu_run_real_total,
227 s->cpu_scaled_run_real_total,
228 s->cpu_run_virtual_total);
229 printf("IO %15llu%15llu%15llu\n",
231 s->blkio_delay_total,
232 average_ns(s->blkio_delay_total, s->blkio_count));
233 printf("Swap %15llu%15llu%15llu\n",
235 s->swapin_delay_total,
236 average_ns(s->swapin_delay_total, s->swapin_count));
237 printf("Reclaim%15llu%15llu%15llu\n",
239 s->freepages_delay_total,
240 average_ns(s->freepages_delay_total, s->freepages_count));
242 const double ms_per_ns = 1e6;
243 printf("CPU %15llu%15.3f%15.3f%15.3f%15.3f%15.3f\n",
245 s->cpu_delay_total / ms_per_ns,
246 average_ms(s->cpu_delay_total, s->cpu_count),
247 s->cpu_run_real_total / ms_per_ns,
248 s->cpu_scaled_run_real_total / ms_per_ns,
249 s->cpu_run_virtual_total / ms_per_ns);
250 printf("IO %15llu%15.3f%15.3f\n",
252 s->blkio_delay_total / ms_per_ns,
253 average_ms(s->blkio_delay_total, s->blkio_count));
254 printf("Swap %15llu%15.3f%15.3f\n",
256 s->swapin_delay_total / ms_per_ns,
257 average_ms(s->swapin_delay_total, s->swapin_count));
258 printf("Reclaim%15llu%15.3f%15.3f\n",
260 s->freepages_delay_total / ms_per_ns,
261 average_ms(s->freepages_delay_total, s->freepages_count));
264 printf("\nExtended accounting fields\n");
265 printf("--------------------------\n");
266 if (human_readable && s->ac_stime) {
267 printf("%-25s%.3f MB\n", "Average RSS usage:",
268 (double)s->coremem / s->ac_stime);
269 printf("%-25s%.3f MB\n", "Average VM usage:",
270 (double)s->virtmem / s->ac_stime);
272 printf("%-25s%llu MB\n", "Accumulated RSS usage:", s->coremem);
273 printf("%-25s%llu MB\n", "Accumulated VM usage:", s->virtmem);
275 printf("%-25s%llu KB\n", "RSS high water mark:", s->hiwater_rss);
276 printf("%-25s%llu KB\n", "VM high water mark:", s->hiwater_vm);
277 printf("%-25s%llu\n", "IO bytes read:", s->read_char);
278 printf("%-25s%llu\n", "IO bytes written:", s->write_char);
279 printf("%-25s%llu\n", "IO read syscalls:", s->read_syscalls);
280 printf("%-25s%llu\n", "IO write syscalls:", s->write_syscalls);
282 printf("\nPer-task/thread statistics\n");
283 printf("--------------------------\n");
284 printf("%-25s%llu\n", "Voluntary switches:", s->nvcsw);
285 printf("%-25s%llu\n", "Involuntary switches:", s->nivcsw);
289 printf("Linux task stats reporting tool\n"
291 "Usage: taskstats [options]\n"
294 " --help This text\n"
295 " --pid PID Print stats for the process id PID\n"
296 " --tgid TGID Print stats for the thread group id TGID\n"
297 " --raw Print raw numbers instead of human readable units\n"
299 "Either PID or TGID must be specified. For more documentation about "
300 "the reported fields, see\n"
301 "https://www.kernel.org/doc/Documentation/accounting/"
302 "taskstats-struct.txt\n");
305 int main(int argc, char** argv) {
306 int command_type = 0;
308 int human_readable = 1;
310 const struct option long_options[] = {
311 {"help", no_argument, 0, 0},
312 {"pid", required_argument, 0, 0},
313 {"tgid", required_argument, 0, 0},
314 {"raw", no_argument, 0, 0},
320 int option_char = getopt_long_only(argc, argv, "", long_options,
322 if (option_char == -1) {
325 switch (option_index) {
330 command_type = TASKSTATS_CMD_ATTR_PID;
334 command_type = TASKSTATS_CMD_ATTR_TGID;
346 printf("Either PID or TGID must be specified\n");
350 struct nl_sock* netlink_socket = nl_socket_alloc();
351 if (!netlink_socket || genl_connect(netlink_socket) < 0) {
352 perror("Unable to open netlink socket (are you root?)");
356 int family_id = get_family_id(netlink_socket, TASKSTATS_GENL_NAME);
358 perror("Unable to determine taskstats family id "
359 "(does your kernel support taskstats?)");
362 struct TaskStatistics stats;
363 if (query_task_stats(netlink_socket, family_id, command_type, pid,
365 perror("Failed to query taskstats");
368 print_task_stats(&stats, human_readable);
370 nl_socket_free(netlink_socket);
374 if (netlink_socket) {
375 nl_socket_free(netlink_socket);