1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-11-26 09:58:16 +00:00

Move affinity specific functions into their own source file

This commit is contained in:
jsteube 2016-09-07 11:16:31 +02:00
parent bcb1bfee86
commit 3ba5c743f8
6 changed files with 169 additions and 122 deletions

29
include/affinity.h Normal file
View File

@ -0,0 +1,29 @@
/**
* Author......: Jens Steube <jens.steube@gmail.com>
* License.....: MIT
*/
#ifndef _AFFINITY_H
#define _AFFINITY_H
#include <stdlib.h>
#ifdef _POSIX
#include <pthread.h>
#endif // _POSIX
#ifdef _WIN
#include <windows.h>
#endif // _WIN
#ifdef __APPLE__
typedef struct cpu_set
{
uint32_t count;
} cpu_set_t;
#endif
void set_cpu_affinity (char *cpu_affinity);
#endif // _AFFINITY_H

View File

@ -21,15 +21,12 @@
*/
#ifdef _POSIX
#include <pthread.h>
#include <dlfcn.h>
#include <pwd.h>
#include <limits.h>
//#include <pthread.h>
//#include <dlfcn.h>
//#include <limits.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#ifndef __APPLE__
#include <sys/sysctl.h>
#endif // __APPLE__
#endif // _POSIX
#ifdef __APPLE__
@ -38,7 +35,7 @@
#endif // __APPLE__
#ifdef _WIN
#include <windows.h>
//#include <windows.h>
#include <psapi.h>
#include <io.h>
#endif // _WIN
@ -47,17 +44,7 @@
* unsorted
*/
#ifdef __APPLE__
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
#ifdef _WIN
@ -195,7 +182,7 @@ void fsync (int fd);
void myabort (void);
void myquit (void);
void set_cpu_affinity (char *cpu_affinity);
void naive_replace (char *s, const u8 key_char, const u8 replace_char);

View File

