Statistics
| Branch: | Revision:

root / src / envir / eventlogfilemgr.cc @ daf081df

History | View | Annotate | Download (19.3 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
#include "casyncmodule.h"
36

    
37
USING_NAMESPACE
38

    
39

    
40
Register_PerRunConfigOption(CFGID_EVENTLOG_FILE, "eventlog-file", CFG_FILENAME, "${resultdir}/${configname}-${runnumber}.elog", "Name of the event log file to generate.");
41
Register_PerRunConfigOption(CFGID_EVENTLOG_MESSAGE_DETAIL_PATTERN, "eventlog-message-detail-pattern", CFG_CUSTOM, NULL,
42
        "A list of patterns separated by '|' character which will be used to write "
43
        "message detail information into the event log for each message sent during "
44
        "the simulation. The message detail will be presented in the sequence chart "
45
        "tool. Each pattern starts with an object pattern optionally followed by ':' "
46
        "character and a comma separated list of field patterns. In both "
47
        "patterns and/or/not/* and various field match expressions can be used. "
48
        "The object pattern matches to class name, the field pattern matches to field name by default.\n"
49
        "  EVENTLOG-MESSAGE-DETAIL-PATTERN := ( DETAIL-PATTERN '|' )* DETAIL_PATTERN\n"
50
        "  DETAIL-PATTERN := OBJECT-PATTERN [ ':' FIELD-PATTERNS ]\n"
51
        "  OBJECT-PATTERN := MATCH-EXPRESSION\n"
52
        "  FIELD-PATTERNS := ( FIELD-PATTERN ',' )* FIELD_PATTERN\n"
53
        "  FIELD-PATTERN := MATCH-EXPRESSION\n"
54
        "Examples (enter them without quotes):\n"
55
        "  \"*\": captures all fields of all messages\n"
56
        "  \"*Frame:*Address,*Id\": captures all fields named somethingAddress and somethingId from messages of any class named somethingFrame\n"
57
        "  \"MyMessage:declaredOn(MyMessage)\": captures instances of MyMessage recording the fields declared on the MyMessage class\n"
58
        "  \"*:(not declaredOn(cMessage) and not declaredOn(cNamedObject) and not declaredOn(cObject))\": records user-defined fields from all messages");
59
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..");
60
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");
61

    
62
static va_list empty_va;
63

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
247
bool EventlogFileManager::hasRecordingIntervals() const
248
{
249
    return recordingIntervals && !recordingIntervals->empty();
250
}
251

    
252
void EventlogFileManager::clearRecordingIntervals()
253
{
254
    if (recordingIntervals)
255
    {
256
        delete recordingIntervals;
257
        recordingIntervals = NULL;
258
    }
259
}
260

    
261
void EventlogFileManager::flush()
262
{
263
    if (isEventLogRecordingEnabled)
264
    {
265
        fflush(feventlog);
266
    }
267
}
268

    
269
void EventlogFileManager::simulationEvent(cMessage *msg)
270
{
271
    cModule *mod = simulation.getContextModule();
272

    
273
    isModuleEventLogRecordingEnabled = simulation.getContextModule()->isRecordEvents();
274
    isIntervalEventLogRecordingEnabled = !recordingIntervals || recordingIntervals->contains(simulation.getSimTime());
275
    isEventLogRecordingEnabled = isModuleEventLogRecordingEnabled && isIntervalEventLogRecordingEnabled;
276

    
277
    if (isEventLogRecordingEnabled)
278
    {
279
        log_eventNumber = simulation.getEventNumber();
280
        log_simtime = simulation.getSimTime();
281
        log_modid = mod->getId();
282
        log_previd =msg->getPreviousEventNumber();
283
        log_msgid = msg->getId();
284
        log_duration = msg->getEventDuration();
285

    
286
        EventLogWriter::recordEventEntry_e_t_m_ce_msg_d(feventlog,
287
                log_eventNumber, log_simtime, log_modid,
288
                log_previd, log_msgid, log_duration);
289
    }
290
}
291

    
292
void EventlogFileManager::simulationEventEnd(double complexity) {
293
    if (isEventLogRecordingEnabled)
294
    {
295
        EventLogWriter::recordEventEndEntry_e_c(feventlog, log_eventNumber,
296
                        ((long)(1000*1000*complexity)));   //Store complexity in microseconds
297
    }
298
}
299

    
300
void EventlogFileManager::bubble(cComponent *component, const char *text)
301
{
302
    if (isEventLogRecordingEnabled)
303
    {
304
        if (dynamic_cast<cModule *>(component))
305
        {
306
            cModule *mod = (cModule *)component;
307
            EventLogWriter::recordBubbleEntry_id_txt(feventlog, mod->getId(), text);
308
        }
309
        else if (dynamic_cast<cChannel *>(component))
310
        {
311
            //TODO
312
        }
313
    }
314
}
315

    
316
void EventlogFileManager::beginSend(cMessage *msg)
317
{
318
    if (isEventLogRecordingEnabled)
319
    {
320
        //TODO record message display string as well?
321
        if (msg->isPacket()) {
322
            cPacket *pkt = (cPacket *)msg;
323
            EventLogWriter::recordBeginSendEntry_id_tid_eid_etid_c_n_pe_k_p_l_er_d(feventlog,
324
                pkt->getId(), pkt->getTreeId(), pkt->getEncapsulationId(), pkt->getEncapsulationTreeId(),
325
                pkt->getClassName(), pkt->getFullName(), pkt->getPreviousEventNumber(),
326
                pkt->getKind(), pkt->getSchedulingPriority(), pkt->getBitLength(), pkt->hasBitError(),
327
                objectPrinter ? objectPrinter->printObjectToString(pkt).c_str() : NULL);
328
        }
329
        else {
330
            EventLogWriter::recordBeginSendEntry_id_tid_eid_etid_c_n_pe_k_p_l_er_d(feventlog,
331
                msg->getId(), msg->getTreeId(), msg->getId(), msg->getTreeId(),
332
                msg->getClassName(), msg->getFullName(), msg->getPreviousEventNumber(),
333
                msg->getKind(), msg->getSchedulingPriority(), 0, false,
334
                objectPrinter ? objectPrinter->printObjectToString(msg).c_str() : NULL);
335
        }
336
    }
337
}
338

    
339
void EventlogFileManager::messageScheduled(cMessage *msg)
340
{
341
    if (isEventLogRecordingEnabled)
342
    {
343
        EventlogFileManager::beginSend(msg);
344
        EventlogFileManager::endSend(msg);
345
    }
346
}
347

    
348
void EventlogFileManager::messageCancelled(cMessage *msg)
349
{
350
    if (isEventLogRecordingEnabled)
351
    {
352
        EventLogWriter::recordCancelEventEntry_id_pe(feventlog, msg->getId(), msg->getPreviousEventNumber());
353
    }
354
}
355

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

    
366
void EventlogFileManager::messageSendHop(cMessage *msg, cGate *srcGate)
367
{
368
    if (isEventLogRecordingEnabled)
369
    {
370
        EventLogWriter::recordSendHopEntry_sm_sg(feventlog,
371
            srcGate->getOwnerModule()->getId(), srcGate->getId());
372
    }
373
}
374

    
375
void EventlogFileManager::messageSendHop(cMessage *msg, cGate *srcGate, simtime_t propagationDelay, simtime_t transmissionDelay)
376
{
377
    if (isEventLogRecordingEnabled)
378
    {
379
        EventLogWriter::recordSendHopEntry_sm_sg_pd_td(feventlog,
380
            srcGate->getOwnerModule()->getId(), srcGate->getId(), propagationDelay, transmissionDelay);
381
    }
382
}
383

    
384
void EventlogFileManager::endSend(cMessage *msg)
385
{
386
    if (isEventLogRecordingEnabled)
387
    {
388
        bool isStart = msg->isPacket() ? ((cPacket *)msg)->isReceptionStart() : false;
389
        EventLogWriter::recordEndSendEntry_t_is(feventlog, msg->getArrivalTime(), isStart);
390
    }
391
}
392

    
393
void EventlogFileManager::messageDeleted(cMessage *msg)
394
{
395
    if (isEventLogRecordingEnabled)
396
    {
397
        EventLogWriter::recordDeleteMessageEntry_id_pe(feventlog, msg->getId(), msg->getPreviousEventNumber());
398
    }
399
}
400

    
401
void EventlogFileManager::componentMethodBegin(cComponent *from, cComponent *to, const char *methodFmt, va_list va)
402
{
403
    if (isEventLogRecordingEnabled)
404
    {
405
        if (from && from->isModule() && to->isModule())
406
        {
407
            const char *methodText = "";  // for the Enter_Method_Silent case
408
            if (methodFmt)
409
            {
410
                static char methodTextBuf[MAX_METHODCALL];
411
                vsnprintf(methodTextBuf, MAX_METHODCALL, methodFmt, va);
412
                methodTextBuf[MAX_METHODCALL-1] = '\0';
413
                methodText = methodTextBuf;
414
            }
415
            EventLogWriter::recordModuleMethodBeginEntry_sm_tm_m(feventlog,
416
                ((cModule *)from)->getId(), ((cModule *)to)->getId(), methodText);
417
        }
418
    }
419
}
420

    
421
void EventlogFileManager::componentMethodEnd()
422
{
423
    if (isEventLogRecordingEnabled)
424
    {
425
        //XXX problem when channel method is called: we'll emit an "End" entry but no "Begin"
426
        //XXX same problem when the caller is not a module or is NULL
427
        EventLogWriter::recordModuleMethodEndEntry(feventlog);
428
    }
429
}
430

    
431
void EventlogFileManager::moduleCreated(cModule *newmodule)
432
{
433
    if (isEventLogRecordingEnabled)
434
    {
435
        cModule *m = newmodule;
436

    
437
        bool recordModuleEvents = ev.getConfig()->getAsBool(m->getFullPath().c_str(), CFGID_MODULE_EVENTLOG_RECORDING);
438
        m->setRecordEvents(recordModuleEvents);
439

    
440
        bool isCompoundModule = dynamic_cast<cCompoundModule *>(m);
441

    
442
        EventLogWriter::recordModuleCreatedEntry_id_c_t_pid_n_cm(feventlog,
443
            m->getId(), m->getClassName(), m->getNedTypeName(), m->getParentModule() ? m->getParentModule()->getId() : -1, m->getFullName(), isCompoundModule); //FIXME size() is missing
444
    }
445
}
446

    
447
void EventlogFileManager::moduleDeleted(cModule *module)
448
{
449
    if (isEventLogRecordingEnabled)
450
    {
451
        EventLogWriter::recordModuleDeletedEntry_id(feventlog, module->getId());
452
    }
453
}
454

    
455
void EventlogFileManager::moduleReparented(cModule *module, cModule *oldparent)
456
{
457
    if (isEventLogRecordingEnabled)
458
    {
459
        EventLogWriter::recordModuleReparentedEntry_id_p(feventlog, module->getId(), module->getParentModule()->getId());
460
    }
461
}
462

    
463
void EventlogFileManager::gateCreated(cGate *newgate)
464
{
465
    if (isEventLogRecordingEnabled)
466
    {
467
        EventLogWriter::recordGateCreatedEntry_m_g_n_i_o(feventlog,
468
            newgate->getOwnerModule()->getId(), newgate->getId(), newgate->getName(),
469
            newgate->isVector() ? newgate->getIndex() : -1, newgate->getType() == cGate::OUTPUT);
470
    }
471
}
472

    
473
void EventlogFileManager::gateDeleted(cGate *gate)
474
{
475
    if (isEventLogRecordingEnabled)
476
    {
477
        EventLogWriter::recordGateDeletedEntry_m_g(feventlog, gate->getOwnerModule()->getId(), gate->getId());
478
    }
479
}
480

    
481
void EventlogFileManager::connectionCreated(cGate *srcgate)
482
{
483
    if (isEventLogRecordingEnabled)
484
    {
485
        cGate *destgate = srcgate->getNextGate();
486
        EventLogWriter::recordConnectionCreatedEntry_sm_sg_dm_dg(feventlog,
487
            srcgate->getOwnerModule()->getId(), srcgate->getId(), destgate->getOwnerModule()->getId(), destgate->getId());  //XXX channel, channel attributes, etc
488
    }
489
}
490

    
491
void EventlogFileManager::connectionDeleted(cGate *srcgate)
492
{
493
    if (isEventLogRecordingEnabled)
494
    {
495
        EventLogWriter::recordConnectionDeletedEntry_sm_sg(feventlog,
496
            srcgate->getOwnerModule()->getId(), srcgate->getId());
497
    }
498
}
499

    
500
void EventlogFileManager::displayStringChanged(cComponent *component)
501
{
502
    if (isEventLogRecordingEnabled)
503
    {
504
        if (dynamic_cast<cModule *>(component))
505
        {
506
            cModule *module = (cModule *)component;
507
            EventLogWriter::recordModuleDisplayStringChangedEntry_id_d(feventlog,
508
                module->getId(), module->getDisplayString().str());
509
        }
510
        else if (dynamic_cast<cChannel *>(component))
511
        {
512
            cChannel *channel = (cChannel *)component;
513
            cGate *gate = channel->getSourceGate();
514
            EventLogWriter::recordConnectionDisplayStringChangedEntry_sm_sg_d(feventlog,
515
                gate->getOwnerModule()->getId(), gate->getId(), channel->getDisplayString().str());
516
        }
517
    }
518
}
519

    
520
void EventlogFileManager::sputn(const char *s, int n)
521
{
522
    if (isEventLogRecordingEnabled)
523
    {
524
        EventLogWriter::recordLogLine(feventlog, s, n);
525
    }
526
}
527