|
| 1 | +package socpower |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/macadmins/osquery-extension/pkg/utils" |
| 10 | + "github.com/micromdm/plist" |
| 11 | + "github.com/osquery/osquery-go/plugin/table" |
| 12 | + "github.com/pkg/errors" |
| 13 | +) |
| 14 | + |
| 15 | +const defaultInterval = 3000 |
| 16 | + |
| 17 | +type powermetricsOutput struct { |
| 18 | + Processor struct { |
| 19 | + CPUPower float64 `plist:"cpu_power"` |
| 20 | + GPUPower float64 `plist:"gpu_power"` |
| 21 | + ANEPower float64 `plist:"ane_power"` |
| 22 | + CombinedPower float64 `plist:"combined_power"` |
| 23 | + } `plist:"processor"` |
| 24 | + GPU struct { |
| 25 | + IdleRatio float64 `plist:"idle_ratio"` |
| 26 | + } `plist:"gpu"` |
| 27 | +} |
| 28 | + |
| 29 | +func SocPowerColumns() []table.ColumnDefinition { |
| 30 | + return []table.ColumnDefinition{ |
| 31 | + table.TextColumn("cpu_power_mw"), |
| 32 | + table.TextColumn("gpu_power_mw"), |
| 33 | + table.TextColumn("ane_power_mw"), |
| 34 | + table.TextColumn("combined_power_mw"), |
| 35 | + table.TextColumn("gpu_active_ratio"), |
| 36 | + table.IntegerColumn("interval"), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func SocPowerGenerate(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) { |
| 41 | + interval := defaultInterval |
| 42 | + if constraintList, present := queryContext.Constraints["interval"]; present { |
| 43 | + for _, constraint := range constraintList.Constraints { |
| 44 | + if constraint.Operator == table.OperatorEquals { |
| 45 | + if parsed, err := strconv.Atoi(constraint.Expression); err == nil { |
| 46 | + interval = parsed |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + r := utils.NewRunner() |
| 53 | + fs := utils.OSFileSystem{} |
| 54 | + result, err := runPowermetrics(r, fs, interval) |
| 55 | + if err != nil { |
| 56 | + fmt.Println(err) |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + if result == nil { |
| 60 | + return nil, nil |
| 61 | + } |
| 62 | + |
| 63 | + gpuActiveRatio := 1.0 - result.GPU.IdleRatio |
| 64 | + |
| 65 | + return []map[string]string{{ |
| 66 | + "cpu_power_mw": fmt.Sprintf("%.2f", result.Processor.CPUPower), |
| 67 | + "gpu_power_mw": fmt.Sprintf("%.2f", result.Processor.GPUPower), |
| 68 | + "ane_power_mw": fmt.Sprintf("%.2f", result.Processor.ANEPower), |
| 69 | + "combined_power_mw": fmt.Sprintf("%.2f", result.Processor.CombinedPower), |
| 70 | + "gpu_active_ratio": fmt.Sprintf("%.4f", gpuActiveRatio), |
| 71 | + "interval": strconv.Itoa(interval), |
| 72 | + }}, nil |
| 73 | +} |
| 74 | + |
| 75 | +func runPowermetrics(r utils.Runner, fs utils.FileSystem, interval int) (*powermetricsOutput, error) { |
| 76 | + _, err := fs.Stat("/usr/bin/powermetrics") |
| 77 | + if err != nil { |
| 78 | + if errors.Is(err, os.ErrNotExist) { |
| 79 | + return nil, nil |
| 80 | + } |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + out, err := r.Runner.RunCmd( |
| 85 | + "/usr/bin/powermetrics", |
| 86 | + "-f", "plist", |
| 87 | + "-n", "1", |
| 88 | + "-i", strconv.Itoa(interval), |
| 89 | + "--samplers", "cpu_power,gpu_power,ane_power", |
| 90 | + ) |
| 91 | + if err != nil { |
| 92 | + return nil, errors.Wrap(err, "running powermetrics") |
| 93 | + } |
| 94 | + |
| 95 | + var output powermetricsOutput |
| 96 | + if err := plist.Unmarshal(out, &output); err != nil { |
| 97 | + return nil, errors.Wrap(err, "unmarshalling powermetrics output") |
| 98 | + } |
| 99 | + |
| 100 | + return &output, nil |
| 101 | +} |
0 commit comments