osSysinfo.c 31.2 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
18
#include "taoserror.h"
S
Shengliang Guan 已提交
19

sangshuduo's avatar
sangshuduo 已提交
20 21 22 23
#if defined(CUS_NAME) || defined(CUS_PROMPT) || defined(CUS_EMAIL)
#include "cus_name.h"
#endif

wafwerar's avatar
wafwerar 已提交
24
#define PROCESS_ITEM 12
25
#define UUIDLEN37 37
wafwerar's avatar
wafwerar 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

typedef struct {
  uint64_t user;
  uint64_t nice;
  uint64_t system;
  uint64_t idle;
} SysCpuInfo;

typedef struct {
  uint64_t utime;   // user time
  uint64_t stime;   // kernel time
  uint64_t cutime;  // all user time
  uint64_t cstime;  // all dead time
} ProcCpuInfo;

#ifdef WINDOWS
S
Shengliang Guan 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

/*
 * windows implementation
 */

#if (_WIN64)
#include <iphlpapi.h>
#include <mswsock.h>
#include <psapi.h>
#include <stdio.h>
#include <windows.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Mswsock.lib ")
#endif

#include <objbase.h>

#pragma warning(push)
#pragma warning(disable : 4091)
#include <DbgHelp.h>
#pragma warning(pop)

LONG WINAPI FlCrashDump(PEXCEPTION_POINTERS ep) {
  typedef BOOL(WINAPI * FxMiniDumpWriteDump)(IN HANDLE hProcess, IN DWORD ProcessId, IN HANDLE hFile,
                                             IN MINIDUMP_TYPE                           DumpType,
                                             IN CONST PMINIDUMP_EXCEPTION_INFORMATION   ExceptionParam,
                                             IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
                                             IN CONST PMINIDUMP_CALLBACK_INFORMATION    CallbackParam);

  HMODULE dll = LoadLibrary("dbghelp.dll");
  if (dll == NULL) return EXCEPTION_CONTINUE_SEARCH;
  FxMiniDumpWriteDump mdwd = (FxMiniDumpWriteDump)(GetProcAddress(dll, "MiniDumpWriteDump"));
  if (mdwd == NULL) {
    FreeLibrary(dll);
    return EXCEPTION_CONTINUE_SEARCH;
  }

  TCHAR path[MAX_PATH];
  DWORD len = GetModuleFileName(NULL, path, _countof(path));
  path[len - 3] = 'd';
  path[len - 2] = 'm';
  path[len - 1] = 'p';

  HANDLE file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

  MINIDUMP_EXCEPTION_INFORMATION mei;
  mei.ThreadId = GetCurrentThreadId();
  mei.ExceptionPointers = ep;
  mei.ClientPointers = FALSE;

  (*mdwd)(GetCurrentProcess(), GetCurrentProcessId(), file, MiniDumpWithHandleData, &mei, NULL, NULL);

  CloseHandle(file);
  FreeLibrary(dll);

  return EXCEPTION_CONTINUE_SEARCH;
}
wafwerar's avatar
wafwerar 已提交
99
LONG WINAPI exceptionHandler(LPEXCEPTION_POINTERS exception);
S
Shengliang Guan 已提交
100 101 102 103 104

#elif defined(_TD_DARWIN_64)

#include <errno.h>
#include <libproc.h>
F
facetosea 已提交
105
#include <sys/sysctl.h>
106 107 108
#include <SystemConfiguration/SCDynamicStoreCopySpecific.h>
#include <CoreFoundation/CFString.h>
#include <stdio.h>
S
Shengliang Guan 已提交
109 110 111 112 113 114 115 116 117 118

#else

#include <argp.h>
#include <linux/sysctl.h>
#include <sys/file.h>
#include <sys/resource.h>
#include <sys/statvfs.h>
#include <sys/syscall.h>
#include <sys/utsname.h>
S
Shengliang Guan 已提交
119
#include <unistd.h>
S
Shengliang Guan 已提交
120 121 122 123 124 125 126 127

static pid_t tsProcId;
static char  tsSysNetFile[] = "/proc/net/dev";
static char  tsSysCpuFile[] = "/proc/stat";
static char  tsProcCpuFile[25] = {0};
static char  tsProcMemFile[25] = {0};
static char  tsProcIOFile[25] = {0};

S
monitor  
Shengliang Guan 已提交
128
static void taosGetProcIOnfos() {
S
Shengliang Guan 已提交
129
  tsPageSizeKB = sysconf(_SC_PAGESIZE) / 1024;
S
Shengliang Guan 已提交
130
  tsOpenMax = sysconf(_SC_OPEN_MAX);
sangshuduo's avatar
sangshuduo 已提交
131
  tsStreamMax = TMAX(sysconf(_SC_STREAM_MAX), 0);
S
Shengliang Guan 已提交
132 133
  tsProcId = (pid_t)syscall(SYS_gettid);

S
Shengliang Guan 已提交
134 135 136
  snprintf(tsProcMemFile, sizeof(tsProcMemFile), "/proc/%d/status", tsProcId);
  snprintf(tsProcCpuFile, sizeof(tsProcCpuFile), "/proc/%d/stat", tsProcId);
  snprintf(tsProcIOFile, sizeof(tsProcIOFile), "/proc/%d/io", tsProcId);
S
Shengliang Guan 已提交
137
}
wafwerar's avatar
wafwerar 已提交
138
#endif
S
Shengliang Guan 已提交
139

wafwerar's avatar
wafwerar 已提交
140
static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) {
wafwerar's avatar
wafwerar 已提交
141
#ifdef WINDOWS
142 143 144 145 146 147
  FILETIME pre_idleTime = {0};
  FILETIME pre_kernelTime = {0};
  FILETIME pre_userTime = {0};
  FILETIME idleTime;
  FILETIME kernelTime;
  FILETIME userTime;
H
Hongze Cheng 已提交
148
  bool     res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
149 150 151 152 153 154
  if (res) {
    cpuInfo->idle = CompareFileTime(&pre_idleTime, &idleTime);
    cpuInfo->system = CompareFileTime(&pre_kernelTime, &kernelTime);
    cpuInfo->user = CompareFileTime(&pre_userTime, &userTime);
    cpuInfo->nice = 0;
  }
wafwerar's avatar
wafwerar 已提交
155 156 157 158 159
#elif defined(DARWIN)
  cpuInfo->idle = 0;
  cpuInfo->system = 0;
  cpuInfo->user = 0;
  cpuInfo->nice = 0;
wafwerar's avatar
wafwerar 已提交
160
#else
wafwerar's avatar
wafwerar 已提交
161
  TdFilePtr pFile = taosOpenFile(tsSysCpuFile, TD_FILE_READ | TD_FILE_STREAM);
162
  if (pFile == NULL) {
S
Shengliang Guan 已提交
163
    return -1;
S
Shengliang Guan 已提交
164 165
  }

wafwerar's avatar
wafwerar 已提交
166
  char    line[1024];
H
Haojun Liao 已提交
167 168
  ssize_t bytes = taosGetsFile(pFile, sizeof(line), line);
  if (bytes < 0) {
169
    taosCloseFile(&pFile);
S
Shengliang Guan 已提交
170
    return -1;
S
Shengliang Guan 已提交
171 172
  }

wafwerar's avatar
wafwerar 已提交
173 174 175
  char cpu[10] = {0};
  sscanf(line, "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, cpu, &cpuInfo->user, &cpuInfo->nice, &cpuInfo->system,
         &cpuInfo->idle);
S
Shengliang Guan 已提交
176

177
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
178
#endif
S
Shengliang Guan 已提交
179
  return 0;
S
Shengliang Guan 已提交
180 181
}

