Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Osx temps #1284

Merged
merged 12 commits into from
Jun 26, 2018
4 changes: 2 additions & 2 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2175,7 +2175,7 @@ def net_if_stats():
# =====================================================================


# Linux
# Linux, OSX
if hasattr(_psplatform, "sensors_temperatures"):

def sensors_temperatures(fahrenheit=False):
Expand Down Expand Up @@ -2213,7 +2213,7 @@ def convert(n):
__all__.append("sensors_temperatures")


# Linux
# Linux, OSX
if hasattr(_psplatform, "sensors_fans"):

def sensors_fans():
Expand Down
2 changes: 1 addition & 1 deletion psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class BatteryTime(enum.IntEnum):
'shwtemp', ['label', 'current', 'high', 'critical'])
# psutil.sensors_battery()
sbattery = namedtuple('sbattery', ['percent', 'secsleft', 'power_plugged'])
# psutil.sensors_battery()
# psutil.sensors_fans()
sfan = namedtuple('sfan', ['label', 'current'])

# --- for Process methods
Expand Down
146 changes: 146 additions & 0 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import contextlib
import errno
import collections
import functools
import os
from socket import AF_INET
Expand Down Expand Up @@ -212,6 +213,137 @@ def disk_partitions(all=False):
# =====================================================================


