<div class="highlight"><pre><span class="cm">/*</span>
<span class="cm"> * Copyright (c) 2009-2010, Salvatore Sanfilippo &lt;antirez at gmail dot com&gt;</span>
<span class="cm"> * All rights reserved.</span>
<span class="cm"> *</span>
<span class="cm"> * Redistribution and use in source and binary forms, with or without</span>
<span class="cm"> * modification, are permitted provided that the following conditions are met:</span>
<span class="cm"> *</span>
<span class="cm"> *   * Redistributions of source code must retain the above copyright notice,</span>
<span class="cm"> *     this list of conditions and the following disclaimer.</span>
<span class="cm"> *   * Redistributions in binary form must reproduce the above copyright</span>
<span class="cm"> *     notice, this list of conditions and the following disclaimer in the</span>
<span class="cm"> *     documentation and/or other materials provided with the distribution.</span>
<span class="cm"> *   * Neither the name of Redis nor the names of its contributors may be used</span>
<span class="cm"> *     to endorse or promote products derived from this software without</span>
<span class="cm"> *     specific prior written permission.</span>
<span class="cm"> *</span>
<span class="cm"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot;</span>
<span class="cm"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
<span class="cm"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
<span class="cm"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE</span>
<span class="cm"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span>
<span class="cm"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span>
<span class="cm"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
<span class="cm"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span>
<span class="cm"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span>
<span class="cm"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span>
<span class="cm"> * POSSIBILITY OF SUCH DAMAGE.</span>
<span class="cm"> */</span>

<span class="cp">#include &quot;redis.h&quot;</span>
<span class="cp">#include &quot;slowlog.h&quot;</span>
<span class="cp">#include &quot;bio.h&quot;</span>

<span class="cp">#include &lt;time.h&gt;</span>
<span class="cp">#include &lt;signal.h&gt;</span>
<span class="cp">#include &lt;sys/wait.h&gt;</span>
<span class="cp">#include &lt;errno.h&gt;</span>
<span class="cp">#include &lt;assert.h&gt;</span>
<span class="cp">#include &lt;ctype.h&gt;</span>
<span class="cp">#include &lt;stdarg.h&gt;</span>
<span class="cp">#include &lt;arpa/inet.h&gt;</span>
<span class="cp">#include &lt;sys/stat.h&gt;</span>
<span class="cp">#include &lt;fcntl.h&gt;</span>
<span class="cp">#include &lt;sys/time.h&gt;</span>
<span class="cp">#include &lt;sys/resource.h&gt;</span>
<span class="cp">#include &lt;sys/uio.h&gt;</span>
<span class="cp">#include &lt;limits.h&gt;</span>
<span class="cp">#include &lt;float.h&gt;</span>
<span class="cp">#include &lt;math.h&gt;</span>
<span class="cp">#include &lt;sys/resource.h&gt;</span>
<span class="cp">#include &lt;sys/utsname.h&gt;</span>

<span class="cm">/* Our shared &quot;common&quot; objects */</span>

<span class="k">struct</span> <span class="n">sharedObjectsStruct</span> <span class="n">shared</span><span class="p">;</span>

<span class="cm">/* Global vars that are actually used as constants. The following double</span>
<span class="cm"> * values are used for double on-disk serialization, and are initialized</span>
<span class="cm"> * at runtime to avoid strange compiler optimizations. */</span>

<span class="kt">double</span> <span class="n">R_Zero</span><span class="p">,</span> <span class="n">R_PosInf</span><span class="p">,</span> <span class="n">R_NegInf</span><span class="p">,</span> <span class="n">R_Nan</span><span class="p">;</span>

<span class="cm">/*================================= Globals ================================= */</span>

<span class="cm">/* Global vars */</span>
<span class="k">struct</span> <span class="n">redisServer</span> <span class="n">server</span><span class="p">;</span> <span class="cm">/* server global state */</span>
<span class="k">struct</span> <span class="n">redisCommand</span> <span class="o">*</span><span class="n">commandTable</span><span class="p">;</span>

<span class="cm">/* Our command table.</span>
<span class="cm"> *</span>
<span class="cm"> * Every entry is composed of the following fields:</span>
<span class="cm"> *</span>
<span class="cm"> * name: a string representing the command name.</span>
<span class="cm"> * function: pointer to the C function implementing the command.</span>
<span class="cm"> * arity: number of arguments, it is possible to use -N to say &gt;= N</span>
<span class="cm"> * sflags: command flags as string. See below for a table of flags.</span>
<span class="cm"> * flags: flags as bitmask. Computed by Redis using the &#39;sflags&#39; field.</span>
<span class="cm"> * get_keys_proc: an optional function to get key arguments from a command.</span>
<span class="cm"> *                This is only used when the following three fields are not</span>
<span class="cm"> *                enough to specify what arguments are keys.</span>
<span class="cm"> * first_key_index: first argument that is a key</span>
<span class="cm"> * last_key_index: last argument that is a key</span>
<span class="cm"> * key_step: step to get all the keys from first to last argument. For instance</span>
<span class="cm"> *           in MSET the step is two since arguments are key,val,key,val,...</span>
<span class="cm"> * microseconds: microseconds of total execution time for this command.</span>
<span class="cm"> * calls: total number of calls of this command.</span>
<span class="cm"> *</span>
<span class="cm"> * The flags, microseconds and calls fields are computed by Redis and should</span>
<span class="cm"> * always be set to zero.</span>
<span class="cm"> *</span>
<span class="cm"> * Command flags are expressed using strings where every character represents</span>
<span class="cm"> * a flag. Later the populateCommandTable() function will take care of</span>
<span class="cm"> * populating the real &#39;flags&#39; field using this characters.</span>
<span class="cm"> *</span>
<span class="cm"> * This is the meaning of the flags:</span>
<span class="cm"> *</span>
<span class="cm"> * w: write command (may modify the key space).</span>
<span class="cm"> * r: read command  (will never modify the key space).</span>
<span class="cm"> * m: may increase memory usage once called. Don&#39;t allow if out of memory.</span>
<span class="cm"> * a: admin command, like SAVE or SHUTDOWN.</span>
<span class="cm"> * p: Pub/Sub related command.</span>
<span class="cm"> * f: force replication of this command, regarless of server.dirty.</span>
<span class="cm"> * s: command not allowed in scripts.</span>
<span class="cm"> * R: random command. Command is not deterministic, that is, the same command</span>
<span class="cm"> *    with the same arguments, with the same key space, may have different</span>
<span class="cm"> *    results. For instance SPOP and RANDOMKEY are two random commands.</span>
<span class="cm"> * S: Sort command output array if called from script, so that the output</span>
<span class="cm"> *    is deterministic.</span>
<span class="cm"> */</span>
<span class="k">struct</span> <span class="n">redisCommand</span> <span class="n">redisCommandTable</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span>
    <span class="p">{</span><span class="s">&quot;get&quot;</span><span class="p">,</span><span class="n">getCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;set&quot;</span><span class="p">,</span><span class="n">setCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">noPreloadGetKeys</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;setnx&quot;</span><span class="p">,</span><span class="n">setnxCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">noPreloadGetKeys</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;setex&quot;</span><span class="p">,</span><span class="n">setexCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">noPreloadGetKeys</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;psetex&quot;</span><span class="p">,</span><span class="n">psetexCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">noPreloadGetKeys</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;append&quot;</span><span class="p">,</span><span class="n">appendCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;strlen&quot;</span><span class="p">,</span><span class="n">strlenCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;del&quot;</span><span class="p">,</span><span class="n">delCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">noPreloadGetKeys</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;exists&quot;</span><span class="p">,</span><span class="n">existsCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;setbit&quot;</span><span class="p">,</span><span class="n">setbitCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;getbit&quot;</span><span class="p">,</span><span class="n">getbitCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;setrange&quot;</span><span class="p">,</span><span class="n">setrangeCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;getrange&quot;</span><span class="p">,</span><span class="n">getrangeCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;substr&quot;</span><span class="p">,</span><span class="n">getrangeCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;incr&quot;</span><span class="p">,</span><span class="n">incrCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;decr&quot;</span><span class="p">,</span><span class="n">decrCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;mget&quot;</span><span class="p">,</span><span class="n">mgetCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;rpush&quot;</span><span class="p">,</span><span class="n">rpushCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;lpush&quot;</span><span class="p">,</span><span class="n">lpushCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;rpushx&quot;</span><span class="p">,</span><span class="n">rpushxCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;lpushx&quot;</span><span class="p">,</span><span class="n">lpushxCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;linsert&quot;</span><span class="p">,</span><span class="n">linsertCommand</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;rpop&quot;</span><span class="p">,</span><span class="n">rpopCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;lpop&quot;</span><span class="p">,</span><span class="n">lpopCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;brpop&quot;</span><span class="p">,</span><span class="n">brpopCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;ws&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;brpoplpush&quot;</span><span class="p">,</span><span class="n">brpoplpushCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wms&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;blpop&quot;</span><span class="p">,</span><span class="n">blpopCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;ws&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;llen&quot;</span><span class="p">,</span><span class="n">llenCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;lindex&quot;</span><span class="p">,</span><span class="n">lindexCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;lset&quot;</span><span class="p">,</span><span class="n">lsetCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;lrange&quot;</span><span class="p">,</span><span class="n">lrangeCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;ltrim&quot;</span><span class="p">,</span><span class="n">ltrimCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;lrem&quot;</span><span class="p">,</span><span class="n">lremCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;rpoplpush&quot;</span><span class="p">,</span><span class="n">rpoplpushCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;sadd&quot;</span><span class="p">,</span><span class="n">saddCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;srem&quot;</span><span class="p">,</span><span class="n">sremCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;smove&quot;</span><span class="p">,</span><span class="n">smoveCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;sismember&quot;</span><span class="p">,</span><span class="n">sismemberCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;scard&quot;</span><span class="p">,</span><span class="n">scardCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;spop&quot;</span><span class="p">,</span><span class="n">spopCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;wRs&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;srandmember&quot;</span><span class="p">,</span><span class="n">srandmemberCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rR&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;sinter&quot;</span><span class="p">,</span><span class="n">sinterCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rS&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;sinterstore&quot;</span><span class="p">,</span><span class="n">sinterstoreCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;sunion&quot;</span><span class="p">,</span><span class="n">sunionCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rS&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;sunionstore&quot;</span><span class="p">,</span><span class="n">sunionstoreCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;sdiff&quot;</span><span class="p">,</span><span class="n">sdiffCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rS&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;sdiffstore&quot;</span><span class="p">,</span><span class="n">sdiffstoreCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;smembers&quot;</span><span class="p">,</span><span class="n">sinterCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rS&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zadd&quot;</span><span class="p">,</span><span class="n">zaddCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zincrby&quot;</span><span class="p">,</span><span class="n">zincrbyCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zrem&quot;</span><span class="p">,</span><span class="n">zremCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zremrangebyscore&quot;</span><span class="p">,</span><span class="n">zremrangebyscoreCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zremrangebyrank&quot;</span><span class="p">,</span><span class="n">zremrangebyrankCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zunionstore&quot;</span><span class="p">,</span><span class="n">zunionstoreCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">zunionInterGetKeys</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zinterstore&quot;</span><span class="p">,</span><span class="n">zinterstoreCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">zunionInterGetKeys</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zrange&quot;</span><span class="p">,</span><span class="n">zrangeCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zrangebyscore&quot;</span><span class="p">,</span><span class="n">zrangebyscoreCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zrevrangebyscore&quot;</span><span class="p">,</span><span class="n">zrevrangebyscoreCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zcount&quot;</span><span class="p">,</span><span class="n">zcountCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zrevrange&quot;</span><span class="p">,</span><span class="n">zrevrangeCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zcard&quot;</span><span class="p">,</span><span class="n">zcardCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zscore&quot;</span><span class="p">,</span><span class="n">zscoreCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zrank&quot;</span><span class="p">,</span><span class="n">zrankCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;zrevrank&quot;</span><span class="p">,</span><span class="n">zrevrankCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hset&quot;</span><span class="p">,</span><span class="n">hsetCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hsetnx&quot;</span><span class="p">,</span><span class="n">hsetnxCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hget&quot;</span><span class="p">,</span><span class="n">hgetCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hmset&quot;</span><span class="p">,</span><span class="n">hmsetCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hmget&quot;</span><span class="p">,</span><span class="n">hmgetCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hincrby&quot;</span><span class="p">,</span><span class="n">hincrbyCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hincrbyfloat&quot;</span><span class="p">,</span><span class="n">hincrbyfloatCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hdel&quot;</span><span class="p">,</span><span class="n">hdelCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hlen&quot;</span><span class="p">,</span><span class="n">hlenCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hkeys&quot;</span><span class="p">,</span><span class="n">hkeysCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rS&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hvals&quot;</span><span class="p">,</span><span class="n">hvalsCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rS&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hgetall&quot;</span><span class="p">,</span><span class="n">hgetallCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;hexists&quot;</span><span class="p">,</span><span class="n">hexistsCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;incrby&quot;</span><span class="p">,</span><span class="n">incrbyCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;decrby&quot;</span><span class="p">,</span><span class="n">decrbyCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;incrbyfloat&quot;</span><span class="p">,</span><span class="n">incrbyfloatCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;getset&quot;</span><span class="p">,</span><span class="n">getsetCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;mset&quot;</span><span class="p">,</span><span class="n">msetCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;msetnx&quot;</span><span class="p">,</span><span class="n">msetnxCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;randomkey&quot;</span><span class="p">,</span><span class="n">randomkeyCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;rR&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;select&quot;</span><span class="p">,</span><span class="n">selectCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;move&quot;</span><span class="p">,</span><span class="n">moveCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;rename&quot;</span><span class="p">,</span><span class="n">renameCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">renameGetKeys</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;renamenx&quot;</span><span class="p">,</span><span class="n">renamenxCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">renameGetKeys</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;expire&quot;</span><span class="p">,</span><span class="n">expireCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;expireat&quot;</span><span class="p">,</span><span class="n">expireatCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;pexpire&quot;</span><span class="p">,</span><span class="n">pexpireCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;pexpireat&quot;</span><span class="p">,</span><span class="n">pexpireatCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;keys&quot;</span><span class="p">,</span><span class="n">keysCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rS&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;dbsize&quot;</span><span class="p">,</span><span class="n">dbsizeCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;auth&quot;</span><span class="p">,</span><span class="n">authCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rs&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;ping&quot;</span><span class="p">,</span><span class="n">pingCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;echo&quot;</span><span class="p">,</span><span class="n">echoCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;save&quot;</span><span class="p">,</span><span class="n">saveCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;ars&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;bgsave&quot;</span><span class="p">,</span><span class="n">bgsaveCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;ar&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;bgrewriteaof&quot;</span><span class="p">,</span><span class="n">bgrewriteaofCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;ar&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;shutdown&quot;</span><span class="p">,</span><span class="n">shutdownCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;ar&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;lastsave&quot;</span><span class="p">,</span><span class="n">lastsaveCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;type&quot;</span><span class="p">,</span><span class="n">typeCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;multi&quot;</span><span class="p">,</span><span class="n">multiCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;rs&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;exec&quot;</span><span class="p">,</span><span class="n">execCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;s&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;discard&quot;</span><span class="p">,</span><span class="n">discardCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;rs&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;sync&quot;</span><span class="p">,</span><span class="n">syncCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;ars&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;replconf&quot;</span><span class="p">,</span><span class="n">replconfCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;ars&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;flushdb&quot;</span><span class="p">,</span><span class="n">flushdbCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;flushall&quot;</span><span class="p">,</span><span class="n">flushallCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;sort&quot;</span><span class="p">,</span><span class="n">sortCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;wmS&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;info&quot;</span><span class="p">,</span><span class="n">infoCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;monitor&quot;</span><span class="p">,</span><span class="n">monitorCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;ars&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;ttl&quot;</span><span class="p">,</span><span class="n">ttlCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;pttl&quot;</span><span class="p">,</span><span class="n">pttlCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;persist&quot;</span><span class="p">,</span><span class="n">persistCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;slaveof&quot;</span><span class="p">,</span><span class="n">slaveofCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;as&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;debug&quot;</span><span class="p">,</span><span class="n">debugCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;as&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;config&quot;</span><span class="p">,</span><span class="n">configCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;ar&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;subscribe&quot;</span><span class="p">,</span><span class="n">subscribeCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rps&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;unsubscribe&quot;</span><span class="p">,</span><span class="n">unsubscribeCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;rps&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;psubscribe&quot;</span><span class="p">,</span><span class="n">psubscribeCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rps&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;punsubscribe&quot;</span><span class="p">,</span><span class="n">punsubscribeCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;rps&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;publish&quot;</span><span class="p">,</span><span class="n">publishCommand</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;pf&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;watch&quot;</span><span class="p">,</span><span class="n">watchCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;rs&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">noPreloadGetKeys</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;unwatch&quot;</span><span class="p">,</span><span class="n">unwatchCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;rs&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;cluster&quot;</span><span class="p">,</span><span class="n">clusterCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;ar&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;restore&quot;</span><span class="p">,</span><span class="n">restoreCommand</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;awm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;migrate&quot;</span><span class="p">,</span><span class="n">migrateCommand</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="s">&quot;aw&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;asking&quot;</span><span class="p">,</span><span class="n">askingCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;dump&quot;</span><span class="p">,</span><span class="n">dumpCommand</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;ar&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;object&quot;</span><span class="p">,</span><span class="n">objectCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;client&quot;</span><span class="p">,</span><span class="n">clientCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;ar&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;eval&quot;</span><span class="p">,</span><span class="n">evalCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;s&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">zunionInterGetKeys</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;evalsha&quot;</span><span class="p">,</span><span class="n">evalShaCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;s&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">zunionInterGetKeys</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;slowlog&quot;</span><span class="p">,</span><span class="n">slowlogCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;script&quot;</span><span class="p">,</span><span class="n">scriptCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;ras&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;time&quot;</span><span class="p">,</span><span class="n">timeCommand</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;rR&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;bitop&quot;</span><span class="p">,</span><span class="n">bitopCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="s">&quot;wm&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&quot;bitcount&quot;</span><span class="p">,</span><span class="n">bitcountCommand</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">}</span>
<span class="p">};</span>

<span class="cm">/*============================ Utility functions ============================ */</span>

<span class="cm">/* Low level logging. To use only for very big messages, otherwise</span>
<span class="cm"> * redisLog() is to prefer. */</span>
<span class="kt">void</span> <span class="nf">redisLogRaw</span><span class="p">(</span><span class="kt">int</span> <span class="n">level</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">msg</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">const</span> <span class="kt">int</span> <span class="n">syslogLevelMap</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="n">LOG_DEBUG</span><span class="p">,</span> <span class="n">LOG_INFO</span><span class="p">,</span> <span class="n">LOG_NOTICE</span><span class="p">,</span> <span class="n">LOG_WARNING</span> <span class="p">};</span>
    <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">c</span> <span class="o">=</span> <span class="s">&quot;.-*#&quot;</span><span class="p">;</span>
    <span class="kt">FILE</span> <span class="o">*</span><span class="n">fp</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">64</span><span class="p">];</span>
    <span class="kt">int</span> <span class="n">rawmode</span> <span class="o">=</span> <span class="p">(</span><span class="n">level</span> <span class="o">&amp;</span> <span class="n">REDIS_LOG_RAW</span><span class="p">);</span>

    <span class="n">level</span> <span class="o">&amp;=</span> <span class="mh">0xff</span><span class="p">;</span> <span class="cm">/* clear flags */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">level</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">verbosity</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>

    <span class="n">fp</span> <span class="o">=</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">logfile</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="o">?</span> <span class="n">stdout</span> <span class="o">:</span> <span class="n">fopen</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">logfile</span><span class="p">,</span><span class="s">&quot;a&quot;</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">fp</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">rawmode</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span><span class="s">&quot;%s&quot;</span><span class="p">,</span><span class="n">msg</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">off</span><span class="p">;</span>
        <span class="k">struct</span> <span class="n">timeval</span> <span class="n">tv</span><span class="p">;</span>

        <span class="n">gettimeofday</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tv</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
        <span class="n">off</span> <span class="o">=</span> <span class="n">strftime</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="k">sizeof</span><span class="p">(</span><span class="n">buf</span><span class="p">),</span><span class="s">&quot;%d %b %H:%M:%S.&quot;</span><span class="p">,</span><span class="n">localtime</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tv</span><span class="p">.</span><span class="n">tv_sec</span><span class="p">));</span>
        <span class="n">snprintf</span><span class="p">(</span><span class="n">buf</span><span class="o">+</span><span class="n">off</span><span class="p">,</span><span class="k">sizeof</span><span class="p">(</span><span class="n">buf</span><span class="p">)</span><span class="o">-</span><span class="n">off</span><span class="p">,</span><span class="s">&quot;%03d&quot;</span><span class="p">,(</span><span class="kt">int</span><span class="p">)</span><span class="n">tv</span><span class="p">.</span><span class="n">tv_usec</span><span class="o">/</span><span class="mi">1000</span><span class="p">);</span>
        <span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span><span class="s">&quot;[%d] %s %c %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,(</span><span class="kt">int</span><span class="p">)</span><span class="n">getpid</span><span class="p">(),</span><span class="n">buf</span><span class="p">,</span><span class="n">c</span><span class="p">[</span><span class="n">level</span><span class="p">],</span><span class="n">msg</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="n">fflush</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">logfile</span><span class="p">)</span> <span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">syslog_enabled</span><span class="p">)</span> <span class="n">syslog</span><span class="p">(</span><span class="n">syslogLevelMap</span><span class="p">[</span><span class="n">level</span><span class="p">],</span> <span class="s">&quot;%s&quot;</span><span class="p">,</span> <span class="n">msg</span><span class="p">);</span>
<span class="p">}</span>

<span class="cm">/* Like redisLogRaw() but with printf-alike support. This is the funciton that</span>
<span class="cm"> * is used across the code. The raw version is only used in order to dump</span>
<span class="cm"> * the INFO output on crash. */</span>
<span class="kt">void</span> <span class="nf">redisLog</span><span class="p">(</span><span class="kt">int</span> <span class="n">level</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">fmt</span><span class="p">,</span> <span class="p">...)</span> <span class="p">{</span>
    <span class="kt">va_list</span> <span class="n">ap</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">msg</span><span class="p">[</span><span class="n">REDIS_MAX_LOGMSG_LEN</span><span class="p">];</span>

    <span class="k">if</span> <span class="p">((</span><span class="n">level</span><span class="o">&amp;</span><span class="mh">0xff</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">verbosity</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>

    <span class="n">va_start</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="n">fmt</span><span class="p">);</span>
    <span class="n">vsnprintf</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">msg</span><span class="p">),</span> <span class="n">fmt</span><span class="p">,</span> <span class="n">ap</span><span class="p">);</span>
    <span class="n">va_end</span><span class="p">(</span><span class="n">ap</span><span class="p">);</span>

    <span class="n">redisLogRaw</span><span class="p">(</span><span class="n">level</span><span class="p">,</span><span class="n">msg</span><span class="p">);</span>
<span class="p">}</span>

<span class="cm">/* Log a fixed message without printf-alike capabilities, in a way that is</span>
<span class="cm"> * safe to call from a signal handler.</span>
<span class="cm"> *</span>
<span class="cm"> * We actually use this only for signals that are not fatal from the point</span>
<span class="cm"> * of view of Redis. Signals that are going to kill the server anyway and</span>
<span class="cm"> * where we need printf-alike features are served by redisLog(). */</span>
<span class="kt">void</span> <span class="nf">redisLogFromHandler</span><span class="p">(</span><span class="kt">int</span> <span class="n">level</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">msg</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">fd</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">64</span><span class="p">];</span>

    <span class="k">if</span> <span class="p">((</span><span class="n">level</span><span class="o">&amp;</span><span class="mh">0xff</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">verbosity</span> <span class="o">||</span>
        <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">logfile</span> <span class="o">==</span> <span class="nb">NULL</span> <span class="o">&amp;&amp;</span> <span class="n">server</span><span class="p">.</span><span class="n">daemonize</span><span class="p">))</span> <span class="k">return</span><span class="p">;</span>
    <span class="n">fd</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">logfile</span> <span class="o">?</span>
        <span class="n">open</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">logfile</span><span class="p">,</span> <span class="n">O_APPEND</span><span class="o">|</span><span class="n">O_CREAT</span><span class="o">|</span><span class="n">O_WRONLY</span><span class="p">,</span> <span class="mo">0644</span><span class="p">)</span> <span class="o">:</span>
        <span class="n">STDOUT_FILENO</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">fd</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
    <span class="n">ll2string</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="k">sizeof</span><span class="p">(</span><span class="n">buf</span><span class="p">),</span><span class="n">getpid</span><span class="p">());</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">write</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span><span class="s">&quot;[&quot;</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">goto</span> <span class="n">err</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">write</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">strlen</span><span class="p">(</span><span class="n">buf</span><span class="p">))</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">goto</span> <span class="n">err</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">write</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span><span class="s">&quot; | signal handler] (&quot;</span><span class="p">,</span><span class="mi">20</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">goto</span> <span class="n">err</span><span class="p">;</span>
    <span class="n">ll2string</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="k">sizeof</span><span class="p">(</span><span class="n">buf</span><span class="p">),</span><span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">));</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">write</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">strlen</span><span class="p">(</span><span class="n">buf</span><span class="p">))</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">goto</span> <span class="n">err</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">write</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span><span class="s">&quot;) &quot;</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">goto</span> <span class="n">err</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">write</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span><span class="n">msg</span><span class="p">,</span><span class="n">strlen</span><span class="p">(</span><span class="n">msg</span><span class="p">))</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">goto</span> <span class="n">err</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">write</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">goto</span> <span class="n">err</span><span class="p">;</span>
<span class="nl">err:</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">logfile</span><span class="p">)</span> <span class="n">close</span><span class="p">(</span><span class="n">fd</span><span class="p">);</span>
<span class="p">}</span>

<span class="cm">/* Redis generally does not try to recover from out of memory conditions</span>
<span class="cm"> * when allocating objects or strings, it is not clear if it will be possible</span>
<span class="cm"> * to report this condition to the client since the networking layer itself</span>
<span class="cm"> * is based on heap allocation for send buffers, so we simply abort.</span>
<span class="cm"> * At least the code will be simpler to read... */</span>
<span class="kt">void</span> <span class="nf">oom</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">msg</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span> <span class="s">&quot;%s: Out of memory</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="n">msg</span><span class="p">);</span>
    <span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
    <span class="n">abort</span><span class="p">();</span>
<span class="p">}</span>

