1 /* 2 * Copyright (C) 2024 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 #ifndef SRC_TRACE_REDACTION_REDACT_SCHED_EVENTS_H_ 18 #define SRC_TRACE_REDACTION_REDACT_SCHED_EVENTS_H_ 19 20 #include "src/trace_redaction/filtering.h" 21 #include "src/trace_redaction/modify.h" 22 #include "src/trace_redaction/trace_redaction_framework.h" 23 24 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h" 25 #include "protos/perfetto/trace/ftrace/sched.pbzero.h" 26 27 namespace perfetto::trace_redaction { 28 29 class InternTable { 30 public: 31 int64_t Push(const char* data, size_t size); 32 33 std::string_view Find(size_t index) const; 34 values()35 const std::vector<std::string_view>& values() const { 36 return interned_comms_; 37 } 38 39 private: 40 constexpr static size_t kExpectedCommLength = 16; 41 constexpr static size_t kMaxElements = 4096; 42 43 std::array<char, kMaxElements * kExpectedCommLength> comms_; 44 size_t comms_length_ = 0; 45 46 std::vector<std::string_view> interned_comms_; 47 }; 48 49 class RedactSchedEvents : public TransformPrimitive { 50 public: 51 base::Status Transform(const Context& context, 52 std::string* packet) const override; 53 54 template <class Modifier> emplace_modifier()55 void emplace_modifier() { 56 modifier_ = std::make_unique<Modifier>(); 57 } 58 59 template <class Filter> emplace_waking_filter()60 void emplace_waking_filter() { 61 waking_filter_ = std::make_unique<Filter>(); 62 } 63 64 private: 65 base::Status OnFtraceEvents(const Context& context, 66 protozero::Field ftrace_events, 67 protos::pbzero::FtraceEventBundle* message) const; 68 69 base::Status OnFtraceEvent(const Context& context, 70 int32_t cpu, 71 protozero::Field ftrace_event, 72 protos::pbzero::FtraceEvent* message) const; 73 74 // scratch_str is a reusable string, allowing comm modifications to be done in 75 // a shared buffer, avoiding allocations when processing ftrace events. 76 base::Status OnFtraceEventSwitch( 77 const Context& context, 78 uint64_t ts, 79 int32_t cpu, 80 protos::pbzero::SchedSwitchFtraceEvent::Decoder& sched_switch, 81 std::string* scratch_str, 82 protos::pbzero::SchedSwitchFtraceEvent* message) const; 83 84 // Unlike other On* functions, this one takes the parent message, allowing it 85 // to optionally add the body. This is what allows the waking event to be 86 // removed. 87 base::Status OnFtraceEventWaking( 88 const Context& context, 89 uint64_t ts, 90 int32_t cpu, 91 protos::pbzero::SchedWakingFtraceEvent::Decoder& sched_waking, 92 std::string* scratch_str, 93 protos::pbzero::FtraceEvent* parent_message) const; 94 95 base::Status OnCompSched( 96 const Context& context, 97 int32_t cpu, 98 protos::pbzero::FtraceEventBundle::CompactSched::Decoder& comp_sched, 99 protos::pbzero::FtraceEventBundle::CompactSched* message) const; 100 101 base::Status OnCompSchedSwitch( 102 const Context& context, 103 int32_t cpu, 104 protos::pbzero::FtraceEventBundle::CompactSched::Decoder& comp_sched, 105 InternTable* intern_table, 106 protos::pbzero::FtraceEventBundle::CompactSched* message) const; 107 108 base::Status OnCompactSchedWaking( 109 const Context& context, 110 protos::pbzero::FtraceEventBundle::CompactSched::Decoder& compact_sched, 111 InternTable* intern_table, 112 protos::pbzero::FtraceEventBundle::CompactSched* compact_sched_message) 113 const; 114 115 std::unique_ptr<PidCommModifier> modifier_; 116 std::unique_ptr<PidFilter> waking_filter_; 117 }; 118 119 } // namespace perfetto::trace_redaction 120 121 #endif // SRC_TRACE_REDACTION_REDACT_SCHED_EVENTS_H_ 122