Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions src/trace_redaction/redact_sched_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,12 @@ base::Status RedactSchedEvents::OnCompSched(
comp_sched.has_switch_next_comm_index(),
};

// The final intern table that will only contain referenced entries.
std::vector<std::string> dest_intern_table;

if (std::any_of(has_switch_fields.begin(), has_switch_fields.end(), IsTrue)) {
RETURN_IF_ERROR(
OnCompSchedSwitch(context, cpu, comp_sched, &intern_table, message));
RETURN_IF_ERROR(OnCompSchedSwitch(context, cpu, comp_sched, &intern_table,
&dest_intern_table, message));
}

std::array<bool, 6> has_waking_fields = {
Expand All @@ -381,14 +384,14 @@ base::Status RedactSchedEvents::OnCompSched(
};

if (std::any_of(has_waking_fields.begin(), has_waking_fields.end(), IsTrue)) {
RETURN_IF_ERROR(
OnCompactSchedWaking(context, comp_sched, &intern_table, message));
RETURN_IF_ERROR(OnCompactSchedWaking(context, comp_sched, &intern_table,
&dest_intern_table, message));
}

// IMPORTANT: The intern table can only be added after switch and waking
// because switch and/or waking can/will modify the intern table.
for (auto view : intern_table.values()) {
message->add_intern_table(view.data(), view.size());
for (const std::string& str : dest_intern_table) {
message->add_intern_table(str.c_str(), str.size());
}

return base::OkStatus();
Expand All @@ -398,7 +401,8 @@ base::Status RedactSchedEvents::OnCompSchedSwitch(
const Context& context,
int32_t cpu,
protos::pbzero::FtraceEventBundle::CompactSched::Decoder& comp_sched,
InternTable* intern_table,
InternTable* source_intern_table,
std::vector<std::string>* target_intern_table,
protos::pbzero::FtraceEventBundle::CompactSched* message) const {
PERFETTO_DCHECK(modifier_);
PERFETTO_DCHECK(message);
Expand Down Expand Up @@ -439,20 +443,23 @@ base::Status RedactSchedEvents::OnCompSchedSwitch(
auto pid = *it_pid;

auto comm_index = *it_comm;
auto comm = intern_table->Find(comm_index);
auto comm = source_intern_table->Find(comm_index);

scratch_str.assign(comm);

modifier_->Modify(context, ts, cpu, &pid, &scratch_str);

auto found = intern_table->Push(scratch_str.data(), scratch_str.size());

if (found < 0) {
return base::ErrStatus(
"RedactSchedEvents: failed to insert string into intern table.");
// Add the string to the intern table if it is not already there, otherwise,
// reuse it.
auto found = std::find(target_intern_table->begin(),
target_intern_table->end(), scratch_str);
if (found == target_intern_table->end()) {
target_intern_table->push_back(scratch_str);
packed_comm.Append(target_intern_table->size() - 1);
} else {
packed_comm.Append(std::distance(target_intern_table->begin(), found));
}

packed_comm.Append(found);
packed_pid.Append(pid);

++it_ts;
Expand Down Expand Up @@ -516,7 +523,8 @@ base::Status RedactSchedEvents::OnCompSchedSwitch(
base::Status RedactSchedEvents::OnCompactSchedWaking(
const Context& context,
protos::pbzero::FtraceEventBundle::CompactSched::Decoder& compact_sched,
InternTable* intern_table,
InternTable* source_intern_table,
std::vector<std::string>* target_intern_table,
protos::pbzero::FtraceEventBundle::CompactSched* compact_sched_message)
const {
protozero::PackedVarInt var_comm_index;
Expand Down Expand Up @@ -552,44 +560,42 @@ base::Status RedactSchedEvents::OnCompactSchedWaking(

std::string comm;

std::array<bool, 7> parse_errors = {!compact_sched.has_intern_table(),
false,
false,
false,
false,
false,
false};

std::array<bool, 6> parse_errors = {false, false, false, false, false, false};
// A note on readability, because the waking iterators are the primary focus,
// they won't have a "waking" prefix.
auto it_comm_index = compact_sched.waking_comm_index(&parse_errors.at(1));
auto it_common_flags = compact_sched.waking_common_flags(&parse_errors.at(2));
auto it_pid = compact_sched.waking_pid(&parse_errors.at(3));
auto it_prio = compact_sched.waking_prio(&parse_errors.at(4));
auto it_target_cpu = compact_sched.waking_target_cpu(&parse_errors.at(5));
auto it_timestamp = compact_sched.waking_timestamp(&parse_errors.at(6));
auto it_comm_index = compact_sched.waking_comm_index(&parse_errors.at(0));
auto it_common_flags = compact_sched.waking_common_flags(&parse_errors.at(1));
auto it_pid = compact_sched.waking_pid(&parse_errors.at(2));
auto it_prio = compact_sched.waking_prio(&parse_errors.at(3));
auto it_target_cpu = compact_sched.waking_target_cpu(&parse_errors.at(4));
auto it_timestamp = compact_sched.waking_timestamp(&parse_errors.at(5));

while (it_comm_index && it_common_flags && it_pid && it_prio &&
it_target_cpu && it_timestamp) {
ts_bucket += *it_timestamp; // add time to the bucket
ts_absolute += *it_timestamp;

if (waking_filter_->Includes(context, ts_absolute, *it_pid)) {
// Now that the waking event will be kept, it can be modified using the
// same rules as switch events.
auto pid = *it_pid;
comm.assign(intern_table->Find(*it_comm_index));
comm.assign(source_intern_table->Find(*it_comm_index));
modifier_->Modify(context, ts_absolute, *it_target_cpu, &pid, &comm);

auto comm_it = intern_table->Push(comm.data(), comm.size());
auto found = std::find(target_intern_table->begin(),
target_intern_table->end(), comm);
if (found == target_intern_table->end()) {
target_intern_table->push_back(comm);
var_comm_index.Append(target_intern_table->size() - 1);
} else {
var_comm_index.Append(
std::distance(target_intern_table->begin(), found));
}

var_comm_index.Append(comm_it);
var_common_flags.Append(*it_common_flags);
var_pid.Append(pid);
var_prio.Append(*it_prio);
var_target_cpu.Append(*it_target_cpu);
var_timestamp.Append(ts_bucket);

ts_bucket = 0; // drain the whole bucket.
}

Expand Down
6 changes: 4 additions & 2 deletions src/trace_redaction/redact_sched_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ class RedactSchedEvents : public TransformPrimitive {
const Context& context,
int32_t cpu,
protos::pbzero::FtraceEventBundle::CompactSched::Decoder& comp_sched,
InternTable* intern_table,
InternTable* source_intern_table,
std::vector<std::string>* target_intern_table,
protos::pbzero::FtraceEventBundle::CompactSched* message) const;

base::Status OnCompactSchedWaking(
const Context& context,
protos::pbzero::FtraceEventBundle::CompactSched::Decoder& compact_sched,
InternTable* intern_table,
InternTable* source_intern_table,
std::vector<std::string>* target_intern_table,
protos::pbzero::FtraceEventBundle::CompactSched* compact_sched_message)
const;

Expand Down
Loading
Loading