"Fossies" - the Fresh Open Source Software Archive 
Member "hashcat-6.2.6/src/affinity.c" (2 Sep 2022, 3738 Bytes) of package /linux/privat/hashcat-6.2.6.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "affinity.c" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
6.2.5_vs_6.2.6.
1 /**
2 * Author......: See docs/credits.txt
3 * License.....: MIT
4 */
5
6 #include "common.h"
7 #include "types.h"
8 #include "memory.h"
9 #include "event.h"
10 #include "affinity.h"
11
12 #if defined (__APPLE__)
13 static void CPU_ZERO (cpu_set_t *cs)
14 {
15 cs->count = 0;
16 }
17
18 static void CPU_SET (int num, cpu_set_t *cs)
19 {
20 cs->count |= (1 << num);
21 }
22
23 static int CPU_ISSET (int num, cpu_set_t *cs)
24 {
25 return (cs->count & (1 << num));
26 }
27
28 static int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set)
29 {
30 int core;
31
32 for (core = 0; core < (8 * (int) cpu_size); core++)
33 {
34 if (CPU_ISSET (core, cpu_set)) break;
35 }
36
37 thread_affinity_policy_data_t policy = { core };
38
39 return thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1);
40 }
41 #endif
42
43 #if defined (__FreeBSD__)
44 #include <pthread_np.h>
45 typedef cpuset_t cpu_set_t;
46 #endif
47
48 #if defined(__NetBSD__)
49 #include <pthread.h>
50 #include <sched.h>
51 typedef cpuset_t cpu_set_t;
52 #endif
53
54 int set_cpu_affinity (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx)
55 {
56 #if defined (__CYGWIN__)
57 return 0;
58 #else
59 const user_options_t *user_options = hashcat_ctx->user_options;
60
61 if (user_options->cpu_affinity == NULL) return 0;
62
63 char *devices = hcstrdup (user_options->cpu_affinity);
64
65 if (devices == NULL) return -1;
66
67 #if defined (_WIN)
68 DWORD_PTR aff_mask = 0;
69 const int cpu_id_max = 8 * sizeof (aff_mask);
70 #elif defined(__NetBSD__)
71 cpuset_t * cpuset;
72 const int cpu_id_max = 8 * cpuset_size (cpuset);
73 cpuset = cpuset_create ();
74 if (cpuset == NULL)
75 {
76 event_log_error (hashcat_ctx, "cpuset_create() failed with error: %d", errno);
77
78 hcfree (devices);
79
80 return -1;
81 }
82 #else
83 cpu_set_t cpuset;
84 const int cpu_id_max = 8 * sizeof (cpuset);
85 CPU_ZERO (&cpuset);
86 #endif
87
88 char *saveptr = NULL;
89
90 char *next = strtok_r (devices, ",", &saveptr);
91
92 do
93 {
94 const int cpu_id = (const int) strtol (next, NULL, 10);
95
96 if (cpu_id == 0)
97 {
98 #if defined (_WIN)
99 aff_mask = 0;
100 #elif defined (__NetBSD__)
101 cpuset_destroy (cpuset);
102 cpuset = cpuset_create ();
103 if (cpuset == NULL)
104 {
105 event_log_error (hashcat_ctx, "cpuset_create() failed with error: %d", errno);
106
107 hcfree (devices);
108
109 return -1;
110 }
111 #else
112 CPU_ZERO (&cpuset);
113 #endif
114
115 break;
116 }
117
118 if (cpu_id > cpu_id_max)
119 {
120 event_log_error (hashcat_ctx, "Invalid cpu_id %d specified.", cpu_id);
121
122 #if defined (__NetBSD__)
123 cpuset_destroy (cpuset);
124 #endif
125
126 hcfree (devices);
127
128 return -1;
129 }
130
131 #if defined (_WIN)
132 aff_mask |= ((DWORD_PTR) 1) << (cpu_id - 1);
133 #elif defined (__NetBSD__)
134 cpuset_set (cpu_id - 1, cpuset);
135 #else
136 CPU_SET ((cpu_id - 1), &cpuset);
137 #endif
138
139 } while ((next = strtok_r ((char *) NULL, ",", &saveptr)) != NULL);
140
141 #if defined (__NetBSD__)
142 cpuset_destroy (cpuset);
143 #endif
144
145 hcfree (devices);
146
147 #if defined (_WIN)
148
149 if (SetProcessAffinityMask (GetCurrentProcess (), aff_mask) == 0)
150 {
151 event_log_error (hashcat_ctx, "SetProcessAffinityMask() failed with error: %d", (int) GetLastError ());
152
153 return -1;
154 }
155
156 #elif defined (__NetBSD__)
157
158 pthread_t thread = pthread_self ();
159
160 const int rc = pthread_setaffinity_np (thread, cpuset_size (cpuset), cpuset);
161
162 if (rc != 0)
163 {
164 event_log_error (hashcat_ctx, "pthread_setaffinity_np() failed with error: %d", rc);
165
166 return -1;
167 }
168
169 #else
170
171 pthread_t thread = pthread_self ();
172
173 const int rc = pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset);
174
175 if (rc != 0)
176 {
177 event_log_error (hashcat_ctx, "pthread_setaffinity_np() failed with error: %d", rc);
178
179 return -1;
180 }
181
182 #endif
183
184 return 0;
185 #endif
186 }