Skip to content

Commit 2fe32ee

Browse files
committed
Initial commit
0 parents  commit 2fe32ee

19 files changed

+1813
-0
lines changed

CpuHelper.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- encoding:utf-8 -*-
2+
from __future__ import print_function
3+
4+
import platform
5+
6+
import psutil
7+
from osx_cpu_temp import get_cpu_temp
8+
9+
__author__ = 'BBFamily'
10+
11+
12+
def get_cpu_temp_proxy():
13+
"""
14+
暂时只支持mac,但是也会返回0.0,方便统一处理
15+
"""
16+
if platform.system().lower().find("windows") >= 0:
17+
ret_text = 'get_cpu_temp only support mac now!'
18+
print(ret_text)
19+
return 0.0, ret_text
20+
return get_cpu_temp()
21+
22+
23+
def cpu_times():
24+
return psutil.cpu_times()
25+
26+
27+
def cpu_count(logical=True):
28+
return psutil.cpu_count(logical=logical)
29+
30+
31+
def cpu_percent(times=False, interval=1, percpu=True):
32+
func = psutil.cpu_times_percent if times else psutil.cpu_percent
33+
34+
return func(interval=interval, percpu=percpu)

Demo.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- encoding:utf-8 -*-
2+
"""
3+
使用sklearn的parallel作为demo
4+
"""
5+
import numpy as np
6+
from ProcessMonitor import add_process_wrapper
7+
from sklearn.externals.joblib import Parallel
8+
from sklearn.externals.joblib import delayed
9+
import time
10+
11+
from concurrent.futures import ProcessPoolExecutor
12+
13+
__author__ = 'BBFamily'
14+
15+
"""
16+
只需要在具体process job @add_procee_wrapper就ok了
17+
"""
18+
19+
20+
@add_process_wrapper
21+
def do_process_job(jb):
22+
c = count(jb)
23+
end_tick = 100
24+
for cb in c:
25+
time.sleep(1)
26+
if cb % 10 == 0:
27+
print(cb)
28+
if end_tick < cb:
29+
break
30+
return jb
31+
32+
33+
def when_done(r):
34+
print('job {} done:'.format(r.result()))
35+
36+
37+
def make_parallel_poll_jobs():
38+
with ProcessPoolExecutor() as pool:
39+
n_jobs = 10
40+
for jb in np.arange(0, n_jobs):
41+
future_result = pool.submit(do_process_job, jb)
42+
future_result.add_done_callback(when_done)
43+
44+
45+
def make_parallel_jobs():
46+
n_jobs = 10
47+
parallel = Parallel(
48+
n_jobs=n_jobs, verbose=0, pre_dispatch='2*n_jobs')
49+
50+
out = parallel(delayed(do_process_job)(jb) for jb in np.arange(0, n_jobs))
51+
return out
52+
53+
54+
def count(n):
55+
while True:
56+
yield n
57+
n += 1
58+
59+
60+
if __name__ == "__main__":
61+
make_parallel_jobs()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
CC = cc
3+
CFLAGS = -O2 -Wall
4+
INC = -framework IOKit
5+
PREFIX = /usr/local
6+
EXEC = osx-cpu-temp
7+
8+
build : $(EXEC)
9+
10+
clean :
11+
rm $(EXEC)
12+
13+
$(EXEC) : smc.c
14+
$(CC) $(CFLAGS) $(INC) -o $@ $?
15+
16+
install : $(EXEC)
17+
install $(EXEC) $(PREFIX)/bin
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# OSX CPU Temp
2+
3+
Outputs current CPU temperature for OSX.
4+
5+
## Usage
6+
7+
### Compiling
8+
9+
```bash
10+
make
11+
```
12+
13+
### Running
14+
15+
```bash
16+
./osx-cpu-temp
17+
```
18+
19+
or
20+
21+
```bash
22+
sudo make install # installs to /usr/local/bin
23+
osx-cpu-temp
24+
```
25+
26+
### Using clib
27+
28+
```bash
29+
clib install lavoiesl/osx-cpu-temp
30+
```
31+
32+
### Output example
33+
34+
```
35+
61.8°C
36+
```
37+
38+
### Options
39+
40+
* `-C` Output temperature in Celsius (default).
41+
* `-F` Output temperature in Fahrenheit.
42+
43+
## Maintainer
44+
45+
Sébastien Lavoie <[email protected]>
46+
47+
### Source
48+
49+
Apple System Management Control (SMC) Tool
50+
Copyright (C) 2006
51+
52+
### Inspiration
53+
54+
* http://www.eidac.de/smcfancontrol/
55+
* https://github.com/hholtmann/smcFanControl/tree/master/smc-command
13.6 KB
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "osx-cpu-temp",
3+
"version": "0.0.0",
4+
"repo": "lavoiesl/osx-cpu-temp",
5+
"description": "Outputs current CPU temperature in °C for OSX",
6+
"keywords": ["osx", "cpu", "temperature", "sensors"],
7+
"install": "make install"
8+
}

