[maemo-commits] [maemo-commits] r18704 - in projects/haf/trunk/hildon-thumbnail: . daemon

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Thu Jun 11 16:33:09 EEST 2009
Author: pvanhoof
Date: 2009-06-11 16:32:56 +0300 (Thu, 11 Jun 2009)
New Revision: 18704

Modified:
   projects/haf/trunk/hildon-thumbnail/ChangeLog
   projects/haf/trunk/hildon-thumbnail/daemon/hildon-thumbnail-daemon.c
Log:
        * daemon/hildon-thumbnail-daemon.c: Setting rlimit on memory usage



Modified: projects/haf/trunk/hildon-thumbnail/ChangeLog
===================================================================
--- projects/haf/trunk/hildon-thumbnail/ChangeLog	2009-06-11 12:01:37 UTC (rev 18703)
+++ projects/haf/trunk/hildon-thumbnail/ChangeLog	2009-06-11 13:32:56 UTC (rev 18704)
@@ -2,6 +2,7 @@
 
 	* daemon/plugins/gdkpixbuf-plugin.c: Detect animated GIF, and block support
 	for it
+	* daemon/hildon-thumbnail-daemon.c: Setting rlimit on memory usage
 
 2009-06-11  Philip Van Hoof  <philip at codeminded.be>
 

Modified: projects/haf/trunk/hildon-thumbnail/daemon/hildon-thumbnail-daemon.c
===================================================================
--- projects/haf/trunk/hildon-thumbnail/daemon/hildon-thumbnail-daemon.c	2009-06-11 12:01:37 UTC (rev 18703)
+++ projects/haf/trunk/hildon-thumbnail/daemon/hildon-thumbnail-daemon.c	2009-06-11 13:32:56 UTC (rev 18704)
@@ -45,6 +45,10 @@
 #include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/resource.h>
 
 #include <glib.h>
 #include <dbus/dbus-glib-bindings.h>
@@ -58,6 +62,22 @@
 #include "albumart-manager.h"
 #include "thumb-hal.h"
 
+/* Maximum here is a G_MAXLONG, so if you want to use > 2GB, you have
+ * to set MEM_LIMIT to RLIM_INFINITY
+ */
+#ifdef __x86_64__
+#define MEM_LIMIT 512 * 1024 * 1024
+#else
+#define MEM_LIMIT 80 * 1024 * 1024
+#endif
+
+#if defined(__OpenBSD__) && !defined(RLIMIT_AS)
+#define RLIMIT_AS RLIMIT_DATA
+#endif
+
+#undef DISABLE_MEM_LIMITS
+
+
 void keep_alive (void);
 
 static GHashTable *registrations;
@@ -153,6 +173,108 @@
 #endif
 }
 
+static guint
+get_memory_total (void)
+{
+	GError      *error = NULL;
+	const gchar *filename;
+	gchar       *contents = NULL;
+	glong        total = 0;
+
+	filename = "/proc/meminfo";
+
+	if (!g_file_get_contents (filename,
+				  &contents,
+				  NULL,
+				  &error)) {
+		g_critical ("Couldn't get memory information:'%s', %s",
+			    filename,
+			    error ? error->message : "no error given");
+		g_clear_error (&error);
+	} else {
+		gchar *start, *end, *p;
+
+		start = "MemTotal:";
+		end = "kB";
+
+		p = strstr (contents, start);
+		if (p) {
+			p += strlen (start);
+			end = strstr (p, end);
+
+			if (end) {
+				*end = '\0';
+				total = 1024 * atol (p);
+			}
+		}
+
+		g_free (contents);
+	}
+
+	if (!total) {
+		/* Setting limit to an arbitary limit */
+		total = RLIM_INFINITY;
+	}
+
+	return total;
+}
+
+static gboolean
+memory_setrlimits (void)
+{
+#ifndef DISABLE_MEM_LIMITS
+	struct rlimit rl;
+	glong         total;
+	glong         limit;
+
+	total = get_memory_total ();
+	limit = CLAMP (MEM_LIMIT, 0, total);
+
+	/* We want to limit the max virtual memory
+	 * most extractors use mmap() so only virtual memory can be
+	 * effectively limited.
+	 */
+	getrlimit (RLIMIT_AS, &rl);
+	rl.rlim_cur = limit;
+
+	if (setrlimit (RLIMIT_AS, &rl) == -1) {
+               const gchar *str = g_strerror (errno);
+
+               g_critical ("Could not set virtual memory limit with setrlimit(RLIMIT_AS), %s",
+			   str ? str : "no error given");
+
+               return FALSE;
+	} else {
+		getrlimit (RLIMIT_DATA, &rl);
+		rl.rlim_cur = limit;
+
+		if (setrlimit (RLIMIT_DATA, &rl) == -1) {
+			const gchar *str = g_strerror (errno);
+
+			g_critical ("Could not set heap memory limit with setrlimit(RLIMIT_DATA), %s",
+				    str ? str : "no error given");
+
+			return FALSE;
+		} else {
+			gchar *str1, *str2;
+
+			str1 = g_format_size_for_display (total);
+			str2 = g_format_size_for_display (limit);
+
+			g_message ("Setting memory limitations: total is %s, virtual/heap set to %s",
+				   str1,
+				   str2);
+
+			g_free (str2);
+			g_free (str1);
+		}
+	}
+#endif /* DISABLE_MEM_LIMITS */
+
+	return TRUE;
+}
+
+
 void
 keep_alive (void) 
 {
@@ -438,6 +560,8 @@
 	if (!g_thread_supported ())
 		g_thread_init (NULL);
 
+	memory_setrlimits ();
+
 	create_dummy_files ();
 
 	connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);

More information about the maemo-commits mailing list