1
0

util.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright 2013-2014 Wincent Colaiuta. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are met:
  5. #
  6. # 1. Redistributions of source code must retain the above copyright notice,
  7. # this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright notice,
  9. # this list of conditions and the following disclaimer in the documentation
  10. # and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  13. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  16. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  17. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  18. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  19. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  20. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  21. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  22. # POSSIBILITY OF SUCH DAMAGE.
  23. require 'rbconfig'
  24. module CommandT
  25. module Util
  26. class << self
  27. def processor_count
  28. @processor_count ||= begin
  29. count = processor_count!
  30. count = 1 if count < 1 # sanity check
  31. count = 32 if count > 32 # sanity check
  32. count
  33. end
  34. end
  35. private
  36. # This method derived from:
  37. #
  38. # https://github.com/grosser/parallel/blob/d11e4a3c8c1a/lib/parallel.rb
  39. #
  40. # Number of processors seen by the OS and used for process scheduling.
  41. #
  42. # * AIX: /usr/sbin/pmcycles (AIX 5+), /usr/sbin/lsdev
  43. # * BSD: /sbin/sysctl
  44. # * Cygwin: /proc/cpuinfo
  45. # * Darwin: /usr/bin/hwprefs, /usr/sbin/sysctl
  46. # * HP-UX: /usr/sbin/ioscan
  47. # * IRIX: /usr/sbin/sysconf
  48. # * Linux: /proc/cpuinfo
  49. # * Minix 3+: /proc/cpuinfo
  50. # * Solaris: /usr/sbin/psrinfo
  51. # * Tru64 UNIX: /usr/sbin/psrinfo
  52. # * UnixWare: /usr/sbin/psrinfo
  53. #
  54. # Copyright (C) 2013 Michael Grosser <michael@grosser.it>
  55. #
  56. # Permission is hereby granted, free of charge, to any person obtaining
  57. # a copy of this software and associated documentation files (the
  58. # "Software"), to deal in the Software without restriction, including
  59. # without limitation the rights to use, copy, modify, merge, publish,
  60. # distribute, sublicense, and/or sell copies of the Software, and to
  61. # permit persons to whom the Software is furnished to do so, subject to
  62. # the following conditions:
  63. #
  64. # The above copyright notice and this permission notice shall be
  65. # included in all copies or substantial portions of the Software.
  66. #
  67. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  68. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  69. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  70. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  71. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  72. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  73. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  74. #
  75. def processor_count!
  76. os_name = RbConfig::CONFIG['target_os']
  77. if os_name =~ /mingw|mswin/
  78. require 'win32ole'
  79. result = WIN32OLE.connect('winmgmts://').ExecQuery(
  80. 'select NumberOfLogicalProcessors from Win32_Processor')
  81. result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+)
  82. elsif File.readable?('/proc/cpuinfo')
  83. IO.read('/proc/cpuinfo').scan(/^processor/).size
  84. elsif File.executable?('/usr/bin/hwprefs')
  85. IO.popen(%w[/usr/bin/hwprefs thread_count]).read.to_i
  86. elsif File.executable?('/usr/sbin/psrinfo')
  87. IO.popen('/usr/sbin/psrinfo').read.scan(/^.*on-*line/).size
  88. elsif File.executable?('/usr/sbin/ioscan')
  89. IO.popen(%w[/usr/sbin/ioscan -kC processor]) do |out|
  90. out.read.scan(/^.*processor/).size
  91. end
  92. elsif File.executable?('/usr/sbin/pmcycles')
  93. IO.popen(%w[/usr/sbin/pmcycles -m]).read.count("\n")
  94. elsif File.executable?('/usr/sbin/lsdev')
  95. IO.popen(%w[/usr/sbin/lsdev -Cc processor -S 1]).read.count("\n")
  96. elsif File.executable?('/usr/sbin/sysconf') and os_name =~ /irix/i
  97. IO.popen(%w[/usr/sbin/sysconf NPROC_ONLN]).read.to_i
  98. elsif File.executable?('/usr/sbin/sysctl')
  99. IO.popen(%w[/usr/sbin/sysctl -n hw.ncpu]).read.to_i
  100. elsif File.executable?('/sbin/sysctl')
  101. IO.popen(%w[/sbin/sysctl -n hw.ncpu]).read.to_i
  102. else # unknown platform
  103. 1
  104. end
  105. rescue
  106. 1
  107. end
  108. end
  109. end # module Util
  110. end # module commandT