From bf1d43ae6507c10a8f8651c207791157f644d05e Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Thu, 19 May 2011 10:02:55 +0000 Subject: [PATCH] improve efficiency of devices/ports/, add some caching, fix db profiling. git-svn-id: http://www.observium.org/svn/observer/trunk@2349 61d68cd4-352d-0410-923a-c4978735b2b8 --- html/includes/functions.inc.php | 3 +- html/includes/print-interface.inc.php | 5 ++-- html/index.php | 6 +++- html/pages/device/ports.inc.php | 15 +++++++++- includes/common.php | 41 +++++++++++++++++++++++---- includes/dbFacile.php | 13 +++++---- 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/html/includes/functions.inc.php b/html/includes/functions.inc.php index c959d8e31..105d5f436 100644 --- a/html/includes/functions.inc.php +++ b/html/includes/functions.inc.php @@ -250,6 +250,7 @@ function generate_port_link($args, $text = NULL, $type = NULL) if (!isset($args['graph_type'])) { $args['graph_type'] = 'port_bits'; } $class = ifclass($args['ifOperStatus'], $args['ifAdminStatus']); + if (!isset($args['hostname'])) { $args = array_merge($args, device_by_id_cache($args['device_id'])); } $content = "
".$args['hostname']." - " . fixifName($args['label']) . "
"; @@ -273,7 +274,7 @@ function generate_port_link($args, $text = NULL, $type = NULL) $url = generate_port_url($args); - if (port_permitted($args['interface_id'])) { + if (port_permitted($args['interface_id'], $args['device_id'])) { return overlib_link($url, $text, $content, $class); } else { return fixifName($text); diff --git a/html/includes/print-interface.inc.php b/html/includes/print-interface.inc.php index 7d530de01..62b5b19c3 100644 --- a/html/includes/print-interface.inc.php +++ b/html/includes/print-interface.inc.php @@ -225,7 +225,7 @@ foreach(dbFetchRows("SELECT * FROM `ports_stack` WHERE `interface_id_low` = ? an { if($higher_if['interface_id_high']) { - $this_port = get_port_by_ifIndex($device, $higher_if['interface_id_high']); + $this_port = get_port_by_index_cache($device['device_id'], $higher_if['interface_id_high']); echo("$br " . generate_port_link($this_port) . ""); $br = "
"; } @@ -235,13 +235,14 @@ foreach(dbFetchRows("SELECT * FROM `ports_stack` WHERE `interface_id_high` = ? a { if($lower_if['interface_id_low']) { - $this_port = get_port_by_ifIndex($device, $lower_if['interface_id_low']); + $this_port = get_port_by_index_cache($device['device_id'], $lower_if['interface_id_low']); echo("$br " . generate_port_link($this_port) . ""); $br = "
"; } } + unset($int_links, $int_links_v6, $int_links_v4, $int_links_phys, $br); echo(""); diff --git a/html/index.php b/html/index.php index eabc78826..2f4654d21 100755 --- a/html/index.php +++ b/html/index.php @@ -142,7 +142,11 @@ echo('. Copyright © 2006-'. date("Y"). ' by Adam Armstrong. All rights if ($config['page_gen']) { - echo('
MySQL: Cell '.($db_stats['fetchcell']+0).'/'.($db_stats['fetchcell_sec']+0).'s Row '.($db_stats['fetchrow']+0).'/'.($db_stats['fetchrow_sec']+0).'s Rows '.($db_stats['fetchrows']+0).'/'.($db_stats['fetchrows_sec']+0).'s Column '.($db_stats['fetchcol']+0).'/'.($db_stats['fetchcol_sec']+0)); + echo('
MySQL: Cell '.($db_stats['fetchcell']+0).'/'.round($db_stats['fetchcell_sec']+0,2).'s'. + ' Row '.($db_stats['fetchrow']+0). '/'.round($db_stats['fetchrow_sec']+0,2).'s'. + ' Rows '.($db_stats['fetchrows']+0).'/'.round($db_stats['fetchrows_sec']+0,2).'s'. + ' Column '.($db_stats['fetchcol']+0). '/'.round($db_stats['fetchcol_sec']+0,2).'s'); + echo('
Generated in ' . $gentime . ' seconds.'); } diff --git a/html/pages/device/ports.inc.php b/html/pages/device/ports.inc.php index 6e35b4fb9..2c5f1f9d2 100644 --- a/html/pages/device/ports.inc.php +++ b/html/pages/device/ports.inc.php @@ -88,7 +88,20 @@ if ($_GET['optc'] == thumbs) if ($_GET['opta'] == "details") { $port_details = 1; } echo("
"); $i = "1"; - foreach (dbFetchRows("SELECT * FROM `ports` WHERE `device_id` = ? AND `deleted` = '0' ORDER BY `ifIndex` ASC", array($device['device_id'])) as $interface) + + global $port_cache; + global $port_index_cache; + + $ports = dbFetchRows("SELECT * FROM `ports` WHERE `device_id` = ? AND `deleted` = '0' ORDER BY `ifIndex` ASC", array($device['device_id'])); + ### As we've dragged the whole database, lets pre-populate our caches :) + ### FIXME - we should probably split the fetching of link/stack/etc into functions and cache them here too to cut down on single row queries. + foreach($ports as $port) + { + $port_cache[$port['interface_id']] = $port; + $port_index_cache[$port['device_id']][$port['ifIndex']] = $port; + } + + foreach ($ports as $interface) { include("includes/print-interface.inc.php"); $i++; diff --git a/includes/common.php b/includes/common.php index 539084b39..63bda59b0 100644 --- a/includes/common.php +++ b/includes/common.php @@ -48,6 +48,42 @@ function get_sensor_rrd($device, $sensor) return($rrd_file); } +function get_port_by_index_cache($device_id, $ifIndex) +{ + global $port_index_cache; + + if (isset($port_index_cache[$device_id][$ifIndex]) && is_array($port_index_cache[$device_id][$ifIndex])) + { + $port = $port_index_cache[$device_id][$ifIndex]; + } else { + $port = get_port_by_ifIndex($device_id, $ifIndex); + $port_index_cache[$device_id][$ifIndex] = $port; + } + + return $port; +} + +function get_port_by_ifIndex($device_id, $ifIndex) +{ + return dbFetchRow("SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?", array($device_id, $ifIndex)); +} + + +function port_by_id_cache($port_id) +{ + global $port_cache; + + if (isset($port_cache[$port_id]) && is_array($port_cache[$device_id])) + { + $port = $port_cache[$port_id]; + } else { + $port = dbFetchRow("SELECT * FROM `ports` WHERE `interface_id` = ?", array($port_id)); + $port_cache[$port_id] = $port; + } + return $port; +} + + function get_port_by_id($port_id) { if (is_numeric($port_id)) @@ -184,11 +220,6 @@ function getifindexbyid($id) return dbFetchCell("SELECT `ifIndex` FROM `ports` WHERE `interface_id` = ?", array($id)); } -function get_port_by_ifIndex($device, $ifIndex) -{ - return dbFetchRow("SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?", array($device['device_id'], $ifIndex)); -} - function getifbyid($id) { return dbFetchRow("SELECT * FROM `ports` WHERE `interface_id` = ?", array($id)); diff --git a/includes/dbFacile.php b/includes/dbFacile.php index a99318b35..8a5a869e7 100644 --- a/includes/dbFacile.php +++ b/includes/dbFacile.php @@ -136,7 +136,10 @@ function dbDelete($table, $where = null, $parameters = array()) { * */ function dbFetchRows($sql, $parameters = array()) { global $db_stats; + + $time_start = microtime(true); $result = dbQuery($sql, $parameters); + if(mysql_num_rows($result) > 0) { $rows = array(); while($row = mysql_fetch_assoc($result)) { @@ -146,10 +149,9 @@ function dbFetchRows($sql, $parameters = array()) { return $rows; } - $time_start = microtime(true); mysql_free_result($result); - $time_end = microtime(true); + $time_end = microtime(true); $db_stats['fetchrows_sec'] += number_format($time_end - $time_start, 8); $db_stats['fetchrows']++; @@ -181,11 +183,9 @@ function dbFetch($sql, $parameters = array()) { * */ function dbFetchRow($sql = null, $parameters = array()) { global $db_stats; - $time_start = microtime(true); + $time_start = microtime(true); $result = dbQuery($sql, $parameters); - $time_start = microtime(true); - if($result) { $row = mysql_fetch_assoc($result); mysql_free_result($result); @@ -198,6 +198,8 @@ function dbFetchRow($sql = null, $parameters = array()) { } else { return null; } + $time_start = microtime(true); + } /* @@ -206,7 +208,6 @@ function dbFetchRow($sql = null, $parameters = array()) { function dbFetchCell($sql, $parameters = array()) { global $db_stats; $time_start = microtime(true); - $row = dbFetchRow($sql, $parameters); if($row) { return array_shift($row); // shift first field off first row