File: CpuAffinity.pm

package info (click to toggle)
libsys-cpuaffinity-perl 1.13~03-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 252 kB
  • sloc: perl: 1,261; makefile: 3
file content (2048 lines) | stat: -rw-r--r-- 64,171 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
package Sys::CpuAffinity;
use Math::BigInt;
use Carp;
use warnings;
use strict;
use base qw(DynaLoader);
use Data::Dumper;

## no critic (ProhibitBacktick,RequireExtendedFormatting)
## no critic (DotMatch,LineBoundary,Sigils,Punctuation,Quotes,Magic,Checked)
## no critic (NamingConventions::Capitalization,BracedFileHandle)

our $VERSION = '1.13_03';
our $DEBUG = $ENV{DEBUG} || 0;
our $XS_LOADED = 0;
eval { bootstrap Sys::CpuAffinity $VERSION; $XS_LOADED = 1 };

sub TWO () { Math::BigInt->new(2) }

# sub import { }

#
# Development guide:
#
# when you figure out a new way to perform a task
# (in this case, getting cpu affinity), write the method and insert
# the call into the chain here.
#
# Methods should be named  _getAffinity_with_XXX, _setAffinity_with_XXX,
# or _getNumCpus_from_XXX. The t/inventory.pl file will identify these
# methods so they can be included in the tests.
#
# The new method should return false (0 or '' or undef) whenever it
# knows it is the wrong tool for the current system or any other time
# that it can't figure out the answer.
#
# For XS-based solutions, the stub will go in the distributions
# contrib/  directory, and will be available if it successfully
# compiles during the installation process. See
# _getAffinity_with_xs_sched_getaffinity  for an example of
# how to use a compiled function. All exported XS function names
# should begin with "xs_" and all function names, even the ones
# that aren't exported to XS, should be unique across the whole
# /contrib  space.
#
# Methods that might return with the wrong answer (for example, methods
# that make a guess) should go toward the end of the chain. This
# probably should include methods that read environment variables
# or methods that rely on external commands as these methods are
# easier to spoof, even accidentally.
#

sub getAffinity {
    my ($pid, %flags) = @_; # %flags reserved for future use
    my $wpid = $pid;

    # 2020-12-18: taskset only returns 32 bits? Give xs_sched more priority
    
    my $mask = 0
	|| _getAffinity_with_xs_sched_getaffinity($pid)
	|| _getAffinity_with_xs_pthread_self_getaffinity($pid)
	|| _getAffinity_with_taskset($pid)
	|| _getAffinity_with_BSD_Process_Affinity($pid)
	|| _getAffinity_with_xs_freebsd_getaffinity($pid)
	|| _getAffinity_with_cpuset($pid)
	|| _getAffinity_with_xs_processor_affinity($pid)
	|| _getAffinity_with_pbind($pid)
	|| _getAffinity_with_xs_processor_bind($pid)
        || _getAffinity_with_psaix($pid)
	|| _getAffinity_with_xs_win32($pid)
	|| _getAffinity_with_xs_irix_sysmp($pid)
	|| _getAffinity_with_Win32Process($wpid)
	|| _getAffinity_with_Win32API($wpid)
	|| 0;

    return if $mask == 0;
    return wantarray ? _maskToArray($mask) : $mask;
}

sub _sanitize_set_affinity_args {
    my ($pid,$mask) = @_;

    if ($DEBUG) {
        print STDERR "sanitize_set_affinity_args: input is ",Dumper(@_),"\n";
    }

    return if ! $pid;
    if (ref $mask eq 'ARRAY') {
	$mask = _arrayToMask(@$mask);
        if ($DEBUG) {
            print STDERR "sanitize_set_affinity_args: ",
                         Dumper($_[1])," => $mask\n";
        }
    }
    my $np = getNumCpus();
    if ($mask == -1 && $np > 0) {
	$mask = (TWO ** $np) - 1;
        if ($DEBUG) {
            print STDERR "sanitize_set_affinity_args: -1 => ",
                         $mask," ",Dumper($mask),"\n";
        }
    }
    if ($mask <= 0) {
	carp "Sys::CpuAffinity: invalid mask $mask in call to setAffinty\n";
	return;
    }

    my $maxmask = TWO ** $np;
    if ($maxmask > 1 && $mask >= $maxmask) {
	my $newmask = $mask & ($maxmask - 1);
	if ($newmask == 0) {
	    carp "Sys::CpuAffinity: mask $mask is not valid for system with ",
	    "$np processors.\n";
	    return;
	} else {
	    carp "Sys::CpuAffinity: mask $mask adjusted to $newmask for ",
	    "system with $np processors\n";
	    $mask = $newmask;
	}
    }
    $_[1] = $mask;
    return 1;
}

sub setAffinity {
    my ($pid, $mask, %flags) = @_; # %flags reserved for future use

    return 0 if ! _sanitize_set_affinity_args($pid, $mask);

    return _setAffinity_with_Win32API($pid,$mask)
	|| _setAffinity_with_xs_win32($pid,$mask)
	|| _setAffinity_with_Win32Process($pid,$mask)
	|| _setAffinity_with_xs_sched_setaffinity($pid,$mask)
	|| _setAffinity_with_taskset($pid,$mask)
	|| _setAffinity_with_BSD_Process_Affinity($pid,$mask)
	|| _setAffinity_with_xs_freebsd_setaffinity($pid,$mask)
        || _setAffinity_with_xs_processor_affinity($pid,$mask)
	|| _setAffinity_with_pbind($pid,$mask)
        || _setAffinity_with_xs_processor_bind($pid,$mask)
	|| _setAffinity_with_xs_pthread_self_setaffinity($pid,$mask)
	|| _setAffinity_with_bindprocessor($pid,$mask)
	|| _setAffinity_with_cpuset($pid,$mask)
	|| _setAffinity_with_xs_irix_sysmp($pid,$mask)
	|| 0;
}

our $_NUM_CPUS_CACHED = 0;
sub getNumCpus {
    if ($_NUM_CPUS_CACHED) {
	return $_NUM_CPUS_CACHED;
    }
    return $_NUM_CPUS_CACHED =
	_getNumCpus_from_xs_Win32API_System_Info()
	|| _getNumCpus_from_xs_cpusetGetCPUCount()
	|| _getNumCpus_from_proc_cpuinfo()
	|| _getNumCpus_from_proc_stat()
	|| _getNumCpus_from_lsdev()
	|| _getNumCpus_from_bindprocessor()
	|| _getNumCpus_from_BSD_Process_Affinity()
	|| _getNumCpus_from_sysctl_freebsd()
	|| _getNumCpus_from_sysctl()
	|| _getNumCpus_from_dmesg_bsd()
        || _getNumCpus_from_xs_solaris()
	|| _getNumCpus_from_dmesg_solaris()
	|| _getNumCpus_from_psrinfo()
	|| _getNumCpus_from_hinv()
	|| _getNumCpus_from_hwprefs()
	|| _getNumCpus_from_system_profiler()
	|| _getNumCpus_from_Win32API_System_Info()
	|| _getNumCpus_from_Test_Smoke_SysInfo()
	|| _getNumCpus_from_prtconf()   # slower than bindprocessor, lsdev
	|| _getNumCpus_from_ENV()
	|| _getNumCpus_from_taskset()
	|| -1;
}

######################################################################

# count processors toolbox

sub _getNumCpus_from_ENV {
    # in some OS, the number of processors is part of the default environment
    # this also makes it easy to spoof the value (is that good or bad?)
    if ($^O eq 'MSWin32' || $^O eq 'cygwin') {
	if (defined $ENV{NUMBER_OF_PROCESSORS}) {
	    _debug("from Windows ENV: nproc=$ENV{NUMBER_OF_PROCESSORS}");
	    return $ENV{NUMBER_OF_PROCESSORS};
	}
    }
    return 0;
}

our %WIN32_SYSTEM_INFO = ();
our %WIN32API = ();

sub __is_wow64 {
    # determines whether this (Windows) program is running the WOW64 emulator
    # (to let 32-bit apps run on 64-bit architecture)

    # used in _getNumCpus_from_Win32API_System_Info to decide whether to use
    # GetSystemInfo  or  GetNativeSystemInfo  in the Windows API.

    return 0 if $^O ne 'MSWin32' && $^O ne 'cygwin';
    return 0 if !_configModule('Win32::API');
    return $Sys::CpuAffinity::IS_WOW64
	if $Sys::CpuAffinity::IS_WOW64_INITIALIZED++;

    my $hmodule = _win32api('GetModuleHandle', 'kernel32');
    return 0 if $hmodule == 0;

    my $proc = _win32api('GetProcAddress', $hmodule, 'IsWow64Process');
    return 0 if $proc == 0;

    my $current = _win32api('GetCurrentProcess');
    return 0 if $current == 0;  # carp ...

    my $bool = 0;
    my $result = _win32api('IsWow64Process', $current, $bool);
    if ($result != 0) {
	$Sys::CpuAffinity::IS_WOW64 = $bool;
    }
    $Sys::CpuAffinity::IS_WOW64_INITIALIZED++;
    return $Sys::CpuAffinity::IS_WOW64;
}

sub _getNumCpus_from_Win32API_System_Info {
    return 0 if $^O ne 'MSWin32' && $^O ne 'cygwin';
    return 0 if !_configModule('Win32::API');

    if (0 == scalar keys %WIN32_SYSTEM_INFO) {
	if (!defined $WIN32API{'GetSystemInfo'}) {
	    my $is_wow64 = __is_wow64();
	    my $lpsysinfo_type_avail
		= Win32::API::Type::is_known('LPSYSTEM_INFO');

	    my $proto = sprintf 'BOOL %s(%s i)',
		    $is_wow64 ? 'GetNativeSystemInfo' : 'GetSystemInfo',
		    $lpsysinfo_type_avail ? 'LPSYSTEM_INFO' : 'PCHAR';

	    $WIN32API{'GetSystemInfo'} = Win32::API->new('kernel32', $proto);
	}

	# does this part break on 64-bit machines? Don't think so.
	my $buffer = chr(0) x 36;
	$WIN32API{'GetSystemInfo'}->Call($buffer);
	($WIN32_SYSTEM_INFO{'PageSize'},
	 $WIN32_SYSTEM_INFO{'...'},
	 $WIN32_SYSTEM_INFO{'...'},
	 $WIN32_SYSTEM_INFO{'...'},
	 $WIN32_SYSTEM_INFO{'NumberOfProcessors'},
	 $WIN32_SYSTEM_INFO{'...'},
	 $WIN32_SYSTEM_INFO{'...'},
	 $WIN32_SYSTEM_INFO{'...'},
	 $WIN32_SYSTEM_INFO{'...'})
	    = unpack 'VVVVVVVvv',   substr $buffer,4;
    }
    return $WIN32_SYSTEM_INFO{'NumberOfProcessors'} || 0;
}


sub _getNumCpus_from_xs_cpusetGetCPUCount { # NOT TESTED irix
    if ($XS_LOADED && defined &xs_cpusetGetCPUCount) {
	return xs_cpusetGetCPUCount();
    } else {
	return 0;
    }
}

sub _getNumCpus_from_xs_Win32API_System_Info {
    if (defined &xs_get_numcpus_from_windows_system_info) {
	return xs_get_numcpus_from_windows_system_info();
    } elsif (defined &xs_get_numcpus_from_windows_system_info_alt) {
	return xs_get_numcpus_from_windows_system_info_alt();
    } else {
	return 0;
    }
}

sub _getNumCpus_from_proc_cpuinfo {

    # I'm told this could give the wrong answer with a "non-SMP kernel"
    # http://www-oss.fnal.gov/fss/hypermail/archives/hyp-linux/0746.html

    return 0 if ! -r '/proc/cpuinfo';

    my $num_processors = 0;
    my $cpuinfo_fh;
    if (open $cpuinfo_fh, '<', '/proc/cpuinfo') {
	while (<$cpuinfo_fh>) {
	    if (/^processor\s/) {
		$num_processors++;
	    }
	}
	close $cpuinfo_fh;
    }
    _debug("from /proc/cpuinfo: nproc=$num_processors");
    return $num_processors || 0;
}

sub _getNumCpus_from_proc_stat {

    return 0 if ! -r '/proc/stat';

    my $num_processors = 0;
    my $stat_fh;
    if (open $stat_fh, '<', '/proc/stat') {
	while (<$stat_fh>) {
	    if (/^cpu\d/i) {
		$num_processors++;
	    }
	}
	close $stat_fh;
    }
    _debug("from /proc/stat: nproc=$num_processors");
    return $num_processors || 0;
}

sub __set_aix_hints {
    my ($bindprocessor) = @_;
    our $AIX_HINTS = { READY => 0 };
    if (!$bindprocessor) {
        $bindprocessor = _configExternalProgram('bindprocessor');
    }
    return unless $bindprocessor;

    my $vp_output = qx('$bindprocessor' -q 2>/dev/null);
    if ($vp_output !~ s/The available process\S+ are:\s*//) {
        return;
    }
    my @vp = split /\s+/, $vp_output;
    @vp = sort { $a <=> $b } @vp;
    $AIX_HINTS->{VIRTUAL_PROCESSORS} = \@vp;
    my %vp = map {; $_ => -1 } @vp;
    my $proc_output = qx('$bindprocessor' -s 0 2>/dev/null);
    if ($proc_output !~ s/The available process\S+ are:\s*//) {
        $AIX_HINTS->{PROCESSORS} = $AIX_HINTS->{VIRTUAL_PROCESSORS};
        $AIX_HINTS->{NUM_CORES} = @vp;
        return;
    }
    my @procs = split /\s+/, $proc_output;
    @procs = sort { $a <=> $b } @procs;
    $AIX_HINTS->{PROCESSORS} = \@procs;
    $AIX_HINTS->{NUM_CORES} = @procs;
    $AIX_HINTS->{READY} = 1;
    if (@procs == @vp) {
        foreach my $proc (@procs) {
            $AIX_HINTS->{PROC_MAP}{$_} = $_;
        }
    } else {
        my $core = -1;
        foreach my $proc (@procs) {
            $core++;
            my $bound_output = qx('$bindprocessor' -b $proc 2>/dev/null);
            if ($bound_output =~ s/The available process\S+ are:\s*//) {
                my @bound_proc = split /\s+/, $bound_output;
                foreach my $bound_proc (@bound_proc) {
                    $AIX_HINTS->{PROC_MAP}{$bound_proc} = $core;
                }
            }
        }
    }
}

sub _is_solarisMultiCpuBinding {
    our $SOLARIS_HINTS;
    return unless $^O =~ /solaris/i;
    if (!$SOLARIS_HINTS || !$SOLARIS_HINTS->{multicpu}) {
        local $?;
        my ($maj,$min) = split /[.]/, qx(uname -v);
        if ($? == 0 && ($maj > 11 || ($maj == 11 && $min >= 2))) {
            $SOLARIS_HINTS->{multicpu} = 'yes';
        } elsif (defined &xs_setaffinity_processor_affinity) {
            $SOLARIS_HINTS->{multicpu} = 'yes';
        } else {
            $SOLARIS_HINTS->{multicpu} = 'no';
        }
    }
    return $SOLARIS_HINTS->{multicpu} eq 'yes';
}

sub _getNumCpus_from_bindprocessor {
    return 0 if $^O !~ /aix/i;
    return 0 if !_configExternalProgram('bindprocessor');
    my $cmd = _configExternalProgram('bindprocessor');
    our $AIX_HINTS;
    __set_aix_hints($cmd) unless $AIX_HINTS;
    return $AIX_HINTS->{NUM_CORES} || 0;
    #my $bindprocessor_output = qx($cmd -s 0 2>/dev/null); # or $cmd -q ?
    my $bindprocessor_output = qx($cmd -q 2>/dev/null); # or $cmd -s 0 ?
    $bindprocessor_output =~ s/\s+$//;
    return 0 if !$bindprocessor_output;

    # Typical output: "The available processors are: 0 1 2 3"
    $bindprocessor_output =~ s/.*:\s+//;
    my @p = split /\s+/, $bindprocessor_output;
    return 0+@p;
}

sub _getNumCpus_from_lsdev {
    return 0 if $^O !~ /aix/i;
    return 0 if !_configExternalProgram('lsdev');
    my $cmd = _configExternalProgram('lsdev');
    my @lsdev_output = qx($cmd -Cc processor 2>/dev/null);
    return 0+@lsdev_output;
}

sub _getNumCpus_from_dmesg_bsd {
    return 0 if $^O !~ /bsd/i;

    my @dmesg;
    if (-r '/var/run/dmesg.boot' && open my $fh, '<', '/var/run/dmesg.boot') {
	@dmesg = <$fh>;
	close $fh;
    } elsif (! _configExternalProgram('dmesg')) {
	return 0;
    } else {
	my $cmd = _configExternalProgram('dmesg');
	@dmesg = qx($cmd 2> /dev/null);
    }
    # on the version of FreeBSD that I have to play with
    # (8.0), dmesg contains this message:
    #
    #       FreeBSD/SMP: Multiprocessor System Detected: 2 CPUs
    #
    # so we'll go with that.
    #
    # on NetBSD, the message is:
    #
    #       cpu3 at mainbus0 apid 3: AMD 686-class, 1975MHz, id 0x100f53

    # try FreeBSD format
    my @d = grep { /Multiprocessor System Detected:/i } @dmesg;
    my $ncpus;
    if (@d > 0) {
	_debug("dmesg_bsd contains:\n@d");
	($ncpus) = $d[0] =~ /Detected: (\d+) CPUs/i;
    }

    # try NetBSD format. This will also probably work for OpenBSD.
    if (!$ncpus) {
	# 1.05 - account for duplicates in @dmesg
	my %d = ();
	@d = grep { /^cpu\d+ at / } @dmesg;
	foreach my $dmesg (@d) {
	    if ($dmesg =~ /^cpu(\d+) at /) {
		$d{$1}++;
	    }
	}
	_debug("dmesg_bsd[2] contains:\n",@d);
	$ncpus = scalar keys %d;
    }
    if (@dmesg < 50) {
	_debug("full dmesg log:\n", @dmesg);
    }
    return $ncpus || 0;
}

