Land Sysprof 2.x

This is a major redesign a modernization of Sysprof. The core data
structures and design are largely the same, but it has been ported to
Gtk3 and has lots of additions that should make your profiling experience
smoother. Especially for those that are new to profiling.

There are some very simple help docs added, but we really need the
experts to come in and write some documentation here.
This commit is contained in:
Christian Hergert
2016-04-13 05:24:03 -07:00
parent 34db28db32
commit 29c4ec495f
231 changed files with 35471 additions and 24788 deletions

37
tests/Makefile.am Normal file
View File

@ -0,0 +1,37 @@
TESTS =
test_cflags = \
$(SYSPROF_CFLAGS) \
-I$(top_srcdir)/lib \
-I$(top_builddir)/lib
test_libs = \
$(SYSPROF_LIBS) \
$(top_builddir)/lib/libsysprof-@API_VERSION@.la
TESTS += test-model-filter
test_model_filter_SOURCES = test-model-filter.c
test_model_filter_CFLAGS = $(test_cflags)
test_model_filter_LDADD = $(test_libs)
TESTS += test-capture
test_capture_SOURCES = test-capture.c
test_capture_CFLAGS = $(test_cflags)
test_capture_LDADD = $(test_libs)
TEST_ENVIRONMENT = \
G_TEST_SRCDIR="$(abs_srcdir)" \
G_TEST_BUILDDIR="$(abs_builddir)" \
G_DEBUG=gc-friendly \
GSETTINGS_BACKEND=memory \
MALLOC_CHECK_=2 \
MALLOC_PERTURB_=$$(($${RANDOM:-256} % 256))
TEST_PROGS = test-model-filter
LOG_COMPILER = $(top_srcdir)/build-aux/tap-test
noinst_PROGRAMS = $(TESTS)
check_PROGRAMS = $(TESTS)
-include $(top_srcdir)/git.mk

455
tests/test-capture.c Normal file
View File

