<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" ><channel><title>Alunduil&#039;s Hosting &#187; optimization</title> <atom:link href="http://www.alunduil.com/tag/optimization/feed/" rel="self" type="application/rss+xml" /><link>http://www.alunduil.com</link> <description>Gentoo Hackery and Other Fun ...</description> <lastBuildDate>Sun, 27 Jun 2010 20:50:03 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0</generator> <item><title>Memcached Remote Memory Sharing</title><link>http://www.alunduil.com/2010/06/24/memcached-remote-memory-sharing/</link> <comments>http://www.alunduil.com/2010/06/24/memcached-remote-memory-sharing/#comments</comments> <pubDate>Thu, 24 Jun 2010 22:15:03 +0000</pubDate> <dc:creator>Alex Brandt</dc:creator> <category><![CDATA[Linux Guides]]></category> <category><![CDATA[apache]]></category> <category><![CDATA[mediawiki]]></category> <category><![CDATA[memcached]]></category> <category><![CDATA[optimization]]></category> <category><![CDATA[php]]></category> <category><![CDATA[web]]></category> <category><![CDATA[wordpress]]></category><guid isPermaLink="false">http://www.alunduil.com/?p=150</guid> <description><![CDATA[Introduction Memcached is a simple key/value memory store that allows values from pretty much any application to be stored in memory for quick retrieval. Most languages have simple APIs that make this very easy from a programming standpoint. This also means that it&#8217;s not quite as simple to configure as APC (but nearly for modern [...]]]></description> <content:encoded><![CDATA[<h1>Introduction</h1><p>Memcached is a simple key/value memory store that allows values from pretty much any application to be stored in memory for quick retrieval.  Most languages have simple APIs that make this very easy from a programming standpoint.  This also means that it&#8217;s not quite as simple to <a href="http://www.alunduil.com/2010/05/26/apc-caching-php/">configure as APC</a> (but nearly for modern applications).</p><h1>Installation and Configuration</h1><p>To install memcached I recommend using your choice of installation (mine being emerge) and then checking the configuration (again in the case of Gentoo) located at /etc/conf.d/memcached.</p><p>Update the file to listen on the interface you want, use the memory you want, etc and then start the daemon up.  Now if you&#8217;re using memcached on the same server as your apache server most webapps will be able to just turn on memcached and connect and work but if you&#8217;re using memcached and apache on separate servers it&#8217;s a little more work.</p><h1>WordPress and memcached</h1><p>I&#8217;ve already touted the wordpress plugin: <a href="http://www.w3-edge.com/wordpress-plugins/w3-total-cache/">W3 Total Cache</a> but unfortunately this plugin doesn&#8217;t allow you to easily configure memcached usage through the web interface.  It defaults to using the memcached server located at 127.0.0.1:11211 which if we&#8217;re using separate servers is less than ideal.  The file we need to modify with a simply sed (or by hand if you prefer knowing what&#8217;s going on) is %{DOCROOT}/wp-content/w3-total-cache-config.php.</p><p>We just need to change the memcached servers to point at our server&#8217;s location before enabling it in the interface.  To easily accomplish this we can use sed:</p><pre><code>sed -i -e 's/127.0.0.1:11211/&lt;host&gt;:&lt;port&gt;/g' %{DOCROOT}/wp-content/w3-total-cache-config.php
</code></pre><h1>Mediawiki and memcached</h1><p>The other application we can easily setup for memcached is mediawiki.  Mediawiki wants us to modify its LocalSettings.php with the following additions:</p><pre><code>## Shared memory settings 
$wgMainCacheType = CACHE_MEMCACHED; 
$wgParserCacheType = CACHE_MEMCACHED; 
$wgMessageCacheType = CACHE_MEMCACHED; 
$wgMemCachedServers = array("giskard.alunduil.com:11211"); 

