summaryrefslogtreecommitdiff
path: root/man/man3/pmdacache.3
blob: d0b6726a8dd5ac037243549a7870ee0ae0a85463 (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
'\"! tbl | mmdoc
'\"macro stdmacro
.\"
.\" Copyright (c) 2013 Red Hat.
.\" Copyright (c) 2000-2004 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.
.\" 
.\"
.TH PMDACACHE 3 "PCP" "Performance Co-Pilot"
.SH NAME
\f3pmdaCacheStore\f1,
\f3pmdaCacheStoreKey\f1,
\f3pmdaCacheLookup\f1,
\f3pmdaCacheLookupName\f1,
\f3pmdaCacheLookupKey\f1,
\f3pmdaCacheOp\f1,
\f3pmdaCachePurge\f1 \- manage a cache of instance domain information for a PMDA
.SH "C SYNOPSIS"
.ft 3
#include <pcp/pmapi.h>
.br
#include <pcp/pmda.h>
.sp
.ad l
.hy 0
.in +8n
.ti -8n
int pmdaCacheStore(pmInDom \fIindom\fP, int \fIflags\fP, const\ char\ *\fIname\fP, void\ *\fIprivate\fP);
.br
.ti -8n
int pmdaCacheStoreKey(pmInDom \fIindom\fP, int \fIflags\fP, const\ char\ *\fIname\fP, int\ \fIkeylen\fP, const void\ *\fIkey\fP, void\ *\fIprivate\fP);
.br
.ti -8n
int pmdaCacheLookup(pmInDom \fIindom\fP, int \fIinst\fP, char **\fIname\fP, void\ **\fIprivate\fP);
.br
.ti -8n
int pmdaCacheLookupName(pmInDom \fIindom\fP, const char *\fIname\fP, int\ *\fIinst\fP, void\ **\fIprivate\fP);
.br
.ti -8n
int pmdaCacheLookupKey(pmInDom \fIindom\fP, const char *\fIname\fP, int\ \fIkeylen\fP, const void\ *\fIkey\fP, char **\fIoname\fP, int\ *\fIinst\fP, void\ **\fIprivate\fP);
.br
.ti -8n
int pmdaCacheOp(pmInDom \fIindom\fP, int \fIop\fP);
.br
.ti -8n
int pmdaCachePurge(pmInDom \fIindom\fP, time_t \fIrecent\fP);
.sp
.in
.hy
.ad
cc ... \-lpcp_pmda \-lpcp
.ft 1
.de EX
.in +2n
.ie t .ft C
.el .ft B
.ie t .sp .5v
.el .sp
.ta \\w' 'u*8
.nf
..
.de EE
.fi
.ie t .sp .5v
.el .sp
.ft R
.in
..
.SH DESCRIPTION
The
.B pmdaCache
family of routines provide services to support the maintenance of
complex instance domains for Performance Co-Pilot PMDAs.
There is potentially one cache of information for each instance
domain, and for each instance the cache maintains:
.PD 0
.IP \- 2m
external instance name (supplied by the PMDA)
.IP \- 2m
internal instance identifier (assigned by
.B pmdaCacheStore
or calculated from a ``hint'' by
.BR pmdaCacheStoreKey )
.IP \- 2m
state, where
.B active
instances are visible and part of the current
instance domain, and
.B inactive
instances are hidden, but not forgotten;
.B pmdaCacheStore
or
.B pmdaCacheStoreKey
may be used to change the state of an instance
.IP \- 2m
an optional opaque pointer to data that is associated with the instance,
but maintained by the PMDA 
.IP \- 2m
an optional opaque key that is used as a ``hint'' to
.B pmdaCacheStoreKey
when guessing the initial internal instance identifier
.IP \- 2m
the last time the cache was saved and the instance had been marked as
.B active
at some point since the previous cache load or save operation
.PD
.PP
The semantics of a PCP instance domain require a number of rules to
be followed, namely:
.PD 0
.IP 1. 3n
Each internal instance identifier must be unique and in the range
0 to 2^31\0\-\01.
This rule is enforced by the
.B pmdaCache
family of routines.
.IP 2. 3n
The external instance name must be unique.  When the instance name
contains a space, it is further constrained such that the name to
the left of the first space (the short name) must also be unique.
Refer to the INSTANCE NAME MATCHING section below.
The PMDA must honor this rule, the
.B pmdaCache
family of routines will detect attempts to violate this rule.
.IP 3. 3n
Where an external instance name corresponds to some object or entity,
there is an expectation that the association between the name and
the object is fixed, e.g. ``/dev/hda'' is always the name of the same disk
on a particular system.
This rule is perhaps the responsibility of the PMDA, but is often
a characteristic of the environment in which the PMDA runs.
.IP 4. 3n
It is preferable, although not mandatory, for the association between
and external instance name and an internal instance identifier to
be persistent.
This rule is supported by the
.B pmdaCache
family of routines.
.IP 5. 3n
When opaque keys are used, the values of the keys must be unique across all
instances within an instance domain.
This rule is enforced by the
.B pmdaCache
family of routines.
.PD
.PP
The visible interface to the cache is oriented towards the PMDA
developer who is most concerned about the names of instances, while
the details of how the rest of the PCP infrastructure
expects the internal instance identifiers
to be managed is not relevant.
.PP
Instances are updated in the cache for instance domain
.I indom
by calling
.B pmdaCacheStore
or
.B pmdaCacheStoreKey
with the external name of the instance passed via
.I name.
The opaque pointer
.I private
may be used to associate additional data with the entry in the cache;
if no such data is required,
.I private
should be NULL.
Any manipulation of the additional data (including allocation or
freeing) is the responsibility of the PMDA caller, as the cache simply
maintains the pointer to the data
(passed via
.IR private ).
.PP
For cases where the PMDA developer wishes to influence the allocation
of internal instance identifiers, e.g. for instance domains with more
than one natural dimension, or where there is a desire to allocate the same
instance identifier each time the PMDA is started, even on different
hosts,
.B pmdaCacheStoreKey
may be used.
In this case, an initial ``hint'' for the instance identifier is provided
as an opqaue key via
the first
.I keylen
bytes in
.I key
(which could be any sort of data, including binary values)
else if
.I keylen
is less than 1 or
.I key
is
.B NULL
then
.I name
is used as the ``hint''.
The ``hint'' is hashed to produce an initial instance identifier in the range
0 to 2^31\0-\01.  If this instance identifier is already allocated, then the
value is rehashed.  This procedure is repeated until an unallocated
instance identifier is found, or
.B pmdaCacheStoreKey
gives up and returns
.BR PM_ERR_GENERIC .
For each instance domain, the ``hint'' must be unique across all
instances, else
.B pmdaCacheStoreKey
returns
.BR PM_ERR_INST .
.PP
The
.I flags
argument controls how the instance should be processed in the cache
as follows:
.TP
PMDA_CACHE_ADD
Insert the entry into the cache if it is not already there and mark
it
.BR active .
If the entry is already in the cache mark it
.BR active .
.TP
PMDA_CACHE_HIDE
Mark the entry in the cache as
.BR inactive ,
but remember the
details of the association between the
external instance name and the internal instance identifier.
Entries that are
.B inactive
will be hidden from cache traversal via PMDA_CACHE_WALK_NEXT
operations, but remain visible to
.BR pmdaCacheLookup ,
.B pmdaCacheLookupName
and
.B pmdaCacheLookupKey
requests.
.TP
PMDA_CACHE_CULL
Remove the entry from the cache.
.PP
On success
.B pmdaCacheStore
or
.B pmdaCacheStoreKey
will return the internal instance identifier of the associated cache
entry.
Valid instance identifiers are guaranteed to be unique and non-negative.
Failure will be indicated by a negative value (suitable for decoding
with
.BR pmErrStr (3))
and most likely PM_ERR_INST to indicate the requested instance is not
in the cache, or \-EINVAL to indicate a potential violation of the
short name uniqueness property
(see the INSTANCE NAME MATCHING section below).
.PP
.B pmdaCacheLookup
is used to search the
entries in the cache based on the internal
instance identifier
.IR inst .
.PP
On success the return value will be PMDA_CACHE_ACTIVE or PMDA_CACHE_INACTIVE
(depending on the
.B active
or
.B inactive
state of the cache entry),
.I name
(if not NULL) and
.I private
(if not NULL)
will be set to the external instance name and the associate additional data
area as provided when the instance was last activated via
.B pmdaCacheStore 
or
.BR pmdaCacheStoreKey .
.PP
.B pmdaCacheLookup
failure is indicated by a negative return value
suitable for decoding with
.BR pmErrStr (3).
.PP
The
.B pmdaCacheLookup
interface is required by the PMDA's fetch callback
that is registered via
.BR pmdaSetFetchCallback (3).
Here the internal instance identifier is passed to the fetch callback
to identifier for which instance a value is required.
Typical usage is shown in the code fragment below.
.EX
static int
foo_callback(pmdaMetric *mdesc, unsigned int inst, pmAtomValue *atom)
{
    mydata   *mdp;
    char     *name;
    int      sts;

    sts = pmdaCacheLookup(mdesc->m_desc.indom, inst, &name, (void **)&mdp);
    /*
     * expect sts == PMDA_CACHE_ACTIVE except for cataclysmic events
     * use mdp as required, name may be useful for diagnostics
     */
    ...
.EE
.PP
.B pmdaCacheLookupName
is used to search the
entries in the cache based on the external
instance name
.IR name .
.PP
On success the return value will be PMDA_CACHE_ACTIVE or PMDA_CACHE_INACTIVE
(depending on the
.B active
or
.B inactive
state of the cache entry),
.I inst
(if not NULL) and
.I private
(if not NULL)
will be set to the internal instance identifier and the associate additional data
area as provided when the instance was last activated via
.B pmdaCacheStore
or
.BR pmdaCacheStoreKey .
.PP
.B pmdaCacheLookupName
failure is indicated by a negative return value
suitable for decoding with
.BR pmErrStr (3).
.PP
The
.B pmdaCacheLookupName
interface is useful for PMDAs wishing to update an instance domain based
on the external instance names.
.PP
.B pmdaCacheLookupKey
is used to search the entries in the cache
based on an opaque key (or ``hint'') previously used in a call to
.BR pmdaCacheStoreKey .
The ``hint'' is provided via the first
.I keylen
bytes in
.IR key .
For symmetry with
.BR pmdaCacheStoreKey ,
if
.I keylen
is less than 1 or
.I key
is
.B NULL
then
.I name
is used as the ``hint'' (although the results will be the same as
calling
.B pmdaCacheLookupName
in this case).
.PP
On success the return value will be PMDA_CACHE_ACTIVE or PMDA_CACHE_INACTIVE
(depending on the
.B active
or
.B inactive
state of the cache entry),
.I oname
(if not NULL),
.I inst
(if not NULL) and
.I private
(if not NULL)
will be set to the external instance name, the internal instance
identifier and the associate additional data
area as provided when the instance was last activated via
.B pmdaCacheStore 
or
.BR pmdaCacheStoreKey .
.PP
.B pmdaCacheLookupKey
failure is indicated by a negative return value
suitable for decoding with
.BR pmErrStr (3).
.PP
To avoid a persistent cache growing without bound,
.B pmdaCachePurge
can be used to cull all entries that have
.I not
been
.B active
in the last
.I recent
seconds.
For performance reasons, the time accounting is imprecise and the entries
are timestamped
at the time of the next cache save operation
.I after
the entry has been added or marked
.B active 
(refer to PMDA_CACHE_SAVE and PMDA_CACHE_SYNC below).
On success
.B pmdaCachePurge
returns the number of culled entries, else in the case of an error
the return value is negative (and suitable for decoding with
.BR pmErrStr (3)).
.PP
.B pmdaCacheOp
may be used to perform additional operations on the cache as follows:
.TP
PMDA_CACHE_LOAD
The cache can optionally be maintained as a persistent external file,
so that the mapping of instance names to instance identifiers is persistent
across executions of a PMDA.
This operation loads the cache from the external file, and then
all new cache entries are marked
.BR inactive ,
and the additional
data pointer is set to NULL.
Entries loaded from the external file are checked against the current
cache contents and if the instance name and instance identifiers match
then the state in the cache (\c
.B active
or
.BR inactive )
is not changed. Should a mismatch be found (same instance name and
different instance identifier, or same instance identifier and different
instance name, or some but not all of the instance identifier,
the instance name and the ``hint'' match)
then the entry from the external file is ignored
and a warning is issued on
.IR stderr .
Typically a PMDA would only
perform this operation once per execution.
.TP
PMDA_CACHE_SAVE
If any instance has been added to, or deleted from, the instance
domain since the last PMDA_CACHE_LOAD, PMDA_CACHE_SAVE or PMDA_CACHE_SYNC
operation, the
.I entire
cache is written to the external file as a bulk operation.
This operation is provided for PMDAs that are
.I not
interested
in using
.B pmdaCachePurge
and simply want the external file to reflect the set of known instances
without accurate details of when they were last marked
.BR active .
.RS
.PP
Returns the number of instances saved to the external file, else 0
if the external file was already up to date.
.RE
.TP
PMDA_CACHE_STRINGS
Annotates this cache as being a special-purpose cache used for string
de-duplication in PMDAs exporting large numbers of string valued metrics.
This can be used to reduce the memory footprint of the PMDA (duplicate
strings hash to the same bucket, and are stored in memory once only).
Key comparisons are not terminated at the first space but rather the
entire string is used for matching.
These are specialised caches not useful for general purpose instance
domain handling.
.TP
PMDA_CACHE_SYNC
Within an instance domain,
if any instance has been added to, or deleted from, or marked
.B active
since the last PMDA_CACHE_LOAD, PMDA_CACHE_SAVE or PMDA_CACHE_SYNC
operation, the
.I entire
cache is written to the external file as a bulk operation.
This operation is similar to PMDA_CACHE_SAVE, but will save the
instance domain more frequently so the timestamps more
accurately match the semantics expected by
.BR pmdaCachePurge .
.RS
.PP
Returns the number of instances saved to the external file, else 0
if the external file was already synchronized.
.RE
.TP
PMDA_CACHE_CHECK
Returns 1 if a cache exists for the specified instance domain,
else 0.
.TP
PMDA_CACHE_REUSE
When a new instance is added to the cache,
the default strategy is to assign instance identifiers in a monotonic
increasing
manner.  Once the maximum possible instance identifier value has been
assigned, the strategy changes to one where starting from 0,
the next available unused instance identifier will be used.
Calling
.B pmdaCacheOp
with PMDA_CACHE_REUSE forces an irreversible change to a second
(reuse) strategy where the next unallocated instance identifier
will be used.  This may be useful in cases where there is a
desire to restrict the allocated instance identifiers to smaller
values.  The prevailing strategy will be saved and restored across
PMDA_CACHE_SAVE and PMDA_CACHE_LOAD operations.
If
.B pmdaCacheStoreKey
is ever used, the associated instance domain will be changed to
PMDA_CACHE_REUSE mode.
.TP
PMDA_CACHE_REORG
Reorganize the cache to allow faster retrieval of
.B active
entries, and the cost of slower retrieval for
.B inactive
entries, and reclaim any culled entries.  The cache may be internally
re-organized as entries are added, so this operation is not required
for most PMDAs.
.TP
PMDA_CACHE_WALK_REWIND
Prepares for a traversal of the cache in ascending instance identifier
sequence.
.TP
PMDA_CACHE_WALK_NEXT
Fetch the next
.B active
instance identifier from the cache.  Requires a prior
call using PMDA_CACHE_WALK_REWIND and will return \-1 when all instances
have been processed.
.RS
.PP
Only one cache walk can be active at any given time, nesting calls
to PMDA_CACHE_WALK and PMDA_CACHE_REWIND will interfere with each
other.
.RE
.TP
PMDA_CACHE_ACTIVE
Changes
.B every
.B inactive
entry in the cache to be marked
.BR active .
.TP
PMDA_CACHE_INACTIVE
Changes
.B every
.B active
entry in the cache to be marked
.BR inactive .
.TP
PMDA_CACHE_CULL
Remove 
.B every
entry from the cache.
.TP
PMDA_CACHE_SIZE
Return the number of entries in the cache (includes
.BR active ,
.B inactive
and any culled entries that have not yet been reclaimed).
.TP
PMDA_CACHE_SIZE_ACTIVE
Return the number of
.B active
entries in the cache.
.TP
PMDA_CACHE_SIZE_INACTIVE
Return the number of
.B inactive
entries in the cache.
.TP
PMDA_CACHE_DUMP
Dump the current state of the cache on
.IR stderr .
.TP
PMDA_CACHE_DUMP_ALL
Like PMDA_CACHE_DUMP, but also dump the internal hashing structures
used to support lookup by instance name, lookup by instance identifier and
the collision statistics for ``hint'' hashing from
.BR pmdaCacheStoreKey .
.PP
.B pmdaCacheOp
returns a non-negative value on success, and failure is indicated
by a negative return value (suitable for decoding
with
.BR pmErrStr (3)).
.SH OTHER CONSIDERATIONS
.PP
When the
.B pmdaCache
routines are used for particular instance domain,
.B pmdaInstance (3)
and the instance domain enumeration behind
.BR pmdaFetch (3)
will attempt to extract instance domain information from the cache, thereby avoiding
reference to the
.B pmdaIndom
data structures that have historically been used to define instance domains
and service instance requests.
A PMDA can adopt a hybrid approach and choose to implement some instance
domains via the traditional
.B pmdaIndom
method, and others via the
.B pmdaCache
approach, however attempts to manage the
.I same
instance domain by both
methods will result in the
.B pmdaCache
method silently prevailing.
.PP
If
.B all
instances in a PMDA are to be serviced from a
.B pmdaCache
then a
.B pmdaIndom
is not required, and the
.B pmdaInit (3)
call becomes
.EX
    pmdaInit(dp, NULL, 0, metrictab, nmetrics);
.EE
However, the PMDA will need to explicitly initialize the
.B indom
field of the
.B pmDesc
in the
.I metrictab
entries, as this cannot be done by
.BR pmdaInit (3)
if
.I indomtab
is missing entries for the instance domains maintained in the cache.
.PP
Independent of how the instance domain is being maintained,
to refresh an instance domain prior to a fetch or an instance domain
operation, the standard methods of a ``wrapper'' to the
.B pmdaInstance (3)
and
.B pmdaFetch (3)
methods should be used.
.PP
Refer to the
.B simple
PMDA source code for an example use of the
.B pmdaCache
routines.
.PP
When using
.BR pmdaCacheStoreKey ,
if there is a desire to ensure the given ``hint'' generates the same
initial instance identifier across all platforms, then the caller
should ensure the endian and word-size issues are considered, e.g. if
the natural data structure used for the
.I key
is an array of 32-bit integers, then
.BR htonl (3)
should be used on each element of the array before calling
.B pmdaCacheStoreKey
or
.BR pmdaCacheLookupKey .
.SH INSTANCE NAME MATCHING
.PP
The following table summarizes the ``short name'' matching semantics
for an instance domain (caches other than PMDA_CACHE_STRINGS style).
.TS
box, center;
l | l | l
l | l | ^
l | l | l.
name in	\fBpmdaCacheLookup\fR	result
cache	name
_
foodle	foo	no match (PM_ERR_INST)
foo	foodle	no match (PM_ERR_INST)
foo	foo	match
foo bar	foo	match on short name (instance identifier)
foo bar	foo bar	match on full name (instance identifier)
foo	foo bar	bad match (\-EDOM)
foo bar	foo blah	bad match (\-EDOM)
.TE
.SH FILES
Cache persistence uses files with names constructed from the
.I indom
within the
.B $PCP_VAR_DIR/config/pmda
directory.
.SH SEE ALSO
.BR BYTEORDER (3),
.BR PMAPI (3),
.BR PMDA (3),
.BR pmdaInit (3),
.BR pmdaInstance (3),
.BR pmdaFetch (3),
.BR pmdaSetFetchCallback (3),
.BR pmErrStr (3)
and
.BR pmGetInDom (3).