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