Debian Codegraph @

src/libsysprof-capture/tests/allocs-by-size.c

raw
1 /* allocs-by-size.c
2 *
3 * Copyright 2020 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 #include "config.h"
22
23 #include <errno.h>
24 #include <glib/gi18n.h>
25 #include <locale.h>
26 #include <stddef.h>
27
28 #include <sysprof-capture.h>
29
30 typedef struct
31 {
32 gsize size;
33 gsize count;
34 gsize cmp;
35 } Item;
36
37 static gint
38 item_compare (gconstpointer a,
39 gconstpointer b)
40 {
41 const Item *item_a = a;
42 const Item *item_b = b;
43
44 if (item_a->cmp < item_b->cmp)
45 return -1;
46 else if (item_a->cmp > item_b->cmp)
47 return 1;
48 else
49 return 0;
50 }
51
52 static void
53 allocs_by_size (SysprofCaptureReader *reader)
54 {
55 SysprofCaptureFrameType type;
56 g_autoptr(GHashTable) allocs = NULL;
57 g_autoptr(GArray) ar = NULL;
58 GHashTableIter iter;
59 gpointer k,v;
60 gsize *count;
61
62 allocs = g_hash_table_new_full (NULL, NULL, NULL, g_free);
63 ar = g_array_new (FALSE, FALSE, sizeof (Item));
64
65 while (sysprof_capture_reader_peek_type (reader, &type))
66 {
67 if (type == SYSPROF_CAPTURE_FRAME_ALLOCATION)
68 {
69 const SysprofCaptureAllocation *ev = sysprof_capture_reader_read_allocation (reader);
70
71 if (ev == NULL)
72 break;
73
74 /* Ignore frees */
75 if (ev->alloc_size <= 0)
76 continue;
77
78 if (!(count = g_hash_table_lookup (allocs, GSIZE_TO_POINTER (ev->alloc_size))))
79 {
80 count = g_new0 (gsize, 1);
81 g_hash_table_insert (allocs, GSIZE_TO_POINTER (ev->alloc_size), count);
82 }
83
84 (*count)++;
85 }
86 else
87 {
88 if (!sysprof_capture_reader_skip (reader))
89 break;
90 }
91 }
92
93 g_hash_table_iter_init (&iter, allocs);
94 while (g_hash_table_iter_next (&iter, &k, &v))
95 {
96 const Item item = {
97 .size = GPOINTER_TO_SIZE (k),
98 .count = *(gsize *)v,
99 .cmp = *(gsize *)v * GPOINTER_TO_SIZE (k),
100 };
101
102 g_array_append_val (ar, item);
103 }
104
105 g_array_sort (ar, item_compare);
106
107 g_print ("alloc_size,total_alloc,n_allocs\n");
108
109 for (guint i = 0; i < ar->len; i++)
110 {
111 const Item *item = &g_array_index (ar, Item, i);
112
113 g_print ("%"G_GSIZE_FORMAT",%"G_GSIZE_FORMAT",%"G_GSIZE_FORMAT"\n",
114 item->size, item->cmp, item->count);
115 }
116 }
117
118 gint
119 main (gint argc,
120 gchar *argv[])
121 {
122 SysprofCaptureReader *reader;
123 const gchar *filename = argv[1];
124
125 if (argc < 2)
126 {
127 g_printerr ("usage: %s FILENAME\n", argv[0]);
128 return EXIT_FAILURE;
129 }
130
131 /* Set up gettext translations */
132 setlocale (LC_ALL, "");
133 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
134 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
135 textdomain (GETTEXT_PACKAGE);
136
137 if (!(reader = sysprof_capture_reader_new (filename)))
138 {
139 int errsv = errno;
140 g_printerr ("%s\n", g_strerror (errsv));
141 return EXIT_FAILURE;
142 }
143
144 allocs_by_size (reader);
145
146 sysprof_capture_reader_unref (reader);
147
148 return EXIT_SUCCESS;
149 }