cputime.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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, 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
  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. #ifdef __KERNEL__
  31. /*
  32. * One jiffy in timebase units computed during initialization
  33. */
  34. extern cputime_t cputime_one_jiffy;
  35. /*
  36. * Convert cputime <-> jiffies
  37. */
  38. extern u64 __cputime_jiffies_factor;
  39. DECLARE_PER_CPU(unsigned long, cputime_last_delta);
  40. DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta);
  41. static inline unsigned long cputime_to_jiffies(const cputime_t ct)
  42. {
  43. return mulhdu((__force u64) ct, __cputime_jiffies_factor);
  44. }
  45. /* Estimate the scaled cputime by scaling the real cputime based on
  46. * the last scaled to real ratio */
  47. static inline cputime_t cputime_to_scaled(const cputime_t ct)
  48. {
  49. if (cpu_has_feature(CPU_FTR_SPURR) &&
  50. __get_cpu_var(cputime_last_delta))
  51. return (__force u64) ct *
  52. __get_cpu_var(cputime_scaled_last_delta) /
  53. __get_cpu_var(cputime_last_delta);
  54. return ct;
  55. }
  56. static inline cputime_t jiffies_to_cputime(const unsigned long jif)
  57. {
  58. u64 ct;
  59. unsigned long sec;
  60. /* have to be a little careful about overflow */
  61. ct = jif % HZ;
  62. sec = jif / HZ;
  63. if (ct) {
  64. ct *= tb_ticks_per_sec;
  65. do_div(ct, HZ);
  66. }
  67. if (sec)
  68. ct += (cputime_t) sec * tb_ticks_per_sec;
  69. return (__force cputime_t) ct;
  70. }
  71. static inline void setup_cputime_one_jiffy(void)
  72. {
  73. cputime_one_jiffy = jiffies_to_cputime(1);
  74. }
  75. static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
  76. {
  77. u64 ct;
  78. u64 sec;
  79. /* have to be a little careful about overflow */
  80. ct = jif % HZ;
  81. sec = jif / HZ;
  82. if (ct) {
  83. ct *= tb_ticks_per_sec;
  84. do_div(ct, HZ);
  85. }
  86. if (sec)
  87. ct += (u64) sec * tb_ticks_per_sec;
  88. return (__force cputime64_t) ct;
  89. }
  90. static inline u64 cputime64_to_jiffies64(const cputime_t ct)
  91. {
  92. return mulhdu((__force u64) ct, __cputime_jiffies_factor);
  93. }
  94. /*
  95. * Convert cputime <-> microseconds
  96. */
  97. extern u64 __cputime_usec_factor;
  98. static inline unsigned long cputime_to_usecs(const cputime_t ct)
  99. {
  100. return mulhdu((__force u64) ct, __cputime_usec_factor);
  101. }
  102. static inline cputime_t usecs_to_cputime(const unsigned long us)
  103. {
  104. u64 ct;
  105. unsigned long sec;
  106. /* have to be a little careful about overflow */
  107. ct = us % 1000000;
  108. sec = us / 1000000;
  109. if (ct) {
  110. ct *= tb_ticks_per_sec;
  111. do_div(ct, 1000000);
  112. }
  113. if (sec)
  114. ct += (cputime_t) sec * tb_ticks_per_sec;
  115. return (__force cputime_t) ct;
  116. }
  117. #define usecs_to_cputime64(us) usecs_to_cputime(us)
  118. /*
  119. * Convert cputime <-> seconds
  120. */
  121. extern u64 __cputime_sec_factor;
  122. static inline unsigned long cputime_to_secs(const cputime_t ct)
  123. {
  124. return mulhdu((__force u64) ct, __cputime_sec_factor);
  125. }
  126. static inline cputime_t secs_to_cputime(const unsigned long sec)
  127. {
  128. return (__force cputime_t)((u64) sec * tb_ticks_per_sec);
  129. }
  130. /*
  131. * Convert cputime <-> timespec
  132. */
  133. static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p)
  134. {
  135. u64 x = (__force u64) ct;
  136. unsigned int frac;
  137. frac = do_div(x, tb_ticks_per_sec);
  138. p->tv_sec = x;
  139. x = (u64) frac * 1000000000;
  140. do_div(x, tb_ticks_per_sec);
  141. p->tv_nsec = x;
  142. }
  143. static inline cputime_t timespec_to_cputime(const struct timespec *p)
  144. {
  145. u64 ct;
  146. ct = (u64) p->tv_nsec * tb_ticks_per_sec;
  147. do_div(ct, 1000000000);
  148. return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec);
  149. }
  150. /*
  151. * Convert cputime <-> timeval
  152. */
  153. static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p)
  154. {
  155. u64 x = (__force u64) ct;
  156. unsigned int frac;
  157. frac = do_div(x, tb_ticks_per_sec);
  158. p->tv_sec = x;
  159. x = (u64) frac * 1000000;
  160. do_div(x, tb_ticks_per_sec);
  161. p->tv_usec = x;
  162. }
  163. static inline cputime_t timeval_to_cputime(const struct timeval *p)
  164. {
  165. u64 ct;
  166. ct = (u64) p->tv_usec * tb_ticks_per_sec;
  167. do_div(ct, 1000000);
  168. return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec);
  169. }
  170. /*
  171. * Convert cputime <-> clock_t (units of 1/USER_HZ seconds)
  172. */
  173. extern u64 __cputime_clockt_factor;
  174. static inline unsigned long cputime_to_clock_t(const cputime_t ct)
  175. {
  176. return mulhdu((__force u64) ct, __cputime_clockt_factor);
  177. }
  178. static inline cputime_t clock_t_to_cputime(const unsigned long clk)
  179. {
  180. u64 ct;
  181. unsigned long sec;
  182. /* have to be a little careful about overflow */
  183. ct = clk % USER_HZ;
  184. sec = clk / USER_HZ;
  185. if (ct) {
  186. ct *= tb_ticks_per_sec;
  187. do_div(ct, USER_HZ);
  188. }
  189. if (sec)
  190. ct += (u64) sec * tb_ticks_per_sec;
  191. return (__force cputime_t) ct;
  192. }
  193. #define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct))
  194. #endif /* __KERNEL__ */
  195. #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
  196. #endif /* __POWERPC_CPUTIME_H */