Skip to content

Commit e8a2546

Browse files
committed
[EventPipe][DiagnosticServer] Add poll interface to IpcStreams
1 parent a131d0b commit e8a2546

File tree

9 files changed

+138
-28
lines changed

9 files changed

+138
-28
lines changed

src/native/eventpipe/ds-ipc-pal-namedpipe.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ ds_ipc_poll (
213213
handles [i] = poll_handles_data [i].ipc->overlap.hEvent;
214214
if (handles [i] == INVALID_HANDLE_VALUE) {
215215
// Invalid handle, wait will fail. Signal error
216-
poll_handles_data [i].events = DS_IPC_POLL_EVENTS_ERR;
216+
poll_handles_data [i].events = EP_IPC_POLL_EVENTS_ERR;
217217
}
218218
} else {
219219
// CLIENT
@@ -238,7 +238,7 @@ ds_ipc_poll (
238238
handles [i] = poll_handles_data [i].stream->overlap.hEvent;
239239
break;
240240
case ERROR_PIPE_NOT_CONNECTED:
241-
poll_handles_data [i].events = (uint8_t)DS_IPC_POLL_EVENTS_HANGUP;
241+
poll_handles_data [i].events = (uint8_t)EP_IPC_POLL_EVENTS_HANGUP;
242242
result = -1;
243243
ep_raise_error ();
244244
default:
@@ -288,7 +288,7 @@ ds_ipc_poll (
288288
// check if we abandoned something
289289
DWORD abandonedIndex = wait - WAIT_ABANDONED_0;
290290
if (abandonedIndex > 0 || abandonedIndex < (poll_handles_data_len - 1)) {
291-
poll_handles_data [abandonedIndex].events = (uint8_t)DS_IPC_POLL_EVENTS_HANGUP;
291+
poll_handles_data [abandonedIndex].events = (uint8_t)EP_IPC_POLL_EVENTS_HANGUP;
292292
result = -1;
293293
ep_raise_error ();
294294
} else {
@@ -325,20 +325,20 @@ ds_ipc_poll (
325325
if (!success) {
326326
DWORD error = GetLastError();
327327
if (error == ERROR_PIPE_NOT_CONNECTED || error == ERROR_BROKEN_PIPE) {
328-
poll_handles_data [index].events = (uint8_t)DS_IPC_POLL_EVENTS_HANGUP;
328+
poll_handles_data [index].events = (uint8_t)EP_IPC_POLL_EVENTS_HANGUP;
329329
} else {
330330
if (callback)
331331
callback ("Client connection error", error);
332-
poll_handles_data [index].events = (uint8_t)DS_IPC_POLL_EVENTS_ERR;
332+
poll_handles_data [index].events = (uint8_t)EP_IPC_POLL_EVENTS_ERR;
333333
result = -1;
334334
ep_raise_error ();
335335
}
336336
} else {
337-
poll_handles_data [index].events = (uint8_t)DS_IPC_POLL_EVENTS_SIGNALED;
337+
poll_handles_data [index].events = (uint8_t)EP_IPC_POLL_EVENTS_SIGNALED;
338338
}
339339
} else {
340340
// SERVER
341-
poll_handles_data [index].events = (uint8_t)DS_IPC_POLL_EVENTS_SIGNALED;
341+
poll_handles_data [index].events = (uint8_t)EP_IPC_POLL_EVENTS_SIGNALED;
342342
}
343343

344344
result = 1;
@@ -834,12 +834,23 @@ ipc_stream_close_func (void *object)
834834
return ds_ipc_stream_close (ipc_stream, NULL);
835835
}
836836

837+
static
838+
EventPipeIpcPollEvents
839+
ipc_stream_poll_func (
840+
void *object,
841+
uint32_t timeout_ms)
842+
{
843+
// Needs to be implemented.
844+
return EP_IPC_POLL_EVENTS_UNKNOWN;
845+
}
846+
837847
static IpcStreamVtable ipc_stream_vtable = {
838848
ipc_stream_free_func,
839849
ipc_stream_read_func,
840850
ipc_stream_write_func,
841851
ipc_stream_flush_func,
842-
ipc_stream_close_func };
852+
ipc_stream_close_func,
853+
ipc_stream_poll_func };
843854

844855
static
845856
DiagnosticsIpcStream *

src/native/eventpipe/ds-ipc-pal-socket.c

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,15 +1146,15 @@ ds_ipc_poll (
11461146
// check for hangup first because a closed socket
11471147
// will technically meet the requirements for POLLIN
11481148
// i.e., a call to recv/read won't block
1149-
poll_handles_data [i].events = (uint8_t)DS_IPC_POLL_EVENTS_HANGUP;
1149+
poll_handles_data [i].events = (uint8_t)EP_IPC_POLL_EVENTS_HANGUP;
11501150
} else if ((poll_fds [i].revents & (POLLERR|POLLNVAL))) {
11511151
if (callback)
11521152
callback ("Poll error", (uint32_t)poll_fds [i].revents);
1153-
poll_handles_data [i].events = (uint8_t)DS_IPC_POLL_EVENTS_ERR;
1153+
poll_handles_data [i].events = (uint8_t)EP_IPC_POLL_EVENTS_ERR;
11541154
} else if (poll_fds [i].revents & (POLLIN|POLLPRI)) {
1155-
poll_handles_data [i].events = (uint8_t)DS_IPC_POLL_EVENTS_SIGNALED;
1155+
poll_handles_data [i].events = (uint8_t)EP_IPC_POLL_EVENTS_SIGNALED;
11561156
} else {
1157-
poll_handles_data [i].events = (uint8_t)DS_IPC_POLL_EVENTS_UNKNOWN;
1157+
poll_handles_data [i].events = (uint8_t)EP_IPC_POLL_EVENTS_UNKNOWN;
11581158
if (callback)
11591159
callback ("unknown poll response", (uint32_t)poll_fds [i].revents);
11601160
}
@@ -1489,12 +1489,24 @@ ipc_stream_close_func (void *object)
14891489
return ds_ipc_stream_close (ipc_stream, NULL);
14901490
}
14911491

1492+
static
1493+
EventPipeIpcPollEvents
1494+
ipc_stream_poll_func (
1495+
void *object,
1496+
uint32_t timeout_ms)
1497+
{
1498+
EP_ASSERT (object != NULL);
1499+
DiagnosticsIpcStream *ipc_stream = (DiagnosticsIpcStream *)object;
1500+
return ds_ipc_stream_poll (ipc_stream, timeout_ms);
1501+
}
1502+
14921503
static IpcStreamVtable ipc_stream_vtable = {
14931504
ipc_stream_free_func,
14941505
ipc_stream_read_func,
14951506
ipc_stream_write_func,
14961507
ipc_stream_flush_func,
1497-
ipc_stream_close_func };
1508+
ipc_stream_close_func,
1509+
ipc_stream_poll_func };
14981510

