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

Commit

Permalink
Event Tracing API wrapper (#1452)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Sean Parker authored Aug 28, 2020
1 parent 08e894f commit 9da43ba
Show file tree
Hide file tree
Showing 9 changed files with 748 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Added

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

### Changed

Expand Down
38 changes: 26 additions & 12 deletions workers/unity/Packages/io.improbable.worker.sdk/CEventTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,27 @@ internal unsafe class CEventTrace
* Data for an event. This is a collection of key-value pairs (fields). Use EventData* functions to
* read or write fields.
*/
public class EventData : CptrHandle
public class EventDataHandle : CptrHandle
{
public EventDataHandle()
{
}

internal EventDataHandle(IntPtr handle)
{
SetHandle(handle);
}

protected override bool ReleaseHandle()
{
EventDataDestroy(handle);
return true;
}

internal IntPtr GetUnderlying()
{
return handle;
}
}

public class EventTracer : CptrHandle
Expand Down Expand Up @@ -100,7 +114,7 @@ public struct Span
*/
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "Trace_EventData_Create")]
public static extern EventData EventDataCreate();
public static extern EventDataHandle EventDataCreate();

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

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

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

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

/** Data for an event added to the event-tracer. */
[StructLayout(LayoutKind.Sequential)]
Expand All @@ -153,7 +167,7 @@ public struct Event
public struct Item
{
/** The type of the item, defined using ItemType. */
public Uint8 ItemType;
public ItemType ItemType;

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

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

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

/**
Expand Down Expand Up @@ -234,8 +248,8 @@ public struct EventTracerParameters
* EventTracerGetActiveSpanId will return a null span ID.
*/
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "Trace_EventTracer_UnsetActiveSpanId")]
public static extern void EventTracerUnsetActiveSpanId(EventTracer eventTracer);
EntryPoint = "Trace_EventTracer_ClearActiveSpanId")]
public static extern void EventTracerClearActiveSpanId(EventTracer eventTracer);

/** Gets the active span ID on the event-tracer. */
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
Expand Down Expand Up @@ -272,7 +286,7 @@ public struct EventTracerParameters
* The item is initialized by copying the provided item; pass a NULL item argument to create an
* item in an uninitialized state.
*
* Directly creating a TraceItem object (on the stack or the heap) by other means than calling this
* Directly creating a Item object (on the stack or the heap) by other means than calling this
* method is discouraged as it will lead to undefined behaviour when passing that item to certain
* trace API methods (e.g. SerializeItemToStream).
*/
Expand Down
46 changes: 37 additions & 9 deletions workers/unity/Packages/io.improbable.worker.sdk/CIO.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Runtime.InteropServices;
using Int64 = System.Int64;
using Uint64 = System.UInt64;
Expand Down Expand Up @@ -28,10 +29,31 @@ protected override bool ReleaseHandle()
}
}

public enum OpenMode
[Flags]
public enum OpenModes : Uint32
{
/* Opens the stream in the default mode. */
OpenModeDefault = 0x00,
/**
* Allow input operations on the stream. Input operations always occur at the read position, which
* is initialized to the beginning of the stream.
*/
OpenModeRead = 0x01,

/**
* Allow output operations on the stream. Output operations always occur at the write position,
* which is initialized to the end of the stream.
*/
OpenModeWrite = 0x02,

/**
* Truncates any existing content upon opening. If not set, writes are appended to the end of the
* stream's existing content.
*/
OpenModeTruncate = 0x04,

/**
* Specify that writes should be appended to the stream's existing content, if any exists.
*/
OpenModeAppend = 0x08,
}

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

/* Destroys the I/O stream. */
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
Expand Down Expand Up @@ -168,5 +188,13 @@ public enum OpenMode
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "Io_Stream_GetLastError")]
public static extern Char* StreamGetLastError(StreamHandle stream);

/**
* Clears the stream's current error such that the next call to Io_Stream_GetLastError returns
* nullptr.
*/
[DllImport(Constants.WorkerLibrary, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "Io_Stream_ClearError")]
public static extern void StreamClearError(StreamHandle stream);
}
}
Loading

0 comments on commit 9da43ba

Please sign in to comment.