def sensors_temperatures():
"""Return CPU temperatures including key name and temperature.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update docstring


References:

https://stackoverflow.com/questions/28568775/
description-for-apples-smc-keys/31033665#31033665

https://github.com/Chris911/iStats/blob/
09b159f85a9481b59f347a37259f6d272f65cc05/lib/iStats/smc.rb

http://web.archive.org/web/20140714090133/http://www.parhelia.ch:80/
blog/statics/k3_keys.html
"""
ret = collections.defaultdict(list)
temperatures = (
# group, key, label

# --- CPU
("CPU", "TCXC", "PECI CPU"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please define this globally as SMC_TEMPS after PROC_STATUSES

("CPU", "TCXc", "PECI CPU"),
("CPU", "TC0P", "CPU 1 Proximity"),
("CPU", "TC0H", "CPU 1 Heatsink"),
("CPU", "TC0D", "CPU 1 Package"),
("CPU", "TC0E", "CPU 1"),
("CPU", "TC0F", "CPU 1"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Judging from the script output you paster earlier it seems we get a duplicate for TC0E and TC0F so I suggest to remove TC0F

("CPU", "TC1C", "CPU Core 1"),
("CPU", "TC2C", "CPU Core 2"),
("CPU", "TC3C", "CPU Core 3"),
("CPU", "TC4C", "CPU Core 4"),
("CPU", "TC5C", "CPU Core 5"),
("CPU", "TC6C", "CPU Core 6"),
("CPU", "TC7C", "CPU Core 7"),
("CPU", "TC8C", "CPU Core 8"),
("CPU", "TCAH", "CPU 1 Heatsink Alt."),
("CPU", "TCAD", "CPU 1 Package Alt."),
("CPU", "TC1P", "CPU 2 Proximity"),
("CPU", "TC1H", "CPU 2 Heatsink"),
("CPU", "TC1D", "CPU 2 Package"),
("CPU", "TC1E", "CPU 2"),
("CPU", "TC1F", "CPU 2"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Just keep TC1E

("CPU", "TCBH", "CPU 2 Heatsink Alt."),
("CPU", "TCBD", "CPU 2 Package Alt."),

("CPU", "TCSC", "PECI SA"),
("CPU", "TCSc", "PECI SA"),
("CPU", "TCSA", "PECI SA"),

# --- GPU
("GPU", "TCGC", "PECI GPU"),
("GPU", "TCGc", "PECI GPU"),
("GPU", "TG0P", "GPU Proximity"),
("GPU", "TG0D", "GPU Die"),
("GPU", "TG1D", "GPU Die"),
("GPU", "TG0H", "GPU Heatsink"),
("GPU", "TG1H", "GPU Heatsink"),

# --- Memory
("Memory", "Ts0S", "Memory Proximity"),
("Memory", "TM0P", "Mem Bank A1"),
("Memory", "TM1P", "Mem Bank A2"),
("Memory", "TM8P", "Mem Bank B1"),
("Memory", "TM9P", "Mem Bank B2"),
("Memory", "TM0S", "Mem Module A1"),
("Memory", "TM1S", "Mem Module A2"),
("Memory", "TM8S", "Mem Module B1"),
("Memory", "TM9S", "Mem Module B2"),

# --- HDD
("HDD", "TH0P", "HDD Bay 1"),
("HDD", "TH1P", "HDD Bay 2"),
("HDD", "TH2P", "HDD Bay 3"),
("HDD", "TH3P", "HDD Bay 4"),

# --- Battery
("Battery", "TB0T", "Battery TS_MAX"),
("Battery", "TB1T", "Battery 1"),
("Battery", "TB2T", "Battery 2"),
("Battery", "TB3T", "Battery"),

# --- Others
("Others", "TN0D", "Northbridge Die"),
("Others", "TN0P", "Northbridge Proximity 1"),
("Others", "TN1P", "Northbridge Proximity 2"),
("Others", "TN0C", "MCH Die"),
("Others", "TN0H", "MCH Heatsink"),
("Others", "TP0D", "PCH Die"),
("Others", "TPCD", "PCH Die"),
("Others", "TP0P", "PCH Proximity"),

("Others", "TA0P", "Airflow 1"),
("Others", "TA1P", "Airflow 2"),
("Others", "Th0H", "Heatpipe 1"),
("Others", "Th1H", "Heatpipe 2"),
("Others", "Th2H", "Heatpipe 3"),

("Others", "Tm0P", "Mainboard Proximity"),
("Others", "Tp0P", "Powerboard Proximity"),
("Others", "Ts0P", "Palm Rest"),
("Others", "Tb0P", "BLC Proximity"),

("Others", "TL0P", "LCD Proximity"),
("Others", "TW0P", "Airport Proximity"),
("Others", "TO0P", "Optical Drive"),

("Others", "Tp0P", "Power Supply 1"),
("Others", "Tp0C", "Power Supply 1 Alt."),
("Others", "Tp1P", "Power Supply 2"),
("Others", "Tp1C", "Power Supply 2 Alt."),
("Others", "Tp2P", "Power Supply 3"),
("Others", "Tp3P", "Power Supply 4"),
("Others", "Tp4P", "Power Supply 5"),
("Others", "Tp5P", "Power Supply 6"),

("Others", "TS0C", "Expansion Slots"),
("Others", "TA0S", "PCI Slot 1 Pos 1"),
("Others", "TA1S", "PCI Slot 1 Pos 2"),
("Others", "TA2S", "PCI Slot 2 Pos 1"),
("Others", "TA3S", "PCI Slot 2 Pos 2"),
)

for group, key, label in temperatures:
result = cext.smc_get_temperature(key)
result = round(result, 1)
if result <= 0:
continue
ret[group].append((label, result, None, None))

return dict(ret)


def sensors_battery():
"""Return battery information.
"""
Expand All @@ -230,6 +362,20 @@ def sensors_battery():
return _common.sbattery(percent, secsleft, power_plugged)


def sensors_fans():
"""Return fans speed information.
"""
ret = collections.defaultdict(list)
fan_num = cext.fans_count()
for fan in range(fan_num):
result = cext.get_fan_speed(fan)
if result < 0:
continue
ret["Fans"].append(_common.sfan("Fan %i" % fan, int(result)))

return dict(ret)


# =====================================================================
# --- network
# =====================================================================
Expand Down
67 changes: 67 additions & 0 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "_psutil_common.h"
#include "_psutil_posix.h"
#include "arch/osx/process_info.h"
#include "arch/osx/smc.h"


#define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0)
Expand Down Expand Up @@ -521,6 +522,19 @@ psutil_cpu_count_phys(PyObject *self, PyObject *args) {
return Py_BuildValue("i", num);
}

/*
* Return the number of CPU packages
*/
static PyObject *
psutil_cpu_count_packages(PyObject *self, PyObject *args) {
int num;
size_t size = sizeof(int);

if (sysctlbyname("hw.packages", &num, &size, NULL, 0))
Py_RETURN_NONE;
else
return Py_BuildValue("i", num);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is no longer necessary


/*
* Indicates if the given virtual address on the given architecture is in the
Expand Down Expand Up @@ -860,6 +874,50 @@ psutil_boot_time(PyObject *self, PyObject *args) {
return Py_BuildValue("f", (float)boot_time);
}

/*
* Return a Python float indicating the value of the temperature
* measured by an SMC key
*/
static PyObject *
psutil_smc_get_temperature(PyObject *self, PyObject *args) {
char* key;
float temp;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new line


if (! PyArg_ParseTuple(args, "s", &key)) {
return NULL;
}
temp = SMCGetTemperature(key);
return Py_BuildValue("d", temp);
}

/*
* Return a Python float indicating the number of fans in the system
*/
static PyObject *
psutil_fans_count(PyObject *self, PyObject *args) {
int fan_count;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new line


fan_count = SMCGetFanNumber(SMC_KEY_FAN_NUM);
if (fan_count < 0) {
fan_count = 0;
}
return Py_BuildValue("i", fan_count);
}

/*
* Return a Python float indicating the fan speed of the number of fan passed
*/
static PyObject *
psutil_get_fan_speed(PyObject *self, PyObject *args) {
int key;
float speed;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new line


if (! PyArg_ParseTuple(args, "i", &key)) {
return NULL;
}
speed = SMCGetFanSpeed(key);
return Py_BuildValue("f", speed);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to use a single C psutil_sendors_fans() function which gets the number of fans and their speed in one shot. I see no need of 2 separate functions.


/*
* Return a list of tuples including device, mount point and fs type
Expand Down Expand Up @@ -1798,6 +1856,7 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
}



/*
* Return battery information.
*/
Expand Down Expand Up @@ -1927,6 +1986,8 @@ PsutilMethods[] = {
"Return number of logical CPUs on the system"},
{"cpu_count_phys", psutil_cpu_count_phys, METH_VARARGS,
"Return number of physical CPUs on the system"},
{"cpu_count_packages", psutil_cpu_count_packages, METH_VARARGS,
"Return number of cpu packages"},
{"virtual_mem", psutil_virtual_mem, METH_VARARGS,
"Return system virtual memory stats"},
{"swap_mem", psutil_swap_mem, METH_VARARGS,
Expand All @@ -1950,6 +2011,12 @@ PsutilMethods[] = {
"Return currently connected users as a list of tuples"},
{"cpu_stats", psutil_cpu_stats, METH_VARARGS,
"Return CPU statistics"},
{"smc_get_temperature", psutil_smc_get_temperature, METH_VARARGS,
"Temperature of SMC key as float"},
{"fans_count", psutil_fans_count, METH_VARARGS,
"Return the number of fans"},
{"get_fan_speed", psutil_get_fan_speed, METH_VARARGS,
"Return the RPM of the fan with SMC key"},
{"sensors_battery", psutil_sensors_battery, METH_VARARGS,
"Return battery information."},

Expand Down
Loading