<span class="cm">/* Return the UNIX time in microseconds */</span>
<span class="kt">long</span> <span class="kt">long</span> <span class="nf">ustime</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">struct</span> <span class="n">timeval</span> <span class="n">tv</span><span class="p">;</span>
    <span class="kt">long</span> <span class="kt">long</span> <span class="n">ust</span><span class="p">;</span>

    <span class="n">gettimeofday</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tv</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
    <span class="n">ust</span> <span class="o">=</span> <span class="p">((</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)</span><span class="n">tv</span><span class="p">.</span><span class="n">tv_sec</span><span class="p">)</span><span class="o">*</span><span class="mi">1000000</span><span class="p">;</span>
    <span class="n">ust</span> <span class="o">+=</span> <span class="n">tv</span><span class="p">.</span><span class="n">tv_usec</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">ust</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* Return the UNIX time in milliseconds */</span>
<span class="kt">long</span> <span class="kt">long</span> <span class="nf">mstime</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">ustime</span><span class="p">()</span><span class="o">/</span><span class="mi">1000</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* After an RDB dump or AOF rewrite we exit from children using _exit() instead of</span>
<span class="cm"> * exit(), because the latter may interact with the same file objects used by</span>
<span class="cm"> * the parent process. However if we are testing the coverage normal exit() is</span>
<span class="cm"> * used in order to obtain the right coverage information. */</span>
<span class="kt">void</span> <span class="nf">exitFromChild</span><span class="p">(</span><span class="kt">int</span> <span class="n">retcode</span><span class="p">)</span> <span class="p">{</span>
<span class="cp">#ifdef COVERAGE_TEST</span>
    <span class="n">exit</span><span class="p">(</span><span class="n">retcode</span><span class="p">);</span>
<span class="cp">#else</span>
    <span class="n">_exit</span><span class="p">(</span><span class="n">retcode</span><span class="p">);</span>
<span class="cp">#endif</span>
<span class="p">}</span>

<span class="cm">/*====================== Hash table type implementation  ==================== */</span>

<span class="cm">/* This is an hash table type that uses the SDS dynamic strings libary as</span>
<span class="cm"> * keys and radis objects as values (objects can hold SDS strings,</span>
<span class="cm"> * lists, sets). */</span>