sub _getNumCpus_from_xs_solaris {
    return 0 if $^O !~ /solaris/i;
    return 0 if !defined &xs_solaris_numCpus;
    my $n = eval { xs_solaris_numCpus() };
    return $n || 0;
}

sub _getNumCpus_from_sysctl_freebsd {
    return 0 unless defined &xs_num_cpus_freebsd;
    return xs_num_cpus_freebsd() || 0;
}

sub _getNumCpus_from_dmesg_solaris {
    return 0 if $^O !~ /solaris/i;
    return 0 if !_configExternalProgram('dmesg');
    my $cmd = _configExternalProgram('dmesg');
    my @dmesg = qx($cmd 2> /dev/null);

    # a few clues that I see on my system (opensolaris 5.11 i86pc):
    #      ... blah blah is bound to cpu <n>
    #      ^cpu<n>: x86 blah blah
    my $ncpus = 0;
    foreach my $dmesg (@dmesg) {
        if ($dmesg =~ /is bound to cpu (\d+)/) {
	  my $n = $1;
	  if ($ncpus <= $n) {
	    $ncpus = $n + 1;
	  }
        }
        if ($dmesg =~ /^cpu(\d+):/) {
	  my $n = $1;
	  if ($ncpus <= $n) {
	    $ncpus = $n + 1;
	  }
        }
    }

    # this doesn't always work 
    # (www.cpantesters.org/cpan/report/35d7685a-70b0-11e0-9552-4df9775ebe45)
    # what else should we check for in  @dmesg ?
    if ($ncpus == 0) {
      # ...
    }

    return $ncpus;
}

sub _getNumCpus_from_sysctl {
    # sysctl works on a number of systems including MacOS
    return 0 if !_configExternalProgram('sysctl');
    my $cmd = _configExternalProgram('sysctl');
    my @sysctl = qx($cmd -a 2> /dev/null);
    my @results = grep { /^hw.(?:avail|n)cpu\s*[:=]/ } @sysctl;
    _debug("sysctl output:\n@results");
    return 0 if @results == 0;
    my ($ncpus) = $results[0] =~ /[:=]\s*(\d+)/;

    if ($ncpus == 0) {
	my $result = qx($cmd -n hw.ncpu 2> /dev/null);
	_debug("sysctl[2] result: $result");
	$ncpus = 0 + $result;
    }
    if ($ncpus == 0) {
	my $result = qx($cmd -n hw.ncpufound 2> /dev/null);
	_debug("sysctl[3] result: $result");
	$ncpus = 0 + $result;
    }
    if ($ncpus == 0) {
	my $result = qx($cmd -n hw.availcpu 2> /dev/null);
	_debug("sysctl[4] result: $result");
	$ncpus = 0 + $result;
    }


    return $ncpus || 0;

    # there are also sysctl/sysctlbyname system calls
}

sub _getNumCpus_from_psrinfo {
    return 0 if !_configExternalProgram('psrinfo');
    my $cmd = _configExternalProgram('psrinfo');
    my @info = qx($cmd 2> /dev/null);
#   return scalar grep /core/, qx($cmd -t 2>/dev/null);
    return scalar @info;
}

sub _getNumCpus_from_hinv {   # NOT TESTED irix
    return 0 if $^O =~ /irix/i;
    return 0 if !_configExternalProgram('hinv');
    my $cmd = _configExternalProgram('hinv');

    # test debug
    if ($Sys::CpuAffinity::IS_TEST && !$Sys::CpuAffinity::HINV_CALLED++) {
	print STDERR "$cmd output:\n";
	print STDERR qx($cmd);
	print STDERR "\n\n";
	print STDERR "$cmd -c processor output:\n";
	print STDERR qx($cmd -c processor);
	print STDERR "\n\n";
    }

    # found this in Test::Smoke::SysInfo v0.042 in Test-Smoke-1.43 module
    my @processor = qx($cmd -c processor 2> /dev/null);
    _debug('"hinv -c processor" output: ', @processor);
    my ($cpu_cnt) = grep { /\d+.+processors?$/i } @processor;
    my $ncpu = (split ' ', $cpu_cnt)[0];

    if ($ncpu == 0) {
	# there might be output like:
	# PU 30 at Module 001c35/Slot 0/Slice C: 400 Mhz MIPS R12000 Processor
	$ncpu = grep { /^CPU / } @processor;
    }

    return $ncpu;
}

sub _getNumCpus_from_hwprefs {
    return 0 if $^O !~ /darwin/i && $^O !~ /MacOS/i;
    return 0 if !_configExternalProgram('hwprefs');
    my $cmd = _configExternalProgram('hwprefs');
    my $result = qx($cmd cpu_count 2> /dev/null);
    $result =~ s/\s+$//;
    _debug("\"$cmd cpu_count\" output: ", $result);
    return $result || 0;
}

sub _getNumCpus_from_system_profiler {  # NOT TESTED darwin
    return 0 if $^O !~ /darwin/ && $^O !~ /MacOS/i;
    return 0 if !_configExternalProgram('system_profiler');

    # with help from Test::Smoke::SysInfo
    my $cmd = _configExternalProgram('system_profiler');
    my $system_profiler_output
	= qx($cmd -detailLevel mini SPHardwardDataType 2> /dev/null);
    my %system_profiler;
    while ($system_profiler_output =~ m/^\s*([\w ]+):\s+(.+)$/gm) {
	$system_profiler{uc $1} = $2;
    }

    my $ncpus = $system_profiler{'NUMBER OF CPUS'};
    if (!defined $ncpus) {
	$ncpus = $system_profiler{'TOTAL NUMBER OF CORES'};
    }
    return $ncpus;
}

sub _getNumCpus_from_prtconf {
    # solaris has a prtconf command, but I don't think it outputs #cpus.
    return 0 if $^O !~ /aix/i;
    return 0 if !_configExternalProgram('prtconf');
    my $cmd = _configExternalProgram('prtconf');

    # prtconf can take a long time to run, so cache the result
    our $AIX_prtconf_cache;
    if (!defined($AIX_prtconf_cache)) {
        my @result = qx($cmd 2> /dev/null);
        my ($result) = grep { /Number Of Processors:/ } @result;
        return 0 if !$result;
        my ($ncpus) = $result =~ /:\s+(\d+)/;
        $AIX_prtconf_cache = $ncpus || 0;
    }
    return $AIX_prtconf_cache;
}

sub _getNumCpus_from_Test_Smoke_SysInfo {   # NOT TESTED
    return 0 if !_configModule('Test::Smoke::SysInfo');
    my $sysinfo = Test::Smoke::SysInfo->new();
    if (defined $sysinfo && defined $sysinfo->{_ncpu}) {
	# darwin: result might have format  "1 [2 cores]", see
	# www.cpantesters.org/cpan/report/db6067c4-9a66-11e0-91fb-39e97f60f2f7
	$sysinfo->{_ncpu} =~ s/\d+ \[(\d+) cores\]/$1/;
	return $sysinfo->{_ncpu};
    }
    return;
}

sub _getNumCpus_from_taskset {
    return 0 if $^O !~ /linux/i;
    my $taskset = _configExternalProgram('taskset');
    return 0 unless $taskset;

    # neither of these approaches are foolproof
    # 1. read affinity mask of PID 1
    # 2. try different affinity settings until it fails
    #
    # Will probably undercount if there are >64 cpus!

    my $result = qx($taskset -p 1 2> /dev/null);
    my ($mask) = $result =~ /:\s+(\w+)/;
    if ($mask) {
	my $n = 1+__hex($mask);
	return int(0.5+log($n)/log(2));
    }

    my $n = 0;
    do {
	my $cmd = sprintf '%s -p %x $$', $taskset, 1<<$n;
	my $result = qx($cmd >/dev/null 2>/dev/null);
	$n++;
    } while ($?==0 && $n < 64);

    if ($n > 1) {      # n==1 could be a false positive
	return $n;
    }

    $n = 0;
    while ( do { qx($taskset -pc $n $$ >/dev/null 2>/dev/null); $?==0 } ) {
	$n++;
	last if $n >= 256;
    }
    return 0;
}

######################################################################

# get affinity toolbox

sub _getAffinity_with_Win32API {
    my $opid = shift;
    return 0 if $^O ne 'MSWin32' && $^O ne 'cygwin';
    return 0 if !_configModule('Win32::API');

    my $pid = $opid;
    if ($^O eq 'cygwin') {
	$pid = __pid_to_winpid($opid);
	# return 0 if !defined $pid;
    }
    return 0 if !$pid;

    if ($pid > 0) {
	return _getProcessAffinity_with_Win32API($pid);
    } else { # $pid is a Windows pseudo-process (thread ID)
	return _getThreadAffinity_with_Win32API(-$pid);
    }
}

sub _getProcessAffinity_with_Win32API {
    my $pid = shift;
    my ($processMask, $systemMask, $processHandle) = (' ' x 16, ' ' x 16);

    # 0x0400 - PROCESS_QUERY_INFORMATION,
    # 0x1000 - PROCESS_QUERY_LIMITED_INFORMATION
    $processHandle = _win32api('OpenProcess',0x0400,0,$pid)
	|| _win32api('OpenProcess',0x1000,0,$pid);
    return 0 if ! $processHandle;
    return 0 if ! _win32api('GetProcessAffinityMask', $processHandle,
			    $processMask, $systemMask);

    my $mask = _unpack_Win32_mask($processMask);
    _debug("affinity with Win32::API: $mask");
    return $mask;
}

sub _getThreadAffinity_with_Win32API {
    my $thrid = shift;
    my ($processMask, $systemMask, $threadHandle) = (' 'x16, ' 'x16);

    # 0x0020: THREAD_QUERY_INFORMATION
    # 0x0400: THREAD_QUERY_LIMITED_INFORMATION
    # 0x0040: THREAD_SET_INFORMATION
    # 0x0200: THREAD_SET_LIMITED_INFORMATION
    $threadHandle = _win32api('OpenThread', 0x0060, 0, $thrid)
        || _win32api('OpenThread', 0x0600, 0, $thrid)
        || _win32api('OpenThread', 0x0020, 0, $thrid)
        || _win32api('OpenThread', 0x0400, 0, $thrid);
    if (! $threadHandle) {
	return 0;
    }

    # The Win32 API does not have a  GetThreadAffinityMask  function.
    # SetThreadAffinityMask  will return the previous affinity,
    # but then you have to call it again to restore the original affinity.
    # Also, SetThreadAffinityMask won't work if you don't have permission
    # to change the affinity.

    # SetThreadAffinityMask argument has to be compatible with
    # process affinity, so get process affinity.

    # XXX - this function only works for threads that are contained
    #       by the current process, and that should cover most use
    #       cases of this module. But how would you get the process
    #       id of an arbitrary Win32 thread?
    my $cpid = _win32api('GetCurrentProcessId');

    my $processHandle
	= _win32api('OpenProcess', 0x0400, 0, $cpid)
	|| _win32api('OpenProcess', 0x1000, 0, $cpid);

    local ($!,$^E) = (0,0);
    my $result = _win32api('GetProcessAffinityMask',
			   $processHandle, $processMask, $systemMask);

    if ($result == 0) {
	carp 'Could not determine process affinity ',
	        "(required to get thread affinity)\n";
	return 0;
    }

    $processMask = _unpack_Win32_mask($processMask);
    if ($processMask == 0) {
	carp 'Process affinity apparently set to zero, ',
        	"will not be able to set/get compatible thread affinity\n";
	return 0;
    }

    my $previous_affinity = _win32api('SetThreadAffinityMask',
				      $threadHandle, $processMask);

    if ($previous_affinity == 0) {
	Carp::cluck "Win32::API::SetThreadAffinityMask: $! / $^E\n";
	return 0;
    }

    # hope we can restore it.
    if ($previous_affinity != $processMask) {
	local $! = 0;
	local $^E = 0;
	my $new_affinity = _win32api('SetThreadAffinityMask',
				     $threadHandle, $previous_affinity);
	if ($new_affinity == 0) {

	    # http://msdn.microsoft.com/en-us/library/ms686247(v=vs.85).aspx:
	    #
	    # "If the thread affinity mask requests a processor that is not
	    # selected for the process affinity mask, the last error code
	    # is ERROR_INVALID_PARAMETER." ($! => 87)
	    #
	    # In MSWin32, the result of a fork() is a "pseudo-process",
	    # a Win32 thread that is still contained by its parent.
	    # So on MSWin32 a race condition exists where the parent
	    # process can choose an incompatible set of affinities
	    # during the execution of this function (basically, between
	    # the two calls to  SetThreadAffinityMask , above).

	    carp "Sys::CpuAffinity::_getThreadAffinity_with_Win32API:\n",
		    "set thread $thrid affinity to $processMask ",
		    "in order to retrieve\naffinity, but was unable to ",
		    "restore previous value:\nHandle=$threadHandle, ",
		    "Prev=$previous_affinity, Error=$! / $^E\n";
	}
    }
    return $previous_affinity;
}

sub _unpack_Win32_mask {
    # The Win32 GetProcessAffinityMask function takes
    # "PDWORD" arguments. We pass (arbitrary) integers for these
    # arguments, but on return they are changed to 1-4 bytes
    # representing a packed integer.

    my $packed = shift;
    return unpack "L", substr($packed . "\0\0\0\0", 0, 4);
}



sub _getAffinity_with_Win32Process {
    my $pid = shift;

    return 0 if $^O ne 'MSWin32' && $^O ne 'cygwin';
    return 0 if !_configModule('Win32::Process');
    return 0 if $pid < 0;  # pseudo-process / thread id

    if ($^O eq 'cygwin') {
	$pid = __pid_to_winpid($pid);
	return 0 if !defined $pid;
    }

    my ($processMask, $systemMask, $result, $processHandle) = (' 'x16, ' 'x16);
    if (! Win32::Process::Open($processHandle, $pid, 0)
	|| ref($processHandle) ne 'Win32::Process') {
	return 0;
    }
    if (! $processHandle->GetProcessAffinityMask($processMask, $systemMask)) {
	return 0;
    }
    _debug("affinity with Win32::Process: $processMask");
    return $processMask;
}

sub _getAffinity_with_taskset {
    my $pid = shift;
    return 0 if $^O ne 'linux';
    return 0 if !_configExternalProgram('taskset');
    my $taskset = _configExternalProgram('taskset');
    my $taskset_output = qx($taskset -p $pid 2> /dev/null);
    $taskset_output =~ s/\s+$//;
    _debug("taskset output: $taskset_output");
    return 0 if ! $taskset_output;
    my ($mask) = $taskset_output =~ /: (\S+)/;
    _debug("affinity with taskset: $mask");
    return __hex($mask);
}

sub __hex {
    # hex() method with better support for input > 0xffffffff
    my $mask = shift;
    if (length($mask) > 8) {
        my $mask2 = substr($mask,-8);
        my $mask1 = substr($mask,0,-8);
        return hex($mask2) + (__hex($mask1) << 32);
    } else {
        return hex($mask);
    }
}

sub _getAffinity_with_xs_sched_getaffinity {
    my $pid = shift;
    return 0 if !defined &xs_sched_getaffinity_get_affinity;
    my @mask;
    my $r = xs_sched_getaffinity_get_affinity($pid,\@mask,0);
    if ($r) {
        return _arrayToMask(@mask);
    }
    return;
}

sub _getAffinity_with_xs_DEBUG_sched_getaffinity {
    # to debug errors in xs_sched_getaffinity_get_affinity
    # during t/11-exercise-all.t
    my $pid = shift;
    return 0 if !defined &xs_sched_getaffinity_get_affinity;
    my @mask;
    my $r = xs_sched_getaffinity_get_affinity($pid,\@mask,1);
    if ($r) {
        return _arrayToMask(@mask);
    }
    return;
}

sub _getAffinity_with_pbind {
    my ($pid) = @_;
    return 0 if $^O !~ /solaris/i;
    return 0 if !_configExternalProgram('pbind');
    my $pbind = _configExternalProgram('pbind');
    my $cmd = "$pbind -q $pid";
    my $pbind_output = qx($cmd 2> /dev/null);
    if ($pbind_output eq '' && $? == 0) {

        # pid is unbound  or  pid is invalid?
        if (kill 'ZERO', $pid) {      
            $pbind_output = 'not bound';
        } else {
            warn "_getAffinity_with_pbind: could not signal unbound pid $pid";
            return;
        }
    }

    # possible output:
    #     process id $pid: $index
    #     process id $pid: not bound
    #     pid \d+ \w+ bound to proccessor(s) \d+ \d+ \d+.

    if ($pbind_output =~ /not bound/) {
        my $np = getNumCpus();
        if ($np > 0) {
            return (TWO ** $np) - 1;
        } else {
            carp '_getAffinity_with_pbind: ',
            "process $pid unbound but can't count processors\n";
            return TWO**32 - 1;
        }
    } elsif ($pbind_output =~ /: (\d+)/) {
        my $bound_processor = $1;
        return TWO ** $bound_processor;
    } elsif ($pbind_output =~ / bound to proces\S+\s+(.+)\.$/) {
        my $cpus = $1;
        if (!defined($cpus)) {
            return 0;
        }
        my @cpus = split /\s+/, $1;
        return _arrayToMask(@cpus);
    }
    return 0;
}

sub _getAffinity_with_psaix {
    my ($pid) = @_;
    return 0 if $^O !~ /aix/i;
    my $pscmd = _configExternalProgram('ps');
    return 0 if !$pscmd;
    our $AIX_HINTS;
    __set_aix_hints() unless $AIX_HINTS;

    my ($header,$data) = qx(ps -o THREAD -p $pid 2>/dev/null);
    return 0 unless $data;
    $header =~ s/^\s+//;
    my @h = split /\s+/, $header;
    my @d = split /\s+/, $data;
    my ($ipid) = grep { $h[$_] eq 'PID' } 0 .. $#h;
    my ($ibnd) = grep { $h[$_] eq 'BND' } 0 .. $#h;
    if ($ipid ne '' && $ibnd) {
        my $pidd = $d[$ipid];
        my $bndd = $d[$ibnd];
        if ($pidd == $pid) {
            $bndd =~ s/^\s+//;
            $bndd =~ s/\s+$//;
            if ($bndd eq '-') { # not bound
                return (TWO ** getNumCpus()) - 1;
            }
            if ($AIX_HINTS) {
                $bndd = $AIX_HINTS->{PROC_MAP}{$bndd} || $bndd;
            }
            return TWO ** $bndd;
        }
    }
    warn "ps\\aix: could not parse result:\n$header$data\n";
    return 0;
}

