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
,
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
);
101 * Send taskstats data in @skb to listener with nl_pid @pid
103 static int send_reply(struct sk_buff
*skb
, pid_t pid
)
105 struct genlmsghdr
*genlhdr
= nlmsg_data((struct nlmsghdr
*)skb
->data
);
106 void *reply
= genlmsg_data(genlhdr
);
109 rc
= genlmsg_end(skb
, reply
);
115 return genlmsg_unicast(skb
, pid
);
119 * Send taskstats data in @skb to listeners registered for @cpu's exit data
121 static void send_cpu_listeners(struct sk_buff
*skb
,
122 struct listener_list
*listeners
)
124 struct genlmsghdr
*genlhdr
= nlmsg_data((struct nlmsghdr
*)skb
->data
);
125 struct listener
*s
, *tmp
;
126 struct sk_buff
*skb_next
, *skb_cur
= skb
;
127 void *reply
= genlmsg_data(genlhdr
);
128 int rc
, delcount
= 0;
130 rc
= genlmsg_end(skb
, reply
);
137 down_read(&listeners
->sem
);
138 list_for_each_entry(s
, &listeners
->list
, list
) {
140 if (!list_is_last(&s
->list
, &listeners
->list
)) {
141 skb_next
= skb_clone(skb_cur
, GFP_KERNEL
);
145 rc
= genlmsg_unicast(skb_cur
, s
->pid
);
146 if (rc
== -ECONNREFUSED
) {
152 up_read(&listeners
->sem
);
160 /* Delete invalidated entries */
161 down_write(&listeners
->sem
);
162 list_for_each_entry_safe(s
, tmp
, &listeners
->list
, list
) {
168 up_write(&listeners
->sem
);
171 static int fill_pid(pid_t pid
, struct task_struct
*tsk
,
172 struct taskstats
*stats
)
178 tsk
= find_task_by_pid(pid
);
180 get_task_struct(tsk
);
185 get_task_struct(tsk
);
187 memset(stats
, 0, sizeof(*stats
));
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
));
231 memset(stats
, 0, sizeof(*stats
));
238 * Accounting subsystem can call its functions here to
239 * fill in relevant parts of struct taskstsats as follows
241 * per-task-foo(stats, tsk);
243 delayacct_add_tsk(stats
, tsk
);
245 } while_each_thread(first
, tsk
);
247 unlock_task_sighand(first
, &flags
);
252 stats
->version
= TASKSTATS_VERSION
;
254 * Accounting subsytems can also add calls here to modify
255 * fields of taskstats.
261 static void fill_tgid_exit(struct task_struct
*tsk
)
265 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
266 if (!tsk
->signal
->stats
)
270 * Each accounting subsystem calls its functions here to
271 * accumalate its per-task stats for tsk, into the per-tgid structure
273 * per-task-foo(tsk->signal->stats, tsk);
275 delayacct_add_tsk(tsk
->signal
->stats
, tsk
);
277 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
281 static int add_del_listener(pid_t pid
, cpumask_t
*maskp
, int isadd
)
283 struct listener_list
*listeners
;
284 struct listener
*s
, *tmp
;
286 cpumask_t mask
= *maskp
;
288 if (!cpus_subset(mask
, cpu_possible_map
))
291 if (isadd
== REGISTER
) {
292 for_each_cpu_mask(cpu
, mask
) {
293 s
= kmalloc_node(sizeof(struct listener
), GFP_KERNEL
,
298 INIT_LIST_HEAD(&s
->list
);
301 listeners
= &per_cpu(listener_array
, cpu
);
302 down_write(&listeners
->sem
);
303 list_add(&s
->list
, &listeners
->list
);
304 up_write(&listeners
->sem
);
309 /* Deregister or cleanup */
311 for_each_cpu_mask(cpu
, mask
) {
312 listeners
= &per_cpu(listener_array
, cpu
);
313 down_write(&listeners
->sem
);
314 list_for_each_entry_safe(s
, tmp
, &listeners
->list
, list
) {
321 up_write(&listeners
->sem
);
326 static int parse(struct nlattr
*na
, cpumask_t
*mask
)
335 if (len
> TASKSTATS_CPUMASK_MAXLEN
)
339 data
= kmalloc(len
, GFP_KERNEL
);
342 nla_strlcpy(data
, na
, len
);
343 ret
= cpulist_parse(data
, *mask
);
348 static struct taskstats
*mk_reply(struct sk_buff
*skb
, int type
, u32 pid
)
350 struct nlattr
*na
, *ret
;
353 aggr
= (type
== TASKSTATS_TYPE_PID
)
354 ? TASKSTATS_TYPE_AGGR_PID
355 : TASKSTATS_TYPE_AGGR_TGID
;
357 na
= nla_nest_start(skb
, aggr
);
360 if (nla_put(skb
, type
, sizeof(pid
), &pid
) < 0)
362 ret
= nla_reserve(skb
, TASKSTATS_TYPE_STATS
, sizeof(struct taskstats
));
365 nla_nest_end(skb
, na
);
367 return nla_data(ret
);
372 static int taskstats_user_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
375 struct sk_buff
*rep_skb
;
376 struct taskstats
*stats
;
380 rc
= parse(info
->attrs
[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK
], &mask
);
384 return add_del_listener(info
->snd_pid
, &mask
, REGISTER
);
386 rc
= parse(info
->attrs
[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK
], &mask
);
390 return add_del_listener(info
->snd_pid
, &mask
, DEREGISTER
);
393 * Size includes space for nested attributes
395 size
= nla_total_size(sizeof(u32
)) +
396 nla_total_size(sizeof(struct taskstats
)) + nla_total_size(0);
398 rc
= prepare_reply(info
, TASKSTATS_CMD_NEW
, &rep_skb
, size
);
403 if (info
->attrs
[TASKSTATS_CMD_ATTR_PID
]) {
404 u32 pid
= nla_get_u32(info
->attrs
[TASKSTATS_CMD_ATTR_PID
]);
405 stats
= mk_reply(rep_skb
, TASKSTATS_TYPE_PID
, pid
);
409 rc
= fill_pid(pid
, NULL
, stats
);
412 } else if (info
->attrs
[TASKSTATS_CMD_ATTR_TGID
]) {
413 u32 tgid
= nla_get_u32(info
->attrs
[TASKSTATS_CMD_ATTR_TGID
]);
414 stats
= mk_reply(rep_skb
, TASKSTATS_TYPE_TGID
, tgid
);
418 rc
= fill_tgid(tgid
, NULL
, stats
);
424 return send_reply(rep_skb
, info
->snd_pid
);
430 static struct taskstats
*taskstats_tgid_alloc(struct task_struct
*tsk
)
432 struct signal_struct
*sig
= tsk
->signal
;
433 struct taskstats
*stats
;
435 if (sig
->stats
|| thread_group_empty(tsk
))
438 /* No problem if kmem_cache_zalloc() fails */
439 stats
= kmem_cache_zalloc(taskstats_cache
, GFP_KERNEL
);
441 spin_lock_irq(&tsk
->sighand
->siglock
);
446 spin_unlock_irq(&tsk
->sighand
->siglock
);
449 kmem_cache_free(taskstats_cache
, stats
);
454 /* Send pid data out on exit */
455 void taskstats_exit(struct task_struct
*tsk
, int group_dead
)
458 struct listener_list
*listeners
;
459 struct taskstats
*stats
;
460 struct sk_buff
*rep_skb
;
464 if (!family_registered
)
468 * Size includes space for nested attributes
470 size
= nla_total_size(sizeof(u32
)) +
471 nla_total_size(sizeof(struct taskstats
)) + nla_total_size(0);
473 is_thread_group
= !!taskstats_tgid_alloc(tsk
);
474 if (is_thread_group
) {
475 /* PID + STATS + TGID + STATS */
477 /* fill the tsk->signal->stats structure */
481 listeners
= &__raw_get_cpu_var(listener_array
);
482 if (list_empty(&listeners
->list
))
485 rc
= prepare_reply(NULL
, TASKSTATS_CMD_NEW
, &rep_skb
, size
);
489 stats
= mk_reply(rep_skb
, TASKSTATS_TYPE_PID
, tsk
->pid
);
493 rc
= fill_pid(tsk
->pid
, tsk
, stats
);
498 * Doesn't matter if tsk is the leader or the last group member leaving
500 if (!is_thread_group
|| !group_dead
)
503 stats
= mk_reply(rep_skb
, TASKSTATS_TYPE_TGID
, tsk
->tgid
);
507 memcpy(stats
, tsk
->signal
->stats
, sizeof(*stats
));
510 send_cpu_listeners(rep_skb
, listeners
);
516 static struct genl_ops taskstats_ops
= {
517 .cmd
= TASKSTATS_CMD_GET
,
518 .doit
= taskstats_user_cmd
,
519 .policy
= taskstats_cmd_get_policy
,
522 /* Needed early in initialization */
523 void __init
taskstats_init_early(void)
527 taskstats_cache
= kmem_cache_create("taskstats_cache",
528 sizeof(struct taskstats
),
529 0, SLAB_PANIC
, NULL
, NULL
);
530 for_each_possible_cpu(i
) {
531 INIT_LIST_HEAD(&(per_cpu(listener_array
, i
).list
));
532 init_rwsem(&(per_cpu(listener_array
, i
).sem
));
536 static int __init
taskstats_init(void)
540 rc
= genl_register_family(&family
);
544 rc
= genl_register_ops(&family
, &taskstats_ops
);
548 family_registered
= 1;
551 genl_unregister_family(&family
);
556 * late initcall ensures initialization of statistics collection
557 * mechanisms precedes initialization of the taskstats interface
559 late_initcall(taskstats_init
);