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>
37 #include <kern/coalition.h>
39 #include <kern/processor.h>
40 #include <kern/machine.h>
41 #include <kern/queue.h>
42 #include <kern/policy_internal.h>
44 #include <sys/errno.h>
46 #include <libkern/OSAtomic.h>
47 #include <mach/mach_types.h>
48 #include <os/overflow.h>
53 * Ledger entry flags. Bits in second nibble (masked by 0xF0) are used for
54 * ledger actions (LEDGER_ACTION_BLOCK, etc).
56 #define LF_ENTRY_ACTIVE 0x0001 /* entry is active if set */
57 #define LF_WAKE_NEEDED 0x0100 /* one or more threads are asleep */
58 #define LF_WAKE_INPROGRESS 0x0200 /* the wait queue is being processed */
59 #define LF_REFILL_SCHEDULED 0x0400 /* a refill timer has been set */
60 #define LF_REFILL_INPROGRESS 0x0800 /* the ledger is being refilled */
61 #define LF_CALLED_BACK 0x1000 /* callback was called for balance in deficit */
62 #define LF_WARNED 0x2000 /* callback was called for balance warning */
63 #define LF_TRACKING_MAX 0x4000 /* track max balance. Exclusive w.r.t refill */
64 #define LF_PANIC_ON_NEGATIVE 0x8000 /* panic if it goes negative */
65 #define LF_TRACK_CREDIT_ONLY 0x10000 /* only update "credit" */
67 /* Determine whether a ledger entry exists and has been initialized and active */
68 #define ENTRY_VALID(l, e) \
69 (((l) != NULL) && ((e) >= 0) && ((e) < (l)->l_size) && \
70 (((l)->l_entries[e].le_flags & LF_ENTRY_ACTIVE) == LF_ENTRY_ACTIVE))
72 #define ASSERT(a) assert(a)
77 #define lprintf(a) if (ledger_debug) { \
78 printf("%lld ", abstime_to_nsecs(mach_absolute_time() / 1000000)); \
85 struct ledger_callback
{
86 ledger_callback_t lc_func
;
87 const void *lc_param0
;
88 const void *lc_param1
;
91 struct entry_template
{
92 char et_key
[LEDGER_NAME_MAX
];
93 char et_group
[LEDGER_NAME_MAX
];
94 char et_units
[LEDGER_NAME_MAX
];
96 struct ledger_callback
*et_callback
;
99 lck_grp_t ledger_lck_grp
;
100 os_refgrp_decl(static, ledger_refgrp
, "ledger", NULL
);
103 * Modifying the reference count, table size, or table contents requires
104 * holding the lt_lock. Modfying the table address requires both lt_lock
105 * and setting the inuse bit. This means that the lt_entries field can be
106 * safely dereferenced if you hold either the lock or the inuse bit. The
107 * inuse bit exists solely to allow us to swap in a new, larger entries
108 * table without requiring a full lock to be acquired on each lookup.
109 * Accordingly, the inuse bit should never be held for longer than it takes
110 * to extract a value from the table - i.e., 2 or 3 memory references.
112 struct ledger_template
{
117 volatile uint32_t lt_inuse
;
121 struct entry_template
*lt_entries
;
124 #define template_lock(template) lck_mtx_lock(&(template)->lt_lock)
125 #define template_unlock(template) lck_mtx_unlock(&(template)->lt_lock)
127 #define TEMPLATE_INUSE(s, t) { \
129 while (OSCompareAndSwap(0, 1, &((t)->lt_inuse))) \
133 #define TEMPLATE_IDLE(s, t) { \
138 static int ledger_cnt
= 0;
139 /* ledger ast helper functions */
140 static uint32_t ledger_check_needblock(ledger_t l
, uint64_t now
);
141 static kern_return_t
ledger_perform_blocking(ledger_t l
);
142 static uint32_t flag_set(volatile uint32_t *flags
, uint32_t bit
);
143 static uint32_t flag_clear(volatile uint32_t *flags
, uint32_t bit
);
145 static void ledger_entry_check_new_balance(thread_t thread
, ledger_t ledger
,
146 int entry
, struct ledger_entry
*le
);
150 debug_callback(const void *p0
, __unused
const void *p1
)
152 printf("ledger: resource exhausted [%s] for task %p\n",
153 (const char *)p0
, p1
);
157 /************************************/
160 abstime_to_nsecs(uint64_t abstime
)
164 absolutetime_to_nanoseconds(abstime
, &nsecs
);
169 nsecs_to_abstime(uint64_t nsecs
)
173 nanoseconds_to_absolutetime(nsecs
, &abstime
);
180 lck_grp_init(&ledger_lck_grp
, "ledger", LCK_GRP_ATTR_NULL
);
184 ledger_template_create(const char *name
)
186 ledger_template_t
template;
188 template = (ledger_template_t
)kalloc(sizeof(*template));
189 if (template == NULL
) {
193 template->lt_name
= name
;
194 template->lt_refs
= 1;
195 template->lt_cnt
= 0;
196 template->lt_table_size
= 1;
197 template->lt_inuse
= 0;
198 template->lt_zone
= NULL
;
199 lck_mtx_init(&template->lt_lock
, &ledger_lck_grp
, LCK_ATTR_NULL
);
201 template->lt_entries
= (struct entry_template
*)
202 kalloc(sizeof(struct entry_template
) * template->lt_table_size
);
203 if (template->lt_entries
== NULL
) {
204 kfree(template, sizeof(*template));
212 ledger_template_copy(ledger_template_t
template, const char *name
)
214 struct entry_template
* new_entries
= NULL
;
215 ledger_template_t new_template
= ledger_template_create(name
);
217 if (new_template
== NULL
) {
221 template_lock(template);
222 assert(template->lt_initialized
);
224 new_entries
= (struct entry_template
*)
225 kalloc(sizeof(struct entry_template
) * template->lt_table_size
);
228 /* Copy the template entries. */
229 bcopy(template->lt_entries
, new_entries
, sizeof(struct entry_template
) * template->lt_table_size
);
230 kfree(new_template
->lt_entries
, sizeof(struct entry_template
) * new_template
->lt_table_size
);
232 new_template
->lt_entries
= new_entries
;
233 new_template
->lt_table_size
= template->lt_table_size
;
234 new_template
->lt_cnt
= template->lt_cnt
;
236 /* Tear down the new template; we've failed. :( */
237 ledger_template_dereference(new_template
);
241 template_unlock(template);
247 ledger_template_dereference(ledger_template_t
template)
249 template_lock(template);
251 template_unlock(template);
253 if (template->lt_refs
== 0) {
254 kfree(template->lt_entries
, sizeof(struct entry_template
) * template->lt_table_size
);
255 lck_mtx_destroy(&template->lt_lock
, &ledger_lck_grp
);
256 kfree(template, sizeof(*template));
261 * Add a new entry to the list of entries in a ledger template. There is
262 * currently no mechanism to remove an entry. Implementing such a mechanism
263 * would require us to maintain per-entry reference counts, which we would
264 * prefer to avoid if possible.
267 ledger_entry_add(ledger_template_t
template, const char *key
,
268 const char *group
, const char *units
)
271 struct entry_template
*et
;
273 if ((key
== NULL
) || (strlen(key
) >= LEDGER_NAME_MAX
) || (template->lt_zone
!= NULL
)) {
277 template_lock(template);
279 /* If the table is full, attempt to double its size */
280 if (template->lt_cnt
== template->lt_table_size
) {
281 struct entry_template
*new_entries
, *old_entries
;
282 int old_cnt
, old_sz
, new_sz
= 0;
285 old_cnt
= template->lt_table_size
;
286 old_sz
= old_cnt
* (int)(sizeof(struct entry_template
));
287 /* double old_sz allocation, but check for overflow */
288 if (os_mul_overflow(old_sz
, 2, &new_sz
)) {
289 template_unlock(template);
292 new_entries
= kalloc(new_sz
);
293 if (new_entries
== NULL
) {
294 template_unlock(template);
297 memcpy(new_entries
, template->lt_entries
, old_sz
);
298 memset(((char *)new_entries
) + old_sz
, 0, old_sz
);
299 /* assume: if the sz didn't overflow, neither will the count */
300 template->lt_table_size
= old_cnt
* 2;
302 old_entries
= template->lt_entries
;
304 TEMPLATE_INUSE(s
, template);
305 template->lt_entries
= new_entries
;
306 TEMPLATE_IDLE(s
, template);
308 kfree(old_entries
, old_sz
);
311 et
= &template->lt_entries
[template->lt_cnt
];
312 strlcpy(et
->et_key
, key
, LEDGER_NAME_MAX
);
313 strlcpy(et
->et_group
, group
, LEDGER_NAME_MAX
);
314 strlcpy(et
->et_units
, units
, LEDGER_NAME_MAX
);
315 et
->et_flags
= LF_ENTRY_ACTIVE
;
316 et
->et_callback
= NULL
;
318 idx
= template->lt_cnt
++;
319 template_unlock(template);
326 ledger_entry_setactive(ledger_t ledger
, int entry
)
328 struct ledger_entry
*le
;
330 if ((ledger
== NULL
) || (entry
< 0) || (entry
>= ledger
->l_size
)) {
331 return KERN_INVALID_ARGUMENT
;
334 le
= &ledger
->l_entries
[entry
];
335 if ((le
->le_flags
& LF_ENTRY_ACTIVE
) == 0) {
336 flag_set(&le
->le_flags
, LF_ENTRY_ACTIVE
);
343 ledger_key_lookup(ledger_template_t
template, const char *key
)
347 template_lock(template);
348 for (idx
= 0; idx
< template->lt_cnt
; idx
++) {
349 if (template->lt_entries
!= NULL
&&
350 (strcmp(key
, template->lt_entries
[idx
].et_key
) == 0)) {
355 if (idx
>= template->lt_cnt
) {
358 template_unlock(template);
364 * Complete the initialization of ledger template
365 * by initializing ledger zone. After initializing
366 * the ledger zone, adding an entry in the ledger
367 * template would fail.
370 ledger_template_complete(ledger_template_t
template)
373 ledger_size
= sizeof(struct ledger
) + (template->lt_cnt
* sizeof(struct ledger_entry
));
374 template->lt_zone
= zinit(ledger_size
, CONFIG_TASK_MAX
* ledger_size
,
377 template->lt_initialized
= true;
381 * Like ledger_template_complete, except we'll ask
382 * the pmap layer to manage allocations for us.
383 * Meant for ledgers that should be owned by the
387 ledger_template_complete_secure_alloc(ledger_template_t
template)
390 ledger_size
= sizeof(struct ledger
) + (template->lt_cnt
* sizeof(struct ledger_entry
));
391 pmap_ledger_alloc_init(ledger_size
);
392 template->lt_initialized
= true;
396 * Create a new ledger based on the specified template. As part of the
397 * ledger creation we need to allocate space for a table of ledger entries.
398 * The size of the table is based on the size of the template at the time
399 * the ledger is created. If additional entries are added to the template
400 * after the ledger is created, they will not be tracked in this ledger.
403 ledger_instantiate(ledger_template_t
template, int entry_type
)
409 template_lock(template);
411 cnt
= template->lt_cnt
;
412 template_unlock(template);
414 if (template->lt_zone
) {
415 ledger
= (ledger_t
)zalloc(template->lt_zone
);
417 ledger
= pmap_ledger_alloc();
420 if (ledger
== NULL
) {
421 ledger_template_dereference(template);
425 ledger
->l_template
= template;
426 ledger
->l_id
= ledger_cnt
++;
427 os_ref_init(&ledger
->l_refs
, &ledger_refgrp
);
428 ledger
->l_size
= (int32_t)cnt
;
430 template_lock(template);
431 assert(ledger
->l_size
<= template->lt_cnt
);
432 for (i
= 0; i
< ledger
->l_size
; i
++) {
433 struct ledger_entry
*le
= &ledger
->l_entries
[i
];
434 struct entry_template
*et
= &template->lt_entries
[i
];
436 le
->le_flags
= et
->et_flags
;
437 /* make entry inactive by removing active bit */
438 if (entry_type
== LEDGER_CREATE_INACTIVE_ENTRIES
) {
439 flag_clear(&le
->le_flags
, LF_ENTRY_ACTIVE
);
442 * If template has a callback, this entry is opted-in,
445 if (et
->et_callback
!= NULL
) {
446 flag_set(&le
->le_flags
, LEDGER_ACTION_CALLBACK
);
450 le
->le_limit
= LEDGER_LIMIT_INFINITY
;
451 le
->le_warn_percent
= LEDGER_PERCENT_NONE
;
452 le
->_le
.le_refill
.le_refill_period
= 0;
453 le
->_le
.le_refill
.le_last_refill
= 0;
455 template_unlock(template);
461 flag_set(volatile uint32_t *flags
, uint32_t bit
)
463 return OSBitOrAtomic(bit
, flags
);
467 flag_clear(volatile uint32_t *flags
, uint32_t bit
)
469 return OSBitAndAtomic(~bit
, flags
);
473 * Take a reference on a ledger
476 ledger_reference(ledger_t ledger
)
478 if (!LEDGER_VALID(ledger
)) {
482 os_ref_retain(&ledger
->l_refs
);
486 * Remove a reference on a ledger. If this is the last reference,
487 * deallocate the unused ledger.
490 ledger_dereference(ledger_t ledger
)
492 if (!LEDGER_VALID(ledger
)) {
496 if (os_ref_release(&ledger
->l_refs
) == 0) {
497 if (ledger
->l_template
->lt_zone
) {
498 zfree(ledger
->l_template
->lt_zone
, ledger
);
500 pmap_ledger_free(ledger
);
506 * Determine whether an entry has exceeded its warning level.
509 warn_level_exceeded(struct ledger_entry
*le
)
511 ledger_amount_t balance
;
513 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
514 assert(le
->le_debit
== 0);
516 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
520 * XXX - Currently, we only support warnings for ledgers which
521 * use positive limits.
523 balance
= le
->le_credit
- le
->le_debit
;
524 if (le
->le_warn_percent
!= LEDGER_PERCENT_NONE
&&
525 ((balance
> (le
->le_limit
* le
->le_warn_percent
) >> 16))) {
532 * Determine whether an entry has exceeded its limit.
535 limit_exceeded(struct ledger_entry
*le
)
537 ledger_amount_t balance
;
539 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
540 assert(le
->le_debit
== 0);
542 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
545 balance
= le
->le_credit
- le
->le_debit
;
546 if ((le
->le_limit
<= 0) && (balance
< le
->le_limit
)) {
550 if ((le
->le_limit
> 0) && (balance
> le
->le_limit
)) {
556 static inline struct ledger_callback
*
557 entry_get_callback(ledger_t ledger
, int entry
)
559 struct ledger_callback
*callback
;
562 TEMPLATE_INUSE(s
, ledger
->l_template
);
563 callback
= ledger
->l_template
->lt_entries
[entry
].et_callback
;
564 TEMPLATE_IDLE(s
, ledger
->l_template
);
570 * If the ledger value is positive, wake up anybody waiting on it.
573 ledger_limit_entry_wakeup(struct ledger_entry
*le
)
577 if (!limit_exceeded(le
)) {
578 flags
= flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
580 while (le
->le_flags
& LF_WAKE_NEEDED
) {
581 flag_clear(&le
->le_flags
, LF_WAKE_NEEDED
);
582 thread_wakeup((event_t
)le
);
588 * Refill the coffers.
591 ledger_refill(uint64_t now
, ledger_t ledger
, int entry
)
593 uint64_t elapsed
, period
, periods
;
594 struct ledger_entry
*le
;
595 ledger_amount_t balance
, due
;
597 assert(entry
>= 0 && entry
< ledger
->l_size
);
599 le
= &ledger
->l_entries
[entry
];
601 assert(le
->le_limit
!= LEDGER_LIMIT_INFINITY
);
603 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
604 assert(le
->le_debit
== 0);
609 * If another thread is handling the refill already, we're not
612 if (flag_set(&le
->le_flags
, LF_REFILL_INPROGRESS
) & LF_REFILL_INPROGRESS
) {
617 * If the timestamp we're about to use to refill is older than the
618 * last refill, then someone else has already refilled this ledger
619 * and there's nothing for us to do here.
621 if (now
<= le
->_le
.le_refill
.le_last_refill
) {
622 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
627 * See how many refill periods have passed since we last
630 period
= le
->_le
.le_refill
.le_refill_period
;
631 elapsed
= now
- le
->_le
.le_refill
.le_last_refill
;
632 if ((period
== 0) || (elapsed
< period
)) {
633 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
638 * Optimize for the most common case of only one or two
642 while ((periods
< 2) && (elapsed
> 0)) {
648 * OK, it's been a long time. Do a divide to figure out
652 periods
= (now
- le
->_le
.le_refill
.le_last_refill
) / period
;
655 balance
= le
->le_credit
- le
->le_debit
;
656 due
= periods
* le
->le_limit
;
658 if (balance
- due
< 0) {
662 assertf(due
>= 0, "now=%llu, ledger=%p, entry=%d, balance=%lld, due=%lld", now
, ledger
, entry
, balance
, due
);
664 OSAddAtomic64(due
, &le
->le_debit
);
666 assert(le
->le_debit
>= 0);
669 * If we've completely refilled the pool, set the refill time to now.
670 * Otherwise set it to the time at which it last should have been
673 if (balance
== due
) {
674 le
->_le
.le_refill
.le_last_refill
= now
;
676 le
->_le
.le_refill
.le_last_refill
+= (le
->_le
.le_refill
.le_refill_period
* periods
);
679 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
681 lprintf(("Refill %lld %lld->%lld\n", periods
, balance
, balance
- due
));
682 if (!limit_exceeded(le
)) {
683 ledger_limit_entry_wakeup(le
);
688 ledger_entry_check_new_balance(thread_t thread
, ledger_t ledger
,
689 int entry
, struct ledger_entry
*le
)
691 if (le
->le_flags
& LF_TRACKING_MAX
) {
692 ledger_amount_t balance
= le
->le_credit
- le
->le_debit
;
694 if (balance
> le
->_le
._le_max
.le_lifetime_max
) {
695 le
->_le
._le_max
.le_lifetime_max
= balance
;
698 #if CONFIG_LEDGER_INTERVAL_MAX
699 if (balance
> le
->_le
._le_max
.le_interval_max
) {
700 le
->_le
._le_max
.le_interval_max
= balance
;
702 #endif /* LEDGER_CONFIG_INTERVAL_MAX */
705 /* Check to see whether we're due a refill */
706 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
707 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
709 uint64_t now
= mach_absolute_time();
710 if ((now
- le
->_le
.le_refill
.le_last_refill
) > le
->_le
.le_refill
.le_refill_period
) {
711 ledger_refill(now
, ledger
, entry
);
715 if (limit_exceeded(le
)) {
717 * We've exceeded the limit for this entry. There
718 * are several possible ways to handle it. We can block,
719 * we can execute a callback, or we can ignore it. In
720 * either of the first two cases, we want to set the AST
721 * flag so we can take the appropriate action just before
722 * leaving the kernel. The one caveat is that if we have
723 * already called the callback, we don't want to do it
724 * again until it gets rearmed.
726 if ((le
->le_flags
& LEDGER_ACTION_BLOCK
) ||
727 (!(le
->le_flags
& LF_CALLED_BACK
) &&
728 entry_get_callback(ledger
, entry
))) {
729 act_set_astledger_async(thread
);
733 * The balance on the account is below the limit.
735 * If there are any threads blocked on this entry, now would
736 * be a good time to wake them up.
738 if (le
->le_flags
& LF_WAKE_NEEDED
) {
739 ledger_limit_entry_wakeup(le
);
742 if (le
->le_flags
& LEDGER_ACTION_CALLBACK
) {
744 * Client has requested that a callback be invoked whenever
745 * the ledger's balance crosses into or out of the warning
748 if (warn_level_exceeded(le
)) {
750 * This ledger's balance is above the warning level.
752 if ((le
->le_flags
& LF_WARNED
) == 0) {
754 * If we are above the warning level and
755 * have not yet invoked the callback,
756 * set the AST so it can be done before returning
759 act_set_astledger_async(thread
);
763 * This ledger's balance is below the warning level.
765 if (le
->le_flags
& LF_WARNED
) {
767 * If we are below the warning level and
768 * the LF_WARNED flag is still set, we need
769 * to invoke the callback to let the client
770 * know the ledger balance is now back below
773 act_set_astledger_async(thread
);
779 if ((le
->le_flags
& LF_PANIC_ON_NEGATIVE
) &&
780 (le
->le_credit
< le
->le_debit
)) {
781 panic("ledger_entry_check_new_balance(%p,%d): negative ledger %p credit:%lld debit:%lld balance:%lld\n",
785 le
->le_credit
- le
->le_debit
);
790 ledger_check_new_balance(thread_t thread
, ledger_t ledger
, int entry
)
792 struct ledger_entry
*le
;
793 assert(entry
> 0 && entry
<= ledger
->l_size
);
794 le
= &ledger
->l_entries
[entry
];
795 ledger_entry_check_new_balance(thread
, ledger
, entry
, le
);
799 * Add value to an entry in a ledger for a specific thread.
802 ledger_credit_thread(thread_t thread
, ledger_t ledger
, int entry
, ledger_amount_t amount
)
804 ledger_amount_t old
, new;
805 struct ledger_entry
*le
;
807 if (!ENTRY_VALID(ledger
, entry
) || (amount
< 0)) {
808 return KERN_INVALID_VALUE
;
815 le
= &ledger
->l_entries
[entry
];
817 old
= OSAddAtomic64(amount
, &le
->le_credit
);
819 lprintf(("%p Credit %lld->%lld\n", thread
, old
, new));
822 ledger_entry_check_new_balance(thread
, ledger
, entry
, le
);
829 * Add value to an entry in a ledger.
832 ledger_credit(ledger_t ledger
, int entry
, ledger_amount_t amount
)
834 return ledger_credit_thread(current_thread(), ledger
, entry
, amount
);
838 * Add value to an entry in a ledger; do not check balance after update.
841 ledger_credit_nocheck(ledger_t ledger
, int entry
, ledger_amount_t amount
)
843 return ledger_credit_thread(NULL
, ledger
, entry
, amount
);
846 /* Add all of one ledger's values into another.
847 * They must have been created from the same template.
848 * This is not done atomically. Another thread (if not otherwise synchronized)
849 * may see bogus values when comparing one entry to another.
850 * As each entry's credit & debit are modified one at a time, the warning/limit
851 * may spuriously trip, or spuriously fail to trip, or another thread (if not
852 * otherwise synchronized) may see a bogus balance.
855 ledger_rollup(ledger_t to_ledger
, ledger_t from_ledger
)
859 assert(to_ledger
->l_template
->lt_cnt
== from_ledger
->l_template
->lt_cnt
);
861 for (i
= 0; i
< to_ledger
->l_size
; i
++) {
862 ledger_rollup_entry(to_ledger
, from_ledger
, i
);
868 /* Add one ledger entry value to another.
869 * They must have been created from the same template.
870 * Since the credit and debit values are added one
871 * at a time, other thread might read the a bogus value.
874 ledger_rollup_entry(ledger_t to_ledger
, ledger_t from_ledger
, int entry
)
876 struct ledger_entry
*from_le
, *to_le
;
878 assert(to_ledger
->l_template
->lt_cnt
== from_ledger
->l_template
->lt_cnt
);
879 if (ENTRY_VALID(from_ledger
, entry
) && ENTRY_VALID(to_ledger
, entry
)) {
880 from_le
= &from_ledger
->l_entries
[entry
];
881 to_le
= &to_ledger
->l_entries
[entry
];
882 OSAddAtomic64(from_le
->le_credit
, &to_le
->le_credit
);
883 OSAddAtomic64(from_le
->le_debit
, &to_le
->le_debit
);
890 * Zero the balance of a ledger by adding to its credit or debit, whichever is smaller.
891 * Note that some clients of ledgers (notably, task wakeup statistics) require that
892 * le_credit only ever increase as a function of ledger_credit().
895 ledger_zero_balance(ledger_t ledger
, int entry
)
897 struct ledger_entry
*le
;
898 ledger_amount_t debit
, credit
;
900 if (!ENTRY_VALID(ledger
, entry
)) {
901 return KERN_INVALID_VALUE
;
904 le
= &ledger
->l_entries
[entry
];
907 debit
= le
->le_debit
;
908 credit
= le
->le_credit
;
910 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
911 assert(le
->le_debit
== 0);
912 if (!OSCompareAndSwap64(credit
, 0, &le
->le_credit
)) {
915 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_credit
, 0));
916 } else if (credit
> debit
) {
917 if (!OSCompareAndSwap64(debit
, credit
, &le
->le_debit
)) {
920 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_debit
, le
->le_credit
));
921 } else if (credit
< debit
) {
922 if (!OSCompareAndSwap64(credit
, debit
, &le
->le_credit
)) {
925 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_credit
, le
->le_debit
));
932 ledger_get_limit(ledger_t ledger
, int entry
, ledger_amount_t
*limit
)
934 struct ledger_entry
*le
;
936 if (!ENTRY_VALID(ledger
, entry
)) {
937 return KERN_INVALID_VALUE
;
940 le
= &ledger
->l_entries
[entry
];
941 *limit
= le
->le_limit
;
943 lprintf(("ledger_get_limit: %lld\n", *limit
));
949 * Adjust the limit of a limited resource. This does not affect the
950 * current balance, so the change doesn't affect the thread until the
953 * warn_level: If non-zero, causes the callback to be invoked when
954 * the balance exceeds this level. Specified as a percentage [of the limit].
957 ledger_set_limit(ledger_t ledger
, int entry
, ledger_amount_t limit
,
958 uint8_t warn_level_percentage
)
960 struct ledger_entry
*le
;
962 if (!ENTRY_VALID(ledger
, entry
)) {
963 return KERN_INVALID_VALUE
;
966 lprintf(("ledger_set_limit: %lld\n", limit
));
967 le
= &ledger
->l_entries
[entry
];
969 if (limit
== LEDGER_LIMIT_INFINITY
) {
971 * Caller wishes to disable the limit. This will implicitly
972 * disable automatic refill, as refills implicitly depend
975 ledger_disable_refill(ledger
, entry
);
978 le
->le_limit
= limit
;
979 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
980 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
981 le
->_le
.le_refill
.le_last_refill
= 0;
983 flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
984 flag_clear(&le
->le_flags
, LF_WARNED
);
985 ledger_limit_entry_wakeup(le
);
987 if (warn_level_percentage
!= 0) {
988 assert(warn_level_percentage
<= 100);
989 assert(limit
> 0); /* no negative limit support for warnings */
990 assert(limit
!= LEDGER_LIMIT_INFINITY
); /* warn % without limit makes no sense */
991 le
->le_warn_percent
= warn_level_percentage
* (1u << 16) / 100;
993 le
->le_warn_percent
= LEDGER_PERCENT_NONE
;
999 #if CONFIG_LEDGER_INTERVAL_MAX
1001 ledger_get_interval_max(ledger_t ledger
, int entry
,
1002 ledger_amount_t
*max_interval_balance
, int reset
)
1004 struct ledger_entry
*le
;
1005 le
= &ledger
->l_entries
[entry
];
1007 if (!ENTRY_VALID(ledger
, entry
) || !(le
->le_flags
& LF_TRACKING_MAX
)) {
1008 return KERN_INVALID_VALUE
;
1011 *max_interval_balance
= le
->_le
._le_max
.le_interval_max
;
1012 lprintf(("ledger_get_interval_max: %lld%s\n", *max_interval_balance
,
1013 (reset
) ? " --> 0" : ""));
1016 le
->_le
._le_max
.le_interval_max
= 0;
1019 return KERN_SUCCESS
;
1021 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
1024 ledger_get_lifetime_max(ledger_t ledger
, int entry
,
1025 ledger_amount_t
*max_lifetime_balance
)
1027 struct ledger_entry
*le
;
1028 le
= &ledger
->l_entries
[entry
];
1030 if (!ENTRY_VALID(ledger
, entry
) || !(le
->le_flags
& LF_TRACKING_MAX
)) {
1031 return KERN_INVALID_VALUE
;
1034 *max_lifetime_balance
= le
->_le
._le_max
.le_lifetime_max
;
1035 lprintf(("ledger_get_lifetime_max: %lld\n", *max_lifetime_balance
));
1037 return KERN_SUCCESS
;
1041 * Enable tracking of periodic maximums for this ledger entry.
1044 ledger_track_maximum(ledger_template_t
template, int entry
,
1045 __unused
int period_in_secs
)
1047 template_lock(template);
1049 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1050 template_unlock(template);
1051 return KERN_INVALID_VALUE
;
1054 /* Refill is incompatible with max tracking. */
1055 if (template->lt_entries
[entry
].et_flags
& LF_REFILL_SCHEDULED
) {
1056 return KERN_INVALID_VALUE
;
1059 template->lt_entries
[entry
].et_flags
|= LF_TRACKING_MAX
;
1060 template_unlock(template);
1062 return KERN_SUCCESS
;
1066 ledger_panic_on_negative(ledger_template_t
template, int entry
)
1068 template_lock(template);
1070 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1071 template_unlock(template);
1072 return KERN_INVALID_VALUE
;
1075 template->lt_entries
[entry
].et_flags
|= LF_PANIC_ON_NEGATIVE
;
1077 template_unlock(template);
1079 return KERN_SUCCESS
;
1083 ledger_track_credit_only(ledger_template_t
template, int entry
)
1085 template_lock(template);
1087 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1088 template_unlock(template);
1089 return KERN_INVALID_VALUE
;
1092 template->lt_entries
[entry
].et_flags
|= LF_TRACK_CREDIT_ONLY
;
1094 template_unlock(template);
1096 return KERN_SUCCESS
;
1100 * Add a callback to be executed when the resource goes into deficit.
1103 ledger_set_callback(ledger_template_t
template, int entry
,
1104 ledger_callback_t func
, const void *param0
, const void *param1
)
1106 struct entry_template
*et
;
1107 struct ledger_callback
*old_cb
, *new_cb
;
1109 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1110 return KERN_INVALID_VALUE
;
1114 new_cb
= (struct ledger_callback
*)kalloc(sizeof(*new_cb
));
1115 new_cb
->lc_func
= func
;
1116 new_cb
->lc_param0
= param0
;
1117 new_cb
->lc_param1
= param1
;
1122 template_lock(template);
1123 et
= &template->lt_entries
[entry
];
1124 old_cb
= et
->et_callback
;
1125 et
->et_callback
= new_cb
;
1126 template_unlock(template);
1128 kfree(old_cb
, sizeof(*old_cb
));
1131 return KERN_SUCCESS
;
1135 * Disable callback notification for a specific ledger entry.
1137 * Otherwise, if using a ledger template which specified a
1138 * callback function (ledger_set_callback()), it will be invoked when
1139 * the resource goes into deficit.
1142 ledger_disable_callback(ledger_t ledger
, int entry
)
1144 if (!ENTRY_VALID(ledger
, entry
)) {
1145 return KERN_INVALID_VALUE
;
1149 * le_warn_percent is used to indicate *if* this ledger has a warning configured,
1150 * in addition to what that warning level is set to.
1151 * This means a side-effect of ledger_disable_callback() is that the
1152 * warning level is forgotten.
1154 ledger
->l_entries
[entry
].le_warn_percent
= LEDGER_PERCENT_NONE
;
1155 flag_clear(&ledger
->l_entries
[entry
].le_flags
, LEDGER_ACTION_CALLBACK
);
1156 return KERN_SUCCESS
;
1160 * Enable callback notification for a specific ledger entry.
1162 * This is only needed if ledger_disable_callback() has previously
1163 * been invoked against an entry; there must already be a callback
1167 ledger_enable_callback(ledger_t ledger
, int entry
)
1169 if (!ENTRY_VALID(ledger
, entry
)) {
1170 return KERN_INVALID_VALUE
;
1173 assert(entry_get_callback(ledger
, entry
) != NULL
);
1175 flag_set(&ledger
->l_entries
[entry
].le_flags
, LEDGER_ACTION_CALLBACK
);
1176 return KERN_SUCCESS
;
1180 * Query the automatic refill period for this ledger entry.
1182 * A period of 0 means this entry has none configured.
1185 ledger_get_period(ledger_t ledger
, int entry
, uint64_t *period
)
1187 struct ledger_entry
*le
;
1189 if (!ENTRY_VALID(ledger
, entry
)) {
1190 return KERN_INVALID_VALUE
;
1193 le
= &ledger
->l_entries
[entry
];
1194 *period
= abstime_to_nsecs(le
->_le
.le_refill
.le_refill_period
);
1195 lprintf(("ledger_get_period: %llx\n", *period
));
1196 return KERN_SUCCESS
;
1200 * Adjust the automatic refill period.
1203 ledger_set_period(ledger_t ledger
, int entry
, uint64_t period
)
1205 struct ledger_entry
*le
;
1207 lprintf(("ledger_set_period: %llx\n", period
));
1208 if (!ENTRY_VALID(ledger
, entry
)) {
1209 return KERN_INVALID_VALUE
;
1212 le
= &ledger
->l_entries
[entry
];
1215 * A refill period refills the ledger in multiples of the limit,
1216 * so if you haven't set one yet, you need a lesson on ledgers.
1218 assert(le
->le_limit
!= LEDGER_LIMIT_INFINITY
);
1220 if (le
->le_flags
& LF_TRACKING_MAX
) {
1222 * Refill is incompatible with rolling max tracking.
1224 return KERN_INVALID_VALUE
;
1227 le
->_le
.le_refill
.le_refill_period
= nsecs_to_abstime(period
);
1230 * Set the 'starting time' for the next refill to now. Since
1231 * we're resetting the balance to zero here, we consider this
1232 * moment the starting time for accumulating a balance that
1233 * counts towards the limit.
1235 le
->_le
.le_refill
.le_last_refill
= mach_absolute_time();
1236 ledger_zero_balance(ledger
, entry
);
1238 flag_set(&le
->le_flags
, LF_REFILL_SCHEDULED
);
1240 return KERN_SUCCESS
;
1244 * Disable automatic refill.
1247 ledger_disable_refill(ledger_t ledger
, int entry
)
1249 struct ledger_entry
*le
;
1251 if (!ENTRY_VALID(ledger
, entry
)) {
1252 return KERN_INVALID_VALUE
;
1255 le
= &ledger
->l_entries
[entry
];
1257 flag_clear(&le
->le_flags
, LF_REFILL_SCHEDULED
);
1259 return KERN_SUCCESS
;
1263 ledger_get_actions(ledger_t ledger
, int entry
, int *actions
)
1265 if (!ENTRY_VALID(ledger
, entry
)) {
1266 return KERN_INVALID_VALUE
;
1269 *actions
= ledger
->l_entries
[entry
].le_flags
& LEDGER_ACTION_MASK
;
1270 lprintf(("ledger_get_actions: %#x\n", *actions
));
1271 return KERN_SUCCESS
;
1275 ledger_set_action(ledger_t ledger
, int entry
, int action
)
1277 lprintf(("ledger_set_action: %#x\n", action
));
1278 if (!ENTRY_VALID(ledger
, entry
)) {
1279 return KERN_INVALID_VALUE
;
1282 flag_set(&ledger
->l_entries
[entry
].le_flags
, action
);
1283 return KERN_SUCCESS
;
1287 ledger_debit_thread(thread_t thread
, ledger_t ledger
, int entry
, ledger_amount_t amount
)
1289 struct ledger_entry
*le
;
1290 ledger_amount_t old
, new;
1292 if (!ENTRY_VALID(ledger
, entry
) || (amount
< 0)) {
1293 return KERN_INVALID_ARGUMENT
;
1297 return KERN_SUCCESS
;
1300 le
= &ledger
->l_entries
[entry
];
1302 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
1303 assert(le
->le_debit
== 0);
1304 old
= OSAddAtomic64(-amount
, &le
->le_credit
);
1307 old
= OSAddAtomic64(amount
, &le
->le_debit
);
1310 lprintf(("%p Debit %lld->%lld\n", thread
, old
, new));
1313 ledger_entry_check_new_balance(thread
, ledger
, entry
, le
);
1316 return KERN_SUCCESS
;
1320 ledger_debit(ledger_t ledger
, int entry
, ledger_amount_t amount
)
1322 return ledger_debit_thread(current_thread(), ledger
, entry
, amount
);
1326 ledger_debit_nocheck(ledger_t ledger
, int entry
, ledger_amount_t amount
)
1328 return ledger_debit_thread(NULL
, ledger
, entry
, amount
);
1332 ledger_ast(thread_t thread
)
1334 struct ledger
*l
= thread
->t_ledger
;
1336 struct ledger
*coalition_ledger
;
1340 uint8_t task_percentage
;
1341 uint64_t task_interval
;
1344 task_t task
= thread
->task
;
1346 lprintf(("Ledger AST for %p\n", thread
));
1348 ASSERT(task
!= NULL
);
1349 ASSERT(thread
== current_thread());
1353 * Take a self-consistent snapshot of the CPU usage monitor parameters. The task
1354 * can change them at any point (with the task locked).
1357 task_flags
= task
->rusage_cpu_flags
;
1358 task_percentage
= task
->rusage_cpu_perthr_percentage
;
1359 task_interval
= task
->rusage_cpu_perthr_interval
;
1363 * Make sure this thread is up to date with regards to any task-wide per-thread
1364 * CPU limit, but only if it doesn't have a thread-private blocking CPU limit.
1366 if (((task_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) != 0) &&
1367 ((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0)) {
1372 thread_get_cpulimit(&action
, &percentage
, &interval
);
1375 * If the thread's CPU limits no longer match the task's, or the
1376 * task has a limit but the thread doesn't, update the limit.
1378 if (((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0) ||
1379 (interval
!= task_interval
) || (percentage
!= task_percentage
)) {
1380 thread_set_cpulimit(THREAD_CPULIMIT_EXCEPTION
, task_percentage
, task_interval
);
1381 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) != 0);
1383 } else if (((task_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) &&
1384 (thread
->options
& TH_OPT_PROC_CPULIMIT
)) {
1385 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
1388 * Task no longer has a per-thread CPU limit; remove this thread's
1389 * corresponding CPU limit.
1391 thread_set_cpulimit(THREAD_CPULIMIT_DISABLE
, 0, 0);
1392 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
1396 * If the task or thread is being terminated, let's just get on with it
1398 if ((l
== NULL
) || !task
->active
|| task
->halting
|| !thread
->active
) {
1403 * Examine all entries in deficit to see which might be eligble for
1404 * an automatic refill, which require callbacks to be issued, and
1405 * which require blocking.
1408 now
= mach_absolute_time();
1411 * Note that thread->t_threadledger may have been changed by the
1412 * thread_set_cpulimit() call above - so don't examine it until afterwards.
1414 thl
= thread
->t_threadledger
;
1415 if (LEDGER_VALID(thl
)) {
1416 block
|= ledger_check_needblock(thl
, now
);
1418 block
|= ledger_check_needblock(l
, now
);
1420 coalition_ledger
= coalition_ledger_get_from_task(task
);
1421 if (LEDGER_VALID(coalition_ledger
)) {
1422 block
|= ledger_check_needblock(coalition_ledger
, now
);
1424 ledger_dereference(coalition_ledger
);
1426 * If we are supposed to block on the availability of one or more
1427 * resources, find the first entry in deficit for which we should wait.
1428 * Schedule a refill if necessary and then sleep until the resource
1429 * becomes available.
1432 if (LEDGER_VALID(thl
)) {
1433 ret
= ledger_perform_blocking(thl
);
1434 if (ret
!= KERN_SUCCESS
) {
1438 ret
= ledger_perform_blocking(l
);
1439 if (ret
!= KERN_SUCCESS
) {
1446 ledger_check_needblock(ledger_t l
, uint64_t now
)
1449 uint32_t flags
, block
= 0;
1450 struct ledger_entry
*le
;
1451 struct ledger_callback
*lc
;
1454 for (i
= 0; i
< l
->l_size
; i
++) {
1455 le
= &l
->l_entries
[i
];
1457 lc
= entry_get_callback(l
, i
);
1459 if (limit_exceeded(le
) == FALSE
) {
1460 if (le
->le_flags
& LEDGER_ACTION_CALLBACK
) {
1462 * If needed, invoke the callback as a warning.
1463 * This needs to happen both when the balance rises above
1464 * the warning level, and also when it dips back below it.
1468 * See comments for matching logic in ledger_check_new_balance().
1470 if (warn_level_exceeded(le
)) {
1471 flags
= flag_set(&le
->le_flags
, LF_WARNED
);
1472 if ((flags
& LF_WARNED
) == 0) {
1473 lc
->lc_func(LEDGER_WARNING_ROSE_ABOVE
, lc
->lc_param0
, lc
->lc_param1
);
1476 flags
= flag_clear(&le
->le_flags
, LF_WARNED
);
1477 if (flags
& LF_WARNED
) {
1478 lc
->lc_func(LEDGER_WARNING_DIPPED_BELOW
, lc
->lc_param0
, lc
->lc_param1
);
1486 /* We're over the limit, so refill if we are eligible and past due. */
1487 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
1488 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
1490 if ((le
->_le
.le_refill
.le_last_refill
+ le
->_le
.le_refill
.le_refill_period
) <= now
) {
1491 ledger_refill(now
, l
, i
);
1492 if (limit_exceeded(le
) == FALSE
) {
1498 if (le
->le_flags
& LEDGER_ACTION_BLOCK
) {
1501 if ((le
->le_flags
& LEDGER_ACTION_CALLBACK
) == 0) {
1506 * If the LEDGER_ACTION_CALLBACK flag is on, we expect there to
1507 * be a registered callback.
1510 flags
= flag_set(&le
->le_flags
, LF_CALLED_BACK
);
1511 /* Callback has already been called */
1512 if (flags
& LF_CALLED_BACK
) {
1515 lc
->lc_func(FALSE
, lc
->lc_param0
, lc
->lc_param1
);
1521 /* return KERN_SUCCESS to continue, KERN_FAILURE to restart */
1522 static kern_return_t
1523 ledger_perform_blocking(ledger_t l
)
1527 struct ledger_entry
*le
;
1529 for (i
= 0; i
< l
->l_size
; i
++) {
1530 le
= &l
->l_entries
[i
];
1531 if ((!limit_exceeded(le
)) ||
1532 ((le
->le_flags
& LEDGER_ACTION_BLOCK
) == 0)) {
1536 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
1538 /* Prepare to sleep until the resource is refilled */
1539 ret
= assert_wait_deadline(le
, THREAD_INTERRUPTIBLE
,
1540 le
->_le
.le_refill
.le_last_refill
+ le
->_le
.le_refill
.le_refill_period
);
1541 if (ret
!= THREAD_WAITING
) {
1542 return KERN_SUCCESS
;
1545 /* Mark that somebody is waiting on this entry */
1546 flag_set(&le
->le_flags
, LF_WAKE_NEEDED
);
1548 ret
= thread_block_reason(THREAD_CONTINUE_NULL
, NULL
,
1550 if (ret
!= THREAD_AWAKENED
) {
1551 return KERN_SUCCESS
;
1555 * The world may have changed while we were asleep.
1556 * Some other resource we need may have gone into
1557 * deficit. Or maybe we're supposed to die now.
1558 * Go back to the top and reevaluate.
1560 return KERN_FAILURE
;
1562 return KERN_SUCCESS
;
1567 ledger_get_entries(ledger_t ledger
, int entry
, ledger_amount_t
*credit
,
1568 ledger_amount_t
*debit
)
1570 struct ledger_entry
*le
;
1572 if (!ENTRY_VALID(ledger
, entry
)) {
1573 return KERN_INVALID_ARGUMENT
;
1576 le
= &ledger
->l_entries
[entry
];
1578 *credit
= le
->le_credit
;
1579 *debit
= le
->le_debit
;
1581 return KERN_SUCCESS
;
1585 ledger_reset_callback_state(ledger_t ledger
, int entry
)
1587 struct ledger_entry
*le
;
1589 if (!ENTRY_VALID(ledger
, entry
)) {
1590 return KERN_INVALID_ARGUMENT
;
1593 le
= &ledger
->l_entries
[entry
];
1595 flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
1597 return KERN_SUCCESS
;
1601 ledger_disable_panic_on_negative(ledger_t ledger
, int entry
)
1603 struct ledger_entry
*le
;
1605 if (!ENTRY_VALID(ledger
, entry
)) {
1606 return KERN_INVALID_ARGUMENT
;
1609 le
= &ledger
->l_entries
[entry
];
1611 flag_clear(&le
->le_flags
, LF_PANIC_ON_NEGATIVE
);
1613 return KERN_SUCCESS
;
1617 ledger_get_panic_on_negative(ledger_t ledger
, int entry
, int *panic_on_negative
)
1619 struct ledger_entry
*le
;
1621 if (!ENTRY_VALID(ledger
, entry
)) {
1622 return KERN_INVALID_ARGUMENT
;
1625 le
= &ledger
->l_entries
[entry
];
1627 if (le
->le_flags
& LF_PANIC_ON_NEGATIVE
) {
1628 *panic_on_negative
= TRUE
;
1630 *panic_on_negative
= FALSE
;
1633 return KERN_SUCCESS
;
1637 ledger_get_balance(ledger_t ledger
, int entry
, ledger_amount_t
*balance
)
1639 struct ledger_entry
*le
;
1641 if (!ENTRY_VALID(ledger
, entry
)) {
1642 return KERN_INVALID_ARGUMENT
;
1645 le
= &ledger
->l_entries
[entry
];
1647 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
1648 assert(le
->le_debit
== 0);
1650 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
1653 *balance
= le
->le_credit
- le
->le_debit
;
1655 return KERN_SUCCESS
;
1659 ledger_template_info(void **buf
, int *len
)
1661 struct ledger_template_info
*lti
;
1662 struct entry_template
*et
;
1667 * Since all tasks share a ledger template, we'll just use the
1668 * caller's as the source.
1670 l
= current_task()->ledger
;
1671 if ((*len
< 0) || (l
== NULL
)) {
1675 if (*len
> l
->l_size
) {
1678 lti
= kalloc((*len
) * sizeof(struct ledger_template_info
));
1684 template_lock(l
->l_template
);
1685 et
= l
->l_template
->lt_entries
;
1687 for (i
= 0; i
< *len
; i
++) {
1688 memset(lti
, 0, sizeof(*lti
));
1689 strlcpy(lti
->lti_name
, et
->et_key
, LEDGER_NAME_MAX
);
1690 strlcpy(lti
->lti_group
, et
->et_group
, LEDGER_NAME_MAX
);
1691 strlcpy(lti
->lti_units
, et
->et_units
, LEDGER_NAME_MAX
);
1695 template_unlock(l
->l_template
);
1701 ledger_fill_entry_info(struct ledger_entry
*le
,
1702 struct ledger_entry_info
*lei
,
1706 assert(lei
!= NULL
);
1708 memset(lei
, 0, sizeof(*lei
));
1710 lei
->lei_limit
= le
->le_limit
;
1711 lei
->lei_credit
= le
->le_credit
;
1712 lei
->lei_debit
= le
->le_debit
;
1713 lei
->lei_balance
= lei
->lei_credit
- lei
->lei_debit
;
1714 lei
->lei_refill_period
= (le
->le_flags
& LF_REFILL_SCHEDULED
) ?
1715 abstime_to_nsecs(le
->_le
.le_refill
.le_refill_period
) : 0;
1716 lei
->lei_last_refill
= abstime_to_nsecs(now
- le
->_le
.le_refill
.le_last_refill
);
1720 ledger_get_task_entry_info_multiple(task_t task
, void **buf
, int *len
)
1722 struct ledger_entry_info
*lei
;
1723 struct ledger_entry
*le
;
1724 uint64_t now
= mach_absolute_time();
1728 if ((*len
< 0) || ((l
= task
->ledger
) == NULL
)) {
1732 if (*len
> l
->l_size
) {
1735 lei
= kalloc((*len
) * sizeof(struct ledger_entry_info
));
1743 for (i
= 0; i
< *len
; i
++) {
1744 ledger_fill_entry_info(le
, lei
, now
);
1753 ledger_get_entry_info(ledger_t ledger
,
1755 struct ledger_entry_info
*lei
)
1757 uint64_t now
= mach_absolute_time();
1759 assert(ledger
!= NULL
);
1760 assert(lei
!= NULL
);
1762 if (entry
>= 0 && entry
< ledger
->l_size
) {
1763 struct ledger_entry
*le
= &ledger
->l_entries
[entry
];
1764 ledger_fill_entry_info(le
, lei
, now
);
1769 ledger_info(task_t task
, struct ledger_info
*info
)
1773 if ((l
= task
->ledger
) == NULL
) {
1777 memset(info
, 0, sizeof(*info
));
1779 strlcpy(info
->li_name
, l
->l_template
->lt_name
, LEDGER_NAME_MAX
);
1780 info
->li_id
= l
->l_id
;
1781 info
->li_entries
= l
->l_size
;
1787 ledger_limit(task_t task
, struct ledger_limit_args
*args
)
1793 if ((l
= task
->ledger
) == NULL
) {
1797 idx
= ledger_key_lookup(l
->l_template
, args
->lla_name
);
1798 if ((idx
< 0) || (idx
>= l
->l_size
)) {
1803 * XXX - this doesn't really seem like the right place to have
1804 * a context-sensitive conversion of userspace units into kernel
1805 * units. For now I'll handwave and say that the ledger() system
1806 * call isn't meant for civilians to use - they should be using
1807 * the process policy interfaces.
1809 if (idx
== task_ledgers
.cpu_time
) {
1812 if (args
->lla_refill_period
) {
1814 * If a refill is scheduled, then the limit is
1815 * specified as a percentage of one CPU. The
1816 * syscall specifies the refill period in terms of
1817 * milliseconds, so we need to convert to nsecs.
1819 args
->lla_refill_period
*= 1000000;
1820 nsecs
= args
->lla_limit
*
1821 (args
->lla_refill_period
/ 100);
1822 lprintf(("CPU limited to %lld nsecs per second\n",
1826 * If no refill is scheduled, then this is a
1827 * fixed amount of CPU time (in nsecs) that can
1830 nsecs
= args
->lla_limit
;
1831 lprintf(("CPU limited to %lld nsecs\n", nsecs
));
1833 limit
= nsecs_to_abstime(nsecs
);
1835 limit
= args
->lla_limit
;
1836 lprintf(("%s limited to %lld\n", args
->lla_name
, limit
));
1839 if (args
->lla_refill_period
> 0) {
1840 ledger_set_period(l
, idx
, args
->lla_refill_period
);
1843 ledger_set_limit(l
, idx
, limit
);
1844 flag_set(&l
->l_entries
[idx
].le_flags
, LEDGER_ACTION_BLOCK
);