Coverage Report

Created: 2026-12-01 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/dpif-netdev-perf.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2017 Ericsson AB.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at:
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <config.h>
18
#include <stdint.h>
19
20
#include "dpdk.h"
21
#include "dpif-netdev-perf.h"
22
#include "openvswitch/dynamic-string.h"
23
#include "openvswitch/vlog.h"
24
#include "ovs-numa.h"
25
#include "ovs-thread.h"
26
#include "timeval.h"
27
28
VLOG_DEFINE_THIS_MODULE(pmd_perf);
29
30
#define ITER_US_THRESHOLD 250   /* Warning threshold for iteration duration
31
                                   in microseconds. */
32
#define VHOST_QUEUE_FULL 128    /* Size of the virtio TX queue. */
33
0
#define LOG_IT_BEFORE 5         /* Number of iterations to log before
34
                                   suspicious iteration. */
35
0
#define LOG_IT_AFTER 5          /* Number of iterations to log after
36
                                   suspicious iteration. */
37
38
bool log_enabled = false;
39
bool log_extend = false;
40
static uint32_t log_it_before = LOG_IT_BEFORE;
41
static uint32_t log_it_after = LOG_IT_AFTER;
42
static uint32_t log_us_thr = ITER_US_THRESHOLD;
43
uint32_t log_q_thr = VHOST_QUEUE_FULL;
44
uint64_t iter_cycle_threshold;
45
46
static struct vlog_rate_limit latency_rl = VLOG_RATE_LIMIT_INIT(600, 600);
47
48
static uint64_t tsc_hz = 1;
49
50
void
51
pmd_perf_estimate_tsc_frequency(void)
52
0
{
53
#ifdef DPDK_NETDEV
54
    if (dpdk_available()) {
55
        tsc_hz = rte_get_tsc_hz();
56
    }
57
    if (tsc_hz > 1) {
58
        VLOG_INFO("DPDK provided TSC frequency: %"PRIu64" KHz", tsc_hz / 1000);
59
        return;
60
    }
61
#endif
62
0
    struct ovs_numa_dump *affinity;
63
0
    struct pmd_perf_stats s;
64
0
    uint64_t start, stop;
65
66
    /* DPDK is not available or returned unreliable value.
67
     * Trying to estimate. */
68
0
    affinity = ovs_numa_thread_getaffinity_dump();
69
0
    if (affinity) {
70
0
        const struct ovs_numa_info_core *core;
71
72
0
        FOR_EACH_CORE_ON_DUMP (core, affinity) {
73
            /* Setting affinity to a single core from the affinity mask to
74
             * avoid re-scheduling to another core while sleeping. */
75
0
            ovs_numa_thread_setaffinity_core(core->core_id);
76
0
            break;
77
0
        }
78
0
    }
79
80
0
    memset(&s, 0, sizeof s);
81
0
    start = cycles_counter_update(&s);
82
    /* Using xnanosleep as it's interrupt resistant.
83
     * Sleeping only 100 ms to avoid holding the main thread for too long. */
84
0
    xnanosleep(1E8);
85
0
    stop = cycles_counter_update(&s);
86
87
0
    if (affinity) {
88
        /* Restoring previous affinity. */
89
0
        ovs_numa_thread_setaffinity_dump(affinity);
90
0
        ovs_numa_dump_destroy(affinity);
91
0
    }
92
93
0
    if (stop <= start) {
94
0
        VLOG_WARN("TSC source is unreliable.");
95
0
        tsc_hz = 1;
96
0
    } else {
97
0
        tsc_hz = (stop - start) * 10;
98
0
    }
99
100
0
    VLOG_INFO("Estimated TSC frequency: %"PRIu64" KHz", tsc_hz / 1000);
101
0
}
102
103
/* Histogram functions. */
104
105
static void
106
histogram_walls_set_lin(struct histogram *hist, uint32_t min, uint32_t max)
107
0
{
108
0
    int i;
109
110
0
    ovs_assert(min < max);
111
0
    for (i = 0; i < NUM_BINS-1; i++) {
112
0
        hist->wall[i] = min + (i * (max - min)) / (NUM_BINS - 2);
113
0
    }
114
0
    hist->wall[NUM_BINS-1] = UINT32_MAX;
115
0
}
116
117
static void
118
histogram_walls_set_log(struct histogram *hist, uint32_t min, uint32_t max)
119
0
{
120
0
    int i, start, bins, wall;
121
0
    double log_min, log_max;
122
123
0
    ovs_assert(min < max);
124
0
    if (min > 0) {
125
0
        log_min = log(min);
126
0
        log_max = log(max);
127
0
        start = 0;
128
0
        bins = NUM_BINS - 1;
129
0
    } else {
130
0
        hist->wall[0] = 0;
131
0
        log_min = log(1);
132
0
        log_max = log(max);
133
0
        start = 1;
134
0
        bins = NUM_BINS - 2;
135
0
    }
136
0
    wall = start;
137
0
    for (i = 0; i < bins; i++) {
138
        /* Make sure each wall is monotonically increasing. */
139
0
        wall = MAX(wall, exp(log_min + (i * (log_max - log_min)) / (bins-1)));
140
0
        hist->wall[start + i] = wall++;
141
0
    }
142
0
    if (hist->wall[NUM_BINS-2] < max) {
143
0
        hist->wall[NUM_BINS-2] = max;
144
0
    }
145
0
    hist->wall[NUM_BINS-1] = UINT32_MAX;
146
0
}
147
148
uint64_t
149
histogram_samples(const struct histogram *hist)
150
0
{
151
0
    uint64_t samples = 0;
152
153
0
    for (int i = 0; i < NUM_BINS; i++) {
154
0
        samples += hist->bin[i];
155
0
    }
156
0
    return samples;
157
0
}
158
159
static void
160
histogram_clear(struct histogram *hist)
161
0
{
162
0
    int i;
163
164
0
    for (i = 0; i < NUM_BINS; i++) {
165
0
        hist->bin[i] = 0;
166
0
    }
167
0
}
168
169
static void
170
history_init(struct history *h)
171
0
{
172
0
    memset(h, 0, sizeof(*h));
173
0
}
174
175
void
176
pmd_perf_stats_init(struct pmd_perf_stats *s)
177
0
{
178
0
    memset(s, 0, sizeof(*s));
179
0
    ovs_mutex_init(&s->stats_mutex);
180
0
    ovs_mutex_init(&s->clear_mutex);
181
    /* Logarithmic histogram for cycles/it ranging from 500 to 24M
182
     * (corresponding to 200 ns to 9.6 ms at 2.5 GHz TSC clock). */
183
0
    histogram_walls_set_log(&s->cycles, 500, 24000000);
184
    /* Logarithmic histogram for pkts/it ranging from 0 to 1000. */
185
0
    histogram_walls_set_log(&s->pkts, 0, 1000);
186
    /* Linear histogram for cycles/pkt ranging from 100 to 30K. */
187
0
    histogram_walls_set_lin(&s->cycles_per_pkt, 100, 30000);
188
    /* Linear histogram for pkts/rx batch ranging from 0 to 32,
189
     * the maximum rx batch size in OVS. */
190
0
    histogram_walls_set_lin(&s->pkts_per_batch, 0, 32);
191
    /* Linear histogram for upcalls/it ranging from 0 to 30. */
192
0
    histogram_walls_set_lin(&s->upcalls, 0, 30);
193
    /* Logarithmic histogram for cycles/upcall ranging from 1000 to 1M
194
     * (corresponding to 400 ns to 400 us at 2.5 GHz TSC clock). */
195
0
    histogram_walls_set_log(&s->cycles_per_upcall, 1000, 1000000);
196
    /* Log. histogram for max vhostuser queue fill level from 0 to 512.
197
     * 512 is the maximum fill level for a virtio queue with 1024
198
     * descriptors (maximum configurable length in Qemu), with the
199
     * DPDK 17.11 virtio PMD in the guest. */
200
0
    histogram_walls_set_log(&s->max_vhost_qfill, 0, 512);
201
0
    s->iteration_cnt = 0;
202
0
    s->start_ms = time_msec();
203
0
    s->log_susp_it = UINT32_MAX;
204
0
    s->log_begin_it = UINT32_MAX;
205
0
    s->log_end_it = UINT32_MAX;
206
0
    s->log_reason = NULL;
207
0
}
208
209
void
210
pmd_perf_format_overall_stats(struct ds *str, struct pmd_perf_stats *s,
211
                              double duration, bool format_iterations)
