func main() { nlsk := nlgo.NlSocketAlloc() nlgo.NlConnect(nlsk, syscall.NETLINK_GENERIC) familyID := uint16(22) fmt.Println(familyID) files, _ := ioutil.ReadDir("/proc/") var pids []int for _, f := range files { if f.Name()[0] >= '0' && f.Name()[0] <= '9' { pid, _ := strconv.ParseInt(f.Name(), 10, 32) pids = append(pids, int(pid)) } } for i := 0; i < 50; i++ { time.Sleep(1000 * time.Millisecond) for _, p := range pids { t := netlink.GetTaskStats(nlsk, p) fmt.Printf("pid: %d ", p) if t != nil { if taskstatsMap[p] == nil { taskstatsMap[p] = t } else { fmt.Println(t.Read_bytes-taskstatsMap[p].Read_bytes, t.Write_bytes-taskstatsMap[p].Write_bytes) taskstatsMap[p] = t } } } } }
func BenchmarkNetlink(b *testing.B) { nlsk := nlgo.NlSocketAlloc() nlgo.NlConnect(nlsk, syscall.NETLINK_GENERIC) for i := 0; i < b.N; i++ { for _, p := range pids { t := netlink.GetTaskStats(nlsk, p) if t != nil { if taskstatsMap[p] == nil { taskstatsMap[p] = t } else { taskstatsMap[p] = t } } } } }
func main() { nlsk := nlgo.NlSocketAlloc() nlgo.NlConnect(nlsk, syscall.NETLINK_GENERIC) syscall.Bind(nlsk.Fd, &syscall.SockaddrNetlink{ Family: syscall.AF_NETLINK, Pid: uint32(os.Getpid()), Pad: CN_IDX_PROC, }) data := []byte{ 40, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 77, 26, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, } p := (*ppp)(unsafe.Pointer(&data[12])) p.pid = uint32(os.Getpid()) fmt.Println(data) err := syscall.Sendto(nlsk.Fd, data, 0, &nlsk.Peer) if err != nil { fmt.Println(err) } else { fmt.Println("send success") } func() error { for { buf := make([]byte, 1024) if _, _, err := syscall.Recvfrom(nlsk.Fd, buf, 0); err != nil { return err } fmt.Println(buf) } }() }