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
|
/*
* Copyright (c) 2017 Balabit
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include <criterion/criterion.h>
#include "mainloop.h"
#include "scratch-buffers.h"
#include "stats/stats-registry.h"
#include <iv.h>
#define ITERATIONS 10
#define DEFAULT_ALLOC_SIZE 256L
static void
_do_something_with_a_gstring(GString *str)
{
g_string_assign(str, "foobar");
g_string_append(str, "len");
g_string_truncate(str, 0);
}
Test(scratch_buffers, alloc_returns_a_gstring_instance)
{
GString *str = scratch_buffers_alloc();
cr_assert_not_null(str);
_do_something_with_a_gstring(str);
cr_assert_eq(scratch_buffers_get_local_usage_count(), 1);
}
Test(scratch_buffers, reclaim_allocations_drops_all_allocs)
{
for (gint i = 0; i < ITERATIONS; i++)
{
GString *str = scratch_buffers_alloc();
cr_assert_not_null(str);
_do_something_with_a_gstring(str);
}
cr_assert_eq(scratch_buffers_get_local_usage_count(), ITERATIONS);
scratch_buffers_reclaim_allocations();
cr_assert_eq(scratch_buffers_get_local_usage_count(), 0);
}
Test(scratch_buffers, reclaim_marked_allocs)
{
ScratchBuffersMarker marker;
GString *str;
str = scratch_buffers_alloc();
cr_assert_not_null(str);
_do_something_with_a_gstring(str);
scratch_buffers_mark(&marker);
for (gint i = 0; i < ITERATIONS; i++)
{
str = scratch_buffers_alloc();
cr_assert_not_null(str);
_do_something_with_a_gstring(str);
}
cr_assert_eq(scratch_buffers_get_local_usage_count(), ITERATIONS + 1);
scratch_buffers_reclaim_marked(marker);
cr_assert_eq(scratch_buffers_get_local_usage_count(), 1);
}
Test(scratch_buffers, reclaim_up_to_a_marked_alloc)
{
ScratchBuffersMarker marker;
GString *str;
str = scratch_buffers_alloc();
cr_assert_not_null(str);
_do_something_with_a_gstring(str);
str = scratch_buffers_alloc_and_mark(&marker);
cr_assert_not_null(str);
_do_something_with_a_gstring(str);
for (gint i = 0; i < ITERATIONS; i++)
{
str = scratch_buffers_alloc();
cr_assert_not_null(str);
_do_something_with_a_gstring(str);
}
cr_assert_eq(scratch_buffers_get_local_usage_count(), ITERATIONS + 2);
scratch_buffers_reclaim_marked(marker);
cr_assert_eq(scratch_buffers_get_local_usage_count(), 1);
}
Test(scratch_buffers, local_usage_metrics_measure_allocs)
{
GString *str;
str = scratch_buffers_alloc();
cr_assert_not_null(str);
_do_something_with_a_gstring(str);
cr_assert_eq(scratch_buffers_get_local_usage_count(), 1);
cr_assert_eq(scratch_buffers_get_local_allocation_bytes(), DEFAULT_ALLOC_SIZE);
str = scratch_buffers_alloc();
cr_assert_not_null(str);
_do_something_with_a_gstring(str);
cr_assert_eq(scratch_buffers_get_local_usage_count(), 2);
cr_assert_eq(scratch_buffers_get_local_allocation_bytes(), 2*DEFAULT_ALLOC_SIZE);
}
/* not published via the header */
extern StatsCounterItem *stats_scratch_buffers_count;
extern StatsCounterItem *stats_scratch_buffers_bytes;
Test(scratch_buffers_stats, stats_counters_are_updated)
{
GString *str;
gint allocated_counter = 0;
for (gint i = 1; i <= ITERATIONS; i++)
{
str = scratch_buffers_alloc();
cr_assert_not_null(str);
allocated_counter++;
/* check through accessor functions */
cr_assert_eq(scratch_buffers_get_local_usage_count(), allocated_counter,
"get_local_usage_count() not returning proper value, value=%d, expected=%d",
scratch_buffers_get_local_usage_count(), allocated_counter);
cr_assert_eq(scratch_buffers_get_local_allocation_bytes(), allocated_counter * DEFAULT_ALLOC_SIZE,
"get_local_allocation_bytes() not returning proper value, value=%ld, expected=%ld",
scratch_buffers_get_local_allocation_bytes(), allocated_counter * DEFAULT_ALLOC_SIZE);
/* check through metrics */
cr_assert_eq(stats_counter_get(stats_scratch_buffers_count), allocated_counter,
"Statistic scratch_buffers_count is not updated properly, value=%d, expected=%d",
(gint) stats_counter_get(stats_scratch_buffers_count), allocated_counter);
/* check if byte counter is updated */
scratch_buffers_update_stats();
cr_assert_eq(stats_counter_get(stats_scratch_buffers_bytes), allocated_counter * DEFAULT_ALLOC_SIZE);
/* Check internal state is as expected */
cr_assert_eq(scratch_buffers_get_local_allocation_count(), allocated_counter,
"Local allocation count is not updated properly, value=%ld, expected=%d",
scratch_buffers_get_local_allocation_count(), allocated_counter);
}
scratch_buffers_explicit_gc();
cr_assert_eq(scratch_buffers_get_local_usage_count(), 0,
"get_local_usage_count() failed to reset to 0, value=%d, expected=%d",
scratch_buffers_get_local_usage_count(), 0);
cr_assert_eq(scratch_buffers_get_local_allocation_count(), allocated_counter,
"Local allocation count is not updated properly, value=%ld, expected=%d",
scratch_buffers_get_local_allocation_count(), allocated_counter);
cr_assert_eq(stats_counter_get(stats_scratch_buffers_count), allocated_counter,
"Statistic scratch_buffers_count should not be changed, value=%d, expected=%d",
(gint) stats_counter_get(stats_scratch_buffers_count), allocated_counter);
scratch_buffers_allocator_deinit();
cr_assert_eq(stats_counter_get(stats_scratch_buffers_count), 0,
"Statistic scratch_buffers_count failed to reset to 0, value=%ld, expected=%d",
stats_counter_get(stats_scratch_buffers_count), 0);
}
static void
setup(void)
{
iv_init();
main_loop_thread_resource_init();
stats_init();
scratch_buffers_global_init();
scratch_buffers_allocator_init();
scratch_buffers_register_stats();
}
static void
teardown(void)
{
scratch_buffers_explicit_gc();
scratch_buffers_unregister_stats();
scratch_buffers_allocator_deinit();
scratch_buffers_global_deinit();
stats_destroy();
iv_deinit();
}
static void
stats_test_teardown(void)
{
scratch_buffers_global_deinit();
stats_destroy();
}
TestSuite(scratch_buffers, .init = setup, .fini = teardown);
TestSuite(scratch_buffers_stats, .init = setup, .fini = stats_test_teardown);
|