tsched.c 7.3 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
static void *taosProcessSchedQueue(void *param);
S
Shengliang Guan 已提交
26
static void  taosDumpSchedulerStatus(void *qhandle, void *tmrId);
H
hzcheng 已提交
27

D
dapan1121 已提交
28 29 30 31 32 33 34
void *taosInitScheduler(int32_t queueSize, int32_t numOfThreads, const char *label, SSchedQueue *pSched) {
  if (NULL == pSched) {
    pSched = (SSchedQueue *)taosMemoryCalloc(sizeof(SSchedQueue), 1);
    if (pSched == NULL) {
      uError("%s: no enough memory for pSched", label);
      return NULL;
    }
J
Jeff Tao 已提交
35 36
  }

wafwerar's avatar
wafwerar 已提交
37
  pSched->queue = (SSchedMsg *)taosMemoryCalloc(sizeof(SSchedMsg), queueSize);
J
Jeff Tao 已提交
38
  if (pSched->queue == NULL) {
S
slguan 已提交
39
    uError("%s: no enough memory for queue", label);
J
Jeff Tao 已提交
40
    taosCleanUpScheduler(pSched);
dengyihao's avatar
dengyihao 已提交
41
    taosMemoryFree(pSched);
J
Jeff Tao 已提交
42 43 44
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
45
  pSched->qthread = taosMemoryCalloc(sizeof(TdThread), numOfThreads);
J
Jeff Tao 已提交
46
  if (pSched->qthread == NULL) {
S
slguan 已提交
47
    uError("%s: no enough memory for qthread", label);
J
Jeff Tao 已提交
48 49
    taosCleanUpScheduler(pSched);
    return NULL;
50
  }
H
hzcheng 已提交
51 52

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

J
Jeff Tao 已提交
55 56 57
  pSched->fullSlot = 0;
  pSched->emptySlot = 0;

wafwerar's avatar
wafwerar 已提交
58
  if (taosThreadMutexInit(&pSched->queueMutex, NULL) < 0) {
S
slguan 已提交
59
    uError("init %s:queueMutex failed(%s)", label, strerror(errno));
J
Jeff Tao 已提交
60 61
    taosCleanUpScheduler(pSched);
    return NULL;
H
hzcheng 已提交
62 63
  }

S
Shengliang Guan 已提交
64
  if (tsem_init(&pSched->emptySem, 0, (uint32_t)pSched->queueSize) != 0) {
S
slguan 已提交
65
    uError("init %s:empty semaphore failed(%s)", label, strerror(errno));
J
Jeff Tao 已提交
66 67
    taosCleanUpScheduler(pSched);
    return NULL;
H
hzcheng 已提交
68 69
  }

S
slguan 已提交
70
  if (tsem_init(&pSched->fullSem, 0, 0) != 0) {
S
slguan 已提交
71
    uError("init %s:full semaphore failed(%s)", label, strerror(errno));
J
Jeff Tao 已提交
72 73
    taosCleanUpScheduler(pSched);
    return NULL;
H
hzcheng 已提交
74 75
  }

D
dapan1121 已提交
76
  atomic_store_8(&pSched->stop, 0);
S
Shengliang Guan 已提交
77
  for (int32_t i = 0; i < numOfThreads; ++i) {
wafwerar's avatar
wafwerar 已提交
78 79 80 81 82
    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 已提交
83
    if (code != 0) {
S
slguan 已提交
84
      uError("%s: failed to create rpc thread(%s)", label, strerror(errno));
J
Jeff Tao 已提交
85 86
      taosCleanUpScheduler(pSched);
      return NULL;
H
hzcheng 已提交
87
    }
88
    ++pSched->numOfThreads;
H
hzcheng 已提交
89 90
  }

91
  uDebug("%s scheduler is initialized, numOfThreads:%d", label, pSched->numOfThreads);
H
hzcheng 已提交
92 93 94 95

  return (void *)pSched;
}

S
Shengliang Guan 已提交
96
void *taosInitSchedulerWithInfo(int32_t queueSize, int32_t numOfThreads, const char *label, void *tmrCtrl) {
D
dapan1121 已提交
97
  SSchedQueue *pSched = taosInitScheduler(queueSize, numOfThreads, label, NULL);
98

99 100 101 102
  if (tmrCtrl != NULL && pSched != NULL) {
    pSched->pTmrCtrl = tmrCtrl;
    taosTmrReset(taosDumpSchedulerStatus, DUMP_SCHEDULER_TIME_WINDOW, pSched, pSched->pTmrCtrl, &pSched->pTimer);
  }
103

104 105 106
  return pSched;
}

107
void *taosProcessSchedQueue(void *scheduler) {
H
hzcheng 已提交
108
  SSchedMsg    msg;
109
  SSchedQueue *pSched = (SSchedQueue *)scheduler;
S
Shengliang Guan 已提交
110
  int32_t      ret = 0;
H
hzcheng 已提交
111

H
Haojun Liao 已提交
112 113 114
  char name[16] = {0};
  snprintf(name, tListLen(name), "%s-taskQ", pSched->label);
  setThreadName(name);
115

H
hzcheng 已提交
116
  while (1) {
117 118
    if ((ret = tsem_wait(&pSched->fullSem)) != 0) {
      uFatal("wait %s fullSem failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
119
      ASSERT(0);
H
hzcheng 已提交
120
    }
D
dapan1121 已提交
121
    if (atomic_load_8(&pSched->stop)) {
122 123
      break;
    }
H
hzcheng 已提交
124

wafwerar's avatar
wafwerar 已提交
125
    if ((ret = taosThreadMutexLock(&pSched->queueMutex)) != 0) {
126
      uFatal("lock %s queueMutex failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
127
      ASSERT(0);
128
    }
H
hzcheng 已提交
129 130 131 132 133

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

wafwerar's avatar
wafwerar 已提交
134
    if ((ret = taosThreadMutexUnlock(&pSched->queueMutex)) != 0) {
135
      uFatal("unlock %s queueMutex failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
136
      ASSERT(0);
137
    }
H
hzcheng 已提交
138

139 140
    if ((ret = tsem_post(&pSched->emptySem)) != 0) {
      uFatal("post %s emptySem failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
141
      ASSERT(0);
142
    }
H
hzcheng 已提交
143 144 145 146 147 148

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

  return NULL;
H
hzcheng 已提交
151 152
}

dengyihao's avatar
dengyihao 已提交
153
int taosScheduleTask(void *queueScheduler, SSchedMsg *pMsg) {
154
  SSchedQueue *pSched = (SSchedQueue *)queueScheduler;
S
Shengliang Guan 已提交
155
  int32_t      ret = 0;
156

H
hzcheng 已提交
157
  if (pSched == NULL) {
S
slguan 已提交
158
    uError("sched is not ready, msg:%p is dropped", pMsg);
dengyihao's avatar
dengyihao 已提交
159
    return -1;
H
hzcheng 已提交
160 161
  }

D
dapan1121 已提交
162 163
  if (atomic_load_8(&pSched->stop)) {
    uError("sched is already stopped, msg:%p is dropped", pMsg);
dengyihao's avatar
dengyihao 已提交
164
    return -1;
D
dapan1121 已提交
165 166
  }

167 168
  if ((ret = tsem_wait(&pSched->emptySem)) != 0) {
    uFatal("wait %s emptySem failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
169
    ASSERT(0);
170
  }
H
hzcheng 已提交
171

wafwerar's avatar
wafwerar 已提交
172
  if ((ret = taosThreadMutexLock(&pSched->queueMutex)) != 0) {
173
    uFatal("lock %s queueMutex failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
174
    ASSERT(0);
175
  }
H
hzcheng 已提交
176 177 178 179

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

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

185 186
  if ((ret = tsem_post(&pSched->fullSem)) != 0) {
    uFatal("post %s fullSem failed(%s)", pSched->label, strerror(errno));
D
dapan1121 已提交
187
    ASSERT(0);
188
  }
dengyihao's avatar
dengyihao 已提交
189
  return ret;
H
hzcheng 已提交
190 191 192 193 194 195
}

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

D
dapan1121 已提交
196
  uDebug("start to cleanup %s schedQsueue", pSched->label);
dengyihao's avatar
dengyihao 已提交
197

D
dapan1121 已提交
198 199 200
  atomic_store_8(&pSched->stop, 1);

  taosMsleep(200);
dengyihao's avatar
dengyihao 已提交
201

S
Shengliang Guan 已提交
202 203
  for (int32_t i = 0; i < pSched->numOfThreads; ++i) {
    if (taosCheckPthreadValid(pSched->qthread[i])) {
204 205
      tsem_post(&pSched->fullSem);
    }
206
  }
S
Shengliang Guan 已提交
207
  for (int32_t i = 0; i < pSched->numOfThreads; ++i) {
S
TD-1037  
Shengliang Guan 已提交
208
    if (taosCheckPthreadValid(pSched->qthread[i])) {
wafwerar's avatar
wafwerar 已提交
209
      taosThreadJoin(pSched->qthread[i], NULL);
210
      taosThreadClear(&pSched->qthread[i]);
211
    }
H
hzcheng 已提交
212 213
  }

S
slguan 已提交
214 215
  tsem_destroy(&pSched->emptySem);
  tsem_destroy(&pSched->fullSem);
wafwerar's avatar
wafwerar 已提交
216
  taosThreadMutexDestroy(&pSched->queueMutex);
S
Shengliang Guan 已提交
217

218
  if (pSched->pTimer) {
L
Liu Jicong 已提交
219 220
    taosTmrStop(pSched->pTimer);
    pSched->pTimer = NULL;
221
  }
H
hzcheng 已提交
222

wafwerar's avatar
wafwerar 已提交
223 224
  if (pSched->queue) taosMemoryFree(pSched->queue);
  if (pSched->qthread) taosMemoryFree(pSched->qthread);
dengyihao's avatar
dengyihao 已提交
225
  // taosMemoryFree(pSched);
H
hzcheng 已提交
226
}
227 228 229 230 231 232 233

// 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 已提交
234

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

240
  taosTmrReset(taosDumpSchedulerStatus, DUMP_SCHEDULER_TIME_WINDOW, pSched, pSched->pTmrCtrl, &pSched->pTimer);
241
}