juranga
Posts: 183
Joined: Fri Nov 06, 2015 11:39 am
Location: Basque Country

Priorities of the program

Wed Feb 17, 2016 11:12 am

Hi,

I am looking for information about optimizing the performance of my raspberry pi's program. There are really useful two commands that change the priority of my program: chrt and nice

I want to use them to get better performance but I do not know how I have to use. I have a script to run my program that executes:

1. sudo nice -n-20 ./MyProgram

Then, manually I set chrt command but the way I set it is not very comfortable. I have to run top command an see my program's PID number, for example 835. So now, I set chrt command.

2. sudo chrt -f -p 99 835

The problem is that when I set the second command (the priority is changed, I see in top window), my program give up writing in a csv (it is what It should do), and the other problem is that I want to do all this process automatically (set chrt command).


Any help would be very appreciate.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23874
Joined: Sat Jul 30, 2011 7:41 pm

Re: Priorities of the program

Wed Feb 17, 2016 11:46 am

Whilst nice will give the program more CPU time relative to the rest of the system, it won't improve performance that much, since if nothing else is happening on the device, there isn;t likely to be many more CPU cycles available to spend on the program.

You really need to optimise the code itself.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

User avatar
jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

Re: Priorities of the program

Wed Feb 17, 2016 1:00 pm

After your program fails with chrt, does the output of "dmesg" include the line "sched: RT throttling activated"?

"nice" affects how much CPU time your process will get, if the machine is overloaded. "chrt" allows your process to wake up whenever it wants to, preempting all normal tasks. Neither allows you to take 100% CPU forever. You cannot meaningfully mix the two.

You have still not explained how your program waits for and reads the sensor data, or detects that data has been missed.

juranga
Posts: 183
Joined: Fri Nov 06, 2015 11:39 am
Location: Basque Country

Re: Priorities of the program

Wed Feb 17, 2016 1:28 pm

Many thanks for your answers.

The sensor is sending data through UART at 200Hz and I have another GPS working at 5Hz that I read from USB. I measure the sample period and I save in a csv file with the rest of the data. Then, I analyze it using a matlab code and I can know if it has written correctly or it has missed some data.

When I set sudo chrt -f -p 99 835 command I can see that its NI parameters is set at -20 and PR to rt (real time).

User avatar
jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

Re: Priorities of the program

Wed Feb 17, 2016 2:06 pm

It should be almost impossible to miss data from a UART (ttyAMA0 or ttyUSBx), because the kernel will buffer up to 4KB for you. At sensible baud rates that is a good fraction of a second, so well above the latency of scheduling.

If the sample rate is known to be 200Hz, then I do not understand why you would need to measure it (every time).

juranga
Posts: 183
Joined: Fri Nov 06, 2015 11:39 am
Location: Basque Country

Re: Priorities of the program

Wed Feb 17, 2016 2:44 pm

I do not miss data from UART, the point is that after reading/writing sensors data there is a moment that the program lasts 0.5-3 seconds to write in an excel file (it is a concrete delay), so I want to delete this "delay" or improve it. I have ensured that this delay is created by csv writing functions debugging the program. Moreover, the Raspberry Pi is working in the limit, I measured the time that required different code blocks and they last 3-5 milliseconds. Right now, I am testing in a Raspberry Pi model 2 (I was using Model B+) and is working much better.

I want to improve the performance of my program as much as possible so I am interested in understand better nice and chrt command and how to use it.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23874
Joined: Sat Jul 30, 2011 7:41 pm

Re: Priorities of the program

Wed Feb 17, 2016 4:00 pm

juranga wrote:I do not miss data from UART, the point is that after reading/writing sensors data there is a moment that the program lasts 0.5-3 seconds to write in an excel file (it is a concrete delay), so I want to delete this "delay" or improve it. I have ensured that this delay is created by csv writing functions debugging the program. Moreover, the Raspberry Pi is working in the limit, I measured the time that required different code blocks and they last 3-5 milliseconds. Right now, I am testing in a Raspberry Pi model 2 (I was using Model B+) and is working much better.

I want to improve the performance of my program as much as possible so I am interested in understand better nice and chrt command and how to use it.
I don't think Nice or Chrt are going to help in this case.

You need to find out why the write to the CSV is taking so long - shouldn't be that long at all. Do you open the file, write a bit of data, then close it, then repeat? Open and close file, IIRC, can be relatively slow, so only do them at start and end of program.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

User avatar
rpdom
Posts: 15371
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Priorities of the program

Wed Feb 17, 2016 4:09 pm

jamesh wrote:You need to find out why the write to the CSV is taking so long - shouldn't be that long at all. Do you open the file, write a bit of data, then close it, then repeat? Open and close file, IIRC, can be relatively slow, so only do them at start and end of program.
+1
Exactly what I was going to say. I believe that closing the file will cause the buffers to be written out, which could take some time. If you are depending on the file being updated each time there may be better ways of doing it. Having the file on a ramfs partition may be faster for a start. Having a separate thread that does the actual writing may be another way.

It won't make any difference what priority you make your program if it is waiting on IO to happen.

User avatar
experix
Posts: 204
Joined: Mon Nov 10, 2014 7:39 pm
Location: Coquille OR
Contact: Website

Re: Priorities of the program

Wed Feb 17, 2016 4:34 pm

jojopi wrote:"chrt" allows your process to wake up whenever it wants to, preempting all normal tasks.
The man page speaks of selecting the scheduling policy and priority, with choices SCHED_OTHER, SCHED_FIFO, SCHED_RR, SCHED_BATCH, SCHED_IDLE, SCHED_RESET_ON_FORK for the policy. It does not say anything about waking up whenever it wants to-- indeed, how could it do that if it is sleeping? Please clarify.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Priorities of the program

Wed Feb 17, 2016 4:59 pm

http://man7.org/linux/man-pages/man7/sched.7.html

SCHED_FIFO allows the task to run as long as it wants to unless a higher priority task wants to run, even if there are other tasks with the same priority. SCHED_RR only allows it to run for a certain period before being preempted by task with the same priority. Most of the proceses running under Linux use SCHED_OTHER, which allows the task to be preempted by lower priority tasks in order to allow every task at least some time to run.

juranga
Posts: 183
Joined: Fri Nov 06, 2015 11:39 am
Location: Basque Country

Re: Priorities of the program

Thu Feb 18, 2016 9:42 am

Many thanks for your answers! :)

1. No, I do not open and close the file when I have to write something in the csv file. I open the file when it is created and I close the file when the program is finished.

2. I have made a lot of different test with different operating system (jessie and wheezy), Raspberry Pi 2 and Model B+, different SD cards and what I have discovered is that the culprit is the Wheezy operating system or the SD cards that have Wheezy installed. I can run the program with Model B+ and Jessie SD card and in despite of not being as fast as when I run in RPi 2, the csv writing interrupts do not appear... The best performance I get with RPi 2 and Jessie, and if I run using multi threading code, the performance is even better.

Trying with RPI 2 and Wheezy, the writing delays continue appearing...

3. Despite the fact that I know the culprit of the problem and I know how to solve it, I am really interested in know more about chrt and nice commands. I can run using nice command but I do not know how to apply chrt command, to set my program with Real Time priority and I think this could be improve a little bit more the performance.

juranga
Posts: 183
Joined: Fri Nov 06, 2015 11:39 am
Location: Basque Country

Re: Priorities of the program

Mon Feb 29, 2016 7:56 am

I am still looking for more information about chrt and nice commands. As I said, I can use correctly nice command when I execute my program but I do not know how I should use chrt command.


Any help would be really appreciate.

Return to “C/C++”