Skip to content

Commit

Permalink
Add serial option
Browse files Browse the repository at this point in the history
Summary: As title.

Reviewed By: ssj933

Differential Revision: D66428539

fbshipit-source-id: 1e70a41f0b63c7d11767c0f7dbc8dfe6316f6506
  • Loading branch information
agampe authored and facebook-github-bot committed Nov 26, 2024
1 parent 55a9943 commit e02b5a9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
36 changes: 25 additions & 11 deletions libredex/DexLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,13 +649,16 @@ const dex_header* DexLoader::get_dex_header(const char* file_name) {

DexClasses DexLoader::load_dex(const char* file_name,
dex_stats_t* stats,
int support_dex_version) {
int support_dex_version,
Parallel p) {
const dex_header* dh = get_dex_header(file_name);
validate_dex_header(dh, m_file->size(), support_dex_version);
return load_dex(dh, stats);
return load_dex(dh, stats, p);
}

DexClasses DexLoader::load_dex(const dex_header* dh, dex_stats_t* stats) {
DexClasses DexLoader::load_dex(const dex_header* dh,
dex_stats_t* stats,
Parallel p) {
if (dh->class_defs_size == 0) {
return DexClasses(0);
}
Expand All @@ -666,7 +669,14 @@ DexClasses DexLoader::load_dex(const dex_header* dh, dex_stats_t* stats) {
DexClasses classes(dh->class_defs_size);
m_classes = &classes;

{
switch (p) {
case Parallel::kNo: {
for (size_t i = 0; i < dh->class_defs_size; ++i) {
load_dex_class(i);
}
break;
}
case Parallel::kYes: {
auto num_threads = redex_parallel::default_num_threads();
std::vector<std::exception_ptr> all_exceptions;
std::mutex all_exceptions_mutex;
Expand All @@ -684,10 +694,11 @@ DexClasses DexLoader::load_dex(const dex_header* dh, dex_stats_t* stats) {
num_threads);

if (!all_exceptions.empty()) {
// At least one of the workers raised an exception
aggregate_exception ae(all_exceptions);
throw ae;
}
break;
}
}

gather_input_stats(stats, dh);
Expand Down Expand Up @@ -731,22 +742,24 @@ static void balloon_all(const Scope& scope, bool throw_on_error) {
DexClasses load_classes_from_dex(const DexLocation* location,
bool balloon,
bool throw_on_balloon_error,
int support_dex_version) {
int support_dex_version,
DexLoader::Parallel p) {
dex_stats_t stats;
return load_classes_from_dex(location, &stats, balloon,
throw_on_balloon_error, support_dex_version);
throw_on_balloon_error, support_dex_version, p);
}

DexClasses load_classes_from_dex(const DexLocation* location,
dex_stats_t* stats,
bool balloon,
bool throw_on_balloon_error,
int support_dex_version) {
int support_dex_version,
DexLoader::Parallel p) {
TRACE(MAIN, 1, "Loading classes from dex from %s",
location->get_file_name().c_str());
DexLoader dl(location);
auto classes = dl.load_dex(location->get_file_name().c_str(), stats,
support_dex_version);
support_dex_version, p);
if (balloon) {
balloon_all(classes, throw_on_balloon_error);
}
Expand All @@ -756,9 +769,10 @@ DexClasses load_classes_from_dex(const DexLocation* location,
DexClasses load_classes_from_dex(const dex_header* dh,
const DexLocation* location,
bool balloon,
bool throw_on_balloon_error) {
bool throw_on_balloon_error,
DexLoader::Parallel p) {
DexLoader dl(location);
auto classes = dl.load_dex(dh, nullptr);
auto classes = dl.load_dex(dh, nullptr, p);
if (balloon) {
balloon_all(classes, throw_on_balloon_error);
}
Expand Down
42 changes: 27 additions & 15 deletions libredex/DexLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,43 @@ class DexLoader {
const DexLocation* m_location;

public:
enum class Parallel { kYes, kNo };

explicit DexLoader(const DexLocation* location);

const dex_header* get_dex_header(const char* file_name);
DexClasses load_dex(const char* file_name,
dex_stats_t* stats,
int support_dex_version);
DexClasses load_dex(const dex_header* dh, dex_stats_t* stats);
int support_dex_version,
Parallel p = Parallel::kYes);
DexClasses load_dex(const dex_header* dh,
dex_stats_t* stats,
Parallel p = Parallel::kYes);
void load_dex_class(int num);
void gather_input_stats(dex_stats_t* stats, const dex_header* dh);
DexIdx* get_idx() { return m_idx.get(); }
};

DexClasses load_classes_from_dex(const DexLocation* location,
bool balloon = true,
bool throw_on_balloon_error = true,
int support_dex_version = 35);
DexClasses load_classes_from_dex(const DexLocation* location,
dex_stats_t* stats,
bool balloon = true,
bool throw_on_balloon_error = true,
int support_dex_version = 35);
DexClasses load_classes_from_dex(const dex_header* dh,
const DexLocation* location,
bool balloon = true,
bool throw_on_balloon_error = true);
DexClasses load_classes_from_dex(
const DexLocation* location,
bool balloon = true,
bool throw_on_balloon_error = true,
int support_dex_version = 35,
DexLoader::Parallel p = DexLoader::Parallel::kYes);
DexClasses load_classes_from_dex(
const DexLocation* location,
dex_stats_t* stats,
bool balloon = true,
bool throw_on_balloon_error = true,
int support_dex_version = 35,
DexLoader::Parallel p = DexLoader::Parallel::kYes);
DexClasses load_classes_from_dex(
const dex_header* dh,
const DexLocation* location,
bool balloon = true,
bool throw_on_balloon_error = true,
DexLoader::Parallel p = DexLoader::Parallel::kYes);

std::string load_dex_magic_from_dex(const DexLocation* location);
void balloon_for_test(const Scope& scope);

Expand Down

0 comments on commit e02b5a9

Please sign in to comment.