]> git.saurik.com Git - apple/system_cmds.git/blob - KDBG/MachineThread.mutable-impl.hpp
0ae018a018330a7366f0295556f1d9f9b8fcb401
[apple/system_cmds.git] / KDBG / MachineThread.mutable-impl.hpp
1 //
2 // MachineThread.mutable-impl.hpp
3 // KDBG
4 //
5 // Created by James McIlree on 10/30/12.
6 // Copyright (c) 2014 Apple. All rights reserved.
7 //
8
9 #include "KDebug.h"
10
11
12 template <typename SIZE>
13 void MachineThread<SIZE>::set_is_idle_thread() {
14 ASSERT(!is_trace_terminated(), "Attempt to mark terminated thread as IDLE");
15 ASSERT(_process->is_kernel(), "Attempt to set non-kernel thread as IDLE");
16
17 set_flags(kMachineThreadFlag::IsIdle);
18 }
19
20 template <typename SIZE>
21 void MachineThread<SIZE>::set_trace_terminated(AbsTime timestamp) {
22 ASSERT(!is_trace_terminated(), "Attempt to trace terminate thread more than once");
23 ASSERT(!is_idle_thread(), "Attempt to terminate IDLE thread");
24
25 AbsTime terminated_timestamp = timestamp + AbsTime(1);
26
27 // If we were killed with a block event pending, we need to flush it
28 // to the queue. The make_runnable call will do sanity checks to
29 // handle the corner cases when called from here.
30 make_runnable(terminated_timestamp);
31
32 // We need to set the final timestamp for this thread's last voucher.
33 // Note that the null voucher and unset voucher are actual objects,
34 // they are not represented by NULL or nullptr.
35 _vouchers_by_time.back().set_max(terminated_timestamp);
36
37 //
38 // Finally set this threads timespan
39 //
40 _timespan.set_max(terminated_timestamp);
41
42
43 set_flags(kMachineThreadFlag::TraceTerminated);
44 }
45
46 template <typename SIZE>
47 void MachineThread<SIZE>::make_runnable(AbsTime timestamp) {
48 ASSERT(!is_trace_terminated(), "Attempting to make terminated thread runnable");
49 ASSERT(timestamp >= _timespan.location(), "Attempt to make thread runnable before it exists");
50
51 if (_begin_blocked > 0) {
52 ASSERT(timestamp > _begin_blocked, "Sanity");
53 ASSERT(_blocked.empty() || _begin_blocked > _blocked.back().max(), "Out of order blocked regions");
54 _blocked.emplace_back(_begin_blocked, timestamp - _begin_blocked);
55 _begin_blocked = AbsTime(0);
56 }
57 }
58
59 template <typename SIZE>
60 void MachineThread<SIZE>::make_unrunnable(AbsTime timestamp) {
61 ASSERT(!is_trace_terminated(), "Attempting to make terminated thread unrunnable");
62 ASSERT(timestamp >= _timespan.location(), "Attempt to make thread unrunnable before it exists");
63
64 _begin_blocked = timestamp;
65 }
66
67 template <typename SIZE>
68 void MachineThread<SIZE>::begin_vm_fault(AbsTime timestamp) {
69 ASSERT(timestamp >= _timespan.location(), "Attempt to begin vm fault before thread exists");
70 ASSERT(!is_trace_terminated(), "Attempt to begin vm fault on thread that has terminated");
71 ASSERT(!is_idle_thread(), "Attempt to begin vm fault on IDLE thread");
72
73 ASSERT(_begin_vm_fault == 0, "Attempt to begin vm_fault without end");
74 _begin_vm_fault = timestamp;
75 }
76
77 template <typename SIZE>
78 void MachineThread<SIZE>::end_vm_fault(AbsTime timestamp) {
79 ASSERT(timestamp >= _timespan.location(), "Attempt to end vm fault before thread exists");
80 ASSERT(!is_trace_terminated(), "Attempt to end vm fault on thread that has terminated");
81 ASSERT(!is_idle_thread(), "Attempt to end vm fault on IDLE thread");
82
83 if (_begin_vm_fault > 0) {
84 ASSERT(timestamp > _begin_vm_fault, "Sanity");
85 ASSERT(_vm_faults.empty() || _begin_vm_fault > _vm_faults.back().max(), "Out of order vm_fault regions");
86 _vm_faults.emplace_back(_begin_vm_fault, timestamp - _begin_vm_fault);
87 _begin_vm_fault = AbsTime(0);
88 }
89 }
90
91 template <typename SIZE>
92 void MachineThread<SIZE>::begin_jetsam_activity(uint32_t type, AbsTime timestamp) {
93 ASSERT(timestamp >= _timespan.location(), "Attempt to begin jetsam activity before thread exists");
94 ASSERT(!is_trace_terminated(), "Attempt to begin jetsam activity on thread that has terminated");
95 ASSERT(!is_idle_thread(), "Attempt to begin jetsam activity on IDLE thread");
96
97 ASSERT(_begin_jetsam_activity == 0, "Attempt to begin jetsam activity without end");
98 ASSERT(_begin_jetsam_activity_type == 0, "Sanity");
99
100 _begin_jetsam_activity = timestamp;
101 DEBUG_ONLY(_begin_jetsam_activity_type = type;)
102 }
103
104 template <typename SIZE>
105 void MachineThread<SIZE>::end_jetsam_activity(uint32_t type, AbsTime timestamp) {
106 ASSERT(timestamp >= _timespan.location(), "Attempt to end jetsam activity before thread exists");
107 ASSERT(!is_trace_terminated(), "Attempt to end jetsam activity on thread that has terminated");
108 ASSERT(!is_idle_thread(), "Attempt to end jetsam activity on IDLE thread");
109
110 if (_begin_jetsam_activity > 0) {
111 ASSERT(type == _begin_jetsam_activity_type, "End event type does not match start event");
112 ASSERT(timestamp > _begin_jetsam_activity, "Sanity");
113 ASSERT(_jetsam_activity.empty() || _begin_jetsam_activity > _jetsam_activity.back().max(), "Out of order jetsam activities");
114 _jetsam_activity.emplace_back(_begin_jetsam_activity, timestamp - _begin_jetsam_activity);
115 _begin_jetsam_activity = AbsTime(0);
116 DEBUG_ONLY(_begin_jetsam_activity_type = 0;)
117 }
118 }
119
120 template <typename SIZE>
121 void MachineThread<SIZE>::set_voucher(MachineVoucher<SIZE>* voucher, AbsTime timestamp) {
122 ASSERT(timestamp >= _timespan.location(), "Attempt to set voucher on thread before thread exists");
123 ASSERT(!is_trace_terminated(), "Attempt to set voucher on terminated thread");
124 ASSERT(!is_idle_thread(), "Attempt to set voucher on IDLE thread");
125 ASSERT(!_vouchers_by_time.empty() || _vouchers_by_time.back().max() < timestamp, "Sanity");
126 ASSERT(_vouchers_by_time.back().location() < timestamp, "Sanity");
127
128 VoucherInterval<SIZE>& last_voucher = _vouchers_by_time.back();
129
130 if (voucher != last_voucher.voucher()) {
131 ASSERT(last_voucher.max() == AbsTime::END_OF_TIME, "Sanity");
132
133 //
134 // By default, the voucher interval has the last voucher continuing "forever".
135 // We need to trim the time used by that voucher, while handling the case of
136 // the very first event setting a new voucher as well.
137 //
138 // There are three cases possible.
139 //
140 // 1) timestamp > last_voucher.location // This is the expected case
141 // 2) timestamp == last_voucher.location // This should only be possible on the very first event for a thread
142 // 3) timestamp < last_voucher.location // This is an error at all times.
143 //
144
145 if (timestamp > last_voucher.location()) {
146 // Expected case (#1)
147 last_voucher.set_max(timestamp);
148 _vouchers_by_time.emplace_back(voucher, AbsInterval(timestamp, AbsTime::END_OF_TIME - timestamp));
149 } else if (timestamp == last_voucher.location()) {
150 // Corner case (#2)
151 //
152 // Note that we cannot assert that the voucher being replaced is the unset voucher,
153 // as vouchers are forwarded during "live" event handling. This means that the thread
154 // may have a valid voucher that is replaced on the first event.
155 //
156 // The timestamp == _timespan.location assert may also be too strong, if we start forwarding threads true lifetimes.
157
158 ASSERT(timestamp == _timespan.location(), "Should only be overriding a voucher on the first event for a given thread.");
159 ASSERT(_vouchers_by_time.size() == 1, "Attempt to replace the current voucher when it isn't the first voucher");
160 _vouchers_by_time.pop_back();
161 _vouchers_by_time.emplace_back(voucher, AbsInterval(timestamp, AbsTime::END_OF_TIME - timestamp));
162 } else {
163 ASSERT(false, "Attempting to set a voucher on thread earlier in time than the thread's current voucher");
164
165 }
166 }
167 }
168
169 template <typename SIZE>
170 void MachineThread<SIZE>::post_initialize(AbsTime last_machine_timestamp) {
171 if (!is_trace_terminated()) {
172 //
173 // For threads that are still alive at the post_initialize phase,
174 // we want to extend their timespan(s) to the end of the machine state,
175 // so they can be looked up by tid/timestamp
176 //
177 ASSERT(_timespan.length() == 0, "Sanity");
178
179 // Time in a range is always half open. [ 10, 11 ) means 10 is included,
180 // but 11 is not. In order to include a given timestamp, we must use
181 // a value one greater.
182 AbsTime half_open_timestamp = last_machine_timestamp + AbsTime(1);
183
184 _timespan.set_max(half_open_timestamp);
185
186 // 6/22/2014 Not sure about this. Just working on the massive time
187 // cleanup, along with the "we really know when threads and processes
188 // are done" cleanup. We used to always check and flush any outstanding
189 // blocked events in post_initialize. This is done explicitly in the trace
190 // terminated code now. However, it is possible to have a blocked event
191 // outstanding in a live thread at this point. If we actually forward state
192 // to future threads, we would want to pick that up, right?
193 //
194 // So what do we do here?
195 //
196 // We could make sure the intermediate states were properly fowarded
197 // as the threads are forwarded. That leaves the problem of queries against
198 // this machine state not showing an existing blocked state, which could
199 // have begun long ago.
200 //
201 // If we flush, how do we tag that last block so the forwarding happens
202 // correctly?
203 //
204 // For now, no one is doing the live update thing and using the cpu
205 // states, so I'm going to flush.
206 //
207 // Note that if the very last event is a make_unrunnable for a thread,
208 // this is going to yield a zero length blocking event, which might assert.
209 //
210 // NEEDS REVIEW, FIX ME.
211 make_runnable(half_open_timestamp);
212
213 _vouchers_by_time.back().set_max(half_open_timestamp);
214 }
215 }