selftest: size: Add size test for Linux kernel
PATCH 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.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/size/Makefile | 21 +++++++ tools/testing/selftests/size/get_size.c | 105 ++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) 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/Makefile new file +21 -0
@@ -0,0 +1,21 @@ +#ifndef CC + CC = $(CROSS_COMPILE)gcc +#endif + +#ifndef STRIP + STRIP = $(CROSS_COMPILE)strip +#endif + +all: get_size + +get_size: get_size.c + $(CC) --static -ffreestanding -nostartfiles \ + -Wl,--entry=main get_size.c -o get_size \ + `cc -print-libgcc-file-name` + $(STRIP) -s get_size + +run_tests: all + ./get_size + +clean: + $(RM) get_size
tools/testing/selftests/size/get_size.c new file +105 -0
105 lines not on this page · raw message
Signature · 1 line
1.8.2.2
PATCH v2 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 v1:
- use more correct Copyright string in get_size.c
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/size/Makefile | 21 +++++++ tools/testing/selftests/size/get_size.c | 105 ++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) 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/Makefile new file +21 -0
@@ -0,0 +1,21 @@ +#ifndef CC + CC = $(CROSS_COMPILE)gcc +#endif + +#ifndef STRIP + STRIP = $(CROSS_COMPILE)strip +#endif + +all: get_size + +get_size: get_size.c + $(CC) --static -ffreestanding -nostartfiles \ + -Wl,--entry=main get_size.c -o get_size \ + `cc -print-libgcc-file-name` + $(STRIP) -s get_size + +run_tests: all + ./get_size + +clean: + $(RM) get_size
tools/testing/selftests/size/get_size.c new file +105 -0
105 lines not on this page · raw message
Signature · 1 line
1.8.2.2
Quoted 14 lines · Tim Bird
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 myx86_64system, this results in a statically linked binary of less than 5K.Changes from v1:
- use more correct Copyright string inget_size.c
Quoted 8 lines
Signed-off-by Tim Bird <tim.bird-/[email protected]>--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/size/Makefile | 21 +++++++ tools/testing/selftests/size/get_size.c | 105 ++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 tools/testing/selftests/size/Makefile create mode 100644 tools/testing/selftests/size/get_size.c
Tim,
The test looks good, but you are missing .gitignore file. Please add a .gitignore for the binary that gets generated to avoid git status including the binary it in its output.
thanks,
-- Shuah
Signature · 4 lines
Shuah Khan Sr. Linux Kernel Developer Samsung Research America (Silicon Valley) [email protected] | (970) 217-8978
Quoted 24 lines · Shuah Khan
Quoted 14 lines · Tim Bird
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 myx86_64system, this results in a statically linked binary of less than 5K.Changes from v1:
- use more correct Copyright string inget_size.cQuoted 8 lines
Signed-off-by Tim Bird <tim.bird-/[email protected]>--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/size/Makefile | 21 +++++++ tools/testing/selftests/size/get_size.c | 105 ++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 tools/testing/selftests/size/Makefile create mode 100644 tools/testing/selftests/size/get_size.c
Quoted 5 lines
Tim,
The test looks good, but you are missing .gitignore file. Please add a .gitignore for the binary that gets generated to avoid git status including the binary it in its output.
Will do.
I'll send v3 today.
Thanks,
-- Tim
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 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 | 21 +++++++ tools/testing/selftests/size/get_size.c | 105 ++++++++++++++++++++++++++++++++ 4 files changed, 128 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 +21 -0
@@ -0,0 +1,21 @@ +#ifndef CC + CC = $(CROSS_COMPILE)gcc +#endif + +#ifndef STRIP + STRIP = $(CROSS_COMPILE)strip +#endif + +all: get_size + +get_size: get_size.c + $(CC) --static -ffreestanding -nostartfiles \ + -Wl,--entry=main get_size.c -o get_size \ + `cc -print-libgcc-file-name` + $(STRIP) -s get_size + +run_tests: all + ./get_size + +clean: + $(RM) get_size
tools/testing/selftests/size/get_size.c new file +105 -0
105 lines not on this page · raw message
Signature · 1 line
1.8.2.2
Quoted 11 lines · Tim Bird
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 myx86_64system, this results in a statically linked binary of less than 5K.
This following version information shouldn't be part of changelog.
Quoted 9 lines
Changes from v2:
- add return values to print routines
- add .gitignore fileChanges from v1:
- use more correct Copyright string inget_size.cSigned-off-by Tim Bird <tim.bird-/[email protected]>---
Goes here.
Quoted 135 lines
tools/testing/selftests/Makefile | 1 + tools/testing/selftests/size/.gitignore | 1 + tools/testing/selftests/size/Makefile | 21 +++++++ tools/testing/selftests/size/get_size.c | 105 ++++++++++++++++++++++++++++++++ 4 files changed, 128 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.ctools/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-hotplugtools/testing/selftests/size/.gitignore new file +1
-0@@ -0,0 +1 @@ +get_sizetools/testing/selftests/size/Makefile new file +21
-0@@ -0,0 +1,21 @@ +#ifndef CC + CC = $(CROSS_COMPILE)gcc +#endif + +#ifndef STRIP + STRIP = $(CROSS_COMPILE)strip +#endif + +all: get_size + +get_size: get_size.c + $(CC) --static -ffreestanding -nostartfiles \ + -Wl,--entry=main get_size.c -o get_size \ + `cc -print-libgcc-file-name` + $(STRIP) -s get_size + +run_tests: all + ./get_size + +clean: + $(RM) get_sizetools/testing/selftests/size/get_size.c new file +74
-0@@ -0,0 +1,105 @@ +/* +* Copyright 2014 Sony +* +* Licensed under the terms of the GNU GPL License version 2 +* +* Selftest for runtime system size +* +* Prints the amount of RAM that the currently running system is using. +* +* This program tries to be as small as possible itself, to +* avoid perturbing the system memory utilization with its +* own execution. It also attempts to have as few dependencies +* on kernel features as possible. +* +* It should be statically linked, with startup libs avoided. +* It uses no library calls, and only the following 3 syscalls: +* sysinfo(), write(), and _exit() +* +* For output, it avoids printf (which in some C libraries +* has large external dependencies) by implementing its own +* strlen(), number output and print() routines. +*/ + +#include <sys/sysinfo.h> +#include <unistd.h> + +#define STDOUT_FILENO 1 + +int my_strlen (const char *s) +{ +int len =0 ; + +while (*s++) + len++; +return len; +} + +/* +* num_to_str - put digits from num into *s, left to right +* do this by dividing the number by powers of 10 +* the tricky part is to omit leading zeros +* don't print zeros until we've started printing any numbers at all +*/ +void num_to_str (unsigned long num,char *s) +{ +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 ; + } + *s =0 ; +} + +int print_num (unsigned long num) +{ +char num_buf[30 ]; + +num_to_str (num, num_buf); +return write (STDOUT_FILENO , num_buf,my_strlen (num_buf)); +} + +int char *s) +{ +return write (STDOUT_FILENO , s,my_strlen (s)); +}
tools/testing/selftests/size/get_size.c line 84
Quoted 10 lines
tools/testing/selftests/size/get_size.c new file +10
-0+ +void main (int argc,char **argv) +{ +int ccode; +unsigned long used; +struct sysinfo info; +unsigned long long temp; + +"Testing system size.\n" ); +"1..1\n" );
I ran this test and the reporting could be improved. Could you change the above to say
System RAM in use instead of system size. Also can you also print total RAM and other information you already have:
Maybe something along the lines of:
System RAM report (units Kilobytes):
Total:
Free:
Bufferram:
In use:
I would remove the print("1..1\n");
Quoted 8 lines
tools/testing/selftests/size/get_size.c new file +8
-0+ + ccode =sysinfo (&info); +if (ccode <0 ) { +"not ok 1 get size runtime size\n" ); +"# could not get sysinfo\n" ); +_exit (ccode); + } +
tools/testing/selftests/size/get_size.c line 102
Quoted 10 lines
tools/testing/selftests/size/get_size.c new file +10
-0+/* ignore cache complexities for now */ + temp = info.totalram - info.freeram - info.bufferram; + temp = temp * info.mem_unit; + temp = temp /1024 ; + + used = temp; + +"ok 1 get runtime size # size = " ); +print_num (used); +" K\n" );
Please see above. You can get rid of print(" K\n"); at the end.
Sorry for not giving this feedback earlier. The not so clear reporting aspect stood out for me after running the test.
thanks,
-- Shuah
Signature · 4 lines
Shuah Khan Sr. Linux Kernel Developer Samsung Research America (Silicon Valley) [email protected] | (970) 217-8978
Quoted 19 lines · Shuah Khan
Quoted 11 lines · Tim Bird
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 myx86_64system, this results in a statically linked binary of less than 5K.This following version information shouldn't be part of changelog.
Quoted 4 lines
Changes from v2:
- add return values to print routines
- add .gitignore file
Quoted 7 lines
Quoted 5 lines
Changes from v1:
- use more correct Copyright string inget_size.cSigned-off-by Tim Bird <tim.bird-/[email protected]>---
Goes here.
OK - I'll change this.
Quoted 150 lines
Quoted 136 lines
tools/testing/selftests/Makefile | 1 + tools/testing/selftests/size/.gitignore | 1 + tools/testing/selftests/size/Makefile | 21 +++++++ tools/testing/selftests/size/get_size.c | 105 ++++++++++++++++++++++++++++++++ 4 files changed, 128 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.ctools/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-hotplugtools/testing/selftests/size/.gitignore new file +1
-0@@ -0,0 +1 @@ +get_sizetools/testing/selftests/size/Makefile new file +21
-0@@ -0,0 +1,21 @@ +#ifndef CC + CC = $(CROSS_COMPILE)gcc +#endif + +#ifndef STRIP + STRIP = $(CROSS_COMPILE)strip +#endif + +all: get_size + +get_size: get_size.c + $(CC) --static -ffreestanding -nostartfiles \ + -Wl,--entry=main get_size.c -o get_size \ + `cc -print-libgcc-file-name` + $(STRIP) -s get_size + +run_tests: all + ./get_size + +clean: + $(RM) get_sizetools/testing/selftests/size/get_size.c new file +74
-0@@ -0,0 +1,105 @@ +/* +* Copyright 2014 Sony +* +* Licensed under the terms of the GNU GPL License version 2 +* +* Selftest for runtime system size +* +* Prints the amount of RAM that the currently running system is using. +* +* This program tries to be as small as possible itself, to +* avoid perturbing the system memory utilization with its +* own execution. It also attempts to have as few dependencies +* on kernel features as possible. +* +* It should be statically linked, with startup libs avoided. +* It uses no library calls, and only the following 3 syscalls: +* sysinfo(), write(), and _exit() +* +* For output, it avoids printf (which in some C libraries +* has large external dependencies) by implementing its own +* strlen(), number output and print() routines. +*/ + +#include <sys/sysinfo.h> +#include <unistd.h> + +#define STDOUT_FILENO 1 + +int my_strlen (const char *s) +{ +int len =0 ; + +while (*s++) + len++; +return len; +} + +/* +* num_to_str - put digits from num into *s, left to right +* do this by dividing the number by powers of 10 +* the tricky part is to omit leading zeros +* don't print zeros until we've started printing any numbers at all +*/ +void num_to_str (unsigned long num,char *s) +{ +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 ; + } + *s =0 ; +} + +int print_num (unsigned long num) +{ +char num_buf[30 ]; + +num_to_str (num, num_buf); +return write (STDOUT_FILENO , num_buf,my_strlen (num_buf)); +} + +int char *s) +{ +return write (STDOUT_FILENO , s,my_strlen (s)); +}tools/testing/selftests/size/get_size.c line 84
Quoted 10 lines
tools/testing/selftests/size/get_size.c new file +10
-0+ +void main (int argc,char **argv) +{ +int ccode; +unsigned long used; +struct sysinfo info; +unsigned long long temp; + +"Testing system size.\n" ); +"1..1\n" );I ran this test and the reporting could be improved. Could you change the above to say
Quoted 1 line
System RAM in use instead of system size.
I'll have to think about this. I'm not sure what the correct phrasing would be for XIP systems (for which part of the kernel resides in flash, not RAM, maybe "memory"?)
Quoted 3 lines
Also can you also
print total RAM and other information you already have:
I can print it as part of total output, but for regression testing and automation I want the tool to have a single number result as output (well, one number relating to runtime size, and I'm considering adding one number relating to compile-time size.)
Quoted 9 lines
Maybe something along the lines of:
System RAM report (units Kilobytes):
Total:
Free:
Bufferram:
In use:I would remove the
print("1..1\n");
This is part of the TAP output format.
Is there any (other) output format being standardized for selftest programs?
TAP allows me to output more human-readable information to go along with the "official" result, but I'd like to keep the official result in TAP format so it can be handled by automated tools. Maybe it would be best to output both.
I'll try to make a counter-proposal tomorrow that we can discus.
Quoted 21 lines
Quoted 8 lines
tools/testing/selftests/size/get_size.c new file +8
-0+ + ccode =sysinfo (&info); +if (ccode <0 ) { +"not ok 1 get size runtime size\n" ); +"# could not get sysinfo\n" ); +_exit (ccode); + } +tools/testing/selftests/size/get_size.c line 102
Quoted 10 lines
tools/testing/selftests/size/get_size.c new file +10
-0+/* ignore cache complexities for now */ + temp = info.totalram - info.freeram - info.bufferram; + temp = temp * info.mem_unit; + temp = temp /1024 ; + + used = temp; + +"ok 1 get runtime size # size = " ); +print_num (used); +" K\n" );Please see above. You can get rid of
print(" K\n"); at the end.
Quoted 2 lines
Sorry for not giving this feedback earlier. The not so clear reporting aspect stood out for me after running the test.
No problem. Thanks for looking at it.
I have some additional changes, based on feedback from Josh Triplett, so look for a v4 tomorrow.
-- Tim