File Coverage

blib/lib/MCE/Core/Validation.pm
Criterion Covered Total %
statement 134 167 80.2
branch 85 150 56.6
condition 45 103 43.6
subroutine 9 9 100.0
pod n/a
total 273 429 63.6


line stmt bran cond sub pod time code
1             ###############################################################################
2             ## ----------------------------------------------------------------------------
3             ## Core validation methods for Many-Core Engine.
4             ##
5             ## This package provides validation methods used internally by the manager
6             ## process.
7             ##
8             ## There is no public API.
9             ##
10             ###############################################################################
11              
12             package MCE::Core::Validation;
13              
14 85     85   611 use strict;
  85         156  
  85         2621  
15 85     85   535 use warnings;
  85         171  
  85         4215  
16              
17             our $VERSION = '1.888';
18              
19             ## Items below are folded into MCE.
20              
21             package # hide from rpm
22             MCE;
23              
24 85     85   475 no warnings qw( threads recursion uninitialized );
  85         181  
  85         222339  
25              
26             ###############################################################################
27             ## ----------------------------------------------------------------------------
28             ## Validation method (attributes allowed for top-level).
29             ##
30             ###############################################################################
31              
32             sub _validate_args {
33              
34 421     421   1305 my $_s = $_[0];
35              
36 421         1117 @_ = ();
37              
38 421         1099 my $_tag = 'MCE::_validate_args';
39              
40 421 100 100     2452 if (defined $_s->{input_data} && ref $_s->{input_data} eq '') {
41             _croak("$_tag: ($_s->{input_data}) does not exist")
42 18 50       320 unless (-e $_s->{input_data});
43             }
44              
45 421         1445 for my $_k (qw(job_delay spawn_delay submit_delay loop_timeout)) {
46             _croak("$_tag: ($_k) is not valid")
47 1684 0 0     4456 if ($_s->{$_k} && (!looks_like_number($_s->{$_k}) || $_s->{$_k} < 0));
      33        
48             }
49 421         1065 for my $_k (qw(freeze thaw on_post_exit on_post_run user_error user_output)) {
50             _croak("$_tag: ($_k) is not a CODE reference")
51 2526 50 66     8398 if ($_s->{$_k} && ref $_s->{$_k} ne 'CODE');
52             }
53              
54 421         1856 _validate_args_s($_s);
55              
56 421 100       1154 if (defined $_s->{user_tasks}) {
57 257         569 for my $_t (@{ $_s->{user_tasks} }) {
  257         802  
58 406         844 _validate_args_s($_s, $_t);
59             }
60             }
61              
62 421         1217 return;
63             }
64              
65             ###############################################################################
66             ## ----------------------------------------------------------------------------
67             ## Validation method (top-level and sub-tasks).
68             ##
69             ###############################################################################
70              
71             sub _validate_args_s {
72              
73 827   66 827   1578 my $self = $_[0]; my $_s = $_[1] || $self;
  827         2774  
74              
75 827         1463 @_ = ();
76              
77 827         1599 my $_tag = 'MCE::_validate_args_s';
78              
79 827 50       2381 if (defined $_s->{max_workers}) {
80 827         2073 $_s->{max_workers} = _parse_max_workers($_s->{max_workers});
81              
82             _croak("$_tag: (max_workers) is not valid")
83 827 50       5055 if ($_s->{max_workers} !~ /\A\d+\z/);
84             }
85              
86 827 100       3035 if (defined $_s->{chunk_size}) {
87 421 50       2557 if ($_s->{chunk_size} =~ /([0-9\.]+)K\z/i) {
    50          
88 0         0 $_s->{chunk_size} = int($1 * 1024 + 0.5);
89             }
90             elsif ($_s->{chunk_size} =~ /([0-9\.]+)M\z/i) {
91 0         0 $_s->{chunk_size} = int($1 * 1024 * 1024 + 0.5);
92             }
93              
94             _croak("$_tag: (chunk_size) is not valid")
95 421 50 33     3886 if ($_s->{chunk_size} !~ /\A[0-9e\+]+\z/ or $_s->{chunk_size} == 0);
96              
97 421         1143 $_s->{chunk_size} = int($_s->{chunk_size});
98             }
99              
100             _croak("$_tag: (RS) is not valid")
101 827 50 33     2096 if ($_s->{RS} && ref $_s->{RS} ne '');
102             _croak("$_tag: (max_retries) is not valid")
103 827 50 33     2211 if ($_s->{max_retries} && $_s->{max_retries} !~ /\A\d+\z/);
104              
105 827         2009 for my $_k (qw(progress user_begin user_end user_func task_end)) {
106             _croak("$_tag: ($_k) is not a CODE reference")
107 4135 50 66     11781 if ($_s->{$_k} && ref $_s->{$_k} ne 'CODE');
108             }
109              
110 827 100       2254 if (defined $_s->{gather}) {
111 268         1022 my $_ref = ref $_s->{gather};
112              
113 268 50 66     2502 _croak("$_tag: (gather) is not a valid reference")
      100        
      66        
      66        
114             if ( $_ref ne 'MCE::Queue' && $_ref ne 'Thread::Queue' &&
115             $_ref ne 'ARRAY' && $_ref ne 'HASH' && $_ref ne 'CODE' );
116             }
117              
118 827 100       1932 if (defined $_s->{sequence}) {
119 18         82 my $_seq = $_s->{sequence};
120              
121 18 50       82 if (ref $_seq eq 'ARRAY') {
122 18         65 my ($_begin, $_end, $_step, $_fmt) = @{ $_seq };
  18         70  
123 18         229 $_seq = {
124             begin => $_begin, end => $_end, step => $_step, format => $_fmt
125             };
126             }
127             else {
128 0 0       0 _croak("$_tag: (sequence) is not a HASH or ARRAY reference")
129             if (ref $_seq ne 'HASH');
130             }
131              
132 18         88 for my $_k (qw(begin end)) {
133             _croak("$_tag: ($_k) is not defined for sequence")
134 36 50       119 unless (defined $_seq->{$_k});
135             }
136              
137 18         59 for my $_p (qw(begin end step)) {
138             _croak("$_tag: ($_p) is not valid for sequence")
139 54 50 66     504 if (defined $_seq->{$_p} && !looks_like_number($_seq->{$_p}));
140             }
141              
142 18 50       89 unless (defined $_seq->{step}) {
143 18 50       111 $_seq->{step} = ($_seq->{begin} <= $_seq->{end}) ? 1 : -1;
144 18 50       94 if (ref $_s->{sequence} eq 'ARRAY') {
145 18         386 $_s->{sequence}->[2] = $_seq->{step};
146             }
147             }
148              
149 18 50       88 if (ref $_s->{sequence} eq 'HASH') {
150 0         0 for my $_k ('begin', 'end', 'step') {
151             $_s->{sequence}{$_k} = int($_s->{sequence}{$_k})
152 0 0       0 unless ($_s->{sequence}{$_k} =~ /\./);
153             }
154             }
155             else {
156 18         62 for my $_i (0, 1, 2) {
157             $_s->{sequence}[$_i] = int($_s->{sequence}[$_i])
158 54 50       214 unless ($_s->{sequence}[$_i] =~ /\./);
159             }
160             }
161              
162 18 50 33     502 if ( ($_seq->{step} < 0 && $_seq->{begin} < $_seq->{end}) ||
      33        
      33        
      33        
163             ($_seq->{step} > 0 && $_seq->{begin} > $_seq->{end}) ||
164             ($_seq->{step} == 0)
165             ) {
166 0         0 _croak("$_tag: impossible (step size) for sequence");
167             }
168             }
169              
170 827 50       1966 if (defined $_s->{interval}) {
171 0 0       0 if (ref $_s->{interval} eq '') {
172 0         0 $_s->{interval} = { delay => $_s->{interval} };
173             }
174              
175 0         0 my $_i = $_s->{interval};
176              
177 0 0       0 _croak("$_tag: (interval) is not a HASH reference")
178             if (ref $_i ne 'HASH');
179             _croak("$_tag: (delay) is not defined for interval")
180 0 0       0 unless (defined $_i->{delay});
181             _croak("$_tag: (delay) is not valid for interval")
182 0 0 0     0 if (!looks_like_number($_i->{delay}) || $_i->{delay} < 0);
183              
184 0         0 for my $_p (qw(max_nodes node_id)) {
185             _croak("$_tag: ($_p) is not valid for interval")
186             if (defined $_i->{$_p} && (
187             !looks_like_number($_i->{$_p}) ||
188             int($_i->{$_p}) != $_i->{$_p} ||
189 0 0 0     0 $_i->{$_p} < 1
      0        
190             ));
191             }
192              
193 0 0       0 $_i->{max_nodes} = 1 unless (exists $_i->{max_nodes});
194 0 0       0 $_i->{node_id} = 1 unless (exists $_i->{node_id});
195 0         0 $_i->{_time} = MCE::Util::_time();
196             }
197              
198 827         1805 return;
199             }
200              
201             ###############################################################################
202             ## ----------------------------------------------------------------------------
203             ## Validation method (run state).
204             ##
205             ###############################################################################
206              
207             sub _validate_runstate {
208              
209 167     167   601 my $self = $_[0]; my $_tag = $_[1];
  167         750  
210              
211 167         461 @_ = ();
212              
213             _croak("$_tag: method is not allowed by the worker process")
214 167 50       588 if ($self->{_wid});
215             _croak("$_tag: method is not allowed while processing")
216 167 50       491 if ($self->{_send_cnt});
217             _croak("$_tag: method is not allowed while running")
218 167 50       592 if ($self->{_total_running});
219              
220 167         449 return;
221             }
222              
223             ###############################################################################
224             ## ----------------------------------------------------------------------------
225             ## Private functions for MCE Models { Flow, Grep, Loop, Map, Step, Stream }.
226             ##
227             ###############################################################################
228              
229             sub _parse_chunk_size {
230              
231 182     182   1063 my ($_chunk_size, $_max_workers, $_params, $_input_data, $_array_size) = @_;
232              
233 182         460 @_ = ();
234              
235 182 50 33     1250 return $_chunk_size if (!defined $_chunk_size || !defined $_max_workers);
236              
237 182 50 33     1488 if (defined $_params && exists $_params->{chunk_size}) {
238 0         0 $_chunk_size = $_params->{chunk_size};
239             }
240              
241 182 50       2227 if ($_chunk_size =~ /([0-9\.]+)K\z/i) {
    50          
242 0         0 $_chunk_size = int($1 * 1024 + 0.5);
243             }
244             elsif ($_chunk_size =~ /([0-9\.]+)M\z/i) {
245 0         0 $_chunk_size = int($1 * 1024 * 1024 + 0.5);
246             }
247              
248 182 50       1188 if ($_chunk_size eq 'auto') {
249              
250 182 50 33     2057 if ( (defined $_params && ref $_params->{input_data} eq 'CODE') ||
      66        
      33        
251             (defined $_input_data && ref $_input_data eq 'CODE')
252             ) {
253             # Iterators may optionally use chunk_size to determine how much
254             # to return per iteration. The default is 1 for MCE Models, same
255             # as for the Core API. The user_func receives an array_ref
256             # regardless if 1 or greater.
257             #
258             # sub make_iter {
259             # ...
260             # return sub {
261             # my ($chunk_size) = @_;
262             # ...
263             # };
264             # }
265 0         0 return 1;
266             }
267              
268 182         450 my $_is_file;
269 182         403 my $_size = $_array_size;
270              
271 182 100       699 if (defined $_input_data) {
272 24 100       137 if (ref $_input_data eq 'ARRAY') {
    50          
273 18         53 $_size = scalar @{ $_input_data };
  18         47  
274             } elsif (ref $_input_data eq 'HASH') {
275 6         20 $_size = scalar keys %{ $_input_data };
  6         38  
276             }
277             }
278              
279 182 100 66     2395 if (defined $_params && exists $_params->{sequence}) {
    100 33        
    100          
280 18         74 my ($_begin, $_end, $_step);
281              
282 18 50       151 if (ref $_params->{sequence} eq 'HASH') {
283 0         0 $_begin = $_params->{sequence}->{begin};
284 0         0 $_end = $_params->{sequence}->{end};
285 0   0     0 $_step = $_params->{sequence}->{step} || 1;
286             }
287             else {
288 18         60 $_begin = $_params->{sequence}[0];
289 18         47 $_end = $_params->{sequence}[1];
290 18   50     286 $_step = $_params->{sequence}[2] || 1;
291             }
292              
293 18 50 33     131 if (!defined $_input_data && !$_array_size) {
294 18         153 $_size = abs($_end - $_begin) / $_step + 1;
295             }
296             }
297             elsif (defined $_params && exists $_params->{_file}) {
298 36         101 my $_ref = ref $_params->{_file};
299              
300 36 50       187 if ($_ref eq 'SCALAR') {
    100          
301 0         0 $_size = length ${ $_params->{_file} };
  0         0  
302             } elsif ($_ref eq '') {
303 18         302 $_size = -s $_params->{_file};
304             } else {
305 18         49 $_size = 0; $_chunk_size = 393_216; # 384K
  18         39  
306             }
307              
308 36         96 $_is_file = 1;
309             }
310             elsif (defined $_input_data) {
311 24 50       406 if (ref($_input_data) =~ /^(?:GLOB|FileHandle|IO::)/) {
    50          
312 0         0 $_is_file = 1; $_size = 0; $_chunk_size = 393_216; # 384K
  0         0  
  0         0  
313             }
314             elsif (ref $_input_data eq 'SCALAR') {
315 0         0 $_is_file = 1; $_size = length ${ $_input_data };
  0         0  
  0         0  
316             }
317             }
318              
319 182 100       652 if (defined $_is_file) {
320 36 100       101 if ($_size) {
321 18         74 $_chunk_size = int($_size / $_max_workers / 24 + 0.5);
322 18 50       52 $_chunk_size = 5_242_880 if $_chunk_size > 5_242_880; # 5M
323 18 50       46 if ($_chunk_size <= 8192) {
324 18 100       155 $_chunk_size = (caller() =~ /^MCE::(?:Grep|Map|Stream)/) ? 1 : 2;
325             }
326             }
327             }
328             else {
329 146         765 $_chunk_size = int($_size / $_max_workers / 24 + 0.5);
330 146 50       490 $_chunk_size = 8000 if $_chunk_size > 8000;
331 146 50       679 if ($_chunk_size < 2) {
332 146 100       1717 $_chunk_size = (caller() =~ /^MCE::(?:Grep|Map|Stream)/) ? 1 : 2;
333             }
334             }
335             }
336              
337 182         780 return $_chunk_size;
338             }
339              
340             sub _parse_max_workers {
341              
342 1091     1091   2799 my ($_max_workers) = @_;
343              
344 1091         1910 @_ = ();
345              
346 1091 50       2416 return $_max_workers unless (defined $_max_workers);
347              
348 1091 100       4281 if ($_max_workers =~ /^auto(?:$|\s*([\-\+\/\*])\s*(.+)$)/i) {
    100          
349 126         232 my ($_ncpu_ul, $_ncpu);
350              
351 126         560 $_ncpu_ul = $_ncpu = MCE::Util::get_ncpu();
352 126 50       536 $_ncpu_ul = 8 if ($_ncpu_ul > 8);
353              
354 126 100 66     975 if (defined($1) && defined($2)) {
355 48         84 local $@; $_max_workers = eval "int($_ncpu_ul $1 $2 + 0.5)"; ## no critic
  48         3054  
356 48 100 66     402 $_max_workers = 1 if (!$_max_workers || $_max_workers < 1);
357 48 50       162 $_max_workers = $_ncpu if ($_max_workers > $_ncpu);
358             }
359             else {
360 78         197 $_max_workers = $_ncpu_ul;
361             }
362             }
363             elsif ($_max_workers =~ /^([0-9.]+)%$/) {
364 54         204 my $_percent = $1 / 100;
365 54         144 my $_ncpu = MCE::Util::get_ncpu();
366              
367 54         234 $_max_workers = int($_ncpu * $_percent + 0.5);
368 54 100       138 $_max_workers = 1 if ($_max_workers < 1);
369             }
370              
371 1091         3687 return $_max_workers;
372             }
373              
374             sub _validate_number {
375              
376 72     72   258 my ($_n, $_key, $_tag) = @_;
377              
378 72 50       307 _croak("$_tag: ($_key) is not valid") if (!defined $_n);
379              
380 72         353 $_n =~ s/K\z//i; $_n =~ s/M\z//i;
  72         164  
381              
382 72 50 33     1029 if (!looks_like_number($_n) || int($_n) != $_n || $_n < 1) {
      33        
383 0         0 _croak("$_tag: ($_key) is not valid");
384             }
385              
386 72         230 return;
387             }
388              
389             1;
390              
391             __END__