taskstats.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. tsk = first;
  208. read_lock(&tasklist_lock);
  209. /* Start with stats from dead tasks */
  210. if (first->sighand) {
  211. spin_lock_irqsave(&first->sighand->siglock, flags);
  212. if (first->signal->stats)
  213. memcpy(stats, first->signal->stats, sizeof(*stats));
  214. spin_unlock_irqrestore(&first->sighand->siglock, flags);
  215. }
  216. do {
  217. if (tsk->exit_state == EXIT_ZOMBIE && thread_group_leader(tsk))
  218. continue;
  219. /*
  220. * Accounting subsystem can call its functions here to
  221. * fill in relevant parts of struct taskstsats as follows
  222. *
  223. * per-task-foo(stats, tsk);
  224. */
  225. delayacct_add_tsk(stats, tsk);
  226. } while_each_thread(first, tsk);
  227. read_unlock(&tasklist_lock);
  228. stats->version = TASKSTATS_VERSION;
  229. /*
  230. * Accounting subsytems can also add calls here to modify
  231. * fields of taskstats.
  232. */
  233. put_task_struct(first);
  234. return 0;
  235. }
  236. static void fill_tgid_exit(struct task_struct *tsk)
  237. {
  238. unsigned long flags;
  239. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  240. if (!tsk->signal->stats)
  241. goto ret;
  242. /*
  243. * Each accounting subsystem calls its functions here to
  244. * accumalate its per-task stats for tsk, into the per-tgid structure
  245. *
  246. * per-task-foo(tsk->signal->stats, tsk);
  247. */
  248. delayacct_add_tsk(tsk->signal->stats, tsk);
  249. ret:
  250. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  251. return;
  252. }
  253. static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
  254. {
  255. struct listener_list *listeners;
  256. struct listener *s, *tmp;
  257. unsigned int cpu;
  258. cpumask_t mask = *maskp;
  259. if (!cpus_subset(mask, cpu_possible_map))
  260. return -EINVAL;
  261. if (isadd == REGISTER) {
  262. for_each_cpu_mask(cpu, mask) {
  263. s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
  264. cpu_to_node(cpu));
  265. if (!s)
  266. goto cleanup;
  267. s->pid = pid;
  268. INIT_LIST_HEAD(&s->list);
  269. s->valid = 1;
  270. listeners = &per_cpu(listener_array, cpu);
  271. down_write(&listeners->sem);
  272. list_add(&s->list, &listeners->list);
  273. up_write(&listeners->sem);
  274. }
  275. return 0;
  276. }
  277. /* Deregister or cleanup */
  278. cleanup:
  279. for_each_cpu_mask(cpu, mask) {
  280. listeners = &per_cpu(listener_array, cpu);
  281. down_write(&listeners->sem);
  282. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  283. if (s->pid == pid) {
  284. list_del(&s->list);
  285. kfree(s);
  286. break;
  287. }
  288. }
  289. up_write(&listeners->sem);
  290. }
  291. return 0;
  292. }
  293. static int parse(struct nlattr *na, cpumask_t *mask)
  294. {
  295. char *data;
  296. int len;
  297. int ret;
  298. if (na == NULL)
  299. return 1;
  300. len = nla_len(na);
  301. if (len > TASKSTATS_CPUMASK_MAXLEN)
  302. return -E2BIG;
  303. if (len < 1)
  304. return -EINVAL;
  305. data = kmalloc(len, GFP_KERNEL);
  306. if (!data)
  307. return -ENOMEM;
  308. nla_strlcpy(data, na, len);
  309. ret = cpulist_parse(data, *mask);
  310. kfree(data);
  311. return ret;
  312. }
  313. static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
  314. {
  315. int rc = 0;
  316. struct sk_buff *rep_skb;
  317. struct taskstats stats;
  318. void *reply;
  319. size_t size;
  320. struct nlattr *na;
  321. cpumask_t mask;
  322. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
  323. if (rc < 0)
  324. return rc;
  325. if (rc == 0)
  326. return add_del_listener(info->snd_pid, &mask, REGISTER);
  327. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
  328. if (rc < 0)
  329. return rc;
  330. if (rc == 0)
  331. return add_del_listener(info->snd_pid, &mask, DEREGISTER);
  332. /*
  333. * Size includes space for nested attributes
  334. */
  335. size = nla_total_size(sizeof(u32)) +
  336. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  337. memset(&stats, 0, sizeof(stats));
  338. rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
  339. if (rc < 0)
  340. return rc;
  341. if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
  342. u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
  343. rc = fill_pid(pid, NULL, &stats);
  344. if (rc < 0)
  345. goto err;
  346. na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
  347. NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
  348. NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
  349. stats);
  350. } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
  351. u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
  352. rc = fill_tgid(tgid, NULL, &stats);
  353. if (rc < 0)
  354. goto err;
  355. na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
  356. NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
  357. NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
  358. stats);
  359. } else {
  360. rc = -EINVAL;
  361. goto err;
  362. }
  363. nla_nest_end(rep_skb, na);
  364. return send_reply(rep_skb, info->snd_pid);
  365. nla_put_failure:
  366. return genlmsg_cancel(rep_skb, reply);
  367. err:
  368. nlmsg_free(rep_skb);
  369. return rc;
  370. }
  371. void taskstats_exit_alloc(struct taskstats **ptidstats, unsigned int *mycpu)
  372. {
  373. struct listener_list *listeners;
  374. struct taskstats *tmp;
  375. /*
  376. * This is the cpu on which the task is exiting currently and will
  377. * be the one for which the exit event is sent, even if the cpu
  378. * on which this function is running changes later.
  379. */
  380. *mycpu = raw_smp_processor_id();
  381. *ptidstats = NULL;
  382. tmp = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
  383. if (!tmp)
  384. return;
  385. listeners = &per_cpu(listener_array, *mycpu);
  386. down_read(&listeners->sem);
  387. if (!list_empty(&listeners->list)) {
  388. *ptidstats = tmp;
  389. tmp = NULL;
  390. }
  391. up_read(&listeners->sem);
  392. kfree(tmp);
  393. }
  394. /* Send pid data out on exit */
  395. void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
  396. int group_dead, unsigned int mycpu)
  397. {
  398. int rc;
  399. struct sk_buff *rep_skb;
  400. void *reply;
  401. size_t size;
  402. int is_thread_group;
  403. struct nlattr *na;
  404. if (!family_registered || !tidstats)
  405. return;
  406. rc = 0;
  407. /*
  408. * Size includes space for nested attributes
  409. */
  410. size = nla_total_size(sizeof(u32)) +
  411. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  412. is_thread_group = (tsk->signal->stats != NULL);
  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);