sub _getAffinity_with_xs_processor_affinity {
    my ($pid) = @_;
    return 0 if !defined &xs_getaffinity_processor_affinity;
    my @mask = ();
    my $ret = xs_getaffinity_processor_affinity($pid,\@mask);
    if ($ret == 0) {
        return 0;
    }
    _debug("affinity with getaffinity_xs_processor_affinity: @mask");
    return _arrayToMask(@mask);
}

sub _getAffinity_with_xs_processor_bind {
    my ($pid) = @_;
    return 0 if !defined &xs_getaffinity_processor_bind;
    return 0 if $^O !~ /solaris/i;
    return 0 if _is_solarisMultiCpuBinding();
    my @mask = ();
    my $ret = xs_getaffinity_processor_bind($pid,\@mask);
    if ($ret == 0) {
        return 0;
    }
    _debug("affinity with getaffinity_xs_processor_affinity: @mask");
    return _arrayToMask(@mask);
}

sub _getAffinity_with_BSD_Process_Affinity {
    my ($pid) = @_;
    return 0 if $^O !~ /bsd/i;
    return 0 if !_configModule('BSD::Process::Affinity','0.04');

    my $mask;
    if (! eval {
	my $affinity = BSD::Process::Affinity::get_process_mask($pid);
	$mask = $affinity->get;
        1 }  ) {
        # $MODULE{'BSD::Process::Affinity'} = 0
        _debug("error in _setAffinity_with_BSD_Process_Affinity: $@");
        return 0;
    }
    return $mask;
}

sub _getAffinity_with_cpuset {
    my ($pid) = @_;
    return 0 if $^O !~ /bsd/i;
    return 0 if !_configExternalProgram('cpuset');
    my $cpuset = _configExternalProgram('cpuset');
    my $cmd = "$cpuset -g -p $pid";
    my $cpuset_output = qx($cmd 2> /dev/null);

    # output format:
    #     pid nnnnn mask: i, j, k, ...

    $cpuset_output =~ s/.*:\s*//;
    my @cpus = split /\s*,\s*/, $cpuset_output;
    if (@cpus > 0) {
        return _arrayToMask(@cpus);
    }
    return 0;
}

sub _getAffinity_with_xs_freebsd_getaffinity {
    my $pid = shift;
    return 0 if !defined &xs_getaffinity_freebsd;
    my @mask = ();
    my $ret = xs_getaffinity_freebsd($pid,\@mask,0);
    if ($ret == 0) {
	return 0;
    }
    return _arrayToMask(@mask);
}

sub _getAffinity_with_xs_freebsd_getaffinity_debug {
    my $pid = shift;
    if (!defined &xs_getaffinity_freebsd) {
        if ($^O =~ /bsd/) {
            warn "\$^O=$^O, xs_getaffinity_freebsd not defined";
        }
        return;
    }
    my @mask = ();
    my $ret = xs_getaffinity_freebsd($pid,\@mask,1);
    warn "return value from xs_getaffinity_freebsd: $ret";
    if ($ret == 0) {
	return 0;
    }
    return _arrayToMask(@mask);
}

sub _getAffinity_with_xs_win32 {
    my ($opid) = @_;
    my $pid = $opid;
    if ($^O =~ /cygwin/) {
        $pid = __pid_to_winpid($opid);
        return 0 if !defined $pid;
    }

    if ($pid < 0) {
        return 0 if !defined &xs_win32_getAffinity_thread;
        return xs_win32_getAffinity_thread(-$pid);
    } elsif ($opid == $$) {
        if (defined &xs_win32_getAffinity_proc) {
            return xs_win32_getAffinity_proc($pid);
        } elsif (defined &xs_win32_getAffinity_thread) {
            return xs_win32_getAffinity_thread(0);
        } else {
        }
        return 0;
    } elsif (defined &xs_win32_getAffinity_proc) {
        return xs_win32_getAffinity_proc($pid);
    }
    return 0;
}

sub _getAffinity_with_xs_pthread_self_getaffinity {

    # new in 1.00, may only work when run as root

    my ($pid) = @_;
    return 0 if $^O !~ /bsd/;

    # this function can only be used on the calling process.
    return 0 if $pid != $$;
    return 0 if !defined &xs_pthread_self_getaffinity;
    my $z = xs_pthread_self_getaffinity(0);
    if ($z == 0) {

        # does $z==0 mean that the current thread is not bound (i.e.,
        # bound to all processors)? Or does it mean that the
        # pthread_getaffinity_np() call didn't do anything (but still
        # returned 0/success?)
        # Does pthread_getaffinity_np() always return 0 for normal users
        # and return non-zero for the super-user?

        # must use $_NUM_CPUS_CACHED || ... to pass test t/12#2
        my $np = $_NUM_CPUS_CACHED || getNumCpus();
        my $maxmask = TWO ** $np - 1;

        my $y = _setAffinity_with_xs_pthread_self_setaffinity($pid, $maxmask);
        if ($y) {
            return $maxmask;
        } else {
            return 0;
        }
    }
    return $z;
}

sub _getAffinity_with_xs_irix_sysmp {

    # new in 1.00, not tested

    my ($pid) = @_;
    return 0 if $^O !~ /irix/i;
    return 0 if !defined &xs_irix_sysmp_getaffinity;
    my $result = xs_irix_sysmp_getaffinity($pid);
    if ($result < -1) { # error
        return 0;
    } elsif ($result == -1) { # unrestricted
        my $np = getNumCpus();
        return TWO ** $np - 1;
    } else {  # restricted to a single processor.
        return TWO ** $result;
    }
}

######################################################################

# set affinity toolbox

sub _setAffinity_with_Win32API {
    my ($pid, $mask) = @_;
    return 0 if $^O ne 'MSWin32' && $^O ne 'cygwin';
    return 0 if !_configModule('Win32::API');

    # if $^O is 'cygwin', make sure you are passing the Windows pid,
    # using Cygwin::pid_to_winpid if necessary!

    if ($^O eq 'cygwin') {
        $pid = __pid_to_winpid($pid);
        if ($DEBUG) {
            print STDERR "winpid is $pid ($_[0])\n";
        }
        return 0 if !defined $pid;
    }

    if ($pid > 0) {
        my $processHandle;
        # 0x0200 - PROCESS_SET_INFORMATION
        $processHandle = _win32api('OpenProcess', 0x0200,0,$pid);
        if ($DEBUG) {
            print STDERR "process handle: $processHandle\n";
        }
        return 0 if ! $processHandle;
        my $result = _win32api('SetProcessAffinityMask', $processHandle, $mask);
        _debug("set affinity with Win32::API: $result");
        return $result;
    } else {
        # negative pid indicates Windows "pseudo-process", which should
        # use the Thread functions.
        # Thread access rights definitions:
        # 0x0020: THREAD_QUERY_INFORMATION
        # 0x0400: THREAD_QUERY_LIMITED_INFORMATION
        # 0x0040: THREAD_SET_INFORMATION
        # 0x0200: THREAD_SET_LIMITED_INFORMATION
        my $threadHandle;
        local $! = undef;
        local $^E = 0;
        $threadHandle = _win32api('OpenThread', 0x0060, 0, -$pid)
            || _win32api('OpenThread', 0x0600, 0, -$pid)
            || _win32api('OpenThread', 0x0040, 0, -$pid)
            || _win32api('OpenThread', 0x0200, 0, -$pid);
        return 0 if ! $threadHandle;
        my $previous_affinity = _win32api('SetThreadAffinityMask',
                                          $threadHandle, $mask);
        if ($previous_affinity == 0) {
            carp 'Sys::CpuAffinity::_setAffinity_with_Win32API: ',
                 "SetThreadAffinityMask call failed: $! / $^E\n";
        }
        return $previous_affinity;
    }
}

sub _setAffinity_with_Win32Process {
    my ($pid, $mask) = @_;
    return 0 if $^O ne 'MSWin32';   # cygwin? can't get it to work reliably
    return 0 if !_configModule('Win32::Process');

    if ($^O eq 'cygwin') {
        $pid = __pid_to_winpid($pid);

        if ($DEBUG) {
            print STDERR "cygwin pid $_[0] => winpid $pid\n";
        }
        return 0 if !defined $pid;
    }

    my $processHandle;
    if (! Win32::Process::Open($processHandle, $pid, 0)
        || ref($processHandle) ne 'Win32::Process') {
        return 0;
    }

    # Seg fault on Cygwin? We really prefer not to use it on Cygwin.
    local $SIG{SEGV} = 'IGNORE';

    # SetProcessAffinityMask: "only available on Windows NT"
    use Config;
    my $v = $Config{osvers};
    if ($^O eq 'MSWin32' && ($v < 3.51 || $v >= 6.0)) {
        if ($DEBUG) {
            print STDERR 'SetProcessAffinityMask ',
                         "not available on MSWin32 osvers $v?\n";
        }
        return 0;
    }
    # Don't trust Strawberry Perl $Config{osvers}. Win32::GetOSVersion
    # is more reliable if it is available.
    if (_configModule('Win32')) {
        if (!Win32::IsWinNT()) {
            if ($DEBUG) {
                print STDERR 'SetProcessorAffinityMask ',
                             "not available on MSWin32 OS Version $v\n";
            }
            return 0;
        }
    }

    my $result = $processHandle->SetProcessAffinityMask($mask);
    _debug("set affinity with Win32::Process: $result");
    return $result;
}

