Skip to content

Commit

Permalink
test run: generate runtime.pinner method for each struct
Browse files Browse the repository at this point in the history
Signed-off-by: kwakubiney <kebiney@hotmail.com>

fix: fix lint errors

Signed-off-by: kwakubiney <kebiney@hotmail.com>
  • Loading branch information
kwakubiney committed Nov 17, 2023
1 parent e1bf3cc commit 366d952
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 2 deletions.
21 changes: 19 additions & 2 deletions internal/cmd/gentypes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ package sys
import (
"unsafe"
"runtime"
)
`)
Expand Down Expand Up @@ -222,6 +223,8 @@ import (
if err := outputPatchedStruct(gf, w, s.goType, t, s.patches); err != nil {
return nil, fmt.Errorf("output %q: %w", s.goType, err)
}

fmt.Fprintf(w, "func (s *%s) Pin(p *runtime.Pinner){ p.Pin(s)}\n\n", s.goType)
}

// Attrs
Expand Down Expand Up @@ -495,11 +498,23 @@ import (
return nil, fmt.Errorf("output %q: %w", goAttrType, err)
}

fmt.Fprintf(w, "func (s *%s) Pin(p *runtime.Pinner){ p.Pin(s)}\n\n", goAttrType)

switch s.ret {
case retError:
fmt.Fprintf(w, "func %s(attr *%s) error { _, err := BPF(%s, unsafe.Pointer(attr), unsafe.Sizeof(*attr)); return err }\n\n", s.goType, goAttrType, s.cmd)
fmt.Fprintf(w, "func %s(attr *%s) error {\n"+
"var pinner runtime.Pinner\n"+
"attr.Pin(&pinner)\n"+
"defer pinner.Unpin()\n"+
"_, err := BPF(%s, unsafe.Pointer(attr), unsafe.Sizeof(*attr));\n"+
"return err }\n\n", s.goType, goAttrType, s.cmd)
case retFd:
fmt.Fprintf(w, "func %s(attr *%s) (*FD, error) { fd, err := BPF(%s, unsafe.Pointer(attr), unsafe.Sizeof(*attr)); if err != nil { return nil, err }; return NewFD(int(fd)) }\n\n", s.goType, goAttrType, s.cmd)
fmt.Fprintf(w, "func %s(attr *%s) (*FD, error) {\n"+
"var pinner runtime.Pinner\n"+
"attr.Pin(&pinner)\n"+
"defer pinner.Unpin()\n"+
"fd, err := BPF(%s, unsafe.Pointer(attr), unsafe.Sizeof(*attr)); if err != nil { return nil, err }; \n"+
"return NewFD(int(fd)) }\n\n", s.goType, goAttrType, s.cmd)
}
}

Expand Down Expand Up @@ -553,6 +568,8 @@ import (
if err := outputPatchedStruct(gf, w, s.goType, t, s.patches); err != nil {
return nil, fmt.Errorf("output %q: %w", s.goType, err)
}
//TODO: Find a way to determine pointer fields and pin them separately.
fmt.Fprintf(w, "func (s *%s) Pin(p *runtime.Pinner){ p.Pin(s)}\n\n", s.goType)
}

return w.Bytes(), nil
Expand Down
Loading

0 comments on commit 366d952

Please sign in to comment.