1 /******************************************************************************
4 * Copyright (c) 2008 Isaku Yamahata <yamahata at valinux co jp>
5 * VA Linux Systems Japan K.K.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/delay.h>
24 #include <linux/kernel_stat.h>
25 #include <linux/posix-timers.h>
26 #include <linux/irq.h>
27 #include <linux/clocksource.h>
29 #include <asm/xen/hypervisor.h>
31 #include <xen/interface/vcpu.h>
33 #include "../kernel/fsyscall_gtod_data.h"
35 DEFINE_PER_CPU(struct vcpu_runstate_info
, runstate
);
36 DEFINE_PER_CPU(unsigned long, processed_stolen_time
);
37 DEFINE_PER_CPU(unsigned long, processed_blocked_time
);
39 /* taken from i386/kernel/time-xen.c */
40 static void xen_init_missing_ticks_accounting(int cpu
)
42 struct vcpu_register_runstate_memory_area area
;
43 struct vcpu_runstate_info
*runstate
= &per_cpu(runstate
, cpu
);
46 memset(runstate
, 0, sizeof(*runstate
));
48 area
.addr
.v
= runstate
;
49 rc
= HYPERVISOR_vcpu_op(VCPUOP_register_runstate_memory_area
, cpu
,
51 WARN_ON(rc
&& rc
!= -ENOSYS
);
53 per_cpu(processed_blocked_time
, cpu
) = runstate
->time
[RUNSTATE_blocked
];
54 per_cpu(processed_stolen_time
, cpu
) = runstate
->time
[RUNSTATE_runnable
]
55 + runstate
->time
[RUNSTATE_offline
];
61 /* stolen from arch/x86/xen/time.c */
62 static void get_runstate_snapshot(struct vcpu_runstate_info
*res
)
65 struct vcpu_runstate_info
*state
;
67 BUG_ON(preemptible());
69 state
= &__get_cpu_var(runstate
);
72 * The runstate info is always updated by the hypervisor on
73 * the current CPU, so there's no need to use anything
74 * stronger than a compiler barrier when fetching it.
77 state_time
= state
->state_entry_time
;
81 } while (state
->state_entry_time
!= state_time
);
84 #define NS_PER_TICK (1000000000LL/HZ)
87 consider_steal_time(unsigned long new_itm
)
89 unsigned long stolen
, blocked
;
90 unsigned long delta_itm
= 0, stolentick
= 0;
91 int cpu
= smp_processor_id();
92 struct vcpu_runstate_info runstate
;
93 struct task_struct
*p
= current
;
95 get_runstate_snapshot(&runstate
);
98 * Check for vcpu migration effect
99 * In this case, itc value is reversed.
100 * This causes huge stolen value.
101 * This function just checks and reject this effect.
103 if (!time_after_eq(runstate
.time
[RUNSTATE_blocked
],
104 per_cpu(processed_blocked_time
, cpu
)))
107 if (!time_after_eq(runstate
.time
[RUNSTATE_runnable
] +
108 runstate
.time
[RUNSTATE_offline
],
109 per_cpu(processed_stolen_time
, cpu
)))
112 if (!time_after(delta_itm
+ new_itm
, ia64_get_itc()))
113 stolentick
= ia64_get_itc() - new_itm
;
115 do_div(stolentick
, NS_PER_TICK
);
118 do_div(stolen
, NS_PER_TICK
);
120 if (stolen
> stolentick
)
123 stolentick
-= stolen
;
124 do_div(blocked
, NS_PER_TICK
);
126 if (blocked
> stolentick
)
127 blocked
= stolentick
;
129 if (stolen
> 0 || blocked
> 0) {
130 account_steal_time(NULL
, jiffies_to_cputime(stolen
));
131 account_steal_time(idle_task(cpu
), jiffies_to_cputime(blocked
));
134 if (rcu_pending(cpu
))
135 rcu_check_callbacks(cpu
, user_mode(get_irq_regs()));
138 run_posix_cpu_timers(p
);
139 delta_itm
+= local_cpu_data
->itm_delta
* (stolen
+ blocked
);
141 if (cpu
== time_keeper_id
) {
142 write_seqlock(&xtime_lock
);
143 do_timer(stolen
+ blocked
);
144 local_cpu_data
->itm_next
= delta_itm
+ new_itm
;
145 write_sequnlock(&xtime_lock
);
147 local_cpu_data
->itm_next
= delta_itm
+ new_itm
;
149 per_cpu(processed_stolen_time
, cpu
) += NS_PER_TICK
* stolen
;
150 per_cpu(processed_blocked_time
, cpu
) += NS_PER_TICK
* blocked
;
155 static int xen_do_steal_accounting(unsigned long *new_itm
)
157 unsigned long delta_itm
;
158 delta_itm
= consider_steal_time(*new_itm
);
159 *new_itm
+= delta_itm
;
160 if (time_after(*new_itm
, ia64_get_itc()) && delta_itm
)
166 static void xen_itc_jitter_data_reset(void)
171 lcycle
= itc_jitter_data
.itc_lastcycle
;
172 ret
= cmpxchg(&itc_jitter_data
.itc_lastcycle
, lcycle
, 0);
173 } while (unlikely(ret
!= lcycle
));
176 struct pv_time_ops xen_time_ops __initdata
= {
177 .init_missing_ticks_accounting
= xen_init_missing_ticks_accounting
,
178 .do_steal_accounting
= xen_do_steal_accounting
,
179 .clocksource_resume
= xen_itc_jitter_data_reset
,