@ -165,7 +165,7 @@ LFLAGS_CROSS_WIN := -lpsapi
## Objects
##
NATIVE_OBJS := obj/filehandling.NATIVE.o obj/tuningdb.NATIVE.o obj/locking.NATIVE.o obj/folder.NATIVE.o obj/bitops.NATIVE.o obj/convert.NATIVE.o obj/cpu_aes.NATIVE.o obj/cpu_crc32.NATIVE.o obj/cpu_des.NATIVE.o obj/cpu_md5.NATIVE.o obj/cpu_sha1.NATIVE.o obj/cpu_sha256.NATIVE.o obj/data.NATIVE.o obj/ext_OpenCL.NATIVE.o obj/hwmon.NATIVE.o obj/interface.NATIVE.o obj/logfile.NATIVE.o obj/logging.NATIVE.o obj/memory.NATIVE.o obj/mpsp.NATIVE.o obj/rp_cpu.NATIVE.o obj/rp_kernel_on_cpu.NATIVE.o obj/shared.NATIVE.o obj/terminal.NATIVE.o obj/usage.NATIVE.o
NATIVE_OBJS := obj/affinity.NATIVE.o obj/filehandling.NATIVE.o obj/tuningdb.NATIVE.o obj/locking.NATIVE.o obj/folder.NATIVE.o obj/bitops.NATIVE.o obj/convert.NATIVE.o obj/cpu_aes.NATIVE.o obj/cpu_crc32.NATIVE.o obj/cpu_des.NATIVE.o obj/cpu_md5.NATIVE.o obj/cpu_sha1.NATIVE.o obj/cpu_sha256.NATIVE.o obj/data.NATIVE.o obj/ext_OpenCL.NATIVE.o obj/hwmon.NATIVE.o obj/interface.NATIVE.o obj/logfile.NATIVE.o obj/logging.NATIVE.o obj/memory.NATIVE.o obj/mpsp.NATIVE.o obj/rp_cpu.NATIVE.o obj/rp_kernel_on_cpu.NATIVE.o obj/shared.NATIVE.o obj/terminal.NATIVE.o obj/usage.NATIVE.o
ifeq ($(UNAME),Linux)
NATIVE_OBJS += obj/ext_ADL.NATIVE.o
@ -174,8 +174,8 @@ NATIVE_OBJS += obj/ext_nvml.NATIVE.o
NATIVE_OBJS += obj/ext_xnvctrl.NATIVE.o
endif
LINUX_32_OBJS := obj/filehandling.LINUX.32.o obj/tuningdb.LINUX.32.o obj/locking.LINUX.32.o obj/folder.LINUX.32.o obj/bitops.LINUX.32.o obj/convert.LINUX.32.o obj/cpu_aes.LINUX.32.o obj/cpu_crc32.LINUX.32.o obj/cpu_des.LINUX.32.o obj/cpu_md5.LINUX.32.o obj/cpu_sha1.LINUX.32.o obj/cpu_sha256.LINUX.32.o obj/data.LINUX.32.o obj/ext_ADL.LINUX.32.o obj/ext_nvapi.LINUX.32.o obj/ext_nvml.LINUX.32.o obj/ext_OpenCL.LINUX.32.o obj/ext_xnvctrl.LINUX.32.o obj/hwmon.LINUX.32.o obj/interface.LINUX.32.o obj/logfile.LINUX.32.o obj/logging.LINUX.32.o obj/memory.LINUX.32.o obj/mpsp.LINUX.32.o obj/rp_cpu.LINUX.32.o obj/rp_kernel_on_cpu.LINUX.32.o obj/shared.LINUX.32.o obj/terminal.LINUX.32.o obj/usage.LINUX.32.o
LINUX_64_OBJS := obj/filehandling.LINUX.64.o obj/tuningdb.LINUX.64.o obj/locking.LINUX.64.o obj/folder.LINUX.64.o obj/bitops.LINUX.64.o obj/convert.LINUX.64.o obj/cpu_aes.LINUX.64.o obj/cpu_crc32.LINUX.64.o obj/cpu_des.LINUX.64.o obj/cpu_md5.LINUX.64.o obj/cpu_sha1.LINUX.64.o obj/cpu_sha256.LINUX.64.o obj/data.LINUX.64.o obj/ext_ADL.LINUX.64.o obj/ext_nvapi.LINUX.64.o obj/ext_nvml.LINUX.64.o obj/ext_OpenCL.LINUX.64.o obj/ext_xnvctrl.LINUX.64.o obj/hwmon.LINUX.64.o obj/interface.LINUX.64.o obj/logfile.LINUX.64.o obj/logging.LINUX.64.o obj/memory.LINUX.64.o obj/mpsp.LINUX.64.o obj/rp_cpu.LINUX.64.o obj/rp_kernel_on_cpu.LINUX.64.o obj/shared.LINUX.64.o obj/terminal.LINUX.64.o obj/usage.LINUX.64.o
LINUX_32_OBJS := obj/affinity.LINUX.32.o obj/filehandling.LINUX.32.o obj/tuningdb.LINUX.32.o obj/locking.LINUX.32.o obj/folder.LINUX.32.o obj/bitops.LINUX.32.o obj/convert.LINUX.32.o obj/cpu_aes.LINUX.32.o obj/cpu_crc32.LINUX.32.o obj/cpu_des.LINUX.32.o obj/cpu_md5.LINUX.32.o obj/cpu_sha1.LINUX.32.o obj/cpu_sha256.LINUX.32.o obj/data.LINUX.32.o obj/ext_ADL.LINUX.32.o obj/ext_nvapi.LINUX.32.o obj/ext_nvml.LINUX.32.o obj/ext_OpenCL.LINUX.32.o obj/ext_xnvctrl.LINUX.32.o obj/hwmon.LINUX.32.o obj/interface.LINUX.32.o obj/logfile.LINUX.32.o obj/logging.LINUX.32.o obj/memory.LINUX.32.o obj/mpsp.LINUX.32.o obj/rp_cpu.LINUX.32.o obj/rp_kernel_on_cpu.LINUX.32.o obj/shared.LINUX.32.o obj/terminal.LINUX.32.o obj/usage.LINUX.32.o
LINUX_64_OBJS := obj/affinity.LINUX.64.o obj/filehandling.LINUX.64.o obj/tuningdb.LINUX.64.o obj/locking.LINUX.64.o obj/folder.LINUX.64.o obj/bitops.LINUX.64.o obj/convert.LINUX.64.o obj/cpu_aes.LINUX.64.o obj/cpu_crc32.LINUX.64.o obj/cpu_des.LINUX.64.o obj/cpu_md5.LINUX.64.o obj/cpu_sha1.LINUX.64.o obj/cpu_sha256.LINUX.64.o obj/data.LINUX.64.o obj/ext_ADL.LINUX.64.o obj/ext_nvapi.LINUX.64.o obj/ext_nvml.LINUX.64.o obj/ext_OpenCL.LINUX.64.o obj/ext_xnvctrl.LINUX.64.o obj/hwmon.LINUX.64.o obj/interface.LINUX.64.o obj/logfile.LINUX.64.o obj/logging.LINUX.64.o obj/memory.LINUX.64.o obj/mpsp.LINUX.64.o obj/rp_cpu.LINUX.64.o obj/rp_kernel_on_cpu.LINUX.64.o obj/shared.LINUX.64.o obj/terminal.LINUX.64.o obj/usage.LINUX.64.o
# Windows CRT file globbing:
@ -185,8 +185,8 @@ CRT_GLOB_INCLUDE_FOLDER := $(dir $(lastword $(MAKEFILE_LIST)))
include $(CRT_GLOB_INCLUDE_FOLDER)/win_file_globbing.mk
WIN_32_OBJS := obj/filehandling.WIN.32.o obj/tuningdb.WIN.32.o obj/locking.WIN.32.o obj/folder.WIN.32.o obj/bitops.WIN.32.o obj/convert.WIN.32.o obj/cpu_aes.WIN.32.o obj/cpu_crc32.WIN.32.o obj/cpu_des.WIN.32.o obj/cpu_md5.WIN.32.o obj/cpu_sha1.WIN.32.o obj/cpu_sha256.WIN.32.o obj/data.WIN.32.o obj/ext_ADL.WIN.32.o obj/ext_nvapi.WIN.32.o obj/ext_nvml.WIN.32.o obj/ext_OpenCL.WIN.32.o obj/ext_xnvctrl.WIN.32.o obj/hwmon.WIN.32.o obj/interface.WIN.32.o obj/logfile.WIN.32.o obj/logging.WIN.32.o obj/memory.WIN.32.o obj/mpsp.WIN.32.o obj/rp_cpu.WIN.32.o obj/rp_kernel_on_cpu.WIN.32.o obj/shared.WIN.32.o obj/terminal.WIN.32.o obj/usage.WIN.32.o $(CRT_GLOB_32)
WIN_64_OBJS := obj/filehandling.WIN.64.o obj/tuningdb.WIN.64.o obj/locking.WIN.64.o obj/folder.WIN.64.o obj/bitops.WIN.64.o obj/convert.WIN.64.o obj/cpu_aes.WIN.64.o obj/cpu_crc32.WIN.64.o obj/cpu_des.WIN.64.o obj/cpu_md5.WIN.64.o obj/cpu_sha1.WIN.64.o obj/cpu_sha256.WIN.64.o obj/data.WIN.64.o obj/ext_ADL.WIN.64.o obj/ext_nvapi.WIN.64.o obj/ext_nvml.WIN.64.o obj/ext_OpenCL.WIN.64.o obj/ext_xnvctrl.WIN.64.o obj/hwmon.WIN.64.o obj/interface.WIN.64.o obj/logfile.WIN.64.o obj/logging.WIN.64.o obj/memory.WIN.64.o obj/mpsp.WIN.64.o obj/rp_cpu.WIN.64.o obj/rp_kernel_on_cpu.WIN.64.o obj/shared.WIN.64.o obj/terminal.WIN.64.o obj/usage.WIN.64.o $(CRT_GLOB_64)
WIN_32_OBJS := obj/affinity.WIN.32.o obj/filehandling.WIN.32.o obj/tuningdb.WIN.32.o obj/locking.WIN.32.o obj/folder.WIN.32.o obj/bitops.WIN.32.o obj/convert.WIN.32.o obj/cpu_aes.WIN.32.o obj/cpu_crc32.WIN.32.o obj/cpu_des.WIN.32.o obj/cpu_md5.WIN.32.o obj/cpu_sha1.WIN.32.o obj/cpu_sha256.WIN.32.o obj/data.WIN.32.o obj/ext_ADL.WIN.32.o obj/ext_nvapi.WIN.32.o obj/ext_nvml.WIN.32.o obj/ext_OpenCL.WIN.32.o obj/ext_xnvctrl.WIN.32.o obj/hwmon.WIN.32.o obj/interface.WIN.32.o obj/logfile.WIN.32.o obj/logging.WIN.32.o obj/memory.WIN.32.o obj/mpsp.WIN.32.o obj/rp_cpu.WIN.32.o obj/rp_kernel_on_cpu.WIN.32.o obj/shared.WIN.32.o obj/terminal.WIN.32.o obj/usage.WIN.32.o $(CRT_GLOB_32)
WIN_64_OBJS := obj/affinity.WIN.64.o obj/filehandling.WIN.64.o obj/tuningdb.WIN.64.o obj/locking.WIN.64.o obj/folder.WIN.64.o obj/bitops.WIN.64.o obj/convert.WIN.64.o obj/cpu_aes.WIN.64.o obj/cpu_crc32.WIN.64.o obj/cpu_des.WIN.64.o obj/cpu_md5.WIN.64.o obj/cpu_sha1.WIN.64.o obj/cpu_sha256.WIN.64.o obj/data.WIN.64.o obj/ext_ADL.WIN.64.o obj/ext_nvapi.WIN.64.o obj/ext_nvml.WIN.64.o obj/ext_OpenCL.WIN.64.o obj/ext_xnvctrl.WIN.64.o obj/hwmon.WIN.64.o obj/interface.WIN.64.o obj/logfile.WIN.64.o obj/logging.WIN.64.o obj/memory.WIN.64.o obj/mpsp.WIN.64.o obj/rp_cpu.WIN.64.o obj/rp_kernel_on_cpu.WIN.64.o obj/shared.WIN.64.o obj/terminal.WIN.64.o obj/usage.WIN.64.o $(CRT_GLOB_64)
##
## Targets: Global