212
0
{
213
0
    uint64_t stats[PMD_N_STATS];
214
0
    double us_per_cycle = 1000000.0 / tsc_hz;
215
216
0
    if (duration == 0) {
217
0
        return;
218
0
    }
219
220
0
    pmd_perf_read_counters(s, stats);
221
0
    uint64_t tot_cycles = stats[PMD_CYCLES_ITER_IDLE] +
222
0
                          stats[PMD_CYCLES_ITER_BUSY];
223
0
    uint64_t rx_packets = stats[PMD_STAT_RECV];
224
0
    uint64_t tx_packets = stats[PMD_STAT_SENT_PKTS];
225
0
    uint64_t tx_batches = stats[PMD_STAT_SENT_BATCHES];
226
0
    uint64_t passes = stats[PMD_STAT_RECV] +
227
0
                      stats[PMD_STAT_RECIRC];
228
0
    uint64_t upcalls = stats[PMD_STAT_MISS];
229
0
    uint64_t upcall_cycles = stats[PMD_CYCLES_UPCALL];
230
0
    uint64_t tot_iter = histogram_samples(&s->pkts);
231
0
    uint64_t idle_iter = s->pkts.bin[0];
232
0
    uint64_t busy_iter = tot_iter >= idle_iter ? tot_iter - idle_iter : 0;
233
0
    uint64_t sleep_iter = stats[PMD_SLEEP_ITER];
234
0
    uint64_t tot_sleep_cycles = stats[PMD_CYCLES_SLEEP];
235
236
0
    if (format_iterations) {
237
0
        ds_put_format(str,
238
0
            "  Iterations:               %12"PRIu64"  (%.2f us/it)\n"
239
0
            "  - Used TSC cycles:        %12"PRIu64"  (%5.1f %% "
240
0
                                                       "of total cycles)\n"
241
0
            "  - idle iterations:        %12"PRIu64"  (%5.1f %% "
242
0
                                                       "of used cycles)\n"
243
0
            "  - busy iterations:        %12"PRIu64"  (%5.1f %% "
244
0
                                                       "of used cycles)\n"
245
0
            "  - sleep iterations:       %12"PRIu64"  (%5.1f %% "
246
0
                                                       "of iterations)\n"
247
0
            "  Sleep time (us):          %12.0f  (%3.0f us/iteration avg.)\n",
248
0
            tot_iter,
249
0
            tot_iter
250
0
                ? (tot_cycles + tot_sleep_cycles) * us_per_cycle / tot_iter
251
0
                : 0,
252
0
            tot_cycles, 100.0 * (tot_cycles / duration) / tsc_hz,
253
0
            idle_iter,
254
0
            tot_cycles ? 100.0 * stats[PMD_CYCLES_ITER_IDLE] / tot_cycles : 0,
255
0
            busy_iter,
256
0
            tot_cycles ? 100.0 * stats[PMD_CYCLES_ITER_BUSY] / tot_cycles : 0,
257
0
            sleep_iter, tot_iter ? 100.0 * sleep_iter / tot_iter : 0,
258
0
            tot_sleep_cycles * us_per_cycle,
259
0
            sleep_iter ? (tot_sleep_cycles * us_per_cycle) / sleep_iter : 0);
260
0
    }
261
0
    if (rx_packets > 0) {
262
0
        ds_put_format(str,
263
0
            "  Rx packets:         %12"PRIu64"  (%.0f Kpps",
264
0
            rx_packets, (rx_packets / duration) / 1000);
265
0
        if (tot_iter) {
266
0
            ds_put_format(str, ", %.0f cycles/pkt",
267
0
                1.0 * stats[PMD_CYCLES_ITER_BUSY] / rx_packets);
268
0
        }
269
0
        ds_put_cstr(str, ")\n");
270
271
0
        ds_put_format(str,
272
0
            "  Datapath passes:          %12"PRIu64"  (%.2f passes/pkt)\n"
273
0
            "  - PHWOL without ACT hits: %12"PRIu64"  (%5.1f %%)\n"
274
0
            "  - PHWOL with ACT hits :   %12"PRIu64"  (%5.1f %%)\n"
275
0
            "  - Simple Match hits:      %12"PRIu64"  (%5.1f %%)\n"
276
0
            "  - EMC hits:               %12"PRIu64"  (%5.1f %%)\n"
277
0
            "  - SMC hits:               %12"PRIu64"  (%5.1f %%)\n"
278
0
            "  - Megaflow hits:          %12"PRIu64"  (%5.1f %%, %.2f "
279
0
                                                       "subtbl lookups/hit)\n",
280
0
            passes, 1.0 * passes / rx_packets,
281
0
            stats[PMD_STAT_PHWOL_HIT],
282
0
            100.0 * stats[PMD_STAT_PHWOL_HIT] / passes,
283
0
            stats[PMD_STAT_PHWOL_ACT_HIT],
284
0
            100.0 * stats[PMD_STAT_PHWOL_ACT_HIT] / passes,
285
0
            stats[PMD_STAT_SIMPLE_HIT],
286
0
            100.0 * stats[PMD_STAT_SIMPLE_HIT] / passes,
287
0
            stats[PMD_STAT_EXACT_HIT],
288
0
            100.0 * stats[PMD_STAT_EXACT_HIT] / passes,
289
0
            stats[PMD_STAT_SMC_HIT],
290
0
            100.0 * stats[PMD_STAT_SMC_HIT] / passes,
291
0
            stats[PMD_STAT_MASKED_HIT],
292
0
            100.0 * stats[PMD_STAT_MASKED_HIT] / passes,
293
0
            stats[PMD_STAT_MASKED_HIT]
294
0
            ? 1.0 * stats[PMD_STAT_MASKED_LOOKUP] / stats[PMD_STAT_MASKED_HIT]
295
0
            : 0);
296
297
0
        ds_put_format(str,
298
0
            "  - Upcalls:                %12"PRIu64"  (%5.1f %%",
299
0
            upcalls, 100.0 * upcalls / passes);
300
0
        if (tot_iter) {
301
0
            ds_put_format(str, ", %.1f us/upcall",
302
0
                upcalls ? (upcall_cycles * us_per_cycle) / upcalls : 0);
303
0
        }
304
0
        ds_put_cstr(str, ")\n");
305
306
0
        ds_put_format(str,
307
0
            "  - Lost upcalls:           %12"PRIu64"  (%5.1f %%)\n",
308
0
            stats[PMD_STAT_LOST], 100.0 * stats[PMD_STAT_LOST] / passes);
309
0
    } else {
310
0
        ds_put_format(str,
311
0
            "  Rx packets:               %12d\n", 0);
312
0
    }
313
0
    if (tx_packets > 0) {
314
0
        ds_put_format(str,
315
0
            "  Tx packets:               %12"PRIu64"  (%.0f Kpps)\n"
316
0
            "  Tx batches:               %12"PRIu64"  (%.2f pkts/batch)\n",
317
0
            tx_packets, (tx_packets / duration) / 1000,
318
0
            tx_batches, 1.0 * tx_packets / tx_batches);
319
0
    } else {
320
0
        ds_put_format(str,
321
0
            "  Tx packets:               %12d\n\n", 0);
322
0
    }
323
0
}
324
325
void
326
pmd_perf_format_histograms(struct ds *str, struct pmd_perf_stats *s)
327
0
{
328
0
    int i;
329
330
0
    ds_put_cstr(str, "Histograms\n");
331
0
    ds_put_format(str,
332
0
                  "   %-21s  %-21s  %-21s  %-21s  %-21s  %-21s  %-21s\n",
333
0
                  "cycles/it", "packets/it", "cycles/pkt", "pkts/batch",
334
0
                  "max vhost qlen", "upcalls/it", "cycles/upcall");
335
0
    for (i = 0; i < NUM_BINS-1; i++) {
336
0
        ds_put_format(str,
337
0
            "   %-9d %-11"PRIu64"  %-9d %-11"PRIu64"  %-9d %-11"PRIu64
338
0
            "  %-9d %-11"PRIu64"  %-9d %-11"PRIu64"  %-9d %-11"PRIu64
339
0
            "  %-9d %-11"PRIu64"\n",
340
0
            s->cycles.wall[i], s->cycles.bin[i],
341
0
            s->pkts.wall[i],s->pkts.bin[i],
342
0
            s->cycles_per_pkt.wall[i], s->cycles_per_pkt.bin[i],
343
0
            s->pkts_per_batch.wall[i], s->pkts_per_batch.bin[i],
344
0
            s->max_vhost_qfill.wall[i], s->max_vhost_qfill.bin[i],
345
0
            s->upcalls.wall[i], s->upcalls.bin[i],
346
0
            s->cycles_per_upcall.wall[i], s->cycles_per_upcall.bin[i]);
347
0
    }
348
0
    ds_put_format(str,
349
0
                  "   %-9s %-11"PRIu64"  %-9s %-11"PRIu64"  %-9s %-11"PRIu64
350
0
                  "  %-9s %-11"PRIu64"  %-9s %-11"PRIu64"  %-9s %-11"PRIu64
351
0
                  "  %-9s %-11"PRIu64"\n",
352
0
                  ">", s->cycles.bin[i],
353
0
                  ">", s->pkts.bin[i],
354
0
                  ">", s->cycles_per_pkt.bin[i],
355
0
                  ">", s->pkts_per_batch.bin[i],
356
0
                  ">", s->max_vhost_qfill.bin[i],
357
0
                  ">", s->upcalls.bin[i],
358
0
                  ">", s->cycles_per_upcall.bin[i]);
359
0
    ds_put_cstr(str,
360
0
                "-----------------------------------------------------"
361
0
                "-----------------------------------------------------"
362
0
                "------------------------------------------------\n");
363
0
    ds_put_format(str,
364
0
                  "   %-21s  %-21s  %-21s  %-21s  %-21s  %-21s  %-21s\n",
365
0
                  "cycles/it", "packets/it", "cycles/pkt", "pkts/batch",
366
0
                  "vhost qlen", "upcalls/it", "cycles/upcall");
367
0
    ds_put_format(str,
368
0
                  "   %-21"PRIu64"  %-21.5f  %-21"PRIu64
369
0
                  "  %-21.5f  %-21.5f  %-21.5f  %-21"PRIu32"\n",
370
0
                  s->totals.iterations
371
0
                      ? s->totals.cycles / s->totals.iterations : 0,
372
0
                  s->totals.iterations
373
0
                      ? 1.0 * s->totals.pkts / s->totals.iterations : 0,
374
0
                  s->totals.pkts
375
0
                      ? s->totals.busy_cycles / s->totals.pkts : 0,
376
0
                  s->totals.batches
377
0
                      ? 1.0 * s->totals.pkts / s->totals.batches : 0,
378
0
                  s->totals.iterations
379
0
                      ? 1.0 * s->totals.max_vhost_qfill / s->totals.iterations
380
0
                      : 0,
381
0
                  s->totals.iterations
382
0
                      ? 1.0 * s->totals.upcalls / s->totals.iterations : 0,
383
0
                  s->totals.upcalls
384
0
                      ? s->totals.upcall_cycles / s->totals.upcalls : 0);
385
0
}
386
387
void
388
pmd_perf_format_iteration_history(struct ds *str, struct pmd_perf_stats *s,
389
                                  int n_iter)