wafwerar's avatar
wafwerar 已提交
182
static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) {
wafwerar's avatar
wafwerar 已提交
183
#ifdef WINDOWS
184 185
  FILETIME pre_krnlTm = {0};
  FILETIME pre_usrTm = {0};
H
Hongze Cheng 已提交
186
  FILETIME creatTm, exitTm, krnlTm, usrTm;
187

H
Hongze Cheng 已提交
188
  if (GetThreadTimes(GetCurrentThread(), &creatTm, &exitTm, &krnlTm, &usrTm)) {
189 190 191 192
    cpuInfo->stime = CompareFileTime(&pre_krnlTm, &krnlTm);
    cpuInfo->utime = CompareFileTime(&pre_usrTm, &usrTm);
    cpuInfo->cutime = 0;
    cpuInfo->cstime = 0;
H
Hongze Cheng 已提交
193
  }
wafwerar's avatar
wafwerar 已提交
194 195 196 197 198
#elif defined(DARWIN)
  cpuInfo->stime = 0;
  cpuInfo->utime = 0;
  cpuInfo->cutime = 0;
  cpuInfo->cstime = 0;
wafwerar's avatar
wafwerar 已提交
199
#else
wafwerar's avatar
wafwerar 已提交
200
  TdFilePtr pFile = taosOpenFile(tsProcCpuFile, TD_FILE_READ | TD_FILE_STREAM);
201
  if (pFile == NULL) {
S
Shengliang Guan 已提交
202
    return -1;
S
Shengliang Guan 已提交
203 204
  }

H
Haojun Liao 已提交
205 206 207
  char    line[1024] = {0};
  ssize_t bytes = taosGetsFile(pFile, sizeof(line), line);
  if (bytes < 0) {
208
    taosCloseFile(&pFile);
S
Shengliang Guan 已提交
209
    return -1;
S
Shengliang Guan 已提交
210 211
  }

wafwerar's avatar
wafwerar 已提交
212 213 214 215 216 217 218 219
  for (int i = 0, blank = 0; line[i] != 0; ++i) {
    if (line[i] == ' ') blank++;
    if (blank == PROCESS_ITEM) {
      sscanf(line + i + 1, "%" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, &cpuInfo->utime, &cpuInfo->stime,
             &cpuInfo->cutime, &cpuInfo->cstime);
      break;
    }
  }
S
Shengliang Guan 已提交
220

221
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
222
#endif
S
Shengliang Guan 已提交
223
  return 0;
S
Shengliang Guan 已提交
224 225
}

D
dapan1121 已提交
226
bool taosCheckSystemIsLittleEnd() {
227 228 229 230 231 232
  union check {
    int16_t i;
    char    ch[2];
  } c;
  c.i = 1;
  return c.ch[0] == 1;
wafwerar's avatar
wafwerar 已提交
233 234 235
}

void taosGetSystemInfo() {
wafwerar's avatar
wafwerar 已提交
236
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
237 238
  taosGetCpuCores(&tsNumOfCores);
  taosGetTotalMemory(&tsTotalMemoryKB);
S
Shengliang Guan 已提交
239
  taosGetCpuUsage(NULL, NULL);
wafwerar's avatar
wafwerar 已提交
240 241 242 243 244 245 246 247 248 249
#elif defined(_TD_DARWIN_64)
  long physical_pages = sysconf(_SC_PHYS_PAGES);
  long page_size = sysconf(_SC_PAGESIZE);
  tsTotalMemoryKB = physical_pages * page_size / 1024;
  tsPageSizeKB = page_size / 1024;
  tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN);
#else
  taosGetProcIOnfos();
  taosGetCpuCores(&tsNumOfCores);
  taosGetTotalMemory(&tsTotalMemoryKB);
S
Shengliang Guan 已提交
250
  taosGetCpuUsage(NULL, NULL);
H
Haojun Liao 已提交
251
  taosGetCpuInstructions(&tsSSE42Enable, &tsAVXEnable, &tsAVX2Enable, &tsFMAEnable);
wafwerar's avatar
wafwerar 已提交
252 253 254 255
#endif
}

int32_t taosGetEmail(char *email, int32_t maxLen) {
wafwerar's avatar
wafwerar 已提交
256
#ifdef WINDOWS
X
xinsheng Ren 已提交
257
  // ASSERT(0);
wafwerar's avatar
wafwerar 已提交
258
#elif defined(_TD_DARWIN_64)
sangshuduo's avatar
sangshuduo 已提交
259 260 261
#ifdef CUS_PROMPT
  const char *filepath = "/usr/local/"CUS_PROMPT"/email";
#else
wafwerar's avatar
wafwerar 已提交
262
  const char *filepath = "/usr/local/taos/email";
sangshuduo's avatar
sangshuduo 已提交
263
#endif  // CUS_PROMPT
wafwerar's avatar
wafwerar 已提交
264 265 266 267 268 269

  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ);
  if (pFile == NULL) return false;

  if (taosReadFile(pFile, (void *)email, maxLen) < 0) {
    taosCloseFile(&pFile);
S
Shengliang Guan 已提交
270
    return -1;
S
Shengliang Guan 已提交
271 272
  }

wafwerar's avatar
wafwerar 已提交
273 274
  taosCloseFile(&pFile);
  return 0;
sangshuduo's avatar
sangshuduo 已提交
275 276 277
#else
#ifdef CUS_PROMPT
  const char *filepath = "/usr/local/"CUS_PROMPT"/email";
wafwerar's avatar
wafwerar 已提交
278 279
#else
  const char *filepath = "/usr/local/taos/email";
sangshuduo's avatar
sangshuduo 已提交
280
#endif  // CUS_PROMPT
wafwerar's avatar
wafwerar 已提交
281 282 283 284 285

  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ);
  if (pFile == NULL) return false;

  if (taosReadFile(pFile, (void *)email, maxLen) < 0) {
286
    taosCloseFile(&pFile);
S
Shengliang Guan 已提交
287
    return -1;
S
Shengliang Guan 已提交
288 289
  }

