]> git.saurik.com Git - apple/system_cmds.git/blob - kdprof/Globals.cpp
6f8132d636034932c88d470c7ec1685accc8a314
[apple/system_cmds.git] / kdprof / Globals.cpp
1 //
2 // MachineGlobals.cpp
3 // kdprof
4 //
5 // Created by James McIlree on 4/17/13.
6 // Copyright (c) 2013 Apple. All rights reserved.
7 //
8
9 #include "global.h"
10
11 Globals::Globals() :
12 _cpu_count(0),
13 _iop_count(0),
14 _kernel_size(Kernel::is_64_bit() ? KernelSize::k64 : KernelSize::k32),
15 _is_cpu_count_set(false),
16 _is_iop_count_set(false),
17 _is_kernel_size_set(false),
18 _is_summary_start_set(false),
19 _is_summary_stop_set(false),
20 _is_summary_step_set(false),
21 _is_should_print_summary_set(false),
22 _is_timebase_set(false),
23 _should_read_default_trace_codes(true),
24 _should_print_mach_absolute_timestamps(false),
25 _should_print_event_index(false),
26 _should_print_symbolic_event_codes(true),
27 _is_verbose(false),
28 _should_presort_events(false),
29 _should_print_cpu_summaries(false),
30 _should_print_process_summaries(true),
31 _should_print_thread_summaries(false),
32 _should_print_events(false),
33 _should_print_summary(false),
34 _should_zero_base_timestamps(true),
35 _should_print_process_start_stop_timestamps(false),
36 _should_print_csv_summary(false),
37 _sort_key(kSortKey::CPU)
38 {
39 // Default to the current machine's values
40 mach_timebase_info(&_timebase_info);
41
42 for (auto& entry : KDBG::cpumap()) {
43 if (entry.is_iop())
44 _iop_count++;
45 else
46 _cpu_count++;
47 }
48
49 // If we are unable to get a cpumap,
50 // fallback on the current # of cpus
51 if (_cpu_count == 0) {
52 _cpu_count = Kernel::active_cpu_count();
53 _iop_count = 0;
54 }
55 }
56
57 AbsTime Globals::parse_time(const char* arg) const {
58
59 char* units;
60 uint64_t value = strtoull(arg, &units, 0);
61
62 // Unspecified units are treated as seconds
63 if (*units == 0 || strcmp(units, "s") == 0) {
64 return NanoTime(value * NANOSECONDS_PER_SECOND).abs_time(_timebase_info);
65 }
66
67 if (strcmp(units, "ms") == 0)
68 return NanoTime(value * NANOSECONDS_PER_MILLISECOND).abs_time(_timebase_info);
69
70 if (strcmp(units, "us") == 0)
71 return NanoTime(value * NANOSECONDS_PER_MICROSECOND).abs_time(_timebase_info);
72
73 if (strcmp(units, "ns") == 0)
74 return NanoTime(value).abs_time(_timebase_info);
75
76 if (strcmp(units, "mabs") == 0) {
77 return AbsTime(value);
78 }
79
80 usage("Unable to parse units on time value");
81 }
82
83 AbsTime Globals::summary_start(AbsInterval timespan) const {
84 AbsTime start(timespan.location());
85
86 if (is_summary_start_set()) {
87 AbsTime summary_start = parse_time(_summary_start.c_str());
88
89 bool absolute_start_stop = (_beginning_of_time == 0);
90 if (absolute_start_stop)
91 start = summary_start;
92 else
93 start += summary_start;
94 }
95
96 return start;
97 }
98
99 AbsTime Globals::summary_stop(AbsInterval timespan) const {
100
101 if (is_summary_stop_set()) {
102 AbsTime summary_stop = parse_time(_summary_stop.c_str());
103
104 bool absolute_start_stop = (_beginning_of_time == 0);
105 if (absolute_start_stop)
106 return summary_stop;
107 else
108 return timespan.location() + summary_stop;
109 }
110
111 return timespan.max();
112 }
113
114 AbsTime Globals::summary_step(AbsInterval timespan) const {
115 if (is_summary_step_set()) {
116 return parse_time(_summary_step.c_str());
117 }
118
119 return timespan.length();
120 }