Skip to content

Commit dc1c95e

Browse files
theanarkhRafaelGSS
authored andcommitted
src: trace fs async api
PR-URL: #44057 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 0f4e98b commit dc1c95e

File tree

4 files changed

+585
-27
lines changed

4 files changed

+585
-27
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

+55-9
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,57 @@ using v8::Object;
4242
using v8::ObjectTemplate;
4343
using v8::Value;
4444

45+
static const char* get_dir_func_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
4660
#define GET_TRACE_ENABLED \
47-
(*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
48-
(TRACING_CATEGORY_NODE2(fs_dir, sync)) != 0)
61+
(*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( \
62+
TRACING_CATEGORY_NODE2(fs_dir, sync)) != 0)
4963
#define FS_DIR_SYNC_TRACE_BEGIN(syscall, ...) \
5064
if (GET_TRACE_ENABLED) \
51-
TRACE_EVENT_BEGIN(TRACING_CATEGORY_NODE2(fs_dir, sync), TRACE_NAME(syscall), \
52-
##__VA_ARGS__);
65+
TRACE_EVENT_BEGIN(TRACING_CATEGORY_NODE2(fs_dir, sync), \
66+
TRACE_NAME(syscall), \
67+
##__VA_ARGS__);
5368
#define FS_DIR_SYNC_TRACE_END(syscall, ...) \
5469
if (GET_TRACE_ENABLED) \
55-
TRACE_EVENT_END(TRACING_CATEGORY_NODE2(fs_dir, sync), TRACE_NAME(syscall), \
56-
##__VA_ARGS__);
70+
TRACE_EVENT_END(TRACING_CATEGORY_NODE2(fs_dir, sync), \
71+
TRACE_NAME(syscall), \
72+
##__VA_ARGS__);
73+
74+
#define FS_DIR_ASYNC_TRACE_BEGIN0(fs_type, id) \
75+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(TRACING_CATEGORY_NODE2(fs_dir, async), \
76+
get_dir_func_name_by_type(fs_type), \
77+
id);
78+
#define FS_DIR_ASYNC_TRACE_END0(fs_type, id) \
79+
TRACE_EVENT_NESTABLE_ASYNC_END0(TRACING_CATEGORY_NODE2(fs_dir, async), \
80+
get_dir_func_name_by_type(fs_type), \
81+
id);
82+
83+
#define FS_DIR_ASYNC_TRACE_BEGIN1(fs_type, id, name, value) \
84+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE2(fs_dir, async), \
85+
get_dir_func_name_by_type(fs_type), \
86+
id, \
87+
name, \
88+
value);
89+
90+
#define FS_DIR_ASYNC_TRACE_END1(fs_type, id, name, value) \
91+
TRACE_EVENT_NESTABLE_ASYNC_END1(TRACING_CATEGORY_NODE2(fs_dir, async), \
92+
get_dir_func_name_by_type(fs_type), \
93+
id, \
94+
name, \
95+
value);
5796

5897
DirHandle::DirHandle(Environment* env, Local<Object> obj, uv_dir_t* dir)
5998
: AsyncWrap(env, obj, AsyncWrap::PROVIDER_DIRHANDLE),
@@ -132,7 +171,8 @@ inline void DirHandle::GCClose() {
132171
void AfterClose(uv_fs_t* req) {
133172
FSReqBase* req_wrap = FSReqBase::from_req(req);
134173
FSReqAfterScope after(req_wrap, req);
135-
174+
FS_DIR_ASYNC_TRACE_END1(
175+
req->fs_type, req_wrap, "result", static_cast<int>(req->result))
136176
if (after.Proceed())
137177
req_wrap->Resolve(Undefined(req_wrap->env()->isolate()));
138178
}
@@ -151,6 +191,7 @@ void DirHandle::Close(const FunctionCallbackInfo<Value>& args) {
151191

152192
FSReqBase* req_wrap_async = GetReqWrap(args, 0);
153193
if (req_wrap_async != nullptr) { // close(req)
194+
FS_DIR_ASYNC_TRACE_BEGIN0(UV_FS_CLOSEDIR, req_wrap_async)
154195
AsyncCall(env, req_wrap_async, args, "closedir", UTF8, AfterClose,
155196
uv_fs_closedir, dir->dir());
156197
} else { // close(undefined, ctx)
@@ -196,7 +237,8 @@ static MaybeLocal<Array> DirentListToArray(
196237
static void AfterDirRead(uv_fs_t* req) {
197238
BaseObjectPtr<FSReqBase> req_wrap { FSReqBase::from_req(req) };
198239
FSReqAfterScope after(req_wrap.get(), req);
199-
240+
FS_DIR_ASYNC_TRACE_END1(
241+
req->fs_type, req_wrap, "result", static_cast<int>(req->result))
200242
if (!after.Proceed()) {
201243
return;
202244
}
@@ -256,6 +298,7 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
256298

257299
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
258300
if (req_wrap_async != nullptr) { // dir.read(encoding, bufferSize, req)
301+
FS_DIR_ASYNC_TRACE_BEGIN0(UV_FS_READDIR, req_wrap_async)
259302
AsyncCall(env, req_wrap_async, args, "readdir", encoding,
260303
AfterDirRead, uv_fs_readdir, dir->dir());
261304
} else { // dir.read(encoding, bufferSize, undefined, ctx)
@@ -298,7 +341,8 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
298341
void AfterOpenDir(uv_fs_t* req) {
299342
FSReqBase* req_wrap = FSReqBase::from_req(req);
300343
FSReqAfterScope after(req_wrap, req);
301-
344+
FS_DIR_ASYNC_TRACE_END1(
345+
req->fs_type, req_wrap, "result", static_cast<int>(req->result))
302346
if (!after.Proceed()) {
303347
return;
304348
}
@@ -325,6 +369,8 @@ static void OpenDir(const FunctionCallbackInfo<Value>& args) {
325369

326370
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
327371
if (req_wrap_async != nullptr) { // openDir(path, encoding, req)
372+
FS_DIR_ASYNC_TRACE_BEGIN1(
373+
UV_FS_OPENDIR, req_wrap_async, "path", TRACE_STR_COPY(*path))
328374
AsyncCall(env, req_wrap_async, args, "opendir", encoding, AfterOpenDir,
329375
uv_fs_opendir, *path);
330376
} else { // openDir(path, encoding, undefined, ctx)

0 commit comments

Comments
 (0)