Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
tsn-measures
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
tsn-measures
Commits
702faf9e
Commit
702faf9e
authored
May 08, 2020
by
Joanne Hugé
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add CPU affinity option to server and client
parent
e01d6464
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
2 deletions
+39
-2
packet-exchange/src/client.c
packet-exchange/src/client.c
+20
-1
packet-exchange/src/server.c
packet-exchange/src/server.c
+19
-1
No files found.
packet-exchange/src/client.c
View file @
702faf9e
#define _GNU_SOURCE
#include <errno.h>
#include <error.h>
#include <pthread.h>
...
...
@@ -22,6 +24,9 @@ typedef struct thread_param {
int
interval
;
int
priority
;
int
max_cycles
;
int
enable_affinity
;
const
char
*
ip_address
;
thread_stat_t
stats
;
}
thread_param_t
;
...
...
@@ -39,13 +44,24 @@ static void *packet_sending_thread(void *p) {
struct
timespec
next
;
struct
sched_param
priority
;
thread_param_t
*
param
=
(
thread_param_t
*
)
p
;
cpu_set_t
mask
;
if
(
param
->
enable_affinity
)
{
// Set thread CPU affinity
CPU_ZERO
(
&
mask
);
CPU_SET
(
1
,
&
mask
);
if
(
sched_setaffinity
(
0
,
sizeof
(
mask
),
&
mask
))
fprintf
(
stderr
,
"Could not set CPU affinity to CPU #1
\n
"
);
}
// Set thread priority
priority
.
sched_priority
=
param
->
priority
;
if
(
sched_setscheduler
(
0
,
SCHED_FIFO
,
&
priority
))
error
(
EXIT_FAILURE
,
errno
,
"Couldn't set priority"
);
clock_gettime
(
CLOCK_ID
,
&
next
);
// Packet sending loop
for
(
param
->
stats
.
nb_cycles
=
0
;;
param
->
stats
.
nb_cycles
++
)
{
if
(
param
->
max_cycles
)
if
(
param
->
stats
.
nb_cycles
>=
param
->
max_cycles
)
break
;
...
...
@@ -104,11 +120,14 @@ int main(int argc, char *argv[]) {
static
void
process_options
(
int
argc
,
char
*
argv
[],
thread_param_t
*
param
,
main_param_t
*
main_param
)
{
for
(;;)
{
int
c
=
getopt
(
argc
,
argv
,
"i:l:p:r:"
);
int
c
=
getopt
(
argc
,
argv
,
"
a
i:l:p:r:"
);
if
(
c
==
-
1
)
break
;
switch
(
c
)
{
case
'a'
:
param
->
enable_affinity
=
1
;
break
;
case
'i'
:
param
->
interval
=
atoi
(
optarg
)
*
1000
;
break
;
...
...
packet-exchange/src/server.c
View file @
702faf9e
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <error.h>
...
...
@@ -30,6 +32,8 @@ typedef struct thread_param {
int
priority
;
thread_stat_t
stats
;
int
enable_affinity
;
int
sockfd
;
}
thread_param_t
;
...
...
@@ -53,6 +57,7 @@ static void *packet_receiving_thread(void *p) {
thread_param_t
*
param
=
(
thread_param_t
*
)
p
;
thread_stat_t
*
stats
=
&
param
->
stats
;
uint64_t
diff
=
0
;
cpu_set_t
mask
;
char
buf
[
BUFFER_SIZE
];
int
bytes_received
=
0
;
...
...
@@ -64,10 +69,20 @@ static void *packet_receiving_thread(void *p) {
stats
->
min_interval
=
UINT64_MAX
;
stats
->
max_interval
=
0
;
if
(
param
->
enable_affinity
)
{
// Set thread CPU affinity
CPU_ZERO
(
&
mask
);
CPU_SET
(
1
,
&
mask
);
if
(
sched_setaffinity
(
0
,
sizeof
(
mask
),
&
mask
))
fprintf
(
stderr
,
"Could not set CPU affinity to CPU #1
\n
"
);
}
// Set thread priority
priority
.
sched_priority
=
param
->
priority
;
if
(
sched_setscheduler
(
0
,
SCHED_FIFO
,
&
priority
))
error
(
EXIT_FAILURE
,
errno
,
"Couldn't set priority"
);
// Packet receiving loop
for
(
stats
->
packets_received
=
0
;;
stats
->
packets_received
++
)
{
bytes_received
=
recvfrom
(
param
->
sockfd
,
buf
,
BUFFER_SIZE
-
1
,
0
,
(
struct
sockaddr
*
)
&
client_addr
,
&
addr_len
);
...
...
@@ -124,11 +139,14 @@ int main(int argc, char *argv[]) {
static
void
process_options
(
int
argc
,
char
*
argv
[],
thread_param_t
*
param
,
main_param_t
*
main_param
)
{
for
(;;)
{
int
c
=
getopt
(
argc
,
argv
,
"p:r:"
);
int
c
=
getopt
(
argc
,
argv
,
"
a
p:r:"
);
if
(
c
==
-
1
)
break
;
switch
(
c
)
{
case
'a'
:
param
->
enable_affinity
=
1
;
break
;
case
'p'
:
param
->
priority
=
atoi
(
optarg
);
break
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment