1
2
3
4
5
6
7
8
9
10#include "qemu/osdep.h"
11#include "cpu.h"
12#include "trace-root.h"
13#include "trace/control.h"
14#include "translate-all.h"
15
16
17void trace_event_set_state_dynamic_init(TraceEvent *ev, bool state)
18{
19 bool state_pre;
20 assert(trace_event_get_state_static(ev));
21
22
23
24
25 state_pre = *ev->dstate;
26 if (state_pre != state) {
27 if (state) {
28 trace_events_enabled_count++;
29 *ev->dstate = 1;
30 } else {
31 trace_events_enabled_count--;
32 *ev->dstate = 0;
33 }
34 }
35}
36
37void trace_event_set_state_dynamic(TraceEvent *ev, bool state)
38{
39 CPUState *vcpu;
40 assert(trace_event_get_state_static(ev));
41 if (trace_event_is_vcpu(ev) && likely(first_cpu != NULL)) {
42 CPU_FOREACH(vcpu) {
43 trace_event_set_vcpu_state_dynamic(vcpu, ev, state);
44 }
45 } else {
46
47
48
49
50
51 bool state_pre = *ev->dstate;
52 if (state_pre != state) {
53 if (state) {
54 trace_events_enabled_count++;
55 *ev->dstate = 1;
56 } else {
57 trace_events_enabled_count--;
58 *ev->dstate = 0;
59 }
60 }
61 }
62}
63
64static void trace_event_synchronize_vcpu_state_dynamic(
65 CPUState *vcpu, run_on_cpu_data ignored)
66{
67 bitmap_copy(vcpu->trace_dstate, vcpu->trace_dstate_delayed,
68 CPU_TRACE_DSTATE_MAX_EVENTS);
69 cpu_tb_jmp_cache_clear(vcpu);
70}
71
72void trace_event_set_vcpu_state_dynamic(CPUState *vcpu,
73 TraceEvent *ev, bool state)
74{
75 uint32_t vcpu_id;
76 bool state_pre;
77 assert(trace_event_get_state_static(ev));
78 assert(trace_event_is_vcpu(ev));
79 vcpu_id = trace_event_get_vcpu_id(ev);
80 state_pre = test_bit(vcpu_id, vcpu->trace_dstate);
81 if (state_pre != state) {
82 if (state) {
83 trace_events_enabled_count++;
84 set_bit(vcpu_id, vcpu->trace_dstate_delayed);
85 (*ev->dstate)++;
86 } else {
87 trace_events_enabled_count--;
88 clear_bit(vcpu_id, vcpu->trace_dstate_delayed);
89 (*ev->dstate)--;
90 }
91 if (vcpu->created) {
92
93
94
95
96
97 async_run_on_cpu(vcpu, trace_event_synchronize_vcpu_state_dynamic,
98 RUN_ON_CPU_NULL);
99 } else {
100 trace_event_synchronize_vcpu_state_dynamic(vcpu, RUN_ON_CPU_NULL);
101 }
102 }
103}
104
105static bool adding_first_cpu1(void)
106{
107 CPUState *cpu;
108 size_t count = 0;
109 CPU_FOREACH(cpu) {
110 count++;
111 if (count > 1) {
112 return false;
113 }
114 }
115 return true;
116}
117
118static bool adding_first_cpu(void)
119{
120 bool res;
121 cpu_list_lock();
122 res = adding_first_cpu1();
123 cpu_list_unlock();
124 return res;
125}
126
127void trace_init_vcpu(CPUState *vcpu)
128{
129 TraceEventIter iter;
130 TraceEvent *ev;
131 trace_event_iter_init(&iter, NULL);
132 while ((ev = trace_event_iter_next(&iter)) != NULL) {
133 if (trace_event_is_vcpu(ev) &&
134 trace_event_get_state_static(ev) &&
135 trace_event_get_state_dynamic(ev)) {
136 if (adding_first_cpu()) {
137
138 assert(*ev->dstate == 1);
139
140 *ev->dstate = 0;
141 trace_events_enabled_count--;
142
143 trace_event_set_vcpu_state_dynamic(vcpu, ev, true);
144 } else {
145 trace_event_set_vcpu_state_dynamic(vcpu, ev, true);
146 }
147 }
148 }
149 trace_guest_cpu_enter(vcpu);
150}
151