2 * Copyright (C) 2007 Bastien Nocera <hadess@hadess.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The Totem project hereby grant permission for non-gpl compatible GStreamer
19 * plugins to be used and distributed together with GStreamer and Totem. This
20 * permission are above and beyond the permissions granted by the GPL license
21 * Totem is covered by.
23 * Monday 7th February 2005: Christian Schaller: Add exception clause.
24 * See license_change file for details.
31 #include <glib/gstdio.h>
32 #include <glib/gthread.h>
36 #include <sys/types.h>
38 #include <sys/resource.h>
40 #include "totem-resources.h"
42 #define MAX_HELPER_MEMORY (256 * 1024 * 1024) /* 256 MB */
43 #define MAX_HELPER_SECONDS (15) /* 15 seconds */
44 #define DEFAULT_SLEEP_TIME (30 * G_USEC_PER_SEC) /* 30 seconds */
46 static guint sleep_time = DEFAULT_SLEEP_TIME;
47 static gboolean finished = TRUE;
50 set_resource_limits (const char *input)
56 g_return_if_fail (input != NULL);
58 max = MAX_HELPER_MEMORY;
60 /* Set the maximum virtual size depending on the size
61 * of the file to process, as we wouldn't be able to
62 * mmap it otherwise */
63 if (g_stat (input, &buf) == 0) {
64 max = MAX_HELPER_MEMORY + buf.st_size;
65 } else if (g_str_has_prefix (input, "file://") != FALSE) {
67 file = g_filename_from_uri (input, NULL, NULL);
68 if (file != NULL && g_stat (file, &buf) == 0)
69 max = MAX_HELPER_MEMORY + buf.st_size;
76 setrlimit (RLIMIT_DATA, &limit);
78 limit.rlim_cur = MAX_HELPER_SECONDS;
79 limit.rlim_max = MAX_HELPER_SECONDS;
80 setrlimit (RLIMIT_CPU, &limit);
84 time_monitor (gpointer data)
88 g_usleep (sleep_time);
90 if (finished != FALSE)
93 app_name = g_get_application_name ();
95 app_name = g_get_prgname ();
96 g_print ("%s couln't process file: '%s'\n"
97 "Reason: Took too much time to process.\n",
105 totem_resources_monitor_start (const char *input, guint wall_clock_time)
107 set_resource_limits (input);
109 if (wall_clock_time != 0)
110 sleep_time = wall_clock_time;
113 g_thread_create (time_monitor, (gpointer) input, FALSE, NULL);
117 totem_resources_monitor_stop (void)