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 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,
92 reply = genlmsg_put(skb, info->snd_pid, info->snd_seq,
106 * Send taskstats data in @skb to listener with nl_pid @pid
108 static int send_reply(struct sk_buff *skb, pid_t pid)
110 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
111 void *reply = genlmsg_data(genlhdr);
114 rc = genlmsg_end(skb, reply);
120 return genlmsg_unicast(skb, pid);
124 * Send taskstats data in @skb to listeners registered for @cpu's exit data
126 static void send_cpu_listeners(struct sk_buff *skb, unsigned int cpu)
128 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
129 struct listener_list *listeners;
130 struct listener *s, *tmp;
131 struct sk_buff *skb_next, *skb_cur = skb;
132 void *reply = genlmsg_data(genlhdr);
133 int rc, delcount = 0;
135 rc = genlmsg_end(skb, reply);
142 listeners = &per_cpu(listener_array, cpu);
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(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_pid(pid);
186 get_task_struct(tsk);
191 get_task_struct(tsk);
194 * Each accounting subsystem adds calls to its functions to
195 * fill in relevant parts of struct taskstsats as follows
197 * per-task-foo(stats, tsk);
200 delayacct_add_tsk(stats, tsk);
202 /* fill in basic acct fields */
203 stats->version = TASKSTATS_VERSION;
204 bacct_add_tsk(stats, tsk);
206 /* fill in extended acct fields */
207 xacct_add_tsk(stats, tsk);
209 /* Define err: label here if needed */
210 put_task_struct(tsk);
215 static int fill_tgid(pid_t tgid, struct task_struct *first,
216 struct taskstats *stats)
218 struct task_struct *tsk;
223 * Add additional stats from live tasks except zombie thread group
224 * leaders who are already counted with the dead tasks
228 first = find_task_by_pid(tgid);
230 if (!first || !lock_task_sighand(first, &flags))
233 if (first->signal->stats)
234 memcpy(stats, first->signal->stats, sizeof(*stats));
241 * Accounting subsystem can call its functions here to
242 * fill in relevant parts of struct taskstsats as follows
244 * per-task-foo(stats, tsk);
246 delayacct_add_tsk(stats, tsk);
248 } while_each_thread(first, tsk);
250 unlock_task_sighand(first, &flags);
255 stats->version = TASKSTATS_VERSION;
257 * Accounting subsytems can also add calls here to modify
258 * fields of taskstats.
264 static void fill_tgid_exit(struct task_struct *tsk)
268 spin_lock_irqsave(&tsk->sighand->siglock, flags);
269 if (!tsk->signal->stats)
273 * Each accounting subsystem calls its functions here to
274 * accumalate its per-task stats for tsk, into the per-tgid structure
276 * per-task-foo(tsk->signal->stats, tsk);
278 delayacct_add_tsk(tsk->signal->stats, tsk);
280 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
284 static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
286 struct listener_list *listeners;
287 struct listener *s, *tmp;
289 cpumask_t mask = *maskp;
291 if (!cpus_subset(mask, cpu_possible_map))
294 if (isadd == REGISTER) {
295 for_each_cpu_mask(cpu, mask) {
296 s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
301 INIT_LIST_HEAD(&s->list);
304 listeners = &per_cpu(listener_array, cpu);
305 down_write(&listeners->sem);
306 list_add(&s->list, &listeners->list);
307 up_write(&listeners->sem);
312 /* Deregister or cleanup */
314 for_each_cpu_mask(cpu, mask) {
315 listeners = &per_cpu(listener_array, cpu);
316 down_write(&listeners->sem);
317 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
324 up_write(&listeners->sem);
329 static int parse(struct nlattr *na, cpumask_t *mask)
338 if (len > TASKSTATS_CPUMASK_MAXLEN)
342 data = kmalloc(len, GFP_KERNEL);
345 nla_strlcpy(data, na, len);
346 ret = cpulist_parse(data, *mask);
351 static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
354 struct sk_buff *rep_skb;
355 struct taskstats stats;
361 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
365 return add_del_listener(info->snd_pid, &mask, REGISTER);
367 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
371 return add_del_listener(info->snd_pid, &mask, DEREGISTER);
374 * Size includes space for nested attributes
376 size = nla_total_size(sizeof(u32)) +
377 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
379 memset(&stats, 0, sizeof(stats));
380 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
384 if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
385 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
386 rc = fill_pid(pid, NULL, &stats);
390 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
391 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
392 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
394 } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
395 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
396 rc = fill_tgid(tgid, NULL, &stats);
400 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
401 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
402 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
409 nla_nest_end(rep_skb, na);
411 return send_reply(rep_skb, info->snd_pid);
414 rc = genlmsg_cancel(rep_skb, reply);
420 void taskstats_exit_alloc(struct taskstats **ptidstats, unsigned int *mycpu)
422 struct listener_list *listeners;
423 struct taskstats *tmp;
425 * This is the cpu on which the task is exiting currently and will
426 * be the one for which the exit event is sent, even if the cpu
427 * on which this function is running changes later.
429 *mycpu = raw_smp_processor_id();
432 tmp = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
436 listeners = &per_cpu(listener_array, *mycpu);
437 down_read(&listeners->sem);
438 if (!list_empty(&listeners->list)) {
442 up_read(&listeners->sem);
446 /* Send pid data out on exit */
447 void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
448 int group_dead, unsigned int mycpu)
451 struct sk_buff *rep_skb;
457 if (!family_registered)
461 * Size includes space for nested attributes
463 size = nla_total_size(sizeof(u32)) +
464 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
466 is_thread_group = (tsk->signal->stats != NULL);
467 if (is_thread_group) {
468 /* PID + STATS + TGID + STATS */
470 /* fill the tsk->signal->stats structure */
477 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
481 rc = fill_pid(tsk->pid, tsk, tidstats);
485 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
486 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
487 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
489 nla_nest_end(rep_skb, na);
491 if (!is_thread_group)
495 * Doesn't matter if tsk is the leader or the last group member leaving
500 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
501 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
502 /* No locking needed for tsk->signal->stats since group is dead */
503 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
504 *tsk->signal->stats);
505 nla_nest_end(rep_skb, na);
508 send_cpu_listeners(rep_skb, mycpu);
512 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);