From 4fa002d36ee9111bcc750e11aa2be611e4dedd76 Mon Sep 17 00:00:00 2001 From: Ioannis Ilkos Date: Thu, 23 Apr 2020 13:20:29 +0100 Subject: [PATCH] ANDROID: add ion_stat tracepoint to common kernel Emitted an event whenever ion buffers are created or freed. This enables tracking ion memory utilization changes, as well as individual buffer allocations. This was inspired by the pixel kernel patches by timmurray@ and carmenjackson@. Port of I4d4b23ae4dbda5012d3582f5564a87e0d08c68c7 Test: manual test on cuttlefish Bug: 154302786 Signed-off-by: Ioannis Ilkos Change-Id: I31a49d2c7778be307fdc1c9cc6847c44e026957f --- drivers/staging/android/ion/Makefile | 1 + drivers/staging/android/ion/ion_buffer.c | 20 ++++++++- drivers/staging/android/ion/ion_trace.h | 55 ++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 drivers/staging/android/ion/ion_trace.h diff --git a/drivers/staging/android/ion/Makefile b/drivers/staging/android/ion/Makefile index 1f7704e02fb0..7f8fd0f537b4 100644 --- a/drivers/staging/android/ion/Makefile +++ b/drivers/staging/android/ion/Makefile @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_ION) += ion.o ion_buffer.o ion_dma_buf.o ion_heap.o +CFLAGS_ion_buffer.o = -I$(src) obj-y += heaps/ diff --git a/drivers/staging/android/ion/ion_buffer.c b/drivers/staging/android/ion/ion_buffer.c index f01c564d23f3..cbbe3d5c9377 100644 --- a/drivers/staging/android/ion/ion_buffer.c +++ b/drivers/staging/android/ion/ion_buffer.c @@ -11,10 +11,26 @@ #include #include +#define CREATE_TRACE_POINTS +#include "ion_trace.h" #include "ion_private.h" static atomic_long_t total_heap_bytes; +static void track_buffer_created(struct ion_buffer *buffer) +{ + long total = atomic_long_add_return(buffer->size, &total_heap_bytes); + + trace_ion_stat(buffer->sg_table, buffer->size, total); +} + +static void track_buffer_destroyed(struct ion_buffer *buffer) +{ + long total = atomic_long_sub_return(buffer->size, &total_heap_bytes); + + trace_ion_stat(buffer->sg_table, -buffer->size, total); +} + /* this function should only be called while dev->lock is held */ static struct ion_buffer *ion_buffer_create(struct ion_heap *heap, struct ion_device *dev, @@ -67,7 +83,7 @@ static struct ion_buffer *ion_buffer_create(struct ion_heap *heap, INIT_LIST_HEAD(&buffer->attachments); mutex_init(&buffer->lock); - atomic_long_add(len, &total_heap_bytes); + track_buffer_created(buffer); return buffer; err1: @@ -218,7 +234,7 @@ int ion_buffer_destroy(struct ion_device *dev, struct ion_buffer *buffer) } heap = buffer->heap; - atomic_long_sub(buffer->size, &total_heap_bytes); + track_buffer_destroyed(buffer); if (heap->flags & ION_HEAP_FLAG_DEFER_FREE) ion_heap_freelist_add(heap, buffer); diff --git a/drivers/staging/android/ion/ion_trace.h b/drivers/staging/android/ion/ion_trace.h new file mode 100644 index 000000000000..8233691a73eb --- /dev/null +++ b/drivers/staging/android/ion/ion_trace.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * drivers/staging/android/ion/ion-trace.h + * + * Copyright (C) 2020 Google, Inc. + */ + +#undef TRACE_SYSTEM +#define TRACE_SYSTEM ion + +#if !defined(_ION_TRACE_H) || defined(TRACE_HEADER_MULTI_READ) +#define _ION_TRACE_H + +#include + +#ifndef __ION_PTR_TO_HASHVAL +static unsigned int __ion_ptr_to_hash(const void *ptr) +{ + unsigned long hashval; + + if (ptr_to_hashval(ptr, &hashval)) + return 0; + + /* The hashed value is only 32-bit */ + return (unsigned int)hashval; +} + +#define __ION_PTR_TO_HASHVAL +#endif + +TRACE_EVENT(ion_stat, + TP_PROTO(const void *addr, long len, + unsigned long total_allocated), + TP_ARGS(addr, len, total_allocated), + TP_STRUCT__entry(__field(unsigned int, buffer_id) + __field(long, len) + __field(unsigned long, total_allocated) + ), + TP_fast_assign(__entry->buffer_id = __ion_ptr_to_hash(addr); + __entry->len = len; + __entry->total_allocated = total_allocated; + ), + TP_printk("buffer_id=%u len=%ldB total_allocated=%ldB", + __entry->buffer_id, + __entry->len, + __entry->total_allocated) + ); + +#endif /* _ION_TRACE_H */ + +/* This part must be outside protection */ +#undef TRACE_INCLUDE_PATH +#define TRACE_INCLUDE_PATH . +#define TRACE_INCLUDE_FILE ion_trace +#include