-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModalityLoggerPlugin.cs
250 lines (212 loc) · 8.06 KB
/
ModalityLoggerPlugin.cs
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
using System;
using Antmicro.Renode.Logging;
using Antmicro.Renode.UserInterface;
using Antmicro.Renode.Plugins;
using Antmicro.Renode.Plugins.Auxon.Interop;
using System.Runtime.InteropServices;
namespace Antmicro.Renode.Plugins.Auxon.ModalityLogger
{
[Plugin(Name = "Modality Logger", Description = "Modality logger plugin", Version = "0.1", Vendor = "Auxon")]
public class ModalityLoggerPlugin : IDisposable
{
public ModalityLoggerPlugin(Antmicro.Renode.UserInterface.Monitor monitor)
{
try
{
this.backend = new ModalityLoggerBackend();
Logger.AddBackend(this.backend, "modality");
}
catch (Exception e)
{
Logger.Error("Unable to initialize Modality logger backend: " + e);
}
}
public void Dispose()
{
if (this.backend != null)
{
Logger.RemoveBackend(this.backend);
this.backend = null;
}
}
private ILoggerBackend? backend;
}
class TimelineAttrKeys
{
public TimelineAttrKeys(ModalityIngestClient client)
{
this.name = client.DeclareAttrKey("timeline.name");
this.runId = client.DeclareAttrKey("timeline.run_id");
this.internalSource = client.DeclareAttrKey("timeline.internal.source");
}
public UInt32 name;
public UInt32 runId;
public UInt32 internalSource;
}
class EventAttrKeys
{
public EventAttrKeys(ModalityIngestClient client)
{
this.name = client.DeclareAttrKey("event.name");
this.timestamp = client.DeclareAttrKey("event.timestamp");
this.timestamp = client.DeclareAttrKey("event.timestamp");
this.sourceId = client.DeclareAttrKey("event.source_id");
this.threadId = client.DeclareAttrKey("event.thread_id");
this.machineName = client.DeclareAttrKey("event.machine_name");
this.objectName = client.DeclareAttrKey("event.object_name");
this.logLevel = client.DeclareAttrKey("event.log_level");
}
public UInt32 name;
public UInt32 timestamp;
public UInt32 sourceId;
public UInt32 threadId;
public UInt32 machineName;
public UInt32 objectName;
public UInt32 logLevel;
}
public class ModalityLoggerBackend : LoggerBackend
{
public ModalityLoggerBackend()
{
this.disposed = false;
this.runtime = new ModalityRuntime();
this.ingest = new ModalityIngestClient(this.runtime);
this.epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var ingest_url = GetEnv("MODALITY_INGEST_URL");
if (ingest_url == null)
{
throw new Exception("MODALITY_INGEST_URL env var is not set");
}
var auth_token = GetEnv("MODALITY_AUTH_TOKEN");
if (auth_token == null)
{
throw new Exception("MODALITY_AUTH_TOKEN env var is not set");
}
this.runId = GetEnv("MODALITY_RUN_ID") ?? Guid.NewGuid().ToString();
this.ingest.Connect(ingest_url);
this.ingest.Authenticate(auth_token);
this.timeline_keys = new TimelineAttrKeys(this.ingest);
this.event_keys = new EventAttrKeys(this.ingest);
this.globalTimeline = new RenodeTimeline(TimelineId.Allocate(), null);
this.currentTimeline = this.globalTimeline;
this.machineTimelines = new Dictionary<string, RenodeTimeline>();
var tlAttrs = new AttrKVs();
tlAttrs.Add(timeline_keys.name, "RenodeGlobal");
tlAttrs.Add(timeline_keys.runId, this.runId);
tlAttrs.Add(timeline_keys.internalSource, "renode");
this.ingest.OpenTimeline(this.globalTimeline.id);
this.ingest.TimelineMetadata(tlAttrs);
}
[DllImport("libc.so.6")]
private static extern IntPtr getenv([MarshalAs(UnmanagedType.LPUTF8Str)] string name);
static string? GetEnv(string name)
{
var ptr = getenv(name);
if (ptr == IntPtr.Zero)
{
return null;
}
return Marshal.PtrToStringUTF8(ptr);
}
public override void Log(LogEntry entry)
{
if (!ShouldBeLogged(entry))
{
return;
}
SwitchToTimelineForMachineName(entry.MachineName);
var attrs = new AttrKVs();
attrs.Add(event_keys.name, entry.Message);
attrs.Add(event_keys.logLevel, entry.Type.ToString());
attrs.Add(event_keys.sourceId, entry.SourceId);
attrs.Add(event_keys.sourceId, entry.SourceId);
UInt64 epochNanos = ((UInt64)(entry.Time - this.epochStart).Ticks) * 100;
attrs.Add(event_keys.timestamp, new Nanoseconds(epochNanos));
if (entry.MachineName != null)
{
attrs.Add(event_keys.machineName, entry.MachineName);
}
if (entry.ThreadId != null)
{
attrs.Add(event_keys.threadId, (int)entry.ThreadId);
}
lock (ingest)
{
ingest.Event(entry.Id, 0, attrs);
}
}
private void SwitchToTimelineForMachineName(string? machineName)
{
lock (this.currentTimeline)
{
if (this.currentTimeline.machineName == machineName)
{
return;
}
if (machineName == null)
{
this.currentTimeline = this.globalTimeline;
lock (ingest)
{
this.ingest.OpenTimeline(this.currentTimeline.id);
}
return;
}
RenodeTimeline? tl = null;
var exists = machineTimelines.TryGetValue(machineName, out tl);
if (exists && tl != null)
{
// Switch to an existing timeline
this.currentTimeline = tl;
lock (ingest)
{
this.ingest.OpenTimeline(this.currentTimeline.id);
}
return;
}
// First time we've seen this MachineName; allocate a new timeline and send metadata
tl = new RenodeTimeline(TimelineId.Allocate(), machineName);
this.machineTimelines.Add(machineName, tl);
this.currentTimeline = tl;
var tlAttrs = new AttrKVs();
tlAttrs.Add(timeline_keys.name, machineName);
tlAttrs.Add(timeline_keys.runId, this.runId);
tlAttrs.Add(timeline_keys.internalSource, "renode");
lock (ingest)
{
this.ingest.OpenTimeline(currentTimeline.id);
this.ingest.TimelineMetadata(tlAttrs);
}
}
}
public override void Dispose()
{
if (!this.disposed)
{
this.ingest.Dispose();
this.runtime.Dispose();
this.disposed = true;
}
}
private bool disposed;
private readonly ModalityRuntime runtime;
private readonly ModalityIngestClient ingest;
private readonly TimelineAttrKeys timeline_keys;
private readonly EventAttrKeys event_keys;
private readonly DateTime epochStart;
private RenodeTimeline currentTimeline;
private Dictionary<string, RenodeTimeline> machineTimelines;
private RenodeTimeline globalTimeline;
private string runId;
}
}
class RenodeTimeline
{
internal TimelineId id;
internal string? machineName;
public RenodeTimeline(TimelineId id, string? machineName)
{
this.id = id;
this.machineName = machineName;
}
}