tlog.c 21.1 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
slguan 已提交
16
#define _DEFAULT_SOURCE
H
hzcheng 已提交
17
#include "tlog.h"
L
Liu Jicong 已提交
18
#include "os.h"
S
TD-2371  
Shengliang Guan 已提交
19
#include "tnote.h"
H
hzcheng 已提交
20
#include "tutil.h"
L
Liu Jicong 已提交
21 22 23 24 25 26 27
#include "ulog.h"

#define MAX_LOGLINE_SIZE (1000)
#define MAX_LOGLINE_BUFFER_SIZE (MAX_LOGLINE_SIZE + 10)
#define MAX_LOGLINE_CONTENT_SIZE (MAX_LOGLINE_SIZE - 100)
#define MAX_LOGLINE_DUMP_SIZE (65 * 1024)
#define MAX_LOGLINE_DUMP_BUFFER_SIZE (MAX_LOGLINE_DUMP_SIZE + 10)
S
slguan 已提交
28 29
#define MAX_LOGLINE_DUMP_CONTENT_SIZE (MAX_LOGLINE_DUMP_SIZE - 100)

L
Liu Jicong 已提交
30
#define LOG_FILE_NAME_LEN 300
D
fix bug  
dapan1121 已提交
31
#define TSDB_DEFAULT_LOG_BUF_SIZE (20 * 1024 * 1024)  // 20MB
H
hzcheng 已提交
32

D
fix bug  
dapan1121 已提交
33 34 35 36 37
#define DEFAULT_LOG_INTERVAL 25
#define LOG_INTERVAL_STEP 5
#define MIN_LOG_INTERVAL 5
#define MAX_LOG_INTERVAL 25
#define LOG_MAX_WAIT_MSEC 1000
D
fix bug  
dapan1121 已提交
38

S
slguan 已提交
39
#define LOG_BUF_BUFFER(x) ((x)->buffer)
L
Liu Jicong 已提交
40 41 42 43
#define LOG_BUF_START(x) ((x)->buffStart)
#define LOG_BUF_END(x) ((x)->buffEnd)
#define LOG_BUF_SIZE(x) ((x)->buffSize)
#define LOG_BUF_MUTEX(x) ((x)->buffMutex)
S
slguan 已提交
44

H
hzcheng 已提交
45
typedef struct {
L
Liu Jicong 已提交
46
  char           *buffer;
S
slguan 已提交
47 48 49
  int32_t         buffStart;
  int32_t         buffEnd;
  int32_t         buffSize;
D
fix bu  
dapan1121 已提交
50
  int32_t         minBuffSize;
S
slguan 已提交
51 52
  int32_t         fd;
  int32_t         stop;
H
hzcheng 已提交
53 54
  pthread_t       asyncThread;
  pthread_mutex_t buffMutex;
S
slguan 已提交
55
  tsem_t          buffNotEmpty;
H
hzcheng 已提交
56 57
} SLogBuff;

S
slguan 已提交
58
typedef struct {
L
Liu Jicong 已提交
59 60 61 62 63 64 65 66
  int32_t         fileNum;
  int32_t         maxLines;
  int32_t         lines;
  int32_t         flag;
  int32_t         openInProgress;
  pid_t           pid;
  char            logName[LOG_FILE_NAME_LEN];
  SLogBuff       *logHandle;
S
slguan 已提交
67 68 69
  pthread_mutex_t logMutex;
} SLogObj;

L
Liu Jicong 已提交
70
int8_t tscEmbeddedInUtil = 0;
L
Liu Jicong 已提交
71

72
int32_t tsLogKeepDays = 0;
S
Shengliang Guan 已提交
73
bool    tsAsyncLog = true;
S
Shengliang Guan 已提交
74
bool    tsLogInited = false;
D
fix bug  
dapan1121 已提交
75
int64_t asyncLogLostLines = 0;
D
fix bug  
dapan1121 已提交
76
int32_t writeInterval = DEFAULT_LOG_INTERVAL;
D
fix bug  
dapan1121 已提交
77

S
Shengliang Guan 已提交
78 79 80 81
// log
int32_t tsNumOfLogLines = 10000000;
int32_t dDebugFlag = 135;
int32_t vDebugFlag = 135;
S
Shengliang Guan 已提交
82
int32_t mDebugFlag = 131;
S
Shengliang Guan 已提交
83 84
int32_t cDebugFlag = 131;
int32_t jniDebugFlag = 131;
S
Shengliang Guan 已提交
85
int32_t tmrDebugFlag = 131;
S
Shengliang Guan 已提交
86
int32_t uDebugFlag = 131;
S
Shengliang Guan 已提交
87 88
int32_t rpcDebugFlag = 131;
int32_t qDebugFlag = 131;
S
Shengliang Guan 已提交
89
int32_t wDebugFlag = 135;
S
Shengliang Guan 已提交
90
int32_t sDebugFlag = 135;
S
Shengliang Guan 已提交
91
int32_t tsdbDebugFlag = 131;
L
Liu Jicong 已提交
92
int32_t tqDebugFlag = 135;
S
Shengliang Guan 已提交
93
int32_t fsDebugFlag = 135;
D
dapan1121 已提交
94

D
fix bug  
dapan1121 已提交
95
int64_t dbgEmptyW = 0;
D
fix bug  
dapan1121 已提交
96 97 98
int64_t dbgWN = 0;
int64_t dbgSmallWN = 0;
int64_t dbgBigWN = 0;
D
fix bug  
dapan1121 已提交
99 100
int64_t dbgWSize = 0;

L
Liu Jicong 已提交
101 102
static SLogObj   tsLogObj = {.fileNum = 1};
static void     *taosAsyncOutputLog(void *param);
S
slguan 已提交
103 104 105 106
static int32_t   taosPushLogBuffer(SLogBuff *tLogBuff, char *msg, int32_t msgLen);
static SLogBuff *taosLogBuffNew(int32_t bufSize);
static void      taosCloseLogByFd(int32_t oldFd);
static int32_t   taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum);
107
extern void      taosPrintCfg();
S
Shengliang Guan 已提交
108
static int32_t   taosCompressFile(char *srcFileName, char *destFileName);
S
slguan 已提交
109 110

static int32_t taosStartLog() {
H
hzcheng 已提交
111 112
  pthread_attr_t threadAttr;
  pthread_attr_init(&threadAttr);
S
slguan 已提交
113
  if (pthread_create(&(tsLogObj.logHandle->asyncThread), &threadAttr, taosAsyncOutputLog, tsLogObj.logHandle) != 0) {
H
hzcheng 已提交
114 115 116 117 118 119
    return -1;
  }
  pthread_attr_destroy(&threadAttr);
  return 0;
}

S
Shengliang Guan 已提交
120
int32_t taosInitLog(const char *logName, int maxFiles) {
S
Shengliang Guan 已提交
121 122
  if (tsLogInited) return 0;

S
Shengliang Guan 已提交
123 124 125
  char fullName[PATH_MAX] = {0};
  snprintf(fullName, PATH_MAX, "%s" TD_DIRSEP "%s", tsLogDir, logName);

S
slguan 已提交
126 127
  tsLogObj.logHandle = taosLogBuffNew(TSDB_DEFAULT_LOG_BUF_SIZE);
  if (tsLogObj.logHandle == NULL) return -1;
S
Shengliang Guan 已提交
128
  if (taosOpenLogFile(fullName, tsNumOfLogLines, maxFiles) < 0) return -1;
H
hzcheng 已提交
129
  if (taosStartLog() < 0) return -1;
S
Shengliang Guan 已提交
130
  tsLogInited = true;
H
hzcheng 已提交
131 132 133
  return 0;
}

S
slguan 已提交
134 135 136 137
static void taosStopLog() {
  if (tsLogObj.logHandle) {
    tsLogObj.logHandle->stop = 1;
  }
H
hzcheng 已提交
138 139
}

S
slguan 已提交
140
void taosCloseLog() {
H
hzcheng 已提交
141
  taosStopLog();
L
Liu Jicong 已提交
142 143
  // tsem_post(&(tsLogObj.logHandle->buffNotEmpty));
  taosMsleep(MAX_LOG_INTERVAL / 1000);
S
slguan 已提交
144 145
  if (taosCheckPthreadValid(tsLogObj.logHandle->asyncThread)) {
    pthread_join(tsLogObj.logHandle->asyncThread, NULL);
S
slguan 已提交
146
  }
S
slguan 已提交
147 148 149
  // In case that other threads still use log resources causing invalid write in valgrind
  // we comment two lines below.
  // taosLogBuffDestroy(tsLogObj.logHandle);
H
hzcheng 已提交
150 151 152
  // taosCloseLog();
}

S
Shengliang Guan 已提交
153
static bool taosLockLogFile(int32_t fd) {
H
hzcheng 已提交
154 155
  if (fd < 0) return false;

S
slguan 已提交
156
  if (tsLogObj.fileNum > 1) {
157
    int32_t ret = taosLockFile(fd);
H
hzcheng 已提交
158 159 160 161 162 163 164 165
    if (ret == 0) {
      return true;
    }
  }

  return false;
}

S
Shengliang Guan 已提交
166
static void taosUnLockLogFile(int32_t fd) {
H
hzcheng 已提交
167 168
  if (fd < 0) return;

S
slguan 已提交
169
  if (tsLogObj.fileNum > 1) {
S
Shengliang Guan 已提交
170
    taosUnLockFile(fd);
H
hzcheng 已提交
171 172 173
  }
}

S
TD-1263  
Shengliang Guan 已提交
174
static void taosKeepOldLog(char *oldName) {
S
TD-1574  
Shengliang Guan 已提交
175
  if (tsLogKeepDays == 0) return;
S
TD-1263  
Shengliang Guan 已提交
176

S
TD-1263  
Shengliang Guan 已提交
177
  int64_t fileSec = taosGetTimestampSec();
S
TD-1263  
Shengliang Guan 已提交
178
  char    fileName[LOG_FILE_NAME_LEN + 20];
S
TD-1263  
Shengliang Guan 已提交
179
  snprintf(fileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64, tsLogObj.logName, fileSec);
S
TD-1263  
Shengliang Guan 已提交
180

S
Shengliang Guan 已提交
181
  taosRenameFile(oldName, fileName);
S
TD-1574  
Shengliang Guan 已提交
182 183 184 185 186 187 188 189
  if (tsLogKeepDays < 0) {
    char compressFileName[LOG_FILE_NAME_LEN + 20];
    snprintf(compressFileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64 ".gz", tsLogObj.logName, fileSec);
    if (taosCompressFile(fileName, compressFileName) == 0) {
      (void)remove(fileName);
    }
  }

dengyihao's avatar
dengyihao 已提交
190
  taosRemoveOldFiles(tsLogDir, TABS(tsLogKeepDays));
S
TD-1263  
Shengliang Guan 已提交
191 192
}

S
slguan 已提交
193
static void *taosThreadToOpenNewFile(void *param) {
S
TD-1263  
Shengliang Guan 已提交
194 195
  char keepName[LOG_FILE_NAME_LEN + 20];
  sprintf(keepName, "%s.%d", tsLogObj.logName, tsLogObj.flag);
H
hzcheng 已提交
196

S
slguan 已提交
197 198
  tsLogObj.flag ^= 1;
  tsLogObj.lines = 0;
S
TD-1263  
Shengliang Guan 已提交
199
  char name[LOG_FILE_NAME_LEN + 20];
S
slguan 已提交
200
  sprintf(name, "%s.%d", tsLogObj.logName, tsLogObj.flag);
H
hzcheng 已提交
201

S
Shengliang Guan 已提交
202
  taosUmaskFile(0);
H
hzcheng 已提交
203

204
  int32_t fd = taosOpenFileCreateWriteTrunc(name);
H
Hui Li 已提交
205
  if (fd < 0) {
206 207 208
    tsLogObj.openInProgress = 0;
    tsLogObj.lines = tsLogObj.maxLines - 1000;
    uError("open new log file fail! fd:%d reason:%s, reuse lastlog", fd, strerror(errno));
H
Hui Li 已提交
209 210
    return NULL;
  }
S
TD-1263  
Shengliang Guan 已提交
211

S
Shengliang Guan 已提交
212 213
  taosLockLogFile(fd);
  (void)taosLSeekFile(fd, 0, SEEK_SET);
H
hzcheng 已提交
214

S
slguan 已提交
215 216 217 218
  int32_t oldFd = tsLogObj.logHandle->fd;
  tsLogObj.logHandle->fd = fd;
  tsLogObj.lines = 0;
  tsLogObj.openInProgress = 0;
S
TD-1263  
Shengliang Guan 已提交
219
  taosCloseLogByFd(oldFd);
L
Liu Jicong 已提交
220

S
TD-1263  
Shengliang Guan 已提交
221 222
  uInfo("   new log file:%d is opened", tsLogObj.flag);
  uInfo("==================================");
S
config  
Shengliang Guan 已提交
223
  // taosPrintCfg();
S
TD-1263  
Shengliang Guan 已提交
224 225
  taosKeepOldLog(keepName);

H
hzcheng 已提交
226 227 228
  return NULL;
}

S
slguan 已提交
229 230
static int32_t taosOpenNewLogFile() {
  pthread_mutex_lock(&tsLogObj.logMutex);
H
hzcheng 已提交
231

S
slguan 已提交
232 233
  if (tsLogObj.lines > tsLogObj.maxLines && tsLogObj.openInProgress == 0) {
    tsLogObj.openInProgress = 1;
H
hzcheng 已提交
234

235
    uInfo("open new log file ......");
H
hzcheng 已提交
236 237 238 239 240 241 242 243 244
    pthread_t      thread;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    pthread_create(&thread, &attr, taosThreadToOpenNewFile, NULL);
    pthread_attr_destroy(&attr);
  }

S
slguan 已提交
245
  pthread_mutex_unlock(&tsLogObj.logMutex);
H
hzcheng 已提交
246 247 248 249

  return 0;
}

S
slguan 已提交
250 251 252
void taosResetLog() {
  char lastName[LOG_FILE_NAME_LEN + 20];
  sprintf(lastName, "%s.%d", tsLogObj.logName, tsLogObj.flag);
H
hzcheng 已提交
253 254

  // force create a new log file
S
slguan 已提交
255
  tsLogObj.lines = tsLogObj.maxLines + 10;
H
hzcheng 已提交
256 257

  taosOpenNewLogFile();
H
Hui Li 已提交
258
  (void)remove(lastName);
H
hzcheng 已提交
259

260 261
  uInfo("==================================");
  uInfo("   reset log file ");
H
hzcheng 已提交
262 263
}

S
slguan 已提交
264
static bool taosCheckFileIsOpen(char *logFileName) {
S
Shengliang Guan 已提交
265
  int32_t fd = taosOpenFileWrite(logFileName);
H
hzcheng 已提交
266
  if (fd < 0) {
267 268 269 270 271 272
    if (errno == ENOENT) {
      return false;
    } else {
      printf("\nfailed to open log file:%s, reason:%s\n", logFileName, strerror(errno));
      return true;
    }
H
hzcheng 已提交
273 274
  }

S
Shengliang Guan 已提交
275 276 277
  if (taosLockLogFile(fd)) {
    taosUnLockLogFile(fd);
    taosCloseFile(fd);
H
hzcheng 已提交
278 279
    return false;
  } else {
S
Shengliang Guan 已提交
280
    taosCloseFile(fd);
H
hzcheng 已提交
281 282 283 284
    return true;
  }
}

S
slguan 已提交
285 286 287
static void taosGetLogFileName(char *fn) {
  if (tsLogObj.fileNum > 1) {
    for (int32_t i = 0; i < tsLogObj.fileNum; i++) {
H
hzcheng 已提交
288 289 290 291 292 293 294 295 296
      char fileName[LOG_FILE_NAME_LEN];

      sprintf(fileName, "%s%d.0", fn, i);
      bool file1open = taosCheckFileIsOpen(fileName);

      sprintf(fileName, "%s%d.1", fn, i);
      bool file2open = taosCheckFileIsOpen(fileName);

      if (!file1open && !file2open) {
S
slguan 已提交
297
        sprintf(tsLogObj.logName, "%s%d", fn, i);
H
hzcheng 已提交
298 299 300 301 302
        return;
      }
    }
  }

H
Hui Li 已提交
303 304 305
  if (strlen(fn) < LOG_FILE_NAME_LEN) {
    strcpy(tsLogObj.logName, fn);
  }
H
hzcheng 已提交
306 307
}

