blob: e94935c520197d2d13af1eff0e8b149d4ad9b274 [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
Martin Schwidefsky64861632011-12-15 14:56:09 +010032typedef u64 __nocast cputime_t;
33typedef u64 __nocast cputime64_t;
Paul Mackerrasc6622f62006-02-24 10:06:59 +110034
35#ifdef __KERNEL__
36
37/*
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +020038 * One jiffy in timebase units computed during initialization
39 */
40extern cputime_t cputime_one_jiffy;
41
42/*
Paul Mackerrasc6622f62006-02-24 10:06:59 +110043 * Convert cputime <-> jiffies
44 */
45extern u64 __cputime_jiffies_factor;
Michael Neuling06b8e872008-02-06 01:36:12 -080046DECLARE_PER_CPU(unsigned long, cputime_last_delta);
47DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta);
Paul Mackerrasc6622f62006-02-24 10:06:59 +110048
49static inline unsigned long cputime_to_jiffies(const cputime_t ct)
50{
Martin Schwidefsky64861632011-12-15 14:56:09 +010051 return mulhdu((__force u64) ct, __cputime_jiffies_factor);
Paul Mackerrasc6622f62006-02-24 10:06:59 +110052}
53
Michael Neuling06b8e872008-02-06 01:36:12 -080054/* Estimate the scaled cputime by scaling the real cputime based on
55 * the last scaled to real ratio */
56static inline cputime_t cputime_to_scaled(const cputime_t ct)
57{
58 if (cpu_has_feature(CPU_FTR_SPURR) &&
Anton Blanchard61c03dd2010-01-13 12:04:11 +000059 __get_cpu_var(cputime_last_delta))
Martin Schwidefsky64861632011-12-15 14:56:09 +010060 return (__force u64) ct *
61 __get_cpu_var(cputime_scaled_last_delta) /
62 __get_cpu_var(cputime_last_delta);
Michael Neuling06b8e872008-02-06 01:36:12 -080063 return ct;
64}
65
Paul Mackerrasc6622f62006-02-24 10:06:59 +110066static inline cputime_t jiffies_to_cputime(const unsigned long jif)
67{
Martin Schwidefsky64861632011-12-15 14:56:09 +010068 u64 ct;
Paul Mackerrasc6622f62006-02-24 10:06:59 +110069 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 Schwidefsky64861632011-12-15 14:56:09 +010080 return (__force cputime_t) ct;
Paul Mackerrasc6622f62006-02-24 10:06:59 +110081}
82
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +020083static inline void setup_cputime_one_jiffy(void)
84{
85 cputime_one_jiffy = jiffies_to_cputime(1);
86}
87
David Woodhousea8e0c512006-07-05 11:24:26 +010088static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
89{
Martin Schwidefsky64861632011-12-15 14:56:09 +010090 u64 ct;
David Woodhousea8e0c512006-07-05 11:24:26 +010091 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 Schwidefsky64861632011-12-15 14:56:09 +0100101 ct += (u64) sec * tb_ticks_per_sec;
102 return (__force cputime64_t) ct;
David Woodhousea8e0c512006-07-05 11:24:26 +0100103}
104
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100105static inline u64 cputime64_to_jiffies64(const cputime_t ct)
106{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100107 return mulhdu((__force u64) ct, __cputime_jiffies_factor);
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100108}
109
110/*
Michael Holzheud57af9b2010-10-27 15:34:45 -0700111 * Convert cputime <-> microseconds
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100112 */
113extern u64 __cputime_msec_factor;
114
Michael Holzheud57af9b2010-10-27 15:34:45 -0700115static inline unsigned long cputime_to_usecs(const cputime_t ct)
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100116{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100117 return mulhdu((__force u64) ct, __cputime_msec_factor) * USEC_PER_MSEC;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100118}
119
Michael Holzheud57af9b2010-10-27 15:34:45 -0700120static inline cputime_t usecs_to_cputime(const unsigned long us)
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100121{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100122 u64 ct;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100123 unsigned long sec;
124
125 /* have to be a little careful about overflow */
Michael Holzheud57af9b2010-10-27 15:34:45 -0700126 ct = us % 1000000;
127 sec = us / 1000000;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100128 if (ct) {
129 ct *= tb_ticks_per_sec;
130 do_div(ct, 1000);
131 }
132 if (sec)
133 ct += (cputime_t) sec * tb_ticks_per_sec;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100134 return (__force cputime_t) ct;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100135}
136
137/*
138 * Convert cputime <-> seconds
139 */
140extern u64 __cputime_sec_factor;
141
142static inline unsigned long cputime_to_secs(const cputime_t ct)
143{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100144 return mulhdu((__force u64) ct, __cputime_sec_factor);
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100145}
146
147static inline cputime_t secs_to_cputime(const unsigned long sec)
148{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100149 return (__force cputime_t)((u64) sec * tb_ticks_per_sec);
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100150}
151
152/*
153 * Convert cputime <-> timespec
154 */
155static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p)
156{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100157 u64 x = (__force u64) ct;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100158 unsigned int frac;
159
160 frac = do_div(x, tb_ticks_per_sec);
161 p->tv_sec = x;
162 x = (u64) frac * 1000000000;
163 do_div(x, tb_ticks_per_sec);
164 p->tv_nsec = x;
165}
166
167static inline cputime_t timespec_to_cputime(const struct timespec *p)
168{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100169 u64 ct;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100170
171 ct = (u64) p->tv_nsec * tb_ticks_per_sec;
172 do_div(ct, 1000000000);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100173 return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec);
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100174}
175
176/*
177 * Convert cputime <-> timeval
178 */
179static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p)
180{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100181 u64 x = (__force u64) ct;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100182 unsigned int frac;
183
184 frac = do_div(x, tb_ticks_per_sec);
185 p->tv_sec = x;
186 x = (u64) frac * 1000000;
187 do_div(x, tb_ticks_per_sec);
188 p->tv_usec = x;
189}
190
191static inline cputime_t timeval_to_cputime(const struct timeval *p)
192{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100193 u64 ct;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100194
195 ct = (u64) p->tv_usec * tb_ticks_per_sec;
196 do_div(ct, 1000000);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100197 return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec);
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100198}
199
200/*
201 * Convert cputime <-> clock_t (units of 1/USER_HZ seconds)
202 */
203extern u64 __cputime_clockt_factor;
204
205static inline unsigned long cputime_to_clock_t(const cputime_t ct)
206{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100207 return mulhdu((__force u64) ct, __cputime_clockt_factor);
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100208}
209
210static inline cputime_t clock_t_to_cputime(const unsigned long clk)
211{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100212 u64 ct;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100213 unsigned long sec;
214
215 /* have to be a little careful about overflow */
216 ct = clk % USER_HZ;
217 sec = clk / USER_HZ;
218 if (ct) {
219 ct *= tb_ticks_per_sec;
220 do_div(ct, USER_HZ);
221 }
222 if (sec)
Martin Schwidefsky64861632011-12-15 14:56:09 +0100223 ct += (u64) sec * tb_ticks_per_sec;
224 return (__force cputime_t) ct;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100225}
226
227#define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct))
228
229#endif /* __KERNEL__ */
230#endif /* CONFIG_VIRT_CPU_ACCOUNTING */
231#endif /* __POWERPC_CPUTIME_H */