Example #1
0
 public function disable(array $options = []) : ProviderInterface
 {
     $this->data = tideways_disable();
     return $this;
 }
Example #2
0
    if (extension_loaded('tideways')) {
        tideways_enable(TIDEWAYS_FLAGS_CPU | TIDEWAYS_FLAGS_MEMORY | TIDEWAYS_FLAGS_NO_SPANS);
    } else {
        if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 4) {
            xhprof_enable(XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS);
        } else {
            xhprof_enable(XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
        }
    }
}
register_shutdown_function(function () {
    if (extension_loaded('uprofiler')) {
        $data['profile'] = uprofiler_disable();
    } else {
        if (extension_loaded('tideways')) {
            $data['profile'] = tideways_disable();
        } else {
            $data['profile'] = xhprof_disable();
        }
    }
    // ignore_user_abort(true) allows your PHP script to continue executing, even if the user has terminated their request.
    // Further Reading: http://blog.preinheimer.com/index.php?/archives/248-When-does-a-user-abort.html
    // flush() asks PHP to send any data remaining in the output buffers. This is normally done when the script completes, but
    // since we're delaying that a bit by dealing with the xhprof stuff, we'll do it now to avoid making the user wait.
    ignore_user_abort(true);
    flush();
    if (!defined('XHGUI_ROOT_DIR')) {
        require dirname(dirname(__FILE__)) . '/src/bootstrap.php';
    }
    $uri = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : null;
    if (empty($uri) && isset($_SERVER['argv'])) {
Example #3
0
/**
 * Stop profiling, gathering results and storing them
 */
function profiling_stop()
{
    global $CFG, $DB, $SCRIPT;
    // If profiling isn't available, nothing to stop
    if (!extension_loaded('xhprof') && !extension_loaded('tideways')) {
        return false;
    }
    // If profiling isn't enabled, nothing to stop
    if (empty($CFG->profilingenabled) && empty($CFG->earlyprofilingenabled)) {
        return false;
    }
    // If profiling is not running or is already saved, nothing to stop
    if (!profiling_is_running() || profiling_is_saved()) {
        return false;
    }
    // Set script (from global if available, else our own)
    $script = !empty($SCRIPT) ? $SCRIPT : profiling_get_script();
    // Arrived here, profiling is running, stop and save everything
    profiling_is_running(false);
    if (extension_loaded('tideways')) {
        $data = tideways_disable();
    } else {
        $data = xhprof_disable();
    }
    // We only save the run after ensuring the DB table exists
    // (this prevents problems with profiling runs enabled in
    // config.php before Moodle is installed. Rare but...
    $tables = $DB->get_tables();
    if (!in_array('profiling', $tables)) {
        return false;
    }
    $run = new moodle_xhprofrun();
    $run->prepare_run($script);
    $runid = $run->save_run($data, null);
    profiling_is_saved(true);
    // Prune old runs
    profiling_prune_old_runs($runid);
    // Finished, return true
    return true;
}
Example #4
0
 /**
  * Stop all profiling actions and submit collected data.
  */
 public static function stop()
 {
     if (self::$mode === self::MODE_NONE) {
         return;
     }
     $mode = self::$mode;
     if (self::$trace['tx'] === 'default' && self::$extension === self::EXTENSION_TIDEWAYS) {
         self::$trace['tx'] = tideways_transaction_name() ?: 'default';
     }
     if (function_exists('tideways_last_detected_exception') && ($exception = tideways_last_detected_exception())) {
         self::logException($exception);
     } elseif (function_exists("http_response_code") && http_response_code() >= 500) {
         self::logFatal("PHP request set error HTTP response code to '" . http_response_code() . "'.", "", 0, E_USER_ERROR);
     }
     $profilingData = array();
     if (($mode & self::MODE_FULL) > 0) {
         if (self::$extension === self::EXTENSION_TIDEWAYS) {
             $profilingData = tideways_disable();
         } elseif (self::$extension === self::EXTENSION_XHPROF) {
             $profilingData = xhprof_disable();
             self::$currentRootSpan->stopTimer();
         }
         $annotations = array('mem' => ceil(memory_get_peak_usage() / 1024));
         if (self::$extension === self::EXTENSION_TIDEWAYS) {
             $annotations['xhpv'] = phpversion('tideways');
         } elseif (self::$extension === self::EXTENSION_XHPROF) {
             $annotations['xhpv'] = phpversion('xhprof');
         }
         if (extension_loaded('xdebug')) {
             $annotations['xdebug'] = '1';
         }
         if (isset($_SERVER['REQUEST_URI'])) {
             $annotations['title'] = '';
             if (isset($_SERVER['REQUEST_METHOD'])) {
                 $annotations['title'] = $_SERVER["REQUEST_METHOD"] . ' ';
             }
             if (isset($_SERVER['HTTP_HOST'])) {
                 $annotations['title'] .= (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . self::getRequestUri();
             } elseif (isset($_SERVER['SERVER_ADDR'])) {
                 $annotations['title'] .= (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['SERVER_ADDR'] . self::getRequestUri();
             }
         } elseif (php_sapi_name() === "cli") {
             $annotations['title'] = basename($_SERVER['argv'][0]);
         }
     } else {
         self::$currentRootSpan->stopTimer();
         $annotations = array('mem' => ceil(memory_get_peak_usage() / 1024));
     }
     self::$currentRootSpan->annotate($annotations);
     if (($mode & self::MODE_PROFILING) > 0) {
         self::$trace['profdata'] = $profilingData ?: array();
     }
     self::$mode = self::MODE_NONE;
     $spans = self::$currentRootSpan->getSpans();
     if (self::$error === true || ($mode & self::MODE_FULL) > 0) {
         self::$trace['spans'] = $spans;
         self::$backend->socketStore(self::$trace);
     } else {
         self::$trace['spans'] = isset($spans[0]) ? array($spans[0]) : array();
         // prevent flooding udp by accident
         self::$backend->udpStore(self::$trace);
     }
     self::$trace = null;
     // free memory
 }
 /**
  * Stop profiling if we where profiling.
  *
  * @param string $serverName The server name the request is running on, or cli for command line.
  * @param string $uri        The requested uri / command name.
  *
  * @return bool
  */
 public function stopProfiling($serverName, $uri)
 {
     if (isset($this->data['xhprof'])) {
         return $this->data['xhprof'];
     }
     if (!$this->collecting) {
         return $this->data['xhprof'] ? $this->data['xhprof'] : false;
     }
     $this->collecting = false;
     if (function_exists('xhprof_disable')) {
         $xhprof_data = xhprof_disable();
     } else {
         $xhprof_data = tideways_disable();
     }
     if ($this->logger) {
         $this->logger->debug('Disabled XHProf');
     }
     $xhprof_runs = $this->createRun($serverName, $uri);
     $source = null;
     if ($xhprof_runs instanceof \XHProfRuns_Default) {
         $source = $this->sanitizeUriForSource($uri);
     }
     $this->runId = $xhprof_runs->save_run($xhprof_data, $source);
     $this->data = array('xhprof' => $this->runId, 'source' => $source);
     if ($xhprof_runs instanceof XhguiRuns_Document) {
         $this->data['xhprof_url'] = $this->container->getParameter('mjr_library_profiler.location_web') . '/run/view?id=' . $this->data['xhprof'];
     } else {
         $this->data['xhprof_url'] = $this->container->getParameter('mjr_library_profiler.location_web') . '?run=' . $this->data['xhprof'] . '&source=' . $this->data['source'];
     }
     $this->data['system_url'] = 'https://' . $_SERVER['SERVER_NAME'];
     return $this->data['xhprof'];
 }