$wgSessionsInMemcached = true; 
</code></pre><p>These settings simply turn on memcached (CACHE_MEMCACHED) for the various caching areas that mediawiki uses and specifies an array of memcached servers to store that caching information in.</p><p>If you do have more than one memcached server configured and available you can use the following syntax to include all of them with various weights:</p><pre><code>$wgMemCachedServers = array(array("host:port", weight), array("host2:port2", weight));
</code></pre><h1>Conclusion</h1><p>Using memcached can greatly improve the performance of webapps or other applications that don&#8217;t need to present the most up to date information.  This can reduce traffic to an overloaded database or configuration file or whatever other store you want to place a cache in front of.</p> ]]></content:encoded> <wfw:commentRss>http://www.alunduil.com/2010/06/24/memcached-remote-memory-sharing/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>APC Caching PHP</title><link>http://www.alunduil.com/2010/05/26/apc-caching-php/</link> <comments>http://www.alunduil.com/2010/05/26/apc-caching-php/#comments</comments> <pubDate>Thu, 27 May 2010 00:42:57 +0000</pubDate> <dc:creator>Alex Brandt</dc:creator> <category><![CDATA[Linux Guides]]></category> <category><![CDATA[apc]]></category> <category><![CDATA[optimization]]></category> <category><![CDATA[php]]></category> <category><![CDATA[w3 total cache]]></category> <category><![CDATA[web]]></category> <category><![CDATA[wordpress]]></category><guid isPermaLink="false">http://www.alunduil.com/?p=132</guid> <description><![CDATA[Introduction Making PHP run faster is usually pretty easy when you see some of the code that people can write (myself included) but what if we either don&#8217;t want to look at their code or don&#8217;t want to fix the code? What else can we do to improve the runtime and the load time of [...]]]></description> <content:encoded><![CDATA[<h1>Introduction</h1><p>Making PHP run faster is usually pretty easy when you see some of the code that people can write (myself included) but what if we either don&#8217;t want to look at their code or don&#8217;t want to fix the code?  What else can we do to improve the runtime and the load time of PHP web sites (this website included)?</p><p>We&#8217;ll cover how to use APC (Alternative PHP Cache) to help alleviate some of the issues of a slow web site.  Just like <a href="http://www.alunduil.com/2010/05/23/optimizing-the-am-out-of-lamp/">last time</a> we optimized MySQL and talked about optimizing Apache we&#8217;ll continue on the path to getting PHP applications as fast as we can (without touching a line of code).</p><h1>Enter APC</h1><p>APC is PEAR project and a PHP module that after installed and a restart of Apache is already doing some work to make life better.  By default APC turns on op-code caching which saves on the compile and execution times of PHP.  The configuration for this module is pretty straight forward but let&#8217;s look at a copy of the one I&#8217;m using:</p><pre><code>; /etc/php/apache2-php5/ext-active/apc.ini (On Gentoo anyways ...)
extension=apc.so
apc.enabled="1"
apc.shm_segments="4"
apc.shm_size="128"
apc.num_files_hint="1024"
apc.ttl="7200"
apc.user_ttl="7200"
apc.gc_ttl="3600"
apc.cache_by_default="1"
;apc.filters=""
;apc.mmap_file_mask="/tmp/apcphp5.XXXXXX"
apc.slam_defense="0"
apc.file_update_protection="2"
apc.enable_cli="0"
apc.max_file_size="1M"
apc.stat="1"
apc.write_lock="1"
apc.report_autofilter="0"
apc.include_once_override="0"
apc.rfc1867="0"
apc.rfc1867_prefix="upload_"
apc.rfc1867_name="APC_UPLOAD_PROGRESS"
apc.rfc1867_freq="0"
apc.localcache="0"
apc.localcache.size="512"
apc.coredump_unmap="0"
</code></pre><h2>Zooming In</h2><p>These options are documented fairly thoroughly in the <a href="http://php.net/manual/en/apc.configuration.php">APC PHP Manual</a>.</p><p>Most of these options can stay at their defaults and provide a pleasant experience but if we have the memory we should probably tweak these to get more out of them: * apc.shm_segments &#8211; The number of chunks to use from /dev/shm. * apc.shm_size &#8211; The size of aforementioned chunks.</p><p>These two parameters are the heart of APC and dictate the memory usage you&#8217;ll get out of it.  If you take segments*size you&#8217;ll get the maximum amount of shm space used by APC for the cache.  A limitation on shm_size is usually in place through the kernel and to determine what this is you can simply <code>cat /proc/sys/kernel/shmmax</code> (remember this command prints out B and APC expects MB).</p><h1>Further Caching</h1><p>APC also has an object cache available which can be used for just about anything but the application has to be modified to support this.  If you&#8217;re using <a href="http://wordpress.org/">WordPress</a> to host your site an excellent plugin for doing this is <a href="http://www.w3-edge.com/wordpress-plugins/w3-total-cache/">W3 Total Cache</a>.</p><h1>Conclusion</h1><p>APC can help you get an edge out of your server but without careful tuning won&#8217;t get you more than the last inch of performance.  Play with the memory settings until it isn&#8217;t over-utilizing memory (have a larger cache than your code base needs) or under-utilizing memory (having a smaller cache than your code base needs) but don&#8217;t forget you don&#8217;t want it to run into your swap space.</p> ]]></content:encoded> <wfw:commentRss>http://www.alunduil.com/2010/05/26/apc-caching-php/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Optimizing the AM out of LAMP</title><link>http://www.alunduil.com/2010/05/23/optimizing-the-am-out-of-lamp/</link> <comments>http://www.alunduil.com/2010/05/23/optimizing-the-am-out-of-lamp/#comments</comments> <pubDate>Mon, 24 May 2010 02:22:34 +0000</pubDate> <dc:creator>Alex Brandt</dc:creator> <category><![CDATA[Linux Guides]]></category> <category><![CDATA[apache]]></category> <category><![CDATA[efficiency]]></category> <category><![CDATA[lamp]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[optimization]]></category> <category><![CDATA[php]]></category> <category><![CDATA[web]]></category><guid isPermaLink="false">http://www.alunduil.com/?p=127</guid> <description><![CDATA[A hot topic for anyone running MySQL and Apache on any machine is optimization and what I hope to accomplish is a quick explanation of how to optimize MySQL and where to tweak Apache to help with it&#8217;s operational efficiency as well. First things first &#8230; A few common things to possibly turn on, install, [...]]]></description> <content:encoded><![CDATA[<p>A hot topic for anyone running MySQL and Apache on any machine is optimization and what I hope to accomplish is a quick explanation of how to optimize MySQL and where to tweak Apache to help with it&#8217;s operational efficiency as well.</p><h1>First things first &#8230;</h1><p>A few common things to possibly turn on, install, or enable:</p><ul><li>APC (Another PHP Cache)</li><li>Memcached</li></ul><h1>MySQL Optimization</h1><p>The easiest way (and the quickest) to determine what can be done to optimize MySQL is to use a script called mysqltuner.pl.  The really interesting thing about this script is that to get it you simply run the following:</p><pre><code>wget mysqltuner.pl
</code></pre><p>After you&#8217;ve gotten this script run it with your perl interpreter:</p><pre><code>perl mysqltuner.pl
</code></pre><p>It will prompt you for your username and password for mysql and then print out a nice report outlining how your mysql daemon has been running.  Here&#8217;s an example of this output:</p><pre><code> &gt;&gt;  MySQLTuner 1.0.1 - Major Hayden &lt;major@mhtx.net&gt;
 &gt;&gt;  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 &gt;&gt;  Run with '--help' for additional options and output filtering
