cputime.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. #include <linux/export.h>
  2. #include <linux/sched.h>
  3. #include <linux/tsacct_kern.h>
  4. #include <linux/kernel_stat.h>
  5. #include <linux/static_key.h>
  6. #include <linux/context_tracking.h>
  7. #include "sched.h"
  8. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  9. /*
  10. * There are no locks covering percpu hardirq/softirq time.
  11. * They are only modified in vtime_account, on corresponding CPU
  12. * with interrupts disabled. So, writes are safe.
  13. * They are read and saved off onto struct rq in update_rq_clock().
  14. * This may result in other CPU reading this CPU's irq time and can
  15. * race with irq/vtime_account on this CPU. We would either get old
  16. * or new value with a side effect of accounting a slice of irq time to wrong
  17. * task when irq is in progress while we read rq->clock. That is a worthy
  18. * compromise in place of having locks on each irq in account_system_time.
  19. */
  20. DEFINE_PER_CPU(u64, cpu_hardirq_time);
  21. DEFINE_PER_CPU(u64, cpu_softirq_time);
  22. static DEFINE_PER_CPU(u64, irq_start_time);
  23. static int sched_clock_irqtime;
  24. void enable_sched_clock_irqtime(void)
  25. {
  26. sched_clock_irqtime = 1;
  27. }
  28. void disable_sched_clock_irqtime(void)
  29. {
  30. sched_clock_irqtime = 0;
  31. }
  32. #ifndef CONFIG_64BIT
  33. DEFINE_PER_CPU(seqcount_t, irq_time_seq);
  34. #endif /* CONFIG_64BIT */
  35. /*
  36. * Called before incrementing preempt_count on {soft,}irq_enter
  37. * and before decrementing preempt_count on {soft,}irq_exit.
  38. */
  39. void irqtime_account_irq(struct task_struct *curr)
  40. {
  41. unsigned long flags;
  42. s64 delta;
  43. int cpu;
  44. if (!sched_clock_irqtime)
  45. return;
  46. local_irq_save(flags);
  47. cpu = smp_processor_id();
  48. delta = sched_clock_cpu(cpu) - __this_cpu_read(irq_start_time);
  49. __this_cpu_add(irq_start_time, delta);
  50. irq_time_write_begin();
  51. /*
  52. * We do not account for softirq time from ksoftirqd here.
  53. * We want to continue accounting softirq time to ksoftirqd thread
  54. * in that case, so as not to confuse scheduler with a special task
  55. * that do not consume any time, but still wants to run.
  56. */
  57. if (hardirq_count())
  58. __this_cpu_add(cpu_hardirq_time, delta);
  59. else if (in_serving_softirq() && curr != this_cpu_ksoftirqd())
  60. __this_cpu_add(cpu_softirq_time, delta);
  61. irq_time_write_end();
  62. local_irq_restore(flags);
  63. }
  64. EXPORT_SYMBOL_GPL(irqtime_account_irq);
  65. static int irqtime_account_hi_update(void)
  66. {
  67. u64 *cpustat = kcpustat_this_cpu->cpustat;
  68. unsigned long flags;
  69. u64 latest_ns;
  70. int ret = 0;
  71. local_irq_save(flags);
  72. latest_ns = this_cpu_read(cpu_hardirq_time);
  73. if (nsecs_to_cputime64(latest_ns) > cpustat[CPUTIME_IRQ])
  74. ret = 1;
  75. local_irq_restore(flags);
  76. return ret;
  77. }
  78. static int irqtime_account_si_update(void)
  79. {
  80. u64 *cpustat = kcpustat_this_cpu->cpustat;
  81. unsigned long flags;
  82. u64 latest_ns;
  83. int ret = 0;
  84. local_irq_save(flags);
  85. latest_ns = this_cpu_read(cpu_softirq_time);
  86. if (nsecs_to_cputime64(latest_ns) > cpustat[CPUTIME_SOFTIRQ])
  87. ret = 1;
  88. local_irq_restore(flags);
  89. return ret;
  90. }
  91. #else /* CONFIG_IRQ_TIME_ACCOUNTING */
  92. #define sched_clock_irqtime (0)
  93. #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
  94. static inline void task_group_account_field(struct task_struct *p, int index,
  95. u64 tmp)
  96. {
  97. #ifdef CONFIG_CGROUP_CPUACCT
  98. struct kernel_cpustat *kcpustat;
  99. struct cpuacct *ca;
  100. #endif
  101. /*
  102. * Since all updates are sure to touch the root cgroup, we
  103. * get ourselves ahead and touch it first. If the root cgroup
  104. * is the only cgroup, then nothing else should be necessary.
  105. *
  106. */
  107. __get_cpu_var(kernel_cpustat).cpustat[index] += tmp;
  108. #ifdef CONFIG_CGROUP_CPUACCT
  109. if (unlikely(!cpuacct_subsys.active))
  110. return;
  111. rcu_read_lock();
  112. ca = task_ca(p);
  113. while (ca && (ca != &root_cpuacct)) {
  114. kcpustat = this_cpu_ptr(ca->cpustat);
  115. kcpustat->cpustat[index] += tmp;
  116. ca = parent_ca(ca);
  117. }
  118. rcu_read_unlock();
  119. #endif
  120. }
  121. /*
  122. * Account user cpu time to a process.
  123. * @p: the process that the cpu time gets accounted to
  124. * @cputime: the cpu time spent in user space since the last update
  125. * @cputime_scaled: cputime scaled by cpu frequency
  126. */
  127. void account_user_time(struct task_struct *p, cputime_t cputime,
  128. cputime_t cputime_scaled)
  129. {
  130. int index;
  131. /* Add user time to process. */
  132. p->utime += cputime;
  133. p->utimescaled += cputime_scaled;
  134. account_group_user_time(p, cputime);
  135. index = (TASK_NICE(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
  136. /* Add user time to cpustat. */
  137. task_group_account_field(p, index, (__force u64) cputime);
  138. /* Account for user time used */
  139. acct_account_cputime(p);
  140. }
  141. /*
  142. * Account guest cpu time to a process.
  143. * @p: the process that the cpu time gets accounted to
  144. * @cputime: the cpu time spent in virtual machine since the last update
  145. * @cputime_scaled: cputime scaled by cpu frequency
  146. */
  147. static void account_guest_time(struct task_struct *p, cputime_t cputime,
  148. cputime_t cputime_scaled)
  149. {
  150. u64 *cpustat = kcpustat_this_cpu->cpustat;
  151. /* Add guest time to process. */
  152. p->utime += cputime;
  153. p->utimescaled += cputime_scaled;
  154. account_group_user_time(p, cputime);
  155. p->gtime += cputime;
  156. /* Add guest time to cpustat. */
  157. if (TASK_NICE(p) > 0) {
  158. cpustat[CPUTIME_NICE] += (__force u64) cputime;
  159. cpustat[CPUTIME_GUEST_NICE] += (__force u64) cputime;
  160. } else {
  161. cpustat[CPUTIME_USER] += (__force u64) cputime;
  162. cpustat[CPUTIME_GUEST] += (__force u64) cputime;
  163. }
  164. }
  165. /*
  166. * Account system cpu time to a process and desired cpustat field
  167. * @p: the process that the cpu time gets accounted to
  168. * @cputime: the cpu time spent in kernel space since the last update
  169. * @cputime_scaled: cputime scaled by cpu frequency
  170. * @target_cputime64: pointer to cpustat field that has to be updated
  171. */
  172. static inline
  173. void __account_system_time(struct task_struct *p, cputime_t cputime,
  174. cputime_t cputime_scaled, int index)
  175. {
  176. /* Add system time to process. */
  177. p->stime += cputime;
  178. p->stimescaled += cputime_scaled;
  179. account_group_system_time(p, cputime);
  180. /* Add system time to cpustat. */
  181. task_group_account_field(p, index, (__force u64) cputime);
  182. /* Account for system time used */
  183. acct_account_cputime(p);
  184. }
  185. /*
  186. * Account system cpu time to a process.
  187. * @p: the process that the cpu time gets accounted to
  188. * @hardirq_offset: the offset to subtract from hardirq_count()
  189. * @cputime: the cpu time spent in kernel space since the last update
  190. * @cputime_scaled: cputime scaled by cpu frequency
  191. */
  192. void account_system_time(struct task_struct *p, int hardirq_offset,
  193. cputime_t cputime, cputime_t cputime_scaled)
  194. {
  195. int index;
  196. if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
  197. account_guest_time(p, cputime, cputime_scaled);
  198. return;
  199. }
  200. if (hardirq_count() - hardirq_offset)
  201. index = CPUTIME_IRQ;
  202. else if (in_serving_softirq())
  203. index = CPUTIME_SOFTIRQ;
  204. else
  205. index = CPUTIME_SYSTEM;
  206. __account_system_time(p, cputime, cputime_scaled, index);
  207. }
  208. /*
  209. * Account for involuntary wait time.
  210. * @cputime: the cpu time spent in involuntary wait
  211. */
  212. void account_steal_time(cputime_t cputime)
  213. {
  214. u64 *cpustat = kcpustat_this_cpu->cpustat;
  215. cpustat[CPUTIME_STEAL] += (__force u64) cputime;
  216. }
  217. /*
  218. * Account for idle time.
  219. * @cputime: the cpu time spent in idle wait
  220. */
  221. void account_idle_time(cputime_t cputime)
  222. {
  223. u64 *cpustat = kcpustat_this_cpu->cpustat;
  224. struct rq *rq = this_rq();
  225. if (atomic_read(&rq->nr_iowait) > 0)
  226. cpustat[CPUTIME_IOWAIT] += (__force u64) cputime;
  227. else
  228. cpustat[CPUTIME_IDLE] += (__force u64) cputime;
  229. }
  230. static __always_inline bool steal_account_process_tick(void)
  231. {
  232. #ifdef CONFIG_PARAVIRT
  233. if (static_key_false(&paravirt_steal_enabled)) {
  234. u64 steal, st = 0;
  235. steal = paravirt_steal_clock(smp_processor_id());
  236. steal -= this_rq()->prev_steal_time;
  237. st = steal_ticks(steal);
  238. this_rq()->prev_steal_time += st * TICK_NSEC;
  239. account_steal_time(st);
  240. return st;
  241. }
  242. #endif
  243. return false;
  244. }
  245. /*
  246. * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
  247. * tasks (sum on group iteration) belonging to @tsk's group.
  248. */
  249. void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
  250. {
  251. struct signal_struct *sig = tsk->signal;
  252. cputime_t utime, stime;
  253. struct task_struct *t;
  254. times->utime = sig->utime;
  255. times->stime = sig->stime;
  256. times->sum_exec_runtime = sig->sum_sched_runtime;
  257. rcu_read_lock();
  258. /* make sure we can trust tsk->thread_group list */
  259. if (!likely(pid_alive(tsk)))
  260. goto out;
  261. t = tsk;
  262. do {
  263. task_cputime(tsk, &utime, &stime);
  264. times->utime += utime;
  265. times->stime += stime;
  266. times->sum_exec_runtime += task_sched_runtime(t);
  267. } while_each_thread(tsk, t);
  268. out:
  269. rcu_read_unlock();
  270. }
  271. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  272. /*
  273. * Account a tick to a process and cpustat
  274. * @p: the process that the cpu time gets accounted to
  275. * @user_tick: is the tick from userspace
  276. * @rq: the pointer to rq
  277. *
  278. * Tick demultiplexing follows the order
  279. * - pending hardirq update
  280. * - pending softirq update
  281. * - user_time
  282. * - idle_time
  283. * - system time
  284. * - check for guest_time
  285. * - else account as system_time
  286. *
  287. * Check for hardirq is done both for system and user time as there is
  288. * no timer going off while we are on hardirq and hence we may never get an
  289. * opportunity to update it solely in system time.
  290. * p->stime and friends are only updated on system time and not on irq
  291. * softirq as those do not count in task exec_runtime any more.
  292. */
  293. static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
  294. struct rq *rq)
  295. {
  296. cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
  297. u64 *cpustat = kcpustat_this_cpu->cpustat;
  298. if (steal_account_process_tick())
  299. return;
  300. if (irqtime_account_hi_update()) {
  301. cpustat[CPUTIME_IRQ] += (__force u64) cputime_one_jiffy;
  302. } else if (irqtime_account_si_update()) {
  303. cpustat[CPUTIME_SOFTIRQ] += (__force u64) cputime_one_jiffy;
  304. } else if (this_cpu_ksoftirqd() == p) {
  305. /*
  306. * ksoftirqd time do not get accounted in cpu_softirq_time.
  307. * So, we have to handle it separately here.
  308. * Also, p->stime needs to be updated for ksoftirqd.
  309. */
  310. __account_system_time(p, cputime_one_jiffy, one_jiffy_scaled,
  311. CPUTIME_SOFTIRQ);
  312. } else if (user_tick) {
  313. account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
  314. } else if (p == rq->idle) {
  315. account_idle_time(cputime_one_jiffy);
  316. } else if (p->flags & PF_VCPU) { /* System time or guest time */
  317. account_guest_time(p, cputime_one_jiffy, one_jiffy_scaled);
  318. } else {
  319. __account_system_time(p, cputime_one_jiffy, one_jiffy_scaled,
  320. CPUTIME_SYSTEM);
  321. }
  322. }
  323. static void irqtime_account_idle_ticks(int ticks)
  324. {
  325. int i;
  326. struct rq *rq = this_rq();
  327. for (i = 0; i < ticks; i++)
  328. irqtime_account_process_tick(current, 0, rq);
  329. }
  330. #else /* CONFIG_IRQ_TIME_ACCOUNTING */
  331. static inline void irqtime_account_idle_ticks(int ticks) {}
  332. static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
  333. struct rq *rq) {}
  334. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
  335. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  336. /*
  337. * Account a single tick of cpu time.
  338. * @p: the process that the cpu time gets accounted to
  339. * @user_tick: indicates if the tick is a user or a system tick
  340. */
  341. void account_process_tick(struct task_struct *p, int user_tick)
  342. {
  343. cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
  344. struct rq *rq = this_rq();
  345. if (vtime_accounting_enabled())
  346. return;
  347. if (sched_clock_irqtime) {
  348. irqtime_account_process_tick(p, user_tick, rq);
  349. return;
  350. }
  351. if (steal_account_process_tick())
  352. return;
  353. if (user_tick)
  354. account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
  355. else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
  356. account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy,
  357. one_jiffy_scaled);
  358. else
  359. account_idle_time(cputime_one_jiffy);
  360. }
  361. /*
  362. * Account multiple ticks of steal time.
  363. * @p: the process from which the cpu time has been stolen
  364. * @ticks: number of stolen ticks
  365. */
  366. void account_steal_ticks(unsigned long ticks)
  367. {
  368. account_steal_time(jiffies_to_cputime(ticks));
  369. }
  370. /*
  371. * Account multiple ticks of idle time.
  372. * @ticks: number of stolen ticks
  373. */
  374. void account_idle_ticks(unsigned long ticks)
  375. {
  376. if (sched_clock_irqtime) {
  377. irqtime_account_idle_ticks(ticks);
  378. return;
  379. }
  380. account_idle_time(jiffies_to_cputime(ticks));
  381. }
  382. #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  383. /*
  384. * Use precise platform statistics if available:
  385. */
  386. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  387. void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  388. {
  389. *ut = p->utime;
  390. *st = p->stime;
  391. }
  392. void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  393. {
  394. struct task_cputime cputime;
  395. thread_group_cputime(p, &cputime);
  396. *ut = cputime.utime;
  397. *st = cputime.stime;
  398. }
  399. #ifndef __ARCH_HAS_VTIME_TASK_SWITCH
  400. void vtime_task_switch(struct task_struct *prev)
  401. {
  402. if (!vtime_accounting_enabled())
  403. return;
  404. if (is_idle_task(prev))
  405. vtime_account_idle(prev);
  406. else
  407. vtime_account_system(prev);
  408. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  409. vtime_account_user(prev);
  410. #endif
  411. arch_vtime_task_switch(prev);
  412. }
  413. #endif
  414. /*
  415. * Archs that account the whole time spent in the idle task
  416. * (outside irq) as idle time can rely on this and just implement
  417. * vtime_account_system() and vtime_account_idle(). Archs that
  418. * have other meaning of the idle time (s390 only includes the
  419. * time spent by the CPU when it's in low power mode) must override
  420. * vtime_account().
  421. */
  422. #ifndef __ARCH_HAS_VTIME_ACCOUNT
  423. void vtime_account_irq_enter(struct task_struct *tsk)
  424. {
  425. if (!vtime_accounting_enabled())
  426. return;
  427. if (!in_interrupt()) {
  428. /*
  429. * If we interrupted user, context_tracking_in_user()
  430. * is 1 because the context tracking don't hook
  431. * on irq entry/exit. This way we know if
  432. * we need to flush user time on kernel entry.
  433. */
  434. if (context_tracking_in_user()) {
  435. vtime_account_user(tsk);
  436. return;
  437. }
  438. if (is_idle_task(tsk)) {
  439. vtime_account_idle(tsk);
  440. return;
  441. }
  442. }
  443. vtime_account_system(tsk);
  444. }
  445. EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
  446. #endif /* __ARCH_HAS_VTIME_ACCOUNT */
  447. #else /* !CONFIG_VIRT_CPU_ACCOUNTING */
  448. static cputime_t scale_stime(cputime_t stime, cputime_t rtime, cputime_t total)
  449. {
  450. u64 temp = (__force u64) rtime;
  451. temp *= (__force u64) stime;
  452. if (sizeof(cputime_t) == 4)
  453. temp = div_u64(temp, (__force u32) total);
  454. else
  455. temp = div64_u64(temp, (__force u64) total);
  456. return (__force cputime_t) temp;
  457. }
  458. /*
  459. * Adjust tick based cputime random precision against scheduler
  460. * runtime accounting.
  461. */
  462. static void cputime_adjust(struct task_cputime *curr,
  463. struct cputime *prev,
  464. cputime_t *ut, cputime_t *st)
  465. {
  466. cputime_t rtime, stime, total;
  467. stime = curr->stime;
  468. total = stime + curr->utime;
  469. /*
  470. * Tick based cputime accounting depend on random scheduling
  471. * timeslices of a task to be interrupted or not by the timer.
  472. * Depending on these circumstances, the number of these interrupts
  473. * may be over or under-optimistic, matching the real user and system
  474. * cputime with a variable precision.
  475. *
  476. * Fix this by scaling these tick based values against the total
  477. * runtime accounted by the CFS scheduler.
  478. */
  479. rtime = nsecs_to_cputime(curr->sum_exec_runtime);
  480. if (total)
  481. stime = scale_stime(stime, rtime, total);
  482. else
  483. stime = rtime;
  484. /*
  485. * If the tick based count grows faster than the scheduler one,
  486. * the result of the scaling may go backward.
  487. * Let's enforce monotonicity.
  488. */
  489. prev->stime = max(prev->stime, stime);
  490. prev->utime = max(prev->utime, rtime - prev->stime);
  491. *ut = prev->utime;
  492. *st = prev->stime;
  493. }
  494. void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  495. {
  496. struct task_cputime cputime = {
  497. .sum_exec_runtime = p->se.sum_exec_runtime,
  498. };
  499. task_cputime(p, &cputime.utime, &cputime.stime);
  500. cputime_adjust(&cputime, &p->prev_cputime, ut, st);
  501. }
  502. /*
  503. * Must be called with siglock held.
  504. */
  505. void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  506. {
  507. struct task_cputime cputime;
  508. thread_group_cputime(p, &cputime);
  509. cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st);
  510. }
  511. #endif /* !CONFIG_VIRT_CPU_ACCOUNTING */
  512. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  513. static unsigned long long vtime_delta(struct task_struct *tsk)
  514. {
  515. unsigned long long clock;
  516. clock = sched_clock();
  517. if (clock < tsk->vtime_snap)
  518. return 0;
  519. return clock - tsk->vtime_snap;
  520. }
  521. static cputime_t get_vtime_delta(struct task_struct *tsk)
  522. {
  523. unsigned long long delta = vtime_delta(tsk);
  524. WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_SLEEPING);
  525. tsk->vtime_snap += delta;
  526. /* CHECKME: always safe to convert nsecs to cputime? */
  527. return nsecs_to_cputime(delta);
  528. }
  529. static void __vtime_account_system(struct task_struct *tsk)
  530. {
  531. cputime_t delta_cpu = get_vtime_delta(tsk);
  532. account_system_time(tsk, irq_count(), delta_cpu, cputime_to_scaled(delta_cpu));
  533. }
  534. void vtime_account_system(struct task_struct *tsk)
  535. {
  536. if (!vtime_accounting_enabled())
  537. return;
  538. write_seqlock(&tsk->vtime_seqlock);
  539. __vtime_account_system(tsk);
  540. write_sequnlock(&tsk->vtime_seqlock);
  541. }
  542. void vtime_account_irq_exit(struct task_struct *tsk)
  543. {
  544. if (!vtime_accounting_enabled())
  545. return;
  546. write_seqlock(&tsk->vtime_seqlock);
  547. if (context_tracking_in_user())
  548. tsk->vtime_snap_whence = VTIME_USER;
  549. __vtime_account_system(tsk);
  550. write_sequnlock(&tsk->vtime_seqlock);
  551. }
  552. void vtime_account_user(struct task_struct *tsk)
  553. {
  554. cputime_t delta_cpu;
  555. if (!vtime_accounting_enabled())
  556. return;
  557. delta_cpu = get_vtime_delta(tsk);
  558. write_seqlock(&tsk->vtime_seqlock);
  559. tsk->vtime_snap_whence = VTIME_SYS;
  560. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  561. write_sequnlock(&tsk->vtime_seqlock);
  562. }
  563. void vtime_user_enter(struct task_struct *tsk)
  564. {
  565. if (!vtime_accounting_enabled())
  566. return;
  567. write_seqlock(&tsk->vtime_seqlock);
  568. tsk->vtime_snap_whence = VTIME_USER;
  569. __vtime_account_system(tsk);
  570. write_sequnlock(&tsk->vtime_seqlock);
  571. }
  572. void vtime_guest_enter(struct task_struct *tsk)
  573. {
  574. write_seqlock(&tsk->vtime_seqlock);
  575. __vtime_account_system(tsk);
  576. current->flags |= PF_VCPU;
  577. write_sequnlock(&tsk->vtime_seqlock);
  578. }
  579. void vtime_guest_exit(struct task_struct *tsk)
  580. {
  581. write_seqlock(&tsk->vtime_seqlock);
  582. __vtime_account_system(tsk);
  583. current->flags &= ~PF_VCPU;
  584. write_sequnlock(&tsk->vtime_seqlock);
  585. }
  586. void vtime_account_idle(struct task_struct *tsk)
  587. {
  588. cputime_t delta_cpu = get_vtime_delta(tsk);
  589. account_idle_time(delta_cpu);
  590. }
  591. bool vtime_accounting_enabled(void)
  592. {
  593. return context_tracking_active();
  594. }
  595. void arch_vtime_task_switch(struct task_struct *prev)
  596. {
  597. write_seqlock(&prev->vtime_seqlock);
  598. prev->vtime_snap_whence = VTIME_SLEEPING;
  599. write_sequnlock(&prev->vtime_seqlock);
  600. write_seqlock(&current->vtime_seqlock);
  601. current->vtime_snap_whence = VTIME_SYS;
  602. current->vtime_snap = sched_clock();
  603. write_sequnlock(&current->vtime_seqlock);
  604. }
  605. void vtime_init_idle(struct task_struct *t)
  606. {
  607. unsigned long flags;
  608. write_seqlock_irqsave(&t->vtime_seqlock, flags);
  609. t->vtime_snap_whence = VTIME_SYS;
  610. t->vtime_snap = sched_clock();
  611. write_sequnlock_irqrestore(&t->vtime_seqlock, flags);
  612. }
  613. cputime_t task_gtime(struct task_struct *t)
  614. {
  615. unsigned int seq;
  616. cputime_t gtime;
  617. do {
  618. seq = read_seqbegin(&t->vtime_seqlock);
  619. gtime = t->gtime;
  620. if (t->flags & PF_VCPU)
  621. gtime += vtime_delta(t);
  622. } while (read_seqretry(&t->vtime_seqlock, seq));
  623. return gtime;
  624. }
  625. /*
  626. * Fetch cputime raw values from fields of task_struct and
  627. * add up the pending nohz execution time since the last
  628. * cputime snapshot.
  629. */
  630. static void
  631. fetch_task_cputime(struct task_struct *t,
  632. cputime_t *u_dst, cputime_t *s_dst,
  633. cputime_t *u_src, cputime_t *s_src,
  634. cputime_t *udelta, cputime_t *sdelta)
  635. {
  636. unsigned int seq;
  637. unsigned long long delta;
  638. do {
  639. *udelta = 0;
  640. *sdelta = 0;
  641. seq = read_seqbegin(&t->vtime_seqlock);
  642. if (u_dst)
  643. *u_dst = *u_src;
  644. if (s_dst)
  645. *s_dst = *s_src;
  646. /* Task is sleeping, nothing to add */
  647. if (t->vtime_snap_whence == VTIME_SLEEPING ||
  648. is_idle_task(t))
  649. continue;
  650. delta = vtime_delta(t);
  651. /*
  652. * Task runs either in user or kernel space, add pending nohz time to
  653. * the right place.
  654. */
  655. if (t->vtime_snap_whence == VTIME_USER || t->flags & PF_VCPU) {
  656. *udelta = delta;
  657. } else {
  658. if (t->vtime_snap_whence == VTIME_SYS)
  659. *sdelta = delta;
  660. }
  661. } while (read_seqretry(&t->vtime_seqlock, seq));
  662. }
  663. void task_cputime(struct task_struct *t, cputime_t *utime, cputime_t *stime)
  664. {
  665. cputime_t udelta, sdelta;
  666. fetch_task_cputime(t, utime, stime, &t->utime,
  667. &t->stime, &udelta, &sdelta);
  668. if (utime)
  669. *utime += udelta;
  670. if (stime)
  671. *stime += sdelta;
  672. }
  673. void task_cputime_scaled(struct task_struct *t,
  674. cputime_t *utimescaled, cputime_t *stimescaled)
  675. {
  676. cputime_t udelta, sdelta;
  677. fetch_task_cputime(t, utimescaled, stimescaled,
  678. &t->utimescaled, &t->stimescaled, &udelta, &sdelta);
  679. if (utimescaled)
  680. *utimescaled += cputime_to_scaled(udelta);
  681. if (stimescaled)
  682. *stimescaled += cputime_to_scaled(sdelta);
  683. }
  684. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */