cputime.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. typedef u64 __nocast cputime_t;
  29. typedef u64 __nocast cputime64_t;
  30. #define cmpxchg_cputime(ptr, old, new) cmpxchg(ptr, old, new)
  31. #ifdef __KERNEL__
  32. /*
  33. * One jiffy in timebase units computed during initialization
  34. */
  35. extern cputime_t cputime_one_jiffy;
  36. /*
  37. * Convert cputime <-> jiffies
  38. */
  39. extern u64 __cputime_jiffies_factor;
  40. DECLARE_PER_CPU(unsigned long, cputime_last_delta);
  41. DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta);
  42. static inline unsigned long cputime_to_jiffies(const cputime_t ct)
  43. {
  44. return mulhdu((__force u64) ct, __cputime_jiffies_factor);
  45. }
  46. /* Estimate the scaled cputime by scaling the real cputime based on
  47. * the last scaled to real ratio */
  48. static inline cputime_t cputime_to_scaled(const cputime_t ct)
  49. {
  50. if (cpu_has_feature(CPU_FTR_SPURR) &&
  51. __this_cpu_read(cputime_last_delta))
  52. return (__force u64) ct *
  53. __this_cpu_read(cputime_scaled_last_delta) /
  54. __this_cpu_read(cputime_last_delta);
  55. return ct;
  56. }
  57. static inline cputime_t jiffies_to_cputime(const unsigned long jif)
  58. {
  59. u64 ct;
  60. unsigned long sec;
  61. /* have to be a little careful about overflow */
  62. ct = jif % HZ;
  63. sec = jif / HZ;
  64. if (ct) {
  65. ct *= tb_ticks_per_sec;
  66. do_div(ct, HZ);
  67. }
  68. if (sec)
  69. ct += (cputime_t) sec * tb_ticks_per_sec;
  70. return (__force cputime_t) ct;
  71. }
  72. static inline void setup_cputime_one_jiffy(void)
  73. {
  74. cputime_one_jiffy = jiffies_to_cputime(1);
  75. }
  76. static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
  77. {
  78. u64 ct;
  79. u64 sec;
  80. /* have to be a little careful about overflow */
  81. ct = jif % HZ;
  82. sec = jif / 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. static inline void arch_vtime_task_switch(struct task_struct *tsk) { }
  196. #endif /* __KERNEL__ */
  197. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  198. #endif /* __POWERPC_CPUTIME_H */