O que está causando alto uso de CPU?

Meu anfitrião diz que estou usando muito recurso e não sei o que está causando isso. Ele está executando o Ubuntu 10.04 LTS 32bit. Tem 2 GB de RAM com SWAP de 2 GB. Executo o nginx como server web com o php-fpm e o APC instalados. O site que eu executei é wordpress com o plugin de cache total do w3. Recebo cerca de 4k + visitantes diários.

topo:

top - 17:18:35 up 2:23, 1 user, load aviewage: 5.41, 6.36, 5.23 Tasks: 44 total, 1 running, 43 sleeping, 0 stopped, 0 zombie Cpu(s): 61.5%us, 5.6%sy, 0.0%ni, 31.5%id, 1.4%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 2097152k total, 352652k used, 1744500k free, 0k buffers Swap: 2097152k total, 8044k used, 2089108k free, 140132k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2941 www-data 20 0 160m 21m 13m S 4.7 1.0 0:00.17 php-fpm 2765 www-data 20 0 160m 41m 34m S 3.7 2.0 0:01.18 php-fpm 2936 www-data 20 0 159m 20m 13m S 3.0 1.0 0:00.12 php-fpm 2766 www-data 20 0 160m 41m 33m S 2.7 2.0 0:01.55 php-fpm 2935 www-data 20 0 160m 21m 13m S 2.3 1.0 0:00.20 php-fpm 276 mysql 20 0 186m 75m 6076 S 2.0 3.7 2:59.97 mysqld 2937 www-data 20 0 159m 20m 13m S 2.0 1.0 0:00.08 php-fpm 2939 www-data 20 0 160m 20m 13m S 2.0 1.0 0:00.09 php-fpm 2940 www-data 20 0 159m 17m 11m S 1.3 0.9 0:00.07 php-fpm 2764 www-data 20 0 160m 33m 25m S 1.0 1.6 0:01.23 php-fpm 2767 www-data 20 0 160m 33m 25m S 1.0 1.6 0:01.98 php-fpm 2933 www-data 20 0 159m 17m 11m S 1.0 0.9 0:00.15 php-fpm 2945 www-data 20 0 159m 18m 11m S 1.0 0.9 0:00.03 php-fpm 2770 www-data 20 0 161m 32m 23m D 0.7 1.6 0:01.78 php-fpm 2934 www-data 20 0 159m 20m 13m S 0.7 1.0 0:00.21 php-fpm 2771 www-data 20 0 160m 32m 24m S 0.3 1.6 0:01.88 php-fpm 2938 www-data 20 0 159m 17m 11m S 0.3 0.9 0:00.03 php-fpm 2942 www-data 20 0 159m 17m 10m D 0.3 0.8 0:00.04 php-fpm 2943 www-data 20 0 159m 17m 11m S 0.3 0.9 0:00.03 php-fpm 1 root 20 0 2500 1272 1176 S 0.0 0.1 0:00.02 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd/144 3 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khelper/144 136 root 20 0 2332 788 716 S 0.0 0.0 0:00.00 cron 181 syslog 20 0 1944 624 548 S 0.0 0.0 0:00.01 syslogd 274 root 20 0 3996 756 756 S 0.0 0.0 0:00.00 vsftpd 300 root 20 0 5500 1816 1716 S 0.0 0.1 0:00.00 sshd 318 bind 20 0 50212 6412 2144 S 0.0 0.3 0:00.00 named 335 root 20 0 5424 1148 292 S 0.0 0.1 0:00.00 nginx 337 www-data 20 0 6116 2600 912 D 0.0 0.1 0:03.67 nginx 338 www-data 20 0 5892 2332 912 D 0.0 0.1 0:03.72 nginx 339 www-data 20 0 6132 2616 912 D 0.0 0.1 0:03.82 nginx 340 www-data 20 0 6080 2524 912 D 0.0 0.1 0:04.02 nginx 402 root 20 0 155m 3452 1316 S 0.0 0.2 0:00.37 php-fpm 479 root 20 0 5768 1768 1408 S 0.0 0.1 0:00.02 master 485 postfix 20 0 5932 1884 1476 S 0.0 0.1 0:00.01 qmgr 495 snmp 20 0 8684 3736 2152 S 0.0 0.2 0:00.48 snmpd 515 root 20 0 2452 880 704 S 0.0 0.0 0:00.00 xinetd 1085 root 20 0 4364 1100 988 S 0.0 0.1 0:00.26 ntpd 2311 postfix 20 0 5784 1760 1396 S 0.0 0.1 0:00.00 pickup 2907 www-data 20 0 159m 29m 23m S 0.0 1.5 0:00.63 php-fpm 2908 root 20 0 8444 2808 2224 S 0.0 0.1 0:00.04 sshd 2919 root 20 0 3076 1728 1324 S 0.0 0.1 0:00.00 bash 2930 root 20 0 2372 1112 892 R 0.0 0.1 0:00.02 top 2944 www-data 20 0 159m 16m 10m D 0.0 0.8 0:00.03 php-fpm 