S
slguan 已提交
308
static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) {
S
slguan 已提交
309 310
#ifdef WINDOWS
  /*
L
Liu Jicong 已提交
311 312 313
   * always set maxFileNum to 1
   * means client log filename is unique in windows
   */
S
slguan 已提交
314 315 316
  maxFileNum = 1;
#endif

S
Shengliang Guan 已提交
317 318 319
  char    name[LOG_FILE_NAME_LEN + 50] = "\0";
  int32_t logstat0_mtime, logstat1_mtime;
  int32_t size;
H
hzcheng 已提交
320

S
slguan 已提交
321 322
  tsLogObj.maxLines = maxLines;
  tsLogObj.fileNum = maxFileNum;
H
hzcheng 已提交
323 324
  taosGetLogFileName(fn);

H
Hui Li 已提交
325 326 327 328
  if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) {
    strcpy(name, fn);
    strcat(name, ".0");
  }
S
Shengliang Guan 已提交
329
  bool log0Exist = taosStatFile(name, NULL, &logstat0_mtime) >= 0;
H
hzcheng 已提交
330

S
TD-1263  
Shengliang Guan 已提交
331 332 333 334
  if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) {
    strcpy(name, fn);
    strcat(name, ".1");
  }
S
Shengliang Guan 已提交
335 336
  bool log1Exist = taosStatFile(name, NULL, &logstat1_mtime) >= 0;

H
hzcheng 已提交
337
  // if none of the log files exist, open 0, if both exists, open the old one
S
TD-1263  
Shengliang Guan 已提交
338 339 340
  if (!log0Exist && !log1Exist) {
    tsLogObj.flag = 0;
  } else if (!log1Exist) {
S
slguan 已提交
341
    tsLogObj.flag = 0;
S
TD-1263  
Shengliang Guan 已提交
342 343
  } else if (!log0Exist) {
    tsLogObj.flag = 1;
H
hzcheng 已提交
344
  } else {
S
Shengliang Guan 已提交
345
    tsLogObj.flag = (logstat0_mtime > logstat1_mtime) ? 0 : 1;
H
hzcheng 已提交
346 347
  }

H
Hui Li 已提交
348 349
  char fileName[LOG_FILE_NAME_LEN + 50] = "\0";
  sprintf(fileName, "%s.%d", tsLogObj.logName, tsLogObj.flag);
S
slguan 已提交
350
  pthread_mutex_init(&tsLogObj.logMutex, NULL);
H
hzcheng 已提交
351

S
Shengliang Guan 已提交
352 353
  taosUmaskFile(0);
  tsLogObj.logHandle->fd = taosOpenFileCreateWrite(fileName);
H
hzcheng 已提交
354

S
slguan 已提交
355
  if (tsLogObj.logHandle->fd < 0) {
H
Hui Li 已提交
356
    printf("\nfailed to open log file:%s, reason:%s\n", fileName, strerror(errno));
H
hzcheng 已提交
357 358
    return -1;
  }
S
Shengliang Guan 已提交
359
  taosLockLogFile(tsLogObj.logHandle->fd);
H
hzcheng 已提交
360 361

  // only an estimate for number of lines
S
Shengliang Guan 已提交
362 363
  int64_t filesize = 0;
  if (taosFStatFile(tsLogObj.logHandle->fd, &filesize, NULL) < 0) {
H
Hui Li 已提交
364
    printf("\nfailed to fstat log file:%s, reason:%s\n", fileName, strerror(errno));
H
Hui Li 已提交
365 366
    return -1;
  }
S
Shengliang Guan 已提交
367
  size = (int32_t)filesize;
S
slguan 已提交
368
  tsLogObj.lines = size / 60;
H
hzcheng 已提交
369

S
Shengliang Guan 已提交
370
  taosLSeekFile(tsLogObj.logHandle->fd, 0, SEEK_END);
H
hzcheng 已提交
371 372

  sprintf(name, "==================================================\n");
S
Shengliang Guan 已提交
373
  taosWriteFile(tsLogObj.logHandle->fd, name, (uint32_t)strlen(name));
H
hzcheng 已提交
374
  sprintf(name, "                new log file                      \n");
S
Shengliang Guan 已提交
375
  taosWriteFile(tsLogObj.logHandle->fd, name, (uint32_t)strlen(name));
H
hzcheng 已提交
376
  sprintf(name, "==================================================\n");
S
Shengliang Guan 已提交
377
  taosWriteFile(tsLogObj.logHandle->fd, name, (uint32_t)strlen(name));
H
hzcheng 已提交
378 379 380 381

  return 0;
}

B
Bomin Zhang 已提交
382
void taosPrintLog(const char *flags, int32_t dflag, const char *format, ...) {
S
Shengliang Guan 已提交
383
  if (!taosLogSpaceAvailable()) return;
S
slguan 已提交
384

H
hzcheng 已提交
385
  va_list        argpointer;
L
Liu Jicong 已提交
386
  char           buffer[MAX_LOGLINE_BUFFER_SIZE] = {0};
S
slguan 已提交
387
  int32_t        len;
H
hzcheng 已提交
388 389 390 391
  struct tm      Tm, *ptm;
  struct timeval timeSecs;
  time_t         curTime;

S
Shengliang Guan 已提交
392
  taosGetTimeOfDay(&timeSecs);
H
hzcheng 已提交
393 394
  curTime = timeSecs.tv_sec;
  ptm = localtime_r(&curTime, &Tm);
S
slguan 已提交
395

396
  len = sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %08" PRId64 " ", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour,
S
TD-2616  
Shengliang Guan 已提交
397
                ptm->tm_min, ptm->tm_sec, (int32_t)timeSecs.tv_usec, taosGetSelfPthreadId());
