]> git.saurik.com Git - apple/system_cmds.git/blob - KDBG/ThreadSummary.hpp
system_cmds-671.10.3.tar.gz
[apple/system_cmds.git] / KDBG / ThreadSummary.hpp
1 //
2 // ThreadSummary.hpp
3 // KDBG
4 //
5 // Created by James McIlree on 4/23/13.
6 // Copyright (c) 2014 Apple. All rights reserved.
7 //
8
9 #ifndef kdprof_ThreadSummary_hpp
10 #define kdprof_ThreadSummary_hpp
11
12 template <typename SIZE>
13 class MachineThread;
14
15 template <typename SIZE>
16 class ThreadSummary {
17 protected:
18 const MachineThread<SIZE>* _thread;
19
20 AbsTime _total_run_time;
21 AbsTime _total_idle_time;
22 AbsTime _total_intr_time;
23 AbsTime _total_vm_fault_time;
24 AbsTime _total_io_time;
25 AbsTime _total_jetsam_time;
26
27 uint32_t _context_switch_count;
28 uint32_t _count_idle_events;
29 uint32_t _count_intr_events;
30 uint32_t _count_vm_fault_events;
31 uint32_t _count_io_events;
32
33 uint64_t _io_bytes_completed;
34
35 AbsTime _total_future_run_time;
36
37 // Future run helper vars
38 AbsTime _total_blocked_in_summary;
39 AbsTime _max_possible_future_run_time;
40 AbsTime _first_block_after_summary;
41 bool _is_blocked_in_future;
42 bool _is_future_initialized;
43
44 friend class Machine<SIZE>;
45
46 void add_run_time(AbsTime time) { _total_run_time += time; }
47 void add_idle_time(AbsTime time) { _total_idle_time += time; _count_idle_events++; }
48 void add_intr_time(AbsTime time) { _total_intr_time += time; _count_intr_events++; }
49 void add_vm_fault_time(AbsTime time) { _total_vm_fault_time += time; _count_vm_fault_events++; }
50 void add_io_time(AbsTime time) { _total_io_time += time; _count_io_events++; }
51 void add_jetsam_time(AbsTime time) { _total_jetsam_time += time; }
52
53 void add_io_bytes_completed(typename SIZE::ptr_t bytes) { _io_bytes_completed += bytes; }
54
55 void incr_context_switches() { _context_switch_count++; }
56
57 bool is_blocked_in_future() { return _is_blocked_in_future; }
58 void set_is_blocked_in_future() { _is_blocked_in_future = true; }
59
60 AbsTime total_blocked_in_summary() { return _total_blocked_in_summary; }
61 void set_total_blocked_in_summary(AbsTime time) { _total_blocked_in_summary = time; }
62
63 AbsTime max_possible_future_run_time() { return _max_possible_future_run_time; }
64 void set_max_possible_future_run_time(AbsTime time) { _max_possible_future_run_time = time; }
65
66 AbsTime first_block_after_summary() { return _first_block_after_summary; }
67 void set_first_block_after_summary(AbsTime time) { _first_block_after_summary = time; }
68
69 bool is_future_initialized() { return _is_future_initialized; }
70 void set_future_initialized() { _is_future_initialized = true; }
71
72 AbsTime add_future_run_time(AbsTime time) {
73 ASSERT(_is_future_initialized, "Sanity");
74 ASSERT(!_is_blocked_in_future, "Sanity");
75
76 AbsTime capped_time = _max_possible_future_run_time - _total_future_run_time;
77 if (capped_time < time) {
78 _total_future_run_time += capped_time;
79 _is_blocked_in_future = true;
80 return capped_time;
81 } else {
82 _total_future_run_time += time;
83 return time;
84 }
85
86 ASSERT(_total_future_run_time < _max_possible_future_run_time, "Sanity");
87 }
88
89 public:
90 ThreadSummary(const MachineThread<SIZE>* thread) :
91 _thread(thread),
92 _context_switch_count(0),
93 _count_idle_events(0),
94 _count_intr_events(0),
95 _count_vm_fault_events(0),
96 _count_io_events(0),
97 _io_bytes_completed(0),
98 _is_blocked_in_future(false),
99 _is_future_initialized(false)
100 {
101 }
102
103 const MachineThread<SIZE>* thread() const { return _thread; }
104
105 AbsTime total_time() const { return _total_run_time + _total_idle_time + _total_intr_time; }
106
107 AbsTime total_run_time() const { return _total_run_time; }
108 AbsTime total_idle_time() const { return _total_idle_time; }
109 AbsTime total_intr_time() const { return _total_intr_time; }
110 AbsTime total_future_run_time() const { return _total_future_run_time; }
111 AbsTime total_vm_fault_time() const { return _total_vm_fault_time; }
112 AbsTime total_wallclock_vm_fault_time() const { return _total_vm_fault_time; }
113 AbsTime total_io_time() const { return _total_io_time; }
114 AbsTime total_jetsam_time() const { return _total_jetsam_time; }
115
116 AbsTime avg_on_cpu_time() const { return _total_run_time / _context_switch_count; }
117
118 uint32_t context_switches() const { return _context_switch_count; }
119 uint32_t num_idle_events() const { return _count_idle_events; }
120 uint32_t num_intr_events() const { return _count_intr_events; }
121 uint32_t num_vm_fault_events() const { return _count_vm_fault_events; }
122 uint32_t num_io_events() const { return _count_io_events; }
123
124 uint64_t io_bytes_completed() const { return _io_bytes_completed; }
125
126 DEBUG_ONLY(void validate() const);
127 };
128
129 #if !defined(NDEBUG) && !defined(NS_BLOCK_ASSERTIONS)
130 template <typename SIZE>
131 void ThreadSummary<SIZE>::validate() const {
132 }
133 #endif
134
135 template <typename SIZE>
136 struct ThreadSummaryHash {
137 size_t operator()(const ThreadSummary<SIZE>& summary) const {
138 return std::hash<const MachineThread<SIZE>*>()(summary.thread());
139 }
140 };
141
142 template <typename SIZE>
143 struct ThreadSummaryEqualTo {
144 bool operator()(const ThreadSummary<SIZE>& s1, const ThreadSummary<SIZE>& s2) const {
145 return s1.thread() == s2.thread();
146 }
147 };
148
149 #endif