Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / eventlogfilemgr.cc @ a3be1d55

History | View | Annotate | Download (18.6 KB)

1
//==========================================================================
2
//  EVENTLOGFILEMGR.CC - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17

    
18
#include <algorithm>
19
#include "opp_ctype.h"
20
#include "commonutil.h"  //vsnprintf
21
#include "eventlogfilemgr.h"
22
#include "eventlogwriter.h"
23
#include "cconfigoption.h"
24
#include "fileutil.h"
25
#include "cconfiguration.h"
26
#include "envirbase.h"
27
#include "cmodule.h"
28
#include "cmessage.h"
29
#include "cgate.h"
30
#include "cchannel.h"
31
#include "csimplemodule.h"
32
#include "ccompoundmodule.h"
33
#include "cdisplaystring.h"
34
#include "cclassdescriptor.h"
35

    
36
USING_NAMESPACE
37

    
38

    
39
Register_PerRunConfigOption(CFGID_EVENTLOG_FILE, "eventlog-file", CFG_FILENAME, "${resultdir}/${configname}-${runnumber}.elog", "Name of the event log file to generate.");
40
Register_PerRunConfigOption(CFGID_EVENTLOG_MESSAGE_DETAIL_PATTERN, "eventlog-message-detail-pattern", CFG_CUSTOM, NULL,
41
        "A list of patterns separated by '|' character which will be used to write "
42
        "message detail information into the event log for each message sent during "
43
        "the simulation. The message detail will be presented in the sequence chart "
44
        "tool. Each pattern starts with an object pattern optionally followed by ':' "
45
        "character and a comma separated list of field patterns. In both "
46
        "patterns and/or/not/* and various field match expressions can be used. "
47
        "The object pattern matches to class name, the field pattern matches to field name by default.\n"
48
        "  EVENTLOG-MESSAGE-DETAIL-PATTERN := ( DETAIL-PATTERN '|' )* DETAIL_PATTERN\n"
49
        "  DETAIL-PATTERN := OBJECT-PATTERN [ ':' FIELD-PATTERNS ]\n"
50
        "  OBJECT-PATTERN := MATCH-EXPRESSION\n"
51
        "  FIELD-PATTERNS := ( FIELD-PATTERN ',' )* FIELD_PATTERN\n"
52
        "  FIELD-PATTERN := MATCH-EXPRESSION\n"
53
        "Examples (enter them without quotes):\n"
54
        "  \"*\": captures all fields of all messages\n"
55
        "  \"*Frame:*Address,*Id\": captures all fields named somethingAddress and somethingId from messages of any class named somethingFrame\n"
56
        "  \"MyMessage:declaredOn(MyMessage)\": captures instances of MyMessage recording the fields declared on the MyMessage class\n"
57
        "  \"*:(not declaredOn(cMessage) and not declaredOn(cNamedObject) and not declaredOn(cObject))\": records user-defined fields from all messages");
