Debian Codegraph @

runtime/batch.h

raw
1 /* Definition of the batch_t data structure.
2 * I am not sure yet if this will become a full-blown object. For now, this header just
3 * includes the object definition and is not accompanied by code.
4 *
5 * Copyright 2009-2013 by Rainer Gerhards and Adiscon GmbH.
6 *
7 * This file is part of the rsyslog runtime library.
8 *
9 * The rsyslog runtime library is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * The rsyslog runtime library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * A copy of the GPL can be found in the file "COPYING" in this distribution.
23 * A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution.
24 */
25
26 #ifndef BATCH_H_INCLUDED
27 #define BATCH_H_INCLUDED
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include "msg.h"
32
33 /* enum for batch states. Actually, we violate a layer here, in that we assume that a batch is used
34 * for action processing. So far, this seems acceptable, the status is simply ignored inside the
35 * main message queue. But over time, it could potentially be useful to split the two.
36 * rgerhad, 2009-05-12
37 */
38 #define BATCH_STATE_RDY 0 /* object ready for processing */
39 #define BATCH_STATE_BAD 1 /* unrecoverable failure while processing, do NOT resubmit to same action */
40 #define BATCH_STATE_SUB 2 /* message submitted for processing, outcome yet unknown */
41 #define BATCH_STATE_COMM 3 /* message successfully commited */
42 #define BATCH_STATE_DISC 4 /* discarded - processed OK, but do not submit to any other action */
43 typedef unsigned char batch_state_t;
44
45
46 /* an object inside a batch, including any information (state!) needed for it to "life".
47 */
48 struct batch_obj_s {
49 smsg_t *pMsg;
50 };
51
52 /* the batch
53 * This object is used to dequeue multiple user pointers which are than handed over
54 * to processing. The size of elements is fixed after queue creation, but may be
55 * modified by config variables (better said: queue properties).
56 * Note that a "user pointer" in rsyslog context so far always is a message
57 * object. We stick to the more generic term because queues may potentially hold
58 * other types of objects, too.
59 * rgerhards, 2009-05-12
60 * Note that nElem is not necessarily equal to nElemDeq. This is the case when we
61 * discard some elements (because of configuration) during dequeue processing. As
62 * all Elements are only deleted when the batch is processed, we can not immediately
63 * delete them. So we need to keep their number that we can delete them when the batch
64 * is completed (else, the whole process does not work correctly).
65 */
66 struct batch_s {
67 int maxElem; /* maximum number of elements that this batch supports */
68 int nElem; /* actual number of element in this entry */
69 int nElemDeq; /* actual number of elements dequeued (and thus to be deleted) - see comment above! */
70 qDeqID deqID; /* ID of dequeue operation that generated this batch */
71 batch_obj_t *pElem; /* batch elements */
72 batch_state_t *eltState; /* state (array!) for individual objects.
73 NOTE: we have moved this out of batch_obj_t because we
74 get a *much* better cache hit ratio this way. So do not
75 move it back into this structure! Note that this is really
76 a HUGE saving, even if it doesn't look so (both profiler
77 data as well as practical tests indicate that!).
78 */
79 };
80
81
82 /* get number of msgs for this batch */
83 #define batchNumMsgs(pBatch) ((pBatch)->nElem)
84
85
86 /* set the status of the i-th batch element. Note that once the status is
87 * DISC, it will never be reset. So this function can NOT be used to initialize
88 * the state table. -- rgerhards, 2010-06-10
89 */
90 static inline void __attribute__((unused)) batchSetElemState(batch_t *const pBatch,
91 const int i,
92 const batch_state_t newState) {
93 if (pBatch->eltState[i] != BATCH_STATE_DISC) pBatch->eltState[i] = newState;
94 }
95
96
97 /* check if an element is a valid entry. We do NOT verify if the
98 * element index is valid. -- rgerhards, 2010-06-10
99 */
100 #define batchIsValidElem(pBatch, i) ((pBatch)->eltState[(i)] != BATCH_STATE_DISC)
101
102
103 /* free members of a batch "object". Note that we can not do the usual
104 * destruction as the object typically is allocated on the stack and so the
105 * object itself cannot be freed! -- rgerhards, 2010-06-15
106 */
107 static inline void __attribute__((unused)) batchFree(batch_t *const pBatch) {
108 free(pBatch->pElem);
109 free(pBatch->eltState);
110 }
111
112
113 /* initialiaze a batch "object". The record must already exist,
114 * we "just" initialize it. The max number of elements must be
115 * provided. -- rgerhards, 2010-06-15
116 */
117 static inline rsRetVal __attribute__((unused)) batchInit(batch_t *const pBatch, const int maxElem) {
118 DEFiRet;
119 pBatch->maxElem = maxElem;
120 CHKmalloc(pBatch->pElem = calloc((size_t)maxElem, sizeof(batch_obj_t)));
121 CHKmalloc(pBatch->eltState = calloc((size_t)maxElem, sizeof(batch_state_t)));
122 finalize_it:
123 RETiRet;
124 }
125
126 #endif /* #ifndef BATCH_H_INCLUDED */