| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package AnyEvent::ProcessPool::Util; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: A multi-process pool for Perl | 
| 3 |  |  |  |  |  |  | $AnyEvent::ProcessPool::Util::VERSION = '0.06_001'; # TRIAL | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 3 |  |  | 3 |  | 38 | $AnyEvent::ProcessPool::Util::VERSION = '0.06001';use v5.10; | 
|  | 3 |  |  |  |  | 9 |  | 
| 6 | 3 |  |  | 3 |  | 14 | use common::sense; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 21 |  | 
| 7 | 3 |  |  | 3 |  | 967 | use Data::UUID::MT; | 
|  | 3 |  |  |  |  | 157367 |  | 
|  | 3 |  |  |  |  | 149 |  | 
| 8 | 3 |  |  | 3 |  | 811 | use parent 'Exporter'; | 
|  | 3 |  |  |  |  | 722 |  | 
|  | 3 |  |  |  |  | 15 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our @EXPORT_OK = qw( | 
| 11 |  |  |  |  |  |  | next_id | 
| 12 |  |  |  |  |  |  | cpu_count | 
| 13 |  |  |  |  |  |  | ); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub next_id { | 
| 16 | 23 |  |  | 23 | 0 | 72 | state $ug = Data::UUID::MT->new(version => 4); | 
| 17 | 23 |  |  |  |  | 7627 | $ug->create_string; | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 21 |  |  |  |  |  |  | # "Borrowed" from Test::Smoke::Util::get_ncpus. | 
| 22 |  |  |  |  |  |  | # | 
| 23 |  |  |  |  |  |  | # Modifications: | 
| 24 |  |  |  |  |  |  | #   * Use $^O in place of an input argument | 
| 25 |  |  |  |  |  |  | #   * Return number instead of string | 
| 26 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 27 |  |  |  |  |  |  | sub cpu_count { | 
| 28 |  |  |  |  |  |  | # Only *nixy osses need this, so use ':' | 
| 29 | 3 |  |  | 3 | 0 | 28 | local $ENV{PATH} = "$ENV{PATH}:/usr/sbin:/sbin"; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 3 |  |  |  |  | 8 | my $cpus = "?"; | 
| 32 |  |  |  |  |  |  | OS_CHECK: { | 
| 33 | 3 |  |  |  |  | 6 | local $_ = $^O; | 
|  | 3 |  |  |  |  | 10 |  | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 3 | 50 |  |  |  | 17 | /aix/i && do { | 
| 36 | 0 |  |  |  |  | 0 | my @output = `lsdev -C -c processor -S Available`; | 
| 37 | 0 |  |  |  |  | 0 | $cpus = scalar @output; | 
| 38 | 0 |  |  |  |  | 0 | last OS_CHECK; | 
| 39 |  |  |  |  |  |  | }; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 3 | 50 |  |  |  | 17 | /(?:darwin|.*bsd)/i && do { | 
| 42 | 0 |  |  |  |  | 0 | chomp( my @output = `sysctl -n hw.ncpu` ); | 
| 43 | 0 |  |  |  |  | 0 | $cpus = $output[0]; | 
| 44 | 0 |  |  |  |  | 0 | last OS_CHECK; | 
| 45 |  |  |  |  |  |  | }; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 3 | 50 |  |  |  | 11 | /hp-?ux/i && do { | 
| 48 | 0 |  |  |  |  | 0 | my @output = grep /^processor/ => `ioscan -fnkC processor`; | 
| 49 | 0 |  |  |  |  | 0 | $cpus = scalar @output; | 
| 50 | 0 |  |  |  |  | 0 | last OS_CHECK; | 
| 51 |  |  |  |  |  |  | }; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 3 | 50 |  |  |  | 10 | /irix/i && do { | 
| 54 | 0 |  |  |  |  | 0 | my @output = grep /\s+processors?$/i => `hinv -c processor`; | 
| 55 | 0 |  |  |  |  | 0 | $cpus = (split " ", $output[0])[0]; | 
| 56 | 0 |  |  |  |  | 0 | last OS_CHECK; | 
| 57 |  |  |  |  |  |  | }; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 3 | 50 |  |  |  | 14 | /linux/i && do { | 
| 60 | 3 |  |  |  |  | 5 | my @output; local *PROC; | 
|  | 3 |  |  |  |  | 10 |  | 
| 61 | 3 | 50 |  |  |  | 161 | if ( open PROC, "< /proc/cpuinfo" ) { ## no critic | 
| 62 | 3 |  |  |  |  | 968 | @output = grep /^processor/ => ; | 
| 63 | 3 |  |  |  |  | 96 | close PROC; | 
| 64 |  |  |  |  |  |  | } | 
| 65 | 3 | 50 |  |  |  | 16 | $cpus = @output ? scalar @output : ''; | 
| 66 | 3 |  |  |  |  | 16 | last OS_CHECK; | 
| 67 |  |  |  |  |  |  | }; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 0 | 0 |  |  |  | 0 | /solaris|sunos|osf/i && do { | 
| 70 | 0 |  |  |  |  | 0 | my @output = grep /on-line/ => `psrinfo`; | 
| 71 | 0 |  |  |  |  | 0 | $cpus =  scalar @output; | 
| 72 | 0 |  |  |  |  | 0 | last OS_CHECK; | 
| 73 |  |  |  |  |  |  | }; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 | 0 |  |  |  | 0 | /mswin32|cygwin/i && do { | 
| 76 |  |  |  |  |  |  | $cpus = exists $ENV{NUMBER_OF_PROCESSORS} | 
| 77 | 0 | 0 |  |  |  | 0 | ? $ENV{NUMBER_OF_PROCESSORS} : ''; | 
| 78 | 0 |  |  |  |  | 0 | last OS_CHECK; | 
| 79 |  |  |  |  |  |  | }; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 | 0 |  |  |  | 0 | /vms/i && do { | 
| 82 | 0 |  |  |  |  | 0 | my @output = grep /CPU \d+ is in RUN state/ => `show cpu/active`; | 
| 83 | 0 | 0 |  |  |  | 0 | $cpus = @output ? scalar @output : ''; | 
| 84 | 0 |  |  |  |  | 0 | last OS_CHECK; | 
| 85 |  |  |  |  |  |  | }; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 0 |  |  |  |  | 0 | $cpus = ""; | 
| 88 | 0 |  |  |  |  | 0 | require Carp; | 
| 89 | 0 |  |  |  |  | 0 | Carp::carp( "get_ncpu: unknown operationg system" ); | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 3 |  | 50 |  |  | 89 | return sprintf '%d', ($cpus || 1); | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | 1; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | __END__ |