390
0
{
391
0
    struct iter_stats *is;
392
0
    size_t index;
393
0
    int i;
394
395
0
    if (n_iter == 0) {
396
0
        return;
397
0
    }
398
0
    ds_put_format(str, "   %-17s   %-10s   %-10s   %-10s   %-10s   "
399
0
                  "%-10s   %-10s   %-10s\n",
400
0
                  "iter", "cycles", "packets", "cycles/pkt", "pkts/batch",
401
0
                  "vhost qlen", "upcalls", "cycles/upcall");
402
0
    for (i = 1; i <= n_iter; i++) {
403
0
        index = history_sub(s->iterations.idx, i);
404
0
        is = &s->iterations.sample[index];
405
0
        ds_put_format(str,
406
0
                      "   %-17"PRIu64"   %-11"PRIu64"  %-11"PRIu32
407
0
                      "  %-11"PRIu64"  %-11"PRIu32"  %-11"PRIu32
408
0
                      "  %-11"PRIu32"  %-11"PRIu32"\n",
409
0
                      is->timestamp,
410
0
                      is->cycles,
411
0
                      is->pkts,
412
0
                      is->pkts ? is->cycles / is->pkts : 0,
413
0
                      is->batches ? is->pkts / is->batches : 0,
414
0
                      is->max_vhost_qfill,
415
0
                      is->upcalls,
416
0
                      is->upcalls ? is->upcall_cycles / is->upcalls : 0);
417
0
    }
418
0
}
419
420
void
421
pmd_perf_format_ms_history(struct ds *str, struct pmd_perf_stats *s, int n_ms)
422
0
{
423
0
    struct iter_stats *is;
424
0
    size_t index;
425
0
    int i;
426
427
0
    if (n_ms == 0) {
428
0
        return;
429
0
    }
430
0
    ds_put_format(str,
431
0
                  "   %-12s   %-10s   %-10s   %-10s   %-10s"
432
0
                  "   %-10s   %-10s   %-10s   %-10s\n",
433
0
                  "ms", "iterations", "cycles/it", "Kpps", "cycles/pkt",
434
0
                  "pkts/batch", "vhost qlen", "upcalls", "cycles/upcall");
435
0
    for (i = 1; i <= n_ms; i++) {
436
0
        index = history_sub(s->milliseconds.idx, i);
437
0
        is = &s->milliseconds.sample[index];
438
0
        ds_put_format(str,
439
0
                      "   %-12"PRIu64"   %-11"PRIu32"  %-11"PRIu64
440
0
                      "  %-11"PRIu32"  %-11"PRIu64"  %-11"PRIu32
441
0
                      "  %-11"PRIu32"  %-11"PRIu32"  %-11"PRIu32"\n",
442
0
                      is->timestamp,
443
0
                      is->iterations,
444
0
                      is->iterations ? is->cycles / is->iterations : 0,
445
0
                      is->pkts,
446
0
                      is->pkts ? is->busy_cycles / is->pkts : 0,
447
0
                      is->batches ? is->pkts / is->batches : 0,
448
0
                      is->iterations
449
0
                          ? is->max_vhost_qfill / is->iterations : 0,
450
0
                      is->upcalls,
451
0
                      is->upcalls ? is->upcall_cycles / is->upcalls : 0);
452
0
    }
453
0
}
454
455
void
456
pmd_perf_read_counters(struct pmd_perf_stats *s,
457
                       uint64_t stats[PMD_N_STATS])
