tests: move tests to appropriate libraries

This commit is contained in:
Christian Hergert
2023-07-20 16:11:25 -07:00
parent b8f39ef20a
commit ff40f7d37d
8 changed files with 11 additions and 5 deletions

View File

@ -0,0 +1,149 @@
/* allocs-by-size.c
*
* Copyright 2020 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/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "config.h"
#include <errno.h>
#include <glib/gi18n.h>
#include <locale.h>
#include <stddef.h>
#include <sysprof-capture.h>
typedef struct
{
gsize size;
gsize count;
gsize cmp;
} Item;
static gint
item_compare (gconstpointer a,
gconstpointer b)
{
const Item *item_a = a;
const Item *item_b = b;
if (item_a->cmp < item_b->cmp)
return -1;
else if (item_a->cmp > item_b->cmp)
return 1;
else
return 0;
}
static void
allocs_by_size (SysprofCaptureReader *reader)
{
SysprofCaptureFrameType type;
g_autoptr(GHashTable) allocs = NULL;
g_autoptr(GArray) ar = NULL;
GHashTableIter iter;
gpointer k,v;
gsize *count;
allocs = g_hash_table_new_full (NULL, NULL, NULL, g_free);
ar = g_array_new (FALSE, FALSE, sizeof (Item));
while (sysprof_capture_reader_peek_type (reader, &type))
{
if (type == SYSPROF_CAPTURE_FRAME_ALLOCATION)
{
const SysprofCaptureAllocation *ev = sysprof_capture_reader_read_allocation (reader);
if (ev == NULL)
break;
/* Ignore frees */
if (ev->alloc_size <= 0)
continue;
if (!(count = g_hash_table_lookup (allocs, GSIZE_TO_POINTER (ev->alloc_size))))
{
count = g_new0 (gsize, 1);
g_hash_table_insert (allocs, GSIZE_TO_POINTER (ev->alloc_size), count);
}
(*count)++;
}
else
{
if (!sysprof_capture_reader_skip (reader))
break;
}
}
g_hash_table_iter_init (&iter, allocs);
while (g_hash_table_iter_next (&iter, &k, &v))
{
const Item item = {
.size = GPOINTER_TO_SIZE (k),
.count = *(gsize *)v,
.cmp = *(gsize *)v * GPOINTER_TO_SIZE (k),
};
g_array_append_val (ar, item);
}
g_array_sort (ar, item_compare);
g_print ("alloc_size,total_alloc,n_allocs\n");
for (guint i = 0; i < ar->len; i++)
{
const Item *item = &g_array_index (ar, Item, i);
g_print ("%"G_GSIZE_FORMAT",%"G_GSIZE_FORMAT",%"G_GSIZE_FORMAT"\n",
item->size, item->cmp, item->count);
}
}
gint
main (gint argc,
gchar *argv[])
{
SysprofCaptureReader *reader;
const gchar *filename = argv[1];
if (argc < 2)
{
g_printerr ("usage: %s FILENAME\n", argv[0]);
return EXIT_FAILURE;
}
/* Set up gettext translations */
setlocale (LC_ALL, "");
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
if (!(reader = sysprof_capture_reader_new (filename)))
{
int errsv = errno;
g_printerr ("%s\n", g_strerror (errsv));
return EXIT_FAILURE;
}
allocs_by_size (reader);
sysprof_capture_reader_unref (reader);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,139 @@
/* cross-thread-frees.c
*
* Copyright 2020 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/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "config.h"
#include <errno.h>
#include <glib.h>
#include <stddef.h>
#include <sysprof-capture.h>
typedef struct
{
gint tid;
guint n_addrs;
gint64 size;
SysprofCaptureAddress addrs[0];
} Stack;
static void
stack_free (gpointer ptr)
{
Stack *stack = ptr;
gsize size = sizeof *stack + (stack->n_addrs * sizeof (SysprofCaptureAddress));
g_slice_free1 (size, stack);
}
static Stack *
stack_new (gint tid,
gint64 size,
guint n_addrs,
const SysprofCaptureAddress *addrs)
{
Stack *stack;
stack = g_slice_alloc (sizeof *stack + (n_addrs * sizeof (SysprofCaptureAddress)));
stack->tid = tid;
stack->size = size;
stack->n_addrs = n_addrs;
for (guint i = 0; i < n_addrs; i++)
stack->addrs[i] = addrs[i];
return stack;
}
static void
cross_thread_frees (SysprofCaptureReader *reader)
{
SysprofCaptureFrameType type;
g_autoptr(GHashTable) stacks = NULL;
stacks = g_hash_table_new_full (NULL, NULL, NULL, stack_free);
while (sysprof_capture_reader_peek_type (reader, &type))
{
if (type == SYSPROF_CAPTURE_FRAME_ALLOCATION)
{
const SysprofCaptureAllocation *ev = sysprof_capture_reader_read_allocation (reader);
gpointer key;
if (ev == NULL)
break;
key = GINT_TO_POINTER (ev->alloc_addr);
if (ev->alloc_size > 0)
{
g_hash_table_insert (stacks,
key,
stack_new (ev->tid, ev->alloc_size, ev->n_addrs, ev->addrs));
}
else
{
Stack *stack;
stack = g_hash_table_lookup (stacks, key);
if (stack == NULL)
continue;
if (ev->tid != stack->tid)
{
g_print ("Alloc-Thread=%d Free-Thread=%d Size=%"G_GUINT64_FORMAT"\n",
stack->tid, ev->tid, stack->size);
}
g_hash_table_remove (stacks, key);
}
}
else
{
if (!sysprof_capture_reader_skip (reader))
break;
}
}
}
gint
main (gint argc,
gchar *argv[])
{
SysprofCaptureReader *reader;
const gchar *filename = argv[1];
if (argc < 2)
{
g_printerr ("usage: %s FILENAME\n", argv[0]);
return EXIT_FAILURE;
}
if (!(reader = sysprof_capture_reader_new (filename)))
{
int errsv = errno;
g_printerr ("%s\n", g_strerror (errsv));
return EXIT_FAILURE;
}
cross_thread_frees (reader);
sysprof_capture_reader_unref (reader);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,164 @@
/* find-temp-allocs.c
*
* Copyright 2020 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/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "config.h"
#include <errno.h>
#include <glib.h>
#include <sysprof-capture.h>
static struct {
gint64 total;
gint64 temp;
gint64 leaked;
} allocinfo;
typedef struct
{
gint pid;
gint tid;
gint64 time;
SysprofCaptureAddress addr;
gint64 size;
} Alloc;
static gint
compare_alloc (gconstpointer a,
gconstpointer b)
{
const Alloc *aptr = a;
const Alloc *bptr = b;
if (aptr->pid < bptr->pid)
return -1;
else if (aptr->pid > bptr->pid)
return 1;
if (aptr->tid < bptr->tid)
return -1;
else if (aptr->tid > bptr->tid)
return 1;
if (aptr->time < bptr->time)
return -1;
else if (aptr->time > bptr->time)
return 1;
if (aptr->addr < bptr->addr)
return -1;
else if (aptr->addr > bptr->addr)
return 1;
else
return 0;
}
static void
find_temp_allocs (SysprofCaptureReader *reader)
{
g_autoptr(GArray) ar = NULL;
SysprofCaptureFrameType type;
SysprofCaptureAddress last_addr = 0;
g_assert (reader != NULL);
ar = g_array_new (FALSE, FALSE, sizeof (Alloc));
while (sysprof_capture_reader_peek_type (reader, &type))
{
if (type == SYSPROF_CAPTURE_FRAME_ALLOCATION)
{
const SysprofCaptureAllocation *ev;
Alloc a;
if (!(ev = sysprof_capture_reader_read_allocation (reader)))
break;
a.pid = ev->frame.pid;
a.tid = ev->tid;
a.time = ev->frame.time;
a.addr = ev->alloc_addr;
a.size = ev->alloc_size;
g_array_append_val (ar, a);
}
else
{
if (!sysprof_capture_reader_skip (reader))
break;
}
}
/* Ensure items are in order because threads may have
* reordered things and raced to write out malloc data.
*/
g_array_sort (ar, compare_alloc);
for (guint i = 0; i < ar->len; i++)
{
const Alloc *a = &g_array_index (ar, Alloc, i);
if (a->size <= 0)
{
if (last_addr == a->addr)
allocinfo.temp++;
allocinfo.leaked--;
last_addr = 0;
}
else
{
allocinfo.total++;
allocinfo.leaked++;
last_addr = a->addr;
}
}
g_printerr ("Allocations: %"G_GINT64_FORMAT"\n", allocinfo.total);
g_printerr (" Temporary: %"G_GINT64_FORMAT" (%lf%%)\n",
allocinfo.temp, allocinfo.temp / (gdouble)allocinfo.total * 100.0);
g_printerr (" Leaked: %"G_GINT64_FORMAT"\n", allocinfo.leaked);
}
gint
main (gint argc,
gchar *argv[])
{
SysprofCaptureReader *reader;
const gchar *filename = argv[1];
if (argc < 2)
{
g_printerr ("usage: %s FILENAME\n", argv[0]);
return EXIT_FAILURE;
}
if (!(reader = sysprof_capture_reader_new (filename)))
{
int errsv = errno;
g_printerr ("%s\n", g_strerror (errsv));
return EXIT_FAILURE;
}
find_temp_allocs (reader);
sysprof_capture_reader_unref (reader);
return EXIT_SUCCESS;
}