<span class="kt">void</span> <span class="nf">dictVanillaFree</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">privdata</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">val</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">DICT_NOTUSED</span><span class="p">(</span><span class="n">privdata</span><span class="p">);</span>
    <span class="n">zfree</span><span class="p">(</span><span class="n">val</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">dictListDestructor</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">privdata</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">val</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">DICT_NOTUSED</span><span class="p">(</span><span class="n">privdata</span><span class="p">);</span>
    <span class="n">listRelease</span><span class="p">((</span><span class="n">list</span><span class="o">*</span><span class="p">)</span><span class="n">val</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">dictSdsKeyCompare</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">privdata</span><span class="p">,</span> <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key1</span><span class="p">,</span>
        <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key2</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">l1</span><span class="p">,</span><span class="n">l2</span><span class="p">;</span>
    <span class="n">DICT_NOTUSED</span><span class="p">(</span><span class="n">privdata</span><span class="p">);</span>

    <span class="n">l1</span> <span class="o">=</span> <span class="n">sdslen</span><span class="p">((</span><span class="n">sds</span><span class="p">)</span><span class="n">key1</span><span class="p">);</span>
    <span class="n">l2</span> <span class="o">=</span> <span class="n">sdslen</span><span class="p">((</span><span class="n">sds</span><span class="p">)</span><span class="n">key2</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">l1</span> <span class="o">!=</span> <span class="n">l2</span><span class="p">)</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">memcmp</span><span class="p">(</span><span class="n">key1</span><span class="p">,</span> <span class="n">key2</span><span class="p">,</span> <span class="n">l1</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* A case insensitive version used for the command lookup table. */</span>
<span class="kt">int</span> <span class="nf">dictSdsKeyCaseCompare</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">privdata</span><span class="p">,</span> <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key1</span><span class="p">,</span>
        <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key2</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">DICT_NOTUSED</span><span class="p">(</span><span class="n">privdata</span><span class="p">);</span>

    <span class="k">return</span> <span class="n">strcasecmp</span><span class="p">(</span><span class="n">key1</span><span class="p">,</span> <span class="n">key2</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">dictRedisObjectDestructor</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">privdata</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">val</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">DICT_NOTUSED</span><span class="p">(</span><span class="n">privdata</span><span class="p">);</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">val</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="cm">/* Values of swapped out keys as set to NULL */</span>
    <span class="n">decrRefCount</span><span class="p">(</span><span class="n">val</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">dictSdsDestructor</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">privdata</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">val</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">DICT_NOTUSED</span><span class="p">(</span><span class="n">privdata</span><span class="p">);</span>

    <span class="n">sdsfree</span><span class="p">(</span><span class="n">val</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">dictObjKeyCompare</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">privdata</span><span class="p">,</span> <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key1</span><span class="p">,</span>
        <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key2</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">const</span> <span class="n">robj</span> <span class="o">*</span><span class="n">o1</span> <span class="o">=</span> <span class="n">key1</span><span class="p">,</span> <span class="o">*</span><span class="n">o2</span> <span class="o">=</span> <span class="n">key2</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">dictSdsKeyCompare</span><span class="p">(</span><span class="n">privdata</span><span class="p">,</span><span class="n">o1</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">,</span><span class="n">o2</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">unsigned</span> <span class="kt">int</span> <span class="nf">dictObjHash</span><span class="p">(</span><span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">const</span> <span class="n">robj</span> <span class="o">*</span><span class="n">o</span> <span class="o">=</span> <span class="n">key</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">dictGenHashFunction</span><span class="p">(</span><span class="n">o</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">,</span> <span class="n">sdslen</span><span class="p">((</span><span class="n">sds</span><span class="p">)</span><span class="n">o</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">));</span>
<span class="p">}</span>

<span class="kt">unsigned</span> <span class="kt">int</span> <span class="nf">dictSdsHash</span><span class="p">(</span><span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">dictGenHashFunction</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">key</span><span class="p">,</span> <span class="n">sdslen</span><span class="p">((</span><span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">key</span><span class="p">));</span>
<span class="p">}</span>

<span class="kt">unsigned</span> <span class="kt">int</span> <span class="nf">dictSdsCaseHash</span><span class="p">(</span><span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">dictGenCaseHashFunction</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">key</span><span class="p">,</span> <span class="n">sdslen</span><span class="p">((</span><span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">key</span><span class="p">));</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">dictEncObjKeyCompare</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">privdata</span><span class="p">,</span> <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key1</span><span class="p">,</span>
        <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key2</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">robj</span> <span class="o">*</span><span class="n">o1</span> <span class="o">=</span> <span class="p">(</span><span class="n">robj</span><span class="o">*</span><span class="p">)</span> <span class="n">key1</span><span class="p">,</span> <span class="o">*</span><span class="n">o2</span> <span class="o">=</span> <span class="p">(</span><span class="n">robj</span><span class="o">*</span><span class="p">)</span> <span class="n">key2</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">cmp</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">o1</span><span class="o">-&gt;</span><span class="n">encoding</span> <span class="o">==</span> <span class="n">REDIS_ENCODING_INT</span> <span class="o">&amp;&amp;</span>
        <span class="n">o2</span><span class="o">-&gt;</span><span class="n">encoding</span> <span class="o">==</span> <span class="n">REDIS_ENCODING_INT</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">o1</span><span class="o">-&gt;</span><span class="n">ptr</span> <span class="o">==</span> <span class="n">o2</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">;</span>

    <span class="n">o1</span> <span class="o">=</span> <span class="n">getDecodedObject</span><span class="p">(</span><span class="n">o1</span><span class="p">);</span>
    <span class="n">o2</span> <span class="o">=</span> <span class="n">getDecodedObject</span><span class="p">(</span><span class="n">o2</span><span class="p">);</span>
    <span class="n">cmp</span> <span class="o">=</span> <span class="n">dictSdsKeyCompare</span><span class="p">(</span><span class="n">privdata</span><span class="p">,</span><span class="n">o1</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">,</span><span class="n">o2</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">);</span>
    <span class="n">decrRefCount</span><span class="p">(</span><span class="n">o1</span><span class="p">);</span>
    <span class="n">decrRefCount</span><span class="p">(</span><span class="n">o2</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">cmp</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">unsigned</span> <span class="kt">int</span> <span class="nf">dictEncObjHash</span><span class="p">(</span><span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">key</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">robj</span> <span class="o">*</span><span class="n">o</span> <span class="o">=</span> <span class="p">(</span><span class="n">robj</span><span class="o">*</span><span class="p">)</span> <span class="n">key</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">o</span><span class="o">-&gt;</span><span class="n">encoding</span> <span class="o">==</span> <span class="n">REDIS_ENCODING_RAW</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="n">dictGenHashFunction</span><span class="p">(</span><span class="n">o</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">,</span> <span class="n">sdslen</span><span class="p">((</span><span class="n">sds</span><span class="p">)</span><span class="n">o</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">));</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">o</span><span class="o">-&gt;</span><span class="n">encoding</span> <span class="o">==</span> <span class="n">REDIS_ENCODING_INT</span><span class="p">)</span> <span class="p">{</span>
            <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">32</span><span class="p">];</span>
            <span class="kt">int</span> <span class="n">len</span><span class="p">;</span>

            <span class="n">len</span> <span class="o">=</span> <span class="n">ll2string</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="mi">32</span><span class="p">,(</span><span class="kt">long</span><span class="p">)</span><span class="n">o</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">);</span>
            <span class="k">return</span> <span class="n">dictGenHashFunction</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">buf</span><span class="p">,</span> <span class="n">len</span><span class="p">);</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">hash</span><span class="p">;</span>

            <span class="n">o</span> <span class="o">=</span> <span class="n">getDecodedObject</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
            <span class="n">hash</span> <span class="o">=</span> <span class="n">dictGenHashFunction</span><span class="p">(</span><span class="n">o</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">,</span> <span class="n">sdslen</span><span class="p">((</span><span class="n">sds</span><span class="p">)</span><span class="n">o</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">));</span>
            <span class="n">decrRefCount</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
            <span class="k">return</span> <span class="n">hash</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="cm">/* Sets type hash table */</span>
<span class="n">dictType</span> <span class="n">setDictType</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">dictEncObjHash</span><span class="p">,</span>            <span class="cm">/* hash function */</span>
    <span class="nb">NULL</span><span class="p">,</span>                      <span class="cm">/* key dup */</span>
    <span class="nb">NULL</span><span class="p">,</span>                      <span class="cm">/* val dup */</span>
    <span class="n">dictEncObjKeyCompare</span><span class="p">,</span>      <span class="cm">/* key compare */</span>
    <span class="n">dictRedisObjectDestructor</span><span class="p">,</span> <span class="cm">/* key destructor */</span>
    <span class="nb">NULL</span>                       <span class="cm">/* val destructor */</span>
<span class="p">};</span>

<span class="cm">/* Sorted sets hash (note: a skiplist is used in addition to the hash table) */</span>
<span class="n">dictType</span> <span class="n">zsetDictType</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">dictEncObjHash</span><span class="p">,</span>            <span class="cm">/* hash function */</span>
    <span class="nb">NULL</span><span class="p">,</span>                      <span class="cm">/* key dup */</span>
    <span class="nb">NULL</span><span class="p">,</span>                      <span class="cm">/* val dup */</span>
    <span class="n">dictEncObjKeyCompare</span><span class="p">,</span>      <span class="cm">/* key compare */</span>
    <span class="n">dictRedisObjectDestructor</span><span class="p">,</span> <span class="cm">/* key destructor */</span>
    <span class="nb">NULL</span>                       <span class="cm">/* val destructor */</span>
<span class="p">};</span>

<span class="cm">/* Db-&gt;dict, keys are sds strings, vals are Redis objects. */</span>
<span class="n">dictType</span> <span class="n">dbDictType</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">dictSdsHash</span><span class="p">,</span>                <span class="cm">/* hash function */</span>
    <span class="nb">NULL</span><span class="p">,</span>                       <span class="cm">/* key dup */</span>
    <span class="nb">NULL</span><span class="p">,</span>                       <span class="cm">/* val dup */</span>
    <span class="n">dictSdsKeyCompare</span><span class="p">,</span>          <span class="cm">/* key compare */</span>
    <span class="n">dictSdsDestructor</span><span class="p">,</span>          <span class="cm">/* key destructor */</span>
    <span class="n">dictRedisObjectDestructor</span>   <span class="cm">/* val destructor */</span>
<span class="p">};</span>

<span class="cm">/* Db-&gt;expires */</span>
<span class="n">dictType</span> <span class="n">keyptrDictType</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">dictSdsHash</span><span class="p">,</span>               <span class="cm">/* hash function */</span>
    <span class="nb">NULL</span><span class="p">,</span>                      <span class="cm">/* key dup */</span>
    <span class="nb">NULL</span><span class="p">,</span>                      <span class="cm">/* val dup */</span>
    <span class="n">dictSdsKeyCompare</span><span class="p">,</span>         <span class="cm">/* key compare */</span>
    <span class="nb">NULL</span><span class="p">,</span>                      <span class="cm">/* key destructor */</span>
    <span class="nb">NULL</span>                       <span class="cm">/* val destructor */</span>
<span class="p">};</span>

<span class="cm">/* Command table. sds string -&gt; command struct pointer. */</span>
<span class="n">dictType</span> <span class="n">commandTableDictType</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">dictSdsCaseHash</span><span class="p">,</span>           <span class="cm">/* hash function */</span>
    <span class="nb">NULL</span><span class="p">,</span>                      <span class="cm">/* key dup */</span>
    <span class="nb">NULL</span><span class="p">,</span>                      <span class="cm">/* val dup */</span>
    <span class="n">dictSdsKeyCaseCompare</span><span class="p">,</span>     <span class="cm">/* key compare */</span>
    <span class="n">dictSdsDestructor</span><span class="p">,</span>         <span class="cm">/* key destructor */</span>
    <span class="nb">NULL</span>                       <span class="cm">/* val destructor */</span>
<span class="p">};</span>

<span class="cm">/* Hash type hash table (note that small hashes are represented with zimpaps) */</span>
<span class="n">dictType</span> <span class="n">hashDictType</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">dictEncObjHash</span><span class="p">,</span>             <span class="cm">/* hash function */</span>
    <span class="nb">NULL</span><span class="p">,</span>                       <span class="cm">/* key dup */</span>
    <span class="nb">NULL</span><span class="p">,</span>                       <span class="cm">/* val dup */</span>
    <span class="n">dictEncObjKeyCompare</span><span class="p">,</span>       <span class="cm">/* key compare */</span>
    <span class="n">dictRedisObjectDestructor</span><span class="p">,</span>  <span class="cm">/* key destructor */</span>
    <span class="n">dictRedisObjectDestructor</span>   <span class="cm">/* val destructor */</span>
<span class="p">};</span>

<span class="cm">/* Keylist hash table type has unencoded redis objects as keys and</span>
<span class="cm"> * lists as values. It&#39;s used for blocking operations (BLPOP) and to</span>
<span class="cm"> * map swapped keys to a list of clients waiting for this keys to be loaded. */</span>
<span class="n">dictType</span> <span class="n">keylistDictType</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">dictObjHash</span><span class="p">,</span>                <span class="cm">/* hash function */</span>
    <span class="nb">NULL</span><span class="p">,</span>                       <span class="cm">/* key dup */</span>
    <span class="nb">NULL</span><span class="p">,</span>                       <span class="cm">/* val dup */</span>
    <span class="n">dictObjKeyCompare</span><span class="p">,</span>          <span class="cm">/* key compare */</span>
    <span class="n">dictRedisObjectDestructor</span><span class="p">,</span>  <span class="cm">/* key destructor */</span>
    <span class="n">dictListDestructor</span>          <span class="cm">/* val destructor */</span>
<span class="p">};</span>

<span class="cm">/* Cluster nodes hash table, mapping nodes addresses 1.2.3.4:6379 to</span>
<span class="cm"> * clusterNode structures. */</span>
<span class="n">dictType</span> <span class="n">clusterNodesDictType</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">dictSdsHash</span><span class="p">,</span>                <span class="cm">/* hash function */</span>
    <span class="nb">NULL</span><span class="p">,</span>                       <span class="cm">/* key dup */</span>
    <span class="nb">NULL</span><span class="p">,</span>                       <span class="cm">/* val dup */</span>
    <span class="n">dictSdsKeyCompare</span><span class="p">,</span>          <span class="cm">/* key compare */</span>
    <span class="n">dictSdsDestructor</span><span class="p">,</span>          <span class="cm">/* key destructor */</span>
    <span class="nb">NULL</span>                        <span class="cm">/* val destructor */</span>
<span class="p">};</span>

<span class="kt">int</span> <span class="nf">htNeedsResize</span><span class="p">(</span><span class="n">dict</span> <span class="o">*</span><span class="n">dict</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">long</span> <span class="kt">long</span> <span class="n">size</span><span class="p">,</span> <span class="n">used</span><span class="p">;</span>

    <span class="n">size</span> <span class="o">=</span> <span class="n">dictSlots</span><span class="p">(</span><span class="n">dict</span><span class="p">);</span>
    <span class="n">used</span> <span class="o">=</span> <span class="n">dictSize</span><span class="p">(</span><span class="n">dict</span><span class="p">);</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">size</span> <span class="o">&amp;&amp;</span> <span class="n">used</span> <span class="o">&amp;&amp;</span> <span class="n">size</span> <span class="o">&gt;</span> <span class="n">DICT_HT_INITIAL_SIZE</span> <span class="o">&amp;&amp;</span>
            <span class="p">(</span><span class="n">used</span><span class="o">*</span><span class="mi">100</span><span class="o">/</span><span class="n">size</span> <span class="o">&lt;</span> <span class="n">REDIS_HT_MINFILL</span><span class="p">));</span>
<span class="p">}</span>

<span class="cm">/* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL</span>
<span class="cm"> * we resize the hash table to save memory */</span>
<span class="kt">void</span> <span class="nf">tryResizeHashTables</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">dbnum</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">htNeedsResize</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">dict</span><span class="p">))</span>
            <span class="n">dictResize</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">dict</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">htNeedsResize</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">expires</span><span class="p">))</span>
            <span class="n">dictResize</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">expires</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="cm">/* Our hash table implementation performs rehashing incrementally while</span>
<span class="cm"> * we write/read from the hash table. Still if the server is idle, the hash</span>
<span class="cm"> * table will use two tables for a long time. So we try to use 1 millisecond</span>
<span class="cm"> * of CPU time at every serverCron() loop in order to rehash some key. */</span>
<span class="kt">void</span> <span class="nf">incrementallyRehash</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">dbnum</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="cm">/* Keys dictionary */</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">dictIsRehashing</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">dict</span><span class="p">))</span> <span class="p">{</span>
            <span class="n">dictRehashMilliseconds</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">dict</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
            <span class="k">break</span><span class="p">;</span> <span class="cm">/* already used our millisecond for this loop... */</span>
        <span class="p">}</span>
        <span class="cm">/* Expires */</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">dictIsRehashing</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">expires</span><span class="p">))</span> <span class="p">{</span>
            <span class="n">dictRehashMilliseconds</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">expires</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
            <span class="k">break</span><span class="p">;</span> <span class="cm">/* already used our millisecond for this loop... */</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="cm">/* This function is called once a background process of some kind terminates,</span>
<span class="cm"> * as we want to avoid resizing the hash tables when there is a child in order</span>
<span class="cm"> * to play well with copy-on-write (otherwise when a resize happens lots of</span>
<span class="cm"> * memory pages are copied). The goal of this function is to update the ability</span>
<span class="cm"> * for dict.c to resize the hash tables accordingly to the fact we have o not</span>
<span class="cm"> * running childs. */</span>
<span class="kt">void</span> <span class="nf">updateDictResizePolicy</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="n">server</span><span class="p">.</span><span class="n">aof_child_pid</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">dictEnableResize</span><span class="p">();</span>
    <span class="k">else</span>
        <span class="n">dictDisableResize</span><span class="p">();</span>
<span class="p">}</span>

<span class="cm">/* ======================= Cron: called every 100 ms ======================== */</span>

<span class="cm">/* Try to expire a few timed out keys. The algorithm used is adaptive and</span>
<span class="cm"> * will use few CPU cycles if there are few expiring keys, otherwise</span>
<span class="cm"> * it will get more aggressive to avoid that too much memory is used by</span>
<span class="cm"> * keys that can be removed from the keyspace. */</span>
<span class="kt">void</span> <span class="nf">activeExpireCycle</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">,</span> <span class="n">iteration</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="kt">long</span> <span class="kt">long</span> <span class="n">start</span> <span class="o">=</span> <span class="n">ustime</span><span class="p">(),</span> <span class="n">timelimit</span><span class="p">;</span>

    <span class="cm">/* We can use at max REDIS_EXPIRELOOKUPS_TIME_PERC percentage of CPU time</span>
<span class="cm">     * per iteration. Since this function gets called with a frequency of</span>
<span class="cm">     * REDIS_HZ times per second, the following is the max amount of</span>
<span class="cm">     * microseconds we can spend in this function. */</span>
    <span class="n">timelimit</span> <span class="o">=</span> <span class="mi">1000000</span><span class="o">*</span><span class="n">REDIS_EXPIRELOOKUPS_TIME_PERC</span><span class="o">/</span><span class="n">REDIS_HZ</span><span class="o">/</span><span class="mi">100</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">timelimit</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="n">timelimit</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">dbnum</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">expired</span><span class="p">;</span>
        <span class="n">redisDb</span> <span class="o">*</span><span class="n">db</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="o">+</span><span class="n">j</span><span class="p">;</span>

        <span class="cm">/* Continue to expire if at the end of the cycle more than 25%</span>
<span class="cm">         * of the keys were expired. */</span>
        <span class="k">do</span> <span class="p">{</span>
            <span class="kt">unsigned</span> <span class="kt">long</span> <span class="n">num</span> <span class="o">=</span> <span class="n">dictSize</span><span class="p">(</span><span class="n">db</span><span class="o">-&gt;</span><span class="n">expires</span><span class="p">);</span>
            <span class="kt">unsigned</span> <span class="kt">long</span> <span class="n">slots</span> <span class="o">=</span> <span class="n">dictSlots</span><span class="p">(</span><span class="n">db</span><span class="o">-&gt;</span><span class="n">expires</span><span class="p">);</span>
            <span class="kt">long</span> <span class="kt">long</span> <span class="n">now</span> <span class="o">=</span> <span class="n">mstime</span><span class="p">();</span>

            <span class="cm">/* When there are less than 1% filled slots getting random</span>
<span class="cm">             * keys is expensive, so stop here waiting for better times...</span>
<span class="cm">             * The dictionary will be resized asap. */</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">num</span> <span class="o">&amp;&amp;</span> <span class="n">slots</span> <span class="o">&gt;</span> <span class="n">DICT_HT_INITIAL_SIZE</span> <span class="o">&amp;&amp;</span>
                <span class="p">(</span><span class="n">num</span><span class="o">*</span><span class="mi">100</span><span class="o">/</span><span class="n">slots</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">))</span> <span class="k">break</span><span class="p">;</span>

            <span class="cm">/* The main collection cycle. Sample random keys among keys</span>
<span class="cm">             * with an expire set, checking for expired ones. */</span>
            <span class="n">expired</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">num</span> <span class="o">&gt;</span> <span class="n">REDIS_EXPIRELOOKUPS_PER_CRON</span><span class="p">)</span>
                <span class="n">num</span> <span class="o">=</span> <span class="n">REDIS_EXPIRELOOKUPS_PER_CRON</span><span class="p">;</span>
            <span class="k">while</span> <span class="p">(</span><span class="n">num</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">dictEntry</span> <span class="o">*</span><span class="n">de</span><span class="p">;</span>
                <span class="kt">long</span> <span class="kt">long</span> <span class="n">t</span><span class="p">;</span>

                <span class="k">if</span> <span class="p">((</span><span class="n">de</span> <span class="o">=</span> <span class="n">dictGetRandomKey</span><span class="p">(</span><span class="n">db</span><span class="o">-&gt;</span><span class="n">expires</span><span class="p">))</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span>
                <span class="n">t</span> <span class="o">=</span> <span class="n">dictGetSignedIntegerVal</span><span class="p">(</span><span class="n">de</span><span class="p">);</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">now</span> <span class="o">&gt;</span> <span class="n">t</span><span class="p">)</span> <span class="p">{</span>
                    <span class="n">sds</span> <span class="n">key</span> <span class="o">=</span> <span class="n">dictGetKey</span><span class="p">(</span><span class="n">de</span><span class="p">);</span>
                    <span class="n">robj</span> <span class="o">*</span><span class="n">keyobj</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">sdslen</span><span class="p">(</span><span class="n">key</span><span class="p">));</span>

                    <span class="n">propagateExpire</span><span class="p">(</span><span class="n">db</span><span class="p">,</span><span class="n">keyobj</span><span class="p">);</span>
                    <span class="n">dbDelete</span><span class="p">(</span><span class="n">db</span><span class="p">,</span><span class="n">keyobj</span><span class="p">);</span>
                    <span class="n">decrRefCount</span><span class="p">(</span><span class="n">keyobj</span><span class="p">);</span>
                    <span class="n">expired</span><span class="o">++</span><span class="p">;</span>
                    <span class="n">server</span><span class="p">.</span><span class="n">stat_expiredkeys</span><span class="o">++</span><span class="p">;</span>
                <span class="p">}</span>
            <span class="p">}</span>
            <span class="cm">/* We can&#39;t block forever here even if there are many keys to</span>
<span class="cm">             * expire. So after a given amount of milliseconds return to the</span>
<span class="cm">             * caller waiting for the other active expire cycle. */</span>
            <span class="n">iteration</span><span class="o">++</span><span class="p">;</span>
            <span class="k">if</span> <span class="p">((</span><span class="n">iteration</span> <span class="o">&amp;</span> <span class="mh">0xf</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="cm">/* check once every 16 cycles. */</span>
                <span class="p">(</span><span class="n">ustime</span><span class="p">()</span><span class="o">-</span><span class="n">start</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">timelimit</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="n">expired</span> <span class="o">&gt;</span> <span class="n">REDIS_EXPIRELOOKUPS_PER_CRON</span><span class="o">/</span><span class="mi">4</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">updateLRUClock</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">server</span><span class="p">.</span><span class="n">lruclock</span> <span class="o">=</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">unixtime</span><span class="o">/</span><span class="n">REDIS_LRU_CLOCK_RESOLUTION</span><span class="p">)</span> <span class="o">&amp;</span>
                                                <span class="n">REDIS_LRU_CLOCK_MAX</span><span class="p">;</span>
<span class="p">}</span>


<span class="cm">/* Add a sample to the operations per second array of samples. */</span>
<span class="kt">void</span> <span class="nf">trackOperationsPerSecond</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">long</span> <span class="kt">long</span> <span class="n">t</span> <span class="o">=</span> <span class="n">mstime</span><span class="p">()</span> <span class="o">-</span> <span class="n">server</span><span class="p">.</span><span class="n">ops_sec_last_sample_time</span><span class="p">;</span>
    <span class="kt">long</span> <span class="kt">long</span> <span class="n">ops</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">stat_numcommands</span> <span class="o">-</span> <span class="n">server</span><span class="p">.</span><span class="n">ops_sec_last_sample_ops</span><span class="p">;</span>
    <span class="kt">long</span> <span class="kt">long</span> <span class="n">ops_sec</span><span class="p">;</span>

    <span class="n">ops_sec</span> <span class="o">=</span> <span class="n">t</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">?</span> <span class="p">(</span><span class="n">ops</span><span class="o">*</span><span class="mi">1000</span><span class="o">/</span><span class="n">t</span><span class="p">)</span> <span class="o">:</span> <span class="mi">0</span><span class="p">;</span>

    <span class="n">server</span><span class="p">.</span><span class="n">ops_sec_samples</span><span class="p">[</span><span class="n">server</span><span class="p">.</span><span class="n">ops_sec_idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">ops_sec</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">ops_sec_idx</span> <span class="o">=</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">ops_sec_idx</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="n">REDIS_OPS_SEC_SAMPLES</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">ops_sec_last_sample_time</span> <span class="o">=</span> <span class="n">mstime</span><span class="p">();</span>
    <span class="n">server</span><span class="p">.</span><span class="n">ops_sec_last_sample_ops</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">stat_numcommands</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* Return the mean of all the samples. */</span>
<span class="kt">long</span> <span class="kt">long</span> <span class="nf">getOperationsPerSecond</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">;</span>
    <span class="kt">long</span> <span class="kt">long</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">REDIS_OPS_SEC_SAMPLES</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span>
        <span class="n">sum</span> <span class="o">+=</span> <span class="n">server</span><span class="p">.</span><span class="n">ops_sec_samples</span><span class="p">[</span><span class="n">j</span><span class="p">];</span>
    <span class="k">return</span> <span class="n">sum</span> <span class="o">/</span> <span class="n">REDIS_OPS_SEC_SAMPLES</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* Check for timeouts. Returns non-zero if the client was terminated */</span>
<span class="kt">int</span> <span class="nf">clientsCronHandleTimeout</span><span class="p">(</span><span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">time_t</span> <span class="n">now</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">unixtime</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">maxidletime</span> <span class="o">&amp;&amp;</span>
        <span class="o">!</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_SLAVE</span><span class="p">)</span> <span class="o">&amp;&amp;</span>    <span class="cm">/* no timeout for slaves */</span>
        <span class="o">!</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_MASTER</span><span class="p">)</span> <span class="o">&amp;&amp;</span>   <span class="cm">/* no timeout for masters */</span>
        <span class="o">!</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_BLOCKED</span><span class="p">)</span> <span class="o">&amp;&amp;</span>  <span class="cm">/* no timeout for BLPOP */</span>
        <span class="n">dictSize</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">pubsub_channels</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="cm">/* no timeout for pubsub */</span>
        <span class="n">listLength</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">pubsub_patterns</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span>
        <span class="p">(</span><span class="n">now</span> <span class="o">-</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">lastinteraction</span> <span class="o">&gt;</span> <span class="n">server</span><span class="p">.</span><span class="n">maxidletime</span><span class="p">))</span>
    <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_VERBOSE</span><span class="p">,</span><span class="s">&quot;Closing idle client&quot;</span><span class="p">);</span>
        <span class="n">freeClient</span><span class="p">(</span><span class="n">c</span><span class="p">);</span>
        <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_BLOCKED</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">bpop</span><span class="p">.</span><span class="n">timeout</span> <span class="o">!=</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">bpop</span><span class="p">.</span><span class="n">timeout</span> <span class="o">&lt;</span> <span class="n">now</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">shared</span><span class="p">.</span><span class="n">nullmultibulk</span><span class="p">);</span>
            <span class="n">unblockClientWaitingData</span><span class="p">(</span><span class="n">c</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* The client query buffer is an sds.c string that can end with a lot of</span>
<span class="cm"> * free space not used, this function reclaims space if needed.</span>
<span class="cm"> *</span>
<span class="cm"> * The funciton always returns 0 as it never terminates the client. */</span>
<span class="kt">int</span> <span class="nf">clientsCronResizeQueryBuffer</span><span class="p">(</span><span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">size_t</span> <span class="n">querybuf_size</span> <span class="o">=</span> <span class="n">sdsAllocSize</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">querybuf</span><span class="p">);</span>
    <span class="kt">time_t</span> <span class="n">idletime</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">unixtime</span> <span class="o">-</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">lastinteraction</span><span class="p">;</span>

    <span class="cm">/* There are two conditions to resize the query buffer:</span>
<span class="cm">     * 1) Query buffer is &gt; BIG_ARG and too big for latest peak.</span>
<span class="cm">     * 2) Client is inactive and the buffer is bigger than 1k. */</span>
    <span class="k">if</span> <span class="p">(((</span><span class="n">querybuf_size</span> <span class="o">&gt;</span> <span class="n">REDIS_MBULK_BIG_ARG</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
         <span class="p">(</span><span class="n">querybuf_size</span><span class="o">/</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">querybuf_peak</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">)</span> <span class="o">||</span>
         <span class="p">(</span><span class="n">querybuf_size</span> <span class="o">&gt;</span> <span class="mi">1024</span> <span class="o">&amp;&amp;</span> <span class="n">idletime</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">))</span>
    <span class="p">{</span>
        <span class="cm">/* Only resize the query buffer if it is actually wasting space. */</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">sdsavail</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">querybuf</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1024</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">c</span><span class="o">-&gt;</span><span class="n">querybuf</span> <span class="o">=</span> <span class="n">sdsRemoveFreeSpace</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">querybuf</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="cm">/* Reset the peak again to capture the peak memory usage in the next</span>
<span class="cm">     * cycle. */</span>
    <span class="n">c</span><span class="o">-&gt;</span><span class="n">querybuf_peak</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">clientsCron</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="cm">/* Make sure to process at least 1/(REDIS_HZ*10) of clients per call.</span>
<span class="cm">     * Since this function is called REDIS_HZ times per second we are sure that</span>
<span class="cm">     * in the worst case we process all the clients in 10 seconds.</span>
<span class="cm">     * In normal conditions (a reasonable number of clients) we process</span>
<span class="cm">     * all the clients in a shorter time. */</span>
    <span class="kt">int</span> <span class="n">numclients</span> <span class="o">=</span> <span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">clients</span><span class="p">);</span>
    <span class="kt">int</span> <span class="n">iterations</span> <span class="o">=</span> <span class="n">numclients</span><span class="o">/</span><span class="p">(</span><span class="n">REDIS_HZ</span><span class="o">*</span><span class="mi">10</span><span class="p">);</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">iterations</span> <span class="o">&lt;</span> <span class="mi">50</span><span class="p">)</span>
        <span class="n">iterations</span> <span class="o">=</span> <span class="p">(</span><span class="n">numclients</span> <span class="o">&lt;</span> <span class="mi">50</span><span class="p">)</span> <span class="o">?</span> <span class="n">numclients</span> <span class="o">:</span> <span class="mi">50</span><span class="p">;</span>
    <span class="k">while</span><span class="p">(</span><span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">clients</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="n">iterations</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">;</span>
        <span class="n">listNode</span> <span class="o">*</span><span class="n">head</span><span class="p">;</span>

        <span class="cm">/* Rotate the list, take the current head, process.</span>
<span class="cm">         * This way if the client must be removed from the list it&#39;s the</span>
<span class="cm">         * first element and we don&#39;t incur into O(N) computation. */</span>
        <span class="n">listRotate</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">clients</span><span class="p">);</span>
        <span class="n">head</span> <span class="o">=</span> <span class="n">listFirst</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">clients</span><span class="p">);</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">listNodeValue</span><span class="p">(</span><span class="n">head</span><span class="p">);</span>
        <span class="cm">/* The following functions do different service checks on the client.</span>
<span class="cm">         * The protocol is that they return non-zero if the client was</span>
<span class="cm">         * terminated. */</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">clientsCronHandleTimeout</span><span class="p">(</span><span class="n">c</span><span class="p">))</span> <span class="k">continue</span><span class="p">;</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">clientsCronResizeQueryBuffer</span><span class="p">(</span><span class="n">c</span><span class="p">))</span> <span class="k">continue</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="cm">/* This is our timer interrupt, called REDIS_HZ times per second.</span>
<span class="cm"> * Here is where we do a number of things that need to be done asynchronously.</span>
<span class="cm"> * For instance:</span>
<span class="cm"> *</span>
<span class="cm"> * - Active expired keys collection (it is also performed in a lazy way on</span>
<span class="cm"> *   lookup).</span>
<span class="cm"> * - Software watchdong.</span>
<span class="cm"> * - Update some statistic.</span>
<span class="cm"> * - Incremental rehashing of the DBs hash tables.</span>
<span class="cm"> * - Triggering BGSAVE / AOF rewrite, and handling of terminated children.</span>
<span class="cm"> * - Clients timeout of differnet kinds.</span>
<span class="cm"> * - Replication reconnection.</span>
<span class="cm"> * - Many more...</span>
<span class="cm"> *</span>
<span class="cm"> * Everything directly called here will be called REDIS_HZ times per second,</span>
<span class="cm"> * so in order to throttle execution of things we want to do less frequently</span>
<span class="cm"> * a macro is used: run_with_period(milliseconds) { .... }</span>
<span class="cm"> */</span>

<span class="cm">/* Using the following macro you can run code inside serverCron() with the</span>
<span class="cm"> * specified period, specified in milliseconds.</span>
<span class="cm"> * The actual resolution depends on REDIS_HZ. */</span>
<span class="cp">#define run_with_period(_ms_) if (!(loops % ((_ms_)/(1000/REDIS_HZ))))</span>

<span class="kt">int</span> <span class="nf">serverCron</span><span class="p">(</span><span class="k">struct</span> <span class="n">aeEventLoop</span> <span class="o">*</span><span class="n">eventLoop</span><span class="p">,</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">id</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">clientData</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">,</span> <span class="n">loops</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">cronloops</span><span class="p">;</span>
    <span class="n">REDIS_NOTUSED</span><span class="p">(</span><span class="n">eventLoop</span><span class="p">);</span>
    <span class="n">REDIS_NOTUSED</span><span class="p">(</span><span class="n">id</span><span class="p">);</span>
    <span class="n">REDIS_NOTUSED</span><span class="p">(</span><span class="n">clientData</span><span class="p">);</span>

    <span class="cm">/* Software watchdog: deliver the SIGALRM that will reach the signal</span>
<span class="cm">     * handler if we don&#39;t return here fast enough. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">watchdog_period</span><span class="p">)</span> <span class="n">watchdogScheduleSignal</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">watchdog_period</span><span class="p">);</span>

    <span class="cm">/* We take a cached value of the unix time in the global state because</span>
<span class="cm">     * with virtual memory and aging there is to store the current time</span>
<span class="cm">     * in objects at every object access, and accuracy is not needed.</span>
<span class="cm">     * To access a global var is faster than calling time(NULL) */</span>
    <span class="n">server</span><span class="p">.</span><span class="n">unixtime</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">);</span>

    <span class="n">run_with_period</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span> <span class="n">trackOperationsPerSecond</span><span class="p">();</span>

    <span class="cm">/* We have just 22 bits per object for LRU information.</span>
<span class="cm">     * So we use an (eventually wrapping) LRU clock with 10 seconds resolution.</span>
<span class="cm">     * 2^22 bits with 10 seconds resoluton is more or less 1.5 years.</span>
<span class="cm">     *</span>
<span class="cm">     * Note that even if this will wrap after 1.5 years it&#39;s not a problem,</span>
<span class="cm">     * everything will still work but just some object will appear younger</span>
<span class="cm">     * to Redis. But for this to happen a given object should never be touched</span>
<span class="cm">     * for 1.5 years.</span>
<span class="cm">     *</span>
<span class="cm">     * Note that you can change the resolution altering the</span>
<span class="cm">     * REDIS_LRU_CLOCK_RESOLUTION define.</span>
<span class="cm">     */</span>
    <span class="n">updateLRUClock</span><span class="p">();</span>

    <span class="cm">/* Record the max memory used since the server was started. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">zmalloc_used_memory</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">server</span><span class="p">.</span><span class="n">stat_peak_memory</span><span class="p">)</span>
        <span class="n">server</span><span class="p">.</span><span class="n">stat_peak_memory</span> <span class="o">=</span> <span class="n">zmalloc_used_memory</span><span class="p">();</span>

    <span class="cm">/* We received a SIGTERM, shutting down here in a safe way, as it is</span>
<span class="cm">     * not ok doing so inside the signal handler. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">shutdown_asap</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">prepareForShutdown</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">==</span> <span class="n">REDIS_OK</span><span class="p">)</span> <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;SIGTERM received but errors trying to shut down the server, check the logs for more information&quot;</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="cm">/* Show some info about non-empty databases */</span>
    <span class="n">run_with_period</span><span class="p">(</span><span class="mi">5000</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">dbnum</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
            <span class="kt">long</span> <span class="kt">long</span> <span class="n">size</span><span class="p">,</span> <span class="n">used</span><span class="p">,</span> <span class="n">vkeys</span><span class="p">;</span>

            <span class="n">size</span> <span class="o">=</span> <span class="n">dictSlots</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">dict</span><span class="p">);</span>
            <span class="n">used</span> <span class="o">=</span> <span class="n">dictSize</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">dict</span><span class="p">);</span>
            <span class="n">vkeys</span> <span class="o">=</span> <span class="n">dictSize</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">expires</span><span class="p">);</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">used</span> <span class="o">||</span> <span class="n">vkeys</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_VERBOSE</span><span class="p">,</span><span class="s">&quot;DB %d: %lld keys (%lld volatile) in %lld slots HT.&quot;</span><span class="p">,</span><span class="n">j</span><span class="p">,</span><span class="n">used</span><span class="p">,</span><span class="n">vkeys</span><span class="p">,</span><span class="n">size</span><span class="p">);</span>
                <span class="cm">/* dictPrintStats(server.dict); */</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="cm">/* We don&#39;t want to resize the hash tables while a bacground saving</span>
<span class="cm">     * is in progress: the saving child is created using fork() that is</span>
<span class="cm">     * implemented with a copy-on-write semantic in most modern systems, so</span>
<span class="cm">     * if we resize the HT while there is the saving child at work actually</span>
<span class="cm">     * a lot of memory movements in the parent will cause a lot of pages</span>
<span class="cm">     * copied. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="n">server</span><span class="p">.</span><span class="n">aof_child_pid</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">tryResizeHashTables</span><span class="p">();</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">activerehashing</span><span class="p">)</span> <span class="n">incrementallyRehash</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="cm">/* Show information about connected clients */</span>
    <span class="n">run_with_period</span><span class="p">(</span><span class="mi">5000</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_VERBOSE</span><span class="p">,</span><span class="s">&quot;%d clients connected (%d slaves), %zu bytes in use&quot;</span><span class="p">,</span>
            <span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">clients</span><span class="p">)</span><span class="o">-</span><span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">slaves</span><span class="p">),</span>
            <span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">slaves</span><span class="p">),</span>
            <span class="n">zmalloc_used_memory</span><span class="p">());</span>
    <span class="p">}</span>

    <span class="cm">/* We need to do a few operations on clients asynchronously. */</span>
    <span class="n">clientsCron</span><span class="p">();</span>

    <span class="cm">/* Start a scheduled AOF rewrite if this was requested by the user while</span>
<span class="cm">     * a BGSAVE was in progress. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="n">server</span><span class="p">.</span><span class="n">aof_child_pid</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span>
        <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_scheduled</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">rewriteAppendOnlyFileBackground</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="cm">/* Check if a background saving or AOF rewrite in progress terminated. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">||</span> <span class="n">server</span><span class="p">.</span><span class="n">aof_child_pid</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">statloc</span><span class="p">;</span>
        <span class="n">pid_t</span> <span class="n">pid</span><span class="p">;</span>

        <span class="k">if</span> <span class="p">((</span><span class="n">pid</span> <span class="o">=</span> <span class="n">wait3</span><span class="p">(</span><span class="o">&amp;</span><span class="n">statloc</span><span class="p">,</span><span class="n">WNOHANG</span><span class="p">,</span><span class="nb">NULL</span><span class="p">))</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
            <span class="kt">int</span> <span class="n">exitcode</span> <span class="o">=</span> <span class="n">WEXITSTATUS</span><span class="p">(</span><span class="n">statloc</span><span class="p">);</span>
            <span class="kt">int</span> <span class="n">bysignal</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">WIFSIGNALED</span><span class="p">(</span><span class="n">statloc</span><span class="p">))</span> <span class="n">bysignal</span> <span class="o">=</span> <span class="n">WTERMSIG</span><span class="p">(</span><span class="n">statloc</span><span class="p">);</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">pid</span> <span class="o">==</span> <span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">backgroundSaveDoneHandler</span><span class="p">(</span><span class="n">exitcode</span><span class="p">,</span><span class="n">bysignal</span><span class="p">);</span>
            <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                <span class="n">backgroundRewriteDoneHandler</span><span class="p">(</span><span class="n">exitcode</span><span class="p">,</span><span class="n">bysignal</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="n">updateDictResizePolicy</span><span class="p">();</span>
        <span class="p">}</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="cm">/* If there is not a background saving/rewrite in progress check if</span>
<span class="cm">         * we have to save/rewrite now */</span>
         <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">saveparamslen</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">struct</span> <span class="n">saveparam</span> <span class="o">*</span><span class="n">sp</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">saveparams</span><span class="o">+</span><span class="n">j</span><span class="p">;</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">dirty</span> <span class="o">&gt;=</span> <span class="n">sp</span><span class="o">-&gt;</span><span class="n">changes</span> <span class="o">&amp;&amp;</span>
                <span class="n">server</span><span class="p">.</span><span class="n">unixtime</span><span class="o">-</span><span class="n">server</span><span class="p">.</span><span class="n">lastsave</span> <span class="o">&gt;</span> <span class="n">sp</span><span class="o">-&gt;</span><span class="n">seconds</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;%d changes in %d seconds. Saving...&quot;</span><span class="p">,</span>
                    <span class="n">sp</span><span class="o">-&gt;</span><span class="n">changes</span><span class="p">,</span> <span class="n">sp</span><span class="o">-&gt;</span><span class="n">seconds</span><span class="p">);</span>
                <span class="n">rdbSaveBackground</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_filename</span><span class="p">);</span>
                <span class="k">break</span><span class="p">;</span>
            <span class="p">}</span>
         <span class="p">}</span>

         <span class="cm">/* Trigger an AOF rewrite if needed */</span>
         <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span>
             <span class="n">server</span><span class="p">.</span><span class="n">aof_child_pid</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span>
             <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_perc</span> <span class="o">&amp;&amp;</span>
             <span class="n">server</span><span class="p">.</span><span class="n">aof_current_size</span> <span class="o">&gt;</span> <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_min_size</span><span class="p">)</span>
         <span class="p">{</span>
            <span class="kt">long</span> <span class="kt">long</span> <span class="n">base</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_base_size</span> <span class="o">?</span>
                            <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_base_size</span> <span class="o">:</span> <span class="mi">1</span><span class="p">;</span>
            <span class="kt">long</span> <span class="kt">long</span> <span class="n">growth</span> <span class="o">=</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_current_size</span><span class="o">*</span><span class="mi">100</span><span class="o">/</span><span class="n">base</span><span class="p">)</span> <span class="o">-</span> <span class="mi">100</span><span class="p">;</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">growth</span> <span class="o">&gt;=</span> <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_perc</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;Starting automatic rewriting of AOF on %lld%% growth&quot;</span><span class="p">,</span><span class="n">growth</span><span class="p">);</span>
                <span class="n">rewriteAppendOnlyFileBackground</span><span class="p">();</span>
            <span class="p">}</span>
         <span class="p">}</span>
    <span class="p">}</span>


    <span class="cm">/* If we postponed an AOF buffer flush, let&#39;s try to do it every time the</span>
<span class="cm">     * cron function is called. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_flush_postponed_start</span><span class="p">)</span> <span class="n">flushAppendOnlyFile</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>

    <span class="cm">/* Expire a few keys per cycle, only if this is a master.</span>
<span class="cm">     * On slaves we wait for DEL operations synthesized by the master</span>
<span class="cm">     * in order to guarantee a strict consistency. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">masterhost</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="n">activeExpireCycle</span><span class="p">();</span>

    <span class="cm">/* Close clients that need to be closed asynchronous */</span>
    <span class="n">freeClientsInAsyncFreeQueue</span><span class="p">();</span>

    <span class="cm">/* Replication cron function -- used to reconnect to master and</span>
<span class="cm">     * to detect transfer failures. */</span>
    <span class="n">run_with_period</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="n">replicationCron</span><span class="p">();</span>

    <span class="cm">/* Run other sub-systems specific cron jobs */</span>
    <span class="n">run_with_period</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">cluster_enabled</span><span class="p">)</span> <span class="n">clusterCron</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="n">server</span><span class="p">.</span><span class="n">cronloops</span><span class="o">++</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">1000</span><span class="o">/</span><span class="n">REDIS_HZ</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* This function gets called every time Redis is entering the</span>
<span class="cm"> * main loop of the event driven library, that is, before to sleep</span>
<span class="cm"> * for ready file descriptors. */</span>
<span class="kt">void</span> <span class="nf">beforeSleep</span><span class="p">(</span><span class="k">struct</span> <span class="n">aeEventLoop</span> <span class="o">*</span><span class="n">eventLoop</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">REDIS_NOTUSED</span><span class="p">(</span><span class="n">eventLoop</span><span class="p">);</span>
    <span class="n">listNode</span> <span class="o">*</span><span class="n">ln</span><span class="p">;</span>
    <span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">;</span>

    <span class="cm">/* Try to process pending commands for clients that were just unblocked. */</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">unblocked_clients</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">ln</span> <span class="o">=</span> <span class="n">listFirst</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">unblocked_clients</span><span class="p">);</span>
        <span class="n">redisAssert</span><span class="p">(</span><span class="n">ln</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">);</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">ln</span><span class="o">-&gt;</span><span class="n">value</span><span class="p">;</span>
        <span class="n">listDelNode</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">unblocked_clients</span><span class="p">,</span><span class="n">ln</span><span class="p">);</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;=</span> <span class="o">~</span><span class="n">REDIS_UNBLOCKED</span><span class="p">;</span>

        <span class="cm">/* Process remaining data in the input buffer. */</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">querybuf</span> <span class="o">&amp;&amp;</span> <span class="n">sdslen</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">querybuf</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">server</span><span class="p">.</span><span class="n">current_client</span> <span class="o">=</span> <span class="n">c</span><span class="p">;</span>
            <span class="n">processInputBuffer</span><span class="p">(</span><span class="n">c</span><span class="p">);</span>
            <span class="n">server</span><span class="p">.</span><span class="n">current_client</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="cm">/* Write the AOF buffer on disk */</span>
    <span class="n">flushAppendOnlyFile</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="cm">/* =========================== Server initialization ======================== */</span>

<span class="kt">void</span> <span class="nf">createSharedObjects</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">;</span>

    <span class="n">shared</span><span class="p">.</span><span class="n">crlf</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">ok</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;+OK</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">err</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;-ERR</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">emptybulk</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;$0</span><span class="se">\r\n\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">czero</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;:0</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">cone</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;:1</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">cnegone</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;:-1</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">nullbulk</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;$-1</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">nullmultibulk</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;*-1</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">emptymultibulk</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;*0</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">pong</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;+PONG</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">queued</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;+QUEUED</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">wrongtypeerr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-ERR Operation against a key holding the wrong kind of value</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">nokeyerr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-ERR no such key</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">syntaxerr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-ERR syntax error</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">sameobjecterr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-ERR source and destination objects are the same</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">outofrangeerr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-ERR index out of range</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">noscripterr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-NOSCRIPT No matching script. Please use EVAL.</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">loadingerr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-LOADING Redis is loading the dataset in memory</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">slowscripterr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">masterdownerr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-MASTERDOWN Link with MASTER is down and slave-serve-stale-data is set to &#39;no&#39;.</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">bgsaveerr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">roslaveerr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-READONLY You can&#39;t write against a read only slave.</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">oomerr</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span>
        <span class="s">&quot;-OOM command not allowed when used memory &gt; &#39;maxmemory&#39;.</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">space</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">colon</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;:&quot;</span><span class="p">));</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">plus</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span><span class="n">sdsnew</span><span class="p">(</span><span class="s">&quot;+&quot;</span><span class="p">));</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">REDIS_SHARED_SELECT_CMDS</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">shared</span><span class="p">.</span><span class="n">select</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span>
            <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">sdsempty</span><span class="p">(),</span><span class="s">&quot;select %d</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">j</span><span class="p">));</span>
    <span class="p">}</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">messagebulk</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="s">&quot;$7</span><span class="se">\r\n</span><span class="s">message</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span><span class="mi">13</span><span class="p">);</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">pmessagebulk</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="s">&quot;$8</span><span class="se">\r\n</span><span class="s">pmessage</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span><span class="mi">14</span><span class="p">);</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">subscribebulk</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="s">&quot;$9</span><span class="se">\r\n</span><span class="s">subscribe</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span><span class="mi">15</span><span class="p">);</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">unsubscribebulk</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="s">&quot;$11</span><span class="se">\r\n</span><span class="s">unsubscribe</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span><span class="mi">18</span><span class="p">);</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">psubscribebulk</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="s">&quot;$10</span><span class="se">\r\n</span><span class="s">psubscribe</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span><span class="mi">17</span><span class="p">);</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">punsubscribebulk</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="s">&quot;$12</span><span class="se">\r\n</span><span class="s">punsubscribe</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span><span class="mi">19</span><span class="p">);</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">del</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="s">&quot;DEL&quot;</span><span class="p">,</span><span class="mi">3</span><span class="p">);</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">rpop</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="s">&quot;RPOP&quot;</span><span class="p">,</span><span class="mi">4</span><span class="p">);</span>
    <span class="n">shared</span><span class="p">.</span><span class="n">lpop</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="s">&quot;LPOP&quot;</span><span class="p">,</span><span class="mi">4</span><span class="p">);</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">REDIS_SHARED_INTEGERS</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">shared</span><span class="p">.</span><span class="n">integers</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,(</span><span class="kt">void</span><span class="o">*</span><span class="p">)(</span><span class="kt">long</span><span class="p">)</span><span class="n">j</span><span class="p">);</span>
        <span class="n">shared</span><span class="p">.</span><span class="n">integers</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">-&gt;</span><span class="n">encoding</span> <span class="o">=</span> <span class="n">REDIS_ENCODING_INT</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">REDIS_SHARED_BULKHDR_LEN</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">shared</span><span class="p">.</span><span class="n">mbulkhdr</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span>
            <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">sdsempty</span><span class="p">(),</span><span class="s">&quot;*%d</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span><span class="n">j</span><span class="p">));</span>
        <span class="n">shared</span><span class="p">.</span><span class="n">bulkhdr</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">createObject</span><span class="p">(</span><span class="n">REDIS_STRING</span><span class="p">,</span>
            <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">sdsempty</span><span class="p">(),</span><span class="s">&quot;$%d</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span><span class="n">j</span><span class="p">));</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">initServerConfig</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">getRandomHexChars</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">runid</span><span class="p">,</span><span class="n">REDIS_RUN_ID_SIZE</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">runid</span><span class="p">[</span><span class="n">REDIS_RUN_ID_SIZE</span><span class="p">]</span> <span class="o">=</span> <span class="sc">&#39;\0&#39;</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">arch_bits</span> <span class="o">=</span> <span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="kt">long</span><span class="p">)</span> <span class="o">==</span> <span class="mi">8</span><span class="p">)</span> <span class="o">?</span> <span class="mi">64</span> <span class="o">:</span> <span class="mi">32</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">port</span> <span class="o">=</span> <span class="n">REDIS_SERVERPORT</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">bindaddr</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">unixsocket</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">unixsocketperm</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">ipfd</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">sofd</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">dbnum</span> <span class="o">=</span> <span class="n">REDIS_DEFAULT_DBNUM</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">verbosity</span> <span class="o">=</span> <span class="n">REDIS_NOTICE</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">maxidletime</span> <span class="o">=</span> <span class="n">REDIS_MAXIDLETIME</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">client_max_querybuf_len</span> <span class="o">=</span> <span class="n">REDIS_MAX_QUERYBUF_LEN</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">saveparams</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">loading</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">logfile</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="cm">/* NULL = log on standard output */</span>
    <span class="n">server</span><span class="p">.</span><span class="n">syslog_enabled</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">syslog_ident</span> <span class="o">=</span> <span class="n">zstrdup</span><span class="p">(</span><span class="s">&quot;redis&quot;</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">syslog_facility</span> <span class="o">=</span> <span class="n">LOG_LOCAL0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">daemonize</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_state</span> <span class="o">=</span> <span class="n">REDIS_AOF_OFF</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_fsync</span> <span class="o">=</span> <span class="n">AOF_FSYNC_EVERYSEC</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_no_fsync_on_rewrite</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_perc</span> <span class="o">=</span> <span class="n">REDIS_AOF_REWRITE_PERC</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_min_size</span> <span class="o">=</span> <span class="n">REDIS_AOF_REWRITE_MIN_SIZE</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_base_size</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_scheduled</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_last_fsync</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_time_last</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_time_start</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_delayed_fsync</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_fd</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_selected_db</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="cm">/* Make sure the first time will not match */</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_flush_postponed_start</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">pidfile</span> <span class="o">=</span> <span class="n">zstrdup</span><span class="p">(</span><span class="s">&quot;/var/run/redis.pid&quot;</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">rdb_filename</span> <span class="o">=</span> <span class="n">zstrdup</span><span class="p">(</span><span class="s">&quot;dump.rdb&quot;</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_filename</span> <span class="o">=</span> <span class="n">zstrdup</span><span class="p">(</span><span class="s">&quot;appendonly.aof&quot;</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">requirepass</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">rdb_compression</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">rdb_checksum</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">activerehashing</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">maxclients</span> <span class="o">=</span> <span class="n">REDIS_MAX_CLIENTS</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">bpop_blocked_clients</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">maxmemory</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">=</span> <span class="n">REDIS_MAXMEMORY_VOLATILE_LRU</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">maxmemory_samples</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">hash_max_ziplist_entries</span> <span class="o">=</span> <span class="n">REDIS_HASH_MAX_ZIPLIST_ENTRIES</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">hash_max_ziplist_value</span> <span class="o">=</span> <span class="n">REDIS_HASH_MAX_ZIPLIST_VALUE</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">list_max_ziplist_entries</span> <span class="o">=</span> <span class="n">REDIS_LIST_MAX_ZIPLIST_ENTRIES</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">list_max_ziplist_value</span> <span class="o">=</span> <span class="n">REDIS_LIST_MAX_ZIPLIST_VALUE</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">set_max_intset_entries</span> <span class="o">=</span> <span class="n">REDIS_SET_MAX_INTSET_ENTRIES</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">zset_max_ziplist_entries</span> <span class="o">=</span> <span class="n">REDIS_ZSET_MAX_ZIPLIST_ENTRIES</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">zset_max_ziplist_value</span> <span class="o">=</span> <span class="n">REDIS_ZSET_MAX_ZIPLIST_VALUE</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">shutdown_asap</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">repl_ping_slave_period</span> <span class="o">=</span> <span class="n">REDIS_REPL_PING_SLAVE_PERIOD</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">repl_timeout</span> <span class="o">=</span> <span class="n">REDIS_REPL_TIMEOUT</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">cluster_enabled</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">cluster</span><span class="p">.</span><span class="n">configfile</span> <span class="o">=</span> <span class="n">zstrdup</span><span class="p">(</span><span class="s">&quot;nodes.conf&quot;</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">lua_caller</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">lua_time_limit</span> <span class="o">=</span> <span class="n">REDIS_LUA_TIME_LIMIT</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">lua_client</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">lua_timedout</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="n">updateLRUClock</span><span class="p">();</span>
    <span class="n">resetServerSaveParams</span><span class="p">();</span>

    <span class="n">appendServerSaveParams</span><span class="p">(</span><span class="mi">60</span><span class="o">*</span><span class="mi">60</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>  <span class="cm">/* save after 1 hour and 1 change */</span>
    <span class="n">appendServerSaveParams</span><span class="p">(</span><span class="mi">300</span><span class="p">,</span><span class="mi">100</span><span class="p">);</span>  <span class="cm">/* save after 5 minutes and 100 changes */</span>
    <span class="n">appendServerSaveParams</span><span class="p">(</span><span class="mi">60</span><span class="p">,</span><span class="mi">10000</span><span class="p">);</span> <span class="cm">/* save after 1 minute and 10000 changes */</span>
    <span class="cm">/* Replication related */</span>
    <span class="n">server</span><span class="p">.</span><span class="n">masterauth</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">masterhost</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">masterport</span> <span class="o">=</span> <span class="mi">6379</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">master</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">repl_state</span> <span class="o">=</span> <span class="n">REDIS_REPL_NONE</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">repl_syncio_timeout</span> <span class="o">=</span> <span class="n">REDIS_REPL_SYNCIO_TIMEOUT</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">repl_serve_stale_data</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">repl_slave_ro</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">repl_down_since</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">);</span>

    <span class="cm">/* Client output buffer limits */</span>
    <span class="n">server</span><span class="p">.</span><span class="n">client_obuf_limits</span><span class="p">[</span><span class="n">REDIS_CLIENT_LIMIT_CLASS_NORMAL</span><span class="p">].</span><span class="n">hard_limit_bytes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">client_obuf_limits</span><span class="p">[</span><span class="n">REDIS_CLIENT_LIMIT_CLASS_NORMAL</span><span class="p">].</span><span class="n">soft_limit_bytes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">client_obuf_limits</span><span class="p">[</span><span class="n">REDIS_CLIENT_LIMIT_CLASS_NORMAL</span><span class="p">].</span><span class="n">soft_limit_seconds</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">client_obuf_limits</span><span class="p">[</span><span class="n">REDIS_CLIENT_LIMIT_CLASS_SLAVE</span><span class="p">].</span><span class="n">hard_limit_bytes</span> <span class="o">=</span> <span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="o">*</span><span class="mi">256</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">client_obuf_limits</span><span class="p">[</span><span class="n">REDIS_CLIENT_LIMIT_CLASS_SLAVE</span><span class="p">].</span><span class="n">soft_limit_bytes</span> <span class="o">=</span> <span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="o">*</span><span class="mi">64</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">client_obuf_limits</span><span class="p">[</span><span class="n">REDIS_CLIENT_LIMIT_CLASS_SLAVE</span><span class="p">].</span><span class="n">soft_limit_seconds</span> <span class="o">=</span> <span class="mi">60</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">client_obuf_limits</span><span class="p">[</span><span class="n">REDIS_CLIENT_LIMIT_CLASS_PUBSUB</span><span class="p">].</span><span class="n">hard_limit_bytes</span> <span class="o">=</span> <span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="o">*</span><span class="mi">32</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">client_obuf_limits</span><span class="p">[</span><span class="n">REDIS_CLIENT_LIMIT_CLASS_PUBSUB</span><span class="p">].</span><span class="n">soft_limit_bytes</span> <span class="o">=</span> <span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="o">*</span><span class="mi">8</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">client_obuf_limits</span><span class="p">[</span><span class="n">REDIS_CLIENT_LIMIT_CLASS_PUBSUB</span><span class="p">].</span><span class="n">soft_limit_seconds</span> <span class="o">=</span> <span class="mi">60</span><span class="p">;</span>

    <span class="cm">/* Double constants initialization */</span>
    <span class="n">R_Zero</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span>
    <span class="n">R_PosInf</span> <span class="o">=</span> <span class="mf">1.0</span><span class="o">/</span><span class="n">R_Zero</span><span class="p">;</span>
    <span class="n">R_NegInf</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1.0</span><span class="o">/</span><span class="n">R_Zero</span><span class="p">;</span>
    <span class="n">R_Nan</span> <span class="o">=</span> <span class="n">R_Zero</span><span class="o">/</span><span class="n">R_Zero</span><span class="p">;</span>

    <span class="cm">/* Command table -- we intiialize it here as it is part of the</span>
<span class="cm">     * initial configuration, since command names may be changed via</span>
<span class="cm">     * redis.conf using the rename-command directive. */</span>
    <span class="n">server</span><span class="p">.</span><span class="n">commands</span> <span class="o">=</span> <span class="n">dictCreate</span><span class="p">(</span><span class="o">&amp;</span><span class="n">commandTableDictType</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
    <span class="n">populateCommandTable</span><span class="p">();</span>
    <span class="n">server</span><span class="p">.</span><span class="n">delCommand</span> <span class="o">=</span> <span class="n">lookupCommandByCString</span><span class="p">(</span><span class="s">&quot;del&quot;</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">multiCommand</span> <span class="o">=</span> <span class="n">lookupCommandByCString</span><span class="p">(</span><span class="s">&quot;multi&quot;</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">lpushCommand</span> <span class="o">=</span> <span class="n">lookupCommandByCString</span><span class="p">(</span><span class="s">&quot;lpush&quot;</span><span class="p">);</span>

    <span class="cm">/* Slow log */</span>
    <span class="n">server</span><span class="p">.</span><span class="n">slowlog_log_slower_than</span> <span class="o">=</span> <span class="n">REDIS_SLOWLOG_LOG_SLOWER_THAN</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">slowlog_max_len</span> <span class="o">=</span> <span class="n">REDIS_SLOWLOG_MAX_LEN</span><span class="p">;</span>

    <span class="cm">/* Debugging */</span>
    <span class="n">server</span><span class="p">.</span><span class="n">assert_failed</span> <span class="o">=</span> <span class="s">&quot;&lt;no assertion failed&gt;&quot;</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">assert_file</span> <span class="o">=</span> <span class="s">&quot;&lt;no file&gt;&quot;</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">assert_line</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">bug_report_start</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">watchdog_period</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* This function will try to raise the max number of open files accordingly to</span>
<span class="cm"> * the configured max number of clients. It will also account for 32 additional</span>
<span class="cm"> * file descriptors as we need a few more for persistence, listening</span>
<span class="cm"> * sockets, log files and so forth.</span>
<span class="cm"> *</span>
<span class="cm"> * If it will not be possible to set the limit accordingly to the configured</span>
<span class="cm"> * max number of clients, the function will do the reverse setting</span>
<span class="cm"> * server.maxclients to the value that we can actually handle. */</span>
<span class="kt">void</span> <span class="nf">adjustOpenFilesLimit</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">rlim_t</span> <span class="n">maxfiles</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">maxclients</span><span class="o">+</span><span class="mi">32</span><span class="p">;</span>
    <span class="k">struct</span> <span class="n">rlimit</span> <span class="n">limit</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">getrlimit</span><span class="p">(</span><span class="n">RLIMIT_NOFILE</span><span class="p">,</span><span class="o">&amp;</span><span class="n">limit</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;Unable to obtain the current NOFILE limit (%s), assuming 1024 and setting the max clients configuration accordingly.&quot;</span><span class="p">,</span>
            <span class="n">strerror</span><span class="p">(</span><span class="n">errno</span><span class="p">));</span>
        <span class="n">server</span><span class="p">.</span><span class="n">maxclients</span> <span class="o">=</span> <span class="mi">1024</span><span class="o">-</span><span class="mi">32</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="n">rlim_t</span> <span class="n">oldlimit</span> <span class="o">=</span> <span class="n">limit</span><span class="p">.</span><span class="n">rlim_cur</span><span class="p">;</span>

        <span class="cm">/* Set the max number of files if the current limit is not enough</span>
<span class="cm">         * for our needs. */</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">oldlimit</span> <span class="o">&lt;</span> <span class="n">maxfiles</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">rlim_t</span> <span class="n">f</span><span class="p">;</span>

            <span class="n">f</span> <span class="o">=</span> <span class="n">maxfiles</span><span class="p">;</span>
            <span class="k">while</span><span class="p">(</span><span class="n">f</span> <span class="o">&gt;</span> <span class="n">oldlimit</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">limit</span><span class="p">.</span><span class="n">rlim_cur</span> <span class="o">=</span> <span class="n">f</span><span class="p">;</span>
                <span class="n">limit</span><span class="p">.</span><span class="n">rlim_max</span> <span class="o">=</span> <span class="n">f</span><span class="p">;</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">setrlimit</span><span class="p">(</span><span class="n">RLIMIT_NOFILE</span><span class="p">,</span><span class="o">&amp;</span><span class="n">limit</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span>
                <span class="n">f</span> <span class="o">-=</span> <span class="mi">128</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">f</span> <span class="o">&lt;</span> <span class="n">oldlimit</span><span class="p">)</span> <span class="n">f</span> <span class="o">=</span> <span class="n">oldlimit</span><span class="p">;</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">f</span> <span class="o">!=</span> <span class="n">maxfiles</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">server</span><span class="p">.</span><span class="n">maxclients</span> <span class="o">=</span> <span class="n">f</span><span class="o">-</span><span class="mi">32</span><span class="p">;</span>
                <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;Unable to set the max number of files limit to %d (%s), setting the max clients configuration to %d.&quot;</span><span class="p">,</span>
                    <span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="n">maxfiles</span><span class="p">,</span> <span class="n">strerror</span><span class="p">(</span><span class="n">errno</span><span class="p">),</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="n">server</span><span class="p">.</span><span class="n">maxclients</span><span class="p">);</span>
            <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;Max number of open files set to %d&quot;</span><span class="p">,</span>
                    <span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="n">maxfiles</span><span class="p">);</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">initServer</span><span class="p">()</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">;</span>

    <span class="n">signal</span><span class="p">(</span><span class="n">SIGHUP</span><span class="p">,</span> <span class="n">SIG_IGN</span><span class="p">);</span>
    <span class="n">signal</span><span class="p">(</span><span class="n">SIGPIPE</span><span class="p">,</span> <span class="n">SIG_IGN</span><span class="p">);</span>
    <span class="n">setupSignalHandlers</span><span class="p">();</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">syslog_enabled</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">openlog</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">syslog_ident</span><span class="p">,</span> <span class="n">LOG_PID</span> <span class="o">|</span> <span class="n">LOG_NDELAY</span> <span class="o">|</span> <span class="n">LOG_NOWAIT</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">syslog_facility</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="n">server</span><span class="p">.</span><span class="n">current_client</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">clients</span> <span class="o">=</span> <span class="n">listCreate</span><span class="p">();</span>
    <span class="n">server</span><span class="p">.</span><span class="n">clients_to_close</span> <span class="o">=</span> <span class="n">listCreate</span><span class="p">();</span>
    <span class="n">server</span><span class="p">.</span><span class="n">slaves</span> <span class="o">=</span> <span class="n">listCreate</span><span class="p">();</span>
    <span class="n">server</span><span class="p">.</span><span class="n">monitors</span> <span class="o">=</span> <span class="n">listCreate</span><span class="p">();</span>
    <span class="n">server</span><span class="p">.</span><span class="n">unblocked_clients</span> <span class="o">=</span> <span class="n">listCreate</span><span class="p">();</span>

    <span class="n">createSharedObjects</span><span class="p">();</span>
    <span class="n">adjustOpenFilesLimit</span><span class="p">();</span>
    <span class="n">server</span><span class="p">.</span><span class="n">el</span> <span class="o">=</span> <span class="n">aeCreateEventLoop</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">maxclients</span><span class="o">+</span><span class="mi">1024</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">zmalloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">redisDb</span><span class="p">)</span><span class="o">*</span><span class="n">server</span><span class="p">.</span><span class="n">dbnum</span><span class="p">);</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">port</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">server</span><span class="p">.</span><span class="n">ipfd</span> <span class="o">=</span> <span class="n">anetTcpServer</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">neterr</span><span class="p">,</span><span class="n">server</span><span class="p">.</span><span class="n">port</span><span class="p">,</span><span class="n">server</span><span class="p">.</span><span class="n">bindaddr</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">ipfd</span> <span class="o">==</span> <span class="n">ANET_ERR</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span> <span class="s">&quot;Opening port %d: %s&quot;</span><span class="p">,</span>
                <span class="n">server</span><span class="p">.</span><span class="n">port</span><span class="p">,</span> <span class="n">server</span><span class="p">.</span><span class="n">neterr</span><span class="p">);</span>
            <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">unixsocket</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">unlink</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">unixsocket</span><span class="p">);</span> <span class="cm">/* don&#39;t care if this fails */</span>
        <span class="n">server</span><span class="p">.</span><span class="n">sofd</span> <span class="o">=</span> <span class="n">anetUnixServer</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">neterr</span><span class="p">,</span><span class="n">server</span><span class="p">.</span><span class="n">unixsocket</span><span class="p">,</span><span class="n">server</span><span class="p">.</span><span class="n">unixsocketperm</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">sofd</span> <span class="o">==</span> <span class="n">ANET_ERR</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span> <span class="s">&quot;Opening socket: %s&quot;</span><span class="p">,</span> <span class="n">server</span><span class="p">.</span><span class="n">neterr</span><span class="p">);</span>
            <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">ipfd</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">server</span><span class="p">.</span><span class="n">sofd</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span> <span class="s">&quot;Configured to not listen anywhere, exiting.&quot;</span><span class="p">);</span>
        <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">dbnum</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">dict</span> <span class="o">=</span> <span class="n">dictCreate</span><span class="p">(</span><span class="o">&amp;</span><span class="n">dbDictType</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
        <span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">expires</span> <span class="o">=</span> <span class="n">dictCreate</span><span class="p">(</span><span class="o">&amp;</span><span class="n">keyptrDictType</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
        <span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">blocking_keys</span> <span class="o">=</span> <span class="n">dictCreate</span><span class="p">(</span><span class="o">&amp;</span><span class="n">keylistDictType</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
        <span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">watched_keys</span> <span class="o">=</span> <span class="n">dictCreate</span><span class="p">(</span><span class="o">&amp;</span><span class="n">keylistDictType</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
        <span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">id</span> <span class="o">=</span> <span class="n">j</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">server</span><span class="p">.</span><span class="n">pubsub_channels</span> <span class="o">=</span> <span class="n">dictCreate</span><span class="p">(</span><span class="o">&amp;</span><span class="n">keylistDictType</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">pubsub_patterns</span> <span class="o">=</span> <span class="n">listCreate</span><span class="p">();</span>
    <span class="n">listSetFreeMethod</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">pubsub_patterns</span><span class="p">,</span><span class="n">freePubsubPattern</span><span class="p">);</span>
    <span class="n">listSetMatchMethod</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">pubsub_patterns</span><span class="p">,</span><span class="n">listMatchPubsubPattern</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">cronloops</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_child_pid</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">aofRewriteBufferReset</span><span class="p">();</span>
    <span class="n">server</span><span class="p">.</span><span class="n">aof_buf</span> <span class="o">=</span> <span class="n">sdsempty</span><span class="p">();</span>
    <span class="n">server</span><span class="p">.</span><span class="n">lastsave</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">rdb_save_time_last</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">rdb_save_time_start</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">dirty</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_numcommands</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_numconnections</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_expiredkeys</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_evictedkeys</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_starttime</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_keyspace_misses</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_keyspace_hits</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_peak_memory</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_fork_time</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_rejected_conn</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">memset</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">ops_sec_samples</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="k">sizeof</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">ops_sec_samples</span><span class="p">));</span>
    <span class="n">server</span><span class="p">.</span><span class="n">ops_sec_idx</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">ops_sec_last_sample_time</span> <span class="o">=</span> <span class="n">mstime</span><span class="p">();</span>
    <span class="n">server</span><span class="p">.</span><span class="n">ops_sec_last_sample_ops</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">unixtime</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">lastbgsave_status</span> <span class="o">=</span> <span class="n">REDIS_OK</span><span class="p">;</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stop_writes_on_bgsave_err</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="n">aeCreateTimeEvent</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">el</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">serverCron</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">ipfd</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">aeCreateFileEvent</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">el</span><span class="p">,</span><span class="n">server</span><span class="p">.</span><span class="n">ipfd</span><span class="p">,</span><span class="n">AE_READABLE</span><span class="p">,</span>
        <span class="n">acceptTcpHandler</span><span class="p">,</span><span class="nb">NULL</span><span class="p">)</span> <span class="o">==</span> <span class="n">AE_ERR</span><span class="p">)</span> <span class="n">oom</span><span class="p">(</span><span class="s">&quot;creating file event&quot;</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">sofd</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">aeCreateFileEvent</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">el</span><span class="p">,</span><span class="n">server</span><span class="p">.</span><span class="n">sofd</span><span class="p">,</span><span class="n">AE_READABLE</span><span class="p">,</span>
        <span class="n">acceptUnixHandler</span><span class="p">,</span><span class="nb">NULL</span><span class="p">)</span> <span class="o">==</span> <span class="n">AE_ERR</span><span class="p">)</span> <span class="n">oom</span><span class="p">(</span><span class="s">&quot;creating file event&quot;</span><span class="p">);</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_state</span> <span class="o">==</span> <span class="n">REDIS_AOF_ON</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">server</span><span class="p">.</span><span class="n">aof_fd</span> <span class="o">=</span> <span class="n">open</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_filename</span><span class="p">,</span>
                               <span class="n">O_WRONLY</span><span class="o">|</span><span class="n">O_APPEND</span><span class="o">|</span><span class="n">O_CREAT</span><span class="p">,</span><span class="mo">0644</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_fd</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span> <span class="s">&quot;Can&#39;t open the append-only file: %s&quot;</span><span class="p">,</span>
                <span class="n">strerror</span><span class="p">(</span><span class="n">errno</span><span class="p">));</span>
            <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="cm">/* 32 bit instances are limited to 4GB of address space, so if there is</span>
<span class="cm">     * no explicit limit in the user provided configuration we set a limit</span>
<span class="cm">     * at 3.5GB using maxmemory with &#39;noeviction&#39; policy&#39;. This saves</span>
<span class="cm">     * useless crashes of the Redis instance. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">arch_bits</span> <span class="o">==</span> <span class="mi">32</span> <span class="o">&amp;&amp;</span> <span class="n">server</span><span class="p">.</span><span class="n">maxmemory</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;Warning: 32 bit instance detected but no memory limit set. Setting 3.5 GB maxmemory limit with &#39;noeviction&#39; policy now.&quot;</span><span class="p">);</span>
        <span class="n">server</span><span class="p">.</span><span class="n">maxmemory</span> <span class="o">=</span> <span class="mi">3584LL</span><span class="o">*</span><span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">);</span> <span class="cm">/* 3584 MB = 3.5 GB */</span>
        <span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">=</span> <span class="n">REDIS_MAXMEMORY_NO_EVICTION</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">cluster_enabled</span><span class="p">)</span> <span class="n">clusterInit</span><span class="p">();</span>
    <span class="n">scriptingInit</span><span class="p">();</span>
    <span class="n">slowlogInit</span><span class="p">();</span>
    <span class="n">bioInit</span><span class="p">();</span>
<span class="p">}</span>

<span class="cm">/* Populates the Redis Command Table starting from the hard coded list</span>
<span class="cm"> * we have on top of redis.c file. */</span>
<span class="kt">void</span> <span class="nf">populateCommandTable</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">numcommands</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">redisCommandTable</span><span class="p">)</span><span class="o">/</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">redisCommand</span><span class="p">);</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">numcommands</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">struct</span> <span class="n">redisCommand</span> <span class="o">*</span><span class="n">c</span> <span class="o">=</span> <span class="n">redisCommandTable</span><span class="o">+</span><span class="n">j</span><span class="p">;</span>
        <span class="kt">char</span> <span class="o">*</span><span class="n">f</span> <span class="o">=</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">sflags</span><span class="p">;</span>
        <span class="kt">int</span> <span class="n">retval</span><span class="p">;</span>

        <span class="k">while</span><span class="p">(</span><span class="o">*</span><span class="n">f</span> <span class="o">!=</span> <span class="sc">&#39;\0&#39;</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">switch</span><span class="p">(</span><span class="o">*</span><span class="n">f</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">case</span> <span class="sc">&#39;w&#39;</span>: <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_CMD_WRITE</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span>
            <span class="k">case</span> <span class="sc">&#39;r&#39;</span>: <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_CMD_READONLY</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span>
            <span class="k">case</span> <span class="sc">&#39;m&#39;</span>: <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_CMD_DENYOOM</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span>
            <span class="k">case</span> <span class="sc">&#39;a&#39;</span>: <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_CMD_ADMIN</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span>
            <span class="k">case</span> <span class="sc">&#39;p&#39;</span>: <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_CMD_PUBSUB</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span>
            <span class="k">case</span> <span class="sc">&#39;f&#39;</span>: <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_CMD_FORCE_REPLICATION</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span>
            <span class="k">case</span> <span class="sc">&#39;s&#39;</span>: <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_CMD_NOSCRIPT</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span>
            <span class="k">case</span> <span class="sc">&#39;R&#39;</span>: <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_CMD_RANDOM</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span>
            <span class="k">case</span> <span class="sc">&#39;S&#39;</span>: <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_CMD_SORT_FOR_SCRIPT</span><span class="p">;</span> <span class="k">break</span><span class="p">;</span>
            <span class="nl">default:</span> <span class="n">redisPanic</span><span class="p">(</span><span class="s">&quot;Unsupported command flag&quot;</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="n">f</span><span class="o">++</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="n">retval</span> <span class="o">=</span> <span class="n">dictAdd</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">commands</span><span class="p">,</span> <span class="n">sdsnew</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">name</span><span class="p">),</span> <span class="n">c</span><span class="p">);</span>
        <span class="n">assert</span><span class="p">(</span><span class="n">retval</span> <span class="o">==</span> <span class="n">DICT_OK</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">resetCommandTableStats</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">numcommands</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">redisCommandTable</span><span class="p">)</span><span class="o">/</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">redisCommand</span><span class="p">);</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">;</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">numcommands</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">struct</span> <span class="n">redisCommand</span> <span class="o">*</span><span class="n">c</span> <span class="o">=</span> <span class="n">redisCommandTable</span><span class="o">+</span><span class="n">j</span><span class="p">;</span>

        <span class="n">c</span><span class="o">-&gt;</span><span class="n">microseconds</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">calls</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="cm">/* ========================== Redis OP Array API ============================ */</span>

