2020-08-21 20:49:14 +00:00
|
|
|
#include <errno.h>
|
2020-01-21 22:49:40 +00:00
|
|
|
#include <stdio.h>
|
2020-01-22 20:55:31 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2020-08-21 20:49:14 +00:00
|
|
|
#include <sysprof.h>
|
2020-01-21 22:49:40 +00:00
|
|
|
#include <gio/gio.h>
|
|
|
|
|
|
|
|
typedef struct {
|
2020-02-05 05:45:35 +00:00
|
|
|
const char *mark;
|
|
|
|
const char *detail;
|
2020-02-05 05:23:13 +00:00
|
|
|
gboolean do_start;
|
|
|
|
gint64 start_time;
|
2020-01-21 22:49:40 +00:00
|
|
|
gint64 value;
|
|
|
|
} Data;
|
|
|
|
|
2020-07-04 18:40:21 +00:00
|
|
|
static bool
|
2020-01-21 22:49:40 +00:00
|
|
|
callback (const SysprofCaptureFrame *frame,
|
2020-07-04 18:40:21 +00:00
|
|
|
gpointer user_data)
|
2020-01-21 22:49:40 +00:00
|
|
|
{
|
|
|
|
Data *data = user_data;
|
|
|
|
|
|
|
|
if (frame->type == SYSPROF_CAPTURE_FRAME_MARK)
|
|
|
|
{
|
|
|
|
SysprofCaptureMark *mark = (SysprofCaptureMark *)frame;
|
|
|
|
if (strcmp (mark->group, "gtk") == 0 &&
|
2020-02-05 05:45:35 +00:00
|
|
|
strcmp (mark->name, data->mark) == 0 &&
|
|
|
|
(data->detail == NULL || strcmp (mark->message, data->detail) == 0))
|
2020-01-21 22:49:40 +00:00
|
|
|
{
|
2020-02-05 05:23:13 +00:00
|
|
|
if (data->do_start)
|
|
|
|
data->value = frame->time - data->start_time;
|
|
|
|
else
|
|
|
|
data->value = mark->duration;
|
2020-01-21 22:49:40 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MILLISECONDS(v) ((v) / (1000.0 * G_TIME_SPAN_MILLISECOND))
|
|
|
|
|
|
|
|
static int opt_rep = 10;
|
2020-01-22 05:29:43 +00:00
|
|
|
static char *opt_mark;
|
2020-02-05 05:45:35 +00:00
|
|
|
static char *opt_detail;
|
2020-01-22 20:55:31 +00:00
|
|
|
static char *opt_name;
|
|
|
|
static char *opt_output;
|
2020-02-05 05:23:13 +00:00
|
|
|
static gboolean opt_start_time;
|
2020-08-21 20:49:14 +00:00
|
|
|
static GMainLoop *main_loop;
|
|
|
|
static GError *failure;
|
2020-01-21 22:49:40 +00:00
|
|
|
|
|
|
|
static GOptionEntry options[] = {
|
2020-01-22 05:29:43 +00:00
|
|
|
{ "mark", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &opt_mark, "Name of the mark", "NAME" },
|
2020-02-05 05:45:35 +00:00
|
|
|
{ "detail", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &opt_detail, "Detail of the mark", "DETAIL" },
|
2020-02-05 05:23:13 +00:00
|
|
|
{ "start", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_start_time, "Measure the start time", NULL },
|
2020-01-22 20:55:31 +00:00
|
|
|
{ "runs", '0', G_OPTION_FLAG_NONE, G_OPTION_ARG_INT, &opt_rep, "Number of runs", "COUNT" },
|
|
|
|
{ "name", '0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &opt_name, "Name of this test", "NAME" },
|
|
|
|
{ "output", '0', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &opt_output, "Directory to save syscap files", "DIRECTORY" },
|
2020-01-21 22:49:40 +00:00
|
|
|
{ NULL, }
|
|
|
|
};
|
|
|
|
|
2020-08-21 20:49:14 +00:00
|
|
|
static gboolean
|
|
|
|
start_in_main (gpointer data)
|
|
|
|
{
|
|
|
|
SysprofProfiler *profiler = data;
|
|
|
|
sysprof_profiler_start (profiler);
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_failure_cb (SysprofProfiler *profiler,
|
|
|
|
const GError *error)
|
|
|
|
{
|
|
|
|
g_propagate_error (&failure, g_error_copy (error));
|
|
|
|
g_main_loop_quit (main_loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_stopped_cb (SysprofProfiler *profiler)
|
|
|
|
{
|
|
|
|
g_clear_error (&failure);
|
|
|
|
g_main_loop_quit (main_loop);
|
|
|
|
}
|
|
|
|
|
2020-01-21 22:49:40 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
GOptionContext *context;
|
|
|
|
GError *error = NULL;
|
|
|
|
Data data;
|
|
|
|
SysprofCaptureFrameType type;
|
|
|
|
gint64 *values;
|
|
|
|
gint64 min, max, total;
|
2020-01-29 10:05:24 +00:00
|
|
|
int count;
|
2020-01-22 20:55:31 +00:00
|
|
|
char *output_dir = NULL;
|
2020-08-21 20:49:14 +00:00
|
|
|
char **spawn_env;
|
|
|
|
char *workdir;
|
|
|
|
int i, j;
|
2020-01-21 22:49:40 +00:00
|
|
|
|
|
|
|
context = g_option_context_new ("COMMANDLINE");
|
|
|
|
g_option_context_add_main_entries (context, options, NULL);
|
|
|
|
if (!g_option_context_parse (context, &argc, &argv, &error))
|
|
|
|
g_error ("Parsing options: %s", error->message);
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
2020-02-05 05:23:13 +00:00
|
|
|
g_print ("Usage: %s [OPTIONS] COMMANDLINE\n", argv[0]);
|
2020-01-21 22:49:40 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt_rep < 1)
|
|
|
|
g_error ("COUNT must be a positive number");
|
|
|
|
|
2020-08-21 20:49:14 +00:00
|
|
|
main_loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
workdir = g_get_current_dir ();
|
|
|
|
|
|
|
|
spawn_env = g_get_environ ();
|
|
|
|
spawn_env = g_environ_setenv (spawn_env, "GTK_DEBUG_AUTO_QUIT", "1", TRUE);
|
|
|
|
|
2020-01-22 20:55:31 +00:00
|
|
|
if (opt_output)
|
|
|
|
{
|
|
|
|
GError *err = NULL;
|
|
|
|
GFile *file;
|
2020-05-15 13:18:35 +00:00
|
|
|
const char *subdir;
|
2020-01-22 20:55:31 +00:00
|
|
|
|
|
|
|
file = g_file_new_for_commandline_arg (opt_output);
|
2020-05-15 13:18:35 +00:00
|
|
|
|
|
|
|
subdir = g_getenv ("TEST_OUTPUT_SUBDIR");
|
|
|
|
if (subdir)
|
|
|
|
{
|
|
|
|
GFile *child = g_file_get_child (file, subdir);
|
|
|
|
g_object_unref (file);
|
|
|
|
file = child;
|
|
|
|
}
|
|
|
|
|
2020-01-22 20:55:31 +00:00
|
|
|
if (!g_file_make_directory_with_parents (file, NULL, &err))
|
|
|
|
{
|
|
|
|
if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_EXISTS))
|
|
|
|
g_error ("%s", err->message);
|
|
|
|
}
|
|
|
|
|
|
|
|
output_dir = g_file_get_path (file);
|
|
|
|
g_object_unref (file);
|
|
|
|
}
|
|
|
|
|
2020-01-29 10:05:24 +00:00
|
|
|
opt_rep++;
|
|
|
|
|
2020-01-21 22:49:40 +00:00
|
|
|
values = g_new (gint64, opt_rep);
|
|
|
|
|
|
|
|
for (i = 0; i < opt_rep; i++)
|
|
|
|
{
|
2020-08-21 20:49:14 +00:00
|
|
|
SysprofProfiler *profiler;
|
2020-01-21 22:49:40 +00:00
|
|
|
int fd;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *name;
|
2020-08-21 20:49:14 +00:00
|
|
|
SysprofCaptureWriter *writer;
|
2020-01-21 22:49:40 +00:00
|
|
|
SysprofCaptureReader *reader;
|
|
|
|
SysprofCaptureCursor *cursor;
|
|
|
|
SysprofCaptureCondition *condition;
|
2020-08-21 17:24:29 +00:00
|
|
|
char **child_argv;
|
2020-01-21 22:49:40 +00:00
|
|
|
|
2020-01-29 10:05:24 +00:00
|
|
|
fd = g_file_open_tmp ("gtk.XXXXXX.syscap", &name, &error);
|
|
|
|
if (error)
|
|
|
|
g_error ("Create syscap file: %s", error->message);
|
2020-01-21 22:49:40 +00:00
|
|
|
|
2020-08-21 20:49:14 +00:00
|
|
|
child_argv = g_new (char *, argc);
|
|
|
|
for (j = 0; j + 1 < argc; j++)
|
|
|
|
child_argv[j] = argv[j + 1];
|
|
|
|
child_argv[argc - 1] = NULL;
|
|
|
|
|
|
|
|
writer = sysprof_capture_writer_new_from_fd (fd, 0);
|
|
|
|
if (!writer)
|
|
|
|
g_error ("Failed to create capture writer: %s", g_strerror (errno));
|
|
|
|
|
|
|
|
profiler = sysprof_local_profiler_new ();
|
|
|
|
sysprof_profiler_set_whole_system (profiler, FALSE);
|
|
|
|
sysprof_profiler_set_spawn (profiler, TRUE);
|
|
|
|
sysprof_profiler_set_spawn_argv (profiler, (const char * const *)child_argv);
|
|
|
|
sysprof_profiler_set_spawn_cwd (profiler, workdir);
|
|
|
|
sysprof_profiler_set_spawn_env (profiler, (const char * const *)spawn_env);
|
|
|
|
sysprof_profiler_set_writer (profiler, writer);
|
|
|
|
|
|
|
|
sysprof_capture_writer_unref (writer);
|
2020-08-21 17:24:29 +00:00
|
|
|
g_free (child_argv);
|
|
|
|
|
2020-08-21 20:49:14 +00:00
|
|
|
g_signal_connect_swapped (profiler, "failed", G_CALLBACK (on_failure_cb), NULL);
|
|
|
|
g_signal_connect_swapped (profiler, "stopped", G_CALLBACK (on_stopped_cb), NULL);
|
2020-01-21 22:49:40 +00:00
|
|
|
|
2020-08-21 20:49:14 +00:00
|
|
|
g_idle_add (start_in_main, profiler);
|
|
|
|
g_main_loop_run (main_loop);
|
2020-05-15 13:18:35 +00:00
|
|
|
|
2020-08-21 20:49:14 +00:00
|
|
|
if (failure)
|
|
|
|
g_error ("Run child: %s", failure->message);
|
2020-01-21 22:49:40 +00:00
|
|
|
|
2020-08-21 20:49:14 +00:00
|
|
|
reader = sysprof_capture_writer_create_reader (writer);
|
2020-07-04 18:40:21 +00:00
|
|
|
if (!reader)
|
|
|
|
g_error ("Opening syscap file: %s", g_strerror (errno));
|
2020-01-21 22:49:40 +00:00
|
|
|
|
2020-08-21 20:49:14 +00:00
|
|
|
sysprof_capture_writer_unref (writer);
|
|
|
|
|
2020-02-05 05:45:35 +00:00
|
|
|
data.mark = opt_mark ? opt_mark : "css validation";
|
|
|
|
data.detail = opt_detail ? opt_detail : NULL;
|
2020-02-05 05:23:13 +00:00
|
|
|
data.do_start = opt_start_time;
|
|
|
|
data.start_time = sysprof_capture_reader_get_start_time (reader);
|
2020-01-21 22:49:40 +00:00
|
|
|
data.value = 0;
|
|
|
|
|
|
|
|
cursor = sysprof_capture_cursor_new (reader);
|
|
|
|
|
|
|
|
type = SYSPROF_CAPTURE_FRAME_MARK;
|
|
|
|
condition = sysprof_capture_condition_new_where_type_in (1, &type);
|
|
|
|
sysprof_capture_cursor_add_condition (cursor, condition);
|
|
|
|
|
|
|
|
sysprof_capture_cursor_foreach (cursor, callback, &data);
|
|
|
|
|
|
|
|
values[i] = data.value;
|
|
|
|
|
|
|
|
sysprof_capture_cursor_unref (cursor);
|
|
|
|
sysprof_capture_reader_unref (reader);
|
|
|
|
|
2020-01-22 20:55:31 +00:00
|
|
|
if (output_dir)
|
|
|
|
{
|
|
|
|
GFile *src, *dest;
|
|
|
|
char * save_to;
|
|
|
|
|
|
|
|
save_to = g_strdup_printf ("%s/%s.%d.syscap", output_dir, opt_name ? opt_name : "gtk", i);
|
|
|
|
|
|
|
|
src = g_file_new_for_path (name);
|
|
|
|
dest = g_file_new_for_path (save_to);
|
|
|
|
if (!g_file_copy (src, dest, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error))
|
|
|
|
g_error ("%s", error->message);
|
|
|
|
|
|
|
|
g_free (save_to);
|
|
|
|
g_object_unref (src);
|
|
|
|
g_object_unref (dest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
remove (name);
|
2020-01-21 22:49:40 +00:00
|
|
|
|
|
|
|
g_free (name);
|
2020-01-29 10:05:24 +00:00
|
|
|
|
|
|
|
/* A poor mans way to try and isolate the runs from each other */
|
|
|
|
g_usleep (300000);
|
2020-01-21 22:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 20:49:14 +00:00
|
|
|
g_free (workdir);
|
|
|
|
|
2020-01-21 22:49:40 +00:00
|
|
|
min = G_MAXINT64;
|
|
|
|
max = 0;
|
2020-01-29 10:05:24 +00:00
|
|
|
count = 0;
|
2020-01-21 22:49:40 +00:00
|
|
|
total = 0;
|
|
|
|
|
2020-01-29 10:05:24 +00:00
|
|
|
/* Ignore the first run, to avoid cache effects */
|
|
|
|
for (i = 1; i < opt_rep; i++)
|
2020-01-21 22:49:40 +00:00
|
|
|
{
|
|
|
|
if (min > values[i])
|
|
|
|
min = values[i];
|
|
|
|
if (max < values[i])
|
|
|
|
max = values[i];
|
2020-01-29 10:05:24 +00:00
|
|
|
count++;
|
2020-01-21 22:49:40 +00:00
|
|
|
total += values[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
g_print ("%d runs, min %g, max %g, avg %g\n",
|
2020-01-29 10:05:24 +00:00
|
|
|
count,
|
2020-01-21 22:49:40 +00:00
|
|
|
MILLISECONDS (min),
|
|
|
|
MILLISECONDS (max),
|
2020-01-29 10:05:24 +00:00
|
|
|
MILLISECONDS (total / count));
|
2020-01-21 22:49:40 +00:00
|
|
|
}
|