From c5aaa5a01389da3e88806389127b05eeb8fa8014 Mon Sep 17 00:00:00 2001 From: Philipp Tusch Date: Thu, 5 Jul 2018 23:26:51 +0200 Subject: [PATCH 1/7] process: add nice() function The nice function allows us to fine-tune the process to meet desired scheduling behavior. If our process needs less schedule time because it is a long-running one, we can increase the nice value and cause the scheduler to select the process not so often. --- doc/api/process.md | 34 +++++++++++++++++ lib/internal/bootstrap/node.js | 4 +- lib/internal/process/main_thread_only.js | 26 +++++++++++-- src/bootstrapper.cc | 1 + src/node.cc | 2 +- src/node_internals.h | 1 + src/node_process.cc | 24 ++++++++++++ test/parallel/test-process-nice.js | 47 ++++++++++++++++++++++++ 8 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 test/parallel/test-process-nice.js diff --git a/doc/api/process.md b/doc/api/process.md index edf01258c3941b..2746fa89ecfc7a 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1642,6 +1642,40 @@ This function is only available on POSIX platforms (i.e. not Windows or Android). This feature is not available in [`Worker`][] threads. +## process.nice(inc) + + +* `inc` {integer} The new nice value for the process. + +The `process.nice()` method sets or gets the nice value of the process. +(See nice(2).) The `inc` is supposed to be an integer, consisting of the +_difference_ you want to add to the current nice value. +The higher the nice value of the process, the nicer the process and the less +time is given by the scheduler. + +You can get the current nice value by passing 0 or nothing to the method. + +Unless you have super user permissions, you can only increase your nice value. +Though, it is also possible to increase the priority if a limit (RLIMIT_NICE) +is set. (See getrlimit(2).) + +**NOTE**: Once you incremented your niceness, you can no longer reduce it! + +```js +if (process.nice) { + const currentNice = process.nice(); + console.log(currentNice); // Prints the current nice value + + process.nice(1); // Increases nice value - giving it less priority. +} +``` + +This function is only available on POSIX platforms (i.e. not Windows or +Android). +This feature is not available in [`Worker`][] threads. + ## process.setgid(id) * `inc` {integer} The new nice value for the process. diff --git a/lib/internal/process/main_thread_only.js b/lib/internal/process/main_thread_only.js index 05995bbf4e489f..31e3032c84d86a 100644 --- a/lib/internal/process/main_thread_only.js +++ b/lib/internal/process/main_thread_only.js @@ -78,7 +78,8 @@ function setupPosixMethods(_initgroups, _setegid, _seteuid, _nice, }; process.nice = function nice(inc) { - return validateInc(inc); + validateInc(inc); + return _nice(inc); }; process.setgid = function setgid(id) { @@ -105,12 +106,10 @@ function setupPosixMethods(_initgroups, _setegid, _seteuid, _nice, }; function validateInc(inc) { - const tInc = typeof inc; - if (tInc === 'number') { + if (typeof inc === 'number') { validateInt32(inc, 'nice'); - return _nice(inc); - } else if (tInc === 'undefined') { - return _nice(inc); + } else if (typeof inc === 'undefined') { + // placeholder.. } else { throw new ERR_INVALID_ARG_TYPE('nice', ['number', 'undefined'], inc); } diff --git a/src/node_process.cc b/src/node_process.cc index 2ff04d4fbf236b..10acc8bae67a25 100644 --- a/src/node_process.cc +++ b/src/node_process.cc @@ -33,6 +33,7 @@ using v8::BigUint64Array; using v8::Float64Array; using v8::FunctionCallbackInfo; using v8::HeapStatistics; +using v8::Int32; using v8::Integer; using v8::Isolate; using v8::Local; @@ -444,7 +445,7 @@ void Nice(const FunctionCallbackInfo& args) { // ..only check type if argument is int32 if (!args[0]->IsUndefined()) { CHECK(args[0]->IsInt32()); - inc = args[0]->Int32Value(); + inc = args[0].As()->Value(); } errno = 0; From cc9250314977f265209bb3b473be1c300cfa3009 Mon Sep 17 00:00:00 2001 From: "Philipp Tusch zokker13@posteo.de" Date: Fri, 6 Jul 2018 00:32:17 +0200 Subject: [PATCH 3/7] fixup: follow review comments by devsnek --- lib/internal/process/main_thread_only.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/internal/process/main_thread_only.js b/lib/internal/process/main_thread_only.js index 31e3032c84d86a..2696521dd9e3cf 100644 --- a/lib/internal/process/main_thread_only.js +++ b/lib/internal/process/main_thread_only.js @@ -108,9 +108,7 @@ function setupPosixMethods(_initgroups, _setegid, _seteuid, _nice, function validateInc(inc) { if (typeof inc === 'number') { validateInt32(inc, 'nice'); - } else if (typeof inc === 'undefined') { - // placeholder.. - } else { + } else if (inc !== undefined) { throw new ERR_INVALID_ARG_TYPE('nice', ['number', 'undefined'], inc); } } From bf86dcca09a5a2422824b26885896f506617e82e Mon Sep 17 00:00:00 2001 From: "Philipp Tusch zokker13@posteo.de" Date: Fri, 6 Jul 2018 15:30:56 +0200 Subject: [PATCH 4/7] fixup: follow review comments by various folk --- doc/api/process.md | 22 +++++++++++----------- src/node_internals.h | 5 ++++- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/doc/api/process.md b/doc/api/process.md index da08a292241a9d..8f921d0c56a0a6 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1647,21 +1647,22 @@ This feature is not available in [`Worker`][] threads. added: REPLACEME --> -* `inc` {integer} The new nice value for the process. +* `inc` {integer} The nice value which is added to the current nice value. + +Returns the new nice value of the process. The `process.nice()` method sets or gets the nice value of the process. (See nice(2).) The `inc` is supposed to be an integer, consisting of the -_difference_ you want to add to the current nice value. +_difference_ the user wants to add to the current nice value. The higher the nice value of the process, the nicer the process and the less -time is given by the scheduler. - -You can get the current nice value by passing 0 or nothing to the method. +time is given by the scheduler (effectively lowering the priority). -Unless you have super user permissions, you can only increase your nice value. -Though, it is also possible to increase the priority if a limit (RLIMIT_NICE) -is set. (See getrlimit(2).) +It is possible to get the current nice value by passing 0 or `undefined` +to the method. -**NOTE**: Once you incremented your niceness, you can no longer reduce it! +In a common environment, it is only possible to increase the nice value. +A decrease of the value at a later time might not be possible (unless +the specified permissions are set) (See getrlimit(2).) ```js if (process.nice) { @@ -1672,8 +1673,7 @@ if (process.nice) { } ``` -This function is only available on POSIX platforms (i.e. not Windows or -Android). +This function is only available on POSIX platforms (i.e. not Windows). This feature is not available in [`Worker`][] threads. ## process.setgid(id) diff --git a/src/node_internals.h b/src/node_internals.h index bc2d9a1d6eae40..735517aca6cc88 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -948,12 +948,15 @@ void StopProfilerIdleNotifier(const v8::FunctionCallbackInfo& args); void Umask(const v8::FunctionCallbackInfo& args); void Uptime(const v8::FunctionCallbackInfo& args); +#if defined(__POSIX__) && !defined(__CloudABI__) +void Nice(const v8::FunctionCallbackInfo& args); +#endif // __POSIX__ && !defined(__CloudABI__) + #if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__) void SetGid(const v8::FunctionCallbackInfo& args); void SetEGid(const v8::FunctionCallbackInfo& args); void SetUid(const v8::FunctionCallbackInfo& args); void SetEUid(const v8::FunctionCallbackInfo& args); -void Nice(const v8::FunctionCallbackInfo& args); void SetGroups(const v8::FunctionCallbackInfo& args); void InitGroups(const v8::FunctionCallbackInfo& args); void GetUid(const v8::FunctionCallbackInfo& args); From e1388f32354cdc416daebf28f866495f495b17cb Mon Sep 17 00:00:00 2001 From: "Philipp Tusch zokker13@posteo.de" Date: Fri, 6 Jul 2018 15:50:52 +0200 Subject: [PATCH 5/7] fixup: follow review comment by devsnek --- doc/api/process.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/process.md b/doc/api/process.md index 8f921d0c56a0a6..8a5cfa72e38222 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1653,7 +1653,7 @@ Returns the new nice value of the process. The `process.nice()` method sets or gets the nice value of the process. (See nice(2).) The `inc` is supposed to be an integer, consisting of the -_difference_ the user wants to add to the current nice value. +_difference_ to be added to the current nice value of the process. The higher the nice value of the process, the nicer the process and the less time is given by the scheduler (effectively lowering the priority). From 01b0643eeefa12237c9b5d1b1b9df3c66dfde860 Mon Sep 17 00:00:00 2001 From: "Philipp Tusch zokker13@posteo.de" Date: Fri, 6 Jul 2018 23:15:19 +0200 Subject: [PATCH 6/7] fixup: throw not implemented on win32 --- lib/internal/process/main_thread_only.js | 38 ++++++++++++++---------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/internal/process/main_thread_only.js b/lib/internal/process/main_thread_only.js index 2696521dd9e3cf..2a6ed3f4df9feb 100644 --- a/lib/internal/process/main_thread_only.js +++ b/lib/internal/process/main_thread_only.js @@ -7,7 +7,8 @@ const { errnoException, codes: { ERR_INVALID_ARG_TYPE, - ERR_UNKNOWN_CREDENTIAL + ERR_UNKNOWN_CREDENTIAL, + ERR_METHOD_NOT_IMPLEMENTED } } = require('internal/errors'); const { @@ -32,11 +33,21 @@ function setupStdio() { function setupProcessMethods(_chdir, _umask, _initgroups, _setegid, _seteuid, _nice, _setgid, _setuid, _setgroups) { + if (_setgid !== undefined) { - setupPosixMethods(_initgroups, _setegid, _seteuid, _nice, + setupPosixMethods(_initgroups, _setegid, _seteuid, _setgid, _setuid, _setgroups); } + process.nice = function nice(inc) { + if (process.platform === 'win32') { + throw new ERR_METHOD_NOT_IMPLEMENTED('nice()'); + } + + validateInc(inc); + return _nice(inc); + }; + process.chdir = function chdir(directory) { if (typeof directory !== 'string') { throw new ERR_INVALID_ARG_TYPE('directory', 'string', directory); @@ -52,9 +63,17 @@ function setupProcessMethods(_chdir, _umask, _initgroups, _setegid, mask = validateMode(mask, 'mask'); return _umask(mask); }; + + function validateInc(inc) { + if (typeof inc === 'number') { + validateInt32(inc, 'nice'); + } else if (inc !== undefined) { + throw new ERR_INVALID_ARG_TYPE('nice', ['number', 'undefined'], inc); + } + } } -function setupPosixMethods(_initgroups, _setegid, _seteuid, _nice, +function setupPosixMethods(_initgroups, _setegid, _seteuid, _setgid, _setuid, _setgroups) { process.initgroups = function initgroups(user, extraGroup) { @@ -77,11 +96,6 @@ function setupPosixMethods(_initgroups, _setegid, _seteuid, _nice, return execId(id, 'User', _seteuid); }; - process.nice = function nice(inc) { - validateInc(inc); - return _nice(inc); - }; - process.setgid = function setgid(id) { return execId(id, 'Group', _setgid); }; @@ -105,14 +119,6 @@ function setupPosixMethods(_initgroups, _setegid, _seteuid, _nice, } }; - function validateInc(inc) { - if (typeof inc === 'number') { - validateInt32(inc, 'nice'); - } else if (inc !== undefined) { - throw new ERR_INVALID_ARG_TYPE('nice', ['number', 'undefined'], inc); - } - } - function execId(id, type, method) { validateId(id, 'id'); // Result is 0 on success, 1 if credential is unknown. From aadb65bb43742293e702f7f7c0b8405f3146058b Mon Sep 17 00:00:00 2001 From: "Philipp Tusch zokker13@posteo.de" Date: Fri, 6 Jul 2018 23:23:51 +0200 Subject: [PATCH 7/7] fixup: implement doc improvements by vsemozhetbyt --- doc/api/process.md | 68 +++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/doc/api/process.md b/doc/api/process.md index 8a5cfa72e38222..58f3f8cc5725d5 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1447,6 +1447,40 @@ The next tick queue is completely drained on each pass of the event loop `nextTick()` callbacks will block any I/O from happening, just like a `while(true);` loop. +## process.nice([inc]) + + +* `inc` {integer} The nice value which is added to the current nice value. +* Returns: {integer} The new nice value for the process. + +The `process.nice()` method sets or gets the nice value of the process. +(See nice(2).) The `inc` is supposed to be an integer, consisting of the +_difference_ to be added to the current nice value of the process. +The higher the nice value of the process, the nicer the process and the less +time is given by the scheduler (effectively lowering the priority). + +It is possible to get the current nice value by passing `0` or `undefined` +to the method. + +In a common environment, it is only possible to increase the nice value. +A decrease of the value at a later time might not be possible (unless +the specified permissions are set). (See getrlimit(2).) + +```js +if (process.platform !== 'win32') { + const currentNice = process.nice(); + console.log(currentNice); // Prints the current nice value. + + process.nice(1); // Increases nice value - giving it less priority. +} +``` + +This function works only on POSIX platforms and throws not implemented errors +on other platforms. +This feature is not available in [`Worker`][] threads. + ## process.noDeprecation - -* `inc` {integer} The nice value which is added to the current nice value. - -Returns the new nice value of the process. - -The `process.nice()` method sets or gets the nice value of the process. -(See nice(2).) The `inc` is supposed to be an integer, consisting of the -_difference_ to be added to the current nice value of the process. -The higher the nice value of the process, the nicer the process and the less -time is given by the scheduler (effectively lowering the priority). - -It is possible to get the current nice value by passing 0 or `undefined` -to the method. - -In a common environment, it is only possible to increase the nice value. -A decrease of the value at a later time might not be possible (unless -the specified permissions are set) (See getrlimit(2).) - -```js -if (process.nice) { - const currentNice = process.nice(); - console.log(currentNice); // Prints the current nice value - - process.nice(1); // Increases nice value - giving it less priority. -} -``` - -This function is only available on POSIX platforms (i.e. not Windows). -This feature is not available in [`Worker`][] threads. - ## process.setgid(id)