Skip to contentSkip to conversation

ftrace: simplify hex print

PATCH In review Thomas Gleixner 5 participants 5 messages Apr 18, 2008 – Apr 19, 2008 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ftrace: simplify hex print

simplify hex to ascii conversion with a lookup table.

---
 kernel/trace/trace.c |   23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)
kernel/trace/trace.c +5 -18
Index: linux/kernel/trace/trace.c
===================================================================
@@ -248,19 +248,18 @@ trace_seq_putmem(struct trace_seq *s, vo
 }
 
 #define HEX_CHARS 17
+static const char hex2asc[] = "0123456789abcdef";
 
 static int
 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
 {
 	unsigned char hex[HEX_CHARS];
-	unsigned char *data;
+	unsigned char *data = mem;
 	unsigned char byte;
 	int i, j;
 
 	BUG_ON(len >= HEX_CHARS);
 
-	data = mem;
-
 #ifdef __BIG_ENDIAN
 	for (i = 0, j = 0; i < len; i++) {
 #else
@@ -268,22 +267,10 @@ trace_seq_putmem_hex(struct trace_seq *s
 #endif
 		byte = data[i];
 
-		hex[j]   = byte & 0x0f;
-		if (hex[j] >= 10)
-			hex[j] += 'a' - 10;
-		else
-			hex[j] += '0';
-		j++;
-
-		hex[j] = byte >> 4;
-		if (hex[j] >= 10)
-			hex[j] += 'a' - 10;
-		else
-			hex[j] += '0';
-		j++;
+		hex[j++] = hex2asc[byte & 0x0f];
+		hex[j++] = hex2asc[byte >> 4];
 	}
-	hex[j] = ' ';
-	j++;
+	hex[j++] = ' ';
 
 	return trace_seq_putmem(s, hex, j);
 }
#1 lore raw
Quoted 1 line · Thomas Gleixner

simplify hex to ascii conversion with a lookup table.

thanks Thomas, applied.

Ingo

#2 lore raw
Quoted 5 lines · Ingo Molnar
Quoted 1 line · Thomas Gleixner

simplify hex to ascii conversion with a lookup table.

thanks Thomas, applied.

There is a hex_asc() in kernel.h

Rene.

#3 lore raw
In reply to Thomas Gleixner
Quoted 4 lines · Thomas Gleixner

simplify hex to ascii conversion with a lookup table.

+static const char hex2asc[] = "0123456789abcdef";

[...]

kernel/trace/trace.c trace_seq_putmem_hex(struct trace_seq *s · line 271

Quoted 2 lines
kernel/trace/trace.c +2 -0
+		hex[j++] = hex2asc[byte & 0x0f];
+		hex[j++] = hex2asc[byte >> 4];

we have hex_asc() in include/linux/kernel.h already.