2 // MachineThread.mutable-impl.hpp
5 // Created by James McIlree on 10/30/12.
6 // Copyright (c) 2014 Apple. All rights reserved.
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");
17 set_flags(kMachineThreadFlag::IsIdle);
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");
25 AbsTime terminated_timestamp = timestamp + AbsTime(1);
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);
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);
38 // Finally set this threads timespan
40 _timespan.set_max(terminated_timestamp);
43 set_flags(kMachineThreadFlag::TraceTerminated);
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");
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);
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");
64 _begin_blocked = timestamp;
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");
73 ASSERT(_begin_vm_fault == 0, "Attempt to begin vm_fault without end");
74 _begin_vm_fault = timestamp;
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");
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);
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");
97 ASSERT(_begin_jetsam_activity == 0, "Attempt to begin jetsam activity without end");
98 ASSERT(_begin_jetsam_activity_type == 0, "Sanity");
100 _begin_jetsam_activity = timestamp;
101 DEBUG_ONLY(_begin_jetsam_activity_type = type;)
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");
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;)
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");
128 VoucherInterval<SIZE>& last_voucher = _vouchers_by_time.back();
130 if (voucher != last_voucher.voucher()) {
131 ASSERT(last_voucher.max() == AbsTime::END_OF_TIME, "Sanity");
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.
138 // There are three cases possible.
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.
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()) {
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.
156 // The timestamp == _timespan.location assert may also be too strong, if we start forwarding threads true lifetimes.
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));
163 ASSERT(false, "Attempting to set a voucher on thread earlier in time than the thread's current voucher");
169 template <typename SIZE>
170 void MachineThread<SIZE>::post_initialize(AbsTime last_machine_timestamp) {
171 if (!is_trace_terminated()) {
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
177 ASSERT(_timespan.length() == 0, "Sanity");
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);
184 _timespan.set_max(half_open_timestamp);
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?
194 // So what do we do here?
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.
201 // If we flush, how do we tag that last block so the forwarding happens
204 // For now, no one is doing the live update thing and using the cpu
205 // states, so I'm going to flush.
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.
210 // NEEDS REVIEW, FIX ME.
211 make_runnable(half_open_timestamp);
213 _vouchers_by_time.back().set_max(half_open_timestamp);