File: lflow-cache.c

package info (click to toggle)
ovn 25.09.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,492 kB
  • sloc: ansic: 106,060; xml: 23,314; sh: 3,322; python: 1,838; makefile: 836
file content (419 lines) | stat: -rw-r--r-- 11,810 bytes parent folder | download | duplicates (3)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Copyright (c) 2015, 2016 Nicira, Inc.
 * Copyright (c) 2021, Red Hat, Inc.
 *
 * 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>

#if HAVE_DECL_MALLOC_TRIM
#include <malloc.h>
#endif

#include "coverage.h"
#include "lflow-cache.h"
#include "lib/uuid.h"
#include "memory-trim.h"
#include "openvswitch/vlog.h"
#include "ovn/expr.h"

VLOG_DEFINE_THIS_MODULE(lflow_cache);

COVERAGE_DEFINE(lflow_cache_flush);
COVERAGE_DEFINE(lflow_cache_add_expr);
COVERAGE_DEFINE(lflow_cache_add_matches);
COVERAGE_DEFINE(lflow_cache_free_expr);
COVERAGE_DEFINE(lflow_cache_free_matches);
COVERAGE_DEFINE(lflow_cache_add);
COVERAGE_DEFINE(lflow_cache_hit);
COVERAGE_DEFINE(lflow_cache_miss);
COVERAGE_DEFINE(lflow_cache_delete);
COVERAGE_DEFINE(lflow_cache_full);
COVERAGE_DEFINE(lflow_cache_mem_full);
COVERAGE_DEFINE(lflow_cache_made_room);
COVERAGE_DEFINE(lflow_cache_trim);

static const char *lflow_cache_type_names[LCACHE_T_MAX] = {
    [LCACHE_T_EXPR]    = "cache-expr",
    [LCACHE_T_MATCHES] = "cache-matches",
};

static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);

struct lflow_cache {
    struct hmap entries[LCACHE_T_MAX];
    struct memory_trimmer *mt;
    uint32_t n_entries;
    uint32_t high_watermark;
    uint32_t capacity;
    uint64_t mem_usage;
    uint64_t max_mem_usage;
    uint32_t trim_limit;
    uint32_t trim_wmark_perc;
    uint64_t trim_count;
    bool enabled;
};

struct lflow_cache_entry {
    struct hmap_node node;
    struct uuid lflow_uuid; /* key */
    size_t size;

    struct lflow_cache_value value;
};

static bool lflow_cache_make_room__(struct lflow_cache *lc,
                                    enum lflow_cache_type type);
static struct lflow_cache_value *lflow_cache_add__(
    struct lflow_cache *lc, const struct uuid *lflow_uuid,
    enum lflow_cache_type type, uint64_t value_size);
static void lflow_cache_delete__(struct lflow_cache *lc,
                                 struct lflow_cache_entry *lce);
static void lflow_cache_trim__(struct lflow_cache *lc, bool force);

struct lflow_cache *
lflow_cache_create(void)
{
    struct lflow_cache *lc = xzalloc(sizeof *lc);

    for (size_t i = 0; i < LCACHE_T_MAX; i++) {
        hmap_init(&lc->entries[i]);
    }
    lc->mt = memory_trimmer_create();

    return lc;
}

void
lflow_cache_flush(struct lflow_cache *lc)
{
    if (!lc) {
        return;
    }

    COVERAGE_INC(lflow_cache_flush);
    for (size_t i = 0; i < LCACHE_T_MAX; i++) {
        struct lflow_cache_entry *lce;

        HMAP_FOR_EACH_SAFE (lce, node, &lc->entries[i]) {
            lflow_cache_delete__(lc, lce);
        }
    }
    lflow_cache_trim__(lc, true);
}

void
lflow_cache_destroy(struct lflow_cache *lc)
{
    if (!lc) {
        return;
    }

    lflow_cache_flush(lc);
    for (size_t i = 0; i < LCACHE_T_MAX; i++) {
        hmap_destroy(&lc->entries[i]);
    }
    memory_trimmer_destroy(lc->mt);
    free(lc);
}

