void sprint_hw_stats(router_state *rs, char **buf, unsigned int *len) { char *buffer = calloc(4*HW_STATS_LEN + 1, sizeof(char)); unsigned int total_len = 0; char* port_names[8] = {"eth0", "eth1", "eth2", "eth3", "cpu0", "cpu1", "cpu2", "cpu3"}; lock_netfpga_stats(rs); int i; char line[HW_STATS_LEN]; bzero(line, HW_STATS_LEN); for (i = 0; i < 4; ++i) { snprintf(line, HW_STATS_LEN, "%-4s %12.2f PPS %12.2f kB/s %-4s %12.2f PPS %12.2f kB/s\n", port_names[i], rs->stats_avg[i][0], rs->stats_avg[i][1], port_names[i+4], rs->stats_avg[i+4][0], rs->stats_avg[i+4][1]); COPY_STRING(buffer, total_len, line); } unlock_netfpga_stats(rs); *buf = buffer; *len = total_len; }
/* * Stats thread for the NETFPGA Ports */ void* netfpga_stats(void* arg) { router_state* rs = (router_state*)arg; struct timeval now; uint32_t rx_packets, tx_packets, rx_bytes, tx_bytes; uint32_t rx_packets_diff, tx_packets_diff, rx_bytes_diff, tx_bytes_diff; long double timeDiff; int i, j; while (1) { // printf("or_netfpga.c: NetFPGA begin recording its stats...\n"); gettimeofday(&now, NULL); if (now.tv_sec == rs->stats_last_time.tv_sec) { /* must be a later usec time */ timeDiff = (((long double)(now.tv_usec - rs->stats_last_time.tv_usec)) / (long double)1000000); } else { timeDiff = (now.tv_sec - rs->stats_last_time.tv_sec - 1) + ((long double)((1000000 - rs->stats_last_time.tv_usec) + now.tv_usec) / (long double)1000000); } lock_netfpga_stats(rs); printf("port # rx_pkts tx_pkts rx_kbytes tx_kbytes pkt/s kbyte/s time\n"); printf("======================================================================================\n"); for (i = 0; i < 8; ++i) { /* read all the values */ rx_packets = get_rx_queue_num_pkts_received(&rs->netfpga, i); tx_packets = get_tx_queue_num_pkts_sent(&rs->netfpga, i); rx_bytes = get_rx_queue_num_bytes_received(&rs->netfpga, i); tx_bytes = get_tx_queue_num_bytes_sent(&rs->netfpga, i); /* compute differences */ rx_packets_diff = rx_packets - rs->stats_last[i][0]; tx_packets_diff = tx_packets - rs->stats_last[i][1]; rx_bytes_diff = rx_bytes - rs->stats_last[i][2]; tx_bytes_diff = tx_bytes - rs->stats_last[i][3]; /* bytes_diff will be in kB */ rx_bytes_diff /= 1024; tx_bytes_diff /= 1024; /* update averages */ rs->stats_avg[i][0] = ((double)rx_packets_diff) / timeDiff; rs->stats_avg[i][1] = ((double)tx_packets_diff) / timeDiff; rs->stats_avg[i][2] = ((double)rx_bytes_diff) / timeDiff; rs->stats_avg[i][3] = ((double)tx_bytes_diff) / timeDiff; /* store data back */ rs->stats_last[i][0] = rx_packets; rs->stats_last[i][1] = tx_packets; rs->stats_last[i][2] = rx_bytes; rs->stats_last[i][3] = tx_bytes; rs->stats_last_time = now; printf("%6d %10d %10d %10d %10d %8.2Lf %8.2Lf %u\n", i, rs->stats_last[i][0], rs->stats_last[i][1], rs->stats_last[i][2], rs->stats_last[i][3], rs->stats_avg[i][0] + rs->stats_avg[i][1], rs->stats_avg[i][2] + rs->stats_avg[i][3], now); } node* cur = NULL; pwospf_router* r = get_router_by_rid(rs->router_id, rs->pwospf_router_list); if (r != NULL) { cur = r->interface_list; j = 0; while (cur) { pwospf_interface* iface = (pwospf_interface*)cur->data; iface->rx_rate = (uint32_t)(rs->stats_avg[j][2]); iface->tx_rate = (uint32_t)(rs->stats_avg[j][3]); //printf("or_netfpga.c: iface->rx_rate[%d] = %d (kbytes)\n", j, iface->rx_rate); //printf("or_netfpga.c: iface->tx_rate[%d] = %d (kbytes)\n", j, iface->tx_rate); cur = cur->next; j++; } } unlock_netfpga_stats(rs); printf("======================================================================================\n"); printf("or_netfpga.c: NetFPGA end recording its stats.\n"); printf("or_netfpga.c: There are %d interfaces in this router.\n", j); usleep(500000); } return NULL; }