DPDK latencystats库使用方案
初始化
注意务必调用
rte_metrics_init
/* init latency stats */ /* @TODO should we remove this in product env? */ rte_metrics_init(numa); if (rte_latencystats_init(1, NULL)) { printf("Could not allocate latency data,\n"); }
update
/* 这个函数周期调用,比如1s调用一次,会更新latency数据并打印出之前的统计值 */
static void print_latency(void) { struct rte_metric_value * metrics; struct rte_metric_value * metrics; struct rte_metric_name * names; int len ; int ret; int i,j; len = 4; /* rte_metrics_get_names(NULL, 0); // 这里仅仅用来获取个数,为后续申请内存使用,若已固定知道则可以忽略。 if (len <= 0) { printf("Get metrics count: %d error\n", len); return; }*/ metrics = malloc(sizeof(struct rte_metric_value) * len); names = malloc(sizeof(struct rte_metric_name) * len); if (metrics == NULL || names == NULL) { printf("Cannot allocate memory\n"); free(metrics); free(names); return; } len = rte_metrics_get_names(names,len); if (len <= 0) { printf("Get metrics name: %d error\n", len); return; } for(j = 0; j < 2; j++) { ret = rte_metrics_get_values(j, metrics, len); if (ret < 0 || ret > len) { printf("Cannot get metrics values\n"); free(metrics); free(names); return; } PAL_LOG("Metrics for port %d:\n", j); for (i = 0; i < len; i++) printf(" %s: %"PRIu64"\n",names[metrics[i].key].name, metrics[i].value); } free(metrics); free(names); rte_latencystats_update(); }