@ -0,0 +1,455 @@
/* test-capture.c
*
* Copyright (C) 2016 Christian Hergert <christian@hergert.me>
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This file 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
* Lesser 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 <glib/gstdio.h>
#include "sp-capture-reader.h"
#include "sp-capture-writer.h"
static void
test_reader_basic (void)
{
SpCaptureReader *reader;
SpCaptureWriter *writer;
GError *error = NULL;
gint64 t = g_get_monotonic_time ();
guint i;
gint r;
writer = sp_capture_writer_new ("capture-file", 0);
g_assert (writer != NULL);
reader = sp_capture_reader_new ("capture-file", &error);
g_assert_no_error (error);
g_assert (reader != NULL);
for (i = 0; i < 100; i++)
{
gchar str[16];
g_snprintf (str, sizeof str, "%d", i);
r = sp_capture_writer_add_map (writer, t, -1, -1, i, i + 1, i + 2, i + 3, str);
g_assert_cmpint (r, ==, TRUE);
}
sp_capture_writer_flush (writer);
for (i = 0; i < 100; i++)
{
SpCaptureFrameType type = -1;
const SpCaptureMap *map;
gchar str[16];
g_snprintf (str, sizeof str, "%d", i);
if (!sp_capture_reader_peek_type (reader, &type))
g_assert_not_reached ();
g_assert_cmpint (type, ==, SP_CAPTURE_FRAME_MAP);
map = sp_capture_reader_read_map (reader);
g_assert (map != NULL);
g_assert_cmpint (map->frame.pid, ==, -1);
g_assert_cmpint (map->frame.cpu, ==, -1);
g_assert_cmpint (map->start, ==, i);
g_assert_cmpint (map->end, ==, i + 1);
g_assert_cmpint (map->offset, ==, i + 2);
g_assert_cmpint (map->inode, ==, i + 3);
g_assert_cmpstr (map->filename, ==, str);
}
for (i = 0; i < 100; i++)
{
r = sp_capture_writer_add_timestamp (writer, t, i, -1);
g_assert_cmpint (r, ==, TRUE);
}
sp_capture_writer_flush (writer);
for (i = 0; i < 100; i++)
{
SpCaptureFrameType type = -1;
const SpCaptureTimestamp *ts;
if (!sp_capture_reader_peek_type (reader, &type))
g_assert_not_reached ();
g_assert_cmpint (type, ==, SP_CAPTURE_FRAME_TIMESTAMP);
ts = sp_capture_reader_read_timestamp (reader);
g_assert (ts != NULL);
g_assert_cmpint (ts->frame.cpu, ==, i);
g_assert_cmpint (ts->frame.pid, ==, -1);
}
for (i = 0; i < 100; i++)
{
r = sp_capture_writer_add_exit (writer, t, i, -1);
g_assert_cmpint (r, ==, TRUE);
}
sp_capture_writer_flush (writer);
for (i = 0; i < 100; i++)
{
SpCaptureFrameType type = -1;
const SpCaptureExit *ex;
if (!sp_capture_reader_peek_type (reader, &type))
g_assert_not_reached ();
g_assert_cmpint (type, ==, SP_CAPTURE_FRAME_EXIT);
ex = sp_capture_reader_read_exit (reader);
g_assert (ex != NULL);
g_assert_cmpint (ex->frame.cpu, ==, i);
g_assert_cmpint (ex->frame.pid, ==, -1);
}
for (i = 0; i < 100; i++)
{
char cmdline[32];
g_snprintf (cmdline, sizeof cmdline, "program-%d", i);
r = sp_capture_writer_add_process (writer, t, -1, i, cmdline);
g_assert_cmpint (r, ==, TRUE);
}
sp_capture_writer_flush (writer);
for (i = 0; i < 100; i++)
{
SpCaptureFrameType type = -1;
const SpCaptureProcess *pr;
char str[32];
g_snprintf (str, sizeof str, "program-%d", i);
if (!sp_capture_reader_peek_type (reader, &type))
g_assert_not_reached ();
g_assert_cmpint (type, ==, SP_CAPTURE_FRAME_PROCESS);
pr = sp_capture_reader_read_process (reader);
g_assert (pr != NULL);
g_assert_cmpint (pr->frame.cpu, ==, -1);
g_assert_cmpint (pr->frame.pid, ==, i);
g_assert_cmpstr (pr->cmdline, ==, str);
}
for (i = 0; i < 100; i++)
{
r = sp_capture_writer_add_fork (writer, t, i, -1, i);
g_assert_cmpint (r, ==, TRUE);
}
sp_capture_writer_flush (writer);
for (i = 0; i < 100; i++)
{
SpCaptureFrameType type = -1;
const SpCaptureFork *ex;
if (!sp_capture_reader_peek_type (reader, &type))
g_assert_not_reached ();
g_assert_cmpint (type, ==, SP_CAPTURE_FRAME_FORK);
ex = sp_capture_reader_read_fork (reader);
g_assert (ex != NULL);
g_assert_cmpint (ex->frame.cpu, ==, i);
g_assert_cmpint (ex->frame.pid, ==, -1);
g_assert_cmpint (ex->child_pid, ==, i);
}
for (i = 0; i < 1000; i++)
{
SpCaptureAddress addr;
gchar str[32];
g_snprintf (str, sizeof str, "jitstring-%d", i);
addr = sp_capture_writer_add_jitmap (writer, str);
g_assert_cmpint (addr, ==, (i + 1) | SP_CAPTURE_JITMAP_MARK);
}
sp_capture_writer_flush (writer);
i = 0;
for (;;)
{
SpCaptureFrameType type = -1;
g_autoptr(GHashTable) ret = NULL;
if (sp_capture_reader_peek_type (reader, &type))
g_assert_cmpint (type, ==, SP_CAPTURE_FRAME_JITMAP);
else
break;
ret = sp_capture_reader_read_jitmap (reader);
g_assert (ret != NULL);
i += g_hash_table_size (ret);
}
g_assert_cmpint (1000, ==, i);
{
SpCaptureFrameType type = -1;
if (sp_capture_reader_peek_type (reader, &type))
g_assert_not_reached ();
}
for (i = 1; i <= 1000; i++)
{
SpCaptureAddress *addrs;
guint j;
addrs = alloca (i * sizeof *addrs);
for (j = 0; j < i; j++)
addrs[j] = i;
if (!sp_capture_writer_add_sample (writer, t, -1, -1, addrs, i))
g_assert_not_reached ();
}
sp_capture_writer_flush (writer);
for (i = 1; i <= 1000; i++)
{
SpCaptureFrameType type = -1;
const SpCaptureSample *sample;
guint j;
if (!sp_capture_reader_peek_type (reader, &type))
g_assert_not_reached ();
g_assert_cmpint (type, ==, SP_CAPTURE_FRAME_SAMPLE);
sample = sp_capture_reader_read_sample (reader);
g_assert (sample != NULL);
g_assert_cmpint (sample->frame.time, ==, t);
g_assert_cmpint (sample->frame.cpu, ==, -1);
g_assert_cmpint (sample->frame.pid, ==, -1);
g_assert_cmpint (sample->n_addrs, ==, i);
for (j = 0; j < i; j++)
g_assert_cmpint (sample->addrs[j], ==, i);
}
{
SpCaptureFrameType type = -1;
if (sp_capture_reader_peek_type (reader, &type))
g_assert_not_reached ();
}
r = sp_capture_writer_save_as (writer, "capture-file.bak", &error);
g_assert_no_error (error);
g_assert (r);
g_assert (g_file_test ("capture-file.bak", G_FILE_TEST_IS_REGULAR));
g_clear_pointer (&writer, sp_capture_writer_unref);
g_clear_pointer (&reader, sp_capture_reader_unref);
reader = sp_capture_reader_new ("capture-file.bak", &error);
g_assert_no_error (error);
g_assert (reader != NULL);
for (i = 0; i < 2; i++)
{
SpCaptureFrameType type = -1;
guint count = 0;
while (sp_capture_reader_peek_type (reader, &type))
{
count++;
if (!sp_capture_reader_skip (reader))
g_assert_not_reached ();
}
g_assert_cmpint (count, >, 1500);
sp_capture_reader_reset (reader);
}
sp_capture_reader_unref (reader);
g_unlink ("capture-file");
g_unlink ("capture-file.bak");
}
static void
test_writer_splice (void)
{
SpCaptureWriter *writer1;
SpCaptureWriter *writer2;
SpCaptureReader *reader;
SpCaptureFrameType type;
GError *error = NULL;
guint i;
gint r;
writer1 = sp_capture_writer_new ("writer1.syscap", 0);
writer2 = sp_capture_writer_new ("writer2.syscap", 0);
for (i = 0; i < 1000; i++)
sp_capture_writer_add_timestamp (writer1, SP_CAPTURE_CURRENT_TIME, -1, -1);
r = sp_capture_writer_splice (writer1, writer2, &error);
g_assert_no_error (error);
g_assert (r == TRUE);
g_clear_pointer (&writer1, sp_capture_writer_unref);
g_clear_pointer (&writer2, sp_capture_writer_unref);
reader = sp_capture_reader_new ("writer2.syscap", &error);
g_assert_no_error (error);
g_assert (reader != NULL);
for (i = 0; i < 1000; i++)
{
const SpCaptureTimestamp *ts = sp_capture_reader_read_timestamp (reader);
g_assert (ts != NULL);
g_assert_cmpint (ts->frame.cpu, ==, -1);
g_assert_cmpint (ts->frame.pid, ==, -1);
g_assert_cmpint (ts->frame.time, >, 0);
}
r = sp_capture_reader_peek_type (reader, &type);
g_assert_cmpint (r, ==, FALSE);
g_clear_pointer (&reader, sp_capture_reader_unref);
g_unlink ("writer1.syscap");
g_unlink ("writer2.syscap");
}
static void
test_reader_splice (void)
{
SpCaptureWriter *writer1;
SpCaptureWriter *writer2;
SpCaptureReader *reader;
SpCaptureFrameType type;
GError *error = NULL;
guint i;
guint count;
gint r;
writer1 = sp_capture_writer_new ("writer1.syscap", 0);
writer2 = sp_capture_writer_new ("writer2.syscap", 0);
for (i = 0; i < 1000; i++)
sp_capture_writer_add_timestamp (writer1, SP_CAPTURE_CURRENT_TIME, -1, -1);
r = sp_capture_writer_flush (writer1);
g_assert_cmpint (r, ==, TRUE);
g_clear_pointer (&writer1, sp_capture_writer_unref);
reader = sp_capture_reader_new ("writer1.syscap", &error);
g_assert_no_error (error);
g_assert (reader != NULL);
/* advance to the end of the reader to non-start boundary for fd */
for (i = 0; i < 1000; i++)
{
const SpCaptureTimestamp *ts = sp_capture_reader_read_timestamp (reader);
g_assert (ts != NULL);
g_assert_cmpint (ts->frame.cpu, ==, -1);
g_assert_cmpint (ts->frame.pid, ==, -1);
g_assert_cmpint (ts->frame.time, >, 0);
}
r = sp_capture_reader_peek_type (reader, &type);
g_assert_cmpint (r, ==, FALSE);
r = sp_capture_reader_splice (reader, writer2, &error);
g_assert_no_error (error);
g_assert_cmpint (r, ==, TRUE);
g_clear_pointer (&reader, sp_capture_reader_unref);
g_clear_pointer (&writer2, sp_capture_writer_unref);
reader = sp_capture_reader_new ("writer2.syscap", 0);
for (i = 0; i < 1000; i++)
{
const SpCaptureTimestamp *ts = sp_capture_reader_read_timestamp (reader);
g_assert (ts != NULL);
g_assert_cmpint (ts->frame.cpu, ==, -1);
g_assert_cmpint (ts->frame.pid, ==, -1);
g_assert_cmpint (ts->frame.time, >, 0);
}
r = sp_capture_reader_peek_type (reader, &type);
g_assert_cmpint (r, ==, FALSE);
g_clear_pointer (&reader, sp_capture_reader_unref);
reader = sp_capture_reader_new ("writer2.syscap", &error);
g_assert_no_error (error);
g_assert (reader != NULL);
r = sp_capture_reader_save_as (reader, "writer3.syscap", &error);
g_assert_no_error (error);
g_assert_cmpint (r, ==, TRUE);
g_clear_pointer (&reader, sp_capture_reader_unref);
reader = sp_capture_reader_new ("writer3.syscap", &error);
g_assert_no_error (error);
g_assert (reader != NULL);
count = 0;
while (sp_capture_reader_skip (reader))
count++;
g_assert_cmpint (count, ==, 1000);
g_clear_pointer (&reader, sp_capture_reader_unref);
g_unlink ("writer1.syscap");
g_unlink ("writer2.syscap");
g_unlink ("writer3.syscap");
}
int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/SpCapture/ReaderWriter", test_reader_basic);
g_test_add_func ("/SpCapture/Writer/splice", test_writer_splice);
g_test_add_func ("/SpCapture/Reader/splice", test_reader_splice);
return g_test_run ();
}

