Skip to contentSkip to conversation

I2C statistics as sysfs attributes

RFC PATCH Superseded Sui Chen 3 participants 6 messages Nov 9, 2021 – Nov 11, 2021

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Sui Chen

author
#0 lore raw

cover I2C statistics as sysfs attributes

Add I2C statistics such as Bus Error counts and NACK counts as sysfs attributes.

I2C statistics such as bus error counts and NACK counts, are implemented in many I2C controllers.

Some drivers already populate the counters in debugfs. Having those statistics in sysfs can enable for a unified definition across various I2C drivers, make the statistics more ABI-stable.

Overall the patch works in the following way:
1) A sysfs directory for I2C statistics is created for an i2c_adapter.
2) Each specific I2C driver can optionally instantiate each of the statistics individually.

Test Process:
1. Clone the OpenBMC repository
2. devtool modifyand apply patch to the linux-nuvoton recipe
3. Build image for quanta-gsj
4. Build QEMU
5. Run the image-bmc image in QEMU

Results:

root@gsj:/sys/class/i2c-adapter/i2c-1/stats# ls
ber_cnt       i2c_speed     nack_cnt      rec_fail_cnt  rec_succ_cnt
timeout_cnt
root@gsj:/sys/class/i2c-adapter/i2c-1/stats# cat *
0
100000
0
0
0
0

Sui Chen (2):
i2c debug counters as sysfs attributes
add npcm7xx debug counters as sysfs attributes

 drivers/i2c/busses/i2c-npcm7xx.c |  8 +++
 drivers/i2c/i2c-core-base.c      |  2 +
 drivers/i2c/i2c-dev.c            | 98 ++++++++++++++++++++++++++++++++
 include/linux/i2c.h              | 26 +++++++++
 4 files changed, 134 insertions(+)

(Previously sent to linux-i2c, resending to linux-kernel with CCed correspondents)

CC OpenBMC Mailing List <[email protected]>
CC linux-i2c <[email protected]>
CC linux-kernel <[email protected]>
CC Joel Stanley <[email protected]>
CC Andrew Jeffery <[email protected]>
CC Tali Perry <[email protected]>
CC Benjamin Fair <[email protected]>
CC Josh Lehan <[email protected]>
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
author
#1 lore raw

RFC PATCH 1/2 i2c debug counters as sysfs attributes

This change adds a few example I2C debug counters as sysfs attributes:
- ber_cnt (bus error count)
- nack_cnt (NACK count)
- rec_fail_cnt, rec_succ_cnt (recovery failure/success count)
- timeout_cnt (timeout count)
- i2c_speed (bus frequency)

The function i2c_adapter_create_stats_folder creates a stats directory in the device's sysfs directory to hold the debug counters. The platform drivers will instantiate the counters in the stats directory if available.

---
 drivers/i2c/i2c-core-base.c |  2 +
 drivers/i2c/i2c-dev.c       | 98 +++++++++++++++++++++++++++++++++++++
 include/linux/i2c.h         | 26 ++++++++++
 3 files changed, 126 insertions(+)
drivers/i2c/i2c-core-base.c +2 -0
index 84f12bf90644a..c42113daf32a7 100644
@@ -1610,6 +1610,8 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
 	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
 	mutex_unlock(&core_lock);
 
+	i2c_adapter_create_stats_folder(adap);
+
 	return 0;
 
 out_reg:
drivers/i2c/i2c-dev.c +98 -0

104 lines not on this page · raw message

include/linux/i2c.h +26 -0
index 3eb60a2e9e618..b5c84297c60d9 100644
@@ -21,6 +21,7 @@
 #include <linux/of.h>		/* for struct device_node */
 #include <linux/swab.h>		/* for swab16 */
 #include <uapi/linux/i2c.h>
+#include <linux/slab.h> /* for kzalloc */
 
 extern struct bus_type i2c_bus_type;
 extern struct device_type i2c_adapter_type;
@@ -684,6 +685,26 @@ struct i2c_adapter_quirks {
 	u16 max_comb_2nd_msg_len;
 };
 
