-
Notifications
You must be signed in to change notification settings - Fork 291
/
scm.go
662 lines (554 loc) · 19 KB
/
scm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//
// (C) Copyright 2021-2024 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package storage
import (
"bytes"
"fmt"
"sort"
"github.com/dustin/go-humanize"
"github.com/pkg/errors"
"github.com/daos-stack/daos/src/control/common"
"github.com/daos-stack/daos/src/control/lib/ranklist"
"github.com/daos-stack/daos/src/control/logging"
"github.com/daos-stack/daos/src/control/pbin"
)
// ScmState represents the probed state of PMem modules on the system.
//
//go:generate stringer -type=ScmState
type ScmState int
const (
// ScmStateUnknown represents the default (unknown) state.
ScmStateUnknown ScmState = iota
// ScmNoRegions indicates that PMem modules exist, but no regions have been created.
ScmNoRegions
// ScmFreeCap indicates that PMem AppDirect regions have free capacity.
ScmFreeCap
// ScmNoFreeCap indicates that PMem AppDirect regions have no free capacity.
ScmNoFreeCap
// ScmNotInterleaved indicates that a PMem AppDirect region is in non-interleaved mode.
ScmNotInterleaved
// ScmNoModules indicates that no PMem modules exist.
ScmNoModules
// ScmNotHealthy indicates a PMem AppDirect region is showing health state as "Error".
ScmNotHealthy
// ScmPartFreeCap indicates a PMem AppDirect region has only partial free capacity.
ScmPartFreeCap
// ScmUnknownMode indicates a pMem AppDirect region is in an unsupported memory mode.
ScmUnknownMode
// MinRamdiskMem is the minimum amount of memory needed for each engine's tmpfs RAM-disk.
MinRamdiskMem = humanize.GiByte * 4
)
// Memory reservation constant defaults to be used when calculating RAM-disk size for DAOS I/O engine.
const (
DefaultSysMemRsvd = humanize.GiByte * 16 // per-system
DefaultTgtMemRsvd = humanize.MiByte * 128 // per-engine-target
DefaultEngineMemRsvd = humanize.GiByte * 1 // per-engine
)
func (ss ScmState) String() string {
if val, exists := map[ScmState]string{
ScmStateUnknown: "Unknown",
ScmNoRegions: "NoRegions",
ScmFreeCap: "FreeCapacity",
ScmNoFreeCap: "NoFreeCapacity",
ScmNotInterleaved: "NotInterleaved",
ScmNoModules: "NoModules",
ScmNotHealthy: "NotHealthy",
ScmPartFreeCap: "PartialFreeCapacity",
ScmUnknownMode: "UnknownMode",
}[ss]; exists {
return val
}
return "Unknown"
}
type (
// ScmSocketState indicates the state of PMem for either a specific socket or all sockets.
ScmSocketState struct {
SocketID *uint // If set, state applies to a specific socket.
State ScmState
}
// ScmModule represents a PMem DIMM.
//
// This is a simplified representation of the raw struct used in the ipmctl package.
ScmModule struct {
ChannelID uint32
ChannelPosition uint32
ControllerID uint32
SocketID uint32
PhysicalID uint32
Capacity uint64
UID string
PartNumber string
FirmwareRevision string
HealthState string
}
// ScmModules is a type alias for []ScmModule that implements fmt.Stringer.
ScmModules []*ScmModule
// ScmMountPoint represents location PMem filesystem is mounted.
ScmMountPoint struct {
Class Class `json:"class"`
DeviceList []string `json:"device_list"`
Info string `json:"info"`
Path string `json:"path"`
Rank ranklist.Rank `json:"rank"`
TotalBytes uint64 `json:"total_bytes"`
AvailBytes uint64 `json:"avail_bytes"`
UsableBytes uint64 `json:"usable_bytes"`
}
// ScmMountPoints is a type alias for []ScmMountPoint that implements fmt.Stringer.
ScmMountPoints []*ScmMountPoint
// ScmNamespace is a block device exposing a PMem AppDirect region.
ScmNamespace struct {
UUID string `json:"uuid" hash:"ignore"`
BlockDevice string `json:"blockdev"`
Name string `json:"dev"`
NumaNode uint32 `json:"numa_node"`
Size uint64 `json:"size"`
Mount *ScmMountPoint `json:"mount"`
}
// ScmNamespaces is a type alias for a slice of ScmNamespace references.
ScmNamespaces []*ScmNamespace
// ScmFirmwareUpdateStatus represents the status of a firmware update on the module.
ScmFirmwareUpdateStatus uint32
// ScmFirmwareInfo describes the firmware information of an PMem module.
ScmFirmwareInfo struct {
ActiveVersion string
StagedVersion string
ImageMaxSizeBytes uint32
UpdateStatus ScmFirmwareUpdateStatus
}
)
const (
// ScmUpdateStatusUnknown indicates that the firmware update status is unknown.
ScmUpdateStatusUnknown ScmFirmwareUpdateStatus = iota
// ScmUpdateStatusStaged indicates that a new firmware version has been staged.
ScmUpdateStatusStaged
// ScmUpdateStatusSuccess indicates that the firmware update was successfully applied.
ScmUpdateStatusSuccess
// ScmUpdateStatusFailed indicates that the firmware update failed.
ScmUpdateStatusFailed
)
// String translates the update status to a string
func (s ScmFirmwareUpdateStatus) String() string {
switch s {
case ScmUpdateStatusStaged:
return "Staged"
case ScmUpdateStatusSuccess:
return "Success"
case ScmUpdateStatusFailed:
return "Failed"
}
return "Unknown"
}
func (sm *ScmModule) String() string {
health := ""
if sm.HealthState != "" {
health = fmt.Sprintf(" Health:%s", sm.HealthState)
}
// capacity given in IEC standard units.
return fmt.Sprintf("UID:%s PhysicalID:%d Capacity:%s Location:(socket:%d memctrlr:%d "+
"chan:%d pos:%d)%s", sm.UID, sm.PhysicalID, humanize.IBytes(sm.Capacity),
sm.SocketID, sm.ControllerID, sm.ChannelID, sm.ChannelPosition, health)
}
func (sms ScmModules) String() string {
var buf bytes.Buffer
if len(sms) == 0 {
return "\tnone\n"
}
sort.Slice(sms, func(i, j int) bool { return sms[i].PhysicalID < sms[j].PhysicalID })
fmt.Fprintf(&buf, "\n")
for _, sm := range sms {
fmt.Fprintf(&buf, "\t%s\n", sm)
}
return buf.String()
}
// Capacity reports total storage capacity (bytes) across all modules.
func (sms ScmModules) Capacity() (tb uint64) {
for _, sm := range sms {
tb += sm.Capacity
}
return
}
// Summary reports total storage space and the number of modules.
//
// Capacity given in IEC standard units.
func (sms ScmModules) Summary() string {
return fmt.Sprintf("%s (%d %s)", humanize.IBytes(sms.Capacity()), len(sms),
common.Pluralise("module", len(sms)))
}
// Capacity reports total storage capacity (bytes) of PMem namespace (pmem block device).
func (sn ScmNamespace) Capacity() uint64 {
return sn.Size
}
// Total returns the total bytes on mounted PMem namespace as reported by OS.
func (sn ScmNamespace) Total() uint64 {
if sn.Mount == nil {
return 0
}
return sn.Mount.TotalBytes
}
// Free returns the available free bytes on mounted PMem namespace as reported by OS.
func (sn ScmNamespace) Free() uint64 {
if sn.Mount == nil {
return 0
}
return sn.Mount.AvailBytes
}
// Free returns the available free bytes on mounted PMem namespace as reported by OS.
func (sn ScmNamespace) Usable() uint64 {
if sn.Mount == nil {
return 0
}
return sn.Mount.UsableBytes
}
func (sn *ScmNamespace) String() string {
mountInfo := ""
if sn.Mount != nil {
mountInfo = fmt.Sprintf(" Mount:%+v", *sn.Mount)
}
// capacity given in IEC standard units.
return fmt.Sprintf("UUID:%s BlockDev:%s Name:%s NUMA:%d Size:%s%s",
sn.UUID, sn.BlockDevice, sn.Name, sn.NumaNode, humanize.IBytes(sn.Size), mountInfo)
}
func (sns ScmNamespaces) String() string {
var buf bytes.Buffer
if len(sns) == 0 {
return "\tnone\n"
}
sort.Slice(sns, func(i, j int) bool { return sns[i].Name < sns[j].Name })
fmt.Fprintf(&buf, "\n")
for _, sn := range sns {
fmt.Fprintf(&buf, "\t%s\n", sn)
}
return buf.String()
}
// Capacity reports total storage capacity (bytes) across all namespaces.
func (sns ScmNamespaces) Capacity() (tb uint64) {
for _, sn := range sns {
tb += (*ScmNamespace)(sn).Capacity()
}
return
}
// Total returns the cumulative total bytes on all mounted PMem namespaces.
func (sns ScmNamespaces) Total() (tb uint64) {
for _, sn := range sns {
tb += (*ScmNamespace)(sn).Total()
}
return
}
// Free returns the cumulative available bytes on all mounted PMem namespaces.
func (sns ScmNamespaces) Free() (tb uint64) {
for _, sn := range sns {
tb += (*ScmNamespace)(sn).Free()
}
return
}
// Free returns the cumulative effective available bytes on all mounted PMem namespaces.
func (sns ScmNamespaces) Usable() (tb uint64) {
for _, sn := range sns {
tb += (*ScmNamespace)(sn).Usable()
}
return
}
// PercentUsage returns the percentage of used storage space.
func (sns ScmNamespaces) PercentUsage() string {
return common.PercentageString(sns.Total()-sns.Free(), sns.Total())
}
// Summary reports total storage space and the number of namespaces.
//
// Capacity given in IEC standard units.
func (sns ScmNamespaces) Summary() string {
return fmt.Sprintf("%s (%d %s)", humanize.Bytes(sns.Capacity()), len(sns),
common.Pluralise("namespace", len(sns)))
}
const (
ScmMsgRebootRequired = "A reboot is required to process new PMem memory allocation goals."
ScmMsgNotInited = "PMem storage could not be accessed"
ScmMsgClassNotSupported = "operation unsupported on PMem class"
ScmMsgIpmctlDiscoverFail = "ipmctl module discovery"
)
type (
// ScmProvider defines an interface to be implemented by a PMem provider.
ScmProvider interface {
Mount(ScmMountRequest) (*MountResponse, error)
Unmount(ScmMountRequest) (*MountResponse, error)
Format(ScmFormatRequest) (*ScmFormatResponse, error)
CheckFormat(ScmFormatRequest) (*ScmFormatResponse, error)
Scan(ScmScanRequest) (*ScmScanResponse, error)
Prepare(ScmPrepareRequest) (*ScmPrepareResponse, error)
QueryFirmware(ScmFirmwareQueryRequest) (*ScmFirmwareQueryResponse, error)
UpdateFirmware(ScmFirmwareUpdateRequest) (*ScmFirmwareUpdateResponse, error)
}
// ScmPrepareRequest defines the parameters for a Prepare operation.
ScmPrepareRequest struct {
pbin.ForwardableRequest
Reset bool // Clear PMem namespaces and regions.
NrNamespacesPerSocket uint // Request this many PMem namespaces per socket.
SocketID *uint // Only process PMem attached to this socket.
}
// ScmPrepareResponse contains the results of a successful Prepare operation.
ScmPrepareResponse struct {
Socket *ScmSocketState
RebootRequired bool
Namespaces ScmNamespaces
}
// ScmScanRequest defines the parameters for a Scan operation.
ScmScanRequest struct {
pbin.ForwardableRequest
SocketID *uint // Only process PMem attached to this socket.
}
// ScmScanResponse contains information gleaned during a successful Scan operation.
ScmScanResponse struct {
Modules ScmModules
Namespaces ScmNamespaces
}
// RamdiskParams defines the sub-parameters of a Format or Mount operation that
// will use tmpfs-based ramdisk
RamdiskParams struct {
Size uint
NUMANode uint
DisableHugepages bool
}
// ScmFormatRequest defines the parameters for a Format operation or query.
ScmFormatRequest struct {
pbin.ForwardableRequest
Force bool
Mountpoint string
OwnerUID int
OwnerGID int
Ramdisk *RamdiskParams
Dcpm *DeviceParams
}
// ScmFormatResponse contains the results of a successful Format operation or query.
ScmFormatResponse struct {
Mountpoint string
Formatted bool
Mounted bool
Mountable bool
}
// ScmMountRequest represents an SCM mount request.
ScmMountRequest struct {
pbin.ForwardableRequest
Class Class
Device string
Target string
Ramdisk *RamdiskParams
}
// ScmFirmwareQueryRequest defines the parameters for a firmware query.
ScmFirmwareQueryRequest struct {
pbin.ForwardableRequest
DeviceUIDs []string // requested device UIDs, empty for all
ModelID string // filter by model ID
FirmwareRev string // filter by current FW revision
}
// ScmModuleFirmware represents the results of a firmware query for a specific
// PMem module.
ScmModuleFirmware struct {
Module ScmModule
Info *ScmFirmwareInfo
Error string
}
// ScmFirmwareQueryResponse contains the results of a successful firmware query.
ScmFirmwareQueryResponse struct {
Results []ScmModuleFirmware
}
// ScmFirmwareUpdateRequest defines the parameters for a firmware update.
ScmFirmwareUpdateRequest struct {
pbin.ForwardableRequest
DeviceUIDs []string // requested device UIDs, empty for all
FirmwarePath string // location of the firmware binary
ModelID string // filter devices by model ID
FirmwareRev string // filter devices by current FW revision
}
// ScmFirmwareUpdateResult represents the result of a firmware update for
// a specific PMem module.
ScmFirmwareUpdateResult struct {
Module ScmModule
Error string
}
// ScmFirmwareUpdateResponse contains the results of the firmware update.
ScmFirmwareUpdateResponse struct {
Results []ScmFirmwareUpdateResult
}
)
type ScmForwarder struct {
ScmAdminForwarder
ScmFwForwarder
}
func NewScmForwarder(log logging.Logger) *ScmForwarder {
return &ScmForwarder{
ScmAdminForwarder: *NewScmAdminForwarder(log),
ScmFwForwarder: *NewScmFwForwarder(log),
}
}
// ScmAdminForwarder forwards requests to the DAOS admin binary.
type ScmAdminForwarder struct {
pbin.Forwarder
}
// NewScmAdminForwarder creates a new ScmAdminForwarder.
func NewScmAdminForwarder(log logging.Logger) *ScmAdminForwarder {
pf := pbin.NewForwarder(log, pbin.DaosPrivHelperName)
return &ScmAdminForwarder{
Forwarder: *pf,
}
}
// Mount forwards an SCM mount request.
func (f *ScmAdminForwarder) Mount(req ScmMountRequest) (*MountResponse, error) {
req.Forwarded = true
res := new(MountResponse)
if err := f.SendReq("ScmMount", req, res); err != nil {
return nil, err
}
return res, nil
}
// Unmount forwards an SCM unmount request.
func (f *ScmAdminForwarder) Unmount(req ScmMountRequest) (*MountResponse, error) {
req.Forwarded = true
res := new(MountResponse)
if err := f.SendReq("ScmUnmount", req, res); err != nil {
return nil, err
}
return res, nil
}
// Format forwards a request request to format SCM.
func (f *ScmAdminForwarder) Format(req ScmFormatRequest) (*ScmFormatResponse, error) {
req.Forwarded = true
res := new(ScmFormatResponse)
if err := f.SendReq("ScmFormat", req, res); err != nil {
return nil, err
}
return res, nil
}
// CheckFormat forwards a request to check the SCM formatting.
func (f *ScmAdminForwarder) CheckFormat(req ScmFormatRequest) (*ScmFormatResponse, error) {
req.Forwarded = true
res := new(ScmFormatResponse)
if err := f.SendReq("ScmCheckFormat", req, res); err != nil {
return nil, err
}
return res, nil
}
// Scan forwards an SCM scan request.
func (f *ScmAdminForwarder) Scan(req ScmScanRequest) (*ScmScanResponse, error) {
req.Forwarded = true
res := new(ScmScanResponse)
if err := f.SendReq("ScmScan", req, res); err != nil {
return nil, err
}
return res, nil
}
// Prepare forwards a request to prep the SCM.
func (f *ScmAdminForwarder) Prepare(req ScmPrepareRequest) (*ScmPrepareResponse, error) {
req.Forwarded = true
res := new(ScmPrepareResponse)
if err := f.SendReq("ScmPrepare", req, res); err != nil {
return nil, err
}
return res, nil
}
const (
// ScmFirmwareQueryMethod is the method name used when forwarding the request
// to query SCM firmware.
ScmFirmwareQueryMethod = "ScmFirmwareQuery"
// ScmFirmwareUpdateMethod is the method name used when forwarding the request
// to update SCM firmware.
ScmFirmwareUpdateMethod = "ScmFirmwareUpdate"
)
// ScmFwForwarder forwards firmware requests to a privileged binary.
type ScmFwForwarder struct {
pbin.Forwarder
}
// NewScmFwForwarder returns a new ScmFwForwarder.
func NewScmFwForwarder(log logging.Logger) *ScmFwForwarder {
pf := pbin.NewForwarder(log, pbin.DaosFWName)
return &ScmFwForwarder{
Forwarder: *pf,
}
}
// checkSupport verifies that the firmware support binary is installed.
func (f *ScmFwForwarder) checkSupport() error {
if f.Forwarder.CanForward() {
return nil
}
return errors.Errorf("SCM firmware operations are not supported on this system")
}
// Query forwards an SCM firmware query request.
func (f *ScmFwForwarder) QueryFirmware(req ScmFirmwareQueryRequest) (*ScmFirmwareQueryResponse, error) {
if err := f.checkSupport(); err != nil {
return nil, err
}
req.Forwarded = true
res := new(ScmFirmwareQueryResponse)
if err := f.SendReq(ScmFirmwareQueryMethod, req, res); err != nil {
return nil, err
}
return res, nil
}
// Update forwards a request to update firmware on the SCM.
func (f *ScmFwForwarder) UpdateFirmware(req ScmFirmwareUpdateRequest) (*ScmFirmwareUpdateResponse, error) {
if err := f.checkSupport(); err != nil {
return nil, err
}
req.Forwarded = true
res := new(ScmFirmwareUpdateResponse)
if err := f.SendReq(ScmFirmwareUpdateMethod, req, res); err != nil {
return nil, err
}
return res, nil
}
// CalcRamdiskSize returns recommended tmpfs RAM-disk size calculated as
// (total mem - hugepage mem - sys rsvd mem - engine rsvd mem) / nr engines.
// All values in units of bytes and return value is for a single RAM-disk/engine.
func CalcRamdiskSize(log logging.Logger, memTotal, memHuge, memSys uint64, tgtCount, engCount int) (uint64, error) {
if memTotal == 0 {
return 0, errors.New("requires nonzero total mem")
}
if tgtCount <= 0 {
return 0, errors.New("requires positive nonzero nr engine targets")
}
if engCount <= 0 {
return 0, errors.New("requires positive nonzero nr engines")
}
memEng := uint64(tgtCount) * DefaultTgtMemRsvd
if memEng < DefaultEngineMemRsvd {
memEng = DefaultEngineMemRsvd
}
msgStats := fmt.Sprintf("mem stats: total %s (%d) - (hugepages %s + sys rsvd %s + "+
"(engine rsvd %s * nr engines %d). %d tgts-per-engine)", humanize.IBytes(memTotal),
memTotal, humanize.IBytes(memHuge), humanize.IBytes(memSys),
humanize.IBytes(memEng), engCount, tgtCount)
memRsvd := memHuge + memSys + (memEng * uint64(engCount))
if memTotal < memRsvd {
return 0, errors.Errorf("insufficient ram to meet minimum requirements (%s)",
msgStats)
}
ramdiskSize := (memTotal - memRsvd) / uint64(engCount)
log.Debugf("ram-disk size %s calculated using %s", humanize.IBytes(ramdiskSize), msgStats)
return ramdiskSize, nil
}
// CalcMemForRamdiskSize returns the minimum RAM required for the input requested RAM-disk size.
func CalcMemForRamdiskSize(log logging.Logger, ramdiskSize, memHuge, memSys uint64, tgtCount, engCount int) (uint64, error) {
if ramdiskSize == 0 {
return 0, errors.New("requires nonzero ram-disk size")
}
if tgtCount <= 0 {
return 0, errors.New("requires positive nonzero nr engine targets")
}
if engCount == 0 {
return 0, errors.New("requires nonzero nr engines")
}
memEng := uint64(tgtCount) * DefaultTgtMemRsvd
if memEng < DefaultEngineMemRsvd {
memEng = DefaultEngineMemRsvd
}
msgStats := fmt.Sprintf("required ram-disk size %s (%d). mem hugepage: %s, nr engines: %d, "+
"sys mem rsvd: %s, engine mem rsvd: %s, %d tgts-per-engine",
humanize.IBytes(ramdiskSize), ramdiskSize, humanize.IBytes(memHuge), engCount,
humanize.IBytes(memSys), humanize.IBytes(memEng), tgtCount)
memRsvd := memHuge + memSys + (memEng * uint64(engCount))
memReqd := memRsvd + (ramdiskSize * uint64(engCount))
log.Debugf("%s RAM needed for %s", humanize.IBytes(memReqd), msgStats)
return memReqd, nil
}