tsched.c 7.4 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
Shengliang Guan 已提交
16 17
#define _DEFAULT_SOURCE
#include "tsched.h"
18
#include "tdef.h"
S
log  
Shengliang Guan 已提交
19
#include "tlog.h"
20
#include "ttimer.h"
S
Shengliang Guan 已提交
21
#include "tutil.h"
22

S
Shengliang Guan 已提交
23
#define DUMP_SCHEDULER_TIME_WINDOW 30000  // every 30sec, take a snap shot of task queue.
H
hzcheng 已提交
24 25

typedef struct {
L
Liu Jicong 已提交
26 27 28
  char          label[TSDB_LABEL_LEN];
  tsem_t        emptySem;
  tsem_t        fullSem;
wafwerar's avatar
wafwerar 已提交
29
  TdThreadMutex queueMutex;
L
Liu Jicong 已提交
30 31 32 33 34 35 36 37 38
  int32_t       fullSlot;
  int32_t       emptySlot;
  int32_t       queueSize;
  int32_t       numOfThreads;
  TdThread     *qthread;
  SSchedMsg    *queue;
  bool          stop;
  void         *pTmrCtrl;
  void         *pTimer;
H
hzcheng 已提交
39 40
} SSchedQueue;

41
static void *taosProcessSchedQueue(void *param);
S
Shengliang Guan 已提交
42
static void  taosDumpSchedulerStatus(void *qhandle, void *tmrId);
H
hzcheng 已提交
43

