cputime.c 22 KB

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