Skip to content

Commit

Permalink
replace custom nproc(1) with freebsd nproc(1)
Browse files Browse the repository at this point in the history
  • Loading branch information
q66 committed Aug 18, 2024
1 parent 3747243 commit 01bc499
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 134 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ It replaces the following GNU projects:
It also provides the following functionality:

* ee
* nproc
* tip/cu
* telnet
* fetch
Expand Down
1 change: 1 addition & 0 deletions import-src.sh
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ done

# equivalents of standalone projects
copy_cmd bin/ed
copy_cmd bin/nproc
copy_cmd bin/sh
copy_cmd usr.bin/bintrans
copy_cmd usr.bin/compress
Expand Down
40 changes: 40 additions & 0 deletions patches/src.freebsd.patch
Original file line number Diff line number Diff line change
Expand Up @@ -12670,6 +12670,46 @@
if (*buf == '\0')
break;
}
--- src.orig/nproc/nproc.c
+++ src.freebsd/nproc/nproc.c
@@ -14,7 +14,6 @@
*/

#include <sys/param.h>
-#include <sys/cpuset.h>

#include <err.h>
#include <errno.h>
@@ -25,6 +24,8 @@
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
+#include <pthread.h>
+#include <sched.h>

#define OPT_ALL (CHAR_MAX + 1)
#define OPT_IGNORE (CHAR_MAX + 2)
@@ -75,7 +76,7 @@
main(int argc, char *argv[])
{
const char *errstr;
- cpuset_t mask;
+ cpu_set_t mask;
int ch, cpus, ignore;
bool all_flag;

@@ -115,9 +116,8 @@
err(1, "sysconf");
} else {
CPU_ZERO(&mask);
- if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
- sizeof(mask), &mask) != 0)
- err(1, "cpuset_getaffinity");
+ if (pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &mask) != 0)
+ err(1, "pthread_geteaffinity_np");
cpus = CPU_COUNT(&mask);
}

--- src.orig/nvi/cl/cl_funcs.c
+++ src.freebsd/nvi/cl/cl_funcs.c
@@ -146,7 +146,7 @@
Expand Down
1 change: 0 additions & 1 deletion src.custom/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ subdir('fsfreeze')
subdir('hostid')
subdir('ionice')
subdir('isosize')
subdir('nproc')
subdir('pivot_root')
subdir('resizepart')
subdir('setarch')
Expand Down
5 changes: 0 additions & 5 deletions src.custom/nproc/meson.build

This file was deleted.

128 changes: 0 additions & 128 deletions src.custom/nproc/nproc.c

This file was deleted.

1 change: 1 addition & 0 deletions src.freebsd/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ subdir('grep')
subdir('jot')
subdir('m4')
subdir('netcat')
subdir('nproc')
subdir('patch')
subdir('sed')
subdir('sh')
Expand Down
8 changes: 8 additions & 0 deletions src.freebsd/nproc/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
nproc_prog = executable(
'nproc', ['nproc.c'],
include_directories: inc,
install: true,
link_with: [libcompat],
)

install_man('nproc.1')
60 changes: 60 additions & 0 deletions src.freebsd/nproc/nproc.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
.\"-
.\" * Copyright (c) 2023 Piotr Paweł Stefaniak
.\"
.\" * SPDX-License-Identifier: BSD-2-Clause
.\"
.Dd June 2, 2023
.Dt NPROC 1
.Os
.Sh NAME
.Nm nproc
.Nd print the number of processors
.Sh SYNOPSIS
.Nm
.Op Fl -all
.Op Fl -ignore Ns = Ns Ar count
.Nm Fl -help
.Nm Fl -version
.Sh DESCRIPTION
The
.Nm
utility is used to print the number of processors limited to the
.Xr cpuset 2
of the current process, unless the
.Fl -all
flag is specified.
.Pp
The available flags are:
.Bl -tag -width Ds
.It Fl -all
Count all processors currently online.
.It Fl -ignore Ns = Ns Ar count
The result is decreased by
.Ar count ,
but never below 1.
.It Fl -version
Print the current program version and exit.
Do not use this option.
.It Fl -help
Print usage information and exit.
.El
.Sh COMPATIBILITY
This program is intended to be compatible with nproc as found in GNU coreutils.
.Sh BUGS
If HyperThreading is enabled in BIOS and disabled in the kernel, the
.Fl -all
option on Linux will still report all possible CPU threads.
On FreeBSD only active threads will be reported.
.Sh SEE ALSO
.Xr cpuset 1
.Sh HISTORY
The
.Nm
utility first appeared in
.Fx 13.2 .
.Sh AUTHORS
.An -nosplit
.An Mateusz Guzik Aq Mt [email protected]
wrote the program and
.An Piotr Paweł Stefaniak Aq Mt [email protected]
wrote this page.
Loading

0 comments on commit 01bc499

Please sign in to comment.