Coverage Report

Created: 2025-08-24 06:53

/src/rtpproxy/src/rtpp_pcount.c
Line
Count
Source (jump to first uncovered line)
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_priv {
57
    struct rtpp_pcount pub;
58
    struct _rtpps_pcount cnt;
59
    struct {
60
        _Atomic(const struct rtpp_codeptr *) ptr;
61
        _Atomic(unsigned long) cnt;
62
    } top_drop_locs[TOP_DROPS_SIZE];
63
};
64
65
static void rtpp_pcount_dtor(struct rtpp_pcount_priv *);
66
static void rtpp_pcount_reg_reld(struct rtpp_pcount *);
67
static void rtpp_pcount_reg_drop(struct rtpp_pcount *, HERETYPE);
68
static void rtpp_pcount_reg_ignr(struct rtpp_pcount *);
69
static void rtpp_pcount_get_stats(struct rtpp_pcount *, struct rtpps_pcount *);
70
static void rtpp_pcount_log_drops(struct rtpp_pcount *, struct rtpp_log *);
71
72
DEFINE_SMETHODS(rtpp_pcount,
73
    .reg_reld = &rtpp_pcount_reg_reld,
74
    .reg_drop = &rtpp_pcount_reg_drop,
75
    .reg_ignr = &rtpp_pcount_reg_ignr,
76
    .get_stats = &rtpp_pcount_get_stats,
77
    .log_drops = &rtpp_pcount_log_drops
78
);
79
80
struct rtpp_pcount *
81
rtpp_pcount_ctor(void)
82
0
{
83
0
    struct rtpp_pcount_priv *pvt;
84
85
0
    pvt = rtpp_rzmalloc(sizeof(struct rtpp_pcount_priv), PVT_RCOFFS(pvt));
86
0
    if (pvt == NULL) {
87
0
        goto e0;
88
0
    }
89
0
    atomic_init(&(pvt->cnt.nrelayed), 0);
90
0
    atomic_init(&(pvt->cnt.ndropped), 0);
91
0
    atomic_init(&(pvt->cnt.nignored), 0);
92
0
    for (int i = 0; i < TOP_DROPS_SIZE; i++) {
93
0
        atomic_init(&(pvt->top_drop_locs[i].ptr), NULL);
94
0
        atomic_init(&(pvt->top_drop_locs[i].cnt), 0);
95
0
    }
96
0
    PUBINST_FININIT(&pvt->pub, pvt, rtpp_pcount_dtor);
97
0
    return ((&pvt->pub));
98
99
0
e0:
100
0
    return (NULL);
101
0
}
102
103
static void
104
rtpp_pcount_dtor(struct rtpp_pcount_priv *pvt)
105
0
{
106
107
0
    rtpp_pcount_fin(&(pvt->pub));
108
0
}
109
110
static void
111
rtpp_pcount_reg_reld(struct rtpp_pcount *self)
112
0
{
113
0
    struct rtpp_pcount_priv *pvt;
114
115
0
    PUB2PVT(self, pvt);
116
0
    atomic_fetch_add_explicit(&pvt->cnt.nrelayed, 1, memory_order_relaxed);
117
0
}
118
119
static void
120
rtpp_pcount_reg_drop(struct rtpp_pcount *self, HERETYPEARG)
121
0
{
122
0
    struct rtpp_pcount_priv *pvt;
123
124
0
    PUB2PVT(self, pvt);
125
0
    atomic_fetch_add_explicit(&pvt->cnt.ndropped, 1, memory_order_relaxed);
126
0
    assert(mlp != NULL);
127
0
    for (int i = 0; i < TOP_DROPS_SIZE; i++) {
128
0
        const struct rtpp_codeptr *old_ptr;
129
130
0
retry:
131
0
        old_ptr = atomic_load_explicit(&pvt->top_drop_locs[i].ptr, memory_order_relaxed);
132
0
        if (old_ptr == NULL) {
133
0
            if (atomic_compare_exchange_strong(&pvt->top_drop_locs[i].ptr, &old_ptr, mlp) != true) {
134
0
                goto retry;
135
0
            }
136
0
        }
137
0
        if (old_ptr != NULL && old_ptr != mlp)
138
0
            continue;
139
0
        atomic_fetch_add_explicit(&pvt->top_drop_locs[i].cnt, 1, memory_order_relaxed);
140
0
        break;
141
0
    }
142
0
}
143
144
static void rtpp_pcount_log_drops(struct rtpp_pcount *self, struct rtpp_log *log)
145
0
{
146
0
    struct rtpp_pcount_priv *pvt;
147
148
0
    PUB2PVT(self, pvt);
149
0
    for (int i = 0; i < TOP_DROPS_SIZE; i++) {
150
0
        const struct rtpp_codeptr *mlp;
151
0
        unsigned long cnt;
152
153
0
        mlp = atomic_load_explicit(&pvt->top_drop_locs[i].ptr, memory_order_relaxed);
154
0
        if (mlp == NULL)
155
0
            break;
156
0
        cnt = atomic_load_explicit(&pvt->top_drop_locs[i].cnt, memory_order_relaxed);
157
0
        if (cnt == 0)
158
0
            continue;
159
0
        RTPP_LOG(log, RTPP_LOG_INFO, CODEPTR_FMT(": %lu packets dropped here", mlp, cnt));
160
0
    }
161
0
}
162
163
164
static void
165
rtpp_pcount_reg_ignr(struct rtpp_pcount *self)
166
0
{
167
0
    struct rtpp_pcount_priv *pvt;
168
169
0
    PUB2PVT(self, pvt);
170
0
    atomic_fetch_add_explicit(&pvt->cnt.nignored, 1, memory_order_relaxed);
171
0
}
172
173
static void
174
rtpp_pcount_get_stats(struct rtpp_pcount *self, struct rtpps_pcount *ocnt)
175
0
{
176
0
    struct rtpp_pcount_priv *pvt;
177
178
0
    PUB2PVT(self, pvt);
179
0
    ocnt->nrelayed = atomic_load_explicit((&pvt->cnt.nrelayed), memory_order_relaxed);
180
0
    ocnt->ndropped = atomic_load_explicit((&pvt->cnt.ndropped), memory_order_relaxed);
181
0
    ocnt->nignored = atomic_load_explicit((&pvt->cnt.nignored), memory_order_relaxed);
182
0
}