xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/proto/chrome_system_probes_parser.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2021 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_system_probes_parser.h"
18 
19 #include <cstdint>
20 
21 #include "perfetto/protozero/proto_decoder.h"
22 #include "src/trace_processor/importers/common/event_tracker.h"
23 #include "src/trace_processor/importers/common/process_tracker.h"
24 #include "src/trace_processor/importers/common/track_tracker.h"
25 #include "src/trace_processor/importers/common/tracks.h"
26 #include "src/trace_processor/importers/common/tracks_common.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28 #include "src/trace_processor/types/trace_processor_context.h"
29 
30 #include "protos/perfetto/trace/ps/process_stats.pbzero.h"
31 #include "src/trace_processor/types/variadic.h"
32 
33 namespace perfetto::trace_processor {
34 
ChromeSystemProbesParser(TraceProcessorContext * context)35 ChromeSystemProbesParser::ChromeSystemProbesParser(
36     TraceProcessorContext* context)
37     : context_(context),
38       is_peak_rss_resettable_id_(
39           context->storage->InternString("is_peak_rss_resettable")) {}
40 
ParseProcessStats(int64_t ts,ConstBytes blob)41 void ChromeSystemProbesParser::ParseProcessStats(int64_t ts, ConstBytes blob) {
42   protos::pbzero::ProcessStats::Decoder stats(blob.data, blob.size);
43   for (auto it = stats.processes(); it; ++it) {
44     protozero::ProtoDecoder proc(*it);
45     auto pid_field =
46         proc.FindField(protos::pbzero::ProcessStats::Process::kPidFieldNumber);
47     uint32_t pid = pid_field.as_uint32();
48 
49     for (auto fld = proc.ReadField(); fld.valid(); fld = proc.ReadField()) {
50       using ProcessStats = protos::pbzero::ProcessStats;
51       if (fld.id() == ProcessStats::Process::kIsPeakRssResettableFieldNumber) {
52         UniquePid upid = context_->process_tracker->GetOrCreateProcess(pid);
53         context_->process_tracker->AddArgsTo(upid).AddArg(
54             is_peak_rss_resettable_id_, Variadic::Boolean(fld.as_bool()));
55         continue;
56       }
57       if (fld.id() ==
58           ProcessStats::Process::kChromePrivateFootprintKbFieldNumber) {
59         UniquePid upid = context_->process_tracker->GetOrCreateProcess(pid);
60         TrackId track = context_->track_tracker->InternTrack(
61             tracks::kChromeProcessStatsBlueprint,
62             tracks::Dimensions(upid, "private_footprint_kb"));
63         int64_t value = fld.as_int64() * 1024;
64         context_->event_tracker->PushCounter(ts, static_cast<double>(value),
65                                              track);
66         continue;
67       }
68       if (fld.id() ==
69           ProcessStats::Process::kChromePeakResidentSetKbFieldNumber) {
70         UniquePid upid = context_->process_tracker->GetOrCreateProcess(pid);
71         TrackId track = context_->track_tracker->InternTrack(
72             tracks::kChromeProcessStatsBlueprint,
73             tracks::Dimensions(upid, "peak_resident_set_kb"));
74         int64_t value = fld.as_int64() * 1024;
75         context_->event_tracker->PushCounter(ts, static_cast<double>(value),
76                                              track);
77         continue;
78       }
79     }
80   }
81 }
82 
83 }  // namespace perfetto::trace_processor
84