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);
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 int 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, ret, 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_safe(s, tmp, &listeners->list, list) {
144 if (!list_is_last(&s->list, &listeners->list)) {
145 skb_next = skb_clone(skb_cur, GFP_KERNEL);
152 ret = genlmsg_unicast(skb_cur, s->pid);
153 if (ret == -ECONNREFUSED) {
160 up_read(&listeners->sem);
165 /* Delete invalidated entries */
166 down_write(&listeners->sem);
167 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
173 up_write(&listeners->sem);
177 static int fill_pid(pid_t pid, struct task_struct *pidtsk,
178 struct taskstats *stats)
181 struct task_struct *tsk = pidtsk;
184 read_lock(&tasklist_lock);
185 tsk = find_task_by_pid(pid);
187 read_unlock(&tasklist_lock);
190 get_task_struct(tsk);
191 read_unlock(&tasklist_lock);
193 get_task_struct(tsk);
196 * Each accounting subsystem adds calls to its functions to
197 * fill in relevant parts of struct taskstsats as follows
199 * per-task-foo(stats, tsk);
202 delayacct_add_tsk(stats, tsk);
203 stats->version = TASKSTATS_VERSION;
205 /* Define err: label here if needed */
206 put_task_struct(tsk);
211 static int fill_tgid(pid_t tgid, struct task_struct *tgidtsk,
212 struct taskstats *stats)
214 struct task_struct *tsk, *first;
218 * Add additional stats from live tasks except zombie thread group
219 * leaders who are already counted with the dead tasks
223 read_lock(&tasklist_lock);
224 first = find_task_by_pid(tgid);
226 read_unlock(&tasklist_lock);
229 get_task_struct(first);
230 read_unlock(&tasklist_lock);
232 get_task_struct(first);
234 /* Start with stats from dead tasks */
235 spin_lock_irqsave(&first->signal->stats_lock, flags);
236 if (first->signal->stats)
237 memcpy(stats, first->signal->stats, sizeof(*stats));
238 spin_unlock_irqrestore(&first->signal->stats_lock, flags);
241 read_lock(&tasklist_lock);
243 if (tsk->exit_state == EXIT_ZOMBIE && thread_group_leader(tsk))
246 * Accounting subsystem can call its functions here to
247 * fill in relevant parts of struct taskstsats as follows
249 * per-task-foo(stats, tsk);
251 delayacct_add_tsk(stats, tsk);
253 } while_each_thread(first, tsk);
254 read_unlock(&tasklist_lock);
255 stats->version = TASKSTATS_VERSION;
258 * Accounting subsytems can also add calls here to modify
259 * fields of taskstats.
266 static void fill_tgid_exit(struct task_struct *tsk)
270 spin_lock_irqsave(&tsk->signal->stats_lock, flags);
271 if (!tsk->signal->stats)
275 * Each accounting subsystem calls its functions here to
276 * accumalate its per-task stats for tsk, into the per-tgid structure
278 * per-task-foo(tsk->signal->stats, tsk);
280 delayacct_add_tsk(tsk->signal->stats, tsk);
282 spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
286 static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
288 struct listener_list *listeners;
289 struct listener *s, *tmp;
291 cpumask_t mask = *maskp;
293 if (!cpus_subset(mask, cpu_possible_map))
296 if (isadd == REGISTER) {
297 for_each_cpu_mask(cpu, mask) {
298 s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
303 INIT_LIST_HEAD(&s->list);
306 listeners = &per_cpu(listener_array, cpu);
307 down_write(&listeners->sem);
308 list_add(&s->list, &listeners->list);
309 up_write(&listeners->sem);
314 /* Deregister or cleanup */
316 for_each_cpu_mask(cpu, mask) {
317 listeners = &per_cpu(listener_array, cpu);
318 down_write(&listeners->sem);
319 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
326 up_write(&listeners->sem);
331 static int parse(struct nlattr *na, cpumask_t *mask)
340 if (len > TASKSTATS_CPUMASK_MAXLEN)
344 data = kmalloc(len, GFP_KERNEL);
347 nla_strlcpy(data, na, len);
348 ret = cpulist_parse(data, *mask);
353 static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
356 struct sk_buff *rep_skb;
357 struct taskstats stats;
363 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
367 return add_del_listener(info->snd_pid, &mask, REGISTER);
369 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
373 return add_del_listener(info->snd_pid, &mask, DEREGISTER);
376 * Size includes space for nested attributes
378 size = nla_total_size(sizeof(u32)) +
379 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
381 memset(&stats, 0, sizeof(stats));
382 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
386 if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
387 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
388 rc = fill_pid(pid, NULL, &stats);
392 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
393 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
394 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
396 } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
397 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
398 rc = fill_tgid(tgid, NULL, &stats);
402 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
403 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
404 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
411 nla_nest_end(rep_skb, na);
413 return send_reply(rep_skb, info->snd_pid);
416 return genlmsg_cancel(rep_skb, reply);
422 void taskstats_exit_alloc(struct taskstats **ptidstats, unsigned int *mycpu)
424 struct listener_list *listeners;
425 struct taskstats *tmp;
427 * This is the cpu on which the task is exiting currently and will
428 * be the one for which the exit event is sent, even if the cpu
429 * on which this function is running changes later.
431 *mycpu = raw_smp_processor_id();
434 tmp = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
438 listeners = &per_cpu(listener_array, *mycpu);
439 down_read(&listeners->sem);
440 if (!list_empty(&listeners->list)) {
444 up_read(&listeners->sem);
448 /* Send pid data out on exit */
449 void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
450 int group_dead, unsigned int mycpu)
453 struct sk_buff *rep_skb;
460 if (!family_registered || !tidstats)
463 spin_lock_irqsave(&tsk->signal->stats_lock, flags);
464 is_thread_group = tsk->signal->stats ? 1 : 0;
465 spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
469 * Size includes space for nested attributes
471 size = nla_total_size(sizeof(u32)) +
472 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
475 size = 2 * size; /* PID + STATS + TGID + STATS */
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 * tsk has/had a thread group so fill the tsk->signal->stats structure
496 * Doesn't matter if tsk is the leader or the last group member leaving
503 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
504 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
505 /* No locking needed for tsk->signal->stats since group is dead */
506 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
507 *tsk->signal->stats);
508 nla_nest_end(rep_skb, na);
511 send_cpu_listeners(rep_skb, mycpu);
515 genlmsg_cancel(rep_skb, reply);
523 static struct genl_ops taskstats_ops = {
524 .cmd = TASKSTATS_CMD_GET,
525 .doit = taskstats_user_cmd,
526 .policy = taskstats_cmd_get_policy,
529 /* Needed early in initialization */
530 void __init taskstats_init_early(void)
534 taskstats_cache = kmem_cache_create("taskstats_cache",
535 sizeof(struct taskstats),
536 0, SLAB_PANIC, NULL, NULL);
537 for_each_possible_cpu(i) {
538 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
539 init_rwsem(&(per_cpu(listener_array, i).sem));
543 static int __init taskstats_init(void)
547 rc = genl_register_family(&family);
551 rc = genl_register_ops(&family, &taskstats_ops);
555 family_registered = 1;
558 genl_unregister_family(&family);
563 * late initcall ensures initialization of statistics collection
564 * mechanisms precedes initialization of the taskstats interface
566 late_initcall(taskstats_init);