Skip to content

Commit e6421b8

Browse files
committed
refactor: move netdev stats into netdev struct
Signed-off-by: sakumisu <1203593632@qq.com>
1 parent bde602a commit e6421b8

File tree

9 files changed

+162
-316
lines changed

9 files changed

+162
-316
lines changed

include/ec_datagram.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef struct {
4545
ec_dlist_t queue;
4646
ec_dlist_t ext_queue;
4747
ec_dlist_t sent;
48-
ec_netdev_index_t netdev_idx; /**< Netdev via which the datagram shall be / was sent. */
48+
uint8_t netdev_idx; /**< Netdev via which the datagram shall be / was sent. */
4949
ec_datagram_type_t type; /**< Datagram type (APRD, BWR, etc.). */
5050
bool static_alloc; /**< True, if \a data is statically allocated. */
5151
uint8_t address[EC_ADDR_LEN]; /**< Recipient address. */

include/ec_def.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ typedef enum {
127127
/**< Acknowledge/Error bit (no actual state) */
128128
} ec_slave_state_t;
129129

130+
typedef enum {
131+
EC_PORT_NOT_IMPLEMENTED, /**< Port is not implemented. */
132+
EC_PORT_NOT_CONFIGURED, /**< Port is not configured. */
133+
EC_PORT_EBUS, /**< Port is an E-Bus. */
134+
EC_PORT_MII /**< Port is a MII. */
135+
} ec_slave_port_desc_t;
136+
130137
/** Slave information interface CANopen over EtherCAT details flags.
131138
*/
132139
typedef struct {

include/ec_master.h

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,14 @@
3232
#include "ec_eoe.h"
3333
#include "ec_cmd.h"
3434

35-
/** Netdev statistics.
36-
*/
37-
typedef struct {
38-
uint64_t tx_count; /**< Number of frames sent. */
39-
uint64_t last_tx_count; /**< Number of frames sent of last statistics cycle. */
40-
uint64_t rx_count; /**< Number of frames received. */
41-
uint64_t last_rx_count; /**< Number of frames received of last statistics cycle. */
42-
uint64_t tx_bytes; /**< Number of bytes sent. */
43-
uint64_t last_tx_bytes; /**< Number of bytes sent of last statistics cycle. */
44-
uint64_t rx_bytes; /**< Number of bytes received. */
45-
uint64_t last_rx_bytes; /**< Number of bytes received of last statistics cycle. */
46-
uint64_t last_loss; /**< Tx/Rx difference of last statistics cycle. */
47-
int32_t tx_frame_rates[EC_RATE_COUNT]; /**< Transmit rates in frames/s for different statistics cycle periods.*/
48-
int32_t rx_frame_rates[EC_RATE_COUNT]; /**< Receive rates in frames/s for different statistics cycle periods.*/
49-
int32_t tx_byte_rates[EC_RATE_COUNT]; /**< Transmit rates in byte/s for different statistics cycle periods. */
50-
int32_t rx_byte_rates[EC_RATE_COUNT]; /**< Receive rates in byte/s for different statistics cycle periods. */
51-
int32_t loss_rates[EC_RATE_COUNT]; /**< Frame loss rates for different statistics cycle periods. */
52-
uint64_t last_jiffies; /**< Jiffies of last statistic cycle. */
53-
} ec_netdev_stats_t;
54-
55-
/** Cyclic statistics.
56-
*/
5735
typedef struct {
58-
unsigned int timeouts; /**< datagram timeouts */
59-
unsigned int corrupted; /**< corrupted frames */
60-
unsigned int unmatched; /**< unmatched datagrams (received, but not queued any longer) */
61-
unsigned long output_jiffies; /**< time of last output */
62-
} ec_stats_t;
36+
unsigned int timeouts;
37+
unsigned int corrupted;
38+
unsigned int unmatched;
39+
} ec_master_stats_t;
6340

6441
typedef enum {
65-
EC_ORPHANED, /**< Orphaned phase. The master has no Ethernet device attached. */
42+
EC_UNKNOWN, /**< Unknown phase. No Ethernet device is attached. */
6643
EC_IDLE, /**< Idle phase. An Ethernet device is attached, but the master is not in use, yet. */
6744
EC_OPERATION /**< Operation phase. The master was requested by a realtime application. */
6845
} ec_master_phase_t;
@@ -81,32 +58,32 @@ typedef struct ec_master {
8158
uint8_t index;
8259
ec_netdev_t *netdev[CONFIG_EC_MAX_NETDEVS];
8360
bool link_state[CONFIG_EC_MAX_NETDEVS];
84-
uint32_t slaves_responding[CONFIG_EC_MAX_NETDEVS];
61+
uint32_t slaves_working_counter[CONFIG_EC_MAX_NETDEVS];
8562
ec_slave_state_t slaves_state[CONFIG_EC_MAX_NETDEVS];
86-
ec_netdev_stats_t netdev_stats;
87-
ec_stats_t stats;
88-
ec_master_phase_t phase;
89-
bool active; /**< Master is started. */
63+
64+
bool started; /**< Master is started. */
9065
bool scan_done; /**< Slave scan is done. */
9166
bool rescan_request; /**< Rescan requested. */
67+
ec_master_stats_t stats;
68+
ec_master_phase_t phase;
9269

9370
ec_datagram_t main_datagram; /**< Main datagram for slave scan & state change & config & sii */
9471

9572
ec_dlist_t datagram_queue; /**< Queue of pending datagrams*/
9673
ec_dlist_t pdo_datagram_queue; /**< Queue of pdo datagrams*/
9774
uint8_t datagram_index;
9875

99-
ec_slave_t *dc_ref_clock; /**< DC reference clock slave. */
100-
ec_datagram_t dc_ref_sync_datagram; /**< Datagram used for synchronizing the reference clock to the master clock. */
101-
ec_datagram_t dc_all_sync_datagram; /**< Datagram used for synchronizing all slaves to the dc ref clock. */
102-
ec_datagram_t systime_diff_mon_datagram; /**< Datagram used for reading the system time difference between master and reference clock. */
76+
ec_slave_t *dc_ref_clock; /**< DC reference clock slave. */
77+
ec_datagram_t dc_ref_sync_datagram; /**< Datagram used for synchronizing the reference clock to the master clock. */
78+
ec_datagram_t dc_all_sync_datagram; /**< Datagram used for synchronizing all slaves to the dc ref clock. */
10379

80+
ec_datagram_t systime_diff_mon_datagram; /**< Datagram used for reading the system time difference between master and reference clock. */
81+
bool systime_diff_enable;
10482
uint32_t min_systime_diff;
10583
uint32_t max_systime_diff;
10684
uint32_t curr_systime_diff;
10785
uint32_t systime_diff_count;
10886
uint64_t total_systime_diff;
109-
bool systime_diff_enable;
11087

11188
bool dc_sync_with_dc_ref_enable; /**< true: Sync the reference clock by dc ref clock, false: by master */
11289
uint32_t cycle_time; /**< Cycle time [ns]. */
@@ -139,9 +116,9 @@ typedef struct ec_master {
139116
bool nonperiod_suspend;
140117

141118
uint8_t pdo_buffer[CONFIG_EC_MAX_NETDEVS][CONFIG_EC_MAX_PDO_BUFSIZE];
142-
uint32_t actual_pdo_size;
143-
uint32_t expected_working_counter;
144-
uint32_t actual_working_counter;
119+
uint32_t actual_pdo_size; /**< Actual PDO size for current setting. */
120+
uint32_t expected_working_counter; /**< Expected working counter for PDO datagrams. */
121+
uint32_t actual_working_counter; /**< Actual working counter for PDO datagrams. */
145122
} ec_master_t;
146123

147124
int ec_master_init(ec_master_t *master, uint8_t master_index);

include/ec_netdev.h

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,39 @@
1010

1111
typedef struct ec_master ec_master_t;
1212

13-
typedef struct ec_netdev {
14-
ec_master_t *master;
15-
struct chry_phy_device phydev;
16-
ec_netdev_index_t index;
17-
char name[24];
18-
uint8_t mac_addr[6];
19-
bool link_state;
20-
uint8_t tx_frame_index;
21-
unsigned long jiffies_poll;
22-
23-
// Frame statistics
13+
/** Netdev statistics.
14+
*/
15+
typedef struct {
2416
uint64_t tx_count; /**< Number of frames sent. */
25-
uint64_t last_tx_count; /**< Number of frames sent of last statistics cycle.*/
17+
uint64_t last_tx_count; /**< Number of frames sent of last statistics cycle. */
2618
uint64_t rx_count; /**< Number of frames received. */
27-
uint64_t last_rx_count; /**< Number of frames received of last statistics cycle.*/
19+
uint64_t last_rx_count; /**< Number of frames received of last statistics cycle. */
2820
uint64_t tx_bytes; /**< Number of bytes sent. */
29-
uint64_t last_tx_bytes; /**< Number of bytes sent of last statistics cycle.*/
21+
uint64_t last_tx_bytes; /**< Number of bytes sent of last statistics cycle. */
3022
uint64_t rx_bytes; /**< Number of bytes received. */
31-
uint64_t last_rx_bytes; /**< Number of bytes received of last statistics cycle.*/
23+
uint64_t last_rx_bytes; /**< Number of bytes received of last statistics cycle. */
3224
uint64_t tx_errors; /**< Number of transmit errors. */
25+
uint64_t loss_count; /**< Number of lost frames. */
3326
int32_t tx_frame_rates[EC_RATE_COUNT]; /**< Transmit rates in frames/s for different statistics cycle periods.*/
3427
int32_t rx_frame_rates[EC_RATE_COUNT]; /**< Receive rates in frames/s for different statistics cycle periods.*/
35-
int32_t tx_byte_rates[EC_RATE_COUNT]; /**< Transmit rates in byte/s for different statistics cycle periods.*/
36-
int32_t rx_byte_rates[EC_RATE_COUNT]; /**< Receive rates in byte/s for different statistics cycle periods.*/
28+
int32_t tx_byte_rates[EC_RATE_COUNT]; /**< Transmit rates in byte/s for different statistics cycle periods. */
29+
int32_t rx_byte_rates[EC_RATE_COUNT]; /**< Receive rates in byte/s for different statistics cycle periods. */
30+
int32_t loss_rates[EC_RATE_COUNT]; /**< Frame loss rates for different statistics cycle periods. */
31+
uint64_t last_jiffies; /**< Jiffies of last statistic cycle. */
32+
} ec_netdev_stats_t;
33+
34+
typedef struct ec_netdev {
35+
ec_master_t *master;
36+
struct chry_phy_device phydev;
37+
uint8_t index;
38+
char name[24];
39+
uint8_t mac_addr[6];
40+
bool link_state;
41+
uint8_t tx_frame_index;
3742

43+
ec_netdev_stats_t stats;
3844
} ec_netdev_t;
3945

40-
void ec_netdev_clear_stats(ec_netdev_t *netdev);
4146
void ec_netdev_update_stats(ec_netdev_t *netdev);
4247

4348
ec_netdev_t *ec_netdev_init(uint8_t netdev_index);

include/ec_slave.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ typedef struct {
3737
ec_sync_signal_t dc_sync[EC_SYNC_SIGNAL_COUNT]; /**< DC sync signals. */
3838
} ec_slave_config_t;
3939

40-
/** EtherCAT slave port descriptor.
41-
*/
42-
typedef enum {
43-
EC_PORT_NOT_IMPLEMENTED, /**< Port is not implemented. */
44-
EC_PORT_NOT_CONFIGURED, /**< Port is not configured. */
45-
EC_PORT_EBUS, /**< Port is an E-Bus. */
46-
EC_PORT_MII /**< Port is a MII. */
47-
} ec_slave_port_desc_t;
48-
4940
/** EtherCAT slave port information.
5041
*/
5142
typedef struct {
@@ -63,13 +54,13 @@ typedef struct {
6354
} ec_slave_port_t;
6455

6556
typedef struct ec_slave {
66-
uint32_t index; /**< Index of the slave in the master slave array. */
67-
ec_master_t *master; /**< Master owning the slave. */
68-
ec_netdev_index_t netdev_idx; /**< Index of device the slave responds on. */
57+
uint32_t index; /**< Index of the slave in the master slave array. */
58+
ec_master_t *master; /**< Master owning the slave. */
59+
uint8_t netdev_idx; /**< Index of device the slave responds on. */
6960

7061
uint16_t autoinc_address; /**< Auto-increment address. */
7162
uint16_t station_address; /**< Configured station address. */
72-
uint16_t effective_alias; /**< Effective alias address. */
63+
uint16_t alias_address; /**< alias address. */
7364

7465
ec_slave_port_t ports[EC_MAX_PORTS]; /**< Port information. */
7566

src/ec_cmd.c

Lines changed: 36 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,27 @@ void ec_master_cmd_master(ec_master_t *master)
8585
{
8686
unsigned int dev_idx, j;
8787
uint64_t lost;
88-
double perc;
8988
int colwidth = 8;
89+
ec_netdev_stats_t netdev_stats;
90+
uintptr_t flags;
9091

9192
EC_LOG_RAW("Master%d\n", master->index);
9293
EC_LOG_RAW(" Phase: ");
9394
switch (master->phase) {
9495
case 0:
95-
EC_LOG_RAW("Waiting for device(s)...");
96+
EC_LOG_RAW("Waiting for device attached...\n");
9697
break;
9798
case 1:
98-
EC_LOG_RAW("Idle");
99+
EC_LOG_RAW("Idle\n");
99100
break;
100101
case 2:
101-
EC_LOG_RAW("Operation");
102+
EC_LOG_RAW("Operation\n");
102103
break;
103104
default:
104-
EC_LOG_RAW("???");
105+
EC_LOG_RAW("Unknown\n");
106+
break;
105107
}
106108

107-
EC_LOG_RAW("\n");
108109
EC_LOG_RAW(" Slaves: %u\n", master->slave_count);
109110
EC_LOG_RAW(" Ethernet net devices:\n");
110111

@@ -118,89 +119,56 @@ void ec_master_cmd_master(ec_master_t *master)
118119
master->netdev[dev_idx]->mac_addr[4],
119120
master->netdev[dev_idx]->mac_addr[5]);
120121
EC_LOG_RAW(" Link: %s\n", master->netdev[dev_idx]->link_state ? "UP" : "DOWN");
121-
EC_LOG_RAW(" Tx frames: %llu\n", master->netdev[dev_idx]->tx_count);
122-
EC_LOG_RAW(" Tx bytes: %llu\n", master->netdev[dev_idx]->tx_bytes);
123-
EC_LOG_RAW(" Rx frames: %llu\n", master->netdev[dev_idx]->rx_count);
124-
EC_LOG_RAW(" Rx bytes: %llu\n", master->netdev[dev_idx]->rx_bytes);
125-
EC_LOG_RAW(" Tx errors: %llu\n", master->netdev[dev_idx]->tx_errors);
122+
123+
flags = ec_osal_enter_critical_section();
124+
memcpy(&netdev_stats, &master->netdev[dev_idx]->stats, sizeof(ec_netdev_stats_t));
125+
ec_osal_leave_critical_section(flags);
126+
127+
EC_LOG_RAW(" Tx frames: %llu\n", netdev_stats.tx_count);
128+
EC_LOG_RAW(" Tx bytes: %llu\n", netdev_stats.tx_bytes);
129+
EC_LOG_RAW(" Rx frames: %llu\n", netdev_stats.rx_count);
130+
EC_LOG_RAW(" Rx bytes: %llu\n", netdev_stats.rx_bytes);
131+
EC_LOG_RAW(" Tx errors: %llu\n", netdev_stats.tx_errors);
132+
133+
lost = netdev_stats.loss_count;
134+
if (lost == 1)
135+
lost = 0;
136+
EC_LOG_RAW(" Lost frames: %llu\n", lost);
126137

127138
EC_LOG_RAW(" Tx frame rate [1/s]: ");
128139
for (j = 0; j < EC_RATE_COUNT; j++) {
129-
EC_LOG_RAW("%*.*f", colwidth, 0, master->netdev[dev_idx]->tx_frame_rates[j] / 1000.0);
140+
EC_LOG_RAW("%*.*f", colwidth, 0, netdev_stats.tx_frame_rates[j] / 1000.0);
130141
if (j < EC_RATE_COUNT - 1)
131142
EC_LOG_RAW(" ");
132143
}
133144
EC_LOG_RAW("\n Tx rate [KByte/s]: ");
134145
for (j = 0; j < EC_RATE_COUNT; j++) {
135-
EC_LOG_RAW("%*.*f", colwidth, 1, master->netdev[dev_idx]->tx_byte_rates[j] / 1024.0);
146+
EC_LOG_RAW("%*.*f", colwidth, 1, netdev_stats.tx_byte_rates[j] / 1024.0);
136147
if (j < EC_RATE_COUNT - 1)
137148
EC_LOG_RAW(" ");
138149
}
139150
EC_LOG_RAW("\n Rx frame rate [1/s]: ");
140151
for (j = 0; j < EC_RATE_COUNT; j++) {
141-
EC_LOG_RAW("%*.*f", colwidth, 0, master->netdev[dev_idx]->rx_frame_rates[j] / 1000.0);
152+
EC_LOG_RAW("%*.*f", colwidth, 0, netdev_stats.rx_frame_rates[j] / 1000.0);
142153
if (j < EC_RATE_COUNT - 1)
143154
EC_LOG_RAW(" ");
144155
}
145156
EC_LOG_RAW("\n Rx rate [KByte/s]: ");
146157
for (j = 0; j < EC_RATE_COUNT; j++) {
147-
EC_LOG_RAW("%*.*f", colwidth, 1, master->netdev[dev_idx]->rx_byte_rates[j] / 1024.0);
158+
EC_LOG_RAW("%*.*f", colwidth, 1, netdev_stats.rx_byte_rates[j] / 1024.0);
148159
if (j < EC_RATE_COUNT - 1)
149160
EC_LOG_RAW(" ");
150161
}
151-
EC_LOG_RAW("\n");
152-
}
153162

154-
lost = master->netdev_stats.tx_count - master->netdev_stats.rx_count;
155-
if (lost == 1)
156-
lost = 0;
157-
EC_LOG_RAW(" Common:\n");
158-
EC_LOG_RAW(" Tx frames: %llu\n", master->netdev_stats.tx_count);
159-
EC_LOG_RAW(" Tx bytes: %llu\n", master->netdev_stats.tx_bytes);
160-
EC_LOG_RAW(" Rx frames: %llu\n", master->netdev_stats.rx_count);
161-
EC_LOG_RAW(" Rx bytes: %llu\n", master->netdev_stats.rx_bytes);
162-
EC_LOG_RAW(" Lost frames: %llu\n", lost);
163-
164-
EC_LOG_RAW(" Tx frame rate [1/s]: ");
165-
for (j = 0; j < EC_RATE_COUNT; j++) {
166-
EC_LOG_RAW("%*.*f", colwidth, 0, master->netdev_stats.tx_frame_rates[j] / 1000.0);
167-
if (j < EC_RATE_COUNT - 1)
168-
EC_LOG_RAW(" ");
169-
}
170-
EC_LOG_RAW("\n Tx rate [KByte/s]: ");
171-
for (j = 0; j < EC_RATE_COUNT; j++) {
172-
EC_LOG_RAW("%*.*f", colwidth, 1, master->netdev_stats.tx_byte_rates[j] / 1024.0);
173-
if (j < EC_RATE_COUNT - 1)
174-
EC_LOG_RAW(" ");
175-
}
176-
EC_LOG_RAW("\n Rx frame rate [1/s]: ");
177-
for (j = 0; j < EC_RATE_COUNT; j++) {
178-
EC_LOG_RAW("%*.*f", colwidth, 0, master->netdev_stats.rx_frame_rates[j] / 1000.0);
179-
if (j < EC_RATE_COUNT - 1)
180-
EC_LOG_RAW(" ");
181-
}
182-
EC_LOG_RAW("\n Rx rate [KByte/s]: ");
183-
for (j = 0; j < EC_RATE_COUNT; j++) {
184-
EC_LOG_RAW("%*.*f", colwidth, 1, master->netdev_stats.rx_byte_rates[j] / 1024.0);
185-
if (j < EC_RATE_COUNT - 1)
186-
EC_LOG_RAW(" ");
187-
}
188-
EC_LOG_RAW("\n Loss rate [1/s]: ");
189-
for (j = 0; j < EC_RATE_COUNT; j++) {
190-
EC_LOG_RAW("%*.*f", colwidth, 0, master->netdev_stats.loss_rates[j] / 1000.0);
191-
if (j < EC_RATE_COUNT - 1)
192-
EC_LOG_RAW(" ");
193-
}
194-
EC_LOG_RAW("\n Frame loss [%%]: ");
195-
for (j = 0; j < EC_RATE_COUNT; j++) {
196-
perc = 0.0;
197-
if (master->netdev_stats.tx_frame_rates[j])
198-
perc = 100.0 * master->netdev_stats.loss_rates[j] / master->netdev_stats.tx_frame_rates[j];
199-
EC_LOG_RAW("%*.*f", colwidth, 1, perc);
200-
if (j < EC_RATE_COUNT - 1)
201-
EC_LOG_RAW(" ");
163+
EC_LOG_RAW("\n Loss rate [1/s]: ");
164+
for (j = 0; j < EC_RATE_COUNT; j++) {
165+
EC_LOG_RAW("%*.*f", colwidth, 0, netdev_stats.loss_rates[j] / 1000.0);
166+
if (j < EC_RATE_COUNT - 1)
167+
EC_LOG_RAW(" ");
168+
}
169+
170+
EC_LOG_RAW("\n");
202171
}
203-
EC_LOG_RAW("\n");
204172
}
205173

206174
static void ec_cmd_show_slave_detail(ec_master_t *master, uint32_t slave_idx)
@@ -222,8 +190,8 @@ static void ec_cmd_show_slave_detail(ec_master_t *master, uint32_t slave_idx)
222190

223191
EC_LOG_RAW("=== Master %d, Slave %d ===\n", master->index, slave_idx);
224192

225-
if (slave_data.effective_alias != 0) {
226-
EC_LOG_RAW("Alias: 0x%04x\n", slave_data.effective_alias);
193+
if (slave_data.alias_address != 0) {
194+
EC_LOG_RAW("Alias: 0x%04x\n", slave_data.alias_address);
227195
}
228196

229197
EC_LOG_RAW("Device: %s\n", master->netdev[slave->netdev_idx]->name);

0 commit comments

Comments
 (0)