• 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 protos from '../../../protos';
16import {ADV_FTRACE_PROBE_ID, ADV_PROC_ASSOC_PROBE_ID} from './advanced';
17import {RecordSubpage, RecordProbe} from '../config/config_interfaces';
18import {TraceConfigBuilder} from '../config/trace_config_builder';
19import {POLL_INTERVAL_SLIDER, Slider} from './widgets/slider';
20
21const PROC_POLL_DS = 'linux.sys_stats';
22
23export function cpuRecordSection(): RecordSubpage {
24  return {
25    kind: 'PROBES_PAGE',
26    id: 'cpu',
27    title: 'CPU',
28    subtitle: 'CPU usage, scheduling, wakeups',
29    icon: 'subtitles',
30    probes: [cpuUsage(), sched(), cpuFreq(), syscalls()],
31  };
32}
33
34function cpuUsage(): RecordProbe {
35  const settings = {pollMs: new Slider(POLL_INTERVAL_SLIDER)};
36  return {
37    id: 'cpu_usage',
38    image: 'rec_cpu_coarse.png',
39    title: 'Coarse CPU usage counter',
40    supportedPlatforms: ['ANDROID', 'LINUX', 'CHROME_OS'],
41    description:
42      'Lightweight polling of CPU usage counters via /proc/stat. ' +
43      'Allows to periodically monitor CPU usage.',
44    dependencies: [ADV_PROC_ASSOC_PROBE_ID],
45    settings,
46    genConfig: function (tc: TraceConfigBuilder) {
47      const cfg = tc.addDataSource(PROC_POLL_DS);
48      cfg.sysStatsConfig ??= {};
49      cfg.sysStatsConfig.statPeriodMs = settings.pollMs.value;
50      cfg.sysStatsConfig.statCounters ??= [];
51      cfg.sysStatsConfig.statCounters.push(
52        protos.SysStatsConfig.StatCounters.STAT_CPU_TIMES,
53        protos.SysStatsConfig.StatCounters.STAT_FORK_COUNT,
54      );
55    },
56  };
57}
58
59function sched(): RecordProbe {
60  return {
61    id: 'cpu_sched',
62    image: 'rec_cpu_fine.png',
63    title: 'Scheduling details',
64    supportedPlatforms: ['ANDROID', 'LINUX', 'CHROME_OS'],
65    dependencies: [ADV_FTRACE_PROBE_ID, ADV_PROC_ASSOC_PROBE_ID],
66    description: 'Enables high-detailed tracking of scheduling events',
67    genConfig: function (tc: TraceConfigBuilder) {
68      tc.addFtraceEvents(
69        'sched/sched_switch',
70        'power/suspend_resume',
71        'sched/sched_blocked_reason',
72        'sched/sched_wakeup',
73        'sched/sched_wakeup_new',
74        'sched/sched_waking',
75        'sched/sched_process_exit',
76        'sched/sched_process_free',
77        'task/task_newtask',
78        'task/task_rename',
79      );
80    },
81  };
82}
83
84function cpuFreq(): RecordProbe {
85  const settings = {pollMs: new Slider(POLL_INTERVAL_SLIDER)};
86  return {
87    id: 'cpu_freq',
88    image: 'rec_cpu_freq.png',
89    title: 'CPU frequency and idle states',
90    description:
91      'Records cpu frequency and idle state changes via ftrace and sysfs',
92    supportedPlatforms: ['ANDROID', 'LINUX', 'CHROME_OS'],
93    settings,
94    genConfig: function (tc: TraceConfigBuilder) {
95      const cfg = tc.addDataSource(PROC_POLL_DS);
96      cfg.sysStatsConfig ??= {};
97      cfg.sysStatsConfig.cpufreqPeriodMs = settings.pollMs.value;
98      tc.addFtraceEvents(
99        'power/cpu_frequency',
100        'power/cpu_idle',
101        'power/suspend_resume',
102      );
103    },
104  };
105}
106
107function syscalls(): RecordProbe {
108  return {
109    id: 'cpu_syscalls',
110    image: 'rec_syscalls.png',
111    title: 'Syscalls',
112    description:
113      'Tracks the enter and exit of all syscalls. On Android' +
114      'requires a userdebug or eng build.',
115    supportedPlatforms: ['ANDROID', 'LINUX', 'CHROME_OS'],
116    genConfig: function (tc: TraceConfigBuilder) {
117      tc.addFtraceEvents(
118        'raw_syscalls/sys_enter', //
119        'raw_syscalls/sys_exit', //
120      );
121    },
122  };
123}
124