xref: /xnu-8796.121.2/tools/trace/bridgetime.lua (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions#!/usr/local/bin/luatrace -s
2*c54f35caSApple OSS Distributions
3*c54f35caSApple OSS Distributionstrace_codename = function(codename, callback)
4*c54f35caSApple OSS Distributions	local debugid = trace.debugid(codename)
5*c54f35caSApple OSS Distributions	if debugid ~= 0 then
6*c54f35caSApple OSS Distributions		trace.single(debugid,callback)
7*c54f35caSApple OSS Distributions	else
8*c54f35caSApple OSS Distributions		printf("WARNING: Cannot locate debugid for '%s'\n", codename)
9*c54f35caSApple OSS Distributions	end
10*c54f35caSApple OSS Distributionsend
11*c54f35caSApple OSS Distributions
12*c54f35caSApple OSS Distributionsinitial_timestamp = 0
13*c54f35caSApple OSS Distributionsget_prefix = function(buf, char)
14*c54f35caSApple OSS Distributions	-- if initial_timestamp == 0 then
15*c54f35caSApple OSS Distributions		-- initial_timestamp = buf.timestamp
16*c54f35caSApple OSS Distributions	-- end
17*c54f35caSApple OSS Distributions	local secs = trace.convert_timestamp_to_nanoseconds(buf.timestamp - initial_timestamp) / 1000000000
18*c54f35caSApple OSS Distributions
19*c54f35caSApple OSS Distributions	return string.format("%s %6.9f %-30s",
20*c54f35caSApple OSS Distributions		char, secs, buf.debugname)
21*c54f35caSApple OSS Distributionsend
22*c54f35caSApple OSS Distributions
23*c54f35caSApple OSS Distributionsinitial_arm_timestamp = 0
24*c54f35caSApple OSS Distributionsformat_timestamp_arm = function(ts)
25*c54f35caSApple OSS Distributions	local secs = trace.convert_timestamp_to_nanoseconds(ts - initial_arm_timestamp) / 1000000000
26*c54f35caSApple OSS Distributions	return string.format("%6.9f", secs);
27*c54f35caSApple OSS Distributionsend
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributionsinitial_intel_timestamp = 0
30*c54f35caSApple OSS Distributionsformat_timestamp_intel = function(ts)
31*c54f35caSApple OSS Distributions	local secs = (ts - initial_intel_timestamp) / 1000000000
32*c54f35caSApple OSS Distributions	return string.format("%6.9f", secs);
33*c54f35caSApple OSS Distributionsend
34*c54f35caSApple OSS Distributions
35*c54f35caSApple OSS Distributionsformat_timestamp_ns = function(ts)
36*c54f35caSApple OSS Distributions	local secs = (ts) / 1000000000
37*c54f35caSApple OSS Distributions	return string.format("%6.9f", secs);
38*c54f35caSApple OSS Distributionsend
39*c54f35caSApple OSS Distributions
40*c54f35caSApple OSS Distributionstrace_codename("MACH_CLOCK_BRIDGE_RESET_TS", function(buf)
41*c54f35caSApple OSS Distributions	local prefix = get_prefix(buf, "X")
42*c54f35caSApple OSS Distributions	local reason = "UNKNOWN";
43*c54f35caSApple OSS Distributions	if buf[3] == 1 then
44*c54f35caSApple OSS Distributions		reason = "RecvSentinel"
45*c54f35caSApple OSS Distributions	elseif buf[3] == 2 then
46*c54f35caSApple OSS Distributions		reason = "ResetTrue"
47*c54f35caSApple OSS Distributions	elseif buf[3] == 3 then
48*c54f35caSApple OSS Distributions		reason = "RateZero"
49*c54f35caSApple OSS Distributions	elseif buf[3] == 4 then
50*c54f35caSApple OSS Distributions		reason = "TSMismatch"
51*c54f35caSApple OSS Distributions	end
52*c54f35caSApple OSS Distributions	printf("%s %-15s ( %-10s %-10s ) ----------------------------------------\n",
53*c54f35caSApple OSS Distributions		prefix, reason, format_timestamp_arm(buf[1]), format_timestamp_intel(buf[2]))
54*c54f35caSApple OSS Distributions
55*c54f35caSApple OSS Distributions	-- initial_arm_timestamp = buf[1]
56*c54f35caSApple OSS Distributions	-- initial_intel_timestamp = buf[2]
57*c54f35caSApple OSS Distributionsend)
58*c54f35caSApple OSS Distributions
59*c54f35caSApple OSS Distributionstrace_codename("MACH_CLOCK_BRIDGE_TS_PARAMS", function(buf)
60*c54f35caSApple OSS Distributions	local prefix = get_prefix(buf, ">")
61*c54f35caSApple OSS Distributions
62*c54f35caSApple OSS Distributions	local rate
63*c54f35caSApple OSS Distributions	if darwin.uint64_to_double then
64*c54f35caSApple OSS Distributions		rate = darwin.uint64_to_double(buf[3])
65*c54f35caSApple OSS Distributions	else
66*c54f35caSApple OSS Distributions		rate = math.nan
67*c54f35caSApple OSS Distributions	end
68*c54f35caSApple OSS Distributions
69*c54f35caSApple OSS Distributions	printf("%s %30s( %-10s %-10s ) rate = %f\n",
70*c54f35caSApple OSS Distributions		prefix, "", format_timestamp_ns(buf[1]), format_timestamp_intel(buf[2]),
71*c54f35caSApple OSS Distributions		rate)
72*c54f35caSApple OSS Distributionsend)
73*c54f35caSApple OSS Distributions
74*c54f35caSApple OSS Distributionstrace_codename("MACH_CLOCK_BRIDGE_REMOTE_TIME", function(buf)
75*c54f35caSApple OSS Distributions	local prefix = get_prefix(buf, "-")
76*c54f35caSApple OSS Distributions
77*c54f35caSApple OSS Distributions	printf("%s ( %-10s %-10s ) @ %-20s\n",
78*c54f35caSApple OSS Distributions		prefix, format_timestamp_arm(buf[1]), format_timestamp_intel(buf[2]), format_timestamp_arm(buf[3]))
79*c54f35caSApple OSS Distributionsend)
80*c54f35caSApple OSS Distributions
81*c54f35caSApple OSS Distributionstrace_codename("MACH_CLOCK_BRIDGE_RCV_TS", function(buf)
82*c54f35caSApple OSS Distributions	local prefix = get_prefix(buf, "<")
83*c54f35caSApple OSS Distributions
84*c54f35caSApple OSS Distributions	if buf[2] == 0xfffffffffffffffe then
85*c54f35caSApple OSS Distributions		printf("%s ( %-10s  Sleep )\n",
86*c54f35caSApple OSS Distributions			prefix, format_timestamp_arm(buf[1]), format_timestamp_intel(buf[2]))
87*c54f35caSApple OSS Distributions	elseif buf[2] == 0xfffffffffffffffd then
88*c54f35caSApple OSS Distributions		printf("%s ( %-10s Wake )\n",
89*c54f35caSApple OSS Distributions			prefix, format_timestamp_arm(buf[1]), format_timestamp_intel(buf[2]))
90*c54f35caSApple OSS Distributions	elseif buf[2] == 0xfffffffffffffffc then
91*c54f35caSApple OSS Distributions		printf("%s ( %-10s Reset )\n",
92*c54f35caSApple OSS Distributions			prefix, format_timestamp_arm(buf[1]), format_timestamp_intel(buf[2]))
93*c54f35caSApple OSS Distributions	else
94*c54f35caSApple OSS Distributions		local skip = ""
95*c54f35caSApple OSS Distributions		if buf[1] == 0 then
96*c54f35caSApple OSS Distributions			skip = "Int handler"
97*c54f35caSApple OSS Distributions		end
98*c54f35caSApple OSS Distributions		printf("%s ( %-10s %-10s ) %s\n",
99*c54f35caSApple OSS Distributions			prefix, format_timestamp_arm(buf[1]), format_timestamp_intel(buf[2]), skip)
100*c54f35caSApple OSS Distributions	end
101*c54f35caSApple OSS Distributions
102*c54f35caSApple OSS Distributionsend)
103*c54f35caSApple OSS Distributions
104*c54f35caSApple OSS Distributionstrace_codename("MACH_CLOCK_BRIDGE_SKIP_TS", function(buf)
105*c54f35caSApple OSS Distributions	local prefix = get_prefix(buf, "*")
106*c54f35caSApple OSS Distributions
107*c54f35caSApple OSS Distributions	if buf[4] > 0 then
108*c54f35caSApple OSS Distributions		printf("%s SKIP_RESET:%3d (Cur: %-10s Prev:%-10s) %-10s\n",
109*c54f35caSApple OSS Distributions			prefix, buf[4], format_timestamp_arm(buf[1]), format_timestamp_arm(buf[3]),
110*c54f35caSApple OSS Distributions			format_timestamp_intel(buf[2]))
111*c54f35caSApple OSS Distributions	else
112*c54f35caSApple OSS Distributions		printf("%s SKIP_DISTANCE: (Cur: %-10s Prev: %-10s) %-10s\n",
113*c54f35caSApple OSS Distributions			prefix, format_timestamp_arm(buf[1]), format_timestamp_arm(buf[3]),
114*c54f35caSApple OSS Distributions			format_timestamp_intel(buf[2]))
115*c54f35caSApple OSS Distributions	end
116*c54f35caSApple OSS Distributions
117*c54f35caSApple OSS Distributionsend)
118*c54f35caSApple OSS Distributions
119*c54f35caSApple OSS Distributionstrace_codename("MACH_CLOCK_BRIDGE_TS_MISMATCH", function(buf)
120*c54f35caSApple OSS Distributions	local prefix = get_prefix(buf, "?")
121*c54f35caSApple OSS Distributions
122*c54f35caSApple OSS Distributions	local diff = (math.abs(buf[2] - buf[3]))/1000000
123*c54f35caSApple OSS Distributions
124*c54f35caSApple OSS Distributions	printf("%s ( Cur: %-10s Pred: %-10s Diff: %5.6f ms Count: %d ) @ %-20s\n",
125*c54f35caSApple OSS Distributions		prefix, format_timestamp_intel(buf[2]), format_timestamp_intel(buf[3]),
126*c54f35caSApple OSS Distributions		diff, buf[4], format_timestamp_arm(buf[1]))
127*c54f35caSApple OSS Distributions
128*c54f35caSApple OSS Distributionsend)
129*c54f35caSApple OSS Distributions
130*c54f35caSApple OSS Distributionstrace_codename("MACH_CLOCK_BRIDGE_OBSV_RATE", function(buf)
131*c54f35caSApple OSS Distributions	local prefix = get_prefix(buf, "=")
132*c54f35caSApple OSS Distributions
133*c54f35caSApple OSS Distributions	local rate
134*c54f35caSApple OSS Distributions	if darwin.uint64_to_double then
135*c54f35caSApple OSS Distributions		rate = darwin.uint64_to_double(buf[1])
136*c54f35caSApple OSS Distributions	else
137*c54f35caSApple OSS Distributions		rate = math.nan
138*c54f35caSApple OSS Distributions	end
139*c54f35caSApple OSS Distributions
140*c54f35caSApple OSS Distributions	printf("%s obsv_rate = %f exceeded limits(0.8, 1.2)\n", prefix, rate)
141*c54f35caSApple OSS Distributions
142*c54f35caSApple OSS Distributionsend)
143