Example #1
0
func (bp *Backplane) handleStats(w http.ResponseWriter, req *http.Request) {
	tr := trace.New("backend.internalstats", req.RequestURI)
	defer tr.Finish()
	hostname, err := os.Hostname()
	if err != nil {
		glog.Error("Unable to obtain hostname: ", err)
		tr.LazyPrintf("Unable to obtain hostname: ", err)
	}
	var data = struct {
		Backends                         []*Backend
		Frontends                        []*Frontend
		Pid                              int
		Hostname                         string
		Uptime                           time.Duration
		LimitAs, LimitFsize, LimitNofile syscall.Rlimit
	}{
		Backends:  bp.Backends,
		Frontends: bp.Frontends,
		Pid:       os.Getpid(),
		Hostname:  hostname,
		Uptime:    time.Since(starttime),
	}
	syscall.Getrlimit(syscall.RLIMIT_AS, &data.LimitAs)
	syscall.Getrlimit(syscall.RLIMIT_FSIZE, &data.LimitFsize)
	syscall.Getrlimit(syscall.RLIMIT_NOFILE, &data.LimitNofile)

	err = StatsTemplate().Execute(w, &data)
	if err != nil {
		glog.Errorf("unable to execute template: %s", err)
		tr.LazyPrintf("unable to execute template: %s", err)
	}
}
Example #2
0
// MaximizeOpenFileLimit tries to set the resource limit RLIMIT_NOFILE (number
// of open file descriptors) to the max (hard limit), if the current (soft
// limit) is below the max. Returns the new (though possibly unchanged) limit,
// or an error if it could not be changed.
func MaximizeOpenFileLimit() (int, error) {
	// Get the current limit on number of open files.
	var lim syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil {
		return 0, err
	}

	// If we're already at max, there's no need to try to raise the limit.
	if lim.Cur >= lim.Max {
		return int(lim.Cur), nil
	}

	// Try to increase the limit to the max.
	oldLimit := lim.Cur
	lim.Cur = lim.Max
	if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil {
		return int(oldLimit), err
	}

	// If the set succeeded, perform a new get to see what happened. We might
	// have gotten a value lower than the one in lim.Max, if lim.Max was
	// something that indicated "unlimited" (i.e. intmax).
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil {
		// We don't really know the correct value here since Getrlimit
		// mysteriously failed after working once... Shouldn't ever happen, I
		// think.
		return 0, err
	}

	return int(lim.Cur), nil
}
Example #3
0
func main() {
	var rlim syscall.Rlimit
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim)
	if err != nil {
		fmt.Errorf("获取Rlimit报错 %s", err)
		os.Exit(1)
	}
	fmt.Printf("ENV RLIMIT_NOFILE : %+v\n", rlim)

	rlim.Max = 65535
	rlim.Cur = 65535
	//err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
	err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
	if err != nil {
		fmt.Errorf("设置Rlimit报错 %s", err)
		os.Exit(1)
	}
	//fmt.Printf("ENV RLIMIT_NOFILE : %+v\n", rlim)
	err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim)
	if err != nil {
		fmt.Errorf("获取Rlimit报错 %s", err)
		os.Exit(1)
	}
	fmt.Printf("ENV RLIMIT_NOFILE : %+v\n", rlim)
}
Example #4
0
func TestRlimit(t *testing.T) {
	var rlimit, zero syscall.Rlimit
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit)
	if err != nil {
		t.Fatalf("Getrlimit: save failed: %v", err)
	}
	if zero == rlimit {
		t.Fatalf("Getrlimit: save failed: got zero value %#v", rlimit)
	}
	set := rlimit
	set.Cur = set.Max - 1
	err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &set)
	if err != nil {
		t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
	}
	var get syscall.Rlimit
	err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &get)
	if err != nil {
		t.Fatalf("Getrlimit: get failed: %v", err)
	}
	set = rlimit
	set.Cur = set.Max - 1
	if set != get {
		t.Fatalf("Rlimit: change failed: wanted %#v got %#v", set, get)
	}
	err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit)
	if err != nil {
		t.Fatalf("Setrlimit: restore failed: %#v %v", rlimit, err)
	}
}
Example #5
0
func main() {
	lim := &syscall.Rlimit{}

	syscall.Getrlimit(syscall.RLIMIT_NOFILE, lim)
	lim.Cur = 15
	syscall.Setrlimit(syscall.RLIMIT_NOFILE, lim)
	fmt.Println(lim)
	var s []*os.File
	for i := 0; i < int(lim.Max+10); i++ {
		f, err := os.Open("/tmp/whj.txt")
		fmt.Println(f, err)
		s = append(s, f)
		err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, lim)
		fmt.Println(lim, err)
	}
	err := syscall.Getrlimit(syscall.RLIMIT_CORE, lim)
	fmt.Println(lim, err)
	rusage := &syscall.Rusage{}
	err = syscall.Getrusage(0, rusage)
	fmt.Println(rusage, err)
	err = syscall.Getrusage(1, rusage)
	fmt.Println(rusage, err)
	err = syscall.Getrusage(2, rusage)
	fmt.Println(rusage, err)
	err = syscall.Getrusage(3, rusage)
	fmt.Println(rusage, err)
	err = syscall.Getrusage(4, rusage)
	fmt.Println(rusage, err)
}
func fdlimit() int {
	var nofile syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &nofile); err != nil {
		return 0
	}
	return int(nofile.Cur)
}
Example #7
0
func getFdLimit() uint64 {
	var rlim syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
		panic(err)
	}
	return rlim.Max
}
func server_func(port *string) {

	// check for max_allowed ?
	var rlimit syscall.Rlimit
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit)
	if err != nil {
		log.Fatal("syscall.Getrlimit | ", err)
	}

	// if server run the server
	l := start_listener(*port)
	defer l.Close()
	log.Println("the listener started on port " + *port)

	// now we need to accept the comming req
	// but B4 that we need to check for openfd's
	for {
		// refresh the number of openfd's
		nofile := check_open_fd()
		// log.Printf("the new number of openfd's %d\n", nofile)
		// check them ?
		if rlimit.Cur > nofile {
			// if no problem accept the new connection
			c, err := l.Accept()
			if err != nil {
				log.Fatal("l.Accept ", err)
			}
			// defer c.Close()

			go handleConn(c)
		}
	}
}
Example #9
0
func maxOpenFiles() int {
	var rlim syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
		return defaultMaxOpenFiles
	}
	return int(rlim.Cur)
}
Example #10
0
func (fs *Fs) Attach() error {

	fs.fidLock.Lock()
	defer fs.fidLock.Unlock()

	// Load the root.
	var err error
	fs.root, err = fs.lookup("/")
	if err != nil {
		return err
	}

	if fs.Fdlimit == 0 {
		// Figure out our active limit (1/2 open limit).
		// We use 1/2 because control connections, tap devices,
		// disks, etc. all need file descriptors. Note that we
		// also explicitly handle running out of file descriptors,
		// but this gives us an open bound to leave room for the
		// rest of the system (because pieces don't always handle
		// an EMFILE or ENFILE appropriately).
		var rlim syscall.Rlimit
		err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim)
		if err != nil {
			return err
		}
		fs.Fdlimit = uint(rlim.Cur) / 2
	}

	return nil
}
Example #11
0
func maxFD() (uint64, error) {
	var rlim syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
		return 0, fmt.Errorf("ulimit error: %v", err)
	}
	return rlim.Cur, nil
}
Example #12
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	os.Setenv("GOTRACEBACK", "crash")

	lim := syscall.Rlimit{}
	syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim)
	if lim.Cur < _MaxOpenfile || lim.Max < _MaxOpenfile {
		lim.Cur = _MaxOpenfile
		lim.Max = _MaxOpenfile
		syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim)
	}

	ln, err := net.Listen("tcp", ":1153")
	if err != nil {
		log.Fatal(err)
	}
	go TCPServer(ln, queryTypeDNS)

	ln, err = net.Listen("tcp", ":1154")
	if err != nil {
		log.Fatal(err)
	}
	go TCPServer(ln, queryTypeMYIP)

	http.HandleFunc("/myip", HTTPServerMYIP)
	http.HandleFunc("/dns", HTTPServerDNS)
	http.HandleFunc("/health", HTTPServerHealth)
	log.Fatal(http.ListenAndServe(":1053", nil))
}
Example #13
0
func newHerd(imageServerAddress string, logger *log.Logger) *Herd {
	var herd Herd
	herd.imageServerAddress = imageServerAddress
	herd.logger = logger
	herd.subsByName = make(map[string]*Sub)
	herd.imagesByName = make(map[string]*image.Image)
	herd.missingImages = make(map[string]time.Time)
	// Limit concurrent connection attempts so that the file descriptor limit is
	// not exceeded.
	var rlim syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
		panic(err)
	}
	maxConnAttempts := rlim.Cur - 50
	maxConnAttempts = (maxConnAttempts / 100)
	if maxConnAttempts < 1 {
		maxConnAttempts = 1
	} else {
		maxConnAttempts *= 100
	}
	herd.connectionSemaphore = make(chan bool, maxConnAttempts)
	herd.pollSemaphore = make(chan bool, runtime.NumCPU()*2)
	herd.currentScanStartTime = time.Now()
	return &herd
}
Example #14
0
// Set Max File Descriptor limits
//
// Background information:
//
// - SG docs
//   http://developer.couchbase.com/documentation/mobile/1.1.0/develop/guides/sync-gateway/os-level-tuning/max-file-descriptors/index.html
// - Related SG issues
//   https://github.com/couchbase/sync_gateway/issues/1083
// - Hard limit vs Soft limit
//   http://unix.stackexchange.com/questions/29577/ulimit-difference-between-hard-and-soft-limits
func SetMaxFileDescriptors(requestedSoftFDLimit uint64) (uint64, error) {

	var limits syscall.Rlimit

	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits); err != nil {
		return 0, err
	}

	requiresUpdate, recommendedSoftFDLimit := getSoftFDLimit(
		requestedSoftFDLimit,
		limits,
	)

	// No call to Setrlimit required, because the requested soft limit is lower than current soft limit
	if !requiresUpdate {
		return 0, nil
	}

	// Update the soft limit (but don't bother updating the hard limit, since only root can do that,
	// and it's assumed that this process is not running as root)
	limits.Cur = recommendedSoftFDLimit
	err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limits)

	if err == nil {
		Logf("Configured process to allow %d open file descriptors", recommendedSoftFDLimit)
	}

	return recommendedSoftFDLimit, err

}
Example #15
0
func checkAndSetUlimit() error {
	var rLimit syscall.Rlimit
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		return fmt.Errorf("error getting rlimit: %s", err)
	}

	ipfsFileDescNum := int64(ipfsFileDescNum)

	var setting bool
	if rLimit.Cur < ipfsFileDescNum {
		if rLimit.Max < ipfsFileDescNum {
			log.Error("adjusting max")
			rLimit.Max = ipfsFileDescNum
		}
		fmt.Printf("Adjusting current ulimit to %d...\n", ipfsFileDescNum)
		rLimit.Cur = ipfsFileDescNum
		setting = true
	}

	err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		return fmt.Errorf("error setting ulimit: %s", err)
	}

	if setting {
		fmt.Printf("Successfully raised file descriptor limit to %d.\n", ipfsFileDescNum)
	}

	return nil
}
Example #16
0
func testSlowClient(t *testing.T, name string, numMessages, numSlowClients, numNormalClients, slowDelay, timeout int) {
	syscall.Getrlimit(syscall.RLIMIT_NOFILE, &syscall.Rlimit{Cur: 64000, Max: 64000})
	fmt.Printf("========== %s: %d total clients, %d slow client(s), %d messages each ==========\n",
		name, numSlowClients+numNormalClients, numSlowClients, numMessages)

	ts := newTestSystem(t)
	if err := ts.startServer(startServerTries); err != nil {
		t.Errorf("Failed to start server: %s\n", err)
		return
	}
	defer ts.server.Close()

	slowClients := newTestClients(numSlowClients, true)
	if err := ts.startClients(slowClients...); err != nil {
		t.Errorf("Test failed: %s\n", err)
		return
	}
	defer ts.killClients(slowClients...)
	normalClients := newTestClients(numNormalClients, false)
	if err := ts.startClients(normalClients...); err != nil {
		t.Errorf("Test failed: %s\n", err)
		return
	}
	defer ts.killClients(normalClients...)

	// Give the server some time to register the clients before running the test.
	time.Sleep(time.Duration(defaultStartDelay) * time.Millisecond)

	if err := ts.runTest(numMessages, timeout, normalClients, slowClients, slowDelay); err != nil {
		t.Error(err)
		return
	}
}
Example #17
0
func getMaxOpenFiles(t *testing.T) int {
	var limit syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
		t.Fatal(err)
	}
	return int(limit.Cur)
}
Example #18
0
func Init() {
	mu.Lock()
	defer mu.Unlock()
	if inited {
		log.Fatal("servenv.Init called second time")
	}
	inited = true

	// Once you run as root, you pretty much destroy the chances of a
	// non-privileged user starting the program correctly.
	if uid := os.Getuid(); uid == 0 {
		log.Fatalf("servenv.Init: running this as root makes no sense")
	}

	runtime.MemProfileRate = *memProfileRate
	gomaxprocs := os.Getenv("GOMAXPROCS")
	if gomaxprocs == "" {
		gomaxprocs = "1"
	}

	// We used to set this limit directly, but you pretty much have to
	// use a root account to allow increasing a limit reliably. Dropping
	// privileges is also tricky. The best strategy is to make a shell
	// script set up the limits as root and switch users before starting
	// the server.
	fdLimit := &syscall.Rlimit{}
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, fdLimit); err != nil {
		log.Errorf("max-open-fds failed: %v", err)
	}
	fdl := stats.NewInt("MaxFds")
	fdl.Set(int64(fdLimit.Cur))

	onInitHooks.Fire()
}
Example #19
0
// setOpenFilesLimit sets the open file limit in the kernel
// cur is the soft limit, max is the ceiling (or hard limit) for that limit
func (pm *ProcessManager) setOpenFilesLimit(cur, max uint64) error {
	var rLimit syscall.Rlimit
	// First check if the limits are already what we want
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		return err
	}

	// If the current values are less than we want, set them
	if rLimit.Cur < cur || rLimit.Max < max {
		if cur > rLimit.Cur {
			rLimit.Cur = cur
		}
		if max > rLimit.Max {
			rLimit.Max = max
		}

		log.Infof("Setting open files limit (soft, hard) to (%v, %v)", rLimit.Cur, rLimit.Max)
		err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
		if err != nil {
			return err
		}
	}

	return nil
}
Example #20
0
// SetLimits raises some process limits to values which allow dcrd and
// associated utilities to run.
func SetLimits() error {
	var rLimit syscall.Rlimit

	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		return err
	}
	if rLimit.Cur > fileLimitWant {
		return nil
	}
	if rLimit.Max < fileLimitMin {
		err = fmt.Errorf("need at least %v file descriptors",
			fileLimitMin)
		return err
	}
	if rLimit.Max < fileLimitWant {
		rLimit.Cur = rLimit.Max
	} else {
		rLimit.Cur = fileLimitWant
	}
	err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		// try min value
		rLimit.Cur = fileLimitMin
		err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
		if err != nil {
			return err
		}
	}

	return nil
}
Example #21
0
func (dld *DataStoreLoader) calculateMaxOpenFilesPerShard() int {
	var rlim syscall.Rlimit
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim)
	if err != nil {
		dld.lg.Warnf("Unable to calculate maximum open files per shard: "+
			"getrlimit failed: %s\n", err.Error())
		return 0
	}
	// I think RLIMIT_NOFILE fits in 32 bits on all known operating systems,
	// but there's no harm in being careful.  'int' in golang always holds at
	// least 32 bits.
	var maxFd int
	if rlim.Cur > uint64(math.MaxInt32) {
		maxFd = math.MaxInt32
	} else {
		maxFd = int(rlim.Cur)
	}
	if len(dld.shards) == 0 {
		dld.lg.Warnf("Unable to calculate maximum open files per shard, " +
			"since there are 0 shards configured.\n")
		return 0
	}
	fdsPerShard := (maxFd - NON_DATASTORE_FD_MAX) / len(dld.shards)
	if fdsPerShard < MIN_FDS_PER_SHARD {
		dld.lg.Warnf("Expected to be able to use at least %d "+
			"fds per shard, but we have %d shards and %d total fds to allocate, "+
			"giving us only %d FDs per shard.", MIN_FDS_PER_SHARD,
			len(dld.shards), maxFd-NON_DATASTORE_FD_MAX, fdsPerShard)
		return 0
	}
	dld.lg.Infof("maxFd = %d.  Setting maxFdPerShard = %d\n",
		maxFd, fdsPerShard)
	return fdsPerShard
}
Example #22
0
// getFdLimit retrieves the number of file descriptors allowed to be opened by this
// process.
func getFdLimit() (int, error) {
	var limit syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
		return 0, err
	}
	return int(limit.Cur), nil
}
Example #23
0
func main() {
	var rLimit syscall.Rlimit
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		fmt.Println("Error Getting Rlimit ", err)
	}
	fmt.Print("Maximum FD per process: ")
	fmt.Println(rLimit)

	debug = flag.Bool("d", false, "Debug mode can be \"1\" for yes or \"0 for no")
	tcp_port = flag.String("l", ":9090", "ip:port for listening or plain \":port\" for listening all IPs")

	flag.Parse()

	l, err := tproxy.TcpListen(*tcp_port)
	if err != nil && *debug {
		fmt.Fprintln(os.Stderr, err)
	}
	for {
		s, err := l.Accept()
		if err != nil {
			//panic(err)
			fmt.Println(err)
		}
		if *debug {
			fmt.Println("starting connection from: " + (s.RemoteAddr()).String() + ", to: " + (s.LocalAddr()).String())
		}
		go handleConn(s, (s.LocalAddr()).String())
	}
}
Example #24
0
func FDLimit() (uint64, error) {
	var rlimit syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err != nil {
		return 0, err
	}
	return rlimit.Cur, nil
}
Example #25
0
func TestMain(m *testing.M) {
	var lim syscall.Rlimit
	syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim)
	lim.Cur = 1024000
	lim.Max = 1024000
	syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim)
}
Example #26
0
File: main.go Project: na--/nedomi
// upLimitToHard ups a resource limit identified
// by the resource argument to it's hard limit
// the returned error is either from syscall.(Get|Set)rlimit
func upLimitToHard(resource int) error {
	var resLimit = new(syscall.Rlimit)
	if err := syscall.Getrlimit(resource, resLimit); err != nil {
		return err
	}
	resLimit.Cur = resLimit.Max
	return syscall.Setrlimit(resource, resLimit)
}
Example #27
0
// findUlimit uses a syscall to determine the value of `ulimit -v` on the command line.
// This is the amount of virtual memory that can be allocated to programs running in this shell.
// This will return 2^64 if `ulimit -v` is `unlimited`.
func (u linuxSystemFactFinder) findUlimit() (uint64, error) {
	var rLimit syscall.Rlimit
	err := syscall.Getrlimit(syscall.RLIMIT_AS, &rLimit)
	if err != nil {
		return 0, err
	}
	return rLimit.Max, err
}
Example #28
0
func SetUlimit(number uint64) {
	var rLimit syscall.Rlimit
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		fmt.Println("[Error]: Getting Rlimit ", err)
	}
	rLimit.Max = number
	rLimit.Cur = number
	err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		fmt.Println("[Error]: Setting Rlimit ", err)
	}
	err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		fmt.Println("[Error]: Getting Rlimit ", err)
	}
}
Example #29
0
func ShowRlimit() {
	var rLimit syscall.Rlimit
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
	if err != nil {
		panic(fmt.Sprintf("Error Getting Rlimit '%s'", err))
	}
	fmt.Printf("starting rlimit.Cur = %d, rlimit.Max = %d\n", rLimit.Cur, rLimit.Max)
}
Example #30
0
func getRlimitMax(resource int) int {
	var rlimit syscall.Rlimit

	if err := syscall.Getrlimit(resource, &rlimit); err == nil {
		return int(rlimit.Max)
	}

	return 0
}