Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Slow startup on raspberry pi

by vivat (Initiate)
on Jan 16, 2015 at 12:23 UTC ( [id://1113502]=note: print w/replies, xml ) Need Help??


in reply to Re: Slow startup on raspberry pi
in thread Slow startup on raspberry pi

My script uses following modules:
use IO::Socket; use Fcntl; use strict; use threads; use threads::shared; use MP3::Info; use List::Util 'shuffle'; use LWP; use Switch; use Astro::Sunrise;
A simple script just printing "Hello World" needs with this modules more than 6 seconds to load whereby the last module (Astro::Sunrise) needs the longest time (ca. 2,5 s). The load time of the other modules is lower (< 1 second). With my more complex script I have almost the double load time until the first line is executed (with the same modules). Maybe perl is simply not appropriate on low performance platforms except for one-liners and I have to rewrite the stuff in C.

Replies are listed 'Best First'.
Re^3: Slow startup on raspberry pi
by hardburn (Abbot) on Jan 16, 2015 at 21:11 UTC

    First thing you should look at on the Rpi is having a good power supply. Cheap old cellphone chargers won't cut it. I've had good success with the Canakit adapter. Since your program is I/O limited, finding a quality SD card is the second thing you should look at. Afraid I'm not as much help for this, but I usually buy Class 10 cards and get along OK.

    Looking at your list of modules, you should get rid of threads and threads::shared, preferably rewriting those pieces with fork(), or perhaps AnyEvent. A threaded perl build is noticeably slower than a non-threaded perl, and exists mainly to do pretend fork()s on Windows.

    (As an aside, I just noticed that the default Raspbian perl5.14.2 is compiled with threads. I'm not sure, but hiveberry may come with a perl without threads.)

    You can also ditch Switch and rewrite those parts as an if/elsif series, or perhaps given/when if you're feeling adventurous:

    http://perldoc.perl.org/perlsyn.html#Switch-Statements

    Just doing those, I think you'll be able to chew off a few seconds at startup time.


    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re^3: Slow startup on raspberry pi
by Anonymous Monk on Jan 16, 2015 at 12:45 UTC
    Maybe perl is simply not appropriate on low performance platforms except for one-liners and I have to rewrite the stuff in C.

    But I thought earlier you said "Once running, the performance is good"? It sounds like you're just running this script once on boot, like a daemon, or does it get started & stopped more often? If just once on boot, I'm not sure what the problem is, why do you really need to optimize the boot time? I understand the speed might be a bit annoying during development but hey, that's the price for running on that hardware ;-) Maybe you can develop on a desktop machine?

    The Rasperry Pi is in general "slow" by modern standards so if you want to use it you'll have to live with a bit of relative slowness no matter what you do with it - try running startx on it to get a feeling for the speed! FWIW, I've written a bit of Perl for the Pi myself, admittedly with less modules than you're using, and those scripts ran just fine.

      The overall boot time is crucial for that application. I optimised the raspberry pi boot time to 20s but get additional more than 10s by this perl script. I.e. the whole linux system needs 20s but a simple perl script adds 10s.
        It's not a simple Perl script your modules are using other modules which in turn use other modules ...

        And at least one of them is either doing heavy initialization or IO accessing.

        Reprogramming it in C won't help much.

        But learning how to use the NYT profiler and identifying the bottleneck is far more efficient.

        Cheers Rolf

        PS: Je suis Charlie!

        The overall boot time is crucial for that application.

        Ok, thanks for the clarification. I'm still not really surprised that you're getting that kind of performance out of the Pi in general... how fast is your SD card? Perhaps throwing a little more hardware at the problem would help? For example I've read good things about the quad-core 1.5 GHz ODROID-C1, for $35 (http://www.hardkernel.com/).

Re^3: Slow startup on raspberry pi
by vkon (Curate) on Jan 17, 2015 at 12:59 UTC

    I

    there is special consideration on 'use threads;' - start these as early as possible, and yet http://perlmonks.org/?node_id=288022: they are not lightweight, (although some people argue and have different opinion)

    All in all, is this 'use threads;' really a necessity in your script?

    Starting several scripts in parallel could better do the job, IMO.

    II

    I do have a real advise on how to divide startup time of your use ...; lines by factor of 4,

    Those 'use'-d modules are loaded on the fly by 'use' statement and their blabla.so.x object loaded.
    Just build these modules statically, and you're done!

    I have tried this approach on windows only (on linux I only used standard perl and my own ordinary builds at /opt/...)
    On windows, I successfully statically linked perl modules statically into perl executable, fortunately the Win32 makefile allows to do so.

    If startup time is still slow after that, then rip out the Autoloader, so this will avoid usage of a large amount of tiny files, instead most will be loaded quickly.
    Also there is another boost option, I'll let you know on that if needed, ...

    PS. in earlier days, when perl run just fine on 486, it was small and quick, and the problem of slow startup was not on the table.
    :)

      Earlier start of threads did not help. I'm thinking about to rewrite the script without threads, but I'm afraid it wont help much. To speed up the start by factor 4 would really solve the problem. But to make the whole perl from source for static linking is probably on a raspberry pi a never ending story. I'll give a try. Thank you for the hint.
        But to make the whole perl from source for static linking is probably on a raspberry pi a never ending story.
        Haven't done it myself, but in this situation I'd try cross compiling. Google comes up with http://arsv.github.io/perl-cross/
        I'll expand a little on my Win32 experience on what I did when I did static ilnkage of perl modules into main executable, which could help you some.
        • rebuilt perl some needed XS modules statically, (this step could be skipped, actually)
        • installed said perl into some dir for my edits (this could be just normal /opt/perlXXX setup for example)
        • I cut out all of the Autoloader infrastructure
        • use something like Module::Scandeps to get needed "pm" files *and* packed these into ZIP; or you can even ZIP all of perl module PMs
        • some ~20 xxx.pm files are required to bootstrap Archive::ZIP to look into said ZIP archive, so I made in-memory hash of these files, yet I made it fast-to-load by some special technicque, yet I used Compress::Zlib to in-memory_compress it and placed into __DATA__ segment of my bootstrapper
        • I hooked "CORE::require" so that they look into either said ZIP or into in-memory-hash
        As a result, I had these 4 files:
        • perl.exe + my larger perl58st.dll (with statically linked extensions, but normal perl.so will also suffice here)
        • perllib.zip - to contain all pm files
        • compr.pm - my mentioned hooker and bootstrapper
        These 4 files contained entire setup with any said number of modules inside, except you should place module's shared library into the same dir, so it will be about 10 files more...

        Given that static build isn't easy for you, all other steps could still apply and give you a speed win...

        addition just thought, that my way gives you universal perl that is quicker-to-startup... If you need faster application run, you can avoid ZIP usage, and place all into in-memory-compressed content, so will be much simplier (yet smaller and faster), but less universal.

        HTH,
        vkon

Re^3: Slow startup on raspberry pi
by LanX (Saint) on Jan 16, 2015 at 12:28 UTC
      It's not about blaming perl but rather some "horses for courses" thing...
        Perl execution is roughly 10 times slower than C.

        But IO access won't be faster.

        If one of the modules has a slow init phase it'll be reasonable to patch it (eg by memoizing Astronomical tables?)

        Reprogramming it in C is only reasonable if such modules are available in C.

        Otherwise porting will take longer than patching.

        Cheers Rolf

        PS: Je suis Charlie!

Re^3: Slow startup on raspberry pi
by xlr82xs (Acolyte) on Jan 18, 2015 at 23:44 UTC

    Due to the requirements of the modules that you are loading, you are actually reading in a lot more than you think. The actual list is more along the lines of -snip- something too large to actually post here.

    Try using scandeps from Module::ScanDeps against your code (even just the list of use statements) and take a look at everything that Astro::Sunrise is requiring from the DateTime tree.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1113502]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found