• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {Plugin, PluginContextTrace, PluginDescriptor} from '../../public';
16
17import {addDebugCounterTrack} from '../../frontend/debug_tracks/debug_tracks';
18
19class PixelMemory implements Plugin {
20  async onTraceLoad(ctx: PluginContextTrace): Promise<void> {
21    ctx.registerCommand({
22      id: 'dev.perfetto.PixelMemory#ShowTotalMemory',
23      name: 'Add tracks: show a process total memory',
24      callback: async (pid) => {
25        if (pid === undefined) {
26          pid = prompt('Enter a process pid', '');
27          if (pid === null) return;
28        }
29        const RSS_ALL = `
30          INCLUDE PERFETTO MODULE memory.linux.process;
31          INCLUDE PERFETTO MODULE memory.android.gpu;
32
33          DROP TABLE IF EXISTS process_mem_rss_anon_file_shmem_swap_gpu;
34
35          CREATE VIRTUAL TABLE process_mem_rss_anon_file_shmem_swap_gpu
36          USING
37            SPAN_OUTER_JOIN(
38              memory_gpu_per_process PARTITIONED upid, memory_rss_and_swap_per_process PARTITIONED upid);
39        `;
40        await ctx.engine.query(RSS_ALL);
41        await addDebugCounterTrack(
42          ctx,
43          {
44            sqlSource: `
45                SELECT
46                  ts,
47                  COALESCE(rss_and_swap, 0) + COALESCE(gpu_memory, 0) AS value
48                FROM process_mem_rss_anon_file_shmem_swap_gpu
49                WHERE pid = ${pid}
50            `,
51            columns: ['ts', 'value'],
52          },
53          pid + '_rss_anon_file_swap_shmem_gpu',
54          {ts: 'ts', value: 'value'},
55        );
56      },
57    });
58  }
59}
60
61export const plugin: PluginDescriptor = {
62  pluginId: 'com.google.PixelMemory',
63  plugin: PixelMemory,
64};
65