CUBRID Engine  latest
monitor_transaction.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 //
20 // monitor_transaction.cpp - implementation of transaction statistics management
21 //
22 
23 #include "monitor_transaction.hpp"
24 
25 #include "log_impl.h"
26 #include "system_parameter.h"
27 
28 #include <cassert>
29 
30 namespace cubmonitor
31 {
33  // transaction_sheet_manager
35 
36  // static members
38  unsigned transaction_sheet_manager::s_sheet_start_count[MAX_SHEETS] = { 0 };
42 
43  void
45  {
46  // note - should be protected by mutex
47 
49  {
50  // get transaction count
51  std::size_t tran_count = NUM_NORMAL_TRANS;
52 
53  // all transaction start with invalid sheets
54  s_transaction_sheets = new transaction_sheet[tran_count];
55 
56  for (std::size_t it = 0; it < tran_count; it++)
57  {
59  }
60  s_transaction_count = tran_count;
61  }
62  else
63  {
64  // already initialized
65  }
66  }
67 
68  bool
70  {
71  std::unique_lock<std::mutex> ulock (s_sheets_mutex);
72 
73  // make sure it is initialized
74  static_init ();
75 
76  // get current transaction
77  int transaction = logtb_get_current_tran_index ();
78 
79  if (transaction <= LOG_SYSTEM_TRAN_INDEX || transaction >= (int) s_transaction_count)
80  {
81  // invalid transaction
82  assert (false);
83  return false;
84  }
85 
86  std::size_t tran_array_index = std::size_t (transaction - 1);
87  if (s_transaction_sheets[tran_array_index] == INVALID_TRANSACTION_SHEET)
88  {
89  // transaction doesn't have a sheet assigned
90  // find unused sheet
92  {
93  // already maxed
94  return false;
95  }
96 
97  // iterate sheets. must have counter 0 if unused
98  for (std::size_t sheet_index = 0; sheet_index < MAX_SHEETS; sheet_index++)
99  {
100  if (s_sheet_start_count[sheet_index] == 0)
101  {
102  // found free sheet; assign to current transaction
103  s_transaction_sheets[tran_array_index] = sheet_index;
104  s_sheet_start_count[sheet_index] = 1;
105 
106  // increment used sheets count
108  return true;
109  }
110  }
111 
112  assert (false);
113  return false;
114  }
115  else
116  {
117  // already have a sheet; increment its count
118  assert (s_sheet_start_count[s_transaction_sheets[tran_array_index]] > 0);
119 
120  ++s_sheet_start_count[s_transaction_sheets[tran_array_index]];
121  return true;
122  }
123  }
124 
125  void
126  transaction_sheet_manager::end_watch (bool end_all /* = false */)
127  {
128  std::unique_lock<std::mutex> ulock (s_sheets_mutex);
129 
130  // make sure it is initialized
131  static_init ();
132 
133  // get current transaction
134  int transaction = logtb_get_current_tran_index ();
135 
136  if (transaction <= LOG_SYSTEM_TRAN_INDEX || transaction >= (int) s_transaction_count)
137  {
138  // invalid transaction
139  assert (false);
140  return;
141  }
142 
143  std::size_t index = transaction - 1;
145  {
146  // no sheet open... might have been cleared
147  return;
148  }
149 
151  if (s_sheet_start_count[sheet] == 0)
152  {
153  // this is an invalid state
154  assert (false);
155  return;
156  }
157 
158  if (end_all)
159  {
160  // end all
161  s_sheet_start_count[sheet] = 0;
162  }
163  else
164  {
165  // end once
166  --s_sheet_start_count[sheet];
167  }
168 
169  if (s_sheet_start_count[sheet] == 0)
170  {
171  // sheet is now free
174  }
175  }
176 
179  {
180  if (s_current_sheet_count == 0)
181  {
182  // no sheets; early out
184  }
185 
187 
188  int transaction = logtb_get_current_tran_index ();
189 
190  if (transaction <= LOG_SYSTEM_TRAN_INDEX || transaction >= (int) s_transaction_count)
191  {
192  // invalid transaction; may be a daemon or vacuum worker
194  }
195 
196  // return transaction's sheets if open or invalid sheet
197  return s_transaction_sheets[transaction - 1];
198  }
199 
200 } // namespace cubmonitor
#define NUM_NORMAL_TRANS
static const transaction_sheet INVALID_TRANSACTION_SHEET
static API_MUTEX mutex
Definition: api_util.c:72
static transaction_sheet get_sheet(void)
static void end_watch(bool end_all=false)
#define assert(x)
static unsigned s_sheet_start_count[MAX_SHEETS]
#define NULL
Definition: freelistheap.h:34
int logtb_get_current_tran_index(void)
static transaction_sheet * s_transaction_sheets
std::size_t transaction_sheet