Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1259 from dqminh/epoll-io
Use Epoll to perform I/O in linux
- Loading branch information
Showing
10 changed files
with
210 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package shim | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/containerd/console" | ||
"github.com/containerd/fifo" | ||
"github.com/pkg/errors" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type linuxPlatform struct { | ||
epoller *console.Epoller | ||
} | ||
|
||
func (p *linuxPlatform) copyConsole(ctx context.Context, console console.Console, stdin, stdout, stderr string, wg, cwg *sync.WaitGroup) (console.Console, error) { | ||
if p.epoller == nil { | ||
return nil, errors.New("uninitialized epoller") | ||
} | ||
|
||
epollConsole, err := p.epoller.Add(console) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if stdin != "" { | ||
in, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cwg.Add(1) | ||
go func() { | ||
cwg.Done() | ||
io.Copy(epollConsole, in) | ||
}() | ||
} | ||
|
||
outw, err := fifo.OpenFifo(ctx, stdout, syscall.O_WRONLY, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
outr, err := fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
wg.Add(1) | ||
cwg.Add(1) | ||
go func() { | ||
cwg.Done() | ||
io.Copy(outw, epollConsole) | ||
epollConsole.Close() | ||
outr.Close() | ||
outw.Close() | ||
wg.Done() | ||
}() | ||
return epollConsole, nil | ||
} | ||
|
||
func (p *linuxPlatform) shutdownConsole(ctx context.Context, cons console.Console) error { | ||
if p.epoller == nil { | ||
return errors.New("uninitialized epoller") | ||
} | ||
epollConsole, ok := cons.(*console.EpollConsole) | ||
if !ok { | ||
return errors.Errorf("expected EpollConsole, got %#v", cons) | ||
} | ||
return epollConsole.Shutdown(p.epoller.CloseConsole) | ||
} | ||
|
||
// initialize a single epoll fd to manage our consoles. `initPlatform` should | ||
// only be called once. | ||
func (s *Service) initPlatform() error { | ||
if s.platform != nil { | ||
return nil | ||
} | ||
epoller, err := console.NewEpoller() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to initialize epoller") | ||
} | ||
s.platform = &linuxPlatform{ | ||
epoller: epoller, | ||
} | ||
go epoller.Wait() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// +build !windows,!linux | ||
|
||
package shim | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/containerd/console" | ||
"github.com/containerd/fifo" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type unixPlatform struct { | ||
} | ||
|
||
func (p *unixPlatform) copyConsole(ctx context.Context, console console.Console, stdin, stdout, stderr string, wg, cwg *sync.WaitGroup) (console.Console, error) { | ||
if stdin != "" { | ||
in, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cwg.Add(1) | ||
go func() { | ||
cwg.Done() | ||
io.Copy(console, in) | ||
}() | ||
} | ||
outw, err := fifo.OpenFifo(ctx, stdout, syscall.O_WRONLY, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
outr, err := fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
wg.Add(1) | ||
cwg.Add(1) | ||
go func() { | ||
cwg.Done() | ||
io.Copy(outw, console) | ||
console.Close() | ||
outr.Close() | ||
outw.Close() | ||
wg.Done() | ||
}() | ||
return console, nil | ||
} | ||
|
||
func (p *unixPlatform) shutdownConsole(ctx context.Context, cons console.Console) error { | ||
return nil | ||
} | ||
|
||
func (s *Service) initPlatform() error { | ||
s.platform = &unixPlatform{} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.