tsacct.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * tsacct.c - System accounting over taskstats interface
  4. *
  5. * Copyright (C) Jay Lan, <[email protected]>
  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 now_ns, delta;
  25. time64_t btime;
  26. BUILD_BUG_ON(TS_COMM_LEN < TASK_COMM_LEN);
  27. /* calculate task elapsed time in nsec */
  28. now_ns = ktime_get_ns();
  29. /* store whole group time first */
  30. delta = now_ns - tsk->group_leader->start_time;
  31. /* Convert to micro seconds */
  32. do_div(delta, NSEC_PER_USEC);
  33. stats->ac_tgetime = delta;
  34. delta = now_ns - tsk->start_time;
  35. do_div(delta, NSEC_PER_USEC);
  36. stats->ac_etime = delta;
  37. /* Convert to seconds for btime (note y2106 limit) */
  38. btime = ktime_get_real_seconds() - div_u64(delta, USEC_PER_SEC);
  39. stats->ac_btime = clamp_t(time64_t, btime, 0, U32_MAX);
  40. stats->ac_btime64 = btime;
  41. if (tsk->flags & PF_EXITING)
  42. stats->ac_exitcode = tsk->exit_code;
  43. if (thread_group_leader(tsk) && (tsk->flags & PF_FORKNOEXEC))
  44. stats->ac_flag |= AFORK;
  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_pid = task_pid_nr_ns(tsk, pid_ns);
  54. stats->ac_tgid = task_tgid_nr_ns(tsk, pid_ns);
  55. rcu_read_lock();
  56. tcred = __task_cred(tsk);
  57. stats->ac_uid = from_kuid_munged(user_ns, tcred->uid);
  58. stats->ac_gid = from_kgid_munged(user_ns, tcred->gid);
  59. stats->ac_ppid = pid_alive(tsk) ?
  60. task_tgid_nr_ns(rcu_dereference(tsk->real_parent), pid_ns) : 0;
  61. rcu_read_unlock();
  62. task_cputime(tsk, &utime, &stime);
  63. stats->ac_utime = div_u64(utime, NSEC_PER_USEC);
  64. stats->ac_stime = div_u64(stime, NSEC_PER_USEC);
  65. task_cputime_scaled(tsk, &utimescaled, &stimescaled);
  66. stats->ac_utimescaled = div_u64(utimescaled, NSEC_PER_USEC);
  67. stats->ac_stimescaled = div_u64(stimescaled, NSEC_PER_USEC);
  68. stats->ac_minflt = tsk->min_flt;
  69. stats->ac_majflt = tsk->maj_flt;
  70. strncpy(stats->ac_comm, tsk->comm, sizeof(stats->ac_comm));
  71. }
  72. #ifdef CONFIG_TASK_XACCT
  73. #define KB 1024
  74. #define MB (1024*KB)
  75. #define KB_MASK (~(KB-1))
  76. /*
  77. * fill in extended accounting fields
  78. */
  79. void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
  80. {
  81. struct mm_struct *mm;
  82. /* convert pages-nsec/1024 to Mbyte-usec, see __acct_update_integrals */
  83. stats->coremem = p->acct_rss_mem1 * PAGE_SIZE;
  84. do_div(stats->coremem, 1000 * KB);
  85. stats->virtmem = p->acct_vm_mem1 * PAGE_SIZE;
  86. do_div(stats->virtmem, 1000 * KB);
  87. mm = get_task_mm(p);
  88. if (mm) {
  89. /* adjust to KB unit */
  90. stats->hiwater_rss = get_mm_hiwater_rss(mm) * PAGE_SIZE / KB;
  91. stats->hiwater_vm = get_mm_hiwater_vm(mm) * PAGE_SIZE / KB;
  92. mmput(mm);
  93. }
  94. stats->read_char = p->ioac.rchar & KB_MASK;
  95. stats->write_char = p->ioac.wchar & KB_MASK;
  96. stats->read_syscalls = p->ioac.syscr & KB_MASK;
  97. stats->write_syscalls = p->ioac.syscw & KB_MASK;
  98. #ifdef CONFIG_TASK_IO_ACCOUNTING
  99. stats->read_bytes = p->ioac.read_bytes & KB_MASK;
  100. stats->write_bytes = p->ioac.write_bytes & KB_MASK;
  101. stats->cancelled_write_bytes = p->ioac.cancelled_write_bytes & KB_MASK;
  102. #else
  103. stats->read_bytes = 0;
  104. stats->write_bytes = 0;
  105. stats->cancelled_write_bytes = 0;
  106. #endif
  107. }
  108. #undef KB
  109. #undef MB
  110. static void __acct_update_integrals(struct task_struct *tsk,
  111. u64 utime, u64 stime)
  112. {
  113. u64 time, delta;
  114. if (!likely(tsk->mm))
  115. return;
  116. time = stime + utime;
  117. delta = time - tsk->acct_timexpd;
  118. if (delta < TICK_NSEC)
  119. return;
  120. tsk->acct_timexpd = time;
  121. /*
  122. * Divide by 1024 to avoid overflow, and to avoid division.
  123. * The final unit reported to userspace is Mbyte-usecs,
  124. * the rest of the math is done in xacct_add_tsk.
  125. */
  126. tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm) >> 10;
  127. tsk->acct_vm_mem1 += delta * READ_ONCE(tsk->mm->total_vm) >> 10;
  128. }
  129. /**
  130. * acct_update_integrals - update mm integral fields in task_struct
  131. * @tsk: task_struct for accounting
  132. */
  133. void acct_update_integrals(struct task_struct *tsk)
  134. {
  135. u64 utime, stime;
  136. unsigned long flags;
  137. local_irq_save(flags);
  138. task_cputime(tsk, &utime, &stime);
  139. __acct_update_integrals(tsk, utime, stime);
  140. local_irq_restore(flags);
  141. }
  142. /**
  143. * acct_account_cputime - update mm integral after cputime update
  144. * @tsk: task_struct for accounting
  145. */
  146. void acct_account_cputime(struct task_struct *tsk)
  147. {
  148. __acct_update_integrals(tsk, tsk->utime, tsk->stime);
  149. }
  150. /**
  151. * acct_clear_integrals - clear the mm integral fields in task_struct
  152. * @tsk: task_struct whose accounting fields are cleared
  153. */
  154. void acct_clear_integrals(struct task_struct *tsk)
  155. {
  156. tsk->acct_timexpd = 0;
  157. tsk->acct_rss_mem1 = 0;
  158. tsk->acct_vm_mem1 = 0;
  159. }
  160. #endif