File: sp-gjs-source.c

package info (click to toggle)
sysprof 3.30.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,616 kB
  • sloc: ansic: 20,056; xml: 96; cpp: 23; sh: 18; makefile: 9
file content (245 lines) | stat: -rw-r--r-- 5,956 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* sp-gjs-source.c
 *
 * Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <signal.h>
#include <string.h>

#include "capture/sp-capture-reader.h"
#include "sources/sp-gjs-source.h"

struct _SpGjsSource
{
  GObject          parent_instance;

  SpCaptureWriter *writer;
  GArray          *pids;
  GArray          *enabled;
};

#define ENABLE_PROFILER  0x1
#define DISABLE_PROFILER 0x0

static void source_iface_init (SpSourceInterface *iface);

G_DEFINE_TYPE_EXTENDED (SpGjsSource, sp_gjs_source, G_TYPE_OBJECT, 0,
                        G_IMPLEMENT_INTERFACE (SP_TYPE_SOURCE, source_iface_init))

static void
sp_gjs_source_finalize (GObject *object)
{
  SpGjsSource *self = (SpGjsSource *)object;

  g_clear_pointer (&self->pids, g_array_unref);
  g_clear_pointer (&self->enabled, g_array_unref);
  g_clear_pointer (&self->writer, sp_capture_writer_unref);

  G_OBJECT_CLASS (sp_gjs_source_parent_class)->finalize (object);
}

static void
sp_gjs_source_class_init (SpGjsSourceClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = sp_gjs_source_finalize;
}

static void
sp_gjs_source_init (SpGjsSource *self)
{
  self->pids = g_array_new (FALSE, FALSE, sizeof (GPid));
  self->enabled = g_array_new (FALSE, FALSE, sizeof (GPid));
}

static void
sp_gjs_source_process_capture (SpGjsSource *self,
                               GPid         pid,
                               const gchar *path)
{
  g_autoptr(GError) error = NULL;
  g_autoptr(SpCaptureReader) reader = NULL;

  g_assert (SP_IS_GJS_SOURCE (self));
  g_assert (self->writer != NULL);
  g_assert (path != NULL);

  if (!(reader = sp_capture_reader_new (path, &error)))
    {
      g_warning ("Failed to load capture: %s", error->message);
      return;
    }

  if (!sp_capture_reader_splice (reader, self->writer, &error))
    {
      g_warning ("Failed to load capture: %s", error->message);
      return;
    }
}

static void
sp_gjs_source_process_captures (SpGjsSource *self)
{
  guint i;

  g_assert (SP_IS_GJS_SOURCE (self));
  g_assert (self->writer != NULL);

  for (i = 0; i < self->enabled->len; i++)
    {
      g_autofree gchar *filename = NULL;
      g_autofree gchar *path = NULL;
      GPid pid = g_array_index (self->enabled, GPid, i);

      filename = g_strdup_printf ("gjs-profile-%u", (guint)pid);
      path = g_build_filename (g_get_tmp_dir (), filename, NULL);

      sp_gjs_source_process_capture (self, pid, path);
    }
}

static void
sp_gjs_source_set_writer (SpSource        *source,
                          SpCaptureWriter *writer)
{
  SpGjsSource *self = (SpGjsSource *)source;

  g_assert (SP_IS_GJS_SOURCE (self));
  g_assert (writer != NULL);

  self->writer = sp_capture_writer_ref (writer);
}

static gboolean
pid_is_profileable (GPid pid)
{
  g_autofree gchar *path = NULL;
  g_autofree gchar *contents = NULL;
  const gchar *libgjs;
  gsize len = 0;

  g_assert (pid != -1);

  /*
   * Make sure this process has linked in libgjs. No sense in sending it a
   * signal unless we know it can handle it.
   */

  path = g_strdup_printf ("/proc/%d/maps", pid);
  if (!g_file_get_contents (path, &contents, &len, NULL))
    return FALSE;

  if (NULL == (libgjs = strstr (contents, "libgjs."G_MODULE_SUFFIX)))
    return FALSE;

  return TRUE;
}

static void
sp_gjs_source_enable_pid (SpGjsSource *self,
                          GPid         pid)
{
  union sigval si;

  g_assert (SP_IS_GJS_SOURCE (self));
  g_assert (pid != -1);

  si.sival_int = ENABLE_PROFILER;

  if (0 != sigqueue (pid, SIGUSR2, si))
    g_warning ("Failed to queue SIGUSR2 to pid %u", (guint)pid);
  else
    g_array_append_val (self->enabled, pid);
}

static void
sp_gjs_source_disable_pid (SpGjsSource *self,
                           GPid         pid)
{
  union sigval si;

  g_assert (SP_IS_GJS_SOURCE (self));
  g_assert (pid != -1);

  si.sival_int = DISABLE_PROFILER;

  if (0 != sigqueue (pid, SIGUSR2, si))
    g_warning ("Failed to queue SIGUSR2 to pid %u", (guint)pid);
}

static void
sp_gjs_source_start (SpSource *source)
{
  SpGjsSource *self = (SpGjsSource *)source;
  guint i;

  g_assert (SP_IS_GJS_SOURCE (self));

  for (i = 0; i < self->pids->len; i++)
    {
      GPid pid = g_array_index (self->pids, GPid, i);

      if (pid_is_profileable (pid))
        sp_gjs_source_enable_pid (self, pid);
    }
}

static void
sp_gjs_source_stop (SpSource *source)
{
  SpGjsSource *self = (SpGjsSource *)source;
  guint i;

  g_assert (SP_IS_GJS_SOURCE (self));

  for (i = 0; i < self->pids->len; i++)
    {
      GPid pid = g_array_index (self->pids, GPid, i);

      if (pid_is_profileable (pid))
        sp_gjs_source_disable_pid (self, pid);
    }

  sp_gjs_source_process_captures (self);
}

static void
sp_gjs_source_add_pid (SpSource *source,
                       GPid      pid)
{
  SpGjsSource *self = (SpGjsSource *)source;

  g_assert (SP_IS_GJS_SOURCE (self));
  g_assert (pid > -1);

  g_array_append_val (self->pids, pid);
}

static void
source_iface_init (SpSourceInterface *iface)
{
  iface->set_writer = sp_gjs_source_set_writer;
  iface->start = sp_gjs_source_start;
  iface->stop = sp_gjs_source_stop;
  iface->add_pid = sp_gjs_source_add_pid;
}

SpSource *
sp_gjs_source_new (void)
{
  return g_object_new (SP_TYPE_GJS_SOURCE, NULL);
}