xref: /linux-6.15/include/linux/vtime.h (revision 0cfdf9a1)
1 #ifndef _LINUX_KERNEL_VTIME_H
2 #define _LINUX_KERNEL_VTIME_H
3 
4 #include <linux/context_tracking_state.h>
5 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
6 #include <asm/vtime.h>
7 #endif
8 
9 
10 struct task_struct;
11 
12 /*
13  * vtime_accounting_cpu_enabled() definitions/declarations
14  */
15 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
16 static inline bool vtime_accounting_cpu_enabled(void) { return true; }
17 extern void vtime_account_irq_enter(struct task_struct *tsk);
18 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
19 
20 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
21 /*
22  * Checks if vtime is enabled on some CPU. Cputime readers want to be careful
23  * in that case and compute the tickless cputime.
24  * For now vtime state is tied to context tracking. We might want to decouple
25  * those later if necessary.
26  */
27 static inline bool vtime_accounting_enabled(void)
28 {
29 	return context_tracking_is_enabled();
30 }
31 
32 static inline bool vtime_accounting_cpu_enabled(void)
33 {
34 	if (vtime_accounting_enabled()) {
35 		if (context_tracking_cpu_is_enabled())
36 			return true;
37 	}
38 
39 	return false;
40 }
41 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
42 
43 #ifndef CONFIG_VIRT_CPU_ACCOUNTING
44 static inline bool vtime_accounting_cpu_enabled(void) { return false; }
45 #endif /* !CONFIG_VIRT_CPU_ACCOUNTING */
46 
47 
48 /*
49  * Common vtime APIs
50  */
51 #ifdef CONFIG_VIRT_CPU_ACCOUNTING
52 
53 #ifdef __ARCH_HAS_VTIME_TASK_SWITCH
54 extern void vtime_task_switch(struct task_struct *prev);
55 #else
56 extern void vtime_common_task_switch(struct task_struct *prev);
57 static inline void vtime_task_switch(struct task_struct *prev)
58 {
59 	if (vtime_accounting_cpu_enabled())
60 		vtime_common_task_switch(prev);
61 }
62 #endif /* __ARCH_HAS_VTIME_TASK_SWITCH */
63 
64 extern void vtime_account_system(struct task_struct *tsk);
65 extern void vtime_account_idle(struct task_struct *tsk);
66 extern void vtime_account_user(struct task_struct *tsk);
67 
68 #else /* !CONFIG_VIRT_CPU_ACCOUNTING */
69 
70 static inline void vtime_task_switch(struct task_struct *prev) { }
71 static inline void vtime_account_system(struct task_struct *tsk) { }
72 static inline void vtime_account_user(struct task_struct *tsk) { }
73 static inline void vtime_account_irq_enter(struct task_struct *tsk) { }
74 #endif /* !CONFIG_VIRT_CPU_ACCOUNTING */
75 
76 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
77 extern void arch_vtime_task_switch(struct task_struct *tsk);
78 static inline void vtime_account_irq_enter(struct task_struct *tsk) { }
79 static inline void vtime_account_irq_exit(struct task_struct *tsk) { }
80 
81 extern void vtime_user_enter(struct task_struct *tsk);
82 
83 static inline void vtime_user_exit(struct task_struct *tsk)
84 {
85 	vtime_account_user(tsk);
86 }
87 extern void vtime_guest_enter(struct task_struct *tsk);
88 extern void vtime_guest_exit(struct task_struct *tsk);
89 extern void vtime_init_idle(struct task_struct *tsk, int cpu);
90 #else /* !CONFIG_VIRT_CPU_ACCOUNTING_GEN  */
91 static inline void vtime_account_irq_exit(struct task_struct *tsk)
92 {
93 	/* On hard|softirq exit we always account to hard|softirq cputime */
94 	vtime_account_system(tsk);
95 }
96 static inline void vtime_user_enter(struct task_struct *tsk) { }
97 static inline void vtime_user_exit(struct task_struct *tsk) { }
98 static inline void vtime_guest_enter(struct task_struct *tsk) { }
99 static inline void vtime_guest_exit(struct task_struct *tsk) { }
100 static inline void vtime_init_idle(struct task_struct *tsk, int cpu) { }
101 #endif
102 
103 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
104 extern void irqtime_account_irq(struct task_struct *tsk);
105 #else
106 static inline void irqtime_account_irq(struct task_struct *tsk) { }
107 #endif
108 
109 static inline void account_irq_enter_time(struct task_struct *tsk)
110 {
111 	vtime_account_irq_enter(tsk);
112 	irqtime_account_irq(tsk);
113 }
114 
115 static inline void account_irq_exit_time(struct task_struct *tsk)
116 {
117 	vtime_account_irq_exit(tsk);
118 	irqtime_account_irq(tsk);
119 }
120 
121 #endif /* _LINUX_KERNEL_VTIME_H */
122