|
| 1 | +// |
| 2 | +// Really raw access to KStat data |
| 3 | + |
| 4 | +package kstat |
| 5 | + |
| 6 | +// #cgo LDFLAGS: -lkstat |
| 7 | +// |
| 8 | +// #include <sys/types.h> |
| 9 | +// #include <stdlib.h> |
| 10 | +// #include <strings.h> |
| 11 | +// #include <kstat.h> |
| 12 | +// #include <nfs/nfs_clnt.h> |
| 13 | +// |
| 14 | +import "C" |
| 15 | +import ( |
| 16 | + "errors" |
| 17 | + "fmt" |
| 18 | + "reflect" |
| 19 | + "unsafe" |
| 20 | +) |
| 21 | + |
| 22 | +// Raw is the raw data of a KStat. The actual bytes are in Data; |
| 23 | +// Ndata is kstat_t.ks_ndata, and is not normally useful. |
| 24 | +// |
| 25 | +// Note that with RawStat KStats, it turns out that Ndata == len(Data). |
| 26 | +// This is contrary to its meaning for other types of kstats. |
| 27 | +type Raw struct { |
| 28 | + Data []byte |
| 29 | + Ndata uint64 |
| 30 | + Snaptime int64 |
| 31 | + KStat *KStat |
| 32 | +} |
| 33 | + |
| 34 | +// TODO: better functionality split here |
| 35 | +func (k *KStat) prep() error { |
| 36 | + if k.invalid() { |
| 37 | + return errors.New("invalid KStat or closed token") |
| 38 | + } |
| 39 | + |
| 40 | + // Do the initial load of the data if necessary. |
| 41 | + if k.ksp.ks_data == nil { |
| 42 | + if err := k.Refresh(); err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + } |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// Raw returns the raw byte data of a KStat. It may be called on any |
| 50 | +// KStat. It does not refresh the KStat's data. |
| 51 | +func (k *KStat) Raw() (*Raw, error) { |
| 52 | + if err := k.prep(); err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + r := Raw{} |
| 56 | + r.KStat = k |
| 57 | + r.Snaptime = k.Snaptime |
| 58 | + r.Ndata = uint64(k.ksp.ks_ndata) |
| 59 | + // The forced C.int() conversion is dangerous, because C.int |
| 60 | + // is not necessarily large enough to contain a |
| 61 | + // size_t. However this is the interface that Go gives us, so |
| 62 | + // we live with it. |
| 63 | + r.Data = C.GoBytes(unsafe.Pointer(k.ksp.ks_data), C.int(k.ksp.ks_data_size)) |
| 64 | + return &r, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (tok *Token) prepunix(name string, size uintptr) (*KStat, error) { |
| 68 | + k, err := tok.Lookup("unix", 0, name) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + // TODO: handle better? |
| 73 | + if k.ksp.ks_type != C.KSTAT_TYPE_RAW { |
| 74 | + return nil, fmt.Errorf("%s is wrong type %s", k, k.Type) |
| 75 | + } |
| 76 | + if uintptr(k.ksp.ks_data_size) != size { |
| 77 | + return nil, fmt.Errorf("%s is wrong size %d (should be %d)", k, k.ksp.ks_data_size, size) |
| 78 | + } |
| 79 | + return k, nil |
| 80 | +} |
| 81 | + |
| 82 | +// Sysinfo returns the KStat and the statistics from unix:0:sysinfo. |
| 83 | +// It always returns a current, refreshed copy. |
| 84 | +func (tok *Token) Sysinfo() (*KStat, *Sysinfo, error) { |
| 85 | + var si Sysinfo |
| 86 | + k, err := tok.prepunix("sysinfo", unsafe.Sizeof(si)) |
| 87 | + if err != nil { |
| 88 | + return nil, nil, err |
| 89 | + } |
| 90 | + si = *((*Sysinfo)(k.ksp.ks_data)) |
| 91 | + return k, &si, nil |
| 92 | +} |
| 93 | + |
| 94 | +// Vminfo returns the KStat and the statistics from unix:0:vminfo. |
| 95 | +// It always returns a current, refreshed copy. |
| 96 | +func (tok *Token) Vminfo() (*KStat, *Vminfo, error) { |
| 97 | + var vi Vminfo |
| 98 | + k, err := tok.prepunix("vminfo", unsafe.Sizeof(vi)) |
| 99 | + if err != nil { |
| 100 | + return nil, nil, err |
| 101 | + } |
| 102 | + vi = *((*Vminfo)(k.ksp.ks_data)) |
| 103 | + return k, &vi, nil |
| 104 | +} |
| 105 | + |
| 106 | +// Var returns the KStat and the statistics from unix:0:var. |
| 107 | +// It always returns a current, refreshed copy. |
| 108 | +func (tok *Token) Var() (*KStat, *Var, error) { |
| 109 | + var vi Var |
| 110 | + k, err := tok.prepunix("var", unsafe.Sizeof(vi)) |
| 111 | + if err != nil { |
| 112 | + return nil, nil, err |
| 113 | + } |
| 114 | + vi = *((*Var)(k.ksp.ks_data)) |
| 115 | + return k, &vi, nil |
| 116 | +} |
| 117 | + |
| 118 | +// GetMntinfo retrieves a Mntinfo struct from a nfs:*:mntinfo KStat. |
| 119 | +// It does not force a refresh of the KStat. |
| 120 | +func (k *KStat) GetMntinfo() (*Mntinfo, error) { |
| 121 | + var mi Mntinfo |
| 122 | + if err := k.prep(); err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + if k.Type != RawStat || k.Module != "nfs" || k.Name != "mntinfo" { |
| 126 | + return nil, errors.New("KStat is not a Mntinfo kstat") |
| 127 | + } |
| 128 | + if uintptr(k.ksp.ks_data_size) != unsafe.Sizeof(mi) { |
| 129 | + return nil, fmt.Errorf("KStat is wrong size %d (should be %d)", k.ksp.ks_data_size, unsafe.Sizeof(mi)) |
| 130 | + } |
| 131 | + mi = *((*Mntinfo)(k.ksp.ks_data)) |
| 132 | + return &mi, nil |
| 133 | +} |
| 134 | + |
| 135 | +// |
| 136 | +// Support for copying semi-arbitrary structures out of raw |
| 137 | +// KStats. |
| 138 | +// |
| 139 | + |
| 140 | +// safeThing returns true if a given type is either a simple defined |
| 141 | +// size primitive integer type or an array and/or struct composed |
| 142 | +// entirely of safe things. A safe thing is entirely self contained |
| 143 | +// and may be initialized from random memory without breaking Go's |
| 144 | +// memory safety (although the values it contains may be garbage). |
| 145 | +// |
| 146 | +func safeThing(t reflect.Type) bool { |
| 147 | + switch t.Kind() { |
| 148 | + case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 149 | + return true |
| 150 | + case reflect.Array: |
| 151 | + // an array is safe if it's an array of something safe |
| 152 | + return safeThing(t.Elem()) |
| 153 | + case reflect.Struct: |
| 154 | + // a struct is safe if all its components are safe |
| 155 | + for i := 0; i < t.NumField(); i++ { |
| 156 | + if !safeThing(t.Field(i).Type) { |
| 157 | + return false |
| 158 | + } |
| 159 | + } |
| 160 | + return true |
| 161 | + default: |
| 162 | + // other things are not safe. |
| 163 | + return false |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// TODO: add floats to the supported list? It's unlikely to be needed |
| 168 | +// but it should just work. |
| 169 | + |
| 170 | +// CopyTo copies a RawStat KStat into a struct that you supply a |
| 171 | +// pointer to. The size of the struct must exactly match the size of |
| 172 | +// the RawStat's data. |
| 173 | +// |
| 174 | +// CopyStat imposes conditions on the struct that you are copying to: |
| 175 | +// it must be composed entirely of primitive integer types with defined |
| 176 | +// sizes (intN and uintN), or arrays and structs that ultimately only |
| 177 | +// contain them. All fields should be exported. |
| 178 | +// |
| 179 | +// If you give CopyStat a bad argument, it generally panics. |
| 180 | +// |
| 181 | +// This API is provisional and may be changed or deleted. |
| 182 | +func (k *KStat) CopyTo(ptr interface{}) error { |
| 183 | + if err := k.prep(); err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + |
| 187 | + if k.Type != RawStat { |
| 188 | + return errors.New("KStat is not a RawStat") |
| 189 | + } |
| 190 | + |
| 191 | + // Validity checks: not nil value, not nil pointer value, |
| 192 | + // is a pointer to struct. |
| 193 | + if ptr == nil { |
| 194 | + panic("CopyTo given nil pointer") |
| 195 | + } |
| 196 | + vp := reflect.ValueOf(ptr) |
| 197 | + if vp.Kind() != reflect.Ptr { |
| 198 | + panic("CopyTo not given a pointer") |
| 199 | + } |
| 200 | + if vp.IsNil() { |
| 201 | + panic("CopyTo given nil pointer") |
| 202 | + } |
| 203 | + dst := vp.Elem() |
| 204 | + if dst.Kind() != reflect.Struct { |
| 205 | + panic("CopyTo: not pointer to struct") |
| 206 | + } |
| 207 | + // Is the struct safe to copy into, which means primitive types |
| 208 | + // and structs/arrays of primitive types? |
| 209 | + if !safeThing(dst.Type()) { |
| 210 | + panic("CopyTo: not a safe structure, contains unsupported fields") |
| 211 | + } |
| 212 | + if !dst.CanSet() { |
| 213 | + panic("CopyTo: struct cannot be set for some reason") |
| 214 | + } |
| 215 | + |
| 216 | + // Verify that the size of the target struct matches the size |
| 217 | + // of the raw KStat. |
| 218 | + if uintptr(k.ksp.ks_data_size) != dst.Type().Size() { |
| 219 | + return errors.New("struct size does not match KStat size") |
| 220 | + } |
| 221 | + |
| 222 | + // The following is exactly the magic that we performed for |
| 223 | + // specific types earlier. We take k.ksp.ks_data and turn |
| 224 | + // it into a typed pointer to the target object's type: |
| 225 | + // |
| 226 | + // src := ((*<type>)(k.kps.ks_data)) |
| 227 | + src := reflect.NewAt(dst.Type(), unsafe.Pointer(k.ksp.ks_data)) |
| 228 | + |
| 229 | + // We now dereference that into the destination to copy the |
| 230 | + // data: |
| 231 | + // |
| 232 | + // dst = *src |
| 233 | + dst.Set(reflect.Indirect(src)) |
| 234 | + |
| 235 | + return nil |
| 236 | +} |
0 commit comments