'\" t '\"macro stdmacro .\" .\" Copyright (c) 2009 Ken McDonell. 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 PMREGISTERDERIVED 3 "" "Performance Co-Pilot" .SH NAME \f3pmRegisterDerived\f1 \- register a derived metric name and definition .SH "C SYNOPSIS" .ft 3 #include .sp char *pmRegisterDerived(char *\fIname\fP, char *\fIexpr\fP); .sp cc ... \-lpcp .ft 1 .SH DESCRIPTION .PP Derived metrics provide a way of extending the Performance Metrics Name Space (PMNS) with new metrics defined at the PCP client-side using arithmetic expressions over the existing performance metrics. .PP Typical uses would be to aggregate a number of similar metrics to provide a higher-level summary metric or to support the ``delta V over delta V'' class of metrics that are not possible in the base data semantics of PCP. An example of the latter class would be the average I/O size, defined as .br .ce .ft CW delta(disk.dev.total_bytes) / delta(disk.dev.total) .ft R where both of the .ft CW disk.dev .ft R metrics are counters, and what is required is to to sample both metrics, compute the difference between the current and previous values and then calculate the ratio of these differences. .PP The arguments to .B pmRegisterDerived are the .I name of the new derived metric and .I expr is an arithmetic expression defining how the values of .I name should be computed. .PP .I name should follow the syntactic rules for the names of performance metrics, namely one or more components separated with a dot (``.''), and each component must begin with an alphabetic followed by zero or more characters drawn from the alphabetics, numerics and underscore (``_''). For more details, refer to .BR PCPIntro (1) and .BR pmns (5). .PP .I name must be unique across all derived metrics and should .B not match the name of any regular metric in the PMNS. It is acceptable for .I name to share some part of its prefix with an existing subtree of the PMNS, e.g. the average I/O size metric above could be named .ft CW disk.dev.avgsz .ft R which would place it amongst the other .ft CW disk.dev .ft R metrics in the PMNS. Alternatively, derived metrics could populate their own subtree of the PMNS, e.g. the average I/O size metric above could be named .ft CW my.summary.disk.avgsz\c .ft R \&. .PP The expression .I expr follows these syntactic rules: .IP * 2n Terminal elements are either names of existing metrics or integer constants. Recursive definitions are not allowed, so only the names of regular metrics (not other derived metrics) may be used. Integer constants are constrained to the precision of 32-bit unsigned integers. .IP * 2n The usual binary arithmetic operators are supported, namely \- addition (``+''), subtraction (``-''), multiplication (``*'') and division (``/'') with the normal precedence rules where multiplication and division have higher precedence than addition and subtraction, so .ft CW a+b*c .ft R is evaluated as .ft CW a+(b*c)\c .ft R . .IP * 2n Parenthesis may be used for grouping. .IP * 2n The following unary functions operate on a single performance metric and return one or more values. For all functions (except .ft CW count()\c .ft R ), the type of the operand metric must be arithmetic (integer of various sizes and signedness, float or double). .TS box,center; cf(R) | cf(R)w(5i) lf(CW) | lf(R). Function Value _ avg(x) T{ .fi A singular instance being the average value across all instances for the metric x. T} _ count(x) T{ .fi A singular instance being the count of the number of instances for the metric x. T} _ delta(x) T{ .fi Returns the difference in values for the metric x between one call to .BR pmFetch (3) and the next. There is one value in the result for each instance that appears in both the current and the previous sample. T} _ rate(x) T{ .fi Returns the difference in values for the metric x between one call to .BR pmFetch (3) and the next divided by the elapsed time between the calls to .BR pmFetch (3). The semantics of the derived metric are based on the semantics of the operand (x) with the dimension in the .B time domain decreased by one and scaling if required in the time utilization case where the operand is in units of time, and the derived metric is unitless. This mimics the rate conversion applied to counter metrics by tools such as .BR pmval (1), .BR pmie (1) and .BR pmchart (1). There is one value in the result for each instance that appears in both the current and the previous sample. T} _ max(x) T{ .fi A singular instance being the maximum value across all instances for the metric x. T} _ min(x) T{ .fi A singular instance being the minimum value across all instances for the metric x. T} _ sum(x) T{ .fi A singular instance being the sum of the values across all instances for the metric x. T} .TE .IP * 2n White space is ignored. .PP Syntactic checking is performed at the time .B pmRegisterDerived is called, but semantic checking is deferred until each new context is created with .BR pmNewContext (3) or re-established with .BR pmReconnectContext (3), at which time the PMNS and metadata is available to allow semantic checking and the metadata of the derived metrics to be established. .SH "SEMANTIC CHECKS AND RULES" .PP There are a number of conversions required to determine the metadata for a derived metric and to ensure the semantics of the expressions are sound. .PP In a binary expression, if the semantics of both operands is not a counter (i.e. PM_SEM_INSTANT or PM_SEM_DISCRETE) then the result will have semantics PM_SEM_INSTANT unless both operands are PM_SEM_DISCRETE in which case the result is also PM_SEM_DISCRETE. .PP The mapping of the pmUnits of the metadata uses the following rules: .IP * 2n If both operands have a dimension of COUNT and the scales are not the same, use the larger scale and convert the values of the operand with the smaller scale. .IP * 2n If both operands have a dimension of TIME and the scales are not the same, use the larger scale and convert the values of the operand with the smaller scale. .IP * 2n If both operands have a dimension of SPACE and the scales are not the same, use the larger scale and convert the values of the operand with the smaller scale. .IP * 2n For addition and subtraction all dimensions for each of the operands and result are identical. .IP * 2n For multiplication, the dimensions of the result are the sum of the dimensions of the operands. .IP * 2n For division, the dimensions of the result are the difference of the dimensions of the operands. .PP Scale conversion involves division if the dimension is positive else multiplication if the dimension is negative. If scale conversion is applied to either of the operands, the result is promoted to type PM_TYPE_DOUBLE. .PP Putting all of this together in an example, consider the derived metric defined as follows: .br .ad c .ft CW x = network.interface.speed - delta(network.interface.in.bytes) / delta(sample.milliseconds) .ft R .br .ad l The type, dimension and scale settings would propagate up the expression tree as follows. .TS box,center; cf(R) | cf(R) | cf(R) | cf(R) lf(CW) | lf(CW) | lf(R) | lf(R). Expression Type T{ .fi Dimension & Scale T} T{ .fi Scale Factor(s) T} _ sample.milliseconds DOUBLE millisec delta(...) DOUBLE millisec network...bytes U64 byte delta(...) U64 byte delta(...) / delta(...) DOUBLE byte/millisec T{ .fi /1048576 and *1000 T} network...speed FLOAT Mbyte/sec x DOUBLE Mbyte/sec .TE .PP Because semantic checking cannot be done at the time .B pmRegisterDerived is called, errors found during semantic checking are reported using .BR pmprintf (3). These include: .TP Error: derived metric : operand: : There was a problem calling .BR pmLookupName (3) to identify the operand metric used in the definition of the derived metric . .TP Error: derived metric : operand ( []): There was a problem calling .BR pmLookupDesc (3) to identify the operand metric with PMID used in the definition of the derived metric . .TP Semantic error: derived metric : : Illegal operator for counters If both operands have the semantics of counter, only addition or subtraction make sense, so multiplication and division are not allowed. .TP Semantic error: derived metric : : Illegal operator for counter and non-counter Only multiplication or division are allowed if the left operand has the semantics of a counter and the right operand is .B not a counter. .TP Semantic error: derived metric : : Illegal operator for non-counter and counter Only multiplication is allowed if the right operand has the semantics of a counter and the left operand is .B not a counter. .TP Semantic error: derived metric : : Non-arithmetic type for operand The binary arithmetic operators are only allowed with operands with an arithmetic type (integer of various sizes and signedness, float or double). .TP Semantic error: derived metric : (): Non-arithmetic operand for function The unary functions are only defined if the operand has arithmetic type. .TP Semantic error: derived metric : Incorrect time dimension for operand Rate conversion using the .BR rate () function is only possible for operand metrics with a Time dimension of 0 or 1 (see .BR pmLookupDesc (3)). If the operand metric's Time dimension is 0, then the derived metrics has a value "per second" (Time dimension of -1). If the operand metric's Time dimension is 1, then the derived metrics has a value of time utilization (Time dimension of 0). .SH "EXPRESSION EVALUATION" For the binary arithmetic operators, if either operand must be scaled (e.g. convert bytes to Kbytes) then the result is promoted to PM_TYPE_DOUBLE. Otherwise the type of the result is determined by the types of the operands, as per the following table which is evaluated from top to bottom until a match is found. .TS box,center; cf(R) | cf(R) | cf(R) lf(R) | lf(R) | lf(R). Operand Types Operator Result Type _ either is PM_TYPE_DOUBLE any PM_TYPE_DOUBLE _ any division PM_TYPE_DOUBLE _ either is PM_TYPE_FLOAT any PM_TYPE_FLOAT _ either is PM_TYPE_U64 any PM_TYPE_U64 _ either is PM_TYPE_64 any PM_TYPE_64 _ either is PM_TYPE_U32 any PM_TYPE_U32 _ T{ .fi otherwise (both are PM_TYPE_32) T} any PM_TYPE_32 .TE .SH CAVEATS .PP Unary negation is not supported, so the following expressions would be syntactically incorrect, .ft CW \-3*abc .ft R and .ft CW \-this.number\c .ft R . .PP Derived metrics are not available when using .BR pmFetchArchive (3) as this routine does not use a target list of PMIDs that could be remapped (as is done for .BR pmFetch (3)). .PP .B pmRegisterDerived does not apply retrospectively to any open contexts, so the normal use would be to make all calls to .B pmRegisterDerived (possibly via .BR pmLoadDerivedConfig (3)) and then call .BR pmNewContext (3). .PP There is no .B pmUnregisterDerived method, so once registered a derived metric persists for the life of the application. .SH DIAGNOSTICS .PP On success, .B pmRegisterDerived returns NULL. .PP If a syntactic error is found at the time of registration, the value returned by .B pmRegisterDerived is a pointer into .I expr indicating .B where the error was found. To identify .B what the error was, the application should call .BR pmDerivedErrStr (3) to retrieve the corresponding parser error message. .SH SEE ALSO .BR PCPIntro (1), .BR PMAPI (3), .BR pmDerivedErrStr (3), .BR pmFetch (3), .BR pmLoadDerivedConfig (3), .BR pmNewContext (3) and .BR pmReconnectContext (3).