<div>Hello!</div><div><br></div><div>cacheline calculation is hardcode in ngx_cpuinfo, this will make mistake in some intel processor. example cache line is 64 byte in sandy bridge, its family code is 0110 and model no is 1010 or 1101(in this document <a href="http://www.intel.com/content/www/us/en/processors/processor-identification-cpuid-instruction-note.html">http://www.intel.com/content/www/us/en/processors/processor-identification-cpuid-instruction-note.html</a>). but code is this in ngx_cpuinfo:</div>
<div><br></div><div><div> /* Pentium Pro, II, III */</div><div> case 6:</div><div> ngx_cacheline_size = 32;</div><div><br></div><div> model = ((cpu[0] & 0xf0000) >> 8) | (cpu[0] & 0xf0);</div>
<div><br></div><div> if (model >= 0xd0) {</div><div> /* Intel Core, Core 2, Atom */</div><div> ngx_cacheline_size = 64;</div><div> }</div><div><br></div><div> break;</div>
</div><div><br></div><div>if model no is 1010 , ngx_cacheline_size will be 32, and so this is wrong.</div><div><br></div><div><span style>Below is a patch</span><span style>(for nginx trunk)</span><span style> fix this problem, and use cpuid(2) solve hardcode。</span></div>
<div><span style><br></span></div><div><span style><div><font color="#222222" face="arial, sans-serif"><div><div>Index: src/core/ngx_cpuinfo.c</div><div>===================================================================</div>
<div>--- src/core/ngx_cpuinfo.c<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 4615)</div><div>+++ src/core/ngx_cpuinfo.c<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div>
<div>@@ -12,9 +12,93 @@</div><div> #if (( __i386__ || __amd64__ ) && ( __GNUC__ || __INTEL_COMPILER ))</div><div> </div><div> </div><div>+#define NGX_CACHE_LVL_1_DATA 1</div><div>+#define NGX_CACHE_LVL_2 2</div>
<div>+#define NGX_CACHE_LVL_3 3</div><div>+#define NGX_CACHE_PREFETCHING 4</div><div>+</div><div>+</div><div>+typedef struct ngx_cache_table {</div><div>+ u_char descriptor;</div><div>
+ u_char type;</div><div>+ ngx_uint_t size;</div><div>+} ngx_cache_table_t;</div><div>+</div><div>+</div><div> static ngx_inline void ngx_cpuid(uint32_t i, uint32_t *buf);</div><div> </div><div> </div>
<div>+static ngx_cache_table_t cache_table[] = {</div><div>+ { 0x0a, NGX_CACHE_LVL_1_DATA, 32 }, /* 32 byte line size */</div><div>+ { 0x0c, NGX_CACHE_LVL_1_DATA, 32 }, /* 32 byte line size */</div><div>+ { 0x0d, NGX_CACHE_LVL_1_DATA, 64 }, /* 64 byte line size */</div>
<div>+ { 0x0e, NGX_CACHE_LVL_1_DATA, 64 }, /* 64 byte line size */</div><div>+ { 0x21, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x22, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div>
<div>+ { 0x23, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0x25, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0x29, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div>
<div>+ { 0x2c, NGX_CACHE_LVL_1_DATA, 64 }, /* 64 byte line size */</div><div>+ { 0x39, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x3a, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div>
<div>+ { 0x3b, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x3c, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x3d, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div>
<div>+ { 0x3e, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x3f, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x41, NGX_CACHE_LVL_2, 32 }, /* 32 byte line size */</div>
<div>+ { 0x42, NGX_CACHE_LVL_2, 32 }, /* 32 byte line size */</div><div>+ { 0x43, NGX_CACHE_LVL_2, 32 }, /* 32 byte line size */</div><div>+ { 0x44, NGX_CACHE_LVL_2, 32 }, /* 32 byte line size */</div>
<div>+ { 0x45, NGX_CACHE_LVL_2, 32 }, /* 32 byte line size */</div><div>+ { 0x46, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0x47, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div>
<div>+ { 0x48, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x49, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0x4a, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div>
<div>+ { 0x4b, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0x4c, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0x4d, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div>
<div>+ { 0x4e, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x60, NGX_CACHE_LVL_1_DATA, 64 }, /* 64 byte line size */</div><div>+ { 0x66, NGX_CACHE_LVL_1_DATA, 64 }, /* 64 byte line size */</div>
<div>+ { 0x67, NGX_CACHE_LVL_1_DATA, 64 }, /* 64 byte line size */</div><div>+ { 0x68, NGX_CACHE_LVL_1_DATA, 64 }, /* 64 byte line size */</div><div>+ { 0x78, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div>
<div>+ { 0x79, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x7a, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x7b, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div>
<div>+ { 0x7c, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x7d, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x7f, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div>
<div>+ { 0x80, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0x82, NGX_CACHE_LVL_2, 32 }, /* 32 byte line size */</div><div>+ { 0x83, NGX_CACHE_LVL_2, 32 }, /* 32 byte line size */</div>
<div>+ { 0x84, NGX_CACHE_LVL_2, 32 }, /* 32 byte line size */</div><div>+ { 0x85, NGX_CACHE_LVL_2, 32 }, /* 32 byte line size */</div><div>+ { 0x86, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div>
<div>+ { 0x87, NGX_CACHE_LVL_2, 64 }, /* 64 byte line size */</div><div>+ { 0xd0, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0xd1, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div>
<div>+ { 0xd2, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0xd6, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0xd7, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div>
<div>+ { 0xd8, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0xdc, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0xdd, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div>
<div>+ { 0xde, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0xe2, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0xe3, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div>
<div>+ { 0xe4, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0xea, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0xeb, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div>
<div>+ { 0xec, NGX_CACHE_LVL_3, 64 }, /* 64 byte line size */</div><div>+ { 0xf0, NGX_CACHE_PREFETCHING, 64 }, /* 64-byte prefetching */</div><div>+ { 0xf1, NGX_CACHE_PREFETCHING, 128 }, /* 128-byte prefetching */</div>
<div>+ { 0x00, 0, 0}</div><div>+};</div><div>+</div><div>+</div><div> #if ( __i386__ )</div><div> </div><div> static ngx_inline void</div><div>@@ -67,13 +151,25 @@</div><div> #endif</div><div> </div><div> </div><div>+static ngx_inline</div>
<div>+uint32_t ngx_cpuid_eax(uint32_t op)</div><div>+{</div><div>+ uint32_t cpu[4];</div><div>+</div><div>+ ngx_cpuid(op, cpu);</div><div>+</div><div>+ return cpu[0];</div><div>+}</div><div>+</div><div>+</div>
<div>
/* auto detect the L2 cache line size of modern and widespread CPUs */</div><div> </div><div> void</div><div> ngx_cpuinfo(void)</div><div> {</div><div>- u_char *vendor;</div><div>- uint32_t vbuf[5], cpu[4], model;</div>
<div>+ u_char *vendor, *dp, des;</div><div>+ uint32_t vbuf[5], cache[4], n;</div><div>+ ngx_uint_t i, j, k, l1, l2, l3, prefetch;</div><div> </div><div> vbuf[0] = 0;</div><div> vbuf[1] = 0;</div><div>
@@ -81,6 +177,13 @@</div><div> vbuf[3] = 0;</div><div> vbuf[4] = 0;</div><div> </div><div>+ l1 = 0;</div><div>+ l2 = 0;</div><div>+ l3 = 0;</div><div>+ prefetch = 0;</div><div>+</div><div>+ dp = (u_char *) cache;</div>
<div>+</div><div> ngx_cpuid(0, vbuf);</div><div> </div><div> vendor = (u_char *) &vbuf[1];</div><div>@@ -89,39 +192,57 @@</div><div> return;</div><div> }</div><div> </div><div>- ngx_cpuid(1, cpu);</div>
<div>-</div><div> if (ngx_strcmp(vendor, "GenuineIntel") == 0) {</div><div> </div><div>- switch ((cpu[0] & 0xf00) >> 8) {</div><div>+ n = ngx_cpuid_eax(2) & 0xFF;</div><div> </div>
<div>- /* Pentium */</div><div>- case 5:</div><div>- ngx_cacheline_size = 32;</div><div>- break;</div><div>+ for (i = 0 ; i < n ; i++) {</div><div>+ ngx_cpuid(2, cache);</div>
<div> </div><div>- /* Pentium Pro, II, III */</div><div>- case 6:</div><div>- ngx_cacheline_size = 32;</div><div>+ for (j = 0; j < 3; j++) {</div><div>+ if (cache[j] & (1 << 31)) {</div>
<div>+ cache[j] = 0;</div><div>+ }</div><div>+ }</div><div> </div><div>- model = ((cpu[0] & 0xf0000) >> 8) | (cpu[0] & 0xf0);</div><div>+ for (j = 1; j < 16; j++) {</div>
<div>+ des = dp[j];</div><div>+ k = 0;</div><div> </div><div>- if (model >= 0xd0) {</div><div>- /* Intel Core, Core 2, Atom */</div><div>- ngx_cacheline_size = 64;</div>
<div>- }</div><div>+ while (cache_table[k].descriptor != 0) {</div><div>+ if (cache_table[k].descriptor == des) {</div><div> </div><div>- break;</div><div>+ switch (cache_table[k].type) {</div>
<div> </div><div>- /*</div><div>- * Pentium 4, although its cache line size is 64 bytes,</div><div>- * it prefetches up to two cache lines during memory read</div><div>- */</div><div>- case 15:</div>
<div>- ngx_cacheline_size = 128;</div><div>- break;</div><div>+ case NGX_CACHE_LVL_1_DATA:</div><div>+ l1 = cache_table[k].size;</div><div>+ break;</div>
<div>+</div><div>+ case NGX_CACHE_LVL_2:</div><div>+ l2 = cache_table[k].size;</div><div>+ break;</div><div>+</div><div>+ case NGX_CACHE_LVL_3:</div>
<div>+ l3 = cache_table[k].size;</div><div>+ break;</div><div>+</div><div>+ case NGX_CACHE_PREFETCHING:</div><div>+ prefetch = cache_table[k].size;</div>
<div>+ break;</div><div>+ }</div><div>+</div><div>+ break;</div><div>+ }</div><div>+</div><div>+ k++;</div><div>
+ }</div><div>+ }</div><div> }</div><div> </div><div>+ ngx_cacheline_size = ngx_max(l1, l2);</div><div>+ ngx_cacheline_size = ngx_max(l3, ngx_cacheline_size);</div><div>+ ngx_cacheline_size = ngx_max(prefetch, ngx_cacheline_size);</div>
<div>+</div><div> } else if (ngx_strcmp(vendor, "AuthenticAMD") == 0) {</div><div> ngx_cacheline_size = 64;</div><div> }</div></div><div><br></div><div><br></div></font></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">
<br></div></span></div><div><br></div>-- <br><div><span style="color:rgb(102,102,102);font-family:Arial,Helvetica,sans-serif;font-size:12px;line-height:18px;background-color:rgb(255,246,237)">do not fear to be eccentric in opinion, for every opinion now accepted was once eccentric. </span></div>
<br>