Skip to contentSkip to conversation

Split udev_sysfs.c to leave out caches from libudev

RFC PATCH In review Alan Jenkins 2 participants 4 messages Sep 2, 2008 – Sep 3, 2008

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Split udev_sysfs.c to leave out caches from libudev

udev_sysfs.c device and attribute value caches are only appropriate for short-lived processes, like udev-event and usbid. In long lived processes they would fill up with stale data.

Fortunately libudev doesn't use these caches. But this should be made explicit to guard against future changes. Let's move the caches into a separate file, with its own initialization and cleanup functions.

extras/usb_id/Makefile.am +1 -0
index 0de004a..bf3aa1c 100644
@@ -9,6 +9,7 @@ AM_CPPFLAGS = \
 usb_id_SOURCES = \
 	usb_id.c \
 	../../udev/udev_sysfs.c \
+	../../udev/udev_sysfs_cache.c \
 	../../udev/udev_sysdeps.c \
 	../../udev/udev_utils.c \
 	../../udev/udev_utils_string.c
extras/usb_id/usb_id.c +2 -0
index 92492c8..cd0705d 100644
@@ -387,6 +387,7 @@ int main(int argc, char **argv)
 
 	logging_init("usb_id");
 	sysfs_init();
+	sysfs_cache_init();
 
 	while (1) {
 		int option;
@@ -463,6 +464,7 @@ int main(int argc, char **argv)
 	}
 
 exit:
+	sysfs_cache_cleanup();
 	sysfs_cleanup();
 	logging_close();
 	return retval;
udev/Makefile.am +1 -0
index 4a7c785..2d25518 100644
@@ -30,6 +30,7 @@ common_files = \
 	udev_rules_parse.c \
 	udev_sysdeps.c \
 	udev_sysfs.c \
+	udev_sysfs_cache.c \
 	udev_utils.c \
 	udev_utils_file.c \
 	udev_utils_string.c
udev/test-udev.c +2 -0
index 591e930..484c3c0 100644
@@ -130,6 +130,7 @@ int main(int argc, char *argv[], char *envp[])
 	}
 
 	sysfs_init();
+	sysfs_cache_init();
 	udev_rules_init(&rules, 0);
 
 	dev = sysfs_device_get(devpath);
@@ -166,6 +167,7 @@ int main(int argc, char *argv[], char *envp[])
 	udev_device_cleanup(udev);
 fail:
 	udev_rules_cleanup(&rules);
+	sysfs_cache_cleanup();
 	sysfs_cleanup();
 	selinux_exit();
 
udev/udev.h +6 -2
index b633807..455b68e 100644
@@ -114,12 +114,16 @@ extern int sysfs_init(void);
 extern void sysfs_cleanup(void);
 extern void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
 				    const char *subsystem, const char *driver);
+extern int sysfs_resolve_link(char *path, size_t size);
+extern int sysfs_lookup_devpath_by_subsys_id(char *devpath, size_t len, const char *subsystem, const char *id);
+
+/* udev_sysfs_cache.c */
+extern int sysfs_cache_init(void);
+extern void sysfs_cache_cleanup(void);
 extern struct sysfs_device *sysfs_device_get(const char *devpath);
 extern struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev);
 extern struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem);
 extern char *sysfs_attr_get_value(const char *devpath, const char *attr_name);
-extern int sysfs_resolve_link(char *path, size_t size);
-extern int sysfs_lookup_devpath_by_subsys_id(char *devpath, size_t len, const char *subsystem, const char *id);
 
 /* udev_node.c */
 extern int udev_node_mknod(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid);
udev/udev_device.c +0 -14
index 9888676..a2f225a 100644
@@ -66,17 +66,3 @@ void udev_device_cleanup(struct udevice *udev)
 	name_list_cleanup(&udev->env_list);
 	free(udev);
 }
-
-dev_t udev_device_get_devt(struct udevice *udev)
-{
-	const char *attr;
-	unsigned int maj, min;
-
-	/* read it from sysfs  */
-	attr = sysfs_attr_get_value(udev->dev->devpath, "dev");
-	if (attr != NULL) {
-		if (sscanf(attr, "%u:%u", &maj, &min) = 2)
-			return makedev(maj, min);
-	}
-	return makedev(0, 0);
-}
udev/udev_device_event.c +14 -0
index 045035d..d031226 100644
@@ -257,3 +257,17 @@ int udev_device_event(struct udev_rules *rules, struct udevice *udev)
 exit:
 	return retval;
 }
+
+dev_t udev_device_get_devt(struct udevice *udev)
+{
+	const char *attr;
+	unsigned int maj, min;
+
+	/* read it from sysfs  */
+	attr = sysfs_attr_get_value(udev->dev->devpath, "dev");
+	if (attr != NULL) {
+		if (sscanf(attr, "%u:%u", &maj, &min) = 2)
+			return makedev(maj, min);
+	}
+	return makedev(0, 0);
+}
udev/udev_sysfs.c +0 -302

325 lines not on this page · raw message

udev/udev_sysfs_cache.c new file +341 -0

341 lines not on this page · raw message

udev/udevd.c +2 -0
index 22d261f..bbe0b6d 100644
@@ -1077,6 +1077,7 @@ int main(int argc, char *argv[], char *envp[])
 
 	/* parse the rules and keep them in memory */
 	sysfs_init();
+	sysfs_cache_init();
 	udev_rules_init(&rules, 1);
 
 	export_initial_seqnum();
@@ -1289,6 +1290,7 @@ int main(int argc, char *argv[], char *envp[])
 exit:
 	udev_rules_cleanup(&rules);
 	sysfs_cleanup();
+	sysfs_cache_cleanup();
 	selinux_exit();
 
 	if (signal_pipe[READ_END] >= 0)
#1 lore raw
Quoted 3 lines · Alan Jenkins

udev_sysfs.c device and attribute value caches are only appropriate for short-lived processes, like udev-event and usbid. In long lived processes they would fill up with stale data.

Sure, the cache was never coded to be used in threaded programs.

Quoted 1 line

Fortunately libudev doesn't use these caches.

For good reason, yeah. And it will not, at least not in its current state.

Quoted 3 lines

But this should be made
explicit to guard against future changes. Let's move the caches into a separate file, with its own initialization and cleanup functions.

Wouldn't it be nicer to create a sysfs_cache object, which is passed to all these functions? The "cheap" tools can just use NULL there, but any possible user could have more than one instance of the cache. The cache is really needed if you do thousand of rule compares against a sysfs value, over and over. I guess we should start changing the stuff to work with local vars, instead of moving the current global stuff to a different file?

In the long run libudev will take over a lot of the functionality and replace the old code, but depending on the actual need, it might take a while ...

Thanks,
Kay

#2 lore raw
In reply to Alan Jenkins
Quoted 21 lines · Kay Sievers
Quoted 4 lines · Alan Jenkins

udev_sysfs.c device and attribute value caches are only appropriate for short-lived processes, like udev-event and usbid. In long lived processes they would fill up with stale data.

Sure, the cache was never coded to be used in threaded programs.

Quoted 2 lines

Fortunately libudev doesn't use these caches.

For good reason, yeah. And it will not, at least not in its current state.

Quoted 4 lines

But this should be made
explicit to guard against future changes. Let's move the caches into a separate file, with its own initialization and cleanup functions.

Quoted 8 lines

Wouldn't it be nicer to create a sysfs_cache object, which is passed to all these functions? The "cheap" tools can just use NULL there, but any possible user could have more than one instance of the cache. The cache is really needed if you do thousand of rule compares against a sysfs value, over and over. I guess we should start changing the stuff to work with local vars, instead of moving the current global stuff to a different file?

Good point. That helps make the cache usage more explicit, and it's much nicer for my purposes as well. (I had blindly converted the global cache variables to per-thread variables, without touching the callers).

Ok. To keep the existing behaviour where udev_rules_get_run() reuses the cache from udev_rules_get_name(), they both need a sysfs_cache parameter. Then I have to change test-udev and udevtest as well as udevd - no point having test programs if they use different code paths. I'll see what that looks like then and resend.

Thanks
Alan

#3 lore raw
In reply to Alan Jenkins
Quoted 40 lines · Alan Jenkins
Quoted 24 lines · Kay Sievers
Quoted 5 lines · Alan Jenkins

udev_sysfs.c device and attribute value caches are only appropriate for short-lived processes, like udev-event and usbid. In long lived processes they would fill up with stale data.

Sure, the cache was never coded to be used in threaded programs.

Quoted 3 lines

Fortunately libudev doesn't use these caches.

For good reason, yeah. And it will not, at least not in its current state.

Quoted 5 lines

But this should be made
explicit to guard against future changes. Let's move the caches into a separate file, with its own initialization and cleanup functions.

Quoted 9 lines

Wouldn't it be nicer to create a sysfs_cache object, which is passed to all these functions? The "cheap" tools can just use NULL there, but any possible user could have more than one instance of the cache. The cache is really needed if you do thousand of rule compares against a sysfs value, over and over. I guess we should start changing the stuff to work with local vars, instead of moving the current global stuff to a different file?

Good point. That helps make the cache usage more explicit, and it's much nicer for my purposes as well. (I had blindly converted the global cache variables to per-thread variables, without touching the callers).

Quoted 6 lines

Ok. To keep the existing behaviour where udev_rules_get_run() reuses the cache from udev_rules_get_name(), they both need a sysfs_cache parameter. Then I have to change test-udev and udevtest as well as udevd - no point having test programs if they use different code paths. I'll see what that looks like then and resend.

Oops. sysfs_cache can't be made optional ("just use NULL") without a bit more effort. At the moment we rely on sysfs_cleanup() freeing everything in the cache. If the cache isn't used, we need a new API to free individual results.

So we also need sysfs_device_cleanup(), to free the device if it's not on a cache list. And sysfs_attr_get_value() can copy the result to a buffer instead of returning a string. I think that's reasonable.

Alan