cputime.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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_PARAVIRT
  9. #include <asm/paravirt.h>
  10. #endif
  11. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  12. /*
  13. * There are no locks covering percpu hardirq/softirq time.
  14. * They are only modified in vtime_account, on corresponding CPU
  15. * with interrupts disabled. So, writes are safe.
  16. * They are read and saved off onto struct rq in update_rq_clock().
  17. * This may result in other CPU reading this CPU's irq time and can
  18. * race with irq/vtime_account on this CPU. We would either get old
  19. * or new value with a side effect of accounting a slice of irq time to wrong
  20. * task when irq is in progress while we read rq->clock. That is a worthy
  21. * compromise in place of having locks on each irq in account_system_time.
  22. */
  23. DEFINE_PER_CPU(u64, cpu_hardirq_time);
  24. DEFINE_PER_CPU(u64, cpu_softirq_time);
  25. static DEFINE_PER_CPU(u64, irq_start_time);
  26. static int sched_clock_irqtime;
  27. void enable_sched_clock_irqtime(void)
  28. {
  29. sched_clock_irqtime = 1;
  30. }
  31. void disable_sched_clock_irqtime(void)
  32. {
  33. sched_clock_irqtime = 0;
  34. }
  35. #ifndef CONFIG_64BIT
  36. DEFINE_PER_CPU(seqcount_t, irq_time_seq);
  37. #endif /* CONFIG_64BIT */
  38. /*
  39. * Called before incrementing preempt_count on {soft,}irq_enter
  40. * and before decrementing preempt_count on {soft,}irq_exit.
  41. */
  42. void irqtime_account_irq(struct task_struct *curr)
  43. {
  44. unsigned long flags;
  45. s64 delta;
  46. int cpu;
  47. if (!sched_clock_irqtime)
  48. return;
  49. local_irq_save(flags);
  50. cpu = smp_processor_id();
  51. delta = sched_clock_cpu(cpu) - __this_cpu_read(irq_start_time);
  52. __this_cpu_add(irq_start_time, delta);
  53. irq_time_write_begin();
  54. /*
  55. * We do not account for softirq time from ksoftirqd here.
  56. * We want to continue accounting softirq time to ksoftirqd thread
  57. * in that case, so as not to confuse scheduler with a special task
  58. * that do not consume any time, but still wants to run.
  59. */
  60. if (hardirq_count())
  61. __this_cpu_add(cpu_hardirq_time, delta);
  62. else if (in_serving_softirq() && curr != this_cpu_ksoftirqd())
  63. __this_cpu_add(cpu_softirq_time, delta);
  64. irq_time_write_end();
  65. local_irq_restore(flags);
  66. }
  67. EXPORT_SYMBOL_GPL(irqtime_account_irq);
  68. static int irqtime_account_hi_update(void)
  69. {
  70. u64 *cpustat = kcpustat_this_cpu->cpustat;
  71. unsigned long flags;
  72. u64 latest_ns;
  73. int ret = 0;
  74. local_irq_save(flags);
  75. latest_ns = this_cpu_read(cpu_hardirq_time);
  76. if (nsecs_to_cputime64(latest_ns) > cpustat[CPUTIME_IRQ])
  77. ret = 1;
  78. local_irq_restore(flags);
  79. return ret;
  80. }
  81. static int irqtime_account_si_update(void)
  82. {
  83. u64 *cpustat = kcpustat_this_cpu->cpustat;
  84. unsigned long flags;
  85. u64 latest_ns;
  86. int ret = 0;
  87. local_irq_save(flags);
  88. latest_ns = this_cpu_read(cpu_softirq_time);
  89. if (nsecs_to_cputime64(latest_ns) > cpustat[CPUTIME_SOFTIRQ])
  90. ret = 1;
  91. local_irq_restore(flags);
  92. return ret;
  93. }
  94. #else /* CONFIG_IRQ_TIME_ACCOUNTING */
  95. #define sched_clock_irqtime (0)
  96. #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
  97. static inline void task_group_account_field(struct task_struct *p, int index,
  98. u64 tmp)
  99. {
  100. /*
  101. * Since all updates are sure to touch the root cgroup, we
  102. * get ourselves ahead and touch it first. If the root cgroup
  103. * is the only cgroup, then nothing else should be necessary.
  104. *
  105. */
  106. __this_cpu_add(kernel_cpustat.cpustat[index], tmp);
  107. cpuacct_account_field(p, index, tmp);
  108. }
  109. /*
  110. * Account user cpu time to a process.
  111. * @p: the process that the cpu time gets accounted to
  112. * @cputime: the cpu time spent in user space since the last update
  113. * @cputime_scaled: cputime scaled by cpu frequency
  114. */
  115. void account_user_time(struct task_struct *p, cputime_t cputime,
  116. cputime_t cputime_scaled)
  117. {
  118. int index;
  119. /* Add user time to process. */
  120. p->utime += cputime;
  121. p->utimescaled += cputime_scaled;
  122. account_group_user_time(p, cputime);
  123. index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
  124. /* Add user time to cpustat. */
  125. task_group_account_field(p, index, (__force u64) cputime);
  126. /* Account for user time used */
  127. acct_account_cputime(p);
  128. }
  129. /*
  130. * Account guest cpu time to a process.
  131. * @p: the process that the cpu time gets accounted to
  132. * @cputime: the cpu time spent in virtual machine since the last update
  133. * @cputime_scaled: cputime scaled by cpu frequency
  134. */
  135. static void account_guest_time(struct task_struct *p, cputime_t cputime,
  136. cputime_t cputime_scaled)
  137. {
  138. u64 *cpustat = kcpustat_this_cpu->cpustat;
  139. /* Add guest time to process. */
  140. p->utime += cputime;
  141. p->utimescaled += cputime_scaled;
  142. account_group_user_time(p, cputime);
  143. p->gtime += cputime;
  144. /* Add guest time to cpustat. */
  145. if (task_nice(p) > 0) {
  146. cpustat[CPUTIME_NICE] += (__force u64) cputime;
  147. cpustat[CPUTIME_GUEST_NICE] += (__force u64) cputime;
  148. } else {
  149. cpustat[CPUTIME_USER] += (__force u64) cputime;
  150. cpustat[CPUTIME_GUEST] += (__force u64) cputime;
  151. }
  152. }
  153. /*
  154. * Account system cpu time to a process and desired cpustat field
  155. * @p: the process that the cpu time gets accounted to
  156. * @cputime: the cpu time spent in kernel space since the last update
  157. * @cputime_scaled: cputime scaled by cpu frequency
  158. * @target_cputime64: pointer to cpustat field that has to be updated
  159. */
  160. static inline
  161. void __account_system_time(struct task_struct *p, cputime_t cputime,
  162. cputime_t cputime_scaled, int index)
  163. {
  164. /* Add system time to process. */
  165. p->stime += cputime;
  166. p->stimescaled += cputime_scaled;
  167. account_group_system_time(p, cputime);
  168. /* Add system time to cpustat. */
  169. task_group_account_field(p, index, (__force u64) cputime);
  170. /* Account for system time used */
  171. acct_account_cputime(p);
  172. }
  173. /*
  174. * Account system cpu time to a process.
  175. * @p: the process that the cpu time gets accounted to
  176. * @hardirq_offset: the offset to subtract from hardirq_count()
  177. * @cputime: the cpu time spent in kernel space since the last update
  178. * @cputime_scaled: cputime scaled by cpu frequency
  179. */
  180. void account_system_time(struct task_struct *p, int hardirq_offset,
  181. cputime_t cputime, cputime_t cputime_scaled)
  182. {
  183. int index;
  184. if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
  185. account_guest_time(p, cputime, cputime_scaled);
  186. return;
  187. }
  188. if (hardirq_count() - hardirq_offset)
  189. index = CPUTIME_IRQ;
  190. else if (in_serving_softirq())
  191. index = CPUTIME_SOFTIRQ;
  192. else
  193. index = CPUTIME_SYSTEM;
  194. __account_system_time(p, cputime, cputime_scaled, index);
  195. }
  196. /*
  197. * Account for involuntary wait time.
  198. * @cputime: the cpu time spent in involuntary wait
  199. */
  200. void account_steal_time(cputime_t cputime)
  201. {
  202. u64 *cpustat = kcpustat_this_cpu->cpustat;
  203. cpustat[CPUTIME_STEAL] += (__force u64) cputime;
  204. }
  205. /*
  206. * Account for idle time.
  207. * @cputime: the cpu time spent in idle wait
  208. */
  209. void account_idle_time(cputime_t cputime)
  210. {
  211. u64 *cpustat = kcpustat_this_cpu->cpustat;
  212. struct rq *rq = this_rq();
  213. if (atomic_read(&rq->nr_iowait) > 0)
  214. cpustat[CPUTIME_IOWAIT] += (__force u64) cputime;
  215. else
  216. cpustat[CPUTIME_IDLE] += (__force u64) cputime;
  217. }
  218. static __always_inline bool steal_account_process_tick(void)
  219. {
  220. #ifdef CONFIG_PARAVIRT
  221. if (static_key_false(&paravirt_steal_enabled)) {
  222. u64 steal;
  223. cputime_t steal_ct;
  224. steal = paravirt_steal_clock(smp_processor_id());
  225. steal -= this_rq()->prev_steal_time;
  226. /*
  227. * cputime_t may be less precise than nsecs (eg: if it's
  228. * based on jiffies). Lets cast the result to cputime
  229. * granularity and account the rest on the next rounds.
  230. */
  231. steal_ct = nsecs_to_cputime(steal);
  232. this_rq()->prev_steal_time += cputime_to_nsecs(steal_ct);
  233. account_steal_time(steal_ct);
  234. return steal_ct;
  235. }
  236. #endif
  237. return false;
  238. }
  239. /*
  240. * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
  241. * tasks (sum on group iteration) belonging to @tsk's group.
  242. */
  243. void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
  244. {
  245. struct signal_struct *sig = tsk->signal;
  246. cputime_t utime, stime;
  247. struct task_struct *t;
  248. unsigned int seq, nextseq;
  249. unsigned long flags;
  250. rcu_read_lock();
  251. /* Attempt a lockless read on the first round. */
  252. nextseq = 0;
  253. do {
  254. seq = nextseq;
  255. flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
  256. times->utime = sig->utime;
  257. times->stime = sig->stime;
  258. times->sum_exec_runtime = sig->sum_sched_runtime;
  259. for_each_thread(tsk, t) {
  260. task_cputime(t, &utime, &stime);
  261. times->utime += utime;
  262. times->stime += stime;
  263. times->sum_exec_runtime += task_sched_runtime(t);
  264. }
  265. /* If lockless access failed, take the lock. */
  266. nextseq = 1;
  267. } while (need_seqretry(&sig->stats_lock, seq));
  268. done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
  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, int ticks)
  295. {
  296. cputime_t scaled = cputime_to_scaled(cputime_one_jiffy);
  297. u64 cputime = (__force u64) cputime_one_jiffy;
  298. u64 *cpustat = kcpustat_this_cpu->cpustat;
  299. if (steal_account_process_tick())
  300. return;
  301. cputime *= ticks;
  302. scaled *= ticks;
  303. if (irqtime_account_hi_update()) {
  304. cpustat[CPUTIME_IRQ] += cputime;
  305. } else if (irqtime_account_si_update()) {
  306. cpustat[CPUTIME_SOFTIRQ] += cputime;
  307. } else if (this_cpu_ksoftirqd() == p) {
  308. /*
  309. * ksoftirqd time do not get accounted in cpu_softirq_time.
  310. * So, we have to handle it separately here.
  311. * Also, p->stime needs to be updated for ksoftirqd.
  312. */
  313. __account_system_time(p, cputime, scaled, CPUTIME_SOFTIRQ);
  314. } else if (user_tick) {
  315. account_user_time(p, cputime, scaled);
  316. } else if (p == rq->idle) {
  317. account_idle_time(cputime);
  318. } else if (p->flags & PF_VCPU) { /* System time or guest time */
  319. account_guest_time(p, cputime, scaled);
  320. } else {
  321. __account_system_time(p, cputime, scaled, CPUTIME_SYSTEM);
  322. }
  323. }
  324. static void irqtime_account_idle_ticks(int ticks)
  325. {
  326. struct rq *rq = this_rq();
  327. irqtime_account_process_tick(current, 0, rq, ticks);
  328. }
  329. #else /* CONFIG_IRQ_TIME_ACCOUNTING */
  330. static inline void irqtime_account_idle_ticks(int ticks) {}
  331. static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
  332. struct rq *rq, int nr_ticks) {}
  333. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
  334. /*
  335. * Use precise platform statistics if available:
  336. */
  337. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  338. #ifndef __ARCH_HAS_VTIME_TASK_SWITCH
  339. void vtime_common_task_switch(struct task_struct *prev)
  340. {
  341. if (is_idle_task(prev))
  342. vtime_account_idle(prev);
  343. else
  344. vtime_account_system(prev);
  345. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  346. vtime_account_user(prev);
  347. #endif
  348. arch_vtime_task_switch(prev);
  349. }
  350. #endif
  351. /*
  352. * Archs that account the whole time spent in the idle task
  353. * (outside irq) as idle time can rely on this and just implement
  354. * vtime_account_system() and vtime_account_idle(). Archs that
  355. * have other meaning of the idle time (s390 only includes the
  356. * time spent by the CPU when it's in low power mode) must override
  357. * vtime_account().
  358. */
  359. #ifndef __ARCH_HAS_VTIME_ACCOUNT
  360. void vtime_common_account_irq_enter(struct task_struct *tsk)
  361. {
  362. if (!in_interrupt()) {
  363. /*
  364. * If we interrupted user, context_tracking_in_user()
  365. * is 1 because the context tracking don't hook
  366. * on irq entry/exit. This way we know if
  367. * we need to flush user time on kernel entry.
  368. */
  369. if (context_tracking_in_user()) {
  370. vtime_account_user(tsk);
  371. return;
  372. }
  373. if (is_idle_task(tsk)) {
  374. vtime_account_idle(tsk);
  375. return;
  376. }
  377. }
  378. vtime_account_system(tsk);
  379. }
  380. EXPORT_SYMBOL_GPL(vtime_common_account_irq_enter);
  381. #endif /* __ARCH_HAS_VTIME_ACCOUNT */
  382. #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
  383. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  384. void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  385. {
  386. *ut = p->utime;
  387. *st = p->stime;
  388. }
  389. EXPORT_SYMBOL_GPL(task_cputime_adjusted);
  390. void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  391. {
  392. struct task_cputime cputime;
  393. thread_group_cputime(p, &cputime);
  394. *ut = cputime.utime;
  395. *st = cputime.stime;
  396. }
  397. #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  398. /*
  399. * Account a single tick of cpu time.
  400. * @p: the process that the cpu time gets accounted to
  401. * @user_tick: indicates if the tick is a user or a system tick
  402. */
  403. void account_process_tick(struct task_struct *p, int user_tick)
  404. {
  405. cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
  406. struct rq *rq = this_rq();
  407. if (vtime_accounting_cpu_enabled())
  408. return;
  409. if (sched_clock_irqtime) {
  410. irqtime_account_process_tick(p, user_tick, rq, 1);
  411. return;
  412. }
  413. if (steal_account_process_tick())
  414. return;
  415. if (user_tick)
  416. account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
  417. else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
  418. account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy,
  419. one_jiffy_scaled);
  420. else
  421. account_idle_time(cputime_one_jiffy);
  422. }
  423. /*
  424. * Account multiple ticks of steal time.
  425. * @p: the process from which the cpu time has been stolen
  426. * @ticks: number of stolen ticks
  427. */
  428. void account_steal_ticks(unsigned long ticks)
  429. {
  430. account_steal_time(jiffies_to_cputime(ticks));
  431. }
  432. /*
  433. * Account multiple ticks of idle time.
  434. * @ticks: number of stolen ticks
  435. */
  436. void account_idle_ticks(unsigned long ticks)
  437. {
  438. if (sched_clock_irqtime) {
  439. irqtime_account_idle_ticks(ticks);
  440. return;
  441. }
  442. account_idle_time(jiffies_to_cputime(ticks));
  443. }
  444. /*
  445. * Perform (stime * rtime) / total, but avoid multiplication overflow by
  446. * loosing precision when the numbers are big.
  447. */
  448. static cputime_t scale_stime(u64 stime, u64 rtime, u64 total)
  449. {
  450. u64 scaled;
  451. for (;;) {
  452. /* Make sure "rtime" is the bigger of stime/rtime */
  453. if (stime > rtime)
  454. swap(rtime, stime);
  455. /* Make sure 'total' fits in 32 bits */
  456. if (total >> 32)
  457. goto drop_precision;
  458. /* Does rtime (and thus stime) fit in 32 bits? */
  459. if (!(rtime >> 32))
  460. break;
  461. /* Can we just balance rtime/stime rather than dropping bits? */
  462. if (stime >> 31)
  463. goto drop_precision;
  464. /* We can grow stime and shrink rtime and try to make them both fit */
  465. stime <<= 1;
  466. rtime >>= 1;
  467. continue;
  468. drop_precision:
  469. /* We drop from rtime, it has more bits than stime */
  470. rtime >>= 1;
  471. total >>= 1;
  472. }
  473. /*
  474. * Make sure gcc understands that this is a 32x32->64 multiply,
  475. * followed by a 64/32->64 divide.
  476. */
  477. scaled = div_u64((u64) (u32) stime * (u64) (u32) rtime, (u32)total);
  478. return (__force cputime_t) scaled;
  479. }
  480. /*
  481. * Adjust tick based cputime random precision against scheduler runtime
  482. * accounting.
  483. *
  484. * Tick based cputime accounting depend on random scheduling timeslices of a
  485. * task to be interrupted or not by the timer. Depending on these
  486. * circumstances, the number of these interrupts may be over or
  487. * under-optimistic, matching the real user and system cputime with a variable
  488. * precision.
  489. *
  490. * Fix this by scaling these tick based values against the total runtime
  491. * accounted by the CFS scheduler.
  492. *
  493. * This code provides the following guarantees:
  494. *
  495. * stime + utime == rtime
  496. * stime_i+1 >= stime_i, utime_i+1 >= utime_i
  497. *
  498. * Assuming that rtime_i+1 >= rtime_i.
  499. */
  500. static void cputime_adjust(struct task_cputime *curr,
  501. struct prev_cputime *prev,
  502. cputime_t *ut, cputime_t *st)
  503. {
  504. cputime_t rtime, stime, utime;
  505. unsigned long flags;
  506. /* Serialize concurrent callers such that we can honour our guarantees */
  507. raw_spin_lock_irqsave(&prev->lock, flags);
  508. rtime = nsecs_to_cputime(curr->sum_exec_runtime);
  509. /*
  510. * This is possible under two circumstances:
  511. * - rtime isn't monotonic after all (a bug);
  512. * - we got reordered by the lock.
  513. *
  514. * In both cases this acts as a filter such that the rest of the code
  515. * can assume it is monotonic regardless of anything else.
  516. */
  517. if (prev->stime + prev->utime >= rtime)
  518. goto out;
  519. stime = curr->stime;
  520. utime = curr->utime;
  521. if (utime == 0) {
  522. stime = rtime;
  523. goto update;
  524. }
  525. if (stime == 0) {
  526. utime = rtime;
  527. goto update;
  528. }
  529. stime = scale_stime((__force u64)stime, (__force u64)rtime,
  530. (__force u64)(stime + utime));
  531. /*
  532. * Make sure stime doesn't go backwards; this preserves monotonicity
  533. * for utime because rtime is monotonic.
  534. *
  535. * utime_i+1 = rtime_i+1 - stime_i
  536. * = rtime_i+1 - (rtime_i - utime_i)
  537. * = (rtime_i+1 - rtime_i) + utime_i
  538. * >= utime_i
  539. */
  540. if (stime < prev->stime)
  541. stime = prev->stime;
  542. utime = rtime - stime;
  543. /*
  544. * Make sure utime doesn't go backwards; this still preserves
  545. * monotonicity for stime, analogous argument to above.
  546. */
  547. if (utime < prev->utime) {
  548. utime = prev->utime;
  549. stime = rtime - utime;
  550. }
  551. update:
  552. prev->stime = stime;
  553. prev->utime = utime;
  554. out:
  555. *ut = prev->utime;
  556. *st = prev->stime;
  557. raw_spin_unlock_irqrestore(&prev->lock, flags);
  558. }
  559. void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  560. {
  561. struct task_cputime cputime = {
  562. .sum_exec_runtime = p->se.sum_exec_runtime,
  563. };
  564. task_cputime(p, &cputime.utime, &cputime.stime);
  565. cputime_adjust(&cputime, &p->prev_cputime, ut, st);
  566. }
  567. EXPORT_SYMBOL_GPL(task_cputime_adjusted);
  568. void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  569. {
  570. struct task_cputime cputime;
  571. thread_group_cputime(p, &cputime);
  572. cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st);
  573. }
  574. #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  575. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  576. static unsigned long long vtime_delta(struct task_struct *tsk)
  577. {
  578. unsigned long long clock;
  579. clock = local_clock();
  580. if (clock < tsk->vtime_snap)
  581. return 0;
  582. return clock - tsk->vtime_snap;
  583. }
  584. static cputime_t get_vtime_delta(struct task_struct *tsk)
  585. {
  586. unsigned long long delta = vtime_delta(tsk);
  587. WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE);
  588. tsk->vtime_snap += delta;
  589. /* CHECKME: always safe to convert nsecs to cputime? */
  590. return nsecs_to_cputime(delta);
  591. }
  592. static void __vtime_account_system(struct task_struct *tsk)
  593. {
  594. cputime_t delta_cpu = get_vtime_delta(tsk);
  595. account_system_time(tsk, irq_count(), delta_cpu, cputime_to_scaled(delta_cpu));
  596. }
  597. void vtime_account_system(struct task_struct *tsk)
  598. {
  599. write_seqcount_begin(&tsk->vtime_seqcount);
  600. __vtime_account_system(tsk);
  601. write_seqcount_end(&tsk->vtime_seqcount);
  602. }
  603. void vtime_gen_account_irq_exit(struct task_struct *tsk)
  604. {
  605. write_seqcount_begin(&tsk->vtime_seqcount);
  606. __vtime_account_system(tsk);
  607. if (context_tracking_in_user())
  608. tsk->vtime_snap_whence = VTIME_USER;
  609. write_seqcount_end(&tsk->vtime_seqcount);
  610. }
  611. void vtime_account_user(struct task_struct *tsk)
  612. {
  613. cputime_t delta_cpu;
  614. write_seqcount_begin(&tsk->vtime_seqcount);
  615. delta_cpu = get_vtime_delta(tsk);
  616. tsk->vtime_snap_whence = VTIME_SYS;
  617. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  618. write_seqcount_end(&tsk->vtime_seqcount);
  619. }
  620. void vtime_user_enter(struct task_struct *tsk)
  621. {
  622. write_seqcount_begin(&tsk->vtime_seqcount);
  623. __vtime_account_system(tsk);
  624. tsk->vtime_snap_whence = VTIME_USER;
  625. write_seqcount_end(&tsk->vtime_seqcount);
  626. }
  627. void vtime_guest_enter(struct task_struct *tsk)
  628. {
  629. /*
  630. * The flags must be updated under the lock with
  631. * the vtime_snap flush and update.
  632. * That enforces a right ordering and update sequence
  633. * synchronization against the reader (task_gtime())
  634. * that can thus safely catch up with a tickless delta.
  635. */
  636. write_seqcount_begin(&tsk->vtime_seqcount);
  637. __vtime_account_system(tsk);
  638. current->flags |= PF_VCPU;
  639. write_seqcount_end(&tsk->vtime_seqcount);
  640. }
  641. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  642. void vtime_guest_exit(struct task_struct *tsk)
  643. {
  644. write_seqcount_begin(&tsk->vtime_seqcount);
  645. __vtime_account_system(tsk);
  646. current->flags &= ~PF_VCPU;
  647. write_seqcount_end(&tsk->vtime_seqcount);
  648. }
  649. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  650. void vtime_account_idle(struct task_struct *tsk)
  651. {
  652. cputime_t delta_cpu = get_vtime_delta(tsk);
  653. account_idle_time(delta_cpu);
  654. }
  655. void arch_vtime_task_switch(struct task_struct *prev)
  656. {
  657. write_seqcount_begin(&prev->vtime_seqcount);
  658. prev->vtime_snap_whence = VTIME_INACTIVE;
  659. write_seqcount_end(&prev->vtime_seqcount);
  660. write_seqcount_begin(&current->vtime_seqcount);
  661. current->vtime_snap_whence = VTIME_SYS;
  662. current->vtime_snap = sched_clock_cpu(smp_processor_id());
  663. write_seqcount_end(&current->vtime_seqcount);
  664. }
  665. void vtime_init_idle(struct task_struct *t, int cpu)
  666. {
  667. unsigned long flags;
  668. local_irq_save(flags);
  669. write_seqcount_begin(&t->vtime_seqcount);
  670. t->vtime_snap_whence = VTIME_SYS;
  671. t->vtime_snap = sched_clock_cpu(cpu);
  672. write_seqcount_end(&t->vtime_seqcount);
  673. local_irq_restore(flags);
  674. }
  675. cputime_t task_gtime(struct task_struct *t)
  676. {
  677. unsigned int seq;
  678. cputime_t gtime;
  679. if (!vtime_accounting_enabled())
  680. return t->gtime;
  681. do {
  682. seq = read_seqcount_begin(&t->vtime_seqcount);
  683. gtime = t->gtime;
  684. if (t->vtime_snap_whence == VTIME_SYS && t->flags & PF_VCPU)
  685. gtime += vtime_delta(t);
  686. } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  687. return gtime;
  688. }
  689. /*
  690. * Fetch cputime raw values from fields of task_struct and
  691. * add up the pending nohz execution time since the last
  692. * cputime snapshot.
  693. */
  694. static void
  695. fetch_task_cputime(struct task_struct *t,
  696. cputime_t *u_dst, cputime_t *s_dst,
  697. cputime_t *u_src, cputime_t *s_src,
  698. cputime_t *udelta, cputime_t *sdelta)
  699. {
  700. unsigned int seq;
  701. unsigned long long delta;
  702. do {
  703. *udelta = 0;
  704. *sdelta = 0;
  705. seq = read_seqcount_begin(&t->vtime_seqcount);
  706. if (u_dst)
  707. *u_dst = *u_src;
  708. if (s_dst)
  709. *s_dst = *s_src;
  710. /* Task is sleeping, nothing to add */
  711. if (t->vtime_snap_whence == VTIME_INACTIVE ||
  712. is_idle_task(t))
  713. continue;
  714. delta = vtime_delta(t);
  715. /*
  716. * Task runs either in user or kernel space, add pending nohz time to
  717. * the right place.
  718. */
  719. if (t->vtime_snap_whence == VTIME_USER || t->flags & PF_VCPU) {
  720. *udelta = delta;
  721. } else {
  722. if (t->vtime_snap_whence == VTIME_SYS)
  723. *sdelta = delta;
  724. }
  725. } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  726. }
  727. void task_cputime(struct task_struct *t, cputime_t *utime, cputime_t *stime)
  728. {
  729. cputime_t udelta, sdelta;
  730. if (!vtime_accounting_enabled()) {
  731. if (utime)
  732. *utime = t->utime;
  733. if (stime)
  734. *stime = t->stime;
  735. return;
  736. }
  737. fetch_task_cputime(t, utime, stime, &t->utime,
  738. &t->stime, &udelta, &sdelta);
  739. if (utime)
  740. *utime += udelta;
  741. if (stime)
  742. *stime += sdelta;
  743. }
  744. void task_cputime_scaled(struct task_struct *t,
  745. cputime_t *utimescaled, cputime_t *stimescaled)
  746. {
  747. cputime_t udelta, sdelta;
  748. if (!vtime_accounting_enabled()) {
  749. if (utimescaled)
  750. *utimescaled = t->utimescaled;
  751. if (stimescaled)
  752. *stimescaled = t->stimescaled;
  753. return;
  754. }
  755. fetch_task_cputime(t, utimescaled, stimescaled,
  756. &t->utimescaled, &t->stimescaled, &udelta, &sdelta);
  757. if (utimescaled)
  758. *utimescaled += cputime_to_scaled(udelta);
  759. if (stimescaled)
  760. *stimescaled += cputime_to_scaled(sdelta);
  761. }
  762. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */