[gjs: 8/11] profiler: Always activate profiler if GJS_ENABLE_PROFILER=1



commit f8528da44d3fe55915809c7f92c242bae5c3c4a5
Author: Philip Chimento <philip endlessm com>
Date:   Mon Jan 22 16:29:48 2018 -0800

    profiler: Always activate profiler if GJS_ENABLE_PROFILER=1
    
    This checks in gjs_context_eval() to see if the GJS_ENABLE_PROFILER
    environment variable is set but no profiler has been created. In that
    case, we run a profiler for the duration of the JS code evaluation.
    
    This is to accommodate applications such as Polari that are written in JS
    but ship their own loader that links to libgjs and calls
    gjs_context_eval(). This way, they can be profiled without writing any
    extra code in their loaders (though they will still need it if, for
    example, they want to add a command line argument.)

 gjs-srcs.mk            |  1 +
 gjs/context.cpp        | 14 ++++++++++++++
 gjs/profiler-private.h | 35 +++++++++++++++++++++++++++++++++++
 gjs/profiler.cpp       | 12 +++++++++++-
 4 files changed, 61 insertions(+), 1 deletion(-)
---
diff --git a/gjs-srcs.mk b/gjs-srcs.mk
index ab6ccfb..c7600be 100644
--- a/gjs-srcs.mk
+++ b/gjs-srcs.mk
@@ -76,6 +76,7 @@ gjs_srcs =                            \
        gjs/native.cpp                  \
        gjs/native.h                    \
        gjs/profiler.cpp                \
+       gjs/profiler-private.h          \
        gjs/stack.cpp                   \
        modules/modules.cpp             \
        modules/modules.h               \
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 4a679f6..cb34c86 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -35,6 +35,7 @@
 #include "jsapi-util.h"
 #include "jsapi-wrapper.h"
 #include "native.h"
+#include "profiler-private.h"
 #include "byteArray.h"
 #include "gi/object.h"
 #include "gi/repo.h"
@@ -690,6 +691,13 @@ gjs_context_eval(GjsContext   *js_context,
 {
     bool ret = false;
 
+    GjsProfiler *profiler = nullptr;
+    const char *env_profiler = g_getenv("GJS_ENABLE_PROFILER");
+    if (env_profiler && !_gjs_profiler_get_current()) {
+        profiler = gjs_profiler_new(js_context);
+        gjs_profiler_start(profiler);
+    }
+
     JSAutoCompartment ac(js_context->context, js_context->global);
     JSAutoRequest ar(js_context->context);
 
@@ -704,6 +712,9 @@ gjs_context_eval(GjsContext   *js_context,
      * uncaught exceptions have been reported since draining runs callbacks. */
     ok = _gjs_context_run_jobs(js_context) && ok;
 
+    if (profiler)
+        gjs_profiler_stop(profiler);
+
     if (!ok) {
         uint8_t code;
         if (_gjs_context_should_exit(js_context, &code)) {
@@ -740,6 +751,9 @@ gjs_context_eval(GjsContext   *js_context,
     ret = true;
 
  out:
+    if (profiler)
+        gjs_profiler_free(profiler);
+
     g_object_unref(G_OBJECT(js_context));
     context_reset_exit(js_context);
     return ret;
diff --git a/gjs/profiler-private.h b/gjs/profiler-private.h
new file mode 100644
index 0000000..1e17de8
--- /dev/null
+++ b/gjs/profiler-private.h
@@ -0,0 +1,35 @@
+/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2018 Endless Mobile, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef GJS_PROFILER_PRIVATE_H
+#define GJS_PROFILER_PRIVATE_H
+
+#include "profiler.h"
+
+G_BEGIN_DECLS
+
+GjsProfiler *_gjs_profiler_get_current(void);
+
+G_END_DECLS
+
+#endif  /* GJS_PROFILER_PRIVATE_H */
diff --git a/gjs/profiler.cpp b/gjs/profiler.cpp
index 058fcdd..5049433 100644
--- a/gjs/profiler.cpp
+++ b/gjs/profiler.cpp
@@ -38,7 +38,7 @@
 #include <js/ProfilingStack.h>
 
 #include "jsapi-util.h"
-#include "profiler.h"
+#include "profiler-private.h"
 #ifdef ENABLE_PROFILER
 # include "util/sp-capture-writer.h"
 #endif
@@ -114,6 +114,16 @@ struct _GjsProfiler {
 
 static GjsProfiler *current_profiler;
 
+/* Internal use only - used to determine whether a profiler is already created,
+ * in order to accommodate code that links to libgjs but doesn't create its own
+ * profiler while deciding what to do for the GJS_ENABLE_PROFILER=1 environment
+ * variable. */
+GjsProfiler *
+_gjs_profiler_get_current(void)
+{
+    return current_profiler;
+}
+
 #ifdef ENABLE_PROFILER
 /*
  * gjs_profiler_extract_maps:


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