I'm trying to debug why a PHP of mine is using so much of the CPU.
After running strace like this:
strace -e open,close php /scripName.php
It shows me all of the files that it uses and when I think it reaches the part of the code where it uses a MySql connection, it returns this:
open("/etc/host.conf", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/etc/resolv.conf", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/lib/i386-linux-gnu/libnss_dns.so.2", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/etc/resolv.conf", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
close(3) = 0
open("/usr/share/mysql/charsets/Index.xml", O_RDONLY|O_LARGEFILE) = 4
close(4) = 0
close(3) = 0
open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
close(3) = 0
close(3) = 0
open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
close(3) = 0
close(3) = 0
open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
close(3) = 0
close(3) = 0
open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
close(3) = 0
close(3) = 0
and a LOT more of those
open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
close(3) = 0
close(3) = 0
each one taking quite a bit of time actually.
I tried pinpointing the problem by commenting lines until this didn't happen anymore, and I reached this class:
<?php
include_once 'config.php';
class db extends PDO {
public $dbtipo = DBTIPO;
public $host = DBHOST;
public $user = DBUSER;
public $pass= DBPASS;
public $db = DBNAME;
function __construct(){
$dns = $this->dbtipo.':host='.$this->host.";dbname=".$this->db;
parent::__construct( $dns, $this->user, $this->pass ,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
}
?>
if I comment this line:
parent::__construct( $dns, $this->user, $this->pass ,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
Then this doesn't happen anymore.
FYI, this class is used in another one like this:
public function getDataFromDb(){
$connection = new db();
$query = $connection->prepare("SELECT * FROM table");
$query->execute();
return $query;
}
The constants are defined in the config.php file as this:
define('DBTIPO', 'mysql');
define('DBHOST', 'mysite.com');
define('DBUSER', 'user');
define('DBPASS', 'password');
define('DBNAME', 'database_name');
Any ideas?