112
src/affinity.c Normal file
View File

@ -0,0 +1,112 @@
/**
* Author......: Jens Steube <jens.steube@gmail.com>
* License.....: MIT
*/
#include "common.h"
#include "types_int.h"
#include "memory.h"
#include "logging.h"
#include "affinity.h"
#ifdef __APPLE__
static 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;
}
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));
}
#endif
void set_cpu_affinity (char *cpu_affinity)
{
#if defined(_WIN)
DWORD_PTR aff_mask = 0;
#elif defined(__FreeBSD__)
cpuset_t cpuset;
CPU_ZERO (&cpuset);
#elif defined(_POSIX)
cpu_set_t cpuset;
CPU_ZERO (&cpuset);
#endif
if (cpu_affinity)
{
char *devices = mystrdup (cpu_affinity);
char *next = strtok (devices, ",");
do
{
uint cpu_id = atoi (next);
if (cpu_id == 0)
{
#ifdef _WIN
aff_mask = 0;
#elif _POSIX
CPU_ZERO (&cpuset);
#endif
break;
}
if (cpu_id > 32)
{
log_error ("ERROR: Invalid cpu_id %u specified", cpu_id);
exit (-1);
}
#ifdef _WIN
aff_mask |= 1u << (cpu_id - 1);
#elif _POSIX
CPU_SET ((cpu_id - 1), &cpuset);
#endif
} while ((next = strtok (NULL, ",")) != NULL);
myfree (devices);
}
#if defined( _WIN)
SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
SetThreadAffinityMask (GetCurrentThread (), aff_mask);
#elif defined(__FreeBSD__)
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);
#endif
}

