forked from eclipse-cyclonedds/cyclonedds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netload.c
129 lines (115 loc) · 3.14 KB
/
netload.c
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
// Copyright(c) 2019 to 2020 ZettaScale Technology and others
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
// v. 1.0 which is available at
// http://www.eclipse.org/org/documents/edl-v10.php.
//
// SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "dds/dds.h"
#include "dds/ddsrt/heap.h"
#include "dds/ddsrt/string.h"
#include "dds/ddsrt/netstat.h"
#include "dds/ddsrt/misc.h"
#include "netload.h"
#if DDSRT_HAVE_NETSTAT
struct record_netload_state {
struct ddsrt_netstat_control *ctrl;
char *name;
double bw;
bool errored;
bool data_valid;
dds_time_t tprev;
uint64_t ibytes;
uint64_t obytes;
};
void record_netload (struct record_netload_state *st, const char *prefix, dds_time_t tnow)
{
if (st && !st->errored)
{
struct ddsrt_netstat x;
dds_return_t ret = ddsrt_netstat_get (st->ctrl, &x);
st->errored = (ret == DDS_RETCODE_ERROR);
if (ret == DDS_RETCODE_OK)
{
if (st->data_valid)
{
/* interface speeds are in bits/s, so convert bytes to bits */
const double dt = (double) (tnow - st->tprev) / 1e9;
const double dx = 8 * (double) (x.obytes - st->obytes) / dt;
const double dr = 8 * (double) (x.ibytes - st->ibytes) / dt;
if (st->bw > 0)
{
const double dxpct = 100.0 * dx / st->bw;
const double drpct = 100.0 * dr / st->bw;
if (dxpct >= 0.5 || drpct >= 0.5)
{
printf ("%s %s: xmit %.0f%% recv %.0f%% [%"PRIu64" %"PRIu64"]\n",
prefix, st->name, dxpct, drpct, x.obytes, x.ibytes);
}
}
else if (dx >= 1e5 || dr >= 1e5) // 100kb/s is arbitrary
{
printf ("%s %s: xmit %.2f Mb/s recv %.2f Mb/s [%"PRIu64" %"PRIu64"]\n",
prefix, st->name, dx / 1e6, dr / 1e6, x.obytes, x.ibytes);
}
}
st->obytes = x.obytes;
st->ibytes = x.ibytes;
st->tprev = tnow;
st->data_valid = true;
}
}
}
struct record_netload_state *record_netload_new (const char *dev, double bw)
{
DDSRT_WARNING_MSVC_OFF(4996);
struct record_netload_state *st;
st = malloc (sizeof (*st));
assert (st);
if (ddsrt_netstat_new (&st->ctrl, dev) != DDS_RETCODE_OK)
{
free (st);
return NULL;
}
st->name = strdup (dev);
assert (st->name);
st->bw = bw;
st->data_valid = false;
st->errored = false;
record_netload (st, "", dds_time ());
return st;
DDSRT_WARNING_MSVC_ON(4996);
}
void record_netload_free (struct record_netload_state *st)
{
if (st)
{
ddsrt_netstat_free (st->ctrl);
free (st->name);
free (st);
}
}
#else
void record_netload (struct record_netload_state *st, const char *prefix, dds_time_t tnow)
{
(void) st;
(void) prefix;
(void ) tnow;
}
struct record_netload_state *record_netload_new (const char *dev, double bw)
{
(void) dev;
(void) bw;
return NULL;
}
void record_netload_free (struct record_netload_state *st)
{
(void) st;
}
#endif