tsacct.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /*
  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, tsk->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 = get_seconds() - 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. rcu_read_lock();
  56. stats->ac_ppid = pid_alive(tsk) ?
  57. rcu_dereference(tsk->real_parent)->tgid : 0;
  58. rcu_read_unlock();
  59. stats->ac_utime = cputime_to_msecs(tsk->utime) * USEC_PER_MSEC;
  60. stats->ac_stime = cputime_to_msecs(tsk->stime) * USEC_PER_MSEC;
  61. stats->ac_utimescaled =
  62. cputime_to_msecs(tsk->utimescaled) * USEC_PER_MSEC;
  63. stats->ac_stimescaled =
  64. cputime_to_msecs(tsk->stimescaled) * USEC_PER_MSEC;
  65. stats->ac_minflt = tsk->min_flt;
  66. stats->ac_majflt = tsk->maj_flt;
  67. strncpy(stats->ac_comm, tsk->comm, sizeof(stats->ac_comm));
  68. }
  69. #ifdef CONFIG_TASK_XACCT
  70. #define KB 1024
  71. #define MB (1024*KB)
  72. /*
  73. * fill in extended accounting fields
  74. */
  75. void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
  76. {
  77. struct mm_struct *mm;
  78. /* convert pages-jiffies to Mbyte-usec */
  79. stats->coremem = jiffies_to_usecs(p->acct_rss_mem1) * PAGE_SIZE / MB;
  80. stats->virtmem = jiffies_to_usecs(p->acct_vm_mem1) * PAGE_SIZE / MB;
  81. mm = get_task_mm(p);
  82. if (mm) {
  83. /* adjust to KB unit */
  84. stats->hiwater_rss = mm->hiwater_rss * PAGE_SIZE / KB;
  85. stats->hiwater_vm = mm->hiwater_vm * PAGE_SIZE / KB;
  86. mmput(mm);
  87. }
  88. stats->read_char = p->rchar;
  89. stats->write_char = p->wchar;
  90. stats->read_syscalls = p->syscr;
  91. stats->write_syscalls = p->syscw;
  92. #ifdef CONFIG_TASK_IO_ACCOUNTING
  93. stats->read_bytes = p->ioac.read_bytes;
  94. stats->write_bytes = p->ioac.write_bytes;
  95. stats->cancelled_write_bytes = p->ioac.cancelled_write_bytes;
  96. #else
  97. stats->read_bytes = 0;
  98. stats->write_bytes = 0;
  99. stats->cancelled_write_bytes = 0;
  100. #endif
  101. }
  102. #undef KB
  103. #undef MB
  104. /**
  105. * acct_update_integrals - update mm integral fields in task_struct
  106. * @tsk: task_struct for accounting
  107. */
  108. void acct_update_integrals(struct task_struct *tsk)
  109. {
  110. if (likely(tsk->mm)) {
  111. long delta = cputime_to_jiffies(
  112. cputime_sub(tsk->stime, tsk->acct_stimexpd));
  113. if (delta == 0)
  114. return;
  115. tsk->acct_stimexpd = tsk->stime;
  116. tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm);
  117. tsk->acct_vm_mem1 += delta * tsk->mm->total_vm;
  118. }
  119. }
  120. /**
  121. * acct_clear_integrals - clear the mm integral fields in task_struct
  122. * @tsk: task_struct whose accounting fields are cleared
  123. */
  124. void acct_clear_integrals(struct task_struct *tsk)
  125. {
  126. tsk->acct_stimexpd = 0;
  127. tsk->acct_rss_mem1 = 0;
  128. tsk->acct_vm_mem1 = 0;
  129. }
  130. #endif