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 struct kmem_cache
*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 skb
= genlmsg_new(size
, GFP_KERNEL
);
85 int seq
= get_cpu_var(taskstats_seqnum
)++;
86 put_cpu_var(taskstats_seqnum
);
88 reply
= genlmsg_put(skb
, 0, seq
, &family
, 0, cmd
);
90 reply
= genlmsg_put_reply(skb
, info
, &family
, 0, cmd
);
102 * Send taskstats data in @skb to listener with nl_pid @pid
104 static int send_reply(struct sk_buff
*skb
, pid_t pid
)
106 struct genlmsghdr
*genlhdr
= nlmsg_data((struct nlmsghdr
*)skb
->data
);
107 void *reply
= genlmsg_data(genlhdr
);
110 rc
= genlmsg_end(skb
, reply
);
116 return genlmsg_unicast(skb
, pid
);
120 * Send taskstats data in @skb to listeners registered for @cpu's exit data
122 static void send_cpu_listeners(struct sk_buff
*skb
,
123 struct listener_list
*listeners
)
125 struct genlmsghdr
*genlhdr
= nlmsg_data((struct nlmsghdr
*)skb
->data
);
126 struct listener
*s
, *tmp
;
127 struct sk_buff
*skb_next
, *skb_cur
= skb
;
128 void *reply
= genlmsg_data(genlhdr
);
129 int rc
, delcount
= 0;
131 rc
= genlmsg_end(skb
, reply
);
138 down_read(&listeners
->sem
);
139 list_for_each_entry(s
, &listeners
->list
, list
) {
141 if (!list_is_last(&s
->list
, &listeners
->list
)) {
142 skb_next
= skb_clone(skb_cur
, GFP_KERNEL
);
146 rc
= genlmsg_unicast(skb_cur
, s
->pid
);
147 if (rc
== -ECONNREFUSED
) {
153 up_read(&listeners
->sem
);
161 /* Delete invalidated entries */
162 down_write(&listeners
->sem
);
163 list_for_each_entry_safe(s
, tmp
, &listeners
->list
, list
) {
169 up_write(&listeners
->sem
);
172 static int fill_pid(pid_t pid
, struct task_struct
*tsk
,
173 struct taskstats
*stats
)
179 tsk
= find_task_by_pid(pid
);
181 get_task_struct(tsk
);
186 get_task_struct(tsk
);
189 * Each accounting subsystem adds calls to its functions to
190 * fill in relevant parts of struct taskstsats as follows
192 * per-task-foo(stats, tsk);
195 delayacct_add_tsk(stats
, tsk
);
197 /* fill in basic acct fields */
198 stats
->version
= TASKSTATS_VERSION
;
199 bacct_add_tsk(stats
, tsk
);
201 /* fill in extended acct fields */
202 xacct_add_tsk(stats
, tsk
);
204 /* Define err: label here if needed */
205 put_task_struct(tsk
);
210 static int fill_tgid(pid_t tgid
, struct task_struct
*first
,
211 struct taskstats
*stats
)
213 struct task_struct
*tsk
;
218 * Add additional stats from live tasks except zombie thread group
219 * leaders who are already counted with the dead tasks
223 first
= find_task_by_pid(tgid
);
225 if (!first
|| !lock_task_sighand(first
, &flags
))
228 if (first
->signal
->stats
)
229 memcpy(stats
, first
->signal
->stats
, sizeof(*stats
));
236 * Accounting subsystem can call its functions here to
237 * fill in relevant parts of struct taskstsats as follows
239 * per-task-foo(stats, tsk);
241 delayacct_add_tsk(stats
, tsk
);
243 } while_each_thread(first
, tsk
);
245 unlock_task_sighand(first
, &flags
);
250 stats
->version
= TASKSTATS_VERSION
;
252 * Accounting subsytems can also add calls here to modify
253 * fields of taskstats.
259 static void fill_tgid_exit(struct task_struct
*tsk
)
263 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
264 if (!tsk
->signal
->stats
)
268 * Each accounting subsystem calls its functions here to
269 * accumalate its per-task stats for tsk, into the per-tgid structure
271 * per-task-foo(tsk->signal->stats, tsk);
273 delayacct_add_tsk(tsk
->signal
->stats
, tsk
);
275 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
279 static int add_del_listener(pid_t pid
, cpumask_t
*maskp
, int isadd
)
281 struct listener_list
*listeners
;
282 struct listener
*s
, *tmp
;
284 cpumask_t mask
= *maskp
;
286 if (!cpus_subset(mask
, cpu_possible_map
))
289 if (isadd
== REGISTER
) {
290 for_each_cpu_mask(cpu
, mask
) {
291 s
= kmalloc_node(sizeof(struct listener
), GFP_KERNEL
,
296 INIT_LIST_HEAD(&s
->list
);
299 listeners
= &per_cpu(listener_array
, cpu
);
300 down_write(&listeners
->sem
);
301 list_add(&s
->list
, &listeners
->list
);
302 up_write(&listeners
->sem
);
307 /* Deregister or cleanup */
309 for_each_cpu_mask(cpu
, mask
) {
310 listeners
= &per_cpu(listener_array
, cpu
);
311 down_write(&listeners
->sem
);
312 list_for_each_entry_safe(s
, tmp
, &listeners
->list
, list
) {
319 up_write(&listeners
->sem
);
324 static int parse(struct nlattr
*na
, cpumask_t
*mask
)
333 if (len
> TASKSTATS_CPUMASK_MAXLEN
)
337 data
= kmalloc(len
, GFP_KERNEL
);
340 nla_strlcpy(data
, na
, len
);
341 ret
= cpulist_parse(data
, *mask
);
346 static int taskstats_user_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
349 struct sk_buff
*rep_skb
;
350 struct taskstats stats
;
356 rc
= parse(info
->attrs
[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK
], &mask
);
360 return add_del_listener(info
->snd_pid
, &mask
, REGISTER
);
362 rc
= parse(info
->attrs
[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK
], &mask
);
366 return add_del_listener(info
->snd_pid
, &mask
, DEREGISTER
);
369 * Size includes space for nested attributes
371 size
= nla_total_size(sizeof(u32
)) +
372 nla_total_size(sizeof(struct taskstats
)) + nla_total_size(0);
374 memset(&stats
, 0, sizeof(stats
));
375 rc
= prepare_reply(info
, TASKSTATS_CMD_NEW
, &rep_skb
, &reply
, size
);
379 if (info
->attrs
[TASKSTATS_CMD_ATTR_PID
]) {
380 u32 pid
= nla_get_u32(info
->attrs
[TASKSTATS_CMD_ATTR_PID
]);
381 rc
= fill_pid(pid
, NULL
, &stats
);
385 na
= nla_nest_start(rep_skb
, TASKSTATS_TYPE_AGGR_PID
);
386 NLA_PUT_U32(rep_skb
, TASKSTATS_TYPE_PID
, pid
);
387 NLA_PUT_TYPE(rep_skb
, struct taskstats
, TASKSTATS_TYPE_STATS
,
389 } else if (info
->attrs
[TASKSTATS_CMD_ATTR_TGID
]) {
390 u32 tgid
= nla_get_u32(info
->attrs
[TASKSTATS_CMD_ATTR_TGID
]);
391 rc
= fill_tgid(tgid
, NULL
, &stats
);
395 na
= nla_nest_start(rep_skb
, TASKSTATS_TYPE_AGGR_TGID
);
396 NLA_PUT_U32(rep_skb
, TASKSTATS_TYPE_TGID
, tgid
);
397 NLA_PUT_TYPE(rep_skb
, struct taskstats
, TASKSTATS_TYPE_STATS
,
404 nla_nest_end(rep_skb
, na
);
406 return send_reply(rep_skb
, info
->snd_pid
);
409 rc
= genlmsg_cancel(rep_skb
, reply
);
415 static struct taskstats
*taskstats_tgid_alloc(struct task_struct
*tsk
)
417 struct signal_struct
*sig
= tsk
->signal
;
418 struct taskstats
*stats
;
420 if (sig
->stats
|| thread_group_empty(tsk
))
423 /* No problem if kmem_cache_zalloc() fails */
424 stats
= kmem_cache_zalloc(taskstats_cache
, GFP_KERNEL
);
426 spin_lock_irq(&tsk
->sighand
->siglock
);
431 spin_unlock_irq(&tsk
->sighand
->siglock
);
434 kmem_cache_free(taskstats_cache
, stats
);
439 /* Send pid data out on exit */
440 void taskstats_exit(struct task_struct
*tsk
, int group_dead
)
443 struct listener_list
*listeners
;
444 struct taskstats
*tidstats
;
445 struct sk_buff
*rep_skb
;
451 if (!family_registered
)
455 * Size includes space for nested attributes
457 size
= nla_total_size(sizeof(u32
)) +
458 nla_total_size(sizeof(struct taskstats
)) + nla_total_size(0);
460 is_thread_group
= !!taskstats_tgid_alloc(tsk
);
461 if (is_thread_group
) {
462 /* PID + STATS + TGID + STATS */
464 /* fill the tsk->signal->stats structure */
468 listeners
= &__raw_get_cpu_var(listener_array
);
469 if (list_empty(&listeners
->list
))
472 tidstats
= kmem_cache_zalloc(taskstats_cache
, GFP_KERNEL
);
476 rc
= prepare_reply(NULL
, TASKSTATS_CMD_NEW
, &rep_skb
, &reply
, size
);
480 rc
= fill_pid(tsk
->pid
, tsk
, tidstats
);
484 na
= nla_nest_start(rep_skb
, TASKSTATS_TYPE_AGGR_PID
);
485 NLA_PUT_U32(rep_skb
, TASKSTATS_TYPE_PID
, (u32
)tsk
->pid
);
486 NLA_PUT_TYPE(rep_skb
, struct taskstats
, TASKSTATS_TYPE_STATS
,
488 nla_nest_end(rep_skb
, na
);
490 if (!is_thread_group
)
494 * Doesn't matter if tsk is the leader or the last group member leaving
499 na
= nla_nest_start(rep_skb
, TASKSTATS_TYPE_AGGR_TGID
);
500 NLA_PUT_U32(rep_skb
, TASKSTATS_TYPE_TGID
, (u32
)tsk
->tgid
);
501 /* No locking needed for tsk->signal->stats since group is dead */
502 NLA_PUT_TYPE(rep_skb
, struct taskstats
, TASKSTATS_TYPE_STATS
,
503 *tsk
->signal
->stats
);
504 nla_nest_end(rep_skb
, na
);
507 send_cpu_listeners(rep_skb
, listeners
);
509 kmem_cache_free(taskstats_cache
, tidstats
);
513 genlmsg_cancel(rep_skb
, reply
);
519 static struct genl_ops taskstats_ops
= {
520 .cmd
= TASKSTATS_CMD_GET
,
521 .doit
= taskstats_user_cmd
,
522 .policy
= taskstats_cmd_get_policy
,
525 /* Needed early in initialization */
526 void __init
taskstats_init_early(void)
530 taskstats_cache
= kmem_cache_create("taskstats_cache",
531 sizeof(struct taskstats
),
532 0, SLAB_PANIC
, NULL
, NULL
);
533 for_each_possible_cpu(i
) {
534 INIT_LIST_HEAD(&(per_cpu(listener_array
, i
).list
));
535 init_rwsem(&(per_cpu(listener_array
, i
).sem
));
539 static int __init
taskstats_init(void)
543 rc
= genl_register_family(&family
);
547 rc
= genl_register_ops(&family
, &taskstats_ops
);
551 family_registered
= 1;
554 genl_unregister_family(&family
);
559 * late initcall ensures initialization of statistics collection
560 * mechanisms precedes initialization of the taskstats interface
562 late_initcall(taskstats_init
);