When running a simple app, there are more OS threads than the max allowed by my processor.
This is the sample code:
package main
import (
"fmt"
"runtime"
"time"
)
func doSomething() {
time.Sleep(10 * time.Minute)
}
func main() {
maxProcs := runtime.GOMAXPROCS(0)
numCPU := runtime.NumCPU()
fmt.Println(maxProcs) // -> Prints 8
fmt.Println(numCPU) // -> Prints 8
for i := 0; i < 200; i++ {
go doSomething()
}
time.Sleep(20 * time.Minute)
}
Then, when running:
go build main.go
./main
I can see more than 8 OS threads related with the process:
ps -T 15419
PID SPID TTY STAT TIME COMMAND
15419 15419 pts/2 Sl+ 0:00 ./main
15419 15420 pts/2 Sl+ 0:00 ./main
15419 15421 pts/2 Sl+ 0:00 ./main
15419 15422 pts/2 Sl+ 0:00 ./main
15419 15423 pts/2 Sl+ 0:00 ./main
15419 15424 pts/2 Sl+ 0:00 ./main
15419 15425 pts/2 Sl+ 0:00 ./main
15419 15426 pts/2 Sl+ 0:00 ./main
15419 15427 pts/2 Sl+ 0:00 ./main
15419 15428 pts/2 Sl+ 0:00 ./main
15419 15429 pts/2 Sl+ 0:00 ./main
15419 15430 pts/2 Sl+ 0:00 ./main
As far as I know, I should expect upto 8 OS Threads only. Why do I get more than that?
Thanks!
Edit #2:
It seems I had a wrong understanding of time.Sleep. This post explains that Sleep is blocking, so the new OS Threads are expected.
Edit #1:
Please, correct me if I'm wrong, but I understand that, following pkg.go.dev/runtime documentation:
The GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go code simultaneously. There is no limit to the number of threads that can be blocked in system calls on behalf of Go code; those do not count against the GOMAXPROCS limit.
There should be 8 OS threads only for this application
The answer to your question is related to this one. Essentially, time.Sleep blocks the go routine so it allows you to have more than GOMAXPROCS goroutines running so long as the number of active threads does not exceed this value. Since doSomething just sleeps for 10 minutes and then exits, all 200 calls to it effectively don't contribute to this value at all.
Related
I'm facing an issue.
We have a clean script using to clean old files, and sometimes we need stop it for and will start it again later. Like the below processes. We use kill -STOP $pid and kill -CONT $pid in check.sh to control the clean.sh, $pid is all the pids of clean.sh (at there, they are 23939, 25804):
root 4321 0.0 0.0 74876 1184 ? Ss 2015 0:25 crond
root 23547 0.0 0.0 102084 1604 ? S 2015 0:00 \_ crond
root 23571 0.0 0.0 8728 972 ? Ss 2015 0:00 \_ /bin/bash -c bash /home/test/sbin/check.sh >>/home/test/log/check.log 2>&1
root 23577 0.0 0.0 8732 1092 ? S 2015 0:00 \_ bash /home/test/sbin/check.sh
root 23939 0.0 0.0 8860 1192 ? S 2015 0:45 \_ bash /home/test/bin/clean.sh 30
root 25804 0.0 0.0 8860 620 ? S 2015 0:00 \_ bash /home/test/bin/clean.sh 30
root 25805 0.0 0.0 14432 284 ? T 2015 0:00 \_ ls -d ./455bb4cba6142427156d2b959b8b0986/120x60/ ./455bb4cba6142427156d2b959b8b0986/80x
root 25808 0.0 0.0 3816 432 ? S 2015 0:00 \_ wc -l
Once the check.sh stopped clean.sh, hours later, check.sh started clean.sh, but there is a strange thing, after a stop and continue, there is a child process 'ls -d ....', it's still stopping.
Could you tell me if it's caused by wrong use of the signal? And how can I modify it?
ok, same like my description is not clear, my bad English...
Not sure what's the reason, but there is a way to sovle it:
kill -CONT $pid
pkill -CONT -P $pid
This will continue the child process.
For example there are
ps -ax
PID TTY Stat Time Command
1 ? Ss 0:00 /sbin/init
.
.
.
1564 ? Sl 0:00 usr/bin/xyz
1569 ? Sl 0:00 gnome-terminal
.
.
1730 ? sl 0:00 gcaltool
1759 ? sl 0:00 firefox
I have to interrupt the process 1564
1564 ? Sl 0:00 usr/bin/xyz
1569 ? Sl 0:00 gnome-terminal
The terminal gets hanged if I interrupt the process PID 1564. I think it hangs because I am interrupting the terminal process. Is there any other way as I have to strace the process
process PID 1730,
process PID 1759,
which were generated at the last?
Any way to bypass the strace and interrupt of the terminal process?
I'm trying to automatically start a process when I enter runlevel 1 by init 1. It's a watchdog which has to send a life signal all the time therefore in runlevel 1 too! But when runlevel 1 is entered each process is killed and the system switches to runlevel S. I tried to tell linux to start my process by update-rc.d -f watchdog 99 1 S .. The resulting entries in /etc/rc1.d/ and /etc/rcS.d/ are:
/etc/rc1.d/:
.
.
.
S30killprocs -> ../init.d/killprocs
S90single -> ../init.d/single
S99watchdog -> ../init.d/watchdog
.
.
.
/etc/rcS.d/:
.
.
.
S01glibc.sh -> ../init.d/glibc.sh
S02hostname.sh -> ../init.d/hostname.sh
S99watchdog -> ../init.d/watchdog
.
.
.
A ps ax after runlevel 1 was entered sadfully doesn't return my watchdog process. I have to start it manually.
PID TTY STAT TIME COMMAND
1 ? Ss 0:00 init [S]
2 ? S 0:00 [kthreadd]
3 ? S 0:00 [ksoftirqd/0]
4 ? S 0:00 [kworker/0:0]
5 ? S 0:00 [kworker/u:0]
6 ? S 0:00 [rcu_kthread]
7 ? S< 0:00 [khelper]
8 ? S 0:00 [kworker/u:1]
104 ? S 0:00 [sync_supers]
106 ? S 0:00 [bdi-default]
108 ? S< 0:00 [kblockd]
119 ? S 0:00 [khubd]
219 ? S 0:00 [kswapd0]
220 ? S 0:00 [fsnotify_mark]
221 ? S< 0:00 [aio]
314 ? S< 0:00 [scsi_tgtd]
347 ? S< 0:00 [kpsmoused]
349 ? S 0:00 [kworker/0:1]
366 ? S 0:00 [w1_bus_master1]
390 ? S 0:00 [mmcqd/0]
395 ? S 0:00 [jbd2/mmcblk0p2-]
396 ? S< 0:00 [ext4-dio-unwrit]
475 ? S 0:00 [flush-179:0]
4532 ttyS0 Ss 0:00 init [S]
4533 ttyS0 S 0:00 bash
4536 ttyS0 R+ 0:00 ps ax
The system I'm working on is Debian GNU/Linux 5.0 debarm (embedded). Any hints or solution? Thank you.
As said here:
Run Level 1 is known as 'single user' mode. A more apt description
would be 'rescue', or 'trouble-shooting' mode. In run level 1, no
daemons (services) are started.
You have several options to get around this limitation:
Don't use runlevel 1, this is not what it is meant for
Start the watchdog on login (if this is a valid option), e.g. .bashrc.
I solved it by adding following line into /etc/inittab:
~~:S:wait:/etc/init.d/watchdog start
~~:S:wait:/sbin/sulogin
Note: It must be before /sbin/sulogin.
I connect to a remote server using SSH
I was compiling using cmake and then make, it's not common to have a progress percentage in compilation process, but this time it has. I was watching the compilation process until my internet connection failed, so puTTY closed the session and I had to connect again to my server. I though that all the progress was lost, but i first make sure by watching the processes list by ps aux command, and I noticed that the processes related to the compilation are still running:
1160 tty1 Ss+ 0:00 /sbin/mingetty tty1
2265 ? Ss 0:00 sshd: root#pts/1
2269 pts/1 Ss 0:00 -bash
2353 pts/1 S+ 0:00 make
2356 pts/1 S+ 0:00 make -f CMakeFiles/Makefile2 all
2952 ? S 0:00 pickup -l -t fifo -u
3085 ? Ss 0:00 sshd: root#pts/0
3089 pts/0 Ss 0:00 -bash
3500 pts/1 S+ 0:01 make -f src/compiler/CMakeFiles/hphp_analysis.dir/bui
3509 pts/1 S+ 0:00 /bin/sh -c cd /root/hiphop/hiphop-php/src/compiler &&
3510 pts/1 S+ 0:00 /usr/bin/g++44 -DNO_JEMALLOC=1 -DNO_TCMALLOC=1 -D_GNU
3511 pts/1 R+ 0:03 /usr/libexec/gcc/x86_64-redhat-linux6E/4.4.4/cc1plus
3512 pts/0 R+ 0:00 ps ax
I would like to know if is possible to watch the current progress of the compilation by watching the previously closed terminal output. Something similar like 'cat /dev/vcsa1' or something
As per the comment above, you should have used screen.
As it is, you could try to peek at the file descriptors used by sshd and the shell that you started, but I don't think this will get you very far.
We have a convention whereby developers get into a server with their own username, and then sudo su - django where django is the user our apps run under.
I need to find out which developer is running a script as django. With ps faux :
root 26438 0.0 0.0 90152 3320 ? Ss 10:38 0:00 \_ sshd: fred [priv]
fred 26444 0.0 0.0 90152 1852 ? S 10:38 0:00 | \_ sshd: fred#pts/0
fred 26445 0.0 0.0 66052 1560 pts/0 Ss 10:38 0:00 | \_ -bash
root 27923 0.0 0.0 101052 1336 pts/0 S 10:46 0:00 | \_ su - django
django 27924 0.0 0.0 66188 1752 pts/0 S 10:46 0:00 | \_ - bash
django 31760 0.0 0.5 227028 42320 pts/0 S+ 11:10 0:01 | \_ python target_script.py
I can easily see what fred is up to. However I need to write a script to act on this info, and I can find no way to pull out "fred" and "target_script.py" in one line with ps ... euser,ruser,suser,fuser all say "django." Will I need to fumble through this ps faux output to get the info I need?
I found this old post when trying to find the same basic information. The easiest way I found was to use the "loginuid" file under /proc/[pid]. For example:
cat /proc/${processid}/loginuid
Sorry for resurrecting such and old post, but maybe someone will find it useful.
You used su - django. The " - " will make the new shell a login shell (see manpage of su), which let the child process forget its parent uids. That's why euser,ruser,suser,fuser all say "django".
So yes, you may have to fumble through the parent process id, or through "ps faux".