Can OSProcess functionality be implemented using FFI instead of plugin?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
34 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Nick
<snip>
Well, like opendbx, maybe because opengl has quite standard interface...
</snip>

and

<snip>
It's not that it's not doable, it's that we gonna reinvent gaz plant
and it gonna be so boring...
I'd like to see a proof of concept, even if we restrict to libc, libm,
kernel.dll, msvcrt.dll ...
</snip>

<snip>
Is the unix style select()
ubiquitous or should I use WaitForMultipleObject() on Windows? Are
specification of read/write streams implementation machine independant
(bsd/sysv/others...)
</snip>

Perhaps *a* way forward is to try to find existing projects which have already created cross-platform abstractions for platform specific functionality. Then we can use FFI to access that interface in a similar way to OpenGL and OpenDBX. For example NodeJs works across unixes - perhaps they have a useful cross-platform abstraction, boost  has abstractions of IPC etc 

Nick
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Eliot Miranda-2


On Sat, Jan 16, 2016 at 6:00 AM, Mariano Martinez Peck <[hidden email]> wrote:
 
Hi all,

Sorry for reviving an old thread but I thought it was better to continue the discussion here because of the context. 
As you may have read, the other day I released a first approeach to a subset of OSProcess based on FFI (posix_spwan() family of functions):


 And with that in mind, I wanted to share a few things with you. The main 2 problems I found with implementing this with FFI was:

1) We have all already agree and discussed that fork+exec cannot be done in separate FFI calls. So at the very min you need either a plugin method that does the fork()+exec() OR wrapping a lib like posix_spwan()

2) The other main problem, is, as you all said (and mostly  Nicolas), is the problems with the preprocessor (constants, macros, etc).

With all that said, I was able to get my stuff working. However, I am still using some primitives of OSProcess plugin because of 2). 

I read Eliot idea and what I don't like is the need of a C compiler in the user machine. I think that's a high constrain. Then Igor suggested that WE (developers and maintainers of a certain tool) are the ones that compiles the little C program to extract constant values etc and then WE provide as part of our source code, some packages with some SharedPool depending on the platform/OS. And Igor approach looked a bit better to me.



You misunderstand the proposal.  The C compiler is needed /only when changing the set of constants/, i.e. when /developing/ the interface.  The C compiler is /not/ needed when deploying.

The idea is to
a) at development time, e.g. when a new variable is added to a SharedPool containing platform constants, a C program is autogenerated that outputs in some format a description of the names and values of all the constants defined in the pool.  One convenient notation is e.g. STON.  For the purposes of this discussion let's assume we're using ston, but any format the image an parse (or indeed a shared object the image can load on teh current pkatform) will do.  The output of the autogenerated C program would be called something like <SharedPoolName>.<PlatformName>.ston, e.g. UnixConstants.MacOSX64.ston or UnixConstants.Linux32.ston.  The ston files can easily be parsed by facilities in the Smalltalk image.

b) when deploying the system to a set of platforms one includes all the relevant platform-specific ston files.

c) at startup the image checks its current platform.  If the platform is the same that it was saved on, no action is taken.  But if the platform as changed then the relevant ston file is selected, parsed, and the values for the variables in the shared pool updated to reflect the values of the current platform.

So the C compiler is only needed when developing the interface, not when deploying it.


Then Nicolas made a point that if we plan to manage all that complexity at the image level it may become a hell too. 

So.... what if we take a simpler (probably not better) approach and we consider the "c program that exports constants and sizes" a VM Plugin? Let's say we have a UnixPreprocessorPlugin (that would work for OSX, Linux and other's Unix I imagine for the time being) which provides a function (that is exported) which answers an array of arrays. For each constant, we include the name of the constant, the value, and the sizeof().  Then from image side, we simply do one FFI call, we get the large array and we adapt it to a SharedPool or whatever kind of object representing that info. 



This is what I suggestred in teh first place.  That what is autogenerated is a shared object (be it a plgin or a dll doesn't matter, it is machine code generated by a C compiler form an autogenerated C program compiled with the platform's C compiler) that can be loaded at run-time and interrogated to fetch the values of a set of variables.  But I think that the textual notation suggested above is simpler.  The test files are easier to distribute and change.  Shared objects and plugins have a habit of going stale, and there needs to be metadata in there to describe the set of constants etc, which is tricky to generate and parse because it is binary (pointer sizes, etc, etc).  Instead a simple textual format should be much more robust.  One could even edit by hand to add new constants.  It would be easy to make the textual file a versioned file.  Etc, etc.
 

I know that different users will need different constants. But let's say the infrastructure (plugin etc) is already done. And let's say I am a user that I want to build something with FFI and I need some constants that I see are not defined. Then I can simply add the ones I need in the plugin, and next VM release will have those. If Cog gets moved to Github, then this is even easier. Everybody can do a PR with the constants he needs. And in fact, if we have the infrastructure in place, I think that we each of us spend half an hour, we may have almost everything we need. 

For example, I can add myself all those for signals (to use kill() from FFI), all those from fcntl (to make none blocking pipes), all those from wait()/waitpid() family (so that I can do a waitpid() with WNOHANG), etc etc etc.

I know it's not the best approach but it's something that could be done very easily and would allow A LOT of stuff to be moved to FFI just because we have no access to preprocess constants or sizeof()  (to know how to allocate). I also know this won't cover macros and other stuff. But still.

If you think this is a good idea, I can spend the time to do it. 

Cheers, 









On Thu, May 10, 2012 at 10:09 AM, Nick Ager <[hidden email]> wrote:
<snip>
Well, like opendbx, maybe because opengl has quite standard interface...
</snip>

and

<snip>
It's not that it's not doable, it's that we gonna reinvent gaz plant
and it gonna be so boring...
I'd like to see a proof of concept, even if we restrict to libc, libm,
kernel.dll, msvcrt.dll ...
</snip>

<snip>
Is the unix style select()
ubiquitous or should I use WaitForMultipleObject() on Windows? Are
specification of read/write streams implementation machine independant
(bsd/sysv/others...)
</snip>

Perhaps *a* way forward is to try to find existing projects which have already created cross-platform abstractions for platform specific functionality. Then we can use FFI to access that interface in a similar way to OpenGL and OpenDBX. For example NodeJs works across unixes - perhaps they have a useful cross-platform abstraction, boost  has abstractions of IPC etc 

Nick



--




--
_,,,^..^,,,_
best, Eliot
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Eliot Miranda-2
In reply to this post by Nick


On Sat, Jan 16, 2016 at 4:37 PM, Levente Uzonyi <[hidden email]> wrote:

On Sat, 16 Jan 2016, Mariano Martinez Peck wrote:

(Still no quote.)

How will you read the output of the process without having your image's process blocked in the FFI callout?
How will you make sure that writes to input of the process won't block the FFI callout?

This presupposes the threaded FFI.  The threaded FFI allows the VM to make any number of blocking calls, adding a new thread to run the VM whenever the VM is stalled when the heartbeat beats.  hence one can freely read and write to/from i/o blocking i/o streams (including pipes and sockets) or blocking database connexions, etc, all without stating that the FFI call must be done in a special way, since all calls through the FFI can block without blocking the VM.

Note that the scheme is also amenable to plugins, but the plugins must be rewritten to include the release vm/acquire vm calls around a blocking call.  With the threaded VM the FFI includes these calls around every FFI call.

HTH
_,,,^..^,,,_
best, Eliot
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Mariano Martinez Peck
In reply to this post by Eliot Miranda-2


On Sat, Jan 16, 2016 at 11:02 PM, Eliot Miranda <[hidden email]> wrote:


On Sat, Jan 16, 2016 at 6:00 AM, Mariano Martinez Peck <[hidden email]> wrote:
 
Hi all,

Sorry for reviving an old thread but I thought it was better to continue the discussion here because of the context. 
As you may have read, the other day I released a first approeach to a subset of OSProcess based on FFI (posix_spwan() family of functions):


 And with that in mind, I wanted to share a few things with you. The main 2 problems I found with implementing this with FFI was:

1) We have all already agree and discussed that fork+exec cannot be done in separate FFI calls. So at the very min you need either a plugin method that does the fork()+exec() OR wrapping a lib like posix_spwan()

2) The other main problem, is, as you all said (and mostly  Nicolas), is the problems with the preprocessor (constants, macros, etc).

With all that said, I was able to get my stuff working. However, I am still using some primitives of OSProcess plugin because of 2). 

I read Eliot idea and what I don't like is the need of a C compiler in the user machine. I think that's a high constrain. Then Igor suggested that WE (developers and maintainers of a certain tool) are the ones that compiles the little C program to extract constant values etc and then WE provide as part of our source code, some packages with some SharedPool depending on the platform/OS. And Igor approach looked a bit better to me.



You misunderstand the proposal. 

I think I did. But let me confirm that below ;)
 
The C compiler is needed /only when changing the set of constants/, i.e. when /developing/ the interface.  The C compiler is /not/ needed when deploying.

The idea is to
a) at development time, e.g. when a new variable is added to a SharedPool containing platform constants, a C program is autogenerated that outputs in some format a description of the names and values of all the constants defined in the pool.  One convenient notation is e.g. STON.  For the purposes of this discussion let's assume we're using ston, but any format the image an parse (or indeed a shared object the image can load on teh current pkatform) will do.  The output of the autogenerated C program would be called something like <SharedPoolName>.<PlatformName>.ston, e.g. UnixConstants.MacOSX64.ston or UnixConstants.Linux32.ston.  The ston files can easily be parsed by facilities in the Smalltalk image.

b) when deploying the system to a set of platforms one includes all the relevant platform-specific ston files.


OK. But let me ask something. Below you said "be it a plugin or a dll doesn't matter". To autogenerate the C program, I must know which header files to include for each platform and probably a few others things. For example, besides exporting the value,  I would also like to export the sizeof(). At that depends how was the VM compiled, right?   So...my question is...if such a autogenerated C code could be part of the VM building (considering all the settings being assume when building), cannot I reuse the knowledge the VM already has? Like which header files to include, if it was compiled 32 bits or 64 bits, which C compiler to use, etc..

What I mean is if it would be easier if I take the SharedPool at VM building time, and from there I autogenerate (and run) the C code that would generate the output. Then, when we "deploy" the VM, we can deploy it with relevant platform specific ston files as you said. 

 
c) at startup the image checks its current platform.  If the platform is the same that it was saved on, no action is taken.  But if the platform as changed then the relevant ston file is selected, parsed, and the values for the variables in the shared pool updated to reflect the values of the current platform.

So the C compiler is only needed when developing the interface, not when deploying it.

 
OK 
 

Then Nicolas made a point that if we plan to manage all that complexity at the image level it may become a hell too. 

So.... what if we take a simpler (probably not better) approach and we consider the "c program that exports constants and sizes" a VM Plugin? Let's say we have a UnixPreprocessorPlugin (that would work for OSX, Linux and other's Unix I imagine for the time being) which provides a function (that is exported) which answers an array of arrays. For each constant, we include the name of the constant, the value, and the sizeof().  Then from image side, we simply do one FFI call, we get the large array and we adapt it to a SharedPool or whatever kind of object representing that info. 



This is what I suggestred in teh first place.  That what is autogenerated is a shared object (be it a plgin or a dll doesn't matter, it is machine code generated by a C compiler form an autogenerated C program compiled with the platform's C compiler) that can be loaded at run-time and interrogated to fetch the values of a set of variables

OK, got it. But still, it would be easier if the "platform" in this case is the "machine where we build the VM we will then distribute" right? i mean, I would like to put this in the CI jobs that automatically builds the VM, and not myself building for each platform. 

I mean, my main doubt is if this job of autogenerating C code, compile it, run it, export text file, and distribute text file with the VM, could be done as part of the VM building. 

 
.  But I think that the textual notation suggested above is simpler.  The test files are easier to distribute and change.  Shared objects and plugins have a habit of going stale, and there needs to be metadata in there to describe the set of constants etc, which is tricky to generate and parse because it is binary (pointer sizes, etc, etc).  Instead a simple textual format should be much more robust.  One could even edit by hand to add new constants.  It would be easy to make the textual file a versioned file.  Etc, etc.
 

OK. Got it. And do you think using X Macros for the autogenerated C (from the SharedPool) is a good idea?
And then I simply write a text file out of it. 

 

I know that different users will need different constants. But let's say the infrastructure (plugin etc) is already done. And let's say I am a user that I want to build something with FFI and I need some constants that I see are not defined. Then I can simply add the ones I need in the plugin, and next VM release will have those. If Cog gets moved to Github, then this is even easier. Everybody can do a PR with the constants he needs. And in fact, if we have the infrastructure in place, I think that we each of us spend half an hour, we may have almost everything we need. 

For example, I can add myself all those for signals (to use kill() from FFI), all those from fcntl (to make none blocking pipes), all those from wait()/waitpid() family (so that I can do a waitpid() with WNOHANG), etc etc etc.

I know it's not the best approach but it's something that could be done very easily and would allow A LOT of stuff to be moved to FFI just because we have no access to preprocess constants or sizeof()  (to know how to allocate). I also know this won't cover macros and other stuff. But still.

If you think this is a good idea, I can spend the time to do it. 

Cheers, 









On Thu, May 10, 2012 at 10:09 AM, Nick Ager <[hidden email]> wrote:
<snip>
Well, like opendbx, maybe because opengl has quite standard interface...
</snip>

and

<snip>
It's not that it's not doable, it's that we gonna reinvent gaz plant
and it gonna be so boring...
I'd like to see a proof of concept, even if we restrict to libc, libm,
kernel.dll, msvcrt.dll ...
</snip>

<snip>
Is the unix style select()
ubiquitous or should I use WaitForMultipleObject() on Windows? Are
specification of read/write streams implementation machine independant
(bsd/sysv/others...)
</snip>

Perhaps *a* way forward is to try to find existing projects which have already created cross-platform abstractions for platform specific functionality. Then we can use FFI to access that interface in a similar way to OpenGL and OpenDBX. For example NodeJs works across unixes - perhaps they have a useful cross-platform abstraction, boost  has abstractions of IPC etc 

Nick



--




--
_,,,^..^,,,_
best, Eliot



--
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Eliot Miranda-2
Hi Mariano,

On Sat, Jan 16, 2016 at 6:25 PM, Mariano Martinez Peck <[hidden email]> wrote:


On Sat, Jan 16, 2016 at 11:02 PM, Eliot Miranda <[hidden email]> wrote:


On Sat, Jan 16, 2016 at 6:00 AM, Mariano Martinez Peck <[hidden email]> wrote:
 
Hi all,

Sorry for reviving an old thread but I thought it was better to continue the discussion here because of the context. 
As you may have read, the other day I released a first approeach to a subset of OSProcess based on FFI (posix_spwan() family of functions):


 And with that in mind, I wanted to share a few things with you. The main 2 problems I found with implementing this with FFI was:

1) We have all already agree and discussed that fork+exec cannot be done in separate FFI calls. So at the very min you need either a plugin method that does the fork()+exec() OR wrapping a lib like posix_spwan()

2) The other main problem, is, as you all said (and mostly  Nicolas), is the problems with the preprocessor (constants, macros, etc).

With all that said, I was able to get my stuff working. However, I am still using some primitives of OSProcess plugin because of 2). 

I read Eliot idea and what I don't like is the need of a C compiler in the user machine. I think that's a high constrain. Then Igor suggested that WE (developers and maintainers of a certain tool) are the ones that compiles the little C program to extract constant values etc and then WE provide as part of our source code, some packages with some SharedPool depending on the platform/OS. And Igor approach looked a bit better to me.



You misunderstand the proposal. 

I think I did. But let me confirm that below ;)
 
The C compiler is needed /only when changing the set of constants/, i.e. when /developing/ the interface.  The C compiler is /not/ needed when deploying.

The idea is to
a) at development time, e.g. when a new variable is added to a SharedPool containing platform constants, a C program is autogenerated that outputs in some format a description of the names and values of all the constants defined in the pool.  One convenient notation is e.g. STON.  For the purposes of this discussion let's assume we're using ston, but any format the image an parse (or indeed a shared object the image can load on teh current pkatform) will do.  The output of the autogenerated C program would be called something like <SharedPoolName>.<PlatformName>.ston, e.g. UnixConstants.MacOSX64.ston or UnixConstants.Linux32.ston.  The ston files can easily be parsed by facilities in the Smalltalk image.

b) when deploying the system to a set of platforms one includes all the relevant platform-specific ston files.


OK. But let me ask something. Below you said "be it a plugin or a dll doesn't matter". To autogenerate the C program, I must know which header files to include for each platform and probably a few others things. For example, besides exporting the value,  I would also like to export the sizeof(). At that depends how was the VM compiled, right?   So...my question is...if such a autogenerated C code could be part of the VM building (considering all the settings being assume when building), cannot I reuse the knowledge the VM already has? Like which header files to include, if it was compiled 32 bits or 64 bits, which C compiler to use, etc..

I actually said that using text is easier than a dll.  So I'm saying  autogenerate a C program that outputs name-value pairs in some convenient textual representation, e.g. ston.  But answering your question...

The knowledge in the VM as to what header files are included *applies only to the include files the VM uses*.  The VM uses a subset of the platform.  It doesn't for example include any headers that define a database interface.  It doesn't include header files that define the interface to a UI tooklit such at GTK.  Etc, etc.  So in fact the VM *doesn't* include the knowledge one needs to determine the set of include files for an arbitrary FFI interface.  And even so, the include files that it does use are in the VM's platform source files, and that information is not readily accessible.

Let me summarise.  No, the VM cannot be used to determine the set of include files needed to generate constants used in an arbitrary FFI interface.

What I mean is if it would be easier if I take the SharedPool at VM building time, and from there I autogenerate (and run) the C code that would generate the output. Then, when we "deploy" the VM, we can deploy it with relevant platform specific ston files as you said. 

No.  The VM is something that provides an FFI.  It doesn't *define* an FFI.   One must be able to develop an FFI interface without needing to rebuild the VM.  So computing the values of constants should be *separate* from building a VM.  Now let me give you more of an example.

Let's say we define a subclass of SharedPool called FFISharedPool.   FFISharedPool 's job is to manage autogenerating a C file, compiling it for the platform, and organizing parsing the relevant output.  Let's say we use a convention like class-side pragmas to define include files, and compiler flags.  The VM provides two crucial pieces of information:

1. the platform name
2. the word size

One can't run a Mac OS VM on Linux, and one can't run a 64-bit VM on a 32-bit operating system.  So taking this information from the VM accurately tells the current system what ABI (application binary interface) to use, and that's what's important in generating the right constants. 

So we use these two pieces of information to index the method pragmas that tell us what specific files to include.

Let's imagine we subclass FFISharedPool to add a shared pool for constants for an SQL database.  We might have a class declaration like

FFISharedPool subclass: #MYSQLInterface
instanceVariableNames: ''
classVariableNames: 'MYSQL_DEFAULT_AUTH MYSQL_ENABLE_CLEARTEXT_PLUGIN MYSQL_INIT_COMMAND MYSQL_OPT_BIND MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS MYSQL_OPT_COMPRESS MYSQL_OPT_CONNECT_ATTR_DELETE MYSQL_OPT_CONNECT_ATTR_RESET'
poolDictionaries: ''
category: 'MYSQLInterface-Pools'

The job of FFISharedPool is to compute the right values for the class variables on every platform we want to deploy the MYSQL interface on.

So we need to know the relevant include files and C flags for each platform/word-size combination.  A few of them might look like


MYSQLInterface class methods for platform information
mac32
    "I describe the include files and C flags to use when developing a 32-bit MYSQL FFI interface on Mac OS X"
    <platformName: 'Mac OS' wordSize: 4>
    <cFlags: #('-m32') includeFiles: #('/opt/mysql/include32')>
    ^self "all the info is in the pragmas"

mac64
    "I describe the include files and C flags to use when developing a 64-bit MYSQL FFI interface on Mac OS X"
    <platformName: 'Mac OS' wordSize: 8>
    <cFlags: #('-m64') includeFiles: #('/opt/mysql/include64')>

The above might cause FFISharedPool to autogenerate files called MYSQLInterface.mac32.c & MYSQLInterface.mac64.c.  And these, when run, might output ston notation to MYSQLInterface.mac32.ston & MYSQLInterface.mac64.ston (or maybe to stdout which has to be redirected to MYSQLInterface.mac32.ston; whatever).

Now, you might use pragmas, or you might answer a Dictionary instance.  What ever style pleases you and seems convenient and readable.  But these methods define the necessary metadata (C flags, include paths, and ...?) for FFISharedPool to autogenerate the C program that, when compiled with the supplied C flags and run on the current platform, outputs the values for the constants the shared pool wants to define.


You can get fancy and have FFISharedPool autogenerate the C programs whenever one adds or removes a constant name.  Or you can require the programmer run something, e.g. MYSQLInterface generateInterfaces.  It's really nice if FFISharedPool submits the file to the C compiler automatically, but this can only work for e.g. 32 & 64 bit versions on a single platform.  You have to compile the autogenerated program on the relevant platform, with the necessary libraries and include files installed.

You could imagine a set of servers for different platforms so one could submit the autogenerated program for compilation and execution on each platform.  That's a facility I'd make it easy to implement.  I could imagine that a programmer whose company develops an FFI interface and deploys it on a number of platforms would love to be able to automate compiling and running the relevant autogenerated code on a set of servers.  I could imagine the Pharo community providing a set of servers upon which lots of software is installed for precisely this purpose. That means that people could develop FFI interfaces without even having to have the C compiler installed on their platform.

You could also add a C parser to FFISharedPool  that parses the post-preprocessed code and extracts function declarations.  But the important thing is autogenerating the C program so that it generates easily parsable output containing the values for the constants.  You can extend the system in interesting ways once you ave this core functionality implemented.

So once the program is autogenerated and compiled for the current platform, it is run and its output collected in a file whose name can be recognised by FFISharedPool.


Now the class side of FFISharedPool might be declared as

FFIShardPool class
instanceVariableNames: 'platformName wordSize'

and on start-up FFIShardPool could examine its subclasses, and for each whose platformName & wordSize do not match the current platform, search for all the matching FOOInterface.plat.ston files, parse them and update the subclasses' variables, and update that pool's platformName & wordSize.  It could emit a warning on the Transcript or stdout (headful vs headless) indicating which subclasses it couldn't find the relevant FOOInterface.plat.ston files for.

But the end result is that

a) providing the system is deployed with FOOInterface.plat.ston files for each interface and platform used, a cross-platform application can be deployed *that does not require a C compiler*.
b) providing that a system's FOOInterface files have been initialized on the intended platform, a platform-specific application can be deployed for a single platform *without needing the ston files*.

Does this make more sense now?

c) at startup the image checks its current platform.  If the platform is the same that it was saved on, no action is taken.  But if the platform as changed then the relevant ston file is selected, parsed, and the values for the variables in the shared pool updated to reflect the values of the current platform.

So the C compiler is only needed when developing the interface, not when deploying it.

 
OK 
 

Then Nicolas made a point that if we plan to manage all that complexity at the image level it may become a hell too. 

So.... what if we take a simpler (probably not better) approach and we consider the "c program that exports constants and sizes" a VM Plugin? Let's say we have a UnixPreprocessorPlugin (that would work for OSX, Linux and other's Unix I imagine for the time being) which provides a function (that is exported) which answers an array of arrays. For each constant, we include the name of the constant, the value, and the sizeof().  Then from image side, we simply do one FFI call, we get the large array and we adapt it to a SharedPool or whatever kind of object representing that info. 



This is what I suggestred in teh first place.  That what is autogenerated is a shared object (be it a plgin or a dll doesn't matter, it is machine code generated by a C compiler form an autogenerated C program compiled with the platform's C compiler) that can be loaded at run-time and interrogated to fetch the values of a set of variables

OK, got it. But still, it would be easier if the "platform" in this case is the "machine where we build the VM we will then distribute" right? i mean, I would like to put this in the CI jobs that automatically builds the VM, and not myself building for each platform. 

NO!  For example, why would a company that has some proprietary arithmetic package implemented in its secret labs in C or C++ and accessed through the FFI want to have that code on the Pharo community's build servers?
 

I mean, my main doubt is if this job of autogenerating C code, compile it, run it, export text file, and distribute text file with the VM, could be done as part of the VM building. 