H
hzcheng 已提交
398 399 400
  len += sprintf(buffer + len, "%s", flags);

  va_start(argpointer, format);
S
slguan 已提交
401
  int32_t writeLen = vsnprintf(buffer + len, MAX_LOGLINE_CONTENT_SIZE, format, argpointer);
S
slguan 已提交
402
  if (writeLen <= 0) {
403
    char tmp[MAX_LOGLINE_DUMP_BUFFER_SIZE] = {0};
S
slguan 已提交
404 405 406 407 408 409 410 411
    writeLen = vsnprintf(tmp, MAX_LOGLINE_DUMP_CONTENT_SIZE, format, argpointer);
    strncpy(buffer + len, tmp, MAX_LOGLINE_CONTENT_SIZE);
    len += MAX_LOGLINE_CONTENT_SIZE;
  } else if (writeLen >= MAX_LOGLINE_CONTENT_SIZE) {
    len += MAX_LOGLINE_CONTENT_SIZE;
  } else {
    len += writeLen;
  }
H
hzcheng 已提交
412 413 414 415 416 417 418
  va_end(argpointer);

  if (len > MAX_LOGLINE_SIZE) len = MAX_LOGLINE_SIZE;

  buffer[len++] = '\n';
  buffer[len] = 0;

S
slguan 已提交
419
  if ((dflag & DEBUG_FILE) && tsLogObj.logHandle && tsLogObj.logHandle->fd >= 0) {
H
hzcheng 已提交
420
    if (tsAsyncLog) {
S
slguan 已提交
421
      taosPushLogBuffer(tsLogObj.logHandle, buffer, len);
H
hzcheng 已提交
422
    } else {
S
Shengliang Guan 已提交
423
      taosWriteFile(tsLogObj.logHandle->fd, buffer, len);
H
hzcheng 已提交
424 425
    }

S
slguan 已提交
426 427
    if (tsLogObj.maxLines > 0) {
      atomic_add_fetch_32(&tsLogObj.lines, 1);
H
hzcheng 已提交
428

S
slguan 已提交
429
      if ((tsLogObj.lines > tsLogObj.maxLines) && (tsLogObj.openInProgress == 0)) taosOpenNewLogFile();
H
hzcheng 已提交
430 431 432
    }
  }

L
Liu Jicong 已提交
433
  if (dflag & DEBUG_SCREEN) taosWriteFile(1, buffer, (uint32_t)len);
S
TD-2371  
Shengliang Guan 已提交
434
  if (dflag == 255) nInfo(buffer, len);
H
hzcheng 已提交
435 436
}

S
slguan 已提交
437
void taosDumpData(unsigned char *msg, int32_t len) {
S
Shengliang Guan 已提交
438
  if (!taosLogSpaceAvailable()) return;
S
slguan 已提交
439

L
Liu Jicong 已提交
440 441
  char    temp[256];
  int32_t i, pos = 0, c = 0;
H
hzcheng 已提交
442 443 444 445 446 447 448

  for (i = 0; i < len; ++i) {
    sprintf(temp + pos, "%02x ", msg[i]);
    c++;
    pos += 3;
    if (c >= 16) {
      temp[pos++] = '\n';
S
Shengliang Guan 已提交
449
      taosWriteFile(tsLogObj.logHandle->fd, temp, (uint32_t)pos);
H
hzcheng 已提交
450 451 452 453 454 455 456
      c = 0;
      pos = 0;
    }
  }

  temp[pos++] = '\n';

S
Shengliang Guan 已提交
457
  taosWriteFile(tsLogObj.logHandle->fd, temp, (uint32_t)pos);
H
hzcheng 已提交
458 459
}

B
Bomin Zhang 已提交
460
void taosPrintLongString(const char *flags, int32_t dflag, const char *format, ...) {
S
Shengliang Guan 已提交
461
  if (!taosLogSpaceAvailable()) return;
S
slguan 已提交
462

H
hzcheng 已提交
463
  va_list        argpointer;
S
slguan 已提交
464
  char           buffer[MAX_LOGLINE_DUMP_BUFFER_SIZE];
S
TD-1364  
Shengliang Guan 已提交
465
  int32_t        len;
H
hzcheng 已提交
466 467 468 469
  struct tm      Tm, *ptm;
  struct timeval timeSecs;
  time_t         curTime;

S
Shengliang Guan 已提交
470
  taosGetTimeOfDay(&timeSecs);
H
hzcheng 已提交
471 472
  curTime = timeSecs.tv_sec;
  ptm = localtime_r(&curTime, &Tm);
S
slguan 已提交
473

474
  len = sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %08" PRId64 " ", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour,
S
TD-2616  
Shengliang Guan 已提交
475
                ptm->tm_min, ptm->tm_sec, (int32_t)timeSecs.tv_usec, taosGetSelfPthreadId());
H
hzcheng 已提交
476 477 478
  len += sprintf(buffer + len, "%s", flags);

  va_start(argpointer, format);
S
slguan 已提交
479
  len += vsnprintf(buffer + len, MAX_LOGLINE_DUMP_CONTENT_SIZE, format, argpointer);
H
hzcheng 已提交
480 481
  va_end(argpointer);

S
slguan 已提交
482
  if (len > MAX_LOGLINE_DUMP_SIZE) len = MAX_LOGLINE_DUMP_SIZE;
H
hzcheng 已提交
483 484 485 486

  buffer[len++] = '\n';
  buffer[len] = 0;

