~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/common/debugger/SignalLoggerManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <ndb_global.h>
 
17
 
 
18
#include "SignalLoggerManager.hpp"
 
19
#include <LongSignal.hpp>
 
20
 
 
21
#include <DebuggerNames.hpp>
 
22
 
 
23
SignalLoggerManager::SignalLoggerManager()
 
24
{
 
25
  for (int i = 0; i < NO_OF_BLOCKS; i++){
 
26
      logModes[i] = 0;
 
27
  }
 
28
  outputStream = 0;
 
29
  m_ownNodeId = 0;
 
30
  m_logDistributed = false;
 
31
}
 
32
 
 
33
SignalLoggerManager::~SignalLoggerManager()
 
34
{
 
35
  if(outputStream != 0){
 
36
    fflush(outputStream);
 
37
    fclose(outputStream);
 
38
    outputStream = 0;
 
39
  }
 
40
}
 
41
 
 
42
FILE *
 
43
SignalLoggerManager::setOutputStream(FILE * output)
 
44
{
 
45
  if(outputStream != 0){
 
46
    fflush(outputStream);
 
47
  }
 
48
 
 
49
  FILE * out = outputStream;
 
50
  outputStream = output;
 
51
  return out;
 
52
}
 
53
 
 
54
FILE *
 
55
SignalLoggerManager::getOutputStream() const
 
56
{
 
57
  return outputStream;
 
58
}
 
59
 
 
60
void
 
61
SignalLoggerManager::flushSignalLog()
 
62
{
 
63
  if(outputStream != 0)
 
64
    fflush(outputStream);
 
65
}
 
66
 
 
67
void
 
68
SignalLoggerManager::setTrace(unsigned long trace)
 
69
{
 
70
  traceId = trace;
 
71
}
 
72
 
 
73
unsigned long
 
74
SignalLoggerManager::getTrace() const
 
75
{
 
76
  return traceId;
 
77
}
 
78
 
 
79
void
 
80
SignalLoggerManager::setOwnNodeId(int nodeId){
 
81
  m_ownNodeId = nodeId;
 
82
}
 
83
  
 
84
void
 
85
SignalLoggerManager::setLogDistributed(bool val){
 
86
  m_logDistributed = val;
 
87
}
 
88
 
 
89
int
 
90
getParameter(char *blocks[NO_OF_BLOCKS], const char * par, const char * line)
 
91
{
 
92
  const char * loc = strstr(line, par);
 
93
  if(loc == NULL)
 
94
    return 0;
 
95
 
 
96
  loc += strlen(par);
 
97
 
 
98
  int found = 0;
 
99
 
 
100
  char * copy = strdup(loc);
 
101
  char * tmp = copy;
 
102
  bool done = false;
 
103
  while(!done){
 
104
    int len = strcspn(tmp, ", ;:\0");
 
105
    if(len == 0)
 
106
      done = true;
 
107
    else {
 
108
      if(* (tmp + len) != ',')
 
109
        done = true;
 
110
      * (tmp + len) = 0;
 
111
      blocks[found] = strdup(tmp);
 
112
      found ++;
 
113
      tmp += (len + 1);
 
114
    } 
 
115
  }
 
116
  free(copy);
 
117
  return found;
 
118
}
 
119
 
 
120
 
 
121
#define SLM_OFF    0
 
122
#define SLM_ON     1
 
123
#define SLM_TOGGLE 2
 
124
 
 
125
int
 
126
SignalLoggerManager::log(LogMode logMode, const char * params)
 
127
{
 
128
  char * blocks[NO_OF_BLOCKS];
 
129
  const int count = getParameter(blocks, "BLOCK=", params);
 
130
  
 
131
  int cnt = 0;
 
132
  if((count == 1 && !strcmp(blocks[0], "ALL")) ||
 
133
     count == 0){
 
134
    
 
135
    for (int number = 0; number < NO_OF_BLOCKS; ++number){
 
136
      cnt += log(SLM_ON, number, logMode);
 
137
    }
 
138
  } else {
 
139
    for (int i = 0; i < count; ++i){
 
140
      BlockNumber number = getBlockNo(blocks[i]);
 
141
      cnt += log(SLM_ON, number, logMode);
 
142
    }
 
143
  }
 
144
  for(int i = 0; i<count; i++){
 
145
    free(blocks[i]);
 
146
  }
 
147
 
 
148
  return cnt;
 
149
}
 
150
 
 
151
int
 
152
SignalLoggerManager::log(int cmd, BlockNumber bno, LogMode logMode)
 
153
{
 
154
  // Normalise blocknumber for use in logModes array
 
155
  const BlockNumber bno2 = bno-MIN_BLOCK_NO;
 
156
  assert(bno2<NO_OF_BLOCKS);
 
157
  switch(cmd){
 
158
  case SLM_ON:
 
159
    logModes[bno2] |= logMode;
 
160
    return 1;
 
161
    break;
 
162
  case SLM_OFF:
 
163
    logModes[bno2] &= (~logMode);
 
164
    return 1;
 
165
    break;
 
166
  case SLM_TOGGLE:
 
167
    logModes[bno2] ^= logMode;
 
168
    return 1;
 
169
    break;
 
170
  }
 
171
  return 0;
 
172
}
 
173
 
 
174
int
 
175
SignalLoggerManager::logOn(bool allBlocks, BlockNumber bno, LogMode logMode)
 
176
{
 
177
  if(!allBlocks){
 
178
    return log(SLM_ON, bno, logMode);
 
179
  } 
 
180
  int cnt = 0;
 
181
  for(unsigned int i = MIN_BLOCK_NO; i <= MAX_BLOCK_NO; i++)
 
182
    cnt += log(SLM_ON, i, logMode);
 
183
  return cnt;
 
184
}
 
185
 
 
186
int
 
187
SignalLoggerManager::logOff(bool allBlocks, BlockNumber bno, LogMode logMode)
 
188
{
 
189
  if(!allBlocks){
 
190
    return log(SLM_OFF, bno, logMode);
 
191
  } 
 
192
  int cnt = 0;
 
193
  for(unsigned int i = MIN_BLOCK_NO; i <= MAX_BLOCK_NO; i++)
 
194
    cnt += log(SLM_OFF, i, logMode);
 
195
  return cnt;
 
196
 
 
197
}
 
198
 
 
199
int
 
200
SignalLoggerManager::logToggle(bool allBlocks, BlockNumber bno, LogMode logMode)
 
201
{
 
202
  if(!allBlocks){
 
203
    return log(SLM_TOGGLE, bno, logMode);
 
204
  } 
 
205
  int cnt = 0;
 
206
  for(unsigned int i = MIN_BLOCK_NO; i <= MAX_BLOCK_NO; i++)
 
207
    cnt += log(SLM_TOGGLE, i, logMode);
 
208
  return cnt;
 
209
}
 
210
 
 
211
void
 
212
SignalLoggerManager::executeDirect(const SignalHeader& sh, 
 
213
                                   Uint8 prio,  // in-out flag
 
214
                                   const Uint32 * theData, Uint32 node)
 
215
{
 
216
  Uint32 trace = sh.theTrace;
 
217
  Uint32 senderBlockNo = refToBlock(sh.theSendersBlockRef);
 
218
  Uint32 receiverBlockNo = sh.theReceiversBlockNumber;
 
219
  
 
220
  if(outputStream != 0 && 
 
221
     (traceId == 0 || traceId == trace) &&
 
222
     (logMatch(senderBlockNo, LogOut) || logMatch(receiverBlockNo, LogIn))){
 
223
    const char* inOutStr = prio == 0 ? "In" : "Out";
 
224
#ifdef VM_TRACE_TIME
 
225
    fprintf(outputStream, "---- Direct --- Signal --- %s - %d ----\n", inOutStr, time(0));
 
226
#else
 
227
    fprintf(outputStream, "---- Direct --- Signal --- %s ----------------\n", inOutStr);
 
228
#endif
 
229
    // XXX pass in/out to print* function somehow
 
230
    printSignalHeader(outputStream, sh, 0, node, true);
 
231
    printSignalData(outputStream, sh, theData);
 
232
  }
 
233
}
 
234
 
 
235
/**
 
236
 * For input signals
 
237
 */
 
238
void
 
239
SignalLoggerManager::executeSignal(const SignalHeader& sh, Uint8 prio, 
 
240
                                   const Uint32 * theData, Uint32 node,
 
241
                                   const SegmentedSectionPtr ptr[3], Uint32 secs)
 
242
{
 
243
  Uint32 trace = sh.theTrace;
 
244
  //Uint32 senderBlockNo = refToBlock(sh.theSendersBlockRef);
 
245
  Uint32 receiverBlockNo = sh.theReceiversBlockNumber;
 
246
  Uint32 senderNode = refToNode(sh.theSendersBlockRef);
 
247
 
 
248
  if(outputStream != 0 && 
 
249
     (traceId == 0 || traceId == trace) &&
 
250
     (logMatch(receiverBlockNo, LogOut) ||
 
251
      (m_logDistributed && m_ownNodeId != senderNode))){
 
252
#ifdef VM_TRACE_TIME
 
253
    fprintf(outputStream, "---- Received - Signal - %d ----\n", time(0));
 
254
#else
 
255
    fprintf(outputStream, "---- Received - Signal ----------------\n");
 
256
#endif
 
257
 
 
258
    printSignalHeader(outputStream, sh, prio, node, true);
 
259
    printSignalData(outputStream, sh, theData);
 
260
    for (unsigned i = 0; i < secs; i++)
 
261
      printSegmentedSection(outputStream, sh, ptr, i);
 
262
  }
 
263
}
 
264
 
 
265
void
 
266
SignalLoggerManager::executeSignal(const SignalHeader& sh, Uint8 prio, 
 
267
                                   const Uint32 * theData, Uint32 node,
 
268
                                   const LinearSectionPtr ptr[3], Uint32 secs)
 
269
{
 
270
  Uint32 trace = sh.theTrace;
 
271
  //Uint32 senderBlockNo = refToBlock(sh.theSendersBlockRef);
 
272
  Uint32 receiverBlockNo = sh.theReceiversBlockNumber;
 
273
  Uint32 senderNode = refToNode(sh.theSendersBlockRef);
 
274
 
 
275
  if(outputStream != 0 && 
 
276
     (traceId == 0 || traceId == trace) &&
 
277
     (logMatch(receiverBlockNo, LogOut) ||
 
278
      (m_logDistributed && m_ownNodeId != senderNode))){
 
279
#ifdef VM_TRACE_TIME
 
280
    fprintf(outputStream, "---- Received - Signal - %d ----\n", time(0));
 
281
#else
 
282
    fprintf(outputStream, "---- Received - Signal ----------------\n");
 
283
#endif
 
284
 
 
285
    printSignalHeader(outputStream, sh, prio, node, true);
 
286
    printSignalData(outputStream, sh, theData);
 
287
    for (unsigned i = 0; i < secs; i++)
 
288
      printLinearSection(outputStream, sh, ptr, i);
 
289
  }
 
290
}
 
291
 
 
292
/**
 
293
 * For output signals
 
294
 */
 
295
void
 
296
SignalLoggerManager::sendSignal(const SignalHeader& sh,
 
297
                                Uint8 prio,
 
298
                                const Uint32 * theData, Uint32 node,
 
299
                                const LinearSectionPtr ptr[3], Uint32 secs)
 
300
{
 
301
  Uint32 trace = sh.theTrace;
 
302
  Uint32 senderBlockNo = refToBlock(sh.theSendersBlockRef);
 
303
  //Uint32 receiverBlockNo = sh.theReceiversBlockNumber;
 
304
 
 
305
  if(outputStream != 0 && 
 
306
     (traceId == 0 || traceId == trace) &&
 
307
     (logMatch(senderBlockNo, LogOut) ||
 
308
      (m_logDistributed && m_ownNodeId != node))){
 
309
#ifdef VM_TRACE_TIME
 
310
    fprintf(outputStream, "---- Send ----- Signal - %d ----\n", time(0));
 
311
#else
 
312
    fprintf(outputStream, "---- Send ----- Signal ----------------\n");
 
313
#endif
 
314
 
 
315
    printSignalHeader(outputStream, sh, prio, node, false);
 
316
    printSignalData(outputStream, sh, theData);
 
317
    for (unsigned i = 0; i < secs; i++)
 
318
      printLinearSection(outputStream, sh, ptr, i);
 
319
  }
 
320
}
 
321
 
 
322
/**
 
323
 * For output signals
 
324
 */
 
325
void
 
326
SignalLoggerManager::sendSignal(const SignalHeader& sh, Uint8 prio, 
 
327
                                const Uint32 * theData, Uint32 node,
 
328
                                const SegmentedSectionPtr ptr[3], Uint32 secs)
 
329
{
 
330
  Uint32 trace = sh.theTrace;
 
331
  Uint32 senderBlockNo = refToBlock(sh.theSendersBlockRef);
 
332
  //Uint32 receiverBlockNo = sh.theReceiversBlockNumber;
 
333
 
 
334
  if(outputStream != 0 && 
 
335
     (traceId == 0 || traceId == trace) &&
 
336
     (logMatch(senderBlockNo, LogOut) ||
 
337
      (m_logDistributed && m_ownNodeId != node))){
 
338
#ifdef VM_TRACE_TIME
 
339
    fprintf(outputStream, "---- Send ----- Signal - %d ----\n", time(0));
 
340
#else
 
341
    fprintf(outputStream, "---- Send ----- Signal ----------------\n");
 
342
#endif
 
343
 
 
344
    printSignalHeader(outputStream, sh, prio, node, false);
 
345
    printSignalData(outputStream, sh, theData);
 
346
    for (unsigned i = 0; i < secs; i++)
 
347
      printSegmentedSection(outputStream, sh, ptr, i);
 
348
  }
 
349
}
 
350
 
 
351
void
 
352
SignalLoggerManager::sendSignalWithDelay(Uint32 delayInMilliSeconds,
 
353
                                         const SignalHeader & sh, Uint8 prio, 
 
354
                                         const Uint32 * theData, Uint32 node,
 
355
                                         const SegmentedSectionPtr ptr[3], Uint32 secs)
 
356
{
 
357
  Uint32 trace = sh.theTrace;
 
358
  Uint32 senderBlockNo = refToBlock(sh.theSendersBlockRef);
 
359
  //Uint32 receiverBlockNo = sh.theReceiversBlockNumber;
 
360
 
 
361
  if(outputStream != 0 && 
 
362
     (traceId == 0 || traceId == trace) &&
 
363
     logMatch(senderBlockNo, LogOut)){
 
364
#ifdef VM_TRACE_TIME
 
365
    fprintf(outputStream, 
 
366
            "---- Send ----- Signal (%d ms) %d\n", 
 
367
            delayInMilliSeconds, 
 
368
            time(0));
 
369
#else
 
370
    fprintf(outputStream, "---- Send delay Signal (%d ms) ----------\n", 
 
371
            delayInMilliSeconds);
 
372
#endif
 
373
 
 
374
    printSignalHeader(outputStream, sh, prio, node, false);    
 
375
    printSignalData(outputStream, sh, theData);
 
376
    for (unsigned i = 0; i < secs; i++)
 
377
      printSegmentedSection(outputStream, sh, ptr, i);
 
378
  }
 
379
}
 
380
 
 
381
/**
 
382
 * Generic messages in the signal log
 
383
 */
 
384
void
 
385
SignalLoggerManager::log(BlockNumber bno, const char * msg, ...)
 
386
{
 
387
  // Normalise blocknumber for use in logModes array
 
388
  const BlockNumber bno2 = bno - MIN_BLOCK_NO;
 
389
  assert(bno2<NO_OF_BLOCKS);
 
390
 
 
391
  if(outputStream != 0 &&
 
392
     logModes[bno2] != LogOff){
 
393
    va_list ap;
 
394
    va_start(ap, msg);
 
395
    fprintf(outputStream, "%s: ", getBlockName(bno, "API"));
 
396
    vfprintf(outputStream, msg, ap);
 
397
    fprintf(outputStream, "\n");
 
398
    va_end(ap);
 
399
  }
 
400
}
 
401
 
 
402
 
 
403
void 
 
404
SignalLoggerManager::printSignalHeader(FILE * output, 
 
405
                                       const SignalHeader & sh,
 
406
                                       Uint8 prio, 
 
407
                                       Uint32 node,
 
408
                                       bool printReceiversSignalId)
 
409
{
 
410
  Uint32 receiverBlockNo = sh.theReceiversBlockNumber;
 
411
  Uint32 receiverProcessor = node;
 
412
  Uint32 gsn = sh.theVerId_signalNumber;
 
413
  Uint32 senderBlockNo = refToBlock(sh.theSendersBlockRef);
 
414
  Uint32 senderProcessor = refToNode(sh.theSendersBlockRef);
 
415
  Uint32 length = sh.theLength;
 
416
  Uint32 trace = sh.theTrace;
 
417
  Uint32 rSigId = sh.theSignalId;
 
418
  Uint32 sSigId = sh.theSendersSignalId;
 
419
 
 
420
  const char * signalName = getSignalName(gsn);
 
421
  const char * rBlockName = getBlockName(receiverBlockNo, "API");
 
422
  const char * sBlockName = getBlockName(senderBlockNo, "API");
 
423
  
 
424
  if(printReceiversSignalId)
 
425
    fprintf(output, 
 
426
            "r.bn: %d \"%s\", r.proc: %d, r.sigId: %d gsn: %d \"%s\" prio: %d\n"
 
427
            ,receiverBlockNo, rBlockName, receiverProcessor, rSigId, 
 
428
            gsn, signalName, prio);
 
429
  else 
 
430
    fprintf(output,
 
431
            "r.bn: %d \"%s\", r.proc: %d, gsn: %d \"%s\" prio: %d\n",
 
432
            receiverBlockNo, rBlockName, receiverProcessor, gsn, 
 
433
            signalName, prio);
 
434
  
 
435
  fprintf(output, 
 
436
          "s.bn: %d \"%s\", s.proc: %d, s.sigId: %d length: %d trace: %d "
 
437
          "#sec: %d fragInf: %d\n",
 
438
          senderBlockNo, sBlockName, senderProcessor, sSigId, length, trace,
 
439
          sh.m_noOfSections, sh.m_fragmentInfo);
 
440
}
 
441
 
 
442
void
 
443
SignalLoggerManager::printSignalData(FILE * output, 
 
444
                                     const SignalHeader & sh,
 
445
                                     const Uint32 * signalData)
 
446
{
 
447
  Uint32 len = sh.theLength;
 
448
  SignalDataPrintFunction printFunction = 
 
449
    findPrintFunction(sh.theVerId_signalNumber);
 
450
  
 
451
  bool ok = false;      // done with printing
 
452
  if(printFunction != 0){
 
453
    ok = (* printFunction)(output, signalData, len, sh.theReceiversBlockNumber);
 
454
  }
 
455
  if(!ok){
 
456
    while(len >= 7){
 
457
      fprintf(output, 
 
458
              " H\'%.8x H\'%.8x H\'%.8x H\'%.8x H\'%.8x H\'%.8x H\'%.8x\n",
 
459
              signalData[0], signalData[1], signalData[2], signalData[3], 
 
460
              signalData[4], signalData[5], signalData[6]);
 
461
      len -= 7;
 
462
      signalData += 7;
 
463
    }
 
464
    if(len > 0){
 
465
      for(Uint32 i = 0; i<len; i++)
 
466
        fprintf(output, " H\'%.8x", signalData[i]);
 
467
      fprintf(output, "\n");
 
468
    }
 
469
  }
 
470
}
 
471
 
 
472
void
 
473
SignalLoggerManager::printLinearSection(FILE * output,
 
474
                                        const SignalHeader & sh,
 
475
                                        const LinearSectionPtr ptr[3],
 
476
                                        unsigned i)
 
477
{
 
478
  fprintf(output, "SECTION %u type=linear", i);
 
479
  if (i >= 3) {
 
480
    fprintf(output, " *** invalid ***\n");
 
481
    return;
 
482
  }
 
483
  const Uint32 len = ptr[i].sz;
 
484
  const Uint32 * data = ptr[i].p;
 
485
  Uint32 pos = 0;
 
486
  fprintf(output, " size=%u\n", (unsigned)len);
 
487
  while (pos < len) {
 
488
    printDataWord(output, pos, data[pos]);
 
489
  }
 
490
  if (len > 0)
 
491
    putc('\n', output);
 
492
}
 
493
 
 
494
void
 
495
SignalLoggerManager::printDataWord(FILE * output, Uint32 & pos, const Uint32 data)
 
496
{
 
497
  const char* const hex = "0123456789abcdef";
 
498
  if (pos > 0 && pos % 7 == 0)
 
499
    putc('\n', output);
 
500
  putc(' ', output);
 
501
  putc('H', output);
 
502
  putc('\'', output);
 
503
  for (int i = 7; i >= 0; i--)
 
504
    putc(hex[(data >> (i << 2)) & 0xf], output);
 
505
  pos++;
 
506
}