summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ogness <john.ogness@linutronix.de>2019-03-28 15:17:34 +0106
committerJohn Ogness <john.ogness@linutronix.de>2019-03-28 15:17:34 +0106
commite1e6511cd68b646234dcc8f5f86b25b7ea48a720 (patch)
tree859af07101563fb34d00dfbf256c4a76b3294f99
parenta39de4609225129c19df931d556b1d032d806403 (diff)
schulung_tools: rename/modify malloc to avoid system()
A more complex but cleaner implementation of retrieving/printing the stats has been implemented. The implementation is put into a separate stats.c file so that the main program (malloc.) can be easily reviewed to see what the program is doing. Signed-off-by: John Ogness <john.ogness@linutronix.de>
-rw-r--r--schulung_tools/malloc/Makefile6
-rw-r--r--schulung_tools/malloc/README5
-rw-r--r--schulung_tools/malloc/README.txt5
-rw-r--r--schulung_tools/malloc/malloc.c38
-rw-r--r--schulung_tools/malloc/stats.c56
5 files changed, 77 insertions, 33 deletions
diff --git a/schulung_tools/malloc/Makefile b/schulung_tools/malloc/Makefile
index b6160a8..a1bd685 100644
--- a/schulung_tools/malloc/Makefile
+++ b/schulung_tools/malloc/Makefile
@@ -1,7 +1,7 @@
-malloc: malloc.c
- $(CROSS_COMPILE)gcc -g -omalloc malloc.c
+malloc: malloc.c stats.c
+ $(CROSS_COMPILE)gcc -g $^ -o$@
clean:
rm -f malloc core
-.PHONY: clean
+.PHONY: all clean
diff --git a/schulung_tools/malloc/README b/schulung_tools/malloc/README
deleted file mode 100644
index 77b18cd..0000000
--- a/schulung_tools/malloc/README
+++ /dev/null
@@ -1,5 +0,0 @@
-This program demonstrates that a processes is not actually assigned memory
-until it page-faults on the pages.
-
-WARNING: This program might fill your RAM and cause the Linux OOM Killer to
- be invoked.
diff --git a/schulung_tools/malloc/README.txt b/schulung_tools/malloc/README.txt
new file mode 100644
index 0000000..d496b5e
--- /dev/null
+++ b/schulung_tools/malloc/README.txt
@@ -0,0 +1,5 @@
+This program demonstrates that a process is not actually assigned memory
+until it page-faults on the pages.
+
+WARNING: This program might fill your RAM and cause the Linux OOM Killer
+ to be invoked.
diff --git a/schulung_tools/malloc/malloc.c b/schulung_tools/malloc/malloc.c
index a636426..76defbe 100644
--- a/schulung_tools/malloc/malloc.c
+++ b/schulung_tools/malloc/malloc.c
@@ -1,57 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
-#define NUM_OF_GBYTES 8
-
-/* if verbose is enabled forks are done for printing additional informations */
-#define VERBOSE 0
-
-#if VERBOSE
-/* forking is not always possible if memory is low */
-#define sys(cmd) if(system(cmd) == -1) printf("%s failed\n", cmd);
-#else
-#define sys(cmd) ;
-#endif
+extern void print_stats(int);
-#define MEMFREE "free -h"
+#define NUM_OF_GBYTES 8
int main(void)
{
char *p[NUM_OF_GBYTES];
int i;
- printf("memory usage:\n");
- sys(MEMFREE);
- printf("\n\n");
-
for (i = 0; i < NUM_OF_GBYTES; i++) {
+ printf("allocating 1GB\n");
+ print_stats(0);
p[i] = malloc(1024 * 1024 * 1024);
- printf("allocated 1GB: %p\n", p[i]);
- sys(MEMFREE);
+ print_stats(1);
+ printf(" malloc returned: %p\n", p[i]);
}
- printf("\n\n");
+ printf("\n");
for (i = 0; i < NUM_OF_GBYTES; i++) {
if (p[i]) {
printf("memsetting 1GB: %p\n", p[i]);
- sys("date +%s.%N");
+ print_stats(0);
memset(p[i], 1, 1024 * 1024 * 1024);
- sys("date +%s.%N");
- sys(MEMFREE);
+ print_stats(1);
}
}
- printf("\n\n");
+ printf("\n");
for (i = 0; i < NUM_OF_GBYTES; i++) {
if (p[i]) {
printf("free 1GB: %p\n", p[i]);
+ print_stats(0);
free(p[i]);
- sys(MEMFREE);
+ print_stats(1);
}
}
+
return 0;
}
diff --git a/schulung_tools/malloc/stats.c b/schulung_tools/malloc/stats.c
new file mode 100644
index 0000000..4b17323
--- /dev/null
+++ b/schulung_tools/malloc/stats.c
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+void print_stats(int postcall)
+{
+ static struct timespec tp1;
+ static struct timespec tp2;
+ static int fd = -1;
+ char buf[32];
+ int virt;
+ int res;
+
+ if (fd == -1)
+ fd = open("/proc/self/statm", O_RDONLY);
+
+ if (!postcall) {
+ clock_gettime(CLOCK_MONOTONIC, &tp1);
+ return;
+ }
+
+ printf(" time=");
+ if (tp1.tv_sec && clock_gettime(CLOCK_MONOTONIC, &tp2) == 0) {
+ if (tp1.tv_nsec > tp2.tv_nsec) {
+ tp2.tv_sec--;
+ tp2.tv_nsec += 1000000000;
+ }
+ tp2.tv_sec -= tp1.tv_sec;
+ tp2.tv_nsec -= tp1.tv_nsec;
+ printf("%d.%06ds ", tp2.tv_sec, tp2.tv_nsec / 1000);
+ } else {
+ tp2.tv_sec = 0;
+ printf("? ");
+ }
+ tp1 = tp2;
+
+ if (fd < 0)
+ goto out;
+
+ lseek(fd, 0, SEEK_SET);
+ if (read(fd, buf, sizeof(buf)) <= 0)
+ goto out;
+
+ if (sscanf(buf, "%d %d ", &virt, &res) != 2)
+ goto out;
+
+ printf("virt=%dKB res=%dKB", virt * 4, res * 4);
+out:
+ printf("\n");
+ fflush(stdout);
+}