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 | |
| 32 | typedef u64 cputime_t; |
| 33 | typedef u64 cputime64_t; |
| 34 | |
| 35 | #define cputime_zero ((cputime_t)0) |
| 36 | #define cputime_max ((~((cputime_t)0) >> 1) - 1) |
| 37 | #define cputime_add(__a, __b) ((__a) + (__b)) |
| 38 | #define cputime_sub(__a, __b) ((__a) - (__b)) |
| 39 | #define cputime_div(__a, __n) ((__a) / (__n)) |
| 40 | #define cputime_halve(__a) ((__a) >> 1) |
| 41 | #define cputime_eq(__a, __b) ((__a) == (__b)) |
| 42 | #define cputime_gt(__a, __b) ((__a) > (__b)) |
| 43 | #define cputime_ge(__a, __b) ((__a) >= (__b)) |
| 44 | #define cputime_lt(__a, __b) ((__a) < (__b)) |
| 45 | #define cputime_le(__a, __b) ((__a) <= (__b)) |
| 46 | |
| 47 | #define cputime64_zero ((cputime64_t)0) |
| 48 | #define cputime64_add(__a, __b) ((__a) + (__b)) |
David Woodhouse | a8e0c51 | 2006-07-05 11:24:26 +0100 | [diff] [blame] | 49 | #define cputime64_sub(__a, __b) ((__a) - (__b)) |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 50 | #define cputime_to_cputime64(__ct) (__ct) |
| 51 | |
| 52 | #ifdef __KERNEL__ |
| 53 | |
| 54 | /* |
Stanislaw Gruszka | a42548a | 2009-07-29 12:15:29 +0200 | [diff] [blame] | 55 | * One jiffy in timebase units computed during initialization |
| 56 | */ |
| 57 | extern cputime_t cputime_one_jiffy; |
| 58 | |
| 59 | /* |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 60 | * Convert cputime <-> jiffies |
| 61 | */ |
| 62 | extern u64 __cputime_jiffies_factor; |
Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 63 | DECLARE_PER_CPU(unsigned long, cputime_last_delta); |
| 64 | DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 65 | |
| 66 | static inline unsigned long cputime_to_jiffies(const cputime_t ct) |
| 67 | { |
| 68 | return mulhdu(ct, __cputime_jiffies_factor); |
| 69 | } |
| 70 | |
Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 71 | /* Estimate the scaled cputime by scaling the real cputime based on |
| 72 | * the last scaled to real ratio */ |
| 73 | static inline cputime_t cputime_to_scaled(const cputime_t ct) |
| 74 | { |
| 75 | if (cpu_has_feature(CPU_FTR_SPURR) && |
| 76 | per_cpu(cputime_last_delta, smp_processor_id())) |
| 77 | return ct * |
| 78 | per_cpu(cputime_scaled_last_delta, smp_processor_id())/ |
| 79 | per_cpu(cputime_last_delta, smp_processor_id()); |
| 80 | return ct; |
| 81 | } |
| 82 | |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 83 | static inline cputime_t jiffies_to_cputime(const unsigned long jif) |
| 84 | { |
| 85 | cputime_t ct; |
| 86 | unsigned long sec; |
| 87 | |
| 88 | /* have to be a little careful about overflow */ |
| 89 | ct = jif % HZ; |
| 90 | sec = jif / HZ; |
| 91 | if (ct) { |
| 92 | ct *= tb_ticks_per_sec; |
| 93 | do_div(ct, HZ); |
| 94 | } |
| 95 | if (sec) |
| 96 | ct += (cputime_t) sec * tb_ticks_per_sec; |
| 97 | return ct; |
| 98 | } |
| 99 | |
Stanislaw Gruszka | a42548a | 2009-07-29 12:15:29 +0200 | [diff] [blame] | 100 | static inline void setup_cputime_one_jiffy(void) |
| 101 | { |
| 102 | cputime_one_jiffy = jiffies_to_cputime(1); |
| 103 | } |
| 104 | |
David Woodhouse | a8e0c51 | 2006-07-05 11:24:26 +0100 | [diff] [blame] | 105 | static inline cputime64_t jiffies64_to_cputime64(const u64 jif) |
| 106 | { |
| 107 | cputime_t ct; |
| 108 | u64 sec; |
| 109 | |
| 110 | /* have to be a little careful about overflow */ |
| 111 | ct = jif % HZ; |
| 112 | sec = jif / HZ; |
| 113 | if (ct) { |
| 114 | ct *= tb_ticks_per_sec; |
| 115 | do_div(ct, HZ); |
| 116 | } |
| 117 | if (sec) |
| 118 | ct += (cputime_t) sec * tb_ticks_per_sec; |
| 119 | return ct; |
| 120 | } |
| 121 | |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 122 | static inline u64 cputime64_to_jiffies64(const cputime_t ct) |
| 123 | { |
| 124 | return mulhdu(ct, __cputime_jiffies_factor); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Convert cputime <-> milliseconds |
| 129 | */ |
| 130 | extern u64 __cputime_msec_factor; |
| 131 | |
| 132 | static inline unsigned long cputime_to_msecs(const cputime_t ct) |
| 133 | { |
| 134 | return mulhdu(ct, __cputime_msec_factor); |
| 135 | } |
| 136 | |
| 137 | static inline cputime_t msecs_to_cputime(const unsigned long ms) |
| 138 | { |
| 139 | cputime_t ct; |
| 140 | unsigned long sec; |
| 141 | |
| 142 | /* have to be a little careful about overflow */ |
| 143 | ct = ms % 1000; |
| 144 | sec = ms / 1000; |
| 145 | if (ct) { |
| 146 | ct *= tb_ticks_per_sec; |
| 147 | do_div(ct, 1000); |
| 148 | } |
| 149 | if (sec) |
| 150 | ct += (cputime_t) sec * tb_ticks_per_sec; |
| 151 | return ct; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Convert cputime <-> seconds |
| 156 | */ |
| 157 | extern u64 __cputime_sec_factor; |
| 158 | |
| 159 | static inline unsigned long cputime_to_secs(const cputime_t ct) |
| 160 | { |
| 161 | return mulhdu(ct, __cputime_sec_factor); |
| 162 | } |
| 163 | |
| 164 | static inline cputime_t secs_to_cputime(const unsigned long sec) |
| 165 | { |
| 166 | return (cputime_t) sec * tb_ticks_per_sec; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Convert cputime <-> timespec |
| 171 | */ |
| 172 | static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p) |
| 173 | { |
| 174 | u64 x = ct; |
| 175 | unsigned int frac; |
| 176 | |
| 177 | frac = do_div(x, tb_ticks_per_sec); |
| 178 | p->tv_sec = x; |
| 179 | x = (u64) frac * 1000000000; |
| 180 | do_div(x, tb_ticks_per_sec); |
| 181 | p->tv_nsec = x; |
| 182 | } |
| 183 | |
| 184 | static inline cputime_t timespec_to_cputime(const struct timespec *p) |
| 185 | { |
| 186 | cputime_t ct; |
| 187 | |
| 188 | ct = (u64) p->tv_nsec * tb_ticks_per_sec; |
| 189 | do_div(ct, 1000000000); |
| 190 | return ct + (u64) p->tv_sec * tb_ticks_per_sec; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Convert cputime <-> timeval |
| 195 | */ |
| 196 | static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p) |
| 197 | { |
| 198 | u64 x = ct; |
| 199 | unsigned int frac; |
| 200 | |
| 201 | frac = do_div(x, tb_ticks_per_sec); |
| 202 | p->tv_sec = x; |
| 203 | x = (u64) frac * 1000000; |
| 204 | do_div(x, tb_ticks_per_sec); |
| 205 | p->tv_usec = x; |
| 206 | } |
| 207 | |
| 208 | static inline cputime_t timeval_to_cputime(const struct timeval *p) |
| 209 | { |
| 210 | cputime_t ct; |
| 211 | |
| 212 | ct = (u64) p->tv_usec * tb_ticks_per_sec; |
| 213 | do_div(ct, 1000000); |
| 214 | return ct + (u64) p->tv_sec * tb_ticks_per_sec; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Convert cputime <-> clock_t (units of 1/USER_HZ seconds) |
| 219 | */ |
| 220 | extern u64 __cputime_clockt_factor; |
| 221 | |
| 222 | static inline unsigned long cputime_to_clock_t(const cputime_t ct) |
| 223 | { |
| 224 | return mulhdu(ct, __cputime_clockt_factor); |
| 225 | } |
| 226 | |
| 227 | static inline cputime_t clock_t_to_cputime(const unsigned long clk) |
| 228 | { |
| 229 | cputime_t ct; |
| 230 | unsigned long sec; |
| 231 | |
| 232 | /* have to be a little careful about overflow */ |
| 233 | ct = clk % USER_HZ; |
| 234 | sec = clk / USER_HZ; |
| 235 | if (ct) { |
| 236 | ct *= tb_ticks_per_sec; |
| 237 | do_div(ct, USER_HZ); |
| 238 | } |
| 239 | if (sec) |
| 240 | ct += (cputime_t) sec * tb_ticks_per_sec; |
| 241 | return ct; |
| 242 | } |
| 243 | |
| 244 | #define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct)) |
| 245 | |
| 246 | #endif /* __KERNEL__ */ |
| 247 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING */ |
| 248 | #endif /* __POWERPC_CPUTIME_H */ |