2 * taskstats.c - Export per-task statistics to userland
4 * Copyright (C) Shailabh Nagar, IBM Corp. 2006
5 * (C) Balbir Singh, IBM Corp. 2006
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/taskstats_kern.h>
21 #include <linux/tsacct_kern.h>
22 #include <linux/delayacct.h>
23 #include <linux/cpumask.h>
24 #include <linux/percpu.h>
25 #include <linux/cgroupstats.h>
26 #include <linux/cgroup.h>
28 #include <linux/file.h>
29 #include <net/genetlink.h>
30 #include <asm/atomic.h>
33 * Maximum length of a cpumask that can be specified in
34 * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
36 #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
38 static DEFINE_PER_CPU(__u32
, taskstats_seqnum
);
39 static int family_registered
;
40 struct kmem_cache
*taskstats_cache
;
42 static struct genl_family family
= {
43 .id
= GENL_ID_GENERATE
,
44 .name
= TASKSTATS_GENL_NAME
,
45 .version
= TASKSTATS_GENL_VERSION
,
46 .maxattr
= TASKSTATS_CMD_ATTR_MAX
,
49 static const struct nla_policy taskstats_cmd_get_policy
[TASKSTATS_CMD_ATTR_MAX
+1] = {
50 [TASKSTATS_CMD_ATTR_PID
] = { .type
= NLA_U32
},
51 [TASKSTATS_CMD_ATTR_TGID
] = { .type
= NLA_U32
},
52 [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK
] = { .type
= NLA_STRING
},
53 [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK
] = { .type
= NLA_STRING
},};
55 static const struct nla_policy cgroupstats_cmd_get_policy
[CGROUPSTATS_CMD_ATTR_MAX
+1] = {
56 [CGROUPSTATS_CMD_ATTR_FD
] = { .type
= NLA_U32
},
60 struct list_head list
;
65 struct listener_list
{
66 struct rw_semaphore sem
;
67 struct list_head list
;
69 static DEFINE_PER_CPU(struct listener_list
, listener_array
);
77 static int prepare_reply(struct genl_info
*info
, u8 cmd
, struct sk_buff
**skbp
,
84 * If new attributes are added, please revisit this allocation
86 skb
= genlmsg_new(size
, GFP_KERNEL
);
91 int seq
= get_cpu_var(taskstats_seqnum
)++;
92 put_cpu_var(taskstats_seqnum
);
94 reply
= genlmsg_put(skb
, 0, seq
, &family
, 0, cmd
);
96 reply
= genlmsg_put_reply(skb
, info
, &family
, 0, cmd
);
107 * Send taskstats data in @skb to listener with nl_pid @pid
109 static int send_reply(struct sk_buff
*skb
, struct genl_info
*info
)
111 struct genlmsghdr
*genlhdr
= nlmsg_data(nlmsg_hdr(skb
));
112 void *reply
= genlmsg_data(genlhdr
);
115 rc
= genlmsg_end(skb
, reply
);
121 return genlmsg_reply(skb
, info
);
125 * Send taskstats data in @skb to listeners registered for @cpu's exit data
127 static void send_cpu_listeners(struct sk_buff
*skb
,
128 struct listener_list
*listeners
)
130 struct genlmsghdr
*genlhdr
= nlmsg_data(nlmsg_hdr(skb
));
131 struct listener
*s
, *tmp
;
132 struct sk_buff
*skb_next
, *skb_cur
= skb
;
133 void *reply
= genlmsg_data(genlhdr
);
134 int rc
, delcount
= 0;
136 rc
= genlmsg_end(skb
, reply
);
143 down_read(&listeners
->sem
);
144 list_for_each_entry(s
, &listeners
->list
, list
) {
146 if (!list_is_last(&s
->list
, &listeners
->list
)) {
147 skb_next
= skb_clone(skb_cur
, GFP_KERNEL
);
151 rc
= genlmsg_unicast(&init_net
, skb_cur
, s
->pid
);
152 if (rc
== -ECONNREFUSED
) {
158 up_read(&listeners
->sem
);
166 /* Delete invalidated entries */
167 down_write(&listeners
->sem
);
168 list_for_each_entry_safe(s
, tmp
, &listeners
->list
, list
) {
174 up_write(&listeners
->sem
);
177 static int fill_pid(pid_t pid
, struct task_struct
*tsk
,
178 struct taskstats
*stats
)
184 tsk
= find_task_by_vpid(pid
);
186 get_task_struct(tsk
);
191 get_task_struct(tsk
);
193 memset(stats
, 0, sizeof(*stats
));
195 * Each accounting subsystem adds calls to its functions to
196 * fill in relevant parts of struct taskstsats as follows
198 * per-task-foo(stats, tsk);
201 delayacct_add_tsk(stats
, tsk
);
203 /* fill in basic acct fields */
204 stats
->version
= TASKSTATS_VERSION
;
205 stats
->nvcsw
= tsk
->nvcsw
;
206 stats
->nivcsw
= tsk
->nivcsw
;
207 bacct_add_tsk(stats
, tsk
);
209 /* fill in extended acct fields */
210 xacct_add_tsk(stats
, tsk
);
212 /* Define err: label here if needed */
213 put_task_struct(tsk
);
218 static int fill_tgid(pid_t tgid
, struct task_struct
*first
,
219 struct taskstats
*stats
)
221 struct task_struct
*tsk
;
226 * Add additional stats from live tasks except zombie thread group
227 * leaders who are already counted with the dead tasks
231 first
= find_task_by_vpid(tgid
);
233 if (!first
|| !lock_task_sighand(first
, &flags
))
236 if (first
->signal
->stats
)
237 memcpy(stats
, first
->signal
->stats
, sizeof(*stats
));
239 memset(stats
, 0, sizeof(*stats
));
246 * Accounting subsystem can call its functions here to
247 * fill in relevant parts of struct taskstsats as follows
249 * per-task-foo(stats, tsk);
251 delayacct_add_tsk(stats
, tsk
);
253 stats
->nvcsw
+= tsk
->nvcsw
;
254 stats
->nivcsw
+= tsk
->nivcsw
;
255 } while_each_thread(first
, tsk
);
257 unlock_task_sighand(first
, &flags
);
262 stats
->version
= TASKSTATS_VERSION
;
264 * Accounting subsystems can also add calls here to modify
265 * fields of taskstats.
271 static void fill_tgid_exit(struct task_struct
*tsk
)
275 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
276 if (!tsk
->signal
->stats
)
280 * Each accounting subsystem calls its functions here to
281 * accumalate its per-task stats for tsk, into the per-tgid structure
283 * per-task-foo(tsk->signal->stats, tsk);
285 delayacct_add_tsk(tsk
->signal
->stats
, tsk
);
287 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
291 static int add_del_listener(pid_t pid
, const struct cpumask
*mask
, int isadd
)
293 struct listener_list
*listeners
;
294 struct listener
*s
, *tmp
;
297 if (!cpumask_subset(mask
, cpu_possible_mask
))
300 if (isadd
== REGISTER
) {
301 for_each_cpu(cpu
, mask
) {
302 s
= kmalloc_node(sizeof(struct listener
), GFP_KERNEL
,
307 INIT_LIST_HEAD(&s
->list
);
310 listeners
= &per_cpu(listener_array
, cpu
);
311 down_write(&listeners
->sem
);
312 list_add(&s
->list
, &listeners
->list
);
313 up_write(&listeners
->sem
);
318 /* Deregister or cleanup */
320 for_each_cpu(cpu
, mask
) {
321 listeners
= &per_cpu(listener_array
, cpu
);
322 down_write(&listeners
->sem
);
323 list_for_each_entry_safe(s
, tmp
, &listeners
->list
, list
) {
330 up_write(&listeners
->sem
);
335 static int parse(struct nlattr
*na
, struct cpumask
*mask
)
344 if (len
> TASKSTATS_CPUMASK_MAXLEN
)
348 data
= kmalloc(len
, GFP_KERNEL
);
351 nla_strlcpy(data
, na
, len
);
352 ret
= cpulist_parse(data
, mask
);
357 static struct taskstats
*mk_reply(struct sk_buff
*skb
, int type
, u32 pid
)
359 struct nlattr
*na
, *ret
;
362 aggr
= (type
== TASKSTATS_TYPE_PID
)
363 ? TASKSTATS_TYPE_AGGR_PID
364 : TASKSTATS_TYPE_AGGR_TGID
;
366 na
= nla_nest_start(skb
, aggr
);
369 if (nla_put(skb
, type
, sizeof(pid
), &pid
) < 0)
371 ret
= nla_reserve(skb
, TASKSTATS_TYPE_STATS
, sizeof(struct taskstats
));
374 nla_nest_end(skb
, na
);
376 return nla_data(ret
);
381 static int cgroupstats_user_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
384 struct sk_buff
*rep_skb
;
385 struct cgroupstats
*stats
;
392 na
= info
->attrs
[CGROUPSTATS_CMD_ATTR_FD
];
396 fd
= nla_get_u32(info
->attrs
[CGROUPSTATS_CMD_ATTR_FD
]);
397 file
= fget_light(fd
, &fput_needed
);
401 size
= nla_total_size(sizeof(struct cgroupstats
));
403 rc
= prepare_reply(info
, CGROUPSTATS_CMD_NEW
, &rep_skb
,
408 na
= nla_reserve(rep_skb
, CGROUPSTATS_TYPE_CGROUP_STATS
,
409 sizeof(struct cgroupstats
));
410 stats
= nla_data(na
);
411 memset(stats
, 0, sizeof(*stats
));
413 rc
= cgroupstats_build(stats
, file
->f_dentry
);
419 rc
= send_reply(rep_skb
, info
);
422 fput_light(file
, fput_needed
);
426 static int taskstats_user_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
429 struct sk_buff
*rep_skb
;
430 struct taskstats
*stats
;
434 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
437 rc
= parse(info
->attrs
[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK
], mask
);
441 rc
= add_del_listener(info
->snd_pid
, mask
, REGISTER
);
445 rc
= parse(info
->attrs
[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK
], mask
);
449 rc
= add_del_listener(info
->snd_pid
, mask
, DEREGISTER
);
451 free_cpumask_var(mask
);
454 free_cpumask_var(mask
);
457 * Size includes space for nested attributes
459 size
= nla_total_size(sizeof(u32
)) +
460 nla_total_size(sizeof(struct taskstats
)) + nla_total_size(0);
462 rc
= prepare_reply(info
, TASKSTATS_CMD_NEW
, &rep_skb
, size
);
467 if (info
->attrs
[TASKSTATS_CMD_ATTR_PID
]) {
468 u32 pid
= nla_get_u32(info
->attrs
[TASKSTATS_CMD_ATTR_PID
]);
469 stats
= mk_reply(rep_skb
, TASKSTATS_TYPE_PID
, pid
);
473 rc
= fill_pid(pid
, NULL
, stats
);
476 } else if (info
->attrs
[TASKSTATS_CMD_ATTR_TGID
]) {
477 u32 tgid
= nla_get_u32(info
->attrs
[TASKSTATS_CMD_ATTR_TGID
]);
478 stats
= mk_reply(rep_skb
, TASKSTATS_TYPE_TGID
, tgid
);
482 rc
= fill_tgid(tgid
, NULL
, stats
);
488 return send_reply(rep_skb
, info
);
494 static struct taskstats
*taskstats_tgid_alloc(struct task_struct
*tsk
)
496 struct signal_struct
*sig
= tsk
->signal
;
497 struct taskstats
*stats
;
499 if (sig
->stats
|| thread_group_empty(tsk
))
502 /* No problem if kmem_cache_zalloc() fails */
503 stats
= kmem_cache_zalloc(taskstats_cache
, GFP_KERNEL
);
505 spin_lock_irq(&tsk
->sighand
->siglock
);
510 spin_unlock_irq(&tsk
->sighand
->siglock
);
513 kmem_cache_free(taskstats_cache
, stats
);
518 /* Send pid data out on exit */
519 void taskstats_exit(struct task_struct
*tsk
, int group_dead
)
522 struct listener_list
*listeners
;
523 struct taskstats
*stats
;
524 struct sk_buff
*rep_skb
;
528 if (!family_registered
)
532 * Size includes space for nested attributes
534 size
= nla_total_size(sizeof(u32
)) +
535 nla_total_size(sizeof(struct taskstats
)) + nla_total_size(0);
537 is_thread_group
= !!taskstats_tgid_alloc(tsk
);
538 if (is_thread_group
) {
539 /* PID + STATS + TGID + STATS */
541 /* fill the tsk->signal->stats structure */
545 listeners
= &__raw_get_cpu_var(listener_array
);
546 if (list_empty(&listeners
->list
))
549 rc
= prepare_reply(NULL
, TASKSTATS_CMD_NEW
, &rep_skb
, size
);
553 stats
= mk_reply(rep_skb
, TASKSTATS_TYPE_PID
, tsk
->pid
);
557 rc
= fill_pid(-1, tsk
, stats
);
562 * Doesn't matter if tsk is the leader or the last group member leaving
564 if (!is_thread_group
|| !group_dead
)
567 stats
= mk_reply(rep_skb
, TASKSTATS_TYPE_TGID
, tsk
->tgid
);
571 memcpy(stats
, tsk
->signal
->stats
, sizeof(*stats
));
574 send_cpu_listeners(rep_skb
, listeners
);
580 static struct genl_ops taskstats_ops
= {
581 .cmd
= TASKSTATS_CMD_GET
,
582 .doit
= taskstats_user_cmd
,
583 .policy
= taskstats_cmd_get_policy
,
586 static struct genl_ops cgroupstats_ops
= {
587 .cmd
= CGROUPSTATS_CMD_GET
,
588 .doit
= cgroupstats_user_cmd
,
589 .policy
= cgroupstats_cmd_get_policy
,
592 /* Needed early in initialization */
593 void __init
taskstats_init_early(void)
597 taskstats_cache
= KMEM_CACHE(taskstats
, SLAB_PANIC
);
598 for_each_possible_cpu(i
) {
599 INIT_LIST_HEAD(&(per_cpu(listener_array
, i
).list
));
600 init_rwsem(&(per_cpu(listener_array
, i
).sem
));
604 static int __init
taskstats_init(void)
608 rc
= genl_register_family(&family
);
612 rc
= genl_register_ops(&family
, &taskstats_ops
);
616 rc
= genl_register_ops(&family
, &cgroupstats_ops
);
620 family_registered
= 1;
621 printk("registered taskstats version %d\n", TASKSTATS_GENL_VERSION
);
624 genl_unregister_ops(&family
, &taskstats_ops
);
626 genl_unregister_family(&family
);
631 * late initcall ensures initialization of statistics collection
632 * mechanisms precedes initialization of the taskstats interface
634 late_initcall(taskstats_init
);