cputime.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 micro-second resolution. */
  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), 1000000 / HZ))
  46. #define jiffies_to_cputime(__hz) ((cputime_t)(__hz) * (1000000 / HZ))
  47. #define cputime64_zero (0ULL)
  48. #define cputime64_add(__a, __b) ((__a) + (__b))
  49. #define cputime_to_cputime64(__ct) (__ct)
  50. static inline u64
  51. cputime64_to_jiffies64(cputime64_t cputime)
  52. {
  53. do_div(cputime, 1000000 / HZ);
  54. return cputime;
  55. }
  56. /*
  57. * Convert cputime to milliseconds and back.
  58. */
  59. static inline unsigned int
  60. cputime_to_msecs(const cputime_t cputime)
  61. {
  62. return __div(cputime, 1000);
  63. }
  64. static inline cputime_t
  65. msecs_to_cputime(const unsigned int m)
  66. {
  67. return (cputime_t) m * 1000;
  68. }
  69. /*
  70. * Convert cputime to milliseconds and back.
  71. */
  72. static inline unsigned int
  73. cputime_to_secs(const cputime_t cputime)
  74. {
  75. return __div(cputime, 1000000);
  76. }
  77. static inline cputime_t
  78. secs_to_cputime(const unsigned int s)
  79. {
  80. return (cputime_t) s * 1000000;
  81. }
  82. /*
  83. * Convert cputime to timespec and back.
  84. */
  85. static inline cputime_t
  86. timespec_to_cputime(const struct timespec *value)
  87. {
  88. return value->tv_nsec / 1000 + (u64) value->tv_sec * 1000000;
  89. }
  90. static inline void
  91. cputime_to_timespec(const cputime_t cputime, struct timespec *value)
  92. {
  93. #ifndef __s390x__
  94. register_pair rp;
  95. rp.pair = cputime >> 1;
  96. asm ("dr %0,%1" : "+d" (rp) : "d" (1000000 >> 1));
  97. value->tv_nsec = rp.subreg.even * 1000;
  98. value->tv_sec = rp.subreg.odd;
  99. #else
  100. value->tv_nsec = (cputime % 1000000) * 1000;
  101. value->tv_sec = cputime / 1000000;
  102. #endif
  103. }
  104. /*
  105. * Convert cputime to timeval and back.
  106. * Since cputime and timeval have the same resolution (microseconds)
  107. * this is easy.
  108. */
  109. static inline cputime_t
  110. timeval_to_cputime(const struct timeval *value)
  111. {
  112. return value->tv_usec + (u64) value->tv_sec * 1000000;
  113. }
  114. static inline void
  115. cputime_to_timeval(const cputime_t cputime, struct timeval *value)
  116. {
  117. #ifndef __s390x__
  118. register_pair rp;
  119. rp.pair = cputime >> 1;
  120. asm ("dr %0,%1" : "+d" (rp) : "d" (1000000 >> 1));
  121. value->tv_usec = rp.subreg.even;
  122. value->tv_sec = rp.subreg.odd;
  123. #else
  124. value->tv_usec = cputime % 1000000;
  125. value->tv_sec = cputime / 1000000;
  126. #endif
  127. }
  128. /*
  129. * Convert cputime to clock and back.
  130. */
  131. static inline clock_t
  132. cputime_to_clock_t(cputime_t cputime)
  133. {
  134. return __div(cputime, 1000000 / USER_HZ);
  135. }
  136. static inline cputime_t
  137. clock_t_to_cputime(unsigned long x)
  138. {
  139. return (cputime_t) x * (1000000 / USER_HZ);
  140. }
  141. /*
  142. * Convert cputime64 to clock.
  143. */
  144. static inline clock_t
  145. cputime64_to_clock_t(cputime64_t cputime)
  146. {
  147. return __div(cputime, 1000000 / USER_HZ);
  148. }
  149. #endif /* _S390_CPUTIME_H */