For fuck's sake.  Developing an FFI is not something one does when building a VM.  It is something one does wen using the system.  f you want to do this you *use a plugin*.  The FFI is a different beast.  It is to allow programers to interface to external librarys that are *independent from teh VM*.

I'm not going to answer this one again.  OK?

 

 
.  But I think that the textual notation suggested above is simpler.  The test files are easier to distribute and change.  Shared objects and plugins have a habit of going stale, and there needs to be metadata in there to describe the set of constants etc, which is tricky to generate and parse because it is binary (pointer sizes, etc, etc).  Instead a simple textual format should be much more robust.  One could even edit by hand to add new constants.  It would be easy to make the textual file a versioned file.  Etc, etc.
 

OK. Got it. And do you think using X Macros for the autogenerated C (from the SharedPool) is a good idea?
And then I simply write a text file out of it. 

 

I know that different users will need different constants. But let's say the infrastructure (plugin etc) is already done. And let's say I am a user that I want to build something with FFI and I need some constants that I see are not defined. Then I can simply add the ones I need in the plugin, and next VM release will have those. If Cog gets moved to Github, then this is even easier. Everybody can do a PR with the constants he needs. And in fact, if we have the infrastructure in place, I think that we each of us spend half an hour, we may have almost everything we need. 

For example, I can add myself all those for signals (to use kill() from FFI), all those from fcntl (to make none blocking pipes), all those from wait()/waitpid() family (so that I can do a waitpid() with WNOHANG), etc etc etc.

I know it's not the best approach but it's something that could be done very easily and would allow A LOT of stuff to be moved to FFI just because we have no access to preprocess constants or sizeof()  (to know how to allocate). I also know this won't cover macros and other stuff. But still.

If you think this is a good idea, I can spend the time to do it. 

Cheers, 









On Thu, May 10, 2012 at 10:09 AM, Nick Ager <[hidden email]> wrote:
<snip>
Well, like opendbx, maybe because opengl has quite standard interface...
</snip>

and

<snip>
It's not that it's not doable, it's that we gonna reinvent gaz plant
and it gonna be so boring...
I'd like to see a proof of concept, even if we restrict to libc, libm,
kernel.dll, msvcrt.dll ...
</snip>

<snip>
Is the unix style select()
ubiquitous or should I use WaitForMultipleObject() on Windows? Are
specification of read/write streams implementation machine independant
(bsd/sysv/others...)
</snip>

Perhaps *a* way forward is to try to find existing projects which have already created cross-platform abstractions for platform specific functionality. Then we can use FFI to access that interface in a similar way to OpenGL and OpenDBX. For example NodeJs works across unixes - perhaps they have a useful cross-platform abstraction, boost  has abstractions of IPC etc 

Nick



--




--
_,,,^..^,,,_
best, Eliot



--



--
_,,,^..^,,,_
best, Eliot
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Thierry Goubier
Hi Eliot,

I still don't understand why this platform-specific data (external
datatypes sizes, constants values)  has to be written as a json file and
not as Smalltalk code ?

If I write an interface to an external lib, I'd like to benefit from
nice access to Smalltalk code to, say, test versions of the external
libraries, set external types based on system information, etc, etc...
instead of having to deal with all those various json files outside my
normal workflow.

Okay, I can write smalltalk code which runs those C tests programs,
extract their output, model them in smalltalk and generate the jsons
from there... but, then, I really don't understand why I have to write
those jsons then every user loads those jsons instead of just running
that setup code I have in my external library smalltalk support package ?

Thierry

(Which is going through a hardware/system  capabilities description
language for high performance computing adaptation meeting at the moment
and where one see issues about "static" external information kept in files)

Le 17/01/2016 04:40, Eliot Miranda a écrit :

> Hi Mariano,
>
> On Sat, Jan 16, 2016 at 6:25 PM, Mariano Martinez Peck
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>
>
>     On Sat, Jan 16, 2016 at 11:02 PM, Eliot Miranda
>     <[hidden email] <mailto:[hidden email]>> wrote:
>
>
>
>         On Sat, Jan 16, 2016 at 6:00 AM, Mariano Martinez Peck
>         <[hidden email] <mailto:[hidden email]>> wrote:
>
>
>             Hi all,
>
>             Sorry for reviving an old thread but I thought it was better
>             to continue the discussion here because of the context.
>             As you may have read, the other day I released a first
>             approeach to a subset of OSProcess based on FFI
>             (posix_spwan() family of functions):
>
>             https://github.com/marianopeck/OSSubprocess
>
>               And with that in mind, I wanted to share a few things with
>             you. The main 2 problems I found with implementing this with
>             FFI was:
>
>             1) We have all already agree and discussed that fork+exec
>             cannot be done in separate FFI calls. So at the very min you
>             need either a plugin method that does the fork()+exec() OR
>             wrapping a lib like posix_spwan()
>
>             2) The other main problem, is, as you all said (and mostly
>               Nicolas), is the problems with the preprocessor
>             (constants, macros, etc).
>
>             With all that said, I was able to get my stuff working.
>             However, I am still using some primitives of OSProcess
>             plugin because of 2).
>
>             I read Eliot idea and what I don't like is the need of a C
>             compiler in the user machine. I think that's a high
>             constrain. Then Igor suggested that WE (developers and
>             maintainers of a certain tool) are the ones that compiles
>             the little C program to extract constant values etc and then
>             WE provide as part of our source code, some packages with
>             some SharedPool depending on the platform/OS. And Igor
>             approach looked a bit better to me.
>
>
>
>
>         You misunderstand the proposal.
>
>
>     I think I did. But let me confirm that below ;)
>
>         The C compiler is needed /only when changing the set of
>         constants/, i.e. when /developing/ the interface.  The C
>         compiler is /not/ needed when deploying.
>
>         The idea is to
>         a) at development time, e.g. when a new variable is added to a
>         SharedPool containing platform constants, a C program is
>         autogenerated that outputs in some format a description of the
>         names and values of all the constants defined in the pool.  One
>         convenient notation is e.g. STON.  For the purposes of this
>         discussion let's assume we're using ston, but any format the
>         image an parse (or indeed a shared object the image can load on
>         teh current pkatform) will do.  The output of the autogenerated
>         C program would be called something like
>         <SharedPoolName>.<PlatformName>.ston, e.g.
>         UnixConstants.MacOSX64.ston or UnixConstants.Linux32.ston.  The
>         ston files can easily be parsed by facilities in the Smalltalk
>         image.
>
>         b) when deploying the system to a set of platforms one includes
>         all the relevant platform-specific ston files.
>
>
>     OK. But let me ask something. Below you said "be it a plugin or a
>     dll doesn't matter". To autogenerate the C program, I must know
>     which header files to include for each platform and probably a few
>     others things. For example, besides exporting the value,  I would
>     also like to export the sizeof(). At that depends how was the VM
>     compiled, right?   So...my question is...if such a autogenerated C
>     code could be part of the VM building (considering all the settings
>     being assume when building), cannot I reuse the knowledge the VM
>     already has? Like which header files to include, if it was compiled
>     32 bits or 64 bits, which C compiler to use, etc..
>
>
> I actually said that using text is easier than a dll.  So I'm saying
>   autogenerate a C program that outputs name-value pairs in some
> convenient textual representation, e.g. ston.  But answering your
> question...
>
> The knowledge in the VM as to what header files are included *applies
> only to the include files the VM uses*.  The VM uses a subset of the
> platform.  It doesn't for example include any headers that define a
> database interface.  It doesn't include header files that define the
> interface to a UI tooklit such at GTK.  Etc, etc.  So in fact the VM
> *doesn't* include the knowledge one needs to determine the set of
> include files for an arbitrary FFI interface.  And even so, the include
> files that it does use are in the VM's platform source files, and that
> information is not readily accessible.
>
> Let me summarise.  No, the VM cannot be used to determine the set of
> include files needed to generate constants used in an arbitrary FFI
> interface.
>
>     What I mean is if it would be easier if I take the SharedPool at VM
>     building time, and from there I autogenerate (and run) the C code
>     that would generate the output. Then, when we "deploy" the VM, we
>     can deploy it with relevant platform specific ston files as you said.
>
>
> No.  The VM is something that provides an FFI.  It doesn't *define* an
> FFI.   One must be able to develop an FFI interface without needing to
> rebuild the VM.  So computing the values of constants should be
> *separate* from building a VM.  Now let me give you more of an example.
>
> Let's say we define a subclass of SharedPool called FFISharedPool.
> FFISharedPool 's job is to manage autogenerating a C file, compiling it
> for the platform, and organizing parsing the relevant output.  Let's say
> we use a convention like class-side pragmas to define include files, and
> compiler flags.  The VM provides two crucial pieces of information:
>
> 1. the platform name
> 2. the word size
>
> One can't run a Mac OS VM on Linux, and one can't run a 64-bit VM on a
> 32-bit operating system.  So taking this information from the VM
> accurately tells the current system what ABI (application binary
> interface) to use, and that's what's important in generating the right
> constants.
>
> So we use these two pieces of information to index the method pragmas
> that tell us what specific files to include.
>
> Let's imagine we subclass FFISharedPool to add a shared pool for
> constants for an SQL database.  We might have a class declaration like
>
> FFISharedPool subclass: #MYSQLInterface
> instanceVariableNames: ''
> classVariableNames: 'MYSQL_DEFAULT_AUTH MYSQL_ENABLE_CLEARTEXT_PLUGIN
> MYSQL_INIT_COMMAND MYSQL_OPT_BIND MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS
> MYSQL_OPT_COMPRESS
> MYSQL_OPT_CONNECT_ATTR_DELETE MYSQL_OPT_CONNECT_ATTR_RESET'
> poolDictionaries: ''
> category: 'MYSQLInterface-Pools'
>
> The job of FFISharedPool is to compute the right values for the class
> variables on every platform we want to deploy the MYSQL interface on.
>
> So we need to know the relevant include files and C flags for each
> platform/word-size combination.  A few of them might look like
>
>
> MYSQLInterface class methods for platform information
> mac32
>      "I describe the include files and C flags to use when developing a
> 32-bit MYSQL FFI interface on Mac OS X"
>      <platformName: 'Mac OS' wordSize: 4>
>      <cFlags: #('-m32') includeFiles: #('/opt/mysql/include32')>
>      ^self "all the info is in the pragmas"
>
> mac64
>      "I describe the include files and C flags to use when developing a
> 64-bit MYSQL FFI interface on Mac OS X"
>      <platformName: 'Mac OS' wordSize: 8>
>      <cFlags: #('-m64') includeFiles: #('/opt/mysql/include64')>
>
> The above might cause FFISharedPool to autogenerate files called
> MYSQLInterface.mac32.c & MYSQLInterface.mac64.c.  And these, when run,
> might output ston notation to MYSQLInterface.mac32.ston &
> MYSQLInterface.mac64.ston (or maybe to stdout which has to be redirected
> to MYSQLInterface.mac32.ston; whatever).
>
> Now, you might use pragmas, or you might answer a Dictionary instance.
> What ever style pleases you and seems convenient and readable.  But
> these methods define the necessary metadata (C flags, include paths, and
> ...?) for FFISharedPool to autogenerate the C program that, when
> compiled with the supplied C flags and run on the current platform,
> outputs the values for the constants the shared pool wants to define.
>
>
> You can get fancy and have FFISharedPool autogenerate the C programs
> whenever one adds or removes a constant name.  Or you can require the
> programmer run something, e.g. MYSQLInterface generateInterfaces.  It's
> really nice if FFISharedPool submits the file to the C compiler
> automatically, but this can only work for e.g. 32 & 64 bit versions on a
> single platform.  You have to compile the autogenerated program on the
> relevant platform, with the necessary libraries and include files installed.
>
> You could imagine a set of servers for different platforms so one could
> submit the autogenerated program for compilation and execution on each
> platform.  That's a facility I'd make it easy to implement.  I could
> imagine that a programmer whose company develops an FFI interface and
> deploys it on a number of platforms would love to be able to automate
> compiling and running the relevant autogenerated code on a set of
> servers.  I could imagine the Pharo community providing a set of servers
> upon which lots of software is installed for precisely this purpose.
> That means that people could develop FFI interfaces without even having
> to have the C compiler installed on their platform.
>
> You could also add a C parser to FFISharedPool  that parses the
> post-preprocessed code and extracts function declarations.  But the
> important thing is autogenerating the C program so that it generates
> easily parsable output containing the values for the constants.  You can
> extend the system in interesting ways once you ave this core
> functionality implemented.
>
> So once the program is autogenerated and compiled for the current
> platform, it is run and its output collected in a file whose name can be
> recognised by FFISharedPool.
>
>
> Now the class side of FFISharedPool might be declared as
>
> FFIShardPool class
> instanceVariableNames: 'platformName wordSize'
>
> and on start-up FFIShardPool could examine its subclasses, and for each
> whose platformName & wordSize do not match the current platform, search
> for all the matching FOOInterface.plat.ston files, parse them and update
> the subclasses' variables, and update that pool's platformName &
> wordSize.  It could emit a warning on the Transcript or stdout (headful
> vs headless) indicating which subclasses it couldn't find the relevant
> FOOInterface.plat.ston files for.
>
> But the end result is that
>
> a) providing the system is deployed with FOOInterface.plat.ston files
> for each interface and platform used, a cross-platform application can
> be deployed *that does not require a C compiler*.
> b) providing that a system's FOOInterface files have been initialized on
> the intended platform, a platform-specific application can be deployed
> for a single platform *without needing the ston files*.
>
> Does this make more sense now?
>
>         c) at startup the image checks its current platform.  If the
>         platform is the same that it was saved on, no action is taken.
>         But if the platform as changed then the relevant ston file is
>         selected, parsed, and the values for the variables in the shared
>         pool updated to reflect the values of the current platform.
>
>         So the C compiler is only needed when developing the interface,
>         not when deploying it.
>
>     OK
>
>
>             Then Nicolas made a point that if we plan to manage all that
>             complexity at the image level it may become a hell too.
>
>             So.... what if we take a simpler (probably not better)
>             approach and we consider the "c program that exports
>             constants and sizes" a VM Plugin? Let's say we have a
>             UnixPreprocessorPlugin (that would work for OSX, Linux and
>             other's Unix I imagine for the time being) which provides a
>             function (that is exported) which answers an array of
>             arrays. For each constant, we include the name of the
>             constant, the value, and the sizeof().  Then from image
>             side, we simply do one FFI call, we get the large array and
>             we adapt it to a SharedPool or whatever kind of object
>             representing that info.
>
>
>
>
>         This is what I suggestred in teh first place.  That what is
>         autogenerated is a shared object (be it a plgin or a dll doesn't
>         matter, it is machine code generated by a C compiler form an
>         autogenerated C program compiled with the platform's C compiler)
>         that can be loaded at run-time and interrogated to fetch the
>         values of a set of variables
>
>
>     OK, got it. But still, it would be easier if the "platform" in this
>     case is the "machine where we build the VM we will then distribute"
>     right? i mean, I would like to put this in the CI jobs that
>     automatically builds the VM, and not myself building for each platform.
>
>
> NO!  For example, why would a company that has some proprietary
> arithmetic package implemented in its secret labs in C or C++ and
> accessed through the FFI want to have that code on the Pharo community's
> build servers?
>
>
>     *I mean, my main doubt is if this job of autogenerating C code,
>     compile it, run it, export text file, and distribute text file with
>     the VM, could be done as part of the VM building. *
>
>
> For fuck's sake.  Developing an FFI is not something one does when
> building a VM.  It is something one does wen using the system.  f you
> want to do this you *use a plugin*.  The FFI is a different beast.  It
> is to allow programers to interface to external librarys that are
> *independent from teh VM*.
>
> I'm not going to answer this one again.  OK?
>
>
>         .  But I think that the textual notation suggested above is
>         simpler.  The test files are easier to distribute and change.
>         Shared objects and plugins have a habit of going stale, and
>         there needs to be metadata in there to describe the set of
>         constants etc, which is tricky to generate and parse because it
>         is binary (pointer sizes, etc, etc).  Instead a simple textual
>         format should be much more robust.  One could even edit by hand
>         to add new constants.  It would be easy to make the textual file
>         a versioned file.  Etc, etc.
>
>
>     OK. Got it. And do you think using X Macros for the autogenerated C
>     (from the SharedPool) is a good idea?
>     And then I simply write a text file out of it.
>
>
>             I know that different users will need different constants.
>             But let's say the infrastructure (plugin etc) is already
>             done. And let's say I am a user that I want to build
>             something with FFI and I need some constants that I see are
>             not defined. Then I can simply add the ones I need in the
>             plugin, and next VM release will have those. If Cog gets
>             moved to Github, then this is even easier. Everybody can do
>             a PR with the constants he needs. And in fact, if we have
>             the infrastructure in place, I think that we each of us
>             spend half an hour, we may have almost everything we need.
>
>             For example, I can add myself all those for signals (to use
>             kill() from FFI), all those from fcntl (to make none
>             blocking pipes), all those from wait()/waitpid() family (so
>             that I can do a waitpid() with WNOHANG), etc etc etc.
>
>             I know it's not the best approach but it's something that
>             could be done very easily and would allow A LOT of stuff to
>             be moved to FFI just because we have no access to preprocess
>             constants or sizeof()  (to know how to allocate). I also
>             know this won't cover macros and other stuff. But still.
>
>             If you think this is a good idea, I can spend the time to do
>             it.
>
>             Cheers,
>
>
>
>
>
>
>
>
>
>             On Thu, May 10, 2012 at 10:09 AM, Nick Ager
>             <[hidden email] <mailto:[hidden email]>> wrote:
>
>                 <snip>
>                 Well, like opendbx, maybe because opengl has quite
>                 standard interface...
>                 </snip>
>
>                 and
>
>                 <snip>
>                 It's not that it's not doable, it's that we gonna
>                 reinvent gaz plant
>                 and it gonna be so boring...
>                 I'd like to see a proof of concept, even if we restrict
>                 to libc, libm,
>                 kernel.dll, msvcrt.dll ...
>                 </snip>
>
>                 <snip>
>                 Is the unix style select()
>                 ubiquitous or should I use WaitForMultipleObject() on
>                 Windows? Are
>                 specification of read/write streams implementation
>                 machine independant
>                 (bsd/sysv/others...)
>                 </snip>
>
>                 Perhaps *a* way forward is to try to find existing
>                 projects which have already created cross-platform
>                 abstractions for platform specific functionality. Then
>                 we can use FFI to access that interface in a similar way
>                 to OpenGL and OpenDBX. For example NodeJs works across
>                 unixes - perhaps they have a useful cross-platform
>                 abstraction, boost  has abstractions of IPC etc
>
>                 Nick
>
>
>
>
>             --
>             Mariano
>             http://marianopeck.wordpress.com
>
>
>
>
>         --
>         _,,,^..^,,,_
>         best, Eliot
>
>
>
>
>     --
>     Mariano
>     http://marianopeck.wordpress.com
>
>
>
>
> --
> _,,,^..^,,,_
> best, Eliot


Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Eliot Miranda-2
Hi Thierry,

> On Jan 17, 2016, at 6:10 AM, Thierry Goubier <[hidden email]> wrote:
>
> Hi Eliot,
>
> I still don't understand why this platform-specific data (external datatypes sizes, constants values)  has to be written as a json file and not as Smalltalk code ?

It can be any notation that is easy to parse.  One could use pure Smalltalk, json, ston, xml.  I thought ston /was/ a form of Smalltalk aimed at representing key/value pairs, in which case it seems ideal.

What are the objectives for this notation? My take (feel free to suggest a better set) is:

a) be quick to parse since these files are parsed on start-up when the platform is different from that the image was snapshot red on

b) be human readable. The flexibility of being able to version, edit, search and diff these files seems useful

