2016-09-07 09:16:31 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-07 09:16:31 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
2016-09-16 15:01:18 +00:00
|
|
|
#include "types.h"
|
2016-09-07 09:16:31 +00:00
|
|
|
#include "memory.h"
|
2016-10-08 21:38:34 +00:00
|
|
|
#include "event.h"
|
2016-09-07 09:16:31 +00:00
|
|
|
#include "affinity.h"
|
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
#if defined (__APPLE__)
|
2016-09-07 09:53:23 +00:00
|
|
|
static void CPU_ZERO (cpu_set_t *cs)
|
|
|
|
{
|
|
|
|
cs->count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CPU_SET (int num, cpu_set_t *cs)
|
|
|
|
{
|
|
|
|
cs->count |= (1 << num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int CPU_ISSET (int num, cpu_set_t *cs)
|
|
|
|
{
|
|
|
|
return (cs->count & (1 << num));
|
|
|
|
}
|
|
|
|
|
2016-10-10 07:18:10 +00:00
|
|
|
static int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set)
|
2016-09-07 09:16:31 +00:00
|
|
|
{
|
|
|
|
int core;
|
|
|
|
|
2016-09-07 09:42:05 +00:00
|
|
|
for (core = 0; core < (8 * (int) cpu_size); core++)
|
|
|
|
{
|
|
|
|
if (CPU_ISSET (core, cpu_set)) break;
|
|
|
|
}
|
2016-09-07 09:16:31 +00:00
|
|
|
|
|
|
|
thread_affinity_policy_data_t policy = { core };
|
|
|
|
|
2016-10-08 21:38:34 +00:00
|
|
|
return thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1);
|
2016-09-07 09:16:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-08 21:38:34 +00:00
|
|
|
#if defined (__FreeBSD__)
|
2016-11-21 22:03:40 +00:00
|
|
|
#include <pthread_np.h>
|
2016-10-08 21:38:34 +00:00
|
|
|
typedef cpuset_t cpu_set_t;
|
|
|
|
#endif
|
|
|
|
|
2022-01-09 03:50:28 +00:00
|
|
|
#if defined(__NetBSD__)
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
|
|
|
typedef cpuset_t cpu_set_t;
|
|
|
|
#endif
|
|
|
|
|
2016-11-29 21:39:22 +00:00
|
|
|
int set_cpu_affinity (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx)
|
2016-09-07 09:16:31 +00:00
|
|
|
{
|
2023-01-20 21:01:56 +00:00
|
|
|
#if defined (__CYGWIN__)
|
2016-11-29 21:39:22 +00:00
|
|
|
return 0;
|
2023-01-20 21:01:56 +00:00
|
|
|
#else
|
|
|
|
|
2016-10-08 21:38:34 +00:00
|
|
|
const user_options_t *user_options = hashcat_ctx->user_options;
|
|
|
|
|
|
|
|
if (user_options->cpu_affinity == NULL) return 0;
|
|
|
|
|
2022-01-09 03:50:28 +00:00
|
|
|
char *devices = hcstrdup (user_options->cpu_affinity);
|
|
|
|
|
|
|
|
if (devices == NULL) return -1;
|
|
|
|
|
2016-10-08 21:38:34 +00:00
|
|
|
#if defined (_WIN)
|
2016-09-07 09:16:31 +00:00
|
|
|
DWORD_PTR aff_mask = 0;
|
2021-07-06 13:47:18 +00:00
|
|
|
const int cpu_id_max = 8 * sizeof (aff_mask);
|
2022-01-09 03:50:28 +00:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
cpuset_t * cpuset;
|
|
|
|
const int cpu_id_max = 8 * cpuset_size (cpuset);
|
2022-03-31 17:19:16 +00:00
|
|
|
cpuset = cpuset_create ();
|
2022-01-09 03:50:28 +00:00
|
|
|
if (cpuset == NULL)
|
|
|
|
{
|
|
|
|
event_log_error (hashcat_ctx, "cpuset_create() failed with error: %d", errno);
|
|
|
|
|
|
|
|
hcfree (devices);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2016-10-08 21:38:34 +00:00
|
|
|
#else
|
2016-09-07 09:16:31 +00:00
|
|
|
cpu_set_t cpuset;
|
2021-07-06 13:47:18 +00:00
|
|
|
const int cpu_id_max = 8 * sizeof (cpuset);
|
2016-09-07 09:16:31 +00:00
|
|
|
CPU_ZERO (&cpuset);
|
|
|
|
#endif
|
|
|
|
|
2017-11-18 13:23:02 +00:00
|
|
|
char *saveptr = NULL;
|
2016-11-16 09:35:01 +00:00
|
|
|
|
|
|
|
char *next = strtok_r (devices, ",", &saveptr);
|
2016-10-08 21:38:34 +00:00
|
|
|
|
|
|
|
do
|
2016-09-07 09:16:31 +00:00
|
|
|
{
|
2018-02-08 18:13:29 +00:00
|
|
|
const int cpu_id = (const int) strtol (next, NULL, 10);
|
2016-10-08 21:38:34 +00:00
|
|
|
|
|
|
|
if (cpu_id == 0)
|
|
|
|
{
|
|
|
|
#if defined (_WIN)
|
|
|
|
aff_mask = 0;
|
2022-01-09 03:50:28 +00:00
|
|
|
#elif defined (__NetBSD__)
|
|
|
|
cpuset_destroy (cpuset);
|
|
|
|
cpuset = cpuset_create ();
|
|
|
|
if (cpuset == NULL)
|
|
|
|
{
|
|
|
|
event_log_error (hashcat_ctx, "cpuset_create() failed with error: %d", errno);
|
|
|
|
|
|
|
|
hcfree (devices);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2016-10-08 21:38:34 +00:00
|
|
|
#else
|
|
|
|
CPU_ZERO (&cpuset);
|
|
|
|
#endif
|
2016-09-07 09:16:31 +00:00
|
|
|
|
2016-10-08 21:38:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-09-07 09:16:31 +00:00
|
|
|
|
2021-07-06 13:47:18 +00:00
|
|
|
if (cpu_id > cpu_id_max)
|
2016-09-07 09:16:31 +00:00
|
|
|
{
|
2017-04-02 08:18:59 +00:00
|
|
|
event_log_error (hashcat_ctx, "Invalid cpu_id %d specified.", cpu_id);
|
2016-09-07 09:16:31 +00:00
|
|
|
|
2022-01-09 03:50:28 +00:00
|
|
|
#if defined (__NetBSD__)
|
|
|
|
cpuset_destroy (cpuset);
|
|
|
|
#endif
|
|
|
|
|
2017-02-14 11:42:22 +00:00
|
|
|
hcfree (devices);
|
|
|
|
|
2017-02-14 19:46:03 +00:00
|
|
|
return -1;
|
2016-10-08 21:38:34 +00:00
|
|
|
}
|
2016-09-07 09:16:31 +00:00
|
|
|
|
2016-10-08 21:38:34 +00:00
|
|
|
#if defined (_WIN)
|
2021-07-06 13:47:18 +00:00
|
|
|
aff_mask |= ((DWORD_PTR) 1) << (cpu_id - 1);
|
2022-01-09 03:50:28 +00:00
|
|
|
#elif defined (__NetBSD__)
|
|
|
|
cpuset_set (cpu_id - 1, cpuset);
|
2016-10-08 21:38:34 +00:00
|
|
|
#else
|
|
|
|
CPU_SET ((cpu_id - 1), &cpuset);
|
|
|
|
#endif
|
2016-09-07 09:16:31 +00:00
|
|
|
|
2017-11-18 13:23:02 +00:00
|
|
|
} while ((next = strtok_r ((char *) NULL, ",", &saveptr)) != NULL);
|
2016-09-07 09:16:31 +00:00
|
|
|
|
2022-01-09 03:50:28 +00:00
|
|
|
#if defined (__NetBSD__)
|
|
|
|
cpuset_destroy (cpuset);
|
|
|
|
#endif
|
|
|
|
|
2016-10-10 09:03:11 +00:00
|
|
|
hcfree (devices);
|
2016-09-07 09:16:31 +00:00
|
|
|
|
2016-10-08 21:38:34 +00:00
|
|
|
#if defined (_WIN)
|
|
|
|
|
2021-07-06 13:47:18 +00:00
|
|
|
if (SetProcessAffinityMask (GetCurrentProcess (), aff_mask) == 0)
|
2016-10-08 21:38:34 +00:00
|
|
|
{
|
2021-07-06 13:47:18 +00:00
|
|
|
event_log_error (hashcat_ctx, "SetProcessAffinityMask() failed with error: %d", (int) GetLastError ());
|
2016-09-07 09:16:31 +00:00
|
|
|
|
2016-10-08 21:38:34 +00:00
|
|
|
return -1;
|
2016-09-07 09:16:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 03:50:28 +00:00
|
|
|
#elif defined (__NetBSD__)
|
|
|
|
|
|
|
|
pthread_t thread = pthread_self ();
|
|
|
|
|
2022-03-31 17:19:16 +00:00
|
|
|
const int rc = pthread_setaffinity_np (thread, cpuset_size (cpuset), cpuset);
|
2022-01-09 03:50:28 +00:00
|
|
|
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
|
|
|
event_log_error (hashcat_ctx, "pthread_setaffinity_np() failed with error: %d", rc);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-10-08 21:38:34 +00:00
|
|
|
#else
|
|
|
|
|
2016-09-07 09:16:31 +00:00
|
|
|
pthread_t thread = pthread_self ();
|
2016-10-08 21:38:34 +00:00
|
|
|
|
2021-07-06 13:47:18 +00:00
|
|
|
const int rc = pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset);
|
|
|
|
|
|
|
|
if (rc != 0)
|
2016-10-08 21:38:34 +00:00
|
|
|
{
|
2021-07-06 13:47:18 +00:00
|
|
|
event_log_error (hashcat_ctx, "pthread_setaffinity_np() failed with error: %d", rc);
|
2016-10-08 21:38:34 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-07 09:16:31 +00:00
|
|
|
#endif
|
2016-10-08 21:38:34 +00:00
|
|
|
return 0;
|
2023-01-20 21:01:56 +00:00
|
|
|
#endif
|
2016-09-07 09:16:31 +00:00
|
|
|
}
|