1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-11-30 03:48:17 +00:00

Switch CPU affinity stuff to event_log_*

This commit is contained in:
jsteube 2016-10-08 23:38:34 +02:00
parent 42677df2a6
commit 88565b4a5e
3 changed files with 82 additions and 73 deletions

View File

@ -17,12 +17,13 @@
#if defined (__APPLE__) #if defined (__APPLE__)
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#include <mach/mach.h> #include <mach/mach.h>
#include <mach/thread_policy.h>
#endif // __APPLE__ #endif // __APPLE__
#if defined (_WIN) #if defined (_WIN)
#include <windows.h> #include <windows.h>
#endif // _WIN #endif // _WIN
void set_cpu_affinity (char *cpu_affinity); int set_cpu_affinity (hashcat_ctx_t *hashcat_ctx);
#endif // _AFFINITY_H #endif // _AFFINITY_H

View File

@ -10,7 +10,7 @@
#include "common.h" #include "common.h"
#include "types.h" #include "types.h"
#include "memory.h" #include "memory.h"
#include "logging.h" #include "event.h"
#include "affinity.h" #include "affinity.h"
#if defined (__APPLE__) #if defined (__APPLE__)
@ -29,7 +29,7 @@ static int CPU_ISSET (int num, cpu_set_t *cs)
return (cs->count & (1 << num)); return (cs->count & (1 << num));
} }
static int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set) static int pthread_setaffinity_np (hashcat_ctx_t *hashcat_ctx, pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set)
{ {
int core; int core;
@ -40,34 +40,28 @@ static int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t
thread_affinity_policy_data_t policy = { core }; 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); return thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1);
if (rc != KERN_SUCCESS)
{
log_error ("ERROR: %s : %d", "thread_policy_set()", rc);
exit (-1);
}
return rc;
} }
#endif #endif
void set_cpu_affinity (char *cpu_affinity) #if defined (__FreeBSD__)
typedef cpuset_t cpu_set_t;
#endif
int set_cpu_affinity (hashcat_ctx_t *hashcat_ctx)
{ {
const user_options_t *user_options = hashcat_ctx->user_options;
if (user_options->cpu_affinity == NULL) return 0;
#if defined (_WIN) #if defined (_WIN)
DWORD_PTR aff_mask = 0; DWORD_PTR aff_mask = 0;
#elif defined(__FreeBSD__) #else
cpuset_t cpuset;
CPU_ZERO (&cpuset);
#elif defined(_POSIX)
cpu_set_t cpuset; cpu_set_t cpuset;
CPU_ZERO (&cpuset); CPU_ZERO (&cpuset);
#endif #endif
if (cpu_affinity) char *devices = mystrdup (user_options->cpu_affinity);
{
char *devices = mystrdup (cpu_affinity);
char *next = strtok (devices, ","); char *next = strtok (devices, ",");
@ -79,7 +73,7 @@ void set_cpu_affinity (char *cpu_affinity)
{ {
#if defined (_WIN) #if defined (_WIN)
aff_mask = 0; aff_mask = 0;
#elif defined (_POSIX) #else
CPU_ZERO (&cpuset); CPU_ZERO (&cpuset);
#endif #endif
@ -88,30 +82,44 @@ void set_cpu_affinity (char *cpu_affinity)
if (cpu_id > 32) if (cpu_id > 32)
{ {
log_error ("ERROR: Invalid cpu_id %u specified", cpu_id); event_log_error (hashcat_ctx, "ERROR: Invalid cpu_id %u specified", cpu_id);
exit (-1); return (-1);
} }
#if defined (_WIN) #if defined (_WIN)
aff_mask |= 1u << (cpu_id - 1); aff_mask |= 1u << (cpu_id - 1);
#elif defined (_POSIX) #else
CPU_SET ((cpu_id - 1), &cpuset); CPU_SET ((cpu_id - 1), &cpuset);
#endif #endif
} while ((next = strtok (NULL, ",")) != NULL); } while ((next = strtok (NULL, ",")) != NULL);
myfree (devices); myfree (devices);
}
#if defined (_WIN) #if defined (_WIN)
SetProcessAffinityMask (GetCurrentProcess (), aff_mask); SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
SetThreadAffinityMask (GetCurrentThread (), aff_mask);
#elif defined(__FreeBSD__) if (SetThreadAffinityMask (GetCurrentThread (), aff_mask) == 0)
pthread_t thread = pthread_self (); {
pthread_setaffinity_np (thread, sizeof (cpuset_t), &cpuset); event_log_error (hashcat_ctx, "ERROR: %s", "SetThreadAffinityMask()");
#elif defined(_POSIX)
pthread_t thread = pthread_self (); return -1;
pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset); }
#endif
#else
pthread_t thread = pthread_self ();
if (pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset) == -1)
{
event_log_error (hashcat_ctx, "ERROR: %s", "pthread_setaffinity_np()");
return -1;
}
#endif
return 0;
} }

View File

@ -845,10 +845,24 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
user_options_extra_init (hashcat_ctx); user_options_extra_init (hashcat_ctx);
// from here all user configuration is pre-processed so we can start logging // from here all user configuration is pre-processed so we can start logging if we want to
EVENT (EVENT_LOGFILE_TOP_INITIALIZE); EVENT (EVENT_LOGFILE_TOP_INITIALIZE);
/**
* cpu affinity
*/
const int rc_affinity = set_cpu_affinity (hashcat_ctx);
if (rc_affinity == -1) return -1;
/**
* prepare seeding for random number generator, required by logfile and rules generator
*/
setup_seeding (user_options->rp_gen_seed_chgd, user_options->rp_gen_seed);
/** /**
* To help users a bit * To help users a bit
*/ */
@ -857,12 +871,6 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
setup_umask (); setup_umask ();
/**
* prepare seeding for random number generator, required by logfile and rules generator
*/
setup_seeding (user_options->rp_gen_seed_chgd, user_options->rp_gen_seed);
/** /**
* tuning db * tuning db
*/ */
@ -912,6 +920,7 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
const int rc_potfile_init = potfile_init (hashcat_ctx); const int rc_potfile_init = potfile_init (hashcat_ctx);
if (rc_potfile_init == -1) return -1; if (rc_potfile_init == -1) return -1;
/** /**
* dictstat init * dictstat init
*/ */
@ -938,15 +947,6 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
if (rc_debugfile_init == -1) return -1; if (rc_debugfile_init == -1) return -1;
/**
* cpu affinity
*/
if (user_options->cpu_affinity)
{
set_cpu_affinity (user_options->cpu_affinity);
}
/** /**
* Init OpenCL library loader * Init OpenCL library loader
*/ */