void
lflow_cache_enable(struct lflow_cache *lc, bool enabled, uint32_t capacity,
                   uint64_t max_mem_usage_kb, uint32_t lflow_trim_limit,
                   uint32_t trim_wmark_perc, uint32_t trim_timeout_ms)
{
    if (!lc) {
        return;
    }

    if (trim_wmark_perc > 100) {
        VLOG_WARN_RL(&rl, "Invalid requested trim watermark percentage: "
                     "requested %"PRIu32", using 100 instead",
                     trim_wmark_perc);
        trim_wmark_perc = 100;
    }

    uint64_t max_mem_usage = max_mem_usage_kb * 1024;
    bool need_flush = false;
    bool need_trim = false;

    if ((lc->enabled && !enabled)
            || capacity < lc->n_entries
            || max_mem_usage < lc->mem_usage) {
        need_flush = true;
    } else if (lc->enabled
                    && (lc->trim_limit != lflow_trim_limit
                        || lc->trim_wmark_perc != trim_wmark_perc)) {
        need_trim = true;
    }

    lc->enabled = enabled;
    lc->capacity = capacity;
    lc->max_mem_usage = max_mem_usage;
    lc->trim_limit = lflow_trim_limit;
    lc->trim_wmark_perc = trim_wmark_perc;
    memory_trimmer_set(lc->mt, trim_timeout_ms);

    if (need_flush) {
        memory_trimmer_record_activity(lc->mt);
        lflow_cache_flush(lc);
    } else if (need_trim) {
        memory_trimmer_record_activity(lc->mt);
        lflow_cache_trim__(lc, false);
    }
}

bool
lflow_cache_is_enabled(const struct lflow_cache *lc)
{
    return lc && lc->enabled;
}

void
lflow_cache_get_stats(const struct lflow_cache *lc, struct ds *output)
{
    if (!output) {
        return;
    }

    if (!lc) {
        ds_put_cstr(output, "Invalid arguments.");
        return;
    }

    ds_put_format(output, "Enabled: %s\n",
                  lflow_cache_is_enabled(lc) ? "true" : "false");
    ds_put_format(output, "%-16s: %"PRIu32"\n", "high-watermark",
                  lc->high_watermark);
    ds_put_format(output, "%-16s: %"PRIu32"\n", "total", lc->n_entries);
    for (size_t i = 0; i < LCACHE_T_MAX; i++) {
        ds_put_format(output, "%-16s: %"PRIuSIZE"\n",
                      lflow_cache_type_names[i],
                      hmap_count(&lc->entries[i]));
    }
    ds_put_format(output, "%-16s: %"PRIu64"\n", "trim count", lc->trim_count);
    ds_put_format(output, "%-16s: %"PRIu64"\n", "Mem usage (KB)",
                  ROUND_UP(lc->mem_usage, 1024) / 1024);
}

void
lflow_cache_add_expr(struct lflow_cache *lc, const struct uuid *lflow_uuid,
                     struct expr *expr, size_t expr_sz)
{
    struct lflow_cache_value *lcv =
        lflow_cache_add__(lc, lflow_uuid, LCACHE_T_EXPR, expr_sz);

    if (!lcv) {
        expr_destroy(expr);
        return;
    }
    COVERAGE_INC(lflow_cache_add_expr);
    lcv->expr = expr;
}

void
lflow_cache_add_matches(struct lflow_cache *lc, const struct uuid *lflow_uuid,
                        uint32_t conj_id_ofs, uint32_t n_conjs,
                        struct hmap *matches, size_t matches_sz)
{
    struct lflow_cache_value *lcv =
        lflow_cache_add__(lc, lflow_uuid, LCACHE_T_MATCHES, matches_sz);

    if (!lcv) {
        expr_matches_destroy(matches);
        free(matches);
        return;
    }
    COVERAGE_INC(lflow_cache_add_matches);
    lcv->expr_matches = matches;
    lcv->n_conjs = n_conjs;
    lcv->conj_id_ofs = conj_id_ofs;
}

struct lflow_cache_value *
lflow_cache_get(struct lflow_cache *lc, const struct uuid *lflow_uuid)
{
    if (!lflow_cache_is_enabled(lc)) {
        return NULL;
    }

    size_t hash = uuid_hash(lflow_uuid);

    for (size_t i = 0; i < LCACHE_T_MAX; i++) {
        struct lflow_cache_entry *lce;

        HMAP_FOR_EACH_WITH_HASH (lce, node, hash, &lc->entries[i]) {
            if (uuid_equals(&lce->lflow_uuid, lflow_uuid)) {
                COVERAGE_INC(lflow_cache_hit);
                return &lce->value;
            }
        }
    }
    COVERAGE_INC(lflow_cache_miss);
    return NULL;
}

void
lflow_cache_delete(struct lflow_cache *lc, const struct uuid *lflow_uuid)
{
    if (!lc) {
        return;
    }

    struct lflow_cache_value *lcv = lflow_cache_get(lc, lflow_uuid);
    if (lcv) {
        COVERAGE_INC(lflow_cache_delete);
        lflow_cache_delete__(lc, CONTAINER_OF(lcv, struct lflow_cache_entry,
                                              value));
        lflow_cache_trim__(lc, false);
        memory_trimmer_record_activity(lc->mt);
    }
}

static bool
lflow_cache_make_room__(struct lflow_cache *lc, enum lflow_cache_type type)
{
    /* When the cache becomes full, the rule is to prefer more "important"
     * cache entries over less "important" ones.  That is, evict entries of
     * type LCACHE_T_EXPR if there's no room to add an entry of type
     * LCACHE_T_MATCHES.
     */
    for (size_t i = 0; i < type; i++) {
        if (hmap_count(&lc->entries[i]) > 0) {
            struct lflow_cache_entry *lce =
                CONTAINER_OF(hmap_first(&lc->entries[i]),
                             struct lflow_cache_entry, node);

            lflow_cache_delete__(lc, lce);
            return true;
        }
    }
    return false;
}

void
lflow_cache_get_memory_usage(const struct lflow_cache *lc, struct simap *usage)
{
    for (size_t i = 0; i < LCACHE_T_MAX; i++) {
        char *counter_name = xasprintf("lflow-cache-entries-%s",
                                       lflow_cache_type_names[i]);
        simap_increase(usage, counter_name, hmap_count(&lc->entries[i]));
        free(counter_name);
    }
    simap_increase(usage, "lflow-cache-size-KB",
                   ROUND_UP(lc->mem_usage, 1024) / 1024);
}

void
lflow_cache_run(struct lflow_cache *lc)
{
    if (memory_trimmer_can_run(lc->mt)) {
        lflow_cache_trim__(lc, true);
    }
}

void
lflow_cache_wait(struct lflow_cache *lc)
{
    memory_trimmer_wait(lc->mt);
}

static struct lflow_cache_value *
lflow_cache_add__(struct lflow_cache *lc, const struct uuid *lflow_uuid,
                  enum lflow_cache_type type, uint64_t value_size)
{
    if (!lflow_cache_is_enabled(lc) || !lflow_uuid) {
        return NULL;
    }

    struct lflow_cache_entry *lce;
    size_t size = sizeof *lce + value_size;
    if (size + lc->mem_usage > lc->max_mem_usage) {
        COVERAGE_INC(lflow_cache_mem_full);
        return NULL;
    }

    if (lc->n_entries == lc->capacity) {
        if (!lflow_cache_make_room__(lc, type)) {
            COVERAGE_INC(lflow_cache_full);
            return NULL;
        } else {
            COVERAGE_INC(lflow_cache_made_room);
        }
    }

    memory_trimmer_record_activity(lc->mt);
    lc->mem_usage += size;

    COVERAGE_INC(lflow_cache_add);
    lce = xzalloc(sizeof *lce);
    lce->lflow_uuid = *lflow_uuid;
    lce->size = size;
    lce->value.type = type;
    hmap_insert(&lc->entries[type], &lce->node, uuid_hash(lflow_uuid));
    lc->n_entries++;
    lc->high_watermark = MAX(lc->high_watermark, lc->n_entries);
    return &lce->value;
}

static void
lflow_cache_delete__(struct lflow_cache *lc, struct lflow_cache_entry *lce)
{
    ovs_assert(lc->n_entries > 0);
    hmap_remove(&lc->entries[lce->value.type], &lce->node);
    lc->n_entries--;
    switch (lce->value.type) {
    case LCACHE_T_NONE:
        OVS_NOT_REACHED();
        break;
    case LCACHE_T_EXPR:
        COVERAGE_INC(lflow_cache_free_expr);
        expr_destroy(lce->value.expr);
        break;
    case LCACHE_T_MATCHES:
        COVERAGE_INC(lflow_cache_free_matches);
        expr_matches_destroy(lce->value.expr_matches);
        free(lce->value.expr_matches);
        break;
    }

    ovs_assert(lc->mem_usage >= lce->size);
    lc->mem_usage -= lce->size;
    free(lce);
}

static void
lflow_cache_trim__(struct lflow_cache *lc, bool force)
{
    /* Trim if we had at least 'TRIM_LIMIT' elements at some point and if the
     * current usage is less than half of 'high_watermark'.
     */
    uint32_t upper_trim_limit = lc->high_watermark * lc->trim_wmark_perc / 100;
    ovs_assert(lc->high_watermark >= lc->n_entries);
    if (!force
            && (lc->high_watermark <= lc->trim_limit
                || lc->n_entries > upper_trim_limit)) {
        return;
    }

    COVERAGE_INC(lflow_cache_trim);
    for (size_t i = 0; i < LCACHE_T_MAX; i++) {
        hmap_shrink(&lc->entries[i]);
    }

    memory_trimmer_trim(lc->mt);

    lc->high_watermark = lc->n_entries;
    lc->trim_count++;
}