cputime.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. per_cpu(cputime_last_delta, smp_processor_id()))
  66. return ct *
  67. per_cpu(cputime_scaled_last_delta, smp_processor_id())/
  68. per_cpu(cputime_last_delta, smp_processor_id());
  69. return ct;
  70. }
  71. static inline cputime_t jiffies_to_cputime(const unsigned long jif)
  72. {
  73. cputime_t ct;
  74. unsigned long sec;
  75. /* have to be a little careful about overflow */
  76. ct = jif % HZ;
  77. sec = jif / HZ;
  78. if (ct) {
  79. ct *= tb_ticks_per_sec;
  80. do_div(ct, HZ);
  81. }
  82. if (sec)
  83. ct += (cputime_t) sec * tb_ticks_per_sec;
  84. return ct;
  85. }
  86. static inline void setup_cputime_one_jiffy(void)
  87. {
  88. cputime_one_jiffy = jiffies_to_cputime(1);
  89. }
  90. static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
  91. {
  92. cputime_t ct;
  93. u64 sec;
  94. /* have to be a little careful about overflow */
  95. ct = jif % HZ;
  96. sec = jif / HZ;
  97. if (ct) {
  98. ct *= tb_ticks_per_sec;
  99. do_div(ct, HZ);
  100. }
  101. if (sec)
  102. ct += (cputime_t) sec * tb_ticks_per_sec;
  103. return ct;
  104. }
  105. static inline u64 cputime64_to_jiffies64(const cputime_t ct)
  106. {
  107. return mulhdu(ct, __cputime_jiffies_factor);
  108. }
  109. /*
  110. * Convert cputime <-> milliseconds
  111. */
  112. extern u64 __cputime_msec_factor;
  113. static inline unsigned long cputime_to_msecs(const cputime_t ct)
  114. {
  115. return mulhdu(ct, __cputime_msec_factor);
  116. }
  117. static inline cputime_t msecs_to_cputime(const unsigned long ms)
  118. {
  119. cputime_t ct;
  120. unsigned long sec;
  121. /* have to be a little careful about overflow */
  122. ct = ms % 1000;
  123. sec = ms / 1000;
  124. if (ct) {
  125. ct *= tb_ticks_per_sec;
  126. do_div(ct, 1000);
  127. }
  128. if (sec)
  129. ct += (cputime_t) sec * tb_ticks_per_sec;
  130. return ct;
  131. }
  132. /*
  133. * Convert cputime <-> seconds
  134. */
  135. extern u64 __cputime_sec_factor;
  136. static inline unsigned long cputime_to_secs(const cputime_t ct)
  137. {
  138. return mulhdu(ct, __cputime_sec_factor);
  139. }
  140. static inline cputime_t secs_to_cputime(const unsigned long sec)
  141. {
  142. return (cputime_t) sec * tb_ticks_per_sec;
  143. }
  144. /*
  145. * Convert cputime <-> timespec
  146. */
  147. static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p)
  148. {
  149. u64 x = ct;
  150. unsigned int frac;
  151. frac = do_div(x, tb_ticks_per_sec);
  152. p->tv_sec = x;
  153. x = (u64) frac * 1000000000;
  154. do_div(x, tb_ticks_per_sec);
  155. p->tv_nsec = x;
  156. }
  157. static inline cputime_t timespec_to_cputime(const struct timespec *p)
  158. {
  159. cputime_t ct;
  160. ct = (u64) p->tv_nsec * tb_ticks_per_sec;
  161. do_div(ct, 1000000000);
  162. return ct + (u64) p->tv_sec * tb_ticks_per_sec;
  163. }
  164. /*
  165. * Convert cputime <-> timeval
  166. */
  167. static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p)
  168. {
  169. u64 x = ct;
  170. unsigned int frac;
  171. frac = do_div(x, tb_ticks_per_sec);
  172. p->tv_sec = x;
  173. x = (u64) frac * 1000000;
  174. do_div(x, tb_ticks_per_sec);
  175. p->tv_usec = x;
  176. }
  177. static inline cputime_t timeval_to_cputime(const struct timeval *p)
  178. {
  179. cputime_t ct;
  180. ct = (u64) p->tv_usec * tb_ticks_per_sec;
  181. do_div(ct, 1000000);
  182. return ct + (u64) p->tv_sec * tb_ticks_per_sec;
  183. }
  184. /*
  185. * Convert cputime <-> clock_t (units of 1/USER_HZ seconds)
  186. */
  187. extern u64 __cputime_clockt_factor;
  188. static inline unsigned long cputime_to_clock_t(const cputime_t ct)
  189. {
  190. return mulhdu(ct, __cputime_clockt_factor);
  191. }
  192. static inline cputime_t clock_t_to_cputime(const unsigned long clk)
  193. {
  194. cputime_t ct;
  195. unsigned long sec;
  196. /* have to be a little careful about overflow */
  197. ct = clk % USER_HZ;
  198. sec = clk / USER_HZ;
  199. if (ct) {
  200. ct *= tb_ticks_per_sec;
  201. do_div(ct, USER_HZ);
  202. }
  203. if (sec)
  204. ct += (cputime_t) sec * tb_ticks_per_sec;
  205. return ct;
  206. }
  207. #define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct))
  208. #endif /* __KERNEL__ */
  209. #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
  210. #endif /* __POWERPC_CPUTIME_H */