Hello, dear Community,
First of all,
thanks that you are there and helping such newbies as me in answering linux questions.
And second of all,
sorry for relatively long post
Before I will ask my question, I would like to describe my setup:
I have a client and server applications running on different host-mashines connected over an ethernet cable and communicating over a TCP protocol.
1. The client sends a packet to the server
2. server responds with the same packet after some delay.
3. The client measures time passed between sending own packet and receiving a packet from the server in ticks (later converted to nanoseconds).
The time measurements must be
as precise as possible. I use the following
article to do measurements.
I see two other aspects that must be observed to achieve the best precision:
1) The client process must be active all the time during the communication, i.e. not interrupted by other processes.
This can be achieved with the FIFO scheduling policy for this process, and it has to be assured, that:
- This process does not access any synchronized resources
- It has highest priority over all other processes
2) Related system calls are executed on the same core to avoid that CPU executes some other syscalls not related to this process.
So here are my questions:
1 If affinity of a specific CPU is set to execute my process, are the related syscalls (creating socket, sending packet, receiving packet) also executed on that CPU?
2 If not is it possible to achive that?
3 If not, I know that there is a PREEMPT-RT patch for linux kernel, that allows preempt CPU even when it executes a system call. Is it then a better solution?
4 I read somewhere, that setting a highest priority to my process can be a bad Idea. What can go wrong, when I do that, or what would be a better solution?
I would like to thank you beforehand for your help!
Here is a code snippet, that does the above (if I don't miss something).
Code:
void enable_real_time(){
id_t pid = getpid();
// set a process to use one of the real-time schedulers
int policy = SCHED_FIFO; // type of the real-time scheduler. this one preempts an executing process if it priority is lower that the priority of this (out) process
struct sched_param param;
param.__sched_priority = sched_get_priority_max(policy); // set highest priority to make sure our process is started without a delay
if(sched_setscheduler(pid, policy, ¶m) != 0 ){
fprintf(stderr,"\nError [enable_real_time]: failed setting real-time scheduler\n");
perror("sched_setscheduler");
}
else fprintf(stderr,"\nSuccess [enable_real_time]: Real-time scheduler is set\n");
fprintf(stderr,"PID: %d\n",pid);
fprintf(stderr,"Priority of this process: %d\n",param.__sched_priority);
}
Code:
void set_cpu_affinity(int cpu){
unsigned long int mask = 0;
mask = 1<<cpu;
unsigned int len = sizeof(mask);
if(sched_setaffinity(0, len, (cpu_set_t*) &mask) != 0){
fprintf(stderr,"\nError [set_cpu_affinity]: failed setting affinity\n");
perror("sched_setaffinity");
} else {
fprintf(stderr,"\nSuccess [set_cpu_affinity]: Processor affinity is set\n");
fprintf(stderr,"CPU used by this process: %d\n", sched_getcpu());
}
}