|
| 1 | +package instruments |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/danielpaulus/go-ios/ios" |
| 7 | + dtx "github.com/danielpaulus/go-ios/ios/dtx_codec" |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | +) |
| 10 | + |
| 11 | +type sysmontapMsgDispatcher struct { |
| 12 | + messages chan dtx.Message |
| 13 | +} |
| 14 | + |
| 15 | +func newSysmontapMsgDispatcher() *sysmontapMsgDispatcher { |
| 16 | + return &sysmontapMsgDispatcher{make(chan dtx.Message)} |
| 17 | +} |
| 18 | + |
| 19 | +func (p *sysmontapMsgDispatcher) Dispatch(m dtx.Message) { |
| 20 | + p.messages <- m |
| 21 | +} |
| 22 | + |
| 23 | +const sysmontapName = "com.apple.instruments.server.services.sysmontap" |
| 24 | + |
| 25 | +type sysmontapService struct { |
| 26 | + channel *dtx.Channel |
| 27 | + conn *dtx.Connection |
| 28 | + |
| 29 | + deviceInfoService *DeviceInfoService |
| 30 | + msgDispatcher *sysmontapMsgDispatcher |
| 31 | +} |
| 32 | + |
| 33 | +// NewSysmontapService creates a new sysmontapService |
| 34 | +// - samplingInterval is the rate how often to get samples, i.e Xcode's default is 10, which results in sampling output |
| 35 | +// each 1 second, with 500 the samples are retrieved every 15 seconds. It doesn't make any correlation between |
| 36 | +// the expected rate and the actual rate of samples delivery. We can only conclude, that the lower the rate in digits, |
| 37 | +// the faster the samples are delivered |
| 38 | +func NewSysmontapService(device ios.DeviceEntry, samplingInterval int) (*sysmontapService, error) { |
| 39 | + deviceInfoService, err := NewDeviceInfoService(device) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + msgDispatcher := newSysmontapMsgDispatcher() |
| 45 | + dtxConn, err := connectInstrumentsWithMsgDispatcher(device, msgDispatcher) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + processControlChannel := dtxConn.RequestChannelIdentifier(sysmontapName, loggingDispatcher{dtxConn}) |
| 51 | + |
| 52 | + sysAttrs, err := deviceInfoService.systemAttributes() |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + procAttrs, err := deviceInfoService.processAttributes() |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + config := map[string]interface{}{ |
| 63 | + "ur": samplingInterval, |
| 64 | + "bm": 0, |
| 65 | + "procAttrs": procAttrs, |
| 66 | + "sysAttrs": sysAttrs, |
| 67 | + "cpuUsage": true, |
| 68 | + "physFootprint": true, |
| 69 | + "sampleInterval": 500000000, |
| 70 | + } |
| 71 | + _, err = processControlChannel.MethodCall("setConfig:", config) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + err = processControlChannel.MethodCallAsync("start") |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + return &sysmontapService{processControlChannel, dtxConn, deviceInfoService, msgDispatcher}, nil |
| 82 | +} |
| 83 | + |
| 84 | +// Close closes up the DTX connection, message dispatcher and dtx.Message channel |
| 85 | +func (s *sysmontapService) Close() error { |
| 86 | + close(s.msgDispatcher.messages) |
| 87 | + |
| 88 | + s.deviceInfoService.Close() |
| 89 | + return s.conn.Close() |
| 90 | +} |
| 91 | + |
| 92 | +// ReceiveCPUUsage returns a chan of SysmontapMessage with CPU Usage info |
| 93 | +// The method will close the result channel automatically as soon as sysmontapMsgDispatcher's |
| 94 | +// dtx.Message channel is closed. |
| 95 | +func (s *sysmontapService) ReceiveCPUUsage() chan SysmontapMessage { |
| 96 | + messages := make(chan SysmontapMessage) |
| 97 | + go func() { |
| 98 | + defer close(messages) |
| 99 | + |
| 100 | + for msg := range s.msgDispatcher.messages { |
| 101 | + sysmontapMessage, err := mapToCPUUsage(msg) |
| 102 | + if err != nil { |
| 103 | + log.Debugf("expected `sysmontapMessage` from global channel, but received %v", msg) |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + messages <- sysmontapMessage |
| 108 | + } |
| 109 | + |
| 110 | + log.Infof("sysmontap message dispatcher channel closed") |
| 111 | + }() |
| 112 | + |
| 113 | + return messages |
| 114 | +} |
| 115 | + |
| 116 | +// SysmontapMessage is a wrapper struct for incoming CPU samples |
| 117 | +type SysmontapMessage struct { |
| 118 | + CPUCount uint64 |
| 119 | + EnabledCPUs uint64 |
| 120 | + EndMachAbsTime uint64 |
| 121 | + Type uint64 |
| 122 | + SystemCPUUsage CPUUsage |
| 123 | +} |
| 124 | + |
| 125 | +type CPUUsage struct { |
| 126 | + CPU_TotalLoad float64 |
| 127 | +} |
| 128 | + |
| 129 | +func mapToCPUUsage(msg dtx.Message) (SysmontapMessage, error) { |
| 130 | + payload := msg.Payload |
| 131 | + if len(payload) != 1 { |
| 132 | + return SysmontapMessage{}, fmt.Errorf("payload of message should have only one element: %+v", msg) |
| 133 | + } |
| 134 | + |
| 135 | + resultArray, ok := payload[0].([]interface{}) |
| 136 | + if !ok { |
| 137 | + return SysmontapMessage{}, fmt.Errorf("expected resultArray of type []interface{}: %+v", payload[0]) |
| 138 | + } |
| 139 | + resultMap, ok := resultArray[0].(map[string]interface{}) |
| 140 | + if !ok { |
| 141 | + return SysmontapMessage{}, fmt.Errorf("expected resultMap of type map[string]interface{} as a single element of resultArray: %+v", resultArray[0]) |
| 142 | + } |
| 143 | + cpuCount, ok := resultMap["CPUCount"].(uint64) |
| 144 | + if !ok { |
| 145 | + return SysmontapMessage{}, fmt.Errorf("expected CPUCount of type uint64 of resultMap: %+v", resultMap) |
| 146 | + } |
| 147 | + enabledCPUs, ok := resultMap["EnabledCPUs"].(uint64) |
| 148 | + if !ok { |
| 149 | + return SysmontapMessage{}, fmt.Errorf("expected EnabledCPUs of type uint64 of resultMap: %+v", resultMap) |
| 150 | + } |
| 151 | + endMachAbsTime, ok := resultMap["EndMachAbsTime"].(uint64) |
| 152 | + if !ok { |
| 153 | + return SysmontapMessage{}, fmt.Errorf("expected EndMachAbsTime of type uint64 of resultMap: %+v", resultMap) |
| 154 | + } |
| 155 | + typ, ok := resultMap["Type"].(uint64) |
| 156 | + if !ok { |
| 157 | + return SysmontapMessage{}, fmt.Errorf("expected Type of type uint64 of resultMap: %+v", resultMap) |
| 158 | + } |
| 159 | + sysmontapMessageMap, ok := resultMap["SystemCPUUsage"].(map[string]interface{}) |
| 160 | + if !ok { |
| 161 | + return SysmontapMessage{}, fmt.Errorf("expected SystemCPUUsage of type map[string]interface{} of resultMap: %+v", resultMap) |
| 162 | + } |
| 163 | + cpuTotalLoad, ok := sysmontapMessageMap["CPU_TotalLoad"].(float64) |
| 164 | + if !ok { |
| 165 | + return SysmontapMessage{}, fmt.Errorf("expected CPU_TotalLoad of type uint64 of sysmontapMessageMap: %+v", sysmontapMessageMap) |
| 166 | + } |
| 167 | + cpuUsage := CPUUsage{CPU_TotalLoad: cpuTotalLoad} |
| 168 | + |
| 169 | + sysmontapMessage := SysmontapMessage{ |
| 170 | + cpuCount, |
| 171 | + enabledCPUs, |
| 172 | + endMachAbsTime, |
| 173 | + typ, |
| 174 | + cpuUsage, |
| 175 | + } |
| 176 | + return sysmontapMessage, nil |
| 177 | +} |
0 commit comments