blob: 98b7c4b49c9d0586cce4d7132593ee359c3fd803 [file] [log] [blame]
Paul Mackerrasc6622f62006-02-24 10:06:59 +11001/*
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 Rothwell88999ce2005-08-29 14:06:56 +100020#include <asm-generic/cputime.h>
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +020021#ifdef __KERNEL__
22static inline void setup_cputime_one_jiffy(void) { }
23#endif
Paul Mackerrasc6622f62006-02-24 10:06:59 +110024#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
32typedef u64 cputime_t;
33typedef 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 Woodhousea8e0c512006-07-05 11:24:26 +010049#define cputime64_sub(__a, __b) ((__a) - (__b))
Paul Mackerrasc6622f62006-02-24 10:06:59 +110050#define cputime_to_cputime64(__ct) (__ct)
51
52#ifdef __KERNEL__
53
54/*
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +020055 * One jiffy in timebase units computed during initialization
56 */
57extern cputime_t cputime_one_jiffy;
58
59/*
Paul Mackerrasc6622f62006-02-24 10:06:59 +110060 * Convert cputime <-> jiffies
61 */
62extern u64 __cputime_jiffies_factor;
Michael Neuling06b8e872008-02-06 01:36:12 -080063DECLARE_PER_CPU(unsigned long, cputime_last_delta);
64DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta);
Paul Mackerrasc6622f62006-02-24 10:06:59 +110065
66static inline unsigned long cputime_to_jiffies(const cputime_t ct)
67{
68 return mulhdu(ct, __cputime_jiffies_factor);
69}
70
Michael Neuling06b8e872008-02-06 01:36:12 -080071/* Estimate the scaled cputime by scaling the real cputime based on
72 * the last scaled to real ratio */
73static inline cputime_t cputime_to_scaled(const cputime_t ct)
74{
75 if (cpu_has_feature(CPU_FTR_SPURR) &&
Anton Blanchard61c03dd2010-01-13 12:04:11 +000076 __get_cpu_var(cputime_last_delta))
77 return ct * __get_cpu_var(cputime_scaled_last_delta) /
78 __get_cpu_var(cputime_last_delta);
Michael Neuling06b8e872008-02-06 01:36:12 -080079 return ct;
80}
81
Paul Mackerrasc6622f62006-02-24 10:06:59 +110082static 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
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +020099static inline void setup_cputime_one_jiffy(void)
100{
101 cputime_one_jiffy = jiffies_to_cputime(1);
102}
103
David Woodhousea8e0c512006-07-05 11:24:26 +0100104static 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
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100121static inline u64 cputime64_to_jiffies64(const cputime_t ct)
122{
123 return mulhdu(ct, __cputime_jiffies_factor);
124}
125
126/*
Michael Holzheud57af9b2010-10-27 15:34:45 -0700127 * Convert cputime <-> microseconds
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100128 */
129extern u64 __cputime_msec_factor;
130
Michael Holzheud57af9b2010-10-27 15:34:45 -0700131static inline unsigned long cputime_to_usecs(const cputime_t ct)
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100132{
Michael Holzheud57af9b2010-10-27 15:34:45 -0700133 return mulhdu(ct, __cputime_msec_factor) * USEC_PER_MSEC;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100134}
135
Michael Holzheud57af9b2010-10-27 15:34:45 -0700136static inline cputime_t usecs_to_cputime(const unsigned long us)
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100137{
138 cputime_t ct;
139 unsigned long sec;
140
141 /* have to be a little careful about overflow */
Michael Holzheud57af9b2010-10-27 15:34:45 -0700142 ct = us % 1000000;
143 sec = us / 1000000;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100144 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
Andreas Schwab34845632011-12-28 15:57:15 -0800153#define usecs_to_cputime64(us) usecs_to_cputime(us)
154
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100155/*
156 * Convert cputime <-> seconds
157 */
158extern u64 __cputime_sec_factor;
159
160static inline unsigned long cputime_to_secs(const cputime_t ct)
161{
162 return mulhdu(ct, __cputime_sec_factor);
163}
164
165static inline cputime_t secs_to_cputime(const unsigned long sec)
166{
167 return (cputime_t) sec * tb_ticks_per_sec;
168}
169
170/*
171 * Convert cputime <-> timespec
172 */
173static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p)
174{
175 u64 x = ct;
176 unsigned int frac;
177
178 frac = do_div(x, tb_ticks_per_sec);
179 p->tv_sec = x;
180 x = (u64) frac * 1000000000;
181 do_div(x, tb_ticks_per_sec);
182 p->tv_nsec = x;
183}
184
185static inline cputime_t timespec_to_cputime(const struct timespec *p)
186{
187 cputime_t ct;
188
189 ct = (u64) p->tv_nsec * tb_ticks_per_sec;
190 do_div(ct, 1000000000);
191 return ct + (u64) p->tv_sec * tb_ticks_per_sec;
192}
193
194/*
195 * Convert cputime <-> timeval
196 */
197static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p)
198{
199 u64 x = ct;
200 unsigned int frac;
201
202 frac = do_div(x, tb_ticks_per_sec);
203 p->tv_sec = x;
204 x = (u64) frac * 1000000;
205 do_div(x, tb_ticks_per_sec);
206 p->tv_usec = x;
207}
208
209static inline cputime_t timeval_to_cputime(const struct timeval *p)
210{
211 cputime_t ct;
212
213 ct = (u64) p->tv_usec * tb_ticks_per_sec;
214 do_div(ct, 1000000);
215 return ct + (u64) p->tv_sec * tb_ticks_per_sec;
216}
217
218/*
219 * Convert cputime <-> clock_t (units of 1/USER_HZ seconds)
220 */
221extern u64 __cputime_clockt_factor;
222
223static inline unsigned long cputime_to_clock_t(const cputime_t ct)
224{
225 return mulhdu(ct, __cputime_clockt_factor);
226}
227
228static inline cputime_t clock_t_to_cputime(const unsigned long clk)
229{
230 cputime_t ct;
231 unsigned long sec;
232
233 /* have to be a little careful about overflow */
234 ct = clk % USER_HZ;
235 sec = clk / USER_HZ;
236 if (ct) {
237 ct *= tb_ticks_per_sec;
238 do_div(ct, USER_HZ);
239 }
240 if (sec)
241 ct += (cputime_t) sec * tb_ticks_per_sec;
242 return ct;
243}
244
245#define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct))
246
247#endif /* __KERNEL__ */
248#endif /* CONFIG_VIRT_CPU_ACCOUNTING */
249#endif /* __POWERPC_CPUTIME_H */