S
slguan 已提交
487
  if ((dflag & DEBUG_FILE) && tsLogObj.logHandle && tsLogObj.logHandle->fd >= 0) {
488 489 490
    if (tsAsyncLog) {
      taosPushLogBuffer(tsLogObj.logHandle, buffer, len);
    } else {
S
Shengliang Guan 已提交
491
      taosWriteFile(tsLogObj.logHandle->fd, buffer, len);
492
    }
L
Liu Jicong 已提交
493

S
slguan 已提交
494 495
    if (tsLogObj.maxLines > 0) {
      atomic_add_fetch_32(&tsLogObj.lines, 1);
H
hzcheng 已提交
496

S
slguan 已提交
497
      if ((tsLogObj.lines > tsLogObj.maxLines) && (tsLogObj.openInProgress == 0)) taosOpenNewLogFile();
H
hzcheng 已提交
498 499 500
    }
  }

S
Shengliang Guan 已提交
501
  if (dflag & DEBUG_SCREEN) taosWriteFile(1, buffer, (uint32_t)len);
H
hzcheng 已提交
502 503
}

S
slguan 已提交
504 505 506 507 508
#if 0
void taosCloseLog() { 
  taosCloseLogByFd(tsLogObj.logHandle->fd); 
}
#endif
H
hzcheng 已提交
509

S
slguan 已提交
510
static void taosCloseLogByFd(int32_t fd) {
H
hzcheng 已提交
511
  if (fd >= 0) {
S
Shengliang Guan 已提交
512 513
    taosUnLockLogFile(fd);
    taosCloseFile(fd);
H
hzcheng 已提交
514 515 516
  }
}

S
slguan 已提交
517
static SLogBuff *taosLogBuffNew(int32_t bufSize) {
H
hzcheng 已提交
518 519 520 521 522 523 524 525 526 527
  SLogBuff *tLogBuff = NULL;

  tLogBuff = calloc(1, sizeof(SLogBuff));
  if (tLogBuff == NULL) return NULL;

  LOG_BUF_BUFFER(tLogBuff) = malloc(bufSize);
  if (LOG_BUF_BUFFER(tLogBuff) == NULL) goto _err;

  LOG_BUF_START(tLogBuff) = LOG_BUF_END(tLogBuff) = 0;
  LOG_BUF_SIZE(tLogBuff) = bufSize;
D
fix bu  
dapan1121 已提交
528
  tLogBuff->minBuffSize = bufSize / 10;
H
hzcheng 已提交
529 530 531
  tLogBuff->stop = 0;

  if (pthread_mutex_init(&LOG_BUF_MUTEX(tLogBuff), NULL) < 0) goto _err;
L
Liu Jicong 已提交
532
  // tsem_init(&(tLogBuff->buffNotEmpty), 0, 0);
H
hzcheng 已提交
533 534 535 536

  return tLogBuff;

_err:
S
TD-1848  
Shengliang Guan 已提交
537 538
  tfree(LOG_BUF_BUFFER(tLogBuff));
  tfree(tLogBuff);
H
hzcheng 已提交
539 540 541
  return NULL;
}

S
slguan 已提交
542 543
#if 0
static void taosLogBuffDestroy(SLogBuff *tLogBuff) {
S
slguan 已提交
544
  tsem_destroy(&(tLogBuff->buffNotEmpty));
H
hzcheng 已提交
545 546
  pthread_mutex_destroy(&(tLogBuff->buffMutex));
  free(tLogBuff->buffer);
S
TD-1848  
Shengliang Guan 已提交
547
  tfree(tLogBuff);
H
hzcheng 已提交
548
}
S
slguan 已提交
549
#endif
H
hzcheng 已提交
550

D
fix bug  
dapan1121 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564
static void taosCopyLogBuffer(SLogBuff *tLogBuff, int32_t start, int32_t end, char *msg, int32_t msgLen) {
  if (start > end) {
    memcpy(LOG_BUF_BUFFER(tLogBuff) + end, msg, msgLen);
  } else {
    if (LOG_BUF_SIZE(tLogBuff) - end < msgLen) {
      memcpy(LOG_BUF_BUFFER(tLogBuff) + end, msg, LOG_BUF_SIZE(tLogBuff) - end);
      memcpy(LOG_BUF_BUFFER(tLogBuff), msg + LOG_BUF_SIZE(tLogBuff) - end, msgLen - LOG_BUF_SIZE(tLogBuff) + end);
    } else {
      memcpy(LOG_BUF_BUFFER(tLogBuff) + end, msg, msgLen);
    }
  }
  LOG_BUF_END(tLogBuff) = (LOG_BUF_END(tLogBuff) + msgLen) % LOG_BUF_SIZE(tLogBuff);
}

