cputime.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Definitions for measuring cputime on powerpc machines.
  3. *
  4. * Copyright (C) 2006 Paul Mackerras, IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * If we have CONFIG_VIRT_CPU_ACCOUNTING_NATIVE, we measure cpu time in
  12. * the same units as the timebase. Otherwise we measure cpu time
  13. * in jiffies using the generic definitions.
  14. */
  15. #ifndef __POWERPC_CPUTIME_H
  16. #define __POWERPC_CPUTIME_H
  17. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  18. #include <asm-generic/cputime.h>
  19. #ifdef __KERNEL__
  20. static inline void setup_cputime_one_jiffy(void) { }
  21. #endif
  22. #else
  23. #include <linux/types.h>
  24. #include <linux/time.h>
  25. #include <asm/div64.h>
  26. #include <asm/time.h>
  27. #include <asm/param.h>
  28. #include <asm/cpu_has_feature.h>
  29. typedef u64 __nocast cputime_t;
  30. typedef u64 __nocast cputime64_t;
  31. #define cmpxchg_cputime(ptr, old, new) cmpxchg(ptr, old, new)
  32. #ifdef __KERNEL__
  33. /*
  34. * One jiffy in timebase units computed during initialization
  35. */
  36. extern cputime_t cputime_one_jiffy;
  37. /*
  38. * Convert cputime <-> jiffies
  39. */
  40. extern u64 __cputime_jiffies_factor;
  41. DECLARE_PER_CPU(unsigned long, cputime_last_delta);
  42. DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta);
  43. static inline unsigned long cputime_to_jiffies(const cputime_t ct)
  44. {
  45. return mulhdu((__force u64) ct, __cputime_jiffies_factor);
  46. }
  47. /* Estimate the scaled cputime by scaling the real cputime based on
  48. * the last scaled to real ratio */
  49. static inline cputime_t cputime_to_scaled(const cputime_t ct)
  50. {
  51. if (cpu_has_feature(CPU_FTR_SPURR) &&
  52. __this_cpu_read(cputime_last_delta))
  53. return (__force u64) ct *
  54. __this_cpu_read(cputime_scaled_last_delta) /
  55. __this_cpu_read(cputime_last_delta);
  56. return ct;
  57. }
  58. static inline cputime_t jiffies_to_cputime(const unsigned long jif)
  59. {
  60. u64 ct;
  61. unsigned long sec;
  62. /* have to be a little careful about overflow */
  63. ct = jif % HZ;
  64. sec = jif / HZ;
  65. if (ct) {
  66. ct *= tb_ticks_per_sec;
  67. do_div(ct, HZ);
  68. }
  69. if (sec)
  70. ct += (cputime_t) sec * tb_ticks_per_sec;
  71. return (__force cputime_t) ct;
  72. }
  73. static inline void setup_cputime_one_jiffy(void)
  74. {
  75. cputime_one_jiffy = jiffies_to_cputime(1);
  76. }
  77. static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
  78. {
  79. u64 ct;
  80. u64 sec = jif;
  81. /* have to be a little careful about overflow */
  82. ct = do_div(sec, HZ);
  83. if (ct) {
  84. ct *= tb_ticks_per_sec;
  85. do_div(ct, HZ);
  86. }
  87. if (sec)
  88. ct += (u64) sec * tb_ticks_per_sec;
  89. return (__force cputime64_t) ct;
  90. }
  91. static inline u64 cputime64_to_jiffies64(const cputime_t ct)
  92. {
  93. return mulhdu((__force u64) ct, __cputime_jiffies_factor);
  94. }
  95. /*
  96. * Convert cputime <-> microseconds
  97. */
  98. extern u64 __cputime_usec_factor;
  99. static inline unsigned long cputime_to_usecs(const cputime_t ct)
  100. {
  101. return mulhdu((__force u64) ct, __cputime_usec_factor);
  102. }
  103. static inline cputime_t usecs_to_cputime(const unsigned long us)
  104. {
  105. u64 ct;
  106. unsigned long sec;
  107. /* have to be a little careful about overflow */
  108. ct = us % 1000000;
  109. sec = us / 1000000;
  110. if (ct) {
  111. ct *= tb_ticks_per_sec;
  112. do_div(ct, 1000000);
  113. }
  114. if (sec)
  115. ct += (cputime_t) sec * tb_ticks_per_sec;
  116. return (__force cputime_t) ct;
  117. }
  118. #define usecs_to_cputime64(us) usecs_to_cputime(us)
  119. /*
  120. * Convert cputime <-> seconds
  121. */
  122. extern u64 __cputime_sec_factor;
  123. static inline unsigned long cputime_to_secs(const cputime_t ct)
  124. {
  125. return mulhdu((__force u64) ct, __cputime_sec_factor);
  126. }
  127. static inline cputime_t secs_to_cputime(const unsigned long sec)
  128. {
  129. return (__force cputime_t)((u64) sec * tb_ticks_per_sec);
  130. }
  131. /*
  132. * Convert cputime <-> timespec
  133. */
  134. static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p)
  135. {
  136. u64 x = (__force u64) ct;
  137. unsigned int frac;
  138. frac = do_div(x, tb_ticks_per_sec);
  139. p->tv_sec = x;
  140. x = (u64) frac * 1000000000;
  141. do_div(x, tb_ticks_per_sec);
  142. p->tv_nsec = x;
  143. }
  144. static inline cputime_t timespec_to_cputime(const struct timespec *p)
  145. {
  146. u64 ct;
  147. ct = (u64) p->tv_nsec * tb_ticks_per_sec;
  148. do_div(ct, 1000000000);
  149. return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec);
  150. }
  151. /*
  152. * Convert cputime <-> timeval
  153. */
  154. static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p)
  155. {
  156. u64 x = (__force u64) ct;
  157. unsigned int frac;
  158. frac = do_div(x, tb_ticks_per_sec);
  159. p->tv_sec = x;
  160. x = (u64) frac * 1000000;
  161. do_div(x, tb_ticks_per_sec);
  162. p->tv_usec = x;
  163. }
  164. static inline cputime_t timeval_to_cputime(const struct timeval *p)
  165. {
  166. u64 ct;
  167. ct = (u64) p->tv_usec * tb_ticks_per_sec;
  168. do_div(ct, 1000000);
  169. return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec);
  170. }
  171. /*
  172. * Convert cputime <-> clock_t (units of 1/USER_HZ seconds)
  173. */
  174. extern u64 __cputime_clockt_factor;
  175. static inline unsigned long cputime_to_clock_t(const cputime_t ct)
  176. {
  177. return mulhdu((__force u64) ct, __cputime_clockt_factor);
  178. }
  179. static inline cputime_t clock_t_to_cputime(const unsigned long clk)
  180. {
  181. u64 ct;
  182. unsigned long sec;
  183. /* have to be a little careful about overflow */
  184. ct = clk % USER_HZ;
  185. sec = clk / USER_HZ;
  186. if (ct) {
  187. ct *= tb_ticks_per_sec;
  188. do_div(ct, USER_HZ);
  189. }
  190. if (sec)
  191. ct += (u64) sec * tb_ticks_per_sec;
  192. return (__force cputime_t) ct;
  193. }
  194. #define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct))
  195. /*
  196. * PPC64 uses PACA which is task independent for storing accounting data while
  197. * PPC32 uses struct thread_info, therefore at task switch the accounting data
  198. * has to be populated in the new task
  199. */
  200. #ifdef CONFIG_PPC64
  201. static inline void arch_vtime_task_switch(struct task_struct *tsk) { }
  202. #else
  203. void arch_vtime_task_switch(struct task_struct *tsk);
  204. #endif
  205. #endif /* __KERNEL__ */
  206. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  207. #endif /* __POWERPC_CPUTIME_H */