Skip to content

Commit f1e70b9

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge "Load-balancing update_verifier worker threads."
2 parents 16b8b8f + 160514b commit f1e70b9

File tree

5 files changed

+154
-27
lines changed

5 files changed

+154
-27
lines changed

otautil/include/otautil/rangeset.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ class RangeSet {
4949
// bounds. For example, "3,5" contains blocks 3 and 4. So "3,5" and "5,7" are not overlapped.
5050
bool Overlaps(const RangeSet& other) const;
5151

52+
// Returns a vector of RangeSets that contain the same set of blocks represented by the current
53+
// RangeSet. The RangeSets in the vector contain similar number of blocks, with a maximum delta
54+
// of 1-block between any two of them. For example, 14 blocks would be split into 4 + 4 + 3 + 3,
55+
// as opposed to 4 + 4 + 4 + 2. If the total number of blocks (T) is less than groups, it
56+
// returns a vector of T 1-block RangeSets. Otherwise the number of the returned RangeSets must
57+
// equal to groups. The current RangeSet remains intact after the split.
58+
std::vector<RangeSet> Split(size_t groups) const;
59+
5260
// Returns the number of Range's in this RangeSet.
5361
size_t size() const {
5462
return ranges_.size();

otautil/rangeset.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,46 @@ void RangeSet::Clear() {
103103
blocks_ = 0;
104104
}
105105

106+
std::vector<RangeSet> RangeSet::Split(size_t groups) const {
107+
if (ranges_.empty() || groups == 0) return {};
108+
109+
if (blocks_ < groups) {
110+
groups = blocks_;
111+
}
112+
113+
// Evenly distribute blocks, with the first few groups possibly containing one more.
114+
size_t mean = blocks_ / groups;
115+
std::vector<size_t> blocks_per_group(groups, mean);
116+
std::fill_n(blocks_per_group.begin(), blocks_ % groups, mean + 1);
117+
118+
std::vector<RangeSet> result;
119+
120+
// Forward iterate Ranges and fill up each group with the desired number of blocks.
121+
auto it = ranges_.cbegin();
122+
Range range = *it;
123+
for (const auto& blocks : blocks_per_group) {
124+
RangeSet buffer;
125+
size_t needed = blocks;
126+
while (needed > 0) {
127+
size_t range_blocks = range.second - range.first;
128+
if (range_blocks > needed) {
129+
// Split the current range and don't advance the iterator.
130+
buffer.PushBack({ range.first, range.first + needed });
131+
range.first = range.first + needed;
132+
break;
133+
}
134+
buffer.PushBack(range);
135+
it++;
136+
if (it != ranges_.cend()) {
137+
range = *it;
138+
}
139+
needed -= range_blocks;
140+
}
141+
result.push_back(std::move(buffer));
142+
}
143+
return result;
144+
}
145+
106146
std::string RangeSet::ToString() const {
107147
if (ranges_.empty()) {
108148
return "";

tests/unit/rangeset_test.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,86 @@ TEST(RangeSetTest, Overlaps) {
123123
ASSERT_FALSE(RangeSet::Parse("2,5,7").Overlaps(RangeSet::Parse("2,3,5")));
124124
}
125125

126+
TEST(RangeSetTest, Split) {
127+
RangeSet rs1 = RangeSet::Parse("2,1,2");
128+
ASSERT_TRUE(rs1);
129+
ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,1,2") }), rs1.Split(1));
130+
131+
RangeSet rs2 = RangeSet::Parse("2,5,10");
132+
ASSERT_TRUE(rs2);
133+
ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,5,8"), RangeSet::Parse("2,8,10") }),
134+
rs2.Split(2));
135+
136+
RangeSet rs3 = RangeSet::Parse("4,0,1,5,10");
137+
ASSERT_TRUE(rs3);
138+
ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("4,0,1,5,7"), RangeSet::Parse("2,7,10") }),
139+
rs3.Split(2));
140+
141+
RangeSet rs4 = RangeSet::Parse("6,1,3,3,4,4,5");
142+
ASSERT_TRUE(rs4);
143+
ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,1,3"), RangeSet::Parse("2,3,4"),
144+
RangeSet::Parse("2,4,5") }),
145+
rs4.Split(3));
146+
147+
RangeSet rs5 = RangeSet::Parse("2,0,10");
148+
ASSERT_TRUE(rs5);
149+
ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,0,3"), RangeSet::Parse("2,3,6"),
150+
RangeSet::Parse("2,6,8"), RangeSet::Parse("2,8,10") }),
151+
rs5.Split(4));
152+
153+
RangeSet rs6 = RangeSet::Parse(
154+
"20,0,268,269,271,286,447,8350,32770,33022,98306,98558,163842,164094,196609,204800,229378,"
155+
"229630,294914,295166,457564");
156+
ASSERT_TRUE(rs6);
157+
size_t rs6_blocks = rs6.blocks();
158+
auto splits = rs6.Split(4);
159+
ASSERT_EQ(
160+
(std::vector<RangeSet>{
161+
RangeSet::Parse("12,0,268,269,271,286,447,8350,32770,33022,98306,98558,118472"),
162+
RangeSet::Parse("8,118472,163842,164094,196609,204800,229378,229630,237216"),
163+
RangeSet::Parse("4,237216,294914,295166,347516"), RangeSet::Parse("2,347516,457564") }),
164+
splits);
165+
size_t sum = 0;
166+
for (const auto& element : splits) {
167+
sum += element.blocks();
168+
}
169+
ASSERT_EQ(rs6_blocks, sum);
170+
}
171+
172+
TEST(RangeSetTest, Split_EdgeCases) {
173+
// Empty RangeSet.
174+
RangeSet rs1;
175+
ASSERT_FALSE(rs1);
176+
ASSERT_EQ((std::vector<RangeSet>{}), rs1.Split(2));
177+
ASSERT_FALSE(rs1);
178+
179+
// Zero group.
180+
RangeSet rs2 = RangeSet::Parse("2,1,5");
181+
ASSERT_TRUE(rs2);
182+
ASSERT_EQ((std::vector<RangeSet>{}), rs2.Split(0));
183+
184+
// The number of blocks equals to the number of groups.
185+
RangeSet rs3 = RangeSet::Parse("2,1,5");
186+
ASSERT_TRUE(rs3);
187+
ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,1,2"), RangeSet::Parse("2,2,3"),
188+
RangeSet::Parse("2,3,4"), RangeSet::Parse("2,4,5") }),
189+
rs3.Split(4));
190+
191+
// Less blocks than the number of groups.
192+
RangeSet rs4 = RangeSet::Parse("2,1,5");
193+
ASSERT_TRUE(rs4);
194+
ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,1,2"), RangeSet::Parse("2,2,3"),
195+
RangeSet::Parse("2,3,4"), RangeSet::Parse("2,4,5") }),
196+
rs4.Split(8));
197+
198+
// Less blocks than the number of groups.
199+
RangeSet rs5 = RangeSet::Parse("2,0,3");
200+
ASSERT_TRUE(rs5);
201+
ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,0,1"), RangeSet::Parse("2,1,2"),
202+
RangeSet::Parse("2,2,3") }),
203+
rs5.Split(4));
204+
}
205+
126206
TEST(RangeSetTest, GetBlockNumber) {
127207
RangeSet rs = RangeSet::Parse("2,1,10");
128208
ASSERT_EQ(static_cast<size_t>(1), rs.GetBlockNumber(0));

update_verifier/Android.mk

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ LOCAL_SRC_FILES := \
2222
update_verifier.cpp
2323

2424
LOCAL_MODULE := libupdate_verifier
25+
26+
LOCAL_STATIC_LIBRARIES := \
27+
libotautil
28+
2529
LOCAL_SHARED_LIBRARIES := \
2630
libbase \
2731
libcutils \
@@ -54,7 +58,9 @@ LOCAL_SRC_FILES := \
5458

5559
LOCAL_MODULE := update_verifier
5660
LOCAL_STATIC_LIBRARIES := \
57-
libupdate_verifier
61+
libupdate_verifier \
62+
libotautil
63+
5864
LOCAL_SHARED_LIBRARIES := \
5965
libbase \
6066
libcutils \

update_verifier/update_verifier.cpp

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
#include <android/hardware/boot/1.0/IBootControl.h>
5959
#include <cutils/android_reboot.h>
6060

61+
#include "otautil/rangeset.h"
62+
6163
using android::sp;
6264
using android::hardware::boot::V1_0::IBootControl;
6365
using android::hardware::boot::V1_0::BoolResult;
@@ -129,42 +131,33 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
129131
// followed by 'count' number comma separated integers. Every two integers reprensent a
130132
// block range with the first number included in range but second number not included.
131133
// For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150).
132-
std::vector<std::string> ranges = android::base::Split(range_str, ",");
133-
size_t range_count;
134-
bool status = android::base::ParseUint(ranges[0], &range_count);
135-
if (!status || (range_count == 0) || (range_count % 2 != 0) ||
136-
(range_count != ranges.size() - 1)) {
137-
LOG(ERROR) << "Error in parsing range string.";
134+
RangeSet ranges = RangeSet::Parse(range_str);
135+
if (!ranges) {
136+
LOG(ERROR) << "Error parsing RangeSet string " << range_str;
138137
return false;
139138
}
140-
range_count /= 2;
141139

142-
std::vector<std::future<bool>> threads;
140+
// RangeSet::Split() splits the ranges into multiple groups with same number of blocks (except for
141+
// the last group).
143142
size_t thread_num = std::thread::hardware_concurrency() ?: 4;
144-
thread_num = std::min(thread_num, range_count);
145-
size_t group_range_count = (range_count + thread_num - 1) / thread_num;
143+
std::vector<RangeSet> groups = ranges.Split(thread_num);
146144

147-
for (size_t t = 0; t < thread_num; t++) {
148-
auto thread_func = [t, group_range_count, &dm_block_device, &ranges, &partition]() {
149-
size_t blk_count = 0;
150-
static constexpr size_t kBlockSize = 4096;
151-
std::vector<uint8_t> buf(1024 * kBlockSize);
145+
std::vector<std::future<bool>> threads;
146+
for (const auto& group : groups) {
147+
auto thread_func = [&group, &dm_block_device, &partition]() {
152148
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY)));
153149
if (fd.get() == -1) {
154150
PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition;
155151
return false;
156152
}
157153

158-
for (size_t i = group_range_count * 2 * t + 1;
159-
i < std::min(group_range_count * 2 * (t + 1) + 1, ranges.size()); i += 2) {
160-
unsigned int range_start, range_end;
161-
bool parse_status = android::base::ParseUint(ranges[i], &range_start);
162-
parse_status = parse_status && android::base::ParseUint(ranges[i + 1], &range_end);
163-
if (!parse_status || range_start >= range_end) {
164-
LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i + 1];
165-
return false;
166-
}
154+
static constexpr size_t kBlockSize = 4096;
155+
std::vector<uint8_t> buf(1024 * kBlockSize);
167156

157+
size_t block_count = 0;
158+
for (const auto& range : group) {
159+
size_t range_start = range.first;
160+
size_t range_end = range.second;
168161
if (lseek64(fd.get(), static_cast<off64_t>(range_start) * kBlockSize, SEEK_SET) == -1) {
169162
PLOG(ERROR) << "lseek to " << range_start << " failed";
170163
return false;
@@ -179,9 +172,9 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
179172
}
180173
remain -= to_read;
181174
}
182-
blk_count += (range_end - range_start);
175+
block_count += (range_end - range_start);
183176
}
184-
LOG(INFO) << "Finished reading " << blk_count << " blocks on " << dm_block_device;
177+
LOG(INFO) << "Finished reading " << block_count << " blocks on " << dm_block_device;
185178
return true;
186179
};
187180

0 commit comments

Comments
 (0)