-
Notifications
You must be signed in to change notification settings - Fork 72
Run termux-elf-cleaner on multiple threads when there's many input files #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // SPDX-License-Identifier: CC0-1.0 | ||
|
|
||
| /* Based on cpucount.c from github.com/cathugger/mkp224o */ | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <strings.h> | ||
|
|
||
| static int parsecpuinfo(void) | ||
| { | ||
| unsigned char cpubitmap[128]; | ||
|
|
||
| memset(cpubitmap,0,sizeof(cpubitmap)); | ||
|
|
||
| FILE *f = fopen("/proc/cpuinfo","r"); | ||
| if (!f) | ||
| return -1; | ||
|
|
||
| char buf[8192]; | ||
| while (fgets(buf,sizeof(buf),f)) { | ||
| // we don't like newlines | ||
| for (char *p = buf;*p;++p) { | ||
| if (*p == '\n') { | ||
| *p = 0; | ||
| break; | ||
| } | ||
| } | ||
| // split ':' | ||
| char *v = 0; | ||
| for (char *p = buf;*p;++p) { | ||
| if (*p == ':') { | ||
| *p = 0; | ||
| v = p + 1; | ||
| break; | ||
| } | ||
| } | ||
| // key padding | ||
| size_t kl = strlen(buf); | ||
| while (kl > 0 && (buf[kl - 1] == '\t' || buf[kl - 1] == ' ')) { | ||
| --kl; | ||
| buf[kl] = 0; | ||
| } | ||
| // space before value | ||
| if (v) { | ||
| while (*v && (*v == ' ' || *v == '\t')) | ||
| ++v; | ||
| } | ||
| // check what we need | ||
| if (strcasecmp(buf,"processor") == 0 && v) { | ||
| char *endp = 0; | ||
| long n = strtol(v,&endp,10); | ||
| if (endp && endp > v && n >= 0 && (size_t)n < sizeof(cpubitmap) * 8) | ||
| cpubitmap[n / 8] |= 1 << (n % 8); | ||
| } | ||
| } | ||
|
|
||
| fclose(f); | ||
|
|
||
| // count bits in bitmap | ||
| int ncpu = 0; | ||
| for (size_t n = 0;n < sizeof(cpubitmap) * 8;++n) | ||
| if (cpubitmap[n / 8] & (1 << (n % 8))) | ||
| ++ncpu; | ||
|
|
||
| return ncpu; | ||
| } | ||
|
|
||
| int cpucount(void) | ||
| { | ||
| int ncpu; | ||
| ncpu = parsecpuinfo(); | ||
| if (ncpu > 0) | ||
| return ncpu; | ||
| return -1; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| extern "C" int | ||
| cpucount(void); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is too ugly. We probably should be relying on C++11's
std::threadimplementation instead of creating the thread ourselves using the lower level pthread library. Also instead of going this way and dividing all the input files equally among the available CPUs, it would be much better if we:std::atomic::notify_all()just before completingstd::atomic::wait, and spawn a new thread as soon as one finishes.This should also increase the performance improvements in your benchmarks as not all CPUs might perform in the same fashion I guess. And also will definitely improve in conditions where the user input contains a lot of small binaries with a large binary.
I don't know whether that's the optimal approach, but I've used this in one of my personal projects'test runner, and it works pretty well. Although I haven't battle tested it. In case anyone else has a better approach feel free to comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the multithreaded implementation with std::thread.
Here's the repo: https://github.com/XniceCraft/termux-elf-cleaner/tree/multithreading
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@XniceCraft that is great! Would you like to open a pull request to replace this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Grimler91 Okay. #27