hmain_test.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "hv.h"
  2. #include "hmain.h"
  3. #include "iniparser.h"
  4. typedef struct conf_ctx_s {
  5. IniParser* parser;
  6. int loglevel;
  7. int worker_processes;
  8. int worker_threads;
  9. int port;
  10. } conf_ctx_t;
  11. conf_ctx_t g_conf_ctx;
  12. inline void conf_ctx_init(conf_ctx_t* ctx) {
  13. ctx->parser = new IniParser;
  14. ctx->loglevel = LOG_LEVEL_DEBUG;
  15. ctx->worker_processes = 0;
  16. ctx->worker_threads = 0;
  17. ctx->port = 0;
  18. }
  19. static void print_version();
  20. static void print_help();
  21. static int parse_confile(const char* confile);
  22. static void worker_fn(void* userdata);
  23. // short options
  24. static const char options[] = "hvc:ts:dp:";
  25. // long options
  26. static const option_t long_options[] = {
  27. {'h', "help", NO_ARGUMENT},
  28. {'v', "version", NO_ARGUMENT},
  29. {'c', "confile", REQUIRED_ARGUMENT},
  30. {'t', "test", NO_ARGUMENT},
  31. {'s', "signal", REQUIRED_ARGUMENT},
  32. {'d', "daemon", NO_ARGUMENT},
  33. {'p', "port", REQUIRED_ARGUMENT}
  34. };
  35. static const char detail_options[] = R"(
  36. -h|--help Print this information
  37. -v|--version Print version
  38. -c|--confile <confile> Set configure file, default etc/{program}.conf
  39. -t|--test Test Configure file and exit
  40. -s|--signal <signal> Send <signal> to process,
  41. <signal>=[start,stop,restart,status,reload]
  42. -d|--daemon Daemonize
  43. -p|--port <port> 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. printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
  50. printf("Options:\n%s\n", detail_options);
  51. }
  52. int parse_confile(const char* confile) {
  53. int ret = g_conf_ctx.parser->LoadFromFile(confile);
  54. if (ret != 0) {
  55. printf("Load confile [%s] failed: %d\n", confile, ret);
  56. exit(-40);
  57. }
  58. // logfile
  59. string str = g_conf_ctx.parser->GetValue("logfile");
  60. if (!str.empty()) {
  61. strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile));
  62. }
  63. hlog_set_file(g_main_ctx.logfile);
  64. // loglevel
  65. const char* szLoglevel = g_conf_ctx.parser->GetValue("loglevel").c_str();
  66. int loglevel = LOG_LEVEL_INFO;
  67. if (stricmp(szLoglevel, "VERBOSE") == 0) {
  68. loglevel = LOG_LEVEL_VERBOSE;
  69. } else if (stricmp(szLoglevel, "DEBUG") == 0) {
  70. loglevel = LOG_LEVEL_DEBUG;
  71. } else if (stricmp(szLoglevel, "INFO") == 0) {
  72. loglevel = LOG_LEVEL_INFO;
  73. } else if (stricmp(szLoglevel, "WARN") == 0) {
  74. loglevel = LOG_LEVEL_WARN;
  75. } else if (stricmp(szLoglevel, "ERROR") == 0) {
  76. loglevel = LOG_LEVEL_ERROR;
  77. } else if (stricmp(szLoglevel, "FATAL") == 0) {
  78. loglevel = LOG_LEVEL_FATAL;
  79. } else if (stricmp(szLoglevel, "SILENT") == 0) {
  80. loglevel = LOG_LEVEL_SILENT;
  81. } else {
  82. loglevel = LOG_LEVEL_INFO;
  83. }
  84. g_conf_ctx.loglevel = loglevel;
  85. hlog_set_level(loglevel);
  86. // log_filesize
  87. str = g_conf_ctx.parser->GetValue("log_filesize");
  88. if (!str.empty()) {
  89. int num = atoi(str.c_str());
  90. if (num > 0) {
  91. // 16 16M 16MB
  92. const char* p = str.c_str() + str.size() - 1;
  93. char unit;
  94. if (*p >= '0' && *p <= '9') unit = 'M';
  95. else if (*p == 'B') unit = *(p-1);
  96. else unit = *p;
  97. unsigned long long filesize = num;
  98. switch (unit) {
  99. case 'K': filesize <<= 10; break;
  100. case 'M': filesize <<= 20; break;
  101. case 'G': filesize <<= 30; break;
  102. default: filesize <<= 20; break;
  103. }
  104. hlog_set_max_filesize(filesize);
  105. }
  106. }
  107. // log_remain_days
  108. str = g_conf_ctx.parser->GetValue("log_remain_days");
  109. if (!str.empty()) {
  110. hlog_set_remain_days(atoi(str.c_str()));
  111. }
  112. // log_fsync
  113. str = g_conf_ctx.parser->GetValue("log_fsync");
  114. if (!str.empty()) {
  115. logger_enable_fsync(hlog, getboolean(str.c_str()));
  116. }
  117. // first log here
  118. hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version());
  119. hlog_fsync();
  120. // worker_processes
  121. int worker_processes = 0;
  122. str = g_conf_ctx.parser->GetValue("worker_processes");
  123. if (str.size() != 0) {
  124. if (strcmp(str.c_str(), "auto") == 0) {
  125. worker_processes = get_ncpu();
  126. hlogd("worker_processes=ncpu=%d", worker_processes);
  127. }
  128. else {
  129. worker_processes = atoi(str.c_str());
  130. }
  131. }
  132. g_conf_ctx.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  133. // worker_threads
  134. int worker_threads = g_conf_ctx.parser->Get<int>("worker_threads");
  135. g_conf_ctx.worker_threads = LIMIT(0, worker_threads, 16);
  136. // port
  137. int port = 0;
  138. const char* szPort = get_arg("p");
  139. if (szPort) {
  140. port = atoi(szPort);
  141. }
  142. if (port == 0) {
  143. port = g_conf_ctx.parser->Get<int>("port");
  144. }
  145. if (port == 0) {
  146. printf("Please config listen port!\n");
  147. exit(-10);
  148. }
  149. g_conf_ctx.port = port;
  150. hlogi("parse_confile('%s') OK", confile);
  151. return 0;
  152. }
  153. void master_init(void* userdata) {
  154. #ifdef OS_UNIX
  155. char proctitle[256] = {0};
  156. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  157. setproctitle(proctitle);
  158. signal(SIGNAL_RELOAD, signal_handler);
  159. #endif
  160. }
  161. void worker_init(void* userdata) {
  162. #ifdef OS_UNIX
  163. char proctitle[256] = {0};
  164. snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);
  165. setproctitle(proctitle);
  166. signal(SIGNAL_RELOAD, signal_handler);
  167. #endif
  168. }
  169. static void on_reload(void* userdata) {
  170. hlogi("reload confile [%s]", g_main_ctx.confile);
  171. parse_confile(g_main_ctx.confile);
  172. }
  173. int main(int argc, char** argv) {
  174. // g_main_ctx
  175. main_ctx_init(argc, argv);
  176. if (argc == 1) {
  177. print_help();
  178. exit(10);
  179. }
  180. // int ret = parse_opt(argc, argv, options);
  181. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  182. if (ret != 0) {
  183. print_help();
  184. exit(ret);
  185. }
  186. printf("---------------arg------------------------------\n");
  187. printf("%s\n", g_main_ctx.cmdline);
  188. for (auto& pair : g_main_ctx.arg_kv) {
  189. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  190. }
  191. for (auto& item : g_main_ctx.arg_list) {
  192. printf("%s\n", item.c_str());
  193. }
  194. printf("================================================\n");
  195. printf("---------------env------------------------------\n");
  196. for (auto& pair : g_main_ctx.env_kv) {
  197. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  198. }
  199. printf("================================================\n");
  200. // help
  201. if (get_arg("h")) {
  202. print_help();
  203. exit(0);
  204. }
  205. // version
  206. if (get_arg("v")) {
  207. print_version();
  208. exit(0);
  209. }
  210. // g_conf_ctx
  211. conf_ctx_init(&g_conf_ctx);
  212. const char* confile = get_arg("c");
  213. if (confile) {
  214. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  215. }
  216. parse_confile(g_main_ctx.confile);
  217. // test
  218. if (get_arg("t")) {
  219. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  220. exit(0);
  221. }
  222. // signal
  223. signal_init(on_reload);
  224. const char* signal = get_arg("s");
  225. if (signal) {
  226. signal_handle(signal);
  227. }
  228. #ifdef OS_UNIX
  229. // daemon
  230. if (get_arg("d")) {
  231. // nochdir, noclose
  232. int ret = daemon(1, 1);
  233. if (ret != 0) {
  234. printf("daemon error: %d\n", ret);
  235. exit(-10);
  236. }
  237. // parent process exit after daemon, so pid changed.
  238. g_main_ctx.pid = getpid();
  239. }
  240. #endif
  241. // pidfile
  242. create_pidfile();
  243. master_workers_run(worker_fn, (void*)(intptr_t)100L, g_conf_ctx.worker_processes, g_conf_ctx.worker_threads);
  244. return 0;
  245. }
  246. void worker_fn(void* userdata) {
  247. long num = (long)(intptr_t)(userdata);
  248. while (1) {
  249. printf("num=%ld pid=%ld tid=%ld\n", num, hv_getpid(), hv_gettid());
  250. hv_delay(10000);
  251. }
  252. }