]> git.saurik.com Git - apple/system_cmds.git/blob - kdprof/Globals.hpp
281edebc32111e4d7af28a3e9fa0e850aee6162a
[apple/system_cmds.git] / kdprof / Globals.hpp
1 //
2 // Globals.hpp
3 // kdprof
4 //
5 // Created by James McIlree on 4/17/13.
6 // Copyright (c) 2013 Apple. All rights reserved.
7 //
8
9 #ifndef kdprof_Globals_hpp
10 #define kdprof_Globals_hpp
11
12 //
13 // These are "global" values that control parsing and printing behavior.
14 //
15
16 enum class kSortKey : std::uint32_t {
17 CPU=0,
18 VMFault,
19 IO_Ops,
20 IO_Size,
21 IO_Wait,
22 ID
23 };
24
25 class Globals {
26 protected:
27 // Default/unknown parsing values
28 uint32_t _cpu_count;
29 uint32_t _iop_count;
30 KernelSize _kernel_size;
31 std::string _summary_start;
32 std::string _summary_stop;
33 std::string _summary_step;
34
35 bool _is_cpu_count_set;
36 bool _is_iop_count_set;
37 bool _is_kernel_size_set;
38 bool _is_summary_start_set;
39 bool _is_summary_stop_set;
40 bool _is_summary_step_set;
41 bool _is_should_print_summary_set;
42 bool _is_timebase_set;
43
44 // Output, printing related.
45 AbsTime _beginning_of_time;
46 mach_timebase_info_data_t _timebase_info;
47 FileDescriptor _output_fd;
48 bool _should_read_default_trace_codes;
49 std::vector<std::string> _additional_trace_code_paths;
50 std::unordered_map<uint32_t, std::string> _trace_codes;
51 bool _should_print_mach_absolute_timestamps;
52 bool _should_print_event_index;
53 bool _should_print_symbolic_event_codes;
54 bool _is_verbose;
55 bool _should_presort_events;
56 bool _should_print_cpu_summaries;
57 bool _should_print_process_summaries;
58 bool _should_print_thread_summaries;
59 bool _should_print_events;
60 bool _should_print_summary;
61 bool _should_zero_base_timestamps;
62 bool _should_print_process_start_stop_timestamps;
63 bool _should_print_csv_summary;
64 kSortKey _sort_key;
65
66 AbsTime parse_time(const char* arg) const;
67
68 public:
69 Globals();
70
71 uint32_t cpu_count() const { return _cpu_count; }
72 void set_cpu_count(uint32_t num) { _cpu_count = num; _is_cpu_count_set = true; }
73 bool is_cpu_count_set() const { return _is_cpu_count_set; }
74
75 uint32_t iop_count() const { return _iop_count; }
76 void set_iop_count(uint32_t num) { _iop_count = num; _is_iop_count_set = true; }
77 bool is_iop_count_set() const { return _is_iop_count_set; }
78
79 KernelSize kernel_size() const { return _kernel_size; }
80 void set_kernel_size(KernelSize size) { _kernel_size = size; _is_kernel_size_set = true; }
81 bool is_kernel_size_set() const { return _is_kernel_size_set; }
82
83 AbsTime beginning_of_time() const { return _beginning_of_time; }
84 void set_beginning_of_time(AbsTime t) { _beginning_of_time = t; }
85
86 mach_timebase_info_data_t timebase() const { return _timebase_info; }
87 void set_timebase(mach_timebase_info_data_t timebase, bool is_user_set) { _timebase_info = timebase; if (is_user_set) _is_timebase_set = true; }
88 bool is_timebase_set() const { return _is_timebase_set; }
89
90 int output_fd() const { return _output_fd.is_open() ? (int)_output_fd : STDOUT_FILENO; }
91
92 // Okay, this method caused enough pain to make the final resolution worth a comment.
93 //
94 // http://thbecker.net/articles/rvalue_references/section_05.html
95 //
96 // Things that are declared as rvalue reference can be lvalues or rvalues.
97 // The distinguishing criterion is: if it has a name, then it is an lvalue. Otherwise, it is an rvalue.
98 //
99 // In this case, you cannot call set_output_fd with an lvalue, but fd is STILL an lvalue.
100 // We must still explicitly use std::move on fd!
101 void set_output_fd(FileDescriptor&& fd) { _output_fd = std::move(fd); }
102
103 void set_should_read_default_trace_codes(bool value) { _should_read_default_trace_codes = value; }
104 void append_trace_codes_at_path(std::string path) { _additional_trace_code_paths.push_back(path); }
105 void resolve_trace_codes(void) { _trace_codes = ::resolve_trace_codes(_should_read_default_trace_codes, _is_verbose ? 1 : -1, _additional_trace_code_paths); }
106
107 const std::unordered_map<uint32_t, std::string>& trace_codes() const { return _trace_codes; }
108 void set_trace_codes(std::unordered_map<uint32_t, std::string>&& codes) { _trace_codes = codes; }
109
110 bool should_print_mach_absolute_timestamps() const { return _should_print_mach_absolute_timestamps; }
111 void set_should_print_mach_absolute_timestamps(bool value) { _should_print_mach_absolute_timestamps = value; }
112
113 bool should_print_event_index() const { return _should_print_event_index; }
114 void set_should_print_event_index(bool value) { _should_print_event_index = value; }
115
116 bool should_print_symbolic_event_codes() const { return _should_print_symbolic_event_codes; }
117 void set_should_print_symbolic_event_codes(bool value) { _should_print_symbolic_event_codes = value; }
118
119 bool is_verbose() const { return _is_verbose; }
120 void set_is_verbose(bool value) { _is_verbose = value; }
121
122 bool should_presort_events() const { return _should_presort_events; }
123 void set_should_presort_events(bool value) { _should_presort_events = value; }
124
125 bool should_print_cpu_summaries() const { return _should_print_cpu_summaries; }
126 void set_should_print_cpu_summaries(bool value) { _should_print_cpu_summaries = value; }
127
128 bool should_print_process_summaries() const { return _should_print_process_summaries; }
129 void set_should_print_process_summaries(bool value) { _should_print_process_summaries = value; }
130
131 bool should_print_thread_summaries() const { return _should_print_thread_summaries; }
132 void set_should_print_thread_summaries(bool value) { _should_print_thread_summaries = value; }
133
134 bool should_print_events() const { return _should_print_events; }
135 void set_should_print_events(bool value) { _should_print_events = value; }
136
137 bool should_print_summary() const { return _should_print_summary; }
138 void set_should_print_summary(bool value) { _should_print_summary = value; _is_should_print_summary_set = true; }
139 bool is_should_print_summary_set() const { return _is_should_print_summary_set; }
140
141 bool should_zero_base_timestamps() const { return _should_zero_base_timestamps; }
142 void set_should_zero_base_timestamps(bool value) { _should_zero_base_timestamps = value; }
143
144 bool should_print_process_start_stop_timestamps() const { return _should_print_process_start_stop_timestamps; }
145 void set_should_print_process_start_stop_timestamps(bool value) { _should_print_process_start_stop_timestamps = value; }
146
147 bool should_print_csv_summary() const { return _should_print_csv_summary; }
148 void set_should_print_csv_summary(bool value) { _should_print_csv_summary = value; }
149
150 kSortKey sort_key() const { return _sort_key; }
151 void set_sort_key(kSortKey key) { _sort_key = key; }
152
153 //
154 // The summary {start/stop/step} functions translate the string on the fly,
155 // using the currently set timebase. They need to be fed a timespan that
156 // corresponds to the Machine<SIZE>'s timespan, because the default values
157 // and offsets depend on that.
158 //
159 // This solve the issue of the user saying --start 1234mabs at the command line
160 // and getting an offset of 1234 nanoseconds on a desktop when they are looking
161 // at a device file.
162 //
163 AbsTime summary_start(AbsInterval timespan) const;
164 void set_summary_start(const char* value) { _summary_start = value; _is_summary_start_set = true; }
165 bool is_summary_start_set() const { return _is_summary_start_set; }
166
167 AbsTime summary_stop(AbsInterval timespan) const;
168 void set_summary_stop(const char* value) { _summary_stop = value; _is_summary_stop_set = true; }
169 bool is_summary_stop_set() const { return _is_summary_stop_set; }
170
171 AbsTime summary_step(AbsInterval timespan) const;
172 void set_summary_step(const char* value) { _summary_step = value; _is_summary_step_set = true; }
173 bool is_summary_step_set() const { return _is_summary_step_set; }
174 };
175
176 #endif