Skip to content

Commit 3819d92

Browse files
csweichelroboquat
authored andcommitted
[ws-daemon] Fix CPU limit annotation
1 parent 2f52899 commit 3819d92

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

components/ws-daemon/pkg/cpulimit/cpulimit.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type Workspace struct {
1818
NrThrottled uint64
1919
Usage CPUTime
2020
QoS int
21+
22+
BaseLimit Bandwidth
23+
BurstLimit Bandwidth
2124
}
2225

2326
type WorkspaceHistory struct {
@@ -113,7 +116,7 @@ func (d *Distributor) Tick(dt time.Duration) (DistributorDebug, error) {
113116
return DistributorDebug{}, err
114117
}
115118

116-
f := make(map[string]struct{}, len(ws))
119+
wsidx := make(map[string]Workspace, len(ws))
117120
for _, w := range ws {
118121
h, ok := d.History[w.ID]
119122
if !ok {
@@ -123,10 +126,10 @@ func (d *Distributor) Tick(dt time.Duration) (DistributorDebug, error) {
123126
d.History[w.ID] = h
124127
}
125128
h.Update(w)
126-
f[w.ID] = struct{}{}
129+
wsidx[w.ID] = w
127130
}
128131
for oldWS := range d.History {
129-
if _, found := f[oldWS]; !found {
132+
if _, found := wsidx[oldWS]; !found {
130133
delete(d.History, oldWS)
131134
}
132135
}
@@ -172,15 +175,23 @@ func (d *Distributor) Tick(dt time.Duration) (DistributorDebug, error) {
172175
var burstBandwidth Bandwidth
173176
for _, id := range wsOrder {
174177
ws := d.History[id]
175-
limit := d.Limiter.Limit(ws.Usage())
178+
limiter := d.Limiter
179+
if w := wsidx[id]; w.BaseLimit > 0 {
180+
limiter = FixedLimiter(w.BaseLimit)
181+
}
182+
limit := limiter.Limit(ws.Usage())
176183

177184
// if we didn't get the max bandwidth, but were throttled last time
178185
// and there's still some bandwidth left to give, let's act as if had
179186
// never spent any CPU time and assume the workspace will spend their
180187
// entire bandwidth at once.
181188
var burst bool
182189
if totalBandwidth < d.TotalBandwidth && ws.Throttled() {
183-
limit = d.BurstLimiter.Limit(ws.Usage())
190+
limiter := d.BurstLimiter
191+
if w := wsidx[id]; w.BaseLimit > 0 {
192+
limiter = FixedLimiter(w.BurstLimit)
193+
}
194+
limit = limiter.Limit(ws.Usage())
184195

185196
// We assume the workspace is going to use as much as their limit allows.
186197
// This might not be true, because their process which consumed so much CPU

components/ws-daemon/pkg/cpulimit/dispatch.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ type DispatchListener struct {
9898
}
9999

100100
type workspace struct {
101-
CFS CFSController
102-
OWI logrus.Fields
103-
HardLimit ResourceLimiter
101+
CFS CgroupCFSController
102+
OWI logrus.Fields
103+
BaseLimit Bandwidth
104+
BurstLimit Bandwidth
105+
HardLimit ResourceLimiter
104106

105107
lastThrottled uint64
106108
}
@@ -135,6 +137,8 @@ func (d *DispatchListener) source(context.Context) ([]Workspace, error) {
135137
ID: id,
136138
NrThrottled: throttled,
137139
Usage: usage,
140+
BaseLimit: w.BaseLimit,
141+
BurstLimit: w.BurstLimit,
138142
})
139143
}
140144
return res, nil
@@ -215,7 +219,8 @@ func (d *DispatchListener) WorkspaceUpdated(ctx context.Context, ws *dispatch.Wo
215219
if err != nil {
216220
return xerrors.Errorf("cannot enforce fixed CPU limit: %w", err)
217221
}
218-
wsinfo.HardLimit = FixedLimiter(BandwidthFromQuantity(limit))
222+
wsinfo.BaseLimit = BandwidthFromQuantity(limit)
223+
wsinfo.BurstLimit = BandwidthFromQuantity(limit)
219224
}
220225

221226
return nil

0 commit comments

Comments
 (0)