cputime.h 4.0 KB

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