cputime.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. /*
  98. * Since all updates are sure to touch the root cgroup, we
  99. * get ourselves ahead and touch it first. If the root cgroup
  100. * is the only cgroup, then nothing else should be necessary.
  101. *
  102. */
  103. __this_cpu_add(kernel_cpustat.cpustat[index], tmp);
  104. cpuacct_account_field(p, index, tmp);
  105. }
  106. /*
  107. * Account user cpu time to a process.
  108. * @p: the process that the cpu time gets accounted to
  109. * @cputime: the cpu time spent in user space since the last update
  110. * @cputime_scaled: cputime scaled by cpu frequency
  111. */
  112. void account_user_time(struct task_struct *p, cputime_t cputime,
  113. cputime_t cputime_scaled)
  114. {
  115. int index;
  116. /* Add user time to process. */
  117. p->utime += cputime;
  118. p->utimescaled += cputime_scaled;
  119. account_group_user_time(p, cputime);
  120. index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
  121. /* Add user time to cpustat. */
  122. task_group_account_field(p, index, (__force u64) cputime);
  123. /* Account for user time used */
  124. acct_account_cputime(p);
  125. }
  126. /*
  127. * Account guest cpu time to a process.
  128. * @p: the process that the cpu time gets accounted to
  129. * @cputime: the cpu time spent in virtual machine since the last update
  130. * @cputime_scaled: cputime scaled by cpu frequency
  131. */
  132. static void account_guest_time(struct task_struct *p, cputime_t cputime,
  133. cputime_t cputime_scaled)
  134. {
  135. u64 *cpustat = kcpustat_this_cpu->cpustat;
  136. /* Add guest time to process. */
  137. p->utime += cputime;
  138. p->utimescaled += cputime_scaled;
  139. account_group_user_time(p, cputime);
  140. p->gtime += cputime;
  141. /* Add guest time to cpustat. */
  142. if (task_nice(p) > 0) {
  143. cpustat[CPUTIME_NICE] += (__force u64) cputime;
  144. cpustat[CPUTIME_GUEST_NICE] += (__force u64) cputime;
  145. } else {
  146. cpustat[CPUTIME_USER] += (__force u64) cputime;
  147. cpustat[CPUTIME_GUEST] += (__force u64) cputime;
  148. }
  149. }
  150. /*
  151. * Account system cpu time to a process and desired cpustat field
  152. * @p: the process that the cpu time gets accounted to
  153. * @cputime: the cpu time spent in kernel space since the last update
  154. * @cputime_scaled: cputime scaled by cpu frequency
  155. * @target_cputime64: pointer to cpustat field that has to be updated
  156. */
  157. static inline
  158. void __account_system_time(struct task_struct *p, cputime_t cputime,
  159. cputime_t cputime_scaled, int index)
  160. {
  161. /* Add system time to process. */
  162. p->stime += cputime;
  163. p->stimescaled += cputime_scaled;
  164. account_group_system_time(p, cputime);
  165. /* Add system time to cpustat. */
  166. task_group_account_field(p, index, (__force u64) cputime);
  167. /* Account for system time used */
  168. acct_account_cputime(p);
  169. }
  170. /*
  171. * Account system cpu time to a process.
  172. * @p: the process that the cpu time gets accounted to
  173. * @hardirq_offset: the offset to subtract from hardirq_count()
  174. * @cputime: the cpu time spent in kernel space since the last update
  175. * @cputime_scaled: cputime scaled by cpu frequency
  176. */
  177. void account_system_time(struct task_struct *p, int hardirq_offset,
  178. cputime_t cputime, cputime_t cputime_scaled)
  179. {
  180. int index;
  181. if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
  182. account_guest_time(p, cputime, cputime_scaled);
  183. return;
  184. }
  185. if (hardirq_count() - hardirq_offset)
  186. index = CPUTIME_IRQ;
  187. else if (in_serving_softirq())
  188. index = CPUTIME_SOFTIRQ;
  189. else
  190. index = CPUTIME_SYSTEM;
  191. __account_system_time(p, cputime, cputime_scaled, index);
  192. }
  193. /*
  194. * Account for involuntary wait time.
  195. * @cputime: the cpu time spent in involuntary wait
  196. */
  197. void account_steal_time(cputime_t cputime)
  198. {
  199. u64 *cpustat = kcpustat_this_cpu->cpustat;
  200. cpustat[CPUTIME_STEAL] += (__force u64) cputime;
  201. }
  202. /*
  203. * Account for idle time.
  204. * @cputime: the cpu time spent in idle wait
  205. */
  206. void account_idle_time(cputime_t cputime)
  207. {
  208. u64 *cpustat = kcpustat_this_cpu->cpustat;
  209. struct rq *rq = this_rq();
  210. if (atomic_read(&rq->nr_iowait) > 0)
  211. cpustat[CPUTIME_IOWAIT] += (__force u64) cputime;
  212. else
  213. cpustat[CPUTIME_IDLE] += (__force u64) cputime;
  214. }
  215. static __always_inline bool steal_account_process_tick(void)
  216. {
  217. #ifdef CONFIG_PARAVIRT
  218. if (static_key_false(&paravirt_steal_enabled)) {
  219. u64 steal;
  220. unsigned long steal_jiffies;
  221. steal = paravirt_steal_clock(smp_processor_id());
  222. steal -= this_rq()->prev_steal_time;
  223. /*
  224. * steal is in nsecs but our caller is expecting steal
  225. * time in jiffies. Lets cast the result to jiffies
  226. * granularity and account the rest on the next rounds.
  227. */
  228. steal_jiffies = nsecs_to_jiffies(steal);
  229. this_rq()->prev_steal_time += jiffies_to_nsecs(steal_jiffies);
  230. account_steal_time(jiffies_to_cputime(steal_jiffies));
  231. return steal_jiffies;
  232. }
  233. #endif
  234. return false;
  235. }
  236. /*
  237. * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
  238. * tasks (sum on group iteration) belonging to @tsk's group.
  239. */
  240. void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
  241. {
  242. struct signal_struct *sig = tsk->signal;
  243. cputime_t utime, stime;
  244. struct task_struct *t;
  245. unsigned int seq, nextseq;
  246. unsigned long flags;
  247. rcu_read_lock();
  248. /* Attempt a lockless read on the first round. */
  249. nextseq = 0;
  250. do {
  251. seq = nextseq;
  252. flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
  253. times->utime = sig->utime;
  254. times->stime = sig->stime;
  255. times->sum_exec_runtime = sig->sum_sched_runtime;
  256. for_each_thread(tsk, t) {
  257. task_cputime(t, &utime, &stime);
  258. times->utime += utime;
  259. times->stime += stime;
  260. times->sum_exec_runtime += task_sched_runtime(t);
  261. }
  262. /* If lockless access failed, take the lock. */
  263. nextseq = 1;
  264. } while (need_seqretry(&sig->stats_lock, seq));
  265. done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
  266. rcu_read_unlock();
  267. }
  268. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  269. /*
  270. * Account a tick to a process and cpustat
  271. * @p: the process that the cpu time gets accounted to
  272. * @user_tick: is the tick from userspace
  273. * @rq: the pointer to rq
  274. *
  275. * Tick demultiplexing follows the order
  276. * - pending hardirq update
  277. * - pending softirq update
  278. * - user_time
  279. * - idle_time
  280. * - system time
  281. * - check for guest_time
  282. * - else account as system_time
  283. *
  284. * Check for hardirq is done both for system and user time as there is
  285. * no timer going off while we are on hardirq and hence we may never get an
  286. * opportunity to update it solely in system time.
  287. * p->stime and friends are only updated on system time and not on irq
  288. * softirq as those do not count in task exec_runtime any more.
  289. */
  290. static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
  291. struct rq *rq, int ticks)
  292. {
  293. cputime_t scaled = cputime_to_scaled(cputime_one_jiffy);
  294. u64 cputime = (__force u64) cputime_one_jiffy;
  295. u64 *cpustat = kcpustat_this_cpu->cpustat;
  296. if (steal_account_process_tick())
  297. return;
  298. cputime *= ticks;
  299. scaled *= ticks;
  300. if (irqtime_account_hi_update()) {
  301. cpustat[CPUTIME_IRQ] += cputime;
  302. } else if (irqtime_account_si_update()) {
  303. cpustat[CPUTIME_SOFTIRQ] += cputime;
  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, scaled, CPUTIME_SOFTIRQ);
  311. } else if (user_tick) {
  312. account_user_time(p, cputime, scaled);
  313. } else if (p == rq->idle) {
  314. account_idle_time(cputime);
  315. } else if (p->flags & PF_VCPU) { /* System time or guest time */
  316. account_guest_time(p, cputime, scaled);
  317. } else {
  318. __account_system_time(p, cputime, scaled, CPUTIME_SYSTEM);
  319. }
  320. }
  321. static void irqtime_account_idle_ticks(int ticks)
  322. {
  323. struct rq *rq = this_rq();
  324. irqtime_account_process_tick(current, 0, rq, ticks);
  325. }
  326. #else /* CONFIG_IRQ_TIME_ACCOUNTING */
  327. static inline void irqtime_account_idle_ticks(int ticks) {}
  328. static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
  329. struct rq *rq, int nr_ticks) {}
  330. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
  331. /*
  332. * Use precise platform statistics if available:
  333. */
  334. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  335. #ifndef __ARCH_HAS_VTIME_TASK_SWITCH
  336. void vtime_common_task_switch(struct task_struct *prev)
  337. {
  338. if (is_idle_task(prev))
  339. vtime_account_idle(prev);
  340. else
  341. vtime_account_system(prev);
  342. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  343. vtime_account_user(prev);
  344. #endif
  345. arch_vtime_task_switch(prev);
  346. }
  347. #endif
  348. /*
  349. * Archs that account the whole time spent in the idle task
  350. * (outside irq) as idle time can rely on this and just implement
  351. * vtime_account_system() and vtime_account_idle(). Archs that
  352. * have other meaning of the idle time (s390 only includes the
  353. * time spent by the CPU when it's in low power mode) must override
  354. * vtime_account().
  355. */
  356. #ifndef __ARCH_HAS_VTIME_ACCOUNT
  357. void vtime_common_account_irq_enter(struct task_struct *tsk)
  358. {
  359. if (!in_interrupt()) {
  360. /*
  361. * If we interrupted user, context_tracking_in_user()
  362. * is 1 because the context tracking don't hook
  363. * on irq entry/exit. This way we know if
  364. * we need to flush user time on kernel entry.
  365. */
  366. if (context_tracking_in_user()) {
  367. vtime_account_user(tsk);
  368. return;
  369. }
  370. if (is_idle_task(tsk)) {
  371. vtime_account_idle(tsk);
  372. return;
  373. }
  374. }
  375. vtime_account_system(tsk);
  376. }
  377. EXPORT_SYMBOL_GPL(vtime_common_account_irq_enter);
  378. #endif /* __ARCH_HAS_VTIME_ACCOUNT */
  379. #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
  380. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  381. void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  382. {
  383. *ut = p->utime;
  384. *st = p->stime;
  385. }
  386. EXPORT_SYMBOL_GPL(task_cputime_adjusted);
  387. void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  388. {
  389. struct task_cputime cputime;
  390. thread_group_cputime(p, &cputime);
  391. *ut = cputime.utime;
  392. *st = cputime.stime;
  393. }
  394. #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  395. /*
  396. * Account a single tick of cpu time.
  397. * @p: the process that the cpu time gets accounted to
  398. * @user_tick: indicates if the tick is a user or a system tick
  399. */
  400. void account_process_tick(struct task_struct *p, int user_tick)
  401. {
  402. cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
  403. struct rq *rq = this_rq();
  404. if (vtime_accounting_enabled())
  405. return;
  406. if (sched_clock_irqtime) {
  407. irqtime_account_process_tick(p, user_tick, rq, 1);
  408. return;
  409. }
  410. if (steal_account_process_tick())
  411. return;
  412. if (user_tick)
  413. account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
  414. else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
  415. account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy,
  416. one_jiffy_scaled);
  417. else
  418. account_idle_time(cputime_one_jiffy);
  419. }
  420. /*
  421. * Account multiple ticks of steal time.
  422. * @p: the process from which the cpu time has been stolen
  423. * @ticks: number of stolen ticks
  424. */
  425. void account_steal_ticks(unsigned long ticks)
  426. {
  427. account_steal_time(jiffies_to_cputime(ticks));
  428. }
  429. /*
  430. * Account multiple ticks of idle time.
  431. * @ticks: number of stolen ticks
  432. */
  433. void account_idle_ticks(unsigned long ticks)
  434. {
  435. if (sched_clock_irqtime) {
  436. irqtime_account_idle_ticks(ticks);
  437. return;
  438. }
  439. account_idle_time(jiffies_to_cputime(ticks));
  440. }
  441. /*
  442. * Perform (stime * rtime) / total, but avoid multiplication overflow by
  443. * loosing precision when the numbers are big.
  444. */
  445. static cputime_t scale_stime(u64 stime, u64 rtime, u64 total)
  446. {
  447. u64 scaled;
  448. for (;;) {
  449. /* Make sure "rtime" is the bigger of stime/rtime */
  450. if (stime > rtime)
  451. swap(rtime, stime);
  452. /* Make sure 'total' fits in 32 bits */
  453. if (total >> 32)
  454. goto drop_precision;
  455. /* Does rtime (and thus stime) fit in 32 bits? */
  456. if (!(rtime >> 32))
  457. break;
  458. /* Can we just balance rtime/stime rather than dropping bits? */
  459. if (stime >> 31)
  460. goto drop_precision;
  461. /* We can grow stime and shrink rtime and try to make them both fit */
  462. stime <<= 1;
  463. rtime >>= 1;
  464. continue;
  465. drop_precision:
  466. /* We drop from rtime, it has more bits than stime */
  467. rtime >>= 1;
  468. total >>= 1;
  469. }
  470. /*
  471. * Make sure gcc understands that this is a 32x32->64 multiply,
  472. * followed by a 64/32->64 divide.
  473. */
  474. scaled = div_u64((u64) (u32) stime * (u64) (u32) rtime, (u32)total);
  475. return (__force cputime_t) scaled;
  476. }
  477. /*
  478. * Adjust tick based cputime random precision against scheduler runtime
  479. * accounting.
  480. *
  481. * Tick based cputime accounting depend on random scheduling timeslices of a
  482. * task to be interrupted or not by the timer. Depending on these
  483. * circumstances, the number of these interrupts may be over or
  484. * under-optimistic, matching the real user and system cputime with a variable
  485. * precision.
  486. *
  487. * Fix this by scaling these tick based values against the total runtime
  488. * accounted by the CFS scheduler.
  489. *
  490. * This code provides the following guarantees:
  491. *
  492. * stime + utime == rtime
  493. * stime_i+1 >= stime_i, utime_i+1 >= utime_i
  494. *
  495. * Assuming that rtime_i+1 >= rtime_i.
  496. */
  497. static void cputime_adjust(struct task_cputime *curr,
  498. struct prev_cputime *prev,
  499. cputime_t *ut, cputime_t *st)
  500. {
  501. cputime_t rtime, stime, utime;
  502. unsigned long flags;
  503. /* Serialize concurrent callers such that we can honour our guarantees */
  504. raw_spin_lock_irqsave(&prev->lock, flags);
  505. rtime = nsecs_to_cputime(curr->sum_exec_runtime);
  506. /*
  507. * This is possible under two circumstances:
  508. * - rtime isn't monotonic after all (a bug);
  509. * - we got reordered by the lock.
  510. *
  511. * In both cases this acts as a filter such that the rest of the code
  512. * can assume it is monotonic regardless of anything else.
  513. */
  514. if (prev->stime + prev->utime >= rtime)
  515. goto out;
  516. stime = curr->stime;
  517. utime = curr->utime;
  518. /*
  519. * If either stime or both stime and utime are 0, assume all runtime is
  520. * userspace. Once a task gets some ticks, the monotonicy code at
  521. * 'update' will ensure things converge to the observed ratio.
  522. */
  523. if (stime == 0) {
  524. utime = rtime;
  525. goto update;
  526. }
  527. if (utime == 0) {
  528. stime = rtime;
  529. goto update;
  530. }
  531. stime = scale_stime((__force u64)stime, (__force u64)rtime,
  532. (__force u64)(stime + utime));
  533. update:
  534. /*
  535. * Make sure stime doesn't go backwards; this preserves monotonicity
  536. * for utime because rtime is monotonic.
  537. *
  538. * utime_i+1 = rtime_i+1 - stime_i
  539. * = rtime_i+1 - (rtime_i - utime_i)
  540. * = (rtime_i+1 - rtime_i) + utime_i
  541. * >= utime_i
  542. */
  543. if (stime < prev->stime)
  544. stime = prev->stime;
  545. utime = rtime - stime;
  546. /*
  547. * Make sure utime doesn't go backwards; this still preserves
  548. * monotonicity for stime, analogous argument to above.
  549. */
  550. if (utime < prev->utime) {
  551. utime = prev->utime;
  552. stime = rtime - utime;
  553. }
  554. prev->stime = stime;
  555. prev->utime = utime;
  556. out:
  557. *ut = prev->utime;
  558. *st = prev->stime;
  559. raw_spin_unlock_irqrestore(&prev->lock, flags);
  560. }
  561. void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  562. {
  563. struct task_cputime cputime = {
  564. .sum_exec_runtime = p->se.sum_exec_runtime,
  565. };
  566. task_cputime(p, &cputime.utime, &cputime.stime);
  567. cputime_adjust(&cputime, &p->prev_cputime, ut, st);
  568. }
  569. EXPORT_SYMBOL_GPL(task_cputime_adjusted);
  570. void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
  571. {
  572. struct task_cputime cputime;
  573. thread_group_cputime(p, &cputime);
  574. cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st);
  575. }
  576. #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  577. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  578. static unsigned long long vtime_delta(struct task_struct *tsk)
  579. {
  580. unsigned long long clock;
  581. clock = local_clock();
  582. if (clock < tsk->vtime_snap)
  583. return 0;
  584. return clock - tsk->vtime_snap;
  585. }
  586. static cputime_t get_vtime_delta(struct task_struct *tsk)
  587. {
  588. unsigned long long delta = vtime_delta(tsk);
  589. WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_SLEEPING);
  590. tsk->vtime_snap += delta;
  591. /* CHECKME: always safe to convert nsecs to cputime? */
  592. return nsecs_to_cputime(delta);
  593. }
  594. static void __vtime_account_system(struct task_struct *tsk)
  595. {
  596. cputime_t delta_cpu = get_vtime_delta(tsk);
  597. account_system_time(tsk, irq_count(), delta_cpu, cputime_to_scaled(delta_cpu));
  598. }
  599. void vtime_account_system(struct task_struct *tsk)
  600. {
  601. write_seqlock(&tsk->vtime_seqlock);
  602. __vtime_account_system(tsk);
  603. write_sequnlock(&tsk->vtime_seqlock);
  604. }
  605. void vtime_gen_account_irq_exit(struct task_struct *tsk)
  606. {
  607. write_seqlock(&tsk->vtime_seqlock);
  608. __vtime_account_system(tsk);
  609. if (context_tracking_in_user())
  610. tsk->vtime_snap_whence = VTIME_USER;
  611. write_sequnlock(&tsk->vtime_seqlock);
  612. }
  613. void vtime_account_user(struct task_struct *tsk)
  614. {
  615. cputime_t delta_cpu;
  616. write_seqlock(&tsk->vtime_seqlock);
  617. delta_cpu = get_vtime_delta(tsk);
  618. tsk->vtime_snap_whence = VTIME_SYS;
  619. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  620. write_sequnlock(&tsk->vtime_seqlock);
  621. }
  622. void vtime_user_enter(struct task_struct *tsk)
  623. {
  624. write_seqlock(&tsk->vtime_seqlock);
  625. __vtime_account_system(tsk);
  626. tsk->vtime_snap_whence = VTIME_USER;
  627. write_sequnlock(&tsk->vtime_seqlock);
  628. }
  629. void vtime_guest_enter(struct task_struct *tsk)
  630. {
  631. /*
  632. * The flags must be updated under the lock with
  633. * the vtime_snap flush and update.
  634. * That enforces a right ordering and update sequence
  635. * synchronization against the reader (task_gtime())
  636. * that can thus safely catch up with a tickless delta.
  637. */
  638. write_seqlock(&tsk->vtime_seqlock);
  639. __vtime_account_system(tsk);
  640. current->flags |= PF_VCPU;
  641. write_sequnlock(&tsk->vtime_seqlock);
  642. }
  643. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  644. void vtime_guest_exit(struct task_struct *tsk)
  645. {
  646. write_seqlock(&tsk->vtime_seqlock);
  647. __vtime_account_system(tsk);
  648. current->flags &= ~PF_VCPU;
  649. write_sequnlock(&tsk->vtime_seqlock);
  650. }
  651. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  652. void vtime_account_idle(struct task_struct *tsk)
  653. {
  654. cputime_t delta_cpu = get_vtime_delta(tsk);
  655. account_idle_time(delta_cpu);
  656. }
  657. void arch_vtime_task_switch(struct task_struct *prev)
  658. {
  659. write_seqlock(&prev->vtime_seqlock);
  660. prev->vtime_snap_whence = VTIME_SLEEPING;
  661. write_sequnlock(&prev->vtime_seqlock);
  662. write_seqlock(&current->vtime_seqlock);
  663. current->vtime_snap_whence = VTIME_SYS;
  664. current->vtime_snap = sched_clock_cpu(smp_processor_id());
  665. write_sequnlock(&current->vtime_seqlock);
  666. }
  667. void vtime_init_idle(struct task_struct *t, int cpu)
  668. {
  669. unsigned long flags;
  670. write_seqlock_irqsave(&t->vtime_seqlock, flags);
  671. t->vtime_snap_whence = VTIME_SYS;
  672. t->vtime_snap = sched_clock_cpu(cpu);
  673. write_sequnlock_irqrestore(&t->vtime_seqlock, flags);
  674. }
  675. cputime_t task_gtime(struct task_struct *t)
  676. {
  677. unsigned int seq;
  678. cputime_t gtime;
  679. if (!context_tracking_is_enabled())
  680. return t->gtime;
  681. do {
  682. seq = read_seqbegin(&t->vtime_seqlock);
  683. gtime = t->gtime;
  684. if (t->flags & PF_VCPU)
  685. gtime += vtime_delta(t);
  686. } while (read_seqretry(&t->vtime_seqlock, 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_seqbegin(&t->vtime_seqlock);
  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_SLEEPING ||
  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_seqretry(&t->vtime_seqlock, seq));
  726. }
  727. void task_cputime(struct task_struct *t, cputime_t *utime, cputime_t *stime)
  728. {
  729. cputime_t udelta, sdelta;
  730. fetch_task_cputime(t, utime, stime, &t->utime,
  731. &t->stime, &udelta, &sdelta);
  732. if (utime)
  733. *utime += udelta;
  734. if (stime)
  735. *stime += sdelta;
  736. }
  737. void task_cputime_scaled(struct task_struct *t,
  738. cputime_t *utimescaled, cputime_t *stimescaled)
  739. {
  740. cputime_t udelta, sdelta;
  741. fetch_task_cputime(t, utimescaled, stimescaled,
  742. &t->utimescaled, &t->stimescaled, &udelta, &sdelta);
  743. if (utimescaled)
  744. *utimescaled += cputime_to_scaled(udelta);
  745. if (stimescaled)
  746. *stimescaled += cputime_to_scaled(sdelta);
  747. }
  748. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */