Home | History | Annotate | Download | only in sys
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #ifndef	_SYS_EXACCT_H
     27 #define	_SYS_EXACCT_H
     28 
     29 #include <sys/types.h>
     30 #include <sys/task.h>
     31 #include <sys/proc.h>
     32 #include <sys/procset.h>
     33 
     34 #ifdef _KERNEL
     35 #include <sys/acctctl.h>
     36 #include <sys/kmem.h>
     37 #include <sys/taskq.h>
     38 #include <sys/vnode.h>
     39 #endif
     40 
     41 #ifdef	__cplusplus
     42 extern "C" {
     43 #endif
     44 
     45 #define	EXACCT_VERSION	1
     46 
     47 /*
     48  * unpack and free allocation options:  the behaviour of the ea_free_object()
     49  * function is coordinated with whether an unpack used EUP_ALLOC or EUP_NOALLOC,
     50  * such that unpacked object hierarchies can be later freed successfully.
     51  */
     52 #define	EUP_ALLOC	0x0	/* allocate new memory for vbl length objects */
     53 #define	EUP_NOALLOC	0x1	/* use existing buffer for vbl length objects */
     54 #define	EUP_ALLOC_MASK	0x1
     55 
     56 /*
     57  * wracct and putacct record type options:  the properties of the partial and
     58  * interval records differ slightly:  a partial record is a snapshot of the
     59  * resource usage for the given process or task, while an interval record
     60  * reports the current usage since the last interval record or creation.
     61  * Interval records are supported only for tasks.
     62  */
     63 #define	EW_PARTIAL		(0x01)	/* partial record */
     64 #define	EW_INTERVAL		(0x02)	/* interval record */
     65 #define	EW_FINAL		(0x04)	/* final record: used only in kernel */
     66 
     67 /*
     68  * putacct contents option:  the contents of the buffer passed to putacct may be
     69  * identified either as raw data or as a packed exacct record.
     70  */
     71 #define	EP_RAW			0
     72 #define	EP_EXACCT_OBJECT	1
     73 
     74 #define	EXACCT_MAX_BUFSIZE	(64 * 1024)
     75 
     76 #ifndef _KERNEL
     77 extern size_t getacct(idtype_t, id_t, void *, size_t);
     78 extern int putacct(idtype_t, id_t, void *, size_t, int);
     79 extern int wracct(idtype_t, id_t, int);
     80 #endif /* ! _KERNEL */
     81 
     82 /*
     83  * Error codes.  libexacct reports these errors through the ea_error() function;
     84  * in the case of EXR_SYSCALL_FAIL, errno will contain the error code
     85  * encountered by the underlying system call.
     86  */
     87 #define	EXR_OK			0
     88 #define	EXR_SYSCALL_FAIL	1
     89 #define	EXR_CORRUPT_FILE	2
     90 #define	EXR_EOF			3
     91 #define	EXR_NO_CREATOR		4
     92 #define	EXR_INVALID_BUF		5
     93 #define	EXR_NOTSUPP		6
     94 #define	EXR_UNKN_VERSION	7
     95 #define	EXR_INVALID_OBJ		8
     96 
     97 typedef uint64_t ea_size_t;
     98 typedef uint32_t ea_catalog_t;
     99 
    100 typedef enum {EO_ERROR = -1, EO_NONE = 0, EO_GROUP, EO_ITEM} ea_object_type_t;
    101 
    102 typedef struct ea_item {
    103 	/*
    104 	 * The ei_u union is discriminated via the type field of the enclosing
    105 	 * object's catalog tag.
    106 	 */
    107 	union {
    108 		uint8_t		ei_u_uint8;
    109 		uint16_t	ei_u_uint16;
    110 		uint32_t	ei_u_uint32;
    111 		uint64_t	ei_u_uint64;
    112 		double		ei_u_double;
    113 		char		*ei_u_string;
    114 		void		*ei_u_object;	/* for embedded packed object */
    115 		void		*ei_u_raw;
    116 	}			ei_u;
    117 	ea_size_t		ei_size;
    118 } ea_item_t;
    119 #define	ei_uint8	ei_u.ei_u_uint8
    120 #define	ei_uint16	ei_u.ei_u_uint16
    121 #define	ei_uint32	ei_u.ei_u_uint32
    122 #define	ei_uint64	ei_u.ei_u_uint64
    123 #define	ei_double	ei_u.ei_u_double
    124 #define	ei_string	ei_u.ei_u_string
    125 #define	ei_object	ei_u.ei_u_object
    126 #define	ei_raw		ei_u.ei_u_raw
    127 
    128 typedef struct ea_group {
    129 	uint32_t		eg_nobjs;
    130 	struct ea_object	*eg_objs;
    131 } ea_group_t;
    132 
    133 typedef struct ea_object {
    134 	ea_object_type_t	eo_type;
    135 	union {
    136 		ea_group_t	eo_u_group;
    137 		ea_item_t	eo_u_item;
    138 	}			eo_u;
    139 	struct ea_object	*eo_next;
    140 	ea_catalog_t		eo_catalog;
    141 } ea_object_t;
    142 #define	eo_group	eo_u.eo_u_group
    143 #define	eo_item		eo_u.eo_u_item
    144 
    145 extern int ea_set_item(ea_object_t *, ea_catalog_t, const void *, size_t);
    146 extern int ea_set_group(ea_object_t *, ea_catalog_t);
    147 
    148 /*
    149  * In prior releases, the following three functions had the type void, and so
    150  * could not return a status code.  In SunOS 5.9, the return type has been
    151  * changed to int, so that if errors are detected the invoking application
    152  * can be notified appropriately.
    153  */
    154 extern int ea_attach_to_object(ea_object_t *, ea_object_t *);
    155 extern int ea_attach_to_group(ea_object_t *, ea_object_t *);
    156 extern int ea_free_item(ea_object_t *, int);
    157 
    158 extern void ea_free_object(ea_object_t *, int);
    159 extern size_t ea_pack_object(ea_object_t *, void *, size_t);
    160 extern void *ea_alloc(size_t);
    161 extern void ea_free(void *, size_t);
    162 extern char *ea_strdup(const char *);
    163 extern void ea_strfree(char *);
    164 
    165 #ifdef _KERNEL
    166 extern ea_object_t *ea_alloc_item(ea_catalog_t, void *, size_t);
    167 extern ea_object_t *ea_alloc_group(ea_catalog_t);
    168 extern ea_object_t *ea_attach_item(ea_object_t *, void *, size_t, ea_catalog_t);
    169 extern void exacct_commit_task(void *);
    170 extern void exacct_commit_proc(proc_t *, int);
    171 extern void exacct_update_task_mstate(proc_t *);
    172 extern int exacct_tag_task(ac_info_t *, task_t *, void *, size_t, int);
    173 extern int exacct_tag_proc(ac_info_t *, pid_t, taskid_t, void *, size_t, int,
    174     const char *);
    175 extern void exacct_commit_flow(void *);
    176 extern int exacct_commit_netinfo(void *, int);
    177 extern void exacct_init(void);
    178 extern void *exacct_create_header(size_t *);
    179 extern int exacct_write_header(ac_info_t *, void *, size_t);
    180 extern void exacct_calculate_proc_usage(proc_t *, proc_usage_t *,
    181     ulong_t *, int, int);
    182 extern int exacct_commit_callback(ac_info_t *, void *, size_t, void *,
    183     size_t, size_t *);
    184 extern int exacct_assemble_proc_usage(ac_info_t *, proc_usage_t *,
    185     int (*)(ac_info_t *, void *, size_t, void *, size_t, size_t *),
    186     void *, size_t, size_t *, int);
    187 extern int exacct_assemble_task_usage(ac_info_t *, task_t *,
    188     int (*)(ac_info_t *, void *, size_t, void *, size_t, size_t *),
    189     void *, size_t, size_t *, int);
    190 extern int exacct_assemble_flow_usage(ac_info_t *, flow_usage_t *,
    191     int (*)(ac_info_t *, void *, size_t, void *, size_t, size_t *),
    192     void *, size_t, size_t *);
    193 extern void exacct_move_mstate(proc_t *, task_t *, task_t *);
    194 extern int exacct_assemble_net_usage(ac_info_t *, void *,
    195     int (*)(ac_info_t *, void *, size_t, void *, size_t, size_t *),
    196     void *, size_t, size_t *, int);
    197 extern taskq_t *exacct_queue;
    198 extern kmem_cache_t *exacct_object_cache;
    199 #endif /* _KERNEL */
    200 
    201 #ifdef	__cplusplus
    202 }
    203 #endif
    204 
    205 #endif	/* _SYS_EXACCT_H */
    206