+static inline struct lnet_comm_count *
+get_stats_counts(struct lnet_element_stats *stats,
+ enum lnet_stats_type stats_type)
+{
+ switch (stats_type) {
+ case LNET_STATS_TYPE_SEND:
+ return &stats->el_send_stats;
+ case LNET_STATS_TYPE_RECV:
+ return &stats->el_recv_stats;
+ case LNET_STATS_TYPE_DROP:
+ return &stats->el_drop_stats;
+ default:
+ CERROR("Unknown stats type\n");
+ }
+
+ return NULL;
+}
+
+void lnet_incr_stats(struct lnet_element_stats *stats, lnet_msg_type_t msg_type,
+ enum lnet_stats_type stats_type)
+{
+ struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
+ if (!counts)
+ return;
+
+ switch (msg_type) {
+ case LNET_MSG_ACK:
+ atomic_inc(&counts->co_ack_count);
+ break;
+ case LNET_MSG_PUT:
+ atomic_inc(&counts->co_put_count);
+ break;
+ case LNET_MSG_GET:
+ atomic_inc(&counts->co_get_count);
+ break;
+ case LNET_MSG_REPLY:
+ atomic_inc(&counts->co_reply_count);
+ break;
+ case LNET_MSG_HELLO:
+ atomic_inc(&counts->co_hello_count);
+ break;
+ default:
+ CERROR("There is a BUG in the code. Unknown message type\n");
+ break;
+ }
+}
+
+__u32 lnet_sum_stats(struct lnet_element_stats *stats,
+ enum lnet_stats_type stats_type)
+{
+ struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
+ if (!counts)
+ return 0;
+
+ return (atomic_read(&counts->co_ack_count) +
+ atomic_read(&counts->co_put_count) +
+ atomic_read(&counts->co_get_count) +
+ atomic_read(&counts->co_reply_count) +
+ atomic_read(&counts->co_hello_count));
+}
+
+static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
+ struct lnet_comm_count *counts)
+{
+ msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
+ msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
+ msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
+ msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
+ msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
+}
+
+void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
+ struct lnet_element_stats *stats)
+{
+ struct lnet_comm_count *counts;
+
+ LASSERT(msg_stats);
+ LASSERT(stats);
+
+ counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
+ if (!counts)
+ return;
+ assign_stats(&msg_stats->im_send_stats, counts);
+
+ counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
+ if (!counts)
+ return;
+ assign_stats(&msg_stats->im_recv_stats, counts);
+
+ counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
+ if (!counts)
+ return;
+ assign_stats(&msg_stats->im_drop_stats, counts);
+}
+