You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_thread_pool.cxx 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*-
  2. * Copyright 2018 Mikhail Galanin
  3. * Copyright 2019 Vsevolod Stakhov
  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. #include "config.h"
  18. #include "lua_common.h"
  19. #include "lua_thread_pool.h"
  20. #include <vector>
  21. #define msg_debug_lua_threads(...) rspamd_conditional_debug_fast(NULL, NULL, \
  22. rspamd_lua_threads_log_id, "lua_threads", NULL, \
  23. RSPAMD_LOG_FUNC, \
  24. __VA_ARGS__)
  25. INIT_LOG_MODULE(lua_threads)
  26. static struct thread_entry *thread_entry_new(lua_State *L);
  27. static void thread_entry_free(lua_State *L, struct thread_entry *ent);
  28. #define CFG_POOL_GET(cfg) (reinterpret_cast<lua_thread_pool *>((cfg)->lua_thread_pool))
  29. struct lua_thread_pool {
  30. std::vector<struct thread_entry *> available_items;
  31. lua_State *L;
  32. int max_items;
  33. struct thread_entry *running_entry;
  34. static const int default_max_items = 100;
  35. lua_thread_pool(lua_State *L, int max_items = default_max_items)
  36. : L(L), max_items(max_items)
  37. {
  38. running_entry = nullptr;
  39. available_items.reserve(max_items);
  40. for (auto i = 0; i < MAX(2, max_items / 10); i++) {
  41. auto *ent = thread_entry_new(L);
  42. available_items.push_back(ent);
  43. }
  44. }
  45. ~lua_thread_pool()
  46. {
  47. for (auto *ent: available_items) {
  48. thread_entry_free(L, ent);
  49. }
  50. }
  51. auto get_thread() -> struct thread_entry *
  52. {
  53. struct thread_entry *ent;
  54. if (!available_items.empty()) {
  55. ent = available_items.back();
  56. available_items.pop_back();
  57. }
  58. else {
  59. ent = thread_entry_new(L);
  60. }
  61. running_entry = ent;
  62. return ent;
  63. }
  64. auto return_thread(struct thread_entry *thread_entry, const char *loc) -> void
  65. {
  66. /* we can't return a running/yielded thread into the pool */
  67. g_assert(lua_status(thread_entry->lua_state) == 0);
  68. if (running_entry == thread_entry) {
  69. running_entry = NULL;
  70. }
  71. if (available_items.size() <= max_items) {
  72. thread_entry->cd = NULL;
  73. thread_entry->finish_callback = NULL;
  74. thread_entry->error_callback = NULL;
  75. thread_entry->task = NULL;
  76. thread_entry->cfg = NULL;
  77. msg_debug_lua_threads("%s: returned thread to the threads pool %ud items",
  78. loc,
  79. available_items.size());
  80. available_items.push_back(thread_entry);
  81. }
  82. else {
  83. msg_debug_lua_threads("%s: removed thread as thread pool has %ud items",
  84. loc,
  85. available_items.size());
  86. thread_entry_free(L, thread_entry);
  87. }
  88. }
  89. auto terminate_thread(struct thread_entry *thread_entry,
  90. const char *loc,
  91. bool enforce) -> void
  92. {
  93. struct thread_entry *ent = NULL;
  94. if (!enforce) {
  95. /* we should only terminate failed threads */
  96. g_assert(lua_status(thread_entry->lua_state) != 0 &&
  97. lua_status(thread_entry->lua_state) != LUA_YIELD);
  98. }
  99. if (running_entry == thread_entry) {
  100. running_entry = NULL;
  101. }
  102. msg_debug_lua_threads("%s: terminated thread entry", loc);
  103. thread_entry_free(L, thread_entry);
  104. if (available_items.size() <= max_items) {
  105. ent = thread_entry_new(L);
  106. available_items.push_back(ent);
  107. }
  108. }
  109. auto get_running_entry(void) -> struct thread_entry *
  110. {
  111. return running_entry;
  112. };
  113. auto set_running_entry(struct thread_entry *ent) -> struct thread_entry *
  114. {
  115. auto *old_entry = running_entry;
  116. running_entry = ent;
  117. return old_entry;
  118. };
  119. };
  120. static struct thread_entry *
  121. thread_entry_new(lua_State *L)
  122. {
  123. struct thread_entry *ent;
  124. ent = g_new0(struct thread_entry, 1);
  125. ent->lua_state = lua_newthread(L);
  126. ent->thread_index = luaL_ref(L, LUA_REGISTRYINDEX);
  127. return ent;
  128. }
  129. static void
  130. thread_entry_free(lua_State *L, struct thread_entry *ent)
  131. {
  132. luaL_unref(L, LUA_REGISTRYINDEX, ent->thread_index);
  133. g_free(ent);
  134. }
  135. struct lua_thread_pool *
  136. lua_thread_pool_new(lua_State *L)
  137. {
  138. auto *pool = new lua_thread_pool(L);
  139. return pool;
  140. }
  141. void lua_thread_pool_free(struct lua_thread_pool *pool)
  142. {
  143. delete pool;
  144. }
  145. struct thread_entry *
  146. lua_thread_pool_get_for_task(struct rspamd_task *task)
  147. {
  148. struct thread_entry *ent = CFG_POOL_GET(task->cfg)->get_thread();
  149. ent->task = task;
  150. return ent;
  151. }
  152. struct thread_entry *
  153. lua_thread_pool_get_for_config(struct rspamd_config *cfg)
  154. {
  155. struct thread_entry *ent = CFG_POOL_GET(cfg)->get_thread();
  156. ent->cfg = cfg;
  157. return ent;
  158. }
  159. void lua_thread_pool_return_full(struct lua_thread_pool *pool,
  160. struct thread_entry *thread_entry, const char *loc)
  161. {
  162. pool->return_thread(thread_entry, loc);
  163. }
  164. void lua_thread_pool_terminate_entry_full(struct lua_thread_pool *pool,
  165. struct thread_entry *thread_entry, const char *loc,
  166. bool enforce)
  167. {
  168. pool->terminate_thread(thread_entry, loc, enforce);
  169. }
  170. struct thread_entry *
  171. lua_thread_pool_get_running_entry_full(struct lua_thread_pool *pool,
  172. const char *loc)
  173. {
  174. msg_debug_lua_threads("%s: lua_thread_pool_get_running_entry_full", loc);
  175. return pool->get_running_entry();
  176. }
  177. void lua_thread_pool_set_running_entry_full(struct lua_thread_pool *pool,
  178. struct thread_entry *thread_entry,
  179. const char *loc)
  180. {
  181. msg_debug_lua_threads("%s: lua_thread_pool_set_running_entry_full", loc);
  182. pool->set_running_entry(thread_entry);
  183. }
  184. static void
  185. lua_thread_pool_set_running_entry_for_thread(struct thread_entry *thread_entry,
  186. const char *loc)
  187. {
  188. struct lua_thread_pool *pool;
  189. if (thread_entry->task) {
  190. pool = CFG_POOL_GET(thread_entry->task->cfg);
  191. }
  192. else {
  193. pool = CFG_POOL_GET(thread_entry->cfg);
  194. }
  195. lua_thread_pool_set_running_entry_full(pool, thread_entry, loc);
  196. }
  197. void lua_thread_pool_prepare_callback_full(struct lua_thread_pool *pool,
  198. struct lua_callback_state *cbs,
  199. const char *loc)
  200. {
  201. msg_debug_lua_threads("%s: lua_thread_pool_prepare_callback_full", loc);
  202. cbs->thread_pool = pool;
  203. cbs->previous_thread = lua_thread_pool_get_running_entry_full(pool, loc);
  204. cbs->my_thread = pool->get_thread();
  205. cbs->L = cbs->my_thread->lua_state;
  206. }
  207. void lua_thread_pool_restore_callback_full(struct lua_callback_state *cbs,
  208. const char *loc)
  209. {
  210. lua_thread_pool_return_full(cbs->thread_pool, cbs->my_thread, loc);
  211. lua_thread_pool_set_running_entry_full(cbs->thread_pool,
  212. cbs->previous_thread, loc);
  213. }
  214. static int
  215. lua_do_resume_full(lua_State *L, int narg, const char *loc)
  216. {
  217. #if LUA_VERSION_NUM >= 504
  218. int nres;
  219. #endif
  220. msg_debug_lua_threads("%s: lua_do_resume_full", loc);
  221. #if LUA_VERSION_NUM < 502
  222. return lua_resume(L, narg);
  223. #else
  224. #if LUA_VERSION_NUM >= 504
  225. return lua_resume(L, NULL, narg, &nres);
  226. #else
  227. return lua_resume(L, NULL, narg);
  228. #endif
  229. #endif
  230. }
  231. static void
  232. lua_resume_thread_internal_full(struct thread_entry *thread_entry,
  233. int narg, const char *loc)
  234. {
  235. int ret;
  236. struct lua_thread_pool *pool;
  237. struct rspamd_task *task;
  238. msg_debug_lua_threads("%s: lua_resume_thread_internal_full", loc);
  239. ret = lua_do_resume_full(thread_entry->lua_state, narg, loc);
  240. if (ret != LUA_YIELD) {
  241. /*
  242. LUA_YIELD state should not be handled here.
  243. It should only happen when the thread initiated a asynchronous event and it will be restored as soon
  244. the event is finished
  245. */
  246. if (thread_entry->task) {
  247. pool = CFG_POOL_GET(thread_entry->task->cfg);
  248. }
  249. else {
  250. pool = CFG_POOL_GET(thread_entry->cfg);
  251. }
  252. if (ret == 0) {
  253. if (thread_entry->finish_callback) {
  254. thread_entry->finish_callback(thread_entry, ret);
  255. }
  256. pool->return_thread(thread_entry, loc);
  257. }
  258. else {
  259. rspamd_lua_traceback(thread_entry->lua_state);
  260. if (thread_entry->error_callback) {
  261. thread_entry->error_callback(thread_entry, ret,
  262. lua_tostring(thread_entry->lua_state, -1));
  263. }
  264. else if (thread_entry->task) {
  265. task = thread_entry->task;
  266. msg_err_task("lua call failed (%d): %s", ret,
  267. lua_tostring(thread_entry->lua_state, -1));
  268. }
  269. else {
  270. msg_err("lua call failed (%d): %s", ret,
  271. lua_tostring(thread_entry->lua_state, -1));
  272. }
  273. /*
  274. * Maybe there is a way to recover here.
  275. * For now, just remove faulty thread
  276. */
  277. pool->terminate_thread(thread_entry, loc, false);
  278. }
  279. }
  280. }
  281. void lua_thread_resume_full(struct thread_entry *thread_entry, int narg,
  282. const char *loc)
  283. {
  284. /*
  285. * The only state where we can resume from is LUA_YIELD
  286. * Another acceptable status is OK (0) but in that case we should push function on stack
  287. * to start the thread from, which is happening in lua_thread_call(), not in this function.
  288. */
  289. g_assert(lua_status(thread_entry->lua_state) == LUA_YIELD);
  290. msg_debug_lua_threads("%s: lua_thread_resume_full", loc);
  291. lua_thread_pool_set_running_entry_for_thread(thread_entry, loc);
  292. lua_resume_thread_internal_full(thread_entry, narg, loc);
  293. }
  294. void lua_thread_call_full(struct thread_entry *thread_entry,
  295. int narg, const char *loc)
  296. {
  297. g_assert(lua_status(thread_entry->lua_state) == 0); /* we can't call running/yielded thread */
  298. g_assert(thread_entry->task != NULL || thread_entry->cfg != NULL); /* we can't call without pool */
  299. lua_resume_thread_internal_full(thread_entry, narg, loc);
  300. }
  301. int lua_thread_yield_full(struct thread_entry *thread_entry,
  302. int nresults,
  303. const char *loc)
  304. {
  305. g_assert(lua_status(thread_entry->lua_state) == 0);
  306. msg_debug_lua_threads("%s: lua_thread_yield_full", loc);
  307. return lua_yield(thread_entry->lua_state, nresults);
  308. }