Skip to contentSkip to conversation

selftest: size: Add size test for Linux kernel

PATCH Superseded Tim Bird 4 participants 9 messages Dec 3, 2014 – Dec 4, 2014

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Tim Bird

author
#0 lore raw

PATCH v5 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 v5:
- make most routines static
- replace strip with gcc -s
- remove explicit reference to _start
- change --static to -static
- remove explicit reference to LIBGCC
- fix test description for ok and not ok paths, for test 1

Changes from v4:
- add more human-readable output
- put libgcc reference into a variable in Makefile

Changes from v3:
- fix copyright string (again!)
- use __builtin_strlen instead of my own strlen
- replace main with _start

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   |  15 +++++
 tools/testing/selftests/size/get_size.c | 111 ++++++++++++++++++++++++++++++++
 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
index 45f145c..fa91aef 100644
@@ -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
new file mode 100644
index 0000000..189b781
@@ -0,0 +1 @@
+get_size
tools/testing/selftests/size/Makefile new file +15 -0
new file mode 100644
index 0000000..47f8e9c
@@ -0,0 +1,15 @@
+#ifndef CC
+	CC = $(CROSS_COMPILE)gcc
+#endif
+
+all: get_size
+
+get_size: get_size.c
+	$(CC) -static -ffreestanding -nostartfiles \
+		-s get_size.c -o 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
#1 lore raw
Quoted 4 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.

Sorry to only chime in at v5.

tools/testing/selftests/size/Makefile line 3

Quoted 9 lines
tools/testing/selftests/size/Makefile new file +3 -0
new file mode 100644
index 0000000..47f8e9c
@@ -0,0 +1,15 @@
+#ifndef CC
+	CC = $(CROSS_COMPILE)gcc
+#endif

I think the following is preferable:

  CC := $(CROSS_COMPILE)$(CC)

It allows optionally setting a custom CC, as well as optionally CROSS_COMPILE.

The only thing it doesn't do is choose gcc explicitly, but you shouldn't really do that anyway - unless you absolutely require gcc. Let the user choose their compiler by choosing where cc points.

cheers

#2 lore raw

Michael, Tim,

Quoted 12 lines · Michael Ellerman

tools/testing/selftests/size/Makefile line 3

Quoted 9 lines
tools/testing/selftests/size/Makefile new file +3 -0
new file mode 100644
index 0000000..47f8e9c
@@ -0,0 +1,15 @@
+#ifndef CC
+	CC = $(CROSS_COMPILE)gcc
+#endif

I think the following is preferable:

Quoted 1 line
  CC := $(CROSS_COMPILE)$(CC)

It is even more necessary that #ifndef and #endif don't exist in make. They are just comments, and therefore, ignored. Seems like Tim does too much C :-)

Best regards,

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com
author
#3 lore raw
Quoted 16 lines · Thomas Petazzoni

Michael, Tim,

Quoted 12 lines · Michael Ellerman

tools/testing/selftests/size/Makefile line 3

Quoted 9 lines
tools/testing/selftests/size/Makefile new file +3 -0
new file mode 100644
index 0000000..47f8e9c
@@ -0,0 +1,15 @@
+#ifndef CC
+	CC = $(CROSS_COMPILE)gcc
+#endif

I think the following is preferable:

Quoted 5 lines
Quoted 1 line
  CC := $(CROSS_COMPILE)$(CC)

It is even more necessary that #ifndef and #endif don't exist in make. They are just comments, and therefore, ignored. Seems like Tim does too much C :-)

OK - that's hilarious. Saying 'Oops!' would be too casual for my degree of embarrassment. :-)

Makefiles do have similar constructs. Those should have been ifeq ($(CC),)

	...

endif

This obviously got through via a failiure in testing - which is somewhat ironic.

