This commit is contained in:
Shengliang Guan 2021-01-15 18:14:50 +08:00
parent ac49a78d42
commit 8a9ed8e2ef
2 changed files with 62 additions and 59 deletions

View File

@ -124,8 +124,8 @@ void bnStartTimer(int32_t mseconds) {
bool updateSoon = (mseconds != -1); bool updateSoon = (mseconds != -1);
if (updateSoon) { if (updateSoon) {
mTrace("balance function will be called after %" PRId64 " ms", mseconds); mTrace("balance function will be called after %d ms", mseconds);
taosTmrReset(bnProcessTimer, mseconds, (void *)mseconds, tsMnodeTmr, &tsBnThread.timer); taosTmrReset(bnProcessTimer, mseconds, (void *)(int64_t)mseconds, tsMnodeTmr, &tsBnThread.timer);
} else { } else {
taosTmrReset(bnProcessTimer, tsStatusInterval * 1000, NULL, tsMnodeTmr, &tsBnThread.timer); taosTmrReset(bnProcessTimer, tsStatusInterval * 1000, NULL, tsMnodeTmr, &tsBnThread.timer);
} }

View File

@ -45,6 +45,21 @@ static char tsProcMemFile[25] = {0};
static char tsProcIOFile[25] = {0}; static char tsProcIOFile[25] = {0};
static float tsPageSizeKB = 0; static float tsPageSizeKB = 0;
static void taosGetProcInfos() {
tsPageSize = sysconf(_SC_PAGESIZE);
tsOpenMax = sysconf(_SC_OPEN_MAX);
tsStreamMax = sysconf(_SC_STREAM_MAX);
tsProcId = (pid_t)syscall(SYS_gettid);
tsPageSizeKB = (float)(sysconf(_SC_PAGESIZE)) / 1024;
snprintf(tsProcMemFile, 25, "/proc/%d/status", tsProcId);
snprintf(tsProcCpuFile, 25, "/proc/%d/stat", tsProcId);
snprintf(tsProcIOFile, 25, "/proc/%d/io", tsProcId);
}
static int32_t taosGetTotalMemory() { return (int32_t)((float)sysconf(_SC_PHYS_PAGES) * tsPageSizeKB / 1024); }
bool taosGetSysMemory(float *memoryUsedMB) { bool taosGetSysMemory(float *memoryUsedMB) {
float memoryAvailMB = (float)sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB / 1024; float memoryAvailMB = (float)sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB / 1024;
*memoryUsedMB = (float)tsTotalMemoryMB - memoryAvailMB; *memoryUsedMB = (float)tsTotalMemoryMB - memoryAvailMB;
@ -105,7 +120,8 @@ static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) {
} }
char cpu[10] = {0}; char cpu[10] = {0};
sscanf(line, "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, cpu, &cpuInfo->user, &cpuInfo->nice, &cpuInfo->system, &cpuInfo->idle); sscanf(line, "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, cpu, &cpuInfo->user, &cpuInfo->nice, &cpuInfo->system,
&cpuInfo->idle);
tfree(line); tfree(line);
fclose(fp); fclose(fp);
@ -131,7 +147,8 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) {
for (int i = 0, blank = 0; line[i] != 0; ++i) { for (int i = 0, blank = 0; line[i] != 0; ++i) {
if (line[i] == ' ') blank++; if (line[i] == ' ') blank++;
if (blank == PROCESS_ITEM) { if (blank == PROCESS_ITEM) {
sscanf(line + i + 1, "%" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, &cpuInfo->utime, &cpuInfo->stime, &cpuInfo->cutime, &cpuInfo->cstime); sscanf(line + i + 1, "%" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, &cpuInfo->utime, &cpuInfo->stime,
&cpuInfo->cutime, &cpuInfo->cstime);
break; break;
} }
} }
@ -162,12 +179,12 @@ static void taosGetSystemTimezone() {
char buf[68] = {0}; char buf[68] = {0};
if (f != NULL) { if (f != NULL) {
int len = fread(buf, 64, 1, f); int len = fread(buf, 64, 1, f);
if(len < 64 && ferror(f)) { if (len < 64 && ferror(f)) {
fclose(f); fclose(f);
uError("read /etc/timezone error, reason:%s", strerror(errno)); uError("read /etc/timezone error, reason:%s", strerror(errno));
return; return;
} }
fclose(f); fclose(f);
buf[sizeof(buf) - 1] = 0; buf[sizeof(buf) - 1] = 0;
@ -258,6 +275,8 @@ static void taosGetSystemLocale() { // get and set default locale
} }
} }
static int32_t taosGetCpuCores() { return (int32_t)sysconf(_SC_NPROCESSORS_ONLN); }
bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) {
static uint64_t lastSysUsed = 0; static uint64_t lastSysUsed = 0;
static uint64_t lastSysTotal = 0; static uint64_t lastSysTotal = 0;
@ -300,11 +319,9 @@ bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) {
bool taosGetDisk() { bool taosGetDisk() {
struct statvfs info; struct statvfs info;
const double unit = 1024 * 1024 * 1024; const double unit = 1024 * 1024 * 1024;
if (tscEmbedded) { if (tscEmbedded) {
if (statvfs(tsDataDir, &info)) { if (statvfs(tsDataDir, &info)) {
//tsTotalDataDirGB = 0;
//tsAvailDataDirGB = 0;
uError("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); uError("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno));
return false; return false;
} else { } else {
@ -314,8 +331,6 @@ bool taosGetDisk() {
} }
if (statvfs(tsLogDir, &info)) { if (statvfs(tsLogDir, &info)) {
//tsTotalLogDirGB = 0;
//tsAvailLogDirGB = 0;
uError("failed to get disk size, logDir:%s errno:%s", tsLogDir, strerror(errno)); uError("failed to get disk size, logDir:%s errno:%s", tsLogDir, strerror(errno));
return false; return false;
} else { } else {
@ -324,8 +339,6 @@ bool taosGetDisk() {
} }
if (statvfs("/tmp", &info)) { if (statvfs("/tmp", &info)) {
//tsTotalTmpDirGB = 0;
//tsAvailTmpDirectorySpace = 0;
uError("failed to get disk size, tmpDir:/tmp errno:%s", strerror(errno)); uError("failed to get disk size, tmpDir:/tmp errno:%s", strerror(errno));
return false; return false;
} else { } else {
@ -344,13 +357,12 @@ static bool taosGetCardInfo(int64_t *bytes) {
return false; return false;
} }
size_t len = 2048; size_t len = 2048;
char * line = calloc(1, len); char * line = calloc(1, len);
while (!feof(fp)) { while (!feof(fp)) {
memset(line, 0, len); memset(line, 0, len);
int64_t rbytes = 0; int64_t rbytes = 0;
int64_t rpackts = 0; int64_t rpackts = 0;
int64_t tbytes = 0; int64_t tbytes = 0;
@ -465,7 +477,7 @@ bool taosGetProcIO(float *readKB, float *writeKB) {
static int64_t lastReadbyte = -1; static int64_t lastReadbyte = -1;
static int64_t lastWritebyte = -1; static int64_t lastWritebyte = -1;
int64_t curReadbyte = 0; int64_t curReadbyte = 0;
int64_t curWritebyte = 0; int64_t curWritebyte = 0;
if (!taosReadProcIO(&curReadbyte, &curWritebyte)) { if (!taosReadProcIO(&curReadbyte, &curWritebyte)) {
@ -490,18 +502,10 @@ bool taosGetProcIO(float *readKB, float *writeKB) {
} }
void taosGetSystemInfo() { void taosGetSystemInfo() {
tsNumOfCores = (int32_t)sysconf(_SC_NPROCESSORS_ONLN); taosGetProcInfos();
tsPageSize = sysconf(_SC_PAGESIZE);
tsOpenMax = sysconf(_SC_OPEN_MAX);
tsStreamMax = sysconf(_SC_STREAM_MAX);
tsProcId = (pid_t)syscall(SYS_gettid); tsNumOfCores = taosGetCpuCores();
tsPageSizeKB = (float)(sysconf(_SC_PAGESIZE)) / 1024; tsTotalMemoryMB = taosGetTotalMemory();
tsTotalMemoryMB = (int32_t)((float)sysconf(_SC_PHYS_PAGES) * tsPageSizeKB / 1024);
snprintf(tsProcMemFile, 25, "/proc/%d/status", tsProcId);
snprintf(tsProcCpuFile, 25, "/proc/%d/stat", tsProcId);
snprintf(tsProcIOFile, 25, "/proc/%d/io", tsProcId);
float tmp1, tmp2; float tmp1, tmp2;
taosGetSysMemory(&tmp1); taosGetSysMemory(&tmp1);
@ -573,16 +577,16 @@ void taosSetCoreDump() {
if (0 == tsEnableCoreFile) { if (0 == tsEnableCoreFile) {
return; return;
} }
// 1. set ulimit -c unlimited // 1. set ulimit -c unlimited
struct rlimit rlim; struct rlimit rlim;
struct rlimit rlim_new; struct rlimit rlim_new;
if (getrlimit(RLIMIT_CORE, &rlim) == 0) { if (getrlimit(RLIMIT_CORE, &rlim) == 0) {
#ifndef _ALPINE #ifndef _ALPINE
uInfo("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); uInfo("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max);
#else #else
uInfo("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); uInfo("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max);
#endif #endif
rlim_new.rlim_cur = RLIM_INFINITY; rlim_new.rlim_cur = RLIM_INFINITY;
rlim_new.rlim_max = RLIM_INFINITY; rlim_new.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) { if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) {
@ -594,57 +598,56 @@ void taosSetCoreDump() {
} }
if (getrlimit(RLIMIT_CORE, &rlim) == 0) { if (getrlimit(RLIMIT_CORE, &rlim) == 0) {
#ifndef _ALPINE #ifndef _ALPINE
uInfo("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); uInfo("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max);
#else #else
uInfo("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); uInfo("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max);
#endif #endif
} }
#ifndef _TD_ARM_ #ifndef _TD_ARM_
// 2. set the path for saving core file // 2. set the path for saving core file
struct __sysctl_args args; struct __sysctl_args args;
int old_usespid = 0;
size_t old_len = 0; int old_usespid = 0;
int new_usespid = 1; size_t old_len = 0;
size_t new_len = sizeof(new_usespid); int new_usespid = 1;
size_t new_len = sizeof(new_usespid);
int name[] = {CTL_KERN, KERN_CORE_USES_PID}; int name[] = {CTL_KERN, KERN_CORE_USES_PID};
memset(&args, 0, sizeof(struct __sysctl_args)); memset(&args, 0, sizeof(struct __sysctl_args));
args.name = name; args.name = name;
args.nlen = sizeof(name)/sizeof(name[0]); args.nlen = sizeof(name) / sizeof(name[0]);
args.oldval = &old_usespid; args.oldval = &old_usespid;
args.oldlenp = &old_len; args.oldlenp = &old_len;
args.newval = &new_usespid; args.newval = &new_usespid;
args.newlen = new_len; args.newlen = new_len;
old_len = sizeof(old_usespid); old_len = sizeof(old_usespid);
if (syscall(SYS__sysctl, &args) == -1) { if (syscall(SYS__sysctl, &args) == -1) {
uInfo("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno)); uInfo("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno));
} }
uInfo("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); uInfo("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid);
old_usespid = 0; old_usespid = 0;
old_len = 0; old_len = 0;
memset(&args, 0, sizeof(struct __sysctl_args)); memset(&args, 0, sizeof(struct __sysctl_args));
args.name = name; args.name = name;
args.nlen = sizeof(name)/sizeof(name[0]); args.nlen = sizeof(name) / sizeof(name[0]);
args.oldval = &old_usespid; args.oldval = &old_usespid;
args.oldlenp = &old_len; args.oldlenp = &old_len;
old_len = sizeof(old_usespid); old_len = sizeof(old_usespid);
if (syscall(SYS__sysctl, &args) == -1) { if (syscall(SYS__sysctl, &args) == -1) {
uInfo("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno)); uInfo("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno));
} }
uInfo("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); uInfo("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid);
#endif #endif
} }
bool taosGetSystemUid(char *uid) { bool taosGetSystemUid(char *uid) {