1 /*
2    Copyright (C) 2003-2008 MySQL AB, 2008 Sun Microsystems, Inc.
3     All rights reserved. Use is subject to license terms.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 //****************************************************************************
27 //
28 // .NAME
29 //      SignalLoggerManager - Handle signal loggers
30 //
31 //****************************************************************************
32 #ifndef SignalLoggerManager_H
33 #define SignalLoggerManager_H
34 
35 
36 #include <kernel_types.h>
37 #include <BlockNumbers.h>
38 #include <TransporterDefinitions.hpp>
39 #include <RefConvert.hpp>
40 
41 class SignalLoggerManager
42 {
43 public:
44   SignalLoggerManager();
45   virtual ~SignalLoggerManager();
46 
47   /**
48    * Sets output
49    * @Returns old output stream
50    */
51   FILE * setOutputStream(FILE * output);
52 
53   /**
54    * Gets current output
55    */
56   FILE * getOutputStream() const;
57 
58   void flushSignalLog();
59 
60   /**
61    * For direct signals
62    * @See also SimulatedBlock EXECUTE_DIRECT
63    */
64   void executeDirect(const SignalHeader&,
65 		     Uint8 prio, const Uint32 * theData, Uint32 node);
66 
67   /**
68    * For input signals
69    */
executeSignal(const SignalHeader & sh,Uint8 prio,const Uint32 * theData,Uint32 node)70   void executeSignal(const SignalHeader& sh, Uint8 prio,
71 		     const Uint32 * theData, Uint32 node) {
72     executeSignal(sh, prio, theData, node, (LinearSectionPtr*)0, 0);
73   }
74 
75   void executeSignal(const SignalHeader&, Uint8 prio,
76                      const Uint32 * theData, Uint32 node,
77                      const SegmentedSectionPtr ptr[3], Uint32 secs);
78 
79   void executeSignal(const SignalHeader&, Uint8 prio,
80                      const Uint32 * theData, Uint32 node,
81                      const LinearSectionPtr ptr[3], Uint32 secs);
82 
83   /**
84    * For output signals
85    */
sendSignal(const SignalHeader & sh,Uint8 prio,const Uint32 * theData,Uint32 node)86   void sendSignal(const SignalHeader& sh, Uint8 prio,
87 		  const Uint32 * theData, Uint32 node) {
88     sendSignal(sh, prio, theData, node, (LinearSectionPtr*)0, 0);
89   }
90 
91   void sendSignal(const SignalHeader&, Uint8 prio,
92 		  const Uint32 * theData, Uint32 node,
93                   const SegmentedSectionPtr ptr[3], Uint32 secs);
94 
95   void sendSignal(const SignalHeader&, Uint8 prio,
96 		  const Uint32 * theData, Uint32 node,
97                   const LinearSectionPtr ptr[3], Uint32 secs);
98 
99   void sendSignal(const SignalHeader&, Uint8 prio,
100                   const Uint32 * theData, Uint32 node,
101                   const GenericSectionPtr ptr[3], Uint32 secs);
102 
103   /**
104    * For output signals
105    */
sendSignalWithDelay(Uint32 delayInMilliSeconds,const SignalHeader & sh,Uint8 prio,const Uint32 * data,Uint32 node)106   void sendSignalWithDelay(Uint32 delayInMilliSeconds,
107 			   const SignalHeader& sh,
108 			   Uint8 prio, const Uint32 * data, Uint32 node){
109     sendSignalWithDelay(delayInMilliSeconds, sh, prio, data, node,
110 			(SegmentedSectionPtr*)0, 0);
111   }
112 
113   void sendSignalWithDelay(Uint32 delayInMilliSeconds,
114 			   const SignalHeader&,
115 			   Uint8 prio, const Uint32 * data, Uint32 node,
116                            const SegmentedSectionPtr ptr[3], Uint32 secs);
117 
118   /**
119    * Generic messages in the signal log
120    */
121   void log(BlockNumber bno, const char * msg, ...)
122     ATTRIBUTE_FORMAT(printf, 3, 4);
123 
124   /**
125    * LogModes
126    */
127   enum LogMode {
128     LogOff   = 0,
129     LogIn    = 1,
130     LogOut   = 2,
131     LogInOut = 3
132   };
133 
134   /**
135    * Returns no of loggers affected
136    */
137   int log(LogMode logMode, const char * params);
138   int logOn(bool allBlocks, BlockNumber bno, LogMode logMode);
139   int logOff(bool allBlocks, BlockNumber bno, LogMode logMode);
140   int logToggle(bool allBlocks, BlockNumber bno, LogMode logMode);
141 
142   void setTrace(unsigned long trace);
143   unsigned long getTrace() const;
144 
145   void setOwnNodeId(int nodeId);
146   void setLogDistributed(bool val);
147 
148   /**
149    * Print header
150    */
151   static void printSignalHeader(FILE * output,
152 				const SignalHeader & sh,
153 				Uint8 prio,
154 				Uint32 node,
155 				bool printReceiversSignalId);
156 
157   /**
158    * Function for printing the Signal Data
159    */
160   static void printSignalData(FILE * out,
161 			      const SignalHeader & sh, const Uint32 *);
162 
163   /**
164    * Print linear section.
165    */
166   static void printLinearSection(FILE * output,
167                                  const SignalHeader & sh,
168                                  const LinearSectionPtr ptr[3],
169                                  unsigned i);
170 
171   /**
172    * Print segmented section.
173    */
174   static void printSegmentedSection(FILE * output,
175                                     const SignalHeader & sh,
176                                     const SegmentedSectionPtr ptr[3],
177                                     unsigned i);
178 
179   /**
180    * Print generic section.
181    */
182   static void printGenericSection(FILE * output,
183                                   const SignalHeader & sh,
184                                   const GenericSectionPtr ptr[3],
185                                   unsigned i);
186 
187   /**
188    * Print data word in hex.  Adds line break before the word
189    * when pos > 0 && pos % 7 == 0.  Increments pos.
190    */
191   static void printDataWord(FILE * output, Uint32 & pos, const Uint32 data);
192 
193 private:
194   bool m_logDistributed;
195   Uint32 m_ownNodeId;
196 
197   FILE * outputStream;
198   int log(int cmd, BlockNumber bno, LogMode logMode);
199 
200   Uint32        traceId;
201   Uint8         logModes[NO_OF_BLOCKS];
202 
203   NdbMutex* m_mutex;
204 
205 public:
lock()206   void lock() { if (m_mutex != 0) NdbMutex_Lock(m_mutex); }
unlock()207   void unlock() { if (m_mutex != 0) NdbMutex_Unlock(m_mutex); }
208 
209   inline bool
logMatch(BlockNumber bno,LogMode mask)210   logMatch(BlockNumber bno, LogMode mask)
211   {
212     // extract main block number
213     bno = blockToMain(bno);
214     // avoid addressing outside logModes
215     return
216       bno < MIN_BLOCK_NO || bno > MAX_BLOCK_NO ||
217       (logModes[bno-MIN_BLOCK_NO] & mask);
218   }
219 };
220 
221 #endif // SignalLoggerManager_H
222 
223