sub _setAffinity_with_taskset {
    my ($pid, $mask) = @_;
    return 0 if $^O ne 'linux' || !_configExternalProgram('taskset');
    my $cmd = sprintf '%s -p %x %d 2>&1',
		    _configExternalProgram('taskset'), $mask, $pid;

    my $taskset_output = qx($cmd 2> /dev/null);
    my $taskset_status = $?;

    if ($taskset_status) {
        _debug("taskset output: $taskset_output");
    }

    return $taskset_status == 0;
}

sub _setAffinity_with_xs_sched_setaffinity {
    my ($pid,$mask,$debug) = @_;
    return 0 if !defined &xs_sched_setaffinity_set_affinity;
    my @mask = _maskToArray($mask);
    return xs_sched_setaffinity_set_affinity($pid,\@mask,0+!!$debug);
}

sub _setAffinity_with_BSD_Process_Affinity {
    my ($pid,$mask) = @_;
    return 0 if $^O !~ /bsd/i;
    return 0 if !_configModule('BSD::Process::Affinity','0.04');

    if (not eval {
	my $affinity = BSD::Process::Affinity::get_process_mask($pid);
	$affinity->set($mask)->update;
        1}) {
        _debug("error in _setAffinity_with_BSD_Process_Affinity: $@");
        return 0;
    }
}

sub _getNumCpus_from_BSD_Process_Affinity {
    return 0 if $^O !~ /bsd/i;
    return 0 if !_configModule('BSD::Process::Affinity','0.04');
    my $n = BSD::Process::Affinity::current_set()->get;
    $n = log( $n+1.01 ) / log(2);
    return int($n);
}

sub _setAffinity_with_bindprocessor {
    my ($pid,$mask) = @_;
    return 0 if $^O !~ /aix/i;
    return 0 if $pid < 0;
    return 0 if !_configExternalProgram('bindprocessor');
    my $cmd = _configExternalProgram('bindprocessor');
    our $AIX_HINTS;
    __set_aix_hints($cmd) unless $AIX_HINTS;

    my @mask = _maskToArray($mask);
    my @cores = map { $AIX_HINTS->{PROCESSORS}[$_] } @mask;
    if (@cores == $AIX_HINTS->{NUM_CORES}) {
        return system("'$cmd' -u $pid") == 0;
    } elsif (@cores > 1) {
        warn "_setAffinity_with_bindprocessor: will only set one core on aix";
    }
    return system("'$cmd' $pid $cores[0]") == 0;
}

sub _setAffinity_with_pbind {
    my ($pid,$mask) = @_;
    return 0 if $^O !~ /solaris/i;
    return 0 if !_configExternalProgram('pbind');
    my $pbind = _configExternalProgram('pbind');
    my @mask = _maskToArray($mask);

    my $cpus = join ",", @mask;
    my $np = getNumCpus();
    my $c1;
    if (@mask == $np) {
        # unbind
        $c1 = system("'$pbind' -u $pid > /dev/null 2>&1");
    } else {
        $c1 = system("'$pbind' -b -c $cpus -s $pid > /dev/null 2>&1");
    }
    return !$c1;
}

sub _setAffinity_with_xs_processor_affinity {
    my ($pid,$mask) = @_;
    return 0 if $^O !~ /solaris/i;
    return 0 if !defined &xs_setaffinity_processor_affinity;
    my @mask = _maskToArray($mask);
    my $ret = xs_setaffinity_processor_affinity($pid, \@mask);
    if ($ret == 0) {
        return 0;
    }
    return 1;
}

sub _setAffinity_with_xs_processor_bind {
    my ($pid,$mask) = @_;
    return 0 if $^O !~ /solaris/i;
    return 0 if !defined &xs_setaffinity_processor_bind;
    return 0 if _is_solarisMultiCpuBinding();
    my @mask = _maskToArray($mask);
    my $ret = xs_setaffinity_processor_bind($pid, \@mask);
    if ($ret == 0) {
        return 0;
    }
    return 1;
}

sub _setAffinity_with_cpuset {
    my ($pid, $mask) = @_;
    return 0 if $^O !~ /bsd/i;
    return 0 if !_configExternalProgram('cpuset');

    my $lmask = join ',' => _maskToArray($mask);
    my $cmd = _configExternalProgram('cpuset') . " -l $lmask -p $pid";
    my $c1 = system "$cmd 2> /dev/null";
    return !$c1;
}

sub _setAffinity_with_xs_freebsd_setaffinity {
    my ($pid,$mask) = @_;
    return 0 if !defined &xs_setaffinity_freebsd;
    my @mask = _maskToArray($mask);
    return xs_setaffinity_freebsd($pid,\@mask);
}

sub _setAffinity_with_xs_win32 {
    my ($opid, $mask) = @_;

    my $pid = $opid;
    if ($^O =~ /cygwin/) {
        $pid = __pid_to_winpid($opid);
        return 0 if !defined $pid;
    }

    if ($pid < 0) {
        if (defined &xs_win32_setAffinity_thread) {
            my $r = xs_win32_setAffinity_thread(-$pid,$mask);
            _debug("xs_win32_setAffinity_thread -$pid,$mask => $r");
            return $r if $r;
        }
        return 0;
    } elsif ($opid == $$) {
        if (defined &xs_win32_setAffinity_proc) {
            _debug('xs_win32_setAffinity_proc $$');
            return xs_win32_setAffinity_proc($pid,$mask);
        }
        if ($^O eq 'cygwin' && defined &xs_win32_setAffinity_thread) {
            my $r = xs_win32_setAffinity_thread(0, $mask);
            return $r if $r;
        }
        return 0;
    } elsif (defined &xs_win32_setAffinity_proc) {
        my $r = xs_win32_setAffinity_proc($pid, $mask);
        _debug("xs_win32_setAffinity_proc +$pid,$mask => $r");
        return $r;
    }
    return 0;
}

sub _setAffinity_with_xs_pthread_self_setaffinity {

    # new in 1.00, may only work when run as root

    my ($pid, $mask) = @_;
    return 0 if $^O !~ /bsd/i;

    # this function only works with the calling process
    return 0 if $$ != $pid;
    return 0 if !defined &xs_pthread_self_setaffinity;
    return &xs_pthread_self_setaffinity($mask);
}

sub _setAffinity_with_xs_irix_sysmp {

    # new in 1.00, not tested

    my ($pid, $mask) = @_;

    return 0 if $^O !~ /irix/i;
    return 0 if !defined &xs_irix_sysmp_setaffinity;

    # Like the  pbind  function in solaris, Irix's sysmp function can only
    #   * bind a process to a single specific CPU, or
    #   * bind a process to all CPUs

    my @mask = _maskToArray($mask);

    my $np = getNumCpus();
    my $c1;
    if ($np > 0 && $mask + 1 == TWO ** $np) {
        return xs_irix_sysmp_setaffinity($pid, -1);
    } else {
        my $element = 0;
        return xs_irix_sysmp_setaffinity($pid, $mask[$element]);
    }
}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

sub _maskToArray {
    my ($mask) = @_;
    my @mask = ();
    my $i = 0;
    while ($mask > 0) {
        if ($mask & 1) {
            push @mask, $i;
        }
        $i++;
        $mask >>= 1;
    }
    return @mask;
}

sub _arrayToMask {
    my @procs = @_;
    my $mask = Math::BigInt->new(0);
    for my $proc (@procs) {
        $mask |= TWO ** $proc;
    }
    return $mask;
}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

sub __pid_to_winpid {
    my ($cygwinpid) = @_;
    if ($] >= 5.008 && defined &Cygwin::pid_to_winpid) {
        return Cygwin::pid_to_winpid($cygwinpid);
    } else {
        return __poor_mans_pid_to_winpid($cygwinpid);
    }
}

sub __poor_mans_pid_to_winpid {
    my ($cygwinpid) = @_;
    my @psw = qx(/usr/bin/ps -W 2> /dev/null);
    foreach my $psw (@psw) {
        $psw =~ s/^[A-Z\s]+//;
        my ($pid,$ppid,$pgid,$winpid) = split /\s+/, $psw;
        next if ! $pid;
        if ($pid == $cygwinpid) {
            return $winpid;
        }
    }
    warn "Could not resolve cygwin pid $cygwinpid into winpid.\n";
    return $cygwinpid;
}

######################################################################

# configuration code

sub _debug {
    my @msg = @_;
    return if !$DEBUG;
    print STDERR 'Sys::CpuAffinity: ',@msg,"\n";
    return;
}

our %MODULE = ();
our %PROGRAM = ();
our %INLINE_CODE = ();

