diff --git a/src/dnode/src/dnodeSystem.c b/src/dnode/src/dnodeSystem.c index 6c28552e1eb54cc2808720c8379838bcf0166257..a16ff826bf498f4fc56838a36f2ba0539b7ac5e1 100644 --- a/src/dnode/src/dnodeSystem.c +++ b/src/dnode/src/dnodeSystem.c @@ -19,43 +19,9 @@ #include "tconfig.h" #include "dnodeMain.h" +static void signal_handler(int32_t signum, siginfo_t *sigInfo, void *context); static tsem_t exitSem; -#ifdef WINDOWS -static void signal_handler(int32_t signum) { - dInfo("shut down signal is %d", signum); -#else -static void signal_handler(int32_t signum, siginfo_t *sigInfo, void *context) { - if (signum == SIGUSR1) { - taosCfgDynamicOptions("debugFlag 143"); - return; - } - if (signum == SIGUSR2) { - taosCfgDynamicOptions("resetlog"); - return; - } - dInfo("shut down signal is %d, sender PID:%d cmdline:%s", signum, sigInfo->si_pid, taosGetCmdlineByPID(sigInfo->si_pid)); -#endif - - syslog(LOG_INFO, "Shut down signal is %d", signum); - syslog(LOG_INFO, "Shutting down TDengine service..."); - - // protect the application from receive another signal - struct sigaction act = {{0}}; - act.sa_handler = SIG_IGN; - sigaction(SIGTERM, &act, NULL); - sigaction(SIGINT, &act, NULL); - -#ifndef WINDOWS - sigaction(SIGHUP, &act, NULL); - sigaction(SIGUSR1, &act, NULL); - sigaction(SIGUSR2, &act, NULL); -#endif - - // inform main thread to exit - tsem_post(&exitSem); -} - int32_t main(int32_t argc, char *argv[]) { int dump_config = 0; @@ -147,8 +113,6 @@ int32_t main(int32_t argc, char *argv[]) { /* Set termination handler. */ struct sigaction act = {{0}}; - -#ifndef WINDOWS act.sa_flags = SA_SIGINFO; act.sa_sigaction = signal_handler; sigaction(SIGTERM, &act, NULL); @@ -156,11 +120,6 @@ int32_t main(int32_t argc, char *argv[]) { sigaction(SIGINT, &act, NULL); sigaction(SIGUSR1, &act, NULL); sigaction(SIGUSR2, &act, NULL); -#else - act.sa_handler = signal_handler; - sigaction(SIGTERM, &act, NULL); - sigaction(SIGINT, &act, NULL); -#endif // Open /var/log/syslog file to record information. openlog("TDengine:", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_LOCAL1); @@ -187,3 +146,33 @@ int32_t main(int32_t argc, char *argv[]) { closelog(); return EXIT_SUCCESS; } + +static void signal_handler(int32_t signum, siginfo_t *sigInfo, void *context) { + if (signum == SIGUSR1) { + taosCfgDynamicOptions("debugFlag 143"); + return; + } + if (signum == SIGUSR2) { + taosCfgDynamicOptions("resetlog"); + return; + } + + syslog(LOG_INFO, "Shut down signal is %d", signum); + syslog(LOG_INFO, "Shutting down TDengine service..."); + // clean the system. + dInfo("shut down signal is %d, sender PID:%d cmdline:%s", signum, sigInfo->si_pid, taosGetCmdlineByPID(sigInfo->si_pid)); + + // protect the application from receive another signal + struct sigaction act = {{0}}; +#ifndef WINDOWS + act.sa_handler = SIG_IGN; +#endif + sigaction(SIGTERM, &act, NULL); + sigaction(SIGHUP, &act, NULL); + sigaction(SIGINT, &act, NULL); + sigaction(SIGUSR1, &act, NULL); + sigaction(SIGUSR2, &act, NULL); + + // inform main thread to exit + tsem_post(&exitSem); +} \ No newline at end of file diff --git a/src/kit/shell/src/shellMain.c b/src/kit/shell/src/shellMain.c index 496cef41ba92a7bd7827767bf460541e59da8992..cd801ef07a3f7a0000e76a1601ce7d6372864cac 100644 --- a/src/kit/shell/src/shellMain.c +++ b/src/kit/shell/src/shellMain.c @@ -21,7 +21,7 @@ pthread_t pid; static tsem_t cancelSem; -void shellQueryInterruptHandler(int32_t signum) { +void shellQueryInterruptHandler(int32_t signum, siginfo_t *sigInfo, void *context) { tsem_post(&cancelSem); } diff --git a/src/os/inc/osWindows.h b/src/os/inc/osWindows.h index 396577a5d7feae0c8c679486b861e48cc8d72412..15b26d208d0a549facd751ceb1ccbb36b4f9c217 100644 --- a/src/os/inc/osWindows.h +++ b/src/os/inc/osWindows.h @@ -193,9 +193,19 @@ int gettimeofday(struct timeval *ptv, void *pTimeZone); //for signal, not dispose #define SIGALRM 1234 +#define SIGHUP 1234 +#define SIGUSR1 1234 +#define SIGUSR2 1234 +#define SA_SIGINFO 1234 + typedef int sigset_t; +typedef struct siginfo_t { + int si_pid; +} siginfo_t; struct sigaction { - void (*sa_handler)(int); + int sa_flags; + void (*sa_handler)(int32_t signum, siginfo_t *sigInfo, void *context); + void (*sa_sigaction)(int32_t signum, siginfo_t *sigInfo, void *context); }; int sigaction(int, struct sigaction *, void *); diff --git a/src/os/src/linux/linuxEnv.c b/src/os/src/linux/linuxEnv.c index 5772885cb43454d147dd05dc437347bbdd15f741..e3eadbc94bcf71e7cf09e70901e4de0aed427c54 100644 --- a/src/os/src/linux/linuxEnv.c +++ b/src/os/src/linux/linuxEnv.c @@ -18,7 +18,6 @@ #include "tglobal.h" void osInit() { - #ifdef _TD_POWER_ if (configDir[0] == 0) { strcpy(configDir, "/etc/power"); @@ -43,16 +42,14 @@ void osInit() { char cmdline[1024]; -char *taosGetCmdlineByPID(int pid) -{ - sprintf(cmdline, "/proc/%d/cmdline",pid); - FILE* f = fopen(cmdline,"r"); - if(f){ +char* taosGetCmdlineByPID(int pid) { + sprintf(cmdline, "/proc/%d/cmdline", pid); + FILE* f = fopen(cmdline, "r"); + if (f) { size_t size; size = fread(cmdline, sizeof(char), 1024, f); - if(size>0){ - if('\n'==cmdline[size-1]) - cmdline[size-1]='\0'; + if (size > 0) { + if ('\n' == cmdline[size - 1]) cmdline[size - 1] = '\0'; } fclose(f); } diff --git a/src/os/src/windows/wSysinfo.c b/src/os/src/windows/wSysinfo.c index b3fd4c28b9c92f37cba8eb34fcf8eb5fb564eadb..0499173a0b1f5bd84d9dd6bcaf0613682f7b8027 100644 --- a/src/os/src/windows/wSysinfo.c +++ b/src/os/src/windows/wSysinfo.c @@ -32,10 +32,51 @@ #endif #pragma warning(push) -#pragma warning(disable:4091) +#pragma warning(disable : 4091) #include #pragma warning(pop) +static int32_t taosGetTotalMemory() { + MEMORYSTATUSEX memsStat; + memsStat.dwLength = sizeof(memsStat); + if (!GlobalMemoryStatusEx(&memsStat)) { + return 0; + } + + float nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); + return (int32_t)nMemTotal; +} + +bool taosGetSysMemory(float *memoryUsedMB) { + MEMORYSTATUSEX memsStat; + memsStat.dwLength = sizeof(memsStat); + if (!GlobalMemoryStatusEx(&memsStat)) { + return false; + } + + float nMemFree = memsStat.ullAvailPhys / (1024.0f * 1024.0f); + float nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); + + *memoryUsedMB = nMemTotal - nMemFree; + return true; +} + +bool taosGetProcMemory(float *memoryUsedMB) { + unsigned bytes_used = 0; + +#if defined(_WIN32) && defined(_MSC_VER) + PROCESS_MEMORY_COUNTERS pmc; + HANDLE cur_proc = GetCurrentProcess(); + + if (GetProcessMemoryInfo(cur_proc, &pmc, sizeof(pmc))) { + bytes_used = (unsigned)(pmc.WorkingSetSize + pmc.PagefileUsage); + } +#endif + + *memoryUsedMB = (float)bytes_used / 1024 / 1024; + return true; +} + static void taosGetSystemTimezone() { // get and set default timezone SGlobalCfg *cfg_timezone = taosGetConfigOption("timezone"); @@ -71,16 +112,16 @@ static void taosGetSystemLocale() { } } -void taosPrintOsInfo() {} - -void taosKillSystem() { - uError("function taosKillSystem, exit!"); - exit(0); +static int32_t taosGetCpuCores() { + SYSTEM_INFO info; + GetSystemInfo(&info); + return (int32_t)info.dwNumberOfProcessors; } -void taosGetSystemInfo() { - taosGetSystemTimezone(); - taosGetSystemLocale(); +bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { + *sysCpuUsage = 0; + *procCpuUsage = 0; + return true; } bool taosGetDisk() { @@ -89,20 +130,35 @@ bool taosGetDisk() { unsigned _int64 i64FreeBytesToCaller; unsigned _int64 i64TotalBytes; unsigned _int64 i64FreeBytes; - char dir[4] = {'C', ':', '\\', '\0'}; - int drive_type; if (tscEmbedded) { - drive_type = GetDriveTypeA(dir); - if (drive_type == DRIVE_FIXED) { - fResult = GetDiskFreeSpaceExA(dir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, - (PULARGE_INTEGER)&i64FreeBytes); - if (fResult) { - tsTotalDataDirGB = tsTotalLogDirGB = tsTotalTmpDirGB = (float)(i64TotalBytes / unit); - tsAvailDataDirGB = tsAvailLogDirGB = tsAvailTmpDirectorySpace = (float)(i64FreeBytes / unit); - } + fResult = GetDiskFreeSpaceExA(tsDataDir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, + (PULARGE_INTEGER)&i64FreeBytes); + if (fResult) { + tsTotalDataDirGB = (float)(i64TotalBytes / unit); + tsAvailDataDirGB = (float)(i64FreeBytes / unit); } } + + fResult = GetDiskFreeSpaceExA(tsLogDir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, + (PULARGE_INTEGER)&i64FreeBytes); + if (fResult) { + tsTotalLogDirGB = (float)(i64TotalBytes / unit); + tsAvailLogDirGB = (float)(i64FreeBytes / unit); + } + + fResult = GetDiskFreeSpaceExA(tsTempDir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, + (PULARGE_INTEGER)&i64FreeBytes); + if (fResult) { + tsTotalTmpDirGB = (float)(i64TotalBytes / unit); + tsAvailTmpDirectorySpace = (float)(i64FreeBytes / unit); + } + + return true; +} + +bool taosGetBandSpeed(float *bandSpeedKb) { + *bandSpeedKb = 0; return true; } @@ -144,48 +200,30 @@ bool taosGetProcIO(float *readKB, float *writeKB) { return true; } -bool taosGetBandSpeed(float *bandSpeedKb) { - *bandSpeedKb = 0; - return true; -} - -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { - *sysCpuUsage = 0; - *procCpuUsage = 0; - return true; -} - -bool taosGetProcMemory(float *memoryUsedMB) { - unsigned bytes_used = 0; -#if 0 -#if defined(_WIN32) && defined(_MSC_VER) - PROCESS_MEMORY_COUNTERS pmc; - HANDLE cur_proc = GetCurrentProcess(); - - if (GetProcessMemoryInfo(cur_proc, &pmc, sizeof(pmc))) { - bytes_used = (unsigned)(pmc.WorkingSetSize + pmc.PagefileUsage); - } -#endif -#endif +void taosGetSystemInfo() { + tsNumOfCores = taosGetCpuCores(); + tsTotalMemoryMB = taosGetTotalMemory(); - *memoryUsedMB = (float)bytes_used / 1024 / 1024; + float tmp1, tmp2; + taosGetDisk(); + taosGetBandSpeed(&tmp1); + taosGetCpuUsage(&tmp1, &tmp2); + taosGetProcIO(&tmp1, &tmp2); - return true; + taosGetSystemTimezone(); + taosGetSystemLocale(); } -bool taosGetSysMemory(float *memoryUsedMB) { - MEMORYSTATUSEX memsStat; - float nMemFree; - float nMemTotal; +void taosPrintOsInfo() { + uInfo(" os numOfCores: %d", tsNumOfCores); + uInfo(" os totalDisk: %f(GB)", tsTotalDataDirGB); + uInfo(" os totalMemory: %d(MB)", tsTotalMemoryMB); + uInfo("=================================="); +} - memsStat.dwLength = sizeof(memsStat); - if (!GlobalMemoryStatusEx(&memsStat)) { - return false; - } - nMemFree = memsStat.ullAvailPhys / (1024.0f * 1024.0f); - nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); - *memoryUsedMB = nMemTotal - nMemFree; - return true; +void taosKillSystem() { + uError("function taosKillSystem, exit!"); + exit(0); } int taosSystem(const char *cmd) { @@ -240,4 +278,6 @@ void taosSetCoreDump() { SetUnhandledExceptionFilter(&FlCrashDump); } bool taosGetSystemUid(char *uid) { sprintf(uid, "uid_not_implemented_yet"); return true; -} \ No newline at end of file +} + +char *taosGetCmdlineByPID(int pid) { return ""; } diff --git a/src/sync/src/syncArbitrator.c b/src/sync/src/syncArbitrator.c index dbcc4b40a6efca3c9f66f6f2fcb512f150bfd2fd..06b4a61d6f9132f190af3c446bf1db72b4ca43cc 100644 --- a/src/sync/src/syncArbitrator.c +++ b/src/sync/src/syncArbitrator.c @@ -27,7 +27,7 @@ #include "syncInt.h" #include "syncTcp.h" -static void arbSignalHandler(int32_t signum); +static void arbSignalHandler(int32_t signum, siginfo_t *sigInfo, void *context); static void arbProcessIncommingConnection(SOCKET connFd, uint32_t sourceIp); static void arbProcessBrokenLink(int64_t rid); static int32_t arbProcessPeerMsg(int64_t rid, void *buffer); @@ -35,9 +35,9 @@ static tsem_t tsArbSem; static void * tsArbTcpPool; typedef struct { - char id[TSDB_EP_LEN + 24]; - SOCKET nodeFd; - void * pConn; + char id[TSDB_EP_LEN + 24]; + SOCKET nodeFd; + void * pConn; } SNodeConn; int32_t main(int32_t argc, char *argv[]) { @@ -70,14 +70,13 @@ int32_t main(int32_t argc, char *argv[]) { /* Set termination handler. */ struct sigaction act = {{0}}; - memset(&act, 0, sizeof(struct sigaction)); + act.sa_flags = SA_SIGINFO; + act.sa_sigaction = arbSignalHandler; act.sa_handler = arbSignalHandler; sigaction(SIGTERM, &act, NULL); - sigaction(SIGINT, &act, NULL); -#ifndef WINDOWS sigaction(SIGHUP, &act, NULL); -#endif + sigaction(SIGINT, &act, NULL); tsAsyncLog = 0; strcat(arbLogPath, "/arbitrator.log"); @@ -107,6 +106,7 @@ int32_t main(int32_t argc, char *argv[]) { syncCloseTcpThreadPool(tsArbTcpPool); sInfo("TAOS arbitrator is shut down"); + closelog(); return 0; } @@ -174,16 +174,16 @@ static int32_t arbProcessPeerMsg(int64_t rid, void *buffer) { return 0; } -static void arbSignalHandler(int32_t signum) { +static void arbSignalHandler(int32_t signum, siginfo_t *sigInfo, void *context) { struct sigaction act = {{0}}; +#ifndef WINDOWS act.sa_handler = SIG_IGN; +#endif sigaction(SIGTERM, &act, NULL); - sigaction(SIGINT, &act, NULL); -#ifndef WINDOWS sigaction(SIGHUP, &act, NULL); -#endif + sigaction(SIGINT, &act, NULL); - sInfo("shut down signal is %d", signum); + sInfo("shut down signal is %d, sender PID:%d", signum, sigInfo->si_pid); // inform main thread to exit tsem_post(&tsArbSem);