1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/trace_processor/importers/proto/chrome_string_lookup.h"
18 
19 #include "perfetto/ext/base/utils.h"
20 #include "protos/perfetto/trace/track_event/chrome_legacy_ipc.pbzero.h"
21 #include "protos/perfetto/trace/track_event/chrome_process_descriptor.pbzero.h"
22 #include "protos/perfetto/trace/track_event/chrome_thread_descriptor.pbzero.h"
23 #include "src/trace_processor/storage/trace_storage.h"
24 
25 using ::perfetto::protos::pbzero::ChromeProcessDescriptor;
26 using ::perfetto::protos::pbzero::ChromeThreadDescriptor;
27 
28 namespace perfetto {
29 namespace trace_processor {
30 
31 namespace {
32 
33 struct ProcessName {
34   protos::pbzero::ChromeProcessDescriptor::ProcessType type;
35   const char* name;
36 };
37 
38 constexpr ProcessName kProcessNames[] = {
39     {ChromeProcessDescriptor::PROCESS_UNSPECIFIED, nullptr},
40     {ChromeProcessDescriptor::PROCESS_BROWSER, "Browser"},
41     {ChromeProcessDescriptor::PROCESS_RENDERER, "Renderer"},
42     {ChromeProcessDescriptor::PROCESS_UTILITY, "Utility"},
43     {ChromeProcessDescriptor::PROCESS_ZYGOTE, "SandboxHelper"},
44     {ChromeProcessDescriptor::PROCESS_GPU, "Gpu"},
45     {ChromeProcessDescriptor::PROCESS_PPAPI_PLUGIN, "PpapiPlugin"},
46     {ChromeProcessDescriptor::PROCESS_PPAPI_BROKER, "PpapiBroker"},
47     {ChromeProcessDescriptor::PROCESS_SERVICE_NETWORK,
48      "Service: network.mojom.NetworkService"},
49     {ChromeProcessDescriptor::PROCESS_SERVICE_TRACING,
50      "Service: tracing.mojom.TracingService"},
51     {ChromeProcessDescriptor::PROCESS_SERVICE_STORAGE,
52      "Service: storage.mojom.StorageService"},
53     {ChromeProcessDescriptor::PROCESS_SERVICE_AUDIO,
54      "Service: audio.mojom.AudioService"},
55     {ChromeProcessDescriptor::PROCESS_SERVICE_DATA_DECODER,
56      "Service: data_decoder.mojom.DataDecoderService"},
57     {ChromeProcessDescriptor::PROCESS_SERVICE_UTIL_WIN,
58      "Service: chrome.mojom.UtilWin"},
59     {ChromeProcessDescriptor::PROCESS_SERVICE_PROXY_RESOLVER,
60      "Service: proxy_resolver.mojom.ProxyResolverFactory"},
61     {ChromeProcessDescriptor::PROCESS_SERVICE_CDM,
62      "Service: media.mojom.CdmService"},
63     {ChromeProcessDescriptor::PROCESS_SERVICE_VIDEO_CAPTURE,
64      "Service: video_capture.mojom.VideoCaptureService"},
65     {ChromeProcessDescriptor::PROCESS_SERVICE_UNZIPPER,
66      "Service: unzip.mojom.Unzipper"},
67     {ChromeProcessDescriptor::PROCESS_SERVICE_MIRRORING,
68      "Service: mirroring.mojom.MirroringService"},
69     {ChromeProcessDescriptor::PROCESS_SERVICE_FILEPATCHER,
70      "Service: patch.mojom.FilePatcher"},
71     {ChromeProcessDescriptor::PROCESS_SERVICE_TTS,
72      "Service: chromeos.tts.mojom.TtsService"},
73     {ChromeProcessDescriptor::PROCESS_SERVICE_PRINTING,
74      "Service: printing.mojom.PrintingService"},
75     {ChromeProcessDescriptor::PROCESS_SERVICE_QUARANTINE,
76      "Service: quarantine.mojom.Quarantine"},
77     {ChromeProcessDescriptor::PROCESS_SERVICE_CROS_LOCALSEARCH,
78      "Service: chromeos.local_search_service.mojom.LocalSearchService"},
79     {ChromeProcessDescriptor::PROCESS_SERVICE_CROS_ASSISTANT_AUDIO_DECODER,
80      "Service: chromeos.assistant.mojom.AssistantAudioDecoderFactory"},
81     {ChromeProcessDescriptor::PROCESS_SERVICE_FILEUTIL,
82      "Service: chrome.mojom.FileUtilService"},
83     {ChromeProcessDescriptor::PROCESS_SERVICE_PRINTCOMPOSITOR,
84      "Service: printing.mojom.PrintCompositor"},
85     {ChromeProcessDescriptor::PROCESS_SERVICE_PAINTPREVIEW,
86      "Service: paint_preview.mojom.PaintPreviewCompositorCollection"},
87     {ChromeProcessDescriptor::PROCESS_SERVICE_SPEECHRECOGNITION,
88      "Service: media.mojom.SpeechRecognitionService"},
89     {ChromeProcessDescriptor::PROCESS_SERVICE_XRDEVICE,
90      "Service: device.mojom.XRDeviceService"},
91     {ChromeProcessDescriptor::PROCESS_SERVICE_READICON,
92      "Service: chrome.mojom.UtilReadIcon"},
93     {ChromeProcessDescriptor::PROCESS_SERVICE_LANGUAGEDETECTION,
94      "Service: language_detection.mojom.LanguageDetectionService"},
95     {ChromeProcessDescriptor::PROCESS_SERVICE_SHARING,
96      "Service: sharing.mojom.Sharing"},
97     {ChromeProcessDescriptor::PROCESS_SERVICE_MEDIAPARSER,
98      "Service: chrome.mojom.MediaParserFactory"},
99     {ChromeProcessDescriptor::PROCESS_SERVICE_QRCODEGENERATOR,
100      "Service: qrcode_generator.mojom.QRCodeGeneratorService"},
101     {ChromeProcessDescriptor::PROCESS_SERVICE_PROFILEIMPORT,
102      "Service: chrome.mojom.ProfileImport"},
103     {ChromeProcessDescriptor::PROCESS_SERVICE_IME,
104      "Service: chromeos.ime.mojom.ImeService"},
105     {ChromeProcessDescriptor::PROCESS_SERVICE_RECORDING,
106      "Service: recording.mojom.RecordingService"},
107     {ChromeProcessDescriptor::PROCESS_SERVICE_SHAPEDETECTION,
108      "Service: shape_detection.mojom.ShapeDetectionService"},
109 };
110 
111 struct ThreadName {
112   protos::pbzero::ChromeThreadDescriptor::ThreadType type;
113   const char* name;
114 };
115 
116 constexpr ThreadName kThreadNames[] = {
117     {ChromeThreadDescriptor::THREAD_UNSPECIFIED, nullptr},
118     {ChromeThreadDescriptor::THREAD_MAIN, "CrProcessMain"},
119     {ChromeThreadDescriptor::THREAD_IO, "ChromeIOThread"},
120     {ChromeThreadDescriptor::THREAD_NETWORK_SERVICE, "NetworkService"},
121     {ChromeThreadDescriptor::THREAD_POOL_BG_WORKER,
122      "ThreadPoolBackgroundWorker&"},
123     {ChromeThreadDescriptor::THREAD_POOL_FG_WORKER,
124      "ThreadPoolForegroundWorker&"},
125     {ChromeThreadDescriptor::THREAD_POOL_BG_BLOCKING,
126      "ThreadPoolSingleThreadBackgroundBlocking&"},
127     {ChromeThreadDescriptor::THREAD_POOL_FG_BLOCKING,
128      "ThreadPoolSingleThreadForegroundBlocking&"},
129     {ChromeThreadDescriptor::THREAD_POOL_SERVICE, "ThreadPoolService"},
130     {ChromeThreadDescriptor::THREAD_COMPOSITOR, "Compositor"},
131     {ChromeThreadDescriptor::THREAD_VIZ_COMPOSITOR, "VizCompositorThread"},
132     {ChromeThreadDescriptor::THREAD_COMPOSITOR_WORKER, "CompositorTileWorker&"},
133     {ChromeThreadDescriptor::THREAD_SERVICE_WORKER, "ServiceWorkerThread&"},
134     {ChromeThreadDescriptor::THREAD_MEMORY_INFRA, "MemoryInfra"},
135     {ChromeThreadDescriptor::THREAD_SAMPLING_PROFILER, "StackSamplingProfiler"},
136 
137     {ChromeThreadDescriptor::THREAD_BROWSER_MAIN, "CrBrowserMain"},
138     {ChromeThreadDescriptor::THREAD_RENDERER_MAIN, "CrRendererMain"},
139     {ChromeThreadDescriptor::THREAD_CHILD_IO, "Chrome_ChildIOThread"},
140     {ChromeThreadDescriptor::THREAD_BROWSER_IO, "Chrome_IOThread"},
141     {ChromeThreadDescriptor::THREAD_UTILITY_MAIN, "CrUtilityMain"},
142     {ChromeThreadDescriptor::THREAD_GPU_MAIN, "CrGpuMain"},
143     {ChromeThreadDescriptor::THREAD_CACHE_BLOCKFILE, "CacheThread_BlockFile"},
144     {ChromeThreadDescriptor::ChromeThreadDescriptor::THREAD_MEDIA, "Media"},
145     {ChromeThreadDescriptor::THREAD_AUDIO_OUTPUTDEVICE, "AudioOutputDevice"},
146     {ChromeThreadDescriptor::THREAD_GPU_MEMORY, "GpuMemoryThread"},
147     {ChromeThreadDescriptor::THREAD_GPU_VSYNC, "GpuVSyncThread"},
148     {ChromeThreadDescriptor::THREAD_DXA_VIDEODECODER, "DXVAVideoDecoderThread"},
149     {ChromeThreadDescriptor::THREAD_BROWSER_WATCHDOG, "BrowserWatchdog"},
150     {
151         ChromeThreadDescriptor::THREAD_WEBRTC_NETWORK,
152         "WebRTC_Network",
153     },
154     {ChromeThreadDescriptor::THREAD_WINDOW_OWNER, "Window owner thread"},
155     {ChromeThreadDescriptor::THREAD_WEBRTC_SIGNALING, "WebRTC_Signaling"},
156     {ChromeThreadDescriptor::THREAD_PPAPI_MAIN, "CrPPAPIMain"},
157     {ChromeThreadDescriptor::THREAD_GPU_WATCHDOG, "GpuWatchdog"},
158     {ChromeThreadDescriptor::THREAD_SWAPPER, "swapper"},
159     {ChromeThreadDescriptor::THREAD_GAMEPAD_POLLING, "Gamepad polling thread"},
160     {ChromeThreadDescriptor::THREAD_AUDIO_INPUTDEVICE, "AudioInputDevice"},
161     {ChromeThreadDescriptor::THREAD_WEBRTC_WORKER, "WebRTC_Worker"},
162     {ChromeThreadDescriptor::THREAD_WEBCRYPTO, "WebCrypto"},
163     {ChromeThreadDescriptor::THREAD_DATABASE, "Database thread"},
164     {ChromeThreadDescriptor::THREAD_PROXYRESOLVER, "Proxy Resolver"},
165     {ChromeThreadDescriptor::THREAD_DEVTOOLSADB, "Chrome_DevToolsADBThread"},
166     {ChromeThreadDescriptor::THREAD_NETWORKCONFIGWATCHER,
167      "NetworkConfigWatcher"},
168     {ChromeThreadDescriptor::THREAD_WASAPI_RENDER, "wasapi_render_thread"},
169 };
170 
171 }  // namespace
172 
ChromeStringLookup(TraceStorage * storage)173 ChromeStringLookup::ChromeStringLookup(TraceStorage* storage) {
174   for (uint32_t i = 0; i < base::ArraySize(kProcessNames); i++) {
175     chrome_process_name_ids_[kProcessNames[i].type] =
176         kProcessNames[i].name ? storage->InternString(kProcessNames[i].name)
177                               : kNullStringId;
178   }
179 
180   for (uint32_t i = 0; i < base::ArraySize(kThreadNames); i++) {
181     chrome_thread_name_ids_[kThreadNames[i].type] =
182         kThreadNames[i].name ? storage->InternString(kThreadNames[i].name)
183                              : kNullStringId;
184   }
185 }
186 
GetProcessName(int32_t process_type) const187 StringId ChromeStringLookup::GetProcessName(int32_t process_type) const {
188   auto process_name_it = chrome_process_name_ids_.find(process_type);
189   if (process_name_it != chrome_process_name_ids_.end())
190     return process_name_it->second;
191 
192   PERFETTO_DLOG("GetProcessName error: Unknown Chrome process type %u",
193                 process_type);
194   return kNullStringId;
195 }
196 
GetThreadName(int32_t thread_type) const197 StringId ChromeStringLookup::GetThreadName(int32_t thread_type) const {
198   auto thread_name_it = chrome_thread_name_ids_.find(thread_type);
199   if (thread_name_it != chrome_thread_name_ids_.end())
200     return thread_name_it->second;
201 
202   PERFETTO_DLOG("GetThreadName error: Unknown Chrome thread type %u",
203                 thread_type);
204   return kNullStringId;
205 }
206 
207 }  // namespace trace_processor
208 }  // namespace perfetto
209