Contained WithinFind More DocumentationFeatured Support Resources | Scarica il manuale in formato PDF (875 KB)
Chapter 4 Using the Perl Interface to Extended AccountingThe Perl interface provides a Perl binding to the extended accounting tasks and projects. The interface allows the accounting files produced by the exacct framework to be read by Perl scripts. The interface also allows the writing of exacct files by Perl scripts. This chapter includes the following topics: Extended Accounting OverviewThe exacct is a new accounting framework for the Solaris operating system provides additional functionality to that provided by the traditional SVR4 accounting mechanism. Traditional SVR4 accounting has these drawbacks:
The exacct framework addresses the limitations of SVR4 accounting and provides a configurable, open, and extensible framework for the collection of accounting data.
For a more extensive overview of extended accounting, see Chapter 4, Extended Accounting (Overview), in System Administration Guide: Solaris Containers-Resource Management and Solaris Zones. Perl Interface to libexacctObject ModelThe Sun::Solaris::Exacct module is the parent of all the classes provided by libexacct(3LIB) library. libexacct(3LIB) provides operations on types of entities: exacct format files, catalog tags and exacct objects. exacct objects are subdivided into two types.
Benefits of Using the Perl Interface to libexacctThe Perl extensions to extended accounting provide a Perl interface to the underlying libexacct(3LIB) API and offer the following enhancements.
Perl Double-Typed ScalarsThe modules described in this document make extensive use of the Perl double-typed scalar facility. The double-typed scalar facility allows a scalar value to behave either as an integer or as a string, depending upon the context. This behavior is the same as exhibited by the $! Perl variable (errno). The double-typed scalar facility avoids the need to map from an integer value into the corresponding string in order to display a value. The following example illustrates the use of double-typed scalars. # Assume $obj is a Sun::Solaris::Item
my $type = $obj->type();
# prints out "2 EO_ITEM"
printf("%d %s\n", $type, $type);
# Behaves as an integer, $i == 2
my $i = 0 + $type;
# Behaves as a string, $s = "abc EO_ITEM xyx"
my $s = "abc $type xyz";
Perl ModulesThe various project, task and exacct-related functions have been separated into groups, and each group is placed in a separate Perl module. Each function has the Sun Microsystems standard Sun::Solaris:: Perl package prefix. Table 4–1 Perl Modules
Sun::Solaris::Project ModuleThe Sun::Solaris::Project module provides wrappers for the project-related system calls and the libproject(3LIB) library. Sun::Solaris::Project ConstantsThe Sun::Solaris::Project module uses constants from the project-related header files.
Sun::Solaris::Project Functions, Class Methods, and Object MethodsThe perl extensions to the libexacct(3LIB) API provide the following functions for projects.
The Sun::Solaris::Project module has no class methods. The Sun::Solaris::Project module has no object methods. Sun::Solaris::Project ExportsBy default, nothing is exported from this module. The following tags can be used to selectively import constants and functions defined in this module.
Sun::Solaris::Task ModuleThe Sun::Solaris::Task module provides wrappers for the settaskid(2) and gettaskid(2) system calls. Sun::Solaris::Task ConstantsThe Sun::Solaris::Task module uses the following constants.
Sun::Solaris::Task Functions, Class Methods, and Object MethodsThe perl extensions to the libexacct(3LIB) API provides the following functions for tasks.
The Sun::Solaris::Task module has no class methods. The Sun::Solaris::Task module has no object methods. Sun::Solaris::Task ExportsBy default, nothing is exported from this module. The following tags can be used to selectively import constants and functions defined in this module.
Sun::Solaris::Exacct ModuleThe Sun::Solaris::Exacct module provides wrappers for the ea_error(3EXACCT) function, and for all the exacct system calls. Sun::Solaris::Exacct ConstantsThe Sun::Solaris::Exacct module provides constants from the various exacct header files. The P_PID, P_TASKID, P_PROJID and all the EW_*, EP_*, EXR_* macros are extracted during the module build process. The macros are extracted from the exacct header files under /usr/include and provided as Perl constants. Constants passed to the Sun::Solaris::Exacct functions can either be an integer value such as. EW_FINAL or a string representation of the same variable such as. “EW_FINAL”. Sun::Solaris::Exacct Functions, Class Methods, and Object MethodsThe perl extensions to the libexacct(3LIB) API provide the following functions for the Sun::Solaris::Exacct module.
Note – ea_error_str() is provided as a convenience, so that repeated blocks of code like the following can be avoided: if (ea_error() == EXR_SYSCALL_FAIL) {
print("error: $!\n");
} else {
print("error: ", ea_error(), "\n");
}
The Sun::Solaris::Exacct module has no class methods. The Sun::Solaris::Exacct module has no object methods. Sun::Solaris::Exacct ExportsBy default, nothing is exported from this module. The following tags can be used to selectively import constants and functions defined in this module.
Sun::Solaris::Exacct::Catalog ModuleThe Sun::Solaris::Exacct::Catalog module provides a wrapper around the 32-bit integer used as a catalog tag. The catalog tag is represented as a Perl object blessed into the Sun::Solaris::Exacct::Catalog class. Methods can be used to manipulate fields in a catalog tag. Sun::Solaris::Exacct::Catalog ConstantsAll the EXT_*, EXC_* and EXD_* macros are extracted during the module build process from the /usr/include/sys/exact_catalog.h file and are provided as constants. Constants passed to the Sun::Solaris::Exacct::Catalog methods can either be an integer value, such as EXT_UINT8, or the string representation of the same variable, such as “EXT_UINT8”. Sun::Solaris::Exacct::Catalog Functions, Class Methods, and Object MethodsThe Perl extensions to the libexacct(3LIB) API provide the following class methods forSun::Solaris::Exacct::Catalog. Exacct(3PERL) andExacct::Catalog(3PERL)
The Perl extensions to the libexacct(3LIB) API provide the following object methods for Sun::Solaris::Exacct::Catalog.
Sun::Solaris::Exacct::Catalog ExportsBy default, nothing is exported from this module. The following tags can be used to selectively import constants and functions defined in this module.
Additionally, any constants defined with the register() function can optionally be exported into the caller's package. Sun::Solaris::Exacct::File ModuleThe Sun::Solaris::Exacct::File module provides wrappers for the exacct functions that manipulate accounting files. The interface is object-oriented, and allows the creation and reading of exacct files. The C library calls that are wrapped by this module are:
The file read and write methods operate on Sun::Solaris::Exacct::Object objects. These methods perform all the necessary memory management, packing, unpacking and structure conversions that are required. Sun::Solaris::Exacct::File ConstantsSun::Solaris::Exacct::File provides the EO_HEAD, EO_TAIL, EO_NO_VALID_HDR, EO_POSN_MSK and EO_VALIDATE_MSK constants. Other constants that are needed by the new() method are in the standard Perl Fcntl module. Table 4–2 describes the action of new() for various values of $oflags and $aflags. Sun::Solaris::Exacct::File Functions, Class Methods, and Object MethodsThe Sun::Solaris::Exacct::File module has no functions. The Perl extensions to the libexacct(3LIB) API provide the following class method forSun::Solaris::Exacct::File.
The following table describes the new() action for combinations of the $oflags and $aflags parameters. Table 4–2 $oflags and $aflags Parameters
Note – The only valid values for $oflags are the combinations of O_RDONLY, O_WRONLY, O_RDWR or O_CREAT. $aflags describes the required positioning in the file for O_RDONLY. Either EO_HEAD or EO_TAIL are allowed. If absent, EO_HEAD is assumed. The perl extensions to the libexacct(3LIB) API provide the following object methods forSun::Solaris::Exacct::File.
Note – Close a Sun::Solaris::Exacct::File. There is no explicit close() method for a Sun::Solaris::Exacct::File. The file is closed when the filehandle object is undefined or reassigned. Sun::Solaris::Exacct::File ExportsBy default, nothing is exported from this module. The following tags can be used to selectively import constants that are defined in this module.
Sun::Solaris::Exacct::Object ModuleThe Sun::Solaris::Exacct::Object module serves as a parent of the two possible types of exacct objects: Items and Groups. An exacct Item is a single data value, an embedded exacct object, or a block of raw data. An example of a single data value is the number of seconds of user CPU time consumed by a process. An exacct Group is an ordered collection of exacct Items such as all of the resource usage values for a particular process or task. If Groups need to be nested within each other, the inner Groups can be stored as embedded exacct objects inside the enclosing Group. The Sun::Solaris::Exacct::Object module contains methods that are common to both exacct Items and Groups. Note that the attributes of Sun::Solaris::Exacct::Object and all classes derived from it are read-only after initial creation via new(). The attributes made read-only prevents the inadvertent modification of the attributes which could give rise to inconsistent catalog tags and data values. The only exception to the read-only attributes is the array used to store the Items inside a Group object. This array can be modified using the normal perl array operators. Sun::Solaris::Exacct::Object ConstantsSun::Solaris::Exacct::Object provides the EO_ERROR, EO_NONE, EO_ITEM and EO_GROUP constants. Sun::Solaris::Exacct::Object Functions, Class Methods, and Object MethodsThe Sun::Solaris::Exacct::Object module has no functions. The Perl extensions to the libexacct(3LIB) API provide the following class method forSun::Solaris::Exacct::Object.
The Perl extensions to the libexacct(3LIB) API provide the following object methods forSun::Solaris::Exacct::Object.
Sun::Solaris::Exacct::Object ExportsBy default, nothing is exported from this module. The following tags can be used to selectively import constants and functions defined in this module.
Sun::Solaris::Exacct::Object::Item ModuleThe Sun::Solaris::Exacct::Object::Item module is used for exacct data Items. An exacct data Item is represented as an opaque reference, blessed into the Sun::Solaris::Exacct::Object::Item class, which is a subclass of the Sun::Solaris::Exacct::Object class. The underlying exacct data types are mapped onto Perl types as follows. Table 4–3 exacct Data Types Mapped to Perl Data Types
Sun::Solaris::Exacct::Object::Item ConstantsSun::Solaris::Exacct::Object::Item has no constants. Sun::Solaris::Exacct::Object::Item Functions, Class Methods, and Object MethodsSun::Solaris::Exacct::Object::Item has no functions. Sun::Solaris::Exacct::Object::Item inherits all class methods from the Sun::Solaris::Exacct::Object base class, plus the new() class method.
Sun::Solaris::Exacct::Object::Item inherits all object methods from the Sun::Solaris::Exacct::Object base class. Sun::Solaris::Exacct::Object::Item ExportsSun::Solaris::Exacct::Object::Item has no exports. Sun::Solaris::Exacct::Object::Group ModuleThe Sun::Solaris::Exacct::Object::Group module is used for exacct Group objects. An exacct Group object is represented as an opaque reference, blessed into the Sun::Solaris::Exacct::Object::Group class, which is a subclass of the Sun::Solaris::Exacct::Object class. The Items within a Group are stored inside a Perl array, and a reference to the array can be accessed via the inherited value() method. This means that the individual Items within a Group can be manipulated with the normal Perl array syntax and operators. All data elements of the array must be derived from the Sun::Solaris::Exacct::Object class. Group objects can also be nested inside each other merely by adding an existing Group as a data Item. Sun::Solaris::Exacct::Object::Group ConstantsSun::Solaris::Exacct::Object::Group has no constants. Sun::Solaris::Exacct::Object::Group Functions, Class Methods, and Object MethodsSun::Solaris::Exacct::Object::Group has no functions. Sun::Solaris::Exacct::Object::Group inherits all class methods from the Sun::Solaris::Exacct::Object base class, plus the new() class method.
Sun::Solaris::Exacct::Object::Group inherits all object methods from the Sun::Solaris::Exacct::Object base class, plus the new() class method.
Sun::Solaris::Exacct::Object::Group ExportsSun::Solaris::Exacct::Object::Group has no exports. Sun::Solaris::Exacct::Object::_Array ModuleThe Sun::Solaris::Exacct::Object::_Array class is used internally for enforcing type checking of the data Items that are placed in an exacct Group. Sun::Solaris::Exacct::Object::_Array should not be created directly by the user. Sun::Solaris::Exacct::Object::_Array ConstantsSun::Solaris::Exacct::Object::_Array has no constants. Sun::Solaris::Exacct::Object::_Array Functions, Class Methods, and Object MethodsSun::Solaris::Exacct::Object::_Array has no functions. Sun::Solaris::Exacct::Object::_Array has internal-use class methods. Sun::Solaris::Exacct::Object::_Array uses perl TIEARRAY methods. Sun::Solaris::Exacct::Object::_Array ExportsSun::Solaris::Exacct::Object::_Array has no exports. Perl Code ExamplesThis section shows perl code examples for accessing exacct files. Example 4–1 Using the Pseudocode PrototypeIn typical use the Perl exacct library reads existing exacct files. Use pseudocode to show the relationships of the various Perl exacct classes. Illustrate in pseudocode the process of opening and scanning an exacct file, and processing objects of interest. In the following pseudocode, the ‘convenience’ functions are used in the interest of clarity. -- Open the exacct file ($f is a Sun::Solaris::Exacct::File)
my $f = ea_new_file(...)
-- While not EOF ($o is a Sun::Solaris::Exacct::Object)
while (my $o = $f->get())
-- Check to see if object is of interest
if ($o->type() == &EO_ITEM)
...
-- Retrieve the catalog ($c is a Sun::Solaris::Exacct::Catalog)
$c = $o->catalog()
-- Retrieve the value
$v = $o->value();
-- $v is a reference to a Sun::Solaris::Exacct::Group for a Group
if (ref($v))
....
-- $v is perl scalar for Items
else
Example 4–2 Recursively dumping an exacct Objectsub dump_object
{
my ($obj, $indent) = @_;
my $istr = ' ' x $indent;
#
# Retrieve the catalog tag. Because we are doing this in an array
# context, the catalog tag will be returned as a (type, catalog, id)
# triplet, where each member of the triplet will behave as an integer
# or a string, depending on context. If instead this next line provided
# a scalar context, e.g.
# my $cat = $obj->catalog()->value();
# then $cat would be set to the integer value of the catalog tag.
#
my @cat = $obj->catalog()->value();
#
# If the object is a plain item
#
if ($obj->type() == &EO_ITEM) {
#
# Note: The '%s' formats provide s string context, so the
# components of the catalog tag will be displayed as the
# symbolic values. If we changed the '%s' formats to '%d',
# the numeric value of the components would be displayed.
#
printf("%sITEM\n%s Catalog = %s|%s|%s\n",
$istr, $istr, @cat);
$indent++;
#
# Retrieve the value of the item. If the item contains in
# turn a nested exacct object (i.e. a item or group), then
# the value method will return a reference to the appropriate
# sort of perl object (Exacct::Object::Item or
# Exacct::Object::Group). We could of course figure out that
# the item contained a nested item or group by examining
# the catalog tag in @cat and looking for a type of
# EXT_EXACCT_OBJECT or EXT_GROUP.
my $val = $obj->value();
if (ref($val)) {
# If it is a nested object, recurse to dump it.
dump_object($val, $indent);
} else {
# Otherwise it is just a 'plain' value, so display it.
printf("%s Value = %s\n", $istr, $val);
}
#
# Otherwise we know we are dealing with a group. Groups represent
# contents as a perl list or array (depending on context), so we
# can process the contents of the group with a 'foreach' loop, which
# provides a list context. In a list context the value method
# returns the content of the group as a perl list, which is the
# quickest mechanism, but doesn't allow the group to be modified.
# If we wanted to modify the contents of the group we could do so
# like this:
# my $grp = $obj->value(); # Returns an array reference
# $grp->[0] = $newitem;
# but accessing the group elements this way is much slower.
#
} else {
printf("%sGROUP\n%s Catalog = %s|%s|%s\n",
$istr, $istr, @cat);
$indent++;
# 'foreach' provides a list context.
foreach my $val ($obj->value()) {
dump_object($val, $indent);
}
printf("%sENDGROUP\n", $istr);
}
}
Example 4–3 Creating a New Group Record and Writing to a File# Prototype list of catalog tags and values.
my @items = (
[ &EXT_STRING | &EXC_DEFAULT | &EXD_CREATOR => "me" ],
[ &EXT_UINT32 | &EXC_DEFAULT | &EXD_PROC_PID => $$ ],
[ &EXT_UINT32 | &EXC_DEFAULT | &EXD_PROC_UID => $< ],
[ &EXT_UINT32 | &EXC_DEFAULT | &EXD_PROC_GID => $( ],
[ &EXT_STRING | &EXC_DEFAULT | &EXD_PROC_COMMAND => "/bin/stuff" ],
);
# Create a new group catalog object.
my $cat = new_catalog(&EXT_GROUP | &EXC_DEFAULT | &EXD_NONE);
# Create a new Group object and retrieve its data array.
my $group = new_group($cat);
my $ary = $group->value();
# Push the new Items onto the Group array.
foreach my $v (@items) {
push(@$ary, new_item(new_catalog($v->[0]), $v->[1]));
}
# Nest the group within itself (performs a deep copy).
push(@$ary, $group);
# Dump out the group.
dump_object($group);
Example 4–4 Dumping an exacct File#!/usr/bin/perl
use strict;
use warnings;
use blib;
use Sun::Solaris::Exacct qw(:EXACCT_ALL);
die("Usage is dumpexacct
# Open the exact file and display the header information.
my $ef = ea_new_file($ARGV[0], &O_RDONLY) || die(error_str());
printf("Creator: %s\n", $ef->creator());
printf("Hostname: %s\n\n", $ef->hostname());
# Dump the file contents
while (my $obj = $ef->get()) {
ea_dump_object($obj);
}
# Report any errors
if (ea_error() != EXR_OK && ea_error() != EXR_EOF) {
printf("\nERROR: %s\n", ea_error_str());
exit(1);
}
exit(0);
Output From dump MethodThis example shows the formatted output of the Sun::Solaris::Exacct::Object->dump() method. GROUP
Catalog = EXT_GROUP|EXC_DEFAULT|EXD_GROUP_PROC_PARTIAL
ITEM
Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_PID
Value = 3
ITEM
Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_UID
Value = 0
ITEM
Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_GID
Value = 0
ITEM
Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_PROJID
Value = 0
ITEM
Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_TASKID
Value = 0
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_CPU_USER_SEC
Value = 0
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_CPU_USER_NSEC
Value = 0
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_CPU_SYS_SEC
Value = 890
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_CPU_SYS_NSEC
Value = 760000000
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_START_SEC
Value = 1011869897
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_START_NSEC
Value = 380771911
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_FINISH_SEC
Value = 0
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_FINISH_NSEC
Value = 0
ITEM
Catalog = EXT_STRING|EXC_DEFAULT|EXD_PROC_COMMAND
Value = fsflush
ITEM
Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_TTY_MAJOR
Value = 4294967295
ITEM
Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_TTY_MINOR
Value = 4294967295
ITEM
Catalog = EXT_STRING|EXC_DEFAULT|EXD_PROC_HOSTNAME
Value = mower
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_FAULTS_MAJOR
Value = 0
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_FAULTS_MINOR
Value = 0
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_MESSAGES_SND
Value = 0
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_MESSAGES_RCV
Value = 0
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_BLOCKS_IN
Value = 19
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_BLOCKS_OUT
Value = 40833
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_CHARS_RDWR
Value = 0
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_CONTEXT_VOL
Value = 129747
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_CONTEXT_INV
Value = 79
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_SIGNALS
Value = 0
ITEM
Catalog = EXT_UINT64|EXC_DEFAULT|EXD_PROC_SYSCALLS
Value = 0
ITEM
Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_ACCT_FLAGS
Value = 1
ITEM
Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_ANCPID
Value = 0
ITEM
Catalog = EXT_UINT32|EXC_DEFAULT|EXD_PROC_WAIT_STATUS
Value = 0
ENDGROUP
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||