This commit is contained in:
Shengliang Guan 2021-01-15 18:55:29 +08:00
parent 8a9ed8e2ef
commit de09879d50
6 changed files with 161 additions and 125 deletions

View File

@ -19,43 +19,9 @@
#include "tconfig.h" #include "tconfig.h"
#include "dnodeMain.h" #include "dnodeMain.h"
static void signal_handler(int32_t signum, siginfo_t *sigInfo, void *context);
static tsem_t exitSem; 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[]) { int32_t main(int32_t argc, char *argv[]) {
int dump_config = 0; int dump_config = 0;
@ -147,8 +113,6 @@ int32_t main(int32_t argc, char *argv[]) {
/* Set termination handler. */ /* Set termination handler. */
struct sigaction act = {{0}}; struct sigaction act = {{0}};
#ifndef WINDOWS
act.sa_flags = SA_SIGINFO; act.sa_flags = SA_SIGINFO;
act.sa_sigaction = signal_handler; act.sa_sigaction = signal_handler;
sigaction(SIGTERM, &act, NULL); sigaction(SIGTERM, &act, NULL);
@ -156,11 +120,6 @@ int32_t main(int32_t argc, char *argv[]) {
sigaction(SIGINT, &act, NULL); sigaction(SIGINT, &act, NULL);
sigaction(SIGUSR1, &act, NULL); sigaction(SIGUSR1, &act, NULL);
sigaction(SIGUSR2, &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. // Open /var/log/syslog file to record information.
openlog("TDengine:", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_LOCAL1); openlog("TDengine:", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_LOCAL1);
@ -187,3 +146,33 @@ int32_t main(int32_t argc, char *argv[]) {
closelog(); closelog();
return EXIT_SUCCESS; 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);
}

View File

@ -21,7 +21,7 @@
pthread_t pid; pthread_t pid;
static tsem_t cancelSem; static tsem_t cancelSem;
void shellQueryInterruptHandler(int32_t signum) { void shellQueryInterruptHandler(int32_t signum, siginfo_t *sigInfo, void *context) {
tsem_post(&cancelSem); tsem_post(&cancelSem);
} }

View File

@ -193,9 +193,19 @@ int gettimeofday(struct timeval *ptv, void *pTimeZone);
//for signal, not dispose //for signal, not dispose
#define SIGALRM 1234 #define SIGALRM 1234
#define SIGHUP 1234
#define SIGUSR1 1234
#define SIGUSR2 1234
#define SA_SIGINFO 1234
typedef int sigset_t; typedef int sigset_t;
typedef struct siginfo_t {
int si_pid;
} siginfo_t;
struct sigaction { 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 *); int sigaction(int, struct sigaction *, void *);

View File

@ -18,7 +18,6 @@
#include "tglobal.h" #include "tglobal.h"
void osInit() { void osInit() {
#ifdef _TD_POWER_ #ifdef _TD_POWER_
if (configDir[0] == 0) { if (configDir[0] == 0) {
strcpy(configDir, "/etc/power"); strcpy(configDir, "/etc/power");
@ -43,16 +42,14 @@ void osInit() {
char cmdline[1024]; char cmdline[1024];
char *taosGetCmdlineByPID(int pid) char* taosGetCmdlineByPID(int pid) {
{ sprintf(cmdline, "/proc/%d/cmdline", pid);
sprintf(cmdline, "/proc/%d/cmdline",pid); FILE* f = fopen(cmdline, "r");
FILE* f = fopen(cmdline,"r"); if (f) {
if(f){
size_t size; size_t size;
size = fread(cmdline, sizeof(char), 1024, f); size = fread(cmdline, sizeof(char), 1024, f);
if(size>0){ if (size > 0) {
if('\n'==cmdline[size-1]) if ('\n' == cmdline[size - 1]) cmdline[size - 1] = '\0';
cmdline[size-1]='\0';
} }
fclose(f); fclose(f);
} }

View File

@ -32,10 +32,51 @@
#endif #endif
#pragma warning(push) #pragma warning(push)
#pragma warning(disable:4091) #pragma warning(disable : 4091)
#include <DbgHelp.h> #include <DbgHelp.h>
#pragma warning(pop) #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() { static void taosGetSystemTimezone() {
// get and set default timezone // get and set default timezone
SGlobalCfg *cfg_timezone = taosGetConfigOption("timezone"); SGlobalCfg *cfg_timezone = taosGetConfigOption("timezone");
@ -71,16 +112,16 @@ static void taosGetSystemLocale() {
} }
} }
void taosPrintOsInfo() {} static int32_t taosGetCpuCores() {
SYSTEM_INFO info;
void taosKillSystem() { GetSystemInfo(&info);
uError("function taosKillSystem, exit!"); return (int32_t)info.dwNumberOfProcessors;
exit(0);
} }
void taosGetSystemInfo() { bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) {
taosGetSystemTimezone(); *sysCpuUsage = 0;
taosGetSystemLocale(); *procCpuUsage = 0;
return true;
} }
bool taosGetDisk() { bool taosGetDisk() {
@ -89,20 +130,35 @@ bool taosGetDisk() {
unsigned _int64 i64FreeBytesToCaller; unsigned _int64 i64FreeBytesToCaller;
unsigned _int64 i64TotalBytes; unsigned _int64 i64TotalBytes;
unsigned _int64 i64FreeBytes; unsigned _int64 i64FreeBytes;
char dir[4] = {'C', ':', '\\', '\0'};
int drive_type;
if (tscEmbedded) { if (tscEmbedded) {
drive_type = GetDriveTypeA(dir); fResult = GetDiskFreeSpaceExA(tsDataDir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes,
if (drive_type == DRIVE_FIXED) {
fResult = GetDiskFreeSpaceExA(dir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes); (PULARGE_INTEGER)&i64FreeBytes);
if (fResult) { if (fResult) {
tsTotalDataDirGB = tsTotalLogDirGB = tsTotalTmpDirGB = (float)(i64TotalBytes / unit); tsTotalDataDirGB = (float)(i64TotalBytes / unit);
tsAvailDataDirGB = tsAvailLogDirGB = tsAvailTmpDirectorySpace = (float)(i64FreeBytes / 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; return true;
} }
@ -144,48 +200,30 @@ bool taosGetProcIO(float *readKB, float *writeKB) {
return true; return true;
} }
bool taosGetBandSpeed(float *bandSpeedKb) { void taosGetSystemInfo() {
*bandSpeedKb = 0; tsNumOfCores = taosGetCpuCores();
return true; tsTotalMemoryMB = taosGetTotalMemory();
float tmp1, tmp2;
taosGetDisk();
taosGetBandSpeed(&tmp1);
taosGetCpuUsage(&tmp1, &tmp2);
taosGetProcIO(&tmp1, &tmp2);
taosGetSystemTimezone();
taosGetSystemLocale();
} }
bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { void taosPrintOsInfo() {
*sysCpuUsage = 0; uInfo(" os numOfCores: %d", tsNumOfCores);
*procCpuUsage = 0; uInfo(" os totalDisk: %f(GB)", tsTotalDataDirGB);
return true; uInfo(" os totalMemory: %d(MB)", tsTotalMemoryMB);
uInfo("==================================");
} }
bool taosGetProcMemory(float *memoryUsedMB) { void taosKillSystem() {
unsigned bytes_used = 0; uError("function taosKillSystem, exit!");
#if 0 exit(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
*memoryUsedMB = (float)bytes_used / 1024 / 1024;
return true;
}
bool taosGetSysMemory(float *memoryUsedMB) {
MEMORYSTATUSEX memsStat;
float nMemFree;
float nMemTotal;
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;
} }
int taosSystem(const char *cmd) { int taosSystem(const char *cmd) {
@ -241,3 +279,5 @@ bool taosGetSystemUid(char *uid) {
sprintf(uid, "uid_not_implemented_yet"); sprintf(uid, "uid_not_implemented_yet");
return true; return true;
} }
char *taosGetCmdlineByPID(int pid) { return ""; }

View File

@ -27,7 +27,7 @@
#include "syncInt.h" #include "syncInt.h"
#include "syncTcp.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 arbProcessIncommingConnection(SOCKET connFd, uint32_t sourceIp);
static void arbProcessBrokenLink(int64_t rid); static void arbProcessBrokenLink(int64_t rid);
static int32_t arbProcessPeerMsg(int64_t rid, void *buffer); static int32_t arbProcessPeerMsg(int64_t rid, void *buffer);
@ -70,14 +70,13 @@ int32_t main(int32_t argc, char *argv[]) {
/* Set termination handler. */ /* Set termination handler. */
struct sigaction act = {{0}}; struct sigaction act = {{0}};
memset(&act, 0, sizeof(struct sigaction)); act.sa_flags = SA_SIGINFO;
act.sa_sigaction = arbSignalHandler;
act.sa_handler = arbSignalHandler; act.sa_handler = arbSignalHandler;
sigaction(SIGTERM, &act, NULL); sigaction(SIGTERM, &act, NULL);
sigaction(SIGINT, &act, NULL);
#ifndef WINDOWS
sigaction(SIGHUP, &act, NULL); sigaction(SIGHUP, &act, NULL);
#endif sigaction(SIGINT, &act, NULL);
tsAsyncLog = 0; tsAsyncLog = 0;
strcat(arbLogPath, "/arbitrator.log"); strcat(arbLogPath, "/arbitrator.log");
@ -107,6 +106,7 @@ int32_t main(int32_t argc, char *argv[]) {
syncCloseTcpThreadPool(tsArbTcpPool); syncCloseTcpThreadPool(tsArbTcpPool);
sInfo("TAOS arbitrator is shut down"); sInfo("TAOS arbitrator is shut down");
closelog();
return 0; return 0;
} }
@ -174,16 +174,16 @@ static int32_t arbProcessPeerMsg(int64_t rid, void *buffer) {
return 0; return 0;
} }
static void arbSignalHandler(int32_t signum) { static void arbSignalHandler(int32_t signum, siginfo_t *sigInfo, void *context) {
struct sigaction act = {{0}}; struct sigaction act = {{0}};
act.sa_handler = SIG_IGN;
sigaction(SIGTERM, &act, NULL);
sigaction(SIGINT, &act, NULL);
#ifndef WINDOWS #ifndef WINDOWS
sigaction(SIGHUP, &act, NULL); act.sa_handler = SIG_IGN;
#endif #endif
sigaction(SIGTERM, &act, NULL);
sigaction(SIGHUP, &act, NULL);
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 // inform main thread to exit
tsem_post(&tsArbSem); tsem_post(&tsArbSem);