tsacct.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include <linux/jiffies.h>
  23. #define USEC_PER_TICK (USEC_PER_SEC/HZ)
  24. /*
  25. * fill in basic accounting fields
  26. */
  27. void bacct_add_tsk(struct taskstats *stats, struct task_struct *tsk)
  28. {
  29. struct timespec uptime, ts;
  30. s64 ac_etime;
  31. BUILD_BUG_ON(TS_COMM_LEN < TASK_COMM_LEN);
  32. /* calculate task elapsed time in timespec */
  33. do_posix_clock_monotonic_gettime(&uptime);
  34. ts = timespec_sub(uptime, tsk->start_time);
  35. /* rebase elapsed time to usec */
  36. ac_etime = timespec_to_ns(&ts);
  37. do_div(ac_etime, NSEC_PER_USEC);
  38. stats->ac_etime = ac_etime;
  39. stats->ac_btime = xtime.tv_sec - ts.tv_sec;
  40. if (thread_group_leader(tsk)) {
  41. stats->ac_exitcode = tsk->exit_code;
  42. if (tsk->flags & PF_FORKNOEXEC)
  43. stats->ac_flag |= AFORK;
  44. }
  45. if (tsk->flags & PF_SUPERPRIV)
  46. stats->ac_flag |= ASU;
  47. if (tsk->flags & PF_DUMPCORE)
  48. stats->ac_flag |= ACORE;
  49. if (tsk->flags & PF_SIGNALED)
  50. stats->ac_flag |= AXSIG;
  51. stats->ac_nice = task_nice(tsk);
  52. stats->ac_sched = tsk->policy;
  53. stats->ac_uid = tsk->uid;
  54. stats->ac_gid = tsk->gid;
  55. stats->ac_pid = tsk->pid;
  56. rcu_read_lock();
  57. stats->ac_ppid = pid_alive(tsk) ?
  58. rcu_dereference(tsk->real_parent)->tgid : 0;
  59. rcu_read_unlock();
  60. stats->ac_utime = cputime_to_msecs(tsk->utime) * USEC_PER_MSEC;
  61. stats->ac_stime = cputime_to_msecs(tsk->stime) * USEC_PER_MSEC;
  62. stats->ac_minflt = tsk->min_flt;
  63. stats->ac_majflt = tsk->maj_flt;
  64. strncpy(stats->ac_comm, tsk->comm, sizeof(stats->ac_comm));
  65. }
  66. #ifdef CONFIG_TASK_XACCT
  67. #define KB 1024
  68. #define MB (1024*KB)
  69. /*
  70. * fill in extended accounting fields
  71. */
  72. void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
  73. {
  74. struct mm_struct *mm;
  75. /* convert pages-jiffies to Mbyte-usec */
  76. stats->coremem = jiffies_to_usecs(p->acct_rss_mem1) * PAGE_SIZE / MB;
  77. stats->virtmem = jiffies_to_usecs(p->acct_vm_mem1) * PAGE_SIZE / MB;
  78. mm = get_task_mm(p);
  79. if (mm) {
  80. /* adjust to KB unit */
  81. stats->hiwater_rss = mm->hiwater_rss * PAGE_SIZE / KB;
  82. stats->hiwater_vm = mm->hiwater_vm * PAGE_SIZE / KB;
  83. mmput(mm);
  84. }
  85. stats->read_char = p->rchar;
  86. stats->write_char = p->wchar;
  87. stats->read_syscalls = p->syscr;
  88. stats->write_syscalls = p->syscw;
  89. #ifdef CONFIG_TASK_IO_ACCOUNTING
  90. stats->read_bytes = p->ioac.read_bytes;
  91. stats->write_bytes = p->ioac.write_bytes;
  92. stats->cancelled_write_bytes = p->ioac.cancelled_write_bytes;
  93. #else
  94. stats->read_bytes = 0;
  95. stats->write_bytes = 0;
  96. stats->cancelled_write_bytes = 0;
  97. #endif
  98. }
  99. #undef KB
  100. #undef MB
  101. /**
  102. * acct_update_integrals - update mm integral fields in task_struct
  103. * @tsk: task_struct for accounting
  104. */
  105. void acct_update_integrals(struct task_struct *tsk)
  106. {
  107. if (likely(tsk->mm)) {
  108. long delta = cputime_to_jiffies(
  109. cputime_sub(tsk->stime, tsk->acct_stimexpd));
  110. if (delta == 0)
  111. return;
  112. tsk->acct_stimexpd = tsk->stime;
  113. tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm);
  114. tsk->acct_vm_mem1 += delta * tsk->mm->total_vm;
  115. }
  116. }
  117. /**
  118. * acct_clear_integrals - clear the mm integral fields in task_struct
  119. * @tsk: task_struct whose accounting fields are cleared
  120. */
  121. void acct_clear_integrals(struct task_struct *tsk)
  122. {
  123. tsk->acct_stimexpd = 0;
  124. tsk->acct_rss_mem1 = 0;
  125. tsk->acct_vm_mem1 = 0;
  126. }
  127. #endif