]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ledger.h
5d1c6818f9634e35c72b62a7b171c66952229b9c
[apple/xnu.git] / osfmk / kern / ledger.h
1 /*
2 * Copyright (c) 2010 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31
32 #ifndef _KERN_LEDGER_H_
33 #define _KERN_LEDGER_H_
34
35 #include <mach/mach_types.h> /* ledger_t */
36
37 #define LEDGER_INFO 0
38 #define LEDGER_ENTRY_INFO 1
39 #define LEDGER_TEMPLATE_INFO 2
40 #define LEDGER_LIMIT 3
41 /* LEDGER_MAX_CMD always tracks the index of the last ledger command. */
42 #define LEDGER_MAX_CMD LEDGER_LIMIT
43
44 #define LEDGER_NAME_MAX 32
45
46 struct ledger_info {
47 char li_name[LEDGER_NAME_MAX];
48 int64_t li_id;
49 int64_t li_entries;
50 };
51
52 struct ledger_template_info {
53 char lti_name[LEDGER_NAME_MAX];
54 char lti_group[LEDGER_NAME_MAX];
55 char lti_units[LEDGER_NAME_MAX];
56 };
57
58 struct ledger_entry_info {
59 int64_t lei_balance;
60 int64_t lei_credit;
61 int64_t lei_debit;
62 uint64_t lei_limit;
63 uint64_t lei_refill_period; /* In nanoseconds */
64 uint64_t lei_last_refill; /* Time since last refill */
65 };
66
67 struct ledger_limit_args {
68 char lla_name[LEDGER_NAME_MAX];
69 uint64_t lla_limit;
70 uint64_t lla_refill_period;
71 };
72
73 #ifdef KERNEL_PRIVATE
74
75 typedef struct ledger_template *ledger_template_t;
76
77 #define LEDGER_VALID(ledger) (ledger != LEDGER_NULL)
78
79 /* Action to take when a ledger goes into deficit */
80 #define LEDGER_ACTION_IGNORE 0x0000
81 #define LEDGER_ACTION_BLOCK 0x0010
82 #define LEDGER_ACTION_CALLBACK 0x0020
83 #define LEDGER_ACTION_MASK 0x00f0
84
85 /*
86 * Types of warnings that trigger a callback.
87 */
88 #define LEDGER_WARNING_ROSE_ABOVE 1
89 #define LEDGER_WARNING_DIPPED_BELOW 2
90
91 typedef void (*ledger_callback_t)(int warning, const void * param0, const void *param1);
92
93 extern void ledger_init(void);
94
95 extern ledger_template_t ledger_template_create(const char *name);
96 extern void ledger_template_dereference(ledger_template_t template);
97 extern int ledger_entry_add(ledger_template_t template, const char *key,
98 const char *group, const char *units);
99 extern kern_return_t ledger_set_callback(ledger_template_t template, int entry,
100 ledger_callback_t callback, const void *param0, const void *param1);
101 extern kern_return_t ledger_track_maximum(ledger_template_t template, int entry,
102 int period_in_secs);
103 extern kern_return_t ledger_panic_on_negative(ledger_template_t template,
104 int entry);
105 extern kern_return_t ledger_track_credit_only(ledger_template_t template,
106 int entry);
107 extern int ledger_key_lookup(ledger_template_t template, const char *key);
108
109 /* value of entry type */
110 #define LEDGER_CREATE_ACTIVE_ENTRIES 0
111 #define LEDGER_CREATE_INACTIVE_ENTRIES 1
112 extern ledger_t ledger_instantiate(ledger_template_t template, int entry_type);
113 extern kern_return_t ledger_disable_callback(ledger_t ledger, int entry);
114 extern kern_return_t ledger_enable_callback(ledger_t ledger, int entry);
115 extern kern_return_t ledger_get_limit(ledger_t ledger, int entry,
116 ledger_amount_t *limit);
117 extern kern_return_t ledger_set_limit(ledger_t ledger, int entry,
118 ledger_amount_t limit, uint8_t warn_level_percentage);
119 extern kern_return_t ledger_get_maximum(ledger_t ledger, int entry,
120 ledger_amount_t *max_observed_balance);
121 extern kern_return_t ledger_get_actions(ledger_t ledger, int entry, int *actions);
122 extern kern_return_t ledger_set_action(ledger_t ledger, int entry, int action);
123 extern kern_return_t ledger_get_period(ledger_t ledger, int entry,
124 uint64_t *period);
125 extern kern_return_t ledger_set_period(ledger_t ledger, int entry,
126 uint64_t period);
127 extern kern_return_t ledger_disable_refill(ledger_t l, int entry);
128 extern kern_return_t ledger_entry_setactive(ledger_t ledger, int entry);
129 extern void ledger_check_new_balance(ledger_t ledger, int entry);
130 extern kern_return_t ledger_credit(ledger_t ledger, int entry,
131 ledger_amount_t amount);
132 extern kern_return_t ledger_debit(ledger_t ledger, int entry,
133 ledger_amount_t amount);
134 extern kern_return_t ledger_zero_balance(ledger_t ledger, int entry);
135 extern kern_return_t ledger_get_entries(ledger_t ledger, int entry,
136 ledger_amount_t *credit, ledger_amount_t *debit);
137 extern kern_return_t ledger_get_balance(ledger_t ledger, int entry,
138 ledger_amount_t *balance);
139 extern kern_return_t ledger_reset_callback_state(ledger_t ledger, int entry);
140 extern kern_return_t ledger_disable_panic_on_negative(ledger_t ledger, int entry);
141
142 extern kern_return_t ledger_rollup(ledger_t to_ledger, ledger_t from_ledger);
143 extern kern_return_t ledger_rollup_entry(ledger_t to_ledger, ledger_t from_ledger, int entry);
144
145 extern void ledger_ast(thread_t thread);
146
147 extern int ledger_reference_count(ledger_t ledger);
148 extern kern_return_t ledger_reference(ledger_t ledger);
149 extern kern_return_t ledger_dereference(ledger_t ledger);
150
151 /* Per-pmap ledger operations */
152 #define pmap_ledger_debit(p, e, a) ledger_debit((p)->ledger, e, a)
153 #define pmap_ledger_credit(p, e, a) ledger_credit((p)->ledger, e, a)
154
155 /* Support for ledger() syscall */
156 #ifdef LEDGER_DEBUG
157 extern int ledger_limit(task_t task, struct ledger_limit_args *args);
158 #endif
159 extern int ledger_info(task_t task, struct ledger_info *info);
160
161 extern int
162 ledger_get_task_entry_info_multiple(task_t task, void **buf, int *len);
163
164 extern void
165 ledger_get_entry_info(ledger_t ledger, int entry,
166 struct ledger_entry_info *lei);
167
168 extern int ledger_template_info(void **buf, int *len);
169
170 #endif /* KERNEL_PRIVATE */
171
172 #endif /* _KERN_LEDGER_H_ */