diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-06-15 21:44:46 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-06-15 21:44:46 +0000 |
commit | 611f3f1981d0a77719d9e710086f3641336af4d4 (patch) | |
tree | 384e51cab6b1186d6bfada666e744f0884b51d82 | |
parent | 71e3f6c6a7675b1f5d6b8fa84937ea96037c417a (diff) | |
parent | 80e00bf59e594ac253e736536f3dcbdc6d526dde (diff) | |
download | num_cpus-aml_tz3_312410010.tar.gz |
Snap for 8730993 from 80e00bf59e594ac253e736536f3dcbdc6d526dde to mainline-tzdata3-releaseaml_tz3_314012070aml_tz3_314012050aml_tz3_314012010aml_tz3_313110000aml_tz3_312511020aml_tz3_312511010aml_tz3_312410020aml_tz3_312410010android12-mainline-tzdata3-releaseaml_tz3_314012010
Change-Id: I5be02a99018c7ecc834e8a1d5ac6bdc900afad67
-rw-r--r-- | .appveyor.yml | 16 | ||||
-rw-r--r-- | .cargo_vcs_info.json | 7 | ||||
-rw-r--r-- | .github/workflows/ci.yml | 166 | ||||
-rw-r--r-- | .travis.yml | 81 | ||||
-rw-r--r-- | Android.bp | 24 | ||||
-rw-r--r-- | CHANGELOG.md | 6 | ||||
-rw-r--r-- | Cargo.lock | 16 | ||||
-rw-r--r-- | Cargo.toml | 17 | ||||
-rw-r--r-- | Cargo.toml.orig | 6 | ||||
-rw-r--r-- | METADATA | 14 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | TEST_MAPPING | 78 | ||||
-rw-r--r-- | cargo2android.json | 13 | ||||
-rw-r--r-- | ci/cgroups/Dockerfile | 2 | ||||
-rw-r--r-- | fixtures/cgroups/proc/cgroups/mountinfo_multi_opt | 8 | ||||
-rw-r--r-- | fixtures/cgroups/proc/cgroups/mountinfo_zero_opt | 8 | ||||
-rw-r--r-- | patches/Android.bp.patch | 12 | ||||
-rw-r--r-- | src/lib.rs | 5 | ||||
-rw-r--r-- | src/linux.rs | 104 |
19 files changed, 184 insertions, 401 deletions
diff --git a/.appveyor.yml b/.appveyor.yml new file mode 100644 index 0000000..e75d59f --- /dev/null +++ b/.appveyor.yml @@ -0,0 +1,16 @@ +environment: + matrix: + - TARGET: x86_64-pc-windows-msvc + - TARGET: i686-pc-windows-msvc + - TARGET: x86_64-pc-windows-gnu + - TARGET: i686-pc-windows-gnu +install: + - curl -sSf -o rustup-init.exe https://win.rustup.rs/ + - rustup-init.exe -y --default-host %TARGET% + - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin + - rustc -vV + - cargo -vV +build: false +test_script: + - cargo build --verbose + - cargo test --verbose diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 45e1a87..6fcb99c 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,6 +1,5 @@ { "git": { - "sha1": "5f1b03332000b4c4274b5bd35fac516049ff1c6b" - }, - "path_in_vcs": "" -}
\ No newline at end of file + "sha1": "b347731fc0c008dbb88b406c595b1e644458fffe" + } +} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index f9adc30..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,166 +0,0 @@ -name: CI -on: - pull_request: - push: - branches: - - master - -env: - RUST_BACKTRACE: 1 - CARGO_TERM_COLOR: always - -jobs: - ci-pass: - name: CI is green - runs-on: ubuntu-latest - needs: - - msrv - - test-linux - - test-macos - - test-windows - - build-cross - - test-cgroups - steps: - - run: exit 0 - - msrv: - runs-on: ubuntu-latest - - strategy: - matrix: - rust: - - 1.13 - target: - - x86_64-unknown-linux-gnu - - steps: - - uses: actions/checkout@v2 - - name: Run tests - run: | - rustup default ${{ matrix.rust }} - rustup target add ${{ matrix.target }} - cargo test --verbose --target ${{ matrix.target }} - - test-linux: - runs-on: ubuntu-latest - - strategy: - matrix: - rust: - - stable - - beta - - nightly - target: - - x86_64-unknown-linux-gnu - - x86_64-unknown-linux-musl - - steps: - - uses: actions/checkout@v2 - - name: Run tests - run: | - rustup default ${{ matrix.rust }} - rustup target add ${{ matrix.target }} - cargo test --verbose --target ${{ matrix.target }} - - test-macos: - runs-on: macos-latest - - strategy: - matrix: - rust: - - stable - - beta - - nightly - target: - - x86_64-apple-darwin - - steps: - - uses: actions/checkout@v2 - - name: Run tests - run: | - rustup default ${{ matrix.rust }} - rustup target add ${{ matrix.target }} - cargo test --verbose --target ${{ matrix.target }} - - test-windows: - runs-on: windows-latest - - strategy: - matrix: - rust: - - stable - - beta - - nightly - target: - - x86_64-pc-windows-gnu - - x86_64-pc-windows-msvc - - steps: - - uses: actions/checkout@v2 - - name: Run tests - run: | - rustup default ${{ matrix.rust }} - rustup target add ${{ matrix.target }} - cargo test --verbose --target ${{ matrix.target }} - - build-cross: - runs-on: ubuntu-latest - - strategy: - matrix: - rust: - - stable - target: - - aarch64-unknown-linux-gnu - - i686-pc-windows-gnu - - i686-pc-windows-msvc - - i686-unknown-linux-gnu - - aarch64-apple-darwin - - aarch64-pc-windows-msvc - - aarch64-unknown-linux-musl - - arm-unknown-linux-gnueabi - - arm-unknown-linux-gnueabihf - - armv7-unknown-linux-gnueabihf - - mips-unknown-linux-gnu - - mips64-unknown-linux-gnuabi64 - - mips64el-unknown-linux-gnuabi64 - - mipsel-unknown-linux-gnu - - powerpc-unknown-linux-gnu - - powerpc64-unknown-linux-gnu - - powerpc64le-unknown-linux-gnu - - riscv64gc-unknown-linux-gnu - - s390x-unknown-linux-gnu - - x86_64-unknown-freebsd - - x86_64-unknown-illumos - - x86_64-unknown-netbsd - - i686-linux-android - - x86_64-linux-android - - arm-linux-androideabi - - aarch64-linux-android - - x86_64-apple-ios - - asmjs-unknown-emscripten - - aarch64-apple-ios - - aarch64-apple-ios-sim - - aarch64-apple-darwin - steps: - - uses: actions/checkout@v2 - - name: Run build - run: | - rustup default ${{ matrix.rust }} - rustup target add ${{ matrix.target }} - cargo build --verbose --target ${{ matrix.target }} - - test-cgroups: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: Test Cgroup - run: | - docker build -f ci/cgroups/Dockerfile -t num-cpus-cgroups . - # Test without cgroups - docker run -e NUM_CPUS_TEST_GET=2 num-cpus-cgroups - # Only 1 CPU - docker run --cpus="1" -e NUM_CPUS_TEST_GET=1 num-cpus-cgroups - # 1.5 CPUs - docker run --cpus="1.5" -e NUM_CPUS_TEST_GET=2 num-cpus-cgroups diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..cec6ddb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,81 @@ +language: rust +rust: stable +dist: xenial +sudo: false +notifications: + email: disabled + +cache: cargo + +install: + - if [ -z "$NO_ADD" ]; then rustup target add "$TARGET"; fi + +script: + - cargo build --verbose --target "$TARGET" + # Only run the targets if we are on the same OS. + - if [ "$RUN" == "1" ]; then cargo test --verbose --target "$TARGET"; fi + +env: + global: + # Travis CI servers make 2 cores available to processes via virtualization. + # See https://docs.travis-ci.com/user/reference/overview/ + - NUM_CPUS_TEST_GET=2 + +matrix: + include: + # Linux + # Minimum Supported Rust Version + - name: Minimum Supported Rust Version + env: TARGET=x86_64-unknown-linux-gnu RUN=1 NO_ADD=1 + rust: 1.13.0 + # 32-bit requires multilib. + - env: TARGET=i686-unknown-linux-gnu RUN=1 + addons: + apt: + packages: + - gcc-multilib + # 64-bit stable. + - env: TARGET=x86_64-unknown-linux-gnu RUN=1 NO_ADD=1 + rust: stable + # 64-bit beta. + - env: TARGET=x86_64-unknown-linux-gnu RUN=1 NO_ADD=1 + rust: beta + # 64-bit nightly. + - env: TARGET=x86_64-unknown-linux-gnu RUN=1 NO_ADD=1 + rust: nightly + + # Mac OS X + - os: osx + env: TARGET=x86_64-apple-darwin RUN=1 NO_ADD=1 + # Android + # - for x86 + - env: TARGET=i686-linux-android + - env: TARGET=x86_64-linux-android + # - for ARM + - env: TARGET=arm-linux-androideabi + - env: TARGET=aarch64-linux-android + # IOS + - os: osx + osx_image: xcode9 + env: TARGET=x86_64-apple-ios + # FreeBSD + - env: TARGET=x86_64-unknown-freebsd + # Fuchsia + # FIXME: figure out why target is missing and re-enable + #- env: TARGET=x86_64-unknown-fuchsia + # NetBSD + - env: TARGET=x86_64-unknown-netbsd + # Emscripten + - env: TARGET=asmjs-unknown-emscripten + + # CGroups in Docker + - name: Docker CGroups + install: + script: + - docker build -f ci/cgroups/Dockerfile -t num-cpus-cgroups . + # Test without cgroups + - docker run -it -e NUM_CPUS_TEST_GET=2 num-cpus-cgroups + # Only 1 CPU + - docker run -it --cpus="1" -e NUM_CPUS_TEST_GET=1 num-cpus-cgroups + # 1.5 CPUs + - docker run -it --cpus="1.5" -e NUM_CPUS_TEST_GET=2 num-cpus-cgroups @@ -41,8 +41,6 @@ rust_library { name: "libnum_cpus", host_supported: true, crate_name: "num_cpus", - cargo_env_compat: true, - cargo_pkg_version: "1.13.1", srcs: ["src/lib.rs"], edition: "2015", rustlibs: [ @@ -50,29 +48,11 @@ rust_library { ], apex_available: [ "//apex_available:platform", - "com.android.bluetooth", - "com.android.compos", "com.android.resolv", - "com.android.uwb", "com.android.virt", ], - vendor_available: true, min_sdk_version: "29", } -rust_test { - name: "num_cpus_test_src_lib", - crate_name: "num_cpus", - cargo_env_compat: true, - cargo_pkg_version: "1.13.1", - srcs: ["src/lib.rs"], - test_suites: ["general-tests"], - auto_gen_config: true, - test_options: { - unit_test: false, - }, - edition: "2015", - rustlibs: [ - "liblibc", - ], -} +// dependent_library ["feature_list"] +// libc-0.2.94 "default,std" diff --git a/CHANGELOG.md b/CHANGELOG.md index 5496ace..0caafb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,3 @@ -## v1.13.1 - -### Fixes - -- fix parsing zero or multiple optional fields in cgroup mountinfo. - ## v1.13.0 ### Features @@ -1,26 +1,26 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 - [[package]] name = "hermit-abi" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "307c3c9f937f38e3534b1d6447ecf090cafcc9744e4a6360e8b037b2cf5af120" dependencies = [ - "libc", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "libc" version = "0.2.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" [[package]] name = "num_cpus" -version = "1.13.1" +version = "1.13.0" dependencies = [ - "hermit-abi", - "libc", + "hermit-abi 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] + +[metadata] +"checksum hermit-abi 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "307c3c9f937f38e3534b1d6447ecf090cafcc9744e4a6360e8b037b2cf5af120" +"checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" @@ -3,24 +3,25 @@ # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies -# to registry (e.g., crates.io) dependencies. +# to registry (e.g., crates.io) dependencies # -# If you are reading this file be aware that the original Cargo.toml -# will likely look very different (and much more reasonable). -# See Cargo.toml.orig for the original contents. +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) [package] name = "num_cpus" -version = "1.13.1" +version = "1.13.0" authors = ["Sean McArthur <sean@seanmonstar.com>"] description = "Get the number of CPUs on a machine." documentation = "https://docs.rs/num_cpus" readme = "README.md" keywords = ["cpu", "cpus", "cores"] categories = ["hardware-support"] -license = "MIT OR Apache-2.0" +license = "MIT/Apache-2.0" repository = "https://github.com/seanmonstar/num_cpus" +[dependencies.libc] +version = "0.2.26" [target."cfg(all(any(target_arch = \"x86_64\", target_arch = \"aarch64\"), target_os = \"hermit\"))".dependencies.hermit-abi] version = "0.1.3" -[target."cfg(not(windows))".dependencies.libc] -version = "0.2.26" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index a6cbbd2..a6e9434 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,17 +1,17 @@ [package] name = "num_cpus" -version = "1.13.1" +version = "1.13.0" # don't forget to update html_root_url description = "Get the number of CPUs on a machine." authors = ["Sean McArthur <sean@seanmonstar.com>"] -license = "MIT OR Apache-2.0" +license = "MIT/Apache-2.0" repository = "https://github.com/seanmonstar/num_cpus" documentation = "https://docs.rs/num_cpus" keywords = ["cpu", "cpus", "cores"] categories = ["hardware-support"] readme = "README.md" -[target.'cfg(not(windows))'.dependencies] +[dependencies] libc = "0.2.26" [target.'cfg(all(any(target_arch = "x86_64", target_arch = "aarch64"), target_os = "hermit"))'.dependencies] @@ -1,5 +1,7 @@ name: "num_cpus" -description: "Get the number of CPUs on a machine." +description: + "Get the number of CPUs on a machine." + third_party { url { type: HOMEPAGE @@ -7,13 +9,9 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/num_cpus/num_cpus-1.13.1.crate" + value: "https://static.crates.io/crates/num_cpus/num_cpus-1.13.0.crate" } - version: "1.13.1" + version: "1.13.0" + last_upgrade_date { year: 2020 month: 10 day: 15 } license_type: NOTICE - last_upgrade_date { - year: 2022 - month: 3 - day: 1 - } } @@ -1,6 +1,6 @@ # num_cpus -[![crates.io](https://img.shields.io/crates/v/num_cpus.svg)](https://crates.io/crates/num_cpus) +[![crates.io](http://meritbadge.herokuapp.com/num_cpus)](https://crates.io/crates/num_cpus) [![Travis CI Status](https://travis-ci.org/seanmonstar/num_cpus.svg?branch=master)](https://travis-ci.org/seanmonstar/num_cpus) [![AppVeyor status](https://ci.appveyor.com/api/projects/status/qn8t6grhko5jwno6?svg=true)](https://ci.appveyor.com/project/seanmonstar/num-cpus) diff --git a/TEST_MAPPING b/TEST_MAPPING deleted file mode 100644 index 45ba394..0000000 --- a/TEST_MAPPING +++ /dev/null @@ -1,78 +0,0 @@ -// Generated by update_crate_tests.py for tests that depend on this crate. -{ - "imports": [ - { - "path": "external/rust/crates/anyhow" - }, - { - "path": "external/rust/crates/base64" - }, - { - "path": "external/rust/crates/futures-util" - }, - { - "path": "external/rust/crates/ryu" - }, - { - "path": "external/rust/crates/tinytemplate" - }, - { - "path": "external/rust/crates/tinyvec" - }, - { - "path": "external/rust/crates/tokio" - }, - { - "path": "external/rust/crates/tokio-test" - }, - { - "path": "external/rust/crates/unicode-xid" - } - ], - "presubmit": [ - { - "name": "ZipFuseTest" - }, - { - "name": "authfs_device_test_src_lib" - }, - { - "name": "doh_unit_test" - }, - { - "name": "keystore2_selinux_concurrency_test" - }, - { - "name": "num_cpus_test_src_lib" - }, - { - "name": "rustBinderTest" - }, - { - "name": "virtualizationservice_device_test" - } - ], - "presubmit-rust": [ - { - "name": "ZipFuseTest" - }, - { - "name": "authfs_device_test_src_lib" - }, - { - "name": "doh_unit_test" - }, - { - "name": "keystore2_selinux_concurrency_test" - }, - { - "name": "num_cpus_test_src_lib" - }, - { - "name": "rustBinderTest" - }, - { - "name": "virtualizationservice_device_test" - } - ] -} diff --git a/cargo2android.json b/cargo2android.json index 5dce664..01465d0 100644 --- a/cargo2android.json +++ b/cargo2android.json @@ -1,18 +1,11 @@ { "apex-available": [ "//apex_available:platform", - "com.android.bluetooth", - "com.android.compos", "com.android.resolv", - "com.android.uwb", "com.android.virt" ], + "min_sdk_version": "29", "dependencies": true, "device": true, - "min-sdk-version": "29", - "no-presubmit": true, - "patch": "patches/Android.bp.patch", - "run": true, - "tests": true, - "vendor-available": true -} + "run": true +}
\ No newline at end of file diff --git a/ci/cgroups/Dockerfile b/ci/cgroups/Dockerfile index 7636a47..d5f288a 100644 --- a/ci/cgroups/Dockerfile +++ b/ci/cgroups/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:latest +FROM rust:1.40 WORKDIR /usr/num_cpus diff --git a/fixtures/cgroups/proc/cgroups/mountinfo_multi_opt b/fixtures/cgroups/proc/cgroups/mountinfo_multi_opt deleted file mode 100644 index e03a95b..0000000 --- a/fixtures/cgroups/proc/cgroups/mountinfo_multi_opt +++ /dev/null @@ -1,8 +0,0 @@ -1 0 8:1 / / rw,noatime shared:1 - ext4 /dev/sda1 rw,errors=remount-ro,data=reordered -2 1 0:1 / /dev rw,relatime shared:2 - devtmpfs udev rw,size=10240k,nr_inodes=16487629,mode=755 -3 1 0:2 / /proc rw,nosuid,nodev,noexec,relatime shared:3 - proc proc rw -4 1 0:3 / /sys rw,nosuid,nodev,noexec,relatime shared:4 - sysfs sysfs rw -5 4 0:4 / /sys/fs/cgroup ro,nosuid,nodev,noexec shared:5 - tmpfs tmpfs ro,mode=755 -6 5 0:5 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:6 - cgroup cgroup rw,cpuset -7 5 0:6 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:7 shared:8 shared:9 - cgroup cgroup rw,cpu,cpuacct -8 5 0:7 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:8 - cgroup cgroup rw,memory diff --git a/fixtures/cgroups/proc/cgroups/mountinfo_zero_opt b/fixtures/cgroups/proc/cgroups/mountinfo_zero_opt deleted file mode 100644 index ad27a96..0000000 --- a/fixtures/cgroups/proc/cgroups/mountinfo_zero_opt +++ /dev/null @@ -1,8 +0,0 @@ -1 0 8:1 / / rw,noatime shared:1 - ext4 /dev/sda1 rw,errors=remount-ro,data=reordered -2 1 0:1 / /dev rw,relatime shared:2 - devtmpfs udev rw,size=10240k,nr_inodes=16487629,mode=755 -3 1 0:2 / /proc rw,nosuid,nodev,noexec,relatime shared:3 - proc proc rw -4 1 0:3 / /sys rw,nosuid,nodev,noexec,relatime shared:4 - sysfs sysfs rw -5 4 0:4 / /sys/fs/cgroup ro,nosuid,nodev,noexec shared:5 - tmpfs tmpfs ro,mode=755 -6 5 0:5 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:6 - cgroup cgroup rw,cpuset -7 5 0:6 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,cpu,cpuacct -8 5 0:7 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:8 - cgroup cgroup rw,memory diff --git a/patches/Android.bp.patch b/patches/Android.bp.patch deleted file mode 100644 index 04bbbda..0000000 --- a/patches/Android.bp.patch +++ /dev/null @@ -1,12 +0,0 @@ -diff --git a/Android.bp b/Android.bp -index a441683..8f89a16 100644 ---- a/Android.bp -+++ b/Android.bp -@@ -59,7 +59,6 @@ rust_library { - - rust_test { - name: "num_cpus_test_src_lib", -- host_supported: true, - crate_name: "num_cpus", - cargo_env_compat: true, - cargo_pkg_version: "1.13.0", @@ -28,6 +28,7 @@ //! [`rayon::ThreadPool`]: https://docs.rs/rayon/1.*/rayon/struct.ThreadPool.html #![cfg_attr(test, deny(warnings))] #![deny(missing_docs)] +#![doc(html_root_url = "https://docs.rs/num_cpus/1.13.0")] #![allow(non_snake_case)] #[cfg(not(windows))] @@ -46,8 +47,6 @@ use linux::{get_num_cpus, get_num_physical_cpus}; /// This function will get the number of logical cores. Sometimes this is different from the number /// of physical cores (See [Simultaneous multithreading on Wikipedia][smt]). /// -/// This will always return at least `1`. -/// /// # Examples /// /// ``` @@ -76,8 +75,6 @@ pub fn get() -> usize { /// Returns the number of physical cores of the current system. /// -/// This will always return at least `1`. -/// /// # Note /// /// Physical count is supported only on Linux, mac OS and Windows platforms. diff --git a/src/linux.rs b/src/linux.rs index 36f4727..671a943 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -18,7 +18,7 @@ macro_rules! debug { } macro_rules! some { - ($e:expr) => {{ + ($e:expr) => ({ match $e { Some(v) => v, None => { @@ -26,7 +26,7 @@ macro_rules! some { return None; } } - }}; + }) } pub fn get_num_cpus() -> usize { @@ -126,15 +126,18 @@ fn init_cgroups() { // Should only be called once debug_assert!(CGROUPS_CPUS.load(Ordering::SeqCst) == 0); - if let Some(quota) = load_cgroups("/proc/self/cgroup", "/proc/self/mountinfo") { - if quota == 0 { - return; - } + match load_cgroups("/proc/self/cgroup", "/proc/self/mountinfo") { + Some(quota) => { + if quota == 0 { + return; + } - let logical = logical_cpus(); - let count = ::std::cmp::min(quota, logical); + let logical = logical_cpus(); + let count = ::std::cmp::min(quota, logical); - CGROUPS_CPUS.store(count, Ordering::SeqCst); + CGROUPS_CPUS.store(count, Ordering::SeqCst); + } + None => return, } } @@ -164,14 +167,18 @@ struct Subsys { impl Cgroup { fn new(dir: PathBuf) -> Cgroup { - Cgroup { base: dir } + Cgroup { + base: dir, + } } fn translate(mntinfo: MountInfo, subsys: Subsys) -> Option<Cgroup> { // Translate the subsystem directory via the host paths. debug!( "subsys = {:?}; root = {:?}; mount_point = {:?}", - subsys.base, mntinfo.root, mntinfo.mount_point + subsys.base, + mntinfo.root, + mntinfo.mount_point ); let rel_from_root = some!(Path::new(&subsys.base).strip_prefix(&mntinfo.root).ok()); @@ -231,27 +238,13 @@ impl MountInfo { fn parse_line(line: String) -> Option<MountInfo> { let mut fields = line.split(' '); - // 7 5 0:6 </> /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:7 - cgroup cgroup rw,cpu,cpuacct let mnt_root = some!(fields.nth(3)); - // 7 5 0:6 / </sys/fs/cgroup/cpu,cpuacct> rw,nosuid,nodev,noexec,relatime shared:7 - cgroup cgroup rw,cpu,cpuacct - let mnt_point = some!(fields.next()); - - // Ignore all fields until the separator(-). - // Note: there could be zero or more optional fields before hyphen. - // See: https://man7.org/linux/man-pages/man5/proc.5.html - // 7 5 0:6 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:7 <-> cgroup cgroup rw,cpu,cpuacct - // Note: we cannot use `?` here because we need to support Rust 1.13. - match fields.find(|&s| s == "-") { - Some(_) => {} - None => return None, - }; + let mnt_point = some!(fields.nth(0)); - // 7 5 0:6 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:7 - <cgroup> cgroup rw,cpu,cpuacct - if fields.next() != Some("cgroup") { + if fields.nth(3) != Some("cgroup") { return None; } - // 7 5 0:6 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:7 - cgroup cgroup <rw,cpu,cpuacct> let super_opts = some!(fields.nth(1)); // We only care about the 'cpu' option @@ -288,18 +281,16 @@ impl Subsys { return None; } - fields.next().map(|path| Subsys { - base: path.to_owned(), - }) + fields.next().map(|path| Subsys { base: path.to_owned() }) } } #[cfg(test)] mod tests { - use super::{Cgroup, MountInfo, Subsys}; use std::path::{Path, PathBuf}; + use super::{Cgroup, MountInfo, Subsys}; + - // `static_in_const` feature is not stable in Rust 1.13. static FIXTURES_PROC: &'static str = "fixtures/cgroups/proc/cgroups"; static FIXTURES_CGROUPS: &'static str = "fixtures/cgroups/cgroups"; @@ -313,29 +304,12 @@ mod tests { #[test] fn test_load_mountinfo() { - // test only one optional fields let path = join!(FIXTURES_PROC, "mountinfo"); let mnt_info = MountInfo::load_cpu(path).unwrap(); assert_eq!(mnt_info.root, "/"); assert_eq!(mnt_info.mount_point, "/sys/fs/cgroup/cpu,cpuacct"); - - // test zero optional field - let path = join!(FIXTURES_PROC, "mountinfo_zero_opt"); - - let mnt_info = MountInfo::load_cpu(path).unwrap(); - - assert_eq!(mnt_info.root, "/"); - assert_eq!(mnt_info.mount_point, "/sys/fs/cgroup/cpu,cpuacct"); - - // test multi optional fields - let path = join!(FIXTURES_PROC, "mountinfo_multi_opt"); - - let mnt_info = MountInfo::load_cpu(path).unwrap(); - - assert_eq!(mnt_info.root, "/"); - assert_eq!(mnt_info.mount_point, "/sys/fs/cgroup/cpu,cpuacct"); } #[test] @@ -350,7 +324,12 @@ mod tests { #[test] fn test_cgroup_mount() { let cases = &[ - ("/", "/sys/fs/cgroup/cpu", "/", Some("/sys/fs/cgroup/cpu")), + ( + "/", + "/sys/fs/cgroup/cpu", + "/", + Some("/sys/fs/cgroup/cpu"), + ), ( "/docker/01abcd", "/sys/fs/cgroup/cpu", @@ -369,10 +348,27 @@ mod tests { "/docker/01abcd/large", Some("/sys/fs/cgroup/cpu/large"), ), + // fails - ("/docker/01abcd", "/sys/fs/cgroup/cpu", "/", None), - ("/docker/01abcd", "/sys/fs/cgroup/cpu", "/docker", None), - ("/docker/01abcd", "/sys/fs/cgroup/cpu", "/elsewhere", None), + + ( + "/docker/01abcd", + "/sys/fs/cgroup/cpu", + "/", + None, + ), + ( + "/docker/01abcd", + "/sys/fs/cgroup/cpu", + "/docker", + None, + ), + ( + "/docker/01abcd", + "/sys/fs/cgroup/cpu", + "/elsewhere", + None, + ), ( "/docker/01abcd", "/sys/fs/cgroup/cpu", @@ -391,7 +387,7 @@ mod tests { }; let actual = Cgroup::translate(mnt_info, subsys).map(|c| c.base); - let expected = expected.map(PathBuf::from); + let expected = expected.map(|s| PathBuf::from(s)); assert_eq!(actual, expected); } } |