[CORE] Use monotonic time via hrtime() where applicable

The realtime clock is not reliable when calculating elapsed time.
This commit is contained in:
Alexei Sorokin 2020-01-07 19:48:13 +03:00 committed by Diogo Peralta Cordeiro
parent d467370efb
commit 3951ccbef7
11 changed files with 364 additions and 290 deletions

View File

@ -665,7 +665,7 @@ class Memcached_DataObject extends Safe_DataObject
$string = $this->annotateQuery($string); $string = $this->annotateQuery($string);
} }
$start = microtime(true); $start = hrtime(true);
$fail = false; $fail = false;
$result = null; $result = null;
if (Event::handle('StartDBQuery', array($this, $string, &$result))) { if (Event::handle('StartDBQuery', array($this, $string, &$result))) {
@ -677,7 +677,7 @@ class Memcached_DataObject extends Safe_DataObject
} }
Event::handle('EndDBQuery', array($this, $string, &$result)); Event::handle('EndDBQuery', array($this, $string, &$result));
} }
$delta = microtime(true) - $start; $delta = (hrtime(true) - $start) / 1000000000;
$limit = common_config('db', 'log_slow_queries'); $limit = common_config('db', 'log_slow_queries');
if (($limit > 0 && $delta >= $limit) || common_config('db', 'log_queries')) { if (($limit > 0 && $delta >= $limit) || common_config('db', 'log_queries')) {
@ -978,7 +978,7 @@ class Memcached_DataObject extends Safe_DataObject
$vstr = ''; $vstr = '';
break; break;
} }
// fallthrough // no break
case 'blob': case 'blob':
case 'string': case 'string':
case 'datetime': case 'datetime':

View File

@ -600,11 +600,11 @@ class Action extends HTMLOutputter // lawsuit
public function endHTML() public function endHTML()
{ {
global $_startTime; global $_startCpuTime;
if (isset($_startTime)) { if (isset($_startCpuTime)) {
$endTime = microtime(true); $end_cpu_time = hrtime(true);
$diff = round(($endTime - $_startTime) * 1000); $diff = round(($end_cpu_time - $_startCpuTime) / 1000000);
$this->raw("<!-- ${diff}ms -->"); $this->raw("<!-- ${diff}ms -->");
} }

View File

@ -1,44 +1,38 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* StatusNet, the distributed open-source microblogging tool * Widget for displaying a list of notices.
*
* widget for displaying a list of notices
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* @category Search * @category Search
* @package StatusNet * @package GNUsocial
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @copyright 2009 StatusNet, Inc. * @copyright 2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { defined('GNUSOCIAL') || die();
exit(1);
}
/** /**
* widget-like class for showing JSON search results * Widget-like class for showing JSON search results.
* *
* @category Search * @category Search
* @package StatusNet * @package GNUsocial
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
* *
*/ */
@ -50,14 +44,14 @@ class JSONSearchResultsList
// The below attributes are carefully named so the JSON output from // The below attributes are carefully named so the JSON output from
// this obj matches the output from search.twitter.com // this obj matches the output from search.twitter.com
var $results; public $results;
var $since_id; public $since_id;
var $max_id; public $max_id;
var $refresh_url; public $refresh_url;
var $results_per_page; public $results_per_page;
var $completed_in; public $completed_in;
var $page; public $page;
var $query; public $query;
/** /**
* constructor * constructor
@ -69,7 +63,7 @@ class JSONSearchResultsList
* @param int $since_id only display notices newer than this * @param int $since_id only display notices newer than this
*/ */
function __construct($notice, $query, $rpp, $page, $since_id = 0) public function __construct($notice, $query, $rpp, $page, $since_id = 0)
{ {
$this->notice = $notice; $this->notice = $notice;
$this->query = urlencode($query); $this->query = urlencode($query);
@ -86,12 +80,12 @@ class JSONSearchResultsList
* @return int $count of the search results listed. * @return int $count of the search results listed.
*/ */
function show() public function show()
{ {
$cnt = 0; $cnt = 0;
$this->max_id = 0; $this->max_id = 0;
$time_start = microtime(true); $time_start = hrtime(true);
while ($this->notice->fetch() && $cnt <= $this->rpp) { while ($this->notice->fetch() && $cnt <= $this->rpp) {
$cnt++; $cnt++;
@ -119,8 +113,8 @@ class JSONSearchResultsList
} }
} }
$time_end = microtime(true); $time_end = hrtime(true);
$this->completed_in = $time_end - $time_start; $this->completed_in = ($time_end - $time_start) / 1000000000;
// Set other attrs // Set other attrs
@ -154,13 +148,12 @@ class JSONSearchResultsList
} }
/** /**
* widget for displaying a single JSON search result * Widget for displaying a single JSON search result.
* *
* @category UI * @category UI
* @package StatusNet * @package GNUsocial
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
* @see JSONSearchResultsList * @see JSONSearchResultsList
*/ */
@ -177,17 +170,17 @@ class ResultItem
// The below attributes are carefully named so the JSON output from // The below attributes are carefully named so the JSON output from
// this obj matches the output from search.twitter.com // this obj matches the output from search.twitter.com
var $text; public $text;
var $to_user_id; public $to_user_id;
var $to_user; public $to_user;
var $from_user; public $from_user;
var $id; public $id;
var $from_user_id; public $from_user_id;
var $iso_language_code; public $iso_language_code;
var $source = null; public $source = null;
var $source_link = null; public $source_link = null;
var $profile_image_url; public $profile_image_url;
var $created_at; public $created_at;
/** /**
* constructor * constructor
@ -197,7 +190,7 @@ class ResultItem
* @param Notice $notice The notice we'll display * @param Notice $notice The notice we'll display
*/ */
function __construct($notice) public function __construct($notice)
{ {
$this->notice = $notice; $this->notice = $notice;
$this->profile = $notice->getProfile(); $this->profile = $notice->getProfile();
@ -212,7 +205,7 @@ class ResultItem
* @return void * @return void
*/ */
function buildResult() public function buildResult()
{ {
$this->text = $this->notice->content; $this->text = $this->notice->content;
$replier_profile = null; $replier_profile = null;
@ -251,7 +244,7 @@ class ResultItem
* *
* @return void * @return void
*/ */
function setSourceData() public function setSourceData()
{ {
$source = null; $source = null;
$source_link = null; $source_link = null;

View File

@ -1,32 +1,31 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* StatusNet, the distributed open-source microblogging tool
*
* I/O manager to wrap around socket-reading and polling queue & connection managers. * I/O manager to wrap around socket-reading and polling queue & connection managers.
* *
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category QueueManager * @category QueueManager
* @package StatusNet * @package GNUsocial
* @author Brion Vibber <brion@status.net> * @author Brion Vibber <brion@status.net>
* @copyright 2009 StatusNet, Inc. * @copyright 2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
defined('GNUSOCIAL') || die();
abstract class IoMaster abstract class IoMaster
{ {
public $id; public $id;
@ -68,7 +67,7 @@ abstract class IoMaster
* *
* Switching site configurations is an acceptable side effect. * Switching site configurations is an acceptable side effect.
*/ */
abstract function initManagers(); abstract public function initManagers();
/** /**
* Instantiate an i/o manager class for the current site. * Instantiate an i/o manager class for the current site.
@ -105,7 +104,7 @@ abstract class IoMaster
* Between events or timeouts, pass control back to idle() method * Between events or timeouts, pass control back to idle() method
* to allow for any additional background processing. * to allow for any additional background processing.
*/ */
function service() public function service()
{ {
$this->logState('init'); $this->logState('init');
$this->start(); $this->start();
@ -199,7 +198,7 @@ abstract class IoMaster
* Return fully-parsed soft memory limit in bytes. * Return fully-parsed soft memory limit in bytes.
* @return intval 0 or -1 if not set * @return intval 0 or -1 if not set
*/ */
function softMemoryLimit() public function softMemoryLimit()
{ {
$softLimit = trim(common_config('queue', 'softlimit')); $softLimit = trim(common_config('queue', 'softlimit'));
if (substr($softLimit, -1) == '%') { if (substr($softLimit, -1) == '%') {
@ -242,7 +241,7 @@ abstract class IoMaster
} }
} }
function start() public function start()
{ {
foreach ($this->managers as $index => $manager) { foreach ($this->managers as $index => $manager) {
$manager->start($this); $manager->start($this);
@ -255,7 +254,7 @@ abstract class IoMaster
} }
} }
function finish() public function finish()
{ {
foreach ($this->managers as $manager) { foreach ($this->managers as $manager) {
$manager->finish(); $manager->finish();
@ -266,7 +265,7 @@ abstract class IoMaster
/** /**
* Called during the idle portion of the runloop to see which handlers * Called during the idle portion of the runloop to see which handlers
*/ */
function poll() public function poll()
{ {
foreach ($this->managers as $index => $manager) { foreach ($this->managers as $index => $manager) {
$interval = $manager->pollInterval(); $interval = $manager->pollInterval();
@ -277,7 +276,7 @@ abstract class IoMaster
if (isset($this->pollTimeouts[$index])) { if (isset($this->pollTimeouts[$index])) {
$timeout = $this->pollTimeouts[$index]; $timeout = $this->pollTimeouts[$index];
if (time() - $this->lastPoll[$index] < $timeout) { if (hrtime(true) - $this->lastPoll[$index] < $timeout * 1000000000) {
// Not time to poll yet. // Not time to poll yet.
continue; continue;
} }
@ -286,7 +285,7 @@ abstract class IoMaster
} }
$hit = $manager->poll(); $hit = $manager->poll();
$this->lastPoll[$index] = time(); $this->lastPoll[$index] = hrtime(true);
if ($hit) { if ($hit) {
// Do the next poll quickly, there may be more input! // Do the next poll quickly, there may be more input!
$this->pollTimeouts[$index] = 0; $this->pollTimeouts[$index] = 0;
@ -306,7 +305,7 @@ abstract class IoMaster
* Called after each handled item or empty polling cycle. * Called after each handled item or empty polling cycle.
* This is a good time to e.g. service your XMPP connection. * This is a good time to e.g. service your XMPP connection.
*/ */
function idle() public function idle()
{ {
foreach ($this->managers as $manager) { foreach ($this->managers as $manager) {
$manager->idle(); $manager->idle();
@ -355,6 +354,4 @@ abstract class IoMaster
$this->shutdown = true; $this->shutdown = true;
$this->respawn = true; $this->respawn = true;
} }
} }

View File

@ -2606,11 +2606,11 @@ function common_perf_counter($key, $val=null)
function common_log_perf_counters() function common_log_perf_counters()
{ {
if (common_config('site', 'logperf')) { if (common_config('site', 'logperf')) {
global $_startTime, $_perfCounters; global $_startCpuTime, $_perfCounters;
if (isset($_startTime)) { if (isset($_startCpuTime)) {
$endTime = microtime(true); $end_cpu_time = hrtime(true);
$diff = round(($endTime - $_startTime) * 1000); $diff = round(($end_cpu_time - $_startCpuTime) / 1000000);
common_log(LOG_DEBUG, "PERF runtime: ${diff}ms"); common_log(LOG_DEBUG, "PERF runtime: ${diff}ms");
} }
$counters = $_perfCounters; $counters = $_perfCounters;

View File

@ -1,4 +1,19 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* GNU social cron-on-visit class * GNU social cron-on-visit class
* *
@ -8,10 +23,12 @@
* @category Cron * @category Cron
* @package GNUsocial * @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se> * @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2013 Free Software Foundation, Inc. * @copyright 2013 Free Software Foundation, Inc http://www.fsf.or
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
defined('GNUSOCIAL') || die();
class Cronish class Cronish
{ {
/** /**
@ -37,15 +54,15 @@ class Cronish
$found = $lastrun->find(true); $found = $lastrun->find(true);
if (!$found) { if (!$found) {
$lastrun->value = time(); $lastrun->value = hrtime(true);
if ($lastrun->insert() === false) { if ($lastrun->insert() === false) {
common_log(LOG_WARNING, "Could not save 'cron' setting '{$name}'"); common_log(LOG_WARNING, "Could not save 'cron' setting '{$name}'");
continue; continue;
} }
$run = true; $run = true;
} elseif ($lastrun->value < time() - $interval) { } elseif ($lastrun->value < hrtime(true) - $interval * 1000000000) {
$orig = clone($lastrun); $orig = clone($lastrun);
$lastrun->value = time(); $lastrun->value = hrtime(true);
$lastrun->update($orig); $lastrun->update($orig);
$run = true; $run = true;
} }

View File

@ -1,48 +1,42 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* StatusNet, the distributed open-source microblogging tool * Plugin to convert string locations to Geonames IDs and vice versa.
*
* Plugin to convert string locations to Geonames IDs and vice versa
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* @category Action * @category Action
* @package StatusNet * @package GNUsocial
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @copyright 2009 StatusNet Inc. * @copyright 2009 StatusNet Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
if (!defined('STATUSNET')) { defined('GNUSOCIAL') || die();
exit(1);
}
/** /**
* Plugin to convert string locations to Geonames IDs and vice versa * Plugin to convert string locations to Geonames IDs and vice versa.
* *
* This handles most of the events that Location class emits. It uses * This handles most of the events that Location class emits. It uses
* the geonames.org Web service to convert names like 'Montreal, Quebec, Canada' * the geonames.org Web service to convert names like 'Montreal, Quebec, Canada'
* into IDs and lat/lon pairs. * into IDs and lat/lon pairs.
* *
* @category Plugin * @category Plugin
* @package StatusNet * @package GNUsocial
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
* *
* @seeAlso Location * @seeAlso Location
*/ */
@ -61,7 +55,7 @@ class GeonamesPlugin extends Plugin
public $cachePrefix = null; // Optional shared memcache prefix override public $cachePrefix = null; // Optional shared memcache prefix override
// to share lookups between local instances. // to share lookups between local instances.
protected $lastTimeout = null; // timestamp of last web service timeout protected $lastTimeout = null; // CPU time of last web service timeout
/** /**
* convert a name into a Location object * convert a name into a Location object
@ -72,10 +66,12 @@ class GeonamesPlugin extends Plugin
* *
* @return boolean whether to continue (results in $location) * @return boolean whether to continue (results in $location)
*/ */
function onLocationFromName($name, $language, &$location) public function onLocationFromName($name, $language, &$location)
{ {
$loc = $this->getCache(array('name' => $name, $loc = $this->getCache([
'language' => $language)); 'name' => $name,
'language' => $language,
]);
if ($loc !== false) { if ($loc !== false) {
$location = $loc; $location = $loc;
@ -83,11 +79,15 @@ class GeonamesPlugin extends Plugin
} }
try { try {
$geonames = $this->getGeonames('search', $geonames = $this->getGeonames(
array('maxRows' => 1, 'search',
[
'maxRows' => 1,
'q' => $name, 'q' => $name,
'lang' => $language, 'lang' => $language,
'type' => 'xml')); 'type' => 'xml',
]
);
} catch (Exception $e) { } catch (Exception $e) {
$this->log(LOG_WARNING, "Error for $name: " . $e->getMessage()); $this->log(LOG_WARNING, "Error for $name: " . $e->getMessage());
return true; return true;
@ -95,9 +95,13 @@ class GeonamesPlugin extends Plugin
if (count($geonames) == 0) { if (count($geonames) == 0) {
// no results // no results
$this->setCache(array('name' => $name, $this->setCache(
'language' => $language), [
null); 'name' => $name,
'language' => $language,
],
null
);
return true; return true;
} }
@ -111,9 +115,13 @@ class GeonamesPlugin extends Plugin
$location->location_id = (string)$n->geonameId; $location->location_id = (string)$n->geonameId;
$location->location_ns = self::LOCATION_NS; $location->location_ns = self::LOCATION_NS;
$this->setCache(array('name' => $name, $this->setCache(
'language' => $language), [
$location); 'name' => $name,
'language' => $language,
],
$location
);
// handled, don't continue processing! // handled, don't continue processing!
return false; return false;
@ -129,7 +137,7 @@ class GeonamesPlugin extends Plugin
* *
* @return boolean whether to continue (results in $location) * @return boolean whether to continue (results in $location)
*/ */
function onLocationFromId($id, $ns, $language, &$location) public function onLocationFromId($id, $ns, $language, &$location)
{ {
if ($ns != self::LOCATION_NS) { if ($ns != self::LOCATION_NS) {
// It's not one of our IDs... keep processing // It's not one of our IDs... keep processing
@ -144,9 +152,13 @@ class GeonamesPlugin extends Plugin
} }
try { try {
$geonames = $this->getGeonames('hierarchy', $geonames = $this->getGeonames(
array('geonameId' => $id, 'hierarchy',
'lang' => $language)); [
'geonameId' => $id,
'lang' => $language,
]
);
} catch (Exception $e) { } catch (Exception $e) {
$this->log(LOG_WARNING, "Error for ID $id: " . $e->getMessage()); $this->log(LOG_WARNING, "Error for ID $id: " . $e->getMessage());
return false; return false;
@ -175,8 +187,10 @@ class GeonamesPlugin extends Plugin
$location->names[$language] = implode(', ', array_reverse($parts)); $location->names[$language] = implode(', ', array_reverse($parts));
$this->setCache(array('id' => (string)$last->geonameId), $this->setCache(
$location); ['id' => (string) $last->geonameId],
$location
);
// We're responsible for this namespace; nobody else // We're responsible for this namespace; nobody else
// can resolve it // can resolve it
@ -197,15 +211,14 @@ class GeonamesPlugin extends Plugin
* *
* @return boolean whether to continue (results in $location) * @return boolean whether to continue (results in $location)
*/ */
function onLocationFromLatLon($lat, $lon, $language, &$location) public function onLocationFromLatLon($lat, $lon, $language, &$location)
{ {
// Make sure they're canonical // Make sure they're canonical
$lat = $this->canonical($lat); $lat = $this->canonical($lat);
$lon = $this->canonical($lon); $lon = $this->canonical($lon);
$loc = $this->getCache(array('lat' => $lat, $loc = $this->getCache(['lat' => $lat, 'lon' => $lon]);
'lon' => $lon));
if ($loc !== false) { if ($loc !== false) {
$location = $loc; $location = $loc;
@ -213,10 +226,14 @@ class GeonamesPlugin extends Plugin
} }
try { try {
$geonames = $this->getGeonames('findNearbyPlaceName', $geonames = $this->getGeonames(
array('lat' => $lat, 'findNearbyPlaceName',
[
'lat' => $lat,
'lng' => $lon, 'lng' => $lon,
'lang' => $language)); 'lang' => $language,
]
);
} catch (Exception $e) { } catch (Exception $e) {
$this->log(LOG_WARNING, "Error for coords $lat, $lon: " . $e->getMessage()); $this->log(LOG_WARNING, "Error for coords $lat, $lon: " . $e->getMessage());
return true; return true;
@ -224,9 +241,10 @@ class GeonamesPlugin extends Plugin
if (count($geonames) == 0) { if (count($geonames) == 0) {
// no results // no results
$this->setCache(array('lat' => $lat, $this->setCache(
'lon' => $lon), ['lat' => $lat, 'lon' => $lon],
null); null
);
return true; return true;
} }
@ -253,9 +271,10 @@ class GeonamesPlugin extends Plugin
$location->names[$language] = implode(', ', $parts); $location->names[$language] = implode(', ', $parts);
$this->setCache(array('lat' => $lat, $this->setCache(
'lon' => $lon), ['lat' => $lat, 'lon' => $lon],
$location); $location
);
// Success! We handled it, so no further processing // Success! We handled it, so no further processing
@ -274,7 +293,7 @@ class GeonamesPlugin extends Plugin
* *
* @return boolean whether to continue * @return boolean whether to continue
*/ */
function onLocationNameLanguage($location, $language, &$name) public function onLocationNameLanguage($location, $language, &$name)
{ {
if ($location->location_ns != self::LOCATION_NS) { if ($location->location_ns != self::LOCATION_NS) {
// It's not one of our IDs... keep processing // It's not one of our IDs... keep processing
@ -292,18 +311,26 @@ class GeonamesPlugin extends Plugin
} }
try { try {
$geonames = $this->getGeonames('hierarchy', $geonames = $this->getGeonames(
array('geonameId' => $id, 'hierarchy',
'lang' => $language)); [
'geonameId' => $id,
'lang' => $language,
]
);
} catch (Exception $e) { } catch (Exception $e) {
$this->log(LOG_WARNING, "Error for ID $id: " . $e->getMessage()); $this->log(LOG_WARNING, "Error for ID $id: " . $e->getMessage());
return false; return false;
} }
if (count($geonames) == 0) { if (count($geonames) == 0) {
$this->setCache(array('id' => $id, $this->setCache(
'language' => $language), [
null); 'id' => $id,
'language' => $language,
],
null
);
return false; return false;
} }
@ -323,9 +350,13 @@ class GeonamesPlugin extends Plugin
if (count($parts)) { if (count($parts)) {
$name = implode(', ', array_reverse($parts)); $name = implode(', ', array_reverse($parts));
$this->setCache(array('id' => $id, $this->setCache(
'language' => $language), [
$name); 'id' => $id,
'language' => $language,
],
$name
);
} }
return false; return false;
@ -341,7 +372,7 @@ class GeonamesPlugin extends Plugin
* *
* @return boolean whether to continue * @return boolean whether to continue
*/ */
function onLocationUrl($location, &$url) public function onLocationUrl($location, &$url)
{ {
if ($location->location_ns != self::LOCATION_NS) { if ($location->location_ns != self::LOCATION_NS) {
// It's not one of our IDs... keep processing // It's not one of our IDs... keep processing
@ -364,7 +395,7 @@ class GeonamesPlugin extends Plugin
* *
* @return boolean whether to continue * @return boolean whether to continue
*/ */
function onLocationRdfUrl($location, &$url) public function onLocationRdfUrl($location, &$url)
{ {
if ($location->location_ns != self::LOCATION_NS) { if ($location->location_ns != self::LOCATION_NS) {
// It's not one of our IDs... keep processing // It's not one of our IDs... keep processing
@ -377,7 +408,7 @@ class GeonamesPlugin extends Plugin
return false; return false;
} }
function getCache($attrs) public function getCache($attrs)
{ {
$c = Cache::instance(); $c = Cache::instance();
@ -392,7 +423,7 @@ class GeonamesPlugin extends Plugin
return $value; return $value;
} }
function setCache($attrs, $loc) public function setCache($attrs, $loc)
{ {
$c = Cache::instance(); $c = Cache::instance();
@ -407,7 +438,7 @@ class GeonamesPlugin extends Plugin
return $result; return $result;
} }
function cacheKey($attrs) public function cacheKey($attrs)
{ {
$key = 'geonames:' . $key = 'geonames:' .
implode(',', array_keys($attrs)) . ':'. implode(',', array_keys($attrs)) . ':'.
@ -419,7 +450,7 @@ class GeonamesPlugin extends Plugin
} }
} }
function wsUrl($method, $params) public function wsUrl($method, $params)
{ {
if (!empty($this->username)) { if (!empty($this->username)) {
$params['username'] = $this->username; $params['username'] = $this->username;
@ -431,12 +462,13 @@ class GeonamesPlugin extends Plugin
$str = http_build_query($params, null, '&'); $str = http_build_query($params, null, '&');
return 'http://'.$this->host.'/'.$method.'?'.$str; return "http://{$this->host}/{$method}?{$str}";
} }
function getGeonames($method, $params) public function getGeonames($method, $params)
{ {
if ($this->lastTimeout && (time() - $this->lastTimeout < $this->timeoutWindow)) { if (!is_null($this->lastTimeout)
&& (hrtime(true) - $this->lastTimeout < $this->timeoutWindow * 1000000000)) {
// TRANS: Exception thrown when a geo names service is not used because of a recent timeout. // TRANS: Exception thrown when a geo names service is not used because of a recent timeout.
throw new Exception(_m('Skipping due to recent web service timeout.')); throw new Exception(_m('Skipping due to recent web service timeout.'));
} }
@ -449,7 +481,7 @@ class GeonamesPlugin extends Plugin
$result = $client->get($this->wsUrl($method, $params)); $result = $client->get($this->wsUrl($method, $params));
} catch (Exception $e) { } catch (Exception $e) {
common_log(LOG_ERR, __METHOD__ . ": " . $e->getMessage()); common_log(LOG_ERR, __METHOD__ . ": " . $e->getMessage());
$this->lastTimeout = time(); $this->lastTimeout = hrtime(true);
throw $e; throw $e;
} }
@ -502,7 +534,7 @@ class GeonamesPlugin extends Plugin
return true; return true;
} }
function canonical($coord) public function canonical($coord)
{ {
$coord = rtrim($coord, "0"); $coord = rtrim($coord, "0");
$coord = rtrim($coord, "."); $coord = rtrim($coord, ".");

View File

@ -25,55 +25,55 @@ _bindtextdomain("statusnet", INSTALLDIR . '/locale');
_bindtextdomain("FeedSub", INSTALLDIR . '/plugins/FeedSub/locale'); _bindtextdomain("FeedSub", INSTALLDIR . '/plugins/FeedSub/locale');
$times = 10000; $times = 10000;
$delta = array(); $delta = [];
$start = microtime(true); $start = hrtime(true);
for($i = 0; $i < $times; $i++) { for ($i = 0; $i < $times; ++$i) {
$result = _("Send"); $result = _('Send');
} }
$delta["_"] = array((microtime(true) - $start) / $times, $result); $delta['_'] = [(hrtime(true) - $start) / $times, $result];
$start = microtime(true); $start = hrtime(true);
for($i = 0; $i < $times; $i++) { for ($i = 0; $i < $times; ++$i) {
$result = __("Send"); $result = __('Send');
} }
$delta["__"] = array((microtime(true) - $start) / $times, $result); $delta['__'] = [(hrtime(true) - $start) / $times, $result];
$start = microtime(true); $start = hrtime(true);
for($i = 0; $i < $times; $i++) { for ($i = 0; $i < $times; ++$i) {
$result = dgettext("FeedSub", "Feeds"); $result = dgettext('FeedSub', 'Feeds');
} }
$delta["dgettext"] = array((microtime(true) - $start) / $times, $result); $delta['dgettext'] = [(hrtime(true) - $start) / $times, $result];
$start = microtime(true); $start = hrtime(true);
for($i = 0; $i < $times; $i++) { for ($i = 0; $i < $times; ++$i) {
$result = _dgettext("FeedSub", "Feeds"); $result = _dgettext('FeedSub', 'Feeds');
} }
$delta["_dgettext"] = array((microtime(true) - $start) / $times, $result); $delta['_dgettext'] = [(hrtime(true) - $start) / $times, $result];
$start = microtime(true); $start = hrtime(true);
for($i = 0; $i < $times; $i++) { for ($i = 0; $i < $times; ++$i) {
// TRANS: String in the gettext speed test script. Unimportant. // TRANS: String in the gettext speed test script. Unimportant.
$result = _m("Feeds"); $result = _m('Feeds');
} }
$delta["_m"] = array((microtime(true) - $start) / $times, $result); $delta['_m'] = [(hrtime(true) - $start) / $times, $result];
$start = microtime(true); $start = hrtime(true);
for($i = 0; $i < $times; $i++) { for ($i = 0; $i < $times; ++$i) {
$result = fake("Feeds"); $result = fake('Feeds');
} }
$delta["fake"] = array((microtime(true) - $start) / $times, $result); $delta['fake'] = [(hrtime(true) - $start) / $times, $result];
foreach ($delta as $func => $bits) { foreach ($delta as $func => $bits) {
list($time, $result) = $bits; [$time, $result] = $bits;
$ms = $time * 1000.0; $ms = $time / 1000000;
printf("%10s %2.4fms %s\n", $func, $ms, $result); printf("%10s %2.4fms %s\n", $func, $ms, $result);
} }
function fake($str) { function fake(string $str)
{
return $str; return $str;
} }

View File

@ -1,4 +1,19 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* GNU social queue-manager-on-visit class * GNU social queue-manager-on-visit class
* *
@ -10,10 +25,12 @@
* @category Cron * @category Cron
* @package GNUsocial * @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se> * @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2013 Free Software Foundation, Inc. * @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
defined('GNUSOCIAL') || die();
class OpportunisticQueueManager extends QueueManager class OpportunisticQueueManager extends QueueManager
{ {
protected $qmkey = false; protected $qmkey = false;
@ -21,7 +38,7 @@ class OpportunisticQueueManager extends QueueManager
protected $max_execution_margin = null; // margin to PHP's max_execution_time protected $max_execution_margin = null; // margin to PHP's max_execution_time
protected $max_queue_items = null; protected $max_queue_items = null;
protected $started_at = null; protected $start_cpu_time = null;
protected $handled_items = 0; protected $handled_items = 0;
protected $verbosity = null; protected $verbosity = null;
@ -29,7 +46,8 @@ class OpportunisticQueueManager extends QueueManager
// typically just used for the /main/cron action, only used if php.ini max_execution_time is 0 // typically just used for the /main/cron action, only used if php.ini max_execution_time is 0
const MAXEXECTIME = 20; const MAXEXECTIME = 20;
public function __construct(array $args = []) { public function __construct(array $args = [])
{
foreach (get_class_vars(get_class($this)) as $key=>$val) { foreach (get_class_vars(get_class($this)) as $key=>$val) {
if (array_key_exists($key, $args)) { if (array_key_exists($key, $args)) {
$this->$key = $args[$key]; $this->$key = $args[$key];
@ -37,15 +55,15 @@ class OpportunisticQueueManager extends QueueManager
} }
$this->verifyKey(); $this->verifyKey();
if ($this->started_at === null) { if (is_null($this->start_cpu_time)) {
$this->started_at = time(); $this->start_cpu_time = hrtime(true);
} }
if ($this->max_execution_time === null) { if (is_null($this->max_execution_time)) {
$this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME; $this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME;
} }
if ($this->max_execution_margin === null) { if (is_null($this->max_execution_margin)) {
// think PHP's max exec time, minus this value to have time for timeouts etc. // think PHP's max exec time, minus this value to have time for timeouts etc.
$this->max_execution_margin = common_config('http', 'connect_timeout') + 1; $this->max_execution_margin = common_config('http', 'connect_timeout') + 1;
} }
@ -69,7 +87,7 @@ class OpportunisticQueueManager extends QueueManager
public function canContinue() public function canContinue()
{ {
$time_passed = time() - $this->started_at; $time_passed = (hrtime(true) - $this->start_cpu_time) / 1000000000;
// Only continue if limit values are sane // Only continue if limit values are sane
if ($time_passed <= 0 && (!is_null($this->max_queue_items) && $this->max_queue_items <= 0)) { if ($time_passed <= 0 && (!is_null($this->max_queue_items) && $this->max_queue_items <= 0)) {
@ -121,9 +139,10 @@ class OpportunisticQueueManager extends QueueManager
'handling limit without being out of work.'); 'handling limit without being out of work.');
return true; return true;
} elseif ($this->verbosity > 1) { } elseif ($this->verbosity > 1) {
$time_passed = (hrtime(true) - $this->start_cpu_time) / 1000000000;
common_debug('Opportunistic queue manager did not have time to start ' . common_debug('Opportunistic queue manager did not have time to start ' .
'on this action (max: ' . $this->max_execution_time . "on this action (max: {$this->max_execution_time}" .
' exceeded: ' . abs(time() - $this->started_at) . ').'); " exceeded: {$time_passed}).");
} }
return false; return false;

View File

@ -1,31 +1,36 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/* /*
* StatusNet - the distributed open-source microblogging tool * Check DB queries for filesorts and such and log em.
* Copyright (C) 2011, StatusNet, Inc.
* *
* This program is free software: you can redistribute it and/or modify * @package SQLStatsPlugin
* it under the terms of the GNU Affero General Public License as published by * @author Evan Prodromou <evan@status.net>
* the Free Software Foundation, either version 3 of the License, or * @copyright 2011 StatusNet, Inc.
* (at your option) any later version. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
if (!defined('STATUSNET')) { defined('GNUSOCIAL') || die();
exit(1);
}
/** /**
* Check DB queries for filesorts and such and log em. * Check DB queries for filesorts and such and log em.
* *
* @package SQLStatsPlugin * @package SQLStatsPlugin
* @maintainer Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
class SQLStatsPlugin extends Plugin class SQLStatsPlugin extends Plugin
{ {
@ -49,33 +54,35 @@ class SQLStatsPlugin extends Plugin
return true; return true;
} }
function onStartDBQuery($obj, $query, &$result) public function onStartDBQuery($obj, $query, &$result)
{ {
$this->queryStart = microtime(true); $this->queryStart = hrtime(true);
return true; return true;
} }
function onEndDBQuery($obj, $query, &$result) public function onEndDBQuery($obj, $query, &$result)
{ {
$endTime = microtime(true); $endTime = hrtime(true);
$this->queryTimes[] = round(($endTime - $this->queryStart) * 1000); $this->queryTimes[] = round(($endTime - $this->queryStart) / 1000000);
$this->queries[] = trim(preg_replace('/\s/', ' ', $query)); $this->queries[] = trim(preg_replace('/\s/', ' ', $query));
$this->queryStart = 0; $this->queryStart = 0;
return true; return true;
} }
function cleanup() public function cleanup()
{ {
if (count($this->queryTimes) == 0) { if (count($this->queryTimes) == 0) {
$this->log(LOG_INFO, sprintf('0 queries this hit.')); $this->log(LOG_INFO, sprintf('0 queries this hit.'));
} else { } else {
$this->log(LOG_INFO, sprintf('%d queries this hit (total = %d, avg = %d, max = %d, min = %d)', $this->log(LOG_INFO, sprintf(
'%d queries this hit (total = %d, avg = %d, max = %d, min = %d)',
count($this->queryTimes), count($this->queryTimes),
array_sum($this->queryTimes), array_sum($this->queryTimes),
array_sum($this->queryTimes) / count($this->queryTimes), array_sum($this->queryTimes) / count($this->queryTimes),
max($this->queryTimes), max($this->queryTimes),
min($this->queryTimes))); min($this->queryTimes)
));
} }
$verbose = common_config('sqlstats', 'verbose'); $verbose = common_config('sqlstats', 'verbose');

View File

@ -48,7 +48,8 @@
*/ */
$_startTime = microtime(true); $_startTime = microtime(true);
$_perfCounters = array(); $_startCpuTime = hrtime(true);
$_perfCounters = [];
// We provide all our dependencies through our own autoload. // We provide all our dependencies through our own autoload.
// This will probably be configurable for distributing with // This will probably be configurable for distributing with
@ -99,7 +100,6 @@ function getPath($req)
function handleError($error) function handleError($error)
{ {
try { try {
if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) { if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
return; return;
} }
@ -152,7 +152,6 @@ function handleError($error)
$erraction = new ServerErrorAction($error->getMessage(), 500, $error); $erraction = new ServerErrorAction($error->getMessage(), 500, $error);
} }
$erraction->showPage(); $erraction->showPage();
} catch (Exception $e) { } catch (Exception $e) {
// TRANS: Error message. // TRANS: Error message.
echo _('An error occurred.'); echo _('An error occurred.');
@ -182,12 +181,18 @@ require_once INSTALLDIR . '/lib/util/common.php';
function formatBacktraceLine($n, $line) function formatBacktraceLine($n, $line)
{ {
$out = "#$n "; $out = "#$n ";
if (isset($line['class'])) $out .= $line['class']; if (array_key_exists('class', $line)) {
if (isset($line['type'])) $out .= $line['type']; $out .= $line['class'];
if (isset($line['function'])) $out .= $line['function']; }
if (array_key_exists('type', $line)) {
$out .= $line['type'];
}
if (array_key_exists('function', $line)) {
$out .= $line['function'];
}
$out .= '('; $out .= '(';
if (isset($line['args'])) { if (array_key_exists('args', $line)) {
$args = array(); $args = [];
foreach ($line['args'] as $arg) { foreach ($line['args'] as $arg) {
// debug_print_backtrace seems to use var_export // debug_print_backtrace seems to use var_export
// but this gets *very* verbose! // but this gets *very* verbose!
@ -197,8 +202,12 @@ function formatBacktraceLine($n, $line)
} }
$out .= ')'; $out .= ')';
$out .= ' called at ['; $out .= ' called at [';
if (isset($line['file'])) $out .= $line['file']; if (array_key_exists('file', $line)) {
if (isset($line['line'])) $out .= ':' . $line['line']; $out .= $line['file'];
}
if (array_key_exists('line', $line)) {
$out .= ':' . $line['line'];
}
$out .= ']'; $out .= ']';
return $out; return $out;
} }