plugin_worker.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <sys/mman.h>
  21. #include <sched.h>
  22. #include "plugin_agent_mgr.h"
  23. #include "plugin_worker.h"
  24. #include "plugin_global.h"
  25. #include "mem_check.h"
  26. #include "../log/log.h"
  27. PluginWorkerThread::PluginWorkerThread(const char *name,
  28. worker_notify_t *worker_notify,
  29. int seq_no)
  30. : Thread(name, Thread::ThreadTypeSync), _worker_notify(worker_notify),
  31. _seq_no(seq_no)
  32. {
  33. }
  34. PluginWorkerThread::~PluginWorkerThread()
  35. {
  36. }
  37. int PluginWorkerThread::initialize(void)
  38. {
  39. _dll = PluginAgentManager::instance()->get_dll();
  40. if ((NULL == _dll) || (NULL == _dll->handle_init) ||
  41. (NULL == _dll->handle_process)) {
  42. log4cplus_error("get server bench handle failed.");
  43. return -1;
  44. }
  45. return 0;
  46. }
  47. void PluginWorkerThread::Prepare(void)
  48. {
  49. const char *plugin_config_file[2] = {
  50. (const char *)PluginGlobal::_plugin_conf_file, NULL
  51. };
  52. if (_dll->handle_init(1, (char **)plugin_config_file,
  53. _seq_no + PROC_WORK) != 0) {
  54. log4cplus_error("invoke handle_init() failed, worker[%d]",
  55. _seq_no);
  56. return;
  57. }
  58. return;
  59. }
  60. void *PluginWorkerThread::do_process(void)
  61. {
  62. plugin_request_t *plugin_request = NULL;
  63. while (!stopping()) {
  64. plugin_request = _worker_notify->Pop();
  65. if (PLUGIN_STOP_REQUEST == plugin_request) {
  66. break;
  67. }
  68. if (NULL == plugin_request) {
  69. log4cplus_error(
  70. "the worker_notify::Pop() invalid, plugin_request=%p, worker[%d]",
  71. plugin_request, _gettid_());
  72. continue;
  73. }
  74. if (stopping()) {
  75. break;
  76. }
  77. plugin_request->handle_process();
  78. }
  79. if (NULL != _dll->handle_fini) {
  80. _dll->handle_fini(_seq_no + PROC_WORK);
  81. }
  82. return NULL;
  83. }