• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#define _DEFAULT_SOURCE
17
#include "os.h"
18
#include "taoserror.h"
19
#include "cus_name.h"
20

21
#define PROCESS_ITEM 12
22
#define UUIDLEN37 37
23

24
typedef struct {
25
  uint64_t user;
26
  uint64_t nice;
27
  uint64_t system;
28
  uint64_t idle;
29
  uint64_t wa;
30
  uint64_t hi;
31
  uint64_t si;
32
  uint64_t st;
33
  uint64_t guest;
34
  uint64_t guest_nice;
35
} SysCpuInfo;
36

37
typedef struct {
38
  uint64_t utime;   // user time
39
  uint64_t stime;   // kernel time
40
  uint64_t cutime;  // all user time
41
  uint64_t cstime;  // all dead time
42
} ProcCpuInfo;
43

44
#ifdef WINDOWS
45

46
/*
47
 * windows implementation
48
 */
49

50
#if (_WIN64)
51
#include <iphlpapi.h>
52
#include <mswsock.h>
53
#include <psapi.h>
54
#include <stdio.h>
55
#include <windows.h>
56
#include <ws2tcpip.h>
57
#pragma comment(lib, "Mswsock.lib ")
58
#endif
59

60
#include <objbase.h>
61

62
#pragma warning(push)
63
#pragma warning(disable : 4091)
64
#include <DbgHelp.h>
65
#pragma warning(pop)
66

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

74
  HMODULE dll = LoadLibrary("dbghelp.dll");
75
  if (dll == NULL) return EXCEPTION_CONTINUE_SEARCH;
76
  FxMiniDumpWriteDump mdwd = (FxMiniDumpWriteDump)(GetProcAddress(dll, "MiniDumpWriteDump"));
77
  if (mdwd == NULL) {
78
    FreeLibrary(dll);
79
    return EXCEPTION_CONTINUE_SEARCH;
80
  }
81

82
  TCHAR path[MAX_PATH];
83
  DWORD len = GetModuleFileName(NULL, path, _countof(path));
84
  path[len - 3] = 'd';
85
  path[len - 2] = 'm';
86
  path[len - 1] = 'p';
87

88
  HANDLE file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
89
  if (file == INVALID_HANDLE_VALUE) {
90
    FreeLibrary(dll);
91
    return EXCEPTION_CONTINUE_SEARCH;
92
  }
93

94
  MINIDUMP_EXCEPTION_INFORMATION mei;
95
  mei.ThreadId = GetCurrentThreadId();
96
  mei.ExceptionPointers = ep;
97
  mei.ClientPointers = FALSE;
98

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

101
  CloseHandle(file);
102
  FreeLibrary(dll);
103

104
  return EXCEPTION_CONTINUE_SEARCH;
105
}
106
LONG WINAPI exceptionHandler(LPEXCEPTION_POINTERS exception);
107

108
#elif defined(_TD_DARWIN_64)
109

110
#include <errno.h>
111
#include <libproc.h>
112
#include <sys/sysctl.h>
113
#include <SystemConfiguration/SCDynamicStoreCopySpecific.h>
114
#include <CoreFoundation/CFString.h>
115
#include <stdio.h>
116

117
#else
118

119
#include <argp.h>
120
#ifndef TD_ASTRA
121
#include <linux/sysctl.h>
122
#include <sys/file.h>
123
#include <sys/resource.h>
124
#include <sys/statvfs.h>
125
#include <sys/syscall.h>
126
#endif
127
#include <sys/utsname.h>
128
#include <unistd.h>
129

130
static pid_t tsProcId;
131
static char  tsSysNetFile[] = "/proc/net/dev";
132
static char  tsSysCpuFile[] = "/proc/stat";
133
static char  tsCpuPeriodFile[] = "/sys/fs/cgroup/cpu/cpu.cfs_period_us";
134
static char  tsCpuQuotaFile[] = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us";
135
static char  tsProcCpuFile[25] = {0};
136
static char  tsProcMemFile[25] = {0};
137
static char  tsProcIOFile[25] = {0};
138

139
static void taosGetProcIOnfos() {
92,925✔
140
  tsPageSizeKB = sysconf(_SC_PAGESIZE) / 1024;
92,925✔
141
  tsOpenMax = sysconf(_SC_OPEN_MAX);
92,925✔
142
  tsStreamMax = TMAX(sysconf(_SC_STREAM_MAX), 0);
92,925✔
143
#ifndef TD_ASTRA
144
  tsProcId = (pid_t)syscall(SYS_gettid);
92,925✔
145

146
  (void)snprintf(tsProcMemFile, sizeof(tsProcMemFile), "/proc/%d/status", tsProcId);
92,925✔
147
  (void)snprintf(tsProcCpuFile, sizeof(tsProcCpuFile), "/proc/%d/stat", tsProcId);
92,925✔
148
  (void)snprintf(tsProcIOFile, sizeof(tsProcIOFile), "/proc/%d/io", tsProcId);
92,925✔
149
#endif
150
}
92,925✔
151
#endif
152

153
static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) {
92,929✔
154
  int32_t code = 0;
92,929✔
155
#ifdef WINDOWS
156
  FILETIME pre_idleTime = {0};
157
  FILETIME pre_kernelTime = {0};
158
  FILETIME pre_userTime = {0};
159
  FILETIME idleTime;
160
  FILETIME kernelTime;
161
  FILETIME userTime;
162
  bool     res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
163
  if (res) {
164
    cpuInfo->idle = CompareFileTime(&pre_idleTime, &idleTime);
165
    cpuInfo->system = CompareFileTime(&pre_kernelTime, &kernelTime);
166
    cpuInfo->user = CompareFileTime(&pre_userTime, &userTime);
167
    cpuInfo->nice = 0;
168
  }
169
#elif defined(DARWIN) || defined(TD_ASTRA)
170
  cpuInfo->idle = 0;
171
  cpuInfo->system = 0;
172
  cpuInfo->user = 0;
173
  cpuInfo->nice = 0;
174
#else
175
  TdFilePtr pFile = taosOpenFile(tsSysCpuFile, TD_FILE_READ | TD_FILE_STREAM);
92,929✔
176
  if (pFile == NULL) {
92,929!
177
    return terrno;
×
178
  }
179

180
  char    line[1024];
181
  ssize_t bytes = taosGetsFile(pFile, sizeof(line), line);
92,929✔
182
  if (bytes < 0) {
92,929!
183
    TAOS_SKIP_ERROR(taosCloseFile(&pFile));
×
184
    return terrno;
×
185
  }
186

187
  char cpu[10] = {0};
92,929✔
188
  code = sscanf(line,
92,929✔
189
         "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
190
         " %" PRIu64,
191
         cpu, &cpuInfo->user, &cpuInfo->nice, &cpuInfo->system, &cpuInfo->idle, &cpuInfo->wa, &cpuInfo->hi,
192
         &cpuInfo->si, &cpuInfo->st, &cpuInfo->guest, &cpuInfo->guest_nice);
193
  if (EOF == code) {
92,929!
194
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
195
    TAOS_SKIP_ERROR(taosCloseFile(&pFile));
×
196
    return terrno;
×
197
  }
198
  
199
  TAOS_SKIP_ERROR(taosCloseFile(&pFile));
92,929✔
200
#endif
201

202
  return 0;
92,929✔
203
}
204

