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
|
/*
* Copyright (c) 2024 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>
#include "backtrace.h"
#include "cooperative-multitasking-private.h"
#include "cooperative-multitasking.h"
#include "hash.h"
#include "openvswitch/hmap.h"
#include "openvswitch/vlog.h"
#include "timeval.h"
VLOG_DEFINE_THIS_MODULE(cooperative_multitasking);
struct hmap cooperative_multitasking_callbacks = HMAP_INITIALIZER(
&cooperative_multitasking_callbacks);
/* Free any data allocated by calls to cooperative_multitasking_set(). */
void
cooperative_multitasking_destroy(void)
{
struct cm_entry *cm_entry;
HMAP_FOR_EACH_SAFE (cm_entry, node, &cooperative_multitasking_callbacks) {
hmap_remove(&cooperative_multitasking_callbacks, &cm_entry->node);
free(cm_entry);
}
}
/* Set/update callback as identified by 'cb' and 'arg'.
*
* 'name' is used for logging events related to this callback.
*
* The value for 'last_run' must be updated each time the callback is run.
*
* Updating the value for 'threshold' may be necessary as a consequence of
* change in runtime configuration or requirements of the part of the program
* serviced by the callback.
*
* Providing a value of 0 for 'last_run' or 'threshold' will leave the stored
* value untouched. */
void
cooperative_multitasking_set(void (*cb)(void *), void *arg,
long long int last_run, long long int threshold,
const char *name)
{
struct cm_entry *cm_entry;
HMAP_FOR_EACH_WITH_HASH (cm_entry, node, hash_pointer((void *) cb, 0),
&cooperative_multitasking_callbacks) {
if (cm_entry->cb == cb && cm_entry->arg == arg) {
if (last_run) {
cm_entry->last_run = last_run;
}
if (threshold) {
cm_entry->threshold = threshold;
}
return;
}
}
cm_entry = xzalloc(sizeof *cm_entry);
cm_entry->cb = cb;
cm_entry->arg = arg;
cm_entry->threshold = threshold;
cm_entry->last_run = last_run ? last_run : time_msec();
cm_entry->name = name;
hmap_insert(&cooperative_multitasking_callbacks,
&cm_entry->node, hash_pointer((void *) cm_entry->cb, 0));
}
/* Remove callback identified by 'cb' and 'arg'. */
void
cooperative_multitasking_remove(void (*cb)(void *), void *arg)
{
struct cm_entry *cm_entry;
HMAP_FOR_EACH_WITH_HASH (cm_entry, node, hash_pointer((void *) cb, 0),
&cooperative_multitasking_callbacks) {
if (cm_entry->cb == cb && cm_entry->arg == arg) {
hmap_remove(&cooperative_multitasking_callbacks, &cm_entry->node);
free(cm_entry);
return;
}
}
}
static void
cooperative_multitasking_yield_at__(const char *source_location)
{
long long int start = time_msec();
struct cm_entry *cm_entry;
long long int elapsed;
bool warn;
HMAP_FOR_EACH (cm_entry, node, &cooperative_multitasking_callbacks) {
elapsed = time_msec() - cm_entry->last_run;
if (elapsed >= cm_entry->threshold) {
warn = elapsed - cm_entry->threshold > cm_entry->threshold / 8;
VLOG(warn ? VLL_WARN : VLL_DBG, "%s: yield for %s(%p): "
"elapsed(%lld) >= threshold(%lld), overrun: %lld",
source_location, cm_entry->name, cm_entry->arg, elapsed,
cm_entry->threshold, elapsed - cm_entry->threshold);
if (warn && VLOG_IS_DBG_ENABLED()) {
log_backtrace();
}
(*cm_entry->cb)(cm_entry->arg);
}
}
elapsed = time_msec() - start;
if (elapsed > 1000) {
VLOG_WARN("Unreasonably long %lldms runtime for callbacks.", elapsed);
}
}
/* Iterate over registered callbacks and execute callbacks as demanded by the
* recorded time threshold. */
void
cooperative_multitasking_yield_at(const char *source_location)
{
static bool yield_in_progress = false;
if (yield_in_progress) {
VLOG_ERR_ONCE("Nested yield avoided, this is a bug! "
"Enable debug logging for more details.");
if (VLOG_IS_DBG_ENABLED()) {
VLOG_DBG("%s: nested yield.", source_location);
log_backtrace();
}
return;
}
yield_in_progress = true;
cooperative_multitasking_yield_at__(source_location);
yield_in_progress = false;
}
|