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/tsacct_kern.h>
24 #include <linux/cpumask.h>
25 #include <linux/percpu.h>
26 #include <net/genetlink.h>
27 #include <asm/atomic.h>
30 * Maximum length of a cpumask that can be specified in
31 * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
33 #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
35 static DEFINE_PER_CPU(__u32
, taskstats_seqnum
) = { 0 };
36 static int family_registered
;
37 kmem_cache_t
*taskstats_cache
;
39 static struct genl_family family
= {
40 .id
= GENL_ID_GENERATE
,
41 .name
= TASKSTATS_GENL_NAME
,
42 .version
= TASKSTATS_GENL_VERSION
,
43 .maxattr
= TASKSTATS_CMD_ATTR_MAX
,
46 static struct nla_policy taskstats_cmd_get_policy
[TASKSTATS_CMD_ATTR_MAX
+1]
48 [TASKSTATS_CMD_ATTR_PID
] = { .type
= NLA_U32
},
49 [TASKSTATS_CMD_ATTR_TGID
] = { .type
= NLA_U32
},
50 [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK
] = { .type
= NLA_STRING
},
51 [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK
] = { .type
= NLA_STRING
},};
54 struct list_head list
;
59 struct listener_list
{
60 struct rw_semaphore sem
;
61 struct list_head list
;
63 static DEFINE_PER_CPU(struct listener_list
, listener_array
);
71 static int prepare_reply(struct genl_info
*info
, u8 cmd
, struct sk_buff
**skbp
,
72 void **replyp
, size_t size
)
78 * If new attributes are added, please revisit this allocation
80 size
= nlmsg_total_size(genlmsg_total_size(size
));
81 skb
= nlmsg_new(size
, GFP_KERNEL
);
86 int seq
= get_cpu_var(taskstats_seqnum
)++;
87 put_cpu_var(taskstats_seqnum
);
89 reply
= genlmsg_put(skb
, 0, seq
,
93 reply
= genlmsg_put(skb
, info
->snd_pid
, info
->snd_seq
,
107 * Send taskstats data in @skb to listener with nl_pid @pid
109 static int send_reply(struct sk_buff
*skb
, pid_t pid
)
111 struct genlmsghdr
*genlhdr
= nlmsg_data((struct nlmsghdr
*)skb
->data
);
112 void *reply
= genlmsg_data(genlhdr
);
115 rc
= genlmsg_end(skb
, reply
);
121 return genlmsg_unicast(skb
, pid
);
125 * Send taskstats data in @skb to listeners registered for @cpu's exit data
127 static void send_cpu_listeners(struct sk_buff
*skb
, unsigned int cpu
)
129 struct genlmsghdr
*genlhdr
= nlmsg_data((struct nlmsghdr
*)skb
->data
);
130 struct listener_list
*listeners
;
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 listeners
= &per_cpu(listener_array
, cpu
);
144 down_read(&listeners
->sem
);
145 list_for_each_entry(s
, &listeners
->list
, list
) {
147 if (!list_is_last(&s
->list
, &listeners
->list
)) {
148 skb_next
= skb_clone(skb_cur
, GFP_KERNEL
);
152 rc
= genlmsg_unicast(skb_cur
, s
->pid
);
153 if (rc
== -ECONNREFUSED
) {
159 up_read(&listeners
->sem
);
167 /* Delete invalidated entries */
168 down_write(&listeners
->sem
);
169 list_for_each_entry_safe(s
, tmp
, &listeners
->list
, list
) {
175 up_write(&listeners
->sem
);
178 static int fill_pid(pid_t pid
, struct task_struct
*tsk
,
179 struct taskstats
*stats
)
185 tsk
= find_task_by_pid(pid
);
187 get_task_struct(tsk
);
192 get_task_struct(tsk
);
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 bacct_add_tsk(stats
, tsk
);
207 /* fill in extended acct fields */
208 xacct_add_tsk(stats
, tsk
);
210 /* Define err: label here if needed */
211 put_task_struct(tsk
);
216 static int fill_tgid(pid_t tgid
, struct task_struct
*first
,
217 struct taskstats
*stats
)
219 struct task_struct
*tsk
;
224 * Add additional stats from live tasks except zombie thread group
225 * leaders who are already counted with the dead tasks
229 first
= find_task_by_pid(tgid
);
231 if (!first
|| !lock_task_sighand(first
, &flags
))
234 if (first
->signal
->stats
)
235 memcpy(stats
, first
->signal
->stats
, sizeof(*stats
));
242 * Accounting subsystem can call its functions here to
243 * fill in relevant parts of struct taskstsats as follows
245 * per-task-foo(stats, tsk);
247 delayacct_add_tsk(stats
, tsk
);
249 } while_each_thread(first
, tsk
);
251 unlock_task_sighand(first
, &flags
);
256 stats
->version
= TASKSTATS_VERSION
;
258 * Accounting subsytems can also add calls here to modify
259 * fields of taskstats.
265 static void fill_tgid_exit(struct task_struct
*tsk
)
269 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
270 if (!tsk
->signal
->stats
)
274 * Each accounting subsystem calls its functions here to
275 * accumalate its per-task stats for tsk, into the per-tgid structure
277 * per-task-foo(tsk->signal->stats, tsk);
279 delayacct_add_tsk(tsk
->signal
->stats
, tsk
);
281 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
285 static int add_del_listener(pid_t pid
, cpumask_t
*maskp
, int isadd
)
287 struct listener_list
*listeners
;
288 struct listener
*s
, *tmp
;
290 cpumask_t mask
= *maskp
;
292 if (!cpus_subset(mask
, cpu_possible_map
))
295 if (isadd
== REGISTER
) {
296 for_each_cpu_mask(cpu
, mask
) {
297 s
= kmalloc_node(sizeof(struct listener
), GFP_KERNEL
,
302 INIT_LIST_HEAD(&s
->list
);
305 listeners
= &per_cpu(listener_array
, cpu
);
306 down_write(&listeners
->sem
);
307 list_add(&s
->list
, &listeners
->list
);
308 up_write(&listeners
->sem
);
313 /* Deregister or cleanup */
315 for_each_cpu_mask(cpu
, mask
) {
316 listeners
= &per_cpu(listener_array
, cpu
);
317 down_write(&listeners
->sem
);
318 list_for_each_entry_safe(s
, tmp
, &listeners
->list
, list
) {
325 up_write(&listeners
->sem
);
330 static int parse(struct nlattr
*na
, cpumask_t
*mask
)
339 if (len
> TASKSTATS_CPUMASK_MAXLEN
)
343 data
= kmalloc(len
, GFP_KERNEL
);
346 nla_strlcpy(data
, na
, len
);
347 ret
= cpulist_parse(data
, *mask
);
352 static int taskstats_user_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
355 struct sk_buff
*rep_skb
;
356 struct taskstats stats
;
362 rc
= parse(info
->attrs
[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK
], &mask
);
366 return add_del_listener(info
->snd_pid
, &mask
, REGISTER
);
368 rc
= parse(info
->attrs
[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK
], &mask
);
372 return add_del_listener(info
->snd_pid
, &mask
, DEREGISTER
);
375 * Size includes space for nested attributes
377 size
= nla_total_size(sizeof(u32
)) +
378 nla_total_size(sizeof(struct taskstats
)) + nla_total_size(0);
380 memset(&stats
, 0, sizeof(stats
));
381 rc
= prepare_reply(info
, TASKSTATS_CMD_NEW
, &rep_skb
, &reply
, size
);
385 if (info
->attrs
[TASKSTATS_CMD_ATTR_PID
]) {
386 u32 pid
= nla_get_u32(info
->attrs
[TASKSTATS_CMD_ATTR_PID
]);
387 rc
= fill_pid(pid
, NULL
, &stats
);
391 na
= nla_nest_start(rep_skb
, TASKSTATS_TYPE_AGGR_PID
);
392 NLA_PUT_U32(rep_skb
, TASKSTATS_TYPE_PID
, pid
);
393 NLA_PUT_TYPE(rep_skb
, struct taskstats
, TASKSTATS_TYPE_STATS
,
395 } else if (info
->attrs
[TASKSTATS_CMD_ATTR_TGID
]) {
396 u32 tgid
= nla_get_u32(info
->attrs
[TASKSTATS_CMD_ATTR_TGID
]);
397 rc
= fill_tgid(tgid
, NULL
, &stats
);
401 na
= nla_nest_start(rep_skb
, TASKSTATS_TYPE_AGGR_TGID
);
402 NLA_PUT_U32(rep_skb
, TASKSTATS_TYPE_TGID
, tgid
);
403 NLA_PUT_TYPE(rep_skb
, struct taskstats
, TASKSTATS_TYPE_STATS
,
410 nla_nest_end(rep_skb
, na
);
412 return send_reply(rep_skb
, info
->snd_pid
);
415 rc
= genlmsg_cancel(rep_skb
, reply
);
421 void taskstats_exit_alloc(struct taskstats
**ptidstats
, unsigned int *mycpu
)
423 struct listener_list
*listeners
;
424 struct taskstats
*tmp
;
426 * This is the cpu on which the task is exiting currently and will
427 * be the one for which the exit event is sent, even if the cpu
428 * on which this function is running changes later.
430 *mycpu
= raw_smp_processor_id();
433 tmp
= kmem_cache_zalloc(taskstats_cache
, SLAB_KERNEL
);
437 listeners
= &per_cpu(listener_array
, *mycpu
);
438 down_read(&listeners
->sem
);
439 if (!list_empty(&listeners
->list
)) {
443 up_read(&listeners
->sem
);
447 /* Send pid data out on exit */
448 void taskstats_exit_send(struct task_struct
*tsk
, struct taskstats
*tidstats
,
449 int group_dead
, unsigned int mycpu
)
452 struct sk_buff
*rep_skb
;
458 if (!family_registered
)
462 * Size includes space for nested attributes
464 size
= nla_total_size(sizeof(u32
)) +
465 nla_total_size(sizeof(struct taskstats
)) + nla_total_size(0);
467 is_thread_group
= (tsk
->signal
->stats
!= NULL
);
468 if (is_thread_group
) {
469 /* PID + STATS + TGID + STATS */
471 /* fill the tsk->signal->stats structure */
478 rc
= prepare_reply(NULL
, TASKSTATS_CMD_NEW
, &rep_skb
, &reply
, size
);
482 rc
= fill_pid(tsk
->pid
, tsk
, tidstats
);
486 na
= nla_nest_start(rep_skb
, TASKSTATS_TYPE_AGGR_PID
);
487 NLA_PUT_U32(rep_skb
, TASKSTATS_TYPE_PID
, (u32
)tsk
->pid
);
488 NLA_PUT_TYPE(rep_skb
, struct taskstats
, TASKSTATS_TYPE_STATS
,
490 nla_nest_end(rep_skb
, na
);
492 if (!is_thread_group
)
496 * Doesn't matter if tsk is the leader or the last group member leaving
501 na
= nla_nest_start(rep_skb
, TASKSTATS_TYPE_AGGR_TGID
);
502 NLA_PUT_U32(rep_skb
, TASKSTATS_TYPE_TGID
, (u32
)tsk
->tgid
);
503 /* No locking needed for tsk->signal->stats since group is dead */
504 NLA_PUT_TYPE(rep_skb
, struct taskstats
, TASKSTATS_TYPE_STATS
,
505 *tsk
->signal
->stats
);
506 nla_nest_end(rep_skb
, na
);
509 send_cpu_listeners(rep_skb
, mycpu
);
513 genlmsg_cancel(rep_skb
, reply
);
520 static struct genl_ops taskstats_ops
= {
521 .cmd
= TASKSTATS_CMD_GET
,
522 .doit
= taskstats_user_cmd
,
523 .policy
= taskstats_cmd_get_policy
,
526 /* Needed early in initialization */
527 void __init
taskstats_init_early(void)
531 taskstats_cache
= kmem_cache_create("taskstats_cache",
532 sizeof(struct taskstats
),
533 0, SLAB_PANIC
, NULL
, NULL
);
534 for_each_possible_cpu(i
) {
535 INIT_LIST_HEAD(&(per_cpu(listener_array
, i
).list
));
536 init_rwsem(&(per_cpu(listener_array
, i
).sem
));
540 static int __init
taskstats_init(void)
544 rc
= genl_register_family(&family
);
548 rc
= genl_register_ops(&family
, &taskstats_ops
);
552 family_registered
= 1;
555 genl_unregister_family(&family
);
560 * late initcall ensures initialization of statistics collection
561 * mechanisms precedes initialization of the taskstats interface
563 late_initcall(taskstats_init
);