205
static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) {
92,929✔
206
  int32_t code = 0;
92,929✔
207

208
#ifdef WINDOWS
209
  FILETIME pre_krnlTm = {0};
210
  FILETIME pre_usrTm = {0};
211
  FILETIME creatTm, exitTm, krnlTm, usrTm;
212

213
  if (GetThreadTimes(GetCurrentThread(), &creatTm, &exitTm, &krnlTm, &usrTm)) {
214
    cpuInfo->stime = CompareFileTime(&pre_krnlTm, &krnlTm);
215
    cpuInfo->utime = CompareFileTime(&pre_usrTm, &usrTm);
216
    cpuInfo->cutime = 0;
217
    cpuInfo->cstime = 0;
218
  }
219
#elif defined(DARWIN) || defined(TD_ASTRA)
220
  cpuInfo->stime = 0;
221
  cpuInfo->utime = 0;
222
  cpuInfo->cutime = 0;
223
  cpuInfo->cstime = 0;
224
#else
225
  TdFilePtr pFile = taosOpenFile(tsProcCpuFile, TD_FILE_READ | TD_FILE_STREAM);
92,929✔
226
  if (pFile == NULL) {
92,929✔
227
    return terrno;
3✔
228
  }
229

230
  char    line[1024] = {0};
92,926✔
231
  ssize_t bytes = taosGetsFile(pFile, sizeof(line), line);
92,926✔
232
  if (bytes < 0) {
92,926!
233
    TAOS_SKIP_ERROR(taosCloseFile(&pFile));
×
234
    return code;
×
235
  }
236

237
  for (int i = 0, blank = 0; line[i] != 0; ++i) {
7,470,414!
238
    if (line[i] == ' ') blank++;
7,470,414✔
239
    if (blank == PROCESS_ITEM) {
7,470,414✔
240
      code = sscanf(line + i + 1, "%" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, &cpuInfo->utime, &cpuInfo->stime,
92,926✔
241
             &cpuInfo->cutime, &cpuInfo->cstime);
242
      if (EOF == code) {
92,926!
243
        terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
244
        return terrno;
×
245
      }
246
             
247
      break;
92,926✔
248
    }
249
  }
250

251
  TAOS_SKIP_ERROR(taosCloseFile(&pFile));
92,926✔
252
#endif
253

254
  return 0;
92,926✔
255
}
256

257
bool taosCheckSystemIsLittleEnd() {
4,787✔
258
  union check {
259
    int16_t i;
260
    char    ch[2];
261
  } c;
262
  c.i = 1;
4,787✔
263
  return c.ch[0] == 1;
4,787✔
264
}
265

266
void taosGetSystemInfo() {
92,925✔
267
#ifdef WINDOWS
268
  TAOS_SKIP_ERROR(taosGetCpuCores(&tsNumOfCores, false));
269
  TAOS_SKIP_ERROR(taosGetTotalMemory(&tsTotalMemoryKB));
270
  TAOS_SKIP_ERROR(taosGetCpuUsage(NULL, NULL));
271
#elif defined(_TD_DARWIN_64)
272
  long physical_pages = sysconf(_SC_PHYS_PAGES);
273
  long page_size = sysconf(_SC_PAGESIZE);
274
  tsTotalMemoryKB = physical_pages * page_size / 1024;
275
  tsPageSizeKB = page_size / 1024;
276
  tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN);
277
#elif defined(TD_ASTRA)
278
  taosGetProcIOnfos();
279
  TAOS_SKIP_ERROR(taosGetCpuCores(&tsNumOfCores, false));
280
  TAOS_SKIP_ERROR(taosGetTotalMemory(&tsTotalMemoryKB));
281
  TAOS_SKIP_ERROR(taosGetCpuUsage(NULL, NULL));
282
#else
283
  taosGetProcIOnfos();
92,925✔
284
  TAOS_SKIP_ERROR(taosGetCpuCores(&tsNumOfCores, false)); 
92,925✔
285
  TAOS_SKIP_ERROR(taosGetTotalMemory(&tsTotalMemoryKB));
92,925✔
286
  TAOS_SKIP_ERROR(taosGetCpuUsage(NULL, NULL));
92,925✔
287
  TAOS_SKIP_ERROR(taosGetCpuInstructions(&tsSSE42Supported, &tsAVXSupported, &tsAVX2Supported, &tsFMASupported, &tsAVX512Supported));
92,925✔
288
#endif
289
}
92,925✔
290

291
int32_t taosGetEmail(char *email, int32_t maxLen) {
1,934✔
292
  OS_PARAM_CHECK(email);
1,934✔
293
#ifdef WINDOWS
294
  return 0;
295
#elif defined(_TD_DARWIN_64)
296
#ifdef CUS_PROMPT
297
  const char *filepath = "/usr/local/"CUS_PROMPT"/email";
298
#else
299
  const char *filepath = "/usr/local/taos/email";
300
#endif  // CUS_PROMPT
301

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

305
  if (taosReadFile(pFile, (void *)email, maxLen) < 0) {
306
    taosCloseFile(&pFile);
307
    return terrno;
308
  }
309

310
  taosCloseFile(&pFile);
311
  return 0;
312
#else
313
#ifdef CUS_PROMPT
314
  const char *filepath = "/usr/local/"CUS_PROMPT"/email";
1,931✔
315
#else
316
  const char *filepath = "/usr/local/taos/email";
317
#endif  // CUS_PROMPT
318

319
  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ);
1,931✔
320
  if (pFile == NULL) return terrno;
1,931!
321

322
  if (taosReadFile(pFile, (void *)email, maxLen) < 0) {
×
323
    int32_t code = terrno;
×
324
    TAOS_SKIP_ERROR(taosCloseFile(&pFile));
×
325
    return code;
×
326
  }
327

328
  TAOS_SKIP_ERROR(taosCloseFile(&pFile));
×
329
  
330
  return 0;
×
331
#endif
332
}
333

334
#ifdef WINDOWS
335
bool getWinVersionReleaseName(char *releaseName, int32_t maxLen) {
336
  if(releaseName == NULL) return false;
337
  TCHAR          szFileName[MAX_PATH];
338
  DWORD             dwHandle;
339
  DWORD             dwLen;
340
  LPVOID            lpData;
341
  UINT              uLen;
342
  VS_FIXEDFILEINFO *pFileInfo;
343

344
  int ret = GetWindowsDirectory(szFileName, MAX_PATH);
345
  if (ret == 0) {
346
    return false;
347
  }
348
  wsprintf(szFileName, L"%s%s", szFileName, L"\\explorer.exe");
349
  dwLen = GetFileVersionInfoSize(szFileName, &dwHandle);
350
  if (dwLen == 0) {
351
    return false;
352
  }
353

354
  lpData = malloc(dwLen);
355
  if (lpData == NULL) return false;
356
  if (!GetFileVersionInfo(szFileName, dwHandle, dwLen, lpData)) {
357
    free(lpData);
358
    return false;
359
  }
360

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

366
  snprintf(releaseName, maxLen, "Windows %d.%d", HIWORD(pFileInfo->dwProductVersionMS),
367
           LOWORD(pFileInfo->dwProductVersionMS));
368
  free(lpData);
369
  return true;
370
}
371
#endif
372

373
int32_t taosGetOsReleaseName(char *releaseName, char* sName, char* ver, int32_t maxLen) {
8✔
374
  OS_PARAM_CHECK(releaseName);
8✔
375
#ifdef WINDOWS
376
  if (!getWinVersionReleaseName(releaseName, maxLen)) {
377
    snprintf(releaseName, maxLen, "Windows");
378
  }
379
  if(sName) snprintf(sName, maxLen, "Windows");
380
  return 0;
381
#elif defined(_TD_DARWIN_64)
382
  char osversion[32];
383
  size_t osversion_len = sizeof(osversion) - 1;
384
  int osversion_name[] = { CTL_KERN, KERN_OSRELEASE };
385

386
  if(sName) snprintf(sName, maxLen, "macOS");
387
  if (sysctl(osversion_name, 2, osversion, &osversion_len, NULL, 0) == -1) {
388
    return TAOS_SYSTEM_ERROR(ERRNO);
389
  }
390

391
  uint32_t major, minor;
392
  if (sscanf(osversion, "%u.%u", &major, &minor) == EOF) {
393
      return TAOS_SYSTEM_ERROR(ERRNO);
394
  }
395
  if (major >= 20) {
396
      major -= 9; // macOS 11 and newer
397
      snprintf(releaseName, maxLen, "macOS %u.%u", major, minor);
398
  } else {
399
      major -= 4; // macOS 10.1.1 and newer
400
      snprintf(releaseName, maxLen, "macOS 10.%d.%d", major, minor);
401
  }
402

403
  return 0;
404
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
405
  if(sName) snprintf(sName, maxLen, "Astra");
406
  snprintf(releaseName, maxLen, "Astra");
407
  return 0;
408
#else
409
  char    line[1024];
410
  char   *dest = NULL;
5✔
411
  size_t  size = 0;
5✔
412
  int32_t code = 0;
5✔
413
  int32_t cnt = 0;
5✔
414

415
  TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM);
