Skip to content

Commit 1b1e38c

Browse files
esyrSashan
andcommitted
perflib: add ability to set thread affinity
Co-Authored-by: Alexandr Nedvedicky <[email protected]> Signed-off-by: Eugene Syromiatnikov <[email protected]>
1 parent e78cd6a commit 1b1e38c

File tree

3 files changed

+355
-24
lines changed

3 files changed

+355
-24
lines changed

source/perflib/perfhelper.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <string.h>
1111
#include <openssl/crypto.h>
12+
#include <openssl/macros.h>
1213
#include <openssl/bio.h>
1314
#include <openssl/err.h>
1415
#include <openssl/ssl.h>

source/perflib/perflib.h

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# include <windows.h>
2222

2323
typedef HANDLE thread_t;
24+
typedef DWORD_PTR affinity_t;
2425

2526
# define strcasecmp(_a, _b) _stricmp((_a), (_b))
2627

@@ -29,6 +30,7 @@ typedef HANDLE thread_t;
2930
# include <pthread.h>
3031

3132
typedef pthread_t thread_t;
33+
typedef unsigned long affinity_t;
3234

3335
# endif
3436

@@ -37,10 +39,60 @@ struct thread_arg_st {
3739
size_t num;
3840
};
3941

40-
int perflib_run_thread(thread_t *t, struct thread_arg_st *arg);
42+
/**
43+
* A callback that allows setting CPU affinity for the threads being run.
44+
* Gets the set of available CPUs in the cpu_set argument and expected
45+
* to update it in accordance with the information provided in num and arg
46+
* arguments.
47+
*
48+
* Currently supported only on glibc on Linux (because of a non-privileged
49+
* pthread_attr_setaffinity_np, with a maximum of 1024 CPUs) and Windows
50+
* (with a limitation of using only the initial process group).
51+
*
52+
* @param[in,out] cpu_set On entering, contains the set of CPUs available
53+
* to the test. The callback is supposed
54+
* to update the set in accordance
55+
* with the information provided in num, cnt,
56+
* and arg parameters.
57+
* @param[in] cpu_set_bits Size of cpu_set_bits, in bits.
58+
* @param[in] num Index of a thread index being run,
59+
* counted from 0.
60+
* @param[in] cnt Total count of available CPUs
61+
* (popcnt(cpu_set_bits)).
62+
* @param arg An opaque pointer that a caller has provided
63+
* along the callback.
64+
* @return 1 on success, 0 on error.
65+
*/
66+
typedef int (*perflib_affinity_fn)(affinity_t *cpu_set, size_t cpu_set_bits,
67+
size_t num, size_t cnt, void *arg);
68+
69+
/**
70+
* A simple affinity callback that assigns each thread a single CPU
71+
* in a round robin fashion. arg must be NULL.
72+
*/
73+
int perflib_roundrobin_affinity(affinity_t *cpu_set_bits, size_t cpu_set_size,
74+
size_t num, size_t cnt, void *arg);
75+
76+
int perflib_run_thread_ex(thread_t *t, struct thread_arg_st *arg,
77+
perflib_affinity_fn affinity_cb,
78+
void *affinity_cb_arg);
79+
static ossl_unused ossl_inline int
80+
perflib_run_thread(thread_t *t, struct thread_arg_st *arg)
81+
{
82+
return perflib_run_thread_ex(t, arg, NULL, NULL);
83+
}
4184
int perflib_wait_for_thread(thread_t thread);
42-
int perflib_run_multi_thread_test(void (*f)(size_t), size_t threadcount,
43-
OSSL_TIME *duration);
85+
int perflib_run_multi_thread_test_ex(void (*f)(size_t), size_t threadcount,
86+
OSSL_TIME *duration,
87+
perflib_affinity_fn affinity_cb,
88+
void *affinity_cb_arg);
89+
static ossl_unused ossl_inline int
90+
perflib_run_multi_thread_test(void (*f)(size_t), size_t threadcount,
91+
OSSL_TIME *duration)
92+
{
93+
return perflib_run_multi_thread_test_ex(f, threadcount, duration,
94+
NULL, NULL);
95+
}
4496
char *perflib_mk_file_path(const char *dir, const char *file);
4597
char *perflib_glue_strings(const char *list[], size_t *out_len);
4698

0 commit comments

Comments
 (0)