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>
49 * Ledger entry flags. Bits in second nibble (masked by 0xF0) are used for
50 * ledger actions (LEDGER_ACTION_BLOCK, etc).
52 #define LF_ENTRY_ACTIVE 0x0001 /* entry is active if set */
53 #define LF_WAKE_NEEDED 0x0100 /* one or more threads are asleep */
54 #define LF_WAKE_INPROGRESS 0x0200 /* the wait queue is being processed */
55 #define LF_REFILL_SCHEDULED 0x0400 /* a refill timer has been set */
56 #define LF_REFILL_INPROGRESS 0x0800 /* the ledger is being refilled */
57 #define LF_CALLED_BACK 0x1000 /* callback was called for balance in deficit */
58 #define LF_WARNED 0x2000 /* callback was called for balance warning */
59 #define LF_TRACKING_MAX 0x4000 /* track max balance over user-specfied time */
60 #define LF_PANIC_ON_NEGATIVE 0x8000 /* panic if it goes negative */
61 #define LF_TRACK_CREDIT_ONLY 0x10000 /* only update "credit" */
63 /* Determine whether a ledger entry exists and has been initialized and active */
64 #define ENTRY_VALID(l, e) \
65 (((l) != NULL) && ((e) >= 0) && ((e) < (l)->l_size) && \
66 (((l)->l_entries[e].le_flags & LF_ENTRY_ACTIVE) == LF_ENTRY_ACTIVE))
68 #define ASSERT(a) assert(a)
73 #define lprintf(a) if (ledger_debug) { \
74 printf("%lld ", abstime_to_nsecs(mach_absolute_time() / 1000000)); \
81 struct ledger_callback
{
82 ledger_callback_t lc_func
;
83 const void *lc_param0
;
84 const void *lc_param1
;
87 struct entry_template
{
88 char et_key
[LEDGER_NAME_MAX
];
89 char et_group
[LEDGER_NAME_MAX
];
90 char et_units
[LEDGER_NAME_MAX
];
92 struct ledger_callback
*et_callback
;
95 lck_grp_t ledger_lck_grp
;
98 * Modifying the reference count, table size, or table contents requires
99 * holding the lt_lock. Modfying the table address requires both lt_lock
100 * and setting the inuse bit. This means that the lt_entries field can be
101 * safely dereferenced if you hold either the lock or the inuse bit. The
102 * inuse bit exists solely to allow us to swap in a new, larger entries
103 * table without requiring a full lock to be acquired on each lookup.
104 * Accordingly, the inuse bit should never be held for longer than it takes
105 * to extract a value from the table - i.e., 2 or 3 memory references.
107 struct ledger_template
{
112 volatile uint32_t lt_inuse
;
114 struct entry_template
*lt_entries
;
117 #define template_lock(template) lck_mtx_lock(&(template)->lt_lock)
118 #define template_unlock(template) lck_mtx_unlock(&(template)->lt_lock)
120 #define TEMPLATE_INUSE(s, t) { \
122 while (OSCompareAndSwap(0, 1, &((t)->lt_inuse))) \
126 #define TEMPLATE_IDLE(s, t) { \
132 * Use 2 "tocks" to track the rolling maximum balance of a ledger entry.
136 * The explicit alignment is to ensure that atomic operations don't panic
139 struct ledger_entry
{
140 volatile uint32_t le_flags
;
141 ledger_amount_t le_limit
;
142 ledger_amount_t le_warn_level
;
143 volatile ledger_amount_t le_credit
__attribute__((aligned(8)));
144 volatile ledger_amount_t le_debit
__attribute__((aligned(8)));
148 * XXX - the following two fields can go away if we move all of
149 * the refill logic into process policy
151 uint64_t le_refill_period
;
152 uint64_t le_last_refill
;
155 uint32_t le_max
; /* Lower 32-bits of observed max balance */
156 uint32_t le_time
; /* time when this peak was observed */
159 } __attribute__((aligned(8)));
165 struct ledger_template
*l_template
;
166 struct ledger_entry l_entries
[0] __attribute__((aligned(8)));
169 static int ledger_cnt
= 0;
170 /* ledger ast helper functions */
171 static uint32_t ledger_check_needblock(ledger_t l
, uint64_t now
);
172 static kern_return_t
ledger_perform_blocking(ledger_t l
);
173 static uint32_t flag_set(volatile uint32_t *flags
, uint32_t bit
);
174 static uint32_t flag_clear(volatile uint32_t *flags
, uint32_t bit
);
176 static void ledger_entry_check_new_balance(ledger_t ledger
, int entry
,
177 struct ledger_entry
*le
);
181 debug_callback(const void *p0
, __unused
const void *p1
)
183 printf("ledger: resource exhausted [%s] for task %p\n",
184 (const char *)p0
, p1
);
188 /************************************/
191 abstime_to_nsecs(uint64_t abstime
)
195 absolutetime_to_nanoseconds(abstime
, &nsecs
);
200 nsecs_to_abstime(uint64_t nsecs
)
204 nanoseconds_to_absolutetime(nsecs
, &abstime
);
211 lck_grp_init(&ledger_lck_grp
, "ledger", LCK_GRP_ATTR_NULL
);
215 ledger_template_create(const char *name
)
217 ledger_template_t
template;
219 template = (ledger_template_t
)kalloc(sizeof (*template));
220 if (template == NULL
)
223 template->lt_name
= name
;
224 template->lt_refs
= 1;
225 template->lt_cnt
= 0;
226 template->lt_table_size
= 1;
227 template->lt_inuse
= 0;
228 lck_mtx_init(&template->lt_lock
, &ledger_lck_grp
, LCK_ATTR_NULL
);
230 template->lt_entries
= (struct entry_template
*)
231 kalloc(sizeof (struct entry_template
) * template->lt_table_size
);
232 if (template->lt_entries
== NULL
) {
233 kfree(template, sizeof (*template));
241 ledger_template_dereference(ledger_template_t
template)
243 template_lock(template);
245 template_unlock(template);
247 if (template->lt_refs
== 0)
248 kfree(template, sizeof (*template));
252 * Add a new entry to the list of entries in a ledger template. There is
253 * currently no mechanism to remove an entry. Implementing such a mechanism
254 * would require us to maintain per-entry reference counts, which we would
255 * prefer to avoid if possible.
258 ledger_entry_add(ledger_template_t
template, const char *key
,
259 const char *group
, const char *units
)
262 struct entry_template
*et
;
264 if ((key
== NULL
) || (strlen(key
) >= LEDGER_NAME_MAX
))
267 template_lock(template);
269 /* If the table is full, attempt to double its size */
270 if (template->lt_cnt
== template->lt_table_size
) {
271 struct entry_template
*new_entries
, *old_entries
;
275 old_cnt
= template->lt_table_size
;
276 old_sz
= (int)(old_cnt
* sizeof (struct entry_template
));
277 new_entries
= kalloc(old_sz
* 2);
278 if (new_entries
== NULL
) {
279 template_unlock(template);
282 memcpy(new_entries
, template->lt_entries
, old_sz
);
283 memset(((char *)new_entries
) + old_sz
, 0, old_sz
);
284 template->lt_table_size
= old_cnt
* 2;
286 old_entries
= template->lt_entries
;
288 TEMPLATE_INUSE(s
, template);
289 template->lt_entries
= new_entries
;
290 TEMPLATE_IDLE(s
, template);
292 kfree(old_entries
, old_sz
);
295 et
= &template->lt_entries
[template->lt_cnt
];
296 strlcpy(et
->et_key
, key
, LEDGER_NAME_MAX
);
297 strlcpy(et
->et_group
, group
, LEDGER_NAME_MAX
);
298 strlcpy(et
->et_units
, units
, LEDGER_NAME_MAX
);
299 et
->et_flags
= LF_ENTRY_ACTIVE
;
300 et
->et_callback
= NULL
;
302 idx
= template->lt_cnt
++;
303 template_unlock(template);
310 ledger_entry_setactive(ledger_t ledger
, int entry
)
312 struct ledger_entry
*le
;
314 if ((ledger
== NULL
) || (entry
< 0) || (entry
>= ledger
->l_size
))
315 return (KERN_INVALID_ARGUMENT
);
317 le
= &ledger
->l_entries
[entry
];
318 if ((le
->le_flags
& LF_ENTRY_ACTIVE
) == 0) {
319 flag_set(&le
->le_flags
, LF_ENTRY_ACTIVE
);
321 return (KERN_SUCCESS
);
326 ledger_key_lookup(ledger_template_t
template, const char *key
)
330 template_lock(template);
331 for (idx
= 0; idx
< template->lt_cnt
; idx
++)
332 if (template->lt_entries
!= NULL
&&
333 (strcmp(key
, template->lt_entries
[idx
].et_key
) == 0))
336 if (idx
>= template->lt_cnt
)
338 template_unlock(template);
344 * Create a new ledger based on the specified template. As part of the
345 * ledger creation we need to allocate space for a table of ledger entries.
346 * The size of the table is based on the size of the template at the time
347 * the ledger is created. If additional entries are added to the template
348 * after the ledger is created, they will not be tracked in this ledger.
351 ledger_instantiate(ledger_template_t
template, int entry_type
)
357 template_lock(template);
359 cnt
= template->lt_cnt
;
360 template_unlock(template);
362 sz
= sizeof(*ledger
) + (cnt
* sizeof(struct ledger_entry
));
364 ledger
= (ledger_t
)kalloc(sz
);
365 if (ledger
== NULL
) {
366 ledger_template_dereference(template);
370 ledger
->l_template
= template;
371 ledger
->l_id
= ledger_cnt
++;
373 ledger
->l_size
= (int32_t)cnt
;
375 template_lock(template);
376 assert(ledger
->l_size
<= template->lt_cnt
);
377 for (i
= 0; i
< ledger
->l_size
; i
++) {
378 struct ledger_entry
*le
= &ledger
->l_entries
[i
];
379 struct entry_template
*et
= &template->lt_entries
[i
];
381 le
->le_flags
= et
->et_flags
;
382 /* make entry inactive by removing active bit */
383 if (entry_type
== LEDGER_CREATE_INACTIVE_ENTRIES
)
384 flag_clear(&le
->le_flags
, LF_ENTRY_ACTIVE
);
386 * If template has a callback, this entry is opted-in,
389 if (et
->et_callback
!= NULL
)
390 flag_set(&le
->le_flags
, LEDGER_ACTION_CALLBACK
);
393 le
->le_limit
= LEDGER_LIMIT_INFINITY
;
394 le
->le_warn_level
= LEDGER_LIMIT_INFINITY
;
395 le
->_le
.le_refill
.le_refill_period
= 0;
396 le
->_le
.le_refill
.le_last_refill
= 0;
398 template_unlock(template);
404 flag_set(volatile uint32_t *flags
, uint32_t bit
)
406 return (OSBitOrAtomic(bit
, flags
));
410 flag_clear(volatile uint32_t *flags
, uint32_t bit
)
412 return (OSBitAndAtomic(~bit
, flags
));
416 * Take a reference on a ledger
419 ledger_reference(ledger_t ledger
)
421 if (!LEDGER_VALID(ledger
))
422 return (KERN_INVALID_ARGUMENT
);
423 OSIncrementAtomic(&ledger
->l_refs
);
424 return (KERN_SUCCESS
);
428 ledger_reference_count(ledger_t ledger
)
430 if (!LEDGER_VALID(ledger
))
433 return (ledger
->l_refs
);
437 * Remove a reference on a ledger. If this is the last reference,
438 * deallocate the unused ledger.
441 ledger_dereference(ledger_t ledger
)
445 if (!LEDGER_VALID(ledger
))
446 return (KERN_INVALID_ARGUMENT
);
448 v
= OSDecrementAtomic(&ledger
->l_refs
);
451 /* Just released the last reference. Free it. */
454 sizeof(*ledger
) + ledger
->l_size
* sizeof(struct ledger_entry
));
457 return (KERN_SUCCESS
);
461 * Determine whether an entry has exceeded its warning level.
464 warn_level_exceeded(struct ledger_entry
*le
)
466 ledger_amount_t balance
;
468 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
469 assert(le
->le_debit
== 0);
471 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
475 * XXX - Currently, we only support warnings for ledgers which
476 * use positive limits.
478 balance
= le
->le_credit
- le
->le_debit
;
479 if ((le
->le_warn_level
!= LEDGER_LIMIT_INFINITY
) && (balance
> le
->le_warn_level
))
485 * Determine whether an entry has exceeded its limit.
488 limit_exceeded(struct ledger_entry
*le
)
490 ledger_amount_t balance
;
492 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
493 assert(le
->le_debit
== 0);
495 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
498 balance
= le
->le_credit
- le
->le_debit
;
499 if ((le
->le_limit
<= 0) && (balance
< le
->le_limit
))
502 if ((le
->le_limit
> 0) && (balance
> le
->le_limit
))
507 static inline struct ledger_callback
*
508 entry_get_callback(ledger_t ledger
, int entry
)
510 struct ledger_callback
*callback
;
513 TEMPLATE_INUSE(s
, ledger
->l_template
);
514 callback
= ledger
->l_template
->lt_entries
[entry
].et_callback
;
515 TEMPLATE_IDLE(s
, ledger
->l_template
);
521 * If the ledger value is positive, wake up anybody waiting on it.
524 ledger_limit_entry_wakeup(struct ledger_entry
*le
)
528 if (!limit_exceeded(le
)) {
529 flags
= flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
531 while (le
->le_flags
& LF_WAKE_NEEDED
) {
532 flag_clear(&le
->le_flags
, LF_WAKE_NEEDED
);
533 thread_wakeup((event_t
)le
);
539 * Refill the coffers.
542 ledger_refill(uint64_t now
, ledger_t ledger
, int entry
)
544 uint64_t elapsed
, period
, periods
;
545 struct ledger_entry
*le
;
546 ledger_amount_t balance
, due
;
548 assert(entry
>= 0 && entry
< ledger
->l_size
);
550 le
= &ledger
->l_entries
[entry
];
552 assert(le
->le_limit
!= LEDGER_LIMIT_INFINITY
);
554 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
555 assert(le
->le_debit
== 0);
560 * If another thread is handling the refill already, we're not
563 if (flag_set(&le
->le_flags
, LF_REFILL_INPROGRESS
) & LF_REFILL_INPROGRESS
) {
568 * If the timestamp we're about to use to refill is older than the
569 * last refill, then someone else has already refilled this ledger
570 * and there's nothing for us to do here.
572 if (now
<= le
->_le
.le_refill
.le_last_refill
) {
573 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
578 * See how many refill periods have passed since we last
581 period
= le
->_le
.le_refill
.le_refill_period
;
582 elapsed
= now
- le
->_le
.le_refill
.le_last_refill
;
583 if ((period
== 0) || (elapsed
< period
)) {
584 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
589 * Optimize for the most common case of only one or two
593 while ((periods
< 2) && (elapsed
> 0)) {
599 * OK, it's been a long time. Do a divide to figure out
603 periods
= (now
- le
->_le
.le_refill
.le_last_refill
) / period
;
605 balance
= le
->le_credit
- le
->le_debit
;
606 due
= periods
* le
->le_limit
;
607 if (balance
- due
< 0)
612 OSAddAtomic64(due
, &le
->le_debit
);
614 assert(le
->le_debit
>= 0);
617 * If we've completely refilled the pool, set the refill time to now.
618 * Otherwise set it to the time at which it last should have been
622 le
->_le
.le_refill
.le_last_refill
= now
;
624 le
->_le
.le_refill
.le_last_refill
+= (le
->_le
.le_refill
.le_refill_period
* periods
);
626 flag_clear(&le
->le_flags
, LF_REFILL_INPROGRESS
);
628 lprintf(("Refill %lld %lld->%lld\n", periods
, balance
, balance
- due
));
629 if (!limit_exceeded(le
))
630 ledger_limit_entry_wakeup(le
);
634 * In tenths of a second, the length of one lookback period (a "tock") for
635 * ledger rolling maximum calculations. The effective lookback window will be this times
638 * Use a tock length of 2.5 seconds to get a total lookback period of 5 seconds.
640 * XXX Could make this caller-definable, at the point that rolling max tracking
641 * is enabled for the entry.
646 * How many sched_tick's are there in one tock (one of our lookback periods)?
648 * X sched_ticks 2.5 sec N sched_ticks
649 * --------------- = ---------- * -------------
652 * where N sched_ticks/sec is calculated via 1 << SCHED_TICK_SHIFT (see sched_prim.h)
654 * This should give us 20 sched_tick's in one 2.5 second-long tock.
656 #define SCHED_TICKS_PER_TOCK ((TOCKLEN * (1 << SCHED_TICK_SHIFT)) / 10)
659 * Rolling max timestamps use their own unit (let's call this a "tock"). One tock is the
660 * length of one lookback period that we use for our rolling max calculation.
662 * Calculate the current time in tocks from sched_tick (which runs at a some
665 #define CURRENT_TOCKSTAMP() (sched_tick / SCHED_TICKS_PER_TOCK)
668 * Does the given tockstamp fall in either the current or the previous tocks?
670 #define TOCKSTAMP_IS_STALE(now, tock) ((((now) - (tock)) < NTOCKS) ? FALSE : TRUE)
673 ledger_entry_check_new_balance(ledger_t ledger
, int entry
, struct ledger_entry
*le
)
675 ledger_amount_t credit
, debit
;
677 if (le
->le_flags
& LF_TRACKING_MAX
) {
678 ledger_amount_t balance
= le
->le_credit
- le
->le_debit
;
679 uint32_t now
= CURRENT_TOCKSTAMP();
680 struct _le_peak
*p
= &le
->_le
.le_peaks
[now
% NTOCKS
];
682 if (!TOCKSTAMP_IS_STALE(now
, p
->le_time
) || (balance
> p
->le_max
)) {
684 * The current balance is greater than the previously
685 * observed peak for the current time block, *or* we
686 * haven't yet recorded a peak for the current time block --
687 * so this is our new peak.
689 * (We only track the lower 32-bits of a balance for rolling
692 p
->le_max
= (uint32_t)balance
;
697 /* Check to see whether we're due a refill */
698 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
699 uint64_t now
= mach_absolute_time();
700 if ((now
- le
->_le
.le_refill
.le_last_refill
) > le
->_le
.le_refill
.le_refill_period
)
701 ledger_refill(now
, ledger
, entry
);
704 if (limit_exceeded(le
)) {
706 * We've exceeded the limit for this entry. There
707 * are several possible ways to handle it. We can block,
708 * we can execute a callback, or we can ignore it. In
709 * either of the first two cases, we want to set the AST
710 * flag so we can take the appropriate action just before
711 * leaving the kernel. The one caveat is that if we have
712 * already called the callback, we don't want to do it
713 * again until it gets rearmed.
715 if ((le
->le_flags
& LEDGER_ACTION_BLOCK
) ||
716 (!(le
->le_flags
& LF_CALLED_BACK
) &&
717 entry_get_callback(ledger
, entry
))) {
718 set_astledger(current_thread());
722 * The balance on the account is below the limit.
724 * If there are any threads blocked on this entry, now would
725 * be a good time to wake them up.
727 if (le
->le_flags
& LF_WAKE_NEEDED
)
728 ledger_limit_entry_wakeup(le
);
730 if (le
->le_flags
& LEDGER_ACTION_CALLBACK
) {
732 * Client has requested that a callback be invoked whenever
733 * the ledger's balance crosses into or out of the warning
736 if (warn_level_exceeded(le
)) {
738 * This ledger's balance is above the warning level.
740 if ((le
->le_flags
& LF_WARNED
) == 0) {
742 * If we are above the warning level and
743 * have not yet invoked the callback,
744 * set the AST so it can be done before returning
747 set_astledger(current_thread());
751 * This ledger's balance is below the warning level.
753 if (le
->le_flags
& LF_WARNED
) {
755 * If we are below the warning level and
756 * the LF_WARNED flag is still set, we need
757 * to invoke the callback to let the client
758 * know the ledger balance is now back below
761 set_astledger(current_thread());
767 credit
= le
->le_credit
;
768 debit
= le
->le_debit
;
769 if ((le
->le_flags
& LF_PANIC_ON_NEGATIVE
) &&
771 (le
->le_credit
< le
->le_debit
))) {
772 panic("ledger_entry_check_new_balance(%p,%d): negative ledger %p credit:%lld/%lld debit:%lld/%lld balance:%lld/%lld\n",
774 credit
, le
->le_credit
,
776 credit
- debit
, le
->le_credit
- le
->le_debit
);
781 ledger_check_new_balance(ledger_t ledger
, int entry
)
783 struct ledger_entry
*le
;
784 assert(entry
> 0 && entry
<= ledger
->l_size
);
785 le
= &ledger
->l_entries
[entry
];
786 ledger_entry_check_new_balance(ledger
, entry
, le
);
791 * Add value to an entry in a ledger.
794 ledger_credit(ledger_t ledger
, int entry
, ledger_amount_t amount
)
796 ledger_amount_t old
, new;
797 struct ledger_entry
*le
;
799 if (!ENTRY_VALID(ledger
, entry
) || (amount
< 0))
800 return (KERN_INVALID_VALUE
);
803 return (KERN_SUCCESS
);
805 le
= &ledger
->l_entries
[entry
];
807 old
= OSAddAtomic64(amount
, &le
->le_credit
);
809 lprintf(("%p Credit %lld->%lld\n", current_thread(), old
, new));
810 ledger_entry_check_new_balance(ledger
, entry
, le
);
812 return (KERN_SUCCESS
);
815 /* Add all of one ledger's values into another.
816 * They must have been created from the same template.
817 * This is not done atomically. Another thread (if not otherwise synchronized)
818 * may see bogus values when comparing one entry to another.
819 * As each entry's credit & debit are modified one at a time, the warning/limit
820 * may spuriously trip, or spuriously fail to trip, or another thread (if not
821 * otherwise synchronized) may see a bogus balance.
824 ledger_rollup(ledger_t to_ledger
, ledger_t from_ledger
)
828 assert(to_ledger
->l_template
== from_ledger
->l_template
);
830 for (i
= 0; i
< to_ledger
->l_size
; i
++) {
831 ledger_rollup_entry(to_ledger
, from_ledger
, i
);
834 return (KERN_SUCCESS
);
837 /* Add one ledger entry value to another.
838 * They must have been created from the same template.
839 * Since the credit and debit values are added one
840 * at a time, other thread might read the a bogus value.
843 ledger_rollup_entry(ledger_t to_ledger
, ledger_t from_ledger
, int entry
)
845 struct ledger_entry
*from_le
, *to_le
;
847 assert(to_ledger
->l_template
== from_ledger
->l_template
);
848 if (ENTRY_VALID(from_ledger
, entry
) && ENTRY_VALID(to_ledger
, entry
)) {
849 from_le
= &from_ledger
->l_entries
[entry
];
850 to_le
= &to_ledger
->l_entries
[entry
];
851 OSAddAtomic64(from_le
->le_credit
, &to_le
->le_credit
);
852 OSAddAtomic64(from_le
->le_debit
, &to_le
->le_debit
);
855 return (KERN_SUCCESS
);
859 * Zero the balance of a ledger by adding to its credit or debit, whichever is smaller.
860 * Note that some clients of ledgers (notably, task wakeup statistics) require that
861 * le_credit only ever increase as a function of ledger_credit().
864 ledger_zero_balance(ledger_t ledger
, int entry
)
866 struct ledger_entry
*le
;
868 if (!ENTRY_VALID(ledger
, entry
))
869 return (KERN_INVALID_VALUE
);
871 le
= &ledger
->l_entries
[entry
];
874 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
875 assert(le
->le_debit
== 0);
876 if (!OSCompareAndSwap64(le
->le_credit
, 0, &le
->le_credit
)) {
879 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_credit
, 0));
880 } else if (le
->le_credit
> le
->le_debit
) {
881 if (!OSCompareAndSwap64(le
->le_debit
, le
->le_credit
, &le
->le_debit
))
883 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_debit
, le
->le_credit
));
884 } else if (le
->le_credit
< le
->le_debit
) {
885 if (!OSCompareAndSwap64(le
->le_credit
, le
->le_debit
, &le
->le_credit
))
887 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le
->le_credit
, le
->le_debit
));
890 return (KERN_SUCCESS
);
894 ledger_get_limit(ledger_t ledger
, int entry
, ledger_amount_t
*limit
)
896 struct ledger_entry
*le
;
898 if (!ENTRY_VALID(ledger
, entry
))
899 return (KERN_INVALID_VALUE
);
901 le
= &ledger
->l_entries
[entry
];
902 *limit
= le
->le_limit
;
904 lprintf(("ledger_get_limit: %lld\n", *limit
));
906 return (KERN_SUCCESS
);
910 * Adjust the limit of a limited resource. This does not affect the
911 * current balance, so the change doesn't affect the thread until the
914 * warn_level: If non-zero, causes the callback to be invoked when
915 * the balance exceeds this level. Specified as a percentage [of the limit].
918 ledger_set_limit(ledger_t ledger
, int entry
, ledger_amount_t limit
,
919 uint8_t warn_level_percentage
)
921 struct ledger_entry
*le
;
923 if (!ENTRY_VALID(ledger
, entry
))
924 return (KERN_INVALID_VALUE
);
926 lprintf(("ledger_set_limit: %lld\n", limit
));
927 le
= &ledger
->l_entries
[entry
];
929 if (limit
== LEDGER_LIMIT_INFINITY
) {
931 * Caller wishes to disable the limit. This will implicitly
932 * disable automatic refill, as refills implicitly depend
935 ledger_disable_refill(ledger
, entry
);
938 le
->le_limit
= limit
;
939 le
->_le
.le_refill
.le_last_refill
= 0;
940 flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
941 flag_clear(&le
->le_flags
, LF_WARNED
);
942 ledger_limit_entry_wakeup(le
);
944 if (warn_level_percentage
!= 0) {
945 assert(warn_level_percentage
<= 100);
946 assert(limit
> 0); /* no negative limit support for warnings */
947 assert(limit
!= LEDGER_LIMIT_INFINITY
); /* warn % without limit makes no sense */
948 le
->le_warn_level
= (le
->le_limit
* warn_level_percentage
) / 100;
950 le
->le_warn_level
= LEDGER_LIMIT_INFINITY
;
953 return (KERN_SUCCESS
);
957 ledger_get_maximum(ledger_t ledger
, int entry
,
958 ledger_amount_t
*max_observed_balance
)
960 struct ledger_entry
*le
;
961 uint32_t now
= CURRENT_TOCKSTAMP();
964 le
= &ledger
->l_entries
[entry
];
966 if (!ENTRY_VALID(ledger
, entry
) || !(le
->le_flags
& LF_TRACKING_MAX
)) {
967 return (KERN_INVALID_VALUE
);
971 * Start with the current balance; if neither of the recorded peaks are
972 * within recent history, we use this.
974 *max_observed_balance
= le
->le_credit
- le
->le_debit
;
976 for (i
= 0; i
< NTOCKS
; i
++) {
977 if (!TOCKSTAMP_IS_STALE(now
, le
->_le
.le_peaks
[i
].le_time
) &&
978 (le
->_le
.le_peaks
[i
].le_max
> *max_observed_balance
)) {
980 * The peak for this time block isn't stale, and it
981 * is greater than the current balance -- so use it.
983 *max_observed_balance
= le
->_le
.le_peaks
[i
].le_max
;
987 lprintf(("ledger_get_maximum: %lld\n", *max_observed_balance
));
989 return (KERN_SUCCESS
);
993 * Enable tracking of periodic maximums for this ledger entry.
996 ledger_track_maximum(ledger_template_t
template, int entry
,
997 __unused
int period_in_secs
)
999 template_lock(template);
1001 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1002 template_unlock(template);
1003 return (KERN_INVALID_VALUE
);
1006 template->lt_entries
[entry
].et_flags
|= LF_TRACKING_MAX
;
1007 template_unlock(template);
1009 return (KERN_SUCCESS
);
1013 ledger_panic_on_negative(ledger_template_t
template, int entry
)
1015 template_lock(template);
1017 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1018 template_unlock(template);
1019 return (KERN_INVALID_VALUE
);
1022 template->lt_entries
[entry
].et_flags
|= LF_PANIC_ON_NEGATIVE
;
1024 template_unlock(template);
1026 return (KERN_SUCCESS
);
1030 ledger_track_credit_only(ledger_template_t
template, int entry
)
1032 template_lock(template);
1034 if ((entry
< 0) || (entry
>= template->lt_cnt
)) {
1035 template_unlock(template);
1036 return (KERN_INVALID_VALUE
);
1039 template->lt_entries
[entry
].et_flags
|= LF_TRACK_CREDIT_ONLY
;
1041 template_unlock(template);
1043 return (KERN_SUCCESS
);
1047 * Add a callback to be executed when the resource goes into deficit.
1050 ledger_set_callback(ledger_template_t
template, int entry
,
1051 ledger_callback_t func
, const void *param0
, const void *param1
)
1053 struct entry_template
*et
;
1054 struct ledger_callback
*old_cb
, *new_cb
;
1056 if ((entry
< 0) || (entry
>= template->lt_cnt
))
1057 return (KERN_INVALID_VALUE
);
1060 new_cb
= (struct ledger_callback
*)kalloc(sizeof (*new_cb
));
1061 new_cb
->lc_func
= func
;
1062 new_cb
->lc_param0
= param0
;
1063 new_cb
->lc_param1
= param1
;
1068 template_lock(template);
1069 et
= &template->lt_entries
[entry
];
1070 old_cb
= et
->et_callback
;
1071 et
->et_callback
= new_cb
;
1072 template_unlock(template);
1074 kfree(old_cb
, sizeof (*old_cb
));
1076 return (KERN_SUCCESS
);
1080 * Disable callback notification for a specific ledger entry.
1082 * Otherwise, if using a ledger template which specified a
1083 * callback function (ledger_set_callback()), it will be invoked when
1084 * the resource goes into deficit.
1087 ledger_disable_callback(ledger_t ledger
, int entry
)
1089 if (!ENTRY_VALID(ledger
, entry
))
1090 return (KERN_INVALID_VALUE
);
1093 * le_warn_level is used to indicate *if* this ledger has a warning configured,
1094 * in addition to what that warning level is set to.
1095 * This means a side-effect of ledger_disable_callback() is that the
1096 * warning level is forgotten.
1098 ledger
->l_entries
[entry
].le_warn_level
= LEDGER_LIMIT_INFINITY
;
1099 flag_clear(&ledger
->l_entries
[entry
].le_flags
, LEDGER_ACTION_CALLBACK
);
1100 return (KERN_SUCCESS
);
1104 * Enable callback notification for a specific ledger entry.
1106 * This is only needed if ledger_disable_callback() has previously
1107 * been invoked against an entry; there must already be a callback
1111 ledger_enable_callback(ledger_t ledger
, int entry
)
1113 if (!ENTRY_VALID(ledger
, entry
))
1114 return (KERN_INVALID_VALUE
);
1116 assert(entry_get_callback(ledger
, entry
) != NULL
);
1118 flag_set(&ledger
->l_entries
[entry
].le_flags
, LEDGER_ACTION_CALLBACK
);
1119 return (KERN_SUCCESS
);
1123 * Query the automatic refill period for this ledger entry.
1125 * A period of 0 means this entry has none configured.
1128 ledger_get_period(ledger_t ledger
, int entry
, uint64_t *period
)
1130 struct ledger_entry
*le
;
1132 if (!ENTRY_VALID(ledger
, entry
))
1133 return (KERN_INVALID_VALUE
);
1135 le
= &ledger
->l_entries
[entry
];
1136 *period
= abstime_to_nsecs(le
->_le
.le_refill
.le_refill_period
);
1137 lprintf(("ledger_get_period: %llx\n", *period
));
1138 return (KERN_SUCCESS
);
1142 * Adjust the automatic refill period.
1145 ledger_set_period(ledger_t ledger
, int entry
, uint64_t period
)
1147 struct ledger_entry
*le
;
1149 lprintf(("ledger_set_period: %llx\n", period
));
1150 if (!ENTRY_VALID(ledger
, entry
))
1151 return (KERN_INVALID_VALUE
);
1153 le
= &ledger
->l_entries
[entry
];
1156 * A refill period refills the ledger in multiples of the limit,
1157 * so if you haven't set one yet, you need a lesson on ledgers.
1159 assert(le
->le_limit
!= LEDGER_LIMIT_INFINITY
);
1161 if (le
->le_flags
& LF_TRACKING_MAX
) {
1163 * Refill is incompatible with rolling max tracking.
1165 return (KERN_INVALID_VALUE
);
1168 le
->_le
.le_refill
.le_refill_period
= nsecs_to_abstime(period
);
1171 * Set the 'starting time' for the next refill to now. Since
1172 * we're resetting the balance to zero here, we consider this
1173 * moment the starting time for accumulating a balance that
1174 * counts towards the limit.
1176 le
->_le
.le_refill
.le_last_refill
= mach_absolute_time();
1177 ledger_zero_balance(ledger
, entry
);
1179 flag_set(&le
->le_flags
, LF_REFILL_SCHEDULED
);
1181 return (KERN_SUCCESS
);
1185 * Disable automatic refill.
1188 ledger_disable_refill(ledger_t ledger
, int entry
)
1190 struct ledger_entry
*le
;
1192 if (!ENTRY_VALID(ledger
, entry
))
1193 return (KERN_INVALID_VALUE
);
1195 le
= &ledger
->l_entries
[entry
];
1197 flag_clear(&le
->le_flags
, LF_REFILL_SCHEDULED
);
1199 return (KERN_SUCCESS
);
1203 ledger_get_actions(ledger_t ledger
, int entry
, int *actions
)
1205 if (!ENTRY_VALID(ledger
, entry
))
1206 return (KERN_INVALID_VALUE
);
1208 *actions
= ledger
->l_entries
[entry
].le_flags
& LEDGER_ACTION_MASK
;
1209 lprintf(("ledger_get_actions: %#x\n", *actions
));
1210 return (KERN_SUCCESS
);
1214 ledger_set_action(ledger_t ledger
, int entry
, int action
)
1216 lprintf(("ledger_set_action: %#x\n", action
));
1217 if (!ENTRY_VALID(ledger
, entry
))
1218 return (KERN_INVALID_VALUE
);
1220 flag_set(&ledger
->l_entries
[entry
].le_flags
, action
);
1221 return (KERN_SUCCESS
);
1225 ledger_debit(ledger_t ledger
, int entry
, ledger_amount_t amount
)
1227 struct ledger_entry
*le
;
1228 ledger_amount_t old
, new;
1230 if (!ENTRY_VALID(ledger
, entry
) || (amount
< 0))
1231 return (KERN_INVALID_ARGUMENT
);
1234 return (KERN_SUCCESS
);
1236 le
= &ledger
->l_entries
[entry
];
1238 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
1239 assert(le
->le_debit
== 0);
1240 old
= OSAddAtomic64(-amount
, &le
->le_credit
);
1243 old
= OSAddAtomic64(amount
, &le
->le_debit
);
1246 lprintf(("%p Debit %lld->%lld\n", thread
, old
, new));
1248 ledger_entry_check_new_balance(ledger
, entry
, le
);
1249 return (KERN_SUCCESS
);
1254 ledger_ast(thread_t thread
)
1256 struct ledger
*l
= thread
->t_ledger
;
1261 uint8_t task_percentage
;
1262 uint64_t task_interval
;
1265 task_t task
= thread
->task
;
1267 lprintf(("Ledger AST for %p\n", thread
));
1269 ASSERT(task
!= NULL
);
1270 ASSERT(thread
== current_thread());
1274 * Take a self-consistent snapshot of the CPU usage monitor parameters. The task
1275 * can change them at any point (with the task locked).
1278 task_flags
= task
->rusage_cpu_flags
;
1279 task_percentage
= task
->rusage_cpu_perthr_percentage
;
1280 task_interval
= task
->rusage_cpu_perthr_interval
;
1284 * Make sure this thread is up to date with regards to any task-wide per-thread
1285 * CPU limit, but only if it doesn't have a thread-private blocking CPU limit.
1287 if (((task_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) != 0) &&
1288 ((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0)) {
1293 thread_get_cpulimit(&action
, &percentage
, &interval
);
1296 * If the thread's CPU limits no longer match the task's, or the
1297 * task has a limit but the thread doesn't, update the limit.
1299 if (((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0) ||
1300 (interval
!= task_interval
) || (percentage
!= task_percentage
)) {
1301 thread_set_cpulimit(THREAD_CPULIMIT_EXCEPTION
, task_percentage
, task_interval
);
1302 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) != 0);
1304 } else if (((task_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) &&
1305 (thread
->options
& TH_OPT_PROC_CPULIMIT
)) {
1306 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
1309 * Task no longer has a per-thread CPU limit; remove this thread's
1310 * corresponding CPU limit.
1312 thread_set_cpulimit(THREAD_CPULIMIT_DISABLE
, 0, 0);
1313 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
1317 * If the task or thread is being terminated, let's just get on with it
1319 if ((l
== NULL
) || !task
->active
|| task
->halting
|| !thread
->active
)
1323 * Examine all entries in deficit to see which might be eligble for
1324 * an automatic refill, which require callbacks to be issued, and
1325 * which require blocking.
1328 now
= mach_absolute_time();
1331 * Note that thread->t_threadledger may have been changed by the
1332 * thread_set_cpulimit() call above - so don't examine it until afterwards.
1334 thl
= thread
->t_threadledger
;
1335 if (LEDGER_VALID(thl
)) {
1336 block
|= ledger_check_needblock(thl
, now
);
1338 block
|= ledger_check_needblock(l
, now
);
1341 * If we are supposed to block on the availability of one or more
1342 * resources, find the first entry in deficit for which we should wait.
1343 * Schedule a refill if necessary and then sleep until the resource
1344 * becomes available.
1347 if (LEDGER_VALID(thl
)) {
1348 ret
= ledger_perform_blocking(thl
);
1349 if (ret
!= KERN_SUCCESS
)
1352 ret
= ledger_perform_blocking(l
);
1353 if (ret
!= KERN_SUCCESS
)
1359 ledger_check_needblock(ledger_t l
, uint64_t now
)
1362 uint32_t flags
, block
= 0;
1363 struct ledger_entry
*le
;
1364 struct ledger_callback
*lc
;
1367 for (i
= 0; i
< l
->l_size
; i
++) {
1368 le
= &l
->l_entries
[i
];
1370 lc
= entry_get_callback(l
, i
);
1372 if (limit_exceeded(le
) == FALSE
) {
1373 if (le
->le_flags
& LEDGER_ACTION_CALLBACK
) {
1375 * If needed, invoke the callback as a warning.
1376 * This needs to happen both when the balance rises above
1377 * the warning level, and also when it dips back below it.
1381 * See comments for matching logic in ledger_check_new_balance().
1383 if (warn_level_exceeded(le
)) {
1384 flags
= flag_set(&le
->le_flags
, LF_WARNED
);
1385 if ((flags
& LF_WARNED
) == 0) {
1386 lc
->lc_func(LEDGER_WARNING_ROSE_ABOVE
, lc
->lc_param0
, lc
->lc_param1
);
1389 flags
= flag_clear(&le
->le_flags
, LF_WARNED
);
1390 if (flags
& LF_WARNED
) {
1391 lc
->lc_func(LEDGER_WARNING_DIPPED_BELOW
, lc
->lc_param0
, lc
->lc_param1
);
1399 /* We're over the limit, so refill if we are eligible and past due. */
1400 if (le
->le_flags
& LF_REFILL_SCHEDULED
) {
1401 if ((le
->_le
.le_refill
.le_last_refill
+ le
->_le
.le_refill
.le_refill_period
) > now
) {
1402 ledger_refill(now
, l
, i
);
1403 if (limit_exceeded(le
) == FALSE
)
1408 if (le
->le_flags
& LEDGER_ACTION_BLOCK
)
1410 if ((le
->le_flags
& LEDGER_ACTION_CALLBACK
) == 0)
1414 * If the LEDGER_ACTION_CALLBACK flag is on, we expect there to
1415 * be a registered callback.
1418 flags
= flag_set(&le
->le_flags
, LF_CALLED_BACK
);
1419 /* Callback has already been called */
1420 if (flags
& LF_CALLED_BACK
)
1422 lc
->lc_func(FALSE
, lc
->lc_param0
, lc
->lc_param1
);
1428 /* return KERN_SUCCESS to continue, KERN_FAILURE to restart */
1429 static kern_return_t
1430 ledger_perform_blocking(ledger_t l
)
1434 struct ledger_entry
*le
;
1436 for (i
= 0; i
< l
->l_size
; i
++) {
1437 le
= &l
->l_entries
[i
];
1438 if ((!limit_exceeded(le
)) ||
1439 ((le
->le_flags
& LEDGER_ACTION_BLOCK
) == 0))
1442 /* Prepare to sleep until the resource is refilled */
1443 ret
= assert_wait_deadline(le
, TRUE
,
1444 le
->_le
.le_refill
.le_last_refill
+ le
->_le
.le_refill
.le_refill_period
);
1445 if (ret
!= THREAD_WAITING
)
1446 return(KERN_SUCCESS
);
1448 /* Mark that somebody is waiting on this entry */
1449 flag_set(&le
->le_flags
, LF_WAKE_NEEDED
);
1451 ret
= thread_block_reason(THREAD_CONTINUE_NULL
, NULL
,
1453 if (ret
!= THREAD_AWAKENED
)
1454 return(KERN_SUCCESS
);
1457 * The world may have changed while we were asleep.
1458 * Some other resource we need may have gone into
1459 * deficit. Or maybe we're supposed to die now.
1460 * Go back to the top and reevaluate.
1462 return(KERN_FAILURE
);
1464 return(KERN_SUCCESS
);
1469 ledger_get_entries(ledger_t ledger
, int entry
, ledger_amount_t
*credit
,
1470 ledger_amount_t
*debit
)
1472 struct ledger_entry
*le
;
1474 if (!ENTRY_VALID(ledger
, entry
))
1475 return (KERN_INVALID_ARGUMENT
);
1477 le
= &ledger
->l_entries
[entry
];
1479 *credit
= le
->le_credit
;
1480 *debit
= le
->le_debit
;
1482 return (KERN_SUCCESS
);
1486 ledger_reset_callback_state(ledger_t ledger
, int entry
)
1488 struct ledger_entry
*le
;
1490 if (!ENTRY_VALID(ledger
, entry
))
1491 return (KERN_INVALID_ARGUMENT
);
1493 le
= &ledger
->l_entries
[entry
];
1495 flag_clear(&le
->le_flags
, LF_CALLED_BACK
);
1497 return (KERN_SUCCESS
);
1501 ledger_disable_panic_on_negative(ledger_t ledger
, int entry
)
1503 struct ledger_entry
*le
;
1505 if (!ENTRY_VALID(ledger
, entry
))
1506 return (KERN_INVALID_ARGUMENT
);
1508 le
= &ledger
->l_entries
[entry
];
1510 flag_clear(&le
->le_flags
, LF_PANIC_ON_NEGATIVE
);
1512 return (KERN_SUCCESS
);
1516 ledger_get_balance(ledger_t ledger
, int entry
, ledger_amount_t
*balance
)
1518 struct ledger_entry
*le
;
1520 if (!ENTRY_VALID(ledger
, entry
))
1521 return (KERN_INVALID_ARGUMENT
);
1523 le
= &ledger
->l_entries
[entry
];
1525 if (le
->le_flags
& LF_TRACK_CREDIT_ONLY
) {
1526 assert(le
->le_debit
== 0);
1528 assert((le
->le_credit
>= 0) && (le
->le_debit
>= 0));
1531 *balance
= le
->le_credit
- le
->le_debit
;
1533 return (KERN_SUCCESS
);
1537 ledger_template_info(void **buf
, int *len
)
1539 struct ledger_template_info
*lti
;
1540 struct entry_template
*et
;
1545 * Since all tasks share a ledger template, we'll just use the
1546 * caller's as the source.
1548 l
= current_task()->ledger
;
1549 if ((*len
< 0) || (l
== NULL
))
1552 if (*len
> l
->l_size
)
1554 lti
= kalloc((*len
) * sizeof (struct ledger_template_info
));
1559 template_lock(l
->l_template
);
1560 et
= l
->l_template
->lt_entries
;
1562 for (i
= 0; i
< *len
; i
++) {
1563 memset(lti
, 0, sizeof (*lti
));
1564 strlcpy(lti
->lti_name
, et
->et_key
, LEDGER_NAME_MAX
);
1565 strlcpy(lti
->lti_group
, et
->et_group
, LEDGER_NAME_MAX
);
1566 strlcpy(lti
->lti_units
, et
->et_units
, LEDGER_NAME_MAX
);
1570 template_unlock(l
->l_template
);
1576 ledger_fill_entry_info(struct ledger_entry
*le
,
1577 struct ledger_entry_info
*lei
,
1581 assert(lei
!= NULL
);
1583 memset(lei
, 0, sizeof (*lei
));
1585 lei
->lei_limit
= le
->le_limit
;
1586 lei
->lei_credit
= le
->le_credit
;
1587 lei
->lei_debit
= le
->le_debit
;
1588 lei
->lei_balance
= lei
->lei_credit
- lei
->lei_debit
;
1589 lei
->lei_refill_period
= (le
->le_flags
& LF_REFILL_SCHEDULED
) ?
1590 abstime_to_nsecs(le
->_le
.le_refill
.le_refill_period
) : 0;
1591 lei
->lei_last_refill
= abstime_to_nsecs(now
- le
->_le
.le_refill
.le_last_refill
);
1595 ledger_get_task_entry_info_multiple(task_t task
, void **buf
, int *len
)
1597 struct ledger_entry_info
*lei
;
1598 struct ledger_entry
*le
;
1599 uint64_t now
= mach_absolute_time();
1603 if ((*len
< 0) || ((l
= task
->ledger
) == NULL
))
1606 if (*len
> l
->l_size
)
1608 lei
= kalloc((*len
) * sizeof (struct ledger_entry_info
));
1615 for (i
= 0; i
< *len
; i
++) {
1616 ledger_fill_entry_info(le
, lei
, now
);
1625 ledger_get_entry_info(ledger_t ledger
,
1627 struct ledger_entry_info
*lei
)
1629 uint64_t now
= mach_absolute_time();
1631 assert(ledger
!= NULL
);
1632 assert(lei
!= NULL
);
1634 if (entry
>= 0 && entry
< ledger
->l_size
) {
1635 struct ledger_entry
*le
= &ledger
->l_entries
[entry
];
1636 ledger_fill_entry_info(le
, lei
, now
);
1641 ledger_info(task_t task
, struct ledger_info
*info
)
1645 if ((l
= task
->ledger
) == NULL
)
1648 memset(info
, 0, sizeof (*info
));
1650 strlcpy(info
->li_name
, l
->l_template
->lt_name
, LEDGER_NAME_MAX
);
1651 info
->li_id
= l
->l_id
;
1652 info
->li_entries
= l
->l_size
;
1658 ledger_limit(task_t task
, struct ledger_limit_args
*args
)
1664 if ((l
= task
->ledger
) == NULL
)
1667 idx
= ledger_key_lookup(l
->l_template
, args
->lla_name
);
1668 if ((idx
< 0) || (idx
>= l
->l_size
))
1672 * XXX - this doesn't really seem like the right place to have
1673 * a context-sensitive conversion of userspace units into kernel
1674 * units. For now I'll handwave and say that the ledger() system
1675 * call isn't meant for civilians to use - they should be using
1676 * the process policy interfaces.
1678 if (idx
== task_ledgers
.cpu_time
) {
1681 if (args
->lla_refill_period
) {
1683 * If a refill is scheduled, then the limit is
1684 * specified as a percentage of one CPU. The
1685 * syscall specifies the refill period in terms of
1686 * milliseconds, so we need to convert to nsecs.
1688 args
->lla_refill_period
*= 1000000;
1689 nsecs
= args
->lla_limit
*
1690 (args
->lla_refill_period
/ 100);
1691 lprintf(("CPU limited to %lld nsecs per second\n",
1695 * If no refill is scheduled, then this is a
1696 * fixed amount of CPU time (in nsecs) that can
1699 nsecs
= args
->lla_limit
;
1700 lprintf(("CPU limited to %lld nsecs\n", nsecs
));
1702 limit
= nsecs_to_abstime(nsecs
);
1704 limit
= args
->lla_limit
;
1705 lprintf(("%s limited to %lld\n", args
->lla_name
, limit
));
1708 if (args
->lla_refill_period
> 0)
1709 ledger_set_period(l
, idx
, args
->lla_refill_period
);
1711 ledger_set_limit(l
, idx
, limit
);
1712 flag_set(&l
->l_entries
[idx
].le_flags
, LEDGER_ACTION_BLOCK
);