CUBRID Engine  latest
thread_worker_pool.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  * thread_worker_pool.cpp
21  */
22 
23 #include "thread_worker_pool.hpp"
24 
25 #include "error_manager.h"
26 #include "perf.hpp"
27 
28 #include <sstream>
29 
30 #include <cstring>
31 
32 namespace cubthread
33 {
34  static bool FORCE_THREAD_ALWAYS_ALIVE = false;
35 
36  bool
38  {
40  }
41 
42  void
44  {
45  FORCE_THREAD_ALWAYS_ALIVE = true;
46  }
47 
49  // statistics
51 
53  {
55  "Counter_start_thread", "Timer_start_thread"),
57  "Counter_create_context", "Timer_create_context"),
59  "Counter_execute_task", "Timer_execute_task"),
61  "Counter_retire_task", "Timer_retire_task"),
63  "Counter_found_task_in_queue", "Timer_found_task_in_queue"),
65  "Counter_wakeup_with_task", "Timer_wakeup_with_task"),
67  "Counter_recycle_context", "Timer_recycle_context"),
69  "Counter_retire_context", "Timer_retire_context")
70  };
71 
74  {
75  return *Worker_pool_statdef.create_statset ();
76  }
77 
78  void
80  {
81  delete &stats;
82  }
83 
84  void
86  {
87  Worker_pool_statdef.time_and_increment (stats, id);
88  }
89 
90  void
92  {
93  Worker_pool_statdef.add_stat_values_with_converted_timers<std::chrono::microseconds> (what, where);
94  }
95 
96  std::size_t
98  {
99  return Worker_pool_statdef.get_value_count ();
100  }
101 
102  const char *
103  wp_worker_statset_get_name (std::size_t stat_index)
104  {
105  return Worker_pool_statdef.get_value_name (stat_index);
106  }
107 
109  // functions
111 
112  std::size_t
114  {
115  std::size_t count = std::thread::hardware_concurrency ();
116 
117  if (count == 0)
118  {
119  count = 1;
120  }
121 
122  return count;
123  }
124 
125  void
126  wp_handle_system_error (const char *message, const std::system_error &e)
127  {
128  er_print_callstack (ARG_FILE_LINE, "%s - throws err = %d: %s\n", message, e.code (), e.what ());
129  assert (false);
130  throw e;
131  }
132 
133  void
134  wp_er_log_stats (const char *header, cubperf::stat_value *statsp)
135  {
136  std::stringstream ss;
137 
138  ss << "Worker pool statistics: " << header << std::endl;
139 
140  for (std::size_t index = 0; index < Worker_pool_statdef.get_value_count (); index++)
141  {
142  ss << "\t" << Worker_pool_statdef.get_value_name (index) << ": ";
143  ss << statsp[index] << std::endl;
144  }
145 
146  std::string str = ss.str ();
147  _er_log_debug (ARG_FILE_LINE, str.c_str ());
148  }
149 
150 } // namespace cubthread
static const cubperf::stat_id Wpstat_recycle_context
static const cubperf::stat_id Wpstat_start_thread
std::size_t wp_worker_statset_get_count(void)
void wp_er_log_stats(const char *header, cubperf::stat_value *statsp)
void time_and_increment(statset &statsetr, stat_id id, duration d, stat_value incr=1) const
Definition: perf.hpp:340
void wp_worker_statset_destroy(cubperf::statset &stats)
generic_value< false > stat_value
Definition: perf_def.hpp:46
void wp_set_force_thread_always_alive()
std::size_t stat_id
Definition: perf_def.hpp:50
void wp_worker_statset_time_and_increment(cubperf::statset &stats, cubperf::stat_id id)
void _er_log_debug(const char *file_name, const int line_no, const char *fmt,...)
static const cubperf::stat_id Wpstat_create_context
static const cubperf::stat_id Wpstat_wakeup_with_task
static const cubperf::stat_id Wpstat_found_in_queue
std::size_t get_value_count() const
Definition: perf.cpp:149
#define assert(x)
statset * create_statset(void) const
Definition: perf.cpp:131
static const cubperf::stat_id Wpstat_retire_context
void add_stat_values_with_converted_timers(const statset &statsetr, stat_value *output_stats) const
Definition: perf.hpp:446
static const cubperf::stat_id Wpstat_retire_task
static bool FORCE_THREAD_ALWAYS_ALIVE
static const cubperf::statset_definition Worker_pool_statdef
const char * wp_worker_statset_get_name(std::size_t stat_index)
bool wp_is_thread_always_alive_forced()
std::size_t system_core_count(void)
int count(int &result, const cub_regex_object &reg, const std::string &src, const int position, const INTL_CODESET codeset)
#define ARG_FILE_LINE
Definition: error_manager.h:44
void er_print_callstack(const char *file_name, const int line_no, const char *fmt,...)
void wp_handle_system_error(const char *message, const std::system_error &e)
const char * get_value_name(std::size_t value_index) const
Definition: perf.cpp:155
cubperf::statset & wp_worker_statset_create(void)
static const cubperf::stat_id Wpstat_execute_task
void wp_worker_statset_accumulate(const cubperf::statset &what, cubperf::stat_value *where)