Skip to content

Commit 3bd3676

Browse files
committed
src: trace fs async api
1 parent 327030e commit 3bd3676

File tree

4 files changed

+571
-26
lines changed

4 files changed

+571
-26
lines changed

doc/api/tracing.md

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ The available categories are:
2626
* `node.net.native`: Enables capture of trace data for network.
2727
* `node.environment`: Enables capture of Node.js Environment milestones.
2828
* `node.fs.sync`: Enables capture of trace data for file system sync methods.
29+
* `node.fs_dir.sync`: Enables capture of trace data for file system sync
30+
directory methods.
31+
* `node.fs.async`: Enables capture of trace data for file system async methods.
32+
* `node.fs_dir.async`: Enables capture of trace data for file system async
33+
directory methods.
2934
* `node.perf`: Enables capture of [Performance API][] measurements.
3035
* `node.perf.usertiming`: Enables capture of only Performance API User Timing
3136
measures and marks.

src/node_dir.cc

+53-11
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,54 @@ using v8::Object;
4242
using v8::ObjectTemplate;
4343
using v8::Value;
4444

45+
static const char* get_fs_name_by_type(uv_fs_type req_type) {
46+
switch (req_type) {
47+
#define FS_TYPE_TO_NAME(type, name) \
48+
case UV_FS_##type: \
49+
return name;
50+
FS_TYPE_TO_NAME(OPENDIR, "opendir")
51+
FS_TYPE_TO_NAME(READDIR, "readdir")
52+
FS_TYPE_TO_NAME(CLOSEDIR, "closedir")
53+
#undef FS_TYPE_TO_NAME
54+
default:
55+
return "unknow";
56+
}
57+
}
58+
4559
#define TRACE_NAME(name) "fs_dir.sync." #name
46-
#define GET_TRACE_ENABLED \
47-
(*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
60+
#define GET_TRACE_ENABLED \
61+
(*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
4862
(TRACING_CATEGORY_NODE2(fs_dir, sync)) != 0)
49-
#define FS_DIR_SYNC_TRACE_BEGIN(syscall, ...) \
50-
if (GET_TRACE_ENABLED) \
51-
TRACE_EVENT_BEGIN(TRACING_CATEGORY_NODE2(fs_dir, sync), TRACE_NAME(syscall), \
63+
#define FS_DIR_SYNC_TRACE_BEGIN(syscall, ...) \
64+
if (GET_TRACE_ENABLED) \
65+
TRACE_EVENT_BEGIN(TRACING_CATEGORY_NODE2(fs_dir, sync), TRACE_NAME(syscall), \
5266
##__VA_ARGS__);
53-
#define FS_DIR_SYNC_TRACE_END(syscall, ...) \
54-
if (GET_TRACE_ENABLED) \
55-
TRACE_EVENT_END(TRACING_CATEGORY_NODE2(fs_dir, sync), TRACE_NAME(syscall), \
67+
#define FS_DIR_SYNC_TRACE_END(syscall, ...) \
68+
if (GET_TRACE_ENABLED) \
69+
TRACE_EVENT_END(TRACING_CATEGORY_NODE2(fs_dir, sync), TRACE_NAME(syscall), \
5670
##__VA_ARGS__);
5771

72+
#define FS_ASYNC_TRACE_BEGIN0(fs_type, id) \
73+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(TRACING_CATEGORY_NODE2(fs_dir, async), \
74+
get_fs_name_by_type(fs_type), \
75+
id);
76+
#define FS_ASYNC_TRACE_END0(fs_type, id) \
77+
TRACE_EVENT_NESTABLE_ASYNC_END0(TRACING_CATEGORY_NODE2(fs_dir, async), \
78+
get_fs_name_by_type(fs_type), \
79+
id);
80+
81+
#define FS_ASYNC_TRACE_BEGIN1(fs_type, id, ...) \
82+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE2(fs_dir, async), \
83+
get_fs_name_by_type(fs_type), \
84+
id, \
85+
##__VA_ARGS__);
86+
87+
#define FS_ASYNC_TRACE_END1(fs_type, id, ...) \
88+
TRACE_EVENT_NESTABLE_ASYNC_END1(TRACING_CATEGORY_NODE2(fs_dir, async), \
89+
get_fs_name_by_type(fs_type), \
90+
id, \
91+
##__VA_ARGS__);
92+
5893
DirHandle::DirHandle(Environment* env, Local<Object> obj, uv_dir_t* dir)
5994
: AsyncWrap(env, obj, AsyncWrap::PROVIDER_DIRHANDLE),
6095
dir_(dir) {
@@ -132,7 +167,8 @@ inline void DirHandle::GCClose() {
132167
void AfterClose(uv_fs_t* req) {
133168
FSReqBase* req_wrap = FSReqBase::from_req(req);
134169
FSReqAfterScope after(req_wrap, req);
135-
170+
FS_ASYNC_TRACE_END1(
171+
req->fs_type, req_wrap, "result", static_cast<int>(req->result))
136172
if (after.Proceed())
137173
req_wrap->Resolve(Undefined(req_wrap->env()->isolate()));
138174
}
@@ -151,6 +187,7 @@ void DirHandle::Close(const FunctionCallbackInfo<Value>& args) {
151187

152188
FSReqBase* req_wrap_async = GetReqWrap(args, 0);
153189
if (req_wrap_async != nullptr) { // close(req)
190+
FS_ASYNC_TRACE_BEGIN0(UV_FS_CLOSEDIR, req_wrap_async)
154191
AsyncCall(env, req_wrap_async, args, "closedir", UTF8, AfterClose,
155192
uv_fs_closedir, dir->dir());
156193
} else { // close(undefined, ctx)
@@ -196,7 +233,8 @@ static MaybeLocal<Array> DirentListToArray(
196233
static void AfterDirRead(uv_fs_t* req) {
197234
BaseObjectPtr<FSReqBase> req_wrap { FSReqBase::from_req(req) };
198235
FSReqAfterScope after(req_wrap.get(), req);
199-
236+
FS_ASYNC_TRACE_END1(
237+
req->fs_type, req_wrap, "result", static_cast<int>(req->result))
200238
if (!after.Proceed()) {
201239
return;
202240
}
@@ -256,6 +294,7 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
256294

257295
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
258296
if (req_wrap_async != nullptr) { // dir.read(encoding, bufferSize, req)
297+
FS_ASYNC_TRACE_BEGIN0(UV_FS_READDIR, req_wrap_async)
259298
AsyncCall(env, req_wrap_async, args, "readdir", encoding,
260299
AfterDirRead, uv_fs_readdir, dir->dir());
261300
} else { // dir.read(encoding, bufferSize, undefined, ctx)
@@ -298,7 +337,8 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
298337
void AfterOpenDir(uv_fs_t* req) {
299338
FSReqBase* req_wrap = FSReqBase::from_req(req);
300339
FSReqAfterScope after(req_wrap, req);
301-
340+
FS_ASYNC_TRACE_END1(
341+
req->fs_type, req_wrap, "result", static_cast<int>(req->result))
302342
if (!after.Proceed()) {
303343
return;
304344
}
@@ -325,6 +365,8 @@ static void OpenDir(const FunctionCallbackInfo<Value>& args) {
325365

326366
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
327367
if (req_wrap_async != nullptr) { // openDir(path, encoding, req)
368+
FS_ASYNC_TRACE_BEGIN1(
369+
UV_FS_OPENDIR, req_wrap_async, "path", TRACE_STR_COPY(*path))
328370
AsyncCall(env, req_wrap_async, args, "opendir", encoding, AfterOpenDir,
329371
uv_fs_opendir, *path);
330372
} else { // openDir(path, encoding, undefined, ctx)

0 commit comments

Comments
 (0)