S
slguan 已提交
565
static int32_t taosPushLogBuffer(SLogBuff *tLogBuff, char *msg, int32_t msgLen) {
L
Liu Jicong 已提交
566 567 568
  int32_t        start = 0;
  int32_t        end = 0;
  int32_t        remainSize = 0;
D
fix bug  
dapan1121 已提交
569
  static int64_t lostLine = 0;
L
Liu Jicong 已提交
570 571
  char           tmpBuf[40] = {0};
  int32_t        tmpBufLen = 0;
H
hzcheng 已提交
572 573 574 575 576 577 578

  if (tLogBuff == NULL || tLogBuff->stop) return -1;

  pthread_mutex_lock(&LOG_BUF_MUTEX(tLogBuff));
  start = LOG_BUF_START(tLogBuff);
  end = LOG_BUF_END(tLogBuff);

D
fix bug  
dapan1121 已提交
579
  remainSize = (start > end) ? (start - end - 1) : (start + LOG_BUF_SIZE(tLogBuff) - end - 1);
H
hzcheng 已提交
580

D
fix bug  
dapan1121 已提交
581
  if (lostLine > 0) {
L
Liu Jicong 已提交
582
    sprintf(tmpBuf, "...Lost %" PRId64 " lines here...\n", lostLine);
D
dapan1121 已提交
583
    tmpBufLen = (int32_t)strlen(tmpBuf);
D
fix bug  
dapan1121 已提交
584 585 586 587 588
  }

  if (remainSize <= msgLen || ((lostLine > 0) && (remainSize <= (msgLen + tmpBufLen)))) {
    lostLine++;
    asyncLogLostLines++;
H
hzcheng 已提交
589 590 591 592
    pthread_mutex_unlock(&LOG_BUF_MUTEX(tLogBuff));
    return -1;
  }

D
fix bug  
dapan1121 已提交
593 594 595
  if (lostLine > 0) {
    taosCopyLogBuffer(tLogBuff, start, end, tmpBuf, tmpBufLen);
    lostLine = 0;
H
hzcheng 已提交
596
  }
D
fix bug  
dapan1121 已提交
597 598

  taosCopyLogBuffer(tLogBuff, LOG_BUF_START(tLogBuff), LOG_BUF_END(tLogBuff), msg, msgLen);
H
hzcheng 已提交
599

L
Liu Jicong 已提交
600
  // int32_t w = atomic_sub_fetch_32(&waitLock, 1);
D
fix bug  
dapan1121 已提交
601
  /*
D
fix bug  
dapan1121 已提交
602 603
  if (w <= 0 || ((remainSize - msgLen - tmpBufLen) < (LOG_BUF_SIZE(tLogBuff) * 4 /5))) {
    tsem_post(&(tLogBuff->buffNotEmpty));
D
fix bug  
dapan1121 已提交
604 605 606
    dbgPostN++;
  } else {
    dbgNoPostN++;
D
fix bug  
dapan1121 已提交
607
  }
D
fix bug  
dapan1121 已提交
608
  */
H
hzcheng 已提交
609 610 611 612 613 614

  pthread_mutex_unlock(&LOG_BUF_MUTEX(tLogBuff));

  return 0;
}

D
fix bu  
dapan1121 已提交
615
static int32_t taosGetLogRemainSize(SLogBuff *tLogBuff, int32_t start, int32_t end) {
D
fix bug  
dapan1121 已提交
616
  int32_t rSize = end - start;
H
hzcheng 已提交
617

D
fix bug  
dapan1121 已提交
618 619
  return rSize >= 0 ? rSize : LOG_BUF_SIZE(tLogBuff) + rSize;
}
H
hzcheng 已提交
620

D
fix bug  
dapan1121 已提交
621
static void taosWriteLog(SLogBuff *tLogBuff) {
D
fix bu  
dapan1121 已提交
622
  static int32_t lastDuration = 0;
L
Liu Jicong 已提交
623 624 625
  int32_t        remainChecked = 0;
  int32_t        start, end, pollSize;

D
fix bug  
dapan1121 已提交
626
  do {
D
fix bu  
dapan1121 已提交
627 628 629 630 631 632 633 634 635
    if (remainChecked == 0) {
      start = LOG_BUF_START(tLogBuff);
      end = LOG_BUF_END(tLogBuff);

      if (start == end) {
        dbgEmptyW++;
        writeInterval = MAX_LOG_INTERVAL;
        return;
      }
H
hzcheng 已提交
636

D
fix bu  
dapan1121 已提交
637 638 639
      pollSize = taosGetLogRemainSize(tLogBuff, start, end);
      if (pollSize < tLogBuff->minBuffSize) {
        lastDuration += writeInterval;
D
fix bug  
dapan1121 已提交
640
        if (lastDuration < LOG_MAX_WAIT_MSEC) {
D
fix bu  
dapan1121 已提交
641 642 643
          break;
        }
      }
D
fix bug  
dapan1121 已提交
644

D
fix bu  
dapan1121 已提交
645 646 647 648
      lastDuration = 0;
    }

    if (start < end) {
S
Shengliang Guan 已提交
649
      taosWriteFile(tLogBuff->fd, LOG_BUF_BUFFER(tLogBuff) + start, pollSize);
H
hzcheng 已提交
650
    } else {
L
Liu Jicong 已提交
651 652
      int32_t tsize = LOG_BUF_SIZE(tLogBuff) - start;
      taosWriteFile(tLogBuff->fd, LOG_BUF_BUFFER(tLogBuff) + start, tsize);
D
fix bug  
dapan1121 已提交
653

L
Liu Jicong 已提交
654
      taosWriteFile(tLogBuff->fd, LOG_BUF_BUFFER(tLogBuff), end);
H
hzcheng 已提交
655
    }
D
fix bug  
dapan1121 已提交
656 657

    dbgWN++;
L
Liu Jicong 已提交
658 659
    dbgWSize += pollSize;

D
fix bu  
dapan1121 已提交
660
    if (pollSize < tLogBuff->minBuffSize) {
D
fix bug  
dapan1121 已提交
661 662 663 664
      dbgSmallWN++;
      if (writeInterval < MAX_LOG_INTERVAL) {
        writeInterval += LOG_INTERVAL_STEP;
      }
L
Liu Jicong 已提交
665
    } else if (pollSize > LOG_BUF_SIZE(tLogBuff) / 3) {
D
fix bug  
dapan1121 已提交
666 667
      dbgBigWN++;
      writeInterval = MIN_LOG_INTERVAL;
L
Liu Jicong 已提交
668
    } else if (pollSize > LOG_BUF_SIZE(tLogBuff) / 4) {
D
fix bug  
dapan1121 已提交
669 670 671
      if (writeInterval > MIN_LOG_INTERVAL) {
        writeInterval -= LOG_INTERVAL_STEP;
      }
D
fix bug  
dapan1121 已提交
672 673 674 675
    }

    LOG_BUF_START(tLogBuff) = (LOG_BUF_START(tLogBuff) + pollSize) % LOG_BUF_SIZE(tLogBuff);

D
fix bu  
dapan1121 已提交
676 677 678
    start = LOG_BUF_START(tLogBuff);
    end = LOG_BUF_END(tLogBuff);

D
fix bug  
dapan1121 已提交
679
    pollSize = taosGetLogRemainSize(tLogBuff, start, end);
D
fix bu  
dapan1121 已提交
680
    if (pollSize < tLogBuff->minBuffSize) {
D
fix bug  
dapan1121 已提交
681 682 683 684
      break;
    }

    writeInterval = MIN_LOG_INTERVAL;
D
fix bu  
dapan1121 已提交
685 686

    remainChecked = 1;
L
Liu Jicong 已提交
687
  } while (1);
H
hzcheng 已提交
688 689
}

S
slguan 已提交
690
static void *taosAsyncOutputLog(void *param) {
H
hzcheng 已提交
691
  SLogBuff *tLogBuff = (SLogBuff *)param;
H
Haojun Liao 已提交
692
  setThreadName("log");
L
Liu Jicong 已提交
693

H
hzcheng 已提交
694
  while (1) {
D
fix bug  
dapan1121 已提交
695
    taosMsleep(writeInterval);
H
hzcheng 已提交
696 697

    // Polling the buffer
D
fix bug  
dapan1121 已提交
698
    taosWriteLog(tLogBuff);
H
hzcheng 已提交
699 700 701 702 703 704

    if (tLogBuff->stop) break;
  }

  return NULL;
}
S
Shengliang Guan 已提交
705 706 707 708 709

int32_t taosCompressFile(char *srcFileName, char *destFileName) {
  int32_t compressSize = 163840;
  int32_t ret = 0;
  int32_t len = 0;
L
Liu Jicong 已提交
710 711
  char   *data = malloc(compressSize);
  FILE   *srcFp = NULL;
H
Haojun Liao 已提交
712
//  gzFile  dstFp = NULL;
S
Shengliang Guan 已提交
713 714 715 716 717 718 719

  srcFp = fopen(srcFileName, "r");
  if (srcFp == NULL) {
    ret = -1;
    goto cmp_end;
  }

720
  int32_t fd = taosOpenFileCreateWriteTrunc(destFileName);
S
Shengliang Guan 已提交
721 722 723 724 725
  if (fd < 0) {
    ret = -2;
    goto cmp_end;
  }

H
Haojun Liao 已提交
726 727 728 729 730 731 732 733 734 735 736
//  dstFp = gzdopen(fd, "wb6f");
//  if (dstFp == NULL) {
//    ret = -3;
//    close(fd);
//    goto cmp_end;
//  }
//
//  while (!feof(srcFp)) {
//    len = (int32_t)fread(data, 1, compressSize, srcFp);
//    (void)gzwrite(dstFp, data, len);
//  }
S
Shengliang Guan 已提交
737 738 739 740 741

cmp_end:
  if (srcFp) {
    fclose(srcFp);
  }
H
Haojun Liao 已提交
742 743 744
//  if (dstFp) {
//    gzclose(dstFp);
//  }
S
Shengliang Guan 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
  free(data);

  return ret;
}

void taosPrintOsInfo() {
  SysNameInfo info = taosGetSysNameInfo();

  uInfo(" os pageSize:            %" PRId64 "(KB)", tsPageSize);
  uInfo(" os openMax:             %" PRId64, tsOpenMax);
  uInfo(" os streamMax:           %" PRId64, tsStreamMax);
  uInfo(" os numOfCores:          %d", tsNumOfCores);
  uInfo(" os totalMemory:         %d(MB)", tsTotalMemoryMB);
  uInfo(" os sysname:             %s", info.sysname);
  uInfo(" os nodename:            %s", info.nodename);
  uInfo(" os release:             %s", info.release);
  uInfo(" os version:             %s", info.version);
  uInfo(" os machine:             %s", info.machine);
}
S
Shengliang Guan 已提交
764

S
Shengliang Guan 已提交
765 766
void taosSetAllDebugFlag(int32_t flag) {
  if (!(flag & DEBUG_TRACE || flag & DEBUG_DEBUG || flag & DEBUG_DUMP)) return;
S
Shengliang Guan 已提交
767 768

  dDebugFlag = flag;
S
Shengliang Guan 已提交
769 770
  vDebugFlag = flag;
  mDebugFlag = flag;
S
Shengliang Guan 已提交
771
  cDebugFlag = flag;
S
Shengliang Guan 已提交
772 773 774 775 776 777 778 779 780 781 782
  jniDebugFlag = flag;
  uDebugFlag = flag;
  rpcDebugFlag = flag;
  qDebugFlag = flag;
  wDebugFlag = flag;
  sDebugFlag = flag;
  tsdbDebugFlag = flag;
  tqDebugFlag = flag;
  fsDebugFlag = flag;

  uInfo("all debug flag are set to %d", flag);
S
Shengliang Guan 已提交
783
}