Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VHPIDIRECT callback on simulation termination #894

Open
vdkeybus opened this issue Aug 15, 2019 · 5 comments
Open

VHPIDIRECT callback on simulation termination #894

vdkeybus opened this issue Aug 15, 2019 · 5 comments

Comments

@vdkeybus
Copy link
Contributor

vdkeybus commented Aug 15, 2019

Is your feature request related to a problem? Please describe.
When using a foreign language interface (VHPIDIRECT), it is possible to do some initialization e.g. at the beginning of a process. However, there's no easy way to detect that a simulation ends. That would be useful to close files or sockets, free malloc'ed memory or print a final diagnostic message.

Describe the solution you'd like
Could an attribute be added like:
attribute foreign of function : procedure is "VHPIDIRECT_EXIT <function_exit>";
that causes function_exit() to be called before termination ?

Describe alternatives you've considered
You can track 'now' as a variable in your foreign function / procedure. But then you'd have to keep in sync with --stop-time, and be unable to get final foreign function output upon a FAILURE assertion.

Additional context
Get rid of dirty termination of foreign code.

Thanks.

@eine
Copy link
Collaborator

eine commented Aug 15, 2019

Hi @vdkeybus! Can you please provide a minimal example of what you are trying to achieve? I believe that the problem you are having is related to the version of VHDL that you are using. The behaviour is very different with --std=93 or --std=08. With VHDL 2008 you should be all good.

With VHDL 93, when the simulator exits cleanly, you can use atexit to register an exit_handler as in https://github.com/VUnit/vunit/blob/36d63f1645ae64b3de86142579433a9c5feaee7a/examples/vhdl/external_buffer/src/main.c. I believe that this is equivalent to what you mean with VHPIDIRECT_EXIT <function_exit>, isn't it?

However, when the simulation is finished with a failure in VHDL 93, you can get an abortion. In this context, SIGABRT can be used as a handler, as shown in https://github.com/VUnit/vunit/blob/36d63f1645ae64b3de86142579433a9c5feaee7a/examples/vhdl/external_buffer/sigabrt.md. This approach is limited, tho. For example, if the GHDL binary is loaded and executed from Python, there seems not to be possible to execute any Python cleanup code after the abortion of GHDL (see VUnit/vunit#469 (comment)).

Note that the abortion is not expected. It is being tracked in #803, but the issue has not been updated since april. It might be worth trying with latest master.

@vdkeybus
Copy link
Contributor Author

@1138-4eb I am going to try the atexit() solution you propose. I wasn't aware you could use it. I'll try to catch SIGABRT for good measure too.

So the feature already exists. That's good. I'm now wondering, are FAILURE assertions actually implemented using signals ? So, could I also terminate a GHDL run that way from C ?

BTW thanks for analyzing my request and compiling the elaborate response. Is there a way we can include this kind of useful information in the foreign language manual section ?

@eine
Copy link
Collaborator

eine commented Aug 15, 2019

@1138-4eb I am going to try the atexit() solution you propose. I wasn't aware you could use it.

In practice, ghdl_main is "just another regular C function". Hence, you can use regular C tooling/libraries. For example, you can potentially use pthreads and dlopen to load two binaries generated with ghdl and effectively co-simulate two independent designs, while using C to allocate/provide a shared memory space.

I'll try to catch SIGABRT for good measure too.

So the feature already exists. That's good. I'm now wondering, are FAILURE assertions actually implemented using signals ? So, could I also terminate a GHDL run that way from C?

Note that this is likely to be a bug. According to @tgingold, ghdl should not exit with an abortion, not even after a simulation failure. See #800 (comment). So, it is probably not ghdl who is producing the signal, but some other OS logic in-between C main and ghdl_main.

Nonetheless, I think you should be able to terminate a GHDL run that way from C. I mean, this is not dependent on ghdl's codebase but on the context/system that loads the function, isn't it?

BTW thanks for analyzing my request and compiling the elaborate response.

You are welcome.

Is there a way we can include this kind of useful information in the foreign language manual section?

Actually, I'm the one who did latest updates to that section: https://github.com/ghdl/ghdl/blob/master/doc/using/Foreign.rst#id4. I'd be glad to review a PR if you are willing to propose it. I have not added it myself yet because I'd like to investigate the abortion issue further. Nevertheless, it is ok to add it with some hint/warning if you think this is useful.

@tgingold
Copy link
Member

There is the endofsimulation vpi/vhpi callback for this purpose. That's heavier to use than a vhpidirect function but that's the standard way.

@vdkeybus
Copy link
Contributor Author

vdkeybus commented Aug 17, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants