[sysprof/ftrace: 10/16] More cleanup
- From: Søren Sandmann Pedersen <ssp src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [sysprof/ftrace: 10/16] More cleanup
- Date: Fri, 14 Aug 2009 06:57:29 +0000 (UTC)
commit 580cef3e87ff87cd28ec862889166d987d22557e
Author: Søren Sandmann Pedersen <sandmann daimi au dk>
Date: Fri Aug 14 02:02:35 2009 -0400
More cleanup
TODO | 2 +
collector.c | 120 +++++++++++++++++++++++-----------------------------------
collector.h | 3 +-
3 files changed, 52 insertions(+), 73 deletions(-)
---
diff --git a/TODO b/TODO
index 4e51bdd..f39f7bd 100644
--- a/TODO
+++ b/TODO
@@ -23,6 +23,8 @@ Before 1.0.4:
Before 1.2:
+* Give an informative error message if run as root
+
* Find out why gtk_tree_view_columns_autosize() apparently doesn't
work on empty tree views.
diff --git a/collector.c b/collector.c
index 79d470d..de7d728 100644
--- a/collector.c
+++ b/collector.c
@@ -37,7 +37,9 @@ typedef struct CPUInfo CPUInfo;
#define N_CPU 8 /* FIXME: Arbitrary number of CPU's */
-#define SYSPROF_FILE "/sys/kernel/debug/tracing/trace_pipe"
+#define SYSPROF_DIR "/sys/kernel/debug/tracing/"
+#define SYSPROF_FILE (SYSPROF_DIR "trace_pipe")
+
enum
{
@@ -212,28 +214,24 @@ in_dead_period (Collector *collector)
return FALSE;
}
-static void
-warn (const char *text)
-{
- g_warning ("%s: (%s)\n", text, strerror (errno));
-}
-
static gboolean
-write_text (const char *file, const char *text)
+write_text (const char *file, const char *text, GError **err)
{
int fd = open (file, O_RDWR);
if (fd < 0)
{
- g_print ("open %s fail\n",file);
- warn ("Failed to open");
+ g_set_error (err, COLLECTOR_ERROR, COLLECTOR_ERROR_IO_ERROR,
+ "Could not open %s", file);
+
return FALSE;
}
if (write (fd, text, strlen (text)) < 0)
{
- g_print ("write to %s failed\n", file);
- warn ("asdf");
+ g_set_error (err, COLLECTOR_ERROR, COLLECTOR_ERROR_IO_ERROR,
+ "Could not write %s to %s", text, file);
+
return FALSE;
}
@@ -243,22 +241,22 @@ write_text (const char *file, const char *text)
void
collector_set_tracing (Collector *collector,
- gboolean trace)
+ gboolean trace)
{
if (trace)
- write_text ("/sys/kernel/debug/tracing/tracing_enabled", "1\n");
+ write_text ("/sys/kernel/debug/tracing/tracing_enabled", "1\n", NULL);
else
- write_text ("/sys/kernel/debug/tracing/tracing_enabled", "0\n");
+ write_text ("/sys/kernel/debug/tracing/tracing_enabled", "0\n", NULL);
}
typedef struct Entry
{
- guint32 pid;
- guint32 cpu;
- guint64 timestamp;
- glong type;
- gulong address;
- gulong location;
+ guint32 pid;
+ guint32 cpu;
+ guint64 timestamp;
+ glong type;
+ gulong address;
+ gulong location;
} Entry;
static void
@@ -422,72 +420,56 @@ static gboolean
start_tracing (Collector *collector,
GError **err)
{
+ typedef struct
+ {
+ const char file[56];
+ const char text[8];
+ } Setting;
+
+ static const Setting settings[] =
+ {
+ { SYSPROF_DIR "trace_options", "noblock" },
+ { SYSPROF_DIR "current_tracer", "sysprof" },
+ { SYSPROF_DIR "trace_options", "raw" },
+ { SYSPROF_DIR "trace_options", "bin" },
+ { SYSPROF_DIR "sysprof_sample_period", "10000" },
+ };
+
int fd;
int i;
- if (access ("/sys/kernel/debug/tracing", X_OK) < 0)
+ if (access (SYSPROF_DIR, X_OK) < 0)
{
mount ("nodev", "/sys/kernel/debug", "debugfs", 0, NULL);
- if (access ("/sys/kernel/debug/tracing", X_OK) < 0)
+ if (access (SYSPROF_DIR, X_OK) < 0)
{
- g_print ("couldn't mount directory FIXME error\n");
+ g_set_error (err, COLLECTOR_ERROR, COLLECTOR_ERROR_IO_ERROR,
+ "Could not mount tracer directory");
+
return FALSE;
}
}
- if (!write_text ("/sys/kernel/debug/tracing/trace_options", "noblock"))
- {
- return FALSE;
- }
-
- if (!write_text ("/sys/kernel/debug/tracing/current_tracer", "sysprof\n"))
- {
- warn ("Failed to append sysprof\n");
- return FALSE;
- }
-
- if (!write_text ("/sys/kernel/debug/tracing/tracing_enabled", "0\n"))
+ for (i = 0; i < G_N_ELEMENTS (settings); ++i)
{
+ const Setting *setting = &settings[i];
+ if (!write_text (setting->file, setting->text, err))
+ return FALSE;
}
- if (!write_text ("/sys/kernel/debug/tracing/trace_options", "raw\n"))
- {
- g_print ("Failed to make raw\n");
- return FALSE;
- }
-
- if (!write_text ("/sys/kernel/debug/tracing/trace_options", "bin\n"))
- {
- g_print ("Failed to make binary\n");
- return FALSE;
- }
-
- if (!write_text ("/sys/kernel/debug/tracing/sysprof_sample_period", "10000\n"))
- {
- g_print ("Failed to set sample period\n");
- return FALSE;
- }
-
fd = open (SYSPROF_FILE, O_RDONLY | O_NONBLOCK);
if (fd < 0)
{
- g_print ("Failed to open trace pipe\n");
+ g_set_error (err, COLLECTOR_ERROR, COLLECTOR_ERROR_IO_ERROR,
+ "Could not open %s", SYSPROF_FILE);
+
return FALSE;
}
- g_print ("fd: %d\n", fd);
-
- if (!write_text ("/sys/kernel/debug/tracing/tracing_enabled", "1\n"))
- {
- g_print ("Failed to enable\n");
+ if (!write_text (SYSPROF_DIR "tracing_enabled", "1\n", err))
return FALSE;
- }
- else
- {
- g_print ("enabled\n");
- }
fd_add_watch (fd, collector);
@@ -503,8 +485,6 @@ gboolean
collector_start (Collector *collector,
GError **err)
{
- g_print ("start\n");
-
if (collector->fd < 0 && !start_tracing (collector, err))
return FALSE;
@@ -527,8 +507,7 @@ collector_stop (Collector *collector)
close (collector->fd);
- if (!write_text ("/sys/kernel/debug/tracing/tracing_enabled", "0\n"))
- g_print ("Failed to disable\n");
+ write_text ("/sys/kernel/debug/tracing/tracing_enabled", "0\n", NULL);
collector->fd = -1;
}
@@ -551,9 +530,6 @@ collector_reset (Collector *collector)
int
collector_get_n_samples (Collector *collector)
{
-#if 0
- g_print ("returning %d samples\n", collector->n_samples);
-#endif
return collector->n_samples;
}
diff --git a/collector.h b/collector.h
index 11131db..aa83dc6 100644
--- a/collector.h
+++ b/collector.h
@@ -30,7 +30,8 @@ GQuark collector_error_quark (void);
typedef enum
{
- COLLECTOR_ERROR_CANT_OPEN_FILE
+ COLLECTOR_ERROR_CANT_OPEN_FILE,
+ COLLECTOR_ERROR_IO_ERROR,
} CollectorError;
/* callback is called whenever a new sample arrives */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]