File Coverage

blib/lib/Fsdb/Support/OS.pm
Criterion Covered Total %
statement 9 23 39.1
branch 0 14 0.0
condition n/a
subroutine 3 4 75.0
pod 1 1 100.0
total 13 42 30.9


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2              
3             #
4             # Fsdb::Support::OS.pm
5             # Copyright (C) 2013 by John Heidemann
6             # $Id: d0eb3c7879eb9a5375ee0c177598e2663e364792 $
7             #
8             # This program is distributed under terms of the GNU general
9             # public license, version 2. See the file COPYING
10             # in $dblib for details.
11             #
12              
13             package Fsdb::Support::OS;
14              
15              
16             =head1 NAME
17              
18             Fsdb::Support::OS - operating-system-specific support functions
19              
20             =head1 SYNOPSIS
21              
22             use Fsdb::Support::OS;
23              
24             =cut
25             #'
26              
27              
28 1     1   5 use Exporter 'import';
  1         2  
  1         74  
29             @EXPORT = qw();
30             @EXPORT_OK = qw($max_parallelism $parallelism_available);
31             $VERSION = 1.0;
32              
33 1     1   4 use Carp qw(croak);
  1         3  
  1         63  
34              
35             # Track parallelism here.
36             # it's shared across all instances of dbmerge, but that's a feature.
37 1     1   928 our $parallelism_available : shared = undef;
  1         1367  
  1         5  
38              
39             my $max_parallelism_cache = undef;
40              
41             =head2 max_parallelism
42              
43             $cores = Fsdb::Support::OS::max_parallism()
44              
45             Finds the number of cores in the current computer,
46             or some other plausible value of parallelism for CPU-intensive tasks.
47              
48             =cut
49             sub max_parallelism() {
50 0 0   0 1   return $max_parallelism_cache
51             if (defined($max_parallelism_cache));
52              
53             # If no clue what os, so pick a plausible value for 2013.
54 0           my $max_parallelism = 4;
55              
56             #
57             # Poke around in os-specific ways to see if we can do better than
58             # the default.
59             #
60 0 0         if (-f "/proc/cpuinfo") {
    0          
61             # Linux
62 0 0         open(INFO, "/proc/cpuinfo") or die "exists, but cannot open /proc/cpuinfo\n";
63 0           while () {
64 0 0         if (/^processor\s+:\s+(\d+)/) {
65 0           $max_parallelism = $1 + 1; # add one because cpus number from 0
66             };
67             };
68 0           close INFO;
69             } elsif (-f "/var/run/dmesg.boot") {
70             # FreeBSD:
71             #
72             # Ted Faber tells me:
73             # $ grep -i CPU /var/run/dmesg.boot/var/run/dmesg.boot | grep Detected
74             # FreeBSD/SMP: Multiprocessor System Detected: 4 CPUs
75 0 0         open(INFO, "/var/run/dmesg.boot") or die "exists, but cannot open /var/run/dmesg.boot\n";
76 0           while () {
77 0 0         if (/Multiprocessor.*\s(\d+)\sCPU/) {
78 0           $max_parallelism = $1;
79 0           last;
80             };
81             };
82             };
83              
84 0           return $max_parallelism_cache = $max_parallelism;
85             }
86              
87             1;