From e2db8afdf841ce606642807629773a79a64875ef Mon Sep 17 00:00:00 2001 From: Gabriele 'matrix' Gristina Date: Sat, 6 Feb 2016 18:00:04 +0100 Subject: [PATCH] Add missing pthread_setaffinity_np for osx --- include/common.h | 1 + include/shared.h | 11 +++++++++++ src/oclHashcat.c | 14 -------------- src/shared.c | 32 ++++++++++++++++++++++++++++---- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/include/common.h b/include/common.h index c84363a..de176b6 100644 --- a/include/common.h +++ b/include/common.h @@ -45,6 +45,7 @@ #include #include #include +#include #endif typedef void *OCL_LIB; diff --git a/include/shared.h b/include/shared.h index 9bf5cc1..891f516 100644 --- a/include/shared.h +++ b/include/shared.h @@ -43,7 +43,18 @@ #define hc_thread_mutex_unlock(m) pthread_mutex_unlock (&m) #define hc_thread_mutex_init(m) pthread_mutex_init (&m, NULL) #define hc_thread_mutex_delete(m) pthread_mutex_destroy (&m) +#endif + +#ifdef OSX +typedef struct cpu_set +{ + uint32_t count; + +} cpu_set_t; +static inline void CPU_ZERO (cpu_set_t *cs) { cs->count = 0; } +static inline void CPU_SET (int num, cpu_set_t *cs) { cs->count |= (1 << num); } +static inline int CPU_ISSET (int num, cpu_set_t *cs) { return (cs->count & (1 << num)); } #endif /** diff --git a/src/oclHashcat.c b/src/oclHashcat.c index e4eaca5..36583fc 100644 --- a/src/oclHashcat.c +++ b/src/oclHashcat.c @@ -390,11 +390,7 @@ const char *USAGE_BIG[] = " -c, --segment-size=NUM Size in MB to cache from the wordfile", " --bitmap-min=NUM Minimum number of bits allowed for bitmaps", " --bitmap-max=NUM Maximum number of bits allowed for bitmaps", - #ifndef OSX " --cpu-affinity=STR Locks to CPU devices, separate with comma", - #else - " --cpu-affinity=STR Locks to CPU devices, separate with comma (disabled on OSX)", - #endif " --opencl-platforms=STR OpenCL platforms to use, separate with comma", " -d, --opencl-devices=STR OpenCL devices to use, separate with comma", " --opencl-device-types=STR OpenCL device-types to use, separate with comma, see references below", @@ -5196,9 +5192,7 @@ int main (int argc, char **argv) uint increment = INCREMENT; uint increment_min = INCREMENT_MIN; uint increment_max = INCREMENT_MAX; - #ifndef OSX char *cpu_affinity = NULL; - #endif OCL_PTR *ocl = NULL; char *opencl_devices = NULL; char *opencl_platforms = NULL; @@ -5361,9 +5355,7 @@ int main (int argc, char **argv) {"markov-classic", no_argument, 0, IDX_MARKOV_CLASSIC}, {"markov-threshold", required_argument, 0, IDX_MARKOV_THRESHOLD}, {"markov-hcstat", required_argument, 0, IDX_MARKOV_HCSTAT}, - #ifndef OSX {"cpu-affinity", required_argument, 0, IDX_CPU_AFFINITY}, - #endif {"opencl-devices", required_argument, 0, IDX_OPENCL_DEVICES}, {"opencl-platforms", required_argument, 0, IDX_OPENCL_PLATFORMS}, {"opencl-device-types", required_argument, 0, IDX_OPENCL_DEVICE_TYPES}, @@ -5672,9 +5664,7 @@ int main (int argc, char **argv) case IDX_HEX_CHARSET: hex_charset = 1; break; case IDX_HEX_SALT: hex_salt = 1; break; case IDX_HEX_WORDLIST: hex_wordlist = 1; break; - #ifndef OSX case IDX_CPU_AFFINITY: cpu_affinity = optarg; break; - #endif case IDX_OPENCL_DEVICES: opencl_devices = optarg; break; case IDX_OPENCL_PLATFORMS: opencl_platforms = optarg; break; case IDX_OPENCL_DEVICE_TYPES: @@ -6423,12 +6413,10 @@ int main (int argc, char **argv) * cpu affinity */ - #ifndef OSX if (cpu_affinity) { set_cpu_affinity (cpu_affinity); } - #endif if (rp_gen_seed_chgd == 0) { @@ -6543,9 +6531,7 @@ int main (int argc, char **argv) logfile_top_uint64 (limit); logfile_top_uint64 (skip); logfile_top_char (separator); - #ifndef OSX logfile_top_string (cpu_affinity); - #endif logfile_top_string (custom_charset_1); logfile_top_string (custom_charset_2); logfile_top_string (custom_charset_3); diff --git a/src/shared.c b/src/shared.c index de0fb2e..8ffad9e 100644 --- a/src/shared.c +++ b/src/shared.c @@ -4235,11 +4235,35 @@ void truecrypt_crc32 (const char *filename, u8 keytab[64]) myfree (buf); } +#ifdef OSX +int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set) +{ + int core; + + for (core = 0; core < (8 * (int)cpu_size); core++) + if (CPU_ISSET(core, cpu_set)) break; + + thread_affinity_policy_data_t policy = { core }; + + const int rc = thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1); + + if (data.quiet == 0) + { + if (rc != KERN_SUCCESS) + { + log_error ("ERROR: %s : %d", "thread_policy_set()", rc); + } + } + + return rc; +} +#endif + void set_cpu_affinity (char *cpu_affinity) { #ifdef WIN DWORD_PTR aff_mask = 0; - #elif LINUX + #elif _POSIX cpu_set_t cpuset; CPU_ZERO (&cpuset); #endif @@ -4258,7 +4282,7 @@ void set_cpu_affinity (char *cpu_affinity) { #ifdef WIN aff_mask = 0; - #elif LINUX + #elif _POSIX CPU_ZERO (&cpuset); #endif @@ -4274,7 +4298,7 @@ void set_cpu_affinity (char *cpu_affinity) #ifdef WIN aff_mask |= 1 << (cpu_id - 1); - #elif LINUX + #elif _POSIX CPU_SET ((cpu_id - 1), &cpuset); #endif @@ -4286,7 +4310,7 @@ void set_cpu_affinity (char *cpu_affinity) #ifdef WIN SetProcessAffinityMask (GetCurrentProcess (), aff_mask); SetThreadAffinityMask (GetCurrentThread (), aff_mask); - #elif LINUX + #elif _POSIX pthread_t thread = pthread_self (); pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset); #endif -- 2.25.1