S
Shengliang Guan 已提交
44
void *taosInitScheduler(int32_t queueSize, int32_t numOfThreads, const char *label) {
wafwerar's avatar
wafwerar 已提交
45
  SSchedQueue *pSched = (SSchedQueue *)taosMemoryCalloc(sizeof(SSchedQueue), 1);
46
  if (pSched == NULL) {
S
slguan 已提交
47
    uError("%s: no enough memory for pSched", label);
J
Jeff Tao 已提交
48 49 50
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
51
  pSched->queue = (SSchedMsg *)taosMemoryCalloc(sizeof(SSchedMsg), queueSize);
J
Jeff Tao 已提交
52
  if (pSched->queue == NULL) {
S
slguan 已提交
53
    uError("%s: no enough memory for queue", label);
J
Jeff Tao 已提交
54 55 56 57
    taosCleanUpScheduler(pSched);
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
58
  pSched->qthread = taosMemoryCalloc(sizeof(TdThread), numOfThreads);
J
Jeff Tao 已提交
59
  if (pSched->qthread == NULL) {
S
slguan 已提交
60
    uError("%s: no enough memory for qthread", label);
J
Jeff Tao 已提交
61 62
    taosCleanUpScheduler(pSched);
    return NULL;
63
  }
H
hzcheng 已提交
64 65

  pSched->queueSize = queueSize;
S
Shengliang Guan 已提交
66
  tstrncpy(pSched->label, label, sizeof(pSched->label));  // fix buffer overflow
H
hzcheng 已提交
67

J
Jeff Tao 已提交
68 69 70
  pSched->fullSlot = 0;
  pSched->emptySlot = 0;

wafwerar's avatar
wafwerar 已提交
71
  if (taosThreadMutexInit(&pSched->queueMutex, NULL) < 0) {
S
slguan 已提交
72
    uError("init %s:queueMutex failed(%s)", label, strerror(errno));
J
Jeff Tao 已提交
73 74
    taosCleanUpScheduler(pSched);
    return NULL;
H
hzcheng 已提交
75 76
  }

S
Shengliang Guan 已提交
77
  if (tsem_init(&pSched->emptySem, 0, (uint32_t)pSched->queueSize) != 0) {
S
slguan 已提交
78
    uError("init %s:empty semaphore failed(%s)", label, strerror(errno));
J
Jeff Tao 已提交
79 80
    taosCleanUpScheduler(pSched);
    return NULL;
H
hzcheng 已提交
81 82
  }

S
slguan 已提交
83
  if (tsem_init(&pSched->fullSem, 0, 0) != 0) {
S
slguan 已提交
84
    uError("init %s:full semaphore failed(%s)", label, strerror(errno));
J
Jeff Tao 已提交
85 86
    taosCleanUpScheduler(pSched);
    return NULL;
H
hzcheng 已提交
87 88
  }

89
  pSched->stop = false;
S
Shengliang Guan 已提交
90
  for (int32_t i = 0; i < numOfThreads; ++i) {
wafwerar's avatar
wafwerar 已提交
91 92 93 94 95
    TdThreadAttr attr;
    taosThreadAttrInit(&attr);
    taosThreadAttrSetDetachState(&attr, PTHREAD_CREATE_JOINABLE);
    int32_t code = taosThreadCreate(pSched->qthread + i, &attr, taosProcessSchedQueue, (void *)pSched);
    taosThreadAttrDestroy(&attr);
J
Jeff Tao 已提交
96
    if (code != 0) {
S
slguan 已提交
97
      uError("%s: failed to create rpc thread(%s)", label, strerror(errno));
J
Jeff Tao 已提交
98 99
      taosCleanUpScheduler(pSched);
      return NULL;
H
hzcheng 已提交
100
    }
101
    ++pSched->numOfThreads;
H
hzcheng 已提交
102 103
  }

104
  uDebug("%s scheduler is initialized, numOfThreads:%d", label, pSched->numOfThreads);
H
hzcheng 已提交
105 106 107 108

  return (void *)pSched;
}

S
Shengliang Guan 已提交
109 110
void *taosInitSchedulerWithInfo(int32_t queueSize, int32_t numOfThreads, const char *label, void *tmrCtrl) {
  SSchedQueue *pSched = taosInitScheduler(queueSize, numOfThreads, label);
111

112 113 114 115
  if (tmrCtrl != NULL && pSched != NULL) {
    pSched->pTmrCtrl = tmrCtrl;
    taosTmrReset(taosDumpSchedulerStatus, DUMP_SCHEDULER_TIME_WINDOW, pSched, pSched->pTmrCtrl, &pSched->pTimer);
  }
116

117 118 119
  return pSched;
}

120
void *taosProcessSchedQueue(void *scheduler) {
H
hzcheng 已提交
121
  SSchedMsg    msg;
122
  SSchedQueue *pSched = (SSchedQueue *)scheduler;
S
Shengliang Guan 已提交
123
  int32_t      ret = 0;
H
hzcheng 已提交
124

H
Haojun Liao 已提交
125 126 127
  char name[16] = {0};
  snprintf(name, tListLen(name), "%s-taskQ", pSched->label);
  setThreadName(name);
128

H
hzcheng 已提交
129
  while (1) {
130 131
    if ((ret = tsem_wait(&pSched->fullSem)) != 0) {
      uFatal("wait %s fullSem failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
132
      ASSERT(0);
H
hzcheng 已提交
133
    }
134 135 136
    if (pSched->stop) {
      break;
    }
H
hzcheng 已提交
137

wafwerar's avatar
wafwerar 已提交
138
    if ((ret = taosThreadMutexLock(&pSched->queueMutex)) != 0) {
139
      uFatal("lock %s queueMutex failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
140
      ASSERT(0);
141
    }
H
hzcheng 已提交
142 143 144 145 146

    msg = pSched->queue[pSched->fullSlot];
    memset(pSched->queue + pSched->fullSlot, 0, sizeof(SSchedMsg));
    pSched->fullSlot = (pSched->fullSlot + 1) % pSched->queueSize;

wafwerar's avatar
wafwerar 已提交
147
    if ((ret = taosThreadMutexUnlock(&pSched->queueMutex)) != 0) {
148
      uFatal("unlock %s queueMutex failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
149
      ASSERT(0);
150
    }
H
hzcheng 已提交
151

152 153
    if ((ret = tsem_post(&pSched->emptySem)) != 0) {
      uFatal("post %s emptySem failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
154
      ASSERT(0);
155
    }
H
hzcheng 已提交
156 157 158 159 160 161

    if (msg.fp)
      (*(msg.fp))(&msg);
    else if (msg.tfp)
      (*(msg.tfp))(msg.ahandle, msg.thandle);
  }
S
slguan 已提交
162 163

  return NULL;
H
hzcheng 已提交
164 165
}

166 167
void taosScheduleTask(void *queueScheduler, SSchedMsg *pMsg) {
  SSchedQueue *pSched = (SSchedQueue *)queueScheduler;
S
Shengliang Guan 已提交
168
  int32_t      ret = 0;
169

H
hzcheng 已提交
170
  if (pSched == NULL) {
S
slguan 已提交
171
    uError("sched is not ready, msg:%p is dropped", pMsg);
172
    return;
H
hzcheng 已提交
173 174
  }

175 176
  if ((ret = tsem_wait(&pSched->emptySem)) != 0) {
    uFatal("wait %s emptySem failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
177
    ASSERT(0);
178
  }
H
hzcheng 已提交
179

wafwerar's avatar
wafwerar 已提交
180
  if ((ret = taosThreadMutexLock(&pSched->queueMutex)) != 0) {
181
    uFatal("lock %s queueMutex failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
182
    ASSERT(0);
183
  }
H
hzcheng 已提交
184 185 186 187

  pSched->queue[pSched->emptySlot] = *pMsg;
  pSched->emptySlot = (pSched->emptySlot + 1) % pSched->queueSize;

wafwerar's avatar
wafwerar 已提交
188
  if ((ret = taosThreadMutexUnlock(&pSched->queueMutex)) != 0) {
189
    uFatal("unlock %s queueMutex failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
190
    ASSERT(0);
191
  }
H
hzcheng 已提交
192

193 194
  if ((ret = tsem_post(&pSched->fullSem)) != 0) {
    uFatal("post %s fullSem failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
195
    ASSERT(0);
196
  }
H
hzcheng 已提交
197 198 199 200 201 202
}

void taosCleanUpScheduler(void *param) {
  SSchedQueue *pSched = (SSchedQueue *)param;
  if (pSched == NULL) return;

D
dapan1121 已提交
203 204
  uDebug("start to cleanup %s schedQsueue", pSched->label);
  
205
  pSched->stop = true;
S
Shengliang Guan 已提交
206 207
  for (int32_t i = 0; i < pSched->numOfThreads; ++i) {
    if (taosCheckPthreadValid(pSched->qthread[i])) {
208 209
      tsem_post(&pSched->fullSem);
    }
210
  }
S
Shengliang Guan 已提交
211
  for (int32_t i = 0; i < pSched->numOfThreads; ++i) {
S
TD-1037  
Shengliang Guan 已提交
212
    if (taosCheckPthreadValid(pSched->qthread[i])) {
wafwerar's avatar
wafwerar 已提交
213
      taosThreadJoin(pSched->qthread[i], NULL);
214
      taosThreadClear(&pSched->qthread[i]);
215
    }
H
hzcheng 已提交
216 217
  }

S
slguan 已提交
218 219
  tsem_destroy(&pSched->emptySem);
  tsem_destroy(&pSched->fullSem);
wafwerar's avatar
wafwerar 已提交
220
  taosThreadMutexDestroy(&pSched->queueMutex);
S
Shengliang Guan 已提交
221

222
  if (pSched->pTimer) {
L
Liu Jicong 已提交
223 224
    taosTmrStop(pSched->pTimer);
    pSched->pTimer = NULL;
225
  }
H
hzcheng 已提交
226

wafwerar's avatar
wafwerar 已提交
227 228 229
  if (pSched->queue) taosMemoryFree(pSched->queue);
  if (pSched->qthread) taosMemoryFree(pSched->qthread);
  taosMemoryFree(pSched);  // fix memory leak
H
hzcheng 已提交
230
}
231 232 233 234 235 236 237

// for debug purpose, dump the scheduler status every 1min.
void taosDumpSchedulerStatus(void *qhandle, void *tmrId) {
  SSchedQueue *pSched = (SSchedQueue *)qhandle;
  if (pSched == NULL || pSched->pTimer == NULL || pSched->pTimer != tmrId) {
    return;
  }
S
Shengliang Guan 已提交
238

239 240
  int32_t size = ((pSched->emptySlot - pSched->fullSlot) + pSched->queueSize) % pSched->queueSize;
  if (size > 0) {
241
    uDebug("scheduler:%s, current tasks in queue:%d, task thread:%d", pSched->label, size, pSched->numOfThreads);
242
  }
S
Shengliang Guan 已提交
243

244
  taosTmrReset(taosDumpSchedulerStatus, DUMP_SCHEDULER_TIME_WINDOW, pSched, pSched->pTmrCtrl, &pSched->pTimer);
245
}