<span class="kt">void</span> <span class="nf">redisOpArrayInit</span><span class="p">(</span><span class="n">redisOpArray</span> <span class="o">*</span><span class="n">oa</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">oa</span><span class="o">-&gt;</span><span class="n">ops</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">oa</span><span class="o">-&gt;</span><span class="n">numops</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">redisOpArrayAppend</span><span class="p">(</span><span class="n">redisOpArray</span> <span class="o">*</span><span class="n">oa</span><span class="p">,</span> <span class="k">struct</span> <span class="n">redisCommand</span> <span class="o">*</span><span class="n">cmd</span><span class="p">,</span> <span class="kt">int</span> <span class="n">dbid</span><span class="p">,</span>
                       <span class="n">robj</span> <span class="o">**</span><span class="n">argv</span><span class="p">,</span> <span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">int</span> <span class="n">target</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">redisOp</span> <span class="o">*</span><span class="n">op</span><span class="p">;</span>

    <span class="n">oa</span><span class="o">-&gt;</span><span class="n">ops</span> <span class="o">=</span> <span class="n">zrealloc</span><span class="p">(</span><span class="n">oa</span><span class="o">-&gt;</span><span class="n">ops</span><span class="p">,</span><span class="k">sizeof</span><span class="p">(</span><span class="n">redisOp</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">oa</span><span class="o">-&gt;</span><span class="n">numops</span><span class="o">+</span><span class="mi">1</span><span class="p">));</span>
    <span class="n">op</span> <span class="o">=</span> <span class="n">oa</span><span class="o">-&gt;</span><span class="n">ops</span><span class="o">+</span><span class="n">oa</span><span class="o">-&gt;</span><span class="n">numops</span><span class="p">;</span>
    <span class="n">op</span><span class="o">-&gt;</span><span class="n">cmd</span> <span class="o">=</span> <span class="n">cmd</span><span class="p">;</span>
    <span class="n">op</span><span class="o">-&gt;</span><span class="n">dbid</span> <span class="o">=</span> <span class="n">dbid</span><span class="p">;</span>
    <span class="n">op</span><span class="o">-&gt;</span><span class="n">argv</span> <span class="o">=</span> <span class="n">argv</span><span class="p">;</span>
    <span class="n">op</span><span class="o">-&gt;</span><span class="n">argc</span> <span class="o">=</span> <span class="n">argc</span><span class="p">;</span>
    <span class="n">op</span><span class="o">-&gt;</span><span class="n">target</span> <span class="o">=</span> <span class="n">target</span><span class="p">;</span>
    <span class="n">oa</span><span class="o">-&gt;</span><span class="n">numops</span><span class="o">++</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">oa</span><span class="o">-&gt;</span><span class="n">numops</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">redisOpArrayFree</span><span class="p">(</span><span class="n">redisOpArray</span> <span class="o">*</span><span class="n">oa</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">while</span><span class="p">(</span><span class="n">oa</span><span class="o">-&gt;</span><span class="n">numops</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">j</span><span class="p">;</span>
        <span class="n">redisOp</span> <span class="o">*</span><span class="n">op</span><span class="p">;</span>

        <span class="n">oa</span><span class="o">-&gt;</span><span class="n">numops</span><span class="o">--</span><span class="p">;</span>
        <span class="n">op</span> <span class="o">=</span> <span class="n">oa</span><span class="o">-&gt;</span><span class="n">ops</span><span class="o">+</span><span class="n">oa</span><span class="o">-&gt;</span><span class="n">numops</span><span class="p">;</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">op</span><span class="o">-&gt;</span><span class="n">argc</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span>
            <span class="n">decrRefCount</span><span class="p">(</span><span class="n">op</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">[</span><span class="n">j</span><span class="p">]);</span>
        <span class="n">zfree</span><span class="p">(</span><span class="n">op</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="n">zfree</span><span class="p">(</span><span class="n">oa</span><span class="o">-&gt;</span><span class="n">ops</span><span class="p">);</span>
<span class="p">}</span>

<span class="cm">/* ====================== Commands lookup and execution ===================== */</span>

<span class="k">struct</span> <span class="n">redisCommand</span> <span class="o">*</span><span class="nf">lookupCommand</span><span class="p">(</span><span class="n">sds</span> <span class="n">name</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">dictFetchValue</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">commands</span><span class="p">,</span> <span class="n">name</span><span class="p">);</span>
<span class="p">}</span>

<span class="k">struct</span> <span class="n">redisCommand</span> <span class="o">*</span><span class="nf">lookupCommandByCString</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">s</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">struct</span> <span class="n">redisCommand</span> <span class="o">*</span><span class="n">cmd</span><span class="p">;</span>
    <span class="n">sds</span> <span class="n">name</span> <span class="o">=</span> <span class="n">sdsnew</span><span class="p">(</span><span class="n">s</span><span class="p">);</span>

    <span class="n">cmd</span> <span class="o">=</span> <span class="n">dictFetchValue</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">commands</span><span class="p">,</span> <span class="n">name</span><span class="p">);</span>
    <span class="n">sdsfree</span><span class="p">(</span><span class="n">name</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">cmd</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* Propagate the specified command (in the context of the specified database id)</span>
<span class="cm"> * to AOF, Slaves and Monitors.</span>
<span class="cm"> *</span>
<span class="cm"> * flags are an xor between:</span>
<span class="cm"> * + REDIS_PROPAGATE_NONE (no propagation of command at all)</span>
<span class="cm"> * + REDIS_PROPAGATE_AOF (propagate into the AOF file if is enabled)</span>
<span class="cm"> * + REDIS_PROPAGATE_REPL (propagate into the replication link)</span>
<span class="cm"> */</span>
<span class="kt">void</span> <span class="nf">propagate</span><span class="p">(</span><span class="k">struct</span> <span class="n">redisCommand</span> <span class="o">*</span><span class="n">cmd</span><span class="p">,</span> <span class="kt">int</span> <span class="n">dbid</span><span class="p">,</span> <span class="n">robj</span> <span class="o">**</span><span class="n">argv</span><span class="p">,</span> <span class="kt">int</span> <span class="n">argc</span><span class="p">,</span>
               <span class="kt">int</span> <span class="n">flags</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_state</span> <span class="o">!=</span> <span class="n">REDIS_AOF_OFF</span> <span class="o">&amp;&amp;</span> <span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_PROPAGATE_AOF</span><span class="p">)</span>
        <span class="n">feedAppendOnlyFile</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span><span class="n">dbid</span><span class="p">,</span><span class="n">argv</span><span class="p">,</span><span class="n">argc</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_PROPAGATE_REPL</span> <span class="o">&amp;&amp;</span> <span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">slaves</span><span class="p">))</span>
        <span class="n">replicationFeedSlaves</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">slaves</span><span class="p">,</span><span class="n">dbid</span><span class="p">,</span><span class="n">argv</span><span class="p">,</span><span class="n">argc</span><span class="p">);</span>
<span class="p">}</span>

<span class="cm">/* Used inside commands to schedule the propagation of additional commands</span>
<span class="cm"> * after the current command is propagated to AOF / Replication. */</span>
<span class="kt">void</span> <span class="nf">alsoPropagate</span><span class="p">(</span><span class="k">struct</span> <span class="n">redisCommand</span> <span class="o">*</span><span class="n">cmd</span><span class="p">,</span> <span class="kt">int</span> <span class="n">dbid</span><span class="p">,</span> <span class="n">robj</span> <span class="o">**</span><span class="n">argv</span><span class="p">,</span> <span class="kt">int</span> <span class="n">argc</span><span class="p">,</span>
                   <span class="kt">int</span> <span class="n">target</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">redisOpArrayAppend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">server</span><span class="p">.</span><span class="n">also_propagate</span><span class="p">,</span><span class="n">cmd</span><span class="p">,</span><span class="n">dbid</span><span class="p">,</span><span class="n">argv</span><span class="p">,</span><span class="n">argc</span><span class="p">,</span><span class="n">target</span><span class="p">);</span>
<span class="p">}</span>

<span class="cm">/* Call() is the core of Redis execution of a command */</span>
<span class="kt">void</span> <span class="nf">call</span><span class="p">(</span><span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">,</span> <span class="kt">int</span> <span class="n">flags</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">long</span> <span class="kt">long</span> <span class="n">dirty</span><span class="p">,</span> <span class="n">start</span> <span class="o">=</span> <span class="n">ustime</span><span class="p">(),</span> <span class="n">duration</span><span class="p">;</span>

    <span class="cm">/* Sent the command to clients in MONITOR mode, only if the commands are</span>
<span class="cm">     * not geneated from reading an AOF. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">monitors</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">server</span><span class="p">.</span><span class="n">loading</span><span class="p">)</span>
        <span class="n">replicationFeedMonitors</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">server</span><span class="p">.</span><span class="n">monitors</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">db</span><span class="o">-&gt;</span><span class="n">id</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argc</span><span class="p">);</span>

    <span class="cm">/* Call the command. */</span>
    <span class="n">redisOpArrayInit</span><span class="p">(</span><span class="o">&amp;</span><span class="n">server</span><span class="p">.</span><span class="n">also_propagate</span><span class="p">);</span>
    <span class="n">dirty</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">dirty</span><span class="p">;</span>
    <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span><span class="p">(</span><span class="n">c</span><span class="p">);</span>
    <span class="n">dirty</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">dirty</span><span class="o">-</span><span class="n">dirty</span><span class="p">;</span>
    <span class="n">duration</span> <span class="o">=</span> <span class="n">ustime</span><span class="p">()</span><span class="o">-</span><span class="n">start</span><span class="p">;</span>

    <span class="cm">/* When EVAL is called loading the AOF we don&#39;t want commands called</span>
<span class="cm">     * from Lua to go into the slowlog or to populate statistics. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">loading</span> <span class="o">&amp;&amp;</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_LUA_CLIENT</span><span class="p">)</span>
        <span class="n">flags</span> <span class="o">&amp;=</span> <span class="o">~</span><span class="p">(</span><span class="n">REDIS_CALL_SLOWLOG</span> <span class="o">|</span> <span class="n">REDIS_CALL_STATS</span><span class="p">);</span>

    <span class="cm">/* Log the command into the Slow log if needed, and populate the</span>
<span class="cm">     * per-command statistics that we show in INFO commandstats. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_CALL_SLOWLOG</span><span class="p">)</span>
        <span class="n">slowlogPushEntryIfNeeded</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argc</span><span class="p">,</span><span class="n">duration</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_CALL_STATS</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">microseconds</span> <span class="o">+=</span> <span class="n">duration</span><span class="p">;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">calls</span><span class="o">++</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Propagate the command into the AOF and replication link */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_CALL_PROPAGATE</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">flags</span> <span class="o">=</span> <span class="n">REDIS_PROPAGATE_NONE</span><span class="p">;</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_CMD_FORCE_REPLICATION</span><span class="p">)</span>
            <span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_PROPAGATE_REPL</span><span class="p">;</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">dirty</span><span class="p">)</span>
            <span class="n">flags</span> <span class="o">|=</span> <span class="p">(</span><span class="n">REDIS_PROPAGATE_REPL</span> <span class="o">|</span> <span class="n">REDIS_PROPAGATE_AOF</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">flags</span> <span class="o">!=</span> <span class="n">REDIS_PROPAGATE_NONE</span><span class="p">)</span>
            <span class="n">propagate</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">db</span><span class="o">-&gt;</span><span class="n">id</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argc</span><span class="p">,</span><span class="n">flags</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="cm">/* Commands such as LPUSH or BRPOPLPUSH may propagate an additional</span>
<span class="cm">     * PUSH command. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">also_propagate</span><span class="p">.</span><span class="n">numops</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">j</span><span class="p">;</span>
        <span class="n">redisOp</span> <span class="o">*</span><span class="n">rop</span><span class="p">;</span>

        <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">also_propagate</span><span class="p">.</span><span class="n">numops</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">rop</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">server</span><span class="p">.</span><span class="n">also_propagate</span><span class="p">.</span><span class="n">ops</span><span class="p">[</span><span class="n">j</span><span class="p">];</span>
            <span class="n">propagate</span><span class="p">(</span><span class="n">rop</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="p">,</span> <span class="n">rop</span><span class="o">-&gt;</span><span class="n">dbid</span><span class="p">,</span> <span class="n">rop</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">,</span> <span class="n">rop</span><span class="o">-&gt;</span><span class="n">argc</span><span class="p">,</span> <span class="n">rop</span><span class="o">-&gt;</span><span class="n">target</span><span class="p">);</span>
        <span class="p">}</span>
        <span class="n">redisOpArrayFree</span><span class="p">(</span><span class="o">&amp;</span><span class="n">server</span><span class="p">.</span><span class="n">also_propagate</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="n">server</span><span class="p">.</span><span class="n">stat_numcommands</span><span class="o">++</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* If this function gets called we already read a whole</span>
<span class="cm"> * command, argments are in the client argv/argc fields.</span>
<span class="cm"> * processCommand() execute the command or prepare the</span>
<span class="cm"> * server for a bulk read from the client.</span>
<span class="cm"> *</span>
<span class="cm"> * If 1 is returned the client is still alive and valid and</span>
<span class="cm"> * and other operations can be performed by the caller. Otherwise</span>
<span class="cm"> * if 0 is returned the client was destroied (i.e. after QUIT). */</span>
<span class="kt">int</span> <span class="nf">processCommand</span><span class="p">(</span><span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="cm">/* The QUIT command is handled separately. Normal command procs will</span>
<span class="cm">     * go through checking for replication and QUIT will cause trouble</span>
<span class="cm">     * when FORCE_REPLICATION is enabled and would be implemented in</span>
<span class="cm">     * a regular command proc. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">,</span><span class="s">&quot;quit&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">shared</span><span class="p">.</span><span class="n">ok</span><span class="p">);</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="n">REDIS_CLOSE_AFTER_REPLY</span><span class="p">;</span>
        <span class="k">return</span> <span class="n">REDIS_ERR</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Now lookup the command and check ASAP about trivial error conditions</span>
<span class="cm">     * such as wrong arity, bad command name and so forth. */</span>
    <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span> <span class="o">=</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">lastcmd</span> <span class="o">=</span> <span class="n">lookupCommand</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">addReplyErrorFormat</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="s">&quot;unknown command &#39;%s&#39;&quot;</span><span class="p">,</span>
            <span class="p">(</span><span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">((</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">arity</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">arity</span> <span class="o">!=</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">argc</span><span class="p">)</span> <span class="o">||</span>
               <span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argc</span> <span class="o">&lt;</span> <span class="o">-</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">arity</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">addReplyErrorFormat</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="s">&quot;wrong number of arguments for &#39;%s&#39; command&quot;</span><span class="p">,</span>
            <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">name</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Check if the user is authenticated */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">requirepass</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">authenticated</span> <span class="o">&amp;&amp;</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">authCommand</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">addReplyError</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="s">&quot;operation not permitted&quot;</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* If cluster is enabled, redirect here */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">cluster_enabled</span> <span class="o">&amp;&amp;</span>
                <span class="o">!</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">getkeys_proc</span> <span class="o">==</span> <span class="nb">NULL</span> <span class="o">&amp;&amp;</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">firstkey</span> <span class="o">==</span> <span class="mi">0</span><span class="p">))</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">hashslot</span><span class="p">;</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">cluster</span><span class="p">.</span><span class="n">state</span> <span class="o">!=</span> <span class="n">REDIS_CLUSTER_OK</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">addReplyError</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="s">&quot;The cluster is down. Check with CLUSTER INFO for more information&quot;</span><span class="p">);</span>
            <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="kt">int</span> <span class="n">ask</span><span class="p">;</span>
            <span class="n">clusterNode</span> <span class="o">*</span><span class="n">n</span> <span class="o">=</span> <span class="n">getNodeByQuery</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argc</span><span class="p">,</span><span class="o">&amp;</span><span class="n">hashslot</span><span class="p">,</span><span class="o">&amp;</span><span class="n">ask</span><span class="p">);</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">addReplyError</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="s">&quot;Multi keys request invalid in cluster&quot;</span><span class="p">);</span>
                <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
            <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">!=</span> <span class="n">server</span><span class="p">.</span><span class="n">cluster</span><span class="p">.</span><span class="n">myself</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">addReplySds</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">sdscatprintf</span><span class="p">(</span><span class="n">sdsempty</span><span class="p">(),</span>
                    <span class="s">&quot;-%s %d %s:%d</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">ask</span> <span class="o">?</span> <span class="s">&quot;ASK&quot;</span> <span class="o">:</span> <span class="s">&quot;MOVED&quot;</span><span class="p">,</span>
                    <span class="n">hashslot</span><span class="p">,</span><span class="n">n</span><span class="o">-&gt;</span><span class="n">ip</span><span class="p">,</span><span class="n">n</span><span class="o">-&gt;</span><span class="n">port</span><span class="p">));</span>
                <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="cm">/* Handle the maxmemory directive.</span>
<span class="cm">     *</span>
<span class="cm">     * First we try to free some memory if possible (if there are volatile</span>
<span class="cm">     * keys in the dataset). If there are not the only thing we can do</span>
<span class="cm">     * is returning an error. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">maxmemory</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">retval</span> <span class="o">=</span> <span class="n">freeMemoryIfNeeded</span><span class="p">();</span>
        <span class="k">if</span> <span class="p">((</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_CMD_DENYOOM</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="n">retval</span> <span class="o">==</span> <span class="n">REDIS_ERR</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">shared</span><span class="p">.</span><span class="n">oomerr</span><span class="p">);</span>
            <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="cm">/* Don&#39;t accept write commands if there are problems persisting on disk. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">stop_writes_on_bgsave_err</span> <span class="o">&amp;&amp;</span>
        <span class="n">server</span><span class="p">.</span><span class="n">saveparamslen</span> <span class="o">&gt;</span> <span class="mi">0</span>
        <span class="o">&amp;&amp;</span> <span class="n">server</span><span class="p">.</span><span class="n">lastbgsave_status</span> <span class="o">==</span> <span class="n">REDIS_ERR</span> <span class="o">&amp;&amp;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_CMD_WRITE</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">shared</span><span class="p">.</span><span class="n">bgsaveerr</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Don&#39;t accept wirte commands if this is a read only slave. But</span>
<span class="cm">     * accept write commands if this is our master. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">masterhost</span> <span class="o">&amp;&amp;</span> <span class="n">server</span><span class="p">.</span><span class="n">repl_slave_ro</span> <span class="o">&amp;&amp;</span>
        <span class="o">!</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_MASTER</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_CMD_WRITE</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">shared</span><span class="p">.</span><span class="n">roslaveerr</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Only allow SUBSCRIBE and UNSUBSCRIBE in the context of Pub/Sub */</span>
    <span class="k">if</span> <span class="p">((</span><span class="n">dictSize</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">pubsub_channels</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">listLength</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">pubsub_patterns</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
        <span class="o">&amp;&amp;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">subscribeCommand</span> <span class="o">&amp;&amp;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">unsubscribeCommand</span> <span class="o">&amp;&amp;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">psubscribeCommand</span> <span class="o">&amp;&amp;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">punsubscribeCommand</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">addReplyError</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="s">&quot;only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context&quot;</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Only allow INFO and SLAVEOF when slave-serve-stale-data is no and</span>
<span class="cm">     * we are a slave with a broken link with master. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">masterhost</span> <span class="o">&amp;&amp;</span> <span class="n">server</span><span class="p">.</span><span class="n">repl_state</span> <span class="o">!=</span> <span class="n">REDIS_REPL_CONNECTED</span> <span class="o">&amp;&amp;</span>
        <span class="n">server</span><span class="p">.</span><span class="n">repl_serve_stale_data</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">infoCommand</span> <span class="o">&amp;&amp;</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">slaveofCommand</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">shared</span><span class="p">.</span><span class="n">masterdownerr</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Loading DB? Return an error if the command is not INFO */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">loading</span> <span class="o">&amp;&amp;</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">infoCommand</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">shared</span><span class="p">.</span><span class="n">loadingerr</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Lua script too slow? Only allow SHUTDOWN NOSAVE and SCRIPT KILL. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">lua_timedout</span> <span class="o">&amp;&amp;</span>
        <span class="o">!</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">==</span> <span class="n">shutdownCommand</span> <span class="o">&amp;&amp;</span>
          <span class="n">c</span><span class="o">-&gt;</span><span class="n">argc</span> <span class="o">==</span> <span class="mi">2</span> <span class="o">&amp;&amp;</span>
          <span class="n">tolower</span><span class="p">(((</span><span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="sc">&#39;n&#39;</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
        <span class="o">!</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">==</span> <span class="n">scriptCommand</span> <span class="o">&amp;&amp;</span>
          <span class="n">c</span><span class="o">-&gt;</span><span class="n">argc</span> <span class="o">==</span> <span class="mi">2</span> <span class="o">&amp;&amp;</span>
          <span class="n">tolower</span><span class="p">(((</span><span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="sc">&#39;k&#39;</span><span class="p">))</span>
    <span class="p">{</span>
        <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">shared</span><span class="p">.</span><span class="n">slowscripterr</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/* Exec the command */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_MULTI</span> <span class="o">&amp;&amp;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">execCommand</span> <span class="o">&amp;&amp;</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">discardCommand</span> <span class="o">&amp;&amp;</span>
        <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">multiCommand</span> <span class="o">&amp;&amp;</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">cmd</span><span class="o">-&gt;</span><span class="n">proc</span> <span class="o">!=</span> <span class="n">watchCommand</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">queueMultiCommand</span><span class="p">(</span><span class="n">c</span><span class="p">);</span>
        <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">shared</span><span class="p">.</span><span class="n">queued</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="n">call</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">REDIS_CALL_FULL</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/*================================== Shutdown =============================== */</span>

<span class="kt">int</span> <span class="nf">prepareForShutdown</span><span class="p">(</span><span class="kt">int</span> <span class="n">flags</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">save</span> <span class="o">=</span> <span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_SHUTDOWN_SAVE</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">nosave</span> <span class="o">=</span> <span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_SHUTDOWN_NOSAVE</span><span class="p">;</span>

    <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;User requested shutdown...&quot;</span><span class="p">);</span>
    <span class="cm">/* Kill the saving child if there is a background saving in progress.</span>
<span class="cm">       We want to avoid race conditions, for instance our saving child may</span>
<span class="cm">       overwrite the synchronous saving did by SHUTDOWN. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;There is a child saving an .rdb. Killing it!&quot;</span><span class="p">);</span>
        <span class="n">kill</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span><span class="p">,</span><span class="n">SIGKILL</span><span class="p">);</span>
        <span class="n">rdbRemoveTempFile</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_state</span> <span class="o">!=</span> <span class="n">REDIS_AOF_OFF</span><span class="p">)</span> <span class="p">{</span>
        <span class="cm">/* Kill the AOF saving child as the AOF we already have may be longer</span>
<span class="cm">         * but contains the full dataset anyway. */</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_child_pid</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span>
                <span class="s">&quot;There is a child rewriting the AOF. Killing it!&quot;</span><span class="p">);</span>
            <span class="n">kill</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_child_pid</span><span class="p">,</span><span class="n">SIGKILL</span><span class="p">);</span>
        <span class="p">}</span>
        <span class="cm">/* Append only file: fsync() the AOF and exit */</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;Calling fsync() on the AOF file.&quot;</span><span class="p">);</span>
        <span class="n">aof_fsync</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_fd</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">((</span><span class="n">server</span><span class="p">.</span><span class="n">saveparamslen</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">nosave</span><span class="p">)</span> <span class="o">||</span> <span class="n">save</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;Saving the final RDB snapshot before exiting.&quot;</span><span class="p">);</span>
        <span class="cm">/* Snapshotting. Perform a SYNC SAVE and exit */</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">rdbSave</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_filename</span><span class="p">)</span> <span class="o">!=</span> <span class="n">REDIS_OK</span><span class="p">)</span> <span class="p">{</span>
            <span class="cm">/* Ooops.. error saving! The best we can do is to continue</span>
<span class="cm">             * operating. Note that if there was a background saving process,</span>
<span class="cm">             * in the next cron() Redis will be notified that the background</span>
<span class="cm">             * saving aborted, handling special stuff like slaves pending for</span>
<span class="cm">             * synchronization... */</span>
            <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;Error trying to save the DB, can&#39;t exit.&quot;</span><span class="p">);</span>
            <span class="k">return</span> <span class="n">REDIS_ERR</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">daemonize</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;Removing the pid file.&quot;</span><span class="p">);</span>
        <span class="n">unlink</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">pidfile</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="cm">/* Close the listening sockets. Apparently this allows faster restarts. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">ipfd</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="n">close</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">ipfd</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">sofd</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="n">close</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">sofd</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">unixsocket</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;Removing the unix socket file.&quot;</span><span class="p">);</span>
        <span class="n">unlink</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">unixsocket</span><span class="p">);</span> <span class="cm">/* don&#39;t care if this fails */</span>
    <span class="p">}</span>

    <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;Redis is now ready to exit, bye bye...&quot;</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/*================================== Commands =============================== */</span>

<span class="cm">/* Return zero if strings are the same, non-zero if they are not.</span>
<span class="cm"> * The comparison is performed in a way that prevents an attacker to obtain</span>
<span class="cm"> * information about the nature of the strings just monitoring the execution</span>
<span class="cm"> * time of the function.</span>
<span class="cm"> *</span>
<span class="cm"> * Note that limiting the comparison length to strings up to 512 bytes we</span>
<span class="cm"> * can avoid leaking any information about the password length and any</span>
<span class="cm"> * possible branch misprediction related leak.</span>
<span class="cm"> */</span>
<span class="kt">int</span> <span class="nf">time_independent_strcmp</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">a</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">b</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">char</span> <span class="n">bufa</span><span class="p">[</span><span class="n">REDIS_AUTHPASS_MAX_LEN</span><span class="p">],</span> <span class="n">bufb</span><span class="p">[</span><span class="n">REDIS_AUTHPASS_MAX_LEN</span><span class="p">];</span>
    <span class="cm">/* The above two strlen perform len(a) + len(b) operations where either</span>
<span class="cm">     * a or b are fixed (our password) length, and the difference is only</span>
<span class="cm">     * relative to the length of the user provided string, so no information</span>
<span class="cm">     * leak is possible in the following two lines of code. */</span>
    <span class="kt">int</span> <span class="n">alen</span> <span class="o">=</span> <span class="n">strlen</span><span class="p">(</span><span class="n">a</span><span class="p">);</span>
    <span class="kt">int</span> <span class="n">blen</span> <span class="o">=</span> <span class="n">strlen</span><span class="p">(</span><span class="n">b</span><span class="p">);</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">diff</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="cm">/* We can&#39;t compare strings longer than our static buffers.</span>
<span class="cm">     * Note that this will never pass the first test in practical circumstances</span>
<span class="cm">     * so there is no info leak. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">alen</span> <span class="o">&gt;</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">bufa</span><span class="p">)</span> <span class="o">||</span> <span class="n">blen</span> <span class="o">&gt;</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">bufb</span><span class="p">))</span> <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>

    <span class="n">memset</span><span class="p">(</span><span class="n">bufa</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="k">sizeof</span><span class="p">(</span><span class="n">bufa</span><span class="p">));</span>        <span class="cm">/* Constant time. */</span>
    <span class="n">memset</span><span class="p">(</span><span class="n">bufb</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="k">sizeof</span><span class="p">(</span><span class="n">bufb</span><span class="p">));</span>        <span class="cm">/* Constant time. */</span>
    <span class="cm">/* Again the time of the following two copies is proportional to</span>
<span class="cm">     * len(a) + len(b) so no info is leaked. */</span>
    <span class="n">memcpy</span><span class="p">(</span><span class="n">bufa</span><span class="p">,</span><span class="n">a</span><span class="p">,</span><span class="n">alen</span><span class="p">);</span>
    <span class="n">memcpy</span><span class="p">(</span><span class="n">bufb</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">blen</span><span class="p">);</span>

    <span class="cm">/* Always compare all the chars in the two buffers without</span>
<span class="cm">     * conditional expressions. */</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">bufa</span><span class="p">);</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">diff</span> <span class="o">|=</span> <span class="p">(</span><span class="n">bufa</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">^</span> <span class="n">bufb</span><span class="p">[</span><span class="n">j</span><span class="p">]);</span>
    <span class="p">}</span>
    <span class="cm">/* Length must be equal as well. */</span>
    <span class="n">diff</span> <span class="o">|=</span> <span class="n">alen</span> <span class="o">^</span> <span class="n">blen</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">diff</span><span class="p">;</span> <span class="cm">/* If zero strings are the same. */</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">authCommand</span><span class="p">(</span><span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">server</span><span class="p">.</span><span class="n">requirepass</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">addReplyError</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="s">&quot;Client sent AUTH, but no password is set&quot;</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">time_independent_strcmp</span><span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">-&gt;</span><span class="n">ptr</span><span class="p">,</span> <span class="n">server</span><span class="p">.</span><span class="n">requirepass</span><span class="p">))</span> <span class="p">{</span>
      <span class="n">c</span><span class="o">-&gt;</span><span class="n">authenticated</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
      <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">shared</span><span class="p">.</span><span class="n">ok</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
      <span class="n">c</span><span class="o">-&gt;</span><span class="n">authenticated</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
      <span class="n">addReplyError</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="s">&quot;invalid password&quot;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">pingCommand</span><span class="p">(</span><span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">shared</span><span class="p">.</span><span class="n">pong</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">echoCommand</span><span class="p">(</span><span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">addReplyBulk</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">timeCommand</span><span class="p">(</span><span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">struct</span> <span class="n">timeval</span> <span class="n">tv</span><span class="p">;</span>

    <span class="cm">/* gettimeofday() can only fail if &amp;tv is a bad addresss so we</span>
<span class="cm">     * don&#39;t check for errors. */</span>
    <span class="n">gettimeofday</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tv</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
    <span class="n">addReplyMultiBulkLen</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
    <span class="n">addReplyBulkLongLong</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">tv</span><span class="p">.</span><span class="n">tv_sec</span><span class="p">);</span>
    <span class="n">addReplyBulkLongLong</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">tv</span><span class="p">.</span><span class="n">tv_usec</span><span class="p">);</span>
<span class="p">}</span>

<span class="cm">/* Convert an amount of bytes into a human readable string in the form</span>
<span class="cm"> * of 100B, 2G, 100M, 4K, and so forth. */</span>
<span class="kt">void</span> <span class="nf">bytesToHuman</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">s</span><span class="p">,</span> <span class="kt">unsigned</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">double</span> <span class="n">d</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1024</span><span class="p">)</span> <span class="p">{</span>
        <span class="cm">/* Bytes */</span>
        <span class="n">sprintf</span><span class="p">(</span><span class="n">s</span><span class="p">,</span><span class="s">&quot;%lluB&quot;</span><span class="p">,</span><span class="n">n</span><span class="p">);</span>
        <span class="k">return</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="kt">double</span><span class="p">)</span><span class="n">n</span><span class="o">/</span><span class="p">(</span><span class="mi">1024</span><span class="p">);</span>
        <span class="n">sprintf</span><span class="p">(</span><span class="n">s</span><span class="p">,</span><span class="s">&quot;%.2fK&quot;</span><span class="p">,</span><span class="n">d</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">1024LL</span><span class="o">*</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="kt">double</span><span class="p">)</span><span class="n">n</span><span class="o">/</span><span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">);</span>
        <span class="n">sprintf</span><span class="p">(</span><span class="n">s</span><span class="p">,</span><span class="s">&quot;%.2fM&quot;</span><span class="p">,</span><span class="n">d</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">1024LL</span><span class="o">*</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="kt">double</span><span class="p">)</span><span class="n">n</span><span class="o">/</span><span class="p">(</span><span class="mi">1024LL</span><span class="o">*</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">);</span>
        <span class="n">sprintf</span><span class="p">(</span><span class="n">s</span><span class="p">,</span><span class="s">&quot;%.2fG&quot;</span><span class="p">,</span><span class="n">d</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="cm">/* Create the string returned by the INFO command. This is decoupled</span>
<span class="cm"> * by the INFO command itself as we need to report the same information</span>
<span class="cm"> * on memory corruption problems. */</span>
<span class="n">sds</span> <span class="nf">genRedisInfoString</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">section</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">sds</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdsempty</span><span class="p">();</span>
    <span class="kt">time_t</span> <span class="n">uptime</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">unixtime</span><span class="o">-</span><span class="n">server</span><span class="p">.</span><span class="n">stat_starttime</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">j</span><span class="p">,</span> <span class="n">numcommands</span><span class="p">;</span>
    <span class="k">struct</span> <span class="n">rusage</span> <span class="n">self_ru</span><span class="p">,</span> <span class="n">c_ru</span><span class="p">;</span>
    <span class="kt">unsigned</span> <span class="kt">long</span> <span class="n">lol</span><span class="p">,</span> <span class="n">bib</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">allsections</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">defsections</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">sections</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">section</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">allsections</span> <span class="o">=</span> <span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;all&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
        <span class="n">defsections</span> <span class="o">=</span> <span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;default&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">getrusage</span><span class="p">(</span><span class="n">RUSAGE_SELF</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">self_ru</span><span class="p">);</span>
    <span class="n">getrusage</span><span class="p">(</span><span class="n">RUSAGE_CHILDREN</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">c_ru</span><span class="p">);</span>
    <span class="n">getClientsMaxBuffers</span><span class="p">(</span><span class="o">&amp;</span><span class="n">lol</span><span class="p">,</span><span class="o">&amp;</span><span class="n">bib</span><span class="p">);</span>

    <span class="cm">/* Server */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">allsections</span> <span class="o">||</span> <span class="n">defsections</span> <span class="o">||</span> <span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;server&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">struct</span> <span class="n">utsname</span> <span class="n">name</span><span class="p">;</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">sections</span><span class="o">++</span><span class="p">)</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">uname</span><span class="p">(</span><span class="o">&amp;</span><span class="n">name</span><span class="p">);</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
            <span class="s">&quot;# Server</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;redis_version:%s</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;redis_git_sha1:%s</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;redis_git_dirty:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;os:%s %s %s</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;arch_bits:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;multiplexing_api:%s</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;gcc_version:%d.%d.%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;process_id:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;run_id:%s</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;tcp_port:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;uptime_in_seconds:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;uptime_in_days:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;lru_clock:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
            <span class="n">REDIS_VERSION</span><span class="p">,</span>
            <span class="n">redisGitSHA1</span><span class="p">(),</span>
            <span class="n">strtol</span><span class="p">(</span><span class="n">redisGitDirty</span><span class="p">(),</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">10</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span>
            <span class="n">name</span><span class="p">.</span><span class="n">sysname</span><span class="p">,</span> <span class="n">name</span><span class="p">.</span><span class="n">release</span><span class="p">,</span> <span class="n">name</span><span class="p">.</span><span class="n">machine</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">arch_bits</span><span class="p">,</span>
            <span class="n">aeGetApiName</span><span class="p">(),</span>
<span class="cp">#ifdef __GNUC__</span>
            <span class="n">__GNUC__</span><span class="p">,</span><span class="n">__GNUC_MINOR__</span><span class="p">,</span><span class="n">__GNUC_PATCHLEVEL__</span><span class="p">,</span>
<span class="cp">#else</span>
            <span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span>
<span class="cp">#endif</span>
            <span class="p">(</span><span class="kt">long</span><span class="p">)</span> <span class="n">getpid</span><span class="p">(),</span>
            <span class="n">server</span><span class="p">.</span><span class="n">runid</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">port</span><span class="p">,</span>
            <span class="n">uptime</span><span class="p">,</span>
            <span class="n">uptime</span><span class="o">/</span><span class="p">(</span><span class="mi">3600</span><span class="o">*</span><span class="mi">24</span><span class="p">),</span>
            <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">long</span><span class="p">)</span> <span class="n">server</span><span class="p">.</span><span class="n">lruclock</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="cm">/* Clients */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">allsections</span> <span class="o">||</span> <span class="n">defsections</span> <span class="o">||</span> <span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;clients&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">sections</span><span class="o">++</span><span class="p">)</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
            <span class="s">&quot;# Clients</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;connected_clients:%lu</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;client_longest_output_list:%lu</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;client_biggest_input_buf:%lu</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;blocked_clients:%d</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
            <span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">clients</span><span class="p">)</span><span class="o">-</span><span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">slaves</span><span class="p">),</span>
            <span class="n">lol</span><span class="p">,</span> <span class="n">bib</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">bpop_blocked_clients</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="cm">/* Memory */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">allsections</span> <span class="o">||</span> <span class="n">defsections</span> <span class="o">||</span> <span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;memory&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="kt">char</span> <span class="n">hmem</span><span class="p">[</span><span class="mi">64</span><span class="p">];</span>
        <span class="kt">char</span> <span class="n">peak_hmem</span><span class="p">[</span><span class="mi">64</span><span class="p">];</span>

        <span class="n">bytesToHuman</span><span class="p">(</span><span class="n">hmem</span><span class="p">,</span><span class="n">zmalloc_used_memory</span><span class="p">());</span>
        <span class="n">bytesToHuman</span><span class="p">(</span><span class="n">peak_hmem</span><span class="p">,</span><span class="n">server</span><span class="p">.</span><span class="n">stat_peak_memory</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">sections</span><span class="o">++</span><span class="p">)</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
            <span class="s">&quot;# Memory</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;used_memory:%zu</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;used_memory_human:%s</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;used_memory_rss:%zu</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;used_memory_peak:%zu</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;used_memory_peak_human:%s</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;used_memory_lua:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;mem_fragmentation_ratio:%.2f</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;mem_allocator:%s</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
            <span class="n">zmalloc_used_memory</span><span class="p">(),</span>
            <span class="n">hmem</span><span class="p">,</span>
            <span class="n">zmalloc_get_rss</span><span class="p">(),</span>
            <span class="n">server</span><span class="p">.</span><span class="n">stat_peak_memory</span><span class="p">,</span>
            <span class="n">peak_hmem</span><span class="p">,</span>
            <span class="p">((</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)</span><span class="n">lua_gc</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">lua</span><span class="p">,</span><span class="n">LUA_GCCOUNT</span><span class="p">,</span><span class="mi">0</span><span class="p">))</span><span class="o">*</span><span class="mi">1024LL</span><span class="p">,</span>
            <span class="n">zmalloc_get_fragmentation_ratio</span><span class="p">(),</span>
            <span class="n">ZMALLOC_LIB</span>
            <span class="p">);</span>
    <span class="p">}</span>

    <span class="cm">/* Persistence */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">allsections</span> <span class="o">||</span> <span class="n">defsections</span> <span class="o">||</span> <span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;persistence&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">sections</span><span class="o">++</span><span class="p">)</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
            <span class="s">&quot;# Persistence</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;loading:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;rdb_changes_since_last_save:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;rdb_bgsave_in_progress:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;rdb_last_save_time:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;rdb_last_bgsave_status:%s</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;rdb_last_bgsave_time_sec:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;rdb_current_bgsave_time_sec:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;aof_enabled:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;aof_rewrite_in_progress:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;aof_rewrite_scheduled:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;aof_last_rewrite_time_sec:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;aof_current_rewrite_time_sec:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">loading</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">dirty</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">lastsave</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">lastbgsave_status</span> <span class="o">==</span> <span class="n">REDIS_OK</span> <span class="o">?</span> <span class="s">&quot;ok&quot;</span> <span class="o">:</span> <span class="s">&quot;err&quot;</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">rdb_save_time_last</span><span class="p">,</span>
            <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_child_pid</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">?</span>
                <span class="o">-</span><span class="mi">1</span> <span class="o">:</span> <span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span><span class="o">-</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_save_time_start</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">aof_state</span> <span class="o">!=</span> <span class="n">REDIS_AOF_OFF</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">aof_child_pid</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_scheduled</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_time_last</span><span class="p">,</span>
            <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_child_pid</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">?</span>
                <span class="o">-</span><span class="mi">1</span> <span class="o">:</span> <span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span><span class="o">-</span><span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_time_start</span><span class="p">);</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_state</span> <span class="o">!=</span> <span class="n">REDIS_AOF_OFF</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
                <span class="s">&quot;aof_current_size:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;aof_base_size:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;aof_pending_rewrite:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;aof_buffer_length:%zu</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;aof_rewrite_buffer_length:%zu</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;aof_pending_bio_fsync:%llu</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;aof_delayed_fsync:%lu</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
                <span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)</span> <span class="n">server</span><span class="p">.</span><span class="n">aof_current_size</span><span class="p">,</span>
                <span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)</span> <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_base_size</span><span class="p">,</span>
                <span class="n">server</span><span class="p">.</span><span class="n">aof_rewrite_scheduled</span><span class="p">,</span>
                <span class="n">sdslen</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_buf</span><span class="p">),</span>
                <span class="n">aofRewriteBufferSize</span><span class="p">(),</span>
                <span class="n">bioPendingJobsOfType</span><span class="p">(</span><span class="n">REDIS_BIO_AOF_FSYNC</span><span class="p">),</span>
                <span class="n">server</span><span class="p">.</span><span class="n">aof_delayed_fsync</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">loading</span><span class="p">)</span> <span class="p">{</span>
            <span class="kt">double</span> <span class="n">perc</span><span class="p">;</span>
            <span class="kt">time_t</span> <span class="n">eta</span><span class="p">,</span> <span class="n">elapsed</span><span class="p">;</span>
            <span class="kt">off_t</span> <span class="n">remaining_bytes</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">loading_total_bytes</span><span class="o">-</span>
                                    <span class="n">server</span><span class="p">.</span><span class="n">loading_loaded_bytes</span><span class="p">;</span>

            <span class="n">perc</span> <span class="o">=</span> <span class="p">((</span><span class="kt">double</span><span class="p">)</span><span class="n">server</span><span class="p">.</span><span class="n">loading_loaded_bytes</span> <span class="o">/</span>
                   <span class="n">server</span><span class="p">.</span><span class="n">loading_total_bytes</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">;</span>

            <span class="n">elapsed</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">unixtime</span><span class="o">-</span><span class="n">server</span><span class="p">.</span><span class="n">loading_start_time</span><span class="p">;</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">elapsed</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">eta</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="cm">/* A fake 1 second figure if we don&#39;t have</span>
<span class="cm">                            enough info */</span>
            <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                <span class="n">eta</span> <span class="o">=</span> <span class="p">(</span><span class="n">elapsed</span><span class="o">*</span><span class="n">remaining_bytes</span><span class="p">)</span><span class="o">/</span><span class="n">server</span><span class="p">.</span><span class="n">loading_loaded_bytes</span><span class="p">;</span>
            <span class="p">}</span>

            <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
                <span class="s">&quot;loading_start_time:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;loading_total_bytes:%llu</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;loading_loaded_bytes:%llu</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;loading_loaded_perc:%.2f</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;loading_eta_seconds:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="p">,(</span><span class="kt">unsigned</span> <span class="kt">long</span><span class="p">)</span> <span class="n">server</span><span class="p">.</span><span class="n">loading_start_time</span><span class="p">,</span>
                <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">long</span> <span class="kt">long</span><span class="p">)</span> <span class="n">server</span><span class="p">.</span><span class="n">loading_total_bytes</span><span class="p">,</span>
                <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">long</span> <span class="kt">long</span><span class="p">)</span> <span class="n">server</span><span class="p">.</span><span class="n">loading_loaded_bytes</span><span class="p">,</span>
                <span class="n">perc</span><span class="p">,</span>
                <span class="n">eta</span>
            <span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="cm">/* Stats */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">allsections</span> <span class="o">||</span> <span class="n">defsections</span> <span class="o">||</span> <span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;stats&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">sections</span><span class="o">++</span><span class="p">)</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
            <span class="s">&quot;# Stats</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;total_connections_received:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;total_commands_processed:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;instantaneous_ops_per_sec:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;rejected_connections:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;expired_keys:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;evicted_keys:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;keyspace_hits:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;keyspace_misses:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;pubsub_channels:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;pubsub_patterns:%lu</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;latest_fork_usec:%lld</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">stat_numconnections</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">stat_numcommands</span><span class="p">,</span>
            <span class="n">getOperationsPerSecond</span><span class="p">(),</span>
            <span class="n">server</span><span class="p">.</span><span class="n">stat_rejected_conn</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">stat_expiredkeys</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">stat_evictedkeys</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">stat_keyspace_hits</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">stat_keyspace_misses</span><span class="p">,</span>
            <span class="n">dictSize</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">pubsub_channels</span><span class="p">),</span>
            <span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">pubsub_patterns</span><span class="p">),</span>
            <span class="n">server</span><span class="p">.</span><span class="n">stat_fork_time</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="cm">/* Replication */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">allsections</span> <span class="o">||</span> <span class="n">defsections</span> <span class="o">||</span> <span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;replication&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">sections</span><span class="o">++</span><span class="p">)</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
            <span class="s">&quot;# Replication</span><span class="se">\r\n</span><span class="s">&quot;</span>
            <span class="s">&quot;role:%s</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
            <span class="n">server</span><span class="p">.</span><span class="n">masterhost</span> <span class="o">==</span> <span class="nb">NULL</span> <span class="o">?</span> <span class="s">&quot;master&quot;</span> <span class="o">:</span> <span class="s">&quot;slave&quot;</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">masterhost</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
                <span class="s">&quot;master_host:%s</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;master_port:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;master_link_status:%s</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;master_last_io_seconds_ago:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="s">&quot;master_sync_in_progress:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
                <span class="p">,</span><span class="n">server</span><span class="p">.</span><span class="n">masterhost</span><span class="p">,</span>
                <span class="n">server</span><span class="p">.</span><span class="n">masterport</span><span class="p">,</span>
                <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">repl_state</span> <span class="o">==</span> <span class="n">REDIS_REPL_CONNECTED</span><span class="p">)</span> <span class="o">?</span>
                    <span class="s">&quot;up&quot;</span> <span class="o">:</span> <span class="s">&quot;down&quot;</span><span class="p">,</span>
                <span class="n">server</span><span class="p">.</span><span class="n">master</span> <span class="o">?</span>
                <span class="p">((</span><span class="kt">int</span><span class="p">)(</span><span class="n">server</span><span class="p">.</span><span class="n">unixtime</span><span class="o">-</span><span class="n">server</span><span class="p">.</span><span class="n">master</span><span class="o">-&gt;</span><span class="n">lastinteraction</span><span class="p">))</span> <span class="o">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
                <span class="n">server</span><span class="p">.</span><span class="n">repl_state</span> <span class="o">==</span> <span class="n">REDIS_REPL_TRANSFER</span>
            <span class="p">);</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">repl_state</span> <span class="o">==</span> <span class="n">REDIS_REPL_TRANSFER</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
                    <span class="s">&quot;master_sync_left_bytes:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span>
                    <span class="s">&quot;master_sync_last_io_seconds_ago:%d</span><span class="se">\r\n</span><span class="s">&quot;</span>
                    <span class="p">,(</span><span class="kt">long</span><span class="p">)</span><span class="n">server</span><span class="p">.</span><span class="n">repl_transfer_left</span><span class="p">,</span>
                    <span class="p">(</span><span class="kt">int</span><span class="p">)(</span><span class="n">server</span><span class="p">.</span><span class="n">unixtime</span><span class="o">-</span><span class="n">server</span><span class="p">.</span><span class="n">repl_transfer_lastio</span><span class="p">)</span>
                <span class="p">);</span>
            <span class="p">}</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">repl_state</span> <span class="o">!=</span> <span class="n">REDIS_REPL_CONNECTED</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
                    <span class="s">&quot;master_link_down_since_seconds:%ld</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
                    <span class="p">(</span><span class="kt">long</span><span class="p">)</span><span class="n">server</span><span class="p">.</span><span class="n">unixtime</span><span class="o">-</span><span class="n">server</span><span class="p">.</span><span class="n">repl_down_since</span><span class="p">);</span>
            <span class="p">}</span>
        <span class="p">}</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
            <span class="s">&quot;connected_slaves:%lu</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
            <span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">slaves</span><span class="p">));</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">slaves</span><span class="p">))</span> <span class="p">{</span>
            <span class="kt">int</span> <span class="n">slaveid</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
            <span class="n">listNode</span> <span class="o">*</span><span class="n">ln</span><span class="p">;</span>
            <span class="n">listIter</span> <span class="n">li</span><span class="p">;</span>

            <span class="n">listRewind</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">slaves</span><span class="p">,</span><span class="o">&amp;</span><span class="n">li</span><span class="p">);</span>
            <span class="k">while</span><span class="p">((</span><span class="n">ln</span> <span class="o">=</span> <span class="n">listNext</span><span class="p">(</span><span class="o">&amp;</span><span class="n">li</span><span class="p">)))</span> <span class="p">{</span>
                <span class="n">redisClient</span> <span class="o">*</span><span class="n">slave</span> <span class="o">=</span> <span class="n">listNodeValue</span><span class="p">(</span><span class="n">ln</span><span class="p">);</span>
                <span class="kt">char</span> <span class="o">*</span><span class="n">state</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
                <span class="kt">char</span> <span class="n">ip</span><span class="p">[</span><span class="mi">32</span><span class="p">];</span>
                <span class="kt">int</span> <span class="n">port</span><span class="p">;</span>

                <span class="k">if</span> <span class="p">(</span><span class="n">anetPeerToString</span><span class="p">(</span><span class="n">slave</span><span class="o">-&gt;</span><span class="n">fd</span><span class="p">,</span><span class="n">ip</span><span class="p">,</span><span class="o">&amp;</span><span class="n">port</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
                <span class="k">switch</span><span class="p">(</span><span class="n">slave</span><span class="o">-&gt;</span><span class="n">replstate</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">case</span> <span class="n">REDIS_REPL_WAIT_BGSAVE_START</span>:
                <span class="k">case</span> <span class="n">REDIS_REPL_WAIT_BGSAVE_END</span>:
                    <span class="n">state</span> <span class="o">=</span> <span class="s">&quot;wait_bgsave&quot;</span><span class="p">;</span>
                    <span class="k">break</span><span class="p">;</span>
                <span class="k">case</span> <span class="n">REDIS_REPL_SEND_BULK</span>:
                    <span class="n">state</span> <span class="o">=</span> <span class="s">&quot;send_bulk&quot;</span><span class="p">;</span>
                    <span class="k">break</span><span class="p">;</span>
                <span class="k">case</span> <span class="n">REDIS_REPL_ONLINE</span>:
                    <span class="n">state</span> <span class="o">=</span> <span class="s">&quot;online&quot;</span><span class="p">;</span>
                    <span class="k">break</span><span class="p">;</span>
                <span class="p">}</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">state</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
                <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;slave%d:%s,%d,%s</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
                    <span class="n">slaveid</span><span class="p">,</span><span class="n">ip</span><span class="p">,</span><span class="n">slave</span><span class="o">-&gt;</span><span class="n">slave_listening_port</span><span class="p">,</span><span class="n">state</span><span class="p">);</span>
                <span class="n">slaveid</span><span class="o">++</span><span class="p">;</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="cm">/* CPU */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">allsections</span> <span class="o">||</span> <span class="n">defsections</span> <span class="o">||</span> <span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;cpu&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">sections</span><span class="o">++</span><span class="p">)</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
        <span class="s">&quot;# CPU</span><span class="se">\r\n</span><span class="s">&quot;</span>
        <span class="s">&quot;used_cpu_sys:%.2f</span><span class="se">\r\n</span><span class="s">&quot;</span>
        <span class="s">&quot;used_cpu_user:%.2f</span><span class="se">\r\n</span><span class="s">&quot;</span>
        <span class="s">&quot;used_cpu_sys_children:%.2f</span><span class="se">\r\n</span><span class="s">&quot;</span>
        <span class="s">&quot;used_cpu_user_children:%.2f</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
        <span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="n">self_ru</span><span class="p">.</span><span class="n">ru_stime</span><span class="p">.</span><span class="n">tv_sec</span><span class="o">+</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="n">self_ru</span><span class="p">.</span><span class="n">ru_stime</span><span class="p">.</span><span class="n">tv_usec</span><span class="o">/</span><span class="mi">1000000</span><span class="p">,</span>
        <span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="n">self_ru</span><span class="p">.</span><span class="n">ru_utime</span><span class="p">.</span><span class="n">tv_sec</span><span class="o">+</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="n">self_ru</span><span class="p">.</span><span class="n">ru_utime</span><span class="p">.</span><span class="n">tv_usec</span><span class="o">/</span><span class="mi">1000000</span><span class="p">,</span>
        <span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="n">c_ru</span><span class="p">.</span><span class="n">ru_stime</span><span class="p">.</span><span class="n">tv_sec</span><span class="o">+</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="n">c_ru</span><span class="p">.</span><span class="n">ru_stime</span><span class="p">.</span><span class="n">tv_usec</span><span class="o">/</span><span class="mi">1000000</span><span class="p">,</span>
        <span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="n">c_ru</span><span class="p">.</span><span class="n">ru_utime</span><span class="p">.</span><span class="n">tv_sec</span><span class="o">+</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span><span class="n">c_ru</span><span class="p">.</span><span class="n">ru_utime</span><span class="p">.</span><span class="n">tv_usec</span><span class="o">/</span><span class="mi">1000000</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="cm">/* cmdtime */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">allsections</span> <span class="o">||</span> <span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;commandstats&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">sections</span><span class="o">++</span><span class="p">)</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span> <span class="s">&quot;# Commandstats</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">numcommands</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">redisCommandTable</span><span class="p">)</span><span class="o">/</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">redisCommand</span><span class="p">);</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">numcommands</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">struct</span> <span class="n">redisCommand</span> <span class="o">*</span><span class="n">c</span> <span class="o">=</span> <span class="n">redisCommandTable</span><span class="o">+</span><span class="n">j</span><span class="p">;</span>

            <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">calls</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
            <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
                <span class="s">&quot;cmdstat_%s:calls=%lld,usec=%lld,usec_per_call=%.2f</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
                <span class="n">c</span><span class="o">-&gt;</span><span class="n">name</span><span class="p">,</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">calls</span><span class="p">,</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">microseconds</span><span class="p">,</span>
                <span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">calls</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="o">?</span> <span class="mi">0</span> <span class="o">:</span> <span class="p">((</span><span class="kt">float</span><span class="p">)</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">microseconds</span><span class="o">/</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">calls</span><span class="p">));</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="cm">/* Cluster */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">allsections</span> <span class="o">||</span> <span class="n">defsections</span> <span class="o">||</span> <span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;cluster&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">sections</span><span class="o">++</span><span class="p">)</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span>
        <span class="s">&quot;# Cluster</span><span class="se">\r\n</span><span class="s">&quot;</span>
        <span class="s">&quot;cluster_enabled:%d</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
        <span class="n">server</span><span class="p">.</span><span class="n">cluster_enabled</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="cm">/* Key space */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">allsections</span> <span class="o">||</span> <span class="n">defsections</span> <span class="o">||</span> <span class="o">!</span><span class="n">strcasecmp</span><span class="p">(</span><span class="n">section</span><span class="p">,</span><span class="s">&quot;keyspace&quot;</span><span class="p">))</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">sections</span><span class="o">++</span><span class="p">)</span> <span class="n">info</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">info</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span> <span class="s">&quot;# Keyspace</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">dbnum</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
            <span class="kt">long</span> <span class="kt">long</span> <span class="n">keys</span><span class="p">,</span> <span class="n">vkeys</span><span class="p">;</span>

            <span class="n">keys</span> <span class="o">=</span> <span class="n">dictSize</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">dict</span><span class="p">);</span>
            <span class="n">vkeys</span> <span class="o">=</span> <span class="n">dictSize</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">expires</span><span class="p">);</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">keys</span> <span class="o">||</span> <span class="n">vkeys</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">info</span> <span class="o">=</span> <span class="n">sdscatprintf</span><span class="p">(</span><span class="n">info</span><span class="p">,</span> <span class="s">&quot;db%d:keys=%lld,expires=%lld</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
                    <span class="n">j</span><span class="p">,</span> <span class="n">keys</span><span class="p">,</span> <span class="n">vkeys</span><span class="p">);</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">info</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">infoCommand</span><span class="p">(</span><span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">char</span> <span class="o">*</span><span class="n">section</span> <span class="o">=</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">argc</span> <span class="o">==</span> <span class="mi">2</span> <span class="o">?</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">-&gt;</span><span class="n">ptr</span> <span class="o">:</span> <span class="s">&quot;default&quot;</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">argc</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">shared</span><span class="p">.</span><span class="n">syntaxerr</span><span class="p">);</span>
        <span class="k">return</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">sds</span> <span class="n">info</span> <span class="o">=</span> <span class="n">genRedisInfoString</span><span class="p">(</span><span class="n">section</span><span class="p">);</span>
    <span class="n">addReplySds</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">sdscatprintf</span><span class="p">(</span><span class="n">sdsempty</span><span class="p">(),</span><span class="s">&quot;$%lu</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">,</span>
        <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">long</span><span class="p">)</span><span class="n">sdslen</span><span class="p">(</span><span class="n">info</span><span class="p">)));</span>
    <span class="n">addReplySds</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">info</span><span class="p">);</span>
    <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">shared</span><span class="p">.</span><span class="n">crlf</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">monitorCommand</span><span class="p">(</span><span class="n">redisClient</span> <span class="o">*</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="cm">/* ignore MONITOR if aleady slave or in monitor mode */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">REDIS_SLAVE</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>

    <span class="n">c</span><span class="o">-&gt;</span><span class="n">flags</span> <span class="o">|=</span> <span class="p">(</span><span class="n">REDIS_SLAVE</span><span class="o">|</span><span class="n">REDIS_MONITOR</span><span class="p">);</span>
    <span class="n">c</span><span class="o">-&gt;</span><span class="n">slaveseldb</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">listAddNodeTail</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">monitors</span><span class="p">,</span><span class="n">c</span><span class="p">);</span>
    <span class="n">addReply</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="n">shared</span><span class="p">.</span><span class="n">ok</span><span class="p">);</span>
<span class="p">}</span>

<span class="cm">/* ============================ Maxmemory directive  ======================== */</span>

<span class="cm">/* This function gets called when &#39;maxmemory&#39; is set on the config file to limit</span>
<span class="cm"> * the max memory used by the server, before processing a command.</span>
<span class="cm"> *</span>
<span class="cm"> * The goal of the function is to free enough memory to keep Redis under the</span>
<span class="cm"> * configured memory limit.</span>
<span class="cm"> *</span>
<span class="cm"> * The function starts calculating how many bytes should be freed to keep</span>
<span class="cm"> * Redis under the limit, and enters a loop selecting the best keys to</span>
<span class="cm"> * evict accordingly to the configured policy.</span>
<span class="cm"> *</span>
<span class="cm"> * If all the bytes needed to return back under the limit were freed the</span>
<span class="cm"> * function returns REDIS_OK, otherwise REDIS_ERR is returned, and the caller</span>
<span class="cm"> * should block the execution of commands that will result in more memory</span>
<span class="cm"> * used by the server.</span>
<span class="cm"> */</span>
<span class="kt">int</span> <span class="nf">freeMemoryIfNeeded</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">size_t</span> <span class="n">mem_used</span><span class="p">,</span> <span class="n">mem_tofree</span><span class="p">,</span> <span class="n">mem_freed</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">slaves</span> <span class="o">=</span> <span class="n">listLength</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">slaves</span><span class="p">);</span>

    <span class="cm">/* Remove the size of slaves output buffers and AOF buffer from the</span>
<span class="cm">     * count of used memory. */</span>
    <span class="n">mem_used</span> <span class="o">=</span> <span class="n">zmalloc_used_memory</span><span class="p">();</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">slaves</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">listIter</span> <span class="n">li</span><span class="p">;</span>
        <span class="n">listNode</span> <span class="o">*</span><span class="n">ln</span><span class="p">;</span>

        <span class="n">listRewind</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">slaves</span><span class="p">,</span><span class="o">&amp;</span><span class="n">li</span><span class="p">);</span>
        <span class="k">while</span><span class="p">((</span><span class="n">ln</span> <span class="o">=</span> <span class="n">listNext</span><span class="p">(</span><span class="o">&amp;</span><span class="n">li</span><span class="p">)))</span> <span class="p">{</span>
            <span class="n">redisClient</span> <span class="o">*</span><span class="n">slave</span> <span class="o">=</span> <span class="n">listNodeValue</span><span class="p">(</span><span class="n">ln</span><span class="p">);</span>
            <span class="kt">unsigned</span> <span class="kt">long</span> <span class="n">obuf_bytes</span> <span class="o">=</span> <span class="n">getClientOutputBufferMemoryUsage</span><span class="p">(</span><span class="n">slave</span><span class="p">);</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">obuf_bytes</span> <span class="o">&gt;</span> <span class="n">mem_used</span><span class="p">)</span>
                <span class="n">mem_used</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
            <span class="k">else</span>
                <span class="n">mem_used</span> <span class="o">-=</span> <span class="n">obuf_bytes</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_state</span> <span class="o">!=</span> <span class="n">REDIS_AOF_OFF</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">mem_used</span> <span class="o">-=</span> <span class="n">sdslen</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_buf</span><span class="p">);</span>
        <span class="n">mem_used</span> <span class="o">-=</span> <span class="n">aofRewriteBufferSize</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="cm">/* Check if we are over the memory limit. */</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">mem_used</span> <span class="o">&lt;=</span> <span class="n">server</span><span class="p">.</span><span class="n">maxmemory</span><span class="p">)</span> <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">==</span> <span class="n">REDIS_MAXMEMORY_NO_EVICTION</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">REDIS_ERR</span><span class="p">;</span> <span class="cm">/* We need to free memory, but policy forbids. */</span>

    <span class="cm">/* Compute how much memory we need to free. */</span>
    <span class="n">mem_tofree</span> <span class="o">=</span> <span class="n">mem_used</span> <span class="o">-</span> <span class="n">server</span><span class="p">.</span><span class="n">maxmemory</span><span class="p">;</span>
    <span class="n">mem_freed</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">mem_freed</span> <span class="o">&lt;</span> <span class="n">mem_tofree</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">keys_freed</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

        <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">dbnum</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
            <span class="kt">long</span> <span class="n">bestval</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="cm">/* just to prevent warning */</span>
            <span class="n">sds</span> <span class="n">bestkey</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
            <span class="k">struct</span> <span class="n">dictEntry</span> <span class="o">*</span><span class="n">de</span><span class="p">;</span>
            <span class="n">redisDb</span> <span class="o">*</span><span class="n">db</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="o">+</span><span class="n">j</span><span class="p">;</span>
            <span class="n">dict</span> <span class="o">*</span><span class="n">dict</span><span class="p">;</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">==</span> <span class="n">REDIS_MAXMEMORY_ALLKEYS_LRU</span> <span class="o">||</span>
                <span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">==</span> <span class="n">REDIS_MAXMEMORY_ALLKEYS_RANDOM</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">dict</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">dict</span><span class="p">;</span>
            <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                <span class="n">dict</span> <span class="o">=</span> <span class="n">server</span><span class="p">.</span><span class="n">db</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">expires</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">dictSize</span><span class="p">(</span><span class="n">dict</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>

            <span class="cm">/* volatile-random and allkeys-random policy */</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">==</span> <span class="n">REDIS_MAXMEMORY_ALLKEYS_RANDOM</span> <span class="o">||</span>
                <span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">==</span> <span class="n">REDIS_MAXMEMORY_VOLATILE_RANDOM</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">de</span> <span class="o">=</span> <span class="n">dictGetRandomKey</span><span class="p">(</span><span class="n">dict</span><span class="p">);</span>
                <span class="n">bestkey</span> <span class="o">=</span> <span class="n">dictGetKey</span><span class="p">(</span><span class="n">de</span><span class="p">);</span>
            <span class="p">}</span>

            <span class="cm">/* volatile-lru and allkeys-lru policy */</span>
            <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">==</span> <span class="n">REDIS_MAXMEMORY_ALLKEYS_LRU</span> <span class="o">||</span>
                <span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">==</span> <span class="n">REDIS_MAXMEMORY_VOLATILE_LRU</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="k">for</span> <span class="p">(</span><span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">maxmemory_samples</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
                    <span class="n">sds</span> <span class="n">thiskey</span><span class="p">;</span>
                    <span class="kt">long</span> <span class="n">thisval</span><span class="p">;</span>
                    <span class="n">robj</span> <span class="o">*</span><span class="n">o</span><span class="p">;</span>

                    <span class="n">de</span> <span class="o">=</span> <span class="n">dictGetRandomKey</span><span class="p">(</span><span class="n">dict</span><span class="p">);</span>
                    <span class="n">thiskey</span> <span class="o">=</span> <span class="n">dictGetKey</span><span class="p">(</span><span class="n">de</span><span class="p">);</span>
                    <span class="cm">/* When policy is volatile-lru we need an additonal lookup</span>
<span class="cm">                     * to locate the real key, as dict is set to db-&gt;expires. */</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">==</span> <span class="n">REDIS_MAXMEMORY_VOLATILE_LRU</span><span class="p">)</span>
                        <span class="n">de</span> <span class="o">=</span> <span class="n">dictFind</span><span class="p">(</span><span class="n">db</span><span class="o">-&gt;</span><span class="n">dict</span><span class="p">,</span> <span class="n">thiskey</span><span class="p">);</span>
                    <span class="n">o</span> <span class="o">=</span> <span class="n">dictGetVal</span><span class="p">(</span><span class="n">de</span><span class="p">);</span>
                    <span class="n">thisval</span> <span class="o">=</span> <span class="n">estimateObjectIdleTime</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>

                    <span class="cm">/* Higher idle time is better candidate for deletion */</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">bestkey</span> <span class="o">==</span> <span class="nb">NULL</span> <span class="o">||</span> <span class="n">thisval</span> <span class="o">&gt;</span> <span class="n">bestval</span><span class="p">)</span> <span class="p">{</span>
                        <span class="n">bestkey</span> <span class="o">=</span> <span class="n">thiskey</span><span class="p">;</span>
                        <span class="n">bestval</span> <span class="o">=</span> <span class="n">thisval</span><span class="p">;</span>
                    <span class="p">}</span>
                <span class="p">}</span>
            <span class="p">}</span>

            <span class="cm">/* volatile-ttl */</span>
            <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">maxmemory_policy</span> <span class="o">==</span> <span class="n">REDIS_MAXMEMORY_VOLATILE_TTL</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">for</span> <span class="p">(</span><span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="n">server</span><span class="p">.</span><span class="n">maxmemory_samples</span><span class="p">;</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
                    <span class="n">sds</span> <span class="n">thiskey</span><span class="p">;</span>
                    <span class="kt">long</span> <span class="n">thisval</span><span class="p">;</span>

                    <span class="n">de</span> <span class="o">=</span> <span class="n">dictGetRandomKey</span><span class="p">(</span><span class="n">dict</span><span class="p">);</span>
                    <span class="n">thiskey</span> <span class="o">=</span> <span class="n">dictGetKey</span><span class="p">(</span><span class="n">de</span><span class="p">);</span>
                    <span class="n">thisval</span> <span class="o">=</span> <span class="p">(</span><span class="kt">long</span><span class="p">)</span> <span class="n">dictGetVal</span><span class="p">(</span><span class="n">de</span><span class="p">);</span>

                    <span class="cm">/* Expire sooner (minor expire unix timestamp) is better</span>
<span class="cm">                     * candidate for deletion */</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">bestkey</span> <span class="o">==</span> <span class="nb">NULL</span> <span class="o">||</span> <span class="n">thisval</span> <span class="o">&lt;</span> <span class="n">bestval</span><span class="p">)</span> <span class="p">{</span>
                        <span class="n">bestkey</span> <span class="o">=</span> <span class="n">thiskey</span><span class="p">;</span>
                        <span class="n">bestval</span> <span class="o">=</span> <span class="n">thisval</span><span class="p">;</span>
                    <span class="p">}</span>
                <span class="p">}</span>
            <span class="p">}</span>

            <span class="cm">/* Finally remove the selected key. */</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">bestkey</span><span class="p">)</span> <span class="p">{</span>
                <span class="kt">long</span> <span class="kt">long</span> <span class="n">delta</span><span class="p">;</span>

                <span class="n">robj</span> <span class="o">*</span><span class="n">keyobj</span> <span class="o">=</span> <span class="n">createStringObject</span><span class="p">(</span><span class="n">bestkey</span><span class="p">,</span><span class="n">sdslen</span><span class="p">(</span><span class="n">bestkey</span><span class="p">));</span>
                <span class="n">propagateExpire</span><span class="p">(</span><span class="n">db</span><span class="p">,</span><span class="n">keyobj</span><span class="p">);</span>
                <span class="cm">/* We compute the amount of memory freed by dbDelete() alone.</span>
<span class="cm">                 * It is possible that actually the memory needed to propagate</span>
<span class="cm">                 * the DEL in AOF and replication link is greater than the one</span>
<span class="cm">                 * we are freeing removing the key, but we can&#39;t account for</span>
<span class="cm">                 * that otherwise we would never exit the loop.</span>
<span class="cm">                 *</span>
<span class="cm">                 * AOF and Output buffer memory will be freed eventually so</span>
<span class="cm">                 * we only care about memory used by the key space. */</span>
                <span class="n">delta</span> <span class="o">=</span> <span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)</span> <span class="n">zmalloc_used_memory</span><span class="p">();</span>
                <span class="n">dbDelete</span><span class="p">(</span><span class="n">db</span><span class="p">,</span><span class="n">keyobj</span><span class="p">);</span>
                <span class="n">delta</span> <span class="o">-=</span> <span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)</span> <span class="n">zmalloc_used_memory</span><span class="p">();</span>
                <span class="n">mem_freed</span> <span class="o">+=</span> <span class="n">delta</span><span class="p">;</span>
                <span class="n">server</span><span class="p">.</span><span class="n">stat_evictedkeys</span><span class="o">++</span><span class="p">;</span>
                <span class="n">decrRefCount</span><span class="p">(</span><span class="n">keyobj</span><span class="p">);</span>
                <span class="n">keys_freed</span><span class="o">++</span><span class="p">;</span>

                <span class="cm">/* When the memory to free starts to be big enough, we may</span>
<span class="cm">                 * start spending so much time here that is impossible to</span>
<span class="cm">                 * deliver data to the slaves fast enough, so we force the</span>
<span class="cm">                 * transmission here inside the loop. */</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">slaves</span><span class="p">)</span> <span class="n">flushSlavesOutputBuffers</span><span class="p">();</span>
            <span class="p">}</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">keys_freed</span><span class="p">)</span> <span class="k">return</span> <span class="n">REDIS_ERR</span><span class="p">;</span> <span class="cm">/* nothing to free... */</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">REDIS_OK</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* =================================== Main! ================================ */</span>

