Exemple #1
0
func main() {
	log.SetFlags(0)
	log.SetPrefix("nice: ")

	flag.Usage = usage
	flag.Parse()

	xprio, err := syscall.Getpriority(syscall.PRIO_PROCESS, 0)
	ck(err)

	if flag.NArg() < 1 {
		fmt.Println(xprio)
		os.Exit(0)
	}

	err = syscall.Setpriority(syscall.PRIO_PROCESS, 0, *prio+xprio)
	ck(err)

	args := flag.Args()
	cmd := exec.Command(args[0], args[1:]...)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	ck(cmd.Run())
}
Exemple #2
0
func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, int32, error) {
	pid := p.Pid
	statPath := common.HostProc(strconv.Itoa(int(pid)), "stat")
	contents, err := ioutil.ReadFile(statPath)
	if err != nil {
		return "", 0, nil, 0, 0, err
	}
	fields := strings.Fields(string(contents))

	i := 1
	for !strings.HasSuffix(fields[i], ")") {
		i++
	}

	termmap, err := getTerminalMap()
	terminal := ""
	if err == nil {
		t, err := strconv.ParseUint(fields[i+5], 10, 64)
		if err != nil {
			return "", 0, nil, 0, 0, err
		}
		terminal = termmap[t]
	}

	ppid, err := strconv.ParseInt(fields[i+2], 10, 32)
	if err != nil {
		return "", 0, nil, 0, 0, err
	}
	utime, err := strconv.ParseFloat(fields[i+12], 64)
	if err != nil {
		return "", 0, nil, 0, 0, err
	}

	stime, err := strconv.ParseFloat(fields[i+13], 64)
	if err != nil {
		return "", 0, nil, 0, 0, err
	}

	cpuTimes := &cpu.TimesStat{
		CPU:    "cpu",
		User:   float64(utime / ClockTicks),
		System: float64(stime / ClockTicks),
	}

	bootTime, _ := host.BootTime()
	t, err := strconv.ParseUint(fields[i+20], 10, 64)
	if err != nil {
		return "", 0, nil, 0, 0, err
	}
	ctime := (t / uint64(ClockTicks)) + uint64(bootTime)
	createTime := int64(ctime * 1000)

	//	p.Nice = mustParseInt32(fields[18])
	// use syscall instead of parse Stat file
	snice, _ := syscall.Getpriority(PrioProcess, int(pid))
	nice := int32(snice) // FIXME: is this true?

	return terminal, int32(ppid), cpuTimes, createTime, nice, nil
}
Exemple #3
0
func main() {

	app := cli.NewApp()
	app.Name = Name
	app.Version = Version
	app.Author = "KUWASHIMA Yuichiro"
	app.Email = "*****@*****.**"
	app.Usage = "viagra [-d minutes] PID"

	app.Flags = GlobalFlags
	app.Commands = Commands
	app.CommandNotFound = CommandNotFound

	app.Action = func(c *cli.Context) {
		var err error
		var min int64
		min, err = strconv.ParseInt(c.String("duration"), 10, 64)
		if err != nil {
			println("Duration is not number")
		}

		if len(c.Args()) > 0 {
			var proc int
			var preNice int
			proc, err = strconv.Atoi(c.Args()[len(c.Args())-1])
			if err != nil {
				println("Proc no parse error")
				return
			}
			preNice, err = syscall.Getpriority(syscall.PRIO_PROCESS, proc)
			if err != nil {
				println("Unknown PID")
				return
			}
			err = syscall.Setpriority(syscall.PRIO_PROCESS, proc, 20)
			if err != nil {
				println("Setpriority failed")
				return
			}
			println("Power up!")
			timer := time.NewTimer(time.Duration(min) * time.Second)
			<-timer.C
			err = syscall.Setpriority(syscall.PRIO_PROCESS, proc, preNice)
			println("Time is up!")
		}
	}

	app.Run(os.Args)
}
Exemple #4
0
func main() {
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() < 1 {
		usage()
	}

	which := syscall.PRIO_PROCESS
	if *gflag {
		which = syscall.PRIO_PGRP
	} else if *uflag {
		which = syscall.PRIO_USER
	}

	for _, arg := range flag.Args() {
		var n int
		var err error

		id := -1
		if *uflag {
			p, err := user.Lookup(arg)
			if err == nil {
				n, err = strconv.Atoi(p.Uid)
			}
		} else {
			n, err = strconv.Atoi(arg)
		}
		if err == nil {
			id = n
		}

		if id < 0 {
			ek(fmt.Errorf("bad %q", arg))
			continue
		}

		prio, err := syscall.Getpriority(which, id)
		if ek(err) {
			continue
		}

		ek(syscall.Setpriority(which, id, prio+*inc))
	}

	os.Exit(status)
}
Exemple #5
0
func (p *Process) fillFromStat() (string, int32, *CPUTimesStat, int64, int32, error) {
	pid := p.Pid
	statPath := filepath.Join("/", "proc", strconv.Itoa(int(pid)), "stat")
	contents, err := ioutil.ReadFile(statPath)
	if err != nil {
		return "", 0, nil, 0, 0, err
	}
	fields := strings.Fields(string(contents))

	termmap, err := getTerminalMap()
	terminal := ""
	if err == nil {
		terminal = termmap[mustParseUint64(fields[6])]
	}

	ppid := mustParseInt32(fields[3])
	utime, _ := strconv.ParseFloat(fields[13], 64)
	stime, _ := strconv.ParseFloat(fields[14], 64)

	cpuTimes := &CPUTimesStat{
		CPU:    "cpu",
		User:   float32(utime * (1000 / CLOCK_TICKS)),
		System: float32(stime * (1000 / CLOCK_TICKS)),
	}

	bootTime, _ := BootTime()
	ctime := ((mustParseUint64(fields[21]) / uint64(CLOCK_TICKS)) + uint64(bootTime)) * 1000
	createTime := int64(ctime)

	//	p.Nice = mustParseInt32(fields[18])
	// use syscall instead of parse Stat file
	snice, _ := syscall.Getpriority(PRIO_PROCESS, int(pid))
	nice := int32(snice) // FIXME: is this true?

	return terminal, ppid, cpuTimes, createTime, nice, nil
}