1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*
* metriclist.c
*
* Copyright (c) 1997,2005 Silicon Graphics, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* 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. See the GNU General Public License
* for more details.
*/
#include "pmapi.h"
#include "impl.h"
#include "logger.h"
/*
* extract the pmid in vsetp and all its instances, and put it in
* a pmResult of its own
*/
void
extractpmid(pmValueSet *vsetp, struct timeval *timestamp, pmResult **resp)
{
int i;
int size;
pmResult *result;
pmValueBlock *vbp; /* value block pointer */
result = (pmResult *)malloc(sizeof(pmResult));
if (result == NULL) {
fprintf(stderr, "%s: Error: cannot malloc space in \"extractpmid\".\n",
pmProgname);
exit(1);
}
size = sizeof(pmValueSet) + (vsetp->numval-1) * sizeof(pmValue);
result->vset[0] = (pmValueSet *)malloc(size);
if (result->vset[0] == NULL) {
fprintf(stderr, "%s: Error: cannot malloc space in \"extractpmid\".\n",
pmProgname);
exit(1);
}
result->timestamp.tv_sec = timestamp->tv_sec;
result->timestamp.tv_usec = timestamp->tv_usec;
result->numpmid = 1;
result->vset[0]->pmid = vsetp->pmid;
result->vset[0]->numval = vsetp->numval;
result->vset[0]->valfmt = vsetp->valfmt;
for(i=0; i<vsetp->numval; i++) {
result->vset[0]->vlist[i].inst = vsetp->vlist[i].inst;
if (vsetp->valfmt == PM_VAL_INSITU)
result->vset[0]->vlist[i].value = vsetp->vlist[i].value;
else {
vbp = vsetp->vlist[i].value.pval;
size = (int)vbp->vlen;
result->vset[0]->vlist[i].value.pval = (pmValueBlock *)malloc(size);
if (result->vset[0]->vlist[i].value.pval == NULL) {
fprintf(stderr,
"%s: Error: cannot malloc space in \"extractpmid\".\n",
pmProgname);
exit(1);
}
result->vset[0]->vlist[i].value.pval->vtype = vbp->vtype;
result->vset[0]->vlist[i].value.pval->vlen = vbp->vlen;
/* in a pmValueBlock, the first byte is assigned to vtype,
* and the subsequent 3 bytes are assigned to vlen - that's
* a total of 4 bytes - the rest is used for vbuf
*/
if (vbp->vlen < 4) {
fprintf(stderr, "%s: Warning: pmValueBlock vlen (%u) is too small\n", pmProgname, vbp->vlen);
}
memcpy(result->vset[0]->vlist[i].value.pval->vbuf,
vbp->vbuf, vbp->vlen-4);
}
} /*for(i)*/
*resp = result;
}
rlist_t *
mk_rlist_t(void)
{
rlist_t *rlist;
if ((rlist = (rlist_t *)malloc(sizeof(rlist_t))) == NULL) {
fprintf(stderr, "%s: Error: cannot malloc space in \"mk_rlist_t\"\n",
pmProgname);
exit(1);
}
rlist->res = NULL;
rlist->next = NULL;
return(rlist);
}
/*
* insert rlist element in rlist list
*/
void
insertrlist(rlist_t **rlist, rlist_t *elm)
{
rlist_t *curr;
rlist_t *prev;
if (elm == NULL)
return;
elm->next = NULL;
if (*rlist == NULL) {
*rlist = elm;
return;
}
if (elm->res->timestamp.tv_sec < (*rlist)->res->timestamp.tv_sec ||
(elm->res->timestamp.tv_sec == (*rlist)->res->timestamp.tv_sec &&
elm->res->timestamp.tv_usec <= (*rlist)->res->timestamp.tv_usec)) {
curr = *rlist;
*rlist = elm;
(*rlist)->next = curr;
return;
}
curr = (*rlist)->next;
prev = *rlist;
while (curr != NULL) {
if (elm->res->timestamp.tv_sec < curr->res->timestamp.tv_sec ||
(elm->res->timestamp.tv_sec == curr->res->timestamp.tv_sec &&
elm->res->timestamp.tv_usec <= curr->res->timestamp.tv_usec)) {
break;
}
prev = curr;
curr = prev->next;
}
prev->next = elm;
elm->next = curr;
}
/*
* insert pmResult in rlist list
*/
void
insertresult(rlist_t **rlist, pmResult *result)
{
rlist_t *elm;
elm = mk_rlist_t();
elm->res = result;
elm->next = NULL;
insertrlist (rlist, elm);
}
/*
* Find out whether the metrics in _result are in the metric list ml
*/
pmResult *
searchmlist(pmResult *_Oresult)
{
int i;
int j;
int k;
int q;
int r;
int found = 0;
int maxinst = 0; /* max number of instances */
int numpmid = 0;
int *ilist;
int *jlist;
pmResult *_Nresult;
pmValue *vlistp = NULL; /* temporary list of instances */
pmValueSet *vsetp; /* value set pointer */
ilist = (int *) malloc(_Oresult->numpmid * sizeof(int));
if (ilist == NULL)
goto nomem;
jlist = (int *) malloc(_Oresult->numpmid * sizeof(int));
if (jlist == NULL)
goto nomem;
/* find out how many of the pmid's in _Oresult need to be written out
* (also, find out the maximum number of instances to write out)
*/
numpmid = 0;
maxinst = 0;
for (i=0; i<_Oresult->numpmid; i++) {
vsetp = _Oresult->vset[i];
for (j=0; j<ml_numpmid; j++) {
if (vsetp->pmid == ml[j].idesc->pmid) {
/* pmid has been found in metric list
*/
if (ml[j].numinst > maxinst)
maxinst = ml[j].numinst;
++numpmid;
ilist[numpmid-1] = i; /* _Oresult index */
jlist[numpmid-1] = j; /* ml list index */
break;
}
}
}
/* if no matches (no pmid's are ready for writing), then return
*/
if (numpmid == 0) {
free(ilist);
free(jlist);
return(NULL);
}
/* `numpmid' matches were found (some or all pmid's are ready for writing),
* then allocate space for new result
*/
_Nresult = (pmResult *) malloc(sizeof(pmResult) +
(numpmid - 1) * sizeof(pmValueSet *));
if (_Nresult == NULL)
goto nomem;
_Nresult->timestamp.tv_sec = _Oresult->timestamp.tv_sec;
_Nresult->timestamp.tv_usec = _Oresult->timestamp.tv_usec;
_Nresult->numpmid = numpmid;
/* make array for indeces into vlist
*/
if (maxinst > 0) {
vlistp = (pmValue *) malloc(maxinst * sizeof(pmValue));
if (vlistp == NULL)
goto nomem;
}
/* point _Nresult at the right pmValueSet(s)
*/
for (k=0; k<numpmid; k++) {
i = ilist[k];
j = jlist[k];
/* point new result at the wanted pmid
*/
_Nresult->vset[k] = _Oresult->vset[i];
/* allocate the right instances
*/
vsetp = _Nresult->vset[k];
found = 0;
for (q=0; q<ml[j].numinst; q++) {
for (r=0; r<vsetp->numval; r++) {
/* if id in ml is -1, chances are that we haven't seen
* it before ... set the instance id
*/
if (ml[j].instlist[q] < 0)
ml[j].instlist[q] = vsetp->vlist[r].inst;
if (ml[j].instlist[q] == vsetp->vlist[r].inst) {
/* instance has been found
*/
vlistp[found].inst = vsetp->vlist[r].inst;
vlistp[found].value = vsetp->vlist[r].value;
++found;
break;
} /*if*/
} /*for(r)*/
} /*for(q)*/
/* note: found may be <= ml[j].numinst
* further more, found may be zero ... deal with this later?
* - NUMVAL
*/
vsetp->numval = found;
for (q=0; q<vsetp->numval; q++) {
vsetp->vlist[q].inst = vlistp[q].inst;
vsetp->vlist[q].value = vlistp[q].value;
vlistp[q].inst = 0;
vlistp[q].value.lval = 0;
} /*for(q)*/
} /*for(k)*/
free(ilist);
free(jlist);
if (maxinst > 0) free(vlistp); /* free only if space was allocated */
return(_Nresult);
nomem:
fprintf(stderr, "%s: Error: cannot malloc space in \"searchmlist\".\n",
pmProgname);
exit(1);
}
|