[PATCH bpf-next 2/5] samples/bpf: Dynamically allocate structure 'syms'

2018-04-18 Thread Leo Yan
Structure 'syms' is used to store kernel symbol info by reading proc fs
node '/proc/kallsyms', this structure is declared with 30 entries
and static linked into bss section.  For most case the kernel symbols
has less than 30 entries, so it's safe to define so large array, but
the side effect is bss section is big introduced by this structure and
it isn't flexible.

To fix this, this patch dynamically allocates memory for structure
'syms' based on parsing '/proc/kallsyms' line number at the runtime,
which can save elf file required memory significantly.

Before:
   textdata bss dec hex filename
  188411172 5199776 5219789  4fa5cd samples/bpf/sampleip

After:
   textdata bss dec hex filename
  191011188  399792  420081   668f1 samples/bpf/sampleip

Signed-off-by: Leo Yan 
---
 samples/bpf/bpf_load.c | 23 ---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c
index 28e4678..c2bf7ca 100644
--- a/samples/bpf/bpf_load.c
+++ b/samples/bpf/bpf_load.c
@@ -651,8 +651,7 @@ void read_trace_pipe(void)
}
 }
 
-#define MAX_SYMS 30
-static struct ksym syms[MAX_SYMS];
+static struct ksym *syms;
 static int sym_cnt;
 
 static int ksym_cmp(const void *p1, const void *p2)
@@ -678,12 +677,30 @@ int load_kallsyms(void)
break;
if (!addr)
continue;
+   sym_cnt++;
+   }
+
+   syms = calloc(sym_cnt, sizeof(*syms));
+   if (!syms) {
+   fclose(f);
+   return -ENOMEM;
+   }
+
+   rewind(f);
+   while (!feof(f)) {
+   if (!fgets(buf, sizeof(buf), f))
+   break;
+   if (sscanf(buf, "%p %c %s", , , func) != 3)
+   break;
+   if (!addr)
+   continue;
syms[i].addr = (long) addr;
syms[i].name = strdup(func);
i++;
}
-   sym_cnt = i;
qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
+
+   fclose(f);
return 0;
 }
 
-- 
1.9.1



[PATCH bpf-next 2/5] samples/bpf: Dynamically allocate structure 'syms'

2018-04-18 Thread Leo Yan
Structure 'syms' is used to store kernel symbol info by reading proc fs
node '/proc/kallsyms', this structure is declared with 30 entries
and static linked into bss section.  For most case the kernel symbols
has less than 30 entries, so it's safe to define so large array, but
the side effect is bss section is big introduced by this structure and
it isn't flexible.

To fix this, this patch dynamically allocates memory for structure
'syms' based on parsing '/proc/kallsyms' line number at the runtime,
which can save elf file required memory significantly.

Before:
   textdata bss dec hex filename
  188411172 5199776 5219789  4fa5cd samples/bpf/sampleip

After:
   textdata bss dec hex filename
  191011188  399792  420081   668f1 samples/bpf/sampleip

Signed-off-by: Leo Yan 
---
 samples/bpf/bpf_load.c | 23 ---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c
index 28e4678..c2bf7ca 100644
--- a/samples/bpf/bpf_load.c
+++ b/samples/bpf/bpf_load.c
@@ -651,8 +651,7 @@ void read_trace_pipe(void)
}
 }
 
-#define MAX_SYMS 30
-static struct ksym syms[MAX_SYMS];
+static struct ksym *syms;
 static int sym_cnt;
 
 static int ksym_cmp(const void *p1, const void *p2)
@@ -678,12 +677,30 @@ int load_kallsyms(void)
break;
if (!addr)
continue;
+   sym_cnt++;
+   }
+
+   syms = calloc(sym_cnt, sizeof(*syms));
+   if (!syms) {
+   fclose(f);
+   return -ENOMEM;
+   }
+
+   rewind(f);
+   while (!feof(f)) {
+   if (!fgets(buf, sizeof(buf), f))
+   break;
+   if (sscanf(buf, "%p %c %s", , , func) != 3)
+   break;
+   if (!addr)
+   continue;
syms[i].addr = (long) addr;
syms[i].name = strdup(func);
i++;
}
-   sym_cnt = i;
qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
+
+   fclose(f);
return 0;
 }
 
-- 
1.9.1