119
tests/test-model-filter.c Normal file
View File

@ -0,0 +1,119 @@
#include <sysprof.h>
#include "sp-model-filter.h"
#define TEST_TYPE_ITEM (test_item_get_type())
struct _TestItem
{
GObject p;
guint n;
};
G_DECLARE_FINAL_TYPE (TestItem, test_item, TEST, ITEM, GObject)
G_DEFINE_TYPE (TestItem, test_item, G_TYPE_OBJECT)
static void
test_item_class_init (TestItemClass *klass)
{
}
static void
test_item_init (TestItem *self)
{
}
static TestItem *
test_item_new (guint n)
{
TestItem *item;
item = g_object_new (TEST_TYPE_ITEM, NULL);
item->n = n;
return item;
}
static gboolean
filter_func1 (GObject *object,
gpointer user_data)
{
return (TEST_ITEM (object)->n & 1) == 0;
}
static gboolean
filter_func2 (GObject *object,
gpointer user_data)
{
return (TEST_ITEM (object)->n & 1) == 1;
}
static void
test_basic (void)
{
GListStore *model;
SpModelFilter *filter;
TestItem *item;
guint i;
model = g_list_store_new (TEST_TYPE_ITEM);
g_assert (model);
for (i = 0; i < 1000; i++)
{
g_autoptr(TestItem) val = test_item_new (i);
g_list_store_append (model, val);
}
g_assert_cmpint (1000, ==, g_list_model_get_n_items (G_LIST_MODEL (model)));
filter = sp_model_filter_new (G_LIST_MODEL (model));
g_assert (filter);
g_assert_cmpint (1000, ==, g_list_model_get_n_items (G_LIST_MODEL (filter)));
sp_model_filter_set_filter_func (filter, filter_func1, NULL, NULL);
g_assert_cmpint (500, ==, g_list_model_get_n_items (G_LIST_MODEL (filter)));
for (i = 0; i < 1000; i += 2)
g_list_store_remove (model, 998 - i);
g_assert_cmpint (500, ==, g_list_model_get_n_items (G_LIST_MODEL (model)));
g_assert_cmpint (0, ==, g_list_model_get_n_items (G_LIST_MODEL (filter)));
sp_model_filter_set_filter_func (filter, NULL, NULL, NULL);
g_assert_cmpint (500, ==, g_list_model_get_n_items (G_LIST_MODEL (filter)));
sp_model_filter_set_filter_func (filter, filter_func2, NULL, NULL);
g_assert_cmpint (500, ==, g_list_model_get_n_items (G_LIST_MODEL (filter)));
g_list_store_append (model, test_item_new (1001));
for (i = 0; i < 500; i++)
g_list_store_remove (model, 0);
g_assert_cmpint (1, ==, g_list_model_get_n_items (G_LIST_MODEL (model)));
g_assert_cmpint (1, ==, g_list_model_get_n_items (G_LIST_MODEL (filter)));
sp_model_filter_set_filter_func (filter, NULL, NULL, NULL);
g_assert_cmpint (1, ==, g_list_model_get_n_items (G_LIST_MODEL (model)));
g_assert_cmpint (1, ==, g_list_model_get_n_items (G_LIST_MODEL (filter)));
item = g_list_model_get_item (G_LIST_MODEL (filter), 0);
g_assert (item);
g_assert (TEST_IS_ITEM (item));
g_assert_cmpint (item->n, ==, 1001);
g_clear_object (&item);
g_clear_object (&model);
g_clear_object (&filter);
}
gint
main (gint argc,
gchar *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/SpModelFilter/basic", test_basic);
return g_test_run ();
}