View File

@ -15,8 +15,13 @@ libsysprof_capture_testsuite_c_args = [
]
libsysprof_capture_testsuite = {
'test-mapped-ring-buffer' : {},
'allocs-by-size' : {'skip': true},
'cross-thread-frees' : {'skip': true},
'find-temp-allocs' : {'skip': true},
'rewrite-pid' : {'skip': true},
'test-capture' : {},
'test-capture-cursor' : {},
'test-mapped-ring-buffer' : {},
}
libsysprof_capture_testsuite_deps = [

View File

@ -0,0 +1,91 @@
/* test-capture-cursor.c
*
* Copyright 2016o-2019 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/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <glib.h>
#include <glib/gstdio.h>
#include <sysprof-capture.h>
static bool
increment (const SysprofCaptureFrame *frame,
void *user_data)
{
gint *count= user_data;
(*count)++;
return true;
}
static void
test_cursor_basic (void)
{
SysprofCaptureReader *reader;
SysprofCaptureWriter *writer;
SysprofCaptureCursor *cursor;
gint64 t = SYSPROF_CAPTURE_CURRENT_TIME;
guint i;
gint r;
gint count = 0;
writer = sysprof_capture_writer_new ("capture-cursor-file", 0);
g_assert_nonnull (writer);
sysprof_capture_writer_flush (writer);
reader = sysprof_capture_reader_new ("capture-cursor-file");
g_assert_nonnull (reader);
for (i = 0; i < 100; i++)
{
r = sysprof_capture_writer_add_timestamp (writer, t, i, -1);
g_assert_cmpint (r, ==, TRUE);
}
sysprof_capture_writer_flush (writer);
cursor = sysprof_capture_cursor_new (reader);
sysprof_capture_cursor_foreach (cursor, increment, &count);
g_assert_cmpint (count, ==, 100);
g_clear_pointer (&cursor, sysprof_capture_cursor_unref);
sysprof_capture_reader_unref (reader);
sysprof_capture_writer_unref (writer);
g_unlink ("capture-cursor-file");
}
static void
test_cursor_null (void)
{
SysprofCaptureCursor *cursor = sysprof_capture_cursor_new (NULL);
gint count = 0;
sysprof_capture_cursor_foreach (cursor, increment, &count);
g_assert_cmpint (count, ==, 0);
g_clear_pointer (&cursor, sysprof_capture_cursor_unref);
}
int
main (int argc,
char *argv[])
{
sysprof_clock_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/SysprofCaptureCursor/basic", test_cursor_basic);
g_test_add_func ("/SysprofCaptureCursor/null", test_cursor_null);
return g_test_run ();
}

File diff suppressed because it is too large Load Diff