5 // Created by James McIlree on 4/20/13.
6 // Copyright (c) 2013 Apple. All rights reserved.
9 #ifndef kdprof_ParallelPrinting_hpp
10 #define kdprof_ParallelPrinting_hpp
12 void print_event_header(const Globals& globals, bool is_64_bit);
14 template <typename SIZE>
15 char* print_event(char* buf, char* buf_end, const Globals& globals, const Machine<SIZE>& machine, const KDEvent<SIZE>& event, uintptr_t event_index)
19 // [Index] Time Type Code arg1 arg2 arg3 arg4 thread cpu# command/IOP-name pid
20 // 8 16 4 34 8/16 8/16 8/16 8/16 10 4 16 6
22 // For now, each column is folding up the "after" spacing in a single printf, IOW
24 // buf += snprintf(buf, buf_end - buf, "%8s ", "COL"); /* + 2 spaces */
28 // buf += snprintf(buf, buf_end - buf, "%8s", "COL");
29 // buf += snprintf(buf, buf_end - buf, " "); /* 2 spaces */
31 ASSERT(event.cpu() > -1 && event.cpu() < machine.cpus().size(), "cpu_id out of range");
32 const MachineCPU<SIZE>& cpu = machine.cpus()[event.cpu()];
35 // Okay, here is how snprintf works.
39 // snprintf(buf, 0, "a"); // Returns 1, buf is unchanged.
40 // snprintf(buf, 1, "a"); // Returns 1, buf = \0
41 // snprintf(buf, 2, "a"); // Returns 1, buf = 'a', \0
44 // If we cannot print successfully, we return the orignal pointer.
51 if (globals.should_print_event_index()) {
52 buf += snprintf(buf, buf_end - buf, "%8llu ", (uint64_t)event_index);
61 if (globals.should_print_mach_absolute_timestamps()) {
62 if (globals.beginning_of_time().value() == 0)
63 buf += snprintf(buf, buf_end - buf, "%16llX ", (event.timestamp() - globals.beginning_of_time()).value());
65 buf += snprintf(buf, buf_end - buf, "%16llu ", (event.timestamp() - globals.beginning_of_time()).value());
67 NanoTime ntime = (event.timestamp() - globals.beginning_of_time()).nano_time(globals.timebase());
68 buf += snprintf(buf, buf_end - buf, "%16.2f ", (double)ntime.value() / 1000.0);
77 const char* type = event.is_func_start() ? "beg" : (event.is_func_end() ? "end" : "---");
78 auto trace_code_it = globals.trace_codes().find(event.dbg_cooked());
79 if (cpu.is_iop() || !globals.should_print_symbolic_event_codes() || trace_code_it == globals.trace_codes().end()) {
80 buf += snprintf(buf, buf_end - buf, "%4s %-34x ", type, event.dbg_cooked());
82 buf += snprintf(buf, buf_end - buf, "%4s %-34s ", type, trace_code_it->second.c_str());
91 if (event.dbg_class() == DBG_IOKIT && event.dbg_subclass() == DBG_IOPOWER) {
92 std::string kext_name = event.arg1_as_string();
93 std::reverse(kext_name.begin(), kext_name.end());
96 buf += snprintf(buf, buf_end - buf, "%-16s ", kext_name.c_str());
98 buf += snprintf(buf, buf_end - buf, "%-8s ", kext_name.c_str());
101 buf += snprintf(buf, buf_end - buf, "%-16llX ", (uint64_t)event.arg1());
103 buf += snprintf(buf, buf_end - buf, "%-8x ", (uint32_t)event.arg1());
110 // Profiling showed that the repeated snprintf calls were hot, rolling them up is ~2.5% per on a HUGE file.
112 // arg2 arg3 arg4 thread cpu
115 buf += snprintf(buf, buf_end - buf, "%-16llX %-16llX %-16llX %10llX %4u ", (uint64_t)event.arg2(), (uint64_t)event.arg3(), (uint64_t)event.arg4(), (uint64_t)event.tid(), event.cpu());
117 buf += snprintf(buf, buf_end - buf, "%-8x %-8x %-8x %10llX %4u ", (uint32_t)event.arg2(), (uint32_t)event.arg3(), (uint32_t)event.arg4(), (uint64_t)event.tid(), event.cpu());
123 // command & pid (handled together due to IOP not printing a pid
126 // We print the IOP name instead of a command
127 buf += snprintf(buf, buf_end - buf, "%-16s\n", cpu.name());
129 if (const MachineThread<SIZE>* thread = machine.thread(event.tid(), event.timestamp())) {
130 buf += snprintf(buf, buf_end - buf, "%-16s %-6d\n", thread->process().name(), thread->process().pid());
132 buf += snprintf(buf, buf_end - buf, "%-16s %-6s\n", "?????", "???");
136 // Still need to check this, its an error if we overflow on the last print!
143 template <typename SIZE>
144 char* print_event_range_to_buffer(const Globals& globals, const Machine<SIZE>& machine, TRange<uintptr_t> range, MemoryBuffer<char>& buffer ) {
145 char* cursor = buffer.data();
146 char* cursor_end = cursor + buffer.capacity();
148 if (const KDEvent<SIZE>* events = machine.events()) {
149 ASSERT(TRange<uintptr_t>(0, machine.event_count()).contains(range), "Sanity");
150 for (uintptr_t index = range.location(); index < range.max(); ++index) {
151 char* temp = print_event(cursor, cursor_end, globals, machine, events[index], index);
155 // Changing the capacity will invalidate the cursor
156 ptrdiff_t offset = cursor - buffer.data();
157 buffer.set_capacity(buffer.capacity()*2);
158 cursor = buffer.data() + offset;
159 cursor_end = buffer.data() + buffer.capacity();
167 class PrintWorkUnit {
169 MemoryBuffer<char> _buffer;
170 TRange<uintptr_t> _event_range;
173 // We do not want work units copied.
174 PrintWorkUnit(const PrintWorkUnit& that) = delete;
175 PrintWorkUnit& operator=(const PrintWorkUnit& other) = delete;
178 PrintWorkUnit(MemoryBuffer<char>&& buffer, TRange<uintptr_t> event_range, char* buffer_end) :
179 _buffer(std::move(buffer)),
180 _event_range(event_range),
181 _buffer_end(buffer_end)
183 ASSERT(_buffer.capacity(), "Sanity");
184 ASSERT(_buffer.data(), "Sanity");
185 ASSERT(!_buffer_end || _buffer_end > _buffer.data(), "Sanity");
186 ASSERT(!_buffer_end || (_buffer_end < _buffer.data() + _buffer.capacity()), "Sanity");
189 MemoryBuffer<char>& buffer() { return _buffer; }
191 TRange<uintptr_t> event_range() { return _event_range; }
192 void set_event_range(TRange<uintptr_t> range) { _event_range = range; }
194 char* buffer_end() const { return _buffer_end; }
195 void set_buffer_end(char* buffer_end) { _buffer_end = buffer_end; }
198 template <typename SIZE>
199 class PrintProducer {
201 const Globals& _globals;
202 const Machine<SIZE>& _machine;
203 uintptr_t _start_index;
204 uintptr_t _end_index;
205 uintptr_t _chunk_size;
208 PrintProducer(const Globals& globals, const Machine<SIZE>& machine, uintptr_t chunk_size) :
211 _chunk_size(chunk_size)
214 _end_index = machine.event_count();
216 if (globals.is_summary_start_set() || globals.is_summary_stop_set()) {
217 AbsInterval machine_timespan = machine.timespan();
219 KDEvent<SIZE> start_event(globals.summary_start(machine_timespan));
220 auto it = std::lower_bound(machine.events(), machine.events() + _end_index, start_event);
221 ASSERT(&*it >= machine.events(), "Returned start index lower than start");
222 _start_index = std::distance(machine.events(), it);
224 KDEvent<SIZE> end_event(globals.summary_stop(machine_timespan));
225 it = std::lower_bound(machine.events(), machine.events() + _end_index, end_event);
226 ASSERT(&*it <= machine.events() + _end_index, "Returned end index greater than end");
227 _end_index = std::distance(machine.events(), it);
229 ASSERT(_start_index <= _end_index, "start index is > end index");
233 bool produce(PrintWorkUnit& work_unit) {
234 // Claim a chunk of work to do
235 uintptr_t orig_start_index, new_start_index;
237 orig_start_index = _start_index;
238 new_start_index = orig_start_index + std::min(_chunk_size, _end_index - orig_start_index);
239 } while (orig_start_index < _end_index && !OSAtomicCompareAndSwapPtrBarrier((void*)orig_start_index, (void *)new_start_index, (void * volatile *)&_start_index));
241 // Did we claim work?
242 if (orig_start_index < _end_index) {
243 TRange<uintptr_t> event_range(orig_start_index, new_start_index - orig_start_index);
244 char* end = print_event_range_to_buffer(_globals, _machine, event_range, work_unit.buffer());
246 work_unit.set_event_range(event_range);
247 work_unit.set_buffer_end(end);
254 uintptr_t start_index() const { return _start_index; }
257 template <typename SIZE>
258 class PrintConsumer {
260 const Globals& _globals;
261 uintptr_t _write_index;
262 std::mutex _write_mutex;
263 std::condition_variable _write_condition;
266 PrintConsumer(const Globals& globals, const Machine<SIZE>& machine, uintptr_t start_index) :
268 _write_index(start_index)
272 void consume(PrintWorkUnit& work_unit) {
273 std::unique_lock<std::mutex> guard(_write_mutex);
274 _write_condition.wait(guard, [&](){ return work_unit.event_range().location() == this->_write_index; });
276 ASSERT(work_unit.event_range().location() == _write_index, "Sanity");
278 char* data = work_unit.buffer().data();
279 size_t bytes = work_unit.buffer_end() - data;
280 write(_globals.output_fd(), work_unit.buffer().data(), bytes);
281 _write_index = work_unit.event_range().max();
283 _write_condition.notify_all();
287 template <typename SIZE>
288 uintptr_t print_machine_events(const Globals& globals, const Machine<SIZE>& machine) {
289 print_event_header(globals, SIZE::is_64_bit);
291 if (const KDEvent<SIZE>* events = machine.events()) {
292 if (uintptr_t event_count = machine.event_count()) {
295 // We want to chunk this up into reasonably sized pieces of work.
296 // Because each piece of work can potentially accumulate a large
297 // amount of memory, we need to limit the amount of work "in-flight".
299 uint32_t active_cpus = Kernel::active_cpu_count();
301 uintptr_t chunk_size = 2000;
303 PrintProducer<SIZE> producer(globals, machine, chunk_size);
304 PrintConsumer<SIZE> consumer(globals, machine, producer.start_index());
306 std::vector<std::thread> threads;
307 for (uint32_t i=0; i<active_cpus; ++i) {
308 threads.push_back(std::thread([&]() {
309 PrintWorkUnit work_unit(MemoryBuffer<char>(160 * chunk_size), TRange<uintptr_t>(0, 0), (char*)NULL);
310 while (producer.produce(work_unit)) {
311 consumer.consume(work_unit);
316 for(auto& thread : threads){
320 uint32_t totalProcesses = 0;
321 uint32_t totalThreads = 0;
323 for (auto process : machine.processes()) {
324 if (!process->is_created_by_previous_machine_state()) {
329 for (auto thread : machine.threads()) {
330 if (!thread->is_created_by_previous_machine_state()) {
335 dprintf(globals.output_fd(), "Total Events: %llu\n", (uint64_t)event_count);
336 dprintf(globals.output_fd(), "Total Processes: %u\n", totalProcesses);
337 dprintf(globals.output_fd(), "Total Threads: %u\n", totalThreads);