blob: 4e3b18e559b11270c44e669c6a40ce0b566d8ccb [file] [log] [blame]
Frederic Weisbecker39613762013-01-15 13:25:07 +01001/*
2 * Definitions for measuring cputime in nsecs resolution.
3 *
4 * Based on <arch/ia64/include/asm/cputime.h>
5 *
6 * Copyright (C) 2007 FUJITSU LIMITED
7 * Copyright (C) 2007 Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
16#ifndef _ASM_GENERIC_CPUTIME_NSECS_H
17#define _ASM_GENERIC_CPUTIME_NSECS_H
18
Kevin Hilman8c23b802013-03-20 16:34:25 -070019#include <linux/math64.h>
20
Frederic Weisbecker39613762013-01-15 13:25:07 +010021typedef u64 __nocast cputime_t;
22typedef u64 __nocast cputime64_t;
23
Rik van Riel347abad2014-09-30 15:59:47 -040024#define cmpxchg_cputime(ptr, old, new) cmpxchg64(ptr, old, new)
25
Frederic Weisbecker39613762013-01-15 13:25:07 +010026#define cputime_one_jiffy jiffies_to_cputime(1)
27
Kevin Hilman8c23b802013-03-20 16:34:25 -070028#define cputime_div(__ct, divisor) div_u64((__force u64)__ct, divisor)
29#define cputime_div_rem(__ct, divisor, remainder) \
30 div_u64_rem((__force u64)__ct, divisor, remainder);
31
Frederic Weisbecker39613762013-01-15 13:25:07 +010032/*
33 * Convert cputime <-> jiffies (HZ)
34 */
35#define cputime_to_jiffies(__ct) \
Kevin Hilman8c23b802013-03-20 16:34:25 -070036 cputime_div(__ct, NSEC_PER_SEC / HZ)
Frederic Weisbecker39613762013-01-15 13:25:07 +010037#define jiffies_to_cputime(__jif) \
38 (__force cputime_t)((__jif) * (NSEC_PER_SEC / HZ))
39#define cputime64_to_jiffies64(__ct) \
Kevin Hilman8c23b802013-03-20 16:34:25 -070040 cputime_div(__ct, NSEC_PER_SEC / HZ)
Frederic Weisbecker39613762013-01-15 13:25:07 +010041#define jiffies64_to_cputime64(__jif) \
42 (__force cputime64_t)((__jif) * (NSEC_PER_SEC / HZ))
43
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +020044
45/*
46 * Convert cputime <-> nanoseconds
47 */
Frederic Weisbeckerd8a9ce32014-03-05 16:22:37 +010048#define cputime_to_nsecs(__ct) \
49 (__force u64)(__ct)
Frederic Weisbecker69bb2602014-03-05 15:33:18 +010050#define nsecs_to_cputime(__nsecs) \
51 (__force cputime_t)(__nsecs)
Rik van Riel57430212016-07-13 16:50:01 +020052#define nsecs_to_cputime64(__nsecs) \
53 (__force cputime64_t)(__nsecs)
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +020054
55
Frederic Weisbecker39613762013-01-15 13:25:07 +010056/*
57 * Convert cputime <-> microseconds
58 */
59#define cputime_to_usecs(__ct) \
Kevin Hilman8c23b802013-03-20 16:34:25 -070060 cputime_div(__ct, NSEC_PER_USEC)
Frederic Weisbecker39613762013-01-15 13:25:07 +010061#define usecs_to_cputime(__usecs) \
62 (__force cputime_t)((__usecs) * NSEC_PER_USEC)
63#define usecs_to_cputime64(__usecs) \
64 (__force cputime64_t)((__usecs) * NSEC_PER_USEC)
65
66/*
67 * Convert cputime <-> seconds
68 */
69#define cputime_to_secs(__ct) \
Kevin Hilman8c23b802013-03-20 16:34:25 -070070 cputime_div(__ct, NSEC_PER_SEC)
Frederic Weisbecker39613762013-01-15 13:25:07 +010071#define secs_to_cputime(__secs) \
72 (__force cputime_t)((__secs) * NSEC_PER_SEC)
73
74/*
75 * Convert cputime <-> timespec (nsec)
76 */
77static inline cputime_t timespec_to_cputime(const struct timespec *val)
78{
zengtao0f269222016-02-02 11:38:34 +080079 u64 ret = (u64)val->tv_sec * NSEC_PER_SEC + val->tv_nsec;
Frederic Weisbecker39613762013-01-15 13:25:07 +010080 return (__force cputime_t) ret;
81}
82static inline void cputime_to_timespec(const cputime_t ct, struct timespec *val)
83{
Kevin Hilman8c23b802013-03-20 16:34:25 -070084 u32 rem;
85
86 val->tv_sec = cputime_div_rem(ct, NSEC_PER_SEC, &rem);
87 val->tv_nsec = rem;
Frederic Weisbecker39613762013-01-15 13:25:07 +010088}
89
90/*
91 * Convert cputime <-> timeval (msec)
92 */
Li Zhongc78a4bc2013-02-23 17:28:44 +010093static inline cputime_t timeval_to_cputime(const struct timeval *val)
Frederic Weisbecker39613762013-01-15 13:25:07 +010094{
zengtao0f269222016-02-02 11:38:34 +080095 u64 ret = (u64)val->tv_sec * NSEC_PER_SEC +
96 val->tv_usec * NSEC_PER_USEC;
Frederic Weisbecker39613762013-01-15 13:25:07 +010097 return (__force cputime_t) ret;
98}
99static inline void cputime_to_timeval(const cputime_t ct, struct timeval *val)
100{
Kevin Hilman8c23b802013-03-20 16:34:25 -0700101 u32 rem;
102
103 val->tv_sec = cputime_div_rem(ct, NSEC_PER_SEC, &rem);
104 val->tv_usec = rem / NSEC_PER_USEC;
Frederic Weisbecker39613762013-01-15 13:25:07 +0100105}
106
107/*
108 * Convert cputime <-> clock (USER_HZ)
109 */
110#define cputime_to_clock_t(__ct) \
Kevin Hilman8c23b802013-03-20 16:34:25 -0700111 cputime_div(__ct, (NSEC_PER_SEC / USER_HZ))
Frederic Weisbecker39613762013-01-15 13:25:07 +0100112#define clock_t_to_cputime(__x) \
113 (__force cputime_t)((__x) * (NSEC_PER_SEC / USER_HZ))
114
115/*
116 * Convert cputime64 to clock.
117 */
118#define cputime64_to_clock_t(__ct) \
119 cputime_to_clock_t((__force cputime_t)__ct)
120
121#endif