blob: 21221338ad18018b9b8c479efe1f938b67e6f685 [file] [log] [blame]
Peter Zijlstra145ca252007-10-16 23:25:49 -07001/*
2 * FLoating proportions
3 *
Peter Zijlstra90eec102015-11-16 11:08:45 +01004 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
Peter Zijlstra145ca252007-10-16 23:25:49 -07005 *
6 * This file contains the public data structure and API definitions.
7 */
8
9#ifndef _LINUX_PROPORTIONS_H
10#define _LINUX_PROPORTIONS_H
11
12#include <linux/percpu_counter.h>
13#include <linux/spinlock.h>
14#include <linux/mutex.h>
Tejun Heo20ae0072014-09-08 09:51:30 +090015#include <linux/gfp.h>
Peter Zijlstra145ca252007-10-16 23:25:49 -070016
17struct prop_global {
18 /*
19 * The period over which we differentiate
20 *
21 * period = 2^shift
22 */
23 int shift;
24 /*
25 * The total event counter aka 'time'.
26 *
27 * Treated as an unsigned long; the lower 'shift - 1' bits are the
28 * counter bits, the remaining upper bits the period counter.
29 */
30 struct percpu_counter events;
31};
32
33/*
34 * global proportion descriptor
35 *
Geert Uytterhoeven377a51a2015-07-06 15:40:22 +020036 * this is needed to consistently flip prop_global structures.
Peter Zijlstra145ca252007-10-16 23:25:49 -070037 */
38struct prop_descriptor {
39 int index;
40 struct prop_global pg[2];
41 struct mutex mutex; /* serialize the prop_global switch */
42};
43
Tejun Heo20ae0072014-09-08 09:51:30 +090044int prop_descriptor_init(struct prop_descriptor *pd, int shift, gfp_t gfp);
Peter Zijlstra145ca252007-10-16 23:25:49 -070045void prop_change_shift(struct prop_descriptor *pd, int new_shift);
46
47/*
48 * ----- PERCPU ------
49 */
50
51struct prop_local_percpu {
52 /*
53 * the local events counter
54 */
55 struct percpu_counter events;
56
57 /*
58 * snapshot of the last seen global state
59 */
60 int shift;
61 unsigned long period;
Thomas Gleixner740969f2009-07-25 16:43:30 +020062 raw_spinlock_t lock; /* protect the snapshot state */
Peter Zijlstra145ca252007-10-16 23:25:49 -070063};
64
Tejun Heo20ae0072014-09-08 09:51:30 +090065int prop_local_init_percpu(struct prop_local_percpu *pl, gfp_t gfp);
Peter Zijlstra145ca252007-10-16 23:25:49 -070066void prop_local_destroy_percpu(struct prop_local_percpu *pl);
67void __prop_inc_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl);
68void prop_fraction_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl,
69 long *numerator, long *denominator);
70
71static inline
72void prop_inc_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl)
73{
74 unsigned long flags;
75
76 local_irq_save(flags);
77 __prop_inc_percpu(pd, pl);
78 local_irq_restore(flags);
79}
80
81/*
Peter Zijlstraa42dde02008-04-30 00:54:36 -070082 * Limit the time part in order to ensure there are some bits left for the
83 * cycle counter and fraction multiply.
84 */
Wu Fengguang33102252012-01-09 11:53:50 -060085#if BITS_PER_LONG == 32
Peter Zijlstraa42dde02008-04-30 00:54:36 -070086#define PROP_MAX_SHIFT (3*BITS_PER_LONG/4)
Wu Fengguang33102252012-01-09 11:53:50 -060087#else
88#define PROP_MAX_SHIFT (BITS_PER_LONG/2)
89#endif
Peter Zijlstraa42dde02008-04-30 00:54:36 -070090
91#define PROP_FRAC_SHIFT (BITS_PER_LONG - PROP_MAX_SHIFT - 1)
92#define PROP_FRAC_BASE (1UL << PROP_FRAC_SHIFT)
93
94void __prop_inc_percpu_max(struct prop_descriptor *pd,
95 struct prop_local_percpu *pl, long frac);
96
97
98/*
Peter Zijlstra145ca252007-10-16 23:25:49 -070099 * ----- SINGLE ------
100 */
101
102struct prop_local_single {
103 /*
104 * the local events counter
105 */
106 unsigned long events;
107
108 /*
109 * snapshot of the last seen global state
110 * and a lock protecting this state
111 */
Peter Zijlstra145ca252007-10-16 23:25:49 -0700112 unsigned long period;
Richard Kennedy3fb669d2008-08-01 13:36:28 +0100113 int shift;
Thomas Gleixner740969f2009-07-25 16:43:30 +0200114 raw_spinlock_t lock; /* protect the snapshot state */
Peter Zijlstra145ca252007-10-16 23:25:49 -0700115};
116
117#define INIT_PROP_LOCAL_SINGLE(name) \
Thomas Gleixner740969f2009-07-25 16:43:30 +0200118{ .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
Peter Zijlstra145ca252007-10-16 23:25:49 -0700119}
120
121int prop_local_init_single(struct prop_local_single *pl);
122void prop_local_destroy_single(struct prop_local_single *pl);
123void __prop_inc_single(struct prop_descriptor *pd, struct prop_local_single *pl);
124void prop_fraction_single(struct prop_descriptor *pd, struct prop_local_single *pl,
125 long *numerator, long *denominator);
126
127static inline
128void prop_inc_single(struct prop_descriptor *pd, struct prop_local_single *pl)
129{
130 unsigned long flags;
131
132 local_irq_save(flags);
133 __prop_inc_single(pd, pl);
134 local_irq_restore(flags);
135}
136
137#endif /* _LINUX_PROPORTIONS_H */