2007-10-17 14:25:49 +08:00
|
|
|
/*
|
|
|
|
* FLoating proportions
|
|
|
|
*
|
2015-11-16 18:08:45 +08:00
|
|
|
* Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
|
2007-10-17 14:25:49 +08:00
|
|
|
*
|
|
|
|
* This file contains the public data structure and API definitions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINUX_PROPORTIONS_H
|
|
|
|
#define _LINUX_PROPORTIONS_H
|
|
|
|
|
|
|
|
#include <linux/percpu_counter.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/mutex.h>
|
2014-09-08 08:51:30 +08:00
|
|
|
#include <linux/gfp.h>
|
2007-10-17 14:25:49 +08:00
|
|
|
|
|
|
|
struct prop_global {
|
|
|
|
/*
|
|
|
|
* The period over which we differentiate
|
|
|
|
*
|
|
|
|
* period = 2^shift
|
|
|
|
*/
|
|
|
|
int shift;
|
|
|
|
/*
|
|
|
|
* The total event counter aka 'time'.
|
|
|
|
*
|
|
|
|
* Treated as an unsigned long; the lower 'shift - 1' bits are the
|
|
|
|
* counter bits, the remaining upper bits the period counter.
|
|
|
|
*/
|
|
|
|
struct percpu_counter events;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* global proportion descriptor
|
|
|
|
*
|
2015-07-06 21:40:22 +08:00
|
|
|
* this is needed to consistently flip prop_global structures.
|
2007-10-17 14:25:49 +08:00
|
|
|
*/
|
|
|
|
struct prop_descriptor {
|
|
|
|
int index;
|
|
|
|
struct prop_global pg[2];
|
|
|
|
struct mutex mutex; /* serialize the prop_global switch */
|
|
|
|
};
|
|
|
|
|
2014-09-08 08:51:30 +08:00
|
|
|
int prop_descriptor_init(struct prop_descriptor *pd, int shift, gfp_t gfp);
|
2007-10-17 14:25:49 +08:00
|
|
|
void prop_change_shift(struct prop_descriptor *pd, int new_shift);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ----- PERCPU ------
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct prop_local_percpu {
|
|
|
|
/*
|
|
|
|
* the local events counter
|
|
|
|
*/
|
|
|
|
struct percpu_counter events;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* snapshot of the last seen global state
|
|
|
|
*/
|
|
|
|
int shift;
|
|
|
|
unsigned long period;
|
2009-07-25 22:43:30 +08:00
|
|
|
raw_spinlock_t lock; /* protect the snapshot state */
|
2007-10-17 14:25:49 +08:00
|
|
|
};
|
|
|
|
|
2014-09-08 08:51:30 +08:00
|
|
|
int prop_local_init_percpu(struct prop_local_percpu *pl, gfp_t gfp);
|
2007-10-17 14:25:49 +08:00
|
|
|
void prop_local_destroy_percpu(struct prop_local_percpu *pl);
|
|
|
|
void __prop_inc_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl);
|
|
|
|
void prop_fraction_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl,
|
|
|
|
long *numerator, long *denominator);
|
|
|
|
|
|
|
|
static inline
|
|
|
|
void prop_inc_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
__prop_inc_percpu(pd, pl);
|
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
|
|
|
|
2008-04-30 15:54:36 +08:00
|
|
|
/*
|
|
|
|
* Limit the time part in order to ensure there are some bits left for the
|
|
|
|
* cycle counter and fraction multiply.
|
|
|
|
*/
|
2012-01-10 01:53:50 +08:00
|
|
|
#if BITS_PER_LONG == 32
|
2008-04-30 15:54:36 +08:00
|
|
|
#define PROP_MAX_SHIFT (3*BITS_PER_LONG/4)
|
2012-01-10 01:53:50 +08:00
|
|
|
#else
|
|
|
|
#define PROP_MAX_SHIFT (BITS_PER_LONG/2)
|
|
|
|
#endif
|
2008-04-30 15:54:36 +08:00
|
|
|
|
|
|
|
#define PROP_FRAC_SHIFT (BITS_PER_LONG - PROP_MAX_SHIFT - 1)
|
|
|
|
#define PROP_FRAC_BASE (1UL << PROP_FRAC_SHIFT)
|
|
|
|
|
|
|
|
void __prop_inc_percpu_max(struct prop_descriptor *pd,
|
|
|
|
struct prop_local_percpu *pl, long frac);
|
|
|
|
|
|
|
|
|
2007-10-17 14:25:49 +08:00
|
|
|
/*
|
|
|
|
* ----- SINGLE ------
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct prop_local_single {
|
|
|
|
/*
|
|
|
|
* the local events counter
|
|
|
|
*/
|
|
|
|
unsigned long events;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* snapshot of the last seen global state
|
|
|
|
* and a lock protecting this state
|
|
|
|
*/
|
|
|
|
unsigned long period;
|
2008-08-01 20:36:28 +08:00
|
|
|
int shift;
|
2009-07-25 22:43:30 +08:00
|
|
|
raw_spinlock_t lock; /* protect the snapshot state */
|
2007-10-17 14:25:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define INIT_PROP_LOCAL_SINGLE(name) \
|
2009-07-25 22:43:30 +08:00
|
|
|
{ .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
|
2007-10-17 14:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int prop_local_init_single(struct prop_local_single *pl);
|
|
|
|
void prop_local_destroy_single(struct prop_local_single *pl);
|
|
|
|
void __prop_inc_single(struct prop_descriptor *pd, struct prop_local_single *pl);
|
|
|
|
void prop_fraction_single(struct prop_descriptor *pd, struct prop_local_single *pl,
|
|
|
|
long *numerator, long *denominator);
|
|
|
|
|
|
|
|
static inline
|
|
|
|
void prop_inc_single(struct prop_descriptor *pd, struct prop_local_single *pl)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
__prop_inc_single(pd, pl);
|
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _LINUX_PROPORTIONS_H */
|