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

taosdata / TDengine / #4729

12 Sep 2025 02:34AM UTC coverage: 58.085% (-1.0%) from 59.125%
#4729

push

travis-ci

web-flow
docs: optimize taosd config parameters doc better (#32964)

133518 of 292959 branches covered (45.58%)

Branch coverage included in aggregate %.

201933 of 284559 relevant lines covered (70.96%)

5466318.3 hits per line

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

51.05
/source/dnode/vnode/src/vnd/vnodeBufPool.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
#include "vnd.h"
17

18
/* ------------------------ STRUCTURES ------------------------ */
19
static int32_t vnodeBufPoolCreate(SVnode *pVnode, int32_t id, int64_t size, SVBufPool **ppPool) {
36,788✔
20
  SVBufPool *pPool;
21

22
  pPool = taosMemoryCalloc(1, sizeof(SVBufPool));
36,788!
23
  if (pPool == NULL) {
36,785!
24
    vError("vgId:%d, failed to allocate buffer pool", TD_VID(pVnode));
×
25
    return terrno;
×
26
  }
27
  pPool->node.data = taosMemoryMalloc(size);
36,785!
28
  if (NULL == pPool->node.data) {
36,796!
29
    vError("vgId:%d, failed to allocate buffer pool data, size:%" PRId64, TD_VID(pVnode), size);
×
30
    taosMemoryFree(pPool);
×
31
    return terrno;
×
32
  }
33

34
  // query handle list
35
  (void)taosThreadMutexInit(&pPool->mutex, NULL);
36,796✔
36
  pPool->nQuery = 0;
36,791✔
37
  pPool->qList.pNext = &pPool->qList;
36,791✔
38
  pPool->qList.ppNext = &pPool->qList.pNext;
36,791✔
39

40
  pPool->pVnode = pVnode;
36,791✔
41
  pPool->id = id;
36,791✔
42
  pPool->ptr = pPool->node.data;
36,791✔
43
  pPool->pTail = &pPool->node;
36,791✔
44
  pPool->node.prev = NULL;
36,791✔
45
  pPool->node.pnext = &pPool->pTail;
36,791✔
46
  pPool->node.size = size;
36,791✔
47

48
  if (VND_IS_RSMA(pVnode)) {
36,791!
49
    pPool->lock = taosMemoryMalloc(sizeof(TdThreadSpinlock));
×
50
    if (!pPool->lock) {
×
51
      taosMemoryFree(pPool->node.data);
×
52
      taosMemoryFree(pPool);
×
53
      return terrno;
×
54
    }
55
    if (taosThreadSpinInit(pPool->lock, 0) != 0) {
×
56
      taosMemoryFree((void *)pPool->lock);
3!
57
      taosMemoryFree(pPool->node.data);
×
58
      taosMemoryFree(pPool);
×
59
      return terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
60
    }
61
  } else {
62
    pPool->lock = NULL;
36,794✔
63
  }
64

65
  *ppPool = pPool;
36,791✔
66
  return 0;
36,791✔
67
}
68

69
static void vnodeBufPoolDestroy(SVBufPool *pPool) {
36,801✔
70
  vnodeBufPoolReset(pPool);
36,801✔
71
  if (pPool->lock) {
36,801!
72
    (void)taosThreadSpinDestroy(pPool->lock);
×
73
    taosMemoryFree((void *)pPool->lock);
×
74
  }
75
  (void)taosThreadMutexDestroy(&pPool->mutex);
36,801✔
76
  taosMemoryFree(pPool->node.data);
36,801!
77
  taosMemoryFree(pPool);
36,801!
78
}
36,801✔
79

80
int vnodeOpenBufPool(SVnode *pVnode) {
12,267✔
81
  int64_t size = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
12,267✔
82

83
  for (int i = 0; i < VNODE_BUFPOOL_SEGMENTS; i++) {
49,060✔
84
    // create pool
85
    int32_t code;
86
    if ((code = vnodeBufPoolCreate(pVnode, i, size, &pVnode->aBufPool[i]))) {
36,789!
87
      vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
×
88
      vnodeCloseBufPool(pVnode);
×
89
      return code;
×
90
    }
91

92
    // add to free list
93
    pVnode->aBufPool[i]->freeNext = pVnode->freeList;
36,793✔
94
    pVnode->freeList = pVnode->aBufPool[i];
36,793✔
95
  }
96

97
  vDebug("vgId:%d, vnode buffer pool is opened, size:%" PRId64, TD_VID(pVnode), size);
12,271✔
98
  return 0;
12,267✔
99
}
100

101
void vnodeCloseBufPool(SVnode *pVnode) {
12,267✔
102
  for (int32_t i = 0; i < VNODE_BUFPOOL_SEGMENTS; i++) {
49,068✔
103
    if (pVnode->aBufPool[i]) {
36,800!
104
      vnodeBufPoolDestroy(pVnode->aBufPool[i]);
36,800✔
105
      pVnode->aBufPool[i] = NULL;
36,801✔
106
    }
107
  }
108

109
  vDebug("vgId:%d, vnode buffer pool is closed", TD_VID(pVnode));
12,268✔
110
}
12,268✔
111

112
void vnodeBufPoolReset(SVBufPool *pPool) {
58,922✔
113
  if (pPool->nQuery != 0) {
58,922!
114
    vError("vgId:%d, buffer pool %p of id %d has %d queries, reset it may cause problem", TD_VID(pPool->pVnode), pPool,
×
115
           pPool->id, pPool->nQuery);
116
  }
117

118
  for (SVBufPoolNode *pNode = pPool->pTail; pNode->prev; pNode = pPool->pTail) {
256,865✔
119
    pNode->prev->pnext = &pPool->pTail;
197,943✔
120
    pPool->pTail = pNode->prev;
197,943✔
121
    pPool->size = pPool->size - sizeof(*pNode) - pNode->size;
197,943✔
122
    taosMemoryFree(pNode);
197,943!
123
  }
124

125
  pPool->size = 0;
58,922✔
126
  pPool->ptr = pPool->node.data;
58,922✔
127
}
58,922✔
128

129
void *vnodeBufPoolMallocAligned(SVBufPool *pPool, int size) {
123,048,720✔
130
  SVBufPoolNode *pNode;
131
  void          *p = NULL;
123,048,720✔
132
  uint8_t       *ptr = NULL;
123,048,720✔
133
  int            paddingLen = 0;
123,048,720✔
134

135
  if (pPool == NULL) {
123,048,720!
136
    terrno = TSDB_CODE_INVALID_PARA;
×
137
    return NULL;
×
138
  }
139

140
  if (pPool->lock) taosThreadSpinLock(pPool->lock);
123,048,720!
141

142
  ptr = pPool->ptr;
123,064,332✔
143
  paddingLen = (((long)ptr + 7) & ~7) - (long)ptr;
123,064,332✔
144

145
  if (pPool->node.size >= pPool->ptr - pPool->node.data + size + paddingLen) {
123,064,332✔
146
    // allocate from the anchor node
147
    p = pPool->ptr + paddingLen;
122,866,389✔
148
    size += paddingLen;
122,866,389✔
149
    pPool->ptr = pPool->ptr + size;
122,866,389✔
150
    pPool->size += size;
122,866,389✔
151
  } else {
152
    // allocate a new node
153
    pNode = taosMemoryMalloc(sizeof(*pNode) + size);
197,943!
154
    if (pNode == NULL) {
197,943!
155
      if (pPool->lock) {
×
156
        (void)taosThreadSpinUnlock(pPool->lock);
×
157
      }
158
      return NULL;
×
159
    }
160
    pNode->data = (uint8_t *)&pNode[1];
197,943✔
161

162
    p = pNode->data;
197,943✔
163
    pNode->size = size;
197,943✔
164
    pNode->prev = pPool->pTail;
197,943✔
165
    pNode->pnext = &pPool->pTail;
197,943✔
166
    pPool->pTail->pnext = &pNode->prev;
197,943✔
167
    pPool->pTail = pNode;
197,943✔
168

169
    pPool->size = pPool->size + sizeof(*pNode) + size;
197,943✔
170
  }
171
  if (pPool->lock) (void)taosThreadSpinUnlock(pPool->lock);
123,064,332!
172
  return p;
123,047,829✔
173
}
174

175
void *vnodeBufPoolMalloc(SVBufPool *pPool, int size) {
19,633✔
176
  SVBufPoolNode *pNode;
177
  void          *p = NULL;
19,633✔
178

179
  if (pPool == NULL) {
19,633!
180
    terrno = TSDB_CODE_INVALID_PARA;
×
181
    return NULL;
×
182
  }
183

184
  if (pPool->lock) taosThreadSpinLock(pPool->lock);
19,633!
185
  if (pPool->node.size >= pPool->ptr - pPool->node.data + size) {
19,634!
186
    // allocate from the anchor node
187
    p = pPool->ptr;
19,634✔
188
    pPool->ptr = pPool->ptr + size;
19,634✔
189
    pPool->size += size;
19,634✔
190
  } else {
191
    // allocate a new node
192
    pNode = taosMemoryMalloc(sizeof(*pNode) + size);
×
193
    if (pNode == NULL) {
×
194
      if (pPool->lock) (void)taosThreadSpinUnlock(pPool->lock);
×
195
      return NULL;
×
196
    }
197
    pNode->data = (uint8_t *)&pNode[1];
×
198

199
    p = pNode->data;
×
200
    pNode->size = size;
×
201
    pNode->prev = pPool->pTail;
×
202
    pNode->pnext = &pPool->pTail;
×
203
    pPool->pTail->pnext = &pNode->prev;
×
204
    pPool->pTail = pNode;
×
205

206
    pPool->size = pPool->size + sizeof(*pNode) + size;
×
207
  }
208
  if (pPool->lock) (void)taosThreadSpinUnlock(pPool->lock);
19,634!
209
  return p;
19,634✔
210
}
211

212
void vnodeBufPoolFree(SVBufPool *pPool, void *p) {
180,951✔
213
  // uint8_t       *ptr = (uint8_t *)p;
214
  // SVBufPoolNode *pNode;
215

216
  // if (ptr < pPool->node.data || ptr >= pPool->node.data + pPool->node.size) {
217
  //   pNode = &((SVBufPoolNode *)p)[-1];
218
  //   *pNode->pnext = pNode->prev;
219
  //   pNode->prev->pnext = pNode->pnext;
220

221
  //   pPool->size = pPool->size - sizeof(*pNode) - pNode->size;
222
  //   taosMemoryFree(pNode);
223
  // }
224
}
180,951✔
225

226
void vnodeBufPoolRef(SVBufPool *pPool) {
34,384✔
227
  int32_t nRef = atomic_fetch_add_32(&pPool->nRef, 1);
34,384✔
228
  if (nRef <= 0) {
34,385!
229
    vError("vgId:%d, buffer pool %p of id %d is referenced by %d", TD_VID(pPool->pVnode), pPool, pPool->id, nRef);
×
230
  }
231
}
34,385✔
232

233
static void vnodeBufPoolResize(SVBufPool *pPool, int64_t size) {
×
234
  if (pPool == NULL) return;
×
235

236
  uint8_t *pDataNew = taosMemoryMalloc(size);
×
237
  if (pDataNew == NULL) {
×
238
    vError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pPool->pVnode), __func__, __FILE__, __LINE__,
×
239
           tstrerror(terrno));
240
    return;
×
241
  }
