Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 9da43ba

Browse files
author
Sean Parker
authored
Event Tracing API wrapper (#1452)
* Add C# API wrappers for Span, Item, EventData data operations * Add wrappers for Event Tracer logic * Add Parameter Conversions * Use closures to convert from external to internal Event * Add Event Tracer parameter conversion closure * Update Worker SDK IO bindings to support v14.8.0 * Add changelog entry
1 parent 08e894f commit 9da43ba

File tree

9 files changed

+748
-46
lines changed

9 files changed

+748
-46
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
### Added
1212

1313
- Added `MeansImplicitUse` attribute to `RequireAttribute` to reduce warnings in Rider IDE. [#1462](https://github.com/spatialos/gdk-for-unity/pull/1462)
14+
- Added Event Tracing API. [#1452](https://github.com/spatialos/gdk-for-unity/pull/1452)
1415

1516
### Changed
1617

workers/unity/Packages/io.improbable.worker.sdk/CEventTrace.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,27 @@ internal unsafe class CEventTrace
1818
* Data for an event. This is a collection of key-value pairs (fields). Use EventData* functions to
1919
* read or write fields.
2020
*/
21-
public class EventData : CptrHandle
21+
public class EventDataHandle : CptrHandle
2222
{
23+
public EventDataHandle()
24+
{
25+
}
26+
27+
internal EventDataHandle(IntPtr handle)
28+
{
29+
SetHandle(handle);
30+
}
31+
2332
protected override bool ReleaseHandle()
2433
{
2534
EventDataDestroy(handle);
2635
return true;
2736
}
37+
38+
internal IntPtr GetUnderlying()
39+
{
40+
return handle;
41+
}
2842
}
2943

3044
public class EventTracer : CptrHandle
@@ -100,7 +114,7 @@ public struct Span
100114
*/
101115
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
102116
EntryPoint = "Trace_EventData_Create")]
103-
public static extern EventData EventDataCreate();
117+
public static extern EventDataHandle EventDataCreate();
104118

105119
/** Frees resources for the event data object.*/
106120
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
@@ -113,12 +127,12 @@ public struct Span
113127
*/
114128
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
115129
EntryPoint = "Trace_EventData_AddStringFields")]
116-
public static extern void EventDataAddStringFields(EventData data, Uint32 count, Char** keys, Char** values);
130+
public static extern void EventDataAddStringFields(EventDataHandle data, Uint32 count, Char** keys, Char** values);
117131

118132
/** Returns the number of fields on the given event data object. */
119133
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
120134
EntryPoint = "Trace_EventData_GetFieldCount")]
121-
public static extern Uint32 EventDataGetFieldCount(EventData data);
135+
public static extern Uint32 EventDataGetFieldCount(EventDataHandle data);
122136

123137
/**
124138
* Returns all the key value pairs in the event data object. keys and values must have capacity for
@@ -128,12 +142,12 @@ public struct Span
128142
*/
129143
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
130144
EntryPoint = "Trace_EventData_GetStringFields")]
131-
public static extern void EventDataGetStringFields(EventData data, Char** keys, Char** values);
145+
public static extern void EventDataGetStringFields(EventDataHandle data, Char** keys, Char** values);
132146

133147
/** Returns the value for the given key. */
134148
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
135149
EntryPoint = "Trace_EventData_GetFieldValue")]
136-
public static extern Char* EventDataGetFieldValue(EventData data, Char* key);
150+
public static extern Char* EventDataGetFieldValue(EventDataHandle data, Char* key);
137151

138152
/** Data for an event added to the event-tracer. */
139153
[StructLayout(LayoutKind.Sequential)]
@@ -153,7 +167,7 @@ public struct Event
153167
public struct Item
154168
{
155169
/** The type of the item, defined using ItemType. */
156-
public Uint8 ItemType;
170+
public ItemType ItemType;
157171

158172
/** An item can either be a Span or an Event. */
159173
public Union ItemUnion;
@@ -187,12 +201,12 @@ public struct EventTracerParameters
187201

188202
/** Creates an event-tracer. */
189203
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
190-
EntryPoint = "EventTracerCreate")]
204+
EntryPoint = "Trace_EventTracer_Create")]
191205
public static extern EventTracer EventTracerCreate(EventTracerParameters* parameters);
192206