458
0
{
459
0
    uint64_t val;
460
461
    /* These loops subtracts reference values (.zero[*]) from the counters.
462
     * Since loads and stores are relaxed, it might be possible for a .zero[*]
463
     * value to be more recent than the current value we're reading from the
464
     * counter.  This is not a big problem, since these numbers are not
465
     * supposed to be 100% accurate, but we should at least make sure that
466
     * the result is not negative. */
467
0
    for (int i = 0; i < PMD_N_STATS; i++) {
468
0
        atomic_read_relaxed(&s->counters.n[i], &val);
469
0
        if (val > s->counters.zero[i]) {
470
0
            stats[i] = val - s->counters.zero[i];
471
0
        } else {
472
0
            stats[i] = 0;
473
0
        }
474
0
    }
475
0
}
476
477
/* This function clears the PMD performance counters from within the PMD
478
 * thread or from another thread when the PMD thread is not executing its
479
 * poll loop. */
480
void
481
pmd_perf_stats_clear_lock(struct pmd_perf_stats *s)
482
    OVS_REQUIRES(s->stats_mutex)
483
0
{
484
0
    ovs_mutex_lock(&s->clear_mutex);
485
0
    for (int i = 0; i < PMD_N_STATS; i++) {
486
0
        atomic_read_relaxed(&s->counters.n[i], &s->counters.zero[i]);
487
0
    }
488
    /* The following stats are only applicable in PMD thread and */
489
0
    memset(&s->current, 0 , sizeof(struct iter_stats));
490
0
    memset(&s->totals, 0 , sizeof(struct iter_stats));
491
0
    histogram_clear(&s->cycles);
492
0
    histogram_clear(&s->pkts);
493
0
    histogram_clear(&s->cycles_per_pkt);
494
0
    histogram_clear(&s->upcalls);
495
0
    histogram_clear(&s->cycles_per_upcall);
496
0
    histogram_clear(&s->pkts_per_batch);
497
0
    histogram_clear(&s->max_vhost_qfill);
498
0
    history_init(&s->iterations);
499
0
    history_init(&s->milliseconds);
500
0
    s->start_ms = time_msec();
501
0
    s->milliseconds.sample[0].timestamp = s->start_ms;
502
0
    s->log_susp_it = UINT32_MAX;
503
0
    s->log_begin_it = UINT32_MAX;
504
0
    s->log_end_it = UINT32_MAX;
505
0
    s->log_reason = NULL;
506
    /* Clearing finished. */
507
0
    s->clear = false;
508
0
    ovs_mutex_unlock(&s->clear_mutex);
509
0
}
510
511
/* This function can be called from the anywhere to clear the stats
512
 * of PMD and non-PMD threads. */
