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 "perfetto/base/logging.h"
20 #include "perfetto/ext/base/string_utils.h"
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/storage/metadata.h"
26 #include "src/trace_processor/types/trace_processor_context.h"
27
28 #include "protos/perfetto/trace/ps/process_stats.pbzero.h"
29 #include "protos/perfetto/trace/ps/process_tree.pbzero.h"
30 #include "protos/perfetto/trace/sys_stats/sys_stats.pbzero.h"
31
32 namespace perfetto {
33 namespace 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 using ProcessStats = protos::pbzero::ProcessStats;
41 proc_stats_process_names_
42 [ProcessStats::Process::kChromePrivateFootprintKbFieldNumber] =
43 context->storage->InternString("chrome.private_footprint_kb");
44 proc_stats_process_names_
45 [ProcessStats::Process::kChromePeakResidentSetKbFieldNumber] =
46 context->storage->InternString("chrome.peak_resident_set_kb");
47 }
48
ParseProcessStats(int64_t ts,ConstBytes blob)49 void ChromeSystemProbesParser::ParseProcessStats(int64_t ts, ConstBytes blob) {
50 protos::pbzero::ProcessStats::Decoder stats(blob.data, blob.size);
51 for (auto it = stats.processes(); it; ++it) {
52 protozero::ProtoDecoder proc(*it);
53 uint32_t pid = 0;
54 for (auto fld = proc.ReadField(); fld.valid(); fld = proc.ReadField()) {
55 if (fld.id() == protos::pbzero::ProcessStats::Process::kPidFieldNumber) {
56 pid = fld.as_uint32();
57 break;
58 }
59 }
60
61 for (auto fld = proc.ReadField(); fld.valid(); fld = proc.ReadField()) {
62 if (fld.id() == protos::pbzero::ProcessStats::Process::
63 kIsPeakRssResettableFieldNumber) {
64 UniquePid upid = context_->process_tracker->GetOrCreateProcess(pid);
65 context_->process_tracker->AddArgsTo(upid).AddArg(
66 is_peak_rss_resettable_id_, Variadic::Boolean(fld.as_bool()));
67 continue;
68 }
69
70 if (fld.id() >= proc_stats_process_names_.size())
71 continue;
72 const StringId& name = proc_stats_process_names_[fld.id()];
73 if (name == StringId::Null())
74 continue;
75 UniquePid upid = context_->process_tracker->GetOrCreateProcess(pid);
76 TrackId track =
77 context_->track_tracker->InternProcessCounterTrack(name, upid);
78 int64_t value = fld.as_int64() * 1024;
79 context_->event_tracker->PushCounter(ts, static_cast<double>(value),
80 track);
81 }
82 }
83 }
84
85 } // namespace trace_processor
86 } // namespace perfetto
87