tsacct.c 3.8 KB

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