hmain_test.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "hv.h"
  2. #include "hmain.h"
  3. #include "iniparser.h"
  4. /*
  5. * @build: make examples
  6. * @usage: bin/hmain_test -h
  7. * bin/hmain_test -v
  8. *
  9. * bin/hmain_test -c etc/hmain_test.conf -d
  10. * ps aux | grep hmain_test
  11. *
  12. * bin/hmain_test -s stop
  13. * ps aux | grep hmain_test
  14. *
  15. */
  16. typedef struct conf_ctx_s {
  17. IniParser* parser;
  18. int loglevel;
  19. int worker_processes;
  20. int worker_threads;
  21. int port;
  22. } conf_ctx_t;
  23. conf_ctx_t g_conf_ctx;
  24. inline void conf_ctx_init(conf_ctx_t* ctx) {
  25. ctx->parser = new IniParser;
  26. ctx->loglevel = LOG_LEVEL_DEBUG;
  27. ctx->worker_processes = 0;
  28. ctx->worker_threads = 0;
  29. ctx->port = 0;
  30. }
  31. static void print_version();
  32. static void print_help();
  33. static int parse_confile(const char* confile);
  34. static void worker_fn(void* userdata);
  35. // long options
  36. static const option_t long_options[] = {
  37. {'h', "help", NO_ARGUMENT, "Print this information"},
  38. {'v', "version", NO_ARGUMENT, "Print version"},
  39. {'c', "confile", REQUIRED_ARGUMENT, "Set configure file, default etc/{program}.conf"},
  40. {'t', "test", NO_ARGUMENT, "Test configure file and exit"},
  41. {'s', "signal", REQUIRED_ARGUMENT, "send signal to process, signal=[start,stop,restart,status,reload]"},
  42. {'d', "daemon", NO_ARGUMENT, "Daemonize"},
  43. {'p', "port", REQUIRED_ARGUMENT, "Set listen port"}
  44. };
  45. void print_version() {
  46. printf("%s version %s\n", g_main_ctx.program_name, hv_compile_version());
  47. }
  48. void print_help() {
  49. char detail_options[1024] = {0};
  50. dump_opt_long(long_options, ARRAY_SIZE(long_options), detail_options, sizeof(detail_options));
  51. printf("%s\n", detail_options);
  52. }
  53. int parse_confile(const char* confile) {
  54. int ret = g_conf_ctx.parser->LoadFromFile(confile);
  55. if (ret != 0) {
  56. printf("Load confile [%s] failed: %d\n", confile, ret);
  57. exit(-40);
  58. }
  59. // logfile
  60. std::string str = g_conf_ctx.parser->GetValue("logfile");
  61. if (!str.empty()) {
  62. strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile));
  63. }
  64. hlog_set_file(g_main_ctx.logfile);
  65. // loglevel
  66. str = g_conf_ctx.parser->GetValue("loglevel");
  67. if (!str.empty()) {
  68. hlog_set_level_by_str(str.c_str());
  69. }
  70. // log_filesize
  71. str = g_conf_ctx.parser->GetValue("log_filesize");
  72. if (!str.empty()) {
  73. hlog_set_max_filesize_by_str(str.c_str());
  74. }
  75. // log_remain_days
  76. str = g_conf_ctx.parser->GetValue("log_remain_days");
  77. if (!str.empty()) {
  78. hlog_set_remain_days(atoi(str.c_str()));
  79. }
  80. // log_fsync
  81. str = g_conf_ctx.parser->GetValue("log_fsync");
  82. if (!str.empty()) {
  83. logger_enable_fsync(hlog, hv_getboolean(str.c_str()));
  84. }
  85. // first log here
  86. hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version());
  87. hlog_fsync();
  88. // worker_processes
  89. int worker_processes = 0;
  90. #ifdef DEBUG
  91. // Disable multi-processes mode for debugging
  92. worker_processes = 0;
  93. #else
  94. str = g_conf_ctx.parser->GetValue("worker_processes");
  95. if (str.size() != 0) {
  96. if (strcmp(str.c_str(), "auto") == 0) {
  97. worker_processes = get_ncpu();
  98. hlogd("worker_processes=ncpu=%d", worker_processes);
  99. }
  100. else {
  101. worker_processes = atoi(str.c_str());
  102. }
  103. }
  104. #endif
  105. g_conf_ctx.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  106. // worker_threads
  107. int worker_threads = 0;
  108. str = g_conf_ctx.parser->GetValue("worker_threads");
  109. if (str.size() != 0) {
  110. if (strcmp(str.c_str(), "auto") == 0) {
  111. worker_threads = get_ncpu();
  112. hlogd("worker_threads=ncpu=%d", worker_threads);
  113. }
  114. else {
  115. worker_threads = atoi(str.c_str());
  116. }
  117. }
  118. g_conf_ctx.worker_threads = LIMIT(0, worker_threads, 64);
  119. // port
  120. int port = 0;
  121. const char* szPort = get_arg("p");
  122. if (szPort) {
  123. port = atoi(szPort);
  124. }
  125. if (port == 0) {
  126. port = g_conf_ctx.parser->Get<int>("port");
  127. }
  128. if (port == 0) {
  129. printf("Please config listen port!\n");
  130. exit(-10);
  131. }
  132. g_conf_ctx.port = port;
  133. hlogi("parse_confile('%s') OK", confile);
  134. return 0;
  135. }
  136. static void on_reload(void* userdata) {
  137. hlogi("reload confile [%s]", g_main_ctx.confile);
  138. parse_confile(g_main_ctx.confile);
  139. }
  140. int main(int argc, char** argv) {
  141. // g_main_ctx
  142. main_ctx_init(argc, argv);
  143. if (argc == 1) {
  144. print_help();
  145. exit(10);
  146. }
  147. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  148. if (ret != 0) {
  149. print_help();
  150. exit(ret);
  151. }
  152. /*
  153. printf("---------------arg------------------------------\n");
  154. printf("%s\n", g_main_ctx.cmdline);
  155. for (int i = 0; i < g_main_ctx.arg_kv_size; ++i) {
  156. printf("%s\n", g_main_ctx.arg_kv[i]);
  157. }
  158. for (int i = 0; i < g_main_ctx.arg_list_size; ++i) {
  159. printf("%s\n", g_main_ctx.arg_list[i]);
  160. }
  161. printf("================================================\n");
  162. printf("---------------env------------------------------\n");
  163. for (int i = 0; i < g_main_ctx.envc; ++i) {
  164. printf("%s\n", g_main_ctx.save_envp[i]);
  165. }
  166. printf("================================================\n");
  167. */
  168. // help
  169. if (get_arg("h")) {
  170. print_help();
  171. exit(0);
  172. }
  173. // version
  174. if (get_arg("v")) {
  175. print_version();
  176. exit(0);
  177. }
  178. // g_conf_ctx
  179. conf_ctx_init(&g_conf_ctx);
  180. const char* confile = get_arg("c");
  181. if (confile) {
  182. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  183. }
  184. parse_confile(g_main_ctx.confile);
  185. // test
  186. if (get_arg("t")) {
  187. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  188. exit(0);
  189. }
  190. // signal
  191. signal_init(on_reload);
  192. const char* signal = get_arg("s");
  193. if (signal) {
  194. signal_handle(signal);
  195. }
  196. #ifdef OS_UNIX
  197. // daemon
  198. if (get_arg("d")) {
  199. // nochdir, noclose
  200. int ret = daemon(1, 1);
  201. if (ret != 0) {
  202. printf("daemon error: %d\n", ret);
  203. exit(-10);
  204. }
  205. }
  206. #endif
  207. // pidfile
  208. create_pidfile();
  209. master_workers_run(worker_fn, (void*)(intptr_t)g_conf_ctx.port, g_conf_ctx.worker_processes, g_conf_ctx.worker_threads);
  210. return 0;
  211. }
  212. void worker_fn(void* userdata) {
  213. long port = (long)(intptr_t)(userdata);
  214. while (1) {
  215. printf("port=%ld pid=%ld tid=%ld\n", port, hv_getpid(), hv_gettid());
  216. hv_delay(60000);
  217. }
  218. }