Skip to contentSkip to conversation

ALSA: control: Use xarray for faster lookups

RFC PATCH Superseded Takashi Iwai 2 participants 5 messages Oct 28, 2021 – Oct 29, 2021 Sound

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 ALSA: control: Use xarray for faster lookups

The control elements are managed in a single linked list and we traverse the whole list for matching each numid or ctl id per every inquiry of a control element. This is OK-ish for a small number of elements but obviously it doesn't scale. Especially the matching with the ctl id takes time because it checks each field of the snd_ctl_id element, e.g. the name string is matched with strcmp().

This patch adds the hash tables with Xarray in addition to the linked list for improving the lookup speed of a control element. There are two xarray tables added to the card; one for numid and another for ctl id. For the numid, we use the numid as the index, while for the ctl id, we calculate a hash key.

The lookup is done via a single xa_load(). When nothing hits, we look through the full list entries as a fallback. So, the inquiry for a non-existing element would take (even slightly longer) time with this patch.

---

This is not really meant to be merged now, but just shown as a reference implementation, as the issue doesn't surface unless you have a large number of elements and query the late element frequently. In my test, 1,000,000 queries of the 1000th element took over 12 seconds, while the patched kernel took 0.2 second. But for the most cases (at most 20 or 30 elements), it doesn't matter much.

 include/sound/core.h |   3 +
 sound/core/control.c | 142 +++++++++++++++++++++++++++++++++----------
 sound/core/init.c    |   2 +
 3 files changed, 115 insertions(+), 32 deletions(-)
include/sound/core.h +3 -0
index b7e9b58d3c78..e8bb6d943f3f 100644
@@ -14,6 +14,7 @@
 #include <linux/pm.h>			/* pm_message_t */
 #include <linux/stringify.h>
 #include <linux/printk.h>
+#include <linux/xarray.h>
 
 /* number of supported soundcards */
 #ifdef CONFIG_SND_DYNAMIC_MINORS
@@ -103,6 +104,8 @@ struct snd_card {
 	size_t user_ctl_alloc_size;	// current memory allocation by user controls.
 	struct list_head controls;	/* all controls for this card */
 	struct list_head ctl_files;	/* active control files */
+	struct xarray ctl_numids;	/* hash table for numids */
+	struct xarray ctl_hash;		/* hash table for ctl id matching */
 
 	struct snd_info_entry *proc_root;	/* root for soundcard specific files */
 	struct proc_dir_entry *proc_root_link;	/* number link to real id */
sound/core/control.c +110 -32

197 lines not on this page · raw message

sound/core/init.c +2 -0
index ac335f5906c6..a1979fe27d53 100644
@@ -282,6 +282,8 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
 	rwlock_init(&card->ctl_files_rwlock);
 	INIT_LIST_HEAD(&card->controls);
 	INIT_LIST_HEAD(&card->ctl_files);
+	xa_init(&card->ctl_numids);
+	xa_init(&card->ctl_hash);
 	spin_lock_init(&card->files_lock);
 	INIT_LIST_HEAD(&card->files_list);
 	mutex_init(&card->memory_mutex);
Signature · 1 line
2.31.1
#1 lore raw
[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 Takashi,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on tiwai-sound/for-next]
[also build test WARNING on v5.15-rc7 next-20211028]
[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/Takashi-Iwai/ALSA-control-Use-xarray-for-faster-lookups/20211028-210215
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
config: ia64-defconfig (attached as .config)
compiler: ia64-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/d9e50e9647a202433a7b1c261b0edb0021f16004
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Takashi-Iwai/ALSA-control-Use-xarray-for-faster-lookups/20211028-210215 git checkout d9e50e9647a202433a7b1c261b0edb0021f16004
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross ARCH=ia64

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

All warnings (new ones prefixed by >>):

sound/core/control.c: In function 'add_hash_entries':

>> sound/core/control.c:404:16: warning: variable 'err' set but not used [-Wunused-but-set-variable]
     404 |         int i, err;
         |                ^~~

vim +/err +404 sound/core/control.c

398
399 /* add hash entries to numid and ctl xarray tables */
400 static void add_hash_entries(struct snd_card *card,
401 struct snd_kcontrol *kcontrol)
402 {
403 struct snd_ctl_elem_id id = kcontrol->id;

 > 404		int i, err;

405
406 xa_store_range(&card->ctl_numids, kcontrol->id.numid,
407 kcontrol->id.numid + kcontrol->count - 1,
408 kcontrol, GFP_KERNEL);
409
410 for (i = 0; i < kcontrol->count; i++) {
411 id.index = kcontrol->id.index + i;
412 err = xa_insert(&card->ctl_hash, get_ctl_id_hash(&id),
413 kcontrol, GFP_KERNEL);
414 /* ignore errors - it's a best effort */
415 }
416 }
417

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

#2 lore raw
In reply to Takashi Iwai
[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 Takashi,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on tiwai-sound/for-next]
[also build test WARNING on v5.15-rc7 next-20211028]
[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/Takashi-Iwai/ALSA-control-Use-xarray-for-faster-lookups/20211028-210215
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
config: mips-randconfig-s031-20211028 (attached as .config)
compiler: mips64-linux-gcc (GCC) 11.2.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-dirty
# https://github.com/0day-ci/linux/commit/d9e50e9647a202433a7b1c261b0edb0021f16004
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Takashi-Iwai/ALSA-control-Use-xarray-for-faster-lookups/20211028-210215 git checkout d9e50e9647a202433a7b1c261b0edb0021f16004
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=mips

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

sparse warnings: (new ones prefixed by >>)
command-line: note: in included file:
builtin:1:9: sparse: sparse: preprocessor token __ATOMIC_ACQUIRE redefined builtin:0:0: sparse: this was the original definition builtin:1:9: sparse: sparse: preprocessor token __ATOMIC_SEQ_CST redefined builtin:0:0: sparse: this was the original definition builtin:1:9: sparse: sparse: preprocessor token __ATOMIC_ACQ_REL redefined builtin:0:0: sparse: this was the original definition builtin:1:9: sparse: sparse: preprocessor token __ATOMIC_RELEASE redefined builtin:0:0: sparse: this was the original definition

>> sound/core/control.c:377:11: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned long h @@     got restricted snd_ctl_elem_iface_t const [usertype] iface @@

sound/core/control.c:377:11: sparse: expected unsigned long h
sound/core/control.c:377:11: sparse: got restricted snd_ctl_elem_iface_t const [usertype] iface
sound/core/control.c:890:17: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control.c:890:26: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control.c:891:17: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control.c:891:26: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control.c:910:48: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control.c:1604:40: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control.c: note: in included file:
sound/core/control_compat.c:193:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected int [assigned] err @@ got restricted snd_ctl_elem_type_t [usertype] type @@
sound/core/control_compat.c:193:21: sparse: expected int [assigned] err
sound/core/control_compat.c:193:21: sparse: got restricted snd_ctl_elem_type_t [usertype] type
sound/core/control_compat.c:203:14: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control_compat.c:205:14: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control_compat.c:207:14: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control_compat.c:209:14: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control_compat.c:237:21: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control_compat.c:238:21: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control_compat.c:269:21: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer sound/core/control_compat.c:270:21: sparse: sparse: restricted snd_ctl_elem_type_t degrades to integer

vim +377 sound/core/control.c

366
367 /* Compute a hash key for the corresponding ctl id
368 * It's for the name lookup, hence the numid is excluded.
369 * The hash key is bound in LONG_MAX to be used for Xarray key.
370 */
371 #define MULTIPLIER 37
372 static unsigned long get_ctl_id_hash(const struct snd_ctl_elem_id *id)
373 {
374 unsigned long h;
375 const unsigned char *p;
376

 > 377		h = id->iface;

378 h = MULTIPLIER * h + id->device;
379 h = MULTIPLIER * h + id->subdevice;
380 for (p = id->name; *p; p++)
381 h = MULTIPLIER * h + *p;
382 h = MULTIPLIER * h + id->index;
383 h &= LONG_MAX;
384 return h;
385 }
386

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

#3 lore raw
In reply to Takashi Iwai
[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 Takashi,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on tiwai-sound/for-next]
[also build test WARNING on v5.15-rc7 next-20211028]
[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/Takashi-Iwai/ALSA-control-Use-xarray-for-faster-lookups/20211028-210215
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
config: i386-randconfig-a012-20211028 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 5db7568a6a1fcb408eb8988abdaff2a225a8eb72)

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/d9e50e9647a202433a7b1c261b0edb0021f16004
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Takashi-Iwai/ALSA-control-Use-xarray-for-faster-lookups/20211028-210215 git checkout d9e50e9647a202433a7b1c261b0edb0021f16004
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 ARCH=i386

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

All warnings (new ones prefixed by >>):

>> sound/core/control.c:404:9: warning: variable 'err' set but not used [-Wunused-but-set-variable]
           int i, err;
                  ^

1 warning generated.

vim +/err +404 sound/core/control.c

398
399 /* add hash entries to numid and ctl xarray tables */
400 static void add_hash_entries(struct snd_card *card,
401 struct snd_kcontrol *kcontrol)
402 {
403 struct snd_ctl_elem_id id = kcontrol->id;

 > 404		int i, err;

405
406 xa_store_range(&card->ctl_numids, kcontrol->id.numid,
407 kcontrol->id.numid + kcontrol->count - 1,
408 kcontrol, GFP_KERNEL);
409
410 for (i = 0; i < kcontrol->count; i++) {
411 id.index = kcontrol->id.index + i;
412 err = xa_insert(&card->ctl_hash, get_ctl_id_hash(&id),
413 kcontrol, GFP_KERNEL);
414 /* ignore errors - it's a best effort */
415 }
416 }
417

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

#4 lore raw
In reply to Takashi Iwai
[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 Takashi,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on tiwai-sound/for-next]
[also build test ERROR on v5.15-rc7 next-20211028]
[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/Takashi-Iwai/ALSA-control-Use-xarray-for-faster-lookups/20211028-210215
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
config: mips-rs90_defconfig (attached as .config)
compiler: mipsel-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/d9e50e9647a202433a7b1c261b0edb0021f16004
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Takashi-Iwai/ALSA-control-Use-xarray-for-faster-lookups/20211028-210215 git checkout d9e50e9647a202433a7b1c261b0edb0021f16004
# 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=mips SHELL=/bin/bash

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

All errors (new ones prefixed by >>):

mipsel-linux-ld: sound/core/control.o: in function `add_hash_entries':

>> control.c:(.text.add_hash_entries+0x74): undefined reference to `xa_store_range'

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