1
mirror of https://github.com/hashcat/hashcat synced 2024-11-06 11:40:38 +01: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__)
#include <mach-o/dyld.h>
#include <mach/mach.h>
#include <mach/thread_policy.h>
#endif // __APPLE__
#if defined (_WIN)
#include <windows.h>
#endif // _WIN
void set_cpu_affinity (char *cpu_affinity);
int set_cpu_affinity (hashcat_ctx_t *hashcat_ctx);
#endif // _AFFINITY_H

View File

@ -10,7 +10,7 @@
#include "common.h"
#include "types.h"
#include "memory.h"
#include "logging.h"
#include "event.h"
#include "affinity.h"
#if defined (__APPLE__)
@ -29,7 +29,7 @@ static int CPU_ISSET (int num, cpu_set_t *cs)
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;
@ -40,78 +40,86 @@ static int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t
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 (rc != KERN_SUCCESS)
{
log_error ("ERROR: %s : %d", "thread_policy_set()", rc);
exit (-1);
}
return rc;
return thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1);
}
#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)
{
#if defined(_WIN)
const user_options_t *user_options = hashcat_ctx->user_options;
if (user_options->cpu_affinity == NULL) return 0;
#if defined (_WIN)
DWORD_PTR aff_mask = 0;
#elif defined(__FreeBSD__)
cpuset_t cpuset;
CPU_ZERO (&cpuset);
#elif defined(_POSIX)
#else
cpu_set_t cpuset;
CPU_ZERO (&cpuset);
#endif
if (cpu_affinity)
char *devices = mystrdup (user_options->cpu_affinity);
char *next = strtok (devices, ",");
do
{
char *devices = mystrdup (cpu_affinity);
int cpu_id = atoi (next);
char *next = strtok (devices, ",");
do
if (cpu_id == 0)
{
int cpu_id = atoi (next);
if (cpu_id == 0)
{
#if defined (_WIN)
aff_mask = 0;
#elif defined (_POSIX)
CPU_ZERO (&cpuset);
#endif
break;
}
if (cpu_id > 32)
{
log_error ("ERROR: Invalid cpu_id %u specified", cpu_id);
exit (-1);
}
#if defined (_WIN)
aff_mask |= 1u << (cpu_id - 1);
#elif defined (_POSIX)
CPU_SET ((cpu_id - 1), &cpuset);
aff_mask = 0;
#else
CPU_ZERO (&cpuset);
#endif
} while ((next = strtok (NULL, ",")) != NULL);
break;
}
myfree (devices);
if (cpu_id > 32)
{
event_log_error (hashcat_ctx, "ERROR: Invalid cpu_id %u specified", cpu_id);
return (-1);
}
#if defined (_WIN)
aff_mask |= 1u << (cpu_id - 1);
#else
CPU_SET ((cpu_id - 1), &cpuset);
#endif
} while ((next = strtok (NULL, ",")) != NULL);
myfree (devices);
#if defined (_WIN)
SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
if (SetThreadAffinityMask (GetCurrentThread (), aff_mask) == 0)
{
event_log_error (hashcat_ctx, "ERROR: %s", "SetThreadAffinityMask()");
return -1;
}
#if defined( _WIN)
SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
SetThreadAffinityMask (GetCurrentThread (), aff_mask);
#elif defined(__FreeBSD__)
#else
pthread_t thread = pthread_self ();
pthread_setaffinity_np (thread, sizeof (cpuset_t), &cpuset);
#elif defined(_POSIX)
pthread_t thread = pthread_self ();
pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset);
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);
// 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);
/**
* 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
*/
@ -857,12 +871,6 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
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
*/
@ -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);
if (rc_potfile_init == -1) return -1;
/**
* 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;
/**
* cpu affinity
*/
if (user_options->cpu_affinity)
{
set_cpu_affinity (user_options->cpu_affinity);
}
/**
* Init OpenCL library loader
*/