Look for a v6 soon. (Geez, when is the merge window coming. I thought this trivial program would get in pretty easily, but no... that's never the way. Of course it helps if the submitter is not an idiot.)
-- Tim

author
#4 lore raw
In reply to Michael Ellerman
Quoted 20 lines · Michael Ellerman
Quoted 4 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.

Sorry to only chime in at v5.

tools/testing/selftests/size/Makefile line 3

Quoted 9 lines
tools/testing/selftests/size/Makefile new file +3 -0
new file mode 100644
index 0000000..47f8e9c
@@ -0,0 +1,15 @@
+#ifndef CC
+	CC = $(CROSS_COMPILE)gcc
+#endif

I think the following is preferable:

Quoted 4 lines
  CC := $(CROSS_COMPILE)$(CC)

It allows optionally setting a custom CC, as well as optionally CROSS_COMPILE.

I'm not sure I follow this.

If CC is unset, you get only the CROSS_COMPILE prefix.
If CC is set to e.g. 'gcc', then you get a nicely formatted toolchain string. But if CC already has the prefix applied, then this will result in
having it duplicated, which surely won't work correctly.

In the long run, I would hope that a higher level Makefile or environment setting will be setting the toolchain string appropriately (as well as handling build flags) which is why I wanted to use an ifndef (which Thomas correctly pointed out is just wrong).

Actually, after getting this tiny program accepted, my next task was working on a proper fix for handling cross compilation in a more generic (not case-by-case) way.

CROSS_COMPILE prefix usage looks a bit uncoordinated in the tools directory, but most tests seem to be favoring $(CROSS_COMPILE)gcc.

 $ cd tools ; mgrep CROSS
./vm/Makefile:CC = $(CROSS_COMPILE)gcc
./usb/Makefile:CC = $(CROSS_COMPILE)gcc
./testing/selftests/net/Makefile:CC = $(CROSS_COMPILE)gcc
./testing/selftests/vm/Makefile:CC = $(CROSS_COMPILE)gcc
./testing/selftests/efivarfs/Makefile:CC = $(CROSS_COMPILE)gcc
./testing/selftests/size/Makefile:	CC = $(CROSS_COMPILE)gcc
./testing/selftests/powerpc/Makefile:CC := $(CROSS_COMPILE)$(CC)
./hv/Makefile:CC = $(CROSS_COMPILE)gcc
./perf/config/feature-checks/Makefile:CC := $(CROSS_COMPILE)gcc -MD
./perf/config/feature-checks/Makefile:PKG_CONFIG := $(CROSS_COMPILE)pkg-config
./lib/api/Makefile:CC = $(CROSS_COMPILE)gcc
./lib/api/Makefile:AR = $(CROSS_COMPILE)ar
./lib/lockdep/Makefile:# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
./lib/lockdep/Makefile:$(call allow-override,CC,$(CROSS_COMPILE)gcc)
./lib/lockdep/Makefile:$(call allow-override,AR,$(CROSS_COMPILE)ar)
./lib/lockdep/Makefile:TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
./lib/traceevent/Makefile:# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
./lib/traceevent/Makefile:$(call allow-override,CC,$(CROSS_COMPILE)gcc)
./lib/traceevent/Makefile:$(call allow-override,AR,$(CROSS_COMPILE)ar)
./lib/traceevent/Makefile:TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
./cgroup/Makefile:CC = $(CROSS_COMPILE)gcc
./power/acpi/Makefile:CROSS = #/usr/i386-linux-uclibc/usr/bin/i386-uclibc-
./power/acpi/Makefile:CC = $(CROSS)gcc
./power/acpi/Makefile:LD = $(CROSS)gcc
./power/acpi/Makefile:STRIP = $(CROSS)strip
./power/cpupower/Makefile:CROSS = #/usr/i386-linux-uclibc/usr/bin/i386-uclibc-
./power/cpupower/Makefile:CC = $(CROSS)gcc
./power/cpupower/Makefile:LD = $(CROSS)gcc
./power/cpupower/Makefile:AR = $(CROSS)ar
./power/cpupower/Makefile:STRIP = $(CROSS)strip
./power/cpupower/Makefile:RANLIB = $(CROSS)ranlib
./power/cpupower/Makefile:export CROSS CC AR STRIP RANLIB CFLAGS LDFLAGS LIB_OBJS
./power/x86/turbostat/Makefile:CC		= $(CROSS_COMPILE)gcc

I agree it's desirable not to hardcode gcc, but we seem to be doing it all over the place already.
-- Tim

#5 lore raw
Quoted 36 lines · Tim Bird
Quoted 12 lines

tools/testing/selftests/size/Makefile line 3

Quoted 9 lines
tools/testing/selftests/size/Makefile new file +3 -0
new file mode 100644
index 0000000..47f8e9c
@@ -0,0 +1,15 @@
+#ifndef CC
+    CC = $(CROSS_COMPILE)gcc
+#endif

I think the following is preferable:

Quoted 4 lines
  CC := $(CROSS_COMPILE)$(CC)

It allows optionally setting a custom CC, as well as optionally CROSS_COMPILE.

I'm not sure I follow this.

If CC is unset, you get only the CROSS_COMPILE prefix.
If CC is set to e.g. 'gcc', then you get a nicely formatted toolchain string. But if CC already has the prefix applied, then this will result in
having it duplicated, which surely won't work correctly.

In the long run, I would hope that a higher level Makefile or environment setting will be setting the toolchain string appropriately (as well as handling build flags) which is why I wanted to use an ifndef (which Thomas correctly pointed out is just wrong).

Actually, after getting this tiny program accepted, my next task was working on a proper fix for handling cross compilation in a more generic (not case-by-case) way.

CROSS_COMPILE prefix usage looks a bit uncoordinated in the tools directory, but most tests seem to be favoring $(CROSS_COMPILE)gcc.

 $ cd tools ; mgrep CROSS

[...]

Quoted 2 lines

I agree it's desirable not to hardcode gcc, but we seem to be doing it all over the place already.

Seems like it's time to start integrating the tests with the regular Kbuild system, which handles cross-compilation fine...

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

author
#6 lore raw
Quoted 38 lines · Geert Uytterhoeven
Quoted 29 lines · Tim Bird
Quoted 12 lines

tools/testing/selftests/size/Makefile line 3

Quoted 9 lines
tools/testing/selftests/size/Makefile new file +3 -0
new file mode 100644
index 0000000..47f8e9c
@@ -0,0 +1,15 @@
+#ifndef CC
+    CC = $(CROSS_COMPILE)gcc
+#endif

I think the following is preferable:

Quoted 4 lines
  CC := $(CROSS_COMPILE)$(CC)

It allows optionally setting a custom CC, as well as optionally CROSS_COMPILE.

I'm not sure I follow this.

If CC is unset, you get only the CROSS_COMPILE prefix.
If CC is set to e.g. 'gcc', then you get a nicely formatted toolchain string. But if CC already has the prefix applied, then this will result in
having it duplicated, which surely won't work correctly.

In the long run, I would hope that a higher level Makefile or environment setting will be setting the toolchain string appropriately (as well as handling build flags) which is why I wanted to use an ifndef (which Thomas correctly pointed out is just wrong).

Quoted 7 lines

Actually, after getting this tiny program accepted, my next task was working on a proper fix for handling cross compilation in a more generic (not case-by-case) way.

CROSS_COMPILE prefix usage looks a bit uncoordinated in the tools directory, but most tests seem to be favoring $(CROSS_COMPILE)gcc.

 $ cd tools ; mgrep CROSS
Quoted 7 lines

[...]

Quoted 2 lines

I agree it's desirable not to hardcode gcc, but we seem to be doing it all over the place already.

Seems like it's time to start integrating the tests with the regular Kbuild system, which handles cross-compilation fine...

Where possible, yes. It would be nice to leverage CROSS_COMPILE and CFLAGS, or a portion thereof, from the Kbuild system (as well a KBUILD_OUTPUT and friends). It's on my to-do list, after getting my little C program accepted...
-- Tim

#7 lore raw
In reply to Tim Bird
Quoted 22 lines · Tim Bird
Quoted 20 lines · Michael Ellerman
Quoted 4 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.

Sorry to only chime in at v5.

tools/testing/selftests/size/Makefile line 3

Quoted 9 lines
tools/testing/selftests/size/Makefile new file +3 -0
new file mode 100644
index 0000000..47f8e9c
@@ -0,0 +1,15 @@
+#ifndef CC
+	CC = $(CROSS_COMPILE)gcc
+#endif

I think the following is preferable:

Quoted 8 lines
Quoted 4 lines
  CC := $(CROSS_COMPILE)$(CC)

It allows optionally setting a custom CC, as well as optionally CROSS_COMPILE.

I'm not sure I follow this.

If CC is unset, you get only the CROSS_COMPILE prefix.

CC is never unset. The default value is 'cc'.

Quoted 1 line

If CC is set to e.g. 'gcc', then you get a nicely formatted toolchain string.

Right.

Quoted 2 lines

But if CC already has the prefix applied, then this will result in
having it duplicated, which surely won't work correctly.

That's just PEBKAC. Don't specify CROSS_COMPILE and also a fully specified CC. Try it with the kernel Makefile and see how well it works.

Quoted 2 lines

CROSS_COMPILE prefix usage looks a bit uncoordinated in the tools directory, but most tests seem to be favoring $(CROSS_COMPILE)gcc.

That doesn't make it right :)

Quoted 2 lines
 $ cd tools ; mgrep CROSS

...

Quoted 1 line

./testing/selftests/powerpc/Makefile:CC := $(CROSS_COMPILE)$(CC)

You can run git blame on that one if you like ;)

