cpu.scm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix cpu)
  19. #:use-module (guix sets)
  20. #:use-module (guix memoization)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-9)
  23. #:use-module (ice-9 match)
  24. #:use-module (ice-9 rdelim)
  25. #:export (current-cpu
  26. cpu?
  27. cpu-architecture
  28. cpu-family
  29. cpu-model
  30. cpu-flags
  31. cpu->gcc-architecture))
  32. ;;; Commentary:
  33. ;;;
  34. ;;; This module provides tools to determine the micro-architecture supported
  35. ;;; by the CPU and to map it to a name known to GCC's '-march'.
  36. ;;;
  37. ;;; Code:
  38. ;; CPU description.
  39. (define-record-type <cpu>
  40. (cpu architecture family model flags)
  41. cpu?
  42. (architecture cpu-architecture) ;string, from 'uname'
  43. (family cpu-family) ;integer
  44. (model cpu-model) ;integer
  45. (flags cpu-flags)) ;set of strings
  46. (define current-cpu
  47. (mlambda ()
  48. "Return a <cpu> record representing the host CPU."
  49. (define (prefix? prefix)
  50. (lambda (str)
  51. (string-prefix? prefix str)))
  52. (call-with-input-file "/proc/cpuinfo"
  53. (lambda (port)
  54. (let loop ((family #f)
  55. (model #f))
  56. (match (read-line port)
  57. ((? eof-object?)
  58. #f)
  59. ((? (prefix? "cpu family") str)
  60. (match (string-tokenize str)
  61. (("cpu" "family" ":" family)
  62. (loop (string->number family) model))))
  63. ((? (prefix? "model") str)
  64. (match (string-tokenize str)
  65. (("model" ":" model)
  66. (loop family (string->number model)))
  67. (_
  68. (loop family model))))
  69. ((? (prefix? "flags") str)
  70. (match (string-tokenize str)
  71. (("flags" ":" flags ...)
  72. (cpu (utsname:machine (uname))
  73. family model (list->set flags)))))
  74. (_
  75. (loop family model))))))))
  76. (define (cpu->gcc-architecture cpu)
  77. "Return the architecture name, suitable for GCC's '-march' flag, that
  78. corresponds to CPU, a record as returned by 'current-cpu'."
  79. (match (cpu-architecture cpu)
  80. ("x86_64"
  81. ;; Transcribed from GCC's 'host_detect_local_cpu' in driver-i386.c.
  82. (or (and (= 6 (cpu-family cpu)) ;the "Pentium Pro" family
  83. (letrec-syntax ((model (syntax-rules (=>)
  84. ((_) #f)
  85. ((_ (candidate => integers ...) rest
  86. ...)
  87. (or (and (= (cpu-model cpu) integers)
  88. candidate)
  89. ...
  90. (model rest ...))))))
  91. (model ("bonnel" => #x1c #x26)
  92. ("silvermont" => #x37 #x4a #x4d #x5a #x5d)
  93. ("core2" => #x0f #x17 #x1d)
  94. ("nehalem" => #x1a #x1e #x1f #x2e)
  95. ("westmere" => #x25 #x2c #x2f)
  96. ("sandybridge" => #x2a #x2d)
  97. ("ivybridge" => #x3a #x3e)
  98. ("haswell" => #x3c #x3f #x45 #x46)
  99. ("broadwell" => #x3d #x47 #x4f #x56)
  100. ("skylake" => #x4e #x5e #x8e #x9e)
  101. ("skylake-avx512" => #x55) ;TODO: cascadelake
  102. ("knl" => #x57)
  103. ("cannonlake" => #x66)
  104. ("knm" => #x85))))
  105. ;; Fallback case for non-Intel processors or for Intel processors not
  106. ;; recognized above.
  107. (letrec-syntax ((if-flags (syntax-rules (=>)
  108. ((_)
  109. #f)
  110. ((_ (flags ... => name) rest ...)
  111. (if (every (lambda (flag)
  112. (set-contains? (cpu-flags cpu)
  113. flag))
  114. '(flags ...))
  115. name
  116. (if-flags rest ...))))))
  117. (if-flags ("avx512" => "knl")
  118. ("adx" => "broadwell")
  119. ("avx2" => "haswell")
  120. ;; TODO: tigerlake, cooperlake, etc.
  121. ("avx" => "sandybridge")
  122. ("sse4_2" "gfni" => "tremont")
  123. ("sse4_2" "sgx" => "goldmont-plus")
  124. ("sse4_2" "xsave" => "goldmont")
  125. ("sse4_2" "movbe" => "silvermont")
  126. ("sse4_2" => "nehalem")
  127. ("ssse3" "movbe" => "bonnell")
  128. ("ssse3" => "core2")))
  129. ;; TODO: Recognize AMD models (bdver*, znver*, etc.)?
  130. "x86_64"))
  131. (architecture
  132. ;; TODO: AArch64.
  133. architecture)))