tsacct.c 4.2 KB

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