cputime.h 5.4 KB

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