Changeset 3871

Show
Ignore:
Timestamp:
08/20/10 14:02:58 (1 year ago)
Author:
janl
Message:

* munin-html: Start using fork to save some wall-clock time. Option --fork us now active and max_html_jobs is recognized by munin-html as the maximum number of paralell processes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/master/lib/Munin/Master/HTMLOld.pm

    r3404 r3871  
    9090 
    9191my $do_dump = 0; 
    92 my $do_fork = 0; # No effect in this program. 
     92my $do_fork = 1; 
     93my $max_running=6; 
     94my $running=0; 
     95 
    9396 
    9497sub html_startup { 
     
    122125    $tmpldir = $config->{tmpldir}; 
    123126    $htmldir = $config->{htmldir}; 
     127 
     128    $max_running = &munin_get($config, "max_html_jobs", $max_running); 
    124129 
    125130    %comparisontemplates = instanciate_comparison_templates($tmpldir); 
     
    827832 
    828833 
     834sub fork_and_work { 
     835    my ($work) = @_; 
     836 
     837    if (!$do_fork) { 
     838 
     839        # We're not forking.  Do work and return. 
     840        DEBUG "[DEBUG] Doing work synchrnonously"; 
     841        &$work; 
     842        return; 
     843    } 
     844 
     845    # Make sure we don't fork too much 
     846    while ($running >= $max_running) { 
     847        DEBUG 
     848            "[DEBUG] Too many forks ($running/$max_running), wait for something to get done"; 
     849        look_for_child("block"); 
     850        --$running; 
     851    } 
     852 
     853    my $pid = fork(); 
     854 
     855    if (!defined $pid) { 
     856        ERROR "[ERROR] fork failed: $!"; 
     857        die "fork failed: $!"; 
     858    } 
     859 
     860    if ($pid == 0) { 
     861 
     862        # This block does the real work.  Since we're forking exit 
     863        # afterwards. 
     864 
     865        &$work; 
     866 
     867        # See?! 
     868 
     869        exit 0; 
     870 
     871    } 
     872    else { 
     873        ++$running; 
     874        DEBUG "[DEBUG] Forked: $pid. Now running $running/$max_running"; 
     875        while ($running and look_for_child()) { 
     876            --$running; 
     877        } 
     878    } 
     879} 
     880 
     881 
    829882sub generate_group_templates { 
    830883    my $arr = shift || return; 
     
    840893            if (defined $key->{'ngroups'} and $key->{'ngroups'}) { 
    841894                # WTF: $key->{'groups'} = $key->{'groups'}; 
    842                 generate_group_templates($key->{'groups'}); 
     895                fork_and_work(sub {generate_group_templates($key->{'groups'})}); 
    843896 
    844897                emit_group_template($key);