// CPUsPerSocket returns the number of logical CPUs are associated with
// each socket.
510312
func (topo *CPUTopology) CPUsPerSocket() int {
	if topo.NumSockets == 0 {
		return 0
	}
	return topo.NumCPUs / topo.NumSockets
}
235555
func NewTempConsoleSocket() (*Socket, error) {
	runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
	dir, err := ioutil.TempDir(runtimeDir, "pty")
	if err != nil {
		return nil, err
	}
	abs, err := filepath.Abs(filepath.Join(dir, "pty.sock"))
	if err != nil {
		return nil, err
	}
	addr, err := net.ResolveUnixAddr("unix", abs)
	if err != nil {
		return nil, err
	}
	l, err := net.ListenUnix("unix", addr)
	if err != nil {
		return nil, err
	}
	if runtimeDir != "" {
		if err := os.Chmod(abs, 0755|os.ModeSticky); err != nil {
			return nil, err
		}
	}
	return &Socket{
		l:     l,
		rmdir: true,
	}, nil
}
372421
func sendSuccess(conn *websocket.Conn, msg string) error {
	return send(conn, map[string]string{"success": msg}, time.Second)
}
447189
func Websocket(route string, httpHandler websocket.Handler) {
	mainServer.Websocket(route, httpHandler)
}
1297505
func ThroughputServer(addr string, msgSize int, count int) {
	s, err := pair.NewSocket()
	if err != nil {
		log.Fatalf("Failed to make new pair socket: %v", err)
	}
	defer s.Close()

	all.AddTransports(s)
	l, err := s.NewListener(addr, nil)
	if err != nil {
		log.Fatalf("Failed to make new listener: %v", err)
	}

	// Disable TCP no delay, please! - only valid for TCP
	l.SetOption(mangos.OptionNoDelay, false)

	// Make sure we linger a bit on close...
	err = s.SetOption(mangos.OptionLinger, time.Second)
	if err != nil {
		log.Fatalf("Failed set Linger: %v", err)
	}

	err = l.Listen()
	if err != nil {
		log.Fatalf("Failed to listen: %v", err)
	}

	msg, err := s.RecvMsg()
	if err != nil {
		log.Fatalf("Failed to receive start message: %v", err)
	}
	msg.Free()

	start := time.Now()

	for i := 0; i != count; i++ {
		msg, err := s.RecvMsg()
		if err != nil {
			log.Fatalf("Failed to recv: %v", err)
		}
		if len(msg.Body) != msgSize {
			log.Fatalf("Received wrong message size: %d != %d", len(msg.Body), msgSize)
		}
		// return to cache to avoid GC
		msg.Free()
	}

	finish := time.Now()

	delta := finish.Sub(start)
	deltasec := float64(delta) / float64(time.Second)
	msgpersec := float64(count) / deltasec
	mbps := (float64((count)*8*msgSize) / deltasec) / 1000000.0
	fmt.Printf("message size: %d [B]\n", msgSize)
	fmt.Printf("message count: %d\n", count)
	fmt.Printf("throughput: %d [msg/s]\n", uint64(msgpersec))
	fmt.Printf("throughput: %.3f [Mb/s]\n", mbps)
}
264371
func ZmqReplySocket(ip string, reply_ports []int) *zmq.Socket {
	context, _ := zmq.NewContext()
	socket, _ := context.NewSocket(zmq.REP)
	for _, _port := range reply_ports {
		socket.Bind(fmt.Sprintf("tcp://%s:%d", ip, _port))
	}

	return socket
}
356087
func (a *cpuAccumulator) isSocketFree(socketID int) bool {
	return a.details.CPUsInSocket(socketID).Size() == a.topo.CPUsPerSocket()
}
34001
func (c *KBFSContext) GetKBFSSocket(clearError bool) (
	net.Conn, rpc.Transporter, bool, error) {
	var err error
	c.g.Trace("GetSocket", func() error { return err })()

	// Protect all global socket wrapper manipulation with a
	// lock to prevent race conditions.
	c.kbfsSocketMtx.Lock()
	defer c.kbfsSocketMtx.Unlock()

	needWrapper := false
	if c.kbfsSocketWrapper == nil {
		needWrapper = true
		c.g.Log.Debug("empty socket wrapper; need a new one")
	} else if c.kbfsSocketWrapper.Transporter != nil && !c.kbfsSocketWrapper.Transporter.IsConnected() {
		// need reconnect
		c.g.Log.Debug("rpc transport isn't connected, reconnecting...")
		needWrapper = true
	}

	isNew := false
	if needWrapper {
		sw := libkb.SocketWrapper{}
		if c.kbfsSocket == nil {
			sw.Err = fmt.Errorf("Cannot get socket")
		} else {
			sw.Conn, sw.Err = c.kbfsSocket.DialSocket()
			c.g.Log.Debug("DialSocket -> %s", libkb.ErrToOk(sw.Err))
			isNew = true
		}
		if sw.Err == nil {
			sw.Transporter = c.newTransportFromSocket(sw.Conn)
		}
		c.kbfsSocketWrapper = &sw
	}

	// Return the current error no matter what
	sw := c.kbfsSocketWrapper
	if sw.Err != nil && clearError {
		c.kbfsSocketWrapper = nil
	}

	return sw.Conn, sw.Transporter, isNew, sw.Err
}
894269
func (p *PodContainer) CPUs(cpu float64) *PodContainer {
	p.Resources.Cpus = cpu
	return p
}
241464
func NewBusSocket() (*BusSocket, error) {
	s, err := NewSocket(AF_SP, BUS)
	return &BusSocket{s}, err
}
264451
func NewSocket(addr *net.UDPAddr, recvBuf int, reuseport bool) (net.PacketConn, error) {
	// default to AF_INET6 to be equivalent to net.ListenUDP()
	domain := unix.AF_INET6
	if addr.IP.To4() != nil {
		domain = unix.AF_INET
	}
	sockFD, err := unix.Socket(domain, unix.SOCK_DGRAM|syscall.SOCK_CLOEXEC|syscall.SOCK_NONBLOCK, 0)
	if err != nil {
		return nil, err
	}

	// unix.SO_REUSEPORT is not defined on linux 386/amd64, see
	// https://github.com/golang/go/issues/16075
	if reuseport {
		if err := unix.SetsockoptInt(sockFD, unix.SOL_SOCKET, 0xf, 1); err != nil {
			unix.Close(sockFD)
			return nil, err
		}
	}
	if err = unix.SetsockoptInt(sockFD, unix.SOL_SOCKET, unix.SO_RCVBUF, recvBuf); err != nil {
		unix.Close(sockFD)
		return nil, err
	}

	var sa unix.Sockaddr
	if domain == unix.AF_INET {
		sockaddr := &unix.SockaddrInet4{
			Port: addr.Port,
		}
		if copied := copy(sockaddr.Addr[:], addr.IP.To4()); copied != net.IPv4len {
			panic("did not copy enough bytes of ip address")
		}
		sa = sockaddr
	} else {
		sockaddr := &unix.SockaddrInet6{
			Port: addr.Port,
		}
		// addr.IP will be length 0 for "bind all interfaces"
		if copied := copy(sockaddr.Addr[:], addr.IP.To16()); !(copied == net.IPv6len || copied == 0) {
			panic("did not copy enough bytes of ip address")
		}
		sa = sockaddr
	}
	if err = unix.Bind(sockFD, sa); err != nil {
		unix.Close(sockFD)
		return nil, err
	}

	osFD := os.NewFile(uintptr(sockFD), "veneursock")
	// this will close the FD we passed to NewFile
	defer osFD.Close()

	// however, FilePacketConn duplicates the FD, so closing the File's FD does
	// not affect this object's FD
	ret, err := net.FilePacketConn(osFD)
	if err != nil {
		return nil, err
	}
	return ret, nil
}
518157
func (e *Env) sandboxSocketFile() string {
	sandboxCacheDir := e.HomeFinder.SandboxCacheDir()
	if sandboxCacheDir == "" {
		return ""
	}
	return filepath.Join(sandboxCacheDir, SocketFile)
}
429402
func (d CPUDetails) Sockets() cpuset.CPUSet {
	b := cpuset.NewBuilder()
	for _, info := range d {
		b.Add(info.SocketID)
	}
	return b.Result()
}
1149568
func newSysSocket(config *Config) (*sysSocket, error) {
	g, err := newLockedNetNSGoroutine(config.NetNS)
	if err != nil {
		return nil, err
	}
	return &sysSocket{
		g: g,
	}, nil
}
824044
func NewSocket(addr *net.UDPAddr, recvBuf int, reuseport bool) (net.PacketConn, error) {
	if reuseport {
		panic("SO_REUSEPORT not supported on this platform")
	}
	serverConn, err := net.ListenUDP("udp", addr)
	if err != nil {
		return nil, err
	}
	if err := serverConn.SetReadBuffer(recvBuf); err != nil {
		return nil, err
	}
	return serverConn, nil
}
1301929
func parseLsofForSockets(output []byte) (sockets []string) {
	socketsMap := map[string]struct{}{}
	lines := bytes.Split(output, []byte("\n"))
	for _, line := range lines {
		// `lsof -F n`
		// When the -F option is specified, lsof produces output that is suitable for processing by another program
		// - e.g, an awk or Perl script, or a C program.
		// n    file name, comment, Internet address
		if !bytes.HasPrefix(line, []byte("n")) {
			continue
		}
		line = bytes.TrimPrefix(line, []byte("n"))

		// lsof on trusty:                 `/var/run/mysqld/mysqld.sock`
		// lsof on xenial, artful, bionic: `/var/run/mysqld/mysqld.sock type=STREAM`
		line = bytes.TrimSuffix(line, []byte("type=STREAM"))
		line = bytes.TrimSpace(line)

		// Skip empty lines.
		if len(line) == 0 {
			continue
		}
		socket := string(line)

		// @Nailya had a case on xenial where `lsof` returned `ntype=DGRAM` and `ntype=STREAM` without any path.
		// I'm not sure what are those but we can try to avoid this by checking for absolute path.
		// # lsof -a -n -P -U -F n -p $(pgrep -x mysqld | tr \\n ,)
		// p952
		// f3
		// ntype=DGRAM
		// f18
		// ntype=STREAM
		// f19
		// ntype=STREAM
		// f22
		// n/var/run/mysqld/mysqld.sock type=STREAM
		// f24
		// n/var/run/mysqld/mysqlx.sock type=STREAM
		if !path.IsAbs(socket) {
			continue
		}

		if _, exist := socketsMap[socket]; !exist {
			socketsMap[socket] = struct{}{}
			sockets = append(sockets, socket)
		}
	}

	return sockets
}
616921
func AddSocket(steamid string, so *wsevent.Client) {
	socketsMu.Lock()
	defer socketsMu.Unlock()

	steamIDSockets[steamid] = append(steamIDSockets[steamid], so)
	if len(steamIDSockets[steamid]) == 1 {
		connectedMu.Lock()
		timer, ok := connectedTimer[steamid]
		if ok {
			timer.Stop()
			delete(connectedTimer, steamid)
		}
		connectedMu.Unlock()
	}
}