[PATCH] delay accounting taskstats interface send tgid once
[safe/jmp/linux-2.6] / kernel / taskstats.c
1 /*
2  * taskstats.c - Export per-task statistics to userland
3  *
4  * Copyright (C) Shailabh Nagar, IBM Corp. 2006
5  *           (C) Balbir Singh,   IBM Corp. 2006
6  *
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.
11  *
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.
16  *
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/taskstats_kern.h>
21 #include <linux/delayacct.h>
22 #include <net/genetlink.h>
23 #include <asm/atomic.h>
24
25 static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
26 static int family_registered;
27 kmem_cache_t *taskstats_cache;
28
29 static struct genl_family family = {
30         .id             = GENL_ID_GENERATE,
31         .name           = TASKSTATS_GENL_NAME,
32         .version        = TASKSTATS_GENL_VERSION,
33         .maxattr        = TASKSTATS_CMD_ATTR_MAX,
34 };
35
36 static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
37 __read_mostly = {
38         [TASKSTATS_CMD_ATTR_PID]  = { .type = NLA_U32 },
39         [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
40 };
41
42
43 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
44                         void **replyp, size_t size)
45 {
46         struct sk_buff *skb;
47         void *reply;
48
49         /*
50          * If new attributes are added, please revisit this allocation
51          */
52         skb = nlmsg_new(size);
53         if (!skb)
54                 return -ENOMEM;
55
56         if (!info) {
57                 int seq = get_cpu_var(taskstats_seqnum)++;
58                 put_cpu_var(taskstats_seqnum);
59
60                 reply = genlmsg_put(skb, 0, seq,
61                                 family.id, 0, 0,
62                                 cmd, family.version);
63         } else
64                 reply = genlmsg_put(skb, info->snd_pid, info->snd_seq,
65                                 family.id, 0, 0,
66                                 cmd, family.version);
67         if (reply == NULL) {
68                 nlmsg_free(skb);
69                 return -EINVAL;
70         }
71
72         *skbp = skb;
73         *replyp = reply;
74         return 0;
75 }
76
77 static int send_reply(struct sk_buff *skb, pid_t pid, int event)
78 {
79         struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
80         void *reply;
81         int rc;
82
83         reply = genlmsg_data(genlhdr);
84
85         rc = genlmsg_end(skb, reply);
86         if (rc < 0) {
87                 nlmsg_free(skb);
88                 return rc;
89         }
90
91         if (event == TASKSTATS_MSG_MULTICAST)
92                 return genlmsg_multicast(skb, pid, TASKSTATS_LISTEN_GROUP);
93         return genlmsg_unicast(skb, pid);
94 }
95
96 static int fill_pid(pid_t pid, struct task_struct *pidtsk,
97                 struct taskstats *stats)
98 {
99         int rc;
100         struct task_struct *tsk = pidtsk;
101
102         if (!pidtsk) {
103                 read_lock(&tasklist_lock);
104                 tsk = find_task_by_pid(pid);
105                 if (!tsk) {
106                         read_unlock(&tasklist_lock);
107                         return -ESRCH;
108                 }
109                 get_task_struct(tsk);
110                 read_unlock(&tasklist_lock);
111         } else
112                 get_task_struct(tsk);
113
114         /*
115          * Each accounting subsystem adds calls to its functions to
116          * fill in relevant parts of struct taskstsats as follows
117          *
118          *      rc = per-task-foo(stats, tsk);
119          *      if (rc)
120          *              goto err;
121          */
122
123         rc = delayacct_add_tsk(stats, tsk);
124         stats->version = TASKSTATS_VERSION;
125
126         /* Define err: label here if needed */
127         put_task_struct(tsk);
128         return rc;
129
130 }
131
132 static int fill_tgid(pid_t tgid, struct task_struct *tgidtsk,
133                 struct taskstats *stats)
134 {
135         struct task_struct *tsk, *first;
136         unsigned long flags;
137
138         /*
139          * Add additional stats from live tasks except zombie thread group
140          * leaders who are already counted with the dead tasks
141          */
142         first = tgidtsk;
143         if (!first) {
144                 read_lock(&tasklist_lock);
145                 first = find_task_by_pid(tgid);
146                 if (!first) {
147                         read_unlock(&tasklist_lock);
148                         return -ESRCH;
149                 }
150                 get_task_struct(first);
151                 read_unlock(&tasklist_lock);
152         } else
153                 get_task_struct(first);
154
155         /* Start with stats from dead tasks */
156         spin_lock_irqsave(&first->signal->stats_lock, flags);
157         if (first->signal->stats)
158                 memcpy(stats, first->signal->stats, sizeof(*stats));
159         spin_unlock_irqrestore(&first->signal->stats_lock, flags);
160
161         tsk = first;
162         read_lock(&tasklist_lock);
163         do {
164                 if (tsk->exit_state == EXIT_ZOMBIE && thread_group_leader(tsk))
165                         continue;
166                 /*
167                  * Accounting subsystem can call its functions here to
168                  * fill in relevant parts of struct taskstsats as follows
169                  *
170                  *      per-task-foo(stats, tsk);
171                  */
172                 delayacct_add_tsk(stats, tsk);
173
174         } while_each_thread(first, tsk);
175         read_unlock(&tasklist_lock);
176         stats->version = TASKSTATS_VERSION;
177
178         /*
179          * Accounting subsytems can also add calls here to modify
180          * fields of taskstats.
181          */
182
183         return 0;
184 }
185
186
187 static void fill_tgid_exit(struct task_struct *tsk)
188 {
189         unsigned long flags;
190
191         spin_lock_irqsave(&tsk->signal->stats_lock, flags);
192         if (!tsk->signal->stats)
193                 goto ret;
194
195         /*
196          * Each accounting subsystem calls its functions here to
197          * accumalate its per-task stats for tsk, into the per-tgid structure
198          *
199          *      per-task-foo(tsk->signal->stats, tsk);
200          */
201         delayacct_add_tsk(tsk->signal->stats, tsk);
202 ret:
203         spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
204         return;
205 }
206
207
208 static int taskstats_send_stats(struct sk_buff *skb, struct genl_info *info)
209 {
210         int rc = 0;
211         struct sk_buff *rep_skb;
212         struct taskstats stats;
213         void *reply;
214         size_t size;
215         struct nlattr *na;
216
217         /*
218          * Size includes space for nested attributes
219          */
220         size = nla_total_size(sizeof(u32)) +
221                 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
222
223         memset(&stats, 0, sizeof(stats));
224         rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
225         if (rc < 0)
226                 return rc;
227
228         if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
229                 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
230                 rc = fill_pid(pid, NULL, &stats);
231                 if (rc < 0)
232                         goto err;
233
234                 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
235                 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
236                 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
237                                 stats);
238         } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
239                 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
240                 rc = fill_tgid(tgid, NULL, &stats);
241                 if (rc < 0)
242                         goto err;
243
244                 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
245                 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
246                 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
247                                 stats);
248         } else {
249                 rc = -EINVAL;
250                 goto err;
251         }
252
253         nla_nest_end(rep_skb, na);
254
255         return send_reply(rep_skb, info->snd_pid, TASKSTATS_MSG_UNICAST);
256
257 nla_put_failure:
258         return genlmsg_cancel(rep_skb, reply);
259 err:
260         nlmsg_free(rep_skb);
261         return rc;
262 }
263
264 /* Send pid data out on exit */
265 void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
266                         int group_dead)
267 {
268         int rc;
269         struct sk_buff *rep_skb;
270         void *reply;
271         size_t size;
272         int is_thread_group;
273         struct nlattr *na;
274         unsigned long flags;
275
276         if (!family_registered || !tidstats)
277                 return;
278
279         spin_lock_irqsave(&tsk->signal->stats_lock, flags);
280         is_thread_group = tsk->signal->stats ? 1 : 0;
281         spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
282
283         rc = 0;
284         /*
285          * Size includes space for nested attributes
286          */
287         size = nla_total_size(sizeof(u32)) +
288                 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
289
290         if (is_thread_group)
291                 size = 2 * size;        /* PID + STATS + TGID + STATS */
292
293         rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
294         if (rc < 0)
295                 goto ret;
296
297         rc = fill_pid(tsk->pid, tsk, tidstats);
298         if (rc < 0)
299                 goto err_skb;
300
301         na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
302         NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
303         NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
304                         *tidstats);
305         nla_nest_end(rep_skb, na);
306
307         if (!is_thread_group)
308                 goto send;
309
310         /*
311          * tsk has/had a thread group so fill the tsk->signal->stats structure
312          * Doesn't matter if tsk is the leader or the last group member leaving
313          */
314
315         fill_tgid_exit(tsk);
316         if (!group_dead)
317                 goto send;
318
319         na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
320         NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
321         /* No locking needed for tsk->signal->stats since group is dead */
322         NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
323                         *tsk->signal->stats);
324         nla_nest_end(rep_skb, na);
325
326 send:
327         send_reply(rep_skb, 0, TASKSTATS_MSG_MULTICAST);
328         return;
329
330 nla_put_failure:
331         genlmsg_cancel(rep_skb, reply);
332         goto ret;
333 err_skb:
334         nlmsg_free(rep_skb);
335 ret:
336         return;
337 }
338
339 static struct genl_ops taskstats_ops = {
340         .cmd            = TASKSTATS_CMD_GET,
341         .doit           = taskstats_send_stats,
342         .policy         = taskstats_cmd_get_policy,
343 };
344
345 /* Needed early in initialization */
346 void __init taskstats_init_early(void)
347 {
348         taskstats_cache = kmem_cache_create("taskstats_cache",
349                                                 sizeof(struct taskstats),
350                                                 0, SLAB_PANIC, NULL, NULL);
351 }
352
353 static int __init taskstats_init(void)
354 {
355         int rc;
356
357         rc = genl_register_family(&family);
358         if (rc)
359                 return rc;
360
361         rc = genl_register_ops(&family, &taskstats_ops);
362         if (rc < 0)
363                 goto err;
364
365         family_registered = 1;
366         return 0;
367 err:
368         genl_unregister_family(&family);
369         return rc;
370 }
371
372 /*
373  * late initcall ensures initialization of statistics collection
374  * mechanisms precedes initialization of the taskstats interface
375  */
376 late_initcall(taskstats_init);