Merge pull request #2243 from fcrisciani/syscalls

Syscalls reduction
This commit is contained in:
elangovan sivanandam 2019-07-31 17:57:15 -04:00 committed by GitHub
commit 5c9a062864
6 changed files with 12 additions and 29 deletions

View file

@ -850,6 +850,7 @@ func parallelJoin(t *testing.T, rc libnetwork.Sandbox, ep libnetwork.Endpoint, t
err = ep.Join(sb) err = ep.Join(sb)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err != nil { if err != nil {
if _, ok := err.(types.ForbiddenError); !ok { if _, ok := err.(types.ForbiddenError); !ok {
t.Fatalf("thread %d: %v", thrNumber, err) t.Fatalf("thread %d: %v", thrNumber, err)
@ -867,6 +868,7 @@ func parallelLeave(t *testing.T, rc libnetwork.Sandbox, ep libnetwork.Endpoint,
err = ep.Leave(sb) err = ep.Leave(sb)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err != nil { if err != nil {
if _, ok := err.(types.ForbiddenError); !ok { if _, ok := err.(types.ForbiddenError); !ok {
t.Fatalf("thread %d: %v", thrNumber, err) t.Fatalf("thread %d: %v", thrNumber, err)

View file

@ -39,19 +39,6 @@ func Init() {
} }
} }
// SetNamespace sets the initial namespace handler
func SetNamespace() error {
initOnce.Do(Init)
if err := netns.Set(initNs); err != nil {
linkInfo, linkErr := getLink()
if linkErr != nil {
linkInfo = linkErr.Error()
}
return fmt.Errorf("failed to set to initial namespace, %v, initns fd %d: %v", linkInfo, initNs, err)
}
return nil
}
// ParseHandlerInt transforms the namespace handler into an integer // ParseHandlerInt transforms the namespace handler into an integer
func ParseHandlerInt() int { func ParseHandlerInt() int {
return int(getHandler()) return int(getHandler())

View file

@ -424,13 +424,10 @@ func (n *networkNamespace) InvokeFunc(f func()) error {
// InitOSContext initializes OS context while configuring network resources // InitOSContext initializes OS context while configuring network resources
func InitOSContext() func() { func InitOSContext() func() {
runtime.LockOSThread() runtime.LockOSThread()
if err := ns.SetNamespace(); err != nil {
logrus.Error(err)
}
return runtime.UnlockOSThread return runtime.UnlockOSThread
} }
func nsInvoke(path string, prefunc func(nsFD int) error, postfunc func(callerFD int) error) error { func nsInvoke(path string, prefunc, postfunc func(int) error) error {
defer InitOSContext()() defer InitOSContext()()
newNs, err := netns.GetFromPath(path) newNs, err := netns.GetFromPath(path)
@ -445,10 +442,17 @@ func nsInvoke(path string, prefunc func(nsFD int) error, postfunc func(callerFD
return fmt.Errorf("failed in prefunc: %v", err) return fmt.Errorf("failed in prefunc: %v", err)
} }
// save the current namespace (host namespace)
curNs, err := netns.Get()
if err != nil {
return err
}
defer curNs.Close()
if err = netns.Set(newNs); err != nil { if err = netns.Set(newNs); err != nil {
return err return err
} }
defer ns.SetNamespace() // will restore the previous namespace before unlocking the thread
defer netns.Set(curNs)
// Invoked after the namespace switch. // Invoked after the namespace switch.
return postfunc(ns.ParseHandlerInt()) return postfunc(ns.ParseHandlerInt())
@ -681,7 +685,7 @@ func (n *networkNamespace) ApplyOSTweaks(types []SandboxType) {
for _, t := range types { for _, t := range types {
switch t { switch t {
case SandboxTypeLoadBalancer: case SandboxTypeLoadBalancer:
kernel.ApplyOSTweaks(loadBalancerConfig) n.InvokeFunc(func() { kernel.ApplyOSTweaks(loadBalancerConfig) })
} }
} }
} }

View file

@ -7,7 +7,6 @@ import (
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"syscall" "syscall"
"testing" "testing"
@ -199,7 +198,6 @@ func TestDisableIPv6DAD(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create a new sandbox: %v", err) t.Fatalf("Failed to create a new sandbox: %v", err)
} }
runtime.LockOSThread()
defer s.Destroy() defer s.Destroy()
n, ok := s.(*networkNamespace) n, ok := s.(*networkNamespace)
@ -253,7 +251,6 @@ func TestSetInterfaceIP(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create a new sandbox: %v", err) t.Fatalf("Failed to create a new sandbox: %v", err)
} }
runtime.LockOSThread()
defer s.Destroy() defer s.Destroy()
n, ok := s.(*networkNamespace) n, ok := s.(*networkNamespace)
@ -329,7 +326,6 @@ func TestLiveRestore(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create a new sandbox: %v", err) t.Fatalf("Failed to create a new sandbox: %v", err)
} }
runtime.LockOSThread()
defer s.Destroy() defer s.Destroy()
n, ok := s.(*networkNamespace) n, ok := s.(*networkNamespace)

View file

@ -2,7 +2,6 @@ package osl
import ( import (
"os" "os"
"runtime"
"testing" "testing"
"github.com/docker/docker/pkg/reexec" "github.com/docker/docker/pkg/reexec"
@ -80,7 +79,6 @@ func TestSandboxCreateTwice(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create a new sandbox: %v", err) t.Fatalf("Failed to create a new sandbox: %v", err)
} }
runtime.LockOSThread()
// Create another sandbox with the same key to see if we handle it // Create another sandbox with the same key to see if we handle it
// gracefully. // gracefully.
@ -88,7 +86,6 @@ func TestSandboxCreateTwice(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create a new sandbox: %v", err) t.Fatalf("Failed to create a new sandbox: %v", err)
} }
runtime.LockOSThread()
err = s.Destroy() err = s.Destroy()
if err != nil { if err != nil {
@ -130,7 +127,6 @@ func TestAddRemoveInterface(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create a new sandbox: %v", err) t.Fatalf("Failed to create a new sandbox: %v", err)
} }
runtime.LockOSThread()
if s.Key() != key { if s.Key() != key {
t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key) t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)

View file

@ -33,8 +33,6 @@ func SetupTestOSContext(t *testing.T) func() {
// sure to re-initialize initNs context // sure to re-initialize initNs context
ns.Init() ns.Init()
runtime.LockOSThread()
return func() { return func() {
if err := syscall.Close(fd); err != nil { if err := syscall.Close(fd); err != nil {
t.Logf("Warning: netns closing failed (%v)", err) t.Logf("Warning: netns closing failed (%v)", err)