cputime.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright IBM Corp. 2004
  3. *
  4. * Author: Martin Schwidefsky <schwidefsky@de.ibm.com>
  5. */
  6. #ifndef _S390_CPUTIME_H
  7. #define _S390_CPUTIME_H
  8. #include <linux/types.h>
  9. #include <asm/div64.h>
  10. /* We want to use full resolution of the CPU timer: 2**-12 micro-seconds. */
  11. typedef unsigned long long __nocast cputime_t;
  12. typedef unsigned long long __nocast cputime64_t;
  13. #define cmpxchg_cputime(ptr, old, new) cmpxchg64(ptr, old, new)
  14. static inline unsigned long __div(unsigned long long n, unsigned long base)
  15. {
  16. #ifndef CONFIG_64BIT
  17. register_pair rp;
  18. rp.pair = n >> 1;
  19. asm ("dr %0,%1" : "+d" (rp) : "d" (base >> 1));
  20. return rp.subreg.odd;
  21. #else /* CONFIG_64BIT */
  22. return n / base;
  23. #endif /* CONFIG_64BIT */
  24. }
  25. #define cputime_one_jiffy jiffies_to_cputime(1)
  26. /*
  27. * Convert cputime to jiffies and back.
  28. */
  29. static inline unsigned long cputime_to_jiffies(const cputime_t cputime)
  30. {
  31. return __div((__force unsigned long long) cputime, 4096000000ULL / HZ);
  32. }
  33. static inline cputime_t jiffies_to_cputime(const unsigned int jif)
  34. {
  35. return (__force cputime_t)(jif * (4096000000ULL / HZ));
  36. }
  37. static inline u64 cputime64_to_jiffies64(cputime64_t cputime)
  38. {
  39. unsigned long long jif = (__force unsigned long long) cputime;
  40. do_div(jif, 4096000000ULL / HZ);
  41. return jif;
  42. }
  43. static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
  44. {
  45. return (__force cputime64_t)(jif * (4096000000ULL / HZ));
  46. }
  47. /*
  48. * Convert cputime to microseconds and back.
  49. */
  50. static inline unsigned int cputime_to_usecs(const cputime_t cputime)
  51. {
  52. return (__force unsigned long long) cputime >> 12;
  53. }
  54. static inline cputime_t usecs_to_cputime(const unsigned int m)
  55. {
  56. return (__force cputime_t)(m * 4096ULL);
  57. }
  58. #define usecs_to_cputime64(m) usecs_to_cputime(m)
  59. /*
  60. * Convert cputime to milliseconds and back.
  61. */
  62. static inline unsigned int cputime_to_secs(const cputime_t cputime)
  63. {
  64. return __div((__force unsigned long long) cputime, 2048000000) >> 1;
  65. }
  66. static inline cputime_t secs_to_cputime(const unsigned int s)
  67. {
  68. return (__force cputime_t)(s * 4096000000ULL);
  69. }
  70. /*
  71. * Convert cputime to timespec and back.
  72. */
  73. static inline cputime_t timespec_to_cputime(const struct timespec *value)
  74. {
  75. unsigned long long ret = value->tv_sec * 4096000000ULL;
  76. return (__force cputime_t)(ret + value->tv_nsec * 4096 / 1000);
  77. }
  78. static inline void cputime_to_timespec(const cputime_t cputime,
  79. struct timespec *value)
  80. {
  81. unsigned long long __cputime = (__force unsigned long long) cputime;
  82. #ifndef CONFIG_64BIT
  83. register_pair rp;
  84. rp.pair = __cputime >> 1;
  85. asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
  86. value->tv_nsec = rp.subreg.even * 1000 / 4096;
  87. value->tv_sec = rp.subreg.odd;
  88. #else
  89. value->tv_nsec = (__cputime % 4096000000ULL) * 1000 / 4096;
  90. value->tv_sec = __cputime / 4096000000ULL;
  91. #endif
  92. }
  93. /*
  94. * Convert cputime to timeval and back.
  95. * Since cputime and timeval have the same resolution (microseconds)
  96. * this is easy.
  97. */
  98. static inline cputime_t timeval_to_cputime(const struct timeval *value)
  99. {
  100. unsigned long long ret = value->tv_sec * 4096000000ULL;
  101. return (__force cputime_t)(ret + value->tv_usec * 4096ULL);
  102. }
  103. static inline void cputime_to_timeval(const cputime_t cputime,
  104. struct timeval *value)
  105. {
  106. unsigned long long __cputime = (__force unsigned long long) cputime;
  107. #ifndef CONFIG_64BIT
  108. register_pair rp;
  109. rp.pair = __cputime >> 1;
  110. asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
  111. value->tv_usec = rp.subreg.even / 4096;
  112. value->tv_sec = rp.subreg.odd;
  113. #else
  114. value->tv_usec = (__cputime % 4096000000ULL) / 4096;
  115. value->tv_sec = __cputime / 4096000000ULL;
  116. #endif
  117. }
  118. /*
  119. * Convert cputime to clock and back.
  120. */
  121. static inline clock_t cputime_to_clock_t(cputime_t cputime)
  122. {
  123. unsigned long long clock = (__force unsigned long long) cputime;
  124. do_div(clock, 4096000000ULL / USER_HZ);
  125. return clock;
  126. }
  127. static inline cputime_t clock_t_to_cputime(unsigned long x)
  128. {
  129. return (__force cputime_t)(x * (4096000000ULL / USER_HZ));
  130. }
  131. /*
  132. * Convert cputime64 to clock.
  133. */
  134. static inline clock_t cputime64_to_clock_t(cputime64_t cputime)
  135. {
  136. unsigned long long clock = (__force unsigned long long) cputime;
  137. do_div(clock, 4096000000ULL / USER_HZ);
  138. return clock;
  139. }
  140. cputime64_t arch_cpu_idle_time(int cpu);
  141. #define arch_idle_time(cpu) arch_cpu_idle_time(cpu)
  142. #endif /* _S390_CPUTIME_H */