<span class="cp">#ifdef __linux__</span>
<span class="kt">int</span> <span class="nf">linuxOvercommitMemoryValue</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">FILE</span> <span class="o">*</span><span class="n">fp</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="s">&quot;/proc/sys/vm/overcommit_memory&quot;</span><span class="p">,</span><span class="s">&quot;r&quot;</span><span class="p">);</span>
    <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">64</span><span class="p">];</span>

    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">fp</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">fgets</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="mi">64</span><span class="p">,</span><span class="n">fp</span><span class="p">)</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>

    <span class="k">return</span> <span class="n">atoi</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">linuxOvercommitMemoryWarning</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">linuxOvercommitMemoryValue</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add &#39;vm.overcommit_memory = 1&#39; to /etc/sysctl.conf and then reboot or run the command &#39;sysctl vm.overcommit_memory=1&#39; for this to take effect.&quot;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="cp">#endif </span><span class="cm">/* __linux__ */</span><span class="cp"></span>

<span class="kt">void</span> <span class="nf">createPidFile</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="cm">/* Try to write the pid file in a best-effort way. */</span>
    <span class="kt">FILE</span> <span class="o">*</span><span class="n">fp</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">pidfile</span><span class="p">,</span><span class="s">&quot;w&quot;</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">fp</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span><span class="s">&quot;%d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,(</span><span class="kt">int</span><span class="p">)</span><span class="n">getpid</span><span class="p">());</span>
        <span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">daemonize</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">fd</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">fork</span><span class="p">()</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="cm">/* parent exits */</span>
    <span class="n">setsid</span><span class="p">();</span> <span class="cm">/* create a new session */</span>

    <span class="cm">/* Every output goes to /dev/null. If Redis is daemonized but</span>
<span class="cm">     * the &#39;logfile&#39; is set to &#39;stdout&#39; in the configuration file</span>
<span class="cm">     * it will not log at all. */</span>
    <span class="k">if</span> <span class="p">((</span><span class="n">fd</span> <span class="o">=</span> <span class="n">open</span><span class="p">(</span><span class="s">&quot;/dev/null&quot;</span><span class="p">,</span> <span class="n">O_RDWR</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">dup2</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">STDIN_FILENO</span><span class="p">);</span>
        <span class="n">dup2</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">STDOUT_FILENO</span><span class="p">);</span>
        <span class="n">dup2</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">STDERR_FILENO</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">fd</span> <span class="o">&gt;</span> <span class="n">STDERR_FILENO</span><span class="p">)</span> <span class="n">close</span><span class="p">(</span><span class="n">fd</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">version</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Redis server v=%s sha=%s:%d malloc=%s bits=%d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span>
        <span class="n">REDIS_VERSION</span><span class="p">,</span>
        <span class="n">redisGitSHA1</span><span class="p">(),</span>
        <span class="n">atoi</span><span class="p">(</span><span class="n">redisGitDirty</span><span class="p">())</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span>
        <span class="n">ZMALLOC_LIB</span><span class="p">,</span>
        <span class="k">sizeof</span><span class="p">(</span><span class="kt">long</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span> <span class="o">?</span> <span class="mi">32</span> <span class="o">:</span> <span class="mi">64</span><span class="p">);</span>
    <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">usage</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;Usage: ./redis-server [/path/to/redis.conf] [options]</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;       ./redis-server - (read config from stdin)</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;       ./redis-server -v or --version</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;       ./redis-server -h or --help</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;       ./redis-server --test-memory &lt;megabytes&gt;</span><span class="se">\n\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;Examples:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;       ./redis-server (run the server with default conf)</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;       ./redis-server /etc/redis/6379.conf</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;       ./redis-server --port 7777</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;       ./redis-server --port 7777 --slaveof 127.0.0.1 8888</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;       ./redis-server /etc/myredis.conf --loglevel verbose</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">redisAsciiArt</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
<span class="cp">#include &quot;asciilogo.h&quot;</span>
    <span class="kt">char</span> <span class="o">*</span><span class="n">buf</span> <span class="o">=</span> <span class="n">zmalloc</span><span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">16</span><span class="p">);</span>

    <span class="n">snprintf</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="mi">1024</span><span class="o">*</span><span class="mi">16</span><span class="p">,</span><span class="n">ascii_logo</span><span class="p">,</span>
        <span class="n">REDIS_VERSION</span><span class="p">,</span>
        <span class="n">redisGitSHA1</span><span class="p">(),</span>
        <span class="n">strtol</span><span class="p">(</span><span class="n">redisGitDirty</span><span class="p">(),</span><span class="nb">NULL</span><span class="p">,</span><span class="mi">10</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span>
        <span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="kt">long</span><span class="p">)</span> <span class="o">==</span> <span class="mi">8</span><span class="p">)</span> <span class="o">?</span> <span class="s">&quot;64&quot;</span> <span class="o">:</span> <span class="s">&quot;32&quot;</span><span class="p">,</span>
        <span class="n">server</span><span class="p">.</span><span class="n">cluster_enabled</span> <span class="o">?</span> <span class="s">&quot;cluster&quot;</span> <span class="o">:</span> <span class="s">&quot;stand alone&quot;</span><span class="p">,</span>
        <span class="n">server</span><span class="p">.</span><span class="n">port</span><span class="p">,</span>
        <span class="p">(</span><span class="kt">long</span><span class="p">)</span> <span class="n">getpid</span><span class="p">()</span>
    <span class="p">);</span>
    <span class="n">redisLogRaw</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="o">|</span><span class="n">REDIS_LOG_RAW</span><span class="p">,</span><span class="n">buf</span><span class="p">);</span>
    <span class="n">zfree</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span>
<span class="p">}</span>

<span class="k">static</span> <span class="kt">void</span> <span class="nf">sigtermHandler</span><span class="p">(</span><span class="kt">int</span> <span class="n">sig</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">REDIS_NOTUSED</span><span class="p">(</span><span class="n">sig</span><span class="p">);</span>

    <span class="n">redisLogFromHandler</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;Received SIGTERM, scheduling shutdown...&quot;</span><span class="p">);</span>
    <span class="n">server</span><span class="p">.</span><span class="n">shutdown_asap</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">setupSignalHandlers</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">struct</span> <span class="n">sigaction</span> <span class="n">act</span><span class="p">;</span>

    <span class="cm">/* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction is used.</span>
<span class="cm">     * Otherwise, sa_handler is used. */</span>
    <span class="n">sigemptyset</span><span class="p">(</span><span class="o">&amp;</span><span class="n">act</span><span class="p">.</span><span class="n">sa_mask</span><span class="p">);</span>
    <span class="n">act</span><span class="p">.</span><span class="n">sa_flags</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="n">act</span><span class="p">.</span><span class="n">sa_handler</span> <span class="o">=</span> <span class="n">sigtermHandler</span><span class="p">;</span>
    <span class="n">sigaction</span><span class="p">(</span><span class="n">SIGTERM</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">act</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>

<span class="cp">#ifdef HAVE_BACKTRACE</span>
    <span class="n">sigemptyset</span><span class="p">(</span><span class="o">&amp;</span><span class="n">act</span><span class="p">.</span><span class="n">sa_mask</span><span class="p">);</span>
    <span class="n">act</span><span class="p">.</span><span class="n">sa_flags</span> <span class="o">=</span> <span class="n">SA_NODEFER</span> <span class="o">|</span> <span class="n">SA_RESETHAND</span> <span class="o">|</span> <span class="n">SA_SIGINFO</span><span class="p">;</span>
    <span class="n">act</span><span class="p">.</span><span class="n">sa_sigaction</span> <span class="o">=</span> <span class="n">sigsegvHandler</span><span class="p">;</span>
    <span class="n">sigaction</span><span class="p">(</span><span class="n">SIGSEGV</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">act</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
    <span class="n">sigaction</span><span class="p">(</span><span class="n">SIGBUS</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">act</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
    <span class="n">sigaction</span><span class="p">(</span><span class="n">SIGFPE</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">act</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
    <span class="n">sigaction</span><span class="p">(</span><span class="n">SIGILL</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">act</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="cp">#endif</span>
    <span class="k">return</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="n">memtest</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">megabytes</span><span class="p">,</span> <span class="kt">int</span> <span class="n">passes</span><span class="p">);</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">long</span> <span class="kt">long</span> <span class="n">start</span><span class="p">;</span>
    <span class="k">struct</span> <span class="n">timeval</span> <span class="n">tv</span><span class="p">;</span>

    <span class="cm">/* We need to initialize our libraries, and the server configuration. */</span>
    <span class="n">zmalloc_enable_thread_safeness</span><span class="p">();</span>
    <span class="n">srand</span><span class="p">(</span><span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span><span class="o">^</span><span class="n">getpid</span><span class="p">());</span>
    <span class="n">gettimeofday</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tv</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
    <span class="n">dictSetHashFunctionSeed</span><span class="p">(</span><span class="n">tv</span><span class="p">.</span><span class="n">tv_sec</span><span class="o">^</span><span class="n">tv</span><span class="p">.</span><span class="n">tv_usec</span><span class="o">^</span><span class="n">getpid</span><span class="p">());</span>
    <span class="n">initServerConfig</span><span class="p">();</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">argc</span> <span class="o">&gt;=</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="cm">/* First option to parse in argv[] */</span>
        <span class="n">sds</span> <span class="n">options</span> <span class="o">=</span> <span class="n">sdsempty</span><span class="p">();</span>
        <span class="kt">char</span> <span class="o">*</span><span class="n">configfile</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>

        <span class="cm">/* Handle special options --help and --version */</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">strcmp</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s">&quot;-v&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">||</span>
            <span class="n">strcmp</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s">&quot;--version&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="n">version</span><span class="p">();</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">strcmp</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s">&quot;--help&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">||</span>
            <span class="n">strcmp</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s">&quot;-h&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="n">usage</span><span class="p">();</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">strcmp</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s">&quot;--test-memory&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">argc</span> <span class="o">==</span> <span class="mi">3</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">memtest</span><span class="p">(</span><span class="n">atoi</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">]),</span><span class="mi">50</span><span class="p">);</span>
                <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
            <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;Please specify the amount of memory to test in megabytes.</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
                <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&quot;Example: ./redis-server --test-memory 4096</span><span class="se">\n\n</span><span class="s">&quot;</span><span class="p">);</span>
                <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="cm">/* First argument is the config file name? */</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="sc">&#39;-&#39;</span> <span class="o">||</span> <span class="n">argv</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="sc">&#39;-&#39;</span><span class="p">)</span>
            <span class="n">configfile</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="n">j</span><span class="o">++</span><span class="p">];</span>
        <span class="cm">/* All the other options are parsed and conceptually appended to the</span>
<span class="cm">         * configuration file. For instance --port 6380 will generate the</span>
<span class="cm">         * string &quot;port 6380\n&quot; to be parsed after the actual file name</span>
<span class="cm">         * is parsed, if any. */</span>
        <span class="k">while</span><span class="p">(</span><span class="n">j</span> <span class="o">!=</span> <span class="n">argc</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="sc">&#39;-&#39;</span> <span class="o">&amp;&amp;</span> <span class="n">argv</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="sc">&#39;-&#39;</span><span class="p">)</span> <span class="p">{</span>
                <span class="cm">/* Option name */</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">sdslen</span><span class="p">(</span><span class="n">options</span><span class="p">))</span> <span class="n">options</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">options</span><span class="p">,</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
                <span class="n">options</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">options</span><span class="p">,</span><span class="n">argv</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">+</span><span class="mi">2</span><span class="p">);</span>
                <span class="n">options</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">options</span><span class="p">,</span><span class="s">&quot; &quot;</span><span class="p">);</span>
            <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                <span class="cm">/* Option argument */</span>
                <span class="n">options</span> <span class="o">=</span> <span class="n">sdscatrepr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span><span class="n">argv</span><span class="p">[</span><span class="n">j</span><span class="p">],</span><span class="n">strlen</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="n">j</span><span class="p">]));</span>
                <span class="n">options</span> <span class="o">=</span> <span class="n">sdscat</span><span class="p">(</span><span class="n">options</span><span class="p">,</span><span class="s">&quot; &quot;</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="n">j</span><span class="o">++</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="n">resetServerSaveParams</span><span class="p">();</span>
        <span class="n">loadServerConfig</span><span class="p">(</span><span class="n">configfile</span><span class="p">,</span><span class="n">options</span><span class="p">);</span>
        <span class="n">sdsfree</span><span class="p">(</span><span class="n">options</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;Warning: no config file specified, using the default config. In order to specify a config file use &#39;redis-server /path/to/redis.conf&#39;&quot;</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">daemonize</span><span class="p">)</span> <span class="n">daemonize</span><span class="p">();</span>
    <span class="n">initServer</span><span class="p">();</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">daemonize</span><span class="p">)</span> <span class="n">createPidFile</span><span class="p">();</span>
    <span class="n">redisAsciiArt</span><span class="p">();</span>
    <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;Server started, Redis version &quot;</span> <span class="n">REDIS_VERSION</span><span class="p">);</span>
<span class="cp">#ifdef __linux__</span>
    <span class="n">linuxOvercommitMemoryWarning</span><span class="p">();</span>
<span class="cp">#endif</span>
    <span class="n">start</span> <span class="o">=</span> <span class="n">ustime</span><span class="p">();</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_state</span> <span class="o">==</span> <span class="n">REDIS_AOF_ON</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">loadAppendOnlyFile</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">aof_filename</span><span class="p">)</span> <span class="o">==</span> <span class="n">REDIS_OK</span><span class="p">)</span>
            <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;DB loaded from append only file: %.3f seconds&quot;</span><span class="p">,(</span><span class="kt">float</span><span class="p">)(</span><span class="n">ustime</span><span class="p">()</span><span class="o">-</span><span class="n">start</span><span class="p">)</span><span class="o">/</span><span class="mi">1000000</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">rdbLoad</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">rdb_filename</span><span class="p">)</span> <span class="o">==</span> <span class="n">REDIS_OK</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;DB loaded from disk: %.3f seconds&quot;</span><span class="p">,</span>
                <span class="p">(</span><span class="kt">float</span><span class="p">)(</span><span class="n">ustime</span><span class="p">()</span><span class="o">-</span><span class="n">start</span><span class="p">)</span><span class="o">/</span><span class="mi">1000000</span><span class="p">);</span>
        <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">errno</span> <span class="o">!=</span> <span class="n">ENOENT</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_WARNING</span><span class="p">,</span><span class="s">&quot;Fatal error loading the DB. Exiting.&quot;</span><span class="p">);</span>
            <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">ipfd</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;The server is now ready to accept connections on port %d&quot;</span><span class="p">,</span> <span class="n">server</span><span class="p">.</span><span class="n">port</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">sofd</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">redisLog</span><span class="p">(</span><span class="n">REDIS_NOTICE</span><span class="p">,</span><span class="s">&quot;The server is now ready to accept connections at %s&quot;</span><span class="p">,</span> <span class="n">server</span><span class="p">.</span><span class="n">unixsocket</span><span class="p">);</span>
    <span class="n">aeSetBeforeSleepProc</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">el</span><span class="p">,</span><span class="n">beforeSleep</span><span class="p">);</span>
    <span class="n">aeMain</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">el</span><span class="p">);</span>
    <span class="n">aeDeleteEventLoop</span><span class="p">(</span><span class="n">server</span><span class="p">.</span><span class="n">el</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* The End */</span>
</pre></div>