c) be reasonably straight-forward to generate from C.  But hey, it's only code :-)

>
> If I write an interface to an external lib, I'd like to benefit from nice access to Smalltalk code to, say, test versions of the external libraries, set external types based on system information, etc, etc... instead of having to deal with all those various json files outside my normal workflow.
>
> Okay, I can write smalltalk code which runs those C tests programs, extract their output, model them in smalltalk and generate the jsons from there... but, then, I really don't understand why I have to write those jsons then every user loads those jsons instead of just running that setup code I have in my external library smalltalk support package ?

You /don't/ have to write them ever.  They are written for you by the FFI shared pool.

So you're suggesting the C program output a Smalltalk method that looks like:e.g.

initializeMYSQLMac32
    MYSQL_ENABLE_CLEARTEXT_PLUGIN := 123.
    MYSQL_INIT_COMMAND := 234.
    MYSQL_OPT_BIND := 345

So the trade off you're discussing is whether it is valuable to keep these in the image or not.  For me these initialize toon methods are overhead.  They get parsed as infrequently as possible. So personally I prefer to use several external files that are parsed infrequently instead of the overhead of several initialization methods that are run infrequently.

For example, if I were deploying on some embedded arm device, the initialization method would be something I'd have to strip from the image.  Seems easier to just parse an external file to me.

I like the use of Smalltalk for eg user interface opening code where
- we want a UI construction tool to edit an e excitable representation of the interface
- there is only one version of the UI method

But with these interface descriptions we could easily end up with a 32-bit and a 64-bit version for every UNIX variant plus Mac OS and Windows.  You could easily have ten long methods with overheads in the tens of kilobytes.  Seems a waste to me.

> Thierry
>
> (Which is going through a hardware/system  capabilities description language for high performance computing adaptation meeting at the moment and where one see issues about "static" external information kept in files)
>
> Le 17/01/2016 04:40, Eliot Miranda a écrit :
>> Hi Mariano,
>>
>> On Sat, Jan 16, 2016 at 6:25 PM, Mariano Martinez Peck
>> <[hidden email] <mailto:[hidden email]>> wrote:
>>
>>
>>
>>    On Sat, Jan 16, 2016 at 11:02 PM, Eliot Miranda
>>    <[hidden email] <mailto:[hidden email]>> wrote:
>>
>>
>>
>>        On Sat, Jan 16, 2016 at 6:00 AM, Mariano Martinez Peck
>>        <[hidden email] <mailto:[hidden email]>> wrote:
>>
>>
>>            Hi all,
>>
>>            Sorry for reviving an old thread but I thought it was better
>>            to continue the discussion here because of the context.
>>            As you may have read, the other day I released a first
>>            approeach to a subset of OSProcess based on FFI
>>            (posix_spwan() family of functions):
>>
>>            https://github.com/marianopeck/OSSubprocess
>>
>>              And with that in mind, I wanted to share a few things with
>>            you. The main 2 problems I found with implementing this with
>>            FFI was:
>>
>>            1) We have all already agree and discussed that fork+exec
>>            cannot be done in separate FFI calls. So at the very min you
>>            need either a plugin method that does the fork()+exec() OR
>>            wrapping a lib like posix_spwan()
>>
>>            2) The other main problem, is, as you all said (and mostly
>>              Nicolas), is the problems with the preprocessor
>>            (constants, macros, etc).
>>
>>            With all that said, I was able to get my stuff working.
>>            However, I am still using some primitives of OSProcess
>>            plugin because of 2).
>>
>>            I read Eliot idea and what I don't like is the need of a C
>>            compiler in the user machine. I think that's a high
>>            constrain. Then Igor suggested that WE (developers and
>>            maintainers of a certain tool) are the ones that compiles
>>            the little C program to extract constant values etc and then
>>            WE provide as part of our source code, some packages with
>>            some SharedPool depending on the platform/OS. And Igor
>>            approach looked a bit better to me.
>>
>>
>>
>>
>>        You misunderstand the proposal.
>>
>>
>>    I think I did. But let me confirm that below ;)
>>
>>        The C compiler is needed /only when changing the set of
>>        constants/, i.e. when /developing/ the interface.  The C
>>        compiler is /not/ needed when deploying.
>>
>>        The idea is to
>>        a) at development time, e.g. when a new variable is added to a
>>        SharedPool containing platform constants, a C program is
>>        autogenerated that outputs in some format a description of the
>>        names and values of all the constants defined in the pool.  One
>>        convenient notation is e.g. STON.  For the purposes of this
>>        discussion let's assume we're using ston, but any format the
>>        image an parse (or indeed a shared object the image can load on
>>        teh current pkatform) will do.  The output of the autogenerated
>>        C program would be called something like
>>        <SharedPoolName>.<PlatformName>.ston, e.g.
>>        UnixConstants.MacOSX64.ston or UnixConstants.Linux32.ston.  The
>>        ston files can easily be parsed by facilities in the Smalltalk
>>        image.
>>
>>        b) when deploying the system to a set of platforms one includes
>>        all the relevant platform-specific ston files.
>>
>>
>>    OK. But let me ask something. Below you said "be it a plugin or a
>>    dll doesn't matter". To autogenerate the C program, I must know
>>    which header files to include for each platform and probably a few
>>    others things. For example, besides exporting the value,  I would
>>    also like to export the sizeof(). At that depends how was the VM
>>    compiled, right?   So...my question is...if such a autogenerated C
>>    code could be part of the VM building (considering all the settings
>>    being assume when building), cannot I reuse the knowledge the VM
>>    already has? Like which header files to include, if it was compiled
>>    32 bits or 64 bits, which C compiler to use, etc..
>>
>>
>> I actually said that using text is easier than a dll.  So I'm saying
>>  autogenerate a C program that outputs name-value pairs in some
>> convenient textual representation, e.g. ston.  But answering your
>> question...
>>
>> The knowledge in the VM as to what header files are included *applies
>> only to the include files the VM uses*.  The VM uses a subset of the
>> platform.  It doesn't for example include any headers that define a
>> database interface.  It doesn't include header files that define the
>> interface to a UI tooklit such at GTK.  Etc, etc.  So in fact the VM
>> *doesn't* include the knowledge one needs to determine the set of
>> include files for an arbitrary FFI interface.  And even so, the include
>> files that it does use are in the VM's platform source files, and that
>> information is not readily accessible.
>>
>> Let me summarise.  No, the VM cannot be used to determine the set of
>> include files needed to generate constants used in an arbitrary FFI
>> interface.
>>
>>    What I mean is if it would be easier if I take the SharedPool at VM
>>    building time, and from there I autogenerate (and run) the C code
>>    that would generate the output. Then, when we "deploy" the VM, we
>>    can deploy it with relevant platform specific ston files as you said.
>>
>>
>> No.  The VM is something that provides an FFI.  It doesn't *define* an
>> FFI.   One must be able to develop an FFI interface without needing to
>> rebuild the VM.  So computing the values of constants should be
>> *separate* from building a VM.  Now let me give you more of an example.
>>
>> Let's say we define a subclass of SharedPool called FFISharedPool.
>> FFISharedPool 's job is to manage autogenerating a C file, compiling it
>> for the platform, and organizing parsing the relevant output.  Let's say
>> we use a convention like class-side pragmas to define include files, and
>> compiler flags.  The VM provides two crucial pieces of information:
>>
>> 1. the platform name
>> 2. the word size
>>
>> One can't run a Mac OS VM on Linux, and one can't run a 64-bit VM on a
>> 32-bit operating system.  So taking this information from the VM
>> accurately tells the current system what ABI (application binary
>> interface) to use, and that's what's important in generating the right
>> constants.
>>
>> So we use these two pieces of information to index the method pragmas
>> that tell us what specific files to include.
>>
>> Let's imagine we subclass FFISharedPool to add a shared pool for
>> constants for an SQL database.  We might have a class declaration like
>>
>> FFISharedPool subclass: #MYSQLInterface
>> instanceVariableNames: ''
>> classVariableNames: 'MYSQL_DEFAULT_AUTH MYSQL_ENABLE_CLEARTEXT_PLUGIN
>> MYSQL_INIT_COMMAND MYSQL_OPT_BIND MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS
>> MYSQL_OPT_COMPRESS
>> MYSQL_OPT_CONNECT_ATTR_DELETE MYSQL_OPT_CONNECT_ATTR_RESET'
>> poolDictionaries: ''
>> category: 'MYSQLInterface-Pools'
>>
>> The job of FFISharedPool is to compute the right values for the class
>> variables on every platform we want to deploy the MYSQL interface on.
>>
>> So we need to know the relevant include files and C flags for each
>> platform/word-size combination.  A few of them might look like
>>
>>
>> MYSQLInterface class methods for platform information
>> mac32
>>     "I describe the include files and C flags to use when developing a
>> 32-bit MYSQL FFI interface on Mac OS X"
>>     <platformName: 'Mac OS' wordSize: 4>
>>     <cFlags: #('-m32') includeFiles: #('/opt/mysql/include32')>
>>     ^self "all the info is in the pragmas"
>>
>> mac64
>>     "I describe the include files and C flags to use when developing a
>> 64-bit MYSQL FFI interface on Mac OS X"
>>     <platformName: 'Mac OS' wordSize: 8>
>>     <cFlags: #('-m64') includeFiles: #('/opt/mysql/include64')>
>>
>> The above might cause FFISharedPool to autogenerate files called
>> MYSQLInterface.mac32.c & MYSQLInterface.mac64.c.  And these, when run,
>> might output ston notation to MYSQLInterface.mac32.ston &
>> MYSQLInterface.mac64.ston (or maybe to stdout which has to be redirected
>> to MYSQLInterface.mac32.ston; whatever).
>>
>> Now, you might use pragmas, or you might answer a Dictionary instance.
>> What ever style pleases you and seems convenient and readable.  But
>> these methods define the necessary metadata (C flags, include paths, and
>> ...?) for FFISharedPool to autogenerate the C program that, when
>> compiled with the supplied C flags and run on the current platform,
>> outputs the values for the constants the shared pool wants to define.
>>
>>
>> You can get fancy and have FFISharedPool autogenerate the C programs
>> whenever one adds or removes a constant name.  Or you can require the
>> programmer run something, e.g. MYSQLInterface generateInterfaces.  It's
>> really nice if FFISharedPool submits the file to the C compiler
>> automatically, but this can only work for e.g. 32 & 64 bit versions on a
>> single platform.  You have to compile the autogenerated program on the
>> relevant platform, with the necessary libraries and include files installed.
>>
>> You could imagine a set of servers for different platforms so one could
>> submit the autogenerated program for compilation and execution on each
>> platform.  That's a facility I'd make it easy to implement.  I could
>> imagine that a programmer whose company develops an FFI interface and
>> deploys it on a number of platforms would love to be able to automate
>> compiling and running the relevant autogenerated code on a set of
>> servers.  I could imagine the Pharo community providing a set of servers
>> upon which lots of software is installed for precisely this purpose.
>> That means that people could develop FFI interfaces without even having
>> to have the C compiler installed on their platform.
>>
>> You could also add a C parser to FFISharedPool  that parses the
>> post-preprocessed code and extracts function declarations.  But the
>> important thing is autogenerating the C program so that it generates
>> easily parsable output containing the values for the constants.  You can
>> extend the system in interesting ways once you ave this core
>> functionality implemented.
>>
>> So once the program is autogenerated and compiled for the current
>> platform, it is run and its output collected in a file whose name can be
>> recognised by FFISharedPool.
>>
>>
>> Now the class side of FFISharedPool might be declared as
>>
>> FFIShardPool class
>> instanceVariableNames: 'platformName wordSize'
>>
>> and on start-up FFIShardPool could examine its subclasses, and for each
>> whose platformName & wordSize do not match the current platform, search
>> for all the matching FOOInterface.plat.ston files, parse them and update
>> the subclasses' variables, and update that pool's platformName &
>> wordSize.  It could emit a warning on the Transcript or stdout (headful
>> vs headless) indicating which subclasses it couldn't find the relevant
>> FOOInterface.plat.ston files for.
>>
>> But the end result is that
>>
>> a) providing the system is deployed with FOOInterface.plat.ston files
>> for each interface and platform used, a cross-platform application can
>> be deployed *that does not require a C compiler*.
>> b) providing that a system's FOOInterface files have been initialized on
>> the intended platform, a platform-specific application can be deployed
>> for a single platform *without needing the ston files*.
>>
>> Does this make more sense now?
>>
>>        c) at startup the image checks its current platform.  If the
>>        platform is the same that it was saved on, no action is taken.
>>        But if the platform as changed then the relevant ston file is
>>        selected, parsed, and the values for the variables in the shared
>>        pool updated to reflect the values of the current platform.
>>
>>        So the C compiler is only needed when developing the interface,
>>        not when deploying it.
>>
>>    OK
>>
>>
>>            Then Nicolas made a point that if we plan to manage all that
>>            complexity at the image level it may become a hell too.
>>
>>            So.... what if we take a simpler (probably not better)
>>            approach and we consider the "c program that exports
>>            constants and sizes" a VM Plugin? Let's say we have a
>>            UnixPreprocessorPlugin (that would work for OSX, Linux and
>>            other's Unix I imagine for the time being) which provides a
>>            function (that is exported) which answers an array of
>>            arrays. For each constant, we include the name of the
>>            constant, the value, and the sizeof().  Then from image
>>            side, we simply do one FFI call, we get the large array and
>>            we adapt it to a SharedPool or whatever kind of object
>>            representing that info.
>>
>>
>>
>>
>>        This is what I suggestred in teh first place.  That what is
>>        autogenerated is a shared object (be it a plgin or a dll doesn't
>>        matter, it is machine code generated by a C compiler form an
>>        autogenerated C program compiled with the platform's C compiler)
>>        that can be loaded at run-time and interrogated to fetch the
>>        values of a set of variables
>>
>>
>>    OK, got it. But still, it would be easier if the "platform" in this
>>    case is the "machine where we build the VM we will then distribute"
>>    right? i mean, I would like to put this in the CI jobs that
>>    automatically builds the VM, and not myself building for each platform.
>>
>>
>> NO!  For example, why would a company that has some proprietary
>> arithmetic package implemented in its secret labs in C or C++ and
>> accessed through the FFI want to have that code on the Pharo community's
>> build servers?
>>
>>
>>    *I mean, my main doubt is if this job of autogenerating C code,
>>    compile it, run it, export text file, and distribute text file with
>>    the VM, could be done as part of the VM building. *
>>
>>
>> For fuck's sake.  Developing an FFI is not something one does when
>> building a VM.  It is something one does wen using the system.  f you
>> want to do this you *use a plugin*.  The FFI is a different beast.  It
>> is to allow programers to interface to external librarys that are
>> *independent from teh VM*.
>>
>> I'm not going to answer this one again.  OK?
>>
>>
>>        .  But I think that the textual notation suggested above is
>>        simpler.  The test files are easier to distribute and change.
>>        Shared objects and plugins have a habit of going stale, and
>>        there needs to be metadata in there to describe the set of
>>        constants etc, which is tricky to generate and parse because it
>>        is binary (pointer sizes, etc, etc).  Instead a simple textual
>>        format should be much more robust.  One could even edit by hand
>>        to add new constants.  It would be easy to make the textual file
>>        a versioned file.  Etc, etc.
>>
>>
>>    OK. Got it. And do you think using X Macros for the autogenerated C
>>    (from the SharedPool) is a good idea?
>>    And then I simply write a text file out of it.
>>
>>
>>            I know that different users will need different constants.
>>            But let's say the infrastructure (plugin etc) is already
>>            done. And let's say I am a user that I want to build
>>            something with FFI and I need some constants that I see are
>>            not defined. Then I can simply add the ones I need in the
>>            plugin, and next VM release will have those. If Cog gets
>>            moved to Github, then this is even easier. Everybody can do
>>            a PR with the constants he needs. And in fact, if we have
>>            the infrastructure in place, I think that we each of us
>>            spend half an hour, we may have almost everything we need.
>>
>>            For example, I can add myself all those for signals (to use
>>            kill() from FFI), all those from fcntl (to make none
>>            blocking pipes), all those from wait()/waitpid() family (so
>>            that I can do a waitpid() with WNOHANG), etc etc etc.
>>
>>            I know it's not the best approach but it's something that
>>            could be done very easily and would allow A LOT of stuff to
>>            be moved to FFI just because we have no access to preprocess
>>            constants or sizeof()  (to know how to allocate). I also
>>            know this won't cover macros and other stuff. But still.
>>
>>            If you think this is a good idea, I can spend the time to do
>>            it.
>>
>>            Cheers,
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>            On Thu, May 10, 2012 at 10:09 AM, Nick Ager
>>            <[hidden email] <mailto:[hidden email]>> wrote:
>>
>>                <snip>
>>                Well, like opendbx, maybe because opengl has quite
>>                standard interface...
>>                </snip>
>>
>>                and
>>
>>                <snip>
>>                It's not that it's not doable, it's that we gonna
>>                reinvent gaz plant
>>                and it gonna be so boring...
>>                I'd like to see a proof of concept, even if we restrict
>>                to libc, libm,
>>                kernel.dll, msvcrt.dll ...
>>                </snip>
>>
>>                <snip>
>>                Is the unix style select()
>>                ubiquitous or should I use WaitForMultipleObject() on
>>                Windows? Are
>>                specification of read/write streams implementation
>>                machine independant
>>                (bsd/sysv/others...)
>>                </snip>
>>
>>                Perhaps *a* way forward is to try to find existing
>>                projects which have already created cross-platform
>>                abstractions for platform specific functionality. Then
>>                we can use FFI to access that interface in a similar way
>>                to OpenGL and OpenDBX. For example NodeJs works across
>>                unixes - perhaps they have a useful cross-platform
>>                abstraction, boost  has abstractions of IPC etc
>>
>>                Nick
>>
>>
>>
>>
>>            --
>>            Mariano
>>            http://marianopeck.wordpress.com
>>
>>
>>
>>
>>        --
>>        _,,,^..^,,,_
>>        best, Eliot
>>
>>
>>
>>
>>    --
>>    Mariano
>>    http://marianopeck.wordpress.com
>>
>>
>>
>>
>> --
>> _,,,^..^,,,_
>> best, Eliot
>
>

Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Mariano Martinez Peck
In reply to this post by Eliot Miranda-2
Hi Eliot,

Thanks, much clearer now. Sometimes I am slow :)    I was confused because I was only thinking in libc kind of lib (very kernel and very likely used by the VM). But when you gave the SQL example, then I did get the general nature you were trying to explain. So it's clear now. 

I would like to add 2 more comments:

1) Do you agree that besides the name / value it would also help having the result of  sizeof ? Otherwise, I may still find problems when I need to allocate from FFI and it's not clear size of a struct (as it was my case same days ago).  So in this case, it would be kind of an array rather than a key / value pairs.


