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 20 #include <asm-generic/cputime.h> 21 #ifdef __KERNEL__ 22 static inline void setup_cputime_one_jiffy(void) { } 23 #endif 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)) 49 #define cputime64_sub(__a, __b) ((__a) - (__b)) 50 #define cputime_to_cputime64(__ct) (__ct) 51 52 #ifdef __KERNEL__ 53 54 /* 55 * One jiffy in timebase units computed during initialization 56 */ 57 extern cputime_t cputime_one_jiffy; 58 59 /* 60 * Convert cputime <-> jiffies 61 */ 62 extern u64 __cputime_jiffies_factor; 63 DECLARE_PER_CPU(unsigned long, cputime_last_delta); 64 DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta); 65 66 static inline unsigned long cputime_to_jiffies(const cputime_t ct) 67 { 68 return mulhdu(ct, __cputime_jiffies_factor); 69 } 70 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 __get_cpu_var(cputime_last_delta)) 77 return ct * __get_cpu_var(cputime_scaled_last_delta) / 78 __get_cpu_var(cputime_last_delta); 79 return ct; 80 } 81 82 static inline cputime_t jiffies_to_cputime(const unsigned long jif) 83 { 84 cputime_t ct; 85 unsigned long sec; 86 87 /* have to be a little careful about overflow */ 88 ct = jif % HZ; 89 sec = jif / HZ; 90 if (ct) { 91 ct *= tb_ticks_per_sec; 92 do_div(ct, HZ); 93 } 94 if (sec) 95 ct += (cputime_t) sec * tb_ticks_per_sec; 96 return ct; 97 } 98 99 static inline void setup_cputime_one_jiffy(void) 100 { 101 cputime_one_jiffy = jiffies_to_cputime(1); 102 } 103 104 static inline cputime64_t jiffies64_to_cputime64(const u64 jif) 105 { 106 cputime_t ct; 107 u64 sec; 108 109 /* have to be a little careful about overflow */ 110 ct = jif % HZ; 111 sec = jif / HZ; 112 if (ct) { 113 ct *= tb_ticks_per_sec; 114 do_div(ct, HZ); 115 } 116 if (sec) 117 ct += (cputime_t) sec * tb_ticks_per_sec; 118 return ct; 119 } 120 121 static inline u64 cputime64_to_jiffies64(const cputime_t ct) 122 { 123 return mulhdu(ct, __cputime_jiffies_factor); 124 } 125 126 /* 127 * Convert cputime <-> milliseconds 128 */ 129 extern u64 __cputime_msec_factor; 130 131 static inline unsigned long cputime_to_msecs(const cputime_t ct) 132 { 133 return mulhdu(ct, __cputime_msec_factor); 134 } 135 136 static inline cputime_t msecs_to_cputime(const unsigned long ms) 137 { 138 cputime_t ct; 139 unsigned long sec; 140 141 /* have to be a little careful about overflow */ 142 ct = ms % 1000; 143 sec = ms / 1000; 144 if (ct) { 145 ct *= tb_ticks_per_sec; 146 do_div(ct, 1000); 147 } 148 if (sec) 149 ct += (cputime_t) sec * tb_ticks_per_sec; 150 return ct; 151 } 152 153 /* 154 * Convert cputime <-> seconds 155 */ 156 extern u64 __cputime_sec_factor; 157 158 static inline unsigned long cputime_to_secs(const cputime_t ct) 159 { 160 return mulhdu(ct, __cputime_sec_factor); 161 } 162 163 static inline cputime_t secs_to_cputime(const unsigned long sec) 164 { 165 return (cputime_t) sec * tb_ticks_per_sec; 166 } 167 168 /* 169 * Convert cputime <-> timespec 170 */ 171 static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p) 172 { 173 u64 x = ct; 174 unsigned int frac; 175 176 frac = do_div(x, tb_ticks_per_sec); 177 p->tv_sec = x; 178 x = (u64) frac * 1000000000; 179 do_div(x, tb_ticks_per_sec); 180 p->tv_nsec = x; 181 } 182 183 static inline cputime_t timespec_to_cputime(const struct timespec *p) 184 { 185 cputime_t ct; 186 187 ct = (u64) p->tv_nsec * tb_ticks_per_sec; 188 do_div(ct, 1000000000); 189 return ct + (u64) p->tv_sec * tb_ticks_per_sec; 190 } 191 192 /* 193 * Convert cputime <-> timeval 194 */ 195 static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p) 196 { 197 u64 x = ct; 198 unsigned int frac; 199 200 frac = do_div(x, tb_ticks_per_sec); 201 p->tv_sec = x; 202 x = (u64) frac * 1000000; 203 do_div(x, tb_ticks_per_sec); 204 p->tv_usec = x; 205 } 206 207 static inline cputime_t timeval_to_cputime(const struct timeval *p) 208 { 209 cputime_t ct; 210 211 ct = (u64) p->tv_usec * tb_ticks_per_sec; 212 do_div(ct, 1000000); 213 return ct + (u64) p->tv_sec * tb_ticks_per_sec; 214 } 215 216 /* 217 * Convert cputime <-> clock_t (units of 1/USER_HZ seconds) 218 */ 219 extern u64 __cputime_clockt_factor; 220 221 static inline unsigned long cputime_to_clock_t(const cputime_t ct) 222 { 223 return mulhdu(ct, __cputime_clockt_factor); 224 } 225 226 static inline cputime_t clock_t_to_cputime(const unsigned long clk) 227 { 228 cputime_t ct; 229 unsigned long sec; 230 231 /* have to be a little careful about overflow */ 232 ct = clk % USER_HZ; 233 sec = clk / USER_HZ; 234 if (ct) { 235 ct *= tb_ticks_per_sec; 236 do_div(ct, USER_HZ); 237 } 238 if (sec) 239 ct += (cputime_t) sec * tb_ticks_per_sec; 240 return ct; 241 } 242 243 #define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct)) 244 245 #endif /* __KERNEL__ */ 246 #endif /* CONFIG_VIRT_CPU_ACCOUNTING */ 247 #endif /* __POWERPC_CPUTIME_H */ 248