[PATCH 1/3] Add get_free_memory_size() to get the amount of free memory.
Atsushi Kumagai
kumagai-atsushi at mxc.nes.nec.co.jp
Fri Nov 9 02:02:53 EST 2012
get_free_memory_size() reads /proc/meminfo and consider "MemFree:"
the amount of free memory.
Signed-off-by: Atsushi Kumagai <kumagai-atsushi at mxc.nes.nec.co.jp>
---
makedumpfile.c | 34 ++++++++++++++++++++++++++++++++++
makedumpfile.h | 1 +
2 files changed, 35 insertions(+)
diff --git a/makedumpfile.c b/makedumpfile.c
index 1183330..73a466e 100644
--- a/makedumpfile.c
+++ b/makedumpfile.c
@@ -7972,6 +7972,40 @@ out:
return ret;
}
+/*
+ * Get the amount of free memory from /proc/meminfo.
+ */
+unsigned long long
+get_free_memory_size(void) {
+ char buf[BUFSIZE_FGETS];
+ char unit[4];
+ unsigned long long free_size = 0;
+ char *name_meminfo = "/proc/meminfo";
+ FILE *file_meminfo;
+
+ if ((file_meminfo = fopen(name_meminfo, "r")) == NULL) {
+ ERRMSG("Can't open the %s. %s\n", name_meminfo, strerror(errno));
+ return FALSE;
+ }
+
+ while (fgets(buf, BUFSIZE_FGETS, file_meminfo) != NULL) {
+ if (sscanf(buf, "MemFree: %llu %s", &free_size, unit) == 2) {
+ if (strcmp(unit, "kB") == 0) {
+ free_size *= 1024;
+ goto out;
+ }
+ }
+ }
+
+ ERRMSG("Can't get free memory size.\n");
+ free_size = 0;
+out:
+ if (fclose(file_meminfo) < 0)
+ ERRMSG("Can't close the %s. %s\n", name_meminfo, strerror(errno));
+
+ return free_size;
+}
+
static struct option longopts[] = {
{"split", no_argument, NULL, 's'},
{"reassemble", no_argument, NULL, 'r'},
diff --git a/makedumpfile.h b/makedumpfile.h
index 97aca2a..c91b4e9 100644
--- a/makedumpfile.h
+++ b/makedumpfile.h
@@ -1611,5 +1611,6 @@ struct elf_prstatus {
unsigned long long get_num_dumpable_cyclic(void);
int get_loads_dumpfile_cyclic(void);
int initial_xen(void);
+unsigned long long get_free_memory_size(void);
#endif /* MAKEDUMPFILE_H */
--
1.7.11
More information about the kexec
mailing list