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/delayacct.h>
22 #include <linux/cpumask.h>
23 #include <linux/percpu.h>
24 #include <net/genetlink.h>
25 #include <asm/atomic.h>
28 * Maximum length of a cpumask that can be specified in
29 * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
31 #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
33 static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
34 static int family_registered;
35 kmem_cache_t *taskstats_cache;
37 static struct genl_family family = {
38 .id = GENL_ID_GENERATE,
39 .name = TASKSTATS_GENL_NAME,
40 .version = TASKSTATS_GENL_VERSION,
41 .maxattr = TASKSTATS_CMD_ATTR_MAX,
44 static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
46 [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
47 [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
48 [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
49 [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
52 struct list_head list;
57 struct listener_list {
58 struct rw_semaphore sem;
59 struct list_head list;
61 static DEFINE_PER_CPU(struct listener_list, listener_array);
69 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
70 void **replyp, size_t size)
76 * If new attributes are added, please revisit this allocation
78 skb = nlmsg_new(size, GFP_KERNEL);
83 int seq = get_cpu_var(taskstats_seqnum)++;
84 put_cpu_var(taskstats_seqnum);
86 reply = genlmsg_put(skb, 0, seq,
90 reply = genlmsg_put(skb, info->snd_pid, info->snd_seq,
104 * Send taskstats data in @skb to listener with nl_pid @pid
106 static int send_reply(struct sk_buff *skb, pid_t pid)
108 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
109 void *reply = genlmsg_data(genlhdr);
112 rc = genlmsg_end(skb, reply);
118 return genlmsg_unicast(skb, pid);
122 * Send taskstats data in @skb to listeners registered for @cpu's exit data
124 static void send_cpu_listeners(struct sk_buff *skb, unsigned int cpu)
126 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
127 struct listener_list *listeners;
128 struct listener *s, *tmp;
129 struct sk_buff *skb_next, *skb_cur = skb;
130 void *reply = genlmsg_data(genlhdr);
131 int rc, delcount = 0;
133 rc = genlmsg_end(skb, reply);
140 listeners = &per_cpu(listener_array, cpu);
141 down_read(&listeners->sem);
142 list_for_each_entry(s, &listeners->list, list) {
144 if (!list_is_last(&s->list, &listeners->list)) {
145 skb_next = skb_clone(skb_cur, GFP_KERNEL);
149 rc = genlmsg_unicast(skb_cur, s->pid);
150 if (rc == -ECONNREFUSED) {
156 up_read(&listeners->sem);
164 /* Delete invalidated entries */
165 down_write(&listeners->sem);
166 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
172 up_write(&listeners->sem);
175 static int fill_pid(pid_t pid, struct task_struct *pidtsk,
176 struct taskstats *stats)
179 struct task_struct *tsk = pidtsk;
182 read_lock(&tasklist_lock);
183 tsk = find_task_by_pid(pid);
185 read_unlock(&tasklist_lock);
188 get_task_struct(tsk);
189 read_unlock(&tasklist_lock);
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);
201 stats->version = TASKSTATS_VERSION;
203 /* Define err: label here if needed */
204 put_task_struct(tsk);
209 static int fill_tgid(pid_t tgid, struct task_struct *tgidtsk,
210 struct taskstats *stats)
212 struct task_struct *tsk, *first;
216 * Add additional stats from live tasks except zombie thread group
217 * leaders who are already counted with the dead tasks
221 read_lock(&tasklist_lock);
222 first = find_task_by_pid(tgid);
224 read_unlock(&tasklist_lock);
227 get_task_struct(first);
228 read_unlock(&tasklist_lock);
230 get_task_struct(first);
232 /* Start with stats from dead tasks */
233 spin_lock_irqsave(&first->signal->stats_lock, flags);
234 if (first->signal->stats)
235 memcpy(stats, first->signal->stats, sizeof(*stats));
236 spin_unlock_irqrestore(&first->signal->stats_lock, flags);
239 read_lock(&tasklist_lock);
241 if (tsk->exit_state == EXIT_ZOMBIE && thread_group_leader(tsk))
244 * Accounting subsystem can call its functions here to
245 * fill in relevant parts of struct taskstsats as follows
247 * per-task-foo(stats, tsk);
249 delayacct_add_tsk(stats, tsk);
251 } while_each_thread(first, tsk);
252 read_unlock(&tasklist_lock);
253 stats->version = TASKSTATS_VERSION;
256 * Accounting subsytems can also add calls here to modify
257 * fields of taskstats.
264 static void fill_tgid_exit(struct task_struct *tsk)
268 spin_lock_irqsave(&tsk->signal->stats_lock, 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->signal->stats_lock, 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 return 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;
458 if (!family_registered || !tidstats)
461 spin_lock_irqsave(&tsk->signal->stats_lock, flags);
462 is_thread_group = tsk->signal->stats ? 1 : 0;
463 spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
467 * Size includes space for nested attributes
469 size = nla_total_size(sizeof(u32)) +
470 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
473 size = 2 * size; /* PID + STATS + TGID + STATS */
475 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
479 rc = fill_pid(tsk->pid, tsk, tidstats);
483 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
484 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
485 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
487 nla_nest_end(rep_skb, na);
489 if (!is_thread_group)
493 * tsk has/had a thread group so fill the tsk->signal->stats structure
494 * 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);
521 static struct genl_ops taskstats_ops = {
522 .cmd = TASKSTATS_CMD_GET,
523 .doit = taskstats_user_cmd,
524 .policy = taskstats_cmd_get_policy,
527 /* Needed early in initialization */
528 void __init taskstats_init_early(void)
532 taskstats_cache = kmem_cache_create("taskstats_cache",
533 sizeof(struct taskstats),
534 0, SLAB_PANIC, NULL, NULL);
535 for_each_possible_cpu(i) {
536 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
537 init_rwsem(&(per_cpu(listener_array, i).sem));
541 static int __init taskstats_init(void)
545 rc = genl_register_family(&family);
549 rc = genl_register_ops(&family, &taskstats_ops);
553 family_registered = 1;
556 genl_unregister_family(&family);
561 * late initcall ensures initialization of statistics collection
562 * mechanisms precedes initialization of the taskstats interface
564 late_initcall(taskstats_init);