1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Copyright (C) 2010 Артём Попов <artfwo@gmail.com>
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU General Public License version 3, as published
6
# by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
# PURPOSE. See the GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License along
14
# with this program. If not, see <http://www.gnu.org/licenses/>.
18
from ctypes.util import find_library
20
_libcpufreq = cdll.LoadLibrary(find_library("cpufreq"))
22
class _cpufreq_policy(Structure):
26
("governor", c_char_p)]
28
class _cpufreq_available_governors(Structure):
30
_cpufreq_available_governors._fields_ = [
31
("governor", c_char_p),
32
("next", POINTER(_cpufreq_available_governors)),
33
("first", POINTER(_cpufreq_available_governors))]
35
class _cpufreq_available_frequencies(Structure):
37
_cpufreq_available_frequencies._fields_ = [
38
("frequency", c_ulong),
39
("next", POINTER(_cpufreq_available_frequencies)),
40
("first", POINTER(_cpufreq_available_frequencies))]
42
class _cpufreq_affected_cpus(Structure):
44
_cpufreq_affected_cpus._fields_ = [
46
("next", POINTER(_cpufreq_affected_cpus)),
47
("first", POINTER(_cpufreq_affected_cpus))]
49
class _cpufreq_stats(Structure):
51
_cpufreq_stats._fields_ = [
52
("frequency", c_ulong),
53
("time_in_state", c_ulonglong),
54
("next", POINTER(_cpufreq_stats)),
55
("first", POINTER(_cpufreq_stats))]
57
###############################################################################
59
_libcpufreq.cpufreq_cpu_exists.argtypes = [c_uint]
60
_libcpufreq.cpufreq_cpu_exists.restype = c_int
62
_libcpufreq.cpufreq_get_freq_kernel.argtypes = [c_uint]
63
_libcpufreq.cpufreq_get_freq_kernel.restype = c_ulong
65
_libcpufreq.cpufreq_get_freq_hardware.argtypes = [c_uint]
66
_libcpufreq.cpufreq_get_freq_hardware.restype = c_ulong
68
_libcpufreq.cpufreq_get_transition_latency.argtypes = [c_uint]
69
_libcpufreq.cpufreq_get_transition_latency.restype = c_ulong
71
_libcpufreq.cpufreq_get_hardware_limits.argtypes = [c_uint, POINTER(c_ulong), POINTER(c_ulong)]
72
_libcpufreq.cpufreq_get_hardware_limits.restype = c_int
74
_libcpufreq.cpufreq_get_driver.argtypes = [c_uint]
75
_libcpufreq.cpufreq_get_driver.restype = c_char_p
76
#extern void cpufreq_put_driver(char * ptr);
78
_libcpufreq.cpufreq_get_policy.argtypes = [c_uint]
79
_libcpufreq.cpufreq_get_policy.restype = POINTER(_cpufreq_policy)
80
#extern void cpufreq_put_policy(struct cpufreq_policy *policy);
82
_libcpufreq.cpufreq_get_available_governors.argtypes = [c_uint]
83
_libcpufreq.cpufreq_get_available_governors.restype = POINTER(_cpufreq_available_governors)
84
#extern void cpufreq_put_available_governors(struct _cpufreq_available_governors *first);
86
_libcpufreq.cpufreq_get_available_frequencies.argtypes = [c_uint]
87
_libcpufreq.cpufreq_get_available_frequencies.restype = POINTER(_cpufreq_available_frequencies)
88
#extern void cpufreq_put_available_frequencies(struct _cpufreq_available_frequencies *first);
90
_libcpufreq.cpufreq_get_affected_cpus.argtypes = [c_uint]
91
_libcpufreq.cpufreq_get_affected_cpus.restype = POINTER(_cpufreq_affected_cpus)
92
#extern void cpufreq_put_affected_cpus(struct _cpufreq_affected_cpus *first);
94
_libcpufreq.cpufreq_get_related_cpus.argtypes = [c_uint]
95
_libcpufreq.cpufreq_get_related_cpus.restype = POINTER(_cpufreq_affected_cpus)
96
#extern void cpufreq_put_related_cpus(struct _cpufreq_affected_cpus *first);
98
_libcpufreq.cpufreq_get_stats.argtypes = [c_uint, POINTER(c_ulonglong)]
99
_libcpufreq.cpufreq_get_stats.restype = POINTER(_cpufreq_stats)
100
#extern void cpufreq_put_stats(struct _cpufreq_stats *stats);
102
_libcpufreq.cpufreq_get_transitions.argtypes = [c_uint]
103
_libcpufreq.cpufreq_get_transitions.restype = c_ulong
105
_libcpufreq.cpufreq_set_policy.argtypes = [c_uint, POINTER(_cpufreq_policy)]
106
_libcpufreq.cpufreq_set_policy.restype = c_int
108
_libcpufreq.cpufreq_modify_policy_min.argtypes = [c_uint, c_ulong]
109
_libcpufreq.cpufreq_modify_policy_min.restype = c_int
111
_libcpufreq.cpufreq_modify_policy_max.argtypes = [c_uint, c_ulong]
112
_libcpufreq.cpufreq_modify_policy_max.restype = c_int
114
_libcpufreq.cpufreq_modify_policy_governor.argtypes = [c_uint, c_char_p]
115
_libcpufreq.cpufreq_modify_policy_governor.restype = c_int
117
_libcpufreq.cpufreq_set_frequency.argtypes = [c_uint, c_ulong]
118
_libcpufreq.cpufreq_set_frequency.restype = c_int
121
return _libcpufreq.cpufreq_cpu_exists(cpu)
123
def get_freq_kernel(cpu):
124
return _libcpufreq.cpufreq_get_freq_kernel(cpu)
126
def get_freq_hardware(cpu):
127
return _libcpufreq.cpufreq_get_freq_hardware(cpu)
129
def get_transition_latency(cpu):
130
return _libcpufreq.cpufreq_get_transition_latency(cpu)
132
def get_hardware_limits(cpu):
135
_libcpufreq.cpufreq_get_hardware_limits(cpu, byref(min), byref(max))
136
return (min.value, max.value)
139
return _libcpufreq.cpufreq_get_driver(cpu)
142
p = _libcpufreq.cpufreq_get_policy(cpu)
143
policy = (p.contents.min, p.contents.max, p.contents.governor)
144
_libcpufreq.cpufreq_put_policy(p)
147
def _marshall_structs(first, field):
151
values.append(getattr(p.contents, field))
155
def get_available_governors(cpu):
156
structs = _libcpufreq.cpufreq_get_available_governors(cpu)
157
values = _marshall_structs(structs, 'governor')
158
_libcpufreq.cpufreq_put_available_governors(structs)
161
def get_available_frequencies(cpu):
162
structs = _libcpufreq.cpufreq_get_available_frequencies(cpu)
163
values = _marshall_structs(structs, 'frequency')
164
_libcpufreq.cpufreq_put_available_frequencies(structs)
167
def get_affected_cpus(cpu):
168
structs = _libcpufreq.cpufreq_get_affected_cpus(cpu)
169
values = _marshall_structs(structs, 'cpu')
170
_libcpufreq.cpufreq_put_affected_cpus(structs)
173
def get_related_cpus(cpu):
174
structs = _libcpufreq.cpufreq_get_related_cpus(cpu)
175
values = _marshall_structs(structs, 'cpu')
176
_libcpufreq.cpufreq_put_related_cpus(structs)
180
total_time = c_ulonglong()
181
p = _libcpufreq.cpufreq_get_stats(cpu, byref(total_time))
184
stats.append((p.contents.frequency, p.contents.time_in_state))
186
_libcpufreq.cpufreq_put_stats(p)
187
return total_time.value, stats
189
def get_transitions(cpu):
190
return _libcpufreq.cpufreq_get_transitions(cpu)
192
def set_policy(cpu, min, max, governor):
193
return _libcpufreq.cpufreq_set_policy(cpu, _cpufreq_policy(min, max, governor))
195
def modify_policy_min(cpu, min_freq):
196
return _libcpufreq.cpufreq_modify_policy_min(cpu, min_freq)
198
def modify_policy_min(cpu, max_freq):
199
return _libcpufreq.cpufreq_modify_policy_max(cpu, max_freq)
201
def modify_policy_governor(cpu, governor):
202
return _libcpufreq.cpufreq_modify_policy_governor(cpu, governor)
204
def set_frequency(cpu, target_frequency):
205
return _libcpufreq.cpufreq_set_frequency(cpu, target_frequency)