sub _configModule {
    my $module = shift;
    my $version = shift || "";
    return $MODULE{$module} if defined $MODULE{$module};

    if (eval "require $module") {        ## no critic (StringyEval)
	my $v = eval "\$$module" . "::VERSION";
	if (!$@ && (!$version || $version <= $v)) {
	    _debug("module $module is available.");
	    return $MODULE{$module} = 1;
	} else {
	    _debug("module $module $version not available ($v)");
	    return $MODULE{$module} = 0;
	}
    } else {
        _debug("module $module $version not available: $@");
        return $MODULE{$module} = 0;
    }
}

our @PATH = ();

sub _configExternalProgram {
    my $program = shift;
    return $PROGRAM{$program} if defined $PROGRAM{$program};
    if (-x $program) {
        _debug("Program $program is available in $program");
        return $PROGRAM{$program} = $program;
    }

    if ($^O ne 'MSWin32') {
        my $which = qx(which $program 2> /dev/null);
        $which =~ s/\s+$//;

        if ($which =~ / not in /                # negative output on irix
            || $which =~ /no \Q$program\E in /  # negative output on solaris
            || $which =~ /Command not found/    # negative output on openbsd
            || ! -x $which                      # not executable, may be junk
            ) {

            $which = '';
        }
        if ($which) {
            _debug("Program $program is available in $which");
            return $PROGRAM{$program} = $which;
        }
    }

    # poor man's which
    if (@PATH == 0) {
        @PATH = split /:/, $ENV{PATH};
        push @PATH, split /;/, $ENV{PATH};
        push @PATH, '.';
        push @PATH, '/sbin', '/usr/sbin';
    }
    foreach my $dir (@PATH) {
        if (-x "$dir/$program") {
            _debug("Program $program is available in $dir/$program");
            return $PROGRAM{$program} = "$dir/$program";
        }
    }
    return $PROGRAM{$program} = 0;
}

######################################################################

# some Win32::API specific code

our %WIN32_API_SPECS
    = ('GetActiveProcessorCount' => [ 'kernel32',
                'DWORD GetActiveProcessorCount(WORD g)' ],
       'GetCurrentProcess' => [ 'kernel32',
                'HANDLE GetCurrentProcess()' ],
       'GetCurrentProcessId' => [ 'kernel32',
                'DWORD GetCurrentProcessId()' ],
       'GetCurrentThread' => [ 'kernel32',
                'HANDLE GetCurrentThread()' ],
       'GetCurrentThreadId' => [ 'kernel32',
                'int GetCurrentThreadId()' ],
       'GetLastError' => [ 'kernel32', 'DWORD GetLastError()' ],
       'GetModuleHandle' => [ 'kernel32', 'HMODULE GetModuleHandle(LPCTSTR n)' ],
       'GetPriorityClass' => [ 'kernel32',
                'DWORD GetPriorityClass(HANDLE h)' ],
       'GetProcAddress' => [ 'kernel32',
			   'DWORD GetProcAddress(HINSTANCE a,LPCTSTR b)' ],
#			   'DWORD GetProcAddress(HINSTANCE a,LPCWSTR b)' ],
       'GetProcessAffinityMask' => [ 'kernel32',
                'BOOL GetProcessAffinityMask(HANDLE h,PDWORD a,PDWORD b)' ],
       'GetThreadPriority' => [ 'kernel32',
                'int GetThreadPriority(HANDLE h)' ],
       'IsWow64Process' => [ 'kernel32', 'BOOL IsWow64Process(HANDLE h,PBOOL b)' ],
       'OpenProcess' => [ 'kernel32',
                'HANDLE OpenProcess(DWORD a,BOOL b,DWORD c)' ],
       'OpenThread' => [ 'kernel32',
                'HANDLE OpenThread(DWORD a,BOOL b,DWORD c)' ],
       'SetProcessAffinityMask' => [ 'kernel32',
                'BOOL SetProcessAffinityMask(HANDLE h,DWORD m)' ],
       'SetThreadAffinityMask' => [ 'kernel32',
                'DWORD SetThreadAffinityMask(HANDLE h,DWORD d)' ],
       'SetThreadPriority' => [ 'kernel32',
                'BOOL SetThreadPriority(HANDLE h,int n)' ],
       'TerminateThread' => [ 'kernel32',
                'BOOL TerminateThread(HANDLE h,DWORD x)' ],
    );
our %WIN32_API_SPECS_ 
    = map { $_ => $WIN32_API_SPECS{$_}[1] } keys %WIN32_API_SPECS;

sub _win32api {                 ## no critic (RequireArgUnpacking)
                                ## (we want spooky action-at-a-distance)
    my $function = shift;
    return if !_configModule('Win32::API');
    if (!defined $WIN32API{$function}) {
        __load_win32api_function($function);
    }
    return if !defined($WIN32API{$function}) || $WIN32API{$function} == 0;

    return $WIN32API{$function}->Call(@_);
}

sub __load_win32api_function {
    my $function = shift;
    my $spec = $WIN32_API_SPECS{$function};
    if (!defined $spec) {
        croak "Sys::CpuAffinity: bad Win32::API function request: $function\n";
    }

    local ($!, $^E) = (0, 0);

    my $spec_ = $WIN32_API_SPECS_{$function};
    $WIN32API{$function} = Win32::API->new('kernel32',$spec_);

    if ($!) {
        carp 'Sys::CpuAffinity: ',
            "error initializing Win32::API function $function: $! / $^E\n";
        $WIN32API{$function} = 0;
    }
    return;
}

######################################################################

1; # End of Sys::CpuAffinity

__END__

######################################################################

=head1 NAME

Sys::CpuAffinity - Set CPU affinity for processes

=head1 VERSION

Version 1.13_03

=head1 SYNOPSIS

    use Sys::CpuAffinity;

    $num_cpus = Sys::CpuAffinity::getNumCpus();

    $mask = 1 | 4 | 8 | 16;   # prefer CPU's # 0, 2, 3, 4
    $success = Sys::CpuAffinity::setAffinity($pid,$mask);
    $success = Sys::CpuAffinity::setAffinity($pid, \@preferred_cpus);

    $mask = Sys::CpuAffinity::getAffinity($pid);
    @cpus = Sys::CpuAffinity::getAffinity($pid);

=head1 DESCRIPTION

The details of getting and setting process CPU affinities
varies greatly from system to system. Even among the different
flavors of Unix there is very little in the way of a common
interface to CPU affinities. The existing tools and libraries
for setting CPU affinities are not very standardized, so
that a technique for setting CPU affinities on one system
may not work on another system with the same architecture.

This module seeks to do one thing and do it well:
manipulate CPU affinities through a common interface
on as many systems as possible, by any means necessary.

The module is composed of several subroutines, each one
implementing a different technique to perform a CPU affinity
operation. A technique might try to import a Perl module,
run an external program that might be installed on your system,
or invoke some C code to access your system libraries.
Usually, a technique is applicable to only a single
or small group of operating systems, and on any particular
system, most of the techniques would fail.
Regardless of your particular system and configuration,
it is hoped that at least one of the techniques will work
and you will be able to get and set the CPU affinities of
your processes.

=head1 DEPENDENCIES

No modules are required by Sys::CpuAffinity, but there are
techniques for manipulating CPU affinities in other
existing modules, and Sys::CpuAffinity will use these
modules if they are available:

    Win32::API, Win32::Process   [MSWin32, cygwin]
    BSD::Process::Affinity       [FreeBSD]

=head1 CONFIGURATION AND ENVIRONMENT

It is important that your C<PATH> variable is set correctly so that
this module can find any external programs on your system that can
help it to manipulate CPU affinities (for example, C<taskset> on Linux,
C<cpuset> on FreeBSD).

If C<$ENV{DEBUG}> is set to a true value, this module will produce
some output that may or may not be good for debugging.

=head1 SUPPORTED SYSTEMS

The techniques for manipulating CPU affinities for Windows
(including Cygwin) and Linux have been refined and tested
pretty well. Some techniques applicable to BSD systems
(particularly FreeBSD) and Solaris have been tested a little bit.
The hope is that this module will include more techniques for
more systems in future releases. See the L</"NOTE TO DEVELOPERS">
below for information about how you can help.

MacOS, OpenBSD are explicitly not supported,
as there does not appear to be any public interface for specifying
the CPU affinity of a process directly on those platforms.

On NetBSD, getting and setting CPU affinity is supported B<only for
the calling process>, and, AFAICT, B<only when run as the super-user>.
Which is to say, you can do this:

    use Sys::CpuAffinity;
    # run this process on CPUs 0, 1, 3
    Sys::CpuAffinity::setAffinity($$, [0, 1, 3]);

but not this:

    use Sys::CpuAffinity;
    $pid = `ps | grep emacs` + 0;
    # run another process on CPUs 0, 1, 3
    Sys::CpuAffinity::setAffinity($pid, [0, 1, 3]);

=head1 SUBROUTINES/METHODS

=over 4

=item C<$bitmask = Sys::CpuAffinity::getAffinity($pid)>

=item C<@preferred_cpus = Sys::CpuAffinity::getAffinity($pid)>

Retrieves the current CPU affinity for the process
with the specified process ID.
In scalar context, returns a bit-mask of the CPUs that the
process has affinity for, with the least significant bit
denoting CPU #0. The return value is actually a
L<Math::BigInt> value, so it can store a bit mask on systems
with an arbitrarily high number of CPUs.

In list context, returns a list of integers indicating the
indices of the CPU that the process has affinity for.

So for example, if a process in an 8 core machine
had affinity for cores # 2, 6, and 7, then
in scalar context, C<getAffinity()> would return

    (1 << 2) | (1 << 6) | (1 << 7) ==> 196

and in list context, it would return

    (2, 6, 7)

A return value of 0 or C<undef> indicates an error
such as an invalid process ID.

=back

=over 4

=item C<$success = Sys::CpuAffinity::setAffinity($pid, $bitmask)>

=item C<$success = Sys::CpuAffinity::setAffinity($pid, \@preferred_cpus)>

Sets the CPU affinity of a process to the specified processors.
First argument is the process ID. The second argument is either
a bitmask of the desired processors to assign to the PID, or an
array reference with the index values of processors to assign to
the PID.

    # two ways to assign to CPU #'s 1 and 4:
    Sys::CpuAffinity::setAffinity($pid, 0x12); # 0x12 = (1<<1) | (1<<4)
    Sys::CpuAffinity::setAffinity($pid, [1,4]);

As a special case, using a C<$bitmask> value of C<-1> will clear
the CPU affinities of a process -- setting the affinity to all
available processors.

On some platforms, notably AIX and Irix, it is only possible to
bind a process to a single CPU. If the processor mask argument to
C<setAffinity> specifies more than one processor (but less than the
total number of processors in your system), then this function might
only bind the process to one of the specified processors.

=back

=over 4

=item C<$ncpu = Sys::CpuAffinity::getNumCpus()>

Returns the module's best guess about the number of
processors on this system.

=back

=head1 BUGS AND LIMITATIONS

This module may not work or produce undefined results on
systems with more than 32 CPUs, though support for these
larger systems has improved with v1.07.

For some systems (darwin, OpenBSD) there are no currently
known ways to affect process CPU affinity.

On some systems (aix, irix, Solaris E<lt> 11.2), it may
only be possible to bind a process to either a single
processor or to all processors, and not to an arbitrary
subset of processors.

On NetBSD, it is believed that it is only possible to 
get/set the CPU affinity on the calling process, and only 
by the root user.

On Windows, updating CPU affinity of a thread can generally
only be done on threads within the calling process, but not
with an arbitrary Win32 thread.

Please report any bugs or feature requests to
C<bug-sys-cpuaffinity at rt.cpan.org>, or through
the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sys-CpuAffinity>.
I will be notified, and then you'll automatically be notified of
progress on your bug as I make changes.

=head1 INCOMPATIBILITIES

None known, but they are likely to arise as this module makes a
lot of assumptions about how to provide input and interpret output
for many different system utilities on many different platforms.
Please report a bug if you suspect this module of misusing any
system utilities.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Sys::CpuAffinity

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Sys-CpuAffinity>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Sys-CpuAffinity>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Sys-CpuAffinity>

=item * Search CPAN

L<http://search.cpan.org/dist/Sys-CpuAffinity/>

=back

=head1 NOTE TO DEVELOPERS

This module seeks to work for as many systems in as many
configurations as possible. If you know of a tool, a function,
a technique to set CPU affinities on a system -- any system,
-- then let's include it in this module.

Feel free to submit code through this module's request tracker:

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Sys-CpuAffinity>

or directly to me at C<< <mob at cpan.org> >> and it will
be included in the next release.

=head1 ACKNOWLEDGEMENTS

L<BSD::Process::Affinity|BSD::Process::Affinity> for demonstrating
how to get/set affinities on BSD systems.

L<Test::Smoke::SysInfo|Test::Smoke::SysInfo> has some fairly portable
code for detecting the number of processors.

L<http://devio.us/> provided a free OpenBSD account that allowed
this module to be tested on that platform.

=head1 AUTHOR

Marty O'Brien, C<< <mob at cpan.org> >>

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2010-2020 Marty O'Brien.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut








######################################################################

Notes and to do list:

Why worry about CPU affinity? See
http://www.ibm.com/developerworks/linux/library/l-affinity.html?ca=dgr-lnxw41Affinity
Other reasons are:
    bind expensive processes to subset of CPUs, leaving at least
    one CPU for other tasks or other users

See http://www.ibm.com/developerworks/aix/library/au-processinfinity.html
for hints about cpu affinity on AIX

From v0.90, test to get num CPUs failed on Irix.

Rumors of cpu affinity on other systems:
    BSD:  pthread_setaffinity_np(), pthread_getaffinity_np()
          copy XS code from BSD::Resource::Affinity
          FreeBSD:  /cpuset, cpuset_setaffinity(), cpuset_getaffinity()
          NetBSD:   /psrset
    Irix: /dplace, cpusetXXX() methods (with -lcpuset)
          pthread_setrunon_np(int), pthread_getrunon_np(int*) to affine 
              the current thread with a single CPU.
          sysmp(MP_MUSTRUN_PID,cpu_id,process_id)
          sysmp(MP_RUNANYWHERE_PID,process_id)
          sysmp(MP_GETMUSTRUN_PID,process_id)
              for binding a process to a single specific processor
    Solaris:  /pbind, /psrset, processor_bind(), pset_bind()

    using /psrset in this module is not recommended
      * processor sets are *exclusive*. processors assigned to a processor set
        can only be used by processes assigned to that set
      * processor sets can only be changed by sysadmin
      * /cpuset in Irix has these same issues (different from /cpuset command
            in FreeBSD)

    Solaris:  Solaris::Lgrp module
        lgrp_affinity_set(P_PID,$pid,$lgrp,LGRP_AFF_xxx)
        lgrp_affinity_get(P_PID,$pid,$lgrp)
        affinity_get

    AIX:  /bindprocessor, bindprocessor() in <sys/processor.h>
        bindprocessor -q     lists virtual processors
        bindprocessor -s 0   lists available cores
        lsdev -Cc processor  lists available cores, consistent with bind... -s 0
 
        bindprocessor -u pid    unbind process pid

    MacOS: thread_policy_set(),thread_policy_get() in <mach/thread_policy.h>

        In MacOS it is possible to assign threads to the same
        processor, but generally not to assign them to any particular
        processor. MacOS is totally unsupported for now.

    DragonflyBSD: all CPAN tests are from single-core systems, so who knows
        whether any of this code works on that platform.

    There also hasn't been a CPAN tester with AIX yet.


how to find the number of processors:
    AIX:  sysconf(_SC_NPROCESSORS_CONF), sysconf(_SC_NPROCESSORS_ONLN)
          prtconf | grep "Number Of Processors:" | cut -d: -f2
    Solaris:   processor_info(), p_online()
    MacOS:     hwprefs cpu_count, system_profiler | grep Cores: | cut -d: -f2
               do something with `sysctl -a`
    AIX:       prtconf
               solaris also has prtconf, but don't think it has cpu data
    BSD also has `sysctl`, they tell me
        AIX:   `smtctl | grep "Bind processor "`  ... not reliable
        AIX:   `lsdev -Cc processor`  -- all processors
        AIX:    `bindprocessor -q`    -- all shares of processors

Some systems have a concept of "processor groups" or "cpu sets"
that can we could either exploit or be exploited by

Some systems have a concept of "strong" affinity and "weak" affinity.
Where the distinction is important, let's use "strong" affinity
by default.

Some systems have a concept of the maximum number of processors that
they can suppport.

Currently (0.91-1.04), constant parameters to Win32 API functions are
hard coded, not extracted from the local header files.

##########################################

Issues in 1.02-1.04

   1. darwin:  hwprefs  and  sysctl  give different results?
     www.cpantesters.org/cpan/report/3982d2fa-9c2a-11e0-a04e-9d9517dc0771
   2. openbsd: dmesg_bsd  and  sysctl  give different results?
     www.cpantesters.org/cpan/report/84d41dda-9942-11e0-a324-58f41aecacb6
     www.cpantesters.org/cpan/report/0c6e981c-a2dd-11e0-a324-58f41aecacb6
   3. linux: /usr/bin/taskset available but still cannot count CPUs? (x16)
       /www.cpantesters.org/cpan/report/92ab9df8-a6fc-11e0-829d-5250641c9bbe
      xs_sched_getaffinity keeps segfaulting (x4)
   4. getNumCpus_from_Win32API_System_Info: garbage result on WOW64 systems

Issues in 1.09
   1. linux might have more than 64 cpus, so xs_sched_getaffinity_get_affinity
      and xs_sched_setaffinity_set_affinity should also work in AV space; see
      Linux::CPUAffinity
   2. fix setaffinity_processor_bind.xs, getaffinity_processor_bind.xs
      for solaris
   3. Not tested on Windows 10
   4. Solaris XS. processor_bind usage matches old processor_bind man page,
      not current page, doesn't look like you can use processor_bind() on
      more than one core.
      Solaris 11.2 has "Multi-CPU Binding" and we may need to distinguish
      between systems that have it and systems that don't.
      blogs.oracle.com/observatory/entry/multi_cpu_binding_mcb:
         ``[MCB] is available through a new API called "processor_affinity(2)"''