242

243
  // Apply change
244
  int64_t oldSize = pPool->node.size;
×
245
  taosMemoryFree(pPool->node.data);
×
246
  pPool->node.data = pDataNew;
×
247
  pPool->node.size = size;
×
248

249
  vInfo("vgId:%d, buffer pool %d resized from %" PRId64 " to %" PRId64, TD_VID(pPool->pVnode), pPool->id, oldSize,
×
250
        size);
251
  return;
×
252
}
253

254
void vnodeBufPoolAddToFreeList(SVBufPool *pPool) {
22,121✔
255
  SVnode *pVnode = pPool->pVnode;
22,121✔
256

257
  int64_t size = pVnode->config.szBuf / VNODE_BUFPOOL_SEGMENTS;
22,121✔
258
  if (pPool->node.size != size) {
22,121!
259
    vnodeBufPoolResize(pPool, size);
×
260
  }
261

262
  // add to free list
263
  vDebug("vgId:%d, buffer pool %p of id %d is added to free list", TD_VID(pVnode), pPool, pPool->id);
22,121✔
264
  vnodeBufPoolReset(pPool);
22,121✔
265
  pPool->freeNext = pVnode->freeList;
22,121✔
266
  pVnode->freeList = pPool;
22,121✔
267
  (void)taosThreadCondSignal(&pVnode->poolNotEmpty);
22,121✔
268
}
22,121✔
269

