Skip to contentSkip to conversation

binder: transaction latency tracking for user build

PATCH Superseded Frankie Chang 5 participants 89 messages Feb 5, 2020 – Nov 13, 2020 4 messages not in the archive Page 2 of 2

Subsystems (2)
lore
Share & tools
lore
#78 lore raw

Re: [PATCH v13 3/3] binder: add transaction latency tracer

In reply to a message not in the archive
Quoted 6 lines · Frankie Chang

From: "Frankie.Chang" <[email protected]>

Record start/end timestamp for binder transaction.
When transaction is completed or transaction is free, it would be checked if transaction latency over threshold (default 2 sec), if yes, printing related information for tracing.

I've applied the first 2 patches (finally!), but in looking at this some more, I have some questions:

Quoted 8 lines

/* Implement details */
- Add latency tracer module to monitor transaction by attaching to new tracepoints introduced when transactions are allocated and freed.
The trace_binder_txn_latency_free would not be enabled by default. Monitoring which transaction is too slow to cause some of exceptions is important. So we hook the tracepoint to call the monitor function.

This feels odd, but ok...

Quoted 8 lines

- Since some of modules would trigger timeout NE if their binder transaction don't finish in time, such as audio timeout (5 sec), even BT command timeout (2 sec), etc.
Therefore, setting the timeout threshold as default 2 seconds could be helpful to debug.
But this timeout threshold is configurable, to let all users determine the more suitable threshold.

You made this a module parameter, which while it works, is very unfriendly as it requires people to modify the boot command line or their module parameter configuration file. Also, you are doing this on as a "global binder" operation, for all instances, not on a per-instance basis.

Which is my larger question, you seem to want this tracer either on or off for ALL of the binder instances, which feels like overkill. Why not make this a per-instance thing, and allow it to be configured properly in the binder filesystem interface, to allow individual instances to enable/disable this.

This also prevents binder instances from seeing data outside of their instances, and leaking that to the kernel log, which brings me to:

Quoted 5 lines

- The reason why printing the related information to kernel information log but not trace buffer is that some abnormal transactions may be pending for a long time ago, they could not be recorded due to buffer limited.

Don't abuse the kernel information log for stuff that is just normal operations. What is wrong with using the trace buffers here? That's what they are designed for from what I can tell.

You don't want to "leak" this information to the world in the kernel log for stuff that should only go to a user/process that has the correct permissions to see it as specified by the tracing permissions, right?

Some implementation questions below:

Quoted 19 lines
---
 drivers/android/Kconfig                 |    8 +++
 drivers/android/Makefile                |    1 +
 drivers/android/binder.c                |    6 ++
 drivers/android/binder_internal.h       |   13 ++++
 drivers/android/binder_latency_tracer.c |  107 +++++++++++++++++++++++++++++++
 drivers/android/binder_trace.h          |   26 +++++++-
 6 files changed, 158 insertions(+), 3 deletions(-)
 create mode 100644 drivers/android/binder_latency_tracer.c
drivers/android/Kconfig +0 -0
index 53b22e2..8aadaf4 100644

drivers/android/Kconfig config ANDROID_BINDER_IPC_SELFTEST · line 63

Quoted 10 lines
drivers/android/Kconfig +7 -0
@@ -54,6 +54,14 @@ config ANDROID_BINDER_IPC_SELFTEST
 	  exhaustively with combinations of various buffer sizes and
 	  alignments.
 
+config BINDER_TRANSACTION_LATENCY_TRACKING
+	tristate "Android Binder transaction tracking"
+	help
+	  Used for track abnormal binder transaction which is over threshold,
+	  when the transaction is done or be free, this transaction would be
+	  checked whether it executed overtime.
+	  If yes, printing out the detailed info.

Why is this a separate module? Who will ever want this split out?

Quoted 34 lines
drivers/android/Kconfig +1 -0
+
 endif # if ANDROID
 
 endmenu
drivers/android/Makefile +1 -0
index c9d3d0c9..c2ffdb6 100644
@@ -4,3 +4,4 @@ ccflags-y += -I$(src)			# needed for trace events
 obj-$(CONFIG_ANDROID_BINDERFS)		+= binderfs.o
 obj-$(CONFIG_ANDROID_BINDER_IPC)	+= binder.o binder_alloc.o
 obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o
+obj-$(CONFIG_BINDER_TRANSACTION_LATENCY_TRACKING)	+= binder_latency_tracer.o
drivers/android/binder.c +2 -0
index 20b08f52..6384a37 100644
@@ -2651,6 +2651,7 @@ static void binder_transaction(struct binder_proc *proc,
 		return_error_line = __LINE__;
 		goto err_alloc_t_failed;
 	}
+	trace_binder_txn_latency_alloc(t);
 	INIT_LIST_HEAD(&t->fd_fixups);
 	binder_stats_created(BINDER_STAT_TRANSACTION);
 	spin_lock_init(&t->lock);
@@ -5151,6 +5152,7 @@ static void print_binder_transaction_ilocked(struct seq_file *m,
 		   to_proc ? to_proc->pid : 0,
 		   t->to_thread ? t->to_thread->pid : 0,
 		   t->code, t->flags, t->priority, t->need_reply);
+	trace_binder_txn_latency_info(m, t);
 	spin_unlock(&t->lock);
 
 	if (proc != to_proc) {

drivers/android/binder.c static int __init binder_init(void) · line 5799

Quoted 6 lines
drivers/android/binder.c +3 -0
@@ -5792,4 +5794,8 @@ static int __init binder_init(void)
 #define CREATE_TRACE_POINTS
 #include "binder_trace.h"
 
+EXPORT_TRACEPOINT_SYMBOL(binder_txn_latency_alloc);
+EXPORT_TRACEPOINT_SYMBOL(binder_txn_latency_info);
+EXPORT_TRACEPOINT_SYMBOL(binder_txn_latency_free);

EXPORT_TRACEPOINT_SYMBOL_GPL()?

Quoted 7 lines
drivers/android/binder.c +1 -0
+
 MODULE_LICENSE("GPL v2");
drivers/android/binder_internal.h +0 -0
index 6cd7901..f86911d 100644

drivers/android/binder_internal.h line 21

Quoted 7 lines
drivers/android/binder_internal.h +4 -0
@@ -15,6 +15,11 @@
 #include <uapi/linux/android/binderfs.h>
 #include "binder_alloc.h"
 
+#if IS_ENABLED(CONFIG_BINDER_TRANSACTION_LATENCY_TRACKING)
+#include <linux/rtc.h>
+#include <linux/time.h>
+#endif

Why the #if check in a .h file? Just include them always, right?

Quoted 5 lines
drivers/android/binder_internal.h +1 -0
+
 struct binder_context {
 	struct binder_node *binder_context_mgr_node;
 	struct mutex context_mgr_node_lock;

drivers/android/binder_internal.h struct binder_transaction { · line 539

Quoted 10 lines
drivers/android/binder_internal.h +7 -0
@@ -525,6 +530,14 @@ struct binder_transaction {
 	 * during thread teardown
 	 */
 	spinlock_t lock;
+	/**
+	 * @kt and @real_kt are used to record the time
+	 * that the binder transaction startup
+	 */
+#if IS_ENABLED(CONFIG_BINDER_TRANSACTION_LATENCY_TRACKING)
+	ktime_t kt;
+	ktime_t real_kt;

What does "kt" stand for here? You document that is it used to record time, but what is the difference between them?

Quoted 19 lines
drivers/android/binder_internal.h +1 -0
+#endif
 };
 
 /**
drivers/android/binder_latency_tracer.c new file +9 -0
new file mode 100644
index 0000000..07c53a0
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019 MediaTek Inc.
+ */
+
+#include <linux/module.h>
+#include <uapi/linux/android/binder.h>
+#include "binder_internal.h"
+#include "binder_trace.h"

drivers/android/binder_latency_tracer.c line 19

Quoted 10 lines
drivers/android/binder_latency_tracer.c new file +10 -0
+
+/*
+ * The reason setting the binder_txn_latency_threshold to 2 sec
+ * is that most of timeout abort is greater or equal to 2 sec.
+ * Making it configurable to let all users determine which
+ * threshold is more suitable.
+ */
+static uint32_t binder_txn_latency_threshold = 2;
+module_param_named(threshold, binder_txn_latency_threshold,
+			uint, 0644);

Again, this isn't the 1990's, please do not add module parameters if at all possible.

Quoted 4 lines
drivers/android/binder_latency_tracer.c new file +4 -0
+
+/*
+ * probe_binder_txn_latency_free - Output info of a delay transaction
+ * @t:          pointer to the over-time transaction

drivers/android/binder_latency_tracer.c line 33

Quoted 10 lines
drivers/android/binder_latency_tracer.c new file +10 -0
+ */
+static void probe_binder_txn_latency_free(void *ignore, struct binder_transaction *t,
+					int from_proc, int from_thread,
+					int to_proc, int to_thread)
+{
+	struct rtc_time tm;
+	ktime_t cur, startime, sub_t;
+
+	cur = ktime_get();
+	startime = t->kt;

"starttime"?

Quoted 13 lines
drivers/android/binder_latency_tracer.c new file +13 -0
+	sub_t = ktime_sub(cur, startime);
+
+	/* if transaction time is over than binder_txn_latency_threshold (sec),
+	 * show timeout warning log.
+	 */
+	if (ktime_divns(sub_t, NSEC_PER_SEC) < binder_txn_latency_threshold)
+		return;
+
+	tm = rtc_ktime_to_tm(t->real_kt);
+
+	pr_info_ratelimited("%d: from %d:%d to %d:%d",
+			t->debug_id, from_proc, from_thread,
+			to_proc, to_thread);

drivers/android/binder_latency_tracer.c line 56

Quoted 10 lines
drivers/android/binder_latency_tracer.c new file +10 -0
+
+	pr_info_ratelimited(" total %u.%03lu s code %u start %lu.%03lu android %d-%02d-%02d %02d:%02d:%02d.%03lu\n",
+			(unsigned int)ktime_divns(sub_t, NSEC_PER_SEC),
+			(unsigned long)(ktime_to_ms(sub_t) % MSEC_PER_SEC),
+			t->code,
+			(unsigned long)ktime_divns(startime, NSEC_PER_SEC),
+			(unsigned long)(ktime_to_ms(startime) % MSEC_PER_SEC),
+			(tm.tm_year + 1900), (tm.tm_mon + 1), tm.tm_mday,
+			tm.tm_hour, tm.tm_min, tm.tm_sec,
+			(unsigned long)(ktime_to_ms(t->real_kt) % MSEC_PER_SEC));

Again, this is a LOT of kernel log spam. Please just put this in the tracebuffer instead.

And why do this date math in the kernel? Isn't there some other standard way of expressing time to userspace that is easier to understand?

And do you really need the year? :)

Quoted 13 lines
drivers/android/binder_latency_tracer.c new file +13 -0
+}
+
+static void probe_binder_txn_latency_alloc(void *ignore,
+					struct binder_transaction *t)
+{
+	t->kt = ktime_get();
+	t->real_kt = ktime_get_real();
+	t->real_kt -= (sys_tz.tz_minuteswest * 60 * NSEC_PER_SEC);
+}
+
+static void probe_binder_txn_latency_info(void *ignore, struct seq_file *m,
+					struct binder_transaction *t)
+{

drivers/android/binder_latency_tracer.c line 79

Quoted 10 lines
drivers/android/binder_latency_tracer.c new file +10 -0
+	struct rtc_time tm;
+
+	tm = rtc_ktime_to_tm(t->real_kt);
+	seq_printf(m,
+		" start %lu.%06lu android %d-%02d-%02d %02d:%02d:%02d.%03lu",
+		(unsigned long)ktime_divns(t->kt, NSEC_PER_SEC),
+		(unsigned long)(ktime_to_us(t->kt) % USEC_PER_SEC),
+		(tm.tm_year + 1900), (tm.tm_mon + 1), tm.tm_mday,
+		 tm.tm_hour, tm.tm_min, tm.tm_sec,
+		(unsigned long)(ktime_to_ms(t->real_kt) % MSEC_PER_SEC));

Again, isn't there a better way to export time information to userspace where the kernel isn't doing the processing of the data itself?

Quoted 51 lines
drivers/android/binder_latency_tracer.c new file +28 -0
+}
+
+static int __init init_binder_latency_tracer(void)
+{
+	register_trace_binder_txn_latency_free(
+			probe_binder_txn_latency_free, NULL);
+	register_trace_binder_txn_latency_alloc(
+			probe_binder_txn_latency_alloc, NULL);
+	register_trace_binder_txn_latency_info(
+			probe_binder_txn_latency_info, NULL);
+
+	return 0;
+}
+
+static void exit_binder_latency_tracer(void)
+{
+	unregister_trace_binder_txn_latency_free(
+			probe_binder_txn_latency_free, NULL);
+	unregister_trace_binder_txn_latency_alloc(
+			probe_binder_txn_latency_alloc, NULL);
+	unregister_trace_binder_txn_latency_info(
+			probe_binder_txn_latency_info, NULL);
+}
+
+module_init(init_binder_latency_tracer);
+module_exit(exit_binder_latency_tracer);
+
+MODULE_LICENSE("GPL v2");
drivers/android/binder_trace.h +11 -0
index 8eeccdc..5494f36 100644
@@ -95,6 +95,17 @@
 		  __entry->thread_todo)
 );
 
+DECLARE_TRACE(binder_txn_latency_alloc,
+	TP_PROTO(struct binder_transaction *t),
+	TP_ARGS(t)
+);
+
+DECLARE_TRACE(binder_txn_latency_info,
+	TP_PROTO(struct seq_file *m,
+		 struct binder_transaction *t),
+	TP_ARGS(m, t)
+);
+
 TRACE_EVENT(binder_txn_latency_free,
 	TP_PROTO(struct binder_transaction *t,
 		 int from_proc, int from_thread,

drivers/android/binder_trace.h line 123

Quoted 5 lines
drivers/android/binder_trace.h +2 -0
@@ -108,6 +119,8 @@
 		__field(int, to_thread)
 		__field(unsigned int, code)
 		__field(unsigned int, flags)
+		__field(unsigned long, start_sec)
+		__field(unsigned long, start_msec)

Why track seconds and mseconds separately? Can't you just track one time value?

thanks,

greg k-h

#79 lore raw

PATCH v14 1/1 binder: add transaction latency tracer

Change from v14:
- prevent from directly print transaction info to kernel log.
- give up doing the date math in the kernel.
- track msec only instead of both sec and msec.
- change EXPORT_TRACEPOINT_SYMBOL to EXPORT_TRACEPOINT_SYMBOL_GPL.

Change from v13:
- modify patch subject.

Change from v12:
- rebase.

Change from v11:
- rebase.

Change from v10:
- replace timespec64 with ktime_t.
- fix build warning.

Change from v9:
- rename timestamp to ts in binder_internal.h for conciseness.
- change timeval to timespec64 in binder_internal.h

Change from v8:
- change rtc_time_to_tm to rtc_time64_to_tm.
- change timeval to __kernel_old_timeval due to

    https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c766d1472c70d25ad475cf56042af1652e792b23

- export tracepoint symbol for binder_txn_latency_* which binder_transaction_latency_tracer to be ko needed.

Change from v7:
- Use the passed-in values instead of accessing via t->from/to_proc/to_thread for trace_binder_txn_latency_free, when trace_binder_txn_latency_free_enable() return true.
- make a helper function to do the above.

Change from v6:
- change CONFIG_BINDER_TRANSACTION_LATENCY_TRACKING type from bool to tristate
- add comments to @timestamp and @tv under struct binder_transaction
- make binder_txn_latency threshold configurable
- enhance lock protection

Change from v5:
- change config name to the proper one, CONFIG_BINDER_TRANSACTION_LATENCY_TRACKING.
- change tracepoint name to more descriptive one, trace_binder_txn_latency_(alloc|info|free)
- enhance some lock protection.

Change from v4:
- split up into patch series.

Change from v3:
- use tracepoints for binder_update_info and print_binder_transaction_ext, instead of custom registration functions.

Change from v2:
- create transaction latency module to monitor slow transaction.

Change from v1:
- first patchset.

Frankie.Chang (3):
binder: move structs from core file to header file
binder: add trace at free transaction.
binder: add transaction latency tracer

 drivers/android/Kconfig                 |   8 +
 drivers/android/Makefile                |   1 +
 drivers/android/binder.c                | 430 ++----------------------
 drivers/android/binder_internal.h       | 414 +++++++++++++++++++++++
 drivers/android/binder_latency_tracer.c | 108 ++++++
 drivers/android/binder_trace.h          |  46 +++
 6 files changed, 601 insertions(+), 406 deletions(-)
 create mode 100644 drivers/android/binder_latency_tracer.c

A message that is not in the archive (sent off-list or not yet received): [email protected]

#81 lore raw

Re: [PATCH v14 1/3] binder: move structs from core file to header file

Quoted 6 lines · Frankie Chang

From: "Frankie.Chang" <[email protected]>

Moving all structs to header file makes module more extendable, and makes all these structs to be defined in the same file.

Quoted 6 lines
---
 drivers/android/binder.c          |  406 -------------------------------------
 drivers/android/binder_internal.h |  406 +++++++++++++++++++++++++++++++++++++
 2 files changed, 406 insertions(+), 406 deletions(-)

This, and patch 2/3 was already accepted, right?

I can't do anything with them again :)

thanks,

greg k-h

#82 lore raw

Re: [PATCH v14 1/3] binder: move structs from core file to header file

Quoted 14 lines · Greg Kroah-Hartman
Quoted 6 lines · Frankie Chang

From: "Frankie.Chang" <[email protected]>

Moving all structs to header file makes module more extendable, and makes all these structs to be defined in the same file.

Quoted 6 lines
---
 drivers/android/binder.c          |  406 -------------------------------------
 drivers/android/binder_internal.h |  406 +++++++++++++++++++++++++++++++++++++
 2 files changed, 406 insertions(+), 406 deletions(-)
Quoted 4 lines

This, and patch 2/3 was already accepted, right?

I can't do anything with them again :)

Oh, okay :)

many thanks
Frankie Chang

A message that is not in the archive (sent off-list or not yet received): 1605106986.11768.14.camel@mtkswgap22

#84 lore raw

Re: [PATCH v13 3/3] binder: add transaction latency tracer

Quoted 7 lines · Frankie Chang
Quoted 6 lines · Greg Kroah-Hartman
Quoted 5 lines

- The reason why printing the related information to kernel information log but not trace buffer is that some abnormal transactions may be pending for a long time ago, they could not be recorded due to buffer limited.

Quoted 6 lines
Quoted 4 lines

Don't abuse the kernel information log for stuff that is just normal operations. What is wrong with using the trace buffers here? That's what they are designed for from what I can tell.

As mentioned before, time limitation of recording is the reason why we don't just use trace here.

What limitation?

Quoted 1 line

In some long time stability test, such as MTBF,

What is "MTBF"?

Quoted 3 lines

the exception is caused by a series of transactions interaction. Some abnormal transactions may be pending for a long time ago, they could not be recorded due to buffer limited.

How long of a time is this? If they are pending, only when the timeout happens is the trace logged, right?

Again, please do not abuse the kernel log for this, that is not what it is for.

Quoted 10 lines
Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+config BINDER_TRANSACTION_LATENCY_TRACKING
+	tristate "Android Binder transaction tracking"
+	help
+	  Used for track abnormal binder transaction which is over threshold,
+	  when the transaction is done or be free, this transaction would be
+	  checked whether it executed overtime.
+	  If yes, printing out the detailed info.

Why is this a separate module? Who will ever want this split out?

Quoted 4 lines

The reason we split out a separate module is that we adopted the previously discussed recommendations in PATCH v1.

This way all of this tracing code is in-kernel but outside of binder.c.

Putting it in a single file is fine, but what does this benifit doing it in a separate file? Doesn't it waste more codespace this way?

Quoted 14 lines
Quoted 4 lines
Quoted 4 lines

+/*
+ * The reason setting the binder_txn_latency_threshold to 2 sec
+ * is that most of timeout abort is greater or equal to 2 sec.
+ * Making it configurable to let all users determine which

Quoted 9 lines

Review

Quoted 5 lines

+ * threshold is more suitable.
+ */

(quoted diff) +3 -0
+static uint32_t binder_txn_latency_threshold = 2;
+module_param_named(threshold, binder_txn_latency_threshold,
+			uint, 0644);

Again, this isn't the 1990's, please do not add module parameters if at all possible.

Quoted 3 lines

Is any recommended method here?
Because we refer to the method in binder.c, we don't know if this method is not suitable.

Look at the individual binder instances. That is what trace should be on/off for, not for all binder instances in the system at the same time.

thanks,

greg k-h

#85 lore raw

Re: [PATCH v13 3/3] binder: add transaction latency tracer

Quoted 12 lines · Greg Kroah-Hartman
Quoted 11 lines · Frankie Chang
Quoted 10 lines · Greg Kroah-Hartman
Quoted 5 lines

- The reason why printing the related information to kernel information log but not trace buffer is that some abnormal transactions may be pending for a long time ago, they could not be recorded due to buffer limited.

Don't abuse the kernel information log for stuff that is just normal operations. What is wrong with using the trace buffers here? That's what they are designed for from what I can tell.

Quoted 9 lines
Quoted 2 lines

As mentioned before, time limitation of recording is the reason why we don't just use trace here.

What limitation?

Quoted 1 line

In some long time stability test, such as MTBF,

What is "MTBF"?

Mean time between failures (MTBF) is the predicted elapsed time between inherent failures of a mechanical or electronic system, during normal system operation.

And we use MTBF script to run long time stress test to make sure our product stability is no problem.

Quoted 10 lines
Quoted 3 lines

the exception is caused by a series of transactions interaction. Some abnormal transactions may be pending for a long time ago, they could not be recorded due to buffer limited.

How long of a time is this? If they are pending, only when the timeout happens is the trace logged, right?

Again, please do not abuse the kernel log for this, that is not what it is for.

Hmm..Do you mean that make timeout log print to trace buffer but not kernel log?

The reason We don't do this is that we need to enable these trace events and enable trace everytimes before testing. But our testing script could lead to device reboot, and then it continue testing after reboot. The reboot would make these trace events disable, and we cannot get the timeout log which happen after reboot.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+config BINDER_TRANSACTION_LATENCY_TRACKING
+	tristate "Android Binder transaction tracking"
+	help
+	  Used for track abnormal binder transaction which is over threshold,
+	  when the transaction is done or be free, this transaction would be
+	  checked whether it executed overtime.
+	  If yes, printing out the detailed info.

Why is this a separate module? Who will ever want this split out?

Quoted 8 lines
Quoted 4 lines

The reason we split out a separate module is that we adopted the previously discussed recommendations in PATCH v1.

This way all of this tracing code is in-kernel but outside of binder.c.

Putting it in a single file is fine, but what does this benifit doing it in a separate file? Doesn't it waste more codespace this way?

Yeah, but I think separate file may be more manageable.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines

Review

Quoted 9 lines
(quoted diff) +9 -0
+/*
+ * The reason setting the binder_txn_latency_threshold to 2 sec
+ * is that most of timeout abort is greater or equal to 2 sec.
+ * Making it configurable to let all users determine which
+ * threshold is more suitable.
+ */
+static uint32_t binder_txn_latency_threshold = 2;
+module_param_named(threshold, binder_txn_latency_threshold,
+			uint, 0644);

Again, this isn't the 1990's, please do not add module parameters if at all possible.

Quoted 7 lines
Quoted 3 lines

Is any recommended method here?
Because we refer to the method in binder.c, we don't know if this method is not suitable.

Look at the individual binder instances. That is what trace should be on/off for, not for all binder instances in the system at the same time.

But our testing script is not for specific binder instances, it includes several testing and tests for several processes.

So the reason we trace for all binder instances is that we cannot predict which process would happen timeout transaction.

Many thanks
Frankie Chang

#86 lore raw

Re: [PATCH v13 3/3] binder: add transaction latency tracer

Quoted 20 lines · Frankie Chang
Quoted 19 lines · Greg Kroah-Hartman
Quoted 7 lines · Frankie Chang
Quoted 6 lines · Greg Kroah-Hartman
Quoted 5 lines

- The reason why printing the related information to kernel information log but not trace buffer is that some abnormal transactions may be pending for a long time ago, they could not be recorded due to buffer limited.

Quoted 6 lines
Quoted 4 lines

Don't abuse the kernel information log for stuff that is just normal operations. What is wrong with using the trace buffers here? That's what they are designed for from what I can tell.

As mentioned before, time limitation of recording is the reason why we don't just use trace here.

What limitation?

Quoted 1 line

In some long time stability test, such as MTBF,

Quoted 8 lines
Quoted 2 lines

What is "MTBF"?

Mean time between failures (MTBF) is the predicted elapsed time between inherent failures of a mechanical or electronic system, during normal system operation.

And we use MTBF script to run long time stress test to make sure our product stability is no problem.

Ok, great.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

the exception is caused by a series of transactions interaction. Some abnormal transactions may be pending for a long time ago, they could not be recorded due to buffer limited.

Quoted 8 lines
Quoted 6 lines

How long of a time is this? If they are pending, only when the timeout happens is the trace logged, right?

Again, please do not abuse the kernel log for this, that is not what it is for.

Hmm..Do you mean that make timeout log print to trace buffer but not kernel log?

Yes.

Quoted 5 lines

The reason We don't do this is that we need to enable these trace events and enable trace everytimes before testing. But our testing script could lead to device reboot, and then it continue testing after reboot. The reboot would make these trace events disable, and we cannot get the timeout log which happen after reboot.

When you reboot you enable tracing, that's not an issue, and then you will see the messages. Nothing is going to work across reboots.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+config BINDER_TRANSACTION_LATENCY_TRACKING
+	tristate "Android Binder transaction tracking"
+	help
+	  Used for track abnormal binder transaction which is over threshold,
+	  when the transaction is done or be free, this transaction would be
+	  checked whether it executed overtime.
+	  If yes, printing out the detailed info.

Why is this a separate module? Who will ever want this split out?

The reason we split out a separate module is that we adopted the previously discussed recommendations in PATCH v1.

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

This way all of this tracing code is in-kernel but outside of binder.c.

Putting it in a single file is fine, but what does this benifit doing it in a separate file? Doesn't it waste more codespace this way?

Yeah, but I think separate file may be more manageable.

Sorry, I mean, "why put this in a separate kernel module", not file. File is fine.

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 4 lines
Quoted 4 lines

+/*
+ * The reason setting the binder_txn_latency_threshold to 2 sec
+ * is that most of timeout abort is greater or equal to 2 sec.
+ * Making it configurable to let all users determine which

Quoted 9 lines

Review

Quoted 5 lines

+ * threshold is more suitable.
+ */

(quoted diff) +3 -0
+static uint32_t binder_txn_latency_threshold = 2;
+module_param_named(threshold, binder_txn_latency_threshold,
+			uint, 0644);

Again, this isn't the 1990's, please do not add module parameters if at all possible.

Is any recommended method here?
Because we refer to the method in binder.c, we don't know if this method is not suitable.

Quoted 5 lines
Quoted 3 lines

Look at the individual binder instances. That is what trace should be on/off for, not for all binder instances in the system at the same time.

But our testing script is not for specific binder instances, it includes several testing and tests for several processes.

Then turn it on for all binder instances.

Quoted 2 lines

So the reason we trace for all binder instances is that we cannot predict which process would happen timeout transaction.

That's fine, but provide the ability to do this on a per-instance, as that is what binder now supports. To make it "global" is a big regression from the way it was changed recently to support different instances.

thanks,

greg k-h

#87 lore raw

Re: [PATCH v13] binder: add transaction latency tracer

In reply to Frankie Chang
Quoted 6 lines · Frankie Chang

Frankie.Chang (3):
binder: move structs from core file to header file
binder: add trace at free transaction.
binder: add transaction latency tracer

Quoted 8 lines
 drivers/android/Kconfig                 |   8 +
 drivers/android/Makefile                |   1 +
 drivers/android/binder.c                | 430 ++----------------------
 drivers/android/binder_internal.h       | 419 +++++++++++++++++++++++
 drivers/android/binder_latency_tracer.c | 107 ++++++
 drivers/android/binder_trace.h          |  49 +++
 6 files changed, 608 insertions(+), 406 deletions(-)
 create mode 100644 drivers/android/binder_latency_tracer.c

Your cover letter here lost all of the information that used to be in other versions of the series :(

#88 lore raw
In reply to Frankie Chang
Quoted 3 lines · Frankie Chang

Change from v9:
- rename timestamp to ts in binder_internal.h for conciseness.
- change 'struct timeval' to 'struct timespec64' in binder_internal.h.

These just blew up in the 0-day testing, again, so I'm dropping them from my tree.

Can you please fix them up and resend when you have them properly building?

thanks,

greg k-h

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

#89 lore raw
In reply to Frankie Chang
Quoted 4 lines · Frankie Chang

Change from v8:
- change rtc_time_to_tm to rtc_time64_to_tm.
- change timeval to __kernel_old_timeval due to

    https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c766d1472c70d25ad475cf56042af1652e792b23

That feels wrong, why can't you use the "new" structure instead?

thanks,

greg k-h

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

#90 lore raw

PATCH v6 3/3 binder: add transaction latency tracer

In reply to Frankie Chang

From: "Frankie.Chang" <[email protected]>

Record start/end timestamp for binder transaction.
When transaction is completed or transaction is free, it would be checked if transaction latency over threshold (default 2 sec), if yes, printing related information for tracing.

/* Implement details */
- Add latency tracer module to monitor transaction by attaching to new tracepoints introduced when transactions are allocated and freed.
The trace_binder_txn_latency_free would not be enabled by default. Monitoring which transaction is too slow to cause some of exceptions is important. So we hook the tracepoint to call the monitor function.

- Since some of modules would trigger timeout NE if their binder transaction don't finish in time, such as audio timeout (5 sec), even BT command timeout (2 sec), etc.
Therefore, setting the timeout threshold as default 2 seconds could be helpful to debug.
But this timeout threshold is configurable, to let all users determine the more suitable threshold.

- The reason why printing the related information to kernel information log but not trace buffer is that some abnormal transactions may be pending for a long time ago, they could not be recorded due to buffer limited.

---
 drivers/android/Kconfig                 |    8 +++
 drivers/android/Makefile                |    1 +
 drivers/android/binder.c                |    2 +
 drivers/android/binder_internal.h       |   13 ++++
 drivers/android/binder_latency_tracer.c |  112 +++++++++++++++++++++++++++++++
 drivers/android/binder_trace.h          |   26 ++++++-
 6 files changed, 159 insertions(+), 3 deletions(-)
 create mode 100644 drivers/android/binder_latency_tracer.c
drivers/android/Kconfig +8 -0
index 6fdf2ab..91fff1e 100644
@@ -54,6 +54,14 @@ config ANDROID_BINDER_IPC_SELFTEST
 	  exhaustively with combinations of various buffer sizes and
 	  alignments.
 
+config BINDER_TRANSACTION_LATENCY_TRACKING
+	tristate "Android Binder transaction tracking"
+	help
+	  Used for track abnormal binder transaction which is over threshold,
+	  when the transaction is done or be free, this transaction would be
+	  checked whether it executed overtime.
+	  If yes, printing out the detailed info.
+
 endif # if ANDROID
 
 endmenu
drivers/android/Makefile +1 -0
index c9d3d0c9..c2ffdb6 100644
@@ -4,3 +4,4 @@ ccflags-y += -I$(src)			# needed for trace events
 obj-$(CONFIG_ANDROID_BINDERFS)		+= binderfs.o
 obj-$(CONFIG_ANDROID_BINDER_IPC)	+= binder.o binder_alloc.o
 obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o
+obj-$(CONFIG_BINDER_TRANSACTION_LATENCY_TRACKING)	+= binder_latency_tracer.o
drivers/android/binder.c +2 -0
index 1e6fc40..0956403 100644
@@ -2658,6 +2658,7 @@ static void binder_transaction(struct binder_proc *proc,
 		return_error_line = __LINE__;
 		goto err_alloc_t_failed;
 	}
+	trace_binder_txn_latency_alloc(t);
 	INIT_LIST_HEAD(&t->fd_fixups);
 	binder_stats_created(BINDER_STAT_TRANSACTION);
 	spin_lock_init(&t->lock);
@@ -5147,6 +5148,7 @@ static void print_binder_transaction_ilocked(struct seq_file *m,
 		   to_proc ? to_proc->pid : 0,
 		   t->to_thread ? t->to_thread->pid : 0,
 		   t->code, t->flags, t->priority, t->need_reply);
+	trace_binder_txn_latency_info(m, t);
 	spin_unlock(&t->lock);
 
 	if (proc != to_proc) {
drivers/android/binder_internal.h +13 -0
index 5b65413..596db00 100644
@@ -13,6 +13,11 @@
 #include <linux/uidgid.h>
 #include "binder_alloc.h"
 
+#if IS_ENABLED(CONFIG_BINDER_TRANSACTION_LATENCY_TRACKING)
+#include <linux/rtc.h>
+#include <linux/time.h>
+#endif
+
 struct binder_context {
 	struct binder_node *binder_context_mgr_node;
 	struct mutex context_mgr_node_lock;
@@ -521,6 +526,14 @@ struct binder_transaction {
 	 * during thread teardown
 	 */
 	spinlock_t lock;
+	/**
+	 * @timestamp and @tv are used to record the time
+	 * that the binder transaction startup
+	 */
+#if IS_ENABLED(CONFIG_BINDER_TRANSACTION_LATENCY_TRACKING)
+	struct timespec64 timestamp;
+	struct timeval tv;
+#endif
 };
 
 /**
drivers/android/binder_latency_tracer.c new file +112 -0

112 lines not on this page · raw message

drivers/android/binder_trace.h +23 -3
index 8ac87d1..405d493 100644
@@ -95,6 +95,17 @@
 		  __entry->thread_todo)
 );
 
+DECLARE_TRACE(binder_txn_latency_alloc,
+	TP_PROTO(struct binder_transaction *t),
+	TP_ARGS(t, e)
+);
+
+DECLARE_TRACE(binder_txn_latency_info,
+	TP_PROTO(struct seq_file *m,
+		 struct binder_transaction *t),
+	TP_ARGS(m, t)
+);
+
 TRACE_EVENT(binder_txn_latency_free,
 	TP_PROTO(struct binder_transaction *t),
 	TP_ARGS(t),
@@ -106,6 +117,8 @@
 		__field(int, to_thread)
 		__field(unsigned int, code)
 		__field(unsigned int, flags)
+		__field(unsigned long, start_sec)
+		__field(unsigned long, start_nsec)
 	),
 	TP_fast_assign(
 		__entry->debug_id = t->debug_id;
@@ -115,11 +128,18 @@
 		__entry->to_thread = t->to_thread ? t->to_thread->pid : 0;
 		__entry->code = t->code;
 		__entry->flags = t->flags;
-	),
-	TP_printk("transaction=%d from %d:%d to %d:%d flags=0x%x code=0x%x",
+#if IS_ENABLED(CONFIG_BINDER_TRANSACTION_LATENCY_TRACKING)
+		__entry->start_sec = t->timestamp.tv_sec;
+		__entry->start_nsec = t->timestamp.tv_nsec / NSEC_PER_MSEC;
+#else
+		__entry->start_sec = 0;
+		__entry->start_nsec = 0;
+#endif
+	),
+	TP_printk("transaction=%d from %d:%d to %d:%d flags=0x%x code=0x%x start %lu.%03ld",
 		  __entry->debug_id, __entry->from_proc, __entry->from_thread,
 		  __entry->to_proc, __entry->to_thread, __entry->code,
-		  __entry->flags)
+		  __entry->flags, __entry->start_sec, __entry->start_nsec)
 );
 
 TRACE_EVENT(binder_transaction,
Signature · 1 line
1.7.9.5
#91 lore raw

Re: [PATCH v4 3/3] binder: add transaction latency tracer

In reply to Frankie Chang
Quoted 6 lines · Frankie Chang

From: "Frankie.Chang" <[email protected]>

Record start/end timestamp for binder transaction.
When transaction is completed or transaction is free, it would be checked if transaction latency over threshold (2 sec),

If this is a hard-coded threshold, provide rationale for why 2 sec is the right value and it doesn't need to be tunable

Quoted 8 lines

if yes, printing related information for tracing.

/* Implement details */
- Add latency tracer module to monitor slow transaction. The trace_binder_free_transaction would not be enabled by default. Monitoring which transaction is too slow to cause some of exceptions is important. So we hook the tracepoint to call the monitor function.

Please add a more complete description. This patch adds a module to monitor transaction latency by attaching to new tracepoints introduced when transactions are allocated and freed. Describe this in the commit message.

Quoted 30 lines
---
 drivers/android/Kconfig                 |    8 +++
 drivers/android/Makefile                |    1 +
 drivers/android/binder.c                |    2 +
 drivers/android/binder_internal.h       |   13 ++++
 drivers/android/binder_latency_tracer.c |  105 +++++++++++++++++++++++++++++++
 drivers/android/binder_trace.h          |   26 +++++++-
 6 files changed, 152 insertions(+), 3 deletions(-)
 create mode 100644 drivers/android/binder_latency_tracer.c

Change from v4:
split up into patch series.

Change from v3:
use tracepoints for binder_update_info and print_binder_transaction_ext, instead of custom registration functions.

Change from v2:
create transaction latency module to monitor slow transaction.

Change from v1:
first patchset.

drivers/android/Kconfig +0 -0
index 6fdf2ab..7ba80eb 100644

drivers/android/Kconfig config ANDROID_BINDER_IPC_SELFTEST · line 57

Quoted 4 lines
drivers/android/Kconfig +1 -0
@@ -54,6 +54,14 @@ config ANDROID_BINDER_IPC_SELFTEST
          exhaustively with combinations of various buffer sizes and
          alignments.
 
+config BINDER_USER_TRACKING

Why not "BINDER_TRANSACTION_LATENCY_TRACKING"?

drivers/android/Kconfig config ANDROID_BINDER_IPC_SELFTEST · line 63

Quoted 6 lines
drivers/android/Kconfig +6 -0
+       bool "Android Binder transaction tracking"
+       help
+         Used for track abnormal binder transaction which is over 2 seconds,
+         when the transaction is done or be free, this transaction would be
+         checked whether it executed overtime.
+         If yes, printing out the detail info about it.

"If yes, print out the detailed info"

Quoted 18 lines
drivers/android/Kconfig +1 -0
+
 endif # if ANDROID
 
 endmenu
drivers/android/Makefile +1 -0
index c9d3d0c9..552e8ac 100644
@@ -4,3 +4,4 @@ ccflags-y += -I$(src)                   # needed for trace events
 obj-$(CONFIG_ANDROID_BINDERFS)         += binderfs.o
 obj-$(CONFIG_ANDROID_BINDER_IPC)       += binder.o binder_alloc.o
 obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o
+obj-$(CONFIG_BINDER_USER_TRACKING)     += binder_latency_tracer.o
drivers/android/binder.c +0 -0
index 4c3dd98..b89d75a 100644

drivers/android/binder.c static void binder_transaction(struct binder_proc *proc, · line 2660

Quoted 4 lines
drivers/android/binder.c +1 -0
@@ -2657,6 +2657,7 @@ static void binder_transaction(struct binder_proc *proc,
                return_error_line = __LINE__;
                goto err_alloc_t_failed;
        }
+       trace_binder_update_info(t, e);

Can this be a more descriptive name? Perhaps "trace_binder_txn_create()"

drivers/android/binder.c static void print_binder_transaction_ilocked(struct seq_file *m, · line 5149

Quoted 8 lines
drivers/android/binder.c +1 -0
        INIT_LIST_HEAD(&t->fd_fixups);
        binder_stats_created(BINDER_STAT_TRANSACTION);
        spin_lock_init(&t->lock);
@@ -5145,6 +5146,7 @@ static void print_binder_transaction_ilocked(struct seq_file *m,
                   t->to_thread ? t->to_thread->pid : 0,
                   t->code, t->flags, t->priority, t->need_reply);
        spin_unlock(&t->lock);
+       trace_print_binder_transaction_ext(m, t);

Why do you need to trace when dumping out the transaction info?

Quoted 124 lines
drivers/android/binder.c +0 -0
        if (proc != to_proc) {
                /*
drivers/android/binder_internal.h +13 -0
index ed61b3e..24d7beb 100644
@@ -12,6 +12,11 @@
 #include <linux/types.h>
 #include <linux/uidgid.h>
 
+#ifdef CONFIG_BINDER_USER_TRACKING
+#include <linux/rtc.h>
+#include <linux/time.h>
+#endif
+
 struct binder_context {
        struct binder_node *binder_context_mgr_node;
        struct mutex context_mgr_node_lock;
@@ -131,6 +136,10 @@ struct binder_transaction_log_entry {
        uint32_t return_error;
        uint32_t return_error_param;
        char context_name[BINDERFS_MAX_NAME + 1];
+#ifdef CONFIG_BINDER_USER_TRACKING
+       struct timespec timestamp;
+       struct timeval tv;
+#endif
 };
 
 struct binder_transaction_log {
@@ -520,6 +529,10 @@ struct binder_transaction {
         * during thread teardown
         */
        spinlock_t lock;
+#ifdef CONFIG_BINDER_USER_TRACKING
+       struct timespec timestamp;
+       struct timeval tv;
+#endif
 };
 
 /**
drivers/android/binder_latency_tracer.c new file +77 -0
new file mode 100644
index 0000000..45c14fb
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019 MediaTek Inc.
+ */
+
+#include <linux/module.h>
+#include <uapi/linux/android/binder.h>
+#include "binder_alloc.h"
+#include "binder_internal.h"
+#include "binder_trace.h"
+
+/*
+ * probe_binder_free_transaction - Output info of a delay transaction
+ * @t:          pointer to the over-time transaction
+ */
+void probe_binder_free_transaction(void *ignore, struct binder_transaction *t)
+{
+       struct rtc_time tm;
+       struct timespec *startime;
+       struct timespec cur, sub_t;
+
+       ktime_get_ts(&cur);
+       startime = &t->timestamp;
+       sub_t = timespec_sub(cur, *startime);
+
+       /* if transaction time is over than 2 sec,
+        * show timeout warning log.
+        */
+       if (sub_t.tv_sec < 2)
+               return;
+
+       rtc_time_to_tm(t->tv.tv_sec, &tm);
+
+       spin_lock(&t->lock);
+       pr_info_ratelimited("%d: from %d:%d to %d:%d",
+                       t->debug_id,
+                       t->from ? t->from->proc->pid : 0,
+                       t->from ? t->from->pid : 0,
+                       t->to_proc ? t->to_proc->pid : 0,
+                       t->to_thread ? t->to_thread->pid : 0);
+       spin_unlock(&t->lock);
+
+       pr_info_ratelimited(" total %u.%03ld s code %u start %lu.%03ld android %d-%02d-%02d %02d:%02d:%02d.%03lu\n",
+                       (unsigned int)sub_t.tv_sec,
+                       (sub_t.tv_nsec / NSEC_PER_MSEC),
+                       t->code,
+                       (unsigned long)startime->tv_sec,
+                       (startime->tv_nsec / NSEC_PER_MSEC),
+                       (tm.tm_year + 1900), (tm.tm_mon + 1), tm.tm_mday,
+                       tm.tm_hour, tm.tm_min, tm.tm_sec,
+                       (unsigned long)(t->tv.tv_usec / USEC_PER_MSEC));
+}
+
+static void probe_binder_update_info(void *ignore, struct binder_transaction *t,
+                          struct binder_transaction_log_entry *e)
+{
+       ktime_get_ts(&e->timestamp);
+       do_gettimeofday(&e->tv);
+       e->tv.tv_sec -= (sys_tz.tz_minuteswest * 60);
+       memcpy(&t->timestamp, &e->timestamp, sizeof(struct timespec));
+       memcpy(&t->tv, &e->tv, sizeof(struct timeval));
+}
+
+static void probe_print_binder_transaction_ext(void *ignore, struct seq_file *m,
+                                        struct binder_transaction *t)
+{
+       struct rtc_time tm;
+
+       rtc_time_to_tm(t->tv.tv_sec, &tm);
+       seq_printf(m,
+                  " start %lu.%06lu android %d-%02d-%02d %02d:%02d:%02d.%03lu",
+                  (unsigned long)t->timestamp.tv_sec,
+                  (t->timestamp.tv_nsec / NSEC_PER_USEC),
+                  (tm.tm_year + 1900), (tm.tm_mon + 1), tm.tm_mday,
+                  tm.tm_hour, tm.tm_min, tm.tm_sec,
+                  (unsigned long)(t->tv.tv_usec / USEC_PER_MSEC));
+

drivers/android/binder_latency_tracer.c line 87

Quoted 10 lines
drivers/android/binder_latency_tracer.c new file +10 -0
+}
+
+static int __init init_binder_latency_tracer(void)
+{
+       register_trace_binder_free_transaction(
+                       probe_binder_free_transaction, NULL);
+       register_trace_binder_update_info(
+                       probe_binder_update_info, NULL);
+       register_trace_print_binder_transaction_ext(
+                       probe_print_binder_transaction_ext, NULL);

Ah, now the trace in the print path makes sense. Please add a more detailed description to the commit message. Also add a comment at the trace point that it is for modules to attach to so additional information can be printed. Also, make the names of the tracepoints more descriptive of what they really are ...something like trace_binder_txn_latency_(alloc|info|free)

Quoted 73 lines
drivers/android/binder_latency_tracer.c new file +18 -0
+
+       return 0;
+}
+
+static void exit_binder_latency_tracer(void)
+{
+       unregister_trace_binder_free_transaction(
+                       probe_binder_free_transaction, NULL);
+       unregister_trace_binder_update_info(
+                       probe_binder_update_info, NULL);
+       unregister_trace_print_binder_transaction_ext(
+                       probe_print_binder_transaction_ext, NULL);
+}
+
+module_init(init_binder_latency_tracer);
+module_exit(exit_binder_latency_tracer);
+
+MODULE_LICENSE("GPL v2");
drivers/android/binder_trace.h +23 -4
index 7acc18d..466993e 100644
@@ -18,6 +18,7 @@
 struct binder_ref_data;
 struct binder_thread;
 struct binder_transaction;
+struct binder_transaction_log_entry;
 
 TRACE_EVENT(binder_ioctl,
        TP_PROTO(unsigned int cmd, unsigned long arg),
@@ -95,6 +96,18 @@
                  __entry->thread_todo)
 );
 
+DECLARE_TRACE(binder_update_info,
+       TP_PROTO(struct binder_transaction *t,
+                struct binder_transaction_log_entry *e),
+       TP_ARGS(t, e)
+);
+
+DECLARE_TRACE(print_binder_transaction_ext,
+       TP_PROTO(struct seq_file *m,
+                struct binder_transaction *t),
+       TP_ARGS(m, t)
+);
+
 TRACE_EVENT(binder_free_transaction,
        TP_PROTO(struct binder_transaction *t),
        TP_ARGS(t),
@@ -115,11 +128,18 @@
                __entry->to_thread = t->to_thread ? t->to_thread->pid : 0;
                __entry->code = t->code;
                __entry->flags = t->flags;
-       ),
-       TP_printk("transaction=%d from %d:%d to %d:%d flags=0x%x code=0x%x",
+#ifdef CONFIG_BINDER_USER_TRACKING
+               __entry->start_sec = t->timestamp.tv_sec;
+               __entry->start_nsec = t->timestamp.tv_nsec / NSEC_PER_MSEC;
+#else
+               __entry->start_sec = 0;
+               __entry->start_nsec = 0;
+#endif
+       ),
+       TP_printk("transaction=%d from %d:%d to %d:%d flags=0x%x code=0x%x start %lu.%03ld",
                  __entry->debug_id, __entry->from_proc, __entry->from_thread,
                  __entry->to_proc, __entry->to_thread, __entry->code,
-                 __entry->flags)
+                 __entry->flags, __entry->start_sec, __entry->start_nsec)
 );
 
 TRACE_EVENT(binder_transaction,
--

1.7.9.5

#92 lore raw

Re: [PATCH v4 3/3] binder: add transaction latency tracer

Quoted 11 lines · Todd Kjos
Quoted 6 lines · Frankie Chang

From: "Frankie.Chang" <[email protected]>

Record start/end timestamp for binder transaction.
When transaction is completed or transaction is free, it would be checked if transaction latency over threshold (2 sec),

If this is a hard-coded threshold, provide rationale for why 2 sec is the right value and it doesn't need to be tunable

Some of modules would trigger timeout NE if their binder transaction don't finish in time, such as audio timeout (5 sec), even BT command timeout (2 sec), etc.

Therefore, we want to record related transactions which exceed 2 sec. It could be helpful to debug.

Quoted 9 lines
Quoted 8 lines

if yes, printing related information for tracing.

/* Implement details */
- Add latency tracer module to monitor slow transaction. The trace_binder_free_transaction would not be enabled by default. Monitoring which transaction is too slow to cause some of exceptions is important. So we hook the tracepoint to call the monitor function.

Quoted 5 lines

Please add a more complete description. This patch adds a module to monitor transaction latency by attaching to new tracepoints introduced when transactions are allocated and freed. Describe this in the commit message.

Okay, we would add these description in next version.

Quoted 33 lines
Quoted 33 lines
---
 drivers/android/Kconfig                 |    8 +++
 drivers/android/Makefile                |    1 +
 drivers/android/binder.c                |    2 +
 drivers/android/binder_internal.h       |   13 ++++
 drivers/android/binder_latency_tracer.c |  105 +++++++++++++++++++++++++++++++
 drivers/android/binder_trace.h          |   26 +++++++-
 6 files changed, 152 insertions(+), 3 deletions(-)
 create mode 100644 drivers/android/binder_latency_tracer.c

Change from v4:
split up into patch series.

Change from v3:
use tracepoints for binder_update_info and print_binder_transaction_ext, instead of custom registration functions.

Change from v2:
create transaction latency module to monitor slow transaction.

Change from v1:
first patchset.

drivers/android/Kconfig +0 -0
index 6fdf2ab..7ba80eb 100644
@@ -54,6 +54,14 @@ config ANDROID_BINDER_IPC_SELFTEST
          exhaustively with combinations of various buffer sizes and
          alignments.
 
Quoted 4 lines

drivers/android/Kconfig config ANDROID_BINDER_IPC_SELFTEST · line 57

Quoted 1 line
drivers/android/Kconfig +1 -0
+config BINDER_USER_TRACKING

Why not "BINDER_TRANSACTION_LATENCY_TRACKING"?

Thanks for your advice. We would modify the config name to this proper one.

Quoted 9 lines

drivers/android/Kconfig config ANDROID_BINDER_IPC_SELFTEST · line 63

Quoted 6 lines
drivers/android/Kconfig +6 -0
+       bool "Android Binder transaction tracking"
+       help
+         Used for track abnormal binder transaction which is over 2 seconds,
+         when the transaction is done or be free, this transaction would be
+         checked whether it executed overtime.
+         If yes, printing out the detail info about it.

"If yes, print out the detailed info"

We would modify it, thanks.

Quoted 16 lines
Quoted 16 lines
drivers/android/Kconfig +1 -0
+
 endif # if ANDROID
 
 endmenu
drivers/android/Makefile +1 -0
index c9d3d0c9..552e8ac 100644
@@ -4,3 +4,4 @@ ccflags-y += -I$(src)                   # needed for trace events
 obj-$(CONFIG_ANDROID_BINDERFS)         += binderfs.o
 obj-$(CONFIG_ANDROID_BINDER_IPC)       += binder.o binder_alloc.o
 obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o
+obj-$(CONFIG_BINDER_USER_TRACKING)     += binder_latency_tracer.o
drivers/android/binder.c +0 -1
index 4c3dd98..b89d75a 100644
--- a/drivers/android/binder.c
Quoted 9 lines

drivers/android/binder.c static void binder_transaction(struct binder_proc *proc, · line 2660

Quoted 6 lines

+++ b/drivers/android/binder.c

drivers/android/binder.c +1 -0
@@ -2657,6 +2657,7 @@ static void binder_transaction(struct binder_proc *proc,
                return_error_line = __LINE__;
                goto err_alloc_t_failed;
        }
+       trace_binder_update_info(t, e);

Can this be a more descriptive name? Perhaps "trace_binder_txn_create()"

Okay, we would replace the original name with
trace_binder_txn_latency_alloc.

Quoted 11 lines

drivers/android/binder.c static void print_binder_transaction_ilocked(struct seq_file *m, · line 5149

Quoted 8 lines
drivers/android/binder.c +1 -0
        INIT_LIST_HEAD(&t->fd_fixups);
        binder_stats_created(BINDER_STAT_TRANSACTION);
        spin_lock_init(&t->lock);
@@ -5145,6 +5146,7 @@ static void print_binder_transaction_ilocked(struct seq_file *m,
                   t->to_thread ? t->to_thread->pid : 0,
                   t->code, t->flags, t->priority, t->need_reply);
        spin_unlock(&t->lock);
+       trace_print_binder_transaction_ext(m, t);

Why do you need to trace when dumping out the transaction info?

Because we want to get the start time about this transaction, to check whether these transactions consume too much time.

Converting kernel time to android time let us easily correspond kernel log to android log. But it is not necessary if you think that is too redundant.

Quoted 133 lines
Quoted 133 lines
drivers/android/binder.c +0 -0
        if (proc != to_proc) {
                /*
drivers/android/binder_internal.h +13 -0
index ed61b3e..24d7beb 100644
@@ -12,6 +12,11 @@
 #include <linux/types.h>
 #include <linux/uidgid.h>
 
+#ifdef CONFIG_BINDER_USER_TRACKING
+#include <linux/rtc.h>
+#include <linux/time.h>
+#endif
+
 struct binder_context {
        struct binder_node *binder_context_mgr_node;
        struct mutex context_mgr_node_lock;
@@ -131,6 +136,10 @@ struct binder_transaction_log_entry {
        uint32_t return_error;
        uint32_t return_error_param;
        char context_name[BINDERFS_MAX_NAME + 1];
+#ifdef CONFIG_BINDER_USER_TRACKING
+       struct timespec timestamp;
+       struct timeval tv;
+#endif
 };
 
 struct binder_transaction_log {
@@ -520,6 +529,10 @@ struct binder_transaction {
         * during thread teardown
         */
        spinlock_t lock;
+#ifdef CONFIG_BINDER_USER_TRACKING
+       struct timespec timestamp;
+       struct timeval tv;
+#endif
 };
 
 /**
drivers/android/binder_latency_tracer.c new file +86 -0
new file mode 100644
index 0000000..45c14fb
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019 MediaTek Inc.
+ */
+
+#include <linux/module.h>
+#include <uapi/linux/android/binder.h>
+#include "binder_alloc.h"
+#include "binder_internal.h"
+#include "binder_trace.h"
+
+/*
+ * probe_binder_free_transaction - Output info of a delay transaction
+ * @t:          pointer to the over-time transaction
+ */
+void probe_binder_free_transaction(void *ignore, struct binder_transaction *t)
+{
+       struct rtc_time tm;
+       struct timespec *startime;
+       struct timespec cur, sub_t;
+
+       ktime_get_ts(&cur);
+       startime = &t->timestamp;
+       sub_t = timespec_sub(cur, *startime);
+
+       /* if transaction time is over than 2 sec,
+        * show timeout warning log.
+        */
+       if (sub_t.tv_sec < 2)
+               return;
+
+       rtc_time_to_tm(t->tv.tv_sec, &tm);
+
+       spin_lock(&t->lock);
+       pr_info_ratelimited("%d: from %d:%d to %d:%d",
+                       t->debug_id,
+                       t->from ? t->from->proc->pid : 0,
+                       t->from ? t->from->pid : 0,
+                       t->to_proc ? t->to_proc->pid : 0,
+                       t->to_thread ? t->to_thread->pid : 0);
+       spin_unlock(&t->lock);
+
+       pr_info_ratelimited(" total %u.%03ld s code %u start %lu.%03ld android %d-%02d-%02d %02d:%02d:%02d.%03lu\n",
+                       (unsigned int)sub_t.tv_sec,
+                       (sub_t.tv_nsec / NSEC_PER_MSEC),
+                       t->code,
+                       (unsigned long)startime->tv_sec,
+                       (startime->tv_nsec / NSEC_PER_MSEC),
+                       (tm.tm_year + 1900), (tm.tm_mon + 1), tm.tm_mday,
+                       tm.tm_hour, tm.tm_min, tm.tm_sec,
+                       (unsigned long)(t->tv.tv_usec / USEC_PER_MSEC));
+}
+
+static void probe_binder_update_info(void *ignore, struct binder_transaction *t,
+                          struct binder_transaction_log_entry *e)
+{
+       ktime_get_ts(&e->timestamp);
+       do_gettimeofday(&e->tv);
+       e->tv.tv_sec -= (sys_tz.tz_minuteswest * 60);
+       memcpy(&t->timestamp, &e->timestamp, sizeof(struct timespec));
+       memcpy(&t->tv, &e->tv, sizeof(struct timeval));
+}
+
+static void probe_print_binder_transaction_ext(void *ignore, struct seq_file *m,
+                                        struct binder_transaction *t)
+{
+       struct rtc_time tm;
+
+       rtc_time_to_tm(t->tv.tv_sec, &tm);
+       seq_printf(m,
+                  " start %lu.%06lu android %d-%02d-%02d %02d:%02d:%02d.%03lu",
+                  (unsigned long)t->timestamp.tv_sec,
+                  (t->timestamp.tv_nsec / NSEC_PER_USEC),
+                  (tm.tm_year + 1900), (tm.tm_mon + 1), tm.tm_mday,
+                  tm.tm_hour, tm.tm_min, tm.tm_sec,
+                  (unsigned long)(t->tv.tv_usec / USEC_PER_MSEC));
+
+}
+
+static int __init init_binder_latency_tracer(void)
+{
+       register_trace_binder_free_transaction(
+                       probe_binder_free_transaction, NULL);
+       register_trace_binder_update_info(
+                       probe_binder_update_info, NULL);
+       register_trace_print_binder_transaction_ext(
Quoted 9 lines

drivers/android/binder_latency_tracer.c line 87

Quoted 1 line
drivers/android/binder_latency_tracer.c new file +1 -0
+                       probe_print_binder_transaction_ext, NULL);

Ah, now the trace in the print path makes sense. Please add a more detailed description to the commit message. Also add a comment at the trace point that it is for modules to attach to so additional information can be printed. Also, make the names of the tracepoints more descriptive of what they really are ...something like trace_binder_txn_latency_(alloc|info|free)

Yes, we agree that this is a better way to make these tracepoints more meaningful, and we would change it.

Quoted 73 lines
Quoted 73 lines
drivers/android/binder_latency_tracer.c new file +18 -0
+
+       return 0;
+}
+
+static void exit_binder_latency_tracer(void)
+{
+       unregister_trace_binder_free_transaction(
+                       probe_binder_free_transaction, NULL);
+       unregister_trace_binder_update_info(
+                       probe_binder_update_info, NULL);
+       unregister_trace_print_binder_transaction_ext(
+                       probe_print_binder_transaction_ext, NULL);
+}
+
+module_init(init_binder_latency_tracer);
+module_exit(exit_binder_latency_tracer);
+
+MODULE_LICENSE("GPL v2");
drivers/android/binder_trace.h +23 -4
index 7acc18d..466993e 100644
@@ -18,6 +18,7 @@
 struct binder_ref_data;
 struct binder_thread;
 struct binder_transaction;
+struct binder_transaction_log_entry;
 
 TRACE_EVENT(binder_ioctl,
        TP_PROTO(unsigned int cmd, unsigned long arg),
@@ -95,6 +96,18 @@
                  __entry->thread_todo)
 );
 
+DECLARE_TRACE(binder_update_info,
+       TP_PROTO(struct binder_transaction *t,
+                struct binder_transaction_log_entry *e),
+       TP_ARGS(t, e)
+);
+
+DECLARE_TRACE(print_binder_transaction_ext,
+       TP_PROTO(struct seq_file *m,
+                struct binder_transaction *t),
+       TP_ARGS(m, t)
+);
+
 TRACE_EVENT(binder_free_transaction,
        TP_PROTO(struct binder_transaction *t),
        TP_ARGS(t),
@@ -115,11 +128,18 @@
                __entry->to_thread = t->to_thread ? t->to_thread->pid : 0;
                __entry->code = t->code;
                __entry->flags = t->flags;
-       ),
-       TP_printk("transaction=%d from %d:%d to %d:%d flags=0x%x code=0x%x",
+#ifdef CONFIG_BINDER_USER_TRACKING
+               __entry->start_sec = t->timestamp.tv_sec;
+               __entry->start_nsec = t->timestamp.tv_nsec / NSEC_PER_MSEC;
+#else
+               __entry->start_sec = 0;
+               __entry->start_nsec = 0;
+#endif
+       ),
+       TP_printk("transaction=%d from %d:%d to %d:%d flags=0x%x code=0x%x start %lu.%03ld",
                  __entry->debug_id, __entry->from_proc, __entry->from_thread,
                  __entry->to_proc, __entry->to_thread, __entry->code,
-                 __entry->flags)
+                 __entry->flags, __entry->start_sec, __entry->start_nsec)
 );
 
 TRACE_EVENT(binder_transaction,
--

1.7.9.5