xref: /aosp_15_r20/external/perfetto/ui/src/plugins/com.google.PixelMemory/index.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {Trace} from '../../public/trace';
16import {PerfettoPlugin} from '../../public/plugin';
17import {addDebugCounterTrack} from '../../components/tracks/debug_tracks';
18
19export default class implements PerfettoPlugin {
20  static readonly id = 'com.google.PixelMemory';
21
22  async onTraceLoad(ctx: Trace): Promise<void> {
23    ctx.commands.registerCommand({
24      id: 'dev.perfetto.PixelMemory#ShowTotalMemory',
25      name: 'Add tracks: show a process total memory',
26      callback: async (pid) => {
27        if (pid === undefined) {
28          pid = prompt('Enter a process pid', '');
29          if (pid === null) return;
30        }
31        const RSS_ALL = `
32          INCLUDE PERFETTO MODULE android.gpu.memory;
33          INCLUDE PERFETTO MODULE linux.memory.process;
34
35          DROP TABLE IF EXISTS process_mem_rss_anon_file_shmem_swap_gpu;
36
37          CREATE VIRTUAL TABLE process_mem_rss_anon_file_shmem_swap_gpu
38          USING
39            SPAN_OUTER_JOIN(
40              android_gpu_memory_per_process PARTITIONED upid,
41              memory_rss_and_swap_per_process PARTITIONED upid
42            );
43        `;
44        await ctx.engine.query(RSS_ALL);
45        await addDebugCounterTrack({
46          trace: ctx,
47          data: {
48            sqlSource: `
49                SELECT
50                  ts,
51                  COALESCE(rss_and_swap, 0) + COALESCE(gpu_memory, 0) AS value
52                FROM process_mem_rss_anon_file_shmem_swap_gpu
53                WHERE pid = ${pid}
54            `,
55            columns: ['ts', 'value'],
56          },
57          title: pid + '_rss_anon_file_swap_shmem_gpu',
58        });
59      },
60    });
61  }
62}
63