CPU usage of Java + JDBC
While testing my webapp under load I got following top
excerpt under Linux:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3964 nobody 20 0 4965m 622m 6048 S 8.5 11.0 6:02.49 java
1985 mysql 20 0 294m 125m 3804 S 2.1 2.2 0:05.39 /usr/sbin/mysqld
I need an explanation on java's %CPU
column. As I understand it, during web request one of java
's thread is performing some pure Java logic, consuming some o开发者_开发百科f CPU time (let it be 5ms). Then it connects to database via JDBC, sends some sql query and waits, say 10ms, for response. These 10ms are obviously counted as /usr/sbin/mysqld
CPU usage. Then java
thread resumes it's operation and finishes, consuming another 20ms and amounting to 5+10+20=30ms total execution time.
And the newbie question is: don't you think that these db-related 10ms are counted twice: one time as java thread waiting for db to process the query and second time as database CPU usage itself? What don't I understand here?
When a process is simply waiting on a socket it will not normally be using significant amounts of CPU. Basically, the kernel knows that it is in a waiting state and will not schedule it to run - except for little things like signal handling, which can be used e.g. to run the garbage collector regularly.
(OK, if there is a lot of garbage to collect, that "little thing" might not be so little.)
精彩评论