View File

@ -6,12 +6,20 @@
* License.....: MIT
*/
#ifdef __APPLE__
#include "common.h"
#include <stdio.h>
#include <string.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <search.h>
#include <getopt.h>
#ifdef _POSIX
#include <pthread.h>
#include <pwd.h>
#endif // _POSIX
#include "common.h"
#include "types_int.h"
#include "types.h"
#include "timer.h"
@ -44,11 +52,9 @@
#include "hwmon.h"
#include "mpsp.h"
#include "data.h"
#include "affinity.h"
#include "usage.h"
#include <search.h>
#include <getopt.h>
extern hc_global_data_t data;
extern int SUPPRESS_OUTPUT;
@ -6331,7 +6337,7 @@ int main (int argc, char **argv)
profile_dir = get_profile_dir (homedir);
session_dir = get_session_dir (profile_dir);
shared_dir = strdup (SHARED_FOLDER);
shared_dir = mystrdup (SHARED_FOLDER);
mkdir (profile_dir, 0700);
mkdir (session_dir, 0700);
@ -13607,7 +13613,7 @@ int main (int argc, char **argv)
{
uint *keyfile_buf = ((tc_t *) esalts_buf)->keyfile_buf;
char *keyfiles = strdup (truecrypt_keyfiles);
char *keyfiles = mystrdup (truecrypt_keyfiles);
char *keyfile = strtok (keyfiles, ",");
@ -13624,7 +13630,7 @@ int main (int argc, char **argv)
{
uint *keyfile_buf = ((tc_t *) esalts_buf)->keyfile_buf;
char *keyfiles = strdup (veracrypt_keyfiles);
char *keyfiles = mystrdup (veracrypt_keyfiles);
char *keyfile = strtok (keyfiles, ",");
@ -17698,7 +17704,7 @@ int main (int argc, char **argv)
dictcnt++;
dictfiles[dictcnt - 1] = strdup (l1_filename);
dictfiles[dictcnt - 1] = mystrdup (l1_filename);
}
}
}
@ -18161,7 +18167,7 @@ int main (int argc, char **argv)
dictcnt++;
dictfiles[dictcnt - 1] = strdup (l1_filename);
dictfiles[dictcnt - 1] = mystrdup (l1_filename);
}
}
}
@ -18340,7 +18346,7 @@ int main (int argc, char **argv)
dictcnt++;
dictfiles[dictcnt - 1] = strdup (l1_filename);
dictfiles[dictcnt - 1] = mystrdup (l1_filename);
}
}
}

View File

@ -56,93 +56,6 @@ void fsync (int fd)
#ifdef __APPLE__
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)
{
#if defined(_WIN)
DWORD_PTR aff_mask = 0;
#elif defined(__FreeBSD__)
cpuset_t cpuset;
CPU_ZERO (&cpuset);
#elif defined(_POSIX)
cpu_set_t cpuset;
CPU_ZERO (&cpuset);
#endif
if (cpu_affinity)
{
char *devices = mystrdup (cpu_affinity);
char *next = strtok (devices, ",");
do
{
uint cpu_id = atoi (next);
if (cpu_id == 0)
{
#ifdef _WIN
aff_mask = 0;
#elif _POSIX
CPU_ZERO (&cpuset);
#endif
break;
}
if (cpu_id > 32)
{
log_error ("ERROR: Invalid cpu_id %u specified", cpu_id);
exit (-1);
}
#ifdef _WIN
aff_mask |= 1u << (cpu_id - 1);
#elif _POSIX
CPU_SET ((cpu_id - 1), &cpuset);
#endif
} while ((next = strtok (NULL, ",")) != NULL);
myfree (devices);
}
#if defined( _WIN)
SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
SetThreadAffinityMask (GetCurrentThread (), aff_mask);
#elif defined(__FreeBSD__)
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);
#endif
}
void *rulefind (const void *key, void *base, int nmemb, size_t size, int (*compar) (const void *, const void *))
{
char *element, *end;