5✔
416
  if (pFile == NULL) {
5!
417
    return terrno;
×
418
  }
419

420
  while ((size = taosGetsFile(pFile, sizeof(line), line)) > 0) {
65✔
421
    line[size - 1] = '\0';
60✔
422
    if (strncmp(line, "NAME", 4) == 0) {
60✔
423
      dest = sName;
5✔
424
    } else if (strncmp(line, "PRETTY_NAME", 11) == 0) {
55✔
425
      dest = releaseName;
5✔
426
      code = 0;
5✔
427
    } else if (strncmp(line, "VERSION_ID", 10) == 0) {
50✔
428
      dest = ver;
5✔
429
    } else {
430
      continue;
45✔
431
    }
432
    if (!dest) continue;
15✔
433
    const char *p = strchr(line, '=') + 1;
5✔
434
    if (*p == '"') {
5!
435
      p++;
5✔
436
      line[size - 2] = 0;
5✔
437
    }
438
    tstrncpy(dest, p, maxLen);
5✔
439

440
    if (++cnt >= 3) break;
5!
441
  }
442

443
  TAOS_SKIP_ERROR(taosCloseFile(&pFile));
5✔
444
  return code;
5✔
445
#endif
446
}
447

448
int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) {
11✔
449
  OS_PARAM_CHECK(cpuModel);
11✔
450
  OS_PARAM_CHECK(numOfCores);
8✔
451
#ifdef WINDOWS
452
  char  value[100];
453
  DWORD bufferSize = sizeof(value);
454
  LSTATUS ret = RegGetValue(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "ProcessorNameString",
455
              RRF_RT_ANY, NULL, (PVOID)&value, &bufferSize);
456
  if (ret != ERROR_SUCCESS) {
457
    return TAOS_SYSTEM_ERROR(ret);
458
  }
459
  tstrncpy(cpuModel, value, maxLen);
460
  SYSTEM_INFO si;
461
  memset(&si, 0, sizeof(SYSTEM_INFO));
462
  GetSystemInfo(&si);
463
  *numOfCores = si.dwNumberOfProcessors;
464
  return 0;
465
#elif defined(_TD_DARWIN_64)
466
  char    buf[16];
467
  int32_t done = 0;
468
  int32_t code = -1;
469

470
  TdCmdPtr pCmd = taosOpenCmd("sysctl -n machdep.cpu.brand_string");
471
  if (pCmd == NULL) return code;
472
  if (taosGetsCmd(pCmd, maxLen, cpuModel) > 0) {
473
    code = 0;
474
    done |= 1;
475
  }
476
  int endPos = strlen(cpuModel)-1;
477
  if (cpuModel[endPos] == '\n') {
478
    cpuModel[endPos] = '\0';
479
  }
480
  taosCloseCmd(&pCmd);
481

482
  pCmd = taosOpenCmd("sysctl -n machdep.cpu.core_count");
483
  if (pCmd == NULL) return code;
484
  memset(buf, 0, sizeof(buf));
485
  if (taosGetsCmd(pCmd, sizeof(buf) - 1, buf) > 0) {
486
    code = 0;
487
    done |= 2;
488
    *numOfCores = taosStr2Float(buf, NULL);
489
  }
490
  taosCloseCmd(&pCmd);
491

492
  return code;
493
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
494
  tstrncpy(cpuModel, "ft_2000_4", maxLen);
495
  TAOS_SKIP_ERROR(taosGetCpuCores(numOfCores, false));
496
  return 0;
497
#else
498
  char    line[1024] = {0};
5✔
499
  size_t  size = 0;
5✔
500
  int32_t done = 0;
5✔
501
  int32_t code = 0;
5✔
502
  float   coreCount = 0;
5✔
503

504
  TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM);
5✔
505
  if (pFile == NULL) return terrno;
5!
506

507
  while (done != 3 && (size = taosGetsFile(pFile, sizeof(line), line)) > 0) {
70!
508
    line[size - 1] = '\0';
65✔
509
    if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) {
65✔
510
      const char *v = strchr(line, ':') + 2;
5✔
511
      tstrncpy(cpuModel, v, maxLen);
5✔
512
      code = 0;
5✔
513
      done |= 1;
5✔
514
    } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) {
60!
515
      const char *v = strchr(line, ':') + 2;
5✔
516
      *numOfCores = taosStr2Float(v, NULL);
5✔
517
      done |= 2;
5✔
518
    }
519
    if (strncmp(line, "processor", 9) == 0) coreCount += 1;
65✔
520
  }
521

522
  TAOS_SKIP_ERROR(taosCloseFile(&pFile));
5✔
523

524
  if (code != 0 && (done & 1) == 0) {
5!
525
    TdFilePtr pFile1 = taosOpenFile("/proc/device-tree/model", TD_FILE_READ | TD_FILE_STREAM);
×
526
    if (pFile1 != NULL) {
×
527
      ssize_t bytes = taosGetsFile(pFile1, maxLen, cpuModel);
×
528
      TAOS_SKIP_ERROR(taosCloseFile(&pFile));
×
529
      if (bytes > 0) {
×
530
        code = 0;
×
531
        done |= 1;
×
532
      }
533
    }
534
  }
535

536
  if (code != 0 && (done & 1) == 0) {
5!
537
    TdCmdPtr pCmd = taosOpenCmd("uname -a");
×
538
    if (pCmd == NULL) {
×
539
      return terrno;
×
540
    }
541
    if (taosGetsCmd(pCmd, maxLen, cpuModel) > 0) {
×
542
      code = 0;
×
543
      done |= 1;
×
544
    }
545
    taosCloseCmd(&pCmd);
×
546
  }
547

548
  if ((done & 2) == 0) {
5!
549
    *numOfCores = coreCount;
×
550
    done |= 2;
×
551
  }
552

553
  return code;
5✔
554
#endif
555
}
556

557
// Returns the container's CPU quota if successful, otherwise returns the physical CPU cores
558
static int32_t taosCntrGetCpuCores(float *numOfCores) {
95,425✔
559
#ifdef WINDOWS
560
  return TSDB_CODE_UNSUPPORT_OS;
561
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA)
562
  return TSDB_CODE_UNSUPPORT_OS;
563
#else
564
  TdFilePtr pFile = NULL;
95,425✔
565
  if (!(pFile = taosOpenFile(tsCpuQuotaFile, TD_FILE_READ | TD_FILE_STREAM))) {
95,425!
566
    goto _sys;
×
567
  }
568
  char qline[32] = {0};
95,425✔
569
  if (taosGetsFile(pFile, sizeof(qline), qline) <= 0) {
95,425!
570
    TAOS_SKIP_ERROR(taosCloseFile(&pFile));
×
571
    goto _sys;
×
572
  }
573
  
574
  TAOS_SKIP_ERROR(taosCloseFile(&pFile));
95,425✔
575
  float quota = taosStr2Float(qline, NULL);
95,425✔
576
  if (quota < 0) {
95,425!
577
    goto _sys;
95,425✔
578
  }
579

580
  if (!(pFile = taosOpenFile(tsCpuPeriodFile, TD_FILE_READ | TD_FILE_STREAM))) {
×
581
    goto _sys;
×
582
  }
