master/master.c
branchredundancy
changeset 2158 69f2b2702336
parent 2157 8fa0571f9996
child 2267 2d36f36a433c
equal deleted inserted replaced
2157:8fa0571f9996 2158:69f2b2702336
    80  */
    80  */
    81 static unsigned long ext_injection_timeout_jiffies;
    81 static unsigned long ext_injection_timeout_jiffies;
    82 
    82 
    83 #endif
    83 #endif
    84 
    84 
       
    85 /** List of intervals for statistics [s].
       
    86  */
       
    87 const unsigned int rate_intervals[] = {
       
    88     1, 10, 60
       
    89 };
       
    90 
    85 /*****************************************************************************/
    91 /*****************************************************************************/
    86 
    92 
    87 void ec_master_clear_slave_configs(ec_master_t *);
    93 void ec_master_clear_slave_configs(ec_master_t *);
    88 void ec_master_clear_domains(ec_master_t *);
    94 void ec_master_clear_domains(ec_master_t *);
    89 static int ec_master_idle_thread(void *);
    95 static int ec_master_idle_thread(void *);
    90 static int ec_master_operation_thread(void *);
    96 static int ec_master_operation_thread(void *);
    91 #ifdef EC_EOE
    97 #ifdef EC_EOE
    92 static int ec_master_eoe_thread(void *);
    98 static int ec_master_eoe_thread(void *);
    93 #endif
    99 #endif
    94 void ec_master_find_dc_ref_clock(ec_master_t *);
   100 void ec_master_find_dc_ref_clock(ec_master_t *);
       
   101 void ec_master_clear_device_stats(ec_master_t *);
       
   102 void ec_master_update_device_stats(ec_master_t *);
    95 
   103 
    96 /*****************************************************************************/
   104 /*****************************************************************************/
    97 
   105 
    98 /** Static variables initializer.
   106 /** Static variables initializer.
    99 */
   107 */
   132 
   140 
   133     sema_init(&master->master_sem, 1);
   141     sema_init(&master->master_sem, 1);
   134 
   142 
   135     master->main_mac = main_mac;
   143     master->main_mac = main_mac;
   136     master->backup_mac = backup_mac;
   144     master->backup_mac = backup_mac;
       
   145     ec_master_clear_device_stats(master);
   137 
   146 
   138     sema_init(&master->device_sem, 1);
   147     sema_init(&master->device_sem, 1);
   139 
   148 
   140     master->phase = EC_ORPHANED;
   149     master->phase = EC_ORPHANED;
   141     master->active = 0;
   150     master->active = 0;
  1146             master->stats.unmatched = 0;
  1155             master->stats.unmatched = 0;
  1147         }
  1156         }
  1148     }
  1157     }
  1149 }
  1158 }
  1150 
  1159 
       
  1160 /*****************************************************************************/
       
  1161 
       
  1162 /** Clears the common device statistics.
       
  1163  */
       
  1164 void ec_master_clear_device_stats(
       
  1165         ec_master_t *master /**< EtherCAT master */
       
  1166         )
       
  1167 {
       
  1168     unsigned int i;
       
  1169 
       
  1170     // zero frame statistics
       
  1171     master->device_stats.tx_count = 0;
       
  1172     master->device_stats.last_tx_count = 0;
       
  1173     master->device_stats.rx_count = 0;
       
  1174     master->device_stats.last_rx_count = 0;
       
  1175     master->device_stats.tx_bytes = 0;
       
  1176     master->device_stats.last_tx_bytes = 0;
       
  1177     master->device_stats.rx_bytes = 0;
       
  1178     master->device_stats.last_rx_bytes = 0;
       
  1179     master->device_stats.last_loss = 0;
       
  1180 
       
  1181     for (i = 0; i < EC_RATE_COUNT; i++) {
       
  1182         master->device_stats.tx_frame_rates[i] = 0;
       
  1183         master->device_stats.tx_byte_rates[i] = 0;
       
  1184         master->device_stats.loss_rates[i] = 0;
       
  1185     }
       
  1186 }
       
  1187 
       
  1188 /*****************************************************************************/
       
  1189 
       
  1190 /** Updates the common device statistics.
       
  1191  */
       
  1192 void ec_master_update_device_stats(
       
  1193         ec_master_t *master /**< EtherCAT master */
       
  1194         )
       
  1195 {
       
  1196     ec_device_stats_t *s = &master->device_stats;
       
  1197     u32 tx_frame_rate, rx_frame_rate, tx_byte_rate, rx_byte_rate;
       
  1198     u64 loss;
       
  1199     s32 loss_rate;
       
  1200     unsigned int i;
       
  1201 
       
  1202     // frame statistics
       
  1203     if (likely(jiffies - s->jiffies < HZ)) {
       
  1204         return;
       
  1205     }
       
  1206 
       
  1207     tx_frame_rate = (u32) (s->tx_count - s->last_tx_count) * 1000;
       
  1208     rx_frame_rate = (u32) (s->rx_count - s->last_rx_count) * 1000;
       
  1209     tx_byte_rate = (s->tx_bytes - s->last_tx_bytes);
       
  1210     rx_byte_rate = (s->rx_bytes - s->last_rx_bytes);
       
  1211     loss = s->tx_count - s->rx_count;
       
  1212     loss_rate = (s32) (loss - s->last_loss) * 1000;
       
  1213 
       
  1214     for (i = 0; i < EC_RATE_COUNT; i++) {
       
  1215         unsigned int n = rate_intervals[i];
       
  1216         s->tx_frame_rates[i] =
       
  1217             (s->tx_frame_rates[i] * (n - 1) + tx_frame_rate) / n;
       
  1218         s->rx_frame_rates[i] =
       
  1219             (s->rx_frame_rates[i] * (n - 1) + rx_frame_rate) / n;
       
  1220         s->tx_byte_rates[i] =
       
  1221             (s->tx_byte_rates[i] * (n - 1) + tx_byte_rate) / n;
       
  1222         s->rx_byte_rates[i] =
       
  1223             (s->rx_byte_rates[i] * (n - 1) + rx_byte_rate) / n;
       
  1224         s->loss_rates[i] =
       
  1225             (s->loss_rates[i] * (n - 1) + loss_rate) / n;
       
  1226 
       
  1227     }
       
  1228     s->last_tx_count = s->tx_count;
       
  1229     s->last_rx_count = s->rx_count;
       
  1230     s->last_tx_bytes = s->tx_bytes;
       
  1231     s->last_rx_bytes = s->rx_bytes;
       
  1232 
       
  1233     ec_device_update_stats(&master->main_device);
       
  1234     ec_device_update_stats(&master->backup_device);
       
  1235 
       
  1236     s->jiffies = jiffies;
       
  1237 }
  1151 
  1238 
  1152 /*****************************************************************************/
  1239 /*****************************************************************************/
  1153 
  1240 
  1154 #ifdef EC_USE_HRTIMER
  1241 #ifdef EC_USE_HRTIMER
  1155 
  1242 
  2127     // receive datagrams
  2214     // receive datagrams
  2128     ec_device_poll(&master->main_device);
  2215     ec_device_poll(&master->main_device);
  2129     if (master->backup_device.dev) {
  2216     if (master->backup_device.dev) {
  2130         ec_device_poll(&master->backup_device);
  2217         ec_device_poll(&master->backup_device);
  2131     }
  2218     }
       
  2219     ec_master_update_device_stats(master);
  2132 
  2220 
  2133     // dequeue all datagrams that timed out
  2221     // dequeue all datagrams that timed out
  2134     list_for_each_entry_safe(datagram, next, &master->datagram_queue, queue) {
  2222     list_for_each_entry_safe(datagram, next, &master->datagram_queue, queue) {
  2135         if (datagram->state != EC_DATAGRAM_SENT) continue;
  2223         if (datagram->state != EC_DATAGRAM_SENT) continue;
  2136 
  2224