cputime.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #define CPUTIME_PER_USEC 4096ULL
  11. #define CPUTIME_PER_SEC (CPUTIME_PER_USEC * USEC_PER_SEC)
  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. #define cmpxchg_cputime(ptr, old, new) cmpxchg64(ptr, old, new)
  16. static inline unsigned long __div(unsigned long long n, unsigned long base)
  17. {
  18. return n / base;
  19. }
  20. #define cputime_one_jiffy jiffies_to_cputime(1)
  21. /*
  22. * Convert cputime to jiffies and back.
  23. */
  24. static inline unsigned long cputime_to_jiffies(const cputime_t cputime)
  25. {
  26. return __div((__force unsigned long long) cputime, CPUTIME_PER_SEC / HZ);
  27. }
  28. static inline cputime_t jiffies_to_cputime(const unsigned int jif)
  29. {
  30. return (__force cputime_t)(jif * (CPUTIME_PER_SEC / HZ));
  31. }
  32. static inline u64 cputime64_to_jiffies64(cputime64_t cputime)
  33. {
  34. unsigned long long jif = (__force unsigned long long) cputime;
  35. do_div(jif, CPUTIME_PER_SEC / HZ);
  36. return jif;
  37. }
  38. static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
  39. {
  40. return (__force cputime64_t)(jif * (CPUTIME_PER_SEC / HZ));
  41. }
  42. /*
  43. * Convert cputime to microseconds and back.
  44. */
  45. static inline unsigned int cputime_to_usecs(const cputime_t cputime)
  46. {
  47. return (__force unsigned long long) cputime >> 12;
  48. }
  49. static inline cputime_t usecs_to_cputime(const unsigned int m)
  50. {
  51. return (__force cputime_t)(m * CPUTIME_PER_USEC);
  52. }
  53. #define usecs_to_cputime64(m) usecs_to_cputime(m)
  54. /*
  55. * Convert cputime to milliseconds and back.
  56. */
  57. static inline unsigned int cputime_to_secs(const cputime_t cputime)
  58. {
  59. return __div((__force unsigned long long) cputime, CPUTIME_PER_SEC / 2) >> 1;
  60. }
  61. static inline cputime_t secs_to_cputime(const unsigned int s)
  62. {
  63. return (__force cputime_t)(s * CPUTIME_PER_SEC);
  64. }
  65. /*
  66. * Convert cputime to timespec and back.
  67. */
  68. static inline cputime_t timespec_to_cputime(const struct timespec *value)
  69. {
  70. unsigned long long ret = value->tv_sec * CPUTIME_PER_SEC;
  71. return (__force cputime_t)(ret + __div(value->tv_nsec * CPUTIME_PER_USEC, NSEC_PER_USEC));
  72. }
  73. static inline void cputime_to_timespec(const cputime_t cputime,
  74. struct timespec *value)
  75. {
  76. unsigned long long __cputime = (__force unsigned long long) cputime;
  77. value->tv_nsec = (__cputime % CPUTIME_PER_SEC) * NSEC_PER_USEC / CPUTIME_PER_USEC;
  78. value->tv_sec = __cputime / CPUTIME_PER_SEC;
  79. }
  80. /*
  81. * Convert cputime to timeval and back.
  82. * Since cputime and timeval have the same resolution (microseconds)
  83. * this is easy.
  84. */
  85. static inline cputime_t timeval_to_cputime(const struct timeval *value)
  86. {
  87. unsigned long long ret = value->tv_sec * CPUTIME_PER_SEC;
  88. return (__force cputime_t)(ret + value->tv_usec * CPUTIME_PER_USEC);
  89. }
  90. static inline void cputime_to_timeval(const cputime_t cputime,
  91. struct timeval *value)
  92. {
  93. unsigned long long __cputime = (__force unsigned long long) cputime;
  94. value->tv_usec = (__cputime % CPUTIME_PER_SEC) / CPUTIME_PER_USEC;
  95. value->tv_sec = __cputime / CPUTIME_PER_SEC;
  96. }
  97. /*
  98. * Convert cputime to clock and back.
  99. */
  100. static inline clock_t cputime_to_clock_t(cputime_t cputime)
  101. {
  102. unsigned long long clock = (__force unsigned long long) cputime;
  103. do_div(clock, CPUTIME_PER_SEC / USER_HZ);
  104. return clock;
  105. }
  106. static inline cputime_t clock_t_to_cputime(unsigned long x)
  107. {
  108. return (__force cputime_t)(x * (CPUTIME_PER_SEC / USER_HZ));
  109. }
  110. /*
  111. * Convert cputime64 to clock.
  112. */
  113. static inline clock_t cputime64_to_clock_t(cputime64_t cputime)
  114. {
  115. unsigned long long clock = (__force unsigned long long) cputime;
  116. do_div(clock, CPUTIME_PER_SEC / USER_HZ);
  117. return clock;
  118. }
  119. cputime64_t arch_cpu_idle_time(int cpu);
  120. #define arch_idle_time(cpu) arch_cpu_idle_time(cpu)
  121. #endif /* _S390_CPUTIME_H */