583
  
584
  char pline[32] = {0};
×
585
  if (taosGetsFile(pFile, sizeof(pline), pline) <= 0) {
×
586
    TAOS_SKIP_ERROR(taosCloseFile(&pFile));
×
587
    goto _sys;
×
588
  }
589
  
590
  TAOS_SKIP_ERROR(taosCloseFile(&pFile));
×
591

592
  float period = taosStr2Float(pline, NULL);
×
593
  float quotaCores = quota / period;
×
594
  float sysCores = sysconf(_SC_NPROCESSORS_ONLN);
×
595
  if (quotaCores < sysCores && quotaCores > 0) {
×
596
    *numOfCores = quotaCores;
×
597
  } else {
598
    *numOfCores = sysCores;
×
599
  }
600
  if(*numOfCores <= 0) {
×
601
    return TAOS_SYSTEM_ERROR(ERRNO);
×
602
  }
603
  goto _end;
×
604
  
605
_sys:
95,425✔
606
  *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
95,425✔
607
  if(*numOfCores <= 0) {
95,425!
608
    return TAOS_SYSTEM_ERROR(ERRNO);
×
609
  }
610
  
611
_end:
95,425✔
612
  return 0;
95,425✔
613
  
614
#endif
615
}
616

617
int32_t taosGetCpuCores(float *numOfCores, bool physical) {
99,252✔
618
  OS_PARAM_CHECK(numOfCores);
99,252✔
619
#ifdef WINDOWS
620
  SYSTEM_INFO info;
621
  GetSystemInfo(&info);
622
  *numOfCores = info.dwNumberOfProcessors;
623
  return  0;
624
#elif defined(_TD_DARWIN_64)
625
  *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
626
  if(*numOfCores <= 0) {
627
    return TAOS_SYSTEM_ERROR(ERRNO);
628
  }
629
  return 0;
630
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
631
  *numOfCores = 4;
632
  return 0;
633
#else
634
  if (physical) {
99,249✔
635
    *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
3,824✔
636
    if(*numOfCores <= 0) {
3,824!
637
      return TAOS_SYSTEM_ERROR(ERRNO);
×
638
    }
639
  } else {
640
    int code= taosCntrGetCpuCores(numOfCores);
95,425✔
641
    if(code != 0) {
95,425!
642
      return code;
×
643
    }
644
  }
645
  return 0;
99,249✔
646
#endif
647
}
648

649
int32_t taosGetCpuUsage(double *cpu_system, double *cpu_engine) {
92,929✔
650
  static int64_t lastSysUsed = -1;
651
  static int64_t lastSysTotal = -1;
652
  static int64_t lastProcTotal = -1;
653
  static int64_t curSysUsed = 0;
654
  static int64_t curSysTotal = 0;
655
  static int64_t curProcTotal = 0;
656

657
  if (cpu_system != NULL) *cpu_system = 0;
92,929✔
658
  if (cpu_engine != NULL) *cpu_engine = 0;
92,929✔
659

660
  SysCpuInfo  sysCpu = {0};
92,929✔
661
  ProcCpuInfo procCpu = {0};
92,929✔
662
  if (taosGetSysCpuInfo(&sysCpu) == 0 && taosGetProcCpuInfo(&procCpu) == 0) {
92,929!
663
    curSysUsed = sysCpu.user + sysCpu.nice + sysCpu.system + sysCpu.wa + sysCpu.hi + sysCpu.si + sysCpu.st +
92,926✔
664
                 sysCpu.guest + sysCpu.guest_nice;
92,926✔
665
    curSysTotal = curSysUsed + sysCpu.idle;
92,926✔
666
    curProcTotal = procCpu.utime + procCpu.stime + procCpu.cutime + procCpu.cstime;
92,926✔
667

668
    if(lastSysUsed >= 0 && lastSysTotal >=0 && lastProcTotal >=0){
92,926!
669
      if (curSysTotal - lastSysTotal > 0 && curSysUsed >= lastSysUsed && curProcTotal >= lastProcTotal) {
86,065!
670
        if (cpu_system != NULL) {
78,888✔
671
          *cpu_system = (curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100;
1✔
672
        }
673
        if (cpu_engine != NULL) {
78,888✔
674
          *cpu_engine = (curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100;
1✔
675
        }
676
      }
677
    }
678

679
    lastSysUsed = curSysUsed;
92,926✔
680
    lastSysTotal = curSysTotal;
92,926✔
681
    lastProcTotal = curProcTotal;
92,926✔
682
  }
683
  return 0;
92,929✔
684
}
685

686
#define __cpuid_fix(level, a, b, c, d) \
687
              __asm__("xor %%ecx, %%ecx\n" \
688
                      "cpuid\n" \
689
                      : "=a"(a), "=b"(b), "=c"(c), "=d"(d) \
690
                      : "0"(level))
691

692
// todo add for windows and mac
693
int32_t taosGetCpuInstructions(char* sse42, char* avx, char* avx2, char* fma, char* avx512) {
92,925✔
694
#ifdef WINDOWS
695
#elif defined(_TD_DARWIN_64)
696
#else
697

698
#ifdef _TD_X86_
699
  // Since the compiler is not support avx/avx2 instructions, the global variables always need to be
700
  // set to be false
701
  uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
92,925✔
702

703
  int32_t ret = __get_cpuid(1, &eax, &ebx, &ecx, &edx);
92,925✔
704
  if (ret == 0) {
92,925!
705
    return -1;  // failed to get the cpuid info
×
706
  }
707

708
  *sse42 = (char) ((ecx & bit_SSE4_2) == bit_SSE4_2);
92,925✔
709
  *avx   = (char) ((ecx & bit_AVX) == bit_AVX);
92,925✔
710
  *fma   = (char) ((ecx & bit_FMA) == bit_FMA);
92,925✔
711

712
  // work around a bug in GCC.
713
  // Ref to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77756
714
  __cpuid_fix(7u, eax, ebx, ecx, edx);
92,925✔
715
  *avx2 = (char) ((ebx & bit_AVX2) == bit_AVX2);
92,925✔
716
  *avx512 = (char)((ebx & bit_AVX512F) == bit_AVX512F);
92,925✔
717
#endif   // _TD_X86_
718
#endif
719

720
  return 0;
92,925✔
721
}
722

723
int32_t taosGetTotalMemory(int64_t *totalKB) {
95,312✔
724
  OS_PARAM_CHECK(totalKB);
95,312✔
725
#ifdef WINDOWS
726
  MEMORYSTATUSEX memsStat;
727
  memsStat.dwLength = sizeof(memsStat);
728
  if (!GlobalMemoryStatusEx(&memsStat)) {
729
    return TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
730
  }
731

732
  *totalKB = memsStat.ullTotalPhys / 1024;
733
  return 0;
734
#elif defined(_TD_DARWIN_64)
735
  return 0;
736
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
737
  *totalKB = (int64_t)256 * 1024;
738
  return 0;
739
#else
740
  *totalKB = (int64_t)(sysconf(_SC_PHYS_PAGES) * tsPageSizeKB);
95,309✔
741
  if(*totalKB <= 0) {
95,309!
742
    return TAOS_SYSTEM_ERROR(ERRNO);
×
743
  }
744
  return 0;
95,309✔
745
#endif
746
}
747

748
int32_t taosGetProcMemory(int64_t *usedKB) {
7✔
749
  OS_PARAM_CHECK(usedKB);
7✔
750
#ifdef WINDOWS
751
  unsigned bytes_used = 0;
752

753
#if defined(_WIN64) && defined(_MSC_VER)
754
  PROCESS_MEMORY_COUNTERS pmc;
755
  HANDLE                  cur_proc = GetCurrentProcess();
756

757
  if (GetProcessMemoryInfo(cur_proc, &pmc, sizeof(pmc))) {
758
    bytes_used = (unsigned)(pmc.WorkingSetSize + pmc.PagefileUsage);
759
  }
760
#endif
761

762
  *usedKB = bytes_used / 1024;
763
  return 0;
764
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA)
765
  *usedKB = 0;
766
  return 0;
767
#else
768
  TdFilePtr pFile = taosOpenFile(tsProcMemFile, TD_FILE_READ | TD_FILE_STREAM);
4✔
769
  if (pFile == NULL) {
4✔
770
    // printf("open file:%s failed", tsProcMemFile);
771
    return terrno;
3✔
772
  }
773

774
  ssize_t bytes = 0;
1✔
775
  char    line[1024] = {0};
1✔
776
  while (!taosEOFFile(pFile)) {
22!
777
    bytes = taosGetsFile(pFile, sizeof(line), line);
22✔
778
    if (bytes <= 0) {
22!
779
      break;
×
780
    }
781
    if (strstr(line, "VmRSS:") != NULL) {
22✔
782
      break;
1✔
783
    }
784
  }
785

786
  char tmp[10];
787
  (void)sscanf(line, "%s %" PRId64, tmp, usedKB);
1✔
788

789
  TAOS_SKIP_ERROR(taosCloseFile(&pFile));
1✔
790
  
791
  return 0;
1✔
792
#endif
793
}
794

795
int32_t taosGetSysAvailMemory(int64_t *availSize) {
10,467,243✔
796
#ifdef WINDOWS
797
  MEMORYSTATUSEX memsStat;
798
  memsStat.dwLength = sizeof(memsStat);
799
  if (!GlobalMemoryStatusEx(&memsStat)) {
800
    return -1;
801
  }
802

803
  int64_t nMemFree = memsStat.ullAvailPhys;
804
  int64_t nMemTotal = memsStat.ullTotalPhys;
805

806
  *availSize = nMemTotal - nMemFree;
807
  return 0;
808
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA)
809
  *availSize = 0;
810
  return 0;
811
#else
812
  TdFilePtr pFile = taosOpenFile("/proc/meminfo", TD_FILE_READ | TD_FILE_STREAM);
10,467,243✔
813
  if (pFile == NULL) {
10,467,243!
814
    return terrno;
×
815
  }
816

817
  ssize_t bytes = 0;
10,467,243✔
818
  char    line[128] = {0};
10,467,243✔
819
  int32_t expectedSize = 13; //"MemAvailable:"
10,467,243✔
820
  while (!taosEOFFile(pFile)) {
31,401,729!
821
    bytes = taosGetsFile(pFile, sizeof(line), line);
31,401,729✔
822
    if (bytes < 0) {
31,401,729!
823
      break;
×
824
    }
825
    if (line[0] != 'M' && line[3] != 'A') {
31,401,729!
826
      line[0] = 0;
×
827
      continue;
×
828
    }
829
    if (0 == strncmp(line, "MemAvailable:", expectedSize)) {
31,401,729✔
830
      break;
10,467,243✔
831
    }
832
  }
833

834
  if (0 == line[0]) {
10,467,243!
835
    return TSDB_CODE_UNSUPPORT_OS;
×
836
  }
837
  
838
  char tmp[32];
839
  (void)sscanf(line, "%s %" PRId64, tmp, availSize);
10,467,243✔
840

841
  *availSize *= 1024;
10,467,243✔
842
  
843
  (void)taosCloseFile(&pFile);
10,467,243✔
844
  return 0;
10,467,243✔
845
#endif
846
}
847

848
int32_t taosGetSysMemory(int64_t *usedKB) {
7✔
849
  OS_PARAM_CHECK(usedKB);
7✔
850
#ifdef WINDOWS
851
  MEMORYSTATUSEX memsStat;
852
  memsStat.dwLength = sizeof(memsStat);
853
  if (!GlobalMemoryStatusEx(&memsStat)) {
854
    return TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
855
  }
856

857
  int64_t nMemFree = memsStat.ullAvailPhys / 1024;
858
  int64_t nMemTotal = memsStat.ullTotalPhys / 1024.0;
859

860
  *usedKB = nMemTotal - nMemFree;
861
  return 0;
862
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA) // TD_ASTRA_TODO
863
  *usedKB = 0;
864
  return 0;
865
#else
866
  *usedKB = sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB;
4✔
867
  if(*usedKB <= 0) {
4✔
868
    return TAOS_SYSTEM_ERROR(ERRNO);
3✔
869
  }
870
  return 0;
1✔
871
#endif
872
}
873

874
int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) {
1,030,458✔
875
  OS_PARAM_CHECK(dataDir);
1,030,458✔
876
  OS_PARAM_CHECK(diskSize);
1,030,455✔
877
#if defined(WINDOWS)
878
  unsigned _int64 i64FreeBytesToCaller;
879
  unsigned _int64 i64TotalBytes;
880
  unsigned _int64 i64FreeBytes;
881

882
  BOOL fResult = GetDiskFreeSpaceExA(dataDir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes,
883
                                     (PULARGE_INTEGER)&i64FreeBytes);
884
  if (fResult) {
885
    diskSize->total = (int64_t)(i64TotalBytes);
886
    diskSize->avail = (int64_t)(i64FreeBytesToCaller);
887
    diskSize->used = (int64_t)(i64TotalBytes - i64FreeBytes);
888
    return 0;
889
  } else {
890
    // printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(ERRNO));
891
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
892
    return terrno;
893
  }
894
#elif defined(_TD_DARWIN_64)
895
  struct statvfs info;
896
  if (statvfs(dataDir, &info)) {
897
    // printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(ERRNO));
898
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
899
    return terrno;
900
  } else {
901
    diskSize->total = info.f_blocks * info.f_frsize;
902
    diskSize->avail = info.f_bavail * info.f_frsize;
903
    diskSize->used = (info.f_blocks - info.f_bfree) * info.f_frsize;
904
    return 0;
905
  }
906
#elif defined(TD_ASTRA)  // TD_ASTRA_TODO
907
  //  if (-1 == ioctl(dataDir, FIOFSTATVFSGETBYNAME, &info)) { // TODO:try to check whether the API is available
908
  //     terrno = TAOS_SYSTEM_ERROR(ERRNO);
909
  //     return terrno;
910
  diskSize->total = 100LL * 1024 * 1024 * 1024;
911
  diskSize->avail = 50LL * 1024 * 1024 * 1024;
912
  diskSize->used = 50LL * 1024 * 1024 * 1024;
913
  //  } else {
914
  //    diskSize->total = info.f_blocks * info.f_frsize;
915
  //    diskSize->avail = info.f_bavail * info.f_frsize;
916
  //    diskSize->used = diskSize->total - diskSize->avail;
917
  //  }
918
  return 0;
919
#else
920
  struct statvfs info;
921
  if (-1 == statvfs(dataDir, &info)) {
1,030,452!
922
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
923
    return terrno;
×
924
  } else {
925
    diskSize->total = info.f_blocks * info.f_frsize;
1,030,452✔
926
    diskSize->avail = info.f_bavail * info.f_frsize;
1,030,452✔
927
    diskSize->used = diskSize->total - diskSize->avail;
1,030,452✔
928
    
929
    return 0;
1,030,452✔
930
  }
931
#endif
932
}
933

934
int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) {
16✔
935
  OS_PARAM_CHECK(rchars);
16✔
936
  OS_PARAM_CHECK(wchars);
13✔
937
  OS_PARAM_CHECK(read_bytes);
10✔
938
  OS_PARAM_CHECK(write_bytes);
7✔
939
#ifdef WINDOWS
940
  IO_COUNTERS io_counter;
941
  if (GetProcessIoCounters(GetCurrentProcess(), &io_counter)) {
942
    *rchars = io_counter.ReadTransferCount;
943
    *wchars = io_counter.WriteTransferCount;
944
    *read_bytes = 0;
945
    *write_bytes = 0;
946
    return 0;
947
  }
948
  return TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
949
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA)
950
  *rchars = 0;
951
  *wchars = 0;
952
  *read_bytes = 0;
953
  *write_bytes = 0;
954
  return 0;
955
#else
956
  TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM);
4✔
957
  if (pFile == NULL) {
4✔
958
    return terrno;
3✔
959
  }
960

961
  ssize_t bytes = 0;
1✔
962
  char    line[1024] = {0};
1✔
963
  char    tmp[24];
964
  int     readIndex = 0;
1✔
965

966
  while (!taosEOFFile(pFile)) {
6!
967
    bytes = taosGetsFile(pFile, sizeof(line), line);
6✔
968
    if (bytes < 10) {
6!
969
      break;
×
970
    }
971
    if (strstr(line, "rchar:") != NULL) {
6✔
972
      (void)sscanf(line, "%s %" PRId64, tmp, rchars);
1✔
973
      readIndex++;
1✔
974
    } else if (strstr(line, "wchar:") != NULL) {
5✔
975
      (void)sscanf(line, "%s %" PRId64, tmp, wchars);
1✔
976
      readIndex++;
1✔
977
    } else if (strstr(line, "read_bytes:") != NULL) {  // read_bytes
4✔
978
      (void)sscanf(line, "%s %" PRId64, tmp, read_bytes);
1✔
979
      readIndex++;
1✔
980
    } else if (strstr(line, "write_bytes:") != NULL) {  // write_bytes
3✔
981
      (void)sscanf(line, "%s %" PRId64, tmp, write_bytes);
1✔
982
      readIndex++;
1✔
983
    } else {
984
    }
985

986
    if (readIndex >= 4) break;
6✔
987
  }
988

989
  TAOS_SKIP_ERROR(taosCloseFile(&pFile));
1✔
990

991
  if (readIndex < 4) {
1!
992
    return -1;
×
993
  }
994

995
  return 0;
1✔
996
#endif
997
}
998

999
int32_t taosGetProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) {
19✔
1000
  if (rchars == NULL || wchars == NULL || read_bytes == NULL || write_bytes == NULL) {
19✔
1001
    return TSDB_CODE_INVALID_PARA;
15✔
1002
  }
1003
  static int64_t last_rchars = -1;
1004
  static int64_t last_wchars = -1;
1005
  static int64_t last_read_bytes = -1;
1006
  static int64_t last_write_bytes = -1;
1007
  static int64_t cur_rchars = 0;
1008
  static int64_t cur_wchars = 0;
1009
  static int64_t cur_read_bytes = 0;
1010
  static int64_t cur_write_bytes = 0;
1011
  int32_t code = taosGetProcIO(&cur_rchars, &cur_wchars, &cur_read_bytes, &cur_write_bytes);
4✔
1012
  if (code == 0) {
4✔
1013
    if(last_rchars >=0 && last_wchars >=0 && last_read_bytes >=0 && last_write_bytes >= 0){
1!
1014
      *rchars = cur_rchars - last_rchars;
×
1015
      *wchars = cur_wchars - last_wchars;
×
1016
      *read_bytes = cur_read_bytes - last_read_bytes;
×
1017
      *write_bytes = cur_write_bytes - last_write_bytes;
×
1018
    }
1019
    else{
1020
      *rchars = 0;
1✔
1021
      *wchars = 0;
1✔
1022
      *read_bytes = 0;
1✔
1023
      *write_bytes = 0;
1✔
1024
    }
1025
    last_rchars = cur_rchars;
1✔
1026
    last_wchars = cur_wchars;
1✔
1027
    last_read_bytes = cur_read_bytes;
1✔
1028
    last_write_bytes = cur_write_bytes;
1✔
1029
  } else {
1030
    return code;
3✔
1031
  }
1032
  return 0;
1✔
1033
}
1034
void taosSetDefaultProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) {
×
1035
  if(rchars) *rchars = 0;
×
1036
  if(wchars) *wchars = 0;
×
1037
  if(read_bytes) *read_bytes = 0;
×
1038
  if(write_bytes) *write_bytes = 0;
×
1039
}
×
1040

1041
int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) {
10✔
1042
  OS_PARAM_CHECK(receive_bytes);
10✔
1043
  OS_PARAM_CHECK(transmit_bytes);
7✔
1044
  *receive_bytes = 0;
4✔
1045
  *transmit_bytes = 0;
4✔
1046

1047
#ifdef WINDOWS
1048
  return 0;
1049
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA)
1050
  return 0;
1051
#else
1052
  TdFilePtr pFile = taosOpenFile(tsSysNetFile, TD_FILE_READ | TD_FILE_STREAM);
4✔
1053
  if (pFile == NULL) {
4!
1054
    return terrno;
×
1055
  }
1056

1057
  ssize_t _bytes = 0;
4✔
1058
  char    line[1024];
1059

1060
  while (!taosEOFFile(pFile)) {
20!
1061
    int64_t o_rbytes = 0;
20✔
1062
    int64_t rpackts = 0;
20✔
1063
    int64_t o_tbytes = 0;
20✔
1064
    int64_t tpackets = 0;
20✔
1065
    int64_t nouse1 = 0;
20✔
1066
    int64_t nouse2 = 0;
20✔
1067
    int64_t nouse3 = 0;
20✔
1068
    int64_t nouse4 = 0;
20✔
1069
    int64_t nouse5 = 0;
20✔
1070
    int64_t nouse6 = 0;
20✔
1071
    char    nouse0[200] = {0};
20✔
1072

1073
    _bytes = taosGetsFile(pFile, sizeof(line), line);
20✔
1074
    if (_bytes <= 0) {
20✔
1075
      break;
4✔
1076
    }
1077

1078
    line[_bytes - 1] = 0;
16✔
1079

1080
    if (strstr(line, "lo:") != NULL) {
16✔
1081
      continue;
4✔
1082
    }
1083

1084
    (void)sscanf(line,
12✔
1085
           "%s %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64
1086
           " %" PRId64,
1087
           nouse0, &o_rbytes, &rpackts, &nouse1, &nouse2, &nouse3, &nouse4, &nouse5, &nouse6, &o_tbytes, &tpackets);
1088
    *receive_bytes += o_rbytes;
12✔
1089
    *transmit_bytes += o_tbytes;
12✔
1090
  }
1091

1092
  TAOS_SKIP_ERROR(taosCloseFile(&pFile));
4✔
1093

1094
  return 0;
4✔
1095
#endif
1096
}
1097

1098
int32_t taosGetCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes) {
13✔
1099
  OS_PARAM_CHECK(receive_bytes);
13✔
1100
  OS_PARAM_CHECK(transmit_bytes);
7✔
1101
  static int64_t last_receive_bytes = -1;
1102
  static int64_t last_transmit_bytes = -1;
1103
  int64_t cur_receive_bytes = 0;
4✔
1104
  int64_t cur_transmit_bytes = 0;
4✔
1105
  int32_t code = taosGetCardInfo(&cur_receive_bytes, &cur_transmit_bytes);
4✔
1106
  if (code == 0) {
4!
1107
    if(last_receive_bytes >= 0 && last_transmit_bytes >= 0){
4!
1108
      *receive_bytes = cur_receive_bytes - last_receive_bytes;
×
1109
      *transmit_bytes = cur_transmit_bytes - last_transmit_bytes;
×
1110
    }
1111
    else{
1112
      *receive_bytes = 0;
4✔
1113
      *transmit_bytes = 0;
4✔
1114
    }
1115

1116
    last_receive_bytes = cur_receive_bytes;
4✔
1117
    last_transmit_bytes = cur_transmit_bytes;
4✔
1118
  } else {
1119
    return code;
×
1120
  }
1121
  return 0;
4✔
1122
}
1123
void taosSetDefaultCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes) {
×
1124
  if (receive_bytes) *receive_bytes = 0;
×
1125
  if (transmit_bytes) *transmit_bytes = 0;
×
1126
}
×
1127

