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_level
= LEDGER_LIMIT_INFINITY
;
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_level
!= LEDGER_LIMIT_INFINITY
) && (balance
> le
->le_warn_level
)) {
531 * Determine whether an entry has exceeded its limit.
534 limit_exceeded(struct ledger_entry
*le
)
536 ledger_amount_t balance
;
538 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
539 assert(le
->le_debit
== 0);
541 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
544 balance
= le
->le_credit
- le
->le_debit
;
545 if ((le
->le_limit
<= 0) && (balance
< le
->le_limit
)) {
549 if ((le
->le_limit
> 0) && (balance
> le
->le_limit
)) {
555 static inline struct ledger_callback
*
556 entry_get_callback(ledger_t ledger
, int entry
)
558 struct ledger_callback
*callback
;
561 TEMPLATE_INUSE(s
, ledger
->l_template
);
562 callback
= ledger
->l_template
->lt_entries
[entry
].et_callback
;
563 TEMPLATE_IDLE(s
, ledger
->l_template
);
569 * If the ledger value is positive, wake up anybody waiting on it.
572 ledger_limit_entry_wakeup(struct ledger_entry
*le
)
576 if (!limit_exceeded(le
)) {
577 flags
= flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
579 while (le
->le_flags
& LF_WAKE_NEEDED
) {
580 flag_clear(&le
->le_flags
, LF_WAKE_NEEDED
);
581 thread_wakeup((event_t
)le
);
587 * Refill the coffers.
590 ledger_refill(uint64_t now
, ledger_t ledger
, int entry
)
592 uint64_t elapsed
, period
, periods
;
593 struct ledger_entry
*le
;
594 ledger_amount_t balance
, due
;
596 assert(entry
>= 0 && entry
< ledger
->l_size
);
598 le
= &ledger
->l_entries
[entry
];
600 assert(le
->le_limit
!= LEDGER_LIMIT_INFINITY
);
602 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
603 assert(le
->le_debit
== 0);
608 * If another thread is handling the refill already, we're not
611 if (flag_set(&le
->le_flags
, LF_REFILL_INPROGRESS
) & LF_REFILL_INPROGRESS
) {
616 * If the timestamp we're about to use to refill is older than the
617 * last refill, then someone else has already refilled this ledger
618 * and there's nothing for us to do here.
620 if (now
<= le
->_le
.le_refill
.le_last_refill
) {
621 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
626 * See how many refill periods have passed since we last
629 period
= le
->_le
.le_refill
.le_refill_period
;
630 elapsed
= now
- le
->_le
.le_refill
.le_last_refill
;
631 if ((period
== 0) || (elapsed
< period
)) {
632 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
637 * Optimize for the most common case of only one or two
641 while ((periods
< 2) && (elapsed
> 0)) {
647 * OK, it's been a long time. Do a divide to figure out
651 periods
= (now
- le
->_le
.le_refill
.le_last_refill
) / period
;
654 balance
= le
->le_credit
- le
->le_debit
;
655 due
= periods
* le
->le_limit
;
657 if (balance
- due
< 0) {
661 assertf(due
>= 0, "now=%llu, ledger=%p, entry=%d, balance=%lld, due=%lld", now
, ledger
, entry
, balance
, due
);
663 OSAddAtomic64(due
, &le
->le_debit
);
665 assert(le
->le_debit
>= 0);
668 * If we've completely refilled the pool, set the refill time to now.
669 * Otherwise set it to the time at which it last should have been
672 if (balance
== due
) {
673 le
->_le
.le_refill
.le_last_refill
= now
;
675 le
->_le
.le_refill
.le_last_refill
+= (le
->_le
.le_refill
.le_refill_period
* periods
);
678 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
680 lprintf(("Refill %lld %lld->%lld\n", periods
, balance
, balance
- due
));
681 if (!limit_exceeded(le
)) {
682 ledger_limit_entry_wakeup(le
);
687 ledger_entry_check_new_balance(thread_t thread
, ledger_t ledger
,
688 int entry
, struct ledger_entry
*le
)
690 if (le
->le_flags
& LF_TRACKING_MAX
) {
691 ledger_amount_t balance
= le
->le_credit
- le
->le_debit
;
693 if (balance
> le
->_le
._le_max
.le_lifetime_max
) {
694 le
->_le
._le_max
.le_lifetime_max
= balance
;
697 #if CONFIG_LEDGER_INTERVAL_MAX
698 if (balance
> le
->_le
._le_max
.le_interval_max
) {
699 le
->_le
._le_max
.le_interval_max
= balance
;
701 #endif /* LEDGER_CONFIG_INTERVAL_MAX */
704 /* Check to see whether we're due a refill */
705 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
706 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
708 uint64_t now
= mach_absolute_time();
709 if ((now
- le
->_le
.le_refill
.le_last_refill
) > le
->_le
.le_refill
.le_refill_period
) {
710 ledger_refill(now
, ledger
, entry
);
714 if (limit_exceeded(le
)) {
716 * We've exceeded the limit for this entry. There
717 * are several possible ways to handle it. We can block,
718 * we can execute a callback, or we can ignore it. In
719 * either of the first two cases, we want to set the AST
720 * flag so we can take the appropriate action just before
721 * leaving the kernel. The one caveat is that if we have
722 * already called the callback, we don't want to do it
723 * again until it gets rearmed.
725 if ((le
->le_flags
& LEDGER_ACTION_BLOCK
) ||
726 (!(le
->le_flags
& LF_CALLED_BACK
) &&
727 entry_get_callback(ledger
, entry
))) {
728 act_set_astledger_async(thread
);
732 * The balance on the account is below the limit.
734 * If there are any threads blocked on this entry, now would
735 * be a good time to wake them up.
737 if (le
->le_flags
& LF_WAKE_NEEDED
) {
738 ledger_limit_entry_wakeup(le
);
741 if (le
->le_flags
& LEDGER_ACTION_CALLBACK
) {
743 * Client has requested that a callback be invoked whenever
744 * the ledger's balance crosses into or out of the warning
747 if (warn_level_exceeded(le
)) {
749 * This ledger's balance is above the warning level.
751 if ((le
->le_flags
& LF_WARNED
) == 0) {
753 * If we are above the warning level and
754 * have not yet invoked the callback,
755 * set the AST so it can be done before returning
758 act_set_astledger_async(thread
);
762 * This ledger's balance is below the warning level.
764 if (le
->le_flags
& LF_WARNED
) {
766 * If we are below the warning level and
767 * the LF_WARNED flag is still set, we need
768 * to invoke the callback to let the client
769 * know the ledger balance is now back below
772 act_set_astledger_async(thread
);
778 if ((le
->le_flags
& LF_PANIC_ON_NEGATIVE
) &&
779 (le
->le_credit
< le
->le_debit
)) {
780 panic("ledger_entry_check_new_balance(%p,%d): negative ledger %p credit:%lld debit:%lld balance:%lld\n",
784 le
->le_credit
- le
->le_debit
);
789 ledger_check_new_balance(thread_t thread
, ledger_t ledger
, int entry
)
791 struct ledger_entry
*le
;
792 assert(entry
> 0 && entry
<= ledger
->l_size
);
793 le
= &ledger
->l_entries
[entry
];
794 ledger_entry_check_new_balance(thread
, ledger
, entry
, le
);
798 * Add value to an entry in a ledger for a specific thread.
801 ledger_credit_thread(thread_t thread
, ledger_t ledger
, int entry
, ledger_amount_t amount
)
803 ledger_amount_t old
, new;
804 struct ledger_entry
*le
;
806 if (!ENTRY_VALID(ledger
, entry
) || (amount
< 0)) {
807 return KERN_INVALID_VALUE
;
814 le
= &ledger
->l_entries
[entry
];
816 old
= OSAddAtomic64(amount
, &le
->le_credit
);
818 lprintf(("%p Credit %lld->%lld\n", thread
, old
, new));
821 ledger_entry_check_new_balance(thread
, ledger
, entry
, le
);
828 * Add value to an entry in a ledger.
831 ledger_credit(ledger_t ledger
, int entry
, ledger_amount_t amount
)
833 return ledger_credit_thread(current_thread(), ledger
, entry
, amount
);
837 * Add value to an entry in a ledger; do not check balance after update.
840 ledger_credit_nocheck(ledger_t ledger
, int entry
, ledger_amount_t amount
)
842 return ledger_credit_thread(NULL
, ledger
, entry
, amount
);
845 /* Add all of one ledger's values into another.
846 * They must have been created from the same template.
847 * This is not done atomically. Another thread (if not otherwise synchronized)
848 * may see bogus values when comparing one entry to another.
849 * As each entry's credit & debit are modified one at a time, the warning/limit
850 * may spuriously trip, or spuriously fail to trip, or another thread (if not
851 * otherwise synchronized) may see a bogus balance.
854 ledger_rollup(ledger_t to_ledger
, ledger_t from_ledger
)
858 assert(to_ledger
->l_template
->lt_cnt
== from_ledger
->l_template
->lt_cnt
);
860 for (i
= 0; i
< to_ledger
->l_size
; i
++) {
861 ledger_rollup_entry(to_ledger
, from_ledger
, i
);
867 /* Add one ledger entry value to another.
868 * They must have been created from the same template.
869 * Since the credit and debit values are added one
870 * at a time, other thread might read the a bogus value.
873 ledger_rollup_entry(ledger_t to_ledger
, ledger_t from_ledger
, int entry
)
875 struct ledger_entry
*from_le
, *to_le
;
877 assert(to_ledger
->l_template
->lt_cnt
== from_ledger
->l_template
->lt_cnt
);
878 if (ENTRY_VALID(from_ledger
, entry
) && ENTRY_VALID(to_ledger
, entry
)) {
879 from_le
= &from_ledger
->l_entries
[entry
];
880 to_le
= &to_ledger
->l_entries
[entry
];
881 OSAddAtomic64(from_le
->le_credit
, &to_le
->le_credit
);
882 OSAddAtomic64(from_le
->le_debit
, &to_le
->le_debit
);
889 * Zero the balance of a ledger by adding to its credit or debit, whichever is smaller.
890 * Note that some clients of ledgers (notably, task wakeup statistics) require that
891 * le_credit only ever increase as a function of ledger_credit().
894 ledger_zero_balance(ledger_t ledger
, int entry
)
896 struct ledger_entry
*le
;
897 ledger_amount_t debit
, credit
;
899 if (!ENTRY_VALID(ledger
, entry
)) {
900 return KERN_INVALID_VALUE
;
903 le
= &ledger
->l_entries
[entry
];
906 debit
= le
->le_debit
;
907 credit
= le
->le_credit
;
909 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
910 assert(le
->le_debit
== 0);
911 if (!OSCompareAndSwap64(credit
, 0, &le
->le_credit
)) {
914 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_credit
, 0));
915 } else if (credit
> debit
) {
916 if (!OSCompareAndSwap64(debit
, credit
, &le
->le_debit
)) {
919 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_debit
, le
->le_credit
));
920 } else if (credit
< debit
) {
921 if (!OSCompareAndSwap64(credit
, debit
, &le
->le_credit
)) {
924 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_credit
, le
->le_debit
));
931 ledger_get_limit(ledger_t ledger
, int entry
, ledger_amount_t
*limit
)
933 struct ledger_entry
*le
;
935 if (!ENTRY_VALID(ledger
, entry
)) {
936 return KERN_INVALID_VALUE
;
939 le
= &ledger
->l_entries
[entry
];
940 *limit
= le
->le_limit
;
942 lprintf(("ledger_get_limit: %lld\n", *limit
));
948 * Adjust the limit of a limited resource. This does not affect the
949 * current balance, so the change doesn't affect the thread until the
952 * warn_level: If non-zero, causes the callback to be invoked when
953 * the balance exceeds this level. Specified as a percentage [of the limit].
956 ledger_set_limit(ledger_t ledger
, int entry
, ledger_amount_t limit
,
957 uint8_t warn_level_percentage
)
959 struct ledger_entry
*le
;
961 if (!ENTRY_VALID(ledger
, entry
)) {
962 return KERN_INVALID_VALUE
;
965 lprintf(("ledger_set_limit: %lld\n", limit
));
966 le
= &ledger
->l_entries
[entry
];
968 if (limit
== LEDGER_LIMIT_INFINITY
) {
970 * Caller wishes to disable the limit. This will implicitly
971 * disable automatic refill, as refills implicitly depend
974 ledger_disable_refill(ledger
, entry
);
977 le
->le_limit
= limit
;
978 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
979 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
980 le
->_le
.le_refill
.le_last_refill
= 0;
982 flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
983 flag_clear(&le
->le_flags
, LF_WARNED
);
984 ledger_limit_entry_wakeup(le
);
986 if (warn_level_percentage
!= 0) {
987 assert(warn_level_percentage
<= 100);
988 assert(limit
> 0); /* no negative limit support for warnings */
989 assert(limit
!= LEDGER_LIMIT_INFINITY
); /* warn % without limit makes no sense */
990 le
->le_warn_level
= (le
->le_limit
* warn_level_percentage
) / 100;
992 le
->le_warn_level
= LEDGER_LIMIT_INFINITY
;
998 #if CONFIG_LEDGER_INTERVAL_MAX
1000 ledger_get_interval_max(ledger_t ledger
, int entry
,
1001 ledger_amount_t
*max_interval_balance
, int reset
)
1003 struct ledger_entry
*le
;
1004 le
= &ledger
->l_entries
[entry
];
1006 if (!ENTRY_VALID(ledger
, entry
) || !(le
->le_flags
& LF_TRACKING_MAX
)) {
1007 return KERN_INVALID_VALUE
;
1010 *max_interval_balance
= le
->_le
._le_max
.le_interval_max
;
1011 lprintf(("ledger_get_interval_max: %lld%s\n", *max_interval_balance
,
1012 (reset
) ? " --> 0" : ""));
1015 le
->_le
._le_max
.le_interval_max
= 0;
1018 return KERN_SUCCESS
;
1020 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
1023 ledger_get_lifetime_max(ledger_t ledger
, int entry
,
1024 ledger_amount_t
*max_lifetime_balance
)
1026 struct ledger_entry
*le
;
1027 le
= &ledger
->l_entries
[entry
];
1029 if (!ENTRY_VALID(ledger
, entry
) || !(le
->le_flags
& LF_TRACKING_MAX
)) {
1030 return KERN_INVALID_VALUE
;
1033 *max_lifetime_balance
= le
->_le
._le_max
.le_lifetime_max
;
1034 lprintf(("ledger_get_lifetime_max: %lld\n", *max_lifetime_balance
));
1036 return KERN_SUCCESS
;
1040 * Enable tracking of periodic maximums for this ledger entry.
1043 ledger_track_maximum(ledger_template_t
template, int entry
,
1044 __unused
int period_in_secs
)
1046 template_lock(template);
1048 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1049 template_unlock(template);
1050 return KERN_INVALID_VALUE
;
1053 /* Refill is incompatible with max tracking. */
1054 if (template->lt_entries
[entry
].et_flags
& LF_REFILL_SCHEDULED
) {
1055 return KERN_INVALID_VALUE
;
1058 template->lt_entries
[entry
].et_flags
|= LF_TRACKING_MAX
;
1059 template_unlock(template);
1061 return KERN_SUCCESS
;
1065 ledger_panic_on_negative(ledger_template_t
template, int entry
)
1067 template_lock(template);
1069 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1070 template_unlock(template);
1071 return KERN_INVALID_VALUE
;
1074 template->lt_entries
[entry
].et_flags
|= LF_PANIC_ON_NEGATIVE
;
1076 template_unlock(template);
1078 return KERN_SUCCESS
;
1082 ledger_track_credit_only(ledger_template_t
template, int entry
)
1084 template_lock(template);
1086 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1087 template_unlock(template);
1088 return KERN_INVALID_VALUE
;
1091 template->lt_entries
[entry
].et_flags
|= LF_TRACK_CREDIT_ONLY
;
1093 template_unlock(template);
1095 return KERN_SUCCESS
;
1099 * Add a callback to be executed when the resource goes into deficit.
1102 ledger_set_callback(ledger_template_t
template, int entry
,
1103 ledger_callback_t func
, const void *param0
, const void *param1
)
1105 struct entry_template
*et
;
1106 struct ledger_callback
*old_cb
, *new_cb
;
1108 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1109 return KERN_INVALID_VALUE
;
1113 new_cb
= (struct ledger_callback
*)kalloc(sizeof(*new_cb
));
1114 new_cb
->lc_func
= func
;
1115 new_cb
->lc_param0
= param0
;
1116 new_cb
->lc_param1
= param1
;
1121 template_lock(template);
1122 et
= &template->lt_entries
[entry
];
1123 old_cb
= et
->et_callback
;
1124 et
->et_callback
= new_cb
;
1125 template_unlock(template);
1127 kfree(old_cb
, sizeof(*old_cb
));
1130 return KERN_SUCCESS
;
1134 * Disable callback notification for a specific ledger entry.
1136 * Otherwise, if using a ledger template which specified a
1137 * callback function (ledger_set_callback()), it will be invoked when
1138 * the resource goes into deficit.
1141 ledger_disable_callback(ledger_t ledger
, int entry
)
1143 if (!ENTRY_VALID(ledger
, entry
)) {
1144 return KERN_INVALID_VALUE
;
1148 * le_warn_level is used to indicate *if* this ledger has a warning configured,
1149 * in addition to what that warning level is set to.
1150 * This means a side-effect of ledger_disable_callback() is that the
1151 * warning level is forgotten.
1153 ledger
->l_entries
[entry
].le_warn_level
= LEDGER_LIMIT_INFINITY
;
1154 flag_clear(&ledger
->l_entries
[entry
].le_flags
, LEDGER_ACTION_CALLBACK
);
1155 return KERN_SUCCESS
;
1159 * Enable callback notification for a specific ledger entry.
1161 * This is only needed if ledger_disable_callback() has previously
1162 * been invoked against an entry; there must already be a callback
1166 ledger_enable_callback(ledger_t ledger
, int entry
)
1168 if (!ENTRY_VALID(ledger
, entry
)) {
1169 return KERN_INVALID_VALUE
;
1172 assert(entry_get_callback(ledger
, entry
) != NULL
);
1174 flag_set(&ledger
->l_entries
[entry
].le_flags
, LEDGER_ACTION_CALLBACK
);
1175 return KERN_SUCCESS
;
1179 * Query the automatic refill period for this ledger entry.
1181 * A period of 0 means this entry has none configured.
1184 ledger_get_period(ledger_t ledger
, int entry
, uint64_t *period
)
1186 struct ledger_entry
*le
;
1188 if (!ENTRY_VALID(ledger
, entry
)) {
1189 return KERN_INVALID_VALUE
;
1192 le
= &ledger
->l_entries
[entry
];
1193 *period
= abstime_to_nsecs(le
->_le
.le_refill
.le_refill_period
);
1194 lprintf(("ledger_get_period: %llx\n", *period
));
1195 return KERN_SUCCESS
;
1199 * Adjust the automatic refill period.
1202 ledger_set_period(ledger_t ledger
, int entry
, uint64_t period
)
1204 struct ledger_entry
*le
;
1206 lprintf(("ledger_set_period: %llx\n", period
));
1207 if (!ENTRY_VALID(ledger
, entry
)) {
1208 return KERN_INVALID_VALUE
;
1211 le
= &ledger
->l_entries
[entry
];
1214 * A refill period refills the ledger in multiples of the limit,
1215 * so if you haven't set one yet, you need a lesson on ledgers.
1217 assert(le
->le_limit
!= LEDGER_LIMIT_INFINITY
);
1219 if (le
->le_flags
& LF_TRACKING_MAX
) {
1221 * Refill is incompatible with rolling max tracking.
1223 return KERN_INVALID_VALUE
;
1226 le
->_le
.le_refill
.le_refill_period
= nsecs_to_abstime(period
);
1229 * Set the 'starting time' for the next refill to now. Since
1230 * we're resetting the balance to zero here, we consider this
1231 * moment the starting time for accumulating a balance that
1232 * counts towards the limit.
1234 le
->_le
.le_refill
.le_last_refill
= mach_absolute_time();
1235 ledger_zero_balance(ledger
, entry
);
1237 flag_set(&le
->le_flags
, LF_REFILL_SCHEDULED
);
1239 return KERN_SUCCESS
;
1243 * Disable automatic refill.
1246 ledger_disable_refill(ledger_t ledger
, int entry
)
1248 struct ledger_entry
*le
;
1250 if (!ENTRY_VALID(ledger
, entry
)) {
1251 return KERN_INVALID_VALUE
;
1254 le
= &ledger
->l_entries
[entry
];
1256 flag_clear(&le
->le_flags
, LF_REFILL_SCHEDULED
);
1258 return KERN_SUCCESS
;
1262 ledger_get_actions(ledger_t ledger
, int entry
, int *actions
)
1264 if (!ENTRY_VALID(ledger
, entry
)) {
1265 return KERN_INVALID_VALUE
;
1268 *actions
= ledger
->l_entries
[entry
].le_flags
& LEDGER_ACTION_MASK
;
1269 lprintf(("ledger_get_actions: %#x\n", *actions
));
1270 return KERN_SUCCESS
;
1274 ledger_set_action(ledger_t ledger
, int entry
, int action
)
1276 lprintf(("ledger_set_action: %#x\n", action
));
1277 if (!ENTRY_VALID(ledger
, entry
)) {
1278 return KERN_INVALID_VALUE
;
1281 flag_set(&ledger
->l_entries
[entry
].le_flags
, action
);
1282 return KERN_SUCCESS
;
1286 ledger_debit_thread(thread_t thread
, ledger_t ledger
, int entry
, ledger_amount_t amount
)
1288 struct ledger_entry
*le
;
1289 ledger_amount_t old
, new;
1291 if (!ENTRY_VALID(ledger
, entry
) || (amount
< 0)) {
1292 return KERN_INVALID_ARGUMENT
;
1296 return KERN_SUCCESS
;
1299 le
= &ledger
->l_entries
[entry
];
1301 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
1302 assert(le
->le_debit
== 0);
1303 old
= OSAddAtomic64(-amount
, &le
->le_credit
);
1306 old
= OSAddAtomic64(amount
, &le
->le_debit
);
1309 lprintf(("%p Debit %lld->%lld\n", thread
, old
, new));
1312 ledger_entry_check_new_balance(thread
, ledger
, entry
, le
);
1315 return KERN_SUCCESS
;
1319 ledger_debit(ledger_t ledger
, int entry
, ledger_amount_t amount
)
1321 return ledger_debit_thread(current_thread(), ledger
, entry
, amount
);
1325 ledger_debit_nocheck(ledger_t ledger
, int entry
, ledger_amount_t amount
)
1327 return ledger_debit_thread(NULL
, ledger
, entry
, amount
);
1331 ledger_ast(thread_t thread
)
1333 struct ledger
*l
= thread
->t_ledger
;
1335 struct ledger
*coalition_ledger
;
1339 uint8_t task_percentage
;
1340 uint64_t task_interval
;
1343 task_t task
= thread
->task
;
1345 lprintf(("Ledger AST for %p\n", thread
));
1347 ASSERT(task
!= NULL
);
1348 ASSERT(thread
== current_thread());
1352 * Take a self-consistent snapshot of the CPU usage monitor parameters. The task
1353 * can change them at any point (with the task locked).
1356 task_flags
= task
->rusage_cpu_flags
;
1357 task_percentage
= task
->rusage_cpu_perthr_percentage
;
1358 task_interval
= task
->rusage_cpu_perthr_interval
;
1362 * Make sure this thread is up to date with regards to any task-wide per-thread
1363 * CPU limit, but only if it doesn't have a thread-private blocking CPU limit.
1365 if (((task_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) != 0) &&
1366 ((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0)) {
1371 thread_get_cpulimit(&action
, &percentage
, &interval
);
1374 * If the thread's CPU limits no longer match the task's, or the
1375 * task has a limit but the thread doesn't, update the limit.
1377 if (((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0) ||
1378 (interval
!= task_interval
) || (percentage
!= task_percentage
)) {
1379 thread_set_cpulimit(THREAD_CPULIMIT_EXCEPTION
, task_percentage
, task_interval
);
1380 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) != 0);
1382 } else if (((task_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) &&
1383 (thread
->options
& TH_OPT_PROC_CPULIMIT
)) {
1384 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
1387 * Task no longer has a per-thread CPU limit; remove this thread's
1388 * corresponding CPU limit.
1390 thread_set_cpulimit(THREAD_CPULIMIT_DISABLE
, 0, 0);
1391 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
1395 * If the task or thread is being terminated, let's just get on with it
1397 if ((l
== NULL
) || !task
->active
|| task
->halting
|| !thread
->active
) {
1402 * Examine all entries in deficit to see which might be eligble for
1403 * an automatic refill, which require callbacks to be issued, and
1404 * which require blocking.
1407 now
= mach_absolute_time();
1410 * Note that thread->t_threadledger may have been changed by the
1411 * thread_set_cpulimit() call above - so don't examine it until afterwards.
1413 thl
= thread
->t_threadledger
;
1414 if (LEDGER_VALID(thl
)) {
1415 block
|= ledger_check_needblock(thl
, now
);
1417 block
|= ledger_check_needblock(l
, now
);
1419 coalition_ledger
= coalition_ledger_get_from_task(task
);
1420 if (LEDGER_VALID(coalition_ledger
)) {
1421 block
|= ledger_check_needblock(coalition_ledger
, now
);
1423 ledger_dereference(coalition_ledger
);
1425 * If we are supposed to block on the availability of one or more
1426 * resources, find the first entry in deficit for which we should wait.
1427 * Schedule a refill if necessary and then sleep until the resource
1428 * becomes available.
1431 if (LEDGER_VALID(thl
)) {
1432 ret
= ledger_perform_blocking(thl
);
1433 if (ret
!= KERN_SUCCESS
) {
1437 ret
= ledger_perform_blocking(l
);
1438 if (ret
!= KERN_SUCCESS
) {
1445 ledger_check_needblock(ledger_t l
, uint64_t now
)
1448 uint32_t flags
, block
= 0;
1449 struct ledger_entry
*le
;
1450 struct ledger_callback
*lc
;
1453 for (i
= 0; i
< l
->l_size
; i
++) {
1454 le
= &l
->l_entries
[i
];
1456 lc
= entry_get_callback(l
, i
);
1458 if (limit_exceeded(le
) == FALSE
) {
1459 if (le
->le_flags
& LEDGER_ACTION_CALLBACK
) {
1461 * If needed, invoke the callback as a warning.
1462 * This needs to happen both when the balance rises above
1463 * the warning level, and also when it dips back below it.
1467 * See comments for matching logic in ledger_check_new_balance().
1469 if (warn_level_exceeded(le
)) {
1470 flags
= flag_set(&le
->le_flags
, LF_WARNED
);
1471 if ((flags
& LF_WARNED
) == 0) {
1472 lc
->lc_func(LEDGER_WARNING_ROSE_ABOVE
, lc
->lc_param0
, lc
->lc_param1
);
1475 flags
= flag_clear(&le
->le_flags
, LF_WARNED
);
1476 if (flags
& LF_WARNED
) {
1477 lc
->lc_func(LEDGER_WARNING_DIPPED_BELOW
, lc
->lc_param0
, lc
->lc_param1
);
1485 /* We're over the limit, so refill if we are eligible and past due. */
1486 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
1487 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
1489 if ((le
->_le
.le_refill
.le_last_refill
+ le
->_le
.le_refill
.le_refill_period
) <= now
) {
1490 ledger_refill(now
, l
, i
);
1491 if (limit_exceeded(le
) == FALSE
) {
1497 if (le
->le_flags
& LEDGER_ACTION_BLOCK
) {
1500 if ((le
->le_flags
& LEDGER_ACTION_CALLBACK
) == 0) {
1505 * If the LEDGER_ACTION_CALLBACK flag is on, we expect there to
1506 * be a registered callback.
1509 flags
= flag_set(&le
->le_flags
, LF_CALLED_BACK
);
1510 /* Callback has already been called */
1511 if (flags
& LF_CALLED_BACK
) {
1514 lc
->lc_func(FALSE
, lc
->lc_param0
, lc
->lc_param1
);
1520 /* return KERN_SUCCESS to continue, KERN_FAILURE to restart */
1521 static kern_return_t
1522 ledger_perform_blocking(ledger_t l
)
1526 struct ledger_entry
*le
;
1528 for (i
= 0; i
< l
->l_size
; i
++) {
1529 le
= &l
->l_entries
[i
];
1530 if ((!limit_exceeded(le
)) ||
1531 ((le
->le_flags
& LEDGER_ACTION_BLOCK
) == 0)) {
1535 assert(!(le
->le_flags
& LF_TRACKING_MAX
));
1537 /* Prepare to sleep until the resource is refilled */
1538 ret
= assert_wait_deadline(le
, THREAD_INTERRUPTIBLE
,
1539 le
->_le
.le_refill
.le_last_refill
+ le
->_le
.le_refill
.le_refill_period
);
1540 if (ret
!= THREAD_WAITING
) {
1541 return KERN_SUCCESS
;
1544 /* Mark that somebody is waiting on this entry */
1545 flag_set(&le
->le_flags
, LF_WAKE_NEEDED
);
1547 ret
= thread_block_reason(THREAD_CONTINUE_NULL
, NULL
,
1549 if (ret
!= THREAD_AWAKENED
) {
1550 return KERN_SUCCESS
;
1554 * The world may have changed while we were asleep.
1555 * Some other resource we need may have gone into
1556 * deficit. Or maybe we're supposed to die now.
1557 * Go back to the top and reevaluate.
1559 return KERN_FAILURE
;
1561 return KERN_SUCCESS
;
1566 ledger_get_entries(ledger_t ledger
, int entry
, ledger_amount_t
*credit
,
1567 ledger_amount_t
*debit
)
1569 struct ledger_entry
*le
;
1571 if (!ENTRY_VALID(ledger
, entry
)) {
1572 return KERN_INVALID_ARGUMENT
;
1575 le
= &ledger
->l_entries
[entry
];
1577 *credit
= le
->le_credit
;
1578 *debit
= le
->le_debit
;
1580 return KERN_SUCCESS
;
1584 ledger_reset_callback_state(ledger_t ledger
, int entry
)
1586 struct ledger_entry
*le
;
1588 if (!ENTRY_VALID(ledger
, entry
)) {
1589 return KERN_INVALID_ARGUMENT
;
1592 le
= &ledger
->l_entries
[entry
];
1594 flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
1596 return KERN_SUCCESS
;
1600 ledger_disable_panic_on_negative(ledger_t ledger
, int entry
)
1602 struct ledger_entry
*le
;
1604 if (!ENTRY_VALID(ledger
, entry
)) {
1605 return KERN_INVALID_ARGUMENT
;
1608 le
= &ledger
->l_entries
[entry
];
1610 flag_clear(&le
->le_flags
, LF_PANIC_ON_NEGATIVE
);
1612 return KERN_SUCCESS
;
1616 ledger_get_panic_on_negative(ledger_t ledger
, int entry
, int *panic_on_negative
)
1618 struct ledger_entry
*le
;
1620 if (!ENTRY_VALID(ledger
, entry
)) {
1621 return KERN_INVALID_ARGUMENT
;
1624 le
= &ledger
->l_entries
[entry
];
1626 if (le
->le_flags
& LF_PANIC_ON_NEGATIVE
) {
1627 *panic_on_negative
= TRUE
;
1629 *panic_on_negative
= FALSE
;
1632 return KERN_SUCCESS
;
1636 ledger_get_balance(ledger_t ledger
, int entry
, ledger_amount_t
*balance
)
1638 struct ledger_entry
*le
;
1640 if (!ENTRY_VALID(ledger
, entry
)) {
1641 return KERN_INVALID_ARGUMENT
;
1644 le
= &ledger
->l_entries
[entry
];
1646 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
1647 assert(le
->le_debit
== 0);
1649 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
1652 *balance
= le
->le_credit
- le
->le_debit
;
1654 return KERN_SUCCESS
;
1658 ledger_template_info(void **buf
, int *len
)
1660 struct ledger_template_info
*lti
;
1661 struct entry_template
*et
;
1666 * Since all tasks share a ledger template, we'll just use the
1667 * caller's as the source.
1669 l
= current_task()->ledger
;
1670 if ((*len
< 0) || (l
== NULL
)) {
1674 if (*len
> l
->l_size
) {
1677 lti
= kalloc((*len
) * sizeof(struct ledger_template_info
));
1683 template_lock(l
->l_template
);
1684 et
= l
->l_template
->lt_entries
;
1686 for (i
= 0; i
< *len
; i
++) {
1687 memset(lti
, 0, sizeof(*lti
));
1688 strlcpy(lti
->lti_name
, et
->et_key
, LEDGER_NAME_MAX
);
1689 strlcpy(lti
->lti_group
, et
->et_group
, LEDGER_NAME_MAX
);
1690 strlcpy(lti
->lti_units
, et
->et_units
, LEDGER_NAME_MAX
);
1694 template_unlock(l
->l_template
);
1700 ledger_fill_entry_info(struct ledger_entry
*le
,
1701 struct ledger_entry_info
*lei
,
1705 assert(lei
!= NULL
);
1707 memset(lei
, 0, sizeof(*lei
));
1709 lei
->lei_limit
= le
->le_limit
;
1710 lei
->lei_credit
= le
->le_credit
;
1711 lei
->lei_debit
= le
->le_debit
;
1712 lei
->lei_balance
= lei
->lei_credit
- lei
->lei_debit
;
1713 lei
->lei_refill_period
= (le
->le_flags
& LF_REFILL_SCHEDULED
) ?
1714 abstime_to_nsecs(le
->_le
.le_refill
.le_refill_period
) : 0;
1715 lei
->lei_last_refill
= abstime_to_nsecs(now
- le
->_le
.le_refill
.le_last_refill
);
1719 ledger_get_task_entry_info_multiple(task_t task
, void **buf
, int *len
)
1721 struct ledger_entry_info
*lei
;
1722 struct ledger_entry
*le
;
1723 uint64_t now
= mach_absolute_time();
1727 if ((*len
< 0) || ((l
= task
->ledger
) == NULL
)) {
1731 if (*len
> l
->l_size
) {
1734 lei
= kalloc((*len
) * sizeof(struct ledger_entry_info
));
1742 for (i
= 0; i
< *len
; i
++) {
1743 ledger_fill_entry_info(le
, lei
, now
);
1752 ledger_get_entry_info(ledger_t ledger
,
1754 struct ledger_entry_info
*lei
)
1756 uint64_t now
= mach_absolute_time();
1758 assert(ledger
!= NULL
);
1759 assert(lei
!= NULL
);
1761 if (entry
>= 0 && entry
< ledger
->l_size
) {
1762 struct ledger_entry
*le
= &ledger
->l_entries
[entry
];
1763 ledger_fill_entry_info(le
, lei
, now
);
1768 ledger_info(task_t task
, struct ledger_info
*info
)
1772 if ((l
= task
->ledger
) == NULL
) {
1776 memset(info
, 0, sizeof(*info
));
1778 strlcpy(info
->li_name
, l
->l_template
->lt_name
, LEDGER_NAME_MAX
);
1779 info
->li_id
= l
->l_id
;
1780 info
->li_entries
= l
->l_size
;
1786 ledger_limit(task_t task
, struct ledger_limit_args
*args
)
1792 if ((l
= task
->ledger
) == NULL
) {
1796 idx
= ledger_key_lookup(l
->l_template
, args
->lla_name
);
1797 if ((idx
< 0) || (idx
>= l
->l_size
)) {
1802 * XXX - this doesn't really seem like the right place to have
1803 * a context-sensitive conversion of userspace units into kernel
1804 * units. For now I'll handwave and say that the ledger() system
1805 * call isn't meant for civilians to use - they should be using
1806 * the process policy interfaces.
1808 if (idx
== task_ledgers
.cpu_time
) {
1811 if (args
->lla_refill_period
) {
1813 * If a refill is scheduled, then the limit is
1814 * specified as a percentage of one CPU. The
1815 * syscall specifies the refill period in terms of
1816 * milliseconds, so we need to convert to nsecs.
1818 args
->lla_refill_period
*= 1000000;
1819 nsecs
= args
->lla_limit
*
1820 (args
->lla_refill_period
/ 100);
1821 lprintf(("CPU limited to %lld nsecs per second\n",
1825 * If no refill is scheduled, then this is a
1826 * fixed amount of CPU time (in nsecs) that can
1829 nsecs
= args
->lla_limit
;
1830 lprintf(("CPU limited to %lld nsecs\n", nsecs
));
1832 limit
= nsecs_to_abstime(nsecs
);
1834 limit
= args
->lla_limit
;
1835 lprintf(("%s limited to %lld\n", args
->lla_name
, limit
));
1838 if (args
->lla_refill_period
> 0) {
1839 ledger_set_period(l
, idx
, args
->lla_refill_period
);
1842 ledger_set_limit(l
, idx
, limit
);
1843 flag_set(&l
->l_entries
[idx
].le_flags
, LEDGER_ACTION_BLOCK
);