wafwerar's avatar
wafwerar 已提交
290 291 292 293 294
  taosCloseFile(&pFile);
  return 0;
#endif
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
#ifdef WINDOWS
bool getWinVersionReleaseName(char *releaseName, int32_t maxLen) {
  TCHAR          szFileName[MAX_PATH];
  DWORD             dwHandle;
  DWORD             dwLen;
  LPVOID            lpData;
  UINT              uLen;
  VS_FIXEDFILEINFO *pFileInfo;

  GetWindowsDirectory(szFileName, MAX_PATH);
  wsprintf(szFileName, L"%s%s", szFileName, L"\\explorer.exe");
  dwLen = GetFileVersionInfoSize(szFileName, &dwHandle);
  if (dwLen == 0) {
    return false;
  }
F
facetosea 已提交
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  lpData = malloc(dwLen);
  if (lpData == NULL) return false;
  if (!GetFileVersionInfo(szFileName, dwHandle, dwLen, lpData)) {
    free(lpData);
    return false;
  }

  if (!VerQueryValue(lpData, L"\\", (LPVOID *)&pFileInfo, &uLen)) {
    free(lpData);
    return false;
  }

  snprintf(releaseName, maxLen, "Windows %d.%d", HIWORD(pFileInfo->dwProductVersionMS),
           LOWORD(pFileInfo->dwProductVersionMS));
  free(lpData);
  return true;
}
#endif
F
facetosea 已提交
329

K
kailixu 已提交
330
int32_t taosGetOsReleaseName(char *releaseName, char* sName, char* ver, int32_t maxLen) {
wafwerar's avatar
wafwerar 已提交
331
#ifdef WINDOWS
332 333 334
  if (!getWinVersionReleaseName(releaseName, maxLen)) {
    snprintf(releaseName, maxLen, "Windows");
  }
K
kailixu 已提交
335
  if(sName) snprintf(sName, maxLen, "Windows");
wafwerar's avatar
wafwerar 已提交
336
  return 0;
wafwerar's avatar
wafwerar 已提交
337
#elif defined(_TD_DARWIN_64)
F
facetosea 已提交
338 339 340
  char osversion[32];
  size_t osversion_len = sizeof(osversion) - 1;
  int osversion_name[] = { CTL_KERN, KERN_OSRELEASE };
wafwerar's avatar
wafwerar 已提交
341

K
kailixu 已提交
342
  if(sName) snprintf(sName, maxLen, "macOS");
F
facetosea 已提交
343 344 345
  if (sysctl(osversion_name, 2, osversion, &osversion_len, NULL, 0) == -1) {
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
346

F
facetosea 已提交
347 348 349 350 351 352
  uint32_t major, minor;
  if (sscanf(osversion, "%u.%u", &major, &minor) != 2) {
      return -1;
  }
  if (major >= 20) {
      major -= 9; // macOS 11 and newer
F
facetosea 已提交
353
      sprintf(releaseName, "macOS %u.%u", major, minor);
F
facetosea 已提交
354 355
  } else {
      major -= 4; // macOS 10.1.1 and newer
F
facetosea 已提交
356
      sprintf(releaseName, "macOS 10.%d.%d", major, minor);
S
Shengliang Guan 已提交
357 358
  }

F
facetosea 已提交
359
  return 0;
wafwerar's avatar
wafwerar 已提交
360
#else
wafwerar's avatar
wafwerar 已提交
361
  char    line[1024];
K
kailixu 已提交
362
  char   *dest = NULL;
wafwerar's avatar
wafwerar 已提交
363 364
  size_t  size = 0;
  int32_t code = -1;
K
kailixu 已提交
365
  int32_t cnt = 0;
wafwerar's avatar
wafwerar 已提交
366 367

  TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM);
K
kailixu 已提交
368
  if (pFile == NULL) return code;
wafwerar's avatar
wafwerar 已提交
369

wafwerar's avatar
wafwerar 已提交
370
  while ((size = taosGetsFile(pFile, sizeof(line), line)) != -1) {
wafwerar's avatar
wafwerar 已提交
371
    line[size - 1] = '\0';
K
kailixu 已提交
372 373 374 375
    if (strncmp(line, "NAME", 4) == 0) {
      dest = sName;
    } else if (strncmp(line, "PRETTY_NAME", 11) == 0) {
      dest = releaseName;
K
kailixu 已提交
376
      code = 0;
K
kailixu 已提交
377 378 379 380 381
    } else if (strncmp(line, "VERSION_ID", 10) == 0) {
      dest = ver;
    } else {
      continue;
    }
K
kailixu 已提交
382
    if (!dest) continue;
K
kailixu 已提交
383 384 385 386 387 388 389
    const char *p = strchr(line, '=') + 1;
    if (*p == '"') {
      p++;
      line[size - 2] = 0;
    }
    tstrncpy(dest, p, maxLen);

K
kailixu 已提交
390
    if (++cnt >= 3) break;
wafwerar's avatar
wafwerar 已提交
391 392 393 394 395 396 397 398
  }

  taosCloseFile(&pFile);
  return code;
#endif
}

int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) {
wafwerar's avatar
wafwerar 已提交
399
#ifdef WINDOWS
H
Hongze Cheng 已提交
400
  char  value[100];
wafwerar's avatar
wafwerar 已提交
401
  DWORD bufferSize = sizeof(value);
H
Hongze Cheng 已提交
402 403
  RegGetValue(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "ProcessorNameString",
              RRF_RT_ANY, NULL, (PVOID)&value, &bufferSize);
wafwerar's avatar
wafwerar 已提交
404 405
  tstrncpy(cpuModel, value, maxLen);
  SYSTEM_INFO si;
H
Hongze Cheng 已提交
406
  memset(&si, 0, sizeof(SYSTEM_INFO));
wafwerar's avatar
wafwerar 已提交
407 408 409
  GetSystemInfo(&si);
  *numOfCores = si.dwNumberOfProcessors;
  return 0;
wafwerar's avatar
wafwerar 已提交
410
#elif defined(_TD_DARWIN_64)
wafwerar's avatar
wafwerar 已提交
411
  char    buf[16];
wafwerar's avatar
wafwerar 已提交
412 413 414
  int32_t done = 0;
  int32_t code = -1;

wafwerar's avatar
wafwerar 已提交
415 416 417 418 419
  TdCmdPtr pCmd = taosOpenCmd("sysctl -n machdep.cpu.brand_string");
  if (pCmd == NULL) return code;
  if (taosGetsCmd(pCmd, maxLen, cpuModel) > 0) {
    code = 0;
    done |= 1;
wafwerar's avatar
wafwerar 已提交
420
  }
421 422 423 424
  int endPos = strlen(cpuModel)-1;
  if (cpuModel[endPos] == '\n') {
    cpuModel[endPos] = '\0';
  }
wafwerar's avatar
wafwerar 已提交
425 426 427 428 429
  taosCloseCmd(&pCmd);

  pCmd = taosOpenCmd("sysctl -n machdep.cpu.core_count");
  if (pCmd == NULL) return code;
  memset(buf, 0, sizeof(buf));
wafwerar's avatar
wafwerar 已提交
430
  if (taosGetsCmd(pCmd, sizeof(buf) - 1, buf) > 0) {
wafwerar's avatar
wafwerar 已提交
431 432 433 434 435
    code = 0;
    done |= 2;
    *numOfCores = atof(buf);
  }
  taosCloseCmd(&pCmd);
wafwerar's avatar
wafwerar 已提交
436 437 438

  return code;
#else
H
Haojun Liao 已提交
439
  char    line[1024] = {0};
wafwerar's avatar
wafwerar 已提交
440 441 442
  size_t  size = 0;
  int32_t done = 0;
  int32_t code = -1;
wafwerar's avatar
wafwerar 已提交
443
  float   coreCount = 0;
wafwerar's avatar
wafwerar 已提交
444 445

  TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM);
wafwerar's avatar
wafwerar 已提交
446
  if (pFile == NULL) return code;
wafwerar's avatar
wafwerar 已提交
447

wafwerar's avatar
wafwerar 已提交
448
  while (done != 3 && (size = taosGetsFile(pFile, sizeof(line), line)) != -1) {
wafwerar's avatar
wafwerar 已提交
449 450 451 452 453 454 455 456 457 458 459
    line[size - 1] = '\0';
    if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) {
      const char *v = strchr(line, ':') + 2;
      tstrncpy(cpuModel, v, maxLen);
      code = 0;
      done |= 1;
    } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) {
      const char *v = strchr(line, ':') + 2;
      *numOfCores = atof(v);
      done |= 2;
    }
wafwerar's avatar
wafwerar 已提交
460
    if (strncmp(line, "processor", 9) == 0) coreCount += 1;
wafwerar's avatar
wafwerar 已提交
461 462 463 464
  }

  taosCloseFile(&pFile);

wafwerar's avatar
wafwerar 已提交
465
  if (code != 0 && (done & 1) == 0) {
wafwerar's avatar
wafwerar 已提交
466
    TdFilePtr pFile1 = taosOpenFile("/proc/device-tree/model", TD_FILE_READ | TD_FILE_STREAM);
467 468 469 470 471 472 473 474
    if (pFile1 != NULL) {
      ssize_t bytes = taosGetsFile(pFile1, maxLen, cpuModel);
      taosCloseFile(&pFile);
      if (bytes > 0) {
        code = 0;
        done |= 1;
      }
    }
wafwerar's avatar
wafwerar 已提交
475 476
  }

wafwerar's avatar
wafwerar 已提交
477 478 479 480 481 482 483 484 485 486
  if (code != 0 && (done & 1) == 0) {
    TdCmdPtr pCmd = taosOpenCmd("uname -a");
    if (pCmd == NULL) return code;
    if (taosGetsCmd(pCmd, maxLen, cpuModel) > 0) {
      code = 0;
      done |= 1;
    }
    taosCloseCmd(&pCmd);
  }

wafwerar's avatar
wafwerar 已提交
487
  if ((done & 2) == 0) {
H
Hongze Cheng 已提交
488 489
    *numOfCores = coreCount;
    done |= 2;
wafwerar's avatar
wafwerar 已提交
490
  }
H
Hongze Cheng 已提交
491

wafwerar's avatar
wafwerar 已提交
492 493
  return code;
#endif
S
Shengliang Guan 已提交
494 495
}

S
Shengliang Guan 已提交
496
int32_t taosGetCpuCores(float *numOfCores) {
wafwerar's avatar
wafwerar 已提交
497
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
498 499 500 501 502
  SYSTEM_INFO info;
  GetSystemInfo(&info);
  *numOfCores = info.dwNumberOfProcessors;
  return 0;
#elif defined(_TD_DARWIN_64)
S
Shengliang Guan 已提交
503 504
  *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
  return 0;
wafwerar's avatar
wafwerar 已提交
505 506 507 508
#else
  *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
  return 0;
#endif
S
Shengliang Guan 已提交
509
}
S
Shengliang Guan 已提交
510

511 512 513 514 515 516 517 518
void taosGetCpuUsage(double *cpu_system, double *cpu_engine) {
  static int64_t lastSysUsed = 0;
  static int64_t lastSysTotal = 0;
  static int64_t lastProcTotal = 0;
  static int64_t curSysUsed = 0;
  static int64_t curSysTotal = 0;
  static int64_t curProcTotal = 0;

S
Shengliang Guan 已提交
519 520
  if (cpu_system != NULL) *cpu_system = 0;
  if (cpu_engine != NULL) *cpu_engine = 0;
S
Shengliang Guan 已提交
521

522 523 524 525 526 527
  SysCpuInfo  sysCpu = {0};
  ProcCpuInfo procCpu = {0};
  if (taosGetSysCpuInfo(&sysCpu) == 0 && taosGetProcCpuInfo(&procCpu) == 0) {
    curSysUsed = sysCpu.user + sysCpu.nice + sysCpu.system;
    curSysTotal = curSysUsed + sysCpu.idle;
    curProcTotal = procCpu.utime + procCpu.stime + procCpu.cutime + procCpu.cstime;
S
Shengliang Guan 已提交
528

529
    if (curSysTotal - lastSysTotal > 0 && curSysUsed >= lastSysUsed && curProcTotal >= lastProcTotal) {
S
Shengliang Guan 已提交
530 531 532 533 534 535
      if (cpu_system != NULL) {
        *cpu_system = (curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100;
      }
      if (cpu_engine != NULL) {
        *cpu_engine = (curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100;
      }
536
    }
S
Shengliang Guan 已提交
537

538 539 540
    lastSysUsed = curSysUsed;
    lastSysTotal = curSysTotal;
    lastProcTotal = curProcTotal;
S
Shengliang Guan 已提交
541 542 543
  }
}

H
Haojun Liao 已提交
544 545 546 547 548 549 550 551 552 553 554 555
#define __cpuid_fix(level, a, b, c, d) \
              __asm__("xor %%ecx, %%ecx\n" \
                      "cpuid\n" \
                      : "=a"(a), "=b"(b), "=c"(c), "=d"(d) \
                      : "0"(level))

// todo add for windows and mac
int32_t taosGetCpuInstructions(char* sse42, char* avx, char* avx2, char* fma) {
#ifdef WINDOWS
#elif defined(_TD_DARWIN_64)
#else

sangshuduo's avatar
sangshuduo 已提交
556
#ifdef _TD_X86_
H
Haojun Liao 已提交
557 558
  // Since the compiler is not support avx/avx2 instructions, the global variables always need to be
  // set to be false
H
Haojun Liao 已提交
559 560 561 562 563
//#if __AVX__ || __AVX2__
//  tsSIMDBuiltins = true;
//#else
//  tsSIMDBuiltins = false;
//#endif
H
Haojun Liao 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

  uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;

  int32_t ret = __get_cpuid(1, &eax, &ebx, &ecx, &edx);
  if (ret == 0) {
    return -1;  // failed to get the cpuid info
  }

  *sse42 = (char) ((ecx & bit_SSE4_2) == bit_SSE4_2);
  *avx   = (char) ((ecx & bit_AVX) == bit_AVX);
  *fma   = (char) ((ecx & bit_FMA) == bit_FMA);

  // work around a bug in GCC.
  // Ref to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77756
  __cpuid_fix(7u, eax, ebx, ecx, edx);
  *avx2 = (char) ((ebx & bit_AVX2) == bit_AVX2);
sangshuduo's avatar
sangshuduo 已提交
580
#endif   // _TD_X86_
H
Haojun Liao 已提交
581
#endif
H
Haojun Liao 已提交
582 583

  return 0;
H
Haojun Liao 已提交
584 585
}

wafwerar's avatar
wafwerar 已提交
586
int32_t taosGetTotalMemory(int64_t *totalKB) {
wafwerar's avatar
wafwerar 已提交
587
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
588 589 590
  MEMORYSTATUSEX memsStat;
  memsStat.dwLength = sizeof(memsStat);
  if (!GlobalMemoryStatusEx(&memsStat)) {
S
Shengliang Guan 已提交
591 592
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
593 594 595 596 597 598 599 600 601

  *totalKB = memsStat.ullTotalPhys / 1024;
  return 0;
#elif defined(_TD_DARWIN_64)
  return 0;
#else
  *totalKB = (int64_t)(sysconf(_SC_PHYS_PAGES) * tsPageSizeKB);
  return 0;
#endif
S
Shengliang Guan 已提交
602 603
}

wafwerar's avatar
wafwerar 已提交
604
int32_t taosGetProcMemory(int64_t *usedKB) {
wafwerar's avatar
wafwerar 已提交
605
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
  unsigned bytes_used = 0;

#if defined(_WIN64) && 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

  *usedKB = bytes_used / 1024;
  return 0;
#elif defined(_TD_DARWIN_64)
  *usedKB = 0;
  return 0;
#else
  TdFilePtr pFile = taosOpenFile(tsProcMemFile, TD_FILE_READ | TD_FILE_STREAM);
  if (pFile == NULL) {
    // printf("open file:%s failed", tsProcMemFile);
    return -1;
  }
S
Shengliang Guan 已提交
628

H
Haojun Liao 已提交
629 630
  ssize_t bytes = 0;
  char    line[1024] = {0};
631
  while (!taosEOFFile(pFile)) {
H
Haojun Liao 已提交
632 633
    bytes = taosGetsFile(pFile, sizeof(line), line);
    if (bytes < 0) {
S
Shengliang Guan 已提交
634 635
      break;
    }
wafwerar's avatar
wafwerar 已提交
636 637
    if (strstr(line, "VmRSS:") != NULL) {
      break;
S
Shengliang Guan 已提交
638
    }
wafwerar's avatar
wafwerar 已提交
639
  }
S
Shengliang Guan 已提交
640

wafwerar's avatar
wafwerar 已提交
641 642 643
  char tmp[10];
  sscanf(line, "%s %" PRId64, tmp, usedKB);

644
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
645 646 647 648 649
  return 0;
#endif
}

int32_t taosGetSysMemory(int64_t *usedKB) {
wafwerar's avatar
wafwerar 已提交
650
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
651 652 653 654 655
  MEMORYSTATUSEX memsStat;
  memsStat.dwLength = sizeof(memsStat);
  if (!GlobalMemoryStatusEx(&memsStat)) {
    return -1;
  }
S
Shengliang Guan 已提交
656

wafwerar's avatar
wafwerar 已提交
657 658 659 660 661 662 663 664 665 666
  int64_t nMemFree = memsStat.ullAvailPhys / 1024;
  int64_t nMemTotal = memsStat.ullTotalPhys / 1024.0;

  *usedKB = nMemTotal - nMemFree;
  return 0;
#elif defined(_TD_DARWIN_64)
  *usedKB = 0;
  return 0;
#else
  *usedKB = sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB;
S
monitor  
Shengliang Guan 已提交
667
  return 0;
wafwerar's avatar
wafwerar 已提交
668 669 670 671
#endif
}

int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) {
672
#if defined(WINDOWS)
wafwerar's avatar
wafwerar 已提交
673 674 675 676 677 678 679
  unsigned _int64 i64FreeBytesToCaller;
  unsigned _int64 i64TotalBytes;
  unsigned _int64 i64FreeBytes;

  BOOL fResult = GetDiskFreeSpaceExA(dataDir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes,
                                     (PULARGE_INTEGER)&i64FreeBytes);
  if (fResult) {
680
    diskSize->total = (int64_t)(i64TotalBytes);
wafwerar's avatar
wafwerar 已提交
681 682 683 684 685
    diskSize->avail = (int64_t)(i64FreeBytesToCaller);
    diskSize->used = (int64_t)(i64TotalBytes - i64FreeBytes);
    return 0;
  } else {
    // printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno));
S
Shengliang Guan 已提交
686
    // terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
687 688 689 690 691 692
    return -1;
  }
#elif defined(_TD_DARWIN_64)
  struct statvfs info;
  if (statvfs(dataDir, &info)) {
    // printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno));
S
Shengliang Guan 已提交
693
    // terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
694 695
    return -1;
  } else {
696
    diskSize->total = info.f_blocks * info.f_frsize;
wafwerar's avatar
wafwerar 已提交
697 698 699 700 701 702 703
    diskSize->avail = info.f_bavail * info.f_frsize;
    diskSize->used = (info.f_blocks - info.f_bfree) * info.f_frsize;
    return 0;
  }
#else
  struct statvfs info;
  if (statvfs(dataDir, &info)) {
S
Shengliang Guan 已提交
704
    // terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
705 706 707 708 709 710 711 712
    return -1;
  } else {
    diskSize->total = info.f_blocks * info.f_frsize;
    diskSize->avail = info.f_bavail * info.f_frsize;
    diskSize->used = diskSize->total - diskSize->avail;
    return 0;
  }
#endif
S
Shengliang Guan 已提交
713 714
}

S
monitor  
Shengliang Guan 已提交
715
int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) {
wafwerar's avatar
wafwerar 已提交
716
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
  IO_COUNTERS io_counter;
  if (GetProcessIoCounters(GetCurrentProcess(), &io_counter)) {
    if (rchars) *rchars = io_counter.ReadTransferCount;
    if (wchars) *wchars = io_counter.WriteTransferCount;
    if (read_bytes) *read_bytes = 0;
    if (write_bytes) *write_bytes = 0;
    return 0;
  }
  return -1;
#elif defined(_TD_DARWIN_64)
  if (rchars) *rchars = 0;
  if (wchars) *wchars = 0;
  if (read_bytes) *read_bytes = 0;
  if (write_bytes) *write_bytes = 0;
  return 0;
#else
733
  TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM);
S
Shengliang Guan 已提交
734
  if (pFile == NULL) return -1;
S
Shengliang Guan 已提交
735

H
Haojun Liao 已提交
736 737
  ssize_t bytes = 0;
  char    line[1024] = {0};
S
Shengliang Guan 已提交
738
  char    tmp[24];
S
Shengliang Guan 已提交
739 740
  int     readIndex = 0;

741
  while (!taosEOFFile(pFile)) {
H
Haojun Liao 已提交
742 743
    bytes = taosGetsFile(pFile, sizeof(line), line);
    if (bytes < 10) {
S
Shengliang Guan 已提交
744 745 746 747 748 749 750 751
      break;
    }
    if (strstr(line, "rchar:") != NULL) {
      sscanf(line, "%s %" PRId64, tmp, rchars);
      readIndex++;
    } else if (strstr(line, "wchar:") != NULL) {
      sscanf(line, "%s %" PRId64, tmp, wchars);
      readIndex++;
S
Shengliang Guan 已提交
752
    } else if (strstr(line, "read_bytes:") != NULL) {  // read_bytes
S
Shengliang Guan 已提交
753 754
      sscanf(line, "%s %" PRId64, tmp, read_bytes);
      readIndex++;
S
Shengliang Guan 已提交
755
    } else if (strstr(line, "write_bytes:") != NULL) {  // write_bytes
S
Shengliang Guan 已提交
756 757
      sscanf(line, "%s %" PRId64, tmp, write_bytes);
      readIndex++;
S
Shengliang Guan 已提交
758
    } else {
S
Shengliang Guan 已提交
759 760
    }

wafwerar's avatar
wafwerar 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773
    if (readIndex >= 4) break;
  }

  taosCloseFile(&pFile);

  if (readIndex < 4) {
    return -1;
  }

  return 0;
#endif
}

774 775 776 777 778 779 780 781 782
void taosGetProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) {
  static int64_t last_rchars = 0;
  static int64_t last_wchars = 0;
  static int64_t last_read_bytes = 0;
  static int64_t last_write_bytes = 0;
  static int64_t cur_rchars = 0;
  static int64_t cur_wchars = 0;
  static int64_t cur_read_bytes = 0;
  static int64_t cur_write_bytes = 0;
783
  if (taosGetProcIO(&cur_rchars, &cur_wchars, &cur_read_bytes, &cur_write_bytes) == 0) {
784 785 786 787 788 789 790 791
    *rchars = cur_rchars - last_rchars;
    *wchars = cur_wchars - last_wchars;
    *read_bytes = cur_read_bytes - last_read_bytes;
    *write_bytes = cur_write_bytes - last_write_bytes;
    last_rchars = cur_rchars;
    last_wchars = cur_wchars;
    last_read_bytes = cur_read_bytes;
    last_write_bytes = cur_write_bytes;
792 793 794 795 796
  } else {
    *rchars = 0;
    *wchars = 0;
    *read_bytes = 0;
    *write_bytes = 0;
797 798 799
  }
}

wafwerar's avatar
wafwerar 已提交
800
int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) {
wafwerar's avatar
wafwerar 已提交
801
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
802 803 804 805 806 807 808 809 810 811 812 813
  *receive_bytes = 0;
  *transmit_bytes = 0;
  return 0;
#elif defined(_TD_DARWIN_64)
  *receive_bytes = 0;
  *transmit_bytes = 0;
  return 0;
#else
  TdFilePtr pFile = taosOpenFile(tsSysNetFile, TD_FILE_READ | TD_FILE_STREAM);
  if (pFile == NULL) return -1;

  ssize_t _bytes = 0;
H
Hongze Cheng 已提交
814
  char    line[1024];
wafwerar's avatar
wafwerar 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828

  while (!taosEOFFile(pFile)) {
    int64_t o_rbytes = 0;
    int64_t rpackts = 0;
    int64_t o_tbytes = 0;
    int64_t tpackets = 0;
    int64_t nouse1 = 0;
    int64_t nouse2 = 0;
    int64_t nouse3 = 0;
    int64_t nouse4 = 0;
    int64_t nouse5 = 0;
    int64_t nouse6 = 0;
    char    nouse0[200] = {0};

wafwerar's avatar
wafwerar 已提交
829
    _bytes = taosGetsFile(pFile, sizeof(line), line);
wafwerar's avatar
wafwerar 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
    if (_bytes < 0) {
      break;
    }

    line[_bytes - 1] = 0;

    if (strstr(line, "lo:") != NULL) {
      continue;
    }

    sscanf(line,
           "%s %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64
           " %" PRId64,
           nouse0, &o_rbytes, &rpackts, &nouse1, &nouse2, &nouse3, &nouse4, &nouse5, &nouse6, &o_tbytes, &tpackets);
    *receive_bytes = o_rbytes;
    *transmit_bytes = o_tbytes;
S
Shengliang Guan 已提交
846 847
  }

848
  taosCloseFile(&pFile);
S
Shengliang Guan 已提交
849

S
io  
Shengliang Guan 已提交
850
  return 0;
wafwerar's avatar
wafwerar 已提交
851
#endif
S
Shengliang Guan 已提交
852 853
}

854 855 856 857 858
void taosGetCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes) {
  static int64_t last_receive_bytes = 0;
  static int64_t last_transmit_bytes = 0;
  static int64_t cur_receive_bytes = 0;
  static int64_t cur_transmit_bytes = 0;
859
  if (taosGetCardInfo(&cur_receive_bytes, &cur_transmit_bytes) == 0) {
860 861 862 863
    *receive_bytes = cur_receive_bytes - last_receive_bytes;
    *transmit_bytes = cur_transmit_bytes - last_transmit_bytes;
    last_receive_bytes = cur_receive_bytes;
    last_transmit_bytes = cur_transmit_bytes;
864 865 866
  } else {
    *receive_bytes = 0;
    *transmit_bytes = 0;
867 868 869
  }
}

wafwerar's avatar
wafwerar 已提交
870
void taosKillSystem() {
wafwerar's avatar
wafwerar 已提交
871
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884
  printf("function taosKillSystem, exit!");
  exit(0);
#elif defined(_TD_DARWIN_64)
  printf("function taosKillSystem, exit!");
  exit(0);
#else
  // SIGINT
  printf("taosd will shut down soon");
  kill(tsProcId, 2);
#endif
}

int32_t taosGetSystemUUID(char *uid, int32_t uidlen) {
wafwerar's avatar
wafwerar 已提交
885
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
886 887
  GUID guid;
  CoCreateGuid(&guid);
H
Hongze Cheng 已提交
888 889 890
  snprintf(uid, uidlen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", guid.Data1, guid.Data2, guid.Data3,
           guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6],
           guid.Data4[7]);
wafwerar's avatar
wafwerar 已提交
891 892 893 894

  return 0;
#elif defined(_TD_DARWIN_64)
  uuid_t uuid = {0};
895 896
  char   buf[UUIDLEN37];
  memset(buf, 0, UUIDLEN37);
wafwerar's avatar
wafwerar 已提交
897 898
  uuid_generate(uuid);
  // it's caller's responsibility to make enough space for `uid`, that's 36-char + 1-null
wafwerar's avatar
wafwerar 已提交
899
  uuid_unparse_lower(uuid, buf);
900 901 902 903 904
  int n = snprintf(uid, uidlen, "%.*s", (int)sizeof(buf), buf);  // though less performance, much safer
  if (n >= uidlen) {
    // target buffer is too small
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
  return 0;
#else
  int len = 0;

  // fd = open("/proc/sys/kernel/random/uuid", 0);
  TdFilePtr pFile = taosOpenFile("/proc/sys/kernel/random/uuid", TD_FILE_READ);
  if (pFile == NULL) {
    return -1;
  } else {
    len = taosReadFile(pFile, uid, uidlen);
    taosCloseFile(&pFile);
  }

  if (len >= 36) {
    uid[36] = 0;
    return 0;
  }

  return 0;
#endif
}

char *taosGetCmdlineByPID(int pid) {
wafwerar's avatar
wafwerar 已提交
928
#ifdef WINDOWS
X
xinsheng Ren 已提交
929
  ASSERT(0);
wafwerar's avatar
wafwerar 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
  return "";
#elif defined(_TD_DARWIN_64)
  static char cmdline[1024];
  errno = 0;

  if (proc_pidpath(pid, cmdline, sizeof(cmdline)) <= 0) {
    fprintf(stderr, "PID is %d, %s", pid, strerror(errno));
    return strerror(errno);
  }

  return cmdline;
#else
  static char cmdline[1024];
  sprintf(cmdline, "/proc/%d/cmdline", pid);

  // int fd = open(cmdline, O_RDONLY);
  TdFilePtr pFile = taosOpenFile(cmdline, TD_FILE_READ);
  if (pFile != NULL) {
    int n = taosReadFile(pFile, cmdline, sizeof(cmdline) - 1);
    if (n < 0) n = 0;

    if (n > 0 && cmdline[n - 1] == '\n') --n;

    cmdline[n] = 0;

    taosCloseFile(&pFile);
  } else {
    cmdline[0] = 0;
  }

  return cmdline;
#endif
S
Shengliang Guan 已提交
962 963
}

K
kailixu 已提交
964 965
int64_t taosGetOsUptime() {
#ifdef WINDOWS
K
kailixu 已提交
966
#elif defined(_TD_DARWIN_64)
K
kailixu 已提交
967 968 969 970 971 972
#else
  struct sysinfo info;
  if (0 == sysinfo(&info)) {
    return info.uptime;
  };
#endif
K
kailixu 已提交
973
  return 0;
K
kailixu 已提交
974 975
}

S
Shengliang Guan 已提交
976
void taosSetCoreDump(bool enable) {
wafwerar's avatar
wafwerar 已提交
977
  if (!enable) return;
wafwerar's avatar
wafwerar 已提交
978
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
979 980
  SetUnhandledExceptionFilter(exceptionHandler);
  SetUnhandledExceptionFilter(&FlCrashDump);
wafwerar's avatar
wafwerar 已提交
981 982
#elif defined(_TD_DARWIN_64)
#else
S
Shengliang Guan 已提交
983 984 985 986 987
  // 1. set ulimit -c unlimited
  struct rlimit rlim;
  struct rlimit rlim_new;
  if (getrlimit(RLIMIT_CORE, &rlim) == 0) {
#ifndef _ALPINE
S
Shengliang Guan 已提交
988
    // printf("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max);
S
Shengliang Guan 已提交
989
#else
S
Shengliang Guan 已提交
990
    // printf("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max);
S
Shengliang Guan 已提交
991 992 993 994
#endif
    rlim_new.rlim_cur = RLIM_INFINITY;
    rlim_new.rlim_max = RLIM_INFINITY;
    if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) {
S
Shengliang Guan 已提交
995
      // printf("set unlimited fail, error: %s", strerror(errno));
S
Shengliang Guan 已提交
996 997 998 999 1000 1001 1002 1003
      rlim_new.rlim_cur = rlim.rlim_max;
      rlim_new.rlim_max = rlim.rlim_max;
      (void)setrlimit(RLIMIT_CORE, &rlim_new);
    }
  }

  if (getrlimit(RLIMIT_CORE, &rlim) == 0) {
#ifndef _ALPINE
S
Shengliang Guan 已提交
1004
    // printf("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max);
S
Shengliang Guan 已提交
1005
#else
S
Shengliang Guan 已提交
1006
    // printf("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max);
S
Shengliang Guan 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
#endif
  }

#ifndef _TD_ARM_
  // 2. set the path for saving core file
  struct __sysctl_args args;

  int    old_usespid = 0;
  size_t old_len = 0;
  int    new_usespid = 1;
  size_t new_len = sizeof(new_usespid);

  int name[] = {CTL_KERN, KERN_CORE_USES_PID};

  memset(&args, 0, sizeof(struct __sysctl_args));
  args.name = name;
  args.nlen = sizeof(name) / sizeof(name[0]);
  args.oldval = &old_usespid;
  args.oldlenp = &old_len;
  args.newval = &new_usespid;
  args.newlen = new_len;

  old_len = sizeof(old_usespid);

1031
#ifndef __loongarch64
S
Shengliang Guan 已提交
1032
  if (syscall(SYS__sysctl, &args) == -1) {
S
Shengliang Guan 已提交
1033
    // printf("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno));
S
Shengliang Guan 已提交
1034
  }
1035
#endif
S
Shengliang Guan 已提交
1036

S
Shengliang Guan 已提交
1037
  // printf("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid);