Thanks,


On Sun, Jan 17, 2016 at 12:40 AM, Eliot Miranda <[hidden email]> wrote:
Hi Mariano,

On Sat, Jan 16, 2016 at 6:25 PM, Mariano Martinez Peck <[hidden email]> wrote:


On Sat, Jan 16, 2016 at 11:02 PM, Eliot Miranda <[hidden email]> wrote:


On Sat, Jan 16, 2016 at 6:00 AM, Mariano Martinez Peck <[hidden email]> wrote:
 
Hi all,

Sorry for reviving an old thread but I thought it was better to continue the discussion here because of the context. 
As you may have read, the other day I released a first approeach to a subset of OSProcess based on FFI (posix_spwan() family of functions):


 And with that in mind, I wanted to share a few things with you. The main 2 problems I found with implementing this with FFI was:

1) We have all already agree and discussed that fork+exec cannot be done in separate FFI calls. So at the very min you need either a plugin method that does the fork()+exec() OR wrapping a lib like posix_spwan()

2) The other main problem, is, as you all said (and mostly  Nicolas), is the problems with the preprocessor (constants, macros, etc).

With all that said, I was able to get my stuff working. However, I am still using some primitives of OSProcess plugin because of 2). 

I read Eliot idea and what I don't like is the need of a C compiler in the user machine. I think that's a high constrain. Then Igor suggested that WE (developers and maintainers of a certain tool) are the ones that compiles the little C program to extract constant values etc and then WE provide as part of our source code, some packages with some SharedPool depending on the platform/OS. And Igor approach looked a bit better to me.



You misunderstand the proposal. 

I think I did. But let me confirm that below ;)
 
The C compiler is needed /only when changing the set of constants/, i.e. when /developing/ the interface.  The C compiler is /not/ needed when deploying.

The idea is to
a) at development time, e.g. when a new variable is added to a SharedPool containing platform constants, a C program is autogenerated that outputs in some format a description of the names and values of all the constants defined in the pool.  One convenient notation is e.g. STON.  For the purposes of this discussion let's assume we're using ston, but any format the image an parse (or indeed a shared object the image can load on teh current pkatform) will do.  The output of the autogenerated C program would be called something like <SharedPoolName>.<PlatformName>.ston, e.g. UnixConstants.MacOSX64.ston or UnixConstants.Linux32.ston.  The ston files can easily be parsed by facilities in the Smalltalk image.

b) when deploying the system to a set of platforms one includes all the relevant platform-specific ston files.


OK. But let me ask something. Below you said "be it a plugin or a dll doesn't matter". To autogenerate the C program, I must know which header files to include for each platform and probably a few others things. For example, besides exporting the value,  I would also like to export the sizeof(). At that depends how was the VM compiled, right?   So...my question is...if such a autogenerated C code could be part of the VM building (considering all the settings being assume when building), cannot I reuse the knowledge the VM already has? Like which header files to include, if it was compiled 32 bits or 64 bits, which C compiler to use, etc..

I actually said that using text is easier than a dll.  So I'm saying  autogenerate a C program that outputs name-value pairs in some convenient textual representation, e.g. ston.  But answering your question...

The knowledge in the VM as to what header files are included *applies only to the include files the VM uses*.  The VM uses a subset of the platform.  It doesn't for example include any headers that define a database interface.  It doesn't include header files that define the interface to a UI tooklit such at GTK.  Etc, etc.  So in fact the VM *doesn't* include the knowledge one needs to determine the set of include files for an arbitrary FFI interface.  And even so, the include files that it does use are in the VM's platform source files, and that information is not readily accessible.

Let me summarise.  No, the VM cannot be used to determine the set of include files needed to generate constants used in an arbitrary FFI interface.

What I mean is if it would be easier if I take the SharedPool at VM building time, and from there I autogenerate (and run) the C code that would generate the output. Then, when we "deploy" the VM, we can deploy it with relevant platform specific ston files as you said. 

No.  The VM is something that provides an FFI.  It doesn't *define* an FFI.   One must be able to develop an FFI interface without needing to rebuild the VM.  So computing the values of constants should be *separate* from building a VM.  Now let me give you more of an example.

Let's say we define a subclass of SharedPool called FFISharedPool.   FFISharedPool 's job is to manage autogenerating a C file, compiling it for the platform, and organizing parsing the relevant output.  Let's say we use a convention like class-side pragmas to define include files, and compiler flags.  The VM provides two crucial pieces of information:

1. the platform name
2. the word size

One can't run a Mac OS VM on Linux, and one can't run a 64-bit VM on a 32-bit operating system.  So taking this information from the VM accurately tells the current system what ABI (application binary interface) to use, and that's what's important in generating the right constants. 

So we use these two pieces of information to index the method pragmas that tell us what specific files to include.

Let's imagine we subclass FFISharedPool to add a shared pool for constants for an SQL database.  We might have a class declaration like

FFISharedPool subclass: #MYSQLInterface
instanceVariableNames: ''
classVariableNames: 'MYSQL_DEFAULT_AUTH MYSQL_ENABLE_CLEARTEXT_PLUGIN MYSQL_INIT_COMMAND MYSQL_OPT_BIND MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS MYSQL_OPT_COMPRESS MYSQL_OPT_CONNECT_ATTR_DELETE MYSQL_OPT_CONNECT_ATTR_RESET'
poolDictionaries: ''
category: 'MYSQLInterface-Pools'

The job of FFISharedPool is to compute the right values for the class variables on every platform we want to deploy the MYSQL interface on.

So we need to know the relevant include files and C flags for each platform/word-size combination.  A few of them might look like


MYSQLInterface class methods for platform information
mac32
    "I describe the include files and C flags to use when developing a 32-bit MYSQL FFI interface on Mac OS X"
    <platformName: 'Mac OS' wordSize: 4>
    <cFlags: #('-m32') includeFiles: #('/opt/mysql/include32')>
    ^self "all the info is in the pragmas"

mac64
    "I describe the include files and C flags to use when developing a 64-bit MYSQL FFI interface on Mac OS X"
    <platformName: 'Mac OS' wordSize: 8>
    <cFlags: #('-m64') includeFiles: #('/opt/mysql/include64')>

The above might cause FFISharedPool to autogenerate files called MYSQLInterface.mac32.c & MYSQLInterface.mac64.c.  And these, when run, might output ston notation to MYSQLInterface.mac32.ston & MYSQLInterface.mac64.ston (or maybe to stdout which has to be redirected to MYSQLInterface.mac32.ston; whatever).

Now, you might use pragmas, or you might answer a Dictionary instance.  What ever style pleases you and seems convenient and readable.  But these methods define the necessary metadata (C flags, include paths, and ...?) for FFISharedPool to autogenerate the C program that, when compiled with the supplied C flags and run on the current platform, outputs the values for the constants the shared pool wants to define.


You can get fancy and have FFISharedPool autogenerate the C programs whenever one adds or removes a constant name.  Or you can require the programmer run something, e.g. MYSQLInterface generateInterfaces.  It's really nice if FFISharedPool submits the file to the C compiler automatically, but this can only work for e.g. 32 & 64 bit versions on a single platform.  You have to compile the autogenerated program on the relevant platform, with the necessary libraries and include files installed.

You could imagine a set of servers for different platforms so one could submit the autogenerated program for compilation and execution on each platform.  That's a facility I'd make it easy to implement.  I could imagine that a programmer whose company develops an FFI interface and deploys it on a number of platforms would love to be able to automate compiling and running the relevant autogenerated code on a set of servers.  I could imagine the Pharo community providing a set of servers upon which lots of software is installed for precisely this purpose. That means that people could develop FFI interfaces without even having to have the C compiler installed on their platform.

You could also add a C parser to FFISharedPool  that parses the post-preprocessed code and extracts function declarations.  But the important thing is autogenerating the C program so that it generates easily parsable output containing the values for the constants.  You can extend the system in interesting ways once you ave this core functionality implemented.

So once the program is autogenerated and compiled for the current platform, it is run and its output collected in a file whose name can be recognised by FFISharedPool.


Now the class side of FFISharedPool might be declared as

FFIShardPool class
instanceVariableNames: 'platformName wordSize'

and on start-up FFIShardPool could examine its subclasses, and for each whose platformName & wordSize do not match the current platform, search for all the matching FOOInterface.plat.ston files, parse them and update the subclasses' variables, and update that pool's platformName & wordSize.  It could emit a warning on the Transcript or stdout (headful vs headless) indicating which subclasses it couldn't find the relevant FOOInterface.plat.ston files for.

But the end result is that

a) providing the system is deployed with FOOInterface.plat.ston files for each interface and platform used, a cross-platform application can be deployed *that does not require a C compiler*.
b) providing that a system's FOOInterface files have been initialized on the intended platform, a platform-specific application can be deployed for a single platform *without needing the ston files*.

Does this make more sense now?

c) at startup the image checks its current platform.  If the platform is the same that it was saved on, no action is taken.  But if the platform as changed then the relevant ston file is selected, parsed, and the values for the variables in the shared pool updated to reflect the values of the current platform.

So the C compiler is only needed when developing the interface, not when deploying it.

 
OK 
 

Then Nicolas made a point that if we plan to manage all that complexity at the image level it may become a hell too. 

So.... what if we take a simpler (probably not better) approach and we consider the "c program that exports constants and sizes" a VM Plugin? Let's say we have a UnixPreprocessorPlugin (that would work for OSX, Linux and other's Unix I imagine for the time being) which provides a function (that is exported) which answers an array of arrays. For each constant, we include the name of the constant, the value, and the sizeof().  Then from image side, we simply do one FFI call, we get the large array and we adapt it to a SharedPool or whatever kind of object representing that info. 



This is what I suggestred in teh first place.  That what is autogenerated is a shared object (be it a plgin or a dll doesn't matter, it is machine code generated by a C compiler form an autogenerated C program compiled with the platform's C compiler) that can be loaded at run-time and interrogated to fetch the values of a set of variables

