cputime.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <linux/percpu.h>
  10. #include <linux/spinlock.h>
  11. #include <asm/div64.h>
  12. /* We want to use full resolution of the CPU timer: 2**-12 micro-seconds. */
  13. typedef unsigned long long __nocast cputime_t;
  14. typedef unsigned long long __nocast cputime64_t;
  15. static inline unsigned long __div(unsigned long long n, unsigned long base)
  16. {
  17. #ifndef CONFIG_64BIT
  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. #else /* CONFIG_64BIT */
  23. return n / base;
  24. #endif /* CONFIG_64BIT */
  25. }
  26. #define cputime_one_jiffy jiffies_to_cputime(1)
  27. /*
  28. * Convert cputime to jiffies and back.
  29. */
  30. static inline unsigned long cputime_to_jiffies(const cputime_t cputime)
  31. {
  32. return __div((__force unsigned long long) cputime, 4096000000ULL / HZ);
  33. }
  34. static inline cputime_t jiffies_to_cputime(const unsigned int jif)
  35. {
  36. return (__force cputime_t)(jif * (4096000000ULL / HZ));
  37. }
  38. static inline u64 cputime64_to_jiffies64(cputime64_t cputime)
  39. {
  40. unsigned long long jif = (__force unsigned long long) cputime;
  41. do_div(jif, 4096000000ULL / HZ);
  42. return jif;
  43. }
  44. static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
  45. {
  46. return (__force cputime64_t)(jif * (4096000000ULL / HZ));
  47. }
  48. /*
  49. * Convert cputime to microseconds and back.
  50. */
  51. static inline unsigned int cputime_to_usecs(const cputime_t cputime)
  52. {
  53. return (__force unsigned long long) cputime >> 12;
  54. }
  55. static inline cputime_t usecs_to_cputime(const unsigned int m)
  56. {
  57. return (__force cputime_t)(m * 4096ULL);
  58. }
  59. #define usecs_to_cputime64(m) usecs_to_cputime(m)
  60. /*
  61. * Convert cputime to milliseconds and back.
  62. */
  63. static inline unsigned int cputime_to_secs(const cputime_t cputime)
  64. {
  65. return __div((__force unsigned long long) cputime, 2048000000) >> 1;
  66. }
  67. static inline cputime_t secs_to_cputime(const unsigned int s)
  68. {
  69. return (__force cputime_t)(s * 4096000000ULL);
  70. }
  71. /*
  72. * Convert cputime to timespec and back.
  73. */
  74. static inline cputime_t timespec_to_cputime(const struct timespec *value)
  75. {
  76. unsigned long long ret = value->tv_sec * 4096000000ULL;
  77. return (__force cputime_t)(ret + value->tv_nsec * 4096 / 1000);
  78. }
  79. static inline void cputime_to_timespec(const cputime_t cputime,
  80. struct timespec *value)
  81. {
  82. unsigned long long __cputime = (__force unsigned long long) cputime;
  83. #ifndef CONFIG_64BIT
  84. register_pair rp;
  85. rp.pair = __cputime >> 1;
  86. asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
  87. value->tv_nsec = rp.subreg.even * 1000 / 4096;
  88. value->tv_sec = rp.subreg.odd;
  89. #else
  90. value->tv_nsec = (__cputime % 4096000000ULL) * 1000 / 4096;
  91. value->tv_sec = __cputime / 4096000000ULL;
  92. #endif
  93. }
  94. /*
  95. * Convert cputime to timeval and back.
  96. * Since cputime and timeval have the same resolution (microseconds)
  97. * this is easy.
  98. */
  99. static inline cputime_t timeval_to_cputime(const struct timeval *value)
  100. {
  101. unsigned long long ret = value->tv_sec * 4096000000ULL;
  102. return (__force cputime_t)(ret + value->tv_usec * 4096ULL);
  103. }
  104. static inline void cputime_to_timeval(const cputime_t cputime,
  105. struct timeval *value)
  106. {
  107. unsigned long long __cputime = (__force unsigned long long) cputime;
  108. #ifndef CONFIG_64BIT
  109. register_pair rp;
  110. rp.pair = __cputime >> 1;
  111. asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
  112. value->tv_usec = rp.subreg.even / 4096;
  113. value->tv_sec = rp.subreg.odd;
  114. #else
  115. value->tv_usec = (__cputime % 4096000000ULL) / 4096;
  116. value->tv_sec = __cputime / 4096000000ULL;
  117. #endif
  118. }
  119. /*
  120. * Convert cputime to clock and back.
  121. */
  122. static inline clock_t cputime_to_clock_t(cputime_t cputime)
  123. {
  124. unsigned long long clock = (__force unsigned long long) cputime;
  125. do_div(clock, 4096000000ULL / USER_HZ);
  126. return clock;
  127. }
  128. static inline cputime_t clock_t_to_cputime(unsigned long x)
  129. {
  130. return (__force cputime_t)(x * (4096000000ULL / USER_HZ));
  131. }
  132. /*
  133. * Convert cputime64 to clock.
  134. */
  135. static inline clock_t cputime64_to_clock_t(cputime64_t cputime)
  136. {
  137. unsigned long long clock = (__force unsigned long long) cputime;
  138. do_div(clock, 4096000000ULL / USER_HZ);
  139. return clock;
  140. }
  141. struct s390_idle_data {
  142. int nohz_delay;
  143. unsigned int sequence;
  144. unsigned long long idle_count;
  145. unsigned long long idle_time;
  146. unsigned long long clock_idle_enter;
  147. unsigned long long clock_idle_exit;
  148. unsigned long long timer_idle_enter;
  149. unsigned long long timer_idle_exit;
  150. };
  151. DECLARE_PER_CPU(struct s390_idle_data, s390_idle);
  152. cputime64_t s390_get_idle_time(int cpu);
  153. #define arch_idle_time(cpu) s390_get_idle_time(cpu)
  154. static inline int s390_nohz_delay(int cpu)
  155. {
  156. return __get_cpu_var(s390_idle).nohz_delay != 0;
  157. }
  158. #define arch_needs_cpu(cpu) s390_nohz_delay(cpu)
  159. #endif /* _S390_CPUTIME_H */