github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadget-collection/gadgets/top/block-io/gadget.go (about) 1 // Copyright 2019-2022 The Inspektor Gadget authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package biotop 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "strconv" 21 "strings" 22 "time" 23 24 log "github.com/sirupsen/logrus" 25 26 gadgetv1alpha1 "github.com/inspektor-gadget/inspektor-gadget/pkg/apis/gadget/v1alpha1" 27 "github.com/inspektor-gadget/inspektor-gadget/pkg/columns/sort" 28 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-collection/gadgets" 29 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/top" 30 biotoptracer "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/top/block-io/tracer" 31 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/top/block-io/types" 32 ) 33 34 type Trace struct { 35 helpers gadgets.GadgetHelpers 36 37 started bool 38 tracer *biotoptracer.Tracer 39 } 40 41 type TraceFactory struct { 42 gadgets.BaseFactory 43 } 44 45 func NewFactory() gadgets.TraceFactory { 46 return &TraceFactory{ 47 BaseFactory: gadgets.BaseFactory{DeleteTrace: deleteTrace}, 48 } 49 } 50 51 func (f *TraceFactory) Description() string { 52 cols := types.GetColumns() 53 validCols, _ := sort.FilterSortableColumns(cols.ColumnMap, cols.GetColumnNames()) 54 55 t := `biotop shows command generating block I/O, with container details. 56 57 The following parameters are supported: 58 - %s: Output interval, in seconds. (default %d) 59 - %s: Maximum rows to print. (default %d) 60 - %s: The field to sort the results by (%s). (default %s)` 61 return fmt.Sprintf(t, top.IntervalParam, top.IntervalDefault, 62 top.MaxRowsParam, top.MaxRowsDefault, 63 top.SortByParam, strings.Join(validCols, ","), strings.Join(types.SortByDefault, ",")) 64 } 65 66 func (f *TraceFactory) OutputModesSupported() map[gadgetv1alpha1.TraceOutputMode]struct{} { 67 return map[gadgetv1alpha1.TraceOutputMode]struct{}{ 68 gadgetv1alpha1.TraceOutputModeStream: {}, 69 } 70 } 71 72 func deleteTrace(name string, t interface{}) { 73 trace := t.(*Trace) 74 if trace.tracer != nil { 75 trace.tracer.Stop() 76 } 77 } 78 79 func (f *TraceFactory) Operations() map[gadgetv1alpha1.Operation]gadgets.TraceOperation { 80 n := func() interface{} { 81 return &Trace{ 82 helpers: f.Helpers, 83 } 84 } 85 86 return map[gadgetv1alpha1.Operation]gadgets.TraceOperation{ 87 gadgetv1alpha1.OperationStart: { 88 Doc: "Start biotop gadget", 89 Operation: func(name string, trace *gadgetv1alpha1.Trace) { 90 f.LookupOrCreate(name, n).(*Trace).Start(trace) 91 }, 92 }, 93 gadgetv1alpha1.OperationStop: { 94 Doc: "Stop biotop gadget", 95 Operation: func(name string, trace *gadgetv1alpha1.Trace) { 96 f.LookupOrCreate(name, n).(*Trace).Stop(trace) 97 }, 98 }, 99 } 100 } 101 102 func (t *Trace) Start(trace *gadgetv1alpha1.Trace) { 103 if t.started { 104 trace.Status.State = gadgetv1alpha1.TraceStateStarted 105 return 106 } 107 108 traceName := gadgets.TraceName(trace.ObjectMeta.Namespace, trace.ObjectMeta.Name) 109 110 maxRows := top.MaxRowsDefault 111 intervalSeconds := top.IntervalDefault 112 sortBy := types.SortByDefault 113 114 if trace.Spec.Parameters != nil { 115 params := trace.Spec.Parameters 116 var err error 117 118 if val, ok := params[top.MaxRowsParam]; ok { 119 maxRows, err = strconv.Atoi(val) 120 if err != nil { 121 trace.Status.OperationError = fmt.Sprintf("%q is not valid for %q", val, top.MaxRowsParam) 122 return 123 } 124 } 125 126 if val, ok := params[top.IntervalParam]; ok { 127 intervalSeconds, err = strconv.Atoi(val) 128 if err != nil { 129 trace.Status.OperationError = fmt.Sprintf("%q is not valid for %q", val, top.IntervalParam) 130 return 131 } 132 } 133 134 if val, ok := params[top.SortByParam]; ok { 135 sortByColumns := strings.Split(val, ",") 136 137 _, invalidCols := sort.FilterSortableColumns(types.GetColumns().ColumnMap, sortByColumns) 138 if len(invalidCols) > 0 { 139 trace.Status.OperationError = fmt.Sprintf("%q are not valid for %q", strings.Join(invalidCols, ","), top.SortByParam) 140 return 141 } 142 143 sortBy = sortByColumns 144 } 145 } 146 147 mountNsMap, err := t.helpers.TracerMountNsMap(traceName) 148 if err != nil { 149 trace.Status.OperationError = fmt.Sprintf("failed to find tracer's mount ns map: %s", err) 150 return 151 } 152 config := &biotoptracer.Config{ 153 MaxRows: maxRows, 154 Interval: time.Second * time.Duration(intervalSeconds), 155 SortBy: sortBy, 156 MountnsMap: mountNsMap, 157 } 158 159 eventCallback := func(ev *top.Event[types.Stats]) { 160 r, err := json.Marshal(ev) 161 if err != nil { 162 log.Warnf("Gadget %s: Failed to marshall event: %s", trace.Spec.Gadget, err) 163 return 164 } 165 t.helpers.PublishEvent(traceName, string(r)) 166 } 167 168 tracer, err := biotoptracer.NewTracer(config, t.helpers, eventCallback) 169 if err != nil { 170 trace.Status.OperationError = fmt.Sprintf("failed to create tracer: %s", err) 171 return 172 } 173 174 t.tracer = tracer 175 t.started = true 176 177 trace.Status.State = gadgetv1alpha1.TraceStateStarted 178 } 179 180 func (t *Trace) Stop(trace *gadgetv1alpha1.Trace) { 181 if !t.started { 182 trace.Status.OperationError = "Not started" 183 return 184 } 185 186 t.tracer.Stop() 187 t.tracer = nil 188 t.started = false 189 190 trace.Status.State = gadgetv1alpha1.TraceStateStopped 191 }