File: Queue.pm

package info (click to toggle)
auto-multiple-choice 1.5.0~rc2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 56,204 kB
  • sloc: perl: 27,850; xml: 23,201; cpp: 1,810; python: 700; makefile: 496; sh: 217; ansic: 195
file content (184 lines) | stat: -rw-r--r-- 5,244 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#
# Copyright (C) 2008-2021 Alexis Bienvenüe <paamc@passoire.fr>
#
# This file is part of Auto-Multiple-Choice
#
# Auto-Multiple-Choice is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# Auto-Multiple-Choice is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Auto-Multiple-Choice.  If not, see
# <http://www.gnu.org/licenses/>.

use warnings;
use 5.012;

package AMC::Queue;

use AMC::Basic;
use Module::Load;
use Module::Load::Conditional qw/check_install/;
use File::Temp qw/ tempdir /;
use Storable;

# recupere le nombre de CPU, avec Sys::CPU si ce module est installe
sub get_ncpu {
    my $n = 0;
    if ( check_install( module => "Sys::CPU" ) ) {
        load("Sys::CPU");
        debug_pm_version("Sys::CPU");
        $n = Sys::CPU::cpu_count();
    } else {
        open( CI, "/proc/cpuinfo" );
        while (<CI>) {
            $n++ if (/^processor\s*:/);
        }
        close(CI);
    }
    $n = 1 if ( $n <= 0 );
    return ($n);
}

sub new {
    my (%o) = @_;

    my $self = {
        pids                => [],
        queue               => [],
        pidof               => {},
        get_returned_values => 0,
        'max.procs'         => 0,
    };

    for my $k ( keys %o ) {
        $self->{$k} = $o{$k} if ( defined( $self->{$k} ) );
    }

    if ( $self->{'max.procs'} < 1 ) {
        $self->{'max.procs'} = get_ncpu();
        debug "Max number of processes: " . $self->{'max.procs'};
    }

    if ( $self->{get_returned_values} ) {
        $self->{returned} = [];
        $self->{tmpdir}   = tempdir( );
        debug "Queue tmp dir: " . $self->{tmpdir};
    }

    bless $self;

    return ($self);
}

sub add_process {
    my ( $self, @o ) = @_;
    push @{ $self->{queue} }, [@o];
}

sub maj {
    my ($self) = @_;
    my @p = ();
    my %alive=();
    for my $pid ( @{ $self->{pids} } ) {
        if ( kill( 0, $pid ) ) {
            push @p, $pid;
            $alive{$pid} = 1;
        }
    }
    @{ $self->{pids} } = @p;
    debug "MAJ : " . join( ' ', @p );
    if ( $self->{get_returned_values} ) {
        opendir( my $dh, $self->{tmpdir} )
          || debug "Can't open $self->{tmpdir}: $!";
        while ( readdir $dh ) {
            if (/^[0-9]+$/ && !$alive{$self->{pidof}->{$_}}) {
                my $f = $self->{tmpdir} . "/" . $_;
                debug "Retrieving from $f";
                $self->{returned}->[$_] = retrieve($f);
                unlink($f);
            }
        }
        closedir $dh;
    }
    return ( 1 + $#{ $self->{pids} } );
}

sub killall {
    my ($self) = @_;
    $self->{queue} = [];
    debug_and_stderr "Queue interruption\n";
    for my $p ( @{ $self->{pids} } ) {
        debug_and_stderr "Queue: killing $p\n";
        kill 9, $p;
    }
}

sub run {
    my ( $self, $subsys ) = @_;
    debug "Queue RUN";
    my $i = 0;
    while ( @{ $self->{queue} } ) {
        while ( $self->maj() < $self->{'max.procs'}
            && @{ $self->{queue} } )
        {
            my $cs = shift( @{ $self->{queue} } );
            if ($cs) {
                my $p = fork();
                if ($p) {
                    debug "Fork : $p";
                    push @{ $self->{pids} }, $p;
                    $self->{pidof}->{$i} = $p;
                } else {
                    if ( ref( $cs->[0] ) eq 'ARRAY' ) {
                        for my $c (@$cs) {
                            debug "Command [$$] : " . join( ' ', @$c );
                            if ( system(@$c) == 0 ) {
                                debug "Command [$$] OK";
                            } else {
                                debug "Error [$$] : $?\n";
                            }
                        }
                        exit(0);
                    } elsif ( ref( $cs->[0] ) eq 'CODE' ) {
                        my $c = shift @$cs;
                        my $ret = &$c(@$cs);
                        if($self->{get_returned_values}) {
                            store($ret, $self->{tmpdir} . "/" . $i);
                        }
                        exit(0);
                    } else {
                        no warnings 'exec';
                        debug "Command [$$] : " . join( ' ', @$cs );
                        exec(@$cs);
                        debug "Bad exec $$ [" . $cs->[0] . "] unknown command";
                        die "Unknown command";
                    }
                }
                $i++;
            } else {
                debug "Queue empty";
            }
        }
        waitpid( -1, 0 );
    }
    while ( $self->maj() ) {
        debug("Waiting for all childs to end...");
        waitpid( -1, 0 );
    }
    debug("Leaving queue.");
    rmdir $self->{tmpdir} if ( $self->{tmpdir} );
}

sub returned_values {
    my ($self) = @_;
    return ( $self->{returned} );
}

1;