1128
#if 0
1129
void taosKillSystem() {
1130
#ifdef WINDOWS
1131
  printf("function taosKillSystem, exit!");
1132
  exit(0);
1133
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA)
1134
  printf("function taosKillSystem, exit!");
1135
  exit(0);
1136
#else
1137
  // SIGINT
1138
  (void)printf("%sd will shut down soon", CUS_PROMPT);
1139
  (void)kill(tsProcId, 2);
1140
#endif
1141
}
1142
#endif
1143

1144
#define UUIDLEN (36)
1145
int32_t taosGetSystemUUIDLimit36(char *uid, int32_t uidlen) {
35,532✔
1146
  OS_PARAM_CHECK(uid);
35,532✔
1147
#ifdef WINDOWS
1148
  GUID guid;
1149
  HRESULT h = CoCreateGuid(&guid);
1150
  if (h != S_OK) {
1151
    return TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
1152
  }
1153
  (void)snprintf(uid, uidlen, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", guid.Data1, guid.Data2, guid.Data3,
1154
           guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6],
1155
           guid.Data4[7]);
1156

1157
  return 0;
1158
#elif defined(_TD_DARWIN_64)
1159
  uuid_t uuid = {0};
1160
  char   buf[UUIDLEN37];
1161
  memset(buf, 0, UUIDLEN37);
1162
  uuid_generate(uuid);
1163
  // it's caller's responsibility to make enough space for `uid`, that's 36-char + 1-null
1164
  uuid_unparse_lower(uuid, buf);
1165
  (void)snprintf(uid, uidlen, "%.*s", (int)sizeof(buf), buf);
1166
  return 0;
1167
#elif defined(TD_ASTRA)
1168
  const char *template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
1169
  const char *hex_chars = "0123456789abcdef";
1170
  int32_t     len = uidlen > 36 ? 36 : uidlen;
1171

1172
  for (int32_t i = 0; i < len; i++) {
1173
    if (template[i] == 'x') {
1174
      uid[i] = hex_chars[taosRand() & 15];
1175
    } else if (template[i] == 'y') {
1176
      uid[i] = hex_chars[(taosRand() & 3) + 8];  // 8, 9, a, or b
1177
    } else {
1178
      uid[i] = template[i];
1179
    }
1180
  }
1181
  if (len >= 0) {
1182
    uid[len] = 0;
1183
  }
1184

1185
  return 0;
1186
#else
1187
  int64_t len = 0;
35,529✔
1188

1189
  // fd = open("/proc/sys/kernel/random/uuid", 0);
1190
  TdFilePtr pFile = taosOpenFile("/proc/sys/kernel/random/uuid", TD_FILE_READ);
35,529✔
1191
  if (pFile == NULL) {
35,534!
1192
    return terrno;
×
1193
  } else {
1194
    len = taosReadFile(pFile, uid, uidlen);
35,534✔
1195
    TAOS_SKIP_ERROR(taosCloseFile(&pFile));
35,574✔
1196
    if (len < 0) {
35,598!
1197
      return terrno;
×
1198
    }
1199
  }
1200
  if (len >= UUIDLEN + 1) {
35,598✔
1201
    uid[len - 1] = 0;
34,128✔
1202
  } else {
1203
    uid[uidlen - 1] = 0;
1,470✔
1204
  }
1205

1206
  return 0;
35,598✔
1207
#endif
1208
}
1209

1210
int32_t taosGetSystemUUIDLen(char *uid, int32_t uidlen) {
1,461✔
1211
  if (uid == NULL || uidlen <= 0) {
1,461✔
1212
    return TSDB_CODE_APP_ERROR;
6✔
1213
  }
1214
  int num = (uidlen % UUIDLEN == 0) ? (uidlen / UUIDLEN) : (uidlen / UUIDLEN + 1);
1,455✔
1215
  int left = uidlen;
1,455✔
1216
  for (int i = 0; i < num; ++i) {
4,353✔
1217
    int32_t code = taosGetSystemUUIDLimit36(uid + i * UUIDLEN, left);
2,898✔
1218
    if (code != 0) {
2,898!
1219
      return code;
×
1220
    }
1221
    left -= UUIDLEN;
2,898✔
1222
  }
1223
  return TSDB_CODE_SUCCESS;
1,455✔
1224
}
1225

1226
char *taosGetCmdlineByPID(int pid) {
2,365✔
1227
#ifdef WINDOWS
1228
  return "";
1229
#elif defined(_TD_DARWIN_64)
1230
  static char cmdline[1024];
1231
  SET_ERRNO(0);
1232

1233
  if (proc_pidpath(pid, cmdline, sizeof(cmdline)) <= 0) {
1234
    fprintf(stderr, "PID is %d, %s", pid, strerror(ERRNO));
1235
    return strerror(ERRNO);
1236
  }
1237

1238
  return cmdline;
1239
#elif defined(TD_ASTRA)
1240
  return "";
1241
#else
1242
  static char cmdline[1024];
1243
  (void)snprintf(cmdline, sizeof(cmdline), "/proc/%d/cmdline", pid);
2,365✔
1244

1245
  // int fd = open(cmdline, O_RDONLY);
1246
  TdFilePtr pFile = taosOpenFile(cmdline, TD_FILE_READ);
2,365✔
1247
  if (pFile != NULL) {
2,365✔
1248
    int n = taosReadFile(pFile, cmdline, sizeof(cmdline) - 1);
2,353✔
1249
    if (n < 0) n = 0;
2,353✔
1250

1251
    if (n > 0 && cmdline[n - 1] == '\n') --n;
2,353!
1252

1253
    cmdline[n] = 0;
2,353✔
1254

1255
    TAOS_SKIP_ERROR(taosCloseFile(&pFile));
2,353✔
1256
  } else {
1257
    cmdline[0] = 0;
12✔
1258
  }
1259

1260
  return cmdline;
2,365✔
1261
#endif
1262
}
1263

1264
int64_t taosGetOsUptime() {
3,187✔
1265
#ifdef WINDOWS
1266
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA)
1267
#else
1268
  struct sysinfo info;
1269
  if (-1 == sysinfo(&info)) {
3,187!
1270
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1271
    return terrno;
×
1272
  }
1273
  
1274
  return (int64_t)info.uptime * 1000;
3,187✔
1275
  
1276
#endif
1277
  return 0;
1278
}
1279

1280
void taosSetCoreDump(bool enable) {
8,718✔
1281
  if (!enable) return;
8,718✔
1282
#ifdef WINDOWS
1283
  SetUnhandledExceptionFilter(exceptionHandler);
1284
  SetUnhandledExceptionFilter(&FlCrashDump);
1285
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA)
1286
#else
1287
  // 1. set ulimit -c unlimited
1288
  struct rlimit rlim;
1289
  struct rlimit rlim_new;
1290
  if (getrlimit(RLIMIT_CORE, &rlim) == 0) {
8,703!
1291
#ifndef _ALPINE
1292
    // printf("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max);
1293
#else
1294
    // printf("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max);
1295
#endif
1296
    rlim_new.rlim_cur = RLIM_INFINITY;
8,703✔
1297
    rlim_new.rlim_max = RLIM_INFINITY;
8,703✔
1298
    if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) {
8,703!
1299
      // printf("set unlimited fail, error: %s", strerror(ERRNO));
1300
      rlim_new.rlim_cur = rlim.rlim_max;
×
1301
      rlim_new.rlim_max = rlim.rlim_max;
×
1302
      (void)setrlimit(RLIMIT_CORE, &rlim_new);
×
1303
    }
1304
  }
1305

1306
  if (getrlimit(RLIMIT_CORE, &rlim) == 0) {
8,703✔
1307
#ifndef _ALPINE
1308
    // printf("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max);
1309
#else
1310
    // printf("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max);
1311
#endif
1312
  }
1313

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

1318
  int    old_usespid = 0;
