Coverage Report

Created: 2026-06-10 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rtpproxy/src/rtpp_pcount.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2004-2006 Maxim Sobolev <sobomax@FreeBSD.org>
3
 * Copyright (c) 2006-2015 Sippy Software, Inc., http://www.sippysoft.com
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
 * SUCH DAMAGE.
26
 *
27
 */
28
29
#include <assert.h>
30
#include <stdatomic.h>
31
#include <stddef.h>
32
#include <stdbool.h>
33
#include <stdint.h>
34
#include <stdlib.h>
35
#include <string.h>
36
37
#include "config.h"
38
39
#include "rtpp_types.h"
40
#include "rtpp_mallocs.h"
41
#include "rtpp_refcnt.h"
42
#include "rtpp_codeptr.h"
43
#include "rtpp_log.h"
44
#include "rtpp_log_obj.h"
45
#include "rtpp_pcount.h"
46
#include "rtpp_pcount_fin.h"
47
48
struct _rtpps_pcount {
49
    _Atomic(unsigned long) nrelayed;
50
    _Atomic(unsigned long) ndropped;
51
    _Atomic(unsigned long) nignored;
52
};
53
54
0
#define TOP_DROPS_SIZE 4
55
56
struct rtpp_pcount_loc {
57
    _Atomic(const struct rtpp_codeptr *) ptr __attribute__((aligned(CACHELINE_SIZE)));
58
    _Atomic(unsigned long) cnt;
59
};
60
61
struct rtpp_pcount_priv {
62
    struct rtpp_pcount pub;
63
    struct _rtpps_pcount cnt;
64
    struct rtpp_pcount_loc top_drop_locs[TOP_DROPS_SIZE];
65
};
66
67
static void rtpp_pcount_dtor(struct rtpp_pcount_priv *);
68
static void rtpp_pcount_reg_reld(struct rtpp_pcount *);
69
static void rtpp_pcount_reg_drop(struct rtpp_pcount *, HERETYPE);
70
static void rtpp_pcount_reg_ignr(struct rtpp_pcount *);
71
static void rtpp_pcount_get_stats(struct rtpp_pcount *, struct rtpps_pcount *);
72
static void rtpp_pcount_log_drops(struct rtpp_pcount *, struct rtpp_log *);
73
74
DEFINE_SMETHODS(rtpp_pcount,
75
    .reg_reld = &rtpp_pcount_reg_reld,
76
    .reg_drop = &rtpp_pcount_reg_drop,
77
    .reg_ignr = &rtpp_pcount_reg_ignr,
78
    .get_stats = &rtpp_pcount_get_stats,
79
    .log_drops = &rtpp_pcount_log_drops
80
);
81
82
struct rtpp_pcount *
83
rtpp_pcount_ctor(void)
84
0
{
85
0
    struct rtpp_pcount_priv *pvt;
86
87
0
    pvt = rtpp_rzmalloc(sizeof(struct rtpp_pcount_priv), PVT_RCOFFS(pvt));
88
0
    if (pvt == NULL) {
89
0
        goto e0;
90
0
    }
91
0
    atomic_init(&(pvt->cnt.nrelayed), 0);
92
0
    atomic_init(&(pvt->cnt.ndropped), 0);
93
0
    atomic_init(&(pvt->cnt.nignored), 0);
94
0
    for (int i = 0; i < TOP_DROPS_SIZE; i++) {
95
0
        atomic_init(&(pvt->top_drop_locs[i].ptr), NULL);
96
0
        atomic_init(&(pvt->top_drop_locs[i].cnt), 0);
97
0
    }
98
0
    PUBINST_FININIT(&pvt->pub, pvt, rtpp_pcount_dtor);
99
0
    return ((&pvt->pub));
100
101
0
e0:
102
0
    return (NULL);
103
0
}
104
105
static void
106
rtpp_pcount_dtor(struct rtpp_pcount_priv *pvt)
107
0
{
108
109
0
    rtpp_pcount_fin(&(pvt->pub));
110
0
}
111
112
static void
113
rtpp_pcount_reg_reld(struct rtpp_pcount *self)
114
0
{
115
0
    struct rtpp_pcount_priv *pvt;
116
117
0
    PUB2PVT(self, pvt);
118
0
    atomic_fetch_add_explicit(&pvt->cnt.nrelayed, 1, memory_order_relaxed);
119
0
}
120
121
static void
122
rtpp_pcount_reg_drop(struct rtpp_pcount *self, HERETYPEARG)
123
0
{
124
0
    struct rtpp_pcount_priv *pvt;
125
126
0
    PUB2PVT(self, pvt);
127
0
    atomic_fetch_add_explicit(&pvt->cnt.ndropped, 1, memory_order_relaxed);
128
0
    assert(mlp != NULL);
129
0
    for (int i = 0; i < TOP_DROPS_SIZE; i++) {
130
0
        const struct rtpp_codeptr *old_ptr;
131
132
0
retry:
133
0
        old_ptr = atomic_load_explicit(&pvt->top_drop_locs[i].ptr, memory_order_relaxed);
134
0
        if (old_ptr == NULL) {
135
0
            if (atomic_compare_exchange_strong(&pvt->top_drop_locs[i].ptr, &old_ptr, mlp) != true) {
136
0
                goto retry;
137
0
            }
138
0
        }
139
0
        if (old_ptr != NULL && old_ptr != mlp)
140
0
            continue;
141
0
        atomic_fetch_add_explicit(&pvt->top_drop_locs[i].cnt, 1, memory_order_relaxed);
142
0
        break;
143
0
    }
144
0
}
145
146
static void rtpp_pcount_log_drops(struct rtpp_pcount *self, struct rtpp_log *log)
147
0
{
148
0
    struct rtpp_pcount_priv *pvt;
149
150
0
    PUB2PVT(self, pvt);
151
0
    for (int i = 0; i < TOP_DROPS_SIZE; i++) {
152
0
        const struct rtpp_codeptr *mlp;
153
0
        unsigned long cnt;
154
155
0
        mlp = atomic_load_explicit(&pvt->top_drop_locs[i].ptr, memory_order_relaxed);
156
0
        if (mlp == NULL)
157
0
            break;
158
0
        cnt = atomic_load_explicit(&pvt->top_drop_locs[i].cnt, memory_order_relaxed);
159
0
        if (cnt == 0)
160
0
            continue;
161
0
        RTPP_LOG(log, RTPP_LOG_INFO, CODEPTR_FMT(": %lu packets dropped here", mlp, cnt));
162
0
    }
163
0
}
164
165
166
static void
167
rtpp_pcount_reg_ignr(struct rtpp_pcount *self)
168
0
{
169
0
    struct rtpp_pcount_priv *pvt;
170
171
0
    PUB2PVT(self, pvt);
172
0
    atomic_fetch_add_explicit(&pvt->cnt.nignored, 1, memory_order_relaxed);
173
0
}
174
175
static void
176
rtpp_pcount_get_stats(struct rtpp_pcount *self, struct rtpps_pcount *ocnt)
177
0
{
178
0
    struct rtpp_pcount_priv *pvt;
179
180
0
    PUB2PVT(self, pvt);
181
0
    ocnt->nrelayed = atomic_load_explicit((&pvt->cnt.nrelayed), memory_order_relaxed);
182
0
    ocnt->ndropped = atomic_load_explicit((&pvt->cnt.ndropped), memory_order_relaxed);
183
    ocnt->nignored = atomic_load_explicit((&pvt->cnt.nignored), memory_order_relaxed);
184
0
}