~artfwo/indicator-cpufreq/trunk

2 by Артём Попов
Initial working version.
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
### BEGIN LICENSE
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.
7
# 
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.
12
# 
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/>.
15
### END LICENSE
16
17
from ctypes import *
18
from ctypes.util import find_library
19
20
_libcpufreq = cdll.LoadLibrary(find_library("cpufreq"))
21
22
class _cpufreq_policy(Structure):
23
    _fields_ = [
24
        ("min", c_ulong),
25
        ("max", c_ulong),
26
        ("governor", c_char_p)]
27
28
class _cpufreq_available_governors(Structure):
29
    pass
30
_cpufreq_available_governors._fields_ = [
31
    ("governor", c_char_p),
32
    ("next", POINTER(_cpufreq_available_governors)),
33
    ("first", POINTER(_cpufreq_available_governors))]
34
35
class _cpufreq_available_frequencies(Structure):
36
    pass
37
_cpufreq_available_frequencies._fields_ = [
38
    ("frequency", c_ulong),
39
    ("next", POINTER(_cpufreq_available_frequencies)),
40
    ("first", POINTER(_cpufreq_available_frequencies))]
41
42
class _cpufreq_affected_cpus(Structure):
43
    pass
44
_cpufreq_affected_cpus._fields_ = [
45
    ("cpu", c_uint),
46
    ("next", POINTER(_cpufreq_affected_cpus)),
47
    ("first", POINTER(_cpufreq_affected_cpus))]
48
49
class _cpufreq_stats(Structure):
50
    pass
51
_cpufreq_stats._fields_ = [
52
    ("frequency", c_ulong),
53
    ("time_in_state", c_ulonglong),
54
    ("next", POINTER(_cpufreq_stats)),
55
    ("first", POINTER(_cpufreq_stats))]
56
57
###############################################################################
58
59
_libcpufreq.cpufreq_cpu_exists.argtypes = [c_uint]
60
_libcpufreq.cpufreq_cpu_exists.restype = c_int
61
62
_libcpufreq.cpufreq_get_freq_kernel.argtypes = [c_uint]
63
_libcpufreq.cpufreq_get_freq_kernel.restype = c_ulong
64
65
_libcpufreq.cpufreq_get_freq_hardware.argtypes = [c_uint]
66
_libcpufreq.cpufreq_get_freq_hardware.restype = c_ulong
67
68
_libcpufreq.cpufreq_get_transition_latency.argtypes = [c_uint]
69
_libcpufreq.cpufreq_get_transition_latency.restype = c_ulong
70
71
_libcpufreq.cpufreq_get_hardware_limits.argtypes = [c_uint, POINTER(c_ulong), POINTER(c_ulong)]
72
_libcpufreq.cpufreq_get_hardware_limits.restype = c_int
73
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);
77
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);
81
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);
85
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);
89
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);
93
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);
97
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);
101
102
_libcpufreq.cpufreq_get_transitions.argtypes = [c_uint]
103
_libcpufreq.cpufreq_get_transitions.restype = c_ulong
104
105
_libcpufreq.cpufreq_set_policy.argtypes = [c_uint, POINTER(_cpufreq_policy)]
106
_libcpufreq.cpufreq_set_policy.restype = c_int
107
108
_libcpufreq.cpufreq_modify_policy_min.argtypes = [c_uint, c_ulong]
109
_libcpufreq.cpufreq_modify_policy_min.restype = c_int
110
111
_libcpufreq.cpufreq_modify_policy_max.argtypes = [c_uint, c_ulong]
112
_libcpufreq.cpufreq_modify_policy_max.restype = c_int
113
114
_libcpufreq.cpufreq_modify_policy_governor.argtypes = [c_uint, c_char_p]
115
_libcpufreq.cpufreq_modify_policy_governor.restype = c_int
116
117
_libcpufreq.cpufreq_set_frequency.argtypes = [c_uint, c_ulong]
118
_libcpufreq.cpufreq_set_frequency.restype = c_int
119
120
def cpu_exists(cpu):
121
    return _libcpufreq.cpufreq_cpu_exists(cpu)
122
123
def get_freq_kernel(cpu):
124
    return _libcpufreq.cpufreq_get_freq_kernel(cpu)
125
126
def get_freq_hardware(cpu):
127
    return _libcpufreq.cpufreq_get_freq_hardware(cpu)
128
129
def get_transition_latency(cpu):
130
    return _libcpufreq.cpufreq_get_transition_latency(cpu)
131
132
def get_hardware_limits(cpu):
133
    min = c_ulong()
134
    max = c_ulong()
135
    _libcpufreq.cpufreq_get_hardware_limits(cpu, byref(min), byref(max))
136
    return (min.value, max.value)
137
138
def get_driver(cpu):
139
    return _libcpufreq.cpufreq_get_driver(cpu)
140
141
def get_policy(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)
145
    return policy
146
147
def _marshall_structs(first, field):
148
    values = []
149
    p = first
150
    while p:
151
        values.append(getattr(p.contents, field))
152
        p = p.contents.next
153
    return values
154
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)
159
    return values
160
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)
165
    return values
166
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)
171
    return values
172
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)
177
    return values
178
179
def get_stats(cpu):
180
    total_time = c_ulonglong()
181
    p = _libcpufreq.cpufreq_get_stats(cpu, byref(total_time))
182
    stats = []
183
    while p:
184
        stats.append((p.contents.frequency, p.contents.time_in_state))
185
        p = p.contents.next
186
    _libcpufreq.cpufreq_put_stats(p)
187
    return total_time.value, stats
188
189
def get_transitions(cpu):
190
    return _libcpufreq.cpufreq_get_transitions(cpu)
191
192
def set_policy(cpu, min, max, governor):
193
    return _libcpufreq.cpufreq_set_policy(cpu, _cpufreq_policy(min, max, governor))
194
195
def modify_policy_min(cpu, min_freq):
196
    return _libcpufreq.cpufreq_modify_policy_min(cpu, min_freq)
197
198
def modify_policy_min(cpu, max_freq):
199
    return _libcpufreq.cpufreq_modify_policy_max(cpu, max_freq)
200
201
def modify_policy_governor(cpu, governor):
202
    return _libcpufreq.cpufreq_modify_policy_governor(cpu, governor)
203
204
def set_frequency(cpu, target_frequency):
205
    return _libcpufreq.cpufreq_set_frequency(cpu, target_frequency)