Repository: spack/spack

Issue:
Mac M1 sysctl
I'm getting some sysctl error messages when running the spack setup-env shell scripts. 

### Error messages

For both bash and fish I see:

```
$ source share/spack/setup-env.sh
sysctl: unknown oid 'machdep.cpu.leaf7_features'
sysctl: unknown oid 'machdep.cpu.vendor'
sysctl: unknown oid 'machdep.cpu.model'
sysctl: unknown oid 'machdep.cpu.leaf7_features'
sysctl: unknown oid 'machdep.cpu.vendor'
sysctl: unknown oid 'machdep.cpu.model'
sysctl: unknown oid 'machdep.cpu.leaf7_features'
sysctl: unknown oid 'machdep.cpu.vendor'
sysctl: unknown oid 'machdep.cpu.model'
```

I also get this when I run `spack arch`

```
$ spack arch
sysctl: unknown oid 'machdep.cpu.leaf7_features'
sysctl: unknown oid 'machdep.cpu.vendor'
sysctl: unknown oid 'machdep.cpu.model'
darwin-bigsur-arm64
```

I'm running macOS 11.2.1, python3 version 3.9.1, with Apple's new M1 chip.


### Information on your system

```
$ spack debug report
* **Spack:** 0.16.1
* **Python:** 3.9.1
sysctl: unknown oid 'machdep.cpu.leaf7_features'
sysctl: unknown oid 'machdep.cpu.vendor'
sysctl: unknown oid 'machdep.cpu.model'
* **Platform:** darwin-bigsur-arm64
* **Concretizer:** original
```

### Additional information

- [X] I have run `spack debug report` and reported the version of Spack/Python/Platform
- [X] I have searched the issues of this repo and believe this is not a duplicate
- [X] I have run the failing commands in debug mode and reported the output

I think this is related to the spack arch / microarchitecture detection stuff. I'm happy to help test/develop arch stuff with this new Mac core. I thought there was a separate spin-off repo for spack arch, but I can't find it. 


Discussion:
Thanks @gonsie The separate report is here https://github.com/archspec/archspec/issues/38 I was waiting to have access to some M1 CPU to better test the support for myself and don't code it "blind", but maybe it's time to tackle the issue. I can either:
- Try to code the support for M1 based on the sysctl dump in the issue linked and bother you for testing it
- Help you develop the support for it and submit a PR for archspec

Let me know if one of the offer above seems acceptable :slightly_smiling_face: 
Thank you for the archspec link!

I took a look, and maybe you can try to code it up. I am happy to test. I'm also in the spack slack (@gonsie) if you want to work with me in real time.

To get started, here is what sysctl does have:

```
$ sysctl -a | grep machdep.cpu
machdep.cpu.cores_per_package: 8
machdep.cpu.core_count: 8
machdep.cpu.logical_per_package: 8
machdep.cpu.thread_count: 8
machdep.cpu.brand_string: Apple M1
```

The query `sysctl -n machdep.cpu.features` is valid, but returns an empty string. The queries for `machdep.cpu.leaf7_features`, `machdep.cpu.vendor`, and `machdep.cpu.model` are invalid.

@certik @gonsie I'll try to finish https://github.com/archspec/archspec/pull/46 at some point next week
I tested archspec/archspec#46 and archspec/archspec-json#24 and it works. :)

    $ git clone https://github.com/spack/spack.git
    $ export PATH="spack/bin:$PATH"
    $ git clone https://github.com/archspec/archspec.git
    $ cd archspec
    $ git checkout features/apple_m1
    $ cd
    $ cp archspec/archspec/cpu/detect.py spack/lib/spack/external/archspec/cpu/detect.py
    $ git clone https://github.com/archspec/archspec-json.git
    $ cp archspec-json/cpu/microarchitectures.json spack/lib/spack/external/archspec/json/cpu/microarchitectures.json
    $ spack arch
    darwin-bigsur-arm64
    $ spack compilers
    ==> Available compilers
    -- apple-clang bigsur-arm64 -------------------------------------
    apple-clang@12.0.0


Resolving patch:
diff --git a/lib/spack/external/__init__.py b/lib/spack/external/__init__.py
--- a/lib/spack/external/__init__.py
+++ b/lib/spack/external/__init__.py
@@ -11,7 +11,7 @@
 
 * Homepage: https://pypi.python.org/pypi/archspec
 * Usage: Labeling, comparison and detection of microarchitectures
-* Version: 0.1.2 (commit 0389e83e87d3dc5043a7ac08172bd970706524d6)
+* Version: 0.1.2 (commit a15485ab632478c15fadc65a00cd88b75ef7528a)
 
 argparse
 --------
diff --git a/lib/spack/external/archspec/cpu/detect.py b/lib/spack/external/archspec/cpu/detect.py
--- a/lib/spack/external/archspec/cpu/detect.py
+++ b/lib/spack/external/archspec/cpu/detect.py
@@ -99,17 +99,29 @@ def sysctl_info_dict():
     def sysctl(*args):
         return _check_output(["sysctl"] + list(args), env=child_environment).strip()
 
-    flags = (
-        sysctl("-n", "machdep.cpu.features").lower()
-        + " "
-        + sysctl("-n", "machdep.cpu.leaf7_features").lower()
-    )
-    info = {
-        "vendor_id": sysctl("-n", "machdep.cpu.vendor"),
-        "flags": flags,
-        "model": sysctl("-n", "machdep.cpu.model"),
-        "model name": sysctl("-n", "machdep.cpu.brand_string"),
-    }
+    if platform.machine() == "x86_64":
+        flags = (
+            sysctl("-n", "machdep.cpu.features").lower()
+            + " "
+            + sysctl("-n", "machdep.cpu.leaf7_features").lower()
+        )
+        info = {
+            "vendor_id": sysctl("-n", "machdep.cpu.vendor"),
+            "flags": flags,
+            "model": sysctl("-n", "machdep.cpu.model"),
+            "model name": sysctl("-n", "machdep.cpu.brand_string"),
+        }
+    else:
+        model = (
+            "m1" if "Apple" in sysctl("-n", "machdep.cpu.brand_string") else "unknown"
+        )
+        info = {
+            "vendor_id": "Apple",
+            "flags": [],
+            "model": model,
+            "CPU implementer": "Apple",
+            "model name": sysctl("-n", "machdep.cpu.brand_string"),
+        }
     return info
 
 


Test patch: