cputime.h 4.5 KB

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