-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
process: add nice() function #21675
process: add nice() function #21675
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good so far … nice :)
doc/api/process.md
Outdated
@@ -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) | |||
<!-- YAML | |||
added: v10.?.? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use REPLACEME
as the placeholder here? It gets picked up by release tooling automatically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/node_process.cc
Outdated
// ..only check type if argument is int32 | ||
if (!args[0]->IsUndefined()) { | ||
CHECK(args[0]->IsInt32()); | ||
inc = args[0]->Int32Value(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use args[0].As<Int32>()->Value()
? ->Int32Value()
is an operation that potentially performs a type conversion, but that’s unnecessarily complex, since we already know that it’s an Int32
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds great and it's done, thanks!
@@ -98,6 +104,18 @@ function setupPosixMethods(_initgroups, _setegid, _seteuid, | |||
} | |||
}; | |||
|
|||
function validateInc(inc) { | |||
const tInc = typeof inc; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you inline typeof inc
here? The engine is generally better at recognizing that pattern and can eliminate the temporary string you’d be creating here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the deep insights. It is fixed.
validateInt32(inc, 'nice'); | ||
return _nice(inc); | ||
} else if (tInc === 'undefined') { | ||
return _nice(inc); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it’s a bit confusing to mix validation and the actual calls to _nice
… could you separate them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split them up.:)
Also: Your author name in this commit is given as “zokker13”. Is that intended or do you prefer to be listed (changelog, git log, AUTHORS file) with some other name? People typically prefer their full name, but ultimately it’s up to you. |
doc/api/process.md
Outdated
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. |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
84d744e
to
0b5707b
Compare
@addaleax Thanks for the review and the lightning fast response. Added your requests. Hope that "fixup" -commit is appropriate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks!
return _nice(inc); | ||
} else if (tInc === 'undefined') { | ||
return _nice(inc); | ||
} else if (typeof inc === 'undefined') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can just combine this with the below
function validateInc(inc) {
if (typeof inc === 'number') {
validateInt32(inc, 'nice');
} else if (inc !== undefined) {
throw new ERR_INVALID_ARG_TYPE('nice', ['number', 'undefined'], inc);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, totally missed that!
Here it is: 655894e745de20829ff40ee330557da1305f9c7e
CI: https://ci.nodejs.org/job/node-test-pull-request/15747/ As a heads up, our rules will require this PR to stay open for 72 hours so people have a chance to weigh in, and you may have to update this one or two times more during that time. But as first PRs go, this is definitely a first-class example. :) |
doc/api/process.md
Outdated
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a sentence fragment. You can make it a complete sentence by removing Though,
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For those not familiar with the 'nice' system, the two different uses of 'increase' here might be confusing. At the very least it would be a good idea to note that increasing the 'nice' value means lowering the process priority. This is hinted at in the paragraph above this one, but I still think using 'increase' back to back here could confuse some people.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda changed the entire thing: a099642629b36e908c5114227c4fd8e9c18cc12f
doc/api/process.md
Outdated
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! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove **NOTE**:
, the exclamation point, and use of you
.
Maybe this?:
The
nice
value can be increased only. It can not be decreased.
...or...
The
nice
value can never be decreased except by the super user.
...or something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in a way that is (hopefully) better readable: a099642629b36e908c5114227c4fd8e9c18cc12f
Hi, @zokker13! Welcome and thanks for the PR! I have two requests for the documentation you've added:
These (well, the second one) are noted in |
Feels notable enough that pinging @nodejs/tsc seems appropriate. Also pinging @nodejs/documentation for any other comments on the docs. |
doc/api/process.md
Outdated
``` | ||
|
||
This function is only available on POSIX platforms (i.e. not Windows or | ||
Android). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence is confusing as Android is about as POSIX compliant as Linux is. It would be better to leave that part out and just say that the function is not available on Windows or Android.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added Android support for this PR: a099642629b36e908c5114227c4fd8e9c18cc12f
doc/api/process.md
Outdated
added: REPLACEME | ||
--> | ||
|
||
* `inc` {integer} The new nice value for the process. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should include return value information here since this function always returns a value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check. a099642629b36e908c5114227c4fd8e9c18cc12f
Is it not possible to somehow bridge Also, from what I'm reading, Android does support |
Does this work on non-linuxies? I also wonder if the underlying impl isn’t better suited for libuv. |
@mscdex Hey, @Fishrock123 Yes, it should work for various POSIX compliant OS'. There are subtle differences between them though. But those differences are limited to the range of the niceness. So perhaps a reference to the min/max niceness would be fitting and convenient to the user (or at least a documentation). |
[ I am just thinking aload here, please don't consider as a negative remark on the PR. ] are there any benchmarks that provide insights on scheduling behavior and its implications on the workload with default priority? I believe most of the UNIX schedulers have special optimizations for I/O workload, so wondering whether manual modification will influence the OS logic and thereby the overall performance positively or negatively. Also wondering about the characteristics of stream velocity / backpressure etc. under a cluster, child process, IPC toppologies when processes are running with manually preset scheduling priorities. If we don't have these info or cannot determine, it would be worthwhile considering writing caveats in the doc (or else we can get unwanted issues that complain about not getting desired througput after altering the nice etc.) on these lines: the overall efficiency of Node programs when catering to highly interactive workloads depend on peer end points resident in the host or otherwise. So altering the nice value of the process should be performed carefully, only after running your performance benchmark under full load and realizing any desired improvements. |
I imagine someone at some point will want the Windows equivalent in core because
@zokker13 https://android.googlesource.com/platform/bionic.git/+/master/libc/upstream-netbsd/lib/libc/gen/nice.c |
@gireeshpunathil I don’t necessarily think it’s our job to talk about these things in the docs for |
ok, sure! |
the specified permissions are set). (See getrlimit(2).) | ||
|
||
```js | ||
if (process.platform !== 'win32') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think having this is necessary, especially since we already document Windows is not supported below this example.
if (_setgid !== undefined) { | ||
setupPosixMethods(_initgroups, _setegid, _seteuid, | ||
_setgid, _setuid, _setgroups); | ||
} | ||
|
||
process.nice = function nice(inc) { | ||
if (process.platform === 'win32') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps change this so the platform check is only run once when this file is evaluated, since the process platform will not change during runtime. Example:
if (process.platform === 'win32') {
process.nice = function nice(inc) {
throw new ERR_METHOD_NOT_IMPLEMENTED('nice()');
};
} else {
process.nice = function nice(inc) {
validateInc(inc);
return _nice(inc);
};
}
or something similar
const assert = require('assert'); | ||
|
||
if (common.isWindows || !common.isMainThread) { | ||
assert.strictEqual(process.nice, undefined); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no longer valid since we're now throwing.
However, I feel like we should just call common.skip()
with an appropriate message to make it obvious that we're not testing the real implementation.
} | ||
); | ||
|
||
[null, false, true, {}, [], () => {}, 'text'].forEach((val) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about also testing that passing '0' or undefined
results in a finite integer being returned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, we might want to test that something like process.nice(1)
works as expected.
Mh.. okay guys, I thought about having a more neutral function (say So I'd like to close this for now, go visit the libuv fellows and come back to implement a neutral call here. |
@zokker13 I think this PR is still in pretty good shape (I personally disagree about being inconsistent with other functions re: Windows, but, oh well). Can we mark this PR as |
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.
bd1e086
to
aadb65b
Compare
You'd probably want to target https://github.com/libuv/libuv/blob/v1.x/src/unix/core.c and https://github.com/libuv/libuv/blob/v1.x/src/win/util.c. |
Refs: nodejs/node#21675 PR-URL: libuv#1945 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Refs: nodejs/node#21675 PR-URL: #1945 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Notable changes: - Restores compatibility with the old IPC protocol. - Adds uv_open_osfhandle(). - Adds uv_os_{get,set}priority(). PR-URL: nodejs#22365 Fixes: nodejs#21671 Fixes: nodejs#15433 Refs: nodejs#21675 Refs: nodejs/node-addon-api#304 Refs: nodejs/abi-stable-node#318 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Notable changes: - Restores compatibility with the old IPC protocol. - Adds uv_open_osfhandle(). - Adds uv_os_{get,set}priority(). PR-URL: #22365 Fixes: #21671 Fixes: #15433 Refs: #21675 Refs: nodejs/node-addon-api#304 Refs: nodejs/abi-stable-node#318 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Notable changes: - Restores compatibility with the old IPC protocol. - Adds uv_open_osfhandle(). - Adds uv_os_{get,set}priority(). PR-URL: #22365 Fixes: #21671 Fixes: #15433 Refs: #21675 Refs: nodejs/node-addon-api#304 Refs: nodejs/abi-stable-node#318 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Superseded by the now-merged #22407 |
Notable changes: - Restores compatibility with the old IPC protocol. - Adds uv_open_osfhandle(). - Adds uv_os_{get,set}priority(). PR-URL: nodejs#22365 Fixes: nodejs#21671 Fixes: nodejs#15433 Refs: nodejs#21675 Refs: nodejs/node-addon-api#304 Refs: nodejs/abi-stable-node#318 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Notable changes: - Restores compatibility with the old IPC protocol. - Adds uv_open_osfhandle(). - Adds uv_os_{get,set}priority(). Backport-PR-URL: #24103 PR-URL: #22365 Fixes: #21671 Fixes: #15433 Refs: #21675 Refs: nodejs/node-addon-api#304 Refs: nodejs/abi-stable-node#318 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
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.
If desired, here's the technical documentation: http://man7.org/linux/man-pages/man2/nice.2.html
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes