<commit_before>'\"! 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,
\f3pmdaCacheResize\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);
.br
.ti -8n
int pmdaCacheResize(pmInDom \fIindom\fP, int \fImaximum\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
an 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
The upper bound for identifiers allocated for any given
.I indom
cache can be optionally
reduced from the default (2^31\0\-\01) to some lesser
.IR maximum ,
using
.BR pmdaCacheResize .
This
.I maximum
will then be persisted and restored in the usual manner, and
can thus be associated permanently with a cache once set.
This has applications when using these interfaces as general
purpose identifier caches, and is less applicable when using
them for instance domain caching.
.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 opaque 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 (or lesser
.IR maximum ,
if set).  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<commit_msg>docs: minor typo fix on the pmdacache man page<commit_after>'\"! 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,
\f3pmdaCacheResize\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);
.br
.ti -8n
int pmdaCacheResize(pmInDom \fIindom\fP, int \fImaximum\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
an 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
The upper bound for identifiers allocated for any given
.I indom
cache can be optionally
reduced from the default (2^31\0\-\01) to some lesser
.IR maximum ,
using
.BR pmdaCacheResize .
This
.I maximum
will then be persisted and restored in the usual manner, and
can thus be associated permanently with a cache once set.
This has applications when using these interfaces as general
purpose identifier caches, and is less applicable when using
them for instance domain caching.
.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 opaque 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 (or lesser
.IR maximum ,
if set).  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, at 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
{'fraction_non_alphanumeric': 0.053219488309462604, 'fraction_numerical': 0.005998286203941731, 'mean_word_length': 4.090668328400063, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2317153', 'n_tokens_mistral': 10026, 'n_tokens_neox': 9569, 'n_words': 5137}