| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
import pathlib |
| 5 |
import re |
| 6 |
import sys |
| 7 |
from datetime import datetime |
| 8 |
|
| 9 |
MEM_MATCHER = re.compile("([\\d,]*)K:\\s([\\S]*)\\s\\(") |
| 10 |
|
| 11 |
|
| 12 |
def make_differential_metrics( |
| 13 |
differential_name, base_measures, mem_measures, cpu_measures |
| 14 |
): |
| 15 |
metrics = [] |
| 16 |
|
| 17 |
# Setup memory differentials |
| 18 |
metrics.extend( |
| 19 |
[ |
| 20 |
{ |
| 21 |
"name": f"{mem_type}-{category}-{differential_name}", |
| 22 |
"unit": "Kb", |
| 23 |
"values": [ |
| 24 |
round(mem_usage - base_measures["mem"][mem_type][category], 2) |
| 25 |
], |
| 26 |
} |
| 27 |
for mem_type, mem_info in mem_measures.items() |
| 28 |
for category, mem_usage in mem_info.items() |
| 29 |
] |
| 30 |
) |
| 31 |
metrics.extend( |
| 32 |
[ |
| 33 |
{ |
| 34 |
"name": f"{mem_type}-total-{differential_name}", |
| 35 |
"unit": "Kb", |
| 36 |
"values": [ |
| 37 |
round( |
| 38 |
sum(mem_info.values()) |
| 39 |
- sum(base_measures["mem"][mem_type].values()), |
| 40 |
2, |
| 41 |
) |
| 42 |
], |
| 43 |
} |
| 44 |
for mem_type, mem_info in mem_measures.items() |
| 45 |
] |
| 46 |
) |
| 47 |
|
| 48 |
# Setup cpuTime differentials |
| 49 |
metrics.extend( |
| 50 |
[ |
| 51 |
{ |
| 52 |
"name": f"cpuTime-{category}-{differential_name}", |
| 53 |
"unit": "ms", |
| 54 |
"values": [cpu_time - base_measures["cpu"][category]], |
| 55 |
} |
| 56 |
for category, cpu_time in cpu_measures.items() |
| 57 |
] |
| 58 |
) |
| 59 |
metrics.append( |
| 60 |
{ |
| 61 |
"name": f"cpuTime-total-{differential_name}", |
| 62 |
"unit": "ms", |
| 63 |
"values": [ |
| 64 |
round( |
| 65 |
sum(cpu_measures.values()) - sum(base_measures["cpu"].values()), 2 |
| 66 |
) |
| 67 |
], |
| 68 |
} |
| 69 |
) |
| 70 |
|
| 71 |
return metrics |
| 72 |
|
| 73 |
|
| 74 |
def get_chrome_process_category(process, binary): |
| 75 |
if "privileged_process" in process: |
| 76 |
return "gpu" |
| 77 |
elif "sandboxed_process" in process: |
| 78 |
return "tab" |
| 79 |
elif "zygote" in process: |
| 80 |
return "zygote" |
| 81 |
return "main" |
| 82 |
|
| 83 |
|
| 84 |
def get_fenix_process_category(process, binary): |
| 85 |
# In the future, we'll also need to catch media/utility procs |
| 86 |
if "tab" in process: |
| 87 |
return "tab" |
| 88 |
elif f"{binary}" in process: |
| 89 |
return "main" |
| 90 |
elif "zygote" in process: |
| 91 |
return "zygote" |
| 92 |
return process |
| 93 |
|
| 94 |
|
| 95 |
def get_category_for_process(process, binary): |
| 96 |
if "fenix" in binary: |
| 97 |
return get_fenix_process_category(process, binary) |
| 98 |
elif "chrome" in binary: |
| 99 |
return get_chrome_process_category(process, binary) |
| 100 |
raise Exception("Unknown binary for determining process category") |
| 101 |
|
| 102 |
|
| 103 |
def parse_memory_usage(mem_file, binary): |
| 104 |
mem_info = [] |
| 105 |
with mem_file.open() as f: |
| 106 |
mem_info = f.readlines() |
| 107 |
|
| 108 |
curr_mem = "" |
| 109 |
final_mems = {"rss": {}, "pss": {}} |
| 110 |
for line in mem_info: |
| 111 |
if not line.strip(): |
| 112 |
# Anytime a blank line is hit, the current |
| 113 |
# memory type being tracked changes |
| 114 |
curr_mem = "" |
| 115 |
continue |
| 116 |
if not curr_mem: |
| 117 |
if "Total RSS by process:" in line: |
| 118 |
curr_mem = "rss" |
| 119 |
elif "Total PSS by process:" in line: |
| 120 |
curr_mem = "pss" |
| 121 |
continue |
| 122 |
|
| 123 |
match = MEM_MATCHER.search(line.strip()) |
| 124 |
if not match: |
| 125 |
continue |
| 126 |
|
| 127 |
mem_usage, binary_name = match.groups() |
| 128 |
if binary not in binary_name: |
| 129 |
continue |
| 130 |
|
| 131 |
name_split = binary_name.split(f"{binary}:") |
| 132 |
if len(name_split) == 1: |
| 133 |
name = name_split[0] |
| 134 |
else: |
| 135 |
name = name_split[-1] |
| 136 |
|
| 137 |
final_mems[curr_mem][name] = round(float(mem_usage.replace(",", "")), 2) |
| 138 |
|
| 139 |
measurements = { |
| 140 |
"rss": {"tab": 0, "gpu": 0, "main": 0, "crashhelper": 0}, |
| 141 |
"pss": {"tab": 0, "gpu": 0, "main": 0, "crashhelper": 0}, |
| 142 |
} |
| 143 |
for mem_type, mem_info in final_mems.items(): |
| 144 |
for name, mem_usage in mem_info.items(): |
| 145 |
final_name = get_category_for_process(name, binary) |
| 146 |
if ( |
| 147 |
final_name == "zygote" |
| 148 |
and measurements[mem_type].get("zygote", None) is None |
| 149 |
): |
| 150 |
# Only add this process if it exists (it doesn't exist on fenix) |
| 151 |
measurements[mem_type]["zygote"] = 0 |
| 152 |
measurements[mem_type][final_name] += mem_usage |
| 153 |
|
| 154 |
return measurements |
| 155 |
|
| 156 |
|
| 157 |
def parse_cpu_usage(cpu_file, binary): |
| 158 |
cpu_info = [] |
| 159 |
with cpu_file.open() as f: |
| 160 |
cpu_info = f.readlines() |
| 161 |
|
| 162 |
# Gather all the final cpu times for the processes |
| 163 |
final_times = {} |
| 164 |
for line in cpu_info: |
| 165 |
if not line.strip(): |
| 166 |
continue |
| 167 |
vals = line.split() |
| 168 |
|
| 169 |
name = vals[0] |
| 170 |
if f"{binary}" not in name: |
| 171 |
# Sometimes the PID catches the wrong process |
| 172 |
continue |
| 173 |
|
| 174 |
name_split = name.split(f"{binary}:") |
| 175 |
if len(name_split) == 1: |
| 176 |
name = name_split[0] |
| 177 |
else: |
| 178 |
name = name_split[-1] |
| 179 |
|
| 180 |
final_times[name] = vals[-2] |
| 181 |
|
| 182 |
# Convert the final times to milliseconds |
| 183 |
cpu_times = {"tab": 0, "gpu": 0, "main": 0, "crashhelper": 0} |
| 184 |
for name, time in final_times.items(): |
| 185 |
# adb shell ps -o time+= gives us MIN:SEC.HUNDREDTHS. |
| 186 |
# That's why we divide dt.microseconds by 1000 for measuring in milliseconds. |
| 187 |
dt = datetime.strptime(time, "%M:%S.%f") |
| 188 |
milliseconds = (((dt.minute * 60) + dt.second) * 1000) + (dt.microsecond / 1000) |
| 189 |
|
| 190 |
final_name = get_category_for_process(name, binary) |
| 191 |
if final_name == "zygote" and cpu_times.get("zygote", None) is None: |
| 192 |
# Only add this process if it exists (it doesn't exist on fenix) |
| 193 |
cpu_times["zygote"] = 0 |
| 194 |
|
| 195 |
cpu_times[final_name] += milliseconds |
| 196 |
|
| 197 |
return cpu_times |
| 198 |
|
| 199 |
|
| 200 |
def main(): |
| 201 |
args = sys.argv[1:] |
| 202 |
binary = args[1] |
| 203 |
testing_dir = pathlib.Path(args[0]) |
| 204 |
run_background = True if args[2] == "True" else False |
| 205 |
|
| 206 |
cpu_info_files = sorted(testing_dir.glob("cpu_info*")) |
| 207 |
mem_info_files = sorted(testing_dir.glob("mem_info*")) |
| 208 |
|
| 209 |
perf_metrics = [] |
| 210 |
base_measures = {} |
| 211 |
for i, measurement_time in enumerate(("start", "10%", "50%", "end")): |
| 212 |
cpu_measures = parse_cpu_usage(cpu_info_files[i], binary) |
| 213 |
mem_measures = parse_memory_usage(mem_info_files[i], binary) |
| 214 |
|
| 215 |
if not base_measures: |
| 216 |
base_measures["cpu"] = cpu_measures |
| 217 |
base_measures["mem"] = mem_measures |
| 218 |
|
| 219 |
perf_metrics.extend( |
| 220 |
[ |
| 221 |
{ |
| 222 |
"name": f"cpuTime-{category}-{measurement_time}", |
| 223 |
"unit": "ms", |
| 224 |
"values": [cpu_time], |
| 225 |
} |
| 226 |
for category, cpu_time in cpu_measures.items() |
| 227 |
] |
| 228 |
) |
| 229 |
perf_metrics.append( |
| 230 |
{ |
| 231 |
"name": f"cpuTime-total-{measurement_time}", |
| 232 |
"unit": "ms", |
| 233 |
"values": [round(sum(cpu_measures.values()), 2)], |
| 234 |
} |
| 235 |
) |
| 236 |
|
| 237 |
perf_metrics.extend( |
| 238 |
[ |
| 239 |
{ |
| 240 |
"name": f"{mem_type}-{category}-{measurement_time}", |
| 241 |
"unit": "Kb", |
| 242 |
"values": [round(mem_usage, 2)], |
| 243 |
} |
| 244 |
for mem_type, mem_info in mem_measures.items() |
| 245 |
for category, mem_usage in mem_info.items() |
| 246 |
] |
| 247 |
) |
| 248 |
perf_metrics.extend( |
| 249 |
[ |
| 250 |
{ |
| 251 |
"name": f"{mem_type}-total-{measurement_time}", |
| 252 |
"unit": "Kb", |
| 253 |
"values": [round(sum(mem_info.values()), 2)], |
| 254 |
} |
| 255 |
for mem_type, mem_info in mem_measures.items() |
| 256 |
] |
| 257 |
) |
| 258 |
|
| 259 |
if base_measures and run_background: |
| 260 |
if measurement_time == "10%": |
| 261 |
perf_metrics.extend( |
| 262 |
make_differential_metrics( |
| 263 |
"backgrounding-diff", base_measures, mem_measures, cpu_measures |
| 264 |
) |
| 265 |
) |
| 266 |
elif measurement_time == "end": |
| 267 |
perf_metrics.extend( |
| 268 |
make_differential_metrics( |
| 269 |
"background-diff", base_measures, mem_measures, cpu_measures |
| 270 |
) |
| 271 |
) |
| 272 |
|
| 273 |
print( |
| 274 |
"perfMetrics: " |
| 275 |
+ str(perf_metrics).replace("{", "{{").replace("}", "}}").replace("'", '"') |
| 276 |
) |
| 277 |
|
| 278 |
|
| 279 |
if __name__ == "__main__": |
| 280 |
main() |