+/**
+ * I2C statistics
+ * The list of statistics are currently copied from npcm7xx.
+ * Perhaps a more universal set of statistics can be used.
+ *
+ * The stats are currently modeled as pointers to members in the bus drivers.
+ * A null pointer indicates the counter is not supported by the bus driver.
+ */
+struct i2c_adapter_stats {
+	struct kobject* kobj;
+
+	// For the counters a NULL value means the counter is not available.
+	u64* ber_cnt;
+	u64* nack_cnt;
+	u64* rec_succ_cnt;
+	u64* rec_fail_cnt;
+	u64* timeout_cnt;
+	u64* i2c_speed;
+};
+
 /* enforce max_num_msgs = 2 and use max_comb_*_len for length checks */
 #define I2C_AQ_COMB			BIT(0)
 /* first combined message must be write */
@@ -735,12 +756,17 @@ struct i2c_adapter {
 
 	struct i2c_bus_recovery_info *bus_recovery_info;
 	const struct i2c_adapter_quirks *quirks;
+	struct i2c_adapter_stats* stats;
 
 	struct irq_domain *host_notify_domain;
 	struct regulator *bus_regulator;
 };
 #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
 
+void i2c_adapter_create_stats_folder(struct i2c_adapter* adapter);
+void i2c_adapter_stats_register_counter(struct i2c_adapter* adapter,
+	const char* counter_name, void* data_source);
+
 static inline void *i2c_get_adapdata(const struct i2c_adapter *adap)
 {
 	return dev_get_drvdata(&adap->dev);
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
#2 lore raw

Re: [RFC Patch Resend 1/2] i2c debug counters as sysfs attributes

Quoted 11 lines · Sui Chen

This change adds a few example I2C debug counters as sysfs attributes:
- ber_cnt (bus error count)
- nack_cnt (NACK count)
- rec_fail_cnt, rec_succ_cnt (recovery failure/success count)
- timeout_cnt (timeout count)
- i2c_speed (bus frequency)

The function i2c_adapter_create_stats_folder creates a stats directory in the device's sysfs directory to hold the debug counters. The platform drivers will instantiate the counters in the stats directory if available.

[]

drivers/i2c/i2c-dev.c

Quoted 1 line
drivers/i2c/i2c-dev.c +0 -0

[]

drivers/i2c/i2c-dev.c static void __exit i2c_dev_exit(void) · line 781

Quoted 5 lines
drivers/i2c/i2c-dev.c +5 -0
+static ssize_t ber_cnt_show(struct device* pdev,
+	struct device_attribute* attr, char* buf) {
+	u64* ber_cnt = kobj_to_adapter(pdev)->stats->ber_cnt;
+	if (ber_cnt == NULL) return 0;
+	ssize_t ret = sprintf(buf, "%llu\n", *ber_cnt);

Please do not declare after code.

And this should instead use:

	return sysfs_emit(buf, "%llu\n", *ber_cnt);

drivers/i2c/i2c-dev.c static void __exit i2c_dev_exit(void) · line 790

Quoted 9 lines
drivers/i2c/i2c-dev.c +9 -0
+	return ret;
+}
+DEVICE_ATTR_RO(ber_cnt);
+
+ssize_t nack_cnt_show(struct device* pdev,
+	struct device_attribute* attr, char* buf) {
+	u64* nack_cnt = kobj_to_adapter(pdev)->stats->nack_cnt;
+	if (nack_cnt == NULL) return 0;
+	ssize_t ret = sprintf(buf, "%llu\n", *nack_cnt);

etc...

#3 lore raw

Re: [RFC Patch Resend 1/2] i2c debug counters as sysfs attributes

In reply to Sui Chen
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Sui,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on wsa/i2c/for-next]
[also build test ERROR on v5.15 next-20211111]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Sui-Chen/I2C-statistics-as-sysfs-attributes/20211110-065938
base:   https://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git i2c/for-next
config: parisc-defconfig (attached as .config)
compiler: hppa-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/769ea06c7d8b5744deba5e6cfe2df6d9079b41df
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Sui-Chen/I2C-statistics-as-sysfs-attributes/20211110-065938 git checkout 769ea06c7d8b5744deba5e6cfe2df6d9079b41df
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=parisc SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

hppa-linux-ld: drivers/i2c/i2c-core-base.o: in function `i2c_register_adapter':

>> (.text+0x3258): undefined reference to `i2c_adapter_create_stats_folder'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#4 lore raw

Re: [RFC Patch Resend 1/2] i2c debug counters as sysfs attributes

In reply to Sui Chen
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Sui,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on wsa/i2c/for-next]
[also build test ERROR on linux/master linus/master v5.15 next-20211111] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Sui-Chen/I2C-statistics-as-sysfs-attributes/20211110-065938
base:   https://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git i2c/for-next
config: openrisc-buildonly-randconfig-r003-20211111 (attached as .config)
compiler: or1k-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/769ea06c7d8b5744deba5e6cfe2df6d9079b41df
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Sui-Chen/I2C-statistics-as-sysfs-attributes/20211110-065938 git checkout 769ea06c7d8b5744deba5e6cfe2df6d9079b41df
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross ARCH=openrisc

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

drivers/i2c/i2c-dev.c: In function 'ber_cnt_show':

>> drivers/i2c/i2c-dev.c:779:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
     779 |         ssize_t ret = sprintf(buf, "%llu\n", *ber_cnt);
         |         ^~~~~~~
   drivers/i2c/i2c-dev.c: At top level:
>> drivers/i2c/i2c-dev.c:784:9: error: no previous prototype for 'nack_cnt_show' [-Werror=missing-prototypes]
     784 | ssize_t nack_cnt_show(struct device* pdev,
         |         ^~~~~~~~~~~~~
   drivers/i2c/i2c-dev.c: In function 'nack_cnt_show':
   drivers/i2c/i2c-dev.c:788:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
     788 |         ssize_t ret = sprintf(buf, "%llu\n", *nack_cnt);
         |         ^~~~~~~
   drivers/i2c/i2c-dev.c: At top level:
>> drivers/i2c/i2c-dev.c:793:9: error: no previous prototype for 'rec_succ_cnt_show' [-Werror=missing-prototypes]
     793 | ssize_t rec_succ_cnt_show(struct device* pdev,
         |         ^~~~~~~~~~~~~~~~~
   drivers/i2c/i2c-dev.c: In function 'rec_succ_cnt_show':
   drivers/i2c/i2c-dev.c:797:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
     797 |         ssize_t ret = sprintf(buf, "%llu\n", *rec_succ_cnt);
         |         ^~~~~~~
   drivers/i2c/i2c-dev.c: At top level:
>> drivers/i2c/i2c-dev.c:802:9: error: no previous prototype for 'rec_fail_cnt_show' [-Werror=missing-prototypes]
     802 | ssize_t rec_fail_cnt_show(struct device* pdev,
         |         ^~~~~~~~~~~~~~~~~
   drivers/i2c/i2c-dev.c: In function 'rec_fail_cnt_show':
   drivers/i2c/i2c-dev.c:806:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
     806 |         ssize_t ret = sprintf(buf, "%llu\n", *rec_fail_cnt);
         |         ^~~~~~~
   drivers/i2c/i2c-dev.c: At top level:
>> drivers/i2c/i2c-dev.c:811:9: error: no previous prototype for 'timeout_cnt_show' [-Werror=missing-prototypes]
     811 | ssize_t timeout_cnt_show(struct device* pdev,
         |         ^~~~~~~~~~~~~~~~
   drivers/i2c/i2c-dev.c: In function 'timeout_cnt_show':
   drivers/i2c/i2c-dev.c:815:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
     815 |         ssize_t ret = sprintf(buf, "%llu\n", *timeout_cnt);
         |         ^~~~~~~
   drivers/i2c/i2c-dev.c: At top level:
>> drivers/i2c/i2c-dev.c:820:9: error: no previous prototype for 'i2c_speed_show' [-Werror=missing-prototypes]
     820 | ssize_t i2c_speed_show(struct device* pdev,
         |         ^~~~~~~~~~~~~~
   drivers/i2c/i2c-dev.c: In function 'i2c_speed_show':
   drivers/i2c/i2c-dev.c:824:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
     824 |         ssize_t ret = sprintf(buf, "%llu\n", *i2c_speed);
         |         ^~~~~~~

cc1: all warnings being treated as errors

vim +779 drivers/i2c/i2c-dev.c

774
775 static ssize_t ber_cnt_show(struct device* pdev,
776 struct device_attribute* attr, char* buf) {
777 u64* ber_cnt = kobj_to_adapter(pdev)->stats->ber_cnt;
778 if (ber_cnt == NULL) return 0;

 > 779		ssize_t ret = sprintf(buf, "%llu\n", *ber_cnt);
   780		return ret;
   781	}
   782	DEVICE_ATTR_RO(ber_cnt);
   783	
 > 784	ssize_t nack_cnt_show(struct device* pdev,
   785		struct device_attribute* attr, char* buf) {
   786		u64* nack_cnt = kobj_to_adapter(pdev)->stats->nack_cnt;
   787		if (nack_cnt == NULL) return 0;
   788		ssize_t ret = sprintf(buf, "%llu\n", *nack_cnt);
   789		return ret;
   790	}
   791	DEVICE_ATTR_RO(nack_cnt);
   792	
 > 793	ssize_t rec_succ_cnt_show(struct device* pdev,
   794		struct device_attribute* attr, char* buf) {
   795		u64* rec_succ_cnt = kobj_to_adapter(pdev)->stats->rec_succ_cnt;
   796		if (rec_succ_cnt == NULL) return 0;
   797		ssize_t ret = sprintf(buf, "%llu\n", *rec_succ_cnt);
   798		return ret;
   799	}
   800	DEVICE_ATTR_RO(rec_succ_cnt);
   801	
 > 802	ssize_t rec_fail_cnt_show(struct device* pdev,
   803		struct device_attribute* attr, char* buf) {
   804		u64* rec_fail_cnt = kobj_to_adapter(pdev)->stats->rec_fail_cnt;
   805		if (rec_fail_cnt == NULL) return 0;
   806		ssize_t ret = sprintf(buf, "%llu\n", *rec_fail_cnt);
   807		return ret;
   808	}
   809	DEVICE_ATTR_RO(rec_fail_cnt);
   810	
 > 811	ssize_t timeout_cnt_show(struct device* pdev,
   812		struct device_attribute* attr, char* buf) {
   813		u64* timeout_cnt = kobj_to_adapter(pdev)->stats->timeout_cnt;
   814		if (timeout_cnt == NULL) return 0;
   815		ssize_t ret = sprintf(buf, "%llu\n", *timeout_cnt);
   816		return ret;
   817	}
   818	DEVICE_ATTR_RO(timeout_cnt);
   819	
 > 820	ssize_t i2c_speed_show(struct device* pdev,

821 struct device_attribute* attr, char* buf) {
822 u64* i2c_speed = kobj_to_adapter(pdev)->stats->i2c_speed;
823 if (i2c_speed == NULL) return 0;
824 ssize_t ret = sprintf(buf, "%llu\n", *i2c_speed);
825 return ret;
826 }
827 DEVICE_ATTR_RO(i2c_speed);
828

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author
#5 lore raw

RFC PATCH 2/2 add npcm7xx debug counters as sysfs attributes

In reply to Sui Chen

This change adds npcm7xx debug counters as sysfs attributes using the i2c_adapter_stats_register_counter function.

---
 drivers/i2c/busses/i2c-npcm7xx.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/i2c/busses/i2c-npcm7xx.c +8 -0
index 2ad166355ec9b..def044207cae2 100644
@@ -2224,6 +2224,14 @@ static void npcm_i2c_init_debugfs(struct platform_device *pdev,
 	debugfs_create_u64("rec_fail_cnt", 0444, d, &bus->rec_fail_cnt);
 	debugfs_create_u64("timeout_cnt", 0444, d, &bus->timeout_cnt);
 
+	/* register debug counters in sysfs */
+	i2c_adapter_stats_register_counter(&bus->adap, "ber_cnt", &bus->ber_cnt);
+	i2c_adapter_stats_register_counter(&bus->adap, "nack_cnt", &bus->nack_cnt);
+	i2c_adapter_stats_register_counter(&bus->adap, "rec_succ_cnt", &bus->rec_succ_cnt);
+	i2c_adapter_stats_register_counter(&bus->adap, "rec_fail_cnt", &bus->rec_fail_cnt);
+	i2c_adapter_stats_register_counter(&bus->adap, "timeout_cnt", &bus->timeout_cnt);
+	i2c_adapter_stats_register_counter(&bus->adap, "i2c_speed", &bus->bus_freq);
+
 	bus->debugfs = d;
 }
 
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog