[gjs/wip/chergert/sysprof-3: 4/4] profiler: add support for GJS_TRACE_FD
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/chergert/sysprof-3: 4/4] profiler: add support for GJS_TRACE_FD
- Date: Sat, 1 Jun 2019 18:49:52 +0000 (UTC)
commit bd3f4a24029735083195dfc1a9384fde23a64420
Author: Christian Hergert <chergert redhat com>
Date: Sat Jun 1 11:46:04 2019 -0700
profiler: add support for GJS_TRACE_FD
This commit adds support for a special environment variable GJS_TRACE_FD.
The console will, in response to this environment variable, enable
tracing of the GJS context and deliver the output to the provided file-
descriptor.
In the upcoming Sysprof-3 branch, this can be used to automatically sample
some GJS-based applications. Applications that use libgjs from a C-based
main() will need to implement the g_getenv() GJS_TRACE_FD manually.
gjs/console.cpp | 19 +++++++++++++++++++
gjs/profiler.cpp | 31 +++++++++++++++++++++++++++----
gjs/profiler.h | 2 ++
3 files changed, 48 insertions(+), 4 deletions(-)
---
diff --git a/gjs/console.cpp b/gjs/console.cpp
index 4f876153..f6aac9e0 100644
--- a/gjs/console.cpp
+++ b/gjs/console.cpp
@@ -281,6 +281,16 @@ main(int argc, char **argv)
/* This should be removed after a suitable time has passed */
check_script_args_for_stray_gjs_args(script_argc, script_argv);
+ /* Check for GJS_TRACE_FD for sysprof profiling */
+ const char* env_tracefd = g_getenv("GJS_TRACE_FD");
+ int tracefd = -1;
+ if (env_tracefd) {
+ tracefd = g_ascii_strtoll(env_tracefd, nullptr, 10);
+ g_setenv("GJS_TRACE_FD", "", true);
+ if (tracefd > 0)
+ enable_profiler = true;
+ }
+
if (interactive_mode && enable_profiler) {
g_message("Profiler disabled in interactive mode.");
enable_profiler = false;
@@ -318,6 +328,15 @@ main(int argc, char **argv)
if (enable_profiler && profile_output_path) {
GjsProfiler *profiler = gjs_context_get_profiler(js_context);
gjs_profiler_set_filename(profiler, profile_output_path);
+ } else if (enable_profiler && tracefd > -1) {
+ GjsProfiler* profiler = gjs_context_get_profiler(js_context);
+ gjs_profiler_set_fd(profiler, tracefd);
+ tracefd = -1;
+ }
+
+ if (tracefd != -1) {
+ close(tracefd);
+ tracefd = -1;
}
/* prepare command line arguments */
diff --git a/gjs/profiler.cpp b/gjs/profiler.cpp
index 6f37eaca..6f0f6e72 100644
--- a/gjs/profiler.cpp
+++ b/gjs/profiler.cpp
@@ -101,6 +101,9 @@ struct _GjsProfiler {
/* The filename to write to */
char *filename;
+ /* An FD to capture to */
+ int fd;
+
#ifdef ENABLE_PROFILER
/* Our POSIX timer to wakeup SIGPROF */
timer_t timer;
@@ -219,6 +222,7 @@ _gjs_profiler_new(GjsContext *context)
self->cx = static_cast<JSContext *>(gjs_context_get_native_context(context));
self->pid = getpid();
#endif
+ self->fd = -1;
profiling_context = context;
@@ -245,6 +249,9 @@ _gjs_profiler_free(GjsProfiler *self)
profiling_context = nullptr;
+ if (self->fd != -1)
+ close(self->fd);
+
g_clear_pointer(&self->filename, g_free);
#ifdef ENABLE_PROFILER
g_clear_pointer(&self->capture, sp_capture_writer_unref);
@@ -399,11 +406,16 @@ gjs_profiler_start(GjsProfiler *self)
struct itimerspec its = { 0 };
struct itimerspec old_its;
- GjsAutoChar path = g_strdup(self->filename);
- if (!path)
- path = g_strdup_printf("gjs-%jd.syscap", intmax_t(self->pid));
+ if (self->fd != -1) {
+ self->capture = sp_capture_writer_new_from_fd(self->fd, 0);
+ self->fd = -1;
+ } else {
+ GjsAutoChar path = g_strdup(self->filename);
+ if (!path)
+ path = g_strdup_printf("gjs-%jd.syscap", intmax_t(self->pid));
- self->capture = sp_capture_writer_new(path, 0);
+ self->capture = sp_capture_writer_new(path, 0);
+ }
if (!self->capture) {
g_warning("Failed to open profile capture");
@@ -636,3 +648,14 @@ gjs_profiler_set_filename(GjsProfiler *self,
g_free(self->filename);
self->filename = g_strdup(filename);
}
+
+void gjs_profiler_set_fd(GjsProfiler* self, int fd) {
+ g_return_if_fail(self);
+ g_return_if_fail(!self->running);
+
+ if (self->fd != fd) {
+ if (self->fd != -1)
+ close(self->fd);
+ self->fd = fd;
+ }
+}
diff --git a/gjs/profiler.h b/gjs/profiler.h
index 8bebed25..95615bd4 100644
--- a/gjs/profiler.h
+++ b/gjs/profiler.h
@@ -40,6 +40,8 @@ GType gjs_profiler_get_type(void);
GJS_EXPORT
void gjs_profiler_set_filename(GjsProfiler *self,
const char *filename);
+GJS_EXPORT
+void gjs_profiler_set_fd(GjsProfiler* self, int fd);
GJS_EXPORT
void gjs_profiler_start(GjsProfiler *self);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]