270
void vnodeBufPoolUnRef(SVBufPool *pPool, bool proactive) {
34,388✔
271
  if (pPool == NULL) return;
34,388!
272

273
  SVnode *pVnode = pPool->pVnode;
34,388✔
274

275
  if (proactive) {
34,388!
276
    (void)taosThreadMutexLock(&pVnode->mutex);
34,388✔
277
  }
278

279
  if (atomic_sub_fetch_32(&pPool->nRef, 1) > 0) goto _exit;
34,388✔
280

281
  // remove from recycle queue or on-recycle position
282
  if (pVnode->onRecycle == pPool) {
105!
283
    pVnode->onRecycle = NULL;
×
284
  } else {
285
    if (pPool->recyclePrev) {
105!
286
      pPool->recyclePrev->recycleNext = pPool->recycleNext;
×
287
    } else {
288
      pVnode->recycleHead = pPool->recycleNext;
105✔
289
    }
290

291
    if (pPool->recycleNext) {
105!
292
      pPool->recycleNext->recyclePrev = pPool->recyclePrev;
×
293
    } else {
294
      pVnode->recycleTail = pPool->recyclePrev;
105✔
295
    }
296
    pPool->recyclePrev = pPool->recycleNext = NULL;
105✔
297
  }
298

299
  vnodeBufPoolAddToFreeList(pPool);
105✔
300

301
_exit:
34,388✔
302
  if (proactive) {
34,388!
303
    (void)taosThreadMutexUnlock(&pVnode->mutex);
34,388✔
304
  }
305
  return;
34,388✔
306
}
307

308
void vnodeBufPoolRegisterQuery(SVBufPool *pPool, SQueryNode *pQNode) {
835,294✔
309
  (void)taosThreadMutexLock(&pPool->mutex);
835,294✔
310

311
  pQNode->pNext = pPool->qList.pNext;
835,689✔
312
  pQNode->ppNext = &pPool->qList.pNext;
835,689✔
313
  pPool->qList.pNext->ppNext = &pQNode->pNext;
835,689✔
314
  pPool->qList.pNext = pQNode;
835,689✔
315
  pPool->nQuery++;
835,689✔
316

317
  (void)taosThreadMutexUnlock(&pPool->mutex);
835,689✔
318
}
835,673✔
319

320
void vnodeBufPoolDeregisterQuery(SVBufPool *pPool, SQueryNode *pQNode, bool proactive) {
835,688✔
321
  int32_t code = 0;
835,688✔
322

323
  if (proactive) {
835,688!
324
    (void)taosThreadMutexLock(&pPool->mutex);
835,694✔
325
  }
326

327
  pQNode->pNext->ppNext = pQNode->ppNext;
835,697✔
328
  *pQNode->ppNext = pQNode->pNext;
835,697✔
329
  pPool->nQuery--;
835,697✔
330

331
  if (proactive) {
835,697!
332
    (void)taosThreadMutexUnlock(&pPool->mutex);
835,703✔
333
  }
334
}
835,696✔
335

336
int32_t vnodeBufPoolRecycle(SVBufPool *pPool) {
×
337
  int32_t code = 0;
×
338

339
  SVnode *pVnode = pPool->pVnode;
×
340

341
  vDebug("vgId:%d, recycle buffer pool %p of id %d", TD_VID(pVnode), pPool, pPool->id);
×
342

343
  (void)taosThreadMutexLock(&pPool->mutex);
×
344

345
  SQueryNode *pNode = pPool->qList.pNext;
×
346
  while (pNode != &pPool->qList) {
×
347
    SQueryNode *pTNode = pNode->pNext;
×
348

349
    int32_t rc = pNode->reseek(pNode->pQHandle);
×
350
    if (rc == 0 || rc == TSDB_CODE_VND_QUERY_BUSY) {
×
351
      pNode = pTNode;
×
352
    } else {
353
      code = rc;
×
354
      goto _exit;
×
355
    }
356
  }
357

358
_exit:
×
359
  (void)taosThreadMutexUnlock(&pPool->mutex);
×
360
  return code;
×
361
}
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

© 2026 Coveralls, Inc