Use num_possible_cpus() instead of NR_CPUS for timer distribution
[linux-2.6/mini2440.git] / kernel / taskstats.c
blob7d4d7f9c1bb2a0575cf15f9119c403e696657bb8
1 /*
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/cpumask.h>
24 #include <linux/percpu.h>
25 #include <net/genetlink.h>
26 #include <asm/atomic.h>
29 * Maximum length of a cpumask that can be specified in
30 * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
32 #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
34 static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
35 static int family_registered;
36 struct kmem_cache *taskstats_cache;
38 static struct genl_family family = {
39 .id = GENL_ID_GENERATE,
40 .name = TASKSTATS_GENL_NAME,
41 .version = TASKSTATS_GENL_VERSION,
42 .maxattr = TASKSTATS_CMD_ATTR_MAX,
45 static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
46 __read_mostly = {
47 [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
48 [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
49 [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
50 [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
52 struct listener {
53 struct list_head list;
54 pid_t pid;
55 char valid;
58 struct listener_list {
59 struct rw_semaphore sem;
60 struct list_head list;
62 static DEFINE_PER_CPU(struct listener_list, listener_array);
64 enum actions {
65 REGISTER,
66 DEREGISTER,
67 CPU_DONT_CARE
70 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
71 size_t size)
73 struct sk_buff *skb;
74 void *reply;
77 * If new attributes are added, please revisit this allocation
79 skb = genlmsg_new(size, GFP_KERNEL);
80 if (!skb)
81 return -ENOMEM;
83 if (!info) {
84 int seq = get_cpu_var(taskstats_seqnum)++;
85 put_cpu_var(taskstats_seqnum);
87 reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
88 } else
89 reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
90 if (reply == NULL) {
91 nlmsg_free(skb);
92 return -EINVAL;
95 *skbp = skb;
96 return 0;
100 * Send taskstats data in @skb to listener with nl_pid @pid
102 static int send_reply(struct sk_buff *skb, pid_t pid)
104 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
105 void *reply = genlmsg_data(genlhdr);
106 int rc;
108 rc = genlmsg_end(skb, reply);
109 if (rc < 0) {
110 nlmsg_free(skb);
111 return rc;
114 return genlmsg_unicast(skb, pid);
118 * Send taskstats data in @skb to listeners registered for @cpu's exit data
120 static void send_cpu_listeners(struct sk_buff *skb,
121 struct listener_list *listeners)
123 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
124 struct listener *s, *tmp;
125 struct sk_buff *skb_next, *skb_cur = skb;
126 void *reply = genlmsg_data(genlhdr);
127 int rc, delcount = 0;
129 rc = genlmsg_end(skb, reply);
130 if (rc < 0) {
131 nlmsg_free(skb);
132 return;
135 rc = 0;
136 down_read(&listeners->sem);
137 list_for_each_entry(s, &listeners->list, list) {
138 skb_next = NULL;
139 if (!list_is_last(&s->list, &listeners->list)) {
140 skb_next = skb_clone(skb_cur, GFP_KERNEL);
141 if (!skb_next)
142 break;
144 rc = genlmsg_unicast(skb_cur, s->pid);
145 if (rc == -ECONNREFUSED) {
146 s->valid = 0;
147 delcount++;
149 skb_cur = skb_next;
151 up_read(&listeners->sem);
153 if (skb_cur)
154 nlmsg_free(skb_cur);
156 if (!delcount)
157 return;
159 /* Delete invalidated entries */
160 down_write(&listeners->sem);
161 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
162 if (!s->valid) {
163 list_del(&s->list);
164 kfree(s);
167 up_write(&listeners->sem);
170 static int fill_pid(pid_t pid, struct task_struct *tsk,
171 struct taskstats *stats)
173 int rc = 0;
175 if (!tsk) {
176 rcu_read_lock();
177 tsk = find_task_by_pid(pid);
178 if (tsk)
179 get_task_struct(tsk);
180 rcu_read_unlock();
181 if (!tsk)
182 return -ESRCH;
183 } else
184 get_task_struct(tsk);
186 memset(stats, 0, sizeof(*stats));
188 * Each accounting subsystem adds calls to its functions to
189 * fill in relevant parts of struct taskstsats as follows
191 * per-task-foo(stats, tsk);
194 delayacct_add_tsk(stats, tsk);
196 /* fill in basic acct fields */
197 stats->version = TASKSTATS_VERSION;
198 stats->nvcsw = tsk->nvcsw;
199 stats->nivcsw = tsk->nivcsw;
200 bacct_add_tsk(stats, tsk);
202 /* fill in extended acct fields */
203 xacct_add_tsk(stats, tsk);
205 /* Define err: label here if needed */
206 put_task_struct(tsk);
207 return rc;
211 static int fill_tgid(pid_t tgid, struct task_struct *first,
212 struct taskstats *stats)
214 struct task_struct *tsk;
215 unsigned long flags;
216 int rc = -ESRCH;
219 * Add additional stats from live tasks except zombie thread group
220 * leaders who are already counted with the dead tasks
222 rcu_read_lock();
223 if (!first)
224 first = find_task_by_pid(tgid);
226 if (!first || !lock_task_sighand(first, &flags))
227 goto out;
229 if (first->signal->stats)
230 memcpy(stats, first->signal->stats, sizeof(*stats));
231 else
232 memset(stats, 0, sizeof(*stats));
234 tsk = first;
235 do {
236 if (tsk->exit_state)
237 continue;
239 * Accounting subsystem can call its functions here to
240 * fill in relevant parts of struct taskstsats as follows
242 * per-task-foo(stats, tsk);
244 delayacct_add_tsk(stats, tsk);
246 stats->nvcsw += tsk->nvcsw;
247 stats->nivcsw += tsk->nivcsw;
248 } while_each_thread(first, tsk);
250 unlock_task_sighand(first, &flags);
251 rc = 0;
252 out:
253 rcu_read_unlock();
255 stats->version = TASKSTATS_VERSION;
257 * Accounting subsytems can also add calls here to modify
258 * fields of taskstats.
260 return rc;
264 static void fill_tgid_exit(struct task_struct *tsk)
266 unsigned long flags;
268 spin_lock_irqsave(&tsk->sighand->siglock, flags);
269 if (!tsk->signal->stats)
270 goto ret;
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);
279 ret:
280 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
281 return;
284 static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
286 struct listener_list *listeners;
287 struct listener *s, *tmp;
288 unsigned int cpu;
289 cpumask_t mask = *maskp;
291 if (!cpus_subset(mask, cpu_possible_map))
292 return -EINVAL;
294 if (isadd == REGISTER) {
295 for_each_cpu_mask(cpu, mask) {
296 s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
297 cpu_to_node(cpu));
298 if (!s)
299 goto cleanup;
300 s->pid = pid;
301 INIT_LIST_HEAD(&s->list);
302 s->valid = 1;
304 listeners = &per_cpu(listener_array, cpu);
305 down_write(&listeners->sem);
306 list_add(&s->list, &listeners->list);
307 up_write(&listeners->sem);
309 return 0;
312 /* Deregister or cleanup */
313 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) {
318 if (s->pid == pid) {
319 list_del(&s->list);
320 kfree(s);
321 break;
324 up_write(&listeners->sem);
326 return 0;
329 static int parse(struct nlattr *na, cpumask_t *mask)
331 char *data;
332 int len;
333 int ret;
335 if (na == NULL)
336 return 1;
337 len = nla_len(na);
338 if (len > TASKSTATS_CPUMASK_MAXLEN)
339 return -E2BIG;
340 if (len < 1)
341 return -EINVAL;
342 data = kmalloc(len, GFP_KERNEL);
343 if (!data)
344 return -ENOMEM;
345 nla_strlcpy(data, na, len);
346 ret = cpulist_parse(data, *mask);
347 kfree(data);
348 return ret;
351 static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
353 struct nlattr *na, *ret;
354 int aggr;
356 aggr = (type == TASKSTATS_TYPE_PID)
357 ? TASKSTATS_TYPE_AGGR_PID
358 : TASKSTATS_TYPE_AGGR_TGID;
360 na = nla_nest_start(skb, aggr);
361 if (!na)
362 goto err;
363 if (nla_put(skb, type, sizeof(pid), &pid) < 0)
364 goto err;
365 ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
366 if (!ret)
367 goto err;
368 nla_nest_end(skb, na);
370 return nla_data(ret);
371 err:
372 return NULL;
375 static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
377 int rc = 0;
378 struct sk_buff *rep_skb;
379 struct taskstats *stats;
380 size_t size;
381 cpumask_t mask;
383 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
384 if (rc < 0)
385 return rc;
386 if (rc == 0)
387 return add_del_listener(info->snd_pid, &mask, REGISTER);
389 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
390 if (rc < 0)
391 return rc;
392 if (rc == 0)
393 return add_del_listener(info->snd_pid, &mask, DEREGISTER);
396 * Size includes space for nested attributes
398 size = nla_total_size(sizeof(u32)) +
399 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
401 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
402 if (rc < 0)
403 return rc;
405 rc = -EINVAL;
406 if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
407 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
408 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
409 if (!stats)
410 goto err;
412 rc = fill_pid(pid, NULL, stats);
413 if (rc < 0)
414 goto err;
415 } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
416 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
417 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
418 if (!stats)
419 goto err;
421 rc = fill_tgid(tgid, NULL, stats);
422 if (rc < 0)
423 goto err;
424 } else
425 goto err;
427 return send_reply(rep_skb, info->snd_pid);
428 err:
429 nlmsg_free(rep_skb);
430 return rc;
433 static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
435 struct signal_struct *sig = tsk->signal;
436 struct taskstats *stats;
438 if (sig->stats || thread_group_empty(tsk))
439 goto ret;
441 /* No problem if kmem_cache_zalloc() fails */
442 stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
444 spin_lock_irq(&tsk->sighand->siglock);
445 if (!sig->stats) {
446 sig->stats = stats;
447 stats = NULL;
449 spin_unlock_irq(&tsk->sighand->siglock);
451 if (stats)
452 kmem_cache_free(taskstats_cache, stats);
453 ret:
454 return sig->stats;
457 /* Send pid data out on exit */
458 void taskstats_exit(struct task_struct *tsk, int group_dead)
460 int rc;
461 struct listener_list *listeners;
462 struct taskstats *stats;
463 struct sk_buff *rep_skb;
464 size_t size;
465 int is_thread_group;
467 if (!family_registered)
468 return;
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);
476 is_thread_group = !!taskstats_tgid_alloc(tsk);
477 if (is_thread_group) {
478 /* PID + STATS + TGID + STATS */
479 size = 2 * size;
480 /* fill the tsk->signal->stats structure */
481 fill_tgid_exit(tsk);
484 listeners = &__raw_get_cpu_var(listener_array);
485 if (list_empty(&listeners->list))
486 return;
488 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
489 if (rc < 0)
490 return;
492 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
493 if (!stats)
494 goto err;
496 rc = fill_pid(tsk->pid, tsk, stats);
497 if (rc < 0)
498 goto err;
501 * Doesn't matter if tsk is the leader or the last group member leaving
503 if (!is_thread_group || !group_dead)
504 goto send;
506 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
507 if (!stats)
508 goto err;
510 memcpy(stats, tsk->signal->stats, sizeof(*stats));
512 send:
513 send_cpu_listeners(rep_skb, listeners);
514 return;
515 err:
516 nlmsg_free(rep_skb);
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)
528 unsigned int i;
530 taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
531 for_each_possible_cpu(i) {
532 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
533 init_rwsem(&(per_cpu(listener_array, i).sem));
537 static int __init taskstats_init(void)
539 int rc;
541 rc = genl_register_family(&family);
542 if (rc)
543 return rc;
545 rc = genl_register_ops(&family, &taskstats_ops);
546 if (rc < 0)
547 goto err;
549 family_registered = 1;
550 return 0;
551 err:
552 genl_unregister_family(&family);
553 return rc;
557 * late initcall ensures initialization of statistics collection
558 * mechanisms precedes initialization of the taskstats interface
560 late_initcall(taskstats_init);