2 * Copyright (c) 2010 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>
50 * Ledger entry flags. Bits in second nibble (masked by 0xF0) are used for
51 * ledger actions (LEDGER_ACTION_BLOCK, etc).
53 #define LF_ENTRY_ACTIVE 0x0001 /* entry is active if set */
54 #define LF_WAKE_NEEDED 0x0100 /* one or more threads are asleep */
55 #define LF_WAKE_INPROGRESS 0x0200 /* the wait queue is being processed */
56 #define LF_REFILL_SCHEDULED 0x0400 /* a refill timer has been set */
57 #define LF_REFILL_INPROGRESS 0x0800 /* the ledger is being refilled */
58 #define LF_CALLED_BACK 0x1000 /* callback was called for balance in deficit */
59 #define LF_WARNED 0x2000 /* callback was called for balance warning */
60 #define LF_TRACKING_MAX 0x4000 /* track max balance over user-specfied time */
61 #define LF_PANIC_ON_NEGATIVE 0x8000 /* panic if it goes negative */
62 #define LF_TRACK_CREDIT_ONLY 0x10000 /* only update "credit" */
64 /* Determine whether a ledger entry exists and has been initialized and active */
65 #define ENTRY_VALID(l, e) \
66 (((l) != NULL) && ((e) >= 0) && ((e) < (l)->l_size) && \
67 (((l)->l_entries[e].le_flags & LF_ENTRY_ACTIVE) == LF_ENTRY_ACTIVE))
69 #define ASSERT(a) assert(a)
74 #define lprintf(a) if (ledger_debug) { \
75 printf("%lld ", abstime_to_nsecs(mach_absolute_time() / 1000000)); \
82 struct ledger_callback
{
83 ledger_callback_t lc_func
;
84 const void *lc_param0
;
85 const void *lc_param1
;
88 struct entry_template
{
89 char et_key
[LEDGER_NAME_MAX
];
90 char et_group
[LEDGER_NAME_MAX
];
91 char et_units
[LEDGER_NAME_MAX
];
93 struct ledger_callback
*et_callback
;
96 lck_grp_t ledger_lck_grp
;
99 * Modifying the reference count, table size, or table contents requires
100 * holding the lt_lock. Modfying the table address requires both lt_lock
101 * and setting the inuse bit. This means that the lt_entries field can be
102 * safely dereferenced if you hold either the lock or the inuse bit. The
103 * inuse bit exists solely to allow us to swap in a new, larger entries
104 * table without requiring a full lock to be acquired on each lookup.
105 * Accordingly, the inuse bit should never be held for longer than it takes
106 * to extract a value from the table - i.e., 2 or 3 memory references.
108 struct ledger_template
{
113 volatile uint32_t lt_inuse
;
115 struct entry_template
*lt_entries
;
118 #define template_lock(template) lck_mtx_lock(&(template)->lt_lock)
119 #define template_unlock(template) lck_mtx_unlock(&(template)->lt_lock)
121 #define TEMPLATE_INUSE(s, t) { \
123 while (OSCompareAndSwap(0, 1, &((t)->lt_inuse))) \
127 #define TEMPLATE_IDLE(s, t) { \
133 * Use 2 "tocks" to track the rolling maximum balance of a ledger entry.
137 * The explicit alignment is to ensure that atomic operations don't panic
140 struct ledger_entry
{
141 volatile uint32_t le_flags
;
142 ledger_amount_t le_limit
;
143 ledger_amount_t le_warn_level
;
144 volatile ledger_amount_t le_credit
__attribute__((aligned(8)));
145 volatile ledger_amount_t le_debit
__attribute__((aligned(8)));
149 * XXX - the following two fields can go away if we move all of
150 * the refill logic into process policy
152 uint64_t le_refill_period
;
153 uint64_t le_last_refill
;
156 uint32_t le_max
; /* Lower 32-bits of observed max balance */
157 uint32_t le_time
; /* time when this peak was observed */
160 } __attribute__((aligned(8)));
166 struct ledger_template
*l_template
;
167 struct ledger_entry l_entries
[0] __attribute__((aligned(8)));
170 static int ledger_cnt
= 0;
171 /* ledger ast helper functions */
172 static uint32_t ledger_check_needblock(ledger_t l
, uint64_t now
);
173 static kern_return_t
ledger_perform_blocking(ledger_t l
);
174 static uint32_t flag_set(volatile uint32_t *flags
, uint32_t bit
);
175 static uint32_t flag_clear(volatile uint32_t *flags
, uint32_t bit
);
177 static void ledger_entry_check_new_balance(ledger_t ledger
, int entry
,
178 struct ledger_entry
*le
);
182 debug_callback(const void *p0
, __unused
const void *p1
)
184 printf("ledger: resource exhausted [%s] for task %p\n",
185 (const char *)p0
, p1
);
189 /************************************/
192 abstime_to_nsecs(uint64_t abstime
)
196 absolutetime_to_nanoseconds(abstime
, &nsecs
);
201 nsecs_to_abstime(uint64_t nsecs
)
205 nanoseconds_to_absolutetime(nsecs
, &abstime
);
212 lck_grp_init(&ledger_lck_grp
, "ledger", LCK_GRP_ATTR_NULL
);
216 ledger_template_create(const char *name
)
218 ledger_template_t
template;
220 template = (ledger_template_t
)kalloc(sizeof (*template));
221 if (template == NULL
)
224 template->lt_name
= name
;
225 template->lt_refs
= 1;
226 template->lt_cnt
= 0;
227 template->lt_table_size
= 1;
228 template->lt_inuse
= 0;
229 lck_mtx_init(&template->lt_lock
, &ledger_lck_grp
, LCK_ATTR_NULL
);
231 template->lt_entries
= (struct entry_template
*)
232 kalloc(sizeof (struct entry_template
) * template->lt_table_size
);
233 if (template->lt_entries
== NULL
) {
234 kfree(template, sizeof (*template));
242 ledger_template_dereference(ledger_template_t
template)
244 template_lock(template);
246 template_unlock(template);
248 if (template->lt_refs
== 0)
249 kfree(template, sizeof (*template));
253 * Add a new entry to the list of entries in a ledger template. There is
254 * currently no mechanism to remove an entry. Implementing such a mechanism
255 * would require us to maintain per-entry reference counts, which we would
256 * prefer to avoid if possible.
259 ledger_entry_add(ledger_template_t
template, const char *key
,
260 const char *group
, const char *units
)
263 struct entry_template
*et
;
265 if ((key
== NULL
) || (strlen(key
) >= LEDGER_NAME_MAX
))
268 template_lock(template);
270 /* If the table is full, attempt to double its size */
271 if (template->lt_cnt
== template->lt_table_size
) {
272 struct entry_template
*new_entries
, *old_entries
;
273 int old_cnt
, old_sz
, new_sz
= 0;
276 old_cnt
= template->lt_table_size
;
277 old_sz
= old_cnt
* (int)(sizeof(struct entry_template
));
278 /* double old_sz allocation, but check for overflow */
279 if (os_mul_overflow(old_sz
, 2, &new_sz
)) {
280 template_unlock(template);
283 new_entries
= kalloc(new_sz
);
284 if (new_entries
== NULL
) {
285 template_unlock(template);
288 memcpy(new_entries
, template->lt_entries
, old_sz
);
289 memset(((char *)new_entries
) + old_sz
, 0, old_sz
);
290 /* assume: if the sz didn't overflow, neither will the count */
291 template->lt_table_size
= old_cnt
* 2;
293 old_entries
= template->lt_entries
;
295 TEMPLATE_INUSE(s
, template);
296 template->lt_entries
= new_entries
;
297 TEMPLATE_IDLE(s
, template);
299 kfree(old_entries
, old_sz
);
302 et
= &template->lt_entries
[template->lt_cnt
];
303 strlcpy(et
->et_key
, key
, LEDGER_NAME_MAX
);
304 strlcpy(et
->et_group
, group
, LEDGER_NAME_MAX
);
305 strlcpy(et
->et_units
, units
, LEDGER_NAME_MAX
);
306 et
->et_flags
= LF_ENTRY_ACTIVE
;
307 et
->et_callback
= NULL
;
309 idx
= template->lt_cnt
++;
310 template_unlock(template);
317 ledger_entry_setactive(ledger_t ledger
, int entry
)
319 struct ledger_entry
*le
;
321 if ((ledger
== NULL
) || (entry
< 0) || (entry
>= ledger
->l_size
))
322 return (KERN_INVALID_ARGUMENT
);
324 le
= &ledger
->l_entries
[entry
];
325 if ((le
->le_flags
& LF_ENTRY_ACTIVE
) == 0) {
326 flag_set(&le
->le_flags
, LF_ENTRY_ACTIVE
);
328 return (KERN_SUCCESS
);
333 ledger_key_lookup(ledger_template_t
template, const char *key
)
337 template_lock(template);
338 for (idx
= 0; idx
< template->lt_cnt
; idx
++)
339 if (template->lt_entries
!= NULL
&&
340 (strcmp(key
, template->lt_entries
[idx
].et_key
) == 0))
343 if (idx
>= template->lt_cnt
)
345 template_unlock(template);
351 * Create a new ledger based on the specified template. As part of the
352 * ledger creation we need to allocate space for a table of ledger entries.
353 * The size of the table is based on the size of the template at the time
354 * the ledger is created. If additional entries are added to the template
355 * after the ledger is created, they will not be tracked in this ledger.
358 ledger_instantiate(ledger_template_t
template, int entry_type
)
364 template_lock(template);
366 cnt
= template->lt_cnt
;
367 template_unlock(template);
369 sz
= sizeof(*ledger
) + (cnt
* sizeof(struct ledger_entry
));
371 ledger
= (ledger_t
)kalloc(sz
);
372 if (ledger
== NULL
) {
373 ledger_template_dereference(template);
377 ledger
->l_template
= template;
378 ledger
->l_id
= ledger_cnt
++;
380 ledger
->l_size
= (int32_t)cnt
;
382 template_lock(template);
383 assert(ledger
->l_size
<= template->lt_cnt
);
384 for (i
= 0; i
< ledger
->l_size
; i
++) {
385 struct ledger_entry
*le
= &ledger
->l_entries
[i
];
386 struct entry_template
*et
= &template->lt_entries
[i
];
388 le
->le_flags
= et
->et_flags
;
389 /* make entry inactive by removing active bit */
390 if (entry_type
== LEDGER_CREATE_INACTIVE_ENTRIES
)
391 flag_clear(&le
->le_flags
, LF_ENTRY_ACTIVE
);
393 * If template has a callback, this entry is opted-in,
396 if (et
->et_callback
!= NULL
)
397 flag_set(&le
->le_flags
, LEDGER_ACTION_CALLBACK
);
400 le
->le_limit
= LEDGER_LIMIT_INFINITY
;
401 le
->le_warn_level
= LEDGER_LIMIT_INFINITY
;
402 le
->_le
.le_refill
.le_refill_period
= 0;
403 le
->_le
.le_refill
.le_last_refill
= 0;
405 template_unlock(template);
411 flag_set(volatile uint32_t *flags
, uint32_t bit
)
413 return (OSBitOrAtomic(bit
, flags
));
417 flag_clear(volatile uint32_t *flags
, uint32_t bit
)
419 return (OSBitAndAtomic(~bit
, flags
));
423 * Take a reference on a ledger
426 ledger_reference(ledger_t ledger
)
428 if (!LEDGER_VALID(ledger
))
429 return (KERN_INVALID_ARGUMENT
);
430 OSIncrementAtomic(&ledger
->l_refs
);
431 return (KERN_SUCCESS
);
435 ledger_reference_count(ledger_t ledger
)
437 if (!LEDGER_VALID(ledger
))
440 return (ledger
->l_refs
);
444 * Remove a reference on a ledger. If this is the last reference,
445 * deallocate the unused ledger.
448 ledger_dereference(ledger_t ledger
)
452 if (!LEDGER_VALID(ledger
))
453 return (KERN_INVALID_ARGUMENT
);
455 v
= OSDecrementAtomic(&ledger
->l_refs
);
458 /* Just released the last reference. Free it. */
461 sizeof(*ledger
) + ledger
->l_size
* sizeof(struct ledger_entry
));
464 return (KERN_SUCCESS
);
468 * Determine whether an entry has exceeded its warning level.
471 warn_level_exceeded(struct ledger_entry
*le
)
473 ledger_amount_t balance
;
475 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
476 assert(le
->le_debit
== 0);
478 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
482 * XXX - Currently, we only support warnings for ledgers which
483 * use positive limits.
485 balance
= le
->le_credit
- le
->le_debit
;
486 if ((le
->le_warn_level
!= LEDGER_LIMIT_INFINITY
) && (balance
> le
->le_warn_level
))
492 * Determine whether an entry has exceeded its limit.
495 limit_exceeded(struct ledger_entry
*le
)
497 ledger_amount_t balance
;
499 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
500 assert(le
->le_debit
== 0);
502 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
505 balance
= le
->le_credit
- le
->le_debit
;
506 if ((le
->le_limit
<= 0) && (balance
< le
->le_limit
))
509 if ((le
->le_limit
> 0) && (balance
> le
->le_limit
))
514 static inline struct ledger_callback
*
515 entry_get_callback(ledger_t ledger
, int entry
)
517 struct ledger_callback
*callback
;
520 TEMPLATE_INUSE(s
, ledger
->l_template
);
521 callback
= ledger
->l_template
->lt_entries
[entry
].et_callback
;
522 TEMPLATE_IDLE(s
, ledger
->l_template
);
528 * If the ledger value is positive, wake up anybody waiting on it.
531 ledger_limit_entry_wakeup(struct ledger_entry
*le
)
535 if (!limit_exceeded(le
)) {
536 flags
= flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
538 while (le
->le_flags
& LF_WAKE_NEEDED
) {
539 flag_clear(&le
->le_flags
, LF_WAKE_NEEDED
);
540 thread_wakeup((event_t
)le
);
546 * Refill the coffers.
549 ledger_refill(uint64_t now
, ledger_t ledger
, int entry
)
551 uint64_t elapsed
, period
, periods
;
552 struct ledger_entry
*le
;
553 ledger_amount_t balance
, due
;
555 assert(entry
>= 0 && entry
< ledger
->l_size
);
557 le
= &ledger
->l_entries
[entry
];
559 assert(le
->le_limit
!= LEDGER_LIMIT_INFINITY
);
561 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
562 assert(le
->le_debit
== 0);
567 * If another thread is handling the refill already, we're not
570 if (flag_set(&le
->le_flags
, LF_REFILL_INPROGRESS
) & LF_REFILL_INPROGRESS
) {
575 * If the timestamp we're about to use to refill is older than the
576 * last refill, then someone else has already refilled this ledger
577 * and there's nothing for us to do here.
579 if (now
<= le
->_le
.le_refill
.le_last_refill
) {
580 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
585 * See how many refill periods have passed since we last
588 period
= le
->_le
.le_refill
.le_refill_period
;
589 elapsed
= now
- le
->_le
.le_refill
.le_last_refill
;
590 if ((period
== 0) || (elapsed
< period
)) {
591 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
596 * Optimize for the most common case of only one or two
600 while ((periods
< 2) && (elapsed
> 0)) {
606 * OK, it's been a long time. Do a divide to figure out
610 periods
= (now
- le
->_le
.le_refill
.le_last_refill
) / period
;
612 balance
= le
->le_credit
- le
->le_debit
;
613 due
= periods
* le
->le_limit
;
615 if (balance
- due
< 0)
618 assertf(due
>= 0,"now=%llu, ledger=%p, entry=%d, balance=%lld, due=%lld", now
, ledger
, entry
, balance
, due
);
620 OSAddAtomic64(due
, &le
->le_debit
);
622 assert(le
->le_debit
>= 0);
625 * If we've completely refilled the pool, set the refill time to now.
626 * Otherwise set it to the time at which it last should have been
630 le
->_le
.le_refill
.le_last_refill
= now
;
632 le
->_le
.le_refill
.le_last_refill
+= (le
->_le
.le_refill
.le_refill_period
* periods
);
634 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
636 lprintf(("Refill %lld %lld->%lld\n", periods
, balance
, balance
- due
));
637 if (!limit_exceeded(le
))
638 ledger_limit_entry_wakeup(le
);
642 * In tenths of a second, the length of one lookback period (a "tock") for
643 * ledger rolling maximum calculations. The effective lookback window will be this times
646 * Use a tock length of 2.5 seconds to get a total lookback period of 5 seconds.
648 * XXX Could make this caller-definable, at the point that rolling max tracking
649 * is enabled for the entry.
654 * How many sched_tick's are there in one tock (one of our lookback periods)?
656 * X sched_ticks 2.5 sec N sched_ticks
657 * --------------- = ---------- * -------------
660 * where N sched_ticks/sec is calculated via 1 << SCHED_TICK_SHIFT (see sched_prim.h)
662 * This should give us 20 sched_tick's in one 2.5 second-long tock.
664 #define SCHED_TICKS_PER_TOCK ((TOCKLEN * (1 << SCHED_TICK_SHIFT)) / 10)
667 * Rolling max timestamps use their own unit (let's call this a "tock"). One tock is the
668 * length of one lookback period that we use for our rolling max calculation.
670 * Calculate the current time in tocks from sched_tick (which runs at a some
673 #define CURRENT_TOCKSTAMP() (sched_tick / SCHED_TICKS_PER_TOCK)
676 * Does the given tockstamp fall in either the current or the previous tocks?
678 #define TOCKSTAMP_IS_STALE(now, tock) ((((now) - (tock)) < NTOCKS) ? FALSE : TRUE)
681 ledger_entry_check_new_balance(ledger_t ledger
, int entry
, struct ledger_entry
*le
)
683 ledger_amount_t credit
, debit
;
685 if (le
->le_flags
& LF_TRACKING_MAX
) {
686 ledger_amount_t balance
= le
->le_credit
- le
->le_debit
;
687 uint32_t now
= CURRENT_TOCKSTAMP();
688 struct _le_peak
*p
= &le
->_le
.le_peaks
[now
% NTOCKS
];
690 if (!TOCKSTAMP_IS_STALE(now
, p
->le_time
) || (balance
> p
->le_max
)) {
692 * The current balance is greater than the previously
693 * observed peak for the current time block, *or* we
694 * haven't yet recorded a peak for the current time block --
695 * so this is our new peak.
697 * (We only track the lower 32-bits of a balance for rolling
700 p
->le_max
= (uint32_t)balance
;
705 /* Check to see whether we're due a refill */
706 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
707 uint64_t now
= mach_absolute_time();
708 if ((now
- le
->_le
.le_refill
.le_last_refill
) > le
->_le
.le_refill
.le_refill_period
)
709 ledger_refill(now
, ledger
, entry
);
712 if (limit_exceeded(le
)) {
714 * We've exceeded the limit for this entry. There
715 * are several possible ways to handle it. We can block,
716 * we can execute a callback, or we can ignore it. In
717 * either of the first two cases, we want to set the AST
718 * flag so we can take the appropriate action just before
719 * leaving the kernel. The one caveat is that if we have
720 * already called the callback, we don't want to do it
721 * again until it gets rearmed.
723 if ((le
->le_flags
& LEDGER_ACTION_BLOCK
) ||
724 (!(le
->le_flags
& LF_CALLED_BACK
) &&
725 entry_get_callback(ledger
, entry
))) {
726 set_astledger(current_thread());
730 * The balance on the account is below the limit.
732 * If there are any threads blocked on this entry, now would
733 * be a good time to wake them up.
735 if (le
->le_flags
& LF_WAKE_NEEDED
)
736 ledger_limit_entry_wakeup(le
);
738 if (le
->le_flags
& LEDGER_ACTION_CALLBACK
) {
740 * Client has requested that a callback be invoked whenever
741 * the ledger's balance crosses into or out of the warning
744 if (warn_level_exceeded(le
)) {
746 * This ledger's balance is above the warning level.
748 if ((le
->le_flags
& LF_WARNED
) == 0) {
750 * If we are above the warning level and
751 * have not yet invoked the callback,
752 * set the AST so it can be done before returning
755 set_astledger(current_thread());
759 * This ledger's balance is below the warning level.
761 if (le
->le_flags
& LF_WARNED
) {
763 * If we are below the warning level and
764 * the LF_WARNED flag is still set, we need
765 * to invoke the callback to let the client
766 * know the ledger balance is now back below
769 set_astledger(current_thread());
775 credit
= le
->le_credit
;
776 debit
= le
->le_debit
;
777 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/%lld debit:%lld/%lld balance:%lld/%lld\n",
782 credit
, le
->le_credit
,
784 credit
- debit
, le
->le_credit
- le
->le_debit
);
789 ledger_check_new_balance(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(ledger
, entry
, le
);
799 * Add value to an entry in a ledger.
802 ledger_credit(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
);
811 return (KERN_SUCCESS
);
813 le
= &ledger
->l_entries
[entry
];
815 old
= OSAddAtomic64(amount
, &le
->le_credit
);
817 lprintf(("%p Credit %lld->%lld\n", current_thread(), old
, new));
818 ledger_entry_check_new_balance(ledger
, entry
, le
);
820 return (KERN_SUCCESS
);
823 /* Add all of one ledger's values into another.
824 * They must have been created from the same template.
825 * This is not done atomically. Another thread (if not otherwise synchronized)
826 * may see bogus values when comparing one entry to another.
827 * As each entry's credit & debit are modified one at a time, the warning/limit
828 * may spuriously trip, or spuriously fail to trip, or another thread (if not
829 * otherwise synchronized) may see a bogus balance.
832 ledger_rollup(ledger_t to_ledger
, ledger_t from_ledger
)
836 assert(to_ledger
->l_template
== from_ledger
->l_template
);
838 for (i
= 0; i
< to_ledger
->l_size
; i
++) {
839 ledger_rollup_entry(to_ledger
, from_ledger
, i
);
842 return (KERN_SUCCESS
);
845 /* Add one ledger entry value to another.
846 * They must have been created from the same template.
847 * Since the credit and debit values are added one
848 * at a time, other thread might read the a bogus value.
851 ledger_rollup_entry(ledger_t to_ledger
, ledger_t from_ledger
, int entry
)
853 struct ledger_entry
*from_le
, *to_le
;
855 assert(to_ledger
->l_template
== from_ledger
->l_template
);
856 if (ENTRY_VALID(from_ledger
, entry
) && ENTRY_VALID(to_ledger
, entry
)) {
857 from_le
= &from_ledger
->l_entries
[entry
];
858 to_le
= &to_ledger
->l_entries
[entry
];
859 OSAddAtomic64(from_le
->le_credit
, &to_le
->le_credit
);
860 OSAddAtomic64(from_le
->le_debit
, &to_le
->le_debit
);
863 return (KERN_SUCCESS
);
867 * Zero the balance of a ledger by adding to its credit or debit, whichever is smaller.
868 * Note that some clients of ledgers (notably, task wakeup statistics) require that
869 * le_credit only ever increase as a function of ledger_credit().
872 ledger_zero_balance(ledger_t ledger
, int entry
)
874 struct ledger_entry
*le
;
875 ledger_amount_t debit
, credit
;
877 if (!ENTRY_VALID(ledger
, entry
))
878 return (KERN_INVALID_VALUE
);
880 le
= &ledger
->l_entries
[entry
];
883 debit
= le
->le_debit
;
884 credit
= le
->le_credit
;
886 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
887 assert(le
->le_debit
== 0);
888 if (!OSCompareAndSwap64(credit
, 0, &le
->le_credit
)) {
891 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_credit
, 0));
892 } else if (credit
> debit
) {
893 if (!OSCompareAndSwap64(debit
, credit
, &le
->le_debit
))
895 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_debit
, le
->le_credit
));
896 } else if (credit
< debit
) {
897 if (!OSCompareAndSwap64(credit
, debit
, &le
->le_credit
))
899 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_credit
, le
->le_debit
));
902 return (KERN_SUCCESS
);
906 ledger_get_limit(ledger_t ledger
, int entry
, ledger_amount_t
*limit
)
908 struct ledger_entry
*le
;
910 if (!ENTRY_VALID(ledger
, entry
))
911 return (KERN_INVALID_VALUE
);
913 le
= &ledger
->l_entries
[entry
];
914 *limit
= le
->le_limit
;
916 lprintf(("ledger_get_limit: %lld\n", *limit
));
918 return (KERN_SUCCESS
);
922 * Adjust the limit of a limited resource. This does not affect the
923 * current balance, so the change doesn't affect the thread until the
926 * warn_level: If non-zero, causes the callback to be invoked when
927 * the balance exceeds this level. Specified as a percentage [of the limit].
930 ledger_set_limit(ledger_t ledger
, int entry
, ledger_amount_t limit
,
931 uint8_t warn_level_percentage
)
933 struct ledger_entry
*le
;
935 if (!ENTRY_VALID(ledger
, entry
))
936 return (KERN_INVALID_VALUE
);
938 lprintf(("ledger_set_limit: %lld\n", limit
));
939 le
= &ledger
->l_entries
[entry
];
941 if (limit
== LEDGER_LIMIT_INFINITY
) {
943 * Caller wishes to disable the limit. This will implicitly
944 * disable automatic refill, as refills implicitly depend
947 ledger_disable_refill(ledger
, entry
);
950 le
->le_limit
= limit
;
951 le
->_le
.le_refill
.le_last_refill
= 0;
952 flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
953 flag_clear(&le
->le_flags
, LF_WARNED
);
954 ledger_limit_entry_wakeup(le
);
956 if (warn_level_percentage
!= 0) {
957 assert(warn_level_percentage
<= 100);
958 assert(limit
> 0); /* no negative limit support for warnings */
959 assert(limit
!= LEDGER_LIMIT_INFINITY
); /* warn % without limit makes no sense */
960 le
->le_warn_level
= (le
->le_limit
* warn_level_percentage
) / 100;
962 le
->le_warn_level
= LEDGER_LIMIT_INFINITY
;
965 return (KERN_SUCCESS
);
969 ledger_get_maximum(ledger_t ledger
, int entry
,
970 ledger_amount_t
*max_observed_balance
)
972 struct ledger_entry
*le
;
973 uint32_t now
= CURRENT_TOCKSTAMP();
976 le
= &ledger
->l_entries
[entry
];
978 if (!ENTRY_VALID(ledger
, entry
) || !(le
->le_flags
& LF_TRACKING_MAX
)) {
979 return (KERN_INVALID_VALUE
);
983 * Start with the current balance; if neither of the recorded peaks are
984 * within recent history, we use this.
986 *max_observed_balance
= le
->le_credit
- le
->le_debit
;
988 for (i
= 0; i
< NTOCKS
; i
++) {
989 if (!TOCKSTAMP_IS_STALE(now
, le
->_le
.le_peaks
[i
].le_time
) &&
990 (le
->_le
.le_peaks
[i
].le_max
> *max_observed_balance
)) {
992 * The peak for this time block isn't stale, and it
993 * is greater than the current balance -- so use it.
995 *max_observed_balance
= le
->_le
.le_peaks
[i
].le_max
;
999 lprintf(("ledger_get_maximum: %lld\n", *max_observed_balance
));
1001 return (KERN_SUCCESS
);
1005 * Enable tracking of periodic maximums for this ledger entry.
1008 ledger_track_maximum(ledger_template_t
template, int entry
,
1009 __unused
int period_in_secs
)
1011 template_lock(template);
1013 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1014 template_unlock(template);
1015 return (KERN_INVALID_VALUE
);
1018 template->lt_entries
[entry
].et_flags
|= LF_TRACKING_MAX
;
1019 template_unlock(template);
1021 return (KERN_SUCCESS
);
1025 ledger_panic_on_negative(ledger_template_t
template, int entry
)
1027 template_lock(template);
1029 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1030 template_unlock(template);
1031 return (KERN_INVALID_VALUE
);
1034 template->lt_entries
[entry
].et_flags
|= LF_PANIC_ON_NEGATIVE
;
1036 template_unlock(template);
1038 return (KERN_SUCCESS
);
1042 ledger_track_credit_only(ledger_template_t
template, int entry
)
1044 template_lock(template);
1046 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1047 template_unlock(template);
1048 return (KERN_INVALID_VALUE
);
1051 template->lt_entries
[entry
].et_flags
|= LF_TRACK_CREDIT_ONLY
;
1053 template_unlock(template);
1055 return (KERN_SUCCESS
);
1059 * Add a callback to be executed when the resource goes into deficit.
1062 ledger_set_callback(ledger_template_t
template, int entry
,
1063 ledger_callback_t func
, const void *param0
, const void *param1
)
1065 struct entry_template
*et
;
1066 struct ledger_callback
*old_cb
, *new_cb
;
1068 if ((entry
< 0) || (entry
>= template->lt_cnt
))
1069 return (KERN_INVALID_VALUE
);
1072 new_cb
= (struct ledger_callback
*)kalloc(sizeof (*new_cb
));
1073 new_cb
->lc_func
= func
;
1074 new_cb
->lc_param0
= param0
;
1075 new_cb
->lc_param1
= param1
;
1080 template_lock(template);
1081 et
= &template->lt_entries
[entry
];
1082 old_cb
= et
->et_callback
;
1083 et
->et_callback
= new_cb
;
1084 template_unlock(template);
1086 kfree(old_cb
, sizeof (*old_cb
));
1088 return (KERN_SUCCESS
);
1092 * Disable callback notification for a specific ledger entry.
1094 * Otherwise, if using a ledger template which specified a
1095 * callback function (ledger_set_callback()), it will be invoked when
1096 * the resource goes into deficit.
1099 ledger_disable_callback(ledger_t ledger
, int entry
)
1101 if (!ENTRY_VALID(ledger
, entry
))
1102 return (KERN_INVALID_VALUE
);
1105 * le_warn_level is used to indicate *if* this ledger has a warning configured,
1106 * in addition to what that warning level is set to.
1107 * This means a side-effect of ledger_disable_callback() is that the
1108 * warning level is forgotten.
1110 ledger
->l_entries
[entry
].le_warn_level
= LEDGER_LIMIT_INFINITY
;
1111 flag_clear(&ledger
->l_entries
[entry
].le_flags
, LEDGER_ACTION_CALLBACK
);
1112 return (KERN_SUCCESS
);
1116 * Enable callback notification for a specific ledger entry.
1118 * This is only needed if ledger_disable_callback() has previously
1119 * been invoked against an entry; there must already be a callback
1123 ledger_enable_callback(ledger_t ledger
, int entry
)
1125 if (!ENTRY_VALID(ledger
, entry
))
1126 return (KERN_INVALID_VALUE
);
1128 assert(entry_get_callback(ledger
, entry
) != NULL
);
1130 flag_set(&ledger
->l_entries
[entry
].le_flags
, LEDGER_ACTION_CALLBACK
);
1131 return (KERN_SUCCESS
);
1135 * Query the automatic refill period for this ledger entry.
1137 * A period of 0 means this entry has none configured.
1140 ledger_get_period(ledger_t ledger
, int entry
, uint64_t *period
)
1142 struct ledger_entry
*le
;
1144 if (!ENTRY_VALID(ledger
, entry
))
1145 return (KERN_INVALID_VALUE
);
1147 le
= &ledger
->l_entries
[entry
];
1148 *period
= abstime_to_nsecs(le
->_le
.le_refill
.le_refill_period
);
1149 lprintf(("ledger_get_period: %llx\n", *period
));
1150 return (KERN_SUCCESS
);
1154 * Adjust the automatic refill period.
1157 ledger_set_period(ledger_t ledger
, int entry
, uint64_t period
)
1159 struct ledger_entry
*le
;
1161 lprintf(("ledger_set_period: %llx\n", period
));
1162 if (!ENTRY_VALID(ledger
, entry
))
1163 return (KERN_INVALID_VALUE
);
1165 le
= &ledger
->l_entries
[entry
];
1168 * A refill period refills the ledger in multiples of the limit,
1169 * so if you haven't set one yet, you need a lesson on ledgers.
1171 assert(le
->le_limit
!= LEDGER_LIMIT_INFINITY
);
1173 if (le
->le_flags
& LF_TRACKING_MAX
) {
1175 * Refill is incompatible with rolling max tracking.
1177 return (KERN_INVALID_VALUE
);
1180 le
->_le
.le_refill
.le_refill_period
= nsecs_to_abstime(period
);
1183 * Set the 'starting time' for the next refill to now. Since
1184 * we're resetting the balance to zero here, we consider this
1185 * moment the starting time for accumulating a balance that
1186 * counts towards the limit.
1188 le
->_le
.le_refill
.le_last_refill
= mach_absolute_time();
1189 ledger_zero_balance(ledger
, entry
);
1191 flag_set(&le
->le_flags
, LF_REFILL_SCHEDULED
);
1193 return (KERN_SUCCESS
);
1197 * Disable automatic refill.
1200 ledger_disable_refill(ledger_t ledger
, int entry
)
1202 struct ledger_entry
*le
;
1204 if (!ENTRY_VALID(ledger
, entry
))
1205 return (KERN_INVALID_VALUE
);
1207 le
= &ledger
->l_entries
[entry
];
1209 flag_clear(&le
->le_flags
, LF_REFILL_SCHEDULED
);
1211 return (KERN_SUCCESS
);
1215 ledger_get_actions(ledger_t ledger
, int entry
, int *actions
)
1217 if (!ENTRY_VALID(ledger
, entry
))
1218 return (KERN_INVALID_VALUE
);
1220 *actions
= ledger
->l_entries
[entry
].le_flags
& LEDGER_ACTION_MASK
;
1221 lprintf(("ledger_get_actions: %#x\n", *actions
));
1222 return (KERN_SUCCESS
);
1226 ledger_set_action(ledger_t ledger
, int entry
, int action
)
1228 lprintf(("ledger_set_action: %#x\n", action
));
1229 if (!ENTRY_VALID(ledger
, entry
))
1230 return (KERN_INVALID_VALUE
);
1232 flag_set(&ledger
->l_entries
[entry
].le_flags
, action
);
1233 return (KERN_SUCCESS
);
1237 ledger_debit(ledger_t ledger
, int entry
, ledger_amount_t amount
)
1239 struct ledger_entry
*le
;
1240 ledger_amount_t old
, new;
1242 if (!ENTRY_VALID(ledger
, entry
) || (amount
< 0))
1243 return (KERN_INVALID_ARGUMENT
);
1246 return (KERN_SUCCESS
);
1248 le
= &ledger
->l_entries
[entry
];
1250 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
1251 assert(le
->le_debit
== 0);
1252 old
= OSAddAtomic64(-amount
, &le
->le_credit
);
1255 old
= OSAddAtomic64(amount
, &le
->le_debit
);
1258 lprintf(("%p Debit %lld->%lld\n", thread
, old
, new));
1260 ledger_entry_check_new_balance(ledger
, entry
, le
);
1261 return (KERN_SUCCESS
);
1266 ledger_ast(thread_t thread
)
1268 struct ledger
*l
= thread
->t_ledger
;
1273 uint8_t task_percentage
;
1274 uint64_t task_interval
;
1277 task_t task
= thread
->task
;
1279 lprintf(("Ledger AST for %p\n", thread
));
1281 ASSERT(task
!= NULL
);
1282 ASSERT(thread
== current_thread());
1286 * Take a self-consistent snapshot of the CPU usage monitor parameters. The task
1287 * can change them at any point (with the task locked).
1290 task_flags
= task
->rusage_cpu_flags
;
1291 task_percentage
= task
->rusage_cpu_perthr_percentage
;
1292 task_interval
= task
->rusage_cpu_perthr_interval
;
1296 * Make sure this thread is up to date with regards to any task-wide per-thread
1297 * CPU limit, but only if it doesn't have a thread-private blocking CPU limit.
1299 if (((task_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) != 0) &&
1300 ((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0)) {
1305 thread_get_cpulimit(&action
, &percentage
, &interval
);
1308 * If the thread's CPU limits no longer match the task's, or the
1309 * task has a limit but the thread doesn't, update the limit.
1311 if (((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0) ||
1312 (interval
!= task_interval
) || (percentage
!= task_percentage
)) {
1313 thread_set_cpulimit(THREAD_CPULIMIT_EXCEPTION
, task_percentage
, task_interval
);
1314 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) != 0);
1316 } else if (((task_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) &&
1317 (thread
->options
& TH_OPT_PROC_CPULIMIT
)) {
1318 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
1321 * Task no longer has a per-thread CPU limit; remove this thread's
1322 * corresponding CPU limit.
1324 thread_set_cpulimit(THREAD_CPULIMIT_DISABLE
, 0, 0);
1325 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
1329 * If the task or thread is being terminated, let's just get on with it
1331 if ((l
== NULL
) || !task
->active
|| task
->halting
|| !thread
->active
)
1335 * Examine all entries in deficit to see which might be eligble for
1336 * an automatic refill, which require callbacks to be issued, and
1337 * which require blocking.
1340 now
= mach_absolute_time();
1343 * Note that thread->t_threadledger may have been changed by the
1344 * thread_set_cpulimit() call above - so don't examine it until afterwards.
1346 thl
= thread
->t_threadledger
;
1347 if (LEDGER_VALID(thl
)) {
1348 block
|= ledger_check_needblock(thl
, now
);
1350 block
|= ledger_check_needblock(l
, now
);
1353 * If we are supposed to block on the availability of one or more
1354 * resources, find the first entry in deficit for which we should wait.
1355 * Schedule a refill if necessary and then sleep until the resource
1356 * becomes available.
1359 if (LEDGER_VALID(thl
)) {
1360 ret
= ledger_perform_blocking(thl
);
1361 if (ret
!= KERN_SUCCESS
)
1364 ret
= ledger_perform_blocking(l
);
1365 if (ret
!= KERN_SUCCESS
)
1371 ledger_check_needblock(ledger_t l
, uint64_t now
)
1374 uint32_t flags
, block
= 0;
1375 struct ledger_entry
*le
;
1376 struct ledger_callback
*lc
;
1379 for (i
= 0; i
< l
->l_size
; i
++) {
1380 le
= &l
->l_entries
[i
];
1382 lc
= entry_get_callback(l
, i
);
1384 if (limit_exceeded(le
) == FALSE
) {
1385 if (le
->le_flags
& LEDGER_ACTION_CALLBACK
) {
1387 * If needed, invoke the callback as a warning.
1388 * This needs to happen both when the balance rises above
1389 * the warning level, and also when it dips back below it.
1393 * See comments for matching logic in ledger_check_new_balance().
1395 if (warn_level_exceeded(le
)) {
1396 flags
= flag_set(&le
->le_flags
, LF_WARNED
);
1397 if ((flags
& LF_WARNED
) == 0) {
1398 lc
->lc_func(LEDGER_WARNING_ROSE_ABOVE
, lc
->lc_param0
, lc
->lc_param1
);
1401 flags
= flag_clear(&le
->le_flags
, LF_WARNED
);
1402 if (flags
& LF_WARNED
) {
1403 lc
->lc_func(LEDGER_WARNING_DIPPED_BELOW
, lc
->lc_param0
, lc
->lc_param1
);
1411 /* We're over the limit, so refill if we are eligible and past due. */
1412 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
1413 if ((le
->_le
.le_refill
.le_last_refill
+ le
->_le
.le_refill
.le_refill_period
) > now
) {
1414 ledger_refill(now
, l
, i
);
1415 if (limit_exceeded(le
) == FALSE
)
1420 if (le
->le_flags
& LEDGER_ACTION_BLOCK
)
1422 if ((le
->le_flags
& LEDGER_ACTION_CALLBACK
) == 0)
1426 * If the LEDGER_ACTION_CALLBACK flag is on, we expect there to
1427 * be a registered callback.
1430 flags
= flag_set(&le
->le_flags
, LF_CALLED_BACK
);
1431 /* Callback has already been called */
1432 if (flags
& LF_CALLED_BACK
)
1434 lc
->lc_func(FALSE
, lc
->lc_param0
, lc
->lc_param1
);
1440 /* return KERN_SUCCESS to continue, KERN_FAILURE to restart */
1441 static kern_return_t
1442 ledger_perform_blocking(ledger_t l
)
1446 struct ledger_entry
*le
;
1448 for (i
= 0; i
< l
->l_size
; i
++) {
1449 le
= &l
->l_entries
[i
];
1450 if ((!limit_exceeded(le
)) ||
1451 ((le
->le_flags
& LEDGER_ACTION_BLOCK
) == 0))
1454 /* Prepare to sleep until the resource is refilled */
1455 ret
= assert_wait_deadline(le
, TRUE
,
1456 le
->_le
.le_refill
.le_last_refill
+ le
->_le
.le_refill
.le_refill_period
);
1457 if (ret
!= THREAD_WAITING
)
1458 return(KERN_SUCCESS
);
1460 /* Mark that somebody is waiting on this entry */
1461 flag_set(&le
->le_flags
, LF_WAKE_NEEDED
);
1463 ret
= thread_block_reason(THREAD_CONTINUE_NULL
, NULL
,
1465 if (ret
!= THREAD_AWAKENED
)
1466 return(KERN_SUCCESS
);
1469 * The world may have changed while we were asleep.
1470 * Some other resource we need may have gone into
1471 * deficit. Or maybe we're supposed to die now.
1472 * Go back to the top and reevaluate.
1474 return(KERN_FAILURE
);
1476 return(KERN_SUCCESS
);
1481 ledger_get_entries(ledger_t ledger
, int entry
, ledger_amount_t
*credit
,
1482 ledger_amount_t
*debit
)
1484 struct ledger_entry
*le
;
1486 if (!ENTRY_VALID(ledger
, entry
))
1487 return (KERN_INVALID_ARGUMENT
);
1489 le
= &ledger
->l_entries
[entry
];
1491 *credit
= le
->le_credit
;
1492 *debit
= le
->le_debit
;
1494 return (KERN_SUCCESS
);
1498 ledger_reset_callback_state(ledger_t ledger
, int entry
)
1500 struct ledger_entry
*le
;
1502 if (!ENTRY_VALID(ledger
, entry
))
1503 return (KERN_INVALID_ARGUMENT
);
1505 le
= &ledger
->l_entries
[entry
];
1507 flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
1509 return (KERN_SUCCESS
);
1513 ledger_disable_panic_on_negative(ledger_t ledger
, int entry
)
1515 struct ledger_entry
*le
;
1517 if (!ENTRY_VALID(ledger
, entry
))
1518 return (KERN_INVALID_ARGUMENT
);
1520 le
= &ledger
->l_entries
[entry
];
1522 flag_clear(&le
->le_flags
, LF_PANIC_ON_NEGATIVE
);
1524 return (KERN_SUCCESS
);
1528 ledger_get_balance(ledger_t ledger
, int entry
, ledger_amount_t
*balance
)
1530 struct ledger_entry
*le
;
1532 if (!ENTRY_VALID(ledger
, entry
))
1533 return (KERN_INVALID_ARGUMENT
);
1535 le
= &ledger
->l_entries
[entry
];
1537 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
1538 assert(le
->le_debit
== 0);
1540 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
1543 *balance
= le
->le_credit
- le
->le_debit
;
1545 return (KERN_SUCCESS
);
1549 ledger_template_info(void **buf
, int *len
)
1551 struct ledger_template_info
*lti
;
1552 struct entry_template
*et
;
1557 * Since all tasks share a ledger template, we'll just use the
1558 * caller's as the source.
1560 l
= current_task()->ledger
;
1561 if ((*len
< 0) || (l
== NULL
))
1564 if (*len
> l
->l_size
)
1566 lti
= kalloc((*len
) * sizeof (struct ledger_template_info
));
1571 template_lock(l
->l_template
);
1572 et
= l
->l_template
->lt_entries
;
1574 for (i
= 0; i
< *len
; i
++) {
1575 memset(lti
, 0, sizeof (*lti
));
1576 strlcpy(lti
->lti_name
, et
->et_key
, LEDGER_NAME_MAX
);
1577 strlcpy(lti
->lti_group
, et
->et_group
, LEDGER_NAME_MAX
);
1578 strlcpy(lti
->lti_units
, et
->et_units
, LEDGER_NAME_MAX
);
1582 template_unlock(l
->l_template
);
1588 ledger_fill_entry_info(struct ledger_entry
*le
,
1589 struct ledger_entry_info
*lei
,
1593 assert(lei
!= NULL
);
1595 memset(lei
, 0, sizeof (*lei
));
1597 lei
->lei_limit
= le
->le_limit
;
1598 lei
->lei_credit
= le
->le_credit
;
1599 lei
->lei_debit
= le
->le_debit
;
1600 lei
->lei_balance
= lei
->lei_credit
- lei
->lei_debit
;
1601 lei
->lei_refill_period
= (le
->le_flags
& LF_REFILL_SCHEDULED
) ?
1602 abstime_to_nsecs(le
->_le
.le_refill
.le_refill_period
) : 0;
1603 lei
->lei_last_refill
= abstime_to_nsecs(now
- le
->_le
.le_refill
.le_last_refill
);
1607 ledger_get_task_entry_info_multiple(task_t task
, void **buf
, int *len
)
1609 struct ledger_entry_info
*lei
;
1610 struct ledger_entry
*le
;
1611 uint64_t now
= mach_absolute_time();
1615 if ((*len
< 0) || ((l
= task
->ledger
) == NULL
))
1618 if (*len
> l
->l_size
)
1620 lei
= kalloc((*len
) * sizeof (struct ledger_entry_info
));
1627 for (i
= 0; i
< *len
; i
++) {
1628 ledger_fill_entry_info(le
, lei
, now
);
1637 ledger_get_entry_info(ledger_t ledger
,
1639 struct ledger_entry_info
*lei
)
1641 uint64_t now
= mach_absolute_time();
1643 assert(ledger
!= NULL
);
1644 assert(lei
!= NULL
);
1646 if (entry
>= 0 && entry
< ledger
->l_size
) {
1647 struct ledger_entry
*le
= &ledger
->l_entries
[entry
];
1648 ledger_fill_entry_info(le
, lei
, now
);
1653 ledger_info(task_t task
, struct ledger_info
*info
)
1657 if ((l
= task
->ledger
) == NULL
)
1660 memset(info
, 0, sizeof (*info
));
1662 strlcpy(info
->li_name
, l
->l_template
->lt_name
, LEDGER_NAME_MAX
);
1663 info
->li_id
= l
->l_id
;
1664 info
->li_entries
= l
->l_size
;
1670 ledger_limit(task_t task
, struct ledger_limit_args
*args
)
1676 if ((l
= task
->ledger
) == NULL
)
1679 idx
= ledger_key_lookup(l
->l_template
, args
->lla_name
);
1680 if ((idx
< 0) || (idx
>= l
->l_size
))
1684 * XXX - this doesn't really seem like the right place to have
1685 * a context-sensitive conversion of userspace units into kernel
1686 * units. For now I'll handwave and say that the ledger() system
1687 * call isn't meant for civilians to use - they should be using
1688 * the process policy interfaces.
1690 if (idx
== task_ledgers
.cpu_time
) {
1693 if (args
->lla_refill_period
) {
1695 * If a refill is scheduled, then the limit is
1696 * specified as a percentage of one CPU. The
1697 * syscall specifies the refill period in terms of
1698 * milliseconds, so we need to convert to nsecs.
1700 args
->lla_refill_period
*= 1000000;
1701 nsecs
= args
->lla_limit
*
1702 (args
->lla_refill_period
/ 100);
1703 lprintf(("CPU limited to %lld nsecs per second\n",
1707 * If no refill is scheduled, then this is a
1708 * fixed amount of CPU time (in nsecs) that can
1711 nsecs
= args
->lla_limit
;
1712 lprintf(("CPU limited to %lld nsecs\n", nsecs
));
1714 limit
= nsecs_to_abstime(nsecs
);
1716 limit
= args
->lla_limit
;
1717 lprintf(("%s limited to %lld\n", args
->lla_name
, limit
));
1720 if (args
->lla_refill_period
> 0)
1721 ledger_set_period(l
, idx
, args
->lla_refill_period
);
1723 ledger_set_limit(l
, idx
, limit
);
1724 flag_set(&l
->l_entries
[idx
].le_flags
, LEDGER_ACTION_BLOCK
);