From: Eli Zaretskii Date: Sun, 7 Mar 2021 13:32:55 +0000 (+0200) Subject: Use MS-Windows system APIs to get number of processors X-Git-Tag: emacs-28.0.90~2727^2~93 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=619f66f423c76d94b2bca728f9c22b536e909108;p=emacs.git Use MS-Windows system APIs to get number of processors * lisp/emacs-lisp/comp.el: Use 'w32-get-nproc' instead of the environment variable NUMBER_OF_PROCESSORS. * src/w32proc.c (Fw32_get_nproc): New primitive. * src/w32.c (w32_get_nproc): New function. (sample_system_load): Call w32_get_nproc to initialize the number of processors on this system. * src/w32.h (w32_get_nproc): Add prototype. --- diff --git a/lisp/emacs-lisp/comp.el b/lisp/emacs-lisp/comp.el index 134b5a28086..70e10644ca8 100644 --- a/lisp/emacs-lisp/comp.el +++ b/lisp/emacs-lisp/comp.el @@ -3824,8 +3824,7 @@ processes from `comp-async-compilations'" ;; the number of processors, see get_native_system_info in w32.c. ;; The result needs to be exported to Lisp. (max 1 (/ (cond ((eq 'windows-nt system-type) - (string-to-number (getenv - "NUMBER_OF_PROCESSORS"))) + (w32-get-nproc)) ((executable-find "nproc") (string-to-number (shell-command-to-string "nproc"))) diff --git a/src/w32.c b/src/w32.c index 96eba1e5681..7ce907d0adb 100644 --- a/src/w32.c +++ b/src/w32.c @@ -1941,11 +1941,10 @@ buf_prev (int from) return prev_idx; } -static void -sample_system_load (ULONGLONG *idle, ULONGLONG *kernel, ULONGLONG *user) +unsigned +w32_get_nproc (void) { SYSTEM_INFO sysinfo; - FILETIME ft_idle, ft_user, ft_kernel; /* Initialize the number of processors on this machine. */ if (num_of_processors <= 0) @@ -1960,6 +1959,15 @@ sample_system_load (ULONGLONG *idle, ULONGLONG *kernel, ULONGLONG *user) if (num_of_processors <= 0) num_of_processors = 1; } + return num_of_processors; +} + +static void +sample_system_load (ULONGLONG *idle, ULONGLONG *kernel, ULONGLONG *user) +{ + FILETIME ft_idle, ft_user, ft_kernel; + + (void) w32_get_nproc (); /* TODO: Take into account threads that are ready to run, by sampling the "\System\Processor Queue Length" performance diff --git a/src/w32.h b/src/w32.h index 3f8eb250cc1..a382dbe791a 100644 --- a/src/w32.h +++ b/src/w32.h @@ -233,6 +233,9 @@ extern int w32_memory_info (unsigned long long *, unsigned long long *, /* Compare 2 UTF-8 strings in locale-dependent fashion. */ extern int w32_compare_strings (const char *, const char *, char *, int); +/* Return the number of processor execution units on this system. */ +extern unsigned w32_get_nproc (void); + /* Return a cryptographically secure seed for PRNG. */ extern int w32_init_random (void *, ptrdiff_t); diff --git a/src/w32proc.c b/src/w32proc.c index 2b6cb9c1e1d..a50c87777fa 100644 --- a/src/w32proc.c +++ b/src/w32proc.c @@ -3877,6 +3877,14 @@ w32_compare_strings (const char *s1, const char *s2, char *locname, return val - 2; } +DEFUN ("w32-get-nproc", Fw32_get_nproc, + Sw32_get_nproc, 0, 0, 0, + doc: /* Return the number of system's processor execution units. */) + (void) +{ + return make_fixnum (w32_get_nproc ()); +} + void syms_of_ntproc (void) @@ -3911,6 +3919,8 @@ syms_of_ntproc (void) defsubr (&Sw32_get_keyboard_layout); defsubr (&Sw32_set_keyboard_layout); + defsubr (&Sw32_get_nproc); + DEFVAR_LISP ("w32-quote-process-args", Vw32_quote_process_args, doc: /* Non-nil enables quoting of process arguments to ensure correct parsing. Because Windows does not directly pass argv arrays to child processes,