selftest: size: Add size test for Linux kernel
PATCH v4 1/1 selftest: size: Add size test for Linux kernel
This test shows the amount of memory used by the system. Note that this is dependent on the user-space that is loaded when this program runs. Optimally, this program would be run as the init program itself.
The program is optimized for size itself, to avoid conflating its own execution with that of the system software.
The code is compiled statically, with no stdlibs. On my x86_64 system, this results in a statically linked binary of less than 5K.
---
Changes from v3:
- fix copyright string (again!)
- use __builtin_strlen instead of my own strlen
- replace main with _start
- add more human-readable output
- put libgcc reference into a variable in Makefile
Changes from v2:
- add return values to print routines
- add .gitignore file
Changes from v1:
- use more correct Copyright string in get_size.c
tools/testing/selftests/Makefile | 1 + tools/testing/selftests/size/.gitignore | 1 + tools/testing/selftests/size/Makefile | 23 +++++++ tools/testing/selftests/size/get_size.c | 111 ++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 tools/testing/selftests/size/.gitignore create mode 100644 tools/testing/selftests/size/Makefile create mode 100644 tools/testing/selftests/size/get_size.c
tools/testing/selftests/Makefile +1 -0
@@ -15,6 +15,7 @@ TARGETS += user TARGETS += sysctl TARGETS += firmware TARGETS += ftrace +TARGETS += size TARGETS_HOTPLUG = cpu-hotplug TARGETS_HOTPLUG += memory-hotplug
tools/testing/selftests/size/.gitignore new file +1 -0
@@ -0,0 +1 @@ +get_size
tools/testing/selftests/size/Makefile new file +23 -0
@@ -0,0 +1,23 @@ +#ifndef CC + CC = $(CROSS_COMPILE)gcc +#endif + +#ifndef STRIP + STRIP = $(CROSS_COMPILE)strip +#endif + +all: get_size + +LIBGCC=$(shell $(CC) -print-libgcc-file-name) + +get_size: get_size.c + $(CC) --static -ffreestanding -nostartfiles \ + -Wl,--entry=_start get_size.c $(LIBGCC) \ + -o get_size + $(STRIP) -s get_size + +run_tests: all + ./get_size + +clean: + $(RM) get_size
tools/testing/selftests/size/get_size.c new file +111 -0
111 lines not on this page · raw message
Signature · 1 line
1.8.2.2
Quoted 2 lines · Tim Bird
tools/testing/selftests/size/Makefile new file +0
-0
[...]
tools/testing/selftests/size/Makefile line 16
Quoted 6 lines
tools/testing/selftests/size/Makefile new file +6
-0+LIBGCC=$(shell $(CC) -print-libgcc-file-name) + +get_size: get_size.c + $(CC) --static -ffreestanding -nostartfiles \ + -Wl,--entry=_start get_size.c $(LIBGCC) \ + -o get_size
You don't need -Wl,--entry=_start; that's the default.
You shouldn't need to manually find libgcc, either; the compiler should do that for you. What goes wrong if you don't include that? If you're trying to link libgcc statically, try -static-libgcc.
Also, static is normally spelled -static, not --static.
Quoted 2 lines
tools/testing/selftests/size/get_size.c new file +0
-0
[...]
Quoted 1 line
+int
print(const char *s)
This function, and all the others apart from _start, should be declared static.
Quoted 1 line
+void
num_to_str(unsigned long num, char *s)
Likewise, static.
Quoted 7 lines
tools/testing/selftests/size/get_size.c new file +7
-0+{ +unsigned long long temp, div; +int started; + + temp = num; + div =1000000000000000000LL ; + started =0 ;
tools/testing/selftests/size/get_size.c line 58
Quoted 10 lines
tools/testing/selftests/size/get_size.c new file +10
-0+while (div) { +if (temp/div || started) { + *s++ = (unsigned char )(temp/div +'0' ); + started =1 ; + } + temp -= (temp/div)*div; + div /=10 ; + } + *s =0 ; +}
You'd probably end up with significantly smaller code (and no divisions, and thus no corner cases on architectures that need a special function to do unsigned long long division) if you print in hex. You could also drop the "no leading zeros" logic, and just *always* print a 64-bit value as 16 hex digits.
Quoted 1 line
+int
print_num(unsigned long num)
Likewise, static.
Quoted 11 lines
tools/testing/selftests/size/get_size.c new file +11
-0+{ +char num_buf[30 ]; + +num_to_str (num, num_buf); +return int print_k_value (const char *s,unsigned long num,unsigned long units) +{ +unsigned long long temp; +int ccode;
tools/testing/selftests/size/get_size.c line 81
Quoted 10 lines
tools/testing/selftests/size/get_size.c new file +10
-0+ +1024 ; + num = temp; + ccode =print_num (num); +"\n" ); +return ccode; +}
I'd suggest dropping this entirely, and just always printing the exact values returned by sysinfo. Drop the multiply, too, and just print info.mem_unit as well. It's easy to post-process the value in a more capable environment.
tools/testing/selftests/size/get_size.c line 95
Quoted 13 lines
tools/testing/selftests/size/get_size.c new file +13
-0+/* this program has no main(), as startup libraries are not used */ +void _start (void ) +{ +int ccode; +struct sysinfo info; +unsigned long used; + +"Testing system size.\n" ); +"1..1\n" ); + + ccode =sysinfo (&info); +if (ccode <0 ) { +"not ok 1 get size runtime size\n" );
Shouldn't the "not ok" here and the "ok" below have the same test description?
Quoted 19 lines
tools/testing/selftests/size/get_size.c new file +16
-0+"# could not get sysinfo\n" ); +_exit (ccode); + } +/* ignore cache complexities for now */ + used = info.totalram - info.freeram - info.bufferram; +print_k_value ("ok 1 get runtime memory use # size = " , used, + info.mem_unit); + +"# System runtime memory report (units in Kilobytes):\n" ); +print_k_value ("# Total: " , info.totalram, info.mem_unit); +print_k_value ("# Free: " , info.freeram, info.mem_unit); +print_k_value ("# Buffer: " , info.bufferram, info.mem_unit); +print_k_value ("# In use: " , used, info.mem_unit); + +_exit (0 ); +}Signature · 1 line
1.8.2.2
Quoted 4 lines · Josh Triplett
Quoted 2 lines · Tim Bird
tools/testing/selftests/size/Makefile new file +0
-0[...]
Quoted 8 lines
tools/testing/selftests/size/Makefile line 16
Quoted 6 lines
tools/testing/selftests/size/Makefile new file +6
-0+LIBGCC=$(shell $(CC) -print-libgcc-file-name) + +get_size: get_size.c + $(CC) --static -ffreestanding -nostartfiles \ + -Wl,--entry=_start get_size.c $(LIBGCC) \ + -o get_sizeYou don't need -Wl,--entry=_start; that's the default.
OK - it works without this. Thanks.
Quoted 5 lines
You shouldn't need to manually find libgcc, either; the compiler should do that for you. What goes wrong if you don't include that? If you're trying to link libgcc statically, try -static-libgcc.
Also, static is normally spelled -static, not --static.
Hmm. Not sure where I got --static from, but it worked. But if -static is the norm I'm fine changing to that.
Upon experimentation, I don't need the explicit libgcc or for that matter the -static-libgcc either.
Quoted 7 lines
Quoted 2 lines
tools/testing/selftests/size/get_size.c new file +0
-0[...]
Quoted 1 line
+int
print(const char *s)This function, and all the others apart from
_start, should be declared static.
OK will do.
Quoted 3 lines
Quoted 1 line
+void
num_to_str(unsigned long num, char *s)Likewise, static.
OK
Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/size/get_size.c new file +15
-0+{ +unsigned long long temp, div; +int started; + + temp = num; + div =1000000000000000000LL ; + started =0 ; +while (div) { +if (temp/div || started) { + *s++ = (unsigned char )(temp/div +'0' ); + started =1 ; + } + temp -= (temp/div)*div; + div /=10 ; + }
Quoted 8 lines
tools/testing/selftests/size/get_size.c
Quoted 2 lines
tools/testing/selftests/size/get_size.c new file +2
-0+ *s =0 ; +}You'd probably end up with significantly smaller code (and no divisions, and thus no corner cases on architectures that need a special function to do unsigned long long division) if you print in hex. You could also drop the "no leading zeros" logic, and just *always* print a 64-bit value as 16 hex digits.
I'd like to keep base-10 output. As far as size is concerned, the code is now at well under 2 pages (8k), which is much smaller than any actually useful program. The only noticeable change in size would be if I got it under 4096, but I don't want to sacrifice too many features to get there (as that's still only 1 page difference in memory usage.)
BTW - using sstrip, I can get it to 4096 already, as is.
Unfortunately, 'sstrip -z', which gets it to 2061, makes it not work. I need to check out what the problem is there. Also, the ELF file still has an unneeded note section. I think easier reductions, without sacrificing functionality, are available by tweaking the ELF header (ie fixing sstrip, for those that want to use it).
Quoted 3 lines
Quoted 1 line
+int
print_num(unsigned long num)Likewise, static.
OK
Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/size/get_size.c new file +18
-0+{ +char num_buf[30 ]; + +num_to_str (num, num_buf); +return int print_k_value (const char *s,unsigned long num,unsigned long units) +{ +unsigned long long temp; +int ccode; + +1024 ; + num = temp; + ccode =print_num (num);
Quoted 8 lines
tools/testing/selftests/size/get_size.c line 81
Quoted 3 lines
tools/testing/selftests/size/get_size.c new file +3
-0+"\n" ); +return ccode; +}I'd suggest dropping this entirely, and just always printing the exact values returned by sysinfo. Drop the multiply, too, and just print info.mem_unit as well. It's easy to post-process the value in a more capable environment.
I'd prefer to keep the output easily human-readable.
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/size/get_size.c new file +8
-0+/* this program has no main(), as startup libraries are not used */ +void _start (void ) +{ +int ccode; +struct sysinfo info; +unsigned long used; + +"Testing system size.\n" );
Quoted 8 lines
tools/testing/selftests/size/get_size.c line 95
Quoted 5 lines
tools/testing/selftests/size/get_size.c new file +5
-0+"1..1\n" ); + + ccode =sysinfo (&info); +if (ccode <0 ) { +"not ok 1 get size runtime size\n" );Shouldn't the "not ok" here and the "ok" below have the same test description?
Yes. Thanks. Good catch.
Quoted 10 lines
Quoted 10 lines
tools/testing/selftests/size/get_size.c new file +10
-0+"# could not get sysinfo\n" ); +_exit (ccode); + } +/* ignore cache complexities for now */ + used = info.totalram - info.freeram - info.bufferram; +print_k_value ("ok 1 get runtime memory use # size = " , used, + info.mem_unit); + +"# System runtime memory report (units in Kilobytes):\n" ); +print_k_value ("# Total: " , info.totalram, info.mem_unit);
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/size/get_size.c new file +6
-0+print_k_value ("# Free: " , info.freeram, info.mem_unit); +print_k_value ("# Buffer: " , info.bufferram, info.mem_unit); +print_k_value ("# In use: " , used, info.mem_unit); + +_exit (0 ); +}Signature · 1 line
1.8.2.2
OK - look for a new version shortly (maybe not today, though).
-- Tim