513
void
514
pmd_perf_stats_clear(struct pmd_perf_stats *s)
515
0
{
516
0
    if (ovs_mutex_trylock(&s->stats_mutex) == 0) {
517
        /* Locking successful. PMD not polling. */
518
0
        pmd_perf_stats_clear_lock(s);
519
0
        ovs_mutex_unlock(&s->stats_mutex);
520
0
    } else {
521
        /* Request the polling PMD to clear the stats. There is no need to
522
         * block here as stats retrieval is prevented during clearing. */
523
0
        s->clear = true;
524
0
    }
525
0
}
526
527
/* Functions recording PMD metrics per iteration. */
528
529
void
530
pmd_perf_start_iteration(struct pmd_perf_stats *s)
531
OVS_REQUIRES(s->stats_mutex)
532
0
{
533
0
    if (s->clear) {
534
        /* Clear the PMD stats before starting next iteration. */
535
0
        pmd_perf_stats_clear_lock(s);
536
0
    }
537
0
    s->iteration_cnt++;
538
    /* Initialize the current interval stats. */
539
0
    memset(&s->current, 0, sizeof(struct iter_stats));
540
0
    if (OVS_LIKELY(s->last_tsc)) {
541
        /* We assume here that last_tsc was updated immediately prior at
542
         * the end of the previous iteration, or just before the first
543
         * iteration. */
544
0
        s->start_tsc = s->last_tsc;
545
0
    } else {
546
        /* In case last_tsc has never been set before. */
547
0
        s->start_tsc = cycles_counter_update(s);
548
0
    }
549
0
}
550
551
void
552
pmd_perf_end_iteration(struct pmd_perf_stats *s, int rx_packets,
553
                       int tx_packets, uint64_t sleep_cycles,
554
                       bool full_metrics)