Please enter your MySQL administrative login: root
Please enter your MySQL administrative password: 

-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.0.90-log
[OK] Operating on 32-bit architecture with less than 2GB RAM

-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster 
[--] Data in MyISAM tables: 28M (Tables: 96)
[--] Data in InnoDB tables: 1M (Tables: 55)
[!!] Total fragmented tables: 2

-------- Performance Metrics -------------------------------------------------
[--] Up for: 1h 39m 49s (24K q [4.163 qps], 354 conn, TX: 9M, RX: 34M)
[--] Reads / Writes: 60% / 40%
[--] Total buffers: 148.6M global + 960.0K per thread (100 max threads)
[OK] Maximum possible memory usage: 242.3M (11% of installed RAM)
[OK] Slow queries: 0% (0/24K)
[OK] Highest usage of available connections: 28% (28/100)
[OK] Key buffer size / total MyISAM indexes: 64.0K/6.3M
[OK] Key buffer hit rate: 99.6% (164K cached / 682 reads)
[OK] Query cache efficiency: 81.3% (14K cached / 17K selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 463 sorts)
[!!] Joins performed without indexes: 42
[OK] Temporary tables created on disk: 12% (54 on disk / 417 total)
[OK] Thread cache hit rate: 70% (106 created / 354 connections)
[!!] Table cache hit rate: 2% (48 open / 2K opened)
[OK] Open file limit used: 9% (95/1K)
[OK] Table locks acquired immediately: 98% (7K immediate / 7K locks)
[!!] Connections aborted: 7%
[OK] InnoDB data size / buffer pool: 1.0M/4.0M