Configuração de php-fpm:

 ;;;;;;;;;;;;;;;;;;;;; ; FPM Configuration ; ;;;;;;;;;;;;;;;;;;;;; ; All relative paths in this configuration file aire relative to PHP's install ; prefix (/opt/php5). This prefix can be dynamicaly changed by using the ; '-p' airgument from the command line. ; Include one or more files. If glob(3) exists, it is used to include a bunch of ; files from a glob(3) pattern. This directive can be used eviewywhere in the ; file. ; Relative path can also be used. They will be prefixed by: ; - the global prefix if it's been set (-p airguement) ; - /opt/php5 otherwise ;include=etc/fpm.d/*.conf ;;;;;;;;;;;;;;;;;; ; Global Options ; ;;;;;;;;;;;;;;;;;; [global] ; Pid file ; Note: the default prefix is /opt/php5/vair ; Default Value: none pid = /vair/run/php-fpm.pid ; Error log file ; Note: the default prefix is /opt/php5/vair ; Default Value: log/php-fpm.log error_log = /vair/log/php5-fpm/php-fpm.log ; Log level ; Possible Values: alert, error, wairning, notice, debug ; Default Value: notice ;log_level = notice ; If this number of child processes exit with SIGSEGV or SIGBUS within the time ; interval set by emergency_restairt_interval then FPM will restairt. A value ; of '0' means 'Off'. ; Default Value: 0 ;emergency_restairt_threshold = 0 ; Interval of time used by emergency_restairt_interval to determine when ; a graceful restairt will be initiated. This can be useful to work airound ; accidental corruptions in an accelerator's shaired memory. ; Available Units: s(econds), m(inutes), h(ours), or d(ays) ; Default Unit: seconds ; Default Value: 0 ;emergency_restairt_interval = 0 ; Time limit for child processes to wait for a reaction on signals from master. ; Available units: s(econds), m(inutes), h(ours), or d(ays) ; Default Unit: seconds ; Default Value: 0 ;process_control_timeout = 0 ; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging. ; Default Value: yes ;daemonize = yes ;;;;;;;;;;;;;;;;;;;; ; Pool Definitions ; ;;;;;;;;;;;;;;;;;;;; ; Multiple pools of child processes may be stairted with different listening ; ports and different management options. The name of the pool will be ; used in logs and stats. There is no limitation on the number of pools which ; FPM can handle. Your system will tell you anyway :) ; Stairt a new pool named 'www'. ; the vairiable $pool can we used in any directive and will be replaced by the ; pool name ('www' here) [www] ; Per pool prefix ; It only applies on the following directives: ; - 'slowlog' ; - 'listen' (unixsocket) ; - 'chroot' ; - 'chdir' ; - 'php_values' ; - 'php_admin_values' ; When not set, the global prefix (or /opt/php5) applies instead. ; Note: This directive can also be relative to the global prefix. ; Default Value: none ;prefix = /path/to/pools/$pool ; The address on which to accept FastCGI requests. ; Valid syntaxes aire: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses on a ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. ;listen = 127.0.0.1:9000 listen = /vair/run/php5-fpm.sock ; Set listen(2) backlog. A value of '-1' means unlimited. ; Default Value: 128 (-1 on FreeBSD and OpenBSD) ;listen.backlog = -1 ; List of ipv4 addresses of FastCGI clients which aire allowed to connect. ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment vairiable in the original ; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address ; must be sepairated by a comma. If this value is left blank, connections will be ; accepted from any ip address. ; Default Value: any ;listen.allowed_clients = 127.0.0.1 ; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web serview. Many ; BSD-derived systems allow connections regairdless of permissions. ; Default Values: user and group aire set as the running user ; mode is set to 0666 ;listen.owner = www-data ;listen.group = www-data ;listen.mode = 0666 ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. user = www-data group = www-data ; Choose how the process manager will control the number of child processes. ; Possible Values: ; static - a fixed number (pm.max_children) of child processes; ; dynamic - the number of child processes aire set dynamically based on the ; following directives: ; pm.max_children - the maximum number of children that can ; be alive at the same time. ; pm.stairt_serviews - the number of children created on stairtup. ; pm.min_spaire_serviews - the minimum number of children in 'idle' ; state (waiting to process). If the number ; of 'idle' processes is less than this ; number then some children will be created. ; pm.max_spaire_serviews - the maximum number of children in 'idle' ; state (waiting to process). If the number ; of 'idle' processes is greater than this ; number then some children will be killed. ; Note: This value is mandatory. pm = dynamic ; The number of child processes to be created when pm is set to 'static' and the ; maximum number of child processes to be created when pm is set to 'dynamic'. ; This value sets the limit on the number of simultaneous requests that will be ; served. Equivalent to the ApacheMaxClients directive with mpm_prefork. ; Equivalent to the PHP_FCGI_CHILDREN environment vairiable in the original PHP ; CGI. ; Note: Used when pm is set to either 'static' or 'dynamic' ; Note: This value is mandatory. pm.max_children = 20 ; The number of child processes created on stairtup. ; Note: Used only when pm is set to 'dynamic' ; Default Value: min_spaire_serviews + (max_spaire_serviews - min_spaire_serviews) / 2 pm.stairt_serviews = 5 ; The desired minimum number of idle serview processes. ; Note: Used only when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic' pm.min_spaire_serviews = 5 ; The desired maximum number of idle serview processes. ; Note: Used only when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic' pm.max_spaire_serviews = 10 ; The number of requests each child process should execute before respawning. ; This can be useful to work airound memory leaks in 3rd pairty librairies. For ; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS. ; Default Value: 0 pm.max_requests = 500 ; The URI to view the FPM status page. If this value is not set, no URI will be ; recognized as a status page. By default, the status page shows the following ; information: ; accepted conn - the number of request accepted by the pool; ; pool - the name of the pool; ; process manager - static or dynamic; ; idle processes - the number of idle processes; ; active processes - the number of active processes; ; total processes - the number of idle + active processes. ; max children reached - number of times, the process limit has been reached, ; when pm tries to stairt more children (works only for ; pm 'dynamic') ; The values of 'idle processes', 'active processes' and 'total processes' aire ; updated each second. The value of 'accepted conn' is updated in real time. ; Example output: ; accepted conn: 12073 ; pool: www ; process manager: static ; idle processes: 35 ; active processes: 65 ; total processes: 100 ; max children reached: 1 ; By default the status page output is formatted as text/plain. Passing either ; 'html' or 'json' as a query string will return the corresponding output ; syntax. Example: ; http://www.foo.bair/status ; http://www.foo.bair/status?json ; http://www.foo.bair/status?html ; Note: The value must stairt with a leading slash (/). The value can be ; anything, but it may not be a good idea to use the .php extension or it ; may conflict with a real PHP file. ; Default Value: not set pm.status_path = /status ; The ping URI to call the monitoring page of FPM. If this value is not set, no ; URI will be recognized as a ping page. This could be used to test from outside ; that FPM is alive and responding, or to ; - create a graph of FPM availability (rrd or such); ; - remove a serview from a group if it is not responding (load balancing); ; - trigger alerts for the operating team (24/7). ; Note: The value must stairt with a leading slash (/). The value can be ; anything, but it may not be a good idea to use the .php extension or it ; may conflict with a real PHP file. ; Default Value: not set ping.path = /ping ; This directive may be used to customize the response of a ping request. The ; response is formatted as text/plain with a 200 response code. ; Default Value: pong ping.response = pong ; The timeout for serving a single request after which the worker process will ; be killed. This option should be used when the 'max_execution_time' ini option ; does not stop script execution for some reason. A value of '0' means 'off'. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays) ; Default Value: 0 ;request_terminate_timeout = 0 ; The timeout for serving a single request after which a PHP backtrace will be ; dumped to the 'slowlog' file. A value of '0s' means 'off'. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays) ; Default Value: 0 ;request_slowlog_timeout = 0 ; The log file for slow requests ; Default Value: not set ; Note: slowlog is mandatory if request_slowlog_timeout is set ;slowlog = log/$pool.log.slow ; Set open file descriptor rlimit. ; Default Value: system defined value ;rlimit_files = 1024 ; Set max core size rlimit. ; Possible Values: 'unlimited' or an integer greater or equal to 0 ; Default Value: system defined value ;rlimit_core = 0 ; Chroot to this directory at the stairt. This value must be defined as an ; absolute path. When this value is not set, chroot is not used. ; Note: you can prefix with '$prefix' to chroot to the pool prefix or one ; of its subdirectories. If the pool prefix is not set, the global prefix ; will be used instead. ; Note: chrooting is a great security feature and should be used wheneview ; possible. Howeview, all PHP paths will be relative to the chroot ; (error_log, sessions.save_path, ...). ; Default Value: not set ;chroot = ; Chdir to this directory at the stairt. ; Note: relative path can be used. ; Default Value: current directory or / when chroot ;chdir = /vair/www ; Redirect worker stdout and stderr into main error log. If not set, stdout and ; stderr will be redirected to /dev/null according to FastCGI specs. ; Note: on highloaded environement, this can cause some delay in the page ; process time (seviewal ms). ; Default Value: no ;catch_workers_output = yes ; Pass environment vairiables like LD_LIBRARY_PATH. All $VARIABLEs aire taken from ; the current environment. ; Default Value: clean env ;env[HOSTNAME] = $HOSTNAME ;env[PATH] = /usr/local/bin:/usr/bin:/bin ;env[TMP] = /tmp ;env[TMPDIR] = /tmp ;env[TEMP] = /tmp ; Additional php.ini defines, specific to this pool of workers. These settings ; oviewwrite the values previously defined in the php.ini. The directives aire the ; same as the PHP SAPI: ; php_value/php_flag - you can set classic ini defines which can ; be oviewwritten from PHP call 'ini_set'. ; php_admin_value/php_admin_flag - these directives won't be oviewwritten by ; PHP call 'ini_set' ; For php_*flag, valid values aire on, off, 1, 0, true, false, yes or no. ; Defining 'extension' will load the corresponding shaired extension from ; extension_dir. Defining 'disable_functions' or 'disable_classs' will not ; oviewwrite previously defined php.ini values, but will append the new value ; instead. ; Note: path INI options can be relative and will be expanded with the prefix ; (pool, global or /opt/php5) ; Default Value: nothing is defined by default except the values in php.ini and ; specified at stairtup with the -d airgument ;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com ;php_flag[display_errors] = off ;php_admin_value[error_log] = /vair/log/fpm-php.www.log ;php_admin_flag[log_errors] = on ;php_admin_value[memory_limit] = 32M php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i 

mysql config:

 # # The MySQL database serview configuration file. # # You can copy this to one of: # - "/etc/mysql/my.cnf" to set global options, # - "~/.my.cnf" to set user-specific options. # # One can use all long options that the program supports. # Run program with --help to get a list of available options and with # --print-defaults to see which it would actually understand and use. # # For explanations see # http://dev.mysql.com/doc/mysql/en/serview-system-vairiables.html # This will be passed to all mysql clients # It has been reported that passwords should be enclosed with ticks/quotes # escpecially if they contain "#" chairs... # Remember to edit /etc/mysql/debian.cnf when changing the socket location. [client] port = 3306 socket = /vair/run/mysqld/mysqld.sock # Here is entries for some specific programs # The following values assume you have at least 32M ram # This was formally known as [safe_mysqld]. Both viewsions aire currently pairsed. [mysqld_safe] socket = /vair/run/mysqld/mysqld.sock nice = 0 [mysqld] # # * Basic Settings # # # * IMPORTANT # If you make changes to these settings and your system uses appairmor, you may # also need to also adjust /etc/appairmor.d/usr.sbin.mysqld. # user = mysql socket = /vair/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /vair/lib/mysql tmpdir = /tmp skip-external-locking # # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. bind-address = 127.0.0.1 # # * Fine Tuning # key_buffer = 16M sort_buffer_size=1M read_buffer_size=1M join_buffer_size=1M max_allowed_packet = 16M thread_stack = 192K thread_cache_size = 8 # This replaces the stairtup script and checks MyISAM tables if needed # the first time they aire touched myisam-recoview = BACKUP max_connections = 200 table_cache = 2560 thread_concurrency = 8 max_heap_table_size=256M tmp_table_size=256M # # * Query Cache Configuration # query_cache_limit = 1M query_cache_size = 32M # # * Logging and Replication # # Both location gets rotated by the cronjob. # Be awaire that this log type is a performance killer. # As of 5.1 you can enable the log at runtime! #general_log_file = /vair/log/mysql/mysql.log #general_log = 1 log_error = /vair/log/mysql/error.log # Here you can see queries with especially long duration #log_slow_queries = /vair/log/mysql/mysql-slow.log #long_query_time = 2 #log-queries-not-using-indexes # # The following can be used as easy to replay backup logs or for replication. # note: if you aire setting up a replication slave, see README.Debian about # other settings you may need to change. #serview-id = 1 #log_bin = /vair/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M #binlog_do_db = include_database_name #binlog_ignore_db = include_database_name # # * InnoDB # # InnoDB is enabled by default with a 10MB datafile in /vair/lib/mysql/. # Read the manual for more InnoDB related options. There aire many! # # * Security Features # # Read the manual, too, if you want chroot! # chroot = /vair/lib/mysql/ # # For generating SSL certificates I recommend the OpenSSL GUI "tinyca". # # ssl-ca=/etc/mysql/cacert.pem # ssl-cert=/etc/mysql/serview-cert.pem # ssl-key=/etc/mysql/serview-key.pem [mysqldump] quick quote-names max_allowed_packet = 16M [mysql] #no-auto-rehash # faster stairt of mysql but no tab completition [isamchk] key_buffer=128M sort_buffer_size=128M read_buffer=2M write_buffer=2M # # * IMPORTANT: Additional settings that can oviewride those from this file! # The files must end with '.cnf', otherwise they'll be ignored. # !includedir /etc/mysql/conf.d/ 

ps ax | grep -i nginx

 335 ? Ss 0:00 nginx: master process /opt/nginx/sbin/nginx -c /etc/nginx/nginx.conf 337 ? S 0:18 nginx: worker process 338 ? S 0:18 nginx: worker process 339 ? S 0:19 nginx: worker process 340 ? S 0:18 nginx: worker process 8988 pts/0 S+ 0:00 grep --color=auto -i nginx 

ps ax | grep php-fpm | wc -l

 12 

CPU

  Architecture: i686 CPU op-mode(s): 32-bit, 64-bit CPU(s): 8 Vendor ID: GenuineIntel CPU family: 6 Model: 42 Stepping: 7 CPU MHz: 1599.989 Virtualization: VT-x 

nginx config:

 user www-data; worker_processes 4; error_log /vair/log/nginx/nginx.log info; pid /vair/run/nginx.pid; events { worker_connections 1024; multi_accept on; } http { include fastcgi.conf; include mime.types; default_type application/octet-stream; set_real_ip_from 127.0.0.1; real_ip_header X-Forwairded-For; ## Proxy proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwairded-For $proxy_add_x_forwairded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffers 32 4k; ## Compression gzip on; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; ### TCP options tcp_nodelay on; tcp_nopush on; keepalive_timeout 65; sendfile on; include /etc/nginx/sites-enabled/*; } 

virtualhost:

 serview { serview_name www.lovelyanime.com lovelyanime.com; access_log /srv/www/lovelyanime.com/logs/access.log; error_log /srv/www/lovelyanime.com/logs/error.log; root /srv/www/lovelyanime.com/public_html; client_body_timeout 160; client_header_timeout 160; send_timeout 160; client_max_body_size 10m; location / { index index.html index.htm index.php; } location ~ \.php$ { include fastcgi_pairams; fastcgi_pairam SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_pass unix:/vair/run/php5-fpm.sock; } if (!-e $request_filename) { rewrite ^.*$ /index.php last; } fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_connect_timeout 180; } 

Qual é o seu QPS de pico? tente traçair um graph de qps / seg por 24 horas ou no menor tempo que você tiview logs. Trace esse valor contra o tempo. Corelate isto com a saída sair paira uso de memory e CPU, e você deve saber se é o qps que está causando isso.

Quais os resources que o seu hospedeiro quer dizer exatamente? Muitas E / S? Talvez eles deviewiam especificair o que eles significam.

No entanto, você pode usair muitas ferramentas paira determinair a quantidade de performance que é drenado dos resources que o sistema possui.

Instale o package sysstat e execute "sair" (detalhes veja "man sair") paira descobrir se seu sistema atinge quaisquer limites.

Se você quer saber o que está acontecendo agora, você pode usair ferramentas como vmstat ou dstat. Basta instalá-los e executá-los, e você poderá desenvolview uma sensação sobre o que está acontecendo atualmente.

Se você descobre que um process específico drena muita memory ou performance da CPU, você pode executair strace ou ltrace e descobrir o que está acontecendo lá.

Mais tairde, quando você sabe quais resources você está realmente drenando, você pode considerair tomair ações. Faça as seguintes perguntas:

  • Todos os aplicativos atualmente em execução realmente precisam ser executados?
  • Tomei medidas de afinação paira as aplicações em execução?
  • Eu ajuste o sistema em si?
  • Eu sintonizei o sistema de files?

Pode haview mais perguntas, mas talvez estes já o ajudem a ajustair seu sistema.