osSysinfo.c 24.6 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

wafwerar's avatar
wafwerar 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#define PROCESS_ITEM 12

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 已提交
37 38 39 40 41 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 99 100 101 102 103 104 105 106 107 108

/*
 * 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;
}

#elif defined(_TD_DARWIN_64)

#include <errno.h>
#include <libproc.h>

#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 已提交
109
#include <unistd.h>
S
Shengliang Guan 已提交
110 111 112 113 114 115 116 117

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 已提交
118
static void taosGetProcIOnfos() {
S
Shengliang Guan 已提交
119
  tsPageSizeKB = sysconf(_SC_PAGESIZE) / 1024;
S
Shengliang Guan 已提交
120 121 122 123
  tsOpenMax = sysconf(_SC_OPEN_MAX);
  tsStreamMax = sysconf(_SC_STREAM_MAX);
  tsProcId = (pid_t)syscall(SYS_gettid);

S
Shengliang Guan 已提交
124 125 126
  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 已提交
127
}
wafwerar's avatar
wafwerar 已提交
128
#endif
S
Shengliang Guan 已提交
129

wafwerar's avatar
wafwerar 已提交
130
static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) {
wafwerar's avatar
wafwerar 已提交
131
#ifdef WINDOWS
132 133 134 135 136 137 138 139 140 141 142 143 144
  FILETIME pre_idleTime = {0};
  FILETIME pre_kernelTime = {0};
  FILETIME pre_userTime = {0};
  FILETIME idleTime;
  FILETIME kernelTime;
  FILETIME userTime;
  bool res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
  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 已提交
145
#elif defined(_TD_DARWIN_64)
146
  assert(0);
wafwerar's avatar
wafwerar 已提交
147
#else
wafwerar's avatar
wafwerar 已提交
148
  TdFilePtr pFile = taosOpenFile(tsSysCpuFile, TD_FILE_READ | TD_FILE_STREAM);
149
  if (pFile == NULL) {
S
Shengliang Guan 已提交
150
    return -1;
S
Shengliang Guan 已提交
151 152
  }

S
Shengliang Guan 已提交
153
  char   *line = NULL;
wafwerar's avatar
wafwerar 已提交
154 155
  ssize_t _bytes = taosGetLineFile(pFile, &line);
  if ((_bytes < 0) || (line == NULL)) {
156
    taosCloseFile(&pFile);
S
Shengliang Guan 已提交
157
    return -1;
S
Shengliang Guan 已提交
158 159
  }

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

wafwerar's avatar
wafwerar 已提交
164
  if (line != NULL) taosMemoryFreeClear(line);
165
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
166
#endif
S
Shengliang Guan 已提交
167
  return 0;
S
Shengliang Guan 已提交
168 169
}

wafwerar's avatar
wafwerar 已提交
170
static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) {
wafwerar's avatar
wafwerar 已提交
171
#ifdef WINDOWS
172 173 174 175 176 177 178 179 180 181
  FILETIME pre_krnlTm = {0};
  FILETIME pre_usrTm = {0};
	FILETIME creatTm, exitTm, krnlTm, usrTm;

	if (GetThreadTimes(GetCurrentThread(), &creatTm, &exitTm, &krnlTm, &usrTm)) {
    cpuInfo->stime = CompareFileTime(&pre_krnlTm, &krnlTm);
    cpuInfo->utime = CompareFileTime(&pre_usrTm, &usrTm);
    cpuInfo->cutime = 0;
    cpuInfo->cstime = 0;
	}
wafwerar's avatar
wafwerar 已提交
182
#elif defined(_TD_DARWIN_64)
183
  assert(0);
wafwerar's avatar
wafwerar 已提交
184
#else
wafwerar's avatar
wafwerar 已提交
185
  TdFilePtr pFile = taosOpenFile(tsProcCpuFile, TD_FILE_READ | TD_FILE_STREAM);
186
  if (pFile == NULL) {
S
Shengliang Guan 已提交
187
    return -1;
S
Shengliang Guan 已提交
188 189
  }

S
Shengliang Guan 已提交
190
  char   *line = NULL;
191
  ssize_t _bytes = taosGetLineFile(pFile, &line);
S
Shengliang Guan 已提交
192
  if ((_bytes < 0) || (line == NULL)) {
193
    taosCloseFile(&pFile);
S
Shengliang Guan 已提交
194
    return -1;
S
Shengliang Guan 已提交
195 196
  }

wafwerar's avatar
wafwerar 已提交
197 198 199 200 201 202 203 204
  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 已提交
205

wafwerar's avatar
wafwerar 已提交
206
  if (line != NULL) taosMemoryFreeClear(line);
207
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
208
#endif
S
Shengliang Guan 已提交
209
  return 0;
S
Shengliang Guan 已提交
210 211
}

wafwerar's avatar
wafwerar 已提交
212 213

bool taosCheckSystemIsSmallEnd() {
214 215 216 217 218 219
  union check {
    int16_t i;
    char    ch[2];
  } c;
  c.i = 1;
  return c.ch[0] == 1;
wafwerar's avatar
wafwerar 已提交
220 221 222
}

void taosGetSystemInfo() {
wafwerar's avatar
wafwerar 已提交
223
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  taosGetCpuCores(&tsNumOfCores);
  taosGetTotalMemory(&tsTotalMemoryKB);

  double tmp1, tmp2, tmp3, tmp4;
  taosGetCpuUsage(&tmp1, &tmp2);
#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);

  double tmp1, tmp2, tmp3, tmp4;
  taosGetCpuUsage(&tmp1, &tmp2);
#endif
}

int32_t taosGetEmail(char *email, int32_t maxLen) {
wafwerar's avatar
wafwerar 已提交
246
#ifdef WINDOWS
247
  // assert(0);
wafwerar's avatar
wafwerar 已提交
248 249 250 251 252 253 254 255
#elif defined(_TD_DARWIN_64)
  const char *filepath = "/usr/local/taos/email";

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

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

wafwerar's avatar
wafwerar 已提交
259 260 261 262 263 264 265 266 267
  taosCloseFile(&pFile);
  return 0;
#else
  const char *filepath = "/usr/local/taos/email";

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

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

wafwerar's avatar
wafwerar 已提交
272 273 274 275 276 277
  taosCloseFile(&pFile);
  return 0;
#endif
}

int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen) {
wafwerar's avatar
wafwerar 已提交
278
#ifdef WINDOWS
279
  assert(0);
wafwerar's avatar
wafwerar 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
#elif defined(_TD_DARWIN_64)
  char   *line = NULL;
  size_t  size = 0;
  int32_t code = -1;

  TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM);
  if (pFile == NULL) return false;

  while ((size = taosGetLineFile(pFile, &line)) != -1) {
    line[size - 1] = '\0';
    if (strncmp(line, "PRETTY_NAME", 11) == 0) {
      const char *p = strchr(line, '=') + 1;
      if (*p == '"') {
        p++;
        line[size - 2] = 0;
      }
      tstrncpy(releaseName, p, maxLen);
      code = 0;
S
Shengliang Guan 已提交
298 299 300 301
      break;
    }
  }

wafwerar's avatar
wafwerar 已提交
302
  if (line != NULL) taosMemoryFree(line);
303
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  return code;
#else
  char   *line = NULL;
  size_t  size = 0;
  int32_t code = -1;

  TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM);
  if (pFile == NULL) return false;

  while ((size = taosGetLineFile(pFile, &line)) != -1) {
    line[size - 1] = '\0';
    if (strncmp(line, "PRETTY_NAME", 11) == 0) {
      const char *p = strchr(line, '=') + 1;
      if (*p == '"') {
        p++;
        line[size - 2] = 0;
      }
      tstrncpy(releaseName, p, maxLen);
      code = 0;
      break;
    }
  }

wafwerar's avatar
wafwerar 已提交
327
  if (line != NULL) taosMemoryFree(line);
wafwerar's avatar
wafwerar 已提交
328 329 330 331 332 333
  taosCloseFile(&pFile);
  return code;
#endif
}

int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) {
wafwerar's avatar
wafwerar 已提交
334
#ifdef WINDOWS
335
  assert(0);
wafwerar's avatar
wafwerar 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
#elif defined(_TD_DARWIN_64)
  char   *line = NULL;
  size_t  size = 0;
  int32_t done = 0;
  int32_t code = -1;

  TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM);
  if (pFile == NULL) return false;

  while (done != 3 && (size = taosGetLineFile(pFile, &line)) != -1) {
    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 已提交
359
  if (line != NULL) taosMemoryFree(line);
wafwerar's avatar
wafwerar 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  taosCloseFile(&pFile);

  return code;
#else
  char   *line = NULL;
  size_t  size = 0;
  int32_t done = 0;
  int32_t code = -1;

  TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM);
  if (pFile == NULL) return false;

  while (done != 3 && (size = taosGetLineFile(pFile, &line)) != -1) {
    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 已提交
386
  if (line != NULL) taosMemoryFree(line);
wafwerar's avatar
wafwerar 已提交
387 388 389 390
  taosCloseFile(&pFile);

  return code;
#endif
S
Shengliang Guan 已提交
391 392
}

S
Shengliang Guan 已提交
393
int32_t taosGetCpuCores(float *numOfCores) {
wafwerar's avatar
wafwerar 已提交
394
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
395 396 397 398 399
  SYSTEM_INFO info;
  GetSystemInfo(&info);
  *numOfCores = info.dwNumberOfProcessors;
  return 0;
#elif defined(_TD_DARWIN_64)
S
Shengliang Guan 已提交
400 401
  *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
  return 0;
wafwerar's avatar
wafwerar 已提交
402 403 404 405
#else
  *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
  return 0;
#endif
S
Shengliang Guan 已提交
406
}
S
Shengliang Guan 已提交
407

408 409 410 411 412 413 414 415
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;

416 417
  *cpu_system = 0;
  *cpu_engine = 0;
S
Shengliang Guan 已提交
418

419 420 421 422 423 424
  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 已提交
425

426 427 428 429
    if (curSysTotal > lastSysTotal && curSysUsed >= lastSysUsed && curProcTotal >= lastProcTotal) {
      *cpu_engine = (curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100;
      *cpu_system = (curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100;
    }
S
Shengliang Guan 已提交
430

431 432 433
    lastSysUsed = curSysUsed;
    lastSysTotal = curSysTotal;
    lastProcTotal = curProcTotal;
S
Shengliang Guan 已提交
434 435 436
  }
}

wafwerar's avatar
wafwerar 已提交
437
int32_t taosGetTotalMemory(int64_t *totalKB) {
wafwerar's avatar
wafwerar 已提交
438
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
439 440 441
  MEMORYSTATUSEX memsStat;
  memsStat.dwLength = sizeof(memsStat);
  if (!GlobalMemoryStatusEx(&memsStat)) {
S
Shengliang Guan 已提交
442 443
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
444 445 446 447 448 449 450 451 452

  *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 已提交
453 454
}

wafwerar's avatar
wafwerar 已提交
455
int32_t taosGetProcMemory(int64_t *usedKB) {
wafwerar's avatar
wafwerar 已提交
456
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
  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 已提交
479 480

  ssize_t _bytes = 0;
S
Shengliang Guan 已提交
481
  char   *line = NULL;
482 483
  while (!taosEOFFile(pFile)) {
    _bytes = taosGetLineFile(pFile, &line);
wafwerar's avatar
wafwerar 已提交
484
    if ((_bytes < 0) || (line == NULL)) {
S
Shengliang Guan 已提交
485 486
      break;
    }
wafwerar's avatar
wafwerar 已提交
487 488
    if (strstr(line, "VmRSS:") != NULL) {
      break;
S
Shengliang Guan 已提交
489
    }
wafwerar's avatar
wafwerar 已提交
490
  }
S
Shengliang Guan 已提交
491

wafwerar's avatar
wafwerar 已提交
492 493 494 495
  if (line == NULL) {
    // printf("read file:%s failed", tsProcMemFile);
    taosCloseFile(&pFile);
    return -1;
S
Shengliang Guan 已提交
496 497
  }

wafwerar's avatar
wafwerar 已提交
498 499 500
  char tmp[10];
  sscanf(line, "%s %" PRId64, tmp, usedKB);

wafwerar's avatar
wafwerar 已提交
501
  if (line != NULL) taosMemoryFreeClear(line);
502
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
503 504 505 506 507
  return 0;
#endif
}

int32_t taosGetSysMemory(int64_t *usedKB) {
wafwerar's avatar
wafwerar 已提交
508
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
509 510 511 512 513
  MEMORYSTATUSEX memsStat;
  memsStat.dwLength = sizeof(memsStat);
  if (!GlobalMemoryStatusEx(&memsStat)) {
    return -1;
  }
S
Shengliang Guan 已提交
514

wafwerar's avatar
wafwerar 已提交
515 516 517 518 519 520 521 522 523 524
  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 已提交
525
  return 0;
wafwerar's avatar
wafwerar 已提交
526 527 528 529
#endif
}

int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) {
530
#if defined(WINDOWS)
wafwerar's avatar
wafwerar 已提交
531 532 533 534 535 536 537
  unsigned _int64 i64FreeBytesToCaller;
  unsigned _int64 i64TotalBytes;
  unsigned _int64 i64FreeBytes;

  BOOL fResult = GetDiskFreeSpaceExA(dataDir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes,
                                     (PULARGE_INTEGER)&i64FreeBytes);
  if (fResult) {
538
    diskSize->total = (int64_t)(i64TotalBytes);
wafwerar's avatar
wafwerar 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    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));
    terrno = TAOS_SYSTEM_ERROR(errno);
    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));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  } else {
554
    diskSize->total = info.f_blocks * info.f_frsize;
wafwerar's avatar
wafwerar 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    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)) {
    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 已提交
570 571
}

S
monitor  
Shengliang Guan 已提交
572
int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) {
wafwerar's avatar
wafwerar 已提交
573
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
  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
590
  TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM);
S
Shengliang Guan 已提交
591
  if (pFile == NULL) return -1;
S
Shengliang Guan 已提交
592 593

  ssize_t _bytes = 0;
S
Shengliang Guan 已提交
594
  char   *line = NULL;
S
Shengliang Guan 已提交
595
  char    tmp[24];
S
Shengliang Guan 已提交
596 597
  int     readIndex = 0;

598 599
  while (!taosEOFFile(pFile)) {
    _bytes = taosGetLineFile(pFile, &line);
S
Shengliang Guan 已提交
600
    if (_bytes < 10 || line == NULL) {
S
Shengliang Guan 已提交
601 602 603 604 605 606 607 608
      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 已提交
609
    } else if (strstr(line, "read_bytes:") != NULL) {  // read_bytes
S
Shengliang Guan 已提交
610 611
      sscanf(line, "%s %" PRId64, tmp, read_bytes);
      readIndex++;
S
Shengliang Guan 已提交
612
    } else if (strstr(line, "write_bytes:") != NULL) {  // write_bytes
S
Shengliang Guan 已提交
613 614
      sscanf(line, "%s %" PRId64, tmp, write_bytes);
      readIndex++;
S
Shengliang Guan 已提交
615
    } else {
S
Shengliang Guan 已提交
616 617
    }

wafwerar's avatar
wafwerar 已提交
618 619 620
    if (readIndex >= 4) break;
  }

wafwerar's avatar
wafwerar 已提交
621
  if (line != NULL) taosMemoryFreeClear(line);
wafwerar's avatar
wafwerar 已提交
622 623 624 625 626 627 628 629 630 631
  taosCloseFile(&pFile);

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

  return 0;
#endif
}

632 633 634 635 636 637 638 639 640
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;
641
  if (taosGetProcIO(&cur_rchars, &cur_wchars, &cur_read_bytes, &cur_write_bytes) == 0) {
642 643 644 645 646 647 648 649
    *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;
650 651 652 653 654
  } else {
    *rchars = 0;
    *wchars = 0;
    *read_bytes = 0;
    *write_bytes = 0;
655 656 657
  }
}

wafwerar's avatar
wafwerar 已提交
658
int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) {
wafwerar's avatar
wafwerar 已提交
659
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
  *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;
  char   *line = NULL;

  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};

    _bytes = taosGetLineFile(pFile, &line);
    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 已提交
704 705
  }

wafwerar's avatar
wafwerar 已提交
706
  if (line != NULL) taosMemoryFreeClear(line);
707
  taosCloseFile(&pFile);
S
Shengliang Guan 已提交
708

S
io  
Shengliang Guan 已提交
709
  return 0;
wafwerar's avatar
wafwerar 已提交
710
#endif
S
Shengliang Guan 已提交
711 712
}

713 714 715 716 717
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;
718
  if (taosGetCardInfo(&cur_receive_bytes, &cur_transmit_bytes) == 0) {
719 720 721 722
    *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;
723 724 725
  } else {
    *receive_bytes = 0;
    *transmit_bytes = 0;
726 727 728
  }
}

wafwerar's avatar
wafwerar 已提交
729
void taosKillSystem() {
wafwerar's avatar
wafwerar 已提交
730
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743
  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 已提交
744
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
745 746
  GUID guid;
  CoCreateGuid(&guid);
747
  memcpy(uid, &guid, uidlen);
wafwerar's avatar
wafwerar 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777

  return 0;
#elif defined(_TD_DARWIN_64)
  uuid_t uuid = {0};
  uuid_generate(uuid);
  // it's caller's responsibility to make enough space for `uid`, that's 36-char + 1-null
  uuid_unparse_lower(uuid, uid);
  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 已提交
778
#ifdef WINDOWS
779
  assert(0);
wafwerar's avatar
wafwerar 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
  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 已提交
812 813 814
}

void taosSetCoreDump(bool enable) {
wafwerar's avatar
wafwerar 已提交
815
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
816 817 818
  SetUnhandledExceptionFilter(&FlCrashDump);
#elif defined(_TD_DARWIN_64)
#else
S
Shengliang Guan 已提交
819 820 821 822 823 824 825
  if (!enable) return;

  // 1. set ulimit -c unlimited
  struct rlimit rlim;
  struct rlimit rlim_new;
  if (getrlimit(RLIMIT_CORE, &rlim) == 0) {
#ifndef _ALPINE
S
Shengliang Guan 已提交
826
    // printf("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max);
S
Shengliang Guan 已提交
827
#else
S
Shengliang Guan 已提交
828
    // printf("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max);
S
Shengliang Guan 已提交
829 830 831 832
#endif
    rlim_new.rlim_cur = RLIM_INFINITY;
    rlim_new.rlim_max = RLIM_INFINITY;
    if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) {
S
Shengliang Guan 已提交
833
      // printf("set unlimited fail, error: %s", strerror(errno));
S
Shengliang Guan 已提交
834 835 836 837 838 839 840 841
      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 已提交
842
    // printf("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max);
S
Shengliang Guan 已提交
843
#else
S
Shengliang Guan 已提交
844
    // printf("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max);
S
Shengliang Guan 已提交
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
#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);

  if (syscall(SYS__sysctl, &args) == -1) {
S
Shengliang Guan 已提交
870
    // printf("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno));
S
Shengliang Guan 已提交
871 872
  }

S
Shengliang Guan 已提交
873
  // printf("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid);
S
Shengliang Guan 已提交
874 875 876 877 878 879 880 881 882 883 884 885

  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);

  if (syscall(SYS__sysctl, &args) == -1) {
S
Shengliang Guan 已提交
886
    // printf("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno));
S
Shengliang Guan 已提交
887 888
  }

S
Shengliang Guan 已提交
889
  // printf("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid);
S
Shengliang Guan 已提交
890 891
#endif
#endif
wafwerar's avatar
wafwerar 已提交
892
}
S
Shengliang Guan 已提交
893 894

SysNameInfo taosGetSysNameInfo() {
wafwerar's avatar
wafwerar 已提交
895
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
896 897 898
  SysNameInfo info = {0};
  DWORD dwVersion = GetVersion();

wafwerar's avatar
wafwerar 已提交
899 900 901 902 903
  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 已提交
904 905
  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 已提交
906 907
  tmp = getenv("PROCESSOR_ARCHITECTURE");
  if (tmp != NULL) tstrncpy(info.machine, tmp, sizeof(info.machine));
wafwerar's avatar
wafwerar 已提交
908 909

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

913 914
  struct utsname uts;
  if (!uname(&uts)) {
H
Haojun Liao 已提交
915 916 917 918 919
    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 已提交
920 921 922
  }

  return info;
wafwerar's avatar
wafwerar 已提交
923 924
#else
  SysNameInfo info = {0};
925

wafwerar's avatar
wafwerar 已提交
926 927 928 929 930 931 932
  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));
933 934
  }

wafwerar's avatar
wafwerar 已提交
935
  return info;
936
#endif
wafwerar's avatar
wafwerar 已提交
937
}