cputime.h 4.9 KB

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