src/core/ngx_cpuinfo.c - nginx-1.7.10

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>


  7. #if (( __i386__ || __amd64__ ) && ( __GNUC__ || __INTEL_COMPILER ))


  8. static ngx_inline void ngx_cpuid(uint32_t i, uint32_t *buf);


  9. #if ( __i386__ )

  10. static ngx_inline void
  11. ngx_cpuid(uint32_t i, uint32_t *buf)
  12. {

  13.     /*
  14.      * we could not use %ebx as output parameter if gcc builds PIC,
  15.      * and we could not save %ebx on stack, because %esp is used,
  16.      * when the -fomit-frame-pointer optimization is specified.
  17.      */

  18.     __asm__ (

  19.     "    mov    %%ebx, %%esi;  "

  20.     "    cpuid;                "
  21.     "    mov    %%eax, (%1);   "
  22.     "    mov    %%ebx, 4(%1);  "
  23.     "    mov    %%edx, 8(%1);  "
  24.     "    mov    %%ecx, 12(%1); "

  25.     "    mov    %%esi, %%ebx;  "

  26.     : : "a" (i), "D" (buf) : "ecx", "edx", "esi", "memory" );
  27. }


  28. #else /* __amd64__ */


  29. static ngx_inline void
  30. ngx_cpuid(uint32_t i, uint32_t *buf)
  31. {
  32.     uint32_t  eax, ebx, ecx, edx;

  33.     __asm__ (

  34.         "cpuid"

  35.     : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (i) );

  36.     buf[0] = eax;
  37.     buf[1] = ebx;
  38.     buf[2] = edx;
  39.     buf[3] = ecx;
  40. }


  41. #endif


  42. /* auto detect the L2 cache line size of modern and widespread CPUs */

  43. void
  44. ngx_cpuinfo(void)
  45. {
  46.     u_char    *vendor;
  47.     uint32_t   vbuf[5], cpu[4], model;

  48.     vbuf[0] = 0;
  49.     vbuf[1] = 0;
  50.     vbuf[2] = 0;
  51.     vbuf[3] = 0;
  52.     vbuf[4] = 0;

  53.     ngx_cpuid(0, vbuf);

  54.     vendor = (u_char *) &vbuf[1];

  55.     if (vbuf[0] == 0) {
  56.         return;
  57.     }

  58.     ngx_cpuid(1, cpu);

  59.     if (ngx_strcmp(vendor, "GenuineIntel") == 0) {

  60.         switch ((cpu[0] & 0xf00) >> 8) {

  61.         /* Pentium */
  62.         case 5:
  63.             ngx_cacheline_size = 32;
  64.             break;

  65.         /* Pentium Pro, II, III */
  66.         case 6:
  67.             ngx_cacheline_size = 32;

  68.             model = ((cpu[0] & 0xf0000) >> 8) | (cpu[0] & 0xf0);

  69.             if (model >= 0xd0) {
  70.                 /* Intel Core, Core 2, Atom */
  71.                 ngx_cacheline_size = 64;
  72.             }

  73.             break;

  74.         /*
  75.          * Pentium 4, although its cache line size is 64 bytes,
  76.          * it prefetches up to two cache lines during memory read
  77.          */
  78.         case 15:
  79.             ngx_cacheline_size = 128;
  80.             break;
  81.         }

  82.     } else if (ngx_strcmp(vendor, "AuthenticAMD") == 0) {
  83.         ngx_cacheline_size = 64;
  84.     }
  85. }

  86. #else


  87. void
  88. ngx_cpuinfo(void)
  89. {
  90. }


  91. #endif