Skip to content

Commit b362203

Browse files
authored
Add cpuinfo (#5004)
* Add cpuinfo * fix lint * minor fix --------- Co-authored-by: Alexis Placet <2400067+Alex-PLACET@users.noreply.github.com>
1 parent e830a32 commit b362203

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -e
3+
4+
emcmake cmake . \
5+
-DCMAKE_BUILD_TYPE=Release \
6+
-DCMAKE_INSTALL_PREFIX=$PREFIX \
7+
-DCPUINFO_LIBRARY_TYPE=static \
8+
-DCPUINFO_BUILD_TOOLS=OFF \
9+
-DCPUINFO_BUILD_UNIT_TESTS=OFF \
10+
-DCPUINFO_BUILD_MOCK_TESTS=OFF \
11+
-DCPUINFO_BUILD_BENCHMARKS=OFF \
12+
-DCPUINFO_BUILD_PKG_CONFIG=OFF \
13+
-Bcmake-out-wasm
14+
15+
cd cmake-out-wasm
16+
17+
emmake cmake --build . --target install
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -e
3+
4+
emcmake cmake -S tests -B build_tests \
5+
-DCMAKE_BUILD_TYPE=Release \
6+
-DCMAKE_PREFIX_PATH=$PREFIX \
7+
-DCMAKE_FIND_ROOT_PATH=$PREFIX \
8+
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \
9+
-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH
10+
11+
emmake make -C build_tests
12+
13+
node build_tests/test_cpuinfo.js
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
context:
2+
name: cpuinfo
3+
version: "7364b490b5f78d58efe23ea76e74210fd6c3c76f"
4+
5+
package:
6+
name: ${{ name }}
7+
version: ${{ version }}
8+
9+
source:
10+
- url: https://github.com/pytorch/cpuinfo/archive/${{ version }}.tar.gz
11+
sha256: 67093420590f97c84d0c38cc020f134579494089d06de387bb48c7455eb5b8af
12+
13+
build:
14+
number: 0
15+
script: build.sh
16+
17+
requirements:
18+
build:
19+
- ${{ compiler('c') }}
20+
- ${{ compiler('cxx') }}
21+
- cmake
22+
- ninja
23+
- python
24+
25+
tests:
26+
- package_contents:
27+
include:
28+
- cpuinfo.h
29+
lib:
30+
- libcpuinfo.a
31+
files:
32+
- share/cpuinfo/cpuinfo-config.cmake
33+
- script:
34+
- build_tests.sh
35+
requirements:
36+
build:
37+
- ${{ compiler("c") }}
38+
- cmake
39+
- ninja
40+
files:
41+
recipe:
42+
- build_tests.sh
43+
- tests/
44+
45+
about:
46+
homepage: https://github.com/pytorch/cpuinfo
47+
license: BSD-2-Clause
48+
license_file: LICENSE
49+
summary: CPU INFOrmation library for aarch64, x86, and WebAssembly
50+
description: |
51+
cpuinfo is a library to detect essential for performance optimization
52+
information about host CPU. It includes detection of CPU topology,
53+
supported instruction sets, cache sizes, and other properties.
54+
It has native Emscripten/WebAssembly support.
55+
repository: https://github.com/pytorch/cpuinfo
56+
57+
extra:
58+
recipe-maintainers:
59+
- Alex-PLACET
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(cpuinfo_tests)
3+
4+
set(CMAKE_C_STANDARD 11)
5+
6+
find_package(cpuinfo REQUIRED CONFIG)
7+
8+
add_executable(test_cpuinfo test_cpuinfo.c)
9+
target_link_libraries(test_cpuinfo PRIVATE cpuinfo::cpuinfo)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <cpuinfo.h>
2+
#include <stdio.h>
3+
#include <assert.h>
4+
5+
int main(void) {
6+
/* Initialize cpuinfo */
7+
bool ok = cpuinfo_initialize();
8+
assert(ok && "cpuinfo_initialize() failed");
9+
printf("cpuinfo initialized successfully\n");
10+
11+
/* Processor / core / package counts must be >= 1 */
12+
uint32_t processors = cpuinfo_get_processors_count();
13+
uint32_t cores = cpuinfo_get_cores_count();
14+
uint32_t packages = cpuinfo_get_packages_count();
15+
assert(processors >= 1 && "processors count must be >= 1");
16+
assert(cores >= 1 && "cores count must be >= 1");
17+
assert(packages >= 1 && "packages count must be >= 1");
18+
printf("Processors: %u\n", processors);
19+
printf("Cores: %u\n", cores);
20+
printf("Packages: %u\n", packages);
21+
22+
/* Cache info - just check it doesn't crash */
23+
uint32_t l1i = cpuinfo_get_l1i_caches_count();
24+
uint32_t l1d = cpuinfo_get_l1d_caches_count();
25+
uint32_t l2 = cpuinfo_get_l2_caches_count();
26+
printf("L1i caches: %u\n", l1i);
27+
printf("L1d caches: %u\n", l1d);
28+
printf("L2 caches: %u\n", l2);
29+
30+
/* Max cache size must be > 0 when any cache is present */
31+
if (l1i > 0 || l1d > 0 || l2 > 0) {
32+
uint32_t max_cache = cpuinfo_get_max_cache_size();
33+
assert(max_cache > 0 && "max_cache_size must be > 0 when caches exist");
34+
printf("Max cache size: %u bytes\n", max_cache);
35+
}
36+
37+
cpuinfo_deinitialize();
38+
printf("All cpuinfo tests passed!\n");
39+
return 0;
40+
}

0 commit comments

Comments
 (0)