News:

  • May 01, 2024, 04:47:00 PM

Login with username, password and session length

Author Topic: Program Blocks, Yielding, TimeSlices etc.  (Read 2289 times)

Dean

  • Sr. Member
  • ****
  • Posts: 73
Program Blocks, Yielding, TimeSlices etc.
« on: October 08, 2015, 09:49:24 AM »
I've only written a couple of small applications using program blocks,and have never required anything but the default "never yield" and 65535 micro-second time slice. I think I understand the usefulness of the yield instruction when things get larger, but I guess my question is in regard to a block that you want to run continuously. How does one determine a proper time slice value? Is it a matter of trial and error, or is there a way in the Designer to analyze this, and determine the proper time slice values for program blocks? Another question too if you don't mind. I've read the Designer help files, but wondered if anyone could point me to any resources online or otherwise what I could read to better understand this concept.
10 Lather
20 Rinse
30 GOTO 10

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 5990
  • Yes Pinky, Do-more will control the world!
Re: Program Blocks, Yielding, TimeSlices etc.
« Reply #1 on: October 08, 2015, 11:23:22 AM »
We originally had $Main yielding like any other program block. The advantage is that if you stick a long loop in there, it quietly works without popping the watchdog. We changed it in the interest of trying to make it more like a conventional PLC, and decided that a looping watchdog failure would simply be a "teachable moment".

The "right" value is simply an evaluation of the trade-offs between scan time and total execution time...meaning there is no "right" answer, only trade-offs that suit you better. How about an example: Let's say I want to do a bubble sort of 1000 samples. That will take like a bazillion loop iterations...999+998+997+...+1...a bunch. With a time slice of 0, the routine will yield at the NEXT of every loop. Assuming a nominal scan time, that will take like...forever. If the time slice were set to never yield, it would run as fast as possible, but if each iteration took only 10us, that would take about 5 seconds...well over the watchdog and completely unsuitable for a PLC. So in the first case, we have effectively zero bump to the scan time, but the sort will take 5 or 10 minutes to complete, and in the second case, it could be done in 5 seconds, but the PLC doesn't do anything else (like solving logic) while doing so. So, somewhere between those two extremes is a balance between getting it done and remaining responsive.

The default time slice that we use for programs and tasks other than $Main is 100us. That's a pretty good balance between getting the work done and not disrupting the rest of the world.

There are no profiling tools yet, although that is on our wish list. I would just create some simple test cases (like a bubble sort) and play with time slice value, noting how changes in time slice affect completion time and scan time.

We do have some training materials that we have developed, but I'm not sure whether they are published or not. We would love to put those in your hands, although that probably can't happen until Monday. If you were to send a request to support@hosteng.com, I'm sure he would response back early next week.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Dean

  • Sr. Member
  • ****
  • Posts: 73
Re: Program Blocks, Yielding, TimeSlices etc.
« Reply #2 on: October 08, 2015, 12:29:12 PM »
Excellent explanation. Email sent. Thanks Bob.
10 Lather
20 Rinse
30 GOTO 10

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3665
    • Host Engineering
Re: Program Blocks, Yielding, TimeSlices etc.
« Reply #3 on: October 08, 2015, 06:59:45 PM »
Look at the bubble sort example project.  You can tweak the time slice to 0 to minimize scan time impact, but causing the bubble sort to take a long time to complete.  You can tweak the time slice to 1000 (microseconds or 1 millisecond), which adds 1 millisecond to your scan while it is sorting, but it greatly reduces the over all time to do the bubble sort.

There is not a hard fast rule for what is "best".  It is always a trade off between minimizing scan time hit and longer time to complete vs longer scan times and shorter time to complete the "task".  But with the fast execution times, you have a lot of wiggle room to try out different times slice values empirically.

Dean

  • Sr. Member
  • ****
  • Posts: 73
Re: Program Blocks, Yielding, TimeSlices etc.
« Reply #4 on: October 09, 2015, 09:08:26 AM »
franji1,

Will do. Thanks.
EDIT/
Holy Ladder Logic Batman! The stuff you guys can create is way over my head!
/EDIT
« Last Edit: October 09, 2015, 10:32:33 AM by Dean »
10 Lather
20 Rinse
30 GOTO 10