Skip to content

Commit 29be713

Browse files
committed
fpga_plugin: use time.Ticker instead of time.Sleep
Signed-off-by: Ed Bartosh <eduard.bartosh@intel.com>
1 parent f4908a7 commit 29be713

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

cmd/fpga_plugin/fpga_plugin.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const (
4848
// When the device's firmware crashes the driver reports these values
4949
unhealthyAfuID = "ffffffffffffffffffffffffffffffff"
5050
unhealthyInterfaceID = "ffffffffffffffffffffffffffffffff"
51+
52+
// Frequency of device scans
53+
scanFrequency = 5 * time.Second
5154
)
5255

5356
type getDevTreeFunc func(devices []device) dpapi.DeviceTree
@@ -172,17 +175,33 @@ type devicePlugin struct {
172175
ignoreAfuIDs bool
173176
ignoreEmptyRegions bool
174177
annotationValue string
178+
179+
scanTicker *time.Ticker
180+
scanDone chan bool
175181
}
176182

177183
// newDevicePlugin returns new instance of devicePlugin
178184
func newDevicePlugin(mode string) (*devicePlugin, error) {
185+
var dp *devicePlugin
186+
var err error
187+
179188
if _, err := os.Stat(sysfsDirectoryOPAE); os.IsNotExist(err) {
180189
if _, err = os.Stat(sysfsDirectoryDFL); os.IsNotExist(err) {
181190
return nil, fmt.Errorf("kernel driver is not loaded: neither %s nor %s sysfs entry exists", sysfsDirectoryOPAE, sysfsDirectoryDFL)
182191
}
183-
return newDevicePluginDFL(sysfsDirectoryDFL, devfsDirectory, mode)
192+
dp, err = newDevicePluginDFL(sysfsDirectoryDFL, devfsDirectory, mode)
193+
} else {
194+
dp, err = newDevicePluginOPAE(sysfsDirectoryOPAE, devfsDirectory, mode)
184195
}
185-
return newDevicePluginOPAE(sysfsDirectoryOPAE, devfsDirectory, mode)
196+
197+
if err != nil {
198+
return nil, err
199+
}
200+
201+
dp.scanTicker = time.NewTicker(scanFrequency)
202+
dp.scanDone = make(chan bool, 1) // buffered as we may send to it before Scan starts receiving from it
203+
204+
return dp, nil
186205
}
187206

188207
func (dp *devicePlugin) PostAllocate(response *pluginapi.AllocateResponse) error {
@@ -200,6 +219,7 @@ func (dp *devicePlugin) PostAllocate(response *pluginapi.AllocateResponse) error
200219

201220
// Scan starts scanning FPGA devices on the host
202221
func (dp *devicePlugin) Scan(notifier dpapi.Notifier) error {
222+
defer dp.scanTicker.Stop()
203223
for {
204224
devTree, err := dp.scanFPGAs()
205225
if err != nil {
@@ -208,7 +228,11 @@ func (dp *devicePlugin) Scan(notifier dpapi.Notifier) error {
208228

209229
notifier.Notify(devTree)
210230

211-
time.Sleep(5 * time.Second)
231+
select {
232+
case <-dp.scanDone:
233+
return nil
234+
case <-dp.scanTicker.C:
235+
}
212236
}
213237
}
214238

0 commit comments

Comments
 (0)