14991511
static
15001512
DiagnosticsIpcStream *
@@ -1668,6 +1680,44 @@ ds_ipc_stream_to_string (
16681680
return (result > 0 && result < (int32_t)buffer_len) ? result : 0;
16691681
}
16701682

1683+
EventPipeIpcPollEvents
1684+
ds_ipc_stream_poll (
1685+
DiagnosticsIpcStream *ipc_stream,
1686+
uint32_t timeout_ms)
1687+
{
1688+
EP_ASSERT (ipc_stream != NULL);
1689+
1690+
if (ipc_stream->client_socket == DS_IPC_INVALID_SOCKET)
1691+
return EP_IPC_POLL_EVENTS_HANGUP;
1692+
1693+
ds_ipc_pollfd_t pfd;
1694+
pfd.fd = ipc_stream->client_socket;
1695+
pfd.events = POLLIN | POLLPRI | POLLOUT;
1696+
1697+
int result_poll;
1698+
result_poll = ipc_poll_fds (&pfd, 1, timeout_ms);
1699+
1700+
if (result_poll < 0)
1701+
return EP_IPC_POLL_EVENTS_ERR;
1702+
1703+
if (result_poll == 0)
1704+
return EP_IPC_POLL_EVENTS_HANGUP;
1705+
1706+
if (pfd.revents == 0)
1707+
return EP_IPC_POLL_EVENTS_NONE;
1708+
1709+
if (pfd.revents & POLLHUP)
1710+
return EP_IPC_POLL_EVENTS_HANGUP;
1711+
1712+
if (pfd.revents & (POLLERR | POLLNVAL))
1713+
return EP_IPC_POLL_EVENTS_ERR;
1714+
1715+
if (pfd.revents & (POLLIN | POLLPRI | POLLOUT))
1716+
return EP_IPC_POLL_EVENTS_SIGNALED;
1717+
1718+
return EP_IPC_POLL_EVENTS_UNKNOWN;
1719+
}
1720+
16711721
#endif /* ENABLE_PERFTRACING */
16721722

16731723
#ifndef DS_INCLUDE_SOURCE_FILES

src/native/eventpipe/ds-ipc-pal-types.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ typedef struct _DiagnosticsIpcStream DiagnosticsIpcStream;
2222
* Diagnostics IPC PAL Enums.
2323
*/
2424

25-
typedef enum {
26-
DS_IPC_POLL_EVENTS_NONE = 0x00, // no events
27-
DS_IPC_POLL_EVENTS_SIGNALED = 0x01, // ready for use
28-
DS_IPC_POLL_EVENTS_HANGUP = 0x02, // connection remotely closed
29-
DS_IPC_POLL_EVENTS_ERR = 0x04, // error
30-
DS_IPC_POLL_EVENTS_UNKNOWN = 0x80 // unknown state
31-
} DiagnosticsIpcPollEvents;
32-
3325
typedef enum {
3426
DS_IPC_CONNECTION_MODE_CONNECT,
3527
DS_IPC_CONNECTION_MODE_LISTEN

src/native/eventpipe/ds-ipc-pal-websocket.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ ds_ipc_poll (
248248
int client_socket = poll_handles_data [i].stream->client_socket;
249249
int pending = ds_rt_websocket_poll (client_socket);
250250
if (pending < 0){
251-
poll_handles_data [i].events = (uint8_t)DS_IPC_POLL_EVENTS_ERR;
251+
poll_handles_data [i].events = (uint8_t)EP_IPC_POLL_EVENTS_ERR;
252252
return 1;
253253
}
254254
if (pending > 0){
255-
poll_handles_data [i].events = (uint8_t)DS_IPC_POLL_EVENTS_SIGNALED;
255+
poll_handles_data [i].events = (uint8_t)EP_IPC_POLL_EVENTS_SIGNALED;
256256
return 1;
257257
}
258258
}
@@ -425,12 +425,33 @@ ipc_stream_close_func (void *object)
425425
return ds_ipc_stream_close (ipc_stream, NULL);
426426
}
427427

428+
static
429+
EventPipeIpcPollEvents
430+
ipc_stream_poll_func (
431+
void *object,
432+
uint32_t timeout_ms)
433+
{
434+
EP_ASSERT (object != NULL);
435+
DiagnosticsIpcStream *ipc_stream = (DiagnosticsIpcStream *)object;
436+
437+
// Check if the socket is still open
438+
int pending = ds_rt_websocket_poll (ipc_stream->client_socket);
439+
if (pending < 0)
440+
return EP_IPC_POLL_EVENTS_ERR;
441+
442+
if (pending > 0)
443+
return EP_IPC_POLL_EVENTS_SIGNALED;
444+
445+
return EP_IPC_POLL_EVENTS_NONE;
446+
}
447+
428448
static IpcStreamVtable ipc_stream_vtable = {
429449
ipc_stream_free_func,
430450
ipc_stream_read_func,
431451
ipc_stream_write_func,
432452
ipc_stream_flush_func,
433-
ipc_stream_close_func };
453+
ipc_stream_close_func,
454+
ipc_stream_poll_func };
434455

435456
static
436457
DiagnosticsIpcStream *

src/native/eventpipe/ds-ipc-pal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,10 @@ ds_ipc_stream_to_string (
140140
ep_char8_t *buffer,
141141
uint32_t buffer_len);
142142

143+
EventPipeIpcPollEvents
144+
ds_ipc_stream_poll (
145+
DiagnosticsIpcStream *ipc_stream,
146+
uint32_t timeout_ms);
147+
143148
#endif /* ENABLE_PERFTRACING */
144149
#endif /* __DIAGNOSTICS_IPC_PAL_H__ */

