Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Embedded Perl - Saving State?

by McD (Chaplain)
on Apr 30, 2009 at 14:40 UTC ( [id://761106]=perlquestion: print w/replies, xml ) Need Help??

McD has asked for the wisdom of the Perl Monks concerning the following question:

Wandering amongst the dragons here, seeking insight from the wizards.

Suppose I'm embedding Perl in my C app, as is so well documented in perlembed. I've constructed a PerlInterpreter my_perl, parsed my Perl code, and I'm occasionally calling call_argv("MyPerlFunction", ...);.

My question is this: is it possible, in my C app, to save off the state of the interpreter and restore it later?

I'm thinking of something like serializing my_perl, perhaps to disk, and restoring it later - thus saving not only the parsing/compiling, but more importantly, saving the state of things like Perl variables, etc.

I've read the docs and searched the net, but so far I'm coming to the conclusion that this may not be feasible with the API provided - I'd have to hack the perl library itself to provide a serialization interface, which might be a bridge too far.

What say ye, oh deep hackers?

Replies are listed 'Best First'.
Re: Embedded Perl - Saving State?
by samtregar (Abbot) on Apr 30, 2009 at 16:41 UTC
    I'm going to go out on a limb here and say it'd be really hard. Think about all the stuff that's in Perl's memory space - pointers to heap storage, filehandles, dynamically loaded libraries, mmap'd files, network sockets, and much more. Serializing a Perl process and restarting it is no more likely to work than any other C app, by which I mean not likely at all.

    If you're determined to give it a try anyway you could look at how CORE::dump() and the -u option work. You can't use them directly since you'd cause your C app to dump core that way, but it might still be useful. You've got a harder job to do that CORE::dump() since you somehow have to figure out which memory to dump and when you reload you've got to deal with the fact that your memory space isn't necessarily the same.


      I'm going to go out on a limb here and say it'd be really hard.

      Yeah. That's kind of the conclusion I'd come to, but I was hoping someone might have some firsthand knowledge.

      API calls in proto.h like perl_clone(), Perl_get_context() and Perl_set_context() made me think it wasn't a total pipe dream...

Re: Embedded Perl - Saving State?
by otto (Beadle) on Apr 30, 2009 at 18:48 UTC

    My question is this: is it possible, in my C app, to save off the state of the interpreter and restore it later?

    Sure you can, just as you can write a relational database in assembly.

    Realistically you need to ask bigger questions of why? Why not just put it in hibernate? Why not save off the whole OS run-state which I believe you can do with hypervisors like vmware. You need to be able to ask the bigger question of why you want to do this.

    It may be as easy as using Storable as one point, depending upon how the c/perl is stitched together.

    Are there just a few global type objects, singletons, from which all other data within your perl code hang?

    My suggestion is to draw pictures, I like pictures, and show what is in the perl code, what is in the code, how they communicate, what triggers communication, etc.

    You can not well address this issue without a clear understanding of the architecture - yes you may have written it, but take a 50,000 ft view, not the 10 ft view, along with why, what is the driving issues, and the answer will probably pop right out.

Re: Embedded Perl - Saving State?
by repellent (Priest) on Apr 30, 2009 at 22:57 UTC
    McD++ All I can say is, if you can solve this, you also solve the issues with fork emulation on Perl-threaded platforms like MSWin32.

    Viva la Smalltalk!
      Fixing fork emulation on Win32? That's my cue, folks, I'm out of here. Tip your waitress!

      It looks like my suspicions were correct - this will require a deeper level of fiddling than I'll have time for this project.

      Loved the Smalltalk idea, though. :-)
Re: Embedded Perl - Saving State?
by schinkelm (Initiate) on May 01, 2009 at 13:15 UTC
    Hello, you could freeze the whole process with cryopid or have a look at the progress of kernel checkpoint/restart at Marco
      Not a bad idea (and a cool article, thanks for the pointer). But I don't want to freeze the whole process - just the state of the embedded Perl interpreter.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://761106]
Approved by johngg
Front-paged by Bloodnok
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-15 03:32 GMT
Find Nodes?
    Voting Booth?

    No recent polls found