~artfwo/indicator-cpufreq/trunk

« back to all changes in this revision

Viewing changes to indicator_cpufreq/cpufreq.py

  • Committer: Артём Попов
  • Date: 2010-12-16 04:36:29 UTC
  • Revision ID: artfwo@gmail.com-20101216043629-hbvlr5108irut0w0
Initial project creation with Quickly!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
 
### BEGIN LICENSE
3
 
# Copyright (C) 2010 Artem Popov <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).decode()
140
 
 
141
 
def get_policy(cpu):
142
 
    p = _libcpufreq.cpufreq_get_policy(cpu)
143
 
    policy = (p.contents.min, p.contents.max, p.contents.governor.decode())
144
 
    _libcpufreq.cpufreq_put_policy(p)
145
 
    return policy
146
 
 
147
 
def _marshall_structs(first, field, decode=False):
148
 
    values = []
149
 
    p = first
150
 
    while p:
151
 
        if decode:
152
 
            values.append(getattr(p.contents, field).decode())
153
 
        else:
154
 
            values.append(getattr(p.contents, field))
155
 
        p = p.contents.next
156
 
    return values
157
 
 
158
 
def get_available_governors(cpu):
159
 
    structs = _libcpufreq.cpufreq_get_available_governors(cpu)
160
 
    values = _marshall_structs(structs, 'governor', decode=True)
161
 
    _libcpufreq.cpufreq_put_available_governors(structs)
162
 
    return values
163
 
 
164
 
def get_available_frequencies(cpu):
165
 
    structs = _libcpufreq.cpufreq_get_available_frequencies(cpu)
166
 
    values = _marshall_structs(structs, 'frequency')
167
 
    _libcpufreq.cpufreq_put_available_frequencies(structs)
168
 
    return values
169
 
 
170
 
def get_affected_cpus(cpu):
171
 
    structs = _libcpufreq.cpufreq_get_affected_cpus(cpu)
172
 
    values = _marshall_structs(structs, 'cpu')
173
 
    _libcpufreq.cpufreq_put_affected_cpus(structs)
174
 
    return values
175
 
 
176
 
def get_related_cpus(cpu):
177
 
    structs = _libcpufreq.cpufreq_get_related_cpus(cpu)
178
 
    values = _marshall_structs(structs, 'cpu')
179
 
    _libcpufreq.cpufreq_put_related_cpus(structs)
180
 
    return values
181
 
 
182
 
def get_stats(cpu):
183
 
    total_time = c_ulonglong()
184
 
    p = _libcpufreq.cpufreq_get_stats(cpu, byref(total_time))
185
 
    stats = []
186
 
    while p:
187
 
        stats.append((p.contents.frequency, p.contents.time_in_state))
188
 
        p = p.contents.next
189
 
    _libcpufreq.cpufreq_put_stats(p)
190
 
    return total_time.value, stats
191
 
 
192
 
def get_transitions(cpu):
193
 
    return _libcpufreq.cpufreq_get_transitions(cpu)
194
 
 
195
 
def set_policy(cpu, min, max, governor):
196
 
    return _libcpufreq.cpufreq_set_policy(cpu, _cpufreq_policy(min, max, governor))
197
 
 
198
 
def modify_policy_min(cpu, min_freq):
199
 
    return _libcpufreq.cpufreq_modify_policy_min(cpu, min_freq)
200
 
 
201
 
def modify_policy_min(cpu, max_freq):
202
 
    return _libcpufreq.cpufreq_modify_policy_max(cpu, max_freq)
203
 
 
204
 
def modify_policy_governor(cpu, governor):
205
 
    return _libcpufreq.cpufreq_modify_policy_governor(cpu, governor.encode())
206
 
 
207
 
def set_frequency(cpu, target_frequency):
208
 
    return _libcpufreq.cpufreq_set_frequency(cpu, target_frequency)