-------- Recommendations -----------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    MySQL started within last 24 hours - recommendations may be inaccurate
    Adjust your join queries to always utilize indexes
    Increase table_cache gradually to avoid file descriptor limits
    Your applications are not closing MySQL connections properly
Variables to adjust:
    join_buffer_size (&gt; 128.0K, or always use indexes with joins)
    table_cache (&gt; 48)
</code></pre><p>The first thing to notice is that mysql should be running for 24 to 48 hours before you trust the results of this script the second thing to acknowledge is that it isn&#8217;t omniscient.  Understanding how these parameters affect your memory footprint and your runtime efficiency are what we&#8217;re most interested in though.</p><h2>Glossary of Parameters</h2><p>The best source of my.cnf parameters is of course the MySQL documentation but I&#8217;ll clarify a few of the more pertinent items from the above output.</p><ul><li>Maximum Possible Memory &#8211; This is not the actual hard limit on your memory usage but a calculated theoretical maximum based on the parameters in your my.cnf and if this is over ~90% you may use your swap more than you want to.  After making any changes I suggest you check this to make sure you haven&#8217;t over-allocated yourself.</li><li>Highest Usage of Available Connections &#8211; This is the historical max connections used by mysql and if it&#8217;s at 100% this isn&#8217;t a guarantee that you need to increase max_connections but may just indicate that your connections spiked once.  That being said I recommend following its advice if you have the memory for it.</li><li>Query Cache Efficiency &#8211; The hit rate for the cache on your queries (I hope you have enabled).  As with any hash table if you&#8217;re not hitting what&#8217;s in the top bucket you need to increase the number of buckets.</li><li>Temporary Tables Created on Disk &#8211; This can be a finicky parameter to get right and is the first place I would recommend cutting if you don&#8217;t have the memory to run mysql like you would want, but if you do have the memory then up this as far as you can within reason.</li><li>Thread Cache Hit Rate &#8211; Just like any other cache.</li><li>Table Cache Hit Rate &#8211; Just like any other cache.</li></ul><h2>Fragmented Tables</h2><p>If you noticed the fragmented tables along with the recommendation to run OPTIMIZE TABLE that&#8217;s not something that will typically slow mysql down noticeably with today&#8217;s disks but if you want to defragment them I recommend reading <a href="http://www.dufault.info/blog/a-script-to-optimize-fragmented-tables-in-mysql/">this article on the topic</a>.</p><h1>Apache Tweaking</h1><p>The following parameters are usually in httpd.conf unless you run a distribution that organizes apache in an easier to work with manner (my preference of course is Gentoo which stores these parameters in: 00_mpm.conf).</p><p>The parameters for tweaking apache&#8217;s processes depend on which worker module you have in use.  The following is the pertinent sections from my configuration:</p><pre><code># prefork MPM
# This is the default MPM if USE=-threads
#
# MinSpareServers: Minimum number of idle child server processes
# MaxSpareServers: Maximum number of idle child server processes
&lt;IfModule mpm_prefork_module&gt;
        StartServers            5
        MinSpareServers         5
        MaxSpareServers         10
        MaxClients                      50
        MaxRequestsPerChild     500
&lt;/IfModule&gt;

# worker MPM
# This is the default MPM if USE=threads
#
# MinSpareThreads: Minimum number of idle threads available to handle request spikes
# MaxSpareThreads: Maximum number of idle threads
# ThreadsPerChild: Number of threads created by each child process
&lt;IfModule mpm_worker_module&gt;
        StartServers            2
        MinSpareThreads         15
        MaxSpareThreads         50
        ThreadsPerChild         25
        MaxClients                      150
        MaxRequestsPerChild     1000
