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 <net/genetlink.h>
22 #include <asm/atomic.h>
24 static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
25 static int family_registered;
26 kmem_cache_t *taskstats_cache;
27 static DEFINE_MUTEX(taskstats_exit_mutex);
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,
36 static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
38 [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
39 [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
43 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
44 void **replyp, size_t size)
50 * If new attributes are added, please revisit this allocation
52 skb = nlmsg_new(size);
57 int seq = get_cpu_var(taskstats_seqnum)++;
58 put_cpu_var(taskstats_seqnum);
60 reply = genlmsg_put(skb, 0, seq,
64 reply = genlmsg_put(skb, info->snd_pid, info->snd_seq,
77 static int send_reply(struct sk_buff *skb, pid_t pid, int event)
79 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
83 reply = genlmsg_data(genlhdr);
85 rc = genlmsg_end(skb, reply);
91 if (event == TASKSTATS_MSG_MULTICAST)
92 return genlmsg_multicast(skb, pid, TASKSTATS_LISTEN_GROUP);
93 return genlmsg_unicast(skb, pid);
96 static int fill_pid(pid_t pid, struct task_struct *pidtsk,
97 struct taskstats *stats)
100 struct task_struct *tsk = pidtsk;
103 read_lock(&tasklist_lock);
104 tsk = find_task_by_pid(pid);
106 read_unlock(&tasklist_lock);
109 get_task_struct(tsk);
110 read_unlock(&tasklist_lock);
112 get_task_struct(tsk);
115 * Each accounting subsystem adds calls to its functions to
116 * fill in relevant parts of struct taskstsats as follows
118 * rc = per-task-foo(stats, tsk);
124 put_task_struct(tsk);
129 static int fill_tgid(pid_t tgid, struct task_struct *tgidtsk,
130 struct taskstats *stats)
133 struct task_struct *tsk, *first;
136 read_lock(&tasklist_lock);
138 first = find_task_by_pid(tgid);
140 read_unlock(&tasklist_lock);
147 * Each accounting subsystem adds calls its functions to
148 * fill in relevant parts of struct taskstsats as follows
150 * rc = per-task-foo(stats, tsk);
155 } while_each_thread(first, tsk);
156 read_unlock(&tasklist_lock);
159 * Accounting subsytems can also add calls here if they don't
160 * wish to aggregate statistics for per-tgid stats
166 static int taskstats_send_stats(struct sk_buff *skb, struct genl_info *info)
169 struct sk_buff *rep_skb;
170 struct taskstats stats;
176 * Size includes space for nested attributes
178 size = nla_total_size(sizeof(u32)) +
179 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
181 memset(&stats, 0, sizeof(stats));
182 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
186 if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
187 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
188 rc = fill_pid(pid, NULL, &stats);
192 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
193 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
194 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
196 } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
197 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
198 rc = fill_tgid(tgid, NULL, &stats);
202 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
203 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
204 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
211 nla_nest_end(rep_skb, na);
213 return send_reply(rep_skb, info->snd_pid, TASKSTATS_MSG_UNICAST);
216 return genlmsg_cancel(rep_skb, reply);
222 /* Send pid data out on exit */
223 void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
224 struct taskstats *tgidstats)
227 struct sk_buff *rep_skb;
233 if (!family_registered || !tidstats)
236 mutex_lock(&taskstats_exit_mutex);
238 is_thread_group = !thread_group_empty(tsk);
242 * Size includes space for nested attributes
244 size = nla_total_size(sizeof(u32)) +
245 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
248 size = 2 * size; /* PID + STATS + TGID + STATS */
250 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
254 rc = fill_pid(tsk->pid, tsk, tidstats);
258 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
259 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
260 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
262 nla_nest_end(rep_skb, na);
264 if (!is_thread_group || !tgidstats) {
265 send_reply(rep_skb, 0, TASKSTATS_MSG_MULTICAST);
269 rc = fill_tgid(tsk->pid, tsk, tgidstats);
271 * If fill_tgid() failed then one probable reason could be that the
272 * thread group leader has exited. fill_tgid() will fail, send out
273 * the pid statistics collected earlier.
276 send_reply(rep_skb, 0, TASKSTATS_MSG_MULTICAST);
280 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
281 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
282 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
284 nla_nest_end(rep_skb, na);
286 send_reply(rep_skb, 0, TASKSTATS_MSG_MULTICAST);
290 genlmsg_cancel(rep_skb, reply);
295 mutex_unlock(&taskstats_exit_mutex);
299 static struct genl_ops taskstats_ops = {
300 .cmd = TASKSTATS_CMD_GET,
301 .doit = taskstats_send_stats,
302 .policy = taskstats_cmd_get_policy,
305 /* Needed early in initialization */
306 void __init taskstats_init_early(void)
308 taskstats_cache = kmem_cache_create("taskstats_cache",
309 sizeof(struct taskstats),
310 0, SLAB_PANIC, NULL, NULL);
313 static int __init taskstats_init(void)
317 rc = genl_register_family(&family);
321 rc = genl_register_ops(&family, &taskstats_ops);
325 family_registered = 1;
328 genl_unregister_family(&family);
333 * late initcall ensures initialization of statistics collection
334 * mechanisms precedes initialization of the taskstats interface
336 late_initcall(taskstats_init);