cputime.h 4.7 KB

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