Reduce code duplication. There should be no (intended) functional changes.

MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
This commit is contained in:
Edward Tomasz Napierala 2016-04-01 17:05:46 +00:00
parent 910938f079
commit 1028719823

View File

@ -114,6 +114,8 @@ SDT_PROBE_DEFINE3(racct, , rusage, set,
"struct proc *", "int", "uint64_t");
SDT_PROBE_DEFINE3(racct, , rusage, set__failure,
"struct proc *", "int", "uint64_t");
SDT_PROBE_DEFINE3(racct, , rusage, set__force,
"struct proc *", "int", "uint64_t");
SDT_PROBE_DEFINE3(racct, , rusage, sub,
"struct proc *", "int", "uint64_t");
SDT_PROBE_DEFINE3(racct, , rusage, sub__cred,
@ -597,8 +599,6 @@ racct_add_cred_locked(struct ucred *cred, int resource, uint64_t amount)
/*
* Increase allocation of 'resource' by 'amount' for credential 'cred'.
* Doesn't check for limits and never fails.
*
* XXX: Shouldn't this ever return an error?
*/
void
racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
@ -637,7 +637,7 @@ racct_add_force(struct proc *p, int resource, uint64_t amount)
}
static int
racct_set_locked(struct proc *p, int resource, uint64_t amount)
racct_set_locked(struct proc *p, int resource, uint64_t amount, int force)
{
int64_t old_amount, decayed_amount;
int64_t diff_proc, diff_cred;
@ -647,8 +647,6 @@ racct_set_locked(struct proc *p, int resource, uint64_t amount)
ASSERT_RACCT_ENABLED();
SDT_PROBE3(racct, , rusage, set, p, resource, amount);
/*
* We need proc lock to dereference p->p_ucred.
*/
@ -676,7 +674,7 @@ racct_set_locked(struct proc *p, int resource, uint64_t amount)
resource));
#endif
#ifdef RCTL
if (diff_proc > 0) {
if (!force && diff_proc > 0) {
error = rctl_enforce(p, resource, diff_proc);
if (error && RACCT_IS_DENIABLE(resource)) {
SDT_PROBE3(racct, , rusage, set__failure, p, resource,
@ -694,6 +692,20 @@ racct_set_locked(struct proc *p, int resource, uint64_t amount)
return (0);
}
void
racct_set_force(struct proc *p, int resource, uint64_t amount)
{
if (!racct_enable)
return;
SDT_PROBE3(racct, , rusage, set, p, resource, amount);
mtx_lock(&racct_lock);
racct_set_locked(p, resource, amount, 1);
mtx_unlock(&racct_lock);
}
/*
* Set allocation of 'resource' to 'amount' for process 'p'.
* Return 0 if it's below limits, or errno, if it's not.
@ -709,63 +721,14 @@ racct_set(struct proc *p, int resource, uint64_t amount)
if (!racct_enable)
return (0);
SDT_PROBE3(racct, , rusage, set__force, p, resource, amount);
mtx_lock(&racct_lock);
error = racct_set_locked(p, resource, amount);
error = racct_set_locked(p, resource, amount, 0);
mtx_unlock(&racct_lock);
return (error);
}
static void
racct_set_force_locked(struct proc *p, int resource, uint64_t amount)
{
int64_t old_amount, decayed_amount;
int64_t diff_proc, diff_cred;
ASSERT_RACCT_ENABLED();
SDT_PROBE3(racct, , rusage, set, p, resource, amount);
/*
* We need proc lock to dereference p->p_ucred.
*/
PROC_LOCK_ASSERT(p, MA_OWNED);
old_amount = p->p_racct->r_resources[resource];
/*
* The diffs may be negative.
*/
diff_proc = amount - old_amount;
if (RACCT_IS_DECAYING(resource)) {
/*
* Resources in per-credential racct containers may decay.
* If this is the case, we need to calculate the difference
* between the new amount and the proportional value of the
* old amount that has decayed in the ucred racct containers.
*/
decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
diff_cred = amount - decayed_amount;
} else
diff_cred = diff_proc;
racct_adjust_resource(p->p_racct, resource, diff_proc);
if (diff_cred > 0)
racct_add_cred_locked(p->p_ucred, resource, diff_cred);
else if (diff_cred < 0)
racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
}
void
racct_set_force(struct proc *p, int resource, uint64_t amount)
{
if (!racct_enable)
return;
mtx_lock(&racct_lock);
racct_set_force_locked(p, resource, amount);
mtx_unlock(&racct_lock);
}
/*
* Returns amount of 'resource' the process 'p' can keep allocated.
* Allocating more than that would be denied, unless the resource
@ -930,7 +893,7 @@ racct_proc_fork(struct proc *parent, struct proc *child)
continue;
error = racct_set_locked(child, i,
parent->p_racct->r_resources[i]);
parent->p_racct->r_resources[i], 0);
if (error != 0)
goto out;
}
@ -1002,7 +965,7 @@ racct_proc_exit(struct proc *p)
pct = racct_getpcpu(p, pct_estimate);
mtx_lock(&racct_lock);
racct_set_locked(p, RACCT_CPU, runtime);
racct_set_locked(p, RACCT_CPU, runtime, 0);
racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
for (i = 0; i <= RACCT_MAX; i++) {
@ -1010,7 +973,7 @@ racct_proc_exit(struct proc *p)
continue;
if (!RACCT_IS_RECLAIMABLE(i))
continue;
racct_set_locked(p, i, 0);
racct_set_locked(p, i, 0, 0);
}
mtx_unlock(&racct_lock);
@ -1249,11 +1212,11 @@ racctd(void)
pct_estimate = 0;
pct = racct_getpcpu(p, pct_estimate);
mtx_lock(&racct_lock);
racct_set_force_locked(p, RACCT_PCTCPU, pct);
racct_set_locked(p, RACCT_CPU, runtime);
racct_set_locked(p, RACCT_PCTCPU, pct, 1);
racct_set_locked(p, RACCT_CPU, runtime, 0);
racct_set_locked(p, RACCT_WALLCLOCK,
(uint64_t)wallclock.tv_sec * 1000000 +
wallclock.tv_usec);
wallclock.tv_usec, 0);
mtx_unlock(&racct_lock);
PROC_UNLOCK(p);
}