File: parse_resource_usage.py

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (280 lines) | stat: -rw-r--r-- 8,623 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import pathlib
import re
import sys
from datetime import datetime

MEM_MATCHER = re.compile("([\\d,]*)K:\\s([\\S]*)\\s\\(")


def make_differential_metrics(
    differential_name, base_measures, mem_measures, cpu_measures
):
    metrics = []

    # Setup memory differentials
    metrics.extend(
        [
            {
                "name": f"{mem_type}-{category}-{differential_name}",
                "unit": "Kb",
                "values": [
                    round(mem_usage - base_measures["mem"][mem_type][category], 2)
                ],
            }
            for mem_type, mem_info in mem_measures.items()
            for category, mem_usage in mem_info.items()
        ]
    )
    metrics.extend(
        [
            {
                "name": f"{mem_type}-total-{differential_name}",
                "unit": "Kb",
                "values": [
                    round(
                        sum(mem_info.values())
                        - sum(base_measures["mem"][mem_type].values()),
                        2,
                    )
                ],
            }
            for mem_type, mem_info in mem_measures.items()
        ]
    )

    # Setup cpuTime differentials
    metrics.extend(
        [
            {
                "name": f"cpuTime-{category}-{differential_name}",
                "unit": "ms",
                "values": [cpu_time - base_measures["cpu"][category]],
            }
            for category, cpu_time in cpu_measures.items()
        ]
    )
    metrics.append(
        {
            "name": f"cpuTime-total-{differential_name}",
            "unit": "ms",
            "values": [
                round(
                    sum(cpu_measures.values()) - sum(base_measures["cpu"].values()), 2
                )
            ],
        }
    )

    return metrics


def get_chrome_process_category(process, binary):
    if "privileged_process" in process:
        return "gpu"
    elif "sandboxed_process" in process:
        return "tab"
    elif "zygote" in process:
        return "zygote"
    return "main"


def get_fenix_process_category(process, binary):
    # In the future, we'll also need to catch media/utility procs
    if "tab" in process:
        return "tab"
    elif f"{binary}" in process:
        return "main"
    elif "zygote" in process:
        return "zygote"
    return process


def get_category_for_process(process, binary):
    if "fenix" in binary:
        return get_fenix_process_category(process, binary)
    elif "chrome" in binary:
        return get_chrome_process_category(process, binary)
    raise Exception("Unknown binary for determining process category")


def parse_memory_usage(mem_file, binary):
    mem_info = []
    with mem_file.open() as f:
        mem_info = f.readlines()

    curr_mem = ""
    final_mems = {"rss": {}, "pss": {}}
    for line in mem_info:
        if not line.strip():
            # Anytime a blank line is hit, the current
            # memory type being tracked changes
            curr_mem = ""
            continue
        if not curr_mem:
            if "Total RSS by process:" in line:
                curr_mem = "rss"
            elif "Total PSS by process:" in line:
                curr_mem = "pss"
            continue

        match = MEM_MATCHER.search(line.strip())
        if not match:
            continue

        mem_usage, binary_name = match.groups()
        if binary not in binary_name:
            continue

        name_split = binary_name.split(f"{binary}:")
        if len(name_split) == 1:
            name = name_split[0]
        else:
            name = name_split[-1]

        final_mems[curr_mem][name] = round(float(mem_usage.replace(",", "")), 2)

    measurements = {
        "rss": {"tab": 0, "gpu": 0, "main": 0, "crashhelper": 0},
        "pss": {"tab": 0, "gpu": 0, "main": 0, "crashhelper": 0},
    }
    for mem_type, mem_info in final_mems.items():
        for name, mem_usage in mem_info.items():
            final_name = get_category_for_process(name, binary)
            if (
                final_name == "zygote"
                and measurements[mem_type].get("zygote", None) is None
            ):
                # Only add this process if it exists (it doesn't exist on fenix)
                measurements[mem_type]["zygote"] = 0
            measurements[mem_type][final_name] += mem_usage

    return measurements


def parse_cpu_usage(cpu_file, binary):
    cpu_info = []
    with cpu_file.open() as f:
        cpu_info = f.readlines()

    # Gather all the final cpu times for the processes
    final_times = {}
    for line in cpu_info:
        if not line.strip():
            continue
        vals = line.split()

        name = vals[0]
        if f"{binary}" not in name:
            # Sometimes the PID catches the wrong process
            continue

        name_split = name.split(f"{binary}:")
        if len(name_split) == 1:
            name = name_split[0]
        else:
            name = name_split[-1]

        final_times[name] = vals[-2]

    # Convert the final times to milliseconds
    cpu_times = {"tab": 0, "gpu": 0, "main": 0, "crashhelper": 0}
    for name, time in final_times.items():
        # adb shell ps -o time+= gives us MIN:SEC.HUNDREDTHS.
        # That's why we divide dt.microseconds by 1000 for measuring in milliseconds.
        dt = datetime.strptime(time, "%M:%S.%f")
        milliseconds = (((dt.minute * 60) + dt.second) * 1000) + (dt.microsecond / 1000)

        final_name = get_category_for_process(name, binary)
        if final_name == "zygote" and cpu_times.get("zygote", None) is None:
            # Only add this process if it exists (it doesn't exist on fenix)
            cpu_times["zygote"] = 0

        cpu_times[final_name] += milliseconds

    return cpu_times


def main():
    args = sys.argv[1:]
    binary = args[1]
    testing_dir = pathlib.Path(args[0])
    run_background = True if args[2] == "True" else False

    cpu_info_files = sorted(testing_dir.glob("cpu_info*"))
    mem_info_files = sorted(testing_dir.glob("mem_info*"))

    perf_metrics = []
    base_measures = {}
    for i, measurement_time in enumerate(("start", "10%", "50%", "end")):
        cpu_measures = parse_cpu_usage(cpu_info_files[i], binary)
        mem_measures = parse_memory_usage(mem_info_files[i], binary)

        if not base_measures:
            base_measures["cpu"] = cpu_measures
            base_measures["mem"] = mem_measures

        perf_metrics.extend(
            [
                {
                    "name": f"cpuTime-{category}-{measurement_time}",
                    "unit": "ms",
                    "values": [cpu_time],
                }
                for category, cpu_time in cpu_measures.items()
            ]
        )
        perf_metrics.append(
            {
                "name": f"cpuTime-total-{measurement_time}",
                "unit": "ms",
                "values": [round(sum(cpu_measures.values()), 2)],
            }
        )

        perf_metrics.extend(
            [
                {
                    "name": f"{mem_type}-{category}-{measurement_time}",
                    "unit": "Kb",
                    "values": [round(mem_usage, 2)],
                }
                for mem_type, mem_info in mem_measures.items()
                for category, mem_usage in mem_info.items()
            ]
        )
        perf_metrics.extend(
            [
                {
                    "name": f"{mem_type}-total-{measurement_time}",
                    "unit": "Kb",
                    "values": [round(sum(mem_info.values()), 2)],
                }
                for mem_type, mem_info in mem_measures.items()
            ]
        )

        if base_measures and run_background:
            if measurement_time == "10%":
                perf_metrics.extend(
                    make_differential_metrics(
                        "backgrounding-diff", base_measures, mem_measures, cpu_measures
                    )
                )
            elif measurement_time == "end":
                perf_metrics.extend(
                    make_differential_metrics(
                        "background-diff", base_measures, mem_measures, cpu_measures
                    )
                )

    print(
        "perfMetrics: "
        + str(perf_metrics).replace("{", "{{").replace("}", "}}").replace("'", '"')
    )


if __name__ == "__main__":
    main()