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