diff options
-rw-r--r-- | .github/workflows/cmake-netbsd.yml | 45 | ||||
-rw-r--r-- | .github/workflows/continuous-build-freebsd.yml | 2 | ||||
-rw-r--r-- | .github/workflows/continuous-build-gpu.yml | 2 | ||||
-rw-r--r-- | .github/workflows/continuous-build-linux.yml | 6 | ||||
-rw-r--r-- | .github/workflows/continuous-build-macos.yml | 26 | ||||
-rw-r--r-- | .github/workflows/continuous-build-netbsd.yml | 65 | ||||
-rw-r--r-- | .github/workflows/continuous-build-openbsd.yml | 4 | ||||
-rw-r--r-- | .github/workflows/test-snap-can-build.yml | 12 | ||||
-rw-r--r-- | CMakeLists.txt | 9 | ||||
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | README.md | 168 | ||||
-rw-r--r-- | cmake/Modules/Findproplib.cmake | 23 | ||||
-rw-r--r-- | src/btop.cpp | 74 | ||||
-rw-r--r-- | src/btop_shared.hpp | 4 | ||||
-rw-r--r-- | src/netbsd/btop_collect.cpp | 1420 |
15 files changed, 1825 insertions, 41 deletions
diff --git a/.github/workflows/cmake-netbsd.yml b/.github/workflows/cmake-netbsd.yml new file mode 100644 index 0000000..c3ec8d5 --- /dev/null +++ b/.github/workflows/cmake-netbsd.yml @@ -0,0 +1,45 @@ +name: NetBSD CMake + +on: + push: + branches: main + tags-ignore: '*.*' + paths: + - '.github/workflows/cmake-netbsd.yml' + - 'CMakeLists.txt' + - 'include/**' + - 'src/*pp' + - 'src/netbsd/*pp' + pull_request: + branches: main + paths: + - '.github/workflows/cmake-netbsd.yml' + - 'CMakeLists.txt' + - 'include/**' + - 'src/*pp' + - 'src/netbsd/*pp' + +jobs: + cmake_build_on_netbsd: + runs-on: ubuntu-22.04 + concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + steps: + - uses: actions/checkout@v4 + + - name: Compile + uses: vmactions/netbsd-vm@v1 + with: + release: '10.0' + usesh: true + prepare: | + PATH="/usr/pkg/sbin:/usr/pkg/bin:$PATH" + PKG_PATH="https://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/amd64/10.0/All/" + export PATH PKG_PATH + /usr/sbin/pkg_add pkgin + pkgin -y install cmake ninja-build gcc10 coreutils git + git config --global --add safe.directory /home/runner/work/btop/btop + run: | + cmake -DCMAKE_CXX_COMPILER="/usr/pkg/gcc10/bin/g++" -B build -G Ninja -DBTOP_STATIC=OFF + cmake --build build --verbose diff --git a/.github/workflows/continuous-build-freebsd.yml b/.github/workflows/continuous-build-freebsd.yml index 5521ff7..3d4f74f 100644 --- a/.github/workflows/continuous-build-freebsd.yml +++ b/.github/workflows/continuous-build-freebsd.yml @@ -11,6 +11,7 @@ on: - 'src/**' - '!src/linux/**' - '!src/osx/**' + - '!src/netbsd/**' - '!src/openbsd/**' - 'include/**' - 'Makefile' @@ -22,6 +23,7 @@ on: - 'src/**' - '!src/linux/**' - '!src/osx/**' + - '!src/netbsd/**' - '!src/openbsd/**' - 'include/**' - 'Makefile' diff --git a/.github/workflows/continuous-build-gpu.yml b/.github/workflows/continuous-build-gpu.yml index e3bdf01..34c3e11 100644 --- a/.github/workflows/continuous-build-gpu.yml +++ b/.github/workflows/continuous-build-gpu.yml @@ -11,6 +11,7 @@ on: - 'src/**' - '!src/osx/**' - '!src/freebsd/**' + - '!src/netbsd/**' - '!src/openbsd/**' - 'include/**' - 'Makefile' @@ -22,6 +23,7 @@ on: - 'src/**' - '!src/osx/**' - '!src/freebsd/**' + - '!src/netbsd/**' - '!src/openbsd/**' - 'include/**' - 'Makefile' diff --git a/.github/workflows/continuous-build-linux.yml b/.github/workflows/continuous-build-linux.yml index 39de640..edcd130 100644 --- a/.github/workflows/continuous-build-linux.yml +++ b/.github/workflows/continuous-build-linux.yml @@ -11,6 +11,7 @@ on: - 'src/**' - '!src/osx/**' - '!src/freebsd/**' + - '!src/netbsd/**' - '!src/openbsd/**' - 'include/**' - 'Makefile' @@ -22,6 +23,7 @@ on: - 'src/**' - '!src/osx/**' - '!src/freebsd/**' + - '!src/netbsd/**' - '!src/openbsd/**' - 'include/**' - 'Makefile' @@ -95,7 +97,7 @@ jobs: run: git config --global --add safe.directory /__w/btop/btop - name: Checkout source - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: submodules: recursive @@ -121,7 +123,7 @@ jobs: cp bin/btop .artifacts/$FILENAME - name: Upload artifacts - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: btop-${{ matrix.toolchain }} path: '.artifacts/**' diff --git a/.github/workflows/continuous-build-macos.yml b/.github/workflows/continuous-build-macos.yml index 2ea27ce..5f78b93 100644 --- a/.github/workflows/continuous-build-macos.yml +++ b/.github/workflows/continuous-build-macos.yml @@ -11,6 +11,7 @@ on: - 'src/**' - '!src/linux/**' - '!src/freebsd/**' + - '!src/netbsd/**' - '!src/openbsd/**' - 'include/**' - 'Makefile' @@ -22,6 +23,7 @@ on: - 'src/**' - '!src/linux/**' - '!src/freebsd/**' + - '!src/netbsd/**' - '!src/openbsd/**' - 'include/**' - 'Makefile' @@ -35,18 +37,24 @@ jobs: with: xcode-version: latest-stable - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: recursive + - name: Install build tools + run: | + export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 + brew update --quiet + brew install --force --overwrite llvm lowdown + - name: Compile run: | - make CXX=g++-12 ARCH=x86_64 STATIC=true STRIP=true + make CXX=$(brew --prefix llvm)/bin/clang++ ARCH=x86_64 STATIC=true STRIP=true GIT_HASH=$(git rev-parse --short "$GITHUB_SHA") mv bin/btop bin/btop-x86_64-Monterey-$GIT_HASH ls -alh bin - - uses: actions/upload-artifact@v3 + - uses: actions/upload-artifact@v4 with: name: btop-x86_64-macos12-Monterey path: 'bin/*' @@ -58,18 +66,24 @@ jobs: with: xcode-version: latest-stable - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: recursive + - name: Install build tools + run: | + export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 + brew update --quiet + brew install --force --overwrite llvm lowdown + - name: Compile run: | - make CXX=g++-12 ARCH=x86_64 STATIC=true STRIP=true + make CXX=$(brew --prefix llvm)/bin/clang++ ARCH=x86_64 STATIC=true STRIP=true GIT_HASH=$(git rev-parse --short "$GITHUB_SHA") mv bin/btop bin/btop-x86_64-Ventura-$GIT_HASH ls -alh bin - - uses: actions/upload-artifact@v3 + - uses: actions/upload-artifact@v4 with: name: btop-x86_64-macos13-Ventura path: 'bin/*' diff --git a/.github/workflows/continuous-build-netbsd.yml b/.github/workflows/continuous-build-netbsd.yml new file mode 100644 index 0000000..6bc2ad7 --- /dev/null +++ b/.github/workflows/continuous-build-netbsd.yml @@ -0,0 +1,65 @@ +name: Continuous Build NetBSD + +on: + workflow_dispatch: + push: + branches: + - main + tags-ignore: + - '*.*' + paths: + - 'src/**' + - '!src/linux/**' + - '!src/osx/**' + - '!src/freebsd/**' + - '!src/openbsd/**' + - 'include/**' + - 'Makefile' + - '.github/workflows/continuous-build-netbsd.yml' + pull_request: + branches: + - main + paths: + - 'src/**' + - '!src/linux/**' + - '!src/osx/**' + - '!src/freebsd/**' + - '!src/openbsd/**' + - 'include/**' + - 'Makefile' + - '.github/workflows/continuous-build-netbsd.yml' + +jobs: + build-netbsd: + runs-on: ubuntu-22.04 + timeout-minutes: 20 + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Compile + uses: vmactions/netbsd-vm@v1 + + with: + release: '10.0' + usesh: true + prepare: | + PATH="/usr/pkg/sbin:/usr/pkg/bin:$PATH" + PKG_PATH="https://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/amd64/10.0/All/" + export PATH PKG_PATH + /usr/sbin/pkg_add pkgin + pkgin -y install gmake gcc10 coreutils git + git config --global --add safe.directory /home/runner/work/btop/btop + run: | + gmake CXX=/usr/pkg/gcc10/bin/g++ CXXFLAGS='-DNDEBUG -I/usr/pkg/gcc10/include -I/usr/include -I/usr/pkg/include' STATIC=true STRIP=true + GIT_HASH=$(git rev-parse --short "$GITHUB_SHA") + mv bin/btop bin/btop-GCC10-"$GIT_HASH" + ls -alh bin + + - uses: actions/upload-artifact@v3 + with: + name: btop-x86_64-netbsd-9.3 + path: 'bin/*' + if-no-files-found: error + diff --git a/.github/workflows/continuous-build-openbsd.yml b/.github/workflows/continuous-build-openbsd.yml index 6925ebb..81fe4fe 100644 --- a/.github/workflows/continuous-build-openbsd.yml +++ b/.github/workflows/continuous-build-openbsd.yml @@ -12,6 +12,7 @@ on: - '!src/linux/**' - '!src/osx/**' - '!src/freebsd/**' + - '!src/netbsd/**' - 'include/**' - 'Makefile' - '.github/workflows/continuous-build-openbsd.yml' @@ -23,6 +24,7 @@ on: - '!src/linux/**' - '!src/osx/**' - '!src/freebsd/**' + - '!src/netbsd/**' - 'include/**' - 'Makefile' - '.github/workflows/continuous-build-openbsd.yml' @@ -50,7 +52,7 @@ jobs: mv bin/btop bin/btop-GCC11-"$GIT_HASH" ls -alh bin - - uses: actions/upload-artifact@v3 + - uses: actions/upload-artifact@v4 with: name: btop-x86_64-openbsd-7.4 path: 'bin/*' diff --git a/.github/workflows/test-snap-can-build.yml b/.github/workflows/test-snap-can-build.yml index 6df8e47..2f0bba1 100644 --- a/.github/workflows/test-snap-can-build.yml +++ b/.github/workflows/test-snap-can-build.yml @@ -4,11 +4,13 @@ on: push: branches: [ main ] tags-ignore: - - '*.*' + - '*.*' paths: - 'src/**' - '!src/osx/**' - '!src/freebsd/**' + - '!src/netbsd/**' + - '!src/openbsd/**' - 'include/**' - 'Makefile' - '.github/workflows/test-snap-can-build.yml' @@ -18,19 +20,21 @@ on: - 'src/**' - '!src/osx/**' - '!src/freebsd/**' + - '!src/netbsd/**' + - '!src/openbsd/**' - 'include/**' - 'Makefile' - '.github/workflows/test-snap-can-build.yml' - + jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: [20.x] - + steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: snapcore/action-build@v1 id: build diff --git a/CMakeLists.txt b/CMakeLists.txt index 10c33b1..0ed93b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") target_sources(btop PRIVATE src/freebsd/btop_collect.cpp) elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") target_sources(btop PRIVATE src/openbsd/btop_collect.cpp src/openbsd/sysctlbyname.cpp) +elseif(CMAKE_SYSTEM_NAME STREQUAL "NetBSD") + target_sources(btop PRIVATE src/netbsd/btop_collect.cpp) elseif(LINUX) target_sources(btop PRIVATE src/linux/btop_collect.cpp) else() @@ -214,6 +216,13 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") endif() find_package(kvm REQUIRED) target_link_libraries(btop kvm::kvm) +elseif(CMAKE_SYSTEM_NAME STREQUAL "NetBSD") + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + target_compile_options(btop PRIVATE -static-libstdc++ -std=c++20 -DNDEBUG) + endif() + find_package(kvm REQUIRED) + find_package(proplib REQUIRED) + target_link_libraries(btop kvm::kvm proplib::proplib) endif() @@ -151,6 +151,12 @@ else ifeq ($(PLATFORM_LC),openbsd) override ADDFLAGS += -lkvm -static-libstdc++ export MAKE = gmake SU_GROUP := wheel +else ifeq ($(PLATFORM_LC),netbsd) + PLATFORM_DIR := netbsd + THREADS := $(shell sysctl -n hw.ncpu || echo 1) + override ADDFLAGS += -lkvm -lprop + export MAKE = gmake + SU_GROUP := wheel else $(error $(shell printf "\033[1;91mERROR: \033[97mUnsupported platform ($(PLATFORM))\033[0m")) endif @@ -7,6 +7,7 @@ ![Linux](https://img.shields.io/badge/-Linux-grey?logo=linux) ![macOS](https://img.shields.io/badge/-OSX-black?logo=apple) ![FreeBSD](https://img.shields.io/badge/-FreeBSD-red?logo=freebsd) +![NetBSD](https://img.shields.io/badge/-NetBSD-black?logo=netbsd) ![OpenBSD](https://img.shields.io/badge/-OpenBSD-black?logo=openbsd) ![Usage](https://img.shields.io/badge/Usage-System%20resource%20monitor-yellow) ![c++20](https://img.shields.io/badge/cpp-c%2B%2B20-green) @@ -18,6 +19,7 @@ [![Continuous Build Linux](https://github.com/aristocratos/btop/actions/workflows/continuous-build-linux.yml/badge.svg)](https://github.com/aristocratos/btop/actions/workflows/continuous-build-linux.yml) [![Continuous Build macOS](https://github.com/aristocratos/btop/actions/workflows/continuous-build-macos.yml/badge.svg)](https://github.com/aristocratos/btop/actions/workflows/continuous-build-macos.yml) [![Continuous Build FreeBSD](https://github.com/aristocratos/btop/actions/workflows/continuous-build-freebsd.yml/badge.svg)](https://github.com/aristocratos/btop/actions/workflows/continuous-build-freebsd.yml) +[![Continuous Build NetBSD](https://github.com/aristocratos/btop/actions/workflows/continuous-build-netbsd.yml/badge.svg)](https://github.com/aristocratos/btop/actions/workflows/continuous-build-netbsd.yml) [![Continuous Build OpenBSD](https://github.com/aristocratos/btop/actions/workflows/continuous-build-openbsd.yml/badge.svg)](https://github.com/aristocratos/btop/actions/workflows/continuous-build-openbsd.yml) ## Index @@ -35,6 +37,7 @@ * [Compilation Linux](#compilation-linux) * [Compilation macOS](#compilation-macos-osx) * [Compilation FreeBSD](#compilation-freebsd) +* [Compilation NetBSD](#compilation-netbsd) * [Compilation OpenBSD](#compilation-openbsd) * [GPU compatibility](#gpu-compatibility) * [Installing the snap](#installing-the-snap) @@ -343,6 +346,10 @@ If you have an AMD GPU `rocm_smi_lib` is required, which may or may not be packa ```sh pkg install btop ``` +* **NetBSD** + ```sh + pkg_add btop + ``` **Binary release on Homebrew (macOS (x86_64 & ARM64) / Linux (x86_64))** @@ -902,6 +909,167 @@ If you have an AMD GPU `rocm_smi_lib` is required, which may or may not be packa </details> +## Compilation NetBSD + + Requires at least GCC 10. + + Note that GNU make (`gmake`) is required to compile on NetBSD. + +<details> +<summary> + +### With gmake +</summary> + +1. **Install dependencies** + + ```bash + pkg_add gmake gcc10 coreutils git + ``` + +2. **Clone repository** + + ```bash + git clone https://github.com/aristocratos/btop.git + cd btop + ``` + +3. **Compile** + + ```bash + gmake CXXFLAGS="-DNDEBUG" + ``` + + Options for make: + + | Flag | Description | + |---------------------------------|-------------------------------------------------------------------------| + | `VERBOSE=true` | To display full compiler/linker commands | + | `STATIC=true` | For static compilation (only libgcc and libstdc++) | + | `QUIET=true` | For less verbose output | + | `STRIP=true` | To force stripping of debug symbols (adds `-s` linker flag) | + | `DEBUG=true` | Sets OPTFLAGS to `-O0 -g` and enables more verbose debug logging | + | `ARCH=<architecture>` | To manually set the target architecture | + | `FORTIFY_SOURCE=false` | Disable fortification with `_FORTIFY_SOURCE=3` | + | `ADDFLAGS=<flags>` | For appending flags to both compiler and linker | + | `CXX=<compiler>` | Manually set which compiler to use | + + Example: `gmake ADDFLAGS=-march=native` might give a performance boost if compiling only for your own system. + +4. **Install** + + ```bash + sudo gmake install + ``` + + Append `PREFIX=/target/dir` to set target, default: `/usr/local` + + Notice! Only use "sudo" when installing to a NON user owned directory. + +5. **(Recommended) Set suid bit to make btop always run as root (or other user)** + + ```bash + sudo gmake setuid + ``` + + No need for `sudo` to see information for non user owned processes and to enable signal sending to any process. + + Run after make install and use same PREFIX if any was used at install. + + Set `SU_USER` and `SU_GROUP` to select user and group, default is `root` and `wheel` + +* **Uninstall** + + ```bash + sudo gmake uninstall + ``` + +* **Remove any object files from source dir** + + ```bash + gmake clean + ``` + +* **Remove all object files, binaries and created directories in source dir** + + ```bash + gmake distclean + ``` + +* **Show help** + + ```bash + gmake help + ``` + +</details> +<details> +<summary> + +### With CMake (Community maintained) +</summary> + +1. **Install build dependencies** + + Requires GCC, CMake, Ninja and Git + + ```bash + pkg_add cmake ninja-build gcc10 coreutils git + ``` + +2. **Clone the repository** + + ```bash + git clone https://github.com/aristocratos/btop.git && cd btop + ``` + +3. **Compile** + + ```bash + # Configure + cmake -DCMAKE_CXX_COMPILER="/usr/pkg/gcc10/bin/g++" -B build -G Ninja + # Build + cmake --build build + ``` + + This will automatically build a release version of btop. + + Some useful options to pass to the configure step: + + | Configure flag | Description | + |---------------------------------|-------------------------------------------------------------------------| + | `-DBTOP_LTO=<ON\|OFF>` | Enables link time optimization (ON by default) | + | `-DBTOP_USE_MOLD=<ON\|OFF>` | Use mold to link btop (OFF by default) | + | `-DBTOP_PEDANTIC=<ON\|OFF>` | Compile with additional warnings (OFF by default) | + | `-DBTOP_WERROR=<ON\|OFF>` | Compile with warnings as errors (OFF by default) | + | `-DBTOP_FORTIFY=<ON\|OFF>` | Detect buffer overflows with `_FORTIFY_SOURCE=3` (ON by default) | + | `-DCMAKE_INSTALL_PREFIX=<path>` | The installation prefix ('/usr/local' by default) | + + To force any other compiler, run `CXX=<compiler> cmake -B build -G Ninja` + +4. **Install** + + ```bash + cmake --install build + ``` + + May require root privileges + +5. **Uninstall** + + CMake doesn't generate an uninstall target by default. To remove installed files, run + ``` + cat build/install_manifest.txt | xargs rm -irv + ``` + +6. **Cleanup build directory** + + ```bash + cmake --build build -t clean + ``` + +</details> + ## Compilation OpenBSD Requires at least GCC 10. diff --git a/cmake/Modules/Findproplib.cmake b/cmake/Modules/Findproplib.cmake new file mode 100644 index 0000000..4be4393 --- /dev/null +++ b/cmake/Modules/Findproplib.cmake @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Find proplib – property container object library +# + +if(BSD) + find_path(proplib_INCLUDE_DIR NAMES prop/proplib.h) + find_library(proplib_LIBRARY NAMES libprop prop) + + include(FindPackageHandleStandardArgs) + find_package_handle_standard_args(proplib REQUIRED_VARS proplib_LIBRARY proplib_INCLUDE_DIR) + + if(proplib_FOUND AND NOT TARGET proplib::proplib) + add_library(proplib::proplib UNKNOWN IMPORTED) + set_target_properties(proplib::proplib PROPERTIES + IMPORTED_LOCATION "${proplib_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${proplib_INCLUDE_DIR}" + ) + endif() + + mark_as_advanced(proplib_INCLUDE_DIR proplib_LIBRARY) +endif() + diff --git a/src/btop.cpp b/src/btop.cpp index 8eae107..6d4753a 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -120,15 +120,41 @@ namespace Global { static void print_version() { if constexpr (GIT_COMMIT.empty()) { - fmt::print("btop version: {}\n", Global::Version); + fmt::println("btop version: {}", Global::Version); } else { - fmt::print("btop version: {}+{}\n", Global::Version, GIT_COMMIT); + fmt::println("btop version: {}+{}", Global::Version, GIT_COMMIT); } } static void print_version_with_build_info() { print_version(); - fmt::print("Compiled with: {} ({})\nConfigured with: {}\n", COMPILER, COMPILER_VERSION, CONFIGURE_COMMAND); + fmt::println("Compiled with: {} ({})\nConfigured with: {}", COMPILER, COMPILER_VERSION, CONFIGURE_COMMAND); +} + +static void print_usage() { + fmt::println("\033[1;4mUsage:\033[0;1m btop\033[0m [OPTIONS]\n"); +} + +static void print_help() { + print_usage(); + fmt::println( + "{0}{1}Options:{2}\n" + " {0}-h, --help {2}show this help message and exit\n" + " {0}-v, --version {2}show version info and exit\n" + " {0}-lc, --low-color {2}disable truecolor, converts 24-bit colors to 256-color\n" + " {0}-t, --tty_on {2}force (ON) tty mode, max 16 colors and tty friendly graph symbols\n" + " {0}+t, --tty_off {2}force (OFF) tty mode\n" + " {0}-p, --preset <id> {2}start with preset, integer value between 0-9\n" + " {0}-u, --update <ms> {2}set the program update rate in milliseconds\n" + " {0} --utf-force {2}force start even if no UTF-8 locale was detected\n" + " {0} --debug {2}start in DEBUG mode: shows microsecond timer for information collect\n" + " {0} {2}and screen draw functions and sets loglevel to DEBUG", + "\033[1m", "\033[4m", "\033[0m" + ); +} + +static void print_help_hint() { + fmt::println("For more information, try '{0}--help{1}'", "\033[1m", "\033[0m"); } //* A simple argument parser @@ -136,20 +162,7 @@ void argumentParser(const int argc, char **argv) { for(int i = 1; i < argc; i++) { const string argument = argv[i]; if (is_in(argument, "-h", "--help")) { - fmt::println( - "usage: btop [-h] [-v] [-/+t] [-p <id>] [-u <ms>] [--utf-force] [--debug]\n\n" - "optional arguments:\n" - " -h, --help show this help message and exit\n" - " -v, --version show version info and exit\n" - " -lc, --low-color disable truecolor, converts 24-bit colors to 256-color\n" - " -t, --tty_on force (ON) tty mode, max 16 colors and tty friendly graph symbols\n" - " +t, --tty_off force (OFF) tty mode\n" - " -p, --preset <id> start with preset, integer value between 0-9\n" - " -u, --update <ms> set the program update rate in milliseconds\n" - " --utf-force force start even if no UTF-8 locale was detected\n" - " --debug start in DEBUG mode: shows microsecond timer for information collect\n" - " and screen draw functions and sets loglevel to DEBUG" - ); + print_help(); exit(0); } else if (is_in(argument, "-v")) { @@ -173,27 +186,35 @@ void argumentParser(const int argc, char **argv) { } else if (is_in(argument, "-p", "--preset")) { if (++i >= argc) { - fmt::println("ERROR: Preset option needs an argument."); + fmt::println("{0}error:{1} Preset option needs an argument\n", "\033[1;31m", "\033[0m"); + print_usage(); + print_help_hint(); exit(1); } else if (const string val = argv[i]; isint(val) and val.size() == 1) { Global::arg_preset = std::clamp(stoi(val), 0, 9); } else { - fmt::println("ERROR: Preset option only accepts an integer value between 0-9."); + fmt::println("{0}error: {1}Preset option only accepts an integer value between 0-9\n", "\033[1;31m", "\033[0m"); + print_usage(); + print_help_hint(); exit(1); } } else if (is_in(argument, "-u", "--update")) { if (++i >= argc) { - fmt::println("ERROR: Update option needs an argument"); + fmt::println("{0}error:{1} Update option needs an argument\n", "\033[1;31m", "\033[0m"); + print_usage(); + print_help_hint(); exit(1); } const std::string value = argv[i]; if (isint(value)) { Global::arg_update = std::clamp(std::stoi(value), 100, Config::ONE_DAY_MILLIS); } else { - fmt::println("ERROR: Invalid update rate"); + fmt::println("{0}error:{1} Invalid update rate\n", "\033[1;31m", "\033[0m"); + print_usage(); + print_help_hint(); exit(1); } } @@ -202,8 +223,9 @@ void argumentParser(const int argc, char **argv) { else if (argument == "--debug") Global::debug = true; else { - fmt::println(" Unknown argument: {}\n" - " Use -h or --help for help.", argument); + fmt::println("{0}error:{2} unexpected argument '{1}{3}{2}' found\n", "\033[1;31m", "\033[33m", "\033[0m", argument); + print_usage(); + print_help_hint(); exit(1); } } @@ -293,7 +315,7 @@ void clean_quit(int sig) { Global::quitting = true; Runner::stop(); if (Global::_runner_started) { - #if defined __APPLE__ || defined __OpenBSD__ + #if defined __APPLE__ || defined __OpenBSD__ || defined __NetBSD__ if (pthread_join(Runner::runner_id, nullptr) != 0) { Logger::warning("Failed to join _runner thread on exit!"); pthread_cancel(Runner::runner_id); @@ -329,7 +351,7 @@ void clean_quit(int sig) { const auto excode = (sig != -1 ? sig : 0); -#if defined __APPLE__ || defined __OpenBSD__ +#if defined __APPLE__ || defined __OpenBSD__ || defined __NetBSD__ _Exit(excode); #else quick_exit(excode); @@ -1024,7 +1046,7 @@ int main(int argc, char **argv) { Config::set("tty_mode", true); Logger::info("Forcing tty mode: setting 16 color mode and using tty friendly graph symbols"); } -#if not defined __APPLE__ && not defined __OpenBSD__ +#if not defined __APPLE__ && not defined __OpenBSD__ && not defined __NetBSD__ else if (not Global::arg_tty and Term::current_tty.starts_with("/dev/tty")) { Config::set("tty_mode", true); Logger::info("Real tty detected: setting 16 color mode and using tty friendly graph symbols"); diff --git a/src/btop_shared.hpp b/src/btop_shared.hpp index 0a8f452..f1f974e 100644 --- a/src/btop_shared.hpp +++ b/src/btop_shared.hpp @@ -34,7 +34,7 @@ tab-size = 4 #include <ifaddrs.h> // clang-format on -#if defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) # include <kvm.h> #endif @@ -93,7 +93,7 @@ namespace Shared { extern long coreCount, page_size, clk_tck; -#if defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) struct KvmDeleter { void operator()(kvm_t* handle) { kvm_close(handle); diff --git a/src/netbsd/btop_collect.cpp b/src/netbsd/btop_collect.cpp new file mode 100644 index 0000000..234b7d5 --- /dev/null +++ b/src/netbsd/btop_collect.cpp @@ -0,0 +1,1420 @@ +/* Copyright 2021 Aristocratos (jakob@qvantnet.com) + Copyright 2024 Santhosh Raju (fox@NetBSD.org) + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +indent = tab +tab-size = 4 +*/ +#include <arpa/inet.h> +#include <cstddef> +#include <cstdio> +#include <cstdlib> +#include <cstring> +// man 3 getifaddrs: "BUGS: If both <net/if.h> and <ifaddrs.h> are being included, <net/if.h> must be included before <ifaddrs.h>" +#include <net/if.h> +#include <ifaddrs.h> +#include <net/if_dl.h> +#include <net/route.h> +#include <netdb.h> +#include <netinet/tcp_fsm.h> +#include <netinet/in.h> // for inet_ntop stuff +#include <pwd.h> +#include <prop/proplib.h> +#include <sys/endian.h> +#include <sys/iostat.h> +#include <sys/envsys.h> +#include <sys/resource.h> +#include <sys/socket.h> +#include <sys/statvfs.h> +#include <sys/sysctl.h> +#include <sys/sched.h> +#include <sys/signal.h> +#include <sys/siginfo.h> +#include <sys/proc.h> +#include <sys/types.h> +#include <sys/param.h> +#include <sys/ucred.h> +#include <sys/mount.h> +#include <sys/vmmeter.h> +#include <sys/disk.h> +#include <vector> +#include <kvm.h> +#include <paths.h> +#include <fcntl.h> +#include <unistd.h> +#include <uvm/uvm_extern.h> + +#include <stdexcept> +#include <cmath> +#include <fstream> +#include <numeric> +#include <ranges> +#include <algorithm> +#include <regex> +#include <string> +#include <memory> +#include <utility> + +#include "../btop_config.hpp" +#include "../btop_shared.hpp" +#include "../btop_tools.hpp" + +using std::clamp, std::string_literals::operator""s, std::cmp_equal, std::cmp_less, std::cmp_greater; +using std::ifstream, std::numeric_limits, std::streamsize, std::round, std::max, std::min; +namespace fs = std::filesystem; +namespace rng = std::ranges; +using namespace Tools; + +//? --------------------------------------------------- FUNCTIONS ----------------------------------------------------- + +namespace Cpu { + vector<long long> core_old_totals; + vector<long long> core_old_idles; + vector<string> available_fields = {"total"}; + vector<string> available_sensors = {"Auto"}; + cpu_info current_cpu; + bool got_sensors = false, cpu_temp_only = false; + + //* Populate found_sensors map + bool get_sensors(); + + //* Get current cpu clock speed + string get_cpuHz(); + + //* Search /proc/cpuinfo for a cpu name + string get_cpuName(); + + struct Sensor { + fs::path path; + string label; + int64_t temp = 0; + int64_t high = 0; + int64_t crit = 0; + }; + + string cpu_sensor; + vector<string> core_sensors; + std::unordered_map<int, int> core_mapping; +} // namespace Cpu + +namespace Mem { + double old_uptime; +} + +namespace Shared { + + fs::path passwd_path; + uint64_t totalMem; + long pageSize, clkTck, coreCount, physicalCoreCount, arg_max; + int totalMem_len, kfscale; + long bootTime; + size_t size; + + void init() { + //? Shared global variables init + int mib[2]; + mib[0] = CTL_HW; + mib[1] = HW_NCPU; + int ncpu; + size_t len = sizeof(ncpu); + if (sysctl(mib, 2, &ncpu, &len, nullptr, 0) == -1) { + Logger::warning("Could not determine number of cores, defaulting to 1."); + } else { + coreCount = ncpu; + } + + size = sizeof(pageSize); + if (sysctlbyname("hw.pagesize", &pageSize, &size, nullptr, 0) < 0) { + pageSize = 4096; + Logger::warning("Could not get system page size. Defaulting to 4096, processes memory usage might be incorrect."); + } + + clkTck = sysconf(_SC_CLK_TCK); + if (clkTck <= 0) { + clkTck = 100; + Logger::warning("Could not get system clock ticks per second. Defaulting to 100, processes cpu usage might be incorrect."); + } + + size = sizeof(totalMem); + if (sysctlbyname("hw.physmem64", &totalMem, &size, nullptr, 0) < 0) { + Logger::warning("Could not get memory size"); + } + + struct timeval result; + size = sizeof(result); + if (sy |