tsacct.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * tsacct.c - System accounting over taskstats interface
  3. *
  4. * Copyright (C) Jay Lan, <jlan@sgi.com>
  5. *
  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/sched.h>
  20. #include <linux/tsacct_kern.h>
  21. #include <linux/acct.h>
  22. #define USEC_PER_TICK (USEC_PER_SEC/HZ)
  23. /*
  24. * fill in basic accounting fields
  25. */
  26. void bacct_add_tsk(struct taskstats *stats, struct task_struct *tsk)
  27. {
  28. struct timespec uptime, ts;
  29. s64 ac_etime;
  30. BUILD_BUG_ON(TS_COMM_LEN < TASK_COMM_LEN);
  31. /* calculate task elapsed time in timespec */
  32. do_posix_clock_monotonic_gettime(&uptime);
  33. ts = timespec_sub(uptime, current->group_leader->start_time);
  34. /* rebase elapsed time to usec */
  35. ac_etime = timespec_to_ns(&ts);
  36. do_div(ac_etime, NSEC_PER_USEC);
  37. stats->ac_etime = ac_etime;
  38. stats->ac_btime = xtime.tv_sec - ts.tv_sec;
  39. if (thread_group_leader(tsk)) {
  40. stats->ac_exitcode = tsk->exit_code;
  41. if (tsk->flags & PF_FORKNOEXEC)
  42. stats->ac_flag |= AFORK;
  43. }
  44. if (tsk->flags & PF_SUPERPRIV)
  45. stats->ac_flag |= ASU;
  46. if (tsk->flags & PF_DUMPCORE)
  47. stats->ac_flag |= ACORE;
  48. if (tsk->flags & PF_SIGNALED)
  49. stats->ac_flag |= AXSIG;
  50. stats->ac_nice = task_nice(tsk);
  51. stats->ac_sched = tsk->policy;
  52. stats->ac_uid = tsk->uid;
  53. stats->ac_gid = tsk->gid;
  54. stats->ac_pid = tsk->pid;
  55. stats->ac_ppid = (tsk->parent) ? tsk->parent->pid : 0;
  56. stats->ac_utime = cputime_to_msecs(tsk->utime) * USEC_PER_MSEC;
  57. stats->ac_stime = cputime_to_msecs(tsk->stime) * USEC_PER_MSEC;
  58. stats->ac_minflt = tsk->min_flt;
  59. stats->ac_majflt = tsk->maj_flt;
  60. /* Each process gets a minimum of one usec cpu time */
  61. if ((stats->ac_utime == 0) && (stats->ac_stime == 0)) {
  62. stats->ac_stime = 1;
  63. }
  64. strncpy(stats->ac_comm, tsk->comm, sizeof(stats->ac_comm));
  65. }
  66. #ifdef CONFIG_TASK_XACCT
  67. /*
  68. * fill in extended accounting fields
  69. */
  70. void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
  71. {
  72. stats->acct_rss_mem1 = p->acct_rss_mem1;
  73. stats->acct_vm_mem1 = p->acct_vm_mem1;
  74. if (p->mm) {
  75. stats->hiwater_rss = p->mm->hiwater_rss;
  76. stats->hiwater_vm = p->mm->hiwater_vm;
  77. }
  78. stats->read_char = p->rchar;
  79. stats->write_char = p->wchar;
  80. stats->read_syscalls = p->syscr;
  81. stats->write_syscalls = p->syscw;
  82. }
  83. #endif