src/native/eventpipe/ds-ipc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,13 @@ ds_ipc_stream_factory_get_next_available_stream (ds_ipc_error_callback_func call
406406
DN_VECTOR_FOREACH_BEGIN (DiagnosticsIpcPollHandle, ipc_poll_handle, &ipc_poll_handles) {
407407
DiagnosticsPort *port = (DiagnosticsPort *)ipc_poll_handle.user_data;
408408
switch (ipc_poll_handle.events) {
409-
case DS_IPC_POLL_EVENTS_HANGUP:
409+
case EP_IPC_POLL_EVENTS_HANGUP:
410410
EP_ASSERT (port != NULL);
411411
ds_port_reset_vcall (port, callback);
412412
DS_LOG_INFO_2 ("ds_ipc_stream_factory_get_next_available_stream - HUP :: Poll attempt: %d, connection %d hung up. Connect is reset.", poll_attempts, connection_id);
413413
poll_timeout_ms = DS_IPC_POLL_TIMEOUT_MIN_MS;
414414
break;
415-
case DS_IPC_POLL_EVENTS_SIGNALED:
415+
case EP_IPC_POLL_EVENTS_SIGNALED:
416416
EP_ASSERT (port != NULL);
417417
if (!stream) { // only use first signaled stream; will get others on subsequent calls
418418
stream = ds_port_get_connected_stream_vcall (port, callback);
@@ -422,12 +422,12 @@ ds_ipc_stream_factory_get_next_available_stream (ds_ipc_error_callback_func call
422422
}
423423
DS_LOG_DEBUG_2 ("ds_ipc_stream_factory_get_next_available_stream - SIG :: Poll attempt: %d, connection %d signalled.", poll_attempts, connection_id);
424424
break;
425-
case DS_IPC_POLL_EVENTS_ERR:
425+
case EP_IPC_POLL_EVENTS_ERR:
426426
ds_port_reset_vcall ((DiagnosticsPort *)ipc_poll_handle.user_data, callback);
427427
DS_LOG_INFO_2 ("ds_ipc_stream_factory_get_next_available_stream - ERR :: Poll attempt: %d, connection %d errored. Connection is reset.", poll_attempts, connection_id);
428428
saw_error = true;
429429
break;
430-
case DS_IPC_POLL_EVENTS_NONE:
430+
case EP_IPC_POLL_EVENTS_NONE:
431431
DS_LOG_INFO_2 ("ds_ipc_stream_factory_get_next_available_stream - NON :: Poll attempt: %d, connection %d had no events.", poll_attempts, connection_id);
432432
break;
433433
default:

src/native/eventpipe/ep-ipc-pal-types.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,19 @@
1111

1212
#include "ep-ipc-pal-types-forward.h"
1313

14+
/*
15+
* EventPipe IPC PAL
16+
*/
17+
18+
typedef enum {
19+
EP_IPC_POLL_EVENTS_NONE = 0x00, // no events
20+
EP_IPC_POLL_EVENTS_SIGNALED = 0x01, // ready for use
21+
EP_IPC_POLL_EVENTS_HANGUP = 0x02, // connection remotely closed
22+
EP_IPC_POLL_EVENTS_ERR = 0x04, // error
23+
EP_IPC_POLL_EVENTS_UNKNOWN = 0x80 // unknown state
24+
} EventPipeIpcPollEvents;
25+
26+
#define EP_IPC_POLL_TIMEOUT_MIN_MS (uint32_t)10
27+
1428
#endif /* ENABLE_PERFTRACING */
1529
#endif /* __EVENTPIPE_IPC_PAL_TYPES_H__ */

src/native/eventpipe/ep-ipc-stream.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ typedef bool (*IpcStreamReadFunc)(void *object, uint8_t *buffer, uint32_t bytes_
2121
typedef bool (*IpcStreamWriteFunc)(void *object, const uint8_t *buffer, uint32_t bytes_to_write, uint32_t *bytes_written, uint32_t timeout_ms);
2222
typedef bool (*IpcStreamFlushFunc)(void *object);
2323
typedef bool (*IpcStreamCloseFunc)(void *object);
24+
typedef EventPipeIpcPollEvents (*IpcStreamPollFunc)(void *object, uint32_t timeout_ms);
2425

2526
struct _IpcStreamVtable {
2627
IpcStreamFreeFunc free_func;
2728
IpcStreamReadFunc read_func;
2829
IpcStreamWriteFunc write_func;
2930
IpcStreamFlushFunc flush_func;
3031
IpcStreamCloseFunc close_func;
32+
IpcStreamPollFunc poll_func;
3133
};
3234

3335
#if defined(EP_INLINE_GETTER_SETTER) || defined(EP_IMPL_IPC_STREAM_GETTER_SETTER) || defined(DS_IMPL_IPC_PAL_NAMEDPIPE_GETTER_SETTER) || defined(DS_IMPL_IPC_PAL_SOCKET_GETTER_SETTER)
@@ -77,5 +79,8 @@ ep_ipc_stream_flush_vcall (IpcStream *ipc_stream);
7779
bool
7880
ep_ipc_stream_close_vcall (IpcStream *ipc_stream);
7981

82+
EventPipeIpcPollEvents
83+
ep_ipc_stream_poll_vcall (IpcStream *ipc_stream);
84+
8085
#endif /* ENABLE_PERFTRACING */
8186
#endif /* __EVENTPIPE_IPC_STREAM_H__ */

src/native/eventpipe/ep-stream.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,18 @@ ep_ipc_stream_close_vcall (IpcStream *ipc_stream)
538538
return vtable->close_func (ipc_stream);
539539
}
540540

541+
EventPipeIpcPollEvents
542+
ep_ipc_stream_poll_vcall (IpcStream *ipc_stream, uint32_t timeout_ms)
543+
{
544+
EP_ASSERT (ipc_stream != NULL);
545+
546+
EP_ASSERT (ipc_stream->vtable != NULL);
547+
IpcStreamVtable *vtable = ipc_stream->vtable;
548+
549+
EP_ASSERT (vtable->poll_func != NULL);
550+
return vtable->poll_func (ipc_stream, timeout_ms);
551+
}
552+
541553
/*
542554
* IpcStreamWriter.
543555
*/

0 commit comments

Comments
 (0)