PHP 8.2.31
Preview: cpu_counter.rb Size: 2.83 KB
//proc/thread-self/root/opt/alt/ruby22/lib64/ruby/2.2.0/rake/cpu_counter.rb

module Rake

  # Based on a script at:
  #   http://stackoverflow.com/questions/891537/ruby-detect-number-of-cpus-installed
  class CpuCounter # :nodoc: all
    def self.count
      new.count_with_default
    end

    def count_with_default(default=4)
      count || default
    rescue StandardError
      default
    end

    begin
      require 'etc'
    rescue LoadError
    else
      if Etc.respond_to?(:nprocessors)
        def count
          return Etc.nprocessors
        end
      end
    end
  end
end

unless Rake::CpuCounter.method_defined?(:count)
  Rake::CpuCounter.class_eval <<-'end;', __FILE__, __LINE__+1
    require 'rbconfig'

    # TODO: replace with IO.popen using array-style arguments in Rake 11
    require 'open3'

    def count
      if defined?(Java::Java)
        count_via_java_runtime
      else
        case RbConfig::CONFIG['host_os']
        when /darwin9/
          count_via_hwprefs_cpu_count
        when /darwin/
          count_via_hwprefs_thread_count || count_via_sysctl
        when /linux/
          count_via_cpuinfo
        when /bsd/
          count_via_sysctl
        when /mswin|mingw/
          count_via_win32
        else
          # Try everything
          count_via_win32 ||
            count_via_sysctl ||
            count_via_hwprefs_thread_count ||
            count_via_hwprefs_cpu_count ||
            count_via_cpuinfo
        end
      end
    end

    def count_via_java_runtime
      Java::Java.lang.Runtime.getRuntime.availableProcessors
    rescue StandardError
      nil
    end

    def count_via_win32
      require 'win32ole'
      wmi = WIN32OLE.connect("winmgmts://")
      cpu = wmi.ExecQuery("select NumberOfCores from Win32_Processor") # TODO count hyper-threaded in this
      cpu.to_enum.first.NumberOfCores
    rescue StandardError, LoadError
      nil
    end

    def count_via_cpuinfo
      open('/proc/cpuinfo') { |f| f.readlines }.grep(/processor/).size
    rescue StandardError
      nil
    end

    def count_via_hwprefs_thread_count
      run 'hwprefs', 'thread_count'
    end

    def count_via_hwprefs_cpu_count
      run 'hwprefs', 'cpu_count'
    end

    def count_via_sysctl
      run 'sysctl', '-n', 'hw.ncpu'
    end

    def run(command, *args)
      cmd = resolve_command(command)
      if cmd
        Open3.popen3 cmd, *args do |inn, out, err,|
          inn.close
          err.read
          out.read.to_i
        end
      else
        nil
      end
    end

    def resolve_command(command)
      look_for_command("/usr/sbin", command) ||
        look_for_command("/sbin", command) ||
        in_path_command(command)
    end

    def look_for_command(dir, command)
      path = File.join(dir, command)
      File.exist?(path) ? path : nil
    end

    def in_path_command(command)
      Open3.popen3 'which', command do |_, out,|
        out.eof? ? nil : command
      end
    end
  end;
end

Directory Contents

Dirs: 4 × Files: 45

Name Size Perms Modified Actions
contrib DIR
- drwxr-xr-x 2024-03-03 22:53:17
Edit Download
ext DIR
- drwxr-xr-x 2024-03-03 22:53:17
Edit Download
lib DIR
- drwxr-xr-x 2023-07-26 14:31:55
Edit Download
loaders DIR
- drwxr-xr-x 2024-03-03 22:53:17
Edit Download
3.17 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
23.14 KB lrw-r--r-- 2014-12-06 00:22:51
Edit Download
868 B lrw-r--r-- 2014-08-16 03:56:17
Edit Download
1.93 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
418 B lrw-r--r-- 2014-09-06 09:31:37
Edit Download
2.83 KB lrw-r--r-- 2014-11-05 05:19:15
Edit Download
235 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
5.64 KB lrw-r--r-- 2014-12-06 00:22:51
Edit Download
340 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
670 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
12.13 KB lrw-r--r-- 2014-09-06 09:31:37
Edit Download
1.26 KB lrw-r--r-- 2014-11-25 07:03:36
Edit Download
3.66 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
4.05 KB lrw-r--r-- 2013-10-11 21:35:01
Edit Download
144 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
1.16 KB lrw-r--r-- 2014-08-27 12:21:41
Edit Download
431 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
265 B lrw-r--r-- 2014-11-25 07:03:58
Edit Download
2.27 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
315 B lrw-r--r-- 2012-11-15 21:59:37
Edit Download
673 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
5.28 KB lrw-r--r-- 2014-11-25 07:03:36
Edit Download
53 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
351 B lrw-r--r-- 2012-11-15 22:32:34
Edit Download
364 B lrw-r--r-- 2012-11-15 22:01:58
Edit Download
2.28 KB lrw-r--r-- 2013-10-11 21:35:01
Edit Download
375 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
770 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
341 B lrw-r--r-- 2011-06-28 02:45:29
Edit Download
153 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
897 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
353 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
594 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
882 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
11.02 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
618 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
2.15 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
119 B lrw-r--r-- 2011-06-23 22:11:55
Edit Download
8.84 KB lrw-r--r-- 2014-11-25 07:03:36
Edit Download
5.55 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
1.11 KB lrw-r--r-- 2013-10-11 21:35:01
Edit Download
4.78 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download
543 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
156 B lrw-r--r-- 2014-07-15 03:07:37
Edit Download
1.55 KB lrw-r--r-- 2014-07-15 03:07:37
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).