Skip to content

Commit c3c236a

Browse files
committed
swith to std::optional & remove not needed description.
1 parent f5c7fa9 commit c3c236a

File tree

3 files changed

+23
-42
lines changed

3 files changed

+23
-42
lines changed

src/traced/probes/ftrace/tracefs.cc

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,30 +450,30 @@ size_t Tracefs::NumberOfCpus() const {
450450
return num_cpus;
451451
}
452452

453-
bool Tracefs::GetOfflineCpus(std::vector<uint32_t>* offline_cpus) {
453+
std::optional<std::vector<uint32_t>> Tracefs::GetOfflineCpus() {
454454
std::string offline_cpus_str;
455455
if (!ReadFile("/sys/devices/system/cpu/offline", &offline_cpus_str)) {
456456
PERFETTO_ELOG("Failed to read offline cpus file");
457-
return false;
457+
return std::nullopt;
458458
}
459459
offline_cpus_str = base::TrimWhitespace(offline_cpus_str);
460460

461461
// The offline cpus file contains a list of comma-separated CPU ranges.
462462
// Each range is either a single CPU or a range of CPUs, e.g. "0-3,5,7-9".
463463
// Source: https://docs.kernel.org/admin-guide/cputopology.html
464-
std::vector<uint32_t> offline_cpus_tmp;
464+
std::vector<uint32_t> offline_cpus;
465465
for (base::StringSplitter ss(offline_cpus_str, ','); ss.Next();) {
466466
base::StringView offline_cpu_range(ss.cur_token(), ss.cur_token_size());
467467
size_t dash_pos = offline_cpu_range.find('-');
468468
if (dash_pos == base::StringView::npos) {
469469
// Single CPU in the format of "%d".
470470
std::optional<uint32_t> cpu = base::StringViewToUInt32(offline_cpu_range);
471471
if (cpu.has_value()) {
472-
offline_cpus_tmp.push_back(cpu.value());
472+
offline_cpus.push_back(cpu.value());
473473
} else {
474474
PERFETTO_ELOG("Failed to parse single CPU from offline CPU range: %s",
475475
offline_cpu_range.data());
476-
return false;
476+
return std::nullopt;
477477
}
478478
} else {
479479
// Range of CPUs in the format of "%d-%d".
@@ -483,17 +483,16 @@ bool Tracefs::GetOfflineCpus(std::vector<uint32_t>* offline_cpus) {
483483
base::StringViewToUInt32(offline_cpu_range.substr(dash_pos + 1));
484484
if (start_cpu.has_value() && end_cpu.has_value()) {
485485
for (auto cpu = start_cpu.value(); cpu <= end_cpu.value(); ++cpu) {
486-
offline_cpus_tmp.push_back(cpu);
486+
offline_cpus.push_back(cpu);
487487
}
488488
} else {
489489
PERFETTO_ELOG("Failed to parse CPU range from offline CPU range: %s",
490490
offline_cpu_range.data());
491-
return false;
491+
return std::nullopt;
492492
}
493493
}
494494
}
495-
*offline_cpus = std::move(offline_cpus_tmp);
496-
return true;
495+
return offline_cpus;
497496
}
498497

499498
void Tracefs::ClearTrace() {
@@ -506,17 +505,12 @@ void Tracefs::ClearTrace() {
506505
// In case some of the CPUs were not online, their buffer needs to be
507506
// cleared manually.
508507
//
509-
// Note: There is a small time gap between when we clear the trace file and
510-
// when we get the offline cpus. This introduces a small chance that some CPUs
511-
// are missed if they came online in between those two points. This is a
512-
// best-effort approach to keep the logic simple.
513-
//
514508
// We cannot use PERFETTO_CHECK as we might get a permission denied error
515509
// on Android. The permissions to these files are configured in
516510
// platform/framework/native/cmds/atrace/atrace.rc.
517-
std::vector<uint32_t> offline_cpus;
518-
if (GetOfflineCpus(&offline_cpus)) {
519-
for (const auto& cpu : offline_cpus) {
511+
std::optional<std::vector<uint32_t>> offline_cpus = GetOfflineCpus();
512+
if (offline_cpus.has_value()) {
513+
for (const auto& cpu : offline_cpus.value()) {
520514
ClearPerCpuTrace(cpu);
521515
}
522516
} else {

src/traced/probes/ftrace/tracefs.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SRC_TRACED_PROBES_FTRACE_TRACEFS_H_
1919

2020
#include <memory>
21+
#include <optional>
2122
#include <set>
2223
#include <string>
2324
#include <vector>
@@ -152,9 +153,10 @@ class Tracefs {
152153
// This will match the number of tracing/per_cpu/cpuXX directories.
153154
size_t virtual NumberOfCpus() const;
154155

155-
// Parses the list of offline CPUs from /sys/devices/system/cpu/offline into
156-
// |offline_cpus|. Returns true on success.
157-
bool GetOfflineCpus(std::vector<uint32_t>* offline_cpus);
156+
// Parses the list of offline CPUs from "/sys/devices/system/cpu/offline" and
157+
// returns them as a vector if successful, or std::nullopt if any failure in
158+
// reading or parsing.
159+
std::optional<std::vector<uint32_t>> GetOfflineCpus();
158160

159161
// Clears the trace buffers for all CPUs. Blocks until this is done.
160162
void ClearTrace();

src/traced/probes/ftrace/tracefs_unittest.cc

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ using testing::AnyNumber;
2727
using testing::DoAll;
2828
using testing::ElementsAre;
2929
using testing::IsEmpty;
30+
using testing::Optional;
3031
using testing::Return;
3132
using testing::SetArgPointee;
3233
using testing::UnorderedElementsAre;
@@ -153,52 +154,36 @@ TEST(TracefsTest, ReadBufferSizeInPages) {
153154

154155
TEST(TracefsTest, GetOfflineCpus) {
155156
MockTracefs ftrace;
156-
std::vector<uint32_t> cpus;
157157

158158
// ReadFile fails.
159159
EXPECT_CALL(ftrace, ReadFile("/sys/devices/system/cpu/offline", _))
160160
.WillOnce(Return(false));
161-
EXPECT_FALSE(ftrace.Tracefs::GetOfflineCpus(&cpus));
162-
EXPECT_THAT(cpus, IsEmpty());
163-
testing::Mock::VerifyAndClearExpectations(&ftrace);
161+
EXPECT_EQ(ftrace.Tracefs::GetOfflineCpus(), std::nullopt);
164162

165163
// Invalid value.
166164
EXPECT_CALL(ftrace, ReadFile("/sys/devices/system/cpu/offline", _))
167165
.WillOnce(DoAll(SetArgPointee<1>("1,a,3"), Return(true)));
168-
EXPECT_FALSE(ftrace.Tracefs::GetOfflineCpus(&cpus));
169-
EXPECT_THAT(cpus, IsEmpty());
170-
testing::Mock::VerifyAndClearExpectations(&ftrace);
166+
EXPECT_EQ(ftrace.Tracefs::GetOfflineCpus(), std::nullopt);
171167

172168
// Empty offline CPU list.
173169
EXPECT_CALL(ftrace, ReadFile("/sys/devices/system/cpu/offline", _))
174170
.WillOnce(DoAll(SetArgPointee<1>(""), Return(true)));
175-
EXPECT_TRUE(ftrace.Tracefs::GetOfflineCpus(&cpus));
176-
EXPECT_THAT(cpus, IsEmpty());
177-
testing::Mock::VerifyAndClearExpectations(&ftrace);
171+
EXPECT_THAT(ftrace.Tracefs::GetOfflineCpus(), Optional(IsEmpty()));
178172

179173
// Comma-separated list of single offline CPUs.
180174
EXPECT_CALL(ftrace, ReadFile("/sys/devices/system/cpu/offline", _))
181175
.WillOnce(DoAll(SetArgPointee<1>("1,3\n"), Return(true)));
182-
EXPECT_TRUE(ftrace.Tracefs::GetOfflineCpus(&cpus));
183-
EXPECT_THAT(cpus, ElementsAre(1, 3));
184-
cpus.clear();
185-
testing::Mock::VerifyAndClearExpectations(&ftrace);
176+
EXPECT_THAT(ftrace.GetOfflineCpus(), Optional(ElementsAre(1, 3)));
186177

187178
// Range of offline CPUs (e.g., "0-2").
188179
EXPECT_CALL(ftrace, ReadFile("/sys/devices/system/cpu/offline", _))
189180
.WillOnce(DoAll(SetArgPointee<1>("0-2,4-5\n"), Return(true)));
190-
EXPECT_TRUE(ftrace.Tracefs::GetOfflineCpus(&cpus));
191-
EXPECT_THAT(cpus, ElementsAre(0, 1, 2, 4, 5));
192-
cpus.clear();
193-
testing::Mock::VerifyAndClearExpectations(&ftrace);
181+
EXPECT_THAT(ftrace.GetOfflineCpus(), Optional(ElementsAre(0, 1, 2, 4, 5)));
194182

195183
// Combination of single CPUs and ranges.
196184
EXPECT_CALL(ftrace, ReadFile("/sys/devices/system/cpu/offline", _))
197185
.WillOnce(DoAll(SetArgPointee<1>("0,2-3,5\n"), Return(true)));
198-
EXPECT_TRUE(ftrace.Tracefs::GetOfflineCpus(&cpus));
199-
EXPECT_THAT(cpus, ElementsAre(0, 2, 3, 5));
200-
cpus.clear();
201-
testing::Mock::VerifyAndClearExpectations(&ftrace);
186+
EXPECT_THAT(ftrace.GetOfflineCpus(), Optional(ElementsAre(0, 2, 3, 5)));
202187
}
203188

204189
} // namespace

0 commit comments

Comments
 (0)