Calling function to run when idle in matlab - matlab

Is it possible in matlab to call a function when the program I'm running is idle? I don't want this to be a parallel process. Also, I would prefer a solution where I could pause and resume the function when the main program has to run again. Kind of like an interrupt in embedded systems, in my case the main program is the interrupt.
how would I do this?

you could use a timer object to start / stop the second function. see the matlab documentation. See also this mathworks blog entry.

Related

Wait for Simulink model execution

I wrote a script file in MATLAB that executes two functions as shown below:
function TempFunction()
fcn1();
fcn2();
end
After fcn1() is executed, I want the program to wait for execution of a model in Simulink, and then execute fcn2().
How can I do that?
One way to do this would be to use a Model Callback - set up a 'StopFcn' to trigger the next thing you want to run.
Alternatively, you could go into a loop polling the 'SimulationStatus' - see this page in the doc. .
you can call simulink models as functions from MATLAB as sim('Model_Name')
Read more about the options in the docs: https://uk.mathworks.com/help/simulink/ug/using-the-sim-command.html
and the sim function: https://uk.mathworks.com/help/simulink/slref/sim.html

How to generate delay using eBPF kernel program

I'm trying to generate delay in acknowledgement using eBPF kernel program for egress packet.
I'm running the python+c program using bcc.
i tried mdelay/msleep/udelay etc functions with delay.h library in c, it gives me LLVM error that "Program using external function which can't be resolved".
Then I tried implementing sleep functionality :
Taking 3 variables:
tprev (which get the current time at starting of prog)
tnow(this gets current time as the loop starts and gets updates in each iteration with current time)
timer: this is the duration for which we want program to be delayed.
the while loop is: while((tnow - tprev) ≤ timer)
but ebpf prog treat it as n infinite loop and gives error that infinite loop detected.
Whereas it's not an infinite loop.
Is there a way to introduce delay in Ack or delay in general in eBPF program and How?
The short answer is no(not in eBPF). At least not as of kernel 5.18. This is because eBPF programs, particularly those running in the network stack are often called from code that should never sleep.
What perhaps is most useful in your case is that TC(Traffic Control) programs can ask TC to delay a packet for you. The actual delay happens outside of the eBPF program, in the TC subsystem. You can request TC to send the packet at a given time by setting __sk_buff->tstamp. Note: this only works for Egress(outgoing) traffic not Ingress(incomming) traffic. This behavior can also be triggered via TC configuration without using eBPF.
tried mdelay/msleep/udelay etc functions with delay.h library in c, it gives me LLVM error that "Program using external function which can't be resolved".
Yes, you can't use the stdlib in eBPF programs since they use kernel facilities which are unavailable in eBPF.
Side notes:
We do have "sleepable" programs, but only syscall, LSM and tracing programs can be sleepable. "sleepable" doesn't mean we can call some sort of sleep helper with a duration, it means we can call helper functions which in turn may sleep(for example bpf_copy_from_user_stack). So you don't have control over how long the program will sleep.
Another time related feature is BPF timers, which actually allow you to set a timer and execute a callback after a given time. The limitation here is that you can't pass any arguments to this callback, and it is called without a context. So after setting the timer, the original program will continue and return as usual.

Abort execution of parsim

For the use case of being able to abort parallel simulations with a MATLAB GUI, I would like to stop all scheduled simulations after the user pressed the Stop button.
All simulations are submitted at once using the parsim command, hence something like a callback to my GUI variables (App Designer) would be the most preferable solution.
Approaches I have considered but were not exactly providing a desirable solution:
The Simulation Manager provides the functionality to close simulations using its own interface. If I only had the code its Stop button executes...
parsim uses the Simulink.SimulationInput class as input to run simulations, allowing to modify the preSimFcn at the beginning of each simulation. I have not found a way to "skip" the simulation at its initialization phase apart from intentionally throwing an error so far.
Thank you for your help!
Update 1: Using the preSimFcn to set the the termination time equal to the start time drastically reduces simulation time. But since the first step still is computed there has to be a better solution.
simin = simin.setModelParameter('StopTime',get_param(mdl,'StartTime'))
Update 2: Intentionally throwing an error executing the preSimFcn, for example by setting it to
simin = simin.setModelParameter('SimulationCommand','stop')
provides the shortest termination times for me so far. Though, it requires catching and identifying the error in the ErrorMessageof the Simulink.SimulationOutput object. As this is exactly the "ugly" implementation I wanted to avoid, the issue is still active.
If you are using 17b or later, parsim provides an option to 'RunInBackground'. It returns an array of Future objects.
F = parsim(in, 'RunInBackground', 'on')
Please note that is only available for parallel simulations. The Simulink.Simulation.Future object F provides a cancel method which will terminate the simulation. You can use the fetchOutputs methods to fetch the output from the simulation.
F.cancel();

Why does the first call to a Matlab DLL take much longer than subsequent calls?

I call from my C++ code a DLL that was written in MATLAB.
I observe a strange effect: the first call takes much more time that the next calls.
It takes 3-4 times more.
Is it normal?
Is it possible to do something with it?
Yes that is normal, the delay comes from starting up the MATLAB Runtime Compiler. This is what runs the MATLAB code from the dll that you created through MATLAB. The initial startup cannot be avoided AFAIK, but you can maybe add a dummy call to the DLL when your application begins in order to avoid the "cost" later.

Matlab: Is it possible to create signal handlers (.m scripts)

I've looked through the documentation, etc, but I'm not seeing anything obvious. I'd like to have a signal handler that can intercept ^C, ^\, or some other keypress that could be used to interrupt a long-running script (each discrete computation is typically <1s) and allow it to exit gracefully and save current state.
Matlab does have event handlers for COM, but it's windows-only and I'm in a *nix environment.
If the answer is 'tough luck', I'm cool with that ... I'm just not seeing anything that says I'm SOL yet.
MATLAB already interprets ^C as an interrupt. You can use onCleanup objects to ensure that your program state is preserved correctly. I.e. something like:
function testFcn
x = onCleanup( #() disp('perform cleanup here...') );
for ii=1:1000, disp(ii), pause(1), end
Run the above and hit ^C when you get bored. Obviously, you can hook any function handle in to your onCleanup object. See also the reference page for onCleanup.