以 PDF 格式下載這本書 (875 KB)
Chapter 3 Using the C Interface to Extended AccountingThis chapter describes the C interface to extended accounting and covers the following topics: Overview of the C Interface to Extended AccountingProjects and tasks are used to label and separate workloads. The extended accounting subsystem is used to monitor resource consumption by workloads that are running on the system. Extended accounting produces accounting records for the workload tasks and processes. For an overview of extended accounting and example procedures for administering extended accounting, see Chapter 4, Extended Accounting (Overview), in System Administration Guide: Solaris Containers-Resource Management and Solaris Zones and Chapter 5, Administering Extended Accounting (Tasks), in System Administration Guide: Solaris Containers-Resource Management and Solaris Zones. The extended accounting framework has been expanded for zones. Each zone has its own extended accounting files for task and process-based accounting. The extended accounting files in the global zone contain accounting records for the global zone and for all non-global zones. The accounting records contain a zone name tag. The global zone administrator can use the tag during the extraction of per zone accounting data from the accounting files in the global zone. Extended Accounting API FunctionsThe extended accounting API contains functions that perform the following:
exacct System Calls
The following table lists the system calls that interact with the extended accounting subsystem. Table 3–1 Extended Accounting System Calls
Operations on the exacct FileThese functions provide access to the exacct files: Table 3–2 exacct File Functions
Operations on exacct ObjectsThese functions are used to access exacct objects: Table 3–3 exacct Object Functions
Memory ManagementThe following table lists the functions associated with extended accounting memory management. The function name is a link to its man page. Table 3–4 Extended Accounting Memory Management Functions
Miscellaneous OperationsThese functions are associated with miscellaneous operations:
C Code Examples for Accessing exacct FilesThis section provides code examples for accessing exacct files. Example 3–1 Displaying exacct Data for a Designated pidThis example displays a specific pid's exacct data snapshot from the kernel. ...
ea_object_t *scratch;
int unpk_flag = EUP_ALLOC; /* use the same allocation flag */
/* for unpack and free */
/* Omit return value checking, to keep code samples short */
bsize = getacct(P_PID, pid, NULL, 0);
buf = malloc(bsize);
/* Retrieve exacct object and unpack */
getacct(P_PID, pid, buf, bsize);
ea_unpack_object(&scratch, unpk_flag, buf, bsize);
/* Display the exacct record */
disp_obj(scratch);
if (scratch->eo_type == EO_GROUP) {
disp_group(scratch);
}
ea_free_object(scratch, unpk_flag);
...
Example 3–2 Identifying Individual Tasks During a Kernel BuildThis example evaluates kernel builds and displays a string that describes the portion of the source tree being built by this task make. Display the portion of the source being built to aid in the per-source-directory analysis. The key points for this example include the following:
... /* Omit return value checking and error processing */ /* to keep code sample short */ ptid = gettaskid(); /* Save "parent" task-id */ tid = settaskid(getprojid(), TASK_NORMAL); /* Create new task */ /* Set data for item objects ptskid and cwd */ ea_set_item(&ptskid, EXT_UINT32 | EXC_LOCAL | MY_PTID, &ptid, 0); ea_set_item(&cwd, EXT_STRING | EXC_LOCAL | MY_CWD, cwdbuf, strlen(cwdbuf)); /* Set grp object and attach ptskid and cwd to grp */ ea_set_group(&grp, EXT_GROUP | EXC_LOCAL | EXD_GROUP_HEADER); ea_attach_to_group(&grp, &ptskid); ea_attach_to_group(&grp, &cwd); /* Pack the object and put it back into the accounting stream */ ea_buflen = ea_pack_object(&grp, ea_buf, sizeof(ea_buf)); putacct(P_TASKID, tid, ea_buf, ea_buflen, EP_EXACCT_OBJECT); /* Memory management: free memory allocate in ea_set_item */ ea_free_item(&cwd, EUP_ALLOC); ... Example 3–3 Reading and Displaying the Contents of a System exacct FileThis example shows how to read and display a system exacct file for a process or a task. The key points for this example include the following:
/* Omit return value checking and error processing */
/* to keep code sample short */
main(int argc, char *argv)
{
ea_file_t ef;
ea_object_t scratch;
char *fname;
fname = argv[1];
ea_open(&ef, fname, NULL, EO_NO_VALID_HDR, O_RDONLY, 0);
bzero(&scratch, sizeof (ea_object_t));
while (ea_get_object(&ef, &scratch) != -1) {
disp_obj(&scratch);
if (scratch.eo_type == EO_GROUP)
disp_group(&ef, scratch.eo_group.eg_nobjs);
bzero(&scratch, sizeof (ea_object_t));
}
ea_close(&ef);
}
struct catalog_item { /* convert Solaris catalog's type ID */
/* to a meaningful string */
int type;
char *name;
} catalog[] = {
{ EXD_VERSION, "version\t" },
...
{ EXD_PROC_PID, " pid\t" },
...
};
static char *
catalog_name(int type)
{
int i = 0;
while (catalog[i].type != EXD_NONE) {
if (catalog[i].type == type)
return (catalog[i].name);
else
i++;
}
return ("unknown\t");
}
static void disp_obj(ea_object_t *o)
{
printf("%s\t", catalog_name(o->eo_catalog & 0xffffff));
switch(o->eo_catalog & EXT_TYPE_MASK) {
case EXT_UINT8:
printf(" 8: %u", o->eo_item.ei_uint8);
break;
case EXT_UINT16:
...
}
static void disp_group(ea_file_t *ef, uint_t nobjs)
{
for (i = 0; i < nobjs; i++) {
ea_get_object(ef, &scratch));
disp_obj(&scratch);
if (scratch.eo_type == EO_GROUP)
disp_group(ef, scratch.eo_group.eg_nobjs);
}
}
Programming Issues With exacct Files
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||