&lt;/IfModule&gt;
</code></pre><p>These parameters are pretty self explanatory but just for completeness&#8217; sake I&#8217;ll do a small recap on these here:</p><ul><li>StartServers &#8211; The number of servers to have running and handling connections at first launch.</li><li>MinSpareServers &#8211; The minimum number of servers to have running not currently handling connections.</li><li>MaxSpareServers &#8211; The maximum number of servers to have running not currently handling connections.</li><li>MaxClients &#8211; The maximum number of clients that can simultaneously connect to Apache.</li><li>MaxRequestsPerChild &#8211; The maximum number of requests that a child will respond to before terminating.</li></ul><p>Using these parameters we can control the amount of memory that Apache will consume at the cost of other things we may want to have like lots of connections or many sequential requests.</p><h1>Conclusion</h1><p>There are a lot of different things that can be done for a server that seems to be performing less than ideally and I&#8217;ve only covered a fraction of things that can happen.  As always with this topic the situation will change based on your needs and how your server runs but at least now you may have a starting point for what to modify after you&#8217;ve troubleshooted every other thing that could go wrong.</p> ]]></content:encoded> <wfw:commentRss>http://www.alunduil.com/2010/05/23/optimizing-the-am-out-of-lamp/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Optimizing Gentoo CFLAGS</title><link>http://www.alunduil.com/2009/12/18/optimizing-gentoo-cflags/</link> <comments>http://www.alunduil.com/2009/12/18/optimizing-gentoo-cflags/#comments</comments> <pubDate>Fri, 18 Dec 2009 06:39:52 +0000</pubDate> <dc:creator>Alex Brandt</dc:creator> <category><![CDATA[Linux Guides]]></category> <category><![CDATA[cflags]]></category> <category><![CDATA[gentoo]]></category> <category><![CDATA[instructions]]></category> <category><![CDATA[isa]]></category> <category><![CDATA[optimization]]></category><guid isPermaLink="false">http://www.alunduil.com/?p=63</guid> <description><![CDATA[Starting Off Make sure you&#8217;ve at least looked at the following document: [http://www.gentoo.org/doc/en/gcc-optimization.xml](Gentoo Optimization Guide). Checking Flags The only real change we want to make to our system are simple things we know will not break it. Thus, we only look at making sure that we have the correct flags for enabling all of the [...]]]></description> <content:encoded><![CDATA[<h1>Starting Off</h1><p>Make sure you&#8217;ve at least looked at the following document: [http://www.gentoo.org/doc/en/gcc-optimization.xml](Gentoo Optimization Guide).</p><h1>Checking Flags</h1><p>The only real change we want to make to our system are simple things we know will not break it.  Thus, we only look at making sure that we have the correct flags for enabling all of the instruction sets that our processor has available.</p><h2>Checking Processor</h2><pre><code>grep flags /proc/cpuinfo | grep uniq
</code></pre><p>This pulls out all of the features of the processor as detected by the kernel.  Example output is shown (line breaks added for readability):</p><pre><code>alunduil@elijah ~ $ grep flags /proc/cpuinfo | uniq
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr 
pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall 
nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow rep_good 
extd_apicid pni cx16 lahf_lm cmp_legacy svm extapic 
cr8_legacy 3dnowprefetch
</code></pre><h2>Checking Default Flags</h2><p>gcc does a great job of determining what flags should be set by using the new -march=native flag (which you should have set by this point).  Using the following command we can double check that all of the instructions we want enabled are enabled:</p><pre><code>gcc -Q -c -v -march=native --help=target | grep disabled
</code></pre><p>If anything appears in the resulting list it&#8217;s not enabled and should be enabled by adding the appropriate -m flag.  For my cpu this results in: -msse3 -m3dnow.</p><h1>Putting it Together</h1><p>By adding the original guide with a check for instructions we allow gcc to utilize the instructions that were specifically created for uses we may have (multimedia, extended math, etc).</p><p>The resulting CFLAG variable that I placed in my make.conf from the above discussion:</p><pre><code>CFLAGS="-march=native -O2 -pipe -msse3 -m3dnow"
</code></pre>]]></content:encoded> <wfw:commentRss>http://www.alunduil.com/2009/12/18/optimizing-gentoo-cflags/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using apc
Page Caching using memcached (user agent is rejected)
Database Caching 6/12 queries in 0.052 seconds using memcached

Served from: www.alunduil.com @ 2010-07-30 19:42:31 -->