{"id":3251,"date":"2014-06-24T01:54:18","date_gmt":"2014-06-23T17:54:18","guid":{"rendered":"http:\/\/rmohan.com\/?p=3251"},"modified":"2014-06-24T08:56:20","modified_gmt":"2014-06-24T00:56:20","slug":"maximum-number-of-file-descriptors","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3251","title":{"rendered":"maximum-number-of-file-descriptors solaris"},"content":{"rendered":"<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">Before I get into details here is the bottom line. If you start MySQL on Solaris as a non-root (ie, mysql) user and for some reason you need to adjust the file descriptor resource limit for the parent shell, never use &#8216;ulimit -n&#8217;. This will set both the soft and hard limit and may cause MySQL to adjust the max_connections and table_open_cache configuration variables upon next startup.<\/p>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">Use either:<\/p>\n<pre style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; background-color: #ffffff; -webkit-text-stroke-width: 0px;\"> ulimit -S -n 1024<\/pre>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">or something like:<\/p>\n<pre style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; background-color: #ffffff; -webkit-text-stroke-width: 0px;\"><span class=\"Apple-style-span\" style=\"font-family: Times; font-size: 16px; white-space: normal;\"><span class=\"Apple-converted-space\">\u00a0<\/span><\/span> prctl -n process.max-file-descriptor -t basic -v  1024 -r -i process $$\r\n\r\n\r\n\r\n<\/pre>\n<h2 style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-family: Arial, Verdana, sans-serif; font-style: normal; font-variant: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">The Details<\/h2>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">The default &#8216;basic&#8217; privilege value for the resource control process.max-file-descriptor is 256. This control represents the soft ulimit for file descriptors per process. The default &#8216;privileged&#8217; privilege is set to 65535, which represents the hard ulimit. A non-root user can adjust the soft limit down or up to the hard limit. Unless it has PRIV_SYS_RESOURCE a non-root user can only decrease the hard limit.<\/p>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">Not being aware of the default values I started a sysbench run with 256 threads. It failed with the following message:<\/p>\n<pre style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">FATAL: unable to connect to MySQL server, aborting...\r\nFATAL: error 2001: Can't create UNIX socket (24)\r\nFATAL: failed to connect to database server!\r\nFATAL: thread#252: failed to connect to database server, aborting...\r\n<\/pre>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">Sysbench fails because the action for the basic privilege process.max-file-descriptors is deny. I was running sysbench logged in as mysql. I made the mistake of increasing the ulimit to what I thought was a reasonable 1024 via &#8216;ulimit -n 1024&#8217;. When restarting mysqld it inherited a soft and hard limit of 1024. This had the effect of causing mysqld to recalculate the values for max_connections and table_open_cache as displayed in the error log:<\/p>\n<pre style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">090610  9:44:11 [Warning] Changed limits: max_open_files: 1024  max_connections: 886  table_cache: 64\r\n<\/pre>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">This value is much too small for table_open_cache and is guaranteed to cause heavy contention on LOCK_open as concurrent threads increase.<\/p>\n<h2 style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-family: Arial, Verdana, sans-serif; font-style: normal; font-variant: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">Why did MySQL change table_open_cache and max_connections?<\/h2>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">Upon startup mysqld will calculate the number of open files it wants selecting the greatest of:<\/p>\n<ul style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px 10px 17px; padding: 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">\n<li style=\"margin-left: 15px;\">myisam&#8217;s requirement for 2 file handles per connection\n<ul style=\"margin: 10px 0px 10px 17px; padding: 0px;\">\n<li style=\"margin-left: 15px;\">10+max_connections+table_cache_size\\*2<\/li>\n<\/ul>\n<\/li>\n<li style=\"margin-left: 15px;\">max_connnections\\*5<\/li>\n<li style=\"margin-left: 15px;\">open_files_limit<\/li>\n<\/ul>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">For the purpose of this discussion let&#8217;s call this max_open_files.<\/p>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">It then compares max_open_files with the soft resource limit for file descriptors per process. If the soft limit is less than max_open_files both the soft and hard limit will be set to max_open_files via setrlimit. If setrlimit fails the requested max_open_files is modified to the old soft limit. If the returned max_open_files is less than myisam&#8217;s requirement for 2 file handles per connection and open_files_limit has not been specified, max_connections and table_open_cache will be recalculated to fit within the new boundaries.<\/p>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">Now take the case where I inadvertently set both the soft and hard limit via &#8216;ulimit -n 1024&#8217;. I had set\u00a0 open_table_cache=4096 and max_connections=2049, arbitrary large values so I could set them once and forget about them. In this case max_open_files=max_connections\\*5=10245. When trying to increase the limits to 10245, the setrlimit call fails because I started mysqld as the mysql user and it does not have the privilege to increase the hard limit from 1024 to 10245. In this case max_open_files is set to 1024, which causes mysqld to recalculate max_connections and table_open_cache:<\/p>\n<pre style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">090610  9:44:11 [Warning] Changed limits: max_open_files: 1024  max_connections: 886  table_cache: 64<\/pre>\n<h2 style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-family: Arial, Verdana, sans-serif; font-style: normal; font-variant: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">How to observe process.max-file-descriptor<\/h2>\n<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">You can use either prctl or ulimit to observe the current values of process.max-file-descriptor:<\/p>\n<pre style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">$ prctl -n process.max-file-descriptor  -i process $$\r\nprocess: 20773: -ksh\r\nNAME    PRIVILEGE       VALUE    FLAG   ACTION                       RECIPIENT\r\nprocess.max-file-descriptor\r\n        basic             256       -   deny                             20773\r\n        privileged      65.5K       -   deny                                 -\r\n        system          2.15G     max   deny                                 -\r\n<\/pre>\n<pre style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">$ ulimit -S -n ### soft limit\r\n256\r\n$ ulimit -H -n ### hard limit\r\n65535\r\n\r\n<\/pre>\n<p style=\"font: 14px\/24px 'Open Sans', Helvetica, Arial, sans-serif; margin: 0px 0px 1.71rem; padding: 0px; border: 0px currentColor; color: #444444; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; vertical-align: baseline; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">On running processes:<\/p>\n<p style=\"font: 14px\/24px 'Open Sans', Helvetica, Arial, sans-serif; margin: 0px 0px 1.71rem; padding: 0px; border: 0px currentColor; color: #444444; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; vertical-align: baseline; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\"><code style=\"margin: 0px; padding: 0px; border: 0px currentColor; line-height: 2; font-family: Consolas, Monaco, 'Lucida Console', monospace; font-size: 0.85rem; vertical-align: baseline;\"><br \/>\n# ps -ef | grep &lt;username&gt; | grep -v root \\<br \/>\n| awk '{print \"prctl -n process.max-file-descriptor -v &lt;value&gt; -r -i process \" $2}' | sh -x<br \/>\n<\/code><\/p>\n<p style=\"font: 14px\/24px 'Open Sans', Helvetica, Arial, sans-serif; margin: 0px 0px 1.71rem; padding: 0px; border: 0px currentColor; color: #444444; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; vertical-align: baseline; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">System wide via mdb(add to \/etc\/system to be presistent across reboots):<\/p>\n<p style=\"font: 14px\/24px 'Open Sans', Helvetica, Arial, sans-serif; margin: 0px 0px 1.71rem; padding: 0px; border: 0px currentColor; color: #444444; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; vertical-align: baseline; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\"><code style=\"margin: 0px; padding: 0px; border: 0px currentColor; line-height: 2; font-family: Consolas, Monaco, 'Lucida Console', monospace; font-size: 0.85rem; vertical-align: baseline;\"><br \/>\n# mdb -kw<br \/>\nrlim_fd_cur\/W &lt;0tvalue&gt;<br \/>\nrlim_fd_max\/W &lt;0tvalue&gt;<br \/>\n<\/code><\/p>\n<p style=\"font: 14px\/24px 'Open Sans', Helvetica, Arial, sans-serif; margin: 0px 0px 1.71rem; padding: 0px; border: 0px currentColor; color: #444444; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; vertical-align: baseline; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">This only works in the shell and not in .profile or .cshrc:<\/p>\n<p style=\"font: 14px\/24px 'Open Sans', Helvetica, Arial, sans-serif; margin: 0px 0px 1.71rem; padding: 0px; border: 0px currentColor; color: #444444; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; vertical-align: baseline; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\"><code style=\"margin: 0px; padding: 0px; border: 0px currentColor; line-height: 2; font-family: Consolas, Monaco, 'Lucida Console', monospace; font-size: 0.85rem; vertical-align: baseline;\"><br \/>\n$ ulimit -Hn<span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\n$ ulimit -Sn<span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\n<\/code><\/p>\n<p style=\"font: 14px\/24px 'Open Sans', Helvetica, Arial, sans-serif; margin: 0px 0px 1.71rem; padding: 0px; border: 0px currentColor; color: #444444; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; vertical-align: baseline; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">The same with:<\/p>\n<p style=\"font: 14px\/24px 'Open Sans', Helvetica, Arial, sans-serif; margin: 0px 0px 1.71rem; padding: 0px; border: 0px currentColor; color: #444444; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; vertical-align: baseline; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\"><code style=\"margin: 0px; padding: 0px; border: 0px currentColor; line-height: 2; font-family: Consolas, Monaco, 'Lucida Console', monospace; font-size: 0.85rem; vertical-align: baseline;\"><br \/>\n$ prctl -n process.max-file-descriptor -v &lt;value&gt; -r -i process $$<\/code><\/p>\n<pre style=\"color: #555555; text-transform: none; line-height: 18px; text-indent: 0px; letter-spacing: normal; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; margin-top: 10px; margin-bottom: 10px; word-spacing: 0px; background-color: #ffffff; -webkit-text-stroke-width: 0px;\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p style=\"font: 12px\/18px Arial, Verdana, sans-serif; margin: 10px 0px; color: #555555; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">Before I get into details here is the bottom line. If you start MySQL on Solaris as a non-root (ie, mysql) user and for some reason you need to adjust the [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3251"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3251"}],"version-history":[{"count":4,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3251\/revisions"}],"predecessor-version":[{"id":3253,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3251\/revisions\/3253"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}