555
0
{
556
0
    uint64_t now_tsc = cycles_counter_update(s);
557
0
    struct iter_stats *cum_ms;
558
0
    uint64_t cycles, cycles_per_pkt = 0;
559
0
    char *reason = NULL;
560
561
0
    cycles = now_tsc - s->start_tsc - sleep_cycles;
562
0
    s->current.timestamp = s->iteration_cnt;
563
0
    s->current.cycles = cycles;
564
0
    s->current.pkts = rx_packets;
565
566
0
    if (rx_packets + tx_packets > 0) {
567
0
        pmd_perf_update_counter(s, PMD_CYCLES_ITER_BUSY, cycles);
568
0
    } else {
569
0
        pmd_perf_update_counter(s, PMD_CYCLES_ITER_IDLE, cycles);
570
0
    }
571
    /* Add iteration samples to histograms. */
572
0
    histogram_add_sample(&s->cycles, cycles);
573
0
    histogram_add_sample(&s->pkts, rx_packets);
574
575
0
    if (sleep_cycles) {
576
0
        pmd_perf_update_counter(s, PMD_SLEEP_ITER, 1);
577
0
        pmd_perf_update_counter(s, PMD_CYCLES_SLEEP, sleep_cycles);
578
0
    }
579
580
0
    if (!full_metrics) {
581
0
        return;
582
0
    }
583
584
0
    s->counters.n[PMD_CYCLES_UPCALL] += s->current.upcall_cycles;
585
586
0
    if (rx_packets > 0) {
587
0
        cycles_per_pkt = cycles / rx_packets;
588
0
        histogram_add_sample(&s->cycles_per_pkt, cycles_per_pkt);
589
0
    }
590
0
    histogram_add_sample(&s->upcalls, s->current.upcalls);
591
0
    histogram_add_sample(&s->max_vhost_qfill, s->current.max_vhost_qfill);
592
593
    /* Add iteration samples to millisecond stats. */
594
0
    cum_ms = history_current(&s->milliseconds);
595
0
    cum_ms->iterations++;
596
0
    cum_ms->cycles += cycles;
597
0
    if (rx_packets > 0) {
598
0
        cum_ms->busy_cycles += cycles;
599
0
    }
600
0
    cum_ms->pkts += s->current.pkts;
601
0
    cum_ms->upcalls += s->current.upcalls;
602
0
    cum_ms->upcall_cycles += s->current.upcall_cycles;
603
0
    cum_ms->batches += s->current.batches;
604
0
    cum_ms->max_vhost_qfill += s->current.max_vhost_qfill;
605
606
0
    if (log_enabled) {
607
        /* Log suspicious iterations. */
608
0
        if (cycles > iter_cycle_threshold) {
609
0
            reason = "Excessive total cycles";
610
0
        } else if (s->current.max_vhost_qfill >= log_q_thr) {
611
0
            reason = "Vhost RX queue full";
612
0
        }
613
0
        if (OVS_UNLIKELY(reason)) {
614
0
            pmd_perf_set_log_susp_iteration(s, reason);
615
0
            cycles_counter_update(s);
616
0
        }
617
618
        /* Log iteration interval around suspicious iteration when reaching
619
         * the end of the range to be logged. */
620
0
        if (OVS_UNLIKELY(s->log_end_it == s->iterations.idx)) {
621
0
            pmd_perf_log_susp_iteration_neighborhood(s);
622
0
            cycles_counter_update(s);
623
0
        }
624
0
    }
625
626
    /* Store in iteration history. This advances the iteration idx and
627
     * clears the next slot in the iteration history. */
628
0
    history_store(&s->iterations, &s->current);
629
630
0
    if (now_tsc > s->next_check_tsc) {
631
        /* Check if ms is completed and store in milliseconds history. */
632
0
        uint64_t now = time_msec();
633
0
        if (now != cum_ms->timestamp) {
634
            /* Add ms stats to totals. */
635
0
            s->totals.iterations += cum_ms->iterations;
636
0
            s->totals.cycles += cum_ms->cycles;
637
0
            s->totals.busy_cycles += cum_ms->busy_cycles;
638
0
            s->totals.pkts += cum_ms->pkts;
639
0
            s->totals.upcalls += cum_ms->upcalls;
640
0
            s->totals.upcall_cycles += cum_ms->upcall_cycles;
641
0
            s->totals.batches += cum_ms->batches;
642
0
            s->totals.max_vhost_qfill += cum_ms->max_vhost_qfill;
643
0
            cum_ms = history_next(&s->milliseconds);
644
0
            cum_ms->timestamp = now;
645
0
        }
646
        /* Do the next check after 4 us (10K cycles at 2.5 GHz TSC clock). */
647
0
        s->next_check_tsc = cycles_counter_update(s) + tsc_hz / 250000;
648
0
    }
649
0
}
650
651
/* Delay logging of the suspicious iteration and the range of iterations
652
 * around it until after the last iteration in the range to be logged.
653
 * This avoids any distortion of the measurements through the cost of
654
 * logging itself. */
