cputime.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * include/asm-s390/cputime.h
  3. *
  4. * (C) Copyright IBM Corp. 2004
  5. *
  6. * Author: Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. */
  8. #ifndef _S390_CPUTIME_H
  9. #define _S390_CPUTIME_H
  10. #include <linux/types.h>
  11. #include <linux/percpu.h>
  12. #include <linux/spinlock.h>
  13. #include <asm/div64.h>
  14. /* We want to use full resolution of the CPU timer: 2**-12 micro-seconds. */
  15. typedef unsigned long long cputime_t;
  16. typedef unsigned long long cputime64_t;
  17. #ifndef __s390x__
  18. static inline unsigned int
  19. __div(unsigned long long n, unsigned int base)
  20. {
  21. register_pair rp;
  22. rp.pair = n >> 1;
  23. asm ("dr %0,%1" : "+d" (rp) : "d" (base >> 1));
  24. return rp.subreg.odd;
  25. }
  26. #else /* __s390x__ */
  27. static inline unsigned int
  28. __div(unsigned long long n, unsigned int base)
  29. {
  30. return n / base;
  31. }
  32. #endif /* __s390x__ */
  33. #define cputime_zero (0ULL)
  34. #define cputime_max ((~0UL >> 1) - 1)
  35. #define cputime_add(__a, __b) ((__a) + (__b))
  36. #define cputime_sub(__a, __b) ((__a) - (__b))
  37. #define cputime_div(__a, __n) ({ \
  38. unsigned long long __div = (__a); \
  39. do_div(__div,__n); \
  40. __div; \
  41. })
  42. #define cputime_halve(__a) ((__a) >> 1)
  43. #define cputime_eq(__a, __b) ((__a) == (__b))
  44. #define cputime_gt(__a, __b) ((__a) > (__b))
  45. #define cputime_ge(__a, __b) ((__a) >= (__b))
  46. #define cputime_lt(__a, __b) ((__a) < (__b))
  47. #define cputime_le(__a, __b) ((__a) <= (__b))
  48. #define cputime_to_jiffies(__ct) (__div((__ct), 4096000000ULL / HZ))
  49. #define cputime_to_scaled(__ct) (__ct)
  50. #define jiffies_to_cputime(__hz) ((cputime_t)(__hz) * (4096000000ULL / HZ))
  51. #define cputime64_zero (0ULL)
  52. #define cputime64_add(__a, __b) ((__a) + (__b))
  53. #define cputime_to_cputime64(__ct) (__ct)
  54. static inline u64
  55. cputime64_to_jiffies64(cputime64_t cputime)
  56. {
  57. do_div(cputime, 4096000000ULL / HZ);
  58. return cputime;
  59. }
  60. /*
  61. * Convert cputime to milliseconds and back.
  62. */
  63. static inline unsigned int
  64. cputime_to_msecs(const cputime_t cputime)
  65. {
  66. return __div(cputime, 4096000);
  67. }
  68. static inline cputime_t
  69. msecs_to_cputime(const unsigned int m)
  70. {
  71. return (cputime_t) m * 4096000;
  72. }
  73. /*
  74. * Convert cputime to milliseconds and back.
  75. */
  76. static inline unsigned int
  77. cputime_to_secs(const cputime_t cputime)
  78. {
  79. return __div(cputime, 2048000000) >> 1;
  80. }
  81. static inline cputime_t
  82. secs_to_cputime(const unsigned int s)
  83. {
  84. return (cputime_t) s * 4096000000ULL;
  85. }
  86. /*
  87. * Convert cputime to timespec and back.
  88. */
  89. static inline cputime_t
  90. timespec_to_cputime(const struct timespec *value)
  91. {
  92. return value->tv_nsec * 4096 / 1000 + (u64) value->tv_sec * 4096000000ULL;
  93. }
  94. static inline void
  95. cputime_to_timespec(const cputime_t cputime, struct timespec *value)
  96. {
  97. #ifndef __s390x__
  98. register_pair rp;
  99. rp.pair = cputime >> 1;
  100. asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
  101. value->tv_nsec = rp.subreg.even * 1000 / 4096;
  102. value->tv_sec = rp.subreg.odd;
  103. #else
  104. value->tv_nsec = (cputime % 4096000000ULL) * 1000 / 4096;
  105. value->tv_sec = cputime / 4096000000ULL;
  106. #endif
  107. }
  108. /*
  109. * Convert cputime to timeval and back.
  110. * Since cputime and timeval have the same resolution (microseconds)
  111. * this is easy.
  112. */
  113. static inline cputime_t
  114. timeval_to_cputime(const struct timeval *value)
  115. {
  116. return value->tv_usec * 4096 + (u64) value->tv_sec * 4096000000ULL;
  117. }
  118. static inline void
  119. cputime_to_timeval(const cputime_t cputime, struct timeval *value)
  120. {
  121. #ifndef __s390x__
  122. register_pair rp;
  123. rp.pair = cputime >> 1;
  124. asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
  125. value->tv_usec = rp.subreg.even / 4096;
  126. value->tv_sec = rp.subreg.odd;
  127. #else
  128. value->tv_usec = (cputime % 4096000000ULL) / 4096;
  129. value->tv_sec = cputime / 4096000000ULL;
  130. #endif
  131. }
  132. /*
  133. * Convert cputime to clock and back.
  134. */
  135. static inline clock_t
  136. cputime_to_clock_t(cputime_t cputime)
  137. {
  138. return __div(cputime, 4096000000ULL / USER_HZ);
  139. }
  140. static inline cputime_t
  141. clock_t_to_cputime(unsigned long x)
  142. {
  143. return (cputime_t) x * (4096000000ULL / USER_HZ);
  144. }
  145. /*
  146. * Convert cputime64 to clock.
  147. */
  148. static inline clock_t
  149. cputime64_to_clock_t(cputime64_t cputime)
  150. {
  151. return __div(cputime, 4096000000ULL / USER_HZ);
  152. }
  153. struct s390_idle_data {
  154. unsigned int sequence;
  155. unsigned long long idle_count;
  156. unsigned long long idle_enter;
  157. unsigned long long idle_time;
  158. };
  159. DECLARE_PER_CPU(struct s390_idle_data, s390_idle);
  160. void vtime_start_cpu(void);
  161. cputime64_t s390_get_idle_time(int cpu);
  162. #define arch_idle_time(cpu) s390_get_idle_time(cpu)
  163. static inline void s390_idle_check(void)
  164. {
  165. if ((&__get_cpu_var(s390_idle))->idle_enter != 0ULL)
  166. vtime_start_cpu();
  167. }
  168. #endif /* _S390_CPUTIME_H */