Externals/osx-cpu-temp-master/smc.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* Apple System Management Control (SMC) Tool
3+
* Copyright (C) 2006 devnull
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU General Public License
7+
* as published by the Free Software Foundation; either version 2
8+
* of the License, or (at your option) any later version.
9+
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18+
*/
19+
20+
#include <stdio.h>
21+
#include <string.h>
22+
#include <IOKit/IOKitLib.h>
23+
24+
#include "smc.h"
25+
26+
static io_connect_t conn;
27+
28+
UInt32 _strtoul(char *str, int size, int base)
29+
{
30+
UInt32 total = 0;
31+
int i;
32+
33+
for (i = 0; i < size; i++)
34+
{
35+
if (base == 16)
36+
total += str[i] << (size - 1 - i) * 8;
37+
else
38+
total += (unsigned char) (str[i] << (size - 1 - i) * 8);
39+
}
40+
return total;
41+
}
42+
43+
void _ultostr(char *str, UInt32 val)
44+
{
45+
str[0] = '\0';
46+
sprintf(str, "%c%c%c%c",
47+
(unsigned int) val >> 24,
48+
(unsigned int) val >> 16,
49+
(unsigned int) val >> 8,
50+
(unsigned int) val);
51+
}
52+
53+
kern_return_t SMCOpen(void)
54+
{
55+
kern_return_t result;
56+
io_iterator_t iterator;
57+
io_object_t device;
58+
59+
CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC");
60+
result = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &iterator);
61+
if (result != kIOReturnSuccess)
62+
{
63+
printf("Error: IOServiceGetMatchingServices() = %08x\n", result);
64+
return 1;
65+
}
66+
67+
device = IOIteratorNext(iterator);
68+
IOObjectRelease(iterator);
69+
if (device == 0)
70+
{
71+
printf("Error: no SMC found\n");
72+
return 1;
73+
}
74+
75+
result = IOServiceOpen(device, mach_task_self(), 0, &conn);
76+
IOObjectRelease(device);
77+
if (result != kIOReturnSuccess)
78+
{
79+
printf("Error: IOServiceOpen() = %08x\n", result);
80+
return 1;
81+
}
82+
83+
return kIOReturnSuccess;
84+
}
85+
86+
kern_return_t SMCClose()
87+
{
88+
return IOServiceClose(conn);
89+
}
90+
91+
92+
kern_return_t SMCCall(int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure)
93+
{
94+
size_t structureInputSize;
95+
size_t structureOutputSize;
96+
97+
structureInputSize = sizeof(SMCKeyData_t);
98+
structureOutputSize = sizeof(SMCKeyData_t);
99+
100+
#if MAC_OS_X_VERSION_10_5
101+
return IOConnectCallStructMethod( conn, index,
102+
// inputStructure
103+
inputStructure, structureInputSize,
104+
// ouputStructure
105+
outputStructure, &structureOutputSize );
106+
#else
107+
return IOConnectMethodStructureIStructureO( conn, index,
108+
structureInputSize, /* structureInputSize */
109+
&structureOutputSize, /* structureOutputSize */
110+
inputStructure, /* inputStructure */
111+
outputStructure); /* ouputStructure */
112+
#endif
113+
114+
}
115+
116+
kern_return_t SMCReadKey(UInt32Char_t key, SMCVal_t *val)
117+
{
118+
kern_return_t result;
119+
SMCKeyData_t inputStructure;
120+
SMCKeyData_t outputStructure;
121+
122+
memset(&inputStructure, 0, sizeof(SMCKeyData_t));
123+
memset(&outputStructure, 0, sizeof(SMCKeyData_t));
124+
memset(val, 0, sizeof(SMCVal_t));
125+
126+
inputStructure.key = _strtoul(key, 4, 16);
127+
inputStructure.data8 = SMC_CMD_READ_KEYINFO;
128+
129+
result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure);
130+
if (result != kIOReturnSuccess)
131+
return result;
132+
133+
val->dataSize = outputStructure.keyInfo.dataSize;
134+
_ultostr(val->dataType, outputStructure.keyInfo.dataType);
135+
inputStructure.keyInfo.dataSize = val->dataSize;
136+
inputStructure.data8 = SMC_CMD_READ_BYTES;
137+
138+
result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure);
139+
if (result != kIOReturnSuccess)
140+
return result;
141+
142+
memcpy(val->bytes, outputStructure.bytes, sizeof(outputStructure.bytes));
143+
144+
return kIOReturnSuccess;
145+
}
146+
147+
double SMCGetTemperature(char *key)
148+
{
149+
SMCVal_t val;
150+
kern_return_t result;
151+
152+
result = SMCReadKey(key, &val);
153+
if (result == kIOReturnSuccess) {
154+
// read succeeded - check returned value
155+
if (val.dataSize > 0) {
156+
if (strcmp(val.dataType, DATATYPE_SP78) == 0) {
157+
// convert sp78 value to temperature
158+
int intValue = val.bytes[0] * 256 + (unsigned char)val.bytes[1];
159+
return intValue / 256.0;
160+
}
161+
}
162+
}
163+
// read failed
164+
return 0.0;
165+
}
166+
167+
double convertToFahrenheit(double celsius) {
168+
return (celsius * (9.0 / 5.0)) + 32.0;
169+
}
170+
171+
int main(int argc, char *argv[])
172+
{
173+
char scale = 'C';
174+
175+
int c;
176+
while ((c = getopt(argc, argv, "CF")) != -1) {
177+
switch (c) {
178+
case 'F':
179+
case 'C':
180+
scale = c;
181+
break;
182+
}
183+
}
184+
185+
SMCOpen();
186+
double temperature = SMCGetTemperature(SMC_KEY_CPU_TEMP);
187+
SMCClose();
188+
189+
if (scale == 'F') {
190+
temperature = convertToFahrenheit(temperature);
191+
}
192+
193+
printf("%0.1f°%c\n", temperature, scale);
194+
195+
return 0;
196+
}

0 commit comments

Comments
 (0)