OK, got it. But still, it would be easier if the "platform" in this case is the "machine where we build the VM we will then distribute" right? i mean, I would like to put this in the CI jobs that automatically builds the VM, and not myself building for each platform. 

NO!  For example, why would a company that has some proprietary arithmetic package implemented in its secret labs in C or C++ and accessed through the FFI want to have that code on the Pharo community's build servers?
 

I mean, my main doubt is if this job of autogenerating C code, compile it, run it, export text file, and distribute text file with the VM, could be done as part of the VM building. 

For fuck's sake.  Developing an FFI is not something one does when building a VM.  It is something one does wen using the system.  f you want to do this you *use a plugin*.  The FFI is a different beast.  It is to allow programers to interface to external librarys that are *independent from teh VM*.

I'm not going to answer this one again.  OK?

 

 
.  But I think that the textual notation suggested above is simpler.  The test files are easier to distribute and change.  Shared objects and plugins have a habit of going stale, and there needs to be metadata in there to describe the set of constants etc, which is tricky to generate and parse because it is binary (pointer sizes, etc, etc).  Instead a simple textual format should be much more robust.  One could even edit by hand to add new constants.  It would be easy to make the textual file a versioned file.  Etc, etc.
 

OK. Got it. And do you think using X Macros for the autogenerated C (from the SharedPool) is a good idea?
And then I simply write a text file out of it. 

 

I know that different users will need different constants. But let's say the infrastructure (plugin etc) is already done. And let's say I am a user that I want to build something with FFI and I need some constants that I see are not defined. Then I can simply add the ones I need in the plugin, and next VM release will have those. If Cog gets moved to Github, then this is even easier. Everybody can do a PR with the constants he needs. And in fact, if we have the infrastructure in place, I think that we each of us spend half an hour, we may have almost everything we need. 

For example, I can add myself all those for signals (to use kill() from FFI), all those from fcntl (to make none blocking pipes), all those from wait()/waitpid() family (so that I can do a waitpid() with WNOHANG), etc etc etc.

I know it's not the best approach but it's something that could be done very easily and would allow A LOT of stuff to be moved to FFI just because we have no access to preprocess constants or sizeof()  (to know how to allocate). I also know this won't cover macros and other stuff. But still.

If you think this is a good idea, I can spend the time to do it. 

Cheers, 









On Thu, May 10, 2012 at 10:09 AM, Nick Ager <[hidden email]> wrote:
<snip>
Well, like opendbx, maybe because opengl has quite standard interface...
</snip>

and

<snip>
It's not that it's not doable, it's that we gonna reinvent gaz plant
and it gonna be so boring...
I'd like to see a proof of concept, even if we restrict to libc, libm,
kernel.dll, msvcrt.dll ...
</snip>

<snip>
Is the unix style select()
ubiquitous or should I use WaitForMultipleObject() on Windows? Are
specification of read/write streams implementation machine independant
(bsd/sysv/others...)
</snip>

Perhaps *a* way forward is to try to find existing projects which have already created cross-platform abstractions for platform specific functionality. Then we can use FFI to access that interface in a similar way to OpenGL and OpenDBX. For example NodeJs works across unixes - perhaps they have a useful cross-platform abstraction, boost  has abstractions of IPC etc 

Nick



--




--
_,,,^..^,,,_
best, Eliot



--



--
_,,,^..^,,,_
best, Eliot



--
Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Eliot Miranda-2
In reply to this post by Eliot Miranda-2
Hi Levente,

> On Jan 17, 2016, at 7:30 AM, Levente Uzonyi <[hidden email]> wrote:
>
>> On Sat, 16 Jan 2016, Eliot Miranda wrote:
>>
>> On Sat, Jan 16, 2016 at 4:37 PM, Levente Uzonyi <[hidden email]> wrote:
>>
>>      On Sat, 16 Jan 2016, Mariano Martinez Peck wrote:
>>
>>      (Still no quote.)
>>
>>      How will you read the output of the process without having your image's process blocked in the FFI callout?
>>      How will you make sure that writes to input of the process won't block the FFI callout?
>> This presupposes the threaded FFI.  The threaded FFI allows the VM to make any number of blocking calls, adding a new thread to run the VM whenever the VM is stalled when the heartbeat beats.  hence one can freely read and write to/from i/o blocking i/o streams
>> (including pipes and sockets) or blocking database connexions, etc, all without stating that the FFI call must be done in a special way, since all calls through the FFI can block without blocking the VM.
>
> I think it was you who said (in a discussion with Craig) that the threaded FFI was not production ready. Is it ready for produciton now?

No, but I expect this is the year it will be.  Spur provides pinning, so the VM infrastructure is there.  The Pharo community plus some commercial relationships that have developed are providing funding.  Esteban Lorenzano and I want to collaborate on this and I hope to get help from some other people, such as Ronie Salgado.  And Mariano is working on an important part of the problem.  So I feel there's sufficient momentum for us to realize the threaded FFI this year.

> Levente


_,,,^..^,,,_ (phone)

>> Note that the scheme is also amenable to plugins, but the plugins must be rewritten to include the release vm/acquire vm calls around a blocking call.  With the threaded VM the FFI includes these calls around every FFI call.
>> HTH
>> _,,,^..^,,,_
>> best, Eliot

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

kilon.alios
wow impressive work guys , thats awesome news.

On Sun, Jan 17, 2016 at 6:53 PM Eliot Miranda <[hidden email]> wrote:

Hi Levente,

> On Jan 17, 2016, at 7:30 AM, Levente Uzonyi <[hidden email]> wrote:
>
>> On Sat, 16 Jan 2016, Eliot Miranda wrote:
>>
>> On Sat, Jan 16, 2016 at 4:37 PM, Levente Uzonyi <[hidden email]> wrote:
>>
>>      On Sat, 16 Jan 2016, Mariano Martinez Peck wrote:
>>
>>      (Still no quote.)
>>
>>      How will you read the output of the process without having your image's process blocked in the FFI callout?
>>      How will you make sure that writes to input of the process won't block the FFI callout?
>> This presupposes the threaded FFI.  The threaded FFI allows the VM to make any number of blocking calls, adding a new thread to run the VM whenever the VM is stalled when the heartbeat beats.  hence one can freely read and write to/from i/o blocking i/o streams
>> (including pipes and sockets) or blocking database connexions, etc, all without stating that the FFI call must be done in a special way, since all calls through the FFI can block without blocking the VM.
>
> I think it was you who said (in a discussion with Craig) that the threaded FFI was not production ready. Is it ready for produciton now?

No, but I expect this is the year it will be.  Spur provides pinning, so the VM infrastructure is there.  The Pharo community plus some commercial relationships that have developed are providing funding.  Esteban Lorenzano and I want to collaborate on this and I hope to get help from some other people, such as Ronie Salgado.  And Mariano is working on an important part of the problem.  So I feel there's sufficient momentum for us to realize the threaded FFI this year.

> Levente


_,,,^..^,,,_ (phone)

>> Note that the scheme is also amenable to plugins, but the plugins must be rewritten to include the release vm/acquire vm calls around a blocking call.  With the threaded VM the FFI includes these calls around every FFI call.
>> HTH
>> _,,,^..^,,,_
>> best, Eliot
Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Eliot Miranda-2
In reply to this post by Eliot Miranda-2


> On Jan 17, 2016, at 8:53 AM, Eliot Miranda <[hidden email]> wrote:
>
> Hi Levente,
>
>>> On Jan 17, 2016, at 7:30 AM, Levente Uzonyi <[hidden email]> wrote:
>>>
>>> On Sat, 16 Jan 2016, Eliot Miranda wrote:
>>>
>>> On Sat, Jan 16, 2016 at 4:37 PM, Levente Uzonyi <[hidden email]> wrote:
>>>
>>>     On Sat, 16 Jan 2016, Mariano Martinez Peck wrote:
>>>
>>>     (Still no quote.)
>>>
>>>     How will you read the output of the process without having your image's process blocked in the FFI callout?
>>>     How will you make sure that writes to input of the process won't block the FFI callout?
>>> This presupposes the threaded FFI.  The threaded FFI allows the VM to make any number of blocking calls, adding a new thread to run the VM whenever the VM is stalled when the heartbeat beats.  hence one can freely read and write to/from i/o blocking i/o streams
>>> (including pipes and sockets) or blocking database connexions, etc, all without stating that the FFI call must be done in a special way, since all calls through the FFI can block without blocking the VM.
>>
>> I think it was you who said (in a discussion with Craig) that the threaded FFI was not production ready. Is it ready for produciton now?
>
> No, but I expect this is the year it will be.  Spur provides pinning, so the VM infrastructure is there.  The Pharo community plus some commercial relationships that have developed are providing funding.  Esteban Lorenzano and I want to collaborate on this and I hope to get help from some other people, such as Ronie Salgado.  And Mariano is working on an important part of the problem.  So I feel there's sufficient momentum for us to realize the threaded FFI this year.


and when Craig Latta tried to use it late last year it worked up to a point.  The thing that didn't work was callbacks from foreign threads.  So it looks like the core threading code is not too far away from working.

Another really important part, bigger than threading, is marshaling.  Being able to handle the full x85_64 abi requires a better approach than interpreting tops signatures.  Igor's NativeBoost gave an example of how to generate marshaling machine code, but alas only for x86.  But Sista includes an extensible bytecode set for arbitrary instructions.  Sista is close to production, and we know the bytecode set works.  So the plan is to use these bytecodes to do the marshaling.  That neatly solves the problems of a) associating marshaling machine code with a method and b) marshaling in an interpreted stack VM, since the bytecode set works in any Cog VM.  So the plan is to write an ABI compiler from C signatures to marshaling code to replace the interpreted FFI plugin.

So this year I hope we will have an excellent high performance FFI.

>> Levente
>
> _,,,^..^,,,_ (phone)
>
>>> Note that the scheme is also amenable to plugins, but the plugins must be rewritten to include the release vm/acquire vm calls around a blocking call.  With the threaded VM the FFI includes these calls around every FFI call.
>>> HTH
>>> _,,,^..^,,,_
>>> best, Eliot

Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Thierry Goubier
In reply to this post by Eliot Miranda-2
Le 17/01/2016 15:47, Eliot Miranda a écrit :

> Hi Thierry,
>
>> On Jan 17, 2016, at 6:10 AM, Thierry Goubier
>> <[hidden email]> wrote:
>>
>> Hi Eliot,
>>
>> I still don't understand why this platform-specific data (external
>> datatypes sizes, constants values)  has to be written as a json
>> file and not as Smalltalk code ?
>
> It can be any notation that is easy to parse.  One could use pure
> Smalltalk, json, ston, xml.  I thought ston /was/ a form of Smalltalk
> aimed at representing key/value pairs, in which case it seems ideal.
>
> What are the objectives for this notation? My take (feel free to
> suggest a better set) is:
>
> a) be quick to parse since these files are parsed on start-up when
> the platform is different from that the image was snapshot red on
>
> b) be human readable. The flexibility of being able to version, edit,
> search and diff these files seems useful
>
> c) be reasonably straight-forward to generate from C.  But hey, it's
> only code :-)

Agreed to all points, of course :) I do think b and a; c is ok.

>> If I write an interface to an external lib, I'd like to benefit
>> from nice access to Smalltalk code to, say, test versions of the
>> external libraries, set external types based on system information,
>> etc, etc... instead of having to deal with all those various json
>> files outside my normal workflow.
>>
>> Okay, I can write smalltalk code which runs those C tests programs,
>> extract their output, model them in smalltalk and generate the
>> jsons from there... but, then, I really don't understand why I have
>> to write those jsons then every user loads those jsons instead of
>> just running that setup code I have in my external library
>> smalltalk support package ?
>
> You /don't/ have to write them ever.  They are written for you by the
> FFI shared pool.

Yes, that was understood.

> So you're suggesting the C program output a Smalltalk method that
> looks like:e.g.
>
> initializeMYSQLMac32 MYSQL_ENABLE_CLEARTEXT_PLUGIN := 123.
> MYSQL_INIT_COMMAND := 234. MYSQL_OPT_BIND := 345
>
> So the trade off you're discussing is whether it is valuable to keep
> these in the image or not.  For me these initialize toon methods are
> overhead.  They get parsed as infrequently as possible. So personally
> I prefer to use several external files that are parsed infrequently
> instead of the overhead of several initialization methods that are
> run infrequently.
>
> For example, if I were deploying on some embedded arm device, the
> initialization method would be something I'd have to strip from the
> image.  Seems easier to just parse an external file to me.

Well, for me, on a pure technical basis, no.

- It adds one failure point: reading one more file at startup.

- It adds management issues: slight version differences in an external
libs may force me to add context-dependent json file loading.

> I like the use of Smalltalk for eg user interface opening code where
> - we want a UI construction tool to edit an e excitable
> representation of the interface - there is only one version of the UI
> method
>
> But with these interface descriptions we could easily end up with a
> 32-bit and a 64-bit version for every UNIX variant plus Mac OS and
> Windows.  You could easily have ten long methods with overheads in
> the tens of kilobytes.  Seems a waste to me.

No really. You have the same complexity, either in a json or in an init
file; and the same level of customization or even more flexibility (you
can decide to set those initialisation methods in different packages,
and have the flexibility to slice / share the way you like... 64bits /
32bits, 64bits ARM + MIPS are the same except one constant, etc...).

It's a bit like dealing with those configure scripts: in C, if you
really want multiplatform support, you write #ifdef macros and shell
scripts to query the build platform for you... I just prefer to do it in
Smalltalk if I have to.

But I guess that I could still do it by hacking the FFI layer anyway :)
And I'd be happy to see a system modeling layer in Smalltalk (one which
knows how to query hardware/software capabilities at startup) since it
opens up yet unforeseen possibilities (switching on an OpenCL
acceleration layer on supported platforms with the right driver version?).

Thierry

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Eliot Miranda-2
In reply to this post by Eliot Miranda-2


On Sun, Jan 17, 2016 at 12:13 PM, stephane ducasse <[hidden email]> wrote:
 

On 17 Jan 2016, at 18:04, Eliot Miranda <[hidden email]> wrote:

So this year I hope we will have an excellent high performance FFI.

this will be an exciting year. 
BTW when ronie will arrive here we will plan some skype session with you. 

Yes, if we're to pull this off we have all to be working with a coherent shared plan and to keep in communication.  This is exciting.  I've been dreaming of this FFI for a while now :-).  

_,,,^..^,,,_
best, Eliot
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] [Vm-dev] Re: Can OSProcess functionality be implemented using FFI instead of plugin?

Mariano Martinez Peck
In reply to this post by Eliot Miranda-2

Let's say we define a subclass of SharedPool called FFISharedPool.   FFISharedPool 's job is to manage autogenerating a C file, compiling it for the platform, and organizing parsing the relevant output.  Let's say we use a convention like class-side pragmas to define include files, and compiler flags.  The VM provides two crucial pieces of information:

1. the platform name
2. the word size

One can't run a Mac OS VM on Linux, and one can't run a 64-bit VM on a 32-bit operating system.  So taking this information from the VM accurately tells the current system what ABI (application binary interface) to use, and that's what's important in generating the right constants. 

So we use these two pieces of information to index the method pragmas that tell us what specific files to include.

Let's imagine we subclass FFISharedPool to add a shared pool for constants for an SQL database.  We might have a class declaration like

FFISharedPool subclass: #MYSQLInterface
instanceVariableNames: ''
classVariableNames: 'MYSQL_DEFAULT_AUTH MYSQL_ENABLE_CLEARTEXT_PLUGIN MYSQL_INIT_COMMAND MYSQL_OPT_BIND MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS MYSQL_OPT_COMPRESS MYSQL_OPT_CONNECT_ATTR_DELETE MYSQL_OPT_CONNECT_ATTR_RESET'
poolDictionaries: ''
category: 'MYSQLInterface-Pools'

The job of FFISharedPool is to compute the right values for the class variables on every platform we want to deploy the MYSQL interface on.

So we need to know the relevant include files and C flags for each platform/word-size combination.  A few of them might look like


MYSQLInterface class methods for platform information
mac32
    "I describe the include files and C flags to use when developing a 32-bit MYSQL FFI interface on Mac OS X"
    <platformName: 'Mac OS' wordSize: 4>
    <cFlags: #('-m32') includeFiles: #('/opt/mysql/include32')>
    ^self "all the info is in the pragmas"

mac64
    "I describe the include files and C flags to use when developing a 64-bit MYSQL FFI interface on Mac OS X"
    <platformName: 'Mac OS' wordSize: 8>
    <cFlags: #('-m64') includeFiles: #('/opt/mysql/include64')>

The above might cause FFISharedPool to autogenerate files called MYSQLInterface.mac32.c & MYSQLInterface.mac64.c.  And these, when run, might output ston notation to MYSQLInterface.mac32.ston & MYSQLInterface.mac64.ston (or maybe to stdout which has to be redirected to MYSQLInterface.mac32.ston; whatever).

Now, you might use pragmas, or you might answer a Dictionary instance.  What ever style pleases you and seems convenient and readable.  But these methods define the necessary metadata (C flags, include paths, and ...?) for FFISharedPool to autogenerate the C program that, when compiled with the supplied C flags and run on the current platform, outputs the values for the constants the shared pool wants to define.


You can get fancy and have FFISharedPool autogenerate the C programs whenever one adds or removes a constant name.  Or you can require the programmer run something, e.g. MYSQLInterface generateInterfaces.  It's really nice if FFISharedPool submits the file to the C compiler automatically, but this can only work for e.g. 32 & 64 bit versions on a single platform.  You have to compile the autogenerated program on the relevant platform, with the necessary libraries and include files installed.

You could imagine a set of servers for different platforms so one could submit the autogenerated program for compilation and execution on each platform.  That's a facility I'd make it easy to implement.  I could imagine that a programmer whose company develops an FFI interface and deploys it on a number of platforms would love to be able to automate compiling and running the relevant autogenerated code on a set of servers.  I could imagine the Pharo community providing a set of servers upon which lots of software is installed for precisely this purpose. That means that people could develop FFI interfaces without even having to have the C compiler installed on their platform.

You could also add a C parser to FFISharedPool  that parses the post-preprocessed code and extracts function declarations.  But the important thing is autogenerating the C program so that it generates easily parsable output containing the values for the constants.  You can extend the system in interesting ways once you ave this core functionality implemented.

So once the program is autogenerated and compiled for the current platform, it is run and its output collected in a file whose name can be recognised by FFISharedPool.


Hi Eliot,

OK, I have currently a very first prototype where I can autogenerate the C file from a FFISharedPool subclass, compile it, run it and get the ston file output. Please, read below.
 

Now the class side of FFISharedPool might be declared as

FFIShardPool class
instanceVariableNames: 'platformName wordSize'

and on start-up FFIShardPool could examine its subclasses, and for each whose platformName & wordSize do not match the current platform, search for all the matching FOOInterface.plat.ston files, parse them and update the subclasses' variables, and update that pool's platformName & wordSize.  It could emit a warning on the Transcript or stdout (headful vs headless) indicating which subclasses it couldn't find the relevant FOOInterface.plat.ston files for.

But the end result is that

a) providing the system is deployed with FOOInterface.plat.ston files for each interface and platform used, a cross-platform application can be deployed *that does not require a C compiler*.
b) providing that a system's FOOInterface files have been initialized on the intended platform, a platform-specific application can be deployed for a single platform *without needing the ston files*.


I was thinking the following. Having to distribute the FFI wrapper (take as an example the myself wrapper) with the .ston files is a bit of a pain with MC.  So I was thinking...what if FFISharedPool has all the machinery to allow FFI lib wrapper developer (the developer of the MySQL wrapper), to autogenerate the ston file as we said, BUT, the ston file is stored as methods in the MYSQLInterface subclass? Probably under a "autogenerated" protocol. That way, it's very easy to distribute and in addition, at system startup it's easier to "search" for the "ston files".

The only drawback is the for very large ston files MC will suffer a bit.. but..

Thoughts?


 
Does this make more sense now?

c) at startup the image checks its current platform.  If the platform is the same that it was saved on, no action is taken.  But if the platform as changed then the relevant ston file is selected, parsed, and the values for the variables in the shared pool updated to reflect the values of the current platform.

So the C compiler is only needed when developing the interface, not when deploying it.

 
OK 
 

Then Nicolas made a point that if we plan to manage all that complexity at the image level it may become a hell too. 

So.... what if we take a simpler (probably not better) approach and we consider the "c program that exports constants and sizes" a VM Plugin? Let's say we have a UnixPreprocessorPlugin (that would work for OSX, Linux and other's Unix I imagine for the time being) which provides a function (that is exported) which answers an array of arrays. For each constant, we include the name of the constant, the value, and the sizeof().  Then from image side, we simply do one FFI call, we get the large array and we adapt it to a SharedPool or whatever kind of object representing that info. 



This is what I suggestred in teh first place.  That what is autogenerated is a shared object (be it a plgin or a dll doesn't matter, it is machine code generated by a C compiler form an autogenerated C program compiled with the platform's C compiler) that can be loaded at run-time and interrogated to fetch the values of a set of variables

OK, got it. But still, it would be easier if the "platform" in this case is the "machine where we build the VM we will then distribute" right? i mean, I would like to put this in the CI jobs that automatically builds the VM, and not myself building for each platform. 

NO!  For example, why would a company that has some proprietary arithmetic package implemented in its secret labs in C or C++ and accessed through the FFI want to have that code on the Pharo community's build servers?
 

I mean, my main doubt is if this job of autogenerating C code, compile it, run it, export text file, and distribute text file with the VM, could be done as part of the VM building. 

For fuck's sake.  Developing an FFI is not something one does when building a VM.  It is something one does wen using the system.  f you want to do this you *use a plugin*.  The FFI is a different beast.  It is to allow programers to interface to external librarys that are *independent from teh VM*.

I'm not going to answer this one again.  OK?

 

 
.  But I think that the textual notation suggested above is simpler.  The test files are easier to distribute and change.  Shared objects and plugins have a habit of going stale, and there needs to be metadata in there to describe the set of constants etc, which is tricky to generate and parse because it is binary (pointer sizes, etc, etc).  Instead a simple textual format should be much more robust.  One could even edit by hand to add new constants.  It would be easy to make the textual file a versioned file.  Etc, etc.
 

OK. Got it. And do you think using X Macros for the autogenerated C (from the SharedPool) is a good idea?
And then I simply write a text file out of it. 

 

I know that different users will need different constants. But let's say the infrastructure (plugin etc) is already done. And let's say I am a user that I want to build something with FFI and I need some constants that I see are not defined. Then I can simply add the ones I need in the plugin, and next VM release will have those. If Cog gets moved to Github, then this is even easier. Everybody can do a PR with the constants he needs. And in fact, if we have the infrastructure in place, I think that we each of us spend half an hour, we may have almost everything we need. 

For example, I can add myself all those for signals (to use kill() from FFI), all those from fcntl (to make none blocking pipes), all those from wait()/waitpid() family (so that I can do a waitpid() with WNOHANG), etc etc etc.

I know it's not the best approach but it's something that could be done very easily and would allow A LOT of stuff to be moved to FFI just because we have no access to preprocess constants or sizeof()  (to know how to allocate). I also know this won't cover macros and other stuff. But still.

If you think this is a good idea, I can spend the time to do it. 

Cheers, 









On Thu, May 10, 2012 at 10:09 AM, Nick Ager <[hidden email]> wrote:
<snip>
Well, like opendbx, maybe because opengl has quite standard interface...
</snip>

and

<snip>
It's not that it's not doable, it's that we gonna reinvent gaz plant
and it gonna be so boring...
I'd like to see a proof of concept, even if we restrict to libc, libm,
kernel.dll, msvcrt.dll ...
</snip>

<snip>
Is the unix style select()
ubiquitous or should I use WaitForMultipleObject() on Windows? Are
specification of read/write streams implementation machine independant
(bsd/sysv/others...)
</snip>

Perhaps *a* way forward is to try to find existing projects which have already created cross-platform abstractions for platform specific functionality. Then we can use FFI to access that interface in a similar way to OpenGL and OpenDBX. For example NodeJs works across unixes - perhaps they have a useful cross-platform abstraction, boost  has abstractions of IPC etc 

Nick



--




--
_,,,^..^,,,_
best, Eliot



--



--
_,,,^..^,,,_
best, Eliot



--
12