193207
/** Frees resources for an event-tracer. */
194208
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
195-
EntryPoint = "EventTracerDestroy")]
209+
EntryPoint = "Trace_EventTracer_Destroy")]
196210
public static extern void EventTracerDestroy(IntPtr eventTracer);
197211

198212
/**
@@ -234,8 +248,8 @@ public struct EventTracerParameters
234248
* EventTracerGetActiveSpanId will return a null span ID.
235249
*/
236250
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
237-
EntryPoint = "Trace_EventTracer_UnsetActiveSpanId")]
238-
public static extern void EventTracerUnsetActiveSpanId(EventTracer eventTracer);
251+
EntryPoint = "Trace_EventTracer_ClearActiveSpanId")]
252+
public static extern void EventTracerClearActiveSpanId(EventTracer eventTracer);
239253

240254
/** Gets the active span ID on the event-tracer. */
241255
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
@@ -272,7 +286,7 @@ public struct EventTracerParameters
272286
* The item is initialized by copying the provided item; pass a NULL item argument to create an
273287
* item in an uninitialized state.
274288
*
275-
* Directly creating a TraceItem object (on the stack or the heap) by other means than calling this
289+
* Directly creating a Item object (on the stack or the heap) by other means than calling this
276290
* method is discouraged as it will lead to undefined behaviour when passing that item to certain
277291
* trace API methods (e.g. SerializeItemToStream).
278292
*/

workers/unity/Packages/io.improbable.worker.sdk/CIO.cs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Runtime.InteropServices;
23
using Int64 = System.Int64;
34
using Uint64 = System.UInt64;
@@ -28,10 +29,31 @@ protected override bool ReleaseHandle()
2829
}
2930
}
3031

31-
public enum OpenMode
32+
[Flags]
33+
public enum OpenModes : Uint32
3234
{
33-
/* Opens the stream in the default mode. */
34-
OpenModeDefault = 0x00,
35+
/**
36+
* Allow input operations on the stream. Input operations always occur at the read position, which
37+
* is initialized to the beginning of the stream.
38+
*/
39+
OpenModeRead = 0x01,
40+
41+
/**
42+
* Allow output operations on the stream. Output operations always occur at the write position,
43+
* which is initialized to the end of the stream.
44+
*/
45+
OpenModeWrite = 0x02,
46+
47+
/**
48+
* Truncates any existing content upon opening. If not set, writes are appended to the end of the
49+
* stream's existing content.
50+
*/
51+
OpenModeTruncate = 0x04,
52+
53+
/**
54+
* Specify that writes should be appended to the stream's existing content, if any exists.
55+
*/
56+
OpenModeAppend = 0x08,
3557
}
3658

3759
/**
@@ -85,16 +107,14 @@ public enum OpenMode
85107
* The file stream has a conceptually infinite capacity; its true capacity depends on the
86108
* underlying filesystem.
87109
*
88-
* Upon creation of the file stream, the file is created if it does not exist. The file stream is
89-
* initialized to read from the beginning of the file and append to the end, regardless of whether
90-
* it previously existed or not.
110+
* The open_mode argument should be passed as a combination of OpenMode values.
91111
*
92-
* Returns a pointer to a file stream. Never returns NULL. Call StreamGetLastError to check
93-
* if an error occurred during file stream creation.
112+
* Returns a pointer to a file stream. Never returns NULL. You *must* call Io_Stream_GetLastError to
113+
* check if an error occurred during file stream creation.
94114
*/
95115
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
96116
EntryPoint = "Io_CreateFileStream")]
97-
public static extern StreamHandle CreateFileStream(Char* filename, OpenMode openMode);
117+
public static extern StreamHandle CreateFileStream(Char* filename, OpenModes openModes);
98118

99119
/* Destroys the I/O stream. */
100120
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
@@ -168,5 +188,13 @@ public enum OpenMode
168188
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
169189
EntryPoint = "Io_Stream_GetLastError")]
170190
public static extern Char* StreamGetLastError(StreamHandle stream);
191+
192+
/**
193+
* Clears the stream's current error such that the next call to Io_Stream_GetLastError returns
194+
* nullptr.
195+
*/
196+
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
197+
EntryPoint = "Io_Stream_ClearError")]
198+
public static extern void StreamClearError(StreamHandle stream);
171199
}
172200
}

0 commit comments

Comments
 (0)