Debian Codegraph @

src/sysprofd/sysprofd.c

raw
1 /* sysprofd.c
2 *
3 * Copyright 2019 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 #define G_LOG_DOMAIN "sysprofd"
22
23 #include "config.h"
24
25 #include <gio/gio.h>
26 #include <stdlib.h>
27 #include <sysprof-capture.h>
28
29 #include "ipc-service.h"
30
31 #include "ipc-rapl-profiler.h"
32 #include "ipc-service-impl.h"
33 #include "ipc-unwinder-impl.h"
34
35 #define V3_PATH "/org/gnome/Sysprof3"
36 #define RAPL_PATH "/org/gnome/Sysprof3/RAPL"
37 #define UNWINDER_PATH "/org/gnome/Sysprof3/Unwinder"
38 #define NAME_ACQUIRE_DELAY_SECS 3
39 #define INACTIVITY_TIMEOUT_SECS 120
40
41 static const gchar *bus_names[] = { "org.gnome.Sysprof3" };
42 static GMainLoop *main_loop;
43 static gboolean name_acquired;
44 static gint exit_status = EXIT_SUCCESS;
45
46 static guint inactivity;
47 static G_LOCK_DEFINE (activity);
48
49 static gboolean
50 inactivity_cb (gpointer data)
51 {
52 inactivity = 0;
53 g_main_loop_quit (main_loop);
54 return G_SOURCE_REMOVE;
55 }
56
57 static void
58 activity_cb (GObject *object,
59 gpointer user_data)
60 {
61 G_LOCK (activity);
62 if (inactivity)
63 g_source_remove (inactivity);
64 inactivity = g_timeout_add_seconds (INACTIVITY_TIMEOUT_SECS,
65 inactivity_cb,
66 NULL);
67 G_UNLOCK (activity);
68 }
69
70 static void
71 name_acquired_cb (GDBusConnection *connection,
72 const gchar *name,
73 gpointer user_data)
74 {
75 g_message ("Acquired Bus Name: %s", name);
76 name_acquired = TRUE;
77 }
78
79 static void
80 name_lost_cb (GDBusConnection *connection,
81 const gchar *name,
82 gpointer user_data)
83 {
84 /* Exit if we lost one of our bus names */
85 for (guint i = 0; i < G_N_ELEMENTS (bus_names); i++)
86 {
87 if (g_strcmp0 (name, bus_names[i]) == 0)
88 {
89 g_message ("Lost Bus Name: %s, exiting.", bus_names[i]);
90 name_acquired = FALSE;
91 g_main_loop_quit (main_loop);
92 }
93 }
94 }
95
96 static gboolean
97 wait_for_acquire_timeout_cb (gpointer data)
98 {
99 const gchar *bus_name = data;
100
101 if (!name_acquired)
102 {
103 exit_status = EXIT_FAILURE;
104 g_critical ("Failed to acquire %s on bus after %d seconds, exiting.",
105 bus_name, NAME_ACQUIRE_DELAY_SECS);
106 g_main_loop_quit (main_loop);
107 }
108
109 return G_SOURCE_REMOVE;
110 }
111
112 gint
113 main (gint argc,
114 gchar *argv[])
115 {
116 g_autoptr(GDBusConnection) bus = NULL;
117 g_autoptr(GError) error = NULL;
118 GBusType bus_type = G_BUS_TYPE_SYSTEM;
119
120 sysprof_clock_init ();
121
122 g_set_prgname ("sysprofd");
123 g_set_application_name ("sysprofd");
124
125 main_loop = g_main_loop_new (NULL, FALSE);
126
127 if ((bus = g_bus_get_sync (bus_type, NULL, &error)))
128 {
129 g_autoptr(IpcProfiler) rapl = ipc_rapl_profiler_new ();
130 g_autoptr(IpcService) v3_service = ipc_service_impl_new ();
131 g_autoptr(IpcUnwinder) unwinder = ipc_unwinder_impl_new ();
132
133 g_signal_connect (v3_service, "activity", G_CALLBACK (activity_cb), NULL);
134 g_signal_connect (rapl, "activity", G_CALLBACK (activity_cb), NULL);
135
136 activity_cb (NULL, NULL);
137
138 if (g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (v3_service), bus, V3_PATH, &error) &&
139 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (rapl), bus, RAPL_PATH, &error) &&
140 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (unwinder), bus, UNWINDER_PATH, &error))
141 {
142 for (guint i = 0; i < G_N_ELEMENTS (bus_names); i++)
143 {
144 g_bus_own_name_on_connection (bus,
145 bus_names[i],
146 (G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
147 G_BUS_NAME_OWNER_FLAGS_REPLACE),
148 name_acquired_cb,
149 name_lost_cb,
150 NULL,
151 NULL);
152 g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
153 NAME_ACQUIRE_DELAY_SECS,
154 wait_for_acquire_timeout_cb,
155 g_strdup (bus_names[i]),
156 g_free);
157 }
158
159 g_main_loop_run (main_loop);
160 g_main_loop_unref (main_loop);
161
162 return exit_status;
163 }
164 }
165
166 g_error ("Failed to setup system bus: %s", error->message);
167
168 g_main_loop_unref (main_loop);
169
170 return EXIT_FAILURE;
171 }