58
Register_PerRunConfigOption(CFGID_EVENTLOG_RECORDING_INTERVALS, "eventlog-recording-intervals", CFG_CUSTOM, NULL, "Simulation time interval(s) when events should be recorded. Syntax: [<from>]..[<to>],... That is, both start and end of an interval are optional, and intervals are separated by comma. Example: ..10.2, 22.2..100, 233.3..");
59
Register_PerObjectConfigOption(CFGID_MODULE_EVENTLOG_RECORDING, "module-eventlog-recording", CFG_BOOL, "true", "Enables recording events on a per module basis. This is meaningful for simple modules only. \nExample:\n **.router[10..20].**.module-eventlog-recording = true\n **.module-eventlog-recording = false");
60

    
61
static va_list empty_va;
62

    
63
static bool compareMessageEventNumbers(cMessage *message1, cMessage *message2)
64
{
65
    return message1->getPreviousEventNumber() < message2->getPreviousEventNumber();
66
}
67

    
68
static ObjectPrinterRecursionControl recurseIntoMessageFields(void *object, cClassDescriptor *descriptor, int fieldIndex, void *fieldValue, void **parents, int level) {
69
    const char* propertyValue = descriptor->getFieldProperty(object, fieldIndex, "eventlog");
70

    
71
    if (propertyValue) {
72
        if (!strcmp(propertyValue, "skip"))
73
            return SKIP;
74
        else if (!strcmp(propertyValue, "fullName"))
75
            return FULL_NAME;
76
        else if (!strcmp(propertyValue, "fullPath"))
77
            return FULL_PATH;
78
    }
79

    
80
    bool isCObject = descriptor->getFieldIsCObject(object, fieldIndex);
81
    if (!isCObject)
82
        return RECURSE;
83
    else {
84
        if (!fieldValue)
85
            return RECURSE;
86
        else {
87
            cArray *array = dynamic_cast<cArray *>((cObject *)fieldValue);
88
            return !array || array->size() != 0 ? RECURSE : SKIP;
89
        }
90
    }
91
}
92

    
93
EventlogFileManager::EventlogFileManager()
94
{
95
    feventlog = NULL;
96
    objectPrinter = NULL;
97
    recordingIntervals = NULL;
98
    isEventLogRecordingEnabled = true;
99
    isIntervalEventLogRecordingEnabled = true;
100
    isModuleEventLogRecordingEnabled = true;
101
}
102

    
103
EventlogFileManager::~EventlogFileManager()
104
{
105
    delete objectPrinter;
106
    delete recordingIntervals;
107
}
108

    
109
void EventlogFileManager::configure()
110
{
111
    // setup event log object printer
112
    delete objectPrinter;
113
    objectPrinter = NULL;
114

    
115
    const char *eventLogMessageDetailPattern = ev.getConfig()->getAsCustom(CFGID_EVENTLOG_MESSAGE_DETAIL_PATTERN);
116

    
117
    if (eventLogMessageDetailPattern) {
118
        objectPrinter = new ObjectPrinter(recurseIntoMessageFields, eventLogMessageDetailPattern, 3);
119
    }
120

    
121
    // setup eventlog recording intervals
122
    const char *text = ev.getConfig()->getAsCustom(CFGID_EVENTLOG_RECORDING_INTERVALS);
123
    if (text) {
124
        recordingIntervals = new Intervals();
125
        recordingIntervals->parse(text);
126
    }
127

    
128
    // setup filename
129
    filename = ev.getConfig()->getAsFilename(CFGID_EVENTLOG_FILE).c_str();
130
    dynamic_cast<EnvirBase *>(&ev)->processFileName(filename);
131
    ::printf("Recording event log to file `%s'...\n", filename.c_str());
132
}
133

    
134
void EventlogFileManager::open()
135
{
136
    mkPath(directoryOf(filename.c_str()).c_str());
137
    FILE *out = fopen(filename.c_str(), "w");
138
    if (!out)
139
        throw cRuntimeError("Cannot open eventlog file `%s' for write", filename.c_str());
140
    feventlog = out;
141
}
142

    
143
void EventlogFileManager::recordSimulation()
144
{
145
    cModule *systemModule = simulation.getSystemModule();
146
    recordModules(systemModule);
147
    recordConnections(systemModule);
148
    recordMessages();
149
}
150

    
151
void EventlogFileManager::recordMessages()
152
{
153
    const char *runId = ev.getConfigEx()->getVariable(CFGVAR_RUNID);
154
    EventLogWriter::recordSimulationBeginEntry_v_rid(feventlog, OMNETPP_VERSION, runId);
155
    std::vector<cMessage *> messages;
156
    for (cMessageHeap::Iterator it = cMessageHeap::Iterator(simulation.getMessageQueue()); !it.end(); it++)
157
        messages.push_back(it());
158
    std::stable_sort(messages.begin(), messages.end(), compareMessageEventNumbers);
159
    eventnumber_t currentEvent = -1;
160
    for (std::vector<cMessage *>::iterator it = messages.begin(); it != messages.end(); it++) {
161
        cMessage *message = *it;
162
        if (currentEvent != message->getPreviousEventNumber()) {
163
            currentEvent = message->getPreviousEventNumber();
164
            EventLogWriter::recordEventEntry_e_t_m_msg(feventlog, currentEvent, message->getSendingTime(), currentEvent == 0 ? simulation.getSystemModule()->getId() : message->getSenderModuleId(), -1);
165
        }
166
        if (currentEvent == 0)
167
            componentMethodBegin(simulation.getSystemModule(), message->getSenderModule(), "initialize", empty_va);
168
        if (message->isSelfMessage())
169
            messageScheduled(message);
170
        else if (!message->getSenderGate()) {
171
            beginSend(message);
172
            if (message->isPacket()) {
173
                cPacket *packet = (cPacket *)message;
174
                simtime_t propagationDelay = packet->getArrivalTime() - packet->getSendingTime() - (packet->isReceptionStart() ? 0 : packet->getDuration());
175
                messageSendDirect(message, message->getArrivalGate(), propagationDelay, packet->getDuration());
176
            }
177
            else
178
                messageSendDirect(message, message->getArrivalGate(), 0, 0);
179
            endSend(message);
180
        }
181
        else {
182
            beginSend(message);
183
            messageSendHop(message, message->getSenderGate());
184
            endSend(message);
185
        }
186
        if (currentEvent == 0)
187
            componentMethodEnd();
188
    }
189
}
190

    
191
void EventlogFileManager::recordModules(cModule *module)
192
{
193
    for (cModule::GateIterator it(module); !it.end(); it++) {
194
        cGate *gate = it();
195
        gateCreated(gate);
196
    }
197
    moduleCreated(module);
198
    // FIXME: records display string twice if it is lazily created right now
199
    if (strcmp(module->getDisplayString().str(), "")) {
200
        displayStringChanged(module);
201
    }
202
    for (cModule::SubmoduleIterator it(module); !it.end(); it++)
203
        recordModules(it());
204
}
205

    
206
void EventlogFileManager::recordConnections(cModule *module)
207
{
208
    for (cModule::GateIterator it(module); !it.end(); it++) {
209
        cGate *gate = it();
210
        if (gate->getNextGate())
211
            connectionCreated(gate);
212
        cChannel *channel = gate->getChannel();
213
        if (channel && strcmp(channel->getDisplayString(), "")) {
214
            displayStringChanged(channel);
215
        }
216
    }
217
    for (cModule::SubmoduleIterator it(module); !it.end(); it++)
218
        recordConnections(it());
219
}
220

    
221
void EventlogFileManager::startRun()
222
{
223
    if (isEventLogRecordingEnabled)
224
    {
225
        const char *runId = ev.getConfigEx()->getVariable(CFGVAR_RUNID);
226
        // TODO: we can't use simulation.getEventNumber() and simulation.getSimTime(), because when we start a new run
227
        // these numbers are still set from the previous run (i.e. not zero)
228
        EventLogWriter::recordEventEntry_e_t_m_msg(feventlog, 0, 0, simulation.getSystemModule()->getId(), -1);
229
        EventLogWriter::recordSimulationBeginEntry_v_rid(feventlog, OMNETPP_VERSION, runId);
230
        fflush(feventlog);
231
    }
232
}
233

    
234
void EventlogFileManager::endRun()
235
{
236
    if (isEventLogRecordingEnabled)
237
    {
238
        EventLogWriter::recordSimulationEndEntry(feventlog);
239
        fclose(feventlog);
240
        feventlog = NULL;
241
    }
242
}
243

    
244
bool EventlogFileManager::hasRecordingIntervals() const
245
{
246
    return recordingIntervals && !recordingIntervals->empty();
247
}
248

    
249
void EventlogFileManager::clearRecordingIntervals()
250
{
251
    if (recordingIntervals)
252
    {
253
        delete recordingIntervals;
254
        recordingIntervals = NULL;
255
    }
256
}
257

    
258
void EventlogFileManager::flush()
259
{
260
    if (isEventLogRecordingEnabled)
261
    {
262
        fflush(feventlog);
263
    }
264
}
265

    
266
void EventlogFileManager::simulationEvent(cMessage *msg)
267
{
268
    cModule *mod = simulation.getContextModule();
269

    
270
    isModuleEventLogRecordingEnabled = simulation.getContextModule()->isRecordEvents();
271
    isIntervalEventLogRecordingEnabled = !recordingIntervals || recordingIntervals->contains(simulation.getSimTime());
272
    isEventLogRecordingEnabled = isModuleEventLogRecordingEnabled && isIntervalEventLogRecordingEnabled;
273

    
274
    if (isEventLogRecordingEnabled)
275
    {
276
        EventLogWriter::recordEventEntry_e_t_m_ce_msg(feventlog,
277
            simulation.getEventNumber(), simulation.getSimTime(), mod->getId(),
278
            msg->getPreviousEventNumber(), msg->getId());
279
    }
280
}
281

    
282
void EventlogFileManager::bubble(cComponent *component, const char *text)
283
{
284
    if (isEventLogRecordingEnabled)
285
    {
286
        if (dynamic_cast<cModule *>(component))
287
        {
288
            cModule *mod = (cModule *)component;
289
            EventLogWriter::recordBubbleEntry_id_txt(feventlog, mod->getId(), text);
290
        }
291
        else if (dynamic_cast<cChannel *>(component))
292
        {
293
            //TODO
294
        }
295
    }
296
}
297

    
298
void EventlogFileManager::beginSend(cMessage *msg)
299
{
300
    if (isEventLogRecordingEnabled)
301
    {
302
        //TODO record message display string as well?
303
        if (msg->isPacket()) {
304
            cPacket *pkt = (cPacket *)msg;
305
            EventLogWriter::recordBeginSendEntry_id_tid_eid_etid_c_n_pe_k_p_l_er_d(feventlog,
306
                pkt->getId(), pkt->getTreeId(), pkt->getEncapsulationId(), pkt->getEncapsulationTreeId(),
307
                pkt->getClassName(), pkt->getFullName(), pkt->getPreviousEventNumber(),
308
                pkt->getKind(), pkt->getSchedulingPriority(), pkt->getBitLength(), pkt->hasBitError(),
309
                objectPrinter ? objectPrinter->printObjectToString(pkt).c_str() : NULL);
310
        }
311
        else {
312
            EventLogWriter::recordBeginSendEntry_id_tid_eid_etid_c_n_pe_k_p_l_er_d(feventlog,
313
                msg->getId(), msg->getTreeId(), msg->getId(), msg->getTreeId(),
314
                msg->getClassName(), msg->getFullName(), msg->getPreviousEventNumber(),
315
                msg->getKind(), msg->getSchedulingPriority(), 0, false,
316
                objectPrinter ? objectPrinter->printObjectToString(msg).c_str() : NULL);
317
        }
318
    }
319
}
320

    
321
void EventlogFileManager::messageScheduled(cMessage *msg)
322
{
323
    if (isEventLogRecordingEnabled)
324
    {
325
        EventlogFileManager::beginSend(msg);
326
        EventlogFileManager::endSend(msg);
327
    }
328
}
329

    
330
void EventlogFileManager::messageCancelled(cMessage *msg)
331
{
332
    if (isEventLogRecordingEnabled)
333
    {
334
        EventLogWriter::recordCancelEventEntry_id_pe(feventlog, msg->getId(), msg->getPreviousEventNumber());
335
    }
336
}
337

    
338
void EventlogFileManager::messageSendDirect(cMessage *msg, cGate *toGate, simtime_t propagationDelay, simtime_t transmissionDelay)
339
{
340
    if (isEventLogRecordingEnabled)
341
    {
342
        EventLogWriter::recordSendDirectEntry_sm_dm_dg_pd_td(feventlog,
343
            msg->getSenderModuleId(), toGate->getOwnerModule()->getId(), toGate->getId(),
344
            propagationDelay, transmissionDelay);
345
    }
346
}
347

    
348
void EventlogFileManager::messageSendHop(cMessage *msg, cGate *srcGate)
349
{
350
    if (isEventLogRecordingEnabled)
351
    {
352
        EventLogWriter::recordSendHopEntry_sm_sg(feventlog,
353
            srcGate->getOwnerModule()->getId(), srcGate->getId());
354
    }
355
}
356

    
357
void EventlogFileManager::messageSendHop(cMessage *msg, cGate *srcGate, simtime_t propagationDelay, simtime_t transmissionDelay)
358
{
359
    if (isEventLogRecordingEnabled)
360
    {
361
        EventLogWriter::recordSendHopEntry_sm_sg_pd_td(feventlog,
362
            srcGate->getOwnerModule()->getId(), srcGate->getId(), propagationDelay, transmissionDelay);
363
    }
364
}
365

    
366
void EventlogFileManager::endSend(cMessage *msg)
367
{
368
    if (isEventLogRecordingEnabled)
369
    {
370
        bool isStart = msg->isPacket() ? ((cPacket *)msg)->isReceptionStart() : false;
371
        EventLogWriter::recordEndSendEntry_t_is(feventlog, msg->getArrivalTime(), isStart);
372
    }
373
}
374

    
375
void EventlogFileManager::messageDeleted(cMessage *msg)
376
{
377
    if (isEventLogRecordingEnabled)
378
    {
379
        EventLogWriter::recordDeleteMessageEntry_id_pe(feventlog, msg->getId(), msg->getPreviousEventNumber());
380
    }
381
}
382

    
383
void EventlogFileManager::componentMethodBegin(cComponent *from, cComponent *to, const char *methodFmt, va_list va)
384
{
385
    if (isEventLogRecordingEnabled)
386
    {
387
        if (from && from->isModule() && to->isModule())
388
        {
389
            const char *methodText = "";  // for the Enter_Method_Silent case
390
            if (methodFmt)
391
            {
392
                static char methodTextBuf[MAX_METHODCALL];
393
                vsnprintf(methodTextBuf, MAX_METHODCALL, methodFmt, va);
394
                methodTextBuf[MAX_METHODCALL-1] = '\0';
395
                methodText = methodTextBuf;
396
            }
397
            EventLogWriter::recordModuleMethodBeginEntry_sm_tm_m(feventlog,
398
                ((cModule *)from)->getId(), ((cModule *)to)->getId(), methodText);
399
        }
400
    }
401
}
402

    
403
void EventlogFileManager::componentMethodEnd()
404
{
405
    if (isEventLogRecordingEnabled)
406
    {
407
        //XXX problem when channel method is called: we'll emit an "End" entry but no "Begin"
408
        //XXX same problem when the caller is not a module or is NULL
409
        EventLogWriter::recordModuleMethodEndEntry(feventlog);
410
    }
411
}
412

    
413
void EventlogFileManager::moduleCreated(cModule *newmodule)
414
{
415
    if (isEventLogRecordingEnabled)
416
    {
417
        cModule *m = newmodule;
418

    
419
        bool recordModuleEvents = ev.getConfig()->getAsBool(m->getFullPath().c_str(), CFGID_MODULE_EVENTLOG_RECORDING);
420
        m->setRecordEvents(recordModuleEvents);
421

    
422
        bool isCompoundModule = dynamic_cast<cCompoundModule *>(m);
423

    
424
        EventLogWriter::recordModuleCreatedEntry_id_c_t_pid_n_cm(feventlog,
425
            m->getId(), m->getClassName(), m->getNedTypeName(), m->getParentModule() ? m->getParentModule()->getId() : -1, m->getFullName(), isCompoundModule); //FIXME size() is missing
426
    }
427
}
428

    
429
void EventlogFileManager::moduleDeleted(cModule *module)
430
{
431
    if (isEventLogRecordingEnabled)
432
    {
433
        EventLogWriter::recordModuleDeletedEntry_id(feventlog, module->getId());
434
    }
435
}
436

    
437
void EventlogFileManager::moduleReparented(cModule *module, cModule *oldparent)
438
{
439
    if (isEventLogRecordingEnabled)
440
    {
441
        EventLogWriter::recordModuleReparentedEntry_id_p(feventlog, module->getId(), module->getParentModule()->getId());
442
    }
443
}
444

    
445
void EventlogFileManager::gateCreated(cGate *newgate)
446
{
447
    if (isEventLogRecordingEnabled)
448
    {
449
        EventLogWriter::recordGateCreatedEntry_m_g_n_i_o(feventlog,
450
            newgate->getOwnerModule()->getId(), newgate->getId(), newgate->getName(),
451
            newgate->isVector() ? newgate->getIndex() : -1, newgate->getType() == cGate::OUTPUT);
452
    }
453
}
454

    
455
void EventlogFileManager::gateDeleted(cGate *gate)
456
{
457
    if (isEventLogRecordingEnabled)
458
    {
459
        EventLogWriter::recordGateDeletedEntry_m_g(feventlog, gate->getOwnerModule()->getId(), gate->getId());
460
    }
461
}
462

    
463
void EventlogFileManager::connectionCreated(cGate *srcgate)
464
{
465
    if (isEventLogRecordingEnabled)
466
    {
467
        cGate *destgate = srcgate->getNextGate();
468
        EventLogWriter::recordConnectionCreatedEntry_sm_sg_dm_dg(feventlog,
469
            srcgate->getOwnerModule()->getId(), srcgate->getId(), destgate->getOwnerModule()->getId(), destgate->getId());  //XXX channel, channel attributes, etc
470
    }
471
}
472

    
473
void EventlogFileManager::connectionDeleted(cGate *srcgate)
474
{
475
    if (isEventLogRecordingEnabled)
476
    {
477
        EventLogWriter::recordConnectionDeletedEntry_sm_sg(feventlog,
478
            srcgate->getOwnerModule()->getId(), srcgate->getId());
479
    }
480
}
481

    
482
void EventlogFileManager::displayStringChanged(cComponent *component)
483
{
484
    if (isEventLogRecordingEnabled)
485
    {
486
        if (dynamic_cast<cModule *>(component))
487
        {
488
            cModule *module = (cModule *)component;
489
            EventLogWriter::recordModuleDisplayStringChangedEntry_id_d(feventlog,
490
                module->getId(), module->getDisplayString().str());
491
        }
492
        else if (dynamic_cast<cChannel *>(component))
493
        {
494
            cChannel *channel = (cChannel *)component;
495
            cGate *gate = channel->getSourceGate();
496
            EventLogWriter::recordConnectionDisplayStringChangedEntry_sm_sg_d(feventlog,
497
                gate->getOwnerModule()->getId(), gate->getId(), channel->getDisplayString().str());
498
        }
499
    }
500
}
501

    
502
void EventlogFileManager::sputn(const char *s, int n)
503
{
504
    if (isEventLogRecordingEnabled)
505
    {
506
        EventLogWriter::recordLogLine(feventlog, s, n);
507
    }
508
}
509