gnome-control-center r9306 - in trunk: . font-viewer



Author: hadess
Date: Mon Mar  2 23:06:10 2009
New Revision: 9306
URL: http://svn.gnome.org/viewvc/gnome-control-center?rev=9306&view=rev

Log:
2009-03-02  Bastien Nocera  <hadess hadess net>

	* font-viewer/Makefile.am:
	* font-viewer/font-thumbnailer.c (main):
	* font-viewer/totem-resources.c (set_resource_limits),
	(time_monitor), (totem_resources_monitor_start),
	(totem_resources_monitor_stop):
	* font-viewer/totem-resources.h: Stop the font thumbnailer
	eating all the CPU, and limit the resources to 30 seconds
	and 256MB of RAM, which should be plenty (Closes: #573795)



Added:
   trunk/font-viewer/totem-resources.c
   trunk/font-viewer/totem-resources.h
Modified:
   trunk/ChangeLog
   trunk/font-viewer/Makefile.am
   trunk/font-viewer/font-thumbnailer.c

Modified: trunk/font-viewer/Makefile.am
==============================================================================
--- trunk/font-viewer/Makefile.am	(original)
+++ trunk/font-viewer/Makefile.am	Mon Mar  2 23:06:10 2009
@@ -5,7 +5,7 @@
 bin_PROGRAMS = gnome-thumbnail-font gnome-font-viewer
 
 gnome_thumbnail_font_LDADD = $(GNOMECC_CAPPLETS_LIBS) $(FONT_VIEWER_LIBS)
-gnome_thumbnail_font_SOURCES = ftstream-vfs.c font-thumbnailer.c
+gnome_thumbnail_font_SOURCES = ftstream-vfs.c font-thumbnailer.c totem-resources.c totem-resources.h
 
 gnome_font_viewer_LDADD = $(GNOMECC_CAPPLETS_LIBS) $(FONT_VIEWER_LIBS)
 gnome_font_viewer_SOURCES = ftstream-vfs.c font-view.c

Modified: trunk/font-viewer/font-thumbnailer.c
==============================================================================
--- trunk/font-viewer/font-thumbnailer.c	(original)
+++ trunk/font-viewer/font-thumbnailer.c	Mon Mar  2 23:06:10 2009
@@ -32,6 +32,8 @@
 #include <gio/gio.h>
 #include <glib/gi18n.h>
 
+#include "totem-resources.h"
+
 static const gchar *
 get_ft_error(FT_Error error)
 {
@@ -255,6 +257,7 @@
     setlocale (LC_ALL, "");
 
     g_type_init ();
+    g_thread_init (NULL);
 
     context = g_option_context_new (NULL);
     g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
@@ -294,6 +297,8 @@
 	goto out;
     }
 
+    totem_resources_monitor_start (arguments[0], 30);
+
     file = g_file_new_for_commandline_arg (arguments[0]);
     uri = g_file_get_uri (file);
     g_object_unref (file);
@@ -367,6 +372,8 @@
     save_pixbuf(pixbuf, arguments[1]);
     g_object_unref(pixbuf);
 
+    totem_resources_monitor_stop ();
+
     /* freeing the face causes a crash I haven't tracked down yet */
     error = FT_Done_Face(face);
     if (error) {

Added: trunk/font-viewer/totem-resources.c
==============================================================================
--- (empty file)
+++ trunk/font-viewer/totem-resources.c	Mon Mar  2 23:06:10 2009
@@ -0,0 +1,123 @@
+/* 
+ * Copyright (C) 2007 Bastien Nocera <hadess hadess net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
+ *
+ * The Totem project hereby grant permission for non-gpl compatible GStreamer
+ * plugins to be used and distributed together with GStreamer and Totem. This
+ * permission are above and beyond the permissions granted by the GPL license
+ * Totem is covered by.
+ *
+ * Monday 7th February 2005: Christian Schaller: Add exception clause.
+ * See license_change file for details.
+ *
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/resource.h>
+
+#include "totem-resources.h"
+
+#define MAX_HELPER_MEMORY (256 * 1024 * 1024)	/* 256 MB */
+#define MAX_HELPER_SECONDS (15)			/* 15 seconds */
+#define DEFAULT_SLEEP_TIME (30 * G_USEC_PER_SEC) /* 30 seconds */
+
+static guint sleep_time = DEFAULT_SLEEP_TIME;
+static gboolean finished = TRUE;
+
+static void
+set_resource_limits (const char *input)
+{
+	struct rlimit limit;
+	struct stat buf;
+	rlim_t max;
+
+	max = MAX_HELPER_MEMORY;
+
+	/* Set the maximum virtual size depending on the size
+	 * of the file to process, as we wouldn't be able to
+	 * mmap it otherwise */
+	if (input == NULL) {
+		max = MAX_HELPER_MEMORY;
+	} else if (g_stat (input, &buf) == 0) {
+		max = MAX_HELPER_MEMORY + buf.st_size;
+	} else if (g_str_has_prefix (input, "file://") != FALSE) {
+		char *file;
+		file = g_filename_from_uri (input, NULL, NULL);
+		if (file != NULL && g_stat (file, &buf) == 0)
+			max = MAX_HELPER_MEMORY + buf.st_size;
+		g_free (file);
+	}
+
+	limit.rlim_cur = max;
+	limit.rlim_max = max;
+
+	setrlimit (RLIMIT_DATA, &limit);
+
+	limit.rlim_cur = MAX_HELPER_SECONDS;
+	limit.rlim_max = MAX_HELPER_SECONDS;
+	setrlimit (RLIMIT_CPU, &limit);
+}
+
+G_GNUC_NORETURN static gpointer
+time_monitor (gpointer data)
+{
+	const char *app_name;
+
+	g_usleep (sleep_time);
+
+	if (finished != FALSE)
+		g_thread_exit (NULL);
+
+	app_name = g_get_application_name ();
+	if (app_name == NULL)
+		app_name = g_get_prgname ();
+	g_print ("%s couldn't process file: '%s'\n"
+		 "Reason: Took too much time to process.\n",
+		 app_name,
+		 (const char *) data);
+
+	exit (0);
+}
+
+void
+totem_resources_monitor_start (const char *input, gint wall_clock_time)
+{
+	set_resource_limits (input);
+
+	if (wall_clock_time < 0)
+		return;
+
+	if (wall_clock_time > 0)
+		sleep_time = wall_clock_time;
+
+	finished = FALSE;
+	g_thread_create (time_monitor, (gpointer) input, FALSE, NULL);
+}
+
+void
+totem_resources_monitor_stop (void)
+{
+	finished = TRUE;
+}
+

Added: trunk/font-viewer/totem-resources.h
==============================================================================
--- (empty file)
+++ trunk/font-viewer/totem-resources.h	Mon Mar  2 23:06:10 2009
@@ -0,0 +1,33 @@
+/* 
+ * Copyright (C) 2007 Bastien Nocera <hadess hadess net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
+ *
+ * The Totem project hereby grant permission for non-gpl compatible GStreamer
+ * plugins to be used and distributed together with GStreamer and Totem. This
+ * permission are above and beyond the permissions granted by the GPL license
+ * Totem is covered by.
+ *
+ * Monday 7th February 2005: Christian Schaller: Add exception clause.
+ * See license_change file for details.
+ *
+ */
+
+#include <glib.h>
+
+void totem_resources_monitor_start	(const char *input,
+					 gint wall_clock_time);
+void totem_resources_monitor_stop	(void);
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]