service icon indicating copy to clipboard operation
service copied to clipboard

potential performance improvement on aix

Open setharnold opened this issue 4 years ago • 2 comments

Hello, in this section of code the output of ps -ef is scanned in its entirety to find which process is the srcmstr. However, the only use of this function that I saw is interested in only the parent process. This could be rewritten to look up the details for the one specific pid that we're interested in using ps -p, and avoid interrogating the kernel about potentially thousands of other processes, printing those values, sending them through a pipe, scanning all those values, etc.

Furthermore, the ps -o switch could be used to emit only the two properties that appear necessary: the process name (or process executable? I'm not familiar with aix) and the uid that owns the process.

https://www.ibm.com/support/knowledgecenter/ssw_aix_72/p_commands/ps.html

There may be a better solution available than calling ps, such as the /proc/<pid> filesystem available on Linux and Solaris, but I'm too unfamiliar with aix to suggest something concrete.

Thanks

https://github.com/kardianos/service/blob/a1c091bc7f8b6d1a8d28b47d8cf22068608051ef/service_aix.go#L45

func getPidOfSvcMaster() int {
	pat := regexp.MustCompile(`\s+root\s+(\d+)\s+\d+\s+\d+\s+\w+\s+\d+\s+\S+\s+[0-9:]+\s+/usr/sbin/srcmstr`)
	cmd := exec.Command("ps", "-ef")
	var out bytes.Buffer
	cmd.Stdout = &out
	pid := 0
	if err := cmd.Run(); err == nil {
		matches := pat.FindAllStringSubmatch(out.String(),-1)
		for _, match := range matches {
			pid, _ = strconv.Atoi(match[1])
			break
		}
	}
	return pid
}

// [...]

func isInteractive() (bool, error) {
	// The PPid of a service process should match PID of srcmstr.
	return os.Getppid() != getPidOfSvcMaster(), nil
}

setharnold avatar Sep 23 '20 00:09 setharnold

Feel free to open a PR. I don't have access to AIX.

kardianos avatar Nov 16 '20 15:11 kardianos

Executing "ps -Ao pid, args" would indeed simplify the parser. I'd prefer to use pgrep, but it is not available for AIX

chbuescher avatar Nov 10 '21 14:11 chbuescher