655
656
void
657
pmd_perf_set_log_susp_iteration(struct pmd_perf_stats *s,
658
                                char *reason)
659
0
{
660
0
    if (s->log_susp_it == UINT32_MAX) {
661
        /* No logging scheduled yet. */
662
0
        s->log_susp_it = s->iterations.idx;
663
0
        s->log_reason = reason;
664
0
        s->log_begin_it = history_sub(s->iterations.idx, log_it_before);
665
0
        s->log_end_it = history_add(s->iterations.idx, log_it_after + 1);
666
0
    } else if (log_extend) {
667
        /* Logging was initiated earlier, we log the previous suspicious
668
         * iteration now and extend the logging interval, if possible. */
669
0
        struct iter_stats *susp = &s->iterations.sample[s->log_susp_it];
670
0
        uint32_t new_end_it, old_range, new_range;
671
672
0
        VLOG_WARN_RL(&latency_rl,
673
0
                "Suspicious iteration (%s): iter=%"PRIu64
674
0
                " duration=%"PRIu64" us\n",
675
0
                s->log_reason,
676
0
                susp->timestamp,
677
0
                (1000000L * susp->cycles) / tsc_hz);
678
679
0
        new_end_it = history_add(s->iterations.idx, log_it_after + 1);
680
0
        new_range = history_sub(new_end_it, s->log_begin_it);
681
0
        old_range = history_sub(s->log_end_it, s->log_begin_it);
682
0
        if (new_range < old_range) {
683
            /* Extended range exceeds history length. */
684
0
            new_end_it = s->log_begin_it;
685
0
        }
686
0
        s->log_susp_it = s->iterations.idx;
687
0
        s->log_reason = reason;
688
0
        s->log_end_it = new_end_it;
689
0
    }
690
0
}
691
692
void
693
pmd_perf_log_susp_iteration_neighborhood(struct pmd_perf_stats *s)
694
0
{
695
0
    ovs_assert(s->log_reason != NULL);
696
0
    ovs_assert(s->log_susp_it != UINT32_MAX);
697
698
0
    struct ds log = DS_EMPTY_INITIALIZER;
699
0
    struct iter_stats *susp = &s->iterations.sample[s->log_susp_it];
700
0
    uint32_t range = history_sub(s->log_end_it, s->log_begin_it);
701
702
0
    VLOG_WARN_RL(&latency_rl,
703
0
                 "Suspicious iteration (%s): iter=%"PRIu64
704
0
                 " duration=%"PRIu64" us\n",
705
0
                 s->log_reason,
706
0
                 susp->timestamp,
707
0
                 (1000000L * susp->cycles) / tsc_hz);
708
709
0
    pmd_perf_format_iteration_history(&log, s, range);
710
0
    VLOG_WARN_RL(&latency_rl,
711
0
                 "Neighborhood of suspicious iteration:\n"
712
0
                 "%s", ds_cstr(&log));
713
0
    ds_destroy(&log);
714
0
    s->log_susp_it = s->log_end_it = s->log_begin_it = UINT32_MAX;
715
0
    s->log_reason = NULL;
716
717
0
    if (range > 100) {
718
        /* Reset to safe default values to avoid disturbance. */
719
0
        log_it_before = LOG_IT_BEFORE;
720
0
        log_it_after = LOG_IT_AFTER;
721
0
        log_extend = false;
722
0
    }
723
0
}
724
725
void
726
pmd_perf_log_set_cmd(struct unixctl_conn *conn,
727
                 int argc, const char *argv[],
728
                 void *aux OVS_UNUSED)
