setFilePHID($file_phid) ->setSampleRate($sample_rate) ->setUsTotal($access_log->getData('T')) ->setHostname($access_log->getData('h')) ->setRequestPath($access_log->getData('U')) ->setController($access_log->getData('C')) ->setUserPHID($access_log->getData('P')); AphrontWriteGuard::allowDangerousUnguardedWrites(true); $caught = null; try { $profile_sample->save(); } catch (Exception $ex) { $caught = $ex; } AphrontWriteGuard::allowDangerousUnguardedWrites(false); if ($caught) { throw $caught; } } public static function hookProfiler() { if (!self::shouldStartProfiler()) { return; } if (!self::isProfilerAvailable()) { return; } if (self::$profilerStarted) { return; } self::startProfiler(); } private static function startProfiler() { self::includeXHProfLib(); xhprof_enable(); self::$profilerStarted = true; self::$profilerRunning = true; } public static function getProfileFilePHID() { self::stopProfiler(); return self::$profileFilePHID; } private static function stopProfiler() { if (!self::isProfilerRunning()) { return; } $data = xhprof_disable(); $data = @json_encode($data); self::$profilerRunning = false; // Since these happen on GET we can't do guarded writes. These also // sometimes happen after we've disposed of the write guard; in this // case we need to disable the whole mechanism. $use_scope = AphrontWriteGuard::isGuardActive(); if ($use_scope) { $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); } else { AphrontWriteGuard::allowDangerousUnguardedWrites(true); } $caught = null; try { $file = call_user_func( array('PhabricatorFile', 'newFromFileData'), $data, array( 'mime-type' => 'application/xhprof', 'name' => 'profile.xhprof', )); } catch (Exception $ex) { $caught = $ex; } if ($use_scope) { unset($unguarded); } else { AphrontWriteGuard::allowDangerousUnguardedWrites(false); } if ($caught) { throw $caught; } self::$profileFilePHID = $file->getPHID(); } }