Path: blob/master/arch/powerpc/include/asm/cputime.h
15117 views
/*1* Definitions for measuring cputime on powerpc machines.2*3* Copyright (C) 2006 Paul Mackerras, IBM Corp.4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of the GNU General Public License7* as published by the Free Software Foundation; either version8* 2 of the License, or (at your option) any later version.9*10* If we have CONFIG_VIRT_CPU_ACCOUNTING, we measure cpu time in11* the same units as the timebase. Otherwise we measure cpu time12* in jiffies using the generic definitions.13*/1415#ifndef __POWERPC_CPUTIME_H16#define __POWERPC_CPUTIME_H1718#ifndef CONFIG_VIRT_CPU_ACCOUNTING19#include <asm-generic/cputime.h>20#ifdef __KERNEL__21static inline void setup_cputime_one_jiffy(void) { }22#endif23#else2425#include <linux/types.h>26#include <linux/time.h>27#include <asm/div64.h>28#include <asm/time.h>29#include <asm/param.h>3031typedef u64 cputime_t;32typedef u64 cputime64_t;3334#define cputime_zero ((cputime_t)0)35#define cputime_max ((~((cputime_t)0) >> 1) - 1)36#define cputime_add(__a, __b) ((__a) + (__b))37#define cputime_sub(__a, __b) ((__a) - (__b))38#define cputime_div(__a, __n) ((__a) / (__n))39#define cputime_halve(__a) ((__a) >> 1)40#define cputime_eq(__a, __b) ((__a) == (__b))41#define cputime_gt(__a, __b) ((__a) > (__b))42#define cputime_ge(__a, __b) ((__a) >= (__b))43#define cputime_lt(__a, __b) ((__a) < (__b))44#define cputime_le(__a, __b) ((__a) <= (__b))4546#define cputime64_zero ((cputime64_t)0)47#define cputime64_add(__a, __b) ((__a) + (__b))48#define cputime64_sub(__a, __b) ((__a) - (__b))49#define cputime_to_cputime64(__ct) (__ct)5051#ifdef __KERNEL__5253/*54* One jiffy in timebase units computed during initialization55*/56extern cputime_t cputime_one_jiffy;5758/*59* Convert cputime <-> jiffies60*/61extern u64 __cputime_jiffies_factor;62DECLARE_PER_CPU(unsigned long, cputime_last_delta);63DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta);6465static inline unsigned long cputime_to_jiffies(const cputime_t ct)66{67return mulhdu(ct, __cputime_jiffies_factor);68}6970/* Estimate the scaled cputime by scaling the real cputime based on71* the last scaled to real ratio */72static inline cputime_t cputime_to_scaled(const cputime_t ct)73{74if (cpu_has_feature(CPU_FTR_SPURR) &&75__get_cpu_var(cputime_last_delta))76return ct * __get_cpu_var(cputime_scaled_last_delta) /77__get_cpu_var(cputime_last_delta);78return ct;79}8081static inline cputime_t jiffies_to_cputime(const unsigned long jif)82{83cputime_t ct;84unsigned long sec;8586/* have to be a little careful about overflow */87ct = jif % HZ;88sec = jif / HZ;89if (ct) {90ct *= tb_ticks_per_sec;91do_div(ct, HZ);92}93if (sec)94ct += (cputime_t) sec * tb_ticks_per_sec;95return ct;96}9798static inline void setup_cputime_one_jiffy(void)99{100cputime_one_jiffy = jiffies_to_cputime(1);101}102103static inline cputime64_t jiffies64_to_cputime64(const u64 jif)104{105cputime_t ct;106u64 sec;107108/* have to be a little careful about overflow */109ct = jif % HZ;110sec = jif / HZ;111if (ct) {112ct *= tb_ticks_per_sec;113do_div(ct, HZ);114}115if (sec)116ct += (cputime_t) sec * tb_ticks_per_sec;117return ct;118}119120static inline u64 cputime64_to_jiffies64(const cputime_t ct)121{122return mulhdu(ct, __cputime_jiffies_factor);123}124125/*126* Convert cputime <-> microseconds127*/128extern u64 __cputime_msec_factor;129130static inline unsigned long cputime_to_usecs(const cputime_t ct)131{132return mulhdu(ct, __cputime_msec_factor) * USEC_PER_MSEC;133}134135static inline cputime_t usecs_to_cputime(const unsigned long us)136{137cputime_t ct;138unsigned long sec;139140/* have to be a little careful about overflow */141ct = us % 1000000;142sec = us / 1000000;143if (ct) {144ct *= tb_ticks_per_sec;145do_div(ct, 1000);146}147if (sec)148ct += (cputime_t) sec * tb_ticks_per_sec;149return ct;150}151152/*153* Convert cputime <-> seconds154*/155extern u64 __cputime_sec_factor;156157static inline unsigned long cputime_to_secs(const cputime_t ct)158{159return mulhdu(ct, __cputime_sec_factor);160}161162static inline cputime_t secs_to_cputime(const unsigned long sec)163{164return (cputime_t) sec * tb_ticks_per_sec;165}166167/*168* Convert cputime <-> timespec169*/170static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p)171{172u64 x = ct;173unsigned int frac;174175frac = do_div(x, tb_ticks_per_sec);176p->tv_sec = x;177x = (u64) frac * 1000000000;178do_div(x, tb_ticks_per_sec);179p->tv_nsec = x;180}181182static inline cputime_t timespec_to_cputime(const struct timespec *p)183{184cputime_t ct;185186ct = (u64) p->tv_nsec * tb_ticks_per_sec;187do_div(ct, 1000000000);188return ct + (u64) p->tv_sec * tb_ticks_per_sec;189}190191/*192* Convert cputime <-> timeval193*/194static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p)195{196u64 x = ct;197unsigned int frac;198199frac = do_div(x, tb_ticks_per_sec);200p->tv_sec = x;201x = (u64) frac * 1000000;202do_div(x, tb_ticks_per_sec);203p->tv_usec = x;204}205206static inline cputime_t timeval_to_cputime(const struct timeval *p)207{208cputime_t ct;209210ct = (u64) p->tv_usec * tb_ticks_per_sec;211do_div(ct, 1000000);212return ct + (u64) p->tv_sec * tb_ticks_per_sec;213}214215/*216* Convert cputime <-> clock_t (units of 1/USER_HZ seconds)217*/218extern u64 __cputime_clockt_factor;219220static inline unsigned long cputime_to_clock_t(const cputime_t ct)221{222return mulhdu(ct, __cputime_clockt_factor);223}224225static inline cputime_t clock_t_to_cputime(const unsigned long clk)226{227cputime_t ct;228unsigned long sec;229230/* have to be a little careful about overflow */231ct = clk % USER_HZ;232sec = clk / USER_HZ;233if (ct) {234ct *= tb_ticks_per_sec;235do_div(ct, USER_HZ);236}237if (sec)238ct += (cputime_t) sec * tb_ticks_per_sec;239return ct;240}241242#define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct))243244#endif /* __KERNEL__ */245#endif /* CONFIG_VIRT_CPU_ACCOUNTING */246#endif /* __POWERPC_CPUTIME_H */247248249