cputime.h 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Definitions for measuring cputime on powerpc machines.
 *
 * Copyright (C) 2006 Paul Mackerras, IBM Corp.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 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 13 14 15 16 17 18
 * the same units as the timebase.  Otherwise we measure cpu time
 * in jiffies using the generic definitions.
 */

#ifndef __POWERPC_CPUTIME_H
#define __POWERPC_CPUTIME_H

19
#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
20
#include <asm-generic/cputime.h>
21 22 23
#ifdef __KERNEL__
static inline void setup_cputime_one_jiffy(void) { }
#endif
24 25 26 27 28 29 30 31
#else

#include <linux/types.h>
#include <linux/time.h>
#include <asm/div64.h>
#include <asm/time.h>
#include <asm/param.h>

32 33
typedef u64 __nocast cputime_t;
typedef u64 __nocast cputime64_t;
34 35 36

#ifdef __KERNEL__

37 38 39 40 41
/*
 * One jiffy in timebase units computed during initialization
 */
extern cputime_t cputime_one_jiffy;

42 43 44 45
/*
 * Convert cputime <-> jiffies
 */
extern u64 __cputime_jiffies_factor;
M
Michael Neuling 已提交
46 47
DECLARE_PER_CPU(unsigned long, cputime_last_delta);
DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta);
48 49 50

static inline unsigned long cputime_to_jiffies(const cputime_t ct)
{
51
	return mulhdu((__force u64) ct, __cputime_jiffies_factor);
52 53
}

M
Michael Neuling 已提交
54 55 56 57 58
/* Estimate the scaled cputime by scaling the real cputime based on
 * the last scaled to real ratio */
static inline cputime_t cputime_to_scaled(const cputime_t ct)
{
	if (cpu_has_feature(CPU_FTR_SPURR) &&
59
	    __get_cpu_var(cputime_last_delta))
60 61 62
		return (__force u64) ct *
			__get_cpu_var(cputime_scaled_last_delta) /
			__get_cpu_var(cputime_last_delta);
M
Michael Neuling 已提交
63 64 65
	return ct;
}

66 67
static inline cputime_t jiffies_to_cputime(const unsigned long jif)
{
68
	u64 ct;
69 70 71 72 73 74 75 76 77 78 79
	unsigned long sec;

	/* have to be a little careful about overflow */
	ct = jif % HZ;
	sec = jif / HZ;
	if (ct) {
		ct *= tb_ticks_per_sec;
		do_div(ct, HZ);
	}
	if (sec)
		ct += (cputime_t) sec * tb_ticks_per_sec;
80
	return (__force cputime_t) ct;
81 82
}

83 84 85 86 87
static inline void setup_cputime_one_jiffy(void)
{
	cputime_one_jiffy = jiffies_to_cputime(1);
}

88 89
static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
{
90
	u64 ct;
91 92 93 94 95 96 97 98 99 100
	u64 sec;

	/* have to be a little careful about overflow */
	ct = jif % HZ;
	sec = jif / HZ;
	if (ct) {
		ct *= tb_ticks_per_sec;
		do_div(ct, HZ);
	}
	if (sec)
101 102
		ct += (u64) sec * tb_ticks_per_sec;
	return (__force cputime64_t) ct;
103 104
}

105 106
static inline u64 cputime64_to_jiffies64(const cputime_t ct)
{
107
	return mulhdu((__force u64) ct, __cputime_jiffies_factor);
108 109 110
}

/*
111
 * Convert cputime <-> microseconds
112
 */
113
extern u64 __cputime_usec_factor;
114

115
static inline unsigned long cputime_to_usecs(const cputime_t ct)
116
{
117
	return mulhdu((__force u64) ct, __cputime_usec_factor);
118 119
}

120
static inline cputime_t usecs_to_cputime(const unsigned long us)
121
{
122
	u64 ct;
123 124 125
	unsigned long sec;

	/* have to be a little careful about overflow */
126 127
	ct = us % 1000000;
	sec = us / 1000000;
128 129
	if (ct) {
		ct *= tb_ticks_per_sec;
130
		do_div(ct, 1000000);
131 132 133
	}
	if (sec)
		ct += (cputime_t) sec * tb_ticks_per_sec;
134
	return (__force cputime_t) ct;
135 136
}

137 138
#define usecs_to_cputime64(us)		usecs_to_cputime(us)

139 140 141 142 143 144 145
/*
 * Convert cputime <-> seconds
 */
extern u64 __cputime_sec_factor;

static inline unsigned long cputime_to_secs(const cputime_t ct)
{
146
	return mulhdu((__force u64) ct, __cputime_sec_factor);
147 148 149 150
}

static inline cputime_t secs_to_cputime(const unsigned long sec)
{
151
	return (__force cputime_t)((u64) sec * tb_ticks_per_sec);
152 153 154 155 156 157 158
}

/*
 * Convert cputime <-> timespec
 */
static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p)
{
159
	u64 x = (__force u64) ct;
160 161 162 163 164 165 166 167 168 169 170
	unsigned int frac;

	frac = do_div(x, tb_ticks_per_sec);
	p->tv_sec = x;
	x = (u64) frac * 1000000000;
	do_div(x, tb_ticks_per_sec);
	p->tv_nsec = x;
}

static inline cputime_t timespec_to_cputime(const struct timespec *p)
{
171
	u64 ct;
172 173 174

	ct = (u64) p->tv_nsec * tb_ticks_per_sec;
	do_div(ct, 1000000000);
175
	return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec);
176 177 178 179 180 181 182
}

/*
 * Convert cputime <-> timeval
 */
static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p)
{
183
	u64 x = (__force u64) ct;
184 185 186 187 188 189 190 191 192 193 194
	unsigned int frac;

	frac = do_div(x, tb_ticks_per_sec);
	p->tv_sec = x;
	x = (u64) frac * 1000000;
	do_div(x, tb_ticks_per_sec);
	p->tv_usec = x;
}

static inline cputime_t timeval_to_cputime(const struct timeval *p)
{
195
	u64 ct;
196 197 198

	ct = (u64) p->tv_usec * tb_ticks_per_sec;
	do_div(ct, 1000000);
199
	return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec);
200 201 202 203 204 205 206 207 208
}

/*
 * Convert cputime <-> clock_t (units of 1/USER_HZ seconds)
 */
extern u64 __cputime_clockt_factor;

static inline unsigned long cputime_to_clock_t(const cputime_t ct)
{
209
	return mulhdu((__force u64) ct, __cputime_clockt_factor);
210 211 212 213
}

static inline cputime_t clock_t_to_cputime(const unsigned long clk)
{
214
	u64 ct;
215 216 217 218 219 220 221 222 223 224
	unsigned long sec;

	/* have to be a little careful about overflow */
	ct = clk % USER_HZ;
	sec = clk / USER_HZ;
	if (ct) {
		ct *= tb_ticks_per_sec;
		do_div(ct, USER_HZ);
	}
	if (sec)
225 226
		ct += (u64) sec * tb_ticks_per_sec;
	return (__force cputime_t) ct;
227 228 229 230
}

#define cputime64_to_clock_t(ct)	cputime_to_clock_t((cputime_t)(ct))

231 232
static inline void arch_vtime_task_switch(struct task_struct *tsk) { }

233
#endif /* __KERNEL__ */
234
#endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
235
#endif /* __POWERPC_CPUTIME_H */