Merge pull request #2082 from rdmitr/configure-num-cores

Configurable number of CPU cores.
This commit is contained in:
Iceman 2023-08-22 07:35:18 +02:00 committed by GitHub
commit 4b9b545bbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View file

@ -581,6 +581,7 @@ static void show_help(bool showFullHelp, char *exec_name) {
PrintAndLogEx(NORMAL, " --unlock-bootloader Enable flashing of bootloader area *DANGEROUS* (need --flash)");
PrintAndLogEx(NORMAL, " --force Enable flashing even if firmware seems to not match client version");
PrintAndLogEx(NORMAL, " --image <imagefile> image to flash. Can be specified several times.");
PrintAndLogEx(NORMAL, " --ncpu <num_cores> override number of CPU cores");
PrintAndLogEx(NORMAL, "\nExamples:");
PrintAndLogEx(NORMAL, "\n to run Proxmark3 client:\n");
PrintAndLogEx(NORMAL, " %s "SERIAL_PORT_EXAMPLE_H" -- runs the pm3 client", exec_name);
@ -996,6 +997,23 @@ int main(int argc, char *argv[]) {
continue;
}
if (strcmp(argv[i], "--ncpu") == 0) {
if (i + 1 == argc) {
PrintAndLogEx(ERR, _RED_("ERROR:") " missing CPU number specification after --ncpu\n");
show_help(false, exec_name);
return 1;
}
long int ncpus = strtol(argv[i + 1], NULL, 10);
const int detected_cpus = detect_num_CPUs();
if (ncpus < 0 || ncpus >= detected_cpus) {
PrintAndLogEx(ERR, _RED_("ERROR:") " invalid number of CPU cores: --ncpu " _YELLOW_("%s") " (available: %d)\n", argv[i + 1], detected_cpus);
return 1;
}
g_numCPUs = ncpus;
i++;
continue;
}
// We got an unknown parameter
PrintAndLogEx(ERR, _RED_("ERROR:") " invalid parameter: " _YELLOW_("%s") "\n", argv[i]);
show_help(false, exec_name);

View file

@ -40,6 +40,8 @@ uint8_t g_debugMode = 0;
uint8_t g_printAndLog = PRINTANDLOG_PRINT | PRINTANDLOG_LOG;
// global client tell if a pending prompt is present
bool g_pendingPrompt = false;
// global CPU core count override
int g_numCPUs = 0;
#ifdef _WIN32
#include <windows.h>
@ -1077,8 +1079,16 @@ uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor) {
return result;
}
// determine number of logical CPU cores (use for multithreaded functions)
int num_CPUs(void) {
if (g_numCPUs > 0) {
return g_numCPUs;
}
return detect_num_CPUs();
}
// determine number of logical CPU cores (use for multithreaded functions)
int detect_num_CPUs(void) {
#if defined(_WIN32)
#include <sysinfoapi.h>
SYSTEM_INFO sysinfo;

View file

@ -32,6 +32,7 @@
extern uint8_t g_debugMode;
extern uint8_t g_printAndLog;
extern bool g_pendingPrompt;
extern int g_numCPUs;
#define PRINTANDLOG_PRINT 1
#define PRINTANDLOG_LOG 2
@ -130,7 +131,8 @@ void wiegand_add_parity_swapped(uint8_t *target, uint8_t *source, uint8_t length
uint32_t PackBits(uint8_t start, uint8_t len, const uint8_t *bits);
uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor);
int num_CPUs(void); // number of logical CPUs
int num_CPUs(void);
int detect_num_CPUs(void); // number of logical CPUs
void str_lower(char *s); // converts string to lower case
void str_upper(char *s); // converts string to UPPER case