8,703✔
1319
  size_t old_len = 0;
8,703✔
1320
  int    new_usespid = 1;
8,703✔
1321
  size_t new_len = sizeof(new_usespid);
8,703✔
1322

1323
  int name[] = {CTL_KERN, KERN_CORE_USES_PID};
8,703✔
1324

1325
  (void)memset(&args, 0, sizeof(struct __sysctl_args));
8,703✔
1326
  args.name = name;
8,703✔
1327
  args.nlen = sizeof(name) / sizeof(name[0]);
8,703✔
1328
  args.oldval = &old_usespid;
8,703✔
1329
  args.oldlenp = &old_len;
8,703✔
1330
  args.newval = &new_usespid;
8,703✔
1331
  args.newlen = new_len;
8,703✔
1332

1333
  old_len = sizeof(old_usespid);
8,703✔
1334

1335
#ifndef __loongarch64
1336
  if (syscall(SYS__sysctl, &args) == -1) {
8,703✔
1337
    // printf("_sysctl(kern_core_uses_pid) set fail: %s", strerror(ERRNO));
1338
  }
1339
#endif
1340

1341
  // printf("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid);
1342

1343
  old_usespid = 0;
8,703✔
1344
  old_len = 0;
8,703✔
1345
  (void)memset(&args, 0, sizeof(struct __sysctl_args));
8,703✔
1346
  args.name = name;
8,703✔
1347
  args.nlen = sizeof(name) / sizeof(name[0]);
8,703✔
1348
  args.oldval = &old_usespid;
8,703✔
1349
  args.oldlenp = &old_len;
8,703✔
1350

1351
  old_len = sizeof(old_usespid);
8,703✔
1352

1353
#ifndef __loongarch64
1354
  if (syscall(SYS__sysctl, &args) == -1) {
8,703✔
1355
    // printf("_sysctl(kern_core_uses_pid) get fail: %s", strerror(ERRNO));
1356
  }
1357
#endif
1358

1359
  // printf("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid);
1360
#endif
1361
#endif
1362
}
1363

1364
SysNameInfo taosGetSysNameInfo() {
6,822✔
1365
#ifdef WINDOWS
1366
  SysNameInfo info = {0};
1367
  DWORD       dwVersion = GetVersion();
1368

1369
  char *tmp = NULL;
1370
  tmp = getenv("OS");
1371
  if (tmp != NULL) tstrncpy(info.sysname, tmp, sizeof(info.sysname));
1372
  tmp = getenv("COMPUTERNAME");
1373
  if (tmp != NULL) tstrncpy(info.nodename, tmp, sizeof(info.nodename));
1374
  sprintf_s(info.release, sizeof(info.release), "%d", dwVersion & 0x0F);
1375
  sprintf_s(info.version, sizeof(info.release), "%d", (dwVersion >> 8) & 0x0F);
1376
  tmp = getenv("PROCESSOR_ARCHITECTURE");
1377
  if (tmp != NULL) tstrncpy(info.machine, tmp, sizeof(info.machine));
1378

1379
  return info;
1380
#elif defined(_TD_DARWIN_64)
1381
  SysNameInfo info = {0};
1382

1383
  struct utsname uts;
1384
  if (!uname(&uts)) {
1385
    tstrncpy(info.sysname, uts.sysname, sizeof(info.sysname));
1386
    tstrncpy(info.nodename, uts.nodename, sizeof(info.nodename));
1387
    tstrncpy(info.release, uts.release, sizeof(info.release));
1388
    tstrncpy(info.version, uts.version, sizeof(info.version));
1389
    tstrncpy(info.machine, uts.machine, sizeof(info.machine));
1390
  }
1391

1392
  char     localHostName[512];
1393
  TAOS_SKIP_ERROR(taosGetlocalhostname(localHostName, 512));
1394
  TdCmdPtr pCmd = taosOpenCmd("scutil --get LocalHostName");
1395
  tstrncpy(info.nodename, localHostName, sizeof(info.nodename));
1396

1397
  return info;
1398
#else
1399
  SysNameInfo info = {0};
6,822✔
1400
  struct utsname uts;
1401
  if (!uname(&uts)) {
6,822!
1402
    tstrncpy(info.sysname, uts.sysname, sizeof(info.sysname));
6,822✔
1403
    tstrncpy(info.nodename, uts.nodename, sizeof(info.nodename));
6,822✔
1404
    tstrncpy(info.release, uts.release, sizeof(info.release));
6,822✔
1405
    tstrncpy(info.version, uts.version, sizeof(info.version));
6,822✔
1406
    tstrncpy(info.machine, uts.machine, sizeof(info.machine));
6,822✔
1407
  } else {
1408
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1409
  }
1410

1411
  return info;
6,822✔
1412
#endif
1413
}
1414

1415
bool taosCheckCurrentInDll() {
2,125✔
1416
#ifdef WINDOWS
1417
  MEMORY_BASIC_INFORMATION mbi;
1418
  char                     path[PATH_MAX] = {0};
1419
  GetModuleFileName(
1420
      ((VirtualQuery(taosCheckCurrentInDll, &mbi, sizeof(mbi)) != 0) ? (HMODULE)mbi.AllocationBase : NULL), path,
1421
      PATH_MAX);
1422
  int strLastIndex = strlen(path);
1423
  if ((path[strLastIndex - 3] == 'd' || path[strLastIndex - 3] == 'D') &&
1424
      (path[strLastIndex - 2] == 'l' || path[strLastIndex - 2] == 'L') &&
1425
      (path[strLastIndex - 1] == 'l' || path[strLastIndex - 1] == 'L')) {
1426
    return true;
1427
  }
1428
  return false;
1429
#else
1430
  return false;
2,125✔
1431
#endif
1432
}
1433

1434
#ifdef _TD_DARWIN_64
1435
int32_t taosGetMaclocalhostnameByCommand(char *hostname, size_t maxLen) {
1436
  TdCmdPtr pCmd = taosOpenCmd("scutil --get LocalHostName");
1437
  if (pCmd != NULL) {
1438
    if (taosGetsCmd(pCmd, maxLen - 1, hostname) > 0) {
1439
      int len = strlen(hostname);
1440
      if (hostname[len - 1] == '\n') {
1441
        hostname[len - 1] = '\0';
1442
      }
1443
      return 0;
1444
    }
1445
    taosCloseCmd(&pCmd);
1446
  }
1447
  return TAOS_SYSTEM_ERROR(ERRNO);
1448
}
1449

1450
int32_t getMacLocalHostNameBySCD(char *hostname, size_t maxLen) {
1451
  SCDynamicStoreRef store = SCDynamicStoreCreate(NULL, CFSTR(""), NULL, NULL);
1452
  CFStringRef       hostname_cfstr = SCDynamicStoreCopyLocalHostName(store);
1453
  if (hostname_cfstr != NULL) {
1454
    CFStringGetCString(hostname_cfstr, hostname, maxLen - 1, kCFStringEncodingMacRoman);
1455
    CFRelease(hostname_cfstr);
1456
  } else {
1457
    return -1;
1458
  }
1459
  CFRelease(store);
1460
  return 0;
1461
}
1462
#endif
1463

1464
int32_t taosGetlocalhostname(char *hostname, size_t maxLen) {
9,741✔
1465
  OS_PARAM_CHECK(hostname);
9,741✔
1466
#ifdef _TD_DARWIN_64
1467
  int res = getMacLocalHostNameBySCD(hostname, maxLen);
1468
  if (res != 0) {
1469
    return taosGetMaclocalhostnameByCommand(hostname, maxLen);
1470
  } else {
1471
    return 0;
1472
  }
1473
#else
1474
  int r = gethostname(hostname, maxLen);
9,738✔
1475
  if (-1 == r) {
9,738!
1476
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1477
    return terrno;
×
1478
  }
1479
  return r;
9,738✔
1480
#endif
1481
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc