taskstats.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. #include <linux/kernel.h>
  19. #include <linux/taskstats_kern.h>
  20. #include <linux/tsacct_kern.h>
  21. #include <linux/delayacct.h>
  22. #include <linux/tsacct_kern.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/percpu.h>
  25. #include <net/genetlink.h>
  26. #include <asm/atomic.h>
  27. /*
  28. * Maximum length of a cpumask that can be specified in
  29. * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
  30. */
  31. #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
  32. static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
  33. static int family_registered;
  34. kmem_cache_t *taskstats_cache;
  35. static struct genl_family family = {
  36. .id = GENL_ID_GENERATE,
  37. .name = TASKSTATS_GENL_NAME,
  38. .version = TASKSTATS_GENL_VERSION,
  39. .maxattr = TASKSTATS_CMD_ATTR_MAX,
  40. };
  41. static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
  42. __read_mostly = {
  43. [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
  44. [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
  45. [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
  46. [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
  47. struct listener {
  48. struct list_head list;
  49. pid_t pid;
  50. char valid;
  51. };
  52. struct listener_list {
  53. struct rw_semaphore sem;
  54. struct list_head list;
  55. };
  56. static DEFINE_PER_CPU(struct listener_list, listener_array);
  57. enum actions {
  58. REGISTER,
  59. DEREGISTER,
  60. CPU_DONT_CARE
  61. };
  62. static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
  63. void **replyp, size_t size)
  64. {
  65. struct sk_buff *skb;
  66. void *reply;
  67. /*
  68. * If new attributes are added, please revisit this allocation
  69. */
  70. skb = nlmsg_new(genlmsg_total_size(size), GFP_KERNEL);
  71. if (!skb)
  72. return -ENOMEM;
  73. if (!info) {
  74. int seq = get_cpu_var(taskstats_seqnum)++;
  75. put_cpu_var(taskstats_seqnum);
  76. reply = genlmsg_put(skb, 0, seq,
  77. family.id, 0, 0,
  78. cmd, family.version);
  79. } else
  80. reply = genlmsg_put(skb, info->snd_pid, info->snd_seq,
  81. family.id, 0, 0,
  82. cmd, family.version);
  83. if (reply == NULL) {
  84. nlmsg_free(skb);
  85. return -EINVAL;
  86. }
  87. *skbp = skb;
  88. *replyp = reply;
  89. return 0;
  90. }
  91. /*
  92. * Send taskstats data in @skb to listener with nl_pid @pid
  93. */
  94. static int send_reply(struct sk_buff *skb, pid_t pid)
  95. {
  96. struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
  97. void *reply = genlmsg_data(genlhdr);
  98. int rc;
  99. rc = genlmsg_end(skb, reply);
  100. if (rc < 0) {
  101. nlmsg_free(skb);
  102. return rc;
  103. }
  104. return genlmsg_unicast(skb, pid);
  105. }
  106. /*
  107. * Send taskstats data in @skb to listeners registered for @cpu's exit data
  108. */
  109. static void send_cpu_listeners(struct sk_buff *skb, unsigned int cpu)
  110. {
  111. struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
  112. struct listener_list *listeners;
  113. struct listener *s, *tmp;
  114. struct sk_buff *skb_next, *skb_cur = skb;
  115. void *reply = genlmsg_data(genlhdr);
  116. int rc, delcount = 0;
  117. rc = genlmsg_end(skb, reply);
  118. if (rc < 0) {
  119. nlmsg_free(skb);
  120. return;
  121. }
  122. rc = 0;
  123. listeners = &per_cpu(listener_array, cpu);
  124. down_read(&listeners->sem);
  125. list_for_each_entry(s, &listeners->list, list) {
  126. skb_next = NULL;
  127. if (!list_is_last(&s->list, &listeners->list)) {
  128. skb_next = skb_clone(skb_cur, GFP_KERNEL);
  129. if (!skb_next)
  130. break;
  131. }
  132. rc = genlmsg_unicast(skb_cur, s->pid);
  133. if (rc == -ECONNREFUSED) {
  134. s->valid = 0;
  135. delcount++;
  136. }
  137. skb_cur = skb_next;
  138. }
  139. up_read(&listeners->sem);
  140. if (skb_cur)
  141. nlmsg_free(skb_cur);
  142. if (!delcount)
  143. return;
  144. /* Delete invalidated entries */
  145. down_write(&listeners->sem);
  146. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  147. if (!s->valid) {
  148. list_del(&s->list);
  149. kfree(s);
  150. }
  151. }
  152. up_write(&listeners->sem);
  153. }
  154. static int fill_pid(pid_t pid, struct task_struct *pidtsk,
  155. struct taskstats *stats)
  156. {
  157. int rc = 0;
  158. struct task_struct *tsk = pidtsk;
  159. if (!pidtsk) {
  160. read_lock(&tasklist_lock);
  161. tsk = find_task_by_pid(pid);
  162. if (!tsk) {
  163. read_unlock(&tasklist_lock);
  164. return -ESRCH;
  165. }
  166. get_task_struct(tsk);
  167. read_unlock(&tasklist_lock);
  168. } else
  169. get_task_struct(tsk);
  170. /*
  171. * Each accounting subsystem adds calls to its functions to
  172. * fill in relevant parts of struct taskstsats as follows
  173. *
  174. * per-task-foo(stats, tsk);
  175. */
  176. delayacct_add_tsk(stats, tsk);
  177. /* fill in basic acct fields */
  178. stats->version = TASKSTATS_VERSION;
  179. bacct_add_tsk(stats, tsk);
  180. /* fill in extended acct fields */
  181. xacct_add_tsk(stats, tsk);
  182. /* Define err: label here if needed */
  183. put_task_struct(tsk);
  184. return rc;
  185. }
  186. static int fill_tgid(pid_t tgid, struct task_struct *tgidtsk,
  187. struct taskstats *stats)
  188. {
  189. struct task_struct *tsk, *first;
  190. unsigned long flags;
  191. /*
  192. * Add additional stats from live tasks except zombie thread group
  193. * leaders who are already counted with the dead tasks
  194. */
  195. first = tgidtsk;
  196. if (!first) {
  197. read_lock(&tasklist_lock);
  198. first = find_task_by_pid(tgid);
  199. if (!first) {
  200. read_unlock(&tasklist_lock);
  201. return -ESRCH;
  202. }
  203. get_task_struct(first);
  204. read_unlock(&tasklist_lock);
  205. } else
  206. get_task_struct(first);
  207. /* Start with stats from dead tasks */
  208. spin_lock_irqsave(&first->signal->stats_lock, flags);
  209. if (first->signal->stats)
  210. memcpy(stats, first->signal->stats, sizeof(*stats));
  211. spin_unlock_irqrestore(&first->signal->stats_lock, flags);
  212. tsk = first;
  213. read_lock(&tasklist_lock);
  214. do {
  215. if (tsk->exit_state == EXIT_ZOMBIE && thread_group_leader(tsk))
  216. continue;
  217. /*
  218. * Accounting subsystem can call its functions here to
  219. * fill in relevant parts of struct taskstsats as follows
  220. *
  221. * per-task-foo(stats, tsk);
  222. */
  223. delayacct_add_tsk(stats, tsk);
  224. } while_each_thread(first, tsk);
  225. read_unlock(&tasklist_lock);
  226. stats->version = TASKSTATS_VERSION;
  227. /*
  228. * Accounting subsytems can also add calls here to modify
  229. * fields of taskstats.
  230. */
  231. return 0;
  232. }
  233. static void fill_tgid_exit(struct task_struct *tsk)
  234. {
  235. unsigned long flags;
  236. spin_lock_irqsave(&tsk->signal->stats_lock, flags);
  237. if (!tsk->signal->stats)
  238. goto ret;
  239. /*
  240. * Each accounting subsystem calls its functions here to
  241. * accumalate its per-task stats for tsk, into the per-tgid structure
  242. *
  243. * per-task-foo(tsk->signal->stats, tsk);
  244. */
  245. delayacct_add_tsk(tsk->signal->stats, tsk);
  246. ret:
  247. spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
  248. return;
  249. }
  250. static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
  251. {
  252. struct listener_list *listeners;
  253. struct listener *s, *tmp;
  254. unsigned int cpu;
  255. cpumask_t mask = *maskp;
  256. if (!cpus_subset(mask, cpu_possible_map))
  257. return -EINVAL;
  258. if (isadd == REGISTER) {
  259. for_each_cpu_mask(cpu, mask) {
  260. s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
  261. cpu_to_node(cpu));
  262. if (!s)
  263. goto cleanup;
  264. s->pid = pid;
  265. INIT_LIST_HEAD(&s->list);
  266. s->valid = 1;
  267. listeners = &per_cpu(listener_array, cpu);
  268. down_write(&listeners->sem);
  269. list_add(&s->list, &listeners->list);
  270. up_write(&listeners->sem);
  271. }
  272. return 0;
  273. }
  274. /* Deregister or cleanup */
  275. cleanup:
  276. for_each_cpu_mask(cpu, mask) {
  277. listeners = &per_cpu(listener_array, cpu);
  278. down_write(&listeners->sem);
  279. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  280. if (s->pid == pid) {
  281. list_del(&s->list);
  282. kfree(s);
  283. break;
  284. }
  285. }
  286. up_write(&listeners->sem);
  287. }
  288. return 0;
  289. }
  290. static int parse(struct nlattr *na, cpumask_t *mask)
  291. {
  292. char *data;
  293. int len;
  294. int ret;
  295. if (na == NULL)
  296. return 1;
  297. len = nla_len(na);
  298. if (len > TASKSTATS_CPUMASK_MAXLEN)
  299. return -E2BIG;
  300. if (len < 1)
  301. return -EINVAL;
  302. data = kmalloc(len, GFP_KERNEL);
  303. if (!data)
  304. return -ENOMEM;
  305. nla_strlcpy(data, na, len);
  306. ret = cpulist_parse(data, *mask);
  307. kfree(data);
  308. return ret;
  309. }
  310. static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
  311. {
  312. int rc = 0;
  313. struct sk_buff *rep_skb;
  314. struct taskstats stats;
  315. void *reply;
  316. size_t size;
  317. struct nlattr *na;
  318. cpumask_t mask;
  319. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
  320. if (rc < 0)
  321. return rc;
  322. if (rc == 0)
  323. return add_del_listener(info->snd_pid, &mask, REGISTER);
  324. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
  325. if (rc < 0)
  326. return rc;
  327. if (rc == 0)
  328. return add_del_listener(info->snd_pid, &mask, DEREGISTER);
  329. /*
  330. * Size includes space for nested attributes
  331. */
  332. size = nla_total_size(sizeof(u32)) +
  333. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  334. memset(&stats, 0, sizeof(stats));
  335. rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
  336. if (rc < 0)
  337. return rc;
  338. if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
  339. u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
  340. rc = fill_pid(pid, NULL, &stats);
  341. if (rc < 0)
  342. goto err;
  343. na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
  344. NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
  345. NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
  346. stats);
  347. } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
  348. u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
  349. rc = fill_tgid(tgid, NULL, &stats);
  350. if (rc < 0)
  351. goto err;
  352. na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
  353. NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
  354. NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
  355. stats);
  356. } else {
  357. rc = -EINVAL;
  358. goto err;
  359. }
  360. nla_nest_end(rep_skb, na);
  361. return send_reply(rep_skb, info->snd_pid);
  362. nla_put_failure:
  363. return genlmsg_cancel(rep_skb, reply);
  364. err:
  365. nlmsg_free(rep_skb);
  366. return rc;
  367. }
  368. void taskstats_exit_alloc(struct taskstats **ptidstats, unsigned int *mycpu)
  369. {
  370. struct listener_list *listeners;
  371. struct taskstats *tmp;
  372. /*
  373. * This is the cpu on which the task is exiting currently and will
  374. * be the one for which the exit event is sent, even if the cpu
  375. * on which this function is running changes later.
  376. */
  377. *mycpu = raw_smp_processor_id();
  378. *ptidstats = NULL;
  379. tmp = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
  380. if (!tmp)
  381. return;
  382. listeners = &per_cpu(listener_array, *mycpu);
  383. down_read(&listeners->sem);
  384. if (!list_empty(&listeners->list)) {
  385. *ptidstats = tmp;
  386. tmp = NULL;
  387. }
  388. up_read(&listeners->sem);
  389. kfree(tmp);
  390. }
  391. /* Send pid data out on exit */
  392. void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
  393. int group_dead, unsigned int mycpu)
  394. {
  395. int rc;
  396. struct sk_buff *rep_skb;
  397. void *reply;
  398. size_t size;
  399. int is_thread_group;
  400. struct nlattr *na;
  401. unsigned long flags;
  402. if (!family_registered || !tidstats)
  403. return;
  404. spin_lock_irqsave(&tsk->signal->stats_lock, flags);
  405. is_thread_group = tsk->signal->stats ? 1 : 0;
  406. spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
  407. rc = 0;
  408. /*
  409. * Size includes space for nested attributes
  410. */
  411. size = nla_total_size(sizeof(u32)) +
  412. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  413. if (is_thread_group)
  414. size = 2 * size; /* PID + STATS + TGID + STATS */
  415. rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
  416. if (rc < 0)
  417. goto ret;
  418. rc = fill_pid(tsk->pid, tsk, tidstats);
  419. if (rc < 0)
  420. goto err_skb;
  421. na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
  422. NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
  423. NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
  424. *tidstats);
  425. nla_nest_end(rep_skb, na);
  426. if (!is_thread_group)
  427. goto send;
  428. /*
  429. * tsk has/had a thread group so fill the tsk->signal->stats structure
  430. * Doesn't matter if tsk is the leader or the last group member leaving
  431. */
  432. fill_tgid_exit(tsk);
  433. if (!group_dead)
  434. goto send;
  435. na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
  436. NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
  437. /* No locking needed for tsk->signal->stats since group is dead */
  438. NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
  439. *tsk->signal->stats);
  440. nla_nest_end(rep_skb, na);
  441. send:
  442. send_cpu_listeners(rep_skb, mycpu);
  443. return;
  444. nla_put_failure:
  445. genlmsg_cancel(rep_skb, reply);
  446. goto ret;
  447. err_skb:
  448. nlmsg_free(rep_skb);
  449. ret:
  450. return;
  451. }
  452. static struct genl_ops taskstats_ops = {
  453. .cmd = TASKSTATS_CMD_GET,
  454. .doit = taskstats_user_cmd,
  455. .policy = taskstats_cmd_get_policy,
  456. };
  457. /* Needed early in initialization */
  458. void __init taskstats_init_early(void)
  459. {
  460. unsigned int i;
  461. taskstats_cache = kmem_cache_create("taskstats_cache",
  462. sizeof(struct taskstats),
  463. 0, SLAB_PANIC, NULL, NULL);
  464. for_each_possible_cpu(i) {
  465. INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
  466. init_rwsem(&(per_cpu(listener_array, i).sem));
  467. }
  468. }
  469. static int __init taskstats_init(void)
  470. {
  471. int rc;
  472. rc = genl_register_family(&family);
  473. if (rc)
  474. return rc;
  475. rc = genl_register_ops(&family, &taskstats_ops);
  476. if (rc < 0)
  477. goto err;
  478. family_registered = 1;
  479. return 0;
  480. err:
  481. genl_unregister_family(&family);
  482. return rc;
  483. }
  484. /*
  485. * late initcall ensures initialization of statistics collection
  486. * mechanisms precedes initialization of the taskstats interface
  487. */
  488. late_initcall(taskstats_init);