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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
* Copyright (c) 2023 Canonical Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#undef NDEBUG
#include "cooperative-multitasking.h"
#include "cooperative-multitasking-private.h"
#include "openvswitch/hmap.h"
#include "ovstest.h"
#include "timeval.h"
#include "util.h"
#include "openvswitch/vlog.h"
struct fixture_arg {
bool called;
};
static void fixture_run_wrap(void *arg);
#define FIXTURE_RUN_NAME "fixture_run"
static void
fixture_run(struct fixture_arg *arg)
{
cooperative_multitasking_set(&fixture_run_wrap, (void *) arg,
time_msec(), 0, FIXTURE_RUN_NAME);
if (arg) {
arg->called = true;
}
}
static void
fixture_run_wrap(void *arg)
{
struct fixture_arg *fixture_arg = (struct fixture_arg *) arg;
fixture_run(fixture_arg);
}
static void fixture_other_run_wrap(void *arg);
#define FIXTURE_OTHER_RUN_NAME "fixture_other_run"
static void
fixture_other_run(struct fixture_arg *arg)
{
cooperative_multitasking_set(&fixture_other_run_wrap, (void *) arg,
time_msec(), 0, FIXTURE_OTHER_RUN_NAME);
if (arg) {
arg->called = true;
}
}
static void
fixture_other_run_wrap(void *arg)
{
struct fixture_arg *fixture_arg = (struct fixture_arg *) arg;
fixture_other_run(fixture_arg);
}
static void
test_cm_set_registration(void)
{
struct cm_entry *cm_entry;
struct fixture_arg arg1 = {
.called = false,
};
struct fixture_arg arg2 = {
.called = false,
};
timeval_stop();
long long int now = time_msec();
cooperative_multitasking_set(&fixture_run_wrap, (void *) &arg1, 0, 1000,
FIXTURE_RUN_NAME);
cooperative_multitasking_set(&fixture_run_wrap, (void *) &arg2, 0, 2000,
FIXTURE_RUN_NAME);
cooperative_multitasking_set(&fixture_other_run_wrap, NULL, 0, 3000,
FIXTURE_OTHER_RUN_NAME);
ovs_assert(hmap_count(&cooperative_multitasking_callbacks) == 3);
HMAP_FOR_EACH (cm_entry, node, &cooperative_multitasking_callbacks) {
if (cm_entry->arg == (void *) &arg1) {
ovs_assert(cm_entry->cb == &fixture_run_wrap);
ovs_assert(cm_entry->threshold == 1000);
ovs_assert(cm_entry->last_run == now);
} else if (cm_entry->arg == (void *) &arg2) {
ovs_assert(cm_entry->cb == &fixture_run_wrap);
ovs_assert(cm_entry->threshold == 2000);
ovs_assert(cm_entry->last_run == now);
} else if (cm_entry->cb == &fixture_other_run_wrap) {
ovs_assert(cm_entry->arg == NULL);
ovs_assert(cm_entry->threshold == 3000);
ovs_assert(cm_entry->last_run == now);
} else {
OVS_NOT_REACHED();
}
}
cooperative_multitasking_remove(&fixture_other_run_wrap, NULL);
ovs_assert(hmap_count(&cooperative_multitasking_callbacks) == 2);
cooperative_multitasking_remove(&fixture_run_wrap, (void *) &arg2);
ovs_assert(hmap_count(&cooperative_multitasking_callbacks) == 1);
cooperative_multitasking_destroy();
}
static void
test_cm_set_update(void)
{
struct cm_entry *cm_entry;
struct fixture_arg arg1 = {
.called = false,
};
struct fixture_arg arg2 = {
.called = false,
};
timeval_stop();
long long int now = time_msec();
/* First register a couple of callbacks. */
cooperative_multitasking_set(&fixture_run_wrap, (void *) &arg1, 0, 0,
FIXTURE_RUN_NAME);
cooperative_multitasking_set(&fixture_run_wrap, (void *) &arg2, 0, 0,
FIXTURE_RUN_NAME);
ovs_assert(hmap_count(&cooperative_multitasking_callbacks) == 2);
HMAP_FOR_EACH (cm_entry, node, &cooperative_multitasking_callbacks) {
if (cm_entry->arg == (void *) &arg1) {
ovs_assert(cm_entry->threshold == 0);
ovs_assert(cm_entry->last_run == now);
} else if (cm_entry->arg == (void *) &arg2) {
ovs_assert(cm_entry->threshold == 0);
ovs_assert(cm_entry->last_run == now);
} else {
OVS_NOT_REACHED();
}
}
/* Update 'last_run' and 'threshold' for each callback and validate
* that the correct entry was actually updated. */
cooperative_multitasking_set(&fixture_run_wrap, (void *) &arg1, 1, 2,
FIXTURE_RUN_NAME);
cooperative_multitasking_set(&fixture_run_wrap, (void *) &arg2, 3, 4,
FIXTURE_RUN_NAME);
HMAP_FOR_EACH (cm_entry, node, &cooperative_multitasking_callbacks) {
if (cm_entry->arg == (void *) &arg1) {
ovs_assert(cm_entry->threshold == 2);
ovs_assert(cm_entry->last_run == 1);
} else if (cm_entry->arg == (void *) &arg2) {
ovs_assert(cm_entry->threshold == 4);
ovs_assert(cm_entry->last_run == 3);
} else {
OVS_NOT_REACHED();
}
}
/* Confirm that providing 0 for 'last_run' or 'threshold' leaves the
* existing value untouched. */
cooperative_multitasking_set(&fixture_run_wrap, (void *) &arg1, 0, 5,
FIXTURE_RUN_NAME);
cooperative_multitasking_set(&fixture_run_wrap, (void *) &arg2, 6, 0,
FIXTURE_RUN_NAME);
HMAP_FOR_EACH (cm_entry, node, &cooperative_multitasking_callbacks) {
if (cm_entry->arg == (void *) &arg1) {
ovs_assert(cm_entry->threshold == 5);
ovs_assert(cm_entry->last_run == 1);
} else if (cm_entry->arg == (void *) &arg2) {
ovs_assert(cm_entry->threshold == 4);
ovs_assert(cm_entry->last_run == 6);
} else {
OVS_NOT_REACHED();
}
}
cooperative_multitasking_destroy();
}
static void
test_cm_yield(void)
{
struct cm_entry *cm_entry;
struct fixture_arg arg1 = {
.called = false,
};
struct fixture_arg arg2 = {
.called = false,
};
timeval_stop();
long long int now = time_msec();
/* First register a couple of callbacks. */
cooperative_multitasking_set(&fixture_run_wrap, (void *) &arg1, 0, 1000,
FIXTURE_RUN_NAME);
cooperative_multitasking_set(&fixture_run_wrap, (void *) &arg2, 0, 2000,
FIXTURE_RUN_NAME);
ovs_assert(hmap_count(&cooperative_multitasking_callbacks) == 2);
/* Call to yield should not execute callbacks until time threshold. */
cooperative_multitasking_yield();
ovs_assert(arg1.called == false);
ovs_assert(arg2.called == false);
HMAP_FOR_EACH (cm_entry, node, &cooperative_multitasking_callbacks) {
ovs_assert(cm_entry->last_run == now);
}
/* Move clock forward and confirm the expected callbacks to be executed. */
timeval_warp(1000);
timeval_stop();
cooperative_multitasking_yield();
ovs_assert(arg1.called == true);
ovs_assert(arg2.called == false);
/* Move clock forward and confirm the expected callbacks to be executed. */
arg1.called = arg2.called = false;
timeval_warp(1000);
timeval_stop();
cooperative_multitasking_yield();
ovs_assert(arg1.called == true);
ovs_assert(arg2.called == true);
cooperative_multitasking_destroy();
}
static void fixture_buggy_run_wrap(void *arg);
#define FIXTURE_BUGGY_RUN_NAME "fixture_buggy_run"
static void
fixture_buggy_run(struct fixture_arg *arg)
{
cooperative_multitasking_set(&fixture_buggy_run_wrap, (void *) arg,
time_msec(), 0, FIXTURE_BUGGY_RUN_NAME);
if (arg) {
arg->called = true;
}
/* A real run function MUST NOT directly or indirectly call yield, this is
* here to test the detection of such a programming error. */
cooperative_multitasking_yield();
}
static void
fixture_buggy_run_wrap(void *arg)
{
struct fixture_arg *fixture_arg = (struct fixture_arg *) arg;
fixture_buggy_run(fixture_arg);
}
static void
test_cooperative_multitasking_nested_yield(int argc OVS_UNUSED, char *argv[])
{
struct fixture_arg arg1 = {
.called = false,
};
set_program_name(argv[0]);
vlog_set_pattern(VLF_CONSOLE, "%c|%p|%m");
vlog_set_levels(NULL, VLF_SYSLOG, VLL_OFF);
time_msec(); /* Ensure timeval is initialized. */
cooperative_multitasking_set(&fixture_buggy_run_wrap, (void *) &arg1,
0, 1000, FIXTURE_BUGGY_RUN_NAME);
timeval_warp(1000);
cooperative_multitasking_yield();
cooperative_multitasking_destroy();
}
static void
test_cooperative_multitasking(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
{
time_msec(); /* Ensure timeval is initialized. */
test_cm_set_registration();
test_cm_set_update();
test_cm_yield();
}
OVSTEST_REGISTER("test-cooperative-multitasking",
test_cooperative_multitasking);
OVSTEST_REGISTER("test-cooperative-multitasking-nested-yield",
test_cooperative_multitasking_nested_yield);
|