S
Shengliang Guan 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048

  old_usespid = 0;
  old_len = 0;
  memset(&args, 0, sizeof(struct __sysctl_args));
  args.name = name;
  args.nlen = sizeof(name) / sizeof(name[0]);
  args.oldval = &old_usespid;
  args.oldlenp = &old_len;

  old_len = sizeof(old_usespid);

1049
#ifndef __loongarch64
S
Shengliang Guan 已提交
1050
  if (syscall(SYS__sysctl, &args) == -1) {
S
Shengliang Guan 已提交
1051
    // printf("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno));
S
Shengliang Guan 已提交
1052
  }
1053
#endif
S
Shengliang Guan 已提交
1054

S
Shengliang Guan 已提交
1055
  // printf("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid);
S
Shengliang Guan 已提交
1056 1057
#endif
#endif
wafwerar's avatar
wafwerar 已提交
1058
}
S
Shengliang Guan 已提交
1059 1060

SysNameInfo taosGetSysNameInfo() {
wafwerar's avatar
wafwerar 已提交
1061
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
1062
  SysNameInfo info = {0};
H
Hongze Cheng 已提交
1063
  DWORD       dwVersion = GetVersion();
wafwerar's avatar
wafwerar 已提交
1064

wafwerar's avatar
wafwerar 已提交
1065 1066 1067 1068 1069
  char *tmp = NULL;
  tmp = getenv("OS");
  if (tmp != NULL) tstrncpy(info.sysname, tmp, sizeof(info.sysname));
  tmp = getenv("COMPUTERNAME");
  if (tmp != NULL) tstrncpy(info.nodename, tmp, sizeof(info.nodename));
wafwerar's avatar
wafwerar 已提交
1070 1071
  sprintf_s(info.release, sizeof(info.release), "%d", dwVersion & 0x0F);
  sprintf_s(info.version, sizeof(info.release), "%d", (dwVersion >> 8) & 0x0F);
wafwerar's avatar
wafwerar 已提交
1072 1073
  tmp = getenv("PROCESSOR_ARCHITECTURE");
  if (tmp != NULL) tstrncpy(info.machine, tmp, sizeof(info.machine));
wafwerar's avatar
wafwerar 已提交
1074 1075

  return info;
wafwerar's avatar
wafwerar 已提交
1076
#elif defined(_TD_DARWIN_64)
S
Shengliang Guan 已提交
1077 1078
  SysNameInfo info = {0};

1079 1080
  struct utsname uts;
  if (!uname(&uts)) {
H
Haojun Liao 已提交
1081 1082 1083 1084 1085
    tstrncpy(info.sysname, uts.sysname, sizeof(info.sysname));
    tstrncpy(info.nodename, uts.nodename, sizeof(info.nodename));
    tstrncpy(info.release, uts.release, sizeof(info.release));
    tstrncpy(info.version, uts.version, sizeof(info.version));
    tstrncpy(info.machine, uts.machine, sizeof(info.machine));
S
Shengliang Guan 已提交
1086 1087
  }

1088 1089 1090 1091 1092
  char     localHostName[512];
  taosGetlocalhostname(localHostName, 512);
  TdCmdPtr pCmd = taosOpenCmd("scutil --get LocalHostName");
  tstrncpy(info.nodename, localHostName, sizeof(info.nodename));

S
Shengliang Guan 已提交
1093
  return info;
wafwerar's avatar
wafwerar 已提交
1094 1095
#else
  SysNameInfo info = {0};
1096

wafwerar's avatar
wafwerar 已提交
1097 1098 1099 1100 1101 1102 1103
  struct utsname uts;
  if (!uname(&uts)) {
    tstrncpy(info.sysname, uts.sysname, sizeof(info.sysname));
    tstrncpy(info.nodename, uts.nodename, sizeof(info.nodename));
    tstrncpy(info.release, uts.release, sizeof(info.release));
    tstrncpy(info.version, uts.version, sizeof(info.version));
    tstrncpy(info.machine, uts.machine, sizeof(info.machine));
1104 1105
  }

wafwerar's avatar
wafwerar 已提交
1106
  return info;
1107
#endif
wafwerar's avatar
wafwerar 已提交
1108
}
wafwerar's avatar
wafwerar 已提交
1109 1110 1111 1112

bool taosCheckCurrentInDll() {
#ifdef WINDOWS
  MEMORY_BASIC_INFORMATION mbi;
H
Hongze Cheng 已提交
1113 1114 1115 1116
  char                     path[PATH_MAX] = {0};
  GetModuleFileName(
      ((VirtualQuery(taosCheckCurrentInDll, &mbi, sizeof(mbi)) != 0) ? (HMODULE)mbi.AllocationBase : NULL), path,
      PATH_MAX);
wafwerar's avatar
wafwerar 已提交
1117
  int strLastIndex = strlen(path);
H
Hongze Cheng 已提交
1118 1119 1120
  if ((path[strLastIndex - 3] == 'd' || path[strLastIndex - 3] == 'D') &&
      (path[strLastIndex - 2] == 'l' || path[strLastIndex - 2] == 'L') &&
      (path[strLastIndex - 1] == 'l' || path[strLastIndex - 1] == 'L')) {
wafwerar's avatar
wafwerar 已提交
1121 1122 1123 1124 1125 1126
    return true;
  }
  return false;
#else
  return false;
#endif
1127
}
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

#ifdef _TD_DARWIN_64
int taosGetMaclocalhostnameByCommand(char *hostname, size_t maxLen) {
  TdCmdPtr pCmd = taosOpenCmd("scutil --get LocalHostName");
  if (pCmd != NULL) {
    if (taosGetsCmd(pCmd, maxLen - 1, hostname) > 0) {
      int len = strlen(hostname);
      if (hostname[len - 1] == '\n') {
        hostname[len - 1] = '\0';
      }
      return 0;
    }
    taosCloseCmd(&pCmd);
  }
  return -1;
}

int getMacLocalHostNameBySCD(char *hostname, size_t maxLen) {
  SCDynamicStoreRef store = SCDynamicStoreCreate(NULL, CFSTR(""), NULL, NULL);
  CFStringRef       hostname_cfstr = SCDynamicStoreCopyLocalHostName(store);
  if (hostname_cfstr != NULL) {
    CFStringGetCString(hostname_cfstr, hostname, maxLen - 1, kCFStringEncodingMacRoman);
    CFRelease(hostname_cfstr);
  } else {
    return -1;
  }
  CFRelease(store);
  return 0;
}
#endif

int taosGetlocalhostname(char *hostname, size_t maxLen) {
#ifdef _TD_DARWIN_64
  int res = getMacLocalHostNameBySCD(hostname, maxLen);
  if (res != 0) {
    return taosGetMaclocalhostnameByCommand(hostname, maxLen);
  } else {
    return 0;
  }
#else
  return gethostname(hostname, maxLen);
#endif
}