Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,30 @@ process.kill(process.pid, 'SIGHUP');
When `SIGUSR1` is received by a Node.js process, Node.js will start the
debugger. See [Signal Events][].

## process.LIBUV\_HANDLE\_TYPES

This object indicates type enums for [libuv handles][] types.

## process.libuvHandlesCount()

Get [libuv handles][] count for current tick.

The returned object contains two fileds:

```json
{
total: <TOTAL_COUNT>,
each: [ <TYPE_1_COUNT>, <TYPE_2_COUNT>, ... ]
}
```

You may get type enum via `process.LIBUV_HANDLE_TYPES`. e.g.

```js
const result = process.libuvHandlesCount();
result.each[process.LIBUV_HANDLE_TYPES.ASYNC]; // Count of `uv_async_t`
```

## process.mainModule
<!-- YAML
added: v0.1.17
Expand Down Expand Up @@ -2328,3 +2352,4 @@ cases:
[process_emit_warning]: #process_process_emitwarning_warning_type_code_ctor
[process_warning]: #process_event_warning
[report documentation]: report.html
[libuv handles]: http://docs.libuv.org/en/v1.x/handle.html
30 changes: 30 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,39 @@ if (ownsProcessState) {
// process.config is serialized config.gypi
process.config = JSON.parse(internalBinding('native_module').config);

function libuvHandlesCount() {
const arr = rawMethods.libuvHandlesCount();
return {
total: arr.reduce((sum, v) => sum + v, 0),
each: arr,
};
}

process.LIBUV_HANDLE_TYPES = {
Copy link
Member

@joyeecheung joyeecheung Apr 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not hard code this in JS land, this can be attached in the binding - e.g. see require('os').constants

UNKNOWN_HANDLE: 0,
ASYNC: 1,
CHECK: 2,
FS_EVENT: 3,
FS_POLL: 4,
HANDLE: 5,
IDLE: 6,
NAMED_PIPE: 7,
POLL: 8,
PREPARE: 9,
PROCESS: 10,
STREAM: 11,
TCP: 12,
TIMER: 13,
TTY: 14,
UDP: 15,
SIGNAL: 16,
FILE: 17,
};

const rawMethods = internalBinding('process_methods');
// Set up methods and events on the process object for the main thread
if (isMainThread) {
process.libuvHandlesCount = libuvHandlesCount;
process.abort = rawMethods.abort;
const wrapped = mainThreadSetup.wrapProcessMethods(rawMethods);
process.umask = wrapped.umask;
Expand Down
39 changes: 39 additions & 0 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ typedef int mode_t;
#include <termios.h> // tcgetattr, tcsetattr
#endif

typedef std::vector<uv_handle_t*> libuv_handle_array;
unsigned int _libuv_handles_each_count[UV_HANDLE_TYPE_MAX];
unsigned int _libuv_handles_each_count_size =
sizeof(unsigned int) * UV_HANDLE_TYPE_MAX;

namespace node {

using v8::Array;
Expand Down Expand Up @@ -399,6 +404,39 @@ static void ReallyExit(const FunctionCallbackInfo<Value>& args) {
env->Exit(code);
}

void uv_handle_count_walk(uv_handle_t* handle, void* arg) {
libuv_handle_array* array = reinterpret_cast<libuv_handle_array*>arg;
array->push_back(handle);
}

static void LibuvCount(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();

uv_handle_type temp_type;
libuv_handle_array handles;
uv_walk(
env->event_loop(),
uv_handle_count_walk,
reinterpret_cast<void*>&handles);

memset(_libuv_handles_each_count, 0, _libuv_handles_each_count_size);
for (unsigned int i = 0; i < handles.size(); i++) {
temp_type = handles[i]->type;
if (temp_type >= UV_HANDLE_TYPE_MAX || temp_type < 0) {
temp_type = UV_UNKNOWN_HANDLE;
}
_libuv_handles_each_count[(unsigned int)temp_type]++;
}

std::vector<Local<Value>> result(UV_HANDLE_TYPE_MAX);
for (unsigned int i = 0; i < UV_HANDLE_TYPE_MAX; i++) {
result[i] = Number::New(isolate, _libuv_handles_each_count[i]);
}

args.GetReturnValue().Set(Array::New(isolate, result.data(), result.size()));
}

static void InitializeProcessMethods(Local<Object> target,
Local<Value> unused,
Local<Context> context,
Expand Down Expand Up @@ -428,6 +466,7 @@ static void InitializeProcessMethods(Local<Object> target,
env->SetMethod(target, "_getActiveHandles", GetActiveHandles);
env->SetMethod(target, "_kill", Kill);

env->SetMethod(target, "libuvHandlesCount", LibuvCount);
env->SetMethodNoSideEffect(target, "cwd", Cwd);
env->SetMethod(target, "dlopen", binding::DLOpen);
env->SetMethod(target, "reallyExit", ReallyExit);
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-process-libuv-handles-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const common = require('../common');
const assert = require('assert');

if (!common.isMainThread)
common.skip('process.libuvHandlesCount is not available in Workers');

const count = process.libuvHandlesCount();

assert.strictEqual(typeof count.total, 'number');
assert.strictEqual(count.each.length, 18);

const sum = count.each.reduce((sum, c) => sum + c, 0);
assert.strictEqual(sum, count.total);