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]
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 },};
53 struct list_head list;
58 struct listener_list {
59 struct rw_semaphore sem;
60 struct list_head list;
62 static DEFINE_PER_CPU(struct listener_list, listener_array);
70 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
77 * If new attributes are added, please revisit this allocation
79 skb = genlmsg_new(size, GFP_KERNEL);
84 int seq = get_cpu_var(taskstats_seqnum)++;
85 put_cpu_var(taskstats_seqnum);
87 reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
89 reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
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);
108 rc = genlmsg_end(skb, reply);
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);
136 down_read(&listeners->sem);
137 list_for_each_entry(s, &listeners->list, list) {
139 if (!list_is_last(&s->list, &listeners->list)) {
140 skb_next = skb_clone(skb_cur, GFP_KERNEL);
144 rc = genlmsg_unicast(skb_cur, s->pid);
145 if (rc == -ECONNREFUSED) {
151 up_read(&listeners->sem);
159 /* Delete invalidated entries */
160 down_write(&listeners->sem);
161 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
167 up_write(&listeners->sem);
170 static int fill_pid(pid_t pid, struct task_struct *tsk,
171 struct taskstats *stats)
177 tsk = find_task_by_pid(pid);
179 get_task_struct(tsk);
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);
211 static int fill_tgid(pid_t tgid, struct task_struct *first,
212 struct taskstats *stats)
214 struct task_struct *tsk;
219 * Add additional stats from live tasks except zombie thread group
220 * leaders who are already counted with the dead tasks
224 first = find_task_by_pid(tgid);
226 if (!first || !lock_task_sighand(first, &flags))
229 if (first->signal->stats)
230 memcpy(stats, first->signal->stats, sizeof(*stats));
232 memset(stats, 0, sizeof(*stats));
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);
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 struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
353 struct nlattr *na, *ret;
356 aggr = (type == TASKSTATS_TYPE_PID)
357 ? TASKSTATS_TYPE_AGGR_PID
358 : TASKSTATS_TYPE_AGGR_TGID;
360 na = nla_nest_start(skb, aggr);
363 if (nla_put(skb, type, sizeof(pid), &pid) < 0)
365 ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
368 nla_nest_end(skb, na);
370 return nla_data(ret);
375 static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
378 struct sk_buff *rep_skb;
379 struct taskstats *stats;
383 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
387 return add_del_listener(info->snd_pid, &mask, REGISTER);
389 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
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);
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);
412 rc = fill_pid(pid, NULL, stats);
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);
421 rc = fill_tgid(tgid, NULL, stats);
427 return send_reply(rep_skb, info->snd_pid);
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))
441 /* No problem if kmem_cache_zalloc() fails */
442 stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
444 spin_lock_irq(&tsk->sighand->siglock);
449 spin_unlock_irq(&tsk->sighand->siglock);
452 kmem_cache_free(taskstats_cache, stats);
457 /* Send pid data out on exit */
458 void taskstats_exit(struct task_struct *tsk, int group_dead)
461 struct listener_list *listeners;
462 struct taskstats *stats;
463 struct sk_buff *rep_skb;
467 if (!family_registered)
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 */
480 /* fill the tsk->signal->stats structure */
484 listeners = &__raw_get_cpu_var(listener_array);
485 if (list_empty(&listeners->list))
488 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
492 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
496 rc = fill_pid(tsk->pid, tsk, stats);
501 * Doesn't matter if tsk is the leader or the last group member leaving
503 if (!is_thread_group || !group_dead)
506 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
510 memcpy(stats, tsk->signal->stats, sizeof(*stats));
513 send_cpu_listeners(rep_skb, listeners);
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(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)
541 rc = genl_register_family(&family);
545 rc = genl_register_ops(&family, &taskstats_ops);
549 family_registered = 1;
552 genl_unregister_family(&family);
557 * late initcall ensures initialization of statistics collection
558 * mechanisms precedes initialization of the taskstats interface
560 late_initcall(taskstats_init);