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 * rc = per-task-foo(stats, tsk);
204 rc = delayacct_add_tsk(stats, tsk);
205 stats->version = TASKSTATS_VERSION;
207 /* Define err: label here if needed */
208 put_task_struct(tsk);
213 static int fill_tgid(pid_t tgid, struct task_struct *tgidtsk,
214 struct taskstats *stats)
216 struct task_struct *tsk, *first;
220 * Add additional stats from live tasks except zombie thread group
221 * leaders who are already counted with the dead tasks
225 read_lock(&tasklist_lock);
226 first = find_task_by_pid(tgid);
228 read_unlock(&tasklist_lock);
231 get_task_struct(first);
232 read_unlock(&tasklist_lock);
234 get_task_struct(first);
236 /* Start with stats from dead tasks */
237 spin_lock_irqsave(&first->signal->stats_lock, flags);
238 if (first->signal->stats)
239 memcpy(stats, first->signal->stats, sizeof(*stats));
240 spin_unlock_irqrestore(&first->signal->stats_lock, flags);
243 read_lock(&tasklist_lock);
245 if (tsk->exit_state == EXIT_ZOMBIE && thread_group_leader(tsk))
248 * Accounting subsystem can call its functions here to
249 * fill in relevant parts of struct taskstsats as follows
251 * per-task-foo(stats, tsk);
253 delayacct_add_tsk(stats, tsk);
255 } while_each_thread(first, tsk);
256 read_unlock(&tasklist_lock);
257 stats->version = TASKSTATS_VERSION;
260 * Accounting subsytems can also add calls here to modify
261 * fields of taskstats.
268 static void fill_tgid_exit(struct task_struct *tsk)
272 spin_lock_irqsave(&tsk->signal->stats_lock, flags);
273 if (!tsk->signal->stats)
277 * Each accounting subsystem calls its functions here to
278 * accumalate its per-task stats for tsk, into the per-tgid structure
280 * per-task-foo(tsk->signal->stats, tsk);
282 delayacct_add_tsk(tsk->signal->stats, tsk);
284 spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
288 static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
290 struct listener_list *listeners;
291 struct listener *s, *tmp;
293 cpumask_t mask = *maskp;
295 if (!cpus_subset(mask, cpu_possible_map))
298 if (isadd == REGISTER) {
299 for_each_cpu_mask(cpu, mask) {
300 s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
305 INIT_LIST_HEAD(&s->list);
308 listeners = &per_cpu(listener_array, cpu);
309 down_write(&listeners->sem);
310 list_add(&s->list, &listeners->list);
311 up_write(&listeners->sem);
316 /* Deregister or cleanup */
318 for_each_cpu_mask(cpu, mask) {
319 listeners = &per_cpu(listener_array, cpu);
320 down_write(&listeners->sem);
321 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
328 up_write(&listeners->sem);
333 static int parse(struct nlattr *na, cpumask_t *mask)
342 if (len > TASKSTATS_CPUMASK_MAXLEN)
346 data = kmalloc(len, GFP_KERNEL);
349 nla_strlcpy(data, na, len);
350 ret = cpulist_parse(data, *mask);
355 static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
358 struct sk_buff *rep_skb;
359 struct taskstats stats;
365 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
369 return add_del_listener(info->snd_pid, &mask, REGISTER);
371 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
375 return add_del_listener(info->snd_pid, &mask, DEREGISTER);
378 * Size includes space for nested attributes
380 size = nla_total_size(sizeof(u32)) +
381 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
383 memset(&stats, 0, sizeof(stats));
384 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
388 if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
389 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
390 rc = fill_pid(pid, NULL, &stats);
394 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
395 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
396 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
398 } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
399 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
400 rc = fill_tgid(tgid, NULL, &stats);
404 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
405 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
406 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
413 nla_nest_end(rep_skb, na);
415 return send_reply(rep_skb, info->snd_pid);
418 return genlmsg_cancel(rep_skb, reply);
424 void taskstats_exit_alloc(struct taskstats **ptidstats, unsigned int *mycpu)
426 struct listener_list *listeners;
427 struct taskstats *tmp;
429 * This is the cpu on which the task is exiting currently and will
430 * be the one for which the exit event is sent, even if the cpu
431 * on which this function is running changes later.
433 *mycpu = raw_smp_processor_id();
436 tmp = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
440 listeners = &per_cpu(listener_array, *mycpu);
441 down_read(&listeners->sem);
442 if (!list_empty(&listeners->list)) {
446 up_read(&listeners->sem);
450 /* Send pid data out on exit */
451 void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
452 int group_dead, unsigned int mycpu)
455 struct sk_buff *rep_skb;
462 if (!family_registered || !tidstats)
465 spin_lock_irqsave(&tsk->signal->stats_lock, flags);
466 is_thread_group = tsk->signal->stats ? 1 : 0;
467 spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
471 * Size includes space for nested attributes
473 size = nla_total_size(sizeof(u32)) +
474 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
477 size = 2 * size; /* PID + STATS + TGID + STATS */
479 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
483 rc = fill_pid(tsk->pid, tsk, tidstats);
487 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
488 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
489 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
491 nla_nest_end(rep_skb, na);
493 if (!is_thread_group)
497 * tsk has/had a thread group so fill the tsk->signal->stats structure
498 * Doesn't matter if tsk is the leader or the last group member leaving
505 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
506 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
507 /* No locking needed for tsk->signal->stats since group is dead */
508 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
509 *tsk->signal->stats);
510 nla_nest_end(rep_skb, na);
513 send_cpu_listeners(rep_skb, mycpu);
517 genlmsg_cancel(rep_skb, reply);
525 static struct genl_ops taskstats_ops = {
526 .cmd = TASKSTATS_CMD_GET,
527 .doit = taskstats_user_cmd,
528 .policy = taskstats_cmd_get_policy,
531 /* Needed early in initialization */
532 void __init taskstats_init_early(void)
536 taskstats_cache = kmem_cache_create("taskstats_cache",
537 sizeof(struct taskstats),
538 0, SLAB_PANIC, NULL, NULL);
539 for_each_possible_cpu(i) {
540 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
541 init_rwsem(&(per_cpu(listener_array, i).sem));
545 static int __init taskstats_init(void)
549 rc = genl_register_family(&family);
553 rc = genl_register_ops(&family, &taskstats_ops);
557 family_registered = 1;
560 genl_unregister_family(&family);
565 * late initcall ensures initialization of statistics collection
566 * mechanisms precedes initialization of the taskstats interface
568 late_initcall(taskstats_init);