Skip to contentSkip to conversation

speedup kallsyms_lookup

PATCH In review Daniel Ritz 8 participants 13 messages Jan 10, 2003 – Jan 11, 2003 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 speedup kallsyms_lookup

[please cc...you know why]

a patch to speed up the kallsyms_lookup() function while still doing compression.
- make 4 sections: addresses, lens, stem, strings
- only strncpy() when needed
- no strlen() at all (only in the script)
- save space we lose for len table by not making strings zero terminated

not yet included is hugh's patch to sort the symbols by name, i didn't look at it yet. but at least it should not be off-by-one...i din't test it that much, but it looks quiet ok..

not yet sent to linus. comments first. against 2.5.55.

beep
-daniel

scripts/kallsyms.c +61 -28
@@ -26,7 +26,8 @@
 static void
 usage(void)
 {
-	fprintf(stderr, "Usage: kallsyms < in.map > out.S\n");
+	fprintf(stderr, "Usage: kallsyms out.S < in.map\n");
+	fprintf(stderr, "       Generates out.S.[123], cat them together.\n");
 	exit(1);
 }
 
@@ -89,26 +90,42 @@
 }
 
 static void
-write_src(void)
+write_src(char *name)
 {
 	unsigned long long last_addr;
 	int i, valid = 0;
 	char *prev;
 
-	printf("#include <asm/types.h>\n");
-	printf("#if BITS_PER_LONG == 64\n");
-	printf("#define PTR .quad\n");
-	printf("#define ALGN .align 8\n");
-	printf("#else\n");
-	printf("#define PTR .long\n");
-	printf("#define ALGN .align 4\n");
-	printf("#endif\n");
+	char *tname;
+	int len;
+	FILE *f1, *f2, *f3;
 
-	printf(".data\n");
+	len = strlen(name);
+	tname = malloc(len + 3);
+	strcpy(tname, name);
 
-	printf(".globl kallsyms_addresses\n");
-	printf("\tALGN\n");
-	printf("kallsyms_addresses:\n");
+	strcpy(tname + len, ".1");
+	f1 = fopen(tname, "w");
+	strcpy(tname + len, ".2");
+	f2 = fopen(tname, "w");
+	strcpy(tname + len, ".3");
+	f3 = fopen(tname, "w");
+	free(tname);
+
+	fprintf(f1, "#include <asm/types.h>\n");
+	fprintf(f1, "#if BITS_PER_LONG == 64\n");
+	fprintf(f1, "#define PTR .quad\n");
+	fprintf(f1, "#define ALGN .align 8\n");
+	fprintf(f1, "#else\n");
+	fprintf(f1, "#define PTR .long\n");
+	fprintf(f1, "#define ALGN .align 4\n");
+	fprintf(f1, "#endif\n");
+
+	fprintf(f1, ".data\n");
+
+	fprintf(f1, ".globl kallsyms_addresses\n");
+	fprintf(f1, "\tALGN\n");
+	fprintf(f1, "kallsyms_addresses:\n");
 	for (i = 0, last_addr = 0; i < cnt; i++) {
 		if (!symbol_valid(&table[i]))
 			continue;
@@ -116,21 +133,29 @@
 		if (table[i].addr == last_addr)
 			continue;
 
-		printf("\tPTR\t%#llx\n", table[i].addr);
+		fprintf(f1, "\tPTR\t%#llx\n", table[i].addr);
 		valid++;
 		last_addr = table[i].addr;
 	}
-	printf("\n");
+	fprintf(f1, "\n");
 
-	printf(".globl kallsyms_num_syms\n");
-	printf("\tALGN\n");
-	printf("kallsyms_num_syms:\n");
-	printf("\tPTR\t%d\n", valid);
-	printf("\n");
+	fprintf(f1, ".globl kallsyms_num_syms\n");
+	fprintf(f1, "\tALGN\n");
+	fprintf(f1, "kallsyms_num_syms:\n");
+	fprintf(f1, "\tPTR\t%d\n", valid);
+	fprintf(f1, "\n");
 
-	printf(".globl kallsyms_names\n");
-	printf("\tALGN\n");
-	printf("kallsyms_names:\n");
+	fprintf(f1, ".globl kallsyms_lens\n");
+	fprintf(f1, "\tALGN\n");
+	fprintf(f1, "kallsyms_lens:\n");
+
+	fprintf(f2, ".globl kallsyms_stem\n");
+	fprintf(f2, "\tALGN\n");
+	fprintf(f2, "kallsyms_stem:\n");
+
+	fprintf(f3, ".globl kallsyms_names\n");
+	fprintf(f3, "\tALGN\n");
+	fprintf(f3, "kallsyms_names:\n");
 	prev = ""; 
 	for (i = 0, last_addr = 0; i < cnt; i++) {
 		int k;
@@ -144,21 +169,29 @@
 		for (k = 0; table[i].sym[k] && table[i].sym[k] == prev[k]; ++k)
 			; 
 
-		printf("\t.byte 0x%02x ; .asciz\t\"%s\"\n", k, table[i].sym + k);
+		fprintf(f1, "\t.byte 0x%02x\n", strlen(table[i].sym) - k);
+		fprintf(f2, "\t.byte 0x%02x\n", k);
+		fprintf(f3, "\t.ascii\t\"%s\"\n", table[i].sym + k);
 		last_addr = table[i].addr;
 		prev = table[i].sym;
 	}
-	printf("\n");
+	fprintf(f1, "\n");
+	fprintf(f2, "\n");
+	fprintf(f3, "\n");
+
+	fclose(f1);
+	fclose(f2);
+	fclose(f3);
 }
 
 int
 main(int argc, char **argv)
 {
-	if (argc != 1)
+	if (argc != 2)
 		usage();
 
 	read_map(stdin);
-	write_src();
+	write_src(argv[1]);
 
 	return 0;
 }
Makefile +1 -1
@@ -355,7 +355,7 @@
 kallsyms.o := .tmp_kallsyms2.o
 
 quiet_cmd_kallsyms = KSYM    $@
-cmd_kallsyms = $(NM) -n $< | scripts/kallsyms > $@
+cmd_kallsyms = $(NM) -n $< | scripts/kallsyms $@; cat [email protected] [email protected] [email protected] > $@
 
 .tmp_kallsyms1.o .tmp_kallsyms2.o: %.o: %.S scripts FORCE
 	$(call if_changed_dep,as_o_S)
kernel/kallsyms.c +26 -5
@@ -14,6 +14,8 @@
 /* These will be re-linked against their real values during the second link stage */
 extern unsigned long kallsyms_addresses[1] __attribute__((weak, alias("kallsyms_dummy")));
 extern unsigned long kallsyms_num_syms __attribute__((weak, alias("kallsyms_dummy")));
+extern char kallsyms_lens[1] __attribute__((weak, alias("kallsyms_dummy")));
+extern char kallsyms_stem[1] __attribute__((weak, alias("kallsyms_dummy")));
 extern char kallsyms_names[1] __attribute__((weak, alias("kallsyms_dummy")));
 
 /* Defined by the linker script. */
@@ -26,6 +28,7 @@
 			    char **modname, char *namebuf)
 {
 	unsigned long i, best = 0;
+	unsigned long sym_offs = 0, stem = 0, stem_tmp, len;
 
 	/* This kernel should never had been booted. */
 	if ((void *)kallsyms_addresses == &kallsyms_dummy)
@@ -45,13 +48,31 @@
 				best = i;
 		}
 
-		/* Grab name */
-		for (i = 0; i < best; i++) { 
-			unsigned prefix = *name++;
-			strncpy(namebuf + prefix, name, 127 - prefix);
-			name += strlen(name) + 1;
+		/* find name offset */
+		for (i = 0; i < best; i++)
+			sym_offs += (unsigned long) kallsyms_lens[i];
+
+		/* go for the end of the name */
+		name = kallsyms_names + sym_offs;
+		stem = (unsigned long) kallsyms_stem[best];
+		len  = (unsigned long) kallsyms_lens[best];
+		strncpy(namebuf + stem, name, len);
+		namebuf[stem + len] = '\0';
+
+		/* go back, find the beginning */
+		if (stem) {
+			i = best;
+			do {
+				i--;
+				name -= kallsyms_lens[i];
+				stem_tmp = (unsigned long) kallsyms_stem[i];
+
+				if (stem_tmp < stem)
+					strncpy(namebuf + stem_tmp, name, stem - stem_tmp);
+			} while (stem_tmp);
 		}
 
+
 		/* Base symbol size on next symbol. */
 		if (best + 1 < kallsyms_num_syms)
 			symbol_end = kallsyms_addresses[best + 1];
#1 lore raw
Quoted 8 lines · Daniel Ritz

[please cc...you know why]

a patch to speed up the kallsyms_lookup() function while still doing compression.
- make 4 sections: addresses, lens, stem, strings
- only strncpy() when needed
- no strlen() at all (only in the script)
- save space we lose for len table by not making strings zero terminated

First impression is that it has good ideas, but seems inelegant (always easy to make that judgment on others' code! ignore me) and misses the main point.

In earlier mail, Andi highlighted the performance criticality of top reading /proc/<pid>/wchan. I think we have to decide which way to jump on that: either withdraw that functionality as too expensive, and minimize the table size and code stupidity (all those strncpy's of nearly 127! include/asm-i386/string.h strncpy seems in the wrong there); or speed kallsyms_lookup as much as possible (binary chop or whatever algorithm to locate symbol from address). The current linear search through 6000(?) addresses is not nice, but of course the strncpy is making it much worse.

I didn't reply to that part of Andi's mail, not because I thought it irrelevant, quite the reverse; but because I didn't have an opinion which way to go, and hoped someone else would chime in. I don't see how to proceed without deciding that. CC'd rml since I believe he fathered /proc/<pid>/wchan. Now, I'm inclined to say that anyone worried about memory occupancy just shouldn't switch CONFIG_KALLSYMS on, so it's speed we should aim for here.

If maximizing speed, then obviously the values should be sorted by value (as now, unlike in my patch), and maybe we forget all about stem compression? If minimizing memory, then a combination of your patch and mine?

I hope I can leave this discussion to others: I just wanted to get my symbols printing out right, and noticed the current stem compression unnecessarily weak there; but I'm no expert on suitable algorithms.

Hugh

#2 lore raw
Quoted 3 lines · Hugh Dickins

I hope I can leave this discussion to others: I just wanted to get my symbols printing out right, and noticed the current stem compression unnecessarily weak there; but I'm no expert on suitable algorithms.

I can help some here but probably no more than you (in fact, you've spotted far more [> 0] issues with the current code than I).

Basically, if you want fast string lookup of compressed stuff I can sit down with whiteboard etc. and fiddle around, but it sounds like from f's comments that this isn't really wanted.

So the end-result of the discussion is, "What should really happen here?" and "What, if anything, do you want me to do?"

Bill

#3 lore raw
Quoted 3 lines

So the end-result of the discussion is, "What should really happen here?" and "What, if anything, do you want me to do?"

IMHO best would be to get rid of /proc/*/wchan and keep the kallsyms lookup slow, simple and stupid.

-Andi

#4 lore raw
Quoted 2 lines · At some point in the past, I wrote:
Quoted 2 lines

So the end-result of the discussion is, "What should really happen here?" and "What, if anything, do you want me to do?"

Quoted 2 lines · Andi Kleen

IMHO best would be to get rid of /proc/*/wchan and keep the kallsyms lookup slow, simple and stupid.

Slow, simple, and stupid == "wli, get the Hell out". I'm gone.

Thanks,
Bill

#5 lore raw
Quoted 9 lines · William Lee Irwin III
Quoted 2 lines · At some point in the past, I wrote:
Quoted 2 lines

So the end-result of the discussion is, "What should really happen here?" and "What, if anything, do you want me to do?"

Quoted 2 lines · Andi Kleen

IMHO best would be to get rid of /proc/*/wchan and keep the kallsyms lookup slow, simple and stupid.

Slow, simple, and stupid == "wli, get the Hell out". I'm gone.

Indeed! I think that was Andi volunteering :-}
But we should let rml defend his wchan.

Hugh

#6 lore raw
Quoted 2 lines · Hugh Dickins

Indeed! I think that was Andi volunteering :-}
But we should let rml defend his wchan.

Well, of course I want to keep it - but I am biased :)

I think its a simple export that gives us a neat feature. Additionally, from the procps perspective, it saves us from having to parse System.map for each process. In fact, it means we do not need a System.map at all for any procps functionality.

I guess Linus at least mildly liked it too, since he merged it.

But if its such a performance crippling item perhaps it does need to be removed (or somehow restricted).

I do agree that, if possible, wchan should be kept simple... so, is everyone else for the removal of /proc/pid/wchan ? :-(

Robert Love

#7 lore raw

hmm...i don't see why i want wchan. tell me.
but you're right, for some reason linus merged it. so i think (if it's kept) we should restrict it a bit and make the kallsyms_lookup a bit faster. not a super fast complex algorithm, but at least not that braindead as it currently is...

btw. does my ugly early-in-the-moring-hack work right?

Quoted 22 lines · Robert Love
Quoted 2 lines · Hugh Dickins

Indeed! I think that was Andi volunteering :-}
But we should let rml defend his wchan.

Well, of course I want to keep it - but I am biased :)

I think its a simple export that gives us a neat feature. Additionally, from the procps perspective, it saves us from having to parse System.map for each process. In fact, it means we do not need a System.map at all for any procps functionality.

I guess Linus at least mildly liked it too, since he merged it.

But if its such a performance crippling item perhaps it does need to be removed (or somehow restricted).

I do agree that, if possible, wchan should be kept simple... so, is everyone else for the removal of /proc/pid/wchan ? :-(

Robert Love

#8 lore raw
In reply to William Lee Irwin III
Quoted 9 lines · William Lee Irwin III
Quoted 2 lines · At some point in the past, I wrote:
Quoted 2 lines

So the end-result of the discussion is, "What should really happen here?" and "What, if anything, do you want me to do?"

Quoted 2 lines · Andi Kleen

IMHO best would be to get rid of /proc/*/wchan and keep the kallsyms lookup slow, simple and stupid.

Slow, simple, and stupid == "wli, get the Hell out". I'm gone.

I don't really see a need for blazing fast lookup here, as long as it gets done within a reasonable timeframe for common cases. Unless of course your optimised version also saves space code wise.

Zwane

Signature · 1 line
function.linuxpower.ca
#9 lore raw
In reply to Andi Kleen
Quoted 6 lines · Andi Kleen
Quoted 3 lines

So the end-result of the discussion is, "What should really happen here?" and "What, if anything, do you want me to do?"

IMHO best would be to get rid of /proc/*/wchan and keep the kallsyms lookup slow, simple and stupid.

And replace the current /proc/*/wchan functionality with what? (Note that saying "read the System.map file from userspace" doesn't *quite* work, as I may be running a kernel that doesn't have System.map installed someplace easily findable. At the moment my /boot partition has 6 assorted vmlinuz-\(*\) files, only 4 of which have System.map-\1 files matching).

Signature · 3 lines
				Valdis Kletnieks
				Computer Systems Senior Engineer
				Virginia Tech
#10 lore raw
Quoted 9 lines · On Fri, Jan 10, 2003 at 12:13:48PM -0500, [email protected] wrote:
Quoted 6 lines · Andi Kleen
Quoted 3 lines

So the end-result of the discussion is, "What should really happen here?" and "What, if anything, do you want me to do?"

IMHO best would be to get rid of /proc/*/wchan and keep the kallsyms lookup slow, simple and stupid.

And replace the current /proc/*/wchan functionality with what?

Ctrl-Rollen (or whatever the key is called on your keyboard) on the console, like in all previous linux releases.

Note /proc/*/wchan is not in 2.4.

Also you still have WCHAN in ps, just not a full backtrace.

-Andi

#11 lore raw

On Fri, 10 Jan 2003, Andi Kleen wrote:

| On Fri, Jan 10, 2003 at 12:13:48PM -0500, [email protected] wrote:
| > On Fri, 10 Jan 2003 17:12:12 +0100, Andi Kleen <[email protected]>  said:
| > >
| > > > So the end-result of the discussion is, "What should really happen here?"
| > > > and "What, if anything, do you want me to do?"
| > >
| > > IMHO best would be to get rid of /proc/*/wchan and keep the kallsyms
| > > lookup slow, simple and stupid.
| >
| > And replace the current /proc/*/wchan functionality with what?
|
| Ctrl-Rollen (or whatever the key is called on your keyboard) on the console,
| like in all previous linux releases.

Ctrl-ScrollLock on mine if I'm following you correctly. Same as Sysrq-P if sysrq is enabled, except that the former is always available.

Are there other similar functions that are available without Sysrq enabled? If so, where can I find info about them? (other than drivers/char/keyboard.c :)

| Note /proc/*/wchan is not in 2.4.
|
| Also you still have WCHAN in ps, just not a full backtrace.
Signature · 1 line
~Randy
#12 lore raw
Quoted 3 lines

Ctrl-ScrollLock on mine if I'm following you correctly. Same as Sysrq-P if sysrq is enabled, except that the former is always available.

Try Ctrl-,Alt/AltGr-,Shift- with the same key.

Quoted 3 lines

Are there other similar functions that are available without Sysrq enabled? If so, where can I find info about them?

Quite a lot.

In your keymap. e.g. on a SuSE system.

getkeycodes will output a map of the key codes.

% less /usr/share/kbd/keymaps/i386/include/linux-keys-bare.inc

alt keycode 59 = Console_1
alt keycode 60 = Console_2
...
control alt keycode 59 = Console_1 control alt keycode 60 = Console_2
...
alt keycode 71 = Ascii_7
alt keycode 72 = Ascii_8
...
alt keycode 103 = KeyboardSignal alt keycode 105 = Decr_Console alt keycode 106 = Incr_Console
...
shift keycode 104 = Scroll_Backward shift keycode 109 = Scroll_Forward control alt keycode 111 = Boot
...
keycode 84 = Last_Console # Alt+SysRq/PrintScrn
#keycode 99 = Control_backslash # SysRq/PrintScrn
keycode 99 = VoidSymbol
control keycode 99 = Control_backslash

plain keycode 70 = Scroll_Lock
shift keycode 70 = Show_Memory
control keycode 70 = Show_State
alt keycode 70 = Show_Registers
keycode 101 = Break # Ctrl+Break/Pause
control keycode 101 = Control_c
shift keycode 101 = Control_backslash
keycode 119 = Pause # Break/Pause

-Andi