[RFC][patch] time applet execution speed

Bernhard Fischer rep.nop at aon.at
Fri Sep 23 21:33:02 UTC 2005


Hi,

The attached patch provides a simple mean to determine how fast an
applet is.
This is ment as a debugging aid and to help (speed-) regression tests,
eventually.

Would such infrastructure be considered useful?



example usage:

$ time ./busybox --time echo "whatever"
whatever
Took 136360 ticks

real	0m0.001s
user	0m0.000s
sys	0m0.002s


or, if you know your CPU-speed in MHz and want to output the elapsed
time in usecs:

$ time ./busybox --time=3200 echo "whatever"
whatever
Took 37.545 µsec

real	0m0.001s
user	0m0.000s
sys	0m0.001s

-------------- next part --------------
diff -X excl -rduNp busybox.oorig/applets/applets.c busybox/applets/applets.c
--- busybox.oorig/applets/applets.c	2005-09-20 22:31:03.000000000 +0200
+++ busybox/applets/applets.c	2005-09-23 23:12:41.000000000 +0200
@@ -64,7 +64,7 @@ const size_t NUM_APPLETS = (sizeof (appl
 #define CONFIG_FILE "/etc/busybox.conf"
 
 /* applets [] is const, so we have to define this "override" structure */
-struct BB_suid_config
+static struct BB_suid_config
 {
   struct BB_applet *m_applet;
 
@@ -73,7 +73,7 @@ struct BB_suid_config
   mode_t m_mode;
 
   struct BB_suid_config *m_next;
-} static *suid_config;
+} *suid_config;
 
 static int suid_cfg_readable;
 
@@ -442,8 +442,52 @@ struct BB_applet *find_applet_by_name (c
 				  applet_name_compare);
 }
 
+#if ENABLE_FEATURE_APPLET_SPEED
+/* TODO: support more instruction sets and perhaps move this to another file  */
+
+#if defined __x86_64__ || defined __i386__
+typedef unsigned long long cycles_t;
+static cycles_t get_cycles(void)
+{
+	unsigned low, high;
+	unsigned long long ret;
+
+	asm volatile("rdtsc" : "=a" (low), "=d" (high));
+	ret = high;
+	ret = (ret << 32) | low;
+	return ret;
+}
+#elif defined __PPC__
+typedef unsigned long long cycles_t;
+static cycles_t get_cycles(void)
+{
+	cycles_t ret;
+	asm volatile("mftb %0" : "=r" (ret) : );
+	return ret;
+}
+#elif defined __ia64__ || defined __PPC64__
+#include <asm/timex.h>
+#else
+#warning get_cycles not implemented on your architecture.. trying asm/timex.h
+#include <asm/timex.h>
+#endif
+double _cpu_mhz;
+static cycles_t _start_time;
+static void _print_used_cycles(void)
+{
+	cycles_t _end_time = get_cycles();
+	const char *unit = (_cpu_mhz > 0.0000001)?"µsec":"ticks";
+	if (0.0000001 > _cpu_mhz)
+		_cpu_mhz = 1;
+	printf("Took %g %s\n", (_end_time - _start_time) / _cpu_mhz, unit);
+}
+
+#endif /* ENABLE_FEATURE_APPLET_SPEED */
+
 void run_applet_by_name (const char *name, int argc, char **argv)
 {
+
+
 	if(ENABLE_FEATURE_SUID_CONFIG) parse_config_file ();
 
 	if(!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
@@ -453,6 +497,8 @@ void run_applet_by_name (const char *nam
 		bb_applet_name = applet_using->name;
 		if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage ();
 		if(ENABLE_FEATURE_SUID) check_suid (applet_using);
+		if (ENABLE_FEATURE_APPLET_SPEED) atexit(_print_used_cycles);
+		_start_time = get_cycles();
 		exit ((*(applet_using->main)) (argc, argv));
 	}
 }
diff -X excl -rduNp busybox.oorig/applets/busybox.c busybox/applets/busybox.c
--- busybox.oorig/applets/busybox.c	2005-09-20 22:31:03.000000000 +0200
+++ busybox/applets/busybox.c	2005-09-23 22:55:57.000000000 +0200
@@ -107,8 +107,17 @@ int busybox_main(int argc, char **argv)
 		return rc;
 	}
 
+	/* handle applet-timing. Give the current cpu_mhz as --time=12.0 */
+#if ENABLE_FEATURE_APPLET_SPEED
+	if (argc > 1 && !strncmp(argv[1], "--time", 6)) {
+		extern double _cpu_mhz;
+		if (sscanf(argv[1], "--time=%lf", &_cpu_mhz) != 1)
+			_cpu_mhz = 0.0; /* just report ticks */
+		argc--; argv++;
+	}
+#endif /* ENABLE_FEATURE_APPLET_SPEED */
+
 	/* Deal with --help.  (Also print help when called with no arguments) */
-	
 	if (argc==1 || !strcmp(argv[1],"--help") ) {
 		if (argc>2) {
 			run_applet_by_name(bb_applet_name=argv[2], 2, argv);
diff -X excl -rduNp busybox.oorig/sysdeps/linux/Config.in busybox/sysdeps/linux/Config.in
--- busybox.oorig/sysdeps/linux/Config.in	2005-09-20 22:30:59.000000000 +0200
+++ busybox/sysdeps/linux/Config.in	2005-09-23 20:43:53.000000000 +0200
@@ -297,5 +297,10 @@ config CONFIG_EFENCE
 
 endchoice
 
+config CONFIG_FEATURE_APPLET_SPEED
+	bool "Time applet execution speed"
+	default n
+	help
+	  Say Y here if you wish to measure the execution time of applets.
 
 endmenu


More information about the busybox mailing list