Quoted 2 lines

I agree it's desirable not to hardcode gcc, but we seem to be doing it all over the place already.

If everyone jumped off a bridge ... :)

cheers

author
#8 lore raw
Quoted 26 lines · Michael Ellerman
Quoted 25 lines · Tim Bird
Quoted 23 lines · Michael Ellerman
Quoted 4 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.

Sorry to only chime in at v5.

tools/testing/selftests/size/Makefile line 3

Quoted 9 lines
tools/testing/selftests/size/Makefile new file +3 -0
new file mode 100644
index 0000000..47f8e9c
@@ -0,0 +1,15 @@
+#ifndef CC
+	CC = $(CROSS_COMPILE)gcc
+#endif

I think the following is preferable:

  CC := $(CROSS_COMPILE)$(CC)
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

It allows optionally setting a custom CC, as well as optionally CROSS_COMPILE.

I'm not sure I follow this.

If CC is unset, you get only the CROSS_COMPILE prefix.

CC is never unset. The default value is 'cc'.

Yeah - I found that out by experimentation yesterday. So my whole idea for configuring it based on an unset CC was misguided.

Quoted 8 lines
Quoted 2 lines

If CC is set to e.g. 'gcc', then you get a nicely formatted toolchain string.

Right.

Quoted 2 lines

But if CC already has the prefix applied, then this will result in
having it duplicated, which surely won't work correctly.

Quoted 7 lines

That's just PEBKAC. Don't specify CROSS_COMPILE and also a fully specified CC. Try it with the kernel Makefile and see how well it works.

Quoted 2 lines

CROSS_COMPILE prefix usage looks a bit uncoordinated in the tools directory, but most tests seem to be favoring $(CROSS_COMPILE)gcc.

That doesn't make it right :)

Agreed. I'd like to separate this issue from the rest of the patch. Shuah has accepted the patch as it currently is, but I'd like to start working on the best way to support cross compilation throughout the kselftest suite.

Ideally we can have CC set properly in a higher-level Makefile, and not reference CROSS_COMPILE at all in the sub-directory Makefiles. This may involve copying how CROSS_COMPILE and CFLAGS work in the rest of the kernel tree, or making some kselftest-specific modifications.

-- Tim