2 * Copyright (c) 2010-2018 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 #include <kern/kern_types.h>
33 #include <kern/ledger.h>
34 #include <kern/kalloc.h>
35 #include <kern/task.h>
36 #include <kern/thread.h>
38 #include <kern/processor.h>
39 #include <kern/machine.h>
40 #include <kern/queue.h>
41 #include <kern/policy_internal.h>
43 #include <sys/errno.h>
45 #include <libkern/OSAtomic.h>
46 #include <mach/mach_types.h>
47 #include <os/overflow.h>
52 * Ledger entry flags. Bits in second nibble (masked by 0xF0) are used for
53 * ledger actions (LEDGER_ACTION_BLOCK, etc).
55 #define LF_ENTRY_ACTIVE 0x0001 /* entry is active if set */
56 #define LF_WAKE_NEEDED 0x0100 /* one or more threads are asleep */
57 #define LF_WAKE_INPROGRESS 0x0200 /* the wait queue is being processed */
58 #define LF_REFILL_SCHEDULED 0x0400 /* a refill timer has been set */
59 #define LF_REFILL_INPROGRESS 0x0800 /* the ledger is being refilled */
60 #define LF_CALLED_BACK 0x1000 /* callback was called for balance in deficit */
61 #define LF_WARNED 0x2000 /* callback was called for balance warning */
62 #define LF_TRACKING_MAX 0x4000 /* track max balance. Exclusive w.r.t refill */
63 #define LF_PANIC_ON_NEGATIVE 0x8000 /* panic if it goes negative */
64 #define LF_TRACK_CREDIT_ONLY 0x10000 /* only update "credit" */
66 /* Determine whether a ledger entry exists and has been initialized and active */
67 #define ENTRY_VALID(l, e) \
68 (((l) != NULL) && ((e) >= 0) && ((e) < (l)->l_size) && \
69 (((l)->l_entries[e].le_flags & LF_ENTRY_ACTIVE) == LF_ENTRY_ACTIVE))
71 #define ASSERT(a) assert(a)
76 #define lprintf(a) if (ledger_debug) { \
77 printf("%lld ", abstime_to_nsecs(mach_absolute_time() / 1000000)); \
84 struct ledger_callback
{
85 ledger_callback_t lc_func
;
86 const void *lc_param0
;
87 const void *lc_param1
;
90 struct entry_template
{
91 char et_key
[LEDGER_NAME_MAX
];
92 char et_group
[LEDGER_NAME_MAX
];
93 char et_units
[LEDGER_NAME_MAX
];
95 struct ledger_callback
*et_callback
;
98 lck_grp_t ledger_lck_grp
;
101 * Modifying the reference count, table size, or table contents requires
102 * holding the lt_lock. Modfying the table address requires both lt_lock
103 * and setting the inuse bit. This means that the lt_entries field can be
104 * safely dereferenced if you hold either the lock or the inuse bit. The
105 * inuse bit exists solely to allow us to swap in a new, larger entries
106 * table without requiring a full lock to be acquired on each lookup.
107 * Accordingly, the inuse bit should never be held for longer than it takes
108 * to extract a value from the table - i.e., 2 or 3 memory references.
110 struct ledger_template
{
115 volatile uint32_t lt_inuse
;
119 struct entry_template
*lt_entries
;
122 #define template_lock(template) lck_mtx_lock(&(template)->lt_lock)
123 #define template_unlock(template) lck_mtx_unlock(&(template)->lt_lock)
125 #define TEMPLATE_INUSE(s, t) { \
127 while (OSCompareAndSwap(0, 1, &((t)->lt_inuse))) \
131 #define TEMPLATE_IDLE(s, t) { \
136 static int ledger_cnt
= 0;
137 /* ledger ast helper functions */
138 static uint32_t ledger_check_needblock(ledger_t l
, uint64_t now
);
139 static kern_return_t
ledger_perform_blocking(ledger_t l
);
140 static uint32_t flag_set(volatile uint32_t *flags
, uint32_t bit
);
141 static uint32_t flag_clear(volatile uint32_t *flags
, uint32_t bit
);
143 static void ledger_entry_check_new_balance(thread_t thread
, ledger_t ledger
,
144 int entry
, struct ledger_entry
*le
);
148 debug_callback(const void *p0
, __unused
const void *p1
)
150 printf("ledger: resource exhausted [%s] for task %p\n",
151 (const char *)p0
, p1
);
155 /************************************/
158 abstime_to_nsecs(uint64_t abstime
)
162 absolutetime_to_nanoseconds(abstime
, &nsecs
);
167 nsecs_to_abstime(uint64_t nsecs
)
171 nanoseconds_to_absolutetime(nsecs
, &abstime
);
178 lck_grp_init(&ledger_lck_grp
, "ledger", LCK_GRP_ATTR_NULL
);
182 ledger_template_create(const char *name
)
184 ledger_template_t
template;
186 template = (ledger_template_t
)kalloc(sizeof (*template));
187 if (template == NULL
)
190 template->lt_name
= name
;
191 template->lt_refs
= 1;
192 template->lt_cnt
= 0;
193 template->lt_table_size
= 1;
194 template->lt_inuse
= 0;
195 template->lt_zone
= NULL
;
196 lck_mtx_init(&template->lt_lock
, &ledger_lck_grp
, LCK_ATTR_NULL
);
198 template->lt_entries
= (struct entry_template
*)
199 kalloc(sizeof (struct entry_template
) * template->lt_table_size
);
200 if (template->lt_entries
== NULL
) {
201 kfree(template, sizeof (*template));
209 ledger_template_dereference(ledger_template_t
template)
211 template_lock(template);
213 template_unlock(template);
215 if (template->lt_refs
== 0)
216 kfree(template, sizeof (*template));
220 * Add a new entry to the list of entries in a ledger template. There is
221 * currently no mechanism to remove an entry. Implementing such a mechanism
222 * would require us to maintain per-entry reference counts, which we would
223 * prefer to avoid if possible.
226 ledger_entry_add(ledger_template_t
template, const char *key
,
227 const char *group
, const char *units
)
230 struct entry_template
*et
;
232 if ((key
== NULL
) || (strlen(key
) >= LEDGER_NAME_MAX
) || (template->lt_zone
!= NULL
))
235 template_lock(template);
237 /* If the table is full, attempt to double its size */
238 if (template->lt_cnt
== template->lt_table_size
) {
239 struct entry_template
*new_entries
, *old_entries
;
240 int old_cnt
, old_sz
, new_sz
= 0;
243 old_cnt
= template->lt_table_size
;
244 old_sz
= old_cnt
* (int)(sizeof(struct entry_template
));
245 /* double old_sz allocation, but check for overflow */
246 if (os_mul_overflow(old_sz
, 2, &new_sz
)) {
247 template_unlock(template);
250 new_entries
= kalloc(new_sz
);
251 if (new_entries
== NULL
) {
252 template_unlock(template);
255 memcpy(new_entries
, template->lt_entries
, old_sz
);
256 memset(((char *)new_entries
) + old_sz
, 0, old_sz
);
257 /* assume: if the sz didn't overflow, neither will the count */
258 template->lt_table_size
= old_cnt
* 2;
260 old_entries
= template->lt_entries
;
262 TEMPLATE_INUSE(s
, template);
263 template->lt_entries
= new_entries
;
264 TEMPLATE_IDLE(s
, template);
266 kfree(old_entries
, old_sz
);
269 et
= &template->lt_entries
[template->lt_cnt
];
270 strlcpy(et
->et_key
, key
, LEDGER_NAME_MAX
);
271 strlcpy(et
->et_group
, group
, LEDGER_NAME_MAX
);
272 strlcpy(et
->et_units
, units
, LEDGER_NAME_MAX
);
273 et
->et_flags
= LF_ENTRY_ACTIVE
;
274 et
->et_callback
= NULL
;
276 idx
= template->lt_cnt
++;
277 template_unlock(template);
284 ledger_entry_setactive(ledger_t ledger
, int entry
)
286 struct ledger_entry
*le
;
288 if ((ledger
== NULL
) || (entry
< 0) || (entry
>= ledger
->l_size
))
289 return (KERN_INVALID_ARGUMENT
);
291 le
= &ledger
->l_entries
[entry
];
292 if ((le
->le_flags
& LF_ENTRY_ACTIVE
) == 0) {
293 flag_set(&le
->le_flags
, LF_ENTRY_ACTIVE
);
295 return (KERN_SUCCESS
);
300 ledger_key_lookup(ledger_template_t
template, const char *key
)
304 template_lock(template);
305 for (idx
= 0; idx
< template->lt_cnt
; idx
++)
306 if (template->lt_entries
!= NULL
&&
307 (strcmp(key
, template->lt_entries
[idx
].et_key
) == 0))
310 if (idx
>= template->lt_cnt
)
312 template_unlock(template);
318 * Complete the initialization of ledger template
319 * by initializing ledger zone. After initializing
320 * the ledger zone, adding an entry in the ledger
321 * template would fail.
324 ledger_template_complete(ledger_template_t
template)
327 ledger_size
= sizeof(struct ledger
) + (template->lt_cnt
* sizeof(struct ledger_entry
));
328 template->lt_zone
= zinit(ledger_size
, CONFIG_TASK_MAX
* ledger_size
,
331 template->lt_initialized
= true;
335 * Like ledger_template_complete, except we'll ask
336 * the pmap layer to manage allocations for us.
337 * Meant for ledgers that should be owned by the
341 ledger_template_complete_secure_alloc(ledger_template_t
template)
344 ledger_size
= sizeof(struct ledger
) + (template->lt_cnt
* sizeof(struct ledger_entry
));
345 pmap_ledger_alloc_init(ledger_size
);
346 template->lt_initialized
= true;
350 * Create a new ledger based on the specified template. As part of the
351 * ledger creation we need to allocate space for a table of ledger entries.
352 * The size of the table is based on the size of the template at the time
353 * the ledger is created. If additional entries are added to the template
354 * after the ledger is created, they will not be tracked in this ledger.
357 ledger_instantiate(ledger_template_t
template, int entry_type
)
363 template_lock(template);
365 cnt
= template->lt_cnt
;
366 template_unlock(template);
368 if (template->lt_zone
) {
369 ledger
= (ledger_t
)zalloc(template->lt_zone
);
371 ledger
= pmap_ledger_alloc();
374 if (ledger
== NULL
) {
375 ledger_template_dereference(template);
379 ledger
->l_template
= template;
380 ledger
->l_id
= ledger_cnt
++;
381 os_ref_init(&ledger
->l_refs
, NULL
);
382 ledger
->l_size
= (int32_t)cnt
;
384 template_lock(template);
385 assert(ledger
->l_size
<= template->lt_cnt
);
386 for (i
= 0; i
< ledger
->l_size
; i
++) {
387 struct ledger_entry
*le
= &ledger
->l_entries
[i
];
388 struct entry_template
*et
= &template->lt_entries
[i
];
390 le
->le_flags
= et
->et_flags
;
391 /* make entry inactive by removing active bit */
392 if (entry_type
== LEDGER_CREATE_INACTIVE_ENTRIES
)
393 flag_clear(&le
->le_flags
, LF_ENTRY_ACTIVE
);
395 * If template has a callback, this entry is opted-in,
398 if (et
->et_callback
!= NULL
)
399 flag_set(&le
->le_flags
, LEDGER_ACTION_CALLBACK
);
402 le
->le_limit
= LEDGER_LIMIT_INFINITY
;
403 le
->le_warn_level
= LEDGER_LIMIT_INFINITY
;
404 le
->_le
.le_refill
.le_refill_period
= 0;
405 le
->_le
.le_refill
.le_last_refill
= 0;
407 template_unlock(template);
413 flag_set(volatile uint32_t *flags
, uint32_t bit
)
415 return (OSBitOrAtomic(bit
, flags
));
419 flag_clear(volatile uint32_t *flags
, uint32_t bit
)
421 return (OSBitAndAtomic(~bit
, flags
));
425 * Take a reference on a ledger
428 ledger_reference(ledger_t ledger
)
430 if (!LEDGER_VALID(ledger
))
431 return (KERN_INVALID_ARGUMENT
);
432 os_ref_retain(&ledger
->l_refs
);
433 return (KERN_SUCCESS
);
437 ledger_reference_count(ledger_t ledger
)
439 if (!LEDGER_VALID(ledger
))
442 return os_ref_get_count(&ledger
->l_refs
);
446 * Remove a reference on a ledger. If this is the last reference,
447 * deallocate the unused ledger.
450 ledger_dereference(ledger_t ledger
)
452 if (!LEDGER_VALID(ledger
))
453 return (KERN_INVALID_ARGUMENT
);
455 if (os_ref_release(&ledger
->l_refs
) == 0) {
456 if (ledger
->l_template
->lt_zone
) {
457 zfree(ledger
->l_template
->lt_zone
, ledger
);
459 pmap_ledger_free(ledger
);
463 return (KERN_SUCCESS
);
467 * Determine whether an entry has exceeded its warning level.
470 warn_level_exceeded(struct ledger_entry
*le
)
472 ledger_amount_t balance
;
474 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
475 assert(le
->le_debit
== 0);
477 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
481 * XXX - Currently, we only support warnings for ledgers which
482 * use positive limits.
484 balance
= le
->le_credit
- le
->le_debit
;
485 if ((le
->le_warn_level
!= LEDGER_LIMIT_INFINITY
) && (balance
> le
->le_warn_level
))
491 * Determine whether an entry has exceeded its limit.
494 limit_exceeded(struct ledger_entry
*le
)
496 ledger_amount_t balance
;
498 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
499 assert(le
->le_debit
== 0);
501 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
504 balance
= le
->le_credit
- le
->le_debit
;
505 if ((le
->le_limit
<= 0) && (balance
< le
->le_limit
))
508 if ((le
->le_limit
> 0) && (balance
> le
->le_limit
))
513 static inline struct ledger_callback
*
514 entry_get_callback(ledger_t ledger
, int entry
)
516 struct ledger_callback
*callback
;
519 TEMPLATE_INUSE(s
, ledger
->l_template
);
520 callback
= ledger
->l_template
->lt_entries
[entry
].et_callback
;
521 TEMPLATE_IDLE(s
, ledger
->l_template
);
527 * If the ledger value is positive, wake up anybody waiting on it.
530 ledger_limit_entry_wakeup(struct ledger_entry
*le
)
534 if (!limit_exceeded(le
)) {
535 flags
= flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
537 while (le
->le_flags
& LF_WAKE_NEEDED
) {
538 flag_clear(&le
->le_flags
, LF_WAKE_NEEDED
);
539 thread_wakeup((event_t
)le
);
545 * Refill the coffers.
548 ledger_refill(uint64_t now
, ledger_t ledger
, int entry
)
550 uint64_t elapsed
, period
, periods
;
551 struct ledger_entry
*le
;
552 ledger_amount_t balance
, due
;
554 assert(entry
>= 0 && entry
< ledger
->l_size
);
556 le
= &ledger
->l_entries
[entry
];
558 assert(le
->le_limit
!= LEDGER_LIMIT_INFINITY
);
560 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
561 assert(le
->le_debit
== 0);
566 * If another thread is handling the refill already, we're not
569 if (flag_set(&le
->le_flags
, LF_REFILL_INPROGRESS
) & LF_REFILL_INPROGRESS
) {
574 * If the timestamp we're about to use to refill is older than the
575 * last refill, then someone else has already refilled this ledger
576 * and there's nothing for us to do here.
578 if (now
<= le
->_le
.le_refill
.le_last_refill
) {
579 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
584 * See how many refill periods have passed since we last
587 period
= le
->_le
.le_refill
.le_refill_period
;
588 elapsed
= now
- le
->_le
.le_refill
.le_last_refill
;
589 if ((period
== 0) || (elapsed
< period
)) {
590 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
595 * Optimize for the most common case of only one or two
599 while ((periods
< 2) && (elapsed
> 0)) {
605 * OK, it's been a long time. Do a divide to figure out
609 periods
= (now
- le
->_le
.le_refill
.le_last_refill
) / period
;
611 balance
= le
->le_credit
- le
->le_debit
;
612 due
= periods
* le
->le_limit
;
614 if (balance
- due
< 0)
617 assertf(due
>= 0,"now=%llu, ledger=%p, entry=%d, balance=%lld, due=%lld", now
, ledger
, entry
, balance
, due
);
619 OSAddAtomic64(due
, &le
->le_debit
);
621 assert(le
->le_debit
>= 0);
624 * If we've completely refilled the pool, set the refill time to now.
625 * Otherwise set it to the time at which it last should have been
629 le
->_le
.le_refill
.le_last_refill
= now
;
631 le
->_le
.le_refill
.le_last_refill
+= (le
->_le
.le_refill
.le_refill_period
* periods
);
633 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
635 lprintf(("Refill %lld %lld->%lld\n", periods
, balance
, balance
- due
));
636 if (!limit_exceeded(le
))
637 ledger_limit_entry_wakeup(le
);
641 ledger_entry_check_new_balance(thread_t thread
, ledger_t ledger
,
642 int entry
, struct ledger_entry
*le
)
644 if (le
->le_flags
& LF_TRACKING_MAX
) {
645 ledger_amount_t balance
= le
->le_credit
- le
->le_debit
;
647 if (balance
> le
->_le
._le_max
.le_lifetime_max
){
648 le
->_le
._le_max
.le_lifetime_max
= balance
;
651 #if CONFIG_LEDGER_INTERVAL_MAX
652 if (balance
> le
->_le
._le_max
.le_interval_max
) {
653 le
->_le
._le_max
.le_interval_max
= balance
;
655 #endif /* LEDGER_CONFIG_INTERVAL_MAX */
658 /* Check to see whether we're due a refill */
659 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
660 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
662 uint64_t now
= mach_absolute_time();
663 if ((now
- le
->_le
.le_refill
.le_last_refill
) > le
->_le
.le_refill
.le_refill_period
)
664 ledger_refill(now
, ledger
, entry
);
667 if (limit_exceeded(le
)) {
669 * We've exceeded the limit for this entry. There
670 * are several possible ways to handle it. We can block,
671 * we can execute a callback, or we can ignore it. In
672 * either of the first two cases, we want to set the AST
673 * flag so we can take the appropriate action just before
674 * leaving the kernel. The one caveat is that if we have
675 * already called the callback, we don't want to do it
676 * again until it gets rearmed.
678 if ((le
->le_flags
& LEDGER_ACTION_BLOCK
) ||
679 (!(le
->le_flags
& LF_CALLED_BACK
) &&
680 entry_get_callback(ledger
, entry
))) {
681 act_set_astledger_async(thread
);
685 * The balance on the account is below the limit.
687 * If there are any threads blocked on this entry, now would
688 * be a good time to wake them up.
690 if (le
->le_flags
& LF_WAKE_NEEDED
)
691 ledger_limit_entry_wakeup(le
);
693 if (le
->le_flags
& LEDGER_ACTION_CALLBACK
) {
695 * Client has requested that a callback be invoked whenever
696 * the ledger's balance crosses into or out of the warning
699 if (warn_level_exceeded(le
)) {
701 * This ledger's balance is above the warning level.
703 if ((le
->le_flags
& LF_WARNED
) == 0) {
705 * If we are above the warning level and
706 * have not yet invoked the callback,
707 * set the AST so it can be done before returning
710 act_set_astledger_async(thread
);
714 * This ledger's balance is below the warning level.
716 if (le
->le_flags
& LF_WARNED
) {
718 * If we are below the warning level and
719 * the LF_WARNED flag is still set, we need
720 * to invoke the callback to let the client
721 * know the ledger balance is now back below
724 act_set_astledger_async(thread
);
730 if ((le
->le_flags
& LF_PANIC_ON_NEGATIVE
) &&
731 (le
->le_credit
< le
->le_debit
)) {
732 panic("ledger_entry_check_new_balance(%p,%d): negative ledger %p credit:%lld debit:%lld balance:%lld\n",
736 le
->le_credit
- le
->le_debit
);
741 ledger_check_new_balance(thread_t thread
, ledger_t ledger
, int entry
)
743 struct ledger_entry
*le
;
744 assert(entry
> 0 && entry
<= ledger
->l_size
);
745 le
= &ledger
->l_entries
[entry
];
746 ledger_entry_check_new_balance(thread
, ledger
, entry
, le
);
750 * Add value to an entry in a ledger for a specific thread.
753 ledger_credit_thread(thread_t thread
, ledger_t ledger
, int entry
, ledger_amount_t amount
)
755 ledger_amount_t old
, new;
756 struct ledger_entry
*le
;
758 if (!ENTRY_VALID(ledger
, entry
) || (amount
< 0))
759 return (KERN_INVALID_VALUE
);
762 return (KERN_SUCCESS
);
764 le
= &ledger
->l_entries
[entry
];
766 old
= OSAddAtomic64(amount
, &le
->le_credit
);
768 lprintf(("%p Credit %lld->%lld\n", thread
, old
, new));
771 ledger_entry_check_new_balance(thread
, ledger
, entry
, le
);
774 return (KERN_SUCCESS
);
778 * Add value to an entry in a ledger.
781 ledger_credit(ledger_t ledger
, int entry
, ledger_amount_t amount
)
783 return ledger_credit_thread(current_thread(), ledger
, entry
, amount
);
787 * Add value to an entry in a ledger; do not check balance after update.
790 ledger_credit_nocheck(ledger_t ledger
, int entry
, ledger_amount_t amount
)
792 return ledger_credit_thread(NULL
, ledger
, entry
, amount
);
795 /* Add all of one ledger's values into another.
796 * They must have been created from the same template.
797 * This is not done atomically. Another thread (if not otherwise synchronized)
798 * may see bogus values when comparing one entry to another.
799 * As each entry's credit & debit are modified one at a time, the warning/limit
800 * may spuriously trip, or spuriously fail to trip, or another thread (if not
801 * otherwise synchronized) may see a bogus balance.
804 ledger_rollup(ledger_t to_ledger
, ledger_t from_ledger
)
808 assert(to_ledger
->l_template
== from_ledger
->l_template
);
810 for (i
= 0; i
< to_ledger
->l_size
; i
++) {
811 ledger_rollup_entry(to_ledger
, from_ledger
, i
);
814 return (KERN_SUCCESS
);
817 /* Add one ledger entry value to another.
818 * They must have been created from the same template.
819 * Since the credit and debit values are added one
820 * at a time, other thread might read the a bogus value.
823 ledger_rollup_entry(ledger_t to_ledger
, ledger_t from_ledger
, int entry
)
825 struct ledger_entry
*from_le
, *to_le
;
827 assert(to_ledger
->l_template
== from_ledger
->l_template
);
828 if (ENTRY_VALID(from_ledger
, entry
) && ENTRY_VALID(to_ledger
, entry
)) {
829 from_le
= &from_ledger
->l_entries
[entry
];
830 to_le
= &to_ledger
->l_entries
[entry
];
831 OSAddAtomic64(from_le
->le_credit
, &to_le
->le_credit
);
832 OSAddAtomic64(from_le
->le_debit
, &to_le
->le_debit
);
835 return (KERN_SUCCESS
);
839 * Zero the balance of a ledger by adding to its credit or debit, whichever is smaller.
840 * Note that some clients of ledgers (notably, task wakeup statistics) require that
841 * le_credit only ever increase as a function of ledger_credit().
844 ledger_zero_balance(ledger_t ledger
, int entry
)
846 struct ledger_entry
*le
;
847 ledger_amount_t debit
, credit
;
849 if (!ENTRY_VALID(ledger
, entry
))
850 return (KERN_INVALID_VALUE
);
852 le
= &ledger
->l_entries
[entry
];
855 debit
= le
->le_debit
;
856 credit
= le
->le_credit
;
858 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
859 assert(le
->le_debit
== 0);
860 if (!OSCompareAndSwap64(credit
, 0, &le
->le_credit
)) {
863 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_credit
, 0));
864 } else if (credit
> debit
) {
865 if (!OSCompareAndSwap64(debit
, credit
, &le
->le_debit
))
867 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_debit
, le
->le_credit
));
868 } else if (credit
< debit
) {
869 if (!OSCompareAndSwap64(credit
, debit
, &le
->le_credit
))
871 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_credit
, le
->le_debit
));
874 return (KERN_SUCCESS
);
878 ledger_get_limit(ledger_t ledger
, int entry
, ledger_amount_t
*limit
)
880 struct ledger_entry
*le
;
882 if (!ENTRY_VALID(ledger
, entry
))
883 return (KERN_INVALID_VALUE
);
885 le
= &ledger
->l_entries
[entry
];
886 *limit
= le
->le_limit
;
888 lprintf(("ledger_get_limit: %lld\n", *limit
));
890 return (KERN_SUCCESS
);
894 * Adjust the limit of a limited resource. This does not affect the
895 * current balance, so the change doesn't affect the thread until the
898 * warn_level: If non-zero, causes the callback to be invoked when
899 * the balance exceeds this level. Specified as a percentage [of the limit].
902 ledger_set_limit(ledger_t ledger
, int entry
, ledger_amount_t limit
,
903 uint8_t warn_level_percentage
)
905 struct ledger_entry
*le
;
907 if (!ENTRY_VALID(ledger
, entry
))
908 return (KERN_INVALID_VALUE
);
910 lprintf(("ledger_set_limit: %lld\n", limit
));
911 le
= &ledger
->l_entries
[entry
];
913 if (limit
== LEDGER_LIMIT_INFINITY
) {
915 * Caller wishes to disable the limit. This will implicitly
916 * disable automatic refill, as refills implicitly depend
919 ledger_disable_refill(ledger
, entry
);
922 le
->le_limit
= limit
;
923 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
924 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
925 le
->_le
.le_refill
.le_last_refill
= 0;
927 flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
928 flag_clear(&le
->le_flags
, LF_WARNED
);
929 ledger_limit_entry_wakeup(le
);
931 if (warn_level_percentage
!= 0) {
932 assert(warn_level_percentage
<= 100);
933 assert(limit
> 0); /* no negative limit support for warnings */
934 assert(limit
!= LEDGER_LIMIT_INFINITY
); /* warn % without limit makes no sense */
935 le
->le_warn_level
= (le
->le_limit
* warn_level_percentage
) / 100;
937 le
->le_warn_level
= LEDGER_LIMIT_INFINITY
;
940 return (KERN_SUCCESS
);
943 #if CONFIG_LEDGER_INTERVAL_MAX
945 ledger_get_interval_max(ledger_t ledger
, int entry
,
946 ledger_amount_t
*max_interval_balance
, int reset
)
948 struct ledger_entry
*le
;
949 le
= &ledger
->l_entries
[entry
];
951 if (!ENTRY_VALID(ledger
, entry
) || !(le
->le_flags
& LF_TRACKING_MAX
)) {
952 return (KERN_INVALID_VALUE
);
955 *max_interval_balance
= le
->_le
._le_max
.le_interval_max
;
956 lprintf(("ledger_get_interval_max: %lld%s\n", *max_interval_balance
,
957 (reset
) ? " --> 0" : ""));
960 le
->_le
._le_max
.le_interval_max
= 0;
963 return (KERN_SUCCESS
);
965 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
968 ledger_get_lifetime_max(ledger_t ledger
, int entry
,
969 ledger_amount_t
*max_lifetime_balance
)
971 struct ledger_entry
*le
;
972 le
= &ledger
->l_entries
[entry
];
974 if (!ENTRY_VALID(ledger
, entry
) || !(le
->le_flags
& LF_TRACKING_MAX
)) {
975 return (KERN_INVALID_VALUE
);
978 *max_lifetime_balance
= le
->_le
._le_max
.le_lifetime_max
;
979 lprintf(("ledger_get_lifetime_max: %lld\n", *max_lifetime_balance
));
981 return (KERN_SUCCESS
);
985 * Enable tracking of periodic maximums for this ledger entry.
988 ledger_track_maximum(ledger_template_t
template, int entry
,
989 __unused
int period_in_secs
)
991 template_lock(template);
993 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
994 template_unlock(template);
995 return (KERN_INVALID_VALUE
);
998 /* Refill is incompatible with max tracking. */
999 if (template->lt_entries
[entry
].et_flags
& LF_REFILL_SCHEDULED
) {
1000 return (KERN_INVALID_VALUE
);
1003 template->lt_entries
[entry
].et_flags
|= LF_TRACKING_MAX
;
1004 template_unlock(template);
1006 return (KERN_SUCCESS
);
1010 ledger_panic_on_negative(ledger_template_t
template, int entry
)
1012 template_lock(template);
1014 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1015 template_unlock(template);
1016 return (KERN_INVALID_VALUE
);
1019 template->lt_entries
[entry
].et_flags
|= LF_PANIC_ON_NEGATIVE
;
1021 template_unlock(template);
1023 return (KERN_SUCCESS
);
1027 ledger_track_credit_only(ledger_template_t
template, int entry
)
1029 template_lock(template);
1031 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1032 template_unlock(template);
1033 return (KERN_INVALID_VALUE
);
1036 template->lt_entries
[entry
].et_flags
|= LF_TRACK_CREDIT_ONLY
;
1038 template_unlock(template);
1040 return (KERN_SUCCESS
);
1044 * Add a callback to be executed when the resource goes into deficit.
1047 ledger_set_callback(ledger_template_t
template, int entry
,
1048 ledger_callback_t func
, const void *param0
, const void *param1
)
1050 struct entry_template
*et
;
1051 struct ledger_callback
*old_cb
, *new_cb
;
1053 if ((entry
< 0) || (entry
>= template->lt_cnt
))
1054 return (KERN_INVALID_VALUE
);
1057 new_cb
= (struct ledger_callback
*)kalloc(sizeof (*new_cb
));
1058 new_cb
->lc_func
= func
;
1059 new_cb
->lc_param0
= param0
;
1060 new_cb
->lc_param1
= param1
;
1065 template_lock(template);
1066 et
= &template->lt_entries
[entry
];
1067 old_cb
= et
->et_callback
;
1068 et
->et_callback
= new_cb
;
1069 template_unlock(template);
1071 kfree(old_cb
, sizeof (*old_cb
));
1073 return (KERN_SUCCESS
);
1077 * Disable callback notification for a specific ledger entry.
1079 * Otherwise, if using a ledger template which specified a
1080 * callback function (ledger_set_callback()), it will be invoked when
1081 * the resource goes into deficit.
1084 ledger_disable_callback(ledger_t ledger
, int entry
)
1086 if (!ENTRY_VALID(ledger
, entry
))
1087 return (KERN_INVALID_VALUE
);
1090 * le_warn_level is used to indicate *if* this ledger has a warning configured,
1091 * in addition to what that warning level is set to.
1092 * This means a side-effect of ledger_disable_callback() is that the
1093 * warning level is forgotten.
1095 ledger
->l_entries
[entry
].le_warn_level
= LEDGER_LIMIT_INFINITY
;
1096 flag_clear(&ledger
->l_entries
[entry
].le_flags
, LEDGER_ACTION_CALLBACK
);
1097 return (KERN_SUCCESS
);
1101 * Enable callback notification for a specific ledger entry.
1103 * This is only needed if ledger_disable_callback() has previously
1104 * been invoked against an entry; there must already be a callback
1108 ledger_enable_callback(ledger_t ledger
, int entry
)
1110 if (!ENTRY_VALID(ledger
, entry
))
1111 return (KERN_INVALID_VALUE
);
1113 assert(entry_get_callback(ledger
, entry
) != NULL
);
1115 flag_set(&ledger
->l_entries
[entry
].le_flags
, LEDGER_ACTION_CALLBACK
);
1116 return (KERN_SUCCESS
);
1120 * Query the automatic refill period for this ledger entry.
1122 * A period of 0 means this entry has none configured.
1125 ledger_get_period(ledger_t ledger
, int entry
, uint64_t *period
)
1127 struct ledger_entry
*le
;
1129 if (!ENTRY_VALID(ledger
, entry
))
1130 return (KERN_INVALID_VALUE
);
1132 le
= &ledger
->l_entries
[entry
];
1133 *period
= abstime_to_nsecs(le
->_le
.le_refill
.le_refill_period
);
1134 lprintf(("ledger_get_period: %llx\n", *period
));
1135 return (KERN_SUCCESS
);
1139 * Adjust the automatic refill period.
1142 ledger_set_period(ledger_t ledger
, int entry
, uint64_t period
)
1144 struct ledger_entry
*le
;
1146 lprintf(("ledger_set_period: %llx\n", period
));
1147 if (!ENTRY_VALID(ledger
, entry
))
1148 return (KERN_INVALID_VALUE
);
1150 le
= &ledger
->l_entries
[entry
];
1153 * A refill period refills the ledger in multiples of the limit,
1154 * so if you haven't set one yet, you need a lesson on ledgers.
1156 assert(le
->le_limit
!= LEDGER_LIMIT_INFINITY
);
1158 if (le
->le_flags
& LF_TRACKING_MAX
) {
1160 * Refill is incompatible with rolling max tracking.
1162 return (KERN_INVALID_VALUE
);
1165 le
->_le
.le_refill
.le_refill_period
= nsecs_to_abstime(period
);
1168 * Set the 'starting time' for the next refill to now. Since
1169 * we're resetting the balance to zero here, we consider this
1170 * moment the starting time for accumulating a balance that
1171 * counts towards the limit.
1173 le
->_le
.le_refill
.le_last_refill
= mach_absolute_time();
1174 ledger_zero_balance(ledger
, entry
);
1176 flag_set(&le
->le_flags
, LF_REFILL_SCHEDULED
);
1178 return (KERN_SUCCESS
);
1182 * Disable automatic refill.
1185 ledger_disable_refill(ledger_t ledger
, int entry
)
1187 struct ledger_entry
*le
;
1189 if (!ENTRY_VALID(ledger
, entry
))
1190 return (KERN_INVALID_VALUE
);
1192 le
= &ledger
->l_entries
[entry
];
1194 flag_clear(&le
->le_flags
, LF_REFILL_SCHEDULED
);
1196 return (KERN_SUCCESS
);
1200 ledger_get_actions(ledger_t ledger
, int entry
, int *actions
)
1202 if (!ENTRY_VALID(ledger
, entry
))
1203 return (KERN_INVALID_VALUE
);
1205 *actions
= ledger
->l_entries
[entry
].le_flags
& LEDGER_ACTION_MASK
;
1206 lprintf(("ledger_get_actions: %#x\n", *actions
));
1207 return (KERN_SUCCESS
);
1211 ledger_set_action(ledger_t ledger
, int entry
, int action
)
1213 lprintf(("ledger_set_action: %#x\n", action
));
1214 if (!ENTRY_VALID(ledger
, entry
))
1215 return (KERN_INVALID_VALUE
);
1217 flag_set(&ledger
->l_entries
[entry
].le_flags
, action
);
1218 return (KERN_SUCCESS
);
1222 ledger_debit_thread(thread_t thread
, ledger_t ledger
, int entry
, ledger_amount_t amount
)
1224 struct ledger_entry
*le
;
1225 ledger_amount_t old
, new;
1227 if (!ENTRY_VALID(ledger
, entry
) || (amount
< 0))
1228 return (KERN_INVALID_ARGUMENT
);
1231 return (KERN_SUCCESS
);
1233 le
= &ledger
->l_entries
[entry
];
1235 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
1236 assert(le
->le_debit
== 0);
1237 old
= OSAddAtomic64(-amount
, &le
->le_credit
);
1240 old
= OSAddAtomic64(amount
, &le
->le_debit
);
1243 lprintf(("%p Debit %lld->%lld\n", thread
, old
, new));
1246 ledger_entry_check_new_balance(thread
, ledger
, entry
, le
);
1249 return (KERN_SUCCESS
);
1253 ledger_debit(ledger_t ledger
, int entry
, ledger_amount_t amount
)
1255 return ledger_debit_thread(current_thread(), ledger
, entry
, amount
);
1259 ledger_debit_nocheck(ledger_t ledger
, int entry
, ledger_amount_t amount
)
1261 return ledger_debit_thread(NULL
, ledger
, entry
, amount
);
1265 ledger_ast(thread_t thread
)
1267 struct ledger
*l
= thread
->t_ledger
;
1272 uint8_t task_percentage
;
1273 uint64_t task_interval
;
1276 task_t task
= thread
->task
;
1278 lprintf(("Ledger AST for %p\n", thread
));
1280 ASSERT(task
!= NULL
);
1281 ASSERT(thread
== current_thread());
1285 * Take a self-consistent snapshot of the CPU usage monitor parameters. The task
1286 * can change them at any point (with the task locked).
1289 task_flags
= task
->rusage_cpu_flags
;
1290 task_percentage
= task
->rusage_cpu_perthr_percentage
;
1291 task_interval
= task
->rusage_cpu_perthr_interval
;
1295 * Make sure this thread is up to date with regards to any task-wide per-thread
1296 * CPU limit, but only if it doesn't have a thread-private blocking CPU limit.
1298 if (((task_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) != 0) &&
1299 ((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0)) {
1304 thread_get_cpulimit(&action
, &percentage
, &interval
);
1307 * If the thread's CPU limits no longer match the task's, or the
1308 * task has a limit but the thread doesn't, update the limit.
1310 if (((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0) ||
1311 (interval
!= task_interval
) || (percentage
!= task_percentage
)) {
1312 thread_set_cpulimit(THREAD_CPULIMIT_EXCEPTION
, task_percentage
, task_interval
);
1313 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) != 0);
1315 } else if (((task_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) &&
1316 (thread
->options
& TH_OPT_PROC_CPULIMIT
)) {
1317 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
1320 * Task no longer has a per-thread CPU limit; remove this thread's
1321 * corresponding CPU limit.
1323 thread_set_cpulimit(THREAD_CPULIMIT_DISABLE
, 0, 0);
1324 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
1328 * If the task or thread is being terminated, let's just get on with it
1330 if ((l
== NULL
) || !task
->active
|| task
->halting
|| !thread
->active
)
1334 * Examine all entries in deficit to see which might be eligble for
1335 * an automatic refill, which require callbacks to be issued, and
1336 * which require blocking.
1339 now
= mach_absolute_time();
1342 * Note that thread->t_threadledger may have been changed by the
1343 * thread_set_cpulimit() call above - so don't examine it until afterwards.
1345 thl
= thread
->t_threadledger
;
1346 if (LEDGER_VALID(thl
)) {
1347 block
|= ledger_check_needblock(thl
, now
);
1349 block
|= ledger_check_needblock(l
, now
);
1352 * If we are supposed to block on the availability of one or more
1353 * resources, find the first entry in deficit for which we should wait.
1354 * Schedule a refill if necessary and then sleep until the resource
1355 * becomes available.
1358 if (LEDGER_VALID(thl
)) {
1359 ret
= ledger_perform_blocking(thl
);
1360 if (ret
!= KERN_SUCCESS
)
1363 ret
= ledger_perform_blocking(l
);
1364 if (ret
!= KERN_SUCCESS
)
1370 ledger_check_needblock(ledger_t l
, uint64_t now
)
1373 uint32_t flags
, block
= 0;
1374 struct ledger_entry
*le
;
1375 struct ledger_callback
*lc
;
1378 for (i
= 0; i
< l
->l_size
; i
++) {
1379 le
= &l
->l_entries
[i
];
1381 lc
= entry_get_callback(l
, i
);
1383 if (limit_exceeded(le
) == FALSE
) {
1384 if (le
->le_flags
& LEDGER_ACTION_CALLBACK
) {
1386 * If needed, invoke the callback as a warning.
1387 * This needs to happen both when the balance rises above
1388 * the warning level, and also when it dips back below it.
1392 * See comments for matching logic in ledger_check_new_balance().
1394 if (warn_level_exceeded(le
)) {
1395 flags
= flag_set(&le
->le_flags
, LF_WARNED
);
1396 if ((flags
& LF_WARNED
) == 0) {
1397 lc
->lc_func(LEDGER_WARNING_ROSE_ABOVE
, lc
->lc_param0
, lc
->lc_param1
);
1400 flags
= flag_clear(&le
->le_flags
, LF_WARNED
);
1401 if (flags
& LF_WARNED
) {
1402 lc
->lc_func(LEDGER_WARNING_DIPPED_BELOW
, lc
->lc_param0
, lc
->lc_param1
);
1410 /* We're over the limit, so refill if we are eligible and past due. */
1411 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
1412 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
1414 if ((le
->_le
.le_refill
.le_last_refill
+ le
->_le
.le_refill
.le_refill_period
) > now
) {
1415 ledger_refill(now
, l
, i
);
1416 if (limit_exceeded(le
) == FALSE
)
1421 if (le
->le_flags
& LEDGER_ACTION_BLOCK
)
1423 if ((le
->le_flags
& LEDGER_ACTION_CALLBACK
) == 0)
1427 * If the LEDGER_ACTION_CALLBACK flag is on, we expect there to
1428 * be a registered callback.
1431 flags
= flag_set(&le
->le_flags
, LF_CALLED_BACK
);
1432 /* Callback has already been called */
1433 if (flags
& LF_CALLED_BACK
)
1435 lc
->lc_func(FALSE
, lc
->lc_param0
, lc
->lc_param1
);
1441 /* return KERN_SUCCESS to continue, KERN_FAILURE to restart */
1442 static kern_return_t
1443 ledger_perform_blocking(ledger_t l
)
1447 struct ledger_entry
*le
;
1449 for (i
= 0; i
< l
->l_size
; i
++) {
1450 le
= &l
->l_entries
[i
];
1451 if ((!limit_exceeded(le
)) ||
1452 ((le
->le_flags
& LEDGER_ACTION_BLOCK
) == 0))
1455 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
1457 /* Prepare to sleep until the resource is refilled */
1458 ret
= assert_wait_deadline(le
, THREAD_INTERRUPTIBLE
,
1459 le
->_le
.le_refill
.le_last_refill
+ le
->_le
.le_refill
.le_refill_period
);
1460 if (ret
!= THREAD_WAITING
)
1461 return(KERN_SUCCESS
);
1463 /* Mark that somebody is waiting on this entry */
1464 flag_set(&le
->le_flags
, LF_WAKE_NEEDED
);
1466 ret
= thread_block_reason(THREAD_CONTINUE_NULL
, NULL
,
1468 if (ret
!= THREAD_AWAKENED
)
1469 return(KERN_SUCCESS
);
1472 * The world may have changed while we were asleep.
1473 * Some other resource we need may have gone into
1474 * deficit. Or maybe we're supposed to die now.
1475 * Go back to the top and reevaluate.
1477 return(KERN_FAILURE
);
1479 return(KERN_SUCCESS
);
1484 ledger_get_entries(ledger_t ledger
, int entry
, ledger_amount_t
*credit
,
1485 ledger_amount_t
*debit
)
1487 struct ledger_entry
*le
;
1489 if (!ENTRY_VALID(ledger
, entry
))
1490 return (KERN_INVALID_ARGUMENT
);
1492 le
= &ledger
->l_entries
[entry
];
1494 *credit
= le
->le_credit
;
1495 *debit
= le
->le_debit
;
1497 return (KERN_SUCCESS
);
1501 ledger_reset_callback_state(ledger_t ledger
, int entry
)
1503 struct ledger_entry
*le
;
1505 if (!ENTRY_VALID(ledger
, entry
))
1506 return (KERN_INVALID_ARGUMENT
);
1508 le
= &ledger
->l_entries
[entry
];
1510 flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
1512 return (KERN_SUCCESS
);
1516 ledger_disable_panic_on_negative(ledger_t ledger
, int entry
)
1518 struct ledger_entry
*le
;
1520 if (!ENTRY_VALID(ledger
, entry
))
1521 return (KERN_INVALID_ARGUMENT
);
1523 le
= &ledger
->l_entries
[entry
];
1525 flag_clear(&le
->le_flags
, LF_PANIC_ON_NEGATIVE
);
1527 return (KERN_SUCCESS
);
1531 ledger_get_panic_on_negative(ledger_t ledger
, int entry
, int *panic_on_negative
)
1533 struct ledger_entry
*le
;
1535 if (!ENTRY_VALID(ledger
, entry
))
1536 return (KERN_INVALID_ARGUMENT
);
1538 le
= &ledger
->l_entries
[entry
];
1540 if (le
->le_flags
& LF_PANIC_ON_NEGATIVE
) {
1541 *panic_on_negative
= TRUE
;
1543 *panic_on_negative
= FALSE
;
1546 return (KERN_SUCCESS
);
1550 ledger_get_balance(ledger_t ledger
, int entry
, ledger_amount_t
*balance
)
1552 struct ledger_entry
*le
;
1554 if (!ENTRY_VALID(ledger
, entry
))
1555 return (KERN_INVALID_ARGUMENT
);
1557 le
= &ledger
->l_entries
[entry
];
1559 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
1560 assert(le
->le_debit
== 0);
1562 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
1565 *balance
= le
->le_credit
- le
->le_debit
;
1567 return (KERN_SUCCESS
);
1571 ledger_template_info(void **buf
, int *len
)
1573 struct ledger_template_info
*lti
;
1574 struct entry_template
*et
;
1579 * Since all tasks share a ledger template, we'll just use the
1580 * caller's as the source.
1582 l
= current_task()->ledger
;
1583 if ((*len
< 0) || (l
== NULL
))
1586 if (*len
> l
->l_size
)
1588 lti
= kalloc((*len
) * sizeof (struct ledger_template_info
));
1593 template_lock(l
->l_template
);
1594 et
= l
->l_template
->lt_entries
;
1596 for (i
= 0; i
< *len
; i
++) {
1597 memset(lti
, 0, sizeof (*lti
));
1598 strlcpy(lti
->lti_name
, et
->et_key
, LEDGER_NAME_MAX
);
1599 strlcpy(lti
->lti_group
, et
->et_group
, LEDGER_NAME_MAX
);
1600 strlcpy(lti
->lti_units
, et
->et_units
, LEDGER_NAME_MAX
);
1604 template_unlock(l
->l_template
);
1610 ledger_fill_entry_info(struct ledger_entry
*le
,
1611 struct ledger_entry_info
*lei
,
1615 assert(lei
!= NULL
);
1617 memset(lei
, 0, sizeof (*lei
));
1619 lei
->lei_limit
= le
->le_limit
;
1620 lei
->lei_credit
= le
->le_credit
;
1621 lei
->lei_debit
= le
->le_debit
;
1622 lei
->lei_balance
= lei
->lei_credit
- lei
->lei_debit
;
1623 lei
->lei_refill_period
= (le
->le_flags
& LF_REFILL_SCHEDULED
) ?
1624 abstime_to_nsecs(le
->_le
.le_refill
.le_refill_period
) : 0;
1625 lei
->lei_last_refill
= abstime_to_nsecs(now
- le
->_le
.le_refill
.le_last_refill
);
1629 ledger_get_task_entry_info_multiple(task_t task
, void **buf
, int *len
)
1631 struct ledger_entry_info
*lei
;
1632 struct ledger_entry
*le
;
1633 uint64_t now
= mach_absolute_time();
1637 if ((*len
< 0) || ((l
= task
->ledger
) == NULL
))
1640 if (*len
> l
->l_size
)
1642 lei
= kalloc((*len
) * sizeof (struct ledger_entry_info
));
1649 for (i
= 0; i
< *len
; i
++) {
1650 ledger_fill_entry_info(le
, lei
, now
);
1659 ledger_get_entry_info(ledger_t ledger
,
1661 struct ledger_entry_info
*lei
)
1663 uint64_t now
= mach_absolute_time();
1665 assert(ledger
!= NULL
);
1666 assert(lei
!= NULL
);
1668 if (entry
>= 0 && entry
< ledger
->l_size
) {
1669 struct ledger_entry
*le
= &ledger
->l_entries
[entry
];
1670 ledger_fill_entry_info(le
, lei
, now
);
1675 ledger_info(task_t task
, struct ledger_info
*info
)
1679 if ((l
= task
->ledger
) == NULL
)
1682 memset(info
, 0, sizeof (*info
));
1684 strlcpy(info
->li_name
, l
->l_template
->lt_name
, LEDGER_NAME_MAX
);
1685 info
->li_id
= l
->l_id
;
1686 info
->li_entries
= l
->l_size
;
1692 ledger_limit(task_t task
, struct ledger_limit_args
*args
)
1698 if ((l
= task
->ledger
) == NULL
)
1701 idx
= ledger_key_lookup(l
->l_template
, args
->lla_name
);
1702 if ((idx
< 0) || (idx
>= l
->l_size
))
1706 * XXX - this doesn't really seem like the right place to have
1707 * a context-sensitive conversion of userspace units into kernel
1708 * units. For now I'll handwave and say that the ledger() system
1709 * call isn't meant for civilians to use - they should be using
1710 * the process policy interfaces.
1712 if (idx
== task_ledgers
.cpu_time
) {
1715 if (args
->lla_refill_period
) {
1717 * If a refill is scheduled, then the limit is
1718 * specified as a percentage of one CPU. The
1719 * syscall specifies the refill period in terms of
1720 * milliseconds, so we need to convert to nsecs.
1722 args
->lla_refill_period
*= 1000000;
1723 nsecs
= args
->lla_limit
*
1724 (args
->lla_refill_period
/ 100);
1725 lprintf(("CPU limited to %lld nsecs per second\n",
1729 * If no refill is scheduled, then this is a
1730 * fixed amount of CPU time (in nsecs) that can
1733 nsecs
= args
->lla_limit
;
1734 lprintf(("CPU limited to %lld nsecs\n", nsecs
));
1736 limit
= nsecs_to_abstime(nsecs
);
1738 limit
= args
->lla_limit
;
1739 lprintf(("%s limited to %lld\n", args
->lla_name
, limit
));
1742 if (args
->lla_refill_period
> 0)
1743 ledger_set_period(l
, idx
, args
->lla_refill_period
);
1745 ledger_set_limit(l
, idx
, limit
);
1746 flag_set(&l
->l_entries
[idx
].le_flags
, LEDGER_ACTION_BLOCK
);