Python psutil.cpu_count() Examples

The following are 30 code examples of psutil.cpu_count(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module psutil , or try the search function .
Example #1
Source File: system_status.py    From Paradrop with Apache License 2.0 12 votes vote down vote up
def getSystemInfo(cls):
        system = {
            'boot_time': psutil.boot_time(),
            'cpu_count': psutil.cpu_count(),
            'cpu_stats': psutil.cpu_stats().__dict__,
            'cpu_times': [k.__dict__ for k in psutil.cpu_times(percpu=True)],
            'disk_io_counters': psutil.disk_io_counters().__dict__,
            'disk_usage': [],
            'net_io_counters': psutil.net_io_counters().__dict__,
            'swap_memory': psutil.swap_memory().__dict__,
            'virtual_memory': psutil.virtual_memory().__dict__
        }

        partitions = psutil.disk_partitions()
        for p in partitions:
            if p.mountpoint in cls.INCLUDED_PARTITIONS:
                usage = psutil.disk_usage(p.mountpoint)
                system['disk_usage'].append({
                    'mountpoint': p.mountpoint,
                    'total': usage.total,
                    'used': usage.used
                })

        return system 
Example #2
Source File: server.py    From MoePhoto with Apache License 2.0 6 votes vote down vote up
def getSystemInfo(info):
  import readgpu
  cuda, cudnn = readgpu.getCudaVersion()
  info.update({
    'cpu_count_phy': psutil.cpu_count(logical=False),
    'cpu_count_log': psutil.cpu_count(logical=True),
    'cpu_freq': psutil.cpu_freq().max,
    'disk_total': psutil.disk_usage(cwd).total // 2**20,
    'mem_total': psutil.virtual_memory().total // 2**20,
    'python': readgpu.getPythonVersion(),
    'torch': readgpu.getTorchVersion(),
    'cuda': cuda,
    'cudnn': cudnn,
    'gpus': readgpu.getGPUProperties()
  })
  readgpu.uninstall()
  del readgpu
  return info 
Example #3
Source File: worker_factory.py    From garage with MIT License 6 votes vote down vote up
def __init__(
            self,
            *,  # Require passing by keyword.
            seed,
            max_path_length,
            n_workers=psutil.cpu_count(logical=False),
            worker_class=DefaultWorker,
            worker_args=None):
        self.n_workers = n_workers
        self._seed = seed
        self._max_path_length = max_path_length
        self._worker_class = worker_class
        if worker_args is None:
            self._worker_args = {}
        else:
            self._worker_args = worker_args 
Example #4
Source File: test_system.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_cpu_freq(self):
        def check_ls(ls):
            for nt in ls:
                self.assertEqual(nt._fields, ('current', 'min', 'max'))
                self.assertLessEqual(nt.current, nt.max)
                for name in nt._fields:
                    value = getattr(nt, name)
                    self.assertIsInstance(value, (int, long, float))
                    self.assertGreaterEqual(value, 0)

        ls = psutil.cpu_freq(percpu=True)
        if TRAVIS and not ls:
            return

        assert ls, ls
        check_ls([psutil.cpu_freq(percpu=False)])

        if LINUX:
            self.assertEqual(len(ls), psutil.cpu_count()) 
Example #5
Source File: test_bsd.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_sensors_temperatures_against_sysctl(self):
        num_cpus = psutil.cpu_count(True)
        for cpu in range(num_cpus):
            sensor = "dev.cpu.%s.temperature" % cpu
            # sysctl returns a string in the format 46.0C
            try:
                sysctl_result = int(float(sysctl(sensor)[:-1]))
            except RuntimeError:
                self.skipTest("temperatures not supported by kernel")
            self.assertAlmostEqual(
                psutil.sensors_temperatures()["coretemp"][cpu].current,
                sysctl_result, delta=10)

            sensor = "dev.cpu.%s.coretemp.tjmax" % cpu
            sysctl_result = int(float(sysctl(sensor)[:-1]))
            self.assertEqual(
                psutil.sensors_temperatures()["coretemp"][cpu].high,
                sysctl_result)

# =====================================================================
# --- OpenBSD
# ===================================================================== 
Example #6
Source File: test_system.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_cpu_count(self):
        logical = psutil.cpu_count()
        self.assertEqual(logical, len(psutil.cpu_times(percpu=True)))
        self.assertGreaterEqual(logical, 1)
        #
        if os.path.exists("/proc/cpuinfo"):
            with open("/proc/cpuinfo") as fd:
                cpuinfo_data = fd.read()
            if "physical id" not in cpuinfo_data:
                raise unittest.SkipTest("cpuinfo doesn't include physical id")
        physical = psutil.cpu_count(logical=False)
        if WINDOWS and sys.getwindowsversion()[:2] <= (6, 1):  # <= Vista
            self.assertIsNone(physical)
        else:
            self.assertGreaterEqual(physical, 1)
            self.assertGreaterEqual(logical, physical) 
Example #7
Source File: test_invocation.py    From pytest-mp with MIT License 6 votes vote down vote up
def test_ini_without_cmdline(testdir, ini_content, mp, num_processes):
    """Confirms that .ini values are used to determine mp run options"""
    testdir.makeini(ini_content)

    if mp:
        if num_processes != 0:
            num_processes = num_processes or cpu_count
    else:
        num_processes = 0

    testdir.makepyfile("""
        import pytest
        import time

        def test_mp(mp_use_mp):  # mp_use_mp is pytest-mp helper fixture
            assert mp_use_mp == {}

        def test_num_processes(mp_num_processes):  # mp_num_processes is pytest-mp helper fixture
            assert mp_num_processes == {}

    """.format(mp, num_processes))

    result = testdir.runpytest()
    result.stdout.re_match_lines(['.*2 passed.*in.*seconds.*'])
    assert result.ret == 0 
Example #8
Source File: test_system.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_cpu_freq(self):
        def check_ls(ls):
            for nt in ls:
                self.assertEqual(nt._fields, ('current', 'min', 'max'))
                if nt.max != 0.0:
                    self.assertLessEqual(nt.current, nt.max)
                for name in nt._fields:
                    value = getattr(nt, name)
                    self.assertIsInstance(value, (int, long, float))
                    self.assertGreaterEqual(value, 0)

        ls = psutil.cpu_freq(percpu=True)
        if TRAVIS and not ls:
            raise self.skipTest("skipped on Travis")
        if FREEBSD and not ls:
            raise self.skipTest("returns empty list on FreeBSD")

        assert ls, ls
        check_ls([psutil.cpu_freq(percpu=False)])

        if LINUX:
            self.assertEqual(len(ls), psutil.cpu_count()) 
Example #9
Source File: emergency.py    From LinuxEmergency with MIT License 6 votes vote down vote up
def OSinfo():
    '''操作系统基本信息查看'''
    core_number = psutil.cpu_count()
    cpu_number = psutil.cpu_count(logical=True)
    cpu_usage_precent = psutil.cpu_times_percent()
    mem_info = psutil.virtual_memory()
    result = {
        "memtotal": mem_info[0],
        "memavail": mem_info[1],
        "memprecn": mem_info[2],
        "memusage": mem_info[3],
        "memfreed": mem_info[4],
    }
    print '''
        内核版本 : %s
        CORE数量 : %s
        CPU数量 : %s
        CPU使用率 : %s
        内存总量  : %s
        内存使用率 : %s
    '''%(str(platform.platform()),str(core_number),str(cpu_number),str(cpu_usage_precent),str(mem_info[0]),str(mem_info[2])) 
Example #10
Source File: test_system.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cpu_count_none(self):
        # https://github.com/giampaolo/psutil/issues/1085
        for val in (-1, 0, None):
            with mock.patch('psutil._psplatform.cpu_count_logical',
                            return_value=val) as m:
                self.assertIsNone(psutil.cpu_count())
                assert m.called
            with mock.patch('psutil._psplatform.cpu_count_physical',
                            return_value=val) as m:
                self.assertIsNone(psutil.cpu_count(logical=False))
                assert m.called 
Example #11
Source File: test_system.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cpu_count_logical(self):
        logical = psutil.cpu_count()
        self.assertIsNotNone(logical)
        self.assertEqual(logical, len(psutil.cpu_times(percpu=True)))
        self.assertGreaterEqual(logical, 1)
        #
        if os.path.exists("/proc/cpuinfo"):
            with open("/proc/cpuinfo") as fd:
                cpuinfo_data = fd.read()
            if "physical id" not in cpuinfo_data:
                raise unittest.SkipTest("cpuinfo doesn't include physical id") 
Example #12
Source File: test_system.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _test_cpu_percent(self, percent, last_ret, new_ret):
        try:
            self.assertIsInstance(percent, float)
            self.assertGreaterEqual(percent, 0.0)
            self.assertIsNot(percent, -0.0)
            self.assertLessEqual(percent, 100.0 * psutil.cpu_count())
        except AssertionError as err:
            raise AssertionError("\n%s\nlast=%s\nnew=%s" % (
                err, pprint.pformat(last_ret), pprint.pformat(new_ret))) 
Example #13
Source File: test_system.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_per_cpu_percent(self):
        last = psutil.cpu_percent(interval=0.001, percpu=True)
        self.assertEqual(len(last), psutil.cpu_count())
        for x in range(100):
            new = psutil.cpu_percent(interval=None, percpu=True)
            for percent in new:
                self._test_cpu_percent(percent, last, new)
            last = new
        with self.assertRaises(ValueError):
            psutil.cpu_percent(interval=-1, percpu=True) 
Example #14
Source File: test_sunos.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cpu_count(self):
        out = sh("/usr/sbin/psrinfo")
        self.assertEqual(psutil.cpu_count(), len(out.split('\n'))) 
Example #15
Source File: test_linux.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_against_sysdev_cpu_num(self):
        ls = os.listdir("/sys/devices/system/cpu")
        count = len([x for x in ls if re.search(r"cpu\d+$", x) is not None])
        self.assertEqual(psutil.cpu_count(), count) 
Example #16
Source File: test_linux.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_against_nproc(self):
        num = int(sh("nproc --all"))
        self.assertEqual(psutil.cpu_count(logical=True), num) 
Example #17
Source File: test_linux.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_against_lscpu(self):
        out = sh("lscpu -p")
        num = len([x for x in out.split('\n') if not x.startswith('#')])
        self.assertEqual(psutil.cpu_count(logical=True), num) 
Example #18
Source File: test_linux.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_emulate_fallbacks(self):
        import psutil._pslinux
        original = psutil._pslinux.cpu_count_logical()
        # Here we want to mock os.sysconf("SC_NPROCESSORS_ONLN") in
        # order to cause the parsing of /proc/cpuinfo and /proc/stat.
        with mock.patch(
                'psutil._pslinux.os.sysconf', side_effect=ValueError) as m:
            self.assertEqual(psutil._pslinux.cpu_count_logical(), original)
            assert m.called

            # Let's have open() return emtpy data and make sure None is
            # returned ('cause we mimick os.cpu_count()).
            with mock.patch('psutil._common.open', create=True) as m:
                self.assertIsNone(psutil._pslinux.cpu_count_logical())
                self.assertEqual(m.call_count, 2)
                # /proc/stat should be the last one
                self.assertEqual(m.call_args[0][0], '/proc/stat')

            # Let's push this a bit further and make sure /proc/cpuinfo
            # parsing works as expected.
            with open('/proc/cpuinfo', 'rb') as f:
                cpuinfo_data = f.read()
            fake_file = io.BytesIO(cpuinfo_data)
            with mock.patch('psutil._common.open',
                            return_value=fake_file, create=True) as m:
                self.assertEqual(psutil._pslinux.cpu_count_logical(), original)

            # Finally, let's make /proc/cpuinfo return meaningless data;
            # this way we'll fall back on relying on /proc/stat
            with mock_open_content('/proc/cpuinfo', b"") as m:
                self.assertEqual(psutil._pslinux.cpu_count_logical(), original)
                m.called 
Example #19
Source File: profile_recorder.py    From Airtest with Apache License 2.0 5 votes vote down vote up
def __init__(self, interval=0.1):
        super(RecordThread, self).__init__()
        self.pid = os.getpid()
        self.interval = interval

        self.cpu_num = psutil.cpu_count()
        self.process = psutil.Process(self.pid)

        self.profile_data = []
        self.stop_flag = False 
Example #20
Source File: test_system.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cpu_count_physical(self):
        logical = psutil.cpu_count()
        physical = psutil.cpu_count(logical=False)
        if physical is None:
            raise self.skipTest("physical cpu_count() is None")
        if WINDOWS and sys.getwindowsversion()[:2] <= (6, 1):  # <= Vista
            self.assertIsNone(physical)
        else:
            self.assertGreaterEqual(physical, 1)
            self.assertGreaterEqual(logical, physical) 
Example #21
Source File: test_windows.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cpu_count_logical_vs_wmi(self):
        w = wmi.WMI()
        proc = w.Win32_Processor()[0]
        self.assertEqual(psutil.cpu_count(), proc.NumberOfLogicalProcessors) 
Example #22
Source File: test_contracts.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def cpu_num(self, ret, info):
        self.assertIsInstance(ret, int)
        if FREEBSD and ret == -1:
            return
        self.assertGreaterEqual(ret, 0)
        if psutil.cpu_count() == 1:
            self.assertEqual(ret, 0)
        self.assertIn(ret, list(range(psutil.cpu_count()))) 
Example #23
Source File: test_contracts.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cpu_count(self):
        self.assertIsInstance(psutil.cpu_count(), int) 
Example #24
Source File: test_aix.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cpu_count_logical(self):
        out = sh('/usr/bin/mpstat -a')
        mpstat_lcpu = int(re.search(r"lcpu=(\d+)", out).group(1))
        psutil_lcpu = psutil.cpu_count(logical=True)
        self.assertEqual(mpstat_lcpu, psutil_lcpu) 
Example #25
Source File: test_process.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cpu_num(self):
        p = psutil.Process()
        num = p.cpu_num()
        self.assertGreaterEqual(num, 0)
        if psutil.cpu_count() == 1:
            self.assertEqual(num, 0)
        self.assertIn(p.cpu_num(), range(psutil.cpu_count())) 
Example #26
Source File: test_process.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cpu_percent_numcpus_none(self):
        # See: https://github.com/giampaolo/psutil/issues/1087
        with mock.patch('psutil.cpu_count', return_value=None) as m:
            psutil.Process().cpu_percent()
            assert m.called 
Example #27
Source File: local_tf_runner.py    From garage with MIT License 5 votes vote down vote up
def setup(self,
              algo,
              env,
              sampler_cls=None,
              sampler_args=None,
              n_workers=psutil.cpu_count(logical=False),
              worker_class=DefaultWorker,
              worker_args=None):
        """Set up runner and sessions for algorithm and environment.

        This method saves algo and env within runner and creates a sampler,
        and initializes all uninitialized variables in session.

        Note:
            After setup() is called all variables in session should have been
            initialized. setup() respects existing values in session so
            policy weights can be loaded before setup().

        Args:
            algo (garage.np.algos.RLAlgorithm): An algorithm instance.
            env (garage.envs.GarageEnv): An environement instance.
            sampler_cls (garage.sampler.Sampler): A sampler class.
            sampler_args (dict): Arguments to be passed to sampler constructor.
            n_workers (int): The number of workers the sampler should use.
            worker_class (type): Type of worker the sampler should use.
            worker_args (dict or None): Additional arguments that should be
                passed to the worker.

        """
        self.initialize_tf_vars()
        logger.log(self.sess.graph)
        super().setup(algo, env, sampler_cls, sampler_args, n_workers,
                      worker_class, worker_args) 
Example #28
Source File: local_tf_runner.py    From garage with MIT License 5 votes vote down vote up
def make_sampler(self,
                     sampler_cls,
                     *,
                     seed=None,
                     n_workers=psutil.cpu_count(logical=False),
                     max_path_length=None,
                     worker_class=DefaultWorker,
                     sampler_args=None,
                     worker_args=None):
        """Construct a Sampler from a Sampler class.

        Args:
            sampler_cls (type): The type of sampler to construct.
            seed (int): Seed to use in sampler workers.
            max_path_length (int): Maximum path length to be sampled by the
                sampler. Paths longer than this will be truncated.
            n_workers (int): The number of workers the sampler should use.
            worker_class (type): Type of worker the sampler should use.
            sampler_args (dict or None): Additional arguments that should be
                passed to the sampler.
            worker_args (dict or None): Additional arguments that should be
                passed to the worker.

        Returns:
            sampler_cls: An instance of the sampler class.

        """
        # pylint: disable=useless-super-delegation
        return super().make_sampler(
            sampler_cls,
            seed=seed,
            n_workers=n_workers,
            max_path_length=max_path_length,
            worker_class=TFWorkerClassWrapper(worker_class),
            sampler_args=sampler_args,
            worker_args=worker_args) 
Example #29
Source File: eventually.py    From indy-plenum with Apache License 2.0 5 votes vote down vote up
def isMinimalConfiguration():
    mem = psutil.virtual_memory()
    memAvailableGb = mem.available / (1024 * 1024 * 1024)
    # cpuCount = psutil.cpu_count()
    # we can have a 8 cpu but 100Mb free RAM and the tests will be slow
    return memAvailableGb <= 1.5  # and cpuCount == 1


# increase this number to allow eventually to change timeouts proportionatly 
Example #30
Source File: test_sunos.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_cpu_count(self):
        out = sh("/usr/sbin/psrinfo")
        self.assertEqual(psutil.cpu_count(), len(out.split('\n')))