Debian Codegraph @

src/libsysprof/sysprof-power-profile.c

raw
1 /* sysprof-power-profile.c
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (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, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24
25 #include "sysprof-power-profile.h"
26 #include "sysprof-instrument-private.h"
27 #include "sysprof-recording-private.h"
28
29 struct _SysprofPowerProfile
30 {
31 SysprofInstrument parent_instance;
32 SysprofRecording *recording;
33 char *restore_id;
34 char *id;
35 };
36
37 struct _SysprofPowerProfileClass
38 {
39 SysprofInstrumentClass parent_class;
40 };
41
42 G_DEFINE_FINAL_TYPE (SysprofPowerProfile, sysprof_power_profile, SYSPROF_TYPE_INSTRUMENT)
43
44 enum {
45 PROP_0,
46 PROP_ID,
47 N_PROPS
48 };
49
50 static GParamSpec *properties[N_PROPS];
51
52 static char **
53 sysprof_power_profile_list_required_policy (SysprofInstrument *instrument)
54 {
55 static const char *required_policy[] = {"org.freedesktop.UPower.PowerProfiles.switch-profile", NULL};
56
57 return g_strdupv ((char **)required_policy);
58 }
59
60 static void
61 restore_power_profile (char *power_profile)
62 {
63 g_debug ("Restoring performance profile to %s\n", power_profile);
64
65 if (power_profile != NULL)
66 {
67 g_autofree char *hold = power_profile;
68 g_autoptr(GDBusConnection) bus = NULL;
69 g_autoptr(GVariant) ret = NULL;
70 g_autoptr(GError) error = NULL;
71
72 if (!(bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL)))
73 return;
74
75 ret = g_dbus_connection_call_sync (bus,
76 "org.freedesktop.UPower.PowerProfiles",
77 "/org/freedesktop/UPower/PowerProfiles",
78 "org.freedesktop.DBus.Properties",
79 "Set",
80 g_variant_new ("(ssv)",
81 "org.freedesktop.UPower.PowerProfiles",
82 "ActiveProfile",
83 g_variant_new_string (power_profile)),
84 NULL,
85 G_DBUS_CALL_FLAGS_NONE,
86 -1,
87 NULL, &error);
88
89 if (error != NULL)
90 g_warning ("Failed to restore performance profile: %s", error->message);
91 }
92 }
93
94 static DexFuture *
95 sysprof_power_profile_release_cb (DexFuture *future,
96 gpointer user_data)
97 {
98 SysprofPowerProfile *self = user_data;
99
100 g_assert (DEX_IS_FUTURE (future));
101 g_assert (SYSPROF_IS_POWER_PROFILE (self));
102
103 g_clear_pointer (&self->restore_id, restore_power_profile);
104
105 return dex_future_new_for_boolean (TRUE);
106 }
107
108 static DexFuture *
109 sysprof_power_profile_record (SysprofInstrument *instrument,
110 SysprofRecording *recording,
111 GCancellable *cancellable)
112 {
113 g_assert (SYSPROF_IS_POWER_PROFILE (instrument));
114 g_assert (SYSPROF_IS_RECORDING (recording));
115 g_assert (G_IS_CANCELLABLE (cancellable));
116
117 return dex_future_finally (dex_cancellable_new_from_cancellable (cancellable),
118 sysprof_power_profile_release_cb,
119 g_object_ref (instrument),
120 g_object_unref);
121 }
122
123 static char *
124 get_string_prop (GVariant *v)
125 {
126 g_autoptr(GVariant) child1 = g_variant_get_child_value (v, 0);
127 g_autoptr(GVariant) child2 = g_variant_get_child_value (child1, 0);
128
129 return g_variant_dup_string (child2, NULL);
130 }
131
132 static DexFuture *
133 sysprof_power_profile_prepare_fiber (gpointer user_data)
134 {
135 SysprofPowerProfile *self = user_data;
136 g_autoptr(GDBusConnection) bus = NULL;
137 g_autoptr(GVariant) ret = NULL;
138 g_autoptr(GError) error = NULL;
139
140 g_assert (SYSPROF_IS_POWER_PROFILE (self));
141 g_assert (SYSPROF_IS_RECORDING (self->recording));
142
143 if (self->id == NULL)
144 goto failure;
145
146 if (!(bus = dex_await_object (dex_bus_get (G_BUS_TYPE_SYSTEM), &error)))
147 goto failure;
148
149 if (!(ret = dex_await_variant (dex_dbus_connection_call (bus,
150 "org.freedesktop.UPower.PowerProfiles",
151 "/org/freedesktop/UPower/PowerProfiles",
152 "org.freedesktop.DBus.Properties",
153 "Get",
154 g_variant_new ("(ss)",
155 "org.freedesktop.UPower.PowerProfiles",
156 "ActiveProfile"),
157 G_VARIANT_TYPE ("(v)"),
158 G_DBUS_CALL_FLAGS_NONE,
159 -1), &error)))
160 goto failure;
161
162 /* Save the value to be restored after recording */
163 g_clear_pointer (&self->restore_id, g_free);
164 self->restore_id = get_string_prop (ret);
165 g_clear_pointer (&ret, g_variant_unref);
166
167 /* We don't use "HoldPorfile" here because it is not reliable */
168 if (g_strcmp0 (self->id, self->restore_id) != 0)
169 {
170 if (!(ret = dex_await_variant (dex_dbus_connection_call (bus,
171 "org.freedesktop.UPower.PowerProfiles",
172 "/org/freedesktop/UPower/PowerProfiles",
173 "org.freedesktop.DBus.Properties",
174 "Set",
175 g_variant_new ("(ssv)",
176 "org.freedesktop.UPower.PowerProfiles",
177 "ActiveProfile",
178 g_variant_new_string (self->id)),
179 NULL,
180 G_DBUS_CALL_FLAGS_NONE,
181 -1), &error)))
182 goto failure;
183
184 _sysprof_recording_diagnostic (self->recording,
185 "Power Profile",
186 "Power profile temporarily set to %s from %s",
187 self->id, self->restore_id);
188 }
189 else
190 {
191 g_clear_pointer (&self->restore_id, g_free);
192 }
193
194 failure:
195 if (error != NULL)
196 _sysprof_recording_diagnostic (self->recording,
197 "Power Profile",
198 "Failed to set power profile to “%s”: %s",
199 self->id, error->message);
200
201 g_clear_object (&self->recording);
202
203 return dex_future_new_for_boolean (TRUE);
204 }
205
206 static DexFuture *
207 sysprof_power_profile_prepare (SysprofInstrument *instrument,
208 SysprofRecording *recording)
209 {
210 SysprofPowerProfile *self = (SysprofPowerProfile *)instrument;
211
212 g_assert (SYSPROF_IS_POWER_PROFILE (self));
213 g_assert (SYSPROF_IS_RECORDING (recording));
214
215 g_set_object (&self->recording, recording);
216
217 return dex_scheduler_spawn (NULL, 0,
218 sysprof_power_profile_prepare_fiber,
219 g_object_ref (self),
220 g_object_unref);
221 }
222
223 static void
224 sysprof_power_profile_dispose (GObject *object)
225 {
226 SysprofPowerProfile *self = (SysprofPowerProfile *)object;
227
228 g_clear_pointer (&self->id, g_free);
229 g_clear_pointer (&self->restore_id, g_free);
230
231 G_OBJECT_CLASS (sysprof_power_profile_parent_class)->dispose (object);
232 }
233
234 static void
235 sysprof_power_profile_get_property (GObject *object,
236 guint prop_id,
237 GValue *value,
238 GParamSpec *pspec)
239 {
240 SysprofPowerProfile *self = SYSPROF_POWER_PROFILE (object);
241
242 switch (prop_id)
243 {
244 case PROP_ID:
245 g_value_set_string (value, sysprof_power_profile_get_id (self));
246 break;
247
248 default:
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250 }
251 }
252
253 static void
254 sysprof_power_profile_set_property (GObject *object,
255 guint prop_id,
256 const GValue *value,
257 GParamSpec *pspec)
258 {
259 SysprofPowerProfile *self = SYSPROF_POWER_PROFILE (object);
260
261 switch (prop_id)
262 {
263 case PROP_ID:
264 self->id = g_value_dup_string (value);
265 break;
266
267 default:
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 }
270 }
271
272 static void
273 sysprof_power_profile_class_init (SysprofPowerProfileClass *klass)
274 {
275 GObjectClass *object_class = G_OBJECT_CLASS (klass);
276 SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass);
277
278 object_class->dispose = sysprof_power_profile_dispose;
279 object_class->get_property = sysprof_power_profile_get_property;
280 object_class->set_property = sysprof_power_profile_set_property;
281
282 instrument_class->list_required_policy = sysprof_power_profile_list_required_policy;
283 instrument_class->prepare = sysprof_power_profile_prepare;
284 instrument_class->record = sysprof_power_profile_record;
285
286 properties[PROP_ID] =
287 g_param_spec_string ("id", NULL, NULL,
288 NULL,
289 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
290
291 g_object_class_install_properties (object_class, N_PROPS, properties);
292 }
293
294 static void
295 sysprof_power_profile_init (SysprofPowerProfile *self)
296 {
297 }
298
299 SysprofInstrument *
300 sysprof_power_profile_new (const char *id)
301 {
302 return g_object_new (SYSPROF_TYPE_POWER_PROFILE,
303 "id", id,
304 NULL);
305 }
306
307 const char *
308 sysprof_power_profile_get_id (SysprofPowerProfile *self)
309 {
310 g_return_val_if_fail (SYSPROF_IS_POWER_PROFILE (self), NULL);
311
312 return self->id;
313 }