729
0
{
730
0
    unsigned int it_before, it_after, us_thr, q_thr;
731
0
    bool on, extend;
732
0
    bool usage = false;
733
734
0
    on = log_enabled;
735
0
    extend = log_extend;
736
0
    it_before = log_it_before;
737
0
    it_after = log_it_after;
738
0
    q_thr = log_q_thr;
739
0
    us_thr = log_us_thr;
740
741
0
    while (argc > 1) {
742
0
        if (!strcmp(argv[1], "on")) {
743
0
            on = true;
744
0
            argc--;
745
0
            argv++;
746
0
        } else if (!strcmp(argv[1], "off")) {
747
0
            on = false;
748
0
            argc--;
749
0
            argv++;
750
0
        } else if (!strcmp(argv[1], "-e")) {
751
0
            extend = true;
752
0
            argc--;
753
0
            argv++;
754
0
        } else if (!strcmp(argv[1], "-ne")) {
755
0
            extend = false;
756
0
            argc--;
757
0
            argv++;
758
0
        } else if (!strcmp(argv[1], "-a") && argc > 2) {
759
0
            if (str_to_uint(argv[2], 10, &it_after)) {
760
0
                if (it_after > HISTORY_LEN - 2) {
761
0
                    it_after = HISTORY_LEN - 2;
762
0
                }
763
0
            } else {
764
0
                usage = true;
765
0
                break;
766
0
            }
767
0
            argc -= 2;
768
0
            argv += 2;
769
0
        } else if (!strcmp(argv[1], "-b") && argc > 2) {
770
0
            if (str_to_uint(argv[2], 10, &it_before)) {
771
0
                if (it_before > HISTORY_LEN - 2) {
772
0
                    it_before = HISTORY_LEN - 2;
773
0
                }
774
0
            } else {
775
0
                usage = true;
776
0
                break;
777
0
            }
778
0
            argc -= 2;
779
0
            argv += 2;
780
0
        } else if (!strcmp(argv[1], "-q") && argc > 2) {
781
0
            if (!str_to_uint(argv[2], 10, &q_thr)) {
782
0
                usage = true;
783
0
                break;
784
0
            }
785
0
            argc -= 2;
786
0
            argv += 2;
787
0
        } else if (!strcmp(argv[1], "-us") && argc > 2) {
788
0
            if (!str_to_uint(argv[2], 10, &us_thr)) {
789
0
                usage = true;
790
0
                break;
791
0
            }
792
0
            argc -= 2;
793
0
            argv += 2;
794
0
        } else {
795
0
            usage = true;
796
0
            break;
797
0
        }
798
0
    }
799
0
    if (it_before + it_after > HISTORY_LEN - 2) {
800
0
        it_after = HISTORY_LEN - 2 - it_before;
801
0
    }
802
803
0
    if (usage) {
804
0
        unixctl_command_reply_error(conn,
805
0
                "Usage: ovs-appctl dpif-netdev/pmd-perf-log-set "
806
0
                "[on|off] [-b before] [-a after] [-e|-ne] "
807
0
                "[-us usec] [-q qlen]");
808
0
        return;
809
0
    }
810
811
0
    VLOG_INFO("pmd-perf-log-set: %s, before=%d, after=%d, extend=%s, "
812
0
              "us_thr=%d, q_thr=%d\n",
813
0
              on ? "on" : "off", it_before, it_after,
814
0
              extend ? "true" : "false", us_thr, q_thr);
815
0
    log_enabled = on;
816
0
    log_extend = extend;
817
0
    log_it_before = it_before;
818
0
    log_it_after = it_after;
819
0
    log_q_thr = q_thr;
820
0
    log_us_thr = us_thr;
821
0
    iter_cycle_threshold = (log_us_thr * tsc_hz) / 1000000L;
822
823
0
    unixctl_command_reply(conn, "");
824
0
}