2 * Definitions for measuring cputime on powerpc machines.
4 * Copyright (C) 2006 Paul Mackerras, IBM Corp.
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.
11 * If we have CONFIG_VIRT_CPU_ACCOUNTING_NATIVE, we measure cpu time in
12 * the same units as the timebase. Otherwise we measure cpu time
13 * in jiffies using the generic definitions.
16 #ifndef __POWERPC_CPUTIME_H
17 #define __POWERPC_CPUTIME_H
19 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
20 #include <asm-generic/cputime.h>
22 static inline void setup_cputime_one_jiffy(void) { }
26 #include <linux/types.h>
27 #include <linux/time.h>
28 #include <asm/div64.h>
30 #include <asm/param.h>
32 typedef u64 __nocast cputime_t
;
33 typedef u64 __nocast cputime64_t
;
35 #define cmpxchg_cputime(ptr, old, new) cmpxchg(ptr, old, new)
40 * One jiffy in timebase units computed during initialization
42 extern cputime_t cputime_one_jiffy
;
45 * Convert cputime <-> jiffies
47 extern u64 __cputime_jiffies_factor
;
48 DECLARE_PER_CPU(unsigned long, cputime_last_delta
);
49 DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta
);
51 static inline unsigned long cputime_to_jiffies(const cputime_t ct
)
53 return mulhdu((__force u64
) ct
, __cputime_jiffies_factor
);
56 /* Estimate the scaled cputime by scaling the real cputime based on
57 * the last scaled to real ratio */
58 static inline cputime_t
cputime_to_scaled(const cputime_t ct
)
60 if (cpu_has_feature(CPU_FTR_SPURR
) &&
61 __this_cpu_read(cputime_last_delta
))
62 return (__force u64
) ct
*
63 __this_cpu_read(cputime_scaled_last_delta
) /
64 __this_cpu_read(cputime_last_delta
);
68 static inline cputime_t
jiffies_to_cputime(const unsigned long jif
)
73 /* have to be a little careful about overflow */
77 ct
*= tb_ticks_per_sec
;
81 ct
+= (cputime_t
) sec
* tb_ticks_per_sec
;
82 return (__force cputime_t
) ct
;
85 static inline void setup_cputime_one_jiffy(void)
87 cputime_one_jiffy
= jiffies_to_cputime(1);
90 static inline cputime64_t
jiffies64_to_cputime64(const u64 jif
)
95 /* have to be a little careful about overflow */
98 ct
*= tb_ticks_per_sec
;
102 ct
+= (u64
) sec
* tb_ticks_per_sec
;
103 return (__force cputime64_t
) ct
;
106 static inline u64
cputime64_to_jiffies64(const cputime_t ct
)
108 return mulhdu((__force u64
) ct
, __cputime_jiffies_factor
);
112 * Convert cputime <-> microseconds
114 extern u64 __cputime_usec_factor
;
116 static inline unsigned long cputime_to_usecs(const cputime_t ct
)
118 return mulhdu((__force u64
) ct
, __cputime_usec_factor
);
121 static inline cputime_t
usecs_to_cputime(const unsigned long us
)
126 /* have to be a little careful about overflow */
130 ct
*= tb_ticks_per_sec
;
134 ct
+= (cputime_t
) sec
* tb_ticks_per_sec
;
135 return (__force cputime_t
) ct
;
138 #define usecs_to_cputime64(us) usecs_to_cputime(us)
141 * Convert cputime <-> seconds
143 extern u64 __cputime_sec_factor
;
145 static inline unsigned long cputime_to_secs(const cputime_t ct
)
147 return mulhdu((__force u64
) ct
, __cputime_sec_factor
);
150 static inline cputime_t
secs_to_cputime(const unsigned long sec
)
152 return (__force cputime_t
)((u64
) sec
* tb_ticks_per_sec
);
156 * Convert cputime <-> timespec
158 static inline void cputime_to_timespec(const cputime_t ct
, struct timespec
*p
)
160 u64 x
= (__force u64
) ct
;
163 frac
= do_div(x
, tb_ticks_per_sec
);
165 x
= (u64
) frac
* 1000000000;
166 do_div(x
, tb_ticks_per_sec
);
170 static inline cputime_t
timespec_to_cputime(const struct timespec
*p
)
174 ct
= (u64
) p
->tv_nsec
* tb_ticks_per_sec
;
175 do_div(ct
, 1000000000);
176 return (__force cputime_t
)(ct
+ (u64
) p
->tv_sec
* tb_ticks_per_sec
);
180 * Convert cputime <-> timeval
182 static inline void cputime_to_timeval(const cputime_t ct
, struct timeval
*p
)
184 u64 x
= (__force u64
) ct
;
187 frac
= do_div(x
, tb_ticks_per_sec
);
189 x
= (u64
) frac
* 1000000;
190 do_div(x
, tb_ticks_per_sec
);
194 static inline cputime_t
timeval_to_cputime(const struct timeval
*p
)
198 ct
= (u64
) p
->tv_usec
* tb_ticks_per_sec
;
200 return (__force cputime_t
)(ct
+ (u64
) p
->tv_sec
* tb_ticks_per_sec
);
204 * Convert cputime <-> clock_t (units of 1/USER_HZ seconds)
206 extern u64 __cputime_clockt_factor
;
208 static inline unsigned long cputime_to_clock_t(const cputime_t ct
)
210 return mulhdu((__force u64
) ct
, __cputime_clockt_factor
);
213 static inline cputime_t
clock_t_to_cputime(const unsigned long clk
)
218 /* have to be a little careful about overflow */
222 ct
*= tb_ticks_per_sec
;
226 ct
+= (u64
) sec
* tb_ticks_per_sec
;
227 return (__force cputime_t
) ct
;
230 #define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct))
233 * PPC64 uses PACA which is task independent for storing accounting data while
234 * PPC32 uses struct thread_info, therefore at task switch the accounting data
235 * has to be populated in the new task
238 static inline void arch_vtime_task_switch(struct task_struct
*tsk
) { }
240 void arch_vtime_task_switch(struct task_struct
*tsk
);
243 #endif /* __KERNEL__ */
244 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
245 #endif /* __POWERPC_CPUTIME_H */