forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiled_syscalls.cpp
274 lines (207 loc) Β· 9.36 KB
/
profiled_syscalls.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (c) 2022-2023, Jakub Berkop <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/FileSystem/OpenFileDescription.h>
#include <Kernel/Tasks/PerformanceManager.h>
#include <Kernel/Tasks/Process.h>
namespace Kernel {
ErrorOr<NonnullRefPtr<OpenFileDescription>> open_readable_file_description(auto& fds, int fd);
static ErrorOr<size_t> get_path_index(auto& fds, int fd, PerformanceEventBuffer* event_buffer)
{
auto description = TRY(open_readable_file_description(fds, fd));
if (auto path = description->original_absolute_path(); !path.is_error()) {
return TRY(event_buffer->register_string(move(path.value())));
} else if (auto pseudo_path = description->pseudo_path(); !pseudo_path.is_error()) {
return TRY(event_buffer->register_string(move(pseudo_path.value())));
} else {
auto invalid_path_string = TRY(KString::try_create("<INVALID_FILE_PATH>"sv)); // TODO: Performance, unecessary allocations.
return TRY(event_buffer->register_string(move(invalid_path_string)));
}
}
// FIXME: Following functions are very similar, and could be refactored into a more generic solution.
// However, it's not clear how to do it in a way that would be easy to read and understand.
ErrorOr<FlatPtr> Process::sys$open(Userspace<Syscall::SC_open_params const*> user_params)
{
Optional<MonotonicTime> start_timestamp = {};
// We have to check whether profiling is enabled at before going into the syscall implementation
// so that we can measure the time it took to execute the syscall.
// This approach ensures that we don't have a race condition in case profiling was enabled during
// the execution of the syscall.
// If profiling is disabled at the beginning, we don't want to call TimeManagement::the().monotonic_time()
// because of the overhead it would introduce for every syscall.
bool const profiling_enabled_at_entry = !Thread::current()->is_profiling_suppressed();
if (profiling_enabled_at_entry) {
start_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
}
auto const params = TRY(copy_typed_from_user(user_params));
auto result = open_impl(user_params);
if (!profiling_enabled_at_entry || Thread::current()->is_profiling_suppressed())
return result;
auto* event_buffer = current_perf_events_buffer();
if (event_buffer == nullptr)
return result;
auto const end_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
auto const duration = end_timestamp - start_timestamp.value();
FilesystemEvent data;
data.type = FilesystemEventType::Open;
data.durationNs = static_cast<u64>(duration.to_nanoseconds());
if (result.is_error()) {
data.result.is_error = true;
data.result.value = result.error().code();
} else {
data.result.is_error = false;
data.result.value = 0;
}
auto path = get_syscall_path_argument(params.path);
if (!path.is_error()) {
auto value = event_buffer->register_string(move(path.value()));
data.data.open.filename_index = value.value();
}
data.data.open.dirfd = params.dirfd;
data.data.open.options = params.options;
data.data.open.mode = params.mode;
(void)event_buffer->append(PERF_EVENT_FILESYSTEM, 0, 0, {}, Thread::current(), data);
return result;
}
ErrorOr<FlatPtr> Process::sys$close(int fd)
{
Optional<MonotonicTime> start_timestamp = {};
bool const profiling_enabled_at_entry = !Thread::current()->is_profiling_suppressed();
if (profiling_enabled_at_entry) {
start_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
}
auto result = close_impl(fd);
if (Thread::current()->is_profiling_suppressed())
return result;
if (!profiling_enabled_at_entry || Thread::current()->is_profiling_suppressed())
return result;
auto* event_buffer = current_perf_events_buffer();
if (event_buffer == nullptr)
return result;
auto const end_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
auto const duration = end_timestamp - start_timestamp.value();
FilesystemEvent data;
data.type = FilesystemEventType::Close;
data.durationNs = static_cast<u64>(duration.to_nanoseconds());
data.data.close.fd = fd;
if (result.is_error()) {
data.result.is_error = true;
data.result.value = result.error().code();
} else {
data.result.is_error = false;
data.result.value = 0;
}
auto maybe_path_index = get_path_index(fds(), fd, event_buffer);
if (maybe_path_index.is_error())
return result;
data.data.close.filename_index = maybe_path_index.value();
(void)event_buffer->append(PERF_EVENT_FILESYSTEM, 0, 0, {}, Thread::current(), data);
return result;
}
ErrorOr<FlatPtr> Process::sys$readv(int fd, Userspace<const struct iovec*> iov, int iov_count)
{
Optional<MonotonicTime> start_timestamp = {};
bool const profiling_enabled_at_entry = !Thread::current()->is_profiling_suppressed();
if (profiling_enabled_at_entry) {
start_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
}
auto result = readv_impl(fd, iov, iov_count);
if (!profiling_enabled_at_entry || Thread::current()->is_profiling_suppressed())
return result;
if (Thread::current()->is_profiling_suppressed())
return result;
auto* event_buffer = current_perf_events_buffer();
if (event_buffer == nullptr)
return result;
auto const end_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
auto const duration = end_timestamp - start_timestamp.value();
FilesystemEvent data;
data.type = FilesystemEventType::Readv;
data.durationNs = static_cast<u64>(duration.to_nanoseconds());
data.data.readv.fd = fd;
if (result.is_error()) {
data.result.is_error = true;
data.result.value = result.error().code();
} else {
data.result.is_error = false;
data.result.value = 0;
}
auto maybe_path_index = get_path_index(fds(), fd, event_buffer);
if (maybe_path_index.is_error())
return result;
data.data.readv.filename_index = maybe_path_index.value();
(void)event_buffer->append(PERF_EVENT_FILESYSTEM, 0, 0, {}, Thread::current(), data);
return result;
}
ErrorOr<FlatPtr> Process::sys$read(int fd, Userspace<u8*> buffer, size_t size)
{
Optional<MonotonicTime> start_timestamp = {};
bool const profiling_enabled_at_entry = !Thread::current()->is_profiling_suppressed();
if (profiling_enabled_at_entry) {
start_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
}
auto result = read_impl(fd, buffer, size);
if (!profiling_enabled_at_entry || Thread::current()->is_profiling_suppressed())
return result;
auto* event_buffer = current_perf_events_buffer();
if (event_buffer == nullptr)
return result;
auto const end_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
auto const duration = end_timestamp - start_timestamp.value();
FilesystemEvent data;
data.type = FilesystemEventType::Read;
data.durationNs = static_cast<u64>(duration.to_nanoseconds());
data.data.read.fd = fd;
if (result.is_error()) {
data.result.is_error = true;
data.result.value = result.error().code();
} else {
data.result.is_error = false;
data.result.value = 0;
}
auto maybe_path_index = get_path_index(fds(), fd, event_buffer);
if (maybe_path_index.is_error())
return result;
data.data.read.filename_index = maybe_path_index.value();
(void)event_buffer->append(PERF_EVENT_FILESYSTEM, 0, 0, {}, Thread::current(), data);
return result;
}
ErrorOr<FlatPtr> Process::sys$pread(int fd, Userspace<u8*> buffer, size_t size, off_t userspace_offset)
{
Optional<MonotonicTime> start_timestamp = {};
bool const profiling_enabled_at_entry = !Thread::current()->is_profiling_suppressed();
if (profiling_enabled_at_entry) {
start_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
}
auto result = pread_impl(fd, buffer, size, userspace_offset);
if (!profiling_enabled_at_entry || Thread::current()->is_profiling_suppressed())
return result;
auto* event_buffer = current_perf_events_buffer();
if (event_buffer == nullptr)
return result;
auto const end_timestamp = TimeManagement::the().monotonic_time(TimePrecision::Precise);
auto const duration = end_timestamp - start_timestamp.value();
FilesystemEvent data;
data.type = FilesystemEventType::Pread;
data.durationNs = static_cast<u64>(duration.to_nanoseconds());
data.data.pread.fd = fd;
data.data.pread.buffer_ptr = buffer.ptr();
data.data.pread.size = size;
data.data.pread.offset = userspace_offset;
if (result.is_error()) {
data.result.is_error = true;
data.result.value = result.error().code();
} else {
data.result.is_error = false;
data.result.value = 0;
}
auto maybe_path_index = get_path_index(fds(), fd, event_buffer);
if (maybe_path_index.is_error())
return result;
data.data.pread.filename_index = maybe_path_index.value();
(void)event_buffer->append(PERF_EVENT_FILESYSTEM, 0, 0, {}, Thread::current(), data);
return result;
}
}