tsacct.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * tsacct.c - System accounting over taskstats interface
  4. *
  5. * Copyright (C) Jay Lan, <jlan@sgi.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/sched/mm.h>
  10. #include <linux/sched/cputime.h>
  11. #include <linux/tsacct_kern.h>
  12. #include <linux/acct.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/mm.h>
  15. /*
  16. * fill in basic accounting fields
  17. */
  18. void bacct_add_tsk(struct user_namespace *user_ns,
  19. struct pid_namespace *pid_ns,
  20. struct taskstats *stats, struct task_struct *tsk)
  21. {
  22. const struct cred *tcred;
  23. u64 utime, stime, utimescaled, stimescaled;
  24. u64 delta;
  25. time64_t btime;
  26. BUILD_BUG_ON(TS_COMM_LEN < TASK_COMM_LEN);
  27. /* calculate task elapsed time in nsec */
  28. delta = ktime_get_ns() - tsk->start_time;
  29. /* Convert to micro seconds */
  30. do_div(delta, NSEC_PER_USEC);
  31. stats->ac_etime = delta;
  32. /* Convert to seconds for btime (note y2106 limit) */
  33. btime = ktime_get_real_seconds() - div_u64(delta, USEC_PER_SEC);
  34. stats->ac_btime = clamp_t(time64_t, btime, 0, U32_MAX);
  35. stats->ac_btime64 = btime;
  36. if (tsk->flags & PF_EXITING)
  37. stats->ac_exitcode = tsk->exit_code;
  38. if (thread_group_leader(tsk) && (tsk->flags & PF_FORKNOEXEC))
  39. stats->ac_flag |= AFORK;
  40. if (tsk->flags & PF_SUPERPRIV)
  41. stats->ac_flag |= ASU;
  42. if (tsk->flags & PF_DUMPCORE)
  43. stats->ac_flag |= ACORE;
  44. if (tsk->flags & PF_SIGNALED)
  45. stats->ac_flag |= AXSIG;
  46. stats->ac_nice = task_nice(tsk);
  47. stats->ac_sched = tsk->policy;
  48. stats->ac_pid = task_pid_nr_ns(tsk, pid_ns);
  49. rcu_read_lock();
  50. tcred = __task_cred(tsk);
  51. stats->ac_uid = from_kuid_munged(user_ns, tcred->uid);
  52. stats->ac_gid = from_kgid_munged(user_ns, tcred->gid);
  53. stats->ac_ppid = pid_alive(tsk) ?
  54. task_tgid_nr_ns(rcu_dereference(tsk->real_parent), pid_ns) : 0;
  55. rcu_read_unlock();
  56. task_cputime(tsk, &utime, &stime);
  57. stats->ac_utime = div_u64(utime, NSEC_PER_USEC);
  58. stats->ac_stime = div_u64(stime, NSEC_PER_USEC);
  59. task_cputime_scaled(tsk, &utimescaled, &stimescaled);
  60. stats->ac_utimescaled = div_u64(utimescaled, NSEC_PER_USEC);
  61. stats->ac_stimescaled = div_u64(stimescaled, NSEC_PER_USEC);
  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. #define KB_MASK (~(KB-1))
  70. /*
  71. * fill in extended accounting fields
  72. */
  73. void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
  74. {
  75. struct mm_struct *mm;
  76. /* convert pages-nsec/1024 to Mbyte-usec, see __acct_update_integrals */
  77. stats->coremem = p->acct_rss_mem1 * PAGE_SIZE;
  78. do_div(stats->coremem, 1000 * KB);
  79. stats->virtmem = p->acct_vm_mem1 * PAGE_SIZE;
  80. do_div(stats->virtmem, 1000 * KB);
  81. mm = get_task_mm(p);
  82. if (mm) {
  83. /* adjust to KB unit */
  84. stats->hiwater_rss = get_mm_hiwater_rss(mm) * PAGE_SIZE / KB;
  85. stats->hiwater_vm = get_mm_hiwater_vm(mm) * PAGE_SIZE / KB;
  86. mmput(mm);
  87. }
  88. stats->read_char = p->ioac.rchar & KB_MASK;
  89. stats->write_char = p->ioac.wchar & KB_MASK;
  90. stats->read_syscalls = p->ioac.syscr & KB_MASK;
  91. stats->write_syscalls = p->ioac.syscw & KB_MASK;
  92. #ifdef CONFIG_TASK_IO_ACCOUNTING
  93. stats->read_bytes = p->ioac.read_bytes & KB_MASK;
  94. stats->write_bytes = p->ioac.write_bytes & KB_MASK;
  95. stats->cancelled_write_bytes = p->ioac.cancelled_write_bytes & KB_MASK;
  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. static void __acct_update_integrals(struct task_struct *tsk,
  105. u64 utime, u64 stime)
  106. {
  107. u64 time, delta;
  108. if (!likely(tsk->mm))
  109. return;
  110. time = stime + utime;
  111. delta = time - tsk->acct_timexpd;
  112. if (delta < TICK_NSEC)
  113. return;
  114. tsk->acct_timexpd = time;
  115. /*
  116. * Divide by 1024 to avoid overflow, and to avoid division.
  117. * The final unit reported to userspace is Mbyte-usecs,
  118. * the rest of the math is done in xacct_add_tsk.
  119. */
  120. tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm) >> 10;
  121. tsk->acct_vm_mem1 += delta * tsk->mm->total_vm >> 10;
  122. }
  123. /**
  124. * acct_update_integrals - update mm integral fields in task_struct
  125. * @tsk: task_struct for accounting
  126. */
  127. void acct_update_integrals(struct task_struct *tsk)
  128. {
  129. u64 utime, stime;
  130. unsigned long flags;
  131. local_irq_save(flags);
  132. task_cputime(tsk, &utime, &stime);
  133. __acct_update_integrals(tsk, utime, stime);
  134. local_irq_restore(flags);
  135. }
  136. /**
  137. * acct_account_cputime - update mm integral after cputime update
  138. * @tsk: task_struct for accounting
  139. */
  140. void acct_account_cputime(struct task_struct *tsk)
  141. {
  142. __acct_update_integrals(tsk, tsk->utime, tsk->stime);
  143. }
  144. /**
  145. * acct_clear_integrals - clear the mm integral fields in task_struct
  146. * @tsk: task_struct whose accounting fields are cleared
  147. */
  148. void acct_clear_integrals(struct task_struct *tsk)
  149. {
  150. tsk->acct_timexpd = 0;
  151. tsk->acct_rss_mem1 = 0;
  152. tsk->acct_vm_mem1 = 0;
  153. }
  154. #endif