+static int
+test_multi_alloc_statistics(void)
+{
+ int socket = 0;
+ struct rte_malloc_socket_stats pre_stats, post_stats ,first_stats, second_stats;
+ size_t size = 2048;
+ int align = 1024;
+#ifndef RTE_LIBRTE_MALLOC_DEBUG
+ int trailer_size = 0;
+#else
+ int trailer_size = 64;
+#endif
+ int overhead = 64 + trailer_size;
+
+ rte_malloc_get_socket_stats(socket, &pre_stats);
+
+ void *p1 = rte_malloc_socket("stats", size , align, socket);
+ if (!p1)
+ return -1;
+ rte_free(p1);
+ rte_malloc_dump_stats(stdout, "stats");
+
+ rte_malloc_get_socket_stats(socket,&post_stats);
+ /* Check statistics reported are correct */
+ /* All post stats should be equal to pre stats after alloc freed */
+ if ((post_stats.heap_totalsz_bytes != pre_stats.heap_totalsz_bytes) &&
+ (post_stats.heap_freesz_bytes!=pre_stats.heap_freesz_bytes) &&
+ (post_stats.heap_allocsz_bytes!=pre_stats.heap_allocsz_bytes)&&
+ (post_stats.alloc_count!=pre_stats.alloc_count)&&
+ (post_stats.free_count!=pre_stats.free_count)) {
+ printf("Malloc statistics are incorrect - freed alloc\n");
+ return -1;
+ }
+ /* Check two consecutive allocations */
+ size = 1024;
+ align = 0;
+ rte_malloc_get_socket_stats(socket,&pre_stats);
+ void *p2 = rte_malloc_socket("add", size ,align, socket);
+ if (!p2)
+ return -1;
+ rte_malloc_get_socket_stats(socket,&first_stats);
+
+ void *p3 = rte_malloc_socket("add2", size,align, socket);
+ if (!p3)
+ return -1;
+
+ rte_malloc_get_socket_stats(socket,&second_stats);
+
+ rte_free(p2);
+ rte_free(p3);
+
+ /* After freeing both allocations check stats return to original */
+ rte_malloc_get_socket_stats(socket, &post_stats);
+
+ /*
+ * Check that no new blocks added after small allocations
+ * i.e. < RTE_MALLOC_MEMZONE_SIZE
+ */
+ if(second_stats.heap_totalsz_bytes != first_stats.heap_totalsz_bytes) {
+ printf("Incorrect heap statistics: Total size \n");
+ return -1;
+ }
+ /* Check allocated size is equal to two additions plus overhead */
+ if(second_stats.heap_allocsz_bytes !=
+ size + overhead + first_stats.heap_allocsz_bytes) {
+ printf("Incorrect heap statistics: Allocated size \n");
+ return -1;
+ }
+ /* Check that allocation count increments correctly i.e. +1 */
+ if (second_stats.alloc_count != first_stats.alloc_count + 1) {
+ printf("Incorrect heap statistics: Allocated count \n");
+ return -1;
+ }
+
+ if (second_stats.free_count != first_stats.free_count){
+ printf("Incorrect heap statistics: Free count \n");
+ return -1;
+ }
+
+ /* Make sure that we didn't touch our greatest chunk: 2 * 11M) */
+ if (post_stats.greatest_free_size != pre_stats.greatest_free_size) {
+ printf("Incorrect heap statistics: Greatest free size \n");
+ return -1;
+ }
+ /* Free size must equal the original free size minus the new allocation*/
+ if (first_stats.heap_freesz_bytes <= second_stats.heap_freesz_bytes) {
+ printf("Incorrect heap statistics: Free size \n");
+ return -1;
+ }
+
+ if ((post_stats.heap_totalsz_bytes != pre_stats.heap_totalsz_bytes) &&
+ (post_stats.heap_freesz_bytes!=pre_stats.heap_freesz_bytes) &&
+ (post_stats.heap_allocsz_bytes!=pre_stats.heap_allocsz_bytes)&&
+ (post_stats.alloc_count!=pre_stats.alloc_count)&&
+ (post_stats.free_count!=pre_stats.free_count)) {
+ printf("Malloc statistics are incorrect - freed alloc\n");
+ return -1;
+ }
+ return 0;
+}
+