[PATCH] Implement memory tracker for Solarish systems

John Levon <[email protected]>
Newsgroups org.kernel.vger.smatch
Message-ID <[email protected]>
From: Patrick Mooney <[email protected]>

In addition, only try to track if requested by the mem option.

Portions-contributed-by: John Levon <[email protected]>
---
 smatch_mem_tracker.c | 39 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 5 deletions(-)

diff --git a/smatch_mem_tracker.c b/smatch_mem_tracker.c
index b1671857..a92b8f35 100644
--- a/smatch_mem_tracker.c
+++ b/smatch_mem_tracker.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2018 Oracle.
+ * Copyright 2019 Joyent, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -16,21 +17,46 @@
  */
 
 #include "smatch.h"
+#include <fcntl.h>
 #include <unistd.h>
+#ifdef __sun
+#include <sys/procfs.h>
+#endif
 
 static int my_id;
 
 static unsigned long max_size;
 
+#ifdef __sun
+unsigned long get_mem_kb(void)
+{
+	static int my_fd = -2;
+	prpsinfo_t pbuf;
+
+	if (my_fd == -2) {
+		/* Do not repeatedly attempt this if it fails. */
+		my_fd = open("/proc/self/psinfo", O_RDONLY);
+	}
+	if (my_fd == -1) {
+		return (0);
+	}
+
+	if (pread(my_fd, &pbuf, sizeof (pbuf), 0) != sizeof (pbuf)) {
+		return (0);
+	}
+
+	return (pbuf.pr_rssize);
+}
+#else
 unsigned long get_mem_kb(void)
 {
 	FILE *file;
-	char buf[1024];
+	char buf[1024] = "0";
 	unsigned long size;
 
 	file = fopen("/proc/self/statm", "r");
 	if (!file)
-		return 0;
+	        return 0;
 	fread(buf, 1, sizeof(buf), file);
 	fclose(file);
 
@@ -38,14 +64,17 @@ unsigned long get_mem_kb(void)
 	size = size * sysconf(_SC_PAGESIZE) / 1024;
 	return size;
 }
+#endif
 
 static void match_end_func(struct symbol *sym)
 {
 	unsigned long size;
 
-	size = get_mem_kb();
-	if (size > max_size)
-		max_size = size;
+	if (option_mem) {
+		size = get_mem_kb();
+		if (size > max_size)
+			max_size = size;
+	}
 }
 
 unsigned long get_max_memory(void)
-- 
2.17.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.