Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Definitions for measuring cputime on powerpc machines. |
| 3 | * |
| 4 | * Copyright (C) 2006 Paul Mackerras, IBM Corp. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * If we have CONFIG_VIRT_CPU_ACCOUNTING, we measure cpu time in |
| 12 | * the same units as the timebase. Otherwise we measure cpu time |
| 13 | * in jiffies using the generic definitions. |
| 14 | */ |
| 15 | |
| 16 | #ifndef __POWERPC_CPUTIME_H |
| 17 | #define __POWERPC_CPUTIME_H |
| 18 | |
| 19 | #ifndef CONFIG_VIRT_CPU_ACCOUNTING |
Stephen Rothwell | 88999ce | 2005-08-29 14:06:56 +1000 | [diff] [blame] | 20 | #include <asm-generic/cputime.h> |
Stanislaw Gruszka | a42548a | 2009-07-29 12:15:29 +0200 | [diff] [blame] | 21 | #ifdef __KERNEL__ |
| 22 | static inline void setup_cputime_one_jiffy(void) { } |
| 23 | #endif |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 24 | #else |
| 25 | |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/time.h> |
| 28 | #include <asm/div64.h> |
| 29 | #include <asm/time.h> |
| 30 | #include <asm/param.h> |
| 31 | |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 32 | typedef u64 __nocast cputime_t; |
| 33 | typedef u64 __nocast cputime64_t; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 34 | |
| 35 | #ifdef __KERNEL__ |
| 36 | |
| 37 | /* |
Stanislaw Gruszka | a42548a | 2009-07-29 12:15:29 +0200 | [diff] [blame] | 38 | * One jiffy in timebase units computed during initialization |
| 39 | */ |
| 40 | extern cputime_t cputime_one_jiffy; |
| 41 | |
| 42 | /* |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 43 | * Convert cputime <-> jiffies |
| 44 | */ |
| 45 | extern u64 __cputime_jiffies_factor; |
Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 46 | DECLARE_PER_CPU(unsigned long, cputime_last_delta); |
| 47 | DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 48 | |
| 49 | static inline unsigned long cputime_to_jiffies(const cputime_t ct) |
| 50 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 51 | return mulhdu((__force u64) ct, __cputime_jiffies_factor); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 52 | } |
| 53 | |
Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 54 | /* Estimate the scaled cputime by scaling the real cputime based on |
| 55 | * the last scaled to real ratio */ |
| 56 | static inline cputime_t cputime_to_scaled(const cputime_t ct) |
| 57 | { |
| 58 | if (cpu_has_feature(CPU_FTR_SPURR) && |
Anton Blanchard | 61c03dd | 2010-01-13 12:04:11 +0000 | [diff] [blame] | 59 | __get_cpu_var(cputime_last_delta)) |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 60 | return (__force u64) ct * |
| 61 | __get_cpu_var(cputime_scaled_last_delta) / |
| 62 | __get_cpu_var(cputime_last_delta); |
Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 63 | return ct; |
| 64 | } |
| 65 | |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 66 | static inline cputime_t jiffies_to_cputime(const unsigned long jif) |
| 67 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 68 | u64 ct; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 69 | unsigned long sec; |
| 70 | |
| 71 | /* have to be a little careful about overflow */ |
| 72 | ct = jif % HZ; |
| 73 | sec = jif / HZ; |
| 74 | if (ct) { |
| 75 | ct *= tb_ticks_per_sec; |
| 76 | do_div(ct, HZ); |
| 77 | } |
| 78 | if (sec) |
| 79 | ct += (cputime_t) sec * tb_ticks_per_sec; |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 80 | return (__force cputime_t) ct; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 81 | } |
| 82 | |
Stanislaw Gruszka | a42548a | 2009-07-29 12:15:29 +0200 | [diff] [blame] | 83 | static inline void setup_cputime_one_jiffy(void) |
| 84 | { |
| 85 | cputime_one_jiffy = jiffies_to_cputime(1); |
| 86 | } |
| 87 | |
David Woodhouse | a8e0c51 | 2006-07-05 11:24:26 +0100 | [diff] [blame] | 88 | static inline cputime64_t jiffies64_to_cputime64(const u64 jif) |
| 89 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 90 | u64 ct; |
David Woodhouse | a8e0c51 | 2006-07-05 11:24:26 +0100 | [diff] [blame] | 91 | u64 sec; |
| 92 | |
| 93 | /* have to be a little careful about overflow */ |
| 94 | ct = jif % HZ; |
| 95 | sec = jif / HZ; |
| 96 | if (ct) { |
| 97 | ct *= tb_ticks_per_sec; |
| 98 | do_div(ct, HZ); |
| 99 | } |
| 100 | if (sec) |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 101 | ct += (u64) sec * tb_ticks_per_sec; |
| 102 | return (__force cputime64_t) ct; |
David Woodhouse | a8e0c51 | 2006-07-05 11:24:26 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 105 | static inline u64 cputime64_to_jiffies64(const cputime_t ct) |
| 106 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 107 | return mulhdu((__force u64) ct, __cputime_jiffies_factor); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* |
Michael Holzheu | d57af9b | 2010-10-27 15:34:45 -0700 | [diff] [blame] | 111 | * Convert cputime <-> microseconds |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 112 | */ |
Andreas Schwab | 9f5072d | 2011-12-09 11:35:08 +0000 | [diff] [blame] | 113 | extern u64 __cputime_usec_factor; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 114 | |
Michael Holzheu | d57af9b | 2010-10-27 15:34:45 -0700 | [diff] [blame] | 115 | static inline unsigned long cputime_to_usecs(const cputime_t ct) |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 116 | { |
Linus Torvalds | e4e88f3 | 2012-01-06 17:58:22 -0800 | [diff] [blame^] | 117 | return mulhdu((__force u64) ct, __cputime_usec_factor); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 118 | } |
| 119 | |
Michael Holzheu | d57af9b | 2010-10-27 15:34:45 -0700 | [diff] [blame] | 120 | static inline cputime_t usecs_to_cputime(const unsigned long us) |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 121 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 122 | u64 ct; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 123 | unsigned long sec; |
| 124 | |
| 125 | /* have to be a little careful about overflow */ |
Michael Holzheu | d57af9b | 2010-10-27 15:34:45 -0700 | [diff] [blame] | 126 | ct = us % 1000000; |
| 127 | sec = us / 1000000; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 128 | if (ct) { |
| 129 | ct *= tb_ticks_per_sec; |
Andreas Schwab | 9f5072d | 2011-12-09 11:35:08 +0000 | [diff] [blame] | 130 | do_div(ct, 1000000); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 131 | } |
| 132 | if (sec) |
| 133 | ct += (cputime_t) sec * tb_ticks_per_sec; |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 134 | return (__force cputime_t) ct; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 135 | } |
| 136 | |
Andreas Schwab | 3484563 | 2011-12-28 15:57:15 -0800 | [diff] [blame] | 137 | #define usecs_to_cputime64(us) usecs_to_cputime(us) |
| 138 | |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 139 | /* |
| 140 | * Convert cputime <-> seconds |
| 141 | */ |
| 142 | extern u64 __cputime_sec_factor; |
| 143 | |
| 144 | static inline unsigned long cputime_to_secs(const cputime_t ct) |
| 145 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 146 | return mulhdu((__force u64) ct, __cputime_sec_factor); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static inline cputime_t secs_to_cputime(const unsigned long sec) |
| 150 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 151 | return (__force cputime_t)((u64) sec * tb_ticks_per_sec); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Convert cputime <-> timespec |
| 156 | */ |
| 157 | static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p) |
| 158 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 159 | u64 x = (__force u64) ct; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 160 | unsigned int frac; |
| 161 | |
| 162 | frac = do_div(x, tb_ticks_per_sec); |
| 163 | p->tv_sec = x; |
| 164 | x = (u64) frac * 1000000000; |
| 165 | do_div(x, tb_ticks_per_sec); |
| 166 | p->tv_nsec = x; |
| 167 | } |
| 168 | |
| 169 | static inline cputime_t timespec_to_cputime(const struct timespec *p) |
| 170 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 171 | u64 ct; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 172 | |
| 173 | ct = (u64) p->tv_nsec * tb_ticks_per_sec; |
| 174 | do_div(ct, 1000000000); |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 175 | return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Convert cputime <-> timeval |
| 180 | */ |
| 181 | static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p) |
| 182 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 183 | u64 x = (__force u64) ct; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 184 | unsigned int frac; |
| 185 | |
| 186 | frac = do_div(x, tb_ticks_per_sec); |
| 187 | p->tv_sec = x; |
| 188 | x = (u64) frac * 1000000; |
| 189 | do_div(x, tb_ticks_per_sec); |
| 190 | p->tv_usec = x; |
| 191 | } |
| 192 | |
| 193 | static inline cputime_t timeval_to_cputime(const struct timeval *p) |
| 194 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 195 | u64 ct; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 196 | |
| 197 | ct = (u64) p->tv_usec * tb_ticks_per_sec; |
| 198 | do_div(ct, 1000000); |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 199 | return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Convert cputime <-> clock_t (units of 1/USER_HZ seconds) |
| 204 | */ |
| 205 | extern u64 __cputime_clockt_factor; |
| 206 | |
| 207 | static inline unsigned long cputime_to_clock_t(const cputime_t ct) |
| 208 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 209 | return mulhdu((__force u64) ct, __cputime_clockt_factor); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static inline cputime_t clock_t_to_cputime(const unsigned long clk) |
| 213 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 214 | u64 ct; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 215 | unsigned long sec; |
| 216 | |
| 217 | /* have to be a little careful about overflow */ |
| 218 | ct = clk % USER_HZ; |
| 219 | sec = clk / USER_HZ; |
| 220 | if (ct) { |
| 221 | ct *= tb_ticks_per_sec; |
| 222 | do_div(ct, USER_HZ); |
| 223 | } |
| 224 | if (sec) |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 225 | ct += (u64) sec * tb_ticks_per_sec; |
| 226 | return (__force cputime_t) ct; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | #define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct)) |
| 230 | |
| 231 | #endif /* __KERNEL__ */ |
| 232 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING */ |
| 233 | #endif /* __POWERPC_CPUTIME_H */ |